25 #define pr_fmt(fmt) "%s: " fmt, __func__
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/slab.h>
34 #include <linux/string.h>
39 #include <linux/elf.h>
41 #include <linux/virtio_ring.h>
42 #include <asm/byteorder.h>
53 static const char *
const rproc_crash_names[] = {
61 return rproc_crash_names[
type];
76 struct rproc *rproc =
token;
78 dev_err(dev,
"iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
89 static int rproc_enable_iommu(
struct rproc *rproc)
107 dev_dbg(dev,
"iommu not found\n");
113 dev_err(dev,
"can't alloc iommu domain\n");
121 dev_err(dev,
"can't attach iommu device: %d\n", ret);
134 static void rproc_disable_iommu(
struct rproc *rproc)
178 if (offset + len > carveout->
len)
192 struct rproc *rproc = rvdev->
rproc;
203 dev_err(dev,
"idr_pre_get failed\n");
226 dev_err(dev,
"idr_get_new failed: %d\n", ret);
234 dev_dbg(dev,
"vring%d: va %p dma %llx size %x idr %d\n", i, va,
235 (
unsigned long long)dma, size, notifyid);
247 struct rproc *rproc = rvdev->
rproc;
252 dev_dbg(dev,
"vdev rsc: vring%d: da %x, qsz %d, align %d\n",
257 dev_err(dev,
"vring rsc has non zero reserved bytes\n");
263 dev_err(dev,
"invalid qsz (%d) or alignment (%d)\n",
275 static int rproc_max_notifyid(
int id,
void *
p,
void *
data)
278 *maxid =
max(*maxid,
id);
285 struct rproc *rproc = rvring->
rvdev->rproc;
323 static int rproc_handle_vdev(
struct rproc *rproc,
struct fw_rsc_vdev *rsc,
333 dev_err(dev,
"vdev rsc is truncated\n");
339 dev_err(dev,
"vdev rsc has non zero reserved bytes\n");
343 dev_dbg(dev,
"vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
360 ret = rproc_parse_vring(rvdev, rsc, i);
398 static int rproc_handle_trace(
struct rproc *rproc,
struct fw_rsc_trace *rsc,
406 if (
sizeof(*rsc) > avail) {
407 dev_err(dev,
"trace rsc is truncated\n");
413 dev_err(dev,
"trace rsc has non zero reserved bytes\n");
420 dev_err(dev,
"erroneous trace resource entry\n");
426 dev_err(dev,
"kzalloc trace failed\n");
449 dev_dbg(dev,
"%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
480 static int rproc_handle_devmem(
struct rproc *rproc,
struct fw_rsc_devmem *rsc,
491 if (
sizeof(*rsc) > avail) {
492 dev_err(dev,
"devmem rsc is truncated\n");
498 dev_err(dev,
"devmem rsc has non zero reserved bytes\n");
502 mapping = kzalloc(
sizeof(*mapping),
GFP_KERNEL);
504 dev_err(dev,
"kzalloc mapping failed\n");
510 dev_err(dev,
"failed to map devmem: %d\n", ret);
521 mapping->
da = rsc->
da;
525 dev_dbg(dev,
"mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
553 static int rproc_handle_carveout(
struct rproc *rproc,
562 if (
sizeof(*rsc) > avail) {
563 dev_err(dev,
"carveout rsc is truncated\n");
569 dev_err(dev,
"carveout rsc has non zero reserved bytes\n");
573 dev_dbg(dev,
"carveout rsc: da %x, pa %x, len %x, flags %x\n",
576 carveout = kzalloc(
sizeof(*carveout),
GFP_KERNEL);
578 dev_err(dev,
"kzalloc carveout failed\n");
589 dev_dbg(dev,
"carveout va %p, dma %llx, len 0x%x\n", va,
590 (
unsigned long long)dma, rsc->
len);
610 mapping = kzalloc(
sizeof(*mapping),
GFP_KERNEL);
612 dev_err(dev,
"kzalloc mapping failed\n");
620 dev_err(dev,
"iommu_map failed: %d\n", ret);
631 mapping->
da = rsc->
da;
635 dev_dbg(dev,
"carveout mapped 0x%x to 0x%llx\n",
636 rsc->
da, (
unsigned long long)dma);
661 carveout->
da = rsc->
da;
689 rproc_handle_boot_rsc(
struct rproc *rproc,
struct resource_table *table,
int len)
695 for (i = 0; i < table->
num; i++) {
698 int avail = len - offset -
sizeof(*hdr);
699 void *rsc = (
void *)hdr +
sizeof(*hdr);
703 dev_err(dev,
"rsc table is truncated\n");
714 handler = rproc_handle_rsc[hdr->type];
718 ret = handler(rproc, rsc, avail);
728 rproc_handle_virtio_rsc(
struct rproc *rproc,
struct resource_table *table,
int len)
733 for (i = 0; i < table->
num; i++) {
735 struct fw_rsc_hdr *hdr = (
void *)table + offset;
736 int avail = len - offset -
sizeof(*hdr);
741 dev_err(dev,
"rsc table is truncated\n");
745 dev_dbg(dev,
"%s: rsc type %d\n", __func__, hdr->
type);
752 ret = rproc_handle_vdev(rproc, vrsc, avail);
767 static void rproc_resource_cleanup(
struct rproc *rproc)
792 if (unmapped != entry->
len) {
794 dev_err(dev,
"failed to unmap %u/%zu\n", entry->
len,
806 static int rproc_fw_boot(
struct rproc *rproc,
const struct firmware *
fw)
813 ret = rproc_fw_sanity_check(rproc, fw);
817 dev_info(dev,
"Booting fw image %s, size %zd\n", name, fw->
size);
823 ret = rproc_enable_iommu(rproc);
825 dev_err(dev,
"can't enable iommu: %d\n", ret);
829 rproc->
bootaddr = rproc_get_boot_addr(rproc, fw);
832 table = rproc_find_rsc_table(rproc, fw, &tablesz);
839 ret = rproc_handle_boot_rsc(rproc, table, tablesz);
841 dev_err(dev,
"Failed to process resources: %d\n", ret);
846 ret = rproc_load_segments(rproc, fw);
848 dev_err(dev,
"Failed to load program segments: %d\n", ret);
853 ret = rproc->
ops->start(rproc);
855 dev_err(dev,
"can't start rproc %s: %d\n", rproc->
name, ret);
861 dev_info(dev,
"remote processor %s is now up\n", rproc->
name);
866 rproc_resource_cleanup(rproc);
867 rproc_disable_iommu(rproc);
879 static void rproc_fw_config_virtio(
const struct firmware *fw,
void *
context)
885 if (rproc_fw_sanity_check(rproc, fw) < 0)
889 table = rproc_find_rsc_table(rproc, fw, &tablesz);
894 ret = rproc_handle_virtio_rsc(rproc, table, tablesz);
904 static int rproc_add_virtio_devices(
struct rproc *rproc)
921 rproc, rproc_fw_config_virtio);
923 dev_err(&rproc->
dev,
"request_firmware_nowait err: %d\n", ret);
955 return rproc_add_virtio_devices(rproc);
969 dev_dbg(dev,
"enter %s\n", __func__);
1007 pr_err(
"invalid rproc handle\n");
1015 dev_err(dev,
"can't lock rproc %s: %d\n", rproc->
name, ret);
1021 dev_err(dev,
"%s: no firmware to load\n", __func__);
1027 if (!try_module_get(dev->
parent->driver->owner)) {
1028 dev_err(dev,
"%s: can't get owner\n", __func__);
1044 dev_err(dev,
"request_firmware failed: %d\n", ret);
1048 ret = rproc_fw_boot(rproc, firmware_p);
1054 module_put(dev->
parent->driver->owner);
1089 dev_err(dev,
"can't lock rproc %s: %d\n", rproc->
name, ret);
1098 ret = rproc->
ops->stop(rproc);
1101 dev_err(dev,
"can't stop rproc: %d\n", ret);
1106 rproc_resource_cleanup(rproc);
1108 rproc_disable_iommu(rproc);
1116 dev_info(dev,
"stopped remote processor %s\n", rproc->
name);
1121 module_put(dev->
parent->driver->owner);
1156 dev_info(dev,
"Note: remoteproc is still under development and considered experimental.\n");
1157 dev_info(dev,
"THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1162 return rproc_add_virtio_devices(rproc);
1175 static void rproc_type_release(
struct device *dev)
1177 struct rproc *rproc =
container_of(dev,
struct rproc, dev);
1186 if (rproc->
index >= 0)
1193 .name =
"remoteproc",
1194 .release = rproc_type_release,
1224 struct rproc *rproc;
1226 if (!dev || !name || !ops)
1229 rproc = kzalloc(
sizeof(
struct rproc) + len,
GFP_KERNEL);
1231 dev_err(dev,
"%s: kzalloc failed\n", __func__);
1238 rproc->
priv = &rproc[1];
1242 rproc->
dev.type = &rproc_type;
1246 if (rproc->
index < 0) {
1247 dev_err(dev,
"ida_simple_get failed: %d\n", rproc->
index);
1265 INIT_LIST_HEAD(&rproc->
traces);
1266 INIT_LIST_HEAD(&rproc->
rvdevs);
1341 pr_err(
"NULL rproc pointer\n");
1345 dev_err(&rproc->
dev,
"crash detected in %s: type %s\n",
1346 rproc->
name, rproc_crash_to_string(type));
1353 static int __init remoteproc_init(
void)
1361 static void __exit remoteproc_exit(
void)