remoteproc_core.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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 %llx size %x idr %d\n", i, va,
  198. (unsigned long long)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. carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
  488. if (!carveout) {
  489. dev_err(dev, "kzalloc carveout failed\n");
  490. return -ENOMEM;
  491. }
  492. va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
  493. if (!va) {
  494. dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
  495. ret = -ENOMEM;
  496. goto free_carv;
  497. }
  498. dev_dbg(dev, "carveout va %p, dma %llx, len 0x%x\n", va,
  499. (unsigned long long)dma, rsc->len);
  500. /*
  501. * Ok, this is non-standard.
  502. *
  503. * Sometimes we can't rely on the generic iommu-based DMA API
  504. * to dynamically allocate the device address and then set the IOMMU
  505. * tables accordingly, because some remote processors might
  506. * _require_ us to use hard coded device addresses that their
  507. * firmware was compiled with.
  508. *
  509. * In this case, we must use the IOMMU API directly and map
  510. * the memory to the device address as expected by the remote
  511. * processor.
  512. *
  513. * Obviously such remote processor devices should not be configured
  514. * to use the iommu-based DMA API: we expect 'dma' to contain the
  515. * physical address in this case.
  516. */
  517. if (rproc->domain) {
  518. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  519. if (!mapping) {
  520. dev_err(dev, "kzalloc mapping failed\n");
  521. ret = -ENOMEM;
  522. goto dma_free;
  523. }
  524. ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
  525. rsc->flags);
  526. if (ret) {
  527. dev_err(dev, "iommu_map failed: %d\n", ret);
  528. goto free_mapping;
  529. }
  530. /*
  531. * We'll need this info later when we'll want to unmap
  532. * everything (e.g. on shutdown).
  533. *
  534. * We can't trust the remote processor not to change the
  535. * resource table, so we must maintain this info independently.
  536. */
  537. mapping->da = rsc->da;
  538. mapping->len = rsc->len;
  539. list_add_tail(&mapping->node, &rproc->mappings);
  540. dev_dbg(dev, "carveout mapped 0x%x to 0x%llx\n",
  541. rsc->da, (unsigned long long)dma);
  542. }
  543. /*
  544. * Some remote processors might need to know the pa
  545. * even though they are behind an IOMMU. E.g., OMAP4's
  546. * remote M3 processor needs this so it can control
  547. * on-chip hardware accelerators that are not behind
  548. * the IOMMU, and therefor must know the pa.
  549. *
  550. * Generally we don't want to expose physical addresses
  551. * if we don't have to (remote processors are generally
  552. * _not_ trusted), so we might want to do this only for
  553. * remote processor that _must_ have this (e.g. OMAP4's
  554. * dual M3 subsystem).
  555. *
  556. * Non-IOMMU processors might also want to have this info.
  557. * In this case, the device address and the physical address
  558. * are the same.
  559. */
  560. rsc->pa = dma;
  561. carveout->va = va;
  562. carveout->len = rsc->len;
  563. carveout->dma = dma;
  564. carveout->da = rsc->da;
  565. list_add_tail(&carveout->node, &rproc->carveouts);
  566. return 0;
  567. free_mapping:
  568. kfree(mapping);
  569. dma_free:
  570. dma_free_coherent(dev->parent, rsc->len, va, dma);
  571. free_carv:
  572. kfree(carveout);
  573. return ret;
  574. }
  575. /*
  576. * A lookup table for resource handlers. The indices are defined in
  577. * enum fw_resource_type.
  578. */
  579. static rproc_handle_resource_t rproc_handle_rsc[] = {
  580. [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
  581. [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
  582. [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
  583. [RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
  584. };
  585. /* handle firmware resource entries before booting the remote processor */
  586. static int
  587. rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
  588. {
  589. struct device *dev = &rproc->dev;
  590. rproc_handle_resource_t handler;
  591. int ret = 0, i;
  592. for (i = 0; i < table->num; i++) {
  593. int offset = table->offset[i];
  594. struct fw_rsc_hdr *hdr = (void *)table + offset;
  595. int avail = len - offset - sizeof(*hdr);
  596. void *rsc = (void *)hdr + sizeof(*hdr);
  597. /* make sure table isn't truncated */
  598. if (avail < 0) {
  599. dev_err(dev, "rsc table is truncated\n");
  600. return -EINVAL;
  601. }
  602. dev_dbg(dev, "rsc: type %d\n", hdr->type);
  603. if (hdr->type >= RSC_LAST) {
  604. dev_warn(dev, "unsupported resource %d\n", hdr->type);
  605. continue;
  606. }
  607. handler = rproc_handle_rsc[hdr->type];
  608. if (!handler)
  609. continue;
  610. ret = handler(rproc, rsc, avail);
  611. if (ret)
  612. break;
  613. }
  614. return ret;
  615. }
  616. /* handle firmware resource entries while registering the remote processor */
  617. static int
  618. rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
  619. {
  620. struct device *dev = &rproc->dev;
  621. int ret = 0, i;
  622. for (i = 0; i < table->num; i++) {
  623. int offset = table->offset[i];
  624. struct fw_rsc_hdr *hdr = (void *)table + offset;
  625. int avail = len - offset - sizeof(*hdr);
  626. struct fw_rsc_vdev *vrsc;
  627. /* make sure table isn't truncated */
  628. if (avail < 0) {
  629. dev_err(dev, "rsc table is truncated\n");
  630. return -EINVAL;
  631. }
  632. dev_dbg(dev, "%s: rsc type %d\n", __func__, hdr->type);
  633. if (hdr->type != RSC_VDEV)
  634. continue;
  635. vrsc = (struct fw_rsc_vdev *)hdr->data;
  636. ret = rproc_handle_vdev(rproc, vrsc, avail);
  637. if (ret)
  638. break;
  639. }
  640. return ret;
  641. }
  642. /**
  643. * rproc_resource_cleanup() - clean up and free all acquired resources
  644. * @rproc: rproc handle
  645. *
  646. * This function will free all resources acquired for @rproc, and it
  647. * is called whenever @rproc either shuts down or fails to boot.
  648. */
  649. static void rproc_resource_cleanup(struct rproc *rproc)
  650. {
  651. struct rproc_mem_entry *entry, *tmp;
  652. struct device *dev = &rproc->dev;
  653. /* clean up debugfs trace entries */
  654. list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
  655. rproc_remove_trace_file(entry->priv);
  656. rproc->num_traces--;
  657. list_del(&entry->node);
  658. kfree(entry);
  659. }
  660. /* clean up carveout allocations */
  661. list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
  662. dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
  663. list_del(&entry->node);
  664. kfree(entry);
  665. }
  666. /* clean up iommu mapping entries */
  667. list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
  668. size_t unmapped;
  669. unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
  670. if (unmapped != entry->len) {
  671. /* nothing much to do besides complaining */
  672. dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
  673. unmapped);
  674. }
  675. list_del(&entry->node);
  676. kfree(entry);
  677. }
  678. }
  679. /*
  680. * take a firmware and boot a remote processor with it.
  681. */
  682. static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
  683. {
  684. struct device *dev = &rproc->dev;
  685. const char *name = rproc->firmware;
  686. struct resource_table *table;
  687. int ret, tablesz;
  688. ret = rproc_fw_sanity_check(rproc, fw);
  689. if (ret)
  690. return ret;
  691. dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
  692. /*
  693. * if enabling an IOMMU isn't relevant for this rproc, this is
  694. * just a nop
  695. */
  696. ret = rproc_enable_iommu(rproc);
  697. if (ret) {
  698. dev_err(dev, "can't enable iommu: %d\n", ret);
  699. return ret;
  700. }
  701. rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
  702. /* look for the resource table */
  703. table = rproc_find_rsc_table(rproc, fw, &tablesz);
  704. if (!table) {
  705. ret = -EINVAL;
  706. goto clean_up;
  707. }
  708. /* handle fw resources which are required to boot rproc */
  709. ret = rproc_handle_boot_rsc(rproc, table, tablesz);
  710. if (ret) {
  711. dev_err(dev, "Failed to process resources: %d\n", ret);
  712. goto clean_up;
  713. }
  714. /* load the ELF segments to memory */
  715. ret = rproc_load_segments(rproc, fw);
  716. if (ret) {
  717. dev_err(dev, "Failed to load program segments: %d\n", ret);
  718. goto clean_up;
  719. }
  720. /* power up the remote processor */
  721. ret = rproc->ops->start(rproc);
  722. if (ret) {
  723. dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
  724. goto clean_up;
  725. }
  726. rproc->state = RPROC_RUNNING;
  727. dev_info(dev, "remote processor %s is now up\n", rproc->name);
  728. return 0;
  729. clean_up:
  730. rproc_resource_cleanup(rproc);
  731. rproc_disable_iommu(rproc);
  732. return ret;
  733. }
  734. /*
  735. * take a firmware and look for virtio devices to register.
  736. *
  737. * Note: this function is called asynchronously upon registration of the
  738. * remote processor (so we must wait until it completes before we try
  739. * to unregister the device. one other option is just to use kref here,
  740. * that might be cleaner).
  741. */
  742. static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
  743. {
  744. struct rproc *rproc = context;
  745. struct resource_table *table;
  746. int ret, tablesz;
  747. if (rproc_fw_sanity_check(rproc, fw) < 0)
  748. goto out;
  749. /* look for the resource table */
  750. table = rproc_find_rsc_table(rproc, fw, &tablesz);
  751. if (!table)
  752. goto out;
  753. /* look for virtio devices and register them */
  754. ret = rproc_handle_virtio_rsc(rproc, table, tablesz);
  755. if (ret)
  756. goto out;
  757. out:
  758. release_firmware(fw);
  759. /* allow rproc_del() contexts, if any, to proceed */
  760. complete_all(&rproc->firmware_loading_complete);
  761. }
  762. static int rproc_add_virtio_devices(struct rproc *rproc)
  763. {
  764. int ret;
  765. /* rproc_del() calls must wait until async loader completes */
  766. init_completion(&rproc->firmware_loading_complete);
  767. /*
  768. * We must retrieve early virtio configuration info from
  769. * the firmware (e.g. whether to register a virtio device,
  770. * what virtio features does it support, ...).
  771. *
  772. * We're initiating an asynchronous firmware loading, so we can
  773. * be built-in kernel code, without hanging the boot process.
  774. */
  775. ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
  776. rproc->firmware, &rproc->dev, GFP_KERNEL,
  777. rproc, rproc_fw_config_virtio);
  778. if (ret < 0) {
  779. dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
  780. complete_all(&rproc->firmware_loading_complete);
  781. }
  782. return ret;
  783. }
  784. /**
  785. * rproc_trigger_recovery() - recover a remoteproc
  786. * @rproc: the remote processor
  787. *
  788. * The recovery is done by reseting all the virtio devices, that way all the
  789. * rpmsg drivers will be reseted along with the remote processor making the
  790. * remoteproc functional again.
  791. *
  792. * This function can sleep, so it cannot be called from atomic context.
  793. */
  794. int rproc_trigger_recovery(struct rproc *rproc)
  795. {
  796. struct rproc_vdev *rvdev, *rvtmp;
  797. dev_err(&rproc->dev, "recovering %s\n", rproc->name);
  798. init_completion(&rproc->crash_comp);
  799. /* clean up remote vdev entries */
  800. list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
  801. rproc_remove_virtio_dev(rvdev);
  802. /* wait until there is no more rproc users */
  803. wait_for_completion(&rproc->crash_comp);
  804. return rproc_add_virtio_devices(rproc);
  805. }
  806. /**
  807. * rproc_crash_handler_work() - handle a crash
  808. *
  809. * This function needs to handle everything related to a crash, like cpu
  810. * registers and stack dump, information to help to debug the fatal error, etc.
  811. */
  812. static void rproc_crash_handler_work(struct work_struct *work)
  813. {
  814. struct rproc *rproc = container_of(work, struct rproc, crash_handler);
  815. struct device *dev = &rproc->dev;
  816. dev_dbg(dev, "enter %s\n", __func__);
  817. mutex_lock(&rproc->lock);
  818. if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
  819. /* handle only the first crash detected */
  820. mutex_unlock(&rproc->lock);
  821. return;
  822. }
  823. rproc->state = RPROC_CRASHED;
  824. dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
  825. rproc->name);
  826. mutex_unlock(&rproc->lock);
  827. if (!rproc->recovery_disabled)
  828. rproc_trigger_recovery(rproc);
  829. }
  830. /**
  831. * rproc_boot() - boot a remote processor
  832. * @rproc: handle of a remote processor
  833. *
  834. * Boot a remote processor (i.e. load its firmware, power it on, ...).
  835. *
  836. * If the remote processor is already powered on, this function immediately
  837. * returns (successfully).
  838. *
  839. * Returns 0 on success, and an appropriate error value otherwise.
  840. */
  841. int rproc_boot(struct rproc *rproc)
  842. {
  843. const struct firmware *firmware_p;
  844. struct device *dev;
  845. int ret;
  846. if (!rproc) {
  847. pr_err("invalid rproc handle\n");
  848. return -EINVAL;
  849. }
  850. dev = &rproc->dev;
  851. ret = mutex_lock_interruptible(&rproc->lock);
  852. if (ret) {
  853. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  854. return ret;
  855. }
  856. /* loading a firmware is required */
  857. if (!rproc->firmware) {
  858. dev_err(dev, "%s: no firmware to load\n", __func__);
  859. ret = -EINVAL;
  860. goto unlock_mutex;
  861. }
  862. /* prevent underlying implementation from being removed */
  863. if (!try_module_get(dev->parent->driver->owner)) {
  864. dev_err(dev, "%s: can't get owner\n", __func__);
  865. ret = -EINVAL;
  866. goto unlock_mutex;
  867. }
  868. /* skip the boot process if rproc is already powered up */
  869. if (atomic_inc_return(&rproc->power) > 1) {
  870. ret = 0;
  871. goto unlock_mutex;
  872. }
  873. dev_info(dev, "powering up %s\n", rproc->name);
  874. /* load firmware */
  875. ret = request_firmware(&firmware_p, rproc->firmware, dev);
  876. if (ret < 0) {
  877. dev_err(dev, "request_firmware failed: %d\n", ret);
  878. goto downref_rproc;
  879. }
  880. ret = rproc_fw_boot(rproc, firmware_p);
  881. release_firmware(firmware_p);
  882. downref_rproc:
  883. if (ret) {
  884. module_put(dev->parent->driver->owner);
  885. atomic_dec(&rproc->power);
  886. }
  887. unlock_mutex:
  888. mutex_unlock(&rproc->lock);
  889. return ret;
  890. }
  891. EXPORT_SYMBOL(rproc_boot);
  892. /**
  893. * rproc_shutdown() - power off the remote processor
  894. * @rproc: the remote processor
  895. *
  896. * Power off a remote processor (previously booted with rproc_boot()).
  897. *
  898. * In case @rproc is still being used by an additional user(s), then
  899. * this function will just decrement the power refcount and exit,
  900. * without really powering off the device.
  901. *
  902. * Every call to rproc_boot() must (eventually) be accompanied by a call
  903. * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
  904. *
  905. * Notes:
  906. * - we're not decrementing the rproc's refcount, only the power refcount.
  907. * which means that the @rproc handle stays valid even after rproc_shutdown()
  908. * returns, and users can still use it with a subsequent rproc_boot(), if
  909. * needed.
  910. */
  911. void rproc_shutdown(struct rproc *rproc)
  912. {
  913. struct device *dev = &rproc->dev;
  914. int ret;
  915. ret = mutex_lock_interruptible(&rproc->lock);
  916. if (ret) {
  917. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  918. return;
  919. }
  920. /* if the remote proc is still needed, bail out */
  921. if (!atomic_dec_and_test(&rproc->power))
  922. goto out;
  923. /* power off the remote processor */
  924. ret = rproc->ops->stop(rproc);
  925. if (ret) {
  926. atomic_inc(&rproc->power);
  927. dev_err(dev, "can't stop rproc: %d\n", ret);
  928. goto out;
  929. }
  930. /* clean up all acquired resources */
  931. rproc_resource_cleanup(rproc);
  932. rproc_disable_iommu(rproc);
  933. /* if in crash state, unlock crash handler */
  934. if (rproc->state == RPROC_CRASHED)
  935. complete_all(&rproc->crash_comp);
  936. rproc->state = RPROC_OFFLINE;
  937. dev_info(dev, "stopped remote processor %s\n", rproc->name);
  938. out:
  939. mutex_unlock(&rproc->lock);
  940. if (!ret)
  941. module_put(dev->parent->driver->owner);
  942. }
  943. EXPORT_SYMBOL(rproc_shutdown);
  944. /**
  945. * rproc_add() - register a remote processor
  946. * @rproc: the remote processor handle to register
  947. *
  948. * Registers @rproc with the remoteproc framework, after it has been
  949. * allocated with rproc_alloc().
  950. *
  951. * This is called by the platform-specific rproc implementation, whenever
  952. * a new remote processor device is probed.
  953. *
  954. * Returns 0 on success and an appropriate error code otherwise.
  955. *
  956. * Note: this function initiates an asynchronous firmware loading
  957. * context, which will look for virtio devices supported by the rproc's
  958. * firmware.
  959. *
  960. * If found, those virtio devices will be created and added, so as a result
  961. * of registering this remote processor, additional virtio drivers might be
  962. * probed.
  963. */
  964. int rproc_add(struct rproc *rproc)
  965. {
  966. struct device *dev = &rproc->dev;
  967. int ret;
  968. ret = device_add(dev);
  969. if (ret < 0)
  970. return ret;
  971. dev_info(dev, "%s is available\n", rproc->name);
  972. dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
  973. dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
  974. /* create debugfs entries */
  975. rproc_create_debug_dir(rproc);
  976. return rproc_add_virtio_devices(rproc);
  977. }
  978. EXPORT_SYMBOL(rproc_add);
  979. /**
  980. * rproc_type_release() - release a remote processor instance
  981. * @dev: the rproc's device
  982. *
  983. * This function should _never_ be called directly.
  984. *
  985. * It will be called by the driver core when no one holds a valid pointer
  986. * to @dev anymore.
  987. */
  988. static void rproc_type_release(struct device *dev)
  989. {
  990. struct rproc *rproc = container_of(dev, struct rproc, dev);
  991. dev_info(&rproc->dev, "releasing %s\n", rproc->name);
  992. rproc_delete_debug_dir(rproc);
  993. idr_remove_all(&rproc->notifyids);
  994. idr_destroy(&rproc->notifyids);
  995. if (rproc->index >= 0)
  996. ida_simple_remove(&rproc_dev_index, rproc->index);
  997. kfree(rproc);
  998. }
  999. static struct device_type rproc_type = {
  1000. .name = "remoteproc",
  1001. .release = rproc_type_release,
  1002. };
  1003. /**
  1004. * rproc_alloc() - allocate a remote processor handle
  1005. * @dev: the underlying device
  1006. * @name: name of this remote processor
  1007. * @ops: platform-specific handlers (mainly start/stop)
  1008. * @firmware: name of firmware file to load
  1009. * @len: length of private data needed by the rproc driver (in bytes)
  1010. *
  1011. * Allocates a new remote processor handle, but does not register
  1012. * it yet.
  1013. *
  1014. * This function should be used by rproc implementations during initialization
  1015. * of the remote processor.
  1016. *
  1017. * After creating an rproc handle using this function, and when ready,
  1018. * implementations should then call rproc_add() to complete
  1019. * the registration of the remote processor.
  1020. *
  1021. * On success the new rproc is returned, and on failure, NULL.
  1022. *
  1023. * Note: _never_ directly deallocate @rproc, even if it was not registered
  1024. * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
  1025. */
  1026. struct rproc *rproc_alloc(struct device *dev, const char *name,
  1027. const struct rproc_ops *ops,
  1028. const char *firmware, int len)
  1029. {
  1030. struct rproc *rproc;
  1031. if (!dev || !name || !ops)
  1032. return NULL;
  1033. rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
  1034. if (!rproc) {
  1035. dev_err(dev, "%s: kzalloc failed\n", __func__);
  1036. return NULL;
  1037. }
  1038. rproc->name = name;
  1039. rproc->ops = ops;
  1040. rproc->firmware = firmware;
  1041. rproc->priv = &rproc[1];
  1042. device_initialize(&rproc->dev);
  1043. rproc->dev.parent = dev;
  1044. rproc->dev.type = &rproc_type;
  1045. /* Assign a unique device index and name */
  1046. rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
  1047. if (rproc->index < 0) {
  1048. dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
  1049. put_device(&rproc->dev);
  1050. return NULL;
  1051. }
  1052. dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
  1053. atomic_set(&rproc->power, 0);
  1054. /* Set ELF as the default fw_ops handler */
  1055. rproc->fw_ops = &rproc_elf_fw_ops;
  1056. mutex_init(&rproc->lock);
  1057. idr_init(&rproc->notifyids);
  1058. INIT_LIST_HEAD(&rproc->carveouts);
  1059. INIT_LIST_HEAD(&rproc->mappings);
  1060. INIT_LIST_HEAD(&rproc->traces);
  1061. INIT_LIST_HEAD(&rproc->rvdevs);
  1062. INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
  1063. init_completion(&rproc->crash_comp);
  1064. rproc->state = RPROC_OFFLINE;
  1065. return rproc;
  1066. }
  1067. EXPORT_SYMBOL(rproc_alloc);
  1068. /**
  1069. * rproc_put() - unroll rproc_alloc()
  1070. * @rproc: the remote processor handle
  1071. *
  1072. * This function decrements the rproc dev refcount.
  1073. *
  1074. * If no one holds any reference to rproc anymore, then its refcount would
  1075. * now drop to zero, and it would be freed.
  1076. */
  1077. void rproc_put(struct rproc *rproc)
  1078. {
  1079. put_device(&rproc->dev);
  1080. }
  1081. EXPORT_SYMBOL(rproc_put);
  1082. /**
  1083. * rproc_del() - unregister a remote processor
  1084. * @rproc: rproc handle to unregister
  1085. *
  1086. * This function should be called when the platform specific rproc
  1087. * implementation decides to remove the rproc device. it should
  1088. * _only_ be called if a previous invocation of rproc_add()
  1089. * has completed successfully.
  1090. *
  1091. * After rproc_del() returns, @rproc isn't freed yet, because
  1092. * of the outstanding reference created by rproc_alloc. To decrement that
  1093. * one last refcount, one still needs to call rproc_put().
  1094. *
  1095. * Returns 0 on success and -EINVAL if @rproc isn't valid.
  1096. */
  1097. int rproc_del(struct rproc *rproc)
  1098. {
  1099. struct rproc_vdev *rvdev, *tmp;
  1100. if (!rproc)
  1101. return -EINVAL;
  1102. /* if rproc is just being registered, wait */
  1103. wait_for_completion(&rproc->firmware_loading_complete);
  1104. /* clean up remote vdev entries */
  1105. list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
  1106. rproc_remove_virtio_dev(rvdev);
  1107. device_del(&rproc->dev);
  1108. return 0;
  1109. }
  1110. EXPORT_SYMBOL(rproc_del);
  1111. /**
  1112. * rproc_report_crash() - rproc crash reporter function
  1113. * @rproc: remote processor
  1114. * @type: crash type
  1115. *
  1116. * This function must be called every time a crash is detected by the low-level
  1117. * drivers implementing a specific remoteproc. This should not be called from a
  1118. * non-remoteproc driver.
  1119. *
  1120. * This function can be called from atomic/interrupt context.
  1121. */
  1122. void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
  1123. {
  1124. if (!rproc) {
  1125. pr_err("NULL rproc pointer\n");
  1126. return;
  1127. }
  1128. dev_err(&rproc->dev, "crash detected in %s: type %s\n",
  1129. rproc->name, rproc_crash_to_string(type));
  1130. /* create a new task to handle the error */
  1131. schedule_work(&rproc->crash_handler);
  1132. }
  1133. EXPORT_SYMBOL(rproc_report_crash);
  1134. static int __init remoteproc_init(void)
  1135. {
  1136. rproc_init_debugfs();
  1137. return 0;
  1138. }
  1139. module_init(remoteproc_init);
  1140. static void __exit remoteproc_exit(void)
  1141. {
  1142. rproc_exit_debugfs();
  1143. }
  1144. module_exit(remoteproc_exit);
  1145. MODULE_LICENSE("GPL v2");
  1146. MODULE_DESCRIPTION("Generic Remote Processor Framework");