linux/drivers/dma/dmaengine.c
<<
>>
Prefs
   1/*
   2 * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License as published by the Free
   6 * Software Foundation; either version 2 of the License, or (at your option)
   7 * any later version.
   8 *
   9 * This program is distributed in the hope that 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
  12 * more details.
  13 *
  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., 59
  16 * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17 *
  18 * The full GNU General Public License is included in this distribution in the
  19 * file called COPYING.
  20 */
  21
  22/*
  23 * This code implements the DMA subsystem. It provides a HW-neutral interface
  24 * for other kernel code to use asynchronous memory copy capabilities,
  25 * if present, and allows different HW DMA drivers to register as providing
  26 * this capability.
  27 *
  28 * Due to the fact we are accelerating what is already a relatively fast
  29 * operation, the code goes to great lengths to avoid additional overhead,
  30 * such as locking.
  31 *
  32 * LOCKING:
  33 *
  34 * The subsystem keeps a global list of dma_device structs it is protected by a
  35 * mutex, dma_list_mutex.
  36 *
  37 * A subsystem can get access to a channel by calling dmaengine_get() followed
  38 * by dma_find_channel(), or if it has need for an exclusive channel it can call
  39 * dma_request_channel().  Once a channel is allocated a reference is taken
  40 * against its corresponding driver to disable removal.
  41 *
  42 * Each device has a channels list, which runs unlocked but is never modified
  43 * once the device is registered, it's just setup by the driver.
  44 *
  45 * See Documentation/dmaengine.txt for more details
  46 */
  47
  48#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  49
  50#include <linux/dma-mapping.h>
  51#include <linux/init.h>
  52#include <linux/module.h>
  53#include <linux/mm.h>
  54#include <linux/device.h>
  55#include <linux/dmaengine.h>
  56#include <linux/hardirq.h>
  57#include <linux/spinlock.h>
  58#include <linux/percpu.h>
  59#include <linux/rcupdate.h>
  60#include <linux/mutex.h>
  61#include <linux/jiffies.h>
  62#include <linux/rculist.h>
  63#include <linux/idr.h>
  64#include <linux/slab.h>
  65#include <linux/acpi.h>
  66#include <linux/acpi_dma.h>
  67#include <linux/of_dma.h>
  68
  69static DEFINE_MUTEX(dma_list_mutex);
  70static DEFINE_IDR(dma_idr);
  71static LIST_HEAD(dma_device_list);
  72static long dmaengine_ref_count;
  73
  74/* --- sysfs implementation --- */
  75
  76/**
  77 * dev_to_dma_chan - convert a device pointer to the its sysfs container object
  78 * @dev - device node
  79 *
  80 * Must be called under dma_list_mutex
  81 */
  82static struct dma_chan *dev_to_dma_chan(struct device *dev)
  83{
  84        struct dma_chan_dev *chan_dev;
  85
  86        chan_dev = container_of(dev, typeof(*chan_dev), device);
  87        return chan_dev->chan;
  88}
  89
  90static ssize_t show_memcpy_count(struct device *dev, struct device_attribute *attr, char *buf)
  91{
  92        struct dma_chan *chan;
  93        unsigned long count = 0;
  94        int i;
  95        int err;
  96
  97        mutex_lock(&dma_list_mutex);
  98        chan = dev_to_dma_chan(dev);
  99        if (chan) {
 100                for_each_possible_cpu(i)
 101                        count += per_cpu_ptr(chan->local, i)->memcpy_count;
 102                err = sprintf(buf, "%lu\n", count);
 103        } else
 104                err = -ENODEV;
 105        mutex_unlock(&dma_list_mutex);
 106
 107        return err;
 108}
 109
 110static ssize_t show_bytes_transferred(struct device *dev, struct device_attribute *attr,
 111                                      char *buf)
 112{
 113        struct dma_chan *chan;
 114        unsigned long count = 0;
 115        int i;
 116        int err;
 117
 118        mutex_lock(&dma_list_mutex);
 119        chan = dev_to_dma_chan(dev);
 120        if (chan) {
 121                for_each_possible_cpu(i)
 122                        count += per_cpu_ptr(chan->local, i)->bytes_transferred;
 123                err = sprintf(buf, "%lu\n", count);
 124        } else
 125                err = -ENODEV;
 126        mutex_unlock(&dma_list_mutex);
 127
 128        return err;
 129}
 130
 131static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf)
 132{
 133        struct dma_chan *chan;
 134        int err;
 135
 136        mutex_lock(&dma_list_mutex);
 137        chan = dev_to_dma_chan(dev);
 138        if (chan)
 139                err = sprintf(buf, "%d\n", chan->client_count);
 140        else
 141                err = -ENODEV;
 142        mutex_unlock(&dma_list_mutex);
 143
 144        return err;
 145}
 146
 147static struct device_attribute dma_attrs[] = {
 148        __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
 149        __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
 150        __ATTR(in_use, S_IRUGO, show_in_use, NULL),
 151        __ATTR_NULL
 152};
 153
 154static void chan_dev_release(struct device *dev)
 155{
 156        struct dma_chan_dev *chan_dev;
 157
 158        chan_dev = container_of(dev, typeof(*chan_dev), device);
 159        if (atomic_dec_and_test(chan_dev->idr_ref)) {
 160                mutex_lock(&dma_list_mutex);
 161                idr_remove(&dma_idr, chan_dev->dev_id);
 162                mutex_unlock(&dma_list_mutex);
 163                kfree(chan_dev->idr_ref);
 164        }
 165        kfree(chan_dev);
 166}
 167
 168static struct class dma_devclass = {
 169        .name           = "dma",
 170        .dev_attrs      = dma_attrs,
 171        .dev_release    = chan_dev_release,
 172};
 173
 174/* --- client and device registration --- */
 175
 176#define dma_device_satisfies_mask(device, mask) \
 177        __dma_device_satisfies_mask((device), &(mask))
 178static int
 179__dma_device_satisfies_mask(struct dma_device *device,
 180                            const dma_cap_mask_t *want)
 181{
 182        dma_cap_mask_t has;
 183
 184        bitmap_and(has.bits, want->bits, device->cap_mask.bits,
 185                DMA_TX_TYPE_END);
 186        return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
 187}
 188
 189static struct module *dma_chan_to_owner(struct dma_chan *chan)
 190{
 191        return chan->device->dev->driver->owner;
 192}
 193
 194/**
 195 * balance_ref_count - catch up the channel reference count
 196 * @chan - channel to balance ->client_count versus dmaengine_ref_count
 197 *
 198 * balance_ref_count must be called under dma_list_mutex
 199 */
 200static void balance_ref_count(struct dma_chan *chan)
 201{
 202        struct module *owner = dma_chan_to_owner(chan);
 203
 204        while (chan->client_count < dmaengine_ref_count) {
 205                __module_get(owner);
 206                chan->client_count++;
 207        }
 208}
 209
 210/**
 211 * dma_chan_get - try to grab a dma channel's parent driver module
 212 * @chan - channel to grab
 213 *
 214 * Must be called under dma_list_mutex
 215 */
 216static int dma_chan_get(struct dma_chan *chan)
 217{
 218        int err = -ENODEV;
 219        struct module *owner = dma_chan_to_owner(chan);
 220
 221        if (chan->client_count) {
 222                __module_get(owner);
 223                err = 0;
 224        } else if (try_module_get(owner))
 225                err = 0;
 226
 227        if (err == 0)
 228                chan->client_count++;
 229
 230        /* allocate upon first client reference */
 231        if (chan->client_count == 1 && err == 0) {
 232                int desc_cnt = chan->device->device_alloc_chan_resources(chan);
 233
 234                if (desc_cnt < 0) {
 235                        err = desc_cnt;
 236                        chan->client_count = 0;
 237                        module_put(owner);
 238                } else if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
 239                        balance_ref_count(chan);
 240        }
 241
 242        return err;
 243}
 244
 245/**
 246 * dma_chan_put - drop a reference to a dma channel's parent driver module
 247 * @chan - channel to release
 248 *
 249 * Must be called under dma_list_mutex
 250 */
 251static void dma_chan_put(struct dma_chan *chan)
 252{
 253        if (!chan->client_count)
 254                return; /* this channel failed alloc_chan_resources */
 255        chan->client_count--;
 256        module_put(dma_chan_to_owner(chan));
 257        if (chan->client_count == 0)
 258                chan->device->device_free_chan_resources(chan);
 259}
 260
 261enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
 262{
 263        enum dma_status status;
 264        unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
 265
 266        dma_async_issue_pending(chan);
 267        do {
 268                status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
 269                if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
 270                        pr_err("%s: timeout!\n", __func__);
 271                        return DMA_ERROR;
 272                }
 273                if (status != DMA_IN_PROGRESS)
 274                        break;
 275                cpu_relax();
 276        } while (1);
 277
 278        return status;
 279}
 280EXPORT_SYMBOL(dma_sync_wait);
 281
 282/**
 283 * dma_cap_mask_all - enable iteration over all operation types
 284 */
 285static dma_cap_mask_t dma_cap_mask_all;
 286
 287/**
 288 * dma_chan_tbl_ent - tracks channel allocations per core/operation
 289 * @chan - associated channel for this entry
 290 */
 291struct dma_chan_tbl_ent {
 292        struct dma_chan *chan;
 293};
 294
 295/**
 296 * channel_table - percpu lookup table for memory-to-memory offload providers
 297 */
 298static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END];
 299
 300static int __init dma_channel_table_init(void)
 301{
 302        enum dma_transaction_type cap;
 303        int err = 0;
 304
 305        bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
 306
 307        /* 'interrupt', 'private', and 'slave' are channel capabilities,
 308         * but are not associated with an operation so they do not need
 309         * an entry in the channel_table
 310         */
 311        clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
 312        clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
 313        clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
 314
 315        for_each_dma_cap_mask(cap, dma_cap_mask_all) {
 316                channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
 317                if (!channel_table[cap]) {
 318                        err = -ENOMEM;
 319                        break;
 320                }
 321        }
 322
 323        if (err) {
 324                pr_err("initialization failure\n");
 325                for_each_dma_cap_mask(cap, dma_cap_mask_all)
 326                        if (channel_table[cap])
 327                                free_percpu(channel_table[cap]);
 328        }
 329
 330        return err;
 331}
 332arch_initcall(dma_channel_table_init);
 333
 334/**
 335 * dma_find_channel - find a channel to carry out the operation
 336 * @tx_type: transaction type
 337 */
 338struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
 339{
 340        return this_cpu_read(channel_table[tx_type]->chan);
 341}
 342EXPORT_SYMBOL(dma_find_channel);
 343
 344/*
 345 * net_dma_find_channel - find a channel for net_dma
 346 * net_dma has alignment requirements
 347 */
 348struct dma_chan *net_dma_find_channel(void)
 349{
 350        struct dma_chan *chan = dma_find_channel(DMA_MEMCPY);
 351        if (chan && !is_dma_copy_aligned(chan->device, 1, 1, 1))
 352                return NULL;
 353
 354        return chan;
 355}
 356EXPORT_SYMBOL(net_dma_find_channel);
 357
 358/**
 359 * dma_issue_pending_all - flush all pending operations across all channels
 360 */
 361void dma_issue_pending_all(void)
 362{
 363        struct dma_device *device;
 364        struct dma_chan *chan;
 365
 366        rcu_read_lock();
 367        list_for_each_entry_rcu(device, &dma_device_list, global_node) {
 368                if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
 369                        continue;
 370                list_for_each_entry(chan, &device->channels, device_node)
 371                        if (chan->client_count)
 372                                device->device_issue_pending(chan);
 373        }
 374        rcu_read_unlock();
 375}
 376EXPORT_SYMBOL(dma_issue_pending_all);
 377
 378/**
 379 * nth_chan - returns the nth channel of the given capability
 380 * @cap: capability to match
 381 * @n: nth channel desired
 382 *
 383 * Defaults to returning the channel with the desired capability and the
 384 * lowest reference count when 'n' cannot be satisfied.  Must be called
 385 * under dma_list_mutex.
 386 */
 387static struct dma_chan *nth_chan(enum dma_transaction_type cap, int n)
 388{
 389        struct dma_device *device;
 390        struct dma_chan *chan;
 391        struct dma_chan *ret = NULL;
 392        struct dma_chan *min = NULL;
 393
 394        list_for_each_entry(device, &dma_device_list, global_node) {
 395                if (!dma_has_cap(cap, device->cap_mask) ||
 396                    dma_has_cap(DMA_PRIVATE, device->cap_mask))
 397                        continue;
 398                list_for_each_entry(chan, &device->channels, device_node) {
 399                        if (!chan->client_count)
 400                                continue;
 401                        if (!min)
 402                                min = chan;
 403                        else if (chan->table_count < min->table_count)
 404                                min = chan;
 405
 406                        if (n-- == 0) {
 407                                ret = chan;
 408                                break; /* done */
 409                        }
 410                }
 411                if (ret)
 412                        break; /* done */
 413        }
 414
 415        if (!ret)
 416                ret = min;
 417
 418        if (ret)
 419                ret->table_count++;
 420
 421        return ret;
 422}
 423
 424/**
 425 * dma_channel_rebalance - redistribute the available channels
 426 *
 427 * Optimize for cpu isolation (each cpu gets a dedicated channel for an
 428 * operation type) in the SMP case,  and operation isolation (avoid
 429 * multi-tasking channels) in the non-SMP case.  Must be called under
 430 * dma_list_mutex.
 431 */
 432static void dma_channel_rebalance(void)
 433{
 434        struct dma_chan *chan;
 435        struct dma_device *device;
 436        int cpu;
 437        int cap;
 438        int n;
 439
 440        /* undo the last distribution */
 441        for_each_dma_cap_mask(cap, dma_cap_mask_all)
 442                for_each_possible_cpu(cpu)
 443                        per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
 444
 445        list_for_each_entry(device, &dma_device_list, global_node) {
 446                if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
 447                        continue;
 448                list_for_each_entry(chan, &device->channels, device_node)
 449                        chan->table_count = 0;
 450        }
 451
 452        /* don't populate the channel_table if no clients are available */
 453        if (!dmaengine_ref_count)
 454                return;
 455
 456        /* redistribute available channels */
 457        n = 0;
 458        for_each_dma_cap_mask(cap, dma_cap_mask_all)
 459                for_each_online_cpu(cpu) {
 460                        if (num_possible_cpus() > 1)
 461                                chan = nth_chan(cap, n++);
 462                        else
 463                                chan = nth_chan(cap, -1);
 464
 465                        per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
 466                }
 467}
 468
 469static struct dma_chan *private_candidate(const dma_cap_mask_t *mask,
 470                                          struct dma_device *dev,
 471                                          dma_filter_fn fn, void *fn_param)
 472{
 473        struct dma_chan *chan;
 474
 475        if (!__dma_device_satisfies_mask(dev, mask)) {
 476                pr_debug("%s: wrong capabilities\n", __func__);
 477                return NULL;
 478        }
 479        /* devices with multiple channels need special handling as we need to
 480         * ensure that all channels are either private or public.
 481         */
 482        if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
 483                list_for_each_entry(chan, &dev->channels, device_node) {
 484                        /* some channels are already publicly allocated */
 485                        if (chan->client_count)
 486                                return NULL;
 487                }
 488
 489        list_for_each_entry(chan, &dev->channels, device_node) {
 490                if (chan->client_count) {
 491                        pr_debug("%s: %s busy\n",
 492                                 __func__, dma_chan_name(chan));
 493                        continue;
 494                }
 495                if (fn && !fn(chan, fn_param)) {
 496                        pr_debug("%s: %s filter said false\n",
 497                                 __func__, dma_chan_name(chan));
 498                        continue;
 499                }
 500                return chan;
 501        }
 502
 503        return NULL;
 504}
 505
 506/**
 507 * dma_request_channel - try to allocate an exclusive channel
 508 * @mask: capabilities that the channel must satisfy
 509 * @fn: optional callback to disposition available channels
 510 * @fn_param: opaque parameter to pass to dma_filter_fn
 511 */
 512struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
 513                                       dma_filter_fn fn, void *fn_param)
 514{
 515        struct dma_device *device, *_d;
 516        struct dma_chan *chan = NULL;
 517        int err;
 518
 519        /* Find a channel */
 520        mutex_lock(&dma_list_mutex);
 521        list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
 522                chan = private_candidate(mask, device, fn, fn_param);
 523                if (chan) {
 524                        /* Found a suitable channel, try to grab, prep, and
 525                         * return it.  We first set DMA_PRIVATE to disable
 526                         * balance_ref_count as this channel will not be
 527                         * published in the general-purpose allocator
 528                         */
 529                        dma_cap_set(DMA_PRIVATE, device->cap_mask);
 530                        device->privatecnt++;
 531                        err = dma_chan_get(chan);
 532
 533                        if (err == -ENODEV) {
 534                                pr_debug("%s: %s module removed\n",
 535                                         __func__, dma_chan_name(chan));
 536                                list_del_rcu(&device->global_node);
 537                        } else if (err)
 538                                pr_debug("%s: failed to get %s: (%d)\n",
 539                                         __func__, dma_chan_name(chan), err);
 540                        else
 541                                break;
 542                        if (--device->privatecnt == 0)
 543                                dma_cap_clear(DMA_PRIVATE, device->cap_mask);
 544                        chan = NULL;
 545                }
 546        }
 547        mutex_unlock(&dma_list_mutex);
 548
 549        pr_debug("%s: %s (%s)\n",
 550                 __func__,
 551                 chan ? "success" : "fail",
 552                 chan ? dma_chan_name(chan) : NULL);
 553
 554        return chan;
 555}
 556EXPORT_SYMBOL_GPL(__dma_request_channel);
 557
 558/**
 559 * dma_request_slave_channel - try to allocate an exclusive slave channel
 560 * @dev:        pointer to client device structure
 561 * @name:       slave channel name
 562 */
 563struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name)
 564{
 565        /* If device-tree is present get slave info from here */
 566        if (dev->of_node)
 567                return of_dma_request_slave_channel(dev->of_node, name);
 568
 569        /* If device was enumerated by ACPI get slave info from here */
 570        if (ACPI_HANDLE(dev))
 571                return acpi_dma_request_slave_chan_by_name(dev, name);
 572
 573        return NULL;
 574}
 575EXPORT_SYMBOL_GPL(dma_request_slave_channel);
 576
 577void dma_release_channel(struct dma_chan *chan)
 578{
 579        mutex_lock(&dma_list_mutex);
 580        WARN_ONCE(chan->client_count != 1,
 581                  "chan reference count %d != 1\n", chan->client_count);
 582        dma_chan_put(chan);
 583        /* drop PRIVATE cap enabled by __dma_request_channel() */
 584        if (--chan->device->privatecnt == 0)
 585                dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
 586        mutex_unlock(&dma_list_mutex);
 587}
 588EXPORT_SYMBOL_GPL(dma_release_channel);
 589
 590/**
 591 * dmaengine_get - register interest in dma_channels
 592 */
 593void dmaengine_get(void)
 594{
 595        struct dma_device *device, *_d;
 596        struct dma_chan *chan;
 597        int err;
 598
 599        mutex_lock(&dma_list_mutex);
 600        dmaengine_ref_count++;
 601
 602        /* try to grab channels */
 603        list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
 604                if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
 605                        continue;
 606                list_for_each_entry(chan, &device->channels, device_node) {
 607                        err = dma_chan_get(chan);
 608                        if (err == -ENODEV) {
 609                                /* module removed before we could use it */
 610                                list_del_rcu(&device->global_node);
 611                                break;
 612                        } else if (err)
 613                                pr_debug("%s: failed to get %s: (%d)\n",
 614                                       __func__, dma_chan_name(chan), err);
 615                }
 616        }
 617
 618        /* if this is the first reference and there were channels
 619         * waiting we need to rebalance to get those channels
 620         * incorporated into the channel table
 621         */
 622        if (dmaengine_ref_count == 1)
 623                dma_channel_rebalance();
 624        mutex_unlock(&dma_list_mutex);
 625}
 626EXPORT_SYMBOL(dmaengine_get);
 627
 628/**
 629 * dmaengine_put - let dma drivers be removed when ref_count == 0
 630 */
 631void dmaengine_put(void)
 632{
 633        struct dma_device *device;
 634        struct dma_chan *chan;
 635
 636        mutex_lock(&dma_list_mutex);
 637        dmaengine_ref_count--;
 638        BUG_ON(dmaengine_ref_count < 0);
 639        /* drop channel references */
 640        list_for_each_entry(device, &dma_device_list, global_node) {
 641                if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
 642                        continue;
 643                list_for_each_entry(chan, &device->channels, device_node)
 644                        dma_chan_put(chan);
 645        }
 646        mutex_unlock(&dma_list_mutex);
 647}
 648EXPORT_SYMBOL(dmaengine_put);
 649
 650static bool device_has_all_tx_types(struct dma_device *device)
 651{
 652        /* A device that satisfies this test has channels that will never cause
 653         * an async_tx channel switch event as all possible operation types can
 654         * be handled.
 655         */
 656        #ifdef CONFIG_ASYNC_TX_DMA
 657        if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
 658                return false;
 659        #endif
 660
 661        #if defined(CONFIG_ASYNC_MEMCPY) || defined(CONFIG_ASYNC_MEMCPY_MODULE)
 662        if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
 663                return false;
 664        #endif
 665
 666        #if defined(CONFIG_ASYNC_MEMSET) || defined(CONFIG_ASYNC_MEMSET_MODULE)
 667        if (!dma_has_cap(DMA_MEMSET, device->cap_mask))
 668                return false;
 669        #endif
 670
 671        #if defined(CONFIG_ASYNC_XOR) || defined(CONFIG_ASYNC_XOR_MODULE)
 672        if (!dma_has_cap(DMA_XOR, device->cap_mask))
 673                return false;
 674
 675        #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
 676        if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
 677                return false;
 678        #endif
 679        #endif
 680
 681        #if defined(CONFIG_ASYNC_PQ) || defined(CONFIG_ASYNC_PQ_MODULE)
 682        if (!dma_has_cap(DMA_PQ, device->cap_mask))
 683                return false;
 684
 685        #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
 686        if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
 687                return false;
 688        #endif
 689        #endif
 690
 691        return true;
 692}
 693
 694static int get_dma_id(struct dma_device *device)
 695{
 696        int rc;
 697
 698        mutex_lock(&dma_list_mutex);
 699
 700        rc = idr_alloc(&dma_idr, NULL, 0, 0, GFP_KERNEL);
 701        if (rc >= 0)
 702                device->dev_id = rc;
 703
 704        mutex_unlock(&dma_list_mutex);
 705        return rc < 0 ? rc : 0;
 706}
 707
 708/**
 709 * dma_async_device_register - registers DMA devices found
 710 * @device: &dma_device
 711 */
 712int dma_async_device_register(struct dma_device *device)
 713{
 714        int chancnt = 0, rc;
 715        struct dma_chan* chan;
 716        atomic_t *idr_ref;
 717
 718        if (!device)
 719                return -ENODEV;
 720
 721        /* validate device routines */
 722        BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
 723                !device->device_prep_dma_memcpy);
 724        BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
 725                !device->device_prep_dma_xor);
 726        BUG_ON(dma_has_cap(DMA_XOR_VAL, device->cap_mask) &&
 727                !device->device_prep_dma_xor_val);
 728        BUG_ON(dma_has_cap(DMA_PQ, device->cap_mask) &&
 729                !device->device_prep_dma_pq);
 730        BUG_ON(dma_has_cap(DMA_PQ_VAL, device->cap_mask) &&
 731                !device->device_prep_dma_pq_val);
 732        BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
 733                !device->device_prep_dma_memset);
 734        BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
 735                !device->device_prep_dma_interrupt);
 736        BUG_ON(dma_has_cap(DMA_SG, device->cap_mask) &&
 737                !device->device_prep_dma_sg);
 738        BUG_ON(dma_has_cap(DMA_CYCLIC, device->cap_mask) &&
 739                !device->device_prep_dma_cyclic);
 740        BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
 741                !device->device_control);
 742        BUG_ON(dma_has_cap(DMA_INTERLEAVE, device->cap_mask) &&
 743                !device->device_prep_interleaved_dma);
 744
 745        BUG_ON(!device->device_alloc_chan_resources);
 746        BUG_ON(!device->device_free_chan_resources);
 747        BUG_ON(!device->device_tx_status);
 748        BUG_ON(!device->device_issue_pending);
 749        BUG_ON(!device->dev);
 750
 751        /* note: this only matters in the
 752         * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
 753         */
 754        if (device_has_all_tx_types(device))
 755                dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
 756
 757        idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
 758        if (!idr_ref)
 759                return -ENOMEM;
 760        rc = get_dma_id(device);
 761        if (rc != 0) {
 762                kfree(idr_ref);
 763                return rc;
 764        }
 765
 766        atomic_set(idr_ref, 0);
 767
 768        /* represent channels in sysfs. Probably want devs too */
 769        list_for_each_entry(chan, &device->channels, device_node) {
 770                rc = -ENOMEM;
 771                chan->local = alloc_percpu(typeof(*chan->local));
 772                if (chan->local == NULL)
 773                        goto err_out;
 774                chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
 775                if (chan->dev == NULL) {
 776                        free_percpu(chan->local);
 777                        chan->local = NULL;
 778                        goto err_out;
 779                }
 780
 781                chan->chan_id = chancnt++;
 782                chan->dev->device.class = &dma_devclass;
 783                chan->dev->device.parent = device->dev;
 784                chan->dev->chan = chan;
 785                chan->dev->idr_ref = idr_ref;
 786                chan->dev->dev_id = device->dev_id;
 787                atomic_inc(idr_ref);
 788                dev_set_name(&chan->dev->device, "dma%dchan%d",
 789                             device->dev_id, chan->chan_id);
 790
 791                rc = device_register(&chan->dev->device);
 792                if (rc) {
 793                        free_percpu(chan->local);
 794                        chan->local = NULL;
 795                        kfree(chan->dev);
 796                        atomic_dec(idr_ref);
 797                        goto err_out;
 798                }
 799                chan->client_count = 0;
 800        }
 801        device->chancnt = chancnt;
 802
 803        mutex_lock(&dma_list_mutex);
 804        /* take references on public channels */
 805        if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
 806                list_for_each_entry(chan, &device->channels, device_node) {
 807                        /* if clients are already waiting for channels we need
 808                         * to take references on their behalf
 809                         */
 810                        if (dma_chan_get(chan) == -ENODEV) {
 811                                /* note we can only get here for the first
 812                                 * channel as the remaining channels are
 813                                 * guaranteed to get a reference
 814                                 */
 815                                rc = -ENODEV;
 816                                mutex_unlock(&dma_list_mutex);
 817                                goto err_out;
 818                        }
 819                }
 820        list_add_tail_rcu(&device->global_node, &dma_device_list);
 821        if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
 822                device->privatecnt++;   /* Always private */
 823        dma_channel_rebalance();
 824        mutex_unlock(&dma_list_mutex);
 825
 826        return 0;
 827
 828err_out:
 829        /* if we never registered a channel just release the idr */
 830        if (atomic_read(idr_ref) == 0) {
 831                mutex_lock(&dma_list_mutex);
 832                idr_remove(&dma_idr, device->dev_id);
 833                mutex_unlock(&dma_list_mutex);
 834                kfree(idr_ref);
 835                return rc;
 836        }
 837
 838        list_for_each_entry(chan, &device->channels, device_node) {
 839                if (chan->local == NULL)
 840                        continue;
 841                mutex_lock(&dma_list_mutex);
 842                chan->dev->chan = NULL;
 843                mutex_unlock(&dma_list_mutex);
 844                device_unregister(&chan->dev->device);
 845                free_percpu(chan->local);
 846        }
 847        return rc;
 848}
 849EXPORT_SYMBOL(dma_async_device_register);
 850
 851/**
 852 * dma_async_device_unregister - unregister a DMA device
 853 * @device: &dma_device
 854 *
 855 * This routine is called by dma driver exit routines, dmaengine holds module
 856 * references to prevent it being called while channels are in use.
 857 */
 858void dma_async_device_unregister(struct dma_device *device)
 859{
 860        struct dma_chan *chan;
 861
 862        mutex_lock(&dma_list_mutex);
 863        list_del_rcu(&device->global_node);
 864        dma_channel_rebalance();
 865        mutex_unlock(&dma_list_mutex);
 866
 867        list_for_each_entry(chan, &device->channels, device_node) {
 868                WARN_ONCE(chan->client_count,
 869                          "%s called while %d clients hold a reference\n",
 870                          __func__, chan->client_count);
 871                mutex_lock(&dma_list_mutex);
 872                chan->dev->chan = NULL;
 873                mutex_unlock(&dma_list_mutex);
 874                device_unregister(&chan->dev->device);
 875                free_percpu(chan->local);
 876        }
 877}
 878EXPORT_SYMBOL(dma_async_device_unregister);
 879
 880/**
 881 * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
 882 * @chan: DMA channel to offload copy to
 883 * @dest: destination address (virtual)
 884 * @src: source address (virtual)
 885 * @len: length
 886 *
 887 * Both @dest and @src must be mappable to a bus address according to the
 888 * DMA mapping API rules for streaming mappings.
 889 * Both @dest and @src must stay memory resident (kernel memory or locked
 890 * user space pages).
 891 */
 892dma_cookie_t
 893dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
 894                        void *src, size_t len)
 895{
 896        struct dma_device *dev = chan->device;
 897        struct dma_async_tx_descriptor *tx;
 898        dma_addr_t dma_dest, dma_src;
 899        dma_cookie_t cookie;
 900        unsigned long flags;
 901
 902        dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
 903        dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
 904        flags = DMA_CTRL_ACK |
 905                DMA_COMPL_SRC_UNMAP_SINGLE |
 906                DMA_COMPL_DEST_UNMAP_SINGLE;
 907        tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
 908
 909        if (!tx) {
 910                dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
 911                dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
 912                return -ENOMEM;
 913        }
 914
 915        tx->callback = NULL;
 916        cookie = tx->tx_submit(tx);
 917
 918        preempt_disable();
 919        __this_cpu_add(chan->local->bytes_transferred, len);
 920        __this_cpu_inc(chan->local->memcpy_count);
 921        preempt_enable();
 922
 923        return cookie;
 924}
 925EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
 926
 927/**
 928 * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
 929 * @chan: DMA channel to offload copy to
 930 * @page: destination page
 931 * @offset: offset in page to copy to
 932 * @kdata: source address (virtual)
 933 * @len: length
 934 *
 935 * Both @page/@offset and @kdata must be mappable to a bus address according
 936 * to the DMA mapping API rules for streaming mappings.
 937 * Both @page/@offset and @kdata must stay memory resident (kernel memory or
 938 * locked user space pages)
 939 */
 940dma_cookie_t
 941dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
 942                        unsigned int offset, void *kdata, size_t len)
 943{
 944        struct dma_device *dev = chan->device;
 945        struct dma_async_tx_descriptor *tx;
 946        dma_addr_t dma_dest, dma_src;
 947        dma_cookie_t cookie;
 948        unsigned long flags;
 949
 950        dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
 951        dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
 952        flags = DMA_CTRL_ACK | DMA_COMPL_SRC_UNMAP_SINGLE;
 953        tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
 954
 955        if (!tx) {
 956                dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
 957                dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
 958                return -ENOMEM;
 959        }
 960
 961        tx->callback = NULL;
 962        cookie = tx->tx_submit(tx);
 963
 964        preempt_disable();
 965        __this_cpu_add(chan->local->bytes_transferred, len);
 966        __this_cpu_inc(chan->local->memcpy_count);
 967        preempt_enable();
 968
 969        return cookie;
 970}
 971EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
 972
 973/**
 974 * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
 975 * @chan: DMA channel to offload copy to
 976 * @dest_pg: destination page
 977 * @dest_off: offset in page to copy to
 978 * @src_pg: source page
 979 * @src_off: offset in page to copy from
 980 * @len: length
 981 *
 982 * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
 983 * address according to the DMA mapping API rules for streaming mappings.
 984 * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
 985 * (kernel memory or locked user space pages).
 986 */
 987dma_cookie_t
 988dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
 989        unsigned int dest_off, struct page *src_pg, unsigned int src_off,
 990        size_t len)
 991{
 992        struct dma_device *dev = chan->device;
 993        struct dma_async_tx_descriptor *tx;
 994        dma_addr_t dma_dest, dma_src;
 995        dma_cookie_t cookie;
 996        unsigned long flags;
 997
 998        dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
 999        dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
1000                                DMA_FROM_DEVICE);
1001        flags = DMA_CTRL_ACK;
1002        tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
1003
1004        if (!tx) {
1005                dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
1006                dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
1007                return -ENOMEM;
1008        }
1009
1010        tx->callback = NULL;
1011        cookie = tx->tx_submit(tx);
1012
1013        preempt_disable();
1014        __this_cpu_add(chan->local->bytes_transferred, len);
1015        __this_cpu_inc(chan->local->memcpy_count);
1016        preempt_enable();
1017
1018        return cookie;
1019}
1020EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
1021
1022void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1023        struct dma_chan *chan)
1024{
1025        tx->chan = chan;
1026        #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
1027        spin_lock_init(&tx->lock);
1028        #endif
1029}
1030EXPORT_SYMBOL(dma_async_tx_descriptor_init);
1031
1032/* dma_wait_for_async_tx - spin wait for a transaction to complete
1033 * @tx: in-flight transaction to wait on
1034 */
1035enum dma_status
1036dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1037{
1038        unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
1039
1040        if (!tx)
1041                return DMA_SUCCESS;
1042
1043        while (tx->cookie == -EBUSY) {
1044                if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
1045                        pr_err("%s timeout waiting for descriptor submission\n",
1046                               __func__);
1047                        return DMA_ERROR;
1048                }
1049                cpu_relax();
1050        }
1051        return dma_sync_wait(tx->chan, tx->cookie);
1052}
1053EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
1054
1055/* dma_run_dependencies - helper routine for dma drivers to process
1056 *      (start) dependent operations on their target channel
1057 * @tx: transaction with dependencies
1058 */
1059void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
1060{
1061        struct dma_async_tx_descriptor *dep = txd_next(tx);
1062        struct dma_async_tx_descriptor *dep_next;
1063        struct dma_chan *chan;
1064
1065        if (!dep)
1066                return;
1067
1068        /* we'll submit tx->next now, so clear the link */
1069        txd_clear_next(tx);
1070        chan = dep->chan;
1071
1072        /* keep submitting up until a channel switch is detected
1073         * in that case we will be called again as a result of
1074         * processing the interrupt from async_tx_channel_switch
1075         */
1076        for (; dep; dep = dep_next) {
1077                txd_lock(dep);
1078                txd_clear_parent(dep);
1079                dep_next = txd_next(dep);
1080                if (dep_next && dep_next->chan == chan)
1081                        txd_clear_next(dep); /* ->next will be submitted */
1082                else
1083                        dep_next = NULL; /* submit current dep and terminate */
1084                txd_unlock(dep);
1085
1086                dep->tx_submit(dep);
1087        }
1088
1089        chan->device->device_issue_pending(chan);
1090}
1091EXPORT_SYMBOL_GPL(dma_run_dependencies);
1092
1093static int __init dma_bus_init(void)
1094{
1095        return class_register(&dma_devclass);
1096}
1097arch_initcall(dma_bus_init);
1098
1099
1100