remoteproc_core.c 33 KB

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