lsphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget$/translations/zh_CN/core-api/dma-apimodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget$/translations/zh_TW/core-api/dma-apimodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget$/translations/it_IT/core-api/dma-apimodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget$/translations/ja_JP/core-api/dma-apimodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget$/translations/ko_KR/core-api/dma-apimodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget$/translations/sp_SP/core-api/dma-apimodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(h,Dynamic DMA mapping using the generic deviceh]h,Dynamic DMA mapping using the generic device}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhh>/var/lib/git/docbuild/linux/Documentation/core-api/dma-api.rsthKubh field_list)}(hhh]hfield)}(hhh](h field_name)}(hAuthorh]hAuthor}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh field_body)}(h=James E.J. Bottomley h]h paragraph)}(hh](hJames E.J. Bottomley <}(hhhhhNhNubh reference)}(h%James.Bottomley@HansenPartnership.comh]h%James.Bottomley@HansenPartnership.com}(hhhhhNhNubah}(h]h ]h"]h$]h&]refuri,mailto:James.Bottomley@HansenPartnership.comuh1hhhubh>}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh)}(hThis document describes the DMA API. For a more gentle introduction of the API (and actual examples), see Documentation/core-api/dma-api-howto.rst.h]hThis document describes the DMA API. For a more gentle introduction of the API (and actual examples), see Documentation/core-api/dma-api-howto.rst.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hX=This API is split into two pieces. Part I describes the basic API. Part II describes extensions for supporting non-consistent memory machines. Unless you know that your driver absolutely has to support non-consistent platforms (this is usually only legacy platforms) you should only use the API described in part I.h]hX=This API is split into two pieces. Part I describes the basic API. Part II describes extensions for supporting non-consistent memory machines. Unless you know that your driver absolutely has to support non-consistent platforms (this is usually only legacy platforms) you should only use the API described in part I.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hhhhubh)}(hhh](h)}(hPart I - dma_APIh]hPart I - dma_API}(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj+hhhhhKubh)}(hzTo get the dma_API, you must #include . This provides dma_addr_t and the interfaces described below.h]hzTo get the dma_API, you must #include . This provides dma_addr_t and the interfaces described below.}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj+hhubh)}(hX A dma_addr_t can hold any valid DMA address for the platform. It can be given to a device to use as a DMA source or target. A CPU cannot reference a dma_addr_t directly because there may be translation between its physical address space and the DMA address space.h]hX A dma_addr_t can hold any valid DMA address for the platform. It can be given to a device to use as a DMA source or target. A CPU cannot reference a dma_addr_t directly because there may be translation between its physical address space and the DMA address space.}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj+hhubeh}(h]part-i-dma-apiah ]h"]part i - dma_apiah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h*Part Ia - Using large DMA-coherent buffersh]h*Part Ia - Using large DMA-coherent buffers}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1hhj`hhhhhKubh literal_block)}(hqvoid * dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag)h]hqvoid * dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag)}hjssbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1jqhhhK hj`hhubh)}(hX-Consistent memory is memory for which a write by either the device or the processor can immediately be read by the processor or device without having to worry about caching effects. (You may however need to make sure to flush the processor's write buffers before telling devices to read that memory.)h]hX/Consistent memory is memory for which a write by either the device or the processor can immediately be read by the processor or device without having to worry about caching effects. (You may however need to make sure to flush the processor’s write buffers before telling devices to read that memory.)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK$hj`hhubh)}(hEThis routine allocates a region of bytes of consistent memory.h]hEThis routine allocates a region of bytes of consistent memory.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK*hj`hhubh)}(hyIt returns a pointer to the allocated region (in the processor's virtual address space) or NULL if the allocation failed.h]h{It returns a pointer to the allocated region (in the processor’s virtual address space) or NULL if the allocation failed.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK,hj`hhubh)}(hIt also returns a which may be cast to an unsigned integer the same width as the bus and given to the device as the DMA address base of the region.h]hIt also returns a which may be cast to an unsigned integer the same width as the bus and given to the device as the DMA address base of the region.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK/hj`hhubh)}(hXNote: consistent memory can be expensive on some platforms, and the minimum allocation length may be as big as a page, so you should consolidate your requests for consistent memory as much as possible. The simplest way to do that is to use the dma_pool calls (see below).h]hXNote: consistent memory can be expensive on some platforms, and the minimum allocation length may be as big as a page, so you should consolidate your requests for consistent memory as much as possible. The simplest way to do that is to use the dma_pool calls (see below).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK3hj`hhubh)}(hThe flag parameter (dma_alloc_coherent() only) allows the caller to specify the ``GFP_`` flags (see kmalloc()) for the allocation (the implementation may choose to ignore flags that affect the location of the returned memory, like GFP_DMA).h](hPThe flag parameter (dma_alloc_coherent() only) allows the caller to specify the }(hjhhhNhNubhliteral)}(h``GFP_``h]hGFP_}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh flags (see kmalloc()) for the allocation (the implementation may choose to ignore flags that affect the location of the returned memory, like GFP_DMA).}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK8hj`hhubjr)}(hpvoid dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_handle)h]hpvoid dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_handle)}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhK?hj`hhubh)}(hFree a region of consistent memory you previously allocated. dev, size and dma_handle must all be the same as those passed into dma_alloc_coherent(). cpu_addr must be the virtual address returned by the dma_alloc_coherent().h]hFree a region of consistent memory you previously allocated. dev, size and dma_handle must all be the same as those passed into dma_alloc_coherent(). cpu_addr must be the virtual address returned by the dma_alloc_coherent().}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKChj`hhubh)}(heNote that unlike their sibling allocation calls, these routines may only be called with IRQs enabled.h]heNote that unlike their sibling allocation calls, these routines may only be called with IRQs enabled.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKHhj`hhubeh}(h](part-ia-using-large-dma-coherent-buffersah ]h"]*part ia - using large dma-coherent buffersah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h*Part Ib - Using small DMA-coherent buffersh]h*Part Ib - Using small DMA-coherent buffers}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKMubh)}(hDTo get this part of the dma_API, you must #include h]hDTo get this part of the dma_API, you must #include }(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKOhjhhubh)}(hXMany drivers need lots of small DMA-coherent memory regions for DMA descriptors or I/O buffers. Rather than allocating in units of a page or more using dma_alloc_coherent(), you can use DMA pools. These work much like a struct kmem_cache, except that they use the DMA-coherent allocator, not __get_free_pages(). Also, they understand common hardware constraints for alignment, like queue heads needing to be aligned on N-byte boundaries.h]hXMany drivers need lots of small DMA-coherent memory regions for DMA descriptors or I/O buffers. Rather than allocating in units of a page or more using dma_alloc_coherent(), you can use DMA pools. These work much like a struct kmem_cache, except that they use the DMA-coherent allocator, not __get_free_pages(). Also, they understand common hardware constraints for alignment, like queue heads needing to be aligned on N-byte boundaries.}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKQhjhhubjr)}(hstruct dma_pool * dma_pool_create(const char *name, struct device *dev, size_t size, size_t align, size_t alloc);h]hstruct dma_pool * dma_pool_create(const char *name, struct device *dev, size_t size, size_t align, size_t alloc);}hjJsbah}(h]h ]h"]h$]h&]jjuh1jqhhhK[hjhhubh)}(hdma_pool_create() initializes a pool of DMA-coherent buffers for use with a given device. It must be called in a context which can sleep.h]hdma_pool_create() initializes a pool of DMA-coherent buffers for use with a given device. It must be called in a context which can sleep.}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK_hjhhubh)}(hXThe "name" is for diagnostics (like a struct kmem_cache name); dev and size are like what you'd pass to dma_alloc_coherent(). The device's hardware alignment requirement for this type of data is "align" (which is expressed in bytes, and must be a power of two). If your device has no boundary crossing restrictions, pass 0 for alloc; passing 4096 says memory allocated from this pool must not cross 4KByte boundaries.h]hXThe “name” is for diagnostics (like a struct kmem_cache name); dev and size are like what you’d pass to dma_alloc_coherent(). The device’s hardware alignment requirement for this type of data is “align” (which is expressed in bytes, and must be a power of two). If your device has no boundary crossing restrictions, pass 0 for alloc; passing 4096 says memory allocated from this pool must not cross 4KByte boundaries.}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKchjhhubjr)}(hbvoid * dma_pool_zalloc(struct dma_pool *pool, gfp_t mem_flags, dma_addr_t *handle)h]hbvoid * dma_pool_zalloc(struct dma_pool *pool, gfp_t mem_flags, dma_addr_t *handle)}hjtsbah}(h]h ]h"]h$]h&]jjuh1jqhhhKlhjhhubh)}(h_Wraps dma_pool_alloc() and also zeroes the returned memory if the allocation attempt succeeded.h]h_Wraps dma_pool_alloc() and also zeroes the returned memory if the allocation attempt succeeded.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKphjhhubjr)}(hevoid * dma_pool_alloc(struct dma_pool *pool, gfp_t gfp_flags, dma_addr_t *dma_handle);h]hevoid * dma_pool_alloc(struct dma_pool *pool, gfp_t gfp_flags, dma_addr_t *dma_handle);}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhKvhjhhubh)}(hXThis allocates memory from the pool; the returned memory will meet the size and alignment requirements specified at creation time. Pass GFP_ATOMIC to prevent blocking, or if it's permitted (not in_interrupt, not holding SMP locks), pass GFP_KERNEL to allow blocking. Like dma_alloc_coherent(), this returns two values: an address usable by the CPU, and the DMA address usable by the pool's device.h]hXThis allocates memory from the pool; the returned memory will meet the size and alignment requirements specified at creation time. Pass GFP_ATOMIC to prevent blocking, or if it’s permitted (not in_interrupt, not holding SMP locks), pass GFP_KERNEL to allow blocking. Like dma_alloc_coherent(), this returns two values: an address usable by the CPU, and the DMA address usable by the pool’s device.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKzhjhhubjr)}(hVvoid dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t addr);h]hVvoid dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t addr);}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhKhjhhubh)}(hThis puts memory back into the pool. The pool is what was passed to dma_pool_alloc(); the CPU (vaddr) and DMA addresses are what were returned when that routine allocated the memory being freed.h]hThis puts memory back into the pool. The pool is what was passed to dma_pool_alloc(); the CPU (vaddr) and DMA addresses are what were returned when that routine allocated the memory being freed.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjr)}(h-void dma_pool_destroy(struct dma_pool *pool);h]h-void dma_pool_destroy(struct dma_pool *pool);}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhKhjhhubh)}(hdma_pool_destroy() frees the resources of the pool. It must be called in a context which can sleep. Make sure you've freed all allocated memory back to the pool before you destroy it.h]hdma_pool_destroy() frees the resources of the pool. It must be called in a context which can sleep. Make sure you’ve freed all allocated memory back to the pool before you destroy it.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h](part-ib-using-small-dma-coherent-buffersah ]h"]*part ib - using small dma-coherent buffersah$]h&]uh1hhhhhhhhKMubh)}(hhh](h)}(h$Part Ic - DMA addressing limitationsh]h$Part Ic - DMA addressing limitations}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubjr)}(h;int dma_set_mask_and_coherent(struct device *dev, u64 mask)h]h;int dma_set_mask_and_coherent(struct device *dev, u64 mask)}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhKhjhhubh)}(hqChecks to see if the mask is possible and updates the device streaming and coherent DMA mask parameters if it is.h]hqChecks to see if the mask is possible and updates the device streaming and coherent DMA mask parameters if it is.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h5Returns: 0 if successful and a negative error if not.h]h5Returns: 0 if successful and a negative error if not.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjr)}(h.int dma_set_mask(struct device *dev, u64 mask)h]h.int dma_set_mask(struct device *dev, u64 mask)}hj'sbah}(h]h ]h"]h$]h&]jjuh1jqhhhKhjhhubh)}(hQChecks to see if the mask is possible and updates the device parameters if it is.h]hQChecks to see if the mask is possible and updates the device parameters if it is.}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h5Returns: 0 if successful and a negative error if not.h]h5Returns: 0 if successful and a negative error if not.}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjr)}(h7int dma_set_coherent_mask(struct device *dev, u64 mask)h]h7int dma_set_coherent_mask(struct device *dev, u64 mask)}hjQsbah}(h]h ]h"]h$]h&]jjuh1jqhhhKhjhhubh)}(hQChecks to see if the mask is possible and updates the device parameters if it is.h]hQChecks to see if the mask is possible and updates the device parameters if it is.}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h5Returns: 0 if successful and a negative error if not.h]h5Returns: 0 if successful and a negative error if not.}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjr)}(h-u64 dma_get_required_mask(struct device *dev)h]h-u64 dma_get_required_mask(struct device *dev)}hj{sbah}(h]h ]h"]h$]h&]jjuh1jqhhhKhjhhubh)}(hX%This API returns the mask that the platform requires to operate efficiently. Usually this means the returned mask is the minimum required to cover all of memory. Examining the required mask gives drivers with variable descriptor sizes the opportunity to use smaller descriptors as necessary.h]hX%This API returns the mask that the platform requires to operate efficiently. Usually this means the returned mask is the minimum required to cover all of memory. Examining the required mask gives drivers with variable descriptor sizes the opportunity to use smaller descriptors as necessary.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hRequesting the required mask does not alter the current mask. If you wish to take advantage of it, you should issue a dma_set_mask() call to set the mask to the value returned.h]hRequesting the required mask does not alter the current mask. If you wish to take advantage of it, you should issue a dma_set_mask() call to set the mask to the value returned.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjr)}(h0size_t dma_max_mapping_size(struct device *dev);h]h0size_t dma_max_mapping_size(struct device *dev);}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhKhjhhubh)}(hReturns the maximum size of a mapping for the device. The size parameter of the mapping functions like dma_map_single(), dma_map_page() and others should not be larger than the returned value.h]hReturns the maximum size of a mapping for the device. The size parameter of the mapping functions like dma_map_single(), dma_map_page() and others should not be larger than the returned value.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjr)}(h0size_t dma_opt_mapping_size(struct device *dev);h]h0size_t dma_opt_mapping_size(struct device *dev);}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhKhjhhubh)}(h=Returns the maximum optimal size of a mapping for the device.h]h=Returns the maximum optimal size of a mapping for the device.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXMapping larger buffers may take much longer in certain scenarios. In addition, for high-rate short-lived streaming mappings, the upfront time spent on the mapping may account for an appreciable part of the total request lifetime. As such, if splitting larger requests incurs no significant performance penalty, then device drivers are advised to limit total DMA streaming mappings length to the returned value.h]hXMapping larger buffers may take much longer in certain scenarios. In addition, for high-rate short-lived streaming mappings, the upfront time spent on the mapping may account for an appreciable part of the total request lifetime. As such, if splitting larger requests incurs no significant performance penalty, then device drivers are advised to limit total DMA streaming mappings length to the returned value.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjr)}(h and parameters are provided to do partial page mapping, it is recommended that you never use these unless you really know what the cache width is.h]hX"API for mapping and unmapping for pages. All the notes and warnings for the other mapping APIs apply here. Also, although the and parameters are provided to do partial page mapping, it is recommended that you never use these unless you really know what the cache width is.}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMVhj+hhubjr)}(hX&dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size, enum dma_data_direction dir, unsigned long attrs)h]hX&dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size, enum dma_data_direction dir, unsigned long attrs)}hjDsbah}(h]h ]h"]h$]h&]jjuh1jqhhhM^hj+hhubh)}(hAPI for mapping and unmapping for MMIO resources. All the notes and warnings for the other mapping APIs apply here. The API should only be used to map device MMIO resources, mapping of RAM is not permitted.h]hAPI for mapping and unmapping for MMIO resources. All the notes and warnings for the other mapping APIs apply here. The API should only be used to map device MMIO resources, mapping of RAM is not permitted.}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMfhj+hhubjr)}(h>int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)h]h>int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)}hj`sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMlhj+hhubh)}(hXIn some circumstances dma_map_single(), dma_map_page() and dma_map_resource() will fail to create a mapping. A driver can check for these errors by testing the returned DMA address with dma_mapping_error(). A non-zero return value means the mapping could not be created and the driver should take appropriate action (e.g. reduce current DMA mapping usage or delay and try again later).h]hXIn some circumstances dma_map_single(), dma_map_page() and dma_map_resource() will fail to create a mapping. A driver can check for these errors by testing the returned DMA address with dma_mapping_error(). A non-zero return value means the mapping could not be created and the driver should take appropriate action (e.g. reduce current DMA mapping usage or delay and try again later).}(hjnhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMohj+hhubjr)}(hsint dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction)h]hsint dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction)}hj|sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMwhj+hhubh)}(hReturns: the number of DMA address segments mapped (this may be shorter than passed in if some elements of the scatter/gather list are physically or virtually adjacent and an IOMMU maps them with a single entry).h]hReturns: the number of DMA address segments mapped (this may be shorter than passed in if some elements of the scatter/gather list are physically or virtually adjacent and an IOMMU maps them with a single entry).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM{hj+hhubh)}(hPlease note that the sg cannot be mapped again if it has been mapped once. The mapping process is allowed to destroy information in the sg.h]hPlease note that the sg cannot be mapped again if it has been mapped once. The mapping process is allowed to destroy information in the sg.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubh)}(hX/As with the other mapping interfaces, dma_map_sg() can fail. When it does, 0 is returned and a driver must take appropriate action. It is critical that the driver do something, in the case of a block driver aborting the request or even oopsing is better than doing nothing and corrupting the filesystem.h]hX/As with the other mapping interfaces, dma_map_sg() can fail. When it does, 0 is returned and a driver must take appropriate action. It is critical that the driver do something, in the case of a block driver aborting the request or even oopsing is better than doing nothing and corrupting the filesystem.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubh)}(haddress and sg->length as shown above.h]hThen you should loop count times (note: this can be less than nents times) and use sg_dma_address() and sg_dma_len() macros where you previously accessed sg->address and sg->length as shown above.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubjr)}(hxvoid dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction)h]hxvoid dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction)}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhj+hhubh)}(hUnmap the previously mapped scatter/gather list. All the parameters must be the same as those and passed in to the scatter/gather mapping API.h]hUnmap the previously mapped scatter/gather list. All the parameters must be the same as those and passed in to the scatter/gather mapping API.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubh)}(haNote: must be the number you passed in, *not* the number of DMA address entries returned.h](h0Note: must be the number you passed in, }(hjhhhNhNubj)}(h*not*h]hnot}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, the number of DMA address entries returned.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubjr)}(hXvoid dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction direction) void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction direction) void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction) void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction)h]hXvoid dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction direction) void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction direction) void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction) void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction)}hj6sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhj+hhubh)}(hXQSynchronise a single contiguous or scatter/gather mapping for the CPU and device. With the sync_sg API, all the parameters must be the same as those passed into the sg mapping API. With the sync_single API, you can use dma_handle and size parameters that aren't identical to those passed into the single mapping API to do a partial sync.h]hXSSynchronise a single contiguous or scatter/gather mapping for the CPU and device. With the sync_sg API, all the parameters must be the same as those passed into the sg mapping API. With the sync_single API, you can use dma_handle and size parameters that aren’t identical to those passed into the single mapping API to do a partial sync.}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubj)}(hX=You must do this: - Before reading values that have been written by DMA from the device (use the DMA_FROM_DEVICE direction) - After writing values that will be written to the device using DMA (use the DMA_TO_DEVICE) direction - before *and* after handing memory to the device if the memory is DMA_BIDIRECTIONALh](h)}(hYou must do this:h]hYou must do this:}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjRubh bullet_list)}(hhh](h list_item)}(hgBefore reading values that have been written by DMA from the device (use the DMA_FROM_DEVICE direction)h]h)}(hgBefore reading values that have been written by DMA from the device (use the DMA_FROM_DEVICE direction)h]hgBefore reading values that have been written by DMA from the device (use the DMA_FROM_DEVICE direction)}(hjohhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjkubah}(h]h ]h"]h$]h&]uh1jihjfubjj)}(hcAfter writing values that will be written to the device using DMA (use the DMA_TO_DEVICE) directionh]h)}(hcAfter writing values that will be written to the device using DMA (use the DMA_TO_DEVICE) directionh]hcAfter writing values that will be written to the device using DMA (use the DMA_TO_DEVICE) direction}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jihjfubjj)}(hRbefore *and* after handing memory to the device if the memory is DMA_BIDIRECTIONALh]h)}(hRbefore *and* after handing memory to the device if the memory is DMA_BIDIRECTIONALh](hbefore }(hjhhhNhNubj)}(h*and*h]hand}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhF after handing memory to the device if the memory is DMA_BIDIRECTIONAL}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jihjfubeh}(h]h ]h"]h$]h&]bullet-uh1jdhhhMhjRubeh}(h]h ]h"]h$]h&]uh1j~hj+hhhNhNubh)}(hSee also dma_map_single().h]hSee also dma_map_single().}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubjr)}(hXdma_addr_t dma_map_single_attrs(struct device *dev, void *cpu_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) void dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) int dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir, unsigned long attrs) void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir, unsigned long attrs)h]hXdma_addr_t dma_map_single_attrs(struct device *dev, void *cpu_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) void dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) int dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir, unsigned long attrs) void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir, unsigned long attrs)}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhj+hhubh)}(hThe four functions above are just like the counterpart functions without the _attrs suffixes, except that they pass an optional dma_attrs.h]hThe four functions above are just like the counterpart functions without the _attrs suffixes, except that they pass an optional dma_attrs.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubh)}(hThe interpretation of DMA attributes is architecture-specific, and each attribute should be documented in Documentation/core-api/dma-attributes.rst.h]hThe interpretation of DMA attributes is architecture-specific, and each attribute should be documented in Documentation/core-api/dma-attributes.rst.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubh)}(hIf dma_attrs are 0, the semantics of each of these functions is identical to those of the corresponding function without the _attrs suffix. As a result dma_map_single_attrs() can generally replace dma_map_single(), etc.h]hIf dma_attrs are 0, the semantics of each of these functions is identical to those of the corresponding function without the _attrs suffix. As a result dma_map_single_attrs() can generally replace dma_map_single(), etc.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubh)}(hAs an example of the use of the ``*_attrs`` functions, here's how you could pass an attribute DMA_ATTR_FOO when mapping memory for DMA::h](h As an example of the use of the }(hjhhhNhNubj)}(h ``*_attrs``h]h*_attrs}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh^ functions, here’s how you could pass an attribute DMA_ATTR_FOO when mapping memory for DMA:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubjr)}(hX4#include /* DMA_ATTR_FOO should be defined in linux/dma-mapping.h and * documented in Documentation/core-api/dma-attributes.rst */ ... unsigned long attr; attr |= DMA_ATTR_FOO; .... n = dma_map_sg_attrs(dev, sg, nents, DMA_TO_DEVICE, attr); ....h]hX4#include /* DMA_ATTR_FOO should be defined in linux/dma-mapping.h and * documented in Documentation/core-api/dma-attributes.rst */ ... unsigned long attr; attr |= DMA_ATTR_FOO; .... n = dma_map_sg_attrs(dev, sg, nents, DMA_TO_DEVICE, attr); ....}hj9sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhj+hhubh)}(hArchitectures that care about DMA_ATTR_FOO would check for its presence in their implementations of the mapping and unmapping routines, e.g.:::h]hArchitectures that care about DMA_ATTR_FOO would check for its presence in their implementations of the mapping and unmapping routines, e.g.::}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubjr)}(hX+void whizco_dma_map_sg_attrs(struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) { .... if (attrs & DMA_ATTR_FOO) /* twizzle the frobnozzle */ .... }h]hX+void whizco_dma_map_sg_attrs(struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) { .... if (attrs & DMA_ATTR_FOO) /* twizzle the frobnozzle */ .... }}hjUsbah}(h]h ]h"]h$]h&]jjuh1jqhhhM hj+hhubeh}(h]part-id-streaming-dma-mappingsah ]h"] part id - streaming dma mappingsah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h&Part II - Non-coherent DMA allocationsh]h&Part II - Non-coherent DMA allocations}(hjnhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjkhhhhhMubh)}(hThese APIs allow to allocate pages that are guaranteed to be DMA addressable by the passed in device, but which need explicit management of memory ownership for the kernel vs the device.h]hThese APIs allow to allocate pages that are guaranteed to be DMA addressable by the passed in device, but which need explicit management of memory ownership for the kernel vs the device.}(hj|hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjkhhubh)}(hIf you don't understand how cache line coherency works between a processor and an I/O device, you should not be using this part of the API.h]hIf you don’t understand how cache line coherency works between a processor and an I/O device, you should not be using this part of the API.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjkhhubjr)}(hstruct page * dma_alloc_pages(struct device *dev, size_t size, dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)h]hstruct page * dma_alloc_pages(struct device *dev, size_t size, dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhM"hjkhhubh)}(hThis routine allocates a region of bytes of non-coherent memory. It returns a pointer to first struct page for the region, or NULL if the allocation failed. The resulting struct page can be used for everything a struct page is suitable for.h]hThis routine allocates a region of bytes of non-coherent memory. It returns a pointer to first struct page for the region, or NULL if the allocation failed. The resulting struct page can be used for everything a struct page is suitable for.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM&hjkhhubh)}(hIt also returns a which may be cast to an unsigned integer the same width as the bus and given to the device as the DMA address base of the region.h]hIt also returns a which may be cast to an unsigned integer the same width as the bus and given to the device as the DMA address base of the region.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM+hjkhhubh)}(hkThe dir parameter specified if data is read and/or written by the device, see dma_map_single() for details.h]hkThe dir parameter specified if data is read and/or written by the device, see dma_map_single() for details.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM/hjkhhubh)}(hThe gfp parameter allows the caller to specify the ``GFP_`` flags (see kmalloc()) for the allocation, but rejects flags used to specify a memory zone such as GFP_DMA or GFP_HIGHMEM.h](h3The gfp parameter allows the caller to specify the }(hjhhhNhNubj)}(h``GFP_``h]hGFP_}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhz flags (see kmalloc()) for the allocation, but rejects flags used to specify a memory zone such as GFP_DMA or GFP_HIGHMEM.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM2hjkhhubh)}(hBefore giving the memory to the device, dma_sync_single_for_device() needs to be called, and before reading memory written by the device, dma_sync_single_for_cpu(), just like for streaming DMA mappings that are reused.h]hBefore giving the memory to the device, dma_sync_single_for_device() needs to be called, and before reading memory written by the device, dma_sync_single_for_cpu(), just like for streaming DMA mappings that are reused.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM6hjkhhubjr)}(hvoid dma_free_pages(struct device *dev, size_t size, struct page *page, dma_addr_t dma_handle, enum dma_data_direction dir)h]hvoid dma_free_pages(struct device *dev, size_t size, struct page *page, dma_addr_t dma_handle, enum dma_data_direction dir)}hjsbah}(h]h ]h"]h$]h&]jjuh1jqhhhM=hjkhhubh)}(hFree a region of memory previously allocated using dma_alloc_pages(). dev, size, dma_handle and dir must all be the same as those passed into dma_alloc_pages(). page must be the pointer returned by dma_alloc_pages().h]hFree a region of memory previously allocated using dma_alloc_pages(). dev, size, dma_handle and dir must all be the same as those passed into dma_alloc_pages(). page must be the pointer returned by dma_alloc_pages().}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMAhjkhhubjr)}(hqint dma_mmap_pages(struct device *dev, struct vm_area_struct *vma, size_t size, struct page *page)h]hqint dma_mmap_pages(struct device *dev, struct vm_area_struct *vma, size_t size, struct page *page)}hj sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMGhjkhhubh)}(hMap an allocation returned from dma_alloc_pages() into a user address space. dev and size must be the same as those passed into dma_alloc_pages(). page must be the pointer returned by dma_alloc_pages().h]hMap an allocation returned from dma_alloc_pages() into a user address space. dev and size must be the same as those passed into dma_alloc_pages(). page must be the pointer returned by dma_alloc_pages().}(hj( hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMKhjkhhubjr)}(hvoid * dma_alloc_noncoherent(struct device *dev, size_t size, dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)h]hvoid * dma_alloc_noncoherent(struct device *dev, size_t size, dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)}hj6 sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMQhjkhhubh)}(hThis routine is a convenient wrapper around dma_alloc_pages that returns the kernel virtual address for the allocated memory instead of the page structure.h]hThis routine is a convenient wrapper around dma_alloc_pages that returns the kernel virtual address for the allocated memory instead of the page structure.}(hjD hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMVhjkhhubjr)}(hvoid dma_free_noncoherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_handle, enum dma_data_direction dir)h]hvoid dma_free_noncoherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_handle, enum dma_data_direction dir)}hjR sbah}(h]h ]h"]h$]h&]jjuh1jqhhhM[hjkhhubh)}(hFree a region of memory previously allocated using dma_alloc_noncoherent(). dev, size, dma_handle and dir must all be the same as those passed into dma_alloc_noncoherent(). cpu_addr must be the virtual address returned by dma_alloc_noncoherent().h]hFree a region of memory previously allocated using dma_alloc_noncoherent(). dev, size, dma_handle and dir must all be the same as those passed into dma_alloc_noncoherent(). cpu_addr must be the virtual address returned by dma_alloc_noncoherent().}(hj` hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM_hjkhhubjr)}(hstruct sg_table * dma_alloc_noncontiguous(struct device *dev, size_t size, enum dma_data_direction dir, gfp_t gfp, unsigned long attrs);h]hstruct sg_table * dma_alloc_noncontiguous(struct device *dev, size_t size, enum dma_data_direction dir, gfp_t gfp, unsigned long attrs);}hjn sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMfhjkhhubh)}(hX4This routine allocates bytes of non-coherent and possibly non-contiguous memory. It returns a pointer to struct sg_table that describes the allocated and DMA mapped memory, or NULL if the allocation failed. The resulting memory can be used for struct page mapped into a scatterlist are suitable for.h]hX4This routine allocates bytes of non-coherent and possibly non-contiguous memory. It returns a pointer to struct sg_table that describes the allocated and DMA mapped memory, or NULL if the allocation failed. The resulting memory can be used for struct page mapped into a scatterlist are suitable for.}(hj| hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMkhjkhhubh)}(hThe return sg_table is guaranteed to have 1 single DMA mapped segment as indicated by sgt->nents, but it might have multiple CPU side segments as indicated by sgt->orig_nents.h]hThe return sg_table is guaranteed to have 1 single DMA mapped segment as indicated by sgt->nents, but it might have multiple CPU side segments as indicated by sgt->orig_nents.xm}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMphjkhhubh)}(hkThe dir parameter specified if data is read and/or written by the device, see dma_map_single() for details.h]hkThe dir parameter specified if data is read and/or written by the device, see dma_map_single() for details.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMthjkhhubh)}(hThe gfp parameter allows the caller to specify the ``GFP_`` flags (see kmalloc()) for the allocation, but rejects flags used to specify a memory zone such as GFP_DMA or GFP_HIGHMEM.h](h3The gfp parameter allows the caller to specify the }(hj hhhNhNubj)}(h``GFP_``h]hGFP_}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubhz flags (see kmalloc()) for the allocation, but rejects flags used to specify a memory zone such as GFP_DMA or GFP_HIGHMEM.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMwhjkhhubh)}(hCThe attrs argument must be either 0 or DMA_ATTR_ALLOC_SINGLE_PAGES.h]hCThe attrs argument must be either 0 or DMA_ATTR_ALLOC_SINGLE_PAGES.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM{hjkhhubh)}(hBefore giving the memory to the device, dma_sync_sgtable_for_device() needs to be called, and before reading memory written by the device, dma_sync_sgtable_for_cpu(), just like for streaming DMA mappings that are reused.h]hBefore giving the memory to the device, dma_sync_sgtable_for_device() needs to be called, and before reading memory written by the device, dma_sync_sgtable_for_cpu(), just like for streaming DMA mappings that are reused.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM}hjkhhubjr)}(hvoid dma_free_noncontiguous(struct device *dev, size_t size, struct sg_table *sgt, enum dma_data_direction dir)h]hvoid dma_free_noncontiguous(struct device *dev, size_t size, struct sg_table *sgt, enum dma_data_direction dir)}hj sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhjkhhubh)}(hFree memory previously allocated using dma_alloc_noncontiguous(). dev, size, and dir must all be the same as those passed into dma_alloc_noncontiguous(). sgt must be the pointer returned by dma_alloc_noncontiguous().h]hFree memory previously allocated using dma_alloc_noncontiguous(). dev, size, and dir must all be the same as those passed into dma_alloc_noncontiguous(). sgt must be the pointer returned by dma_alloc_noncontiguous().}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjkhhubjr)}(h\void * dma_vmap_noncontiguous(struct device *dev, size_t size, struct sg_table *sgt)h]h\void * dma_vmap_noncontiguous(struct device *dev, size_t size, struct sg_table *sgt)}hj sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhjkhhubh)}(hReturn a contiguous kernel mapping for an allocation returned from dma_alloc_noncontiguous(). dev and size must be the same as those passed into dma_alloc_noncontiguous(). sgt must be the pointer returned by dma_alloc_noncontiguous().h]hReturn a contiguous kernel mapping for an allocation returned from dma_alloc_noncontiguous(). dev and size must be the same as those passed into dma_alloc_noncontiguous(). sgt must be the pointer returned by dma_alloc_noncontiguous().}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjkhhubh)}(hOnce a non-contiguous allocation is mapped using this function, the flush_kernel_vmap_range() and invalidate_kernel_vmap_range() APIs must be used to manage the coherency between the kernel mapping, the device and user space mappings (if any).h]hOnce a non-contiguous allocation is mapped using this function, the flush_kernel_vmap_range() and invalidate_kernel_vmap_range() APIs must be used to manage the coherency between the kernel mapping, the device and user space mappings (if any).}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjkhhubjr)}(h>void dma_vunmap_noncontiguous(struct device *dev, void *vaddr)h]h>void dma_vunmap_noncontiguous(struct device *dev, void *vaddr)}hj( sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhjkhhubh)}(hUnmap a kernel mapping returned by dma_vmap_noncontiguous(). dev must be the same the one passed into dma_alloc_noncontiguous(). vaddr must be the pointer returned by dma_vmap_noncontiguous().h]hUnmap a kernel mapping returned by dma_vmap_noncontiguous(). dev must be the same the one passed into dma_alloc_noncontiguous(). vaddr must be the pointer returned by dma_vmap_noncontiguous().}(hj6 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjkhhubjr)}(hint dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma, size_t size, struct sg_table *sgt)h]hint dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma, size_t size, struct sg_table *sgt)}hjD sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhjkhhubh)}(hMap an allocation returned from dma_alloc_noncontiguous() into a user address space. dev and size must be the same as those passed into dma_alloc_noncontiguous(). sgt must be the pointer returned by dma_alloc_noncontiguous().h]hMap an allocation returned from dma_alloc_noncontiguous() into a user address space. dev and size must be the same as those passed into dma_alloc_noncontiguous(). sgt must be the pointer returned by dma_alloc_noncontiguous().}(hjR hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjkhhubjr)}(h!int dma_get_cache_alignment(void)h]h!int dma_get_cache_alignment(void)}hj` sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhjkhhubh)}(hReturns the processor cache alignment. This is the absolute minimum alignment *and* width that you must observe when either mapping memory or doing partial flushes.h](hOReturns the processor cache alignment. This is the absolute minimum alignment }(hjn hhhNhNubj)}(h*and*h]hand}(hjv hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjn ubhQ width that you must observe when either mapping memory or doing partial flushes.}(hjn hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjkhhubj)}(hThis API may return a number *larger* than the actual cache line, but it will guarantee that one or more cache lines fit exactly into the width returned by this call. It will also always be a power of two for easy alignment.h]h)}(hThis API may return a number *larger* than the actual cache line, but it will guarantee that one or more cache lines fit exactly into the width returned by this call. It will also always be a power of two for easy alignment.h](hThis API may return a number }(hj hhhNhNubj)}(h*larger*h]hlarger}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh than the actual cache line, but it will guarantee that one or more cache lines fit exactly into the width returned by this call. It will also always be a power of two for easy alignment.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1j~hjkhhhhhNubeh}(h]$part-ii-non-coherent-dma-allocationsah ]h"]&part ii - non-coherent dma allocationsah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(h+Part III - Debug drivers use of the DMA-APIh]h+Part III - Debug drivers use of the DMA-API}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hXfThe DMA-API as described above has some constraints. DMA addresses must be released with the corresponding function with the same size for example. With the advent of hardware IOMMUs it becomes more and more important that drivers do not violate those constraints. In the worst case such a violation can result in data corruption up to destroyed filesystems.h]hXfThe DMA-API as described above has some constraints. DMA addresses must be released with the corresponding function with the same size for example. With the advent of hardware IOMMUs it becomes more and more important that drivers do not violate those constraints. In the worst case such a violation can result in data corruption up to destroyed filesystems.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXrTo debug drivers and find bugs in the usage of the DMA-API checking code can be compiled into the kernel which will tell the developer about those violations. If your architecture supports it you can select the "Enable debugging of DMA-API usage" option in your kernel configuration. Enabling this option has a performance impact. Do not enable it in production kernels.h]hXvTo debug drivers and find bugs in the usage of the DMA-API checking code can be compiled into the kernel which will tell the developer about those violations. If your architecture supports it you can select the “Enable debugging of DMA-API usage” option in your kernel configuration. Enabling this option has a performance impact. Do not enable it in production kernels.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXIf you boot the resulting kernel will contain code which does some bookkeeping about what DMA memory was allocated for which device. If this code detects an error it prints a warning message with some details into your kernel log. An example warning message may look like this::h]hXIf you boot the resulting kernel will contain code which does some bookkeeping about what DMA memory was allocated for which device. If this code detects an error it prints a warning message with some details into your kernel log. An example warning message may look like this:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubjr)}(hXWARNING: at /data2/repos/linux-2.6-iommu/lib/dma-debug.c:448 check_unmap+0x203/0x490() Hardware name: forcedeth 0000:00:08.0: DMA-API: device driver frees DMA memory with wrong function [device address=0x00000000640444be] [size=66 bytes] [mapped as single] [unmapped as page] Modules linked in: nfsd exportfs bridge stp llc r8169 Pid: 0, comm: swapper Tainted: G W 2.6.28-dmatest-09289-g8bb99c0 #1 Call Trace: [] warn_slowpath+0xf2/0x130 [] _spin_unlock+0x10/0x30 [] usb_hcd_link_urb_to_ep+0x75/0xc0 [] _spin_unlock_irqrestore+0x12/0x40 [] ohci_urb_enqueue+0x19f/0x7c0 [] queue_work+0x56/0x60 [] enqueue_task_fair+0x20/0x50 [] usb_hcd_submit_urb+0x379/0xbc0 [] cpumask_next_and+0x23/0x40 [] find_busiest_group+0x207/0x8a0 [] _spin_lock_irqsave+0x1f/0x50 [] check_unmap+0x203/0x490 [] debug_dma_unmap_page+0x49/0x50 [] nv_tx_done_optimized+0xc6/0x2c0 [] nv_nic_irq_optimized+0x73/0x2b0 [] handle_IRQ_event+0x34/0x70 [] handle_edge_irq+0xc9/0x150 [] do_IRQ+0xcb/0x1c0 [] ret_from_intr+0x0/0xa <4>---[ end trace f6435a98e2a38c0e ]---h]hXWARNING: at /data2/repos/linux-2.6-iommu/lib/dma-debug.c:448 check_unmap+0x203/0x490() Hardware name: forcedeth 0000:00:08.0: DMA-API: device driver frees DMA memory with wrong function [device address=0x00000000640444be] [size=66 bytes] [mapped as single] [unmapped as page] Modules linked in: nfsd exportfs bridge stp llc r8169 Pid: 0, comm: swapper Tainted: G W 2.6.28-dmatest-09289-g8bb99c0 #1 Call Trace: [] warn_slowpath+0xf2/0x130 [] _spin_unlock+0x10/0x30 [] usb_hcd_link_urb_to_ep+0x75/0xc0 [] _spin_unlock_irqrestore+0x12/0x40 [] ohci_urb_enqueue+0x19f/0x7c0 [] queue_work+0x56/0x60 [] enqueue_task_fair+0x20/0x50 [] usb_hcd_submit_urb+0x379/0xbc0 [] cpumask_next_and+0x23/0x40 [] find_busiest_group+0x207/0x8a0 [] _spin_lock_irqsave+0x1f/0x50 [] check_unmap+0x203/0x490 [] debug_dma_unmap_page+0x49/0x50 [] nv_tx_done_optimized+0xc6/0x2c0 [] nv_nic_irq_optimized+0x73/0x2b0 [] handle_IRQ_event+0x34/0x70 [] handle_edge_irq+0xc9/0x150 [] do_IRQ+0xcb/0x1c0 [] ret_from_intr+0x0/0xa <4>---[ end trace f6435a98e2a38c0e ]---}hj sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMhj hhubh)}(h}The driver developer can find the driver and the device including a stacktrace of the DMA-API call which caused this warning.h]h}The driver developer can find the driver and the device including a stacktrace of the DMA-API call which caused this warning.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hX:Per default only the first error will result in a warning message. All other errors will only silently counted. This limitation exist to prevent the code from flooding your kernel log. To support debugging a device driver this can be disabled via debugfs. See the debugfs interface documentation below for details.h]hX:Per default only the first error will result in a warning message. All other errors will only silently counted. This limitation exist to prevent the code from flooding your kernel log. To support debugging a device driver this can be disabled via debugfs. See the debugfs interface documentation below for details.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hThe debugfs directory for the DMA-API debugging code is called dma-api/. In this directory the following files can currently be found:h]hThe debugfs directory for the DMA-API debugging code is called dma-api/. In this directory the following files can currently be found:}(hj% hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubjg)}(hhh]jl)}(hhh](jq)}(hhh]h}(h]h ]h"]h$]h&]colwidthKuh1jphj6 ubjq)}(hhh]h}(h]h ]h"]h$]h&]colwidthK/uh1jphj6 ubj)}(hhh](j)}(hhh](j)}(hhh]h)}(hdma-api/all_errorsh]hdma-api/all_errors}(hjV hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjS ubah}(h]h ]h"]h$]h&]uh1jhjP ubj)}(hhh]h)}(hThis file contains a numeric value. If this value is not equal to zero the debugging code will print a warning for every error it finds into the kernel log. Be careful with this option, as it can easily flood your logs.h]hThis file contains a numeric value. If this value is not equal to zero the debugging code will print a warning for every error it finds into the kernel log. Be careful with this option, as it can easily flood your logs.}(hjm hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjj ubah}(h]h ]h"]h$]h&]uh1jhjP ubeh}(h]h ]h"]h$]h&]uh1jhjM ubj)}(hhh](j)}(hhh]h)}(hdma-api/disabledh]hdma-api/disabled}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hThis read-only file contains the character 'Y' if the debugging code is disabled. This can happen when it runs out of memory or if it was disabled at boot timeh]hThis read-only file contains the character ‘Y’ if the debugging code is disabled. This can happen when it runs out of memory or if it was disabled at boot time}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjM ubj)}(hhh](j)}(hhh]h)}(h dma-api/dumph]h dma-api/dump}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h2This read-only file contains current DMA mappings.h]h2This read-only file contains current DMA mappings.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjM ubj)}(hhh](j)}(hhh]h)}(hdma-api/error_counth]hdma-api/error_count}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hCThis file is read-only and shows the total numbers of errors found.h]hCThis file is read-only and shows the total numbers of errors found.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjM ubj)}(hhh](j)}(hhh]h)}(hdma-api/num_errorsh]hdma-api/num_errors}(hj2 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj/ ubah}(h]h ]h"]h$]h&]uh1jhj, ubj)}(hhh]h)}(hThe number in this file shows how many warnings will be printed to the kernel log before it stops. This number is initialized to one at system boot and be set by writing into this fileh]hThe number in this file shows how many warnings will be printed to the kernel log before it stops. This number is initialized to one at system boot and be set by writing into this file}(hjI hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjF ubah}(h]h ]h"]h$]h&]uh1jhj, ubeh}(h]h ]h"]h$]h&]uh1jhjM ubj)}(hhh](j)}(hhh]h)}(hdma-api/min_free_entriesh]hdma-api/min_free_entries}(hji hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjf ubah}(h]h ]h"]h$]h&]uh1jhjc ubj)}(hhh]h)}(hThis read-only file can be read to get the minimum number of free dma_debug_entries the allocator has ever seen. If this value goes down to zero the code will attempt to increase nr_total_entries to compensate.h]hThis read-only file can be read to get the minimum number of free dma_debug_entries the allocator has ever seen. If this value goes down to zero the code will attempt to increase nr_total_entries to compensate.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj} ubah}(h]h ]h"]h$]h&]uh1jhjc ubeh}(h]h ]h"]h$]h&]uh1jhjM ubj)}(hhh](j)}(hhh]h)}(hdma-api/num_free_entriesh]hdma-api/num_free_entries}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h>The current number of free dma_debug_entries in the allocator.h]h>The current number of free dma_debug_entries in the allocator.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjM ubj)}(hhh](j)}(hhh]h)}(hdma-api/nr_total_entriesh]hdma-api/nr_total_entries}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM"hj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hKThe total number of dma_debug_entries in the allocator, both free and used.h]hKThe total number of dma_debug_entries in the allocator, both free and used.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM"hj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjM ubj)}(hhh](j)}(hhh]h)}(hdma-api/driver_filterh]hdma-api/driver_filter}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM%hj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hYou can write a name of a driver into this file to limit the debug output to requests from that particular driver. Write an empty string to that file to disable the filter and see all errors again.h]hYou can write a name of a driver into this file to limit the debug output to requests from that particular driver. Write an empty string to that file to disable the filter and see all errors again.}(hj% hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM%hj" ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjM ubeh}(h]h ]h"]h$]h&]uh1jhj6 ubeh}(h]h ]h"]h$]h&]colsKuh1jkhj3 ubah}(h]h ]h"]h$]h&]uh1jfhj hhhhhNubh)}(hX,If you have this code compiled into your kernel it will be enabled by default. If you want to boot without the bookkeeping anyway you can provide 'dma_debug=off' as a boot parameter. This will disable DMA-API debugging. Notice that you can not enable it again at runtime. You have to reboot to do so.h]hX0If you have this code compiled into your kernel it will be enabled by default. If you want to boot without the bookkeeping anyway you can provide ‘dma_debug=off’ as a boot parameter. This will disable DMA-API debugging. Notice that you can not enable it again at runtime. You have to reboot to do so.}(hjR hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM,hj hhubh)}(hX.If you want to see debug messages only for a special device driver you can specify the dma_debug_driver= parameter. This will enable the driver filter at boot time. The debug code will only print errors for that driver afterwards. This filter can be disabled or changed later using debugfs.h]hX.If you want to see debug messages only for a special device driver you can specify the dma_debug_driver= parameter. This will enable the driver filter at boot time. The debug code will only print errors for that driver afterwards. This filter can be disabled or changed later using debugfs.}(hj` hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM2hj hhubh)}(hXWhen the code disables itself at runtime this is most likely because it ran out of dma_debug_entries and was unable to allocate more on-demand. 65536 entries are preallocated at boot - if this is too low for you boot with 'dma_debug_entries=' to overwrite the default. Note that the code allocates entries in batches, so the exact number of preallocated entries may be greater than the actual number requested. The code will print to the kernel log each time it has dynamically allocated as many entries as were initially preallocated. This is to indicate that a larger preallocation size may be appropriate, or if it happens continually that a driver may be leaking mappings.h]hXWhen the code disables itself at runtime this is most likely because it ran out of dma_debug_entries and was unable to allocate more on-demand. 65536 entries are preallocated at boot - if this is too low for you boot with ‘dma_debug_entries=’ to overwrite the default. Note that the code allocates entries in batches, so the exact number of preallocated entries may be greater than the actual number requested. The code will print to the kernel log each time it has dynamically allocated as many entries as were initially preallocated. This is to indicate that a larger preallocation size may be appropriate, or if it happens continually that a driver may be leaking mappings.}(hjn hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM7hj hhubjr)}(hFvoid debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr);h]hFvoid debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr);}hj| sbah}(h]h ]h"]h$]h&]jjuh1jqhhhMDhj hhubh)}(hX;dma-debug interface debug_dma_mapping_error() to debug drivers that fail to check DMA mapping errors on addresses returned by dma_map_single() and dma_map_page() interfaces. This interface clears a flag set by debug_dma_map_page() to indicate that dma_mapping_error() has been called by the driver. When driver does unmap, debug_dma_unmap() checks the flag and if this flag is still set, prints warning message that includes call trace that leads up to the unmap. This interface can be called from dma_mapping_error() routines to enable DMA mapping error check debugging.h]hX;dma-debug interface debug_dma_mapping_error() to debug drivers that fail to check DMA mapping errors on addresses returned by dma_map_single() and dma_map_page() interfaces. This interface clears a flag set by debug_dma_map_page() to indicate that dma_mapping_error() has been called by the driver. When driver does unmap, debug_dma_unmap() checks the flag and if this flag is still set, prints warning message that includes call trace that leads up to the unmap. This interface can be called from dma_mapping_error() routines to enable DMA mapping error check debugging.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMGhj hhubeh}(h])part-iii-debug-drivers-use-of-the-dma-apiah ]h"]+part iii - debug drivers use of the dma-apiah$]h&]uh1hhhhhhhhMubeh}(h],dynamic-dma-mapping-using-the-generic-deviceah ]h"],dynamic dma mapping using the generic deviceah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksjfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerj error_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(j j j]jZjjjjj(j%jhjej j j j u nametypes}(j j]jjj(jhj j uh}(j hjZj+jj`jjj%jjej+j jkj j u footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages] transformerN include_log] decorationNhhub.