remoteproc_core.c 33 KB

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