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