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
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/spinlock.h>
20 #include <linux/interrupt.h>
21 #include <linux/of_device.h>
22 #include <linux/platform_device.h>
23 #include <linux/memory.h>
24 #include <linux/clk.h>
26 #include <linux/of_irq.h>
27 #include <linux/irqdomain.h>
28 #include <linux/cpumask.h>
29 #include <linux/platform_data/dma-mv_xor.h>
31 #include "dmaengine.h"
39 static void mv_xor_issue_pending(struct dma_chan
*chan
);
41 #define to_mv_xor_chan(chan) \
42 container_of(chan, struct mv_xor_chan, dmachan)
44 #define to_mv_xor_slot(tx) \
45 container_of(tx, struct mv_xor_desc_slot, async_tx)
47 #define mv_chan_to_devp(chan) \
50 static void mv_desc_init(struct mv_xor_desc_slot
*desc
,
51 dma_addr_t addr
, u32 byte_count
,
52 enum dma_ctrl_flags flags
)
54 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
56 hw_desc
->status
= XOR_DESC_DMA_OWNED
;
57 hw_desc
->phy_next_desc
= 0;
58 /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */
59 hw_desc
->desc_command
= (flags
& DMA_PREP_INTERRUPT
) ?
60 XOR_DESC_EOD_INT_EN
: 0;
61 hw_desc
->phy_dest_addr
= addr
;
62 hw_desc
->byte_count
= byte_count
;
65 static void mv_desc_set_mode(struct mv_xor_desc_slot
*desc
)
67 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
72 hw_desc
->desc_command
|= XOR_DESC_OPERATION_XOR
;
75 hw_desc
->desc_command
|= XOR_DESC_OPERATION_MEMCPY
;
83 static void mv_desc_set_next_desc(struct mv_xor_desc_slot
*desc
,
86 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
87 BUG_ON(hw_desc
->phy_next_desc
);
88 hw_desc
->phy_next_desc
= next_desc_addr
;
91 static void mv_desc_set_src_addr(struct mv_xor_desc_slot
*desc
,
92 int index
, dma_addr_t addr
)
94 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
95 hw_desc
->phy_src_addr
[mv_phy_src_idx(index
)] = addr
;
96 if (desc
->type
== DMA_XOR
)
97 hw_desc
->desc_command
|= (1 << index
);
100 static u32
mv_chan_get_current_desc(struct mv_xor_chan
*chan
)
102 return readl_relaxed(XOR_CURR_DESC(chan
));
105 static void mv_chan_set_next_descriptor(struct mv_xor_chan
*chan
,
108 writel_relaxed(next_desc_addr
, XOR_NEXT_DESC(chan
));
111 static void mv_chan_unmask_interrupts(struct mv_xor_chan
*chan
)
113 u32 val
= readl_relaxed(XOR_INTR_MASK(chan
));
114 val
|= XOR_INTR_MASK_VALUE
<< (chan
->idx
* 16);
115 writel_relaxed(val
, XOR_INTR_MASK(chan
));
118 static u32
mv_chan_get_intr_cause(struct mv_xor_chan
*chan
)
120 u32 intr_cause
= readl_relaxed(XOR_INTR_CAUSE(chan
));
121 intr_cause
= (intr_cause
>> (chan
->idx
* 16)) & 0xFFFF;
125 static void mv_chan_clear_eoc_cause(struct mv_xor_chan
*chan
)
129 val
= XOR_INT_END_OF_DESC
| XOR_INT_END_OF_CHAIN
| XOR_INT_STOPPED
;
130 val
= ~(val
<< (chan
->idx
* 16));
131 dev_dbg(mv_chan_to_devp(chan
), "%s, val 0x%08x\n", __func__
, val
);
132 writel_relaxed(val
, XOR_INTR_CAUSE(chan
));
135 static void mv_chan_clear_err_status(struct mv_xor_chan
*chan
)
137 u32 val
= 0xFFFF0000 >> (chan
->idx
* 16);
138 writel_relaxed(val
, XOR_INTR_CAUSE(chan
));
141 static void mv_chan_set_mode(struct mv_xor_chan
*chan
,
142 enum dma_transaction_type type
)
145 u32 config
= readl_relaxed(XOR_CONFIG(chan
));
149 op_mode
= XOR_OPERATION_MODE_XOR
;
152 op_mode
= XOR_OPERATION_MODE_MEMCPY
;
155 dev_err(mv_chan_to_devp(chan
),
156 "error: unsupported operation %d\n",
165 #if defined(__BIG_ENDIAN)
166 config
|= XOR_DESCRIPTOR_SWAP
;
168 config
&= ~XOR_DESCRIPTOR_SWAP
;
171 writel_relaxed(config
, XOR_CONFIG(chan
));
172 chan
->current_type
= type
;
175 static void mv_chan_set_mode_to_desc(struct mv_xor_chan
*chan
)
178 u32 config
= readl_relaxed(XOR_CONFIG(chan
));
180 op_mode
= XOR_OPERATION_MODE_IN_DESC
;
185 #if defined(__BIG_ENDIAN)
186 config
|= XOR_DESCRIPTOR_SWAP
;
188 config
&= ~XOR_DESCRIPTOR_SWAP
;
191 writel_relaxed(config
, XOR_CONFIG(chan
));
194 static void mv_chan_activate(struct mv_xor_chan
*chan
)
196 dev_dbg(mv_chan_to_devp(chan
), " activate chan.\n");
198 /* writel ensures all descriptors are flushed before activation */
199 writel(BIT(0), XOR_ACTIVATION(chan
));
202 static char mv_chan_is_busy(struct mv_xor_chan
*chan
)
204 u32 state
= readl_relaxed(XOR_ACTIVATION(chan
));
206 state
= (state
>> 4) & 0x3;
208 return (state
== 1) ? 1 : 0;
212 * mv_chan_start_new_chain - program the engine to operate on new
213 * chain headed by sw_desc
214 * Caller must hold &mv_chan->lock while calling this function
216 static void mv_chan_start_new_chain(struct mv_xor_chan
*mv_chan
,
217 struct mv_xor_desc_slot
*sw_desc
)
219 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d: sw_desc %p\n",
220 __func__
, __LINE__
, sw_desc
);
222 /* set the hardware chain */
223 mv_chan_set_next_descriptor(mv_chan
, sw_desc
->async_tx
.phys
);
226 mv_xor_issue_pending(&mv_chan
->dmachan
);
230 mv_desc_run_tx_complete_actions(struct mv_xor_desc_slot
*desc
,
231 struct mv_xor_chan
*mv_chan
,
234 BUG_ON(desc
->async_tx
.cookie
< 0);
236 if (desc
->async_tx
.cookie
> 0) {
237 cookie
= desc
->async_tx
.cookie
;
239 /* call the callback (must not sleep or submit new
240 * operations to this channel)
242 if (desc
->async_tx
.callback
)
243 desc
->async_tx
.callback(
244 desc
->async_tx
.callback_param
);
246 dma_descriptor_unmap(&desc
->async_tx
);
249 /* run dependent operations */
250 dma_run_dependencies(&desc
->async_tx
);
256 mv_chan_clean_completed_slots(struct mv_xor_chan
*mv_chan
)
258 struct mv_xor_desc_slot
*iter
, *_iter
;
260 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d\n", __func__
, __LINE__
);
261 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
264 if (async_tx_test_ack(&iter
->async_tx
))
265 list_move_tail(&iter
->node
, &mv_chan
->free_slots
);
271 mv_desc_clean_slot(struct mv_xor_desc_slot
*desc
,
272 struct mv_xor_chan
*mv_chan
)
274 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d: desc %p flags %d\n",
275 __func__
, __LINE__
, desc
, desc
->async_tx
.flags
);
277 /* the client is allowed to attach dependent operations
280 if (!async_tx_test_ack(&desc
->async_tx
))
281 /* move this slot to the completed_slots */
282 list_move_tail(&desc
->node
, &mv_chan
->completed_slots
);
284 list_move_tail(&desc
->node
, &mv_chan
->free_slots
);
289 /* This function must be called with the mv_xor_chan spinlock held */
290 static void mv_chan_slot_cleanup(struct mv_xor_chan
*mv_chan
)
292 struct mv_xor_desc_slot
*iter
, *_iter
;
293 dma_cookie_t cookie
= 0;
294 int busy
= mv_chan_is_busy(mv_chan
);
295 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
296 int current_cleaned
= 0;
297 struct mv_xor_desc
*hw_desc
;
299 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d\n", __func__
, __LINE__
);
300 dev_dbg(mv_chan_to_devp(mv_chan
), "current_desc %x\n", current_desc
);
301 mv_chan_clean_completed_slots(mv_chan
);
303 /* free completed slots from the chain starting with
304 * the oldest descriptor
307 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
310 /* clean finished descriptors */
311 hw_desc
= iter
->hw_desc
;
312 if (hw_desc
->status
& XOR_DESC_SUCCESS
) {
313 cookie
= mv_desc_run_tx_complete_actions(iter
, mv_chan
,
316 /* done processing desc, clean slot */
317 mv_desc_clean_slot(iter
, mv_chan
);
319 /* break if we did cleaned the current */
320 if (iter
->async_tx
.phys
== current_desc
) {
325 if (iter
->async_tx
.phys
== current_desc
) {
332 if ((busy
== 0) && !list_empty(&mv_chan
->chain
)) {
333 if (current_cleaned
) {
335 * current descriptor cleaned and removed, run
338 iter
= list_entry(mv_chan
->chain
.next
,
339 struct mv_xor_desc_slot
,
341 mv_chan_start_new_chain(mv_chan
, iter
);
343 if (!list_is_last(&iter
->node
, &mv_chan
->chain
)) {
345 * descriptors are still waiting after
346 * current, trigger them
348 iter
= list_entry(iter
->node
.next
,
349 struct mv_xor_desc_slot
,
351 mv_chan_start_new_chain(mv_chan
, iter
);
354 * some descriptors are still waiting
357 tasklet_schedule(&mv_chan
->irq_tasklet
);
363 mv_chan
->dmachan
.completed_cookie
= cookie
;
366 static void mv_xor_tasklet(unsigned long data
)
368 struct mv_xor_chan
*chan
= (struct mv_xor_chan
*) data
;
370 spin_lock_bh(&chan
->lock
);
371 mv_chan_slot_cleanup(chan
);
372 spin_unlock_bh(&chan
->lock
);
375 static struct mv_xor_desc_slot
*
376 mv_chan_alloc_slot(struct mv_xor_chan
*mv_chan
)
378 struct mv_xor_desc_slot
*iter
;
380 spin_lock_bh(&mv_chan
->lock
);
382 if (!list_empty(&mv_chan
->free_slots
)) {
383 iter
= list_first_entry(&mv_chan
->free_slots
,
384 struct mv_xor_desc_slot
,
387 list_move_tail(&iter
->node
, &mv_chan
->allocated_slots
);
389 spin_unlock_bh(&mv_chan
->lock
);
391 /* pre-ack descriptor */
392 async_tx_ack(&iter
->async_tx
);
393 iter
->async_tx
.cookie
= -EBUSY
;
399 spin_unlock_bh(&mv_chan
->lock
);
401 /* try to free some slots if the allocation fails */
402 tasklet_schedule(&mv_chan
->irq_tasklet
);
407 /************************ DMA engine API functions ****************************/
409 mv_xor_tx_submit(struct dma_async_tx_descriptor
*tx
)
411 struct mv_xor_desc_slot
*sw_desc
= to_mv_xor_slot(tx
);
412 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(tx
->chan
);
413 struct mv_xor_desc_slot
*old_chain_tail
;
415 int new_hw_chain
= 1;
417 dev_dbg(mv_chan_to_devp(mv_chan
),
418 "%s sw_desc %p: async_tx %p\n",
419 __func__
, sw_desc
, &sw_desc
->async_tx
);
421 spin_lock_bh(&mv_chan
->lock
);
422 cookie
= dma_cookie_assign(tx
);
424 if (list_empty(&mv_chan
->chain
))
425 list_move_tail(&sw_desc
->node
, &mv_chan
->chain
);
429 old_chain_tail
= list_entry(mv_chan
->chain
.prev
,
430 struct mv_xor_desc_slot
,
432 list_move_tail(&sw_desc
->node
, &mv_chan
->chain
);
434 dev_dbg(mv_chan_to_devp(mv_chan
), "Append to last desc %pa\n",
435 &old_chain_tail
->async_tx
.phys
);
437 /* fix up the hardware chain */
438 mv_desc_set_next_desc(old_chain_tail
, sw_desc
->async_tx
.phys
);
440 /* if the channel is not busy */
441 if (!mv_chan_is_busy(mv_chan
)) {
442 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
444 * and the curren desc is the end of the chain before
445 * the append, then we need to start the channel
447 if (current_desc
== old_chain_tail
->async_tx
.phys
)
453 mv_chan_start_new_chain(mv_chan
, sw_desc
);
455 spin_unlock_bh(&mv_chan
->lock
);
460 /* returns the number of allocated descriptors */
461 static int mv_xor_alloc_chan_resources(struct dma_chan
*chan
)
466 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
467 struct mv_xor_desc_slot
*slot
= NULL
;
468 int num_descs_in_pool
= MV_XOR_POOL_SIZE
/MV_XOR_SLOT_SIZE
;
470 /* Allocate descriptor slots */
471 idx
= mv_chan
->slots_allocated
;
472 while (idx
< num_descs_in_pool
) {
473 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
475 dev_info(mv_chan_to_devp(mv_chan
),
476 "channel only initialized %d descriptor slots",
480 virt_desc
= mv_chan
->dma_desc_pool_virt
;
481 slot
->hw_desc
= virt_desc
+ idx
* MV_XOR_SLOT_SIZE
;
483 dma_async_tx_descriptor_init(&slot
->async_tx
, chan
);
484 slot
->async_tx
.tx_submit
= mv_xor_tx_submit
;
485 INIT_LIST_HEAD(&slot
->node
);
486 dma_desc
= mv_chan
->dma_desc_pool
;
487 slot
->async_tx
.phys
= dma_desc
+ idx
* MV_XOR_SLOT_SIZE
;
490 spin_lock_bh(&mv_chan
->lock
);
491 mv_chan
->slots_allocated
= idx
;
492 list_add_tail(&slot
->node
, &mv_chan
->free_slots
);
493 spin_unlock_bh(&mv_chan
->lock
);
496 dev_dbg(mv_chan_to_devp(mv_chan
),
497 "allocated %d descriptor slots\n",
498 mv_chan
->slots_allocated
);
500 return mv_chan
->slots_allocated
? : -ENOMEM
;
503 static struct dma_async_tx_descriptor
*
504 mv_xor_prep_dma_xor(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t
*src
,
505 unsigned int src_cnt
, size_t len
, unsigned long flags
)
507 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
508 struct mv_xor_desc_slot
*sw_desc
;
510 if (unlikely(len
< MV_XOR_MIN_BYTE_COUNT
))
513 BUG_ON(len
> MV_XOR_MAX_BYTE_COUNT
);
515 dev_dbg(mv_chan_to_devp(mv_chan
),
516 "%s src_cnt: %d len: %u dest %pad flags: %ld\n",
517 __func__
, src_cnt
, len
, &dest
, flags
);
519 sw_desc
= mv_chan_alloc_slot(mv_chan
);
521 sw_desc
->type
= DMA_XOR
;
522 sw_desc
->async_tx
.flags
= flags
;
523 mv_desc_init(sw_desc
, dest
, len
, flags
);
524 if (mv_chan
->op_in_desc
== XOR_MODE_IN_DESC
)
525 mv_desc_set_mode(sw_desc
);
527 mv_desc_set_src_addr(sw_desc
, src_cnt
, src
[src_cnt
]);
530 dev_dbg(mv_chan_to_devp(mv_chan
),
531 "%s sw_desc %p async_tx %p \n",
532 __func__
, sw_desc
, &sw_desc
->async_tx
);
533 return sw_desc
? &sw_desc
->async_tx
: NULL
;
536 static struct dma_async_tx_descriptor
*
537 mv_xor_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
538 size_t len
, unsigned long flags
)
541 * A MEMCPY operation is identical to an XOR operation with only
542 * a single source address.
544 return mv_xor_prep_dma_xor(chan
, dest
, &src
, 1, len
, flags
);
547 static struct dma_async_tx_descriptor
*
548 mv_xor_prep_dma_interrupt(struct dma_chan
*chan
, unsigned long flags
)
550 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
551 dma_addr_t src
, dest
;
554 src
= mv_chan
->dummy_src_addr
;
555 dest
= mv_chan
->dummy_dst_addr
;
556 len
= MV_XOR_MIN_BYTE_COUNT
;
559 * We implement the DMA_INTERRUPT operation as a minimum sized
560 * XOR operation with a single dummy source address.
562 return mv_xor_prep_dma_xor(chan
, dest
, &src
, 1, len
, flags
);
565 static void mv_xor_free_chan_resources(struct dma_chan
*chan
)
567 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
568 struct mv_xor_desc_slot
*iter
, *_iter
;
569 int in_use_descs
= 0;
571 spin_lock_bh(&mv_chan
->lock
);
573 mv_chan_slot_cleanup(mv_chan
);
575 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
578 list_move_tail(&iter
->node
, &mv_chan
->free_slots
);
580 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
583 list_move_tail(&iter
->node
, &mv_chan
->free_slots
);
585 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->allocated_slots
,
588 list_move_tail(&iter
->node
, &mv_chan
->free_slots
);
590 list_for_each_entry_safe_reverse(
591 iter
, _iter
, &mv_chan
->free_slots
, node
) {
592 list_del(&iter
->node
);
594 mv_chan
->slots_allocated
--;
597 dev_dbg(mv_chan_to_devp(mv_chan
), "%s slots_allocated %d\n",
598 __func__
, mv_chan
->slots_allocated
);
599 spin_unlock_bh(&mv_chan
->lock
);
602 dev_err(mv_chan_to_devp(mv_chan
),
603 "freeing %d in use descriptors!\n", in_use_descs
);
607 * mv_xor_status - poll the status of an XOR transaction
608 * @chan: XOR channel handle
609 * @cookie: XOR transaction identifier
610 * @txstate: XOR transactions state holder (or NULL)
612 static enum dma_status
mv_xor_status(struct dma_chan
*chan
,
614 struct dma_tx_state
*txstate
)
616 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
619 ret
= dma_cookie_status(chan
, cookie
, txstate
);
620 if (ret
== DMA_COMPLETE
)
623 spin_lock_bh(&mv_chan
->lock
);
624 mv_chan_slot_cleanup(mv_chan
);
625 spin_unlock_bh(&mv_chan
->lock
);
627 return dma_cookie_status(chan
, cookie
, txstate
);
630 static void mv_chan_dump_regs(struct mv_xor_chan
*chan
)
634 val
= readl_relaxed(XOR_CONFIG(chan
));
635 dev_err(mv_chan_to_devp(chan
), "config 0x%08x\n", val
);
637 val
= readl_relaxed(XOR_ACTIVATION(chan
));
638 dev_err(mv_chan_to_devp(chan
), "activation 0x%08x\n", val
);
640 val
= readl_relaxed(XOR_INTR_CAUSE(chan
));
641 dev_err(mv_chan_to_devp(chan
), "intr cause 0x%08x\n", val
);
643 val
= readl_relaxed(XOR_INTR_MASK(chan
));
644 dev_err(mv_chan_to_devp(chan
), "intr mask 0x%08x\n", val
);
646 val
= readl_relaxed(XOR_ERROR_CAUSE(chan
));
647 dev_err(mv_chan_to_devp(chan
), "error cause 0x%08x\n", val
);
649 val
= readl_relaxed(XOR_ERROR_ADDR(chan
));
650 dev_err(mv_chan_to_devp(chan
), "error addr 0x%08x\n", val
);
653 static void mv_chan_err_interrupt_handler(struct mv_xor_chan
*chan
,
656 if (intr_cause
& XOR_INT_ERR_DECODE
) {
657 dev_dbg(mv_chan_to_devp(chan
), "ignoring address decode error\n");
661 dev_err(mv_chan_to_devp(chan
), "error on chan %d. intr cause 0x%08x\n",
662 chan
->idx
, intr_cause
);
664 mv_chan_dump_regs(chan
);
668 static irqreturn_t
mv_xor_interrupt_handler(int irq
, void *data
)
670 struct mv_xor_chan
*chan
= data
;
671 u32 intr_cause
= mv_chan_get_intr_cause(chan
);
673 dev_dbg(mv_chan_to_devp(chan
), "intr cause %x\n", intr_cause
);
675 if (intr_cause
& XOR_INTR_ERRORS
)
676 mv_chan_err_interrupt_handler(chan
, intr_cause
);
678 tasklet_schedule(&chan
->irq_tasklet
);
680 mv_chan_clear_eoc_cause(chan
);
685 static void mv_xor_issue_pending(struct dma_chan
*chan
)
687 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
689 if (mv_chan
->pending
>= MV_XOR_THRESHOLD
) {
690 mv_chan
->pending
= 0;
691 mv_chan_activate(mv_chan
);
696 * Perform a transaction to verify the HW works.
699 static int mv_chan_memcpy_self_test(struct mv_xor_chan
*mv_chan
)
703 dma_addr_t src_dma
, dest_dma
;
704 struct dma_chan
*dma_chan
;
706 struct dma_async_tx_descriptor
*tx
;
707 struct dmaengine_unmap_data
*unmap
;
710 src
= kmalloc(sizeof(u8
) * PAGE_SIZE
, GFP_KERNEL
);
714 dest
= kzalloc(sizeof(u8
) * PAGE_SIZE
, GFP_KERNEL
);
720 /* Fill in src buffer */
721 for (i
= 0; i
< PAGE_SIZE
; i
++)
722 ((u8
*) src
)[i
] = (u8
)i
;
724 dma_chan
= &mv_chan
->dmachan
;
725 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
730 unmap
= dmaengine_get_unmap_data(dma_chan
->device
->dev
, 2, GFP_KERNEL
);
736 src_dma
= dma_map_page(dma_chan
->device
->dev
, virt_to_page(src
), 0,
737 PAGE_SIZE
, DMA_TO_DEVICE
);
738 unmap
->addr
[0] = src_dma
;
740 ret
= dma_mapping_error(dma_chan
->device
->dev
, src_dma
);
747 dest_dma
= dma_map_page(dma_chan
->device
->dev
, virt_to_page(dest
), 0,
748 PAGE_SIZE
, DMA_FROM_DEVICE
);
749 unmap
->addr
[1] = dest_dma
;
751 ret
= dma_mapping_error(dma_chan
->device
->dev
, dest_dma
);
757 unmap
->len
= PAGE_SIZE
;
759 tx
= mv_xor_prep_dma_memcpy(dma_chan
, dest_dma
, src_dma
,
762 dev_err(dma_chan
->device
->dev
,
763 "Self-test cannot prepare operation, disabling\n");
768 cookie
= mv_xor_tx_submit(tx
);
769 if (dma_submit_error(cookie
)) {
770 dev_err(dma_chan
->device
->dev
,
771 "Self-test submit error, disabling\n");
776 mv_xor_issue_pending(dma_chan
);
780 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
782 dev_err(dma_chan
->device
->dev
,
783 "Self-test copy timed out, disabling\n");
788 dma_sync_single_for_cpu(dma_chan
->device
->dev
, dest_dma
,
789 PAGE_SIZE
, DMA_FROM_DEVICE
);
790 if (memcmp(src
, dest
, PAGE_SIZE
)) {
791 dev_err(dma_chan
->device
->dev
,
792 "Self-test copy failed compare, disabling\n");
798 dmaengine_unmap_put(unmap
);
799 mv_xor_free_chan_resources(dma_chan
);
806 #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
808 mv_chan_xor_self_test(struct mv_xor_chan
*mv_chan
)
812 struct page
*xor_srcs
[MV_XOR_NUM_SRC_TEST
];
813 dma_addr_t dma_srcs
[MV_XOR_NUM_SRC_TEST
];
815 struct dma_async_tx_descriptor
*tx
;
816 struct dmaengine_unmap_data
*unmap
;
817 struct dma_chan
*dma_chan
;
822 int src_count
= MV_XOR_NUM_SRC_TEST
;
824 for (src_idx
= 0; src_idx
< src_count
; src_idx
++) {
825 xor_srcs
[src_idx
] = alloc_page(GFP_KERNEL
);
826 if (!xor_srcs
[src_idx
]) {
828 __free_page(xor_srcs
[src_idx
]);
833 dest
= alloc_page(GFP_KERNEL
);
836 __free_page(xor_srcs
[src_idx
]);
840 /* Fill in src buffers */
841 for (src_idx
= 0; src_idx
< src_count
; src_idx
++) {
842 u8
*ptr
= page_address(xor_srcs
[src_idx
]);
843 for (i
= 0; i
< PAGE_SIZE
; i
++)
844 ptr
[i
] = (1 << src_idx
);
847 for (src_idx
= 0; src_idx
< src_count
; src_idx
++)
848 cmp_byte
^= (u8
) (1 << src_idx
);
850 cmp_word
= (cmp_byte
<< 24) | (cmp_byte
<< 16) |
851 (cmp_byte
<< 8) | cmp_byte
;
853 memset(page_address(dest
), 0, PAGE_SIZE
);
855 dma_chan
= &mv_chan
->dmachan
;
856 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
861 unmap
= dmaengine_get_unmap_data(dma_chan
->device
->dev
, src_count
+ 1,
869 for (i
= 0; i
< src_count
; i
++) {
870 unmap
->addr
[i
] = dma_map_page(dma_chan
->device
->dev
, xor_srcs
[i
],
871 0, PAGE_SIZE
, DMA_TO_DEVICE
);
872 dma_srcs
[i
] = unmap
->addr
[i
];
873 ret
= dma_mapping_error(dma_chan
->device
->dev
, unmap
->addr
[i
]);
881 unmap
->addr
[src_count
] = dma_map_page(dma_chan
->device
->dev
, dest
, 0, PAGE_SIZE
,
883 dest_dma
= unmap
->addr
[src_count
];
884 ret
= dma_mapping_error(dma_chan
->device
->dev
, unmap
->addr
[src_count
]);
890 unmap
->len
= PAGE_SIZE
;
892 tx
= mv_xor_prep_dma_xor(dma_chan
, dest_dma
, dma_srcs
,
893 src_count
, PAGE_SIZE
, 0);
895 dev_err(dma_chan
->device
->dev
,
896 "Self-test cannot prepare operation, disabling\n");
901 cookie
= mv_xor_tx_submit(tx
);
902 if (dma_submit_error(cookie
)) {
903 dev_err(dma_chan
->device
->dev
,
904 "Self-test submit error, disabling\n");
909 mv_xor_issue_pending(dma_chan
);
913 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
915 dev_err(dma_chan
->device
->dev
,
916 "Self-test xor timed out, disabling\n");
921 dma_sync_single_for_cpu(dma_chan
->device
->dev
, dest_dma
,
922 PAGE_SIZE
, DMA_FROM_DEVICE
);
923 for (i
= 0; i
< (PAGE_SIZE
/ sizeof(u32
)); i
++) {
924 u32
*ptr
= page_address(dest
);
925 if (ptr
[i
] != cmp_word
) {
926 dev_err(dma_chan
->device
->dev
,
927 "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
928 i
, ptr
[i
], cmp_word
);
935 dmaengine_unmap_put(unmap
);
936 mv_xor_free_chan_resources(dma_chan
);
940 __free_page(xor_srcs
[src_idx
]);
945 static int mv_xor_channel_remove(struct mv_xor_chan
*mv_chan
)
947 struct dma_chan
*chan
, *_chan
;
948 struct device
*dev
= mv_chan
->dmadev
.dev
;
950 dma_async_device_unregister(&mv_chan
->dmadev
);
952 dma_free_coherent(dev
, MV_XOR_POOL_SIZE
,
953 mv_chan
->dma_desc_pool_virt
, mv_chan
->dma_desc_pool
);
954 dma_unmap_single(dev
, mv_chan
->dummy_src_addr
,
955 MV_XOR_MIN_BYTE_COUNT
, DMA_FROM_DEVICE
);
956 dma_unmap_single(dev
, mv_chan
->dummy_dst_addr
,
957 MV_XOR_MIN_BYTE_COUNT
, DMA_TO_DEVICE
);
959 list_for_each_entry_safe(chan
, _chan
, &mv_chan
->dmadev
.channels
,
961 list_del(&chan
->device_node
);
964 free_irq(mv_chan
->irq
, mv_chan
);
969 static struct mv_xor_chan
*
970 mv_xor_channel_add(struct mv_xor_device
*xordev
,
971 struct platform_device
*pdev
,
972 int idx
, dma_cap_mask_t cap_mask
, int irq
, int op_in_desc
)
975 struct mv_xor_chan
*mv_chan
;
976 struct dma_device
*dma_dev
;
978 mv_chan
= devm_kzalloc(&pdev
->dev
, sizeof(*mv_chan
), GFP_KERNEL
);
980 return ERR_PTR(-ENOMEM
);
984 mv_chan
->op_in_desc
= op_in_desc
;
986 dma_dev
= &mv_chan
->dmadev
;
989 * These source and destination dummy buffers are used to implement
990 * a DMA_INTERRUPT operation as a minimum-sized XOR operation.
991 * Hence, we only need to map the buffers at initialization-time.
993 mv_chan
->dummy_src_addr
= dma_map_single(dma_dev
->dev
,
994 mv_chan
->dummy_src
, MV_XOR_MIN_BYTE_COUNT
, DMA_FROM_DEVICE
);
995 mv_chan
->dummy_dst_addr
= dma_map_single(dma_dev
->dev
,
996 mv_chan
->dummy_dst
, MV_XOR_MIN_BYTE_COUNT
, DMA_TO_DEVICE
);
998 /* allocate coherent memory for hardware descriptors
999 * note: writecombine gives slightly better performance, but
1000 * requires that we explicitly flush the writes
1002 mv_chan
->dma_desc_pool_virt
=
1003 dma_alloc_writecombine(&pdev
->dev
, MV_XOR_POOL_SIZE
,
1004 &mv_chan
->dma_desc_pool
, GFP_KERNEL
);
1005 if (!mv_chan
->dma_desc_pool_virt
)
1006 return ERR_PTR(-ENOMEM
);
1008 /* discover transaction capabilites from the platform data */
1009 dma_dev
->cap_mask
= cap_mask
;
1011 INIT_LIST_HEAD(&dma_dev
->channels
);
1013 /* set base routines */
1014 dma_dev
->device_alloc_chan_resources
= mv_xor_alloc_chan_resources
;
1015 dma_dev
->device_free_chan_resources
= mv_xor_free_chan_resources
;
1016 dma_dev
->device_tx_status
= mv_xor_status
;
1017 dma_dev
->device_issue_pending
= mv_xor_issue_pending
;
1018 dma_dev
->dev
= &pdev
->dev
;
1020 /* set prep routines based on capability */
1021 if (dma_has_cap(DMA_INTERRUPT
, dma_dev
->cap_mask
))
1022 dma_dev
->device_prep_dma_interrupt
= mv_xor_prep_dma_interrupt
;
1023 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
))
1024 dma_dev
->device_prep_dma_memcpy
= mv_xor_prep_dma_memcpy
;
1025 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1026 dma_dev
->max_xor
= 8;
1027 dma_dev
->device_prep_dma_xor
= mv_xor_prep_dma_xor
;
1030 mv_chan
->mmr_base
= xordev
->xor_base
;
1031 mv_chan
->mmr_high_base
= xordev
->xor_high_base
;
1032 tasklet_init(&mv_chan
->irq_tasklet
, mv_xor_tasklet
, (unsigned long)
1035 /* clear errors before enabling interrupts */
1036 mv_chan_clear_err_status(mv_chan
);
1038 ret
= request_irq(mv_chan
->irq
, mv_xor_interrupt_handler
,
1039 0, dev_name(&pdev
->dev
), mv_chan
);
1043 mv_chan_unmask_interrupts(mv_chan
);
1045 if (mv_chan
->op_in_desc
== XOR_MODE_IN_DESC
)
1046 mv_chan_set_mode_to_desc(mv_chan
);
1048 mv_chan_set_mode(mv_chan
, DMA_XOR
);
1050 spin_lock_init(&mv_chan
->lock
);
1051 INIT_LIST_HEAD(&mv_chan
->chain
);
1052 INIT_LIST_HEAD(&mv_chan
->completed_slots
);
1053 INIT_LIST_HEAD(&mv_chan
->free_slots
);
1054 INIT_LIST_HEAD(&mv_chan
->allocated_slots
);
1055 mv_chan
->dmachan
.device
= dma_dev
;
1056 dma_cookie_init(&mv_chan
->dmachan
);
1058 list_add_tail(&mv_chan
->dmachan
.device_node
, &dma_dev
->channels
);
1060 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
)) {
1061 ret
= mv_chan_memcpy_self_test(mv_chan
);
1062 dev_dbg(&pdev
->dev
, "memcpy self test returned %d\n", ret
);
1067 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1068 ret
= mv_chan_xor_self_test(mv_chan
);
1069 dev_dbg(&pdev
->dev
, "xor self test returned %d\n", ret
);
1074 dev_info(&pdev
->dev
, "Marvell XOR (%s): ( %s%s%s)\n",
1075 mv_chan
->op_in_desc
? "Descriptor Mode" : "Registers Mode",
1076 dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
) ? "xor " : "",
1077 dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
) ? "cpy " : "",
1078 dma_has_cap(DMA_INTERRUPT
, dma_dev
->cap_mask
) ? "intr " : "");
1080 dma_async_device_register(dma_dev
);
1084 free_irq(mv_chan
->irq
, mv_chan
);
1086 dma_free_coherent(&pdev
->dev
, MV_XOR_POOL_SIZE
,
1087 mv_chan
->dma_desc_pool_virt
, mv_chan
->dma_desc_pool
);
1088 return ERR_PTR(ret
);
1092 mv_xor_conf_mbus_windows(struct mv_xor_device
*xordev
,
1093 const struct mbus_dram_target_info
*dram
)
1095 void __iomem
*base
= xordev
->xor_high_base
;
1099 for (i
= 0; i
< 8; i
++) {
1100 writel(0, base
+ WINDOW_BASE(i
));
1101 writel(0, base
+ WINDOW_SIZE(i
));
1103 writel(0, base
+ WINDOW_REMAP_HIGH(i
));
1106 for (i
= 0; i
< dram
->num_cs
; i
++) {
1107 const struct mbus_dram_window
*cs
= dram
->cs
+ i
;
1109 writel((cs
->base
& 0xffff0000) |
1110 (cs
->mbus_attr
<< 8) |
1111 dram
->mbus_dram_target_id
, base
+ WINDOW_BASE(i
));
1112 writel((cs
->size
- 1) & 0xffff0000, base
+ WINDOW_SIZE(i
));
1114 win_enable
|= (1 << i
);
1115 win_enable
|= 3 << (16 + (2 * i
));
1118 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(0));
1119 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(1));
1120 writel(0, base
+ WINDOW_OVERRIDE_CTRL(0));
1121 writel(0, base
+ WINDOW_OVERRIDE_CTRL(1));
1124 static const struct of_device_id mv_xor_dt_ids
[] = {
1125 { .compatible
= "marvell,orion-xor", .data
= (void *)XOR_MODE_IN_REG
},
1126 { .compatible
= "marvell,armada-380-xor", .data
= (void *)XOR_MODE_IN_DESC
},
1130 static unsigned int mv_xor_engine_count
;
1132 static int mv_xor_probe(struct platform_device
*pdev
)
1134 const struct mbus_dram_target_info
*dram
;
1135 struct mv_xor_device
*xordev
;
1136 struct mv_xor_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1137 struct resource
*res
;
1138 unsigned int max_engines
, max_channels
;
1142 dev_notice(&pdev
->dev
, "Marvell shared XOR driver\n");
1144 xordev
= devm_kzalloc(&pdev
->dev
, sizeof(*xordev
), GFP_KERNEL
);
1148 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1152 xordev
->xor_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1153 resource_size(res
));
1154 if (!xordev
->xor_base
)
1157 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1161 xordev
->xor_high_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1162 resource_size(res
));
1163 if (!xordev
->xor_high_base
)
1166 platform_set_drvdata(pdev
, xordev
);
1169 * (Re-)program MBUS remapping windows if we are asked to.
1171 dram
= mv_mbus_dram_info();
1173 mv_xor_conf_mbus_windows(xordev
, dram
);
1175 /* Not all platforms can gate the clock, so it is not
1176 * an error if the clock does not exists.
1178 xordev
->clk
= clk_get(&pdev
->dev
, NULL
);
1179 if (!IS_ERR(xordev
->clk
))
1180 clk_prepare_enable(xordev
->clk
);
1183 * We don't want to have more than one channel per CPU in
1184 * order for async_tx to perform well. So we limit the number
1185 * of engines and channels so that we take into account this
1186 * constraint. Note that we also want to use channels from
1187 * separate engines when possible.
1189 max_engines
= num_present_cpus();
1190 max_channels
= min_t(unsigned int,
1191 MV_XOR_MAX_CHANNELS
,
1192 DIV_ROUND_UP(num_present_cpus(), 2));
1194 if (mv_xor_engine_count
>= max_engines
)
1197 if (pdev
->dev
.of_node
) {
1198 struct device_node
*np
;
1200 const struct of_device_id
*of_id
=
1201 of_match_device(mv_xor_dt_ids
,
1204 for_each_child_of_node(pdev
->dev
.of_node
, np
) {
1205 struct mv_xor_chan
*chan
;
1206 dma_cap_mask_t cap_mask
;
1208 op_in_desc
= (int)of_id
->data
;
1210 if (i
>= max_channels
)
1213 dma_cap_zero(cap_mask
);
1214 dma_cap_set(DMA_MEMCPY
, cap_mask
);
1215 dma_cap_set(DMA_XOR
, cap_mask
);
1216 dma_cap_set(DMA_INTERRUPT
, cap_mask
);
1218 irq
= irq_of_parse_and_map(np
, 0);
1221 goto err_channel_add
;
1224 chan
= mv_xor_channel_add(xordev
, pdev
, i
,
1225 cap_mask
, irq
, op_in_desc
);
1227 ret
= PTR_ERR(chan
);
1228 irq_dispose_mapping(irq
);
1229 goto err_channel_add
;
1232 xordev
->channels
[i
] = chan
;
1235 } else if (pdata
&& pdata
->channels
) {
1236 for (i
= 0; i
< max_channels
; i
++) {
1237 struct mv_xor_channel_data
*cd
;
1238 struct mv_xor_chan
*chan
;
1241 cd
= &pdata
->channels
[i
];
1244 goto err_channel_add
;
1247 irq
= platform_get_irq(pdev
, i
);
1250 goto err_channel_add
;
1253 chan
= mv_xor_channel_add(xordev
, pdev
, i
,
1257 ret
= PTR_ERR(chan
);
1258 goto err_channel_add
;
1261 xordev
->channels
[i
] = chan
;
1268 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++)
1269 if (xordev
->channels
[i
]) {
1270 mv_xor_channel_remove(xordev
->channels
[i
]);
1271 if (pdev
->dev
.of_node
)
1272 irq_dispose_mapping(xordev
->channels
[i
]->irq
);
1275 if (!IS_ERR(xordev
->clk
)) {
1276 clk_disable_unprepare(xordev
->clk
);
1277 clk_put(xordev
->clk
);
1283 static struct platform_driver mv_xor_driver
= {
1284 .probe
= mv_xor_probe
,
1286 .name
= MV_XOR_NAME
,
1287 .of_match_table
= of_match_ptr(mv_xor_dt_ids
),
1292 static int __init
mv_xor_init(void)
1294 return platform_driver_register(&mv_xor_driver
);
1296 device_initcall(mv_xor_init
);
1299 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1300 MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1301 MODULE_LICENSE("GPL");