2019-06-03 13:44:50 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2012-03-05 19:49:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 ARM Ltd.
|
|
|
|
* Author: Catalin Marinas <catalin.marinas@arm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/gfp.h>
|
2016-08-15 14:45:46 +08:00
|
|
|
#include <linux/cache.h>
|
2020-09-22 21:31:03 +08:00
|
|
|
#include <linux/dma-map-ops.h>
|
2022-08-17 01:28:04 +08:00
|
|
|
#include <linux/iommu.h>
|
2019-07-24 20:07:28 +08:00
|
|
|
#include <xen/xen.h>
|
2012-03-05 19:49:30 +08:00
|
|
|
|
|
|
|
#include <asm/cacheflush.h>
|
2022-06-03 03:23:46 +08:00
|
|
|
#include <asm/xen/xen-ops.h>
|
2012-03-05 19:49:30 +08:00
|
|
|
|
2019-11-08 01:03:11 +08:00
|
|
|
void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
|
2022-06-10 23:12:28 +08:00
|
|
|
enum dma_data_direction dir)
|
2013-05-22 00:35:19 +08:00
|
|
|
{
|
2022-06-10 23:12:28 +08:00
|
|
|
unsigned long start = (unsigned long)phys_to_virt(paddr);
|
|
|
|
|
|
|
|
dcache_clean_poc(start, start + size);
|
2013-05-22 00:35:19 +08:00
|
|
|
}
|
|
|
|
|
2019-11-08 01:03:11 +08:00
|
|
|
void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
|
2022-06-10 23:12:28 +08:00
|
|
|
enum dma_data_direction dir)
|
2013-05-22 00:35:19 +08:00
|
|
|
{
|
2022-06-10 23:12:28 +08:00
|
|
|
unsigned long start = (unsigned long)phys_to_virt(paddr);
|
|
|
|
|
|
|
|
if (dir == DMA_TO_DEVICE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dcache_inval_poc(start, start + size);
|
2013-05-22 00:35:19 +08:00
|
|
|
}
|
|
|
|
|
2018-11-05 03:29:28 +08:00
|
|
|
void arch_dma_prep_coherent(struct page *page, size_t size)
|
|
|
|
{
|
2022-06-10 23:12:28 +08:00
|
|
|
unsigned long start = (unsigned long)page_address(page);
|
|
|
|
|
2022-12-06 18:34:03 +08:00
|
|
|
/*
|
|
|
|
* The architecture only requires a clean to the PoC here in order to
|
|
|
|
* meet the requirements of the DMA API. However, some vendors (i.e.
|
|
|
|
* Qualcomm) abuse the DMA API for transferring buffers from the
|
|
|
|
* non-secure to the secure world, resetting the system if a non-secure
|
|
|
|
* access shows up after the buffer has been transferred:
|
|
|
|
*
|
|
|
|
* https://lore.kernel.org/r/20221114110329.68413-1-manivannan.sadhasivam@linaro.org
|
|
|
|
*
|
|
|
|
* Using clean+invalidate appears to make this issue less likely, but
|
|
|
|
* the drivers themselves still need fixing as the CPU could issue a
|
|
|
|
* speculative read from the buffer via the linear mapping irrespective
|
|
|
|
* of the cache maintenance we use. Once the drivers are fixed, we can
|
|
|
|
* relax this to a clean operation.
|
|
|
|
*/
|
|
|
|
dcache_clean_inval_poc(start, start + size);
|
2018-11-05 03:29:28 +08:00
|
|
|
}
|
|
|
|
|
2015-10-02 03:13:59 +08:00
|
|
|
#ifdef CONFIG_IOMMU_DMA
|
2015-10-02 03:14:00 +08:00
|
|
|
void arch_teardown_dma_ops(struct device *dev)
|
|
|
|
{
|
2017-01-21 05:04:02 +08:00
|
|
|
dev->dma_ops = NULL;
|
2015-10-02 03:14:00 +08:00
|
|
|
}
|
2019-05-20 15:29:29 +08:00
|
|
|
#endif
|
2015-10-02 03:13:59 +08:00
|
|
|
|
2015-10-02 03:14:00 +08:00
|
|
|
void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
|
2016-04-08 01:42:05 +08:00
|
|
|
const struct iommu_ops *iommu, bool coherent)
|
2015-10-02 03:14:00 +08:00
|
|
|
{
|
2019-06-14 21:11:41 +08:00
|
|
|
int cls = cache_line_size_of_cpu();
|
|
|
|
|
|
|
|
WARN_TAINT(!coherent && cls > ARCH_DMA_MINALIGN,
|
|
|
|
TAINT_CPU_OUT_OF_SPEC,
|
|
|
|
"%s %s: ARCH_DMA_MINALIGN smaller than CTR_EL0.CWG (%d < %d)",
|
|
|
|
dev_driver_string(dev), dev_name(dev),
|
|
|
|
ARCH_DMA_MINALIGN, cls);
|
|
|
|
|
2018-10-08 15:12:01 +08:00
|
|
|
dev->dma_coherent = coherent;
|
2019-05-20 15:29:29 +08:00
|
|
|
if (iommu)
|
2021-06-18 23:20:59 +08:00
|
|
|
iommu_setup_dma_ops(dev, dma_base, dma_base + size - 1);
|
2017-04-14 05:04:21 +08:00
|
|
|
|
2022-06-03 03:23:46 +08:00
|
|
|
xen_setup_dma_ops(dev);
|
2015-10-02 03:14:00 +08:00
|
|
|
}
|