]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/pci-dma_64.c
b956f5945d67b62e47c54aba646483cbeae37cbb
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / pci-dma_64.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 <linux/dmar.h>
11 #include <linux/bootmem.h>
12 #include <asm/proto.h>
13 #include <asm/io.h>
14 #include <asm/gart.h>
15 #include <asm/calgary.h>
16
17
18 /* Dummy device used for NULL arguments (normally ISA). Better would
19    be probably a smaller DMA mask, but this is bug-to-bug compatible
20    to i386. */
21 struct device fallback_dev = {
22         .bus_id = "fallback device",
23         .coherent_dma_mask = DMA_32BIT_MASK,
24         .dma_mask = &fallback_dev.coherent_dma_mask,
25 };
26
27 /* Allocate DMA memory on node near device */
28 noinline static void *
29 dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
30 {
31         int node;
32
33         node = dev_to_node(dev);
34
35         return alloc_pages_node(node, gfp, order);
36 }
37
38 #define dma_alloc_from_coherent_mem(dev, size, handle, ret) (0)
39 #define dma_release_coherent(dev, order, vaddr) (0)
40 /*
41  * Allocate memory for a coherent mapping.
42  */
43 void *
44 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
45                    gfp_t gfp)
46 {
47         void *memory;
48         struct page *page;
49         unsigned long dma_mask = 0;
50         u64 bus;
51
52         /* ignore region specifiers */
53         gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
54
55         if (dma_alloc_from_coherent_mem(dev, size, dma_handle, &memory))
56                 return memory;
57
58         if (!dev)
59                 dev = &fallback_dev;
60         dma_mask = dev->coherent_dma_mask;
61         if (dma_mask == 0)
62                 dma_mask = DMA_32BIT_MASK;
63
64         /* Device not DMA able */
65         if (dev->dma_mask == NULL)
66                 return NULL;
67
68         /* Don't invoke OOM killer */
69         gfp |= __GFP_NORETRY;
70
71         /* Kludge to make it bug-to-bug compatible with i386. i386
72            uses the normal dma_mask for alloc_coherent. */
73         dma_mask &= *dev->dma_mask;
74
75         /* Why <=? Even when the mask is smaller than 4GB it is often
76            larger than 16MB and in this case we have a chance of
77            finding fitting memory in the next higher zone first. If
78            not retry with true GFP_DMA. -AK */
79         if (dma_mask <= DMA_32BIT_MASK)
80                 gfp |= GFP_DMA32;
81
82  again:
83         page = dma_alloc_pages(dev, gfp, get_order(size));
84         if (page == NULL)
85                 return NULL;
86
87         {
88                 int high, mmu;
89                 bus = page_to_phys(page);
90                 memory = page_address(page);
91                 high = (bus + size) >= dma_mask;
92                 mmu = high;
93                 if (force_iommu && !(gfp & GFP_DMA))
94                         mmu = 1;
95                 else if (high) {
96                         free_pages((unsigned long)memory,
97                                    get_order(size));
98
99                         /* Don't use the 16MB ZONE_DMA unless absolutely
100                            needed. It's better to use remapping first. */
101                         if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
102                                 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
103                                 goto again;
104                         }
105
106                         /* Let low level make its own zone decisions */
107                         gfp &= ~(GFP_DMA32|GFP_DMA);
108
109                         if (dma_ops->alloc_coherent)
110                                 return dma_ops->alloc_coherent(dev, size,
111                                                            dma_handle, gfp);
112                         return NULL;
113                 }
114
115                 memset(memory, 0, size);
116                 if (!mmu) {
117                         *dma_handle = bus;
118                         return memory;
119                 }
120         }
121
122         if (dma_ops->alloc_coherent) {
123                 free_pages((unsigned long)memory, get_order(size));
124                 gfp &= ~(GFP_DMA|GFP_DMA32);
125                 return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
126         }
127
128         if (dma_ops->map_simple) {
129                 *dma_handle = dma_ops->map_simple(dev, virt_to_phys(memory),
130                                               size,
131                                               PCI_DMA_BIDIRECTIONAL);
132                 if (*dma_handle != bad_dma_address)
133                         return memory;
134         }
135
136         if (panic_on_overflow)
137                 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
138         free_pages((unsigned long)memory, get_order(size));
139         return NULL;
140 }
141 EXPORT_SYMBOL(dma_alloc_coherent);
142
143 /*
144  * Unmap coherent memory.
145  * The caller must ensure that the device has finished accessing the mapping.
146  */
147 void dma_free_coherent(struct device *dev, size_t size,
148                          void *vaddr, dma_addr_t bus)
149 {
150         int order = get_order(size);
151         WARN_ON(irqs_disabled());       /* for portability */
152         if (dma_release_coherent(dev, order, vaddr))
153                 return;
154         if (dma_ops->unmap_single)
155                 dma_ops->unmap_single(dev, bus, size, 0);
156         free_pages((unsigned long)vaddr, order);
157 }
158 EXPORT_SYMBOL(dma_free_coherent);