remoteproc_core.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Remote Processor Framework
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Copyright (C) 2011 Google, Inc.
  7. *
  8. * Ohad Ben-Cohen <ohad@wizery.com>
  9. * Brian Swetland <swetland@google.com>
  10. * Mark Grosen <mgrosen@ti.com>
  11. * Fernando Guzman Lugo <fernando.lugo@ti.com>
  12. * Suman Anna <s-anna@ti.com>
  13. * Robert Tivy <rtivy@ti.com>
  14. * Armando Uribe De Leon <x0095078@ti.com>
  15. */
  16. #define pr_fmt(fmt) "%s: " fmt, __func__
  17. #include <asm/byteorder.h>
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/elf.h>
  22. #include <linux/firmware.h>
  23. #include <linux/idr.h>
  24. #include <linux/iommu.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/panic_notifier.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/rculist.h>
  32. #include <linux/remoteproc.h>
  33. #include <linux/slab.h>
  34. #include <linux/string.h>
  35. #include <linux/virtio_ring.h>
  36. #include "remoteproc_internal.h"
  37. #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL
  38. static DEFINE_MUTEX(rproc_list_mutex);
  39. static LIST_HEAD(rproc_list);
  40. static struct notifier_block rproc_panic_nb;
  41. typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
  42. void *, int offset, int avail);
  43. static int rproc_alloc_carveout(struct rproc *rproc,
  44. struct rproc_mem_entry *mem);
  45. static int rproc_release_carveout(struct rproc *rproc,
  46. struct rproc_mem_entry *mem);
  47. /* Unique indices for remoteproc devices */
  48. static DEFINE_IDA(rproc_dev_index);
  49. static struct workqueue_struct *rproc_recovery_wq;
  50. static const char * const rproc_crash_names[] = {
  51. [RPROC_MMUFAULT] = "mmufault",
  52. [RPROC_WATCHDOG] = "watchdog",
  53. [RPROC_FATAL_ERROR] = "fatal error",
  54. };
  55. /* translate rproc_crash_type to string */
  56. static const char *rproc_crash_to_string(enum rproc_crash_type type)
  57. {
  58. if (type < ARRAY_SIZE(rproc_crash_names))
  59. return rproc_crash_names[type];
  60. return "unknown";
  61. }
  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. static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
  71. unsigned long iova, int flags, void *token)
  72. {
  73. struct rproc *rproc = token;
  74. dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
  75. rproc_report_crash(rproc, RPROC_MMUFAULT);
  76. /*
  77. * Let the iommu core know we're not really handling this fault;
  78. * we just used it as a recovery trigger.
  79. */
  80. return -ENOSYS;
  81. }
  82. static int rproc_enable_iommu(struct rproc *rproc)
  83. {
  84. struct iommu_domain *domain;
  85. struct device *dev = rproc->dev.parent;
  86. int ret;
  87. if (!rproc->has_iommu) {
  88. dev_dbg(dev, "iommu not present\n");
  89. return 0;
  90. }
  91. domain = iommu_paging_domain_alloc(dev);
  92. if (IS_ERR(domain)) {
  93. dev_err(dev, "can't alloc iommu domain\n");
  94. return PTR_ERR(domain);
  95. }
  96. iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
  97. ret = iommu_attach_device(domain, dev);
  98. if (ret) {
  99. dev_err(dev, "can't attach iommu device: %d\n", ret);
  100. goto free_domain;
  101. }
  102. rproc->domain = domain;
  103. return 0;
  104. free_domain:
  105. iommu_domain_free(domain);
  106. return ret;
  107. }
  108. static void rproc_disable_iommu(struct rproc *rproc)
  109. {
  110. struct iommu_domain *domain = rproc->domain;
  111. struct device *dev = rproc->dev.parent;
  112. if (!domain)
  113. return;
  114. iommu_detach_device(domain, dev);
  115. iommu_domain_free(domain);
  116. }
  117. phys_addr_t rproc_va_to_pa(void *cpu_addr)
  118. {
  119. /*
  120. * Return physical address according to virtual address location
  121. * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent
  122. * - in kernel: if region allocated in generic dma memory pool
  123. */
  124. if (is_vmalloc_addr(cpu_addr)) {
  125. return page_to_phys(vmalloc_to_page(cpu_addr)) +
  126. offset_in_page(cpu_addr);
  127. }
  128. WARN_ON(!virt_addr_valid(cpu_addr));
  129. return virt_to_phys(cpu_addr);
  130. }
  131. /**
  132. * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
  133. * @rproc: handle of a remote processor
  134. * @da: remoteproc device address to translate
  135. * @len: length of the memory region @da is pointing to
  136. * @is_iomem: optional pointer filled in to indicate if @da is iomapped memory
  137. *
  138. * Some remote processors will ask us to allocate them physically contiguous
  139. * memory regions (which we call "carveouts"), and map them to specific
  140. * device addresses (which are hardcoded in the firmware). They may also have
  141. * dedicated memory regions internal to the processors, and use them either
  142. * exclusively or alongside carveouts.
  143. *
  144. * They may then ask us to copy objects into specific device addresses (e.g.
  145. * code/data sections) or expose us certain symbols in other device address
  146. * (e.g. their trace buffer).
  147. *
  148. * This function is a helper function with which we can go over the allocated
  149. * carveouts and translate specific device addresses to kernel virtual addresses
  150. * so we can access the referenced memory. This function also allows to perform
  151. * translations on the internal remoteproc memory regions through a platform
  152. * implementation specific da_to_va ops, if present.
  153. *
  154. * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
  155. * but only on kernel direct mapped RAM memory. Instead, we're just using
  156. * here the output of the DMA API for the carveouts, which should be more
  157. * correct.
  158. *
  159. * Return: a valid kernel address on success or NULL on failure
  160. */
  161. void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
  162. {
  163. struct rproc_mem_entry *carveout;
  164. void *ptr = NULL;
  165. if (rproc->ops->da_to_va) {
  166. ptr = rproc->ops->da_to_va(rproc, da, len, is_iomem);
  167. if (ptr)
  168. goto out;
  169. }
  170. list_for_each_entry(carveout, &rproc->carveouts, node) {
  171. int offset = da - carveout->da;
  172. /* Verify that carveout is allocated */
  173. if (!carveout->va)
  174. continue;
  175. /* try next carveout if da is too small */
  176. if (offset < 0)
  177. continue;
  178. /* try next carveout if da is too large */
  179. if (offset + len > carveout->len)
  180. continue;
  181. ptr = carveout->va + offset;
  182. if (is_iomem)
  183. *is_iomem = carveout->is_iomem;
  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 (format string)
  194. * @...: optional parameters matching @name string
  195. *
  196. * Platform driver has the capability to register some pre-allacoted carveout
  197. * (physically contiguous memory regions) before rproc firmware loading and
  198. * associated resource table analysis. These regions may be dedicated memory
  199. * regions internal to the coprocessor or specified DDR region with specific
  200. * attributes
  201. *
  202. * This function is a helper function with which we can go over the
  203. * allocated carveouts and return associated region characteristics like
  204. * coprocessor address, length or processor virtual address.
  205. *
  206. * Return: a valid pointer on carveout entry on success or NULL on failure.
  207. */
  208. __printf(2, 3)
  209. struct rproc_mem_entry *
  210. rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...)
  211. {
  212. va_list args;
  213. char _name[32];
  214. struct rproc_mem_entry *carveout, *mem = NULL;
  215. if (!name)
  216. return NULL;
  217. va_start(args, name);
  218. vsnprintf(_name, sizeof(_name), name, args);
  219. va_end(args);
  220. list_for_each_entry(carveout, &rproc->carveouts, node) {
  221. /* Compare carveout and requested names */
  222. if (!strcmp(carveout->name, _name)) {
  223. mem = carveout;
  224. break;
  225. }
  226. }
  227. return mem;
  228. }
  229. /**
  230. * rproc_check_carveout_da() - Check specified carveout da configuration
  231. * @rproc: handle of a remote processor
  232. * @mem: pointer on carveout to check
  233. * @da: area device address
  234. * @len: associated area size
  235. *
  236. * This function is a helper function to verify requested device area (couple
  237. * da, len) is part of specified carveout.
  238. * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is
  239. * checked.
  240. *
  241. * Return: 0 if carveout matches request else error
  242. */
  243. static int rproc_check_carveout_da(struct rproc *rproc,
  244. struct rproc_mem_entry *mem, u32 da, u32 len)
  245. {
  246. struct device *dev = &rproc->dev;
  247. int delta;
  248. /* Check requested resource length */
  249. if (len > mem->len) {
  250. dev_err(dev, "Registered carveout doesn't fit len request\n");
  251. return -EINVAL;
  252. }
  253. if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) {
  254. /* Address doesn't match registered carveout configuration */
  255. return -EINVAL;
  256. } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) {
  257. delta = da - mem->da;
  258. /* Check requested resource belongs to registered carveout */
  259. if (delta < 0) {
  260. dev_err(dev,
  261. "Registered carveout doesn't fit da request\n");
  262. return -EINVAL;
  263. }
  264. if (delta + len > mem->len) {
  265. dev_err(dev,
  266. "Registered carveout doesn't fit len request\n");
  267. return -EINVAL;
  268. }
  269. }
  270. return 0;
  271. }
  272. int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
  273. {
  274. struct rproc *rproc = rvdev->rproc;
  275. struct device *dev = &rproc->dev;
  276. struct rproc_vring *rvring = &rvdev->vring[i];
  277. struct fw_rsc_vdev *rsc;
  278. int ret, notifyid;
  279. struct rproc_mem_entry *mem;
  280. size_t size;
  281. /* actual size of vring (in bytes) */
  282. size = PAGE_ALIGN(vring_size(rvring->num, rvring->align));
  283. rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
  284. /* Search for pre-registered carveout */
  285. mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
  286. i);
  287. if (mem) {
  288. if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size))
  289. return -ENOMEM;
  290. } else {
  291. /* Register carveout in list */
  292. mem = rproc_mem_entry_init(dev, NULL, 0,
  293. size, rsc->vring[i].da,
  294. rproc_alloc_carveout,
  295. rproc_release_carveout,
  296. "vdev%dvring%d",
  297. rvdev->index, i);
  298. if (!mem) {
  299. dev_err(dev, "Can't allocate memory entry structure\n");
  300. return -ENOMEM;
  301. }
  302. rproc_add_carveout(rproc, mem);
  303. }
  304. /*
  305. * Assign an rproc-wide unique index for this vring
  306. * TODO: assign a notifyid for rvdev updates as well
  307. * TODO: support predefined notifyids (via resource table)
  308. */
  309. ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
  310. if (ret < 0) {
  311. dev_err(dev, "idr_alloc failed: %d\n", ret);
  312. return ret;
  313. }
  314. notifyid = ret;
  315. /* Potentially bump max_notifyid */
  316. if (notifyid > rproc->max_notifyid)
  317. rproc->max_notifyid = notifyid;
  318. rvring->notifyid = notifyid;
  319. /* Let the rproc know the notifyid of this vring.*/
  320. rsc->vring[i].notifyid = notifyid;
  321. return 0;
  322. }
  323. int
  324. rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
  325. {
  326. struct rproc *rproc = rvdev->rproc;
  327. struct device *dev = &rproc->dev;
  328. struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
  329. struct rproc_vring *rvring = &rvdev->vring[i];
  330. dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
  331. i, vring->da, vring->num, vring->align);
  332. /* verify queue size and vring alignment are sane */
  333. if (!vring->num || !vring->align) {
  334. dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
  335. vring->num, vring->align);
  336. return -EINVAL;
  337. }
  338. rvring->num = vring->num;
  339. rvring->align = vring->align;
  340. rvring->rvdev = rvdev;
  341. return 0;
  342. }
  343. void rproc_free_vring(struct rproc_vring *rvring)
  344. {
  345. struct rproc *rproc = rvring->rvdev->rproc;
  346. int idx = rvring - rvring->rvdev->vring;
  347. struct fw_rsc_vdev *rsc;
  348. idr_remove(&rproc->notifyids, rvring->notifyid);
  349. /*
  350. * At this point rproc_stop() has been called and the installed resource
  351. * table in the remote processor memory may no longer be accessible. As
  352. * such and as per rproc_stop(), rproc->table_ptr points to the cached
  353. * resource table (rproc->cached_table). The cached resource table is
  354. * only available when a remote processor has been booted by the
  355. * remoteproc core, otherwise it is NULL.
  356. *
  357. * Based on the above, reset the virtio device section in the cached
  358. * resource table only if there is one to work with.
  359. */
  360. if (rproc->table_ptr) {
  361. rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
  362. rsc->vring[idx].da = 0;
  363. rsc->vring[idx].notifyid = -1;
  364. }
  365. }
  366. void rproc_add_rvdev(struct rproc *rproc, struct rproc_vdev *rvdev)
  367. {
  368. if (rvdev && rproc)
  369. list_add_tail(&rvdev->node, &rproc->rvdevs);
  370. }
  371. void rproc_remove_rvdev(struct rproc_vdev *rvdev)
  372. {
  373. if (rvdev)
  374. list_del(&rvdev->node);
  375. }
  376. /**
  377. * rproc_handle_vdev() - handle a vdev fw resource
  378. * @rproc: the remote processor
  379. * @ptr: the vring resource descriptor
  380. * @offset: offset of the resource entry
  381. * @avail: size of available data (for sanity checking the image)
  382. *
  383. * This resource entry requests the host to statically register a virtio
  384. * device (vdev), and setup everything needed to support it. It contains
  385. * everything needed to make it possible: the virtio device id, virtio
  386. * device features, vrings information, virtio config space, etc...
  387. *
  388. * Before registering the vdev, the vrings are allocated from non-cacheable
  389. * physically contiguous memory. Currently we only support two vrings per
  390. * remote processor (temporary limitation). We might also want to consider
  391. * doing the vring allocation only later when ->find_vqs() is invoked, and
  392. * then release them upon ->del_vqs().
  393. *
  394. * Note: @da is currently not really handled correctly: we dynamically
  395. * allocate it using the DMA API, ignoring requested hard coded addresses,
  396. * and we don't take care of any required IOMMU programming. This is all
  397. * going to be taken care of when the generic iommu-based DMA API will be
  398. * merged. Meanwhile, statically-addressed iommu-based firmware images should
  399. * use RSC_DEVMEM resource entries to map their required @da to the physical
  400. * address of their base CMA region (ouch, hacky!).
  401. *
  402. * Return: 0 on success, or an appropriate error code otherwise
  403. */
  404. static int rproc_handle_vdev(struct rproc *rproc, void *ptr,
  405. int offset, int avail)
  406. {
  407. struct fw_rsc_vdev *rsc = ptr;
  408. struct device *dev = &rproc->dev;
  409. struct rproc_vdev *rvdev;
  410. size_t rsc_size;
  411. struct rproc_vdev_data rvdev_data;
  412. struct platform_device *pdev;
  413. /* make sure resource isn't truncated */
  414. rsc_size = struct_size(rsc, vring, rsc->num_of_vrings);
  415. if (size_add(rsc_size, rsc->config_len) > avail) {
  416. dev_err(dev, "vdev rsc is truncated\n");
  417. return -EINVAL;
  418. }
  419. /* make sure reserved bytes are zeroes */
  420. if (rsc->reserved[0] || rsc->reserved[1]) {
  421. dev_err(dev, "vdev rsc has non zero reserved bytes\n");
  422. return -EINVAL;
  423. }
  424. dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
  425. rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
  426. /* we currently support only two vrings per rvdev */
  427. if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
  428. dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
  429. return -EINVAL;
  430. }
  431. rvdev_data.id = rsc->id;
  432. rvdev_data.index = rproc->nb_vdev++;
  433. rvdev_data.rsc_offset = offset;
  434. rvdev_data.rsc = rsc;
  435. /*
  436. * When there is more than one remote processor, rproc->nb_vdev number is
  437. * same for each separate instances of "rproc". If rvdev_data.index is used
  438. * as device id, then we get duplication in sysfs, so need to use
  439. * PLATFORM_DEVID_AUTO to auto select device id.
  440. */
  441. pdev = platform_device_register_data(dev, "rproc-virtio", PLATFORM_DEVID_AUTO, &rvdev_data,
  442. sizeof(rvdev_data));
  443. if (IS_ERR(pdev)) {
  444. dev_err(dev, "failed to create rproc-virtio device\n");
  445. return PTR_ERR(pdev);
  446. }
  447. return 0;
  448. }
  449. /**
  450. * rproc_handle_trace() - handle a shared trace buffer resource
  451. * @rproc: the remote processor
  452. * @ptr: the trace resource descriptor
  453. * @offset: offset of the resource entry
  454. * @avail: size of available data (for sanity checking the image)
  455. *
  456. * In case the remote processor dumps trace logs into memory,
  457. * export it via debugfs.
  458. *
  459. * Currently, the 'da' member of @rsc should contain the device address
  460. * where the remote processor is dumping the traces. Later we could also
  461. * support dynamically allocating this address using the generic
  462. * DMA API (but currently there isn't a use case for that).
  463. *
  464. * Return: 0 on success, or an appropriate error code otherwise
  465. */
  466. static int rproc_handle_trace(struct rproc *rproc, void *ptr,
  467. int offset, int avail)
  468. {
  469. struct fw_rsc_trace *rsc = ptr;
  470. struct rproc_debug_trace *trace;
  471. struct device *dev = &rproc->dev;
  472. char name[15];
  473. if (sizeof(*rsc) > avail) {
  474. dev_err(dev, "trace rsc is truncated\n");
  475. return -EINVAL;
  476. }
  477. /* make sure reserved bytes are zeroes */
  478. if (rsc->reserved) {
  479. dev_err(dev, "trace rsc has non zero reserved bytes\n");
  480. return -EINVAL;
  481. }
  482. trace = kzalloc_obj(*trace);
  483. if (!trace)
  484. return -ENOMEM;
  485. /* set the trace buffer dma properties */
  486. trace->trace_mem.len = rsc->len;
  487. trace->trace_mem.da = rsc->da;
  488. /* set pointer on rproc device */
  489. trace->rproc = rproc;
  490. /* make sure snprintf always null terminates, even if truncating */
  491. snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
  492. /* create the debugfs entry */
  493. trace->tfile = rproc_create_trace_file(name, rproc, trace);
  494. list_add_tail(&trace->node, &rproc->traces);
  495. rproc->num_traces++;
  496. dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n",
  497. name, rsc->da, rsc->len);
  498. return 0;
  499. }
  500. /**
  501. * rproc_handle_devmem() - handle devmem resource entry
  502. * @rproc: remote processor handle
  503. * @ptr: the devmem resource entry
  504. * @offset: offset of the resource entry
  505. * @avail: size of available data (for sanity checking the image)
  506. *
  507. * Remote processors commonly need to access certain on-chip peripherals.
  508. *
  509. * Some of these remote processors access memory via an iommu device,
  510. * and might require us to configure their iommu before they can access
  511. * the on-chip peripherals they need.
  512. *
  513. * This resource entry is a request to map such a peripheral device.
  514. *
  515. * These devmem entries will contain the physical address of the device in
  516. * the 'pa' member. If a specific device address is expected, then 'da' will
  517. * contain it (currently this is the only use case supported). 'len' will
  518. * contain the size of the physical region we need to map.
  519. *
  520. * Currently we just "trust" those devmem entries to contain valid physical
  521. * addresses, but this is going to change: we want the implementations to
  522. * tell us ranges of physical addresses the firmware is allowed to request,
  523. * and not allow firmwares to request access to physical addresses that
  524. * are outside those ranges.
  525. *
  526. * Return: 0 on success, or an appropriate error code otherwise
  527. */
  528. static int rproc_handle_devmem(struct rproc *rproc, void *ptr,
  529. int offset, int avail)
  530. {
  531. struct fw_rsc_devmem *rsc = ptr;
  532. struct rproc_mem_entry *mapping;
  533. struct device *dev = &rproc->dev;
  534. int ret;
  535. /* no point in handling this resource without a valid iommu domain */
  536. if (!rproc->domain)
  537. return -EINVAL;
  538. if (sizeof(*rsc) > avail) {
  539. dev_err(dev, "devmem rsc is truncated\n");
  540. return -EINVAL;
  541. }
  542. /* make sure reserved bytes are zeroes */
  543. if (rsc->reserved) {
  544. dev_err(dev, "devmem rsc has non zero reserved bytes\n");
  545. return -EINVAL;
  546. }
  547. mapping = kzalloc_obj(*mapping);
  548. if (!mapping)
  549. return -ENOMEM;
  550. ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags,
  551. GFP_KERNEL);
  552. if (ret) {
  553. dev_err(dev, "failed to map devmem: %d\n", ret);
  554. goto out;
  555. }
  556. /*
  557. * We'll need this info later when we'll want to unmap everything
  558. * (e.g. on shutdown).
  559. *
  560. * We can't trust the remote processor not to change the resource
  561. * table, so we must maintain this info independently.
  562. */
  563. mapping->da = rsc->da;
  564. mapping->len = rsc->len;
  565. list_add_tail(&mapping->node, &rproc->mappings);
  566. dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
  567. rsc->pa, rsc->da, rsc->len);
  568. return 0;
  569. out:
  570. kfree(mapping);
  571. return ret;
  572. }
  573. /**
  574. * rproc_alloc_carveout() - allocated specified carveout
  575. * @rproc: rproc handle
  576. * @mem: the memory entry to allocate
  577. *
  578. * This function allocate specified memory entry @mem using
  579. * dma_alloc_coherent() as default allocator
  580. *
  581. * Return: 0 on success, or an appropriate error code otherwise
  582. */
  583. static int rproc_alloc_carveout(struct rproc *rproc,
  584. struct rproc_mem_entry *mem)
  585. {
  586. struct rproc_mem_entry *mapping = NULL;
  587. struct device *dev = &rproc->dev;
  588. dma_addr_t dma;
  589. void *va;
  590. int ret;
  591. va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL);
  592. if (!va) {
  593. dev_err(dev->parent,
  594. "failed to allocate dma memory: len 0x%zx\n",
  595. mem->len);
  596. return -ENOMEM;
  597. }
  598. dev_dbg(dev, "carveout va %p, dma %pad, len 0x%zx\n",
  599. va, &dma, mem->len);
  600. if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) {
  601. /*
  602. * Check requested da is equal to dma address
  603. * and print a warn message in case of missalignment.
  604. * Don't stop rproc_start sequence as coprocessor may
  605. * build pa to da translation on its side.
  606. */
  607. if (mem->da != (u32)dma)
  608. dev_warn(dev->parent,
  609. "Allocated carveout doesn't fit device address request\n");
  610. }
  611. /*
  612. * Ok, this is non-standard.
  613. *
  614. * Sometimes we can't rely on the generic iommu-based DMA API
  615. * to dynamically allocate the device address and then set the IOMMU
  616. * tables accordingly, because some remote processors might
  617. * _require_ us to use hard coded device addresses that their
  618. * firmware was compiled with.
  619. *
  620. * In this case, we must use the IOMMU API directly and map
  621. * the memory to the device address as expected by the remote
  622. * processor.
  623. *
  624. * Obviously such remote processor devices should not be configured
  625. * to use the iommu-based DMA API: we expect 'dma' to contain the
  626. * physical address in this case.
  627. */
  628. if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) {
  629. mapping = kzalloc_obj(*mapping);
  630. if (!mapping) {
  631. ret = -ENOMEM;
  632. goto dma_free;
  633. }
  634. ret = iommu_map(rproc->domain, mem->da, dma, mem->len,
  635. mem->flags, GFP_KERNEL);
  636. if (ret) {
  637. dev_err(dev, "iommu_map failed: %d\n", ret);
  638. goto free_mapping;
  639. }
  640. /*
  641. * We'll need this info later when we'll want to unmap
  642. * everything (e.g. on shutdown).
  643. *
  644. * We can't trust the remote processor not to change the
  645. * resource table, so we must maintain this info independently.
  646. */
  647. mapping->da = mem->da;
  648. mapping->len = mem->len;
  649. list_add_tail(&mapping->node, &rproc->mappings);
  650. dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
  651. mem->da, &dma);
  652. }
  653. if (mem->da == FW_RSC_ADDR_ANY) {
  654. /* Update device address as undefined by requester */
  655. if ((u64)dma & HIGH_BITS_MASK)
  656. dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n");
  657. mem->da = (u32)dma;
  658. }
  659. mem->dma = dma;
  660. mem->va = va;
  661. return 0;
  662. free_mapping:
  663. kfree(mapping);
  664. dma_free:
  665. dma_free_coherent(dev->parent, mem->len, va, dma);
  666. return ret;
  667. }
  668. /**
  669. * rproc_release_carveout() - release acquired carveout
  670. * @rproc: rproc handle
  671. * @mem: the memory entry to release
  672. *
  673. * This function releases specified memory entry @mem allocated via
  674. * rproc_alloc_carveout() function by @rproc.
  675. *
  676. * Return: 0 on success, or an appropriate error code otherwise
  677. */
  678. static int rproc_release_carveout(struct rproc *rproc,
  679. struct rproc_mem_entry *mem)
  680. {
  681. struct device *dev = &rproc->dev;
  682. /* clean up carveout allocations */
  683. dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma);
  684. return 0;
  685. }
  686. /**
  687. * rproc_handle_carveout() - handle phys contig memory allocation requests
  688. * @rproc: rproc handle
  689. * @ptr: the resource entry
  690. * @offset: offset of the resource entry
  691. * @avail: size of available data (for image validation)
  692. *
  693. * This function will handle firmware requests for allocation of physically
  694. * contiguous memory regions.
  695. *
  696. * These request entries should come first in the firmware's resource table,
  697. * as other firmware entries might request placing other data objects inside
  698. * these memory regions (e.g. data/code segments, trace resource entries, ...).
  699. *
  700. * Allocating memory this way helps utilizing the reserved physical memory
  701. * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
  702. * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
  703. * pressure is important; it may have a substantial impact on performance.
  704. *
  705. * Return: 0 on success, or an appropriate error code otherwise
  706. */
  707. static int rproc_handle_carveout(struct rproc *rproc,
  708. void *ptr, int offset, int avail)
  709. {
  710. struct fw_rsc_carveout *rsc = ptr;
  711. struct rproc_mem_entry *carveout;
  712. struct device *dev = &rproc->dev;
  713. if (sizeof(*rsc) > avail) {
  714. dev_err(dev, "carveout rsc is truncated\n");
  715. return -EINVAL;
  716. }
  717. /* make sure reserved bytes are zeroes */
  718. if (rsc->reserved) {
  719. dev_err(dev, "carveout rsc has non zero reserved bytes\n");
  720. return -EINVAL;
  721. }
  722. dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
  723. rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
  724. /*
  725. * Check carveout rsc already part of a registered carveout,
  726. * Search by name, then check the da and length
  727. */
  728. carveout = rproc_find_carveout_by_name(rproc, rsc->name);
  729. if (carveout) {
  730. if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
  731. dev_err(dev,
  732. "Carveout already associated to resource table\n");
  733. return -ENOMEM;
  734. }
  735. if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len))
  736. return -ENOMEM;
  737. /* Update memory carveout with resource table info */
  738. carveout->rsc_offset = offset;
  739. carveout->flags = rsc->flags;
  740. return 0;
  741. }
  742. /* Register carveout in list */
  743. carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
  744. rproc_alloc_carveout,
  745. rproc_release_carveout, rsc->name);
  746. if (!carveout) {
  747. dev_err(dev, "Can't allocate memory entry structure\n");
  748. return -ENOMEM;
  749. }
  750. carveout->flags = rsc->flags;
  751. carveout->rsc_offset = offset;
  752. rproc_add_carveout(rproc, carveout);
  753. return 0;
  754. }
  755. /**
  756. * rproc_add_carveout() - register an allocated carveout region
  757. * @rproc: rproc handle
  758. * @mem: memory entry to register
  759. *
  760. * This function registers specified memory entry in @rproc carveouts list.
  761. * Specified carveout should have been allocated before registering.
  762. */
  763. void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem)
  764. {
  765. list_add_tail(&mem->node, &rproc->carveouts);
  766. }
  767. EXPORT_SYMBOL(rproc_add_carveout);
  768. /**
  769. * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
  770. * @dev: pointer on device struct
  771. * @va: virtual address
  772. * @dma: dma address
  773. * @len: memory carveout length
  774. * @da: device address
  775. * @alloc: memory carveout allocation function
  776. * @release: memory carveout release function
  777. * @name: carveout name
  778. *
  779. * This function allocates a rproc_mem_entry struct and fill it with parameters
  780. * provided by client.
  781. *
  782. * Return: a valid pointer on success, or NULL on failure
  783. */
  784. __printf(8, 9)
  785. struct rproc_mem_entry *
  786. rproc_mem_entry_init(struct device *dev,
  787. void *va, dma_addr_t dma, size_t len, u32 da,
  788. int (*alloc)(struct rproc *, struct rproc_mem_entry *),
  789. int (*release)(struct rproc *, struct rproc_mem_entry *),
  790. const char *name, ...)
  791. {
  792. struct rproc_mem_entry *mem;
  793. va_list args;
  794. mem = kzalloc_obj(*mem);
  795. if (!mem)
  796. return mem;
  797. mem->va = va;
  798. mem->dma = dma;
  799. mem->da = da;
  800. mem->len = len;
  801. mem->alloc = alloc;
  802. mem->release = release;
  803. mem->rsc_offset = FW_RSC_ADDR_ANY;
  804. mem->of_resm_idx = -1;
  805. va_start(args, name);
  806. vsnprintf(mem->name, sizeof(mem->name), name, args);
  807. va_end(args);
  808. return mem;
  809. }
  810. EXPORT_SYMBOL(rproc_mem_entry_init);
  811. /**
  812. * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct
  813. * from a reserved memory phandle
  814. * @dev: pointer on device struct
  815. * @of_resm_idx: reserved memory phandle index in "memory-region"
  816. * @len: memory carveout length
  817. * @da: device address
  818. * @name: carveout name
  819. *
  820. * This function allocates a rproc_mem_entry struct and fill it with parameters
  821. * provided by client.
  822. *
  823. * Return: a valid pointer on success, or NULL on failure
  824. */
  825. __printf(5, 6)
  826. struct rproc_mem_entry *
  827. rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len,
  828. u32 da, const char *name, ...)
  829. {
  830. struct rproc_mem_entry *mem;
  831. va_list args;
  832. mem = kzalloc_obj(*mem);
  833. if (!mem)
  834. return mem;
  835. mem->da = da;
  836. mem->len = len;
  837. mem->rsc_offset = FW_RSC_ADDR_ANY;
  838. mem->of_resm_idx = of_resm_idx;
  839. va_start(args, name);
  840. vsnprintf(mem->name, sizeof(mem->name), name, args);
  841. va_end(args);
  842. return mem;
  843. }
  844. EXPORT_SYMBOL(rproc_of_resm_mem_entry_init);
  845. /**
  846. * rproc_of_parse_firmware() - parse and return the firmware-name
  847. * @dev: pointer on device struct representing a rproc
  848. * @index: index to use for the firmware-name retrieval
  849. * @fw_name: pointer to a character string, in which the firmware
  850. * name is returned on success and unmodified otherwise.
  851. *
  852. * This is an OF helper function that parses a device's DT node for
  853. * the "firmware-name" property and returns the firmware name pointer
  854. * in @fw_name on success.
  855. *
  856. * Return: 0 on success, or an appropriate failure.
  857. */
  858. int rproc_of_parse_firmware(struct device *dev, int index, const char **fw_name)
  859. {
  860. int ret;
  861. ret = of_property_read_string_index(dev->of_node, "firmware-name",
  862. index, fw_name);
  863. return ret ? ret : 0;
  864. }
  865. EXPORT_SYMBOL(rproc_of_parse_firmware);
  866. /*
  867. * A lookup table for resource handlers. The indices are defined in
  868. * enum fw_resource_type.
  869. */
  870. static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
  871. [RSC_CARVEOUT] = rproc_handle_carveout,
  872. [RSC_DEVMEM] = rproc_handle_devmem,
  873. [RSC_TRACE] = rproc_handle_trace,
  874. [RSC_VDEV] = rproc_handle_vdev,
  875. };
  876. /* handle firmware resource entries before booting the remote processor */
  877. static int rproc_handle_resources(struct rproc *rproc,
  878. rproc_handle_resource_t handlers[RSC_LAST])
  879. {
  880. struct device *dev = &rproc->dev;
  881. rproc_handle_resource_t handler;
  882. int ret = 0, i;
  883. if (!rproc->table_ptr)
  884. return 0;
  885. for (i = 0; i < rproc->table_ptr->num; i++) {
  886. int offset = rproc->table_ptr->offset[i];
  887. struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
  888. int avail = rproc->table_sz - offset - sizeof(*hdr);
  889. void *rsc = (void *)hdr + sizeof(*hdr);
  890. /* make sure table isn't truncated */
  891. if (avail < 0) {
  892. dev_err(dev, "rsc table is truncated\n");
  893. return -EINVAL;
  894. }
  895. dev_dbg(dev, "rsc: type %d\n", hdr->type);
  896. if (hdr->type >= RSC_VENDOR_START &&
  897. hdr->type <= RSC_VENDOR_END) {
  898. ret = rproc_handle_rsc(rproc, hdr->type, rsc,
  899. offset + sizeof(*hdr), avail);
  900. if (ret == RSC_HANDLED)
  901. continue;
  902. else if (ret < 0)
  903. break;
  904. dev_warn(dev, "unsupported vendor resource %d\n",
  905. hdr->type);
  906. continue;
  907. }
  908. if (hdr->type >= RSC_LAST) {
  909. dev_warn(dev, "unsupported resource %d\n", hdr->type);
  910. continue;
  911. }
  912. handler = handlers[hdr->type];
  913. if (!handler)
  914. continue;
  915. ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
  916. if (ret)
  917. break;
  918. }
  919. return ret;
  920. }
  921. static int rproc_prepare_subdevices(struct rproc *rproc)
  922. {
  923. struct rproc_subdev *subdev;
  924. int ret;
  925. list_for_each_entry(subdev, &rproc->subdevs, node) {
  926. if (subdev->prepare) {
  927. ret = subdev->prepare(subdev);
  928. if (ret)
  929. goto unroll_preparation;
  930. }
  931. }
  932. return 0;
  933. unroll_preparation:
  934. list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
  935. if (subdev->unprepare)
  936. subdev->unprepare(subdev);
  937. }
  938. return ret;
  939. }
  940. static int rproc_start_subdevices(struct rproc *rproc)
  941. {
  942. struct rproc_subdev *subdev;
  943. int ret;
  944. list_for_each_entry(subdev, &rproc->subdevs, node) {
  945. if (subdev->start) {
  946. ret = subdev->start(subdev);
  947. if (ret)
  948. goto unroll_registration;
  949. }
  950. }
  951. return 0;
  952. unroll_registration:
  953. list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
  954. if (subdev->stop)
  955. subdev->stop(subdev, true);
  956. }
  957. return ret;
  958. }
  959. static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
  960. {
  961. struct rproc_subdev *subdev;
  962. list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
  963. if (subdev->stop)
  964. subdev->stop(subdev, crashed);
  965. }
  966. }
  967. static void rproc_unprepare_subdevices(struct rproc *rproc)
  968. {
  969. struct rproc_subdev *subdev;
  970. list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
  971. if (subdev->unprepare)
  972. subdev->unprepare(subdev);
  973. }
  974. }
  975. /**
  976. * rproc_alloc_registered_carveouts() - allocate all carveouts registered
  977. * in the list
  978. * @rproc: the remote processor handle
  979. *
  980. * This function parses registered carveout list, performs allocation
  981. * if alloc() ops registered and updates resource table information
  982. * if rsc_offset set.
  983. *
  984. * Return: 0 on success
  985. */
  986. static int rproc_alloc_registered_carveouts(struct rproc *rproc)
  987. {
  988. struct rproc_mem_entry *entry, *tmp;
  989. struct fw_rsc_carveout *rsc;
  990. struct device *dev = &rproc->dev;
  991. u64 pa;
  992. int ret;
  993. list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
  994. if (entry->alloc) {
  995. ret = entry->alloc(rproc, entry);
  996. if (ret) {
  997. dev_err(dev, "Unable to allocate carveout %s: %d\n",
  998. entry->name, ret);
  999. return -ENOMEM;
  1000. }
  1001. }
  1002. if (entry->rsc_offset != FW_RSC_ADDR_ANY) {
  1003. /* update resource table */
  1004. rsc = (void *)rproc->table_ptr + entry->rsc_offset;
  1005. /*
  1006. * Some remote processors might need to know the pa
  1007. * even though they are behind an IOMMU. E.g., OMAP4's
  1008. * remote M3 processor needs this so it can control
  1009. * on-chip hardware accelerators that are not behind
  1010. * the IOMMU, and therefor must know the pa.
  1011. *
  1012. * Generally we don't want to expose physical addresses
  1013. * if we don't have to (remote processors are generally
  1014. * _not_ trusted), so we might want to do this only for
  1015. * remote processor that _must_ have this (e.g. OMAP4's
  1016. * dual M3 subsystem).
  1017. *
  1018. * Non-IOMMU processors might also want to have this info.
  1019. * In this case, the device address and the physical address
  1020. * are the same.
  1021. */
  1022. /* Use va if defined else dma to generate pa */
  1023. if (entry->va)
  1024. pa = (u64)rproc_va_to_pa(entry->va);
  1025. else
  1026. pa = (u64)entry->dma;
  1027. if (((u64)pa) & HIGH_BITS_MASK)
  1028. dev_warn(dev,
  1029. "Physical address cast in 32bit to fit resource table format\n");
  1030. rsc->pa = (u32)pa;
  1031. rsc->da = entry->da;
  1032. rsc->len = entry->len;
  1033. }
  1034. }
  1035. return 0;
  1036. }
  1037. /**
  1038. * rproc_resource_cleanup() - clean up and free all acquired resources
  1039. * @rproc: rproc handle
  1040. *
  1041. * This function will free all resources acquired for @rproc, and it
  1042. * is called whenever @rproc either shuts down or fails to boot.
  1043. */
  1044. void rproc_resource_cleanup(struct rproc *rproc)
  1045. {
  1046. struct rproc_mem_entry *entry, *tmp;
  1047. struct rproc_debug_trace *trace, *ttmp;
  1048. struct rproc_vdev *rvdev, *rvtmp;
  1049. struct device *dev = &rproc->dev;
  1050. /* clean up debugfs trace entries */
  1051. list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) {
  1052. rproc_remove_trace_file(trace->tfile);
  1053. rproc->num_traces--;
  1054. list_del(&trace->node);
  1055. kfree(trace);
  1056. }
  1057. /* clean up iommu mapping entries */
  1058. list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
  1059. size_t unmapped;
  1060. unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
  1061. if (unmapped != entry->len) {
  1062. /* nothing much to do besides complaining */
  1063. dev_err(dev, "failed to unmap %zx/%zu\n", entry->len,
  1064. unmapped);
  1065. }
  1066. list_del(&entry->node);
  1067. kfree(entry);
  1068. }
  1069. /* clean up carveout allocations */
  1070. list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
  1071. if (entry->release)
  1072. entry->release(rproc, entry);
  1073. list_del(&entry->node);
  1074. kfree(entry);
  1075. }
  1076. /* clean up remote vdev entries */
  1077. list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
  1078. platform_device_unregister(rvdev->pdev);
  1079. rproc_coredump_cleanup(rproc);
  1080. }
  1081. EXPORT_SYMBOL(rproc_resource_cleanup);
  1082. static int rproc_start(struct rproc *rproc, const struct firmware *fw)
  1083. {
  1084. struct resource_table *loaded_table;
  1085. struct device *dev = &rproc->dev;
  1086. int ret;
  1087. /* load the ELF segments to memory */
  1088. ret = rproc_load_segments(rproc, fw);
  1089. if (ret) {
  1090. dev_err(dev, "Failed to load program segments: %d\n", ret);
  1091. return ret;
  1092. }
  1093. /*
  1094. * The starting device has been given the rproc->cached_table as the
  1095. * resource table. The address of the vring along with the other
  1096. * allocated resources (carveouts etc) is stored in cached_table.
  1097. * In order to pass this information to the remote device we must copy
  1098. * this information to device memory. We also update the table_ptr so
  1099. * that any subsequent changes will be applied to the loaded version.
  1100. */
  1101. loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
  1102. if (loaded_table) {
  1103. memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
  1104. rproc->table_ptr = loaded_table;
  1105. }
  1106. ret = rproc_prepare_subdevices(rproc);
  1107. if (ret) {
  1108. dev_err(dev, "failed to prepare subdevices for %s: %d\n",
  1109. rproc->name, ret);
  1110. goto reset_table_ptr;
  1111. }
  1112. /* power up the remote processor */
  1113. ret = rproc->ops->start(rproc);
  1114. if (ret) {
  1115. dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
  1116. goto unprepare_subdevices;
  1117. }
  1118. /* Start any subdevices for the remote processor */
  1119. ret = rproc_start_subdevices(rproc);
  1120. if (ret) {
  1121. dev_err(dev, "failed to probe subdevices for %s: %d\n",
  1122. rproc->name, ret);
  1123. goto stop_rproc;
  1124. }
  1125. rproc->state = RPROC_RUNNING;
  1126. dev_info(dev, "remote processor %s is now up\n", rproc->name);
  1127. return 0;
  1128. stop_rproc:
  1129. rproc->ops->stop(rproc);
  1130. unprepare_subdevices:
  1131. rproc_unprepare_subdevices(rproc);
  1132. reset_table_ptr:
  1133. rproc->table_ptr = rproc->cached_table;
  1134. return ret;
  1135. }
  1136. static int __rproc_attach(struct rproc *rproc)
  1137. {
  1138. struct device *dev = &rproc->dev;
  1139. int ret;
  1140. ret = rproc_prepare_subdevices(rproc);
  1141. if (ret) {
  1142. dev_err(dev, "failed to prepare subdevices for %s: %d\n",
  1143. rproc->name, ret);
  1144. goto out;
  1145. }
  1146. /* Attach to the remote processor */
  1147. ret = rproc_attach_device(rproc);
  1148. if (ret) {
  1149. dev_err(dev, "can't attach to rproc %s: %d\n",
  1150. rproc->name, ret);
  1151. goto unprepare_subdevices;
  1152. }
  1153. /* Start any subdevices for the remote processor */
  1154. ret = rproc_start_subdevices(rproc);
  1155. if (ret) {
  1156. dev_err(dev, "failed to probe subdevices for %s: %d\n",
  1157. rproc->name, ret);
  1158. goto stop_rproc;
  1159. }
  1160. rproc->state = RPROC_ATTACHED;
  1161. dev_info(dev, "remote processor %s is now attached\n", rproc->name);
  1162. return 0;
  1163. stop_rproc:
  1164. rproc->ops->stop(rproc);
  1165. unprepare_subdevices:
  1166. rproc_unprepare_subdevices(rproc);
  1167. out:
  1168. return ret;
  1169. }
  1170. /*
  1171. * take a firmware and boot a remote processor with it.
  1172. */
  1173. static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
  1174. {
  1175. struct device *dev = &rproc->dev;
  1176. const char *name = rproc->firmware;
  1177. int ret;
  1178. ret = rproc_fw_sanity_check(rproc, fw);
  1179. if (ret)
  1180. return ret;
  1181. dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
  1182. /*
  1183. * if enabling an IOMMU isn't relevant for this rproc, this is
  1184. * just a nop
  1185. */
  1186. ret = rproc_enable_iommu(rproc);
  1187. if (ret) {
  1188. dev_err(dev, "can't enable iommu: %d\n", ret);
  1189. return ret;
  1190. }
  1191. /* Prepare rproc for firmware loading if needed */
  1192. ret = rproc_prepare_device(rproc);
  1193. if (ret) {
  1194. dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret);
  1195. goto disable_iommu;
  1196. }
  1197. rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
  1198. /* Load resource table, core dump segment list etc from the firmware */
  1199. ret = rproc_parse_fw(rproc, fw);
  1200. if (ret)
  1201. goto unprepare_rproc;
  1202. /* reset max_notifyid */
  1203. rproc->max_notifyid = -1;
  1204. /* reset handled vdev */
  1205. rproc->nb_vdev = 0;
  1206. /* handle fw resources which are required to boot rproc */
  1207. ret = rproc_handle_resources(rproc, rproc_loading_handlers);
  1208. if (ret) {
  1209. dev_err(dev, "Failed to process resources: %d\n", ret);
  1210. goto clean_up_resources;
  1211. }
  1212. /* Allocate carveout resources associated to rproc */
  1213. ret = rproc_alloc_registered_carveouts(rproc);
  1214. if (ret) {
  1215. dev_err(dev, "Failed to allocate associated carveouts: %d\n",
  1216. ret);
  1217. goto clean_up_resources;
  1218. }
  1219. ret = rproc_start(rproc, fw);
  1220. if (ret)
  1221. goto clean_up_resources;
  1222. return 0;
  1223. clean_up_resources:
  1224. rproc_resource_cleanup(rproc);
  1225. kfree(rproc->cached_table);
  1226. rproc->cached_table = NULL;
  1227. rproc->table_ptr = NULL;
  1228. unprepare_rproc:
  1229. /* release HW resources if needed */
  1230. rproc_unprepare_device(rproc);
  1231. disable_iommu:
  1232. rproc_disable_iommu(rproc);
  1233. return ret;
  1234. }
  1235. static int rproc_set_rsc_table(struct rproc *rproc)
  1236. {
  1237. struct resource_table *table_ptr;
  1238. struct device *dev = &rproc->dev;
  1239. size_t table_sz;
  1240. int ret;
  1241. table_ptr = rproc_get_loaded_rsc_table(rproc, &table_sz);
  1242. if (!table_ptr) {
  1243. /* Not having a resource table is acceptable */
  1244. return 0;
  1245. }
  1246. if (IS_ERR(table_ptr)) {
  1247. ret = PTR_ERR(table_ptr);
  1248. dev_err(dev, "can't load resource table: %d\n", ret);
  1249. return ret;
  1250. }
  1251. /*
  1252. * If it is possible to detach the remote processor, keep an untouched
  1253. * copy of the resource table. That way we can start fresh again when
  1254. * the remote processor is re-attached, that is:
  1255. *
  1256. * DETACHED -> ATTACHED -> DETACHED -> ATTACHED
  1257. *
  1258. * Free'd in rproc_reset_rsc_table_on_detach() and
  1259. * rproc_reset_rsc_table_on_stop().
  1260. */
  1261. if (rproc->ops->detach) {
  1262. rproc->clean_table = kmemdup(table_ptr, table_sz, GFP_KERNEL);
  1263. if (!rproc->clean_table)
  1264. return -ENOMEM;
  1265. } else {
  1266. rproc->clean_table = NULL;
  1267. }
  1268. rproc->cached_table = NULL;
  1269. rproc->table_ptr = table_ptr;
  1270. rproc->table_sz = table_sz;
  1271. return 0;
  1272. }
  1273. static int rproc_reset_rsc_table_on_detach(struct rproc *rproc)
  1274. {
  1275. struct resource_table *table_ptr;
  1276. /* A resource table was never retrieved, nothing to do here */
  1277. if (!rproc->table_ptr)
  1278. return 0;
  1279. /*
  1280. * If we made it to this point a clean_table _must_ have been
  1281. * allocated in rproc_set_rsc_table(). If one isn't present
  1282. * something went really wrong and we must complain.
  1283. */
  1284. if (WARN_ON(!rproc->clean_table))
  1285. return -EINVAL;
  1286. /* Remember where the external entity installed the resource table */
  1287. table_ptr = rproc->table_ptr;
  1288. /*
  1289. * If we made it here the remote processor was started by another
  1290. * entity and a cache table doesn't exist. As such make a copy of
  1291. * the resource table currently used by the remote processor and
  1292. * use that for the rest of the shutdown process. The memory
  1293. * allocated here is free'd in rproc_detach().
  1294. */
  1295. rproc->cached_table = kmemdup(rproc->table_ptr,
  1296. rproc->table_sz, GFP_KERNEL);
  1297. if (!rproc->cached_table)
  1298. return -ENOMEM;
  1299. /*
  1300. * Use a copy of the resource table for the remainder of the
  1301. * shutdown process.
  1302. */
  1303. rproc->table_ptr = rproc->cached_table;
  1304. /*
  1305. * Reset the memory area where the firmware loaded the resource table
  1306. * to its original value. That way when we re-attach the remote
  1307. * processor the resource table is clean and ready to be used again.
  1308. */
  1309. memcpy(table_ptr, rproc->clean_table, rproc->table_sz);
  1310. /*
  1311. * The clean resource table is no longer needed. Allocated in
  1312. * rproc_set_rsc_table().
  1313. */
  1314. kfree(rproc->clean_table);
  1315. return 0;
  1316. }
  1317. static int rproc_reset_rsc_table_on_stop(struct rproc *rproc)
  1318. {
  1319. /* A resource table was never retrieved, nothing to do here */
  1320. if (!rproc->table_ptr)
  1321. return 0;
  1322. /*
  1323. * If a cache table exists the remote processor was started by
  1324. * the remoteproc core. That cache table should be used for
  1325. * the rest of the shutdown process.
  1326. */
  1327. if (rproc->cached_table)
  1328. goto out;
  1329. /*
  1330. * If we made it here the remote processor was started by another
  1331. * entity and a cache table doesn't exist. As such make a copy of
  1332. * the resource table currently used by the remote processor and
  1333. * use that for the rest of the shutdown process. The memory
  1334. * allocated here is free'd in rproc_shutdown().
  1335. */
  1336. rproc->cached_table = kmemdup(rproc->table_ptr,
  1337. rproc->table_sz, GFP_KERNEL);
  1338. if (!rproc->cached_table)
  1339. return -ENOMEM;
  1340. /*
  1341. * Since the remote processor is being switched off the clean table
  1342. * won't be needed. Allocated in rproc_set_rsc_table().
  1343. */
  1344. kfree(rproc->clean_table);
  1345. out:
  1346. /*
  1347. * Use a copy of the resource table for the remainder of the
  1348. * shutdown process.
  1349. */
  1350. rproc->table_ptr = rproc->cached_table;
  1351. return 0;
  1352. }
  1353. /*
  1354. * Attach to remote processor - similar to rproc_fw_boot() but without
  1355. * the steps that deal with the firmware image.
  1356. */
  1357. static int rproc_attach(struct rproc *rproc)
  1358. {
  1359. struct device *dev = &rproc->dev;
  1360. int ret;
  1361. /*
  1362. * if enabling an IOMMU isn't relevant for this rproc, this is
  1363. * just a nop
  1364. */
  1365. ret = rproc_enable_iommu(rproc);
  1366. if (ret) {
  1367. dev_err(dev, "can't enable iommu: %d\n", ret);
  1368. return ret;
  1369. }
  1370. /* Do anything that is needed to boot the remote processor */
  1371. ret = rproc_prepare_device(rproc);
  1372. if (ret) {
  1373. dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret);
  1374. goto disable_iommu;
  1375. }
  1376. ret = rproc_set_rsc_table(rproc);
  1377. if (ret) {
  1378. dev_err(dev, "can't load resource table: %d\n", ret);
  1379. goto clean_up_resources;
  1380. }
  1381. /* reset max_notifyid */
  1382. rproc->max_notifyid = -1;
  1383. /* reset handled vdev */
  1384. rproc->nb_vdev = 0;
  1385. /*
  1386. * Handle firmware resources required to attach to a remote processor.
  1387. * Because we are attaching rather than booting the remote processor,
  1388. * we expect the platform driver to properly set rproc->table_ptr.
  1389. */
  1390. ret = rproc_handle_resources(rproc, rproc_loading_handlers);
  1391. if (ret) {
  1392. dev_err(dev, "Failed to process resources: %d\n", ret);
  1393. goto clean_up_resources;
  1394. }
  1395. /* Allocate carveout resources associated to rproc */
  1396. ret = rproc_alloc_registered_carveouts(rproc);
  1397. if (ret) {
  1398. dev_err(dev, "Failed to allocate associated carveouts: %d\n",
  1399. ret);
  1400. goto clean_up_resources;
  1401. }
  1402. ret = __rproc_attach(rproc);
  1403. if (ret)
  1404. goto clean_up_resources;
  1405. return 0;
  1406. clean_up_resources:
  1407. rproc_resource_cleanup(rproc);
  1408. /* release HW resources if needed */
  1409. rproc_unprepare_device(rproc);
  1410. kfree(rproc->clean_table);
  1411. disable_iommu:
  1412. rproc_disable_iommu(rproc);
  1413. return ret;
  1414. }
  1415. /*
  1416. * take a firmware and boot it up.
  1417. *
  1418. * Note: this function is called asynchronously upon registration of the
  1419. * remote processor (so we must wait until it completes before we try
  1420. * to unregister the device. one other option is just to use kref here,
  1421. * that might be cleaner).
  1422. */
  1423. static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
  1424. {
  1425. struct rproc *rproc = context;
  1426. rproc_boot(rproc);
  1427. release_firmware(fw);
  1428. }
  1429. static int rproc_trigger_auto_boot(struct rproc *rproc)
  1430. {
  1431. int ret;
  1432. /*
  1433. * Since the remote processor is in a detached state, it has already
  1434. * been booted by another entity. As such there is no point in waiting
  1435. * for a firmware image to be loaded, we can simply initiate the process
  1436. * of attaching to it immediately.
  1437. */
  1438. if (rproc->state == RPROC_DETACHED)
  1439. return rproc_boot(rproc);
  1440. /*
  1441. * We're initiating an asynchronous firmware loading, so we can
  1442. * be built-in kernel code, without hanging the boot process.
  1443. */
  1444. ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT,
  1445. rproc->firmware, &rproc->dev, GFP_KERNEL,
  1446. rproc, rproc_auto_boot_callback);
  1447. if (ret < 0)
  1448. dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
  1449. return ret;
  1450. }
  1451. static int rproc_stop(struct rproc *rproc, bool crashed)
  1452. {
  1453. struct device *dev = &rproc->dev;
  1454. int ret;
  1455. /* No need to continue if a stop() operation has not been provided */
  1456. if (!rproc->ops->stop)
  1457. return -EINVAL;
  1458. /* Stop any subdevices for the remote processor */
  1459. rproc_stop_subdevices(rproc, crashed);
  1460. /* the installed resource table is no longer accessible */
  1461. ret = rproc_reset_rsc_table_on_stop(rproc);
  1462. if (ret) {
  1463. dev_err(dev, "can't reset resource table: %d\n", ret);
  1464. return ret;
  1465. }
  1466. /* power off the remote processor */
  1467. ret = rproc->ops->stop(rproc);
  1468. if (ret) {
  1469. dev_err(dev, "can't stop rproc: %d\n", ret);
  1470. return ret;
  1471. }
  1472. rproc_unprepare_subdevices(rproc);
  1473. rproc->state = RPROC_OFFLINE;
  1474. dev_info(dev, "stopped remote processor %s\n", rproc->name);
  1475. return 0;
  1476. }
  1477. /*
  1478. * __rproc_detach(): Does the opposite of __rproc_attach()
  1479. */
  1480. static int __rproc_detach(struct rproc *rproc)
  1481. {
  1482. struct device *dev = &rproc->dev;
  1483. int ret;
  1484. /* No need to continue if a detach() operation has not been provided */
  1485. if (!rproc->ops->detach)
  1486. return -EINVAL;
  1487. /* Stop any subdevices for the remote processor */
  1488. rproc_stop_subdevices(rproc, false);
  1489. /* the installed resource table is no longer accessible */
  1490. ret = rproc_reset_rsc_table_on_detach(rproc);
  1491. if (ret) {
  1492. dev_err(dev, "can't reset resource table: %d\n", ret);
  1493. return ret;
  1494. }
  1495. /* Tell the remote processor the core isn't available anymore */
  1496. ret = rproc->ops->detach(rproc);
  1497. if (ret) {
  1498. dev_err(dev, "can't detach from rproc: %d\n", ret);
  1499. return ret;
  1500. }
  1501. rproc_unprepare_subdevices(rproc);
  1502. rproc->state = RPROC_DETACHED;
  1503. dev_info(dev, "detached remote processor %s\n", rproc->name);
  1504. return 0;
  1505. }
  1506. static int rproc_attach_recovery(struct rproc *rproc)
  1507. {
  1508. int ret;
  1509. ret = __rproc_detach(rproc);
  1510. if (ret)
  1511. return ret;
  1512. return __rproc_attach(rproc);
  1513. }
  1514. static int rproc_boot_recovery(struct rproc *rproc)
  1515. {
  1516. const struct firmware *firmware_p;
  1517. struct device *dev = &rproc->dev;
  1518. int ret;
  1519. ret = rproc_stop(rproc, true);
  1520. if (ret)
  1521. return ret;
  1522. /* generate coredump */
  1523. rproc->ops->coredump(rproc);
  1524. /* load firmware */
  1525. ret = request_firmware(&firmware_p, rproc->firmware, dev);
  1526. if (ret < 0) {
  1527. dev_err(dev, "request_firmware failed: %d\n", ret);
  1528. return ret;
  1529. }
  1530. /* boot the remote processor up again */
  1531. ret = rproc_start(rproc, firmware_p);
  1532. release_firmware(firmware_p);
  1533. return ret;
  1534. }
  1535. /**
  1536. * rproc_trigger_recovery() - recover a remoteproc
  1537. * @rproc: the remote processor
  1538. *
  1539. * The recovery is done by resetting all the virtio devices, that way all the
  1540. * rpmsg drivers will be reseted along with the remote processor making the
  1541. * remoteproc functional again.
  1542. *
  1543. * This function can sleep, so it cannot be called from atomic context.
  1544. *
  1545. * Return: 0 on success or a negative value upon failure
  1546. */
  1547. int rproc_trigger_recovery(struct rproc *rproc)
  1548. {
  1549. struct device *dev = &rproc->dev;
  1550. int ret;
  1551. ret = mutex_lock_interruptible(&rproc->lock);
  1552. if (ret)
  1553. return ret;
  1554. /* State could have changed before we got the mutex */
  1555. if (rproc->state != RPROC_CRASHED)
  1556. goto unlock_mutex;
  1557. dev_err(dev, "recovering %s\n", rproc->name);
  1558. if (rproc_has_feature(rproc, RPROC_FEAT_ATTACH_ON_RECOVERY))
  1559. ret = rproc_attach_recovery(rproc);
  1560. else
  1561. ret = rproc_boot_recovery(rproc);
  1562. unlock_mutex:
  1563. mutex_unlock(&rproc->lock);
  1564. return ret;
  1565. }
  1566. /**
  1567. * rproc_crash_handler_work() - handle a crash
  1568. * @work: work treating the crash
  1569. *
  1570. * This function needs to handle everything related to a crash, like cpu
  1571. * registers and stack dump, information to help to debug the fatal error, etc.
  1572. */
  1573. static void rproc_crash_handler_work(struct work_struct *work)
  1574. {
  1575. struct rproc *rproc = container_of(work, struct rproc, crash_handler);
  1576. struct device *dev = &rproc->dev;
  1577. dev_dbg(dev, "enter %s\n", __func__);
  1578. mutex_lock(&rproc->lock);
  1579. if (rproc->state == RPROC_CRASHED) {
  1580. /* handle only the first crash detected */
  1581. mutex_unlock(&rproc->lock);
  1582. return;
  1583. }
  1584. if (rproc->state == RPROC_OFFLINE) {
  1585. /* Don't recover if the remote processor was stopped */
  1586. mutex_unlock(&rproc->lock);
  1587. goto out;
  1588. }
  1589. rproc->state = RPROC_CRASHED;
  1590. dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
  1591. rproc->name);
  1592. mutex_unlock(&rproc->lock);
  1593. if (!rproc->recovery_disabled)
  1594. rproc_trigger_recovery(rproc);
  1595. out:
  1596. pm_relax(rproc->dev.parent);
  1597. }
  1598. /**
  1599. * rproc_boot() - boot a remote processor
  1600. * @rproc: handle of a remote processor
  1601. *
  1602. * Boot a remote processor (i.e. load its firmware, power it on, ...).
  1603. *
  1604. * If the remote processor is already powered on, this function immediately
  1605. * returns (successfully).
  1606. *
  1607. * Return: 0 on success, and an appropriate error value otherwise
  1608. */
  1609. int rproc_boot(struct rproc *rproc)
  1610. {
  1611. const struct firmware *firmware_p;
  1612. struct device *dev;
  1613. int ret;
  1614. if (!rproc) {
  1615. pr_err("invalid rproc handle\n");
  1616. return -EINVAL;
  1617. }
  1618. dev = &rproc->dev;
  1619. ret = mutex_lock_interruptible(&rproc->lock);
  1620. if (ret) {
  1621. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  1622. return ret;
  1623. }
  1624. if (rproc->state == RPROC_DELETED) {
  1625. ret = -ENODEV;
  1626. dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
  1627. goto unlock_mutex;
  1628. }
  1629. /* skip the boot or attach process if rproc is already powered up */
  1630. if (atomic_inc_return(&rproc->power) > 1) {
  1631. ret = 0;
  1632. goto unlock_mutex;
  1633. }
  1634. if (rproc->state == RPROC_DETACHED) {
  1635. dev_info(dev, "attaching to %s\n", rproc->name);
  1636. ret = rproc_attach(rproc);
  1637. } else {
  1638. dev_info(dev, "powering up %s\n", rproc->name);
  1639. /* load firmware */
  1640. ret = request_firmware(&firmware_p, rproc->firmware, dev);
  1641. if (ret < 0) {
  1642. dev_err(dev, "request_firmware failed: %d\n", ret);
  1643. goto downref_rproc;
  1644. }
  1645. ret = rproc_fw_boot(rproc, firmware_p);
  1646. release_firmware(firmware_p);
  1647. }
  1648. downref_rproc:
  1649. if (ret)
  1650. atomic_dec(&rproc->power);
  1651. unlock_mutex:
  1652. mutex_unlock(&rproc->lock);
  1653. return ret;
  1654. }
  1655. EXPORT_SYMBOL(rproc_boot);
  1656. /**
  1657. * rproc_shutdown() - power off the remote processor
  1658. * @rproc: the remote processor
  1659. *
  1660. * Power off a remote processor (previously booted with rproc_boot()).
  1661. *
  1662. * In case @rproc is still being used by an additional user(s), then
  1663. * this function will just decrement the power refcount and exit,
  1664. * without really powering off the device.
  1665. *
  1666. * Every call to rproc_boot() must (eventually) be accompanied by a call
  1667. * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
  1668. *
  1669. * Notes:
  1670. * - we're not decrementing the rproc's refcount, only the power refcount.
  1671. * which means that the @rproc handle stays valid even after rproc_shutdown()
  1672. * returns, and users can still use it with a subsequent rproc_boot(), if
  1673. * needed.
  1674. *
  1675. * Return: 0 on success, and an appropriate error value otherwise
  1676. */
  1677. int rproc_shutdown(struct rproc *rproc)
  1678. {
  1679. struct device *dev = &rproc->dev;
  1680. int ret;
  1681. ret = mutex_lock_interruptible(&rproc->lock);
  1682. if (ret) {
  1683. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  1684. return ret;
  1685. }
  1686. if (rproc->state != RPROC_RUNNING &&
  1687. rproc->state != RPROC_ATTACHED) {
  1688. ret = -EINVAL;
  1689. goto out;
  1690. }
  1691. /* if the remote proc is still needed, bail out */
  1692. if (!atomic_dec_and_test(&rproc->power))
  1693. goto out;
  1694. ret = rproc_stop(rproc, false);
  1695. if (ret) {
  1696. atomic_inc(&rproc->power);
  1697. goto out;
  1698. }
  1699. /* clean up all acquired resources */
  1700. rproc_resource_cleanup(rproc);
  1701. /* release HW resources if needed */
  1702. rproc_unprepare_device(rproc);
  1703. rproc_disable_iommu(rproc);
  1704. /* Free the copy of the resource table */
  1705. kfree(rproc->cached_table);
  1706. rproc->cached_table = NULL;
  1707. rproc->table_ptr = NULL;
  1708. out:
  1709. mutex_unlock(&rproc->lock);
  1710. return ret;
  1711. }
  1712. EXPORT_SYMBOL(rproc_shutdown);
  1713. /**
  1714. * rproc_detach() - Detach the remote processor from the
  1715. * remoteproc core
  1716. *
  1717. * @rproc: the remote processor
  1718. *
  1719. * Detach a remote processor (previously attached to with rproc_attach()).
  1720. *
  1721. * In case @rproc is still being used by an additional user(s), then
  1722. * this function will just decrement the power refcount and exit,
  1723. * without disconnecting the device.
  1724. *
  1725. * Function rproc_detach() calls __rproc_detach() in order to let a remote
  1726. * processor know that services provided by the application processor are
  1727. * no longer available. From there it should be possible to remove the
  1728. * platform driver and even power cycle the application processor (if the HW
  1729. * supports it) without needing to switch off the remote processor.
  1730. *
  1731. * Return: 0 on success, and an appropriate error value otherwise
  1732. */
  1733. int rproc_detach(struct rproc *rproc)
  1734. {
  1735. struct device *dev = &rproc->dev;
  1736. int ret;
  1737. ret = mutex_lock_interruptible(&rproc->lock);
  1738. if (ret) {
  1739. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  1740. return ret;
  1741. }
  1742. if (rproc->state != RPROC_ATTACHED) {
  1743. ret = -EINVAL;
  1744. goto out;
  1745. }
  1746. /* if the remote proc is still needed, bail out */
  1747. if (!atomic_dec_and_test(&rproc->power)) {
  1748. ret = 0;
  1749. goto out;
  1750. }
  1751. ret = __rproc_detach(rproc);
  1752. if (ret) {
  1753. atomic_inc(&rproc->power);
  1754. goto out;
  1755. }
  1756. /* clean up all acquired resources */
  1757. rproc_resource_cleanup(rproc);
  1758. /* release HW resources if needed */
  1759. rproc_unprepare_device(rproc);
  1760. rproc_disable_iommu(rproc);
  1761. /* Free the copy of the resource table */
  1762. kfree(rproc->cached_table);
  1763. rproc->cached_table = NULL;
  1764. rproc->table_ptr = NULL;
  1765. out:
  1766. mutex_unlock(&rproc->lock);
  1767. return ret;
  1768. }
  1769. EXPORT_SYMBOL(rproc_detach);
  1770. /**
  1771. * rproc_get_by_phandle() - find a remote processor by phandle
  1772. * @phandle: phandle to the rproc
  1773. *
  1774. * Finds an rproc handle using the remote processor's phandle, and then
  1775. * return a handle to the rproc.
  1776. *
  1777. * This function increments the remote processor's refcount, so always
  1778. * use rproc_put() to decrement it back once rproc isn't needed anymore.
  1779. *
  1780. * Return: rproc handle on success, and NULL on failure
  1781. */
  1782. #ifdef CONFIG_OF
  1783. struct rproc *rproc_get_by_phandle(phandle phandle)
  1784. {
  1785. struct rproc *rproc = NULL, *r;
  1786. struct device_driver *driver;
  1787. struct device_node *np;
  1788. np = of_find_node_by_phandle(phandle);
  1789. if (!np)
  1790. return NULL;
  1791. rcu_read_lock();
  1792. list_for_each_entry_rcu(r, &rproc_list, node) {
  1793. if (r->dev.parent && device_match_of_node(r->dev.parent, np)) {
  1794. /* prevent underlying implementation from being removed */
  1795. /*
  1796. * If the remoteproc's parent has a driver, the
  1797. * remoteproc is not part of a cluster and we can use
  1798. * that driver.
  1799. */
  1800. driver = r->dev.parent->driver;
  1801. /*
  1802. * If the remoteproc's parent does not have a driver,
  1803. * look for the driver associated with the cluster.
  1804. */
  1805. if (!driver) {
  1806. if (r->dev.parent->parent)
  1807. driver = r->dev.parent->parent->driver;
  1808. if (!driver)
  1809. break;
  1810. }
  1811. if (!try_module_get(driver->owner)) {
  1812. dev_err(&r->dev, "can't get owner\n");
  1813. break;
  1814. }
  1815. rproc = r;
  1816. get_device(&rproc->dev);
  1817. break;
  1818. }
  1819. }
  1820. rcu_read_unlock();
  1821. of_node_put(np);
  1822. return rproc;
  1823. }
  1824. #else
  1825. struct rproc *rproc_get_by_phandle(phandle phandle)
  1826. {
  1827. return NULL;
  1828. }
  1829. #endif
  1830. EXPORT_SYMBOL(rproc_get_by_phandle);
  1831. /**
  1832. * rproc_set_firmware() - assign a new firmware
  1833. * @rproc: rproc handle to which the new firmware is being assigned
  1834. * @fw_name: new firmware name to be assigned
  1835. *
  1836. * This function allows remoteproc drivers or clients to configure a custom
  1837. * firmware name that is different from the default name used during remoteproc
  1838. * registration. The function does not trigger a remote processor boot,
  1839. * only sets the firmware name used for a subsequent boot. This function
  1840. * should also be called only when the remote processor is offline.
  1841. *
  1842. * This allows either the userspace to configure a different name through
  1843. * sysfs or a kernel-level remoteproc or a remoteproc client driver to set
  1844. * a specific firmware when it is controlling the boot and shutdown of the
  1845. * remote processor.
  1846. *
  1847. * Return: 0 on success or a negative value upon failure
  1848. */
  1849. int rproc_set_firmware(struct rproc *rproc, const char *fw_name)
  1850. {
  1851. struct device *dev;
  1852. int ret, len;
  1853. char *p;
  1854. if (!rproc || !fw_name)
  1855. return -EINVAL;
  1856. dev = rproc->dev.parent;
  1857. ret = mutex_lock_interruptible(&rproc->lock);
  1858. if (ret) {
  1859. dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
  1860. return -EINVAL;
  1861. }
  1862. if (rproc->state != RPROC_OFFLINE) {
  1863. dev_err(dev, "can't change firmware while running\n");
  1864. ret = -EBUSY;
  1865. goto out;
  1866. }
  1867. len = strcspn(fw_name, "\n");
  1868. if (!len) {
  1869. dev_err(dev, "can't provide empty string for firmware name\n");
  1870. ret = -EINVAL;
  1871. goto out;
  1872. }
  1873. p = kstrndup(fw_name, len, GFP_KERNEL);
  1874. if (!p) {
  1875. ret = -ENOMEM;
  1876. goto out;
  1877. }
  1878. kfree_const(rproc->firmware);
  1879. rproc->firmware = p;
  1880. out:
  1881. mutex_unlock(&rproc->lock);
  1882. return ret;
  1883. }
  1884. EXPORT_SYMBOL(rproc_set_firmware);
  1885. static int rproc_validate(struct rproc *rproc)
  1886. {
  1887. switch (rproc->state) {
  1888. case RPROC_OFFLINE:
  1889. /*
  1890. * An offline processor without a start()
  1891. * function makes no sense.
  1892. */
  1893. if (!rproc->ops->start)
  1894. return -EINVAL;
  1895. break;
  1896. case RPROC_DETACHED:
  1897. /*
  1898. * A remote processor in a detached state without an
  1899. * attach() function makes not sense.
  1900. */
  1901. if (!rproc->ops->attach)
  1902. return -EINVAL;
  1903. /*
  1904. * When attaching to a remote processor the device memory
  1905. * is already available and as such there is no need to have a
  1906. * cached table.
  1907. */
  1908. if (rproc->cached_table)
  1909. return -EINVAL;
  1910. break;
  1911. default:
  1912. /*
  1913. * When adding a remote processor, the state of the device
  1914. * can be offline or detached, nothing else.
  1915. */
  1916. return -EINVAL;
  1917. }
  1918. return 0;
  1919. }
  1920. /**
  1921. * rproc_add() - register a remote processor
  1922. * @rproc: the remote processor handle to register
  1923. *
  1924. * Registers @rproc with the remoteproc framework, after it has been
  1925. * allocated with rproc_alloc().
  1926. *
  1927. * This is called by the platform-specific rproc implementation, whenever
  1928. * a new remote processor device is probed.
  1929. *
  1930. * Note: this function initiates an asynchronous firmware loading
  1931. * context, which will look for virtio devices supported by the rproc's
  1932. * firmware.
  1933. *
  1934. * If found, those virtio devices will be created and added, so as a result
  1935. * of registering this remote processor, additional virtio drivers might be
  1936. * probed.
  1937. *
  1938. * Return: 0 on success and an appropriate error code otherwise
  1939. */
  1940. int rproc_add(struct rproc *rproc)
  1941. {
  1942. struct device *dev = &rproc->dev;
  1943. int ret;
  1944. ret = rproc_validate(rproc);
  1945. if (ret < 0)
  1946. return ret;
  1947. /* add char device for this remoteproc */
  1948. ret = rproc_char_device_add(rproc);
  1949. if (ret < 0)
  1950. return ret;
  1951. ret = device_add(dev);
  1952. if (ret < 0) {
  1953. put_device(dev);
  1954. goto rproc_remove_cdev;
  1955. }
  1956. dev_info(dev, "%s is available\n", rproc->name);
  1957. /* create debugfs entries */
  1958. rproc_create_debug_dir(rproc);
  1959. /* if rproc is marked always-on, request it to boot */
  1960. if (rproc->auto_boot) {
  1961. ret = rproc_trigger_auto_boot(rproc);
  1962. if (ret < 0)
  1963. goto rproc_remove_dev;
  1964. }
  1965. /* expose to rproc_get_by_phandle users */
  1966. mutex_lock(&rproc_list_mutex);
  1967. list_add_rcu(&rproc->node, &rproc_list);
  1968. mutex_unlock(&rproc_list_mutex);
  1969. return 0;
  1970. rproc_remove_dev:
  1971. rproc_delete_debug_dir(rproc);
  1972. device_del(dev);
  1973. rproc_remove_cdev:
  1974. rproc_char_device_remove(rproc);
  1975. return ret;
  1976. }
  1977. EXPORT_SYMBOL(rproc_add);
  1978. static void devm_rproc_remove(void *rproc)
  1979. {
  1980. rproc_del(rproc);
  1981. }
  1982. /**
  1983. * devm_rproc_add() - resource managed rproc_add()
  1984. * @dev: the underlying device
  1985. * @rproc: the remote processor handle to register
  1986. *
  1987. * This function performs like rproc_add() but the registered rproc device will
  1988. * automatically be removed on driver detach.
  1989. *
  1990. * Return: 0 on success, negative errno on failure
  1991. */
  1992. int devm_rproc_add(struct device *dev, struct rproc *rproc)
  1993. {
  1994. int err;
  1995. err = rproc_add(rproc);
  1996. if (err)
  1997. return err;
  1998. return devm_add_action_or_reset(dev, devm_rproc_remove, rproc);
  1999. }
  2000. EXPORT_SYMBOL(devm_rproc_add);
  2001. /**
  2002. * rproc_type_release() - release a remote processor instance
  2003. * @dev: the rproc's device
  2004. *
  2005. * This function should _never_ be called directly.
  2006. *
  2007. * It will be called by the driver core when no one holds a valid pointer
  2008. * to @dev anymore.
  2009. */
  2010. static void rproc_type_release(struct device *dev)
  2011. {
  2012. struct rproc *rproc = container_of(dev, struct rproc, dev);
  2013. dev_info(&rproc->dev, "releasing %s\n", rproc->name);
  2014. idr_destroy(&rproc->notifyids);
  2015. if (rproc->index >= 0)
  2016. ida_free(&rproc_dev_index, rproc->index);
  2017. kfree_const(rproc->firmware);
  2018. kfree_const(rproc->name);
  2019. kfree(rproc->ops);
  2020. kfree(rproc);
  2021. }
  2022. static const struct device_type rproc_type = {
  2023. .name = "remoteproc",
  2024. .release = rproc_type_release,
  2025. };
  2026. static int rproc_alloc_firmware(struct rproc *rproc,
  2027. const char *name, const char *firmware)
  2028. {
  2029. const char *p;
  2030. /*
  2031. * Allocate a firmware name if the caller gave us one to work
  2032. * with. Otherwise construct a new one using a default pattern.
  2033. */
  2034. if (firmware)
  2035. p = kstrdup_const(firmware, GFP_KERNEL);
  2036. else
  2037. p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name);
  2038. if (!p)
  2039. return -ENOMEM;
  2040. rproc->firmware = p;
  2041. return 0;
  2042. }
  2043. static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops)
  2044. {
  2045. rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
  2046. if (!rproc->ops)
  2047. return -ENOMEM;
  2048. /* Default to rproc_coredump if no coredump function is specified */
  2049. if (!rproc->ops->coredump)
  2050. rproc->ops->coredump = rproc_coredump;
  2051. if (rproc->ops->load)
  2052. return 0;
  2053. /* Default to ELF loader if no load function is specified */
  2054. rproc->ops->load = rproc_elf_load_segments;
  2055. rproc->ops->parse_fw = rproc_elf_load_rsc_table;
  2056. rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
  2057. rproc->ops->sanity_check = rproc_elf_sanity_check;
  2058. rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
  2059. return 0;
  2060. }
  2061. /**
  2062. * rproc_alloc() - allocate a remote processor handle
  2063. * @dev: the underlying device
  2064. * @name: name of this remote processor
  2065. * @ops: platform-specific handlers (mainly start/stop)
  2066. * @firmware: name of firmware file to load, can be NULL
  2067. * @len: length of private data needed by the rproc driver (in bytes)
  2068. *
  2069. * Allocates a new remote processor handle, but does not register
  2070. * it yet. if @firmware is NULL, a default name is used.
  2071. *
  2072. * This function should be used by rproc implementations during initialization
  2073. * of the remote processor.
  2074. *
  2075. * After creating an rproc handle using this function, and when ready,
  2076. * implementations should then call rproc_add() to complete
  2077. * the registration of the remote processor.
  2078. *
  2079. * Note: _never_ directly deallocate @rproc, even if it was not registered
  2080. * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
  2081. *
  2082. * Return: new rproc pointer on success, and NULL on failure
  2083. */
  2084. struct rproc *rproc_alloc(struct device *dev, const char *name,
  2085. const struct rproc_ops *ops,
  2086. const char *firmware, int len)
  2087. {
  2088. struct rproc *rproc;
  2089. if (!dev || !name || !ops)
  2090. return NULL;
  2091. rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
  2092. if (!rproc)
  2093. return NULL;
  2094. rproc->priv = &rproc[1];
  2095. rproc->auto_boot = true;
  2096. rproc->elf_class = ELFCLASSNONE;
  2097. rproc->elf_machine = EM_NONE;
  2098. device_initialize(&rproc->dev);
  2099. rproc->dev.parent = dev;
  2100. rproc->dev.type = &rproc_type;
  2101. rproc->dev.class = &rproc_class;
  2102. rproc->dev.driver_data = rproc;
  2103. idr_init(&rproc->notifyids);
  2104. /* Assign a unique device index and name */
  2105. rproc->index = ida_alloc(&rproc_dev_index, GFP_KERNEL);
  2106. if (rproc->index < 0) {
  2107. dev_err(dev, "ida_alloc failed: %d\n", rproc->index);
  2108. goto put_device;
  2109. }
  2110. rproc->name = kstrdup_const(name, GFP_KERNEL);
  2111. if (!rproc->name)
  2112. goto put_device;
  2113. if (rproc_alloc_firmware(rproc, name, firmware))
  2114. goto put_device;
  2115. if (rproc_alloc_ops(rproc, ops))
  2116. goto put_device;
  2117. dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
  2118. atomic_set(&rproc->power, 0);
  2119. mutex_init(&rproc->lock);
  2120. INIT_LIST_HEAD(&rproc->carveouts);
  2121. INIT_LIST_HEAD(&rproc->mappings);
  2122. INIT_LIST_HEAD(&rproc->traces);
  2123. INIT_LIST_HEAD(&rproc->rvdevs);
  2124. INIT_LIST_HEAD(&rproc->subdevs);
  2125. INIT_LIST_HEAD(&rproc->dump_segments);
  2126. INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
  2127. rproc->state = RPROC_OFFLINE;
  2128. return rproc;
  2129. put_device:
  2130. put_device(&rproc->dev);
  2131. return NULL;
  2132. }
  2133. EXPORT_SYMBOL(rproc_alloc);
  2134. /**
  2135. * rproc_free() - unroll rproc_alloc()
  2136. * @rproc: the remote processor handle
  2137. *
  2138. * This function decrements the rproc dev refcount.
  2139. *
  2140. * If no one holds any reference to rproc anymore, then its refcount would
  2141. * now drop to zero, and it would be freed.
  2142. */
  2143. void rproc_free(struct rproc *rproc)
  2144. {
  2145. put_device(&rproc->dev);
  2146. }
  2147. EXPORT_SYMBOL(rproc_free);
  2148. /**
  2149. * rproc_put() - release rproc reference
  2150. * @rproc: the remote processor handle
  2151. *
  2152. * This function decrements the rproc dev refcount.
  2153. *
  2154. * If no one holds any reference to rproc anymore, then its refcount would
  2155. * now drop to zero, and it would be freed.
  2156. */
  2157. void rproc_put(struct rproc *rproc)
  2158. {
  2159. if (rproc->dev.parent->driver)
  2160. module_put(rproc->dev.parent->driver->owner);
  2161. else
  2162. module_put(rproc->dev.parent->parent->driver->owner);
  2163. put_device(&rproc->dev);
  2164. }
  2165. EXPORT_SYMBOL(rproc_put);
  2166. /**
  2167. * rproc_del() - unregister a remote processor
  2168. * @rproc: rproc handle to unregister
  2169. *
  2170. * This function should be called when the platform specific rproc
  2171. * implementation decides to remove the rproc device. it should
  2172. * _only_ be called if a previous invocation of rproc_add()
  2173. * has completed successfully.
  2174. *
  2175. * After rproc_del() returns, @rproc isn't freed yet, because
  2176. * of the outstanding reference created by rproc_alloc. To decrement that
  2177. * one last refcount, one still needs to call rproc_free().
  2178. *
  2179. * Return: 0 on success and -EINVAL if @rproc isn't valid
  2180. */
  2181. int rproc_del(struct rproc *rproc)
  2182. {
  2183. if (!rproc)
  2184. return -EINVAL;
  2185. /* TODO: make sure this works with rproc->power > 1 */
  2186. rproc_shutdown(rproc);
  2187. mutex_lock(&rproc->lock);
  2188. rproc->state = RPROC_DELETED;
  2189. mutex_unlock(&rproc->lock);
  2190. rproc_delete_debug_dir(rproc);
  2191. /* the rproc is downref'ed as soon as it's removed from the klist */
  2192. mutex_lock(&rproc_list_mutex);
  2193. list_del_rcu(&rproc->node);
  2194. mutex_unlock(&rproc_list_mutex);
  2195. /* Ensure that no readers of rproc_list are still active */
  2196. synchronize_rcu();
  2197. device_del(&rproc->dev);
  2198. rproc_char_device_remove(rproc);
  2199. return 0;
  2200. }
  2201. EXPORT_SYMBOL(rproc_del);
  2202. static void devm_rproc_free(struct device *dev, void *res)
  2203. {
  2204. rproc_free(*(struct rproc **)res);
  2205. }
  2206. /**
  2207. * devm_rproc_alloc() - resource managed rproc_alloc()
  2208. * @dev: the underlying device
  2209. * @name: name of this remote processor
  2210. * @ops: platform-specific handlers (mainly start/stop)
  2211. * @firmware: name of firmware file to load, can be NULL
  2212. * @len: length of private data needed by the rproc driver (in bytes)
  2213. *
  2214. * This function performs like rproc_alloc() but the acquired rproc device will
  2215. * automatically be released on driver detach.
  2216. *
  2217. * Return: new rproc instance, or NULL on failure
  2218. */
  2219. struct rproc *devm_rproc_alloc(struct device *dev, const char *name,
  2220. const struct rproc_ops *ops,
  2221. const char *firmware, int len)
  2222. {
  2223. struct rproc **ptr, *rproc;
  2224. ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL);
  2225. if (!ptr)
  2226. return NULL;
  2227. rproc = rproc_alloc(dev, name, ops, firmware, len);
  2228. if (rproc) {
  2229. *ptr = rproc;
  2230. devres_add(dev, ptr);
  2231. } else {
  2232. devres_free(ptr);
  2233. }
  2234. return rproc;
  2235. }
  2236. EXPORT_SYMBOL(devm_rproc_alloc);
  2237. /**
  2238. * rproc_add_subdev() - add a subdevice to a remoteproc
  2239. * @rproc: rproc handle to add the subdevice to
  2240. * @subdev: subdev handle to register
  2241. *
  2242. * Caller is responsible for populating optional subdevice function pointers.
  2243. */
  2244. void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
  2245. {
  2246. list_add_tail(&subdev->node, &rproc->subdevs);
  2247. }
  2248. EXPORT_SYMBOL(rproc_add_subdev);
  2249. /**
  2250. * rproc_remove_subdev() - remove a subdevice from a remoteproc
  2251. * @rproc: rproc handle to remove the subdevice from
  2252. * @subdev: subdev handle, previously registered with rproc_add_subdev()
  2253. */
  2254. void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
  2255. {
  2256. list_del(&subdev->node);
  2257. }
  2258. EXPORT_SYMBOL(rproc_remove_subdev);
  2259. /**
  2260. * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
  2261. * @dev: child device to find ancestor of
  2262. *
  2263. * Return: the ancestor rproc instance, or NULL if not found
  2264. */
  2265. struct rproc *rproc_get_by_child(struct device *dev)
  2266. {
  2267. for (dev = dev->parent; dev; dev = dev->parent) {
  2268. if (dev->type == &rproc_type)
  2269. return dev->driver_data;
  2270. }
  2271. return NULL;
  2272. }
  2273. EXPORT_SYMBOL(rproc_get_by_child);
  2274. /**
  2275. * rproc_report_crash() - rproc crash reporter function
  2276. * @rproc: remote processor
  2277. * @type: crash type
  2278. *
  2279. * This function must be called every time a crash is detected by the low-level
  2280. * drivers implementing a specific remoteproc. This should not be called from a
  2281. * non-remoteproc driver.
  2282. *
  2283. * This function can be called from atomic/interrupt context.
  2284. */
  2285. void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
  2286. {
  2287. if (!rproc) {
  2288. pr_err("NULL rproc pointer\n");
  2289. return;
  2290. }
  2291. /* Prevent suspend while the remoteproc is being recovered */
  2292. pm_stay_awake(rproc->dev.parent);
  2293. dev_err(&rproc->dev, "crash detected in %s: type %s\n",
  2294. rproc->name, rproc_crash_to_string(type));
  2295. queue_work(rproc_recovery_wq, &rproc->crash_handler);
  2296. }
  2297. EXPORT_SYMBOL(rproc_report_crash);
  2298. static int rproc_panic_handler(struct notifier_block *nb, unsigned long event,
  2299. void *ptr)
  2300. {
  2301. unsigned int longest = 0;
  2302. struct rproc *rproc;
  2303. unsigned int d;
  2304. rcu_read_lock();
  2305. list_for_each_entry_rcu(rproc, &rproc_list, node) {
  2306. if (!rproc->ops->panic)
  2307. continue;
  2308. if (rproc->state != RPROC_RUNNING &&
  2309. rproc->state != RPROC_ATTACHED)
  2310. continue;
  2311. d = rproc->ops->panic(rproc);
  2312. longest = max(longest, d);
  2313. }
  2314. rcu_read_unlock();
  2315. /*
  2316. * Delay for the longest requested duration before returning. This can
  2317. * be used by the remoteproc drivers to give the remote processor time
  2318. * to perform any requested operations (such as flush caches), when
  2319. * it's not possible to signal the Linux side due to the panic.
  2320. */
  2321. mdelay(longest);
  2322. return NOTIFY_DONE;
  2323. }
  2324. static void __init rproc_init_panic(void)
  2325. {
  2326. rproc_panic_nb.notifier_call = rproc_panic_handler;
  2327. atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb);
  2328. }
  2329. static void __exit rproc_exit_panic(void)
  2330. {
  2331. atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb);
  2332. }
  2333. static int __init remoteproc_init(void)
  2334. {
  2335. rproc_recovery_wq = alloc_workqueue("rproc_recovery_wq",
  2336. WQ_UNBOUND | WQ_FREEZABLE, 0);
  2337. if (!rproc_recovery_wq) {
  2338. pr_err("remoteproc: creation of rproc_recovery_wq failed\n");
  2339. return -ENOMEM;
  2340. }
  2341. rproc_init_sysfs();
  2342. rproc_init_debugfs();
  2343. rproc_init_cdev();
  2344. rproc_init_panic();
  2345. return 0;
  2346. }
  2347. subsys_initcall(remoteproc_init);
  2348. static void __exit remoteproc_exit(void)
  2349. {
  2350. ida_destroy(&rproc_dev_index);
  2351. if (!rproc_recovery_wq)
  2352. return;
  2353. rproc_exit_panic();
  2354. rproc_exit_debugfs();
  2355. rproc_exit_sysfs();
  2356. destroy_workqueue(rproc_recovery_wq);
  2357. }
  2358. module_exit(remoteproc_exit);
  2359. MODULE_DESCRIPTION("Generic Remote Processor Framework");