remoteproc_core.c 41 KB

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