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 <mach/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 enum dma_ctrl_flags flags = desc->async_tx.flags;
89 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
90 addr = iop_desc_get_dest_addr(unmap, iop_chan);
91 dma_unmap_page(dev, addr, len, DMA_FROM_DEVICE);
94 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
95 src_cnt = unmap->unmap_src_cnt;
97 addr = iop_desc_get_src_addr(unmap,
100 dma_unmap_page(dev, addr, len,
104 desc->group_head = NULL;
108 /* run dependent operations */
109 async_tx_run_dependencies(&desc->async_tx);
115 iop_adma_clean_slot(struct iop_adma_desc_slot *desc,
116 struct iop_adma_chan *iop_chan)
118 /* the client is allowed to attach dependent operations
121 if (!async_tx_test_ack(&desc->async_tx))
124 /* leave the last descriptor in the chain
125 * so we can append to it
127 if (desc->chain_node.next == &iop_chan->chain)
130 dev_dbg(iop_chan->device->common.dev,
131 "\tfree slot: %d slots_per_op: %d\n",
132 desc->idx, desc->slots_per_op);
134 list_del(&desc->chain_node);
135 iop_adma_free_slots(desc);
140 static void __iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
142 struct iop_adma_desc_slot *iter, *_iter, *grp_start = NULL;
143 dma_cookie_t cookie = 0;
144 u32 current_desc = iop_chan_get_current_descriptor(iop_chan);
145 int busy = iop_chan_is_busy(iop_chan);
146 int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
148 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
149 /* free completed slots from the chain starting with
150 * the oldest descriptor
152 list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
154 pr_debug("\tcookie: %d slot: %d busy: %d "
155 "this_desc: %#x next_desc: %#x ack: %d\n",
156 iter->async_tx.cookie, iter->idx, busy,
157 iter->async_tx.phys, iop_desc_get_next_desc(iter),
158 async_tx_test_ack(&iter->async_tx));
160 prefetch(&_iter->async_tx);
162 /* do not advance past the current descriptor loaded into the
163 * hardware channel, subsequent descriptors are either in
164 * process or have not been submitted
169 /* stop the search if we reach the current descriptor and the
170 * channel is busy, or if it appears that the current descriptor
171 * needs to be re-read (i.e. has been appended to)
173 if (iter->async_tx.phys == current_desc) {
174 BUG_ON(seen_current++);
175 if (busy || iop_desc_get_next_desc(iter))
179 /* detect the start of a group transaction */
180 if (!slot_cnt && !slots_per_op) {
181 slot_cnt = iter->slot_cnt;
182 slots_per_op = iter->slots_per_op;
183 if (slot_cnt <= slots_per_op) {
190 pr_debug("\tgroup++\n");
193 slot_cnt -= slots_per_op;
196 /* all the members of a group are complete */
197 if (slots_per_op != 0 && slot_cnt == 0) {
198 struct iop_adma_desc_slot *grp_iter, *_grp_iter;
199 int end_of_chain = 0;
200 pr_debug("\tgroup end\n");
202 /* collect the total results */
203 if (grp_start->xor_check_result) {
204 u32 zero_sum_result = 0;
205 slot_cnt = grp_start->slot_cnt;
206 grp_iter = grp_start;
208 list_for_each_entry_from(grp_iter,
209 &iop_chan->chain, chain_node) {
211 iop_desc_get_zero_result(grp_iter);
212 pr_debug("\titer%d result: %d\n",
213 grp_iter->idx, zero_sum_result);
214 slot_cnt -= slots_per_op;
218 pr_debug("\tgrp_start->xor_check_result: %p\n",
219 grp_start->xor_check_result);
220 *grp_start->xor_check_result = zero_sum_result;
223 /* clean up the group */
224 slot_cnt = grp_start->slot_cnt;
225 grp_iter = grp_start;
226 list_for_each_entry_safe_from(grp_iter, _grp_iter,
227 &iop_chan->chain, chain_node) {
228 cookie = iop_adma_run_tx_complete_actions(
229 grp_iter, iop_chan, cookie);
231 slot_cnt -= slots_per_op;
232 end_of_chain = iop_adma_clean_slot(grp_iter,
235 if (slot_cnt == 0 || end_of_chain)
239 /* the group should be complete at this point */
248 } else if (slots_per_op) /* wait for group completion */
251 /* write back zero sum results (single descriptor case) */
252 if (iter->xor_check_result && iter->async_tx.cookie)
253 *iter->xor_check_result =
254 iop_desc_get_zero_result(iter);
256 cookie = iop_adma_run_tx_complete_actions(
257 iter, iop_chan, cookie);
259 if (iop_adma_clean_slot(iter, iop_chan))
263 BUG_ON(!seen_current);
266 iop_chan->completed_cookie = cookie;
267 pr_debug("\tcompleted cookie %d\n", cookie);
272 iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
274 spin_lock_bh(&iop_chan->lock);
275 __iop_adma_slot_cleanup(iop_chan);
276 spin_unlock_bh(&iop_chan->lock);
279 static void iop_adma_tasklet(unsigned long data)
281 struct iop_adma_chan *iop_chan = (struct iop_adma_chan *) data;
283 spin_lock(&iop_chan->lock);
284 __iop_adma_slot_cleanup(iop_chan);
285 spin_unlock(&iop_chan->lock);
288 static struct iop_adma_desc_slot *
289 iop_adma_alloc_slots(struct iop_adma_chan *iop_chan, int num_slots,
292 struct iop_adma_desc_slot *iter, *_iter, *alloc_start = NULL;
294 int slots_found, retry = 0;
296 /* start search from the last allocated descrtiptor
297 * if a contiguous allocation can not be found start searching
298 * from the beginning of the list
303 iter = iop_chan->last_used;
305 iter = list_entry(&iop_chan->all_slots,
306 struct iop_adma_desc_slot,
309 list_for_each_entry_safe_continue(
310 iter, _iter, &iop_chan->all_slots, slot_node) {
312 prefetch(&_iter->async_tx);
313 if (iter->slots_per_op) {
314 /* give up after finding the first busy slot
315 * on the second pass through the list
324 /* start the allocation if the slot is correctly aligned */
325 if (!slots_found++) {
326 if (iop_desc_is_aligned(iter, slots_per_op))
334 if (slots_found == num_slots) {
335 struct iop_adma_desc_slot *alloc_tail = NULL;
336 struct iop_adma_desc_slot *last_used = NULL;
340 dev_dbg(iop_chan->device->common.dev,
341 "allocated slot: %d "
342 "(desc %p phys: %#x) slots_per_op %d\n",
343 iter->idx, iter->hw_desc,
344 iter->async_tx.phys, slots_per_op);
346 /* pre-ack all but the last descriptor */
347 if (num_slots != slots_per_op)
348 async_tx_ack(&iter->async_tx);
350 list_add_tail(&iter->chain_node, &chain);
352 iter->async_tx.cookie = 0;
353 iter->slot_cnt = num_slots;
354 iter->xor_check_result = NULL;
355 for (i = 0; i < slots_per_op; i++) {
356 iter->slots_per_op = slots_per_op - i;
358 iter = list_entry(iter->slot_node.next,
359 struct iop_adma_desc_slot,
362 num_slots -= slots_per_op;
364 alloc_tail->group_head = alloc_start;
365 alloc_tail->async_tx.cookie = -EBUSY;
366 list_splice(&chain, &alloc_tail->async_tx.tx_list);
367 iop_chan->last_used = last_used;
368 iop_desc_clear_next_desc(alloc_start);
369 iop_desc_clear_next_desc(alloc_tail);
376 /* perform direct reclaim if the allocation fails */
377 __iop_adma_slot_cleanup(iop_chan);
383 iop_desc_assign_cookie(struct iop_adma_chan *iop_chan,
384 struct iop_adma_desc_slot *desc)
386 dma_cookie_t cookie = iop_chan->common.cookie;
390 iop_chan->common.cookie = desc->async_tx.cookie = cookie;
394 static void iop_adma_check_threshold(struct iop_adma_chan *iop_chan)
396 dev_dbg(iop_chan->device->common.dev, "pending: %d\n",
399 if (iop_chan->pending >= IOP_ADMA_THRESHOLD) {
400 iop_chan->pending = 0;
401 iop_chan_append(iop_chan);
406 iop_adma_tx_submit(struct dma_async_tx_descriptor *tx)
408 struct iop_adma_desc_slot *sw_desc = tx_to_iop_adma_slot(tx);
409 struct iop_adma_chan *iop_chan = to_iop_adma_chan(tx->chan);
410 struct iop_adma_desc_slot *grp_start, *old_chain_tail;
415 grp_start = sw_desc->group_head;
416 slot_cnt = grp_start->slot_cnt;
417 slots_per_op = grp_start->slots_per_op;
419 spin_lock_bh(&iop_chan->lock);
420 cookie = iop_desc_assign_cookie(iop_chan, sw_desc);
422 old_chain_tail = list_entry(iop_chan->chain.prev,
423 struct iop_adma_desc_slot, chain_node);
424 list_splice_init(&sw_desc->async_tx.tx_list,
425 &old_chain_tail->chain_node);
427 /* fix up the hardware chain */
428 iop_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys);
430 /* 1/ don't add pre-chained descriptors
431 * 2/ dummy read to flush next_desc write
433 BUG_ON(iop_desc_get_next_desc(sw_desc));
435 /* increment the pending count by the number of slots
436 * memcpy operations have a 1:1 (slot:operation) relation
437 * other operations are heavier and will pop the threshold
440 iop_chan->pending += slot_cnt;
441 iop_adma_check_threshold(iop_chan);
442 spin_unlock_bh(&iop_chan->lock);
444 dev_dbg(iop_chan->device->common.dev, "%s cookie: %d slot: %d\n",
445 __func__, sw_desc->async_tx.cookie, sw_desc->idx);
450 static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan);
451 static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan);
454 * iop_adma_alloc_chan_resources - returns the number of allocated descriptors
455 * @chan - allocate descriptor resources for this channel
456 * @client - current client requesting the channel be ready for requests
458 * Note: We keep the slots for 1 operation on iop_chan->chain at all times. To
459 * avoid deadlock, via async_xor, num_descs_in_pool must at a minimum be
460 * greater than 2x the number slots needed to satisfy a device->max_xor
463 static int iop_adma_alloc_chan_resources(struct dma_chan *chan,
464 struct dma_client *client)
468 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
469 struct iop_adma_desc_slot *slot = NULL;
470 int init = iop_chan->slots_allocated ? 0 : 1;
471 struct iop_adma_platform_data *plat_data =
472 iop_chan->device->pdev->dev.platform_data;
473 int num_descs_in_pool = plat_data->pool_size/IOP_ADMA_SLOT_SIZE;
475 /* Allocate descriptor slots */
477 idx = iop_chan->slots_allocated;
478 if (idx == num_descs_in_pool)
481 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
483 printk(KERN_INFO "IOP ADMA Channel only initialized"
484 " %d descriptor slots", idx);
487 hw_desc = (char *) iop_chan->device->dma_desc_pool_virt;
488 slot->hw_desc = (void *) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
490 dma_async_tx_descriptor_init(&slot->async_tx, chan);
491 slot->async_tx.tx_submit = iop_adma_tx_submit;
492 INIT_LIST_HEAD(&slot->chain_node);
493 INIT_LIST_HEAD(&slot->slot_node);
494 INIT_LIST_HEAD(&slot->async_tx.tx_list);
495 hw_desc = (char *) iop_chan->device->dma_desc_pool;
496 slot->async_tx.phys =
497 (dma_addr_t) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
500 spin_lock_bh(&iop_chan->lock);
501 iop_chan->slots_allocated++;
502 list_add_tail(&slot->slot_node, &iop_chan->all_slots);
503 spin_unlock_bh(&iop_chan->lock);
504 } while (iop_chan->slots_allocated < num_descs_in_pool);
506 if (idx && !iop_chan->last_used)
507 iop_chan->last_used = list_entry(iop_chan->all_slots.next,
508 struct iop_adma_desc_slot,
511 dev_dbg(iop_chan->device->common.dev,
512 "allocated %d descriptor slots last_used: %p\n",
513 iop_chan->slots_allocated, iop_chan->last_used);
515 /* initialize the channel and the chain with a null operation */
517 if (dma_has_cap(DMA_MEMCPY,
518 iop_chan->device->common.cap_mask))
519 iop_chan_start_null_memcpy(iop_chan);
520 else if (dma_has_cap(DMA_XOR,
521 iop_chan->device->common.cap_mask))
522 iop_chan_start_null_xor(iop_chan);
527 return (idx > 0) ? idx : -ENOMEM;
530 static struct dma_async_tx_descriptor *
531 iop_adma_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
533 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
534 struct iop_adma_desc_slot *sw_desc, *grp_start;
535 int slot_cnt, slots_per_op;
537 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
539 spin_lock_bh(&iop_chan->lock);
540 slot_cnt = iop_chan_interrupt_slot_count(&slots_per_op, iop_chan);
541 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
543 grp_start = sw_desc->group_head;
544 iop_desc_init_interrupt(grp_start, iop_chan);
545 grp_start->unmap_len = 0;
546 sw_desc->async_tx.flags = flags;
548 spin_unlock_bh(&iop_chan->lock);
550 return sw_desc ? &sw_desc->async_tx : NULL;
553 static struct dma_async_tx_descriptor *
554 iop_adma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
555 dma_addr_t dma_src, size_t len, unsigned long flags)
557 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
558 struct iop_adma_desc_slot *sw_desc, *grp_start;
559 int slot_cnt, slots_per_op;
563 BUG_ON(unlikely(len > IOP_ADMA_MAX_BYTE_COUNT));
565 dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
568 spin_lock_bh(&iop_chan->lock);
569 slot_cnt = iop_chan_memcpy_slot_count(len, &slots_per_op);
570 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
572 grp_start = sw_desc->group_head;
573 iop_desc_init_memcpy(grp_start, flags);
574 iop_desc_set_byte_count(grp_start, iop_chan, len);
575 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
576 iop_desc_set_memcpy_src_addr(grp_start, dma_src);
577 sw_desc->unmap_src_cnt = 1;
578 sw_desc->unmap_len = len;
579 sw_desc->async_tx.flags = flags;
581 spin_unlock_bh(&iop_chan->lock);
583 return sw_desc ? &sw_desc->async_tx : NULL;
586 static struct dma_async_tx_descriptor *
587 iop_adma_prep_dma_memset(struct dma_chan *chan, dma_addr_t dma_dest,
588 int value, size_t len, unsigned long flags)
590 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
591 struct iop_adma_desc_slot *sw_desc, *grp_start;
592 int slot_cnt, slots_per_op;
596 BUG_ON(unlikely(len > IOP_ADMA_MAX_BYTE_COUNT));
598 dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
601 spin_lock_bh(&iop_chan->lock);
602 slot_cnt = iop_chan_memset_slot_count(len, &slots_per_op);
603 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
605 grp_start = sw_desc->group_head;
606 iop_desc_init_memset(grp_start, flags);
607 iop_desc_set_byte_count(grp_start, iop_chan, len);
608 iop_desc_set_block_fill_val(grp_start, value);
609 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
610 sw_desc->unmap_src_cnt = 1;
611 sw_desc->unmap_len = len;
612 sw_desc->async_tx.flags = flags;
614 spin_unlock_bh(&iop_chan->lock);
616 return sw_desc ? &sw_desc->async_tx : NULL;
619 static struct dma_async_tx_descriptor *
620 iop_adma_prep_dma_xor(struct dma_chan *chan, dma_addr_t dma_dest,
621 dma_addr_t *dma_src, unsigned int src_cnt, size_t len,
624 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
625 struct iop_adma_desc_slot *sw_desc, *grp_start;
626 int slot_cnt, slots_per_op;
630 BUG_ON(unlikely(len > IOP_ADMA_XOR_MAX_BYTE_COUNT));
632 dev_dbg(iop_chan->device->common.dev,
633 "%s src_cnt: %d len: %u flags: %lx\n",
634 __func__, src_cnt, len, flags);
636 spin_lock_bh(&iop_chan->lock);
637 slot_cnt = iop_chan_xor_slot_count(len, src_cnt, &slots_per_op);
638 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
640 grp_start = sw_desc->group_head;
641 iop_desc_init_xor(grp_start, src_cnt, flags);
642 iop_desc_set_byte_count(grp_start, iop_chan, len);
643 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
644 sw_desc->unmap_src_cnt = src_cnt;
645 sw_desc->unmap_len = len;
646 sw_desc->async_tx.flags = flags;
648 iop_desc_set_xor_src_addr(grp_start, src_cnt,
651 spin_unlock_bh(&iop_chan->lock);
653 return sw_desc ? &sw_desc->async_tx : NULL;
656 static struct dma_async_tx_descriptor *
657 iop_adma_prep_dma_zero_sum(struct dma_chan *chan, dma_addr_t *dma_src,
658 unsigned int src_cnt, size_t len, u32 *result,
661 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
662 struct iop_adma_desc_slot *sw_desc, *grp_start;
663 int slot_cnt, slots_per_op;
668 dev_dbg(iop_chan->device->common.dev, "%s src_cnt: %d len: %u\n",
669 __func__, src_cnt, len);
671 spin_lock_bh(&iop_chan->lock);
672 slot_cnt = iop_chan_zero_sum_slot_count(len, src_cnt, &slots_per_op);
673 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
675 grp_start = sw_desc->group_head;
676 iop_desc_init_zero_sum(grp_start, src_cnt, flags);
677 iop_desc_set_zero_sum_byte_count(grp_start, len);
678 grp_start->xor_check_result = result;
679 pr_debug("\t%s: grp_start->xor_check_result: %p\n",
680 __func__, grp_start->xor_check_result);
681 sw_desc->unmap_src_cnt = src_cnt;
682 sw_desc->unmap_len = len;
683 sw_desc->async_tx.flags = flags;
685 iop_desc_set_zero_sum_src_addr(grp_start, src_cnt,
688 spin_unlock_bh(&iop_chan->lock);
690 return sw_desc ? &sw_desc->async_tx : NULL;
693 static void iop_adma_free_chan_resources(struct dma_chan *chan)
695 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
696 struct iop_adma_desc_slot *iter, *_iter;
697 int in_use_descs = 0;
699 iop_adma_slot_cleanup(iop_chan);
701 spin_lock_bh(&iop_chan->lock);
702 list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
705 list_del(&iter->chain_node);
707 list_for_each_entry_safe_reverse(
708 iter, _iter, &iop_chan->all_slots, slot_node) {
709 list_del(&iter->slot_node);
711 iop_chan->slots_allocated--;
713 iop_chan->last_used = NULL;
715 dev_dbg(iop_chan->device->common.dev, "%s slots_allocated %d\n",
716 __func__, iop_chan->slots_allocated);
717 spin_unlock_bh(&iop_chan->lock);
719 /* one is ok since we left it on there on purpose */
720 if (in_use_descs > 1)
721 printk(KERN_ERR "IOP: Freeing %d in use descriptors!\n",
726 * iop_adma_is_complete - poll the status of an ADMA transaction
727 * @chan: ADMA channel handle
728 * @cookie: ADMA transaction identifier
730 static enum dma_status iop_adma_is_complete(struct dma_chan *chan,
735 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
736 dma_cookie_t last_used;
737 dma_cookie_t last_complete;
740 last_used = chan->cookie;
741 last_complete = iop_chan->completed_cookie;
744 *done = last_complete;
748 ret = dma_async_is_complete(cookie, last_complete, last_used);
749 if (ret == DMA_SUCCESS)
752 iop_adma_slot_cleanup(iop_chan);
754 last_used = chan->cookie;
755 last_complete = iop_chan->completed_cookie;
758 *done = last_complete;
762 return dma_async_is_complete(cookie, last_complete, last_used);
765 static irqreturn_t iop_adma_eot_handler(int irq, void *data)
767 struct iop_adma_chan *chan = data;
769 dev_dbg(chan->device->common.dev, "%s\n", __func__);
771 tasklet_schedule(&chan->irq_tasklet);
773 iop_adma_device_clear_eot_status(chan);
778 static irqreturn_t iop_adma_eoc_handler(int irq, void *data)
780 struct iop_adma_chan *chan = data;
782 dev_dbg(chan->device->common.dev, "%s\n", __func__);
784 tasklet_schedule(&chan->irq_tasklet);
786 iop_adma_device_clear_eoc_status(chan);
791 static irqreturn_t iop_adma_err_handler(int irq, void *data)
793 struct iop_adma_chan *chan = data;
794 unsigned long status = iop_chan_get_status(chan);
796 dev_printk(KERN_ERR, chan->device->common.dev,
797 "error ( %s%s%s%s%s%s%s)\n",
798 iop_is_err_int_parity(status, chan) ? "int_parity " : "",
799 iop_is_err_mcu_abort(status, chan) ? "mcu_abort " : "",
800 iop_is_err_int_tabort(status, chan) ? "int_tabort " : "",
801 iop_is_err_int_mabort(status, chan) ? "int_mabort " : "",
802 iop_is_err_pci_tabort(status, chan) ? "pci_tabort " : "",
803 iop_is_err_pci_mabort(status, chan) ? "pci_mabort " : "",
804 iop_is_err_split_tx(status, chan) ? "split_tx " : "");
806 iop_adma_device_clear_err_status(chan);
813 static void iop_adma_issue_pending(struct dma_chan *chan)
815 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
817 if (iop_chan->pending) {
818 iop_chan->pending = 0;
819 iop_chan_append(iop_chan);
824 * Perform a transaction to verify the HW works.
826 #define IOP_ADMA_TEST_SIZE 2000
828 static int __devinit iop_adma_memcpy_self_test(struct iop_adma_device *device)
832 dma_addr_t src_dma, dest_dma;
833 struct dma_chan *dma_chan;
835 struct dma_async_tx_descriptor *tx;
837 struct iop_adma_chan *iop_chan;
839 dev_dbg(device->common.dev, "%s\n", __func__);
841 src = kmalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
844 dest = kzalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
850 /* Fill in src buffer */
851 for (i = 0; i < IOP_ADMA_TEST_SIZE; i++)
852 ((u8 *) src)[i] = (u8)i;
854 /* Start copy, using first DMA channel */
855 dma_chan = container_of(device->common.channels.next,
858 if (iop_adma_alloc_chan_resources(dma_chan, NULL) < 1) {
863 dest_dma = dma_map_single(dma_chan->device->dev, dest,
864 IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
865 src_dma = dma_map_single(dma_chan->device->dev, src,
866 IOP_ADMA_TEST_SIZE, DMA_TO_DEVICE);
867 tx = iop_adma_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
869 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
871 cookie = iop_adma_tx_submit(tx);
872 iop_adma_issue_pending(dma_chan);
875 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
877 dev_printk(KERN_ERR, dma_chan->device->dev,
878 "Self-test copy timed out, disabling\n");
883 iop_chan = to_iop_adma_chan(dma_chan);
884 dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
885 IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
886 if (memcmp(src, dest, IOP_ADMA_TEST_SIZE)) {
887 dev_printk(KERN_ERR, dma_chan->device->dev,
888 "Self-test copy failed compare, disabling\n");
894 iop_adma_free_chan_resources(dma_chan);
901 #define IOP_ADMA_NUM_SRC_TEST 4 /* must be <= 15 */
903 iop_adma_xor_zero_sum_self_test(struct iop_adma_device *device)
907 struct page *xor_srcs[IOP_ADMA_NUM_SRC_TEST];
908 struct page *zero_sum_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
909 dma_addr_t dma_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
910 dma_addr_t dma_addr, dest_dma;
911 struct dma_async_tx_descriptor *tx;
912 struct dma_chan *dma_chan;
918 struct iop_adma_chan *iop_chan;
920 dev_dbg(device->common.dev, "%s\n", __func__);
922 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
923 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
924 if (!xor_srcs[src_idx])
926 __free_page(xor_srcs[src_idx]);
931 dest = alloc_page(GFP_KERNEL);
934 __free_page(xor_srcs[src_idx]);
938 /* Fill in src buffers */
939 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
940 u8 *ptr = page_address(xor_srcs[src_idx]);
941 for (i = 0; i < PAGE_SIZE; i++)
942 ptr[i] = (1 << src_idx);
945 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++)
946 cmp_byte ^= (u8) (1 << src_idx);
948 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
949 (cmp_byte << 8) | cmp_byte;
951 memset(page_address(dest), 0, PAGE_SIZE);
953 dma_chan = container_of(device->common.channels.next,
956 if (iop_adma_alloc_chan_resources(dma_chan, NULL) < 1) {
962 dest_dma = dma_map_page(dma_chan->device->dev, dest, 0,
963 PAGE_SIZE, DMA_FROM_DEVICE);
964 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
965 dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
966 0, PAGE_SIZE, DMA_TO_DEVICE);
967 tx = iop_adma_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
968 IOP_ADMA_NUM_SRC_TEST, PAGE_SIZE,
969 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
971 cookie = iop_adma_tx_submit(tx);
972 iop_adma_issue_pending(dma_chan);
975 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
977 dev_printk(KERN_ERR, dma_chan->device->dev,
978 "Self-test xor timed out, disabling\n");
983 iop_chan = to_iop_adma_chan(dma_chan);
984 dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
985 PAGE_SIZE, DMA_FROM_DEVICE);
986 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
987 u32 *ptr = page_address(dest);
988 if (ptr[i] != cmp_word) {
989 dev_printk(KERN_ERR, dma_chan->device->dev,
990 "Self-test xor failed compare, disabling\n");
995 dma_sync_single_for_device(&iop_chan->device->pdev->dev, dest_dma,
996 PAGE_SIZE, DMA_TO_DEVICE);
998 /* skip zero sum if the capability is not present */
999 if (!dma_has_cap(DMA_ZERO_SUM, dma_chan->device->cap_mask))
1000 goto free_resources;
1002 /* zero sum the sources with the destintation page */
1003 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
1004 zero_sum_srcs[i] = xor_srcs[i];
1005 zero_sum_srcs[i] = dest;
1007 zero_sum_result = 1;
1009 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
1010 dma_srcs[i] = dma_map_page(dma_chan->device->dev,
1011 zero_sum_srcs[i], 0, PAGE_SIZE,
1013 tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
1014 IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
1016 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1018 cookie = iop_adma_tx_submit(tx);
1019 iop_adma_issue_pending(dma_chan);
1022 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1023 dev_printk(KERN_ERR, dma_chan->device->dev,
1024 "Self-test zero sum timed out, disabling\n");
1026 goto free_resources;
1029 if (zero_sum_result != 0) {
1030 dev_printk(KERN_ERR, dma_chan->device->dev,
1031 "Self-test zero sum failed compare, disabling\n");
1033 goto free_resources;
1037 dma_addr = dma_map_page(dma_chan->device->dev, dest, 0,
1038 PAGE_SIZE, DMA_FROM_DEVICE);
1039 tx = iop_adma_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
1040 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1042 cookie = iop_adma_tx_submit(tx);
1043 iop_adma_issue_pending(dma_chan);
1046 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1047 dev_printk(KERN_ERR, dma_chan->device->dev,
1048 "Self-test memset timed out, disabling\n");
1050 goto free_resources;
1053 for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
1054 u32 *ptr = page_address(dest);
1056 dev_printk(KERN_ERR, dma_chan->device->dev,
1057 "Self-test memset failed compare, disabling\n");
1059 goto free_resources;
1063 /* test for non-zero parity sum */
1064 zero_sum_result = 0;
1065 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
1066 dma_srcs[i] = dma_map_page(dma_chan->device->dev,
1067 zero_sum_srcs[i], 0, PAGE_SIZE,
1069 tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
1070 IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
1072 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1074 cookie = iop_adma_tx_submit(tx);
1075 iop_adma_issue_pending(dma_chan);
1078 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1079 dev_printk(KERN_ERR, dma_chan->device->dev,
1080 "Self-test non-zero sum timed out, disabling\n");
1082 goto free_resources;
1085 if (zero_sum_result != 1) {
1086 dev_printk(KERN_ERR, dma_chan->device->dev,
1087 "Self-test non-zero sum failed compare, disabling\n");
1089 goto free_resources;
1093 iop_adma_free_chan_resources(dma_chan);
1095 src_idx = IOP_ADMA_NUM_SRC_TEST;
1097 __free_page(xor_srcs[src_idx]);
1102 static int __devexit iop_adma_remove(struct platform_device *dev)
1104 struct iop_adma_device *device = platform_get_drvdata(dev);
1105 struct dma_chan *chan, *_chan;
1106 struct iop_adma_chan *iop_chan;
1108 struct iop_adma_platform_data *plat_data = dev->dev.platform_data;
1110 dma_async_device_unregister(&device->common);
1112 for (i = 0; i < 3; i++) {
1114 irq = platform_get_irq(dev, i);
1115 free_irq(irq, device);
1118 dma_free_coherent(&dev->dev, plat_data->pool_size,
1119 device->dma_desc_pool_virt, device->dma_desc_pool);
1122 struct resource *res;
1123 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1124 release_mem_region(res->start, res->end - res->start);
1127 list_for_each_entry_safe(chan, _chan, &device->common.channels,
1129 iop_chan = to_iop_adma_chan(chan);
1130 list_del(&chan->device_node);
1138 static int __devinit iop_adma_probe(struct platform_device *pdev)
1140 struct resource *res;
1142 struct iop_adma_device *adev;
1143 struct iop_adma_chan *iop_chan;
1144 struct dma_device *dma_dev;
1145 struct iop_adma_platform_data *plat_data = pdev->dev.platform_data;
1147 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1151 if (!devm_request_mem_region(&pdev->dev, res->start,
1152 res->end - res->start, pdev->name))
1155 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
1158 dma_dev = &adev->common;
1160 /* allocate coherent memory for hardware descriptors
1161 * note: writecombine gives slightly better performance, but
1162 * requires that we explicitly flush the writes
1164 if ((adev->dma_desc_pool_virt = dma_alloc_writecombine(&pdev->dev,
1165 plat_data->pool_size,
1166 &adev->dma_desc_pool,
1167 GFP_KERNEL)) == NULL) {
1172 dev_dbg(&pdev->dev, "%s: allocted descriptor pool virt %p phys %p\n",
1173 __func__, adev->dma_desc_pool_virt,
1174 (void *) adev->dma_desc_pool);
1176 adev->id = plat_data->hw_id;
1178 /* discover transaction capabilites from the platform data */
1179 dma_dev->cap_mask = plat_data->cap_mask;
1182 platform_set_drvdata(pdev, adev);
1184 INIT_LIST_HEAD(&dma_dev->channels);
1186 /* set base routines */
1187 dma_dev->device_alloc_chan_resources = iop_adma_alloc_chan_resources;
1188 dma_dev->device_free_chan_resources = iop_adma_free_chan_resources;
1189 dma_dev->device_is_tx_complete = iop_adma_is_complete;
1190 dma_dev->device_issue_pending = iop_adma_issue_pending;
1191 dma_dev->dev = &pdev->dev;
1193 /* set prep routines based on capability */
1194 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
1195 dma_dev->device_prep_dma_memcpy = iop_adma_prep_dma_memcpy;
1196 if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask))
1197 dma_dev->device_prep_dma_memset = iop_adma_prep_dma_memset;
1198 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
1199 dma_dev->max_xor = iop_adma_get_max_xor();
1200 dma_dev->device_prep_dma_xor = iop_adma_prep_dma_xor;
1202 if (dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask))
1203 dma_dev->device_prep_dma_zero_sum =
1204 iop_adma_prep_dma_zero_sum;
1205 if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
1206 dma_dev->device_prep_dma_interrupt =
1207 iop_adma_prep_dma_interrupt;
1209 iop_chan = kzalloc(sizeof(*iop_chan), GFP_KERNEL);
1214 iop_chan->device = adev;
1216 iop_chan->mmr_base = devm_ioremap(&pdev->dev, res->start,
1217 res->end - res->start);
1218 if (!iop_chan->mmr_base) {
1220 goto err_free_iop_chan;
1222 tasklet_init(&iop_chan->irq_tasklet, iop_adma_tasklet, (unsigned long)
1225 /* clear errors before enabling interrupts */
1226 iop_adma_device_clear_err_status(iop_chan);
1228 for (i = 0; i < 3; i++) {
1229 irq_handler_t handler[] = { iop_adma_eot_handler,
1230 iop_adma_eoc_handler,
1231 iop_adma_err_handler };
1232 int irq = platform_get_irq(pdev, i);
1235 goto err_free_iop_chan;
1237 ret = devm_request_irq(&pdev->dev, irq,
1238 handler[i], 0, pdev->name, iop_chan);
1240 goto err_free_iop_chan;
1244 spin_lock_init(&iop_chan->lock);
1245 INIT_LIST_HEAD(&iop_chan->chain);
1246 INIT_LIST_HEAD(&iop_chan->all_slots);
1247 INIT_RCU_HEAD(&iop_chan->common.rcu);
1248 iop_chan->common.device = dma_dev;
1249 list_add_tail(&iop_chan->common.device_node, &dma_dev->channels);
1251 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
1252 ret = iop_adma_memcpy_self_test(adev);
1253 dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1255 goto err_free_iop_chan;
1258 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask) ||
1259 dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) {
1260 ret = iop_adma_xor_zero_sum_self_test(adev);
1261 dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1263 goto err_free_iop_chan;
1266 dev_printk(KERN_INFO, &pdev->dev, "Intel(R) IOP: "
1267 "( %s%s%s%s%s%s%s%s%s%s)\n",
1268 dma_has_cap(DMA_PQ_XOR, dma_dev->cap_mask) ? "pq_xor " : "",
1269 dma_has_cap(DMA_PQ_UPDATE, dma_dev->cap_mask) ? "pq_update " : "",
1270 dma_has_cap(DMA_PQ_ZERO_SUM, dma_dev->cap_mask) ? "pq_zero_sum " : "",
1271 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
1272 dma_has_cap(DMA_DUAL_XOR, dma_dev->cap_mask) ? "dual_xor " : "",
1273 dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask) ? "xor_zero_sum " : "",
1274 dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "",
1275 dma_has_cap(DMA_MEMCPY_CRC32C, dma_dev->cap_mask) ? "cpy+crc " : "",
1276 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1277 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
1279 dma_async_device_register(dma_dev);
1285 dma_free_coherent(&adev->pdev->dev, plat_data->pool_size,
1286 adev->dma_desc_pool_virt, adev->dma_desc_pool);
1293 static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan)
1295 struct iop_adma_desc_slot *sw_desc, *grp_start;
1296 dma_cookie_t cookie;
1297 int slot_cnt, slots_per_op;
1299 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
1301 spin_lock_bh(&iop_chan->lock);
1302 slot_cnt = iop_chan_memcpy_slot_count(0, &slots_per_op);
1303 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
1305 grp_start = sw_desc->group_head;
1307 list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
1308 async_tx_ack(&sw_desc->async_tx);
1309 iop_desc_init_memcpy(grp_start, 0);
1310 iop_desc_set_byte_count(grp_start, iop_chan, 0);
1311 iop_desc_set_dest_addr(grp_start, iop_chan, 0);
1312 iop_desc_set_memcpy_src_addr(grp_start, 0);
1314 cookie = iop_chan->common.cookie;
1319 /* initialize the completed cookie to be less than
1320 * the most recently used cookie
1322 iop_chan->completed_cookie = cookie - 1;
1323 iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
1325 /* channel should not be busy */
1326 BUG_ON(iop_chan_is_busy(iop_chan));
1328 /* clear any prior error-status bits */
1329 iop_adma_device_clear_err_status(iop_chan);
1331 /* disable operation */
1332 iop_chan_disable(iop_chan);
1334 /* set the descriptor address */
1335 iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
1337 /* 1/ don't add pre-chained descriptors
1338 * 2/ dummy read to flush next_desc write
1340 BUG_ON(iop_desc_get_next_desc(sw_desc));
1342 /* run the descriptor */
1343 iop_chan_enable(iop_chan);
1345 dev_printk(KERN_ERR, iop_chan->device->common.dev,
1346 "failed to allocate null descriptor\n");
1347 spin_unlock_bh(&iop_chan->lock);
1350 static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan)
1352 struct iop_adma_desc_slot *sw_desc, *grp_start;
1353 dma_cookie_t cookie;
1354 int slot_cnt, slots_per_op;
1356 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
1358 spin_lock_bh(&iop_chan->lock);
1359 slot_cnt = iop_chan_xor_slot_count(0, 2, &slots_per_op);
1360 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
1362 grp_start = sw_desc->group_head;
1363 list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
1364 async_tx_ack(&sw_desc->async_tx);
1365 iop_desc_init_null_xor(grp_start, 2, 0);
1366 iop_desc_set_byte_count(grp_start, iop_chan, 0);
1367 iop_desc_set_dest_addr(grp_start, iop_chan, 0);
1368 iop_desc_set_xor_src_addr(grp_start, 0, 0);
1369 iop_desc_set_xor_src_addr(grp_start, 1, 0);
1371 cookie = iop_chan->common.cookie;
1376 /* initialize the completed cookie to be less than
1377 * the most recently used cookie
1379 iop_chan->completed_cookie = cookie - 1;
1380 iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
1382 /* channel should not be busy */
1383 BUG_ON(iop_chan_is_busy(iop_chan));
1385 /* clear any prior error-status bits */
1386 iop_adma_device_clear_err_status(iop_chan);
1388 /* disable operation */
1389 iop_chan_disable(iop_chan);
1391 /* set the descriptor address */
1392 iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
1394 /* 1/ don't add pre-chained descriptors
1395 * 2/ dummy read to flush next_desc write
1397 BUG_ON(iop_desc_get_next_desc(sw_desc));
1399 /* run the descriptor */
1400 iop_chan_enable(iop_chan);
1402 dev_printk(KERN_ERR, iop_chan->device->common.dev,
1403 "failed to allocate null descriptor\n");
1404 spin_unlock_bh(&iop_chan->lock);
1407 MODULE_ALIAS("platform:iop-adma");
1409 static struct platform_driver iop_adma_driver = {
1410 .probe = iop_adma_probe,
1411 .remove = iop_adma_remove,
1413 .owner = THIS_MODULE,
1418 static int __init iop_adma_init (void)
1420 return platform_driver_register(&iop_adma_driver);
1423 /* it's currently unsafe to unload this module */
1425 static void __exit iop_adma_exit (void)
1427 platform_driver_unregister(&iop_adma_driver);
1430 module_exit(iop_adma_exit);
1433 module_init(iop_adma_init);
1435 MODULE_AUTHOR("Intel Corporation");
1436 MODULE_DESCRIPTION("IOP ADMA Engine Driver");
1437 MODULE_LICENSE("GPL");