mic_x100_dma.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2014 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * 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. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel MIC X100 DMA Driver.
  19. *
  20. * Adapted from IOAT dma driver.
  21. */
  22. #ifndef _MIC_X100_DMA_H_
  23. #define _MIC_X100_DMA_H_
  24. #include <linux/kernel.h>
  25. #include <linux/delay.h>
  26. #include <linux/sched.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/slab.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/mic_bus.h>
  31. #include "dmaengine.h"
  32. /*
  33. * MIC has a total of 8 dma channels.
  34. * Four channels are assigned for host SW use & the remaining for MIC SW.
  35. * MIC DMA transfer size & addresses need to be 64 byte aligned.
  36. */
  37. #define MIC_DMA_MAX_NUM_CHAN 8
  38. #define MIC_DMA_NUM_CHAN 4
  39. #define MIC_DMA_ALIGN_SHIFT DMAENGINE_ALIGN_64_BYTES
  40. #define MIC_DMA_ALIGN_BYTES (1 << MIC_DMA_ALIGN_SHIFT)
  41. #define MIC_DMA_DESC_RX_SIZE (128 * 1024 - 4)
  42. /*
  43. * Register descriptions
  44. * All the registers are 32 bit registers.
  45. * DCR is a global register and all others are per-channel.
  46. * DCR - bits 0, 2, 4, 6, 8, 10, 12, 14 - enable bits for channels 0 to 7
  47. * bits 1, 3, 5, 7, 9, 11, 13, 15 - owner bits for channels 0 to 7
  48. * DCAR - bit 24 & 25 interrupt masks for mic owned & host owned channels
  49. * DHPR - head of the descriptor ring updated by s/w
  50. * DTPR - tail of the descriptor ring updated by h/w
  51. * DRAR_LO - lower 32 bits of descriptor ring's mic address
  52. * DRAR_HI - 3:0 - remaining 4 bits of descriptor ring's mic address
  53. * 20:4 descriptor ring size
  54. * 25:21 mic smpt entry number
  55. * DSTAT - 16:0 h/w completion count; 31:28 dma engine status
  56. * DCHERR - this register is non-zero on error
  57. * DCHERRMSK - interrupt mask register
  58. */
  59. #define MIC_DMA_HW_CMP_CNT_MASK 0x1ffff
  60. #define MIC_DMA_CHAN_QUIESCE 0x20000000
  61. #define MIC_DMA_SBOX_BASE 0x00010000
  62. #define MIC_DMA_SBOX_DCR 0x0000A280
  63. #define MIC_DMA_SBOX_CH_BASE 0x0001A000
  64. #define MIC_DMA_SBOX_CHAN_OFF 0x40
  65. #define MIC_DMA_SBOX_DCAR_IM0 (0x1 << 24)
  66. #define MIC_DMA_SBOX_DCAR_IM1 (0x1 << 25)
  67. #define MIC_DMA_SBOX_DRARHI_SYS_MASK (0x1 << 26)
  68. #define MIC_DMA_REG_DCAR 0
  69. #define MIC_DMA_REG_DHPR 4
  70. #define MIC_DMA_REG_DTPR 8
  71. #define MIC_DMA_REG_DRAR_LO 20
  72. #define MIC_DMA_REG_DRAR_HI 24
  73. #define MIC_DMA_REG_DSTAT 32
  74. #define MIC_DMA_REG_DCHERR 44
  75. #define MIC_DMA_REG_DCHERRMSK 48
  76. /* HW dma desc */
  77. struct mic_dma_desc {
  78. u64 qw0;
  79. u64 qw1;
  80. };
  81. enum mic_dma_chan_owner {
  82. MIC_DMA_CHAN_MIC = 0,
  83. MIC_DMA_CHAN_HOST
  84. };
  85. /*
  86. * mic_dma_chan - channel specific information
  87. * @ch_num: channel number
  88. * @owner: owner of this channel
  89. * @last_tail: cached value of descriptor ring tail
  90. * @head: index of next descriptor in desc_ring
  91. * @issued: hardware notification point
  92. * @submitted: index that will be used to submit descriptors to h/w
  93. * @api_ch: dma engine api channel
  94. * @desc_ring: dma descriptor ring
  95. * @desc_ring_micpa: mic physical address of desc_ring
  96. * @status_dest: destination for status (fence) descriptor
  97. * @status_dest_micpa: mic address for status_dest,
  98. * DMA controller uses this address
  99. * @tx_array: array of async_tx
  100. * @cleanup_lock: lock held when processing completed tx
  101. * @prep_lock: lock held in prep_memcpy & released in tx_submit
  102. * @issue_lock: lock used to synchronize writes to head
  103. * @cookie: mic_irq cookie used with mic irq request
  104. */
  105. struct mic_dma_chan {
  106. int ch_num;
  107. enum mic_dma_chan_owner owner;
  108. u32 last_tail;
  109. u32 head;
  110. u32 issued;
  111. u32 submitted;
  112. struct dma_chan api_ch;
  113. struct mic_dma_desc *desc_ring;
  114. dma_addr_t desc_ring_micpa;
  115. u64 *status_dest;
  116. dma_addr_t status_dest_micpa;
  117. struct dma_async_tx_descriptor *tx_array;
  118. spinlock_t cleanup_lock;
  119. spinlock_t prep_lock;
  120. spinlock_t issue_lock;
  121. struct mic_irq *cookie;
  122. };
  123. /*
  124. * struct mic_dma_device - per mic device
  125. * @mic_ch: dma channels
  126. * @dma_dev: underlying dma device
  127. * @mbdev: mic bus dma device
  128. * @mmio: virtual address of the mmio space
  129. * @dbg_dir: debugfs directory
  130. * @start_ch: first channel number that can be used
  131. * @max_xfer_size: maximum transfer size per dma descriptor
  132. */
  133. struct mic_dma_device {
  134. struct mic_dma_chan mic_ch[MIC_DMA_MAX_NUM_CHAN];
  135. struct dma_device dma_dev;
  136. struct mbus_device *mbdev;
  137. void __iomem *mmio;
  138. struct dentry *dbg_dir;
  139. int start_ch;
  140. size_t max_xfer_size;
  141. };
  142. static inline struct mic_dma_chan *to_mic_dma_chan(struct dma_chan *ch)
  143. {
  144. return container_of(ch, struct mic_dma_chan, api_ch);
  145. }
  146. static inline struct mic_dma_device *to_mic_dma_dev(struct mic_dma_chan *ch)
  147. {
  148. return
  149. container_of((const typeof(((struct mic_dma_device *)0)->mic_ch)*)
  150. (ch - ch->ch_num), struct mic_dma_device, mic_ch);
  151. }
  152. static inline struct mbus_device *to_mbus_device(struct mic_dma_chan *ch)
  153. {
  154. return to_mic_dma_dev(ch)->mbdev;
  155. }
  156. static inline struct mbus_hw_ops *to_mbus_hw_ops(struct mic_dma_chan *ch)
  157. {
  158. return to_mbus_device(ch)->hw_ops;
  159. }
  160. static inline struct device *mic_dma_ch_to_device(struct mic_dma_chan *ch)
  161. {
  162. return to_mic_dma_dev(ch)->dma_dev.dev;
  163. }
  164. static inline void __iomem *mic_dma_chan_to_mmio(struct mic_dma_chan *ch)
  165. {
  166. return to_mic_dma_dev(ch)->mmio;
  167. }
  168. static inline u32 mic_dma_read_reg(struct mic_dma_chan *ch, u32 reg)
  169. {
  170. return ioread32(mic_dma_chan_to_mmio(ch) + MIC_DMA_SBOX_CH_BASE +
  171. ch->ch_num * MIC_DMA_SBOX_CHAN_OFF + reg);
  172. }
  173. static inline void mic_dma_write_reg(struct mic_dma_chan *ch, u32 reg, u32 val)
  174. {
  175. iowrite32(val, mic_dma_chan_to_mmio(ch) + MIC_DMA_SBOX_CH_BASE +
  176. ch->ch_num * MIC_DMA_SBOX_CHAN_OFF + reg);
  177. }
  178. static inline u32 mic_dma_mmio_read(struct mic_dma_chan *ch, u32 offset)
  179. {
  180. return ioread32(mic_dma_chan_to_mmio(ch) + offset);
  181. }
  182. static inline void mic_dma_mmio_write(struct mic_dma_chan *ch, u32 val,
  183. u32 offset)
  184. {
  185. iowrite32(val, mic_dma_chan_to_mmio(ch) + offset);
  186. }
  187. static inline u32 mic_dma_read_cmp_cnt(struct mic_dma_chan *ch)
  188. {
  189. return mic_dma_read_reg(ch, MIC_DMA_REG_DSTAT) &
  190. MIC_DMA_HW_CMP_CNT_MASK;
  191. }
  192. static inline void mic_dma_chan_set_owner(struct mic_dma_chan *ch)
  193. {
  194. u32 dcr = mic_dma_mmio_read(ch, MIC_DMA_SBOX_BASE + MIC_DMA_SBOX_DCR);
  195. u32 chan_num = ch->ch_num;
  196. dcr = (dcr & ~(0x1 << (chan_num * 2))) | (ch->owner << (chan_num * 2));
  197. mic_dma_mmio_write(ch, dcr, MIC_DMA_SBOX_BASE + MIC_DMA_SBOX_DCR);
  198. }
  199. static inline void mic_dma_enable_chan(struct mic_dma_chan *ch)
  200. {
  201. u32 dcr = mic_dma_mmio_read(ch, MIC_DMA_SBOX_BASE + MIC_DMA_SBOX_DCR);
  202. dcr |= 2 << (ch->ch_num << 1);
  203. mic_dma_mmio_write(ch, dcr, MIC_DMA_SBOX_BASE + MIC_DMA_SBOX_DCR);
  204. }
  205. static inline void mic_dma_disable_chan(struct mic_dma_chan *ch)
  206. {
  207. u32 dcr = mic_dma_mmio_read(ch, MIC_DMA_SBOX_BASE + MIC_DMA_SBOX_DCR);
  208. dcr &= ~(2 << (ch->ch_num << 1));
  209. mic_dma_mmio_write(ch, dcr, MIC_DMA_SBOX_BASE + MIC_DMA_SBOX_DCR);
  210. }
  211. static void mic_dma_chan_set_desc_ring(struct mic_dma_chan *ch)
  212. {
  213. u32 drar_hi;
  214. dma_addr_t desc_ring_micpa = ch->desc_ring_micpa;
  215. drar_hi = (MIC_DMA_DESC_RX_SIZE & 0x1ffff) << 4;
  216. if (MIC_DMA_CHAN_MIC == ch->owner) {
  217. drar_hi |= (desc_ring_micpa >> 32) & 0xf;
  218. } else {
  219. drar_hi |= MIC_DMA_SBOX_DRARHI_SYS_MASK;
  220. drar_hi |= ((desc_ring_micpa >> 34)
  221. & 0x1f) << 21;
  222. drar_hi |= (desc_ring_micpa >> 32) & 0x3;
  223. }
  224. mic_dma_write_reg(ch, MIC_DMA_REG_DRAR_LO, (u32) desc_ring_micpa);
  225. mic_dma_write_reg(ch, MIC_DMA_REG_DRAR_HI, drar_hi);
  226. }
  227. static inline void mic_dma_chan_mask_intr(struct mic_dma_chan *ch)
  228. {
  229. u32 dcar = mic_dma_read_reg(ch, MIC_DMA_REG_DCAR);
  230. if (MIC_DMA_CHAN_MIC == ch->owner)
  231. dcar |= MIC_DMA_SBOX_DCAR_IM0;
  232. else
  233. dcar |= MIC_DMA_SBOX_DCAR_IM1;
  234. mic_dma_write_reg(ch, MIC_DMA_REG_DCAR, dcar);
  235. }
  236. static inline void mic_dma_chan_unmask_intr(struct mic_dma_chan *ch)
  237. {
  238. u32 dcar = mic_dma_read_reg(ch, MIC_DMA_REG_DCAR);
  239. if (MIC_DMA_CHAN_MIC == ch->owner)
  240. dcar &= ~MIC_DMA_SBOX_DCAR_IM0;
  241. else
  242. dcar &= ~MIC_DMA_SBOX_DCAR_IM1;
  243. mic_dma_write_reg(ch, MIC_DMA_REG_DCAR, dcar);
  244. }
  245. static void mic_dma_ack_interrupt(struct mic_dma_chan *ch)
  246. {
  247. if (MIC_DMA_CHAN_MIC == ch->owner) {
  248. /* HW errata */
  249. mic_dma_chan_mask_intr(ch);
  250. mic_dma_chan_unmask_intr(ch);
  251. }
  252. to_mbus_hw_ops(ch)->ack_interrupt(to_mbus_device(ch), ch->ch_num);
  253. }
  254. #endif