19ab65affSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 2c13c8260SChris Leech /* 3c13c8260SChris Leech * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved. 4c13c8260SChris Leech */ 5d2ebfb33SRussell King - ARM Linux #ifndef LINUX_DMAENGINE_H 6d2ebfb33SRussell King - ARM Linux #define LINUX_DMAENGINE_H 71c0f16e5SDavid Woodhouse 8c13c8260SChris Leech #include <linux/device.h> 90ad7c000SStephen Warren #include <linux/err.h> 10c13c8260SChris Leech #include <linux/uio.h> 11187f1882SPaul Gortmaker #include <linux/bug.h> 1290b44f8fSVinod Koul #include <linux/scatterlist.h> 13a8efa9d6SPaul Gortmaker #include <linux/bitmap.h> 14dcc043dcSViresh Kumar #include <linux/types.h> 15a8efa9d6SPaul Gortmaker #include <asm/page.h> 16b7f080cfSAlexey Dobriyan 17c13c8260SChris Leech /** 18fe4ada2dSRandy Dunlap * typedef dma_cookie_t - an opaque DMA cookie 19c13c8260SChris Leech * 20c13c8260SChris Leech * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code 21c13c8260SChris Leech */ 22c13c8260SChris Leech typedef s32 dma_cookie_t; 2376bd061fSSteven J. Magnani #define DMA_MIN_COOKIE 1 24c13c8260SChris Leech 2571ea1483SDan Carpenter static inline int dma_submit_error(dma_cookie_t cookie) 2671ea1483SDan Carpenter { 2771ea1483SDan Carpenter return cookie < 0 ? cookie : 0; 2871ea1483SDan Carpenter } 29c13c8260SChris Leech 30c13c8260SChris Leech /** 31c13c8260SChris Leech * enum dma_status - DMA transaction status 32adfedd9aSVinod Koul * @DMA_COMPLETE: transaction completed 33c13c8260SChris Leech * @DMA_IN_PROGRESS: transaction not yet processed 3407934481SLinus Walleij * @DMA_PAUSED: transaction is paused 35c13c8260SChris Leech * @DMA_ERROR: transaction failed 36c13c8260SChris Leech */ 37c13c8260SChris Leech enum dma_status { 387db5f727SVinod Koul DMA_COMPLETE, 39c13c8260SChris Leech DMA_IN_PROGRESS, 4007934481SLinus Walleij DMA_PAUSED, 41c13c8260SChris Leech DMA_ERROR, 4247ec7f09SDave Jiang DMA_OUT_OF_ORDER, 43c13c8260SChris Leech }; 44c13c8260SChris Leech 45c13c8260SChris Leech /** 467405f74bSDan Williams * enum dma_transaction_type - DMA transaction types/indexes 47138f4c35SDan Williams * 48138f4c35SDan Williams * Note: The DMA_ASYNC_TX capability is not to be set by drivers. It is 49138f4c35SDan Williams * automatically set as dma devices are registered. 507405f74bSDan Williams */ 517405f74bSDan Williams enum dma_transaction_type { 527405f74bSDan Williams DMA_MEMCPY, 53*3218910fSAdrian Larumbe DMA_MEMCPY_SG, 547405f74bSDan Williams DMA_XOR, 55b2f46fd8SDan Williams DMA_PQ, 56099f53cbSDan Williams DMA_XOR_VAL, 57099f53cbSDan Williams DMA_PQ_VAL, 584983a501SMaxime Ripard DMA_MEMSET, 5950c7cd2bSMaxime Ripard DMA_MEMSET_SG, 607405f74bSDan Williams DMA_INTERRUPT, 6159b5ec21SDan Williams DMA_PRIVATE, 62138f4c35SDan Williams DMA_ASYNC_TX, 63dc0ee643SHaavard Skinnemoen DMA_SLAVE, 64782bc950SSascha Hauer DMA_CYCLIC, 65b14dab79SJassi Brar DMA_INTERLEAVE, 6647ec7f09SDave Jiang DMA_COMPLETION_NO_ORDER, 679c8ebd8bSLaurent Pinchart DMA_REPEAT, 689c8ebd8bSLaurent Pinchart DMA_LOAD_EOT, 697405f74bSDan Williams /* last transaction type for creation of the capabilities mask */ 70b14dab79SJassi Brar DMA_TX_TYPE_END, 71b14dab79SJassi Brar }; 72dc0ee643SHaavard Skinnemoen 7349920bc6SVinod Koul /** 7449920bc6SVinod Koul * enum dma_transfer_direction - dma transfer mode and direction indicator 7549920bc6SVinod Koul * @DMA_MEM_TO_MEM: Async/Memcpy mode 7649920bc6SVinod Koul * @DMA_MEM_TO_DEV: Slave mode & From Memory to Device 7749920bc6SVinod Koul * @DMA_DEV_TO_MEM: Slave mode & From Device to Memory 7849920bc6SVinod Koul * @DMA_DEV_TO_DEV: Slave mode & From Device to Device 7949920bc6SVinod Koul */ 8049920bc6SVinod Koul enum dma_transfer_direction { 8149920bc6SVinod Koul DMA_MEM_TO_MEM, 8249920bc6SVinod Koul DMA_MEM_TO_DEV, 8349920bc6SVinod Koul DMA_DEV_TO_MEM, 8449920bc6SVinod Koul DMA_DEV_TO_DEV, 8562268ce9SShawn Guo DMA_TRANS_NONE, 8649920bc6SVinod Koul }; 877405f74bSDan Williams 887405f74bSDan Williams /** 89b14dab79SJassi Brar * Interleaved Transfer Request 90b14dab79SJassi Brar * ---------------------------- 9120d60f63SMaciej Grochowski * A chunk is collection of contiguous bytes to be transferred. 92b14dab79SJassi Brar * The gap(in bytes) between two chunks is called inter-chunk-gap(ICG). 93b14dab79SJassi Brar * ICGs may or may not change between chunks. 94b14dab79SJassi Brar * A FRAME is the smallest series of contiguous {chunk,icg} pairs, 95b14dab79SJassi Brar * that when repeated an integral number of times, specifies the transfer. 96b14dab79SJassi Brar * A transfer template is specification of a Frame, the number of times 97b14dab79SJassi Brar * it is to be repeated and other per-transfer attributes. 98b14dab79SJassi Brar * 99b14dab79SJassi Brar * Practically, a client driver would have ready a template for each 100b14dab79SJassi Brar * type of transfer it is going to need during its lifetime and 101b14dab79SJassi Brar * set only 'src_start' and 'dst_start' before submitting the requests. 102b14dab79SJassi Brar * 103b14dab79SJassi Brar * 104b14dab79SJassi Brar * | Frame-1 | Frame-2 | ~ | Frame-'numf' | 105b14dab79SJassi Brar * |====....==.===...=...|====....==.===...=...| ~ |====....==.===...=...| 106b14dab79SJassi Brar * 107b14dab79SJassi Brar * == Chunk size 108b14dab79SJassi Brar * ... ICG 109b14dab79SJassi Brar */ 110b14dab79SJassi Brar 111b14dab79SJassi Brar /** 112b14dab79SJassi Brar * struct data_chunk - Element of scatter-gather list that makes a frame. 113b14dab79SJassi Brar * @size: Number of bytes to read from source. 114b14dab79SJassi Brar * size_dst := fn(op, size_src), so doesn't mean much for destination. 115b14dab79SJassi Brar * @icg: Number of bytes to jump after last src/dst address of this 116b14dab79SJassi Brar * chunk and before first src/dst address for next chunk. 117b14dab79SJassi Brar * Ignored for dst(assumed 0), if dst_inc is true and dst_sgl is false. 118b14dab79SJassi Brar * Ignored for src(assumed 0), if src_inc is true and src_sgl is false. 119e1031dc1SMaxime Ripard * @dst_icg: Number of bytes to jump after last dst address of this 120e1031dc1SMaxime Ripard * chunk and before the first dst address for next chunk. 121e1031dc1SMaxime Ripard * Ignored if dst_inc is true and dst_sgl is false. 122e1031dc1SMaxime Ripard * @src_icg: Number of bytes to jump after last src address of this 123e1031dc1SMaxime Ripard * chunk and before the first src address for next chunk. 124e1031dc1SMaxime Ripard * Ignored if src_inc is true and src_sgl is false. 125b14dab79SJassi Brar */ 126b14dab79SJassi Brar struct data_chunk { 127b14dab79SJassi Brar size_t size; 128b14dab79SJassi Brar size_t icg; 129e1031dc1SMaxime Ripard size_t dst_icg; 130e1031dc1SMaxime Ripard size_t src_icg; 131b14dab79SJassi Brar }; 132b14dab79SJassi Brar 133b14dab79SJassi Brar /** 134b14dab79SJassi Brar * struct dma_interleaved_template - Template to convey DMAC the transfer pattern 135b14dab79SJassi Brar * and attributes. 136b14dab79SJassi Brar * @src_start: Bus address of source for the first chunk. 137b14dab79SJassi Brar * @dst_start: Bus address of destination for the first chunk. 138b14dab79SJassi Brar * @dir: Specifies the type of Source and Destination. 139b14dab79SJassi Brar * @src_inc: If the source address increments after reading from it. 140b14dab79SJassi Brar * @dst_inc: If the destination address increments after writing to it. 141b14dab79SJassi Brar * @src_sgl: If the 'icg' of sgl[] applies to Source (scattered read). 142b14dab79SJassi Brar * Otherwise, source is read contiguously (icg ignored). 143b14dab79SJassi Brar * Ignored if src_inc is false. 144b14dab79SJassi Brar * @dst_sgl: If the 'icg' of sgl[] applies to Destination (scattered write). 145b14dab79SJassi Brar * Otherwise, destination is filled contiguously (icg ignored). 146b14dab79SJassi Brar * Ignored if dst_inc is false. 147b14dab79SJassi Brar * @numf: Number of frames in this template. 148b14dab79SJassi Brar * @frame_size: Number of chunks in a frame i.e, size of sgl[]. 149b14dab79SJassi Brar * @sgl: Array of {chunk,icg} pairs that make up a frame. 150b14dab79SJassi Brar */ 151b14dab79SJassi Brar struct dma_interleaved_template { 152b14dab79SJassi Brar dma_addr_t src_start; 153b14dab79SJassi Brar dma_addr_t dst_start; 154b14dab79SJassi Brar enum dma_transfer_direction dir; 155b14dab79SJassi Brar bool src_inc; 156b14dab79SJassi Brar bool dst_inc; 157b14dab79SJassi Brar bool src_sgl; 158b14dab79SJassi Brar bool dst_sgl; 159b14dab79SJassi Brar size_t numf; 160b14dab79SJassi Brar size_t frame_size; 161466f966bSGustavo A. R. Silva struct data_chunk sgl[]; 162b14dab79SJassi Brar }; 163b14dab79SJassi Brar 164b14dab79SJassi Brar /** 165636bdeaaSDan Williams * enum dma_ctrl_flags - DMA flags to augment operation preparation, 166636bdeaaSDan Williams * control completion, and communicate status. 167d4c56f97SDan Williams * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of 168d4c56f97SDan Williams * this transaction 169a88f6667SGuennadi Liakhovetski * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client 1702be90e91SRandy Dunlap * acknowledges receipt, i.e. has a chance to establish any dependency 171b2f46fd8SDan Williams * chains 172b2f46fd8SDan Williams * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q 173b2f46fd8SDan Williams * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P 174b2f46fd8SDan Williams * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as 175b2f46fd8SDan Williams * sources that were the result of a previous operation, in the case of a PQ 176b2f46fd8SDan Williams * operation it continues the calculation with new sources 1770403e382SDan Williams * @DMA_PREP_FENCE - tell the driver that subsequent operations depend 1780403e382SDan Williams * on the result of this operation 17927242021SVinod Koul * @DMA_CTRL_REUSE: client can reuse the descriptor and submit again till 18027242021SVinod Koul * cleared or freed 1813e00ab4aSAbhishek Sahu * @DMA_PREP_CMD: tell the driver that the data passed to DMA API is command 1823e00ab4aSAbhishek Sahu * data and the descriptor should be in different format from normal 1833e00ab4aSAbhishek Sahu * data descriptors. 1849c8ebd8bSLaurent Pinchart * @DMA_PREP_REPEAT: tell the driver that the transaction shall be automatically 1859c8ebd8bSLaurent Pinchart * repeated when it ends until a transaction is issued on the same channel 1869c8ebd8bSLaurent Pinchart * with the DMA_PREP_LOAD_EOT flag set. This flag is only applicable to 1879c8ebd8bSLaurent Pinchart * interleaved transactions and is ignored for all other transaction types. 1889c8ebd8bSLaurent Pinchart * @DMA_PREP_LOAD_EOT: tell the driver that the transaction shall replace any 1899c8ebd8bSLaurent Pinchart * active repeated (as indicated by DMA_PREP_REPEAT) transaction when the 1909c8ebd8bSLaurent Pinchart * repeated transaction ends. Not setting this flag when the previously queued 1919c8ebd8bSLaurent Pinchart * transaction is marked with DMA_PREP_REPEAT will cause the new transaction 1929c8ebd8bSLaurent Pinchart * to never be processed and stay in the issued queue forever. The flag is 1939c8ebd8bSLaurent Pinchart * ignored if the previous transaction is not a repeated transaction. 194d4c56f97SDan Williams */ 195636bdeaaSDan Williams enum dma_ctrl_flags { 196d4c56f97SDan Williams DMA_PREP_INTERRUPT = (1 << 0), 197636bdeaaSDan Williams DMA_CTRL_ACK = (1 << 1), 1980776ae7bSBartlomiej Zolnierkiewicz DMA_PREP_PQ_DISABLE_P = (1 << 2), 1990776ae7bSBartlomiej Zolnierkiewicz DMA_PREP_PQ_DISABLE_Q = (1 << 3), 2000776ae7bSBartlomiej Zolnierkiewicz DMA_PREP_CONTINUE = (1 << 4), 2010776ae7bSBartlomiej Zolnierkiewicz DMA_PREP_FENCE = (1 << 5), 20227242021SVinod Koul DMA_CTRL_REUSE = (1 << 6), 2033e00ab4aSAbhishek Sahu DMA_PREP_CMD = (1 << 7), 2049c8ebd8bSLaurent Pinchart DMA_PREP_REPEAT = (1 << 8), 2059c8ebd8bSLaurent Pinchart DMA_PREP_LOAD_EOT = (1 << 9), 206d4c56f97SDan Williams }; 207d4c56f97SDan Williams 208d4c56f97SDan Williams /** 209ad283ea4SDan Williams * enum sum_check_bits - bit position of pq_check_flags 210ad283ea4SDan Williams */ 211ad283ea4SDan Williams enum sum_check_bits { 212ad283ea4SDan Williams SUM_CHECK_P = 0, 213ad283ea4SDan Williams SUM_CHECK_Q = 1, 214ad283ea4SDan Williams }; 215ad283ea4SDan Williams 216ad283ea4SDan Williams /** 217ad283ea4SDan Williams * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations 218ad283ea4SDan Williams * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise 219ad283ea4SDan Williams * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise 220ad283ea4SDan Williams */ 221ad283ea4SDan Williams enum sum_check_flags { 222ad283ea4SDan Williams SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P), 223ad283ea4SDan Williams SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q), 224ad283ea4SDan Williams }; 225ad283ea4SDan Williams 226ad283ea4SDan Williams 227ad283ea4SDan Williams /** 2287405f74bSDan Williams * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t. 2297405f74bSDan Williams * See linux/cpumask.h 2307405f74bSDan Williams */ 2317405f74bSDan Williams typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t; 2327405f74bSDan Williams 2337405f74bSDan Williams /** 2344db8fd32SPeter Ujfalusi * enum dma_desc_metadata_mode - per descriptor metadata mode types supported 2354db8fd32SPeter Ujfalusi * @DESC_METADATA_CLIENT - the metadata buffer is allocated/provided by the 2364db8fd32SPeter Ujfalusi * client driver and it is attached (via the dmaengine_desc_attach_metadata() 2374db8fd32SPeter Ujfalusi * helper) to the descriptor. 2384db8fd32SPeter Ujfalusi * 2394db8fd32SPeter Ujfalusi * Client drivers interested to use this mode can follow: 2404db8fd32SPeter Ujfalusi * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM: 2414db8fd32SPeter Ujfalusi * 1. prepare the descriptor (dmaengine_prep_*) 2424db8fd32SPeter Ujfalusi * construct the metadata in the client's buffer 2434db8fd32SPeter Ujfalusi * 2. use dmaengine_desc_attach_metadata() to attach the buffer to the 2444db8fd32SPeter Ujfalusi * descriptor 2454db8fd32SPeter Ujfalusi * 3. submit the transfer 2464db8fd32SPeter Ujfalusi * - DMA_DEV_TO_MEM: 2474db8fd32SPeter Ujfalusi * 1. prepare the descriptor (dmaengine_prep_*) 2484db8fd32SPeter Ujfalusi * 2. use dmaengine_desc_attach_metadata() to attach the buffer to the 2494db8fd32SPeter Ujfalusi * descriptor 2504db8fd32SPeter Ujfalusi * 3. submit the transfer 2514db8fd32SPeter Ujfalusi * 4. when the transfer is completed, the metadata should be available in the 2524db8fd32SPeter Ujfalusi * attached buffer 2534db8fd32SPeter Ujfalusi * 2544db8fd32SPeter Ujfalusi * @DESC_METADATA_ENGINE - the metadata buffer is allocated/managed by the DMA 2554db8fd32SPeter Ujfalusi * driver. The client driver can ask for the pointer, maximum size and the 2564db8fd32SPeter Ujfalusi * currently used size of the metadata and can directly update or read it. 2574db8fd32SPeter Ujfalusi * dmaengine_desc_get_metadata_ptr() and dmaengine_desc_set_metadata_len() is 2584db8fd32SPeter Ujfalusi * provided as helper functions. 2594db8fd32SPeter Ujfalusi * 2604db8fd32SPeter Ujfalusi * Note: the metadata area for the descriptor is no longer valid after the 2614db8fd32SPeter Ujfalusi * transfer has been completed (valid up to the point when the completion 2624db8fd32SPeter Ujfalusi * callback returns if used). 2634db8fd32SPeter Ujfalusi * 2644db8fd32SPeter Ujfalusi * Client drivers interested to use this mode can follow: 2654db8fd32SPeter Ujfalusi * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM: 2664db8fd32SPeter Ujfalusi * 1. prepare the descriptor (dmaengine_prep_*) 2674db8fd32SPeter Ujfalusi * 2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the engine's 2684db8fd32SPeter Ujfalusi * metadata area 2694db8fd32SPeter Ujfalusi * 3. update the metadata at the pointer 2704db8fd32SPeter Ujfalusi * 4. use dmaengine_desc_set_metadata_len() to tell the DMA engine the amount 2714db8fd32SPeter Ujfalusi * of data the client has placed into the metadata buffer 2724db8fd32SPeter Ujfalusi * 5. submit the transfer 2734db8fd32SPeter Ujfalusi * - DMA_DEV_TO_MEM: 2744db8fd32SPeter Ujfalusi * 1. prepare the descriptor (dmaengine_prep_*) 2754db8fd32SPeter Ujfalusi * 2. submit the transfer 2764db8fd32SPeter Ujfalusi * 3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get the 2774db8fd32SPeter Ujfalusi * pointer to the engine's metadata area 2784db8fd32SPeter Ujfalusi * 4. Read out the metadata from the pointer 2794db8fd32SPeter Ujfalusi * 2804db8fd32SPeter Ujfalusi * Note: the two mode is not compatible and clients must use one mode for a 2814db8fd32SPeter Ujfalusi * descriptor. 2824db8fd32SPeter Ujfalusi */ 2834db8fd32SPeter Ujfalusi enum dma_desc_metadata_mode { 2844db8fd32SPeter Ujfalusi DESC_METADATA_NONE = 0, 2854db8fd32SPeter Ujfalusi DESC_METADATA_CLIENT = BIT(0), 2864db8fd32SPeter Ujfalusi DESC_METADATA_ENGINE = BIT(1), 2874db8fd32SPeter Ujfalusi }; 2884db8fd32SPeter Ujfalusi 289acfbb191SAndy Shevchenko /** 290acfbb191SAndy Shevchenko * struct dma_chan_percpu - the per-CPU part of struct dma_chan 291acfbb191SAndy Shevchenko * @memcpy_count: transaction counter 292acfbb191SAndy Shevchenko * @bytes_transferred: byte counter 293acfbb191SAndy Shevchenko */ 294c13c8260SChris Leech struct dma_chan_percpu { 295c13c8260SChris Leech /* stats */ 296c13c8260SChris Leech unsigned long memcpy_count; 297c13c8260SChris Leech unsigned long bytes_transferred; 298c13c8260SChris Leech }; 299c13c8260SChris Leech 300c13c8260SChris Leech /** 30156f13c0dSPeter Ujfalusi * struct dma_router - DMA router structure 30256f13c0dSPeter Ujfalusi * @dev: pointer to the DMA router device 30356f13c0dSPeter Ujfalusi * @route_free: function to be called when the route can be disconnected 30456f13c0dSPeter Ujfalusi */ 30556f13c0dSPeter Ujfalusi struct dma_router { 30656f13c0dSPeter Ujfalusi struct device *dev; 30756f13c0dSPeter Ujfalusi void (*route_free)(struct device *dev, void *route_data); 30856f13c0dSPeter Ujfalusi }; 30956f13c0dSPeter Ujfalusi 31056f13c0dSPeter Ujfalusi /** 311c13c8260SChris Leech * struct dma_chan - devices supply DMA channels, clients use them 312fe4ada2dSRandy Dunlap * @device: ptr to the dma device who supplies this channel, always !%NULL 31371723a96SGeert Uytterhoeven * @slave: ptr to the device using this channel 314c13c8260SChris Leech * @cookie: last cookie value returned to client 3154d4e58deSRussell King - ARM Linux * @completed_cookie: last completed cookie for this channel 316fe4ada2dSRandy Dunlap * @chan_id: channel ID for sysfs 31741d5e59cSDan Williams * @dev: class device for sysfs 31871723a96SGeert Uytterhoeven * @name: backlink name for sysfs 319e937cc1dSPeter Ujfalusi * @dbg_client_name: slave name for debugfs in format: 320e937cc1dSPeter Ujfalusi * dev_name(requester's dev):channel name, for example: "2b00000.mcasp:tx" 321c13c8260SChris Leech * @device_node: used to add this to the device chan list 322c13c8260SChris Leech * @local: per-cpu pointer to a struct dma_chan_percpu 323868d2ee2SVinod Koul * @client_count: how many clients are using this channel 324bec08513SDan Williams * @table_count: number of appearances in the mem-to-mem allocation table 32556f13c0dSPeter Ujfalusi * @router: pointer to the DMA router structure 32656f13c0dSPeter Ujfalusi * @route_data: channel specific data for the router 327287d8592SDan Williams * @private: private data for certain client-channel associations 328c13c8260SChris Leech */ 329c13c8260SChris Leech struct dma_chan { 330c13c8260SChris Leech struct dma_device *device; 33171723a96SGeert Uytterhoeven struct device *slave; 332c13c8260SChris Leech dma_cookie_t cookie; 3334d4e58deSRussell King - ARM Linux dma_cookie_t completed_cookie; 334c13c8260SChris Leech 335c13c8260SChris Leech /* sysfs */ 336c13c8260SChris Leech int chan_id; 33741d5e59cSDan Williams struct dma_chan_dev *dev; 33871723a96SGeert Uytterhoeven const char *name; 339e937cc1dSPeter Ujfalusi #ifdef CONFIG_DEBUG_FS 340e937cc1dSPeter Ujfalusi char *dbg_client_name; 341e937cc1dSPeter Ujfalusi #endif 342c13c8260SChris Leech 343c13c8260SChris Leech struct list_head device_node; 344a29d8b8eSTejun Heo struct dma_chan_percpu __percpu *local; 3457cc5bf9aSDan Williams int client_count; 346bec08513SDan Williams int table_count; 34756f13c0dSPeter Ujfalusi 34856f13c0dSPeter Ujfalusi /* DMA router */ 34956f13c0dSPeter Ujfalusi struct dma_router *router; 35056f13c0dSPeter Ujfalusi void *route_data; 35156f13c0dSPeter Ujfalusi 352287d8592SDan Williams void *private; 353c13c8260SChris Leech }; 354c13c8260SChris Leech 35541d5e59cSDan Williams /** 35641d5e59cSDan Williams * struct dma_chan_dev - relate sysfs device node to backing channel device 357868d2ee2SVinod Koul * @chan: driver channel device 358868d2ee2SVinod Koul * @device: sysfs device 359868d2ee2SVinod Koul * @dev_id: parent dma_device dev_id 360ab650ef6SPeter Ujfalusi * @chan_dma_dev: The channel is using custom/different dma-mapping 361ab650ef6SPeter Ujfalusi * compared to the parent dma_device 36241d5e59cSDan Williams */ 36341d5e59cSDan Williams struct dma_chan_dev { 36441d5e59cSDan Williams struct dma_chan *chan; 36541d5e59cSDan Williams struct device device; 366864498aaSDan Williams int dev_id; 367ab650ef6SPeter Ujfalusi bool chan_dma_dev; 36841d5e59cSDan Williams }; 36941d5e59cSDan Williams 370c156d0a5SLinus Walleij /** 371ba730340SAlexander Popov * enum dma_slave_buswidth - defines bus width of the DMA slave 372c156d0a5SLinus Walleij * device, source or target buses 373c156d0a5SLinus Walleij */ 374c156d0a5SLinus Walleij enum dma_slave_buswidth { 375c156d0a5SLinus Walleij DMA_SLAVE_BUSWIDTH_UNDEFINED = 0, 376c156d0a5SLinus Walleij DMA_SLAVE_BUSWIDTH_1_BYTE = 1, 377c156d0a5SLinus Walleij DMA_SLAVE_BUSWIDTH_2_BYTES = 2, 37893c6ee94SPeter Ujfalusi DMA_SLAVE_BUSWIDTH_3_BYTES = 3, 379c156d0a5SLinus Walleij DMA_SLAVE_BUSWIDTH_4_BYTES = 4, 380c156d0a5SLinus Walleij DMA_SLAVE_BUSWIDTH_8_BYTES = 8, 381534a7298SLaurent Pinchart DMA_SLAVE_BUSWIDTH_16_BYTES = 16, 382534a7298SLaurent Pinchart DMA_SLAVE_BUSWIDTH_32_BYTES = 32, 383534a7298SLaurent Pinchart DMA_SLAVE_BUSWIDTH_64_BYTES = 64, 384ab959c7dSBiju Das DMA_SLAVE_BUSWIDTH_128_BYTES = 128, 385c156d0a5SLinus Walleij }; 386c156d0a5SLinus Walleij 387c156d0a5SLinus Walleij /** 388c156d0a5SLinus Walleij * struct dma_slave_config - dma slave channel runtime config 389c156d0a5SLinus Walleij * @direction: whether the data shall go in or out on this slave 390397321f4SAlexander Popov * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are 391d9ff958bSLaurent Pinchart * legal values. DEPRECATED, drivers should use the direction argument 392d9ff958bSLaurent Pinchart * to the device_prep_slave_sg and device_prep_dma_cyclic functions or 393d9ff958bSLaurent Pinchart * the dir field in the dma_interleaved_template structure. 394c156d0a5SLinus Walleij * @src_addr: this is the physical address where DMA slave data 395c156d0a5SLinus Walleij * should be read (RX), if the source is memory this argument is 396c156d0a5SLinus Walleij * ignored. 397c156d0a5SLinus Walleij * @dst_addr: this is the physical address where DMA slave data 398c156d0a5SLinus Walleij * should be written (TX), if the source is memory this argument 399c156d0a5SLinus Walleij * is ignored. 400c156d0a5SLinus Walleij * @src_addr_width: this is the width in bytes of the source (RX) 401c156d0a5SLinus Walleij * register where DMA data shall be read. If the source 402c156d0a5SLinus Walleij * is memory this may be ignored depending on architecture. 403ab959c7dSBiju Das * Legal values: 1, 2, 3, 4, 8, 16, 32, 64, 128. 404c156d0a5SLinus Walleij * @dst_addr_width: same as src_addr_width but for destination 405c156d0a5SLinus Walleij * target (TX) mutatis mutandis. 406c156d0a5SLinus Walleij * @src_maxburst: the maximum number of words (note: words, as in 407c156d0a5SLinus Walleij * units of the src_addr_width member, not bytes) that can be sent 408c156d0a5SLinus Walleij * in one burst to the device. Typically something like half the 409c156d0a5SLinus Walleij * FIFO depth on I/O peripherals so you don't overflow it. This 410c156d0a5SLinus Walleij * may or may not be applicable on memory sources. 411c156d0a5SLinus Walleij * @dst_maxburst: same as src_maxburst but for destination target 412c156d0a5SLinus Walleij * mutatis mutandis. 41354cd2558SPeter Ujfalusi * @src_port_window_size: The length of the register area in words the data need 41454cd2558SPeter Ujfalusi * to be accessed on the device side. It is only used for devices which is using 41554cd2558SPeter Ujfalusi * an area instead of a single register to receive the data. Typically the DMA 41654cd2558SPeter Ujfalusi * loops in this area in order to transfer the data. 41754cd2558SPeter Ujfalusi * @dst_port_window_size: same as src_port_window_size but for the destination 41854cd2558SPeter Ujfalusi * port. 419dcc043dcSViresh Kumar * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill 420dcc043dcSViresh Kumar * with 'true' if peripheral should be flow controller. Direction will be 421dcc043dcSViresh Kumar * selected at Runtime. 4224fd1e324SLaxman Dewangan * @slave_id: Slave requester id. Only valid for slave channels. The dma 4234fd1e324SLaxman Dewangan * slave peripheral will have unique id as dma requester which need to be 4244fd1e324SLaxman Dewangan * pass as slave config. 425e7bbb7acSVinod Koul * @peripheral_config: peripheral configuration for programming peripheral 426e7bbb7acSVinod Koul * for dmaengine transfer 427e7bbb7acSVinod Koul * @peripheral_size: peripheral configuration buffer size 428c156d0a5SLinus Walleij * 429c156d0a5SLinus Walleij * This struct is passed in as configuration data to a DMA engine 430c156d0a5SLinus Walleij * in order to set up a certain channel for DMA transport at runtime. 431c156d0a5SLinus Walleij * The DMA device/engine has to provide support for an additional 4322c44ad91SMaxime Ripard * callback in the dma_device structure, device_config and this struct 4332c44ad91SMaxime Ripard * will then be passed in as an argument to the function. 434c156d0a5SLinus Walleij * 4357cbccb55SLars-Peter Clausen * The rationale for adding configuration information to this struct is as 4367cbccb55SLars-Peter Clausen * follows: if it is likely that more than one DMA slave controllers in 4377cbccb55SLars-Peter Clausen * the world will support the configuration option, then make it generic. 4387cbccb55SLars-Peter Clausen * If not: if it is fixed so that it be sent in static from the platform 4397cbccb55SLars-Peter Clausen * data, then prefer to do that. 440c156d0a5SLinus Walleij */ 441c156d0a5SLinus Walleij struct dma_slave_config { 44249920bc6SVinod Koul enum dma_transfer_direction direction; 44395756320SVinod Koul phys_addr_t src_addr; 44495756320SVinod Koul phys_addr_t dst_addr; 445c156d0a5SLinus Walleij enum dma_slave_buswidth src_addr_width; 446c156d0a5SLinus Walleij enum dma_slave_buswidth dst_addr_width; 447c156d0a5SLinus Walleij u32 src_maxburst; 448c156d0a5SLinus Walleij u32 dst_maxburst; 44954cd2558SPeter Ujfalusi u32 src_port_window_size; 45054cd2558SPeter Ujfalusi u32 dst_port_window_size; 451dcc043dcSViresh Kumar bool device_fc; 4524fd1e324SLaxman Dewangan unsigned int slave_id; 453e7bbb7acSVinod Koul void *peripheral_config; 454e7bbb7acSVinod Koul size_t peripheral_size; 455c156d0a5SLinus Walleij }; 456c156d0a5SLinus Walleij 45750720563SLars-Peter Clausen /** 45850720563SLars-Peter Clausen * enum dma_residue_granularity - Granularity of the reported transfer residue 45950720563SLars-Peter Clausen * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The 46050720563SLars-Peter Clausen * DMA channel is only able to tell whether a descriptor has been completed or 46150720563SLars-Peter Clausen * not, which means residue reporting is not supported by this channel. The 46250720563SLars-Peter Clausen * residue field of the dma_tx_state field will always be 0. 46350720563SLars-Peter Clausen * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully 46450720563SLars-Peter Clausen * completed segment of the transfer (For cyclic transfers this is after each 46550720563SLars-Peter Clausen * period). This is typically implemented by having the hardware generate an 46650720563SLars-Peter Clausen * interrupt after each transferred segment and then the drivers updates the 46750720563SLars-Peter Clausen * outstanding residue by the size of the segment. Another possibility is if 46850720563SLars-Peter Clausen * the hardware supports scatter-gather and the segment descriptor has a field 46950720563SLars-Peter Clausen * which gets set after the segment has been completed. The driver then counts 47050720563SLars-Peter Clausen * the number of segments without the flag set to compute the residue. 47150720563SLars-Peter Clausen * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred 47250720563SLars-Peter Clausen * burst. This is typically only supported if the hardware has a progress 47350720563SLars-Peter Clausen * register of some sort (E.g. a register with the current read/write address 47450720563SLars-Peter Clausen * or a register with the amount of bursts/beats/bytes that have been 47550720563SLars-Peter Clausen * transferred or still need to be transferred). 47650720563SLars-Peter Clausen */ 47750720563SLars-Peter Clausen enum dma_residue_granularity { 47850720563SLars-Peter Clausen DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0, 47950720563SLars-Peter Clausen DMA_RESIDUE_GRANULARITY_SEGMENT = 1, 48050720563SLars-Peter Clausen DMA_RESIDUE_GRANULARITY_BURST = 2, 48150720563SLars-Peter Clausen }; 48250720563SLars-Peter Clausen 483c2cbd427SStefan Brüns /** 484c2cbd427SStefan Brüns * struct dma_slave_caps - expose capabilities of a slave channel only 485c2cbd427SStefan Brüns * @src_addr_widths: bit mask of src addr widths the channel supports. 486c2cbd427SStefan Brüns * Width is specified in bytes, e.g. for a channel supporting 487c2cbd427SStefan Brüns * a width of 4 the mask should have BIT(4) set. 488c2cbd427SStefan Brüns * @dst_addr_widths: bit mask of dst addr widths the channel supports 489c2cbd427SStefan Brüns * @directions: bit mask of slave directions the channel supports. 490c2cbd427SStefan Brüns * Since the enum dma_transfer_direction is not defined as bit flag for 491c2cbd427SStefan Brüns * each type, the dma controller should set BIT(<TYPE>) and same 492221a27c7SVinod Koul * should be checked by controller as well 493d97758e0SSerge Semin * @min_burst: min burst capability per-transfer 4946d5bbed3SShawn Lin * @max_burst: max burst capability per-transfer 495b1b40b8fSSerge Semin * @max_sg_burst: max number of SG list entries executed in a single burst 496b1b40b8fSSerge Semin * DMA tansaction with no software intervention for reinitialization. 497b1b40b8fSSerge Semin * Zero value means unlimited number of entries. 498d8095f94SMarek Szyprowski * @cmd_pause: true, if pause is supported (i.e. for reading residue or 499d8095f94SMarek Szyprowski * for resume later) 500d8095f94SMarek Szyprowski * @cmd_resume: true, if resume is supported 501221a27c7SVinod Koul * @cmd_terminate: true, if terminate cmd is supported 50250720563SLars-Peter Clausen * @residue_granularity: granularity of the reported transfer residue 50327242021SVinod Koul * @descriptor_reuse: if a descriptor can be reused by client and 50427242021SVinod Koul * resubmitted multiple times 505221a27c7SVinod Koul */ 506221a27c7SVinod Koul struct dma_slave_caps { 507221a27c7SVinod Koul u32 src_addr_widths; 508ceacbdbfSMaxime Ripard u32 dst_addr_widths; 509221a27c7SVinod Koul u32 directions; 510d97758e0SSerge Semin u32 min_burst; 5116d5bbed3SShawn Lin u32 max_burst; 512b1b40b8fSSerge Semin u32 max_sg_burst; 513221a27c7SVinod Koul bool cmd_pause; 514d8095f94SMarek Szyprowski bool cmd_resume; 515221a27c7SVinod Koul bool cmd_terminate; 51650720563SLars-Peter Clausen enum dma_residue_granularity residue_granularity; 51727242021SVinod Koul bool descriptor_reuse; 518221a27c7SVinod Koul }; 519221a27c7SVinod Koul 52041d5e59cSDan Williams static inline const char *dma_chan_name(struct dma_chan *chan) 52141d5e59cSDan Williams { 52241d5e59cSDan Williams return dev_name(&chan->dev->device); 52341d5e59cSDan Williams } 524d379b01eSDan Williams 525c13c8260SChris Leech void dma_chan_cleanup(struct kref *kref); 526c13c8260SChris Leech 527c13c8260SChris Leech /** 52859b5ec21SDan Williams * typedef dma_filter_fn - callback filter for dma_request_channel 52959b5ec21SDan Williams * @chan: channel to be reviewed 53059b5ec21SDan Williams * @filter_param: opaque parameter passed through dma_request_channel 53159b5ec21SDan Williams * 53259b5ec21SDan Williams * When this optional parameter is specified in a call to dma_request_channel a 53359b5ec21SDan Williams * suitable channel is passed to this routine for further dispositioning before 53459b5ec21SDan Williams * being returned. Where 'suitable' indicates a non-busy channel that 5357dd60251SDan Williams * satisfies the given capability mask. It returns 'true' to indicate that the 5367dd60251SDan Williams * channel is suitable. 53759b5ec21SDan Williams */ 5387dd60251SDan Williams typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param); 53959b5ec21SDan Williams 5407405f74bSDan Williams typedef void (*dma_async_tx_callback)(void *dma_async_param); 541d38a8c62SDan Williams 542f067025bSDave Jiang enum dmaengine_tx_result { 543f067025bSDave Jiang DMA_TRANS_NOERROR = 0, /* SUCCESS */ 544f067025bSDave Jiang DMA_TRANS_READ_FAILED, /* Source DMA read failed */ 545f067025bSDave Jiang DMA_TRANS_WRITE_FAILED, /* Destination DMA write failed */ 546f067025bSDave Jiang DMA_TRANS_ABORTED, /* Op never submitted / aborted */ 547f067025bSDave Jiang }; 548f067025bSDave Jiang 549f067025bSDave Jiang struct dmaengine_result { 550f067025bSDave Jiang enum dmaengine_tx_result result; 551f067025bSDave Jiang u32 residue; 552f067025bSDave Jiang }; 553f067025bSDave Jiang 554f067025bSDave Jiang typedef void (*dma_async_tx_callback_result)(void *dma_async_param, 555f067025bSDave Jiang const struct dmaengine_result *result); 556f067025bSDave Jiang 557d38a8c62SDan Williams struct dmaengine_unmap_data { 5580c0eb4caSZi Yan #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID) 5590c0eb4caSZi Yan u16 map_cnt; 5600c0eb4caSZi Yan #else 561c1f43dd9SXuelin Shi u8 map_cnt; 5620c0eb4caSZi Yan #endif 563d38a8c62SDan Williams u8 to_cnt; 564d38a8c62SDan Williams u8 from_cnt; 565d38a8c62SDan Williams u8 bidi_cnt; 566d38a8c62SDan Williams struct device *dev; 567d38a8c62SDan Williams struct kref kref; 568d38a8c62SDan Williams size_t len; 569466f966bSGustavo A. R. Silva dma_addr_t addr[]; 570d38a8c62SDan Williams }; 571d38a8c62SDan Williams 5724db8fd32SPeter Ujfalusi struct dma_async_tx_descriptor; 5734db8fd32SPeter Ujfalusi 5744db8fd32SPeter Ujfalusi struct dma_descriptor_metadata_ops { 5754db8fd32SPeter Ujfalusi int (*attach)(struct dma_async_tx_descriptor *desc, void *data, 5764db8fd32SPeter Ujfalusi size_t len); 5774db8fd32SPeter Ujfalusi 5784db8fd32SPeter Ujfalusi void *(*get_ptr)(struct dma_async_tx_descriptor *desc, 5794db8fd32SPeter Ujfalusi size_t *payload_len, size_t *max_len); 5804db8fd32SPeter Ujfalusi int (*set_len)(struct dma_async_tx_descriptor *desc, 5814db8fd32SPeter Ujfalusi size_t payload_len); 5824db8fd32SPeter Ujfalusi }; 5834db8fd32SPeter Ujfalusi 5847405f74bSDan Williams /** 5857405f74bSDan Williams * struct dma_async_tx_descriptor - async transaction descriptor 5867405f74bSDan Williams * ---dma generic offload fields--- 5877405f74bSDan Williams * @cookie: tracking cookie for this transaction, set to -EBUSY if 5887405f74bSDan Williams * this tx is sitting on a dependency list 589636bdeaaSDan Williams * @flags: flags to augment operation preparation, control completion, and 590636bdeaaSDan Williams * communicate status 5917405f74bSDan Williams * @phys: physical address of the descriptor 5927405f74bSDan Williams * @chan: target channel for this operation 593aba96badSVinod Koul * @tx_submit: accept the descriptor, assign ordered cookie and mark the 594aba96badSVinod Koul * descriptor pending. To be pushed on .issue_pending() call 5957405f74bSDan Williams * @callback: routine to call after this operation is complete 5967405f74bSDan Williams * @callback_param: general parameter to pass to the callback routine 5974db8fd32SPeter Ujfalusi * @desc_metadata_mode: core managed metadata mode to protect mixed use of 5984db8fd32SPeter Ujfalusi * DESC_METADATA_CLIENT or DESC_METADATA_ENGINE. Otherwise 5994db8fd32SPeter Ujfalusi * DESC_METADATA_NONE 6004db8fd32SPeter Ujfalusi * @metadata_ops: DMA driver provided metadata mode ops, need to be set by the 6014db8fd32SPeter Ujfalusi * DMA driver if metadata mode is supported with the descriptor 6027405f74bSDan Williams * ---async_tx api specific fields--- 60319242d72SDan Williams * @next: at completion submit this descriptor 6047405f74bSDan Williams * @parent: pointer to the next level up in the dependency chain 60519242d72SDan Williams * @lock: protect the parent and next pointers 6067405f74bSDan Williams */ 6077405f74bSDan Williams struct dma_async_tx_descriptor { 6087405f74bSDan Williams dma_cookie_t cookie; 609636bdeaaSDan Williams enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */ 6107405f74bSDan Williams dma_addr_t phys; 6117405f74bSDan Williams struct dma_chan *chan; 6127405f74bSDan Williams dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx); 61327242021SVinod Koul int (*desc_free)(struct dma_async_tx_descriptor *tx); 6147405f74bSDan Williams dma_async_tx_callback callback; 615f067025bSDave Jiang dma_async_tx_callback_result callback_result; 6167405f74bSDan Williams void *callback_param; 617d38a8c62SDan Williams struct dmaengine_unmap_data *unmap; 6184db8fd32SPeter Ujfalusi enum dma_desc_metadata_mode desc_metadata_mode; 6194db8fd32SPeter Ujfalusi struct dma_descriptor_metadata_ops *metadata_ops; 6205fc6d897SDan Williams #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH 62119242d72SDan Williams struct dma_async_tx_descriptor *next; 6227405f74bSDan Williams struct dma_async_tx_descriptor *parent; 6237405f74bSDan Williams spinlock_t lock; 624caa20d97SDan Williams #endif 6257405f74bSDan Williams }; 6267405f74bSDan Williams 62789716462SDan Williams #ifdef CONFIG_DMA_ENGINE 628d38a8c62SDan Williams static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx, 629d38a8c62SDan Williams struct dmaengine_unmap_data *unmap) 630d38a8c62SDan Williams { 631d38a8c62SDan Williams kref_get(&unmap->kref); 632d38a8c62SDan Williams tx->unmap = unmap; 633d38a8c62SDan Williams } 634d38a8c62SDan Williams 63589716462SDan Williams struct dmaengine_unmap_data * 63689716462SDan Williams dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags); 63745c463aeSDan Williams void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap); 63889716462SDan Williams #else 63989716462SDan Williams static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx, 64089716462SDan Williams struct dmaengine_unmap_data *unmap) 64189716462SDan Williams { 64289716462SDan Williams } 64389716462SDan Williams static inline struct dmaengine_unmap_data * 64489716462SDan Williams dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags) 64589716462SDan Williams { 64689716462SDan Williams return NULL; 64789716462SDan Williams } 64889716462SDan Williams static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap) 64989716462SDan Williams { 65089716462SDan Williams } 65189716462SDan Williams #endif 65245c463aeSDan Williams 653d38a8c62SDan Williams static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx) 654d38a8c62SDan Williams { 6553a92063bSAndy Shevchenko if (!tx->unmap) 6563a92063bSAndy Shevchenko return; 6573a92063bSAndy Shevchenko 65845c463aeSDan Williams dmaengine_unmap_put(tx->unmap); 659d38a8c62SDan Williams tx->unmap = NULL; 660d38a8c62SDan Williams } 661d38a8c62SDan Williams 6625fc6d897SDan Williams #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH 663caa20d97SDan Williams static inline void txd_lock(struct dma_async_tx_descriptor *txd) 664caa20d97SDan Williams { 665caa20d97SDan Williams } 666caa20d97SDan Williams static inline void txd_unlock(struct dma_async_tx_descriptor *txd) 667caa20d97SDan Williams { 668caa20d97SDan Williams } 669caa20d97SDan Williams static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next) 670caa20d97SDan Williams { 671caa20d97SDan Williams BUG(); 672caa20d97SDan Williams } 673caa20d97SDan Williams static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd) 674caa20d97SDan Williams { 675caa20d97SDan Williams } 676caa20d97SDan Williams static inline void txd_clear_next(struct dma_async_tx_descriptor *txd) 677caa20d97SDan Williams { 678caa20d97SDan Williams } 679caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd) 680caa20d97SDan Williams { 681caa20d97SDan Williams return NULL; 682caa20d97SDan Williams } 683caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd) 684caa20d97SDan Williams { 685caa20d97SDan Williams return NULL; 686caa20d97SDan Williams } 687caa20d97SDan Williams 688caa20d97SDan Williams #else 689caa20d97SDan Williams static inline void txd_lock(struct dma_async_tx_descriptor *txd) 690caa20d97SDan Williams { 691caa20d97SDan Williams spin_lock_bh(&txd->lock); 692caa20d97SDan Williams } 693caa20d97SDan Williams static inline void txd_unlock(struct dma_async_tx_descriptor *txd) 694caa20d97SDan Williams { 695caa20d97SDan Williams spin_unlock_bh(&txd->lock); 696caa20d97SDan Williams } 697caa20d97SDan Williams static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next) 698caa20d97SDan Williams { 699caa20d97SDan Williams txd->next = next; 700caa20d97SDan Williams next->parent = txd; 701caa20d97SDan Williams } 702caa20d97SDan Williams static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd) 703caa20d97SDan Williams { 704caa20d97SDan Williams txd->parent = NULL; 705caa20d97SDan Williams } 706caa20d97SDan Williams static inline void txd_clear_next(struct dma_async_tx_descriptor *txd) 707caa20d97SDan Williams { 708caa20d97SDan Williams txd->next = NULL; 709caa20d97SDan Williams } 710caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd) 711caa20d97SDan Williams { 712caa20d97SDan Williams return txd->parent; 713caa20d97SDan Williams } 714caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd) 715caa20d97SDan Williams { 716caa20d97SDan Williams return txd->next; 717caa20d97SDan Williams } 718caa20d97SDan Williams #endif 719caa20d97SDan Williams 720c13c8260SChris Leech /** 72107934481SLinus Walleij * struct dma_tx_state - filled in to report the status of 72207934481SLinus Walleij * a transfer. 72307934481SLinus Walleij * @last: last completed DMA cookie 72407934481SLinus Walleij * @used: last issued DMA cookie (i.e. the one in progress) 72507934481SLinus Walleij * @residue: the remaining number of bytes left to transmit 72607934481SLinus Walleij * on the selected transfer for states DMA_IN_PROGRESS and 72707934481SLinus Walleij * DMA_PAUSED if this is implemented in the driver, else 0 7286755ec06SPeter Ujfalusi * @in_flight_bytes: amount of data in bytes cached by the DMA. 72907934481SLinus Walleij */ 73007934481SLinus Walleij struct dma_tx_state { 73107934481SLinus Walleij dma_cookie_t last; 73207934481SLinus Walleij dma_cookie_t used; 73307934481SLinus Walleij u32 residue; 7346755ec06SPeter Ujfalusi u32 in_flight_bytes; 73507934481SLinus Walleij }; 73607934481SLinus Walleij 73707934481SLinus Walleij /** 73877a68e56SMaxime Ripard * enum dmaengine_alignment - defines alignment of the DMA async tx 73977a68e56SMaxime Ripard * buffers 74077a68e56SMaxime Ripard */ 74177a68e56SMaxime Ripard enum dmaengine_alignment { 74277a68e56SMaxime Ripard DMAENGINE_ALIGN_1_BYTE = 0, 74377a68e56SMaxime Ripard DMAENGINE_ALIGN_2_BYTES = 1, 74477a68e56SMaxime Ripard DMAENGINE_ALIGN_4_BYTES = 2, 74577a68e56SMaxime Ripard DMAENGINE_ALIGN_8_BYTES = 3, 74677a68e56SMaxime Ripard DMAENGINE_ALIGN_16_BYTES = 4, 74777a68e56SMaxime Ripard DMAENGINE_ALIGN_32_BYTES = 5, 74877a68e56SMaxime Ripard DMAENGINE_ALIGN_64_BYTES = 6, 749660343d0SPeter Ujfalusi DMAENGINE_ALIGN_128_BYTES = 7, 750660343d0SPeter Ujfalusi DMAENGINE_ALIGN_256_BYTES = 8, 75177a68e56SMaxime Ripard }; 75277a68e56SMaxime Ripard 75377a68e56SMaxime Ripard /** 754a8135d0dSPeter Ujfalusi * struct dma_slave_map - associates slave device and it's slave channel with 755a8135d0dSPeter Ujfalusi * parameter to be used by a filter function 756a8135d0dSPeter Ujfalusi * @devname: name of the device 757a8135d0dSPeter Ujfalusi * @slave: slave channel name 758a8135d0dSPeter Ujfalusi * @param: opaque parameter to pass to struct dma_filter.fn 759a8135d0dSPeter Ujfalusi */ 760a8135d0dSPeter Ujfalusi struct dma_slave_map { 761a8135d0dSPeter Ujfalusi const char *devname; 762a8135d0dSPeter Ujfalusi const char *slave; 763a8135d0dSPeter Ujfalusi void *param; 764a8135d0dSPeter Ujfalusi }; 765a8135d0dSPeter Ujfalusi 766a8135d0dSPeter Ujfalusi /** 767a8135d0dSPeter Ujfalusi * struct dma_filter - information for slave device/channel to filter_fn/param 768a8135d0dSPeter Ujfalusi * mapping 769a8135d0dSPeter Ujfalusi * @fn: filter function callback 770a8135d0dSPeter Ujfalusi * @mapcnt: number of slave device/channel in the map 771a8135d0dSPeter Ujfalusi * @map: array of channel to filter mapping data 772a8135d0dSPeter Ujfalusi */ 773a8135d0dSPeter Ujfalusi struct dma_filter { 774a8135d0dSPeter Ujfalusi dma_filter_fn fn; 775a8135d0dSPeter Ujfalusi int mapcnt; 776a8135d0dSPeter Ujfalusi const struct dma_slave_map *map; 777a8135d0dSPeter Ujfalusi }; 778a8135d0dSPeter Ujfalusi 779a8135d0dSPeter Ujfalusi /** 780c13c8260SChris Leech * struct dma_device - info on the entity supplying DMA services 781c13c8260SChris Leech * @chancnt: how many DMA channels are supported 7820f571515SAtsushi Nemoto * @privatecnt: how many DMA channels are requested by dma_request_channel 783c13c8260SChris Leech * @channels: the list of struct dma_chan 784c13c8260SChris Leech * @global_node: list_head for global dma_device_list 785a8135d0dSPeter Ujfalusi * @filter: information for device/slave to filter function/param mapping 7867405f74bSDan Williams * @cap_mask: one or more dma_capability flags 7874db8fd32SPeter Ujfalusi * @desc_metadata_modes: supported metadata modes by the DMA device 7887405f74bSDan Williams * @max_xor: maximum number of xor sources, 0 if no capability 789b2f46fd8SDan Williams * @max_pq: maximum number of PQ sources and PQ-continue capability 79083544ae9SDan Williams * @copy_align: alignment shift for memcpy operations 79183544ae9SDan Williams * @xor_align: alignment shift for xor operations 79283544ae9SDan Williams * @pq_align: alignment shift for pq operations 7934983a501SMaxime Ripard * @fill_align: alignment shift for memset operations 794fe4ada2dSRandy Dunlap * @dev_id: unique device ID 7957405f74bSDan Williams * @dev: struct device reference for dma mapping api 796dae7a589SLogan Gunthorpe * @owner: owner module (automatically set based on the provided dev) 797cb8cea51SMaxime Ripard * @src_addr_widths: bit mask of src addr widths the device supports 798c2cbd427SStefan Brüns * Width is specified in bytes, e.g. for a device supporting 799c2cbd427SStefan Brüns * a width of 4 the mask should have BIT(4) set. 800cb8cea51SMaxime Ripard * @dst_addr_widths: bit mask of dst addr widths the device supports 801c2cbd427SStefan Brüns * @directions: bit mask of slave directions the device supports. 802c2cbd427SStefan Brüns * Since the enum dma_transfer_direction is not defined as bit flag for 803c2cbd427SStefan Brüns * each type, the dma controller should set BIT(<TYPE>) and same 804c2cbd427SStefan Brüns * should be checked by controller as well 805d97758e0SSerge Semin * @min_burst: min burst capability per-transfer 8066d5bbed3SShawn Lin * @max_burst: max burst capability per-transfer 807b1b40b8fSSerge Semin * @max_sg_burst: max number of SG list entries executed in a single burst 808b1b40b8fSSerge Semin * DMA tansaction with no software intervention for reinitialization. 809b1b40b8fSSerge Semin * Zero value means unlimited number of entries. 810cb8cea51SMaxime Ripard * @residue_granularity: granularity of the transfer residue reported 811cb8cea51SMaxime Ripard * by tx_status 812fe4ada2dSRandy Dunlap * @device_alloc_chan_resources: allocate resources and return the 813fe4ada2dSRandy Dunlap * number of allocated descriptors 8144f910c03SPeter Ujfalusi * @device_router_config: optional callback for DMA router configuration 815fe4ada2dSRandy Dunlap * @device_free_chan_resources: release DMA channel's resources 8167405f74bSDan Williams * @device_prep_dma_memcpy: prepares a memcpy operation 8177405f74bSDan Williams * @device_prep_dma_xor: prepares a xor operation 818099f53cbSDan Williams * @device_prep_dma_xor_val: prepares a xor validation operation 819b2f46fd8SDan Williams * @device_prep_dma_pq: prepares a pq operation 820b2f46fd8SDan Williams * @device_prep_dma_pq_val: prepares a pqzero_sum operation 8214983a501SMaxime Ripard * @device_prep_dma_memset: prepares a memset operation 82250c7cd2bSMaxime Ripard * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list 8237405f74bSDan Williams * @device_prep_dma_interrupt: prepares an end of chain interrupt operation 824dc0ee643SHaavard Skinnemoen * @device_prep_slave_sg: prepares a slave dma operation 825782bc950SSascha Hauer * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio. 826782bc950SSascha Hauer * The function takes a buffer of size buf_len. The callback function will 827782bc950SSascha Hauer * be called after period_len bytes have been transferred. 828b14dab79SJassi Brar * @device_prep_interleaved_dma: Transfer expression in a generic way. 829ff39988aSSiva Yerramreddy * @device_prep_dma_imm_data: DMA's 8 byte immediate data to the dst address 8303b6d694eSSerge Semin * @device_caps: May be used to override the generic DMA slave capabilities 8313b6d694eSSerge Semin * with per-channel specific ones 83294a73e30SMaxime Ripard * @device_config: Pushes a new configuration to a channel, return 0 or an error 83394a73e30SMaxime Ripard * code 83423a3ea2fSMaxime Ripard * @device_pause: Pauses any transfer happening on a channel. Returns 83523a3ea2fSMaxime Ripard * 0 or an error code 83623a3ea2fSMaxime Ripard * @device_resume: Resumes any transfer on a channel previously 83723a3ea2fSMaxime Ripard * paused. Returns 0 or an error code 8387fa0cf46SMaxime Ripard * @device_terminate_all: Aborts all transfers on a channel. Returns 0 8397fa0cf46SMaxime Ripard * or an error code 840b36f09c3SLars-Peter Clausen * @device_synchronize: Synchronizes the termination of a transfers to the 841b36f09c3SLars-Peter Clausen * current context. 84207934481SLinus Walleij * @device_tx_status: poll for transaction completion, the optional 84307934481SLinus Walleij * txstate parameter can be supplied with a pointer to get a 84425985edcSLucas De Marchi * struct with auxiliary transfer status information, otherwise the call 84507934481SLinus Walleij * will just return a simple status code 8467405f74bSDan Williams * @device_issue_pending: push pending transactions to hardware 8479eeacd3aSRobert Jarzmik * @descriptor_reuse: a submitted transfer can be resubmitted after completion 8488ad342a8SLogan Gunthorpe * @device_release: called sometime atfer dma_async_device_unregister() is 8498ad342a8SLogan Gunthorpe * called and there are no further references to this structure. This 8508ad342a8SLogan Gunthorpe * must be implemented to free resources however many existing drivers 8518ad342a8SLogan Gunthorpe * do not and are therefore not safe to unbind while in use. 852e937cc1dSPeter Ujfalusi * @dbg_summary_show: optional routine to show contents in debugfs; default code 853e937cc1dSPeter Ujfalusi * will be used when this is omitted, but custom code can show extra, 854e937cc1dSPeter Ujfalusi * controller specific information. 855c13c8260SChris Leech */ 856c13c8260SChris Leech struct dma_device { 8578ad342a8SLogan Gunthorpe struct kref ref; 858c13c8260SChris Leech unsigned int chancnt; 8590f571515SAtsushi Nemoto unsigned int privatecnt; 860c13c8260SChris Leech struct list_head channels; 861c13c8260SChris Leech struct list_head global_node; 862a8135d0dSPeter Ujfalusi struct dma_filter filter; 8637405f74bSDan Williams dma_cap_mask_t cap_mask; 8644db8fd32SPeter Ujfalusi enum dma_desc_metadata_mode desc_metadata_modes; 865b2f46fd8SDan Williams unsigned short max_xor; 866b2f46fd8SDan Williams unsigned short max_pq; 86777a68e56SMaxime Ripard enum dmaengine_alignment copy_align; 86877a68e56SMaxime Ripard enum dmaengine_alignment xor_align; 86977a68e56SMaxime Ripard enum dmaengine_alignment pq_align; 87077a68e56SMaxime Ripard enum dmaengine_alignment fill_align; 871b2f46fd8SDan Williams #define DMA_HAS_PQ_CONTINUE (1 << 15) 872c13c8260SChris Leech 873c13c8260SChris Leech int dev_id; 8747405f74bSDan Williams struct device *dev; 875dae7a589SLogan Gunthorpe struct module *owner; 87608210094SDave Jiang struct ida chan_ida; 87708210094SDave Jiang struct mutex chan_mutex; /* to protect chan_ida */ 878c13c8260SChris Leech 879cb8cea51SMaxime Ripard u32 src_addr_widths; 880cb8cea51SMaxime Ripard u32 dst_addr_widths; 881cb8cea51SMaxime Ripard u32 directions; 882d97758e0SSerge Semin u32 min_burst; 8836d5bbed3SShawn Lin u32 max_burst; 884b1b40b8fSSerge Semin u32 max_sg_burst; 8859eeacd3aSRobert Jarzmik bool descriptor_reuse; 886cb8cea51SMaxime Ripard enum dma_residue_granularity residue_granularity; 887cb8cea51SMaxime Ripard 888aa1e6f1aSDan Williams int (*device_alloc_chan_resources)(struct dma_chan *chan); 8894f910c03SPeter Ujfalusi int (*device_router_config)(struct dma_chan *chan); 890c13c8260SChris Leech void (*device_free_chan_resources)(struct dma_chan *chan); 8917405f74bSDan Williams 8927405f74bSDan Williams struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)( 893ceacbdbfSMaxime Ripard struct dma_chan *chan, dma_addr_t dst, dma_addr_t src, 894d4c56f97SDan Williams size_t len, unsigned long flags); 895*3218910fSAdrian Larumbe struct dma_async_tx_descriptor *(*device_prep_dma_memcpy_sg)( 896*3218910fSAdrian Larumbe struct dma_chan *chan, 897*3218910fSAdrian Larumbe struct scatterlist *dst_sg, unsigned int dst_nents, 898*3218910fSAdrian Larumbe struct scatterlist *src_sg, unsigned int src_nents, 899*3218910fSAdrian Larumbe unsigned long flags); 9007405f74bSDan Williams struct dma_async_tx_descriptor *(*device_prep_dma_xor)( 901ceacbdbfSMaxime Ripard struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src, 902d4c56f97SDan Williams unsigned int src_cnt, size_t len, unsigned long flags); 903099f53cbSDan Williams struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)( 9040036731cSDan Williams struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt, 905ad283ea4SDan Williams size_t len, enum sum_check_flags *result, unsigned long flags); 906b2f46fd8SDan Williams struct dma_async_tx_descriptor *(*device_prep_dma_pq)( 907b2f46fd8SDan Williams struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src, 908b2f46fd8SDan Williams unsigned int src_cnt, const unsigned char *scf, 909b2f46fd8SDan Williams size_t len, unsigned long flags); 910b2f46fd8SDan Williams struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)( 911b2f46fd8SDan Williams struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src, 912b2f46fd8SDan Williams unsigned int src_cnt, const unsigned char *scf, size_t len, 913b2f46fd8SDan Williams enum sum_check_flags *pqres, unsigned long flags); 9144983a501SMaxime Ripard struct dma_async_tx_descriptor *(*device_prep_dma_memset)( 9154983a501SMaxime Ripard struct dma_chan *chan, dma_addr_t dest, int value, size_t len, 9164983a501SMaxime Ripard unsigned long flags); 91750c7cd2bSMaxime Ripard struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)( 91850c7cd2bSMaxime Ripard struct dma_chan *chan, struct scatterlist *sg, 91950c7cd2bSMaxime Ripard unsigned int nents, int value, unsigned long flags); 9207405f74bSDan Williams struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)( 921636bdeaaSDan Williams struct dma_chan *chan, unsigned long flags); 9227405f74bSDan Williams 923dc0ee643SHaavard Skinnemoen struct dma_async_tx_descriptor *(*device_prep_slave_sg)( 924dc0ee643SHaavard Skinnemoen struct dma_chan *chan, struct scatterlist *sgl, 92549920bc6SVinod Koul unsigned int sg_len, enum dma_transfer_direction direction, 926185ecb5fSAlexandre Bounine unsigned long flags, void *context); 927782bc950SSascha Hauer struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)( 928782bc950SSascha Hauer struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, 929185ecb5fSAlexandre Bounine size_t period_len, enum dma_transfer_direction direction, 93031c1e5a1SLaurent Pinchart unsigned long flags); 931b14dab79SJassi Brar struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)( 932b14dab79SJassi Brar struct dma_chan *chan, struct dma_interleaved_template *xt, 933b14dab79SJassi Brar unsigned long flags); 934ff39988aSSiva Yerramreddy struct dma_async_tx_descriptor *(*device_prep_dma_imm_data)( 935ff39988aSSiva Yerramreddy struct dma_chan *chan, dma_addr_t dst, u64 data, 936ff39988aSSiva Yerramreddy unsigned long flags); 93794a73e30SMaxime Ripard 9383b6d694eSSerge Semin void (*device_caps)(struct dma_chan *chan, 9393b6d694eSSerge Semin struct dma_slave_caps *caps); 94094a73e30SMaxime Ripard int (*device_config)(struct dma_chan *chan, 94194a73e30SMaxime Ripard struct dma_slave_config *config); 94223a3ea2fSMaxime Ripard int (*device_pause)(struct dma_chan *chan); 94323a3ea2fSMaxime Ripard int (*device_resume)(struct dma_chan *chan); 9447fa0cf46SMaxime Ripard int (*device_terminate_all)(struct dma_chan *chan); 945b36f09c3SLars-Peter Clausen void (*device_synchronize)(struct dma_chan *chan); 946dc0ee643SHaavard Skinnemoen 94707934481SLinus Walleij enum dma_status (*device_tx_status)(struct dma_chan *chan, 94807934481SLinus Walleij dma_cookie_t cookie, 94907934481SLinus Walleij struct dma_tx_state *txstate); 9507405f74bSDan Williams void (*device_issue_pending)(struct dma_chan *chan); 9518ad342a8SLogan Gunthorpe void (*device_release)(struct dma_device *dev); 952e937cc1dSPeter Ujfalusi /* debugfs support */ 953e937cc1dSPeter Ujfalusi void (*dbg_summary_show)(struct seq_file *s, struct dma_device *dev); 95426cf132dSPeter Ujfalusi struct dentry *dbg_dev_root; 955c13c8260SChris Leech }; 956c13c8260SChris Leech 9576e3ecaf0SSascha Hauer static inline int dmaengine_slave_config(struct dma_chan *chan, 9586e3ecaf0SSascha Hauer struct dma_slave_config *config) 9596e3ecaf0SSascha Hauer { 96094a73e30SMaxime Ripard if (chan->device->device_config) 96194a73e30SMaxime Ripard return chan->device->device_config(chan, config); 96294a73e30SMaxime Ripard 9632c44ad91SMaxime Ripard return -ENOSYS; 9646e3ecaf0SSascha Hauer } 9656e3ecaf0SSascha Hauer 96661cc13a5SAndy Shevchenko static inline bool is_slave_direction(enum dma_transfer_direction direction) 96761cc13a5SAndy Shevchenko { 96861cc13a5SAndy Shevchenko return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM); 96961cc13a5SAndy Shevchenko } 97061cc13a5SAndy Shevchenko 97190b44f8fSVinod Koul static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_single( 972922ee08bSKuninori Morimoto struct dma_chan *chan, dma_addr_t buf, size_t len, 97349920bc6SVinod Koul enum dma_transfer_direction dir, unsigned long flags) 97490b44f8fSVinod Koul { 97590b44f8fSVinod Koul struct scatterlist sg; 976922ee08bSKuninori Morimoto sg_init_table(&sg, 1); 977922ee08bSKuninori Morimoto sg_dma_address(&sg) = buf; 978922ee08bSKuninori Morimoto sg_dma_len(&sg) = len; 97990b44f8fSVinod Koul 980757d12e5SVinod Koul if (!chan || !chan->device || !chan->device->device_prep_slave_sg) 981757d12e5SVinod Koul return NULL; 982757d12e5SVinod Koul 983185ecb5fSAlexandre Bounine return chan->device->device_prep_slave_sg(chan, &sg, 1, 984185ecb5fSAlexandre Bounine dir, flags, NULL); 98590b44f8fSVinod Koul } 98690b44f8fSVinod Koul 98716052827SAlexandre Bounine static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_sg( 98816052827SAlexandre Bounine struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len, 98916052827SAlexandre Bounine enum dma_transfer_direction dir, unsigned long flags) 99016052827SAlexandre Bounine { 991757d12e5SVinod Koul if (!chan || !chan->device || !chan->device->device_prep_slave_sg) 992757d12e5SVinod Koul return NULL; 993757d12e5SVinod Koul 99416052827SAlexandre Bounine return chan->device->device_prep_slave_sg(chan, sgl, sg_len, 995185ecb5fSAlexandre Bounine dir, flags, NULL); 99616052827SAlexandre Bounine } 99716052827SAlexandre Bounine 998e42d98ebSAlexandre Bounine #ifdef CONFIG_RAPIDIO_DMA_ENGINE 999e42d98ebSAlexandre Bounine struct rio_dma_ext; 1000e42d98ebSAlexandre Bounine static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg( 1001e42d98ebSAlexandre Bounine struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len, 1002e42d98ebSAlexandre Bounine enum dma_transfer_direction dir, unsigned long flags, 1003e42d98ebSAlexandre Bounine struct rio_dma_ext *rio_ext) 1004e42d98ebSAlexandre Bounine { 1005757d12e5SVinod Koul if (!chan || !chan->device || !chan->device->device_prep_slave_sg) 1006757d12e5SVinod Koul return NULL; 1007757d12e5SVinod Koul 1008e42d98ebSAlexandre Bounine return chan->device->device_prep_slave_sg(chan, sgl, sg_len, 1009e42d98ebSAlexandre Bounine dir, flags, rio_ext); 1010e42d98ebSAlexandre Bounine } 1011e42d98ebSAlexandre Bounine #endif 1012e42d98ebSAlexandre Bounine 101316052827SAlexandre Bounine static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic( 101416052827SAlexandre Bounine struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, 1015e7736cdeSPeter Ujfalusi size_t period_len, enum dma_transfer_direction dir, 1016e7736cdeSPeter Ujfalusi unsigned long flags) 101716052827SAlexandre Bounine { 1018757d12e5SVinod Koul if (!chan || !chan->device || !chan->device->device_prep_dma_cyclic) 1019757d12e5SVinod Koul return NULL; 1020757d12e5SVinod Koul 102116052827SAlexandre Bounine return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len, 102231c1e5a1SLaurent Pinchart period_len, dir, flags); 10236e3ecaf0SSascha Hauer } 10246e3ecaf0SSascha Hauer 1025a14acb4aSBarry Song static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma( 1026a14acb4aSBarry Song struct dma_chan *chan, struct dma_interleaved_template *xt, 1027a14acb4aSBarry Song unsigned long flags) 1028a14acb4aSBarry Song { 1029757d12e5SVinod Koul if (!chan || !chan->device || !chan->device->device_prep_interleaved_dma) 1030757d12e5SVinod Koul return NULL; 10319c8ebd8bSLaurent Pinchart if (flags & DMA_PREP_REPEAT && 10329c8ebd8bSLaurent Pinchart !test_bit(DMA_REPEAT, chan->device->cap_mask.bits)) 10339c8ebd8bSLaurent Pinchart return NULL; 1034757d12e5SVinod Koul 1035a14acb4aSBarry Song return chan->device->device_prep_interleaved_dma(chan, xt, flags); 1036a14acb4aSBarry Song } 1037a14acb4aSBarry Song 10384983a501SMaxime Ripard static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset( 10394983a501SMaxime Ripard struct dma_chan *chan, dma_addr_t dest, int value, size_t len, 10404983a501SMaxime Ripard unsigned long flags) 10414983a501SMaxime Ripard { 1042757d12e5SVinod Koul if (!chan || !chan->device || !chan->device->device_prep_dma_memset) 10434983a501SMaxime Ripard return NULL; 10444983a501SMaxime Ripard 10454983a501SMaxime Ripard return chan->device->device_prep_dma_memset(chan, dest, value, 10464983a501SMaxime Ripard len, flags); 10474983a501SMaxime Ripard } 10484983a501SMaxime Ripard 104977d65d6fSBoris Brezillon static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy( 105077d65d6fSBoris Brezillon struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, 105177d65d6fSBoris Brezillon size_t len, unsigned long flags) 105277d65d6fSBoris Brezillon { 105377d65d6fSBoris Brezillon if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy) 105477d65d6fSBoris Brezillon return NULL; 105577d65d6fSBoris Brezillon 105677d65d6fSBoris Brezillon return chan->device->device_prep_dma_memcpy(chan, dest, src, 105777d65d6fSBoris Brezillon len, flags); 105877d65d6fSBoris Brezillon } 105977d65d6fSBoris Brezillon 1060*3218910fSAdrian Larumbe static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy_sg( 1061*3218910fSAdrian Larumbe struct dma_chan *chan, 1062*3218910fSAdrian Larumbe struct scatterlist *dst_sg, unsigned int dst_nents, 1063*3218910fSAdrian Larumbe struct scatterlist *src_sg, unsigned int src_nents, 1064*3218910fSAdrian Larumbe unsigned long flags) 1065*3218910fSAdrian Larumbe { 1066*3218910fSAdrian Larumbe if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy_sg) 1067*3218910fSAdrian Larumbe return NULL; 1068*3218910fSAdrian Larumbe 1069*3218910fSAdrian Larumbe return chan->device->device_prep_dma_memcpy_sg(chan, dst_sg, dst_nents, 1070*3218910fSAdrian Larumbe src_sg, src_nents, 1071*3218910fSAdrian Larumbe flags); 1072*3218910fSAdrian Larumbe } 1073*3218910fSAdrian Larumbe 10744db8fd32SPeter Ujfalusi static inline bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan, 10754db8fd32SPeter Ujfalusi enum dma_desc_metadata_mode mode) 10764db8fd32SPeter Ujfalusi { 10774db8fd32SPeter Ujfalusi if (!chan) 10784db8fd32SPeter Ujfalusi return false; 10794db8fd32SPeter Ujfalusi 10804db8fd32SPeter Ujfalusi return !!(chan->device->desc_metadata_modes & mode); 10814db8fd32SPeter Ujfalusi } 10824db8fd32SPeter Ujfalusi 10834db8fd32SPeter Ujfalusi #ifdef CONFIG_DMA_ENGINE 10844db8fd32SPeter Ujfalusi int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc, 10854db8fd32SPeter Ujfalusi void *data, size_t len); 10864db8fd32SPeter Ujfalusi void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc, 10874db8fd32SPeter Ujfalusi size_t *payload_len, size_t *max_len); 10884db8fd32SPeter Ujfalusi int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc, 10894db8fd32SPeter Ujfalusi size_t payload_len); 10904db8fd32SPeter Ujfalusi #else /* CONFIG_DMA_ENGINE */ 10914db8fd32SPeter Ujfalusi static inline int dmaengine_desc_attach_metadata( 10924db8fd32SPeter Ujfalusi struct dma_async_tx_descriptor *desc, void *data, size_t len) 10934db8fd32SPeter Ujfalusi { 10944db8fd32SPeter Ujfalusi return -EINVAL; 10954db8fd32SPeter Ujfalusi } 10964db8fd32SPeter Ujfalusi static inline void *dmaengine_desc_get_metadata_ptr( 10974db8fd32SPeter Ujfalusi struct dma_async_tx_descriptor *desc, size_t *payload_len, 10984db8fd32SPeter Ujfalusi size_t *max_len) 10994db8fd32SPeter Ujfalusi { 11004db8fd32SPeter Ujfalusi return NULL; 11014db8fd32SPeter Ujfalusi } 11024db8fd32SPeter Ujfalusi static inline int dmaengine_desc_set_metadata_len( 11034db8fd32SPeter Ujfalusi struct dma_async_tx_descriptor *desc, size_t payload_len) 11044db8fd32SPeter Ujfalusi { 11054db8fd32SPeter Ujfalusi return -EINVAL; 11064db8fd32SPeter Ujfalusi } 11074db8fd32SPeter Ujfalusi #endif /* CONFIG_DMA_ENGINE */ 11084db8fd32SPeter Ujfalusi 1109b36f09c3SLars-Peter Clausen /** 1110b36f09c3SLars-Peter Clausen * dmaengine_terminate_all() - Terminate all active DMA transfers 1111b36f09c3SLars-Peter Clausen * @chan: The channel for which to terminate the transfers 1112b36f09c3SLars-Peter Clausen * 1113b36f09c3SLars-Peter Clausen * This function is DEPRECATED use either dmaengine_terminate_sync() or 1114b36f09c3SLars-Peter Clausen * dmaengine_terminate_async() instead. 1115b36f09c3SLars-Peter Clausen */ 11166e3ecaf0SSascha Hauer static inline int dmaengine_terminate_all(struct dma_chan *chan) 11176e3ecaf0SSascha Hauer { 11187fa0cf46SMaxime Ripard if (chan->device->device_terminate_all) 11197fa0cf46SMaxime Ripard return chan->device->device_terminate_all(chan); 11207fa0cf46SMaxime Ripard 11212c44ad91SMaxime Ripard return -ENOSYS; 11226e3ecaf0SSascha Hauer } 11236e3ecaf0SSascha Hauer 1124b36f09c3SLars-Peter Clausen /** 1125b36f09c3SLars-Peter Clausen * dmaengine_terminate_async() - Terminate all active DMA transfers 1126b36f09c3SLars-Peter Clausen * @chan: The channel for which to terminate the transfers 1127b36f09c3SLars-Peter Clausen * 1128b36f09c3SLars-Peter Clausen * Calling this function will terminate all active and pending descriptors 1129b36f09c3SLars-Peter Clausen * that have previously been submitted to the channel. It is not guaranteed 1130b36f09c3SLars-Peter Clausen * though that the transfer for the active descriptor has stopped when the 1131b36f09c3SLars-Peter Clausen * function returns. Furthermore it is possible the complete callback of a 1132b36f09c3SLars-Peter Clausen * submitted transfer is still running when this function returns. 1133b36f09c3SLars-Peter Clausen * 1134b36f09c3SLars-Peter Clausen * dmaengine_synchronize() needs to be called before it is safe to free 1135b36f09c3SLars-Peter Clausen * any memory that is accessed by previously submitted descriptors or before 1136b36f09c3SLars-Peter Clausen * freeing any resources accessed from within the completion callback of any 113720d60f63SMaciej Grochowski * previously submitted descriptors. 1138b36f09c3SLars-Peter Clausen * 1139b36f09c3SLars-Peter Clausen * This function can be called from atomic context as well as from within a 1140b36f09c3SLars-Peter Clausen * complete callback of a descriptor submitted on the same channel. 1141b36f09c3SLars-Peter Clausen * 1142b36f09c3SLars-Peter Clausen * If none of the two conditions above apply consider using 1143b36f09c3SLars-Peter Clausen * dmaengine_terminate_sync() instead. 1144b36f09c3SLars-Peter Clausen */ 1145b36f09c3SLars-Peter Clausen static inline int dmaengine_terminate_async(struct dma_chan *chan) 1146b36f09c3SLars-Peter Clausen { 1147b36f09c3SLars-Peter Clausen if (chan->device->device_terminate_all) 1148b36f09c3SLars-Peter Clausen return chan->device->device_terminate_all(chan); 1149b36f09c3SLars-Peter Clausen 1150b36f09c3SLars-Peter Clausen return -EINVAL; 1151b36f09c3SLars-Peter Clausen } 1152b36f09c3SLars-Peter Clausen 1153b36f09c3SLars-Peter Clausen /** 1154b36f09c3SLars-Peter Clausen * dmaengine_synchronize() - Synchronize DMA channel termination 1155b36f09c3SLars-Peter Clausen * @chan: The channel to synchronize 1156b36f09c3SLars-Peter Clausen * 1157b36f09c3SLars-Peter Clausen * Synchronizes to the DMA channel termination to the current context. When this 1158b36f09c3SLars-Peter Clausen * function returns it is guaranteed that all transfers for previously issued 115920d60f63SMaciej Grochowski * descriptors have stopped and it is safe to free the memory associated 1160b36f09c3SLars-Peter Clausen * with them. Furthermore it is guaranteed that all complete callback functions 1161b36f09c3SLars-Peter Clausen * for a previously submitted descriptor have finished running and it is safe to 1162b36f09c3SLars-Peter Clausen * free resources accessed from within the complete callbacks. 1163b36f09c3SLars-Peter Clausen * 1164b36f09c3SLars-Peter Clausen * The behavior of this function is undefined if dma_async_issue_pending() has 1165b36f09c3SLars-Peter Clausen * been called between dmaengine_terminate_async() and this function. 1166b36f09c3SLars-Peter Clausen * 1167b36f09c3SLars-Peter Clausen * This function must only be called from non-atomic context and must not be 1168b36f09c3SLars-Peter Clausen * called from within a complete callback of a descriptor submitted on the same 1169b36f09c3SLars-Peter Clausen * channel. 1170b36f09c3SLars-Peter Clausen */ 1171b36f09c3SLars-Peter Clausen static inline void dmaengine_synchronize(struct dma_chan *chan) 1172b36f09c3SLars-Peter Clausen { 1173b1d6ab1aSLars-Peter Clausen might_sleep(); 1174b1d6ab1aSLars-Peter Clausen 1175b36f09c3SLars-Peter Clausen if (chan->device->device_synchronize) 1176b36f09c3SLars-Peter Clausen chan->device->device_synchronize(chan); 1177b36f09c3SLars-Peter Clausen } 1178b36f09c3SLars-Peter Clausen 1179b36f09c3SLars-Peter Clausen /** 1180b36f09c3SLars-Peter Clausen * dmaengine_terminate_sync() - Terminate all active DMA transfers 1181b36f09c3SLars-Peter Clausen * @chan: The channel for which to terminate the transfers 1182b36f09c3SLars-Peter Clausen * 1183b36f09c3SLars-Peter Clausen * Calling this function will terminate all active and pending transfers 1184b36f09c3SLars-Peter Clausen * that have previously been submitted to the channel. It is similar to 1185b36f09c3SLars-Peter Clausen * dmaengine_terminate_async() but guarantees that the DMA transfer has actually 1186b36f09c3SLars-Peter Clausen * stopped and that all complete callbacks have finished running when the 1187b36f09c3SLars-Peter Clausen * function returns. 1188b36f09c3SLars-Peter Clausen * 1189b36f09c3SLars-Peter Clausen * This function must only be called from non-atomic context and must not be 1190b36f09c3SLars-Peter Clausen * called from within a complete callback of a descriptor submitted on the same 1191b36f09c3SLars-Peter Clausen * channel. 1192b36f09c3SLars-Peter Clausen */ 1193b36f09c3SLars-Peter Clausen static inline int dmaengine_terminate_sync(struct dma_chan *chan) 1194b36f09c3SLars-Peter Clausen { 1195b36f09c3SLars-Peter Clausen int ret; 1196b36f09c3SLars-Peter Clausen 1197b36f09c3SLars-Peter Clausen ret = dmaengine_terminate_async(chan); 1198b36f09c3SLars-Peter Clausen if (ret) 1199b36f09c3SLars-Peter Clausen return ret; 1200b36f09c3SLars-Peter Clausen 1201b36f09c3SLars-Peter Clausen dmaengine_synchronize(chan); 1202b36f09c3SLars-Peter Clausen 1203b36f09c3SLars-Peter Clausen return 0; 1204b36f09c3SLars-Peter Clausen } 1205b36f09c3SLars-Peter Clausen 12066e3ecaf0SSascha Hauer static inline int dmaengine_pause(struct dma_chan *chan) 12076e3ecaf0SSascha Hauer { 120823a3ea2fSMaxime Ripard if (chan->device->device_pause) 120923a3ea2fSMaxime Ripard return chan->device->device_pause(chan); 121023a3ea2fSMaxime Ripard 12112c44ad91SMaxime Ripard return -ENOSYS; 12126e3ecaf0SSascha Hauer } 12136e3ecaf0SSascha Hauer 12146e3ecaf0SSascha Hauer static inline int dmaengine_resume(struct dma_chan *chan) 12156e3ecaf0SSascha Hauer { 121623a3ea2fSMaxime Ripard if (chan->device->device_resume) 121723a3ea2fSMaxime Ripard return chan->device->device_resume(chan); 121823a3ea2fSMaxime Ripard 12192c44ad91SMaxime Ripard return -ENOSYS; 12206e3ecaf0SSascha Hauer } 12216e3ecaf0SSascha Hauer 12223052cc2cSLars-Peter Clausen static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan, 12233052cc2cSLars-Peter Clausen dma_cookie_t cookie, struct dma_tx_state *state) 12243052cc2cSLars-Peter Clausen { 12253052cc2cSLars-Peter Clausen return chan->device->device_tx_status(chan, cookie, state); 12263052cc2cSLars-Peter Clausen } 12273052cc2cSLars-Peter Clausen 122898d530feSRussell King - ARM Linux static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc) 12296e3ecaf0SSascha Hauer { 12306e3ecaf0SSascha Hauer return desc->tx_submit(desc); 12316e3ecaf0SSascha Hauer } 12326e3ecaf0SSascha Hauer 123377a68e56SMaxime Ripard static inline bool dmaengine_check_align(enum dmaengine_alignment align, 123477a68e56SMaxime Ripard size_t off1, size_t off2, size_t len) 123583544ae9SDan Williams { 123688ac039cSAndy Shevchenko return !(((1 << align) - 1) & (off1 | off2 | len)); 123783544ae9SDan Williams } 123883544ae9SDan Williams 123983544ae9SDan Williams static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1, 124083544ae9SDan Williams size_t off2, size_t len) 124183544ae9SDan Williams { 124283544ae9SDan Williams return dmaengine_check_align(dev->copy_align, off1, off2, len); 124383544ae9SDan Williams } 124483544ae9SDan Williams 124583544ae9SDan Williams static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1, 124683544ae9SDan Williams size_t off2, size_t len) 124783544ae9SDan Williams { 124883544ae9SDan Williams return dmaengine_check_align(dev->xor_align, off1, off2, len); 124983544ae9SDan Williams } 125083544ae9SDan Williams 125183544ae9SDan Williams static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1, 125283544ae9SDan Williams size_t off2, size_t len) 125383544ae9SDan Williams { 125483544ae9SDan Williams return dmaengine_check_align(dev->pq_align, off1, off2, len); 125583544ae9SDan Williams } 125683544ae9SDan Williams 12574983a501SMaxime Ripard static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1, 12584983a501SMaxime Ripard size_t off2, size_t len) 12594983a501SMaxime Ripard { 12604983a501SMaxime Ripard return dmaengine_check_align(dev->fill_align, off1, off2, len); 12614983a501SMaxime Ripard } 12624983a501SMaxime Ripard 1263b2f46fd8SDan Williams static inline void 1264b2f46fd8SDan Williams dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue) 1265b2f46fd8SDan Williams { 1266b2f46fd8SDan Williams dma->max_pq = maxpq; 1267b2f46fd8SDan Williams if (has_pq_continue) 1268b2f46fd8SDan Williams dma->max_pq |= DMA_HAS_PQ_CONTINUE; 1269b2f46fd8SDan Williams } 1270b2f46fd8SDan Williams 1271b2f46fd8SDan Williams static inline bool dmaf_continue(enum dma_ctrl_flags flags) 1272b2f46fd8SDan Williams { 1273b2f46fd8SDan Williams return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE; 1274b2f46fd8SDan Williams } 1275b2f46fd8SDan Williams 1276b2f46fd8SDan Williams static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags) 1277b2f46fd8SDan Williams { 1278b2f46fd8SDan Williams enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P; 1279b2f46fd8SDan Williams 1280b2f46fd8SDan Williams return (flags & mask) == mask; 1281b2f46fd8SDan Williams } 1282b2f46fd8SDan Williams 1283b2f46fd8SDan Williams static inline bool dma_dev_has_pq_continue(struct dma_device *dma) 1284b2f46fd8SDan Williams { 1285b2f46fd8SDan Williams return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE; 1286b2f46fd8SDan Williams } 1287b2f46fd8SDan Williams 1288d3f3cf85SMathieu Lacage static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma) 1289b2f46fd8SDan Williams { 1290b2f46fd8SDan Williams return dma->max_pq & ~DMA_HAS_PQ_CONTINUE; 1291b2f46fd8SDan Williams } 1292b2f46fd8SDan Williams 1293b2f46fd8SDan Williams /* dma_maxpq - reduce maxpq in the face of continued operations 1294b2f46fd8SDan Williams * @dma - dma device with PQ capability 1295b2f46fd8SDan Williams * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set 1296b2f46fd8SDan Williams * 1297b2f46fd8SDan Williams * When an engine does not support native continuation we need 3 extra 1298b2f46fd8SDan Williams * source slots to reuse P and Q with the following coefficients: 1299b2f46fd8SDan Williams * 1/ {00} * P : remove P from Q', but use it as a source for P' 1300b2f46fd8SDan Williams * 2/ {01} * Q : use Q to continue Q' calculation 1301b2f46fd8SDan Williams * 3/ {00} * Q : subtract Q from P' to cancel (2) 1302b2f46fd8SDan Williams * 1303b2f46fd8SDan Williams * In the case where P is disabled we only need 1 extra source: 1304b2f46fd8SDan Williams * 1/ {01} * Q : use Q to continue Q' calculation 1305b2f46fd8SDan Williams */ 1306b2f46fd8SDan Williams static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags) 1307b2f46fd8SDan Williams { 1308b2f46fd8SDan Williams if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags)) 1309b2f46fd8SDan Williams return dma_dev_to_maxpq(dma); 13105f77dd85SAndy Shevchenko if (dmaf_p_disabled_continue(flags)) 1311b2f46fd8SDan Williams return dma_dev_to_maxpq(dma) - 1; 13125f77dd85SAndy Shevchenko if (dmaf_continue(flags)) 1313b2f46fd8SDan Williams return dma_dev_to_maxpq(dma) - 3; 1314b2f46fd8SDan Williams BUG(); 1315b2f46fd8SDan Williams } 1316b2f46fd8SDan Williams 131787d001efSMaxime Ripard static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg, 131887d001efSMaxime Ripard size_t dir_icg) 131987d001efSMaxime Ripard { 132087d001efSMaxime Ripard if (inc) { 132187d001efSMaxime Ripard if (dir_icg) 132287d001efSMaxime Ripard return dir_icg; 13235f77dd85SAndy Shevchenko if (sgl) 132487d001efSMaxime Ripard return icg; 132587d001efSMaxime Ripard } 132687d001efSMaxime Ripard 132787d001efSMaxime Ripard return 0; 132887d001efSMaxime Ripard } 132987d001efSMaxime Ripard 133087d001efSMaxime Ripard static inline size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt, 133187d001efSMaxime Ripard struct data_chunk *chunk) 133287d001efSMaxime Ripard { 133387d001efSMaxime Ripard return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl, 133487d001efSMaxime Ripard chunk->icg, chunk->dst_icg); 133587d001efSMaxime Ripard } 133687d001efSMaxime Ripard 133787d001efSMaxime Ripard static inline size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt, 133887d001efSMaxime Ripard struct data_chunk *chunk) 133987d001efSMaxime Ripard { 134087d001efSMaxime Ripard return dmaengine_get_icg(xt->src_inc, xt->src_sgl, 134187d001efSMaxime Ripard chunk->icg, chunk->src_icg); 134287d001efSMaxime Ripard } 134387d001efSMaxime Ripard 1344c13c8260SChris Leech /* --- public DMA engine API --- */ 1345c13c8260SChris Leech 1346649274d9SDan Williams #ifdef CONFIG_DMA_ENGINE 1347209b84a8SDan Williams void dmaengine_get(void); 1348209b84a8SDan Williams void dmaengine_put(void); 1349649274d9SDan Williams #else 1350649274d9SDan Williams static inline void dmaengine_get(void) 1351649274d9SDan Williams { 1352649274d9SDan Williams } 1353649274d9SDan Williams static inline void dmaengine_put(void) 1354649274d9SDan Williams { 1355649274d9SDan Williams } 1356649274d9SDan Williams #endif 1357649274d9SDan Williams 1358729b5d1bSDan Williams #ifdef CONFIG_ASYNC_TX_DMA 1359729b5d1bSDan Williams #define async_dmaengine_get() dmaengine_get() 1360729b5d1bSDan Williams #define async_dmaengine_put() dmaengine_put() 13615fc6d897SDan Williams #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH 1362138f4c35SDan Williams #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX) 1363138f4c35SDan Williams #else 1364729b5d1bSDan Williams #define async_dma_find_channel(type) dma_find_channel(type) 13655fc6d897SDan Williams #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */ 1366729b5d1bSDan Williams #else 1367729b5d1bSDan Williams static inline void async_dmaengine_get(void) 1368729b5d1bSDan Williams { 1369729b5d1bSDan Williams } 1370729b5d1bSDan Williams static inline void async_dmaengine_put(void) 1371729b5d1bSDan Williams { 1372729b5d1bSDan Williams } 1373729b5d1bSDan Williams static inline struct dma_chan * 1374729b5d1bSDan Williams async_dma_find_channel(enum dma_transaction_type type) 1375729b5d1bSDan Williams { 1376729b5d1bSDan Williams return NULL; 1377729b5d1bSDan Williams } 1378138f4c35SDan Williams #endif /* CONFIG_ASYNC_TX_DMA */ 13797405f74bSDan Williams void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx, 13807405f74bSDan Williams struct dma_chan *chan); 1381c13c8260SChris Leech 13820839875eSDan Williams static inline void async_tx_ack(struct dma_async_tx_descriptor *tx) 13837405f74bSDan Williams { 1384636bdeaaSDan Williams tx->flags |= DMA_CTRL_ACK; 1385636bdeaaSDan Williams } 1386636bdeaaSDan Williams 1387ef560682SGuennadi Liakhovetski static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx) 1388ef560682SGuennadi Liakhovetski { 1389ef560682SGuennadi Liakhovetski tx->flags &= ~DMA_CTRL_ACK; 1390ef560682SGuennadi Liakhovetski } 1391ef560682SGuennadi Liakhovetski 13920839875eSDan Williams static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx) 1393636bdeaaSDan Williams { 13940839875eSDan Williams return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK; 1395c13c8260SChris Leech } 1396c13c8260SChris Leech 13977405f74bSDan Williams #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask)) 13987405f74bSDan Williams static inline void 13997405f74bSDan Williams __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp) 14007405f74bSDan Williams { 14017405f74bSDan Williams set_bit(tx_type, dstp->bits); 14027405f74bSDan Williams } 14037405f74bSDan Williams 14040f571515SAtsushi Nemoto #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask)) 14050f571515SAtsushi Nemoto static inline void 14060f571515SAtsushi Nemoto __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp) 14070f571515SAtsushi Nemoto { 14080f571515SAtsushi Nemoto clear_bit(tx_type, dstp->bits); 14090f571515SAtsushi Nemoto } 14100f571515SAtsushi Nemoto 141133df8ca0SDan Williams #define dma_cap_zero(mask) __dma_cap_zero(&(mask)) 141233df8ca0SDan Williams static inline void __dma_cap_zero(dma_cap_mask_t *dstp) 141333df8ca0SDan Williams { 141433df8ca0SDan Williams bitmap_zero(dstp->bits, DMA_TX_TYPE_END); 141533df8ca0SDan Williams } 141633df8ca0SDan Williams 14177405f74bSDan Williams #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask)) 14187405f74bSDan Williams static inline int 14197405f74bSDan Williams __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp) 14207405f74bSDan Williams { 14217405f74bSDan Williams return test_bit(tx_type, srcp->bits); 14227405f74bSDan Williams } 14237405f74bSDan Williams 14247405f74bSDan Williams #define for_each_dma_cap_mask(cap, mask) \ 1425e5a087fdSAkinobu Mita for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END) 14267405f74bSDan Williams 1427c13c8260SChris Leech /** 14287405f74bSDan Williams * dma_async_issue_pending - flush pending transactions to HW 1429fe4ada2dSRandy Dunlap * @chan: target DMA channel 1430c13c8260SChris Leech * 1431c13c8260SChris Leech * This allows drivers to push copies to HW in batches, 1432c13c8260SChris Leech * reducing MMIO writes where possible. 1433c13c8260SChris Leech */ 14347405f74bSDan Williams static inline void dma_async_issue_pending(struct dma_chan *chan) 1435c13c8260SChris Leech { 1436ec8670f1SDan Williams chan->device->device_issue_pending(chan); 1437c13c8260SChris Leech } 1438c13c8260SChris Leech 1439c13c8260SChris Leech /** 14407405f74bSDan Williams * dma_async_is_tx_complete - poll for transaction completion 1441c13c8260SChris Leech * @chan: DMA channel 1442c13c8260SChris Leech * @cookie: transaction identifier to check status of 1443c13c8260SChris Leech * @last: returns last completed cookie, can be NULL 1444c13c8260SChris Leech * @used: returns last issued cookie, can be NULL 1445c13c8260SChris Leech * 1446c13c8260SChris Leech * If @last and @used are passed in, upon return they reflect the driver 1447c13c8260SChris Leech * internal state and can be used with dma_async_is_complete() to check 1448c13c8260SChris Leech * the status of multiple cookies without re-checking hardware state. 1449c13c8260SChris Leech */ 14507405f74bSDan Williams static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan, 1451c13c8260SChris Leech dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used) 1452c13c8260SChris Leech { 145307934481SLinus Walleij struct dma_tx_state state; 145407934481SLinus Walleij enum dma_status status; 145507934481SLinus Walleij 145607934481SLinus Walleij status = chan->device->device_tx_status(chan, cookie, &state); 145707934481SLinus Walleij if (last) 145807934481SLinus Walleij *last = state.last; 145907934481SLinus Walleij if (used) 146007934481SLinus Walleij *used = state.used; 146107934481SLinus Walleij return status; 1462c13c8260SChris Leech } 1463c13c8260SChris Leech 1464c13c8260SChris Leech /** 1465c13c8260SChris Leech * dma_async_is_complete - test a cookie against chan state 1466c13c8260SChris Leech * @cookie: transaction identifier to test status of 1467c13c8260SChris Leech * @last_complete: last know completed transaction 1468c13c8260SChris Leech * @last_used: last cookie value handed out 1469c13c8260SChris Leech * 1470e239345fSBartlomiej Zolnierkiewicz * dma_async_is_complete() is used in dma_async_is_tx_complete() 14718a5703f8SSebastian Siewior * the test logic is separated for lightweight testing of multiple cookies 1472c13c8260SChris Leech */ 1473c13c8260SChris Leech static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie, 1474c13c8260SChris Leech dma_cookie_t last_complete, dma_cookie_t last_used) 1475c13c8260SChris Leech { 1476c13c8260SChris Leech if (last_complete <= last_used) { 1477c13c8260SChris Leech if ((cookie <= last_complete) || (cookie > last_used)) 1478adfedd9aSVinod Koul return DMA_COMPLETE; 1479c13c8260SChris Leech } else { 1480c13c8260SChris Leech if ((cookie <= last_complete) && (cookie > last_used)) 1481adfedd9aSVinod Koul return DMA_COMPLETE; 1482c13c8260SChris Leech } 1483c13c8260SChris Leech return DMA_IN_PROGRESS; 1484c13c8260SChris Leech } 1485c13c8260SChris Leech 1486bca34692SDan Williams static inline void 1487bca34692SDan Williams dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue) 1488bca34692SDan Williams { 14893a92063bSAndy Shevchenko if (!st) 14903a92063bSAndy Shevchenko return; 14913a92063bSAndy Shevchenko 1492bca34692SDan Williams st->last = last; 1493bca34692SDan Williams st->used = used; 1494bca34692SDan Williams st->residue = residue; 1495bca34692SDan Williams } 1496bca34692SDan Williams 149707f2211eSDan Williams #ifdef CONFIG_DMA_ENGINE 14984a43f394SJon Mason struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type); 14994a43f394SJon Mason enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie); 150007f2211eSDan Williams enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx); 1501c50331e8SDan Williams void dma_issue_pending_all(void); 1502a53e28daSLars-Peter Clausen struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask, 1503f5151311SBaolin Wang dma_filter_fn fn, void *fn_param, 1504f5151311SBaolin Wang struct device_node *np); 1505a8135d0dSPeter Ujfalusi 1506a8135d0dSPeter Ujfalusi struct dma_chan *dma_request_chan(struct device *dev, const char *name); 1507a8135d0dSPeter Ujfalusi struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask); 1508a8135d0dSPeter Ujfalusi 15098f33d527SGuennadi Liakhovetski void dma_release_channel(struct dma_chan *chan); 1510fdb8df99SLaurent Pinchart int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps); 151107f2211eSDan Williams #else 15124a43f394SJon Mason static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type) 15134a43f394SJon Mason { 15144a43f394SJon Mason return NULL; 15154a43f394SJon Mason } 15164a43f394SJon Mason static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie) 15174a43f394SJon Mason { 1518adfedd9aSVinod Koul return DMA_COMPLETE; 15194a43f394SJon Mason } 152007f2211eSDan Williams static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx) 152107f2211eSDan Williams { 1522adfedd9aSVinod Koul return DMA_COMPLETE; 152307f2211eSDan Williams } 1524c50331e8SDan Williams static inline void dma_issue_pending_all(void) 1525c50331e8SDan Williams { 15268f33d527SGuennadi Liakhovetski } 1527a53e28daSLars-Peter Clausen static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask, 1528f5151311SBaolin Wang dma_filter_fn fn, 1529f5151311SBaolin Wang void *fn_param, 1530f5151311SBaolin Wang struct device_node *np) 15318f33d527SGuennadi Liakhovetski { 15328f33d527SGuennadi Liakhovetski return NULL; 15338f33d527SGuennadi Liakhovetski } 1534a8135d0dSPeter Ujfalusi static inline struct dma_chan *dma_request_chan(struct device *dev, 1535a8135d0dSPeter Ujfalusi const char *name) 1536a8135d0dSPeter Ujfalusi { 1537a8135d0dSPeter Ujfalusi return ERR_PTR(-ENODEV); 1538a8135d0dSPeter Ujfalusi } 1539a8135d0dSPeter Ujfalusi static inline struct dma_chan *dma_request_chan_by_mask( 1540a8135d0dSPeter Ujfalusi const dma_cap_mask_t *mask) 1541a8135d0dSPeter Ujfalusi { 1542a8135d0dSPeter Ujfalusi return ERR_PTR(-ENODEV); 1543a8135d0dSPeter Ujfalusi } 15448f33d527SGuennadi Liakhovetski static inline void dma_release_channel(struct dma_chan *chan) 15458f33d527SGuennadi Liakhovetski { 1546c50331e8SDan Williams } 1547fdb8df99SLaurent Pinchart static inline int dma_get_slave_caps(struct dma_chan *chan, 1548fdb8df99SLaurent Pinchart struct dma_slave_caps *caps) 1549fdb8df99SLaurent Pinchart { 1550fdb8df99SLaurent Pinchart return -ENXIO; 1551fdb8df99SLaurent Pinchart } 155207f2211eSDan Williams #endif 1553c13c8260SChris Leech 155427242021SVinod Koul static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx) 155527242021SVinod Koul { 155627242021SVinod Koul struct dma_slave_caps caps; 155753a256a9SLukas Wunner int ret; 155827242021SVinod Koul 155953a256a9SLukas Wunner ret = dma_get_slave_caps(tx->chan, &caps); 156053a256a9SLukas Wunner if (ret) 156153a256a9SLukas Wunner return ret; 156227242021SVinod Koul 15633a92063bSAndy Shevchenko if (!caps.descriptor_reuse) 15643a92063bSAndy Shevchenko return -EPERM; 15653a92063bSAndy Shevchenko 156627242021SVinod Koul tx->flags |= DMA_CTRL_REUSE; 156727242021SVinod Koul return 0; 156827242021SVinod Koul } 156927242021SVinod Koul 157027242021SVinod Koul static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx) 157127242021SVinod Koul { 157227242021SVinod Koul tx->flags &= ~DMA_CTRL_REUSE; 157327242021SVinod Koul } 157427242021SVinod Koul 157527242021SVinod Koul static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx) 157627242021SVinod Koul { 157727242021SVinod Koul return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE; 157827242021SVinod Koul } 157927242021SVinod Koul 158027242021SVinod Koul static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc) 158127242021SVinod Koul { 158227242021SVinod Koul /* this is supported for reusable desc, so check that */ 15833a92063bSAndy Shevchenko if (!dmaengine_desc_test_reuse(desc)) 158427242021SVinod Koul return -EPERM; 15853a92063bSAndy Shevchenko 15863a92063bSAndy Shevchenko return desc->desc_free(desc); 158727242021SVinod Koul } 158827242021SVinod Koul 1589c13c8260SChris Leech /* --- DMA device --- */ 1590c13c8260SChris Leech 1591c13c8260SChris Leech int dma_async_device_register(struct dma_device *device); 1592f39b948dSHuang Shijie int dmaenginem_async_device_register(struct dma_device *device); 1593c13c8260SChris Leech void dma_async_device_unregister(struct dma_device *device); 1594e81274cdSDave Jiang int dma_async_device_channel_register(struct dma_device *device, 1595e81274cdSDave Jiang struct dma_chan *chan); 1596e81274cdSDave Jiang void dma_async_device_channel_unregister(struct dma_device *device, 1597e81274cdSDave Jiang struct dma_chan *chan); 159807f2211eSDan Williams void dma_run_dependencies(struct dma_async_tx_descriptor *tx); 1599f5151311SBaolin Wang #define dma_request_channel(mask, x, y) \ 1600f5151311SBaolin Wang __dma_request_channel(&(mask), x, y, NULL) 1601864ef69bSMatt Porter 16027547dbd3SPeter Ujfalusi /* Deprecated, please use dma_request_chan() directly */ 16037547dbd3SPeter Ujfalusi static inline struct dma_chan * __deprecated 16047547dbd3SPeter Ujfalusi dma_request_slave_channel(struct device *dev, const char *name) 16057547dbd3SPeter Ujfalusi { 16067547dbd3SPeter Ujfalusi struct dma_chan *ch = dma_request_chan(dev, name); 16077547dbd3SPeter Ujfalusi 16087547dbd3SPeter Ujfalusi return IS_ERR(ch) ? NULL : ch; 16097547dbd3SPeter Ujfalusi } 16107547dbd3SPeter Ujfalusi 1611864ef69bSMatt Porter static inline struct dma_chan 161271ca5b78SGeert Uytterhoeven *dma_request_slave_channel_compat(const dma_cap_mask_t mask, 1613a53e28daSLars-Peter Clausen dma_filter_fn fn, void *fn_param, 16141dc04288SJarkko Nikula struct device *dev, const char *name) 1615864ef69bSMatt Porter { 1616864ef69bSMatt Porter struct dma_chan *chan; 1617864ef69bSMatt Porter 1618864ef69bSMatt Porter chan = dma_request_slave_channel(dev, name); 1619864ef69bSMatt Porter if (chan) 1620864ef69bSMatt Porter return chan; 1621864ef69bSMatt Porter 16227dfffb95SGeert Uytterhoeven if (!fn || !fn_param) 16237dfffb95SGeert Uytterhoeven return NULL; 16247dfffb95SGeert Uytterhoeven 162571ca5b78SGeert Uytterhoeven return __dma_request_channel(&mask, fn, fn_param, NULL); 1626864ef69bSMatt Porter } 1627816ebf48SPeter Ujfalusi 1628816ebf48SPeter Ujfalusi static inline char * 1629816ebf48SPeter Ujfalusi dmaengine_get_direction_text(enum dma_transfer_direction dir) 1630816ebf48SPeter Ujfalusi { 1631816ebf48SPeter Ujfalusi switch (dir) { 1632816ebf48SPeter Ujfalusi case DMA_DEV_TO_MEM: 1633816ebf48SPeter Ujfalusi return "DEV_TO_MEM"; 1634816ebf48SPeter Ujfalusi case DMA_MEM_TO_DEV: 1635816ebf48SPeter Ujfalusi return "MEM_TO_DEV"; 1636816ebf48SPeter Ujfalusi case DMA_MEM_TO_MEM: 1637816ebf48SPeter Ujfalusi return "MEM_TO_MEM"; 1638816ebf48SPeter Ujfalusi case DMA_DEV_TO_DEV: 1639816ebf48SPeter Ujfalusi return "DEV_TO_DEV"; 1640816ebf48SPeter Ujfalusi default: 1641816ebf48SPeter Ujfalusi return "invalid"; 1642de5506e1SChris Leech } 16431873300aSAndy Shevchenko } 1644ab650ef6SPeter Ujfalusi 1645ab650ef6SPeter Ujfalusi static inline struct device *dmaengine_get_dma_device(struct dma_chan *chan) 1646ab650ef6SPeter Ujfalusi { 1647ab650ef6SPeter Ujfalusi if (chan->dev->chan_dma_dev) 1648ab650ef6SPeter Ujfalusi return &chan->dev->device; 1649ab650ef6SPeter Ujfalusi 1650ab650ef6SPeter Ujfalusi return chan->device->dev; 1651ab650ef6SPeter Ujfalusi } 1652ab650ef6SPeter Ujfalusi 1653c13c8260SChris Leech #endif /* DMAENGINE_H */ 1654