DMA-API.txt 28 KB

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