Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
0004  */
0005 #ifndef LINUX_DMAENGINE_H
0006 #define LINUX_DMAENGINE_H
0007 
0008 #include <linux/device.h>
0009 #include <linux/err.h>
0010 #include <linux/uio.h>
0011 #include <linux/bug.h>
0012 #include <linux/scatterlist.h>
0013 #include <linux/bitmap.h>
0014 #include <linux/types.h>
0015 #include <asm/page.h>
0016 
0017 /**
0018  * typedef dma_cookie_t - an opaque DMA cookie
0019  *
0020  * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
0021  */
0022 typedef s32 dma_cookie_t;
0023 #define DMA_MIN_COOKIE  1
0024 
0025 static inline int dma_submit_error(dma_cookie_t cookie)
0026 {
0027     return cookie < 0 ? cookie : 0;
0028 }
0029 
0030 /**
0031  * enum dma_status - DMA transaction status
0032  * @DMA_COMPLETE: transaction completed
0033  * @DMA_IN_PROGRESS: transaction not yet processed
0034  * @DMA_PAUSED: transaction is paused
0035  * @DMA_ERROR: transaction failed
0036  */
0037 enum dma_status {
0038     DMA_COMPLETE,
0039     DMA_IN_PROGRESS,
0040     DMA_PAUSED,
0041     DMA_ERROR,
0042     DMA_OUT_OF_ORDER,
0043 };
0044 
0045 /**
0046  * enum dma_transaction_type - DMA transaction types/indexes
0047  *
0048  * Note: The DMA_ASYNC_TX capability is not to be set by drivers.  It is
0049  * automatically set as dma devices are registered.
0050  */
0051 enum dma_transaction_type {
0052     DMA_MEMCPY,
0053     DMA_XOR,
0054     DMA_PQ,
0055     DMA_XOR_VAL,
0056     DMA_PQ_VAL,
0057     DMA_MEMSET,
0058     DMA_MEMSET_SG,
0059     DMA_INTERRUPT,
0060     DMA_PRIVATE,
0061     DMA_ASYNC_TX,
0062     DMA_SLAVE,
0063     DMA_CYCLIC,
0064     DMA_INTERLEAVE,
0065     DMA_COMPLETION_NO_ORDER,
0066     DMA_REPEAT,
0067     DMA_LOAD_EOT,
0068 /* last transaction type for creation of the capabilities mask */
0069     DMA_TX_TYPE_END,
0070 };
0071 
0072 /**
0073  * enum dma_transfer_direction - dma transfer mode and direction indicator
0074  * @DMA_MEM_TO_MEM: Async/Memcpy mode
0075  * @DMA_MEM_TO_DEV: Slave mode & From Memory to Device
0076  * @DMA_DEV_TO_MEM: Slave mode & From Device to Memory
0077  * @DMA_DEV_TO_DEV: Slave mode & From Device to Device
0078  */
0079 enum dma_transfer_direction {
0080     DMA_MEM_TO_MEM,
0081     DMA_MEM_TO_DEV,
0082     DMA_DEV_TO_MEM,
0083     DMA_DEV_TO_DEV,
0084     DMA_TRANS_NONE,
0085 };
0086 
0087 /**
0088  * Interleaved Transfer Request
0089  * ----------------------------
0090  * A chunk is collection of contiguous bytes to be transferred.
0091  * The gap(in bytes) between two chunks is called inter-chunk-gap(ICG).
0092  * ICGs may or may not change between chunks.
0093  * A FRAME is the smallest series of contiguous {chunk,icg} pairs,
0094  *  that when repeated an integral number of times, specifies the transfer.
0095  * A transfer template is specification of a Frame, the number of times
0096  *  it is to be repeated and other per-transfer attributes.
0097  *
0098  * Practically, a client driver would have ready a template for each
0099  *  type of transfer it is going to need during its lifetime and
0100  *  set only 'src_start' and 'dst_start' before submitting the requests.
0101  *
0102  *
0103  *  |      Frame-1        |       Frame-2       | ~ |       Frame-'numf'  |
0104  *  |====....==.===...=...|====....==.===...=...| ~ |====....==.===...=...|
0105  *
0106  *    ==  Chunk size
0107  *    ... ICG
0108  */
0109 
0110 /**
0111  * struct data_chunk - Element of scatter-gather list that makes a frame.
0112  * @size: Number of bytes to read from source.
0113  *    size_dst := fn(op, size_src), so doesn't mean much for destination.
0114  * @icg: Number of bytes to jump after last src/dst address of this
0115  *   chunk and before first src/dst address for next chunk.
0116  *   Ignored for dst(assumed 0), if dst_inc is true and dst_sgl is false.
0117  *   Ignored for src(assumed 0), if src_inc is true and src_sgl is false.
0118  * @dst_icg: Number of bytes to jump after last dst address of this
0119  *   chunk and before the first dst address for next chunk.
0120  *   Ignored if dst_inc is true and dst_sgl is false.
0121  * @src_icg: Number of bytes to jump after last src address of this
0122  *   chunk and before the first src address for next chunk.
0123  *   Ignored if src_inc is true and src_sgl is false.
0124  */
0125 struct data_chunk {
0126     size_t size;
0127     size_t icg;
0128     size_t dst_icg;
0129     size_t src_icg;
0130 };
0131 
0132 /**
0133  * struct dma_interleaved_template - Template to convey DMAC the transfer pattern
0134  *   and attributes.
0135  * @src_start: Bus address of source for the first chunk.
0136  * @dst_start: Bus address of destination for the first chunk.
0137  * @dir: Specifies the type of Source and Destination.
0138  * @src_inc: If the source address increments after reading from it.
0139  * @dst_inc: If the destination address increments after writing to it.
0140  * @src_sgl: If the 'icg' of sgl[] applies to Source (scattered read).
0141  *      Otherwise, source is read contiguously (icg ignored).
0142  *      Ignored if src_inc is false.
0143  * @dst_sgl: If the 'icg' of sgl[] applies to Destination (scattered write).
0144  *      Otherwise, destination is filled contiguously (icg ignored).
0145  *      Ignored if dst_inc is false.
0146  * @numf: Number of frames in this template.
0147  * @frame_size: Number of chunks in a frame i.e, size of sgl[].
0148  * @sgl: Array of {chunk,icg} pairs that make up a frame.
0149  */
0150 struct dma_interleaved_template {
0151     dma_addr_t src_start;
0152     dma_addr_t dst_start;
0153     enum dma_transfer_direction dir;
0154     bool src_inc;
0155     bool dst_inc;
0156     bool src_sgl;
0157     bool dst_sgl;
0158     size_t numf;
0159     size_t frame_size;
0160     struct data_chunk sgl[];
0161 };
0162 
0163 /**
0164  * enum dma_ctrl_flags - DMA flags to augment operation preparation,
0165  *  control completion, and communicate status.
0166  * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
0167  *  this transaction
0168  * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client
0169  *  acknowledges receipt, i.e. has a chance to establish any dependency
0170  *  chains
0171  * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q
0172  * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P
0173  * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as
0174  *  sources that were the result of a previous operation, in the case of a PQ
0175  *  operation it continues the calculation with new sources
0176  * @DMA_PREP_FENCE - tell the driver that subsequent operations depend
0177  *  on the result of this operation
0178  * @DMA_CTRL_REUSE: client can reuse the descriptor and submit again till
0179  *  cleared or freed
0180  * @DMA_PREP_CMD: tell the driver that the data passed to DMA API is command
0181  *  data and the descriptor should be in different format from normal
0182  *  data descriptors.
0183  * @DMA_PREP_REPEAT: tell the driver that the transaction shall be automatically
0184  *  repeated when it ends until a transaction is issued on the same channel
0185  *  with the DMA_PREP_LOAD_EOT flag set. This flag is only applicable to
0186  *  interleaved transactions and is ignored for all other transaction types.
0187  * @DMA_PREP_LOAD_EOT: tell the driver that the transaction shall replace any
0188  *  active repeated (as indicated by DMA_PREP_REPEAT) transaction when the
0189  *  repeated transaction ends. Not setting this flag when the previously queued
0190  *  transaction is marked with DMA_PREP_REPEAT will cause the new transaction
0191  *  to never be processed and stay in the issued queue forever. The flag is
0192  *  ignored if the previous transaction is not a repeated transaction.
0193  */
0194 enum dma_ctrl_flags {
0195     DMA_PREP_INTERRUPT = (1 << 0),
0196     DMA_CTRL_ACK = (1 << 1),
0197     DMA_PREP_PQ_DISABLE_P = (1 << 2),
0198     DMA_PREP_PQ_DISABLE_Q = (1 << 3),
0199     DMA_PREP_CONTINUE = (1 << 4),
0200     DMA_PREP_FENCE = (1 << 5),
0201     DMA_CTRL_REUSE = (1 << 6),
0202     DMA_PREP_CMD = (1 << 7),
0203     DMA_PREP_REPEAT = (1 << 8),
0204     DMA_PREP_LOAD_EOT = (1 << 9),
0205 };
0206 
0207 /**
0208  * enum sum_check_bits - bit position of pq_check_flags
0209  */
0210 enum sum_check_bits {
0211     SUM_CHECK_P = 0,
0212     SUM_CHECK_Q = 1,
0213 };
0214 
0215 /**
0216  * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations
0217  * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
0218  * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
0219  */
0220 enum sum_check_flags {
0221     SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
0222     SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
0223 };
0224 
0225 
0226 /**
0227  * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
0228  * See linux/cpumask.h
0229  */
0230 typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
0231 
0232 /**
0233  * enum dma_desc_metadata_mode - per descriptor metadata mode types supported
0234  * @DESC_METADATA_CLIENT - the metadata buffer is allocated/provided by the
0235  *  client driver and it is attached (via the dmaengine_desc_attach_metadata()
0236  *  helper) to the descriptor.
0237  *
0238  * Client drivers interested to use this mode can follow:
0239  * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
0240  *   1. prepare the descriptor (dmaengine_prep_*)
0241  *  construct the metadata in the client's buffer
0242  *   2. use dmaengine_desc_attach_metadata() to attach the buffer to the
0243  *  descriptor
0244  *   3. submit the transfer
0245  * - DMA_DEV_TO_MEM:
0246  *   1. prepare the descriptor (dmaengine_prep_*)
0247  *   2. use dmaengine_desc_attach_metadata() to attach the buffer to the
0248  *  descriptor
0249  *   3. submit the transfer
0250  *   4. when the transfer is completed, the metadata should be available in the
0251  *  attached buffer
0252  *
0253  * @DESC_METADATA_ENGINE - the metadata buffer is allocated/managed by the DMA
0254  *  driver. The client driver can ask for the pointer, maximum size and the
0255  *  currently used size of the metadata and can directly update or read it.
0256  *  dmaengine_desc_get_metadata_ptr() and dmaengine_desc_set_metadata_len() is
0257  *  provided as helper functions.
0258  *
0259  *  Note: the metadata area for the descriptor is no longer valid after the
0260  *  transfer has been completed (valid up to the point when the completion
0261  *  callback returns if used).
0262  *
0263  * Client drivers interested to use this mode can follow:
0264  * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
0265  *   1. prepare the descriptor (dmaengine_prep_*)
0266  *   2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the engine's
0267  *  metadata area
0268  *   3. update the metadata at the pointer
0269  *   4. use dmaengine_desc_set_metadata_len()  to tell the DMA engine the amount
0270  *  of data the client has placed into the metadata buffer
0271  *   5. submit the transfer
0272  * - DMA_DEV_TO_MEM:
0273  *   1. prepare the descriptor (dmaengine_prep_*)
0274  *   2. submit the transfer
0275  *   3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get the
0276  *  pointer to the engine's metadata area
0277  *   4. Read out the metadata from the pointer
0278  *
0279  * Note: the two mode is not compatible and clients must use one mode for a
0280  * descriptor.
0281  */
0282 enum dma_desc_metadata_mode {
0283     DESC_METADATA_NONE = 0,
0284     DESC_METADATA_CLIENT = BIT(0),
0285     DESC_METADATA_ENGINE = BIT(1),
0286 };
0287 
0288 /**
0289  * struct dma_chan_percpu - the per-CPU part of struct dma_chan
0290  * @memcpy_count: transaction counter
0291  * @bytes_transferred: byte counter
0292  */
0293 struct dma_chan_percpu {
0294     /* stats */
0295     unsigned long memcpy_count;
0296     unsigned long bytes_transferred;
0297 };
0298 
0299 /**
0300  * struct dma_router - DMA router structure
0301  * @dev: pointer to the DMA router device
0302  * @route_free: function to be called when the route can be disconnected
0303  */
0304 struct dma_router {
0305     struct device *dev;
0306     void (*route_free)(struct device *dev, void *route_data);
0307 };
0308 
0309 /**
0310  * struct dma_chan - devices supply DMA channels, clients use them
0311  * @device: ptr to the dma device who supplies this channel, always !%NULL
0312  * @slave: ptr to the device using this channel
0313  * @cookie: last cookie value returned to client
0314  * @completed_cookie: last completed cookie for this channel
0315  * @chan_id: channel ID for sysfs
0316  * @dev: class device for sysfs
0317  * @name: backlink name for sysfs
0318  * @dbg_client_name: slave name for debugfs in format:
0319  *  dev_name(requester's dev):channel name, for example: "2b00000.mcasp:tx"
0320  * @device_node: used to add this to the device chan list
0321  * @local: per-cpu pointer to a struct dma_chan_percpu
0322  * @client_count: how many clients are using this channel
0323  * @table_count: number of appearances in the mem-to-mem allocation table
0324  * @router: pointer to the DMA router structure
0325  * @route_data: channel specific data for the router
0326  * @private: private data for certain client-channel associations
0327  */
0328 struct dma_chan {
0329     struct dma_device *device;
0330     struct device *slave;
0331     dma_cookie_t cookie;
0332     dma_cookie_t completed_cookie;
0333 
0334     /* sysfs */
0335     int chan_id;
0336     struct dma_chan_dev *dev;
0337     const char *name;
0338 #ifdef CONFIG_DEBUG_FS
0339     char *dbg_client_name;
0340 #endif
0341 
0342     struct list_head device_node;
0343     struct dma_chan_percpu __percpu *local;
0344     int client_count;
0345     int table_count;
0346 
0347     /* DMA router */
0348     struct dma_router *router;
0349     void *route_data;
0350 
0351     void *private;
0352 };
0353 
0354 /**
0355  * struct dma_chan_dev - relate sysfs device node to backing channel device
0356  * @chan: driver channel device
0357  * @device: sysfs device
0358  * @dev_id: parent dma_device dev_id
0359  * @chan_dma_dev: The channel is using custom/different dma-mapping
0360  * compared to the parent dma_device
0361  */
0362 struct dma_chan_dev {
0363     struct dma_chan *chan;
0364     struct device device;
0365     int dev_id;
0366     bool chan_dma_dev;
0367 };
0368 
0369 /**
0370  * enum dma_slave_buswidth - defines bus width of the DMA slave
0371  * device, source or target buses
0372  */
0373 enum dma_slave_buswidth {
0374     DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
0375     DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
0376     DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
0377     DMA_SLAVE_BUSWIDTH_3_BYTES = 3,
0378     DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
0379     DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
0380     DMA_SLAVE_BUSWIDTH_16_BYTES = 16,
0381     DMA_SLAVE_BUSWIDTH_32_BYTES = 32,
0382     DMA_SLAVE_BUSWIDTH_64_BYTES = 64,
0383     DMA_SLAVE_BUSWIDTH_128_BYTES = 128,
0384 };
0385 
0386 /**
0387  * struct dma_slave_config - dma slave channel runtime config
0388  * @direction: whether the data shall go in or out on this slave
0389  * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are
0390  * legal values. DEPRECATED, drivers should use the direction argument
0391  * to the device_prep_slave_sg and device_prep_dma_cyclic functions or
0392  * the dir field in the dma_interleaved_template structure.
0393  * @src_addr: this is the physical address where DMA slave data
0394  * should be read (RX), if the source is memory this argument is
0395  * ignored.
0396  * @dst_addr: this is the physical address where DMA slave data
0397  * should be written (TX), if the source is memory this argument
0398  * is ignored.
0399  * @src_addr_width: this is the width in bytes of the source (RX)
0400  * register where DMA data shall be read. If the source
0401  * is memory this may be ignored depending on architecture.
0402  * Legal values: 1, 2, 3, 4, 8, 16, 32, 64, 128.
0403  * @dst_addr_width: same as src_addr_width but for destination
0404  * target (TX) mutatis mutandis.
0405  * @src_maxburst: the maximum number of words (note: words, as in
0406  * units of the src_addr_width member, not bytes) that can be sent
0407  * in one burst to the device. Typically something like half the
0408  * FIFO depth on I/O peripherals so you don't overflow it. This
0409  * may or may not be applicable on memory sources.
0410  * @dst_maxburst: same as src_maxburst but for destination target
0411  * mutatis mutandis.
0412  * @src_port_window_size: The length of the register area in words the data need
0413  * to be accessed on the device side. It is only used for devices which is using
0414  * an area instead of a single register to receive the data. Typically the DMA
0415  * loops in this area in order to transfer the data.
0416  * @dst_port_window_size: same as src_port_window_size but for the destination
0417  * port.
0418  * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill
0419  * with 'true' if peripheral should be flow controller. Direction will be
0420  * selected at Runtime.
0421  * @peripheral_config: peripheral configuration for programming peripheral
0422  * for dmaengine transfer
0423  * @peripheral_size: peripheral configuration buffer size
0424  *
0425  * This struct is passed in as configuration data to a DMA engine
0426  * in order to set up a certain channel for DMA transport at runtime.
0427  * The DMA device/engine has to provide support for an additional
0428  * callback in the dma_device structure, device_config and this struct
0429  * will then be passed in as an argument to the function.
0430  *
0431  * The rationale for adding configuration information to this struct is as
0432  * follows: if it is likely that more than one DMA slave controllers in
0433  * the world will support the configuration option, then make it generic.
0434  * If not: if it is fixed so that it be sent in static from the platform
0435  * data, then prefer to do that.
0436  */
0437 struct dma_slave_config {
0438     enum dma_transfer_direction direction;
0439     phys_addr_t src_addr;
0440     phys_addr_t dst_addr;
0441     enum dma_slave_buswidth src_addr_width;
0442     enum dma_slave_buswidth dst_addr_width;
0443     u32 src_maxburst;
0444     u32 dst_maxburst;
0445     u32 src_port_window_size;
0446     u32 dst_port_window_size;
0447     bool device_fc;
0448     void *peripheral_config;
0449     size_t peripheral_size;
0450 };
0451 
0452 /**
0453  * enum dma_residue_granularity - Granularity of the reported transfer residue
0454  * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The
0455  *  DMA channel is only able to tell whether a descriptor has been completed or
0456  *  not, which means residue reporting is not supported by this channel. The
0457  *  residue field of the dma_tx_state field will always be 0.
0458  * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully
0459  *  completed segment of the transfer (For cyclic transfers this is after each
0460  *  period). This is typically implemented by having the hardware generate an
0461  *  interrupt after each transferred segment and then the drivers updates the
0462  *  outstanding residue by the size of the segment. Another possibility is if
0463  *  the hardware supports scatter-gather and the segment descriptor has a field
0464  *  which gets set after the segment has been completed. The driver then counts
0465  *  the number of segments without the flag set to compute the residue.
0466  * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred
0467  *  burst. This is typically only supported if the hardware has a progress
0468  *  register of some sort (E.g. a register with the current read/write address
0469  *  or a register with the amount of bursts/beats/bytes that have been
0470  *  transferred or still need to be transferred).
0471  */
0472 enum dma_residue_granularity {
0473     DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0,
0474     DMA_RESIDUE_GRANULARITY_SEGMENT = 1,
0475     DMA_RESIDUE_GRANULARITY_BURST = 2,
0476 };
0477 
0478 /**
0479  * struct dma_slave_caps - expose capabilities of a slave channel only
0480  * @src_addr_widths: bit mask of src addr widths the channel supports.
0481  *  Width is specified in bytes, e.g. for a channel supporting
0482  *  a width of 4 the mask should have BIT(4) set.
0483  * @dst_addr_widths: bit mask of dst addr widths the channel supports
0484  * @directions: bit mask of slave directions the channel supports.
0485  *  Since the enum dma_transfer_direction is not defined as bit flag for
0486  *  each type, the dma controller should set BIT(<TYPE>) and same
0487  *  should be checked by controller as well
0488  * @min_burst: min burst capability per-transfer
0489  * @max_burst: max burst capability per-transfer
0490  * @max_sg_burst: max number of SG list entries executed in a single burst
0491  *  DMA tansaction with no software intervention for reinitialization.
0492  *  Zero value means unlimited number of entries.
0493  * @cmd_pause: true, if pause is supported (i.e. for reading residue or
0494  *         for resume later)
0495  * @cmd_resume: true, if resume is supported
0496  * @cmd_terminate: true, if terminate cmd is supported
0497  * @residue_granularity: granularity of the reported transfer residue
0498  * @descriptor_reuse: if a descriptor can be reused by client and
0499  * resubmitted multiple times
0500  */
0501 struct dma_slave_caps {
0502     u32 src_addr_widths;
0503     u32 dst_addr_widths;
0504     u32 directions;
0505     u32 min_burst;
0506     u32 max_burst;
0507     u32 max_sg_burst;
0508     bool cmd_pause;
0509     bool cmd_resume;
0510     bool cmd_terminate;
0511     enum dma_residue_granularity residue_granularity;
0512     bool descriptor_reuse;
0513 };
0514 
0515 static inline const char *dma_chan_name(struct dma_chan *chan)
0516 {
0517     return dev_name(&chan->dev->device);
0518 }
0519 
0520 void dma_chan_cleanup(struct kref *kref);
0521 
0522 /**
0523  * typedef dma_filter_fn - callback filter for dma_request_channel
0524  * @chan: channel to be reviewed
0525  * @filter_param: opaque parameter passed through dma_request_channel
0526  *
0527  * When this optional parameter is specified in a call to dma_request_channel a
0528  * suitable channel is passed to this routine for further dispositioning before
0529  * being returned.  Where 'suitable' indicates a non-busy channel that
0530  * satisfies the given capability mask.  It returns 'true' to indicate that the
0531  * channel is suitable.
0532  */
0533 typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
0534 
0535 typedef void (*dma_async_tx_callback)(void *dma_async_param);
0536 
0537 enum dmaengine_tx_result {
0538     DMA_TRANS_NOERROR = 0,      /* SUCCESS */
0539     DMA_TRANS_READ_FAILED,      /* Source DMA read failed */
0540     DMA_TRANS_WRITE_FAILED,     /* Destination DMA write failed */
0541     DMA_TRANS_ABORTED,      /* Op never submitted / aborted */
0542 };
0543 
0544 struct dmaengine_result {
0545     enum dmaengine_tx_result result;
0546     u32 residue;
0547 };
0548 
0549 typedef void (*dma_async_tx_callback_result)(void *dma_async_param,
0550                 const struct dmaengine_result *result);
0551 
0552 struct dmaengine_unmap_data {
0553 #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
0554     u16 map_cnt;
0555 #else
0556     u8 map_cnt;
0557 #endif
0558     u8 to_cnt;
0559     u8 from_cnt;
0560     u8 bidi_cnt;
0561     struct device *dev;
0562     struct kref kref;
0563     size_t len;
0564     dma_addr_t addr[];
0565 };
0566 
0567 struct dma_async_tx_descriptor;
0568 
0569 struct dma_descriptor_metadata_ops {
0570     int (*attach)(struct dma_async_tx_descriptor *desc, void *data,
0571               size_t len);
0572 
0573     void *(*get_ptr)(struct dma_async_tx_descriptor *desc,
0574              size_t *payload_len, size_t *max_len);
0575     int (*set_len)(struct dma_async_tx_descriptor *desc,
0576                size_t payload_len);
0577 };
0578 
0579 /**
0580  * struct dma_async_tx_descriptor - async transaction descriptor
0581  * ---dma generic offload fields---
0582  * @cookie: tracking cookie for this transaction, set to -EBUSY if
0583  *  this tx is sitting on a dependency list
0584  * @flags: flags to augment operation preparation, control completion, and
0585  *  communicate status
0586  * @phys: physical address of the descriptor
0587  * @chan: target channel for this operation
0588  * @tx_submit: accept the descriptor, assign ordered cookie and mark the
0589  * descriptor pending. To be pushed on .issue_pending() call
0590  * @callback: routine to call after this operation is complete
0591  * @callback_param: general parameter to pass to the callback routine
0592  * @desc_metadata_mode: core managed metadata mode to protect mixed use of
0593  *  DESC_METADATA_CLIENT or DESC_METADATA_ENGINE. Otherwise
0594  *  DESC_METADATA_NONE
0595  * @metadata_ops: DMA driver provided metadata mode ops, need to be set by the
0596  *  DMA driver if metadata mode is supported with the descriptor
0597  * ---async_tx api specific fields---
0598  * @next: at completion submit this descriptor
0599  * @parent: pointer to the next level up in the dependency chain
0600  * @lock: protect the parent and next pointers
0601  */
0602 struct dma_async_tx_descriptor {
0603     dma_cookie_t cookie;
0604     enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
0605     dma_addr_t phys;
0606     struct dma_chan *chan;
0607     dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
0608     int (*desc_free)(struct dma_async_tx_descriptor *tx);
0609     dma_async_tx_callback callback;
0610     dma_async_tx_callback_result callback_result;
0611     void *callback_param;
0612     struct dmaengine_unmap_data *unmap;
0613     enum dma_desc_metadata_mode desc_metadata_mode;
0614     struct dma_descriptor_metadata_ops *metadata_ops;
0615 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
0616     struct dma_async_tx_descriptor *next;
0617     struct dma_async_tx_descriptor *parent;
0618     spinlock_t lock;
0619 #endif
0620 };
0621 
0622 #ifdef CONFIG_DMA_ENGINE
0623 static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
0624                  struct dmaengine_unmap_data *unmap)
0625 {
0626     kref_get(&unmap->kref);
0627     tx->unmap = unmap;
0628 }
0629 
0630 struct dmaengine_unmap_data *
0631 dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags);
0632 void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap);
0633 #else
0634 static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
0635                  struct dmaengine_unmap_data *unmap)
0636 {
0637 }
0638 static inline struct dmaengine_unmap_data *
0639 dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
0640 {
0641     return NULL;
0642 }
0643 static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
0644 {
0645 }
0646 #endif
0647 
0648 static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
0649 {
0650     if (!tx->unmap)
0651         return;
0652 
0653     dmaengine_unmap_put(tx->unmap);
0654     tx->unmap = NULL;
0655 }
0656 
0657 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
0658 static inline void txd_lock(struct dma_async_tx_descriptor *txd)
0659 {
0660 }
0661 static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
0662 {
0663 }
0664 static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
0665 {
0666     BUG();
0667 }
0668 static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
0669 {
0670 }
0671 static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
0672 {
0673 }
0674 static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
0675 {
0676     return NULL;
0677 }
0678 static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
0679 {
0680     return NULL;
0681 }
0682 
0683 #else
0684 static inline void txd_lock(struct dma_async_tx_descriptor *txd)
0685 {
0686     spin_lock_bh(&txd->lock);
0687 }
0688 static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
0689 {
0690     spin_unlock_bh(&txd->lock);
0691 }
0692 static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
0693 {
0694     txd->next = next;
0695     next->parent = txd;
0696 }
0697 static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
0698 {
0699     txd->parent = NULL;
0700 }
0701 static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
0702 {
0703     txd->next = NULL;
0704 }
0705 static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
0706 {
0707     return txd->parent;
0708 }
0709 static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
0710 {
0711     return txd->next;
0712 }
0713 #endif
0714 
0715 /**
0716  * struct dma_tx_state - filled in to report the status of
0717  * a transfer.
0718  * @last: last completed DMA cookie
0719  * @used: last issued DMA cookie (i.e. the one in progress)
0720  * @residue: the remaining number of bytes left to transmit
0721  *  on the selected transfer for states DMA_IN_PROGRESS and
0722  *  DMA_PAUSED if this is implemented in the driver, else 0
0723  * @in_flight_bytes: amount of data in bytes cached by the DMA.
0724  */
0725 struct dma_tx_state {
0726     dma_cookie_t last;
0727     dma_cookie_t used;
0728     u32 residue;
0729     u32 in_flight_bytes;
0730 };
0731 
0732 /**
0733  * enum dmaengine_alignment - defines alignment of the DMA async tx
0734  * buffers
0735  */
0736 enum dmaengine_alignment {
0737     DMAENGINE_ALIGN_1_BYTE = 0,
0738     DMAENGINE_ALIGN_2_BYTES = 1,
0739     DMAENGINE_ALIGN_4_BYTES = 2,
0740     DMAENGINE_ALIGN_8_BYTES = 3,
0741     DMAENGINE_ALIGN_16_BYTES = 4,
0742     DMAENGINE_ALIGN_32_BYTES = 5,
0743     DMAENGINE_ALIGN_64_BYTES = 6,
0744     DMAENGINE_ALIGN_128_BYTES = 7,
0745     DMAENGINE_ALIGN_256_BYTES = 8,
0746 };
0747 
0748 /**
0749  * struct dma_slave_map - associates slave device and it's slave channel with
0750  * parameter to be used by a filter function
0751  * @devname: name of the device
0752  * @slave: slave channel name
0753  * @param: opaque parameter to pass to struct dma_filter.fn
0754  */
0755 struct dma_slave_map {
0756     const char *devname;
0757     const char *slave;
0758     void *param;
0759 };
0760 
0761 /**
0762  * struct dma_filter - information for slave device/channel to filter_fn/param
0763  * mapping
0764  * @fn: filter function callback
0765  * @mapcnt: number of slave device/channel in the map
0766  * @map: array of channel to filter mapping data
0767  */
0768 struct dma_filter {
0769     dma_filter_fn fn;
0770     int mapcnt;
0771     const struct dma_slave_map *map;
0772 };
0773 
0774 /**
0775  * struct dma_device - info on the entity supplying DMA services
0776  * @chancnt: how many DMA channels are supported
0777  * @privatecnt: how many DMA channels are requested by dma_request_channel
0778  * @channels: the list of struct dma_chan
0779  * @global_node: list_head for global dma_device_list
0780  * @filter: information for device/slave to filter function/param mapping
0781  * @cap_mask: one or more dma_capability flags
0782  * @desc_metadata_modes: supported metadata modes by the DMA device
0783  * @max_xor: maximum number of xor sources, 0 if no capability
0784  * @max_pq: maximum number of PQ sources and PQ-continue capability
0785  * @copy_align: alignment shift for memcpy operations
0786  * @xor_align: alignment shift for xor operations
0787  * @pq_align: alignment shift for pq operations
0788  * @fill_align: alignment shift for memset operations
0789  * @dev_id: unique device ID
0790  * @dev: struct device reference for dma mapping api
0791  * @owner: owner module (automatically set based on the provided dev)
0792  * @src_addr_widths: bit mask of src addr widths the device supports
0793  *  Width is specified in bytes, e.g. for a device supporting
0794  *  a width of 4 the mask should have BIT(4) set.
0795  * @dst_addr_widths: bit mask of dst addr widths the device supports
0796  * @directions: bit mask of slave directions the device supports.
0797  *  Since the enum dma_transfer_direction is not defined as bit flag for
0798  *  each type, the dma controller should set BIT(<TYPE>) and same
0799  *  should be checked by controller as well
0800  * @min_burst: min burst capability per-transfer
0801  * @max_burst: max burst capability per-transfer
0802  * @max_sg_burst: max number of SG list entries executed in a single burst
0803  *  DMA tansaction with no software intervention for reinitialization.
0804  *  Zero value means unlimited number of entries.
0805  * @residue_granularity: granularity of the transfer residue reported
0806  *  by tx_status
0807  * @device_alloc_chan_resources: allocate resources and return the
0808  *  number of allocated descriptors
0809  * @device_router_config: optional callback for DMA router configuration
0810  * @device_free_chan_resources: release DMA channel's resources
0811  * @device_prep_dma_memcpy: prepares a memcpy operation
0812  * @device_prep_dma_xor: prepares a xor operation
0813  * @device_prep_dma_xor_val: prepares a xor validation operation
0814  * @device_prep_dma_pq: prepares a pq operation
0815  * @device_prep_dma_pq_val: prepares a pqzero_sum operation
0816  * @device_prep_dma_memset: prepares a memset operation
0817  * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list
0818  * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
0819  * @device_prep_slave_sg: prepares a slave dma operation
0820  * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
0821  *  The function takes a buffer of size buf_len. The callback function will
0822  *  be called after period_len bytes have been transferred.
0823  * @device_prep_interleaved_dma: Transfer expression in a generic way.
0824  * @device_prep_dma_imm_data: DMA's 8 byte immediate data to the dst address
0825  * @device_caps: May be used to override the generic DMA slave capabilities
0826  *  with per-channel specific ones
0827  * @device_config: Pushes a new configuration to a channel, return 0 or an error
0828  *  code
0829  * @device_pause: Pauses any transfer happening on a channel. Returns
0830  *  0 or an error code
0831  * @device_resume: Resumes any transfer on a channel previously
0832  *  paused. Returns 0 or an error code
0833  * @device_terminate_all: Aborts all transfers on a channel. Returns 0
0834  *  or an error code
0835  * @device_synchronize: Synchronizes the termination of a transfers to the
0836  *  current context.
0837  * @device_tx_status: poll for transaction completion, the optional
0838  *  txstate parameter can be supplied with a pointer to get a
0839  *  struct with auxiliary transfer status information, otherwise the call
0840  *  will just return a simple status code
0841  * @device_issue_pending: push pending transactions to hardware
0842  * @descriptor_reuse: a submitted transfer can be resubmitted after completion
0843  * @device_release: called sometime atfer dma_async_device_unregister() is
0844  *     called and there are no further references to this structure. This
0845  *     must be implemented to free resources however many existing drivers
0846  *     do not and are therefore not safe to unbind while in use.
0847  * @dbg_summary_show: optional routine to show contents in debugfs; default code
0848  *     will be used when this is omitted, but custom code can show extra,
0849  *     controller specific information.
0850  */
0851 struct dma_device {
0852     struct kref ref;
0853     unsigned int chancnt;
0854     unsigned int privatecnt;
0855     struct list_head channels;
0856     struct list_head global_node;
0857     struct dma_filter filter;
0858     dma_cap_mask_t  cap_mask;
0859     enum dma_desc_metadata_mode desc_metadata_modes;
0860     unsigned short max_xor;
0861     unsigned short max_pq;
0862     enum dmaengine_alignment copy_align;
0863     enum dmaengine_alignment xor_align;
0864     enum dmaengine_alignment pq_align;
0865     enum dmaengine_alignment fill_align;
0866     #define DMA_HAS_PQ_CONTINUE (1 << 15)
0867 
0868     int dev_id;
0869     struct device *dev;
0870     struct module *owner;
0871     struct ida chan_ida;
0872 
0873     u32 src_addr_widths;
0874     u32 dst_addr_widths;
0875     u32 directions;
0876     u32 min_burst;
0877     u32 max_burst;
0878     u32 max_sg_burst;
0879     bool descriptor_reuse;
0880     enum dma_residue_granularity residue_granularity;
0881 
0882     int (*device_alloc_chan_resources)(struct dma_chan *chan);
0883     int (*device_router_config)(struct dma_chan *chan);
0884     void (*device_free_chan_resources)(struct dma_chan *chan);
0885 
0886     struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
0887         struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
0888         size_t len, unsigned long flags);
0889     struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
0890         struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
0891         unsigned int src_cnt, size_t len, unsigned long flags);
0892     struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
0893         struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
0894         size_t len, enum sum_check_flags *result, unsigned long flags);
0895     struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
0896         struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
0897         unsigned int src_cnt, const unsigned char *scf,
0898         size_t len, unsigned long flags);
0899     struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
0900         struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
0901         unsigned int src_cnt, const unsigned char *scf, size_t len,
0902         enum sum_check_flags *pqres, unsigned long flags);
0903     struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
0904         struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
0905         unsigned long flags);
0906     struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)(
0907         struct dma_chan *chan, struct scatterlist *sg,
0908         unsigned int nents, int value, unsigned long flags);
0909     struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
0910         struct dma_chan *chan, unsigned long flags);
0911 
0912     struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
0913         struct dma_chan *chan, struct scatterlist *sgl,
0914         unsigned int sg_len, enum dma_transfer_direction direction,
0915         unsigned long flags, void *context);
0916     struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
0917         struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
0918         size_t period_len, enum dma_transfer_direction direction,
0919         unsigned long flags);
0920     struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
0921         struct dma_chan *chan, struct dma_interleaved_template *xt,
0922         unsigned long flags);
0923     struct dma_async_tx_descriptor *(*device_prep_dma_imm_data)(
0924         struct dma_chan *chan, dma_addr_t dst, u64 data,
0925         unsigned long flags);
0926 
0927     void (*device_caps)(struct dma_chan *chan,
0928                 struct dma_slave_caps *caps);
0929     int (*device_config)(struct dma_chan *chan,
0930                  struct dma_slave_config *config);
0931     int (*device_pause)(struct dma_chan *chan);
0932     int (*device_resume)(struct dma_chan *chan);
0933     int (*device_terminate_all)(struct dma_chan *chan);
0934     void (*device_synchronize)(struct dma_chan *chan);
0935 
0936     enum dma_status (*device_tx_status)(struct dma_chan *chan,
0937                         dma_cookie_t cookie,
0938                         struct dma_tx_state *txstate);
0939     void (*device_issue_pending)(struct dma_chan *chan);
0940     void (*device_release)(struct dma_device *dev);
0941     /* debugfs support */
0942     void (*dbg_summary_show)(struct seq_file *s, struct dma_device *dev);
0943     struct dentry *dbg_dev_root;
0944 };
0945 
0946 static inline int dmaengine_slave_config(struct dma_chan *chan,
0947                       struct dma_slave_config *config)
0948 {
0949     if (chan->device->device_config)
0950         return chan->device->device_config(chan, config);
0951 
0952     return -ENOSYS;
0953 }
0954 
0955 static inline bool is_slave_direction(enum dma_transfer_direction direction)
0956 {
0957     return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM);
0958 }
0959 
0960 static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
0961     struct dma_chan *chan, dma_addr_t buf, size_t len,
0962     enum dma_transfer_direction dir, unsigned long flags)
0963 {
0964     struct scatterlist sg;
0965     sg_init_table(&sg, 1);
0966     sg_dma_address(&sg) = buf;
0967     sg_dma_len(&sg) = len;
0968 
0969     if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
0970         return NULL;
0971 
0972     return chan->device->device_prep_slave_sg(chan, &sg, 1,
0973                           dir, flags, NULL);
0974 }
0975 
0976 static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
0977     struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
0978     enum dma_transfer_direction dir, unsigned long flags)
0979 {
0980     if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
0981         return NULL;
0982 
0983     return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
0984                           dir, flags, NULL);
0985 }
0986 
0987 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
0988 struct rio_dma_ext;
0989 static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg(
0990     struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
0991     enum dma_transfer_direction dir, unsigned long flags,
0992     struct rio_dma_ext *rio_ext)
0993 {
0994     if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
0995         return NULL;
0996 
0997     return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
0998                           dir, flags, rio_ext);
0999 }
1000 #endif
1001 
1002 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
1003         struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1004         size_t period_len, enum dma_transfer_direction dir,
1005         unsigned long flags)
1006 {
1007     if (!chan || !chan->device || !chan->device->device_prep_dma_cyclic)
1008         return NULL;
1009 
1010     return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len,
1011                         period_len, dir, flags);
1012 }
1013 
1014 static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
1015         struct dma_chan *chan, struct dma_interleaved_template *xt,
1016         unsigned long flags)
1017 {
1018     if (!chan || !chan->device || !chan->device->device_prep_interleaved_dma)
1019         return NULL;
1020     if (flags & DMA_PREP_REPEAT &&
1021         !test_bit(DMA_REPEAT, chan->device->cap_mask.bits))
1022         return NULL;
1023 
1024     return chan->device->device_prep_interleaved_dma(chan, xt, flags);
1025 }
1026 
1027 /**
1028  * dmaengine_prep_dma_memset() - Prepare a DMA memset descriptor.
1029  * @chan: The channel to be used for this descriptor
1030  * @dest: Address of buffer to be set
1031  * @value: Treated as a single byte value that fills the destination buffer
1032  * @len: The total size of dest
1033  * @flags: DMA engine flags
1034  */
1035 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset(
1036         struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
1037         unsigned long flags)
1038 {
1039     if (!chan || !chan->device || !chan->device->device_prep_dma_memset)
1040         return NULL;
1041 
1042     return chan->device->device_prep_dma_memset(chan, dest, value,
1043                             len, flags);
1044 }
1045 
1046 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy(
1047         struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1048         size_t len, unsigned long flags)
1049 {
1050     if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy)
1051         return NULL;
1052 
1053     return chan->device->device_prep_dma_memcpy(chan, dest, src,
1054                             len, flags);
1055 }
1056 
1057 static inline bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan,
1058         enum dma_desc_metadata_mode mode)
1059 {
1060     if (!chan)
1061         return false;
1062 
1063     return !!(chan->device->desc_metadata_modes & mode);
1064 }
1065 
1066 #ifdef CONFIG_DMA_ENGINE
1067 int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,
1068                    void *data, size_t len);
1069 void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
1070                       size_t *payload_len, size_t *max_len);
1071 int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,
1072                     size_t payload_len);
1073 #else /* CONFIG_DMA_ENGINE */
1074 static inline int dmaengine_desc_attach_metadata(
1075         struct dma_async_tx_descriptor *desc, void *data, size_t len)
1076 {
1077     return -EINVAL;
1078 }
1079 static inline void *dmaengine_desc_get_metadata_ptr(
1080         struct dma_async_tx_descriptor *desc, size_t *payload_len,
1081         size_t *max_len)
1082 {
1083     return NULL;
1084 }
1085 static inline int dmaengine_desc_set_metadata_len(
1086         struct dma_async_tx_descriptor *desc, size_t payload_len)
1087 {
1088     return -EINVAL;
1089 }
1090 #endif /* CONFIG_DMA_ENGINE */
1091 
1092 /**
1093  * dmaengine_terminate_all() - Terminate all active DMA transfers
1094  * @chan: The channel for which to terminate the transfers
1095  *
1096  * This function is DEPRECATED use either dmaengine_terminate_sync() or
1097  * dmaengine_terminate_async() instead.
1098  */
1099 static inline int dmaengine_terminate_all(struct dma_chan *chan)
1100 {
1101     if (chan->device->device_terminate_all)
1102         return chan->device->device_terminate_all(chan);
1103 
1104     return -ENOSYS;
1105 }
1106 
1107 /**
1108  * dmaengine_terminate_async() - Terminate all active DMA transfers
1109  * @chan: The channel for which to terminate the transfers
1110  *
1111  * Calling this function will terminate all active and pending descriptors
1112  * that have previously been submitted to the channel. It is not guaranteed
1113  * though that the transfer for the active descriptor has stopped when the
1114  * function returns. Furthermore it is possible the complete callback of a
1115  * submitted transfer is still running when this function returns.
1116  *
1117  * dmaengine_synchronize() needs to be called before it is safe to free
1118  * any memory that is accessed by previously submitted descriptors or before
1119  * freeing any resources accessed from within the completion callback of any
1120  * previously submitted descriptors.
1121  *
1122  * This function can be called from atomic context as well as from within a
1123  * complete callback of a descriptor submitted on the same channel.
1124  *
1125  * If none of the two conditions above apply consider using
1126  * dmaengine_terminate_sync() instead.
1127  */
1128 static inline int dmaengine_terminate_async(struct dma_chan *chan)
1129 {
1130     if (chan->device->device_terminate_all)
1131         return chan->device->device_terminate_all(chan);
1132 
1133     return -EINVAL;
1134 }
1135 
1136 /**
1137  * dmaengine_synchronize() - Synchronize DMA channel termination
1138  * @chan: The channel to synchronize
1139  *
1140  * Synchronizes to the DMA channel termination to the current context. When this
1141  * function returns it is guaranteed that all transfers for previously issued
1142  * descriptors have stopped and it is safe to free the memory associated
1143  * with them. Furthermore it is guaranteed that all complete callback functions
1144  * for a previously submitted descriptor have finished running and it is safe to
1145  * free resources accessed from within the complete callbacks.
1146  *
1147  * The behavior of this function is undefined if dma_async_issue_pending() has
1148  * been called between dmaengine_terminate_async() and this function.
1149  *
1150  * This function must only be called from non-atomic context and must not be
1151  * called from within a complete callback of a descriptor submitted on the same
1152  * channel.
1153  */
1154 static inline void dmaengine_synchronize(struct dma_chan *chan)
1155 {
1156     might_sleep();
1157 
1158     if (chan->device->device_synchronize)
1159         chan->device->device_synchronize(chan);
1160 }
1161 
1162 /**
1163  * dmaengine_terminate_sync() - Terminate all active DMA transfers
1164  * @chan: The channel for which to terminate the transfers
1165  *
1166  * Calling this function will terminate all active and pending transfers
1167  * that have previously been submitted to the channel. It is similar to
1168  * dmaengine_terminate_async() but guarantees that the DMA transfer has actually
1169  * stopped and that all complete callbacks have finished running when the
1170  * function returns.
1171  *
1172  * This function must only be called from non-atomic context and must not be
1173  * called from within a complete callback of a descriptor submitted on the same
1174  * channel.
1175  */
1176 static inline int dmaengine_terminate_sync(struct dma_chan *chan)
1177 {
1178     int ret;
1179 
1180     ret = dmaengine_terminate_async(chan);
1181     if (ret)
1182         return ret;
1183 
1184     dmaengine_synchronize(chan);
1185 
1186     return 0;
1187 }
1188 
1189 static inline int dmaengine_pause(struct dma_chan *chan)
1190 {
1191     if (chan->device->device_pause)
1192         return chan->device->device_pause(chan);
1193 
1194     return -ENOSYS;
1195 }
1196 
1197 static inline int dmaengine_resume(struct dma_chan *chan)
1198 {
1199     if (chan->device->device_resume)
1200         return chan->device->device_resume(chan);
1201 
1202     return -ENOSYS;
1203 }
1204 
1205 static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan,
1206     dma_cookie_t cookie, struct dma_tx_state *state)
1207 {
1208     return chan->device->device_tx_status(chan, cookie, state);
1209 }
1210 
1211 static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
1212 {
1213     return desc->tx_submit(desc);
1214 }
1215 
1216 static inline bool dmaengine_check_align(enum dmaengine_alignment align,
1217                      size_t off1, size_t off2, size_t len)
1218 {
1219     return !(((1 << align) - 1) & (off1 | off2 | len));
1220 }
1221 
1222 static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
1223                        size_t off2, size_t len)
1224 {
1225     return dmaengine_check_align(dev->copy_align, off1, off2, len);
1226 }
1227 
1228 static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
1229                       size_t off2, size_t len)
1230 {
1231     return dmaengine_check_align(dev->xor_align, off1, off2, len);
1232 }
1233 
1234 static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
1235                      size_t off2, size_t len)
1236 {
1237     return dmaengine_check_align(dev->pq_align, off1, off2, len);
1238 }
1239 
1240 static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
1241                        size_t off2, size_t len)
1242 {
1243     return dmaengine_check_align(dev->fill_align, off1, off2, len);
1244 }
1245 
1246 static inline void
1247 dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
1248 {
1249     dma->max_pq = maxpq;
1250     if (has_pq_continue)
1251         dma->max_pq |= DMA_HAS_PQ_CONTINUE;
1252 }
1253 
1254 static inline bool dmaf_continue(enum dma_ctrl_flags flags)
1255 {
1256     return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
1257 }
1258 
1259 static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
1260 {
1261     enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
1262 
1263     return (flags & mask) == mask;
1264 }
1265 
1266 static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
1267 {
1268     return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
1269 }
1270 
1271 static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
1272 {
1273     return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
1274 }
1275 
1276 /* dma_maxpq - reduce maxpq in the face of continued operations
1277  * @dma - dma device with PQ capability
1278  * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
1279  *
1280  * When an engine does not support native continuation we need 3 extra
1281  * source slots to reuse P and Q with the following coefficients:
1282  * 1/ {00} * P : remove P from Q', but use it as a source for P'
1283  * 2/ {01} * Q : use Q to continue Q' calculation
1284  * 3/ {00} * Q : subtract Q from P' to cancel (2)
1285  *
1286  * In the case where P is disabled we only need 1 extra source:
1287  * 1/ {01} * Q : use Q to continue Q' calculation
1288  */
1289 static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
1290 {
1291     if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
1292         return dma_dev_to_maxpq(dma);
1293     if (dmaf_p_disabled_continue(flags))
1294         return dma_dev_to_maxpq(dma) - 1;
1295     if (dmaf_continue(flags))
1296         return dma_dev_to_maxpq(dma) - 3;
1297     BUG();
1298 }
1299 
1300 static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
1301                       size_t dir_icg)
1302 {
1303     if (inc) {
1304         if (dir_icg)
1305             return dir_icg;
1306         if (sgl)
1307             return icg;
1308     }
1309 
1310     return 0;
1311 }
1312 
1313 static inline size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt,
1314                        struct data_chunk *chunk)
1315 {
1316     return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl,
1317                  chunk->icg, chunk->dst_icg);
1318 }
1319 
1320 static inline size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt,
1321                        struct data_chunk *chunk)
1322 {
1323     return dmaengine_get_icg(xt->src_inc, xt->src_sgl,
1324                  chunk->icg, chunk->src_icg);
1325 }
1326 
1327 /* --- public DMA engine API --- */
1328 
1329 #ifdef CONFIG_DMA_ENGINE
1330 void dmaengine_get(void);
1331 void dmaengine_put(void);
1332 #else
1333 static inline void dmaengine_get(void)
1334 {
1335 }
1336 static inline void dmaengine_put(void)
1337 {
1338 }
1339 #endif
1340 
1341 #ifdef CONFIG_ASYNC_TX_DMA
1342 #define async_dmaengine_get()   dmaengine_get()
1343 #define async_dmaengine_put()   dmaengine_put()
1344 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
1345 #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
1346 #else
1347 #define async_dma_find_channel(type) dma_find_channel(type)
1348 #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
1349 #else
1350 static inline void async_dmaengine_get(void)
1351 {
1352 }
1353 static inline void async_dmaengine_put(void)
1354 {
1355 }
1356 static inline struct dma_chan *
1357 async_dma_find_channel(enum dma_transaction_type type)
1358 {
1359     return NULL;
1360 }
1361 #endif /* CONFIG_ASYNC_TX_DMA */
1362 void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1363                   struct dma_chan *chan);
1364 
1365 static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
1366 {
1367     tx->flags |= DMA_CTRL_ACK;
1368 }
1369 
1370 static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
1371 {
1372     tx->flags &= ~DMA_CTRL_ACK;
1373 }
1374 
1375 static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
1376 {
1377     return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
1378 }
1379 
1380 #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
1381 static inline void
1382 __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
1383 {
1384     set_bit(tx_type, dstp->bits);
1385 }
1386 
1387 #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
1388 static inline void
1389 __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
1390 {
1391     clear_bit(tx_type, dstp->bits);
1392 }
1393 
1394 #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
1395 static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
1396 {
1397     bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
1398 }
1399 
1400 #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
1401 static inline int
1402 __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
1403 {
1404     return test_bit(tx_type, srcp->bits);
1405 }
1406 
1407 #define for_each_dma_cap_mask(cap, mask) \
1408     for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END)
1409 
1410 /**
1411  * dma_async_issue_pending - flush pending transactions to HW
1412  * @chan: target DMA channel
1413  *
1414  * This allows drivers to push copies to HW in batches,
1415  * reducing MMIO writes where possible.
1416  */
1417 static inline void dma_async_issue_pending(struct dma_chan *chan)
1418 {
1419     chan->device->device_issue_pending(chan);
1420 }
1421 
1422 /**
1423  * dma_async_is_tx_complete - poll for transaction completion
1424  * @chan: DMA channel
1425  * @cookie: transaction identifier to check status of
1426  * @last: returns last completed cookie, can be NULL
1427  * @used: returns last issued cookie, can be NULL
1428  *
1429  * If @last and @used are passed in, upon return they reflect the driver
1430  * internal state and can be used with dma_async_is_complete() to check
1431  * the status of multiple cookies without re-checking hardware state.
1432  */
1433 static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
1434     dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
1435 {
1436     struct dma_tx_state state;
1437     enum dma_status status;
1438 
1439     status = chan->device->device_tx_status(chan, cookie, &state);
1440     if (last)
1441         *last = state.last;
1442     if (used)
1443         *used = state.used;
1444     return status;
1445 }
1446 
1447 /**
1448  * dma_async_is_complete - test a cookie against chan state
1449  * @cookie: transaction identifier to test status of
1450  * @last_complete: last know completed transaction
1451  * @last_used: last cookie value handed out
1452  *
1453  * dma_async_is_complete() is used in dma_async_is_tx_complete()
1454  * the test logic is separated for lightweight testing of multiple cookies
1455  */
1456 static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
1457             dma_cookie_t last_complete, dma_cookie_t last_used)
1458 {
1459     if (last_complete <= last_used) {
1460         if ((cookie <= last_complete) || (cookie > last_used))
1461             return DMA_COMPLETE;
1462     } else {
1463         if ((cookie <= last_complete) && (cookie > last_used))
1464             return DMA_COMPLETE;
1465     }
1466     return DMA_IN_PROGRESS;
1467 }
1468 
1469 static inline void
1470 dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
1471 {
1472     if (!st)
1473         return;
1474 
1475     st->last = last;
1476     st->used = used;
1477     st->residue = residue;
1478 }
1479 
1480 #ifdef CONFIG_DMA_ENGINE
1481 struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
1482 enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
1483 enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
1484 void dma_issue_pending_all(void);
1485 struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
1486                        dma_filter_fn fn, void *fn_param,
1487                        struct device_node *np);
1488 
1489 struct dma_chan *dma_request_chan(struct device *dev, const char *name);
1490 struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
1491 
1492 void dma_release_channel(struct dma_chan *chan);
1493 int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
1494 #else
1495 static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
1496 {
1497     return NULL;
1498 }
1499 static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
1500 {
1501     return DMA_COMPLETE;
1502 }
1503 static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1504 {
1505     return DMA_COMPLETE;
1506 }
1507 static inline void dma_issue_pending_all(void)
1508 {
1509 }
1510 static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
1511                              dma_filter_fn fn,
1512                              void *fn_param,
1513                              struct device_node *np)
1514 {
1515     return NULL;
1516 }
1517 static inline struct dma_chan *dma_request_chan(struct device *dev,
1518                         const char *name)
1519 {
1520     return ERR_PTR(-ENODEV);
1521 }
1522 static inline struct dma_chan *dma_request_chan_by_mask(
1523                         const dma_cap_mask_t *mask)
1524 {
1525     return ERR_PTR(-ENODEV);
1526 }
1527 static inline void dma_release_channel(struct dma_chan *chan)
1528 {
1529 }
1530 static inline int dma_get_slave_caps(struct dma_chan *chan,
1531                      struct dma_slave_caps *caps)
1532 {
1533     return -ENXIO;
1534 }
1535 #endif
1536 
1537 static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
1538 {
1539     struct dma_slave_caps caps;
1540     int ret;
1541 
1542     ret = dma_get_slave_caps(tx->chan, &caps);
1543     if (ret)
1544         return ret;
1545 
1546     if (!caps.descriptor_reuse)
1547         return -EPERM;
1548 
1549     tx->flags |= DMA_CTRL_REUSE;
1550     return 0;
1551 }
1552 
1553 static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
1554 {
1555     tx->flags &= ~DMA_CTRL_REUSE;
1556 }
1557 
1558 static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
1559 {
1560     return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE;
1561 }
1562 
1563 static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
1564 {
1565     /* this is supported for reusable desc, so check that */
1566     if (!dmaengine_desc_test_reuse(desc))
1567         return -EPERM;
1568 
1569     return desc->desc_free(desc);
1570 }
1571 
1572 /* --- DMA device --- */
1573 
1574 int dma_async_device_register(struct dma_device *device);
1575 int dmaenginem_async_device_register(struct dma_device *device);
1576 void dma_async_device_unregister(struct dma_device *device);
1577 int dma_async_device_channel_register(struct dma_device *device,
1578                       struct dma_chan *chan);
1579 void dma_async_device_channel_unregister(struct dma_device *device,
1580                      struct dma_chan *chan);
1581 void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
1582 #define dma_request_channel(mask, x, y) \
1583     __dma_request_channel(&(mask), x, y, NULL)
1584 
1585 /* Deprecated, please use dma_request_chan() directly */
1586 static inline struct dma_chan * __deprecated
1587 dma_request_slave_channel(struct device *dev, const char *name)
1588 {
1589     struct dma_chan *ch = dma_request_chan(dev, name);
1590 
1591     return IS_ERR(ch) ? NULL : ch;
1592 }
1593 
1594 static inline struct dma_chan
1595 *dma_request_slave_channel_compat(const dma_cap_mask_t mask,
1596                   dma_filter_fn fn, void *fn_param,
1597                   struct device *dev, const char *name)
1598 {
1599     struct dma_chan *chan;
1600 
1601     chan = dma_request_slave_channel(dev, name);
1602     if (chan)
1603         return chan;
1604 
1605     if (!fn || !fn_param)
1606         return NULL;
1607 
1608     return __dma_request_channel(&mask, fn, fn_param, NULL);
1609 }
1610 
1611 static inline char *
1612 dmaengine_get_direction_text(enum dma_transfer_direction dir)
1613 {
1614     switch (dir) {
1615     case DMA_DEV_TO_MEM:
1616         return "DEV_TO_MEM";
1617     case DMA_MEM_TO_DEV:
1618         return "MEM_TO_DEV";
1619     case DMA_MEM_TO_MEM:
1620         return "MEM_TO_MEM";
1621     case DMA_DEV_TO_DEV:
1622         return "DEV_TO_DEV";
1623     default:
1624         return "invalid";
1625     }
1626 }
1627 
1628 static inline struct device *dmaengine_get_dma_device(struct dma_chan *chan)
1629 {
1630     if (chan->dev->chan_dma_dev)
1631         return &chan->dev->device;
1632 
1633     return chan->device->dev;
1634 }
1635 
1636 #endif /* DMAENGINE_H */