dmaengine.h 45 KB

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