keystone_remoteproc.c 14 KB

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