remoteproc_core.c 38 KB

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