remoteproc_core.c 35 KB

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