1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * The Huawei Cache Coherence System (HCCS) is a multi-chip interconnection
4 * bus protocol.
5 *
6 * Copyright (c) 2023 Hisilicon Limited.
7 * Author: Huisong Li <lihuisong@huawei.com>
8 *
9 * HCCS driver for Kunpeng SoC provides the following features:
10 * - Retrieve the following information about each port:
11 * - port type
12 * - lane mode
13 * - enable
14 * - current lane mode
15 * - link finite state machine
16 * - lane mask
17 * - CRC error count
18 *
19 * - Retrieve the following information about all the ports on the chip or
20 * the die:
21 * - if all enabled ports are in linked
22 * - if all linked ports are in full lane
23 * - CRC error count sum
24 *
25 * - Retrieve all HCCS types used on the platform.
26 *
27 * - Support low power feature for all specified HCCS type ports, and
28 * provide the following interface:
29 * - query HCCS types supported increasing and decreasing lane number.
30 * - decrease lane number of all specified HCCS type ports on idle state.
31 * - increase lane number of all specified HCCS type ports.
32 */
33 #include <linux/acpi.h>
34 #include <linux/delay.h>
35 #include <linux/iopoll.h>
36 #include <linux/platform_device.h>
37 #include <linux/stringify.h>
38 #include <linux/sysfs.h>
39 #include <linux/types.h>
40
41 #include <acpi/pcc.h>
42
43 #include "kunpeng_hccs.h"
44
45 /*
46 * Arbitrary retries in case the remote processor is slow to respond
47 * to PCC commands
48 */
49 #define HCCS_PCC_CMD_WAIT_RETRIES_NUM 500ULL
50 #define HCCS_POLL_STATUS_TIME_INTERVAL_US 3
51
kobj_to_port_info(struct kobject * k)52 static struct hccs_port_info *kobj_to_port_info(struct kobject *k)
53 {
54 return container_of(k, struct hccs_port_info, kobj);
55 }
56
kobj_to_die_info(struct kobject * k)57 static struct hccs_die_info *kobj_to_die_info(struct kobject *k)
58 {
59 return container_of(k, struct hccs_die_info, kobj);
60 }
61
kobj_to_chip_info(struct kobject * k)62 static struct hccs_chip_info *kobj_to_chip_info(struct kobject *k)
63 {
64 return container_of(k, struct hccs_chip_info, kobj);
65 }
66
device_kobj_to_hccs_dev(struct kobject * k)67 static struct hccs_dev *device_kobj_to_hccs_dev(struct kobject *k)
68 {
69 struct device *dev = container_of(k, struct device, kobj);
70 struct platform_device *pdev =
71 container_of(dev, struct platform_device, dev);
72
73 return platform_get_drvdata(pdev);
74 }
75
hccs_port_type_to_name(struct hccs_dev * hdev,u8 type)76 static char *hccs_port_type_to_name(struct hccs_dev *hdev, u8 type)
77 {
78 u16 i;
79
80 for (i = 0; i < hdev->used_type_num; i++) {
81 if (hdev->type_name_maps[i].type == type)
82 return hdev->type_name_maps[i].name;
83 }
84
85 return NULL;
86 }
87
hccs_name_to_port_type(struct hccs_dev * hdev,const char * name,u8 * type)88 static int hccs_name_to_port_type(struct hccs_dev *hdev,
89 const char *name, u8 *type)
90 {
91 u16 i;
92
93 for (i = 0; i < hdev->used_type_num; i++) {
94 if (strcmp(hdev->type_name_maps[i].name, name) == 0) {
95 *type = hdev->type_name_maps[i].type;
96 return 0;
97 }
98 }
99
100 return -EINVAL;
101 }
102
103 struct hccs_register_ctx {
104 struct device *dev;
105 u8 chan_id;
106 int err;
107 };
108
hccs_get_register_cb(struct acpi_resource * ares,void * context)109 static acpi_status hccs_get_register_cb(struct acpi_resource *ares,
110 void *context)
111 {
112 struct acpi_resource_generic_register *reg;
113 struct hccs_register_ctx *ctx = context;
114
115 if (ares->type != ACPI_RESOURCE_TYPE_GENERIC_REGISTER)
116 return AE_OK;
117
118 reg = &ares->data.generic_reg;
119 if (reg->space_id != ACPI_ADR_SPACE_PLATFORM_COMM) {
120 dev_err(ctx->dev, "Bad register resource.\n");
121 ctx->err = -EINVAL;
122 return AE_ERROR;
123 }
124 ctx->chan_id = reg->access_size;
125
126 return AE_OK;
127 }
128
hccs_get_pcc_chan_id(struct hccs_dev * hdev)129 static int hccs_get_pcc_chan_id(struct hccs_dev *hdev)
130 {
131 acpi_handle handle = ACPI_HANDLE(hdev->dev);
132 struct hccs_register_ctx ctx = {0};
133 acpi_status status;
134
135 if (!acpi_has_method(handle, METHOD_NAME__CRS)) {
136 dev_err(hdev->dev, "No _CRS method.\n");
137 return -ENODEV;
138 }
139
140 ctx.dev = hdev->dev;
141 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
142 hccs_get_register_cb, &ctx);
143 if (ACPI_FAILURE(status))
144 return ctx.err;
145 hdev->chan_id = ctx.chan_id;
146
147 return 0;
148 }
149
hccs_chan_tx_done(struct mbox_client * cl,void * msg,int ret)150 static void hccs_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
151 {
152 if (ret < 0)
153 pr_debug("TX did not complete: CMD sent:0x%x, ret:%d\n",
154 *(u8 *)msg, ret);
155 else
156 pr_debug("TX completed. CMD sent:0x%x, ret:%d\n",
157 *(u8 *)msg, ret);
158 }
159
hccs_pcc_rx_callback(struct mbox_client * cl,void * mssg)160 static void hccs_pcc_rx_callback(struct mbox_client *cl, void *mssg)
161 {
162 struct hccs_mbox_client_info *cl_info =
163 container_of(cl, struct hccs_mbox_client_info, client);
164
165 complete(&cl_info->done);
166 }
167
hccs_unregister_pcc_channel(struct hccs_dev * hdev)168 static void hccs_unregister_pcc_channel(struct hccs_dev *hdev)
169 {
170 pcc_mbox_free_channel(hdev->cl_info.pcc_chan);
171 }
172
hccs_register_pcc_channel(struct hccs_dev * hdev)173 static int hccs_register_pcc_channel(struct hccs_dev *hdev)
174 {
175 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
176 struct mbox_client *cl = &cl_info->client;
177 struct pcc_mbox_chan *pcc_chan;
178 struct mbox_chan *mbox_chan;
179 struct device *dev = hdev->dev;
180 int rc;
181
182 cl->dev = dev;
183 cl->tx_block = false;
184 cl->knows_txdone = true;
185 cl->tx_done = hccs_chan_tx_done;
186 cl->rx_callback = hdev->verspec_data->rx_callback;
187 init_completion(&cl_info->done);
188
189 pcc_chan = pcc_mbox_request_channel(cl, hdev->chan_id);
190 if (IS_ERR(pcc_chan)) {
191 dev_err(dev, "PCC channel request failed.\n");
192 rc = -ENODEV;
193 goto out;
194 }
195 cl_info->pcc_chan = pcc_chan;
196 mbox_chan = pcc_chan->mchan;
197
198 /*
199 * pcc_chan->latency is just a nominal value. In reality the remote
200 * processor could be much slower to reply. So add an arbitrary amount
201 * of wait on top of nominal.
202 */
203 cl_info->deadline_us =
204 HCCS_PCC_CMD_WAIT_RETRIES_NUM * pcc_chan->latency;
205 if (!hdev->verspec_data->has_txdone_irq &&
206 mbox_chan->mbox->txdone_irq) {
207 dev_err(dev, "PCC IRQ in PCCT is enabled.\n");
208 rc = -EINVAL;
209 goto err_mbx_channel_free;
210 } else if (hdev->verspec_data->has_txdone_irq &&
211 !mbox_chan->mbox->txdone_irq) {
212 dev_err(dev, "PCC IRQ in PCCT isn't supported.\n");
213 rc = -EINVAL;
214 goto err_mbx_channel_free;
215 }
216
217 if (pcc_chan->shmem_size != HCCS_PCC_SHARE_MEM_BYTES) {
218 dev_err(dev, "Base size (%llu) of PCC communication region must be %d bytes.\n",
219 pcc_chan->shmem_size, HCCS_PCC_SHARE_MEM_BYTES);
220 rc = -EINVAL;
221 goto err_mbx_channel_free;
222 }
223
224 return 0;
225
226 err_mbx_channel_free:
227 pcc_mbox_free_channel(cl_info->pcc_chan);
228 out:
229 return rc;
230 }
231
hccs_wait_cmd_complete_by_poll(struct hccs_dev * hdev)232 static int hccs_wait_cmd_complete_by_poll(struct hccs_dev *hdev)
233 {
234 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
235 struct acpi_pcct_shared_memory __iomem *comm_base =
236 cl_info->pcc_chan->shmem;
237 u16 status;
238 int ret;
239
240 /*
241 * Poll PCC status register every 3us(delay_us) for maximum of
242 * deadline_us(timeout_us) until PCC command complete bit is set(cond)
243 */
244 ret = readw_poll_timeout(&comm_base->status, status,
245 status & PCC_STATUS_CMD_COMPLETE,
246 HCCS_POLL_STATUS_TIME_INTERVAL_US,
247 cl_info->deadline_us);
248 if (unlikely(ret))
249 dev_err(hdev->dev, "poll PCC status failed, ret = %d.\n", ret);
250
251 return ret;
252 }
253
hccs_wait_cmd_complete_by_irq(struct hccs_dev * hdev)254 static int hccs_wait_cmd_complete_by_irq(struct hccs_dev *hdev)
255 {
256 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
257
258 if (!wait_for_completion_timeout(&cl_info->done,
259 usecs_to_jiffies(cl_info->deadline_us))) {
260 dev_err(hdev->dev, "PCC command executed timeout!\n");
261 return -ETIMEDOUT;
262 }
263
264 return 0;
265 }
266
hccs_fill_pcc_shared_mem_region(struct hccs_dev * hdev,u8 cmd,struct hccs_desc * desc,void __iomem * comm_space,u16 space_size)267 static inline void hccs_fill_pcc_shared_mem_region(struct hccs_dev *hdev,
268 u8 cmd,
269 struct hccs_desc *desc,
270 void __iomem *comm_space,
271 u16 space_size)
272 {
273 struct acpi_pcct_shared_memory tmp = {
274 .signature = PCC_SIGNATURE | hdev->chan_id,
275 .command = cmd,
276 .status = 0,
277 };
278
279 memcpy_toio(hdev->cl_info.pcc_chan->shmem, (void *)&tmp,
280 sizeof(struct acpi_pcct_shared_memory));
281
282 /* Copy the message to the PCC comm space */
283 memcpy_toio(comm_space, (void *)desc, space_size);
284 }
285
hccs_fill_ext_pcc_shared_mem_region(struct hccs_dev * hdev,u8 cmd,struct hccs_desc * desc,void __iomem * comm_space,u16 space_size)286 static inline void hccs_fill_ext_pcc_shared_mem_region(struct hccs_dev *hdev,
287 u8 cmd,
288 struct hccs_desc *desc,
289 void __iomem *comm_space,
290 u16 space_size)
291 {
292 struct acpi_pcct_ext_pcc_shared_memory tmp = {
293 .signature = PCC_SIGNATURE | hdev->chan_id,
294 .flags = PCC_CMD_COMPLETION_NOTIFY,
295 .length = HCCS_PCC_SHARE_MEM_BYTES,
296 .command = cmd,
297 };
298
299 memcpy_toio(hdev->cl_info.pcc_chan->shmem, (void *)&tmp,
300 sizeof(struct acpi_pcct_ext_pcc_shared_memory));
301
302 /* Copy the message to the PCC comm space */
303 memcpy_toio(comm_space, (void *)desc, space_size);
304 }
305
hccs_pcc_cmd_send(struct hccs_dev * hdev,u8 cmd,struct hccs_desc * desc)306 static int hccs_pcc_cmd_send(struct hccs_dev *hdev, u8 cmd,
307 struct hccs_desc *desc)
308 {
309 const struct hccs_verspecific_data *verspec_data = hdev->verspec_data;
310 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
311 struct mbox_chan *mbox_chan = cl_info->pcc_chan->mchan;
312 struct hccs_fw_inner_head *fw_inner_head;
313 void __iomem *comm_space;
314 u16 space_size;
315 int ret;
316
317 comm_space = cl_info->pcc_chan->shmem + verspec_data->shared_mem_size;
318 space_size = HCCS_PCC_SHARE_MEM_BYTES - verspec_data->shared_mem_size;
319 verspec_data->fill_pcc_shared_mem(hdev, cmd, desc,
320 comm_space, space_size);
321 if (verspec_data->has_txdone_irq)
322 reinit_completion(&cl_info->done);
323
324 /* Ring doorbell */
325 ret = mbox_send_message(mbox_chan, &cmd);
326 if (ret < 0) {
327 dev_err(hdev->dev, "Send PCC mbox message failed, ret = %d.\n",
328 ret);
329 goto end;
330 }
331
332 ret = verspec_data->wait_cmd_complete(hdev);
333 if (ret)
334 goto end;
335
336 /* Copy response data */
337 memcpy_fromio((void *)desc, comm_space, space_size);
338 fw_inner_head = &desc->rsp.fw_inner_head;
339 if (fw_inner_head->retStatus) {
340 dev_err(hdev->dev, "Execute PCC command failed, error code = %u.\n",
341 fw_inner_head->retStatus);
342 ret = -EIO;
343 }
344
345 end:
346 if (verspec_data->has_txdone_irq)
347 mbox_chan_txdone(mbox_chan, ret);
348 else
349 mbox_client_txdone(mbox_chan, ret);
350 return ret;
351 }
352
hccs_init_req_desc(struct hccs_desc * desc)353 static void hccs_init_req_desc(struct hccs_desc *desc)
354 {
355 struct hccs_req_desc *req = &desc->req;
356
357 memset(desc, 0, sizeof(*desc));
358 req->req_head.module_code = HCCS_SERDES_MODULE_CODE;
359 }
360
hccs_get_dev_caps(struct hccs_dev * hdev)361 static int hccs_get_dev_caps(struct hccs_dev *hdev)
362 {
363 struct hccs_desc desc;
364 int ret;
365
366 hccs_init_req_desc(&desc);
367 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DEV_CAP, &desc);
368 if (ret) {
369 dev_err(hdev->dev, "Get device capabilities failed, ret = %d.\n",
370 ret);
371 return ret;
372 }
373 memcpy(&hdev->caps, desc.rsp.data, sizeof(hdev->caps));
374
375 return 0;
376 }
377
hccs_query_chip_num_on_platform(struct hccs_dev * hdev)378 static int hccs_query_chip_num_on_platform(struct hccs_dev *hdev)
379 {
380 struct hccs_desc desc;
381 int ret;
382
383 hccs_init_req_desc(&desc);
384 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_CHIP_NUM, &desc);
385 if (ret) {
386 dev_err(hdev->dev, "query system chip number failed, ret = %d.\n",
387 ret);
388 return ret;
389 }
390
391 hdev->chip_num = *((u8 *)&desc.rsp.data);
392 if (!hdev->chip_num) {
393 dev_err(hdev->dev, "chip num obtained from firmware is zero.\n");
394 return -EINVAL;
395 }
396
397 return 0;
398 }
399
hccs_get_chip_info(struct hccs_dev * hdev,struct hccs_chip_info * chip)400 static int hccs_get_chip_info(struct hccs_dev *hdev,
401 struct hccs_chip_info *chip)
402 {
403 struct hccs_die_num_req_param *req_param;
404 struct hccs_desc desc;
405 int ret;
406
407 hccs_init_req_desc(&desc);
408 req_param = (struct hccs_die_num_req_param *)desc.req.data;
409 req_param->chip_id = chip->chip_id;
410 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_NUM, &desc);
411 if (ret)
412 return ret;
413
414 chip->die_num = *((u8 *)&desc.rsp.data);
415
416 return 0;
417 }
418
hccs_query_chip_info_on_platform(struct hccs_dev * hdev)419 static int hccs_query_chip_info_on_platform(struct hccs_dev *hdev)
420 {
421 struct hccs_chip_info *chip;
422 int ret;
423 u8 idx;
424
425 ret = hccs_query_chip_num_on_platform(hdev);
426 if (ret) {
427 dev_err(hdev->dev, "query chip number on platform failed, ret = %d.\n",
428 ret);
429 return ret;
430 }
431
432 hdev->chips = devm_kzalloc(hdev->dev,
433 hdev->chip_num * sizeof(struct hccs_chip_info),
434 GFP_KERNEL);
435 if (!hdev->chips) {
436 dev_err(hdev->dev, "allocate all chips memory failed.\n");
437 return -ENOMEM;
438 }
439
440 for (idx = 0; idx < hdev->chip_num; idx++) {
441 chip = &hdev->chips[idx];
442 chip->chip_id = idx;
443 ret = hccs_get_chip_info(hdev, chip);
444 if (ret) {
445 dev_err(hdev->dev, "get chip%u info failed, ret = %d.\n",
446 idx, ret);
447 return ret;
448 }
449 chip->hdev = hdev;
450 }
451
452 return 0;
453 }
454
hccs_query_die_info_on_chip(struct hccs_dev * hdev,u8 chip_id,u8 die_idx,struct hccs_die_info * die)455 static int hccs_query_die_info_on_chip(struct hccs_dev *hdev, u8 chip_id,
456 u8 die_idx, struct hccs_die_info *die)
457 {
458 struct hccs_die_info_req_param *req_param;
459 struct hccs_die_info_rsp_data *rsp_data;
460 struct hccs_desc desc;
461 int ret;
462
463 hccs_init_req_desc(&desc);
464 req_param = (struct hccs_die_info_req_param *)desc.req.data;
465 req_param->chip_id = chip_id;
466 req_param->die_idx = die_idx;
467 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_INFO, &desc);
468 if (ret)
469 return ret;
470
471 rsp_data = (struct hccs_die_info_rsp_data *)desc.rsp.data;
472 die->die_id = rsp_data->die_id;
473 die->port_num = rsp_data->port_num;
474 die->min_port_id = rsp_data->min_port_id;
475 die->max_port_id = rsp_data->max_port_id;
476 if (die->min_port_id > die->max_port_id) {
477 dev_err(hdev->dev, "min port id(%u) > max port id(%u) on die_idx(%u).\n",
478 die->min_port_id, die->max_port_id, die_idx);
479 return -EINVAL;
480 }
481 if (die->max_port_id > HCCS_DIE_MAX_PORT_ID) {
482 dev_err(hdev->dev, "max port id(%u) on die_idx(%u) is too big.\n",
483 die->max_port_id, die_idx);
484 return -EINVAL;
485 }
486
487 return 0;
488 }
489
hccs_query_all_die_info_on_platform(struct hccs_dev * hdev)490 static int hccs_query_all_die_info_on_platform(struct hccs_dev *hdev)
491 {
492 struct device *dev = hdev->dev;
493 struct hccs_chip_info *chip;
494 struct hccs_die_info *die;
495 bool has_die_info = false;
496 u8 i, j;
497 int ret;
498
499 for (i = 0; i < hdev->chip_num; i++) {
500 chip = &hdev->chips[i];
501 if (!chip->die_num)
502 continue;
503
504 has_die_info = true;
505 chip->dies = devm_kzalloc(hdev->dev,
506 chip->die_num * sizeof(struct hccs_die_info),
507 GFP_KERNEL);
508 if (!chip->dies) {
509 dev_err(dev, "allocate all dies memory on chip%u failed.\n",
510 i);
511 return -ENOMEM;
512 }
513
514 for (j = 0; j < chip->die_num; j++) {
515 die = &chip->dies[j];
516 ret = hccs_query_die_info_on_chip(hdev, i, j, die);
517 if (ret) {
518 dev_err(dev, "get die idx (%u) info on chip%u failed, ret = %d.\n",
519 j, i, ret);
520 return ret;
521 }
522 die->chip = chip;
523 }
524 }
525
526 return has_die_info ? 0 : -EINVAL;
527 }
528
hccs_get_bd_info(struct hccs_dev * hdev,u8 opcode,struct hccs_desc * desc,void * buf,size_t buf_len,struct hccs_rsp_head * rsp_head)529 static int hccs_get_bd_info(struct hccs_dev *hdev, u8 opcode,
530 struct hccs_desc *desc,
531 void *buf, size_t buf_len,
532 struct hccs_rsp_head *rsp_head)
533 {
534 struct hccs_rsp_head *head;
535 struct hccs_rsp_desc *rsp;
536 int ret;
537
538 ret = hccs_pcc_cmd_send(hdev, opcode, desc);
539 if (ret)
540 return ret;
541
542 rsp = &desc->rsp;
543 head = &rsp->rsp_head;
544 if (head->data_len > buf_len) {
545 dev_err(hdev->dev,
546 "buffer overflow (buf_len = %zu, data_len = %u)!\n",
547 buf_len, head->data_len);
548 return -ENOMEM;
549 }
550
551 memcpy(buf, rsp->data, head->data_len);
552 *rsp_head = *head;
553
554 return 0;
555 }
556
hccs_get_all_port_attr(struct hccs_dev * hdev,struct hccs_die_info * die,struct hccs_port_attr * attrs,u16 size)557 static int hccs_get_all_port_attr(struct hccs_dev *hdev,
558 struct hccs_die_info *die,
559 struct hccs_port_attr *attrs, u16 size)
560 {
561 struct hccs_die_comm_req_param *req_param;
562 struct hccs_req_head *req_head;
563 struct hccs_rsp_head rsp_head;
564 struct hccs_desc desc;
565 size_t left_buf_len;
566 u32 data_len = 0;
567 u8 start_id;
568 u8 *buf;
569 int ret;
570
571 buf = (u8 *)attrs;
572 left_buf_len = sizeof(struct hccs_port_attr) * size;
573 start_id = die->min_port_id;
574 while (start_id <= die->max_port_id) {
575 hccs_init_req_desc(&desc);
576 req_head = &desc.req.req_head;
577 req_head->start_id = start_id;
578 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
579 req_param->chip_id = die->chip->chip_id;
580 req_param->die_id = die->die_id;
581
582 ret = hccs_get_bd_info(hdev, HCCS_GET_DIE_PORT_INFO, &desc,
583 buf + data_len, left_buf_len, &rsp_head);
584 if (ret) {
585 dev_err(hdev->dev,
586 "get the information of port%u on die%u failed, ret = %d.\n",
587 start_id, die->die_id, ret);
588 return ret;
589 }
590
591 data_len += rsp_head.data_len;
592 left_buf_len -= rsp_head.data_len;
593 if (unlikely(rsp_head.next_id <= start_id)) {
594 dev_err(hdev->dev,
595 "next port id (%u) is not greater than last start id (%u) on die%u.\n",
596 rsp_head.next_id, start_id, die->die_id);
597 return -EINVAL;
598 }
599 start_id = rsp_head.next_id;
600 }
601
602 if (left_buf_len != 0) {
603 dev_err(hdev->dev, "failed to get the expected port number(%u) attribute.\n",
604 size);
605 return -EINVAL;
606 }
607
608 return 0;
609 }
610
hccs_get_all_port_info_on_die(struct hccs_dev * hdev,struct hccs_die_info * die)611 static int hccs_get_all_port_info_on_die(struct hccs_dev *hdev,
612 struct hccs_die_info *die)
613 {
614 struct hccs_port_attr *attrs;
615 struct hccs_port_info *port;
616 int ret;
617 u8 i;
618
619 attrs = kzalloc_objs(struct hccs_port_attr, die->port_num);
620 if (!attrs)
621 return -ENOMEM;
622
623 ret = hccs_get_all_port_attr(hdev, die, attrs, die->port_num);
624 if (ret)
625 goto out;
626
627 for (i = 0; i < die->port_num; i++) {
628 port = &die->ports[i];
629 port->port_id = attrs[i].port_id;
630 port->port_type = attrs[i].port_type;
631 port->max_lane_num = attrs[i].max_lane_num;
632 port->enable = attrs[i].enable;
633 port->die = die;
634 }
635
636 out:
637 kfree(attrs);
638 return ret;
639 }
640
hccs_query_all_port_info_on_platform(struct hccs_dev * hdev)641 static int hccs_query_all_port_info_on_platform(struct hccs_dev *hdev)
642 {
643 struct device *dev = hdev->dev;
644 struct hccs_chip_info *chip;
645 struct hccs_die_info *die;
646 bool has_port_info = false;
647 u8 i, j;
648 int ret;
649
650 for (i = 0; i < hdev->chip_num; i++) {
651 chip = &hdev->chips[i];
652 for (j = 0; j < chip->die_num; j++) {
653 die = &chip->dies[j];
654 if (!die->port_num)
655 continue;
656
657 has_port_info = true;
658 die->ports = devm_kzalloc(dev,
659 die->port_num * sizeof(struct hccs_port_info),
660 GFP_KERNEL);
661 if (!die->ports) {
662 dev_err(dev, "allocate ports memory on chip%u/die%u failed.\n",
663 i, die->die_id);
664 return -ENOMEM;
665 }
666
667 ret = hccs_get_all_port_info_on_die(hdev, die);
668 if (ret) {
669 dev_err(dev, "get all port info on chip%u/die%u failed, ret = %d.\n",
670 i, die->die_id, ret);
671 return ret;
672 }
673 }
674 }
675
676 return has_port_info ? 0 : -EINVAL;
677 }
678
hccs_get_hw_info(struct hccs_dev * hdev)679 static int hccs_get_hw_info(struct hccs_dev *hdev)
680 {
681 int ret;
682
683 ret = hccs_query_chip_info_on_platform(hdev);
684 if (ret) {
685 dev_err(hdev->dev, "query chip info on platform failed, ret = %d.\n",
686 ret);
687 return ret;
688 }
689
690 ret = hccs_query_all_die_info_on_platform(hdev);
691 if (ret) {
692 dev_err(hdev->dev, "query all die info on platform failed, ret = %d.\n",
693 ret);
694 return ret;
695 }
696
697 ret = hccs_query_all_port_info_on_platform(hdev);
698 if (ret) {
699 dev_err(hdev->dev, "query all port info on platform failed, ret = %d.\n",
700 ret);
701 return ret;
702 }
703
704 return 0;
705 }
706
hccs_calc_used_type_num(struct hccs_dev * hdev,unsigned long * hccs_ver)707 static u16 hccs_calc_used_type_num(struct hccs_dev *hdev,
708 unsigned long *hccs_ver)
709 {
710 struct hccs_chip_info *chip;
711 struct hccs_port_info *port;
712 struct hccs_die_info *die;
713 u16 used_type_num = 0;
714 u16 i, j, k;
715
716 for (i = 0; i < hdev->chip_num; i++) {
717 chip = &hdev->chips[i];
718 for (j = 0; j < chip->die_num; j++) {
719 die = &chip->dies[j];
720 for (k = 0; k < die->port_num; k++) {
721 port = &die->ports[k];
722 set_bit(port->port_type, hccs_ver);
723 }
724 }
725 }
726
727 for_each_set_bit(i, hccs_ver, HCCS_IP_MAX + 1)
728 used_type_num++;
729
730 return used_type_num;
731 }
732
hccs_init_type_name_maps(struct hccs_dev * hdev)733 static int hccs_init_type_name_maps(struct hccs_dev *hdev)
734 {
735 DECLARE_BITMAP(hccs_ver, HCCS_IP_MAX + 1) = {};
736 unsigned int i;
737 u16 idx = 0;
738
739 hdev->used_type_num = hccs_calc_used_type_num(hdev, hccs_ver);
740 hdev->type_name_maps = devm_kcalloc(hdev->dev, hdev->used_type_num,
741 sizeof(struct hccs_type_name_map),
742 GFP_KERNEL);
743 if (!hdev->type_name_maps)
744 return -ENOMEM;
745
746 for_each_set_bit(i, hccs_ver, HCCS_IP_MAX + 1) {
747 hdev->type_name_maps[idx].type = i;
748 sprintf(hdev->type_name_maps[idx].name,
749 "%s%u", HCCS_IP_PREFIX, i);
750 idx++;
751 }
752
753 return 0;
754 }
755
hccs_query_port_link_status(struct hccs_dev * hdev,const struct hccs_port_info * port,struct hccs_link_status * link_status)756 static int hccs_query_port_link_status(struct hccs_dev *hdev,
757 const struct hccs_port_info *port,
758 struct hccs_link_status *link_status)
759 {
760 const struct hccs_die_info *die = port->die;
761 const struct hccs_chip_info *chip = die->chip;
762 struct hccs_port_comm_req_param *req_param;
763 struct hccs_desc desc;
764 int ret;
765
766 hccs_init_req_desc(&desc);
767 req_param = (struct hccs_port_comm_req_param *)desc.req.data;
768 req_param->chip_id = chip->chip_id;
769 req_param->die_id = die->die_id;
770 req_param->port_id = port->port_id;
771 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_LINK_STATUS, &desc);
772 if (ret) {
773 dev_err(hdev->dev,
774 "get port link status info failed, ret = %d.\n", ret);
775 return ret;
776 }
777
778 *link_status = *((struct hccs_link_status *)desc.rsp.data);
779
780 return 0;
781 }
782
hccs_query_port_crc_err_cnt(struct hccs_dev * hdev,const struct hccs_port_info * port,u64 * crc_err_cnt)783 static int hccs_query_port_crc_err_cnt(struct hccs_dev *hdev,
784 const struct hccs_port_info *port,
785 u64 *crc_err_cnt)
786 {
787 const struct hccs_die_info *die = port->die;
788 const struct hccs_chip_info *chip = die->chip;
789 struct hccs_port_comm_req_param *req_param;
790 struct hccs_desc desc;
791 int ret;
792
793 hccs_init_req_desc(&desc);
794 req_param = (struct hccs_port_comm_req_param *)desc.req.data;
795 req_param->chip_id = chip->chip_id;
796 req_param->die_id = die->die_id;
797 req_param->port_id = port->port_id;
798 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_CRC_ERR_CNT, &desc);
799 if (ret) {
800 dev_err(hdev->dev,
801 "get port crc error count failed, ret = %d.\n", ret);
802 return ret;
803 }
804
805 memcpy(crc_err_cnt, &desc.rsp.data, sizeof(u64));
806
807 return 0;
808 }
809
hccs_get_die_all_link_status(struct hccs_dev * hdev,const struct hccs_die_info * die,u8 * all_linked)810 static int hccs_get_die_all_link_status(struct hccs_dev *hdev,
811 const struct hccs_die_info *die,
812 u8 *all_linked)
813 {
814 struct hccs_die_comm_req_param *req_param;
815 struct hccs_desc desc;
816 int ret;
817
818 if (die->port_num == 0) {
819 *all_linked = 1;
820 return 0;
821 }
822
823 hccs_init_req_desc(&desc);
824 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
825 req_param->chip_id = die->chip->chip_id;
826 req_param->die_id = die->die_id;
827 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LINK_STA, &desc);
828 if (ret) {
829 dev_err(hdev->dev,
830 "get link status of all ports failed on die%u, ret = %d.\n",
831 die->die_id, ret);
832 return ret;
833 }
834
835 *all_linked = *((u8 *)&desc.rsp.data);
836
837 return 0;
838 }
839
hccs_get_die_all_port_lane_status(struct hccs_dev * hdev,const struct hccs_die_info * die,u8 * full_lane)840 static int hccs_get_die_all_port_lane_status(struct hccs_dev *hdev,
841 const struct hccs_die_info *die,
842 u8 *full_lane)
843 {
844 struct hccs_die_comm_req_param *req_param;
845 struct hccs_desc desc;
846 int ret;
847
848 if (die->port_num == 0) {
849 *full_lane = 1;
850 return 0;
851 }
852
853 hccs_init_req_desc(&desc);
854 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
855 req_param->chip_id = die->chip->chip_id;
856 req_param->die_id = die->die_id;
857 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LANE_STA, &desc);
858 if (ret) {
859 dev_err(hdev->dev, "get lane status of all ports failed on die%u, ret = %d.\n",
860 die->die_id, ret);
861 return ret;
862 }
863
864 *full_lane = *((u8 *)&desc.rsp.data);
865
866 return 0;
867 }
868
hccs_get_die_total_crc_err_cnt(struct hccs_dev * hdev,const struct hccs_die_info * die,u64 * total_crc_err_cnt)869 static int hccs_get_die_total_crc_err_cnt(struct hccs_dev *hdev,
870 const struct hccs_die_info *die,
871 u64 *total_crc_err_cnt)
872 {
873 struct hccs_die_comm_req_param *req_param;
874 struct hccs_desc desc;
875 int ret;
876
877 if (die->port_num == 0) {
878 *total_crc_err_cnt = 0;
879 return 0;
880 }
881
882 hccs_init_req_desc(&desc);
883 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
884 req_param->chip_id = die->chip->chip_id;
885 req_param->die_id = die->die_id;
886 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_CRC_ERR_CNT, &desc);
887 if (ret) {
888 dev_err(hdev->dev, "get crc error count sum failed on die%u, ret = %d.\n",
889 die->die_id, ret);
890 return ret;
891 }
892
893 memcpy(total_crc_err_cnt, &desc.rsp.data, sizeof(u64));
894
895 return 0;
896 }
897
hccs_show(struct kobject * k,struct attribute * attr,char * buf)898 static ssize_t hccs_show(struct kobject *k, struct attribute *attr, char *buf)
899 {
900 struct kobj_attribute *kobj_attr;
901
902 kobj_attr = container_of(attr, struct kobj_attribute, attr);
903
904 return kobj_attr->show(k, kobj_attr, buf);
905 }
906
907 static const struct sysfs_ops hccs_comm_ops = {
908 .show = hccs_show,
909 };
910
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)911 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
912 char *buf)
913 {
914 const struct hccs_port_info *port = kobj_to_port_info(kobj);
915
916 return sysfs_emit(buf, "%s%u\n", HCCS_IP_PREFIX, port->port_type);
917 }
918 static struct kobj_attribute hccs_type_attr = __ATTR_RO(type);
919
lane_mode_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)920 static ssize_t lane_mode_show(struct kobject *kobj, struct kobj_attribute *attr,
921 char *buf)
922 {
923 const struct hccs_port_info *port = kobj_to_port_info(kobj);
924
925 return sysfs_emit(buf, "x%u\n", port->max_lane_num);
926 }
927 static struct kobj_attribute lane_mode_attr = __ATTR_RO(lane_mode);
928
enable_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)929 static ssize_t enable_show(struct kobject *kobj,
930 struct kobj_attribute *attr, char *buf)
931 {
932 const struct hccs_port_info *port = kobj_to_port_info(kobj);
933
934 return sysfs_emit(buf, "%u\n", port->enable);
935 }
936 static struct kobj_attribute port_enable_attr = __ATTR_RO(enable);
937
cur_lane_num_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)938 static ssize_t cur_lane_num_show(struct kobject *kobj,
939 struct kobj_attribute *attr, char *buf)
940 {
941 const struct hccs_port_info *port = kobj_to_port_info(kobj);
942 struct hccs_dev *hdev = port->die->chip->hdev;
943 struct hccs_link_status link_status = {0};
944 int ret;
945
946 mutex_lock(&hdev->lock);
947 ret = hccs_query_port_link_status(hdev, port, &link_status);
948 mutex_unlock(&hdev->lock);
949 if (ret)
950 return ret;
951
952 return sysfs_emit(buf, "%u\n", link_status.lane_num);
953 }
954 static struct kobj_attribute cur_lane_num_attr = __ATTR_RO(cur_lane_num);
955
link_fsm_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)956 static ssize_t link_fsm_show(struct kobject *kobj,
957 struct kobj_attribute *attr, char *buf)
958 {
959 const struct hccs_port_info *port = kobj_to_port_info(kobj);
960 struct hccs_dev *hdev = port->die->chip->hdev;
961 struct hccs_link_status link_status = {0};
962 const struct {
963 u8 link_fsm;
964 const char *str;
965 } link_fsm_map[] = {
966 {HCCS_PORT_RESET, "reset"},
967 {HCCS_PORT_SETUP, "setup"},
968 {HCCS_PORT_CONFIG, "config"},
969 {HCCS_PORT_READY, "link-up"},
970 };
971 const char *link_fsm_str = "unknown";
972 size_t i;
973 int ret;
974
975 mutex_lock(&hdev->lock);
976 ret = hccs_query_port_link_status(hdev, port, &link_status);
977 mutex_unlock(&hdev->lock);
978 if (ret)
979 return ret;
980
981 for (i = 0; i < ARRAY_SIZE(link_fsm_map); i++) {
982 if (link_fsm_map[i].link_fsm == link_status.link_fsm) {
983 link_fsm_str = link_fsm_map[i].str;
984 break;
985 }
986 }
987
988 return sysfs_emit(buf, "%s\n", link_fsm_str);
989 }
990 static struct kobj_attribute link_fsm_attr = __ATTR_RO(link_fsm);
991
lane_mask_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)992 static ssize_t lane_mask_show(struct kobject *kobj,
993 struct kobj_attribute *attr, char *buf)
994 {
995 const struct hccs_port_info *port = kobj_to_port_info(kobj);
996 struct hccs_dev *hdev = port->die->chip->hdev;
997 struct hccs_link_status link_status = {0};
998 int ret;
999
1000 mutex_lock(&hdev->lock);
1001 ret = hccs_query_port_link_status(hdev, port, &link_status);
1002 mutex_unlock(&hdev->lock);
1003 if (ret)
1004 return ret;
1005
1006 return sysfs_emit(buf, "0x%x\n", link_status.lane_mask);
1007 }
1008 static struct kobj_attribute lane_mask_attr = __ATTR_RO(lane_mask);
1009
crc_err_cnt_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1010 static ssize_t crc_err_cnt_show(struct kobject *kobj,
1011 struct kobj_attribute *attr, char *buf)
1012 {
1013 const struct hccs_port_info *port = kobj_to_port_info(kobj);
1014 struct hccs_dev *hdev = port->die->chip->hdev;
1015 u64 crc_err_cnt;
1016 int ret;
1017
1018 mutex_lock(&hdev->lock);
1019 ret = hccs_query_port_crc_err_cnt(hdev, port, &crc_err_cnt);
1020 mutex_unlock(&hdev->lock);
1021 if (ret)
1022 return ret;
1023
1024 return sysfs_emit(buf, "%llu\n", crc_err_cnt);
1025 }
1026 static struct kobj_attribute crc_err_cnt_attr = __ATTR_RO(crc_err_cnt);
1027
1028 static struct attribute *hccs_port_default_attrs[] = {
1029 &hccs_type_attr.attr,
1030 &lane_mode_attr.attr,
1031 &port_enable_attr.attr,
1032 &cur_lane_num_attr.attr,
1033 &link_fsm_attr.attr,
1034 &lane_mask_attr.attr,
1035 &crc_err_cnt_attr.attr,
1036 NULL,
1037 };
1038 ATTRIBUTE_GROUPS(hccs_port_default);
1039
1040 static const struct kobj_type hccs_port_type = {
1041 .sysfs_ops = &hccs_comm_ops,
1042 .default_groups = hccs_port_default_groups,
1043 };
1044
all_linked_on_die_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1045 static ssize_t all_linked_on_die_show(struct kobject *kobj,
1046 struct kobj_attribute *attr, char *buf)
1047 {
1048 const struct hccs_die_info *die = kobj_to_die_info(kobj);
1049 struct hccs_dev *hdev = die->chip->hdev;
1050 u8 all_linked;
1051 int ret;
1052
1053 mutex_lock(&hdev->lock);
1054 ret = hccs_get_die_all_link_status(hdev, die, &all_linked);
1055 mutex_unlock(&hdev->lock);
1056 if (ret)
1057 return ret;
1058
1059 return sysfs_emit(buf, "%u\n", all_linked);
1060 }
1061 static struct kobj_attribute all_linked_on_die_attr =
1062 __ATTR(all_linked, 0444, all_linked_on_die_show, NULL);
1063
linked_full_lane_on_die_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1064 static ssize_t linked_full_lane_on_die_show(struct kobject *kobj,
1065 struct kobj_attribute *attr,
1066 char *buf)
1067 {
1068 const struct hccs_die_info *die = kobj_to_die_info(kobj);
1069 struct hccs_dev *hdev = die->chip->hdev;
1070 u8 full_lane;
1071 int ret;
1072
1073 mutex_lock(&hdev->lock);
1074 ret = hccs_get_die_all_port_lane_status(hdev, die, &full_lane);
1075 mutex_unlock(&hdev->lock);
1076 if (ret)
1077 return ret;
1078
1079 return sysfs_emit(buf, "%u\n", full_lane);
1080 }
1081 static struct kobj_attribute linked_full_lane_on_die_attr =
1082 __ATTR(linked_full_lane, 0444, linked_full_lane_on_die_show, NULL);
1083
crc_err_cnt_sum_on_die_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1084 static ssize_t crc_err_cnt_sum_on_die_show(struct kobject *kobj,
1085 struct kobj_attribute *attr,
1086 char *buf)
1087 {
1088 const struct hccs_die_info *die = kobj_to_die_info(kobj);
1089 struct hccs_dev *hdev = die->chip->hdev;
1090 u64 total_crc_err_cnt;
1091 int ret;
1092
1093 mutex_lock(&hdev->lock);
1094 ret = hccs_get_die_total_crc_err_cnt(hdev, die, &total_crc_err_cnt);
1095 mutex_unlock(&hdev->lock);
1096 if (ret)
1097 return ret;
1098
1099 return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
1100 }
1101 static struct kobj_attribute crc_err_cnt_sum_on_die_attr =
1102 __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_die_show, NULL);
1103
1104 static struct attribute *hccs_die_default_attrs[] = {
1105 &all_linked_on_die_attr.attr,
1106 &linked_full_lane_on_die_attr.attr,
1107 &crc_err_cnt_sum_on_die_attr.attr,
1108 NULL,
1109 };
1110 ATTRIBUTE_GROUPS(hccs_die_default);
1111
1112 static const struct kobj_type hccs_die_type = {
1113 .sysfs_ops = &hccs_comm_ops,
1114 .default_groups = hccs_die_default_groups,
1115 };
1116
all_linked_on_chip_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1117 static ssize_t all_linked_on_chip_show(struct kobject *kobj,
1118 struct kobj_attribute *attr, char *buf)
1119 {
1120 const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
1121 struct hccs_dev *hdev = chip->hdev;
1122 const struct hccs_die_info *die;
1123 u8 all_linked = 1;
1124 u8 i, tmp;
1125 int ret;
1126
1127 mutex_lock(&hdev->lock);
1128 for (i = 0; i < chip->die_num; i++) {
1129 die = &chip->dies[i];
1130 ret = hccs_get_die_all_link_status(hdev, die, &tmp);
1131 if (ret) {
1132 mutex_unlock(&hdev->lock);
1133 return ret;
1134 }
1135 if (tmp != all_linked) {
1136 all_linked = 0;
1137 break;
1138 }
1139 }
1140 mutex_unlock(&hdev->lock);
1141
1142 return sysfs_emit(buf, "%u\n", all_linked);
1143 }
1144 static struct kobj_attribute all_linked_on_chip_attr =
1145 __ATTR(all_linked, 0444, all_linked_on_chip_show, NULL);
1146
linked_full_lane_on_chip_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1147 static ssize_t linked_full_lane_on_chip_show(struct kobject *kobj,
1148 struct kobj_attribute *attr,
1149 char *buf)
1150 {
1151 const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
1152 struct hccs_dev *hdev = chip->hdev;
1153 const struct hccs_die_info *die;
1154 u8 full_lane = 1;
1155 u8 i, tmp;
1156 int ret;
1157
1158 mutex_lock(&hdev->lock);
1159 for (i = 0; i < chip->die_num; i++) {
1160 die = &chip->dies[i];
1161 ret = hccs_get_die_all_port_lane_status(hdev, die, &tmp);
1162 if (ret) {
1163 mutex_unlock(&hdev->lock);
1164 return ret;
1165 }
1166 if (tmp != full_lane) {
1167 full_lane = 0;
1168 break;
1169 }
1170 }
1171 mutex_unlock(&hdev->lock);
1172
1173 return sysfs_emit(buf, "%u\n", full_lane);
1174 }
1175 static struct kobj_attribute linked_full_lane_on_chip_attr =
1176 __ATTR(linked_full_lane, 0444, linked_full_lane_on_chip_show, NULL);
1177
crc_err_cnt_sum_on_chip_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1178 static ssize_t crc_err_cnt_sum_on_chip_show(struct kobject *kobj,
1179 struct kobj_attribute *attr,
1180 char *buf)
1181 {
1182 const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
1183 u64 crc_err_cnt, total_crc_err_cnt = 0;
1184 struct hccs_dev *hdev = chip->hdev;
1185 const struct hccs_die_info *die;
1186 int ret;
1187 u16 i;
1188
1189 mutex_lock(&hdev->lock);
1190 for (i = 0; i < chip->die_num; i++) {
1191 die = &chip->dies[i];
1192 ret = hccs_get_die_total_crc_err_cnt(hdev, die, &crc_err_cnt);
1193 if (ret) {
1194 mutex_unlock(&hdev->lock);
1195 return ret;
1196 }
1197
1198 total_crc_err_cnt += crc_err_cnt;
1199 }
1200 mutex_unlock(&hdev->lock);
1201
1202 return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
1203 }
1204 static struct kobj_attribute crc_err_cnt_sum_on_chip_attr =
1205 __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_chip_show, NULL);
1206
1207 static struct attribute *hccs_chip_default_attrs[] = {
1208 &all_linked_on_chip_attr.attr,
1209 &linked_full_lane_on_chip_attr.attr,
1210 &crc_err_cnt_sum_on_chip_attr.attr,
1211 NULL,
1212 };
1213 ATTRIBUTE_GROUPS(hccs_chip_default);
1214
1215 static const struct kobj_type hccs_chip_type = {
1216 .sysfs_ops = &hccs_comm_ops,
1217 .default_groups = hccs_chip_default_groups,
1218 };
1219
hccs_parse_pm_port_type(struct hccs_dev * hdev,const char * buf,u8 * port_type)1220 static int hccs_parse_pm_port_type(struct hccs_dev *hdev, const char *buf,
1221 u8 *port_type)
1222 {
1223 char hccs_name[HCCS_NAME_MAX_LEN + 1] = "";
1224 u8 type;
1225 int ret;
1226
1227 ret = sscanf(buf, "%" __stringify(HCCS_NAME_MAX_LEN) "s", hccs_name);
1228 if (ret != 1)
1229 return -EINVAL;
1230
1231 ret = hccs_name_to_port_type(hdev, hccs_name, &type);
1232 if (ret) {
1233 dev_dbg(hdev->dev, "input invalid, please get the available types from 'used_types'.\n");
1234 return ret;
1235 }
1236
1237 if (type == HCCS_V2 && hdev->caps & HCCS_CAPS_HCCS_V2_PM) {
1238 *port_type = type;
1239 return 0;
1240 }
1241
1242 dev_dbg(hdev->dev, "%s doesn't support for increasing and decreasing lane.\n",
1243 hccs_name);
1244
1245 return -EOPNOTSUPP;
1246 }
1247
hccs_query_port_idle_status(struct hccs_dev * hdev,struct hccs_port_info * port,u8 * idle)1248 static int hccs_query_port_idle_status(struct hccs_dev *hdev,
1249 struct hccs_port_info *port, u8 *idle)
1250 {
1251 const struct hccs_die_info *die = port->die;
1252 const struct hccs_chip_info *chip = die->chip;
1253 struct hccs_port_comm_req_param *req_param;
1254 struct hccs_desc desc;
1255 int ret;
1256
1257 hccs_init_req_desc(&desc);
1258 req_param = (struct hccs_port_comm_req_param *)desc.req.data;
1259 req_param->chip_id = chip->chip_id;
1260 req_param->die_id = die->die_id;
1261 req_param->port_id = port->port_id;
1262 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_IDLE_STATUS, &desc);
1263 if (ret) {
1264 dev_err(hdev->dev,
1265 "get port idle status failed, ret = %d.\n", ret);
1266 return ret;
1267 }
1268
1269 *idle = *((u8 *)desc.rsp.data);
1270 return 0;
1271 }
1272
hccs_get_all_spec_port_idle_sta(struct hccs_dev * hdev,u8 port_type,bool * all_idle)1273 static int hccs_get_all_spec_port_idle_sta(struct hccs_dev *hdev, u8 port_type,
1274 bool *all_idle)
1275 {
1276 struct hccs_chip_info *chip;
1277 struct hccs_port_info *port;
1278 struct hccs_die_info *die;
1279 int ret = 0;
1280 u8 i, j, k;
1281 u8 idle;
1282
1283 *all_idle = false;
1284 for (i = 0; i < hdev->chip_num; i++) {
1285 chip = &hdev->chips[i];
1286 for (j = 0; j < chip->die_num; j++) {
1287 die = &chip->dies[j];
1288 for (k = 0; k < die->port_num; k++) {
1289 port = &die->ports[k];
1290 if (port->port_type != port_type)
1291 continue;
1292 ret = hccs_query_port_idle_status(hdev, port,
1293 &idle);
1294 if (ret) {
1295 dev_err(hdev->dev,
1296 "hccs%u on chip%u/die%u get idle status failed, ret = %d.\n",
1297 port->port_id, chip->chip_id, die->die_id, ret);
1298 return ret;
1299 } else if (idle == 0) {
1300 dev_info(hdev->dev, "hccs%u on chip%u/die%u is busy.\n",
1301 port->port_id, chip->chip_id, die->die_id);
1302 return 0;
1303 }
1304 }
1305 }
1306 }
1307 *all_idle = true;
1308
1309 return 0;
1310 }
1311
hccs_get_all_spec_port_full_lane_sta(struct hccs_dev * hdev,u8 port_type,bool * full_lane)1312 static int hccs_get_all_spec_port_full_lane_sta(struct hccs_dev *hdev,
1313 u8 port_type, bool *full_lane)
1314 {
1315 struct hccs_link_status status = {0};
1316 struct hccs_chip_info *chip;
1317 struct hccs_port_info *port;
1318 struct hccs_die_info *die;
1319 u8 i, j, k;
1320 int ret;
1321
1322 *full_lane = false;
1323 for (i = 0; i < hdev->chip_num; i++) {
1324 chip = &hdev->chips[i];
1325 for (j = 0; j < chip->die_num; j++) {
1326 die = &chip->dies[j];
1327 for (k = 0; k < die->port_num; k++) {
1328 port = &die->ports[k];
1329 if (port->port_type != port_type)
1330 continue;
1331 ret = hccs_query_port_link_status(hdev, port,
1332 &status);
1333 if (ret)
1334 return ret;
1335 if (status.lane_num != port->max_lane_num)
1336 return 0;
1337 }
1338 }
1339 }
1340 *full_lane = true;
1341
1342 return 0;
1343 }
1344
hccs_prepare_inc_lane(struct hccs_dev * hdev,u8 type)1345 static int hccs_prepare_inc_lane(struct hccs_dev *hdev, u8 type)
1346 {
1347 struct hccs_inc_lane_req_param *req_param;
1348 struct hccs_desc desc;
1349 int ret;
1350
1351 hccs_init_req_desc(&desc);
1352 req_param = (struct hccs_inc_lane_req_param *)desc.req.data;
1353 req_param->port_type = type;
1354 req_param->opt_type = HCCS_PREPARE_INC_LANE;
1355 ret = hccs_pcc_cmd_send(hdev, HCCS_PM_INC_LANE, &desc);
1356 if (ret)
1357 dev_err(hdev->dev, "prepare for increasing lane failed, ret = %d.\n",
1358 ret);
1359
1360 return ret;
1361 }
1362
hccs_wait_serdes_adapt_completed(struct hccs_dev * hdev,u8 type)1363 static int hccs_wait_serdes_adapt_completed(struct hccs_dev *hdev, u8 type)
1364 {
1365 #define HCCS_MAX_WAIT_CNT_FOR_ADAPT 10
1366 #define HCCS_QUERY_ADAPT_RES_DELAY_MS 100
1367 #define HCCS_SERDES_ADAPT_OK 0
1368
1369 struct hccs_inc_lane_req_param *req_param;
1370 u8 wait_cnt = HCCS_MAX_WAIT_CNT_FOR_ADAPT;
1371 struct hccs_desc desc;
1372 u8 adapt_res;
1373 int ret;
1374
1375 do {
1376 hccs_init_req_desc(&desc);
1377 req_param = (struct hccs_inc_lane_req_param *)desc.req.data;
1378 req_param->port_type = type;
1379 req_param->opt_type = HCCS_GET_ADAPT_RES;
1380 ret = hccs_pcc_cmd_send(hdev, HCCS_PM_INC_LANE, &desc);
1381 if (ret) {
1382 dev_err(hdev->dev, "query adapting result failed, ret = %d.\n",
1383 ret);
1384 return ret;
1385 }
1386 adapt_res = *((u8 *)&desc.rsp.data);
1387 if (adapt_res == HCCS_SERDES_ADAPT_OK)
1388 return 0;
1389
1390 msleep(HCCS_QUERY_ADAPT_RES_DELAY_MS);
1391 } while (--wait_cnt);
1392
1393 dev_err(hdev->dev, "wait for adapting completed timeout.\n");
1394
1395 return -ETIMEDOUT;
1396 }
1397
hccs_start_hpcs_retraining(struct hccs_dev * hdev,u8 type)1398 static int hccs_start_hpcs_retraining(struct hccs_dev *hdev, u8 type)
1399 {
1400 struct hccs_inc_lane_req_param *req_param;
1401 struct hccs_desc desc;
1402 int ret;
1403
1404 hccs_init_req_desc(&desc);
1405 req_param = (struct hccs_inc_lane_req_param *)desc.req.data;
1406 req_param->port_type = type;
1407 req_param->opt_type = HCCS_START_RETRAINING;
1408 ret = hccs_pcc_cmd_send(hdev, HCCS_PM_INC_LANE, &desc);
1409 if (ret)
1410 dev_err(hdev->dev, "start hpcs retraining failed, ret = %d.\n",
1411 ret);
1412
1413 return ret;
1414 }
1415
hccs_start_inc_lane(struct hccs_dev * hdev,u8 type)1416 static int hccs_start_inc_lane(struct hccs_dev *hdev, u8 type)
1417 {
1418 int ret;
1419
1420 ret = hccs_prepare_inc_lane(hdev, type);
1421 if (ret)
1422 return ret;
1423
1424 ret = hccs_wait_serdes_adapt_completed(hdev, type);
1425 if (ret)
1426 return ret;
1427
1428 return hccs_start_hpcs_retraining(hdev, type);
1429 }
1430
hccs_start_dec_lane(struct hccs_dev * hdev,u8 type)1431 static int hccs_start_dec_lane(struct hccs_dev *hdev, u8 type)
1432 {
1433 struct hccs_desc desc;
1434 u8 *port_type;
1435 int ret;
1436
1437 hccs_init_req_desc(&desc);
1438 port_type = (u8 *)desc.req.data;
1439 *port_type = type;
1440 ret = hccs_pcc_cmd_send(hdev, HCCS_PM_DEC_LANE, &desc);
1441 if (ret)
1442 dev_err(hdev->dev, "start to decrease lane failed, ret = %d.\n",
1443 ret);
1444
1445 return ret;
1446 }
1447
dec_lane_of_type_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)1448 static ssize_t dec_lane_of_type_store(struct kobject *kobj, struct kobj_attribute *attr,
1449 const char *buf, size_t count)
1450 {
1451 struct hccs_dev *hdev = device_kobj_to_hccs_dev(kobj);
1452 bool all_in_idle;
1453 u8 port_type;
1454 int ret;
1455
1456 ret = hccs_parse_pm_port_type(hdev, buf, &port_type);
1457 if (ret)
1458 return ret;
1459
1460 mutex_lock(&hdev->lock);
1461 ret = hccs_get_all_spec_port_idle_sta(hdev, port_type, &all_in_idle);
1462 if (ret)
1463 goto out;
1464 if (!all_in_idle) {
1465 ret = -EBUSY;
1466 dev_err(hdev->dev, "please don't decrease lanes on high load with %s, ret = %d.\n",
1467 hccs_port_type_to_name(hdev, port_type), ret);
1468 goto out;
1469 }
1470
1471 ret = hccs_start_dec_lane(hdev, port_type);
1472 out:
1473 mutex_unlock(&hdev->lock);
1474
1475 return ret == 0 ? count : ret;
1476 }
1477 static struct kobj_attribute dec_lane_of_type_attr =
1478 __ATTR(dec_lane_of_type, 0200, NULL, dec_lane_of_type_store);
1479
inc_lane_of_type_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)1480 static ssize_t inc_lane_of_type_store(struct kobject *kobj, struct kobj_attribute *attr,
1481 const char *buf, size_t count)
1482 {
1483 struct hccs_dev *hdev = device_kobj_to_hccs_dev(kobj);
1484 bool full_lane;
1485 u8 port_type;
1486 int ret;
1487
1488 ret = hccs_parse_pm_port_type(hdev, buf, &port_type);
1489 if (ret)
1490 return ret;
1491
1492 mutex_lock(&hdev->lock);
1493 ret = hccs_get_all_spec_port_full_lane_sta(hdev, port_type, &full_lane);
1494 if (ret || full_lane)
1495 goto out;
1496
1497 ret = hccs_start_inc_lane(hdev, port_type);
1498 out:
1499 mutex_unlock(&hdev->lock);
1500 return ret == 0 ? count : ret;
1501 }
1502 static struct kobj_attribute inc_lane_of_type_attr =
1503 __ATTR(inc_lane_of_type, 0200, NULL, inc_lane_of_type_store);
1504
available_inc_dec_lane_types_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1505 static ssize_t available_inc_dec_lane_types_show(struct kobject *kobj,
1506 struct kobj_attribute *attr,
1507 char *buf)
1508 {
1509 struct hccs_dev *hdev = device_kobj_to_hccs_dev(kobj);
1510
1511 if (hdev->caps & HCCS_CAPS_HCCS_V2_PM)
1512 return sysfs_emit(buf, "%s\n",
1513 hccs_port_type_to_name(hdev, HCCS_V2));
1514
1515 return -EINVAL;
1516 }
1517 static struct kobj_attribute available_inc_dec_lane_types_attr =
1518 __ATTR(available_inc_dec_lane_types, 0444,
1519 available_inc_dec_lane_types_show, NULL);
1520
used_types_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)1521 static ssize_t used_types_show(struct kobject *kobj,
1522 struct kobj_attribute *attr, char *buf)
1523 {
1524 struct hccs_dev *hdev = device_kobj_to_hccs_dev(kobj);
1525 int len = 0;
1526 u16 i;
1527
1528 for (i = 0; i < hdev->used_type_num - 1; i++)
1529 len += sysfs_emit_at(buf, len, "%s ", hdev->type_name_maps[i].name);
1530 len += sysfs_emit_at(buf, len, "%s\n", hdev->type_name_maps[i].name);
1531
1532 return len;
1533 }
1534 static struct kobj_attribute used_types_attr =
1535 __ATTR(used_types, 0444, used_types_show, NULL);
1536
hccs_remove_misc_sysfs(struct hccs_dev * hdev)1537 static void hccs_remove_misc_sysfs(struct hccs_dev *hdev)
1538 {
1539 sysfs_remove_file(&hdev->dev->kobj, &used_types_attr.attr);
1540
1541 if (!(hdev->caps & HCCS_CAPS_HCCS_V2_PM))
1542 return;
1543
1544 sysfs_remove_file(&hdev->dev->kobj,
1545 &available_inc_dec_lane_types_attr.attr);
1546 sysfs_remove_file(&hdev->dev->kobj, &dec_lane_of_type_attr.attr);
1547 sysfs_remove_file(&hdev->dev->kobj, &inc_lane_of_type_attr.attr);
1548 }
1549
hccs_add_misc_sysfs(struct hccs_dev * hdev)1550 static int hccs_add_misc_sysfs(struct hccs_dev *hdev)
1551 {
1552 int ret;
1553
1554 ret = sysfs_create_file(&hdev->dev->kobj, &used_types_attr.attr);
1555 if (ret)
1556 return ret;
1557
1558 if (!(hdev->caps & HCCS_CAPS_HCCS_V2_PM))
1559 return 0;
1560
1561 ret = sysfs_create_file(&hdev->dev->kobj,
1562 &available_inc_dec_lane_types_attr.attr);
1563 if (ret)
1564 goto used_types_remove;
1565
1566 ret = sysfs_create_file(&hdev->dev->kobj, &dec_lane_of_type_attr.attr);
1567 if (ret)
1568 goto inc_dec_lane_types_remove;
1569
1570 ret = sysfs_create_file(&hdev->dev->kobj, &inc_lane_of_type_attr.attr);
1571 if (ret)
1572 goto dec_lane_of_type_remove;
1573
1574 return 0;
1575
1576 dec_lane_of_type_remove:
1577 sysfs_remove_file(&hdev->dev->kobj, &dec_lane_of_type_attr.attr);
1578 inc_dec_lane_types_remove:
1579 sysfs_remove_file(&hdev->dev->kobj,
1580 &available_inc_dec_lane_types_attr.attr);
1581 used_types_remove:
1582 sysfs_remove_file(&hdev->dev->kobj, &used_types_attr.attr);
1583 return ret;
1584 }
1585
hccs_remove_die_dir(struct hccs_die_info * die)1586 static void hccs_remove_die_dir(struct hccs_die_info *die)
1587 {
1588 struct hccs_port_info *port;
1589 u8 i;
1590
1591 for (i = 0; i < die->port_num; i++) {
1592 port = &die->ports[i];
1593 if (port->dir_created)
1594 kobject_put(&port->kobj);
1595 }
1596
1597 kobject_put(&die->kobj);
1598 }
1599
hccs_remove_chip_dir(struct hccs_chip_info * chip)1600 static void hccs_remove_chip_dir(struct hccs_chip_info *chip)
1601 {
1602 struct hccs_die_info *die;
1603 u8 i;
1604
1605 for (i = 0; i < chip->die_num; i++) {
1606 die = &chip->dies[i];
1607 if (die->dir_created)
1608 hccs_remove_die_dir(die);
1609 }
1610
1611 kobject_put(&chip->kobj);
1612 }
1613
hccs_remove_topo_dirs(struct hccs_dev * hdev)1614 static void hccs_remove_topo_dirs(struct hccs_dev *hdev)
1615 {
1616 u8 i;
1617
1618 for (i = 0; i < hdev->chip_num; i++)
1619 hccs_remove_chip_dir(&hdev->chips[i]);
1620
1621 hccs_remove_misc_sysfs(hdev);
1622 }
1623
hccs_create_hccs_dir(struct hccs_die_info * die,struct hccs_port_info * port)1624 static int hccs_create_hccs_dir(struct hccs_die_info *die,
1625 struct hccs_port_info *port)
1626 {
1627 int ret;
1628
1629 ret = kobject_init_and_add(&port->kobj, &hccs_port_type,
1630 &die->kobj, "hccs%u", port->port_id);
1631 if (ret) {
1632 kobject_put(&port->kobj);
1633 return ret;
1634 }
1635
1636 return 0;
1637 }
1638
hccs_create_die_dir(struct hccs_dev * hdev,struct hccs_chip_info * chip,struct hccs_die_info * die)1639 static int hccs_create_die_dir(struct hccs_dev *hdev,
1640 struct hccs_chip_info *chip,
1641 struct hccs_die_info *die)
1642 {
1643 struct hccs_port_info *port;
1644 int ret;
1645 u16 i;
1646
1647 ret = kobject_init_and_add(&die->kobj, &hccs_die_type,
1648 &chip->kobj, "die%u", die->die_id);
1649 if (ret) {
1650 kobject_put(&die->kobj);
1651 return ret;
1652 }
1653
1654 for (i = 0; i < die->port_num; i++) {
1655 port = &die->ports[i];
1656 ret = hccs_create_hccs_dir(die, port);
1657 if (ret) {
1658 dev_err(hdev->dev, "create hccs%u dir failed.\n",
1659 port->port_id);
1660 goto err;
1661 }
1662 port->dir_created = true;
1663 }
1664
1665 return 0;
1666 err:
1667 hccs_remove_die_dir(die);
1668
1669 return ret;
1670 }
1671
hccs_create_chip_dir(struct hccs_dev * hdev,struct hccs_chip_info * chip)1672 static int hccs_create_chip_dir(struct hccs_dev *hdev,
1673 struct hccs_chip_info *chip)
1674 {
1675 struct hccs_die_info *die;
1676 int ret;
1677 u16 id;
1678
1679 ret = kobject_init_and_add(&chip->kobj, &hccs_chip_type,
1680 &hdev->dev->kobj, "chip%u", chip->chip_id);
1681 if (ret) {
1682 kobject_put(&chip->kobj);
1683 return ret;
1684 }
1685
1686 for (id = 0; id < chip->die_num; id++) {
1687 die = &chip->dies[id];
1688 ret = hccs_create_die_dir(hdev, chip, die);
1689 if (ret)
1690 goto err;
1691 die->dir_created = true;
1692 }
1693
1694 return 0;
1695 err:
1696 hccs_remove_chip_dir(chip);
1697
1698 return ret;
1699 }
1700
hccs_create_topo_dirs(struct hccs_dev * hdev)1701 static int hccs_create_topo_dirs(struct hccs_dev *hdev)
1702 {
1703 struct hccs_chip_info *chip;
1704 u8 id, k;
1705 int ret;
1706
1707 for (id = 0; id < hdev->chip_num; id++) {
1708 chip = &hdev->chips[id];
1709 ret = hccs_create_chip_dir(hdev, chip);
1710 if (ret) {
1711 dev_err(hdev->dev, "init chip%u dir failed!\n", id);
1712 goto err;
1713 }
1714 }
1715
1716 ret = hccs_add_misc_sysfs(hdev);
1717 if (ret) {
1718 dev_err(hdev->dev, "create misc sysfs interface failed, ret = %d\n", ret);
1719 goto err;
1720 }
1721
1722 return 0;
1723 err:
1724 for (k = 0; k < id; k++)
1725 hccs_remove_chip_dir(&hdev->chips[k]);
1726
1727 return ret;
1728 }
1729
hccs_probe(struct platform_device * pdev)1730 static int hccs_probe(struct platform_device *pdev)
1731 {
1732 struct acpi_device *acpi_dev;
1733 struct hccs_dev *hdev;
1734 int rc;
1735
1736 if (acpi_disabled) {
1737 dev_err(&pdev->dev, "acpi is disabled.\n");
1738 return -ENODEV;
1739 }
1740 acpi_dev = ACPI_COMPANION(&pdev->dev);
1741 if (!acpi_dev)
1742 return -ENODEV;
1743
1744 hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
1745 if (!hdev)
1746 return -ENOMEM;
1747 hdev->acpi_dev = acpi_dev;
1748 hdev->dev = &pdev->dev;
1749 platform_set_drvdata(pdev, hdev);
1750
1751 /*
1752 * Here would never be failure as the driver and device has been matched.
1753 */
1754 hdev->verspec_data = acpi_device_get_match_data(hdev->dev);
1755
1756 mutex_init(&hdev->lock);
1757 rc = hccs_get_pcc_chan_id(hdev);
1758 if (rc)
1759 return rc;
1760 rc = hccs_register_pcc_channel(hdev);
1761 if (rc)
1762 return rc;
1763
1764 rc = hccs_get_dev_caps(hdev);
1765 if (rc)
1766 goto unregister_pcc_chan;
1767
1768 rc = hccs_get_hw_info(hdev);
1769 if (rc)
1770 goto unregister_pcc_chan;
1771
1772 rc = hccs_init_type_name_maps(hdev);
1773 if (rc)
1774 goto unregister_pcc_chan;
1775
1776 rc = hccs_create_topo_dirs(hdev);
1777 if (rc)
1778 goto unregister_pcc_chan;
1779
1780 return 0;
1781
1782 unregister_pcc_chan:
1783 hccs_unregister_pcc_channel(hdev);
1784
1785 return rc;
1786 }
1787
hccs_remove(struct platform_device * pdev)1788 static void hccs_remove(struct platform_device *pdev)
1789 {
1790 struct hccs_dev *hdev = platform_get_drvdata(pdev);
1791
1792 hccs_remove_topo_dirs(hdev);
1793 hccs_unregister_pcc_channel(hdev);
1794 }
1795
1796 static const struct hccs_verspecific_data hisi04b1_verspec_data = {
1797 .rx_callback = NULL,
1798 .wait_cmd_complete = hccs_wait_cmd_complete_by_poll,
1799 .fill_pcc_shared_mem = hccs_fill_pcc_shared_mem_region,
1800 .shared_mem_size = sizeof(struct acpi_pcct_shared_memory),
1801 .has_txdone_irq = false,
1802 };
1803
1804 static const struct hccs_verspecific_data hisi04b2_verspec_data = {
1805 .rx_callback = hccs_pcc_rx_callback,
1806 .wait_cmd_complete = hccs_wait_cmd_complete_by_irq,
1807 .fill_pcc_shared_mem = hccs_fill_ext_pcc_shared_mem_region,
1808 .shared_mem_size = sizeof(struct acpi_pcct_ext_pcc_shared_memory),
1809 .has_txdone_irq = true,
1810 };
1811
1812 static const struct acpi_device_id hccs_acpi_match[] = {
1813 { "HISI04B1", (unsigned long)&hisi04b1_verspec_data},
1814 { "HISI04B2", (unsigned long)&hisi04b2_verspec_data},
1815 { }
1816 };
1817 MODULE_DEVICE_TABLE(acpi, hccs_acpi_match);
1818
1819 static struct platform_driver hccs_driver = {
1820 .probe = hccs_probe,
1821 .remove = hccs_remove,
1822 .driver = {
1823 .name = "kunpeng_hccs",
1824 .acpi_match_table = hccs_acpi_match,
1825 },
1826 };
1827
1828 module_platform_driver(hccs_driver);
1829
1830 MODULE_DESCRIPTION("Kunpeng SoC HCCS driver");
1831 MODULE_LICENSE("GPL");
1832 MODULE_AUTHOR("Huisong Li <lihuisong@huawei.com>");
1833