dma-api.rst 31 KB

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