blob: 19face3168b4e50c211c5608c21aea1f40b42afc [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 *
Chris Leechc13c8262006-05-23 17:18:44 -070014 * The full GNU General Public License is included in this distribution in the
15 * file called COPYING.
16 */
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000017#ifndef LINUX_DMAENGINE_H
18#define LINUX_DMAENGINE_H
David Woodhouse1c0f16e2006-06-27 02:53:56 -070019
Chris Leechc13c8262006-05-23 17:18:44 -070020#include <linux/device.h>
Stephen Warren0ad7c002013-11-26 10:04:22 -070021#include <linux/err.h>
Chris Leechc13c8262006-05-23 17:18:44 -070022#include <linux/uio.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050023#include <linux/bug.h>
Vinod Koul90b44f82011-07-25 19:57:52 +053024#include <linux/scatterlist.h>
Paul Gortmakera8efa9d2011-07-29 16:55:11 +100025#include <linux/bitmap.h>
Viresh Kumardcc043d2012-02-01 16:12:18 +053026#include <linux/types.h>
Paul Gortmakera8efa9d2011-07-29 16:55:11 +100027#include <asm/page.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000028
Chris Leechc13c8262006-05-23 17:18:44 -070029/**
Randy Dunlapfe4ada22006-07-03 19:44:51 -070030 * typedef dma_cookie_t - an opaque DMA cookie
Chris Leechc13c8262006-05-23 17:18:44 -070031 *
32 * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
33 */
34typedef s32 dma_cookie_t;
Steven J. Magnani76bd0612010-02-28 22:18:16 -070035#define DMA_MIN_COOKIE 1
Chris Leechc13c8262006-05-23 17:18:44 -070036
Dan Carpenter71ea1482013-08-10 10:46:50 +030037static inline int dma_submit_error(dma_cookie_t cookie)
38{
39 return cookie < 0 ? cookie : 0;
40}
Chris Leechc13c8262006-05-23 17:18:44 -070041
42/**
43 * enum dma_status - DMA transaction status
Vinod Kouladfedd92013-10-16 13:29:02 +053044 * @DMA_COMPLETE: transaction completed
Chris Leechc13c8262006-05-23 17:18:44 -070045 * @DMA_IN_PROGRESS: transaction not yet processed
Linus Walleij07934482010-03-26 16:50:49 -070046 * @DMA_PAUSED: transaction is paused
Chris Leechc13c8262006-05-23 17:18:44 -070047 * @DMA_ERROR: transaction failed
48 */
49enum dma_status {
Vinod Koul7db5f722013-10-17 07:29:57 +053050 DMA_COMPLETE,
Chris Leechc13c8262006-05-23 17:18:44 -070051 DMA_IN_PROGRESS,
Linus Walleij07934482010-03-26 16:50:49 -070052 DMA_PAUSED,
Chris Leechc13c8262006-05-23 17:18:44 -070053 DMA_ERROR,
54};
55
56/**
Dan Williams7405f742007-01-02 11:10:43 -070057 * enum dma_transaction_type - DMA transaction types/indexes
Dan Williams138f4c32009-09-08 17:42:51 -070058 *
59 * Note: The DMA_ASYNC_TX capability is not to be set by drivers. It is
60 * automatically set as dma devices are registered.
Dan Williams7405f742007-01-02 11:10:43 -070061 */
62enum dma_transaction_type {
63 DMA_MEMCPY,
64 DMA_XOR,
Dan Williamsb2f46fd2009-07-14 12:20:36 -070065 DMA_PQ,
Dan Williams099f53c2009-04-08 14:28:37 -070066 DMA_XOR_VAL,
67 DMA_PQ_VAL,
Maxime Ripard4983a502015-05-18 13:46:15 +020068 DMA_MEMSET,
Dan Williams7405f742007-01-02 11:10:43 -070069 DMA_INTERRUPT,
Ira Snydera86ee032010-09-30 11:46:44 +000070 DMA_SG,
Dan Williams59b5ec22009-01-06 11:38:15 -070071 DMA_PRIVATE,
Dan Williams138f4c32009-09-08 17:42:51 -070072 DMA_ASYNC_TX,
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -070073 DMA_SLAVE,
Sascha Hauer782bc952010-09-30 13:56:32 +000074 DMA_CYCLIC,
Jassi Brarb14dab72011-10-13 12:33:30 +053075 DMA_INTERLEAVE,
Dan Williams7405f742007-01-02 11:10:43 -070076/* last transaction type for creation of the capabilities mask */
Jassi Brarb14dab72011-10-13 12:33:30 +053077 DMA_TX_TYPE_END,
78};
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -070079
Vinod Koul49920bc2011-10-13 15:15:27 +053080/**
81 * enum dma_transfer_direction - dma transfer mode and direction indicator
82 * @DMA_MEM_TO_MEM: Async/Memcpy mode
83 * @DMA_MEM_TO_DEV: Slave mode & From Memory to Device
84 * @DMA_DEV_TO_MEM: Slave mode & From Device to Memory
85 * @DMA_DEV_TO_DEV: Slave mode & From Device to Device
86 */
87enum dma_transfer_direction {
88 DMA_MEM_TO_MEM,
89 DMA_MEM_TO_DEV,
90 DMA_DEV_TO_MEM,
91 DMA_DEV_TO_DEV,
Shawn Guo62268ce2011-12-13 23:48:03 +080092 DMA_TRANS_NONE,
Vinod Koul49920bc2011-10-13 15:15:27 +053093};
Dan Williams7405f742007-01-02 11:10:43 -070094
95/**
Jassi Brarb14dab72011-10-13 12:33:30 +053096 * Interleaved Transfer Request
97 * ----------------------------
98 * A chunk is collection of contiguous bytes to be transfered.
99 * The gap(in bytes) between two chunks is called inter-chunk-gap(ICG).
100 * ICGs may or maynot change between chunks.
101 * A FRAME is the smallest series of contiguous {chunk,icg} pairs,
102 * that when repeated an integral number of times, specifies the transfer.
103 * A transfer template is specification of a Frame, the number of times
104 * it is to be repeated and other per-transfer attributes.
105 *
106 * Practically, a client driver would have ready a template for each
107 * type of transfer it is going to need during its lifetime and
108 * set only 'src_start' and 'dst_start' before submitting the requests.
109 *
110 *
111 * | Frame-1 | Frame-2 | ~ | Frame-'numf' |
112 * |====....==.===...=...|====....==.===...=...| ~ |====....==.===...=...|
113 *
114 * == Chunk size
115 * ... ICG
116 */
117
118/**
119 * struct data_chunk - Element of scatter-gather list that makes a frame.
120 * @size: Number of bytes to read from source.
121 * size_dst := fn(op, size_src), so doesn't mean much for destination.
122 * @icg: Number of bytes to jump after last src/dst address of this
123 * chunk and before first src/dst address for next chunk.
124 * Ignored for dst(assumed 0), if dst_inc is true and dst_sgl is false.
125 * Ignored for src(assumed 0), if src_inc is true and src_sgl is false.
126 */
127struct data_chunk {
128 size_t size;
129 size_t icg;
130};
131
132/**
133 * struct dma_interleaved_template - Template to convey DMAC the transfer pattern
134 * and attributes.
135 * @src_start: Bus address of source for the first chunk.
136 * @dst_start: Bus address of destination for the first chunk.
137 * @dir: Specifies the type of Source and Destination.
138 * @src_inc: If the source address increments after reading from it.
139 * @dst_inc: If the destination address increments after writing to it.
140 * @src_sgl: If the 'icg' of sgl[] applies to Source (scattered read).
141 * Otherwise, source is read contiguously (icg ignored).
142 * Ignored if src_inc is false.
143 * @dst_sgl: If the 'icg' of sgl[] applies to Destination (scattered write).
144 * Otherwise, destination is filled contiguously (icg ignored).
145 * Ignored if dst_inc is false.
146 * @numf: Number of frames in this template.
147 * @frame_size: Number of chunks in a frame i.e, size of sgl[].
148 * @sgl: Array of {chunk,icg} pairs that make up a frame.
149 */
150struct dma_interleaved_template {
151 dma_addr_t src_start;
152 dma_addr_t dst_start;
153 enum dma_transfer_direction dir;
154 bool src_inc;
155 bool dst_inc;
156 bool src_sgl;
157 bool dst_sgl;
158 size_t numf;
159 size_t frame_size;
160 struct data_chunk sgl[0];
161};
162
163/**
Dan Williams636bdea2008-04-17 20:17:26 -0700164 * enum dma_ctrl_flags - DMA flags to augment operation preparation,
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700165 * control completion, and communicate status.
Dan Williamsd4c56f92008-02-02 19:49:58 -0700166 * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700167 * this transaction
Guennadi Liakhovetskia88f6662009-12-10 18:35:15 +0100168 * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700169 * acknowledges receipt, i.e. has has a chance to establish any dependency
170 * chains
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700171 * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q
172 * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P
173 * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as
174 * sources that were the result of a previous operation, in the case of a PQ
175 * operation it continues the calculation with new sources
Dan Williams0403e382009-09-08 17:42:50 -0700176 * @DMA_PREP_FENCE - tell the driver that subsequent operations depend
177 * on the result of this operation
Dan Williamsd4c56f92008-02-02 19:49:58 -0700178 */
Dan Williams636bdea2008-04-17 20:17:26 -0700179enum dma_ctrl_flags {
Dan Williamsd4c56f92008-02-02 19:49:58 -0700180 DMA_PREP_INTERRUPT = (1 << 0),
Dan Williams636bdea2008-04-17 20:17:26 -0700181 DMA_CTRL_ACK = (1 << 1),
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200182 DMA_PREP_PQ_DISABLE_P = (1 << 2),
183 DMA_PREP_PQ_DISABLE_Q = (1 << 3),
184 DMA_PREP_CONTINUE = (1 << 4),
185 DMA_PREP_FENCE = (1 << 5),
Dan Williamsd4c56f92008-02-02 19:49:58 -0700186};
187
188/**
Dan Williamsad283ea2009-08-29 19:09:26 -0700189 * enum sum_check_bits - bit position of pq_check_flags
190 */
191enum sum_check_bits {
192 SUM_CHECK_P = 0,
193 SUM_CHECK_Q = 1,
194};
195
196/**
197 * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations
198 * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
199 * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
200 */
201enum sum_check_flags {
202 SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
203 SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
204};
205
206
207/**
Dan Williams7405f742007-01-02 11:10:43 -0700208 * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
209 * See linux/cpumask.h
210 */
211typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
212
213/**
Chris Leechc13c8262006-05-23 17:18:44 -0700214 * struct dma_chan_percpu - the per-CPU part of struct dma_chan
Chris Leechc13c8262006-05-23 17:18:44 -0700215 * @memcpy_count: transaction counter
216 * @bytes_transferred: byte counter
217 */
218
219struct dma_chan_percpu {
Chris Leechc13c8262006-05-23 17:18:44 -0700220 /* stats */
221 unsigned long memcpy_count;
222 unsigned long bytes_transferred;
223};
224
225/**
226 * struct dma_chan - devices supply DMA channels, clients use them
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700227 * @device: ptr to the dma device who supplies this channel, always !%NULL
Chris Leechc13c8262006-05-23 17:18:44 -0700228 * @cookie: last cookie value returned to client
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000229 * @completed_cookie: last completed cookie for this channel
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700230 * @chan_id: channel ID for sysfs
Dan Williams41d5e592009-01-06 11:38:21 -0700231 * @dev: class device for sysfs
Chris Leechc13c8262006-05-23 17:18:44 -0700232 * @device_node: used to add this to the device chan list
233 * @local: per-cpu pointer to a struct dma_chan_percpu
Vinod Koul868d2ee2013-12-18 21:39:39 +0530234 * @client_count: how many clients are using this channel
Dan Williamsbec08512009-01-06 11:38:14 -0700235 * @table_count: number of appearances in the mem-to-mem allocation table
Dan Williams287d8592009-02-18 14:48:26 -0800236 * @private: private data for certain client-channel associations
Chris Leechc13c8262006-05-23 17:18:44 -0700237 */
238struct dma_chan {
Chris Leechc13c8262006-05-23 17:18:44 -0700239 struct dma_device *device;
240 dma_cookie_t cookie;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000241 dma_cookie_t completed_cookie;
Chris Leechc13c8262006-05-23 17:18:44 -0700242
243 /* sysfs */
244 int chan_id;
Dan Williams41d5e592009-01-06 11:38:21 -0700245 struct dma_chan_dev *dev;
Chris Leechc13c8262006-05-23 17:18:44 -0700246
Chris Leechc13c8262006-05-23 17:18:44 -0700247 struct list_head device_node;
Tejun Heoa29d8b82010-02-02 14:39:15 +0900248 struct dma_chan_percpu __percpu *local;
Dan Williams7cc5bf92008-07-08 11:58:21 -0700249 int client_count;
Dan Williamsbec08512009-01-06 11:38:14 -0700250 int table_count;
Dan Williams287d8592009-02-18 14:48:26 -0800251 void *private;
Chris Leechc13c8262006-05-23 17:18:44 -0700252};
253
Dan Williams41d5e592009-01-06 11:38:21 -0700254/**
255 * struct dma_chan_dev - relate sysfs device node to backing channel device
Vinod Koul868d2ee2013-12-18 21:39:39 +0530256 * @chan: driver channel device
257 * @device: sysfs device
258 * @dev_id: parent dma_device dev_id
259 * @idr_ref: reference count to gate release of dma_device dev_id
Dan Williams41d5e592009-01-06 11:38:21 -0700260 */
261struct dma_chan_dev {
262 struct dma_chan *chan;
263 struct device device;
Dan Williams864498a2009-01-06 11:38:21 -0700264 int dev_id;
265 atomic_t *idr_ref;
Dan Williams41d5e592009-01-06 11:38:21 -0700266};
267
Linus Walleijc156d0a2010-08-04 13:37:33 +0200268/**
Alexander Popovba730342014-05-15 18:15:31 +0400269 * enum dma_slave_buswidth - defines bus width of the DMA slave
Linus Walleijc156d0a2010-08-04 13:37:33 +0200270 * device, source or target buses
271 */
272enum dma_slave_buswidth {
273 DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
274 DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
275 DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
Peter Ujfalusi93c6ee92014-07-03 07:51:52 +0300276 DMA_SLAVE_BUSWIDTH_3_BYTES = 3,
Linus Walleijc156d0a2010-08-04 13:37:33 +0200277 DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
278 DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
Laurent Pinchart534a7292014-08-06 10:52:41 +0200279 DMA_SLAVE_BUSWIDTH_16_BYTES = 16,
280 DMA_SLAVE_BUSWIDTH_32_BYTES = 32,
281 DMA_SLAVE_BUSWIDTH_64_BYTES = 64,
Linus Walleijc156d0a2010-08-04 13:37:33 +0200282};
283
284/**
285 * struct dma_slave_config - dma slave channel runtime config
286 * @direction: whether the data shall go in or out on this slave
Alexander Popov397321f2013-12-16 12:12:17 +0400287 * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are
Laurent Pinchartd9ff9582014-08-20 19:20:53 +0200288 * legal values. DEPRECATED, drivers should use the direction argument
289 * to the device_prep_slave_sg and device_prep_dma_cyclic functions or
290 * the dir field in the dma_interleaved_template structure.
Linus Walleijc156d0a2010-08-04 13:37:33 +0200291 * @src_addr: this is the physical address where DMA slave data
292 * should be read (RX), if the source is memory this argument is
293 * ignored.
294 * @dst_addr: this is the physical address where DMA slave data
295 * should be written (TX), if the source is memory this argument
296 * is ignored.
297 * @src_addr_width: this is the width in bytes of the source (RX)
298 * register where DMA data shall be read. If the source
299 * is memory this may be ignored depending on architecture.
300 * Legal values: 1, 2, 4, 8.
301 * @dst_addr_width: same as src_addr_width but for destination
302 * target (TX) mutatis mutandis.
303 * @src_maxburst: the maximum number of words (note: words, as in
304 * units of the src_addr_width member, not bytes) that can be sent
305 * in one burst to the device. Typically something like half the
306 * FIFO depth on I/O peripherals so you don't overflow it. This
307 * may or may not be applicable on memory sources.
308 * @dst_maxburst: same as src_maxburst but for destination target
309 * mutatis mutandis.
Viresh Kumardcc043d2012-02-01 16:12:18 +0530310 * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill
311 * with 'true' if peripheral should be flow controller. Direction will be
312 * selected at Runtime.
Laxman Dewangan4fd1e322012-06-06 10:55:26 +0530313 * @slave_id: Slave requester id. Only valid for slave channels. The dma
314 * slave peripheral will have unique id as dma requester which need to be
315 * pass as slave config.
Linus Walleijc156d0a2010-08-04 13:37:33 +0200316 *
317 * This struct is passed in as configuration data to a DMA engine
318 * in order to set up a certain channel for DMA transport at runtime.
319 * The DMA device/engine has to provide support for an additional
Maxime Ripard2c44ad92014-11-17 14:42:54 +0100320 * callback in the dma_device structure, device_config and this struct
321 * will then be passed in as an argument to the function.
Linus Walleijc156d0a2010-08-04 13:37:33 +0200322 *
Lars-Peter Clausen7cbccb52014-02-16 14:21:22 +0100323 * The rationale for adding configuration information to this struct is as
324 * follows: if it is likely that more than one DMA slave controllers in
325 * the world will support the configuration option, then make it generic.
326 * If not: if it is fixed so that it be sent in static from the platform
327 * data, then prefer to do that.
Linus Walleijc156d0a2010-08-04 13:37:33 +0200328 */
329struct dma_slave_config {
Vinod Koul49920bc2011-10-13 15:15:27 +0530330 enum dma_transfer_direction direction;
Linus Walleijc156d0a2010-08-04 13:37:33 +0200331 dma_addr_t src_addr;
332 dma_addr_t dst_addr;
333 enum dma_slave_buswidth src_addr_width;
334 enum dma_slave_buswidth dst_addr_width;
335 u32 src_maxburst;
336 u32 dst_maxburst;
Viresh Kumardcc043d2012-02-01 16:12:18 +0530337 bool device_fc;
Laxman Dewangan4fd1e322012-06-06 10:55:26 +0530338 unsigned int slave_id;
Linus Walleijc156d0a2010-08-04 13:37:33 +0200339};
340
Lars-Peter Clausen50720562014-01-11 14:02:16 +0100341/**
342 * enum dma_residue_granularity - Granularity of the reported transfer residue
343 * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The
344 * DMA channel is only able to tell whether a descriptor has been completed or
345 * not, which means residue reporting is not supported by this channel. The
346 * residue field of the dma_tx_state field will always be 0.
347 * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully
348 * completed segment of the transfer (For cyclic transfers this is after each
349 * period). This is typically implemented by having the hardware generate an
350 * interrupt after each transferred segment and then the drivers updates the
351 * outstanding residue by the size of the segment. Another possibility is if
352 * the hardware supports scatter-gather and the segment descriptor has a field
353 * which gets set after the segment has been completed. The driver then counts
354 * the number of segments without the flag set to compute the residue.
355 * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred
356 * burst. This is typically only supported if the hardware has a progress
357 * register of some sort (E.g. a register with the current read/write address
358 * or a register with the amount of bursts/beats/bytes that have been
359 * transferred or still need to be transferred).
360 */
361enum dma_residue_granularity {
362 DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0,
363 DMA_RESIDUE_GRANULARITY_SEGMENT = 1,
364 DMA_RESIDUE_GRANULARITY_BURST = 2,
365};
366
Vinod Koul221a27c72013-07-08 14:15:25 +0530367/* struct dma_slave_caps - expose capabilities of a slave channel only
368 *
369 * @src_addr_widths: bit mask of src addr widths the channel supports
Maxime Ripardceacbdb2014-11-17 14:41:57 +0100370 * @dst_addr_widths: bit mask of dstn addr widths the channel supports
Vinod Koul221a27c72013-07-08 14:15:25 +0530371 * @directions: bit mask of slave direction the channel supported
372 * since the enum dma_transfer_direction is not defined as bits for each
373 * type of direction, the dma controller should fill (1 << <TYPE>) and same
374 * should be checked by controller as well
375 * @cmd_pause: true, if pause and thereby resume is supported
376 * @cmd_terminate: true, if terminate cmd is supported
Lars-Peter Clausen50720562014-01-11 14:02:16 +0100377 * @residue_granularity: granularity of the reported transfer residue
Vinod Koul221a27c72013-07-08 14:15:25 +0530378 */
379struct dma_slave_caps {
380 u32 src_addr_widths;
Maxime Ripardceacbdb2014-11-17 14:41:57 +0100381 u32 dst_addr_widths;
Vinod Koul221a27c72013-07-08 14:15:25 +0530382 u32 directions;
383 bool cmd_pause;
384 bool cmd_terminate;
Lars-Peter Clausen50720562014-01-11 14:02:16 +0100385 enum dma_residue_granularity residue_granularity;
Vinod Koul221a27c72013-07-08 14:15:25 +0530386};
387
Dan Williams41d5e592009-01-06 11:38:21 -0700388static inline const char *dma_chan_name(struct dma_chan *chan)
389{
390 return dev_name(&chan->dev->device);
391}
Dan Williamsd379b012007-07-09 11:56:42 -0700392
Chris Leechc13c8262006-05-23 17:18:44 -0700393void dma_chan_cleanup(struct kref *kref);
394
Chris Leechc13c8262006-05-23 17:18:44 -0700395/**
Dan Williams59b5ec22009-01-06 11:38:15 -0700396 * typedef dma_filter_fn - callback filter for dma_request_channel
397 * @chan: channel to be reviewed
398 * @filter_param: opaque parameter passed through dma_request_channel
399 *
400 * When this optional parameter is specified in a call to dma_request_channel a
401 * suitable channel is passed to this routine for further dispositioning before
402 * being returned. Where 'suitable' indicates a non-busy channel that
Dan Williams7dd60252009-01-06 11:38:19 -0700403 * satisfies the given capability mask. It returns 'true' to indicate that the
404 * channel is suitable.
Dan Williams59b5ec22009-01-06 11:38:15 -0700405 */
Dan Williams7dd60252009-01-06 11:38:19 -0700406typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
Dan Williams59b5ec22009-01-06 11:38:15 -0700407
Dan Williams7405f742007-01-02 11:10:43 -0700408typedef void (*dma_async_tx_callback)(void *dma_async_param);
Dan Williamsd38a8c62013-10-18 19:35:23 +0200409
410struct dmaengine_unmap_data {
Xuelin Shic1f43dd2014-05-21 14:02:37 -0700411 u8 map_cnt;
Dan Williamsd38a8c62013-10-18 19:35:23 +0200412 u8 to_cnt;
413 u8 from_cnt;
414 u8 bidi_cnt;
415 struct device *dev;
416 struct kref kref;
417 size_t len;
418 dma_addr_t addr[0];
419};
420
Dan Williams7405f742007-01-02 11:10:43 -0700421/**
422 * struct dma_async_tx_descriptor - async transaction descriptor
423 * ---dma generic offload fields---
424 * @cookie: tracking cookie for this transaction, set to -EBUSY if
425 * this tx is sitting on a dependency list
Dan Williams636bdea2008-04-17 20:17:26 -0700426 * @flags: flags to augment operation preparation, control completion, and
427 * communicate status
Dan Williams7405f742007-01-02 11:10:43 -0700428 * @phys: physical address of the descriptor
Dan Williams7405f742007-01-02 11:10:43 -0700429 * @chan: target channel for this operation
Vinod Koulaba96ba2014-12-05 20:49:07 +0530430 * @tx_submit: accept the descriptor, assign ordered cookie and mark the
431 * descriptor pending. To be pushed on .issue_pending() call
Dan Williams7405f742007-01-02 11:10:43 -0700432 * @callback: routine to call after this operation is complete
433 * @callback_param: general parameter to pass to the callback routine
434 * ---async_tx api specific fields---
Dan Williams19242d72008-04-17 20:17:25 -0700435 * @next: at completion submit this descriptor
Dan Williams7405f742007-01-02 11:10:43 -0700436 * @parent: pointer to the next level up in the dependency chain
Dan Williams19242d72008-04-17 20:17:25 -0700437 * @lock: protect the parent and next pointers
Dan Williams7405f742007-01-02 11:10:43 -0700438 */
439struct dma_async_tx_descriptor {
440 dma_cookie_t cookie;
Dan Williams636bdea2008-04-17 20:17:26 -0700441 enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
Dan Williams7405f742007-01-02 11:10:43 -0700442 dma_addr_t phys;
Dan Williams7405f742007-01-02 11:10:43 -0700443 struct dma_chan *chan;
444 dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
Dan Williams7405f742007-01-02 11:10:43 -0700445 dma_async_tx_callback callback;
446 void *callback_param;
Dan Williamsd38a8c62013-10-18 19:35:23 +0200447 struct dmaengine_unmap_data *unmap;
Dan Williams5fc6d892010-10-07 16:44:50 -0700448#ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williams19242d72008-04-17 20:17:25 -0700449 struct dma_async_tx_descriptor *next;
Dan Williams7405f742007-01-02 11:10:43 -0700450 struct dma_async_tx_descriptor *parent;
451 spinlock_t lock;
Dan Williamscaa20d972010-05-17 16:24:16 -0700452#endif
Dan Williams7405f742007-01-02 11:10:43 -0700453};
454
Dan Williams89716462013-10-18 19:35:25 +0200455#ifdef CONFIG_DMA_ENGINE
Dan Williamsd38a8c62013-10-18 19:35:23 +0200456static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
457 struct dmaengine_unmap_data *unmap)
458{
459 kref_get(&unmap->kref);
460 tx->unmap = unmap;
461}
462
Dan Williams89716462013-10-18 19:35:25 +0200463struct dmaengine_unmap_data *
464dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags);
Dan Williams45c463a2013-10-18 19:35:24 +0200465void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap);
Dan Williams89716462013-10-18 19:35:25 +0200466#else
467static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
468 struct dmaengine_unmap_data *unmap)
469{
470}
471static inline struct dmaengine_unmap_data *
472dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
473{
474 return NULL;
475}
476static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
477{
478}
479#endif
Dan Williams45c463a2013-10-18 19:35:24 +0200480
Dan Williamsd38a8c62013-10-18 19:35:23 +0200481static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
482{
483 if (tx->unmap) {
Dan Williams45c463a2013-10-18 19:35:24 +0200484 dmaengine_unmap_put(tx->unmap);
Dan Williamsd38a8c62013-10-18 19:35:23 +0200485 tx->unmap = NULL;
486 }
487}
488
Dan Williams5fc6d892010-10-07 16:44:50 -0700489#ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williamscaa20d972010-05-17 16:24:16 -0700490static inline void txd_lock(struct dma_async_tx_descriptor *txd)
491{
492}
493static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
494{
495}
496static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
497{
498 BUG();
499}
500static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
501{
502}
503static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
504{
505}
506static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
507{
508 return NULL;
509}
510static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
511{
512 return NULL;
513}
514
515#else
516static inline void txd_lock(struct dma_async_tx_descriptor *txd)
517{
518 spin_lock_bh(&txd->lock);
519}
520static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
521{
522 spin_unlock_bh(&txd->lock);
523}
524static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
525{
526 txd->next = next;
527 next->parent = txd;
528}
529static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
530{
531 txd->parent = NULL;
532}
533static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
534{
535 txd->next = NULL;
536}
537static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
538{
539 return txd->parent;
540}
541static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
542{
543 return txd->next;
544}
545#endif
546
Chris Leechc13c8262006-05-23 17:18:44 -0700547/**
Linus Walleij07934482010-03-26 16:50:49 -0700548 * struct dma_tx_state - filled in to report the status of
549 * a transfer.
550 * @last: last completed DMA cookie
551 * @used: last issued DMA cookie (i.e. the one in progress)
552 * @residue: the remaining number of bytes left to transmit
553 * on the selected transfer for states DMA_IN_PROGRESS and
554 * DMA_PAUSED if this is implemented in the driver, else 0
555 */
556struct dma_tx_state {
557 dma_cookie_t last;
558 dma_cookie_t used;
559 u32 residue;
560};
561
562/**
Chris Leechc13c8262006-05-23 17:18:44 -0700563 * struct dma_device - info on the entity supplying DMA services
564 * @chancnt: how many DMA channels are supported
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900565 * @privatecnt: how many DMA channels are requested by dma_request_channel
Chris Leechc13c8262006-05-23 17:18:44 -0700566 * @channels: the list of struct dma_chan
567 * @global_node: list_head for global dma_device_list
Dan Williams7405f742007-01-02 11:10:43 -0700568 * @cap_mask: one or more dma_capability flags
569 * @max_xor: maximum number of xor sources, 0 if no capability
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700570 * @max_pq: maximum number of PQ sources and PQ-continue capability
Dan Williams83544ae2009-09-08 17:42:53 -0700571 * @copy_align: alignment shift for memcpy operations
572 * @xor_align: alignment shift for xor operations
573 * @pq_align: alignment shift for pq operations
Maxime Ripard4983a502015-05-18 13:46:15 +0200574 * @fill_align: alignment shift for memset operations
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700575 * @dev_id: unique device ID
Dan Williams7405f742007-01-02 11:10:43 -0700576 * @dev: struct device reference for dma mapping api
Maxime Ripardcb8cea52014-11-17 14:42:04 +0100577 * @src_addr_widths: bit mask of src addr widths the device supports
578 * @dst_addr_widths: bit mask of dst addr widths the device supports
579 * @directions: bit mask of slave direction the device supports since
580 * the enum dma_transfer_direction is not defined as bits for
581 * each type of direction, the dma controller should fill (1 <<
582 * <TYPE>) and same should be checked by controller as well
583 * @residue_granularity: granularity of the transfer residue reported
584 * by tx_status
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700585 * @device_alloc_chan_resources: allocate resources and return the
586 * number of allocated descriptors
587 * @device_free_chan_resources: release DMA channel's resources
Dan Williams7405f742007-01-02 11:10:43 -0700588 * @device_prep_dma_memcpy: prepares a memcpy operation
589 * @device_prep_dma_xor: prepares a xor operation
Dan Williams099f53c2009-04-08 14:28:37 -0700590 * @device_prep_dma_xor_val: prepares a xor validation operation
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700591 * @device_prep_dma_pq: prepares a pq operation
592 * @device_prep_dma_pq_val: prepares a pqzero_sum operation
Maxime Ripard4983a502015-05-18 13:46:15 +0200593 * @device_prep_dma_memset: prepares a memset operation
Dan Williams7405f742007-01-02 11:10:43 -0700594 * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700595 * @device_prep_slave_sg: prepares a slave dma operation
Sascha Hauer782bc952010-09-30 13:56:32 +0000596 * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
597 * The function takes a buffer of size buf_len. The callback function will
598 * be called after period_len bytes have been transferred.
Jassi Brarb14dab72011-10-13 12:33:30 +0530599 * @device_prep_interleaved_dma: Transfer expression in a generic way.
Maxime Ripard94a73e32014-11-17 14:42:00 +0100600 * @device_config: Pushes a new configuration to a channel, return 0 or an error
601 * code
Maxime Ripard23a3ea22014-11-17 14:42:01 +0100602 * @device_pause: Pauses any transfer happening on a channel. Returns
603 * 0 or an error code
604 * @device_resume: Resumes any transfer on a channel previously
605 * paused. Returns 0 or an error code
Maxime Ripard7fa0cf42014-11-17 14:42:02 +0100606 * @device_terminate_all: Aborts all transfers on a channel. Returns 0
607 * or an error code
Linus Walleij07934482010-03-26 16:50:49 -0700608 * @device_tx_status: poll for transaction completion, the optional
609 * txstate parameter can be supplied with a pointer to get a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300610 * struct with auxiliary transfer status information, otherwise the call
Linus Walleij07934482010-03-26 16:50:49 -0700611 * will just return a simple status code
Dan Williams7405f742007-01-02 11:10:43 -0700612 * @device_issue_pending: push pending transactions to hardware
Chris Leechc13c8262006-05-23 17:18:44 -0700613 */
614struct dma_device {
615
616 unsigned int chancnt;
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900617 unsigned int privatecnt;
Chris Leechc13c8262006-05-23 17:18:44 -0700618 struct list_head channels;
619 struct list_head global_node;
Dan Williams7405f742007-01-02 11:10:43 -0700620 dma_cap_mask_t cap_mask;
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700621 unsigned short max_xor;
622 unsigned short max_pq;
Dan Williams83544ae2009-09-08 17:42:53 -0700623 u8 copy_align;
624 u8 xor_align;
625 u8 pq_align;
Maxime Ripard4983a502015-05-18 13:46:15 +0200626 u8 fill_align;
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700627 #define DMA_HAS_PQ_CONTINUE (1 << 15)
Chris Leechc13c8262006-05-23 17:18:44 -0700628
Chris Leechc13c8262006-05-23 17:18:44 -0700629 int dev_id;
Dan Williams7405f742007-01-02 11:10:43 -0700630 struct device *dev;
Chris Leechc13c8262006-05-23 17:18:44 -0700631
Maxime Ripardcb8cea52014-11-17 14:42:04 +0100632 u32 src_addr_widths;
633 u32 dst_addr_widths;
634 u32 directions;
635 enum dma_residue_granularity residue_granularity;
636
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700637 int (*device_alloc_chan_resources)(struct dma_chan *chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700638 void (*device_free_chan_resources)(struct dma_chan *chan);
Dan Williams7405f742007-01-02 11:10:43 -0700639
640 struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
Maxime Ripardceacbdb2014-11-17 14:41:57 +0100641 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700642 size_t len, unsigned long flags);
Dan Williams7405f742007-01-02 11:10:43 -0700643 struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
Maxime Ripardceacbdb2014-11-17 14:41:57 +0100644 struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700645 unsigned int src_cnt, size_t len, unsigned long flags);
Dan Williams099f53c2009-04-08 14:28:37 -0700646 struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
Dan Williams00367312008-02-02 19:49:57 -0700647 struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
Dan Williamsad283ea2009-08-29 19:09:26 -0700648 size_t len, enum sum_check_flags *result, unsigned long flags);
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700649 struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
650 struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
651 unsigned int src_cnt, const unsigned char *scf,
652 size_t len, unsigned long flags);
653 struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
654 struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
655 unsigned int src_cnt, const unsigned char *scf, size_t len,
656 enum sum_check_flags *pqres, unsigned long flags);
Maxime Ripard4983a502015-05-18 13:46:15 +0200657 struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
658 struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
659 unsigned long flags);
Dan Williams7405f742007-01-02 11:10:43 -0700660 struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
Dan Williams636bdea2008-04-17 20:17:26 -0700661 struct dma_chan *chan, unsigned long flags);
Ira Snydera86ee032010-09-30 11:46:44 +0000662 struct dma_async_tx_descriptor *(*device_prep_dma_sg)(
663 struct dma_chan *chan,
664 struct scatterlist *dst_sg, unsigned int dst_nents,
665 struct scatterlist *src_sg, unsigned int src_nents,
666 unsigned long flags);
Dan Williams7405f742007-01-02 11:10:43 -0700667
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700668 struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
669 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Koul49920bc2011-10-13 15:15:27 +0530670 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500671 unsigned long flags, void *context);
Sascha Hauer782bc952010-09-30 13:56:32 +0000672 struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
673 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500674 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200675 unsigned long flags);
Jassi Brarb14dab72011-10-13 12:33:30 +0530676 struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
677 struct dma_chan *chan, struct dma_interleaved_template *xt,
678 unsigned long flags);
Maxime Ripard94a73e32014-11-17 14:42:00 +0100679
680 int (*device_config)(struct dma_chan *chan,
681 struct dma_slave_config *config);
Maxime Ripard23a3ea22014-11-17 14:42:01 +0100682 int (*device_pause)(struct dma_chan *chan);
683 int (*device_resume)(struct dma_chan *chan);
Maxime Ripard7fa0cf42014-11-17 14:42:02 +0100684 int (*device_terminate_all)(struct dma_chan *chan);
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700685
Linus Walleij07934482010-03-26 16:50:49 -0700686 enum dma_status (*device_tx_status)(struct dma_chan *chan,
687 dma_cookie_t cookie,
688 struct dma_tx_state *txstate);
Dan Williams7405f742007-01-02 11:10:43 -0700689 void (*device_issue_pending)(struct dma_chan *chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700690};
691
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000692static inline int dmaengine_slave_config(struct dma_chan *chan,
693 struct dma_slave_config *config)
694{
Maxime Ripard94a73e32014-11-17 14:42:00 +0100695 if (chan->device->device_config)
696 return chan->device->device_config(chan, config);
697
Maxime Ripard2c44ad92014-11-17 14:42:54 +0100698 return -ENOSYS;
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000699}
700
Andy Shevchenko61cc13a2013-01-10 10:52:56 +0200701static inline bool is_slave_direction(enum dma_transfer_direction direction)
702{
703 return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM);
704}
705
Vinod Koul90b44f82011-07-25 19:57:52 +0530706static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
Kuninori Morimoto922ee082012-04-25 20:50:53 +0200707 struct dma_chan *chan, dma_addr_t buf, size_t len,
Vinod Koul49920bc2011-10-13 15:15:27 +0530708 enum dma_transfer_direction dir, unsigned long flags)
Vinod Koul90b44f82011-07-25 19:57:52 +0530709{
710 struct scatterlist sg;
Kuninori Morimoto922ee082012-04-25 20:50:53 +0200711 sg_init_table(&sg, 1);
712 sg_dma_address(&sg) = buf;
713 sg_dma_len(&sg) = len;
Vinod Koul90b44f82011-07-25 19:57:52 +0530714
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500715 return chan->device->device_prep_slave_sg(chan, &sg, 1,
716 dir, flags, NULL);
Vinod Koul90b44f82011-07-25 19:57:52 +0530717}
718
Alexandre Bounine16052822012-03-08 16:11:18 -0500719static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
720 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
721 enum dma_transfer_direction dir, unsigned long flags)
722{
723 return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500724 dir, flags, NULL);
Alexandre Bounine16052822012-03-08 16:11:18 -0500725}
726
Alexandre Bouninee42d98e2012-05-31 16:26:38 -0700727#ifdef CONFIG_RAPIDIO_DMA_ENGINE
728struct rio_dma_ext;
729static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg(
730 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
731 enum dma_transfer_direction dir, unsigned long flags,
732 struct rio_dma_ext *rio_ext)
733{
734 return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
735 dir, flags, rio_ext);
736}
737#endif
738
Alexandre Bounine16052822012-03-08 16:11:18 -0500739static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
740 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Peter Ujfalusie7736cd2012-09-24 10:58:04 +0300741 size_t period_len, enum dma_transfer_direction dir,
742 unsigned long flags)
Alexandre Bounine16052822012-03-08 16:11:18 -0500743{
744 return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200745 period_len, dir, flags);
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000746}
747
Barry Songa14acb42012-11-06 21:32:39 +0800748static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
749 struct dma_chan *chan, struct dma_interleaved_template *xt,
750 unsigned long flags)
751{
752 return chan->device->device_prep_interleaved_dma(chan, xt, flags);
753}
754
Maxime Ripard4983a502015-05-18 13:46:15 +0200755static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset(
756 struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
757 unsigned long flags)
758{
759 if (!chan || !chan->device)
760 return NULL;
761
762 return chan->device->device_prep_dma_memset(chan, dest, value,
763 len, flags);
764}
765
Vinod Koulb65612a2014-10-11 21:16:43 +0530766static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_sg(
767 struct dma_chan *chan,
768 struct scatterlist *dst_sg, unsigned int dst_nents,
769 struct scatterlist *src_sg, unsigned int src_nents,
770 unsigned long flags)
771{
772 return chan->device->device_prep_dma_sg(chan, dst_sg, dst_nents,
773 src_sg, src_nents, flags);
774}
775
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000776static inline int dmaengine_terminate_all(struct dma_chan *chan)
777{
Maxime Ripard7fa0cf42014-11-17 14:42:02 +0100778 if (chan->device->device_terminate_all)
779 return chan->device->device_terminate_all(chan);
780
Maxime Ripard2c44ad92014-11-17 14:42:54 +0100781 return -ENOSYS;
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000782}
783
784static inline int dmaengine_pause(struct dma_chan *chan)
785{
Maxime Ripard23a3ea22014-11-17 14:42:01 +0100786 if (chan->device->device_pause)
787 return chan->device->device_pause(chan);
788
Maxime Ripard2c44ad92014-11-17 14:42:54 +0100789 return -ENOSYS;
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000790}
791
792static inline int dmaengine_resume(struct dma_chan *chan)
793{
Maxime Ripard23a3ea22014-11-17 14:42:01 +0100794 if (chan->device->device_resume)
795 return chan->device->device_resume(chan);
796
Maxime Ripard2c44ad92014-11-17 14:42:54 +0100797 return -ENOSYS;
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000798}
799
Lars-Peter Clausen3052cc22012-06-11 20:11:40 +0200800static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan,
801 dma_cookie_t cookie, struct dma_tx_state *state)
802{
803 return chan->device->device_tx_status(chan, cookie, state);
804}
805
Russell King - ARM Linux98d530f2011-01-01 23:00:23 +0000806static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
Sascha Hauer6e3ecaf2010-09-30 13:56:33 +0000807{
808 return desc->tx_submit(desc);
809}
810
Dan Williams83544ae2009-09-08 17:42:53 -0700811static inline bool dmaengine_check_align(u8 align, size_t off1, size_t off2, size_t len)
812{
813 size_t mask;
814
815 if (!align)
816 return true;
817 mask = (1 << align) - 1;
818 if (mask & (off1 | off2 | len))
819 return false;
820 return true;
821}
822
823static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
824 size_t off2, size_t len)
825{
826 return dmaengine_check_align(dev->copy_align, off1, off2, len);
827}
828
829static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
830 size_t off2, size_t len)
831{
832 return dmaengine_check_align(dev->xor_align, off1, off2, len);
833}
834
835static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
836 size_t off2, size_t len)
837{
838 return dmaengine_check_align(dev->pq_align, off1, off2, len);
839}
840
Maxime Ripard4983a502015-05-18 13:46:15 +0200841static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
842 size_t off2, size_t len)
843{
844 return dmaengine_check_align(dev->fill_align, off1, off2, len);
845}
846
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700847static inline void
848dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
849{
850 dma->max_pq = maxpq;
851 if (has_pq_continue)
852 dma->max_pq |= DMA_HAS_PQ_CONTINUE;
853}
854
855static inline bool dmaf_continue(enum dma_ctrl_flags flags)
856{
857 return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
858}
859
860static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
861{
862 enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
863
864 return (flags & mask) == mask;
865}
866
867static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
868{
869 return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
870}
871
Mathieu Lacaged3f3cf82010-08-14 15:02:44 +0200872static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700873{
874 return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
875}
876
877/* dma_maxpq - reduce maxpq in the face of continued operations
878 * @dma - dma device with PQ capability
879 * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
880 *
881 * When an engine does not support native continuation we need 3 extra
882 * source slots to reuse P and Q with the following coefficients:
883 * 1/ {00} * P : remove P from Q', but use it as a source for P'
884 * 2/ {01} * Q : use Q to continue Q' calculation
885 * 3/ {00} * Q : subtract Q from P' to cancel (2)
886 *
887 * In the case where P is disabled we only need 1 extra source:
888 * 1/ {01} * Q : use Q to continue Q' calculation
889 */
890static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
891{
892 if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
893 return dma_dev_to_maxpq(dma);
894 else if (dmaf_p_disabled_continue(flags))
895 return dma_dev_to_maxpq(dma) - 1;
896 else if (dmaf_continue(flags))
897 return dma_dev_to_maxpq(dma) - 3;
898 BUG();
899}
900
Chris Leechc13c8262006-05-23 17:18:44 -0700901/* --- public DMA engine API --- */
902
Dan Williams649274d2009-01-11 00:20:39 -0800903#ifdef CONFIG_DMA_ENGINE
Dan Williams209b84a2009-01-06 11:38:17 -0700904void dmaengine_get(void);
905void dmaengine_put(void);
Dan Williams649274d2009-01-11 00:20:39 -0800906#else
907static inline void dmaengine_get(void)
908{
909}
910static inline void dmaengine_put(void)
911{
912}
913#endif
914
Dan Williams729b5d12009-03-25 09:13:25 -0700915#ifdef CONFIG_ASYNC_TX_DMA
916#define async_dmaengine_get() dmaengine_get()
917#define async_dmaengine_put() dmaengine_put()
Dan Williams5fc6d892010-10-07 16:44:50 -0700918#ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williams138f4c32009-09-08 17:42:51 -0700919#define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
920#else
Dan Williams729b5d12009-03-25 09:13:25 -0700921#define async_dma_find_channel(type) dma_find_channel(type)
Dan Williams5fc6d892010-10-07 16:44:50 -0700922#endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
Dan Williams729b5d12009-03-25 09:13:25 -0700923#else
924static inline void async_dmaengine_get(void)
925{
926}
927static inline void async_dmaengine_put(void)
928{
929}
930static inline struct dma_chan *
931async_dma_find_channel(enum dma_transaction_type type)
932{
933 return NULL;
934}
Dan Williams138f4c32009-09-08 17:42:51 -0700935#endif /* CONFIG_ASYNC_TX_DMA */
Dan Williams7405f742007-01-02 11:10:43 -0700936void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
Dan Williams7bced392013-12-30 12:37:29 -0800937 struct dma_chan *chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700938
Dan Williams08398752008-07-17 17:59:56 -0700939static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
Dan Williams7405f742007-01-02 11:10:43 -0700940{
Dan Williams636bdea2008-04-17 20:17:26 -0700941 tx->flags |= DMA_CTRL_ACK;
942}
943
Guennadi Liakhovetskief560682009-01-19 15:36:21 -0700944static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
945{
946 tx->flags &= ~DMA_CTRL_ACK;
947}
948
Dan Williams08398752008-07-17 17:59:56 -0700949static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
Dan Williams636bdea2008-04-17 20:17:26 -0700950{
Dan Williams08398752008-07-17 17:59:56 -0700951 return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
Chris Leechc13c8262006-05-23 17:18:44 -0700952}
953
Dan Williams7405f742007-01-02 11:10:43 -0700954#define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
955static inline void
956__dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
957{
958 set_bit(tx_type, dstp->bits);
959}
960
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900961#define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
962static inline void
963__dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
964{
965 clear_bit(tx_type, dstp->bits);
966}
967
Dan Williams33df8ca2009-01-06 11:38:15 -0700968#define dma_cap_zero(mask) __dma_cap_zero(&(mask))
969static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
970{
971 bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
972}
973
Dan Williams7405f742007-01-02 11:10:43 -0700974#define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
975static inline int
976__dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
977{
978 return test_bit(tx_type, srcp->bits);
979}
980
981#define for_each_dma_cap_mask(cap, mask) \
Akinobu Mitae5a087f2012-10-26 23:35:15 +0900982 for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END)
Dan Williams7405f742007-01-02 11:10:43 -0700983
Chris Leechc13c8262006-05-23 17:18:44 -0700984/**
Dan Williams7405f742007-01-02 11:10:43 -0700985 * dma_async_issue_pending - flush pending transactions to HW
Randy Dunlapfe4ada22006-07-03 19:44:51 -0700986 * @chan: target DMA channel
Chris Leechc13c8262006-05-23 17:18:44 -0700987 *
988 * This allows drivers to push copies to HW in batches,
989 * reducing MMIO writes where possible.
990 */
Dan Williams7405f742007-01-02 11:10:43 -0700991static inline void dma_async_issue_pending(struct dma_chan *chan)
Chris Leechc13c8262006-05-23 17:18:44 -0700992{
Dan Williamsec8670f2008-03-01 07:51:29 -0700993 chan->device->device_issue_pending(chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700994}
995
996/**
Dan Williams7405f742007-01-02 11:10:43 -0700997 * dma_async_is_tx_complete - poll for transaction completion
Chris Leechc13c8262006-05-23 17:18:44 -0700998 * @chan: DMA channel
999 * @cookie: transaction identifier to check status of
1000 * @last: returns last completed cookie, can be NULL
1001 * @used: returns last issued cookie, can be NULL
1002 *
1003 * If @last and @used are passed in, upon return they reflect the driver
1004 * internal state and can be used with dma_async_is_complete() to check
1005 * the status of multiple cookies without re-checking hardware state.
1006 */
Dan Williams7405f742007-01-02 11:10:43 -07001007static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
Chris Leechc13c8262006-05-23 17:18:44 -07001008 dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
1009{
Linus Walleij07934482010-03-26 16:50:49 -07001010 struct dma_tx_state state;
1011 enum dma_status status;
1012
1013 status = chan->device->device_tx_status(chan, cookie, &state);
1014 if (last)
1015 *last = state.last;
1016 if (used)
1017 *used = state.used;
1018 return status;
Chris Leechc13c8262006-05-23 17:18:44 -07001019}
1020
1021/**
1022 * dma_async_is_complete - test a cookie against chan state
1023 * @cookie: transaction identifier to test status of
1024 * @last_complete: last know completed transaction
1025 * @last_used: last cookie value handed out
1026 *
Bartlomiej Zolnierkiewicze239345f2012-11-08 10:01:01 +00001027 * dma_async_is_complete() is used in dma_async_is_tx_complete()
Sebastian Siewior8a5703f2008-04-21 22:38:45 +00001028 * the test logic is separated for lightweight testing of multiple cookies
Chris Leechc13c8262006-05-23 17:18:44 -07001029 */
1030static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
1031 dma_cookie_t last_complete, dma_cookie_t last_used)
1032{
1033 if (last_complete <= last_used) {
1034 if ((cookie <= last_complete) || (cookie > last_used))
Vinod Kouladfedd92013-10-16 13:29:02 +05301035 return DMA_COMPLETE;
Chris Leechc13c8262006-05-23 17:18:44 -07001036 } else {
1037 if ((cookie <= last_complete) && (cookie > last_used))
Vinod Kouladfedd92013-10-16 13:29:02 +05301038 return DMA_COMPLETE;
Chris Leechc13c8262006-05-23 17:18:44 -07001039 }
1040 return DMA_IN_PROGRESS;
1041}
1042
Dan Williamsbca34692010-03-26 16:52:10 -07001043static inline void
1044dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
1045{
1046 if (st) {
1047 st->last = last;
1048 st->used = used;
1049 st->residue = residue;
1050 }
1051}
1052
Dan Williams07f22112009-01-05 17:14:31 -07001053#ifdef CONFIG_DMA_ENGINE
Jon Mason4a43f392013-09-09 16:51:59 -07001054struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
1055enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
Dan Williams07f22112009-01-05 17:14:31 -07001056enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
Dan Williamsc50331e2009-01-19 15:33:14 -07001057void dma_issue_pending_all(void);
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +01001058struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
1059 dma_filter_fn fn, void *fn_param);
Stephen Warren0ad7c002013-11-26 10:04:22 -07001060struct dma_chan *dma_request_slave_channel_reason(struct device *dev,
1061 const char *name);
Markus Pargmannbef29ec2013-02-24 16:36:09 +01001062struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name);
Guennadi Liakhovetski8f33d522010-12-22 14:46:46 +01001063void dma_release_channel(struct dma_chan *chan);
Laurent Pinchartfdb8df92015-01-19 13:54:27 +02001064int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
Dan Williams07f22112009-01-05 17:14:31 -07001065#else
Jon Mason4a43f392013-09-09 16:51:59 -07001066static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
1067{
1068 return NULL;
1069}
1070static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
1071{
Vinod Kouladfedd92013-10-16 13:29:02 +05301072 return DMA_COMPLETE;
Jon Mason4a43f392013-09-09 16:51:59 -07001073}
Dan Williams07f22112009-01-05 17:14:31 -07001074static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1075{
Vinod Kouladfedd92013-10-16 13:29:02 +05301076 return DMA_COMPLETE;
Dan Williams07f22112009-01-05 17:14:31 -07001077}
Dan Williamsc50331e2009-01-19 15:33:14 -07001078static inline void dma_issue_pending_all(void)
1079{
Guennadi Liakhovetski8f33d522010-12-22 14:46:46 +01001080}
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +01001081static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
Guennadi Liakhovetski8f33d522010-12-22 14:46:46 +01001082 dma_filter_fn fn, void *fn_param)
1083{
1084 return NULL;
1085}
Stephen Warren0ad7c002013-11-26 10:04:22 -07001086static inline struct dma_chan *dma_request_slave_channel_reason(
1087 struct device *dev, const char *name)
1088{
1089 return ERR_PTR(-ENODEV);
1090}
Jon Hunter9a6cecc2012-09-14 17:41:57 -05001091static inline struct dma_chan *dma_request_slave_channel(struct device *dev,
Markus Pargmannbef29ec2013-02-24 16:36:09 +01001092 const char *name)
Jon Hunter9a6cecc2012-09-14 17:41:57 -05001093{
Vinod Kould18d5f52012-09-25 16:18:55 +05301094 return NULL;
Jon Hunter9a6cecc2012-09-14 17:41:57 -05001095}
Guennadi Liakhovetski8f33d522010-12-22 14:46:46 +01001096static inline void dma_release_channel(struct dma_chan *chan)
1097{
Dan Williamsc50331e2009-01-19 15:33:14 -07001098}
Laurent Pinchartfdb8df92015-01-19 13:54:27 +02001099static inline int dma_get_slave_caps(struct dma_chan *chan,
1100 struct dma_slave_caps *caps)
1101{
1102 return -ENXIO;
1103}
Dan Williams07f22112009-01-05 17:14:31 -07001104#endif
Chris Leechc13c8262006-05-23 17:18:44 -07001105
1106/* --- DMA device --- */
1107
1108int dma_async_device_register(struct dma_device *device);
1109void dma_async_device_unregister(struct dma_device *device);
Dan Williams07f22112009-01-05 17:14:31 -07001110void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
Zhangfei Gao7bb587f2013-06-28 20:39:12 +08001111struct dma_chan *dma_get_slave_channel(struct dma_chan *chan);
Stephen Warren8010dad2013-11-26 12:40:51 -07001112struct dma_chan *dma_get_any_slave_channel(struct dma_device *device);
Dan Williams59b5ec22009-01-06 11:38:15 -07001113#define dma_request_channel(mask, x, y) __dma_request_channel(&(mask), x, y)
Matt Porter864ef692013-02-01 18:22:52 +00001114#define dma_request_slave_channel_compat(mask, x, y, dev, name) \
1115 __dma_request_slave_channel_compat(&(mask), x, y, dev, name)
1116
1117static inline struct dma_chan
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +01001118*__dma_request_slave_channel_compat(const dma_cap_mask_t *mask,
1119 dma_filter_fn fn, void *fn_param,
1120 struct device *dev, char *name)
Matt Porter864ef692013-02-01 18:22:52 +00001121{
1122 struct dma_chan *chan;
1123
1124 chan = dma_request_slave_channel(dev, name);
1125 if (chan)
1126 return chan;
1127
1128 return __dma_request_channel(mask, fn, fn_param);
1129}
Chris Leechc13c8262006-05-23 17:18:44 -07001130#endif /* DMAENGINE_H */