9e3a25dc99
- move the USB special case that bounced DMA through a device bar into the USB code instead of handling it in the common DMA code (Laurentiu Tudor and Fredrik Noring) - don't dip into the global CMA pool for single page allocations (Nicolin Chen) - fix a crash when allocating memory for the atomic pool failed during boot (Florian Fainelli) - move support for MIPS-style uncached segments to the common code and use that for MIPS and nios2 (me) - make support for DMA_ATTR_NON_CONSISTENT and DMA_ATTR_NO_KERNEL_MAPPING generic (me) - convert nds32 to the generic remapping allocator (me) -----BEGIN PGP SIGNATURE----- iQI/BAABCgApFiEEgdbnc3r/njty3Iq9D55TZVIEUYMFAl0nPqgLHGhjaEBsc3Qu ZGUACgkQD55TZVIEUYNj2hAAxIv2O3wv6V5xhzWwOVo8e/xW1ZLlGAF0/z92u0do 32Tm8jkdAGjZDnyxam7qisMSIjCNykpauQzVVxyUNBRSsn1V5t7KSaH3/OXCOVcr x2VWBirxGO2BbRseaCBjIcA/2qna+VIDGFcNXCtf6rM00YUK6qaJzkMwBKQAeYcM uJMJkaf8qaW4hygLJP8axXiGFdIJyFNLAlJ+ok6kYsJHHJNceOp0bo3CDa2mJBK9 IhraK2zVkyE5EQkQM5cE/Kw1ppPelUKUkHwjgM4wpz2b18WbLu11nKP0hmUcvKRQ heY8xWiKxN0QTgS03ou7EVylyrSAE4dIKgzuA4VO32QCGsWypcAg4iU6s5TX6p9g tZEW2ckE6wbmRdQPyKoDpZg299/eQjRHc4MAA1yinT8tFMokw2tk8Fq1FWyltwL1 8EiP5oNs2qUNvNgqUresl6/f6YOacFi1Q6IhgBVj6d6lyhMhlsHfW4w1XA1siv/I 6l4qJbLohYab6hY7i+mBOd8iG/KrAlr4P6admnv2jDchswbb5t2j+ABE9xv++PFi u1HFqMlxqdWQaXGca2UeCUxUjkwO9N+kHpP+VRz+6D2b64dtCWSu8CN23sYXm2tO ubWIlrQQZPhhMkoFg7XqKSTacd+ut+SXN9Nxsyv548ETV0l1xbiLRHIbhyoIESD5 RAI= =01Fr -----END PGP SIGNATURE----- Merge tag 'dma-mapping-5.3' of git://git.infradead.org/users/hch/dma-mapping Pull dma-mapping updates from Christoph Hellwig: - move the USB special case that bounced DMA through a device bar into the USB code instead of handling it in the common DMA code (Laurentiu Tudor and Fredrik Noring) - don't dip into the global CMA pool for single page allocations (Nicolin Chen) - fix a crash when allocating memory for the atomic pool failed during boot (Florian Fainelli) - move support for MIPS-style uncached segments to the common code and use that for MIPS and nios2 (me) - make support for DMA_ATTR_NON_CONSISTENT and DMA_ATTR_NO_KERNEL_MAPPING generic (me) - convert nds32 to the generic remapping allocator (me) * tag 'dma-mapping-5.3' of git://git.infradead.org/users/hch/dma-mapping: (29 commits) dma-mapping: mark dma_alloc_need_uncached as __always_inline MIPS: only select ARCH_HAS_UNCACHED_SEGMENT for non-coherent platforms usb: host: Fix excessive alignment restriction for local memory allocations lib/genalloc.c: Add algorithm, align and zeroed family of DMA allocators nios2: use the generic uncached segment support in dma-direct nds32: use the generic remapping allocator for coherent DMA allocations arc: use the generic remapping allocator for coherent DMA allocations dma-direct: handle DMA_ATTR_NO_KERNEL_MAPPING in common code dma-direct: handle DMA_ATTR_NON_CONSISTENT in common code dma-mapping: add a dma_alloc_need_uncached helper openrisc: remove the partial DMA_ATTR_NON_CONSISTENT support arc: remove the partial DMA_ATTR_NON_CONSISTENT support arm-nommu: remove the partial DMA_ATTR_NON_CONSISTENT support ARM: dma-mapping: allow larger DMA mask than supported dma-mapping: truncate dma masks to what dma_addr_t can hold iommu/dma: Apply dma_{alloc,free}_contiguous functions dma-remap: Avoid de-referencing NULL atomic_pool MIPS: use the generic uncached segment support in dma-direct dma-direct: provide generic support for uncached kernel segments au1100fb: fix DMA API abuse ...
211 lines
5.5 KiB
C
211 lines
5.5 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* Based on linux/arch/arm/mm/dma-mapping.c
|
|
*
|
|
* Copyright (C) 2000-2004 Russell King
|
|
*/
|
|
|
|
#include <linux/export.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/dma-direct.h>
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <asm/cachetype.h>
|
|
#include <asm/cacheflush.h>
|
|
#include <asm/outercache.h>
|
|
#include <asm/cp15.h>
|
|
|
|
#include "dma.h"
|
|
|
|
/*
|
|
* The generic direct mapping code is used if
|
|
* - MMU/MPU is off
|
|
* - cpu is v7m w/o cache support
|
|
* - device is coherent
|
|
* otherwise arm_nommu_dma_ops is used.
|
|
*
|
|
* arm_nommu_dma_ops rely on consistent DMA memory (please, refer to
|
|
* [1] on how to declare such memory).
|
|
*
|
|
* [1] Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
|
|
*/
|
|
|
|
static void *arm_nommu_dma_alloc(struct device *dev, size_t size,
|
|
dma_addr_t *dma_handle, gfp_t gfp,
|
|
unsigned long attrs)
|
|
|
|
{
|
|
void *ret = dma_alloc_from_global_coherent(size, dma_handle);
|
|
|
|
/*
|
|
* dma_alloc_from_global_coherent() may fail because:
|
|
*
|
|
* - no consistent DMA region has been defined, so we can't
|
|
* continue.
|
|
* - there is no space left in consistent DMA region, so we
|
|
* only can fallback to generic allocator if we are
|
|
* advertised that consistency is not required.
|
|
*/
|
|
|
|
WARN_ON_ONCE(ret == NULL);
|
|
return ret;
|
|
}
|
|
|
|
static void arm_nommu_dma_free(struct device *dev, size_t size,
|
|
void *cpu_addr, dma_addr_t dma_addr,
|
|
unsigned long attrs)
|
|
{
|
|
int ret = dma_release_from_global_coherent(get_order(size), cpu_addr);
|
|
|
|
WARN_ON_ONCE(ret == 0);
|
|
}
|
|
|
|
static int arm_nommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
|
|
void *cpu_addr, dma_addr_t dma_addr, size_t size,
|
|
unsigned long attrs)
|
|
{
|
|
int ret;
|
|
|
|
if (dma_mmap_from_global_coherent(vma, cpu_addr, size, &ret))
|
|
return ret;
|
|
|
|
return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
|
|
}
|
|
|
|
|
|
static void __dma_page_cpu_to_dev(phys_addr_t paddr, size_t size,
|
|
enum dma_data_direction dir)
|
|
{
|
|
dmac_map_area(__va(paddr), size, dir);
|
|
|
|
if (dir == DMA_FROM_DEVICE)
|
|
outer_inv_range(paddr, paddr + size);
|
|
else
|
|
outer_clean_range(paddr, paddr + size);
|
|
}
|
|
|
|
static void __dma_page_dev_to_cpu(phys_addr_t paddr, size_t size,
|
|
enum dma_data_direction dir)
|
|
{
|
|
if (dir != DMA_TO_DEVICE) {
|
|
outer_inv_range(paddr, paddr + size);
|
|
dmac_unmap_area(__va(paddr), size, dir);
|
|
}
|
|
}
|
|
|
|
static dma_addr_t arm_nommu_dma_map_page(struct device *dev, struct page *page,
|
|
unsigned long offset, size_t size,
|
|
enum dma_data_direction dir,
|
|
unsigned long attrs)
|
|
{
|
|
dma_addr_t handle = page_to_phys(page) + offset;
|
|
|
|
__dma_page_cpu_to_dev(handle, size, dir);
|
|
|
|
return handle;
|
|
}
|
|
|
|
static void arm_nommu_dma_unmap_page(struct device *dev, dma_addr_t handle,
|
|
size_t size, enum dma_data_direction dir,
|
|
unsigned long attrs)
|
|
{
|
|
__dma_page_dev_to_cpu(handle, size, dir);
|
|
}
|
|
|
|
|
|
static int arm_nommu_dma_map_sg(struct device *dev, struct scatterlist *sgl,
|
|
int nents, enum dma_data_direction dir,
|
|
unsigned long attrs)
|
|
{
|
|
int i;
|
|
struct scatterlist *sg;
|
|
|
|
for_each_sg(sgl, sg, nents, i) {
|
|
sg_dma_address(sg) = sg_phys(sg);
|
|
sg_dma_len(sg) = sg->length;
|
|
__dma_page_cpu_to_dev(sg_dma_address(sg), sg_dma_len(sg), dir);
|
|
}
|
|
|
|
return nents;
|
|
}
|
|
|
|
static void arm_nommu_dma_unmap_sg(struct device *dev, struct scatterlist *sgl,
|
|
int nents, enum dma_data_direction dir,
|
|
unsigned long attrs)
|
|
{
|
|
struct scatterlist *sg;
|
|
int i;
|
|
|
|
for_each_sg(sgl, sg, nents, i)
|
|
__dma_page_dev_to_cpu(sg_dma_address(sg), sg_dma_len(sg), dir);
|
|
}
|
|
|
|
static void arm_nommu_dma_sync_single_for_device(struct device *dev,
|
|
dma_addr_t handle, size_t size, enum dma_data_direction dir)
|
|
{
|
|
__dma_page_cpu_to_dev(handle, size, dir);
|
|
}
|
|
|
|
static void arm_nommu_dma_sync_single_for_cpu(struct device *dev,
|
|
dma_addr_t handle, size_t size, enum dma_data_direction dir)
|
|
{
|
|
__dma_page_cpu_to_dev(handle, size, dir);
|
|
}
|
|
|
|
static void arm_nommu_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
|
|
int nents, enum dma_data_direction dir)
|
|
{
|
|
struct scatterlist *sg;
|
|
int i;
|
|
|
|
for_each_sg(sgl, sg, nents, i)
|
|
__dma_page_cpu_to_dev(sg_dma_address(sg), sg_dma_len(sg), dir);
|
|
}
|
|
|
|
static void arm_nommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
|
|
int nents, enum dma_data_direction dir)
|
|
{
|
|
struct scatterlist *sg;
|
|
int i;
|
|
|
|
for_each_sg(sgl, sg, nents, i)
|
|
__dma_page_dev_to_cpu(sg_dma_address(sg), sg_dma_len(sg), dir);
|
|
}
|
|
|
|
const struct dma_map_ops arm_nommu_dma_ops = {
|
|
.alloc = arm_nommu_dma_alloc,
|
|
.free = arm_nommu_dma_free,
|
|
.mmap = arm_nommu_dma_mmap,
|
|
.map_page = arm_nommu_dma_map_page,
|
|
.unmap_page = arm_nommu_dma_unmap_page,
|
|
.map_sg = arm_nommu_dma_map_sg,
|
|
.unmap_sg = arm_nommu_dma_unmap_sg,
|
|
.sync_single_for_device = arm_nommu_dma_sync_single_for_device,
|
|
.sync_single_for_cpu = arm_nommu_dma_sync_single_for_cpu,
|
|
.sync_sg_for_device = arm_nommu_dma_sync_sg_for_device,
|
|
.sync_sg_for_cpu = arm_nommu_dma_sync_sg_for_cpu,
|
|
};
|
|
EXPORT_SYMBOL(arm_nommu_dma_ops);
|
|
|
|
void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
|
|
const struct iommu_ops *iommu, bool coherent)
|
|
{
|
|
if (IS_ENABLED(CONFIG_CPU_V7M)) {
|
|
/*
|
|
* Cache support for v7m is optional, so can be treated as
|
|
* coherent if no cache has been detected. Note that it is not
|
|
* enough to check if MPU is in use or not since in absense of
|
|
* MPU system memory map is used.
|
|
*/
|
|
dev->archdata.dma_coherent = (cacheid) ? coherent : true;
|
|
} else {
|
|
/*
|
|
* Assume coherent DMA in case MMU/MPU has not been set up.
|
|
*/
|
|
dev->archdata.dma_coherent = (get_cr() & CR_M) ? coherent : true;
|
|
}
|
|
|
|
if (!dev->archdata.dma_coherent)
|
|
set_dma_ops(dev, &arm_nommu_dma_ops);
|
|
}
|