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
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
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>
36 #include "ioatdma_registers.h"
37 #include "ioatdma_hw.h"
39 #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
40 #define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, common)
41 #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
42 #define tx_to_ioat_desc(tx) container_of(tx, struct ioat_desc_sw, async_tx)
44 static int ioat_pending_level
= 4;
45 module_param(ioat_pending_level
, int, 0644);
46 MODULE_PARM_DESC(ioat_pending_level
,
47 "high-water mark for pushing ioat descriptors (default: 4)");
49 /* internal functions */
50 static void ioat_dma_start_null_desc(struct ioat_dma_chan
*ioat_chan
);
51 static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan
*ioat_chan
);
53 static struct ioat_desc_sw
*
54 ioat1_dma_get_next_descriptor(struct ioat_dma_chan
*ioat_chan
);
55 static struct ioat_desc_sw
*
56 ioat2_dma_get_next_descriptor(struct ioat_dma_chan
*ioat_chan
);
58 static inline struct ioat_dma_chan
*ioat_lookup_chan_by_index(
59 struct ioatdma_device
*device
,
62 return device
->idx
[index
];
66 * ioat_dma_do_interrupt - handler used for single vector interrupt mode
68 * @data: interrupt data
70 static irqreturn_t
ioat_dma_do_interrupt(int irq
, void *data
)
72 struct ioatdma_device
*instance
= data
;
73 struct ioat_dma_chan
*ioat_chan
;
74 unsigned long attnstatus
;
78 intrctrl
= readb(instance
->reg_base
+ IOAT_INTRCTRL_OFFSET
);
80 if (!(intrctrl
& IOAT_INTRCTRL_MASTER_INT_EN
))
83 if (!(intrctrl
& IOAT_INTRCTRL_INT_STATUS
)) {
84 writeb(intrctrl
, instance
->reg_base
+ IOAT_INTRCTRL_OFFSET
);
88 attnstatus
= readl(instance
->reg_base
+ IOAT_ATTNSTATUS_OFFSET
);
89 for_each_bit(bit
, &attnstatus
, BITS_PER_LONG
) {
90 ioat_chan
= ioat_lookup_chan_by_index(instance
, bit
);
91 tasklet_schedule(&ioat_chan
->cleanup_task
);
94 writeb(intrctrl
, instance
->reg_base
+ IOAT_INTRCTRL_OFFSET
);
99 * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
101 * @data: interrupt data
103 static irqreturn_t
ioat_dma_do_interrupt_msix(int irq
, void *data
)
105 struct ioat_dma_chan
*ioat_chan
= data
;
107 tasklet_schedule(&ioat_chan
->cleanup_task
);
112 static void ioat_dma_cleanup_tasklet(unsigned long data
);
115 * ioat_dma_enumerate_channels - find and initialize the device's channels
116 * @device: the device to be enumerated
118 static int ioat_dma_enumerate_channels(struct ioatdma_device
*device
)
123 struct ioat_dma_chan
*ioat_chan
;
125 device
->common
.chancnt
= readb(device
->reg_base
+ IOAT_CHANCNT_OFFSET
);
126 xfercap_scale
= readb(device
->reg_base
+ IOAT_XFERCAP_OFFSET
);
127 xfercap
= (xfercap_scale
== 0 ? -1 : (1UL << xfercap_scale
));
129 for (i
= 0; i
< device
->common
.chancnt
; i
++) {
130 ioat_chan
= kzalloc(sizeof(*ioat_chan
), GFP_KERNEL
);
132 device
->common
.chancnt
= i
;
136 ioat_chan
->device
= device
;
137 ioat_chan
->reg_base
= device
->reg_base
+ (0x80 * (i
+ 1));
138 ioat_chan
->xfercap
= xfercap
;
139 ioat_chan
->desccount
= 0;
140 if (ioat_chan
->device
->version
!= IOAT_VER_1_2
) {
141 writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE
142 | IOAT_DMA_DCA_ANY_CPU
,
143 ioat_chan
->reg_base
+ IOAT_DCACTRL_OFFSET
);
145 spin_lock_init(&ioat_chan
->cleanup_lock
);
146 spin_lock_init(&ioat_chan
->desc_lock
);
147 INIT_LIST_HEAD(&ioat_chan
->free_desc
);
148 INIT_LIST_HEAD(&ioat_chan
->used_desc
);
149 /* This should be made common somewhere in dmaengine.c */
150 ioat_chan
->common
.device
= &device
->common
;
151 list_add_tail(&ioat_chan
->common
.device_node
,
152 &device
->common
.channels
);
153 device
->idx
[i
] = ioat_chan
;
154 tasklet_init(&ioat_chan
->cleanup_task
,
155 ioat_dma_cleanup_tasklet
,
156 (unsigned long) ioat_chan
);
157 tasklet_disable(&ioat_chan
->cleanup_task
);
159 return device
->common
.chancnt
;
163 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
165 * @chan: DMA channel handle
167 static inline void __ioat1_dma_memcpy_issue_pending(
168 struct ioat_dma_chan
*ioat_chan
)
170 ioat_chan
->pending
= 0;
171 writeb(IOAT_CHANCMD_APPEND
, ioat_chan
->reg_base
+ IOAT1_CHANCMD_OFFSET
);
174 static void ioat1_dma_memcpy_issue_pending(struct dma_chan
*chan
)
176 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(chan
);
178 if (ioat_chan
->pending
!= 0) {
179 spin_lock_bh(&ioat_chan
->desc_lock
);
180 __ioat1_dma_memcpy_issue_pending(ioat_chan
);
181 spin_unlock_bh(&ioat_chan
->desc_lock
);
185 static inline void __ioat2_dma_memcpy_issue_pending(
186 struct ioat_dma_chan
*ioat_chan
)
188 ioat_chan
->pending
= 0;
189 writew(ioat_chan
->dmacount
,
190 ioat_chan
->reg_base
+ IOAT_CHAN_DMACOUNT_OFFSET
);
193 static void ioat2_dma_memcpy_issue_pending(struct dma_chan
*chan
)
195 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(chan
);
197 if (ioat_chan
->pending
!= 0) {
198 spin_lock_bh(&ioat_chan
->desc_lock
);
199 __ioat2_dma_memcpy_issue_pending(ioat_chan
);
200 spin_unlock_bh(&ioat_chan
->desc_lock
);
204 static dma_cookie_t
ioat1_tx_submit(struct dma_async_tx_descriptor
*tx
)
206 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(tx
->chan
);
207 struct ioat_desc_sw
*first
= tx_to_ioat_desc(tx
);
208 struct ioat_desc_sw
*prev
, *new;
209 struct ioat_dma_descriptor
*hw
;
211 LIST_HEAD(new_chain
);
216 unsigned int desc_count
= 0;
218 /* src and dest and len are stored in the initial descriptor */
222 orig_ack
= first
->async_tx
.ack
;
225 spin_lock_bh(&ioat_chan
->desc_lock
);
226 prev
= to_ioat_desc(ioat_chan
->used_desc
.prev
);
229 copy
= min_t(size_t, len
, ioat_chan
->xfercap
);
231 new->async_tx
.ack
= 1;
240 /* chain together the physical address list for the HW */
242 prev
->hw
->next
= (u64
) new->async_tx
.phys
;
248 list_add_tail(&new->node
, &new_chain
);
251 } while (len
&& (new = ioat1_dma_get_next_descriptor(ioat_chan
)));
253 hw
->ctl
= IOAT_DMA_DESCRIPTOR_CTL_CP_STS
;
254 if (new->async_tx
.callback
) {
255 hw
->ctl
|= IOAT_DMA_DESCRIPTOR_CTL_INT_GN
;
257 /* move callback into to last desc */
258 new->async_tx
.callback
= first
->async_tx
.callback
;
259 new->async_tx
.callback_param
260 = first
->async_tx
.callback_param
;
261 first
->async_tx
.callback
= NULL
;
262 first
->async_tx
.callback_param
= NULL
;
266 new->tx_cnt
= desc_count
;
267 new->async_tx
.ack
= orig_ack
; /* client is in control of this ack */
269 /* store the original values for use in later cleanup */
271 new->src
= first
->src
;
272 new->dst
= first
->dst
;
273 new->len
= first
->len
;
276 /* cookie incr and addition to used_list must be atomic */
277 cookie
= ioat_chan
->common
.cookie
;
281 ioat_chan
->common
.cookie
= new->async_tx
.cookie
= cookie
;
283 /* write address into NextDescriptor field of last desc in chain */
284 to_ioat_desc(ioat_chan
->used_desc
.prev
)->hw
->next
=
285 first
->async_tx
.phys
;
286 __list_splice(&new_chain
, ioat_chan
->used_desc
.prev
);
288 ioat_chan
->dmacount
+= desc_count
;
289 ioat_chan
->pending
+= desc_count
;
290 if (ioat_chan
->pending
>= ioat_pending_level
)
291 __ioat1_dma_memcpy_issue_pending(ioat_chan
);
292 spin_unlock_bh(&ioat_chan
->desc_lock
);
297 static dma_cookie_t
ioat2_tx_submit(struct dma_async_tx_descriptor
*tx
)
299 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(tx
->chan
);
300 struct ioat_desc_sw
*first
= tx_to_ioat_desc(tx
);
301 struct ioat_desc_sw
*new;
302 struct ioat_dma_descriptor
*hw
;
308 unsigned int desc_count
= 0;
310 /* src and dest and len are stored in the initial descriptor */
314 orig_ack
= first
->async_tx
.ack
;
318 * ioat_chan->desc_lock is still in force in version 2 path
319 * it gets unlocked at end of this function
322 copy
= min_t(size_t, len
, ioat_chan
->xfercap
);
324 new->async_tx
.ack
= 1;
336 } while (len
&& (new = ioat2_dma_get_next_descriptor(ioat_chan
)));
338 hw
->ctl
= IOAT_DMA_DESCRIPTOR_CTL_CP_STS
;
339 if (new->async_tx
.callback
) {
340 hw
->ctl
|= IOAT_DMA_DESCRIPTOR_CTL_INT_GN
;
342 /* move callback into to last desc */
343 new->async_tx
.callback
= first
->async_tx
.callback
;
344 new->async_tx
.callback_param
345 = first
->async_tx
.callback_param
;
346 first
->async_tx
.callback
= NULL
;
347 first
->async_tx
.callback_param
= NULL
;
351 new->tx_cnt
= desc_count
;
352 new->async_tx
.ack
= orig_ack
; /* client is in control of this ack */
354 /* store the original values for use in later cleanup */
356 new->src
= first
->src
;
357 new->dst
= first
->dst
;
358 new->len
= first
->len
;
361 /* cookie incr and addition to used_list must be atomic */
362 cookie
= ioat_chan
->common
.cookie
;
366 ioat_chan
->common
.cookie
= new->async_tx
.cookie
= cookie
;
368 ioat_chan
->dmacount
+= desc_count
;
369 ioat_chan
->pending
+= desc_count
;
370 if (ioat_chan
->pending
>= ioat_pending_level
)
371 __ioat2_dma_memcpy_issue_pending(ioat_chan
);
372 spin_unlock_bh(&ioat_chan
->desc_lock
);
378 * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
379 * @ioat_chan: the channel supplying the memory pool for the descriptors
380 * @flags: allocation flags
382 static struct ioat_desc_sw
*ioat_dma_alloc_descriptor(
383 struct ioat_dma_chan
*ioat_chan
,
386 struct ioat_dma_descriptor
*desc
;
387 struct ioat_desc_sw
*desc_sw
;
388 struct ioatdma_device
*ioatdma_device
;
391 ioatdma_device
= to_ioatdma_device(ioat_chan
->common
.device
);
392 desc
= pci_pool_alloc(ioatdma_device
->dma_pool
, flags
, &phys
);
396 desc_sw
= kzalloc(sizeof(*desc_sw
), flags
);
397 if (unlikely(!desc_sw
)) {
398 pci_pool_free(ioatdma_device
->dma_pool
, desc
, phys
);
402 memset(desc
, 0, sizeof(*desc
));
403 dma_async_tx_descriptor_init(&desc_sw
->async_tx
, &ioat_chan
->common
);
404 switch (ioat_chan
->device
->version
) {
406 desc_sw
->async_tx
.tx_submit
= ioat1_tx_submit
;
409 desc_sw
->async_tx
.tx_submit
= ioat2_tx_submit
;
412 INIT_LIST_HEAD(&desc_sw
->async_tx
.tx_list
);
415 desc_sw
->async_tx
.phys
= phys
;
420 static int ioat_initial_desc_count
= 256;
421 module_param(ioat_initial_desc_count
, int, 0644);
422 MODULE_PARM_DESC(ioat_initial_desc_count
,
423 "initial descriptors per channel (default: 256)");
426 * ioat2_dma_massage_chan_desc - link the descriptors into a circle
427 * @ioat_chan: the channel to be massaged
429 static void ioat2_dma_massage_chan_desc(struct ioat_dma_chan
*ioat_chan
)
431 struct ioat_desc_sw
*desc
, *_desc
;
433 /* setup used_desc */
434 ioat_chan
->used_desc
.next
= ioat_chan
->free_desc
.next
;
435 ioat_chan
->used_desc
.prev
= NULL
;
437 /* pull free_desc out of the circle so that every node is a hw
438 * descriptor, but leave it pointing to the list
440 ioat_chan
->free_desc
.prev
->next
= ioat_chan
->free_desc
.next
;
441 ioat_chan
->free_desc
.next
->prev
= ioat_chan
->free_desc
.prev
;
443 /* circle link the hw descriptors */
444 desc
= to_ioat_desc(ioat_chan
->free_desc
.next
);
445 desc
->hw
->next
= to_ioat_desc(desc
->node
.next
)->async_tx
.phys
;
446 list_for_each_entry_safe(desc
, _desc
, ioat_chan
->free_desc
.next
, node
) {
447 desc
->hw
->next
= to_ioat_desc(desc
->node
.next
)->async_tx
.phys
;
452 * ioat_dma_alloc_chan_resources - returns the number of allocated descriptors
453 * @chan: the channel to be filled out
455 static int ioat_dma_alloc_chan_resources(struct dma_chan
*chan
)
457 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(chan
);
458 struct ioat_desc_sw
*desc
;
464 /* have we already been set up? */
465 if (!list_empty(&ioat_chan
->free_desc
))
466 return ioat_chan
->desccount
;
468 /* Setup register to interrupt and write completion status on error */
469 chanctrl
= IOAT_CHANCTRL_ERR_INT_EN
|
470 IOAT_CHANCTRL_ANY_ERR_ABORT_EN
|
471 IOAT_CHANCTRL_ERR_COMPLETION_EN
;
472 writew(chanctrl
, ioat_chan
->reg_base
+ IOAT_CHANCTRL_OFFSET
);
474 chanerr
= readl(ioat_chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
476 dev_err(&ioat_chan
->device
->pdev
->dev
,
477 "CHANERR = %x, clearing\n", chanerr
);
478 writel(chanerr
, ioat_chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
481 /* Allocate descriptors */
482 for (i
= 0; i
< ioat_initial_desc_count
; i
++) {
483 desc
= ioat_dma_alloc_descriptor(ioat_chan
, GFP_KERNEL
);
485 dev_err(&ioat_chan
->device
->pdev
->dev
,
486 "Only %d initial descriptors\n", i
);
489 list_add_tail(&desc
->node
, &tmp_list
);
491 spin_lock_bh(&ioat_chan
->desc_lock
);
492 ioat_chan
->desccount
= i
;
493 list_splice(&tmp_list
, &ioat_chan
->free_desc
);
494 if (ioat_chan
->device
->version
!= IOAT_VER_1_2
)
495 ioat2_dma_massage_chan_desc(ioat_chan
);
496 spin_unlock_bh(&ioat_chan
->desc_lock
);
498 /* allocate a completion writeback area */
499 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
500 ioat_chan
->completion_virt
=
501 pci_pool_alloc(ioat_chan
->device
->completion_pool
,
503 &ioat_chan
->completion_addr
);
504 memset(ioat_chan
->completion_virt
, 0,
505 sizeof(*ioat_chan
->completion_virt
));
506 writel(((u64
) ioat_chan
->completion_addr
) & 0x00000000FFFFFFFF,
507 ioat_chan
->reg_base
+ IOAT_CHANCMP_OFFSET_LOW
);
508 writel(((u64
) ioat_chan
->completion_addr
) >> 32,
509 ioat_chan
->reg_base
+ IOAT_CHANCMP_OFFSET_HIGH
);
511 tasklet_enable(&ioat_chan
->cleanup_task
);
512 ioat_dma_start_null_desc(ioat_chan
); /* give chain to dma device */
513 return ioat_chan
->desccount
;
517 * ioat_dma_free_chan_resources - release all the descriptors
518 * @chan: the channel to be cleaned
520 static void ioat_dma_free_chan_resources(struct dma_chan
*chan
)
522 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(chan
);
523 struct ioatdma_device
*ioatdma_device
= to_ioatdma_device(chan
->device
);
524 struct ioat_desc_sw
*desc
, *_desc
;
525 int in_use_descs
= 0;
527 tasklet_disable(&ioat_chan
->cleanup_task
);
528 ioat_dma_memcpy_cleanup(ioat_chan
);
530 /* Delay 100ms after reset to allow internal DMA logic to quiesce
531 * before removing DMA descriptor resources.
533 writeb(IOAT_CHANCMD_RESET
,
535 + IOAT_CHANCMD_OFFSET(ioat_chan
->device
->version
));
538 spin_lock_bh(&ioat_chan
->desc_lock
);
539 switch (ioat_chan
->device
->version
) {
541 list_for_each_entry_safe(desc
, _desc
,
542 &ioat_chan
->used_desc
, node
) {
544 list_del(&desc
->node
);
545 pci_pool_free(ioatdma_device
->dma_pool
, desc
->hw
,
546 desc
->async_tx
.phys
);
549 list_for_each_entry_safe(desc
, _desc
,
550 &ioat_chan
->free_desc
, node
) {
551 list_del(&desc
->node
);
552 pci_pool_free(ioatdma_device
->dma_pool
, desc
->hw
,
553 desc
->async_tx
.phys
);
558 list_for_each_entry_safe(desc
, _desc
,
559 ioat_chan
->free_desc
.next
, node
) {
560 list_del(&desc
->node
);
561 pci_pool_free(ioatdma_device
->dma_pool
, desc
->hw
,
562 desc
->async_tx
.phys
);
565 desc
= to_ioat_desc(ioat_chan
->free_desc
.next
);
566 pci_pool_free(ioatdma_device
->dma_pool
, desc
->hw
,
567 desc
->async_tx
.phys
);
569 INIT_LIST_HEAD(&ioat_chan
->free_desc
);
570 INIT_LIST_HEAD(&ioat_chan
->used_desc
);
573 spin_unlock_bh(&ioat_chan
->desc_lock
);
575 pci_pool_free(ioatdma_device
->completion_pool
,
576 ioat_chan
->completion_virt
,
577 ioat_chan
->completion_addr
);
579 /* one is ok since we left it on there on purpose */
580 if (in_use_descs
> 1)
581 dev_err(&ioat_chan
->device
->pdev
->dev
,
582 "Freeing %d in use descriptors!\n",
585 ioat_chan
->last_completion
= ioat_chan
->completion_addr
= 0;
586 ioat_chan
->pending
= 0;
587 ioat_chan
->dmacount
= 0;
591 * ioat_dma_get_next_descriptor - return the next available descriptor
592 * @ioat_chan: IOAT DMA channel handle
594 * Gets the next descriptor from the chain, and must be called with the
595 * channel's desc_lock held. Allocates more descriptors if the channel
598 static struct ioat_desc_sw
*
599 ioat1_dma_get_next_descriptor(struct ioat_dma_chan
*ioat_chan
)
601 struct ioat_desc_sw
*new;
603 if (!list_empty(&ioat_chan
->free_desc
)) {
604 new = to_ioat_desc(ioat_chan
->free_desc
.next
);
605 list_del(&new->node
);
607 /* try to get another desc */
608 new = ioat_dma_alloc_descriptor(ioat_chan
, GFP_ATOMIC
);
610 dev_err(&ioat_chan
->device
->pdev
->dev
,
620 static struct ioat_desc_sw
*
621 ioat2_dma_get_next_descriptor(struct ioat_dma_chan
*ioat_chan
)
623 struct ioat_desc_sw
*new;
626 * used.prev points to where to start processing
627 * used.next points to next free descriptor
628 * if used.prev == NULL, there are none waiting to be processed
629 * if used.next == used.prev.prev, there is only one free descriptor,
630 * and we need to use it to as a noop descriptor before
631 * linking in a new set of descriptors, since the device
632 * has probably already read the pointer to it
634 if (ioat_chan
->used_desc
.prev
&&
635 ioat_chan
->used_desc
.next
== ioat_chan
->used_desc
.prev
->prev
) {
637 struct ioat_desc_sw
*desc
;
638 struct ioat_desc_sw
*noop_desc
;
641 /* set up the noop descriptor */
642 noop_desc
= to_ioat_desc(ioat_chan
->used_desc
.next
);
643 noop_desc
->hw
->size
= 0;
644 noop_desc
->hw
->ctl
= IOAT_DMA_DESCRIPTOR_NUL
;
645 noop_desc
->hw
->src_addr
= 0;
646 noop_desc
->hw
->dst_addr
= 0;
648 ioat_chan
->used_desc
.next
= ioat_chan
->used_desc
.next
->next
;
649 ioat_chan
->pending
++;
650 ioat_chan
->dmacount
++;
652 /* try to get a few more descriptors */
653 for (i
= 16; i
; i
--) {
654 desc
= ioat_dma_alloc_descriptor(ioat_chan
, GFP_ATOMIC
);
656 dev_err(&ioat_chan
->device
->pdev
->dev
,
660 list_add_tail(&desc
->node
, ioat_chan
->used_desc
.next
);
663 = to_ioat_desc(desc
->node
.next
)->async_tx
.phys
;
664 to_ioat_desc(desc
->node
.prev
)->hw
->next
665 = desc
->async_tx
.phys
;
666 ioat_chan
->desccount
++;
669 ioat_chan
->used_desc
.next
= noop_desc
->node
.next
;
671 new = to_ioat_desc(ioat_chan
->used_desc
.next
);
673 ioat_chan
->used_desc
.next
= new->node
.next
;
675 if (ioat_chan
->used_desc
.prev
== NULL
)
676 ioat_chan
->used_desc
.prev
= &new->node
;
682 static struct ioat_desc_sw
*ioat_dma_get_next_descriptor(
683 struct ioat_dma_chan
*ioat_chan
)
688 switch (ioat_chan
->device
->version
) {
690 return ioat1_dma_get_next_descriptor(ioat_chan
);
693 return ioat2_dma_get_next_descriptor(ioat_chan
);
699 static struct dma_async_tx_descriptor
*ioat1_dma_prep_memcpy(
700 struct dma_chan
*chan
,
706 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(chan
);
707 struct ioat_desc_sw
*new;
709 spin_lock_bh(&ioat_chan
->desc_lock
);
710 new = ioat_dma_get_next_descriptor(ioat_chan
);
711 spin_unlock_bh(&ioat_chan
->desc_lock
);
717 new->async_tx
.ack
= 0;
718 return &new->async_tx
;
723 static struct dma_async_tx_descriptor
*ioat2_dma_prep_memcpy(
724 struct dma_chan
*chan
,
730 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(chan
);
731 struct ioat_desc_sw
*new;
733 spin_lock_bh(&ioat_chan
->desc_lock
);
734 new = ioat2_dma_get_next_descriptor(ioat_chan
);
737 * leave ioat_chan->desc_lock set in ioat 2 path
738 * it will get unlocked at end of tx_submit
745 new->async_tx
.ack
= 0;
746 return &new->async_tx
;
751 static void ioat_dma_cleanup_tasklet(unsigned long data
)
753 struct ioat_dma_chan
*chan
= (void *)data
;
754 ioat_dma_memcpy_cleanup(chan
);
755 writew(IOAT_CHANCTRL_INT_DISABLE
,
756 chan
->reg_base
+ IOAT_CHANCTRL_OFFSET
);
760 * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
761 * @chan: ioat channel to be cleaned up
763 static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan
*ioat_chan
)
765 unsigned long phys_complete
;
766 struct ioat_desc_sw
*desc
, *_desc
;
767 dma_cookie_t cookie
= 0;
768 unsigned long desc_phys
;
769 struct ioat_desc_sw
*latest_desc
;
771 prefetch(ioat_chan
->completion_virt
);
773 if (!spin_trylock_bh(&ioat_chan
->cleanup_lock
))
776 /* The completion writeback can happen at any time,
777 so reads by the driver need to be atomic operations
778 The descriptor physical addresses are limited to 32-bits
779 when the CPU can only do a 32-bit mov */
781 #if (BITS_PER_LONG == 64)
783 ioat_chan
->completion_virt
->full
784 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR
;
787 ioat_chan
->completion_virt
->low
& IOAT_LOW_COMPLETION_MASK
;
790 if ((ioat_chan
->completion_virt
->full
791 & IOAT_CHANSTS_DMA_TRANSFER_STATUS
) ==
792 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED
) {
793 dev_err(&ioat_chan
->device
->pdev
->dev
,
794 "Channel halted, chanerr = %x\n",
795 readl(ioat_chan
->reg_base
+ IOAT_CHANERR_OFFSET
));
797 /* TODO do something to salvage the situation */
800 if (phys_complete
== ioat_chan
->last_completion
) {
801 spin_unlock_bh(&ioat_chan
->cleanup_lock
);
806 spin_lock_bh(&ioat_chan
->desc_lock
);
807 switch (ioat_chan
->device
->version
) {
809 list_for_each_entry_safe(desc
, _desc
,
810 &ioat_chan
->used_desc
, node
) {
813 * Incoming DMA requests may use multiple descriptors,
814 * due to exceeding xfercap, perhaps. If so, only the
815 * last one will have a cookie, and require unmapping.
817 if (desc
->async_tx
.cookie
) {
818 cookie
= desc
->async_tx
.cookie
;
821 * yes we are unmapping both _page and _single
822 * alloc'd regions with unmap_page. Is this
825 pci_unmap_page(ioat_chan
->device
->pdev
,
826 pci_unmap_addr(desc
, dst
),
827 pci_unmap_len(desc
, len
),
829 pci_unmap_page(ioat_chan
->device
->pdev
,
830 pci_unmap_addr(desc
, src
),
831 pci_unmap_len(desc
, len
),
834 if (desc
->async_tx
.callback
) {
835 desc
->async_tx
.callback(desc
->async_tx
.callback_param
);
836 desc
->async_tx
.callback
= NULL
;
840 if (desc
->async_tx
.phys
!= phys_complete
) {
842 * a completed entry, but not the last, so clean
843 * up if the client is done with the descriptor
845 if (desc
->async_tx
.ack
) {
846 list_del(&desc
->node
);
847 list_add_tail(&desc
->node
,
848 &ioat_chan
->free_desc
);
850 desc
->async_tx
.cookie
= 0;
853 * last used desc. Do not remove, so we can
854 * append from it, but don't look at it next
857 desc
->async_tx
.cookie
= 0;
859 /* TODO check status bits? */
865 /* has some other thread has already cleaned up? */
866 if (ioat_chan
->used_desc
.prev
== NULL
)
869 /* work backwards to find latest finished desc */
870 desc
= to_ioat_desc(ioat_chan
->used_desc
.next
);
873 desc
= to_ioat_desc(desc
->node
.prev
);
874 desc_phys
= (unsigned long)desc
->async_tx
.phys
875 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR
;
876 if (desc_phys
== phys_complete
) {
880 } while (&desc
->node
!= ioat_chan
->used_desc
.prev
);
882 if (latest_desc
!= NULL
) {
884 /* work forwards to clear finished descriptors */
885 for (desc
= to_ioat_desc(ioat_chan
->used_desc
.prev
);
886 &desc
->node
!= latest_desc
->node
.next
&&
887 &desc
->node
!= ioat_chan
->used_desc
.next
;
888 desc
= to_ioat_desc(desc
->node
.next
)) {
889 if (desc
->async_tx
.cookie
) {
890 cookie
= desc
->async_tx
.cookie
;
891 desc
->async_tx
.cookie
= 0;
893 pci_unmap_page(ioat_chan
->device
->pdev
,
894 pci_unmap_addr(desc
, dst
),
895 pci_unmap_len(desc
, len
),
897 pci_unmap_page(ioat_chan
->device
->pdev
,
898 pci_unmap_addr(desc
, src
),
899 pci_unmap_len(desc
, len
),
902 if (desc
->async_tx
.callback
) {
903 desc
->async_tx
.callback(desc
->async_tx
.callback_param
);
904 desc
->async_tx
.callback
= NULL
;
909 /* move used.prev up beyond those that are finished */
910 if (&desc
->node
== ioat_chan
->used_desc
.next
)
911 ioat_chan
->used_desc
.prev
= NULL
;
913 ioat_chan
->used_desc
.prev
= &desc
->node
;
918 spin_unlock_bh(&ioat_chan
->desc_lock
);
920 ioat_chan
->last_completion
= phys_complete
;
922 ioat_chan
->completed_cookie
= cookie
;
924 spin_unlock_bh(&ioat_chan
->cleanup_lock
);
927 static void ioat_dma_dependency_added(struct dma_chan
*chan
)
929 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(chan
);
930 spin_lock_bh(&ioat_chan
->desc_lock
);
931 if (ioat_chan
->pending
== 0) {
932 spin_unlock_bh(&ioat_chan
->desc_lock
);
933 ioat_dma_memcpy_cleanup(ioat_chan
);
935 spin_unlock_bh(&ioat_chan
->desc_lock
);
939 * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
940 * @chan: IOAT DMA channel handle
941 * @cookie: DMA transaction identifier
942 * @done: if not %NULL, updated with last completed transaction
943 * @used: if not %NULL, updated with last used transaction
945 static enum dma_status
ioat_dma_is_complete(struct dma_chan
*chan
,
950 struct ioat_dma_chan
*ioat_chan
= to_ioat_chan(chan
);
951 dma_cookie_t last_used
;
952 dma_cookie_t last_complete
;
955 last_used
= chan
->cookie
;
956 last_complete
= ioat_chan
->completed_cookie
;
959 *done
= last_complete
;
963 ret
= dma_async_is_complete(cookie
, last_complete
, last_used
);
964 if (ret
== DMA_SUCCESS
)
967 ioat_dma_memcpy_cleanup(ioat_chan
);
969 last_used
= chan
->cookie
;
970 last_complete
= ioat_chan
->completed_cookie
;
973 *done
= last_complete
;
977 return dma_async_is_complete(cookie
, last_complete
, last_used
);
980 static void ioat_dma_start_null_desc(struct ioat_dma_chan
*ioat_chan
)
982 struct ioat_desc_sw
*desc
;
984 spin_lock_bh(&ioat_chan
->desc_lock
);
986 desc
= ioat_dma_get_next_descriptor(ioat_chan
);
987 desc
->hw
->ctl
= IOAT_DMA_DESCRIPTOR_NUL
988 | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
989 | IOAT_DMA_DESCRIPTOR_CTL_CP_STS
;
991 desc
->hw
->src_addr
= 0;
992 desc
->hw
->dst_addr
= 0;
993 desc
->async_tx
.ack
= 1;
994 switch (ioat_chan
->device
->version
) {
997 list_add_tail(&desc
->node
, &ioat_chan
->used_desc
);
999 writel(((u64
) desc
->async_tx
.phys
) & 0x00000000FFFFFFFF,
1000 ioat_chan
->reg_base
+ IOAT1_CHAINADDR_OFFSET_LOW
);
1001 writel(((u64
) desc
->async_tx
.phys
) >> 32,
1002 ioat_chan
->reg_base
+ IOAT1_CHAINADDR_OFFSET_HIGH
);
1004 writeb(IOAT_CHANCMD_START
, ioat_chan
->reg_base
1005 + IOAT_CHANCMD_OFFSET(ioat_chan
->device
->version
));
1008 writel(((u64
) desc
->async_tx
.phys
) & 0x00000000FFFFFFFF,
1009 ioat_chan
->reg_base
+ IOAT2_CHAINADDR_OFFSET_LOW
);
1010 writel(((u64
) desc
->async_tx
.phys
) >> 32,
1011 ioat_chan
->reg_base
+ IOAT2_CHAINADDR_OFFSET_HIGH
);
1013 ioat_chan
->dmacount
++;
1014 __ioat2_dma_memcpy_issue_pending(ioat_chan
);
1017 spin_unlock_bh(&ioat_chan
->desc_lock
);
1021 * Perform a IOAT transaction to verify the HW works.
1023 #define IOAT_TEST_SIZE 2000
1025 static void ioat_dma_test_callback(void *dma_async_param
)
1027 printk(KERN_ERR
"ioatdma: ioat_dma_test_callback(%p)\n",
1032 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
1033 * @device: device to be tested
1035 static int ioat_dma_self_test(struct ioatdma_device
*device
)
1040 struct dma_chan
*dma_chan
;
1041 struct dma_async_tx_descriptor
*tx
;
1042 dma_addr_t dma_dest
, dma_src
;
1043 dma_cookie_t cookie
;
1046 src
= kzalloc(sizeof(u8
) * IOAT_TEST_SIZE
, GFP_KERNEL
);
1049 dest
= kzalloc(sizeof(u8
) * IOAT_TEST_SIZE
, GFP_KERNEL
);
1055 /* Fill in src buffer */
1056 for (i
= 0; i
< IOAT_TEST_SIZE
; i
++)
1059 /* Start copy, using first DMA channel */
1060 dma_chan
= container_of(device
->common
.channels
.next
,
1063 if (device
->common
.device_alloc_chan_resources(dma_chan
) < 1) {
1064 dev_err(&device
->pdev
->dev
,
1065 "selftest cannot allocate chan resource\n");
1070 dma_src
= dma_map_single(dma_chan
->device
->dev
, src
, IOAT_TEST_SIZE
,
1072 dma_dest
= dma_map_single(dma_chan
->device
->dev
, dest
, IOAT_TEST_SIZE
,
1074 tx
= device
->common
.device_prep_dma_memcpy(dma_chan
, dma_dest
, dma_src
,
1077 dev_err(&device
->pdev
->dev
,
1078 "Self-test prep failed, disabling\n");
1080 goto free_resources
;
1084 tx
->callback
= ioat_dma_test_callback
;
1085 tx
->callback_param
= (void *)0x8086;
1086 cookie
= tx
->tx_submit(tx
);
1088 dev_err(&device
->pdev
->dev
,
1089 "Self-test setup failed, disabling\n");
1091 goto free_resources
;
1093 device
->common
.device_issue_pending(dma_chan
);
1096 if (device
->common
.device_is_tx_complete(dma_chan
, cookie
, NULL
, NULL
)
1098 dev_err(&device
->pdev
->dev
,
1099 "Self-test copy timed out, disabling\n");
1101 goto free_resources
;
1103 if (memcmp(src
, dest
, IOAT_TEST_SIZE
)) {
1104 dev_err(&device
->pdev
->dev
,
1105 "Self-test copy failed compare, disabling\n");
1107 goto free_resources
;
1111 device
->common
.device_free_chan_resources(dma_chan
);
1118 static char ioat_interrupt_style
[32] = "msix";
1119 module_param_string(ioat_interrupt_style
, ioat_interrupt_style
,
1120 sizeof(ioat_interrupt_style
), 0644);
1121 MODULE_PARM_DESC(ioat_interrupt_style
,
1122 "set ioat interrupt style: msix (default), "
1123 "msix-single-vector, msi, intx)");
1126 * ioat_dma_setup_interrupts - setup interrupt handler
1127 * @device: ioat device
1129 static int ioat_dma_setup_interrupts(struct ioatdma_device
*device
)
1131 struct ioat_dma_chan
*ioat_chan
;
1132 int err
, i
, j
, msixcnt
;
1135 if (!strcmp(ioat_interrupt_style
, "msix"))
1137 if (!strcmp(ioat_interrupt_style
, "msix-single-vector"))
1138 goto msix_single_vector
;
1139 if (!strcmp(ioat_interrupt_style
, "msi"))
1141 if (!strcmp(ioat_interrupt_style
, "intx"))
1143 dev_err(&device
->pdev
->dev
, "invalid ioat_interrupt_style %s\n",
1144 ioat_interrupt_style
);
1148 /* The number of MSI-X vectors should equal the number of channels */
1149 msixcnt
= device
->common
.chancnt
;
1150 for (i
= 0; i
< msixcnt
; i
++)
1151 device
->msix_entries
[i
].entry
= i
;
1153 err
= pci_enable_msix(device
->pdev
, device
->msix_entries
, msixcnt
);
1157 goto msix_single_vector
;
1159 for (i
= 0; i
< msixcnt
; i
++) {
1160 ioat_chan
= ioat_lookup_chan_by_index(device
, i
);
1161 err
= request_irq(device
->msix_entries
[i
].vector
,
1162 ioat_dma_do_interrupt_msix
,
1163 0, "ioat-msix", ioat_chan
);
1165 for (j
= 0; j
< i
; j
++) {
1167 ioat_lookup_chan_by_index(device
, j
);
1168 free_irq(device
->msix_entries
[j
].vector
,
1171 goto msix_single_vector
;
1174 intrctrl
|= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL
;
1175 device
->irq_mode
= msix_multi_vector
;
1179 device
->msix_entries
[0].entry
= 0;
1180 err
= pci_enable_msix(device
->pdev
, device
->msix_entries
, 1);
1184 err
= request_irq(device
->msix_entries
[0].vector
, ioat_dma_do_interrupt
,
1185 0, "ioat-msix", device
);
1187 pci_disable_msix(device
->pdev
);
1190 device
->irq_mode
= msix_single_vector
;
1194 err
= pci_enable_msi(device
->pdev
);
1198 err
= request_irq(device
->pdev
->irq
, ioat_dma_do_interrupt
,
1199 0, "ioat-msi", device
);
1201 pci_disable_msi(device
->pdev
);
1205 * CB 1.2 devices need a bit set in configuration space to enable MSI
1207 if (device
->version
== IOAT_VER_1_2
) {
1209 pci_read_config_dword(device
->pdev
,
1210 IOAT_PCI_DMACTRL_OFFSET
, &dmactrl
);
1211 dmactrl
|= IOAT_PCI_DMACTRL_MSI_EN
;
1212 pci_write_config_dword(device
->pdev
,
1213 IOAT_PCI_DMACTRL_OFFSET
, dmactrl
);
1215 device
->irq_mode
= msi
;
1219 err
= request_irq(device
->pdev
->irq
, ioat_dma_do_interrupt
,
1220 IRQF_SHARED
, "ioat-intx", device
);
1223 device
->irq_mode
= intx
;
1226 intrctrl
|= IOAT_INTRCTRL_MASTER_INT_EN
;
1227 writeb(intrctrl
, device
->reg_base
+ IOAT_INTRCTRL_OFFSET
);
1231 /* Disable all interrupt generation */
1232 writeb(0, device
->reg_base
+ IOAT_INTRCTRL_OFFSET
);
1233 dev_err(&device
->pdev
->dev
, "no usable interrupts\n");
1234 device
->irq_mode
= none
;
1239 * ioat_dma_remove_interrupts - remove whatever interrupts were set
1240 * @device: ioat device
1242 static void ioat_dma_remove_interrupts(struct ioatdma_device
*device
)
1244 struct ioat_dma_chan
*ioat_chan
;
1247 /* Disable all interrupt generation */
1248 writeb(0, device
->reg_base
+ IOAT_INTRCTRL_OFFSET
);
1250 switch (device
->irq_mode
) {
1251 case msix_multi_vector
:
1252 for (i
= 0; i
< device
->common
.chancnt
; i
++) {
1253 ioat_chan
= ioat_lookup_chan_by_index(device
, i
);
1254 free_irq(device
->msix_entries
[i
].vector
, ioat_chan
);
1256 pci_disable_msix(device
->pdev
);
1258 case msix_single_vector
:
1259 free_irq(device
->msix_entries
[0].vector
, device
);
1260 pci_disable_msix(device
->pdev
);
1263 free_irq(device
->pdev
->irq
, device
);
1264 pci_disable_msi(device
->pdev
);
1267 free_irq(device
->pdev
->irq
, device
);
1270 dev_warn(&device
->pdev
->dev
,
1271 "call to %s without interrupts setup\n", __func__
);
1273 device
->irq_mode
= none
;
1276 struct ioatdma_device
*ioat_dma_probe(struct pci_dev
*pdev
,
1277 void __iomem
*iobase
)
1280 struct ioatdma_device
*device
;
1282 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
1287 device
->pdev
= pdev
;
1288 device
->reg_base
= iobase
;
1289 device
->version
= readb(device
->reg_base
+ IOAT_VER_OFFSET
);
1291 /* DMA coherent memory pool for DMA descriptor allocations */
1292 device
->dma_pool
= pci_pool_create("dma_desc_pool", pdev
,
1293 sizeof(struct ioat_dma_descriptor
),
1295 if (!device
->dma_pool
) {
1300 device
->completion_pool
= pci_pool_create("completion_pool", pdev
,
1301 sizeof(u64
), SMP_CACHE_BYTES
,
1303 if (!device
->completion_pool
) {
1305 goto err_completion_pool
;
1308 INIT_LIST_HEAD(&device
->common
.channels
);
1309 ioat_dma_enumerate_channels(device
);
1311 device
->common
.device_alloc_chan_resources
=
1312 ioat_dma_alloc_chan_resources
;
1313 device
->common
.device_free_chan_resources
=
1314 ioat_dma_free_chan_resources
;
1315 device
->common
.dev
= &pdev
->dev
;
1317 dma_cap_set(DMA_MEMCPY
, device
->common
.cap_mask
);
1318 device
->common
.device_is_tx_complete
= ioat_dma_is_complete
;
1319 device
->common
.device_dependency_added
= ioat_dma_dependency_added
;
1320 switch (device
->version
) {
1322 device
->common
.device_prep_dma_memcpy
= ioat1_dma_prep_memcpy
;
1323 device
->common
.device_issue_pending
=
1324 ioat1_dma_memcpy_issue_pending
;
1327 device
->common
.device_prep_dma_memcpy
= ioat2_dma_prep_memcpy
;
1328 device
->common
.device_issue_pending
=
1329 ioat2_dma_memcpy_issue_pending
;
1333 dev_err(&device
->pdev
->dev
,
1334 "Intel(R) I/OAT DMA Engine found,"
1335 " %d channels, device version 0x%02x, driver version %s\n",
1336 device
->common
.chancnt
, device
->version
, IOAT_DMA_VERSION
);
1338 err
= ioat_dma_setup_interrupts(device
);
1340 goto err_setup_interrupts
;
1342 err
= ioat_dma_self_test(device
);
1346 dma_async_device_register(&device
->common
);
1351 ioat_dma_remove_interrupts(device
);
1352 err_setup_interrupts
:
1353 pci_pool_destroy(device
->completion_pool
);
1354 err_completion_pool
:
1355 pci_pool_destroy(device
->dma_pool
);
1360 "Intel(R) I/OAT DMA Engine initialization failed\n");
1364 void ioat_dma_remove(struct ioatdma_device
*device
)
1366 struct dma_chan
*chan
, *_chan
;
1367 struct ioat_dma_chan
*ioat_chan
;
1369 ioat_dma_remove_interrupts(device
);
1371 dma_async_device_unregister(&device
->common
);
1373 pci_pool_destroy(device
->dma_pool
);
1374 pci_pool_destroy(device
->completion_pool
);
1376 iounmap(device
->reg_base
);
1377 pci_release_regions(device
->pdev
);
1378 pci_disable_device(device
->pdev
);
1380 list_for_each_entry_safe(chan
, _chan
,
1381 &device
->common
.channels
, device_node
) {
1382 ioat_chan
= to_ioat_chan(chan
);
1383 list_del(&chan
->device_node
);