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
)
210 dev_dbg(mv_chan_to_devp(chan
), " activate chan.\n");
211 activation
= readl_relaxed(XOR_ACTIVATION(chan
));
213 writel_relaxed(activation
, XOR_ACTIVATION(chan
));
216 static char mv_chan_is_busy(struct mv_xor_chan
*chan
)
218 u32 state
= readl_relaxed(XOR_ACTIVATION(chan
));
220 state
= (state
>> 4) & 0x3;
222 return (state
== 1) ? 1 : 0;
225 static int mv_chan_xor_slot_count(size_t len
, int src_cnt
)
231 * mv_xor_free_slots - flags descriptor slots for reuse
232 * @slot: Slot to free
233 * Caller must hold &mv_chan->lock while calling this function
235 static void mv_xor_free_slots(struct mv_xor_chan
*mv_chan
,
236 struct mv_xor_desc_slot
*slot
)
238 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d slot %p\n",
239 __func__
, __LINE__
, slot
);
241 slot
->slots_per_op
= 0;
246 * mv_xor_start_new_chain - program the engine to operate on new chain headed by
248 * Caller must hold &mv_chan->lock while calling this function
250 static void mv_xor_start_new_chain(struct mv_xor_chan
*mv_chan
,
251 struct mv_xor_desc_slot
*sw_desc
)
253 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d: sw_desc %p\n",
254 __func__
, __LINE__
, sw_desc
);
255 if (sw_desc
->type
!= mv_chan
->current_type
)
256 mv_set_mode(mv_chan
, sw_desc
->type
);
258 /* set the hardware chain */
259 mv_chan_set_next_descriptor(mv_chan
, sw_desc
->async_tx
.phys
);
261 mv_chan
->pending
+= sw_desc
->slot_cnt
;
262 mv_xor_issue_pending(&mv_chan
->dmachan
);
266 mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot
*desc
,
267 struct mv_xor_chan
*mv_chan
, dma_cookie_t cookie
)
269 BUG_ON(desc
->async_tx
.cookie
< 0);
271 if (desc
->async_tx
.cookie
> 0) {
272 cookie
= desc
->async_tx
.cookie
;
274 /* call the callback (must not sleep or submit new
275 * operations to this channel)
277 if (desc
->async_tx
.callback
)
278 desc
->async_tx
.callback(
279 desc
->async_tx
.callback_param
);
281 /* unmap dma addresses
282 * (unmap_single vs unmap_page?)
284 if (desc
->group_head
&& desc
->unmap_len
) {
285 struct mv_xor_desc_slot
*unmap
= desc
->group_head
;
286 struct device
*dev
= mv_chan_to_devp(mv_chan
);
287 u32 len
= unmap
->unmap_len
;
288 enum dma_ctrl_flags flags
= desc
->async_tx
.flags
;
293 src_cnt
= unmap
->unmap_src_cnt
;
294 dest
= mv_desc_get_dest_addr(unmap
);
295 if (!(flags
& DMA_COMPL_SKIP_DEST_UNMAP
)) {
296 enum dma_data_direction dir
;
298 if (src_cnt
> 1) /* is xor ? */
299 dir
= DMA_BIDIRECTIONAL
;
301 dir
= DMA_FROM_DEVICE
;
302 dma_unmap_page(dev
, dest
, len
, dir
);
305 if (!(flags
& DMA_COMPL_SKIP_SRC_UNMAP
)) {
307 addr
= mv_desc_get_src_addr(unmap
,
311 dma_unmap_page(dev
, addr
, len
,
315 desc
->group_head
= NULL
;
319 /* run dependent operations */
320 dma_run_dependencies(&desc
->async_tx
);
326 mv_xor_clean_completed_slots(struct mv_xor_chan
*mv_chan
)
328 struct mv_xor_desc_slot
*iter
, *_iter
;
330 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d\n", __func__
, __LINE__
);
331 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
334 if (async_tx_test_ack(&iter
->async_tx
)) {
335 list_del(&iter
->completed_node
);
336 mv_xor_free_slots(mv_chan
, iter
);
343 mv_xor_clean_slot(struct mv_xor_desc_slot
*desc
,
344 struct mv_xor_chan
*mv_chan
)
346 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d: desc %p flags %d\n",
347 __func__
, __LINE__
, desc
, desc
->async_tx
.flags
);
348 list_del(&desc
->chain_node
);
349 /* the client is allowed to attach dependent operations
352 if (!async_tx_test_ack(&desc
->async_tx
)) {
353 /* move this slot to the completed_slots */
354 list_add_tail(&desc
->completed_node
, &mv_chan
->completed_slots
);
358 mv_xor_free_slots(mv_chan
, desc
);
362 static void __mv_xor_slot_cleanup(struct mv_xor_chan
*mv_chan
)
364 struct mv_xor_desc_slot
*iter
, *_iter
;
365 dma_cookie_t cookie
= 0;
366 int busy
= mv_chan_is_busy(mv_chan
);
367 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
368 int seen_current
= 0;
370 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d\n", __func__
, __LINE__
);
371 dev_dbg(mv_chan_to_devp(mv_chan
), "current_desc %x\n", current_desc
);
372 mv_xor_clean_completed_slots(mv_chan
);
374 /* free completed slots from the chain starting with
375 * the oldest descriptor
378 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
381 prefetch(&_iter
->async_tx
);
383 /* do not advance past the current descriptor loaded into the
384 * hardware channel, subsequent descriptors are either in
385 * process or have not been submitted
390 /* stop the search if we reach the current descriptor and the
393 if (iter
->async_tx
.phys
== current_desc
) {
399 cookie
= mv_xor_run_tx_complete_actions(iter
, mv_chan
, cookie
);
401 if (mv_xor_clean_slot(iter
, mv_chan
))
405 if ((busy
== 0) && !list_empty(&mv_chan
->chain
)) {
406 struct mv_xor_desc_slot
*chain_head
;
407 chain_head
= list_entry(mv_chan
->chain
.next
,
408 struct mv_xor_desc_slot
,
411 mv_xor_start_new_chain(mv_chan
, chain_head
);
415 mv_chan
->dmachan
.completed_cookie
= cookie
;
419 mv_xor_slot_cleanup(struct mv_xor_chan
*mv_chan
)
421 spin_lock_bh(&mv_chan
->lock
);
422 __mv_xor_slot_cleanup(mv_chan
);
423 spin_unlock_bh(&mv_chan
->lock
);
426 static void mv_xor_tasklet(unsigned long data
)
428 struct mv_xor_chan
*chan
= (struct mv_xor_chan
*) data
;
429 mv_xor_slot_cleanup(chan
);
432 static struct mv_xor_desc_slot
*
433 mv_xor_alloc_slots(struct mv_xor_chan
*mv_chan
, int num_slots
,
436 struct mv_xor_desc_slot
*iter
, *_iter
, *alloc_start
= NULL
;
438 int slots_found
, retry
= 0;
440 /* start search from the last allocated descrtiptor
441 * if a contiguous allocation can not be found start searching
442 * from the beginning of the list
447 iter
= mv_chan
->last_used
;
449 iter
= list_entry(&mv_chan
->all_slots
,
450 struct mv_xor_desc_slot
,
453 list_for_each_entry_safe_continue(
454 iter
, _iter
, &mv_chan
->all_slots
, slot_node
) {
456 prefetch(&_iter
->async_tx
);
457 if (iter
->slots_per_op
) {
458 /* give up after finding the first busy slot
459 * on the second pass through the list
468 /* start the allocation if the slot is correctly aligned */
472 if (slots_found
== num_slots
) {
473 struct mv_xor_desc_slot
*alloc_tail
= NULL
;
474 struct mv_xor_desc_slot
*last_used
= NULL
;
479 /* pre-ack all but the last descriptor */
480 async_tx_ack(&iter
->async_tx
);
482 list_add_tail(&iter
->chain_node
, &chain
);
484 iter
->async_tx
.cookie
= 0;
485 iter
->slot_cnt
= num_slots
;
486 iter
->xor_check_result
= NULL
;
487 for (i
= 0; i
< slots_per_op
; i
++) {
488 iter
->slots_per_op
= slots_per_op
- i
;
490 iter
= list_entry(iter
->slot_node
.next
,
491 struct mv_xor_desc_slot
,
494 num_slots
-= slots_per_op
;
496 alloc_tail
->group_head
= alloc_start
;
497 alloc_tail
->async_tx
.cookie
= -EBUSY
;
498 list_splice(&chain
, &alloc_tail
->tx_list
);
499 mv_chan
->last_used
= last_used
;
500 mv_desc_clear_next_desc(alloc_start
);
501 mv_desc_clear_next_desc(alloc_tail
);
508 /* try to free some slots if the allocation fails */
509 tasklet_schedule(&mv_chan
->irq_tasklet
);
514 /************************ DMA engine API functions ****************************/
516 mv_xor_tx_submit(struct dma_async_tx_descriptor
*tx
)
518 struct mv_xor_desc_slot
*sw_desc
= to_mv_xor_slot(tx
);
519 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(tx
->chan
);
520 struct mv_xor_desc_slot
*grp_start
, *old_chain_tail
;
522 int new_hw_chain
= 1;
524 dev_dbg(mv_chan_to_devp(mv_chan
),
525 "%s sw_desc %p: async_tx %p\n",
526 __func__
, sw_desc
, &sw_desc
->async_tx
);
528 grp_start
= sw_desc
->group_head
;
530 spin_lock_bh(&mv_chan
->lock
);
531 cookie
= dma_cookie_assign(tx
);
533 if (list_empty(&mv_chan
->chain
))
534 list_splice_init(&sw_desc
->tx_list
, &mv_chan
->chain
);
538 old_chain_tail
= list_entry(mv_chan
->chain
.prev
,
539 struct mv_xor_desc_slot
,
541 list_splice_init(&grp_start
->tx_list
,
542 &old_chain_tail
->chain_node
);
544 if (!mv_can_chain(grp_start
))
547 dev_dbg(mv_chan_to_devp(mv_chan
), "Append to last desc %x\n",
548 old_chain_tail
->async_tx
.phys
);
550 /* fix up the hardware chain */
551 mv_desc_set_next_desc(old_chain_tail
, grp_start
->async_tx
.phys
);
553 /* if the channel is not busy */
554 if (!mv_chan_is_busy(mv_chan
)) {
555 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
557 * and the curren desc is the end of the chain before
558 * the append, then we need to start the channel
560 if (current_desc
== old_chain_tail
->async_tx
.phys
)
566 mv_xor_start_new_chain(mv_chan
, grp_start
);
569 spin_unlock_bh(&mv_chan
->lock
);
574 /* returns the number of allocated descriptors */
575 static int mv_xor_alloc_chan_resources(struct dma_chan
*chan
)
579 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
580 struct mv_xor_desc_slot
*slot
= NULL
;
581 int num_descs_in_pool
= MV_XOR_POOL_SIZE
/MV_XOR_SLOT_SIZE
;
583 /* Allocate descriptor slots */
584 idx
= mv_chan
->slots_allocated
;
585 while (idx
< num_descs_in_pool
) {
586 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
588 printk(KERN_INFO
"MV XOR Channel only initialized"
589 " %d descriptor slots", idx
);
592 hw_desc
= (char *) mv_chan
->dma_desc_pool_virt
;
593 slot
->hw_desc
= (void *) &hw_desc
[idx
* MV_XOR_SLOT_SIZE
];
595 dma_async_tx_descriptor_init(&slot
->async_tx
, chan
);
596 slot
->async_tx
.tx_submit
= mv_xor_tx_submit
;
597 INIT_LIST_HEAD(&slot
->chain_node
);
598 INIT_LIST_HEAD(&slot
->slot_node
);
599 INIT_LIST_HEAD(&slot
->tx_list
);
600 hw_desc
= (char *) mv_chan
->dma_desc_pool
;
601 slot
->async_tx
.phys
=
602 (dma_addr_t
) &hw_desc
[idx
* MV_XOR_SLOT_SIZE
];
605 spin_lock_bh(&mv_chan
->lock
);
606 mv_chan
->slots_allocated
= idx
;
607 list_add_tail(&slot
->slot_node
, &mv_chan
->all_slots
);
608 spin_unlock_bh(&mv_chan
->lock
);
611 if (mv_chan
->slots_allocated
&& !mv_chan
->last_used
)
612 mv_chan
->last_used
= list_entry(mv_chan
->all_slots
.next
,
613 struct mv_xor_desc_slot
,
616 dev_dbg(mv_chan_to_devp(mv_chan
),
617 "allocated %d descriptor slots last_used: %p\n",
618 mv_chan
->slots_allocated
, mv_chan
->last_used
);
620 return mv_chan
->slots_allocated
? : -ENOMEM
;
623 static struct dma_async_tx_descriptor
*
624 mv_xor_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
625 size_t len
, unsigned long flags
)
627 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
628 struct mv_xor_desc_slot
*sw_desc
, *grp_start
;
631 dev_dbg(mv_chan_to_devp(mv_chan
),
632 "%s dest: %x src %x len: %u flags: %ld\n",
633 __func__
, dest
, src
, len
, flags
);
634 if (unlikely(len
< MV_XOR_MIN_BYTE_COUNT
))
637 BUG_ON(len
> MV_XOR_MAX_BYTE_COUNT
);
639 spin_lock_bh(&mv_chan
->lock
);
640 slot_cnt
= mv_chan_memcpy_slot_count(len
);
641 sw_desc
= mv_xor_alloc_slots(mv_chan
, slot_cnt
, 1);
643 sw_desc
->type
= DMA_MEMCPY
;
644 sw_desc
->async_tx
.flags
= flags
;
645 grp_start
= sw_desc
->group_head
;
646 mv_desc_init(grp_start
, flags
);
647 mv_desc_set_byte_count(grp_start
, len
);
648 mv_desc_set_dest_addr(sw_desc
->group_head
, dest
);
649 mv_desc_set_src_addr(grp_start
, 0, src
);
650 sw_desc
->unmap_src_cnt
= 1;
651 sw_desc
->unmap_len
= len
;
653 spin_unlock_bh(&mv_chan
->lock
);
655 dev_dbg(mv_chan_to_devp(mv_chan
),
656 "%s sw_desc %p async_tx %p\n",
657 __func__
, sw_desc
, sw_desc
? &sw_desc
->async_tx
: NULL
);
659 return sw_desc
? &sw_desc
->async_tx
: NULL
;
662 static struct dma_async_tx_descriptor
*
663 mv_xor_prep_dma_xor(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t
*src
,
664 unsigned int src_cnt
, size_t len
, unsigned long flags
)
666 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
667 struct mv_xor_desc_slot
*sw_desc
, *grp_start
;
670 if (unlikely(len
< MV_XOR_MIN_BYTE_COUNT
))
673 BUG_ON(len
> MV_XOR_MAX_BYTE_COUNT
);
675 dev_dbg(mv_chan_to_devp(mv_chan
),
676 "%s src_cnt: %d len: dest %x %u flags: %ld\n",
677 __func__
, src_cnt
, len
, dest
, flags
);
679 spin_lock_bh(&mv_chan
->lock
);
680 slot_cnt
= mv_chan_xor_slot_count(len
, src_cnt
);
681 sw_desc
= mv_xor_alloc_slots(mv_chan
, slot_cnt
, 1);
683 sw_desc
->type
= DMA_XOR
;
684 sw_desc
->async_tx
.flags
= flags
;
685 grp_start
= sw_desc
->group_head
;
686 mv_desc_init(grp_start
, flags
);
687 /* the byte count field is the same as in memcpy desc*/
688 mv_desc_set_byte_count(grp_start
, len
);
689 mv_desc_set_dest_addr(sw_desc
->group_head
, dest
);
690 sw_desc
->unmap_src_cnt
= src_cnt
;
691 sw_desc
->unmap_len
= len
;
693 mv_desc_set_src_addr(grp_start
, src_cnt
, src
[src_cnt
]);
695 spin_unlock_bh(&mv_chan
->lock
);
696 dev_dbg(mv_chan_to_devp(mv_chan
),
697 "%s sw_desc %p async_tx %p \n",
698 __func__
, sw_desc
, &sw_desc
->async_tx
);
699 return sw_desc
? &sw_desc
->async_tx
: NULL
;
702 static void mv_xor_free_chan_resources(struct dma_chan
*chan
)
704 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
705 struct mv_xor_desc_slot
*iter
, *_iter
;
706 int in_use_descs
= 0;
708 mv_xor_slot_cleanup(mv_chan
);
710 spin_lock_bh(&mv_chan
->lock
);
711 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
714 list_del(&iter
->chain_node
);
716 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
719 list_del(&iter
->completed_node
);
721 list_for_each_entry_safe_reverse(
722 iter
, _iter
, &mv_chan
->all_slots
, slot_node
) {
723 list_del(&iter
->slot_node
);
725 mv_chan
->slots_allocated
--;
727 mv_chan
->last_used
= NULL
;
729 dev_dbg(mv_chan_to_devp(mv_chan
), "%s slots_allocated %d\n",
730 __func__
, mv_chan
->slots_allocated
);
731 spin_unlock_bh(&mv_chan
->lock
);
734 dev_err(mv_chan_to_devp(mv_chan
),
735 "freeing %d in use descriptors!\n", in_use_descs
);
739 * mv_xor_status - poll the status of an XOR transaction
740 * @chan: XOR channel handle
741 * @cookie: XOR transaction identifier
742 * @txstate: XOR transactions state holder (or NULL)
744 static enum dma_status
mv_xor_status(struct dma_chan
*chan
,
746 struct dma_tx_state
*txstate
)
748 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
751 ret
= dma_cookie_status(chan
, cookie
, txstate
);
752 if (ret
== DMA_SUCCESS
) {
753 mv_xor_clean_completed_slots(mv_chan
);
756 mv_xor_slot_cleanup(mv_chan
);
758 return dma_cookie_status(chan
, cookie
, txstate
);
761 static void mv_dump_xor_regs(struct mv_xor_chan
*chan
)
765 val
= readl_relaxed(XOR_CONFIG(chan
));
766 dev_err(mv_chan_to_devp(chan
), "config 0x%08x\n", val
);
768 val
= readl_relaxed(XOR_ACTIVATION(chan
));
769 dev_err(mv_chan_to_devp(chan
), "activation 0x%08x\n", val
);
771 val
= readl_relaxed(XOR_INTR_CAUSE(chan
));
772 dev_err(mv_chan_to_devp(chan
), "intr cause 0x%08x\n", val
);
774 val
= readl_relaxed(XOR_INTR_MASK(chan
));
775 dev_err(mv_chan_to_devp(chan
), "intr mask 0x%08x\n", val
);
777 val
= readl_relaxed(XOR_ERROR_CAUSE(chan
));
778 dev_err(mv_chan_to_devp(chan
), "error cause 0x%08x\n", val
);
780 val
= readl_relaxed(XOR_ERROR_ADDR(chan
));
781 dev_err(mv_chan_to_devp(chan
), "error addr 0x%08x\n", val
);
784 static void mv_xor_err_interrupt_handler(struct mv_xor_chan
*chan
,
787 if (intr_cause
& (1 << 4)) {
788 dev_dbg(mv_chan_to_devp(chan
),
789 "ignore this error\n");
793 dev_err(mv_chan_to_devp(chan
),
794 "error on chan %d. intr cause 0x%08x\n",
795 chan
->idx
, intr_cause
);
797 mv_dump_xor_regs(chan
);
801 static irqreturn_t
mv_xor_interrupt_handler(int irq
, void *data
)
803 struct mv_xor_chan
*chan
= data
;
804 u32 intr_cause
= mv_chan_get_intr_cause(chan
);
806 dev_dbg(mv_chan_to_devp(chan
), "intr cause %x\n", intr_cause
);
808 if (mv_is_err_intr(intr_cause
))
809 mv_xor_err_interrupt_handler(chan
, intr_cause
);
811 tasklet_schedule(&chan
->irq_tasklet
);
813 mv_xor_device_clear_eoc_cause(chan
);
818 static void mv_xor_issue_pending(struct dma_chan
*chan
)
820 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
822 if (mv_chan
->pending
>= MV_XOR_THRESHOLD
) {
823 mv_chan
->pending
= 0;
824 mv_chan_activate(mv_chan
);
829 * Perform a transaction to verify the HW works.
831 #define MV_XOR_TEST_SIZE 2000
833 static int mv_xor_memcpy_self_test(struct mv_xor_chan
*mv_chan
)
837 dma_addr_t src_dma
, dest_dma
;
838 struct dma_chan
*dma_chan
;
840 struct dma_async_tx_descriptor
*tx
;
843 src
= kmalloc(sizeof(u8
) * MV_XOR_TEST_SIZE
, GFP_KERNEL
);
847 dest
= kzalloc(sizeof(u8
) * MV_XOR_TEST_SIZE
, GFP_KERNEL
);
853 /* Fill in src buffer */
854 for (i
= 0; i
< MV_XOR_TEST_SIZE
; i
++)
855 ((u8
*) src
)[i
] = (u8
)i
;
857 dma_chan
= &mv_chan
->dmachan
;
858 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
863 dest_dma
= dma_map_single(dma_chan
->device
->dev
, dest
,
864 MV_XOR_TEST_SIZE
, DMA_FROM_DEVICE
);
866 src_dma
= dma_map_single(dma_chan
->device
->dev
, src
,
867 MV_XOR_TEST_SIZE
, DMA_TO_DEVICE
);
869 tx
= mv_xor_prep_dma_memcpy(dma_chan
, dest_dma
, src_dma
,
870 MV_XOR_TEST_SIZE
, 0);
871 cookie
= mv_xor_tx_submit(tx
);
872 mv_xor_issue_pending(dma_chan
);
876 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
878 dev_err(dma_chan
->device
->dev
,
879 "Self-test copy timed out, disabling\n");
884 dma_sync_single_for_cpu(dma_chan
->device
->dev
, dest_dma
,
885 MV_XOR_TEST_SIZE
, DMA_FROM_DEVICE
);
886 if (memcmp(src
, dest
, MV_XOR_TEST_SIZE
)) {
887 dev_err(dma_chan
->device
->dev
,
888 "Self-test copy failed compare, disabling\n");
894 mv_xor_free_chan_resources(dma_chan
);
901 #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
903 mv_xor_xor_self_test(struct mv_xor_chan
*mv_chan
)
907 struct page
*xor_srcs
[MV_XOR_NUM_SRC_TEST
];
908 dma_addr_t dma_srcs
[MV_XOR_NUM_SRC_TEST
];
910 struct dma_async_tx_descriptor
*tx
;
911 struct dma_chan
*dma_chan
;
917 for (src_idx
= 0; src_idx
< MV_XOR_NUM_SRC_TEST
; src_idx
++) {
918 xor_srcs
[src_idx
] = alloc_page(GFP_KERNEL
);
919 if (!xor_srcs
[src_idx
]) {
921 __free_page(xor_srcs
[src_idx
]);
926 dest
= alloc_page(GFP_KERNEL
);
929 __free_page(xor_srcs
[src_idx
]);
933 /* Fill in src buffers */
934 for (src_idx
= 0; src_idx
< MV_XOR_NUM_SRC_TEST
; src_idx
++) {
935 u8
*ptr
= page_address(xor_srcs
[src_idx
]);
936 for (i
= 0; i
< PAGE_SIZE
; i
++)
937 ptr
[i
] = (1 << src_idx
);
940 for (src_idx
= 0; src_idx
< MV_XOR_NUM_SRC_TEST
; src_idx
++)
941 cmp_byte
^= (u8
) (1 << src_idx
);
943 cmp_word
= (cmp_byte
<< 24) | (cmp_byte
<< 16) |
944 (cmp_byte
<< 8) | cmp_byte
;
946 memset(page_address(dest
), 0, PAGE_SIZE
);
948 dma_chan
= &mv_chan
->dmachan
;
949 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
955 dest_dma
= dma_map_page(dma_chan
->device
->dev
, dest
, 0, PAGE_SIZE
,
958 for (i
= 0; i
< MV_XOR_NUM_SRC_TEST
; i
++)
959 dma_srcs
[i
] = dma_map_page(dma_chan
->device
->dev
, xor_srcs
[i
],
960 0, PAGE_SIZE
, DMA_TO_DEVICE
);
962 tx
= mv_xor_prep_dma_xor(dma_chan
, dest_dma
, dma_srcs
,
963 MV_XOR_NUM_SRC_TEST
, PAGE_SIZE
, 0);
965 cookie
= mv_xor_tx_submit(tx
);
966 mv_xor_issue_pending(dma_chan
);
970 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
972 dev_err(dma_chan
->device
->dev
,
973 "Self-test xor timed out, disabling\n");
978 dma_sync_single_for_cpu(dma_chan
->device
->dev
, dest_dma
,
979 PAGE_SIZE
, DMA_FROM_DEVICE
);
980 for (i
= 0; i
< (PAGE_SIZE
/ sizeof(u32
)); i
++) {
981 u32
*ptr
= page_address(dest
);
982 if (ptr
[i
] != cmp_word
) {
983 dev_err(dma_chan
->device
->dev
,
984 "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
985 i
, ptr
[i
], cmp_word
);
992 mv_xor_free_chan_resources(dma_chan
);
994 src_idx
= MV_XOR_NUM_SRC_TEST
;
996 __free_page(xor_srcs
[src_idx
]);
1001 /* This driver does not implement any of the optional DMA operations. */
1003 mv_xor_control(struct dma_chan
*chan
, enum dma_ctrl_cmd cmd
,
1009 static int mv_xor_channel_remove(struct mv_xor_chan
*mv_chan
)
1011 struct dma_chan
*chan
, *_chan
;
1012 struct device
*dev
= mv_chan
->dmadev
.dev
;
1014 dma_async_device_unregister(&mv_chan
->dmadev
);
1016 dma_free_coherent(dev
, MV_XOR_POOL_SIZE
,
1017 mv_chan
->dma_desc_pool_virt
, mv_chan
->dma_desc_pool
);
1019 list_for_each_entry_safe(chan
, _chan
, &mv_chan
->dmadev
.channels
,
1021 list_del(&chan
->device_node
);
1024 free_irq(mv_chan
->irq
, mv_chan
);
1029 static struct mv_xor_chan
*
1030 mv_xor_channel_add(struct mv_xor_device
*xordev
,
1031 struct platform_device
*pdev
,
1032 int idx
, dma_cap_mask_t cap_mask
, int irq
)
1035 struct mv_xor_chan
*mv_chan
;
1036 struct dma_device
*dma_dev
;
1038 mv_chan
= devm_kzalloc(&pdev
->dev
, sizeof(*mv_chan
), GFP_KERNEL
);
1040 return ERR_PTR(-ENOMEM
);
1045 dma_dev
= &mv_chan
->dmadev
;
1047 /* allocate coherent memory for hardware descriptors
1048 * note: writecombine gives slightly better performance, but
1049 * requires that we explicitly flush the writes
1051 mv_chan
->dma_desc_pool_virt
=
1052 dma_alloc_writecombine(&pdev
->dev
, MV_XOR_POOL_SIZE
,
1053 &mv_chan
->dma_desc_pool
, GFP_KERNEL
);
1054 if (!mv_chan
->dma_desc_pool_virt
)
1055 return ERR_PTR(-ENOMEM
);
1057 /* discover transaction capabilites from the platform data */
1058 dma_dev
->cap_mask
= cap_mask
;
1060 INIT_LIST_HEAD(&dma_dev
->channels
);
1062 /* set base routines */
1063 dma_dev
->device_alloc_chan_resources
= mv_xor_alloc_chan_resources
;
1064 dma_dev
->device_free_chan_resources
= mv_xor_free_chan_resources
;
1065 dma_dev
->device_tx_status
= mv_xor_status
;
1066 dma_dev
->device_issue_pending
= mv_xor_issue_pending
;
1067 dma_dev
->device_control
= mv_xor_control
;
1068 dma_dev
->dev
= &pdev
->dev
;
1070 /* set prep routines based on capability */
1071 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
))
1072 dma_dev
->device_prep_dma_memcpy
= mv_xor_prep_dma_memcpy
;
1073 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1074 dma_dev
->max_xor
= 8;
1075 dma_dev
->device_prep_dma_xor
= mv_xor_prep_dma_xor
;
1078 mv_chan
->mmr_base
= xordev
->xor_base
;
1079 if (!mv_chan
->mmr_base
) {
1083 tasklet_init(&mv_chan
->irq_tasklet
, mv_xor_tasklet
, (unsigned long)
1086 /* clear errors before enabling interrupts */
1087 mv_xor_device_clear_err_status(mv_chan
);
1089 ret
= request_irq(mv_chan
->irq
, mv_xor_interrupt_handler
,
1090 0, dev_name(&pdev
->dev
), mv_chan
);
1094 mv_chan_unmask_interrupts(mv_chan
);
1096 mv_set_mode(mv_chan
, DMA_MEMCPY
);
1098 spin_lock_init(&mv_chan
->lock
);
1099 INIT_LIST_HEAD(&mv_chan
->chain
);
1100 INIT_LIST_HEAD(&mv_chan
->completed_slots
);
1101 INIT_LIST_HEAD(&mv_chan
->all_slots
);
1102 mv_chan
->dmachan
.device
= dma_dev
;
1103 dma_cookie_init(&mv_chan
->dmachan
);
1105 list_add_tail(&mv_chan
->dmachan
.device_node
, &dma_dev
->channels
);
1107 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
)) {
1108 ret
= mv_xor_memcpy_self_test(mv_chan
);
1109 dev_dbg(&pdev
->dev
, "memcpy self test returned %d\n", ret
);
1114 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1115 ret
= mv_xor_xor_self_test(mv_chan
);
1116 dev_dbg(&pdev
->dev
, "xor self test returned %d\n", ret
);
1121 dev_info(&pdev
->dev
, "Marvell XOR: ( %s%s%s)\n",
1122 dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
) ? "xor " : "",
1123 dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
) ? "cpy " : "",
1124 dma_has_cap(DMA_INTERRUPT
, dma_dev
->cap_mask
) ? "intr " : "");
1126 dma_async_device_register(dma_dev
);
1130 free_irq(mv_chan
->irq
, mv_chan
);
1132 dma_free_coherent(&pdev
->dev
, MV_XOR_POOL_SIZE
,
1133 mv_chan
->dma_desc_pool_virt
, mv_chan
->dma_desc_pool
);
1134 return ERR_PTR(ret
);
1138 mv_xor_conf_mbus_windows(struct mv_xor_device
*xordev
,
1139 const struct mbus_dram_target_info
*dram
)
1141 void __iomem
*base
= xordev
->xor_base
;
1145 for (i
= 0; i
< 8; i
++) {
1146 writel(0, base
+ WINDOW_BASE(i
));
1147 writel(0, base
+ WINDOW_SIZE(i
));
1149 writel(0, base
+ WINDOW_REMAP_HIGH(i
));
1152 for (i
= 0; i
< dram
->num_cs
; i
++) {
1153 const struct mbus_dram_window
*cs
= dram
->cs
+ i
;
1155 writel((cs
->base
& 0xffff0000) |
1156 (cs
->mbus_attr
<< 8) |
1157 dram
->mbus_dram_target_id
, base
+ WINDOW_BASE(i
));
1158 writel((cs
->size
- 1) & 0xffff0000, base
+ WINDOW_SIZE(i
));
1160 win_enable
|= (1 << i
);
1161 win_enable
|= 3 << (16 + (2 * i
));
1164 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(0));
1165 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(1));
1166 writel(0, base
+ WINDOW_OVERRIDE_CTRL(0));
1167 writel(0, base
+ WINDOW_OVERRIDE_CTRL(1));
1170 static int mv_xor_probe(struct platform_device
*pdev
)
1172 const struct mbus_dram_target_info
*dram
;
1173 struct mv_xor_device
*xordev
;
1174 struct mv_xor_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1175 struct resource
*res
;
1178 dev_notice(&pdev
->dev
, "Marvell shared XOR driver\n");
1180 xordev
= devm_kzalloc(&pdev
->dev
, sizeof(*xordev
), GFP_KERNEL
);
1184 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1188 xordev
->xor_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1189 resource_size(res
));
1190 if (!xordev
->xor_base
)
1193 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1197 xordev
->xor_high_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1198 resource_size(res
));
1199 if (!xordev
->xor_high_base
)
1202 platform_set_drvdata(pdev
, xordev
);
1205 * (Re-)program MBUS remapping windows if we are asked to.
1207 dram
= mv_mbus_dram_info();
1209 mv_xor_conf_mbus_windows(xordev
, dram
);
1211 /* Not all platforms can gate the clock, so it is not
1212 * an error if the clock does not exists.
1214 xordev
->clk
= clk_get(&pdev
->dev
, NULL
);
1215 if (!IS_ERR(xordev
->clk
))
1216 clk_prepare_enable(xordev
->clk
);
1218 if (pdev
->dev
.of_node
) {
1219 struct device_node
*np
;
1222 for_each_child_of_node(pdev
->dev
.of_node
, np
) {
1223 dma_cap_mask_t cap_mask
;
1226 dma_cap_zero(cap_mask
);
1227 if (of_property_read_bool(np
, "dmacap,memcpy"))
1228 dma_cap_set(DMA_MEMCPY
, cap_mask
);
1229 if (of_property_read_bool(np
, "dmacap,xor"))
1230 dma_cap_set(DMA_XOR
, cap_mask
);
1231 if (of_property_read_bool(np
, "dmacap,interrupt"))
1232 dma_cap_set(DMA_INTERRUPT
, cap_mask
);
1234 irq
= irq_of_parse_and_map(np
, 0);
1237 goto err_channel_add
;
1240 xordev
->channels
[i
] =
1241 mv_xor_channel_add(xordev
, pdev
, i
,
1243 if (IS_ERR(xordev
->channels
[i
])) {
1244 ret
= PTR_ERR(xordev
->channels
[i
]);
1245 xordev
->channels
[i
] = NULL
;
1246 irq_dispose_mapping(irq
);
1247 goto err_channel_add
;
1252 } else if (pdata
&& pdata
->channels
) {
1253 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++) {
1254 struct mv_xor_channel_data
*cd
;
1257 cd
= &pdata
->channels
[i
];
1260 goto err_channel_add
;
1263 irq
= platform_get_irq(pdev
, i
);
1266 goto err_channel_add
;
1269 xordev
->channels
[i
] =
1270 mv_xor_channel_add(xordev
, pdev
, i
,
1272 if (IS_ERR(xordev
->channels
[i
])) {
1273 ret
= PTR_ERR(xordev
->channels
[i
]);
1274 goto err_channel_add
;
1282 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++)
1283 if (xordev
->channels
[i
]) {
1284 mv_xor_channel_remove(xordev
->channels
[i
]);
1285 if (pdev
->dev
.of_node
)
1286 irq_dispose_mapping(xordev
->channels
[i
]->irq
);
1289 if (!IS_ERR(xordev
->clk
)) {
1290 clk_disable_unprepare(xordev
->clk
);
1291 clk_put(xordev
->clk
);
1297 static int mv_xor_remove(struct platform_device
*pdev
)
1299 struct mv_xor_device
*xordev
= platform_get_drvdata(pdev
);
1302 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++) {
1303 if (xordev
->channels
[i
])
1304 mv_xor_channel_remove(xordev
->channels
[i
]);
1307 if (!IS_ERR(xordev
->clk
)) {
1308 clk_disable_unprepare(xordev
->clk
);
1309 clk_put(xordev
->clk
);
1316 static struct of_device_id mv_xor_dt_ids
[] = {
1317 { .compatible
= "marvell,orion-xor", },
1320 MODULE_DEVICE_TABLE(of
, mv_xor_dt_ids
);
1323 static struct platform_driver mv_xor_driver
= {
1324 .probe
= mv_xor_probe
,
1325 .remove
= mv_xor_remove
,
1327 .owner
= THIS_MODULE
,
1328 .name
= MV_XOR_NAME
,
1329 .of_match_table
= of_match_ptr(mv_xor_dt_ids
),
1334 static int __init
mv_xor_init(void)
1336 return platform_driver_register(&mv_xor_driver
);
1338 module_init(mv_xor_init
);
1340 /* it's currently unsafe to unload this module */
1342 static void __exit
mv_xor_exit(void)
1344 platform_driver_unregister(&mv_xor_driver
);
1348 module_exit(mv_xor_exit
);
1351 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1352 MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1353 MODULE_LICENSE("GPL");