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