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.
7 * Author: Huisong Li <lihuisong@huawei.com>
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 #define HCCS_PCC_SIGNATURE_MASK 0x50434300
36 #define HCCS_PCC_STATUS_CMD_COMPLETE BIT(0)
39 * Arbitrary retries in case the remote processor is slow to respond
42 #define HCCS_PCC_CMD_WAIT_RETRIES_NUM 500ULL
43 #define HCCS_POLL_STATUS_TIME_INTERVAL_US 3
45 static struct hccs_port_info *kobj_to_port_info(struct kobject *k)
47 return container_of(k, struct hccs_port_info, kobj);
50 static struct hccs_die_info *kobj_to_die_info(struct kobject *k)
52 return container_of(k, struct hccs_die_info, kobj);
55 static struct hccs_chip_info *kobj_to_chip_info(struct kobject *k)
57 return container_of(k, struct hccs_chip_info, kobj);
60 struct hccs_register_ctx {
66 static acpi_status hccs_get_register_cb(struct acpi_resource *ares,
69 struct acpi_resource_generic_register *reg;
70 struct hccs_register_ctx *ctx = context;
72 if (ares->type != ACPI_RESOURCE_TYPE_GENERIC_REGISTER)
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");
81 ctx->chan_id = reg->access_size;
86 static int hccs_get_pcc_chan_id(struct hccs_dev *hdev)
88 acpi_handle handle = ACPI_HANDLE(hdev->dev);
89 struct hccs_register_ctx ctx = {0};
92 if (!acpi_has_method(handle, METHOD_NAME__CRS))
96 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
97 hccs_get_register_cb, &ctx);
98 if (ACPI_FAILURE(status))
100 hdev->chan_id = ctx.chan_id;
105 static void hccs_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
108 pr_debug("TX did not complete: CMD sent:0x%x, ret:%d\n",
111 pr_debug("TX completed. CMD sent:0x%x, ret:%d\n",
115 static void hccs_unregister_pcc_channel(struct hccs_dev *hdev)
117 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
119 if (cl_info->pcc_comm_addr)
120 iounmap(cl_info->pcc_comm_addr);
121 pcc_mbox_free_channel(hdev->cl_info.pcc_chan);
124 static int hccs_register_pcc_channel(struct hccs_dev *hdev)
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;
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");
142 cl_info->pcc_chan = pcc_chan;
143 cl_info->mbox_chan = pcc_chan->mchan;
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.
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");
155 goto err_mbx_channel_free;
158 if (pcc_chan->shmem_base_addr) {
159 cl_info->pcc_comm_addr = (void __force *)ioremap(
160 pcc_chan->shmem_base_addr, 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",
165 goto err_mbx_channel_free;
171 err_mbx_channel_free:
172 pcc_mbox_free_channel(cl_info->pcc_chan);
177 static int hccs_check_chan_cmd_complete(struct hccs_dev *hdev)
179 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
180 struct acpi_pcct_shared_memory *comm_base = cl_info->pcc_comm_addr;
185 * Poll PCC status register every 3us(delay_us) for maximum of
186 * deadline_us(timeout_us) until PCC command complete bit is set(cond)
188 ret = readw_poll_timeout(&comm_base->status, status,
189 status & HCCS_PCC_STATUS_CMD_COMPLETE,
190 HCCS_POLL_STATUS_TIME_INTERVAL_US,
191 cl_info->deadline_us);
193 dev_err(hdev->dev, "poll PCC status failed, ret = %d.\n", ret);
198 static int hccs_pcc_cmd_send(struct hccs_dev *hdev, u8 cmd,
199 struct hccs_desc *desc)
201 struct hccs_mbox_client_info *cl_info = &hdev->cl_info;
202 struct acpi_pcct_shared_memory *comm_base = cl_info->pcc_comm_addr;
203 void *comm_space = (void *)(comm_base + 1);
204 struct hccs_fw_inner_head *fw_inner_head;
205 struct acpi_pcct_shared_memory tmp = {0};
209 /* Write signature for this subspace */
210 tmp.signature = HCCS_PCC_SIGNATURE_MASK | hdev->chan_id;
211 /* Write to the shared command region */
213 /* Clear cmd complete bit */
215 memcpy_toio(comm_base, (void *)&tmp,
216 sizeof(struct acpi_pcct_shared_memory));
218 /* Copy the message to the PCC comm space */
219 comm_space_size = HCCS_PCC_SHARE_MEM_BYTES -
220 sizeof(struct acpi_pcct_shared_memory);
221 memcpy_toio(comm_space, (void *)desc, comm_space_size);
224 ret = mbox_send_message(cl_info->mbox_chan, &cmd);
226 dev_err(hdev->dev, "Send PCC mbox message failed, ret = %d.\n",
231 /* Wait for completion */
232 ret = hccs_check_chan_cmd_complete(hdev);
236 /* Copy response data */
237 memcpy_fromio((void *)desc, comm_space, comm_space_size);
238 fw_inner_head = &desc->rsp.fw_inner_head;
239 if (fw_inner_head->retStatus) {
240 dev_err(hdev->dev, "Execute PCC command failed, error code = %u.\n",
241 fw_inner_head->retStatus);
246 mbox_client_txdone(cl_info->mbox_chan, ret);
250 static void hccs_init_req_desc(struct hccs_desc *desc)
252 struct hccs_req_desc *req = &desc->req;
254 memset(desc, 0, sizeof(*desc));
255 req->req_head.module_code = HCCS_SERDES_MODULE_CODE;
258 static int hccs_get_dev_caps(struct hccs_dev *hdev)
260 struct hccs_desc desc;
263 hccs_init_req_desc(&desc);
264 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DEV_CAP, &desc);
266 dev_err(hdev->dev, "Get device capabilities failed, ret = %d.\n",
270 memcpy(&hdev->caps, desc.rsp.data, sizeof(hdev->caps));
275 static int hccs_query_chip_num_on_platform(struct hccs_dev *hdev)
277 struct hccs_desc desc;
280 hccs_init_req_desc(&desc);
281 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_CHIP_NUM, &desc);
283 dev_err(hdev->dev, "query system chip number failed, ret = %d.\n",
288 hdev->chip_num = *((u8 *)&desc.rsp.data);
289 if (!hdev->chip_num) {
290 dev_err(hdev->dev, "chip num obtained from firmware is zero.\n");
297 static int hccs_get_chip_info(struct hccs_dev *hdev,
298 struct hccs_chip_info *chip)
300 struct hccs_die_num_req_param *req_param;
301 struct hccs_desc desc;
304 hccs_init_req_desc(&desc);
305 req_param = (struct hccs_die_num_req_param *)desc.req.data;
306 req_param->chip_id = chip->chip_id;
307 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_NUM, &desc);
311 chip->die_num = *((u8 *)&desc.rsp.data);
316 static int hccs_query_chip_info_on_platform(struct hccs_dev *hdev)
318 struct hccs_chip_info *chip;
322 ret = hccs_query_chip_num_on_platform(hdev);
324 dev_err(hdev->dev, "query chip number on platform failed, ret = %d.\n",
329 hdev->chips = devm_kzalloc(hdev->dev,
330 hdev->chip_num * sizeof(struct hccs_chip_info),
333 dev_err(hdev->dev, "allocate all chips memory failed.\n");
337 for (idx = 0; idx < hdev->chip_num; idx++) {
338 chip = &hdev->chips[idx];
340 ret = hccs_get_chip_info(hdev, chip);
342 dev_err(hdev->dev, "get chip%u info failed, ret = %d.\n",
352 static int hccs_query_die_info_on_chip(struct hccs_dev *hdev, u8 chip_id,
353 u8 die_idx, struct hccs_die_info *die)
355 struct hccs_die_info_req_param *req_param;
356 struct hccs_die_info_rsp_data *rsp_data;
357 struct hccs_desc desc;
360 hccs_init_req_desc(&desc);
361 req_param = (struct hccs_die_info_req_param *)desc.req.data;
362 req_param->chip_id = chip_id;
363 req_param->die_idx = die_idx;
364 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_INFO, &desc);
368 rsp_data = (struct hccs_die_info_rsp_data *)desc.rsp.data;
369 die->die_id = rsp_data->die_id;
370 die->port_num = rsp_data->port_num;
371 die->min_port_id = rsp_data->min_port_id;
372 die->max_port_id = rsp_data->max_port_id;
373 if (die->min_port_id > die->max_port_id) {
374 dev_err(hdev->dev, "min port id(%u) > max port id(%u) on die_idx(%u).\n",
375 die->min_port_id, die->max_port_id, die_idx);
378 if (die->max_port_id > HCCS_DIE_MAX_PORT_ID) {
379 dev_err(hdev->dev, "max port id(%u) on die_idx(%u) is too big.\n",
380 die->max_port_id, die_idx);
387 static int hccs_query_all_die_info_on_platform(struct hccs_dev *hdev)
389 struct device *dev = hdev->dev;
390 struct hccs_chip_info *chip;
391 struct hccs_die_info *die;
395 for (i = 0; i < hdev->chip_num; i++) {
396 chip = &hdev->chips[i];
400 chip->dies = devm_kzalloc(hdev->dev,
401 chip->die_num * sizeof(struct hccs_die_info),
404 dev_err(dev, "allocate all dies memory on chip%u failed.\n",
409 for (j = 0; j < chip->die_num; j++) {
410 die = &chip->dies[j];
411 ret = hccs_query_die_info_on_chip(hdev, i, j, die);
413 dev_err(dev, "get die idx (%u) info on chip%u failed, ret = %d.\n",
424 static int hccs_get_bd_info(struct hccs_dev *hdev, u8 opcode,
425 struct hccs_desc *desc,
426 void *buf, size_t buf_len,
427 struct hccs_rsp_head *rsp_head)
429 struct hccs_rsp_head *head;
430 struct hccs_rsp_desc *rsp;
433 ret = hccs_pcc_cmd_send(hdev, opcode, desc);
438 head = &rsp->rsp_head;
439 if (head->data_len > buf_len) {
441 "buffer overflow (buf_len = %zu, data_len = %u)!\n",
442 buf_len, head->data_len);
446 memcpy(buf, rsp->data, head->data_len);
452 static int hccs_get_all_port_attr(struct hccs_dev *hdev,
453 struct hccs_die_info *die,
454 struct hccs_port_attr *attrs, u16 size)
456 struct hccs_die_comm_req_param *req_param;
457 struct hccs_req_head *req_head;
458 struct hccs_rsp_head rsp_head;
459 struct hccs_desc desc;
467 left_buf_len = sizeof(struct hccs_port_attr) * size;
468 start_id = die->min_port_id;
469 while (start_id <= die->max_port_id) {
470 hccs_init_req_desc(&desc);
471 req_head = &desc.req.req_head;
472 req_head->start_id = start_id;
473 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
474 req_param->chip_id = die->chip->chip_id;
475 req_param->die_id = die->die_id;
477 ret = hccs_get_bd_info(hdev, HCCS_GET_DIE_PORT_INFO, &desc,
478 buf + data_len, left_buf_len, &rsp_head);
481 "get the information of port%u on die%u failed, ret = %d.\n",
482 start_id, die->die_id, ret);
486 data_len += rsp_head.data_len;
487 left_buf_len -= rsp_head.data_len;
488 if (unlikely(rsp_head.next_id <= start_id)) {
490 "next port id (%u) is not greater than last start id (%u) on die%u.\n",
491 rsp_head.next_id, start_id, die->die_id);
494 start_id = rsp_head.next_id;
500 static int hccs_get_all_port_info_on_die(struct hccs_dev *hdev,
501 struct hccs_die_info *die)
503 struct hccs_port_attr *attrs;
504 struct hccs_port_info *port;
508 attrs = kcalloc(die->port_num, sizeof(struct hccs_port_attr),
513 ret = hccs_get_all_port_attr(hdev, die, attrs, die->port_num);
517 for (i = 0; i < die->port_num; i++) {
518 port = &die->ports[i];
519 port->port_id = attrs[i].port_id;
520 port->port_type = attrs[i].port_type;
521 port->lane_mode = attrs[i].lane_mode;
522 port->enable = attrs[i].enable;
531 static int hccs_query_all_port_info_on_platform(struct hccs_dev *hdev)
534 struct device *dev = hdev->dev;
535 struct hccs_chip_info *chip;
536 struct hccs_die_info *die;
540 for (i = 0; i < hdev->chip_num; i++) {
541 chip = &hdev->chips[i];
542 for (j = 0; j < chip->die_num; j++) {
543 die = &chip->dies[j];
547 die->ports = devm_kzalloc(dev,
548 die->port_num * sizeof(struct hccs_port_info),
551 dev_err(dev, "allocate ports memory on chip%u/die%u failed.\n",
556 ret = hccs_get_all_port_info_on_die(hdev, die);
558 dev_err(dev, "get all port info on chip%u/die%u failed, ret = %d.\n",
559 i, die->die_id, ret);
568 static int hccs_get_hw_info(struct hccs_dev *hdev)
572 ret = hccs_query_chip_info_on_platform(hdev);
574 dev_err(hdev->dev, "query chip info on platform failed, ret = %d.\n",
579 ret = hccs_query_all_die_info_on_platform(hdev);
581 dev_err(hdev->dev, "query all die info on platform failed, ret = %d.\n",
586 ret = hccs_query_all_port_info_on_platform(hdev);
588 dev_err(hdev->dev, "query all port info on platform failed, ret = %d.\n",
596 static int hccs_query_port_link_status(struct hccs_dev *hdev,
597 const struct hccs_port_info *port,
598 struct hccs_link_status *link_status)
600 const struct hccs_die_info *die = port->die;
601 const struct hccs_chip_info *chip = die->chip;
602 struct hccs_port_comm_req_param *req_param;
603 struct hccs_desc desc;
606 hccs_init_req_desc(&desc);
607 req_param = (struct hccs_port_comm_req_param *)desc.req.data;
608 req_param->chip_id = chip->chip_id;
609 req_param->die_id = die->die_id;
610 req_param->port_id = port->port_id;
611 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_LINK_STATUS, &desc);
614 "get port link status info failed, ret = %d.\n", ret);
618 *link_status = *((struct hccs_link_status *)desc.rsp.data);
623 static int hccs_query_port_crc_err_cnt(struct hccs_dev *hdev,
624 const struct hccs_port_info *port,
627 const struct hccs_die_info *die = port->die;
628 const struct hccs_chip_info *chip = die->chip;
629 struct hccs_port_comm_req_param *req_param;
630 struct hccs_desc desc;
633 hccs_init_req_desc(&desc);
634 req_param = (struct hccs_port_comm_req_param *)desc.req.data;
635 req_param->chip_id = chip->chip_id;
636 req_param->die_id = die->die_id;
637 req_param->port_id = port->port_id;
638 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_PORT_CRC_ERR_CNT, &desc);
641 "get port crc error count failed, ret = %d.\n", ret);
645 memcpy(crc_err_cnt, &desc.rsp.data, sizeof(u64));
650 static int hccs_get_die_all_link_status(struct hccs_dev *hdev,
651 const struct hccs_die_info *die,
654 struct hccs_die_comm_req_param *req_param;
655 struct hccs_desc desc;
658 if (die->port_num == 0) {
663 hccs_init_req_desc(&desc);
664 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
665 req_param->chip_id = die->chip->chip_id;
666 req_param->die_id = die->die_id;
667 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LINK_STA, &desc);
670 "get link status of all ports failed on die%u, ret = %d.\n",
675 *all_linked = *((u8 *)&desc.rsp.data);
680 static int hccs_get_die_all_port_lane_status(struct hccs_dev *hdev,
681 const struct hccs_die_info *die,
684 struct hccs_die_comm_req_param *req_param;
685 struct hccs_desc desc;
688 if (die->port_num == 0) {
693 hccs_init_req_desc(&desc);
694 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
695 req_param->chip_id = die->chip->chip_id;
696 req_param->die_id = die->die_id;
697 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_LANE_STA, &desc);
699 dev_err(hdev->dev, "get lane status of all ports failed on die%u, ret = %d.\n",
704 *full_lane = *((u8 *)&desc.rsp.data);
709 static int hccs_get_die_total_crc_err_cnt(struct hccs_dev *hdev,
710 const struct hccs_die_info *die,
711 u64 *total_crc_err_cnt)
713 struct hccs_die_comm_req_param *req_param;
714 struct hccs_desc desc;
717 if (die->port_num == 0) {
718 *total_crc_err_cnt = 0;
722 hccs_init_req_desc(&desc);
723 req_param = (struct hccs_die_comm_req_param *)desc.req.data;
724 req_param->chip_id = die->chip->chip_id;
725 req_param->die_id = die->die_id;
726 ret = hccs_pcc_cmd_send(hdev, HCCS_GET_DIE_PORTS_CRC_ERR_CNT, &desc);
728 dev_err(hdev->dev, "get crc error count sum failed on die%u, ret = %d.\n",
733 memcpy(total_crc_err_cnt, &desc.rsp.data, sizeof(u64));
738 static ssize_t hccs_show(struct kobject *k, struct attribute *attr, char *buf)
740 struct kobj_attribute *kobj_attr;
742 kobj_attr = container_of(attr, struct kobj_attribute, attr);
744 return kobj_attr->show(k, kobj_attr, buf);
747 static const struct sysfs_ops hccs_comm_ops = {
751 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
754 const struct hccs_port_info *port = kobj_to_port_info(kobj);
756 return sysfs_emit(buf, "HCCS-v%u\n", port->port_type);
758 static struct kobj_attribute hccs_type_attr = __ATTR_RO(type);
760 static ssize_t lane_mode_show(struct kobject *kobj, struct kobj_attribute *attr,
763 const struct hccs_port_info *port = kobj_to_port_info(kobj);
765 return sysfs_emit(buf, "x%u\n", port->lane_mode);
767 static struct kobj_attribute lane_mode_attr = __ATTR_RO(lane_mode);
769 static ssize_t enable_show(struct kobject *kobj,
770 struct kobj_attribute *attr, char *buf)
772 const struct hccs_port_info *port = kobj_to_port_info(kobj);
774 return sysfs_emit(buf, "%u\n", port->enable);
776 static struct kobj_attribute port_enable_attr = __ATTR_RO(enable);
778 static ssize_t cur_lane_num_show(struct kobject *kobj,
779 struct kobj_attribute *attr, char *buf)
781 const struct hccs_port_info *port = kobj_to_port_info(kobj);
782 struct hccs_dev *hdev = port->die->chip->hdev;
783 struct hccs_link_status link_status = {0};
786 mutex_lock(&hdev->lock);
787 ret = hccs_query_port_link_status(hdev, port, &link_status);
788 mutex_unlock(&hdev->lock);
792 return sysfs_emit(buf, "%u\n", link_status.lane_num);
794 static struct kobj_attribute cur_lane_num_attr = __ATTR_RO(cur_lane_num);
796 static ssize_t link_fsm_show(struct kobject *kobj,
797 struct kobj_attribute *attr, char *buf)
799 const struct hccs_port_info *port = kobj_to_port_info(kobj);
800 struct hccs_dev *hdev = port->die->chip->hdev;
801 struct hccs_link_status link_status = {0};
806 {HCCS_PORT_RESET, "reset"},
807 {HCCS_PORT_SETUP, "setup"},
808 {HCCS_PORT_CONFIG, "config"},
809 {HCCS_PORT_READY, "link-up"},
811 const char *link_fsm_str = "unknown";
815 mutex_lock(&hdev->lock);
816 ret = hccs_query_port_link_status(hdev, port, &link_status);
817 mutex_unlock(&hdev->lock);
821 for (i = 0; i < ARRAY_SIZE(link_fsm_map); i++) {
822 if (link_fsm_map[i].link_fsm == link_status.link_fsm) {
823 link_fsm_str = link_fsm_map[i].str;
828 return sysfs_emit(buf, "%s\n", link_fsm_str);
830 static struct kobj_attribute link_fsm_attr = __ATTR_RO(link_fsm);
832 static ssize_t lane_mask_show(struct kobject *kobj,
833 struct kobj_attribute *attr, char *buf)
835 const struct hccs_port_info *port = kobj_to_port_info(kobj);
836 struct hccs_dev *hdev = port->die->chip->hdev;
837 struct hccs_link_status link_status = {0};
840 mutex_lock(&hdev->lock);
841 ret = hccs_query_port_link_status(hdev, port, &link_status);
842 mutex_unlock(&hdev->lock);
846 return sysfs_emit(buf, "0x%x\n", link_status.lane_mask);
848 static struct kobj_attribute lane_mask_attr = __ATTR_RO(lane_mask);
850 static ssize_t crc_err_cnt_show(struct kobject *kobj,
851 struct kobj_attribute *attr, char *buf)
853 const struct hccs_port_info *port = kobj_to_port_info(kobj);
854 struct hccs_dev *hdev = port->die->chip->hdev;
858 mutex_lock(&hdev->lock);
859 ret = hccs_query_port_crc_err_cnt(hdev, port, &crc_err_cnt);
860 mutex_unlock(&hdev->lock);
864 return sysfs_emit(buf, "%llu\n", crc_err_cnt);
866 static struct kobj_attribute crc_err_cnt_attr = __ATTR_RO(crc_err_cnt);
868 static struct attribute *hccs_port_default_attrs[] = {
869 &hccs_type_attr.attr,
870 &lane_mode_attr.attr,
871 &port_enable_attr.attr,
872 &cur_lane_num_attr.attr,
874 &lane_mask_attr.attr,
875 &crc_err_cnt_attr.attr,
878 ATTRIBUTE_GROUPS(hccs_port_default);
880 static const struct kobj_type hccs_port_type = {
881 .sysfs_ops = &hccs_comm_ops,
882 .default_groups = hccs_port_default_groups,
885 static ssize_t all_linked_on_die_show(struct kobject *kobj,
886 struct kobj_attribute *attr, char *buf)
888 const struct hccs_die_info *die = kobj_to_die_info(kobj);
889 struct hccs_dev *hdev = die->chip->hdev;
893 mutex_lock(&hdev->lock);
894 ret = hccs_get_die_all_link_status(hdev, die, &all_linked);
895 mutex_unlock(&hdev->lock);
899 return sysfs_emit(buf, "%u\n", all_linked);
901 static struct kobj_attribute all_linked_on_die_attr =
902 __ATTR(all_linked, 0444, all_linked_on_die_show, NULL);
904 static ssize_t linked_full_lane_on_die_show(struct kobject *kobj,
905 struct kobj_attribute *attr,
908 const struct hccs_die_info *die = kobj_to_die_info(kobj);
909 struct hccs_dev *hdev = die->chip->hdev;
913 mutex_lock(&hdev->lock);
914 ret = hccs_get_die_all_port_lane_status(hdev, die, &full_lane);
915 mutex_unlock(&hdev->lock);
919 return sysfs_emit(buf, "%u\n", full_lane);
921 static struct kobj_attribute linked_full_lane_on_die_attr =
922 __ATTR(linked_full_lane, 0444, linked_full_lane_on_die_show, NULL);
924 static ssize_t crc_err_cnt_sum_on_die_show(struct kobject *kobj,
925 struct kobj_attribute *attr,
928 const struct hccs_die_info *die = kobj_to_die_info(kobj);
929 struct hccs_dev *hdev = die->chip->hdev;
930 u64 total_crc_err_cnt;
933 mutex_lock(&hdev->lock);
934 ret = hccs_get_die_total_crc_err_cnt(hdev, die, &total_crc_err_cnt);
935 mutex_unlock(&hdev->lock);
939 return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
941 static struct kobj_attribute crc_err_cnt_sum_on_die_attr =
942 __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_die_show, NULL);
944 static struct attribute *hccs_die_default_attrs[] = {
945 &all_linked_on_die_attr.attr,
946 &linked_full_lane_on_die_attr.attr,
947 &crc_err_cnt_sum_on_die_attr.attr,
950 ATTRIBUTE_GROUPS(hccs_die_default);
952 static const struct kobj_type hccs_die_type = {
953 .sysfs_ops = &hccs_comm_ops,
954 .default_groups = hccs_die_default_groups,
957 static ssize_t all_linked_on_chip_show(struct kobject *kobj,
958 struct kobj_attribute *attr, char *buf)
960 const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
961 struct hccs_dev *hdev = chip->hdev;
962 const struct hccs_die_info *die;
967 mutex_lock(&hdev->lock);
968 for (i = 0; i < chip->die_num; i++) {
969 die = &chip->dies[i];
970 ret = hccs_get_die_all_link_status(hdev, die, &tmp);
972 mutex_unlock(&hdev->lock);
975 if (tmp != all_linked) {
980 mutex_unlock(&hdev->lock);
982 return sysfs_emit(buf, "%u\n", all_linked);
984 static struct kobj_attribute all_linked_on_chip_attr =
985 __ATTR(all_linked, 0444, all_linked_on_chip_show, NULL);
987 static ssize_t linked_full_lane_on_chip_show(struct kobject *kobj,
988 struct kobj_attribute *attr,
991 const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
992 struct hccs_dev *hdev = chip->hdev;
993 const struct hccs_die_info *die;
998 mutex_lock(&hdev->lock);
999 for (i = 0; i < chip->die_num; i++) {
1000 die = &chip->dies[i];
1001 ret = hccs_get_die_all_port_lane_status(hdev, die, &tmp);
1003 mutex_unlock(&hdev->lock);
1006 if (tmp != full_lane) {
1011 mutex_unlock(&hdev->lock);
1013 return sysfs_emit(buf, "%u\n", full_lane);
1015 static struct kobj_attribute linked_full_lane_on_chip_attr =
1016 __ATTR(linked_full_lane, 0444, linked_full_lane_on_chip_show, NULL);
1018 static ssize_t crc_err_cnt_sum_on_chip_show(struct kobject *kobj,
1019 struct kobj_attribute *attr,
1022 const struct hccs_chip_info *chip = kobj_to_chip_info(kobj);
1023 u64 crc_err_cnt, total_crc_err_cnt = 0;
1024 struct hccs_dev *hdev = chip->hdev;
1025 const struct hccs_die_info *die;
1029 mutex_lock(&hdev->lock);
1030 for (i = 0; i < chip->die_num; i++) {
1031 die = &chip->dies[i];
1032 ret = hccs_get_die_total_crc_err_cnt(hdev, die, &crc_err_cnt);
1034 mutex_unlock(&hdev->lock);
1038 total_crc_err_cnt += crc_err_cnt;
1040 mutex_unlock(&hdev->lock);
1042 return sysfs_emit(buf, "%llu\n", total_crc_err_cnt);
1044 static struct kobj_attribute crc_err_cnt_sum_on_chip_attr =
1045 __ATTR(crc_err_cnt, 0444, crc_err_cnt_sum_on_chip_show, NULL);
1047 static struct attribute *hccs_chip_default_attrs[] = {
1048 &all_linked_on_chip_attr.attr,
1049 &linked_full_lane_on_chip_attr.attr,
1050 &crc_err_cnt_sum_on_chip_attr.attr,
1053 ATTRIBUTE_GROUPS(hccs_chip_default);
1055 static const struct kobj_type hccs_chip_type = {
1056 .sysfs_ops = &hccs_comm_ops,
1057 .default_groups = hccs_chip_default_groups,
1060 static void hccs_remove_die_dir(struct hccs_die_info *die)
1062 struct hccs_port_info *port;
1065 for (i = 0; i < die->port_num; i++) {
1066 port = &die->ports[i];
1067 if (port->dir_created)
1068 kobject_put(&port->kobj);
1071 kobject_put(&die->kobj);
1074 static void hccs_remove_chip_dir(struct hccs_chip_info *chip)
1076 struct hccs_die_info *die;
1079 for (i = 0; i < chip->die_num; i++) {
1080 die = &chip->dies[i];
1081 if (die->dir_created)
1082 hccs_remove_die_dir(die);
1085 kobject_put(&chip->kobj);
1088 static void hccs_remove_topo_dirs(struct hccs_dev *hdev)
1092 for (i = 0; i < hdev->chip_num; i++)
1093 hccs_remove_chip_dir(&hdev->chips[i]);
1096 static int hccs_create_hccs_dir(struct hccs_dev *hdev,
1097 struct hccs_die_info *die,
1098 struct hccs_port_info *port)
1102 ret = kobject_init_and_add(&port->kobj, &hccs_port_type,
1103 &die->kobj, "hccs%d", port->port_id);
1105 kobject_put(&port->kobj);
1112 static int hccs_create_die_dir(struct hccs_dev *hdev,
1113 struct hccs_chip_info *chip,
1114 struct hccs_die_info *die)
1116 struct hccs_port_info *port;
1120 ret = kobject_init_and_add(&die->kobj, &hccs_die_type,
1121 &chip->kobj, "die%d", die->die_id);
1123 kobject_put(&die->kobj);
1127 for (i = 0; i < die->port_num; i++) {
1128 port = &die->ports[i];
1129 ret = hccs_create_hccs_dir(hdev, die, port);
1131 dev_err(hdev->dev, "create hccs%d dir failed.\n",
1135 port->dir_created = true;
1140 hccs_remove_die_dir(die);
1145 static int hccs_create_chip_dir(struct hccs_dev *hdev,
1146 struct hccs_chip_info *chip)
1148 struct hccs_die_info *die;
1152 ret = kobject_init_and_add(&chip->kobj, &hccs_chip_type,
1153 &hdev->dev->kobj, "chip%d", chip->chip_id);
1155 kobject_put(&chip->kobj);
1159 for (id = 0; id < chip->die_num; id++) {
1160 die = &chip->dies[id];
1161 ret = hccs_create_die_dir(hdev, chip, die);
1164 die->dir_created = true;
1169 hccs_remove_chip_dir(chip);
1174 static int hccs_create_topo_dirs(struct hccs_dev *hdev)
1176 struct hccs_chip_info *chip;
1180 for (id = 0; id < hdev->chip_num; id++) {
1181 chip = &hdev->chips[id];
1182 ret = hccs_create_chip_dir(hdev, chip);
1184 dev_err(hdev->dev, "init chip%d dir failed!\n", id);
1191 for (k = 0; k < id; k++)
1192 hccs_remove_chip_dir(&hdev->chips[k]);
1197 static int hccs_probe(struct platform_device *pdev)
1199 struct acpi_device *acpi_dev;
1200 struct hccs_dev *hdev;
1203 if (acpi_disabled) {
1204 dev_err(&pdev->dev, "acpi is disabled.\n");
1207 acpi_dev = ACPI_COMPANION(&pdev->dev);
1211 hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
1214 hdev->acpi_dev = acpi_dev;
1215 hdev->dev = &pdev->dev;
1216 platform_set_drvdata(pdev, hdev);
1218 mutex_init(&hdev->lock);
1219 rc = hccs_get_pcc_chan_id(hdev);
1222 rc = hccs_register_pcc_channel(hdev);
1226 rc = hccs_get_dev_caps(hdev);
1228 goto unregister_pcc_chan;
1230 rc = hccs_get_hw_info(hdev);
1232 goto unregister_pcc_chan;
1234 rc = hccs_create_topo_dirs(hdev);
1236 goto unregister_pcc_chan;
1240 unregister_pcc_chan:
1241 hccs_unregister_pcc_channel(hdev);
1246 static int hccs_remove(struct platform_device *pdev)
1248 struct hccs_dev *hdev = platform_get_drvdata(pdev);
1250 hccs_remove_topo_dirs(hdev);
1251 hccs_unregister_pcc_channel(hdev);
1256 static const struct acpi_device_id hccs_acpi_match[] = {
1260 MODULE_DEVICE_TABLE(acpi, hccs_acpi_match);
1262 static struct platform_driver hccs_driver = {
1263 .probe = hccs_probe,
1264 .remove = hccs_remove,
1266 .name = "kunpeng_hccs",
1267 .acpi_match_table = hccs_acpi_match,
1271 module_platform_driver(hccs_driver);
1273 MODULE_DESCRIPTION("Kunpeng SoC HCCS driver");
1274 MODULE_LICENSE("GPL");
1275 MODULE_AUTHOR("Huisong Li <lihuisong@huawei.com>");