remoteproc_core.c 43 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/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)
  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);
  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%x\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. mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
  554. if (!mapping) {
  555. dev_err(dev, "kzalloc mapping failed\n");
  556. return -ENOMEM;
  557. }
  558. carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
  559. if (!carveout) {
  560. dev_err(dev, "kzalloc carveout failed\n");
  561. ret = -ENOMEM;
  562. goto free_mapping;
  563. }
  564. va = dma_alloc_coherent(dev, rsc->len, &dma, GFP_KERNEL);
  565. if (!va) {
  566. dev_err(dev, "failed to dma alloc carveout: %d\n", rsc->len);
  567. ret = -ENOMEM;
  568. goto free_carv;
  569. }
  570. dev_dbg(dev, "carveout va %p, dma %x, len 0x%x\n", va, dma, rsc->len);
  571. /*
  572. * Ok, this is non-standard.
  573. *
  574. * Sometimes we can't rely on the generic iommu-based DMA API
  575. * to dynamically allocate the device address and then set the IOMMU
  576. * tables accordingly, because some remote processors might
  577. * _require_ us to use hard coded device addresses that their
  578. * firmware was compiled with.
  579. *
  580. * In this case, we must use the IOMMU API directly and map
  581. * the memory to the device address as expected by the remote
  582. * processor.
  583. *
  584. * Obviously such remote processor devices should not be configured
  585. * to use the iommu-based DMA API: we expect 'dma' to contain the
  586. * physical address in this case.
  587. */
  588. if (rproc->domain) {
  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 dma_free;
  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. dma_free:
  628. dma_free_coherent(dev, rsc->len, va, dma);
  629. free_carv:
  630. kfree(carveout);
  631. free_mapping:
  632. kfree(mapping);
  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_handle_resources() - find and handle 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. * @handler: function that should be used to handle the resource table
  708. *
  709. * This function finds the resource table inside the remote processor's
  710. * firmware, and invoke a user-supplied handler with it (we have two
  711. * possible handlers: one is invoked upon registration of @rproc,
  712. * in order to register the supported virito devices, and the other is
  713. * invoked when @rproc is actually booted).
  714. *
  715. * Currently this function fails if a resource table doesn't exist.
  716. * This restriction will be removed when we'll start supporting remote
  717. * processors that don't need a resource table.
  718. */
  719. static int rproc_handle_resources(struct rproc *rproc, const u8 *elf_data,
  720. size_t len, rproc_handle_resources_t handler)
  721. {
  722. struct elf32_hdr *ehdr;
  723. struct elf32_shdr *shdr;
  724. const char *name_table;
  725. struct device *dev = rproc->dev;
  726. int i, ret = -EINVAL;
  727. struct resource_table *table;
  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 -EINVAL;
  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 -EINVAL;
  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 -EINVAL;
  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 -EINVAL;
  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 -EINVAL;
  763. }
  764. ret = handler(rproc, table, shdr->sh_size);
  765. break;
  766. }
  767. return ret;
  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/%u\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. int ret;
  864. ret = rproc_fw_sanity_check(rproc, fw);
  865. if (ret)
  866. return ret;
  867. ehdr = (struct elf32_hdr *)fw->data;
  868. dev_info(dev, "Booting fw image %s, size %d\n", name, fw->size);
  869. /*
  870. * if enabling an IOMMU isn't relevant for this rproc, this is
  871. * just a nop
  872. */
  873. ret = rproc_enable_iommu(rproc);
  874. if (ret) {
  875. dev_err(dev, "can't enable iommu: %d\n", ret);
  876. return ret;
  877. }
  878. /*
  879. * The ELF entry point is the rproc's boot addr (though this is not
  880. * a configurable property of all remote processors: some will always
  881. * boot at a specific hardcoded address).
  882. */
  883. rproc->bootaddr = ehdr->e_entry;
  884. /* handle fw resources which are required to boot rproc */
  885. ret = rproc_handle_resources(rproc, fw->data, fw->size,
  886. rproc_handle_boot_rsc);
  887. if (ret) {
  888. dev_err(dev, "Failed to process resources: %d\n", ret);
  889. goto clean_up;
  890. }
  891. /* load the ELF segments to memory */
  892. ret = rproc_load_segments(rproc, fw->data, fw->size);
  893. if (ret) {
  894. dev_err(dev, "Failed to load program segments: %d\n", ret);
  895. goto clean_up;
  896. }
  897. /* power up the remote processor */
  898. ret = rproc->ops->start(rproc);
  899. if (ret) {
  900. dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
  901. goto clean_up;
  902. }
  903. rproc->state = RPROC_RUNNING;
  904. dev_info(dev, "remote processor %s is now up\n", rproc->name);
  905. return 0;
  906. clean_up:
  907. rproc_resource_cleanup(rproc);
  908. rproc_disable_iommu(rproc);
  909. return ret;
  910. }
  911. /*
  912. * take a firmware and look for virtio devices to register.
  913. *
  914. * Note: this function is called asynchronously upon registration of the
  915. * remote processor (so we must wait until it completes before we try
  916. * to unregister the device. one other option is just to use kref here,
  917. * that might be cleaner).
  918. */
  919. static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
  920. {
  921. struct rproc *rproc = context;
  922. struct device *dev = rproc->dev;
  923. int ret;
  924. if (rproc_fw_sanity_check(rproc, fw) < 0)
  925. goto out;
  926. /* does the fw support any virtio devices ? */
  927. ret = rproc_handle_resources(rproc, fw->data, fw->size,
  928. rproc_handle_virtio_rsc);
  929. if (ret) {
  930. dev_info(dev, "No fw virtio device was found\n");
  931. goto out;
  932. }
  933. out:
  934. if (fw)
  935. release_firmware(fw);
  936. /* allow rproc_unregister() contexts, if any, to proceed */
  937. complete_all(&rproc->firmware_loading_complete);
  938. }
  939. /**
  940. * rproc_boot() - boot a remote processor
  941. * @rproc: handle of a remote processor
  942. *
  943. * Boot a remote processor (i.e. load its firmware, power it on, ...).
  944. *
  945. * If the remote processor is already powered on, this function immediately
  946. * returns (successfully).
  947. *
  948. * Returns 0 on success, and an appropriate error value otherwise.
  949. */
  950. int rproc_boot(struct rproc *rproc)
  951. {
  952. const struct firmware *firmware_p;
  953. struct device *dev;
  954. int ret;
  955. if (!rproc) {
  956. pr_err("invalid rproc handle\n");
  957. return -EINVAL;
  958. }
  959. dev = rproc->dev;
  960. ret = mutex_lock_interruptible(&rproc->lock);
  961. if (ret) {
  962. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  963. return ret;
  964. }
  965. /* loading a firmware is required */
  966. if (!rproc->firmware) {
  967. dev_err(dev, "%s: no firmware to load\n", __func__);
  968. ret = -EINVAL;
  969. goto unlock_mutex;
  970. }
  971. /* prevent underlying implementation from being removed */
  972. if (!try_module_get(dev->driver->owner)) {
  973. dev_err(dev, "%s: can't get owner\n", __func__);
  974. ret = -EINVAL;
  975. goto unlock_mutex;
  976. }
  977. /* skip the boot process if rproc is already powered up */
  978. if (atomic_inc_return(&rproc->power) > 1) {
  979. ret = 0;
  980. goto unlock_mutex;
  981. }
  982. dev_info(dev, "powering up %s\n", rproc->name);
  983. /* load firmware */
  984. ret = request_firmware(&firmware_p, rproc->firmware, dev);
  985. if (ret < 0) {
  986. dev_err(dev, "request_firmware failed: %d\n", ret);
  987. goto downref_rproc;
  988. }
  989. ret = rproc_fw_boot(rproc, firmware_p);
  990. release_firmware(firmware_p);
  991. downref_rproc:
  992. if (ret) {
  993. module_put(dev->driver->owner);
  994. atomic_dec(&rproc->power);
  995. }
  996. unlock_mutex:
  997. mutex_unlock(&rproc->lock);
  998. return ret;
  999. }
  1000. EXPORT_SYMBOL(rproc_boot);
  1001. /**
  1002. * rproc_shutdown() - power off the remote processor
  1003. * @rproc: the remote processor
  1004. *
  1005. * Power off a remote processor (previously booted with rproc_boot()).
  1006. *
  1007. * In case @rproc is still being used by an additional user(s), then
  1008. * this function will just decrement the power refcount and exit,
  1009. * without really powering off the device.
  1010. *
  1011. * Every call to rproc_boot() must (eventually) be accompanied by a call
  1012. * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
  1013. *
  1014. * Notes:
  1015. * - we're not decrementing the rproc's refcount, only the power refcount.
  1016. * which means that the @rproc handle stays valid even after rproc_shutdown()
  1017. * returns, and users can still use it with a subsequent rproc_boot(), if
  1018. * needed.
  1019. * - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly
  1020. * because rproc_shutdown() _does not_ decrement the refcount of @rproc.
  1021. * To decrement the refcount of @rproc, use rproc_put() (but _only_ if
  1022. * you acquired @rproc using rproc_get_by_name()).
  1023. */
  1024. void rproc_shutdown(struct rproc *rproc)
  1025. {
  1026. struct device *dev = rproc->dev;
  1027. int ret;
  1028. ret = mutex_lock_interruptible(&rproc->lock);
  1029. if (ret) {
  1030. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  1031. return;
  1032. }
  1033. /* if the remote proc is still needed, bail out */
  1034. if (!atomic_dec_and_test(&rproc->power))
  1035. goto out;
  1036. /* power off the remote processor */
  1037. ret = rproc->ops->stop(rproc);
  1038. if (ret) {
  1039. atomic_inc(&rproc->power);
  1040. dev_err(dev, "can't stop rproc: %d\n", ret);
  1041. goto out;
  1042. }
  1043. /* clean up all acquired resources */
  1044. rproc_resource_cleanup(rproc);
  1045. rproc_disable_iommu(rproc);
  1046. rproc->state = RPROC_OFFLINE;
  1047. dev_info(dev, "stopped remote processor %s\n", rproc->name);
  1048. out:
  1049. mutex_unlock(&rproc->lock);
  1050. if (!ret)
  1051. module_put(dev->driver->owner);
  1052. }
  1053. EXPORT_SYMBOL(rproc_shutdown);
  1054. /**
  1055. * rproc_release() - completely deletes the existence of a remote processor
  1056. * @kref: the rproc's kref
  1057. *
  1058. * This function should _never_ be called directly.
  1059. *
  1060. * The only reasonable location to use it is as an argument when kref_put'ing
  1061. * @rproc's refcount.
  1062. *
  1063. * This way it will be called when no one holds a valid pointer to this @rproc
  1064. * anymore (and obviously after it is removed from the rprocs klist).
  1065. *
  1066. * Note: this function is not static because rproc_vdev_release() needs it when
  1067. * it decrements @rproc's refcount.
  1068. */
  1069. void rproc_release(struct kref *kref)
  1070. {
  1071. struct rproc *rproc = container_of(kref, struct rproc, refcount);
  1072. struct rproc_vdev *rvdev, *rvtmp;
  1073. dev_info(rproc->dev, "removing %s\n", rproc->name);
  1074. rproc_delete_debug_dir(rproc);
  1075. /* clean up remote vdev entries */
  1076. list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) {
  1077. __rproc_free_vrings(rvdev, RVDEV_NUM_VRINGS);
  1078. list_del(&rvdev->node);
  1079. }
  1080. /*
  1081. * At this point no one holds a reference to rproc anymore,
  1082. * so we can directly unroll rproc_alloc()
  1083. */
  1084. rproc_free(rproc);
  1085. }
  1086. /* will be called when an rproc is added to the rprocs klist */
  1087. static void klist_rproc_get(struct klist_node *n)
  1088. {
  1089. struct rproc *rproc = container_of(n, struct rproc, node);
  1090. kref_get(&rproc->refcount);
  1091. }
  1092. /* will be called when an rproc is removed from the rprocs klist */
  1093. static void klist_rproc_put(struct klist_node *n)
  1094. {
  1095. struct rproc *rproc = container_of(n, struct rproc, node);
  1096. kref_put(&rproc->refcount, rproc_release);
  1097. }
  1098. static struct rproc *next_rproc(struct klist_iter *i)
  1099. {
  1100. struct klist_node *n;
  1101. n = klist_next(i);
  1102. if (!n)
  1103. return NULL;
  1104. return container_of(n, struct rproc, node);
  1105. }
  1106. /**
  1107. * rproc_get_by_name() - find a remote processor by name and boot it
  1108. * @name: name of the remote processor
  1109. *
  1110. * Finds an rproc handle using the remote processor's name, and then
  1111. * boot it. If it's already powered on, then just immediately return
  1112. * (successfully).
  1113. *
  1114. * Returns the rproc handle on success, and NULL on failure.
  1115. *
  1116. * This function increments the remote processor's refcount, so always
  1117. * use rproc_put() to decrement it back once rproc isn't needed anymore.
  1118. *
  1119. * Note: currently this function (and its counterpart rproc_put()) are not
  1120. * being used. We need to scrutinize the use cases
  1121. * that still need them, and see if we can migrate them to use the non
  1122. * name-based boot/shutdown interface.
  1123. */
  1124. struct rproc *rproc_get_by_name(const char *name)
  1125. {
  1126. struct rproc *rproc;
  1127. struct klist_iter i;
  1128. int ret;
  1129. /* find the remote processor, and upref its refcount */
  1130. klist_iter_init(&rprocs, &i);
  1131. while ((rproc = next_rproc(&i)) != NULL)
  1132. if (!strcmp(rproc->name, name)) {
  1133. kref_get(&rproc->refcount);
  1134. break;
  1135. }
  1136. klist_iter_exit(&i);
  1137. /* can't find this rproc ? */
  1138. if (!rproc) {
  1139. pr_err("can't find remote processor %s\n", name);
  1140. return NULL;
  1141. }
  1142. ret = rproc_boot(rproc);
  1143. if (ret < 0) {
  1144. kref_put(&rproc->refcount, rproc_release);
  1145. return NULL;
  1146. }
  1147. return rproc;
  1148. }
  1149. EXPORT_SYMBOL(rproc_get_by_name);
  1150. /**
  1151. * rproc_put() - decrement the refcount of a remote processor, and shut it down
  1152. * @rproc: the remote processor
  1153. *
  1154. * This function tries to shutdown @rproc, and it then decrements its
  1155. * refcount.
  1156. *
  1157. * After this function returns, @rproc may _not_ be used anymore, and its
  1158. * handle should be considered invalid.
  1159. *
  1160. * This function should be called _iff_ the @rproc handle was grabbed by
  1161. * calling rproc_get_by_name().
  1162. */
  1163. void rproc_put(struct rproc *rproc)
  1164. {
  1165. /* try to power off the remote processor */
  1166. rproc_shutdown(rproc);
  1167. /* downref rproc's refcount */
  1168. kref_put(&rproc->refcount, rproc_release);
  1169. }
  1170. EXPORT_SYMBOL(rproc_put);
  1171. /**
  1172. * rproc_register() - register a remote processor
  1173. * @rproc: the remote processor handle to register
  1174. *
  1175. * Registers @rproc with the remoteproc framework, after it has been
  1176. * allocated with rproc_alloc().
  1177. *
  1178. * This is called by the platform-specific rproc implementation, whenever
  1179. * a new remote processor device is probed.
  1180. *
  1181. * Returns 0 on success and an appropriate error code otherwise.
  1182. *
  1183. * Note: this function initiates an asynchronous firmware loading
  1184. * context, which will look for virtio devices supported by the rproc's
  1185. * firmware.
  1186. *
  1187. * If found, those virtio devices will be created and added, so as a result
  1188. * of registering this remote processor, additional virtio drivers might be
  1189. * probed.
  1190. */
  1191. int rproc_register(struct rproc *rproc)
  1192. {
  1193. struct device *dev = rproc->dev;
  1194. int ret = 0;
  1195. /* expose to rproc_get_by_name users */
  1196. klist_add_tail(&rproc->node, &rprocs);
  1197. dev_info(rproc->dev, "%s is available\n", rproc->name);
  1198. dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
  1199. dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
  1200. /* create debugfs entries */
  1201. rproc_create_debug_dir(rproc);
  1202. /* rproc_unregister() calls must wait until async loader completes */
  1203. init_completion(&rproc->firmware_loading_complete);
  1204. /*
  1205. * We must retrieve early virtio configuration info from
  1206. * the firmware (e.g. whether to register a virtio device,
  1207. * what virtio features does it support, ...).
  1208. *
  1209. * We're initiating an asynchronous firmware loading, so we can
  1210. * be built-in kernel code, without hanging the boot process.
  1211. */
  1212. ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
  1213. rproc->firmware, dev, GFP_KERNEL,
  1214. rproc, rproc_fw_config_virtio);
  1215. if (ret < 0) {
  1216. dev_err(dev, "request_firmware_nowait failed: %d\n", ret);
  1217. complete_all(&rproc->firmware_loading_complete);
  1218. klist_remove(&rproc->node);
  1219. }
  1220. return ret;
  1221. }
  1222. EXPORT_SYMBOL(rproc_register);
  1223. /**
  1224. * rproc_alloc() - allocate a remote processor handle
  1225. * @dev: the underlying device
  1226. * @name: name of this remote processor
  1227. * @ops: platform-specific handlers (mainly start/stop)
  1228. * @firmware: name of firmware file to load
  1229. * @len: length of private data needed by the rproc driver (in bytes)
  1230. *
  1231. * Allocates a new remote processor handle, but does not register
  1232. * it yet.
  1233. *
  1234. * This function should be used by rproc implementations during initialization
  1235. * of the remote processor.
  1236. *
  1237. * After creating an rproc handle using this function, and when ready,
  1238. * implementations should then call rproc_register() to complete
  1239. * the registration of the remote processor.
  1240. *
  1241. * On success the new rproc is returned, and on failure, NULL.
  1242. *
  1243. * Note: _never_ directly deallocate @rproc, even if it was not registered
  1244. * yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free().
  1245. */
  1246. struct rproc *rproc_alloc(struct device *dev, const char *name,
  1247. const struct rproc_ops *ops,
  1248. const char *firmware, int len)
  1249. {
  1250. struct rproc *rproc;
  1251. if (!dev || !name || !ops)
  1252. return NULL;
  1253. rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
  1254. if (!rproc) {
  1255. dev_err(dev, "%s: kzalloc failed\n", __func__);
  1256. return NULL;
  1257. }
  1258. rproc->dev = dev;
  1259. rproc->name = name;
  1260. rproc->ops = ops;
  1261. rproc->firmware = firmware;
  1262. rproc->priv = &rproc[1];
  1263. atomic_set(&rproc->power, 0);
  1264. kref_init(&rproc->refcount);
  1265. mutex_init(&rproc->lock);
  1266. idr_init(&rproc->notifyids);
  1267. INIT_LIST_HEAD(&rproc->carveouts);
  1268. INIT_LIST_HEAD(&rproc->mappings);
  1269. INIT_LIST_HEAD(&rproc->traces);
  1270. INIT_LIST_HEAD(&rproc->rvdevs);
  1271. rproc->state = RPROC_OFFLINE;
  1272. return rproc;
  1273. }
  1274. EXPORT_SYMBOL(rproc_alloc);
  1275. /**
  1276. * rproc_free() - free an rproc handle that was allocated by rproc_alloc
  1277. * @rproc: the remote processor handle
  1278. *
  1279. * This function should _only_ be used if @rproc was only allocated,
  1280. * but not registered yet.
  1281. *
  1282. * If @rproc was already successfully registered (by calling rproc_register()),
  1283. * then use rproc_unregister() instead.
  1284. */
  1285. void rproc_free(struct rproc *rproc)
  1286. {
  1287. idr_remove_all(&rproc->notifyids);
  1288. idr_destroy(&rproc->notifyids);
  1289. kfree(rproc);
  1290. }
  1291. EXPORT_SYMBOL(rproc_free);
  1292. /**
  1293. * rproc_unregister() - unregister a remote processor
  1294. * @rproc: rproc handle to unregister
  1295. *
  1296. * Unregisters a remote processor, and decrements its refcount.
  1297. * If its refcount drops to zero, then @rproc will be freed. If not,
  1298. * it will be freed later once the last reference is dropped.
  1299. *
  1300. * This function should be called when the platform specific rproc
  1301. * implementation decides to remove the rproc device. it should
  1302. * _only_ be called if a previous invocation of rproc_register()
  1303. * has completed successfully.
  1304. *
  1305. * After rproc_unregister() returns, @rproc is _not_ valid anymore and
  1306. * it shouldn't be used. More specifically, don't call rproc_free()
  1307. * or try to directly free @rproc after rproc_unregister() returns;
  1308. * none of these are needed, and calling them is a bug.
  1309. *
  1310. * Returns 0 on success and -EINVAL if @rproc isn't valid.
  1311. */
  1312. int rproc_unregister(struct rproc *rproc)
  1313. {
  1314. struct rproc_vdev *rvdev;
  1315. if (!rproc)
  1316. return -EINVAL;
  1317. /* if rproc is just being registered, wait */
  1318. wait_for_completion(&rproc->firmware_loading_complete);
  1319. /* clean up remote vdev entries */
  1320. list_for_each_entry(rvdev, &rproc->rvdevs, node)
  1321. rproc_remove_virtio_dev(rvdev);
  1322. /* the rproc is downref'ed as soon as it's removed from the klist */
  1323. klist_del(&rproc->node);
  1324. /* the rproc will only be released after its refcount drops to zero */
  1325. kref_put(&rproc->refcount, rproc_release);
  1326. return 0;
  1327. }
  1328. EXPORT_SYMBOL(rproc_unregister);
  1329. static int __init remoteproc_init(void)
  1330. {
  1331. rproc_init_debugfs();
  1332. return 0;
  1333. }
  1334. module_init(remoteproc_init);
  1335. static void __exit remoteproc_exit(void)
  1336. {
  1337. rproc_exit_debugfs();
  1338. }
  1339. module_exit(remoteproc_exit);
  1340. MODULE_LICENSE("GPL v2");
  1341. MODULE_DESCRIPTION("Generic Remote Processor Framework");