ptdma.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 <[email protected]>
  9. * Author: Tom Lendacky <[email protected]>
  10. * Author: Gary R Hook <[email protected]>
  11. */
  12. #ifndef __PT_DEV_H__
  13. #define __PT_DEV_H__
  14. #include <linux/device.h>
  15. #include <linux/dmaengine.h>
  16. #include <linux/pci.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/mutex.h>
  19. #include <linux/list.h>
  20. #include <linux/wait.h>
  21. #include <linux/dmapool.h>
  22. #include "../virt-dma.h"
  23. #define MAX_PT_NAME_LEN 16
  24. #define MAX_DMAPOOL_NAME_LEN 32
  25. #define MAX_HW_QUEUES 1
  26. #define MAX_CMD_QLEN 100
  27. #define PT_ENGINE_PASSTHRU 5
  28. /* Register Mappings */
  29. #define IRQ_MASK_REG 0x040
  30. #define IRQ_STATUS_REG 0x200
  31. #define CMD_Q_ERROR(__qs) ((__qs) & 0x0000003f)
  32. #define CMD_QUEUE_PRIO_OFFSET 0x00
  33. #define CMD_REQID_CONFIG_OFFSET 0x04
  34. #define CMD_TIMEOUT_OFFSET 0x08
  35. #define CMD_PT_VERSION 0x10
  36. #define CMD_Q_CONTROL_BASE 0x0000
  37. #define CMD_Q_TAIL_LO_BASE 0x0004
  38. #define CMD_Q_HEAD_LO_BASE 0x0008
  39. #define CMD_Q_INT_ENABLE_BASE 0x000C
  40. #define CMD_Q_INTERRUPT_STATUS_BASE 0x0010
  41. #define CMD_Q_STATUS_BASE 0x0100
  42. #define CMD_Q_INT_STATUS_BASE 0x0104
  43. #define CMD_Q_DMA_STATUS_BASE 0x0108
  44. #define CMD_Q_DMA_READ_STATUS_BASE 0x010C
  45. #define CMD_Q_DMA_WRITE_STATUS_BASE 0x0110
  46. #define CMD_Q_ABORT_BASE 0x0114
  47. #define CMD_Q_AX_CACHE_BASE 0x0118
  48. #define CMD_CONFIG_OFFSET 0x1120
  49. #define CMD_CLK_GATE_CTL_OFFSET 0x6004
  50. #define CMD_DESC_DW0_VAL 0x500012
  51. /* Address offset for virtual queue registers */
  52. #define CMD_Q_STATUS_INCR 0x1000
  53. /* Bit masks */
  54. #define CMD_CONFIG_REQID 0
  55. #define CMD_TIMEOUT_DISABLE 0
  56. #define CMD_CLK_DYN_GATING_DIS 0
  57. #define CMD_CLK_SW_GATE_MODE 0
  58. #define CMD_CLK_GATE_CTL 0
  59. #define CMD_QUEUE_PRIO GENMASK(2, 1)
  60. #define CMD_CONFIG_VHB_EN BIT(0)
  61. #define CMD_CLK_DYN_GATING_EN BIT(0)
  62. #define CMD_CLK_HW_GATE_MODE BIT(0)
  63. #define CMD_CLK_GATE_ON_DELAY BIT(12)
  64. #define CMD_CLK_GATE_OFF_DELAY BIT(12)
  65. #define CMD_CLK_GATE_CONFIG (CMD_CLK_GATE_CTL | \
  66. CMD_CLK_HW_GATE_MODE | \
  67. CMD_CLK_GATE_ON_DELAY | \
  68. CMD_CLK_DYN_GATING_EN | \
  69. CMD_CLK_GATE_OFF_DELAY)
  70. #define CMD_Q_LEN 32
  71. #define CMD_Q_RUN BIT(0)
  72. #define CMD_Q_HALT BIT(1)
  73. #define CMD_Q_MEM_LOCATION BIT(2)
  74. #define CMD_Q_SIZE_MASK GENMASK(4, 0)
  75. #define CMD_Q_SIZE GENMASK(7, 3)
  76. #define CMD_Q_SHIFT GENMASK(1, 0)
  77. #define QUEUE_SIZE_VAL ((ffs(CMD_Q_LEN) - 2) & \
  78. CMD_Q_SIZE_MASK)
  79. #define Q_PTR_MASK (2 << (QUEUE_SIZE_VAL + 5) - 1)
  80. #define Q_DESC_SIZE sizeof(struct ptdma_desc)
  81. #define Q_SIZE(n) (CMD_Q_LEN * (n))
  82. #define INT_COMPLETION BIT(0)
  83. #define INT_ERROR BIT(1)
  84. #define INT_QUEUE_STOPPED BIT(2)
  85. #define INT_EMPTY_QUEUE BIT(3)
  86. #define SUPPORTED_INTERRUPTS (INT_COMPLETION | INT_ERROR)
  87. /****** Local Storage Block ******/
  88. #define LSB_START 0
  89. #define LSB_END 127
  90. #define LSB_COUNT (LSB_END - LSB_START + 1)
  91. #define PT_DMAPOOL_MAX_SIZE 64
  92. #define PT_DMAPOOL_ALIGN BIT(5)
  93. #define PT_PASSTHRU_BLOCKSIZE 512
  94. struct pt_device;
  95. struct pt_tasklet_data {
  96. struct completion completion;
  97. struct pt_cmd *cmd;
  98. };
  99. /*
  100. * struct pt_passthru_engine - pass-through operation
  101. * without performing DMA mapping
  102. * @mask: mask to be applied to data
  103. * @mask_len: length in bytes of mask
  104. * @src_dma: data to be used for this operation
  105. * @dst_dma: data produced by this operation
  106. * @src_len: length in bytes of data used for this operation
  107. *
  108. * Variables required to be set when calling pt_enqueue_cmd():
  109. * - bit_mod, byte_swap, src, dst, src_len
  110. * - mask, mask_len if bit_mod is not PT_PASSTHRU_BITWISE_NOOP
  111. */
  112. struct pt_passthru_engine {
  113. dma_addr_t mask;
  114. u32 mask_len; /* In bytes */
  115. dma_addr_t src_dma, dst_dma;
  116. u64 src_len; /* In bytes */
  117. };
  118. /*
  119. * struct pt_cmd - PTDMA operation request
  120. * @entry: list element
  121. * @work: work element used for callbacks
  122. * @pt: PT device to be run on
  123. * @ret: operation return code
  124. * @flags: cmd processing flags
  125. * @engine: PTDMA operation to perform (passthru)
  126. * @engine_error: PT engine return code
  127. * @passthru: engine specific structures, refer to specific engine struct below
  128. * @callback: operation completion callback function
  129. * @data: parameter value to be supplied to the callback function
  130. *
  131. * Variables required to be set when calling pt_enqueue_cmd():
  132. * - engine, callback
  133. * - See the operation structures below for what is required for each
  134. * operation.
  135. */
  136. struct pt_cmd {
  137. struct list_head entry;
  138. struct work_struct work;
  139. struct pt_device *pt;
  140. int ret;
  141. u32 engine;
  142. u32 engine_error;
  143. struct pt_passthru_engine passthru;
  144. /* Completion callback support */
  145. void (*pt_cmd_callback)(void *data, int err);
  146. void *data;
  147. };
  148. struct pt_dma_desc {
  149. struct virt_dma_desc vd;
  150. struct pt_device *pt;
  151. enum dma_status status;
  152. size_t len;
  153. bool issued_to_hw;
  154. struct pt_cmd pt_cmd;
  155. };
  156. struct pt_dma_chan {
  157. struct virt_dma_chan vc;
  158. struct pt_device *pt;
  159. };
  160. struct pt_cmd_queue {
  161. struct pt_device *pt;
  162. /* Queue dma pool */
  163. struct dma_pool *dma_pool;
  164. /* Queue base address (not neccessarily aligned)*/
  165. struct ptdma_desc *qbase;
  166. /* Aligned queue start address (per requirement) */
  167. spinlock_t q_lock ____cacheline_aligned;
  168. unsigned int qidx;
  169. unsigned int qsize;
  170. dma_addr_t qbase_dma;
  171. dma_addr_t qdma_tail;
  172. unsigned int active;
  173. unsigned int suspended;
  174. /* Interrupt flag */
  175. bool int_en;
  176. /* Register addresses for queue */
  177. void __iomem *reg_control;
  178. u32 qcontrol; /* Cached control register */
  179. /* Status values from job */
  180. u32 int_status;
  181. u32 q_status;
  182. u32 q_int_status;
  183. u32 cmd_error;
  184. /* Queue Statistics */
  185. unsigned long total_pt_ops;
  186. } ____cacheline_aligned;
  187. struct pt_device {
  188. struct list_head entry;
  189. unsigned int ord;
  190. char name[MAX_PT_NAME_LEN];
  191. struct device *dev;
  192. /* Bus specific device information */
  193. struct pt_msix *pt_msix;
  194. struct pt_dev_vdata *dev_vdata;
  195. unsigned int pt_irq;
  196. /* I/O area used for device communication */
  197. void __iomem *io_regs;
  198. spinlock_t cmd_lock ____cacheline_aligned;
  199. unsigned int cmd_count;
  200. struct list_head cmd;
  201. /*
  202. * The command queue. This represent the queue available on the
  203. * PTDMA that are available for processing cmds
  204. */
  205. struct pt_cmd_queue cmd_q;
  206. /* Support for the DMA Engine capabilities */
  207. struct dma_device dma_dev;
  208. struct pt_dma_chan *pt_dma_chan;
  209. struct kmem_cache *dma_cmd_cache;
  210. struct kmem_cache *dma_desc_cache;
  211. wait_queue_head_t lsb_queue;
  212. /* Device Statistics */
  213. unsigned long total_interrupts;
  214. struct pt_tasklet_data tdata;
  215. };
  216. /*
  217. * descriptor for PTDMA commands
  218. * 8 32-bit words:
  219. * word 0: function; engine; control bits
  220. * word 1: length of source data
  221. * word 2: low 32 bits of source pointer
  222. * word 3: upper 16 bits of source pointer; source memory type
  223. * word 4: low 32 bits of destination pointer
  224. * word 5: upper 16 bits of destination pointer; destination memory type
  225. * word 6: reserved 32 bits
  226. * word 7: reserved 32 bits
  227. */
  228. #define DWORD0_SOC BIT(0)
  229. #define DWORD0_IOC BIT(1)
  230. struct dword3 {
  231. unsigned int src_hi:16;
  232. unsigned int src_mem:2;
  233. unsigned int lsb_cxt_id:8;
  234. unsigned int rsvd1:5;
  235. unsigned int fixed:1;
  236. };
  237. struct dword5 {
  238. unsigned int dst_hi:16;
  239. unsigned int dst_mem:2;
  240. unsigned int rsvd1:13;
  241. unsigned int fixed:1;
  242. };
  243. struct ptdma_desc {
  244. u32 dw0;
  245. u32 length;
  246. u32 src_lo;
  247. struct dword3 dw3;
  248. u32 dst_lo;
  249. struct dword5 dw5;
  250. __le32 rsvd1;
  251. __le32 rsvd2;
  252. };
  253. /* Structure to hold PT device data */
  254. struct pt_dev_vdata {
  255. const unsigned int bar;
  256. };
  257. int pt_dmaengine_register(struct pt_device *pt);
  258. void pt_dmaengine_unregister(struct pt_device *pt);
  259. void ptdma_debugfs_setup(struct pt_device *pt);
  260. int pt_core_init(struct pt_device *pt);
  261. void pt_core_destroy(struct pt_device *pt);
  262. int pt_core_perform_passthru(struct pt_cmd_queue *cmd_q,
  263. struct pt_passthru_engine *pt_engine);
  264. void pt_check_status_trans(struct pt_device *pt, struct pt_cmd_queue *cmd_q);
  265. void pt_start_queue(struct pt_cmd_queue *cmd_q);
  266. void pt_stop_queue(struct pt_cmd_queue *cmd_q);
  267. static inline void pt_core_disable_queue_interrupts(struct pt_device *pt)
  268. {
  269. iowrite32(0, pt->cmd_q.reg_control + 0x000C);
  270. }
  271. static inline void pt_core_enable_queue_interrupts(struct pt_device *pt)
  272. {
  273. iowrite32(SUPPORTED_INTERRUPTS, pt->cmd_q.reg_control + 0x000C);
  274. }
  275. #endif