remoteproc_core.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  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/devcoredump.h>
  35. #include <linux/remoteproc.h>
  36. #include <linux/iommu.h>
  37. #include <linux/idr.h>
  38. #include <linux/elf.h>
  39. #include <linux/crc32.h>
  40. #include <linux/virtio_ids.h>
  41. #include <linux/virtio_ring.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/of.h>
  44. #include <linux/platform_device.h>
  45. #include <asm/byteorder.h>
  46. #include "remoteproc_internal.h"
  47. static DEFINE_MUTEX(rproc_list_mutex);
  48. static LIST_HEAD(rproc_list);
  49. typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
  50. struct resource_table *table, int len);
  51. typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
  52. void *, int offset, int avail);
  53. /* Unique indices for remoteproc devices */
  54. static DEFINE_IDA(rproc_dev_index);
  55. static const char * const rproc_crash_names[] = {
  56. [RPROC_MMUFAULT] = "mmufault",
  57. [RPROC_WATCHDOG] = "watchdog",
  58. [RPROC_FATAL_ERROR] = "fatal error",
  59. };
  60. /* translate rproc_crash_type to string */
  61. static const char *rproc_crash_to_string(enum rproc_crash_type type)
  62. {
  63. if (type < ARRAY_SIZE(rproc_crash_names))
  64. return rproc_crash_names[type];
  65. return "unknown";
  66. }
  67. /*
  68. * This is the IOMMU fault handler we register with the IOMMU API
  69. * (when relevant; not all remote processors access memory through
  70. * an IOMMU).
  71. *
  72. * IOMMU core will invoke this handler whenever the remote processor
  73. * will try to access an unmapped device address.
  74. */
  75. static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
  76. unsigned long iova, int flags, void *token)
  77. {
  78. struct rproc *rproc = token;
  79. dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
  80. rproc_report_crash(rproc, RPROC_MMUFAULT);
  81. /*
  82. * Let the iommu core know we're not really handling this fault;
  83. * we just used it as a recovery trigger.
  84. */
  85. return -ENOSYS;
  86. }
  87. static int rproc_enable_iommu(struct rproc *rproc)
  88. {
  89. struct iommu_domain *domain;
  90. struct device *dev = rproc->dev.parent;
  91. int ret;
  92. if (!rproc->has_iommu) {
  93. dev_dbg(dev, "iommu not present\n");
  94. return 0;
  95. }
  96. domain = iommu_domain_alloc(dev->bus);
  97. if (!domain) {
  98. dev_err(dev, "can't alloc iommu domain\n");
  99. return -ENOMEM;
  100. }
  101. iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
  102. ret = iommu_attach_device(domain, dev);
  103. if (ret) {
  104. dev_err(dev, "can't attach iommu device: %d\n", ret);
  105. goto free_domain;
  106. }
  107. rproc->domain = domain;
  108. return 0;
  109. free_domain:
  110. iommu_domain_free(domain);
  111. return ret;
  112. }
  113. static void rproc_disable_iommu(struct rproc *rproc)
  114. {
  115. struct iommu_domain *domain = rproc->domain;
  116. struct device *dev = rproc->dev.parent;
  117. if (!domain)
  118. return;
  119. iommu_detach_device(domain, dev);
  120. iommu_domain_free(domain);
  121. }
  122. /**
  123. * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
  124. * @rproc: handle of a remote processor
  125. * @da: remoteproc device address to translate
  126. * @len: length of the memory region @da is pointing to
  127. * @flags: flags to pass onto platform implementations for aiding translations
  128. *
  129. * Some remote processors will ask us to allocate them physically contiguous
  130. * memory regions (which we call "carveouts"), and map them to specific
  131. * device addresses (which are hardcoded in the firmware). They may also have
  132. * dedicated memory regions internal to the processors, and use them either
  133. * exclusively or alongside carveouts.
  134. *
  135. * They may then ask us to copy objects into specific device addresses (e.g.
  136. * code/data sections) or expose us certain symbols in other device address
  137. * (e.g. their trace buffer).
  138. *
  139. * This function is a helper function with which we can go over the allocated
  140. * carveouts and translate specific device addresses to kernel virtual addresses
  141. * so we can access the referenced memory. This function also allows to perform
  142. * translations on the internal remoteproc memory regions through a platform
  143. * implementation specific da_to_va ops, if present. The @flags field is passed
  144. * onto these ops to aid the translation within the ops implementation. The
  145. * @flags field is to be passed as a combination of the RPROC_FLAGS_xxx type
  146. * and the pertinent flags value for that type.
  147. *
  148. * The function returns a valid kernel address on success or NULL on failure.
  149. *
  150. * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
  151. * but only on kernel direct mapped RAM memory. Instead, we're just using
  152. * here the output of the DMA API for the carveouts, which should be more
  153. * correct.
  154. */
  155. void *rproc_da_to_va(struct rproc *rproc, u64 da, int len, u32 flags)
  156. {
  157. struct rproc_mem_entry *carveout;
  158. void *ptr = NULL;
  159. if (rproc->ops->da_to_va) {
  160. ptr = rproc->ops->da_to_va(rproc, da, len, flags);
  161. if (ptr)
  162. goto out;
  163. }
  164. list_for_each_entry(carveout, &rproc->carveouts, node) {
  165. int offset = da - carveout->da;
  166. /* try next carveout if da is too small */
  167. if (offset < 0)
  168. continue;
  169. /* try next carveout if da is too large */
  170. if (offset + len > carveout->len)
  171. continue;
  172. ptr = carveout->va + offset;
  173. break;
  174. }
  175. out:
  176. return ptr;
  177. }
  178. EXPORT_SYMBOL(rproc_da_to_va);
  179. /**
  180. * rproc_pa_to_da() - lookup the rproc device address for a physical address
  181. * @rproc: handle of a remote processor
  182. * @pa: physical address of the buffer to translate
  183. * @da: device address to return
  184. *
  185. * Communication clients of remote processors usually would need a means to
  186. * convert a host buffer pointer to an equivalent device virtual address pointer
  187. * that the code running on the remote processor can operate on. These buffer
  188. * pointers can either be from the physically contiguous memory regions (or
  189. * "carveouts") or can be some memory-mapped Device IO memory. This function
  190. * provides a means to translate a given physical address to its associated
  191. * device address.
  192. *
  193. * The function looks through both the carveouts and the device memory mappings
  194. * since both of them are stored in separate lists.
  195. *
  196. * Returns 0 on success, or an appropriate error code otherwise. The translated
  197. * device address is returned through the appropriate function argument.
  198. */
  199. int rproc_pa_to_da(struct rproc *rproc, phys_addr_t pa, u64 *da)
  200. {
  201. int ret = -EINVAL;
  202. struct rproc_mem_entry *maps = NULL;
  203. if (!rproc || !da)
  204. return -EINVAL;
  205. if (mutex_lock_interruptible(&rproc->lock))
  206. return -EINTR;
  207. if (rproc->state == RPROC_RUNNING || rproc->state == RPROC_SUSPENDED) {
  208. /* Look in the mappings first */
  209. list_for_each_entry(maps, &rproc->mappings, node) {
  210. if (pa >= maps->dma && pa < (maps->dma + maps->len)) {
  211. *da = maps->da + (pa - maps->dma);
  212. ret = 0;
  213. goto exit;
  214. }
  215. }
  216. /* If not, check in the carveouts */
  217. list_for_each_entry(maps, &rproc->carveouts, node) {
  218. if (pa >= maps->dma && pa < (maps->dma + maps->len)) {
  219. *da = maps->da + (pa - maps->dma);
  220. ret = 0;
  221. break;
  222. }
  223. }
  224. }
  225. exit:
  226. mutex_unlock(&rproc->lock);
  227. return ret;
  228. }
  229. EXPORT_SYMBOL(rproc_pa_to_da);
  230. int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
  231. {
  232. struct rproc *rproc = rvdev->rproc;
  233. struct device *dev = &rproc->dev;
  234. struct rproc_vring *rvring = &rvdev->vring[i];
  235. struct fw_rsc_vdev *rsc;
  236. dma_addr_t dma;
  237. void *va;
  238. int ret, size, notifyid;
  239. /* actual size of vring (in bytes) */
  240. size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
  241. /*
  242. * Allocate non-cacheable memory for the vring. In the future
  243. * this call will also configure the IOMMU for us
  244. */
  245. va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
  246. if (!va) {
  247. dev_err(dev->parent, "dma_alloc_coherent failed\n");
  248. return -EINVAL;
  249. }
  250. /*
  251. * Assign an rproc-wide unique index for this vring
  252. * TODO: assign a notifyid for rvdev updates as well
  253. * TODO: support predefined notifyids (via resource table)
  254. */
  255. ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
  256. if (ret < 0) {
  257. dev_err(dev, "idr_alloc failed: %d\n", ret);
  258. dma_free_coherent(dev->parent, size, va, dma);
  259. return ret;
  260. }
  261. notifyid = ret;
  262. /* Potentially bump max_notifyid */
  263. if (notifyid > rproc->max_notifyid)
  264. rproc->max_notifyid = notifyid;
  265. dev_dbg(dev, "vring%d: va %pK dma %pad size 0x%x idr %d\n",
  266. i, va, &dma, size, notifyid);
  267. rvring->va = va;
  268. rvring->dma = dma;
  269. rvring->notifyid = notifyid;
  270. /*
  271. * Let the rproc know the notifyid and da of this vring.
  272. * Not all platforms use dma_alloc_coherent to automatically
  273. * set up the iommu. In this case the device address (da) will
  274. * hold the physical address and not the device address.
  275. */
  276. rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
  277. rsc->vring[i].da = dma;
  278. rsc->vring[i].notifyid = notifyid;
  279. return 0;
  280. }
  281. static int
  282. rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
  283. {
  284. struct rproc *rproc = rvdev->rproc;
  285. struct device *dev = &rproc->dev;
  286. struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
  287. struct rproc_vring *rvring = &rvdev->vring[i];
  288. dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
  289. i, vring->da, vring->num, vring->align);
  290. /* verify queue size and vring alignment are sane */
  291. if (!vring->num || !vring->align) {
  292. dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
  293. vring->num, vring->align);
  294. return -EINVAL;
  295. }
  296. rvring->len = vring->num;
  297. rvring->align = vring->align;
  298. rvring->rvdev = rvdev;
  299. return 0;
  300. }
  301. void rproc_free_vring(struct rproc_vring *rvring)
  302. {
  303. int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
  304. struct rproc *rproc = rvring->rvdev->rproc;
  305. int idx = rvring->rvdev->vring - rvring;
  306. struct fw_rsc_vdev *rsc;
  307. dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
  308. idr_remove(&rproc->notifyids, rvring->notifyid);
  309. /* reset resource entry info */
  310. rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
  311. rsc->vring[idx].da = 0;
  312. rsc->vring[idx].notifyid = -1;
  313. }
  314. static int rproc_vdev_do_start(struct rproc_subdev *subdev)
  315. {
  316. struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
  317. return rproc_add_virtio_dev(rvdev, rvdev->id);
  318. }
  319. static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
  320. {
  321. struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
  322. rproc_remove_virtio_dev(rvdev);
  323. }
  324. /**
  325. * rproc_handle_vdev() - handle a vdev fw resource
  326. * @rproc: the remote processor
  327. * @rsc: the vring resource descriptor
  328. * @avail: size of available data (for sanity checking the image)
  329. *
  330. * This resource entry requests the host to statically register a virtio
  331. * device (vdev), and setup everything needed to support it. It contains
  332. * everything needed to make it possible: the virtio device id, virtio
  333. * device features, vrings information, virtio config space, etc...
  334. *
  335. * Before registering the vdev, the vrings are allocated from non-cacheable
  336. * physically contiguous memory. Currently we only support two vrings per
  337. * remote processor (temporary limitation). We might also want to consider
  338. * doing the vring allocation only later when ->find_vqs() is invoked, and
  339. * then release them upon ->del_vqs().
  340. *
  341. * Note: @da is currently not really handled correctly: we dynamically
  342. * allocate it using the DMA API, ignoring requested hard coded addresses,
  343. * and we don't take care of any required IOMMU programming. This is all
  344. * going to be taken care of when the generic iommu-based DMA API will be
  345. * merged. Meanwhile, statically-addressed iommu-based firmware images should
  346. * use RSC_DEVMEM resource entries to map their required @da to the physical
  347. * address of their base CMA region (ouch, hacky!).
  348. *
  349. * Returns 0 on success, or an appropriate error code otherwise
  350. */
  351. static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
  352. int offset, int avail)
  353. {
  354. struct device *dev = &rproc->dev;
  355. struct rproc_vdev *rvdev;
  356. int i, ret;
  357. /* make sure resource isn't truncated */
  358. if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
  359. + rsc->config_len > avail) {
  360. dev_err(dev, "vdev rsc is truncated\n");
  361. return -EINVAL;
  362. }
  363. /* make sure reserved bytes are zeroes */
  364. if (rsc->reserved[0] || rsc->reserved[1]) {
  365. dev_err(dev, "vdev rsc has non zero reserved bytes\n");
  366. return -EINVAL;
  367. }
  368. dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
  369. rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
  370. /* we currently support only two vrings per rvdev */
  371. if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
  372. dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
  373. return -EINVAL;
  374. }
  375. rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
  376. if (!rvdev)
  377. return -ENOMEM;
  378. kref_init(&rvdev->refcount);
  379. rvdev->id = rsc->id;
  380. rvdev->rproc = rproc;
  381. /* parse the vrings */
  382. for (i = 0; i < rsc->num_of_vrings; i++) {
  383. ret = rproc_parse_vring(rvdev, rsc, i);
  384. if (ret)
  385. goto free_rvdev;
  386. }
  387. /* remember the resource offset*/
  388. rvdev->rsc_offset = offset;
  389. /* allocate the vring resources */
  390. for (i = 0; i < rsc->num_of_vrings; i++) {
  391. ret = rproc_alloc_vring(rvdev, i);
  392. if (ret)
  393. goto unwind_vring_allocations;
  394. }
  395. list_add_tail(&rvdev->node, &rproc->rvdevs);
  396. rvdev->subdev.start = rproc_vdev_do_start;
  397. rvdev->subdev.stop = rproc_vdev_do_stop;
  398. rproc_add_subdev(rproc, &rvdev->subdev);
  399. return 0;
  400. unwind_vring_allocations:
  401. for (i--; i >= 0; i--)
  402. rproc_free_vring(&rvdev->vring[i]);
  403. free_rvdev:
  404. kfree(rvdev);
  405. return ret;
  406. }
  407. void rproc_vdev_release(struct kref *ref)
  408. {
  409. struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
  410. struct rproc_vring *rvring;
  411. struct rproc *rproc = rvdev->rproc;
  412. int id;
  413. for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
  414. rvring = &rvdev->vring[id];
  415. if (!rvring->va)
  416. continue;
  417. rproc_free_vring(rvring);
  418. }
  419. rproc_remove_subdev(rproc, &rvdev->subdev);
  420. list_del(&rvdev->node);
  421. kfree(rvdev);
  422. }
  423. /**
  424. * rproc_handle_last_trace() - setup a buffer to capture the trace snapshot
  425. * before recovery
  426. * @rproc: the remote processor
  427. * @trace: the trace resource descriptor
  428. * @count: the index of the trace under process
  429. *
  430. * The last trace is allocated and the contents of the trace buffer are
  431. * copied during a recovery cleanup. Once, the contents get copied, the
  432. * trace buffers are cleaned up for re-use.
  433. *
  434. * It might also happen that the remoteproc binary changes between the
  435. * time that it was loaded and the time that it crashed. In this case,
  436. * the trace descriptors might have changed too. The last traces are
  437. * re-built as required in this case.
  438. *
  439. * Returns 0 on success, or an appropriate error code otherwise
  440. */
  441. static int rproc_handle_last_trace(struct rproc *rproc,
  442. struct rproc_mem_entry *trace, int count)
  443. {
  444. struct rproc_mem_entry *trace_last, *tmp_trace;
  445. struct device *dev = &rproc->dev;
  446. char name[15];
  447. int i = 0;
  448. bool new_trace = false;
  449. if (!rproc || !trace)
  450. return -EINVAL;
  451. /* we need a new trace in this case */
  452. if (count > rproc->num_last_traces) {
  453. new_trace = true;
  454. /*
  455. * make sure snprintf always null terminates, even if truncating
  456. */
  457. snprintf(name, sizeof(name), "trace%d_last", (count - 1));
  458. trace_last = kzalloc(sizeof(*trace_last), GFP_KERNEL);
  459. if (!trace_last) {
  460. dev_err(dev, "kzalloc failed for trace%d_last\n",
  461. count);
  462. return -ENOMEM;
  463. }
  464. } else {
  465. /* try to reuse buffers here */
  466. list_for_each_entry_safe(trace_last, tmp_trace,
  467. &rproc->last_traces, node) {
  468. if (++i == count)
  469. break;
  470. }
  471. /* if we can reuse the trace, copy buffer and exit */
  472. if (trace_last->len == trace->len)
  473. goto copy_and_exit;
  474. /* can reuse the trace struct but not the buffer */
  475. vfree(trace_last->va);
  476. trace_last->va = NULL;
  477. trace_last->len = 0;
  478. }
  479. trace_last->len = trace->len;
  480. trace_last->va = vmalloc(sizeof(u32) * trace_last->len);
  481. if (!trace_last->va) {
  482. dev_err(dev, "vmalloc failed for trace%d_last\n", count);
  483. if (!new_trace) {
  484. list_del(&trace_last->node);
  485. rproc->num_last_traces--;
  486. }
  487. kfree(trace_last);
  488. return -ENOMEM;
  489. }
  490. /* create the debugfs entry */
  491. if (new_trace) {
  492. trace_last->priv = rproc_create_trace_file(name, rproc,
  493. trace_last);
  494. if (!trace_last->priv) {
  495. dev_err(dev, "trace%d_last create debugfs failed\n",
  496. count);
  497. vfree(trace_last->va);
  498. kfree(trace_last);
  499. return -EINVAL;
  500. }
  501. /* add it to the trace list */
  502. list_add_tail(&trace_last->node, &rproc->last_traces);
  503. rproc->num_last_traces++;
  504. }
  505. copy_and_exit:
  506. /* copy the trace to last trace */
  507. memcpy(trace_last->va, trace->va, trace->len);
  508. return 0;
  509. }
  510. /**
  511. * rproc_handle_trace() - handle a shared trace buffer resource
  512. * @rproc: the remote processor
  513. * @rsc: the trace resource descriptor
  514. * @avail: size of available data (for sanity checking the image)
  515. *
  516. * In case the remote processor dumps trace logs into memory,
  517. * export it via debugfs.
  518. *
  519. * Currently, the 'da' member of @rsc should contain the device address
  520. * where the remote processor is dumping the traces. Later we could also
  521. * support dynamically allocating this address using the generic
  522. * DMA API (but currently there isn't a use case for that).
  523. *
  524. * Returns 0 on success, or an appropriate error code otherwise
  525. */
  526. static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
  527. int offset, int avail)
  528. {
  529. struct rproc_mem_entry *trace;
  530. struct device *dev = &rproc->dev;
  531. void *ptr;
  532. char name[15];
  533. if (sizeof(*rsc) > avail) {
  534. dev_err(dev, "trace rsc is truncated\n");
  535. return -EINVAL;
  536. }
  537. /* make sure reserved bytes are zeroes */
  538. if (rsc->reserved) {
  539. dev_err(dev, "trace rsc has non zero reserved bytes\n");
  540. return -EINVAL;
  541. }
  542. /* what's the kernel address of this resource ? */
  543. ptr = rproc_da_to_va(rproc, rsc->da, rsc->len, RPROC_FLAGS_NONE);
  544. if (!ptr) {
  545. dev_err(dev, "erroneous trace resource entry\n");
  546. return -EINVAL;
  547. }
  548. trace = kzalloc(sizeof(*trace), GFP_KERNEL);
  549. if (!trace)
  550. return -ENOMEM;
  551. /* set the trace buffer dma properties */
  552. trace->len = rsc->len;
  553. trace->va = ptr;
  554. /* make sure snprintf always null terminates, even if truncating */
  555. snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
  556. /* create the debugfs entry */
  557. trace->priv = rproc_create_trace_file(name, rproc, trace);
  558. if (!trace->priv) {
  559. trace->va = NULL;
  560. kfree(trace);
  561. return -EINVAL;
  562. }
  563. list_add_tail(&trace->node, &rproc->traces);
  564. rproc->num_traces++;
  565. dev_dbg(dev, "%s added: va %pK, da 0x%x, len 0x%x\n",
  566. name, ptr, rsc->da, rsc->len);
  567. return 0;
  568. }
  569. /**
  570. * rproc_handle_devmem() - handle devmem resource entry
  571. * @rproc: remote processor handle
  572. * @rsc: the devmem resource entry
  573. * @avail: size of available data (for sanity checking the image)
  574. *
  575. * Remote processors commonly need to access certain on-chip peripherals.
  576. *
  577. * Some of these remote processors access memory via an iommu device,
  578. * and might require us to configure their iommu before they can access
  579. * the on-chip peripherals they need.
  580. *
  581. * This resource entry is a request to map such a peripheral device.
  582. *
  583. * These devmem entries will contain the physical address of the device in
  584. * the 'pa' member. If a specific device address is expected, then 'da' will
  585. * contain it (currently this is the only use case supported). 'len' will
  586. * contain the size of the physical region we need to map.
  587. *
  588. * Currently we just "trust" those devmem entries to contain valid physical
  589. * addresses, but this is going to change: we want the implementations to
  590. * tell us ranges of physical addresses the firmware is allowed to request,
  591. * and not allow firmwares to request access to physical addresses that
  592. * are outside those ranges.
  593. */
  594. static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
  595. int offset, int avail)
  596. {
  597. struct rproc_mem_entry *mapping;
  598. struct device *dev = &rproc->dev;
  599. int ret;
  600. /* no point in handling this resource without a valid iommu domain */
  601. if (!rproc->domain)
  602. return 0;
  603. if (sizeof(*rsc) > avail) {
  604. dev_err(dev, "devmem rsc is truncated\n");
  605. return -EINVAL;
  606. }
  607. /* make sure reserved bytes are zeroes */
  608. if (rsc->reserved) {
  609. dev_err(dev, "devmem rsc has non zero reserved bytes\n");
  610. return -EINVAL;
  611. }
  612. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  613. if (!mapping)
  614. return -ENOMEM;
  615. if (!rproc->late_attach) {
  616. ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len,
  617. rsc->flags);
  618. if (ret) {
  619. dev_err(dev, "failed to map devmem: %d\n", ret);
  620. goto out;
  621. }
  622. }
  623. /*
  624. * We'll need this info later when we'll want to unmap everything
  625. * (e.g. on shutdown).
  626. *
  627. * We can't trust the remote processor not to change the resource
  628. * table, so we must maintain this info independently.
  629. */
  630. mapping->dma = rsc->pa;
  631. mapping->da = rsc->da;
  632. mapping->len = rsc->len;
  633. list_add_tail(&mapping->node, &rproc->mappings);
  634. if (!rproc->late_attach)
  635. dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
  636. rsc->pa, rsc->da, rsc->len);
  637. else
  638. dev_dbg(dev, "late-attach: processed devmem pa 0x%x, da 0x%x, len 0x%x\n",
  639. rsc->pa, rsc->da, rsc->len);
  640. return 0;
  641. out:
  642. kfree(mapping);
  643. return ret;
  644. }
  645. /**
  646. * rproc_handle_carveout() - handle phys contig memory allocation requests
  647. * @rproc: rproc handle
  648. * @rsc: the resource entry
  649. * @avail: size of available data (for image validation)
  650. *
  651. * This function will handle firmware requests for allocation of physically
  652. * contiguous memory regions.
  653. *
  654. * These request entries should come first in the firmware's resource table,
  655. * as other firmware entries might request placing other data objects inside
  656. * these memory regions (e.g. data/code segments, trace resource entries, ...).
  657. *
  658. * Allocating memory this way helps utilizing the reserved physical memory
  659. * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
  660. * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
  661. * pressure is important; it may have a substantial impact on performance.
  662. */
  663. static int rproc_handle_carveout(struct rproc *rproc,
  664. struct fw_rsc_carveout *rsc,
  665. int offset, int avail)
  666. {
  667. struct rproc_mem_entry *carveout, *mapping;
  668. struct device *dev = &rproc->dev;
  669. dma_addr_t dma;
  670. void *va;
  671. int ret;
  672. if (sizeof(*rsc) > avail) {
  673. dev_err(dev, "carveout rsc is truncated\n");
  674. return -EINVAL;
  675. }
  676. /* make sure reserved bytes are zeroes */
  677. if (rsc->reserved) {
  678. dev_err(dev, "carveout rsc has non zero reserved bytes\n");
  679. return -EINVAL;
  680. }
  681. dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
  682. rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
  683. carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
  684. if (!carveout)
  685. return -ENOMEM;
  686. if (rproc->late_attach) {
  687. va = dma_malloc_coherent(dev->parent, rsc->len, &dma,
  688. GFP_KERNEL);
  689. } else {
  690. va = dma_alloc_coherent(dev->parent, rsc->len, &dma,
  691. GFP_KERNEL);
  692. }
  693. if (!va) {
  694. dev_err(dev->parent,
  695. "failed to allocate dma memory: len 0x%x\n", rsc->len);
  696. ret = -ENOMEM;
  697. goto free_carv;
  698. }
  699. dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%x\n",
  700. va, &dma, rsc->len);
  701. /*
  702. * Ok, this is non-standard.
  703. *
  704. * Sometimes we can't rely on the generic iommu-based DMA API
  705. * to dynamically allocate the device address and then set the IOMMU
  706. * tables accordingly, because some remote processors might
  707. * _require_ us to use hard coded device addresses that their
  708. * firmware was compiled with.
  709. *
  710. * In this case, we must use the IOMMU API directly and map
  711. * the memory to the device address as expected by the remote
  712. * processor.
  713. *
  714. * Obviously such remote processor devices should not be configured
  715. * to use the iommu-based DMA API: we expect 'dma' to contain the
  716. * physical address in this case.
  717. */
  718. if (rproc->domain) {
  719. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  720. if (!mapping) {
  721. ret = -ENOMEM;
  722. goto dma_free;
  723. }
  724. if (!rproc->late_attach) {
  725. ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
  726. rsc->flags);
  727. if (ret) {
  728. dev_err(dev, "iommu_map failed: %d\n", ret);
  729. goto free_mapping;
  730. }
  731. }
  732. /*
  733. * We'll need this info later when we'll want to unmap
  734. * everything (e.g. on shutdown).
  735. *
  736. * We can't trust the remote processor not to change the
  737. * resource table, so we must maintain this info independently.
  738. */
  739. mapping->da = rsc->da;
  740. mapping->len = rsc->len;
  741. list_add_tail(&mapping->node, &rproc->mappings);
  742. if (!rproc->late_attach)
  743. dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
  744. rsc->da, &dma);
  745. else
  746. dev_dbg(dev, "late-attach: carveout processed 0x%x to %pad\n",
  747. rsc->da, &dma);
  748. }
  749. /*
  750. * Some remote processors might need to know the pa
  751. * even though they are behind an IOMMU. E.g., OMAP4's
  752. * remote M3 processor needs this so it can control
  753. * on-chip hardware accelerators that are not behind
  754. * the IOMMU, and therefor must know the pa.
  755. *
  756. * Generally we don't want to expose physical addresses
  757. * if we don't have to (remote processors are generally
  758. * _not_ trusted), so we might want to do this only for
  759. * remote processor that _must_ have this (e.g. OMAP4's
  760. * dual M3 subsystem).
  761. *
  762. * Non-IOMMU processors might also want to have this info.
  763. * In this case, the device address and the physical address
  764. * are the same.
  765. */
  766. rsc->pa = dma;
  767. carveout->va = va;
  768. carveout->len = rsc->len;
  769. carveout->dma = dma;
  770. carveout->da = rsc->da;
  771. strlcpy(carveout->name, rsc->name, sizeof(carveout->name));
  772. list_add_tail(&carveout->node, &rproc->carveouts);
  773. return 0;
  774. free_mapping:
  775. kfree(mapping);
  776. dma_free:
  777. dma_free_coherent(dev->parent, rsc->len, va, dma);
  778. free_carv:
  779. kfree(carveout);
  780. return ret;
  781. }
  782. /**
  783. * rproc_handle_vendor_rsc() - provide implementation specific hook
  784. * to handle vendor/custom resources
  785. * @rproc: the remote processor
  786. * @rsc: vendor resource to be handled by remoteproc drivers
  787. * @offset: offset of the resource data in resource table
  788. * @avail: size of available data
  789. *
  790. * Remoteproc implementations might want to add resource table entries
  791. * that are not generic enough to be handled by the framework. This
  792. * provides a hook to handle such custom resources. Note that a single
  793. * hook is reused between RSC_PRELOAD_VENDOR and RSC_PRELOAD_VENDOR
  794. * resources with the platform driver implementation distinguishing
  795. * the two based on the sub-type resource.
  796. *
  797. * Returns 0 on success, or an appropriate error code otherwise
  798. */
  799. static int rproc_handle_vendor_rsc(struct rproc *rproc,
  800. struct fw_rsc_vendor *rsc,
  801. int offset, int avail)
  802. {
  803. struct device *dev = &rproc->dev;
  804. if (!rproc->ops->handle_vendor_rsc) {
  805. dev_err(dev, "vendor resource handler not implemented, ignoring resource\n");
  806. return 0;
  807. }
  808. if (sizeof(*rsc) > avail) {
  809. dev_err(dev, "vendor resource is truncated\n");
  810. return -EINVAL;
  811. }
  812. return rproc->ops->handle_vendor_rsc(rproc, (void *)rsc);
  813. }
  814. /*
  815. * A lookup table for resource handlers. The indices are defined in
  816. * enum fw_resource_type.
  817. */
  818. static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
  819. [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
  820. [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
  821. [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
  822. [RSC_PRELOAD_VENDOR] = (rproc_handle_resource_t)rproc_handle_vendor_rsc,
  823. [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
  824. };
  825. static rproc_handle_resource_t rproc_post_loading_handlers[RSC_LAST] = {
  826. [RSC_POSTLOAD_VENDOR] =
  827. (rproc_handle_resource_t)rproc_handle_vendor_rsc,
  828. };
  829. /* handle firmware resource entries before booting the remote processor */
  830. static int rproc_handle_resources(struct rproc *rproc,
  831. rproc_handle_resource_t handlers[RSC_LAST])
  832. {
  833. struct device *dev = &rproc->dev;
  834. rproc_handle_resource_t handler;
  835. int ret = 0, i;
  836. if (!rproc->table_ptr)
  837. return 0;
  838. for (i = 0; i < rproc->table_ptr->num; i++) {
  839. int offset = rproc->table_ptr->offset[i];
  840. struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
  841. int avail = rproc->table_sz - offset - sizeof(*hdr);
  842. void *rsc = (void *)hdr + sizeof(*hdr);
  843. /* make sure table isn't truncated */
  844. if (avail < 0) {
  845. dev_err(dev, "rsc table is truncated\n");
  846. return -EINVAL;
  847. }
  848. dev_dbg(dev, "rsc: type %d\n", hdr->type);
  849. if (hdr->type >= RSC_LAST) {
  850. dev_warn(dev, "unsupported resource %d\n", hdr->type);
  851. continue;
  852. }
  853. handler = handlers[hdr->type];
  854. if (!handler)
  855. continue;
  856. ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
  857. if (ret)
  858. break;
  859. }
  860. return ret;
  861. }
  862. static int rproc_prepare_subdevices(struct rproc *rproc)
  863. {
  864. struct rproc_subdev *subdev;
  865. int ret;
  866. list_for_each_entry(subdev, &rproc->subdevs, node) {
  867. if (subdev->prepare) {
  868. ret = subdev->prepare(subdev);
  869. if (ret)
  870. goto unroll_preparation;
  871. }
  872. }
  873. return 0;
  874. unroll_preparation:
  875. list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
  876. if (subdev->unprepare)
  877. subdev->unprepare(subdev);
  878. }
  879. return ret;
  880. }
  881. static int rproc_start_subdevices(struct rproc *rproc)
  882. {
  883. struct rproc_subdev *subdev;
  884. int ret;
  885. list_for_each_entry(subdev, &rproc->subdevs, node) {
  886. if (subdev->start) {
  887. ret = subdev->start(subdev);
  888. if (ret)
  889. goto unroll_registration;
  890. }
  891. }
  892. return 0;
  893. unroll_registration:
  894. list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
  895. if (subdev->stop)
  896. subdev->stop(subdev, true);
  897. }
  898. return ret;
  899. }
  900. static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
  901. {
  902. struct rproc_subdev *subdev;
  903. list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
  904. if (subdev->stop)
  905. subdev->stop(subdev, crashed);
  906. }
  907. }
  908. static void rproc_unprepare_subdevices(struct rproc *rproc)
  909. {
  910. struct rproc_subdev *subdev;
  911. list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
  912. if (subdev->unprepare)
  913. subdev->unprepare(subdev);
  914. }
  915. }
  916. /**
  917. * rproc_coredump_cleanup() - clean up dump_segments list
  918. * @rproc: the remote processor handle
  919. */
  920. static void rproc_coredump_cleanup(struct rproc *rproc)
  921. {
  922. struct rproc_dump_segment *entry, *tmp;
  923. list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) {
  924. list_del(&entry->node);
  925. kfree(entry);
  926. }
  927. }
  928. /**
  929. * rproc_free_last_trace() - helper function to cleanup a last trace entry
  930. * @trace: the last trace element to be cleaned up
  931. */
  932. static void rproc_free_last_trace(struct rproc_mem_entry *trace)
  933. {
  934. rproc_remove_trace_file(trace->priv);
  935. list_del(&trace->node);
  936. vfree(trace->va);
  937. kfree(trace);
  938. }
  939. /**
  940. * rproc_resource_cleanup() - clean up and free all acquired resources
  941. * @rproc: rproc handle
  942. *
  943. * This function will free all resources acquired for @rproc, and it
  944. * is called whenever @rproc either shuts down or fails to boot.
  945. */
  946. static void rproc_resource_cleanup(struct rproc *rproc)
  947. {
  948. struct rproc_mem_entry *entry, *tmp;
  949. struct rproc_vdev *rvdev, *rvtmp;
  950. struct device *dev = &rproc->dev;
  951. int count = 0, i = rproc->num_traces;
  952. /* clean up debugfs trace entries */
  953. list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
  954. /* handle last trace here */
  955. if (rproc->state == RPROC_CRASHED)
  956. rproc_handle_last_trace(rproc, entry, ++count);
  957. rproc_remove_trace_file(entry->priv);
  958. list_del(&entry->node);
  959. kfree(entry);
  960. }
  961. rproc->num_traces = 0;
  962. /*
  963. * clean up debugfs last trace entries. This either deletes all last
  964. * trace entries during cleanup or just the remaining entries, if any,
  965. * in case of a crash.
  966. */
  967. list_for_each_entry_safe(entry, tmp, &rproc->last_traces, node) {
  968. /* skip the valid traces */
  969. if ((i--) && rproc->state == RPROC_CRASHED)
  970. continue;
  971. rproc_free_last_trace(entry);
  972. rproc->num_last_traces--;
  973. }
  974. /* clean up iommu mapping entries */
  975. list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
  976. size_t unmapped;
  977. if (!rproc->late_attach) {
  978. unmapped = iommu_unmap(rproc->domain, entry->da,
  979. entry->len);
  980. if (unmapped != entry->len) {
  981. /* nothing much to do besides complaining */
  982. dev_err(dev, "failed to unmap %u/%zu\n",
  983. entry->len, unmapped);
  984. }
  985. }
  986. list_del(&entry->node);
  987. kfree(entry);
  988. }
  989. /* clean up carveout allocations */
  990. list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
  991. dma_free_coherent(dev->parent, entry->len, entry->va,
  992. entry->dma);
  993. list_del(&entry->node);
  994. kfree(entry);
  995. }
  996. /* clean up remote vdev entries */
  997. list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
  998. kref_put(&rvdev->refcount, rproc_vdev_release);
  999. rproc_coredump_cleanup(rproc);
  1000. }
  1001. /*
  1002. * take a firmware and boot a remote processor with it.
  1003. */
  1004. static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
  1005. {
  1006. struct device *dev = &rproc->dev;
  1007. const char *name = rproc->firmware;
  1008. struct resource_table *loaded_table;
  1009. int ret;
  1010. ret = rproc_fw_sanity_check(rproc, fw);
  1011. if (ret)
  1012. return ret;
  1013. if (!rproc->skip_firmware_request)
  1014. dev_info(dev, "Booting fw image %s, size %zd\n",
  1015. name, fw->size);
  1016. else
  1017. dev_info(dev, "Booting unspecified pre-loaded fw image\n");
  1018. /*
  1019. * if enabling an IOMMU isn't relevant for this rproc, this is
  1020. * just a nop
  1021. */
  1022. ret = rproc_enable_iommu(rproc);
  1023. if (ret) {
  1024. dev_err(dev, "can't enable iommu: %d\n", ret);
  1025. return ret;
  1026. }
  1027. /* Prepare rproc for firmware loading if needed */
  1028. if (rproc->ops->prepare) {
  1029. ret = rproc->ops->prepare(rproc);
  1030. if (ret) {
  1031. dev_err(dev, "can't prepare rproc %s: %d\n",
  1032. rproc->name, ret);
  1033. goto disable_iommu;
  1034. }
  1035. }
  1036. rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
  1037. /* Load resource table, core dump segment list etc from the firmware */
  1038. ret = rproc_parse_fw(rproc, fw);
  1039. if (ret)
  1040. goto unprepare_rproc;
  1041. /* reset max_notifyid */
  1042. rproc->max_notifyid = -1;
  1043. /* handle fw resources which are required to boot rproc */
  1044. ret = rproc_handle_resources(rproc, rproc_loading_handlers);
  1045. if (ret) {
  1046. dev_err(dev, "Failed to process resources: %d\n", ret);
  1047. goto clean_up_resources;
  1048. }
  1049. if (!rproc->skip_load && !rproc->late_attach) {
  1050. /* load the ELF segments to memory */
  1051. ret = rproc_load_segments(rproc, fw);
  1052. if (ret) {
  1053. dev_err(dev, "Failed to load program segments: %d\n",
  1054. ret);
  1055. goto clean_up_resources;
  1056. }
  1057. } else {
  1058. dev_dbg(dev, "Skipped program segments load for pre-booted rproc\n");
  1059. }
  1060. /*
  1061. * The starting device has been given the rproc->cached_table as the
  1062. * resource table. The address of the vring along with the other
  1063. * allocated resources (carveouts etc) is stored in cached_table.
  1064. * In order to pass this information to the remote device we must copy
  1065. * this information to device memory. We also update the table_ptr so
  1066. * that any subsequent changes will be applied to the loaded version.
  1067. */
  1068. loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
  1069. if (loaded_table) {
  1070. memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
  1071. rproc->table_ptr = loaded_table;
  1072. }
  1073. /* handle fw resources which require fw segments to be loaded */
  1074. ret = rproc_handle_resources(rproc, rproc_post_loading_handlers);
  1075. if (ret) {
  1076. dev_err(dev, "Failed to process post-loading resources: %d\n",
  1077. ret);
  1078. goto reset_table_ptr;
  1079. }
  1080. ret = rproc_prepare_subdevices(rproc);
  1081. if (ret) {
  1082. dev_err(dev, "failed to prepare subdevices for %s: %d\n",
  1083. rproc->name, ret);
  1084. goto reset_table_ptr;
  1085. }
  1086. /* power up the remote processor */
  1087. ret = rproc->ops->start(rproc);
  1088. if (ret) {
  1089. dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
  1090. goto unprepare_subdevices;
  1091. }
  1092. /* Start any subdevices for the remote processor */
  1093. ret = rproc_start_subdevices(rproc);
  1094. if (ret) {
  1095. dev_err(dev, "failed to probe subdevices for %s: %d\n",
  1096. rproc->name, ret);
  1097. goto stop_rproc;
  1098. }
  1099. rproc->state = RPROC_RUNNING;
  1100. dev_info(dev, "remote processor %s is now up\n", rproc->name);
  1101. return 0;
  1102. stop_rproc:
  1103. rproc->ops->stop(rproc);
  1104. unprepare_subdevices:
  1105. rproc_unprepare_subdevices(rproc);
  1106. reset_table_ptr:
  1107. rproc->table_ptr = rproc->cached_table;
  1108. clean_up_resources:
  1109. rproc_resource_cleanup(rproc);
  1110. kfree(rproc->cached_table);
  1111. rproc->cached_table = NULL;
  1112. rproc->table_ptr = NULL;
  1113. rproc->table_sz = 0;
  1114. unprepare_rproc:
  1115. /* release HW resources if needed */
  1116. if (rproc->ops->unprepare)
  1117. rproc->ops->unprepare(rproc);
  1118. disable_iommu:
  1119. rproc_disable_iommu(rproc);
  1120. return ret;
  1121. }
  1122. /*
  1123. * take a firmware and boot it up.
  1124. *
  1125. * Note: this function is called asynchronously upon registration of the
  1126. * remote processor (so we must wait until it completes before we try
  1127. * to unregister the device. one other option is just to use kref here,
  1128. * that might be cleaner).
  1129. */
  1130. static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
  1131. {
  1132. struct rproc *rproc = context;
  1133. rproc_boot(rproc);
  1134. release_firmware(fw);
  1135. }
  1136. static int rproc_trigger_auto_boot(struct rproc *rproc)
  1137. {
  1138. int ret;
  1139. /*
  1140. * We're initiating an asynchronous firmware loading, so we can
  1141. * be built-in kernel code, without hanging the boot process.
  1142. */
  1143. ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
  1144. rproc->firmware, &rproc->dev, GFP_KERNEL,
  1145. rproc, rproc_auto_boot_callback);
  1146. if (ret < 0)
  1147. dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
  1148. return ret;
  1149. }
  1150. /**
  1151. * rproc_coredump_add_segment() - add segment of device memory to coredump
  1152. * @rproc: handle of a remote processor
  1153. * @da: device address
  1154. * @size: size of segment
  1155. *
  1156. * Add device memory to the list of segments to be included in a coredump for
  1157. * the remoteproc.
  1158. *
  1159. * Return: 0 on success, negative errno on error.
  1160. */
  1161. int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size)
  1162. {
  1163. struct rproc_dump_segment *segment;
  1164. segment = kzalloc(sizeof(*segment), GFP_KERNEL);
  1165. if (!segment)
  1166. return -ENOMEM;
  1167. segment->da = da;
  1168. segment->size = size;
  1169. list_add_tail(&segment->node, &rproc->dump_segments);
  1170. return 0;
  1171. }
  1172. EXPORT_SYMBOL(rproc_coredump_add_segment);
  1173. /**
  1174. * rproc_coredump() - perform coredump
  1175. * @rproc: rproc handle
  1176. *
  1177. * This function will generate an ELF header for the registered segments
  1178. * and create a devcoredump device associated with rproc.
  1179. */
  1180. static void rproc_coredump(struct rproc *rproc)
  1181. {
  1182. struct rproc_dump_segment *segment;
  1183. struct elf32_phdr *phdr;
  1184. struct elf32_hdr *ehdr;
  1185. size_t data_size;
  1186. size_t offset;
  1187. void *data;
  1188. void *ptr;
  1189. int phnum = 0;
  1190. if (list_empty(&rproc->dump_segments))
  1191. return;
  1192. data_size = sizeof(*ehdr);
  1193. list_for_each_entry(segment, &rproc->dump_segments, node) {
  1194. data_size += sizeof(*phdr) + segment->size;
  1195. phnum++;
  1196. }
  1197. data = vmalloc(data_size);
  1198. if (!data)
  1199. return;
  1200. ehdr = data;
  1201. memset(ehdr, 0, sizeof(*ehdr));
  1202. memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
  1203. ehdr->e_ident[EI_CLASS] = ELFCLASS32;
  1204. ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
  1205. ehdr->e_ident[EI_VERSION] = EV_CURRENT;
  1206. ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
  1207. ehdr->e_type = ET_CORE;
  1208. ehdr->e_machine = EM_NONE;
  1209. ehdr->e_version = EV_CURRENT;
  1210. ehdr->e_entry = rproc->bootaddr;
  1211. ehdr->e_phoff = sizeof(*ehdr);
  1212. ehdr->e_ehsize = sizeof(*ehdr);
  1213. ehdr->e_phentsize = sizeof(*phdr);
  1214. ehdr->e_phnum = phnum;
  1215. phdr = data + ehdr->e_phoff;
  1216. offset = ehdr->e_phoff + sizeof(*phdr) * ehdr->e_phnum;
  1217. list_for_each_entry(segment, &rproc->dump_segments, node) {
  1218. memset(phdr, 0, sizeof(*phdr));
  1219. phdr->p_type = PT_LOAD;
  1220. phdr->p_offset = offset;
  1221. phdr->p_vaddr = segment->da;
  1222. phdr->p_paddr = segment->da;
  1223. phdr->p_filesz = segment->size;
  1224. phdr->p_memsz = segment->size;
  1225. phdr->p_flags = PF_R | PF_W | PF_X;
  1226. phdr->p_align = 0;
  1227. ptr = rproc_da_to_va(rproc, segment->da, segment->size,
  1228. RPROC_FLAGS_NONE);
  1229. if (!ptr) {
  1230. dev_err(&rproc->dev,
  1231. "invalid coredump segment (%pad, %zu)\n",
  1232. &segment->da, segment->size);
  1233. memset(data + offset, 0xff, segment->size);
  1234. } else {
  1235. memcpy(data + offset, ptr, segment->size);
  1236. }
  1237. offset += phdr->p_filesz;
  1238. phdr++;
  1239. }
  1240. dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL);
  1241. }
  1242. /**
  1243. * rproc_trigger_recovery() - recover a remoteproc
  1244. * @rproc: the remote processor
  1245. *
  1246. * The recovery is done by resetting all the virtio devices, that way all the
  1247. * rpmsg drivers will be reseted along with the remote processor making the
  1248. * remoteproc functional again.
  1249. *
  1250. * This function can sleep, so it cannot be called from atomic context.
  1251. */
  1252. int rproc_trigger_recovery(struct rproc *rproc)
  1253. {
  1254. dev_err(&rproc->dev, "recovering %s\n", rproc->name);
  1255. init_completion(&rproc->crash_comp);
  1256. /* shut down the remote */
  1257. /* TODO: make sure this works with rproc->power > 1 */
  1258. rproc_shutdown(rproc);
  1259. /* wait until there is no more rproc users */
  1260. wait_for_completion(&rproc->crash_comp);
  1261. /*
  1262. * boot the remote processor up again
  1263. */
  1264. rproc_boot(rproc);
  1265. return 0;
  1266. }
  1267. /**
  1268. * rproc_crash_handler_work() - handle a crash
  1269. *
  1270. * This function needs to handle everything related to a crash, like cpu
  1271. * registers and stack dump, information to help to debug the fatal error, etc.
  1272. */
  1273. static void rproc_crash_handler_work(struct work_struct *work)
  1274. {
  1275. struct rproc *rproc = container_of(work, struct rproc, crash_handler);
  1276. struct device *dev = &rproc->dev;
  1277. dev_dbg(dev, "enter %s\n", __func__);
  1278. mutex_lock(&rproc->lock);
  1279. if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
  1280. /* handle only the first crash detected */
  1281. mutex_unlock(&rproc->lock);
  1282. return;
  1283. }
  1284. rproc->state = RPROC_CRASHED;
  1285. dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
  1286. rproc->name);
  1287. mutex_unlock(&rproc->lock);
  1288. if (!rproc->recovery_disabled)
  1289. rproc_trigger_recovery(rproc);
  1290. }
  1291. /**
  1292. * rproc_get_id() - return the id for the rproc device
  1293. * @rproc: handle of a remote processor
  1294. *
  1295. * Each rproc device is associated with a platform device, which is created
  1296. * either from device tree (majority newer platforms) or using legacy style
  1297. * platform device creation (fewer legacy platforms). This function retrieves
  1298. * an unique id for each remote processor and is useful for clients needing
  1299. * to distinguish each of the remoteprocs. This unique id is derived using
  1300. * the platform device id for non-DT devices, or an alternate alias id for
  1301. * DT devices (since they do not have a valid platform device id). It is
  1302. * assumed that the platform devices were created with known ids or were
  1303. * given proper alias ids using the stem "rproc".
  1304. *
  1305. * Return: alias id for DT devices or platform device id for non-DT devices
  1306. * associated with the rproc
  1307. */
  1308. int rproc_get_id(struct rproc *rproc)
  1309. {
  1310. struct device *dev = rproc->dev.parent;
  1311. struct device_node *np = dev->of_node;
  1312. struct platform_device *pdev = to_platform_device(dev);
  1313. if (np)
  1314. return of_alias_get_id(np, "rproc");
  1315. else
  1316. return pdev->id;
  1317. }
  1318. EXPORT_SYMBOL(rproc_get_id);
  1319. /**
  1320. * rproc_boot() - boot a remote processor
  1321. * @rproc: handle of a remote processor
  1322. *
  1323. * Boot a remote processor (i.e. load its firmware, power it on, ...).
  1324. *
  1325. * If the remote processor is already powered on, this function immediately
  1326. * returns (successfully).
  1327. *
  1328. * Returns 0 on success, and an appropriate error value otherwise.
  1329. */
  1330. int rproc_boot(struct rproc *rproc)
  1331. {
  1332. const struct firmware *firmware_p;
  1333. struct device *dev;
  1334. int ret;
  1335. if (!rproc) {
  1336. pr_err("invalid rproc handle\n");
  1337. return -EINVAL;
  1338. }
  1339. dev = &rproc->dev;
  1340. ret = mutex_lock_interruptible(&rproc->lock);
  1341. if (ret) {
  1342. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  1343. return ret;
  1344. }
  1345. if (rproc->state == RPROC_DELETED) {
  1346. ret = -ENODEV;
  1347. dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
  1348. goto unlock_mutex;
  1349. }
  1350. /* skip the boot process if rproc is already powered up */
  1351. if (atomic_inc_return(&rproc->power) > 1) {
  1352. ret = 0;
  1353. goto unlock_mutex;
  1354. }
  1355. dev_info(dev, "powering up %s\n", rproc->name);
  1356. if (!rproc->skip_firmware_request) {
  1357. /* load firmware */
  1358. ret = request_firmware(&firmware_p, rproc->firmware, dev);
  1359. if (ret < 0) {
  1360. dev_err(dev, "request_firmware failed: %d\n", ret);
  1361. goto downref_rproc;
  1362. }
  1363. }
  1364. ret = rproc_fw_boot(rproc, firmware_p);
  1365. if (!rproc->skip_firmware_request)
  1366. release_firmware(firmware_p);
  1367. downref_rproc:
  1368. if (ret)
  1369. atomic_dec(&rproc->power);
  1370. unlock_mutex:
  1371. mutex_unlock(&rproc->lock);
  1372. return ret;
  1373. }
  1374. EXPORT_SYMBOL(rproc_boot);
  1375. /**
  1376. * rproc_shutdown() - power off the remote processor
  1377. * @rproc: the remote processor
  1378. *
  1379. * Power off a remote processor (previously booted with rproc_boot()).
  1380. *
  1381. * In case @rproc is still being used by an additional user(s), then
  1382. * this function will just decrement the power refcount and exit,
  1383. * without really powering off the device.
  1384. *
  1385. * Every call to rproc_boot() must (eventually) be accompanied by a call
  1386. * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
  1387. *
  1388. * Notes:
  1389. * - we're not decrementing the rproc's refcount, only the power refcount.
  1390. * which means that the @rproc handle stays valid even after rproc_shutdown()
  1391. * returns, and users can still use it with a subsequent rproc_boot(), if
  1392. * needed.
  1393. */
  1394. void rproc_shutdown(struct rproc *rproc)
  1395. {
  1396. struct device *dev = &rproc->dev;
  1397. int ret;
  1398. bool crashed = false;
  1399. ret = mutex_lock_interruptible(&rproc->lock);
  1400. if (ret) {
  1401. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  1402. return;
  1403. }
  1404. /* if the remote proc is still needed, bail out */
  1405. if (!atomic_dec_and_test(&rproc->power))
  1406. goto out;
  1407. if (rproc->state == RPROC_CRASHED)
  1408. crashed = true;
  1409. /* remove any subdevices for the remote processor */
  1410. rproc_stop_subdevices(rproc, crashed);
  1411. /* power off the remote processor */
  1412. ret = rproc->ops->stop(rproc);
  1413. if (ret) {
  1414. atomic_inc(&rproc->power);
  1415. dev_err(dev, "can't stop rproc: %d\n", ret);
  1416. goto out;
  1417. }
  1418. rproc_unprepare_subdevices(rproc);
  1419. /* generate coredump */
  1420. if (rproc->state == RPROC_CRASHED)
  1421. rproc_coredump(rproc);
  1422. /* the installed resource table may no longer be accessible */
  1423. rproc->table_ptr = rproc->cached_table;
  1424. /* clean up all acquired resources */
  1425. rproc_resource_cleanup(rproc);
  1426. /* release HW resources if needed */
  1427. if (rproc->ops->unprepare)
  1428. rproc->ops->unprepare(rproc);
  1429. rproc_disable_iommu(rproc);
  1430. /* Free the copy of the resource table */
  1431. kfree(rproc->cached_table);
  1432. rproc->cached_table = NULL;
  1433. rproc->table_ptr = NULL;
  1434. /* if in crash state, unlock crash handler */
  1435. if (rproc->state == RPROC_CRASHED)
  1436. complete_all(&rproc->crash_comp);
  1437. rproc->state = RPROC_OFFLINE;
  1438. rproc->late_attach = 0;
  1439. dev_info(dev, "stopped remote processor %s\n", rproc->name);
  1440. out:
  1441. mutex_unlock(&rproc->lock);
  1442. }
  1443. EXPORT_SYMBOL(rproc_shutdown);
  1444. /**
  1445. * rproc_get_by_phandle() - find a remote processor by phandle
  1446. * @phandle: phandle to the rproc
  1447. *
  1448. * Finds an rproc handle using the remote processor's phandle, and then
  1449. * return a handle to the rproc.
  1450. *
  1451. * This function increments the remote processor's refcount, so always
  1452. * use rproc_put() to decrement it back once rproc isn't needed anymore.
  1453. *
  1454. * Returns the rproc handle on success, and NULL on failure.
  1455. */
  1456. #ifdef CONFIG_OF
  1457. struct rproc *rproc_get_by_phandle(phandle phandle)
  1458. {
  1459. struct rproc *rproc = NULL, *r;
  1460. struct device_node *np;
  1461. np = of_find_node_by_phandle(phandle);
  1462. if (!np)
  1463. return NULL;
  1464. mutex_lock(&rproc_list_mutex);
  1465. list_for_each_entry(r, &rproc_list, node) {
  1466. if (r->dev.parent && r->dev.parent->of_node == np) {
  1467. /* prevent underlying implementation from being removed */
  1468. if (!try_module_get(r->dev.parent->driver->owner)) {
  1469. dev_err(&r->dev, "can't get owner\n");
  1470. break;
  1471. }
  1472. rproc = r;
  1473. get_device(&rproc->dev);
  1474. break;
  1475. }
  1476. }
  1477. mutex_unlock(&rproc_list_mutex);
  1478. of_node_put(np);
  1479. return rproc;
  1480. }
  1481. #else
  1482. struct rproc *rproc_get_by_phandle(phandle phandle)
  1483. {
  1484. return NULL;
  1485. }
  1486. #endif
  1487. EXPORT_SYMBOL(rproc_get_by_phandle);
  1488. /**
  1489. * rproc_add() - register a remote processor
  1490. * @rproc: the remote processor handle to register
  1491. *
  1492. * Registers @rproc with the remoteproc framework, after it has been
  1493. * allocated with rproc_alloc().
  1494. *
  1495. * This is called by the platform-specific rproc implementation, whenever
  1496. * a new remote processor device is probed.
  1497. *
  1498. * Returns 0 on success and an appropriate error code otherwise.
  1499. *
  1500. * Note: this function initiates an asynchronous firmware loading
  1501. * context, which will look for virtio devices supported by the rproc's
  1502. * firmware.
  1503. *
  1504. * If found, those virtio devices will be created and added, so as a result
  1505. * of registering this remote processor, additional virtio drivers might be
  1506. * probed.
  1507. */
  1508. int rproc_add(struct rproc *rproc)
  1509. {
  1510. struct device *dev = &rproc->dev;
  1511. int ret;
  1512. ret = device_add(dev);
  1513. if (ret < 0)
  1514. return ret;
  1515. dev_info(dev, "%s is available\n", rproc->name);
  1516. /* create debugfs entries */
  1517. rproc_create_debug_dir(rproc);
  1518. /* if rproc is marked always-on, request it to boot */
  1519. if (rproc->auto_boot) {
  1520. ret = rproc_trigger_auto_boot(rproc);
  1521. if (ret < 0)
  1522. return ret;
  1523. }
  1524. /* expose to rproc_get_by_phandle users */
  1525. mutex_lock(&rproc_list_mutex);
  1526. list_add(&rproc->node, &rproc_list);
  1527. mutex_unlock(&rproc_list_mutex);
  1528. return 0;
  1529. }
  1530. EXPORT_SYMBOL(rproc_add);
  1531. /**
  1532. * rproc_type_release() - release a remote processor instance
  1533. * @dev: the rproc's device
  1534. *
  1535. * This function should _never_ be called directly.
  1536. *
  1537. * It will be called by the driver core when no one holds a valid pointer
  1538. * to @dev anymore.
  1539. */
  1540. static void rproc_type_release(struct device *dev)
  1541. {
  1542. struct rproc *rproc = container_of(dev, struct rproc, dev);
  1543. dev_info(&rproc->dev, "releasing %s\n", rproc->name);
  1544. idr_destroy(&rproc->notifyids);
  1545. if (rproc->index >= 0)
  1546. ida_simple_remove(&rproc_dev_index, rproc->index);
  1547. kfree(rproc->firmware);
  1548. kfree(rproc->ops);
  1549. kfree(rproc->name);
  1550. kfree(rproc);
  1551. }
  1552. static const struct device_type rproc_type = {
  1553. .name = "remoteproc",
  1554. .release = rproc_type_release,
  1555. };
  1556. /**
  1557. * rproc_alloc() - allocate a remote processor handle
  1558. * @dev: the underlying device
  1559. * @name: name of this remote processor
  1560. * @ops: platform-specific handlers (mainly start/stop)
  1561. * @firmware: name of firmware file to load, can be NULL
  1562. * @len: length of private data needed by the rproc driver (in bytes)
  1563. *
  1564. * Allocates a new remote processor handle, but does not register
  1565. * it yet. if @firmware is NULL, a default name is used.
  1566. *
  1567. * This function should be used by rproc implementations during initialization
  1568. * of the remote processor.
  1569. *
  1570. * After creating an rproc handle using this function, and when ready,
  1571. * implementations should then call rproc_add() to complete
  1572. * the registration of the remote processor.
  1573. *
  1574. * On success the new rproc is returned, and on failure, NULL.
  1575. *
  1576. * Note: _never_ directly deallocate @rproc, even if it was not registered
  1577. * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
  1578. */
  1579. struct rproc *rproc_alloc(struct device *dev, const char *name,
  1580. const struct rproc_ops *ops,
  1581. const char *firmware, int len)
  1582. {
  1583. struct rproc *rproc;
  1584. char *p, *template = "rproc-%s-fw";
  1585. int name_len;
  1586. if (!dev || !name || !ops)
  1587. return NULL;
  1588. if (!firmware) {
  1589. /*
  1590. * If the caller didn't pass in a firmware name then
  1591. * construct a default name.
  1592. */
  1593. name_len = strlen(name) + strlen(template) - 2 + 1;
  1594. p = kmalloc(name_len, GFP_KERNEL);
  1595. if (!p)
  1596. return NULL;
  1597. snprintf(p, name_len, template, name);
  1598. } else {
  1599. p = kstrdup(firmware, GFP_KERNEL);
  1600. if (!p)
  1601. return NULL;
  1602. }
  1603. rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
  1604. if (!rproc) {
  1605. kfree(p);
  1606. return NULL;
  1607. }
  1608. rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
  1609. if (!rproc->ops) {
  1610. kfree(p);
  1611. kfree(rproc);
  1612. return NULL;
  1613. }
  1614. rproc->firmware = p;
  1615. rproc->name = kstrdup(name, GFP_KERNEL);
  1616. if (!rproc->name) {
  1617. kfree(p);
  1618. kfree(rproc->ops);
  1619. kfree(rproc);
  1620. return NULL;
  1621. }
  1622. rproc->priv = &rproc[1];
  1623. rproc->auto_boot = true;
  1624. device_initialize(&rproc->dev);
  1625. rproc->dev.parent = dev;
  1626. rproc->dev.type = &rproc_type;
  1627. rproc->dev.class = &rproc_class;
  1628. rproc->dev.driver_data = rproc;
  1629. /* Assign a unique device index and name */
  1630. rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
  1631. if (rproc->index < 0) {
  1632. dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
  1633. put_device(&rproc->dev);
  1634. return NULL;
  1635. }
  1636. dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
  1637. atomic_set(&rproc->power, 0);
  1638. /* Default to ELF loader if no load function is specified */
  1639. if (!rproc->ops->load) {
  1640. rproc->ops->load = rproc_elf_load_segments;
  1641. rproc->ops->parse_fw = rproc_elf_load_rsc_table;
  1642. rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
  1643. rproc->ops->sanity_check = rproc_elf_sanity_check;
  1644. rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
  1645. }
  1646. mutex_init(&rproc->lock);
  1647. idr_init(&rproc->notifyids);
  1648. INIT_LIST_HEAD(&rproc->carveouts);
  1649. INIT_LIST_HEAD(&rproc->mappings);
  1650. INIT_LIST_HEAD(&rproc->traces);
  1651. INIT_LIST_HEAD(&rproc->last_traces);
  1652. INIT_LIST_HEAD(&rproc->rvdevs);
  1653. INIT_LIST_HEAD(&rproc->subdevs);
  1654. INIT_LIST_HEAD(&rproc->dump_segments);
  1655. INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
  1656. init_completion(&rproc->crash_comp);
  1657. rproc->state = RPROC_OFFLINE;
  1658. return rproc;
  1659. }
  1660. EXPORT_SYMBOL(rproc_alloc);
  1661. /**
  1662. * rproc_free() - unroll rproc_alloc()
  1663. * @rproc: the remote processor handle
  1664. *
  1665. * This function decrements the rproc dev refcount.
  1666. *
  1667. * If no one holds any reference to rproc anymore, then its refcount would
  1668. * now drop to zero, and it would be freed.
  1669. */
  1670. void rproc_free(struct rproc *rproc)
  1671. {
  1672. put_device(&rproc->dev);
  1673. }
  1674. EXPORT_SYMBOL(rproc_free);
  1675. /**
  1676. * rproc_put() - release rproc reference
  1677. * @rproc: the remote processor handle
  1678. *
  1679. * This function decrements the rproc dev refcount.
  1680. *
  1681. * If no one holds any reference to rproc anymore, then its refcount would
  1682. * now drop to zero, and it would be freed.
  1683. */
  1684. void rproc_put(struct rproc *rproc)
  1685. {
  1686. module_put(rproc->dev.parent->driver->owner);
  1687. put_device(&rproc->dev);
  1688. }
  1689. EXPORT_SYMBOL(rproc_put);
  1690. /**
  1691. * rproc_del() - unregister a remote processor
  1692. * @rproc: rproc handle to unregister
  1693. *
  1694. * This function should be called when the platform specific rproc
  1695. * implementation decides to remove the rproc device. it should
  1696. * _only_ be called if a previous invocation of rproc_add()
  1697. * has completed successfully.
  1698. *
  1699. * After rproc_del() returns, @rproc isn't freed yet, because
  1700. * of the outstanding reference created by rproc_alloc. To decrement that
  1701. * one last refcount, one still needs to call rproc_free().
  1702. *
  1703. * Returns 0 on success and -EINVAL if @rproc isn't valid.
  1704. */
  1705. int rproc_del(struct rproc *rproc)
  1706. {
  1707. struct rproc_mem_entry *entry, *tmp;
  1708. if (!rproc)
  1709. return -EINVAL;
  1710. /* if rproc is marked always-on, rproc_add() booted it */
  1711. /* TODO: make sure this works with rproc->power > 1 */
  1712. if (rproc->auto_boot)
  1713. rproc_shutdown(rproc);
  1714. mutex_lock(&rproc->lock);
  1715. rproc->state = RPROC_DELETED;
  1716. mutex_unlock(&rproc->lock);
  1717. /* clean up debugfs last trace entries */
  1718. list_for_each_entry_safe(entry, tmp, &rproc->last_traces, node) {
  1719. rproc_free_last_trace(entry);
  1720. rproc->num_last_traces--;
  1721. }
  1722. rproc_delete_debug_dir(rproc);
  1723. /* the rproc is downref'ed as soon as it's removed from the klist */
  1724. mutex_lock(&rproc_list_mutex);
  1725. list_del(&rproc->node);
  1726. mutex_unlock(&rproc_list_mutex);
  1727. device_del(&rproc->dev);
  1728. return 0;
  1729. }
  1730. EXPORT_SYMBOL(rproc_del);
  1731. /**
  1732. * rproc_add_subdev() - add a subdevice to a remoteproc
  1733. * @rproc: rproc handle to add the subdevice to
  1734. * @subdev: subdev handle to register
  1735. *
  1736. * Caller is responsible for populating optional subdevice function pointers.
  1737. */
  1738. void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
  1739. {
  1740. list_add_tail(&subdev->node, &rproc->subdevs);
  1741. }
  1742. EXPORT_SYMBOL(rproc_add_subdev);
  1743. /**
  1744. * rproc_remove_subdev() - remove a subdevice from a remoteproc
  1745. * @rproc: rproc handle to remove the subdevice from
  1746. * @subdev: subdev handle, previously registered with rproc_add_subdev()
  1747. */
  1748. void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
  1749. {
  1750. list_del(&subdev->node);
  1751. }
  1752. EXPORT_SYMBOL(rproc_remove_subdev);
  1753. /**
  1754. * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
  1755. * @dev: child device to find ancestor of
  1756. *
  1757. * Returns the ancestor rproc instance, or NULL if not found.
  1758. */
  1759. struct rproc *rproc_get_by_child(struct device *dev)
  1760. {
  1761. for (dev = dev->parent; dev; dev = dev->parent) {
  1762. if (dev->type == &rproc_type)
  1763. return dev->driver_data;
  1764. }
  1765. return NULL;
  1766. }
  1767. EXPORT_SYMBOL(rproc_get_by_child);
  1768. /**
  1769. * rproc_report_crash() - rproc crash reporter function
  1770. * @rproc: remote processor
  1771. * @type: crash type
  1772. *
  1773. * This function must be called every time a crash is detected by the low-level
  1774. * drivers implementing a specific remoteproc. This should not be called from a
  1775. * non-remoteproc driver.
  1776. *
  1777. * This function can be called from atomic/interrupt context.
  1778. */
  1779. void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
  1780. {
  1781. if (!rproc) {
  1782. pr_err("NULL rproc pointer\n");
  1783. return;
  1784. }
  1785. dev_err(&rproc->dev, "crash detected in %s: type %s\n",
  1786. rproc->name, rproc_crash_to_string(type));
  1787. /* create a new task to handle the error if not scheduled already */
  1788. if (!work_busy(&rproc->crash_handler))
  1789. schedule_work(&rproc->crash_handler);
  1790. }
  1791. EXPORT_SYMBOL(rproc_report_crash);
  1792. /**
  1793. * rproc_set_firmware() - assign a new firmware
  1794. * @rproc: rproc handle to which the new firmware is being assigned
  1795. * @fw_name: new firmware name to be assigned
  1796. *
  1797. * This function allows remoteproc drivers or clients to configure a custom
  1798. * firmware name that is different from the default name used during remoteproc
  1799. * registration. The function does not trigger a remote processor boot,
  1800. * only sets the firmware name used for a subsequent boot. This function
  1801. * should also be called only when the remote processor is offline.
  1802. *
  1803. * This allows either the userspace to configure a different name through
  1804. * sysfs or a kernel-level remoteproc or a remoteproc client driver to set
  1805. * a specific firmware when it is controlling the boot and shutdown of the
  1806. * remote processor.
  1807. *
  1808. * Returns 0 on success or a negative value upon failure
  1809. */
  1810. int rproc_set_firmware(struct rproc *rproc, const char *fw_name)
  1811. {
  1812. struct device *dev;
  1813. int ret, len;
  1814. char *p;
  1815. if (!rproc || !fw_name)
  1816. return -EINVAL;
  1817. dev = rproc->dev.parent;
  1818. ret = mutex_lock_interruptible(&rproc->lock);
  1819. if (ret) {
  1820. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  1821. return -EINVAL;
  1822. }
  1823. if (rproc->state != RPROC_OFFLINE) {
  1824. dev_err(dev, "can't change firmware while running\n");
  1825. ret = -EBUSY;
  1826. goto out;
  1827. }
  1828. len = strcspn(fw_name, "\n");
  1829. if (!len) {
  1830. dev_err(dev, "can't provide empty string for firmware name\n");
  1831. ret = -EINVAL;
  1832. goto out;
  1833. }
  1834. p = kstrndup(fw_name, len, GFP_KERNEL);
  1835. if (!p) {
  1836. ret = -ENOMEM;
  1837. goto out;
  1838. }
  1839. kfree(rproc->firmware);
  1840. rproc->firmware = p;
  1841. out:
  1842. mutex_unlock(&rproc->lock);
  1843. return ret;
  1844. }
  1845. EXPORT_SYMBOL(rproc_set_firmware);
  1846. static int __init remoteproc_init(void)
  1847. {
  1848. rproc_init_sysfs();
  1849. rproc_init_debugfs();
  1850. return 0;
  1851. }
  1852. module_init(remoteproc_init);
  1853. static void __exit remoteproc_exit(void)
  1854. {
  1855. ida_destroy(&rproc_dev_index);
  1856. rproc_exit_debugfs();
  1857. rproc_exit_sysfs();
  1858. }
  1859. module_exit(remoteproc_exit);
  1860. MODULE_LICENSE("GPL v2");
  1861. MODULE_DESCRIPTION("Generic Remote Processor Framework");