2 * offload engine driver for the Intel Xscale series of i/o processors
3 * Copyright © 2006, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope 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.
21 * This driver supports the asynchrounous DMA copy and RAID engines available
22 * on the Intel Xscale(R) family of I/O Processors (IOP 32x, 33x, 134x)
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/async_tx.h>
28 #include <linux/delay.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/spinlock.h>
31 #include <linux/interrupt.h>
32 #include <linux/platform_device.h>
33 #include <linux/memory.h>
34 #include <linux/ioport.h>
36 #include <asm/arch/adma.h>
38 #define to_iop_adma_chan(chan) container_of(chan, struct iop_adma_chan, common)
39 #define to_iop_adma_device(dev) \
40 container_of(dev, struct iop_adma_device, common)
41 #define tx_to_iop_adma_slot(tx) \
42 container_of(tx, struct iop_adma_desc_slot, async_tx)
45 * iop_adma_free_slots - flags descriptor slots for reuse
47 * Caller must hold &iop_chan->lock while calling this function
49 static void iop_adma_free_slots(struct iop_adma_desc_slot *slot)
51 int stride = slot->slots_per_op;
54 slot->slots_per_op = 0;
55 slot = list_entry(slot->slot_node.next,
56 struct iop_adma_desc_slot,
62 iop_adma_run_tx_complete_actions(struct iop_adma_desc_slot *desc,
63 struct iop_adma_chan *iop_chan, dma_cookie_t cookie)
65 BUG_ON(desc->async_tx.cookie < 0);
66 if (desc->async_tx.cookie > 0) {
67 cookie = desc->async_tx.cookie;
68 desc->async_tx.cookie = 0;
70 /* call the callback (must not sleep or submit new
71 * operations to this channel)
73 if (desc->async_tx.callback)
74 desc->async_tx.callback(
75 desc->async_tx.callback_param);
77 /* unmap dma addresses
78 * (unmap_single vs unmap_page?)
80 if (desc->group_head && desc->unmap_len) {
81 struct iop_adma_desc_slot *unmap = desc->group_head;
83 &iop_chan->device->pdev->dev;
84 u32 len = unmap->unmap_len;
85 u32 src_cnt = unmap->unmap_src_cnt;
86 dma_addr_t addr = iop_desc_get_dest_addr(unmap,
89 dma_unmap_page(dev, addr, len, DMA_FROM_DEVICE);
91 addr = iop_desc_get_src_addr(unmap,
94 dma_unmap_page(dev, addr, len,
97 desc->group_head = NULL;
101 /* run dependent operations */
102 async_tx_run_dependencies(&desc->async_tx);
108 iop_adma_clean_slot(struct iop_adma_desc_slot *desc,
109 struct iop_adma_chan *iop_chan)
111 /* the client is allowed to attach dependent operations
114 if (!desc->async_tx.ack)
117 /* leave the last descriptor in the chain
118 * so we can append to it
120 if (desc->chain_node.next == &iop_chan->chain)
123 dev_dbg(iop_chan->device->common.dev,
124 "\tfree slot: %d slots_per_op: %d\n",
125 desc->idx, desc->slots_per_op);
127 list_del(&desc->chain_node);
128 iop_adma_free_slots(desc);
133 static void __iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
135 struct iop_adma_desc_slot *iter, *_iter, *grp_start = NULL;
136 dma_cookie_t cookie = 0;
137 u32 current_desc = iop_chan_get_current_descriptor(iop_chan);
138 int busy = iop_chan_is_busy(iop_chan);
139 int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
141 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
142 /* free completed slots from the chain starting with
143 * the oldest descriptor
145 list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
147 pr_debug("\tcookie: %d slot: %d busy: %d "
148 "this_desc: %#x next_desc: %#x ack: %d\n",
149 iter->async_tx.cookie, iter->idx, busy,
150 iter->async_tx.phys, iop_desc_get_next_desc(iter),
153 prefetch(&_iter->async_tx);
155 /* do not advance past the current descriptor loaded into the
156 * hardware channel, subsequent descriptors are either in
157 * process or have not been submitted
162 /* stop the search if we reach the current descriptor and the
163 * channel is busy, or if it appears that the current descriptor
164 * needs to be re-read (i.e. has been appended to)
166 if (iter->async_tx.phys == current_desc) {
167 BUG_ON(seen_current++);
168 if (busy || iop_desc_get_next_desc(iter))
172 /* detect the start of a group transaction */
173 if (!slot_cnt && !slots_per_op) {
174 slot_cnt = iter->slot_cnt;
175 slots_per_op = iter->slots_per_op;
176 if (slot_cnt <= slots_per_op) {
183 pr_debug("\tgroup++\n");
186 slot_cnt -= slots_per_op;
189 /* all the members of a group are complete */
190 if (slots_per_op != 0 && slot_cnt == 0) {
191 struct iop_adma_desc_slot *grp_iter, *_grp_iter;
192 int end_of_chain = 0;
193 pr_debug("\tgroup end\n");
195 /* collect the total results */
196 if (grp_start->xor_check_result) {
197 u32 zero_sum_result = 0;
198 slot_cnt = grp_start->slot_cnt;
199 grp_iter = grp_start;
201 list_for_each_entry_from(grp_iter,
202 &iop_chan->chain, chain_node) {
204 iop_desc_get_zero_result(grp_iter);
205 pr_debug("\titer%d result: %d\n",
206 grp_iter->idx, zero_sum_result);
207 slot_cnt -= slots_per_op;
211 pr_debug("\tgrp_start->xor_check_result: %p\n",
212 grp_start->xor_check_result);
213 *grp_start->xor_check_result = zero_sum_result;
216 /* clean up the group */
217 slot_cnt = grp_start->slot_cnt;
218 grp_iter = grp_start;
219 list_for_each_entry_safe_from(grp_iter, _grp_iter,
220 &iop_chan->chain, chain_node) {
221 cookie = iop_adma_run_tx_complete_actions(
222 grp_iter, iop_chan, cookie);
224 slot_cnt -= slots_per_op;
225 end_of_chain = iop_adma_clean_slot(grp_iter,
228 if (slot_cnt == 0 || end_of_chain)
232 /* the group should be complete at this point */
241 } else if (slots_per_op) /* wait for group completion */
244 /* write back zero sum results (single descriptor case) */
245 if (iter->xor_check_result && iter->async_tx.cookie)
246 *iter->xor_check_result =
247 iop_desc_get_zero_result(iter);
249 cookie = iop_adma_run_tx_complete_actions(
250 iter, iop_chan, cookie);
252 if (iop_adma_clean_slot(iter, iop_chan))
256 BUG_ON(!seen_current);
258 iop_chan_idle(busy, iop_chan);
261 iop_chan->completed_cookie = cookie;
262 pr_debug("\tcompleted cookie %d\n", cookie);
267 iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
269 spin_lock_bh(&iop_chan->lock);
270 __iop_adma_slot_cleanup(iop_chan);
271 spin_unlock_bh(&iop_chan->lock);
274 static void iop_adma_tasklet(unsigned long data)
276 struct iop_adma_chan *iop_chan = (struct iop_adma_chan *) data;
278 spin_lock(&iop_chan->lock);
279 __iop_adma_slot_cleanup(iop_chan);
280 spin_unlock(&iop_chan->lock);
283 static struct iop_adma_desc_slot *
284 iop_adma_alloc_slots(struct iop_adma_chan *iop_chan, int num_slots,
287 struct iop_adma_desc_slot *iter, *_iter, *alloc_start = NULL;
289 int slots_found, retry = 0;
291 /* start search from the last allocated descrtiptor
292 * if a contiguous allocation can not be found start searching
293 * from the beginning of the list
298 iter = iop_chan->last_used;
300 iter = list_entry(&iop_chan->all_slots,
301 struct iop_adma_desc_slot,
304 list_for_each_entry_safe_continue(
305 iter, _iter, &iop_chan->all_slots, slot_node) {
307 prefetch(&_iter->async_tx);
308 if (iter->slots_per_op) {
309 /* give up after finding the first busy slot
310 * on the second pass through the list
319 /* start the allocation if the slot is correctly aligned */
320 if (!slots_found++) {
321 if (iop_desc_is_aligned(iter, slots_per_op))
329 if (slots_found == num_slots) {
330 struct iop_adma_desc_slot *alloc_tail = NULL;
331 struct iop_adma_desc_slot *last_used = NULL;
335 dev_dbg(iop_chan->device->common.dev,
336 "allocated slot: %d "
337 "(desc %p phys: %#x) slots_per_op %d\n",
338 iter->idx, iter->hw_desc,
339 iter->async_tx.phys, slots_per_op);
341 /* pre-ack all but the last descriptor */
342 if (num_slots != slots_per_op)
343 iter->async_tx.ack = 1;
345 iter->async_tx.ack = 0;
347 list_add_tail(&iter->chain_node, &chain);
349 iter->async_tx.cookie = 0;
350 iter->slot_cnt = num_slots;
351 iter->xor_check_result = NULL;
352 for (i = 0; i < slots_per_op; i++) {
353 iter->slots_per_op = slots_per_op - i;
355 iter = list_entry(iter->slot_node.next,
356 struct iop_adma_desc_slot,
359 num_slots -= slots_per_op;
361 alloc_tail->group_head = alloc_start;
362 alloc_tail->async_tx.cookie = -EBUSY;
363 list_splice(&chain, &alloc_tail->async_tx.tx_list);
364 iop_chan->last_used = last_used;
365 iop_desc_clear_next_desc(alloc_start);
366 iop_desc_clear_next_desc(alloc_tail);
373 /* try to free some slots if the allocation fails */
374 tasklet_schedule(&iop_chan->irq_tasklet);
380 iop_desc_assign_cookie(struct iop_adma_chan *iop_chan,
381 struct iop_adma_desc_slot *desc)
383 dma_cookie_t cookie = iop_chan->common.cookie;
387 iop_chan->common.cookie = desc->async_tx.cookie = cookie;
391 static void iop_adma_check_threshold(struct iop_adma_chan *iop_chan)
393 dev_dbg(iop_chan->device->common.dev, "pending: %d\n",
396 if (iop_chan->pending >= IOP_ADMA_THRESHOLD) {
397 iop_chan->pending = 0;
398 iop_chan_append(iop_chan);
403 iop_adma_tx_submit(struct dma_async_tx_descriptor *tx)
405 struct iop_adma_desc_slot *sw_desc = tx_to_iop_adma_slot(tx);
406 struct iop_adma_chan *iop_chan = to_iop_adma_chan(tx->chan);
407 struct iop_adma_desc_slot *grp_start, *old_chain_tail;
412 grp_start = sw_desc->group_head;
413 slot_cnt = grp_start->slot_cnt;
414 slots_per_op = grp_start->slots_per_op;
416 spin_lock_bh(&iop_chan->lock);
417 cookie = iop_desc_assign_cookie(iop_chan, sw_desc);
419 old_chain_tail = list_entry(iop_chan->chain.prev,
420 struct iop_adma_desc_slot, chain_node);
421 list_splice_init(&sw_desc->async_tx.tx_list,
422 &old_chain_tail->chain_node);
424 /* fix up the hardware chain */
425 iop_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys);
427 /* 1/ don't add pre-chained descriptors
428 * 2/ dummy read to flush next_desc write
430 BUG_ON(iop_desc_get_next_desc(sw_desc));
432 /* increment the pending count by the number of slots
433 * memcpy operations have a 1:1 (slot:operation) relation
434 * other operations are heavier and will pop the threshold
437 iop_chan->pending += slot_cnt;
438 iop_adma_check_threshold(iop_chan);
439 spin_unlock_bh(&iop_chan->lock);
441 dev_dbg(iop_chan->device->common.dev, "%s cookie: %d slot: %d\n",
442 __func__, sw_desc->async_tx.cookie, sw_desc->idx);
447 static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan);
448 static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan);
450 /* returns the number of allocated descriptors */
451 static int iop_adma_alloc_chan_resources(struct dma_chan *chan)
455 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
456 struct iop_adma_desc_slot *slot = NULL;
457 int init = iop_chan->slots_allocated ? 0 : 1;
458 struct iop_adma_platform_data *plat_data =
459 iop_chan->device->pdev->dev.platform_data;
460 int num_descs_in_pool = plat_data->pool_size/IOP_ADMA_SLOT_SIZE;
462 /* Allocate descriptor slots */
464 idx = iop_chan->slots_allocated;
465 if (idx == num_descs_in_pool)
468 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
470 printk(KERN_INFO "IOP ADMA Channel only initialized"
471 " %d descriptor slots", idx);
474 hw_desc = (char *) iop_chan->device->dma_desc_pool_virt;
475 slot->hw_desc = (void *) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
477 dma_async_tx_descriptor_init(&slot->async_tx, chan);
478 slot->async_tx.tx_submit = iop_adma_tx_submit;
479 INIT_LIST_HEAD(&slot->chain_node);
480 INIT_LIST_HEAD(&slot->slot_node);
481 INIT_LIST_HEAD(&slot->async_tx.tx_list);
482 hw_desc = (char *) iop_chan->device->dma_desc_pool;
483 slot->async_tx.phys =
484 (dma_addr_t) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
487 spin_lock_bh(&iop_chan->lock);
488 iop_chan->slots_allocated++;
489 list_add_tail(&slot->slot_node, &iop_chan->all_slots);
490 spin_unlock_bh(&iop_chan->lock);
491 } while (iop_chan->slots_allocated < num_descs_in_pool);
493 if (idx && !iop_chan->last_used)
494 iop_chan->last_used = list_entry(iop_chan->all_slots.next,
495 struct iop_adma_desc_slot,
498 dev_dbg(iop_chan->device->common.dev,
499 "allocated %d descriptor slots last_used: %p\n",
500 iop_chan->slots_allocated, iop_chan->last_used);
502 /* initialize the channel and the chain with a null operation */
504 if (dma_has_cap(DMA_MEMCPY,
505 iop_chan->device->common.cap_mask))
506 iop_chan_start_null_memcpy(iop_chan);
507 else if (dma_has_cap(DMA_XOR,
508 iop_chan->device->common.cap_mask))
509 iop_chan_start_null_xor(iop_chan);
514 return (idx > 0) ? idx : -ENOMEM;
517 static struct dma_async_tx_descriptor *
518 iop_adma_prep_dma_interrupt(struct dma_chan *chan)
520 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
521 struct iop_adma_desc_slot *sw_desc, *grp_start;
522 int slot_cnt, slots_per_op;
524 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
526 spin_lock_bh(&iop_chan->lock);
527 slot_cnt = iop_chan_interrupt_slot_count(&slots_per_op, iop_chan);
528 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
530 grp_start = sw_desc->group_head;
531 iop_desc_init_interrupt(grp_start, iop_chan);
532 grp_start->unmap_len = 0;
534 spin_unlock_bh(&iop_chan->lock);
536 return sw_desc ? &sw_desc->async_tx : NULL;
539 static struct dma_async_tx_descriptor *
540 iop_adma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
541 dma_addr_t dma_src, size_t len, unsigned long flags)
543 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
544 struct iop_adma_desc_slot *sw_desc, *grp_start;
545 int slot_cnt, slots_per_op;
549 BUG_ON(unlikely(len > IOP_ADMA_MAX_BYTE_COUNT));
551 dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
554 spin_lock_bh(&iop_chan->lock);
555 slot_cnt = iop_chan_memcpy_slot_count(len, &slots_per_op);
556 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
558 grp_start = sw_desc->group_head;
559 iop_desc_init_memcpy(grp_start, flags);
560 iop_desc_set_byte_count(grp_start, iop_chan, len);
561 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
562 iop_desc_set_memcpy_src_addr(grp_start, dma_src);
563 sw_desc->unmap_src_cnt = 1;
564 sw_desc->unmap_len = len;
566 spin_unlock_bh(&iop_chan->lock);
568 return sw_desc ? &sw_desc->async_tx : NULL;
571 static struct dma_async_tx_descriptor *
572 iop_adma_prep_dma_memset(struct dma_chan *chan, dma_addr_t dma_dest,
573 int value, size_t len, unsigned long flags)
575 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
576 struct iop_adma_desc_slot *sw_desc, *grp_start;
577 int slot_cnt, slots_per_op;
581 BUG_ON(unlikely(len > IOP_ADMA_MAX_BYTE_COUNT));
583 dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
586 spin_lock_bh(&iop_chan->lock);
587 slot_cnt = iop_chan_memset_slot_count(len, &slots_per_op);
588 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
590 grp_start = sw_desc->group_head;
591 iop_desc_init_memset(grp_start, flags);
592 iop_desc_set_byte_count(grp_start, iop_chan, len);
593 iop_desc_set_block_fill_val(grp_start, value);
594 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
595 sw_desc->unmap_src_cnt = 1;
596 sw_desc->unmap_len = len;
598 spin_unlock_bh(&iop_chan->lock);
600 return sw_desc ? &sw_desc->async_tx : NULL;
603 static struct dma_async_tx_descriptor *
604 iop_adma_prep_dma_xor(struct dma_chan *chan, dma_addr_t dma_dest,
605 dma_addr_t *dma_src, unsigned int src_cnt, size_t len,
608 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
609 struct iop_adma_desc_slot *sw_desc, *grp_start;
610 int slot_cnt, slots_per_op;
614 BUG_ON(unlikely(len > IOP_ADMA_XOR_MAX_BYTE_COUNT));
616 dev_dbg(iop_chan->device->common.dev,
617 "%s src_cnt: %d len: %u flags: %lx\n",
618 __func__, src_cnt, len, flags);
620 spin_lock_bh(&iop_chan->lock);
621 slot_cnt = iop_chan_xor_slot_count(len, src_cnt, &slots_per_op);
622 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
624 grp_start = sw_desc->group_head;
625 iop_desc_init_xor(grp_start, src_cnt, flags);
626 iop_desc_set_byte_count(grp_start, iop_chan, len);
627 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
628 sw_desc->unmap_src_cnt = src_cnt;
629 sw_desc->unmap_len = len;
631 iop_desc_set_xor_src_addr(grp_start, src_cnt,
634 spin_unlock_bh(&iop_chan->lock);
636 return sw_desc ? &sw_desc->async_tx : NULL;
639 static struct dma_async_tx_descriptor *
640 iop_adma_prep_dma_zero_sum(struct dma_chan *chan, dma_addr_t *dma_src,
641 unsigned int src_cnt, size_t len, u32 *result,
644 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
645 struct iop_adma_desc_slot *sw_desc, *grp_start;
646 int slot_cnt, slots_per_op;
651 dev_dbg(iop_chan->device->common.dev, "%s src_cnt: %d len: %u\n",
652 __func__, src_cnt, len);
654 spin_lock_bh(&iop_chan->lock);
655 slot_cnt = iop_chan_zero_sum_slot_count(len, src_cnt, &slots_per_op);
656 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
658 grp_start = sw_desc->group_head;
659 iop_desc_init_zero_sum(grp_start, src_cnt, flags);
660 iop_desc_set_zero_sum_byte_count(grp_start, len);
661 grp_start->xor_check_result = result;
662 pr_debug("\t%s: grp_start->xor_check_result: %p\n",
663 __func__, grp_start->xor_check_result);
664 sw_desc->unmap_src_cnt = src_cnt;
665 sw_desc->unmap_len = len;
667 iop_desc_set_zero_sum_src_addr(grp_start, src_cnt,
670 spin_unlock_bh(&iop_chan->lock);
672 return sw_desc ? &sw_desc->async_tx : NULL;
675 static void iop_adma_dependency_added(struct dma_chan *chan)
677 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
678 tasklet_schedule(&iop_chan->irq_tasklet);
681 static void iop_adma_free_chan_resources(struct dma_chan *chan)
683 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
684 struct iop_adma_desc_slot *iter, *_iter;
685 int in_use_descs = 0;
687 iop_adma_slot_cleanup(iop_chan);
689 spin_lock_bh(&iop_chan->lock);
690 list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
693 list_del(&iter->chain_node);
695 list_for_each_entry_safe_reverse(
696 iter, _iter, &iop_chan->all_slots, slot_node) {
697 list_del(&iter->slot_node);
699 iop_chan->slots_allocated--;
701 iop_chan->last_used = NULL;
703 dev_dbg(iop_chan->device->common.dev, "%s slots_allocated %d\n",
704 __func__, iop_chan->slots_allocated);
705 spin_unlock_bh(&iop_chan->lock);
707 /* one is ok since we left it on there on purpose */
708 if (in_use_descs > 1)
709 printk(KERN_ERR "IOP: Freeing %d in use descriptors!\n",
714 * iop_adma_is_complete - poll the status of an ADMA transaction
715 * @chan: ADMA channel handle
716 * @cookie: ADMA transaction identifier
718 static enum dma_status iop_adma_is_complete(struct dma_chan *chan,
723 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
724 dma_cookie_t last_used;
725 dma_cookie_t last_complete;
728 last_used = chan->cookie;
729 last_complete = iop_chan->completed_cookie;
732 *done = last_complete;
736 ret = dma_async_is_complete(cookie, last_complete, last_used);
737 if (ret == DMA_SUCCESS)
740 iop_adma_slot_cleanup(iop_chan);
742 last_used = chan->cookie;
743 last_complete = iop_chan->completed_cookie;
746 *done = last_complete;
750 return dma_async_is_complete(cookie, last_complete, last_used);
753 static irqreturn_t iop_adma_eot_handler(int irq, void *data)
755 struct iop_adma_chan *chan = data;
757 dev_dbg(chan->device->common.dev, "%s\n", __func__);
759 tasklet_schedule(&chan->irq_tasklet);
761 iop_adma_device_clear_eot_status(chan);
766 static irqreturn_t iop_adma_eoc_handler(int irq, void *data)
768 struct iop_adma_chan *chan = data;
770 dev_dbg(chan->device->common.dev, "%s\n", __func__);
772 tasklet_schedule(&chan->irq_tasklet);
774 iop_adma_device_clear_eoc_status(chan);
779 static irqreturn_t iop_adma_err_handler(int irq, void *data)
781 struct iop_adma_chan *chan = data;
782 unsigned long status = iop_chan_get_status(chan);
784 dev_printk(KERN_ERR, chan->device->common.dev,
785 "error ( %s%s%s%s%s%s%s)\n",
786 iop_is_err_int_parity(status, chan) ? "int_parity " : "",
787 iop_is_err_mcu_abort(status, chan) ? "mcu_abort " : "",
788 iop_is_err_int_tabort(status, chan) ? "int_tabort " : "",
789 iop_is_err_int_mabort(status, chan) ? "int_mabort " : "",
790 iop_is_err_pci_tabort(status, chan) ? "pci_tabort " : "",
791 iop_is_err_pci_mabort(status, chan) ? "pci_mabort " : "",
792 iop_is_err_split_tx(status, chan) ? "split_tx " : "");
794 iop_adma_device_clear_err_status(chan);
801 static void iop_adma_issue_pending(struct dma_chan *chan)
803 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
805 if (iop_chan->pending) {
806 iop_chan->pending = 0;
807 iop_chan_append(iop_chan);
812 * Perform a transaction to verify the HW works.
814 #define IOP_ADMA_TEST_SIZE 2000
816 static int __devinit iop_adma_memcpy_self_test(struct iop_adma_device *device)
820 dma_addr_t src_dma, dest_dma;
821 struct dma_chan *dma_chan;
823 struct dma_async_tx_descriptor *tx;
825 struct iop_adma_chan *iop_chan;
827 dev_dbg(device->common.dev, "%s\n", __func__);
829 src = kzalloc(sizeof(u8) * IOP_ADMA_TEST_SIZE, GFP_KERNEL);
832 dest = kzalloc(sizeof(u8) * IOP_ADMA_TEST_SIZE, GFP_KERNEL);
838 /* Fill in src buffer */
839 for (i = 0; i < IOP_ADMA_TEST_SIZE; i++)
840 ((u8 *) src)[i] = (u8)i;
842 memset(dest, 0, IOP_ADMA_TEST_SIZE);
844 /* Start copy, using first DMA channel */
845 dma_chan = container_of(device->common.channels.next,
848 if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
853 dest_dma = dma_map_single(dma_chan->device->dev, dest,
854 IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
855 src_dma = dma_map_single(dma_chan->device->dev, src,
856 IOP_ADMA_TEST_SIZE, DMA_TO_DEVICE);
857 tx = iop_adma_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
858 IOP_ADMA_TEST_SIZE, 1);
860 cookie = iop_adma_tx_submit(tx);
861 iop_adma_issue_pending(dma_chan);
865 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
867 dev_printk(KERN_ERR, dma_chan->device->dev,
868 "Self-test copy timed out, disabling\n");
873 iop_chan = to_iop_adma_chan(dma_chan);
874 dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
875 IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
876 if (memcmp(src, dest, IOP_ADMA_TEST_SIZE)) {
877 dev_printk(KERN_ERR, dma_chan->device->dev,
878 "Self-test copy failed compare, disabling\n");
884 iop_adma_free_chan_resources(dma_chan);
891 #define IOP_ADMA_NUM_SRC_TEST 4 /* must be <= 15 */
893 iop_adma_xor_zero_sum_self_test(struct iop_adma_device *device)
897 struct page *xor_srcs[IOP_ADMA_NUM_SRC_TEST];
898 struct page *zero_sum_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
899 dma_addr_t dma_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
900 dma_addr_t dma_addr, dest_dma;
901 struct dma_async_tx_descriptor *tx;
902 struct dma_chan *dma_chan;
908 struct iop_adma_chan *iop_chan;
910 dev_dbg(device->common.dev, "%s\n", __func__);
912 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
913 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
914 if (!xor_srcs[src_idx])
916 __free_page(xor_srcs[src_idx]);
921 dest = alloc_page(GFP_KERNEL);
924 __free_page(xor_srcs[src_idx]);
928 /* Fill in src buffers */
929 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
930 u8 *ptr = page_address(xor_srcs[src_idx]);
931 for (i = 0; i < PAGE_SIZE; i++)
932 ptr[i] = (1 << src_idx);
935 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++)
936 cmp_byte ^= (u8) (1 << src_idx);
938 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
939 (cmp_byte << 8) | cmp_byte;
941 memset(page_address(dest), 0, PAGE_SIZE);
943 dma_chan = container_of(device->common.channels.next,
946 if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
952 dest_dma = dma_map_page(dma_chan->device->dev, dest, 0,
953 PAGE_SIZE, DMA_FROM_DEVICE);
954 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
955 dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
956 0, PAGE_SIZE, DMA_TO_DEVICE);
957 tx = iop_adma_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
958 IOP_ADMA_NUM_SRC_TEST, PAGE_SIZE, 1);
960 cookie = iop_adma_tx_submit(tx);
961 iop_adma_issue_pending(dma_chan);
965 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
967 dev_printk(KERN_ERR, dma_chan->device->dev,
968 "Self-test xor timed out, disabling\n");
973 iop_chan = to_iop_adma_chan(dma_chan);
974 dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
975 PAGE_SIZE, DMA_FROM_DEVICE);
976 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
977 u32 *ptr = page_address(dest);
978 if (ptr[i] != cmp_word) {
979 dev_printk(KERN_ERR, dma_chan->device->dev,
980 "Self-test xor failed compare, disabling\n");
985 dma_sync_single_for_device(&iop_chan->device->pdev->dev, dest_dma,
986 PAGE_SIZE, DMA_TO_DEVICE);
988 /* skip zero sum if the capability is not present */
989 if (!dma_has_cap(DMA_ZERO_SUM, dma_chan->device->cap_mask))
992 /* zero sum the sources with the destintation page */
993 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
994 zero_sum_srcs[i] = xor_srcs[i];
995 zero_sum_srcs[i] = dest;
999 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
1000 dma_srcs[i] = dma_map_page(dma_chan->device->dev,
1001 zero_sum_srcs[i], 0, PAGE_SIZE,
1003 tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
1004 IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
1005 &zero_sum_result, 1);
1007 cookie = iop_adma_tx_submit(tx);
1008 iop_adma_issue_pending(dma_chan);
1012 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1013 dev_printk(KERN_ERR, dma_chan->device->dev,
1014 "Self-test zero sum timed out, disabling\n");
1016 goto free_resources;
1019 if (zero_sum_result != 0) {
1020 dev_printk(KERN_ERR, dma_chan->device->dev,
1021 "Self-test zero sum failed compare, disabling\n");
1023 goto free_resources;
1027 dma_addr = dma_map_page(dma_chan->device->dev, dest, 0,
1028 PAGE_SIZE, DMA_FROM_DEVICE);
1029 tx = iop_adma_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE, 1);
1031 cookie = iop_adma_tx_submit(tx);
1032 iop_adma_issue_pending(dma_chan);
1036 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1037 dev_printk(KERN_ERR, dma_chan->device->dev,
1038 "Self-test memset timed out, disabling\n");
1040 goto free_resources;
1043 for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
1044 u32 *ptr = page_address(dest);
1046 dev_printk(KERN_ERR, dma_chan->device->dev,
1047 "Self-test memset failed compare, disabling\n");
1049 goto free_resources;
1053 /* test for non-zero parity sum */
1054 zero_sum_result = 0;
1055 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
1056 dma_srcs[i] = dma_map_page(dma_chan->device->dev,
1057 zero_sum_srcs[i], 0, PAGE_SIZE,
1059 tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
1060 IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
1061 &zero_sum_result, 1);
1063 cookie = iop_adma_tx_submit(tx);
1064 iop_adma_issue_pending(dma_chan);
1068 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1069 dev_printk(KERN_ERR, dma_chan->device->dev,
1070 "Self-test non-zero sum timed out, disabling\n");
1072 goto free_resources;
1075 if (zero_sum_result != 1) {
1076 dev_printk(KERN_ERR, dma_chan->device->dev,
1077 "Self-test non-zero sum failed compare, disabling\n");
1079 goto free_resources;
1083 iop_adma_free_chan_resources(dma_chan);
1085 src_idx = IOP_ADMA_NUM_SRC_TEST;
1087 __free_page(xor_srcs[src_idx]);
1092 static int __devexit iop_adma_remove(struct platform_device *dev)
1094 struct iop_adma_device *device = platform_get_drvdata(dev);
1095 struct dma_chan *chan, *_chan;
1096 struct iop_adma_chan *iop_chan;
1098 struct iop_adma_platform_data *plat_data = dev->dev.platform_data;
1100 dma_async_device_unregister(&device->common);
1102 for (i = 0; i < 3; i++) {
1104 irq = platform_get_irq(dev, i);
1105 free_irq(irq, device);
1108 dma_free_coherent(&dev->dev, plat_data->pool_size,
1109 device->dma_desc_pool_virt, device->dma_desc_pool);
1112 struct resource *res;
1113 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1114 release_mem_region(res->start, res->end - res->start);
1117 list_for_each_entry_safe(chan, _chan, &device->common.channels,
1119 iop_chan = to_iop_adma_chan(chan);
1120 list_del(&chan->device_node);
1128 static int __devinit iop_adma_probe(struct platform_device *pdev)
1130 struct resource *res;
1132 struct iop_adma_device *adev;
1133 struct iop_adma_chan *iop_chan;
1134 struct dma_device *dma_dev;
1135 struct iop_adma_platform_data *plat_data = pdev->dev.platform_data;
1137 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1141 if (!devm_request_mem_region(&pdev->dev, res->start,
1142 res->end - res->start, pdev->name))
1145 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
1148 dma_dev = &adev->common;
1150 /* allocate coherent memory for hardware descriptors
1151 * note: writecombine gives slightly better performance, but
1152 * requires that we explicitly flush the writes
1154 if ((adev->dma_desc_pool_virt = dma_alloc_writecombine(&pdev->dev,
1155 plat_data->pool_size,
1156 &adev->dma_desc_pool,
1157 GFP_KERNEL)) == NULL) {
1162 dev_dbg(&pdev->dev, "%s: allocted descriptor pool virt %p phys %p\n",
1163 __func__, adev->dma_desc_pool_virt,
1164 (void *) adev->dma_desc_pool);
1166 adev->id = plat_data->hw_id;
1168 /* discover transaction capabilites from the platform data */
1169 dma_dev->cap_mask = plat_data->cap_mask;
1172 platform_set_drvdata(pdev, adev);
1174 INIT_LIST_HEAD(&dma_dev->channels);
1176 /* set base routines */
1177 dma_dev->device_alloc_chan_resources = iop_adma_alloc_chan_resources;
1178 dma_dev->device_free_chan_resources = iop_adma_free_chan_resources;
1179 dma_dev->device_is_tx_complete = iop_adma_is_complete;
1180 dma_dev->device_issue_pending = iop_adma_issue_pending;
1181 dma_dev->device_dependency_added = iop_adma_dependency_added;
1182 dma_dev->dev = &pdev->dev;
1184 /* set prep routines based on capability */
1185 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
1186 dma_dev->device_prep_dma_memcpy = iop_adma_prep_dma_memcpy;
1187 if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask))
1188 dma_dev->device_prep_dma_memset = iop_adma_prep_dma_memset;
1189 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
1190 dma_dev->max_xor = iop_adma_get_max_xor();
1191 dma_dev->device_prep_dma_xor = iop_adma_prep_dma_xor;
1193 if (dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask))
1194 dma_dev->device_prep_dma_zero_sum =
1195 iop_adma_prep_dma_zero_sum;
1196 if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
1197 dma_dev->device_prep_dma_interrupt =
1198 iop_adma_prep_dma_interrupt;
1200 iop_chan = kzalloc(sizeof(*iop_chan), GFP_KERNEL);
1205 iop_chan->device = adev;
1207 iop_chan->mmr_base = devm_ioremap(&pdev->dev, res->start,
1208 res->end - res->start);
1209 if (!iop_chan->mmr_base) {
1211 goto err_free_iop_chan;
1213 tasklet_init(&iop_chan->irq_tasklet, iop_adma_tasklet, (unsigned long)
1216 /* clear errors before enabling interrupts */
1217 iop_adma_device_clear_err_status(iop_chan);
1219 for (i = 0; i < 3; i++) {
1220 irq_handler_t handler[] = { iop_adma_eot_handler,
1221 iop_adma_eoc_handler,
1222 iop_adma_err_handler };
1223 int irq = platform_get_irq(pdev, i);
1226 goto err_free_iop_chan;
1228 ret = devm_request_irq(&pdev->dev, irq,
1229 handler[i], 0, pdev->name, iop_chan);
1231 goto err_free_iop_chan;
1235 spin_lock_init(&iop_chan->lock);
1236 init_timer(&iop_chan->cleanup_watchdog);
1237 iop_chan->cleanup_watchdog.data = (unsigned long) iop_chan;
1238 iop_chan->cleanup_watchdog.function = iop_adma_tasklet;
1239 INIT_LIST_HEAD(&iop_chan->chain);
1240 INIT_LIST_HEAD(&iop_chan->all_slots);
1241 INIT_RCU_HEAD(&iop_chan->common.rcu);
1242 iop_chan->common.device = dma_dev;
1243 list_add_tail(&iop_chan->common.device_node, &dma_dev->channels);
1245 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
1246 ret = iop_adma_memcpy_self_test(adev);
1247 dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1249 goto err_free_iop_chan;
1252 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask) ||
1253 dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) {
1254 ret = iop_adma_xor_zero_sum_self_test(adev);
1255 dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1257 goto err_free_iop_chan;
1260 dev_printk(KERN_INFO, &pdev->dev, "Intel(R) IOP: "
1261 "( %s%s%s%s%s%s%s%s%s%s)\n",
1262 dma_has_cap(DMA_PQ_XOR, dma_dev->cap_mask) ? "pq_xor " : "",
1263 dma_has_cap(DMA_PQ_UPDATE, dma_dev->cap_mask) ? "pq_update " : "",
1264 dma_has_cap(DMA_PQ_ZERO_SUM, dma_dev->cap_mask) ? "pq_zero_sum " : "",
1265 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
1266 dma_has_cap(DMA_DUAL_XOR, dma_dev->cap_mask) ? "dual_xor " : "",
1267 dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask) ? "xor_zero_sum " : "",
1268 dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "",
1269 dma_has_cap(DMA_MEMCPY_CRC32C, dma_dev->cap_mask) ? "cpy+crc " : "",
1270 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1271 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
1273 dma_async_device_register(dma_dev);
1279 dma_free_coherent(&adev->pdev->dev, plat_data->pool_size,
1280 adev->dma_desc_pool_virt, adev->dma_desc_pool);
1287 static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan)
1289 struct iop_adma_desc_slot *sw_desc, *grp_start;
1290 dma_cookie_t cookie;
1291 int slot_cnt, slots_per_op;
1293 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
1295 spin_lock_bh(&iop_chan->lock);
1296 slot_cnt = iop_chan_memcpy_slot_count(0, &slots_per_op);
1297 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
1299 grp_start = sw_desc->group_head;
1301 list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
1302 sw_desc->async_tx.ack = 1;
1303 iop_desc_init_memcpy(grp_start, 0);
1304 iop_desc_set_byte_count(grp_start, iop_chan, 0);
1305 iop_desc_set_dest_addr(grp_start, iop_chan, 0);
1306 iop_desc_set_memcpy_src_addr(grp_start, 0);
1308 cookie = iop_chan->common.cookie;
1313 /* initialize the completed cookie to be less than
1314 * the most recently used cookie
1316 iop_chan->completed_cookie = cookie - 1;
1317 iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
1319 /* channel should not be busy */
1320 BUG_ON(iop_chan_is_busy(iop_chan));
1322 /* clear any prior error-status bits */
1323 iop_adma_device_clear_err_status(iop_chan);
1325 /* disable operation */
1326 iop_chan_disable(iop_chan);
1328 /* set the descriptor address */
1329 iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
1331 /* 1/ don't add pre-chained descriptors
1332 * 2/ dummy read to flush next_desc write
1334 BUG_ON(iop_desc_get_next_desc(sw_desc));
1336 /* run the descriptor */
1337 iop_chan_enable(iop_chan);
1339 dev_printk(KERN_ERR, iop_chan->device->common.dev,
1340 "failed to allocate null descriptor\n");
1341 spin_unlock_bh(&iop_chan->lock);
1344 static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan)
1346 struct iop_adma_desc_slot *sw_desc, *grp_start;
1347 dma_cookie_t cookie;
1348 int slot_cnt, slots_per_op;
1350 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
1352 spin_lock_bh(&iop_chan->lock);
1353 slot_cnt = iop_chan_xor_slot_count(0, 2, &slots_per_op);
1354 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
1356 grp_start = sw_desc->group_head;
1357 list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
1358 sw_desc->async_tx.ack = 1;
1359 iop_desc_init_null_xor(grp_start, 2, 0);
1360 iop_desc_set_byte_count(grp_start, iop_chan, 0);
1361 iop_desc_set_dest_addr(grp_start, iop_chan, 0);
1362 iop_desc_set_xor_src_addr(grp_start, 0, 0);
1363 iop_desc_set_xor_src_addr(grp_start, 1, 0);
1365 cookie = iop_chan->common.cookie;
1370 /* initialize the completed cookie to be less than
1371 * the most recently used cookie
1373 iop_chan->completed_cookie = cookie - 1;
1374 iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
1376 /* channel should not be busy */
1377 BUG_ON(iop_chan_is_busy(iop_chan));
1379 /* clear any prior error-status bits */
1380 iop_adma_device_clear_err_status(iop_chan);
1382 /* disable operation */
1383 iop_chan_disable(iop_chan);
1385 /* set the descriptor address */
1386 iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
1388 /* 1/ don't add pre-chained descriptors
1389 * 2/ dummy read to flush next_desc write
1391 BUG_ON(iop_desc_get_next_desc(sw_desc));
1393 /* run the descriptor */
1394 iop_chan_enable(iop_chan);
1396 dev_printk(KERN_ERR, iop_chan->device->common.dev,
1397 "failed to allocate null descriptor\n");
1398 spin_unlock_bh(&iop_chan->lock);
1401 static struct platform_driver iop_adma_driver = {
1402 .probe = iop_adma_probe,
1403 .remove = iop_adma_remove,
1405 .owner = THIS_MODULE,
1410 static int __init iop_adma_init (void)
1412 return platform_driver_register(&iop_adma_driver);
1415 /* it's currently unsafe to unload this module */
1417 static void __exit iop_adma_exit (void)
1419 platform_driver_unregister(&iop_adma_driver);
1422 module_exit(iop_adma_exit);
1425 module_init(iop_adma_init);
1427 MODULE_AUTHOR("Intel Corporation");
1428 MODULE_DESCRIPTION("IOP ADMA Engine Driver");
1429 MODULE_LICENSE("GPL");