ioat_dma.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2004 - 2007 Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  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. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. */
  22. /*
  23. * This driver supports an Intel I/OAT DMA engine, which does asynchronous
  24. * copy operations.
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include "ioatdma.h"
  34. #include "ioatdma_registers.h"
  35. #include "ioatdma_hw.h"
  36. #define INITIAL_IOAT_DESC_COUNT 128
  37. #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
  38. #define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, common)
  39. #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
  40. #define tx_to_ioat_desc(tx) container_of(tx, struct ioat_desc_sw, async_tx)
  41. /* internal functions */
  42. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan);
  43. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
  44. static struct ioat_desc_sw *
  45. ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
  46. static inline struct ioat_dma_chan *ioat_lookup_chan_by_index(
  47. struct ioatdma_device *device,
  48. int index)
  49. {
  50. return device->idx[index];
  51. }
  52. /**
  53. * ioat_dma_do_interrupt - handler used for single vector interrupt mode
  54. * @irq: interrupt id
  55. * @data: interrupt data
  56. */
  57. static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
  58. {
  59. struct ioatdma_device *instance = data;
  60. struct ioat_dma_chan *ioat_chan;
  61. unsigned long attnstatus;
  62. int bit;
  63. u8 intrctrl;
  64. intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
  65. if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
  66. return IRQ_NONE;
  67. if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
  68. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  69. return IRQ_NONE;
  70. }
  71. attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
  72. for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
  73. ioat_chan = ioat_lookup_chan_by_index(instance, bit);
  74. tasklet_schedule(&ioat_chan->cleanup_task);
  75. }
  76. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  77. return IRQ_HANDLED;
  78. }
  79. /**
  80. * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
  81. * @irq: interrupt id
  82. * @data: interrupt data
  83. */
  84. static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
  85. {
  86. struct ioat_dma_chan *ioat_chan = data;
  87. tasklet_schedule(&ioat_chan->cleanup_task);
  88. return IRQ_HANDLED;
  89. }
  90. static void ioat_dma_cleanup_tasklet(unsigned long data);
  91. /**
  92. * ioat_dma_enumerate_channels - find and initialize the device's channels
  93. * @device: the device to be enumerated
  94. */
  95. static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
  96. {
  97. u8 xfercap_scale;
  98. u32 xfercap;
  99. int i;
  100. struct ioat_dma_chan *ioat_chan;
  101. device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  102. xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  103. xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
  104. for (i = 0; i < device->common.chancnt; i++) {
  105. ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
  106. if (!ioat_chan) {
  107. device->common.chancnt = i;
  108. break;
  109. }
  110. ioat_chan->device = device;
  111. ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
  112. ioat_chan->xfercap = xfercap;
  113. spin_lock_init(&ioat_chan->cleanup_lock);
  114. spin_lock_init(&ioat_chan->desc_lock);
  115. INIT_LIST_HEAD(&ioat_chan->free_desc);
  116. INIT_LIST_HEAD(&ioat_chan->used_desc);
  117. /* This should be made common somewhere in dmaengine.c */
  118. ioat_chan->common.device = &device->common;
  119. list_add_tail(&ioat_chan->common.device_node,
  120. &device->common.channels);
  121. device->idx[i] = ioat_chan;
  122. tasklet_init(&ioat_chan->cleanup_task,
  123. ioat_dma_cleanup_tasklet,
  124. (unsigned long) ioat_chan);
  125. tasklet_disable(&ioat_chan->cleanup_task);
  126. }
  127. return device->common.chancnt;
  128. }
  129. static void ioat_set_src(dma_addr_t addr,
  130. struct dma_async_tx_descriptor *tx,
  131. int index)
  132. {
  133. tx_to_ioat_desc(tx)->src = addr;
  134. }
  135. static void ioat_set_dest(dma_addr_t addr,
  136. struct dma_async_tx_descriptor *tx,
  137. int index)
  138. {
  139. tx_to_ioat_desc(tx)->dst = addr;
  140. }
  141. static dma_cookie_t ioat_tx_submit(struct dma_async_tx_descriptor *tx)
  142. {
  143. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  144. struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
  145. struct ioat_desc_sw *prev, *new;
  146. struct ioat_dma_descriptor *hw;
  147. int append = 0;
  148. dma_cookie_t cookie;
  149. LIST_HEAD(new_chain);
  150. u32 copy;
  151. size_t len;
  152. dma_addr_t src, dst;
  153. int orig_ack;
  154. unsigned int desc_count = 0;
  155. /* src and dest and len are stored in the initial descriptor */
  156. len = first->len;
  157. src = first->src;
  158. dst = first->dst;
  159. orig_ack = first->async_tx.ack;
  160. new = first;
  161. spin_lock_bh(&ioat_chan->desc_lock);
  162. prev = to_ioat_desc(ioat_chan->used_desc.prev);
  163. prefetch(prev->hw);
  164. do {
  165. copy = min((u32) len, ioat_chan->xfercap);
  166. new->async_tx.ack = 1;
  167. hw = new->hw;
  168. hw->size = copy;
  169. hw->ctl = 0;
  170. hw->src_addr = src;
  171. hw->dst_addr = dst;
  172. hw->next = 0;
  173. /* chain together the physical address list for the HW */
  174. wmb();
  175. prev->hw->next = (u64) new->async_tx.phys;
  176. len -= copy;
  177. dst += copy;
  178. src += copy;
  179. list_add_tail(&new->node, &new_chain);
  180. desc_count++;
  181. prev = new;
  182. } while (len && (new = ioat_dma_get_next_descriptor(ioat_chan)));
  183. hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  184. if (new->async_tx.callback) {
  185. hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
  186. if (first != new) {
  187. /* move callback into to last desc */
  188. new->async_tx.callback = first->async_tx.callback;
  189. new->async_tx.callback_param
  190. = first->async_tx.callback_param;
  191. first->async_tx.callback = NULL;
  192. first->async_tx.callback_param = NULL;
  193. }
  194. }
  195. new->tx_cnt = desc_count;
  196. new->async_tx.ack = orig_ack; /* client is in control of this ack */
  197. /* store the original values for use in later cleanup */
  198. if (new != first) {
  199. new->src = first->src;
  200. new->dst = first->dst;
  201. new->len = first->len;
  202. }
  203. /* cookie incr and addition to used_list must be atomic */
  204. cookie = ioat_chan->common.cookie;
  205. cookie++;
  206. if (cookie < 0)
  207. cookie = 1;
  208. ioat_chan->common.cookie = new->async_tx.cookie = cookie;
  209. /* write address into NextDescriptor field of last desc in chain */
  210. to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
  211. first->async_tx.phys;
  212. __list_splice(&new_chain, ioat_chan->used_desc.prev);
  213. ioat_chan->pending += desc_count;
  214. if (ioat_chan->pending >= 4) {
  215. append = 1;
  216. ioat_chan->pending = 0;
  217. }
  218. spin_unlock_bh(&ioat_chan->desc_lock);
  219. if (append)
  220. writeb(IOAT_CHANCMD_APPEND,
  221. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  222. return cookie;
  223. }
  224. static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
  225. struct ioat_dma_chan *ioat_chan,
  226. gfp_t flags)
  227. {
  228. struct ioat_dma_descriptor *desc;
  229. struct ioat_desc_sw *desc_sw;
  230. struct ioatdma_device *ioatdma_device;
  231. dma_addr_t phys;
  232. ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
  233. desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
  234. if (unlikely(!desc))
  235. return NULL;
  236. desc_sw = kzalloc(sizeof(*desc_sw), flags);
  237. if (unlikely(!desc_sw)) {
  238. pci_pool_free(ioatdma_device->dma_pool, desc, phys);
  239. return NULL;
  240. }
  241. memset(desc, 0, sizeof(*desc));
  242. dma_async_tx_descriptor_init(&desc_sw->async_tx, &ioat_chan->common);
  243. desc_sw->async_tx.tx_set_src = ioat_set_src;
  244. desc_sw->async_tx.tx_set_dest = ioat_set_dest;
  245. desc_sw->async_tx.tx_submit = ioat_tx_submit;
  246. INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
  247. desc_sw->hw = desc;
  248. desc_sw->async_tx.phys = phys;
  249. return desc_sw;
  250. }
  251. /* returns the actual number of allocated descriptors */
  252. static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
  253. {
  254. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  255. struct ioat_desc_sw *desc = NULL;
  256. u16 chanctrl;
  257. u32 chanerr;
  258. int i;
  259. LIST_HEAD(tmp_list);
  260. /* have we already been set up? */
  261. if (!list_empty(&ioat_chan->free_desc))
  262. return INITIAL_IOAT_DESC_COUNT;
  263. /* Setup register to interrupt and write completion status on error */
  264. chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
  265. IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
  266. IOAT_CHANCTRL_ERR_COMPLETION_EN;
  267. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  268. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  269. if (chanerr) {
  270. dev_err(&ioat_chan->device->pdev->dev,
  271. "CHANERR = %x, clearing\n", chanerr);
  272. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  273. }
  274. /* Allocate descriptors */
  275. for (i = 0; i < INITIAL_IOAT_DESC_COUNT; i++) {
  276. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  277. if (!desc) {
  278. dev_err(&ioat_chan->device->pdev->dev,
  279. "Only %d initial descriptors\n", i);
  280. break;
  281. }
  282. list_add_tail(&desc->node, &tmp_list);
  283. }
  284. spin_lock_bh(&ioat_chan->desc_lock);
  285. list_splice(&tmp_list, &ioat_chan->free_desc);
  286. spin_unlock_bh(&ioat_chan->desc_lock);
  287. /* allocate a completion writeback area */
  288. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  289. ioat_chan->completion_virt =
  290. pci_pool_alloc(ioat_chan->device->completion_pool,
  291. GFP_KERNEL,
  292. &ioat_chan->completion_addr);
  293. memset(ioat_chan->completion_virt, 0,
  294. sizeof(*ioat_chan->completion_virt));
  295. writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
  296. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  297. writel(((u64) ioat_chan->completion_addr) >> 32,
  298. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  299. tasklet_enable(&ioat_chan->cleanup_task);
  300. ioat_dma_start_null_desc(ioat_chan);
  301. return i;
  302. }
  303. static void ioat_dma_free_chan_resources(struct dma_chan *chan)
  304. {
  305. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  306. struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
  307. struct ioat_desc_sw *desc, *_desc;
  308. int in_use_descs = 0;
  309. tasklet_disable(&ioat_chan->cleanup_task);
  310. ioat_dma_memcpy_cleanup(ioat_chan);
  311. /* Delay 100ms after reset to allow internal DMA logic to quiesce
  312. * before removing DMA descriptor resources.
  313. */
  314. writeb(IOAT_CHANCMD_RESET, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  315. mdelay(100);
  316. spin_lock_bh(&ioat_chan->desc_lock);
  317. list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
  318. in_use_descs++;
  319. list_del(&desc->node);
  320. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  321. desc->async_tx.phys);
  322. kfree(desc);
  323. }
  324. list_for_each_entry_safe(desc, _desc, &ioat_chan->free_desc, node) {
  325. list_del(&desc->node);
  326. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  327. desc->async_tx.phys);
  328. kfree(desc);
  329. }
  330. spin_unlock_bh(&ioat_chan->desc_lock);
  331. pci_pool_free(ioatdma_device->completion_pool,
  332. ioat_chan->completion_virt,
  333. ioat_chan->completion_addr);
  334. /* one is ok since we left it on there on purpose */
  335. if (in_use_descs > 1)
  336. dev_err(&ioat_chan->device->pdev->dev,
  337. "Freeing %d in use descriptors!\n",
  338. in_use_descs - 1);
  339. ioat_chan->last_completion = ioat_chan->completion_addr = 0;
  340. ioat_chan->pending = 0;
  341. }
  342. /**
  343. * ioat_dma_get_next_descriptor - return the next available descriptor
  344. * @ioat_chan: IOAT DMA channel handle
  345. *
  346. * Gets the next descriptor from the chain, and must be called with the
  347. * channel's desc_lock held. Allocates more descriptors if the channel
  348. * has run out.
  349. */
  350. static struct ioat_desc_sw *
  351. ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  352. {
  353. struct ioat_desc_sw *new = NULL;
  354. if (!list_empty(&ioat_chan->free_desc)) {
  355. new = to_ioat_desc(ioat_chan->free_desc.next);
  356. list_del(&new->node);
  357. } else {
  358. /* try to get another desc */
  359. new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  360. /* will this ever happen? */
  361. /* TODO add upper limit on these */
  362. BUG_ON(!new);
  363. }
  364. prefetch(new->hw);
  365. return new;
  366. }
  367. static struct dma_async_tx_descriptor *ioat_dma_prep_memcpy(
  368. struct dma_chan *chan,
  369. size_t len,
  370. int int_en)
  371. {
  372. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  373. struct ioat_desc_sw *new;
  374. spin_lock_bh(&ioat_chan->desc_lock);
  375. new = ioat_dma_get_next_descriptor(ioat_chan);
  376. new->len = len;
  377. spin_unlock_bh(&ioat_chan->desc_lock);
  378. return new ? &new->async_tx : NULL;
  379. }
  380. /**
  381. * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
  382. * descriptors to hw
  383. * @chan: DMA channel handle
  384. */
  385. static void ioat_dma_memcpy_issue_pending(struct dma_chan *chan)
  386. {
  387. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  388. if (ioat_chan->pending != 0) {
  389. ioat_chan->pending = 0;
  390. writeb(IOAT_CHANCMD_APPEND,
  391. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  392. }
  393. }
  394. static void ioat_dma_cleanup_tasklet(unsigned long data)
  395. {
  396. struct ioat_dma_chan *chan = (void *)data;
  397. ioat_dma_memcpy_cleanup(chan);
  398. writew(IOAT_CHANCTRL_INT_DISABLE,
  399. chan->reg_base + IOAT_CHANCTRL_OFFSET);
  400. }
  401. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
  402. {
  403. unsigned long phys_complete;
  404. struct ioat_desc_sw *desc, *_desc;
  405. dma_cookie_t cookie = 0;
  406. prefetch(ioat_chan->completion_virt);
  407. if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
  408. return;
  409. /* The completion writeback can happen at any time,
  410. so reads by the driver need to be atomic operations
  411. The descriptor physical addresses are limited to 32-bits
  412. when the CPU can only do a 32-bit mov */
  413. #if (BITS_PER_LONG == 64)
  414. phys_complete =
  415. ioat_chan->completion_virt->full
  416. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  417. #else
  418. phys_complete =
  419. ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
  420. #endif
  421. if ((ioat_chan->completion_virt->full
  422. & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
  423. IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
  424. dev_err(&ioat_chan->device->pdev->dev,
  425. "Channel halted, chanerr = %x\n",
  426. readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
  427. /* TODO do something to salvage the situation */
  428. }
  429. if (phys_complete == ioat_chan->last_completion) {
  430. spin_unlock_bh(&ioat_chan->cleanup_lock);
  431. return;
  432. }
  433. cookie = 0;
  434. spin_lock_bh(&ioat_chan->desc_lock);
  435. list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
  436. /*
  437. * Incoming DMA requests may use multiple descriptors, due to
  438. * exceeding xfercap, perhaps. If so, only the last one will
  439. * have a cookie, and require unmapping.
  440. */
  441. if (desc->async_tx.cookie) {
  442. cookie = desc->async_tx.cookie;
  443. /*
  444. * yes we are unmapping both _page and _single alloc'd
  445. * regions with unmap_page. Is this *really* that bad?
  446. */
  447. pci_unmap_page(ioat_chan->device->pdev,
  448. pci_unmap_addr(desc, dst),
  449. pci_unmap_len(desc, len),
  450. PCI_DMA_FROMDEVICE);
  451. pci_unmap_page(ioat_chan->device->pdev,
  452. pci_unmap_addr(desc, src),
  453. pci_unmap_len(desc, len),
  454. PCI_DMA_TODEVICE);
  455. if (desc->async_tx.callback) {
  456. desc->async_tx.callback(
  457. desc->async_tx.callback_param);
  458. desc->async_tx.callback = NULL;
  459. }
  460. }
  461. if (desc->async_tx.phys != phys_complete) {
  462. /*
  463. * a completed entry, but not the last, so cleanup
  464. * if the client is done with the descriptor
  465. */
  466. if (desc->async_tx.ack) {
  467. list_del(&desc->node);
  468. list_add_tail(&desc->node,
  469. &ioat_chan->free_desc);
  470. } else
  471. desc->async_tx.cookie = 0;
  472. } else {
  473. /*
  474. * last used desc. Do not remove, so we can append from
  475. * it, but don't look at it next time, either
  476. */
  477. desc->async_tx.cookie = 0;
  478. /* TODO check status bits? */
  479. break;
  480. }
  481. }
  482. spin_unlock_bh(&ioat_chan->desc_lock);
  483. ioat_chan->last_completion = phys_complete;
  484. if (cookie != 0)
  485. ioat_chan->completed_cookie = cookie;
  486. spin_unlock_bh(&ioat_chan->cleanup_lock);
  487. }
  488. static void ioat_dma_dependency_added(struct dma_chan *chan)
  489. {
  490. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  491. spin_lock_bh(&ioat_chan->desc_lock);
  492. if (ioat_chan->pending == 0) {
  493. spin_unlock_bh(&ioat_chan->desc_lock);
  494. ioat_dma_memcpy_cleanup(ioat_chan);
  495. } else
  496. spin_unlock_bh(&ioat_chan->desc_lock);
  497. }
  498. /**
  499. * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
  500. * @chan: IOAT DMA channel handle
  501. * @cookie: DMA transaction identifier
  502. * @done: if not %NULL, updated with last completed transaction
  503. * @used: if not %NULL, updated with last used transaction
  504. */
  505. static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
  506. dma_cookie_t cookie,
  507. dma_cookie_t *done,
  508. dma_cookie_t *used)
  509. {
  510. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  511. dma_cookie_t last_used;
  512. dma_cookie_t last_complete;
  513. enum dma_status ret;
  514. last_used = chan->cookie;
  515. last_complete = ioat_chan->completed_cookie;
  516. if (done)
  517. *done = last_complete;
  518. if (used)
  519. *used = last_used;
  520. ret = dma_async_is_complete(cookie, last_complete, last_used);
  521. if (ret == DMA_SUCCESS)
  522. return ret;
  523. ioat_dma_memcpy_cleanup(ioat_chan);
  524. last_used = chan->cookie;
  525. last_complete = ioat_chan->completed_cookie;
  526. if (done)
  527. *done = last_complete;
  528. if (used)
  529. *used = last_used;
  530. return dma_async_is_complete(cookie, last_complete, last_used);
  531. }
  532. /* PCI API */
  533. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
  534. {
  535. struct ioat_desc_sw *desc;
  536. spin_lock_bh(&ioat_chan->desc_lock);
  537. desc = ioat_dma_get_next_descriptor(ioat_chan);
  538. desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL
  539. | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
  540. | IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  541. desc->hw->next = 0;
  542. desc->hw->size = 0;
  543. desc->hw->src_addr = 0;
  544. desc->hw->dst_addr = 0;
  545. desc->async_tx.ack = 1;
  546. list_add_tail(&desc->node, &ioat_chan->used_desc);
  547. spin_unlock_bh(&ioat_chan->desc_lock);
  548. writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
  549. ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_LOW);
  550. writel(((u64) desc->async_tx.phys) >> 32,
  551. ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_HIGH);
  552. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  553. }
  554. /*
  555. * Perform a IOAT transaction to verify the HW works.
  556. */
  557. #define IOAT_TEST_SIZE 2000
  558. static void ioat_dma_test_callback(void *dma_async_param)
  559. {
  560. printk(KERN_ERR "ioatdma: ioat_dma_test_callback(%p)\n",
  561. dma_async_param);
  562. }
  563. /**
  564. * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
  565. * @device: device to be tested
  566. */
  567. static int ioat_dma_self_test(struct ioatdma_device *device)
  568. {
  569. int i;
  570. u8 *src;
  571. u8 *dest;
  572. struct dma_chan *dma_chan;
  573. struct dma_async_tx_descriptor *tx = NULL;
  574. dma_addr_t addr;
  575. dma_cookie_t cookie;
  576. int err = 0;
  577. src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  578. if (!src)
  579. return -ENOMEM;
  580. dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  581. if (!dest) {
  582. kfree(src);
  583. return -ENOMEM;
  584. }
  585. /* Fill in src buffer */
  586. for (i = 0; i < IOAT_TEST_SIZE; i++)
  587. src[i] = (u8)i;
  588. /* Start copy, using first DMA channel */
  589. dma_chan = container_of(device->common.channels.next,
  590. struct dma_chan,
  591. device_node);
  592. if (ioat_dma_alloc_chan_resources(dma_chan) < 1) {
  593. dev_err(&device->pdev->dev,
  594. "selftest cannot allocate chan resource\n");
  595. err = -ENODEV;
  596. goto out;
  597. }
  598. tx = ioat_dma_prep_memcpy(dma_chan, IOAT_TEST_SIZE, 0);
  599. if (!tx) {
  600. dev_err(&device->pdev->dev,
  601. "Self-test prep failed, disabling\n");
  602. err = -ENODEV;
  603. goto free_resources;
  604. }
  605. async_tx_ack(tx);
  606. addr = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
  607. DMA_TO_DEVICE);
  608. ioat_set_src(addr, tx, 0);
  609. addr = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
  610. DMA_FROM_DEVICE);
  611. ioat_set_dest(addr, tx, 0);
  612. tx->callback = ioat_dma_test_callback;
  613. tx->callback_param = (void *)0x8086;
  614. cookie = ioat_tx_submit(tx);
  615. if (cookie < 0) {
  616. dev_err(&device->pdev->dev,
  617. "Self-test setup failed, disabling\n");
  618. err = -ENODEV;
  619. goto free_resources;
  620. }
  621. ioat_dma_memcpy_issue_pending(dma_chan);
  622. msleep(1);
  623. if (ioat_dma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  624. dev_err(&device->pdev->dev,
  625. "Self-test copy timed out, disabling\n");
  626. err = -ENODEV;
  627. goto free_resources;
  628. }
  629. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  630. dev_err(&device->pdev->dev,
  631. "Self-test copy failed compare, disabling\n");
  632. err = -ENODEV;
  633. goto free_resources;
  634. }
  635. free_resources:
  636. ioat_dma_free_chan_resources(dma_chan);
  637. out:
  638. kfree(src);
  639. kfree(dest);
  640. return err;
  641. }
  642. static char ioat_interrupt_style[32] = "msix";
  643. module_param_string(ioat_interrupt_style, ioat_interrupt_style,
  644. sizeof(ioat_interrupt_style), 0644);
  645. MODULE_PARM_DESC(ioat_interrupt_style,
  646. "set ioat interrupt style: msix (default), "
  647. "msix-single-vector, msi, intx)");
  648. /**
  649. * ioat_dma_setup_interrupts - setup interrupt handler
  650. * @device: ioat device
  651. */
  652. static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
  653. {
  654. struct ioat_dma_chan *ioat_chan;
  655. int err, i, j, msixcnt;
  656. u8 intrctrl = 0;
  657. if (!strcmp(ioat_interrupt_style, "msix"))
  658. goto msix;
  659. if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
  660. goto msix_single_vector;
  661. if (!strcmp(ioat_interrupt_style, "msi"))
  662. goto msi;
  663. if (!strcmp(ioat_interrupt_style, "intx"))
  664. goto intx;
  665. dev_err(&device->pdev->dev, "invalid ioat_interrupt_style %s\n",
  666. ioat_interrupt_style);
  667. goto err_no_irq;
  668. msix:
  669. /* The number of MSI-X vectors should equal the number of channels */
  670. msixcnt = device->common.chancnt;
  671. for (i = 0; i < msixcnt; i++)
  672. device->msix_entries[i].entry = i;
  673. err = pci_enable_msix(device->pdev, device->msix_entries, msixcnt);
  674. if (err < 0)
  675. goto msi;
  676. if (err > 0)
  677. goto msix_single_vector;
  678. for (i = 0; i < msixcnt; i++) {
  679. ioat_chan = ioat_lookup_chan_by_index(device, i);
  680. err = request_irq(device->msix_entries[i].vector,
  681. ioat_dma_do_interrupt_msix,
  682. 0, "ioat-msix", ioat_chan);
  683. if (err) {
  684. for (j = 0; j < i; j++) {
  685. ioat_chan =
  686. ioat_lookup_chan_by_index(device, j);
  687. free_irq(device->msix_entries[j].vector,
  688. ioat_chan);
  689. }
  690. goto msix_single_vector;
  691. }
  692. }
  693. intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
  694. device->irq_mode = msix_multi_vector;
  695. goto done;
  696. msix_single_vector:
  697. device->msix_entries[0].entry = 0;
  698. err = pci_enable_msix(device->pdev, device->msix_entries, 1);
  699. if (err)
  700. goto msi;
  701. err = request_irq(device->msix_entries[0].vector, ioat_dma_do_interrupt,
  702. 0, "ioat-msix", device);
  703. if (err) {
  704. pci_disable_msix(device->pdev);
  705. goto msi;
  706. }
  707. device->irq_mode = msix_single_vector;
  708. goto done;
  709. msi:
  710. err = pci_enable_msi(device->pdev);
  711. if (err)
  712. goto intx;
  713. err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
  714. 0, "ioat-msi", device);
  715. if (err) {
  716. pci_disable_msi(device->pdev);
  717. goto intx;
  718. }
  719. /*
  720. * CB 1.2 devices need a bit set in configuration space to enable MSI
  721. */
  722. if (device->version == IOAT_VER_1_2) {
  723. u32 dmactrl;
  724. pci_read_config_dword(device->pdev,
  725. IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
  726. dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
  727. pci_write_config_dword(device->pdev,
  728. IOAT_PCI_DMACTRL_OFFSET, dmactrl);
  729. }
  730. device->irq_mode = msi;
  731. goto done;
  732. intx:
  733. err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
  734. IRQF_SHARED, "ioat-intx", device);
  735. if (err)
  736. goto err_no_irq;
  737. device->irq_mode = intx;
  738. done:
  739. intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
  740. writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
  741. return 0;
  742. err_no_irq:
  743. /* Disable all interrupt generation */
  744. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  745. dev_err(&device->pdev->dev, "no usable interrupts\n");
  746. device->irq_mode = none;
  747. return -1;
  748. }
  749. /**
  750. * ioat_dma_remove_interrupts - remove whatever interrupts were set
  751. * @device: ioat device
  752. */
  753. static void ioat_dma_remove_interrupts(struct ioatdma_device *device)
  754. {
  755. struct ioat_dma_chan *ioat_chan;
  756. int i;
  757. /* Disable all interrupt generation */
  758. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  759. switch (device->irq_mode) {
  760. case msix_multi_vector:
  761. for (i = 0; i < device->common.chancnt; i++) {
  762. ioat_chan = ioat_lookup_chan_by_index(device, i);
  763. free_irq(device->msix_entries[i].vector, ioat_chan);
  764. }
  765. pci_disable_msix(device->pdev);
  766. break;
  767. case msix_single_vector:
  768. free_irq(device->msix_entries[0].vector, device);
  769. pci_disable_msix(device->pdev);
  770. break;
  771. case msi:
  772. free_irq(device->pdev->irq, device);
  773. pci_disable_msi(device->pdev);
  774. break;
  775. case intx:
  776. free_irq(device->pdev->irq, device);
  777. break;
  778. case none:
  779. dev_warn(&device->pdev->dev,
  780. "call to %s without interrupts setup\n", __func__);
  781. }
  782. device->irq_mode = none;
  783. }
  784. struct ioatdma_device *ioat_dma_probe(struct pci_dev *pdev,
  785. void __iomem *iobase)
  786. {
  787. int err;
  788. struct ioatdma_device *device;
  789. device = kzalloc(sizeof(*device), GFP_KERNEL);
  790. if (!device) {
  791. err = -ENOMEM;
  792. goto err_kzalloc;
  793. }
  794. device->pdev = pdev;
  795. device->reg_base = iobase;
  796. device->version = readb(device->reg_base + IOAT_VER_OFFSET);
  797. /* DMA coherent memory pool for DMA descriptor allocations */
  798. device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
  799. sizeof(struct ioat_dma_descriptor),
  800. 64, 0);
  801. if (!device->dma_pool) {
  802. err = -ENOMEM;
  803. goto err_dma_pool;
  804. }
  805. device->completion_pool = pci_pool_create("completion_pool", pdev,
  806. sizeof(u64), SMP_CACHE_BYTES,
  807. SMP_CACHE_BYTES);
  808. if (!device->completion_pool) {
  809. err = -ENOMEM;
  810. goto err_completion_pool;
  811. }
  812. INIT_LIST_HEAD(&device->common.channels);
  813. ioat_dma_enumerate_channels(device);
  814. dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
  815. device->common.device_alloc_chan_resources =
  816. ioat_dma_alloc_chan_resources;
  817. device->common.device_free_chan_resources =
  818. ioat_dma_free_chan_resources;
  819. device->common.device_prep_dma_memcpy = ioat_dma_prep_memcpy;
  820. device->common.device_is_tx_complete = ioat_dma_is_complete;
  821. device->common.device_issue_pending = ioat_dma_memcpy_issue_pending;
  822. device->common.device_dependency_added = ioat_dma_dependency_added;
  823. device->common.dev = &pdev->dev;
  824. dev_err(&device->pdev->dev,
  825. "Intel(R) I/OAT DMA Engine found,"
  826. " %d channels, device version 0x%02x, driver version %s\n",
  827. device->common.chancnt, device->version, IOAT_DMA_VERSION);
  828. err = ioat_dma_setup_interrupts(device);
  829. if (err)
  830. goto err_setup_interrupts;
  831. err = ioat_dma_self_test(device);
  832. if (err)
  833. goto err_self_test;
  834. dma_async_device_register(&device->common);
  835. return device;
  836. err_self_test:
  837. ioat_dma_remove_interrupts(device);
  838. err_setup_interrupts:
  839. pci_pool_destroy(device->completion_pool);
  840. err_completion_pool:
  841. pci_pool_destroy(device->dma_pool);
  842. err_dma_pool:
  843. kfree(device);
  844. err_kzalloc:
  845. dev_err(&device->pdev->dev,
  846. "Intel(R) I/OAT DMA Engine initialization failed\n");
  847. return NULL;
  848. }
  849. void ioat_dma_remove(struct ioatdma_device *device)
  850. {
  851. struct dma_chan *chan, *_chan;
  852. struct ioat_dma_chan *ioat_chan;
  853. ioat_dma_remove_interrupts(device);
  854. dma_async_device_unregister(&device->common);
  855. pci_pool_destroy(device->dma_pool);
  856. pci_pool_destroy(device->completion_pool);
  857. iounmap(device->reg_base);
  858. pci_release_regions(device->pdev);
  859. pci_disable_device(device->pdev);
  860. list_for_each_entry_safe(chan, _chan,
  861. &device->common.channels, device_node) {
  862. ioat_chan = to_ioat_chan(chan);
  863. list_del(&chan->device_node);
  864. kfree(ioat_chan);
  865. }
  866. kfree(device);
  867. }