dmaengine.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * The contents of this file are private to DMA engine drivers, and is not
  4. * part of the API to be used by DMA engine users.
  5. */
  6. #ifndef _DMAENGINE_H
  7. #define _DMAENGINE_H
  8. #include <stdbool.h>
  9. #include "scatterlist.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. enum dma_data_direction {
  14. DMA_BIDIRECTIONAL = 0,
  15. DMA_TO_DEVICE = 1,
  16. DMA_FROM_DEVICE = 2,
  17. DMA_NONE = 3,
  18. };
  19. /**
  20. * typedef dma_cookie_t - an opaque DMA cookie
  21. *
  22. * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
  23. */
  24. typedef s32 dma_cookie_t;
  25. #define DMA_MIN_COOKIE 1
  26. static __INLINE int dma_submit_error(dma_cookie_t cookie)
  27. {
  28. return cookie < 0 ? cookie : 0;
  29. }
  30. /**
  31. * enum dma_status - DMA transaction status
  32. * @DMA_COMPLETE: transaction completed
  33. * @DMA_IN_PROGRESS: transaction not yet processed
  34. * @DMA_PAUSED: transaction is paused
  35. * @DMA_ERROR: transaction failed
  36. */
  37. enum dma_status {
  38. DMA_COMPLETE,
  39. DMA_IN_PROGRESS,
  40. DMA_PAUSED,
  41. DMA_ERROR,
  42. };
  43. /**
  44. * enum dma_transaction_type - DMA transaction types/indexes
  45. *
  46. * Note: The DMA_ASYNC_TX capability is not to be set by drivers. It is
  47. * automatically set as dma devices are registered.
  48. */
  49. enum dma_transaction_type {
  50. DMA_MEMCPY,
  51. DMA_XOR,
  52. DMA_PQ,
  53. DMA_XOR_VAL,
  54. DMA_PQ_VAL,
  55. DMA_MEMSET,
  56. DMA_MEMSET_SG,
  57. DMA_INTERRUPT,
  58. DMA_PRIVATE,
  59. DMA_ASYNC_TX,
  60. DMA_SLAVE,
  61. DMA_CYCLIC,
  62. DMA_INTERLEAVE,
  63. /* last transaction type for creation of the capabilities mask */
  64. DMA_TX_TYPE_END,
  65. };
  66. /**
  67. * enum dma_transfer_direction - dma transfer mode and direction indicator
  68. * @DMA_MEM_TO_MEM: Async/Memcpy mode
  69. * @DMA_MEM_TO_DEV: Slave mode & From Memory to Device
  70. * @DMA_DEV_TO_MEM: Slave mode & From Device to Memory
  71. * @DMA_DEV_TO_DEV: Slave mode & From Device to Device
  72. */
  73. enum dma_transfer_direction {
  74. DMA_MEM_TO_MEM,
  75. DMA_MEM_TO_DEV,
  76. DMA_DEV_TO_MEM,
  77. DMA_DEV_TO_DEV,
  78. DMA_TRANS_NONE,
  79. };
  80. /**
  81. * Interleaved Transfer Request
  82. * ----------------------------
  83. * A chunk is collection of contiguous bytes to be transfered.
  84. * The gap(in bytes) between two chunks is called inter-chunk-gap(ICG).
  85. * ICGs may or maynot change between chunks.
  86. * A FRAME is the smallest series of contiguous {chunk,icg} pairs,
  87. * that when repeated an integral number of times, specifies the transfer.
  88. * A transfer template is specification of a Frame, the number of times
  89. * it is to be repeated and other per-transfer attributes.
  90. *
  91. * Practically, a client driver would have ready a template for each
  92. * type of transfer it is going to need during its lifetime and
  93. * set only 'src_start' and 'dst_start' before submitting the requests.
  94. *
  95. *
  96. * | Frame-1 | Frame-2 | ~ | Frame-'numf' |
  97. * |====....==.===...=...|====....==.===...=...| ~ |====....==.===...=...|
  98. *
  99. * == Chunk size
  100. * ... ICG
  101. */
  102. /**
  103. * struct data_chunk - Element of scatter-gather list that makes a frame.
  104. * @size: Number of bytes to read from source.
  105. * size_dst := fn(op, size_src), so doesn't mean much for destination.
  106. * @icg: Number of bytes to jump after last src/dst address of this
  107. * chunk and before first src/dst address for next chunk.
  108. * Ignored for dst(assumed 0), if dst_inc is true and dst_sgl is false.
  109. * Ignored for src(assumed 0), if src_inc is true and src_sgl is false.
  110. * @dst_icg: Number of bytes to jump after last dst address of this
  111. * chunk and before the first dst address for next chunk.
  112. * Ignored if dst_inc is true and dst_sgl is false.
  113. * @src_icg: Number of bytes to jump after last src address of this
  114. * chunk and before the first src address for next chunk.
  115. * Ignored if src_inc is true and src_sgl is false.
  116. */
  117. struct data_chunk {
  118. size_t size;
  119. size_t icg;
  120. size_t dst_icg;
  121. size_t src_icg;
  122. };
  123. /**
  124. * struct dma_interleaved_template - Template to convey DMAC the transfer pattern
  125. * and attributes.
  126. * @src_start: Bus address of source for the first chunk.
  127. * @dst_start: Bus address of destination for the first chunk.
  128. * @dir: Specifies the type of Source and Destination.
  129. * @src_inc: If the source address increments after reading from it.
  130. * @dst_inc: If the destination address increments after writing to it.
  131. * @src_sgl: If the 'icg' of sgl[] applies to Source (scattered read).
  132. * Otherwise, source is read contiguously (icg ignored).
  133. * Ignored if src_inc is false.
  134. * @dst_sgl: If the 'icg' of sgl[] applies to Destination (scattered write).
  135. * Otherwise, destination is filled contiguously (icg ignored).
  136. * Ignored if dst_inc is false.
  137. * @numf: Number of frames in this template.
  138. * @frame_size: Number of chunks in a frame i.e, size of sgl[].
  139. * @sgl: Array of {chunk,icg} pairs that make up a frame.
  140. */
  141. struct dma_interleaved_template {
  142. dma_addr_t src_start;
  143. dma_addr_t dst_start;
  144. enum dma_transfer_direction dir;
  145. bool src_inc;
  146. bool dst_inc;
  147. bool src_sgl;
  148. bool dst_sgl;
  149. size_t numf;
  150. size_t frame_size;
  151. struct data_chunk sgl[0];
  152. };
  153. /**
  154. * enum dma_ctrl_flags - DMA flags to augment operation preparation,
  155. * control completion, and communicate status.
  156. * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
  157. * this transaction
  158. * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client
  159. * acknowledges receipt, i.e. has has a chance to establish any dependency
  160. * chains
  161. * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q
  162. * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P
  163. * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as
  164. * sources that were the result of a previous operation, in the case of a PQ
  165. * operation it continues the calculation with new sources
  166. * @DMA_PREP_FENCE - tell the driver that subsequent operations depend
  167. * on the result of this operation
  168. * @DMA_CTRL_REUSE: client can reuse the descriptor and submit again till
  169. * cleared or freed
  170. * @DMA_PREP_CMD: tell the driver that the data passed to DMA API is command
  171. * data and the descriptor should be in different format from normal
  172. * data descriptors.
  173. */
  174. enum dma_ctrl_flags {
  175. DMA_PREP_INTERRUPT = (1 << 0),
  176. DMA_CTRL_ACK = (1 << 1),
  177. DMA_PREP_PQ_DISABLE_P = (1 << 2),
  178. DMA_PREP_PQ_DISABLE_Q = (1 << 3),
  179. DMA_PREP_CONTINUE = (1 << 4),
  180. DMA_PREP_FENCE = (1 << 5),
  181. DMA_CTRL_REUSE = (1 << 6),
  182. DMA_PREP_CMD = (1 << 7),
  183. };
  184. /**
  185. * enum sum_check_bits - bit position of pq_check_flags
  186. */
  187. enum sum_check_bits {
  188. SUM_CHECK_P = 0,
  189. SUM_CHECK_Q = 1,
  190. };
  191. /**
  192. * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations
  193. * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
  194. * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
  195. */
  196. enum sum_check_flags {
  197. SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
  198. SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
  199. };
  200. /**
  201. * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
  202. * See linux/cpumask.h
  203. */
  204. typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
  205. /**
  206. * struct dma_chan_percpu - the per-CPU part of struct dma_chan
  207. * @memcpy_count: transaction counter
  208. * @bytes_transferred: byte counter
  209. */
  210. struct dma_chan_percpu {
  211. /* stats */
  212. unsigned long memcpy_count;
  213. unsigned long bytes_transferred;
  214. };
  215. /**
  216. * struct dma_router - DMA router structure
  217. * @dev: pointer to the DMA router device
  218. * @route_free: function to be called when the route can be disconnected
  219. */
  220. struct dma_router {
  221. struct device *dev;
  222. void (*route_free)(struct device *dev, void *route_data);
  223. };
  224. /**
  225. * struct dma_chan - devices supply DMA channels, clients use them
  226. * @device: ptr to the dma device who supplies this channel, always !%NULL
  227. * @cookie: last cookie value returned to client
  228. * @completed_cookie: last completed cookie for this channel
  229. * @chan_id: channel ID for sysfs
  230. * @dev: class device for sysfs
  231. * @device_node: used to add this to the device chan list
  232. * @local: per-cpu pointer to a struct dma_chan_percpu
  233. * @client_count: how many clients are using this channel
  234. * @table_count: number of appearances in the mem-to-mem allocation table
  235. * @router: pointer to the DMA router structure
  236. * @route_data: channel specific data for the router
  237. * @private: private data for certain client-channel associations
  238. */
  239. struct dma_chan {
  240. struct dma_device *device;
  241. dma_cookie_t cookie;
  242. dma_cookie_t completed_cookie;
  243. /* sysfs */
  244. int chan_id;
  245. struct dma_chan_dev *dev;
  246. ListItem_t device_node;
  247. struct dma_chan_percpu *local;
  248. int client_count;
  249. int table_count;
  250. /* DMA router */
  251. struct dma_router *router;
  252. void *route_data;
  253. void *private;
  254. };
  255. /**
  256. * struct dma_chan_dev - relate sysfs device node to backing channel device
  257. * @chan: driver channel device
  258. * @device: sysfs device
  259. * @dev_id: parent dma_device dev_id
  260. * @idr_ref: reference count to gate release of dma_device dev_id
  261. */
  262. struct dma_chan_dev {
  263. struct dma_chan *chan;
  264. struct device device;
  265. //int dev_id;
  266. //atomic_t *idr_ref;
  267. };
  268. /**
  269. * enum dma_slave_buswidth - defines bus width of the DMA slave
  270. * device, source or target buses
  271. */
  272. enum dma_slave_buswidth {
  273. DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
  274. DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
  275. DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
  276. DMA_SLAVE_BUSWIDTH_3_BYTES = 3,
  277. DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
  278. DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
  279. DMA_SLAVE_BUSWIDTH_16_BYTES = 16,
  280. DMA_SLAVE_BUSWIDTH_32_BYTES = 32,
  281. DMA_SLAVE_BUSWIDTH_64_BYTES = 64,
  282. };
  283. /**
  284. * struct dma_slave_config - dma slave channel runtime config
  285. * @direction: whether the data shall go in or out on this slave
  286. * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are
  287. * legal values. DEPRECATED, drivers should use the direction argument
  288. * to the device_prep_slave_sg and device_prep_dma_cyclic functions or
  289. * the dir field in the dma_interleaved_template structure.
  290. * @src_addr: this is the physical address where DMA slave data
  291. * should be read (RX), if the source is memory this argument is
  292. * ignored.
  293. * @dst_addr: this is the physical address where DMA slave data
  294. * should be written (TX), if the source is memory this argument
  295. * is ignored.
  296. * @src_addr_width: this is the width in bytes of the source (RX)
  297. * register where DMA data shall be read. If the source
  298. * is memory this may be ignored depending on architecture.
  299. * Legal values: 1, 2, 4, 8.
  300. * @dst_addr_width: same as src_addr_width but for destination
  301. * target (TX) mutatis mutandis.
  302. * @src_maxburst: the maximum number of words (note: words, as in
  303. * units of the src_addr_width member, not bytes) that can be sent
  304. * in one burst to the device. Typically something like half the
  305. * FIFO depth on I/O peripherals so you don't overflow it. This
  306. * may or may not be applicable on memory sources.
  307. * @dst_maxburst: same as src_maxburst but for destination target
  308. * mutatis mutandis.
  309. * @src_port_window_size: The length of the register area in words the data need
  310. * to be accessed on the device side. It is only used for devices which is using
  311. * an area instead of a single register to receive the data. Typically the DMA
  312. * loops in this area in order to transfer the data.
  313. * @dst_port_window_size: same as src_port_window_size but for the destination
  314. * port.
  315. * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill
  316. * with 'true' if peripheral should be flow controller. Direction will be
  317. * selected at Runtime.
  318. * @slave_id: Slave requester id. Only valid for slave channels. The dma
  319. * slave peripheral will have unique id as dma requester which need to be
  320. * pass as slave config.
  321. *
  322. * This struct is passed in as configuration data to a DMA engine
  323. * in order to set up a certain channel for DMA transport at runtime.
  324. * The DMA device/engine has to provide support for an additional
  325. * callback in the dma_device structure, device_config and this struct
  326. * will then be passed in as an argument to the function.
  327. *
  328. * The rationale for adding configuration information to this struct is as
  329. * follows: if it is likely that more than one DMA slave controllers in
  330. * the world will support the configuration option, then make it generic.
  331. * If not: if it is fixed so that it be sent in static from the platform
  332. * data, then prefer to do that.
  333. */
  334. struct dma_slave_config {
  335. enum dma_transfer_direction direction;
  336. phys_addr_t src_addr;
  337. phys_addr_t dst_addr;
  338. enum dma_slave_buswidth src_addr_width;
  339. enum dma_slave_buswidth dst_addr_width;
  340. u32 src_maxburst;
  341. u32 dst_maxburst;
  342. u32 src_port_window_size;
  343. u32 dst_port_window_size;
  344. bool device_fc;
  345. unsigned int slave_id;
  346. };
  347. /**
  348. * enum dma_residue_granularity - Granularity of the reported transfer residue
  349. * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The
  350. * DMA channel is only able to tell whether a descriptor has been completed or
  351. * not, which means residue reporting is not supported by this channel. The
  352. * residue field of the dma_tx_state field will always be 0.
  353. * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully
  354. * completed segment of the transfer (For cyclic transfers this is after each
  355. * period). This is typically implemented by having the hardware generate an
  356. * interrupt after each transferred segment and then the drivers updates the
  357. * outstanding residue by the size of the segment. Another possibility is if
  358. * the hardware supports scatter-gather and the segment descriptor has a field
  359. * which gets set after the segment has been completed. The driver then counts
  360. * the number of segments without the flag set to compute the residue.
  361. * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred
  362. * burst. This is typically only supported if the hardware has a progress
  363. * register of some sort (E.g. a register with the current read/write address
  364. * or a register with the amount of bursts/beats/bytes that have been
  365. * transferred or still need to be transferred).
  366. */
  367. enum dma_residue_granularity {
  368. DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0,
  369. DMA_RESIDUE_GRANULARITY_SEGMENT = 1,
  370. DMA_RESIDUE_GRANULARITY_BURST = 2,
  371. };
  372. /* struct dma_slave_caps - expose capabilities of a slave channel only
  373. *
  374. * @src_addr_widths: bit mask of src addr widths the channel supports
  375. * @dst_addr_widths: bit mask of dstn addr widths the channel supports
  376. * @directions: bit mask of slave direction the channel supported
  377. * since the enum dma_transfer_direction is not defined as bits for each
  378. * type of direction, the dma controller should fill (1 << <TYPE>) and same
  379. * should be checked by controller as well
  380. * @max_burst: max burst capability per-transfer
  381. * @cmd_pause: true, if pause and thereby resume is supported
  382. * @cmd_terminate: true, if terminate cmd is supported
  383. * @residue_granularity: granularity of the reported transfer residue
  384. * @descriptor_reuse: if a descriptor can be reused by client and
  385. * resubmitted multiple times
  386. */
  387. struct dma_slave_caps {
  388. u32 src_addr_widths;
  389. u32 dst_addr_widths;
  390. u32 directions;
  391. u32 max_burst;
  392. bool cmd_pause;
  393. bool cmd_terminate;
  394. enum dma_residue_granularity residue_granularity;
  395. bool descriptor_reuse;
  396. };
  397. void dma_chan_cleanup(struct kref *kref);
  398. /**
  399. * typedef dma_filter_fn - callback filter for dma_request_channel
  400. * @chan: channel to be reviewed
  401. * @filter_param: opaque parameter passed through dma_request_channel
  402. *
  403. * When this optional parameter is specified in a call to dma_request_channel a
  404. * suitable channel is passed to this routine for further dispositioning before
  405. * being returned. Where 'suitable' indicates a non-busy channel that
  406. * satisfies the given capability mask. It returns 'true' to indicate that the
  407. * channel is suitable.
  408. */
  409. typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
  410. typedef void (*dma_async_tx_callback)(void *dma_async_param);
  411. enum dmaengine_tx_result {
  412. DMA_TRANS_NOERROR = 0, /* SUCCESS */
  413. DMA_TRANS_READ_FAILED, /* Source DMA read failed */
  414. DMA_TRANS_WRITE_FAILED, /* Destination DMA write failed */
  415. DMA_TRANS_ABORTED, /* Op never submitted / aborted */
  416. };
  417. struct dmaengine_result {
  418. enum dmaengine_tx_result result;
  419. u32 residue;
  420. };
  421. typedef void (*dma_async_tx_callback_result)(void *dma_async_param,
  422. const struct dmaengine_result *result);
  423. struct dmaengine_unmap_data {
  424. u8 map_cnt;
  425. u8 to_cnt;
  426. u8 from_cnt;
  427. u8 bidi_cnt;
  428. struct device *dev;
  429. struct kref kref;
  430. size_t len;
  431. dma_addr_t addr[0];
  432. };
  433. /**
  434. * struct dma_async_tx_descriptor - async transaction descriptor
  435. * ---dma generic offload fields---
  436. * @cookie: tracking cookie for this transaction, set to -EBUSY if
  437. * this tx is sitting on a dependency list
  438. * @flags: flags to augment operation preparation, control completion, and
  439. * communicate status
  440. * @phys: physical address of the descriptor
  441. * @chan: target channel for this operation
  442. * @tx_submit: accept the descriptor, assign ordered cookie and mark the
  443. * descriptor pending. To be pushed on .issue_pending() call
  444. * @callback: routine to call after this operation is complete
  445. * @callback_param: general parameter to pass to the callback routine
  446. * ---async_tx api specific fields---
  447. * @next: at completion submit this descriptor
  448. * @parent: pointer to the next level up in the dependency chain
  449. * @lock: protect the parent and next pointers
  450. */
  451. struct dma_async_tx_descriptor {
  452. dma_cookie_t cookie;
  453. enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
  454. dma_addr_t phys;
  455. struct dma_chan *chan;
  456. dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
  457. int (*desc_free)(struct dma_async_tx_descriptor *tx);
  458. dma_async_tx_callback callback;
  459. dma_async_tx_callback_result callback_result;
  460. void *callback_param;
  461. struct dmaengine_unmap_data *unmap;
  462. #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  463. struct dma_async_tx_descriptor *next;
  464. struct dma_async_tx_descriptor *parent;
  465. spinlock_t lock;
  466. #endif
  467. };
  468. #ifdef CONFIG_DMA_ENGINE
  469. static __INLINE void dma_set_unmap(struct dma_async_tx_descriptor *tx,
  470. struct dmaengine_unmap_data *unmap)
  471. {
  472. kref_get(&unmap->kref);
  473. tx->unmap = unmap;
  474. }
  475. struct dmaengine_unmap_data *
  476. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags);
  477. void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap);
  478. #else
  479. static __INLINE void dma_set_unmap(struct dma_async_tx_descriptor *tx,
  480. struct dmaengine_unmap_data *unmap)
  481. {
  482. }
  483. static __INLINE struct dmaengine_unmap_data *
  484. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
  485. {
  486. return NULL;
  487. }
  488. static __INLINE void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
  489. {
  490. }
  491. #endif
  492. static __INLINE void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
  493. {
  494. if (tx->unmap) {
  495. dmaengine_unmap_put(tx->unmap);
  496. tx->unmap = NULL;
  497. }
  498. }
  499. #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  500. static __INLINE void txd_lock(struct dma_async_tx_descriptor *txd)
  501. {
  502. }
  503. static __INLINE void txd_unlock(struct dma_async_tx_descriptor *txd)
  504. {
  505. }
  506. static __INLINE void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
  507. {
  508. return;
  509. }
  510. static __INLINE void txd_clear_parent(struct dma_async_tx_descriptor *txd)
  511. {
  512. }
  513. static __INLINE void txd_clear_next(struct dma_async_tx_descriptor *txd)
  514. {
  515. }
  516. static __INLINE struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
  517. {
  518. return NULL;
  519. }
  520. static __INLINE struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
  521. {
  522. return NULL;
  523. }
  524. #else
  525. static __INLINE void txd_lock(struct dma_async_tx_descriptor *txd)
  526. {
  527. spin_lock_bh(&txd->lock);
  528. }
  529. static __INLINE void txd_unlock(struct dma_async_tx_descriptor *txd)
  530. {
  531. spin_unlock_bh(&txd->lock);
  532. }
  533. static __INLINE void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
  534. {
  535. txd->next = next;
  536. next->parent = txd;
  537. }
  538. static __INLINE void txd_clear_parent(struct dma_async_tx_descriptor *txd)
  539. {
  540. txd->parent = NULL;
  541. }
  542. static __INLINE void txd_clear_next(struct dma_async_tx_descriptor *txd)
  543. {
  544. txd->next = NULL;
  545. }
  546. static __INLINE struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
  547. {
  548. return txd->parent;
  549. }
  550. static __INLINE struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
  551. {
  552. return txd->next;
  553. }
  554. #endif
  555. /**
  556. * struct dma_tx_state - filled in to report the status of
  557. * a transfer.
  558. * @last: last completed DMA cookie
  559. * @used: last issued DMA cookie (i.e. the one in progress)
  560. * @residue: the remaining number of bytes left to transmit
  561. * on the selected transfer for states DMA_IN_PROGRESS and
  562. * DMA_PAUSED if this is implemented in the driver, else 0
  563. */
  564. struct dma_tx_state {
  565. dma_cookie_t last;
  566. dma_cookie_t used;
  567. u32 residue;
  568. };
  569. /**
  570. * enum dmaengine_alignment - defines alignment of the DMA async tx
  571. * buffers
  572. */
  573. enum dmaengine_alignment {
  574. DMAENGINE_ALIGN_1_BYTE = 0,
  575. DMAENGINE_ALIGN_2_BYTES = 1,
  576. DMAENGINE_ALIGN_4_BYTES = 2,
  577. DMAENGINE_ALIGN_8_BYTES = 3,
  578. DMAENGINE_ALIGN_16_BYTES = 4,
  579. DMAENGINE_ALIGN_32_BYTES = 5,
  580. DMAENGINE_ALIGN_64_BYTES = 6,
  581. };
  582. /**
  583. * struct dma_slave_map - associates slave device and it's slave channel with
  584. * parameter to be used by a filter function
  585. * @devname: name of the device
  586. * @slave: slave channel name
  587. * @param: opaque parameter to pass to struct dma_filter.fn
  588. */
  589. struct dma_slave_map {
  590. const char *devname;
  591. const char *slave;
  592. void *param;
  593. };
  594. /**
  595. * struct dma_filter - information for slave device/channel to filter_fn/param
  596. * mapping
  597. * @fn: filter function callback
  598. * @mapcnt: number of slave device/channel in the map
  599. * @map: array of channel to filter mapping data
  600. */
  601. struct dma_filter {
  602. dma_filter_fn fn;
  603. int mapcnt;
  604. const struct dma_slave_map *map;
  605. };
  606. /**
  607. * struct dma_device - info on the entity supplying DMA services
  608. * @chancnt: how many DMA channels are supported
  609. * @privatecnt: how many DMA channels are requested by dma_request_channel
  610. * @channels: the list of struct dma_chan
  611. * @global_node: list_head for global dma_device_list
  612. * @filter: information for device/slave to filter function/param mapping
  613. * @cap_mask: one or more dma_capability flags
  614. * @max_xor: maximum number of xor sources, 0 if no capability
  615. * @max_pq: maximum number of PQ sources and PQ-continue capability
  616. * @copy_align: alignment shift for memcpy operations
  617. * @xor_align: alignment shift for xor operations
  618. * @pq_align: alignment shift for pq operations
  619. * @fill_align: alignment shift for memset operations
  620. * @dev_id: unique device ID
  621. * @dev: struct device reference for dma mapping api
  622. * @src_addr_widths: bit mask of src addr widths the device supports
  623. * @dst_addr_widths: bit mask of dst addr widths the device supports
  624. * @directions: bit mask of slave direction the device supports since
  625. * the enum dma_transfer_direction is not defined as bits for
  626. * each type of direction, the dma controller should fill (1 <<
  627. * <TYPE>) and same should be checked by controller as well
  628. * @max_burst: max burst capability per-transfer
  629. * @residue_granularity: granularity of the transfer residue reported
  630. * by tx_status
  631. * @device_alloc_chan_resources: allocate resources and return the
  632. * number of allocated descriptors
  633. * @device_free_chan_resources: release DMA channel's resources
  634. * @device_prep_dma_memcpy: prepares a memcpy operation
  635. * @device_prep_dma_xor: prepares a xor operation
  636. * @device_prep_dma_xor_val: prepares a xor validation operation
  637. * @device_prep_dma_pq: prepares a pq operation
  638. * @device_prep_dma_pq_val: prepares a pqzero_sum operation
  639. * @device_prep_dma_memset: prepares a memset operation
  640. * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list
  641. * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
  642. * @device_prep_slave_sg: prepares a slave dma operation
  643. * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
  644. * The function takes a buffer of size buf_len. The callback function will
  645. * be called after period_len bytes have been transferred.
  646. * @device_prep_interleaved_dma: Transfer expression in a generic way.
  647. * @device_prep_dma_imm_data: DMA's 8 byte immediate data to the dst address
  648. * @device_config: Pushes a new configuration to a channel, return 0 or an error
  649. * code
  650. * @device_pause: Pauses any transfer happening on a channel. Returns
  651. * 0 or an error code
  652. * @device_resume: Resumes any transfer on a channel previously
  653. * paused. Returns 0 or an error code
  654. * @device_terminate_all: Aborts all transfers on a channel. Returns 0
  655. * or an error code
  656. * @device_synchronize: Synchronizes the termination of a transfers to the
  657. * current context.
  658. * @device_tx_status: poll for transaction completion, the optional
  659. * txstate parameter can be supplied with a pointer to get a
  660. * struct with auxiliary transfer status information, otherwise the call
  661. * will just return a simple status code
  662. * @device_issue_pending: push pending transactions to hardware
  663. * @descriptor_reuse: a submitted transfer can be resubmitted after completion
  664. */
  665. struct dma_device {
  666. unsigned int chancnt;
  667. unsigned int privatecnt;
  668. List_t channels;
  669. List_t global_node;
  670. struct dma_filter filter;
  671. dma_cap_mask_t cap_mask;
  672. unsigned short max_xor;
  673. unsigned short max_pq;
  674. enum dmaengine_alignment copy_align;
  675. enum dmaengine_alignment xor_align;
  676. enum dmaengine_alignment pq_align;
  677. enum dmaengine_alignment fill_align;
  678. #define DMA_HAS_PQ_CONTINUE (1 << 15)
  679. int dev_id;
  680. struct device *dev;
  681. u32 src_addr_widths;
  682. u32 dst_addr_widths;
  683. u32 directions;
  684. u32 max_burst;
  685. bool descriptor_reuse;
  686. enum dma_residue_granularity residue_granularity;
  687. int (*device_alloc_chan_resources)(struct dma_chan *chan);
  688. void (*device_free_chan_resources)(struct dma_chan *chan);
  689. struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
  690. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  691. size_t len, unsigned long flags);
  692. struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
  693. struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  694. unsigned int src_cnt, size_t len, unsigned long flags);
  695. struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
  696. struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
  697. size_t len, enum sum_check_flags *result, unsigned long flags);
  698. struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
  699. struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  700. unsigned int src_cnt, const unsigned char *scf,
  701. size_t len, unsigned long flags);
  702. struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
  703. struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  704. unsigned int src_cnt, const unsigned char *scf, size_t len,
  705. enum sum_check_flags *pqres, unsigned long flags);
  706. struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
  707. struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
  708. unsigned long flags);
  709. struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)(
  710. struct dma_chan *chan, struct scatterlist *sg,
  711. unsigned int nents, int value, unsigned long flags);
  712. struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
  713. struct dma_chan *chan, unsigned long flags);
  714. struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
  715. struct dma_chan *chan, struct scatterlist *sgl,
  716. unsigned int sg_len, enum dma_transfer_direction direction,
  717. unsigned long flags, void *context);
  718. struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
  719. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  720. size_t period_len, enum dma_transfer_direction direction,
  721. unsigned long flags);
  722. struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
  723. struct dma_chan *chan, struct dma_interleaved_template *xt,
  724. unsigned long flags);
  725. struct dma_async_tx_descriptor *(*device_prep_dma_imm_data)(
  726. struct dma_chan *chan, dma_addr_t dst, u64 data,
  727. unsigned long flags);
  728. int (*device_config)(struct dma_chan *chan,
  729. struct dma_slave_config *config);
  730. int (*device_pause)(struct dma_chan *chan);
  731. int (*device_resume)(struct dma_chan *chan);
  732. int (*device_terminate_all)(struct dma_chan *chan);
  733. void (*device_synchronize)(struct dma_chan *chan);
  734. enum dma_status (*device_tx_status)(struct dma_chan *chan,
  735. dma_cookie_t cookie,
  736. struct dma_tx_state *txstate);
  737. void (*device_issue_pending)(struct dma_chan *chan);
  738. };
  739. static __INLINE int dmaengine_slave_config(struct dma_chan *chan,
  740. struct dma_slave_config *config)
  741. {
  742. if (chan->device->device_config)
  743. return chan->device->device_config(chan, config);
  744. return -ENOSYS;
  745. }
  746. static __INLINE bool is_slave_direction(enum dma_transfer_direction direction)
  747. {
  748. return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM);
  749. }
  750. static __INLINE struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
  751. struct dma_chan *chan, dma_addr_t buf, size_t len,
  752. enum dma_transfer_direction dir, unsigned long flags)
  753. {
  754. struct scatterlist sg;
  755. sg_init_table(&sg, 1);
  756. sg_dma_address(&sg) = buf;
  757. sg_dma_len(&sg) = len;
  758. if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
  759. return NULL;
  760. return chan->device->device_prep_slave_sg(chan, &sg, 1,
  761. dir, flags, NULL);
  762. }
  763. static __INLINE struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
  764. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  765. enum dma_transfer_direction dir, unsigned long flags)
  766. {
  767. if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
  768. return NULL;
  769. return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
  770. dir, flags, NULL);
  771. }
  772. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  773. struct rio_dma_ext;
  774. static __INLINE struct dma_async_tx_descriptor *dmaengine_prep_rio_sg(
  775. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  776. enum dma_transfer_direction dir, unsigned long flags,
  777. struct rio_dma_ext *rio_ext)
  778. {
  779. if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
  780. return NULL;
  781. return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
  782. dir, flags, rio_ext);
  783. }
  784. #endif
  785. static __INLINE struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
  786. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  787. size_t period_len, enum dma_transfer_direction dir,
  788. unsigned long flags)
  789. {
  790. if (!chan || !chan->device || !chan->device->device_prep_dma_cyclic)
  791. return NULL;
  792. return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len,
  793. period_len, dir, flags);
  794. }
  795. static __INLINE struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
  796. struct dma_chan *chan, struct dma_interleaved_template *xt,
  797. unsigned long flags)
  798. {
  799. if (!chan || !chan->device || !chan->device->device_prep_interleaved_dma)
  800. return NULL;
  801. return chan->device->device_prep_interleaved_dma(chan, xt, flags);
  802. }
  803. static __INLINE struct dma_async_tx_descriptor *dmaengine_prep_dma_memset(
  804. struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
  805. unsigned long flags)
  806. {
  807. if (!chan || !chan->device || !chan->device->device_prep_dma_memset)
  808. return NULL;
  809. return chan->device->device_prep_dma_memset(chan, dest, value,
  810. len, flags);
  811. }
  812. static __INLINE struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy(
  813. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  814. size_t len, unsigned long flags)
  815. {
  816. if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy)
  817. return NULL;
  818. return chan->device->device_prep_dma_memcpy(chan, dest, src,
  819. len, flags);
  820. }
  821. /**
  822. * dmaengine_terminate_all() - Terminate all active DMA transfers
  823. * @chan: The channel for which to terminate the transfers
  824. *
  825. * This function is DEPRECATED use either dmaengine_terminate_sync() or
  826. * dmaengine_terminate_async() instead.
  827. */
  828. static __INLINE int dmaengine_terminate_all(struct dma_chan *chan)
  829. {
  830. if (chan->device->device_terminate_all)
  831. return chan->device->device_terminate_all(chan);
  832. return -ENOSYS;
  833. }
  834. /**
  835. * dmaengine_terminate_async() - Terminate all active DMA transfers
  836. * @chan: The channel for which to terminate the transfers
  837. *
  838. * Calling this function will terminate all active and pending descriptors
  839. * that have previously been submitted to the channel. It is not guaranteed
  840. * though that the transfer for the active descriptor has stopped when the
  841. * function returns. Furthermore it is possible the complete callback of a
  842. * submitted transfer is still running when this function returns.
  843. *
  844. * dmaengine_synchronize() needs to be called before it is safe to free
  845. * any memory that is accessed by previously submitted descriptors or before
  846. * freeing any resources accessed from within the completion callback of any
  847. * perviously submitted descriptors.
  848. *
  849. * This function can be called from atomic context as well as from within a
  850. * complete callback of a descriptor submitted on the same channel.
  851. *
  852. * If none of the two conditions above apply consider using
  853. * dmaengine_terminate_sync() instead.
  854. */
  855. static __INLINE int dmaengine_terminate_async(struct dma_chan *chan)
  856. {
  857. if (chan->device->device_terminate_all)
  858. return chan->device->device_terminate_all(chan);
  859. return -EINVAL;
  860. }
  861. /**
  862. * dmaengine_synchronize() - Synchronize DMA channel termination
  863. * @chan: The channel to synchronize
  864. *
  865. * Synchronizes to the DMA channel termination to the current context. When this
  866. * function returns it is guaranteed that all transfers for previously issued
  867. * descriptors have stopped and and it is safe to free the memory assoicated
  868. * with them. Furthermore it is guaranteed that all complete callback functions
  869. * for a previously submitted descriptor have finished running and it is safe to
  870. * free resources accessed from within the complete callbacks.
  871. *
  872. * The behavior of this function is undefined if dma_async_issue_pending() has
  873. * been called between dmaengine_terminate_async() and this function.
  874. *
  875. * This function must only be called from non-atomic context and must not be
  876. * called from within a complete callback of a descriptor submitted on the same
  877. * channel.
  878. */
  879. static __INLINE void dmaengine_synchronize(struct dma_chan *chan)
  880. {
  881. //might_sleep();
  882. if (chan->device->device_synchronize)
  883. chan->device->device_synchronize(chan);
  884. }
  885. /**
  886. * dmaengine_terminate_sync() - Terminate all active DMA transfers
  887. * @chan: The channel for which to terminate the transfers
  888. *
  889. * Calling this function will terminate all active and pending transfers
  890. * that have previously been submitted to the channel. It is similar to
  891. * dmaengine_terminate_async() but guarantees that the DMA transfer has actually
  892. * stopped and that all complete callbacks have finished running when the
  893. * function returns.
  894. *
  895. * This function must only be called from non-atomic context and must not be
  896. * called from within a complete callback of a descriptor submitted on the same
  897. * channel.
  898. */
  899. static __INLINE int dmaengine_terminate_sync(struct dma_chan *chan)
  900. {
  901. int ret;
  902. ret = dmaengine_terminate_async(chan);
  903. if (ret)
  904. return ret;
  905. dmaengine_synchronize(chan);
  906. return 0;
  907. }
  908. static __INLINE int dmaengine_pause(struct dma_chan *chan)
  909. {
  910. if (chan->device->device_pause)
  911. return chan->device->device_pause(chan);
  912. return -ENOSYS;
  913. }
  914. static __INLINE int dmaengine_resume(struct dma_chan *chan)
  915. {
  916. if (chan->device->device_resume)
  917. return chan->device->device_resume(chan);
  918. return -ENOSYS;
  919. }
  920. static __INLINE enum dma_status dmaengine_tx_status(struct dma_chan *chan,
  921. dma_cookie_t cookie, struct dma_tx_state *state)
  922. {
  923. return chan->device->device_tx_status(chan, cookie, state);
  924. }
  925. static __INLINE dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
  926. {
  927. return desc->tx_submit(desc);
  928. }
  929. static __INLINE bool dmaengine_check_align(enum dmaengine_alignment align,
  930. size_t off1, size_t off2, size_t len)
  931. {
  932. size_t mask;
  933. if (!align)
  934. return true;
  935. mask = (1 << align) - 1;
  936. if (mask & (off1 | off2 | len))
  937. return false;
  938. return true;
  939. }
  940. static __INLINE bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
  941. size_t off2, size_t len)
  942. {
  943. return dmaengine_check_align(dev->copy_align, off1, off2, len);
  944. }
  945. static __INLINE bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
  946. size_t off2, size_t len)
  947. {
  948. return dmaengine_check_align(dev->xor_align, off1, off2, len);
  949. }
  950. static __INLINE bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
  951. size_t off2, size_t len)
  952. {
  953. return dmaengine_check_align(dev->pq_align, off1, off2, len);
  954. }
  955. static __INLINE bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
  956. size_t off2, size_t len)
  957. {
  958. return dmaengine_check_align(dev->fill_align, off1, off2, len);
  959. }
  960. static __INLINE void
  961. dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
  962. {
  963. dma->max_pq = maxpq;
  964. if (has_pq_continue)
  965. dma->max_pq |= DMA_HAS_PQ_CONTINUE;
  966. }
  967. static __INLINE bool dmaf_continue(enum dma_ctrl_flags flags)
  968. {
  969. return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
  970. }
  971. static __INLINE bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
  972. {
  973. enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
  974. return (flags & mask) == mask;
  975. }
  976. static __INLINE bool dma_dev_has_pq_continue(struct dma_device *dma)
  977. {
  978. return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
  979. }
  980. static __INLINE unsigned short dma_dev_to_maxpq(struct dma_device *dma)
  981. {
  982. return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
  983. }
  984. /* dma_maxpq - reduce maxpq in the face of continued operations
  985. * @dma - dma device with PQ capability
  986. * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
  987. *
  988. * When an engine does not support native continuation we need 3 extra
  989. * source slots to reuse P and Q with the following coefficients:
  990. * 1/ {00} * P : remove P from Q', but use it as a source for P'
  991. * 2/ {01} * Q : use Q to continue Q' calculation
  992. * 3/ {00} * Q : subtract Q from P' to cancel (2)
  993. *
  994. * In the case where P is disabled we only need 1 extra source:
  995. * 1/ {01} * Q : use Q to continue Q' calculation
  996. */
  997. static __INLINE int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
  998. {
  999. if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
  1000. return dma_dev_to_maxpq(dma);
  1001. else if (dmaf_p_disabled_continue(flags))
  1002. return dma_dev_to_maxpq(dma) - 1;
  1003. else if (dmaf_continue(flags))
  1004. return dma_dev_to_maxpq(dma) - 3;
  1005. BUG();
  1006. return -1;
  1007. }
  1008. static __INLINE size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
  1009. size_t dir_icg)
  1010. {
  1011. if (inc) {
  1012. if (dir_icg)
  1013. return dir_icg;
  1014. else if (sgl)
  1015. return icg;
  1016. }
  1017. return 0;
  1018. }
  1019. static __INLINE size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt,
  1020. struct data_chunk *chunk)
  1021. {
  1022. return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl,
  1023. chunk->icg, chunk->dst_icg);
  1024. }
  1025. static __INLINE size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt,
  1026. struct data_chunk *chunk)
  1027. {
  1028. return dmaengine_get_icg(xt->src_inc, xt->src_sgl,
  1029. chunk->icg, chunk->src_icg);
  1030. }
  1031. /* --- public DMA engine API --- */
  1032. #ifdef CONFIG_DMA_ENGINE
  1033. void dmaengine_get(void);
  1034. void dmaengine_put(void);
  1035. #else
  1036. static __INLINE void dmaengine_get(void)
  1037. {
  1038. }
  1039. static __INLINE void dmaengine_put(void)
  1040. {
  1041. }
  1042. #endif
  1043. #ifdef CONFIG_ASYNC_TX_DMA
  1044. #define async_dmaengine_get() dmaengine_get()
  1045. #define async_dmaengine_put() dmaengine_put()
  1046. #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  1047. #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
  1048. #else
  1049. #define async_dma_find_channel(type) dma_find_channel(type)
  1050. #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
  1051. #else
  1052. static __INLINE void async_dmaengine_get(void)
  1053. {
  1054. }
  1055. static __INLINE void async_dmaengine_put(void)
  1056. {
  1057. }
  1058. static __INLINE struct dma_chan *
  1059. async_dma_find_channel(enum dma_transaction_type type)
  1060. {
  1061. return NULL;
  1062. }
  1063. #endif /* CONFIG_ASYNC_TX_DMA */
  1064. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  1065. struct dma_chan *chan);
  1066. static __INLINE void async_tx_ack(struct dma_async_tx_descriptor *tx)
  1067. {
  1068. tx->flags |= DMA_CTRL_ACK;
  1069. }
  1070. static __INLINE void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
  1071. {
  1072. tx->flags &= ~DMA_CTRL_ACK;
  1073. }
  1074. static __INLINE bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
  1075. {
  1076. return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
  1077. }
  1078. #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
  1079. static __INLINE void
  1080. __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  1081. {
  1082. set_bit(tx_type, dstp->bits);
  1083. }
  1084. #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
  1085. static __INLINE void
  1086. __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  1087. {
  1088. clear_bit(tx_type, dstp->bits);
  1089. }
  1090. #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
  1091. static __INLINE void __dma_cap_zero(dma_cap_mask_t *dstp)
  1092. {
  1093. bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
  1094. }
  1095. #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
  1096. static __INLINE int
  1097. __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
  1098. {
  1099. return test_bit(tx_type, srcp->bits);
  1100. }
  1101. #define for_each_dma_cap_mask(cap, mask) \
  1102. for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END)
  1103. /**
  1104. * dma_async_issue_pending - flush pending transactions to HW
  1105. * @chan: target DMA channel
  1106. *
  1107. * This allows drivers to push copies to HW in batches,
  1108. * reducing MMIO writes where possible.
  1109. */
  1110. static __INLINE void dma_async_issue_pending(struct dma_chan *chan)
  1111. {
  1112. chan->device->device_issue_pending(chan);
  1113. }
  1114. /**
  1115. * dma_async_is_tx_complete - poll for transaction completion
  1116. * @chan: DMA channel
  1117. * @cookie: transaction identifier to check status of
  1118. * @last: returns last completed cookie, can be NULL
  1119. * @used: returns last issued cookie, can be NULL
  1120. *
  1121. * If @last and @used are passed in, upon return they reflect the driver
  1122. * internal state and can be used with dma_async_is_complete() to check
  1123. * the status of multiple cookies without re-checking hardware state.
  1124. */
  1125. static __INLINE enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
  1126. dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
  1127. {
  1128. struct dma_tx_state state;
  1129. enum dma_status status;
  1130. status = chan->device->device_tx_status(chan, cookie, &state);
  1131. if (last)
  1132. *last = state.last;
  1133. if (used)
  1134. *used = state.used;
  1135. return status;
  1136. }
  1137. /**
  1138. * dma_async_is_complete - test a cookie against chan state
  1139. * @cookie: transaction identifier to test status of
  1140. * @last_complete: last know completed transaction
  1141. * @last_used: last cookie value handed out
  1142. *
  1143. * dma_async_is_complete() is used in dma_async_is_tx_complete()
  1144. * the test logic is separated for lightweight testing of multiple cookies
  1145. */
  1146. static __INLINE enum dma_status dma_async_is_complete(dma_cookie_t cookie,
  1147. dma_cookie_t last_complete, dma_cookie_t last_used)
  1148. {
  1149. if (last_complete <= last_used) {
  1150. if ((cookie <= last_complete) || (cookie > last_used))
  1151. return DMA_COMPLETE;
  1152. } else {
  1153. if ((cookie <= last_complete) && (cookie > last_used))
  1154. return DMA_COMPLETE;
  1155. }
  1156. return DMA_IN_PROGRESS;
  1157. }
  1158. static __INLINE void
  1159. dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
  1160. {
  1161. if (st) {
  1162. st->last = last;
  1163. st->used = used;
  1164. st->residue = residue;
  1165. }
  1166. }
  1167. #ifdef CONFIG_DMA_ENGINE
  1168. struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
  1169. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
  1170. enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
  1171. void dma_issue_pending_all(void);
  1172. struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  1173. dma_filter_fn fn, void *fn_param);
  1174. struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name);
  1175. struct dma_chan *dma_request_chan(struct device *dev, const char *name);
  1176. struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
  1177. void dma_release_channel(struct dma_chan *chan);
  1178. int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
  1179. #else
  1180. static __INLINE struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
  1181. {
  1182. return NULL;
  1183. }
  1184. static __INLINE enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  1185. {
  1186. return DMA_COMPLETE;
  1187. }
  1188. static __INLINE enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
  1189. {
  1190. return DMA_COMPLETE;
  1191. }
  1192. static __INLINE void dma_issue_pending_all(void)
  1193. {
  1194. }
  1195. static __INLINE struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  1196. dma_filter_fn fn, void *fn_param)
  1197. {
  1198. return NULL;
  1199. }
  1200. static __INLINE struct dma_chan *dma_request_slave_channel(struct device *dev,
  1201. const char *name)
  1202. {
  1203. return NULL;
  1204. }
  1205. static __INLINE struct dma_chan *dma_request_chan(struct device *dev,
  1206. const char *name)
  1207. {
  1208. return ERR_PTR(-ENODEV);
  1209. }
  1210. static __INLINE struct dma_chan *dma_request_chan_by_mask(
  1211. const dma_cap_mask_t *mask)
  1212. {
  1213. return ERR_PTR(-ENODEV);
  1214. }
  1215. static __INLINE int dma_get_slave_caps(struct dma_chan *chan,
  1216. struct dma_slave_caps *caps)
  1217. {
  1218. return -ENXIO;
  1219. }
  1220. #endif
  1221. #define dma_request_slave_channel_reason(dev, name) dma_request_chan(dev, name)
  1222. static __INLINE int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
  1223. {
  1224. struct dma_slave_caps caps;
  1225. dma_get_slave_caps(tx->chan, &caps);
  1226. if (caps.descriptor_reuse) {
  1227. tx->flags |= DMA_CTRL_REUSE;
  1228. return 0;
  1229. } else {
  1230. return -EPERM;
  1231. }
  1232. }
  1233. static __INLINE void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
  1234. {
  1235. tx->flags &= ~DMA_CTRL_REUSE;
  1236. }
  1237. static __INLINE bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
  1238. {
  1239. return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE;
  1240. }
  1241. static __INLINE int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
  1242. {
  1243. /* this is supported for reusable desc, so check that */
  1244. if (dmaengine_desc_test_reuse(desc))
  1245. return desc->desc_free(desc);
  1246. else
  1247. return -EPERM;
  1248. }
  1249. /* --- DMA device --- */
  1250. int dma_async_device_register(struct dma_device *device);
  1251. int dmaenginem_async_device_register(struct dma_device *device);
  1252. void dma_async_device_unregister(struct dma_device *device);
  1253. void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
  1254. struct dma_chan *dma_get_slave_channel(struct dma_chan *chan);
  1255. struct dma_chan *dma_get_any_slave_channel(struct dma_device *device);
  1256. #define dma_request_slave_channel_compat(mask, x, y, dev, name) \
  1257. __dma_request_slave_channel_compat(&(mask), x, y, dev, name)
  1258. static __INLINE struct dma_chan
  1259. *__dma_request_slave_channel_compat(const dma_cap_mask_t *mask,
  1260. dma_filter_fn fn, void *fn_param,
  1261. struct device *dev, const char *name)
  1262. {
  1263. struct dma_chan *chan;
  1264. chan = dma_request_slave_channel(dev, name);
  1265. if (chan)
  1266. return chan;
  1267. if (!fn || !fn_param)
  1268. return NULL;
  1269. return __dma_request_channel(mask, fn, fn_param);
  1270. }
  1271. /**
  1272. * dma_cookie_init - initialize the cookies for a DMA channel
  1273. * @chan: dma channel to initialize
  1274. */
  1275. static __INLINE void dma_cookie_init(struct dma_chan *chan)
  1276. {
  1277. chan->cookie = DMA_MIN_COOKIE;
  1278. chan->completed_cookie = DMA_MIN_COOKIE;
  1279. }
  1280. /**
  1281. * dma_cookie_assign - assign a DMA engine cookie to the descriptor
  1282. * @tx: descriptor needing cookie
  1283. *
  1284. * Assign a unique non-zero per-channel cookie to the descriptor.
  1285. * Note: caller is expected to hold a lock to prevent concurrency.
  1286. */
  1287. static __INLINE dma_cookie_t dma_cookie_assign(struct dma_async_tx_descriptor *tx)
  1288. {
  1289. struct dma_chan *chan = tx->chan;
  1290. dma_cookie_t cookie;
  1291. cookie = chan->cookie + 1;
  1292. if (cookie < DMA_MIN_COOKIE)
  1293. cookie = DMA_MIN_COOKIE;
  1294. tx->cookie = chan->cookie = cookie;
  1295. return cookie;
  1296. }
  1297. /**
  1298. * dma_cookie_complete - complete a descriptor
  1299. * @tx: descriptor to complete
  1300. *
  1301. * Mark this descriptor complete by updating the channels completed
  1302. * cookie marker. Zero the descriptors cookie to prevent accidental
  1303. * repeated completions.
  1304. *
  1305. * Note: caller is expected to hold a lock to prevent concurrency.
  1306. */
  1307. static __INLINE void dma_cookie_complete(struct dma_async_tx_descriptor *tx)
  1308. {
  1309. BUG_ON(tx->cookie < DMA_MIN_COOKIE);
  1310. tx->chan->completed_cookie = tx->cookie;
  1311. tx->cookie = 0;
  1312. }
  1313. /**
  1314. * dma_cookie_status - report cookie status
  1315. * @chan: dma channel
  1316. * @cookie: cookie we are interested in
  1317. * @state: dma_tx_state structure to return last/used cookies
  1318. *
  1319. * Report the status of the cookie, filling in the state structure if
  1320. * non-NULL. No locking is required.
  1321. */
  1322. static __INLINE enum dma_status dma_cookie_status(struct dma_chan *chan,
  1323. dma_cookie_t cookie, struct dma_tx_state *state)
  1324. {
  1325. dma_cookie_t used, complete;
  1326. used = chan->cookie;
  1327. complete = chan->completed_cookie;
  1328. barrier();
  1329. if (state) {
  1330. state->last = complete;
  1331. state->used = used;
  1332. state->residue = 0;
  1333. }
  1334. return dma_async_is_complete(cookie, complete, used);
  1335. }
  1336. static __INLINE void dma_set_residue(struct dma_tx_state *state, u32 residue)
  1337. {
  1338. if (state)
  1339. state->residue = residue;
  1340. }
  1341. struct dmaengine_desc_callback {
  1342. dma_async_tx_callback callback;
  1343. dma_async_tx_callback_result callback_result;
  1344. void *callback_param;
  1345. };
  1346. /**
  1347. * dmaengine_desc_get_callback - get the passed in callback function
  1348. * @tx: tx descriptor
  1349. * @cb: temp struct to hold the callback info
  1350. *
  1351. * Fill the passed in cb struct with what's available in the passed in
  1352. * tx descriptor struct
  1353. * No locking is required.
  1354. */
  1355. static __INLINE void
  1356. dmaengine_desc_get_callback(struct dma_async_tx_descriptor *tx,
  1357. struct dmaengine_desc_callback *cb)
  1358. {
  1359. cb->callback = tx->callback;
  1360. cb->callback_result = tx->callback_result;
  1361. cb->callback_param = tx->callback_param;
  1362. }
  1363. /**
  1364. * dmaengine_desc_callback_invoke - call the callback function in cb struct
  1365. * @cb: temp struct that is holding the callback info
  1366. * @result: transaction result
  1367. *
  1368. * Call the callback function provided in the cb struct with the parameter
  1369. * in the cb struct.
  1370. * Locking is dependent on the driver.
  1371. */
  1372. static __INLINE void
  1373. dmaengine_desc_callback_invoke(struct dmaengine_desc_callback *cb,
  1374. const struct dmaengine_result *result)
  1375. {
  1376. struct dmaengine_result dummy_result = {
  1377. .result = DMA_TRANS_NOERROR,
  1378. .residue = 0
  1379. };
  1380. if (cb->callback_result) {
  1381. if (!result)
  1382. result = &dummy_result;
  1383. cb->callback_result(cb->callback_param, result);
  1384. } else if (cb->callback) {
  1385. cb->callback(cb->callback_param);
  1386. }
  1387. }
  1388. /**
  1389. * dmaengine_desc_get_callback_invoke - get the callback in tx descriptor and
  1390. * then immediately call the callback.
  1391. * @tx: dma async tx descriptor
  1392. * @result: transaction result
  1393. *
  1394. * Call dmaengine_desc_get_callback() and dmaengine_desc_callback_invoke()
  1395. * in a single function since no work is necessary in between for the driver.
  1396. * Locking is dependent on the driver.
  1397. */
  1398. static __INLINE void
  1399. dmaengine_desc_get_callback_invoke(struct dma_async_tx_descriptor *tx,
  1400. const struct dmaengine_result *result)
  1401. {
  1402. struct dmaengine_desc_callback cb;
  1403. dmaengine_desc_get_callback(tx, &cb);
  1404. dmaengine_desc_callback_invoke(&cb, result);
  1405. }
  1406. /**
  1407. * dmaengine_desc_callback_valid - verify the callback is valid in cb
  1408. * @cb: callback info struct
  1409. *
  1410. * Return a bool that verifies whether callback in cb is valid or not.
  1411. * No locking is required.
  1412. */
  1413. static __INLINE bool
  1414. dmaengine_desc_callback_valid(struct dmaengine_desc_callback *cb)
  1415. {
  1416. return (cb->callback) ? true : false;
  1417. }
  1418. #ifdef __cplusplus
  1419. }
  1420. #endif
  1421. #endif