1 // SPDX-License-Identifier: GPL-2.0+
3 * The Huawei Cache Coherence System (HCCS) is a multi-chip interconnection
6 * Copyright (c) 2023 Hisilicon Limited.
9 * HCCS driver for Kunpeng SoC provides the following features:
10 * - Retrieve the following information about each port:
15 * - link finite state machine
19 * - Retrieve the following information about all the ports on the chip or
21 * - if all enabled ports are in linked
22 * - if all linked ports are in full lane
23 * - CRC error count sum
25 #include <linux/acpi.h>
26 #include <linux/iopoll.h>
27 #include <linux/platform_device.h>
28 #include <linux/sysfs.h>
32 #include "kunpeng_hccs.h"
35 * Arbitrary retries in case the remote processor is slow to respond
38 #define HCCS_PCC_CMD_WAIT_RETRIES_NUM 500ULL
39 #define HCCS_POLL_STATUS_TIME_INTERVAL_US 3
41 static struct hccs_port_info *kobj_to_port_info(struct kobject *k)
43 return container_of(k, struct hccs_port_info, kobj);
46 static struct hccs_die_info *kobj_to_die_info(struct kobject *k)
48 return container_of(k, struct hccs_die_info, kobj);
51 static struct hccs_chip_info *kobj_to_chip_info(struct kobject *k)
53 return container_of(k, struct hccs_chip_info, kobj);
56 struct hccs_register_ctx {
62 static acpi_status hccs_get_register_cb(struct acpi_resource *ares,
65 struct acpi_resource_generic_register *reg;
66 struct hccs_register_ctx *ctx = context;
68 if (ares->type != ACPI_RESOURCE_TYPE_GENERIC_REGISTER)
71 reg = &ares->data.generic_reg;
72 if (reg->space_id != ACPI_ADR_SPACE_PLATFORM_COMM) {
73 dev_err(ctx->dev, "Bad register resource.\n");
77 ctx->chan_id = reg->access_size;
82 static int hccs_get_pcc_chan_id(struct hccs_dev *hdev)
84 acpi_handle handle = ACPI_HANDLE(hdev->dev);
85 struct hccs_register_ctx ctx = {0};
88 if (!acpi_has_method(handle, METHOD_NAME__CRS)) {
89 dev_err(hdev->dev, "No _CRS method.\n");
94 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
95 hccs_get_register_cb, &ctx);
96 if (ACPI_FAILURE(status))
98 hdev->chan_id = ctx.chan_id;
103 static void hccs_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
106 pr_debug("TX did not complete: CMD sent:0x%x, ret:%d\n",
109 pr_debug("TX completed. CMD sent:0x%x, ret:%d\n",
113 static void hccs_pcc_rx_callback(struct mbox_client *cl, void *mssg)
115 struct hccs_mbox_client_info *cl_info =
116 container_of(cl, struct hccs_mbox_client_info, client);
118 complete(&cl_info->done);
121 static void hccs_unregister_pcc_channel(struct hccs_dev *hdev)
123 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
125 if (cl_info->pcc_comm_addr)
126 iounmap(cl_info->pcc_comm_addr);
127 pcc_mbox_free_channel(hdev->cl_info.pcc_chan);
130 static int hccs_register_pcc_channel(struct hccs_dev *hdev)
132 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
133 struct mbox_client *cl = &cl_info->client;
134 struct pcc_mbox_chan *pcc_chan;
135 struct device *dev = hdev->dev;
139 cl->tx_block = false;
140 cl->knows_txdone = true;
141 cl->tx_done = hccs_chan_tx_done;
142 cl->rx_callback = hdev->verspec_data->rx_callback;
143 init_completion(&cl_info->done);
145 pcc_chan = pcc_mbox_request_channel(cl, hdev->chan_id);
146 if (IS_ERR(pcc_chan)) {
147 dev_err(dev, "PPC channel request failed.\n");
151 cl_info->pcc_chan = pcc_chan;
152 cl_info->mbox_chan = pcc_chan->mchan;
155 * pcc_chan->latency is just a nominal value. In reality the remote
156 * processor could be much slower to reply. So add an arbitrary amount
157 * of wait on top of nominal.
159 cl_info->deadline_us =
160 HCCS_PCC_CMD_WAIT_RETRIES_NUM * pcc_chan->latency;
161 if (!hdev->verspec_data->has_txdone_irq &&
162 cl_info->mbox_chan->mbox->txdone_irq) {
163 dev_err(dev, "PCC IRQ in PCCT is enabled.\n");
165 goto err_mbx_channel_free;
166 } else if (hdev->verspec_data->has_txdone_irq &&
167 !cl_info->mbox_chan->mbox->txdone_irq) {
168 dev_err(dev, "PCC IRQ in PCCT isn't supported.\n");
170 goto err_mbx_channel_free;
173 if (pcc_chan->shmem_base_addr) {
174 cl_info->pcc_comm_addr = ioremap(pcc_chan->shmem_base_addr,
175 pcc_chan->shmem_size);
176 if (!cl_info->pcc_comm_addr) {
177 dev_err(dev, "Failed to ioremap PCC communication region for channel-%u.\n",
180 goto err_mbx_channel_free;
186 err_mbx_channel_free:
187 pcc_mbox_free_channel(cl_info->pcc_chan);
192 static int hccs_wait_cmd_complete_by_poll(struct hccs_dev *hdev)
194 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
195 struct acpi_pcct_shared_memory __iomem *comm_base =
196 cl_info->pcc_comm_addr;
201 * Poll PCC status register every 3us(delay_us) for maximum of
202 * deadline_us(timeout_us) until PCC command complete bit is set(cond)
204 ret = readw_poll_timeout(&comm_base->status, status,
205 status & PCC_STATUS_CMD_COMPLETE,
206 HCCS_POLL_STATUS_TIME_INTERVAL_US,
207 cl_info->deadline_us);
209 dev_err(hdev->dev, "poll PCC status failed, ret = %d.\n", ret);
214 static int hccs_wait_cmd_complete_by_irq(struct hccs_dev *hdev)
216 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
218 if (!wait_for_completion_timeout(&cl_info->done,
219 usecs_to_jiffies(cl_info->deadline_us))) {
220 dev_err(hdev->dev, "PCC command executed timeout!\n");
227 static inline void hccs_fill_pcc_shared_mem_region(struct hccs_dev *hdev,
229 struct hccs_desc *desc,
230 void __iomem *comm_space,
233 struct acpi_pcct_shared_memory tmp = {
234 .signature = PCC_SIGNATURE | hdev->chan_id,
239 memcpy_toio(hdev->cl_info.pcc_comm_addr, (void *)&tmp,
240 sizeof(struct acpi_pcct_shared_memory));
242 /* Copy the message to the PCC comm space */
243 memcpy_toio(comm_space, (void *)desc, space_size);
246 static inline void hccs_fill_ext_pcc_shared_mem_region(struct hccs_dev *hdev,
248 struct hccs_desc *desc,
249 void __iomem *comm_space,
252 struct acpi_pcct_ext_pcc_shared_memory tmp = {
253 .signature = PCC_SIGNATURE | hdev->chan_id,
254 .flags = PCC_CMD_COMPLETION_NOTIFY,
255 .length = HCCS_PCC_SHARE_MEM_BYTES,
259 memcpy_toio(hdev->cl_info.pcc_comm_addr, (void *)&tmp,
260 sizeof(struct acpi_pcct_ext_pcc_shared_memory));
262 /* Copy the message to the PCC comm space */
263 memcpy_toio(comm_space, (void *)desc, space_size);
266 static int hccs_pcc_cmd_send(struct hccs_dev *hdev, u8 cmd,
267 struct hccs_desc *desc)
269 const struct hccs_verspecific_data *verspec_data = hdev->verspec_data;
270 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
271 struct hccs_fw_inner_head *fw_inner_head;
272 void __iomem *comm_space;
276 comm_space = cl_info->pcc_comm_addr + verspec_data->shared_mem_size;
277 space_size = HCCS_PCC_SHARE_MEM_BYTES - verspec_data->shared_mem_size;
278 verspec_data->fill_pcc_shared_mem(hdev, cmd, desc,
279 comm_space, space_size);
280 if (verspec_data->has_txdone_irq)
281 reinit_completion(&cl_info->done);
284 ret = mbox_send_message(cl_info->mbox_chan, &cmd);
286 dev_err(hdev->dev, "Send PCC mbox message failed, ret = %d.\n",
291 ret = verspec_data->wait_cmd_complete(hdev);
295 /* Copy response data */
296 memcpy_fromio((void *)desc, comm_space, space_size);
297 fw_inner_head = &desc->rsp.fw_inner_head;
298 if (fw_inner_head->retStatus) {
299 dev_err(hdev->dev, "Execute PCC command failed, error code = %u.\n",
300 fw_inner_head->retStatus);
305 if (verspec_data->has_txdone_irq)
306 mbox_chan_txdone(cl_info->mbox_chan, ret);
308 mbox_client_txdone(cl_info->mbox_chan, ret);
312 static void hccs_init_req_desc(struct hccs_desc *desc)
314 struct hccs_req_desc *req = &desc->req;
316 memset(desc, 0, sizeof(*desc));
317 req->req_head.module_code = HCCS_SERDES_MODULE_CODE;
320 static int hccs_get_dev_caps(struct hccs_dev *hdev)
322 struct hccs_desc desc;
325 hccs_init_req_desc(&desc);
326 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DEV_CAP, &desc);
328 dev_err(hdev->dev, "Get device capabilities failed, ret = %d.\n",
332 memcpy(&hdev->caps, desc.rsp.data, sizeof(hdev->caps));
337 static int hccs_query_chip_num_on_platform(struct hccs_dev *hdev)
339 struct hccs_desc desc;
342 hccs_init_req_desc(&desc);
343 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_CHIP_NUM, &desc);
345 dev_err(hdev->dev, "query system chip number failed, ret = %d.\n",
350 hdev->chip_num = *((u8 *)&desc.rsp.data);
351 if (!hdev->chip_num) {
352 dev_err(hdev->dev, "chip num obtained from firmware is zero.\n");
359 static int hccs_get_chip_info(struct hccs_dev *hdev,
360 struct hccs_chip_info *chip)
362 struct hccs_die_num_req_param *req_param;
363 struct hccs_desc desc;
366 hccs_init_req_desc(&desc);
367 req_param = (struct hccs_die_num_req_param *)desc.req.data;
368 req_param->chip_id = chip->chip_id;
369 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_NUM, &desc);
373 chip->die_num = *((u8 *)&desc.rsp.data);
378 static int hccs_query_chip_info_on_platform(struct hccs_dev *hdev)
380 struct hccs_chip_info *chip;
384 ret = hccs_query_chip_num_on_platform(hdev);
386 dev_err(hdev->dev, "query chip number on platform failed, ret = %d.\n",
391 hdev->chips = devm_kzalloc(hdev->dev,
392 hdev->chip_num * sizeof(struct hccs_chip_info),
395 dev_err(hdev->dev, "allocate all chips memory failed.\n");
399 for (idx = 0; idx < hdev->chip_num; idx++) {
400 chip = &hdev->chips[idx];
402 ret = hccs_get_chip_info(hdev, chip);
404 dev_err(hdev->dev, "get chip%u info failed, ret = %d.\n",
414 static int hccs_query_die_info_on_chip(struct hccs_dev *hdev, u8 chip_id,
415 u8 die_idx, struct hccs_die_info *die)
417 struct hccs_die_info_req_param *req_param;
418 struct hccs_die_info_rsp_data *rsp_data;
419 struct hccs_desc desc;
422 hccs_init_req_desc(&desc);
423 req_param = (struct hccs_die_info_req_param *)desc.req.data;
424 req_param->chip_id = chip_id;
425 req_param->die_idx = die_idx;
426 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_INFO, &desc);
430 rsp_data = (struct hccs_die_info_rsp_data *)desc.rsp.data;
431 die->die_id = rsp_data->die_id;
432 die->port_num = rsp_data->port_num;
433 die->min_port_id = rsp_data->min_port_id;
434 die->max_port_id = rsp_data->max_port_id;
435 if (die->min_port_id > die->max_port_id) {
436 dev_err(hdev->dev, "min port id(%u) > max port id(%u) on die_idx(%u).\n",
437 die->min_port_id, die->max_port_id, die_idx);
440 if (die->max_port_id > HCCS_DIE_MAX_PORT_ID) {
441 dev_err(hdev->dev, "max port id(%u) on die_idx(%u) is too big.\n",
442 die->max_port_id, die_idx);
449 static int hccs_query_all_die_info_on_platform(struct hccs_dev *hdev)
451 struct device *dev = hdev->dev;
452 struct hccs_chip_info *chip;
453 struct hccs_die_info *die;
457 for (i = 0; i < hdev->chip_num; i++) {
458 chip = &hdev->chips[i];
462 chip->dies = devm_kzalloc(hdev->dev,
463 chip->die_num * sizeof(struct hccs_die_info),
466 dev_err(dev, "allocate all dies memory on chip%u failed.\n",
471 for (j = 0; j < chip->die_num; j++) {
472 die = &chip->dies[j];
473 ret = hccs_query_die_info_on_chip(hdev, i, j, die);
475 dev_err(dev, "get die idx (%u) info on chip%u failed, ret = %d.\n",
486 static int hccs_get_bd_info(struct hccs_dev *hdev, u8 opcode,
487 struct hccs_desc *desc,
488 void *buf, size_t buf_len,
489 struct hccs_rsp_head *rsp_head)
491 struct hccs_rsp_head *head;
492 struct hccs_rsp_desc *rsp;
495 ret = hccs_pcc_cmd_send(hdev, opcode, desc);
500 head = &rsp->rsp_head;
501 if (head->data_len > buf_len) {
503 "buffer overflow (buf_len = %zu, data_len = %u)!\n",
504 buf_len, head->data_len);
508 memcpy(buf, rsp->data, head->data_len);
514 static int hccs_get_all_port_attr(struct hccs_dev *hdev,
515 struct hccs_die_info *die,
516 struct hccs_port_attr *attrs, u16 size)
518 struct hccs_die_comm_req_param *req_param;
519 struct hccs_req_head *req_head;
520 struct hccs_rsp_head rsp_head;
521 struct hccs_desc desc;
529 left_buf_len = sizeof(struct hccs_port_attr) * size;
530 start_id = die->min_port_id;
531 while (start_id <= die->max_port_id) {
532 hccs_init_req_desc(&desc);
533 req_head = &desc.req.req_head;
534 req_head->start_id = start_id;
535 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
536 req_param->chip_id = die->chip->chip_id;
537 req_param->die_id = die->die_id;
539 ret = hccs_get_bd_info(hdev, HCCS_GET_DIE_PORT_INFO, &desc,
540 buf + data_len, left_buf_len, &rsp_head);
543 "get the information of port%u on die%u failed, ret = %d.\n",
544 start_id, die->die_id, ret);
548 data_len += rsp_head.data_len;
549 left_buf_len -= rsp_head.data_len;
550 if (unlikely(rsp_head.next_id <= start_id)) {
552 "next port id (%u) is not greater than last start id (%u) on die%u.\n",
553 rsp_head.next_id, start_id, die->die_id);
556 start_id = rsp_head.next_id;
559 if (left_buf_len != 0) {
560 dev_err(hdev->dev, "failed to get the expected port number(%u) attribute.\n",
568 static int hccs_get_all_port_info_on_die(struct hccs_dev *hdev,
569 struct hccs_die_info *die)
571 struct hccs_port_attr *attrs;
572 struct hccs_port_info *port;
576 attrs = kcalloc(die->port_num, sizeof(struct hccs_port_attr),
581 ret = hccs_get_all_port_attr(hdev, die, attrs, die->port_num);
585 for (i = 0; i < die->port_num; i++) {
586 port = &die->ports[i];
587 port->port_id = attrs[i].port_id;
588 port->port_type = attrs[i].port_type;
589 port->lane_mode = attrs[i].lane_mode;
590 port->enable = attrs[i].enable;
599 static int hccs_query_all_port_info_on_platform(struct hccs_dev *hdev)
601 struct device *dev = hdev->dev;
602 struct hccs_chip_info *chip;
603 struct hccs_die_info *die;
607 for (i = 0; i < hdev->chip_num; i++) {
608 chip = &hdev->chips[i];
609 for (j = 0; j < chip->die_num; j++) {
610 die = &chip->dies[j];
614 die->ports = devm_kzalloc(dev,
615 die->port_num * sizeof(struct hccs_port_info),
618 dev_err(dev, "allocate ports memory on chip%u/die%u failed.\n",
623 ret = hccs_get_all_port_info_on_die(hdev, die);
625 dev_err(dev, "get all port info on chip%u/die%u failed, ret = %d.\n",
626 i, die->die_id, ret);
635 static int hccs_get_hw_info(struct hccs_dev *hdev)
639 ret = hccs_query_chip_info_on_platform(hdev);
641 dev_err(hdev->dev, "query chip info on platform failed, ret = %d.\n",
646 ret = hccs_query_all_die_info_on_platform(hdev);
648 dev_err(hdev->dev, "query all die info on platform failed, ret = %d.\n",
653 ret = hccs_query_all_port_info_on_platform(hdev);
655 dev_err(hdev->dev, "query all port info on platform failed, ret = %d.\n",
663 static int hccs_query_port_link_status(struct hccs_dev *hdev,
664 const struct hccs_port_info *port,
665 struct hccs_link_status *link_status)
667 const struct hccs_die_info *die = port->die;
668 const struct hccs_chip_info *chip = die->chip;
669 struct hccs_port_comm_req_param *req_param;
670 struct hccs_desc desc;
673 hccs_init_req_desc(&desc);
674 req_param = (struct hccs_port_comm_req_param *)desc.req.data;
675 req_param->chip_id = chip->chip_id;
676 req_param->die_id = die->die_id;
677 req_param->port_id = port->port_id;
678 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_LINK_STATUS, &desc);
681 "get port link status info failed, ret = %d.\n", ret);
685 *link_status = *((struct hccs_link_status *)desc.rsp.data);
690 static int hccs_query_port_crc_err_cnt(struct hccs_dev *hdev,
691 const struct hccs_port_info *port,
694 const struct hccs_die_info *die = port->die;
695 const struct hccs_chip_info *chip = die->chip;
696 struct hccs_port_comm_req_param *req_param;
697 struct hccs_desc desc;
700 hccs_init_req_desc(&desc);
701 req_param = (struct hccs_port_comm_req_param *)desc.req.data;
702 req_param->chip_id = chip->chip_id;
703 req_param->die_id = die->die_id;
704 req_param->port_id = port->port_id;
705 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_CRC_ERR_CNT, &desc);
708 "get port crc error count failed, ret = %d.\n", ret);
712 memcpy(crc_err_cnt, &desc.rsp.data, sizeof(u64));
717 static int hccs_get_die_all_link_status(struct hccs_dev *hdev,
718 const struct hccs_die_info *die,
721 struct hccs_die_comm_req_param *req_param;
722 struct hccs_desc desc;
725 if (die->port_num == 0) {
730 hccs_init_req_desc(&desc);
731 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
732 req_param->chip_id = die->chip->chip_id;
733 req_param->die_id = die->die_id;
734 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LINK_STA, &desc);
737 "get link status of all ports failed on die%u, ret = %d.\n",
742 *all_linked = *((u8 *)&desc.rsp.data);
747 static int hccs_get_die_all_port_lane_status(struct hccs_dev *hdev,
748 const struct hccs_die_info *die,
751 struct hccs_die_comm_req_param *req_param;
752 struct hccs_desc desc;
755 if (die->port_num == 0) {
760 hccs_init_req_desc(&desc);
761 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
762 req_param->chip_id = die->chip->chip_id;
763 req_param->die_id = die->die_id;
764 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LANE_STA, &desc);
766 dev_err(hdev->dev, "get lane status of all ports failed on die%u, ret = %d.\n",
771 *full_lane = *((u8 *)&desc.rsp.data);
776 static int hccs_get_die_total_crc_err_cnt(struct hccs_dev *hdev,
777 const struct hccs_die_info *die,
778 u64 *total_crc_err_cnt)
780 struct hccs_die_comm_req_param *req_param;
781 struct hccs_desc desc;
784 if (die->port_num == 0) {
785 *total_crc_err_cnt = 0;
789 hccs_init_req_desc(&desc);
790 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
791 req_param->chip_id = die->chip->chip_id;
792 req_param->die_id = die->die_id;
793 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_CRC_ERR_CNT, &desc);
795 dev_err(hdev->dev, "get crc error count sum failed on die%u, ret = %d.\n",
800 memcpy(total_crc_err_cnt, &desc.rsp.data, sizeof(u64));
805 static ssize_t hccs_show(struct kobject *k, struct attribute *attr, char *buf)
807 struct kobj_attribute *kobj_attr;
809 kobj_attr = container_of(attr, struct kobj_attribute, attr);
811 return kobj_attr->show(k, kobj_attr, buf);
814 static const struct sysfs_ops hccs_comm_ops = {
818 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
821 const struct hccs_port_info *port = kobj_to_port_info(kobj);
823 return sysfs_emit(buf, "HCCS-v%u\n", port->port_type);
825 static struct kobj_attribute hccs_type_attr = __ATTR_RO(type);
827 static ssize_t lane_mode_show(struct kobject *kobj, struct kobj_attribute *attr,
830 const struct hccs_port_info *port = kobj_to_port_info(kobj);
832 return sysfs_emit(buf, "x%u\n", port->lane_mode);
834 static struct kobj_attribute lane_mode_attr = __ATTR_RO(lane_mode);
836 static ssize_t enable_show(struct kobject *kobj,
837 struct kobj_attribute *attr, char *buf)
839 const struct hccs_port_info *port = kobj_to_port_info(kobj);
841 return sysfs_emit(buf, "%u\n", port->enable);
843 static struct kobj_attribute port_enable_attr = __ATTR_RO(enable);
845 static ssize_t cur_lane_num_show(struct kobject *kobj,
846 struct kobj_attribute *attr, char *buf)
848 const struct hccs_port_info *port = kobj_to_port_info(kobj);
849 struct hccs_dev *hdev = port->die->chip->hdev;
850 struct hccs_link_status link_status = {0};
853 mutex_lock(&hdev->lock);
854 ret = hccs_query_port_link_status(hdev, port, &link_status);
855 mutex_unlock(&hdev->lock);
859 return sysfs_emit(buf, "%u\n", link_status.lane_num);
861 static struct kobj_attribute cur_lane_num_attr = __ATTR_RO(cur_lane_num);
863 static ssize_t link_fsm_show(struct kobject *kobj,
864 struct kobj_attribute *attr, char *buf)
866 const struct hccs_port_info *port = kobj_to_port_info(kobj);
867 struct hccs_dev *hdev = port->die->chip->hdev;
868 struct hccs_link_status link_status = {0};
873 {HCCS_PORT_RESET, "reset"},
874 {HCCS_PORT_SETUP, "setup"},
875 {HCCS_PORT_CONFIG, "config"},
876 {HCCS_PORT_READY, "link-up"},
878 const char *link_fsm_str = "unknown";
882 mutex_lock(&hdev->lock);
883 ret = hccs_query_port_link_status(hdev, port, &link_status);
884 mutex_unlock(&hdev->lock);
888 for (i = 0; i < ARRAY_SIZE(link_fsm_map); i++) {
889 if (link_fsm_map[i].link_fsm == link_status.link_fsm) {
890 link_fsm_str = link_fsm_map[i].str;
895 return sysfs_emit(buf, "%s\n", link_fsm_str);
897 static struct kobj_attribute link_fsm_attr = __ATTR_RO(link_fsm);
899 static ssize_t lane_mask_show(struct kobject *kobj,
900 struct kobj_attribute *attr, char *buf)
902 const struct hccs_port_info *port = kobj_to_port_info(kobj);
903 struct hccs_dev *hdev = port->die->chip->hdev;
904 struct hccs_link_status link_status = {0};
907 mutex_lock(&hdev->lock);
908 ret = hccs_query_port_link_status(hdev, port, &link_status);
909 mutex_unlock(&hdev->lock);
913 return sysfs_emit(buf, "0x%x\n", link_status.lane_mask);
915 static struct kobj_attribute lane_mask_attr = __ATTR_RO(lane_mask);
917 static ssize_t crc_err_cnt_show(struct kobject *kobj,
918 struct kobj_attribute *attr, char *buf)
920 const struct hccs_port_info *port = kobj_to_port_info(kobj);
921 struct hccs_dev *hdev = port->die->chip->hdev;
925 mutex_lock(&hdev->lock);
926 ret = hccs_query_port_crc_err_cnt(hdev, port, &crc_err_cnt);
927 mutex_unlock(&hdev->lock);
931 return sysfs_emit(buf, "%llu\n", crc_err_cnt);
933 static struct kobj_attribute crc_err_cnt_attr = __ATTR_RO(crc_err_cnt);
935 static struct attribute *hccs_port_default_attrs[] = {
936 &hccs_type_attr.attr,
937 &lane_mode_attr.attr,
938 &port_enable_attr.attr,
939 &cur_lane_num_attr.attr,
941 &lane_mask_attr.attr,
942 &crc_err_cnt_attr.attr,
945 ATTRIBUTE_GROUPS(hccs_port_default);
947 static const struct kobj_type hccs_port_type = {
948 .sysfs_ops = &hccs_comm_ops,
949 .default_groups = hccs_port_default_groups,
952 static ssize_t all_linked_on_die_show(struct kobject *kobj,
953 struct kobj_attribute *attr, char *buf)
955 const struct hccs_die_info *die = kobj_to_die_info(kobj);
956 struct hccs_dev *hdev = die->chip->hdev;
960 mutex_lock(&hdev->lock);
961 ret = hccs_get_die_all_link_status(hdev, die, &all_linked);
962 mutex_unlock(&hdev->lock);
966 return sysfs_emit(buf, "%u\n", all_linked);
968 static struct kobj_attribute all_linked_on_die_attr =
969 __ATTR(all_linked, 0444, all_linked_on_die_show, NULL);
971 static ssize_t linked_full_lane_on_die_show(struct kobject *kobj,
972 struct kobj_attribute *attr,
975 const struct hccs_die_info *die = kobj_to_die_info(kobj);
976 struct hccs_dev *hdev = die->chip->hdev;
980 mutex_lock(&hdev->lock);
981 ret = hccs_get_die_all_port_lane_status(hdev, die, &full_lane);
982 mutex_unlock(&hdev->lock);
986 return sysfs_emit(buf, "%u\n", full_lane);
988 static struct kobj_attribute linked_full_lane_on_die_attr =
989 __ATTR(linked_full_lane, 0444, linked_full_lane_on_die_show, NULL);
991 static ssize_t crc_err_cnt_sum_on_die_show(struct kobject *kobj,
992 struct kobj_attribute *attr,
995 const struct hccs_die_info *die = kobj_to_die_info(kobj);
996 struct hccs_dev *hdev = die->chip->hdev;
997 u64 total_crc_err_cnt;
1000 mutex_lock(&hdev->lock);
1001 ret = hccs_get_die_total_crc_err_cnt(hdev, die, &total_crc_err_cnt);
1002 mutex_unlock(&hdev->lock);
1006 return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
1008 static struct kobj_attribute crc_err_cnt_sum_on_die_attr =
1009 __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_die_show, NULL);
1011 static struct attribute *hccs_die_default_attrs[] = {
1012 &all_linked_on_die_attr.attr,
1013 &linked_full_lane_on_die_attr.attr,
1014 &crc_err_cnt_sum_on_die_attr.attr,
1017 ATTRIBUTE_GROUPS(hccs_die_default);
1019 static const struct kobj_type hccs_die_type = {
1020 .sysfs_ops = &hccs_comm_ops,
1021 .default_groups = hccs_die_default_groups,
1024 static ssize_t all_linked_on_chip_show(struct kobject *kobj,
1025 struct kobj_attribute *attr, char *buf)
1027 const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
1028 struct hccs_dev *hdev = chip->hdev;
1029 const struct hccs_die_info *die;
1034 mutex_lock(&hdev->lock);
1035 for (i = 0; i < chip->die_num; i++) {
1036 die = &chip->dies[i];
1037 ret = hccs_get_die_all_link_status(hdev, die, &tmp);
1039 mutex_unlock(&hdev->lock);
1042 if (tmp != all_linked) {
1047 mutex_unlock(&hdev->lock);
1049 return sysfs_emit(buf, "%u\n", all_linked);
1051 static struct kobj_attribute all_linked_on_chip_attr =
1052 __ATTR(all_linked, 0444, all_linked_on_chip_show, NULL);
1054 static ssize_t linked_full_lane_on_chip_show(struct kobject *kobj,
1055 struct kobj_attribute *attr,
1058 const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
1059 struct hccs_dev *hdev = chip->hdev;
1060 const struct hccs_die_info *die;
1065 mutex_lock(&hdev->lock);
1066 for (i = 0; i < chip->die_num; i++) {
1067 die = &chip->dies[i];
1068 ret = hccs_get_die_all_port_lane_status(hdev, die, &tmp);
1070 mutex_unlock(&hdev->lock);
1073 if (tmp != full_lane) {
1078 mutex_unlock(&hdev->lock);
1080 return sysfs_emit(buf, "%u\n", full_lane);
1082 static struct kobj_attribute linked_full_lane_on_chip_attr =
1083 __ATTR(linked_full_lane, 0444, linked_full_lane_on_chip_show, NULL);
1085 static ssize_t crc_err_cnt_sum_on_chip_show(struct kobject *kobj,
1086 struct kobj_attribute *attr,
1089 const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
1090 u64 crc_err_cnt, total_crc_err_cnt = 0;
1091 struct hccs_dev *hdev = chip->hdev;
1092 const struct hccs_die_info *die;
1096 mutex_lock(&hdev->lock);
1097 for (i = 0; i < chip->die_num; i++) {
1098 die = &chip->dies[i];
1099 ret = hccs_get_die_total_crc_err_cnt(hdev, die, &crc_err_cnt);
1101 mutex_unlock(&hdev->lock);
1105 total_crc_err_cnt += crc_err_cnt;
1107 mutex_unlock(&hdev->lock);
1109 return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
1111 static struct kobj_attribute crc_err_cnt_sum_on_chip_attr =
1112 __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_chip_show, NULL);
1114 static struct attribute *hccs_chip_default_attrs[] = {
1115 &all_linked_on_chip_attr.attr,
1116 &linked_full_lane_on_chip_attr.attr,
1117 &crc_err_cnt_sum_on_chip_attr.attr,
1120 ATTRIBUTE_GROUPS(hccs_chip_default);
1122 static const struct kobj_type hccs_chip_type = {
1123 .sysfs_ops = &hccs_comm_ops,
1124 .default_groups = hccs_chip_default_groups,
1127 static void hccs_remove_die_dir(struct hccs_die_info *die)
1129 struct hccs_port_info *port;
1132 for (i = 0; i < die->port_num; i++) {
1133 port = &die->ports[i];
1134 if (port->dir_created)
1135 kobject_put(&port->kobj);
1138 kobject_put(&die->kobj);
1141 static void hccs_remove_chip_dir(struct hccs_chip_info *chip)
1143 struct hccs_die_info *die;
1146 for (i = 0; i < chip->die_num; i++) {
1147 die = &chip->dies[i];
1148 if (die->dir_created)
1149 hccs_remove_die_dir(die);
1152 kobject_put(&chip->kobj);
1155 static void hccs_remove_topo_dirs(struct hccs_dev *hdev)
1159 for (i = 0; i < hdev->chip_num; i++)
1160 hccs_remove_chip_dir(&hdev->chips[i]);
1163 static int hccs_create_hccs_dir(struct hccs_dev *hdev,
1164 struct hccs_die_info *die,
1165 struct hccs_port_info *port)
1169 ret = kobject_init_and_add(&port->kobj, &hccs_port_type,
1170 &die->kobj, "hccs%u", port->port_id);
1172 kobject_put(&port->kobj);
1179 static int hccs_create_die_dir(struct hccs_dev *hdev,
1180 struct hccs_chip_info *chip,
1181 struct hccs_die_info *die)
1183 struct hccs_port_info *port;
1187 ret = kobject_init_and_add(&die->kobj, &hccs_die_type,
1188 &chip->kobj, "die%u", die->die_id);
1190 kobject_put(&die->kobj);
1194 for (i = 0; i < die->port_num; i++) {
1195 port = &die->ports[i];
1196 ret = hccs_create_hccs_dir(hdev, die, port);
1198 dev_err(hdev->dev, "create hccs%u dir failed.\n",
1202 port->dir_created = true;
1207 hccs_remove_die_dir(die);
1212 static int hccs_create_chip_dir(struct hccs_dev *hdev,
1213 struct hccs_chip_info *chip)
1215 struct hccs_die_info *die;
1219 ret = kobject_init_and_add(&chip->kobj, &hccs_chip_type,
1220 &hdev->dev->kobj, "chip%u", chip->chip_id);
1222 kobject_put(&chip->kobj);
1226 for (id = 0; id < chip->die_num; id++) {
1227 die = &chip->dies[id];
1228 ret = hccs_create_die_dir(hdev, chip, die);
1231 die->dir_created = true;
1236 hccs_remove_chip_dir(chip);
1241 static int hccs_create_topo_dirs(struct hccs_dev *hdev)
1243 struct hccs_chip_info *chip;
1247 for (id = 0; id < hdev->chip_num; id++) {
1248 chip = &hdev->chips[id];
1249 ret = hccs_create_chip_dir(hdev, chip);
1251 dev_err(hdev->dev, "init chip%u dir failed!\n", id);
1258 for (k = 0; k < id; k++)
1259 hccs_remove_chip_dir(&hdev->chips[k]);
1264 static int hccs_probe(struct platform_device *pdev)
1266 struct acpi_device *acpi_dev;
1267 struct hccs_dev *hdev;
1270 if (acpi_disabled) {
1271 dev_err(&pdev->dev, "acpi is disabled.\n");
1274 acpi_dev = ACPI_COMPANION(&pdev->dev);
1278 hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
1281 hdev->acpi_dev = acpi_dev;
1282 hdev->dev = &pdev->dev;
1283 platform_set_drvdata(pdev, hdev);
1286 * Here would never be failure as the driver and device has been matched.
1288 hdev->verspec_data = acpi_device_get_match_data(hdev->dev);
1290 mutex_init(&hdev->lock);
1291 rc = hccs_get_pcc_chan_id(hdev);
1294 rc = hccs_register_pcc_channel(hdev);
1298 rc = hccs_get_dev_caps(hdev);
1300 goto unregister_pcc_chan;
1302 rc = hccs_get_hw_info(hdev);
1304 goto unregister_pcc_chan;
1306 rc = hccs_create_topo_dirs(hdev);
1308 goto unregister_pcc_chan;
1312 unregister_pcc_chan:
1313 hccs_unregister_pcc_channel(hdev);
1318 static void hccs_remove(struct platform_device *pdev)
1320 struct hccs_dev *hdev = platform_get_drvdata(pdev);
1322 hccs_remove_topo_dirs(hdev);
1323 hccs_unregister_pcc_channel(hdev);
1326 static const struct hccs_verspecific_data hisi04b1_verspec_data = {
1327 .rx_callback = NULL,
1328 .wait_cmd_complete = hccs_wait_cmd_complete_by_poll,
1329 .fill_pcc_shared_mem = hccs_fill_pcc_shared_mem_region,
1330 .shared_mem_size = sizeof(struct acpi_pcct_shared_memory),
1331 .has_txdone_irq = false,
1334 static const struct hccs_verspecific_data hisi04b2_verspec_data = {
1335 .rx_callback = hccs_pcc_rx_callback,
1336 .wait_cmd_complete = hccs_wait_cmd_complete_by_irq,
1337 .fill_pcc_shared_mem = hccs_fill_ext_pcc_shared_mem_region,
1338 .shared_mem_size = sizeof(struct acpi_pcct_ext_pcc_shared_memory),
1339 .has_txdone_irq = true,
1342 static const struct acpi_device_id hccs_acpi_match[] = {
1343 { "HISI04B1", (unsigned long)&hisi04b1_verspec_data},
1344 { "HISI04B2", (unsigned long)&hisi04b2_verspec_data},
1347 MODULE_DEVICE_TABLE(acpi, hccs_acpi_match);
1349 static struct platform_driver hccs_driver = {
1350 .probe = hccs_probe,
1351 .remove_new = hccs_remove,
1353 .name = "kunpeng_hccs",
1354 .acpi_match_table = hccs_acpi_match,
1358 module_platform_driver(hccs_driver);
1360 MODULE_DESCRIPTION("Kunpeng SoC HCCS driver");
1361 MODULE_LICENSE("GPL");