blob: 5532bb8b500cd9b7dfec304fe077122b57700512 [file] [log] [blame]
Chris Leechc13c8262006-05-23 17:18:44 -07001/*
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#ifndef DMAENGINE_H
22#define DMAENGINE_H
David Woodhouse1c0f16e2006-06-27 02:53:56 -070023
Chris Leechc13c8262006-05-23 17:18:44 -070024#include <linux/device.h>
25#include <linux/uio.h>
Vinod Koul90b44f82011-07-25 19:57:52 +053026#include <linux/scatterlist.h>
Paul Gortmakera8efa9d2011-07-29 16:55:11 +100027#include <linux/bitmap.h>
28#include <asm/page.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000029
Chris Leechc13c8262006-05-23 17:18:44 -070030/**
Randy Dunlapfe4ada22006-07-03 19:44:51 -070031 * typedef dma_cookie_t - an opaque DMA cookie
Chris Leechc13c8262006-05-23 17:18:44 -070032 *
33 * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
34 */
35typedef s32 dma_cookie_t;
Steven J. Magnani76bd0612010-02-28 22:18:16 -070036#define DMA_MIN_COOKIE 1
37#define DMA_MAX_COOKIE INT_MAX
Chris Leechc13c8262006-05-23 17:18:44 -070038
39#define dma_submit_error(cookie) ((cookie) < 0 ? 1 : 0)
40
41/**
42 * enum dma_status - DMA transaction status
43 * @DMA_SUCCESS: transaction completed successfully
44 * @DMA_IN_PROGRESS: transaction not yet processed
Linus Walleij07934482010-03-26 16:50:49 -070045 * @DMA_PAUSED: transaction is paused
Chris Leechc13c8262006-05-23 17:18:44 -070046 * @DMA_ERROR: transaction failed
47 */
48enum dma_status {
49 DMA_SUCCESS,
50 DMA_IN_PROGRESS,
Linus Walleij07934482010-03-26 16:50:49 -070051 DMA_PAUSED,
Chris Leechc13c8262006-05-23 17:18:44 -070052 DMA_ERROR,
53};
54
55/**
Dan Williams7405f742007-01-02 11:10:43 -070056 * enum dma_transaction_type - DMA transaction types/indexes
Dan Williams138f4c32009-09-08 17:42:51 -070057 *
58 * Note: The DMA_ASYNC_TX capability is not to be set by drivers. It is
59 * automatically set as dma devices are registered.
Dan Williams7405f742007-01-02 11:10:43 -070060 */
61enum dma_transaction_type {
62 DMA_MEMCPY,
63 DMA_XOR,
Dan Williamsb2f46fd2009-07-14 12:20:36 -070064 DMA_PQ,
Dan Williams099f53c2009-04-08 14:28:37 -070065 DMA_XOR_VAL,
66 DMA_PQ_VAL,
Dan Williams7405f742007-01-02 11:10:43 -070067 DMA_MEMSET,
Dan Williams7405f742007-01-02 11:10:43 -070068 DMA_INTERRUPT,
Ira Snydera86ee032010-09-30 11:46:44 +000069 DMA_SG,
Dan Williams59b5ec22009-01-06 11:38:15 -070070 DMA_PRIVATE,
Dan Williams138f4c32009-09-08 17:42:51 -070071 DMA_ASYNC_TX,
Haavard Skinnemoendc0ee642008-07-08 11:59:35 -070072 DMA_SLAVE,
Sascha Hauer782bc952010-09-30 13:56:32 +000073 DMA_CYCLIC,
Jassi Brarb14dab72011-10-13 12:33:30 +053074 DMA_INTERLEAVE,
Dan Williams7405f742007-01-02 11:10:43 -070075/* last transaction type for creation of the capabilities mask */
Jassi Brarb14dab72011-10-13 12:33:30 +053076 DMA_TX_TYPE_END,
77};
Haavard Skinnemoendc0ee642008-07-08 11:59:35 -070078
Vinod Koul49920bc2011-10-13 15:15:27 +053079/**
80 * enum dma_transfer_direction - dma transfer mode and direction indicator
81 * @DMA_MEM_TO_MEM: Async/Memcpy mode
82 * @DMA_MEM_TO_DEV: Slave mode & From Memory to Device
83 * @DMA_DEV_TO_MEM: Slave mode & From Device to Memory
84 * @DMA_DEV_TO_DEV: Slave mode & From Device to Device
85 */
86enum dma_transfer_direction {
87 DMA_MEM_TO_MEM,
88 DMA_MEM_TO_DEV,
89 DMA_DEV_TO_MEM,
90 DMA_DEV_TO_DEV,
91};
Dan Williams7405f742007-01-02 11:10:43 -070092
93/**
Jassi Brarb14dab72011-10-13 12:33:30 +053094 * Interleaved Transfer Request
95 * ----------------------------
96 * A chunk is collection of contiguous bytes to be transfered.
97 * The gap(in bytes) between two chunks is called inter-chunk-gap(ICG).
98 * ICGs may or maynot change between chunks.
99 * A FRAME is the smallest series of contiguous {chunk,icg} pairs,
100 * that when repeated an integral number of times, specifies the transfer.
101 * A transfer template is specification of a Frame, the number of times
102 * it is to be repeated and other per-transfer attributes.
103 *
104 * Practically, a client driver would have ready a template for each
105 * type of transfer it is going to need during its lifetime and
106 * set only 'src_start' and 'dst_start' before submitting the requests.
107 *
108 *
109 * | Frame-1 | Frame-2 | ~ | Frame-'numf' |
110 * |====....==.===...=...|====....==.===...=...| ~ |====....==.===...=...|
111 *
112 * == Chunk size
113 * ... ICG
114 */
115
116/**
117 * struct data_chunk - Element of scatter-gather list that makes a frame.
118 * @size: Number of bytes to read from source.
119 * size_dst := fn(op, size_src), so doesn't mean much for destination.
120 * @icg: Number of bytes to jump after last src/dst address of this
121 * chunk and before first src/dst address for next chunk.
122 * Ignored for dst(assumed 0), if dst_inc is true and dst_sgl is false.
123 * Ignored for src(assumed 0), if src_inc is true and src_sgl is false.
124 */
125struct data_chunk {
126 size_t size;
127 size_t icg;
128};
129
130/**
131 * struct dma_interleaved_template - Template to convey DMAC the transfer pattern
132 * and attributes.
133 * @src_start: Bus address of source for the first chunk.
134 * @dst_start: Bus address of destination for the first chunk.
135 * @dir: Specifies the type of Source and Destination.
136 * @src_inc: If the source address increments after reading from it.
137 * @dst_inc: If the destination address increments after writing to it.
138 * @src_sgl: If the 'icg' of sgl[] applies to Source (scattered read).
139 * Otherwise, source is read contiguously (icg ignored).
140 * Ignored if src_inc is false.
141 * @dst_sgl: If the 'icg' of sgl[] applies to Destination (scattered write).
142 * Otherwise, destination is filled contiguously (icg ignored).
143 * Ignored if dst_inc is false.
144 * @numf: Number of frames in this template.
145 * @frame_size: Number of chunks in a frame i.e, size of sgl[].
146 * @sgl: Array of {chunk,icg} pairs that make up a frame.
147 */
148struct dma_interleaved_template {
149 dma_addr_t src_start;
150 dma_addr_t dst_start;
151 enum dma_transfer_direction dir;
152 bool src_inc;
153 bool dst_inc;
154 bool src_sgl;
155 bool dst_sgl;
156 size_t numf;
157 size_t frame_size;
158 struct data_chunk sgl[0];
159};
160
161/**
Dan Williams636bdeaa2008-04-17 20:17:26 -0700162 * enum dma_ctrl_flags - DMA flags to augment operation preparation,
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700163 * control completion, and communicate status.
Dan Williamsd4c56f92008-02-02 19:49:58 -0700164 * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700165 * this transaction
Guennadi Liakhovetskia88f6662009-12-10 18:35:15 +0100166 * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700167 * acknowledges receipt, i.e. has has a chance to establish any dependency
168 * chains
Dan Williamse1d181e2008-07-04 00:13:40 -0700169 * @DMA_COMPL_SKIP_SRC_UNMAP - set to disable dma-unmapping the source buffer(s)
170 * @DMA_COMPL_SKIP_DEST_UNMAP - set to disable dma-unmapping the destination(s)
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200171 * @DMA_COMPL_SRC_UNMAP_SINGLE - set to do the source dma-unmapping as single
172 * (if not set, do the source dma-unmapping as page)
173 * @DMA_COMPL_DEST_UNMAP_SINGLE - set to do the destination dma-unmapping as single
174 * (if not set, do the destination dma-unmapping as page)
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700175 * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q
176 * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P
177 * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as
178 * sources that were the result of a previous operation, in the case of a PQ
179 * operation it continues the calculation with new sources
Dan Williams0403e382009-09-08 17:42:50 -0700180 * @DMA_PREP_FENCE - tell the driver that subsequent operations depend
181 * on the result of this operation
Dan Williamsd4c56f92008-02-02 19:49:58 -0700182 */
Dan Williams636bdeaa2008-04-17 20:17:26 -0700183enum dma_ctrl_flags {
Dan Williamsd4c56f92008-02-02 19:49:58 -0700184 DMA_PREP_INTERRUPT = (1 << 0),
Dan Williams636bdeaa2008-04-17 20:17:26 -0700185 DMA_CTRL_ACK = (1 << 1),
Dan Williamse1d181e2008-07-04 00:13:40 -0700186 DMA_COMPL_SKIP_SRC_UNMAP = (1 << 2),
187 DMA_COMPL_SKIP_DEST_UNMAP = (1 << 3),
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200188 DMA_COMPL_SRC_UNMAP_SINGLE = (1 << 4),
189 DMA_COMPL_DEST_UNMAP_SINGLE = (1 << 5),
Dan Williamsf9dd2132009-09-08 17:42:29 -0700190 DMA_PREP_PQ_DISABLE_P = (1 << 6),
191 DMA_PREP_PQ_DISABLE_Q = (1 << 7),
192 DMA_PREP_CONTINUE = (1 << 8),
Dan Williams0403e382009-09-08 17:42:50 -0700193 DMA_PREP_FENCE = (1 << 9),
Dan Williamsd4c56f92008-02-02 19:49:58 -0700194};
195
196/**
Linus Walleijc3635c72010-03-26 16:44:01 -0700197 * enum dma_ctrl_cmd - DMA operations that can optionally be exercised
198 * on a running channel.
199 * @DMA_TERMINATE_ALL: terminate all ongoing transfers
200 * @DMA_PAUSE: pause ongoing transfers
201 * @DMA_RESUME: resume paused transfer
Linus Walleijc156d0a2010-08-04 13:37:33 +0200202 * @DMA_SLAVE_CONFIG: this command is only implemented by DMA controllers
203 * that need to runtime reconfigure the slave channels (as opposed to passing
204 * configuration data in statically from the platform). An additional
205 * argument of struct dma_slave_config must be passed in with this
206 * command.
Ira Snyder968f19a2010-09-30 11:46:46 +0000207 * @FSLDMA_EXTERNAL_START: this command will put the Freescale DMA controller
208 * into external start mode.
Linus Walleijc3635c72010-03-26 16:44:01 -0700209 */
210enum dma_ctrl_cmd {
211 DMA_TERMINATE_ALL,
212 DMA_PAUSE,
213 DMA_RESUME,
Linus Walleijc156d0a2010-08-04 13:37:33 +0200214 DMA_SLAVE_CONFIG,
Ira Snyder968f19a2010-09-30 11:46:46 +0000215 FSLDMA_EXTERNAL_START,
Linus Walleijc3635c72010-03-26 16:44:01 -0700216};
217
218/**
Dan Williamsad283ea2009-08-29 19:09:26 -0700219 * enum sum_check_bits - bit position of pq_check_flags
220 */
221enum sum_check_bits {
222 SUM_CHECK_P = 0,
223 SUM_CHECK_Q = 1,
224};
225
226/**
227 * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations
228 * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
229 * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
230 */
231enum sum_check_flags {
232 SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
233 SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
234};
235
236
237/**
Dan Williams7405f742007-01-02 11:10:43 -0700238 * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
239 * See linux/cpumask.h
240 */
241typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
242
243/**
Chris Leechc13c8262006-05-23 17:18:44 -0700244 * struct dma_chan_percpu - the per-CPU part of struct dma_chan
Chris Leechc13c8262006-05-23 17:18:44 -0700245 * @memcpy_count: transaction counter
246 * @bytes_transferred: byte counter
247 */
248
249struct dma_chan_percpu {
Chris Leechc13c8262006-05-23 17:18:44 -0700250 /* stats */
251 unsigned long memcpy_count;
252 unsigned long bytes_transferred;
253};
254
255/**
256 * struct dma_chan - devices supply DMA channels, clients use them
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700257 * @device: ptr to the dma device who supplies this channel, always !%NULL
Chris Leechc13c8262006-05-23 17:18:44 -0700258 * @cookie: last cookie value returned to client
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700259 * @chan_id: channel ID for sysfs
Dan Williams41d5e592009-01-06 11:38:21 -0700260 * @dev: class device for sysfs
Chris Leechc13c8262006-05-23 17:18:44 -0700261 * @device_node: used to add this to the device chan list
262 * @local: per-cpu pointer to a struct dma_chan_percpu
Dan Williams7cc5bf92008-07-08 11:58:21 -0700263 * @client-count: how many clients are using this channel
Dan Williamsbec08512009-01-06 11:38:14 -0700264 * @table_count: number of appearances in the mem-to-mem allocation table
Dan Williams287d8592009-02-18 14:48:26 -0800265 * @private: private data for certain client-channel associations
Chris Leechc13c8262006-05-23 17:18:44 -0700266 */
267struct dma_chan {
Chris Leechc13c8262006-05-23 17:18:44 -0700268 struct dma_device *device;
269 dma_cookie_t cookie;
270
271 /* sysfs */
272 int chan_id;
Dan Williams41d5e592009-01-06 11:38:21 -0700273 struct dma_chan_dev *dev;
Chris Leechc13c8262006-05-23 17:18:44 -0700274
Chris Leechc13c8262006-05-23 17:18:44 -0700275 struct list_head device_node;
Tejun Heoa29d8b82010-02-02 14:39:15 +0900276 struct dma_chan_percpu __percpu *local;
Dan Williams7cc5bf92008-07-08 11:58:21 -0700277 int client_count;
Dan Williamsbec08512009-01-06 11:38:14 -0700278 int table_count;
Dan Williams287d8592009-02-18 14:48:26 -0800279 void *private;
Chris Leechc13c8262006-05-23 17:18:44 -0700280};
281
Dan Williams41d5e592009-01-06 11:38:21 -0700282/**
283 * struct dma_chan_dev - relate sysfs device node to backing channel device
284 * @chan - driver channel device
285 * @device - sysfs device
Dan Williams864498a2009-01-06 11:38:21 -0700286 * @dev_id - parent dma_device dev_id
287 * @idr_ref - reference count to gate release of dma_device dev_id
Dan Williams41d5e592009-01-06 11:38:21 -0700288 */
289struct dma_chan_dev {
290 struct dma_chan *chan;
291 struct device device;
Dan Williams864498a2009-01-06 11:38:21 -0700292 int dev_id;
293 atomic_t *idr_ref;
Dan Williams41d5e592009-01-06 11:38:21 -0700294};
295
Linus Walleijc156d0a2010-08-04 13:37:33 +0200296/**
297 * enum dma_slave_buswidth - defines bus with of the DMA slave
298 * device, source or target buses
299 */
300enum dma_slave_buswidth {
301 DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
302 DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
303 DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
304 DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
305 DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
306};
307
308/**
309 * struct dma_slave_config - dma slave channel runtime config
310 * @direction: whether the data shall go in or out on this slave
311 * channel, right now. DMA_TO_DEVICE and DMA_FROM_DEVICE are
312 * legal values, DMA_BIDIRECTIONAL is not acceptable since we
313 * need to differentiate source and target addresses.
314 * @src_addr: this is the physical address where DMA slave data
315 * should be read (RX), if the source is memory this argument is
316 * ignored.
317 * @dst_addr: this is the physical address where DMA slave data
318 * should be written (TX), if the source is memory this argument
319 * is ignored.
320 * @src_addr_width: this is the width in bytes of the source (RX)
321 * register where DMA data shall be read. If the source
322 * is memory this may be ignored depending on architecture.
323 * Legal values: 1, 2, 4, 8.
324 * @dst_addr_width: same as src_addr_width but for destination
325 * target (TX) mutatis mutandis.
326 * @src_maxburst: the maximum number of words (note: words, as in
327 * units of the src_addr_width member, not bytes) that can be sent
328 * in one burst to the device. Typically something like half the
329 * FIFO depth on I/O peripherals so you don't overflow it. This
330 * may or may not be applicable on memory sources.
331 * @dst_maxburst: same as src_maxburst but for destination target
332 * mutatis mutandis.
333 *
334 * This struct is passed in as configuration data to a DMA engine
335 * in order to set up a certain channel for DMA transport at runtime.
336 * The DMA device/engine has to provide support for an additional
337 * command in the channel config interface, DMA_SLAVE_CONFIG
338 * and this struct will then be passed in as an argument to the
339 * DMA engine device_control() function.
340 *
341 * The rationale for adding configuration information to this struct
342 * is as follows: if it is likely that most DMA slave controllers in
343 * the world will support the configuration option, then make it
344 * generic. If not: if it is fixed so that it be sent in static from
345 * the platform data, then prefer to do that. Else, if it is neither
346 * fixed at runtime, nor generic enough (such as bus mastership on
347 * some CPU family and whatnot) then create a custom slave config
348 * struct and pass that, then make this config a member of that
349 * struct, if applicable.
350 */
351struct dma_slave_config {
Vinod Koul49920bc2011-10-13 15:15:27 +0530352 enum dma_transfer_direction direction;
Linus Walleijc156d0a2010-08-04 13:37:33 +0200353 dma_addr_t src_addr;
354 dma_addr_t dst_addr;
355 enum dma_slave_buswidth src_addr_width;
356 enum dma_slave_buswidth dst_addr_width;
357 u32 src_maxburst;
358 u32 dst_maxburst;
359};
360
Dan Williams41d5e592009-01-06 11:38:21 -0700361static inline const char *dma_chan_name(struct dma_chan *chan)
362{
363 return dev_name(&chan->dev->device);
364}
Dan Williamsd379b012007-07-09 11:56:42 -0700365
Chris Leechc13c8262006-05-23 17:18:44 -0700366void dma_chan_cleanup(struct kref *kref);
367
Chris Leechc13c8262006-05-23 17:18:44 -0700368/**
Dan Williams59b5ec22009-01-06 11:38:15 -0700369 * typedef dma_filter_fn - callback filter for dma_request_channel
370 * @chan: channel to be reviewed
371 * @filter_param: opaque parameter passed through dma_request_channel
372 *
373 * When this optional parameter is specified in a call to dma_request_channel a
374 * suitable channel is passed to this routine for further dispositioning before
375 * being returned. Where 'suitable' indicates a non-busy channel that
Dan Williams7dd60252009-01-06 11:38:19 -0700376 * satisfies the given capability mask. It returns 'true' to indicate that the
377 * channel is suitable.
Dan Williams59b5ec22009-01-06 11:38:15 -0700378 */
Dan Williams7dd60252009-01-06 11:38:19 -0700379typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
Dan Williams59b5ec22009-01-06 11:38:15 -0700380
Dan Williams7405f742007-01-02 11:10:43 -0700381typedef void (*dma_async_tx_callback)(void *dma_async_param);
382/**
383 * struct dma_async_tx_descriptor - async transaction descriptor
384 * ---dma generic offload fields---
385 * @cookie: tracking cookie for this transaction, set to -EBUSY if
386 * this tx is sitting on a dependency list
Dan Williams636bdeaa2008-04-17 20:17:26 -0700387 * @flags: flags to augment operation preparation, control completion, and
388 * communicate status
Dan Williams7405f742007-01-02 11:10:43 -0700389 * @phys: physical address of the descriptor
Dan Williams7405f742007-01-02 11:10:43 -0700390 * @chan: target channel for this operation
391 * @tx_submit: set the prepared descriptor(s) to be executed by the engine
Dan Williams7405f742007-01-02 11:10:43 -0700392 * @callback: routine to call after this operation is complete
393 * @callback_param: general parameter to pass to the callback routine
394 * ---async_tx api specific fields---
Dan Williams19242d72008-04-17 20:17:25 -0700395 * @next: at completion submit this descriptor
Dan Williams7405f742007-01-02 11:10:43 -0700396 * @parent: pointer to the next level up in the dependency chain
Dan Williams19242d72008-04-17 20:17:25 -0700397 * @lock: protect the parent and next pointers
Dan Williams7405f742007-01-02 11:10:43 -0700398 */
399struct dma_async_tx_descriptor {
400 dma_cookie_t cookie;
Dan Williams636bdeaa2008-04-17 20:17:26 -0700401 enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
Dan Williams7405f742007-01-02 11:10:43 -0700402 dma_addr_t phys;
Dan Williams7405f742007-01-02 11:10:43 -0700403 struct dma_chan *chan;
404 dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
Dan Williams7405f742007-01-02 11:10:43 -0700405 dma_async_tx_callback callback;
406 void *callback_param;
Dan Williams5fc6d892010-10-07 16:44:50 -0700407#ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williams19242d72008-04-17 20:17:25 -0700408 struct dma_async_tx_descriptor *next;
Dan Williams7405f742007-01-02 11:10:43 -0700409 struct dma_async_tx_descriptor *parent;
410 spinlock_t lock;
Dan Williamscaa20d972010-05-17 16:24:16 -0700411#endif
Dan Williams7405f742007-01-02 11:10:43 -0700412};
413
Dan Williams5fc6d892010-10-07 16:44:50 -0700414#ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williamscaa20d972010-05-17 16:24:16 -0700415static inline void txd_lock(struct dma_async_tx_descriptor *txd)
416{
417}
418static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
419{
420}
421static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
422{
423 BUG();
424}
425static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
426{
427}
428static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
429{
430}
431static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
432{
433 return NULL;
434}
435static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
436{
437 return NULL;
438}
439
440#else
441static inline void txd_lock(struct dma_async_tx_descriptor *txd)
442{
443 spin_lock_bh(&txd->lock);
444}
445static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
446{
447 spin_unlock_bh(&txd->lock);
448}
449static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
450{
451 txd->next = next;
452 next->parent = txd;
453}
454static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
455{
456 txd->parent = NULL;
457}
458static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
459{
460 txd->next = NULL;
461}
462static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
463{
464 return txd->parent;
465}
466static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
467{
468 return txd->next;
469}
470#endif
471
Chris Leechc13c8262006-05-23 17:18:44 -0700472/**
Linus Walleij07934482010-03-26 16:50:49 -0700473 * struct dma_tx_state - filled in to report the status of
474 * a transfer.
475 * @last: last completed DMA cookie
476 * @used: last issued DMA cookie (i.e. the one in progress)
477 * @residue: the remaining number of bytes left to transmit
478 * on the selected transfer for states DMA_IN_PROGRESS and
479 * DMA_PAUSED if this is implemented in the driver, else 0
480 */
481struct dma_tx_state {
482 dma_cookie_t last;
483 dma_cookie_t used;
484 u32 residue;
485};
486
487/**
Chris Leechc13c8262006-05-23 17:18:44 -0700488 * struct dma_device - info on the entity supplying DMA services
489 * @chancnt: how many DMA channels are supported
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900490 * @privatecnt: how many DMA channels are requested by dma_request_channel
Chris Leechc13c8262006-05-23 17:18:44 -0700491 * @channels: the list of struct dma_chan
492 * @global_node: list_head for global dma_device_list
Dan Williams7405f742007-01-02 11:10:43 -0700493 * @cap_mask: one or more dma_capability flags
494 * @max_xor: maximum number of xor sources, 0 if no capability
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700495 * @max_pq: maximum number of PQ sources and PQ-continue capability
Dan Williams83544ae2009-09-08 17:42:53 -0700496 * @copy_align: alignment shift for memcpy operations
497 * @xor_align: alignment shift for xor operations
498 * @pq_align: alignment shift for pq operations
499 * @fill_align: alignment shift for memset operations
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700500 * @dev_id: unique device ID
Dan Williams7405f742007-01-02 11:10:43 -0700501 * @dev: struct device reference for dma mapping api
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700502 * @device_alloc_chan_resources: allocate resources and return the
503 * number of allocated descriptors
504 * @device_free_chan_resources: release DMA channel's resources
Dan Williams7405f742007-01-02 11:10:43 -0700505 * @device_prep_dma_memcpy: prepares a memcpy operation
506 * @device_prep_dma_xor: prepares a xor operation
Dan Williams099f53c2009-04-08 14:28:37 -0700507 * @device_prep_dma_xor_val: prepares a xor validation operation
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700508 * @device_prep_dma_pq: prepares a pq operation
509 * @device_prep_dma_pq_val: prepares a pqzero_sum operation
Dan Williams7405f742007-01-02 11:10:43 -0700510 * @device_prep_dma_memset: prepares a memset operation
511 * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
Haavard Skinnemoendc0ee642008-07-08 11:59:35 -0700512 * @device_prep_slave_sg: prepares a slave dma operation
Sascha Hauer782bc952010-09-30 13:56:32 +0000513 * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
514 * The function takes a buffer of size buf_len. The callback function will
515 * be called after period_len bytes have been transferred.
Jassi Brarb14dab72011-10-13 12:33:30 +0530516 * @device_prep_interleaved_dma: Transfer expression in a generic way.
Linus Walleijc3635c72010-03-26 16:44:01 -0700517 * @device_control: manipulate all pending operations on a channel, returns
518 * zero or error code
Linus Walleij07934482010-03-26 16:50:49 -0700519 * @device_tx_status: poll for transaction completion, the optional
520 * txstate parameter can be supplied with a pointer to get a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300521 * struct with auxiliary transfer status information, otherwise the call
Linus Walleij07934482010-03-26 16:50:49 -0700522 * will just return a simple status code
Dan Williams7405f742007-01-02 11:10:43 -0700523 * @device_issue_pending: push pending transactions to hardware
Chris Leechc13c8262006-05-23 17:18:44 -0700524 */
525struct dma_device {
526
527 unsigned int chancnt;
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900528 unsigned int privatecnt;
Chris Leechc13c8262006-05-23 17:18:44 -0700529 struct list_head channels;
530 struct list_head global_node;
Dan Williams7405f742007-01-02 11:10:43 -0700531 dma_cap_mask_t cap_mask;
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700532 unsigned short max_xor;
533 unsigned short max_pq;
Dan Williams83544ae2009-09-08 17:42:53 -0700534 u8 copy_align;
535 u8 xor_align;
536 u8 pq_align;
537 u8 fill_align;
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700538 #define DMA_HAS_PQ_CONTINUE (1 << 15)
Chris Leechc13c8262006-05-23 17:18:44 -0700539
Chris Leechc13c8262006-05-23 17:18:44 -0700540 int dev_id;
Dan Williams7405f742007-01-02 11:10:43 -0700541 struct device *dev;
Chris Leechc13c8262006-05-23 17:18:44 -0700542
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700543 int (*device_alloc_chan_resources)(struct dma_chan *chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700544 void (*device_free_chan_resources)(struct dma_chan *chan);
Dan Williams7405f742007-01-02 11:10:43 -0700545
546 struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
Dan Williams00367312008-02-02 19:49:57 -0700547 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700548 size_t len, unsigned long flags);
Dan Williams7405f742007-01-02 11:10:43 -0700549 struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
Dan Williams00367312008-02-02 19:49:57 -0700550 struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700551 unsigned int src_cnt, size_t len, unsigned long flags);
Dan Williams099f53c2009-04-08 14:28:37 -0700552 struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
Dan Williams00367312008-02-02 19:49:57 -0700553 struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
Dan Williamsad283ea2009-08-29 19:09:26 -0700554 size_t len, enum sum_check_flags *result, unsigned long flags);
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700555 struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
556 struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
557 unsigned int src_cnt, const unsigned char *scf,
558 size_t len, unsigned long flags);
559 struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
560 struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
561 unsigned int src_cnt, const unsigned char *scf, size_t len,
562 enum sum_check_flags *pqres, unsigned long flags);
Dan Williams7405f742007-01-02 11:10:43 -0700563 struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
Dan Williams00367312008-02-02 19:49:57 -0700564 struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700565 unsigned long flags);
Dan Williams7405f742007-01-02 11:10:43 -0700566 struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
Dan Williams636bdeaa2008-04-17 20:17:26 -0700567 struct dma_chan *chan, unsigned long flags);
Ira Snydera86ee032010-09-30 11:46:44 +0000568 struct dma_async_tx_descriptor *(*device_prep_dma_sg)(
569 struct dma_chan *chan,
570 struct scatterlist *dst_sg, unsigned int dst_nents,
571 struct scatterlist *src_sg, unsigned int src_nents,
572 unsigned long flags);
Dan Williams7405f742007-01-02 11:10:43 -0700573
Haavard Skinnemoendc0ee642008-07-08 11:59:35 -0700574 struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
575 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Koul49920bc2011-10-13 15:15:27 +0530576 unsigned int sg_len, enum dma_transfer_direction direction,
Haavard Skinnemoendc0ee642008-07-08 11:59:35 -0700577 unsigned long flags);
Sascha Hauer782bc952010-09-30 13:56:32 +0000578 struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
579 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Vinod Koul49920bc2011-10-13 15:15:27 +0530580 size_t period_len, enum dma_transfer_direction direction);
Jassi Brarb14dab72011-10-13 12:33:30 +0530581 struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
582 struct dma_chan *chan, struct dma_interleaved_template *xt,
583 unsigned long flags);
Linus Walleij05827632010-05-17 16:30:42 -0700584 int (*device_control)(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
585 unsigned long arg);
Haavard Skinnemoendc0ee642008-07-08 11:59:35 -0700586
Linus Walleij07934482010-03-26 16:50:49 -0700587 enum dma_status (*device_tx_status)(struct dma_chan *chan,
588 dma_cookie_t cookie,
589 struct dma_tx_state *txstate);
Dan Williams7405f742007-01-02 11:10:43 -0700590 void (*device_issue_pending)(struct dma_chan *chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700591};
592
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000593static inline int dmaengine_device_control(struct dma_chan *chan,
594 enum dma_ctrl_cmd cmd,
595 unsigned long arg)
596{
597 return chan->device->device_control(chan, cmd, arg);
598}
599
600static inline int dmaengine_slave_config(struct dma_chan *chan,
601 struct dma_slave_config *config)
602{
603 return dmaengine_device_control(chan, DMA_SLAVE_CONFIG,
604 (unsigned long)config);
605}
606
Vinod Koul90b44f82011-07-25 19:57:52 +0530607static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
608 struct dma_chan *chan, void *buf, size_t len,
Vinod Koul49920bc2011-10-13 15:15:27 +0530609 enum dma_transfer_direction dir, unsigned long flags)
Vinod Koul90b44f82011-07-25 19:57:52 +0530610{
611 struct scatterlist sg;
612 sg_init_one(&sg, buf, len);
613
614 return chan->device->device_prep_slave_sg(chan, &sg, 1, dir, flags);
615}
616
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000617static inline int dmaengine_terminate_all(struct dma_chan *chan)
618{
619 return dmaengine_device_control(chan, DMA_TERMINATE_ALL, 0);
620}
621
622static inline int dmaengine_pause(struct dma_chan *chan)
623{
624 return dmaengine_device_control(chan, DMA_PAUSE, 0);
625}
626
627static inline int dmaengine_resume(struct dma_chan *chan)
628{
629 return dmaengine_device_control(chan, DMA_RESUME, 0);
630}
631
Russell King - ARM Linux98d530f2011-01-01 23:00:23 +0000632static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000633{
634 return desc->tx_submit(desc);
635}
636
Dan Williams83544ae2009-09-08 17:42:53 -0700637static inline bool dmaengine_check_align(u8 align, size_t off1, size_t off2, size_t len)
638{
639 size_t mask;
640
641 if (!align)
642 return true;
643 mask = (1 << align) - 1;
644 if (mask & (off1 | off2 | len))
645 return false;
646 return true;
647}
648
649static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
650 size_t off2, size_t len)
651{
652 return dmaengine_check_align(dev->copy_align, off1, off2, len);
653}
654
655static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
656 size_t off2, size_t len)
657{
658 return dmaengine_check_align(dev->xor_align, off1, off2, len);
659}
660
661static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
662 size_t off2, size_t len)
663{
664 return dmaengine_check_align(dev->pq_align, off1, off2, len);
665}
666
667static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
668 size_t off2, size_t len)
669{
670 return dmaengine_check_align(dev->fill_align, off1, off2, len);
671}
672
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700673static inline void
674dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
675{
676 dma->max_pq = maxpq;
677 if (has_pq_continue)
678 dma->max_pq |= DMA_HAS_PQ_CONTINUE;
679}
680
681static inline bool dmaf_continue(enum dma_ctrl_flags flags)
682{
683 return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
684}
685
686static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
687{
688 enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
689
690 return (flags & mask) == mask;
691}
692
693static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
694{
695 return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
696}
697
Mathieu Lacaged3f3cf82010-08-14 15:02:44 +0200698static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700699{
700 return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
701}
702
703/* dma_maxpq - reduce maxpq in the face of continued operations
704 * @dma - dma device with PQ capability
705 * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
706 *
707 * When an engine does not support native continuation we need 3 extra
708 * source slots to reuse P and Q with the following coefficients:
709 * 1/ {00} * P : remove P from Q', but use it as a source for P'
710 * 2/ {01} * Q : use Q to continue Q' calculation
711 * 3/ {00} * Q : subtract Q from P' to cancel (2)
712 *
713 * In the case where P is disabled we only need 1 extra source:
714 * 1/ {01} * Q : use Q to continue Q' calculation
715 */
716static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
717{
718 if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
719 return dma_dev_to_maxpq(dma);
720 else if (dmaf_p_disabled_continue(flags))
721 return dma_dev_to_maxpq(dma) - 1;
722 else if (dmaf_continue(flags))
723 return dma_dev_to_maxpq(dma) - 3;
724 BUG();
725}
726
Chris Leechc13c8262006-05-23 17:18:44 -0700727/* --- public DMA engine API --- */
728
Dan Williams649274d2009-01-11 00:20:39 -0800729#ifdef CONFIG_DMA_ENGINE
Dan Williams209b84a2009-01-06 11:38:17 -0700730void dmaengine_get(void);
731void dmaengine_put(void);
Dan Williams649274d2009-01-11 00:20:39 -0800732#else
733static inline void dmaengine_get(void)
734{
735}
736static inline void dmaengine_put(void)
737{
738}
739#endif
740
David S. Millerb4bd07c2009-02-06 22:06:43 -0800741#ifdef CONFIG_NET_DMA
742#define net_dmaengine_get() dmaengine_get()
743#define net_dmaengine_put() dmaengine_put()
744#else
745static inline void net_dmaengine_get(void)
746{
747}
748static inline void net_dmaengine_put(void)
749{
750}
751#endif
752
Dan Williams729b5d12009-03-25 09:13:25 -0700753#ifdef CONFIG_ASYNC_TX_DMA
754#define async_dmaengine_get() dmaengine_get()
755#define async_dmaengine_put() dmaengine_put()
Dan Williams5fc6d892010-10-07 16:44:50 -0700756#ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williams138f4c32009-09-08 17:42:51 -0700757#define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
758#else
Dan Williams729b5d12009-03-25 09:13:25 -0700759#define async_dma_find_channel(type) dma_find_channel(type)
Dan Williams5fc6d892010-10-07 16:44:50 -0700760#endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
Dan Williams729b5d12009-03-25 09:13:25 -0700761#else
762static inline void async_dmaengine_get(void)
763{
764}
765static inline void async_dmaengine_put(void)
766{
767}
768static inline struct dma_chan *
769async_dma_find_channel(enum dma_transaction_type type)
770{
771 return NULL;
772}
Dan Williams138f4c32009-09-08 17:42:51 -0700773#endif /* CONFIG_ASYNC_TX_DMA */
Dan Williams729b5d12009-03-25 09:13:25 -0700774
Dan Williams7405f742007-01-02 11:10:43 -0700775dma_cookie_t dma_async_memcpy_buf_to_buf(struct dma_chan *chan,
776 void *dest, void *src, size_t len);
777dma_cookie_t dma_async_memcpy_buf_to_pg(struct dma_chan *chan,
778 struct page *page, unsigned int offset, void *kdata, size_t len);
779dma_cookie_t dma_async_memcpy_pg_to_pg(struct dma_chan *chan,
Chris Leechc13c8262006-05-23 17:18:44 -0700780 struct page *dest_pg, unsigned int dest_off, struct page *src_pg,
Dan Williams7405f742007-01-02 11:10:43 -0700781 unsigned int src_off, size_t len);
782void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
783 struct dma_chan *chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700784
Dan Williams08398752008-07-17 17:59:56 -0700785static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
Dan Williams7405f742007-01-02 11:10:43 -0700786{
Dan Williams636bdeaa2008-04-17 20:17:26 -0700787 tx->flags |= DMA_CTRL_ACK;
788}
789
Guennadi Liakhovetskief560682009-01-19 15:36:21 -0700790static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
791{
792 tx->flags &= ~DMA_CTRL_ACK;
793}
794
Dan Williams08398752008-07-17 17:59:56 -0700795static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
Dan Williams636bdeaa2008-04-17 20:17:26 -0700796{
Dan Williams08398752008-07-17 17:59:56 -0700797 return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
Chris Leechc13c8262006-05-23 17:18:44 -0700798}
799
Dan Williams7405f742007-01-02 11:10:43 -0700800#define first_dma_cap(mask) __first_dma_cap(&(mask))
801static inline int __first_dma_cap(const dma_cap_mask_t *srcp)
802{
803 return min_t(int, DMA_TX_TYPE_END,
804 find_first_bit(srcp->bits, DMA_TX_TYPE_END));
805}
806
807#define next_dma_cap(n, mask) __next_dma_cap((n), &(mask))
808static inline int __next_dma_cap(int n, const dma_cap_mask_t *srcp)
809{
810 return min_t(int, DMA_TX_TYPE_END,
811 find_next_bit(srcp->bits, DMA_TX_TYPE_END, n+1));
812}
813
814#define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
815static inline void
816__dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
817{
818 set_bit(tx_type, dstp->bits);
819}
820
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900821#define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
822static inline void
823__dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
824{
825 clear_bit(tx_type, dstp->bits);
826}
827
Dan Williams33df8ca2009-01-06 11:38:15 -0700828#define dma_cap_zero(mask) __dma_cap_zero(&(mask))
829static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
830{
831 bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
832}
833
Dan Williams7405f742007-01-02 11:10:43 -0700834#define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
835static inline int
836__dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
837{
838 return test_bit(tx_type, srcp->bits);
839}
840
841#define for_each_dma_cap_mask(cap, mask) \
842 for ((cap) = first_dma_cap(mask); \
843 (cap) < DMA_TX_TYPE_END; \
844 (cap) = next_dma_cap((cap), (mask)))
845
Chris Leechc13c8262006-05-23 17:18:44 -0700846/**
Dan Williams7405f742007-01-02 11:10:43 -0700847 * dma_async_issue_pending - flush pending transactions to HW
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700848 * @chan: target DMA channel
Chris Leechc13c8262006-05-23 17:18:44 -0700849 *
850 * This allows drivers to push copies to HW in batches,
851 * reducing MMIO writes where possible.
852 */
Dan Williams7405f742007-01-02 11:10:43 -0700853static inline void dma_async_issue_pending(struct dma_chan *chan)
Chris Leechc13c8262006-05-23 17:18:44 -0700854{
Dan Williamsec8670f2008-03-01 07:51:29 -0700855 chan->device->device_issue_pending(chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700856}
857
Dan Williams7405f742007-01-02 11:10:43 -0700858#define dma_async_memcpy_issue_pending(chan) dma_async_issue_pending(chan)
859
Chris Leechc13c8262006-05-23 17:18:44 -0700860/**
Dan Williams7405f742007-01-02 11:10:43 -0700861 * dma_async_is_tx_complete - poll for transaction completion
Chris Leechc13c8262006-05-23 17:18:44 -0700862 * @chan: DMA channel
863 * @cookie: transaction identifier to check status of
864 * @last: returns last completed cookie, can be NULL
865 * @used: returns last issued cookie, can be NULL
866 *
867 * If @last and @used are passed in, upon return they reflect the driver
868 * internal state and can be used with dma_async_is_complete() to check
869 * the status of multiple cookies without re-checking hardware state.
870 */
Dan Williams7405f742007-01-02 11:10:43 -0700871static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
Chris Leechc13c8262006-05-23 17:18:44 -0700872 dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
873{
Linus Walleij07934482010-03-26 16:50:49 -0700874 struct dma_tx_state state;
875 enum dma_status status;
876
877 status = chan->device->device_tx_status(chan, cookie, &state);
878 if (last)
879 *last = state.last;
880 if (used)
881 *used = state.used;
882 return status;
Chris Leechc13c8262006-05-23 17:18:44 -0700883}
884
Dan Williams7405f742007-01-02 11:10:43 -0700885#define dma_async_memcpy_complete(chan, cookie, last, used)\
886 dma_async_is_tx_complete(chan, cookie, last, used)
887
Chris Leechc13c8262006-05-23 17:18:44 -0700888/**
889 * dma_async_is_complete - test a cookie against chan state
890 * @cookie: transaction identifier to test status of
891 * @last_complete: last know completed transaction
892 * @last_used: last cookie value handed out
893 *
894 * dma_async_is_complete() is used in dma_async_memcpy_complete()
Sebastian Siewior8a5703f2008-04-21 22:38:45 +0000895 * the test logic is separated for lightweight testing of multiple cookies
Chris Leechc13c8262006-05-23 17:18:44 -0700896 */
897static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
898 dma_cookie_t last_complete, dma_cookie_t last_used)
899{
900 if (last_complete <= last_used) {
901 if ((cookie <= last_complete) || (cookie > last_used))
902 return DMA_SUCCESS;
903 } else {
904 if ((cookie <= last_complete) && (cookie > last_used))
905 return DMA_SUCCESS;
906 }
907 return DMA_IN_PROGRESS;
908}
909
Dan Williamsbca34692010-03-26 16:52:10 -0700910static inline void
911dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
912{
913 if (st) {
914 st->last = last;
915 st->used = used;
916 st->residue = residue;
917 }
918}
919
Dan Williams7405f742007-01-02 11:10:43 -0700920enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
Dan Williams07f22112009-01-05 17:14:31 -0700921#ifdef CONFIG_DMA_ENGINE
922enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
Dan Williamsc50331e2009-01-19 15:33:14 -0700923void dma_issue_pending_all(void);
Guennadi Liakhovetski8f33d522010-12-22 14:46:46 +0100924struct dma_chan *__dma_request_channel(dma_cap_mask_t *mask, dma_filter_fn fn, void *fn_param);
925void dma_release_channel(struct dma_chan *chan);
Dan Williams07f22112009-01-05 17:14:31 -0700926#else
927static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
928{
929 return DMA_SUCCESS;
930}
Dan Williamsc50331e2009-01-19 15:33:14 -0700931static inline void dma_issue_pending_all(void)
932{
Guennadi Liakhovetski8f33d522010-12-22 14:46:46 +0100933}
934static inline struct dma_chan *__dma_request_channel(dma_cap_mask_t *mask,
935 dma_filter_fn fn, void *fn_param)
936{
937 return NULL;
938}
939static inline void dma_release_channel(struct dma_chan *chan)
940{
Dan Williamsc50331e2009-01-19 15:33:14 -0700941}
Dan Williams07f22112009-01-05 17:14:31 -0700942#endif
Chris Leechc13c8262006-05-23 17:18:44 -0700943
944/* --- DMA device --- */
945
946int dma_async_device_register(struct dma_device *device);
947void dma_async_device_unregister(struct dma_device *device);
Dan Williams07f22112009-01-05 17:14:31 -0700948void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
Dan Williamsbec08512009-01-06 11:38:14 -0700949struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
Dan Williams59b5ec22009-01-06 11:38:15 -0700950#define dma_request_channel(mask, x, y) __dma_request_channel(&(mask), x, y)
Chris Leechc13c8262006-05-23 17:18:44 -0700951
Chris Leechde5506e2006-05-23 17:50:37 -0700952/* --- Helper iov-locking functions --- */
953
954struct dma_page_list {
Al Virob2ddb902008-03-29 03:09:38 +0000955 char __user *base_address;
Chris Leechde5506e2006-05-23 17:50:37 -0700956 int nr_pages;
957 struct page **pages;
958};
959
960struct dma_pinned_list {
961 int nr_iovecs;
962 struct dma_page_list page_list[0];
963};
964
965struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len);
966void dma_unpin_iovec_pages(struct dma_pinned_list* pinned_list);
967
968dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
969 struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len);
970dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
971 struct dma_pinned_list *pinned_list, struct page *page,
972 unsigned int offset, size_t len);
973
Chris Leechc13c8262006-05-23 17:18:44 -0700974#endif /* DMAENGINE_H */