dma.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright(c) 2004 - 2009 Intel Corporation. All rights reserved.
  4. */
  5. #ifndef IOATDMA_H
  6. #define IOATDMA_H
  7. #include <linux/dmaengine.h>
  8. #include <linux/init.h>
  9. #include <linux/dmapool.h>
  10. #include <linux/cache.h>
  11. #include <linux/pci_ids.h>
  12. #include <linux/circ_buf.h>
  13. #include <linux/interrupt.h>
  14. #include "registers.h"
  15. #include "hw.h"
  16. #define IOAT_DMA_VERSION "5.00"
  17. #define IOAT_DMA_DCA_ANY_CPU ~0
  18. #define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, dma_dev)
  19. #define to_dev(ioat_chan) (&(ioat_chan)->ioat_dma->pdev->dev)
  20. #define to_pdev(ioat_chan) ((ioat_chan)->ioat_dma->pdev)
  21. #define chan_num(ch) ((int)((ch)->reg_base - (ch)->ioat_dma->reg_base) / 0x80)
  22. /* ioat hardware assumes at least two sources for raid operations */
  23. #define src_cnt_to_sw(x) ((x) + 2)
  24. #define src_cnt_to_hw(x) ((x) - 2)
  25. #define ndest_to_sw(x) ((x) + 1)
  26. #define ndest_to_hw(x) ((x) - 1)
  27. #define src16_cnt_to_sw(x) ((x) + 9)
  28. #define src16_cnt_to_hw(x) ((x) - 9)
  29. /*
  30. * workaround for IOAT ver.3.0 null descriptor issue
  31. * (channel returns error when size is 0)
  32. */
  33. #define NULL_DESC_BUFFER_SIZE 1
  34. enum ioat_irq_mode {
  35. IOAT_NOIRQ = 0,
  36. IOAT_MSIX,
  37. IOAT_MSI,
  38. IOAT_INTX
  39. };
  40. /**
  41. * struct ioatdma_device - internal representation of a IOAT device
  42. * @pdev: PCI-Express device
  43. * @reg_base: MMIO register space base address
  44. * @completion_pool: DMA buffers for completion ops
  45. * @sed_hw_pool: DMA super descriptor pools
  46. * @dma_dev: embedded struct dma_device
  47. * @version: version of ioatdma device
  48. * @msix_entries: irq handlers
  49. * @idx: per channel data
  50. * @dca: direct cache access context
  51. * @irq_mode: interrupt mode (INTX, MSI, MSIX)
  52. * @cap: read DMA capabilities register
  53. */
  54. struct ioatdma_device {
  55. struct pci_dev *pdev;
  56. void __iomem *reg_base;
  57. struct dma_pool *completion_pool;
  58. #define MAX_SED_POOLS 5
  59. struct dma_pool *sed_hw_pool[MAX_SED_POOLS];
  60. struct dma_device dma_dev;
  61. u8 version;
  62. #define IOAT_MAX_CHANS 4
  63. struct msix_entry msix_entries[IOAT_MAX_CHANS];
  64. struct ioatdma_chan *idx[IOAT_MAX_CHANS];
  65. struct dca_provider *dca;
  66. enum ioat_irq_mode irq_mode;
  67. u32 cap;
  68. int chancnt;
  69. /* shadow version for CB3.3 chan reset errata workaround */
  70. u64 msixtba0;
  71. u64 msixdata0;
  72. u32 msixpba;
  73. };
  74. #define IOAT_MAX_ORDER 16
  75. #define IOAT_MAX_DESCS (1 << IOAT_MAX_ORDER)
  76. #define IOAT_CHUNK_SIZE (SZ_512K)
  77. #define IOAT_DESCS_PER_CHUNK (IOAT_CHUNK_SIZE / IOAT_DESC_SZ)
  78. struct ioat_descs {
  79. void *virt;
  80. dma_addr_t hw;
  81. };
  82. struct ioatdma_chan {
  83. struct dma_chan dma_chan;
  84. void __iomem *reg_base;
  85. dma_addr_t last_completion;
  86. spinlock_t cleanup_lock;
  87. unsigned long state;
  88. #define IOAT_CHAN_DOWN 0
  89. #define IOAT_COMPLETION_ACK 1
  90. #define IOAT_RESET_PENDING 2
  91. #define IOAT_KOBJ_INIT_FAIL 3
  92. #define IOAT_RUN 5
  93. #define IOAT_CHAN_ACTIVE 6
  94. struct timer_list timer;
  95. #define RESET_DELAY msecs_to_jiffies(100)
  96. struct ioatdma_device *ioat_dma;
  97. dma_addr_t completion_dma;
  98. u64 *completion;
  99. struct tasklet_struct cleanup_task;
  100. struct kobject kobj;
  101. /* ioat v2 / v3 channel attributes
  102. * @xfercap_log; log2 of channel max transfer length (for fast division)
  103. * @head: allocated index
  104. * @issued: hardware notification point
  105. * @tail: cleanup index
  106. * @dmacount: identical to 'head' except for occasionally resetting to zero
  107. * @alloc_order: log2 of the number of allocated descriptors
  108. * @produce: number of descriptors to produce at submit time
  109. * @ring: software ring buffer implementation of hardware ring
  110. * @prep_lock: serializes descriptor preparation (producers)
  111. */
  112. size_t xfercap_log;
  113. u16 head;
  114. u16 issued;
  115. u16 tail;
  116. u16 dmacount;
  117. u16 alloc_order;
  118. u16 produce;
  119. struct ioat_ring_ent **ring;
  120. spinlock_t prep_lock;
  121. struct ioat_descs descs[IOAT_MAX_DESCS / IOAT_DESCS_PER_CHUNK];
  122. int desc_chunks;
  123. int intr_coalesce;
  124. int prev_intr_coalesce;
  125. };
  126. struct ioat_sysfs_entry {
  127. struct attribute attr;
  128. ssize_t (*show)(struct dma_chan *, char *);
  129. ssize_t (*store)(struct dma_chan *, const char *, size_t);
  130. };
  131. /**
  132. * struct ioat_sed_ent - wrapper around super extended hardware descriptor
  133. * @hw: hardware SED
  134. * @dma: dma address for the SED
  135. * @parent: point to the dma descriptor that's the parent
  136. * @hw_pool: descriptor pool index
  137. */
  138. struct ioat_sed_ent {
  139. struct ioat_sed_raw_descriptor *hw;
  140. dma_addr_t dma;
  141. struct ioat_ring_ent *parent;
  142. unsigned int hw_pool;
  143. };
  144. /**
  145. * struct ioat_ring_ent - wrapper around hardware descriptor
  146. * @hw: hardware DMA descriptor (for memcpy)
  147. * @xor: hardware xor descriptor
  148. * @xor_ex: hardware xor extension descriptor
  149. * @pq: hardware pq descriptor
  150. * @pq_ex: hardware pq extension descriptor
  151. * @pqu: hardware pq update descriptor
  152. * @raw: hardware raw (un-typed) descriptor
  153. * @txd: the generic software descriptor for all engines
  154. * @len: total transaction length for unmap
  155. * @result: asynchronous result of validate operations
  156. * @id: identifier for debug
  157. * @sed: pointer to super extended descriptor sw desc
  158. */
  159. struct ioat_ring_ent {
  160. union {
  161. struct ioat_dma_descriptor *hw;
  162. struct ioat_xor_descriptor *xor;
  163. struct ioat_xor_ext_descriptor *xor_ex;
  164. struct ioat_pq_descriptor *pq;
  165. struct ioat_pq_ext_descriptor *pq_ex;
  166. struct ioat_pq_update_descriptor *pqu;
  167. struct ioat_raw_descriptor *raw;
  168. };
  169. size_t len;
  170. struct dma_async_tx_descriptor txd;
  171. enum sum_check_flags *result;
  172. #ifdef DEBUG
  173. int id;
  174. #endif
  175. struct ioat_sed_ent *sed;
  176. };
  177. extern const struct sysfs_ops ioat_sysfs_ops;
  178. extern struct ioat_sysfs_entry ioat_version_attr;
  179. extern struct ioat_sysfs_entry ioat_cap_attr;
  180. extern int ioat_pending_level;
  181. extern struct kobj_type ioat_ktype;
  182. extern struct kmem_cache *ioat_cache;
  183. extern struct kmem_cache *ioat_sed_cache;
  184. static inline struct ioatdma_chan *to_ioat_chan(struct dma_chan *c)
  185. {
  186. return container_of(c, struct ioatdma_chan, dma_chan);
  187. }
  188. /* wrapper around hardware descriptor format + additional software fields */
  189. #ifdef DEBUG
  190. #define set_desc_id(desc, i) ((desc)->id = (i))
  191. #define desc_id(desc) ((desc)->id)
  192. #else
  193. #define set_desc_id(desc, i)
  194. #define desc_id(desc) (0)
  195. #endif
  196. static inline void
  197. __dump_desc_dbg(struct ioatdma_chan *ioat_chan, struct ioat_dma_descriptor *hw,
  198. struct dma_async_tx_descriptor *tx, int id)
  199. {
  200. struct device *dev = to_dev(ioat_chan);
  201. dev_dbg(dev, "desc[%d]: (%#llx->%#llx) cookie: %d flags: %#x"
  202. " ctl: %#10.8x (op: %#x int_en: %d compl: %d)\n", id,
  203. (unsigned long long) tx->phys,
  204. (unsigned long long) hw->next, tx->cookie, tx->flags,
  205. hw->ctl, hw->ctl_f.op, hw->ctl_f.int_en, hw->ctl_f.compl_write);
  206. }
  207. #define dump_desc_dbg(c, d) \
  208. ({ if (d) __dump_desc_dbg(c, d->hw, &d->txd, desc_id(d)); 0; })
  209. static inline struct ioatdma_chan *
  210. ioat_chan_by_index(struct ioatdma_device *ioat_dma, int index)
  211. {
  212. return ioat_dma->idx[index];
  213. }
  214. static inline u64 ioat_chansts(struct ioatdma_chan *ioat_chan)
  215. {
  216. return readq(ioat_chan->reg_base + IOAT_CHANSTS_OFFSET);
  217. }
  218. static inline u64 ioat_chansts_to_addr(u64 status)
  219. {
  220. return status & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  221. }
  222. static inline u32 ioat_chanerr(struct ioatdma_chan *ioat_chan)
  223. {
  224. return readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  225. }
  226. static inline void ioat_suspend(struct ioatdma_chan *ioat_chan)
  227. {
  228. u8 ver = ioat_chan->ioat_dma->version;
  229. writeb(IOAT_CHANCMD_SUSPEND,
  230. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
  231. }
  232. static inline void ioat_reset(struct ioatdma_chan *ioat_chan)
  233. {
  234. u8 ver = ioat_chan->ioat_dma->version;
  235. writeb(IOAT_CHANCMD_RESET,
  236. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
  237. }
  238. static inline bool ioat_reset_pending(struct ioatdma_chan *ioat_chan)
  239. {
  240. u8 ver = ioat_chan->ioat_dma->version;
  241. u8 cmd;
  242. cmd = readb(ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
  243. return (cmd & IOAT_CHANCMD_RESET) == IOAT_CHANCMD_RESET;
  244. }
  245. static inline bool is_ioat_active(unsigned long status)
  246. {
  247. return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_ACTIVE);
  248. }
  249. static inline bool is_ioat_idle(unsigned long status)
  250. {
  251. return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_DONE);
  252. }
  253. static inline bool is_ioat_halted(unsigned long status)
  254. {
  255. return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_HALTED);
  256. }
  257. static inline bool is_ioat_suspended(unsigned long status)
  258. {
  259. return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_SUSPENDED);
  260. }
  261. /* channel was fatally programmed */
  262. static inline bool is_ioat_bug(unsigned long err)
  263. {
  264. return !!err;
  265. }
  266. static inline u32 ioat_ring_size(struct ioatdma_chan *ioat_chan)
  267. {
  268. return 1 << ioat_chan->alloc_order;
  269. }
  270. /* count of descriptors in flight with the engine */
  271. static inline u16 ioat_ring_active(struct ioatdma_chan *ioat_chan)
  272. {
  273. return CIRC_CNT(ioat_chan->head, ioat_chan->tail,
  274. ioat_ring_size(ioat_chan));
  275. }
  276. /* count of descriptors pending submission to hardware */
  277. static inline u16 ioat_ring_pending(struct ioatdma_chan *ioat_chan)
  278. {
  279. return CIRC_CNT(ioat_chan->head, ioat_chan->issued,
  280. ioat_ring_size(ioat_chan));
  281. }
  282. static inline u32 ioat_ring_space(struct ioatdma_chan *ioat_chan)
  283. {
  284. return ioat_ring_size(ioat_chan) - ioat_ring_active(ioat_chan);
  285. }
  286. static inline u16
  287. ioat_xferlen_to_descs(struct ioatdma_chan *ioat_chan, size_t len)
  288. {
  289. u16 num_descs = len >> ioat_chan->xfercap_log;
  290. num_descs += !!(len & ((1 << ioat_chan->xfercap_log) - 1));
  291. return num_descs;
  292. }
  293. static inline struct ioat_ring_ent *
  294. ioat_get_ring_ent(struct ioatdma_chan *ioat_chan, u16 idx)
  295. {
  296. return ioat_chan->ring[idx & (ioat_ring_size(ioat_chan) - 1)];
  297. }
  298. static inline void
  299. ioat_set_chainaddr(struct ioatdma_chan *ioat_chan, u64 addr)
  300. {
  301. writel(addr & 0x00000000FFFFFFFF,
  302. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
  303. writel(addr >> 32,
  304. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
  305. }
  306. /* IOAT Prep functions */
  307. struct dma_async_tx_descriptor *
  308. ioat_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest,
  309. dma_addr_t dma_src, size_t len, unsigned long flags);
  310. struct dma_async_tx_descriptor *
  311. ioat_prep_interrupt_lock(struct dma_chan *c, unsigned long flags);
  312. struct dma_async_tx_descriptor *
  313. ioat_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  314. unsigned int src_cnt, size_t len, unsigned long flags);
  315. struct dma_async_tx_descriptor *
  316. ioat_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
  317. unsigned int src_cnt, size_t len,
  318. enum sum_check_flags *result, unsigned long flags);
  319. struct dma_async_tx_descriptor *
  320. ioat_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  321. unsigned int src_cnt, const unsigned char *scf, size_t len,
  322. unsigned long flags);
  323. struct dma_async_tx_descriptor *
  324. ioat_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  325. unsigned int src_cnt, const unsigned char *scf, size_t len,
  326. enum sum_check_flags *pqres, unsigned long flags);
  327. struct dma_async_tx_descriptor *
  328. ioat_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  329. unsigned int src_cnt, size_t len, unsigned long flags);
  330. struct dma_async_tx_descriptor *
  331. ioat_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
  332. unsigned int src_cnt, size_t len,
  333. enum sum_check_flags *result, unsigned long flags);
  334. /* IOAT Operation functions */
  335. irqreturn_t ioat_dma_do_interrupt(int irq, void *data);
  336. irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data);
  337. struct ioat_ring_ent **
  338. ioat_alloc_ring(struct dma_chan *c, int order, gfp_t flags);
  339. void ioat_start_null_desc(struct ioatdma_chan *ioat_chan);
  340. void ioat_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan);
  341. int ioat_reset_hw(struct ioatdma_chan *ioat_chan);
  342. enum dma_status
  343. ioat_tx_status(struct dma_chan *c, dma_cookie_t cookie,
  344. struct dma_tx_state *txstate);
  345. void ioat_cleanup_event(struct tasklet_struct *t);
  346. void ioat_timer_event(struct timer_list *t);
  347. int ioat_check_space_lock(struct ioatdma_chan *ioat_chan, int num_descs);
  348. void ioat_issue_pending(struct dma_chan *chan);
  349. /* IOAT Init functions */
  350. bool is_bwd_ioat(struct pci_dev *pdev);
  351. struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase);
  352. void ioat_kobject_add(struct ioatdma_device *ioat_dma, struct kobj_type *type);
  353. void ioat_kobject_del(struct ioatdma_device *ioat_dma);
  354. int ioat_dma_setup_interrupts(struct ioatdma_device *ioat_dma);
  355. void ioat_stop(struct ioatdma_chan *ioat_chan);
  356. #endif /* IOATDMA_H */