48 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52 #include <linux/module.h>
54 #include <linux/device.h>
64 #include <linux/slab.h>
69 static long dmaengine_ref_count;
84 return chan_dev->
chan;
90 unsigned long count = 0;
95 chan = dev_to_dma_chan(dev);
111 unsigned long count = 0;
116 chan = dev_to_dma_chan(dev);
119 count +=
per_cpu_ptr(chan->local, i)->bytes_transferred;
120 err =
sprintf(buf, "%lu\n", count);
134 chan = dev_to_dma_chan(dev);
151 static void chan_dev_release(
struct device *dev)
165 static struct class dma_devclass = {
167 .dev_attrs = dma_attrs,
168 .dev_release = chan_dev_release,
173 #define dma_device_satisfies_mask(device, mask) \
174 __dma_device_satisfies_mask((device), &(mask))
180 bitmap_and(has.bits, want->bits, device->
cap_mask.bits,
187 return chan->
device->dev->driver->owner;
196 static void balance_ref_count(
struct dma_chan *chan)
212 static int dma_chan_get(
struct dma_chan *chan)
215 struct module *owner = dma_chan_to_owner(chan);
220 }
else if (try_module_get(owner))
228 int desc_cnt = chan->
device->device_alloc_chan_resources(chan);
235 balance_ref_count(chan);
247 static void dma_chan_put(
struct dma_chan *chan)
252 module_put(dma_chan_to_owner(chan));
254 chan->
device->device_free_chan_resources(chan);
262 dma_async_issue_pending(chan);
264 status = dma_async_is_tx_complete(chan, cookie,
NULL,
NULL);
266 pr_err(
"%s: timeout!\n", __func__);
293 static int __init dma_channel_table_init(
void)
310 if (!channel_table[cap]) {
317 pr_err(
"initialization failure\n");
319 if (channel_table[cap])
344 if (chan && !is_dma_copy_aligned(chan->
device, 1, 1, 1))
360 list_for_each_entry_rcu(device, &dma_device_list, global_node) {
425 static void dma_channel_rebalance(
void)
442 chan->table_count = 0;
446 if (!dmaengine_ref_count)
454 chan = nth_chan(cap, n++);
456 chan = nth_chan(cap, -1);
467 if (!__dma_device_satisfies_mask(dev, mask)) {
468 pr_debug(
"%s: wrong capabilities\n", __func__);
484 __func__, dma_chan_name(chan));
487 if (fn && !
fn(chan, fn_param)) {
488 pr_debug(
"%s: %s filter said false\n",
489 __func__, dma_chan_name(chan));
513 chan = private_candidate(mask, device, fn, fn_param);
522 err = dma_chan_get(chan);
524 if (err == -ENODEV) {
526 __func__, dma_chan_name(chan));
529 pr_debug(
"%s: failed to get %s: (%d)\n",
530 __func__, dma_chan_name(chan), err);
542 chan ?
"success" :
"fail",
543 chan ? dma_chan_name(chan) : NULL);
556 if (--chan->
device->privatecnt == 0)
572 dmaengine_ref_count++;
579 err = dma_chan_get(chan);
580 if (err == -ENODEV) {
585 pr_debug(
"%s: failed to get %s: (%d)\n",
586 __func__, dma_chan_name(chan), err);
594 if (dmaengine_ref_count == 1)
595 dma_channel_rebalance();
609 dmaengine_ref_count--;
610 BUG_ON(dmaengine_ref_count < 0);
622 static bool device_has_all_tx_types(
struct dma_device *device)
628 #ifdef CONFIG_ASYNC_TX_DMA
633 #if defined(CONFIG_ASYNC_MEMCPY) || defined(CONFIG_ASYNC_MEMCPY_MODULE)
638 #if defined(CONFIG_ASYNC_MEMSET) || defined(CONFIG_ASYNC_MEMSET_MODULE)
643 #if defined(CONFIG_ASYNC_XOR) || defined(CONFIG_ASYNC_XOR_MODULE)
647 #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
653 #if defined(CONFIG_ASYNC_PQ) || defined(CONFIG_ASYNC_PQ_MODULE)
657 #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
666 static int get_dma_id(
struct dma_device *device)
730 if (device_has_all_tx_types(device))
736 rc = get_dma_id(device);
748 if (chan->
local == NULL)
751 if (chan->
dev == NULL) {
758 chan->
dev->device.class = &dma_devclass;
759 chan->
dev->device.parent = device->
dev;
760 chan->
dev->chan = chan;
761 chan->
dev->idr_ref = idr_ref;
786 if (dma_chan_get(chan) == -ENODEV) {
796 list_add_tail_rcu(&device->
global_node, &dma_device_list);
799 dma_channel_rebalance();
815 if (chan->
local == NULL)
840 dma_channel_rebalance();
845 "%s called while %d clients hold a reference\n",
870 void *
src,
size_t len)
918 unsigned int offset,
void *kdata,
size_t len)
965 unsigned int dest_off,
struct page *src_pg,
unsigned int src_off,
1002 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
1021 pr_err(
"%s timeout waiting for descriptor submission\n",
1052 for (; dep; dep = dep_next) {
1054 txd_clear_parent(dep);
1055 dep_next = txd_next(dep);
1056 if (dep_next && dep_next->
chan == chan)
1057 txd_clear_next(dep);
1065 chan->
device->device_issue_pending(chan);
1069 static int __init dma_bus_init(
void)