remoteproc_core.c 47 KB

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