remoteproc_core.c 41 KB

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