remoteproc_core.c 43 KB

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