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>
30 #include <linux/of_irq.h>
31 #include <linux/irqdomain.h>
32 #include <linux/platform_data/dma-mv_xor.h>
34 #include "dmaengine.h"
37 static void mv_xor_issue_pending(struct dma_chan
*chan
);
39 #define to_mv_xor_chan(chan) \
40 container_of(chan, struct mv_xor_chan, dmachan)
42 #define to_mv_xor_slot(tx) \
43 container_of(tx, struct mv_xor_desc_slot, async_tx)
45 #define mv_chan_to_devp(chan) \
48 static void mv_desc_init(struct mv_xor_desc_slot
*desc
, unsigned long flags
)
50 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
52 hw_desc
->status
= (1 << 31);
53 hw_desc
->phy_next_desc
= 0;
54 hw_desc
->desc_command
= (1 << 31);
57 static u32
mv_desc_get_dest_addr(struct mv_xor_desc_slot
*desc
)
59 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
60 return hw_desc
->phy_dest_addr
;
63 static u32
mv_desc_get_src_addr(struct mv_xor_desc_slot
*desc
,
66 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
67 return hw_desc
->phy_src_addr
[mv_phy_src_idx(src_idx
)];
71 static void mv_desc_set_byte_count(struct mv_xor_desc_slot
*desc
,
74 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
75 hw_desc
->byte_count
= byte_count
;
78 static void mv_desc_set_next_desc(struct mv_xor_desc_slot
*desc
,
81 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
82 BUG_ON(hw_desc
->phy_next_desc
);
83 hw_desc
->phy_next_desc
= next_desc_addr
;
86 static void mv_desc_clear_next_desc(struct mv_xor_desc_slot
*desc
)
88 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
89 hw_desc
->phy_next_desc
= 0;
92 static void mv_desc_set_dest_addr(struct mv_xor_desc_slot
*desc
,
95 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
96 hw_desc
->phy_dest_addr
= addr
;
99 static int mv_chan_memset_slot_count(size_t len
)
104 #define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c)
106 static void mv_desc_set_src_addr(struct mv_xor_desc_slot
*desc
,
107 int index
, dma_addr_t addr
)
109 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
110 hw_desc
->phy_src_addr
[mv_phy_src_idx(index
)] = addr
;
111 if (desc
->type
== DMA_XOR
)
112 hw_desc
->desc_command
|= (1 << index
);
115 static u32
mv_chan_get_current_desc(struct mv_xor_chan
*chan
)
117 return readl_relaxed(XOR_CURR_DESC(chan
));
120 static void mv_chan_set_next_descriptor(struct mv_xor_chan
*chan
,
123 writel_relaxed(next_desc_addr
, XOR_NEXT_DESC(chan
));
126 static void mv_chan_unmask_interrupts(struct mv_xor_chan
*chan
)
128 u32 val
= readl_relaxed(XOR_INTR_MASK(chan
));
129 val
|= XOR_INTR_MASK_VALUE
<< (chan
->idx
* 16);
130 writel_relaxed(val
, XOR_INTR_MASK(chan
));
133 static u32
mv_chan_get_intr_cause(struct mv_xor_chan
*chan
)
135 u32 intr_cause
= readl_relaxed(XOR_INTR_CAUSE(chan
));
136 intr_cause
= (intr_cause
>> (chan
->idx
* 16)) & 0xFFFF;
140 static int mv_is_err_intr(u32 intr_cause
)
142 if (intr_cause
& ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9)))
148 static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan
*chan
)
150 u32 val
= ~(1 << (chan
->idx
* 16));
151 dev_dbg(mv_chan_to_devp(chan
), "%s, val 0x%08x\n", __func__
, val
);
152 writel_relaxed(val
, XOR_INTR_CAUSE(chan
));
155 static void mv_xor_device_clear_err_status(struct mv_xor_chan
*chan
)
157 u32 val
= 0xFFFF0000 >> (chan
->idx
* 16);
158 writel_relaxed(val
, XOR_INTR_CAUSE(chan
));
161 static int mv_can_chain(struct mv_xor_desc_slot
*desc
)
163 struct mv_xor_desc_slot
*chain_old_tail
= list_entry(
164 desc
->chain_node
.prev
, struct mv_xor_desc_slot
, chain_node
);
166 if (chain_old_tail
->type
!= desc
->type
)
172 static void mv_set_mode(struct mv_xor_chan
*chan
,
173 enum dma_transaction_type type
)
176 u32 config
= readl_relaxed(XOR_CONFIG(chan
));
180 op_mode
= XOR_OPERATION_MODE_XOR
;
183 op_mode
= XOR_OPERATION_MODE_MEMCPY
;
186 dev_err(mv_chan_to_devp(chan
),
187 "error: unsupported operation %d\n",
196 #if defined(__BIG_ENDIAN)
197 config
|= XOR_DESCRIPTOR_SWAP
;
199 config
&= ~XOR_DESCRIPTOR_SWAP
;
202 writel_relaxed(config
, XOR_CONFIG(chan
));
203 chan
->current_type
= type
;
206 static void mv_chan_activate(struct mv_xor_chan
*chan
)
208 dev_dbg(mv_chan_to_devp(chan
), " activate chan.\n");
210 /* writel ensures all descriptors are flushed before activation */
211 writel(BIT(0), XOR_ACTIVATION(chan
));
214 static char mv_chan_is_busy(struct mv_xor_chan
*chan
)
216 u32 state
= readl_relaxed(XOR_ACTIVATION(chan
));
218 state
= (state
>> 4) & 0x3;
220 return (state
== 1) ? 1 : 0;
223 static int mv_chan_xor_slot_count(size_t len
, int src_cnt
)
229 * mv_xor_free_slots - flags descriptor slots for reuse
230 * @slot: Slot to free
231 * Caller must hold &mv_chan->lock while calling this function
233 static void mv_xor_free_slots(struct mv_xor_chan
*mv_chan
,
234 struct mv_xor_desc_slot
*slot
)
236 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d slot %p\n",
237 __func__
, __LINE__
, slot
);
239 slot
->slots_per_op
= 0;
244 * mv_xor_start_new_chain - program the engine to operate on new chain headed by
246 * Caller must hold &mv_chan->lock while calling this function
248 static void mv_xor_start_new_chain(struct mv_xor_chan
*mv_chan
,
249 struct mv_xor_desc_slot
*sw_desc
)
251 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d: sw_desc %p\n",
252 __func__
, __LINE__
, sw_desc
);
253 if (sw_desc
->type
!= mv_chan
->current_type
)
254 mv_set_mode(mv_chan
, sw_desc
->type
);
256 /* set the hardware chain */
257 mv_chan_set_next_descriptor(mv_chan
, sw_desc
->async_tx
.phys
);
259 mv_chan
->pending
+= sw_desc
->slot_cnt
;
260 mv_xor_issue_pending(&mv_chan
->dmachan
);
264 mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot
*desc
,
265 struct mv_xor_chan
*mv_chan
, dma_cookie_t cookie
)
267 BUG_ON(desc
->async_tx
.cookie
< 0);
269 if (desc
->async_tx
.cookie
> 0) {
270 cookie
= desc
->async_tx
.cookie
;
272 /* call the callback (must not sleep or submit new
273 * operations to this channel)
275 if (desc
->async_tx
.callback
)
276 desc
->async_tx
.callback(
277 desc
->async_tx
.callback_param
);
279 /* unmap dma addresses
280 * (unmap_single vs unmap_page?)
282 if (desc
->group_head
&& desc
->unmap_len
) {
283 struct mv_xor_desc_slot
*unmap
= desc
->group_head
;
284 struct device
*dev
= mv_chan_to_devp(mv_chan
);
285 u32 len
= unmap
->unmap_len
;
286 enum dma_ctrl_flags flags
= desc
->async_tx
.flags
;
291 src_cnt
= unmap
->unmap_src_cnt
;
292 dest
= mv_desc_get_dest_addr(unmap
);
293 if (!(flags
& DMA_COMPL_SKIP_DEST_UNMAP
)) {
294 enum dma_data_direction dir
;
296 if (src_cnt
> 1) /* is xor ? */
297 dir
= DMA_BIDIRECTIONAL
;
299 dir
= DMA_FROM_DEVICE
;
300 dma_unmap_page(dev
, dest
, len
, dir
);
303 if (!(flags
& DMA_COMPL_SKIP_SRC_UNMAP
)) {
305 addr
= mv_desc_get_src_addr(unmap
,
309 dma_unmap_page(dev
, addr
, len
,
313 desc
->group_head
= NULL
;
317 /* run dependent operations */
318 dma_run_dependencies(&desc
->async_tx
);
324 mv_xor_clean_completed_slots(struct mv_xor_chan
*mv_chan
)
326 struct mv_xor_desc_slot
*iter
, *_iter
;
328 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d\n", __func__
, __LINE__
);
329 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
332 if (async_tx_test_ack(&iter
->async_tx
)) {
333 list_del(&iter
->completed_node
);
334 mv_xor_free_slots(mv_chan
, iter
);
341 mv_xor_clean_slot(struct mv_xor_desc_slot
*desc
,
342 struct mv_xor_chan
*mv_chan
)
344 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d: desc %p flags %d\n",
345 __func__
, __LINE__
, desc
, desc
->async_tx
.flags
);
346 list_del(&desc
->chain_node
);
347 /* the client is allowed to attach dependent operations
350 if (!async_tx_test_ack(&desc
->async_tx
)) {
351 /* move this slot to the completed_slots */
352 list_add_tail(&desc
->completed_node
, &mv_chan
->completed_slots
);
356 mv_xor_free_slots(mv_chan
, desc
);
360 static void __mv_xor_slot_cleanup(struct mv_xor_chan
*mv_chan
)
362 struct mv_xor_desc_slot
*iter
, *_iter
;
363 dma_cookie_t cookie
= 0;
364 int busy
= mv_chan_is_busy(mv_chan
);
365 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
366 int current_cleaned
= 0;
367 struct mv_xor_desc
*hw_desc
;
369 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d\n", __func__
, __LINE__
);
370 dev_dbg(mv_chan_to_devp(mv_chan
), "current_desc %x\n", current_desc
);
371 mv_xor_clean_completed_slots(mv_chan
);
373 /* free completed slots from the chain starting with
374 * the oldest descriptor
377 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
380 /* clean finished descriptors */
381 hw_desc
= iter
->hw_desc
;
382 if (hw_desc
->status
& XOR_DESC_SUCCESS
) {
383 cookie
= mv_xor_run_tx_complete_actions(iter
, mv_chan
,
386 /* done processing desc, clean slot */
387 mv_xor_clean_slot(iter
, mv_chan
);
389 /* break if we did cleaned the current */
390 if (iter
->async_tx
.phys
== current_desc
) {
395 if (iter
->async_tx
.phys
== current_desc
) {
402 if ((busy
== 0) && !list_empty(&mv_chan
->chain
)) {
403 if (current_cleaned
) {
405 * current descriptor cleaned and removed, run
408 iter
= list_entry(mv_chan
->chain
.next
,
409 struct mv_xor_desc_slot
,
411 mv_xor_start_new_chain(mv_chan
, iter
);
413 if (!list_is_last(&iter
->chain_node
, &mv_chan
->chain
)) {
415 * descriptors are still waiting after
416 * current, trigger them
418 iter
= list_entry(iter
->chain_node
.next
,
419 struct mv_xor_desc_slot
,
421 mv_xor_start_new_chain(mv_chan
, iter
);
424 * some descriptors are still waiting
427 tasklet_schedule(&mv_chan
->irq_tasklet
);
433 mv_chan
->dmachan
.completed_cookie
= cookie
;
437 mv_xor_slot_cleanup(struct mv_xor_chan
*mv_chan
)
439 spin_lock_bh(&mv_chan
->lock
);
440 __mv_xor_slot_cleanup(mv_chan
);
441 spin_unlock_bh(&mv_chan
->lock
);
444 static void mv_xor_tasklet(unsigned long data
)
446 struct mv_xor_chan
*chan
= (struct mv_xor_chan
*) data
;
447 mv_xor_slot_cleanup(chan
);
450 static struct mv_xor_desc_slot
*
451 mv_xor_alloc_slots(struct mv_xor_chan
*mv_chan
, int num_slots
,
454 struct mv_xor_desc_slot
*iter
, *_iter
, *alloc_start
= NULL
;
456 int slots_found
, retry
= 0;
458 /* start search from the last allocated descrtiptor
459 * if a contiguous allocation can not be found start searching
460 * from the beginning of the list
465 iter
= mv_chan
->last_used
;
467 iter
= list_entry(&mv_chan
->all_slots
,
468 struct mv_xor_desc_slot
,
471 list_for_each_entry_safe_continue(
472 iter
, _iter
, &mv_chan
->all_slots
, slot_node
) {
474 prefetch(&_iter
->async_tx
);
475 if (iter
->slots_per_op
) {
476 /* give up after finding the first busy slot
477 * on the second pass through the list
486 /* start the allocation if the slot is correctly aligned */
490 if (slots_found
== num_slots
) {
491 struct mv_xor_desc_slot
*alloc_tail
= NULL
;
492 struct mv_xor_desc_slot
*last_used
= NULL
;
497 /* pre-ack all but the last descriptor */
498 async_tx_ack(&iter
->async_tx
);
500 list_add_tail(&iter
->chain_node
, &chain
);
502 iter
->async_tx
.cookie
= 0;
503 iter
->slot_cnt
= num_slots
;
504 iter
->xor_check_result
= NULL
;
505 for (i
= 0; i
< slots_per_op
; i
++) {
506 iter
->slots_per_op
= slots_per_op
- i
;
508 iter
= list_entry(iter
->slot_node
.next
,
509 struct mv_xor_desc_slot
,
512 num_slots
-= slots_per_op
;
514 alloc_tail
->group_head
= alloc_start
;
515 alloc_tail
->async_tx
.cookie
= -EBUSY
;
516 list_splice(&chain
, &alloc_tail
->tx_list
);
517 mv_chan
->last_used
= last_used
;
518 mv_desc_clear_next_desc(alloc_start
);
519 mv_desc_clear_next_desc(alloc_tail
);
526 /* try to free some slots if the allocation fails */
527 tasklet_schedule(&mv_chan
->irq_tasklet
);
532 /************************ DMA engine API functions ****************************/
534 mv_xor_tx_submit(struct dma_async_tx_descriptor
*tx
)
536 struct mv_xor_desc_slot
*sw_desc
= to_mv_xor_slot(tx
);
537 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(tx
->chan
);
538 struct mv_xor_desc_slot
*grp_start
, *old_chain_tail
;
540 int new_hw_chain
= 1;
542 dev_dbg(mv_chan_to_devp(mv_chan
),
543 "%s sw_desc %p: async_tx %p\n",
544 __func__
, sw_desc
, &sw_desc
->async_tx
);
546 grp_start
= sw_desc
->group_head
;
548 spin_lock_bh(&mv_chan
->lock
);
549 cookie
= dma_cookie_assign(tx
);
551 if (list_empty(&mv_chan
->chain
))
552 list_splice_init(&sw_desc
->tx_list
, &mv_chan
->chain
);
556 old_chain_tail
= list_entry(mv_chan
->chain
.prev
,
557 struct mv_xor_desc_slot
,
559 list_splice_init(&grp_start
->tx_list
,
560 &old_chain_tail
->chain_node
);
562 if (!mv_can_chain(grp_start
))
565 dev_dbg(mv_chan_to_devp(mv_chan
), "Append to last desc %x\n",
566 old_chain_tail
->async_tx
.phys
);
568 /* fix up the hardware chain */
569 mv_desc_set_next_desc(old_chain_tail
, grp_start
->async_tx
.phys
);
571 /* if the channel is not busy */
572 if (!mv_chan_is_busy(mv_chan
)) {
573 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
575 * and the curren desc is the end of the chain before
576 * the append, then we need to start the channel
578 if (current_desc
== old_chain_tail
->async_tx
.phys
)
584 mv_xor_start_new_chain(mv_chan
, grp_start
);
587 spin_unlock_bh(&mv_chan
->lock
);
592 /* returns the number of allocated descriptors */
593 static int mv_xor_alloc_chan_resources(struct dma_chan
*chan
)
597 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
598 struct mv_xor_desc_slot
*slot
= NULL
;
599 int num_descs_in_pool
= MV_XOR_POOL_SIZE
/MV_XOR_SLOT_SIZE
;
601 /* Allocate descriptor slots */
602 idx
= mv_chan
->slots_allocated
;
603 while (idx
< num_descs_in_pool
) {
604 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
606 printk(KERN_INFO
"MV XOR Channel only initialized"
607 " %d descriptor slots", idx
);
610 hw_desc
= (char *) mv_chan
->dma_desc_pool_virt
;
611 slot
->hw_desc
= (void *) &hw_desc
[idx
* MV_XOR_SLOT_SIZE
];
613 dma_async_tx_descriptor_init(&slot
->async_tx
, chan
);
614 slot
->async_tx
.tx_submit
= mv_xor_tx_submit
;
615 INIT_LIST_HEAD(&slot
->chain_node
);
616 INIT_LIST_HEAD(&slot
->slot_node
);
617 INIT_LIST_HEAD(&slot
->tx_list
);
618 hw_desc
= (char *) mv_chan
->dma_desc_pool
;
619 slot
->async_tx
.phys
=
620 (dma_addr_t
) &hw_desc
[idx
* MV_XOR_SLOT_SIZE
];
623 spin_lock_bh(&mv_chan
->lock
);
624 mv_chan
->slots_allocated
= idx
;
625 list_add_tail(&slot
->slot_node
, &mv_chan
->all_slots
);
626 spin_unlock_bh(&mv_chan
->lock
);
629 if (mv_chan
->slots_allocated
&& !mv_chan
->last_used
)
630 mv_chan
->last_used
= list_entry(mv_chan
->all_slots
.next
,
631 struct mv_xor_desc_slot
,
634 dev_dbg(mv_chan_to_devp(mv_chan
),
635 "allocated %d descriptor slots last_used: %p\n",
636 mv_chan
->slots_allocated
, mv_chan
->last_used
);
638 return mv_chan
->slots_allocated
? : -ENOMEM
;
641 static struct dma_async_tx_descriptor
*
642 mv_xor_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
643 size_t len
, unsigned long flags
)
645 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
646 struct mv_xor_desc_slot
*sw_desc
, *grp_start
;
649 dev_dbg(mv_chan_to_devp(mv_chan
),
650 "%s dest: %x src %x len: %u flags: %ld\n",
651 __func__
, dest
, src
, len
, flags
);
652 if (unlikely(len
< MV_XOR_MIN_BYTE_COUNT
))
655 BUG_ON(len
> MV_XOR_MAX_BYTE_COUNT
);
657 spin_lock_bh(&mv_chan
->lock
);
658 slot_cnt
= mv_chan_memcpy_slot_count(len
);
659 sw_desc
= mv_xor_alloc_slots(mv_chan
, slot_cnt
, 1);
661 sw_desc
->type
= DMA_MEMCPY
;
662 sw_desc
->async_tx
.flags
= flags
;
663 grp_start
= sw_desc
->group_head
;
664 mv_desc_init(grp_start
, flags
);
665 mv_desc_set_byte_count(grp_start
, len
);
666 mv_desc_set_dest_addr(sw_desc
->group_head
, dest
);
667 mv_desc_set_src_addr(grp_start
, 0, src
);
668 sw_desc
->unmap_src_cnt
= 1;
669 sw_desc
->unmap_len
= len
;
671 spin_unlock_bh(&mv_chan
->lock
);
673 dev_dbg(mv_chan_to_devp(mv_chan
),
674 "%s sw_desc %p async_tx %p\n",
675 __func__
, sw_desc
, sw_desc
? &sw_desc
->async_tx
: NULL
);
677 return sw_desc
? &sw_desc
->async_tx
: NULL
;
680 static struct dma_async_tx_descriptor
*
681 mv_xor_prep_dma_xor(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t
*src
,
682 unsigned int src_cnt
, size_t len
, unsigned long flags
)
684 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
685 struct mv_xor_desc_slot
*sw_desc
, *grp_start
;
688 if (unlikely(len
< MV_XOR_MIN_BYTE_COUNT
))
691 BUG_ON(len
> MV_XOR_MAX_BYTE_COUNT
);
693 dev_dbg(mv_chan_to_devp(mv_chan
),
694 "%s src_cnt: %d len: dest %x %u flags: %ld\n",
695 __func__
, src_cnt
, len
, dest
, flags
);
697 spin_lock_bh(&mv_chan
->lock
);
698 slot_cnt
= mv_chan_xor_slot_count(len
, src_cnt
);
699 sw_desc
= mv_xor_alloc_slots(mv_chan
, slot_cnt
, 1);
701 sw_desc
->type
= DMA_XOR
;
702 sw_desc
->async_tx
.flags
= flags
;
703 grp_start
= sw_desc
->group_head
;
704 mv_desc_init(grp_start
, flags
);
705 /* the byte count field is the same as in memcpy desc*/
706 mv_desc_set_byte_count(grp_start
, len
);
707 mv_desc_set_dest_addr(sw_desc
->group_head
, dest
);
708 sw_desc
->unmap_src_cnt
= src_cnt
;
709 sw_desc
->unmap_len
= len
;
711 mv_desc_set_src_addr(grp_start
, src_cnt
, src
[src_cnt
]);
713 spin_unlock_bh(&mv_chan
->lock
);
714 dev_dbg(mv_chan_to_devp(mv_chan
),
715 "%s sw_desc %p async_tx %p \n",
716 __func__
, sw_desc
, &sw_desc
->async_tx
);
717 return sw_desc
? &sw_desc
->async_tx
: NULL
;
720 static void mv_xor_free_chan_resources(struct dma_chan
*chan
)
722 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
723 struct mv_xor_desc_slot
*iter
, *_iter
;
724 int in_use_descs
= 0;
726 mv_xor_slot_cleanup(mv_chan
);
728 spin_lock_bh(&mv_chan
->lock
);
729 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
732 list_del(&iter
->chain_node
);
734 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
737 list_del(&iter
->completed_node
);
739 list_for_each_entry_safe_reverse(
740 iter
, _iter
, &mv_chan
->all_slots
, slot_node
) {
741 list_del(&iter
->slot_node
);
743 mv_chan
->slots_allocated
--;
745 mv_chan
->last_used
= NULL
;
747 dev_dbg(mv_chan_to_devp(mv_chan
), "%s slots_allocated %d\n",
748 __func__
, mv_chan
->slots_allocated
);
749 spin_unlock_bh(&mv_chan
->lock
);
752 dev_err(mv_chan_to_devp(mv_chan
),
753 "freeing %d in use descriptors!\n", in_use_descs
);
757 * mv_xor_status - poll the status of an XOR transaction
758 * @chan: XOR channel handle
759 * @cookie: XOR transaction identifier
760 * @txstate: XOR transactions state holder (or NULL)
762 static enum dma_status
mv_xor_status(struct dma_chan
*chan
,
764 struct dma_tx_state
*txstate
)
766 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
769 ret
= dma_cookie_status(chan
, cookie
, txstate
);
770 if (ret
== DMA_SUCCESS
) {
771 mv_xor_clean_completed_slots(mv_chan
);
774 mv_xor_slot_cleanup(mv_chan
);
776 return dma_cookie_status(chan
, cookie
, txstate
);
779 static void mv_dump_xor_regs(struct mv_xor_chan
*chan
)
783 val
= readl_relaxed(XOR_CONFIG(chan
));
784 dev_err(mv_chan_to_devp(chan
), "config 0x%08x\n", val
);
786 val
= readl_relaxed(XOR_ACTIVATION(chan
));
787 dev_err(mv_chan_to_devp(chan
), "activation 0x%08x\n", val
);
789 val
= readl_relaxed(XOR_INTR_CAUSE(chan
));
790 dev_err(mv_chan_to_devp(chan
), "intr cause 0x%08x\n", val
);
792 val
= readl_relaxed(XOR_INTR_MASK(chan
));
793 dev_err(mv_chan_to_devp(chan
), "intr mask 0x%08x\n", val
);
795 val
= readl_relaxed(XOR_ERROR_CAUSE(chan
));
796 dev_err(mv_chan_to_devp(chan
), "error cause 0x%08x\n", val
);
798 val
= readl_relaxed(XOR_ERROR_ADDR(chan
));
799 dev_err(mv_chan_to_devp(chan
), "error addr 0x%08x\n", val
);
802 static void mv_xor_err_interrupt_handler(struct mv_xor_chan
*chan
,
805 if (intr_cause
& (1 << 4)) {
806 dev_dbg(mv_chan_to_devp(chan
),
807 "ignore this error\n");
811 dev_err(mv_chan_to_devp(chan
),
812 "error on chan %d. intr cause 0x%08x\n",
813 chan
->idx
, intr_cause
);
815 mv_dump_xor_regs(chan
);
819 static irqreturn_t
mv_xor_interrupt_handler(int irq
, void *data
)
821 struct mv_xor_chan
*chan
= data
;
822 u32 intr_cause
= mv_chan_get_intr_cause(chan
);
824 dev_dbg(mv_chan_to_devp(chan
), "intr cause %x\n", intr_cause
);
826 if (mv_is_err_intr(intr_cause
))
827 mv_xor_err_interrupt_handler(chan
, intr_cause
);
829 tasklet_schedule(&chan
->irq_tasklet
);
831 mv_xor_device_clear_eoc_cause(chan
);
836 static void mv_xor_issue_pending(struct dma_chan
*chan
)
838 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
840 if (mv_chan
->pending
>= MV_XOR_THRESHOLD
) {
841 mv_chan
->pending
= 0;
842 mv_chan_activate(mv_chan
);
847 * Perform a transaction to verify the HW works.
849 #define MV_XOR_TEST_SIZE 2000
851 static int mv_xor_memcpy_self_test(struct mv_xor_chan
*mv_chan
)
855 dma_addr_t src_dma
, dest_dma
;
856 struct dma_chan
*dma_chan
;
858 struct dma_async_tx_descriptor
*tx
;
861 src
= kmalloc(sizeof(u8
) * MV_XOR_TEST_SIZE
, GFP_KERNEL
);
865 dest
= kzalloc(sizeof(u8
) * MV_XOR_TEST_SIZE
, GFP_KERNEL
);
871 /* Fill in src buffer */
872 for (i
= 0; i
< MV_XOR_TEST_SIZE
; i
++)
873 ((u8
*) src
)[i
] = (u8
)i
;
875 dma_chan
= &mv_chan
->dmachan
;
876 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
881 dest_dma
= dma_map_single(dma_chan
->device
->dev
, dest
,
882 MV_XOR_TEST_SIZE
, DMA_FROM_DEVICE
);
884 src_dma
= dma_map_single(dma_chan
->device
->dev
, src
,
885 MV_XOR_TEST_SIZE
, DMA_TO_DEVICE
);
887 tx
= mv_xor_prep_dma_memcpy(dma_chan
, dest_dma
, src_dma
,
888 MV_XOR_TEST_SIZE
, 0);
889 cookie
= mv_xor_tx_submit(tx
);
890 mv_xor_issue_pending(dma_chan
);
894 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
896 dev_err(dma_chan
->device
->dev
,
897 "Self-test copy timed out, disabling\n");
902 dma_sync_single_for_cpu(dma_chan
->device
->dev
, dest_dma
,
903 MV_XOR_TEST_SIZE
, DMA_FROM_DEVICE
);
904 if (memcmp(src
, dest
, MV_XOR_TEST_SIZE
)) {
905 dev_err(dma_chan
->device
->dev
,
906 "Self-test copy failed compare, disabling\n");
912 mv_xor_free_chan_resources(dma_chan
);
919 #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
921 mv_xor_xor_self_test(struct mv_xor_chan
*mv_chan
)
925 struct page
*xor_srcs
[MV_XOR_NUM_SRC_TEST
];
926 dma_addr_t dma_srcs
[MV_XOR_NUM_SRC_TEST
];
928 struct dma_async_tx_descriptor
*tx
;
929 struct dma_chan
*dma_chan
;
935 for (src_idx
= 0; src_idx
< MV_XOR_NUM_SRC_TEST
; src_idx
++) {
936 xor_srcs
[src_idx
] = alloc_page(GFP_KERNEL
);
937 if (!xor_srcs
[src_idx
]) {
939 __free_page(xor_srcs
[src_idx
]);
944 dest
= alloc_page(GFP_KERNEL
);
947 __free_page(xor_srcs
[src_idx
]);
951 /* Fill in src buffers */
952 for (src_idx
= 0; src_idx
< MV_XOR_NUM_SRC_TEST
; src_idx
++) {
953 u8
*ptr
= page_address(xor_srcs
[src_idx
]);
954 for (i
= 0; i
< PAGE_SIZE
; i
++)
955 ptr
[i
] = (1 << src_idx
);
958 for (src_idx
= 0; src_idx
< MV_XOR_NUM_SRC_TEST
; src_idx
++)
959 cmp_byte
^= (u8
) (1 << src_idx
);
961 cmp_word
= (cmp_byte
<< 24) | (cmp_byte
<< 16) |
962 (cmp_byte
<< 8) | cmp_byte
;
964 memset(page_address(dest
), 0, PAGE_SIZE
);
966 dma_chan
= &mv_chan
->dmachan
;
967 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
973 dest_dma
= dma_map_page(dma_chan
->device
->dev
, dest
, 0, PAGE_SIZE
,
976 for (i
= 0; i
< MV_XOR_NUM_SRC_TEST
; i
++)
977 dma_srcs
[i
] = dma_map_page(dma_chan
->device
->dev
, xor_srcs
[i
],
978 0, PAGE_SIZE
, DMA_TO_DEVICE
);
980 tx
= mv_xor_prep_dma_xor(dma_chan
, dest_dma
, dma_srcs
,
981 MV_XOR_NUM_SRC_TEST
, PAGE_SIZE
, 0);
983 cookie
= mv_xor_tx_submit(tx
);
984 mv_xor_issue_pending(dma_chan
);
988 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
990 dev_err(dma_chan
->device
->dev
,
991 "Self-test xor timed out, disabling\n");
996 dma_sync_single_for_cpu(dma_chan
->device
->dev
, dest_dma
,
997 PAGE_SIZE
, DMA_FROM_DEVICE
);
998 for (i
= 0; i
< (PAGE_SIZE
/ sizeof(u32
)); i
++) {
999 u32
*ptr
= page_address(dest
);
1000 if (ptr
[i
] != cmp_word
) {
1001 dev_err(dma_chan
->device
->dev
,
1002 "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
1003 i
, ptr
[i
], cmp_word
);
1005 goto free_resources
;
1010 mv_xor_free_chan_resources(dma_chan
);
1012 src_idx
= MV_XOR_NUM_SRC_TEST
;
1014 __free_page(xor_srcs
[src_idx
]);
1019 /* This driver does not implement any of the optional DMA operations. */
1021 mv_xor_control(struct dma_chan
*chan
, enum dma_ctrl_cmd cmd
,
1027 static int mv_xor_channel_remove(struct mv_xor_chan
*mv_chan
)
1029 struct dma_chan
*chan
, *_chan
;
1030 struct device
*dev
= mv_chan
->dmadev
.dev
;
1032 dma_async_device_unregister(&mv_chan
->dmadev
);
1034 dma_free_coherent(dev
, MV_XOR_POOL_SIZE
,
1035 mv_chan
->dma_desc_pool_virt
, mv_chan
->dma_desc_pool
);
1037 list_for_each_entry_safe(chan
, _chan
, &mv_chan
->dmadev
.channels
,
1039 list_del(&chan
->device_node
);
1042 free_irq(mv_chan
->irq
, mv_chan
);
1047 static struct mv_xor_chan
*
1048 mv_xor_channel_add(struct mv_xor_device
*xordev
,
1049 struct platform_device
*pdev
,
1050 int idx
, dma_cap_mask_t cap_mask
, int irq
)
1053 struct mv_xor_chan
*mv_chan
;
1054 struct dma_device
*dma_dev
;
1056 mv_chan
= devm_kzalloc(&pdev
->dev
, sizeof(*mv_chan
), GFP_KERNEL
);
1058 return ERR_PTR(-ENOMEM
);
1063 dma_dev
= &mv_chan
->dmadev
;
1065 /* allocate coherent memory for hardware descriptors
1066 * note: writecombine gives slightly better performance, but
1067 * requires that we explicitly flush the writes
1069 mv_chan
->dma_desc_pool_virt
=
1070 dma_alloc_writecombine(&pdev
->dev
, MV_XOR_POOL_SIZE
,
1071 &mv_chan
->dma_desc_pool
, GFP_KERNEL
);
1072 if (!mv_chan
->dma_desc_pool_virt
)
1073 return ERR_PTR(-ENOMEM
);
1075 /* discover transaction capabilites from the platform data */
1076 dma_dev
->cap_mask
= cap_mask
;
1078 INIT_LIST_HEAD(&dma_dev
->channels
);
1080 /* set base routines */
1081 dma_dev
->device_alloc_chan_resources
= mv_xor_alloc_chan_resources
;
1082 dma_dev
->device_free_chan_resources
= mv_xor_free_chan_resources
;
1083 dma_dev
->device_tx_status
= mv_xor_status
;
1084 dma_dev
->device_issue_pending
= mv_xor_issue_pending
;
1085 dma_dev
->device_control
= mv_xor_control
;
1086 dma_dev
->dev
= &pdev
->dev
;
1088 /* set prep routines based on capability */
1089 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
))
1090 dma_dev
->device_prep_dma_memcpy
= mv_xor_prep_dma_memcpy
;
1091 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1092 dma_dev
->max_xor
= 8;
1093 dma_dev
->device_prep_dma_xor
= mv_xor_prep_dma_xor
;
1096 mv_chan
->mmr_base
= xordev
->xor_base
;
1097 if (!mv_chan
->mmr_base
) {
1101 tasklet_init(&mv_chan
->irq_tasklet
, mv_xor_tasklet
, (unsigned long)
1104 /* clear errors before enabling interrupts */
1105 mv_xor_device_clear_err_status(mv_chan
);
1107 ret
= request_irq(mv_chan
->irq
, mv_xor_interrupt_handler
,
1108 0, dev_name(&pdev
->dev
), mv_chan
);
1112 mv_chan_unmask_interrupts(mv_chan
);
1114 mv_set_mode(mv_chan
, DMA_MEMCPY
);
1116 spin_lock_init(&mv_chan
->lock
);
1117 INIT_LIST_HEAD(&mv_chan
->chain
);
1118 INIT_LIST_HEAD(&mv_chan
->completed_slots
);
1119 INIT_LIST_HEAD(&mv_chan
->all_slots
);
1120 mv_chan
->dmachan
.device
= dma_dev
;
1121 dma_cookie_init(&mv_chan
->dmachan
);
1123 list_add_tail(&mv_chan
->dmachan
.device_node
, &dma_dev
->channels
);
1125 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
)) {
1126 ret
= mv_xor_memcpy_self_test(mv_chan
);
1127 dev_dbg(&pdev
->dev
, "memcpy self test returned %d\n", ret
);
1132 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1133 ret
= mv_xor_xor_self_test(mv_chan
);
1134 dev_dbg(&pdev
->dev
, "xor self test returned %d\n", ret
);
1139 dev_info(&pdev
->dev
, "Marvell XOR: ( %s%s%s)\n",
1140 dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
) ? "xor " : "",
1141 dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
) ? "cpy " : "",
1142 dma_has_cap(DMA_INTERRUPT
, dma_dev
->cap_mask
) ? "intr " : "");
1144 dma_async_device_register(dma_dev
);
1148 free_irq(mv_chan
->irq
, mv_chan
);
1150 dma_free_coherent(&pdev
->dev
, MV_XOR_POOL_SIZE
,
1151 mv_chan
->dma_desc_pool_virt
, mv_chan
->dma_desc_pool
);
1152 return ERR_PTR(ret
);
1156 mv_xor_conf_mbus_windows(struct mv_xor_device
*xordev
,
1157 const struct mbus_dram_target_info
*dram
)
1159 void __iomem
*base
= xordev
->xor_base
;
1163 for (i
= 0; i
< 8; i
++) {
1164 writel(0, base
+ WINDOW_BASE(i
));
1165 writel(0, base
+ WINDOW_SIZE(i
));
1167 writel(0, base
+ WINDOW_REMAP_HIGH(i
));
1170 for (i
= 0; i
< dram
->num_cs
; i
++) {
1171 const struct mbus_dram_window
*cs
= dram
->cs
+ i
;
1173 writel((cs
->base
& 0xffff0000) |
1174 (cs
->mbus_attr
<< 8) |
1175 dram
->mbus_dram_target_id
, base
+ WINDOW_BASE(i
));
1176 writel((cs
->size
- 1) & 0xffff0000, base
+ WINDOW_SIZE(i
));
1178 win_enable
|= (1 << i
);
1179 win_enable
|= 3 << (16 + (2 * i
));
1182 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(0));
1183 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(1));
1184 writel(0, base
+ WINDOW_OVERRIDE_CTRL(0));
1185 writel(0, base
+ WINDOW_OVERRIDE_CTRL(1));
1188 static int mv_xor_probe(struct platform_device
*pdev
)
1190 const struct mbus_dram_target_info
*dram
;
1191 struct mv_xor_device
*xordev
;
1192 struct mv_xor_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1193 struct resource
*res
;
1196 dev_notice(&pdev
->dev
, "Marvell shared XOR driver\n");
1198 xordev
= devm_kzalloc(&pdev
->dev
, sizeof(*xordev
), GFP_KERNEL
);
1202 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1206 xordev
->xor_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1207 resource_size(res
));
1208 if (!xordev
->xor_base
)
1211 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1215 xordev
->xor_high_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1216 resource_size(res
));
1217 if (!xordev
->xor_high_base
)
1220 platform_set_drvdata(pdev
, xordev
);
1223 * (Re-)program MBUS remapping windows if we are asked to.
1225 dram
= mv_mbus_dram_info();
1227 mv_xor_conf_mbus_windows(xordev
, dram
);
1229 /* Not all platforms can gate the clock, so it is not
1230 * an error if the clock does not exists.
1232 xordev
->clk
= clk_get(&pdev
->dev
, NULL
);
1233 if (!IS_ERR(xordev
->clk
))
1234 clk_prepare_enable(xordev
->clk
);
1236 if (pdev
->dev
.of_node
) {
1237 struct device_node
*np
;
1240 for_each_child_of_node(pdev
->dev
.of_node
, np
) {
1241 dma_cap_mask_t cap_mask
;
1244 dma_cap_zero(cap_mask
);
1245 if (of_property_read_bool(np
, "dmacap,memcpy"))
1246 dma_cap_set(DMA_MEMCPY
, cap_mask
);
1247 if (of_property_read_bool(np
, "dmacap,xor"))
1248 dma_cap_set(DMA_XOR
, cap_mask
);
1249 if (of_property_read_bool(np
, "dmacap,interrupt"))
1250 dma_cap_set(DMA_INTERRUPT
, cap_mask
);
1252 irq
= irq_of_parse_and_map(np
, 0);
1255 goto err_channel_add
;
1258 xordev
->channels
[i
] =
1259 mv_xor_channel_add(xordev
, pdev
, i
,
1261 if (IS_ERR(xordev
->channels
[i
])) {
1262 ret
= PTR_ERR(xordev
->channels
[i
]);
1263 xordev
->channels
[i
] = NULL
;
1264 irq_dispose_mapping(irq
);
1265 goto err_channel_add
;
1270 } else if (pdata
&& pdata
->channels
) {
1271 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++) {
1272 struct mv_xor_channel_data
*cd
;
1275 cd
= &pdata
->channels
[i
];
1278 goto err_channel_add
;
1281 irq
= platform_get_irq(pdev
, i
);
1284 goto err_channel_add
;
1287 xordev
->channels
[i
] =
1288 mv_xor_channel_add(xordev
, pdev
, i
,
1290 if (IS_ERR(xordev
->channels
[i
])) {
1291 ret
= PTR_ERR(xordev
->channels
[i
]);
1292 goto err_channel_add
;
1300 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++)
1301 if (xordev
->channels
[i
]) {
1302 mv_xor_channel_remove(xordev
->channels
[i
]);
1303 if (pdev
->dev
.of_node
)
1304 irq_dispose_mapping(xordev
->channels
[i
]->irq
);
1307 if (!IS_ERR(xordev
->clk
)) {
1308 clk_disable_unprepare(xordev
->clk
);
1309 clk_put(xordev
->clk
);
1315 static int mv_xor_remove(struct platform_device
*pdev
)
1317 struct mv_xor_device
*xordev
= platform_get_drvdata(pdev
);
1320 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++) {
1321 if (xordev
->channels
[i
])
1322 mv_xor_channel_remove(xordev
->channels
[i
]);
1325 if (!IS_ERR(xordev
->clk
)) {
1326 clk_disable_unprepare(xordev
->clk
);
1327 clk_put(xordev
->clk
);
1334 static struct of_device_id mv_xor_dt_ids
[] = {
1335 { .compatible
= "marvell,orion-xor", },
1338 MODULE_DEVICE_TABLE(of
, mv_xor_dt_ids
);
1341 static struct platform_driver mv_xor_driver
= {
1342 .probe
= mv_xor_probe
,
1343 .remove
= mv_xor_remove
,
1345 .owner
= THIS_MODULE
,
1346 .name
= MV_XOR_NAME
,
1347 .of_match_table
= of_match_ptr(mv_xor_dt_ids
),
1352 static int __init
mv_xor_init(void)
1354 return platform_driver_register(&mv_xor_driver
);
1356 module_init(mv_xor_init
);
1358 /* it's currently unsafe to unload this module */
1360 static void __exit
mv_xor_exit(void)
1362 platform_driver_unregister(&mv_xor_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");