remoteproc_core.c 39 KB

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