blob: 9239416e93d4e9a9787ea9d5801914c075c359a9 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003 * Copyright (C) 2012 ARM Ltd.
4 * Author: Catalin Marinas <catalin.marinas@arm.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
6
7#include <linux/gfp.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008#include <linux/cache.h>
David Brazdil0f672f62019-12-10 10:32:29 +00009#include <linux/dma-noncoherent.h>
10#include <linux/dma-iommu.h>
11#include <xen/xen.h>
12#include <xen/swiotlb-xen.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013
14#include <asm/cacheflush.h>
15
David Brazdil0f672f62019-12-10 10:32:29 +000016void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
17 size_t size, enum dma_data_direction dir)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018{
David Brazdil0f672f62019-12-10 10:32:29 +000019 __dma_map_area(phys_to_virt(paddr), size, dir);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020}
21
David Brazdil0f672f62019-12-10 10:32:29 +000022void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
23 size_t size, enum dma_data_direction dir)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024{
David Brazdil0f672f62019-12-10 10:32:29 +000025 __dma_unmap_area(phys_to_virt(paddr), size, dir);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026}
27
David Brazdil0f672f62019-12-10 10:32:29 +000028void arch_dma_prep_coherent(struct page *page, size_t size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029{
David Brazdil0f672f62019-12-10 10:32:29 +000030 __dma_flush_area(page_address(page), size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031}
32
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033#ifdef CONFIG_IOMMU_DMA
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034void arch_teardown_dma_ops(struct device *dev)
35{
36 dev->dma_ops = NULL;
37}
David Brazdil0f672f62019-12-10 10:32:29 +000038#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039
40void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
41 const struct iommu_ops *iommu, bool coherent)
42{
David Brazdil0f672f62019-12-10 10:32:29 +000043 int cls = cache_line_size_of_cpu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044
David Brazdil0f672f62019-12-10 10:32:29 +000045 WARN_TAINT(!coherent && cls > ARCH_DMA_MINALIGN,
46 TAINT_CPU_OUT_OF_SPEC,
47 "%s %s: ARCH_DMA_MINALIGN smaller than CTR_EL0.CWG (%d < %d)",
48 dev_driver_string(dev), dev_name(dev),
49 ARCH_DMA_MINALIGN, cls);
50
51 dev->dma_coherent = coherent;
52 if (iommu)
53 iommu_setup_dma_ops(dev, dma_base, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054
55#ifdef CONFIG_XEN
David Brazdil0f672f62019-12-10 10:32:29 +000056 if (xen_initial_domain())
57 dev->dma_ops = &xen_swiotlb_dma_ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058#endif
59}