Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Dynamic DMA mapping support for AMD Hammer. | |
05fccb0e | 3 | * |
1da177e4 LT |
4 | * Use the integrated AGP GART in the Hammer northbridge as an IOMMU for PCI. |
5 | * This allows to use PCI devices that only support 32bit addresses on systems | |
05fccb0e | 6 | * with more than 4GB. |
1da177e4 | 7 | * |
5872fb94 | 8 | * See Documentation/PCI/PCI-DMA-mapping.txt for the interface specification. |
05fccb0e | 9 | * |
1da177e4 | 10 | * Copyright 2002 Andi Kleen, SuSE Labs. |
ff7f3649 | 11 | * Subject to the GNU General Public License v2 only. |
1da177e4 LT |
12 | */ |
13 | ||
1da177e4 LT |
14 | #include <linux/types.h> |
15 | #include <linux/ctype.h> | |
16 | #include <linux/agp_backend.h> | |
17 | #include <linux/init.h> | |
18 | #include <linux/mm.h> | |
d43c36dc | 19 | #include <linux/sched.h> |
1da177e4 LT |
20 | #include <linux/string.h> |
21 | #include <linux/spinlock.h> | |
22 | #include <linux/pci.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/topology.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/bitops.h> | |
1eeb66a1 | 27 | #include <linux/kdebug.h> |
9ee1bea4 | 28 | #include <linux/scatterlist.h> |
fde9a109 | 29 | #include <linux/iommu-helper.h> |
cd76374e | 30 | #include <linux/sysdev.h> |
237a6224 | 31 | #include <linux/io.h> |
1da177e4 | 32 | #include <asm/atomic.h> |
1da177e4 LT |
33 | #include <asm/mtrr.h> |
34 | #include <asm/pgtable.h> | |
35 | #include <asm/proto.h> | |
46a7fa27 | 36 | #include <asm/iommu.h> |
395624fc | 37 | #include <asm/gart.h> |
1da177e4 | 38 | #include <asm/cacheflush.h> |
17a941d8 MBY |
39 | #include <asm/swiotlb.h> |
40 | #include <asm/dma.h> | |
a32073bf | 41 | #include <asm/k8.h> |
338bac52 | 42 | #include <asm/x86_init.h> |
1da177e4 | 43 | |
79da0874 | 44 | static unsigned long iommu_bus_base; /* GART remapping area (physical) */ |
05fccb0e | 45 | static unsigned long iommu_size; /* size of remapping area bytes */ |
1da177e4 LT |
46 | static unsigned long iommu_pages; /* .. and in pages */ |
47 | ||
05fccb0e | 48 | static u32 *iommu_gatt_base; /* Remapping table */ |
1da177e4 | 49 | |
42109197 FT |
50 | static dma_addr_t bad_dma_addr; |
51 | ||
05fccb0e IM |
52 | /* |
53 | * If this is disabled the IOMMU will use an optimized flushing strategy | |
54 | * of only flushing when an mapping is reused. With it true the GART is | |
55 | * flushed for every mapping. Problem is that doing the lazy flush seems | |
56 | * to trigger bugs with some popular PCI cards, in particular 3ware (but | |
57 | * has been also also seen with Qlogic at least). | |
58 | */ | |
c854c919 | 59 | static int iommu_fullflush = 1; |
1da177e4 | 60 | |
05fccb0e | 61 | /* Allocation bitmap for the remapping area: */ |
1da177e4 | 62 | static DEFINE_SPINLOCK(iommu_bitmap_lock); |
05fccb0e IM |
63 | /* Guarded by iommu_bitmap_lock: */ |
64 | static unsigned long *iommu_gart_bitmap; | |
1da177e4 | 65 | |
05fccb0e | 66 | static u32 gart_unmapped_entry; |
1da177e4 LT |
67 | |
68 | #define GPTE_VALID 1 | |
69 | #define GPTE_COHERENT 2 | |
70 | #define GPTE_ENCODE(x) \ | |
71 | (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT) | |
72 | #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28)) | |
73 | ||
05fccb0e | 74 | #define EMERGENCY_PAGES 32 /* = 128KB */ |
1da177e4 LT |
75 | |
76 | #ifdef CONFIG_AGP | |
77 | #define AGPEXTERN extern | |
78 | #else | |
79 | #define AGPEXTERN | |
80 | #endif | |
81 | ||
82 | /* backdoor interface to AGP driver */ | |
83 | AGPEXTERN int agp_memory_reserved; | |
84 | AGPEXTERN __u32 *agp_gatt_table; | |
85 | ||
86 | static unsigned long next_bit; /* protected by iommu_bitmap_lock */ | |
3610f211 | 87 | static bool need_flush; /* global flush state. set for each gart wrap */ |
1da177e4 | 88 | |
7b22ff53 FT |
89 | static unsigned long alloc_iommu(struct device *dev, int size, |
90 | unsigned long align_mask) | |
05fccb0e | 91 | { |
1da177e4 | 92 | unsigned long offset, flags; |
fde9a109 FT |
93 | unsigned long boundary_size; |
94 | unsigned long base_index; | |
95 | ||
96 | base_index = ALIGN(iommu_bus_base & dma_get_seg_boundary(dev), | |
97 | PAGE_SIZE) >> PAGE_SHIFT; | |
05d3ed0a | 98 | boundary_size = ALIGN((unsigned long long)dma_get_seg_boundary(dev) + 1, |
fde9a109 | 99 | PAGE_SIZE) >> PAGE_SHIFT; |
1da177e4 | 100 | |
05fccb0e | 101 | spin_lock_irqsave(&iommu_bitmap_lock, flags); |
fde9a109 | 102 | offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, next_bit, |
7b22ff53 | 103 | size, base_index, boundary_size, align_mask); |
1da177e4 | 104 | if (offset == -1) { |
3610f211 | 105 | need_flush = true; |
fde9a109 | 106 | offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, 0, |
7b22ff53 FT |
107 | size, base_index, boundary_size, |
108 | align_mask); | |
1da177e4 | 109 | } |
05fccb0e | 110 | if (offset != -1) { |
05fccb0e IM |
111 | next_bit = offset+size; |
112 | if (next_bit >= iommu_pages) { | |
1da177e4 | 113 | next_bit = 0; |
3610f211 | 114 | need_flush = true; |
05fccb0e IM |
115 | } |
116 | } | |
1da177e4 | 117 | if (iommu_fullflush) |
3610f211 | 118 | need_flush = true; |
05fccb0e IM |
119 | spin_unlock_irqrestore(&iommu_bitmap_lock, flags); |
120 | ||
1da177e4 | 121 | return offset; |
05fccb0e | 122 | } |
1da177e4 LT |
123 | |
124 | static void free_iommu(unsigned long offset, int size) | |
05fccb0e | 125 | { |
1da177e4 | 126 | unsigned long flags; |
05fccb0e | 127 | |
1da177e4 | 128 | spin_lock_irqsave(&iommu_bitmap_lock, flags); |
fde9a109 | 129 | iommu_area_free(iommu_gart_bitmap, offset, size); |
70d7d357 JR |
130 | if (offset >= next_bit) |
131 | next_bit = offset + size; | |
1da177e4 | 132 | spin_unlock_irqrestore(&iommu_bitmap_lock, flags); |
05fccb0e | 133 | } |
1da177e4 | 134 | |
05fccb0e | 135 | /* |
1da177e4 LT |
136 | * Use global flush state to avoid races with multiple flushers. |
137 | */ | |
a32073bf | 138 | static void flush_gart(void) |
05fccb0e | 139 | { |
1da177e4 | 140 | unsigned long flags; |
05fccb0e | 141 | |
1da177e4 | 142 | spin_lock_irqsave(&iommu_bitmap_lock, flags); |
a32073bf AK |
143 | if (need_flush) { |
144 | k8_flush_garts(); | |
3610f211 | 145 | need_flush = false; |
05fccb0e | 146 | } |
1da177e4 | 147 | spin_unlock_irqrestore(&iommu_bitmap_lock, flags); |
05fccb0e | 148 | } |
1da177e4 | 149 | |
1da177e4 | 150 | #ifdef CONFIG_IOMMU_LEAK |
1da177e4 | 151 | /* Debugging aid for drivers that don't free their IOMMU tables */ |
1da177e4 | 152 | static int leak_trace; |
79da0874 | 153 | static int iommu_leak_pages = 20; |
05fccb0e | 154 | |
79da0874 | 155 | static void dump_leak(void) |
1da177e4 | 156 | { |
05fccb0e IM |
157 | static int dump; |
158 | ||
19c1a6f5 | 159 | if (dump) |
05fccb0e | 160 | return; |
1da177e4 | 161 | dump = 1; |
05fccb0e | 162 | |
19c1a6f5 FT |
163 | show_stack(NULL, NULL); |
164 | debug_dma_dump_mappings(NULL); | |
1da177e4 | 165 | } |
1da177e4 LT |
166 | #endif |
167 | ||
17a941d8 | 168 | static void iommu_full(struct device *dev, size_t size, int dir) |
1da177e4 | 169 | { |
05fccb0e | 170 | /* |
1da177e4 LT |
171 | * Ran out of IOMMU space for this operation. This is very bad. |
172 | * Unfortunately the drivers cannot handle this operation properly. | |
05fccb0e | 173 | * Return some non mapped prereserved space in the aperture and |
1da177e4 LT |
174 | * let the Northbridge deal with it. This will result in garbage |
175 | * in the IO operation. When the size exceeds the prereserved space | |
05fccb0e | 176 | * memory corruption will occur or random memory will be DMAed |
1da177e4 | 177 | * out. Hopefully no network devices use single mappings that big. |
05fccb0e IM |
178 | */ |
179 | ||
fc3a8828 | 180 | dev_err(dev, "PCI-DMA: Out of IOMMU space for %lu bytes\n", size); |
1da177e4 | 181 | |
17a941d8 | 182 | if (size > PAGE_SIZE*EMERGENCY_PAGES) { |
1da177e4 LT |
183 | if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL) |
184 | panic("PCI-DMA: Memory would be corrupted\n"); | |
05fccb0e IM |
185 | if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL) |
186 | panic(KERN_ERR | |
187 | "PCI-DMA: Random memory would be DMAed\n"); | |
188 | } | |
1da177e4 | 189 | #ifdef CONFIG_IOMMU_LEAK |
05fccb0e | 190 | dump_leak(); |
1da177e4 | 191 | #endif |
05fccb0e | 192 | } |
1da177e4 | 193 | |
05fccb0e IM |
194 | static inline int |
195 | need_iommu(struct device *dev, unsigned long addr, size_t size) | |
196 | { | |
a4c2baa6 | 197 | return force_iommu || !dma_capable(dev, addr, size); |
1da177e4 LT |
198 | } |
199 | ||
05fccb0e IM |
200 | static inline int |
201 | nonforced_iommu(struct device *dev, unsigned long addr, size_t size) | |
202 | { | |
a4c2baa6 | 203 | return !dma_capable(dev, addr, size); |
1da177e4 LT |
204 | } |
205 | ||
206 | /* Map a single continuous physical area into the IOMMU. | |
207 | * Caller needs to check if the iommu is needed and flush. | |
208 | */ | |
17a941d8 | 209 | static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem, |
7b22ff53 | 210 | size_t size, int dir, unsigned long align_mask) |
05fccb0e | 211 | { |
1477b8e5 | 212 | unsigned long npages = iommu_num_pages(phys_mem, size, PAGE_SIZE); |
7b22ff53 | 213 | unsigned long iommu_page = alloc_iommu(dev, npages, align_mask); |
1da177e4 | 214 | int i; |
05fccb0e | 215 | |
1da177e4 LT |
216 | if (iommu_page == -1) { |
217 | if (!nonforced_iommu(dev, phys_mem, size)) | |
05fccb0e | 218 | return phys_mem; |
1da177e4 LT |
219 | if (panic_on_overflow) |
220 | panic("dma_map_area overflow %lu bytes\n", size); | |
17a941d8 | 221 | iommu_full(dev, size, dir); |
42109197 | 222 | return bad_dma_addr; |
1da177e4 LT |
223 | } |
224 | ||
225 | for (i = 0; i < npages; i++) { | |
226 | iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem); | |
1da177e4 LT |
227 | phys_mem += PAGE_SIZE; |
228 | } | |
229 | return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK); | |
230 | } | |
231 | ||
232 | /* Map a single area into the IOMMU */ | |
052aedbf FT |
233 | static dma_addr_t gart_map_page(struct device *dev, struct page *page, |
234 | unsigned long offset, size_t size, | |
235 | enum dma_data_direction dir, | |
236 | struct dma_attrs *attrs) | |
1da177e4 | 237 | { |
2be62149 | 238 | unsigned long bus; |
052aedbf | 239 | phys_addr_t paddr = page_to_phys(page) + offset; |
1da177e4 | 240 | |
1da177e4 | 241 | if (!dev) |
6c505ce3 | 242 | dev = &x86_dma_fallback_dev; |
1da177e4 | 243 | |
2be62149 IM |
244 | if (!need_iommu(dev, paddr, size)) |
245 | return paddr; | |
1da177e4 | 246 | |
7b22ff53 FT |
247 | bus = dma_map_area(dev, paddr, size, dir, 0); |
248 | flush_gart(); | |
05fccb0e IM |
249 | |
250 | return bus; | |
17a941d8 MBY |
251 | } |
252 | ||
7c2d9cd2 JM |
253 | /* |
254 | * Free a DMA mapping. | |
255 | */ | |
052aedbf FT |
256 | static void gart_unmap_page(struct device *dev, dma_addr_t dma_addr, |
257 | size_t size, enum dma_data_direction dir, | |
258 | struct dma_attrs *attrs) | |
7c2d9cd2 JM |
259 | { |
260 | unsigned long iommu_page; | |
261 | int npages; | |
262 | int i; | |
263 | ||
264 | if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE || | |
265 | dma_addr >= iommu_bus_base + iommu_size) | |
266 | return; | |
05fccb0e | 267 | |
7c2d9cd2 | 268 | iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT; |
1477b8e5 | 269 | npages = iommu_num_pages(dma_addr, size, PAGE_SIZE); |
7c2d9cd2 JM |
270 | for (i = 0; i < npages; i++) { |
271 | iommu_gatt_base[iommu_page + i] = gart_unmapped_entry; | |
7c2d9cd2 JM |
272 | } |
273 | free_iommu(iommu_page, npages); | |
274 | } | |
275 | ||
17a941d8 MBY |
276 | /* |
277 | * Wrapper for pci_unmap_single working with scatterlists. | |
278 | */ | |
160c1d8e FT |
279 | static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
280 | enum dma_data_direction dir, struct dma_attrs *attrs) | |
17a941d8 | 281 | { |
9ee1bea4 | 282 | struct scatterlist *s; |
17a941d8 MBY |
283 | int i; |
284 | ||
9ee1bea4 | 285 | for_each_sg(sg, s, nents, i) { |
60b08c67 | 286 | if (!s->dma_length || !s->length) |
17a941d8 | 287 | break; |
d7dff840 | 288 | gart_unmap_page(dev, s->dma_address, s->dma_length, dir, NULL); |
17a941d8 MBY |
289 | } |
290 | } | |
1da177e4 LT |
291 | |
292 | /* Fallback for dma_map_sg in case of overflow */ | |
293 | static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg, | |
294 | int nents, int dir) | |
295 | { | |
9ee1bea4 | 296 | struct scatterlist *s; |
1da177e4 LT |
297 | int i; |
298 | ||
299 | #ifdef CONFIG_IOMMU_DEBUG | |
300 | printk(KERN_DEBUG "dma_map_sg overflow\n"); | |
301 | #endif | |
302 | ||
9ee1bea4 | 303 | for_each_sg(sg, s, nents, i) { |
58b053e4 | 304 | unsigned long addr = sg_phys(s); |
05fccb0e IM |
305 | |
306 | if (nonforced_iommu(dev, addr, s->length)) { | |
7b22ff53 | 307 | addr = dma_map_area(dev, addr, s->length, dir, 0); |
42109197 | 308 | if (addr == bad_dma_addr) { |
05fccb0e | 309 | if (i > 0) |
160c1d8e | 310 | gart_unmap_sg(dev, sg, i, dir, NULL); |
05fccb0e | 311 | nents = 0; |
1da177e4 LT |
312 | sg[0].dma_length = 0; |
313 | break; | |
314 | } | |
315 | } | |
316 | s->dma_address = addr; | |
317 | s->dma_length = s->length; | |
318 | } | |
a32073bf | 319 | flush_gart(); |
05fccb0e | 320 | |
1da177e4 LT |
321 | return nents; |
322 | } | |
323 | ||
324 | /* Map multiple scatterlist entries continuous into the first. */ | |
fde9a109 FT |
325 | static int __dma_map_cont(struct device *dev, struct scatterlist *start, |
326 | int nelems, struct scatterlist *sout, | |
327 | unsigned long pages) | |
1da177e4 | 328 | { |
7b22ff53 | 329 | unsigned long iommu_start = alloc_iommu(dev, pages, 0); |
05fccb0e | 330 | unsigned long iommu_page = iommu_start; |
9ee1bea4 | 331 | struct scatterlist *s; |
1da177e4 LT |
332 | int i; |
333 | ||
334 | if (iommu_start == -1) | |
335 | return -1; | |
9ee1bea4 JA |
336 | |
337 | for_each_sg(start, s, nelems, i) { | |
1da177e4 LT |
338 | unsigned long pages, addr; |
339 | unsigned long phys_addr = s->dma_address; | |
05fccb0e | 340 | |
9ee1bea4 JA |
341 | BUG_ON(s != start && s->offset); |
342 | if (s == start) { | |
1da177e4 LT |
343 | sout->dma_address = iommu_bus_base; |
344 | sout->dma_address += iommu_page*PAGE_SIZE + s->offset; | |
345 | sout->dma_length = s->length; | |
05fccb0e IM |
346 | } else { |
347 | sout->dma_length += s->length; | |
1da177e4 LT |
348 | } |
349 | ||
350 | addr = phys_addr; | |
1477b8e5 | 351 | pages = iommu_num_pages(s->offset, s->length, PAGE_SIZE); |
05fccb0e IM |
352 | while (pages--) { |
353 | iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr); | |
1da177e4 LT |
354 | addr += PAGE_SIZE; |
355 | iommu_page++; | |
0d541064 | 356 | } |
05fccb0e IM |
357 | } |
358 | BUG_ON(iommu_page - iommu_start != pages); | |
359 | ||
1da177e4 LT |
360 | return 0; |
361 | } | |
362 | ||
05fccb0e | 363 | static inline int |
fde9a109 FT |
364 | dma_map_cont(struct device *dev, struct scatterlist *start, int nelems, |
365 | struct scatterlist *sout, unsigned long pages, int need) | |
1da177e4 | 366 | { |
9ee1bea4 JA |
367 | if (!need) { |
368 | BUG_ON(nelems != 1); | |
e88a39de | 369 | sout->dma_address = start->dma_address; |
9ee1bea4 | 370 | sout->dma_length = start->length; |
1da177e4 | 371 | return 0; |
9ee1bea4 | 372 | } |
fde9a109 | 373 | return __dma_map_cont(dev, start, nelems, sout, pages); |
1da177e4 | 374 | } |
05fccb0e | 375 | |
1da177e4 LT |
376 | /* |
377 | * DMA map all entries in a scatterlist. | |
05fccb0e | 378 | * Merge chunks that have page aligned sizes into a continuous mapping. |
1da177e4 | 379 | */ |
160c1d8e FT |
380 | static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
381 | enum dma_data_direction dir, struct dma_attrs *attrs) | |
1da177e4 | 382 | { |
9ee1bea4 | 383 | struct scatterlist *s, *ps, *start_sg, *sgmap; |
05fccb0e IM |
384 | int need = 0, nextneed, i, out, start; |
385 | unsigned long pages = 0; | |
42d00284 FT |
386 | unsigned int seg_size; |
387 | unsigned int max_seg_size; | |
1da177e4 | 388 | |
05fccb0e | 389 | if (nents == 0) |
1da177e4 LT |
390 | return 0; |
391 | ||
1da177e4 | 392 | if (!dev) |
6c505ce3 | 393 | dev = &x86_dma_fallback_dev; |
1da177e4 LT |
394 | |
395 | out = 0; | |
396 | start = 0; | |
9ee1bea4 | 397 | start_sg = sgmap = sg; |
42d00284 FT |
398 | seg_size = 0; |
399 | max_seg_size = dma_get_max_seg_size(dev); | |
9ee1bea4 JA |
400 | ps = NULL; /* shut up gcc */ |
401 | for_each_sg(sg, s, nents, i) { | |
58b053e4 | 402 | dma_addr_t addr = sg_phys(s); |
05fccb0e | 403 | |
1da177e4 | 404 | s->dma_address = addr; |
05fccb0e | 405 | BUG_ON(s->length == 0); |
1da177e4 | 406 | |
05fccb0e | 407 | nextneed = need_iommu(dev, addr, s->length); |
1da177e4 LT |
408 | |
409 | /* Handle the previous not yet processed entries */ | |
410 | if (i > start) { | |
05fccb0e IM |
411 | /* |
412 | * Can only merge when the last chunk ends on a | |
413 | * page boundary and the new one doesn't have an | |
414 | * offset. | |
415 | */ | |
1da177e4 | 416 | if (!iommu_merge || !nextneed || !need || s->offset || |
42d00284 | 417 | (s->length + seg_size > max_seg_size) || |
9ee1bea4 | 418 | (ps->offset + ps->length) % PAGE_SIZE) { |
fde9a109 FT |
419 | if (dma_map_cont(dev, start_sg, i - start, |
420 | sgmap, pages, need) < 0) | |
1da177e4 LT |
421 | goto error; |
422 | out++; | |
42d00284 | 423 | seg_size = 0; |
9ee1bea4 | 424 | sgmap = sg_next(sgmap); |
1da177e4 | 425 | pages = 0; |
9ee1bea4 JA |
426 | start = i; |
427 | start_sg = s; | |
1da177e4 LT |
428 | } |
429 | } | |
430 | ||
42d00284 | 431 | seg_size += s->length; |
1da177e4 | 432 | need = nextneed; |
1477b8e5 | 433 | pages += iommu_num_pages(s->offset, s->length, PAGE_SIZE); |
9ee1bea4 | 434 | ps = s; |
1da177e4 | 435 | } |
fde9a109 | 436 | if (dma_map_cont(dev, start_sg, i - start, sgmap, pages, need) < 0) |
1da177e4 LT |
437 | goto error; |
438 | out++; | |
a32073bf | 439 | flush_gart(); |
9ee1bea4 JA |
440 | if (out < nents) { |
441 | sgmap = sg_next(sgmap); | |
442 | sgmap->dma_length = 0; | |
443 | } | |
1da177e4 LT |
444 | return out; |
445 | ||
446 | error: | |
a32073bf | 447 | flush_gart(); |
160c1d8e | 448 | gart_unmap_sg(dev, sg, out, dir, NULL); |
05fccb0e | 449 | |
a1002a48 KV |
450 | /* When it was forced or merged try again in a dumb way */ |
451 | if (force_iommu || iommu_merge) { | |
452 | out = dma_map_sg_nonforce(dev, sg, nents, dir); | |
453 | if (out > 0) | |
454 | return out; | |
455 | } | |
1da177e4 LT |
456 | if (panic_on_overflow) |
457 | panic("dma_map_sg: overflow on %lu pages\n", pages); | |
05fccb0e | 458 | |
17a941d8 | 459 | iommu_full(dev, pages << PAGE_SHIFT, dir); |
9ee1bea4 | 460 | for_each_sg(sg, s, nents, i) |
42109197 | 461 | s->dma_address = bad_dma_addr; |
1da177e4 | 462 | return 0; |
05fccb0e | 463 | } |
1da177e4 | 464 | |
94581094 JR |
465 | /* allocate and map a coherent mapping */ |
466 | static void * | |
467 | gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr, | |
468 | gfp_t flag) | |
469 | { | |
f6a32a36 | 470 | dma_addr_t paddr; |
421076e2 | 471 | unsigned long align_mask; |
1d990882 FT |
472 | struct page *page; |
473 | ||
474 | if (force_iommu && !(flag & GFP_DMA)) { | |
475 | flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32); | |
476 | page = alloc_pages(flag | __GFP_ZERO, get_order(size)); | |
477 | if (!page) | |
478 | return NULL; | |
479 | ||
480 | align_mask = (1UL << get_order(size)) - 1; | |
481 | paddr = dma_map_area(dev, page_to_phys(page), size, | |
482 | DMA_BIDIRECTIONAL, align_mask); | |
483 | ||
484 | flush_gart(); | |
42109197 | 485 | if (paddr != bad_dma_addr) { |
1d990882 FT |
486 | *dma_addr = paddr; |
487 | return page_address(page); | |
488 | } | |
489 | __free_pages(page, get_order(size)); | |
490 | } else | |
491 | return dma_generic_alloc_coherent(dev, size, dma_addr, flag); | |
94581094 JR |
492 | |
493 | return NULL; | |
494 | } | |
495 | ||
43a5a5a0 JR |
496 | /* free a coherent mapping */ |
497 | static void | |
498 | gart_free_coherent(struct device *dev, size_t size, void *vaddr, | |
499 | dma_addr_t dma_addr) | |
500 | { | |
d7dff840 | 501 | gart_unmap_page(dev, dma_addr, size, DMA_BIDIRECTIONAL, NULL); |
43a5a5a0 JR |
502 | free_pages((unsigned long)vaddr, get_order(size)); |
503 | } | |
504 | ||
42109197 FT |
505 | static int gart_mapping_error(struct device *dev, dma_addr_t dma_addr) |
506 | { | |
507 | return (dma_addr == bad_dma_addr); | |
508 | } | |
509 | ||
17a941d8 | 510 | static int no_agp; |
1da177e4 LT |
511 | |
512 | static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size) | |
05fccb0e IM |
513 | { |
514 | unsigned long a; | |
515 | ||
516 | if (!iommu_size) { | |
517 | iommu_size = aper_size; | |
518 | if (!no_agp) | |
519 | iommu_size /= 2; | |
520 | } | |
521 | ||
522 | a = aper + iommu_size; | |
31422c51 | 523 | iommu_size -= round_up(a, PMD_PAGE_SIZE) - a; |
1da177e4 | 524 | |
05fccb0e | 525 | if (iommu_size < 64*1024*1024) { |
1da177e4 | 526 | printk(KERN_WARNING |
05fccb0e IM |
527 | "PCI-DMA: Warning: Small IOMMU %luMB." |
528 | " Consider increasing the AGP aperture in BIOS\n", | |
529 | iommu_size >> 20); | |
530 | } | |
531 | ||
1da177e4 | 532 | return iommu_size; |
05fccb0e | 533 | } |
1da177e4 | 534 | |
05fccb0e IM |
535 | static __init unsigned read_aperture(struct pci_dev *dev, u32 *size) |
536 | { | |
537 | unsigned aper_size = 0, aper_base_32, aper_order; | |
1da177e4 | 538 | u64 aper_base; |
1da177e4 | 539 | |
3bb6fbf9 PM |
540 | pci_read_config_dword(dev, AMD64_GARTAPERTUREBASE, &aper_base_32); |
541 | pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &aper_order); | |
05fccb0e | 542 | aper_order = (aper_order >> 1) & 7; |
1da177e4 | 543 | |
05fccb0e | 544 | aper_base = aper_base_32 & 0x7fff; |
1da177e4 LT |
545 | aper_base <<= 25; |
546 | ||
05fccb0e IM |
547 | aper_size = (32 * 1024 * 1024) << aper_order; |
548 | if (aper_base + aper_size > 0x100000000UL || !aper_size) | |
1da177e4 LT |
549 | aper_base = 0; |
550 | ||
551 | *size = aper_size; | |
552 | return aper_base; | |
05fccb0e | 553 | } |
1da177e4 | 554 | |
6703f6d1 RW |
555 | static void enable_gart_translations(void) |
556 | { | |
557 | int i; | |
558 | ||
559 | for (i = 0; i < num_k8_northbridges; i++) { | |
560 | struct pci_dev *dev = k8_northbridges[i]; | |
561 | ||
562 | enable_gart_translation(dev, __pa(agp_gatt_table)); | |
563 | } | |
564 | } | |
565 | ||
566 | /* | |
567 | * If fix_up_north_bridges is set, the north bridges have to be fixed up on | |
568 | * resume in the same way as they are handled in gart_iommu_hole_init(). | |
569 | */ | |
570 | static bool fix_up_north_bridges; | |
571 | static u32 aperture_order; | |
572 | static u32 aperture_alloc; | |
573 | ||
574 | void set_up_gart_resume(u32 aper_order, u32 aper_alloc) | |
575 | { | |
576 | fix_up_north_bridges = true; | |
577 | aperture_order = aper_order; | |
578 | aperture_alloc = aper_alloc; | |
579 | } | |
580 | ||
cd76374e PM |
581 | static int gart_resume(struct sys_device *dev) |
582 | { | |
6703f6d1 RW |
583 | printk(KERN_INFO "PCI-DMA: Resuming GART IOMMU\n"); |
584 | ||
585 | if (fix_up_north_bridges) { | |
586 | int i; | |
587 | ||
588 | printk(KERN_INFO "PCI-DMA: Restoring GART aperture settings\n"); | |
589 | ||
590 | for (i = 0; i < num_k8_northbridges; i++) { | |
591 | struct pci_dev *dev = k8_northbridges[i]; | |
592 | ||
593 | /* | |
594 | * Don't enable translations just yet. That is the next | |
595 | * step. Restore the pre-suspend aperture settings. | |
596 | */ | |
597 | pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, | |
598 | aperture_order << 1); | |
599 | pci_write_config_dword(dev, AMD64_GARTAPERTUREBASE, | |
600 | aperture_alloc >> 25); | |
601 | } | |
602 | } | |
603 | ||
604 | enable_gart_translations(); | |
605 | ||
cd76374e PM |
606 | return 0; |
607 | } | |
608 | ||
609 | static int gart_suspend(struct sys_device *dev, pm_message_t state) | |
610 | { | |
6703f6d1 | 611 | return 0; |
cd76374e PM |
612 | } |
613 | ||
614 | static struct sysdev_class gart_sysdev_class = { | |
615 | .name = "gart", | |
616 | .suspend = gart_suspend, | |
617 | .resume = gart_resume, | |
618 | ||
619 | }; | |
620 | ||
621 | static struct sys_device device_gart = { | |
622 | .id = 0, | |
623 | .cls = &gart_sysdev_class, | |
624 | }; | |
625 | ||
05fccb0e | 626 | /* |
1da177e4 | 627 | * Private Northbridge GATT initialization in case we cannot use the |
05fccb0e | 628 | * AGP driver for some reason. |
1da177e4 LT |
629 | */ |
630 | static __init int init_k8_gatt(struct agp_kern_info *info) | |
05fccb0e IM |
631 | { |
632 | unsigned aper_size, gatt_size, new_aper_size; | |
633 | unsigned aper_base, new_aper_base; | |
1da177e4 LT |
634 | struct pci_dev *dev; |
635 | void *gatt; | |
cd76374e | 636 | int i, error; |
a32073bf | 637 | |
1da177e4 LT |
638 | printk(KERN_INFO "PCI-DMA: Disabling AGP.\n"); |
639 | aper_size = aper_base = info->aper_size = 0; | |
a32073bf AK |
640 | dev = NULL; |
641 | for (i = 0; i < num_k8_northbridges; i++) { | |
642 | dev = k8_northbridges[i]; | |
05fccb0e IM |
643 | new_aper_base = read_aperture(dev, &new_aper_size); |
644 | if (!new_aper_base) | |
645 | goto nommu; | |
646 | ||
647 | if (!aper_base) { | |
1da177e4 LT |
648 | aper_size = new_aper_size; |
649 | aper_base = new_aper_base; | |
05fccb0e IM |
650 | } |
651 | if (aper_size != new_aper_size || aper_base != new_aper_base) | |
1da177e4 LT |
652 | goto nommu; |
653 | } | |
654 | if (!aper_base) | |
05fccb0e | 655 | goto nommu; |
1da177e4 | 656 | info->aper_base = aper_base; |
05fccb0e | 657 | info->aper_size = aper_size >> 20; |
1da177e4 | 658 | |
05fccb0e | 659 | gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32); |
0114267b JR |
660 | gatt = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
661 | get_order(gatt_size)); | |
05fccb0e | 662 | if (!gatt) |
cf6387da | 663 | panic("Cannot allocate GATT table"); |
6d238cc4 | 664 | if (set_memory_uc((unsigned long)gatt, gatt_size >> PAGE_SHIFT)) |
cf6387da | 665 | panic("Could not set GART PTEs to uncacheable pages"); |
cf6387da | 666 | |
1da177e4 | 667 | agp_gatt_table = gatt; |
a32073bf | 668 | |
cd76374e PM |
669 | error = sysdev_class_register(&gart_sysdev_class); |
670 | if (!error) | |
671 | error = sysdev_register(&device_gart); | |
672 | if (error) | |
237a6224 JR |
673 | panic("Could not register gart_sysdev -- " |
674 | "would corrupt data on next suspend"); | |
6703f6d1 | 675 | |
a32073bf | 676 | flush_gart(); |
05fccb0e IM |
677 | |
678 | printk(KERN_INFO "PCI-DMA: aperture base @ %x size %u KB\n", | |
679 | aper_base, aper_size>>10); | |
7ab073b6 | 680 | |
1da177e4 LT |
681 | return 0; |
682 | ||
683 | nommu: | |
05fccb0e | 684 | /* Should not happen anymore */ |
8f59610d | 685 | printk(KERN_WARNING "PCI-DMA: More than 4GB of RAM and no IOMMU\n" |
ad361c98 | 686 | "falling back to iommu=soft.\n"); |
05fccb0e IM |
687 | return -1; |
688 | } | |
1da177e4 | 689 | |
160c1d8e | 690 | static struct dma_map_ops gart_dma_ops = { |
05fccb0e IM |
691 | .map_sg = gart_map_sg, |
692 | .unmap_sg = gart_unmap_sg, | |
052aedbf FT |
693 | .map_page = gart_map_page, |
694 | .unmap_page = gart_unmap_page, | |
94581094 | 695 | .alloc_coherent = gart_alloc_coherent, |
43a5a5a0 | 696 | .free_coherent = gart_free_coherent, |
42109197 | 697 | .mapping_error = gart_mapping_error, |
17a941d8 MBY |
698 | }; |
699 | ||
338bac52 | 700 | static void gart_iommu_shutdown(void) |
bc2cea6a YL |
701 | { |
702 | struct pci_dev *dev; | |
703 | int i; | |
704 | ||
338bac52 | 705 | if (no_agp) |
bc2cea6a YL |
706 | return; |
707 | ||
05fccb0e IM |
708 | for (i = 0; i < num_k8_northbridges; i++) { |
709 | u32 ctl; | |
bc2cea6a | 710 | |
05fccb0e | 711 | dev = k8_northbridges[i]; |
3bb6fbf9 | 712 | pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &ctl); |
bc2cea6a | 713 | |
3bb6fbf9 | 714 | ctl &= ~GARTEN; |
bc2cea6a | 715 | |
3bb6fbf9 | 716 | pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, ctl); |
05fccb0e | 717 | } |
bc2cea6a YL |
718 | } |
719 | ||
de957628 | 720 | int __init gart_iommu_init(void) |
05fccb0e | 721 | { |
1da177e4 | 722 | struct agp_kern_info info; |
1da177e4 | 723 | unsigned long iommu_start; |
d99e9016 YL |
724 | unsigned long aper_base, aper_size; |
725 | unsigned long start_pfn, end_pfn; | |
1da177e4 LT |
726 | unsigned long scratch; |
727 | long i; | |
728 | ||
55aab5f4 | 729 | if (cache_k8_northbridges() < 0 || num_k8_northbridges == 0) |
de957628 | 730 | return 0; |
a32073bf | 731 | |
1da177e4 | 732 | #ifndef CONFIG_AGP_AMD64 |
05fccb0e | 733 | no_agp = 1; |
1da177e4 LT |
734 | #else |
735 | /* Makefile puts PCI initialization via subsys_initcall first. */ | |
736 | /* Add other K8 AGP bridge drivers here */ | |
05fccb0e IM |
737 | no_agp = no_agp || |
738 | (agp_amd64_init() < 0) || | |
1da177e4 | 739 | (agp_copy_info(agp_bridge, &info) < 0); |
05fccb0e | 740 | #endif |
1da177e4 | 741 | |
1da177e4 | 742 | if (no_iommu || |
c987d12f | 743 | (!force_iommu && max_pfn <= MAX_DMA32_PFN) || |
0440d4c0 | 744 | !gart_iommu_aperture || |
1da177e4 | 745 | (no_agp && init_k8_gatt(&info) < 0)) { |
c987d12f | 746 | if (max_pfn > MAX_DMA32_PFN) { |
8f59610d | 747 | printk(KERN_WARNING "More than 4GB of memory " |
237a6224 JR |
748 | "but GART IOMMU not available.\n"); |
749 | printk(KERN_WARNING "falling back to iommu=soft.\n"); | |
5b7b644c | 750 | } |
de957628 | 751 | return 0; |
1da177e4 LT |
752 | } |
753 | ||
d99e9016 YL |
754 | /* need to map that range */ |
755 | aper_size = info.aper_size << 20; | |
756 | aper_base = info.aper_base; | |
757 | end_pfn = (aper_base>>PAGE_SHIFT) + (aper_size>>PAGE_SHIFT); | |
758 | if (end_pfn > max_low_pfn_mapped) { | |
759 | start_pfn = (aper_base>>PAGE_SHIFT); | |
760 | init_memory_mapping(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT); | |
761 | } | |
762 | ||
5b7b644c | 763 | printk(KERN_INFO "PCI-DMA: using GART IOMMU.\n"); |
05fccb0e IM |
764 | iommu_size = check_iommu_size(info.aper_base, aper_size); |
765 | iommu_pages = iommu_size >> PAGE_SHIFT; | |
766 | ||
0114267b | 767 | iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO, |
05fccb0e IM |
768 | get_order(iommu_pages/8)); |
769 | if (!iommu_gart_bitmap) | |
770 | panic("Cannot allocate iommu bitmap\n"); | |
1da177e4 LT |
771 | |
772 | #ifdef CONFIG_IOMMU_LEAK | |
05fccb0e | 773 | if (leak_trace) { |
19c1a6f5 FT |
774 | int ret; |
775 | ||
776 | ret = dma_debug_resize_entries(iommu_pages); | |
777 | if (ret) | |
05fccb0e | 778 | printk(KERN_DEBUG |
19c1a6f5 | 779 | "PCI-DMA: Cannot trace all the entries\n"); |
05fccb0e | 780 | } |
1da177e4 LT |
781 | #endif |
782 | ||
05fccb0e | 783 | /* |
1da177e4 | 784 | * Out of IOMMU space handling. |
05fccb0e IM |
785 | * Reserve some invalid pages at the beginning of the GART. |
786 | */ | |
d26dbc5c | 787 | iommu_area_reserve(iommu_gart_bitmap, 0, EMERGENCY_PAGES); |
1da177e4 | 788 | |
05fccb0e | 789 | agp_memory_reserved = iommu_size; |
1da177e4 LT |
790 | printk(KERN_INFO |
791 | "PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n", | |
05fccb0e | 792 | iommu_size >> 20); |
1da177e4 | 793 | |
05fccb0e IM |
794 | iommu_start = aper_size - iommu_size; |
795 | iommu_bus_base = info.aper_base + iommu_start; | |
42109197 | 796 | bad_dma_addr = iommu_bus_base; |
1da177e4 LT |
797 | iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT); |
798 | ||
05fccb0e | 799 | /* |
1da177e4 LT |
800 | * Unmap the IOMMU part of the GART. The alias of the page is |
801 | * always mapped with cache enabled and there is no full cache | |
802 | * coherency across the GART remapping. The unmapping avoids | |
803 | * automatic prefetches from the CPU allocating cache lines in | |
804 | * there. All CPU accesses are done via the direct mapping to | |
805 | * the backing memory. The GART address is only used by PCI | |
05fccb0e | 806 | * devices. |
1da177e4 | 807 | */ |
28d6ee41 AK |
808 | set_memory_np((unsigned long)__va(iommu_bus_base), |
809 | iommu_size >> PAGE_SHIFT); | |
184652eb IM |
810 | /* |
811 | * Tricky. The GART table remaps the physical memory range, | |
812 | * so the CPU wont notice potential aliases and if the memory | |
813 | * is remapped to UC later on, we might surprise the PCI devices | |
814 | * with a stray writeout of a cacheline. So play it sure and | |
815 | * do an explicit, full-scale wbinvd() _after_ having marked all | |
816 | * the pages as Not-Present: | |
817 | */ | |
818 | wbinvd(); | |
fe2245c9 ML |
819 | |
820 | /* | |
821 | * Now all caches are flushed and we can safely enable | |
822 | * GART hardware. Doing it early leaves the possibility | |
823 | * of stale cache entries that can lead to GART PTE | |
824 | * errors. | |
825 | */ | |
826 | enable_gart_translations(); | |
1da177e4 | 827 | |
05fccb0e | 828 | /* |
fa3d319a | 829 | * Try to workaround a bug (thanks to BenH): |
05fccb0e | 830 | * Set unmapped entries to a scratch page instead of 0. |
1da177e4 | 831 | * Any prefetches that hit unmapped entries won't get an bus abort |
fa3d319a | 832 | * then. (P2P bridge may be prefetching on DMA reads). |
1da177e4 | 833 | */ |
05fccb0e IM |
834 | scratch = get_zeroed_page(GFP_KERNEL); |
835 | if (!scratch) | |
1da177e4 LT |
836 | panic("Cannot allocate iommu scratch page"); |
837 | gart_unmapped_entry = GPTE_ENCODE(__pa(scratch)); | |
05fccb0e | 838 | for (i = EMERGENCY_PAGES; i < iommu_pages; i++) |
1da177e4 LT |
839 | iommu_gatt_base[i] = gart_unmapped_entry; |
840 | ||
a32073bf | 841 | flush_gart(); |
17a941d8 | 842 | dma_ops = &gart_dma_ops; |
338bac52 | 843 | x86_platform.iommu_shutdown = gart_iommu_shutdown; |
75f1cdf1 | 844 | swiotlb = 0; |
de957628 FT |
845 | |
846 | return 0; | |
05fccb0e | 847 | } |
1da177e4 | 848 | |
43999d9e | 849 | void __init gart_parse_options(char *p) |
17a941d8 MBY |
850 | { |
851 | int arg; | |
852 | ||
1da177e4 | 853 | #ifdef CONFIG_IOMMU_LEAK |
05fccb0e | 854 | if (!strncmp(p, "leak", 4)) { |
17a941d8 MBY |
855 | leak_trace = 1; |
856 | p += 4; | |
237a6224 JR |
857 | if (*p == '=') |
858 | ++p; | |
17a941d8 MBY |
859 | if (isdigit(*p) && get_option(&p, &arg)) |
860 | iommu_leak_pages = arg; | |
861 | } | |
1da177e4 | 862 | #endif |
17a941d8 MBY |
863 | if (isdigit(*p) && get_option(&p, &arg)) |
864 | iommu_size = arg; | |
41855b77 | 865 | if (!strncmp(p, "fullflush", 9)) |
17a941d8 | 866 | iommu_fullflush = 1; |
05fccb0e | 867 | if (!strncmp(p, "nofullflush", 11)) |
17a941d8 | 868 | iommu_fullflush = 0; |
05fccb0e | 869 | if (!strncmp(p, "noagp", 5)) |
17a941d8 | 870 | no_agp = 1; |
05fccb0e | 871 | if (!strncmp(p, "noaperture", 10)) |
17a941d8 MBY |
872 | fix_aperture = 0; |
873 | /* duplicated from pci-dma.c */ | |
05fccb0e | 874 | if (!strncmp(p, "force", 5)) |
0440d4c0 | 875 | gart_iommu_aperture_allowed = 1; |
05fccb0e | 876 | if (!strncmp(p, "allowed", 7)) |
0440d4c0 | 877 | gart_iommu_aperture_allowed = 1; |
17a941d8 MBY |
878 | if (!strncmp(p, "memaper", 7)) { |
879 | fallback_aper_force = 1; | |
880 | p += 7; | |
881 | if (*p == '=') { | |
882 | ++p; | |
883 | if (get_option(&p, &arg)) | |
884 | fallback_aper_order = arg; | |
885 | } | |
886 | } | |
887 | } |