1/*
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
21#include <linux/types.h>
22#include <linux/vmalloc.h>
23
24#include <asm-generic/dma-coherent.h>
25
26#include <xen/xen.h>
27#include <asm/xen/hypervisor.h>
28
29#define DMA_ERROR_CODE	(~(dma_addr_t)0)
30extern struct dma_map_ops *dma_ops;
31extern struct dma_map_ops coherent_swiotlb_dma_ops;
32extern struct dma_map_ops noncoherent_swiotlb_dma_ops;
33
34static inline struct dma_map_ops *__generic_dma_ops(struct device *dev)
35{
36	if (unlikely(!dev) || !dev->archdata.dma_ops)
37		return dma_ops;
38	else
39		return dev->archdata.dma_ops;
40}
41
42static inline struct dma_map_ops *get_dma_ops(struct device *dev)
43{
44	if (xen_initial_domain())
45		return xen_dma_ops;
46	else
47		return __generic_dma_ops(dev);
48}
49
50static inline void set_dma_ops(struct device *dev, struct dma_map_ops *ops)
51{
52	dev->archdata.dma_ops = ops;
53}
54
55static inline int set_arch_dma_coherent_ops(struct device *dev)
56{
57	set_dma_ops(dev, &coherent_swiotlb_dma_ops);
58	return 0;
59}
60#define set_arch_dma_coherent_ops	set_arch_dma_coherent_ops
61
62#include <asm-generic/dma-mapping-common.h>
63
64static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
65{
66	return (dma_addr_t)paddr;
67}
68
69static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
70{
71	return (phys_addr_t)dev_addr;
72}
73
74static inline int dma_mapping_error(struct device *dev, dma_addr_t dev_addr)
75{
76	struct dma_map_ops *ops = get_dma_ops(dev);
77	debug_dma_mapping_error(dev, dev_addr);
78	return ops->mapping_error(dev, dev_addr);
79}
80
81static inline int dma_supported(struct device *dev, u64 mask)
82{
83	struct dma_map_ops *ops = get_dma_ops(dev);
84	return ops->dma_supported(dev, mask);
85}
86
87static inline int dma_set_mask(struct device *dev, u64 mask)
88{
89	if (!dev->dma_mask || !dma_supported(dev, mask))
90		return -EIO;
91	*dev->dma_mask = mask;
92
93	return 0;
94}
95
96static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
97{
98	if (!dev->dma_mask)
99		return 0;
100
101	return addr + size - 1 <= *dev->dma_mask;
102}
103
104static inline void dma_mark_clean(void *addr, size_t size)
105{
106}
107
108#define dma_alloc_coherent(d, s, h, f)	dma_alloc_attrs(d, s, h, f, NULL)
109#define dma_free_coherent(d, s, h, f)	dma_free_attrs(d, s, h, f, NULL)
110
111static inline void *dma_alloc_attrs(struct device *dev, size_t size,
112				    dma_addr_t *dma_handle, gfp_t flags,
113				    struct dma_attrs *attrs)
114{
115	struct dma_map_ops *ops = get_dma_ops(dev);
116	void *vaddr;
117
118	if (dma_alloc_from_coherent(dev, size, dma_handle, &vaddr))
119		return vaddr;
120
121	vaddr = ops->alloc(dev, size, dma_handle, flags, attrs);
122	debug_dma_alloc_coherent(dev, size, *dma_handle, vaddr);
123	return vaddr;
124}
125
126static inline void dma_free_attrs(struct device *dev, size_t size,
127				  void *vaddr, dma_addr_t dev_addr,
128				  struct dma_attrs *attrs)
129{
130	struct dma_map_ops *ops = get_dma_ops(dev);
131
132	if (dma_release_from_coherent(dev, get_order(size), vaddr))
133		return;
134
135	debug_dma_free_coherent(dev, size, vaddr, dev_addr);
136	ops->free(dev, size, vaddr, dev_addr, attrs);
137}
138
139/*
140 * There is no dma_cache_sync() implementation, so just return NULL here.
141 */
142static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
143					  dma_addr_t *handle, gfp_t flags)
144{
145	return NULL;
146}
147
148static inline void dma_free_noncoherent(struct device *dev, size_t size,
149					void *cpu_addr, dma_addr_t handle)
150{
151}
152
153#endif	/* __KERNEL__ */
154#endif	/* __ASM_DMA_MAPPING_H */
155