blob: 1c44ec2a1d58e2495cb21f41759dcee218471ffc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Hewlett-Packard Development Company, L.P.
3 * Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 *
5 * This is a pseudo I/O MMU which dispatches to the hardware I/O MMU
6 * whenever possible. We assume that the hardware I/O MMU requires
7 * full 32-bit addressability, as is the case, e.g., for HP zx1-based
8 * systems (there, the I/O MMU window is mapped at 3-4GB). If a
9 * device doesn't provide full 32-bit addressability, we fall back on
10 * the sw I/O TLB. This is good enough to let us support broken
11 * hardware such as soundcards which have a DMA engine that can
12 * address only 28 bits.
13 */
14
15#include <linux/device.h>
16
17#include <asm/machvec.h>
18
19/* swiotlb declarations & definitions: */
Alex Williamson0b9afed2005-09-06 11:20:49 -060020extern int swiotlb_late_init_with_default_size (size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021extern ia64_mv_dma_alloc_coherent swiotlb_alloc_coherent;
22extern ia64_mv_dma_free_coherent swiotlb_free_coherent;
Arthur Kepner309df0c2008-04-29 01:00:32 -070023extern ia64_mv_dma_map_single_attrs swiotlb_map_single_attrs;
24extern ia64_mv_dma_unmap_single_attrs swiotlb_unmap_single_attrs;
25extern ia64_mv_dma_map_sg_attrs swiotlb_map_sg_attrs;
26extern ia64_mv_dma_unmap_sg_attrs swiotlb_unmap_sg_attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027extern ia64_mv_dma_supported swiotlb_dma_supported;
28extern ia64_mv_dma_mapping_error swiotlb_dma_mapping_error;
29
30/* hwiommu declarations & definitions: */
31
32extern ia64_mv_dma_alloc_coherent sba_alloc_coherent;
33extern ia64_mv_dma_free_coherent sba_free_coherent;
Arthur Kepner309df0c2008-04-29 01:00:32 -070034extern ia64_mv_dma_map_single_attrs sba_map_single_attrs;
35extern ia64_mv_dma_unmap_single_attrs sba_unmap_single_attrs;
36extern ia64_mv_dma_map_sg_attrs sba_map_sg_attrs;
37extern ia64_mv_dma_unmap_sg_attrs sba_unmap_sg_attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038extern ia64_mv_dma_supported sba_dma_supported;
39extern ia64_mv_dma_mapping_error sba_dma_mapping_error;
40
41#define hwiommu_alloc_coherent sba_alloc_coherent
42#define hwiommu_free_coherent sba_free_coherent
Arthur Kepner309df0c2008-04-29 01:00:32 -070043#define hwiommu_map_single_attrs sba_map_single_attrs
44#define hwiommu_unmap_single_attrs sba_unmap_single_attrs
45#define hwiommu_map_sg_attrs sba_map_sg_attrs
46#define hwiommu_unmap_sg_attrs sba_unmap_sg_attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define hwiommu_dma_supported sba_dma_supported
48#define hwiommu_dma_mapping_error sba_dma_mapping_error
49#define hwiommu_sync_single_for_cpu machvec_dma_sync_single
50#define hwiommu_sync_sg_for_cpu machvec_dma_sync_sg
51#define hwiommu_sync_single_for_device machvec_dma_sync_single
52#define hwiommu_sync_sg_for_device machvec_dma_sync_sg
53
54
55/*
56 * Note: we need to make the determination of whether or not to use
57 * the sw I/O TLB based purely on the device structure. Anything else
58 * would be unreliable or would be too intrusive.
59 */
60static inline int
61use_swiotlb (struct device *dev)
62{
63 return dev && dev->dma_mask && !hwiommu_dma_supported(dev, *dev->dma_mask);
64}
65
Tony Luck9a86bbb2007-05-10 11:57:58 -070066void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -070067hwsw_init (void)
68{
69 /* default to a smallish 2MB sw I/O TLB */
Alex Williamson0b9afed2005-09-06 11:20:49 -060070 if (swiotlb_late_init_with_default_size (2 * (1<<20)) != 0) {
71#ifdef CONFIG_IA64_GENERIC
72 /* Better to have normal DMA than panic */
73 printk(KERN_WARNING "%s: Failed to initialize software I/O TLB,"
Harvey Harrisond4ed8082008-03-04 15:15:00 -080074 " reverting to hpzx1 platform vector\n", __func__);
Alex Williamson0b9afed2005-09-06 11:20:49 -060075 machvec_init("hpzx1");
76#else
77 panic("Unable to initialize software I/O TLB services");
78#endif
79 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82void *
Al Viro06a54492005-10-21 03:21:03 -040083hwsw_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 if (use_swiotlb(dev))
86 return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
87 else
88 return hwiommu_alloc_coherent(dev, size, dma_handle, flags);
89}
90
91void
92hwsw_free_coherent (struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle)
93{
94 if (use_swiotlb(dev))
95 swiotlb_free_coherent(dev, size, vaddr, dma_handle);
96 else
97 hwiommu_free_coherent(dev, size, vaddr, dma_handle);
98}
99
100dma_addr_t
Arthur Kepner309df0c2008-04-29 01:00:32 -0700101hwsw_map_single_attrs(struct device *dev, void *addr, size_t size, int dir,
102 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 if (use_swiotlb(dev))
Arthur Kepner309df0c2008-04-29 01:00:32 -0700105 return swiotlb_map_single_attrs(dev, addr, size, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 else
Arthur Kepner309df0c2008-04-29 01:00:32 -0700107 return hwiommu_map_single_attrs(dev, addr, size, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700109EXPORT_SYMBOL(hwsw_map_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700112hwsw_unmap_single_attrs(struct device *dev, dma_addr_t iova, size_t size,
113 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 if (use_swiotlb(dev))
Arthur Kepner309df0c2008-04-29 01:00:32 -0700116 return swiotlb_unmap_single_attrs(dev, iova, size, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 else
Arthur Kepner309df0c2008-04-29 01:00:32 -0700118 return hwiommu_unmap_single_attrs(dev, iova, size, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700120EXPORT_SYMBOL(hwsw_unmap_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122int
Arthur Kepner309df0c2008-04-29 01:00:32 -0700123hwsw_map_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
124 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 if (use_swiotlb(dev))
Arthur Kepner309df0c2008-04-29 01:00:32 -0700127 return swiotlb_map_sg_attrs(dev, sglist, nents, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 else
Arthur Kepner309df0c2008-04-29 01:00:32 -0700129 return hwiommu_map_sg_attrs(dev, sglist, nents, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700131EXPORT_SYMBOL(hwsw_map_sg_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700134hwsw_unmap_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
135 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 if (use_swiotlb(dev))
Arthur Kepner309df0c2008-04-29 01:00:32 -0700138 return swiotlb_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 else
Arthur Kepner309df0c2008-04-29 01:00:32 -0700140 return hwiommu_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700142EXPORT_SYMBOL(hwsw_unmap_sg_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144void
145hwsw_sync_single_for_cpu (struct device *dev, dma_addr_t addr, size_t size, int dir)
146{
147 if (use_swiotlb(dev))
148 swiotlb_sync_single_for_cpu(dev, addr, size, dir);
149 else
150 hwiommu_sync_single_for_cpu(dev, addr, size, dir);
151}
152
153void
154hwsw_sync_sg_for_cpu (struct device *dev, struct scatterlist *sg, int nelems, int dir)
155{
156 if (use_swiotlb(dev))
157 swiotlb_sync_sg_for_cpu(dev, sg, nelems, dir);
158 else
159 hwiommu_sync_sg_for_cpu(dev, sg, nelems, dir);
160}
161
162void
163hwsw_sync_single_for_device (struct device *dev, dma_addr_t addr, size_t size, int dir)
164{
165 if (use_swiotlb(dev))
166 swiotlb_sync_single_for_device(dev, addr, size, dir);
167 else
168 hwiommu_sync_single_for_device(dev, addr, size, dir);
169}
170
171void
172hwsw_sync_sg_for_device (struct device *dev, struct scatterlist *sg, int nelems, int dir)
173{
174 if (use_swiotlb(dev))
175 swiotlb_sync_sg_for_device(dev, sg, nelems, dir);
176 else
177 hwiommu_sync_sg_for_device(dev, sg, nelems, dir);
178}
179
180int
181hwsw_dma_supported (struct device *dev, u64 mask)
182{
183 if (hwiommu_dma_supported(dev, mask))
184 return 1;
185 return swiotlb_dma_supported(dev, mask);
186}
187
188int
189hwsw_dma_mapping_error (dma_addr_t dma_addr)
190{
191 return hwiommu_dma_mapping_error (dma_addr) || swiotlb_dma_mapping_error(dma_addr);
192}
193
194EXPORT_SYMBOL(hwsw_dma_mapping_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195EXPORT_SYMBOL(hwsw_dma_supported);
196EXPORT_SYMBOL(hwsw_alloc_coherent);
197EXPORT_SYMBOL(hwsw_free_coherent);
Jan Beulich671496a2007-02-05 16:20:03 -0800198EXPORT_SYMBOL(hwsw_sync_single_for_cpu);
199EXPORT_SYMBOL(hwsw_sync_single_for_device);
200EXPORT_SYMBOL(hwsw_sync_sg_for_cpu);
201EXPORT_SYMBOL(hwsw_sync_sg_for_device);