blob: f0d6d0bfe55ceceba3339bc9044bed31a159a9cf [file] [log] [blame]
Catalin Marinas09b55412012-03-05 11:49:30 +00001/*
2 * Copyright (C) 2012 ARM Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#ifndef __ASM_DMA_MAPPING_H
17#define __ASM_DMA_MAPPING_H
18
19#ifdef __KERNEL__
20
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -050021#include <linux/acpi.h>
Catalin Marinas09b55412012-03-05 11:49:30 +000022#include <linux/types.h>
23#include <linux/vmalloc.h>
24
25#include <asm-generic/dma-coherent.h>
26
Stefano Stabellini58c8b262013-10-18 16:01:32 +000027#include <xen/xen.h>
28#include <asm/xen/hypervisor.h>
29
Stefano Stabellini25b719d2013-10-15 15:49:03 +000030#define DMA_ERROR_CODE (~(dma_addr_t)0)
Catalin Marinas09b55412012-03-05 11:49:30 +000031extern struct dma_map_ops *dma_ops;
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -050032extern struct dma_map_ops dummy_dma_ops;
Catalin Marinas09b55412012-03-05 11:49:30 +000033
Stefano Stabellini58c8b262013-10-18 16:01:32 +000034static inline struct dma_map_ops *__generic_dma_ops(struct device *dev)
Catalin Marinas09b55412012-03-05 11:49:30 +000035{
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -050036 if (unlikely(!dev))
Catalin Marinas09b55412012-03-05 11:49:30 +000037 return dma_ops;
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -050038 else if (dev->archdata.dma_ops)
Catalin Marinas09b55412012-03-05 11:49:30 +000039 return dev->archdata.dma_ops;
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -050040 else if (acpi_disabled)
41 return dma_ops;
42
43 /*
44 * When ACPI is enabled, if arch_set_dma_ops is not called,
45 * we will disable device DMA capability by setting it
46 * to dummy_dma_ops.
47 */
48 return &dummy_dma_ops;
Catalin Marinas09b55412012-03-05 11:49:30 +000049}
50
Stefano Stabellini58c8b262013-10-18 16:01:32 +000051static inline struct dma_map_ops *get_dma_ops(struct device *dev)
52{
53 if (xen_initial_domain())
54 return xen_dma_ops;
55 else
56 return __generic_dma_ops(dev);
57}
58
Catalin Marinas31dde112014-12-18 17:13:49 +000059static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
60 struct iommu_ops *iommu, bool coherent)
Catalin Marinas21890642014-09-22 11:48:31 +010061{
Suthikulpanit, Suraveeb6197b92015-06-10 11:08:53 -050062 if (!acpi_disabled && !dev->archdata.dma_ops)
63 dev->archdata.dma_ops = dma_ops;
64
Catalin Marinas31dde112014-12-18 17:13:49 +000065 dev->archdata.dma_coherent = coherent;
Catalin Marinas21890642014-09-22 11:48:31 +010066}
Catalin Marinas31dde112014-12-18 17:13:49 +000067#define arch_setup_dma_ops arch_setup_dma_ops
Catalin Marinas21890642014-09-22 11:48:31 +010068
Stefano Stabellinide7ee502014-11-20 10:41:35 +000069/* do not use this function in a driver */
70static inline bool is_device_dma_coherent(struct device *dev)
71{
Catalin Marinas9d3bfbb2015-01-12 20:48:53 +000072 if (!dev)
73 return false;
Stefano Stabellinide7ee502014-11-20 10:41:35 +000074 return dev->archdata.dma_coherent;
75}
76
Catalin Marinas09b55412012-03-05 11:49:30 +000077#include <asm-generic/dma-mapping-common.h>
78
79static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
80{
81 return (dma_addr_t)paddr;
82}
83
84static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
85{
86 return (phys_addr_t)dev_addr;
87}
88
89static inline int dma_mapping_error(struct device *dev, dma_addr_t dev_addr)
90{
91 struct dma_map_ops *ops = get_dma_ops(dev);
Shuah Khanbb7c4de2012-11-23 14:31:09 -070092 debug_dma_mapping_error(dev, dev_addr);
Catalin Marinas09b55412012-03-05 11:49:30 +000093 return ops->mapping_error(dev, dev_addr);
94}
95
96static inline int dma_supported(struct device *dev, u64 mask)
97{
98 struct dma_map_ops *ops = get_dma_ops(dev);
99 return ops->dma_supported(dev, mask);
100}
101
102static inline int dma_set_mask(struct device *dev, u64 mask)
103{
104 if (!dev->dma_mask || !dma_supported(dev, mask))
105 return -EIO;
106 *dev->dma_mask = mask;
107
108 return 0;
109}
110
111static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
112{
113 if (!dev->dma_mask)
Joe Perchescc3979b2015-03-31 00:46:00 +0100114 return false;
Catalin Marinas09b55412012-03-05 11:49:30 +0000115
116 return addr + size - 1 <= *dev->dma_mask;
117}
118
119static inline void dma_mark_clean(void *addr, size_t size)
120{
121}
122
Damian Hobson-Garciad25749a2013-04-30 04:02:13 +0100123#define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
124#define dma_free_coherent(d, s, h, f) dma_free_attrs(d, s, h, f, NULL)
125
126static inline void *dma_alloc_attrs(struct device *dev, size_t size,
127 dma_addr_t *dma_handle, gfp_t flags,
128 struct dma_attrs *attrs)
Catalin Marinas09b55412012-03-05 11:49:30 +0000129{
130 struct dma_map_ops *ops = get_dma_ops(dev);
131 void *vaddr;
132
133 if (dma_alloc_from_coherent(dev, size, dma_handle, &vaddr))
134 return vaddr;
135
Damian Hobson-Garciad25749a2013-04-30 04:02:13 +0100136 vaddr = ops->alloc(dev, size, dma_handle, flags, attrs);
Catalin Marinas09b55412012-03-05 11:49:30 +0000137 debug_dma_alloc_coherent(dev, size, *dma_handle, vaddr);
138 return vaddr;
139}
140
Damian Hobson-Garciad25749a2013-04-30 04:02:13 +0100141static inline void dma_free_attrs(struct device *dev, size_t size,
142 void *vaddr, dma_addr_t dev_addr,
143 struct dma_attrs *attrs)
Catalin Marinas09b55412012-03-05 11:49:30 +0000144{
145 struct dma_map_ops *ops = get_dma_ops(dev);
146
147 if (dma_release_from_coherent(dev, get_order(size), vaddr))
148 return;
149
150 debug_dma_free_coherent(dev, size, vaddr, dev_addr);
Damian Hobson-Garciad25749a2013-04-30 04:02:13 +0100151 ops->free(dev, size, vaddr, dev_addr, attrs);
Catalin Marinas09b55412012-03-05 11:49:30 +0000152}
153
154/*
155 * There is no dma_cache_sync() implementation, so just return NULL here.
156 */
157static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
158 dma_addr_t *handle, gfp_t flags)
159{
160 return NULL;
161}
162
163static inline void dma_free_noncoherent(struct device *dev, size_t size,
164 void *cpu_addr, dma_addr_t handle)
165{
166}
167
168#endif /* __KERNEL__ */
169#endif /* __ASM_DMA_MAPPING_H */