Lines Matching +full:dma +full:- +full:mem
1 // SPDX-License-Identifier: GPL-2.0-only
8 * Ohad Ben-Cohen <ohad@wizery.com>
12 * Suman Anna <s-anna@ti.com>
26 #include <linux/dma-mapping.h>
55 struct rproc_mem_entry *mem);
57 struct rproc_mem_entry *mem);
98 return -ENOSYS; in rproc_iommu_fault()
104 struct device *dev = rproc->dev.parent; in rproc_enable_iommu()
107 if (!rproc->has_iommu) { in rproc_enable_iommu()
112 domain = iommu_domain_alloc(dev->bus); in rproc_enable_iommu()
115 return -ENOMEM; in rproc_enable_iommu()
126 rproc->domain = domain; in rproc_enable_iommu()
137 struct iommu_domain *domain = rproc->domain; in rproc_disable_iommu()
138 struct device *dev = rproc->dev.parent; in rproc_disable_iommu()
151 * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent in rproc_va_to_pa()
152 * - in kernel: if region allocated in generic dma memory pool in rproc_va_to_pa()
165 * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
187 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
189 * here the output of the DMA API for the carveouts, which should be more
199 if (rproc->ops->da_to_va) { in rproc_da_to_va()
200 ptr = rproc->ops->da_to_va(rproc, da, len, is_iomem); in rproc_da_to_va()
205 list_for_each_entry(carveout, &rproc->carveouts, node) { in rproc_da_to_va()
206 int offset = da - carveout->da; in rproc_da_to_va()
209 if (!carveout->va) in rproc_da_to_va()
217 if (offset + len > carveout->len) in rproc_da_to_va()
220 ptr = carveout->va + offset; in rproc_da_to_va()
223 *is_iomem = carveout->is_iomem; in rproc_da_to_va()
234 * rproc_find_carveout_by_name() - lookup the carveout region by a name
239 * Platform driver has the capability to register some pre-allacoted carveout
257 struct rproc_mem_entry *carveout, *mem = NULL; in rproc_find_carveout_by_name() local
266 list_for_each_entry(carveout, &rproc->carveouts, node) { in rproc_find_carveout_by_name()
268 if (!strcmp(carveout->name, _name)) { in rproc_find_carveout_by_name()
269 mem = carveout; in rproc_find_carveout_by_name()
274 return mem; in rproc_find_carveout_by_name()
278 * rproc_check_carveout_da() - Check specified carveout da configuration
280 * @mem: pointer on carveout to check
292 struct rproc_mem_entry *mem, u32 da, u32 len) in rproc_check_carveout_da() argument
294 struct device *dev = &rproc->dev; in rproc_check_carveout_da()
298 if (len > mem->len) { in rproc_check_carveout_da()
300 return -EINVAL; in rproc_check_carveout_da()
303 if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) { in rproc_check_carveout_da()
305 return -EINVAL; in rproc_check_carveout_da()
306 } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) { in rproc_check_carveout_da()
307 delta = da - mem->da; in rproc_check_carveout_da()
313 return -EINVAL; in rproc_check_carveout_da()
316 if (delta + len > mem->len) { in rproc_check_carveout_da()
319 return -EINVAL; in rproc_check_carveout_da()
328 struct rproc *rproc = rvdev->rproc; in rproc_alloc_vring()
329 struct device *dev = &rproc->dev; in rproc_alloc_vring()
330 struct rproc_vring *rvring = &rvdev->vring[i]; in rproc_alloc_vring()
333 struct rproc_mem_entry *mem; in rproc_alloc_vring() local
337 size = PAGE_ALIGN(vring_size(rvring->num, rvring->align)); in rproc_alloc_vring()
339 rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; in rproc_alloc_vring()
341 /* Search for pre-registered carveout */ in rproc_alloc_vring()
342 mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index, in rproc_alloc_vring()
344 if (mem) { in rproc_alloc_vring()
345 if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size)) in rproc_alloc_vring()
346 return -ENOMEM; in rproc_alloc_vring()
349 mem = rproc_mem_entry_init(dev, NULL, 0, in rproc_alloc_vring()
350 size, rsc->vring[i].da, in rproc_alloc_vring()
354 rvdev->index, i); in rproc_alloc_vring()
355 if (!mem) { in rproc_alloc_vring()
357 return -ENOMEM; in rproc_alloc_vring()
360 rproc_add_carveout(rproc, mem); in rproc_alloc_vring()
364 * Assign an rproc-wide unique index for this vring in rproc_alloc_vring()
368 ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL); in rproc_alloc_vring()
376 if (notifyid > rproc->max_notifyid) in rproc_alloc_vring()
377 rproc->max_notifyid = notifyid; in rproc_alloc_vring()
379 rvring->notifyid = notifyid; in rproc_alloc_vring()
382 rsc->vring[i].notifyid = notifyid; in rproc_alloc_vring()
389 struct rproc *rproc = rvdev->rproc; in rproc_parse_vring()
390 struct device *dev = &rproc->dev; in rproc_parse_vring()
391 struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; in rproc_parse_vring()
392 struct rproc_vring *rvring = &rvdev->vring[i]; in rproc_parse_vring()
395 i, vring->da, vring->num, vring->align); in rproc_parse_vring()
398 if (!vring->num || !vring->align) { in rproc_parse_vring()
400 vring->num, vring->align); in rproc_parse_vring()
401 return -EINVAL; in rproc_parse_vring()
404 rvring->num = vring->num; in rproc_parse_vring()
405 rvring->align = vring->align; in rproc_parse_vring()
406 rvring->rvdev = rvdev; in rproc_parse_vring()
413 struct rproc *rproc = rvring->rvdev->rproc; in rproc_free_vring()
414 int idx = rvring - rvring->rvdev->vring; in rproc_free_vring()
417 idr_remove(&rproc->notifyids, rvring->notifyid); in rproc_free_vring()
422 * such and as per rproc_stop(), rproc->table_ptr points to the cached in rproc_free_vring()
423 * resource table (rproc->cached_table). The cached resource table is in rproc_free_vring()
430 if (rproc->table_ptr) { in rproc_free_vring()
431 rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; in rproc_free_vring()
432 rsc->vring[idx].da = 0; in rproc_free_vring()
433 rsc->vring[idx].notifyid = -1; in rproc_free_vring()
440 list_add_tail(&rvdev->node, &rproc->rvdevs); in rproc_add_rvdev()
446 list_del(&rvdev->node); in rproc_remove_rvdev()
449 * rproc_handle_vdev() - handle a vdev fw resource
460 * Before registering the vdev, the vrings are allocated from non-cacheable
463 * doing the vring allocation only later when ->find_vqs() is invoked, and
464 * then release them upon ->del_vqs().
467 * allocate it using the DMA API, ignoring requested hard coded addresses,
469 * going to be taken care of when the generic iommu-based DMA API will be
470 * merged. Meanwhile, statically-addressed iommu-based firmware images should
480 struct device *dev = &rproc->dev; in rproc_handle_vdev()
487 rsc_size = struct_size(rsc, vring, rsc->num_of_vrings); in rproc_handle_vdev()
488 if (size_add(rsc_size, rsc->config_len) > avail) { in rproc_handle_vdev()
490 return -EINVAL; in rproc_handle_vdev()
494 if (rsc->reserved[0] || rsc->reserved[1]) { in rproc_handle_vdev()
496 return -EINVAL; in rproc_handle_vdev()
500 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); in rproc_handle_vdev()
503 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { in rproc_handle_vdev()
504 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); in rproc_handle_vdev()
505 return -EINVAL; in rproc_handle_vdev()
508 rvdev_data.id = rsc->id; in rproc_handle_vdev()
509 rvdev_data.index = rproc->nb_vdev++; in rproc_handle_vdev()
514 * When there is more than one remote processor, rproc->nb_vdev number is in rproc_handle_vdev()
519 pdev = platform_device_register_data(dev, "rproc-virtio", PLATFORM_DEVID_AUTO, &rvdev_data, in rproc_handle_vdev()
522 dev_err(dev, "failed to create rproc-virtio device\n"); in rproc_handle_vdev()
530 * rproc_handle_trace() - handle a shared trace buffer resource
542 * DMA API (but currently there isn't a use case for that).
551 struct device *dev = &rproc->dev; in rproc_handle_trace()
556 return -EINVAL; in rproc_handle_trace()
560 if (rsc->reserved) { in rproc_handle_trace()
562 return -EINVAL; in rproc_handle_trace()
567 return -ENOMEM; in rproc_handle_trace()
569 /* set the trace buffer dma properties */ in rproc_handle_trace()
570 trace->trace_mem.len = rsc->len; in rproc_handle_trace()
571 trace->trace_mem.da = rsc->da; in rproc_handle_trace()
574 trace->rproc = rproc; in rproc_handle_trace()
577 snprintf(name, sizeof(name), "trace%d", rproc->num_traces); in rproc_handle_trace()
580 trace->tfile = rproc_create_trace_file(name, rproc, trace); in rproc_handle_trace()
582 list_add_tail(&trace->node, &rproc->traces); in rproc_handle_trace()
584 rproc->num_traces++; in rproc_handle_trace()
587 name, rsc->da, rsc->len); in rproc_handle_trace()
593 * rproc_handle_devmem() - handle devmem resource entry
599 * Remote processors commonly need to access certain on-chip peripherals.
603 * the on-chip peripherals they need.
625 struct device *dev = &rproc->dev; in rproc_handle_devmem()
629 if (!rproc->domain) in rproc_handle_devmem()
630 return -EINVAL; in rproc_handle_devmem()
634 return -EINVAL; in rproc_handle_devmem()
638 if (rsc->reserved) { in rproc_handle_devmem()
640 return -EINVAL; in rproc_handle_devmem()
645 return -ENOMEM; in rproc_handle_devmem()
647 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags, in rproc_handle_devmem()
661 mapping->da = rsc->da; in rproc_handle_devmem()
662 mapping->len = rsc->len; in rproc_handle_devmem()
663 list_add_tail(&mapping->node, &rproc->mappings); in rproc_handle_devmem()
666 rsc->pa, rsc->da, rsc->len); in rproc_handle_devmem()
676 * rproc_alloc_carveout() - allocated specified carveout
678 * @mem: the memory entry to allocate
680 * This function allocate specified memory entry @mem using
686 struct rproc_mem_entry *mem) in rproc_alloc_carveout() argument
689 struct device *dev = &rproc->dev; in rproc_alloc_carveout()
690 dma_addr_t dma; in rproc_alloc_carveout() local
694 va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL); in rproc_alloc_carveout()
696 dev_err(dev->parent, in rproc_alloc_carveout()
697 "failed to allocate dma memory: len 0x%zx\n", in rproc_alloc_carveout()
698 mem->len); in rproc_alloc_carveout()
699 return -ENOMEM; in rproc_alloc_carveout()
702 dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n", in rproc_alloc_carveout()
703 va, &dma, mem->len); in rproc_alloc_carveout()
705 if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) { in rproc_alloc_carveout()
707 * Check requested da is equal to dma address in rproc_alloc_carveout()
712 if (mem->da != (u32)dma) in rproc_alloc_carveout()
713 dev_warn(dev->parent, in rproc_alloc_carveout()
718 * Ok, this is non-standard. in rproc_alloc_carveout()
720 * Sometimes we can't rely on the generic iommu-based DMA API in rproc_alloc_carveout()
731 * to use the iommu-based DMA API: we expect 'dma' to contain the in rproc_alloc_carveout()
734 if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) { in rproc_alloc_carveout()
737 ret = -ENOMEM; in rproc_alloc_carveout()
741 ret = iommu_map(rproc->domain, mem->da, dma, mem->len, in rproc_alloc_carveout()
742 mem->flags, GFP_KERNEL); in rproc_alloc_carveout()
755 mapping->da = mem->da; in rproc_alloc_carveout()
756 mapping->len = mem->len; in rproc_alloc_carveout()
757 list_add_tail(&mapping->node, &rproc->mappings); in rproc_alloc_carveout()
760 mem->da, &dma); in rproc_alloc_carveout()
763 if (mem->da == FW_RSC_ADDR_ANY) { in rproc_alloc_carveout()
765 if ((u64)dma & HIGH_BITS_MASK) in rproc_alloc_carveout()
766 dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n"); in rproc_alloc_carveout()
768 mem->da = (u32)dma; in rproc_alloc_carveout()
771 mem->dma = dma; in rproc_alloc_carveout()
772 mem->va = va; in rproc_alloc_carveout()
779 dma_free_coherent(dev->parent, mem->len, va, dma); in rproc_alloc_carveout()
784 * rproc_release_carveout() - release acquired carveout
786 * @mem: the memory entry to release
788 * This function releases specified memory entry @mem allocated via
794 struct rproc_mem_entry *mem) in rproc_release_carveout() argument
796 struct device *dev = &rproc->dev; in rproc_release_carveout()
799 dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma); in rproc_release_carveout()
804 * rproc_handle_carveout() - handle phys contig memory allocation requests
829 struct device *dev = &rproc->dev; in rproc_handle_carveout()
833 return -EINVAL; in rproc_handle_carveout()
837 if (rsc->reserved) { in rproc_handle_carveout()
839 return -EINVAL; in rproc_handle_carveout()
843 rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags); in rproc_handle_carveout()
849 carveout = rproc_find_carveout_by_name(rproc, rsc->name); in rproc_handle_carveout()
852 if (carveout->rsc_offset != FW_RSC_ADDR_ANY) { in rproc_handle_carveout()
855 return -ENOMEM; in rproc_handle_carveout()
858 if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len)) in rproc_handle_carveout()
859 return -ENOMEM; in rproc_handle_carveout()
862 carveout->rsc_offset = offset; in rproc_handle_carveout()
863 carveout->flags = rsc->flags; in rproc_handle_carveout()
869 carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da, in rproc_handle_carveout()
871 rproc_release_carveout, rsc->name); in rproc_handle_carveout()
874 return -ENOMEM; in rproc_handle_carveout()
877 carveout->flags = rsc->flags; in rproc_handle_carveout()
878 carveout->rsc_offset = offset; in rproc_handle_carveout()
885 * rproc_add_carveout() - register an allocated carveout region
887 * @mem: memory entry to register
892 void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) in rproc_add_carveout() argument
894 list_add_tail(&mem->node, &rproc->carveouts); in rproc_add_carveout()
899 * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
902 * @dma: dma address
917 void *va, dma_addr_t dma, size_t len, u32 da, in rproc_mem_entry_init() argument
922 struct rproc_mem_entry *mem; in rproc_mem_entry_init() local
925 mem = kzalloc(sizeof(*mem), GFP_KERNEL); in rproc_mem_entry_init()
926 if (!mem) in rproc_mem_entry_init()
927 return mem; in rproc_mem_entry_init()
929 mem->va = va; in rproc_mem_entry_init()
930 mem->dma = dma; in rproc_mem_entry_init()
931 mem->da = da; in rproc_mem_entry_init()
932 mem->len = len; in rproc_mem_entry_init()
933 mem->alloc = alloc; in rproc_mem_entry_init()
934 mem->release = release; in rproc_mem_entry_init()
935 mem->rsc_offset = FW_RSC_ADDR_ANY; in rproc_mem_entry_init()
936 mem->of_resm_idx = -1; in rproc_mem_entry_init()
939 vsnprintf(mem->name, sizeof(mem->name), name, args); in rproc_mem_entry_init()
942 return mem; in rproc_mem_entry_init()
947 * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct
950 * @of_resm_idx: reserved memory phandle index in "memory-region"
965 struct rproc_mem_entry *mem; in rproc_of_resm_mem_entry_init() local
968 mem = kzalloc(sizeof(*mem), GFP_KERNEL); in rproc_of_resm_mem_entry_init()
969 if (!mem) in rproc_of_resm_mem_entry_init()
970 return mem; in rproc_of_resm_mem_entry_init()
972 mem->da = da; in rproc_of_resm_mem_entry_init()
973 mem->len = len; in rproc_of_resm_mem_entry_init()
974 mem->rsc_offset = FW_RSC_ADDR_ANY; in rproc_of_resm_mem_entry_init()
975 mem->of_resm_idx = of_resm_idx; in rproc_of_resm_mem_entry_init()
978 vsnprintf(mem->name, sizeof(mem->name), name, args); in rproc_of_resm_mem_entry_init()
981 return mem; in rproc_of_resm_mem_entry_init()
986 * rproc_of_parse_firmware() - parse and return the firmware-name
988 * @index: index to use for the firmware-name retrieval
993 * the "firmware-name" property and returns the firmware name pointer
1002 ret = of_property_read_string_index(dev->of_node, "firmware-name", in rproc_of_parse_firmware()
1023 struct device *dev = &rproc->dev; in rproc_handle_resources()
1027 if (!rproc->table_ptr) in rproc_handle_resources()
1030 for (i = 0; i < rproc->table_ptr->num; i++) { in rproc_handle_resources()
1031 int offset = rproc->table_ptr->offset[i]; in rproc_handle_resources()
1032 struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset; in rproc_handle_resources()
1033 int avail = rproc->table_sz - offset - sizeof(*hdr); in rproc_handle_resources()
1039 return -EINVAL; in rproc_handle_resources()
1042 dev_dbg(dev, "rsc: type %d\n", hdr->type); in rproc_handle_resources()
1044 if (hdr->type >= RSC_VENDOR_START && in rproc_handle_resources()
1045 hdr->type <= RSC_VENDOR_END) { in rproc_handle_resources()
1046 ret = rproc_handle_rsc(rproc, hdr->type, rsc, in rproc_handle_resources()
1054 hdr->type); in rproc_handle_resources()
1058 if (hdr->type >= RSC_LAST) { in rproc_handle_resources()
1059 dev_warn(dev, "unsupported resource %d\n", hdr->type); in rproc_handle_resources()
1063 handler = handlers[hdr->type]; in rproc_handle_resources()
1080 list_for_each_entry(subdev, &rproc->subdevs, node) { in rproc_prepare_subdevices()
1081 if (subdev->prepare) { in rproc_prepare_subdevices()
1082 ret = subdev->prepare(subdev); in rproc_prepare_subdevices()
1091 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { in rproc_prepare_subdevices()
1092 if (subdev->unprepare) in rproc_prepare_subdevices()
1093 subdev->unprepare(subdev); in rproc_prepare_subdevices()
1104 list_for_each_entry(subdev, &rproc->subdevs, node) { in rproc_start_subdevices()
1105 if (subdev->start) { in rproc_start_subdevices()
1106 ret = subdev->start(subdev); in rproc_start_subdevices()
1115 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { in rproc_start_subdevices()
1116 if (subdev->stop) in rproc_start_subdevices()
1117 subdev->stop(subdev, true); in rproc_start_subdevices()
1127 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { in rproc_stop_subdevices()
1128 if (subdev->stop) in rproc_stop_subdevices()
1129 subdev->stop(subdev, crashed); in rproc_stop_subdevices()
1137 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { in rproc_unprepare_subdevices()
1138 if (subdev->unprepare) in rproc_unprepare_subdevices()
1139 subdev->unprepare(subdev); in rproc_unprepare_subdevices()
1144 * rproc_alloc_registered_carveouts() - allocate all carveouts registered
1158 struct device *dev = &rproc->dev; in rproc_alloc_registered_carveouts()
1162 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { in rproc_alloc_registered_carveouts()
1163 if (entry->alloc) { in rproc_alloc_registered_carveouts()
1164 ret = entry->alloc(rproc, entry); in rproc_alloc_registered_carveouts()
1167 entry->name, ret); in rproc_alloc_registered_carveouts()
1168 return -ENOMEM; in rproc_alloc_registered_carveouts()
1172 if (entry->rsc_offset != FW_RSC_ADDR_ANY) { in rproc_alloc_registered_carveouts()
1174 rsc = (void *)rproc->table_ptr + entry->rsc_offset; in rproc_alloc_registered_carveouts()
1180 * on-chip hardware accelerators that are not behind in rproc_alloc_registered_carveouts()
1189 * Non-IOMMU processors might also want to have this info. in rproc_alloc_registered_carveouts()
1194 /* Use va if defined else dma to generate pa */ in rproc_alloc_registered_carveouts()
1195 if (entry->va) in rproc_alloc_registered_carveouts()
1196 pa = (u64)rproc_va_to_pa(entry->va); in rproc_alloc_registered_carveouts()
1198 pa = (u64)entry->dma; in rproc_alloc_registered_carveouts()
1204 rsc->pa = (u32)pa; in rproc_alloc_registered_carveouts()
1205 rsc->da = entry->da; in rproc_alloc_registered_carveouts()
1206 rsc->len = entry->len; in rproc_alloc_registered_carveouts()
1215 * rproc_resource_cleanup() - clean up and free all acquired resources
1226 struct device *dev = &rproc->dev; in rproc_resource_cleanup()
1229 list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) { in rproc_resource_cleanup()
1230 rproc_remove_trace_file(trace->tfile); in rproc_resource_cleanup()
1231 rproc->num_traces--; in rproc_resource_cleanup()
1232 list_del(&trace->node); in rproc_resource_cleanup()
1237 list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { in rproc_resource_cleanup()
1240 unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); in rproc_resource_cleanup()
1241 if (unmapped != entry->len) { in rproc_resource_cleanup()
1243 dev_err(dev, "failed to unmap %zx/%zu\n", entry->len, in rproc_resource_cleanup()
1247 list_del(&entry->node); in rproc_resource_cleanup()
1252 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { in rproc_resource_cleanup()
1253 if (entry->release) in rproc_resource_cleanup()
1254 entry->release(rproc, entry); in rproc_resource_cleanup()
1255 list_del(&entry->node); in rproc_resource_cleanup()
1260 list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) in rproc_resource_cleanup()
1261 platform_device_unregister(rvdev->pdev); in rproc_resource_cleanup()
1270 struct device *dev = &rproc->dev; in rproc_start()
1281 * The starting device has been given the rproc->cached_table as the in rproc_start()
1290 memcpy(loaded_table, rproc->cached_table, rproc->table_sz); in rproc_start()
1291 rproc->table_ptr = loaded_table; in rproc_start()
1297 rproc->name, ret); in rproc_start()
1302 ret = rproc->ops->start(rproc); in rproc_start()
1304 dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); in rproc_start()
1312 rproc->name, ret); in rproc_start()
1316 rproc->state = RPROC_RUNNING; in rproc_start()
1318 dev_info(dev, "remote processor %s is now up\n", rproc->name); in rproc_start()
1323 rproc->ops->stop(rproc); in rproc_start()
1327 rproc->table_ptr = rproc->cached_table; in rproc_start()
1334 struct device *dev = &rproc->dev; in __rproc_attach()
1340 rproc->name, ret); in __rproc_attach()
1348 rproc->name, ret); in __rproc_attach()
1356 rproc->name, ret); in __rproc_attach()
1360 rproc->state = RPROC_ATTACHED; in __rproc_attach()
1362 dev_info(dev, "remote processor %s is now attached\n", rproc->name); in __rproc_attach()
1367 rproc->ops->stop(rproc); in __rproc_attach()
1379 struct device *dev = &rproc->dev; in rproc_fw_boot()
1380 const char *name = rproc->firmware; in rproc_fw_boot()
1387 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); in rproc_fw_boot()
1402 dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret); in rproc_fw_boot()
1406 rproc->bootaddr = rproc_get_boot_addr(rproc, fw); in rproc_fw_boot()
1414 rproc->max_notifyid = -1; in rproc_fw_boot()
1417 rproc->nb_vdev = 0; in rproc_fw_boot()
1442 kfree(rproc->cached_table); in rproc_fw_boot()
1443 rproc->cached_table = NULL; in rproc_fw_boot()
1444 rproc->table_ptr = NULL; in rproc_fw_boot()
1456 struct device *dev = &rproc->dev; in rproc_set_rsc_table()
1475 * the remote processor is re-attached, that is: in rproc_set_rsc_table()
1477 * DETACHED -> ATTACHED -> DETACHED -> ATTACHED in rproc_set_rsc_table()
1482 if (rproc->ops->detach) { in rproc_set_rsc_table()
1483 rproc->clean_table = kmemdup(table_ptr, table_sz, GFP_KERNEL); in rproc_set_rsc_table()
1484 if (!rproc->clean_table) in rproc_set_rsc_table()
1485 return -ENOMEM; in rproc_set_rsc_table()
1487 rproc->clean_table = NULL; in rproc_set_rsc_table()
1490 rproc->cached_table = NULL; in rproc_set_rsc_table()
1491 rproc->table_ptr = table_ptr; in rproc_set_rsc_table()
1492 rproc->table_sz = table_sz; in rproc_set_rsc_table()
1502 if (!rproc->table_ptr) in rproc_reset_rsc_table_on_detach()
1510 if (WARN_ON(!rproc->clean_table)) in rproc_reset_rsc_table_on_detach()
1511 return -EINVAL; in rproc_reset_rsc_table_on_detach()
1514 table_ptr = rproc->table_ptr; in rproc_reset_rsc_table_on_detach()
1523 rproc->cached_table = kmemdup(rproc->table_ptr, in rproc_reset_rsc_table_on_detach()
1524 rproc->table_sz, GFP_KERNEL); in rproc_reset_rsc_table_on_detach()
1525 if (!rproc->cached_table) in rproc_reset_rsc_table_on_detach()
1526 return -ENOMEM; in rproc_reset_rsc_table_on_detach()
1532 rproc->table_ptr = rproc->cached_table; in rproc_reset_rsc_table_on_detach()
1536 * to its original value. That way when we re-attach the remote in rproc_reset_rsc_table_on_detach()
1539 memcpy(table_ptr, rproc->clean_table, rproc->table_sz); in rproc_reset_rsc_table_on_detach()
1545 kfree(rproc->clean_table); in rproc_reset_rsc_table_on_detach()
1553 if (!rproc->table_ptr) in rproc_reset_rsc_table_on_stop()
1561 if (rproc->cached_table) in rproc_reset_rsc_table_on_stop()
1571 rproc->cached_table = kmemdup(rproc->table_ptr, in rproc_reset_rsc_table_on_stop()
1572 rproc->table_sz, GFP_KERNEL); in rproc_reset_rsc_table_on_stop()
1573 if (!rproc->cached_table) in rproc_reset_rsc_table_on_stop()
1574 return -ENOMEM; in rproc_reset_rsc_table_on_stop()
1580 kfree(rproc->clean_table); in rproc_reset_rsc_table_on_stop()
1587 rproc->table_ptr = rproc->cached_table; in rproc_reset_rsc_table_on_stop()
1592 * Attach to remote processor - similar to rproc_fw_boot() but without
1597 struct device *dev = &rproc->dev; in rproc_attach()
1613 dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret); in rproc_attach()
1624 rproc->max_notifyid = -1; in rproc_attach()
1627 rproc->nb_vdev = 0; in rproc_attach()
1632 * we expect the platform driver to properly set rproc->table_ptr. in rproc_attach()
1691 if (rproc->state == RPROC_DETACHED) in rproc_trigger_auto_boot()
1696 * be built-in kernel code, without hanging the boot process. in rproc_trigger_auto_boot()
1699 rproc->firmware, &rproc->dev, GFP_KERNEL, in rproc_trigger_auto_boot()
1702 dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); in rproc_trigger_auto_boot()
1709 struct device *dev = &rproc->dev; in rproc_stop()
1713 if (!rproc->ops->stop) in rproc_stop()
1714 return -EINVAL; in rproc_stop()
1728 ret = rproc->ops->stop(rproc); in rproc_stop()
1736 rproc->state = RPROC_OFFLINE; in rproc_stop()
1738 dev_info(dev, "stopped remote processor %s\n", rproc->name); in rproc_stop()
1748 struct device *dev = &rproc->dev; in __rproc_detach()
1752 if (!rproc->ops->detach) in __rproc_detach()
1753 return -EINVAL; in __rproc_detach()
1766 ret = rproc->ops->detach(rproc); in __rproc_detach()
1774 rproc->state = RPROC_DETACHED; in __rproc_detach()
1776 dev_info(dev, "detached remote processor %s\n", rproc->name); in __rproc_detach()
1795 struct device *dev = &rproc->dev; in rproc_boot_recovery()
1803 rproc->ops->coredump(rproc); in rproc_boot_recovery()
1806 ret = request_firmware(&firmware_p, rproc->firmware, dev); in rproc_boot_recovery()
1821 * rproc_trigger_recovery() - recover a remoteproc
1834 struct device *dev = &rproc->dev; in rproc_trigger_recovery()
1837 ret = mutex_lock_interruptible(&rproc->lock); in rproc_trigger_recovery()
1842 if (rproc->state != RPROC_CRASHED) in rproc_trigger_recovery()
1845 dev_err(dev, "recovering %s\n", rproc->name); in rproc_trigger_recovery()
1853 mutex_unlock(&rproc->lock); in rproc_trigger_recovery()
1858 * rproc_crash_handler_work() - handle a crash
1867 struct device *dev = &rproc->dev; in rproc_crash_handler_work()
1871 mutex_lock(&rproc->lock); in rproc_crash_handler_work()
1873 if (rproc->state == RPROC_CRASHED) { in rproc_crash_handler_work()
1875 mutex_unlock(&rproc->lock); in rproc_crash_handler_work()
1879 if (rproc->state == RPROC_OFFLINE) { in rproc_crash_handler_work()
1881 mutex_unlock(&rproc->lock); in rproc_crash_handler_work()
1885 rproc->state = RPROC_CRASHED; in rproc_crash_handler_work()
1886 dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, in rproc_crash_handler_work()
1887 rproc->name); in rproc_crash_handler_work()
1889 mutex_unlock(&rproc->lock); in rproc_crash_handler_work()
1891 if (!rproc->recovery_disabled) in rproc_crash_handler_work()
1895 pm_relax(rproc->dev.parent); in rproc_crash_handler_work()
1899 * rproc_boot() - boot a remote processor
1917 return -EINVAL; in rproc_boot()
1920 dev = &rproc->dev; in rproc_boot()
1922 ret = mutex_lock_interruptible(&rproc->lock); in rproc_boot()
1924 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); in rproc_boot()
1928 if (rproc->state == RPROC_DELETED) { in rproc_boot()
1929 ret = -ENODEV; in rproc_boot()
1930 dev_err(dev, "can't boot deleted rproc %s\n", rproc->name); in rproc_boot()
1935 if (atomic_inc_return(&rproc->power) > 1) { in rproc_boot()
1940 if (rproc->state == RPROC_DETACHED) { in rproc_boot()
1941 dev_info(dev, "attaching to %s\n", rproc->name); in rproc_boot()
1945 dev_info(dev, "powering up %s\n", rproc->name); in rproc_boot()
1948 ret = request_firmware(&firmware_p, rproc->firmware, dev); in rproc_boot()
1961 atomic_dec(&rproc->power); in rproc_boot()
1963 mutex_unlock(&rproc->lock); in rproc_boot()
1969 * rproc_shutdown() - power off the remote processor
1982 * - we're not decrementing the rproc's refcount, only the power refcount.
1991 struct device *dev = &rproc->dev; in rproc_shutdown()
1994 ret = mutex_lock_interruptible(&rproc->lock); in rproc_shutdown()
1996 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); in rproc_shutdown()
2000 if (rproc->state != RPROC_RUNNING && in rproc_shutdown()
2001 rproc->state != RPROC_ATTACHED) { in rproc_shutdown()
2002 ret = -EINVAL; in rproc_shutdown()
2007 if (!atomic_dec_and_test(&rproc->power)) in rproc_shutdown()
2012 atomic_inc(&rproc->power); in rproc_shutdown()
2025 kfree(rproc->cached_table); in rproc_shutdown()
2026 rproc->cached_table = NULL; in rproc_shutdown()
2027 rproc->table_ptr = NULL; in rproc_shutdown()
2029 mutex_unlock(&rproc->lock); in rproc_shutdown()
2035 * rproc_detach() - Detach the remote processor from the
2056 struct device *dev = &rproc->dev; in rproc_detach()
2059 ret = mutex_lock_interruptible(&rproc->lock); in rproc_detach()
2061 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); in rproc_detach()
2065 if (rproc->state != RPROC_ATTACHED) { in rproc_detach()
2066 ret = -EINVAL; in rproc_detach()
2071 if (!atomic_dec_and_test(&rproc->power)) { in rproc_detach()
2078 atomic_inc(&rproc->power); in rproc_detach()
2091 kfree(rproc->cached_table); in rproc_detach()
2092 rproc->cached_table = NULL; in rproc_detach()
2093 rproc->table_ptr = NULL; in rproc_detach()
2095 mutex_unlock(&rproc->lock); in rproc_detach()
2101 * rproc_get_by_phandle() - find a remote processor by phandle
2125 if (r->dev.parent && device_match_of_node(r->dev.parent, np)) { in rproc_get_by_phandle()
2133 driver = r->dev.parent->driver; in rproc_get_by_phandle()
2140 if (r->dev.parent->parent) in rproc_get_by_phandle()
2141 driver = r->dev.parent->parent->driver; in rproc_get_by_phandle()
2146 if (!try_module_get(driver->owner)) { in rproc_get_by_phandle()
2147 dev_err(&r->dev, "can't get owner\n"); in rproc_get_by_phandle()
2152 get_device(&rproc->dev); in rproc_get_by_phandle()
2171 * rproc_set_firmware() - assign a new firmware
2182 * sysfs or a kernel-level remoteproc or a remoteproc client driver to set
2195 return -EINVAL; in rproc_set_firmware()
2197 dev = rproc->dev.parent; in rproc_set_firmware()
2199 ret = mutex_lock_interruptible(&rproc->lock); in rproc_set_firmware()
2201 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); in rproc_set_firmware()
2202 return -EINVAL; in rproc_set_firmware()
2205 if (rproc->state != RPROC_OFFLINE) { in rproc_set_firmware()
2207 ret = -EBUSY; in rproc_set_firmware()
2214 ret = -EINVAL; in rproc_set_firmware()
2220 ret = -ENOMEM; in rproc_set_firmware()
2224 kfree_const(rproc->firmware); in rproc_set_firmware()
2225 rproc->firmware = p; in rproc_set_firmware()
2228 mutex_unlock(&rproc->lock); in rproc_set_firmware()
2235 switch (rproc->state) { in rproc_validate()
2241 if (!rproc->ops->start) in rproc_validate()
2242 return -EINVAL; in rproc_validate()
2249 if (!rproc->ops->attach) in rproc_validate()
2250 return -EINVAL; in rproc_validate()
2256 if (rproc->cached_table) in rproc_validate()
2257 return -EINVAL; in rproc_validate()
2264 return -EINVAL; in rproc_validate()
2271 * rproc_add() - register a remote processor
2277 * This is called by the platform-specific rproc implementation, whenever
2292 struct device *dev = &rproc->dev; in rproc_add()
2310 dev_info(dev, "%s is available\n", rproc->name); in rproc_add()
2315 /* if rproc is marked always-on, request it to boot */ in rproc_add()
2316 if (rproc->auto_boot) { in rproc_add()
2324 list_add_rcu(&rproc->node, &rproc_list); in rproc_add()
2344 * devm_rproc_add() - resource managed rproc_add()
2366 * rproc_type_release() - release a remote processor instance
2378 dev_info(&rproc->dev, "releasing %s\n", rproc->name); in rproc_type_release()
2380 idr_destroy(&rproc->notifyids); in rproc_type_release()
2382 if (rproc->index >= 0) in rproc_type_release()
2383 ida_free(&rproc_dev_index, rproc->index); in rproc_type_release()
2385 kfree_const(rproc->firmware); in rproc_type_release()
2386 kfree_const(rproc->name); in rproc_type_release()
2387 kfree(rproc->ops); in rproc_type_release()
2408 p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name); in rproc_alloc_firmware()
2411 return -ENOMEM; in rproc_alloc_firmware()
2413 rproc->firmware = p; in rproc_alloc_firmware()
2420 rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); in rproc_alloc_ops()
2421 if (!rproc->ops) in rproc_alloc_ops()
2422 return -ENOMEM; in rproc_alloc_ops()
2425 if (!rproc->ops->coredump) in rproc_alloc_ops()
2426 rproc->ops->coredump = rproc_coredump; in rproc_alloc_ops()
2428 if (rproc->ops->load) in rproc_alloc_ops()
2432 rproc->ops->load = rproc_elf_load_segments; in rproc_alloc_ops()
2433 rproc->ops->parse_fw = rproc_elf_load_rsc_table; in rproc_alloc_ops()
2434 rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; in rproc_alloc_ops()
2435 rproc->ops->sanity_check = rproc_elf_sanity_check; in rproc_alloc_ops()
2436 rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; in rproc_alloc_ops()
2442 * rproc_alloc() - allocate a remote processor handle
2445 * @ops: platform-specific handlers (mainly start/stop)
2477 rproc->priv = &rproc[1]; in rproc_alloc()
2478 rproc->auto_boot = true; in rproc_alloc()
2479 rproc->elf_class = ELFCLASSNONE; in rproc_alloc()
2480 rproc->elf_machine = EM_NONE; in rproc_alloc()
2482 device_initialize(&rproc->dev); in rproc_alloc()
2483 rproc->dev.parent = dev; in rproc_alloc()
2484 rproc->dev.type = &rproc_type; in rproc_alloc()
2485 rproc->dev.class = &rproc_class; in rproc_alloc()
2486 rproc->dev.driver_data = rproc; in rproc_alloc()
2487 idr_init(&rproc->notifyids); in rproc_alloc()
2489 rproc->name = kstrdup_const(name, GFP_KERNEL); in rproc_alloc()
2490 if (!rproc->name) in rproc_alloc()
2500 rproc->index = ida_alloc(&rproc_dev_index, GFP_KERNEL); in rproc_alloc()
2501 if (rproc->index < 0) { in rproc_alloc()
2502 dev_err(dev, "ida_alloc failed: %d\n", rproc->index); in rproc_alloc()
2506 dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); in rproc_alloc()
2508 atomic_set(&rproc->power, 0); in rproc_alloc()
2510 mutex_init(&rproc->lock); in rproc_alloc()
2512 INIT_LIST_HEAD(&rproc->carveouts); in rproc_alloc()
2513 INIT_LIST_HEAD(&rproc->mappings); in rproc_alloc()
2514 INIT_LIST_HEAD(&rproc->traces); in rproc_alloc()
2515 INIT_LIST_HEAD(&rproc->rvdevs); in rproc_alloc()
2516 INIT_LIST_HEAD(&rproc->subdevs); in rproc_alloc()
2517 INIT_LIST_HEAD(&rproc->dump_segments); in rproc_alloc()
2519 INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); in rproc_alloc()
2521 rproc->state = RPROC_OFFLINE; in rproc_alloc()
2526 put_device(&rproc->dev); in rproc_alloc()
2532 * rproc_free() - unroll rproc_alloc()
2542 put_device(&rproc->dev); in rproc_free()
2547 * rproc_put() - release rproc reference
2557 if (rproc->dev.parent->driver) in rproc_put()
2558 module_put(rproc->dev.parent->driver->owner); in rproc_put()
2560 module_put(rproc->dev.parent->parent->driver->owner); in rproc_put()
2562 put_device(&rproc->dev); in rproc_put()
2567 * rproc_del() - unregister a remote processor
2579 * Return: 0 on success and -EINVAL if @rproc isn't valid
2584 return -EINVAL; in rproc_del()
2586 /* TODO: make sure this works with rproc->power > 1 */ in rproc_del()
2589 mutex_lock(&rproc->lock); in rproc_del()
2590 rproc->state = RPROC_DELETED; in rproc_del()
2591 mutex_unlock(&rproc->lock); in rproc_del()
2597 list_del_rcu(&rproc->node); in rproc_del()
2603 device_del(&rproc->dev); in rproc_del()
2616 * devm_rproc_alloc() - resource managed rproc_alloc()
2619 * @ops: platform-specific handlers (mainly start/stop)
2651 * rproc_add_subdev() - add a subdevice to a remoteproc
2659 list_add_tail(&subdev->node, &rproc->subdevs); in rproc_add_subdev()
2664 * rproc_remove_subdev() - remove a subdevice from a remoteproc
2670 list_del(&subdev->node); in rproc_remove_subdev()
2675 * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
2682 for (dev = dev->parent; dev; dev = dev->parent) { in rproc_get_by_child()
2683 if (dev->type == &rproc_type) in rproc_get_by_child()
2684 return dev->driver_data; in rproc_get_by_child()
2692 * rproc_report_crash() - rproc crash reporter function
2696 * This function must be called every time a crash is detected by the low-level
2698 * non-remoteproc driver.
2710 pm_stay_awake(rproc->dev.parent); in rproc_report_crash()
2712 dev_err(&rproc->dev, "crash detected in %s: type %s\n", in rproc_report_crash()
2713 rproc->name, rproc_crash_to_string(type)); in rproc_report_crash()
2715 queue_work(rproc_recovery_wq, &rproc->crash_handler); in rproc_report_crash()
2728 if (!rproc->ops->panic) in rproc_panic_handler()
2731 if (rproc->state != RPROC_RUNNING && in rproc_panic_handler()
2732 rproc->state != RPROC_ATTACHED) in rproc_panic_handler()
2735 d = rproc->ops->panic(rproc); in rproc_panic_handler()
2768 return -ENOMEM; in remoteproc_init()