keystone_remoteproc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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/of_gpio.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. u32 boot_offset;
  58. int irq_ring;
  59. int irq_fault;
  60. int kick_gpio;
  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 (WARN_ON(ksproc->kick_gpio < 0))
  205. return;
  206. gpio_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. int ret;
  295. if (!of_property_read_bool(np, "ti,syscon-dev")) {
  296. dev_err(dev, "ti,syscon-dev property is absent\n");
  297. return -EINVAL;
  298. }
  299. ksproc->dev_ctrl =
  300. syscon_regmap_lookup_by_phandle(np, "ti,syscon-dev");
  301. if (IS_ERR(ksproc->dev_ctrl)) {
  302. ret = PTR_ERR(ksproc->dev_ctrl);
  303. return ret;
  304. }
  305. if (of_property_read_u32_index(np, "ti,syscon-dev", 1,
  306. &ksproc->boot_offset)) {
  307. dev_err(dev, "couldn't read the boot register offset\n");
  308. return -EINVAL;
  309. }
  310. return 0;
  311. }
  312. static int keystone_rproc_probe(struct platform_device *pdev)
  313. {
  314. struct device *dev = &pdev->dev;
  315. struct device_node *np = dev->of_node;
  316. struct keystone_rproc *ksproc;
  317. struct rproc *rproc;
  318. int dsp_id;
  319. char *fw_name = NULL;
  320. char *template = "keystone-dsp%d-fw";
  321. int name_len = 0;
  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. name_len = strlen(template); /* assuming a single digit alias */
  334. fw_name = devm_kzalloc(dev, name_len, GFP_KERNEL);
  335. if (!fw_name)
  336. return -ENOMEM;
  337. snprintf(fw_name, name_len, template, dsp_id);
  338. rproc = rproc_alloc(dev, dev_name(dev), &keystone_rproc_ops, fw_name,
  339. sizeof(*ksproc));
  340. if (!rproc)
  341. return -ENOMEM;
  342. rproc->has_iommu = false;
  343. ksproc = rproc->priv;
  344. ksproc->rproc = rproc;
  345. ksproc->dev = dev;
  346. ret = keystone_rproc_of_get_dev_syscon(pdev, ksproc);
  347. if (ret)
  348. goto free_rproc;
  349. ksproc->reset = devm_reset_control_get_exclusive(dev, NULL);
  350. if (IS_ERR(ksproc->reset)) {
  351. ret = PTR_ERR(ksproc->reset);
  352. goto free_rproc;
  353. }
  354. /* enable clock for accessing DSP internal memories */
  355. pm_runtime_enable(dev);
  356. ret = pm_runtime_get_sync(dev);
  357. if (ret < 0) {
  358. dev_err(dev, "failed to enable clock, status = %d\n", ret);
  359. pm_runtime_put_noidle(dev);
  360. goto disable_rpm;
  361. }
  362. ret = keystone_rproc_of_get_memories(pdev, ksproc);
  363. if (ret)
  364. goto disable_clk;
  365. ksproc->irq_ring = platform_get_irq_byname(pdev, "vring");
  366. if (ksproc->irq_ring < 0) {
  367. ret = ksproc->irq_ring;
  368. goto disable_clk;
  369. }
  370. ksproc->irq_fault = platform_get_irq_byname(pdev, "exception");
  371. if (ksproc->irq_fault < 0) {
  372. ret = ksproc->irq_fault;
  373. goto disable_clk;
  374. }
  375. ksproc->kick_gpio = of_get_named_gpio_flags(np, "kick-gpios", 0, NULL);
  376. if (ksproc->kick_gpio < 0) {
  377. ret = ksproc->kick_gpio;
  378. dev_err(dev, "failed to get gpio for virtio kicks, status = %d\n",
  379. ret);
  380. goto disable_clk;
  381. }
  382. if (of_reserved_mem_device_init(dev))
  383. dev_warn(dev, "device does not have specific CMA pool\n");
  384. /* ensure the DSP is in reset before loading firmware */
  385. ret = reset_control_status(ksproc->reset);
  386. if (ret < 0) {
  387. dev_err(dev, "failed to get reset status, status = %d\n", ret);
  388. goto release_mem;
  389. } else if (ret == 0) {
  390. WARN(1, "device is not in reset\n");
  391. keystone_rproc_dsp_reset(ksproc);
  392. }
  393. ret = rproc_add(rproc);
  394. if (ret) {
  395. dev_err(dev, "failed to add register device with remoteproc core, status = %d\n",
  396. ret);
  397. goto release_mem;
  398. }
  399. platform_set_drvdata(pdev, ksproc);
  400. return 0;
  401. release_mem:
  402. of_reserved_mem_device_release(dev);
  403. disable_clk:
  404. pm_runtime_put_sync(dev);
  405. disable_rpm:
  406. pm_runtime_disable(dev);
  407. free_rproc:
  408. rproc_free(rproc);
  409. return ret;
  410. }
  411. static int keystone_rproc_remove(struct platform_device *pdev)
  412. {
  413. struct keystone_rproc *ksproc = platform_get_drvdata(pdev);
  414. rproc_del(ksproc->rproc);
  415. pm_runtime_put_sync(&pdev->dev);
  416. pm_runtime_disable(&pdev->dev);
  417. rproc_free(ksproc->rproc);
  418. of_reserved_mem_device_release(&pdev->dev);
  419. return 0;
  420. }
  421. static const struct of_device_id keystone_rproc_of_match[] = {
  422. { .compatible = "ti,k2hk-dsp", },
  423. { .compatible = "ti,k2l-dsp", },
  424. { .compatible = "ti,k2e-dsp", },
  425. { .compatible = "ti,k2g-dsp", },
  426. { /* sentinel */ },
  427. };
  428. MODULE_DEVICE_TABLE(of, keystone_rproc_of_match);
  429. static struct platform_driver keystone_rproc_driver = {
  430. .probe = keystone_rproc_probe,
  431. .remove = keystone_rproc_remove,
  432. .driver = {
  433. .name = "keystone-rproc",
  434. .of_match_table = keystone_rproc_of_match,
  435. },
  436. };
  437. module_platform_driver(keystone_rproc_driver);
  438. MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
  439. MODULE_LICENSE("GPL v2");
  440. MODULE_DESCRIPTION("TI Keystone DSP Remoteproc driver");