1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ZynqMP R5 Remote Processor driver 4 * 5 */ 6 7 #include <dt-bindings/power/xlnx-zynqmp-power.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/firmware/xlnx-zynqmp.h> 10 #include <linux/kernel.h> 11 #include <linux/mailbox_client.h> 12 #include <linux/mailbox/zynqmp-ipi-message.h> 13 #include <linux/module.h> 14 #include <linux/of_address.h> 15 #include <linux/of_platform.h> 16 #include <linux/of_reserved_mem.h> 17 #include <linux/platform_device.h> 18 #include <linux/remoteproc.h> 19 20 #include "remoteproc_internal.h" 21 22 /* IPI buffer MAX length */ 23 #define IPI_BUF_LEN_MAX 32U 24 25 /* RX mailbox client buffer max length */ 26 #define MBOX_CLIENT_BUF_MAX (IPI_BUF_LEN_MAX + \ 27 sizeof(struct zynqmp_ipi_message)) 28 29 #define RSC_TBL_XLNX_MAGIC ((uint32_t)'x' << 24 | (uint32_t)'a' << 16 | \ 30 (uint32_t)'m' << 8 | (uint32_t)'p') 31 32 /* 33 * settings for RPU cluster mode which 34 * reflects possible values of xlnx,cluster-mode dt-property 35 */ 36 enum zynqmp_r5_cluster_mode { 37 SPLIT_MODE = 0, /* When cores run as separate processor */ 38 LOCKSTEP_MODE = 1, /* cores execute same code in lockstep,clk-for-clk */ 39 SINGLE_CPU_MODE = 2, /* core0 is held in reset and only core1 runs */ 40 }; 41 42 /** 43 * struct mem_bank_data - Memory Bank description 44 * 45 * @addr: Start address of memory bank 46 * @da: device address 47 * @size: Size of Memory bank 48 * @pm_domain_id: Power-domains id of memory bank for firmware to turn on/off 49 * @bank_name: name of the bank for remoteproc framework 50 */ 51 struct mem_bank_data { 52 phys_addr_t addr; 53 u32 da; 54 size_t size; 55 u32 pm_domain_id; 56 char *bank_name; 57 }; 58 59 /** 60 * struct zynqmp_sram_bank - sram bank description 61 * 62 * @sram_res: sram address region information 63 * @da: device address of sram 64 */ 65 struct zynqmp_sram_bank { 66 struct resource sram_res; 67 u32 da; 68 }; 69 70 /** 71 * struct mbox_info - mailbox channel data 72 * 73 * @rx_mc_buf: to copy data from mailbox rx channel 74 * @tx_mc_buf: to copy data to mailbox tx channel 75 * @r5_core: this mailbox's corresponding r5_core pointer 76 * @mbox_work: schedule work after receiving data from mailbox 77 * @mbox_cl: mailbox client 78 * @tx_chan: mailbox tx channel 79 * @rx_chan: mailbox rx channel 80 */ 81 struct mbox_info { 82 unsigned char rx_mc_buf[MBOX_CLIENT_BUF_MAX]; 83 unsigned char tx_mc_buf[MBOX_CLIENT_BUF_MAX]; 84 struct zynqmp_r5_core *r5_core; 85 struct work_struct mbox_work; 86 struct mbox_client mbox_cl; 87 struct mbox_chan *tx_chan; 88 struct mbox_chan *rx_chan; 89 }; 90 91 /** 92 * struct rsc_tbl_data - resource table metadata 93 * 94 * Platform specific data structure used to sync resource table address. 95 * It's important to maintain order and size of each field on remote side. 96 * 97 * @version: version of data structure 98 * @magic_num: 32-bit magic number. 99 * @comp_magic_num: complement of above magic number 100 * @rsc_tbl_size: resource table size 101 * @rsc_tbl: resource table address 102 */ 103 struct rsc_tbl_data { 104 const int version; 105 const u32 magic_num; 106 const u32 comp_magic_num; 107 const u32 rsc_tbl_size; 108 const uintptr_t rsc_tbl; 109 } __packed; 110 111 /* 112 * Hardcoded TCM bank values. This will stay in driver to maintain backward 113 * compatibility with device-tree that does not have TCM information. 114 */ 115 static const struct mem_bank_data zynqmp_tcm_banks_split[] = { 116 {0xffe00000UL, 0x0, 0x10000UL, PD_R5_0_ATCM, "atcm0"}, /* TCM 64KB each */ 117 {0xffe20000UL, 0x20000, 0x10000UL, PD_R5_0_BTCM, "btcm0"}, 118 {0xffe90000UL, 0x0, 0x10000UL, PD_R5_1_ATCM, "atcm1"}, 119 {0xffeb0000UL, 0x20000, 0x10000UL, PD_R5_1_BTCM, "btcm1"}, 120 }; 121 122 /* In lockstep mode cluster uses each 64KB TCM from second core as well */ 123 static const struct mem_bank_data zynqmp_tcm_banks_lockstep[] = { 124 {0xffe00000UL, 0x0, 0x10000UL, PD_R5_0_ATCM, "atcm0"}, /* TCM 64KB each */ 125 {0xffe20000UL, 0x20000, 0x10000UL, PD_R5_0_BTCM, "btcm0"}, 126 {0xffe10000UL, 0x10000, 0x10000UL, PD_R5_1_ATCM, "atcm1"}, 127 {0xffe30000UL, 0x30000, 0x10000UL, PD_R5_1_BTCM, "btcm1"}, 128 }; 129 130 /** 131 * struct zynqmp_r5_core - remoteproc core's internal data 132 * 133 * @rsc_tbl_va: resource table virtual address 134 * @sram: Array of sram memories assigned to this core 135 * @num_sram: number of sram for this core 136 * @dev: device of RPU instance 137 * @np: device node of RPU instance 138 * @tcm_bank_count: number TCM banks accessible to this RPU 139 * @tcm_banks: array of each TCM bank data 140 * @rproc: rproc handle 141 * @rsc_tbl_size: resource table size retrieved from remote 142 * @pm_domain_id: RPU CPU power domain id 143 * @ipi: pointer to mailbox information 144 */ 145 struct zynqmp_r5_core { 146 void __iomem *rsc_tbl_va; 147 struct zynqmp_sram_bank *sram; 148 int num_sram; 149 struct device *dev; 150 struct device_node *np; 151 int tcm_bank_count; 152 struct mem_bank_data **tcm_banks; 153 struct rproc *rproc; 154 u32 rsc_tbl_size; 155 u32 pm_domain_id; 156 struct mbox_info *ipi; 157 }; 158 159 /** 160 * struct zynqmp_r5_cluster - remoteproc cluster's internal data 161 * 162 * @dev: r5f subsystem cluster device node 163 * @mode: cluster mode of type zynqmp_r5_cluster_mode 164 * @core_count: number of r5 cores used for this cluster mode 165 * @r5_cores: Array of pointers pointing to r5 core 166 */ 167 struct zynqmp_r5_cluster { 168 struct device *dev; 169 enum zynqmp_r5_cluster_mode mode; 170 int core_count; 171 struct zynqmp_r5_core **r5_cores; 172 }; 173 174 /** 175 * event_notified_idr_cb() - callback for vq_interrupt per notifyid 176 * @id: rproc->notify id 177 * @ptr: pointer to idr private data 178 * @data: data passed to idr_for_each callback 179 * 180 * Pass notification to remoteproc virtio 181 * 182 * Return: 0. having return is to satisfy the idr_for_each() function 183 * pointer input argument requirement. 184 **/ 185 static int event_notified_idr_cb(int id, void *ptr, void *data) 186 { 187 struct rproc *rproc = data; 188 189 if (rproc_vq_interrupt(rproc, id) == IRQ_NONE) 190 dev_dbg(&rproc->dev, "data not found for vqid=%d\n", id); 191 192 return 0; 193 } 194 195 /** 196 * handle_event_notified() - remoteproc notification work function 197 * @work: pointer to the work structure 198 * 199 * It checks each registered remoteproc notify IDs. 200 */ 201 static void handle_event_notified(struct work_struct *work) 202 { 203 struct mbox_info *ipi; 204 struct rproc *rproc; 205 206 ipi = container_of(work, struct mbox_info, mbox_work); 207 rproc = ipi->r5_core->rproc; 208 209 /* 210 * We only use IPI for interrupt. The RPU firmware side may or may 211 * not write the notifyid when it trigger IPI. 212 * And thus, we scan through all the registered notifyids and 213 * find which one is valid to get the message. 214 * Even if message from firmware is NULL, we attempt to get vqid 215 */ 216 idr_for_each(&rproc->notifyids, event_notified_idr_cb, rproc); 217 } 218 219 /** 220 * zynqmp_r5_mb_rx_cb() - receive channel mailbox callback 221 * @cl: mailbox client 222 * @msg: message pointer 223 * 224 * Receive data from ipi buffer, ack interrupt and then 225 * it will schedule the R5 notification work. 226 */ 227 static void zynqmp_r5_mb_rx_cb(struct mbox_client *cl, void *msg) 228 { 229 struct zynqmp_ipi_message *ipi_msg, *buf_msg; 230 struct mbox_info *ipi; 231 size_t len; 232 233 ipi = container_of(cl, struct mbox_info, mbox_cl); 234 235 /* copy data from ipi buffer to r5_core if IPI is buffered. */ 236 ipi_msg = (struct zynqmp_ipi_message *)msg; 237 if (ipi_msg) { 238 buf_msg = (struct zynqmp_ipi_message *)ipi->rx_mc_buf; 239 len = ipi_msg->len; 240 if (len > IPI_BUF_LEN_MAX) { 241 dev_warn(cl->dev, "msg size exceeded than %d\n", 242 IPI_BUF_LEN_MAX); 243 len = IPI_BUF_LEN_MAX; 244 } 245 buf_msg->len = len; 246 memcpy(buf_msg->data, ipi_msg->data, len); 247 } 248 249 /* received and processed interrupt ack */ 250 if (mbox_send_message(ipi->rx_chan, NULL) < 0) 251 dev_err(cl->dev, "ack failed to mbox rx_chan\n"); 252 253 schedule_work(&ipi->mbox_work); 254 } 255 256 /** 257 * zynqmp_r5_setup_mbox() - Setup mailboxes related properties 258 * this is used for each individual R5 core 259 * 260 * @cdev: child node device 261 * 262 * Function to setup mailboxes related properties 263 * return : NULL if failed else pointer to mbox_info 264 */ 265 static struct mbox_info *zynqmp_r5_setup_mbox(struct device *cdev) 266 { 267 struct mbox_client *mbox_cl; 268 struct mbox_info *ipi; 269 270 if (!of_property_present(dev_of_node(cdev), "mboxes") || 271 !of_property_present(dev_of_node(cdev), "mbox-names")) 272 return NULL; 273 274 ipi = kzalloc_obj(*ipi); 275 if (!ipi) 276 return NULL; 277 278 mbox_cl = &ipi->mbox_cl; 279 mbox_cl->rx_callback = zynqmp_r5_mb_rx_cb; 280 mbox_cl->tx_block = false; 281 mbox_cl->knows_txdone = false; 282 mbox_cl->tx_done = NULL; 283 mbox_cl->dev = cdev; 284 285 /* Request TX and RX channels */ 286 ipi->tx_chan = mbox_request_channel_byname(mbox_cl, "tx"); 287 if (IS_ERR(ipi->tx_chan)) { 288 ipi->tx_chan = NULL; 289 kfree(ipi); 290 dev_warn(cdev, "mbox tx channel request failed\n"); 291 return NULL; 292 } 293 294 ipi->rx_chan = mbox_request_channel_byname(mbox_cl, "rx"); 295 if (IS_ERR(ipi->rx_chan)) { 296 mbox_free_channel(ipi->tx_chan); 297 ipi->rx_chan = NULL; 298 ipi->tx_chan = NULL; 299 kfree(ipi); 300 dev_warn(cdev, "mbox rx channel request failed\n"); 301 return NULL; 302 } 303 304 INIT_WORK(&ipi->mbox_work, handle_event_notified); 305 306 return ipi; 307 } 308 309 static void zynqmp_r5_free_mbox(struct mbox_info *ipi) 310 { 311 if (!ipi) 312 return; 313 314 if (ipi->tx_chan) { 315 mbox_free_channel(ipi->tx_chan); 316 ipi->tx_chan = NULL; 317 } 318 319 if (ipi->rx_chan) { 320 mbox_free_channel(ipi->rx_chan); 321 ipi->rx_chan = NULL; 322 } 323 324 kfree(ipi); 325 } 326 327 /* 328 * zynqmp_r5_core_kick() - kick a firmware if mbox is provided 329 * @rproc: r5 core's corresponding rproc structure 330 * @vqid: virtqueue ID 331 */ 332 static void zynqmp_r5_rproc_kick(struct rproc *rproc, int vqid) 333 { 334 struct zynqmp_r5_core *r5_core = rproc->priv; 335 struct device *dev = r5_core->dev; 336 struct zynqmp_ipi_message *mb_msg; 337 struct mbox_info *ipi; 338 int ret; 339 340 ipi = r5_core->ipi; 341 if (!ipi) 342 return; 343 344 mb_msg = (struct zynqmp_ipi_message *)ipi->tx_mc_buf; 345 memcpy(mb_msg->data, &vqid, sizeof(vqid)); 346 mb_msg->len = sizeof(vqid); 347 ret = mbox_send_message(ipi->tx_chan, mb_msg); 348 if (ret < 0) 349 dev_warn(dev, "failed to send message\n"); 350 } 351 352 /* 353 * zynqmp_r5_rproc_start() 354 * @rproc: single R5 core's corresponding rproc instance 355 * 356 * Start R5 Core from designated boot address. 357 * 358 * return 0 on success, otherwise non-zero value on failure 359 */ 360 static int zynqmp_r5_rproc_start(struct rproc *rproc) 361 { 362 struct zynqmp_r5_core *r5_core = rproc->priv; 363 enum rpu_boot_mem bootmem; 364 int ret; 365 366 /* 367 * The exception vector pointers (EVP) refer to the base-address of 368 * exception vectors (for reset, IRQ, FIQ, etc). The reset-vector 369 * starts at the base-address and subsequent vectors are on 4-byte 370 * boundaries. 371 * 372 * Exception vectors can start either from 0x0000_0000 (LOVEC) or 373 * from 0xFFFF_0000 (HIVEC) which is mapped in the OCM (On-Chip Memory) 374 * 375 * Usually firmware will put Exception vectors at LOVEC. 376 * 377 * It is not recommend that you change the exception vector. 378 * Changing the EVP to HIVEC will result in increased interrupt latency 379 * and jitter. Also, if the OCM is secured and the Cortex-R5F processor 380 * is non-secured, then the Cortex-R5F processor cannot access the 381 * HIVEC exception vectors in the OCM. 382 */ 383 bootmem = (rproc->bootaddr >= 0xFFFC0000) ? 384 PM_RPU_BOOTMEM_HIVEC : PM_RPU_BOOTMEM_LOVEC; 385 386 dev_dbg(r5_core->dev, "RPU boot addr 0x%llx from %s.", rproc->bootaddr, 387 bootmem == PM_RPU_BOOTMEM_HIVEC ? "OCM" : "TCM"); 388 389 /* Request node before starting RPU core if new version of API is supported */ 390 if (zynqmp_pm_feature(PM_REQUEST_NODE) > 1) { 391 ret = zynqmp_pm_request_node(r5_core->pm_domain_id, 392 ZYNQMP_PM_CAPABILITY_ACCESS, 0, 393 ZYNQMP_PM_REQUEST_ACK_BLOCKING); 394 if (ret < 0) { 395 dev_err(r5_core->dev, "failed to request 0x%x", 396 r5_core->pm_domain_id); 397 return ret; 398 } 399 } 400 401 ret = zynqmp_pm_request_wake(r5_core->pm_domain_id, 1, 402 bootmem, ZYNQMP_PM_REQUEST_ACK_NO); 403 if (ret) 404 dev_err(r5_core->dev, 405 "failed to start RPU = 0x%x\n", r5_core->pm_domain_id); 406 return ret; 407 } 408 409 /* 410 * zynqmp_r5_rproc_stop() 411 * @rproc: single R5 core's corresponding rproc instance 412 * 413 * Power down R5 Core. 414 * 415 * return 0 on success, otherwise non-zero value on failure 416 */ 417 static int zynqmp_r5_rproc_stop(struct rproc *rproc) 418 { 419 struct zynqmp_r5_core *r5_core = rproc->priv; 420 int ret; 421 422 /* Use release node API to stop core if new version of API is supported */ 423 if (zynqmp_pm_feature(PM_RELEASE_NODE) > 1) { 424 ret = zynqmp_pm_release_node(r5_core->pm_domain_id); 425 if (ret) 426 dev_err(r5_core->dev, "failed to stop remoteproc RPU %d\n", ret); 427 return ret; 428 } 429 430 /* 431 * Check expected version of EEMI call before calling it. This avoids 432 * any error or warning prints from firmware as it is expected that fw 433 * doesn't support it. 434 */ 435 if (zynqmp_pm_feature(PM_FORCE_POWERDOWN) != 1) { 436 dev_dbg(r5_core->dev, "EEMI interface %d ver 1 not supported\n", 437 PM_FORCE_POWERDOWN); 438 return -EOPNOTSUPP; 439 } 440 441 /* maintain force pwr down for backward compatibility */ 442 ret = zynqmp_pm_force_pwrdwn(r5_core->pm_domain_id, 443 ZYNQMP_PM_REQUEST_ACK_BLOCKING); 444 if (ret) 445 dev_err(r5_core->dev, "core force power down failed\n"); 446 447 return ret; 448 } 449 450 /* 451 * zynqmp_r5_mem_region_map() 452 * @rproc: single R5 core's corresponding rproc instance 453 * @mem: mem descriptor to map reserved memory-regions 454 * 455 * Callback to map va for memory-region's carveout. 456 * 457 * return 0 on success, otherwise non-zero value on failure 458 */ 459 static int zynqmp_r5_mem_region_map(struct rproc *rproc, 460 struct rproc_mem_entry *mem) 461 { 462 void __iomem *va; 463 464 va = ioremap_wc(mem->dma, mem->len); 465 if (IS_ERR_OR_NULL(va)) 466 return -ENOMEM; 467 468 mem->va = (void *)va; 469 470 return 0; 471 } 472 473 /* 474 * zynqmp_r5_rproc_mem_unmap 475 * @rproc: single R5 core's corresponding rproc instance 476 * @mem: mem entry to unmap 477 * 478 * Unmap memory-region carveout 479 * 480 * return: always returns 0 481 */ 482 static int zynqmp_r5_mem_region_unmap(struct rproc *rproc, 483 struct rproc_mem_entry *mem) 484 { 485 iounmap((void __iomem *)mem->va); 486 return 0; 487 } 488 489 /* 490 * add_mem_regions_carveout() 491 * @rproc: single R5 core's corresponding rproc instance 492 * 493 * Construct rproc mem carveouts from memory-region property nodes 494 * 495 * return 0 on success, otherwise non-zero value on failure 496 */ 497 static int add_mem_regions_carveout(struct rproc *rproc) 498 { 499 struct rproc_mem_entry *rproc_mem; 500 struct zynqmp_r5_core *r5_core; 501 int i = 0; 502 503 r5_core = rproc->priv; 504 505 /* Register associated reserved memory regions */ 506 while (1) { 507 int err; 508 struct resource res; 509 510 err = of_reserved_mem_region_to_resource(r5_core->np, i, &res); 511 if (err) 512 return 0; 513 514 if (strstarts(res.name, "vdev0buffer")) { 515 /* Init reserved memory for vdev buffer */ 516 rproc_mem = rproc_of_resm_mem_entry_init(&rproc->dev, i, 517 resource_size(&res), 518 res.start, 519 "vdev0buffer"); 520 } else { 521 /* Register associated reserved memory regions */ 522 rproc_mem = rproc_mem_entry_init(&rproc->dev, NULL, 523 (dma_addr_t)res.start, 524 resource_size(&res), res.start, 525 zynqmp_r5_mem_region_map, 526 zynqmp_r5_mem_region_unmap, 527 "%.*s", 528 strchrnul(res.name, '@') - res.name, 529 res.name); 530 } 531 532 if (!rproc_mem) 533 return -ENOMEM; 534 535 rproc_add_carveout(rproc, rproc_mem); 536 rproc_coredump_add_segment(rproc, res.start, resource_size(&res)); 537 538 dev_dbg(&rproc->dev, "reserved mem carveout %pR\n", &res); 539 i++; 540 } 541 } 542 543 static int add_sram_carveouts(struct rproc *rproc) 544 { 545 struct zynqmp_r5_core *r5_core = rproc->priv; 546 struct rproc_mem_entry *rproc_mem; 547 struct zynqmp_sram_bank *sram; 548 dma_addr_t dma_addr; 549 size_t len; 550 int da, i; 551 552 for (i = 0; i < r5_core->num_sram; i++) { 553 sram = &r5_core->sram[i]; 554 555 dma_addr = (dma_addr_t)sram->sram_res.start; 556 557 len = resource_size(&sram->sram_res); 558 da = sram->da; 559 560 rproc_mem = rproc_mem_entry_init(&rproc->dev, NULL, 561 dma_addr, 562 len, da, 563 zynqmp_r5_mem_region_map, 564 zynqmp_r5_mem_region_unmap, 565 sram->sram_res.name); 566 if (!rproc_mem) { 567 dev_err(&rproc->dev, "failed to add sram %s da=0x%x, size=0x%lx", 568 sram->sram_res.name, da, len); 569 return -ENOMEM; 570 } 571 572 rproc_add_carveout(rproc, rproc_mem); 573 rproc_coredump_add_segment(rproc, da, len); 574 575 dev_dbg(&rproc->dev, "sram carveout %s addr=%llx, da=0x%x, size=0x%lx", 576 sram->sram_res.name, dma_addr, da, len); 577 } 578 579 return 0; 580 } 581 582 /* 583 * tcm_mem_unmap() 584 * @rproc: single R5 core's corresponding rproc instance 585 * @mem: tcm mem entry to unmap 586 * 587 * Unmap TCM banks when powering down R5 core. 588 * 589 * return always 0 590 */ 591 static int tcm_mem_unmap(struct rproc *rproc, struct rproc_mem_entry *mem) 592 { 593 iounmap((void __iomem *)mem->va); 594 595 return 0; 596 } 597 598 /* 599 * tcm_mem_map() 600 * @rproc: single R5 core's corresponding rproc instance 601 * @mem: tcm memory entry descriptor 602 * 603 * Given TCM bank entry, this func setup virtual address for TCM bank 604 * remoteproc carveout. It also takes care of va to da address translation 605 * 606 * return 0 on success, otherwise non-zero value on failure 607 */ 608 static int tcm_mem_map(struct rproc *rproc, 609 struct rproc_mem_entry *mem) 610 { 611 void __iomem *va; 612 613 va = ioremap_wc(mem->dma, mem->len); 614 if (IS_ERR_OR_NULL(va)) 615 return -ENOMEM; 616 617 /* Update memory entry va */ 618 mem->va = (void *)va; 619 620 /* clear TCMs */ 621 memset_io(va, 0, mem->len); 622 623 return 0; 624 } 625 626 /* 627 * add_tcm_banks() 628 * @rproc: single R5 core's corresponding rproc instance 629 * 630 * allocate and add remoteproc carveout for TCM memory 631 * 632 * return 0 on success, otherwise non-zero value on failure 633 */ 634 static int add_tcm_banks(struct rproc *rproc) 635 { 636 struct rproc_mem_entry *rproc_mem; 637 struct zynqmp_r5_core *r5_core; 638 int i, num_banks, ret; 639 phys_addr_t bank_addr; 640 struct device *dev; 641 u32 pm_domain_id; 642 size_t bank_size; 643 char *bank_name; 644 u32 da; 645 646 r5_core = rproc->priv; 647 dev = r5_core->dev; 648 num_banks = r5_core->tcm_bank_count; 649 650 /* 651 * Power-on Each 64KB TCM, 652 * register its address space, map and unmap functions 653 * and add carveouts accordingly 654 */ 655 for (i = 0; i < num_banks; i++) { 656 bank_addr = r5_core->tcm_banks[i]->addr; 657 da = r5_core->tcm_banks[i]->da; 658 bank_name = r5_core->tcm_banks[i]->bank_name; 659 bank_size = r5_core->tcm_banks[i]->size; 660 pm_domain_id = r5_core->tcm_banks[i]->pm_domain_id; 661 662 ret = zynqmp_pm_request_node(pm_domain_id, 663 ZYNQMP_PM_CAPABILITY_ACCESS, 0, 664 ZYNQMP_PM_REQUEST_ACK_BLOCKING); 665 if (ret < 0) { 666 dev_err(dev, "failed to turn on TCM 0x%x", pm_domain_id); 667 goto release_tcm; 668 } 669 670 dev_dbg(dev, "TCM carveout %s addr=%llx, da=0x%x, size=0x%lx", 671 bank_name, bank_addr, da, bank_size); 672 673 /* 674 * In DETACHED state firmware is already running so no need to 675 * request add TCM registers. However, request TCM PD node to let 676 * platform management firmware know that TCM is in use. 677 */ 678 if (rproc->state == RPROC_DETACHED) 679 continue; 680 681 rproc_mem = rproc_mem_entry_init(dev, NULL, bank_addr, 682 bank_size, da, 683 tcm_mem_map, tcm_mem_unmap, 684 bank_name); 685 if (!rproc_mem) { 686 ret = -ENOMEM; 687 zynqmp_pm_release_node(pm_domain_id); 688 goto release_tcm; 689 } 690 691 rproc_add_carveout(rproc, rproc_mem); 692 rproc_coredump_add_segment(rproc, da, bank_size); 693 } 694 695 return 0; 696 697 release_tcm: 698 /* If failed, Turn off all TCM banks turned on before */ 699 for (i--; i >= 0; i--) { 700 pm_domain_id = r5_core->tcm_banks[i]->pm_domain_id; 701 zynqmp_pm_release_node(pm_domain_id); 702 } 703 return ret; 704 } 705 706 /* 707 * zynqmp_r5_parse_fw() 708 * @rproc: single R5 core's corresponding rproc instance 709 * @fw: ptr to firmware to be loaded onto r5 core 710 * 711 * get resource table if available 712 * 713 * return 0 on success, otherwise non-zero value on failure 714 */ 715 static int zynqmp_r5_parse_fw(struct rproc *rproc, const struct firmware *fw) 716 { 717 int ret; 718 719 ret = rproc_elf_load_rsc_table(rproc, fw); 720 if (ret == -EINVAL) { 721 /* 722 * resource table only required for IPC. 723 * if not present, this is not necessarily an error; 724 * for example, loading r5 hello world application 725 * so simply inform user and keep going. 726 */ 727 dev_info(&rproc->dev, "no resource table found.\n"); 728 ret = 0; 729 } 730 return ret; 731 } 732 733 /** 734 * zynqmp_r5_rproc_prepare() - prepare core to boot/attach 735 * adds carveouts for TCM bank and reserved memory regions 736 * 737 * @rproc: Device node of each rproc 738 * 739 * Return: 0 for success else < 0 error code 740 */ 741 static int zynqmp_r5_rproc_prepare(struct rproc *rproc) 742 { 743 int ret; 744 745 ret = add_tcm_banks(rproc); 746 if (ret) { 747 dev_err(&rproc->dev, "failed to get TCM banks, err %d\n", ret); 748 return ret; 749 } 750 751 ret = add_mem_regions_carveout(rproc); 752 if (ret) { 753 dev_err(&rproc->dev, "failed to get reserve mem regions %d\n", ret); 754 return ret; 755 } 756 757 ret = add_sram_carveouts(rproc); 758 if (ret) { 759 dev_err(&rproc->dev, "failed to get sram carveout %d\n", ret); 760 return ret; 761 } 762 763 return 0; 764 } 765 766 /** 767 * zynqmp_r5_rproc_unprepare() - programming sequence after stop/detach. 768 * Turns off TCM banks using power-domain id 769 * 770 * @rproc: Device node of each rproc 771 * 772 * Return: always 0 773 */ 774 static int zynqmp_r5_rproc_unprepare(struct rproc *rproc) 775 { 776 struct zynqmp_r5_core *r5_core; 777 u32 pm_domain_id; 778 int i; 779 780 r5_core = rproc->priv; 781 782 for (i = 0; i < r5_core->tcm_bank_count; i++) { 783 pm_domain_id = r5_core->tcm_banks[i]->pm_domain_id; 784 if (zynqmp_pm_release_node(pm_domain_id)) 785 dev_warn(r5_core->dev, 786 "can't turn off TCM bank 0x%x", pm_domain_id); 787 } 788 789 return 0; 790 } 791 792 static struct resource_table *zynqmp_r5_get_loaded_rsc_table(struct rproc *rproc, 793 size_t *size) 794 { 795 struct zynqmp_r5_core *r5_core; 796 797 r5_core = rproc->priv; 798 799 *size = r5_core->rsc_tbl_size; 800 801 return (struct resource_table *)r5_core->rsc_tbl_va; 802 } 803 804 static int zynqmp_r5_get_rsc_table_va(struct zynqmp_r5_core *r5_core) 805 { 806 struct resource_table *rsc_tbl_addr; 807 struct device *dev = r5_core->dev; 808 struct rsc_tbl_data *rsc_data_va; 809 struct resource res_mem; 810 int ret; 811 812 /* 813 * It is expected from remote processor firmware to provide resource 814 * table address via struct rsc_tbl_data data structure. 815 * Start address of first entry under "memory-region" property list 816 * contains that data structure which holds resource table address, size 817 * and some magic number to validate correct resource table entry. 818 */ 819 ret = of_reserved_mem_region_to_resource(r5_core->np, 0, &res_mem); 820 if (ret) { 821 dev_err(dev, "failed to get memory-region resource addr\n"); 822 return -EINVAL; 823 } 824 825 rsc_data_va = (struct rsc_tbl_data *)ioremap_wc(res_mem.start, 826 sizeof(struct rsc_tbl_data)); 827 if (!rsc_data_va) { 828 dev_err(dev, "failed to map resource table data address\n"); 829 return -EIO; 830 } 831 832 /* 833 * If RSC_TBL_XLNX_MAGIC number and its complement isn't found then 834 * do not consider resource table address valid and don't attach 835 */ 836 if (rsc_data_va->magic_num != RSC_TBL_XLNX_MAGIC || 837 rsc_data_va->comp_magic_num != ~RSC_TBL_XLNX_MAGIC) { 838 dev_dbg(dev, "invalid magic number, won't attach\n"); 839 return -EINVAL; 840 } 841 842 r5_core->rsc_tbl_va = ioremap_wc(rsc_data_va->rsc_tbl, 843 rsc_data_va->rsc_tbl_size); 844 if (!r5_core->rsc_tbl_va) { 845 dev_err(dev, "failed to get resource table va\n"); 846 return -EINVAL; 847 } 848 849 rsc_tbl_addr = (struct resource_table *)r5_core->rsc_tbl_va; 850 851 /* 852 * As of now resource table version 1 is expected. Don't fail to attach 853 * but warn users about it. 854 */ 855 if (rsc_tbl_addr->ver != 1) 856 dev_warn(dev, "unexpected resource table version %d\n", 857 rsc_tbl_addr->ver); 858 859 r5_core->rsc_tbl_size = rsc_data_va->rsc_tbl_size; 860 861 iounmap((void __iomem *)rsc_data_va); 862 863 return 0; 864 } 865 866 static int zynqmp_r5_attach(struct rproc *rproc) 867 { 868 dev_dbg(&rproc->dev, "rproc %d attached\n", rproc->index); 869 870 return 0; 871 } 872 873 static int zynqmp_r5_detach(struct rproc *rproc) 874 { 875 /* 876 * Generate last notification to remote after clearing virtio flag. 877 * Remote can avoid polling on virtio reset flag if kick is generated 878 * during detach by host and check virtio reset flag on kick interrupt. 879 */ 880 zynqmp_r5_rproc_kick(rproc, 0); 881 882 return 0; 883 } 884 885 static const struct rproc_ops zynqmp_r5_rproc_ops = { 886 .prepare = zynqmp_r5_rproc_prepare, 887 .unprepare = zynqmp_r5_rproc_unprepare, 888 .start = zynqmp_r5_rproc_start, 889 .stop = zynqmp_r5_rproc_stop, 890 .load = rproc_elf_load_segments, 891 .parse_fw = zynqmp_r5_parse_fw, 892 .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table, 893 .sanity_check = rproc_elf_sanity_check, 894 .get_boot_addr = rproc_elf_get_boot_addr, 895 .kick = zynqmp_r5_rproc_kick, 896 .get_loaded_rsc_table = zynqmp_r5_get_loaded_rsc_table, 897 .attach = zynqmp_r5_attach, 898 .detach = zynqmp_r5_detach, 899 }; 900 901 /** 902 * zynqmp_r5_add_rproc_core() - Add core data to framework. 903 * Allocate and add struct rproc object for each r5f core 904 * This is called for each individual r5f core 905 * 906 * @cdev: Device node of each r5 core 907 * 908 * Return: zynqmp_r5_core object for success else error code pointer 909 */ 910 static struct zynqmp_r5_core *zynqmp_r5_add_rproc_core(struct device *cdev) 911 { 912 struct zynqmp_r5_core *r5_core; 913 struct rproc *r5_rproc; 914 int ret; 915 916 /* Set up DMA mask */ 917 ret = dma_set_coherent_mask(cdev, DMA_BIT_MASK(32)); 918 if (ret) 919 return ERR_PTR(ret); 920 921 /* Allocate remoteproc instance */ 922 r5_rproc = rproc_alloc(cdev, dev_name(cdev), 923 &zynqmp_r5_rproc_ops, 924 NULL, sizeof(struct zynqmp_r5_core)); 925 if (!r5_rproc) { 926 dev_err(cdev, "failed to allocate memory for rproc instance\n"); 927 return ERR_PTR(-ENOMEM); 928 } 929 930 rproc_coredump_set_elf_info(r5_rproc, ELFCLASS32, EM_ARM); 931 932 r5_rproc->recovery_disabled = true; 933 r5_rproc->has_iommu = false; 934 r5_rproc->auto_boot = false; 935 r5_core = r5_rproc->priv; 936 r5_core->dev = cdev; 937 r5_core->np = dev_of_node(cdev); 938 if (!r5_core->np) { 939 dev_err(cdev, "can't get device node for r5 core\n"); 940 ret = -EINVAL; 941 goto free_rproc; 942 } 943 944 /* Add R5 remoteproc core */ 945 ret = rproc_add(r5_rproc); 946 if (ret) { 947 dev_err(cdev, "failed to add r5 remoteproc\n"); 948 goto free_rproc; 949 } 950 951 /* 952 * If firmware is already available in the memory then move rproc state 953 * to DETACHED. Firmware can be preloaded via debugger or by any other 954 * agent (processors) in the system. 955 * If firmware isn't available in the memory and resource table isn't 956 * found, then rproc state remains OFFLINE. 957 */ 958 if (!zynqmp_r5_get_rsc_table_va(r5_core)) 959 r5_rproc->state = RPROC_DETACHED; 960 961 r5_core->rproc = r5_rproc; 962 return r5_core; 963 964 free_rproc: 965 rproc_free(r5_rproc); 966 return ERR_PTR(ret); 967 } 968 969 static int zynqmp_r5_get_sram_banks(struct zynqmp_r5_core *r5_core) 970 { 971 struct device_node *np = r5_core->np; 972 struct device *dev = r5_core->dev; 973 struct zynqmp_sram_bank *sram; 974 struct device_node *sram_np; 975 int num_sram, i, ret; 976 u64 abs_addr, size; 977 978 /* "sram" is optional property. Do not fail, if unavailable. */ 979 if (!of_property_present(r5_core->np, "sram")) 980 return 0; 981 982 num_sram = of_property_count_elems_of_size(np, "sram", sizeof(phandle)); 983 if (num_sram <= 0) { 984 dev_err(dev, "Invalid sram property, ret = %d\n", 985 num_sram); 986 return -EINVAL; 987 } 988 989 sram = devm_kcalloc(dev, num_sram, 990 sizeof(struct zynqmp_sram_bank), GFP_KERNEL); 991 if (!sram) 992 return -ENOMEM; 993 994 for (i = 0; i < num_sram; i++) { 995 sram_np = of_parse_phandle(np, "sram", i); 996 if (!sram_np) { 997 dev_err(dev, "failed to get sram %d phandle\n", i); 998 return -EINVAL; 999 } 1000 1001 if (!of_device_is_available(sram_np)) { 1002 dev_err(dev, "sram device not available\n"); 1003 ret = -EINVAL; 1004 goto fail_sram_get; 1005 } 1006 1007 ret = of_address_to_resource(sram_np, 0, &sram[i].sram_res); 1008 if (ret) { 1009 dev_err(dev, "addr to res failed\n"); 1010 goto fail_sram_get; 1011 } 1012 1013 /* Get SRAM device address */ 1014 ret = of_property_read_reg(sram_np, 0, &abs_addr, &size); 1015 if (ret) { 1016 dev_err(dev, "failed to get reg property\n"); 1017 goto fail_sram_get; 1018 } 1019 1020 sram[i].da = (u32)abs_addr; 1021 1022 of_node_put(sram_np); 1023 1024 dev_dbg(dev, "sram %d: name=%s, addr=0x%llx, da=0x%x, size=0x%llx\n", 1025 i, sram[i].sram_res.name, sram[i].sram_res.start, 1026 sram[i].da, resource_size(&sram[i].sram_res)); 1027 } 1028 1029 r5_core->sram = sram; 1030 r5_core->num_sram = num_sram; 1031 1032 return 0; 1033 1034 fail_sram_get: 1035 of_node_put(sram_np); 1036 1037 return ret; 1038 } 1039 1040 static int zynqmp_r5_get_tcm_node_from_dt(struct zynqmp_r5_cluster *cluster) 1041 { 1042 int i, j, tcm_bank_count, ret, tcm_pd_idx, pd_count; 1043 struct of_phandle_args out_args; 1044 struct zynqmp_r5_core *r5_core; 1045 struct platform_device *cpdev; 1046 struct mem_bank_data *tcm; 1047 struct device_node *np; 1048 struct resource *res; 1049 u64 abs_addr, size; 1050 struct device *dev; 1051 1052 for (i = 0; i < cluster->core_count; i++) { 1053 r5_core = cluster->r5_cores[i]; 1054 dev = r5_core->dev; 1055 np = r5_core->np; 1056 1057 pd_count = of_count_phandle_with_args(np, "power-domains", 1058 "#power-domain-cells"); 1059 1060 if (pd_count <= 0) { 1061 dev_err(dev, "invalid power-domains property, %d\n", pd_count); 1062 return -EINVAL; 1063 } 1064 1065 /* First entry in power-domains list is for r5 core, rest for TCM. */ 1066 tcm_bank_count = pd_count - 1; 1067 1068 if (tcm_bank_count <= 0) { 1069 dev_err(dev, "invalid TCM count %d\n", tcm_bank_count); 1070 return -EINVAL; 1071 } 1072 1073 r5_core->tcm_banks = devm_kcalloc(dev, tcm_bank_count, 1074 sizeof(struct mem_bank_data *), 1075 GFP_KERNEL); 1076 if (!r5_core->tcm_banks) 1077 return -ENOMEM; 1078 1079 r5_core->tcm_bank_count = tcm_bank_count; 1080 for (j = 0, tcm_pd_idx = 1; j < tcm_bank_count; j++, tcm_pd_idx++) { 1081 tcm = devm_kzalloc(dev, sizeof(struct mem_bank_data), 1082 GFP_KERNEL); 1083 if (!tcm) 1084 return -ENOMEM; 1085 1086 r5_core->tcm_banks[j] = tcm; 1087 1088 /* Get power-domains id of TCM. */ 1089 ret = of_parse_phandle_with_args(np, "power-domains", 1090 "#power-domain-cells", 1091 tcm_pd_idx, &out_args); 1092 if (ret) { 1093 dev_err(r5_core->dev, 1094 "failed to get tcm %d pm domain, ret %d\n", 1095 tcm_pd_idx, ret); 1096 return ret; 1097 } 1098 tcm->pm_domain_id = out_args.args[0]; 1099 of_node_put(out_args.np); 1100 1101 /* Get TCM address without translation. */ 1102 ret = of_property_read_reg(np, j, &abs_addr, &size); 1103 if (ret) { 1104 dev_err(dev, "failed to get reg property\n"); 1105 return ret; 1106 } 1107 1108 /* 1109 * Remote processor can address only 32 bits 1110 * so convert 64-bits into 32-bits. This will discard 1111 * any unwanted upper 32-bits. 1112 */ 1113 tcm->da = (u32)abs_addr; 1114 tcm->size = (u32)size; 1115 1116 cpdev = to_platform_device(dev); 1117 res = platform_get_resource(cpdev, IORESOURCE_MEM, j); 1118 if (!res) { 1119 dev_err(dev, "failed to get tcm resource\n"); 1120 return -EINVAL; 1121 } 1122 1123 tcm->addr = (u32)res->start; 1124 tcm->bank_name = (char *)res->name; 1125 res = devm_request_mem_region(dev, tcm->addr, tcm->size, 1126 tcm->bank_name); 1127 if (!res) { 1128 dev_err(dev, "failed to request tcm resource\n"); 1129 return -EINVAL; 1130 } 1131 } 1132 } 1133 1134 return 0; 1135 } 1136 1137 /** 1138 * zynqmp_r5_get_tcm_node() - Get TCM info 1139 * Ideally this function should parse tcm node and store information 1140 * in r5_core instance. For now, Hardcoded TCM information is used. 1141 * This approach is used as TCM bindings for system-dt is being developed 1142 * 1143 * @cluster: pointer to zynqmp_r5_cluster type object 1144 * 1145 * Return: 0 for success and < 0 error code for failure. 1146 */ 1147 static int zynqmp_r5_get_tcm_node(struct zynqmp_r5_cluster *cluster) 1148 { 1149 const struct mem_bank_data *zynqmp_tcm_banks; 1150 struct device *dev = cluster->dev; 1151 struct zynqmp_r5_core *r5_core; 1152 int tcm_bank_count, tcm_node; 1153 int i, j; 1154 1155 if (cluster->mode == SPLIT_MODE) { 1156 zynqmp_tcm_banks = zynqmp_tcm_banks_split; 1157 tcm_bank_count = ARRAY_SIZE(zynqmp_tcm_banks_split); 1158 } else { 1159 zynqmp_tcm_banks = zynqmp_tcm_banks_lockstep; 1160 tcm_bank_count = ARRAY_SIZE(zynqmp_tcm_banks_lockstep); 1161 } 1162 1163 /* count per core tcm banks */ 1164 tcm_bank_count = tcm_bank_count / cluster->core_count; 1165 1166 /* 1167 * r5 core 0 will use all of TCM banks in lockstep mode. 1168 * In split mode, r5 core0 will use 128k and r5 core1 will use another 1169 * 128k. Assign TCM banks to each core accordingly 1170 */ 1171 tcm_node = 0; 1172 for (i = 0; i < cluster->core_count; i++) { 1173 r5_core = cluster->r5_cores[i]; 1174 r5_core->tcm_banks = devm_kcalloc(dev, tcm_bank_count, 1175 sizeof(struct mem_bank_data *), 1176 GFP_KERNEL); 1177 if (!r5_core->tcm_banks) 1178 return -ENOMEM; 1179 1180 for (j = 0; j < tcm_bank_count; j++) { 1181 /* 1182 * Use pre-defined TCM reg values. 1183 * Eventually this should be replaced by values 1184 * parsed from dts. 1185 */ 1186 r5_core->tcm_banks[j] = 1187 (struct mem_bank_data *)&zynqmp_tcm_banks[tcm_node]; 1188 tcm_node++; 1189 } 1190 1191 r5_core->tcm_bank_count = tcm_bank_count; 1192 } 1193 1194 return 0; 1195 } 1196 1197 /* 1198 * zynqmp_r5_core_init() 1199 * Create and initialize zynqmp_r5_core type object 1200 * 1201 * @cluster: pointer to zynqmp_r5_cluster type object 1202 * @fw_reg_val: value expected by firmware to configure RPU cluster mode 1203 * @tcm_mode: value expected by fw to configure TCM mode (lockstep or split) 1204 * 1205 * Return: 0 for success and error code for failure. 1206 */ 1207 static int zynqmp_r5_core_init(struct zynqmp_r5_cluster *cluster, 1208 enum rpu_oper_mode fw_reg_val, 1209 enum rpu_tcm_comb tcm_mode) 1210 { 1211 struct device *dev = cluster->dev; 1212 struct zynqmp_r5_core *r5_core; 1213 int ret = -EINVAL, i; 1214 1215 r5_core = cluster->r5_cores[0]; 1216 1217 /* Maintain backward compatibility for zynqmp by using hardcode TCM address. */ 1218 if (of_property_present(r5_core->np, "reg")) 1219 ret = zynqmp_r5_get_tcm_node_from_dt(cluster); 1220 else if (device_is_compatible(dev, "xlnx,zynqmp-r5fss")) 1221 ret = zynqmp_r5_get_tcm_node(cluster); 1222 1223 if (ret) { 1224 dev_err(dev, "can't get tcm, err %d\n", ret); 1225 return ret; 1226 } 1227 1228 for (i = 0; i < cluster->core_count; i++) { 1229 r5_core = cluster->r5_cores[i]; 1230 1231 /* Initialize r5 cores with power-domains parsed from dts */ 1232 ret = of_property_read_u32_index(r5_core->np, "power-domains", 1233 1, &r5_core->pm_domain_id); 1234 if (ret) { 1235 dev_err(dev, "failed to get power-domains property\n"); 1236 return ret; 1237 } 1238 1239 ret = zynqmp_pm_set_rpu_mode(r5_core->pm_domain_id, fw_reg_val); 1240 if (ret < 0) { 1241 dev_err(r5_core->dev, "failed to set RPU mode\n"); 1242 return ret; 1243 } 1244 1245 if (of_property_present(dev_of_node(dev), "xlnx,tcm-mode") || 1246 device_is_compatible(dev, "xlnx,zynqmp-r5fss")) { 1247 ret = zynqmp_pm_set_tcm_config(r5_core->pm_domain_id, 1248 tcm_mode); 1249 if (ret < 0) { 1250 dev_err(r5_core->dev, "failed to configure TCM\n"); 1251 return ret; 1252 } 1253 } 1254 1255 ret = zynqmp_r5_get_sram_banks(r5_core); 1256 if (ret) 1257 return ret; 1258 } 1259 1260 return 0; 1261 } 1262 1263 /* 1264 * zynqmp_r5_cluster_init() 1265 * Create and initialize zynqmp_r5_cluster type object 1266 * 1267 * @cluster: pointer to zynqmp_r5_cluster type object 1268 * 1269 * Return: 0 for success and error code for failure. 1270 */ 1271 static int zynqmp_r5_cluster_init(struct zynqmp_r5_cluster *cluster) 1272 { 1273 enum zynqmp_r5_cluster_mode cluster_mode = LOCKSTEP_MODE; 1274 struct device *dev = cluster->dev; 1275 struct device_node *dev_node = dev_of_node(dev); 1276 struct platform_device *child_pdev; 1277 struct zynqmp_r5_core **r5_cores; 1278 enum rpu_oper_mode fw_reg_val; 1279 struct device **child_devs; 1280 enum rpu_tcm_comb tcm_mode; 1281 int core_count, ret, i; 1282 struct mbox_info *ipi; 1283 1284 ret = of_property_read_u32(dev_node, "xlnx,cluster-mode", &cluster_mode); 1285 1286 /* 1287 * on success returns 0, if not defined then returns -EINVAL, 1288 * In that case, default is LOCKSTEP mode. Other than that 1289 * returns relative error code < 0. 1290 */ 1291 if (ret != -EINVAL && ret != 0) { 1292 dev_err(dev, "Invalid xlnx,cluster-mode property\n"); 1293 return ret; 1294 } 1295 1296 /* 1297 * For now driver only supports split mode and lockstep mode. 1298 * fail driver probe if either of that is not set in dts. 1299 */ 1300 if (cluster_mode == LOCKSTEP_MODE) { 1301 fw_reg_val = PM_RPU_MODE_LOCKSTEP; 1302 } else if (cluster_mode == SPLIT_MODE) { 1303 fw_reg_val = PM_RPU_MODE_SPLIT; 1304 } else { 1305 dev_err(dev, "driver does not support cluster mode %d\n", cluster_mode); 1306 return -EINVAL; 1307 } 1308 1309 if (of_property_present(dev_node, "xlnx,tcm-mode")) { 1310 ret = of_property_read_u32(dev_node, "xlnx,tcm-mode", (u32 *)&tcm_mode); 1311 if (ret) 1312 return ret; 1313 } else if (device_is_compatible(dev, "xlnx,zynqmp-r5fss")) { 1314 if (cluster_mode == LOCKSTEP_MODE) 1315 tcm_mode = PM_RPU_TCM_COMB; 1316 else 1317 tcm_mode = PM_RPU_TCM_SPLIT; 1318 } else { 1319 tcm_mode = PM_RPU_TCM_COMB; 1320 } 1321 1322 /* 1323 * Number of cores is decided by number of child nodes of 1324 * r5f subsystem node in dts. 1325 * In split mode maximum two child nodes are expected. 1326 * However, only single core can be enabled too. 1327 * Driver can handle following configuration in split mode: 1328 * 1) core0 enabled, core1 disabled 1329 * 2) core0 disabled, core1 enabled 1330 * 3) core0 and core1 both are enabled. 1331 * For now, no more than two cores are expected per cluster 1332 * in split mode. 1333 * In lockstep mode if two child nodes are available, 1334 * only use first child node and consider it as core0 1335 * and ignore core1 dt node. 1336 */ 1337 core_count = of_get_available_child_count(dev_node); 1338 if (core_count == 0 || core_count > 2) { 1339 dev_err(dev, "Invalid number of r5 cores %d", core_count); 1340 return -EINVAL; 1341 } else if (cluster_mode == LOCKSTEP_MODE && core_count == 2) { 1342 dev_warn(dev, "Only r5 core0 will be used\n"); 1343 core_count = 1; 1344 } 1345 1346 child_devs = kzalloc_objs(struct device *, core_count); 1347 if (!child_devs) 1348 return -ENOMEM; 1349 1350 r5_cores = kzalloc_objs(struct zynqmp_r5_core *, core_count); 1351 if (!r5_cores) { 1352 kfree(child_devs); 1353 return -ENOMEM; 1354 } 1355 1356 i = 0; 1357 for_each_available_child_of_node_scoped(dev_node, child) { 1358 child_pdev = of_find_device_by_node(child); 1359 if (!child_pdev) { 1360 ret = -ENODEV; 1361 goto release_r5_cores; 1362 } 1363 1364 child_devs[i] = &child_pdev->dev; 1365 1366 /* create and add remoteproc instance of type struct rproc */ 1367 r5_cores[i] = zynqmp_r5_add_rproc_core(&child_pdev->dev); 1368 if (IS_ERR(r5_cores[i])) { 1369 ret = PTR_ERR(r5_cores[i]); 1370 r5_cores[i] = NULL; 1371 goto release_r5_cores; 1372 } 1373 1374 /* 1375 * If mailbox nodes are disabled using "status" property then 1376 * setting up mailbox channels will fail. 1377 */ 1378 ipi = zynqmp_r5_setup_mbox(&child_pdev->dev); 1379 if (ipi) { 1380 r5_cores[i]->ipi = ipi; 1381 ipi->r5_core = r5_cores[i]; 1382 } 1383 1384 /* 1385 * If two child nodes are available in dts in lockstep mode, 1386 * then ignore second child node. 1387 */ 1388 if (cluster_mode == LOCKSTEP_MODE) 1389 break; 1390 1391 i++; 1392 } 1393 1394 cluster->mode = cluster_mode; 1395 cluster->core_count = core_count; 1396 cluster->r5_cores = r5_cores; 1397 1398 ret = zynqmp_r5_core_init(cluster, fw_reg_val, tcm_mode); 1399 if (ret < 0) { 1400 dev_err(dev, "failed to init r5 core err %d\n", ret); 1401 cluster->core_count = 0; 1402 cluster->r5_cores = NULL; 1403 1404 /* 1405 * at this point rproc resources for each core are allocated. 1406 * adjust index to free resources in reverse order 1407 */ 1408 i = core_count - 1; 1409 goto release_r5_cores; 1410 } 1411 1412 kfree(child_devs); 1413 return 0; 1414 1415 release_r5_cores: 1416 while (i >= 0) { 1417 put_device(child_devs[i]); 1418 if (r5_cores[i]) { 1419 zynqmp_r5_free_mbox(r5_cores[i]->ipi); 1420 of_reserved_mem_device_release(r5_cores[i]->dev); 1421 rproc_del(r5_cores[i]->rproc); 1422 rproc_free(r5_cores[i]->rproc); 1423 } 1424 i--; 1425 } 1426 kfree(r5_cores); 1427 kfree(child_devs); 1428 return ret; 1429 } 1430 1431 static void zynqmp_r5_cluster_exit(void *data) 1432 { 1433 struct platform_device *pdev = data; 1434 struct zynqmp_r5_cluster *cluster; 1435 struct zynqmp_r5_core *r5_core; 1436 int i; 1437 1438 cluster = platform_get_drvdata(pdev); 1439 if (!cluster) 1440 return; 1441 1442 for (i = 0; i < cluster->core_count; i++) { 1443 r5_core = cluster->r5_cores[i]; 1444 zynqmp_r5_free_mbox(r5_core->ipi); 1445 iounmap(r5_core->rsc_tbl_va); 1446 of_reserved_mem_device_release(r5_core->dev); 1447 put_device(r5_core->dev); 1448 rproc_del(r5_core->rproc); 1449 rproc_free(r5_core->rproc); 1450 } 1451 1452 kfree(cluster->r5_cores); 1453 kfree(cluster); 1454 platform_set_drvdata(pdev, NULL); 1455 } 1456 1457 /* 1458 * zynqmp_r5_remoteproc_shutdown() 1459 * Follow shutdown sequence in case of kexec call. 1460 * 1461 * @pdev: domain platform device for cluster 1462 * 1463 * Return: None. 1464 */ 1465 static void zynqmp_r5_remoteproc_shutdown(struct platform_device *pdev) 1466 { 1467 const char *rproc_state_str = NULL; 1468 struct zynqmp_r5_cluster *cluster; 1469 struct zynqmp_r5_core *r5_core; 1470 struct rproc *rproc; 1471 int i, ret = 0; 1472 1473 cluster = platform_get_drvdata(pdev); 1474 1475 for (i = 0; i < cluster->core_count; i++) { 1476 r5_core = cluster->r5_cores[i]; 1477 rproc = r5_core->rproc; 1478 1479 if (rproc->state == RPROC_RUNNING) { 1480 ret = rproc_shutdown(rproc); 1481 rproc_state_str = "shutdown"; 1482 } else if (rproc->state == RPROC_ATTACHED) { 1483 ret = rproc_detach(rproc); 1484 rproc_state_str = "detach"; 1485 } else { 1486 ret = 0; 1487 } 1488 1489 if (ret) { 1490 dev_err(cluster->dev, "failed to %s rproc %d\n", 1491 rproc_state_str, rproc->index); 1492 } 1493 1494 zynqmp_r5_free_mbox(r5_core->ipi); 1495 } 1496 } 1497 1498 /* 1499 * zynqmp_r5_remoteproc_probe() 1500 * parse device-tree, initialize hardware and allocate required resources 1501 * and remoteproc ops 1502 * 1503 * @pdev: domain platform device for R5 cluster 1504 * 1505 * Return: 0 for success and < 0 for failure. 1506 */ 1507 static int zynqmp_r5_remoteproc_probe(struct platform_device *pdev) 1508 { 1509 struct zynqmp_r5_cluster *cluster; 1510 struct device *dev = &pdev->dev; 1511 int ret; 1512 1513 cluster = kzalloc_obj(*cluster); 1514 if (!cluster) 1515 return -ENOMEM; 1516 1517 cluster->dev = dev; 1518 1519 ret = devm_of_platform_populate(dev); 1520 if (ret) { 1521 dev_err_probe(dev, ret, "failed to populate platform dev\n"); 1522 kfree(cluster); 1523 return ret; 1524 } 1525 1526 /* wire in so each core can be cleaned up at driver remove */ 1527 platform_set_drvdata(pdev, cluster); 1528 1529 ret = zynqmp_r5_cluster_init(cluster); 1530 if (ret) { 1531 kfree(cluster); 1532 platform_set_drvdata(pdev, NULL); 1533 dev_err_probe(dev, ret, "Invalid r5f subsystem device tree\n"); 1534 return ret; 1535 } 1536 1537 ret = devm_add_action_or_reset(dev, zynqmp_r5_cluster_exit, pdev); 1538 if (ret) 1539 return ret; 1540 1541 return 0; 1542 } 1543 1544 /* Match table for OF platform binding */ 1545 static const struct of_device_id zynqmp_r5_remoteproc_match[] = { 1546 { .compatible = "xlnx,versal-net-r52fss", }, 1547 { .compatible = "xlnx,versal-r5fss", }, 1548 { .compatible = "xlnx,zynqmp-r5fss", }, 1549 { /* end of list */ }, 1550 }; 1551 MODULE_DEVICE_TABLE(of, zynqmp_r5_remoteproc_match); 1552 1553 static struct platform_driver zynqmp_r5_remoteproc_driver = { 1554 .probe = zynqmp_r5_remoteproc_probe, 1555 .driver = { 1556 .name = "zynqmp_r5_remoteproc", 1557 .of_match_table = zynqmp_r5_remoteproc_match, 1558 }, 1559 .shutdown = zynqmp_r5_remoteproc_shutdown, 1560 }; 1561 module_platform_driver(zynqmp_r5_remoteproc_driver); 1562 1563 MODULE_DESCRIPTION("Xilinx R5F remote processor driver"); 1564 MODULE_AUTHOR("Xilinx Inc."); 1565 MODULE_LICENSE("GPL"); 1566