remoteproc_core.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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. char class;
  729. if (!fw) {
  730. dev_err(dev, "failed to load %s\n", name);
  731. return -EINVAL;
  732. }
  733. if (fw->size < sizeof(struct elf32_hdr)) {
  734. dev_err(dev, "Image is too small\n");
  735. return -EINVAL;
  736. }
  737. ehdr = (struct elf32_hdr *)fw->data;
  738. /* We only support ELF32 at this point */
  739. class = ehdr->e_ident[EI_CLASS];
  740. if (class != ELFCLASS32) {
  741. dev_err(dev, "Unsupported class: %d\n", class);
  742. return -EINVAL;
  743. }
  744. /* We assume the firmware has the same endianess as the host */
  745. # ifdef __LITTLE_ENDIAN
  746. if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
  747. # else /* BIG ENDIAN */
  748. if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
  749. # endif
  750. dev_err(dev, "Unsupported firmware endianess\n");
  751. return -EINVAL;
  752. }
  753. if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
  754. dev_err(dev, "Image is too small\n");
  755. return -EINVAL;
  756. }
  757. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  758. dev_err(dev, "Image is corrupted (bad magic)\n");
  759. return -EINVAL;
  760. }
  761. if (ehdr->e_phnum == 0) {
  762. dev_err(dev, "No loadable segments\n");
  763. return -EINVAL;
  764. }
  765. if (ehdr->e_phoff > fw->size) {
  766. dev_err(dev, "Firmware size is too small\n");
  767. return -EINVAL;
  768. }
  769. return 0;
  770. }
  771. /*
  772. * take a firmware and boot a remote processor with it.
  773. */
  774. static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
  775. {
  776. struct device *dev = rproc->dev;
  777. const char *name = rproc->firmware;
  778. struct elf32_hdr *ehdr;
  779. int ret;
  780. ret = rproc_fw_sanity_check(rproc, fw);
  781. if (ret)
  782. return ret;
  783. ehdr = (struct elf32_hdr *)fw->data;
  784. dev_info(dev, "Booting fw image %s, size %d\n", name, fw->size);
  785. /*
  786. * if enabling an IOMMU isn't relevant for this rproc, this is
  787. * just a nop
  788. */
  789. ret = rproc_enable_iommu(rproc);
  790. if (ret) {
  791. dev_err(dev, "can't enable iommu: %d\n", ret);
  792. return ret;
  793. }
  794. /*
  795. * The ELF entry point is the rproc's boot addr (though this is not
  796. * a configurable property of all remote processors: some will always
  797. * boot at a specific hardcoded address).
  798. */
  799. rproc->bootaddr = ehdr->e_entry;
  800. /* handle fw resources which are required to boot rproc */
  801. ret = rproc_handle_resources(rproc, fw->data, fw->size,
  802. rproc_handle_boot_rsc);
  803. if (ret) {
  804. dev_err(dev, "Failed to process resources: %d\n", ret);
  805. goto clean_up;
  806. }
  807. /* load the ELF segments to memory */
  808. ret = rproc_load_segments(rproc, fw->data, fw->size);
  809. if (ret) {
  810. dev_err(dev, "Failed to load program segments: %d\n", ret);
  811. goto clean_up;
  812. }
  813. /* power up the remote processor */
  814. ret = rproc->ops->start(rproc);
  815. if (ret) {
  816. dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
  817. goto clean_up;
  818. }
  819. rproc->state = RPROC_RUNNING;
  820. dev_info(dev, "remote processor %s is now up\n", rproc->name);
  821. return 0;
  822. clean_up:
  823. rproc_resource_cleanup(rproc);
  824. rproc_disable_iommu(rproc);
  825. return ret;
  826. }
  827. /*
  828. * take a firmware and look for virtio devices to register.
  829. *
  830. * Note: this function is called asynchronously upon registration of the
  831. * remote processor (so we must wait until it completes before we try
  832. * to unregister the device. one other option is just to use kref here,
  833. * that might be cleaner).
  834. */
  835. static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
  836. {
  837. struct rproc *rproc = context;
  838. struct device *dev = rproc->dev;
  839. int ret;
  840. if (rproc_fw_sanity_check(rproc, fw) < 0)
  841. goto out;
  842. /* does the fw supports any virtio devices ? */
  843. ret = rproc_handle_resources(rproc, fw->data, fw->size,
  844. rproc_handle_virtio_rsc);
  845. if (ret) {
  846. dev_info(dev, "No fw virtio device was found\n");
  847. goto out;
  848. }
  849. /* add the virtio device (currently only rpmsg vdevs are supported) */
  850. ret = rproc_add_rpmsg_vdev(rproc);
  851. if (ret)
  852. goto out;
  853. out:
  854. if (fw)
  855. release_firmware(fw);
  856. /* allow rproc_unregister() contexts, if any, to proceed */
  857. complete_all(&rproc->firmware_loading_complete);
  858. }
  859. /**
  860. * rproc_boot() - boot a remote processor
  861. * @rproc: handle of a remote processor
  862. *
  863. * Boot a remote processor (i.e. load its firmware, power it on, ...).
  864. *
  865. * If the remote processor is already powered on, this function immediately
  866. * returns (successfully).
  867. *
  868. * Returns 0 on success, and an appropriate error value otherwise.
  869. */
  870. int rproc_boot(struct rproc *rproc)
  871. {
  872. const struct firmware *firmware_p;
  873. struct device *dev;
  874. int ret;
  875. if (!rproc) {
  876. pr_err("invalid rproc handle\n");
  877. return -EINVAL;
  878. }
  879. dev = rproc->dev;
  880. ret = mutex_lock_interruptible(&rproc->lock);
  881. if (ret) {
  882. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  883. return ret;
  884. }
  885. /* loading a firmware is required */
  886. if (!rproc->firmware) {
  887. dev_err(dev, "%s: no firmware to load\n", __func__);
  888. ret = -EINVAL;
  889. goto unlock_mutex;
  890. }
  891. /* prevent underlying implementation from being removed */
  892. if (!try_module_get(dev->driver->owner)) {
  893. dev_err(dev, "%s: can't get owner\n", __func__);
  894. ret = -EINVAL;
  895. goto unlock_mutex;
  896. }
  897. /* skip the boot process if rproc is already powered up */
  898. if (atomic_inc_return(&rproc->power) > 1) {
  899. ret = 0;
  900. goto unlock_mutex;
  901. }
  902. dev_info(dev, "powering up %s\n", rproc->name);
  903. /* load firmware */
  904. ret = request_firmware(&firmware_p, rproc->firmware, dev);
  905. if (ret < 0) {
  906. dev_err(dev, "request_firmware failed: %d\n", ret);
  907. goto downref_rproc;
  908. }
  909. ret = rproc_fw_boot(rproc, firmware_p);
  910. release_firmware(firmware_p);
  911. downref_rproc:
  912. if (ret) {
  913. module_put(dev->driver->owner);
  914. atomic_dec(&rproc->power);
  915. }
  916. unlock_mutex:
  917. mutex_unlock(&rproc->lock);
  918. return ret;
  919. }
  920. EXPORT_SYMBOL(rproc_boot);
  921. /**
  922. * rproc_shutdown() - power off the remote processor
  923. * @rproc: the remote processor
  924. *
  925. * Power off a remote processor (previously booted with rproc_boot()).
  926. *
  927. * In case @rproc is still being used by an additional user(s), then
  928. * this function will just decrement the power refcount and exit,
  929. * without really powering off the device.
  930. *
  931. * Every call to rproc_boot() must (eventually) be accompanied by a call
  932. * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
  933. *
  934. * Notes:
  935. * - we're not decrementing the rproc's refcount, only the power refcount.
  936. * which means that the @rproc handle stays valid even after rproc_shutdown()
  937. * returns, and users can still use it with a subsequent rproc_boot(), if
  938. * needed.
  939. * - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly
  940. * because rproc_shutdown() _does not_ decrement the refcount of @rproc.
  941. * To decrement the refcount of @rproc, use rproc_put() (but _only_ if
  942. * you acquired @rproc using rproc_get_by_name()).
  943. */
  944. void rproc_shutdown(struct rproc *rproc)
  945. {
  946. struct device *dev = rproc->dev;
  947. int ret;
  948. ret = mutex_lock_interruptible(&rproc->lock);
  949. if (ret) {
  950. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  951. return;
  952. }
  953. /* if the remote proc is still needed, bail out */
  954. if (!atomic_dec_and_test(&rproc->power))
  955. goto out;
  956. /* power off the remote processor */
  957. ret = rproc->ops->stop(rproc);
  958. if (ret) {
  959. atomic_inc(&rproc->power);
  960. dev_err(dev, "can't stop rproc: %d\n", ret);
  961. goto out;
  962. }
  963. /* clean up all acquired resources */
  964. rproc_resource_cleanup(rproc);
  965. rproc_disable_iommu(rproc);
  966. rproc->state = RPROC_OFFLINE;
  967. dev_info(dev, "stopped remote processor %s\n", rproc->name);
  968. out:
  969. mutex_unlock(&rproc->lock);
  970. if (!ret)
  971. module_put(dev->driver->owner);
  972. }
  973. EXPORT_SYMBOL(rproc_shutdown);
  974. /**
  975. * rproc_release() - completely deletes the existence of a remote processor
  976. * @kref: the rproc's kref
  977. *
  978. * This function should _never_ be called directly.
  979. *
  980. * The only reasonable location to use it is as an argument when kref_put'ing
  981. * @rproc's refcount.
  982. *
  983. * This way it will be called when no one holds a valid pointer to this @rproc
  984. * anymore (and obviously after it is removed from the rprocs klist).
  985. *
  986. * Note: this function is not static because rproc_vdev_release() needs it when
  987. * it decrements @rproc's refcount.
  988. */
  989. void rproc_release(struct kref *kref)
  990. {
  991. struct rproc *rproc = container_of(kref, struct rproc, refcount);
  992. dev_info(rproc->dev, "removing %s\n", rproc->name);
  993. rproc_delete_debug_dir(rproc);
  994. /* at this point no one holds a reference to rproc anymore */
  995. kfree(rproc);
  996. }
  997. /* will be called when an rproc is added to the rprocs klist */
  998. static void klist_rproc_get(struct klist_node *n)
  999. {
  1000. struct rproc *rproc = container_of(n, struct rproc, node);
  1001. kref_get(&rproc->refcount);
  1002. }
  1003. /* will be called when an rproc is removed from the rprocs klist */
  1004. static void klist_rproc_put(struct klist_node *n)
  1005. {
  1006. struct rproc *rproc = container_of(n, struct rproc, node);
  1007. kref_put(&rproc->refcount, rproc_release);
  1008. }
  1009. static struct rproc *next_rproc(struct klist_iter *i)
  1010. {
  1011. struct klist_node *n;
  1012. n = klist_next(i);
  1013. if (!n)
  1014. return NULL;
  1015. return container_of(n, struct rproc, node);
  1016. }
  1017. /**
  1018. * rproc_get_by_name() - find a remote processor by name and boot it
  1019. * @name: name of the remote processor
  1020. *
  1021. * Finds an rproc handle using the remote processor's name, and then
  1022. * boot it. If it's already powered on, then just immediately return
  1023. * (successfully).
  1024. *
  1025. * Returns the rproc handle on success, and NULL on failure.
  1026. *
  1027. * This function increments the remote processor's refcount, so always
  1028. * use rproc_put() to decrement it back once rproc isn't needed anymore.
  1029. *
  1030. * Note: currently this function (and its counterpart rproc_put()) are not
  1031. * used anymore by the rpmsg subsystem. We need to scrutinize the use cases
  1032. * that still need them, and see if we can migrate them to use the non
  1033. * name-based boot/shutdown interface.
  1034. */
  1035. struct rproc *rproc_get_by_name(const char *name)
  1036. {
  1037. struct rproc *rproc;
  1038. struct klist_iter i;
  1039. int ret;
  1040. /* find the remote processor, and upref its refcount */
  1041. klist_iter_init(&rprocs, &i);
  1042. while ((rproc = next_rproc(&i)) != NULL)
  1043. if (!strcmp(rproc->name, name)) {
  1044. kref_get(&rproc->refcount);
  1045. break;
  1046. }
  1047. klist_iter_exit(&i);
  1048. /* can't find this rproc ? */
  1049. if (!rproc) {
  1050. pr_err("can't find remote processor %s\n", name);
  1051. return NULL;
  1052. }
  1053. ret = rproc_boot(rproc);
  1054. if (ret < 0) {
  1055. kref_put(&rproc->refcount, rproc_release);
  1056. return NULL;
  1057. }
  1058. return rproc;
  1059. }
  1060. EXPORT_SYMBOL(rproc_get_by_name);
  1061. /**
  1062. * rproc_put() - decrement the refcount of a remote processor, and shut it down
  1063. * @rproc: the remote processor
  1064. *
  1065. * This function tries to shutdown @rproc, and it then decrements its
  1066. * refcount.
  1067. *
  1068. * After this function returns, @rproc may _not_ be used anymore, and its
  1069. * handle should be considered invalid.
  1070. *
  1071. * This function should be called _iff_ the @rproc handle was grabbed by
  1072. * calling rproc_get_by_name().
  1073. */
  1074. void rproc_put(struct rproc *rproc)
  1075. {
  1076. /* try to power off the remote processor */
  1077. rproc_shutdown(rproc);
  1078. /* downref rproc's refcount */
  1079. kref_put(&rproc->refcount, rproc_release);
  1080. }
  1081. EXPORT_SYMBOL(rproc_put);
  1082. /**
  1083. * rproc_register() - register a remote processor
  1084. * @rproc: the remote processor handle to register
  1085. *
  1086. * Registers @rproc with the remoteproc framework, after it has been
  1087. * allocated with rproc_alloc().
  1088. *
  1089. * This is called by the platform-specific rproc implementation, whenever
  1090. * a new remote processor device is probed.
  1091. *
  1092. * Returns 0 on success and an appropriate error code otherwise.
  1093. *
  1094. * Note: this function initiates an asynchronous firmware loading
  1095. * context, which will look for virtio devices supported by the rproc's
  1096. * firmware.
  1097. *
  1098. * If found, those virtio devices will be created and added, so as a result
  1099. * of registering this remote processor, additional virtio drivers will be
  1100. * probed.
  1101. *
  1102. * Currently, though, we only support a single RPMSG virtio vdev per remote
  1103. * processor.
  1104. */
  1105. int rproc_register(struct rproc *rproc)
  1106. {
  1107. struct device *dev = rproc->dev;
  1108. int ret = 0;
  1109. /* expose to rproc_get_by_name users */
  1110. klist_add_tail(&rproc->node, &rprocs);
  1111. dev_info(rproc->dev, "%s is available\n", rproc->name);
  1112. dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
  1113. dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
  1114. /* create debugfs entries */
  1115. rproc_create_debug_dir(rproc);
  1116. /* rproc_unregister() calls must wait until async loader completes */
  1117. init_completion(&rproc->firmware_loading_complete);
  1118. /*
  1119. * We must retrieve early virtio configuration info from
  1120. * the firmware (e.g. whether to register a virtio rpmsg device,
  1121. * what virtio features does it support, ...).
  1122. *
  1123. * We're initiating an asynchronous firmware loading, so we can
  1124. * be built-in kernel code, without hanging the boot process.
  1125. */
  1126. ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
  1127. rproc->firmware, dev, GFP_KERNEL,
  1128. rproc, rproc_fw_config_virtio);
  1129. if (ret < 0) {
  1130. dev_err(dev, "request_firmware_nowait failed: %d\n", ret);
  1131. complete_all(&rproc->firmware_loading_complete);
  1132. klist_remove(&rproc->node);
  1133. }
  1134. return ret;
  1135. }
  1136. EXPORT_SYMBOL(rproc_register);
  1137. /**
  1138. * rproc_alloc() - allocate a remote processor handle
  1139. * @dev: the underlying device
  1140. * @name: name of this remote processor
  1141. * @ops: platform-specific handlers (mainly start/stop)
  1142. * @firmware: name of firmware file to load
  1143. * @len: length of private data needed by the rproc driver (in bytes)
  1144. *
  1145. * Allocates a new remote processor handle, but does not register
  1146. * it yet.
  1147. *
  1148. * This function should be used by rproc implementations during initialization
  1149. * of the remote processor.
  1150. *
  1151. * After creating an rproc handle using this function, and when ready,
  1152. * implementations should then call rproc_register() to complete
  1153. * the registration of the remote processor.
  1154. *
  1155. * On success the new rproc is returned, and on failure, NULL.
  1156. *
  1157. * Note: _never_ directly deallocate @rproc, even if it was not registered
  1158. * yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free().
  1159. */
  1160. struct rproc *rproc_alloc(struct device *dev, const char *name,
  1161. const struct rproc_ops *ops,
  1162. const char *firmware, int len)
  1163. {
  1164. struct rproc *rproc;
  1165. if (!dev || !name || !ops)
  1166. return NULL;
  1167. rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
  1168. if (!rproc) {
  1169. dev_err(dev, "%s: kzalloc failed\n", __func__);
  1170. return NULL;
  1171. }
  1172. rproc->dev = dev;
  1173. rproc->name = name;
  1174. rproc->ops = ops;
  1175. rproc->firmware = firmware;
  1176. rproc->priv = &rproc[1];
  1177. atomic_set(&rproc->power, 0);
  1178. kref_init(&rproc->refcount);
  1179. mutex_init(&rproc->lock);
  1180. INIT_LIST_HEAD(&rproc->carveouts);
  1181. INIT_LIST_HEAD(&rproc->mappings);
  1182. INIT_LIST_HEAD(&rproc->traces);
  1183. rproc->state = RPROC_OFFLINE;
  1184. return rproc;
  1185. }
  1186. EXPORT_SYMBOL(rproc_alloc);
  1187. /**
  1188. * rproc_free() - free an rproc handle that was allocated by rproc_alloc
  1189. * @rproc: the remote processor handle
  1190. *
  1191. * This function should _only_ be used if @rproc was only allocated,
  1192. * but not registered yet.
  1193. *
  1194. * If @rproc was already successfully registered (by calling rproc_register()),
  1195. * then use rproc_unregister() instead.
  1196. */
  1197. void rproc_free(struct rproc *rproc)
  1198. {
  1199. kfree(rproc);
  1200. }
  1201. EXPORT_SYMBOL(rproc_free);
  1202. /**
  1203. * rproc_unregister() - unregister a remote processor
  1204. * @rproc: rproc handle to unregister
  1205. *
  1206. * Unregisters a remote processor, and decrements its refcount.
  1207. * If its refcount drops to zero, then @rproc will be freed. If not,
  1208. * it will be freed later once the last reference is dropped.
  1209. *
  1210. * This function should be called when the platform specific rproc
  1211. * implementation decides to remove the rproc device. it should
  1212. * _only_ be called if a previous invocation of rproc_register()
  1213. * has completed successfully.
  1214. *
  1215. * After rproc_unregister() returns, @rproc is _not_ valid anymore and
  1216. * it shouldn't be used. More specifically, don't call rproc_free()
  1217. * or try to directly free @rproc after rproc_unregister() returns;
  1218. * none of these are needed, and calling them is a bug.
  1219. *
  1220. * Returns 0 on success and -EINVAL if @rproc isn't valid.
  1221. */
  1222. int rproc_unregister(struct rproc *rproc)
  1223. {
  1224. if (!rproc)
  1225. return -EINVAL;
  1226. /* if rproc is just being registered, wait */
  1227. wait_for_completion(&rproc->firmware_loading_complete);
  1228. /* was an rpmsg vdev created ? */
  1229. if (rproc->rvdev)
  1230. rproc_remove_rpmsg_vdev(rproc);
  1231. klist_remove(&rproc->node);
  1232. kref_put(&rproc->refcount, rproc_release);
  1233. return 0;
  1234. }
  1235. EXPORT_SYMBOL(rproc_unregister);
  1236. static int __init remoteproc_init(void)
  1237. {
  1238. rproc_init_debugfs();
  1239. return 0;
  1240. }
  1241. module_init(remoteproc_init);
  1242. static void __exit remoteproc_exit(void)
  1243. {
  1244. rproc_exit_debugfs();
  1245. }
  1246. module_exit(remoteproc_exit);
  1247. MODULE_LICENSE("GPL v2");
  1248. MODULE_DESCRIPTION("Generic Remote Processor Framework");