blob: 22145ded58f61c2fdd0549c90a969a1e9c494e50 [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>
FUJITA Tomonori917f69b2009-01-05 23:36:08 +090016#include <linux/dma-mapping.h>
Joerg Roedel9979aa72008-10-29 14:17:59 -070017#include <linux/swiotlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/machvec.h>
19
20/* swiotlb declarations & definitions: */
Alex Williamson0b9afed2005-09-06 11:20:49 -060021extern int swiotlb_late_init_with_default_size (size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/* hwiommu declarations & definitions: */
24
25extern ia64_mv_dma_alloc_coherent sba_alloc_coherent;
26extern ia64_mv_dma_free_coherent sba_free_coherent;
Arthur Kepner309df0c2008-04-29 01:00:32 -070027extern ia64_mv_dma_map_single_attrs sba_map_single_attrs;
28extern ia64_mv_dma_unmap_single_attrs sba_unmap_single_attrs;
29extern ia64_mv_dma_map_sg_attrs sba_map_sg_attrs;
30extern ia64_mv_dma_unmap_sg_attrs sba_unmap_sg_attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031extern ia64_mv_dma_supported sba_dma_supported;
32extern ia64_mv_dma_mapping_error sba_dma_mapping_error;
33
34#define hwiommu_alloc_coherent sba_alloc_coherent
35#define hwiommu_free_coherent sba_free_coherent
Arthur Kepner309df0c2008-04-29 01:00:32 -070036#define hwiommu_map_single_attrs sba_map_single_attrs
37#define hwiommu_unmap_single_attrs sba_unmap_single_attrs
38#define hwiommu_map_sg_attrs sba_map_sg_attrs
39#define hwiommu_unmap_sg_attrs sba_unmap_sg_attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define hwiommu_dma_supported sba_dma_supported
41#define hwiommu_dma_mapping_error sba_dma_mapping_error
42#define hwiommu_sync_single_for_cpu machvec_dma_sync_single
43#define hwiommu_sync_sg_for_cpu machvec_dma_sync_sg
44#define hwiommu_sync_single_for_device machvec_dma_sync_single
45#define hwiommu_sync_sg_for_device machvec_dma_sync_sg
46
47
48/*
49 * Note: we need to make the determination of whether or not to use
50 * the sw I/O TLB based purely on the device structure. Anything else
51 * would be unreliable or would be too intrusive.
52 */
53static inline int
54use_swiotlb (struct device *dev)
55{
56 return dev && dev->dma_mask && !hwiommu_dma_supported(dev, *dev->dma_mask);
57}
58
FUJITA Tomonori4d9b9772009-01-05 23:36:12 +090059struct dma_mapping_ops hwsw_dma_ops;
60
Tony Luck9a86bbb2007-05-10 11:57:58 -070061void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -070062hwsw_init (void)
63{
FUJITA Tomonori4d9b9772009-01-05 23:36:12 +090064 dma_ops = &hwsw_dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 /* default to a smallish 2MB sw I/O TLB */
Alex Williamson0b9afed2005-09-06 11:20:49 -060066 if (swiotlb_late_init_with_default_size (2 * (1<<20)) != 0) {
67#ifdef CONFIG_IA64_GENERIC
68 /* Better to have normal DMA than panic */
69 printk(KERN_WARNING "%s: Failed to initialize software I/O TLB,"
Harvey Harrisond4ed8082008-03-04 15:15:00 -080070 " reverting to hpzx1 platform vector\n", __func__);
Alex Williamson0b9afed2005-09-06 11:20:49 -060071 machvec_init("hpzx1");
72#else
73 panic("Unable to initialize software I/O TLB services");
74#endif
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78void *
Al Viro06a54492005-10-21 03:21:03 -040079hwsw_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 if (use_swiotlb(dev))
82 return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
83 else
84 return hwiommu_alloc_coherent(dev, size, dma_handle, flags);
85}
86
87void
88hwsw_free_coherent (struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle)
89{
90 if (use_swiotlb(dev))
91 swiotlb_free_coherent(dev, size, vaddr, dma_handle);
92 else
93 hwiommu_free_coherent(dev, size, vaddr, dma_handle);
94}
95
96dma_addr_t
Arthur Kepner309df0c2008-04-29 01:00:32 -070097hwsw_map_single_attrs(struct device *dev, void *addr, size_t size, int dir,
98 struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 if (use_swiotlb(dev))
Arthur Kepner309df0c2008-04-29 01:00:32 -0700101 return swiotlb_map_single_attrs(dev, addr, size, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 else
Arthur Kepner309df0c2008-04-29 01:00:32 -0700103 return hwiommu_map_single_attrs(dev, addr, size, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700105EXPORT_SYMBOL(hwsw_map_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700108hwsw_unmap_single_attrs(struct device *dev, dma_addr_t iova, size_t size,
109 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 if (use_swiotlb(dev))
Arthur Kepner309df0c2008-04-29 01:00:32 -0700112 return swiotlb_unmap_single_attrs(dev, iova, size, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 else
Arthur Kepner309df0c2008-04-29 01:00:32 -0700114 return hwiommu_unmap_single_attrs(dev, iova, size, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700116EXPORT_SYMBOL(hwsw_unmap_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118int
Arthur Kepner309df0c2008-04-29 01:00:32 -0700119hwsw_map_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
120 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 if (use_swiotlb(dev))
Arthur Kepner309df0c2008-04-29 01:00:32 -0700123 return swiotlb_map_sg_attrs(dev, sglist, nents, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 else
Arthur Kepner309df0c2008-04-29 01:00:32 -0700125 return hwiommu_map_sg_attrs(dev, sglist, nents, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700127EXPORT_SYMBOL(hwsw_map_sg_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700130hwsw_unmap_sg_attrs(struct device *dev, struct scatterlist *sglist, int nents,
131 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 if (use_swiotlb(dev))
Arthur Kepner309df0c2008-04-29 01:00:32 -0700134 return swiotlb_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 else
Arthur Kepner309df0c2008-04-29 01:00:32 -0700136 return hwiommu_unmap_sg_attrs(dev, sglist, nents, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700138EXPORT_SYMBOL(hwsw_unmap_sg_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140void
141hwsw_sync_single_for_cpu (struct device *dev, dma_addr_t addr, size_t size, int dir)
142{
143 if (use_swiotlb(dev))
144 swiotlb_sync_single_for_cpu(dev, addr, size, dir);
145 else
146 hwiommu_sync_single_for_cpu(dev, addr, size, dir);
147}
148
149void
150hwsw_sync_sg_for_cpu (struct device *dev, struct scatterlist *sg, int nelems, int dir)
151{
152 if (use_swiotlb(dev))
153 swiotlb_sync_sg_for_cpu(dev, sg, nelems, dir);
154 else
155 hwiommu_sync_sg_for_cpu(dev, sg, nelems, dir);
156}
157
158void
159hwsw_sync_single_for_device (struct device *dev, dma_addr_t addr, size_t size, int dir)
160{
161 if (use_swiotlb(dev))
162 swiotlb_sync_single_for_device(dev, addr, size, dir);
163 else
164 hwiommu_sync_single_for_device(dev, addr, size, dir);
165}
166
167void
168hwsw_sync_sg_for_device (struct device *dev, struct scatterlist *sg, int nelems, int dir)
169{
170 if (use_swiotlb(dev))
171 swiotlb_sync_sg_for_device(dev, sg, nelems, dir);
172 else
173 hwiommu_sync_sg_for_device(dev, sg, nelems, dir);
174}
175
176int
177hwsw_dma_supported (struct device *dev, u64 mask)
178{
179 if (hwiommu_dma_supported(dev, mask))
180 return 1;
181 return swiotlb_dma_supported(dev, mask);
182}
183
184int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700185hwsw_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700187 return hwiommu_dma_mapping_error(dev, dma_addr) ||
188 swiotlb_dma_mapping_error(dev, dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191EXPORT_SYMBOL(hwsw_dma_mapping_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192EXPORT_SYMBOL(hwsw_dma_supported);
193EXPORT_SYMBOL(hwsw_alloc_coherent);
194EXPORT_SYMBOL(hwsw_free_coherent);
Jan Beulich671496a2007-02-05 16:20:03 -0800195EXPORT_SYMBOL(hwsw_sync_single_for_cpu);
196EXPORT_SYMBOL(hwsw_sync_single_for_device);
197EXPORT_SYMBOL(hwsw_sync_sg_for_cpu);
198EXPORT_SYMBOL(hwsw_sync_sg_for_device);
FUJITA Tomonori917f69b2009-01-05 23:36:08 +0900199
200struct dma_mapping_ops hwsw_dma_ops = {
201 .alloc_coherent = hwsw_alloc_coherent,
202 .free_coherent = hwsw_free_coherent,
203 .map_single_attrs = hwsw_map_single_attrs,
204 .unmap_single_attrs = hwsw_unmap_single_attrs,
205 .map_sg_attrs = hwsw_map_sg_attrs,
206 .unmap_sg_attrs = hwsw_unmap_sg_attrs,
207 .sync_single_for_cpu = hwsw_sync_single_for_cpu,
208 .sync_sg_for_cpu = hwsw_sync_sg_for_cpu,
209 .sync_single_for_device = hwsw_sync_single_for_device,
210 .sync_sg_for_device = hwsw_sync_sg_for_device,
211 .dma_supported_op = hwsw_dma_supported,
212 .mapping_error = hwsw_dma_mapping_error,
213};