mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-09-18 22:14:16 +00:00
dma-mapping: add a new dma_alloc_noncoherent API
Add a new API to allocate and free memory that is guaranteed to be addressable by a device, but which potentially is not cache coherent for DMA. To transfer ownership to and from the device, the existing streaming DMA API calls dma_sync_single_for_device and dma_sync_single_for_cpu must be used. For now the new calls are implemented on top of dma_alloc_attrs just like the old-noncoherent API, but once all drivers are switched to the new API it will be replaced with a better working implementation that is available on all architectures. Signed-off-by: Christoph Hellwig <hch@lst.de>
This commit is contained in:
parent
91af2dd255
commit
0d71675f87
2 changed files with 48 additions and 39 deletions
|
@ -516,48 +516,56 @@ routines, e.g.:::
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Part II - Advanced dma usage
|
Part II - Non-coherent DMA allocations
|
||||||
----------------------------
|
--------------------------------------
|
||||||
|
|
||||||
Warning: These pieces of the DMA API should not be used in the
|
These APIs allow to allocate pages in the kernel direct mapping that are
|
||||||
majority of cases, since they cater for unlikely corner cases that
|
guaranteed to be DMA addressable. This means that unlike dma_alloc_coherent,
|
||||||
don't belong in usual drivers.
|
virt_to_page can be called on the resulting address, and the resulting
|
||||||
|
struct page can be used for everything a struct page is suitable for.
|
||||||
|
|
||||||
If you don't understand how cache line coherency works between a
|
If you don't understand how cache line coherency works between a processor and
|
||||||
processor and an I/O device, you should not be using this part of the
|
an I/O device, you should not be using this part of the API.
|
||||||
API at all.
|
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
void *
|
void *
|
||||||
dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
|
dma_alloc_noncoherent(struct device *dev, size_t size,
|
||||||
gfp_t flag, unsigned long attrs)
|
dma_addr_t *dma_handle, enum dma_data_direction dir,
|
||||||
|
gfp_t gfp)
|
||||||
|
|
||||||
Identical to dma_alloc_coherent() except that when the
|
This routine allocates a region of <size> bytes of consistent memory. It
|
||||||
DMA_ATTR_NON_CONSISTENT flags is passed in the attrs argument, the
|
returns a pointer to the allocated region (in the processor's virtual address
|
||||||
platform will choose to return either consistent or non-consistent memory
|
space) or NULL if the allocation failed. The returned memory may or may not
|
||||||
as it sees fit. By using this API, you are guaranteeing to the platform
|
be in the kernels direct mapping. Drivers must not call virt_to_page on
|
||||||
that you have all the correct and necessary sync points for this memory
|
the returned memory region.
|
||||||
in the driver should it choose to return non-consistent memory.
|
|
||||||
|
|
||||||
Note: where the platform can return consistent memory, it will
|
It also returns a <dma_handle> which may be cast to an unsigned integer the
|
||||||
guarantee that the sync points become nops.
|
same width as the bus and given to the device as the DMA address base of
|
||||||
|
the region.
|
||||||
|
|
||||||
Warning: Handling non-consistent memory is a real pain. You should
|
The dir parameter specified if data is read and/or written by the device,
|
||||||
only use this API if you positively know your driver will be
|
see dma_map_single() for details.
|
||||||
required to work on one of the rare (usually non-PCI) architectures
|
|
||||||
that simply cannot make consistent memory.
|
The 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.
|
||||||
|
|
||||||
|
Before 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.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
void
|
void
|
||||||
dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
|
dma_free_noncoherent(struct device *dev, size_t size, void *cpu_addr,
|
||||||
dma_addr_t dma_handle, unsigned long attrs)
|
dma_addr_t dma_handle, enum dma_data_direction dir)
|
||||||
|
|
||||||
Free memory allocated by the dma_alloc_attrs(). All common
|
Free a region of memory previously allocated using dma_alloc_noncoherent().
|
||||||
parameters must be identical to those otherwise passed to dma_free_coherent,
|
dev, size and dma_handle and dir must all be the same as those passed into
|
||||||
and the attrs argument must be identical to the attrs passed to
|
dma_alloc_noncoherent(). cpu_addr must be the virtual address returned by
|
||||||
dma_alloc_attrs().
|
the dma_alloc_noncoherent().
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
@ -575,17 +583,6 @@ memory or doing partial flushes.
|
||||||
into the width returned by this call. It will also always be a power
|
into the width returned by this call. It will also always be a power
|
||||||
of two for easy alignment.
|
of two for easy alignment.
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
void
|
|
||||||
dma_cache_sync(struct device *dev, void *vaddr, size_t size,
|
|
||||||
enum dma_data_direction direction)
|
|
||||||
|
|
||||||
Do a partial sync of memory that was allocated by dma_alloc_attrs() with
|
|
||||||
the DMA_ATTR_NON_CONSISTENT flag starting at virtual address vaddr and
|
|
||||||
continuing on for size. Again, you *must* observe the cache line
|
|
||||||
boundaries when doing this.
|
|
||||||
|
|
||||||
|
|
||||||
Part III - Debug drivers use of the DMA-API
|
Part III - Debug drivers use of the DMA-API
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
|
|
|
@ -384,6 +384,18 @@ static inline unsigned long dma_get_merge_boundary(struct device *dev)
|
||||||
}
|
}
|
||||||
#endif /* CONFIG_HAS_DMA */
|
#endif /* CONFIG_HAS_DMA */
|
||||||
|
|
||||||
|
static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
|
||||||
|
dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
|
||||||
|
{
|
||||||
|
return dma_alloc_attrs(dev, size, dma_handle, gfp,
|
||||||
|
DMA_ATTR_NON_CONSISTENT);
|
||||||
|
}
|
||||||
|
static inline void dma_free_noncoherent(struct device *dev, size_t size,
|
||||||
|
void *vaddr, dma_addr_t dma_handle, enum dma_data_direction dir)
|
||||||
|
{
|
||||||
|
dma_free_attrs(dev, size, vaddr, dma_handle, DMA_ATTR_NON_CONSISTENT);
|
||||||
|
}
|
||||||
|
|
||||||
static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
|
static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
|
||||||
size_t size, enum dma_data_direction dir, unsigned long attrs)
|
size_t size, enum dma_data_direction dir, unsigned long attrs)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue