1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Remote Processor Framework 4 * 5 * Copyright (C) 2011 Texas Instruments, Inc. 6 * Copyright (C) 2011 Google, Inc. 7 * 8 * Ohad Ben-Cohen <ohad@wizery.com> 9 * Brian Swetland <swetland@google.com> 10 * Mark Grosen <mgrosen@ti.com> 11 * Fernando Guzman Lugo <fernando.lugo@ti.com> 12 * Suman Anna <s-anna@ti.com> 13 * Robert Tivy <rtivy@ti.com> 14 * Armando Uribe De Leon <x0095078@ti.com> 15 */ 16 17 #define pr_fmt(fmt) "%s: " fmt, __func__ 18 19 #include <asm/byteorder.h> 20 #include <linux/delay.h> 21 #include <linux/device.h> 22 #include <linux/dma-mapping.h> 23 #include <linux/elf.h> 24 #include <linux/firmware.h> 25 #include <linux/idr.h> 26 #include <linux/iommu.h> 27 #include <linux/kernel.h> 28 #include <linux/module.h> 29 #include <linux/mutex.h> 30 #include <linux/of_platform.h> 31 #include <linux/panic_notifier.h> 32 #include <linux/platform_device.h> 33 #include <linux/rculist.h> 34 #include <linux/remoteproc.h> 35 #include <linux/slab.h> 36 #include <linux/string.h> 37 #include <linux/virtio_ring.h> 38 39 #include "remoteproc_internal.h" 40 41 #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL 42 43 static DEFINE_MUTEX(rproc_list_mutex); 44 static LIST_HEAD(rproc_list); 45 static struct notifier_block rproc_panic_nb; 46 47 typedef int (*rproc_handle_resource_t)(struct rproc *rproc, 48 void *, int offset, int avail); 49 50 static int rproc_alloc_carveout(struct rproc *rproc, 51 struct rproc_mem_entry *mem); 52 static int rproc_release_carveout(struct rproc *rproc, 53 struct rproc_mem_entry *mem); 54 55 /* Unique indices for remoteproc devices */ 56 static DEFINE_IDA(rproc_dev_index); 57 static struct workqueue_struct *rproc_recovery_wq; 58 59 static const char * const rproc_crash_names[] = { 60 [RPROC_MMUFAULT] = "mmufault", 61 [RPROC_WATCHDOG] = "watchdog", 62 [RPROC_FATAL_ERROR] = "fatal error", 63 }; 64 65 /* translate rproc_crash_type to string */ 66 static const char *rproc_crash_to_string(enum rproc_crash_type type) 67 { 68 if (type < ARRAY_SIZE(rproc_crash_names)) 69 return rproc_crash_names[type]; 70 return "unknown"; 71 } 72 73 /* 74 * This is the IOMMU fault handler we register with the IOMMU API 75 * (when relevant; not all remote processors access memory through 76 * an IOMMU). 77 * 78 * IOMMU core will invoke this handler whenever the remote processor 79 * will try to access an unmapped device address. 80 */ 81 static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev, 82 unsigned long iova, int flags, void *token) 83 { 84 struct rproc *rproc = token; 85 86 dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags); 87 88 rproc_report_crash(rproc, RPROC_MMUFAULT); 89 90 /* 91 * Let the iommu core know we're not really handling this fault; 92 * we just used it as a recovery trigger. 93 */ 94 return -ENOSYS; 95 } 96 97 static int rproc_enable_iommu(struct rproc *rproc) 98 { 99 struct iommu_domain *domain; 100 struct device *dev = rproc->dev.parent; 101 int ret; 102 103 if (!rproc->has_iommu) { 104 dev_dbg(dev, "iommu not present\n"); 105 return 0; 106 } 107 108 domain = iommu_paging_domain_alloc(dev); 109 if (IS_ERR(domain)) { 110 dev_err(dev, "can't alloc iommu domain\n"); 111 return PTR_ERR(domain); 112 } 113 114 iommu_set_fault_handler(domain, rproc_iommu_fault, rproc); 115 116 ret = iommu_attach_device(domain, dev); 117 if (ret) { 118 dev_err(dev, "can't attach iommu device: %d\n", ret); 119 goto free_domain; 120 } 121 122 rproc->domain = domain; 123 124 return 0; 125 126 free_domain: 127 iommu_domain_free(domain); 128 return ret; 129 } 130 131 static void rproc_disable_iommu(struct rproc *rproc) 132 { 133 struct iommu_domain *domain = rproc->domain; 134 struct device *dev = rproc->dev.parent; 135 136 if (!domain) 137 return; 138 139 iommu_detach_device(domain, dev); 140 iommu_domain_free(domain); 141 } 142 143 phys_addr_t rproc_va_to_pa(void *cpu_addr) 144 { 145 /* 146 * Return physical address according to virtual address location 147 * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent 148 * - in kernel: if region allocated in generic dma memory pool 149 */ 150 if (is_vmalloc_addr(cpu_addr)) { 151 return page_to_phys(vmalloc_to_page(cpu_addr)) + 152 offset_in_page(cpu_addr); 153 } 154 155 WARN_ON(!virt_addr_valid(cpu_addr)); 156 return virt_to_phys(cpu_addr); 157 } 158 159 /** 160 * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address 161 * @rproc: handle of a remote processor 162 * @da: remoteproc device address to translate 163 * @len: length of the memory region @da is pointing to 164 * @is_iomem: optional pointer filled in to indicate if @da is iomapped memory 165 * 166 * Some remote processors will ask us to allocate them physically contiguous 167 * memory regions (which we call "carveouts"), and map them to specific 168 * device addresses (which are hardcoded in the firmware). They may also have 169 * dedicated memory regions internal to the processors, and use them either 170 * exclusively or alongside carveouts. 171 * 172 * They may then ask us to copy objects into specific device addresses (e.g. 173 * code/data sections) or expose us certain symbols in other device address 174 * (e.g. their trace buffer). 175 * 176 * This function is a helper function with which we can go over the allocated 177 * carveouts and translate specific device addresses to kernel virtual addresses 178 * so we can access the referenced memory. This function also allows to perform 179 * translations on the internal remoteproc memory regions through a platform 180 * implementation specific da_to_va ops, if present. 181 * 182 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too, 183 * but only on kernel direct mapped RAM memory. Instead, we're just using 184 * here the output of the DMA API for the carveouts, which should be more 185 * correct. 186 * 187 * Return: a valid kernel address on success or NULL on failure 188 */ 189 void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem) 190 { 191 struct rproc_mem_entry *carveout; 192 void *ptr = NULL; 193 194 if (rproc->ops->da_to_va) { 195 ptr = rproc->ops->da_to_va(rproc, da, len, is_iomem); 196 if (ptr) 197 goto out; 198 } 199 200 list_for_each_entry(carveout, &rproc->carveouts, node) { 201 int offset = da - carveout->da; 202 203 /* Verify that carveout is allocated */ 204 if (!carveout->va) 205 continue; 206 207 /* try next carveout if da is too small */ 208 if (offset < 0) 209 continue; 210 211 /* try next carveout if da is too large */ 212 if (offset + len > carveout->len) 213 continue; 214 215 ptr = carveout->va + offset; 216 217 if (is_iomem) 218 *is_iomem = carveout->is_iomem; 219 220 break; 221 } 222 223 out: 224 return ptr; 225 } 226 EXPORT_SYMBOL(rproc_da_to_va); 227 228 /** 229 * rproc_find_carveout_by_name() - lookup the carveout region by a name 230 * @rproc: handle of a remote processor 231 * @name: carveout name to find (format string) 232 * @...: optional parameters matching @name string 233 * 234 * Platform driver has the capability to register some pre-allacoted carveout 235 * (physically contiguous memory regions) before rproc firmware loading and 236 * associated resource table analysis. These regions may be dedicated memory 237 * regions internal to the coprocessor or specified DDR region with specific 238 * attributes 239 * 240 * This function is a helper function with which we can go over the 241 * allocated carveouts and return associated region characteristics like 242 * coprocessor address, length or processor virtual address. 243 * 244 * Return: a valid pointer on carveout entry on success or NULL on failure. 245 */ 246 __printf(2, 3) 247 struct rproc_mem_entry * 248 rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...) 249 { 250 va_list args; 251 char _name[32]; 252 struct rproc_mem_entry *carveout, *mem = NULL; 253 254 if (!name) 255 return NULL; 256 257 va_start(args, name); 258 vsnprintf(_name, sizeof(_name), name, args); 259 va_end(args); 260 261 list_for_each_entry(carveout, &rproc->carveouts, node) { 262 /* Compare carveout and requested names */ 263 if (!strcmp(carveout->name, _name)) { 264 mem = carveout; 265 break; 266 } 267 } 268 269 return mem; 270 } 271 272 /** 273 * rproc_check_carveout_da() - Check specified carveout da configuration 274 * @rproc: handle of a remote processor 275 * @mem: pointer on carveout to check 276 * @da: area device address 277 * @len: associated area size 278 * 279 * This function is a helper function to verify requested device area (couple 280 * da, len) is part of specified carveout. 281 * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is 282 * checked. 283 * 284 * Return: 0 if carveout matches request else error 285 */ 286 static int rproc_check_carveout_da(struct rproc *rproc, 287 struct rproc_mem_entry *mem, u32 da, u32 len) 288 { 289 struct device *dev = &rproc->dev; 290 int delta; 291 292 /* Check requested resource length */ 293 if (len > mem->len) { 294 dev_err(dev, "Registered carveout doesn't fit len request\n"); 295 return -EINVAL; 296 } 297 298 if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) { 299 /* Address doesn't match registered carveout configuration */ 300 return -EINVAL; 301 } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) { 302 delta = da - mem->da; 303 304 /* Check requested resource belongs to registered carveout */ 305 if (delta < 0) { 306 dev_err(dev, 307 "Registered carveout doesn't fit da request\n"); 308 return -EINVAL; 309 } 310 311 if (delta + len > mem->len) { 312 dev_err(dev, 313 "Registered carveout doesn't fit len request\n"); 314 return -EINVAL; 315 } 316 } 317 318 return 0; 319 } 320 321 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i) 322 { 323 struct rproc *rproc = rvdev->rproc; 324 struct device *dev = &rproc->dev; 325 struct rproc_vring *rvring = &rvdev->vring[i]; 326 struct fw_rsc_vdev *rsc; 327 int ret, notifyid; 328 struct rproc_mem_entry *mem; 329 size_t size; 330 331 /* actual size of vring (in bytes) */ 332 size = PAGE_ALIGN(vring_size(rvring->num, rvring->align)); 333 334 rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; 335 336 /* Search for pre-registered carveout */ 337 mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index, 338 i); 339 if (mem) { 340 if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size)) 341 return -ENOMEM; 342 } else { 343 /* Register carveout in list */ 344 mem = rproc_mem_entry_init(dev, NULL, 0, 345 size, rsc->vring[i].da, 346 rproc_alloc_carveout, 347 rproc_release_carveout, 348 "vdev%dvring%d", 349 rvdev->index, i); 350 if (!mem) { 351 dev_err(dev, "Can't allocate memory entry structure\n"); 352 return -ENOMEM; 353 } 354 355 rproc_add_carveout(rproc, mem); 356 } 357 358 /* 359 * Assign an rproc-wide unique index for this vring 360 * TODO: assign a notifyid for rvdev updates as well 361 * TODO: support predefined notifyids (via resource table) 362 */ 363 ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL); 364 if (ret < 0) { 365 dev_err(dev, "idr_alloc failed: %d\n", ret); 366 return ret; 367 } 368 notifyid = ret; 369 370 /* Potentially bump max_notifyid */ 371 if (notifyid > rproc->max_notifyid) 372 rproc->max_notifyid = notifyid; 373 374 rvring->notifyid = notifyid; 375 376 /* Let the rproc know the notifyid of this vring.*/ 377 rsc->vring[i].notifyid = notifyid; 378 return 0; 379 } 380 381 int 382 rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) 383 { 384 struct rproc *rproc = rvdev->rproc; 385 struct device *dev = &rproc->dev; 386 struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; 387 struct rproc_vring *rvring = &rvdev->vring[i]; 388 389 dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n", 390 i, vring->da, vring->num, vring->align); 391 392 /* verify queue size and vring alignment are sane */ 393 if (!vring->num || !vring->align) { 394 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n", 395 vring->num, vring->align); 396 return -EINVAL; 397 } 398 399 rvring->num = vring->num; 400 rvring->align = vring->align; 401 rvring->rvdev = rvdev; 402 403 return 0; 404 } 405 406 void rproc_free_vring(struct rproc_vring *rvring) 407 { 408 struct rproc *rproc = rvring->rvdev->rproc; 409 int idx = rvring - rvring->rvdev->vring; 410 struct fw_rsc_vdev *rsc; 411 412 idr_remove(&rproc->notifyids, rvring->notifyid); 413 414 /* 415 * At this point rproc_stop() has been called and the installed resource 416 * table in the remote processor memory may no longer be accessible. As 417 * such and as per rproc_stop(), rproc->table_ptr points to the cached 418 * resource table (rproc->cached_table). The cached resource table is 419 * only available when a remote processor has been booted by the 420 * remoteproc core, otherwise it is NULL. 421 * 422 * Based on the above, reset the virtio device section in the cached 423 * resource table only if there is one to work with. 424 */ 425 if (rproc->table_ptr) { 426 rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; 427 rsc->vring[idx].da = 0; 428 rsc->vring[idx].notifyid = -1; 429 } 430 } 431 432 void rproc_add_rvdev(struct rproc *rproc, struct rproc_vdev *rvdev) 433 { 434 if (rvdev && rproc) 435 list_add_tail(&rvdev->node, &rproc->rvdevs); 436 } 437 438 void rproc_remove_rvdev(struct rproc_vdev *rvdev) 439 { 440 if (rvdev) 441 list_del(&rvdev->node); 442 } 443 /** 444 * rproc_handle_vdev() - handle a vdev fw resource 445 * @rproc: the remote processor 446 * @ptr: the vring resource descriptor 447 * @offset: offset of the resource entry 448 * @avail: size of available data (for sanity checking the image) 449 * 450 * This resource entry requests the host to statically register a virtio 451 * device (vdev), and setup everything needed to support it. It contains 452 * everything needed to make it possible: the virtio device id, virtio 453 * device features, vrings information, virtio config space, etc... 454 * 455 * Before registering the vdev, the vrings are allocated from non-cacheable 456 * physically contiguous memory. Currently we only support two vrings per 457 * remote processor (temporary limitation). We might also want to consider 458 * doing the vring allocation only later when ->find_vqs() is invoked, and 459 * then release them upon ->del_vqs(). 460 * 461 * Note: @da is currently not really handled correctly: we dynamically 462 * allocate it using the DMA API, ignoring requested hard coded addresses, 463 * and we don't take care of any required IOMMU programming. This is all 464 * going to be taken care of when the generic iommu-based DMA API will be 465 * merged. Meanwhile, statically-addressed iommu-based firmware images should 466 * use RSC_DEVMEM resource entries to map their required @da to the physical 467 * address of their base CMA region (ouch, hacky!). 468 * 469 * Return: 0 on success, or an appropriate error code otherwise 470 */ 471 static int rproc_handle_vdev(struct rproc *rproc, void *ptr, 472 int offset, int avail) 473 { 474 struct fw_rsc_vdev *rsc = ptr; 475 struct device *dev = &rproc->dev; 476 struct rproc_vdev *rvdev; 477 size_t rsc_size; 478 struct rproc_vdev_data rvdev_data; 479 struct platform_device *pdev; 480 481 /* make sure resource isn't truncated */ 482 rsc_size = struct_size(rsc, vring, rsc->num_of_vrings); 483 if (size_add(rsc_size, rsc->config_len) > avail) { 484 dev_err(dev, "vdev rsc is truncated\n"); 485 return -EINVAL; 486 } 487 488 /* make sure reserved bytes are zeroes */ 489 if (rsc->reserved[0] || rsc->reserved[1]) { 490 dev_err(dev, "vdev rsc has non zero reserved bytes\n"); 491 return -EINVAL; 492 } 493 494 dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n", 495 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); 496 497 /* we currently support only two vrings per rvdev */ 498 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { 499 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); 500 return -EINVAL; 501 } 502 503 rvdev_data.id = rsc->id; 504 rvdev_data.index = rproc->nb_vdev++; 505 rvdev_data.rsc_offset = offset; 506 rvdev_data.rsc = rsc; 507 508 /* 509 * When there is more than one remote processor, rproc->nb_vdev number is 510 * same for each separate instances of "rproc". If rvdev_data.index is used 511 * as device id, then we get duplication in sysfs, so need to use 512 * PLATFORM_DEVID_AUTO to auto select device id. 513 */ 514 pdev = platform_device_register_data(dev, "rproc-virtio", PLATFORM_DEVID_AUTO, &rvdev_data, 515 sizeof(rvdev_data)); 516 if (IS_ERR(pdev)) { 517 dev_err(dev, "failed to create rproc-virtio device\n"); 518 return PTR_ERR(pdev); 519 } 520 521 return 0; 522 } 523 524 /** 525 * rproc_handle_trace() - handle a shared trace buffer resource 526 * @rproc: the remote processor 527 * @ptr: the trace resource descriptor 528 * @offset: offset of the resource entry 529 * @avail: size of available data (for sanity checking the image) 530 * 531 * In case the remote processor dumps trace logs into memory, 532 * export it via debugfs. 533 * 534 * Currently, the 'da' member of @rsc should contain the device address 535 * where the remote processor is dumping the traces. Later we could also 536 * support dynamically allocating this address using the generic 537 * DMA API (but currently there isn't a use case for that). 538 * 539 * Return: 0 on success, or an appropriate error code otherwise 540 */ 541 static int rproc_handle_trace(struct rproc *rproc, void *ptr, 542 int offset, int avail) 543 { 544 struct fw_rsc_trace *rsc = ptr; 545 struct rproc_debug_trace *trace; 546 struct device *dev = &rproc->dev; 547 char name[15]; 548 549 if (sizeof(*rsc) > avail) { 550 dev_err(dev, "trace rsc is truncated\n"); 551 return -EINVAL; 552 } 553 554 /* make sure reserved bytes are zeroes */ 555 if (rsc->reserved) { 556 dev_err(dev, "trace rsc has non zero reserved bytes\n"); 557 return -EINVAL; 558 } 559 560 trace = kzalloc_obj(*trace); 561 if (!trace) 562 return -ENOMEM; 563 564 /* set the trace buffer dma properties */ 565 trace->trace_mem.len = rsc->len; 566 trace->trace_mem.da = rsc->da; 567 568 /* set pointer on rproc device */ 569 trace->rproc = rproc; 570 571 /* make sure snprintf always null terminates, even if truncating */ 572 snprintf(name, sizeof(name), "trace%d", rproc->num_traces); 573 574 /* create the debugfs entry */ 575 trace->tfile = rproc_create_trace_file(name, rproc, trace); 576 577 list_add_tail(&trace->node, &rproc->traces); 578 579 rproc->num_traces++; 580 581 dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n", 582 name, rsc->da, rsc->len); 583 584 return 0; 585 } 586 587 /** 588 * rproc_handle_devmem() - handle devmem resource entry 589 * @rproc: remote processor handle 590 * @ptr: the devmem resource entry 591 * @offset: offset of the resource entry 592 * @avail: size of available data (for sanity checking the image) 593 * 594 * Remote processors commonly need to access certain on-chip peripherals. 595 * 596 * Some of these remote processors access memory via an iommu device, 597 * and might require us to configure their iommu before they can access 598 * the on-chip peripherals they need. 599 * 600 * This resource entry is a request to map such a peripheral device. 601 * 602 * These devmem entries will contain the physical address of the device in 603 * the 'pa' member. If a specific device address is expected, then 'da' will 604 * contain it (currently this is the only use case supported). 'len' will 605 * contain the size of the physical region we need to map. 606 * 607 * Currently we just "trust" those devmem entries to contain valid physical 608 * addresses, but this is going to change: we want the implementations to 609 * tell us ranges of physical addresses the firmware is allowed to request, 610 * and not allow firmwares to request access to physical addresses that 611 * are outside those ranges. 612 * 613 * Return: 0 on success, or an appropriate error code otherwise 614 */ 615 static int rproc_handle_devmem(struct rproc *rproc, void *ptr, 616 int offset, int avail) 617 { 618 struct fw_rsc_devmem *rsc = ptr; 619 struct rproc_mem_entry *mapping; 620 struct device *dev = &rproc->dev; 621 int ret; 622 623 /* no point in handling this resource without a valid iommu domain */ 624 if (!rproc->domain) 625 return -EINVAL; 626 627 if (sizeof(*rsc) > avail) { 628 dev_err(dev, "devmem rsc is truncated\n"); 629 return -EINVAL; 630 } 631 632 /* make sure reserved bytes are zeroes */ 633 if (rsc->reserved) { 634 dev_err(dev, "devmem rsc has non zero reserved bytes\n"); 635 return -EINVAL; 636 } 637 638 mapping = kzalloc_obj(*mapping); 639 if (!mapping) 640 return -ENOMEM; 641 642 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags, 643 GFP_KERNEL); 644 if (ret) { 645 dev_err(dev, "failed to map devmem: %d\n", ret); 646 goto out; 647 } 648 649 /* 650 * We'll need this info later when we'll want to unmap everything 651 * (e.g. on shutdown). 652 * 653 * We can't trust the remote processor not to change the resource 654 * table, so we must maintain this info independently. 655 */ 656 mapping->da = rsc->da; 657 mapping->len = rsc->len; 658 list_add_tail(&mapping->node, &rproc->mappings); 659 660 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", 661 rsc->pa, rsc->da, rsc->len); 662 663 return 0; 664 665 out: 666 kfree(mapping); 667 return ret; 668 } 669 670 /** 671 * rproc_alloc_carveout() - allocated specified carveout 672 * @rproc: rproc handle 673 * @mem: the memory entry to allocate 674 * 675 * This function allocate specified memory entry @mem using 676 * dma_alloc_coherent() as default allocator 677 * 678 * Return: 0 on success, or an appropriate error code otherwise 679 */ 680 static int rproc_alloc_carveout(struct rproc *rproc, 681 struct rproc_mem_entry *mem) 682 { 683 struct rproc_mem_entry *mapping = NULL; 684 struct device *dev = &rproc->dev; 685 dma_addr_t dma; 686 void *va; 687 int ret; 688 689 va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL); 690 if (!va) { 691 dev_err(dev->parent, 692 "failed to allocate dma memory: len 0x%zx\n", 693 mem->len); 694 return -ENOMEM; 695 } 696 697 dev_dbg(dev, "carveout va %p, dma %pad, len 0x%zx\n", 698 va, &dma, mem->len); 699 700 if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) { 701 /* 702 * Check requested da is equal to dma address 703 * and print a warn message in case of missalignment. 704 * Don't stop rproc_start sequence as coprocessor may 705 * build pa to da translation on its side. 706 */ 707 if (mem->da != (u32)dma) 708 dev_warn(dev->parent, 709 "Allocated carveout doesn't fit device address request\n"); 710 } 711 712 /* 713 * Ok, this is non-standard. 714 * 715 * Sometimes we can't rely on the generic iommu-based DMA API 716 * to dynamically allocate the device address and then set the IOMMU 717 * tables accordingly, because some remote processors might 718 * _require_ us to use hard coded device addresses that their 719 * firmware was compiled with. 720 * 721 * In this case, we must use the IOMMU API directly and map 722 * the memory to the device address as expected by the remote 723 * processor. 724 * 725 * Obviously such remote processor devices should not be configured 726 * to use the iommu-based DMA API: we expect 'dma' to contain the 727 * physical address in this case. 728 */ 729 if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) { 730 mapping = kzalloc_obj(*mapping); 731 if (!mapping) { 732 ret = -ENOMEM; 733 goto dma_free; 734 } 735 736 ret = iommu_map(rproc->domain, mem->da, dma, mem->len, 737 mem->flags, GFP_KERNEL); 738 if (ret) { 739 dev_err(dev, "iommu_map failed: %d\n", ret); 740 goto free_mapping; 741 } 742 743 /* 744 * We'll need this info later when we'll want to unmap 745 * everything (e.g. on shutdown). 746 * 747 * We can't trust the remote processor not to change the 748 * resource table, so we must maintain this info independently. 749 */ 750 mapping->da = mem->da; 751 mapping->len = mem->len; 752 list_add_tail(&mapping->node, &rproc->mappings); 753 754 dev_dbg(dev, "carveout mapped 0x%x to %pad\n", 755 mem->da, &dma); 756 } 757 758 if (mem->da == FW_RSC_ADDR_ANY) { 759 /* Update device address as undefined by requester */ 760 if ((u64)dma & HIGH_BITS_MASK) 761 dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n"); 762 763 mem->da = (u32)dma; 764 } 765 766 mem->dma = dma; 767 mem->va = va; 768 769 return 0; 770 771 free_mapping: 772 kfree(mapping); 773 dma_free: 774 dma_free_coherent(dev->parent, mem->len, va, dma); 775 return ret; 776 } 777 778 /** 779 * rproc_release_carveout() - release acquired carveout 780 * @rproc: rproc handle 781 * @mem: the memory entry to release 782 * 783 * This function releases specified memory entry @mem allocated via 784 * rproc_alloc_carveout() function by @rproc. 785 * 786 * Return: 0 on success, or an appropriate error code otherwise 787 */ 788 static int rproc_release_carveout(struct rproc *rproc, 789 struct rproc_mem_entry *mem) 790 { 791 struct device *dev = &rproc->dev; 792 793 /* clean up carveout allocations */ 794 dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma); 795 return 0; 796 } 797 798 /** 799 * rproc_handle_carveout() - handle phys contig memory allocation requests 800 * @rproc: rproc handle 801 * @ptr: the resource entry 802 * @offset: offset of the resource entry 803 * @avail: size of available data (for image validation) 804 * 805 * This function will handle firmware requests for allocation of physically 806 * contiguous memory regions. 807 * 808 * These request entries should come first in the firmware's resource table, 809 * as other firmware entries might request placing other data objects inside 810 * these memory regions (e.g. data/code segments, trace resource entries, ...). 811 * 812 * Allocating memory this way helps utilizing the reserved physical memory 813 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries 814 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB 815 * pressure is important; it may have a substantial impact on performance. 816 * 817 * Return: 0 on success, or an appropriate error code otherwise 818 */ 819 static int rproc_handle_carveout(struct rproc *rproc, 820 void *ptr, int offset, int avail) 821 { 822 struct fw_rsc_carveout *rsc = ptr; 823 struct rproc_mem_entry *carveout; 824 struct device *dev = &rproc->dev; 825 826 if (sizeof(*rsc) > avail) { 827 dev_err(dev, "carveout rsc is truncated\n"); 828 return -EINVAL; 829 } 830 831 /* make sure reserved bytes are zeroes */ 832 if (rsc->reserved) { 833 dev_err(dev, "carveout rsc has non zero reserved bytes\n"); 834 return -EINVAL; 835 } 836 837 dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n", 838 rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags); 839 840 /* 841 * Check carveout rsc already part of a registered carveout, 842 * Search by name, then check the da and length 843 */ 844 carveout = rproc_find_carveout_by_name(rproc, rsc->name); 845 846 if (carveout) { 847 if (carveout->rsc_offset != FW_RSC_ADDR_ANY) { 848 dev_err(dev, 849 "Carveout already associated to resource table\n"); 850 return -ENOMEM; 851 } 852 853 if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len)) 854 return -ENOMEM; 855 856 /* Update memory carveout with resource table info */ 857 carveout->rsc_offset = offset; 858 carveout->flags = rsc->flags; 859 860 return 0; 861 } 862 863 /* Register carveout in list */ 864 carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da, 865 rproc_alloc_carveout, 866 rproc_release_carveout, rsc->name); 867 if (!carveout) { 868 dev_err(dev, "Can't allocate memory entry structure\n"); 869 return -ENOMEM; 870 } 871 872 carveout->flags = rsc->flags; 873 carveout->rsc_offset = offset; 874 rproc_add_carveout(rproc, carveout); 875 876 return 0; 877 } 878 879 /** 880 * rproc_add_carveout() - register an allocated carveout region 881 * @rproc: rproc handle 882 * @mem: memory entry to register 883 * 884 * This function registers specified memory entry in @rproc carveouts list. 885 * Specified carveout should have been allocated before registering. 886 */ 887 void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) 888 { 889 list_add_tail(&mem->node, &rproc->carveouts); 890 } 891 EXPORT_SYMBOL(rproc_add_carveout); 892 893 /** 894 * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct 895 * @dev: pointer on device struct 896 * @va: virtual address 897 * @dma: dma address 898 * @len: memory carveout length 899 * @da: device address 900 * @alloc: memory carveout allocation function 901 * @release: memory carveout release function 902 * @name: carveout name 903 * 904 * This function allocates a rproc_mem_entry struct and fill it with parameters 905 * provided by client. 906 * 907 * Return: a valid pointer on success, or NULL on failure 908 */ 909 __printf(8, 9) 910 struct rproc_mem_entry * 911 rproc_mem_entry_init(struct device *dev, 912 void *va, dma_addr_t dma, size_t len, u32 da, 913 int (*alloc)(struct rproc *, struct rproc_mem_entry *), 914 int (*release)(struct rproc *, struct rproc_mem_entry *), 915 const char *name, ...) 916 { 917 struct rproc_mem_entry *mem; 918 va_list args; 919 920 mem = kzalloc_obj(*mem); 921 if (!mem) 922 return mem; 923 924 mem->va = va; 925 mem->dma = dma; 926 mem->da = da; 927 mem->len = len; 928 mem->alloc = alloc; 929 mem->release = release; 930 mem->rsc_offset = FW_RSC_ADDR_ANY; 931 mem->of_resm_idx = -1; 932 933 va_start(args, name); 934 vsnprintf(mem->name, sizeof(mem->name), name, args); 935 va_end(args); 936 937 return mem; 938 } 939 EXPORT_SYMBOL(rproc_mem_entry_init); 940 941 /** 942 * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct 943 * from a reserved memory phandle 944 * @dev: pointer on device struct 945 * @of_resm_idx: reserved memory phandle index in "memory-region" 946 * @len: memory carveout length 947 * @da: device address 948 * @name: carveout name 949 * 950 * This function allocates a rproc_mem_entry struct and fill it with parameters 951 * provided by client. 952 * 953 * Return: a valid pointer on success, or NULL on failure 954 */ 955 __printf(5, 6) 956 struct rproc_mem_entry * 957 rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len, 958 u32 da, const char *name, ...) 959 { 960 struct rproc_mem_entry *mem; 961 va_list args; 962 963 mem = kzalloc_obj(*mem); 964 if (!mem) 965 return mem; 966 967 mem->da = da; 968 mem->len = len; 969 mem->rsc_offset = FW_RSC_ADDR_ANY; 970 mem->of_resm_idx = of_resm_idx; 971 972 va_start(args, name); 973 vsnprintf(mem->name, sizeof(mem->name), name, args); 974 va_end(args); 975 976 return mem; 977 } 978 EXPORT_SYMBOL(rproc_of_resm_mem_entry_init); 979 980 /** 981 * rproc_of_parse_firmware() - parse and return the firmware-name 982 * @dev: pointer on device struct representing a rproc 983 * @index: index to use for the firmware-name retrieval 984 * @fw_name: pointer to a character string, in which the firmware 985 * name is returned on success and unmodified otherwise. 986 * 987 * This is an OF helper function that parses a device's DT node for 988 * the "firmware-name" property and returns the firmware name pointer 989 * in @fw_name on success. 990 * 991 * Return: 0 on success, or an appropriate failure. 992 */ 993 int rproc_of_parse_firmware(struct device *dev, int index, const char **fw_name) 994 { 995 int ret; 996 997 ret = of_property_read_string_index(dev->of_node, "firmware-name", 998 index, fw_name); 999 return ret ? ret : 0; 1000 } 1001 EXPORT_SYMBOL(rproc_of_parse_firmware); 1002 1003 /* 1004 * A lookup table for resource handlers. The indices are defined in 1005 * enum fw_resource_type. 1006 */ 1007 static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = { 1008 [RSC_CARVEOUT] = rproc_handle_carveout, 1009 [RSC_DEVMEM] = rproc_handle_devmem, 1010 [RSC_TRACE] = rproc_handle_trace, 1011 [RSC_VDEV] = rproc_handle_vdev, 1012 }; 1013 1014 struct rproc_rsc_cb_data { 1015 struct rproc *rproc; 1016 rproc_handle_resource_t *handlers; 1017 }; 1018 1019 static int rproc_handle_rsc_entry(u32 type, void *rsc, int offset, 1020 int avail, void *data) 1021 { 1022 struct rproc_rsc_cb_data *d = data; 1023 struct rproc *rproc = d->rproc; 1024 struct device *dev = &rproc->dev; 1025 rproc_handle_resource_t handler; 1026 int ret; 1027 1028 dev_dbg(dev, "rsc: type %d\n", type); 1029 1030 if (type >= RSC_VENDOR_START && type <= RSC_VENDOR_END) { 1031 ret = rproc_handle_rsc(rproc, type, rsc, offset, avail); 1032 if (ret == RSC_HANDLED) 1033 return 0; 1034 if (ret < 0) 1035 return ret; 1036 dev_warn(dev, "unsupported vendor resource %d\n", type); 1037 return 0; 1038 } 1039 1040 if (type >= RSC_LAST) { 1041 dev_warn(dev, "unsupported resource %d\n", type); 1042 return 0; 1043 } 1044 1045 handler = d->handlers[type]; 1046 if (!handler) 1047 return 0; 1048 1049 return handler(rproc, rsc, offset, avail); 1050 } 1051 1052 /* handle firmware resource entries before booting the remote processor */ 1053 static int rproc_handle_resources(struct rproc *rproc, 1054 rproc_handle_resource_t handlers[RSC_LAST]) 1055 { 1056 struct rproc_rsc_cb_data d = { .rproc = rproc, .handlers = handlers }; 1057 1058 if (!rproc->table_ptr) 1059 return 0; 1060 1061 return rsc_table_for_each_entry(rproc->table_ptr, rproc->table_sz, 1062 &rproc->dev, rproc_handle_rsc_entry, &d); 1063 } 1064 1065 static int rproc_prepare_subdevices(struct rproc *rproc) 1066 { 1067 struct rproc_subdev *subdev; 1068 int ret; 1069 1070 list_for_each_entry(subdev, &rproc->subdevs, node) { 1071 if (subdev->prepare) { 1072 ret = subdev->prepare(subdev); 1073 if (ret) 1074 goto unroll_preparation; 1075 } 1076 } 1077 1078 return 0; 1079 1080 unroll_preparation: 1081 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1082 if (subdev->unprepare) 1083 subdev->unprepare(subdev); 1084 } 1085 1086 return ret; 1087 } 1088 1089 static int rproc_start_subdevices(struct rproc *rproc) 1090 { 1091 struct rproc_subdev *subdev; 1092 int ret; 1093 1094 list_for_each_entry(subdev, &rproc->subdevs, node) { 1095 if (subdev->start) { 1096 ret = subdev->start(subdev); 1097 if (ret) 1098 goto unroll_registration; 1099 } 1100 } 1101 1102 rproc->subdevs_started = true; 1103 1104 return 0; 1105 1106 unroll_registration: 1107 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1108 if (subdev->stop) 1109 subdev->stop(subdev, true); 1110 } 1111 1112 return ret; 1113 } 1114 1115 static void rproc_stop_subdevices(struct rproc *rproc, bool crashed) 1116 { 1117 struct rproc_subdev *subdev; 1118 1119 if (!rproc->subdevs_started) 1120 return; 1121 1122 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1123 if (subdev->stop) 1124 subdev->stop(subdev, crashed); 1125 } 1126 1127 rproc->subdevs_started = false; 1128 } 1129 1130 static void rproc_unprepare_subdevices(struct rproc *rproc) 1131 { 1132 struct rproc_subdev *subdev; 1133 1134 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1135 if (subdev->unprepare) 1136 subdev->unprepare(subdev); 1137 } 1138 } 1139 1140 /** 1141 * rproc_alloc_registered_carveouts() - allocate all carveouts registered 1142 * in the list 1143 * @rproc: the remote processor handle 1144 * 1145 * This function parses registered carveout list, performs allocation 1146 * if alloc() ops registered and updates resource table information 1147 * if rsc_offset set. 1148 * 1149 * Return: 0 on success 1150 */ 1151 static int rproc_alloc_registered_carveouts(struct rproc *rproc) 1152 { 1153 struct rproc_mem_entry *entry, *tmp; 1154 struct fw_rsc_carveout *rsc; 1155 struct device *dev = &rproc->dev; 1156 u64 pa; 1157 int ret; 1158 1159 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1160 if (entry->alloc) { 1161 ret = entry->alloc(rproc, entry); 1162 if (ret) { 1163 dev_err(dev, "Unable to allocate carveout %s: %d\n", 1164 entry->name, ret); 1165 return -ENOMEM; 1166 } 1167 } 1168 1169 if (entry->rsc_offset != FW_RSC_ADDR_ANY) { 1170 /* update resource table */ 1171 rsc = (void *)rproc->table_ptr + entry->rsc_offset; 1172 1173 /* 1174 * Some remote processors might need to know the pa 1175 * even though they are behind an IOMMU. E.g., OMAP4's 1176 * remote M3 processor needs this so it can control 1177 * on-chip hardware accelerators that are not behind 1178 * the IOMMU, and therefor must know the pa. 1179 * 1180 * Generally we don't want to expose physical addresses 1181 * if we don't have to (remote processors are generally 1182 * _not_ trusted), so we might want to do this only for 1183 * remote processor that _must_ have this (e.g. OMAP4's 1184 * dual M3 subsystem). 1185 * 1186 * Non-IOMMU processors might also want to have this info. 1187 * In this case, the device address and the physical address 1188 * are the same. 1189 */ 1190 1191 /* Use va if defined else dma to generate pa */ 1192 if (entry->va) 1193 pa = (u64)rproc_va_to_pa(entry->va); 1194 else 1195 pa = (u64)entry->dma; 1196 1197 if (((u64)pa) & HIGH_BITS_MASK) 1198 dev_warn(dev, 1199 "Physical address cast in 32bit to fit resource table format\n"); 1200 1201 rsc->pa = (u32)pa; 1202 rsc->da = entry->da; 1203 rsc->len = entry->len; 1204 } 1205 } 1206 1207 return 0; 1208 } 1209 1210 1211 /** 1212 * rproc_resource_cleanup() - clean up and free all acquired resources 1213 * @rproc: rproc handle 1214 * 1215 * This function will free all resources acquired for @rproc, and it 1216 * is called whenever @rproc either shuts down or fails to boot. 1217 */ 1218 void rproc_resource_cleanup(struct rproc *rproc) 1219 { 1220 struct rproc_mem_entry *entry, *tmp; 1221 struct rproc_debug_trace *trace, *ttmp; 1222 struct rproc_vdev *rvdev, *rvtmp; 1223 struct device *dev = &rproc->dev; 1224 1225 /* clean up debugfs trace entries */ 1226 list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) { 1227 rproc_remove_trace_file(trace->tfile); 1228 rproc->num_traces--; 1229 list_del(&trace->node); 1230 kfree(trace); 1231 } 1232 1233 /* clean up iommu mapping entries */ 1234 list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { 1235 size_t unmapped; 1236 1237 unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); 1238 if (unmapped != entry->len) { 1239 /* nothing much to do besides complaining */ 1240 dev_err(dev, "failed to unmap %zx/%zu\n", entry->len, 1241 unmapped); 1242 } 1243 1244 list_del(&entry->node); 1245 kfree(entry); 1246 } 1247 1248 /* clean up carveout allocations */ 1249 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1250 if (entry->release) 1251 entry->release(rproc, entry); 1252 list_del(&entry->node); 1253 kfree(entry); 1254 } 1255 1256 /* clean up remote vdev entries */ 1257 list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) 1258 platform_device_unregister(rvdev->pdev); 1259 1260 rproc_coredump_cleanup(rproc); 1261 } 1262 EXPORT_SYMBOL(rproc_resource_cleanup); 1263 1264 static int rproc_start(struct rproc *rproc, const struct firmware *fw) 1265 { 1266 struct resource_table *loaded_table; 1267 struct device *dev = &rproc->dev; 1268 int ret; 1269 1270 /* load the ELF segments to memory */ 1271 ret = rproc_load_segments(rproc, fw); 1272 if (ret) { 1273 dev_err(dev, "Failed to load program segments: %d\n", ret); 1274 return ret; 1275 } 1276 1277 /* 1278 * The starting device has been given the rproc->cached_table as the 1279 * resource table. The address of the vring along with the other 1280 * allocated resources (carveouts etc) is stored in cached_table. 1281 * In order to pass this information to the remote device we must copy 1282 * this information to device memory. We also update the table_ptr so 1283 * that any subsequent changes will be applied to the loaded version. 1284 */ 1285 loaded_table = rproc_find_loaded_rsc_table(rproc, fw); 1286 if (loaded_table) { 1287 memcpy(loaded_table, rproc->cached_table, rproc->table_sz); 1288 rproc->table_ptr = loaded_table; 1289 } 1290 1291 ret = rproc_prepare_subdevices(rproc); 1292 if (ret) { 1293 dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1294 rproc->name, ret); 1295 goto reset_table_ptr; 1296 } 1297 1298 /* power up the remote processor */ 1299 ret = rproc->ops->start(rproc); 1300 if (ret) { 1301 dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); 1302 goto unprepare_subdevices; 1303 } 1304 1305 /* Start any subdevices for the remote processor */ 1306 ret = rproc_start_subdevices(rproc); 1307 if (ret) { 1308 dev_err(dev, "failed to probe subdevices for %s: %d\n", 1309 rproc->name, ret); 1310 goto stop_rproc; 1311 } 1312 1313 rproc->state = RPROC_RUNNING; 1314 1315 dev_info(dev, "remote processor %s is now up\n", rproc->name); 1316 1317 return 0; 1318 1319 stop_rproc: 1320 rproc->ops->stop(rproc); 1321 unprepare_subdevices: 1322 rproc_unprepare_subdevices(rproc); 1323 reset_table_ptr: 1324 rproc->table_ptr = rproc->cached_table; 1325 1326 return ret; 1327 } 1328 1329 static int __rproc_attach(struct rproc *rproc) 1330 { 1331 struct device *dev = &rproc->dev; 1332 int ret; 1333 1334 ret = rproc_prepare_subdevices(rproc); 1335 if (ret) { 1336 dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1337 rproc->name, ret); 1338 goto out; 1339 } 1340 1341 /* Attach to the remote processor */ 1342 ret = rproc_attach_device(rproc); 1343 if (ret) { 1344 dev_err(dev, "can't attach to rproc %s: %d\n", 1345 rproc->name, ret); 1346 goto unprepare_subdevices; 1347 } 1348 1349 /* Start any subdevices for the remote processor */ 1350 ret = rproc_start_subdevices(rproc); 1351 if (ret) { 1352 dev_err(dev, "failed to probe subdevices for %s: %d\n", 1353 rproc->name, ret); 1354 goto stop_rproc; 1355 } 1356 1357 rproc->state = RPROC_ATTACHED; 1358 1359 dev_info(dev, "remote processor %s is now attached\n", rproc->name); 1360 1361 return 0; 1362 1363 stop_rproc: 1364 rproc->ops->stop(rproc); 1365 unprepare_subdevices: 1366 rproc_unprepare_subdevices(rproc); 1367 out: 1368 return ret; 1369 } 1370 1371 /* 1372 * take a firmware and boot a remote processor with it. 1373 */ 1374 static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) 1375 { 1376 struct device *dev = &rproc->dev; 1377 const char *name = rproc->firmware; 1378 int ret; 1379 1380 ret = rproc_fw_sanity_check(rproc, fw); 1381 if (ret) 1382 return ret; 1383 1384 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); 1385 1386 /* 1387 * if enabling an IOMMU isn't relevant for this rproc, this is 1388 * just a nop 1389 */ 1390 ret = rproc_enable_iommu(rproc); 1391 if (ret) { 1392 dev_err(dev, "can't enable iommu: %d\n", ret); 1393 return ret; 1394 } 1395 1396 /* Prepare rproc for firmware loading if needed */ 1397 ret = rproc_prepare_device(rproc); 1398 if (ret) { 1399 dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret); 1400 goto disable_iommu; 1401 } 1402 1403 rproc->bootaddr = rproc_get_boot_addr(rproc, fw); 1404 1405 /* Load resource table, core dump segment list etc from the firmware */ 1406 ret = rproc_parse_fw(rproc, fw); 1407 if (ret) 1408 goto unprepare_rproc; 1409 1410 /* reset max_notifyid */ 1411 rproc->max_notifyid = -1; 1412 1413 /* reset handled vdev */ 1414 rproc->nb_vdev = 0; 1415 1416 /* handle fw resources which are required to boot rproc */ 1417 ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1418 if (ret) { 1419 dev_err(dev, "Failed to process resources: %d\n", ret); 1420 goto clean_up_resources; 1421 } 1422 1423 /* Allocate carveout resources associated to rproc */ 1424 ret = rproc_alloc_registered_carveouts(rproc); 1425 if (ret) { 1426 dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1427 ret); 1428 goto clean_up_resources; 1429 } 1430 1431 ret = rproc_start(rproc, fw); 1432 if (ret) 1433 goto clean_up_resources; 1434 1435 return 0; 1436 1437 clean_up_resources: 1438 rproc_resource_cleanup(rproc); 1439 kfree(rproc->cached_table); 1440 rproc->cached_table = NULL; 1441 rproc->table_ptr = NULL; 1442 unprepare_rproc: 1443 /* release HW resources if needed */ 1444 rproc_unprepare_device(rproc); 1445 disable_iommu: 1446 rproc_disable_iommu(rproc); 1447 return ret; 1448 } 1449 1450 static int rproc_set_rsc_table(struct rproc *rproc) 1451 { 1452 struct resource_table *table_ptr; 1453 struct device *dev = &rproc->dev; 1454 size_t table_sz; 1455 int ret; 1456 1457 table_ptr = rproc_get_loaded_rsc_table(rproc, &table_sz); 1458 if (!table_ptr) { 1459 /* Not having a resource table is acceptable */ 1460 return 0; 1461 } 1462 1463 if (IS_ERR(table_ptr)) { 1464 ret = PTR_ERR(table_ptr); 1465 dev_err(dev, "can't load resource table: %d\n", ret); 1466 return ret; 1467 } 1468 1469 /* 1470 * If it is possible to detach the remote processor, keep an untouched 1471 * copy of the resource table. That way we can start fresh again when 1472 * the remote processor is re-attached, that is: 1473 * 1474 * DETACHED -> ATTACHED -> DETACHED -> ATTACHED 1475 * 1476 * Free'd in rproc_reset_rsc_table_on_detach() and 1477 * rproc_reset_rsc_table_on_stop(). 1478 */ 1479 if (rproc->ops->detach) { 1480 rproc->clean_table = kmemdup(table_ptr, table_sz, GFP_KERNEL); 1481 if (!rproc->clean_table) 1482 return -ENOMEM; 1483 } else { 1484 rproc->clean_table = NULL; 1485 } 1486 1487 rproc->cached_table = NULL; 1488 rproc->table_ptr = table_ptr; 1489 rproc->table_sz = table_sz; 1490 1491 return 0; 1492 } 1493 1494 static int rproc_reset_rsc_table_on_detach(struct rproc *rproc) 1495 { 1496 struct resource_table *table_ptr; 1497 1498 /* A resource table was never retrieved, nothing to do here */ 1499 if (!rproc->table_ptr) 1500 return 0; 1501 1502 /* 1503 * If we made it to this point a clean_table _must_ have been 1504 * allocated in rproc_set_rsc_table(). If one isn't present 1505 * something went really wrong and we must complain. 1506 */ 1507 if (WARN_ON(!rproc->clean_table)) 1508 return -EINVAL; 1509 1510 /* Remember where the external entity installed the resource table */ 1511 table_ptr = rproc->table_ptr; 1512 1513 /* 1514 * If we made it here the remote processor was started by another 1515 * entity and a cache table doesn't exist. As such make a copy of 1516 * the resource table currently used by the remote processor and 1517 * use that for the rest of the shutdown process. The memory 1518 * allocated here is free'd in rproc_detach(). 1519 */ 1520 rproc->cached_table = kmemdup(rproc->table_ptr, 1521 rproc->table_sz, GFP_KERNEL); 1522 if (!rproc->cached_table) 1523 return -ENOMEM; 1524 1525 /* 1526 * Use a copy of the resource table for the remainder of the 1527 * shutdown process. 1528 */ 1529 rproc->table_ptr = rproc->cached_table; 1530 1531 /* 1532 * Reset the memory area where the firmware loaded the resource table 1533 * to its original value. That way when we re-attach the remote 1534 * processor the resource table is clean and ready to be used again. 1535 */ 1536 memcpy(table_ptr, rproc->clean_table, rproc->table_sz); 1537 1538 /* 1539 * The clean resource table is no longer needed. Allocated in 1540 * rproc_set_rsc_table(). 1541 */ 1542 kfree(rproc->clean_table); 1543 1544 return 0; 1545 } 1546 1547 static int rproc_reset_rsc_table_on_stop(struct rproc *rproc) 1548 { 1549 /* A resource table was never retrieved, nothing to do here */ 1550 if (!rproc->table_ptr) 1551 return 0; 1552 1553 /* 1554 * If a cache table exists the remote processor was started by 1555 * the remoteproc core. That cache table should be used for 1556 * the rest of the shutdown process. 1557 */ 1558 if (rproc->cached_table) 1559 goto out; 1560 1561 /* 1562 * If we made it here the remote processor was started by another 1563 * entity and a cache table doesn't exist. As such make a copy of 1564 * the resource table currently used by the remote processor and 1565 * use that for the rest of the shutdown process. The memory 1566 * allocated here is free'd in rproc_shutdown(). 1567 */ 1568 rproc->cached_table = kmemdup(rproc->table_ptr, 1569 rproc->table_sz, GFP_KERNEL); 1570 if (!rproc->cached_table) 1571 return -ENOMEM; 1572 1573 /* 1574 * Since the remote processor is being switched off the clean table 1575 * won't be needed. Allocated in rproc_set_rsc_table(). 1576 */ 1577 kfree(rproc->clean_table); 1578 1579 out: 1580 /* 1581 * Use a copy of the resource table for the remainder of the 1582 * shutdown process. 1583 */ 1584 rproc->table_ptr = rproc->cached_table; 1585 return 0; 1586 } 1587 1588 /* 1589 * Attach to remote processor - similar to rproc_fw_boot() but without 1590 * the steps that deal with the firmware image. 1591 */ 1592 static int rproc_attach(struct rproc *rproc) 1593 { 1594 struct device *dev = &rproc->dev; 1595 int ret; 1596 1597 /* 1598 * if enabling an IOMMU isn't relevant for this rproc, this is 1599 * just a nop 1600 */ 1601 ret = rproc_enable_iommu(rproc); 1602 if (ret) { 1603 dev_err(dev, "can't enable iommu: %d\n", ret); 1604 return ret; 1605 } 1606 1607 /* Do anything that is needed to boot the remote processor */ 1608 ret = rproc_prepare_device(rproc); 1609 if (ret) { 1610 dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret); 1611 goto disable_iommu; 1612 } 1613 1614 ret = rproc_set_rsc_table(rproc); 1615 if (ret) { 1616 dev_err(dev, "can't load resource table: %d\n", ret); 1617 goto clean_up_resources; 1618 } 1619 1620 /* reset max_notifyid */ 1621 rproc->max_notifyid = -1; 1622 1623 /* reset handled vdev */ 1624 rproc->nb_vdev = 0; 1625 1626 /* 1627 * Handle firmware resources required to attach to a remote processor. 1628 * Because we are attaching rather than booting the remote processor, 1629 * we expect the platform driver to properly set rproc->table_ptr. 1630 */ 1631 ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1632 if (ret) { 1633 dev_err(dev, "Failed to process resources: %d\n", ret); 1634 goto clean_up_resources; 1635 } 1636 1637 /* Allocate carveout resources associated to rproc */ 1638 ret = rproc_alloc_registered_carveouts(rproc); 1639 if (ret) { 1640 dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1641 ret); 1642 goto clean_up_resources; 1643 } 1644 1645 ret = __rproc_attach(rproc); 1646 if (ret) 1647 goto clean_up_resources; 1648 1649 return 0; 1650 1651 clean_up_resources: 1652 rproc_resource_cleanup(rproc); 1653 /* release HW resources if needed */ 1654 rproc_unprepare_device(rproc); 1655 kfree(rproc->clean_table); 1656 disable_iommu: 1657 rproc_disable_iommu(rproc); 1658 return ret; 1659 } 1660 1661 /* 1662 * take a firmware and boot it up. 1663 * 1664 * Note: this function is called asynchronously upon registration of the 1665 * remote processor (so we must wait until it completes before we try 1666 * to unregister the device. one other option is just to use kref here, 1667 * that might be cleaner). 1668 */ 1669 static void rproc_auto_boot_callback(const struct firmware *fw, void *context) 1670 { 1671 struct rproc *rproc = context; 1672 1673 rproc_boot(rproc); 1674 1675 release_firmware(fw); 1676 } 1677 1678 static void rproc_attach_work(struct work_struct *work) 1679 { 1680 struct rproc *rproc = container_of(work, struct rproc, attach_work); 1681 1682 rproc_boot(rproc); 1683 } 1684 1685 static int rproc_trigger_auto_boot(struct rproc *rproc) 1686 { 1687 int ret; 1688 1689 if (rproc->state == RPROC_DETACHED) { 1690 schedule_work(&rproc->attach_work); 1691 return 0; 1692 } 1693 1694 /* 1695 * We're initiating an asynchronous firmware loading, so we can 1696 * be built-in kernel code, without hanging the boot process. 1697 */ 1698 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT, 1699 rproc->firmware, &rproc->dev, GFP_KERNEL, 1700 rproc, rproc_auto_boot_callback); 1701 if (ret < 0) 1702 dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); 1703 1704 return ret; 1705 } 1706 1707 static int rproc_stop(struct rproc *rproc, bool crashed) 1708 { 1709 struct device *dev = &rproc->dev; 1710 int ret; 1711 1712 /* No need to continue if a stop() operation has not been provided */ 1713 if (!rproc->ops->stop) 1714 return -EINVAL; 1715 1716 /* Stop any subdevices for the remote processor */ 1717 rproc_stop_subdevices(rproc, crashed); 1718 1719 /* the installed resource table is no longer accessible */ 1720 ret = rproc_reset_rsc_table_on_stop(rproc); 1721 if (ret) { 1722 dev_err(dev, "can't reset resource table: %d\n", ret); 1723 return ret; 1724 } 1725 1726 1727 /* power off the remote processor */ 1728 ret = rproc->ops->stop(rproc); 1729 if (ret) { 1730 dev_err(dev, "can't stop rproc: %d\n", ret); 1731 return ret; 1732 } 1733 1734 rproc_unprepare_subdevices(rproc); 1735 1736 rproc->state = RPROC_OFFLINE; 1737 1738 dev_info(dev, "stopped remote processor %s\n", rproc->name); 1739 1740 return 0; 1741 } 1742 1743 /* 1744 * __rproc_detach(): Does the opposite of __rproc_attach() 1745 */ 1746 static int __rproc_detach(struct rproc *rproc) 1747 { 1748 struct device *dev = &rproc->dev; 1749 int ret; 1750 1751 /* No need to continue if a detach() operation has not been provided */ 1752 if (!rproc->ops->detach) 1753 return -EINVAL; 1754 1755 /* Stop any subdevices for the remote processor */ 1756 rproc_stop_subdevices(rproc, false); 1757 1758 /* the installed resource table is no longer accessible */ 1759 ret = rproc_reset_rsc_table_on_detach(rproc); 1760 if (ret) { 1761 dev_err(dev, "can't reset resource table: %d\n", ret); 1762 return ret; 1763 } 1764 1765 /* Tell the remote processor the core isn't available anymore */ 1766 ret = rproc->ops->detach(rproc); 1767 if (ret) { 1768 dev_err(dev, "can't detach from rproc: %d\n", ret); 1769 return ret; 1770 } 1771 1772 rproc_unprepare_subdevices(rproc); 1773 1774 rproc->state = RPROC_DETACHED; 1775 1776 dev_info(dev, "detached remote processor %s\n", rproc->name); 1777 1778 return 0; 1779 } 1780 1781 static int rproc_attach_recovery(struct rproc *rproc) 1782 { 1783 int ret; 1784 1785 ret = __rproc_detach(rproc); 1786 if (ret) 1787 return ret; 1788 1789 /* clean up all acquired resources */ 1790 rproc_resource_cleanup(rproc); 1791 1792 /* release HW resources if needed */ 1793 rproc_unprepare_device(rproc); 1794 1795 rproc_disable_iommu(rproc); 1796 1797 /* Free the copy of the resource table */ 1798 kfree(rproc->cached_table); 1799 rproc->cached_table = NULL; 1800 rproc->table_ptr = NULL; 1801 1802 return rproc_attach(rproc); 1803 } 1804 1805 static int rproc_boot_recovery(struct rproc *rproc) 1806 { 1807 const struct firmware *firmware_p; 1808 struct device *dev = &rproc->dev; 1809 int ret; 1810 1811 ret = rproc_stop(rproc, true); 1812 if (ret) 1813 return ret; 1814 1815 /* generate coredump */ 1816 rproc->ops->coredump(rproc); 1817 1818 /* load firmware */ 1819 ret = request_firmware(&firmware_p, rproc->firmware, dev); 1820 if (ret < 0) { 1821 dev_err(dev, "request_firmware failed: %d\n", ret); 1822 return ret; 1823 } 1824 1825 /* boot the remote processor up again */ 1826 ret = rproc_start(rproc, firmware_p); 1827 1828 release_firmware(firmware_p); 1829 1830 return ret; 1831 } 1832 1833 /** 1834 * rproc_trigger_recovery() - recover a remoteproc 1835 * @rproc: the remote processor 1836 * 1837 * The recovery is done by resetting all the virtio devices, that way all the 1838 * rpmsg drivers will be reseted along with the remote processor making the 1839 * remoteproc functional again. 1840 * 1841 * This function can sleep, so it cannot be called from atomic context. 1842 * 1843 * Return: 0 on success or a negative value upon failure 1844 */ 1845 int rproc_trigger_recovery(struct rproc *rproc) 1846 { 1847 struct device *dev = &rproc->dev; 1848 int ret; 1849 1850 ret = mutex_lock_interruptible(&rproc->lock); 1851 if (ret) 1852 return ret; 1853 1854 if (READ_ONCE(rproc->deleting)) { 1855 ret = -ENODEV; 1856 goto unlock_mutex; 1857 } 1858 1859 /* State could have changed before we got the mutex */ 1860 if (rproc->state != RPROC_CRASHED) 1861 goto unlock_mutex; 1862 1863 dev_err(dev, "recovering %s\n", rproc->name); 1864 1865 if (rproc_has_feature(rproc, RPROC_FEAT_ATTACH_ON_RECOVERY)) 1866 ret = rproc_attach_recovery(rproc); 1867 else 1868 ret = rproc_boot_recovery(rproc); 1869 1870 unlock_mutex: 1871 mutex_unlock(&rproc->lock); 1872 return ret; 1873 } 1874 1875 /** 1876 * rproc_crash_handler_work() - handle a crash 1877 * @work: work treating the crash 1878 * 1879 * This function needs to handle everything related to a crash, like cpu 1880 * registers and stack dump, information to help to debug the fatal error, etc. 1881 */ 1882 static void rproc_crash_handler_work(struct work_struct *work) 1883 { 1884 struct rproc *rproc = container_of(work, struct rproc, crash_handler); 1885 struct device *dev = &rproc->dev; 1886 1887 dev_dbg(dev, "enter %s\n", __func__); 1888 1889 mutex_lock(&rproc->lock); 1890 1891 if (READ_ONCE(rproc->deleting)) { 1892 mutex_unlock(&rproc->lock); 1893 goto out; 1894 } 1895 1896 if (rproc->state == RPROC_CRASHED) { 1897 /* handle only the first crash detected */ 1898 mutex_unlock(&rproc->lock); 1899 return; 1900 } 1901 1902 if (rproc->state == RPROC_OFFLINE) { 1903 /* Don't recover if the remote processor was stopped */ 1904 mutex_unlock(&rproc->lock); 1905 goto out; 1906 } 1907 1908 rproc->state = RPROC_CRASHED; 1909 dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, 1910 rproc->name); 1911 1912 mutex_unlock(&rproc->lock); 1913 1914 if (!rproc->recovery_disabled) 1915 rproc_trigger_recovery(rproc); 1916 1917 out: 1918 pm_relax(rproc->dev.parent); 1919 } 1920 1921 /** 1922 * rproc_boot() - boot a remote processor 1923 * @rproc: handle of a remote processor 1924 * 1925 * Boot a remote processor (i.e. load its firmware, power it on, ...). 1926 * 1927 * If the remote processor is already powered on, this function immediately 1928 * returns (successfully). 1929 * 1930 * Return: 0 on success, and an appropriate error value otherwise 1931 */ 1932 int rproc_boot(struct rproc *rproc) 1933 { 1934 const struct firmware *firmware_p; 1935 struct device *dev; 1936 int ret; 1937 1938 if (!rproc) { 1939 pr_err("invalid rproc handle\n"); 1940 return -EINVAL; 1941 } 1942 1943 dev = &rproc->dev; 1944 1945 ret = mutex_lock_interruptible(&rproc->lock); 1946 if (ret) { 1947 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1948 return ret; 1949 } 1950 1951 if (READ_ONCE(rproc->deleting)) { 1952 ret = -ENODEV; 1953 dev_err(dev, "can't boot deleting rproc %s\n", rproc->name); 1954 goto unlock_mutex; 1955 } 1956 1957 /* skip the boot or attach process if rproc is already powered up */ 1958 if (atomic_inc_return(&rproc->power) > 1) { 1959 ret = 0; 1960 goto unlock_mutex; 1961 } 1962 1963 if (rproc->state == RPROC_DETACHED) { 1964 dev_info(dev, "attaching to %s\n", rproc->name); 1965 1966 ret = rproc_attach(rproc); 1967 } else { 1968 dev_info(dev, "powering up %s\n", rproc->name); 1969 1970 /* load firmware */ 1971 ret = request_firmware(&firmware_p, rproc->firmware, dev); 1972 if (ret < 0) { 1973 dev_err(dev, "request_firmware failed: %d\n", ret); 1974 goto downref_rproc; 1975 } 1976 1977 ret = rproc_fw_boot(rproc, firmware_p); 1978 1979 release_firmware(firmware_p); 1980 } 1981 1982 downref_rproc: 1983 if (ret) 1984 atomic_dec(&rproc->power); 1985 unlock_mutex: 1986 mutex_unlock(&rproc->lock); 1987 return ret; 1988 } 1989 EXPORT_SYMBOL(rproc_boot); 1990 1991 static int __rproc_shutdown(struct rproc *rproc, bool force) 1992 { 1993 struct device *dev = &rproc->dev; 1994 bool crashed; 1995 int ret; 1996 1997 ret = mutex_lock_interruptible(&rproc->lock); 1998 if (ret) { 1999 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 2000 return ret; 2001 } 2002 2003 if (rproc->state != RPROC_RUNNING && 2004 rproc->state != RPROC_ATTACHED && 2005 rproc->state != RPROC_CRASHED) { 2006 ret = -EINVAL; 2007 goto out; 2008 } 2009 crashed = rproc->state == RPROC_CRASHED; 2010 2011 if (!atomic_dec_and_test(&rproc->power) && !force) { 2012 /* The remote processor is still needed by another user. */ 2013 goto out; 2014 } 2015 2016 ret = rproc_stop(rproc, crashed); 2017 if (ret) { 2018 atomic_inc(&rproc->power); 2019 goto out; 2020 } 2021 2022 /* clean up all acquired resources */ 2023 rproc_resource_cleanup(rproc); 2024 2025 /* release HW resources if needed */ 2026 rproc_unprepare_device(rproc); 2027 2028 rproc_disable_iommu(rproc); 2029 2030 /* Free the copy of the resource table */ 2031 kfree(rproc->cached_table); 2032 rproc->cached_table = NULL; 2033 rproc->table_ptr = NULL; 2034 out: 2035 mutex_unlock(&rproc->lock); 2036 return ret; 2037 } 2038 2039 /** 2040 * rproc_shutdown() - power off the remote processor 2041 * @rproc: the remote processor 2042 * 2043 * Power off a remote processor (previously booted with rproc_boot()). 2044 * 2045 * In case @rproc is still being used by an additional user(s), then 2046 * this function will just decrement the power refcount and exit, 2047 * without really powering off the device. 2048 * 2049 * Every call to rproc_boot() must (eventually) be accompanied by a call 2050 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. 2051 * 2052 * Notes: 2053 * - we're not decrementing the rproc's refcount, only the power refcount. 2054 * which means that the @rproc handle stays valid even after rproc_shutdown() 2055 * returns, and users can still use it with a subsequent rproc_boot(), if 2056 * needed. 2057 * 2058 * Return: 0 on success, and an appropriate error value otherwise 2059 */ 2060 int rproc_shutdown(struct rproc *rproc) 2061 { 2062 return __rproc_shutdown(rproc, false); 2063 } 2064 EXPORT_SYMBOL(rproc_shutdown); 2065 2066 /** 2067 * rproc_detach() - Detach the remote processor from the 2068 * remoteproc core 2069 * 2070 * @rproc: the remote processor 2071 * 2072 * Detach a remote processor (previously attached to with rproc_attach()). 2073 * 2074 * In case @rproc is still being used by an additional user(s), then 2075 * this function will just decrement the power refcount and exit, 2076 * without disconnecting the device. 2077 * 2078 * Function rproc_detach() calls __rproc_detach() in order to let a remote 2079 * processor know that services provided by the application processor are 2080 * no longer available. From there it should be possible to remove the 2081 * platform driver and even power cycle the application processor (if the HW 2082 * supports it) without needing to switch off the remote processor. 2083 * 2084 * Return: 0 on success, and an appropriate error value otherwise 2085 */ 2086 int rproc_detach(struct rproc *rproc) 2087 { 2088 struct device *dev = &rproc->dev; 2089 int ret; 2090 2091 ret = mutex_lock_interruptible(&rproc->lock); 2092 if (ret) { 2093 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 2094 return ret; 2095 } 2096 2097 if (rproc->state != RPROC_ATTACHED) { 2098 ret = -EINVAL; 2099 goto out; 2100 } 2101 2102 /* if the remote proc is still needed, bail out */ 2103 if (!atomic_dec_and_test(&rproc->power)) { 2104 ret = 0; 2105 goto out; 2106 } 2107 2108 ret = __rproc_detach(rproc); 2109 if (ret) { 2110 atomic_inc(&rproc->power); 2111 goto out; 2112 } 2113 2114 /* clean up all acquired resources */ 2115 rproc_resource_cleanup(rproc); 2116 2117 /* release HW resources if needed */ 2118 rproc_unprepare_device(rproc); 2119 2120 rproc_disable_iommu(rproc); 2121 2122 /* Free the copy of the resource table */ 2123 kfree(rproc->cached_table); 2124 rproc->cached_table = NULL; 2125 rproc->table_ptr = NULL; 2126 out: 2127 mutex_unlock(&rproc->lock); 2128 return ret; 2129 } 2130 EXPORT_SYMBOL(rproc_detach); 2131 2132 /** 2133 * rproc_get_by_phandle() - find a remote processor by phandle 2134 * @phandle: phandle to the rproc 2135 * 2136 * Finds an rproc handle using the remote processor's phandle, and then 2137 * return a handle to the rproc. 2138 * 2139 * This function increments the remote processor's refcount, so always 2140 * use rproc_put() to decrement it back once rproc isn't needed anymore. 2141 * 2142 * Return: rproc handle on success, and NULL on failure 2143 */ 2144 #ifdef CONFIG_OF 2145 struct rproc *rproc_get_by_phandle(phandle phandle) 2146 { 2147 struct rproc *rproc = NULL, *r; 2148 struct device_driver *driver; 2149 struct device_node *np; 2150 2151 np = of_find_node_by_phandle(phandle); 2152 if (!np) 2153 return NULL; 2154 2155 rcu_read_lock(); 2156 list_for_each_entry_rcu(r, &rproc_list, node) { 2157 if (r->dev.parent && device_match_of_node(r->dev.parent, np)) { 2158 /* prevent underlying implementation from being removed */ 2159 2160 /* 2161 * If the remoteproc's parent has a driver, the 2162 * remoteproc is not part of a cluster and we can use 2163 * that driver. 2164 */ 2165 driver = r->dev.parent->driver; 2166 2167 /* 2168 * If the remoteproc's parent does not have a driver, 2169 * look for the driver associated with the cluster. 2170 */ 2171 if (!driver) { 2172 if (r->dev.parent->parent) 2173 driver = r->dev.parent->parent->driver; 2174 if (!driver) 2175 break; 2176 } 2177 2178 if (!try_module_get(driver->owner)) { 2179 dev_err(&r->dev, "can't get owner\n"); 2180 break; 2181 } 2182 2183 rproc = r; 2184 get_device(&rproc->dev); 2185 break; 2186 } 2187 } 2188 rcu_read_unlock(); 2189 2190 of_node_put(np); 2191 2192 return rproc; 2193 } 2194 #else 2195 struct rproc *rproc_get_by_phandle(phandle phandle) 2196 { 2197 return NULL; 2198 } 2199 #endif 2200 EXPORT_SYMBOL(rproc_get_by_phandle); 2201 2202 /** 2203 * rproc_set_firmware() - assign a new firmware 2204 * @rproc: rproc handle to which the new firmware is being assigned 2205 * @fw_name: new firmware name to be assigned 2206 * 2207 * This function allows remoteproc drivers or clients to configure a custom 2208 * firmware name that is different from the default name used during remoteproc 2209 * registration. The function does not trigger a remote processor boot, 2210 * only sets the firmware name used for a subsequent boot. This function 2211 * should also be called only when the remote processor is offline. 2212 * 2213 * This allows either the userspace to configure a different name through 2214 * sysfs or a kernel-level remoteproc or a remoteproc client driver to set 2215 * a specific firmware when it is controlling the boot and shutdown of the 2216 * remote processor. 2217 * 2218 * Return: 0 on success or a negative value upon failure 2219 */ 2220 int rproc_set_firmware(struct rproc *rproc, const char *fw_name) 2221 { 2222 struct device *dev; 2223 int ret, len; 2224 char *p; 2225 2226 if (!rproc || !fw_name) 2227 return -EINVAL; 2228 2229 dev = rproc->dev.parent; 2230 2231 ret = mutex_lock_interruptible(&rproc->lock); 2232 if (ret) { 2233 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 2234 return -EINVAL; 2235 } 2236 2237 if (rproc->state != RPROC_OFFLINE) { 2238 dev_err(dev, "can't change firmware while running\n"); 2239 ret = -EBUSY; 2240 goto out; 2241 } 2242 2243 len = strcspn(fw_name, "\n"); 2244 if (!len) { 2245 dev_err(dev, "can't provide empty string for firmware name\n"); 2246 ret = -EINVAL; 2247 goto out; 2248 } 2249 2250 p = kstrndup(fw_name, len, GFP_KERNEL); 2251 if (!p) { 2252 ret = -ENOMEM; 2253 goto out; 2254 } 2255 2256 kfree_const(rproc->firmware); 2257 rproc->firmware = p; 2258 2259 out: 2260 mutex_unlock(&rproc->lock); 2261 return ret; 2262 } 2263 EXPORT_SYMBOL(rproc_set_firmware); 2264 2265 static int rproc_validate(struct rproc *rproc) 2266 { 2267 switch (rproc->state) { 2268 case RPROC_OFFLINE: 2269 /* 2270 * An offline processor without a start() 2271 * function makes no sense. 2272 */ 2273 if (!rproc->ops->start) 2274 return -EINVAL; 2275 break; 2276 case RPROC_DETACHED: 2277 /* 2278 * A remote processor in a detached state without an 2279 * attach() function makes not sense. 2280 */ 2281 if (!rproc->ops->attach) 2282 return -EINVAL; 2283 /* 2284 * When attaching to a remote processor the device memory 2285 * is already available and as such there is no need to have a 2286 * cached table. 2287 */ 2288 if (rproc->cached_table) 2289 return -EINVAL; 2290 break; 2291 default: 2292 /* 2293 * When adding a remote processor, the state of the device 2294 * can be offline or detached, nothing else. 2295 */ 2296 return -EINVAL; 2297 } 2298 2299 return 0; 2300 } 2301 2302 /** 2303 * rproc_add() - register a remote processor 2304 * @rproc: the remote processor handle to register 2305 * 2306 * Registers @rproc with the remoteproc framework, after it has been 2307 * allocated with rproc_alloc(). 2308 * 2309 * This is called by the platform-specific rproc implementation, whenever 2310 * a new remote processor device is probed. 2311 * 2312 * Note: this function initiates an asynchronous firmware loading 2313 * context, which will look for virtio devices supported by the rproc's 2314 * firmware. 2315 * 2316 * If found, those virtio devices will be created and added, so as a result 2317 * of registering this remote processor, additional virtio drivers might be 2318 * probed. 2319 * 2320 * Return: 0 on success and an appropriate error code otherwise 2321 */ 2322 int rproc_add(struct rproc *rproc) 2323 { 2324 struct device *dev = &rproc->dev; 2325 int ret; 2326 2327 ret = rproc_validate(rproc); 2328 if (ret < 0) 2329 return ret; 2330 2331 /* add char device for this remoteproc */ 2332 ret = rproc_char_device_add(rproc); 2333 if (ret < 0) 2334 return ret; 2335 2336 ret = device_add(dev); 2337 if (ret < 0) { 2338 put_device(dev); 2339 goto rproc_remove_cdev; 2340 } 2341 2342 dev_info(dev, "%s is available\n", rproc->name); 2343 2344 /* create debugfs entries */ 2345 rproc_create_debug_dir(rproc); 2346 2347 /* if rproc is marked always-on, request it to boot */ 2348 if (rproc->auto_boot) { 2349 ret = rproc_trigger_auto_boot(rproc); 2350 if (ret < 0) 2351 goto rproc_remove_dev; 2352 } 2353 2354 /* expose to rproc_get_by_phandle users */ 2355 mutex_lock(&rproc_list_mutex); 2356 list_add_rcu(&rproc->node, &rproc_list); 2357 mutex_unlock(&rproc_list_mutex); 2358 2359 return 0; 2360 2361 rproc_remove_dev: 2362 cancel_work_sync(&rproc->crash_handler); 2363 rproc_delete_debug_dir(rproc); 2364 device_del(dev); 2365 rproc_remove_cdev: 2366 rproc_char_device_remove(rproc); 2367 return ret; 2368 } 2369 EXPORT_SYMBOL(rproc_add); 2370 2371 static void devm_rproc_remove(void *rproc) 2372 { 2373 rproc_del(rproc); 2374 } 2375 2376 /** 2377 * devm_rproc_add() - resource managed rproc_add() 2378 * @dev: the underlying device 2379 * @rproc: the remote processor handle to register 2380 * 2381 * This function performs like rproc_add() but the registered rproc device will 2382 * automatically be removed on driver detach. 2383 * 2384 * Return: 0 on success, negative errno on failure 2385 */ 2386 int devm_rproc_add(struct device *dev, struct rproc *rproc) 2387 { 2388 int err; 2389 2390 err = rproc_add(rproc); 2391 if (err) 2392 return err; 2393 2394 return devm_add_action_or_reset(dev, devm_rproc_remove, rproc); 2395 } 2396 EXPORT_SYMBOL(devm_rproc_add); 2397 2398 /** 2399 * rproc_type_release() - release a remote processor instance 2400 * @dev: the rproc's device 2401 * 2402 * This function should _never_ be called directly. 2403 * 2404 * It will be called by the driver core when no one holds a valid pointer 2405 * to @dev anymore. 2406 */ 2407 static void rproc_type_release(struct device *dev) 2408 { 2409 struct rproc *rproc = container_of(dev, struct rproc, dev); 2410 2411 dev_info(&rproc->dev, "releasing %s\n", rproc->name); 2412 2413 idr_destroy(&rproc->notifyids); 2414 2415 if (rproc->index >= 0) 2416 ida_free(&rproc_dev_index, rproc->index); 2417 2418 kfree_const(rproc->firmware); 2419 kfree_const(rproc->name); 2420 kfree(rproc->ops); 2421 kfree(rproc); 2422 } 2423 2424 static const struct device_type rproc_type = { 2425 .name = "remoteproc", 2426 .release = rproc_type_release, 2427 }; 2428 2429 static int rproc_alloc_firmware(struct rproc *rproc, 2430 const char *name, const char *firmware) 2431 { 2432 const char *p; 2433 2434 /* 2435 * Allocate a firmware name if the caller gave us one to work 2436 * with. Otherwise construct a new one using a default pattern. 2437 */ 2438 if (firmware) 2439 p = kstrdup_const(firmware, GFP_KERNEL); 2440 else 2441 p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name); 2442 2443 if (!p) 2444 return -ENOMEM; 2445 2446 rproc->firmware = p; 2447 2448 return 0; 2449 } 2450 2451 static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops) 2452 { 2453 rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); 2454 if (!rproc->ops) 2455 return -ENOMEM; 2456 2457 /* Default to rproc_coredump if no coredump function is specified */ 2458 if (!rproc->ops->coredump) 2459 rproc->ops->coredump = rproc_coredump; 2460 2461 if (rproc->ops->load) 2462 return 0; 2463 2464 /* Default to ELF loader if no load function is specified */ 2465 rproc->ops->load = rproc_elf_load_segments; 2466 rproc->ops->parse_fw = rproc_elf_load_rsc_table; 2467 rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; 2468 rproc->ops->sanity_check = rproc_elf_sanity_check; 2469 rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; 2470 2471 return 0; 2472 } 2473 2474 /** 2475 * rproc_alloc() - allocate a remote processor handle 2476 * @dev: the underlying device 2477 * @name: name of this remote processor 2478 * @ops: platform-specific handlers (mainly start/stop) 2479 * @firmware: name of firmware file to load, can be NULL 2480 * @len: length of private data needed by the rproc driver (in bytes) 2481 * 2482 * Allocates a new remote processor handle, but does not register 2483 * it yet. if @firmware is NULL, a default name is used. 2484 * 2485 * This function should be used by rproc implementations during initialization 2486 * of the remote processor. 2487 * 2488 * After creating an rproc handle using this function, and when ready, 2489 * implementations should then call rproc_add() to complete 2490 * the registration of the remote processor. 2491 * 2492 * Note: _never_ directly deallocate @rproc, even if it was not registered 2493 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free(). 2494 * 2495 * Return: new rproc pointer on success, and NULL on failure 2496 */ 2497 struct rproc *rproc_alloc(struct device *dev, const char *name, 2498 const struct rproc_ops *ops, 2499 const char *firmware, int len) 2500 { 2501 struct rproc *rproc; 2502 2503 if (!dev || !name || !ops) 2504 return NULL; 2505 2506 rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL); 2507 if (!rproc) 2508 return NULL; 2509 2510 rproc->priv = &rproc[1]; 2511 rproc->auto_boot = true; 2512 rproc->elf_class = ELFCLASSNONE; 2513 rproc->elf_machine = EM_NONE; 2514 2515 device_initialize(&rproc->dev); 2516 rproc->dev.parent = dev; 2517 rproc->dev.type = &rproc_type; 2518 rproc->dev.class = &rproc_class; 2519 rproc->dev.driver_data = rproc; 2520 idr_init(&rproc->notifyids); 2521 2522 /* Assign a unique device index and name */ 2523 rproc->index = ida_alloc(&rproc_dev_index, GFP_KERNEL); 2524 if (rproc->index < 0) { 2525 dev_err(dev, "ida_alloc failed: %d\n", rproc->index); 2526 goto put_device; 2527 } 2528 2529 rproc->name = kstrdup_const(name, GFP_KERNEL); 2530 if (!rproc->name) 2531 goto put_device; 2532 2533 if (rproc_alloc_firmware(rproc, name, firmware)) 2534 goto put_device; 2535 2536 if (rproc_alloc_ops(rproc, ops)) 2537 goto put_device; 2538 2539 dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); 2540 2541 atomic_set(&rproc->power, 0); 2542 2543 mutex_init(&rproc->lock); 2544 2545 INIT_LIST_HEAD(&rproc->carveouts); 2546 INIT_LIST_HEAD(&rproc->mappings); 2547 INIT_LIST_HEAD(&rproc->traces); 2548 INIT_LIST_HEAD(&rproc->rvdevs); 2549 INIT_LIST_HEAD(&rproc->subdevs); 2550 INIT_LIST_HEAD(&rproc->dump_segments); 2551 2552 INIT_WORK(&rproc->attach_work, rproc_attach_work); 2553 INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); 2554 spin_lock_init(&rproc->crash_handler_lock); 2555 2556 rproc->state = RPROC_OFFLINE; 2557 2558 return rproc; 2559 2560 put_device: 2561 put_device(&rproc->dev); 2562 return NULL; 2563 } 2564 EXPORT_SYMBOL(rproc_alloc); 2565 2566 /** 2567 * rproc_free() - unroll rproc_alloc() 2568 * @rproc: the remote processor handle 2569 * 2570 * This function decrements the rproc dev refcount. 2571 * 2572 * If no one holds any reference to rproc anymore, then its refcount would 2573 * now drop to zero, and it would be freed. 2574 */ 2575 void rproc_free(struct rproc *rproc) 2576 { 2577 put_device(&rproc->dev); 2578 } 2579 EXPORT_SYMBOL(rproc_free); 2580 2581 /** 2582 * rproc_put() - release rproc reference 2583 * @rproc: the remote processor handle 2584 * 2585 * This function decrements the rproc dev refcount. 2586 * 2587 * If no one holds any reference to rproc anymore, then its refcount would 2588 * now drop to zero, and it would be freed. 2589 */ 2590 void rproc_put(struct rproc *rproc) 2591 { 2592 if (rproc->dev.parent->driver) 2593 module_put(rproc->dev.parent->driver->owner); 2594 else 2595 module_put(rproc->dev.parent->parent->driver->owner); 2596 2597 put_device(&rproc->dev); 2598 } 2599 EXPORT_SYMBOL(rproc_put); 2600 2601 /** 2602 * rproc_del() - unregister a remote processor 2603 * @rproc: rproc handle to unregister 2604 * 2605 * This function should be called when the platform specific rproc 2606 * implementation decides to remove the rproc device. it should 2607 * _only_ be called if a previous invocation of rproc_add() 2608 * has completed successfully. 2609 * 2610 * After rproc_del() returns, @rproc isn't freed yet, because 2611 * of the outstanding reference created by rproc_alloc. To decrement that 2612 * one last refcount, one still needs to call rproc_free(). 2613 * 2614 * Return: 0 on success and -EINVAL if @rproc isn't valid 2615 */ 2616 int rproc_del(struct rproc *rproc) 2617 { 2618 unsigned long flags; 2619 2620 if (!rproc) 2621 return -EINVAL; 2622 2623 spin_lock_irqsave(&rproc->crash_handler_lock, flags); 2624 WRITE_ONCE(rproc->deleting, true); 2625 spin_unlock_irqrestore(&rproc->crash_handler_lock, flags); 2626 2627 if (cancel_work_sync(&rproc->crash_handler)) 2628 pm_relax(rproc->dev.parent); 2629 2630 __rproc_shutdown(rproc, true); 2631 2632 rproc_delete_debug_dir(rproc); 2633 2634 /* the rproc is downref'ed as soon as it's removed from the klist */ 2635 mutex_lock(&rproc_list_mutex); 2636 list_del_rcu(&rproc->node); 2637 mutex_unlock(&rproc_list_mutex); 2638 2639 /* Ensure that no readers of rproc_list are still active */ 2640 synchronize_rcu(); 2641 2642 device_del(&rproc->dev); 2643 rproc_char_device_remove(rproc); 2644 2645 return 0; 2646 } 2647 EXPORT_SYMBOL(rproc_del); 2648 2649 static void devm_rproc_free(struct device *dev, void *res) 2650 { 2651 rproc_free(*(struct rproc **)res); 2652 } 2653 2654 /** 2655 * devm_rproc_alloc() - resource managed rproc_alloc() 2656 * @dev: the underlying device 2657 * @name: name of this remote processor 2658 * @ops: platform-specific handlers (mainly start/stop) 2659 * @firmware: name of firmware file to load, can be NULL 2660 * @len: length of private data needed by the rproc driver (in bytes) 2661 * 2662 * This function performs like rproc_alloc() but the acquired rproc device will 2663 * automatically be released on driver detach. 2664 * 2665 * Return: new rproc instance, or NULL on failure 2666 */ 2667 struct rproc *devm_rproc_alloc(struct device *dev, const char *name, 2668 const struct rproc_ops *ops, 2669 const char *firmware, int len) 2670 { 2671 struct rproc **ptr, *rproc; 2672 2673 ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL); 2674 if (!ptr) 2675 return NULL; 2676 2677 rproc = rproc_alloc(dev, name, ops, firmware, len); 2678 if (rproc) { 2679 *ptr = rproc; 2680 devres_add(dev, ptr); 2681 } else { 2682 devres_free(ptr); 2683 } 2684 2685 return rproc; 2686 } 2687 EXPORT_SYMBOL(devm_rproc_alloc); 2688 2689 /** 2690 * rproc_add_subdev() - add a subdevice to a remoteproc 2691 * @rproc: rproc handle to add the subdevice to 2692 * @subdev: subdev handle to register 2693 * 2694 * Caller is responsible for populating optional subdevice function pointers. 2695 */ 2696 void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 2697 { 2698 list_add_tail(&subdev->node, &rproc->subdevs); 2699 } 2700 EXPORT_SYMBOL(rproc_add_subdev); 2701 2702 /** 2703 * rproc_remove_subdev() - remove a subdevice from a remoteproc 2704 * @rproc: rproc handle to remove the subdevice from 2705 * @subdev: subdev handle, previously registered with rproc_add_subdev() 2706 */ 2707 void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 2708 { 2709 list_del(&subdev->node); 2710 } 2711 EXPORT_SYMBOL(rproc_remove_subdev); 2712 2713 /** 2714 * rproc_get_by_child() - acquire rproc handle of @dev's ancestor 2715 * @dev: child device to find ancestor of 2716 * 2717 * Return: the ancestor rproc instance, or NULL if not found 2718 */ 2719 struct rproc *rproc_get_by_child(struct device *dev) 2720 { 2721 for (dev = dev->parent; dev; dev = dev->parent) { 2722 if (dev->type == &rproc_type) 2723 return dev->driver_data; 2724 } 2725 2726 return NULL; 2727 } 2728 EXPORT_SYMBOL(rproc_get_by_child); 2729 2730 /** 2731 * rproc_report_crash() - rproc crash reporter function 2732 * @rproc: remote processor 2733 * @type: crash type 2734 * 2735 * This function must be called every time a crash is detected by the low-level 2736 * drivers implementing a specific remoteproc. This should not be called from a 2737 * non-remoteproc driver. 2738 * 2739 * This function can be called from atomic/interrupt context. 2740 */ 2741 void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) 2742 { 2743 unsigned long flags; 2744 2745 if (!rproc) { 2746 pr_err("NULL rproc pointer\n"); 2747 return; 2748 } 2749 2750 spin_lock_irqsave(&rproc->crash_handler_lock, flags); 2751 if (READ_ONCE(rproc->deleting)) { 2752 spin_unlock_irqrestore(&rproc->crash_handler_lock, flags); 2753 return; 2754 } 2755 2756 /* Prevent suspend while the remoteproc is being recovered */ 2757 pm_stay_awake(rproc->dev.parent); 2758 queue_work(rproc_recovery_wq, &rproc->crash_handler); 2759 spin_unlock_irqrestore(&rproc->crash_handler_lock, flags); 2760 2761 dev_err(&rproc->dev, "crash detected in %s: type %s\n", 2762 rproc->name, rproc_crash_to_string(type)); 2763 } 2764 EXPORT_SYMBOL(rproc_report_crash); 2765 2766 static int rproc_panic_handler(struct notifier_block *nb, unsigned long event, 2767 void *ptr) 2768 { 2769 unsigned int longest = 0; 2770 struct rproc *rproc; 2771 unsigned int d; 2772 2773 rcu_read_lock(); 2774 list_for_each_entry_rcu(rproc, &rproc_list, node) { 2775 if (!rproc->ops->panic) 2776 continue; 2777 2778 if (rproc->state != RPROC_RUNNING && 2779 rproc->state != RPROC_ATTACHED) 2780 continue; 2781 2782 d = rproc->ops->panic(rproc); 2783 longest = max(longest, d); 2784 } 2785 rcu_read_unlock(); 2786 2787 /* 2788 * Delay for the longest requested duration before returning. This can 2789 * be used by the remoteproc drivers to give the remote processor time 2790 * to perform any requested operations (such as flush caches), when 2791 * it's not possible to signal the Linux side due to the panic. 2792 */ 2793 mdelay(longest); 2794 2795 return NOTIFY_DONE; 2796 } 2797 2798 static void __init rproc_init_panic(void) 2799 { 2800 rproc_panic_nb.notifier_call = rproc_panic_handler; 2801 atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb); 2802 } 2803 2804 static void __exit rproc_exit_panic(void) 2805 { 2806 atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb); 2807 } 2808 2809 static int __init remoteproc_init(void) 2810 { 2811 rproc_recovery_wq = alloc_workqueue("rproc_recovery_wq", 2812 WQ_UNBOUND | WQ_FREEZABLE, 0); 2813 if (!rproc_recovery_wq) { 2814 pr_err("remoteproc: creation of rproc_recovery_wq failed\n"); 2815 return -ENOMEM; 2816 } 2817 2818 rproc_init_sysfs(); 2819 rproc_init_debugfs(); 2820 rproc_init_cdev(); 2821 rproc_init_panic(); 2822 2823 return 0; 2824 } 2825 subsys_initcall(remoteproc_init); 2826 2827 static void __exit remoteproc_exit(void) 2828 { 2829 ida_destroy(&rproc_dev_index); 2830 2831 if (!rproc_recovery_wq) 2832 return; 2833 2834 rproc_exit_panic(); 2835 rproc_exit_debugfs(); 2836 rproc_exit_sysfs(); 2837 destroy_workqueue(rproc_recovery_wq); 2838 } 2839 module_exit(remoteproc_exit); 2840 2841 MODULE_DESCRIPTION("Generic Remote Processor Framework"); 2842