2 * offload engine driver for the Marvell XOR engine
3 * Copyright (C) 2007, 2008, Marvell International Ltd.
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 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.
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/platform_device.h>
27 #include <linux/memory.h>
28 #include <linux/clk.h>
29 #include <plat/mv_xor.h>
31 #include "dmaengine.h"
34 static void mv_xor_issue_pending(struct dma_chan
*chan
);
36 #define to_mv_xor_chan(chan) \
37 container_of(chan, struct mv_xor_chan, common)
39 #define to_mv_xor_device(dev) \
40 container_of(dev, struct mv_xor_device, common)
42 #define to_mv_xor_slot(tx) \
43 container_of(tx, struct mv_xor_desc_slot, async_tx)
45 static void mv_desc_init(struct mv_xor_desc_slot
*desc
, unsigned long flags
)
47 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
49 hw_desc
->status
= (1 << 31);
50 hw_desc
->phy_next_desc
= 0;
51 hw_desc
->desc_command
= (1 << 31);
54 static u32
mv_desc_get_dest_addr(struct mv_xor_desc_slot
*desc
)
56 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
57 return hw_desc
->phy_dest_addr
;
60 static u32
mv_desc_get_src_addr(struct mv_xor_desc_slot
*desc
,
63 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
64 return hw_desc
->phy_src_addr
[src_idx
];
68 static void mv_desc_set_byte_count(struct mv_xor_desc_slot
*desc
,
71 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
72 hw_desc
->byte_count
= byte_count
;
75 static void mv_desc_set_next_desc(struct mv_xor_desc_slot
*desc
,
78 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
79 BUG_ON(hw_desc
->phy_next_desc
);
80 hw_desc
->phy_next_desc
= next_desc_addr
;
83 static void mv_desc_clear_next_desc(struct mv_xor_desc_slot
*desc
)
85 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
86 hw_desc
->phy_next_desc
= 0;
89 static void mv_desc_set_block_fill_val(struct mv_xor_desc_slot
*desc
, u32 val
)
94 static void mv_desc_set_dest_addr(struct mv_xor_desc_slot
*desc
,
97 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
98 hw_desc
->phy_dest_addr
= addr
;
101 static int mv_chan_memset_slot_count(size_t len
)
106 #define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c)
108 static void mv_desc_set_src_addr(struct mv_xor_desc_slot
*desc
,
109 int index
, dma_addr_t addr
)
111 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
112 hw_desc
->phy_src_addr
[index
] = addr
;
113 if (desc
->type
== DMA_XOR
)
114 hw_desc
->desc_command
|= (1 << index
);
117 static u32
mv_chan_get_current_desc(struct mv_xor_chan
*chan
)
119 return __raw_readl(XOR_CURR_DESC(chan
));
122 static void mv_chan_set_next_descriptor(struct mv_xor_chan
*chan
,
125 __raw_writel(next_desc_addr
, XOR_NEXT_DESC(chan
));
128 static void mv_chan_set_dest_pointer(struct mv_xor_chan
*chan
, u32 desc_addr
)
130 __raw_writel(desc_addr
, XOR_DEST_POINTER(chan
));
133 static void mv_chan_set_block_size(struct mv_xor_chan
*chan
, u32 block_size
)
135 __raw_writel(block_size
, XOR_BLOCK_SIZE(chan
));
138 static void mv_chan_set_value(struct mv_xor_chan
*chan
, u32 value
)
140 __raw_writel(value
, XOR_INIT_VALUE_LOW(chan
));
141 __raw_writel(value
, XOR_INIT_VALUE_HIGH(chan
));
144 static void mv_chan_unmask_interrupts(struct mv_xor_chan
*chan
)
146 u32 val
= __raw_readl(XOR_INTR_MASK(chan
));
147 val
|= XOR_INTR_MASK_VALUE
<< (chan
->idx
* 16);
148 __raw_writel(val
, XOR_INTR_MASK(chan
));
151 static u32
mv_chan_get_intr_cause(struct mv_xor_chan
*chan
)
153 u32 intr_cause
= __raw_readl(XOR_INTR_CAUSE(chan
));
154 intr_cause
= (intr_cause
>> (chan
->idx
* 16)) & 0xFFFF;
158 static int mv_is_err_intr(u32 intr_cause
)
160 if (intr_cause
& ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9)))
166 static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan
*chan
)
168 u32 val
= ~(1 << (chan
->idx
* 16));
169 dev_dbg(chan
->device
->common
.dev
, "%s, val 0x%08x\n", __func__
, val
);
170 __raw_writel(val
, XOR_INTR_CAUSE(chan
));
173 static void mv_xor_device_clear_err_status(struct mv_xor_chan
*chan
)
175 u32 val
= 0xFFFF0000 >> (chan
->idx
* 16);
176 __raw_writel(val
, XOR_INTR_CAUSE(chan
));
179 static int mv_can_chain(struct mv_xor_desc_slot
*desc
)
181 struct mv_xor_desc_slot
*chain_old_tail
= list_entry(
182 desc
->chain_node
.prev
, struct mv_xor_desc_slot
, chain_node
);
184 if (chain_old_tail
->type
!= desc
->type
)
186 if (desc
->type
== DMA_MEMSET
)
192 static void mv_set_mode(struct mv_xor_chan
*chan
,
193 enum dma_transaction_type type
)
196 u32 config
= __raw_readl(XOR_CONFIG(chan
));
200 op_mode
= XOR_OPERATION_MODE_XOR
;
203 op_mode
= XOR_OPERATION_MODE_MEMCPY
;
206 op_mode
= XOR_OPERATION_MODE_MEMSET
;
209 dev_printk(KERN_ERR
, chan
->device
->common
.dev
,
210 "error: unsupported operation %d.\n",
218 __raw_writel(config
, XOR_CONFIG(chan
));
219 chan
->current_type
= type
;
222 static void mv_chan_activate(struct mv_xor_chan
*chan
)
226 dev_dbg(chan
->device
->common
.dev
, " activate chan.\n");
227 activation
= __raw_readl(XOR_ACTIVATION(chan
));
229 __raw_writel(activation
, XOR_ACTIVATION(chan
));
232 static char mv_chan_is_busy(struct mv_xor_chan
*chan
)
234 u32 state
= __raw_readl(XOR_ACTIVATION(chan
));
236 state
= (state
>> 4) & 0x3;
238 return (state
== 1) ? 1 : 0;
241 static int mv_chan_xor_slot_count(size_t len
, int src_cnt
)
247 * mv_xor_free_slots - flags descriptor slots for reuse
248 * @slot: Slot to free
249 * Caller must hold &mv_chan->lock while calling this function
251 static void mv_xor_free_slots(struct mv_xor_chan
*mv_chan
,
252 struct mv_xor_desc_slot
*slot
)
254 dev_dbg(mv_chan
->device
->common
.dev
, "%s %d slot %p\n",
255 __func__
, __LINE__
, slot
);
257 slot
->slots_per_op
= 0;
262 * mv_xor_start_new_chain - program the engine to operate on new chain headed by
264 * Caller must hold &mv_chan->lock while calling this function
266 static void mv_xor_start_new_chain(struct mv_xor_chan
*mv_chan
,
267 struct mv_xor_desc_slot
*sw_desc
)
269 dev_dbg(mv_chan
->device
->common
.dev
, "%s %d: sw_desc %p\n",
270 __func__
, __LINE__
, sw_desc
);
271 if (sw_desc
->type
!= mv_chan
->current_type
)
272 mv_set_mode(mv_chan
, sw_desc
->type
);
274 if (sw_desc
->type
== DMA_MEMSET
) {
275 /* for memset requests we need to program the engine, no
278 struct mv_xor_desc
*hw_desc
= sw_desc
->hw_desc
;
279 mv_chan_set_dest_pointer(mv_chan
, hw_desc
->phy_dest_addr
);
280 mv_chan_set_block_size(mv_chan
, sw_desc
->unmap_len
);
281 mv_chan_set_value(mv_chan
, sw_desc
->value
);
283 /* set the hardware chain */
284 mv_chan_set_next_descriptor(mv_chan
, sw_desc
->async_tx
.phys
);
286 mv_chan
->pending
+= sw_desc
->slot_cnt
;
287 mv_xor_issue_pending(&mv_chan
->common
);
291 mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot
*desc
,
292 struct mv_xor_chan
*mv_chan
, dma_cookie_t cookie
)
294 BUG_ON(desc
->async_tx
.cookie
< 0);
296 if (desc
->async_tx
.cookie
> 0) {
297 cookie
= desc
->async_tx
.cookie
;
299 /* call the callback (must not sleep or submit new
300 * operations to this channel)
302 if (desc
->async_tx
.callback
)
303 desc
->async_tx
.callback(
304 desc
->async_tx
.callback_param
);
306 /* unmap dma addresses
307 * (unmap_single vs unmap_page?)
309 if (desc
->group_head
&& desc
->unmap_len
) {
310 struct mv_xor_desc_slot
*unmap
= desc
->group_head
;
312 &mv_chan
->device
->pdev
->dev
;
313 u32 len
= unmap
->unmap_len
;
314 enum dma_ctrl_flags flags
= desc
->async_tx
.flags
;
319 src_cnt
= unmap
->unmap_src_cnt
;
320 dest
= mv_desc_get_dest_addr(unmap
);
321 if (!(flags
& DMA_COMPL_SKIP_DEST_UNMAP
)) {
322 enum dma_data_direction dir
;
324 if (src_cnt
> 1) /* is xor ? */
325 dir
= DMA_BIDIRECTIONAL
;
327 dir
= DMA_FROM_DEVICE
;
328 dma_unmap_page(dev
, dest
, len
, dir
);
331 if (!(flags
& DMA_COMPL_SKIP_SRC_UNMAP
)) {
333 addr
= mv_desc_get_src_addr(unmap
,
337 dma_unmap_page(dev
, addr
, len
,
341 desc
->group_head
= NULL
;
345 /* run dependent operations */
346 dma_run_dependencies(&desc
->async_tx
);
352 mv_xor_clean_completed_slots(struct mv_xor_chan
*mv_chan
)
354 struct mv_xor_desc_slot
*iter
, *_iter
;
356 dev_dbg(mv_chan
->device
->common
.dev
, "%s %d\n", __func__
, __LINE__
);
357 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
360 if (async_tx_test_ack(&iter
->async_tx
)) {
361 list_del(&iter
->completed_node
);
362 mv_xor_free_slots(mv_chan
, iter
);
369 mv_xor_clean_slot(struct mv_xor_desc_slot
*desc
,
370 struct mv_xor_chan
*mv_chan
)
372 dev_dbg(mv_chan
->device
->common
.dev
, "%s %d: desc %p flags %d\n",
373 __func__
, __LINE__
, desc
, desc
->async_tx
.flags
);
374 list_del(&desc
->chain_node
);
375 /* the client is allowed to attach dependent operations
378 if (!async_tx_test_ack(&desc
->async_tx
)) {
379 /* move this slot to the completed_slots */
380 list_add_tail(&desc
->completed_node
, &mv_chan
->completed_slots
);
384 mv_xor_free_slots(mv_chan
, desc
);
388 static void __mv_xor_slot_cleanup(struct mv_xor_chan
*mv_chan
)
390 struct mv_xor_desc_slot
*iter
, *_iter
;
391 dma_cookie_t cookie
= 0;
392 int busy
= mv_chan_is_busy(mv_chan
);
393 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
394 int seen_current
= 0;
396 dev_dbg(mv_chan
->device
->common
.dev
, "%s %d\n", __func__
, __LINE__
);
397 dev_dbg(mv_chan
->device
->common
.dev
, "current_desc %x\n", current_desc
);
398 mv_xor_clean_completed_slots(mv_chan
);
400 /* free completed slots from the chain starting with
401 * the oldest descriptor
404 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
407 prefetch(&_iter
->async_tx
);
409 /* do not advance past the current descriptor loaded into the
410 * hardware channel, subsequent descriptors are either in
411 * process or have not been submitted
416 /* stop the search if we reach the current descriptor and the
419 if (iter
->async_tx
.phys
== current_desc
) {
425 cookie
= mv_xor_run_tx_complete_actions(iter
, mv_chan
, cookie
);
427 if (mv_xor_clean_slot(iter
, mv_chan
))
431 if ((busy
== 0) && !list_empty(&mv_chan
->chain
)) {
432 struct mv_xor_desc_slot
*chain_head
;
433 chain_head
= list_entry(mv_chan
->chain
.next
,
434 struct mv_xor_desc_slot
,
437 mv_xor_start_new_chain(mv_chan
, chain_head
);
441 mv_chan
->common
.completed_cookie
= cookie
;
445 mv_xor_slot_cleanup(struct mv_xor_chan
*mv_chan
)
447 spin_lock_bh(&mv_chan
->lock
);
448 __mv_xor_slot_cleanup(mv_chan
);
449 spin_unlock_bh(&mv_chan
->lock
);
452 static void mv_xor_tasklet(unsigned long data
)
454 struct mv_xor_chan
*chan
= (struct mv_xor_chan
*) data
;
455 mv_xor_slot_cleanup(chan
);
458 static struct mv_xor_desc_slot
*
459 mv_xor_alloc_slots(struct mv_xor_chan
*mv_chan
, int num_slots
,
462 struct mv_xor_desc_slot
*iter
, *_iter
, *alloc_start
= NULL
;
464 int slots_found
, retry
= 0;
466 /* start search from the last allocated descrtiptor
467 * if a contiguous allocation can not be found start searching
468 * from the beginning of the list
473 iter
= mv_chan
->last_used
;
475 iter
= list_entry(&mv_chan
->all_slots
,
476 struct mv_xor_desc_slot
,
479 list_for_each_entry_safe_continue(
480 iter
, _iter
, &mv_chan
->all_slots
, slot_node
) {
482 prefetch(&_iter
->async_tx
);
483 if (iter
->slots_per_op
) {
484 /* give up after finding the first busy slot
485 * on the second pass through the list
494 /* start the allocation if the slot is correctly aligned */
498 if (slots_found
== num_slots
) {
499 struct mv_xor_desc_slot
*alloc_tail
= NULL
;
500 struct mv_xor_desc_slot
*last_used
= NULL
;
505 /* pre-ack all but the last descriptor */
506 async_tx_ack(&iter
->async_tx
);
508 list_add_tail(&iter
->chain_node
, &chain
);
510 iter
->async_tx
.cookie
= 0;
511 iter
->slot_cnt
= num_slots
;
512 iter
->xor_check_result
= NULL
;
513 for (i
= 0; i
< slots_per_op
; i
++) {
514 iter
->slots_per_op
= slots_per_op
- i
;
516 iter
= list_entry(iter
->slot_node
.next
,
517 struct mv_xor_desc_slot
,
520 num_slots
-= slots_per_op
;
522 alloc_tail
->group_head
= alloc_start
;
523 alloc_tail
->async_tx
.cookie
= -EBUSY
;
524 list_splice(&chain
, &alloc_tail
->tx_list
);
525 mv_chan
->last_used
= last_used
;
526 mv_desc_clear_next_desc(alloc_start
);
527 mv_desc_clear_next_desc(alloc_tail
);
534 /* try to free some slots if the allocation fails */
535 tasklet_schedule(&mv_chan
->irq_tasklet
);
540 /************************ DMA engine API functions ****************************/
542 mv_xor_tx_submit(struct dma_async_tx_descriptor
*tx
)
544 struct mv_xor_desc_slot
*sw_desc
= to_mv_xor_slot(tx
);
545 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(tx
->chan
);
546 struct mv_xor_desc_slot
*grp_start
, *old_chain_tail
;
548 int new_hw_chain
= 1;
550 dev_dbg(mv_chan
->device
->common
.dev
,
551 "%s sw_desc %p: async_tx %p\n",
552 __func__
, sw_desc
, &sw_desc
->async_tx
);
554 grp_start
= sw_desc
->group_head
;
556 spin_lock_bh(&mv_chan
->lock
);
557 cookie
= dma_cookie_assign(tx
);
559 if (list_empty(&mv_chan
->chain
))
560 list_splice_init(&sw_desc
->tx_list
, &mv_chan
->chain
);
564 old_chain_tail
= list_entry(mv_chan
->chain
.prev
,
565 struct mv_xor_desc_slot
,
567 list_splice_init(&grp_start
->tx_list
,
568 &old_chain_tail
->chain_node
);
570 if (!mv_can_chain(grp_start
))
573 dev_dbg(mv_chan
->device
->common
.dev
, "Append to last desc %x\n",
574 old_chain_tail
->async_tx
.phys
);
576 /* fix up the hardware chain */
577 mv_desc_set_next_desc(old_chain_tail
, grp_start
->async_tx
.phys
);
579 /* if the channel is not busy */
580 if (!mv_chan_is_busy(mv_chan
)) {
581 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
583 * and the curren desc is the end of the chain before
584 * the append, then we need to start the channel
586 if (current_desc
== old_chain_tail
->async_tx
.phys
)
592 mv_xor_start_new_chain(mv_chan
, grp_start
);
595 spin_unlock_bh(&mv_chan
->lock
);
600 /* returns the number of allocated descriptors */
601 static int mv_xor_alloc_chan_resources(struct dma_chan
*chan
)
605 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
606 struct mv_xor_desc_slot
*slot
= NULL
;
607 struct mv_xor_platform_data
*plat_data
=
608 mv_chan
->device
->pdev
->dev
.platform_data
;
609 int num_descs_in_pool
= plat_data
->pool_size
/MV_XOR_SLOT_SIZE
;
611 /* Allocate descriptor slots */
612 idx
= mv_chan
->slots_allocated
;
613 while (idx
< num_descs_in_pool
) {
614 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
616 printk(KERN_INFO
"MV XOR Channel only initialized"
617 " %d descriptor slots", idx
);
620 hw_desc
= (char *) mv_chan
->device
->dma_desc_pool_virt
;
621 slot
->hw_desc
= (void *) &hw_desc
[idx
* MV_XOR_SLOT_SIZE
];
623 dma_async_tx_descriptor_init(&slot
->async_tx
, chan
);
624 slot
->async_tx
.tx_submit
= mv_xor_tx_submit
;
625 INIT_LIST_HEAD(&slot
->chain_node
);
626 INIT_LIST_HEAD(&slot
->slot_node
);
627 INIT_LIST_HEAD(&slot
->tx_list
);
628 hw_desc
= (char *) mv_chan
->device
->dma_desc_pool
;
629 slot
->async_tx
.phys
=
630 (dma_addr_t
) &hw_desc
[idx
* MV_XOR_SLOT_SIZE
];
633 spin_lock_bh(&mv_chan
->lock
);
634 mv_chan
->slots_allocated
= idx
;
635 list_add_tail(&slot
->slot_node
, &mv_chan
->all_slots
);
636 spin_unlock_bh(&mv_chan
->lock
);
639 if (mv_chan
->slots_allocated
&& !mv_chan
->last_used
)
640 mv_chan
->last_used
= list_entry(mv_chan
->all_slots
.next
,
641 struct mv_xor_desc_slot
,
644 dev_dbg(mv_chan
->device
->common
.dev
,
645 "allocated %d descriptor slots last_used: %p\n",
646 mv_chan
->slots_allocated
, mv_chan
->last_used
);
648 return mv_chan
->slots_allocated
? : -ENOMEM
;
651 static struct dma_async_tx_descriptor
*
652 mv_xor_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
653 size_t len
, unsigned long flags
)
655 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
656 struct mv_xor_desc_slot
*sw_desc
, *grp_start
;
659 dev_dbg(mv_chan
->device
->common
.dev
,
660 "%s dest: %x src %x len: %u flags: %ld\n",
661 __func__
, dest
, src
, len
, flags
);
662 if (unlikely(len
< MV_XOR_MIN_BYTE_COUNT
))
665 BUG_ON(len
> MV_XOR_MAX_BYTE_COUNT
);
667 spin_lock_bh(&mv_chan
->lock
);
668 slot_cnt
= mv_chan_memcpy_slot_count(len
);
669 sw_desc
= mv_xor_alloc_slots(mv_chan
, slot_cnt
, 1);
671 sw_desc
->type
= DMA_MEMCPY
;
672 sw_desc
->async_tx
.flags
= flags
;
673 grp_start
= sw_desc
->group_head
;
674 mv_desc_init(grp_start
, flags
);
675 mv_desc_set_byte_count(grp_start
, len
);
676 mv_desc_set_dest_addr(sw_desc
->group_head
, dest
);
677 mv_desc_set_src_addr(grp_start
, 0, src
);
678 sw_desc
->unmap_src_cnt
= 1;
679 sw_desc
->unmap_len
= len
;
681 spin_unlock_bh(&mv_chan
->lock
);
683 dev_dbg(mv_chan
->device
->common
.dev
,
684 "%s sw_desc %p async_tx %p\n",
685 __func__
, sw_desc
, sw_desc
? &sw_desc
->async_tx
: 0);
687 return sw_desc
? &sw_desc
->async_tx
: NULL
;
690 static struct dma_async_tx_descriptor
*
691 mv_xor_prep_dma_memset(struct dma_chan
*chan
, dma_addr_t dest
, int value
,
692 size_t len
, unsigned long flags
)
694 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
695 struct mv_xor_desc_slot
*sw_desc
, *grp_start
;
698 dev_dbg(mv_chan
->device
->common
.dev
,
699 "%s dest: %x len: %u flags: %ld\n",
700 __func__
, dest
, len
, flags
);
701 if (unlikely(len
< MV_XOR_MIN_BYTE_COUNT
))
704 BUG_ON(len
> MV_XOR_MAX_BYTE_COUNT
);
706 spin_lock_bh(&mv_chan
->lock
);
707 slot_cnt
= mv_chan_memset_slot_count(len
);
708 sw_desc
= mv_xor_alloc_slots(mv_chan
, slot_cnt
, 1);
710 sw_desc
->type
= DMA_MEMSET
;
711 sw_desc
->async_tx
.flags
= flags
;
712 grp_start
= sw_desc
->group_head
;
713 mv_desc_init(grp_start
, flags
);
714 mv_desc_set_byte_count(grp_start
, len
);
715 mv_desc_set_dest_addr(sw_desc
->group_head
, dest
);
716 mv_desc_set_block_fill_val(grp_start
, value
);
717 sw_desc
->unmap_src_cnt
= 1;
718 sw_desc
->unmap_len
= len
;
720 spin_unlock_bh(&mv_chan
->lock
);
721 dev_dbg(mv_chan
->device
->common
.dev
,
722 "%s sw_desc %p async_tx %p \n",
723 __func__
, sw_desc
, &sw_desc
->async_tx
);
724 return sw_desc
? &sw_desc
->async_tx
: NULL
;
727 static struct dma_async_tx_descriptor
*
728 mv_xor_prep_dma_xor(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t
*src
,
729 unsigned int src_cnt
, size_t len
, unsigned long flags
)
731 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
732 struct mv_xor_desc_slot
*sw_desc
, *grp_start
;
735 if (unlikely(len
< MV_XOR_MIN_BYTE_COUNT
))
738 BUG_ON(len
> MV_XOR_MAX_BYTE_COUNT
);
740 dev_dbg(mv_chan
->device
->common
.dev
,
741 "%s src_cnt: %d len: dest %x %u flags: %ld\n",
742 __func__
, src_cnt
, len
, dest
, flags
);
744 spin_lock_bh(&mv_chan
->lock
);
745 slot_cnt
= mv_chan_xor_slot_count(len
, src_cnt
);
746 sw_desc
= mv_xor_alloc_slots(mv_chan
, slot_cnt
, 1);
748 sw_desc
->type
= DMA_XOR
;
749 sw_desc
->async_tx
.flags
= flags
;
750 grp_start
= sw_desc
->group_head
;
751 mv_desc_init(grp_start
, flags
);
752 /* the byte count field is the same as in memcpy desc*/
753 mv_desc_set_byte_count(grp_start
, len
);
754 mv_desc_set_dest_addr(sw_desc
->group_head
, dest
);
755 sw_desc
->unmap_src_cnt
= src_cnt
;
756 sw_desc
->unmap_len
= len
;
758 mv_desc_set_src_addr(grp_start
, src_cnt
, src
[src_cnt
]);
760 spin_unlock_bh(&mv_chan
->lock
);
761 dev_dbg(mv_chan
->device
->common
.dev
,
762 "%s sw_desc %p async_tx %p \n",
763 __func__
, sw_desc
, &sw_desc
->async_tx
);
764 return sw_desc
? &sw_desc
->async_tx
: NULL
;
767 static void mv_xor_free_chan_resources(struct dma_chan
*chan
)
769 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
770 struct mv_xor_desc_slot
*iter
, *_iter
;
771 int in_use_descs
= 0;
773 mv_xor_slot_cleanup(mv_chan
);
775 spin_lock_bh(&mv_chan
->lock
);
776 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
779 list_del(&iter
->chain_node
);
781 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
784 list_del(&iter
->completed_node
);
786 list_for_each_entry_safe_reverse(
787 iter
, _iter
, &mv_chan
->all_slots
, slot_node
) {
788 list_del(&iter
->slot_node
);
790 mv_chan
->slots_allocated
--;
792 mv_chan
->last_used
= NULL
;
794 dev_dbg(mv_chan
->device
->common
.dev
, "%s slots_allocated %d\n",
795 __func__
, mv_chan
->slots_allocated
);
796 spin_unlock_bh(&mv_chan
->lock
);
799 dev_err(mv_chan
->device
->common
.dev
,
800 "freeing %d in use descriptors!\n", in_use_descs
);
804 * mv_xor_status - poll the status of an XOR transaction
805 * @chan: XOR channel handle
806 * @cookie: XOR transaction identifier
807 * @txstate: XOR transactions state holder (or NULL)
809 static enum dma_status
mv_xor_status(struct dma_chan
*chan
,
811 struct dma_tx_state
*txstate
)
813 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
816 ret
= dma_cookie_status(chan
, cookie
, txstate
);
817 if (ret
== DMA_SUCCESS
) {
818 mv_xor_clean_completed_slots(mv_chan
);
821 mv_xor_slot_cleanup(mv_chan
);
823 return dma_cookie_status(chan
, cookie
, txstate
);
826 static void mv_dump_xor_regs(struct mv_xor_chan
*chan
)
830 val
= __raw_readl(XOR_CONFIG(chan
));
831 dev_printk(KERN_ERR
, chan
->device
->common
.dev
,
832 "config 0x%08x.\n", val
);
834 val
= __raw_readl(XOR_ACTIVATION(chan
));
835 dev_printk(KERN_ERR
, chan
->device
->common
.dev
,
836 "activation 0x%08x.\n", val
);
838 val
= __raw_readl(XOR_INTR_CAUSE(chan
));
839 dev_printk(KERN_ERR
, chan
->device
->common
.dev
,
840 "intr cause 0x%08x.\n", val
);
842 val
= __raw_readl(XOR_INTR_MASK(chan
));
843 dev_printk(KERN_ERR
, chan
->device
->common
.dev
,
844 "intr mask 0x%08x.\n", val
);
846 val
= __raw_readl(XOR_ERROR_CAUSE(chan
));
847 dev_printk(KERN_ERR
, chan
->device
->common
.dev
,
848 "error cause 0x%08x.\n", val
);
850 val
= __raw_readl(XOR_ERROR_ADDR(chan
));
851 dev_printk(KERN_ERR
, chan
->device
->common
.dev
,
852 "error addr 0x%08x.\n", val
);
855 static void mv_xor_err_interrupt_handler(struct mv_xor_chan
*chan
,
858 if (intr_cause
& (1 << 4)) {
859 dev_dbg(chan
->device
->common
.dev
,
860 "ignore this error\n");
864 dev_printk(KERN_ERR
, chan
->device
->common
.dev
,
865 "error on chan %d. intr cause 0x%08x.\n",
866 chan
->idx
, intr_cause
);
868 mv_dump_xor_regs(chan
);
872 static irqreturn_t
mv_xor_interrupt_handler(int irq
, void *data
)
874 struct mv_xor_chan
*chan
= data
;
875 u32 intr_cause
= mv_chan_get_intr_cause(chan
);
877 dev_dbg(chan
->device
->common
.dev
, "intr cause %x\n", intr_cause
);
879 if (mv_is_err_intr(intr_cause
))
880 mv_xor_err_interrupt_handler(chan
, intr_cause
);
882 tasklet_schedule(&chan
->irq_tasklet
);
884 mv_xor_device_clear_eoc_cause(chan
);
889 static void mv_xor_issue_pending(struct dma_chan
*chan
)
891 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
893 if (mv_chan
->pending
>= MV_XOR_THRESHOLD
) {
894 mv_chan
->pending
= 0;
895 mv_chan_activate(mv_chan
);
900 * Perform a transaction to verify the HW works.
902 #define MV_XOR_TEST_SIZE 2000
904 static int __devinit
mv_xor_memcpy_self_test(struct mv_xor_device
*device
)
908 dma_addr_t src_dma
, dest_dma
;
909 struct dma_chan
*dma_chan
;
911 struct dma_async_tx_descriptor
*tx
;
913 struct mv_xor_chan
*mv_chan
;
915 src
= kmalloc(sizeof(u8
) * MV_XOR_TEST_SIZE
, GFP_KERNEL
);
919 dest
= kzalloc(sizeof(u8
) * MV_XOR_TEST_SIZE
, GFP_KERNEL
);
925 /* Fill in src buffer */
926 for (i
= 0; i
< MV_XOR_TEST_SIZE
; i
++)
927 ((u8
*) src
)[i
] = (u8
)i
;
929 /* Start copy, using first DMA channel */
930 dma_chan
= container_of(device
->common
.channels
.next
,
933 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
938 dest_dma
= dma_map_single(dma_chan
->device
->dev
, dest
,
939 MV_XOR_TEST_SIZE
, DMA_FROM_DEVICE
);
941 src_dma
= dma_map_single(dma_chan
->device
->dev
, src
,
942 MV_XOR_TEST_SIZE
, DMA_TO_DEVICE
);
944 tx
= mv_xor_prep_dma_memcpy(dma_chan
, dest_dma
, src_dma
,
945 MV_XOR_TEST_SIZE
, 0);
946 cookie
= mv_xor_tx_submit(tx
);
947 mv_xor_issue_pending(dma_chan
);
951 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
953 dev_printk(KERN_ERR
, dma_chan
->device
->dev
,
954 "Self-test copy timed out, disabling\n");
959 mv_chan
= to_mv_xor_chan(dma_chan
);
960 dma_sync_single_for_cpu(&mv_chan
->device
->pdev
->dev
, dest_dma
,
961 MV_XOR_TEST_SIZE
, DMA_FROM_DEVICE
);
962 if (memcmp(src
, dest
, MV_XOR_TEST_SIZE
)) {
963 dev_printk(KERN_ERR
, dma_chan
->device
->dev
,
964 "Self-test copy failed compare, disabling\n");
970 mv_xor_free_chan_resources(dma_chan
);
977 #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
979 mv_xor_xor_self_test(struct mv_xor_device
*device
)
983 struct page
*xor_srcs
[MV_XOR_NUM_SRC_TEST
];
984 dma_addr_t dma_srcs
[MV_XOR_NUM_SRC_TEST
];
986 struct dma_async_tx_descriptor
*tx
;
987 struct dma_chan
*dma_chan
;
992 struct mv_xor_chan
*mv_chan
;
994 for (src_idx
= 0; src_idx
< MV_XOR_NUM_SRC_TEST
; src_idx
++) {
995 xor_srcs
[src_idx
] = alloc_page(GFP_KERNEL
);
996 if (!xor_srcs
[src_idx
]) {
998 __free_page(xor_srcs
[src_idx
]);
1003 dest
= alloc_page(GFP_KERNEL
);
1006 __free_page(xor_srcs
[src_idx
]);
1010 /* Fill in src buffers */
1011 for (src_idx
= 0; src_idx
< MV_XOR_NUM_SRC_TEST
; src_idx
++) {
1012 u8
*ptr
= page_address(xor_srcs
[src_idx
]);
1013 for (i
= 0; i
< PAGE_SIZE
; i
++)
1014 ptr
[i
] = (1 << src_idx
);
1017 for (src_idx
= 0; src_idx
< MV_XOR_NUM_SRC_TEST
; src_idx
++)
1018 cmp_byte
^= (u8
) (1 << src_idx
);
1020 cmp_word
= (cmp_byte
<< 24) | (cmp_byte
<< 16) |
1021 (cmp_byte
<< 8) | cmp_byte
;
1023 memset(page_address(dest
), 0, PAGE_SIZE
);
1025 dma_chan
= container_of(device
->common
.channels
.next
,
1028 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
1034 dest_dma
= dma_map_page(dma_chan
->device
->dev
, dest
, 0, PAGE_SIZE
,
1037 for (i
= 0; i
< MV_XOR_NUM_SRC_TEST
; i
++)
1038 dma_srcs
[i
] = dma_map_page(dma_chan
->device
->dev
, xor_srcs
[i
],
1039 0, PAGE_SIZE
, DMA_TO_DEVICE
);
1041 tx
= mv_xor_prep_dma_xor(dma_chan
, dest_dma
, dma_srcs
,
1042 MV_XOR_NUM_SRC_TEST
, PAGE_SIZE
, 0);
1044 cookie
= mv_xor_tx_submit(tx
);
1045 mv_xor_issue_pending(dma_chan
);
1049 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
1051 dev_printk(KERN_ERR
, dma_chan
->device
->dev
,
1052 "Self-test xor timed out, disabling\n");
1054 goto free_resources
;
1057 mv_chan
= to_mv_xor_chan(dma_chan
);
1058 dma_sync_single_for_cpu(&mv_chan
->device
->pdev
->dev
, dest_dma
,
1059 PAGE_SIZE
, DMA_FROM_DEVICE
);
1060 for (i
= 0; i
< (PAGE_SIZE
/ sizeof(u32
)); i
++) {
1061 u32
*ptr
= page_address(dest
);
1062 if (ptr
[i
] != cmp_word
) {
1063 dev_printk(KERN_ERR
, dma_chan
->device
->dev
,
1064 "Self-test xor failed compare, disabling."
1065 " index %d, data %x, expected %x\n", i
,
1068 goto free_resources
;
1073 mv_xor_free_chan_resources(dma_chan
);
1075 src_idx
= MV_XOR_NUM_SRC_TEST
;
1077 __free_page(xor_srcs
[src_idx
]);
1082 static int __devexit
mv_xor_remove(struct platform_device
*dev
)
1084 struct mv_xor_device
*device
= platform_get_drvdata(dev
);
1085 struct dma_chan
*chan
, *_chan
;
1086 struct mv_xor_chan
*mv_chan
;
1087 struct mv_xor_platform_data
*plat_data
= dev
->dev
.platform_data
;
1089 dma_async_device_unregister(&device
->common
);
1091 dma_free_coherent(&dev
->dev
, plat_data
->pool_size
,
1092 device
->dma_desc_pool_virt
, device
->dma_desc_pool
);
1094 list_for_each_entry_safe(chan
, _chan
, &device
->common
.channels
,
1096 mv_chan
= to_mv_xor_chan(chan
);
1097 list_del(&chan
->device_node
);
1103 static int __devinit
mv_xor_probe(struct platform_device
*pdev
)
1107 struct mv_xor_device
*adev
;
1108 struct mv_xor_chan
*mv_chan
;
1109 struct dma_device
*dma_dev
;
1110 struct mv_xor_platform_data
*plat_data
= pdev
->dev
.platform_data
;
1113 adev
= devm_kzalloc(&pdev
->dev
, sizeof(*adev
), GFP_KERNEL
);
1117 dma_dev
= &adev
->common
;
1119 /* allocate coherent memory for hardware descriptors
1120 * note: writecombine gives slightly better performance, but
1121 * requires that we explicitly flush the writes
1123 adev
->dma_desc_pool_virt
= dma_alloc_writecombine(&pdev
->dev
,
1124 plat_data
->pool_size
,
1125 &adev
->dma_desc_pool
,
1127 if (!adev
->dma_desc_pool_virt
)
1130 adev
->id
= plat_data
->hw_id
;
1132 /* discover transaction capabilites from the platform data */
1133 dma_dev
->cap_mask
= plat_data
->cap_mask
;
1135 platform_set_drvdata(pdev
, adev
);
1137 adev
->shared
= platform_get_drvdata(plat_data
->shared
);
1139 INIT_LIST_HEAD(&dma_dev
->channels
);
1141 /* set base routines */
1142 dma_dev
->device_alloc_chan_resources
= mv_xor_alloc_chan_resources
;
1143 dma_dev
->device_free_chan_resources
= mv_xor_free_chan_resources
;
1144 dma_dev
->device_tx_status
= mv_xor_status
;
1145 dma_dev
->device_issue_pending
= mv_xor_issue_pending
;
1146 dma_dev
->dev
= &pdev
->dev
;
1148 /* set prep routines based on capability */
1149 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
))
1150 dma_dev
->device_prep_dma_memcpy
= mv_xor_prep_dma_memcpy
;
1151 if (dma_has_cap(DMA_MEMSET
, dma_dev
->cap_mask
))
1152 dma_dev
->device_prep_dma_memset
= mv_xor_prep_dma_memset
;
1153 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1154 dma_dev
->max_xor
= 8;
1155 dma_dev
->device_prep_dma_xor
= mv_xor_prep_dma_xor
;
1158 mv_chan
= devm_kzalloc(&pdev
->dev
, sizeof(*mv_chan
), GFP_KERNEL
);
1163 mv_chan
->device
= adev
;
1164 mv_chan
->idx
= plat_data
->hw_id
;
1165 mv_chan
->mmr_base
= adev
->shared
->xor_base
;
1167 if (!mv_chan
->mmr_base
) {
1171 tasklet_init(&mv_chan
->irq_tasklet
, mv_xor_tasklet
, (unsigned long)
1174 /* clear errors before enabling interrupts */
1175 mv_xor_device_clear_err_status(mv_chan
);
1177 irq
= platform_get_irq(pdev
, 0);
1182 ret
= devm_request_irq(&pdev
->dev
, irq
,
1183 mv_xor_interrupt_handler
,
1184 0, dev_name(&pdev
->dev
), mv_chan
);
1188 mv_chan_unmask_interrupts(mv_chan
);
1190 mv_set_mode(mv_chan
, DMA_MEMCPY
);
1192 spin_lock_init(&mv_chan
->lock
);
1193 INIT_LIST_HEAD(&mv_chan
->chain
);
1194 INIT_LIST_HEAD(&mv_chan
->completed_slots
);
1195 INIT_LIST_HEAD(&mv_chan
->all_slots
);
1196 mv_chan
->common
.device
= dma_dev
;
1197 dma_cookie_init(&mv_chan
->common
);
1199 list_add_tail(&mv_chan
->common
.device_node
, &dma_dev
->channels
);
1201 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
)) {
1202 ret
= mv_xor_memcpy_self_test(adev
);
1203 dev_dbg(&pdev
->dev
, "memcpy self test returned %d\n", ret
);
1208 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1209 ret
= mv_xor_xor_self_test(adev
);
1210 dev_dbg(&pdev
->dev
, "xor self test returned %d\n", ret
);
1215 dev_printk(KERN_INFO
, &pdev
->dev
, "Marvell XOR: "
1217 dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
) ? "xor " : "",
1218 dma_has_cap(DMA_MEMSET
, dma_dev
->cap_mask
) ? "fill " : "",
1219 dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
) ? "cpy " : "",
1220 dma_has_cap(DMA_INTERRUPT
, dma_dev
->cap_mask
) ? "intr " : "");
1222 dma_async_device_register(dma_dev
);
1226 dma_free_coherent(&adev
->pdev
->dev
, plat_data
->pool_size
,
1227 adev
->dma_desc_pool_virt
, adev
->dma_desc_pool
);
1233 mv_xor_conf_mbus_windows(struct mv_xor_shared_private
*msp
,
1234 const struct mbus_dram_target_info
*dram
)
1236 void __iomem
*base
= msp
->xor_base
;
1240 for (i
= 0; i
< 8; i
++) {
1241 writel(0, base
+ WINDOW_BASE(i
));
1242 writel(0, base
+ WINDOW_SIZE(i
));
1244 writel(0, base
+ WINDOW_REMAP_HIGH(i
));
1247 for (i
= 0; i
< dram
->num_cs
; i
++) {
1248 const struct mbus_dram_window
*cs
= dram
->cs
+ i
;
1250 writel((cs
->base
& 0xffff0000) |
1251 (cs
->mbus_attr
<< 8) |
1252 dram
->mbus_dram_target_id
, base
+ WINDOW_BASE(i
));
1253 writel((cs
->size
- 1) & 0xffff0000, base
+ WINDOW_SIZE(i
));
1255 win_enable
|= (1 << i
);
1256 win_enable
|= 3 << (16 + (2 * i
));
1259 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(0));
1260 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(1));
1263 static struct platform_driver mv_xor_driver
= {
1264 .probe
= mv_xor_probe
,
1265 .remove
= __devexit_p(mv_xor_remove
),
1267 .owner
= THIS_MODULE
,
1268 .name
= MV_XOR_NAME
,
1272 static int mv_xor_shared_probe(struct platform_device
*pdev
)
1274 const struct mbus_dram_target_info
*dram
;
1275 struct mv_xor_shared_private
*msp
;
1276 struct resource
*res
;
1278 dev_printk(KERN_NOTICE
, &pdev
->dev
, "Marvell shared XOR driver\n");
1280 msp
= devm_kzalloc(&pdev
->dev
, sizeof(*msp
), GFP_KERNEL
);
1284 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1288 msp
->xor_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1289 resource_size(res
));
1293 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1297 msp
->xor_high_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1298 resource_size(res
));
1299 if (!msp
->xor_high_base
)
1302 platform_set_drvdata(pdev
, msp
);
1305 * (Re-)program MBUS remapping windows if we are asked to.
1307 dram
= mv_mbus_dram_info();
1309 mv_xor_conf_mbus_windows(msp
, dram
);
1311 /* Not all platforms can gate the clock, so it is not
1312 * an error if the clock does not exists.
1314 msp
->clk
= clk_get(&pdev
->dev
, NULL
);
1315 if (!IS_ERR(msp
->clk
))
1316 clk_prepare_enable(msp
->clk
);
1321 static int mv_xor_shared_remove(struct platform_device
*pdev
)
1323 struct mv_xor_shared_private
*msp
= platform_get_drvdata(pdev
);
1325 if (!IS_ERR(msp
->clk
)) {
1326 clk_disable_unprepare(msp
->clk
);
1333 static struct platform_driver mv_xor_shared_driver
= {
1334 .probe
= mv_xor_shared_probe
,
1335 .remove
= mv_xor_shared_remove
,
1337 .owner
= THIS_MODULE
,
1338 .name
= MV_XOR_SHARED_NAME
,
1343 static int __init
mv_xor_init(void)
1347 rc
= platform_driver_register(&mv_xor_shared_driver
);
1349 rc
= platform_driver_register(&mv_xor_driver
);
1351 platform_driver_unregister(&mv_xor_shared_driver
);
1355 module_init(mv_xor_init
);
1357 /* it's currently unsafe to unload this module */
1359 static void __exit
mv_xor_exit(void)
1361 platform_driver_unregister(&mv_xor_driver
);
1362 platform_driver_unregister(&mv_xor_shared_driver
);
1366 module_exit(mv_xor_exit
);
1369 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1370 MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1371 MODULE_LICENSE("GPL");