dmaengine.h 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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. #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
  439. u16 map_cnt;
  440. #else
  441. u8 map_cnt;
  442. #endif
  443. u8 to_cnt;
  444. u8 from_cnt;
  445. u8 bidi_cnt;
  446. struct device *dev;
  447. struct kref kref;
  448. size_t len;
  449. dma_addr_t addr[0];
  450. };
  451. /**
  452. * struct dma_async_tx_descriptor - async transaction descriptor
  453. * ---dma generic offload fields---
  454. * @cookie: tracking cookie for this transaction, set to -EBUSY if
  455. * this tx is sitting on a dependency list
  456. * @flags: flags to augment operation preparation, control completion, and
  457. * communicate status
  458. * @phys: physical address of the descriptor
  459. * @chan: target channel for this operation
  460. * @tx_submit: accept the descriptor, assign ordered cookie and mark the
  461. * descriptor pending. To be pushed on .issue_pending() call
  462. * @callback: routine to call after this operation is complete
  463. * @callback_param: general parameter to pass to the callback routine
  464. * ---async_tx api specific fields---
  465. * @next: at completion submit this descriptor
  466. * @parent: pointer to the next level up in the dependency chain
  467. * @lock: protect the parent and next pointers
  468. */
  469. struct dma_async_tx_descriptor {
  470. dma_cookie_t cookie;
  471. enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
  472. dma_addr_t phys;
  473. struct dma_chan *chan;
  474. dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
  475. int (*desc_free)(struct dma_async_tx_descriptor *tx);
  476. dma_async_tx_callback callback;
  477. dma_async_tx_callback_result callback_result;
  478. void *callback_param;
  479. struct dmaengine_unmap_data *unmap;
  480. #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  481. struct dma_async_tx_descriptor *next;
  482. struct dma_async_tx_descriptor *parent;
  483. spinlock_t lock;
  484. #endif
  485. };
  486. #ifdef CONFIG_DMA_ENGINE
  487. static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
  488. struct dmaengine_unmap_data *unmap)
  489. {
  490. kref_get(&unmap->kref);
  491. tx->unmap = unmap;
  492. }
  493. struct dmaengine_unmap_data *
  494. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags);
  495. void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap);
  496. #else
  497. static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
  498. struct dmaengine_unmap_data *unmap)
  499. {
  500. }
  501. static inline struct dmaengine_unmap_data *
  502. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
  503. {
  504. return NULL;
  505. }
  506. static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
  507. {
  508. }
  509. #endif
  510. static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
  511. {
  512. if (tx->unmap) {
  513. dmaengine_unmap_put(tx->unmap);
  514. tx->unmap = NULL;
  515. }
  516. }
  517. #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  518. static inline void txd_lock(struct dma_async_tx_descriptor *txd)
  519. {
  520. }
  521. static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
  522. {
  523. }
  524. static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
  525. {
  526. BUG();
  527. }
  528. static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
  529. {
  530. }
  531. static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
  532. {
  533. }
  534. static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
  535. {
  536. return NULL;
  537. }
  538. static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
  539. {
  540. return NULL;
  541. }
  542. #else
  543. static inline void txd_lock(struct dma_async_tx_descriptor *txd)
  544. {
  545. spin_lock_bh(&txd->lock);
  546. }
  547. static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
  548. {
  549. spin_unlock_bh(&txd->lock);
  550. }
  551. static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
  552. {
  553. txd->next = next;
  554. next->parent = txd;
  555. }
  556. static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
  557. {
  558. txd->parent = NULL;
  559. }
  560. static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
  561. {
  562. txd->next = NULL;
  563. }
  564. static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
  565. {
  566. return txd->parent;
  567. }
  568. static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
  569. {
  570. return txd->next;
  571. }
  572. #endif
  573. /**
  574. * struct dma_tx_state - filled in to report the status of
  575. * a transfer.
  576. * @last: last completed DMA cookie
  577. * @used: last issued DMA cookie (i.e. the one in progress)
  578. * @residue: the remaining number of bytes left to transmit
  579. * on the selected transfer for states DMA_IN_PROGRESS and
  580. * DMA_PAUSED if this is implemented in the driver, else 0
  581. */
  582. struct dma_tx_state {
  583. dma_cookie_t last;
  584. dma_cookie_t used;
  585. u32 residue;
  586. };
  587. /**
  588. * enum dmaengine_alignment - defines alignment of the DMA async tx
  589. * buffers
  590. */
  591. enum dmaengine_alignment {
  592. DMAENGINE_ALIGN_1_BYTE = 0,
  593. DMAENGINE_ALIGN_2_BYTES = 1,
  594. DMAENGINE_ALIGN_4_BYTES = 2,
  595. DMAENGINE_ALIGN_8_BYTES = 3,
  596. DMAENGINE_ALIGN_16_BYTES = 4,
  597. DMAENGINE_ALIGN_32_BYTES = 5,
  598. DMAENGINE_ALIGN_64_BYTES = 6,
  599. };
  600. /**
  601. * struct dma_slave_map - associates slave device and it's slave channel with
  602. * parameter to be used by a filter function
  603. * @devname: name of the device
  604. * @slave: slave channel name
  605. * @param: opaque parameter to pass to struct dma_filter.fn
  606. */
  607. struct dma_slave_map {
  608. const char *devname;
  609. const char *slave;
  610. void *param;
  611. };
  612. /**
  613. * struct dma_filter - information for slave device/channel to filter_fn/param
  614. * mapping
  615. * @fn: filter function callback
  616. * @mapcnt: number of slave device/channel in the map
  617. * @map: array of channel to filter mapping data
  618. */
  619. struct dma_filter {
  620. dma_filter_fn fn;
  621. int mapcnt;
  622. const struct dma_slave_map *map;
  623. };
  624. /**
  625. * struct dma_device - info on the entity supplying DMA services
  626. * @chancnt: how many DMA channels are supported
  627. * @privatecnt: how many DMA channels are requested by dma_request_channel
  628. * @channels: the list of struct dma_chan
  629. * @global_node: list_head for global dma_device_list
  630. * @filter: information for device/slave to filter function/param mapping
  631. * @cap_mask: one or more dma_capability flags
  632. * @max_xor: maximum number of xor sources, 0 if no capability
  633. * @max_pq: maximum number of PQ sources and PQ-continue capability
  634. * @copy_align: alignment shift for memcpy operations
  635. * @xor_align: alignment shift for xor operations
  636. * @pq_align: alignment shift for pq operations
  637. * @fill_align: alignment shift for memset operations
  638. * @dev_id: unique device ID
  639. * @dev: struct device reference for dma mapping api
  640. * @src_addr_widths: bit mask of src addr widths the device supports
  641. * Width is specified in bytes, e.g. for a device supporting
  642. * a width of 4 the mask should have BIT(4) set.
  643. * @dst_addr_widths: bit mask of dst addr widths the device supports
  644. * @directions: bit mask of slave directions the device supports.
  645. * Since the enum dma_transfer_direction is not defined as bit flag for
  646. * each type, the dma controller should set BIT(<TYPE>) and same
  647. * should be checked by controller as well
  648. * @max_burst: max burst capability per-transfer
  649. * @residue_granularity: granularity of the transfer residue reported
  650. * by tx_status
  651. * @device_alloc_chan_resources: allocate resources and return the
  652. * number of allocated descriptors
  653. * @device_free_chan_resources: release DMA channel's resources
  654. * @device_prep_dma_memcpy: prepares a memcpy operation
  655. * @device_prep_dma_xor: prepares a xor operation
  656. * @device_prep_dma_xor_val: prepares a xor validation operation
  657. * @device_prep_dma_pq: prepares a pq operation
  658. * @device_prep_dma_pq_val: prepares a pqzero_sum operation
  659. * @device_prep_dma_memset: prepares a memset operation
  660. * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list
  661. * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
  662. * @device_prep_slave_sg: prepares a slave dma operation
  663. * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
  664. * The function takes a buffer of size buf_len. The callback function will
  665. * be called after period_len bytes have been transferred.
  666. * @device_prep_interleaved_dma: Transfer expression in a generic way.
  667. * @device_prep_dma_imm_data: DMA's 8 byte immediate data to the dst address
  668. * @device_config: Pushes a new configuration to a channel, return 0 or an error
  669. * code
  670. * @device_pause: Pauses any transfer happening on a channel. Returns
  671. * 0 or an error code
  672. * @device_resume: Resumes any transfer on a channel previously
  673. * paused. Returns 0 or an error code
  674. * @device_terminate_all: Aborts all transfers on a channel. Returns 0
  675. * or an error code
  676. * @device_synchronize: Synchronizes the termination of a transfers to the
  677. * current context.
  678. * @device_tx_status: poll for transaction completion, the optional
  679. * txstate parameter can be supplied with a pointer to get a
  680. * struct with auxiliary transfer status information, otherwise the call
  681. * will just return a simple status code
  682. * @device_issue_pending: push pending transactions to hardware
  683. * @descriptor_reuse: a submitted transfer can be resubmitted after completion
  684. */
  685. struct dma_device {
  686. unsigned int chancnt;
  687. unsigned int privatecnt;
  688. struct list_head channels;
  689. struct list_head global_node;
  690. struct dma_filter filter;
  691. dma_cap_mask_t cap_mask;
  692. unsigned short max_xor;
  693. unsigned short max_pq;
  694. enum dmaengine_alignment copy_align;
  695. enum dmaengine_alignment xor_align;
  696. enum dmaengine_alignment pq_align;
  697. enum dmaengine_alignment fill_align;
  698. #define DMA_HAS_PQ_CONTINUE (1 << 15)
  699. int dev_id;
  700. struct device *dev;
  701. u32 src_addr_widths;
  702. u32 dst_addr_widths;
  703. u32 directions;
  704. u32 max_burst;
  705. bool descriptor_reuse;
  706. enum dma_residue_granularity residue_granularity;
  707. int (*device_alloc_chan_resources)(struct dma_chan *chan);
  708. void (*device_free_chan_resources)(struct dma_chan *chan);
  709. struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
  710. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  711. size_t len, unsigned long flags);
  712. struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
  713. struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  714. unsigned int src_cnt, size_t len, unsigned long flags);
  715. struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
  716. struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
  717. size_t len, enum sum_check_flags *result, unsigned long flags);
  718. struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
  719. struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  720. unsigned int src_cnt, const unsigned char *scf,
  721. size_t len, unsigned long flags);
  722. struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
  723. struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  724. unsigned int src_cnt, const unsigned char *scf, size_t len,
  725. enum sum_check_flags *pqres, unsigned long flags);
  726. struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
  727. struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
  728. unsigned long flags);
  729. struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)(
  730. struct dma_chan *chan, struct scatterlist *sg,
  731. unsigned int nents, int value, unsigned long flags);
  732. struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
  733. struct dma_chan *chan, unsigned long flags);
  734. struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
  735. struct dma_chan *chan, struct scatterlist *sgl,
  736. unsigned int sg_len, enum dma_transfer_direction direction,
  737. unsigned long flags, void *context);
  738. struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
  739. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  740. size_t period_len, enum dma_transfer_direction direction,
  741. unsigned long flags);
  742. struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
  743. struct dma_chan *chan, struct dma_interleaved_template *xt,
  744. unsigned long flags);
  745. struct dma_async_tx_descriptor *(*device_prep_dma_imm_data)(
  746. struct dma_chan *chan, dma_addr_t dst, u64 data,
  747. unsigned long flags);
  748. int (*device_config)(struct dma_chan *chan,
  749. struct dma_slave_config *config);
  750. int (*device_pause)(struct dma_chan *chan);
  751. int (*device_resume)(struct dma_chan *chan);
  752. int (*device_terminate_all)(struct dma_chan *chan);
  753. void (*device_synchronize)(struct dma_chan *chan);
  754. enum dma_status (*device_tx_status)(struct dma_chan *chan,
  755. dma_cookie_t cookie,
  756. struct dma_tx_state *txstate);
  757. void (*device_issue_pending)(struct dma_chan *chan);
  758. };
  759. static inline int dmaengine_slave_config(struct dma_chan *chan,
  760. struct dma_slave_config *config)
  761. {
  762. if (chan->device->device_config)
  763. return chan->device->device_config(chan, config);
  764. return -ENOSYS;
  765. }
  766. static inline bool is_slave_direction(enum dma_transfer_direction direction)
  767. {
  768. return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM);
  769. }
  770. static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
  771. struct dma_chan *chan, dma_addr_t buf, size_t len,
  772. enum dma_transfer_direction dir, unsigned long flags)
  773. {
  774. struct scatterlist sg;
  775. sg_init_table(&sg, 1);
  776. sg_dma_address(&sg) = buf;
  777. sg_dma_len(&sg) = len;
  778. if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
  779. return NULL;
  780. return chan->device->device_prep_slave_sg(chan, &sg, 1,
  781. dir, flags, NULL);
  782. }
  783. static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
  784. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  785. enum dma_transfer_direction dir, unsigned long flags)
  786. {
  787. if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
  788. return NULL;
  789. return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
  790. dir, flags, NULL);
  791. }
  792. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  793. struct rio_dma_ext;
  794. static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg(
  795. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  796. enum dma_transfer_direction dir, unsigned long flags,
  797. struct rio_dma_ext *rio_ext)
  798. {
  799. if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
  800. return NULL;
  801. return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
  802. dir, flags, rio_ext);
  803. }
  804. #endif
  805. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
  806. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  807. size_t period_len, enum dma_transfer_direction dir,
  808. unsigned long flags)
  809. {
  810. if (!chan || !chan->device || !chan->device->device_prep_dma_cyclic)
  811. return NULL;
  812. return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len,
  813. period_len, dir, flags);
  814. }
  815. static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
  816. struct dma_chan *chan, struct dma_interleaved_template *xt,
  817. unsigned long flags)
  818. {
  819. if (!chan || !chan->device || !chan->device->device_prep_interleaved_dma)
  820. return NULL;
  821. return chan->device->device_prep_interleaved_dma(chan, xt, flags);
  822. }
  823. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset(
  824. struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
  825. unsigned long flags)
  826. {
  827. if (!chan || !chan->device || !chan->device->device_prep_dma_memset)
  828. return NULL;
  829. return chan->device->device_prep_dma_memset(chan, dest, value,
  830. len, flags);
  831. }
  832. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy(
  833. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  834. size_t len, unsigned long flags)
  835. {
  836. if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy)
  837. return NULL;
  838. return chan->device->device_prep_dma_memcpy(chan, dest, src,
  839. len, flags);
  840. }
  841. /**
  842. * dmaengine_terminate_all() - Terminate all active DMA transfers
  843. * @chan: The channel for which to terminate the transfers
  844. *
  845. * This function is DEPRECATED use either dmaengine_terminate_sync() or
  846. * dmaengine_terminate_async() instead.
  847. */
  848. static inline int dmaengine_terminate_all(struct dma_chan *chan)
  849. {
  850. if (chan->device->device_terminate_all)
  851. return chan->device->device_terminate_all(chan);
  852. return -ENOSYS;
  853. }
  854. /**
  855. * dmaengine_terminate_async() - Terminate all active DMA transfers
  856. * @chan: The channel for which to terminate the transfers
  857. *
  858. * Calling this function will terminate all active and pending descriptors
  859. * that have previously been submitted to the channel. It is not guaranteed
  860. * though that the transfer for the active descriptor has stopped when the
  861. * function returns. Furthermore it is possible the complete callback of a
  862. * submitted transfer is still running when this function returns.
  863. *
  864. * dmaengine_synchronize() needs to be called before it is safe to free
  865. * any memory that is accessed by previously submitted descriptors or before
  866. * freeing any resources accessed from within the completion callback of any
  867. * perviously submitted descriptors.
  868. *
  869. * This function can be called from atomic context as well as from within a
  870. * complete callback of a descriptor submitted on the same channel.
  871. *
  872. * If none of the two conditions above apply consider using
  873. * dmaengine_terminate_sync() instead.
  874. */
  875. static inline int dmaengine_terminate_async(struct dma_chan *chan)
  876. {
  877. if (chan->device->device_terminate_all)
  878. return chan->device->device_terminate_all(chan);
  879. return -EINVAL;
  880. }
  881. /**
  882. * dmaengine_synchronize() - Synchronize DMA channel termination
  883. * @chan: The channel to synchronize
  884. *
  885. * Synchronizes to the DMA channel termination to the current context. When this
  886. * function returns it is guaranteed that all transfers for previously issued
  887. * descriptors have stopped and and it is safe to free the memory assoicated
  888. * with them. Furthermore it is guaranteed that all complete callback functions
  889. * for a previously submitted descriptor have finished running and it is safe to
  890. * free resources accessed from within the complete callbacks.
  891. *
  892. * The behavior of this function is undefined if dma_async_issue_pending() has
  893. * been called between dmaengine_terminate_async() and this function.
  894. *
  895. * This function must only be called from non-atomic context and must not be
  896. * called from within a complete callback of a descriptor submitted on the same
  897. * channel.
  898. */
  899. static inline void dmaengine_synchronize(struct dma_chan *chan)
  900. {
  901. might_sleep();
  902. if (chan->device->device_synchronize)
  903. chan->device->device_synchronize(chan);
  904. }
  905. /**
  906. * dmaengine_terminate_sync() - Terminate all active DMA transfers
  907. * @chan: The channel for which to terminate the transfers
  908. *
  909. * Calling this function will terminate all active and pending transfers
  910. * that have previously been submitted to the channel. It is similar to
  911. * dmaengine_terminate_async() but guarantees that the DMA transfer has actually
  912. * stopped and that all complete callbacks have finished running when the
  913. * function returns.
  914. *
  915. * This function must only be called from non-atomic context and must not be
  916. * called from within a complete callback of a descriptor submitted on the same
  917. * channel.
  918. */
  919. static inline int dmaengine_terminate_sync(struct dma_chan *chan)
  920. {
  921. int ret;
  922. ret = dmaengine_terminate_async(chan);
  923. if (ret)
  924. return ret;
  925. dmaengine_synchronize(chan);
  926. return 0;
  927. }
  928. static inline int dmaengine_pause(struct dma_chan *chan)
  929. {
  930. if (chan->device->device_pause)
  931. return chan->device->device_pause(chan);
  932. return -ENOSYS;
  933. }
  934. static inline int dmaengine_resume(struct dma_chan *chan)
  935. {
  936. if (chan->device->device_resume)
  937. return chan->device->device_resume(chan);
  938. return -ENOSYS;
  939. }
  940. static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan,
  941. dma_cookie_t cookie, struct dma_tx_state *state)
  942. {
  943. return chan->device->device_tx_status(chan, cookie, state);
  944. }
  945. static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
  946. {
  947. return desc->tx_submit(desc);
  948. }
  949. static inline bool dmaengine_check_align(enum dmaengine_alignment align,
  950. size_t off1, size_t off2, size_t len)
  951. {
  952. size_t mask;
  953. if (!align)
  954. return true;
  955. mask = (1 << align) - 1;
  956. if (mask & (off1 | off2 | len))
  957. return false;
  958. return true;
  959. }
  960. static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
  961. size_t off2, size_t len)
  962. {
  963. return dmaengine_check_align(dev->copy_align, off1, off2, len);
  964. }
  965. static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
  966. size_t off2, size_t len)
  967. {
  968. return dmaengine_check_align(dev->xor_align, off1, off2, len);
  969. }
  970. static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
  971. size_t off2, size_t len)
  972. {
  973. return dmaengine_check_align(dev->pq_align, off1, off2, len);
  974. }
  975. static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
  976. size_t off2, size_t len)
  977. {
  978. return dmaengine_check_align(dev->fill_align, off1, off2, len);
  979. }
  980. static inline void
  981. dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
  982. {
  983. dma->max_pq = maxpq;
  984. if (has_pq_continue)
  985. dma->max_pq |= DMA_HAS_PQ_CONTINUE;
  986. }
  987. static inline bool dmaf_continue(enum dma_ctrl_flags flags)
  988. {
  989. return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
  990. }
  991. static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
  992. {
  993. enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
  994. return (flags & mask) == mask;
  995. }
  996. static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
  997. {
  998. return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
  999. }
  1000. static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
  1001. {
  1002. return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
  1003. }
  1004. /* dma_maxpq - reduce maxpq in the face of continued operations
  1005. * @dma - dma device with PQ capability
  1006. * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
  1007. *
  1008. * When an engine does not support native continuation we need 3 extra
  1009. * source slots to reuse P and Q with the following coefficients:
  1010. * 1/ {00} * P : remove P from Q', but use it as a source for P'
  1011. * 2/ {01} * Q : use Q to continue Q' calculation
  1012. * 3/ {00} * Q : subtract Q from P' to cancel (2)
  1013. *
  1014. * In the case where P is disabled we only need 1 extra source:
  1015. * 1/ {01} * Q : use Q to continue Q' calculation
  1016. */
  1017. static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
  1018. {
  1019. if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
  1020. return dma_dev_to_maxpq(dma);
  1021. else if (dmaf_p_disabled_continue(flags))
  1022. return dma_dev_to_maxpq(dma) - 1;
  1023. else if (dmaf_continue(flags))
  1024. return dma_dev_to_maxpq(dma) - 3;
  1025. BUG();
  1026. }
  1027. static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
  1028. size_t dir_icg)
  1029. {
  1030. if (inc) {
  1031. if (dir_icg)
  1032. return dir_icg;
  1033. else if (sgl)
  1034. return icg;
  1035. }
  1036. return 0;
  1037. }
  1038. static inline size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt,
  1039. struct data_chunk *chunk)
  1040. {
  1041. return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl,
  1042. chunk->icg, chunk->dst_icg);
  1043. }
  1044. static inline size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt,
  1045. struct data_chunk *chunk)
  1046. {
  1047. return dmaengine_get_icg(xt->src_inc, xt->src_sgl,
  1048. chunk->icg, chunk->src_icg);
  1049. }
  1050. /* --- public DMA engine API --- */
  1051. #ifdef CONFIG_DMA_ENGINE
  1052. void dmaengine_get(void);
  1053. void dmaengine_put(void);
  1054. #else
  1055. static inline void dmaengine_get(void)
  1056. {
  1057. }
  1058. static inline void dmaengine_put(void)
  1059. {
  1060. }
  1061. #endif
  1062. #ifdef CONFIG_ASYNC_TX_DMA
  1063. #define async_dmaengine_get() dmaengine_get()
  1064. #define async_dmaengine_put() dmaengine_put()
  1065. #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  1066. #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
  1067. #else
  1068. #define async_dma_find_channel(type) dma_find_channel(type)
  1069. #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
  1070. #else
  1071. static inline void async_dmaengine_get(void)
  1072. {
  1073. }
  1074. static inline void async_dmaengine_put(void)
  1075. {
  1076. }
  1077. static inline struct dma_chan *
  1078. async_dma_find_channel(enum dma_transaction_type type)
  1079. {
  1080. return NULL;
  1081. }
  1082. #endif /* CONFIG_ASYNC_TX_DMA */
  1083. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  1084. struct dma_chan *chan);
  1085. static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
  1086. {
  1087. tx->flags |= DMA_CTRL_ACK;
  1088. }
  1089. static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
  1090. {
  1091. tx->flags &= ~DMA_CTRL_ACK;
  1092. }
  1093. static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
  1094. {
  1095. return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
  1096. }
  1097. #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
  1098. static inline void
  1099. __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  1100. {
  1101. set_bit(tx_type, dstp->bits);
  1102. }
  1103. #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
  1104. static inline void
  1105. __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  1106. {
  1107. clear_bit(tx_type, dstp->bits);
  1108. }
  1109. #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
  1110. static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
  1111. {
  1112. bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
  1113. }
  1114. #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
  1115. static inline int
  1116. __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
  1117. {
  1118. return test_bit(tx_type, srcp->bits);
  1119. }
  1120. #define for_each_dma_cap_mask(cap, mask) \
  1121. for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END)
  1122. /**
  1123. * dma_async_issue_pending - flush pending transactions to HW
  1124. * @chan: target DMA channel
  1125. *
  1126. * This allows drivers to push copies to HW in batches,
  1127. * reducing MMIO writes where possible.
  1128. */
  1129. static inline void dma_async_issue_pending(struct dma_chan *chan)
  1130. {
  1131. chan->device->device_issue_pending(chan);
  1132. }
  1133. /**
  1134. * dma_async_is_tx_complete - poll for transaction completion
  1135. * @chan: DMA channel
  1136. * @cookie: transaction identifier to check status of
  1137. * @last: returns last completed cookie, can be NULL
  1138. * @used: returns last issued cookie, can be NULL
  1139. *
  1140. * If @last and @used are passed in, upon return they reflect the driver
  1141. * internal state and can be used with dma_async_is_complete() to check
  1142. * the status of multiple cookies without re-checking hardware state.
  1143. */
  1144. static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
  1145. dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
  1146. {
  1147. struct dma_tx_state state;
  1148. enum dma_status status;
  1149. status = chan->device->device_tx_status(chan, cookie, &state);
  1150. if (last)
  1151. *last = state.last;
  1152. if (used)
  1153. *used = state.used;
  1154. return status;
  1155. }
  1156. /**
  1157. * dma_async_is_complete - test a cookie against chan state
  1158. * @cookie: transaction identifier to test status of
  1159. * @last_complete: last know completed transaction
  1160. * @last_used: last cookie value handed out
  1161. *
  1162. * dma_async_is_complete() is used in dma_async_is_tx_complete()
  1163. * the test logic is separated for lightweight testing of multiple cookies
  1164. */
  1165. static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
  1166. dma_cookie_t last_complete, dma_cookie_t last_used)
  1167. {
  1168. if (last_complete <= last_used) {
  1169. if ((cookie <= last_complete) || (cookie > last_used))
  1170. return DMA_COMPLETE;
  1171. } else {
  1172. if ((cookie <= last_complete) && (cookie > last_used))
  1173. return DMA_COMPLETE;
  1174. }
  1175. return DMA_IN_PROGRESS;
  1176. }
  1177. static inline void
  1178. dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
  1179. {
  1180. if (st) {
  1181. st->last = last;
  1182. st->used = used;
  1183. st->residue = residue;
  1184. }
  1185. }
  1186. #ifdef CONFIG_DMA_ENGINE
  1187. struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
  1188. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
  1189. enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
  1190. void dma_issue_pending_all(void);
  1191. struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  1192. dma_filter_fn fn, void *fn_param);
  1193. struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name);
  1194. struct dma_chan *dma_request_chan(struct device *dev, const char *name);
  1195. struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
  1196. void dma_release_channel(struct dma_chan *chan);
  1197. int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
  1198. #else
  1199. static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
  1200. {
  1201. return NULL;
  1202. }
  1203. static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  1204. {
  1205. return DMA_COMPLETE;
  1206. }
  1207. static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
  1208. {
  1209. return DMA_COMPLETE;
  1210. }
  1211. static inline void dma_issue_pending_all(void)
  1212. {
  1213. }
  1214. static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  1215. dma_filter_fn fn, void *fn_param)
  1216. {
  1217. return NULL;
  1218. }
  1219. static inline struct dma_chan *dma_request_slave_channel(struct device *dev,
  1220. const char *name)
  1221. {
  1222. return NULL;
  1223. }
  1224. static inline struct dma_chan *dma_request_chan(struct device *dev,
  1225. const char *name)
  1226. {
  1227. return ERR_PTR(-ENODEV);
  1228. }
  1229. static inline struct dma_chan *dma_request_chan_by_mask(
  1230. const dma_cap_mask_t *mask)
  1231. {
  1232. return ERR_PTR(-ENODEV);
  1233. }
  1234. static inline void dma_release_channel(struct dma_chan *chan)
  1235. {
  1236. }
  1237. static inline int dma_get_slave_caps(struct dma_chan *chan,
  1238. struct dma_slave_caps *caps)
  1239. {
  1240. return -ENXIO;
  1241. }
  1242. #endif
  1243. #define dma_request_slave_channel_reason(dev, name) dma_request_chan(dev, name)
  1244. static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
  1245. {
  1246. struct dma_slave_caps caps;
  1247. dma_get_slave_caps(tx->chan, &caps);
  1248. if (caps.descriptor_reuse) {
  1249. tx->flags |= DMA_CTRL_REUSE;
  1250. return 0;
  1251. } else {
  1252. return -EPERM;
  1253. }
  1254. }
  1255. static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
  1256. {
  1257. tx->flags &= ~DMA_CTRL_REUSE;
  1258. }
  1259. static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
  1260. {
  1261. return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE;
  1262. }
  1263. static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
  1264. {
  1265. /* this is supported for reusable desc, so check that */
  1266. if (dmaengine_desc_test_reuse(desc))
  1267. return desc->desc_free(desc);
  1268. else
  1269. return -EPERM;
  1270. }
  1271. /* --- DMA device --- */
  1272. int dma_async_device_register(struct dma_device *device);
  1273. void dma_async_device_unregister(struct dma_device *device);
  1274. void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
  1275. struct dma_chan *dma_get_slave_channel(struct dma_chan *chan);
  1276. struct dma_chan *dma_get_any_slave_channel(struct dma_device *device);
  1277. #define dma_request_channel(mask, x, y) __dma_request_channel(&(mask), x, y)
  1278. #define dma_request_slave_channel_compat(mask, x, y, dev, name) \
  1279. __dma_request_slave_channel_compat(&(mask), x, y, dev, name)
  1280. static inline struct dma_chan
  1281. *__dma_request_slave_channel_compat(const dma_cap_mask_t *mask,
  1282. dma_filter_fn fn, void *fn_param,
  1283. struct device *dev, const char *name)
  1284. {
  1285. struct dma_chan *chan;
  1286. chan = dma_request_slave_channel(dev, name);
  1287. if (chan)
  1288. return chan;
  1289. if (!fn || !fn_param)
  1290. return NULL;
  1291. return __dma_request_channel(mask, fn, fn_param);
  1292. }
  1293. #endif /* DMAENGINE_H */