x86_64: speedup touch_nmi_watchdog
[linux-2.6] / arch / x86_64 / kernel / pci-dma.c
1 /*
2  * Dynamic DMA mapping support.
3  */
4
5 #include <linux/types.h>
6 #include <linux/mm.h>
7 #include <linux/string.h>
8 #include <linux/pci.h>
9 #include <linux/module.h>
10 #include <asm/io.h>
11 #include <asm/proto.h>
12 #include <asm/calgary.h>
13
14 int iommu_merge __read_mostly = 0;
15 EXPORT_SYMBOL(iommu_merge);
16
17 dma_addr_t bad_dma_address __read_mostly;
18 EXPORT_SYMBOL(bad_dma_address);
19
20 /* This tells the BIO block layer to assume merging. Default to off
21    because we cannot guarantee merging later. */
22 int iommu_bio_merge __read_mostly = 0;
23 EXPORT_SYMBOL(iommu_bio_merge);
24
25 static int iommu_sac_force __read_mostly = 0;
26
27 int no_iommu __read_mostly;
28 #ifdef CONFIG_IOMMU_DEBUG
29 int panic_on_overflow __read_mostly = 1;
30 int force_iommu __read_mostly = 1;
31 #else
32 int panic_on_overflow __read_mostly = 0;
33 int force_iommu __read_mostly= 0;
34 #endif
35
36 /* Set this to 1 if there is a HW IOMMU in the system */
37 int iommu_detected __read_mostly = 0;
38
39 /* Dummy device used for NULL arguments (normally ISA). Better would
40    be probably a smaller DMA mask, but this is bug-to-bug compatible
41    to i386. */
42 struct device fallback_dev = {
43         .bus_id = "fallback device",
44         .coherent_dma_mask = DMA_32BIT_MASK,
45         .dma_mask = &fallback_dev.coherent_dma_mask,
46 };
47
48 /* Allocate DMA memory on node near device */
49 noinline static void *
50 dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
51 {
52         struct page *page;
53         int node;
54 #ifdef CONFIG_PCI
55         if (dev->bus == &pci_bus_type)
56                 node = pcibus_to_node(to_pci_dev(dev)->bus);
57         else
58 #endif
59                 node = numa_node_id();
60
61         if (node < first_node(node_online_map))
62                 node = first_node(node_online_map);
63
64         page = alloc_pages_node(node, gfp, order);
65         return page ? page_address(page) : NULL;
66 }
67
68 /*
69  * Allocate memory for a coherent mapping.
70  */
71 void *
72 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
73                    gfp_t gfp)
74 {
75         void *memory;
76         unsigned long dma_mask = 0;
77         u64 bus;
78
79         if (!dev)
80                 dev = &fallback_dev;
81         dma_mask = dev->coherent_dma_mask;
82         if (dma_mask == 0)
83                 dma_mask = DMA_32BIT_MASK;
84
85         /* Don't invoke OOM killer */
86         gfp |= __GFP_NORETRY;
87
88         /* Kludge to make it bug-to-bug compatible with i386. i386
89            uses the normal dma_mask for alloc_coherent. */
90         dma_mask &= *dev->dma_mask;
91
92         /* Why <=? Even when the mask is smaller than 4GB it is often
93            larger than 16MB and in this case we have a chance of
94            finding fitting memory in the next higher zone first. If
95            not retry with true GFP_DMA. -AK */
96         if (dma_mask <= DMA_32BIT_MASK)
97                 gfp |= GFP_DMA32;
98
99  again:
100         memory = dma_alloc_pages(dev, gfp, get_order(size));
101         if (memory == NULL)
102                 return NULL;
103
104         {
105                 int high, mmu;
106                 bus = virt_to_bus(memory);
107                 high = (bus + size) >= dma_mask;
108                 mmu = high;
109                 if (force_iommu && !(gfp & GFP_DMA))
110                         mmu = 1;
111                 else if (high) {
112                         free_pages((unsigned long)memory,
113                                    get_order(size));
114
115                         /* Don't use the 16MB ZONE_DMA unless absolutely
116                            needed. It's better to use remapping first. */
117                         if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
118                                 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
119                                 goto again;
120                         }
121
122                         /* Let low level make its own zone decisions */
123                         gfp &= ~(GFP_DMA32|GFP_DMA);
124
125                         if (dma_ops->alloc_coherent)
126                                 return dma_ops->alloc_coherent(dev, size,
127                                                            dma_handle, gfp);
128                         return NULL;
129                 }
130
131                 memset(memory, 0, size);
132                 if (!mmu) {
133                         *dma_handle = virt_to_bus(memory);
134                         return memory;
135                 }
136         }
137
138         if (dma_ops->alloc_coherent) {
139                 free_pages((unsigned long)memory, get_order(size));
140                 gfp &= ~(GFP_DMA|GFP_DMA32);
141                 return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
142         }
143
144         if (dma_ops->map_simple) {
145                 *dma_handle = dma_ops->map_simple(dev, memory,
146                                               size,
147                                               PCI_DMA_BIDIRECTIONAL);
148                 if (*dma_handle != bad_dma_address)
149                         return memory;
150         }
151
152         if (panic_on_overflow)
153                 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
154         free_pages((unsigned long)memory, get_order(size));
155         return NULL;
156 }
157 EXPORT_SYMBOL(dma_alloc_coherent);
158
159 /*
160  * Unmap coherent memory.
161  * The caller must ensure that the device has finished accessing the mapping.
162  */
163 void dma_free_coherent(struct device *dev, size_t size,
164                          void *vaddr, dma_addr_t bus)
165 {
166         if (dma_ops->unmap_single)
167                 dma_ops->unmap_single(dev, bus, size, 0);
168         free_pages((unsigned long)vaddr, get_order(size));
169 }
170 EXPORT_SYMBOL(dma_free_coherent);
171
172 static int forbid_dac __read_mostly;
173
174 int dma_supported(struct device *dev, u64 mask)
175 {
176 #ifdef CONFIG_PCI
177         if (mask > 0xffffffff && forbid_dac > 0) {
178
179
180
181                 printk(KERN_INFO "PCI: Disallowing DAC for device %s\n", dev->bus_id);
182                 return 0;
183         }
184 #endif
185
186         if (dma_ops->dma_supported)
187                 return dma_ops->dma_supported(dev, mask);
188
189         /* Copied from i386. Doesn't make much sense, because it will
190            only work for pci_alloc_coherent.
191            The caller just has to use GFP_DMA in this case. */
192         if (mask < DMA_24BIT_MASK)
193                 return 0;
194
195         /* Tell the device to use SAC when IOMMU force is on.  This
196            allows the driver to use cheaper accesses in some cases.
197
198            Problem with this is that if we overflow the IOMMU area and
199            return DAC as fallback address the device may not handle it
200            correctly.
201
202            As a special case some controllers have a 39bit address
203            mode that is as efficient as 32bit (aic79xx). Don't force
204            SAC for these.  Assume all masks <= 40 bits are of this
205            type. Normally this doesn't make any difference, but gives
206            more gentle handling of IOMMU overflow. */
207         if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
208                 printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
209                 return 0;
210         }
211
212         return 1;
213 }
214 EXPORT_SYMBOL(dma_supported);
215
216 int dma_set_mask(struct device *dev, u64 mask)
217 {
218         if (!dev->dma_mask || !dma_supported(dev, mask))
219                 return -EIO;
220         *dev->dma_mask = mask;
221         return 0;
222 }
223 EXPORT_SYMBOL(dma_set_mask);
224
225 /*
226  * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
227  * documentation.
228  */
229 __init int iommu_setup(char *p)
230 {
231         iommu_merge = 1;
232
233         if (!p)
234                 return -EINVAL;
235
236         while (*p) {
237                 if (!strncmp(p,"off",3))
238                         no_iommu = 1;
239                 /* gart_parse_options has more force support */
240                 if (!strncmp(p,"force",5))
241                         force_iommu = 1;
242                 if (!strncmp(p,"noforce",7)) {
243                         iommu_merge = 0;
244                         force_iommu = 0;
245                 }
246
247                 if (!strncmp(p, "biomerge",8)) {
248                         iommu_bio_merge = 4096;
249                         iommu_merge = 1;
250                         force_iommu = 1;
251                 }
252                 if (!strncmp(p, "panic",5))
253                         panic_on_overflow = 1;
254                 if (!strncmp(p, "nopanic",7))
255                         panic_on_overflow = 0;
256                 if (!strncmp(p, "merge",5)) {
257                         iommu_merge = 1;
258                         force_iommu = 1;
259                 }
260                 if (!strncmp(p, "nomerge",7))
261                         iommu_merge = 0;
262                 if (!strncmp(p, "forcesac",8))
263                         iommu_sac_force = 1;
264                 if (!strncmp(p, "allowdac", 8))
265                         forbid_dac = 0;
266                 if (!strncmp(p, "nodac", 5))
267                         forbid_dac = -1;
268
269 #ifdef CONFIG_SWIOTLB
270                 if (!strncmp(p, "soft",4))
271                         swiotlb = 1;
272 #endif
273
274 #ifdef CONFIG_IOMMU
275                 gart_parse_options(p);
276 #endif
277
278 #ifdef CONFIG_CALGARY_IOMMU
279                 if (!strncmp(p, "calgary", 7))
280                         use_calgary = 1;
281 #endif /* CONFIG_CALGARY_IOMMU */
282
283                 p += strcspn(p, ",");
284                 if (*p == ',')
285                         ++p;
286         }
287         return 0;
288 }
289 early_param("iommu", iommu_setup);
290
291 void __init pci_iommu_alloc(void)
292 {
293         /*
294          * The order of these functions is important for
295          * fall-back/fail-over reasons
296          */
297 #ifdef CONFIG_IOMMU
298         iommu_hole_init();
299 #endif
300
301 #ifdef CONFIG_CALGARY_IOMMU
302         detect_calgary();
303 #endif
304
305 #ifdef CONFIG_SWIOTLB
306         pci_swiotlb_init();
307 #endif
308 }
309
310 static int __init pci_iommu_init(void)
311 {
312 #ifdef CONFIG_CALGARY_IOMMU
313         calgary_iommu_init();
314 #endif
315
316 #ifdef CONFIG_IOMMU
317         gart_iommu_init();
318 #endif
319
320         no_iommu_init();
321         return 0;
322 }
323
324 #ifdef CONFIG_PCI
325 /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
326
327 static __devinit void via_no_dac(struct pci_dev *dev)
328 {
329         if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
330                 printk(KERN_INFO "PCI: VIA PCI bridge detected. Disabling DAC.\n");
331                 forbid_dac = 1;
332         }
333 }
334 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
335 #endif
336 /* Must execute after PCI subsystem */
337 fs_initcall(pci_iommu_init);