remoteproc_core.c 37 KB

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