dma.h 13 KB

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