x86: make nmi_cpu_busy() always defined
[pv_ops_mirror.git] / drivers / dma / ioat_dma.c
blob7e4a785c2dff6fdb5d1640e9b0499974f27f8cc5
1 /*
2 * Intel I/OAT DMA Linux driver
3 * Copyright(c) 2004 - 2007 Intel Corporation.
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.
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.
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.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
24 * This driver supports an Intel I/OAT DMA engine, which does asynchronous
25 * copy operations.
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/interrupt.h>
32 #include <linux/dmaengine.h>
33 #include <linux/delay.h>
34 #include <linux/dma-mapping.h>
35 #include "ioatdma.h"
36 #include "ioatdma_registers.h"
37 #include "ioatdma_hw.h"
39 #define INITIAL_IOAT_DESC_COUNT 128
41 #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
42 #define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, common)
43 #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
44 #define tx_to_ioat_desc(tx) container_of(tx, struct ioat_desc_sw, async_tx)
46 /* internal functions */
47 static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan);
48 static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
49 static struct ioat_desc_sw *
50 ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
52 static inline struct ioat_dma_chan *ioat_lookup_chan_by_index(
53 struct ioatdma_device *device,
54 int index)
56 return device->idx[index];
59 /**
60 * ioat_dma_do_interrupt - handler used for single vector interrupt mode
61 * @irq: interrupt id
62 * @data: interrupt data
64 static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
66 struct ioatdma_device *instance = data;
67 struct ioat_dma_chan *ioat_chan;
68 unsigned long attnstatus;
69 int bit;
70 u8 intrctrl;
72 intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
74 if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
75 return IRQ_NONE;
77 if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
78 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
79 return IRQ_NONE;
82 attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
83 for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
84 ioat_chan = ioat_lookup_chan_by_index(instance, bit);
85 tasklet_schedule(&ioat_chan->cleanup_task);
88 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
89 return IRQ_HANDLED;
92 /**
93 * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
94 * @irq: interrupt id
95 * @data: interrupt data
97 static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
99 struct ioat_dma_chan *ioat_chan = data;
101 tasklet_schedule(&ioat_chan->cleanup_task);
103 return IRQ_HANDLED;
106 static void ioat_dma_cleanup_tasklet(unsigned long data);
109 * ioat_dma_enumerate_channels - find and initialize the device's channels
110 * @device: the device to be enumerated
112 static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
114 u8 xfercap_scale;
115 u32 xfercap;
116 int i;
117 struct ioat_dma_chan *ioat_chan;
119 device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
120 xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
121 xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
123 for (i = 0; i < device->common.chancnt; i++) {
124 ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
125 if (!ioat_chan) {
126 device->common.chancnt = i;
127 break;
130 ioat_chan->device = device;
131 ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
132 ioat_chan->xfercap = xfercap;
133 spin_lock_init(&ioat_chan->cleanup_lock);
134 spin_lock_init(&ioat_chan->desc_lock);
135 INIT_LIST_HEAD(&ioat_chan->free_desc);
136 INIT_LIST_HEAD(&ioat_chan->used_desc);
137 /* This should be made common somewhere in dmaengine.c */
138 ioat_chan->common.device = &device->common;
139 list_add_tail(&ioat_chan->common.device_node,
140 &device->common.channels);
141 device->idx[i] = ioat_chan;
142 tasklet_init(&ioat_chan->cleanup_task,
143 ioat_dma_cleanup_tasklet,
144 (unsigned long) ioat_chan);
145 tasklet_disable(&ioat_chan->cleanup_task);
147 return device->common.chancnt;
150 static void ioat_set_src(dma_addr_t addr,
151 struct dma_async_tx_descriptor *tx,
152 int index)
154 tx_to_ioat_desc(tx)->src = addr;
157 static void ioat_set_dest(dma_addr_t addr,
158 struct dma_async_tx_descriptor *tx,
159 int index)
161 tx_to_ioat_desc(tx)->dst = addr;
164 static dma_cookie_t ioat_tx_submit(struct dma_async_tx_descriptor *tx)
166 struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
167 struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
168 struct ioat_desc_sw *prev, *new;
169 struct ioat_dma_descriptor *hw;
170 int append = 0;
171 dma_cookie_t cookie;
172 LIST_HEAD(new_chain);
173 u32 copy;
174 size_t len;
175 dma_addr_t src, dst;
176 int orig_ack;
177 unsigned int desc_count = 0;
179 /* src and dest and len are stored in the initial descriptor */
180 len = first->len;
181 src = first->src;
182 dst = first->dst;
183 orig_ack = first->async_tx.ack;
184 new = first;
186 spin_lock_bh(&ioat_chan->desc_lock);
187 prev = to_ioat_desc(ioat_chan->used_desc.prev);
188 prefetch(prev->hw);
189 do {
190 copy = min((u32) len, ioat_chan->xfercap);
192 new->async_tx.ack = 1;
194 hw = new->hw;
195 hw->size = copy;
196 hw->ctl = 0;
197 hw->src_addr = src;
198 hw->dst_addr = dst;
199 hw->next = 0;
201 /* chain together the physical address list for the HW */
202 wmb();
203 prev->hw->next = (u64) new->async_tx.phys;
205 len -= copy;
206 dst += copy;
207 src += copy;
209 list_add_tail(&new->node, &new_chain);
210 desc_count++;
211 prev = new;
212 } while (len && (new = ioat_dma_get_next_descriptor(ioat_chan)));
214 hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
215 if (new->async_tx.callback) {
216 hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
217 if (first != new) {
218 /* move callback into to last desc */
219 new->async_tx.callback = first->async_tx.callback;
220 new->async_tx.callback_param
221 = first->async_tx.callback_param;
222 first->async_tx.callback = NULL;
223 first->async_tx.callback_param = NULL;
227 new->tx_cnt = desc_count;
228 new->async_tx.ack = orig_ack; /* client is in control of this ack */
230 /* store the original values for use in later cleanup */
231 if (new != first) {
232 new->src = first->src;
233 new->dst = first->dst;
234 new->len = first->len;
237 /* cookie incr and addition to used_list must be atomic */
238 cookie = ioat_chan->common.cookie;
239 cookie++;
240 if (cookie < 0)
241 cookie = 1;
242 ioat_chan->common.cookie = new->async_tx.cookie = cookie;
244 /* write address into NextDescriptor field of last desc in chain */
245 to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
246 first->async_tx.phys;
247 __list_splice(&new_chain, ioat_chan->used_desc.prev);
249 ioat_chan->pending += desc_count;
250 if (ioat_chan->pending >= 4) {
251 append = 1;
252 ioat_chan->pending = 0;
254 spin_unlock_bh(&ioat_chan->desc_lock);
256 if (append)
257 writeb(IOAT_CHANCMD_APPEND,
258 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
260 return cookie;
263 static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
264 struct ioat_dma_chan *ioat_chan,
265 gfp_t flags)
267 struct ioat_dma_descriptor *desc;
268 struct ioat_desc_sw *desc_sw;
269 struct ioatdma_device *ioatdma_device;
270 dma_addr_t phys;
272 ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
273 desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
274 if (unlikely(!desc))
275 return NULL;
277 desc_sw = kzalloc(sizeof(*desc_sw), flags);
278 if (unlikely(!desc_sw)) {
279 pci_pool_free(ioatdma_device->dma_pool, desc, phys);
280 return NULL;
283 memset(desc, 0, sizeof(*desc));
284 dma_async_tx_descriptor_init(&desc_sw->async_tx, &ioat_chan->common);
285 desc_sw->async_tx.tx_set_src = ioat_set_src;
286 desc_sw->async_tx.tx_set_dest = ioat_set_dest;
287 desc_sw->async_tx.tx_submit = ioat_tx_submit;
288 INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
289 desc_sw->hw = desc;
290 desc_sw->async_tx.phys = phys;
292 return desc_sw;
295 /* returns the actual number of allocated descriptors */
296 static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
298 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
299 struct ioat_desc_sw *desc = NULL;
300 u16 chanctrl;
301 u32 chanerr;
302 int i;
303 LIST_HEAD(tmp_list);
305 /* have we already been set up? */
306 if (!list_empty(&ioat_chan->free_desc))
307 return INITIAL_IOAT_DESC_COUNT;
309 /* Setup register to interrupt and write completion status on error */
310 chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
311 IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
312 IOAT_CHANCTRL_ERR_COMPLETION_EN;
313 writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
315 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
316 if (chanerr) {
317 dev_err(&ioat_chan->device->pdev->dev,
318 "CHANERR = %x, clearing\n", chanerr);
319 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
322 /* Allocate descriptors */
323 for (i = 0; i < INITIAL_IOAT_DESC_COUNT; i++) {
324 desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
325 if (!desc) {
326 dev_err(&ioat_chan->device->pdev->dev,
327 "Only %d initial descriptors\n", i);
328 break;
330 list_add_tail(&desc->node, &tmp_list);
332 spin_lock_bh(&ioat_chan->desc_lock);
333 list_splice(&tmp_list, &ioat_chan->free_desc);
334 spin_unlock_bh(&ioat_chan->desc_lock);
336 /* allocate a completion writeback area */
337 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
338 ioat_chan->completion_virt =
339 pci_pool_alloc(ioat_chan->device->completion_pool,
340 GFP_KERNEL,
341 &ioat_chan->completion_addr);
342 memset(ioat_chan->completion_virt, 0,
343 sizeof(*ioat_chan->completion_virt));
344 writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
345 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
346 writel(((u64) ioat_chan->completion_addr) >> 32,
347 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
349 tasklet_enable(&ioat_chan->cleanup_task);
350 ioat_dma_start_null_desc(ioat_chan);
351 return i;
354 static void ioat_dma_free_chan_resources(struct dma_chan *chan)
356 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
357 struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
358 struct ioat_desc_sw *desc, *_desc;
359 int in_use_descs = 0;
361 tasklet_disable(&ioat_chan->cleanup_task);
362 ioat_dma_memcpy_cleanup(ioat_chan);
364 /* Delay 100ms after reset to allow internal DMA logic to quiesce
365 * before removing DMA descriptor resources.
367 writeb(IOAT_CHANCMD_RESET, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
368 mdelay(100);
370 spin_lock_bh(&ioat_chan->desc_lock);
371 list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
372 in_use_descs++;
373 list_del(&desc->node);
374 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
375 desc->async_tx.phys);
376 kfree(desc);
378 list_for_each_entry_safe(desc, _desc, &ioat_chan->free_desc, node) {
379 list_del(&desc->node);
380 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
381 desc->async_tx.phys);
382 kfree(desc);
384 spin_unlock_bh(&ioat_chan->desc_lock);
386 pci_pool_free(ioatdma_device->completion_pool,
387 ioat_chan->completion_virt,
388 ioat_chan->completion_addr);
390 /* one is ok since we left it on there on purpose */
391 if (in_use_descs > 1)
392 dev_err(&ioat_chan->device->pdev->dev,
393 "Freeing %d in use descriptors!\n",
394 in_use_descs - 1);
396 ioat_chan->last_completion = ioat_chan->completion_addr = 0;
397 ioat_chan->pending = 0;
401 * ioat_dma_get_next_descriptor - return the next available descriptor
402 * @ioat_chan: IOAT DMA channel handle
404 * Gets the next descriptor from the chain, and must be called with the
405 * channel's desc_lock held. Allocates more descriptors if the channel
406 * has run out.
408 static struct ioat_desc_sw *
409 ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
411 struct ioat_desc_sw *new = NULL;
413 if (!list_empty(&ioat_chan->free_desc)) {
414 new = to_ioat_desc(ioat_chan->free_desc.next);
415 list_del(&new->node);
416 } else {
417 /* try to get another desc */
418 new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
419 /* will this ever happen? */
420 /* TODO add upper limit on these */
421 BUG_ON(!new);
424 prefetch(new->hw);
425 return new;
428 static struct dma_async_tx_descriptor *ioat_dma_prep_memcpy(
429 struct dma_chan *chan,
430 size_t len,
431 int int_en)
433 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
434 struct ioat_desc_sw *new;
436 spin_lock_bh(&ioat_chan->desc_lock);
437 new = ioat_dma_get_next_descriptor(ioat_chan);
438 new->len = len;
439 spin_unlock_bh(&ioat_chan->desc_lock);
441 return new ? &new->async_tx : NULL;
445 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
446 * descriptors to hw
447 * @chan: DMA channel handle
449 static void ioat_dma_memcpy_issue_pending(struct dma_chan *chan)
451 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
453 if (ioat_chan->pending != 0) {
454 ioat_chan->pending = 0;
455 writeb(IOAT_CHANCMD_APPEND,
456 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
460 static void ioat_dma_cleanup_tasklet(unsigned long data)
462 struct ioat_dma_chan *chan = (void *)data;
463 ioat_dma_memcpy_cleanup(chan);
464 writew(IOAT_CHANCTRL_INT_DISABLE,
465 chan->reg_base + IOAT_CHANCTRL_OFFSET);
468 static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
470 unsigned long phys_complete;
471 struct ioat_desc_sw *desc, *_desc;
472 dma_cookie_t cookie = 0;
474 prefetch(ioat_chan->completion_virt);
476 if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
477 return;
479 /* The completion writeback can happen at any time,
480 so reads by the driver need to be atomic operations
481 The descriptor physical addresses are limited to 32-bits
482 when the CPU can only do a 32-bit mov */
484 #if (BITS_PER_LONG == 64)
485 phys_complete =
486 ioat_chan->completion_virt->full
487 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
488 #else
489 phys_complete =
490 ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
491 #endif
493 if ((ioat_chan->completion_virt->full
494 & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
495 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
496 dev_err(&ioat_chan->device->pdev->dev,
497 "Channel halted, chanerr = %x\n",
498 readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
500 /* TODO do something to salvage the situation */
503 if (phys_complete == ioat_chan->last_completion) {
504 spin_unlock_bh(&ioat_chan->cleanup_lock);
505 return;
508 cookie = 0;
509 spin_lock_bh(&ioat_chan->desc_lock);
510 list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
513 * Incoming DMA requests may use multiple descriptors, due to
514 * exceeding xfercap, perhaps. If so, only the last one will
515 * have a cookie, and require unmapping.
517 if (desc->async_tx.cookie) {
518 cookie = desc->async_tx.cookie;
521 * yes we are unmapping both _page and _single alloc'd
522 * regions with unmap_page. Is this *really* that bad?
524 pci_unmap_page(ioat_chan->device->pdev,
525 pci_unmap_addr(desc, dst),
526 pci_unmap_len(desc, len),
527 PCI_DMA_FROMDEVICE);
528 pci_unmap_page(ioat_chan->device->pdev,
529 pci_unmap_addr(desc, src),
530 pci_unmap_len(desc, len),
531 PCI_DMA_TODEVICE);
532 if (desc->async_tx.callback) {
533 desc->async_tx.callback(
534 desc->async_tx.callback_param);
535 desc->async_tx.callback = NULL;
539 if (desc->async_tx.phys != phys_complete) {
541 * a completed entry, but not the last, so cleanup
542 * if the client is done with the descriptor
544 if (desc->async_tx.ack) {
545 list_del(&desc->node);
546 list_add_tail(&desc->node,
547 &ioat_chan->free_desc);
548 } else
549 desc->async_tx.cookie = 0;
550 } else {
552 * last used desc. Do not remove, so we can append from
553 * it, but don't look at it next time, either
555 desc->async_tx.cookie = 0;
557 /* TODO check status bits? */
558 break;
562 spin_unlock_bh(&ioat_chan->desc_lock);
564 ioat_chan->last_completion = phys_complete;
565 if (cookie != 0)
566 ioat_chan->completed_cookie = cookie;
568 spin_unlock_bh(&ioat_chan->cleanup_lock);
571 static void ioat_dma_dependency_added(struct dma_chan *chan)
573 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
574 spin_lock_bh(&ioat_chan->desc_lock);
575 if (ioat_chan->pending == 0) {
576 spin_unlock_bh(&ioat_chan->desc_lock);
577 ioat_dma_memcpy_cleanup(ioat_chan);
578 } else
579 spin_unlock_bh(&ioat_chan->desc_lock);
583 * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
584 * @chan: IOAT DMA channel handle
585 * @cookie: DMA transaction identifier
586 * @done: if not %NULL, updated with last completed transaction
587 * @used: if not %NULL, updated with last used transaction
589 static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
590 dma_cookie_t cookie,
591 dma_cookie_t *done,
592 dma_cookie_t *used)
594 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
595 dma_cookie_t last_used;
596 dma_cookie_t last_complete;
597 enum dma_status ret;
599 last_used = chan->cookie;
600 last_complete = ioat_chan->completed_cookie;
602 if (done)
603 *done = last_complete;
604 if (used)
605 *used = last_used;
607 ret = dma_async_is_complete(cookie, last_complete, last_used);
608 if (ret == DMA_SUCCESS)
609 return ret;
611 ioat_dma_memcpy_cleanup(ioat_chan);
613 last_used = chan->cookie;
614 last_complete = ioat_chan->completed_cookie;
616 if (done)
617 *done = last_complete;
618 if (used)
619 *used = last_used;
621 return dma_async_is_complete(cookie, last_complete, last_used);
624 /* PCI API */
626 static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
628 struct ioat_desc_sw *desc;
630 spin_lock_bh(&ioat_chan->desc_lock);
632 desc = ioat_dma_get_next_descriptor(ioat_chan);
633 desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL
634 | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
635 | IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
636 desc->hw->next = 0;
637 desc->hw->size = 0;
638 desc->hw->src_addr = 0;
639 desc->hw->dst_addr = 0;
640 desc->async_tx.ack = 1;
642 list_add_tail(&desc->node, &ioat_chan->used_desc);
643 spin_unlock_bh(&ioat_chan->desc_lock);
645 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
646 ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_LOW);
647 writel(((u64) desc->async_tx.phys) >> 32,
648 ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_HIGH);
650 writeb(IOAT_CHANCMD_START, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
654 * Perform a IOAT transaction to verify the HW works.
656 #define IOAT_TEST_SIZE 2000
658 static void ioat_dma_test_callback(void *dma_async_param)
660 printk(KERN_ERR "ioatdma: ioat_dma_test_callback(%p)\n",
661 dma_async_param);
665 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
666 * @device: device to be tested
668 static int ioat_dma_self_test(struct ioatdma_device *device)
670 int i;
671 u8 *src;
672 u8 *dest;
673 struct dma_chan *dma_chan;
674 struct dma_async_tx_descriptor *tx = NULL;
675 dma_addr_t addr;
676 dma_cookie_t cookie;
677 int err = 0;
679 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
680 if (!src)
681 return -ENOMEM;
682 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
683 if (!dest) {
684 kfree(src);
685 return -ENOMEM;
688 /* Fill in src buffer */
689 for (i = 0; i < IOAT_TEST_SIZE; i++)
690 src[i] = (u8)i;
692 /* Start copy, using first DMA channel */
693 dma_chan = container_of(device->common.channels.next,
694 struct dma_chan,
695 device_node);
696 if (ioat_dma_alloc_chan_resources(dma_chan) < 1) {
697 dev_err(&device->pdev->dev,
698 "selftest cannot allocate chan resource\n");
699 err = -ENODEV;
700 goto out;
703 tx = ioat_dma_prep_memcpy(dma_chan, IOAT_TEST_SIZE, 0);
704 if (!tx) {
705 dev_err(&device->pdev->dev,
706 "Self-test prep failed, disabling\n");
707 err = -ENODEV;
708 goto free_resources;
711 async_tx_ack(tx);
712 addr = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
713 DMA_TO_DEVICE);
714 ioat_set_src(addr, tx, 0);
715 addr = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
716 DMA_FROM_DEVICE);
717 ioat_set_dest(addr, tx, 0);
718 tx->callback = ioat_dma_test_callback;
719 tx->callback_param = (void *)0x8086;
720 cookie = ioat_tx_submit(tx);
721 if (cookie < 0) {
722 dev_err(&device->pdev->dev,
723 "Self-test setup failed, disabling\n");
724 err = -ENODEV;
725 goto free_resources;
727 ioat_dma_memcpy_issue_pending(dma_chan);
728 msleep(1);
730 if (ioat_dma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
731 dev_err(&device->pdev->dev,
732 "Self-test copy timed out, disabling\n");
733 err = -ENODEV;
734 goto free_resources;
736 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
737 dev_err(&device->pdev->dev,
738 "Self-test copy failed compare, disabling\n");
739 err = -ENODEV;
740 goto free_resources;
743 free_resources:
744 ioat_dma_free_chan_resources(dma_chan);
745 out:
746 kfree(src);
747 kfree(dest);
748 return err;
751 static char ioat_interrupt_style[32] = "msix";
752 module_param_string(ioat_interrupt_style, ioat_interrupt_style,
753 sizeof(ioat_interrupt_style), 0644);
754 MODULE_PARM_DESC(ioat_interrupt_style,
755 "set ioat interrupt style: msix (default), "
756 "msix-single-vector, msi, intx)");
759 * ioat_dma_setup_interrupts - setup interrupt handler
760 * @device: ioat device
762 static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
764 struct ioat_dma_chan *ioat_chan;
765 int err, i, j, msixcnt;
766 u8 intrctrl = 0;
768 if (!strcmp(ioat_interrupt_style, "msix"))
769 goto msix;
770 if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
771 goto msix_single_vector;
772 if (!strcmp(ioat_interrupt_style, "msi"))
773 goto msi;
774 if (!strcmp(ioat_interrupt_style, "intx"))
775 goto intx;
776 dev_err(&device->pdev->dev, "invalid ioat_interrupt_style %s\n",
777 ioat_interrupt_style);
778 goto err_no_irq;
780 msix:
781 /* The number of MSI-X vectors should equal the number of channels */
782 msixcnt = device->common.chancnt;
783 for (i = 0; i < msixcnt; i++)
784 device->msix_entries[i].entry = i;
786 err = pci_enable_msix(device->pdev, device->msix_entries, msixcnt);
787 if (err < 0)
788 goto msi;
789 if (err > 0)
790 goto msix_single_vector;
792 for (i = 0; i < msixcnt; i++) {
793 ioat_chan = ioat_lookup_chan_by_index(device, i);
794 err = request_irq(device->msix_entries[i].vector,
795 ioat_dma_do_interrupt_msix,
796 0, "ioat-msix", ioat_chan);
797 if (err) {
798 for (j = 0; j < i; j++) {
799 ioat_chan =
800 ioat_lookup_chan_by_index(device, j);
801 free_irq(device->msix_entries[j].vector,
802 ioat_chan);
804 goto msix_single_vector;
807 intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
808 device->irq_mode = msix_multi_vector;
809 goto done;
811 msix_single_vector:
812 device->msix_entries[0].entry = 0;
813 err = pci_enable_msix(device->pdev, device->msix_entries, 1);
814 if (err)
815 goto msi;
817 err = request_irq(device->msix_entries[0].vector, ioat_dma_do_interrupt,
818 0, "ioat-msix", device);
819 if (err) {
820 pci_disable_msix(device->pdev);
821 goto msi;
823 device->irq_mode = msix_single_vector;
824 goto done;
826 msi:
827 err = pci_enable_msi(device->pdev);
828 if (err)
829 goto intx;
831 err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
832 0, "ioat-msi", device);
833 if (err) {
834 pci_disable_msi(device->pdev);
835 goto intx;
838 * CB 1.2 devices need a bit set in configuration space to enable MSI
840 if (device->version == IOAT_VER_1_2) {
841 u32 dmactrl;
842 pci_read_config_dword(device->pdev,
843 IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
844 dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
845 pci_write_config_dword(device->pdev,
846 IOAT_PCI_DMACTRL_OFFSET, dmactrl);
848 device->irq_mode = msi;
849 goto done;
851 intx:
852 err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
853 IRQF_SHARED, "ioat-intx", device);
854 if (err)
855 goto err_no_irq;
856 device->irq_mode = intx;
858 done:
859 intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
860 writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
861 return 0;
863 err_no_irq:
864 /* Disable all interrupt generation */
865 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
866 dev_err(&device->pdev->dev, "no usable interrupts\n");
867 device->irq_mode = none;
868 return -1;
872 * ioat_dma_remove_interrupts - remove whatever interrupts were set
873 * @device: ioat device
875 static void ioat_dma_remove_interrupts(struct ioatdma_device *device)
877 struct ioat_dma_chan *ioat_chan;
878 int i;
880 /* Disable all interrupt generation */
881 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
883 switch (device->irq_mode) {
884 case msix_multi_vector:
885 for (i = 0; i < device->common.chancnt; i++) {
886 ioat_chan = ioat_lookup_chan_by_index(device, i);
887 free_irq(device->msix_entries[i].vector, ioat_chan);
889 pci_disable_msix(device->pdev);
890 break;
891 case msix_single_vector:
892 free_irq(device->msix_entries[0].vector, device);
893 pci_disable_msix(device->pdev);
894 break;
895 case msi:
896 free_irq(device->pdev->irq, device);
897 pci_disable_msi(device->pdev);
898 break;
899 case intx:
900 free_irq(device->pdev->irq, device);
901 break;
902 case none:
903 dev_warn(&device->pdev->dev,
904 "call to %s without interrupts setup\n", __func__);
906 device->irq_mode = none;
909 struct ioatdma_device *ioat_dma_probe(struct pci_dev *pdev,
910 void __iomem *iobase)
912 int err;
913 struct ioatdma_device *device;
915 device = kzalloc(sizeof(*device), GFP_KERNEL);
916 if (!device) {
917 err = -ENOMEM;
918 goto err_kzalloc;
920 device->pdev = pdev;
921 device->reg_base = iobase;
922 device->version = readb(device->reg_base + IOAT_VER_OFFSET);
924 /* DMA coherent memory pool for DMA descriptor allocations */
925 device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
926 sizeof(struct ioat_dma_descriptor),
927 64, 0);
928 if (!device->dma_pool) {
929 err = -ENOMEM;
930 goto err_dma_pool;
933 device->completion_pool = pci_pool_create("completion_pool", pdev,
934 sizeof(u64), SMP_CACHE_BYTES,
935 SMP_CACHE_BYTES);
936 if (!device->completion_pool) {
937 err = -ENOMEM;
938 goto err_completion_pool;
941 INIT_LIST_HEAD(&device->common.channels);
942 ioat_dma_enumerate_channels(device);
944 dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
945 device->common.device_alloc_chan_resources =
946 ioat_dma_alloc_chan_resources;
947 device->common.device_free_chan_resources =
948 ioat_dma_free_chan_resources;
949 device->common.device_prep_dma_memcpy = ioat_dma_prep_memcpy;
950 device->common.device_is_tx_complete = ioat_dma_is_complete;
951 device->common.device_issue_pending = ioat_dma_memcpy_issue_pending;
952 device->common.device_dependency_added = ioat_dma_dependency_added;
953 device->common.dev = &pdev->dev;
954 dev_err(&device->pdev->dev,
955 "Intel(R) I/OAT DMA Engine found,"
956 " %d channels, device version 0x%02x, driver version %s\n",
957 device->common.chancnt, device->version, IOAT_DMA_VERSION);
959 err = ioat_dma_setup_interrupts(device);
960 if (err)
961 goto err_setup_interrupts;
963 err = ioat_dma_self_test(device);
964 if (err)
965 goto err_self_test;
967 dma_async_device_register(&device->common);
969 return device;
971 err_self_test:
972 ioat_dma_remove_interrupts(device);
973 err_setup_interrupts:
974 pci_pool_destroy(device->completion_pool);
975 err_completion_pool:
976 pci_pool_destroy(device->dma_pool);
977 err_dma_pool:
978 kfree(device);
979 err_kzalloc:
980 dev_err(&device->pdev->dev,
981 "Intel(R) I/OAT DMA Engine initialization failed\n");
982 return NULL;
985 void ioat_dma_remove(struct ioatdma_device *device)
987 struct dma_chan *chan, *_chan;
988 struct ioat_dma_chan *ioat_chan;
990 ioat_dma_remove_interrupts(device);
992 dma_async_device_unregister(&device->common);
994 pci_pool_destroy(device->dma_pool);
995 pci_pool_destroy(device->completion_pool);
997 iounmap(device->reg_base);
998 pci_release_regions(device->pdev);
999 pci_disable_device(device->pdev);
1001 list_for_each_entry_safe(chan, _chan,
1002 &device->common.channels, device_node) {
1003 ioat_chan = to_ioat_chan(chan);
1004 list_del(&chan->device_node);
1005 kfree(ioat_chan);
1007 kfree(device);