ptdma-dev.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Passthru DMA device driver
  4. * -- Based on the CCP driver
  5. *
  6. * Copyright (C) 2016,2021 Advanced Micro Devices, Inc.
  7. *
  8. * Author: Sanjay R Mehta <sanju.mehta@amd.com>
  9. * Author: Gary R Hook <gary.hook@amd.com>
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include "ptdma.h"
  19. /* Human-readable error strings */
  20. static char *pt_error_codes[] = {
  21. "",
  22. "ERR 01: ILLEGAL_ENGINE",
  23. "ERR 03: ILLEGAL_FUNCTION_TYPE",
  24. "ERR 04: ILLEGAL_FUNCTION_MODE",
  25. "ERR 06: ILLEGAL_FUNCTION_SIZE",
  26. "ERR 08: ILLEGAL_FUNCTION_RSVD",
  27. "ERR 09: ILLEGAL_BUFFER_LENGTH",
  28. "ERR 10: VLSB_FAULT",
  29. "ERR 11: ILLEGAL_MEM_ADDR",
  30. "ERR 12: ILLEGAL_MEM_SEL",
  31. "ERR 13: ILLEGAL_CONTEXT_ID",
  32. "ERR 15: 0xF Reserved",
  33. "ERR 18: CMD_TIMEOUT",
  34. "ERR 19: IDMA0_AXI_SLVERR",
  35. "ERR 20: IDMA0_AXI_DECERR",
  36. "ERR 21: 0x15 Reserved",
  37. "ERR 22: IDMA1_AXI_SLAVE_FAULT",
  38. "ERR 23: IDMA1_AIXI_DECERR",
  39. "ERR 24: 0x18 Reserved",
  40. "ERR 27: 0x1B Reserved",
  41. "ERR 38: ODMA0_AXI_SLVERR",
  42. "ERR 39: ODMA0_AXI_DECERR",
  43. "ERR 40: 0x28 Reserved",
  44. "ERR 41: ODMA1_AXI_SLVERR",
  45. "ERR 42: ODMA1_AXI_DECERR",
  46. "ERR 43: LSB_PARITY_ERR",
  47. };
  48. static void pt_log_error(struct pt_device *d, int e)
  49. {
  50. dev_err(d->dev, "PTDMA error: %s (0x%x)\n", pt_error_codes[e], e);
  51. }
  52. void pt_start_queue(struct pt_cmd_queue *cmd_q)
  53. {
  54. /* Turn on the run bit */
  55. iowrite32(cmd_q->qcontrol | CMD_Q_RUN, cmd_q->reg_control);
  56. }
  57. void pt_stop_queue(struct pt_cmd_queue *cmd_q)
  58. {
  59. /* Turn off the run bit */
  60. iowrite32(cmd_q->qcontrol & ~CMD_Q_RUN, cmd_q->reg_control);
  61. }
  62. static int pt_core_execute_cmd(struct ptdma_desc *desc, struct pt_cmd_queue *cmd_q)
  63. {
  64. bool soc = FIELD_GET(DWORD0_SOC, desc->dw0);
  65. u8 *q_desc = (u8 *)&cmd_q->qbase[cmd_q->qidx];
  66. u32 tail;
  67. unsigned long flags;
  68. if (soc) {
  69. desc->dw0 |= FIELD_PREP(DWORD0_IOC, desc->dw0);
  70. desc->dw0 &= ~DWORD0_SOC;
  71. }
  72. spin_lock_irqsave(&cmd_q->q_lock, flags);
  73. /* Copy 32-byte command descriptor to hw queue. */
  74. memcpy(q_desc, desc, 32);
  75. cmd_q->qidx = (cmd_q->qidx + 1) % CMD_Q_LEN;
  76. /* The data used by this command must be flushed to memory */
  77. wmb();
  78. /* Write the new tail address back to the queue register */
  79. tail = lower_32_bits(cmd_q->qdma_tail + cmd_q->qidx * Q_DESC_SIZE);
  80. iowrite32(tail, cmd_q->reg_control + 0x0004);
  81. /* Turn the queue back on using our cached control register */
  82. pt_start_queue(cmd_q);
  83. spin_unlock_irqrestore(&cmd_q->q_lock, flags);
  84. return 0;
  85. }
  86. int pt_core_perform_passthru(struct pt_cmd_queue *cmd_q,
  87. struct pt_passthru_engine *pt_engine)
  88. {
  89. struct ptdma_desc desc;
  90. struct pt_device *pt = container_of(cmd_q, struct pt_device, cmd_q);
  91. cmd_q->cmd_error = 0;
  92. cmd_q->total_pt_ops++;
  93. memset(&desc, 0, sizeof(desc));
  94. desc.dw0 = CMD_DESC_DW0_VAL;
  95. desc.length = pt_engine->src_len;
  96. desc.src_lo = lower_32_bits(pt_engine->src_dma);
  97. desc.dw3.src_hi = upper_32_bits(pt_engine->src_dma);
  98. desc.dst_lo = lower_32_bits(pt_engine->dst_dma);
  99. desc.dw5.dst_hi = upper_32_bits(pt_engine->dst_dma);
  100. if (cmd_q->int_en)
  101. pt_core_enable_queue_interrupts(pt);
  102. else
  103. pt_core_disable_queue_interrupts(pt);
  104. return pt_core_execute_cmd(&desc, cmd_q);
  105. }
  106. static void pt_do_cmd_complete(unsigned long data)
  107. {
  108. struct pt_tasklet_data *tdata = (struct pt_tasklet_data *)data;
  109. struct pt_cmd *cmd = tdata->cmd;
  110. struct pt_cmd_queue *cmd_q = &cmd->pt->cmd_q;
  111. u32 tail;
  112. if (cmd_q->cmd_error) {
  113. /*
  114. * Log the error and flush the queue by
  115. * moving the head pointer
  116. */
  117. tail = lower_32_bits(cmd_q->qdma_tail + cmd_q->qidx * Q_DESC_SIZE);
  118. pt_log_error(cmd_q->pt, cmd_q->cmd_error);
  119. iowrite32(tail, cmd_q->reg_control + 0x0008);
  120. }
  121. cmd->pt_cmd_callback(cmd->data, cmd->ret);
  122. }
  123. void pt_check_status_trans(struct pt_device *pt, struct pt_cmd_queue *cmd_q)
  124. {
  125. u32 status;
  126. status = ioread32(cmd_q->reg_control + 0x0010);
  127. if (status) {
  128. cmd_q->int_status = status;
  129. cmd_q->q_status = ioread32(cmd_q->reg_control + 0x0100);
  130. cmd_q->q_int_status = ioread32(cmd_q->reg_control + 0x0104);
  131. /* On error, only save the first error value */
  132. if ((status & INT_ERROR) && !cmd_q->cmd_error)
  133. cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status);
  134. /* Acknowledge the completion */
  135. iowrite32(status, cmd_q->reg_control + 0x0010);
  136. pt_do_cmd_complete((ulong)&pt->tdata);
  137. }
  138. }
  139. static irqreturn_t pt_core_irq_handler(int irq, void *data)
  140. {
  141. struct pt_device *pt = data;
  142. struct pt_cmd_queue *cmd_q = &pt->cmd_q;
  143. pt_core_disable_queue_interrupts(pt);
  144. pt->total_interrupts++;
  145. pt_check_status_trans(pt, cmd_q);
  146. pt_core_enable_queue_interrupts(pt);
  147. return IRQ_HANDLED;
  148. }
  149. int pt_core_init(struct pt_device *pt)
  150. {
  151. char dma_pool_name[MAX_DMAPOOL_NAME_LEN];
  152. struct pt_cmd_queue *cmd_q = &pt->cmd_q;
  153. u32 dma_addr_lo, dma_addr_hi;
  154. struct device *dev = pt->dev;
  155. struct dma_pool *dma_pool;
  156. int ret;
  157. /* Allocate a dma pool for the queue */
  158. snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q", dev_name(pt->dev));
  159. dma_pool = dma_pool_create(dma_pool_name, dev,
  160. PT_DMAPOOL_MAX_SIZE,
  161. PT_DMAPOOL_ALIGN, 0);
  162. if (!dma_pool)
  163. return -ENOMEM;
  164. /* ptdma core initialisation */
  165. iowrite32(CMD_CONFIG_VHB_EN, pt->io_regs + CMD_CONFIG_OFFSET);
  166. iowrite32(CMD_QUEUE_PRIO, pt->io_regs + CMD_QUEUE_PRIO_OFFSET);
  167. iowrite32(CMD_TIMEOUT_DISABLE, pt->io_regs + CMD_TIMEOUT_OFFSET);
  168. iowrite32(CMD_CLK_GATE_CONFIG, pt->io_regs + CMD_CLK_GATE_CTL_OFFSET);
  169. iowrite32(CMD_CONFIG_REQID, pt->io_regs + CMD_REQID_CONFIG_OFFSET);
  170. cmd_q->pt = pt;
  171. cmd_q->dma_pool = dma_pool;
  172. spin_lock_init(&cmd_q->q_lock);
  173. /* Page alignment satisfies our needs for N <= 128 */
  174. cmd_q->qsize = Q_SIZE(Q_DESC_SIZE);
  175. cmd_q->qbase = dma_alloc_coherent(dev, cmd_q->qsize,
  176. &cmd_q->qbase_dma,
  177. GFP_KERNEL);
  178. if (!cmd_q->qbase) {
  179. dev_err(dev, "unable to allocate command queue\n");
  180. ret = -ENOMEM;
  181. goto e_destroy_pool;
  182. }
  183. cmd_q->qidx = 0;
  184. /* Preset some register values */
  185. cmd_q->reg_control = pt->io_regs + CMD_Q_STATUS_INCR;
  186. /* Turn off the queues and disable interrupts until ready */
  187. pt_core_disable_queue_interrupts(pt);
  188. cmd_q->qcontrol = 0; /* Start with nothing */
  189. iowrite32(cmd_q->qcontrol, cmd_q->reg_control);
  190. ioread32(cmd_q->reg_control + 0x0104);
  191. ioread32(cmd_q->reg_control + 0x0100);
  192. /* Clear the interrupt status */
  193. iowrite32(SUPPORTED_INTERRUPTS, cmd_q->reg_control + 0x0010);
  194. /* Request an irq */
  195. ret = request_irq(pt->pt_irq, pt_core_irq_handler, 0, dev_name(pt->dev), pt);
  196. if (ret) {
  197. dev_err(dev, "unable to allocate an IRQ\n");
  198. goto e_free_dma;
  199. }
  200. /* Update the device registers with queue information. */
  201. cmd_q->qcontrol &= ~CMD_Q_SIZE;
  202. cmd_q->qcontrol |= FIELD_PREP(CMD_Q_SIZE, QUEUE_SIZE_VAL);
  203. cmd_q->qdma_tail = cmd_q->qbase_dma;
  204. dma_addr_lo = lower_32_bits(cmd_q->qdma_tail);
  205. iowrite32((u32)dma_addr_lo, cmd_q->reg_control + 0x0004);
  206. iowrite32((u32)dma_addr_lo, cmd_q->reg_control + 0x0008);
  207. dma_addr_hi = upper_32_bits(cmd_q->qdma_tail);
  208. cmd_q->qcontrol |= (dma_addr_hi << 16);
  209. iowrite32(cmd_q->qcontrol, cmd_q->reg_control);
  210. pt_core_enable_queue_interrupts(pt);
  211. /* Register the DMA engine support */
  212. ret = pt_dmaengine_register(pt);
  213. if (ret)
  214. goto e_free_irq;
  215. /* Set up debugfs entries */
  216. ptdma_debugfs_setup(pt);
  217. return 0;
  218. e_free_irq:
  219. free_irq(pt->pt_irq, pt);
  220. e_free_dma:
  221. dma_free_coherent(dev, cmd_q->qsize, cmd_q->qbase, cmd_q->qbase_dma);
  222. e_destroy_pool:
  223. dma_pool_destroy(pt->cmd_q.dma_pool);
  224. return ret;
  225. }
  226. void pt_core_destroy(struct pt_device *pt)
  227. {
  228. struct device *dev = pt->dev;
  229. struct pt_cmd_queue *cmd_q = &pt->cmd_q;
  230. struct pt_cmd *cmd;
  231. /* Unregister the DMA engine */
  232. pt_dmaengine_unregister(pt);
  233. /* Disable and clear interrupts */
  234. pt_core_disable_queue_interrupts(pt);
  235. /* Turn off the run bit */
  236. pt_stop_queue(cmd_q);
  237. /* Clear the interrupt status */
  238. iowrite32(SUPPORTED_INTERRUPTS, cmd_q->reg_control + 0x0010);
  239. ioread32(cmd_q->reg_control + 0x0104);
  240. ioread32(cmd_q->reg_control + 0x0100);
  241. free_irq(pt->pt_irq, pt);
  242. dma_free_coherent(dev, cmd_q->qsize, cmd_q->qbase,
  243. cmd_q->qbase_dma);
  244. /* Flush the cmd queue */
  245. while (!list_empty(&pt->cmd)) {
  246. /* Invoke the callback directly with an error code */
  247. cmd = list_first_entry(&pt->cmd, struct pt_cmd, entry);
  248. list_del(&cmd->entry);
  249. cmd->pt_cmd_callback(cmd->data, -ENODEV);
  250. }
  251. }