remoteproc_core.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*
  2. * Remote Processor Framework
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2011 Google, Inc.
  6. *
  7. * Ohad Ben-Cohen <ohad@wizery.com>
  8. * Brian Swetland <swetland@google.com>
  9. * Mark Grosen <mgrosen@ti.com>
  10. * Fernando Guzman Lugo <fernando.lugo@ti.com>
  11. * Suman Anna <s-anna@ti.com>
  12. * Robert Tivy <rtivy@ti.com>
  13. * Armando Uribe De Leon <x0095078@ti.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #define pr_fmt(fmt) "%s: " fmt, __func__
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/slab.h>
  29. #include <linux/mutex.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/firmware.h>
  32. #include <linux/string.h>
  33. #include <linux/debugfs.h>
  34. #include <linux/remoteproc.h>
  35. #include <linux/iommu.h>
  36. #include <linux/klist.h>
  37. #include <linux/elf.h>
  38. #include <linux/virtio_ids.h>
  39. #include <linux/virtio_ring.h>
  40. #include "remoteproc_internal.h"
  41. static void klist_rproc_get(struct klist_node *n);
  42. static void klist_rproc_put(struct klist_node *n);
  43. /*
  44. * klist of the available remote processors.
  45. *
  46. * We need this in order to support name-based lookups (needed by the
  47. * rproc_get_by_name()).
  48. *
  49. * That said, we don't use rproc_get_by_name() anymore within the rpmsg
  50. * framework. The use cases that do require its existence should be
  51. * scrutinized, and hopefully migrated to rproc_boot() using device-based
  52. * binding.
  53. *
  54. * If/when this materializes, we could drop the klist (and the by_name
  55. * API).
  56. */
  57. static DEFINE_KLIST(rprocs, klist_rproc_get, klist_rproc_put);
  58. typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
  59. struct fw_resource *rsc, int len);
  60. /*
  61. * This is the IOMMU fault handler we register with the IOMMU API
  62. * (when relevant; not all remote processors access memory through
  63. * an IOMMU).
  64. *
  65. * IOMMU core will invoke this handler whenever the remote processor
  66. * will try to access an unmapped device address.
  67. *
  68. * Currently this is mostly a stub, but it will be later used to trigger
  69. * the recovery of the remote processor.
  70. */
  71. static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
  72. unsigned long iova, int flags)
  73. {
  74. dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
  75. /*
  76. * Let the iommu core know we're not really handling this fault;
  77. * we just plan to use this as a recovery trigger.
  78. */
  79. return -ENOSYS;
  80. }
  81. static int rproc_enable_iommu(struct rproc *rproc)
  82. {
  83. struct iommu_domain *domain;
  84. struct device *dev = rproc->dev;
  85. int ret;
  86. /*
  87. * We currently use iommu_present() to decide if an IOMMU
  88. * setup is needed.
  89. *
  90. * This works for simple cases, but will easily fail with
  91. * platforms that do have an IOMMU, but not for this specific
  92. * rproc.
  93. *
  94. * This will be easily solved by introducing hw capabilities
  95. * that will be set by the remoteproc driver.
  96. */
  97. if (!iommu_present(dev->bus)) {
  98. dev_dbg(dev, "iommu not found\n");
  99. return 0;
  100. }
  101. domain = iommu_domain_alloc(dev->bus);
  102. if (!domain) {
  103. dev_err(dev, "can't alloc iommu domain\n");
  104. return -ENOMEM;
  105. }
  106. iommu_set_fault_handler(domain, rproc_iommu_fault);
  107. ret = iommu_attach_device(domain, dev);
  108. if (ret) {
  109. dev_err(dev, "can't attach iommu device: %d\n", ret);
  110. goto free_domain;
  111. }
  112. rproc->domain = domain;
  113. return 0;
  114. free_domain:
  115. iommu_domain_free(domain);
  116. return ret;
  117. }
  118. static void rproc_disable_iommu(struct rproc *rproc)
  119. {
  120. struct iommu_domain *domain = rproc->domain;
  121. struct device *dev = rproc->dev;
  122. if (!domain)
  123. return;
  124. iommu_detach_device(domain, dev);
  125. iommu_domain_free(domain);
  126. return;
  127. }
  128. /*
  129. * Some remote processors will ask us to allocate them physically contiguous
  130. * memory regions (which we call "carveouts"), and map them to specific
  131. * device addresses (which are hardcoded in the firmware).
  132. *
  133. * They may then ask us to copy objects into specific device addresses (e.g.
  134. * code/data sections) or expose us certain symbols in other device address
  135. * (e.g. their trace buffer).
  136. *
  137. * This function is an internal helper with which we can go over the allocated
  138. * carveouts and translate specific device address to kernel virtual addresses
  139. * so we can access the referenced memory.
  140. *
  141. * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
  142. * but only on kernel direct mapped RAM memory. Instead, we're just using
  143. * here the output of the DMA API, which should be more correct.
  144. */
  145. static void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
  146. {
  147. struct rproc_mem_entry *carveout;
  148. void *ptr = NULL;
  149. list_for_each_entry(carveout, &rproc->carveouts, node) {
  150. int offset = da - carveout->da;
  151. /* try next carveout if da is too small */
  152. if (offset < 0)
  153. continue;
  154. /* try next carveout if da is too large */
  155. if (offset + len > carveout->len)
  156. continue;
  157. ptr = carveout->va + offset;
  158. break;
  159. }
  160. return ptr;
  161. }
  162. /**
  163. * rproc_load_segments() - load firmware segments to memory
  164. * @rproc: remote processor which will be booted using these fw segments
  165. * @elf_data: the content of the ELF firmware image
  166. * @len: firmware size (in bytes)
  167. *
  168. * This function loads the firmware segments to memory, where the remote
  169. * processor expects them.
  170. *
  171. * Some remote processors will expect their code and data to be placed
  172. * in specific device addresses, and can't have them dynamically assigned.
  173. *
  174. * We currently support only those kind of remote processors, and expect
  175. * the program header's paddr member to contain those addresses. We then go
  176. * through the physically contiguous "carveout" memory regions which we
  177. * allocated (and mapped) earlier on behalf of the remote processor,
  178. * and "translate" device address to kernel addresses, so we can copy the
  179. * segments where they are expected.
  180. *
  181. * Currently we only support remote processors that required carveout
  182. * allocations and got them mapped onto their iommus. Some processors
  183. * might be different: they might not have iommus, and would prefer to
  184. * directly allocate memory for every segment/resource. This is not yet
  185. * supported, though.
  186. */
  187. static int
  188. rproc_load_segments(struct rproc *rproc, const u8 *elf_data, size_t len)
  189. {
  190. struct device *dev = rproc->dev;
  191. struct elf32_hdr *ehdr;
  192. struct elf32_phdr *phdr;
  193. int i, ret = 0;
  194. ehdr = (struct elf32_hdr *)elf_data;
  195. phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
  196. /* go through the available ELF segments */
  197. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  198. u32 da = phdr->p_paddr;
  199. u32 memsz = phdr->p_memsz;
  200. u32 filesz = phdr->p_filesz;
  201. u32 offset = phdr->p_offset;
  202. void *ptr;
  203. if (phdr->p_type != PT_LOAD)
  204. continue;
  205. dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
  206. phdr->p_type, da, memsz, filesz);
  207. if (filesz > memsz) {
  208. dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
  209. filesz, memsz);
  210. ret = -EINVAL;
  211. break;
  212. }
  213. if (offset + filesz > len) {
  214. dev_err(dev, "truncated fw: need 0x%x avail 0x%x\n",
  215. offset + filesz, len);
  216. ret = -EINVAL;
  217. break;
  218. }
  219. /* grab the kernel address for this device address */
  220. ptr = rproc_da_to_va(rproc, da, memsz);
  221. if (!ptr) {
  222. dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
  223. ret = -EINVAL;
  224. break;
  225. }
  226. /* put the segment where the remote processor expects it */
  227. if (phdr->p_filesz)
  228. memcpy(ptr, elf_data + phdr->p_offset, filesz);
  229. /*
  230. * Zero out remaining memory for this segment.
  231. *
  232. * This isn't strictly required since dma_alloc_coherent already
  233. * did this for us. albeit harmless, we may consider removing
  234. * this.
  235. */
  236. if (memsz > filesz)
  237. memset(ptr + filesz, 0, memsz - filesz);
  238. }
  239. return ret;
  240. }
  241. /**
  242. * rproc_handle_virtio_hdr() - handle a virtio header resource
  243. * @rproc: the remote processor
  244. * @rsc: the resource descriptor
  245. *
  246. * The existence of this virtio hdr resource entry means that the firmware
  247. * of this @rproc supports this virtio device.
  248. *
  249. * Currently we support only a single virtio device of type VIRTIO_ID_RPMSG,
  250. * but the plan is to remove this limitation and support any number
  251. * of virtio devices (and of any type). We'll also add support for dynamically
  252. * adding (and removing) virtio devices over the rpmsg bus, but small
  253. * firmwares that doesn't want to get involved with rpmsg will be able
  254. * to simple use the resource table for this.
  255. *
  256. * At this point this virtio header entry is rather simple: it just
  257. * announces the virtio device id and the supported virtio device features.
  258. * The plan though is to extend this to include the vring information and
  259. * the virtio config space, too (but first, some resource table overhaul
  260. * is needed: move from fixed-sized to variable-length TLV entries).
  261. *
  262. * For now, the 'flags' member of the resource entry contains the virtio
  263. * device id, the 'da' member contains the device features, and 'pa' is
  264. * where we need to store the guest features once negotiation completes.
  265. * As usual, the 'id' member of this resource contains the index of this
  266. * resource type (i.e. is this the first virtio hdr entry, the 2nd, ...).
  267. *
  268. * Returns 0 on success, or an appropriate error code otherwise
  269. */
  270. static int rproc_handle_virtio_hdr(struct rproc *rproc, struct fw_resource *rsc)
  271. {
  272. struct rproc_vdev *rvdev;
  273. /* we only support VIRTIO_ID_RPMSG devices for now */
  274. if (rsc->flags != VIRTIO_ID_RPMSG) {
  275. dev_warn(rproc->dev, "unsupported vdev: %d\n", rsc->flags);
  276. return -EINVAL;
  277. }
  278. /* we only support a single vdev per rproc for now */
  279. if (rsc->id || rproc->rvdev) {
  280. dev_warn(rproc->dev, "redundant vdev entry: %s\n", rsc->name);
  281. return -EINVAL;
  282. }
  283. rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
  284. if (!rvdev)
  285. return -ENOMEM;
  286. /* remember the device features */
  287. rvdev->dfeatures = rsc->da;
  288. rproc->rvdev = rvdev;
  289. rvdev->rproc = rproc;
  290. return 0;
  291. }
  292. /**
  293. * rproc_handle_vring() - handle a vring fw resource
  294. * @rproc: the remote processor
  295. * @rsc: the vring resource descriptor
  296. *
  297. * This resource entry requires allocation of non-cacheable memory
  298. * for a virtio vring. Currently we only support two vrings per remote
  299. * processor, required for the virtio rpmsg device.
  300. *
  301. * The 'len' member of @rsc should contain the number of buffers this vring
  302. * support and 'da' should either contain the device address where
  303. * the remote processor is expecting the vring, or indicate that
  304. * dynamically allocation of the vring's device address is supported.
  305. *
  306. * Note: 'da' is currently not handled. This will be revised when the generic
  307. * iommu-based DMA API will arrive, or a dynanic & non-iommu use case show
  308. * up. Meanwhile, statically-addressed iommu-based images should use
  309. * RSC_DEVMEM resource entries to map their require 'da' to the physical
  310. * address of their base CMA region.
  311. *
  312. * Returns 0 on success, or an appropriate error code otherwise
  313. */
  314. static int rproc_handle_vring(struct rproc *rproc, struct fw_resource *rsc)
  315. {
  316. struct device *dev = rproc->dev;
  317. struct rproc_vdev *rvdev = rproc->rvdev;
  318. dma_addr_t dma;
  319. int size, id = rsc->id;
  320. void *va;
  321. /* no vdev is in place ? */
  322. if (!rvdev) {
  323. dev_err(dev, "vring requested without a virtio dev entry\n");
  324. return -EINVAL;
  325. }
  326. /* the firmware must provide the expected queue size */
  327. if (!rsc->len) {
  328. dev_err(dev, "missing expected queue size\n");
  329. return -EINVAL;
  330. }
  331. /* we currently support two vrings per rproc (for rx and tx) */
  332. if (id >= ARRAY_SIZE(rvdev->vring)) {
  333. dev_err(dev, "%s: invalid vring id %d\n", rsc->name, id);
  334. return -EINVAL;
  335. }
  336. /* have we already allocated this vring id ? */
  337. if (rvdev->vring[id].len) {
  338. dev_err(dev, "%s: duplicated id %d\n", rsc->name, id);
  339. return -EINVAL;
  340. }
  341. /* actual size of vring (in bytes) */
  342. size = PAGE_ALIGN(vring_size(rsc->len, AMP_VRING_ALIGN));
  343. /*
  344. * Allocate non-cacheable memory for the vring. In the future
  345. * this call will also configure the IOMMU for us
  346. */
  347. va = dma_alloc_coherent(dev, size, &dma, GFP_KERNEL);
  348. if (!va) {
  349. dev_err(dev, "dma_alloc_coherent failed\n");
  350. return -ENOMEM;
  351. }
  352. dev_dbg(dev, "vring%d: va %p dma %x qsz %d ring size %x\n", id, va,
  353. dma, rsc->len, size);
  354. rvdev->vring[id].len = rsc->len;
  355. rvdev->vring[id].va = va;
  356. rvdev->vring[id].dma = dma;
  357. return 0;
  358. }
  359. /**
  360. * rproc_handle_trace() - handle a shared trace buffer resource
  361. * @rproc: the remote processor
  362. * @rsc: the trace resource descriptor
  363. *
  364. * In case the remote processor dumps trace logs into memory,
  365. * export it via debugfs.
  366. *
  367. * Currently, the 'da' member of @rsc should contain the device address
  368. * where the remote processor is dumping the traces. Later we could also
  369. * support dynamically allocating this address using the generic
  370. * DMA API (but currently there isn't a use case for that).
  371. *
  372. * Returns 0 on success, or an appropriate error code otherwise
  373. */
  374. static int rproc_handle_trace(struct rproc *rproc, struct fw_resource *rsc)
  375. {
  376. struct rproc_mem_entry *trace;
  377. struct device *dev = rproc->dev;
  378. void *ptr;
  379. char name[15];
  380. /* what's the kernel address of this resource ? */
  381. ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
  382. if (!ptr) {
  383. dev_err(dev, "erroneous trace resource entry\n");
  384. return -EINVAL;
  385. }
  386. trace = kzalloc(sizeof(*trace), GFP_KERNEL);
  387. if (!trace) {
  388. dev_err(dev, "kzalloc trace failed\n");
  389. return -ENOMEM;
  390. }
  391. /* set the trace buffer dma properties */
  392. trace->len = rsc->len;
  393. trace->va = ptr;
  394. /* make sure snprintf always null terminates, even if truncating */
  395. snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
  396. /* create the debugfs entry */
  397. trace->priv = rproc_create_trace_file(name, rproc, trace);
  398. if (!trace->priv) {
  399. trace->va = NULL;
  400. kfree(trace);
  401. return -EINVAL;
  402. }
  403. list_add_tail(&trace->node, &rproc->traces);
  404. rproc->num_traces++;
  405. dev_dbg(dev, "%s added: va %p, da 0x%llx, len 0x%x\n", name, ptr,
  406. rsc->da, rsc->len);
  407. return 0;
  408. }
  409. /**
  410. * rproc_handle_devmem() - handle devmem resource entry
  411. * @rproc: remote processor handle
  412. * @rsc: the devmem resource entry
  413. *
  414. * Remote processors commonly need to access certain on-chip peripherals.
  415. *
  416. * Some of these remote processors access memory via an iommu device,
  417. * and might require us to configure their iommu before they can access
  418. * the on-chip peripherals they need.
  419. *
  420. * This resource entry is a request to map such a peripheral device.
  421. *
  422. * These devmem entries will contain the physical address of the device in
  423. * the 'pa' member. If a specific device address is expected, then 'da' will
  424. * contain it (currently this is the only use case supported). 'len' will
  425. * contain the size of the physical region we need to map.
  426. *
  427. * Currently we just "trust" those devmem entries to contain valid physical
  428. * addresses, but this is going to change: we want the implementations to
  429. * tell us ranges of physical addresses the firmware is allowed to request,
  430. * and not allow firmwares to request access to physical addresses that
  431. * are outside those ranges.
  432. */
  433. static int rproc_handle_devmem(struct rproc *rproc, struct fw_resource *rsc)
  434. {
  435. struct rproc_mem_entry *mapping;
  436. int ret;
  437. /* no point in handling this resource without a valid iommu domain */
  438. if (!rproc->domain)
  439. return -EINVAL;
  440. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  441. if (!mapping) {
  442. dev_err(rproc->dev, "kzalloc mapping failed\n");
  443. return -ENOMEM;
  444. }
  445. ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
  446. if (ret) {
  447. dev_err(rproc->dev, "failed to map devmem: %d\n", ret);
  448. goto out;
  449. }
  450. /*
  451. * We'll need this info later when we'll want to unmap everything
  452. * (e.g. on shutdown).
  453. *
  454. * We can't trust the remote processor not to change the resource
  455. * table, so we must maintain this info independently.
  456. */
  457. mapping->da = rsc->da;
  458. mapping->len = rsc->len;
  459. list_add_tail(&mapping->node, &rproc->mappings);
  460. dev_dbg(rproc->dev, "mapped devmem pa 0x%llx, da 0x%llx, len 0x%x\n",
  461. rsc->pa, rsc->da, rsc->len);
  462. return 0;
  463. out:
  464. kfree(mapping);
  465. return ret;
  466. }
  467. /**
  468. * rproc_handle_carveout() - handle phys contig memory allocation requests
  469. * @rproc: rproc handle
  470. * @rsc: the resource entry
  471. *
  472. * This function will handle firmware requests for allocation of physically
  473. * contiguous memory regions.
  474. *
  475. * These request entries should come first in the firmware's resource table,
  476. * as other firmware entries might request placing other data objects inside
  477. * these memory regions (e.g. data/code segments, trace resource entries, ...).
  478. *
  479. * Allocating memory this way helps utilizing the reserved physical memory
  480. * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
  481. * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
  482. * pressure is important; it may have a substantial impact on performance.
  483. */
  484. static int rproc_handle_carveout(struct rproc *rproc, struct fw_resource *rsc)
  485. {
  486. struct rproc_mem_entry *carveout, *mapping;
  487. struct device *dev = rproc->dev;
  488. dma_addr_t dma;
  489. void *va;
  490. int ret;
  491. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  492. if (!mapping) {
  493. dev_err(dev, "kzalloc mapping failed\n");
  494. return -ENOMEM;
  495. }
  496. carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
  497. if (!carveout) {
  498. dev_err(dev, "kzalloc carveout failed\n");
  499. ret = -ENOMEM;
  500. goto free_mapping;
  501. }
  502. va = dma_alloc_coherent(dev, rsc->len, &dma, GFP_KERNEL);
  503. if (!va) {
  504. dev_err(dev, "failed to dma alloc carveout: %d\n", rsc->len);
  505. ret = -ENOMEM;
  506. goto free_carv;
  507. }
  508. dev_dbg(dev, "carveout va %p, dma %x, len 0x%x\n", va, dma, rsc->len);
  509. /*
  510. * Ok, this is non-standard.
  511. *
  512. * Sometimes we can't rely on the generic iommu-based DMA API
  513. * to dynamically allocate the device address and then set the IOMMU
  514. * tables accordingly, because some remote processors might
  515. * _require_ us to use hard coded device addresses that their
  516. * firmware was compiled with.
  517. *
  518. * In this case, we must use the IOMMU API directly and map
  519. * the memory to the device address as expected by the remote
  520. * processor.
  521. *
  522. * Obviously such remote processor devices should not be configured
  523. * to use the iommu-based DMA API: we expect 'dma' to contain the
  524. * physical address in this case.
  525. */
  526. if (rproc->domain) {
  527. ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
  528. rsc->flags);
  529. if (ret) {
  530. dev_err(dev, "iommu_map failed: %d\n", ret);
  531. goto dma_free;
  532. }
  533. /*
  534. * We'll need this info later when we'll want to unmap
  535. * everything (e.g. on shutdown).
  536. *
  537. * We can't trust the remote processor not to change the
  538. * resource table, so we must maintain this info independently.
  539. */
  540. mapping->da = rsc->da;
  541. mapping->len = rsc->len;
  542. list_add_tail(&mapping->node, &rproc->mappings);
  543. dev_dbg(dev, "carveout mapped 0x%llx to 0x%x\n", rsc->da, dma);
  544. /*
  545. * Some remote processors might need to know the pa
  546. * even though they are behind an IOMMU. E.g., OMAP4's
  547. * remote M3 processor needs this so it can control
  548. * on-chip hardware accelerators that are not behind
  549. * the IOMMU, and therefor must know the pa.
  550. *
  551. * Generally we don't want to expose physical addresses
  552. * if we don't have to (remote processors are generally
  553. * _not_ trusted), so we might want to do this only for
  554. * remote processor that _must_ have this (e.g. OMAP4's
  555. * dual M3 subsystem).
  556. */
  557. rsc->pa = dma;
  558. }
  559. carveout->va = va;
  560. carveout->len = rsc->len;
  561. carveout->dma = dma;
  562. carveout->da = rsc->da;
  563. list_add_tail(&carveout->node, &rproc->carveouts);
  564. return 0;
  565. dma_free:
  566. dma_free_coherent(dev, rsc->len, va, dma);
  567. free_carv:
  568. kfree(carveout);
  569. free_mapping:
  570. kfree(mapping);
  571. return ret;
  572. }
  573. /* handle firmware resource entries before booting the remote processor */
  574. static int
  575. rproc_handle_boot_rsc(struct rproc *rproc, struct fw_resource *rsc, int len)
  576. {
  577. struct device *dev = rproc->dev;
  578. int ret = 0;
  579. while (len >= sizeof(*rsc)) {
  580. dev_dbg(dev, "rsc: type %d, da 0x%llx, pa 0x%llx, len 0x%x, "
  581. "id %d, name %s, flags %x\n", rsc->type, rsc->da,
  582. rsc->pa, rsc->len, rsc->id, rsc->name, rsc->flags);
  583. switch (rsc->type) {
  584. case RSC_CARVEOUT:
  585. ret = rproc_handle_carveout(rproc, rsc);
  586. break;
  587. case RSC_DEVMEM:
  588. ret = rproc_handle_devmem(rproc, rsc);
  589. break;
  590. case RSC_TRACE:
  591. ret = rproc_handle_trace(rproc, rsc);
  592. break;
  593. case RSC_VRING:
  594. ret = rproc_handle_vring(rproc, rsc);
  595. break;
  596. case RSC_VIRTIO_DEV:
  597. /* this one is handled early upon registration */
  598. break;
  599. default:
  600. dev_warn(dev, "unsupported resource %d\n", rsc->type);
  601. break;
  602. }
  603. if (ret)
  604. break;
  605. rsc++;
  606. len -= sizeof(*rsc);
  607. }
  608. return ret;
  609. }
  610. /* handle firmware resource entries while registering the remote processor */
  611. static int
  612. rproc_handle_virtio_rsc(struct rproc *rproc, struct fw_resource *rsc, int len)
  613. {
  614. struct device *dev = rproc->dev;
  615. int ret = -ENODEV;
  616. for (; len >= sizeof(*rsc); rsc++, len -= sizeof(*rsc))
  617. if (rsc->type == RSC_VIRTIO_DEV) {
  618. dev_dbg(dev, "found vdev %d/%s features %llx\n",
  619. rsc->flags, rsc->name, rsc->da);
  620. ret = rproc_handle_virtio_hdr(rproc, rsc);
  621. break;
  622. }
  623. return ret;
  624. }
  625. /**
  626. * rproc_handle_resources() - find and handle the resource table
  627. * @rproc: the rproc handle
  628. * @elf_data: the content of the ELF firmware image
  629. * @len: firmware size (in bytes)
  630. * @handler: function that should be used to handle the resource table
  631. *
  632. * This function finds the resource table inside the remote processor's
  633. * firmware, and invoke a user-supplied handler with it (we have two
  634. * possible handlers: one is invoked upon registration of @rproc,
  635. * in order to register the supported virito devices, and the other is
  636. * invoked when @rproc is actually booted).
  637. *
  638. * Currently this function fails if a resource table doesn't exist.
  639. * This restriction will be removed when we'll start supporting remote
  640. * processors that don't need a resource table.
  641. */
  642. static int rproc_handle_resources(struct rproc *rproc, const u8 *elf_data,
  643. size_t len, rproc_handle_resources_t handler)
  644. {
  645. struct elf32_hdr *ehdr;
  646. struct elf32_shdr *shdr;
  647. const char *name_table;
  648. int i, ret = -EINVAL;
  649. ehdr = (struct elf32_hdr *)elf_data;
  650. shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
  651. name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
  652. /* look for the resource table and handle it */
  653. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  654. if (!strcmp(name_table + shdr->sh_name, ".resource_table")) {
  655. struct fw_resource *table = (struct fw_resource *)
  656. (elf_data + shdr->sh_offset);
  657. if (shdr->sh_offset + shdr->sh_size > len) {
  658. dev_err(rproc->dev,
  659. "truncated fw: need 0x%x avail 0x%x\n",
  660. shdr->sh_offset + shdr->sh_size, len);
  661. ret = -EINVAL;
  662. }
  663. ret = handler(rproc, table, shdr->sh_size);
  664. break;
  665. }
  666. }
  667. return ret;
  668. }
  669. /**
  670. * rproc_resource_cleanup() - clean up and free all acquired resources
  671. * @rproc: rproc handle
  672. *
  673. * This function will free all resources acquired for @rproc, and it
  674. * is called when @rproc shuts down, or just failed booting.
  675. */
  676. static void rproc_resource_cleanup(struct rproc *rproc)
  677. {
  678. struct rproc_mem_entry *entry, *tmp;
  679. struct device *dev = rproc->dev;
  680. struct rproc_vdev *rvdev = rproc->rvdev;
  681. int i;
  682. /* clean up debugfs trace entries */
  683. list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
  684. rproc_remove_trace_file(entry->priv);
  685. rproc->num_traces--;
  686. list_del(&entry->node);
  687. kfree(entry);
  688. }
  689. /* free the coherent memory allocated for the vrings */
  690. for (i = 0; rvdev && i < ARRAY_SIZE(rvdev->vring); i++) {
  691. int qsz = rvdev->vring[i].len;
  692. void *va = rvdev->vring[i].va;
  693. int dma = rvdev->vring[i].dma;
  694. /* virtqueue size is expressed in number of buffers supported */
  695. if (qsz) {
  696. /* how many bytes does this vring really occupy ? */
  697. int size = PAGE_ALIGN(vring_size(qsz, AMP_VRING_ALIGN));
  698. dma_free_coherent(rproc->dev, size, va, dma);
  699. rvdev->vring[i].len = 0;
  700. }
  701. }
  702. /* clean up carveout allocations */
  703. list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
  704. dma_free_coherent(dev, entry->len, entry->va, entry->dma);
  705. list_del(&entry->node);
  706. kfree(entry);
  707. }
  708. /* clean up iommu mapping entries */
  709. list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
  710. size_t unmapped;
  711. unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
  712. if (unmapped != entry->len) {
  713. /* nothing much to do besides complaining */
  714. dev_err(dev, "failed to unmap %u/%u\n", entry->len,
  715. unmapped);
  716. }
  717. list_del(&entry->node);
  718. kfree(entry);
  719. }
  720. }
  721. /* make sure this fw image is sane */
  722. static int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
  723. {
  724. const char *name = rproc->firmware;
  725. struct device *dev = rproc->dev;
  726. struct elf32_hdr *ehdr;
  727. if (!fw) {
  728. dev_err(dev, "failed to load %s\n", name);
  729. return -EINVAL;
  730. }
  731. if (fw->size < sizeof(struct elf32_hdr)) {
  732. dev_err(dev, "Image is too small\n");
  733. return -EINVAL;
  734. }
  735. ehdr = (struct elf32_hdr *)fw->data;
  736. if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
  737. dev_err(dev, "Image is too small\n");
  738. return -EINVAL;
  739. }
  740. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  741. dev_err(dev, "Image is corrupted (bad magic)\n");
  742. return -EINVAL;
  743. }
  744. if (ehdr->e_phnum == 0) {
  745. dev_err(dev, "No loadable segments\n");
  746. return -EINVAL;
  747. }
  748. if (ehdr->e_phoff > fw->size) {
  749. dev_err(dev, "Firmware size is too small\n");
  750. return -EINVAL;
  751. }
  752. return 0;
  753. }
  754. /*
  755. * take a firmware and boot a remote processor with it.
  756. */
  757. static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
  758. {
  759. struct device *dev = rproc->dev;
  760. const char *name = rproc->firmware;
  761. struct elf32_hdr *ehdr;
  762. int ret;
  763. ret = rproc_fw_sanity_check(rproc, fw);
  764. if (ret)
  765. return ret;
  766. ehdr = (struct elf32_hdr *)fw->data;
  767. dev_info(dev, "Booting fw image %s, size %d\n", name, fw->size);
  768. /*
  769. * if enabling an IOMMU isn't relevant for this rproc, this is
  770. * just a nop
  771. */
  772. ret = rproc_enable_iommu(rproc);
  773. if (ret) {
  774. dev_err(dev, "can't enable iommu: %d\n", ret);
  775. return ret;
  776. }
  777. /*
  778. * The ELF entry point is the rproc's boot addr (though this is not
  779. * a configurable property of all remote processors: some will always
  780. * boot at a specific hardcoded address).
  781. */
  782. rproc->bootaddr = ehdr->e_entry;
  783. /* handle fw resources which are required to boot rproc */
  784. ret = rproc_handle_resources(rproc, fw->data, fw->size,
  785. rproc_handle_boot_rsc);
  786. if (ret) {
  787. dev_err(dev, "Failed to process resources: %d\n", ret);
  788. goto clean_up;
  789. }
  790. /* load the ELF segments to memory */
  791. ret = rproc_load_segments(rproc, fw->data, fw->size);
  792. if (ret) {
  793. dev_err(dev, "Failed to load program segments: %d\n", ret);
  794. goto clean_up;
  795. }
  796. /* power up the remote processor */
  797. ret = rproc->ops->start(rproc);
  798. if (ret) {
  799. dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
  800. goto clean_up;
  801. }
  802. rproc->state = RPROC_RUNNING;
  803. dev_info(dev, "remote processor %s is now up\n", rproc->name);
  804. return 0;
  805. clean_up:
  806. rproc_resource_cleanup(rproc);
  807. rproc_disable_iommu(rproc);
  808. return ret;
  809. }
  810. /*
  811. * take a firmware and look for virtio devices to register.
  812. *
  813. * Note: this function is called asynchronously upon registration of the
  814. * remote processor (so we must wait until it completes before we try
  815. * to unregister the device. one other option is just to use kref here,
  816. * that might be cleaner).
  817. */
  818. static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
  819. {
  820. struct rproc *rproc = context;
  821. struct device *dev = rproc->dev;
  822. int ret;
  823. if (rproc_fw_sanity_check(rproc, fw) < 0)
  824. goto out;
  825. /* does the fw supports any virtio devices ? */
  826. ret = rproc_handle_resources(rproc, fw->data, fw->size,
  827. rproc_handle_virtio_rsc);
  828. if (ret) {
  829. dev_info(dev, "No fw virtio device was found\n");
  830. goto out;
  831. }
  832. /* add the virtio device (currently only rpmsg vdevs are supported) */
  833. ret = rproc_add_rpmsg_vdev(rproc);
  834. if (ret)
  835. goto out;
  836. out:
  837. if (fw)
  838. release_firmware(fw);
  839. /* allow rproc_unregister() contexts, if any, to proceed */
  840. complete_all(&rproc->firmware_loading_complete);
  841. }
  842. /**
  843. * rproc_boot() - boot a remote processor
  844. * @rproc: handle of a remote processor
  845. *
  846. * Boot a remote processor (i.e. load its firmware, power it on, ...).
  847. *
  848. * If the remote processor is already powered on, this function immediately
  849. * returns (successfully).
  850. *
  851. * Returns 0 on success, and an appropriate error value otherwise.
  852. */
  853. int rproc_boot(struct rproc *rproc)
  854. {
  855. const struct firmware *firmware_p;
  856. struct device *dev;
  857. int ret;
  858. if (!rproc) {
  859. pr_err("invalid rproc handle\n");
  860. return -EINVAL;
  861. }
  862. dev = rproc->dev;
  863. ret = mutex_lock_interruptible(&rproc->lock);
  864. if (ret) {
  865. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  866. return ret;
  867. }
  868. /* loading a firmware is required */
  869. if (!rproc->firmware) {
  870. dev_err(dev, "%s: no firmware to load\n", __func__);
  871. ret = -EINVAL;
  872. goto unlock_mutex;
  873. }
  874. /* prevent underlying implementation from being removed */
  875. if (!try_module_get(dev->driver->owner)) {
  876. dev_err(dev, "%s: can't get owner\n", __func__);
  877. ret = -EINVAL;
  878. goto unlock_mutex;
  879. }
  880. /* skip the boot process if rproc is already powered up */
  881. if (atomic_inc_return(&rproc->power) > 1) {
  882. ret = 0;
  883. goto unlock_mutex;
  884. }
  885. dev_info(dev, "powering up %s\n", rproc->name);
  886. /* load firmware */
  887. ret = request_firmware(&firmware_p, rproc->firmware, dev);
  888. if (ret < 0) {
  889. dev_err(dev, "request_firmware failed: %d\n", ret);
  890. goto downref_rproc;
  891. }
  892. ret = rproc_fw_boot(rproc, firmware_p);
  893. release_firmware(firmware_p);
  894. downref_rproc:
  895. if (ret) {
  896. module_put(dev->driver->owner);
  897. atomic_dec(&rproc->power);
  898. }
  899. unlock_mutex:
  900. mutex_unlock(&rproc->lock);
  901. return ret;
  902. }
  903. EXPORT_SYMBOL(rproc_boot);
  904. /**
  905. * rproc_shutdown() - power off the remote processor
  906. * @rproc: the remote processor
  907. *
  908. * Power off a remote processor (previously booted with rproc_boot()).
  909. *
  910. * In case @rproc is still being used by an additional user(s), then
  911. * this function will just decrement the power refcount and exit,
  912. * without really powering off the device.
  913. *
  914. * Every call to rproc_boot() must (eventually) be accompanied by a call
  915. * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
  916. *
  917. * Notes:
  918. * - we're not decrementing the rproc's refcount, only the power refcount.
  919. * which means that the @rproc handle stays valid even after rproc_shutdown()
  920. * returns, and users can still use it with a subsequent rproc_boot(), if
  921. * needed.
  922. * - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly
  923. * because rproc_shutdown() _does not_ decrement the refcount of @rproc.
  924. * To decrement the refcount of @rproc, use rproc_put() (but _only_ if
  925. * you acquired @rproc using rproc_get_by_name()).
  926. */
  927. void rproc_shutdown(struct rproc *rproc)
  928. {
  929. struct device *dev = rproc->dev;
  930. int ret;
  931. ret = mutex_lock_interruptible(&rproc->lock);
  932. if (ret) {
  933. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  934. return;
  935. }
  936. /* if the remote proc is still needed, bail out */
  937. if (!atomic_dec_and_test(&rproc->power))
  938. goto out;
  939. /* power off the remote processor */
  940. ret = rproc->ops->stop(rproc);
  941. if (ret) {
  942. atomic_inc(&rproc->power);
  943. dev_err(dev, "can't stop rproc: %d\n", ret);
  944. goto out;
  945. }
  946. /* clean up all acquired resources */
  947. rproc_resource_cleanup(rproc);
  948. rproc_disable_iommu(rproc);
  949. rproc->state = RPROC_OFFLINE;
  950. dev_info(dev, "stopped remote processor %s\n", rproc->name);
  951. out:
  952. mutex_unlock(&rproc->lock);
  953. if (!ret)
  954. module_put(dev->driver->owner);
  955. }
  956. EXPORT_SYMBOL(rproc_shutdown);
  957. /**
  958. * rproc_release() - completely deletes the existence of a remote processor
  959. * @kref: the rproc's kref
  960. *
  961. * This function should _never_ be called directly.
  962. *
  963. * The only reasonable location to use it is as an argument when kref_put'ing
  964. * @rproc's refcount.
  965. *
  966. * This way it will be called when no one holds a valid pointer to this @rproc
  967. * anymore (and obviously after it is removed from the rprocs klist).
  968. *
  969. * Note: this function is not static because rproc_vdev_release() needs it when
  970. * it decrements @rproc's refcount.
  971. */
  972. void rproc_release(struct kref *kref)
  973. {
  974. struct rproc *rproc = container_of(kref, struct rproc, refcount);
  975. dev_info(rproc->dev, "removing %s\n", rproc->name);
  976. rproc_delete_debug_dir(rproc);
  977. /* at this point no one holds a reference to rproc anymore */
  978. kfree(rproc);
  979. }
  980. /* will be called when an rproc is added to the rprocs klist */
  981. static void klist_rproc_get(struct klist_node *n)
  982. {
  983. struct rproc *rproc = container_of(n, struct rproc, node);
  984. kref_get(&rproc->refcount);
  985. }
  986. /* will be called when an rproc is removed from the rprocs klist */
  987. static void klist_rproc_put(struct klist_node *n)
  988. {
  989. struct rproc *rproc = container_of(n, struct rproc, node);
  990. kref_put(&rproc->refcount, rproc_release);
  991. }
  992. static struct rproc *next_rproc(struct klist_iter *i)
  993. {
  994. struct klist_node *n;
  995. n = klist_next(i);
  996. if (!n)
  997. return NULL;
  998. return container_of(n, struct rproc, node);
  999. }
  1000. /**
  1001. * rproc_get_by_name() - find a remote processor by name and boot it
  1002. * @name: name of the remote processor
  1003. *
  1004. * Finds an rproc handle using the remote processor's name, and then
  1005. * boot it. If it's already powered on, then just immediately return
  1006. * (successfully).
  1007. *
  1008. * Returns the rproc handle on success, and NULL on failure.
  1009. *
  1010. * This function increments the remote processor's refcount, so always
  1011. * use rproc_put() to decrement it back once rproc isn't needed anymore.
  1012. *
  1013. * Note: currently this function (and its counterpart rproc_put()) are not
  1014. * used anymore by the rpmsg subsystem. We need to scrutinize the use cases
  1015. * that still need them, and see if we can migrate them to use the non
  1016. * name-based boot/shutdown interface.
  1017. */
  1018. struct rproc *rproc_get_by_name(const char *name)
  1019. {
  1020. struct rproc *rproc;
  1021. struct klist_iter i;
  1022. int ret;
  1023. /* find the remote processor, and upref its refcount */
  1024. klist_iter_init(&rprocs, &i);
  1025. while ((rproc = next_rproc(&i)) != NULL)
  1026. if (!strcmp(rproc->name, name)) {
  1027. kref_get(&rproc->refcount);
  1028. break;
  1029. }
  1030. klist_iter_exit(&i);
  1031. /* can't find this rproc ? */
  1032. if (!rproc) {
  1033. pr_err("can't find remote processor %s\n", name);
  1034. return NULL;
  1035. }
  1036. ret = rproc_boot(rproc);
  1037. if (ret < 0) {
  1038. kref_put(&rproc->refcount, rproc_release);
  1039. return NULL;
  1040. }
  1041. return rproc;
  1042. }
  1043. EXPORT_SYMBOL(rproc_get_by_name);
  1044. /**
  1045. * rproc_put() - decrement the refcount of a remote processor, and shut it down
  1046. * @rproc: the remote processor
  1047. *
  1048. * This function tries to shutdown @rproc, and it then decrements its
  1049. * refcount.
  1050. *
  1051. * After this function returns, @rproc may _not_ be used anymore, and its
  1052. * handle should be considered invalid.
  1053. *
  1054. * This function should be called _iff_ the @rproc handle was grabbed by
  1055. * calling rproc_get_by_name().
  1056. */
  1057. void rproc_put(struct rproc *rproc)
  1058. {
  1059. /* try to power off the remote processor */
  1060. rproc_shutdown(rproc);
  1061. /* downref rproc's refcount */
  1062. kref_put(&rproc->refcount, rproc_release);
  1063. }
  1064. EXPORT_SYMBOL(rproc_put);
  1065. /**
  1066. * rproc_register() - register a remote processor
  1067. * @rproc: the remote processor handle to register
  1068. *
  1069. * Registers @rproc with the remoteproc framework, after it has been
  1070. * allocated with rproc_alloc().
  1071. *
  1072. * This is called by the platform-specific rproc implementation, whenever
  1073. * a new remote processor device is probed.
  1074. *
  1075. * Returns 0 on success and an appropriate error code otherwise.
  1076. *
  1077. * Note: this function initiates an asynchronous firmware loading
  1078. * context, which will look for virtio devices supported by the rproc's
  1079. * firmware.
  1080. *
  1081. * If found, those virtio devices will be created and added, so as a result
  1082. * of registering this remote processor, additional virtio drivers will be
  1083. * probed.
  1084. *
  1085. * Currently, though, we only support a single RPMSG virtio vdev per remote
  1086. * processor.
  1087. */
  1088. int rproc_register(struct rproc *rproc)
  1089. {
  1090. struct device *dev = rproc->dev;
  1091. int ret = 0;
  1092. /* expose to rproc_get_by_name users */
  1093. klist_add_tail(&rproc->node, &rprocs);
  1094. dev_info(rproc->dev, "%s is available\n", rproc->name);
  1095. dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
  1096. dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
  1097. /* create debugfs entries */
  1098. rproc_create_debug_dir(rproc);
  1099. /* rproc_unregister() calls must wait until async loader completes */
  1100. init_completion(&rproc->firmware_loading_complete);
  1101. /*
  1102. * We must retrieve early virtio configuration info from
  1103. * the firmware (e.g. whether to register a virtio rpmsg device,
  1104. * what virtio features does it support, ...).
  1105. *
  1106. * We're initiating an asynchronous firmware loading, so we can
  1107. * be built-in kernel code, without hanging the boot process.
  1108. */
  1109. ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
  1110. rproc->firmware, dev, GFP_KERNEL,
  1111. rproc, rproc_fw_config_virtio);
  1112. if (ret < 0) {
  1113. dev_err(dev, "request_firmware_nowait failed: %d\n", ret);
  1114. complete_all(&rproc->firmware_loading_complete);
  1115. klist_remove(&rproc->node);
  1116. }
  1117. return ret;
  1118. }
  1119. EXPORT_SYMBOL(rproc_register);
  1120. /**
  1121. * rproc_alloc() - allocate a remote processor handle
  1122. * @dev: the underlying device
  1123. * @name: name of this remote processor
  1124. * @ops: platform-specific handlers (mainly start/stop)
  1125. * @firmware: name of firmware file to load
  1126. * @len: length of private data needed by the rproc driver (in bytes)
  1127. *
  1128. * Allocates a new remote processor handle, but does not register
  1129. * it yet.
  1130. *
  1131. * This function should be used by rproc implementations during initialization
  1132. * of the remote processor.
  1133. *
  1134. * After creating an rproc handle using this function, and when ready,
  1135. * implementations should then call rproc_register() to complete
  1136. * the registration of the remote processor.
  1137. *
  1138. * On success the new rproc is returned, and on failure, NULL.
  1139. *
  1140. * Note: _never_ directly deallocate @rproc, even if it was not registered
  1141. * yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free().
  1142. */
  1143. struct rproc *rproc_alloc(struct device *dev, const char *name,
  1144. const struct rproc_ops *ops,
  1145. const char *firmware, int len)
  1146. {
  1147. struct rproc *rproc;
  1148. if (!dev || !name || !ops)
  1149. return NULL;
  1150. rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
  1151. if (!rproc) {
  1152. dev_err(dev, "%s: kzalloc failed\n", __func__);
  1153. return NULL;
  1154. }
  1155. rproc->dev = dev;
  1156. rproc->name = name;
  1157. rproc->ops = ops;
  1158. rproc->firmware = firmware;
  1159. rproc->priv = &rproc[1];
  1160. atomic_set(&rproc->power, 0);
  1161. kref_init(&rproc->refcount);
  1162. mutex_init(&rproc->lock);
  1163. INIT_LIST_HEAD(&rproc->carveouts);
  1164. INIT_LIST_HEAD(&rproc->mappings);
  1165. INIT_LIST_HEAD(&rproc->traces);
  1166. rproc->state = RPROC_OFFLINE;
  1167. return rproc;
  1168. }
  1169. EXPORT_SYMBOL(rproc_alloc);
  1170. /**
  1171. * rproc_free() - free an rproc handle that was allocated by rproc_alloc
  1172. * @rproc: the remote processor handle
  1173. *
  1174. * This function should _only_ be used if @rproc was only allocated,
  1175. * but not registered yet.
  1176. *
  1177. * If @rproc was already successfully registered (by calling rproc_register()),
  1178. * then use rproc_unregister() instead.
  1179. */
  1180. void rproc_free(struct rproc *rproc)
  1181. {
  1182. kfree(rproc);
  1183. }
  1184. EXPORT_SYMBOL(rproc_free);
  1185. /**
  1186. * rproc_unregister() - unregister a remote processor
  1187. * @rproc: rproc handle to unregister
  1188. *
  1189. * Unregisters a remote processor, and decrements its refcount.
  1190. * If its refcount drops to zero, then @rproc will be freed. If not,
  1191. * it will be freed later once the last reference is dropped.
  1192. *
  1193. * This function should be called when the platform specific rproc
  1194. * implementation decides to remove the rproc device. it should
  1195. * _only_ be called if a previous invocation of rproc_register()
  1196. * has completed successfully.
  1197. *
  1198. * After rproc_unregister() returns, @rproc is _not_ valid anymore and
  1199. * it shouldn't be used. More specifically, don't call rproc_free()
  1200. * or try to directly free @rproc after rproc_unregister() returns;
  1201. * none of these are needed, and calling them is a bug.
  1202. *
  1203. * Returns 0 on success and -EINVAL if @rproc isn't valid.
  1204. */
  1205. int rproc_unregister(struct rproc *rproc)
  1206. {
  1207. if (!rproc)
  1208. return -EINVAL;
  1209. /* if rproc is just being registered, wait */
  1210. wait_for_completion(&rproc->firmware_loading_complete);
  1211. /* was an rpmsg vdev created ? */
  1212. if (rproc->rvdev)
  1213. rproc_remove_rpmsg_vdev(rproc);
  1214. klist_remove(&rproc->node);
  1215. kref_put(&rproc->refcount, rproc_release);
  1216. return 0;
  1217. }
  1218. EXPORT_SYMBOL(rproc_unregister);
  1219. static int __init remoteproc_init(void)
  1220. {
  1221. rproc_init_debugfs();
  1222. return 0;
  1223. }
  1224. module_init(remoteproc_init);
  1225. static void __exit remoteproc_exit(void)
  1226. {
  1227. rproc_exit_debugfs();
  1228. }
  1229. module_exit(remoteproc_exit);
  1230. MODULE_LICENSE("GPL v2");
  1231. MODULE_DESCRIPTION("Generic Remote Processor Framework");