keystone_remoteproc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI Keystone DSP remoteproc driver
  4. *
  5. * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/io.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_reserved_mem.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/regmap.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/remoteproc.h>
  20. #include <linux/reset.h>
  21. #include "remoteproc_internal.h"
  22. #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK (SZ_16M - 1)
  23. /**
  24. * struct keystone_rproc_mem - internal memory structure
  25. * @cpu_addr: MPU virtual address of the memory region
  26. * @bus_addr: Bus address used to access the memory region
  27. * @dev_addr: Device address of the memory region from DSP view
  28. * @size: Size of the memory region
  29. */
  30. struct keystone_rproc_mem {
  31. void __iomem *cpu_addr;
  32. phys_addr_t bus_addr;
  33. u32 dev_addr;
  34. size_t size;
  35. };
  36. /**
  37. * struct keystone_rproc - keystone remote processor driver structure
  38. * @dev: cached device pointer
  39. * @rproc: remoteproc device handle
  40. * @mem: internal memory regions data
  41. * @num_mems: number of internal memory regions
  42. * @dev_ctrl: device control regmap handle
  43. * @reset: reset control handle
  44. * @boot_offset: boot register offset in @dev_ctrl regmap
  45. * @irq_ring: irq entry for vring
  46. * @irq_fault: irq entry for exception
  47. * @kick_gpio: gpio used for virtio kicks
  48. * @workqueue: workqueue for processing virtio interrupts
  49. */
  50. struct keystone_rproc {
  51. struct device *dev;
  52. struct rproc *rproc;
  53. struct keystone_rproc_mem *mem;
  54. int num_mems;
  55. struct regmap *dev_ctrl;
  56. struct reset_control *reset;
  57. struct gpio_desc *kick_gpio;
  58. u32 boot_offset;
  59. int irq_ring;
  60. int irq_fault;
  61. struct work_struct workqueue;
  62. };
  63. /* Put the DSP processor into reset */
  64. static void keystone_rproc_dsp_reset(struct keystone_rproc *ksproc)
  65. {
  66. reset_control_assert(ksproc->reset);
  67. }
  68. /* Configure the boot address and boot the DSP processor */
  69. static int keystone_rproc_dsp_boot(struct keystone_rproc *ksproc, u32 boot_addr)
  70. {
  71. int ret;
  72. if (boot_addr & (SZ_1K - 1)) {
  73. dev_err(ksproc->dev, "invalid boot address 0x%x, must be aligned on a 1KB boundary\n",
  74. boot_addr);
  75. return -EINVAL;
  76. }
  77. ret = regmap_write(ksproc->dev_ctrl, ksproc->boot_offset, boot_addr);
  78. if (ret) {
  79. dev_err(ksproc->dev, "regmap_write of boot address failed, status = %d\n",
  80. ret);
  81. return ret;
  82. }
  83. reset_control_deassert(ksproc->reset);
  84. return 0;
  85. }
  86. /*
  87. * Process the remoteproc exceptions
  88. *
  89. * The exception reporting on Keystone DSP remote processors is very simple
  90. * compared to the equivalent processors on the OMAP family, it is notified
  91. * through a software-designed specific interrupt source in the IPC interrupt
  92. * generation register.
  93. *
  94. * This function just invokes the rproc_report_crash to report the exception
  95. * to the remoteproc driver core, to trigger a recovery.
  96. */
  97. static irqreturn_t keystone_rproc_exception_interrupt(int irq, void *dev_id)
  98. {
  99. struct keystone_rproc *ksproc = dev_id;
  100. rproc_report_crash(ksproc->rproc, RPROC_FATAL_ERROR);
  101. return IRQ_HANDLED;
  102. }
  103. /*
  104. * Main virtqueue message workqueue function
  105. *
  106. * This function is executed upon scheduling of the keystone remoteproc
  107. * driver's workqueue. The workqueue is scheduled by the vring ISR handler.
  108. *
  109. * There is no payload message indicating the virtqueue index as is the
  110. * case with mailbox-based implementations on OMAP family. As such, this
  111. * handler processes both the Tx and Rx virtqueue indices on every invocation.
  112. * The rproc_vq_interrupt function can detect if there are new unprocessed
  113. * messages or not (returns IRQ_NONE vs IRQ_HANDLED), but there is no need
  114. * to check for these return values. The index 0 triggering will process all
  115. * pending Rx buffers, and the index 1 triggering will process all newly
  116. * available Tx buffers and will wakeup any potentially blocked senders.
  117. *
  118. * NOTE:
  119. * 1. A payload could be added by using some of the source bits in the
  120. * IPC interrupt generation registers, but this would need additional
  121. * changes to the overall IPC stack, and currently there are no benefits
  122. * of adapting that approach.
  123. * 2. The current logic is based on an inherent design assumption of supporting
  124. * only 2 vrings, but this can be changed if needed.
  125. */
  126. static void handle_event(struct work_struct *work)
  127. {
  128. struct keystone_rproc *ksproc =
  129. container_of(work, struct keystone_rproc, workqueue);
  130. rproc_vq_interrupt(ksproc->rproc, 0);
  131. rproc_vq_interrupt(ksproc->rproc, 1);
  132. }
  133. /*
  134. * Interrupt handler for processing vring kicks from remote processor
  135. */
  136. static irqreturn_t keystone_rproc_vring_interrupt(int irq, void *dev_id)
  137. {
  138. struct keystone_rproc *ksproc = dev_id;
  139. schedule_work(&ksproc->workqueue);
  140. return IRQ_HANDLED;
  141. }
  142. /*
  143. * Power up the DSP remote processor.
  144. *
  145. * This function will be invoked only after the firmware for this rproc
  146. * was loaded, parsed successfully, and all of its resource requirements
  147. * were met.
  148. */
  149. static int keystone_rproc_start(struct rproc *rproc)
  150. {
  151. struct keystone_rproc *ksproc = rproc->priv;
  152. int ret;
  153. INIT_WORK(&ksproc->workqueue, handle_event);
  154. ret = request_irq(ksproc->irq_ring, keystone_rproc_vring_interrupt, 0,
  155. dev_name(ksproc->dev), ksproc);
  156. if (ret) {
  157. dev_err(ksproc->dev, "failed to enable vring interrupt, ret = %d\n",
  158. ret);
  159. goto out;
  160. }
  161. ret = request_irq(ksproc->irq_fault, keystone_rproc_exception_interrupt,
  162. 0, dev_name(ksproc->dev), ksproc);
  163. if (ret) {
  164. dev_err(ksproc->dev, "failed to enable exception interrupt, ret = %d\n",
  165. ret);
  166. goto free_vring_irq;
  167. }
  168. ret = keystone_rproc_dsp_boot(ksproc, rproc->bootaddr);
  169. if (ret)
  170. goto free_exc_irq;
  171. return 0;
  172. free_exc_irq:
  173. free_irq(ksproc->irq_fault, ksproc);
  174. free_vring_irq:
  175. free_irq(ksproc->irq_ring, ksproc);
  176. flush_work(&ksproc->workqueue);
  177. out:
  178. return ret;
  179. }
  180. /*
  181. * Stop the DSP remote processor.
  182. *
  183. * This function puts the DSP processor into reset, and finishes processing
  184. * of any pending messages.
  185. */
  186. static int keystone_rproc_stop(struct rproc *rproc)
  187. {
  188. struct keystone_rproc *ksproc = rproc->priv;
  189. keystone_rproc_dsp_reset(ksproc);
  190. free_irq(ksproc->irq_fault, ksproc);
  191. free_irq(ksproc->irq_ring, ksproc);
  192. flush_work(&ksproc->workqueue);
  193. return 0;
  194. }
  195. /*
  196. * Kick the remote processor to notify about pending unprocessed messages.
  197. * The vqid usage is not used and is inconsequential, as the kick is performed
  198. * through a simulated GPIO (a bit in an IPC interrupt-triggering register),
  199. * the remote processor is expected to process both its Tx and Rx virtqueues.
  200. */
  201. static void keystone_rproc_kick(struct rproc *rproc, int vqid)
  202. {
  203. struct keystone_rproc *ksproc = rproc->priv;
  204. if (!ksproc->kick_gpio)
  205. return;
  206. gpiod_set_value(ksproc->kick_gpio, 1);
  207. }
  208. /*
  209. * Custom function to translate a DSP device address (internal RAMs only) to a
  210. * kernel virtual address. The DSPs can access their RAMs at either an internal
  211. * address visible only from a DSP, or at the SoC-level bus address. Both these
  212. * addresses need to be looked through for translation. The translated addresses
  213. * can be used either by the remoteproc core for loading (when using kernel
  214. * remoteproc loader), or by any rpmsg bus drivers.
  215. */
  216. static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
  217. {
  218. struct keystone_rproc *ksproc = rproc->priv;
  219. void __iomem *va = NULL;
  220. phys_addr_t bus_addr;
  221. u32 dev_addr, offset;
  222. size_t size;
  223. int i;
  224. if (len == 0)
  225. return NULL;
  226. for (i = 0; i < ksproc->num_mems; i++) {
  227. bus_addr = ksproc->mem[i].bus_addr;
  228. dev_addr = ksproc->mem[i].dev_addr;
  229. size = ksproc->mem[i].size;
  230. if (da < KEYSTONE_RPROC_LOCAL_ADDRESS_MASK) {
  231. /* handle DSP-view addresses */
  232. if ((da >= dev_addr) &&
  233. ((da + len) <= (dev_addr + size))) {
  234. offset = da - dev_addr;
  235. va = ksproc->mem[i].cpu_addr + offset;
  236. break;
  237. }
  238. } else {
  239. /* handle SoC-view addresses */
  240. if ((da >= bus_addr) &&
  241. (da + len) <= (bus_addr + size)) {
  242. offset = da - bus_addr;
  243. va = ksproc->mem[i].cpu_addr + offset;
  244. break;
  245. }
  246. }
  247. }
  248. return (__force void *)va;
  249. }
  250. static const struct rproc_ops keystone_rproc_ops = {
  251. .start = keystone_rproc_start,
  252. .stop = keystone_rproc_stop,
  253. .kick = keystone_rproc_kick,
  254. .da_to_va = keystone_rproc_da_to_va,
  255. };
  256. static int keystone_rproc_of_get_memories(struct platform_device *pdev,
  257. struct keystone_rproc *ksproc)
  258. {
  259. static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
  260. struct device *dev = &pdev->dev;
  261. struct resource *res;
  262. int num_mems = 0;
  263. int i;
  264. num_mems = ARRAY_SIZE(mem_names);
  265. ksproc->mem = devm_kcalloc(ksproc->dev, num_mems,
  266. sizeof(*ksproc->mem), GFP_KERNEL);
  267. if (!ksproc->mem)
  268. return -ENOMEM;
  269. for (i = 0; i < num_mems; i++) {
  270. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  271. mem_names[i]);
  272. ksproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
  273. if (IS_ERR(ksproc->mem[i].cpu_addr)) {
  274. dev_err(dev, "failed to parse and map %s memory\n",
  275. mem_names[i]);
  276. return PTR_ERR(ksproc->mem[i].cpu_addr);
  277. }
  278. ksproc->mem[i].bus_addr = res->start;
  279. ksproc->mem[i].dev_addr =
  280. res->start & KEYSTONE_RPROC_LOCAL_ADDRESS_MASK;
  281. ksproc->mem[i].size = resource_size(res);
  282. /* zero out memories to start in a pristine state */
  283. memset((__force void *)ksproc->mem[i].cpu_addr, 0,
  284. ksproc->mem[i].size);
  285. }
  286. ksproc->num_mems = num_mems;
  287. return 0;
  288. }
  289. static int keystone_rproc_of_get_dev_syscon(struct platform_device *pdev,
  290. struct keystone_rproc *ksproc)
  291. {
  292. struct device_node *np = pdev->dev.of_node;
  293. struct device *dev = &pdev->dev;
  294. if (!of_property_read_bool(np, "ti,syscon-dev")) {
  295. dev_err(dev, "ti,syscon-dev property is absent\n");
  296. return -EINVAL;
  297. }
  298. ksproc->dev_ctrl = syscon_regmap_lookup_by_phandle_args(np, "ti,syscon-dev",
  299. 1, &ksproc->boot_offset);
  300. if (IS_ERR(ksproc->dev_ctrl))
  301. return PTR_ERR(ksproc->dev_ctrl);
  302. return 0;
  303. }
  304. static void keystone_rproc_mem_release(void *data)
  305. {
  306. struct device *dev = data;
  307. of_reserved_mem_device_release(dev);
  308. }
  309. static void keystone_rproc_pm_runtime_put(void *data)
  310. {
  311. struct device *dev = data;
  312. pm_runtime_put_sync(dev);
  313. }
  314. static int keystone_rproc_probe(struct platform_device *pdev)
  315. {
  316. struct device *dev = &pdev->dev;
  317. struct device_node *np = dev->of_node;
  318. struct keystone_rproc *ksproc;
  319. struct rproc *rproc;
  320. int dsp_id;
  321. char *fw_name = NULL;
  322. int ret = 0;
  323. if (!np) {
  324. dev_err(dev, "only DT-based devices are supported\n");
  325. return -ENODEV;
  326. }
  327. dsp_id = of_alias_get_id(np, "rproc");
  328. if (dsp_id < 0) {
  329. dev_warn(dev, "device does not have an alias id\n");
  330. return dsp_id;
  331. }
  332. /* construct a custom default fw name - subject to change in future */
  333. fw_name = devm_kasprintf(dev, GFP_KERNEL, "keystone-dsp%d-fw", dsp_id);
  334. if (!fw_name)
  335. return -ENOMEM;
  336. rproc = devm_rproc_alloc(dev, dev_name(dev), &keystone_rproc_ops,
  337. fw_name, sizeof(*ksproc));
  338. if (!rproc)
  339. return -ENOMEM;
  340. rproc->has_iommu = false;
  341. ksproc = rproc->priv;
  342. ksproc->rproc = rproc;
  343. ksproc->dev = dev;
  344. ret = keystone_rproc_of_get_dev_syscon(pdev, ksproc);
  345. if (ret)
  346. return ret;
  347. ksproc->reset = devm_reset_control_get_exclusive(dev, NULL);
  348. if (IS_ERR(ksproc->reset))
  349. return PTR_ERR(ksproc->reset);
  350. /* enable clock for accessing DSP internal memories */
  351. ret = devm_pm_runtime_enable(dev);
  352. if (ret < 0)
  353. return dev_err_probe(dev, ret, "Failed to enable runtime PM\n");
  354. ret = pm_runtime_resume_and_get(dev);
  355. if (ret < 0)
  356. return dev_err_probe(dev, ret, "failed to enable clock\n");
  357. ret = devm_add_action_or_reset(dev, keystone_rproc_pm_runtime_put, dev);
  358. if (ret)
  359. return dev_err_probe(dev, ret, "failed to add disable pm devm action\n");
  360. ret = keystone_rproc_of_get_memories(pdev, ksproc);
  361. if (ret)
  362. return ret;
  363. ksproc->irq_ring = platform_get_irq_byname(pdev, "vring");
  364. if (ksproc->irq_ring < 0)
  365. return ksproc->irq_ring;
  366. ksproc->irq_fault = platform_get_irq_byname(pdev, "exception");
  367. if (ksproc->irq_fault < 0)
  368. return ksproc->irq_fault;
  369. ksproc->kick_gpio = devm_gpiod_get(dev, "kick", GPIOD_ASIS);
  370. ret = PTR_ERR_OR_ZERO(ksproc->kick_gpio);
  371. if (ret)
  372. return dev_err_probe(dev, ret, "failed to get gpio for virtio kicks\n");
  373. ret = of_reserved_mem_device_init(dev);
  374. if (ret) {
  375. dev_warn(dev, "device does not have specific CMA pool\n");
  376. } else {
  377. ret = devm_add_action_or_reset(dev, keystone_rproc_mem_release, dev);
  378. if (ret)
  379. return ret;
  380. }
  381. /* ensure the DSP is in reset before loading firmware */
  382. ret = reset_control_status(ksproc->reset);
  383. if (ret < 0) {
  384. return dev_err_probe(dev, ret, "failed to get reset status\n");
  385. } else if (ret == 0) {
  386. WARN(1, "device is not in reset\n");
  387. keystone_rproc_dsp_reset(ksproc);
  388. }
  389. ret = devm_rproc_add(dev, rproc);
  390. if (ret)
  391. return dev_err_probe(dev, ret, "failed to register device with remoteproc core\n");
  392. return 0;
  393. }
  394. static const struct of_device_id keystone_rproc_of_match[] = {
  395. { .compatible = "ti,k2hk-dsp", },
  396. { .compatible = "ti,k2l-dsp", },
  397. { .compatible = "ti,k2e-dsp", },
  398. { .compatible = "ti,k2g-dsp", },
  399. { /* sentinel */ },
  400. };
  401. MODULE_DEVICE_TABLE(of, keystone_rproc_of_match);
  402. static struct platform_driver keystone_rproc_driver = {
  403. .probe = keystone_rproc_probe,
  404. .driver = {
  405. .name = "keystone-rproc",
  406. .of_match_table = keystone_rproc_of_match,
  407. },
  408. };
  409. module_platform_driver(keystone_rproc_driver);
  410. MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
  411. MODULE_LICENSE("GPL v2");
  412. MODULE_DESCRIPTION("TI Keystone DSP Remoteproc driver");