summaryrefslogtreecommitdiff
path: root/arch/ia64/hp/common/hwsw_iommu.c
blob: 22145ded58f61c2fdd0549c90a969a1e9c494e50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (c) 2004 Hewlett-Packard Development Company, L.P.
 *   Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 *
 * This is a pseudo I/O MMU which dispatches to the hardware I/O MMU
 * whenever possible.  We assume that the hardware I/O MMU requires
 * full 32-bit addressability, as is the case, e.g., for HP zx1-based
 * systems (there, the I/O MMU window is mapped at 3-4GB).  If a
 * device doesn't provide full 32-bit addressability, we fall back on
 * the sw I/O TLB.  This is good enough to let us support broken
 * hardware such as soundcards which have a DMA engine that can
 * address only 28 bits.
 */

#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/swiotlb.h>
#include <asm/machvec.h>

/* swiotlb declarations & definitions: */
extern int swiotlb_late_init_with_default_size (size_t size);

/* hwiommu declarations & definitions: */

extern ia64_mv_dma_alloc_coherent	sba_alloc_coherent;
extern ia64_mv_dma_free_coherent	sba_free_coherent;
extern ia64_mv_dma_map_single_attrs	sba_map_single_attrs;
extern ia64_mv_dma_unmap_single_attrs	sba_unmap_single_attrs;
extern ia64_mv_dma_map_sg_attrs		sba_map_sg_attrs;
extern ia64_mv_dma_unmap_sg_attrs	sba_unmap_sg_attrs;
extern ia64_mv_dma_supported		sba_dma_supported;
extern ia64_mv_dma_mapping_error	sba_dma_mapping_error;

#define hwiommu_alloc_coherent		sba_alloc_coherent
#define hwiommu_free_coherent		sba_free_coherent
#define hwiommu_map_single_attrs	sba_map_single_attrs
#define hwiommu_unmap_single_attrs	sba_unmap_single_attrs
#define hwiommu_map_sg_attrs		sba_map_sg_attrs
#define hwiommu_unmap_sg_attrs		sba_unmap_sg_attrs
#define hwiommu_dma_supported		sba_dma_supported
#define hwiommu_dma_mapping_error	sba_dma_mapping_error
#define hwiommu_sync_single_for_cpu	machvec_dma_sync_single
#define hwiommu_sync_sg_for_cpu		machvec_dma_sync_sg
#define hwiommu_sync_single_for_device	machvec_dma_sync_single
#define hwiommu_sync_sg_for_device	machvec_dma_sync_sg


/*
 * Note: we need to make the determination of whether or not to use
 * the sw I/O TLB based purely on the device structure.  Anything else
 * would be unreliable or would be too intrusive.
 */
static inline int
use_swiotlb (struct device *dev)
{
	return dev && dev->dma_mask && !hwiommu_dma_supported(dev, *dev->dma_mask);
}

struct dma_mapping_ops hwsw_dma_ops;

void __init
hwsw_init (void)
{
	dma_ops = &hwsw_dma_ops;
	/* default to a smallish 2MB sw I/O TLB */
	if (swiotlb_late_init_with_default_size (2 * (1<<20)) != 0) {
#ifdef CONFIG_IA64_GENERIC
		/* Better to have normal DMA than panic */
		printk(KERN_WARNING "%s: Failed to initialize software I/O TLB,"
		       " reverting to hpzx1 platform vector\n", __func__);
		machvec_init("hpzx1");
#else
		panic("Unable to initialize software I/O TLB services");
#endif
	}
}

void *
hwsw_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags)
{
	if (use_swiotlb(dev))
		return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
	else
		return hwiommu_alloc_coherent(dev, size, dma_handle, flags);
}

void
hwsw_free_coherent (struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle)
{
	if (use_swiotlb(dev))
		swiotlb_free_coherent(dev, size, vaddr, dma_handle);
	else
		hwiommu_free_coherent(dev, size, vaddr, dma_handle);
}

dma_addr_t
hwsw_map_single_attrs(struct device *dev, void *addr, size_t size, int dir,
		       struct dma_attrs *attrs)
{
	if (use_swiotlb(dev))
		return swiotlb_map_single_attrs(dev, addr, size, dir, attrs);
	else
		return hwiommu_map_single_attrs(dev, addr, size, dir, attrs);
}
EXPORT_SYMBOL(hwsw_map_single_attrs);

void
hwsw_unmap_single_attrs(struct device *dev, dma_addr_t iova, size_t size,
			 int dir, struct dma_attrs *attrs)
{
	if (use_swiotlb(dev))
		return swiotlb_unmap_single_attrs(dev, iova, size, dir, attrs);
	else
		return hwiommu_unmap_single_attrs(dev, iova, size, dir, attrs);
}
EXPORT_SYMBOL(hwsw_unmap_single_attrs);

int
hwsw_map_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
		   int dir, struct dma_attrs *attrs)
{
	if (use_swiotlb(dev))
		return swiotlb_map_sg_attrs(dev, sglist, nents, dir, attrs);
	else
		return hwiommu_map_sg_attrs(dev, sglist, nents, dir, attrs);
}
EXPORT_SYMBOL(hwsw_map_sg_attrs);

void
hwsw_unmap_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
		     int dir, struct dma_attrs *attrs)
{
	if (use_swiotlb(dev))
		return swiotlb_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
	else
		return hwiommu_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
}
EXPORT_SYMBOL(hwsw_unmap_sg_attrs);

void
hwsw_sync_single_for_cpu (struct device *dev, dma_addr_t addr, size_t size, int dir)
{
	if (use_swiotlb(dev))
		swiotlb_sync_single_for_cpu(dev, addr, size, dir);
	else
		hwiommu_sync_single_for_cpu(dev, addr, size, dir);
}

void
hwsw_sync_sg_for_cpu (struct device *dev, struct scatterlist *sg, int nelems, int dir)
{
	if (use_swiotlb(dev))
		swiotlb_sync_sg_for_cpu(dev, sg, nelems, dir);
	else
		hwiommu_sync_sg_for_cpu(dev, sg, nelems, dir);
}

void
hwsw_sync_single_for_device (struct device *dev, dma_addr_t addr, size_t size, int dir)
{
	if (use_swiotlb(dev))
		swiotlb_sync_single_for_device(dev, addr, size, dir);
	else
		hwiommu_sync_single_for_device(dev, addr, size, dir);
}

void
hwsw_sync_sg_for_device (struct device *dev, struct scatterlist *sg, int nelems, int dir)
{
	if (use_swiotlb(dev))
		swiotlb_sync_sg_for_device(dev, sg, nelems, dir);
	else
		hwiommu_sync_sg_for_device(dev, sg, nelems, dir);
}

int
hwsw_dma_supported (struct device *dev, u64 mask)
{
	if (hwiommu_dma_supported(dev, mask))
		return 1;
	return swiotlb_dma_supported(dev, mask);
}

int
hwsw_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
{
	return hwiommu_dma_mapping_error(dev, dma_addr) ||
		swiotlb_dma_mapping_error(dev, dma_addr);
}

EXPORT_SYMBOL(hwsw_dma_mapping_error);
EXPORT_SYMBOL(hwsw_dma_supported);
EXPORT_SYMBOL(hwsw_alloc_coherent);
EXPORT_SYMBOL(hwsw_free_coherent);
EXPORT_SYMBOL(hwsw_sync_single_for_cpu);
EXPORT_SYMBOL(hwsw_sync_single_for_device);
EXPORT_SYMBOL(hwsw_sync_sg_for_cpu);
EXPORT_SYMBOL(hwsw_sync_sg_for_device);

struct dma_mapping_ops hwsw_dma_ops = {
	.alloc_coherent		= hwsw_alloc_coherent,
	.free_coherent		= hwsw_free_coherent,
	.map_single_attrs	= hwsw_map_single_attrs,
	.unmap_single_attrs	= hwsw_unmap_single_attrs,
	.map_sg_attrs		= hwsw_map_sg_attrs,
	.unmap_sg_attrs		= hwsw_unmap_sg_attrs,
	.sync_single_for_cpu	= hwsw_sync_single_for_cpu,
	.sync_sg_for_cpu	= hwsw_sync_sg_for_cpu,
	.sync_single_for_device	= hwsw_sync_single_for_device,
	.sync_sg_for_device	= hwsw_sync_sg_for_device,
	.dma_supported_op	= hwsw_dma_supported,
	.mapping_error		= hwsw_dma_mapping_error,
};