remoteproc_core.c 46 KB

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