dmaengine.h 46 KB

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