dma-api.rst 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. ============================================
  2. Dynamic DMA mapping using the generic device
  3. ============================================
  4. :Author: James E.J. Bottomley <James.Bottomley@HansenPartnership.com>
  5. This document describes the DMA API. For a more gentle introduction
  6. of the API (and actual examples), see Documentation/core-api/dma-api-howto.rst.
  7. This API is split into two pieces. Part I describes the basic API.
  8. Part II describes extensions for supporting non-coherent memory
  9. machines. Unless you know that your driver absolutely has to support
  10. non-coherent platforms (this is usually only legacy platforms) you
  11. should only use the API described in part I.
  12. Part I - DMA API
  13. ----------------
  14. To get the DMA API, you must #include <linux/dma-mapping.h>. This
  15. provides dma_addr_t and the interfaces described below.
  16. A dma_addr_t can hold any valid DMA address for the platform. It can be
  17. given to a device to use as a DMA source or target. A CPU cannot reference
  18. a dma_addr_t directly because there may be translation between its physical
  19. address space and the DMA address space.
  20. Part Ia - Using large DMA-coherent buffers
  21. ------------------------------------------
  22. ::
  23. void *
  24. dma_alloc_coherent(struct device *dev, size_t size,
  25. dma_addr_t *dma_handle, gfp_t flag)
  26. Coherent memory is memory for which a write by either the device or
  27. the processor can immediately be read by the processor or device
  28. without having to worry about caching effects. (You may however need
  29. to make sure to flush the processor's write buffers before telling
  30. devices to read that memory.)
  31. This routine allocates a region of <size> bytes of coherent memory.
  32. It returns a pointer to the allocated region (in the processor's virtual
  33. address space) or NULL if the allocation failed.
  34. It also returns a <dma_handle> which may be cast to an unsigned integer the
  35. same width as the bus and given to the device as the DMA address base of
  36. the region.
  37. Note: coherent memory can be expensive on some platforms, and the
  38. minimum allocation length may be as big as a page, so you should
  39. consolidate your requests for coherent memory as much as possible.
  40. The simplest way to do that is to use the dma_pool calls (see below).
  41. The flag parameter allows the caller to specify the ``GFP_`` flags (see
  42. kmalloc()) for the allocation (the implementation may ignore flags that affect
  43. the location of the returned memory, like GFP_DMA).
  44. ::
  45. void
  46. dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
  47. dma_addr_t dma_handle)
  48. Free a previously allocated region of coherent memory. dev, size and dma_handle
  49. must all be the same as those passed into dma_alloc_coherent(). cpu_addr must
  50. be the virtual address returned by dma_alloc_coherent().
  51. Note that unlike the sibling allocation call, this routine may only be called
  52. with IRQs enabled.
  53. Part Ib - Using small DMA-coherent buffers
  54. ------------------------------------------
  55. To get this part of the DMA API, you must #include <linux/dmapool.h>
  56. Many drivers need lots of small DMA-coherent memory regions for DMA
  57. descriptors or I/O buffers. Rather than allocating in units of a page
  58. or more using dma_alloc_coherent(), you can use DMA pools. These work
  59. much like a struct kmem_cache, except that they use the DMA-coherent allocator,
  60. not __get_free_pages(). Also, they understand common hardware constraints
  61. for alignment, like queue heads needing to be aligned on N-byte boundaries.
  62. .. kernel-doc:: mm/dmapool.c
  63. :export:
  64. .. kernel-doc:: include/linux/dmapool.h
  65. Part Ic - DMA addressing limitations
  66. ------------------------------------
  67. DMA mask is a bit mask of the addressable region for the device. In other words,
  68. if applying the DMA mask (a bitwise AND operation) to the DMA address of a
  69. memory region does not clear any bits in the address, then the device can
  70. perform DMA to that memory region.
  71. All the below functions which set a DMA mask may fail if the requested mask
  72. cannot be used with the device, or if the device is not capable of doing DMA.
  73. ::
  74. int
  75. dma_set_mask_and_coherent(struct device *dev, u64 mask)
  76. Updates both streaming and coherent DMA masks.
  77. Returns: 0 if successful and a negative error if not.
  78. ::
  79. int
  80. dma_set_mask(struct device *dev, u64 mask)
  81. Updates only the streaming DMA mask.
  82. Returns: 0 if successful and a negative error if not.
  83. ::
  84. int
  85. dma_set_coherent_mask(struct device *dev, u64 mask)
  86. Updates only the coherent DMA mask.
  87. Returns: 0 if successful and a negative error if not.
  88. ::
  89. u64
  90. dma_get_required_mask(struct device *dev)
  91. This API returns the mask that the platform requires to
  92. operate efficiently. Usually this means the returned mask
  93. is the minimum required to cover all of memory. Examining the
  94. required mask gives drivers with variable descriptor sizes the
  95. opportunity to use smaller descriptors as necessary.
  96. Requesting the required mask does not alter the current mask. If you
  97. wish to take advantage of it, you should issue a dma_set_mask()
  98. call to set the mask to the value returned.
  99. ::
  100. size_t
  101. dma_max_mapping_size(struct device *dev);
  102. Returns the maximum size of a mapping for the device. The size parameter
  103. of the mapping functions like dma_map_single(), dma_map_page() and
  104. others should not be larger than the returned value.
  105. ::
  106. size_t
  107. dma_opt_mapping_size(struct device *dev);
  108. Returns the maximum optimal size of a mapping for the device.
  109. Mapping larger buffers may take much longer in certain scenarios. In
  110. addition, for high-rate short-lived streaming mappings, the upfront time
  111. spent on the mapping may account for an appreciable part of the total
  112. request lifetime. As such, if splitting larger requests incurs no
  113. significant performance penalty, then device drivers are advised to
  114. limit total DMA streaming mappings length to the returned value.
  115. ::
  116. bool
  117. dma_need_sync(struct device *dev, dma_addr_t dma_addr);
  118. Returns %true if dma_sync_single_for_{device,cpu} calls are required to
  119. transfer memory ownership. Returns %false if those calls can be skipped.
  120. ::
  121. unsigned long
  122. dma_get_merge_boundary(struct device *dev);
  123. Returns the DMA merge boundary. If the device cannot merge any DMA address
  124. segments, the function returns 0.
  125. Part Id - Streaming DMA mappings
  126. --------------------------------
  127. Streaming DMA allows to map an existing buffer for DMA transfers and then
  128. unmap it when finished. Map functions are not guaranteed to succeed, so the
  129. return value must be checked.
  130. .. note::
  131. In particular, mapping may fail for memory not addressable by the
  132. device, e.g. if it is not within the DMA mask of the device and/or a
  133. connecting bus bridge. Streaming DMA functions try to overcome such
  134. addressing constraints, either by using an IOMMU (a device which maps
  135. I/O DMA addresses to physical memory addresses), or by copying the
  136. data to/from a bounce buffer if the kernel is configured with a
  137. :doc:`SWIOTLB <swiotlb>`. However, these methods are not always
  138. available, and even if they are, they may still fail for a number of
  139. reasons.
  140. In short, a device driver may need to be wary of where buffers are
  141. located in physical memory, especially if the DMA mask is less than 32
  142. bits.
  143. ::
  144. dma_addr_t
  145. dma_map_single(struct device *dev, void *cpu_addr, size_t size,
  146. enum dma_data_direction direction)
  147. Maps a piece of processor virtual memory so it can be accessed by the
  148. device and returns the DMA address of the memory.
  149. The DMA API uses a strongly typed enumerator for its direction:
  150. ======================= =============================================
  151. DMA_NONE no direction (used for debugging)
  152. DMA_TO_DEVICE data is going from the memory to the device
  153. DMA_FROM_DEVICE data is coming from the device to the memory
  154. DMA_BIDIRECTIONAL direction isn't known
  155. ======================= =============================================
  156. .. note::
  157. Contiguous kernel virtual space may not be contiguous as
  158. physical memory. Since this API does not provide any scatter/gather
  159. capability, it will fail if the user tries to map a non-physically
  160. contiguous piece of memory. For this reason, memory to be mapped by
  161. this API should be obtained from sources which guarantee it to be
  162. physically contiguous (like kmalloc).
  163. .. warning::
  164. Memory coherency operates at a granularity called the cache
  165. line width. In order for memory mapped by this API to operate
  166. correctly, the mapped region must begin exactly on a cache line
  167. boundary and end exactly on one (to prevent two separately mapped
  168. regions from sharing a single cache line). Since the cache line size
  169. may not be known at compile time, the API will not enforce this
  170. requirement. Therefore, it is recommended that driver writers who
  171. don't take special care to determine the cache line size at run time
  172. only map virtual regions that begin and end on page boundaries (which
  173. are guaranteed also to be cache line boundaries).
  174. DMA_TO_DEVICE synchronisation must be done after the last modification
  175. of the memory region by the software and before it is handed off to
  176. the device. Once this primitive is used, memory covered by this
  177. primitive should be treated as read-only by the device. If the device
  178. may write to it at any point, it should be DMA_BIDIRECTIONAL (see
  179. below).
  180. DMA_FROM_DEVICE synchronisation must be done before the driver
  181. accesses data that may be changed by the device. This memory should
  182. be treated as read-only by the driver. If the driver needs to write
  183. to it at any point, it should be DMA_BIDIRECTIONAL (see below).
  184. DMA_BIDIRECTIONAL requires special handling: it means that the driver
  185. isn't sure if the memory was modified before being handed off to the
  186. device and also isn't sure if the device will also modify it. Thus,
  187. you must always sync bidirectional memory twice: once before the
  188. memory is handed off to the device (to make sure all memory changes
  189. are flushed from the processor) and once before the data may be
  190. accessed after being used by the device (to make sure any processor
  191. cache lines are updated with data that the device may have changed).
  192. ::
  193. void
  194. dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  195. enum dma_data_direction direction)
  196. Unmaps the region previously mapped. All the parameters passed in
  197. must be identical to those passed to (and returned by) dma_map_single().
  198. ::
  199. dma_addr_t
  200. dma_map_page(struct device *dev, struct page *page,
  201. unsigned long offset, size_t size,
  202. enum dma_data_direction direction)
  203. void
  204. dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
  205. enum dma_data_direction direction)
  206. API for mapping and unmapping for pages. All the notes and warnings
  207. for the other mapping APIs apply here. Also, although the <offset>
  208. and <size> parameters are provided to do partial page mapping, it is
  209. recommended that you never use these unless you really know what the
  210. cache width is.
  211. ::
  212. dma_addr_t
  213. dma_map_resource(struct device *dev, phys_addr_t phys_addr, size_t size,
  214. enum dma_data_direction dir, unsigned long attrs)
  215. void
  216. dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
  217. enum dma_data_direction dir, unsigned long attrs)
  218. API for mapping and unmapping for MMIO resources. All the notes and
  219. warnings for the other mapping APIs apply here. The API should only be
  220. used to map device MMIO resources, mapping of RAM is not permitted.
  221. ::
  222. int
  223. dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  224. In some circumstances dma_map_single(), dma_map_page() and dma_map_resource()
  225. will fail to create a mapping. A driver can check for these errors by testing
  226. the returned DMA address with dma_mapping_error(). A non-zero return value
  227. means the mapping could not be created and the driver should take appropriate
  228. action (e.g. reduce current DMA mapping usage or delay and try again later).
  229. ::
  230. int
  231. dma_map_sg(struct device *dev, struct scatterlist *sg,
  232. int nents, enum dma_data_direction direction)
  233. Maps a scatter/gather list for DMA. Returns the number of DMA address segments
  234. mapped, which may be smaller than <nents> passed in if several consecutive
  235. sglist entries are merged (e.g. with an IOMMU, or if some adjacent segments
  236. just happen to be physically contiguous).
  237. Please note that the sg cannot be mapped again if it has been mapped once.
  238. The mapping process is allowed to destroy information in the sg.
  239. As with the other mapping interfaces, dma_map_sg() can fail. When it
  240. does, 0 is returned and a driver must take appropriate action. It is
  241. critical that the driver do something, in the case of a block driver
  242. aborting the request or even oopsing is better than doing nothing and
  243. corrupting the filesystem.
  244. With scatterlists, you use the resulting mapping like this::
  245. int i, count = dma_map_sg(dev, sglist, nents, direction);
  246. struct scatterlist *sg;
  247. for_each_sg(sglist, sg, count, i) {
  248. hw_address[i] = sg_dma_address(sg);
  249. hw_len[i] = sg_dma_len(sg);
  250. }
  251. where nents is the number of entries in the sglist.
  252. The implementation is free to merge several consecutive sglist entries
  253. into one. The returned number is the actual number of sg entries it
  254. mapped them to. On failure, 0 is returned.
  255. Then you should loop count times (note: this can be less than nents times)
  256. and use sg_dma_address() and sg_dma_len() macros where you previously
  257. accessed sg->address and sg->length as shown above.
  258. ::
  259. void
  260. dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  261. int nents, enum dma_data_direction direction)
  262. Unmap the previously mapped scatter/gather list. All the parameters
  263. must be the same as those and passed in to the scatter/gather mapping
  264. API.
  265. Note: <nents> must be the number you passed in, *not* the number of
  266. DMA address entries returned.
  267. ::
  268. void
  269. dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  270. size_t size,
  271. enum dma_data_direction direction)
  272. void
  273. dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  274. size_t size,
  275. enum dma_data_direction direction)
  276. void
  277. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  278. int nents,
  279. enum dma_data_direction direction)
  280. void
  281. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  282. int nents,
  283. enum dma_data_direction direction)
  284. Synchronise a single contiguous or scatter/gather mapping for the CPU
  285. and device. With the sync_sg API, all the parameters must be the same
  286. as those passed into the sg mapping API. With the sync_single API,
  287. you can use dma_handle and size parameters that aren't identical to
  288. those passed into the single mapping API to do a partial sync.
  289. .. note::
  290. You must do this:
  291. - Before reading values that have been written by DMA from the device
  292. (use the DMA_FROM_DEVICE direction)
  293. - After writing values that will be written to the device using DMA
  294. (use the DMA_TO_DEVICE) direction
  295. - before *and* after handing memory to the device if the memory is
  296. DMA_BIDIRECTIONAL
  297. See also dma_map_single().
  298. ::
  299. dma_addr_t
  300. dma_map_single_attrs(struct device *dev, void *cpu_addr, size_t size,
  301. enum dma_data_direction dir,
  302. unsigned long attrs)
  303. void
  304. dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr,
  305. size_t size, enum dma_data_direction dir,
  306. unsigned long attrs)
  307. int
  308. dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
  309. int nents, enum dma_data_direction dir,
  310. unsigned long attrs)
  311. void
  312. dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
  313. int nents, enum dma_data_direction dir,
  314. unsigned long attrs)
  315. The four functions above are just like the counterpart functions
  316. without the _attrs suffixes, except that they pass an optional
  317. dma_attrs.
  318. The interpretation of DMA attributes is architecture-specific, and
  319. each attribute should be documented in
  320. Documentation/core-api/dma-attributes.rst.
  321. If dma_attrs are 0, the semantics of each of these functions
  322. is identical to those of the corresponding function
  323. without the _attrs suffix. As a result dma_map_single_attrs()
  324. can generally replace dma_map_single(), etc.
  325. As an example of the use of the ``*_attrs`` functions, here's how
  326. you could pass an attribute DMA_ATTR_FOO when mapping memory
  327. for DMA::
  328. #include <linux/dma-mapping.h>
  329. /* DMA_ATTR_FOO should be defined in linux/dma-mapping.h and
  330. * documented in Documentation/core-api/dma-attributes.rst */
  331. ...
  332. unsigned long attr;
  333. attr |= DMA_ATTR_FOO;
  334. ....
  335. n = dma_map_sg_attrs(dev, sg, nents, DMA_TO_DEVICE, attr);
  336. ....
  337. Architectures that care about DMA_ATTR_FOO would check for its
  338. presence in their implementations of the mapping and unmapping
  339. routines, e.g.:::
  340. void whizco_dma_map_sg_attrs(struct device *dev, dma_addr_t dma_addr,
  341. size_t size, enum dma_data_direction dir,
  342. unsigned long attrs)
  343. {
  344. ....
  345. if (attrs & DMA_ATTR_FOO)
  346. /* twizzle the frobnozzle */
  347. ....
  348. }
  349. Part Ie - IOVA-based DMA mappings
  350. ---------------------------------
  351. These APIs allow a very efficient mapping when using an IOMMU. They are an
  352. optional path that requires extra code and are only recommended for drivers
  353. where DMA mapping performance, or the space usage for storing the DMA addresses
  354. matter. All the considerations from the previous section apply here as well.
  355. ::
  356. bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state,
  357. phys_addr_t phys, size_t size);
  358. Is used to try to allocate IOVA space for mapping operation. If it returns
  359. false this API can't be used for the given device and the normal streaming
  360. DMA mapping API should be used. The ``struct dma_iova_state`` is allocated
  361. by the driver and must be kept around until unmap time.
  362. ::
  363. static inline bool dma_use_iova(struct dma_iova_state *state)
  364. Can be used by the driver to check if the IOVA-based API is used after a
  365. call to dma_iova_try_alloc. This can be useful in the unmap path.
  366. ::
  367. int dma_iova_link(struct device *dev, struct dma_iova_state *state,
  368. phys_addr_t phys, size_t offset, size_t size,
  369. enum dma_data_direction dir, unsigned long attrs);
  370. Is used to link ranges to the IOVA previously allocated. The start of all
  371. but the first call to dma_iova_link for a given state must be aligned
  372. to the DMA merge boundary returned by ``dma_get_merge_boundary())``, and
  373. the size of all but the last range must be aligned to the DMA merge boundary
  374. as well.
  375. ::
  376. int dma_iova_sync(struct device *dev, struct dma_iova_state *state,
  377. size_t offset, size_t size);
  378. Must be called to sync the IOMMU page tables for IOVA-range mapped by one or
  379. more calls to ``dma_iova_link()``.
  380. For drivers that use a one-shot mapping, all ranges can be unmapped and the
  381. IOVA freed by calling:
  382. ::
  383. void dma_iova_destroy(struct device *dev, struct dma_iova_state *state,
  384. size_t mapped_len, enum dma_data_direction dir,
  385. unsigned long attrs);
  386. Alternatively drivers can dynamically manage the IOVA space by unmapping
  387. and mapping individual regions. In that case
  388. ::
  389. void dma_iova_unlink(struct device *dev, struct dma_iova_state *state,
  390. size_t offset, size_t size, enum dma_data_direction dir,
  391. unsigned long attrs);
  392. is used to unmap a range previously mapped, and
  393. ::
  394. void dma_iova_free(struct device *dev, struct dma_iova_state *state);
  395. is used to free the IOVA space. All regions must have been unmapped using
  396. ``dma_iova_unlink()`` before calling ``dma_iova_free()``.
  397. Part II - Non-coherent DMA allocations
  398. --------------------------------------
  399. These APIs allow to allocate pages that are guaranteed to be DMA addressable
  400. by the passed in device, but which need explicit management of memory ownership
  401. for the kernel vs the device.
  402. If you don't understand how cache line coherency works between a processor and
  403. an I/O device, you should not be using this part of the API.
  404. ::
  405. struct page *
  406. dma_alloc_pages(struct device *dev, size_t size, dma_addr_t *dma_handle,
  407. enum dma_data_direction dir, gfp_t gfp)
  408. This routine allocates a region of <size> bytes of non-coherent memory. It
  409. returns a pointer to first struct page for the region, or NULL if the
  410. allocation failed. The resulting struct page can be used for everything a
  411. struct page is suitable for.
  412. It also returns a <dma_handle> which may be cast to an unsigned integer the
  413. same width as the bus and given to the device as the DMA address base of
  414. the region.
  415. The dir parameter specified if data is read and/or written by the device,
  416. see dma_map_single() for details.
  417. The gfp parameter allows the caller to specify the ``GFP_`` flags (see
  418. kmalloc()) for the allocation, but rejects flags used to specify a memory
  419. zone such as GFP_DMA or GFP_HIGHMEM.
  420. Before giving the memory to the device, dma_sync_single_for_device() needs
  421. to be called, and before reading memory written by the device,
  422. dma_sync_single_for_cpu(), just like for streaming DMA mappings that are
  423. reused.
  424. ::
  425. void
  426. dma_free_pages(struct device *dev, size_t size, struct page *page,
  427. dma_addr_t dma_handle, enum dma_data_direction dir)
  428. Free a region of memory previously allocated using dma_alloc_pages().
  429. dev, size, dma_handle and dir must all be the same as those passed into
  430. dma_alloc_pages(). page must be the pointer returned by dma_alloc_pages().
  431. ::
  432. int
  433. dma_mmap_pages(struct device *dev, struct vm_area_struct *vma,
  434. size_t size, struct page *page)
  435. Map an allocation returned from dma_alloc_pages() into a user address space.
  436. dev and size must be the same as those passed into dma_alloc_pages().
  437. page must be the pointer returned by dma_alloc_pages().
  438. ::
  439. void *
  440. dma_alloc_noncoherent(struct device *dev, size_t size,
  441. dma_addr_t *dma_handle, enum dma_data_direction dir,
  442. gfp_t gfp)
  443. This routine is a convenient wrapper around dma_alloc_pages that returns the
  444. kernel virtual address for the allocated memory instead of the page structure.
  445. ::
  446. void
  447. dma_free_noncoherent(struct device *dev, size_t size, void *cpu_addr,
  448. dma_addr_t dma_handle, enum dma_data_direction dir)
  449. Free a region of memory previously allocated using dma_alloc_noncoherent().
  450. dev, size, dma_handle and dir must all be the same as those passed into
  451. dma_alloc_noncoherent(). cpu_addr must be the virtual address returned by
  452. dma_alloc_noncoherent().
  453. ::
  454. struct sg_table *
  455. dma_alloc_noncontiguous(struct device *dev, size_t size,
  456. enum dma_data_direction dir, gfp_t gfp,
  457. unsigned long attrs);
  458. This routine allocates <size> bytes of non-coherent and possibly non-contiguous
  459. memory. It returns a pointer to struct sg_table that describes the allocated
  460. and DMA mapped memory, or NULL if the allocation failed. The resulting memory
  461. can be used for struct page mapped into a scatterlist are suitable for.
  462. The return sg_table is guaranteed to have 1 single DMA mapped segment as
  463. indicated by sgt->nents, but it might have multiple CPU side segments as
  464. indicated by sgt->orig_nents.
  465. The dir parameter specified if data is read and/or written by the device,
  466. see dma_map_single() for details.
  467. The gfp parameter allows the caller to specify the ``GFP_`` flags (see
  468. kmalloc()) for the allocation, but rejects flags used to specify a memory
  469. zone such as GFP_DMA or GFP_HIGHMEM.
  470. The attrs argument must be either 0 or DMA_ATTR_ALLOC_SINGLE_PAGES.
  471. Before giving the memory to the device, dma_sync_sgtable_for_device() needs
  472. to be called, and before reading memory written by the device,
  473. dma_sync_sgtable_for_cpu(), just like for streaming DMA mappings that are
  474. reused.
  475. ::
  476. void
  477. dma_free_noncontiguous(struct device *dev, size_t size,
  478. struct sg_table *sgt,
  479. enum dma_data_direction dir)
  480. Free memory previously allocated using dma_alloc_noncontiguous(). dev, size,
  481. and dir must all be the same as those passed into dma_alloc_noncontiguous().
  482. sgt must be the pointer returned by dma_alloc_noncontiguous().
  483. ::
  484. void *
  485. dma_vmap_noncontiguous(struct device *dev, size_t size,
  486. struct sg_table *sgt)
  487. Return a contiguous kernel mapping for an allocation returned from
  488. dma_alloc_noncontiguous(). dev and size must be the same as those passed into
  489. dma_alloc_noncontiguous(). sgt must be the pointer returned by
  490. dma_alloc_noncontiguous().
  491. Once a non-contiguous allocation is mapped using this function, the
  492. flush_kernel_vmap_range() and invalidate_kernel_vmap_range() APIs must be used
  493. to manage the coherency between the kernel mapping, the device and user space
  494. mappings (if any).
  495. ::
  496. void
  497. dma_vunmap_noncontiguous(struct device *dev, void *vaddr)
  498. Unmap a kernel mapping returned by dma_vmap_noncontiguous(). dev must be the
  499. same the one passed into dma_alloc_noncontiguous(). vaddr must be the pointer
  500. returned by dma_vmap_noncontiguous().
  501. ::
  502. int
  503. dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma,
  504. size_t size, struct sg_table *sgt)
  505. Map an allocation returned from dma_alloc_noncontiguous() into a user address
  506. space. dev and size must be the same as those passed into
  507. dma_alloc_noncontiguous(). sgt must be the pointer returned by
  508. dma_alloc_noncontiguous().
  509. ::
  510. int
  511. dma_get_cache_alignment(void)
  512. Returns the processor cache alignment. This is the absolute minimum
  513. alignment *and* width that you must observe when either mapping
  514. memory or doing partial flushes.
  515. .. note::
  516. This API may return a number *larger* than the actual cache
  517. line, but it will guarantee that one or more cache lines fit exactly
  518. into the width returned by this call. It will also always be a power
  519. of two for easy alignment.
  520. Part III - Debug drivers use of the DMA API
  521. -------------------------------------------
  522. The DMA API as described above has some constraints. DMA addresses must be
  523. released with the corresponding function with the same size for example. With
  524. the advent of hardware IOMMUs it becomes more and more important that drivers
  525. do not violate those constraints. In the worst case such a violation can
  526. result in data corruption up to destroyed filesystems.
  527. To debug drivers and find bugs in the usage of the DMA API checking code can
  528. be compiled into the kernel which will tell the developer about those
  529. violations. If your architecture supports it you can select the "Enable
  530. debugging of DMA API usage" option in your kernel configuration. Enabling this
  531. option has a performance impact. Do not enable it in production kernels.
  532. If you boot the resulting kernel will contain code which does some bookkeeping
  533. about what DMA memory was allocated for which device. If this code detects an
  534. error it prints a warning message with some details into your kernel log. An
  535. example warning message may look like this::
  536. WARNING: at /data2/repos/linux-2.6-iommu/lib/dma-debug.c:448
  537. check_unmap+0x203/0x490()
  538. Hardware name:
  539. forcedeth 0000:00:08.0: DMA-API: device driver frees DMA memory with wrong
  540. function [device address=0x00000000640444be] [size=66 bytes] [mapped as
  541. single] [unmapped as page]
  542. Modules linked in: nfsd exportfs bridge stp llc r8169
  543. Pid: 0, comm: swapper Tainted: G W 2.6.28-dmatest-09289-g8bb99c0 #1
  544. Call Trace:
  545. <IRQ> [<ffffffff80240b22>] warn_slowpath+0xf2/0x130
  546. [<ffffffff80647b70>] _spin_unlock+0x10/0x30
  547. [<ffffffff80537e75>] usb_hcd_link_urb_to_ep+0x75/0xc0
  548. [<ffffffff80647c22>] _spin_unlock_irqrestore+0x12/0x40
  549. [<ffffffff8055347f>] ohci_urb_enqueue+0x19f/0x7c0
  550. [<ffffffff80252f96>] queue_work+0x56/0x60
  551. [<ffffffff80237e10>] enqueue_task_fair+0x20/0x50
  552. [<ffffffff80539279>] usb_hcd_submit_urb+0x379/0xbc0
  553. [<ffffffff803b78c3>] cpumask_next_and+0x23/0x40
  554. [<ffffffff80235177>] find_busiest_group+0x207/0x8a0
  555. [<ffffffff8064784f>] _spin_lock_irqsave+0x1f/0x50
  556. [<ffffffff803c7ea3>] check_unmap+0x203/0x490
  557. [<ffffffff803c8259>] debug_dma_unmap_phys+0x49/0x50
  558. [<ffffffff80485f26>] nv_tx_done_optimized+0xc6/0x2c0
  559. [<ffffffff80486c13>] nv_nic_irq_optimized+0x73/0x2b0
  560. [<ffffffff8026df84>] handle_IRQ_event+0x34/0x70
  561. [<ffffffff8026ffe9>] handle_edge_irq+0xc9/0x150
  562. [<ffffffff8020e3ab>] do_IRQ+0xcb/0x1c0
  563. [<ffffffff8020c093>] ret_from_intr+0x0/0xa
  564. <EOI> <4>---[ end trace f6435a98e2a38c0e ]---
  565. The driver developer can find the driver and the device including a stacktrace
  566. of the DMA API call which caused this warning.
  567. Per default only the first error will result in a warning message. All other
  568. errors will only silently counted. This limitation exist to prevent the code
  569. from flooding your kernel log. To support debugging a device driver this can
  570. be disabled via debugfs. See the debugfs interface documentation below for
  571. details.
  572. The debugfs directory for the DMA API debugging code is called dma-api/. In
  573. this directory the following files can currently be found:
  574. =============================== ===============================================
  575. dma-api/all_errors This file contains a numeric value. If this
  576. value is not equal to zero the debugging code
  577. will print a warning for every error it finds
  578. into the kernel log. Be careful with this
  579. option, as it can easily flood your logs.
  580. dma-api/disabled This read-only file contains the character 'Y'
  581. if the debugging code is disabled. This can
  582. happen when it runs out of memory or if it was
  583. disabled at boot time
  584. dma-api/dump This read-only file contains current DMA
  585. mappings.
  586. dma-api/error_count This file is read-only and shows the total
  587. numbers of errors found.
  588. dma-api/num_errors The number in this file shows how many
  589. warnings will be printed to the kernel log
  590. before it stops. This number is initialized to
  591. one at system boot and be set by writing into
  592. this file
  593. dma-api/min_free_entries This read-only file can be read to get the
  594. minimum number of free dma_debug_entries the
  595. allocator has ever seen. If this value goes
  596. down to zero the code will attempt to increase
  597. nr_total_entries to compensate.
  598. dma-api/num_free_entries The current number of free dma_debug_entries
  599. in the allocator.
  600. dma-api/nr_total_entries The total number of dma_debug_entries in the
  601. allocator, both free and used.
  602. dma-api/driver_filter You can write a name of a driver into this file
  603. to limit the debug output to requests from that
  604. particular driver. Write an empty string to
  605. that file to disable the filter and see
  606. all errors again.
  607. =============================== ===============================================
  608. If you have this code compiled into your kernel it will be enabled by default.
  609. If you want to boot without the bookkeeping anyway you can provide
  610. 'dma_debug=off' as a boot parameter. This will disable DMA API debugging.
  611. Notice that you can not enable it again at runtime. You have to reboot to do
  612. so.
  613. If you want to see debug messages only for a special device driver you can
  614. specify the dma_debug_driver=<drivername> parameter. This will enable the
  615. driver filter at boot time. The debug code will only print errors for that
  616. driver afterwards. This filter can be disabled or changed later using debugfs.
  617. When the code disables itself at runtime this is most likely because it ran
  618. out of dma_debug_entries and was unable to allocate more on-demand. 65536
  619. entries are preallocated at boot - if this is too low for you boot with
  620. 'dma_debug_entries=<your_desired_number>' to overwrite the default. Note
  621. that the code allocates entries in batches, so the exact number of
  622. preallocated entries may be greater than the actual number requested. The
  623. code will print to the kernel log each time it has dynamically allocated
  624. as many entries as were initially preallocated. This is to indicate that a
  625. larger preallocation size may be appropriate, or if it happens continually
  626. that a driver may be leaking mappings.
  627. ::
  628. void
  629. debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr);
  630. dma-debug interface debug_dma_mapping_error() to debug drivers that fail
  631. to check DMA mapping errors on addresses returned by dma_map_single() and
  632. dma_map_page() interfaces. This interface clears a flag set by
  633. debug_dma_map_phys() to indicate that dma_mapping_error() has been called by
  634. the driver. When driver does unmap, debug_dma_unmap() checks the flag and if
  635. this flag is still set, prints warning message that includes call trace that
  636. leads up to the unmap. This interface can be called from dma_mapping_error()
  637. routines to enable DMA mapping error check debugging.
  638. Functions and structures
  639. ========================
  640. .. kernel-doc:: include/linux/scatterlist.h
  641. .. kernel-doc:: lib/scatterlist.c