remoteproc_core.c 67 KB

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