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