remoteproc_core.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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/klist.h>
  37. #include <linux/elf.h>
  38. #include <linux/virtio_ids.h>
  39. #include <linux/virtio_ring.h>
  40. #include <asm/byteorder.h>
  41. #include "remoteproc_internal.h"
  42. static void klist_rproc_get(struct klist_node *n);
  43. static void klist_rproc_put(struct klist_node *n);
  44. /*
  45. * klist of the available remote processors.
  46. *
  47. * We need this in order to support name-based lookups (needed by the
  48. * rproc_get_by_name()).
  49. *
  50. * That said, we don't use rproc_get_by_name() at this point.
  51. * The use cases that do require its existence should be
  52. * scrutinized, and hopefully migrated to rproc_boot() using device-based
  53. * binding.
  54. *
  55. * If/when this materializes, we could drop the klist (and the by_name
  56. * API).
  57. */
  58. static DEFINE_KLIST(rprocs, klist_rproc_get, klist_rproc_put);
  59. typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
  60. struct resource_table *table, int len);
  61. typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail);
  62. /*
  63. * This is the IOMMU fault handler we register with the IOMMU API
  64. * (when relevant; not all remote processors access memory through
  65. * an IOMMU).
  66. *
  67. * IOMMU core will invoke this handler whenever the remote processor
  68. * will try to access an unmapped device address.
  69. *
  70. * Currently this is mostly a stub, but it will be later used to trigger
  71. * the recovery of the remote processor.
  72. */
  73. static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
  74. unsigned long iova, int flags, void *token)
  75. {
  76. dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
  77. /*
  78. * Let the iommu core know we're not really handling this fault;
  79. * we just plan to use this 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;
  87. int ret;
  88. /*
  89. * We currently use iommu_present() to decide if an IOMMU
  90. * setup is needed.
  91. *
  92. * This works for simple cases, but will easily fail with
  93. * platforms that do have an IOMMU, but not for this specific
  94. * rproc.
  95. *
  96. * This will be easily solved by introducing hw capabilities
  97. * that will be set by the remoteproc driver.
  98. */
  99. if (!iommu_present(dev->bus)) {
  100. dev_dbg(dev, "iommu not found\n");
  101. return 0;
  102. }
  103. domain = iommu_domain_alloc(dev->bus);
  104. if (!domain) {
  105. dev_err(dev, "can't alloc iommu domain\n");
  106. return -ENOMEM;
  107. }
  108. iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
  109. ret = iommu_attach_device(domain, dev);
  110. if (ret) {
  111. dev_err(dev, "can't attach iommu device: %d\n", ret);
  112. goto free_domain;
  113. }
  114. rproc->domain = domain;
  115. return 0;
  116. free_domain:
  117. iommu_domain_free(domain);
  118. return ret;
  119. }
  120. static void rproc_disable_iommu(struct rproc *rproc)
  121. {
  122. struct iommu_domain *domain = rproc->domain;
  123. struct device *dev = rproc->dev;
  124. if (!domain)
  125. return;
  126. iommu_detach_device(domain, dev);
  127. iommu_domain_free(domain);
  128. return;
  129. }
  130. /*
  131. * Some remote processors will ask us to allocate them physically contiguous
  132. * memory regions (which we call "carveouts"), and map them to specific
  133. * device addresses (which are hardcoded in the firmware).
  134. *
  135. * They may then ask us to copy objects into specific device addresses (e.g.
  136. * code/data sections) or expose us certain symbols in other device address
  137. * (e.g. their trace buffer).
  138. *
  139. * This function is an internal helper with which we can go over the allocated
  140. * carveouts and translate specific device address to kernel virtual addresses
  141. * so we can access the referenced memory.
  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, which should be more correct.
  146. */
  147. static void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
  148. {
  149. struct rproc_mem_entry *carveout;
  150. void *ptr = NULL;
  151. list_for_each_entry(carveout, &rproc->carveouts, node) {
  152. int offset = da - carveout->da;
  153. /* try next carveout if da is too small */
  154. if (offset < 0)
  155. continue;
  156. /* try next carveout if da is too large */
  157. if (offset + len > carveout->len)
  158. continue;
  159. ptr = carveout->va + offset;
  160. break;
  161. }
  162. return ptr;
  163. }
  164. /**
  165. * rproc_load_segments() - load firmware segments to memory
  166. * @rproc: remote processor which will be booted using these fw segments
  167. * @elf_data: the content of the ELF firmware image
  168. * @len: firmware size (in bytes)
  169. *
  170. * This function loads the firmware segments to memory, where the remote
  171. * processor expects them.
  172. *
  173. * Some remote processors will expect their code and data to be placed
  174. * in specific device addresses, and can't have them dynamically assigned.
  175. *
  176. * We currently support only those kind of remote processors, and expect
  177. * the program header's paddr member to contain those addresses. We then go
  178. * through the physically contiguous "carveout" memory regions which we
  179. * allocated (and mapped) earlier on behalf of the remote processor,
  180. * and "translate" device address to kernel addresses, so we can copy the
  181. * segments where they are expected.
  182. *
  183. * Currently we only support remote processors that required carveout
  184. * allocations and got them mapped onto their iommus. Some processors
  185. * might be different: they might not have iommus, and would prefer to
  186. * directly allocate memory for every segment/resource. This is not yet
  187. * supported, though.
  188. */
  189. static int
  190. rproc_load_segments(struct rproc *rproc, const u8 *elf_data, size_t len)
  191. {
  192. struct device *dev = rproc->dev;
  193. struct elf32_hdr *ehdr;
  194. struct elf32_phdr *phdr;
  195. int i, ret = 0;
  196. ehdr = (struct elf32_hdr *)elf_data;
  197. phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
  198. /* go through the available ELF segments */
  199. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  200. u32 da = phdr->p_paddr;
  201. u32 memsz = phdr->p_memsz;
  202. u32 filesz = phdr->p_filesz;
  203. u32 offset = phdr->p_offset;
  204. void *ptr;
  205. if (phdr->p_type != PT_LOAD)
  206. continue;
  207. dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
  208. phdr->p_type, da, memsz, filesz);
  209. if (filesz > memsz) {
  210. dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
  211. filesz, memsz);
  212. ret = -EINVAL;
  213. break;
  214. }
  215. if (offset + filesz > len) {
  216. dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
  217. offset + filesz, len);
  218. ret = -EINVAL;
  219. break;
  220. }
  221. /* grab the kernel address for this device address */
  222. ptr = rproc_da_to_va(rproc, da, memsz);
  223. if (!ptr) {
  224. dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
  225. ret = -EINVAL;
  226. break;
  227. }
  228. /* put the segment where the remote processor expects it */
  229. if (phdr->p_filesz)
  230. memcpy(ptr, elf_data + phdr->p_offset, filesz);
  231. /*
  232. * Zero out remaining memory for this segment.
  233. *
  234. * This isn't strictly required since dma_alloc_coherent already
  235. * did this for us. albeit harmless, we may consider removing
  236. * this.
  237. */
  238. if (memsz > filesz)
  239. memset(ptr + filesz, 0, memsz - filesz);
  240. }
  241. return ret;
  242. }
  243. static int
  244. __rproc_handle_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
  245. {
  246. struct rproc *rproc = rvdev->rproc;
  247. struct device *dev = rproc->dev;
  248. struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
  249. dma_addr_t dma;
  250. void *va;
  251. int ret, size, notifyid;
  252. dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
  253. i, vring->da, vring->num, vring->align);
  254. /* make sure reserved bytes are zeroes */
  255. if (vring->reserved) {
  256. dev_err(dev, "vring rsc has non zero reserved bytes\n");
  257. return -EINVAL;
  258. }
  259. /* verify queue size and vring alignment are sane */
  260. if (!vring->num || !vring->align) {
  261. dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
  262. vring->num, vring->align);
  263. return -EINVAL;
  264. }
  265. /* actual size of vring (in bytes) */
  266. size = PAGE_ALIGN(vring_size(vring->num, vring->align));
  267. if (!idr_pre_get(&rproc->notifyids, GFP_KERNEL)) {
  268. dev_err(dev, "idr_pre_get failed\n");
  269. return -ENOMEM;
  270. }
  271. /*
  272. * Allocate non-cacheable memory for the vring. In the future
  273. * this call will also configure the IOMMU for us
  274. */
  275. va = dma_alloc_coherent(dev, size, &dma, GFP_KERNEL);
  276. if (!va) {
  277. dev_err(dev, "dma_alloc_coherent failed\n");
  278. return -EINVAL;
  279. }
  280. /* assign an rproc-wide unique index for this vring */
  281. /* TODO: assign a notifyid for rvdev updates as well */
  282. ret = idr_get_new(&rproc->notifyids, &rvdev->vring[i], &notifyid);
  283. if (ret) {
  284. dev_err(dev, "idr_get_new failed: %d\n", ret);
  285. dma_free_coherent(dev, size, va, dma);
  286. return ret;
  287. }
  288. /* let the rproc know the da and notifyid of this vring */
  289. /* TODO: expose this to remote processor */
  290. vring->da = dma;
  291. vring->notifyid = notifyid;
  292. dev_dbg(dev, "vring%d: va %p dma %x size %x idr %d\n", i, va,
  293. dma, size, notifyid);
  294. rvdev->vring[i].len = vring->num;
  295. rvdev->vring[i].align = vring->align;
  296. rvdev->vring[i].va = va;
  297. rvdev->vring[i].dma = dma;
  298. rvdev->vring[i].notifyid = notifyid;
  299. rvdev->vring[i].rvdev = rvdev;
  300. return 0;
  301. }
  302. static void __rproc_free_vrings(struct rproc_vdev *rvdev, int i)
  303. {
  304. struct rproc *rproc = rvdev->rproc;
  305. for (i--; i >= 0; i--) {
  306. struct rproc_vring *rvring = &rvdev->vring[i];
  307. int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
  308. dma_free_coherent(rproc->dev, size, rvring->va, rvring->dma);
  309. idr_remove(&rproc->notifyids, rvring->notifyid);
  310. }
  311. }
  312. /**
  313. * rproc_handle_vdev() - handle a vdev fw resource
  314. * @rproc: the remote processor
  315. * @rsc: the vring resource descriptor
  316. * @avail: size of available data (for sanity checking the image)
  317. *
  318. * This resource entry requests the host to statically register a virtio
  319. * device (vdev), and setup everything needed to support it. It contains
  320. * everything needed to make it possible: the virtio device id, virtio
  321. * device features, vrings information, virtio config space, etc...
  322. *
  323. * Before registering the vdev, the vrings are allocated from non-cacheable
  324. * physically contiguous memory. Currently we only support two vrings per
  325. * remote processor (temporary limitation). We might also want to consider
  326. * doing the vring allocation only later when ->find_vqs() is invoked, and
  327. * then release them upon ->del_vqs().
  328. *
  329. * Note: @da is currently not really handled correctly: we dynamically
  330. * allocate it using the DMA API, ignoring requested hard coded addresses,
  331. * and we don't take care of any required IOMMU programming. This is all
  332. * going to be taken care of when the generic iommu-based DMA API will be
  333. * merged. Meanwhile, statically-addressed iommu-based firmware images should
  334. * use RSC_DEVMEM resource entries to map their required @da to the physical
  335. * address of their base CMA region (ouch, hacky!).
  336. *
  337. * Returns 0 on success, or an appropriate error code otherwise
  338. */
  339. static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
  340. int avail)
  341. {
  342. struct device *dev = rproc->dev;
  343. struct rproc_vdev *rvdev;
  344. int i, ret;
  345. /* make sure resource isn't truncated */
  346. if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
  347. + rsc->config_len > avail) {
  348. dev_err(rproc->dev, "vdev rsc is truncated\n");
  349. return -EINVAL;
  350. }
  351. /* make sure reserved bytes are zeroes */
  352. if (rsc->reserved[0] || rsc->reserved[1]) {
  353. dev_err(dev, "vdev rsc has non zero reserved bytes\n");
  354. return -EINVAL;
  355. }
  356. dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
  357. rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
  358. /* we currently support only two vrings per rvdev */
  359. if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
  360. dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
  361. return -EINVAL;
  362. }
  363. rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
  364. if (!rvdev)
  365. return -ENOMEM;
  366. rvdev->rproc = rproc;
  367. /* allocate the vrings */
  368. for (i = 0; i < rsc->num_of_vrings; i++) {
  369. ret = __rproc_handle_vring(rvdev, rsc, i);
  370. if (ret)
  371. goto free_vrings;
  372. }
  373. /* remember the device features */
  374. rvdev->dfeatures = rsc->dfeatures;
  375. list_add_tail(&rvdev->node, &rproc->rvdevs);
  376. /* it is now safe to add the virtio device */
  377. ret = rproc_add_virtio_dev(rvdev, rsc->id);
  378. if (ret)
  379. goto free_vrings;
  380. return 0;
  381. free_vrings:
  382. __rproc_free_vrings(rvdev, i);
  383. kfree(rvdev);
  384. return ret;
  385. }
  386. /**
  387. * rproc_handle_trace() - handle a shared trace buffer resource
  388. * @rproc: the remote processor
  389. * @rsc: the trace resource descriptor
  390. * @avail: size of available data (for sanity checking the image)
  391. *
  392. * In case the remote processor dumps trace logs into memory,
  393. * export it via debugfs.
  394. *
  395. * Currently, the 'da' member of @rsc should contain the device address
  396. * where the remote processor is dumping the traces. Later we could also
  397. * support dynamically allocating this address using the generic
  398. * DMA API (but currently there isn't a use case for that).
  399. *
  400. * Returns 0 on success, or an appropriate error code otherwise
  401. */
  402. static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
  403. int avail)
  404. {
  405. struct rproc_mem_entry *trace;
  406. struct device *dev = rproc->dev;
  407. void *ptr;
  408. char name[15];
  409. if (sizeof(*rsc) > avail) {
  410. dev_err(rproc->dev, "trace rsc is truncated\n");
  411. return -EINVAL;
  412. }
  413. /* make sure reserved bytes are zeroes */
  414. if (rsc->reserved) {
  415. dev_err(dev, "trace rsc has non zero reserved bytes\n");
  416. return -EINVAL;
  417. }
  418. /* what's the kernel address of this resource ? */
  419. ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
  420. if (!ptr) {
  421. dev_err(dev, "erroneous trace resource entry\n");
  422. return -EINVAL;
  423. }
  424. trace = kzalloc(sizeof(*trace), GFP_KERNEL);
  425. if (!trace) {
  426. dev_err(dev, "kzalloc trace failed\n");
  427. return -ENOMEM;
  428. }
  429. /* set the trace buffer dma properties */
  430. trace->len = rsc->len;
  431. trace->va = ptr;
  432. /* make sure snprintf always null terminates, even if truncating */
  433. snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
  434. /* create the debugfs entry */
  435. trace->priv = rproc_create_trace_file(name, rproc, trace);
  436. if (!trace->priv) {
  437. trace->va = NULL;
  438. kfree(trace);
  439. return -EINVAL;
  440. }
  441. list_add_tail(&trace->node, &rproc->traces);
  442. rproc->num_traces++;
  443. dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
  444. rsc->da, rsc->len);
  445. return 0;
  446. }
  447. /**
  448. * rproc_handle_devmem() - handle devmem resource entry
  449. * @rproc: remote processor handle
  450. * @rsc: the devmem resource entry
  451. * @avail: size of available data (for sanity checking the image)
  452. *
  453. * Remote processors commonly need to access certain on-chip peripherals.
  454. *
  455. * Some of these remote processors access memory via an iommu device,
  456. * and might require us to configure their iommu before they can access
  457. * the on-chip peripherals they need.
  458. *
  459. * This resource entry is a request to map such a peripheral device.
  460. *
  461. * These devmem entries will contain the physical address of the device in
  462. * the 'pa' member. If a specific device address is expected, then 'da' will
  463. * contain it (currently this is the only use case supported). 'len' will
  464. * contain the size of the physical region we need to map.
  465. *
  466. * Currently we just "trust" those devmem entries to contain valid physical
  467. * addresses, but this is going to change: we want the implementations to
  468. * tell us ranges of physical addresses the firmware is allowed to request,
  469. * and not allow firmwares to request access to physical addresses that
  470. * are outside those ranges.
  471. */
  472. static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
  473. int avail)
  474. {
  475. struct rproc_mem_entry *mapping;
  476. int ret;
  477. /* no point in handling this resource without a valid iommu domain */
  478. if (!rproc->domain)
  479. return -EINVAL;
  480. if (sizeof(*rsc) > avail) {
  481. dev_err(rproc->dev, "devmem rsc is truncated\n");
  482. return -EINVAL;
  483. }
  484. /* make sure reserved bytes are zeroes */
  485. if (rsc->reserved) {
  486. dev_err(rproc->dev, "devmem rsc has non zero reserved bytes\n");
  487. return -EINVAL;
  488. }
  489. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  490. if (!mapping) {
  491. dev_err(rproc->dev, "kzalloc mapping failed\n");
  492. return -ENOMEM;
  493. }
  494. ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
  495. if (ret) {
  496. dev_err(rproc->dev, "failed to map devmem: %d\n", ret);
  497. goto out;
  498. }
  499. /*
  500. * We'll need this info later when we'll want to unmap everything
  501. * (e.g. on shutdown).
  502. *
  503. * We can't trust the remote processor not to change the resource
  504. * table, so we must maintain this info independently.
  505. */
  506. mapping->da = rsc->da;
  507. mapping->len = rsc->len;
  508. list_add_tail(&mapping->node, &rproc->mappings);
  509. dev_dbg(rproc->dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
  510. rsc->pa, rsc->da, rsc->len);
  511. return 0;
  512. out:
  513. kfree(mapping);
  514. return ret;
  515. }
  516. /**
  517. * rproc_handle_carveout() - handle phys contig memory allocation requests
  518. * @rproc: rproc handle
  519. * @rsc: the resource entry
  520. * @avail: size of available data (for image validation)
  521. *
  522. * This function will handle firmware requests for allocation of physically
  523. * contiguous memory regions.
  524. *
  525. * These request entries should come first in the firmware's resource table,
  526. * as other firmware entries might request placing other data objects inside
  527. * these memory regions (e.g. data/code segments, trace resource entries, ...).
  528. *
  529. * Allocating memory this way helps utilizing the reserved physical memory
  530. * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
  531. * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
  532. * pressure is important; it may have a substantial impact on performance.
  533. */
  534. static int rproc_handle_carveout(struct rproc *rproc,
  535. struct fw_rsc_carveout *rsc, int avail)
  536. {
  537. struct rproc_mem_entry *carveout, *mapping;
  538. struct device *dev = rproc->dev;
  539. dma_addr_t dma;
  540. void *va;
  541. int ret;
  542. if (sizeof(*rsc) > avail) {
  543. dev_err(rproc->dev, "carveout rsc is truncated\n");
  544. return -EINVAL;
  545. }
  546. /* make sure reserved bytes are zeroes */
  547. if (rsc->reserved) {
  548. dev_err(dev, "carveout rsc has non zero reserved bytes\n");
  549. return -EINVAL;
  550. }
  551. dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
  552. rsc->da, rsc->pa, rsc->len, rsc->flags);
  553. carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
  554. if (!carveout) {
  555. dev_err(dev, "kzalloc carveout failed\n");
  556. return -ENOMEM;
  557. }
  558. va = dma_alloc_coherent(dev, rsc->len, &dma, GFP_KERNEL);
  559. if (!va) {
  560. dev_err(dev, "failed to dma alloc carveout: %d\n", rsc->len);
  561. ret = -ENOMEM;
  562. goto free_carv;
  563. }
  564. dev_dbg(dev, "carveout va %p, dma %x, len 0x%x\n", va, dma, rsc->len);
  565. /*
  566. * Ok, this is non-standard.
  567. *
  568. * Sometimes we can't rely on the generic iommu-based DMA API
  569. * to dynamically allocate the device address and then set the IOMMU
  570. * tables accordingly, because some remote processors might
  571. * _require_ us to use hard coded device addresses that their
  572. * firmware was compiled with.
  573. *
  574. * In this case, we must use the IOMMU API directly and map
  575. * the memory to the device address as expected by the remote
  576. * processor.
  577. *
  578. * Obviously such remote processor devices should not be configured
  579. * to use the iommu-based DMA API: we expect 'dma' to contain the
  580. * physical address in this case.
  581. */
  582. if (rproc->domain) {
  583. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  584. if (!mapping) {
  585. dev_err(dev, "kzalloc mapping failed\n");
  586. ret = -ENOMEM;
  587. goto dma_free;
  588. }
  589. ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
  590. rsc->flags);
  591. if (ret) {
  592. dev_err(dev, "iommu_map failed: %d\n", ret);
  593. goto free_mapping;
  594. }
  595. /*
  596. * We'll need this info later when we'll want to unmap
  597. * everything (e.g. on shutdown).
  598. *
  599. * We can't trust the remote processor not to change the
  600. * resource table, so we must maintain this info independently.
  601. */
  602. mapping->da = rsc->da;
  603. mapping->len = rsc->len;
  604. list_add_tail(&mapping->node, &rproc->mappings);
  605. dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma);
  606. /*
  607. * Some remote processors might need to know the pa
  608. * even though they are behind an IOMMU. E.g., OMAP4's
  609. * remote M3 processor needs this so it can control
  610. * on-chip hardware accelerators that are not behind
  611. * the IOMMU, and therefor must know the pa.
  612. *
  613. * Generally we don't want to expose physical addresses
  614. * if we don't have to (remote processors are generally
  615. * _not_ trusted), so we might want to do this only for
  616. * remote processor that _must_ have this (e.g. OMAP4's
  617. * dual M3 subsystem).
  618. */
  619. rsc->pa = dma;
  620. }
  621. carveout->va = va;
  622. carveout->len = rsc->len;
  623. carveout->dma = dma;
  624. carveout->da = rsc->da;
  625. list_add_tail(&carveout->node, &rproc->carveouts);
  626. return 0;
  627. free_mapping:
  628. kfree(mapping);
  629. dma_free:
  630. dma_free_coherent(dev, rsc->len, va, dma);
  631. free_carv:
  632. kfree(carveout);
  633. return ret;
  634. }
  635. /*
  636. * A lookup table for resource handlers. The indices are defined in
  637. * enum fw_resource_type.
  638. */
  639. static rproc_handle_resource_t rproc_handle_rsc[] = {
  640. [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
  641. [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
  642. [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
  643. [RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
  644. };
  645. /* handle firmware resource entries before booting the remote processor */
  646. static int
  647. rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
  648. {
  649. struct device *dev = rproc->dev;
  650. rproc_handle_resource_t handler;
  651. int ret = 0, i;
  652. for (i = 0; i < table->num; i++) {
  653. int offset = table->offset[i];
  654. struct fw_rsc_hdr *hdr = (void *)table + offset;
  655. int avail = len - offset - sizeof(*hdr);
  656. void *rsc = (void *)hdr + sizeof(*hdr);
  657. /* make sure table isn't truncated */
  658. if (avail < 0) {
  659. dev_err(dev, "rsc table is truncated\n");
  660. return -EINVAL;
  661. }
  662. dev_dbg(dev, "rsc: type %d\n", hdr->type);
  663. if (hdr->type >= RSC_LAST) {
  664. dev_warn(dev, "unsupported resource %d\n", hdr->type);
  665. continue;
  666. }
  667. handler = rproc_handle_rsc[hdr->type];
  668. if (!handler)
  669. continue;
  670. ret = handler(rproc, rsc, avail);
  671. if (ret)
  672. break;
  673. }
  674. return ret;
  675. }
  676. /* handle firmware resource entries while registering the remote processor */
  677. static int
  678. rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
  679. {
  680. struct device *dev = rproc->dev;
  681. int ret = 0, i;
  682. for (i = 0; i < table->num; i++) {
  683. int offset = table->offset[i];
  684. struct fw_rsc_hdr *hdr = (void *)table + offset;
  685. int avail = len - offset - sizeof(*hdr);
  686. struct fw_rsc_vdev *vrsc;
  687. /* make sure table isn't truncated */
  688. if (avail < 0) {
  689. dev_err(dev, "rsc table is truncated\n");
  690. return -EINVAL;
  691. }
  692. dev_dbg(dev, "%s: rsc type %d\n", __func__, hdr->type);
  693. if (hdr->type != RSC_VDEV)
  694. continue;
  695. vrsc = (struct fw_rsc_vdev *)hdr->data;
  696. ret = rproc_handle_vdev(rproc, vrsc, avail);
  697. if (ret)
  698. break;
  699. }
  700. return ret;
  701. }
  702. /**
  703. * rproc_find_rsc_table() - find the resource table
  704. * @rproc: the rproc handle
  705. * @elf_data: the content of the ELF firmware image
  706. * @len: firmware size (in bytes)
  707. * @tablesz: place holder for providing back the table size
  708. *
  709. * This function finds the resource table inside the remote processor's
  710. * firmware. It is used both upon the registration of @rproc (in order
  711. * to look for and register the supported virito devices), and when the
  712. * @rproc is booted.
  713. *
  714. * Returns the pointer to the resource table if it is found, and write its
  715. * size into @tablesz. If a valid table isn't found, NULL is returned
  716. * (and @tablesz isn't set).
  717. */
  718. static struct resource_table *
  719. rproc_find_rsc_table(struct rproc *rproc, const u8 *elf_data, size_t len,
  720. int *tablesz)
  721. {
  722. struct elf32_hdr *ehdr;
  723. struct elf32_shdr *shdr;
  724. const char *name_table;
  725. struct device *dev = rproc->dev;
  726. struct resource_table *table = NULL;
  727. int i;
  728. ehdr = (struct elf32_hdr *)elf_data;
  729. shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
  730. name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
  731. /* look for the resource table and handle it */
  732. for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
  733. int size = shdr->sh_size;
  734. int offset = shdr->sh_offset;
  735. if (strcmp(name_table + shdr->sh_name, ".resource_table"))
  736. continue;
  737. table = (struct resource_table *)(elf_data + offset);
  738. /* make sure we have the entire table */
  739. if (offset + size > len) {
  740. dev_err(dev, "resource table truncated\n");
  741. return NULL;
  742. }
  743. /* make sure table has at least the header */
  744. if (sizeof(struct resource_table) > size) {
  745. dev_err(dev, "header-less resource table\n");
  746. return NULL;
  747. }
  748. /* we don't support any version beyond the first */
  749. if (table->ver != 1) {
  750. dev_err(dev, "unsupported fw ver: %d\n", table->ver);
  751. return NULL;
  752. }
  753. /* make sure reserved bytes are zeroes */
  754. if (table->reserved[0] || table->reserved[1]) {
  755. dev_err(dev, "non zero reserved bytes\n");
  756. return NULL;
  757. }
  758. /* make sure the offsets array isn't truncated */
  759. if (table->num * sizeof(table->offset[0]) +
  760. sizeof(struct resource_table) > size) {
  761. dev_err(dev, "resource table incomplete\n");
  762. return NULL;
  763. }
  764. *tablesz = shdr->sh_size;
  765. break;
  766. }
  767. return table;
  768. }
  769. /**
  770. * rproc_resource_cleanup() - clean up and free all acquired resources
  771. * @rproc: rproc handle
  772. *
  773. * This function will free all resources acquired for @rproc, and it
  774. * is called whenever @rproc either shuts down or fails to boot.
  775. */
  776. static void rproc_resource_cleanup(struct rproc *rproc)
  777. {
  778. struct rproc_mem_entry *entry, *tmp;
  779. struct device *dev = rproc->dev;
  780. /* clean up debugfs trace entries */
  781. list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
  782. rproc_remove_trace_file(entry->priv);
  783. rproc->num_traces--;
  784. list_del(&entry->node);
  785. kfree(entry);
  786. }
  787. /* clean up carveout allocations */
  788. list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
  789. dma_free_coherent(dev, entry->len, entry->va, entry->dma);
  790. list_del(&entry->node);
  791. kfree(entry);
  792. }
  793. /* clean up iommu mapping entries */
  794. list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
  795. size_t unmapped;
  796. unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
  797. if (unmapped != entry->len) {
  798. /* nothing much to do besides complaining */
  799. dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
  800. unmapped);
  801. }
  802. list_del(&entry->node);
  803. kfree(entry);
  804. }
  805. }
  806. /* make sure this fw image is sane */
  807. static int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
  808. {
  809. const char *name = rproc->firmware;
  810. struct device *dev = rproc->dev;
  811. struct elf32_hdr *ehdr;
  812. char class;
  813. if (!fw) {
  814. dev_err(dev, "failed to load %s\n", name);
  815. return -EINVAL;
  816. }
  817. if (fw->size < sizeof(struct elf32_hdr)) {
  818. dev_err(dev, "Image is too small\n");
  819. return -EINVAL;
  820. }
  821. ehdr = (struct elf32_hdr *)fw->data;
  822. /* We only support ELF32 at this point */
  823. class = ehdr->e_ident[EI_CLASS];
  824. if (class != ELFCLASS32) {
  825. dev_err(dev, "Unsupported class: %d\n", class);
  826. return -EINVAL;
  827. }
  828. /* We assume the firmware has the same endianess as the host */
  829. # ifdef __LITTLE_ENDIAN
  830. if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
  831. # else /* BIG ENDIAN */
  832. if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
  833. # endif
  834. dev_err(dev, "Unsupported firmware endianess\n");
  835. return -EINVAL;
  836. }
  837. if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
  838. dev_err(dev, "Image is too small\n");
  839. return -EINVAL;
  840. }
  841. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  842. dev_err(dev, "Image is corrupted (bad magic)\n");
  843. return -EINVAL;
  844. }
  845. if (ehdr->e_phnum == 0) {
  846. dev_err(dev, "No loadable segments\n");
  847. return -EINVAL;
  848. }
  849. if (ehdr->e_phoff > fw->size) {
  850. dev_err(dev, "Firmware size is too small\n");
  851. return -EINVAL;
  852. }
  853. return 0;
  854. }
  855. /*
  856. * take a firmware and boot a remote processor with it.
  857. */
  858. static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
  859. {
  860. struct device *dev = rproc->dev;
  861. const char *name = rproc->firmware;
  862. struct elf32_hdr *ehdr;
  863. struct resource_table *table;
  864. int ret, tablesz;
  865. ret = rproc_fw_sanity_check(rproc, fw);
  866. if (ret)
  867. return ret;
  868. ehdr = (struct elf32_hdr *)fw->data;
  869. dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
  870. /*
  871. * if enabling an IOMMU isn't relevant for this rproc, this is
  872. * just a nop
  873. */
  874. ret = rproc_enable_iommu(rproc);
  875. if (ret) {
  876. dev_err(dev, "can't enable iommu: %d\n", ret);
  877. return ret;
  878. }
  879. /*
  880. * The ELF entry point is the rproc's boot addr (though this is not
  881. * a configurable property of all remote processors: some will always
  882. * boot at a specific hardcoded address).
  883. */
  884. rproc->bootaddr = ehdr->e_entry;
  885. /* look for the resource table */
  886. table = rproc_find_rsc_table(rproc, fw->data, fw->size, &tablesz);
  887. if (!table) {
  888. ret = -EINVAL;
  889. goto clean_up;
  890. }
  891. /* handle fw resources which are required to boot rproc */
  892. ret = rproc_handle_boot_rsc(rproc, table, tablesz);
  893. if (ret) {
  894. dev_err(dev, "Failed to process resources: %d\n", ret);
  895. goto clean_up;
  896. }
  897. /* load the ELF segments to memory */
  898. ret = rproc_load_segments(rproc, fw->data, fw->size);
  899. if (ret) {
  900. dev_err(dev, "Failed to load program segments: %d\n", ret);
  901. goto clean_up;
  902. }
  903. /* power up the remote processor */
  904. ret = rproc->ops->start(rproc);
  905. if (ret) {
  906. dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
  907. goto clean_up;
  908. }
  909. rproc->state = RPROC_RUNNING;
  910. dev_info(dev, "remote processor %s is now up\n", rproc->name);
  911. return 0;
  912. clean_up:
  913. rproc_resource_cleanup(rproc);
  914. rproc_disable_iommu(rproc);
  915. return ret;
  916. }
  917. /*
  918. * take a firmware and look for virtio devices to register.
  919. *
  920. * Note: this function is called asynchronously upon registration of the
  921. * remote processor (so we must wait until it completes before we try
  922. * to unregister the device. one other option is just to use kref here,
  923. * that might be cleaner).
  924. */
  925. static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
  926. {
  927. struct rproc *rproc = context;
  928. struct resource_table *table;
  929. int ret, tablesz;
  930. if (rproc_fw_sanity_check(rproc, fw) < 0)
  931. goto out;
  932. /* look for the resource table */
  933. table = rproc_find_rsc_table(rproc, fw->data, fw->size, &tablesz);
  934. if (!table)
  935. goto out;
  936. /* look for virtio devices and register them */
  937. ret = rproc_handle_virtio_rsc(rproc, table, tablesz);
  938. if (ret)
  939. goto out;
  940. out:
  941. if (fw)
  942. release_firmware(fw);
  943. /* allow rproc_unregister() contexts, if any, to proceed */
  944. complete_all(&rproc->firmware_loading_complete);
  945. }
  946. /**
  947. * rproc_boot() - boot a remote processor
  948. * @rproc: handle of a remote processor
  949. *
  950. * Boot a remote processor (i.e. load its firmware, power it on, ...).
  951. *
  952. * If the remote processor is already powered on, this function immediately
  953. * returns (successfully).
  954. *
  955. * Returns 0 on success, and an appropriate error value otherwise.
  956. */
  957. int rproc_boot(struct rproc *rproc)
  958. {
  959. const struct firmware *firmware_p;
  960. struct device *dev;
  961. int ret;
  962. if (!rproc) {
  963. pr_err("invalid rproc handle\n");
  964. return -EINVAL;
  965. }
  966. dev = rproc->dev;
  967. ret = mutex_lock_interruptible(&rproc->lock);
  968. if (ret) {
  969. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  970. return ret;
  971. }
  972. /* loading a firmware is required */
  973. if (!rproc->firmware) {
  974. dev_err(dev, "%s: no firmware to load\n", __func__);
  975. ret = -EINVAL;
  976. goto unlock_mutex;
  977. }
  978. /* prevent underlying implementation from being removed */
  979. if (!try_module_get(dev->driver->owner)) {
  980. dev_err(dev, "%s: can't get owner\n", __func__);
  981. ret = -EINVAL;
  982. goto unlock_mutex;
  983. }
  984. /* skip the boot process if rproc is already powered up */
  985. if (atomic_inc_return(&rproc->power) > 1) {
  986. ret = 0;
  987. goto unlock_mutex;
  988. }
  989. dev_info(dev, "powering up %s\n", rproc->name);
  990. /* load firmware */
  991. ret = request_firmware(&firmware_p, rproc->firmware, dev);
  992. if (ret < 0) {
  993. dev_err(dev, "request_firmware failed: %d\n", ret);
  994. goto downref_rproc;
  995. }
  996. ret = rproc_fw_boot(rproc, firmware_p);
  997. release_firmware(firmware_p);
  998. downref_rproc:
  999. if (ret) {
  1000. module_put(dev->driver->owner);
  1001. atomic_dec(&rproc->power);
  1002. }
  1003. unlock_mutex:
  1004. mutex_unlock(&rproc->lock);
  1005. return ret;
  1006. }
  1007. EXPORT_SYMBOL(rproc_boot);
  1008. /**
  1009. * rproc_shutdown() - power off the remote processor
  1010. * @rproc: the remote processor
  1011. *
  1012. * Power off a remote processor (previously booted with rproc_boot()).
  1013. *
  1014. * In case @rproc is still being used by an additional user(s), then
  1015. * this function will just decrement the power refcount and exit,
  1016. * without really powering off the device.
  1017. *
  1018. * Every call to rproc_boot() must (eventually) be accompanied by a call
  1019. * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
  1020. *
  1021. * Notes:
  1022. * - we're not decrementing the rproc's refcount, only the power refcount.
  1023. * which means that the @rproc handle stays valid even after rproc_shutdown()
  1024. * returns, and users can still use it with a subsequent rproc_boot(), if
  1025. * needed.
  1026. * - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly
  1027. * because rproc_shutdown() _does not_ decrement the refcount of @rproc.
  1028. * To decrement the refcount of @rproc, use rproc_put() (but _only_ if
  1029. * you acquired @rproc using rproc_get_by_name()).
  1030. */
  1031. void rproc_shutdown(struct rproc *rproc)
  1032. {
  1033. struct device *dev = rproc->dev;
  1034. int ret;
  1035. ret = mutex_lock_interruptible(&rproc->lock);
  1036. if (ret) {
  1037. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  1038. return;
  1039. }
  1040. /* if the remote proc is still needed, bail out */
  1041. if (!atomic_dec_and_test(&rproc->power))
  1042. goto out;
  1043. /* power off the remote processor */
  1044. ret = rproc->ops->stop(rproc);
  1045. if (ret) {
  1046. atomic_inc(&rproc->power);
  1047. dev_err(dev, "can't stop rproc: %d\n", ret);
  1048. goto out;
  1049. }
  1050. /* clean up all acquired resources */
  1051. rproc_resource_cleanup(rproc);
  1052. rproc_disable_iommu(rproc);
  1053. rproc->state = RPROC_OFFLINE;
  1054. dev_info(dev, "stopped remote processor %s\n", rproc->name);
  1055. out:
  1056. mutex_unlock(&rproc->lock);
  1057. if (!ret)
  1058. module_put(dev->driver->owner);
  1059. }
  1060. EXPORT_SYMBOL(rproc_shutdown);
  1061. /**
  1062. * rproc_release() - completely deletes the existence of a remote processor
  1063. * @kref: the rproc's kref
  1064. *
  1065. * This function should _never_ be called directly.
  1066. *
  1067. * The only reasonable location to use it is as an argument when kref_put'ing
  1068. * @rproc's refcount.
  1069. *
  1070. * This way it will be called when no one holds a valid pointer to this @rproc
  1071. * anymore (and obviously after it is removed from the rprocs klist).
  1072. *
  1073. * Note: this function is not static because rproc_vdev_release() needs it when
  1074. * it decrements @rproc's refcount.
  1075. */
  1076. void rproc_release(struct kref *kref)
  1077. {
  1078. struct rproc *rproc = container_of(kref, struct rproc, refcount);
  1079. struct rproc_vdev *rvdev, *rvtmp;
  1080. dev_info(rproc->dev, "removing %s\n", rproc->name);
  1081. rproc_delete_debug_dir(rproc);
  1082. /* clean up remote vdev entries */
  1083. list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) {
  1084. __rproc_free_vrings(rvdev, RVDEV_NUM_VRINGS);
  1085. list_del(&rvdev->node);
  1086. }
  1087. /*
  1088. * At this point no one holds a reference to rproc anymore,
  1089. * so we can directly unroll rproc_alloc()
  1090. */
  1091. rproc_free(rproc);
  1092. }
  1093. /* will be called when an rproc is added to the rprocs klist */
  1094. static void klist_rproc_get(struct klist_node *n)
  1095. {
  1096. struct rproc *rproc = container_of(n, struct rproc, node);
  1097. kref_get(&rproc->refcount);
  1098. }
  1099. /* will be called when an rproc is removed from the rprocs klist */
  1100. static void klist_rproc_put(struct klist_node *n)
  1101. {
  1102. struct rproc *rproc = container_of(n, struct rproc, node);
  1103. kref_put(&rproc->refcount, rproc_release);
  1104. }
  1105. static struct rproc *next_rproc(struct klist_iter *i)
  1106. {
  1107. struct klist_node *n;
  1108. n = klist_next(i);
  1109. if (!n)
  1110. return NULL;
  1111. return container_of(n, struct rproc, node);
  1112. }
  1113. /**
  1114. * rproc_get_by_name() - find a remote processor by name and boot it
  1115. * @name: name of the remote processor
  1116. *
  1117. * Finds an rproc handle using the remote processor's name, and then
  1118. * boot it. If it's already powered on, then just immediately return
  1119. * (successfully).
  1120. *
  1121. * Returns the rproc handle on success, and NULL on failure.
  1122. *
  1123. * This function increments the remote processor's refcount, so always
  1124. * use rproc_put() to decrement it back once rproc isn't needed anymore.
  1125. *
  1126. * Note: currently this function (and its counterpart rproc_put()) are not
  1127. * being used. We need to scrutinize the use cases
  1128. * that still need them, and see if we can migrate them to use the non
  1129. * name-based boot/shutdown interface.
  1130. */
  1131. struct rproc *rproc_get_by_name(const char *name)
  1132. {
  1133. struct rproc *rproc;
  1134. struct klist_iter i;
  1135. int ret;
  1136. /* find the remote processor, and upref its refcount */
  1137. klist_iter_init(&rprocs, &i);
  1138. while ((rproc = next_rproc(&i)) != NULL)
  1139. if (!strcmp(rproc->name, name)) {
  1140. kref_get(&rproc->refcount);
  1141. break;
  1142. }
  1143. klist_iter_exit(&i);
  1144. /* can't find this rproc ? */
  1145. if (!rproc) {
  1146. pr_err("can't find remote processor %s\n", name);
  1147. return NULL;
  1148. }
  1149. ret = rproc_boot(rproc);
  1150. if (ret < 0) {
  1151. kref_put(&rproc->refcount, rproc_release);
  1152. return NULL;
  1153. }
  1154. return rproc;
  1155. }
  1156. EXPORT_SYMBOL(rproc_get_by_name);
  1157. /**
  1158. * rproc_put() - decrement the refcount of a remote processor, and shut it down
  1159. * @rproc: the remote processor
  1160. *
  1161. * This function tries to shutdown @rproc, and it then decrements its
  1162. * refcount.
  1163. *
  1164. * After this function returns, @rproc may _not_ be used anymore, and its
  1165. * handle should be considered invalid.
  1166. *
  1167. * This function should be called _iff_ the @rproc handle was grabbed by
  1168. * calling rproc_get_by_name().
  1169. */
  1170. void rproc_put(struct rproc *rproc)
  1171. {
  1172. /* try to power off the remote processor */
  1173. rproc_shutdown(rproc);
  1174. /* downref rproc's refcount */
  1175. kref_put(&rproc->refcount, rproc_release);
  1176. }
  1177. EXPORT_SYMBOL(rproc_put);
  1178. /**
  1179. * rproc_register() - register a remote processor
  1180. * @rproc: the remote processor handle to register
  1181. *
  1182. * Registers @rproc with the remoteproc framework, after it has been
  1183. * allocated with rproc_alloc().
  1184. *
  1185. * This is called by the platform-specific rproc implementation, whenever
  1186. * a new remote processor device is probed.
  1187. *
  1188. * Returns 0 on success and an appropriate error code otherwise.
  1189. *
  1190. * Note: this function initiates an asynchronous firmware loading
  1191. * context, which will look for virtio devices supported by the rproc's
  1192. * firmware.
  1193. *
  1194. * If found, those virtio devices will be created and added, so as a result
  1195. * of registering this remote processor, additional virtio drivers might be
  1196. * probed.
  1197. */
  1198. int rproc_register(struct rproc *rproc)
  1199. {
  1200. struct device *dev = rproc->dev;
  1201. int ret = 0;
  1202. /* expose to rproc_get_by_name users */
  1203. klist_add_tail(&rproc->node, &rprocs);
  1204. dev_info(rproc->dev, "%s is available\n", rproc->name);
  1205. dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
  1206. dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
  1207. /* create debugfs entries */
  1208. rproc_create_debug_dir(rproc);
  1209. /* rproc_unregister() calls must wait until async loader completes */
  1210. init_completion(&rproc->firmware_loading_complete);
  1211. /*
  1212. * We must retrieve early virtio configuration info from
  1213. * the firmware (e.g. whether to register a virtio device,
  1214. * what virtio features does it support, ...).
  1215. *
  1216. * We're initiating an asynchronous firmware loading, so we can
  1217. * be built-in kernel code, without hanging the boot process.
  1218. */
  1219. ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
  1220. rproc->firmware, dev, GFP_KERNEL,
  1221. rproc, rproc_fw_config_virtio);
  1222. if (ret < 0) {
  1223. dev_err(dev, "request_firmware_nowait failed: %d\n", ret);
  1224. complete_all(&rproc->firmware_loading_complete);
  1225. klist_remove(&rproc->node);
  1226. }
  1227. return ret;
  1228. }
  1229. EXPORT_SYMBOL(rproc_register);
  1230. /**
  1231. * rproc_alloc() - allocate a remote processor handle
  1232. * @dev: the underlying device
  1233. * @name: name of this remote processor
  1234. * @ops: platform-specific handlers (mainly start/stop)
  1235. * @firmware: name of firmware file to load
  1236. * @len: length of private data needed by the rproc driver (in bytes)
  1237. *
  1238. * Allocates a new remote processor handle, but does not register
  1239. * it yet.
  1240. *
  1241. * This function should be used by rproc implementations during initialization
  1242. * of the remote processor.
  1243. *
  1244. * After creating an rproc handle using this function, and when ready,
  1245. * implementations should then call rproc_register() to complete
  1246. * the registration of the remote processor.
  1247. *
  1248. * On success the new rproc is returned, and on failure, NULL.
  1249. *
  1250. * Note: _never_ directly deallocate @rproc, even if it was not registered
  1251. * yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free().
  1252. */
  1253. struct rproc *rproc_alloc(struct device *dev, const char *name,
  1254. const struct rproc_ops *ops,
  1255. const char *firmware, int len)
  1256. {
  1257. struct rproc *rproc;
  1258. if (!dev || !name || !ops)
  1259. return NULL;
  1260. rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
  1261. if (!rproc) {
  1262. dev_err(dev, "%s: kzalloc failed\n", __func__);
  1263. return NULL;
  1264. }
  1265. rproc->dev = dev;
  1266. rproc->name = name;
  1267. rproc->ops = ops;
  1268. rproc->firmware = firmware;
  1269. rproc->priv = &rproc[1];
  1270. atomic_set(&rproc->power, 0);
  1271. kref_init(&rproc->refcount);
  1272. mutex_init(&rproc->lock);
  1273. idr_init(&rproc->notifyids);
  1274. INIT_LIST_HEAD(&rproc->carveouts);
  1275. INIT_LIST_HEAD(&rproc->mappings);
  1276. INIT_LIST_HEAD(&rproc->traces);
  1277. INIT_LIST_HEAD(&rproc->rvdevs);
  1278. rproc->state = RPROC_OFFLINE;
  1279. return rproc;
  1280. }
  1281. EXPORT_SYMBOL(rproc_alloc);
  1282. /**
  1283. * rproc_free() - free an rproc handle that was allocated by rproc_alloc
  1284. * @rproc: the remote processor handle
  1285. *
  1286. * This function should _only_ be used if @rproc was only allocated,
  1287. * but not registered yet.
  1288. *
  1289. * If @rproc was already successfully registered (by calling rproc_register()),
  1290. * then use rproc_unregister() instead.
  1291. */
  1292. void rproc_free(struct rproc *rproc)
  1293. {
  1294. idr_remove_all(&rproc->notifyids);
  1295. idr_destroy(&rproc->notifyids);
  1296. kfree(rproc);
  1297. }
  1298. EXPORT_SYMBOL(rproc_free);
  1299. /**
  1300. * rproc_unregister() - unregister a remote processor
  1301. * @rproc: rproc handle to unregister
  1302. *
  1303. * Unregisters a remote processor, and decrements its refcount.
  1304. * If its refcount drops to zero, then @rproc will be freed. If not,
  1305. * it will be freed later once the last reference is dropped.
  1306. *
  1307. * This function should be called when the platform specific rproc
  1308. * implementation decides to remove the rproc device. it should
  1309. * _only_ be called if a previous invocation of rproc_register()
  1310. * has completed successfully.
  1311. *
  1312. * After rproc_unregister() returns, @rproc is _not_ valid anymore and
  1313. * it shouldn't be used. More specifically, don't call rproc_free()
  1314. * or try to directly free @rproc after rproc_unregister() returns;
  1315. * none of these are needed, and calling them is a bug.
  1316. *
  1317. * Returns 0 on success and -EINVAL if @rproc isn't valid.
  1318. */
  1319. int rproc_unregister(struct rproc *rproc)
  1320. {
  1321. struct rproc_vdev *rvdev;
  1322. if (!rproc)
  1323. return -EINVAL;
  1324. /* if rproc is just being registered, wait */
  1325. wait_for_completion(&rproc->firmware_loading_complete);
  1326. /* clean up remote vdev entries */
  1327. list_for_each_entry(rvdev, &rproc->rvdevs, node)
  1328. rproc_remove_virtio_dev(rvdev);
  1329. /* the rproc is downref'ed as soon as it's removed from the klist */
  1330. klist_del(&rproc->node);
  1331. /* the rproc will only be released after its refcount drops to zero */
  1332. kref_put(&rproc->refcount, rproc_release);
  1333. return 0;
  1334. }
  1335. EXPORT_SYMBOL(rproc_unregister);
  1336. static int __init remoteproc_init(void)
  1337. {
  1338. rproc_init_debugfs();
  1339. return 0;
  1340. }
  1341. module_init(remoteproc_init);
  1342. static void __exit remoteproc_exit(void)
  1343. {
  1344. rproc_exit_debugfs();
  1345. }
  1346. module_exit(remoteproc_exit);
  1347. MODULE_LICENSE("GPL v2");
  1348. MODULE_DESCRIPTION("Generic Remote Processor Framework");