remoteproc_core.c 40 KB

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