remoteproc_core.c 42 KB

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