Merge tag 'kvm-x86-selftests-6.6-fixes' of https://github.com/kvm-x86/linux into...
[platform/kernel/linux-starfive.git] / drivers / soc / hisilicon / kunpeng_hccs.c
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 #include <linux/acpi.h>
26 #include <linux/iopoll.h>
27 #include <linux/platform_device.h>
28 #include <linux/sysfs.h>
29
30 #include <acpi/pcc.h>
31
32 #include "kunpeng_hccs.h"
33
34 /* PCC defines */
35 #define HCCS_PCC_SIGNATURE_MASK         0x50434300
36 #define HCCS_PCC_STATUS_CMD_COMPLETE    BIT(0)
37
38 /*
39  * Arbitrary retries in case the remote processor is slow to respond
40  * to PCC commands
41  */
42 #define HCCS_PCC_CMD_WAIT_RETRIES_NUM           500ULL
43 #define HCCS_POLL_STATUS_TIME_INTERVAL_US       3
44
45 static struct hccs_port_info *kobj_to_port_info(struct kobject *k)
46 {
47         return container_of(k, struct hccs_port_info, kobj);
48 }
49
50 static struct hccs_die_info *kobj_to_die_info(struct kobject *k)
51 {
52         return container_of(k, struct hccs_die_info, kobj);
53 }
54
55 static struct hccs_chip_info *kobj_to_chip_info(struct kobject *k)
56 {
57         return container_of(k, struct hccs_chip_info, kobj);
58 }
59
60 struct hccs_register_ctx {
61         struct device *dev;
62         u8 chan_id;
63         int err;
64 };
65
66 static acpi_status hccs_get_register_cb(struct acpi_resource *ares,
67                                         void *context)
68 {
69         struct acpi_resource_generic_register *reg;
70         struct hccs_register_ctx *ctx = context;
71
72         if (ares->type != ACPI_RESOURCE_TYPE_GENERIC_REGISTER)
73                 return AE_OK;
74
75         reg = &ares->data.generic_reg;
76         if (reg->space_id != ACPI_ADR_SPACE_PLATFORM_COMM) {
77                 dev_err(ctx->dev, "Bad register resource.\n");
78                 ctx->err = -EINVAL;
79                 return AE_ERROR;
80         }
81         ctx->chan_id = reg->access_size;
82
83         return AE_OK;
84 }
85
86 static int hccs_get_pcc_chan_id(struct hccs_dev *hdev)
87 {
88         acpi_handle handle = ACPI_HANDLE(hdev->dev);
89         struct hccs_register_ctx ctx = {0};
90         acpi_status status;
91
92         if (!acpi_has_method(handle, METHOD_NAME__CRS))
93                 return -ENODEV;
94
95         ctx.dev = hdev->dev;
96         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
97                                      hccs_get_register_cb, &ctx);
98         if (ACPI_FAILURE(status))
99                 return ctx.err;
100         hdev->chan_id = ctx.chan_id;
101
102         return 0;
103 }
104
105 static void hccs_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
106 {
107         if (ret < 0)
108                 pr_debug("TX did not complete: CMD sent:0x%x, ret:%d\n",
109                          *(u8 *)msg, ret);
110         else
111                 pr_debug("TX completed. CMD sent:0x%x, ret:%d\n",
112                          *(u8 *)msg, ret);
113 }
114
115 static void hccs_unregister_pcc_channel(struct hccs_dev *hdev)
116 {
117         struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
118
119         if (cl_info->pcc_comm_addr)
120                 iounmap(cl_info->pcc_comm_addr);
121         pcc_mbox_free_channel(hdev->cl_info.pcc_chan);
122 }
123
124 static int hccs_register_pcc_channel(struct hccs_dev *hdev)
125 {
126         struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
127         struct mbox_client *cl = &cl_info->client;
128         struct pcc_mbox_chan *pcc_chan;
129         struct device *dev = hdev->dev;
130         int rc;
131
132         cl->dev = dev;
133         cl->tx_block = false;
134         cl->knows_txdone = true;
135         cl->tx_done = hccs_chan_tx_done;
136         pcc_chan = pcc_mbox_request_channel(cl, hdev->chan_id);
137         if (IS_ERR(pcc_chan)) {
138                 dev_err(dev, "PPC channel request failed.\n");
139                 rc = -ENODEV;
140                 goto out;
141         }
142         cl_info->pcc_chan = pcc_chan;
143         cl_info->mbox_chan = pcc_chan->mchan;
144
145         /*
146          * pcc_chan->latency is just a nominal value. In reality the remote
147          * processor could be much slower to reply. So add an arbitrary amount
148          * of wait on top of nominal.
149          */
150         cl_info->deadline_us =
151                         HCCS_PCC_CMD_WAIT_RETRIES_NUM * pcc_chan->latency;
152         if (cl_info->mbox_chan->mbox->txdone_irq) {
153                 dev_err(dev, "PCC IRQ in PCCT is enabled.\n");
154                 rc = -EINVAL;
155                 goto err_mbx_channel_free;
156         }
157
158         if (pcc_chan->shmem_base_addr) {
159                 cl_info->pcc_comm_addr = ioremap(pcc_chan->shmem_base_addr,
160                                                  pcc_chan->shmem_size);
161                 if (!cl_info->pcc_comm_addr) {
162                         dev_err(dev, "Failed to ioremap PCC communication region for channel-%d.\n",
163                                 hdev->chan_id);
164                         rc = -ENOMEM;
165                         goto err_mbx_channel_free;
166                 }
167         }
168
169         return 0;
170
171 err_mbx_channel_free:
172         pcc_mbox_free_channel(cl_info->pcc_chan);
173 out:
174         return rc;
175 }
176
177 static int hccs_check_chan_cmd_complete(struct hccs_dev *hdev)
178 {
179         struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
180         struct acpi_pcct_shared_memory __iomem *comm_base =
181                                                         cl_info->pcc_comm_addr;
182         u16 status;
183         int ret;
184
185         /*
186          * Poll PCC status register every 3us(delay_us) for maximum of
187          * deadline_us(timeout_us) until PCC command complete bit is set(cond)
188          */
189         ret = readw_poll_timeout(&comm_base->status, status,
190                                  status & HCCS_PCC_STATUS_CMD_COMPLETE,
191                                  HCCS_POLL_STATUS_TIME_INTERVAL_US,
192                                  cl_info->deadline_us);
193         if (unlikely(ret))
194                 dev_err(hdev->dev, "poll PCC status failed, ret = %d.\n", ret);
195
196         return ret;
197 }
198
199 static int hccs_pcc_cmd_send(struct hccs_dev *hdev, u8 cmd,
200                              struct hccs_desc *desc)
201 {
202         struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
203         void __iomem *comm_space = cl_info->pcc_comm_addr +
204                                         sizeof(struct acpi_pcct_shared_memory);
205         struct hccs_fw_inner_head *fw_inner_head;
206         struct acpi_pcct_shared_memory tmp = {0};
207         u16 comm_space_size;
208         int ret;
209
210         /* Write signature for this subspace */
211         tmp.signature = HCCS_PCC_SIGNATURE_MASK | hdev->chan_id;
212         /* Write to the shared command region */
213         tmp.command = cmd;
214         /* Clear cmd complete bit */
215         tmp.status = 0;
216         memcpy_toio(cl_info->pcc_comm_addr, (void *)&tmp,
217                         sizeof(struct acpi_pcct_shared_memory));
218
219         /* Copy the message to the PCC comm space */
220         comm_space_size = HCCS_PCC_SHARE_MEM_BYTES -
221                                 sizeof(struct acpi_pcct_shared_memory);
222         memcpy_toio(comm_space, (void *)desc, comm_space_size);
223
224         /* Ring doorbell */
225         ret = mbox_send_message(cl_info->mbox_chan, &cmd);
226         if (ret < 0) {
227                 dev_err(hdev->dev, "Send PCC mbox message failed, ret = %d.\n",
228                         ret);
229                 goto end;
230         }
231
232         /* Wait for completion */
233         ret = hccs_check_chan_cmd_complete(hdev);
234         if (ret)
235                 goto end;
236
237         /* Copy response data */
238         memcpy_fromio((void *)desc, comm_space, comm_space_size);
239         fw_inner_head = &desc->rsp.fw_inner_head;
240         if (fw_inner_head->retStatus) {
241                 dev_err(hdev->dev, "Execute PCC command failed, error code = %u.\n",
242                         fw_inner_head->retStatus);
243                 ret = -EIO;
244         }
245
246 end:
247         mbox_client_txdone(cl_info->mbox_chan, ret);
248         return ret;
249 }
250
251 static void hccs_init_req_desc(struct hccs_desc *desc)
252 {
253         struct hccs_req_desc *req = &desc->req;
254
255         memset(desc, 0, sizeof(*desc));
256         req->req_head.module_code = HCCS_SERDES_MODULE_CODE;
257 }
258
259 static int hccs_get_dev_caps(struct hccs_dev *hdev)
260 {
261         struct hccs_desc desc;
262         int ret;
263
264         hccs_init_req_desc(&desc);
265         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DEV_CAP, &desc);
266         if (ret) {
267                 dev_err(hdev->dev, "Get device capabilities failed, ret = %d.\n",
268                         ret);
269                 return ret;
270         }
271         memcpy(&hdev->caps, desc.rsp.data, sizeof(hdev->caps));
272
273         return 0;
274 }
275
276 static int hccs_query_chip_num_on_platform(struct hccs_dev *hdev)
277 {
278         struct hccs_desc desc;
279         int ret;
280
281         hccs_init_req_desc(&desc);
282         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_CHIP_NUM, &desc);
283         if (ret) {
284                 dev_err(hdev->dev, "query system chip number failed, ret = %d.\n",
285                         ret);
286                 return ret;
287         }
288
289         hdev->chip_num = *((u8 *)&desc.rsp.data);
290         if (!hdev->chip_num) {
291                 dev_err(hdev->dev, "chip num obtained from firmware is zero.\n");
292                 return -EINVAL;
293         }
294
295         return 0;
296 }
297
298 static int hccs_get_chip_info(struct hccs_dev *hdev,
299                               struct hccs_chip_info *chip)
300 {
301         struct hccs_die_num_req_param *req_param;
302         struct hccs_desc desc;
303         int ret;
304
305         hccs_init_req_desc(&desc);
306         req_param = (struct hccs_die_num_req_param *)desc.req.data;
307         req_param->chip_id = chip->chip_id;
308         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_NUM, &desc);
309         if (ret)
310                 return ret;
311
312         chip->die_num = *((u8 *)&desc.rsp.data);
313
314         return 0;
315 }
316
317 static int hccs_query_chip_info_on_platform(struct hccs_dev *hdev)
318 {
319         struct hccs_chip_info *chip;
320         int ret;
321         u8 idx;
322
323         ret = hccs_query_chip_num_on_platform(hdev);
324         if (ret) {
325                 dev_err(hdev->dev, "query chip number on platform failed, ret = %d.\n",
326                         ret);
327                 return ret;
328         }
329
330         hdev->chips = devm_kzalloc(hdev->dev,
331                                 hdev->chip_num * sizeof(struct hccs_chip_info),
332                                 GFP_KERNEL);
333         if (!hdev->chips) {
334                 dev_err(hdev->dev, "allocate all chips memory failed.\n");
335                 return -ENOMEM;
336         }
337
338         for (idx = 0; idx < hdev->chip_num; idx++) {
339                 chip = &hdev->chips[idx];
340                 chip->chip_id = idx;
341                 ret = hccs_get_chip_info(hdev, chip);
342                 if (ret) {
343                         dev_err(hdev->dev, "get chip%u info failed, ret = %d.\n",
344                                 idx, ret);
345                         return ret;
346                 }
347                 chip->hdev = hdev;
348         }
349
350         return 0;
351 }
352
353 static int hccs_query_die_info_on_chip(struct hccs_dev *hdev, u8 chip_id,
354                                        u8 die_idx, struct hccs_die_info *die)
355 {
356         struct hccs_die_info_req_param *req_param;
357         struct hccs_die_info_rsp_data *rsp_data;
358         struct hccs_desc desc;
359         int ret;
360
361         hccs_init_req_desc(&desc);
362         req_param = (struct hccs_die_info_req_param *)desc.req.data;
363         req_param->chip_id = chip_id;
364         req_param->die_idx = die_idx;
365         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_INFO, &desc);
366         if (ret)
367                 return ret;
368
369         rsp_data = (struct hccs_die_info_rsp_data *)desc.rsp.data;
370         die->die_id = rsp_data->die_id;
371         die->port_num = rsp_data->port_num;
372         die->min_port_id = rsp_data->min_port_id;
373         die->max_port_id = rsp_data->max_port_id;
374         if (die->min_port_id > die->max_port_id) {
375                 dev_err(hdev->dev, "min port id(%u) > max port id(%u) on die_idx(%u).\n",
376                         die->min_port_id, die->max_port_id, die_idx);
377                 return -EINVAL;
378         }
379         if (die->max_port_id > HCCS_DIE_MAX_PORT_ID) {
380                 dev_err(hdev->dev, "max port id(%u) on die_idx(%u) is too big.\n",
381                         die->max_port_id, die_idx);
382                 return -EINVAL;
383         }
384
385         return 0;
386 }
387
388 static int hccs_query_all_die_info_on_platform(struct hccs_dev *hdev)
389 {
390         struct device *dev = hdev->dev;
391         struct hccs_chip_info *chip;
392         struct hccs_die_info *die;
393         u8 i, j;
394         int ret;
395
396         for (i = 0; i < hdev->chip_num; i++) {
397                 chip = &hdev->chips[i];
398                 if (!chip->die_num)
399                         continue;
400
401                 chip->dies = devm_kzalloc(hdev->dev,
402                                 chip->die_num * sizeof(struct hccs_die_info),
403                                 GFP_KERNEL);
404                 if (!chip->dies) {
405                         dev_err(dev, "allocate all dies memory on chip%u failed.\n",
406                                 i);
407                         return -ENOMEM;
408                 }
409
410                 for (j = 0; j < chip->die_num; j++) {
411                         die = &chip->dies[j];
412                         ret = hccs_query_die_info_on_chip(hdev, i, j, die);
413                         if (ret) {
414                                 dev_err(dev, "get die idx (%u) info on chip%u failed, ret = %d.\n",
415                                         j, i, ret);
416                                 return ret;
417                         }
418                         die->chip = chip;
419                 }
420         }
421
422         return 0;
423 }
424
425 static int hccs_get_bd_info(struct hccs_dev *hdev, u8 opcode,
426                             struct hccs_desc *desc,
427                             void *buf, size_t buf_len,
428                             struct hccs_rsp_head *rsp_head)
429 {
430         struct hccs_rsp_head *head;
431         struct hccs_rsp_desc *rsp;
432         int ret;
433
434         ret = hccs_pcc_cmd_send(hdev, opcode, desc);
435         if (ret)
436                 return ret;
437
438         rsp = &desc->rsp;
439         head = &rsp->rsp_head;
440         if (head->data_len > buf_len) {
441                 dev_err(hdev->dev,
442                         "buffer overflow (buf_len = %zu, data_len = %u)!\n",
443                         buf_len, head->data_len);
444                 return -ENOMEM;
445         }
446
447         memcpy(buf, rsp->data, head->data_len);
448         *rsp_head = *head;
449
450         return 0;
451 }
452
453 static int hccs_get_all_port_attr(struct hccs_dev *hdev,
454                                   struct hccs_die_info *die,
455                                   struct hccs_port_attr *attrs, u16 size)
456 {
457         struct hccs_die_comm_req_param *req_param;
458         struct hccs_req_head *req_head;
459         struct hccs_rsp_head rsp_head;
460         struct hccs_desc desc;
461         size_t left_buf_len;
462         u32 data_len = 0;
463         u8 start_id;
464         u8 *buf;
465         int ret;
466
467         buf = (u8 *)attrs;
468         left_buf_len = sizeof(struct hccs_port_attr) * size;
469         start_id = die->min_port_id;
470         while (start_id <= die->max_port_id) {
471                 hccs_init_req_desc(&desc);
472                 req_head = &desc.req.req_head;
473                 req_head->start_id = start_id;
474                 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
475                 req_param->chip_id = die->chip->chip_id;
476                 req_param->die_id = die->die_id;
477
478                 ret = hccs_get_bd_info(hdev, HCCS_GET_DIE_PORT_INFO, &desc,
479                                        buf + data_len, left_buf_len, &rsp_head);
480                 if (ret) {
481                         dev_err(hdev->dev,
482                                 "get the information of port%u on die%u failed, ret = %d.\n",
483                                 start_id, die->die_id, ret);
484                         return ret;
485                 }
486
487                 data_len += rsp_head.data_len;
488                 left_buf_len -= rsp_head.data_len;
489                 if (unlikely(rsp_head.next_id <= start_id)) {
490                         dev_err(hdev->dev,
491                                 "next port id (%u) is not greater than last start id (%u) on die%u.\n",
492                                 rsp_head.next_id, start_id, die->die_id);
493                         return -EINVAL;
494                 }
495                 start_id = rsp_head.next_id;
496         }
497
498         return 0;
499 }
500
501 static int hccs_get_all_port_info_on_die(struct hccs_dev *hdev,
502                                          struct hccs_die_info *die)
503 {
504         struct hccs_port_attr *attrs;
505         struct hccs_port_info *port;
506         int ret;
507         u8 i;
508
509         attrs = kcalloc(die->port_num, sizeof(struct hccs_port_attr),
510                         GFP_KERNEL);
511         if (!attrs)
512                 return -ENOMEM;
513
514         ret = hccs_get_all_port_attr(hdev, die, attrs, die->port_num);
515         if (ret)
516                 goto out;
517
518         for (i = 0; i < die->port_num; i++) {
519                 port = &die->ports[i];
520                 port->port_id = attrs[i].port_id;
521                 port->port_type = attrs[i].port_type;
522                 port->lane_mode = attrs[i].lane_mode;
523                 port->enable = attrs[i].enable;
524                 port->die = die;
525         }
526
527 out:
528         kfree(attrs);
529         return ret;
530 }
531
532 static int hccs_query_all_port_info_on_platform(struct hccs_dev *hdev)
533 {
534
535         struct device *dev = hdev->dev;
536         struct hccs_chip_info *chip;
537         struct hccs_die_info *die;
538         u8 i, j;
539         int ret;
540
541         for (i = 0; i < hdev->chip_num; i++) {
542                 chip = &hdev->chips[i];
543                 for (j = 0; j < chip->die_num; j++) {
544                         die = &chip->dies[j];
545                         if (!die->port_num)
546                                 continue;
547
548                         die->ports = devm_kzalloc(dev,
549                                 die->port_num * sizeof(struct hccs_port_info),
550                                 GFP_KERNEL);
551                         if (!die->ports) {
552                                 dev_err(dev, "allocate ports memory on chip%u/die%u failed.\n",
553                                         i, die->die_id);
554                                 return -ENOMEM;
555                         }
556
557                         ret = hccs_get_all_port_info_on_die(hdev, die);
558                         if (ret) {
559                                 dev_err(dev, "get all port info on chip%u/die%u failed, ret = %d.\n",
560                                         i, die->die_id, ret);
561                                 return ret;
562                         }
563                 }
564         }
565
566         return 0;
567 }
568
569 static int hccs_get_hw_info(struct hccs_dev *hdev)
570 {
571         int ret;
572
573         ret = hccs_query_chip_info_on_platform(hdev);
574         if (ret) {
575                 dev_err(hdev->dev, "query chip info on platform failed, ret = %d.\n",
576                         ret);
577                 return ret;
578         }
579
580         ret = hccs_query_all_die_info_on_platform(hdev);
581         if (ret) {
582                 dev_err(hdev->dev, "query all die info on platform failed, ret = %d.\n",
583                         ret);
584                 return ret;
585         }
586
587         ret = hccs_query_all_port_info_on_platform(hdev);
588         if (ret) {
589                 dev_err(hdev->dev, "query all port info on platform failed, ret = %d.\n",
590                         ret);
591                 return ret;
592         }
593
594         return 0;
595 }
596
597 static int hccs_query_port_link_status(struct hccs_dev *hdev,
598                                        const struct hccs_port_info *port,
599                                        struct hccs_link_status *link_status)
600 {
601         const struct hccs_die_info *die = port->die;
602         const struct hccs_chip_info *chip = die->chip;
603         struct hccs_port_comm_req_param *req_param;
604         struct hccs_desc desc;
605         int ret;
606
607         hccs_init_req_desc(&desc);
608         req_param = (struct hccs_port_comm_req_param *)desc.req.data;
609         req_param->chip_id = chip->chip_id;
610         req_param->die_id = die->die_id;
611         req_param->port_id = port->port_id;
612         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_LINK_STATUS, &desc);
613         if (ret) {
614                 dev_err(hdev->dev,
615                         "get port link status info failed, ret = %d.\n", ret);
616                 return ret;
617         }
618
619         *link_status = *((struct hccs_link_status *)desc.rsp.data);
620
621         return 0;
622 }
623
624 static int hccs_query_port_crc_err_cnt(struct hccs_dev *hdev,
625                                        const struct hccs_port_info *port,
626                                        u64 *crc_err_cnt)
627 {
628         const struct hccs_die_info *die = port->die;
629         const struct hccs_chip_info *chip = die->chip;
630         struct hccs_port_comm_req_param *req_param;
631         struct hccs_desc desc;
632         int ret;
633
634         hccs_init_req_desc(&desc);
635         req_param = (struct hccs_port_comm_req_param *)desc.req.data;
636         req_param->chip_id = chip->chip_id;
637         req_param->die_id = die->die_id;
638         req_param->port_id = port->port_id;
639         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_CRC_ERR_CNT, &desc);
640         if (ret) {
641                 dev_err(hdev->dev,
642                         "get port crc error count failed, ret = %d.\n", ret);
643                 return ret;
644         }
645
646         memcpy(crc_err_cnt, &desc.rsp.data, sizeof(u64));
647
648         return 0;
649 }
650
651 static int hccs_get_die_all_link_status(struct hccs_dev *hdev,
652                                         const struct hccs_die_info *die,
653                                         u8 *all_linked)
654 {
655         struct hccs_die_comm_req_param *req_param;
656         struct hccs_desc desc;
657         int ret;
658
659         if (die->port_num == 0) {
660                 *all_linked = 1;
661                 return 0;
662         }
663
664         hccs_init_req_desc(&desc);
665         req_param = (struct hccs_die_comm_req_param *)desc.req.data;
666         req_param->chip_id = die->chip->chip_id;
667         req_param->die_id = die->die_id;
668         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LINK_STA, &desc);
669         if (ret) {
670                 dev_err(hdev->dev,
671                         "get link status of all ports failed on die%u, ret = %d.\n",
672                         die->die_id, ret);
673                 return ret;
674         }
675
676         *all_linked = *((u8 *)&desc.rsp.data);
677
678         return 0;
679 }
680
681 static int hccs_get_die_all_port_lane_status(struct hccs_dev *hdev,
682                                              const struct hccs_die_info *die,
683                                              u8 *full_lane)
684 {
685         struct hccs_die_comm_req_param *req_param;
686         struct hccs_desc desc;
687         int ret;
688
689         if (die->port_num == 0) {
690                 *full_lane = 1;
691                 return 0;
692         }
693
694         hccs_init_req_desc(&desc);
695         req_param = (struct hccs_die_comm_req_param *)desc.req.data;
696         req_param->chip_id = die->chip->chip_id;
697         req_param->die_id = die->die_id;
698         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LANE_STA, &desc);
699         if (ret) {
700                 dev_err(hdev->dev, "get lane status of all ports failed on die%u, ret = %d.\n",
701                         die->die_id, ret);
702                 return ret;
703         }
704
705         *full_lane = *((u8 *)&desc.rsp.data);
706
707         return 0;
708 }
709
710 static int hccs_get_die_total_crc_err_cnt(struct hccs_dev *hdev,
711                                           const struct hccs_die_info *die,
712                                           u64 *total_crc_err_cnt)
713 {
714         struct hccs_die_comm_req_param *req_param;
715         struct hccs_desc desc;
716         int ret;
717
718         if (die->port_num == 0) {
719                 *total_crc_err_cnt = 0;
720                 return 0;
721         }
722
723         hccs_init_req_desc(&desc);
724         req_param = (struct hccs_die_comm_req_param *)desc.req.data;
725         req_param->chip_id = die->chip->chip_id;
726         req_param->die_id = die->die_id;
727         ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_CRC_ERR_CNT, &desc);
728         if (ret) {
729                 dev_err(hdev->dev, "get crc error count sum failed on die%u, ret = %d.\n",
730                         die->die_id, ret);
731                 return ret;
732         }
733
734         memcpy(total_crc_err_cnt, &desc.rsp.data, sizeof(u64));
735
736         return 0;
737 }
738
739 static ssize_t hccs_show(struct kobject *k, struct attribute *attr, char *buf)
740 {
741         struct kobj_attribute *kobj_attr;
742
743         kobj_attr = container_of(attr, struct kobj_attribute, attr);
744
745         return kobj_attr->show(k, kobj_attr, buf);
746 }
747
748 static const struct sysfs_ops hccs_comm_ops = {
749         .show = hccs_show,
750 };
751
752 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
753                          char *buf)
754 {
755         const struct hccs_port_info *port = kobj_to_port_info(kobj);
756
757         return sysfs_emit(buf, "HCCS-v%u\n", port->port_type);
758 }
759 static struct kobj_attribute hccs_type_attr = __ATTR_RO(type);
760
761 static ssize_t lane_mode_show(struct kobject *kobj, struct kobj_attribute *attr,
762                               char *buf)
763 {
764         const struct hccs_port_info *port = kobj_to_port_info(kobj);
765
766         return sysfs_emit(buf, "x%u\n", port->lane_mode);
767 }
768 static struct kobj_attribute lane_mode_attr = __ATTR_RO(lane_mode);
769
770 static ssize_t enable_show(struct kobject *kobj,
771                                  struct kobj_attribute *attr, char *buf)
772 {
773         const struct hccs_port_info *port = kobj_to_port_info(kobj);
774
775         return sysfs_emit(buf, "%u\n", port->enable);
776 }
777 static struct kobj_attribute port_enable_attr = __ATTR_RO(enable);
778
779 static ssize_t cur_lane_num_show(struct kobject *kobj,
780                                  struct kobj_attribute *attr, char *buf)
781 {
782         const struct hccs_port_info *port = kobj_to_port_info(kobj);
783         struct hccs_dev *hdev = port->die->chip->hdev;
784         struct hccs_link_status link_status = {0};
785         int ret;
786
787         mutex_lock(&hdev->lock);
788         ret = hccs_query_port_link_status(hdev, port, &link_status);
789         mutex_unlock(&hdev->lock);
790         if (ret)
791                 return ret;
792
793         return sysfs_emit(buf, "%u\n", link_status.lane_num);
794 }
795 static struct kobj_attribute cur_lane_num_attr = __ATTR_RO(cur_lane_num);
796
797 static ssize_t link_fsm_show(struct kobject *kobj,
798                              struct kobj_attribute *attr, char *buf)
799 {
800         const struct hccs_port_info *port = kobj_to_port_info(kobj);
801         struct hccs_dev *hdev = port->die->chip->hdev;
802         struct hccs_link_status link_status = {0};
803         const struct {
804                 u8 link_fsm;
805                 char *str;
806         } link_fsm_map[] = {
807                 {HCCS_PORT_RESET, "reset"},
808                 {HCCS_PORT_SETUP, "setup"},
809                 {HCCS_PORT_CONFIG, "config"},
810                 {HCCS_PORT_READY, "link-up"},
811         };
812         const char *link_fsm_str = "unknown";
813         size_t i;
814         int ret;
815
816         mutex_lock(&hdev->lock);
817         ret = hccs_query_port_link_status(hdev, port, &link_status);
818         mutex_unlock(&hdev->lock);
819         if (ret)
820                 return ret;
821
822         for (i = 0; i < ARRAY_SIZE(link_fsm_map); i++) {
823                 if (link_fsm_map[i].link_fsm == link_status.link_fsm) {
824                         link_fsm_str = link_fsm_map[i].str;
825                         break;
826                 }
827         }
828
829         return sysfs_emit(buf, "%s\n", link_fsm_str);
830 }
831 static struct kobj_attribute link_fsm_attr = __ATTR_RO(link_fsm);
832
833 static ssize_t lane_mask_show(struct kobject *kobj,
834                               struct kobj_attribute *attr, char *buf)
835 {
836         const struct hccs_port_info *port = kobj_to_port_info(kobj);
837         struct hccs_dev *hdev = port->die->chip->hdev;
838         struct hccs_link_status link_status = {0};
839         int ret;
840
841         mutex_lock(&hdev->lock);
842         ret = hccs_query_port_link_status(hdev, port, &link_status);
843         mutex_unlock(&hdev->lock);
844         if (ret)
845                 return ret;
846
847         return sysfs_emit(buf, "0x%x\n", link_status.lane_mask);
848 }
849 static struct kobj_attribute lane_mask_attr = __ATTR_RO(lane_mask);
850
851 static ssize_t crc_err_cnt_show(struct kobject *kobj,
852                                 struct kobj_attribute *attr, char *buf)
853 {
854         const struct hccs_port_info *port = kobj_to_port_info(kobj);
855         struct hccs_dev *hdev = port->die->chip->hdev;
856         u64 crc_err_cnt;
857         int ret;
858
859         mutex_lock(&hdev->lock);
860         ret = hccs_query_port_crc_err_cnt(hdev, port, &crc_err_cnt);
861         mutex_unlock(&hdev->lock);
862         if (ret)
863                 return ret;
864
865         return sysfs_emit(buf, "%llu\n", crc_err_cnt);
866 }
867 static struct kobj_attribute crc_err_cnt_attr = __ATTR_RO(crc_err_cnt);
868
869 static struct attribute *hccs_port_default_attrs[] = {
870         &hccs_type_attr.attr,
871         &lane_mode_attr.attr,
872         &port_enable_attr.attr,
873         &cur_lane_num_attr.attr,
874         &link_fsm_attr.attr,
875         &lane_mask_attr.attr,
876         &crc_err_cnt_attr.attr,
877         NULL,
878 };
879 ATTRIBUTE_GROUPS(hccs_port_default);
880
881 static const struct kobj_type hccs_port_type = {
882         .sysfs_ops = &hccs_comm_ops,
883         .default_groups = hccs_port_default_groups,
884 };
885
886 static ssize_t all_linked_on_die_show(struct kobject *kobj,
887                                       struct kobj_attribute *attr, char *buf)
888 {
889         const struct hccs_die_info *die = kobj_to_die_info(kobj);
890         struct hccs_dev *hdev = die->chip->hdev;
891         u8 all_linked;
892         int ret;
893
894         mutex_lock(&hdev->lock);
895         ret = hccs_get_die_all_link_status(hdev, die, &all_linked);
896         mutex_unlock(&hdev->lock);
897         if (ret)
898                 return ret;
899
900         return sysfs_emit(buf, "%u\n", all_linked);
901 }
902 static struct kobj_attribute all_linked_on_die_attr =
903                 __ATTR(all_linked, 0444, all_linked_on_die_show, NULL);
904
905 static ssize_t linked_full_lane_on_die_show(struct kobject *kobj,
906                                             struct kobj_attribute *attr,
907                                             char *buf)
908 {
909         const struct hccs_die_info *die = kobj_to_die_info(kobj);
910         struct hccs_dev *hdev = die->chip->hdev;
911         u8 full_lane;
912         int ret;
913
914         mutex_lock(&hdev->lock);
915         ret = hccs_get_die_all_port_lane_status(hdev, die, &full_lane);
916         mutex_unlock(&hdev->lock);
917         if (ret)
918                 return ret;
919
920         return sysfs_emit(buf, "%u\n", full_lane);
921 }
922 static struct kobj_attribute linked_full_lane_on_die_attr =
923         __ATTR(linked_full_lane, 0444, linked_full_lane_on_die_show, NULL);
924
925 static ssize_t crc_err_cnt_sum_on_die_show(struct kobject *kobj,
926                                            struct kobj_attribute *attr,
927                                            char *buf)
928 {
929         const struct hccs_die_info *die = kobj_to_die_info(kobj);
930         struct hccs_dev *hdev = die->chip->hdev;
931         u64 total_crc_err_cnt;
932         int ret;
933
934         mutex_lock(&hdev->lock);
935         ret = hccs_get_die_total_crc_err_cnt(hdev, die, &total_crc_err_cnt);
936         mutex_unlock(&hdev->lock);
937         if (ret)
938                 return ret;
939
940         return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
941 }
942 static struct kobj_attribute crc_err_cnt_sum_on_die_attr =
943         __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_die_show, NULL);
944
945 static struct attribute *hccs_die_default_attrs[] = {
946         &all_linked_on_die_attr.attr,
947         &linked_full_lane_on_die_attr.attr,
948         &crc_err_cnt_sum_on_die_attr.attr,
949         NULL,
950 };
951 ATTRIBUTE_GROUPS(hccs_die_default);
952
953 static const struct kobj_type hccs_die_type = {
954         .sysfs_ops = &hccs_comm_ops,
955         .default_groups = hccs_die_default_groups,
956 };
957
958 static ssize_t all_linked_on_chip_show(struct kobject *kobj,
959                                        struct kobj_attribute *attr, char *buf)
960 {
961         const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
962         struct hccs_dev *hdev = chip->hdev;
963         const struct hccs_die_info *die;
964         u8 all_linked = 1;
965         u8 i, tmp;
966         int ret;
967
968         mutex_lock(&hdev->lock);
969         for (i = 0; i < chip->die_num; i++) {
970                 die = &chip->dies[i];
971                 ret = hccs_get_die_all_link_status(hdev, die, &tmp);
972                 if (ret) {
973                         mutex_unlock(&hdev->lock);
974                         return ret;
975                 }
976                 if (tmp != all_linked) {
977                         all_linked = 0;
978                         break;
979                 }
980         }
981         mutex_unlock(&hdev->lock);
982
983         return sysfs_emit(buf, "%u\n", all_linked);
984 }
985 static struct kobj_attribute all_linked_on_chip_attr =
986                 __ATTR(all_linked, 0444, all_linked_on_chip_show, NULL);
987
988 static ssize_t linked_full_lane_on_chip_show(struct kobject *kobj,
989                                              struct kobj_attribute *attr,
990                                              char *buf)
991 {
992         const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
993         struct hccs_dev *hdev = chip->hdev;
994         const struct hccs_die_info *die;
995         u8 full_lane = 1;
996         u8 i, tmp;
997         int ret;
998
999         mutex_lock(&hdev->lock);
1000         for (i = 0; i < chip->die_num; i++) {
1001                 die = &chip->dies[i];
1002                 ret = hccs_get_die_all_port_lane_status(hdev, die, &tmp);
1003                 if (ret) {
1004                         mutex_unlock(&hdev->lock);
1005                         return ret;
1006                 }
1007                 if (tmp != full_lane) {
1008                         full_lane = 0;
1009                         break;
1010                 }
1011         }
1012         mutex_unlock(&hdev->lock);
1013
1014         return sysfs_emit(buf, "%u\n", full_lane);
1015 }
1016 static struct kobj_attribute linked_full_lane_on_chip_attr =
1017         __ATTR(linked_full_lane, 0444, linked_full_lane_on_chip_show, NULL);
1018
1019 static ssize_t crc_err_cnt_sum_on_chip_show(struct kobject *kobj,
1020                                             struct kobj_attribute *attr,
1021                                             char *buf)
1022 {
1023         const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
1024         u64 crc_err_cnt, total_crc_err_cnt = 0;
1025         struct hccs_dev *hdev = chip->hdev;
1026         const struct hccs_die_info *die;
1027         int ret;
1028         u16 i;
1029
1030         mutex_lock(&hdev->lock);
1031         for (i = 0; i < chip->die_num; i++) {
1032                 die = &chip->dies[i];
1033                 ret = hccs_get_die_total_crc_err_cnt(hdev, die, &crc_err_cnt);
1034                 if (ret) {
1035                         mutex_unlock(&hdev->lock);
1036                         return ret;
1037                 }
1038
1039                 total_crc_err_cnt += crc_err_cnt;
1040         }
1041         mutex_unlock(&hdev->lock);
1042
1043         return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
1044 }
1045 static struct kobj_attribute crc_err_cnt_sum_on_chip_attr =
1046                 __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_chip_show, NULL);
1047
1048 static struct attribute *hccs_chip_default_attrs[] = {
1049         &all_linked_on_chip_attr.attr,
1050         &linked_full_lane_on_chip_attr.attr,
1051         &crc_err_cnt_sum_on_chip_attr.attr,
1052         NULL,
1053 };
1054 ATTRIBUTE_GROUPS(hccs_chip_default);
1055
1056 static const struct kobj_type hccs_chip_type = {
1057         .sysfs_ops = &hccs_comm_ops,
1058         .default_groups = hccs_chip_default_groups,
1059 };
1060
1061 static void hccs_remove_die_dir(struct hccs_die_info *die)
1062 {
1063         struct hccs_port_info *port;
1064         u8 i;
1065
1066         for (i = 0; i < die->port_num; i++) {
1067                 port = &die->ports[i];
1068                 if (port->dir_created)
1069                         kobject_put(&port->kobj);
1070         }
1071
1072         kobject_put(&die->kobj);
1073 }
1074
1075 static void hccs_remove_chip_dir(struct hccs_chip_info *chip)
1076 {
1077         struct hccs_die_info *die;
1078         u8 i;
1079
1080         for (i = 0; i < chip->die_num; i++) {
1081                 die = &chip->dies[i];
1082                 if (die->dir_created)
1083                         hccs_remove_die_dir(die);
1084         }
1085
1086         kobject_put(&chip->kobj);
1087 }
1088
1089 static void hccs_remove_topo_dirs(struct hccs_dev *hdev)
1090 {
1091         u8 i;
1092
1093         for (i = 0; i < hdev->chip_num; i++)
1094                 hccs_remove_chip_dir(&hdev->chips[i]);
1095 }
1096
1097 static int hccs_create_hccs_dir(struct hccs_dev *hdev,
1098                                 struct hccs_die_info *die,
1099                                 struct hccs_port_info *port)
1100 {
1101         int ret;
1102
1103         ret = kobject_init_and_add(&port->kobj, &hccs_port_type,
1104                                    &die->kobj, "hccs%d", port->port_id);
1105         if (ret) {
1106                 kobject_put(&port->kobj);
1107                 return ret;
1108         }
1109
1110         return 0;
1111 }
1112
1113 static int hccs_create_die_dir(struct hccs_dev *hdev,
1114                                struct hccs_chip_info *chip,
1115                                struct hccs_die_info *die)
1116 {
1117         struct hccs_port_info *port;
1118         int ret;
1119         u16 i;
1120
1121         ret = kobject_init_and_add(&die->kobj, &hccs_die_type,
1122                                    &chip->kobj, "die%d", die->die_id);
1123         if (ret) {
1124                 kobject_put(&die->kobj);
1125                 return ret;
1126         }
1127
1128         for (i = 0; i < die->port_num; i++) {
1129                 port = &die->ports[i];
1130                 ret = hccs_create_hccs_dir(hdev, die, port);
1131                 if (ret) {
1132                         dev_err(hdev->dev, "create hccs%d dir failed.\n",
1133                                 port->port_id);
1134                         goto err;
1135                 }
1136                 port->dir_created = true;
1137         }
1138
1139         return 0;
1140 err:
1141         hccs_remove_die_dir(die);
1142
1143         return ret;
1144 }
1145
1146 static int hccs_create_chip_dir(struct hccs_dev *hdev,
1147                                 struct hccs_chip_info *chip)
1148 {
1149         struct hccs_die_info *die;
1150         int ret;
1151         u16 id;
1152
1153         ret = kobject_init_and_add(&chip->kobj, &hccs_chip_type,
1154                                    &hdev->dev->kobj, "chip%d", chip->chip_id);
1155         if (ret) {
1156                 kobject_put(&chip->kobj);
1157                 return ret;
1158         }
1159
1160         for (id = 0; id < chip->die_num; id++) {
1161                 die = &chip->dies[id];
1162                 ret = hccs_create_die_dir(hdev, chip, die);
1163                 if (ret)
1164                         goto err;
1165                 die->dir_created = true;
1166         }
1167
1168         return 0;
1169 err:
1170         hccs_remove_chip_dir(chip);
1171
1172         return ret;
1173 }
1174
1175 static int hccs_create_topo_dirs(struct hccs_dev *hdev)
1176 {
1177         struct hccs_chip_info *chip;
1178         u8 id, k;
1179         int ret;
1180
1181         for (id = 0; id < hdev->chip_num; id++) {
1182                 chip = &hdev->chips[id];
1183                 ret = hccs_create_chip_dir(hdev, chip);
1184                 if (ret) {
1185                         dev_err(hdev->dev, "init chip%d dir failed!\n", id);
1186                         goto err;
1187                 }
1188         }
1189
1190         return 0;
1191 err:
1192         for (k = 0; k < id; k++)
1193                 hccs_remove_chip_dir(&hdev->chips[k]);
1194
1195         return ret;
1196 }
1197
1198 static int hccs_probe(struct platform_device *pdev)
1199 {
1200         struct acpi_device *acpi_dev;
1201         struct hccs_dev *hdev;
1202         int rc;
1203
1204         if (acpi_disabled) {
1205                 dev_err(&pdev->dev, "acpi is disabled.\n");
1206                 return -ENODEV;
1207         }
1208         acpi_dev = ACPI_COMPANION(&pdev->dev);
1209         if (!acpi_dev)
1210                 return -ENODEV;
1211
1212         hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
1213         if (!hdev)
1214                 return -ENOMEM;
1215         hdev->acpi_dev = acpi_dev;
1216         hdev->dev = &pdev->dev;
1217         platform_set_drvdata(pdev, hdev);
1218
1219         mutex_init(&hdev->lock);
1220         rc = hccs_get_pcc_chan_id(hdev);
1221         if (rc)
1222                 return rc;
1223         rc = hccs_register_pcc_channel(hdev);
1224         if (rc)
1225                 return rc;
1226
1227         rc = hccs_get_dev_caps(hdev);
1228         if (rc)
1229                 goto unregister_pcc_chan;
1230
1231         rc = hccs_get_hw_info(hdev);
1232         if (rc)
1233                 goto unregister_pcc_chan;
1234
1235         rc = hccs_create_topo_dirs(hdev);
1236         if (rc)
1237                 goto unregister_pcc_chan;
1238
1239         return 0;
1240
1241 unregister_pcc_chan:
1242         hccs_unregister_pcc_channel(hdev);
1243
1244         return rc;
1245 }
1246
1247 static int hccs_remove(struct platform_device *pdev)
1248 {
1249         struct hccs_dev *hdev = platform_get_drvdata(pdev);
1250
1251         hccs_remove_topo_dirs(hdev);
1252         hccs_unregister_pcc_channel(hdev);
1253
1254         return 0;
1255 }
1256
1257 static const struct acpi_device_id hccs_acpi_match[] = {
1258         { "HISI04B1"},
1259         { ""},
1260 };
1261 MODULE_DEVICE_TABLE(acpi, hccs_acpi_match);
1262
1263 static struct platform_driver hccs_driver = {
1264         .probe = hccs_probe,
1265         .remove = hccs_remove,
1266         .driver = {
1267                 .name = "kunpeng_hccs",
1268                 .acpi_match_table = hccs_acpi_match,
1269         },
1270 };
1271
1272 module_platform_driver(hccs_driver);
1273
1274 MODULE_DESCRIPTION("Kunpeng SoC HCCS driver");
1275 MODULE_LICENSE("GPL");
1276 MODULE_AUTHOR("Huisong Li <lihuisong@huawei.com>");