remoteproc_core.c 56 KB

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