remoteproc_core.c 40 KB

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