remoteproc_core.c 39 KB

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