diff options
Diffstat (limited to 'arch/x86_64/kernel/pci-gart.c')
-rw-r--r-- | arch/x86_64/kernel/pci-gart.c | 155 |
1 files changed, 54 insertions, 101 deletions
diff --git a/arch/x86_64/kernel/pci-gart.c b/arch/x86_64/kernel/pci-gart.c index 82a7c9bfdfa0..4ca674d16b09 100644 --- a/arch/x86_64/kernel/pci-gart.c +++ b/arch/x86_64/kernel/pci-gart.c | |||
@@ -32,6 +32,7 @@ | |||
32 | #include <asm/kdebug.h> | 32 | #include <asm/kdebug.h> |
33 | #include <asm/swiotlb.h> | 33 | #include <asm/swiotlb.h> |
34 | #include <asm/dma.h> | 34 | #include <asm/dma.h> |
35 | #include <asm/k8.h> | ||
35 | 36 | ||
36 | unsigned long iommu_bus_base; /* GART remapping area (physical) */ | 37 | unsigned long iommu_bus_base; /* GART remapping area (physical) */ |
37 | static unsigned long iommu_size; /* size of remapping area bytes */ | 38 | static unsigned long iommu_size; /* size of remapping area bytes */ |
@@ -46,8 +47,6 @@ u32 *iommu_gatt_base; /* Remapping table */ | |||
46 | also seen with Qlogic at least). */ | 47 | also seen with Qlogic at least). */ |
47 | int iommu_fullflush = 1; | 48 | int iommu_fullflush = 1; |
48 | 49 | ||
49 | #define MAX_NB 8 | ||
50 | |||
51 | /* Allocation bitmap for the remapping area */ | 50 | /* Allocation bitmap for the remapping area */ |
52 | static DEFINE_SPINLOCK(iommu_bitmap_lock); | 51 | static DEFINE_SPINLOCK(iommu_bitmap_lock); |
53 | static unsigned long *iommu_gart_bitmap; /* guarded by iommu_bitmap_lock */ | 52 | static unsigned long *iommu_gart_bitmap; /* guarded by iommu_bitmap_lock */ |
@@ -63,13 +62,6 @@ static u32 gart_unmapped_entry; | |||
63 | #define to_pages(addr,size) \ | 62 | #define to_pages(addr,size) \ |
64 | (round_up(((addr) & ~PAGE_MASK) + (size), PAGE_SIZE) >> PAGE_SHIFT) | 63 | (round_up(((addr) & ~PAGE_MASK) + (size), PAGE_SIZE) >> PAGE_SHIFT) |
65 | 64 | ||
66 | #define for_all_nb(dev) \ | ||
67 | dev = NULL; \ | ||
68 | while ((dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1103, dev))!=NULL) | ||
69 | |||
70 | static struct pci_dev *northbridges[MAX_NB]; | ||
71 | static u32 northbridge_flush_word[MAX_NB]; | ||
72 | |||
73 | #define EMERGENCY_PAGES 32 /* = 128KB */ | 65 | #define EMERGENCY_PAGES 32 /* = 128KB */ |
74 | 66 | ||
75 | #ifdef CONFIG_AGP | 67 | #ifdef CONFIG_AGP |
@@ -93,7 +85,7 @@ static unsigned long alloc_iommu(int size) | |||
93 | offset = find_next_zero_string(iommu_gart_bitmap,next_bit,iommu_pages,size); | 85 | offset = find_next_zero_string(iommu_gart_bitmap,next_bit,iommu_pages,size); |
94 | if (offset == -1) { | 86 | if (offset == -1) { |
95 | need_flush = 1; | 87 | need_flush = 1; |
96 | offset = find_next_zero_string(iommu_gart_bitmap,0,next_bit,size); | 88 | offset = find_next_zero_string(iommu_gart_bitmap,0,iommu_pages,size); |
97 | } | 89 | } |
98 | if (offset != -1) { | 90 | if (offset != -1) { |
99 | set_bit_string(iommu_gart_bitmap, offset, size); | 91 | set_bit_string(iommu_gart_bitmap, offset, size); |
@@ -120,44 +112,17 @@ static void free_iommu(unsigned long offset, int size) | |||
120 | /* | 112 | /* |
121 | * Use global flush state to avoid races with multiple flushers. | 113 | * Use global flush state to avoid races with multiple flushers. |
122 | */ | 114 | */ |
123 | static void flush_gart(struct device *dev) | 115 | static void flush_gart(void) |
124 | { | 116 | { |
125 | unsigned long flags; | 117 | unsigned long flags; |
126 | int flushed = 0; | ||
127 | int i, max; | ||
128 | |||
129 | spin_lock_irqsave(&iommu_bitmap_lock, flags); | 118 | spin_lock_irqsave(&iommu_bitmap_lock, flags); |
130 | if (need_flush) { | 119 | if (need_flush) { |
131 | max = 0; | 120 | k8_flush_garts(); |
132 | for (i = 0; i < MAX_NB; i++) { | ||
133 | if (!northbridges[i]) | ||
134 | continue; | ||
135 | pci_write_config_dword(northbridges[i], 0x9c, | ||
136 | northbridge_flush_word[i] | 1); | ||
137 | flushed++; | ||
138 | max = i; | ||
139 | } | ||
140 | for (i = 0; i <= max; i++) { | ||
141 | u32 w; | ||
142 | if (!northbridges[i]) | ||
143 | continue; | ||
144 | /* Make sure the hardware actually executed the flush. */ | ||
145 | for (;;) { | ||
146 | pci_read_config_dword(northbridges[i], 0x9c, &w); | ||
147 | if (!(w & 1)) | ||
148 | break; | ||
149 | cpu_relax(); | ||
150 | } | ||
151 | } | ||
152 | if (!flushed) | ||
153 | printk("nothing to flush?\n"); | ||
154 | need_flush = 0; | 121 | need_flush = 0; |
155 | } | 122 | } |
156 | spin_unlock_irqrestore(&iommu_bitmap_lock, flags); | 123 | spin_unlock_irqrestore(&iommu_bitmap_lock, flags); |
157 | } | 124 | } |
158 | 125 | ||
159 | |||
160 | |||
161 | #ifdef CONFIG_IOMMU_LEAK | 126 | #ifdef CONFIG_IOMMU_LEAK |
162 | 127 | ||
163 | #define SET_LEAK(x) if (iommu_leak_tab) \ | 128 | #define SET_LEAK(x) if (iommu_leak_tab) \ |
@@ -266,7 +231,7 @@ static dma_addr_t gart_map_simple(struct device *dev, char *buf, | |||
266 | size_t size, int dir) | 231 | size_t size, int dir) |
267 | { | 232 | { |
268 | dma_addr_t map = dma_map_area(dev, virt_to_bus(buf), size, dir); | 233 | dma_addr_t map = dma_map_area(dev, virt_to_bus(buf), size, dir); |
269 | flush_gart(dev); | 234 | flush_gart(); |
270 | return map; | 235 | return map; |
271 | } | 236 | } |
272 | 237 | ||
@@ -289,6 +254,28 @@ dma_addr_t gart_map_single(struct device *dev, void *addr, size_t size, int dir) | |||
289 | } | 254 | } |
290 | 255 | ||
291 | /* | 256 | /* |
257 | * Free a DMA mapping. | ||
258 | */ | ||
259 | void gart_unmap_single(struct device *dev, dma_addr_t dma_addr, | ||
260 | size_t size, int direction) | ||
261 | { | ||
262 | unsigned long iommu_page; | ||
263 | int npages; | ||
264 | int i; | ||
265 | |||
266 | if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE || | ||
267 | dma_addr >= iommu_bus_base + iommu_size) | ||
268 | return; | ||
269 | iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT; | ||
270 | npages = to_pages(dma_addr, size); | ||
271 | for (i = 0; i < npages; i++) { | ||
272 | iommu_gatt_base[iommu_page + i] = gart_unmapped_entry; | ||
273 | CLEAR_LEAK(iommu_page + i); | ||
274 | } | ||
275 | free_iommu(iommu_page, npages); | ||
276 | } | ||
277 | |||
278 | /* | ||
292 | * Wrapper for pci_unmap_single working with scatterlists. | 279 | * Wrapper for pci_unmap_single working with scatterlists. |
293 | */ | 280 | */ |
294 | void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir) | 281 | void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir) |
@@ -299,7 +286,7 @@ void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int di | |||
299 | struct scatterlist *s = &sg[i]; | 286 | struct scatterlist *s = &sg[i]; |
300 | if (!s->dma_length || !s->length) | 287 | if (!s->dma_length || !s->length) |
301 | break; | 288 | break; |
302 | dma_unmap_single(dev, s->dma_address, s->dma_length, dir); | 289 | gart_unmap_single(dev, s->dma_address, s->dma_length, dir); |
303 | } | 290 | } |
304 | } | 291 | } |
305 | 292 | ||
@@ -329,7 +316,7 @@ static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg, | |||
329 | s->dma_address = addr; | 316 | s->dma_address = addr; |
330 | s->dma_length = s->length; | 317 | s->dma_length = s->length; |
331 | } | 318 | } |
332 | flush_gart(dev); | 319 | flush_gart(); |
333 | return nents; | 320 | return nents; |
334 | } | 321 | } |
335 | 322 | ||
@@ -436,13 +423,13 @@ int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, int dir) | |||
436 | if (dma_map_cont(sg, start, i, sg+out, pages, need) < 0) | 423 | if (dma_map_cont(sg, start, i, sg+out, pages, need) < 0) |
437 | goto error; | 424 | goto error; |
438 | out++; | 425 | out++; |
439 | flush_gart(dev); | 426 | flush_gart(); |
440 | if (out < nents) | 427 | if (out < nents) |
441 | sg[out].dma_length = 0; | 428 | sg[out].dma_length = 0; |
442 | return out; | 429 | return out; |
443 | 430 | ||
444 | error: | 431 | error: |
445 | flush_gart(NULL); | 432 | flush_gart(); |
446 | gart_unmap_sg(dev, sg, nents, dir); | 433 | gart_unmap_sg(dev, sg, nents, dir); |
447 | /* When it was forced or merged try again in a dumb way */ | 434 | /* When it was forced or merged try again in a dumb way */ |
448 | if (force_iommu || iommu_merge) { | 435 | if (force_iommu || iommu_merge) { |
@@ -458,28 +445,6 @@ error: | |||
458 | return 0; | 445 | return 0; |
459 | } | 446 | } |
460 | 447 | ||
461 | /* | ||
462 | * Free a DMA mapping. | ||
463 | */ | ||
464 | void gart_unmap_single(struct device *dev, dma_addr_t dma_addr, | ||
465 | size_t size, int direction) | ||
466 | { | ||
467 | unsigned long iommu_page; | ||
468 | int npages; | ||
469 | int i; | ||
470 | |||
471 | if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE || | ||
472 | dma_addr >= iommu_bus_base + iommu_size) | ||
473 | return; | ||
474 | iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT; | ||
475 | npages = to_pages(dma_addr, size); | ||
476 | for (i = 0; i < npages; i++) { | ||
477 | iommu_gatt_base[iommu_page + i] = gart_unmapped_entry; | ||
478 | CLEAR_LEAK(iommu_page + i); | ||
479 | } | ||
480 | free_iommu(iommu_page, npages); | ||
481 | } | ||
482 | |||
483 | static int no_agp; | 448 | static int no_agp; |
484 | 449 | ||
485 | static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size) | 450 | static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size) |
@@ -532,10 +497,13 @@ static __init int init_k8_gatt(struct agp_kern_info *info) | |||
532 | void *gatt; | 497 | void *gatt; |
533 | unsigned aper_base, new_aper_base; | 498 | unsigned aper_base, new_aper_base; |
534 | unsigned aper_size, gatt_size, new_aper_size; | 499 | unsigned aper_size, gatt_size, new_aper_size; |
535 | 500 | int i; | |
501 | |||
536 | printk(KERN_INFO "PCI-DMA: Disabling AGP.\n"); | 502 | printk(KERN_INFO "PCI-DMA: Disabling AGP.\n"); |
537 | aper_size = aper_base = info->aper_size = 0; | 503 | aper_size = aper_base = info->aper_size = 0; |
538 | for_all_nb(dev) { | 504 | dev = NULL; |
505 | for (i = 0; i < num_k8_northbridges; i++) { | ||
506 | dev = k8_northbridges[i]; | ||
539 | new_aper_base = read_aperture(dev, &new_aper_size); | 507 | new_aper_base = read_aperture(dev, &new_aper_size); |
540 | if (!new_aper_base) | 508 | if (!new_aper_base) |
541 | goto nommu; | 509 | goto nommu; |
@@ -558,11 +526,12 @@ static __init int init_k8_gatt(struct agp_kern_info *info) | |||
558 | panic("Cannot allocate GATT table"); | 526 | panic("Cannot allocate GATT table"); |
559 | memset(gatt, 0, gatt_size); | 527 | memset(gatt, 0, gatt_size); |
560 | agp_gatt_table = gatt; | 528 | agp_gatt_table = gatt; |
561 | 529 | ||
562 | for_all_nb(dev) { | 530 | for (i = 0; i < num_k8_northbridges; i++) { |
563 | u32 ctl; | 531 | u32 ctl; |
564 | u32 gatt_reg; | 532 | u32 gatt_reg; |
565 | 533 | ||
534 | dev = k8_northbridges[i]; | ||
566 | gatt_reg = __pa(gatt) >> 12; | 535 | gatt_reg = __pa(gatt) >> 12; |
567 | gatt_reg <<= 4; | 536 | gatt_reg <<= 4; |
568 | pci_write_config_dword(dev, 0x98, gatt_reg); | 537 | pci_write_config_dword(dev, 0x98, gatt_reg); |
@@ -573,7 +542,7 @@ static __init int init_k8_gatt(struct agp_kern_info *info) | |||
573 | 542 | ||
574 | pci_write_config_dword(dev, 0x90, ctl); | 543 | pci_write_config_dword(dev, 0x90, ctl); |
575 | } | 544 | } |
576 | flush_gart(NULL); | 545 | flush_gart(); |
577 | 546 | ||
578 | printk("PCI-DMA: aperture base @ %x size %u KB\n",aper_base, aper_size>>10); | 547 | printk("PCI-DMA: aperture base @ %x size %u KB\n",aper_base, aper_size>>10); |
579 | return 0; | 548 | return 0; |
@@ -602,15 +571,19 @@ static struct dma_mapping_ops gart_dma_ops = { | |||
602 | .unmap_sg = gart_unmap_sg, | 571 | .unmap_sg = gart_unmap_sg, |
603 | }; | 572 | }; |
604 | 573 | ||
605 | static int __init pci_iommu_init(void) | 574 | void __init gart_iommu_init(void) |
606 | { | 575 | { |
607 | struct agp_kern_info info; | 576 | struct agp_kern_info info; |
608 | unsigned long aper_size; | 577 | unsigned long aper_size; |
609 | unsigned long iommu_start; | 578 | unsigned long iommu_start; |
610 | struct pci_dev *dev; | ||
611 | unsigned long scratch; | 579 | unsigned long scratch; |
612 | long i; | 580 | long i; |
613 | 581 | ||
582 | if (cache_k8_northbridges() < 0 || num_k8_northbridges == 0) { | ||
583 | printk(KERN_INFO "PCI-GART: No AMD northbridge found.\n"); | ||
584 | return; | ||
585 | } | ||
586 | |||
614 | #ifndef CONFIG_AGP_AMD64 | 587 | #ifndef CONFIG_AGP_AMD64 |
615 | no_agp = 1; | 588 | no_agp = 1; |
616 | #else | 589 | #else |
@@ -622,7 +595,11 @@ static int __init pci_iommu_init(void) | |||
622 | #endif | 595 | #endif |
623 | 596 | ||
624 | if (swiotlb) | 597 | if (swiotlb) |
625 | return -1; | 598 | return; |
599 | |||
600 | /* Did we detect a different HW IOMMU? */ | ||
601 | if (iommu_detected && !iommu_aperture) | ||
602 | return; | ||
626 | 603 | ||
627 | if (no_iommu || | 604 | if (no_iommu || |
628 | (!force_iommu && end_pfn <= MAX_DMA32_PFN) || | 605 | (!force_iommu && end_pfn <= MAX_DMA32_PFN) || |
@@ -634,15 +611,7 @@ static int __init pci_iommu_init(void) | |||
634 | "but IOMMU not available.\n" | 611 | "but IOMMU not available.\n" |
635 | KERN_ERR "WARNING 32bit PCI may malfunction.\n"); | 612 | KERN_ERR "WARNING 32bit PCI may malfunction.\n"); |
636 | } | 613 | } |
637 | return -1; | 614 | return; |
638 | } | ||
639 | |||
640 | i = 0; | ||
641 | for_all_nb(dev) | ||
642 | i++; | ||
643 | if (i > MAX_NB) { | ||
644 | printk(KERN_ERR "PCI-GART: Too many northbridges (%ld). Disabled\n", i); | ||
645 | return -1; | ||
646 | } | 615 | } |
647 | 616 | ||
648 | printk(KERN_INFO "PCI-DMA: using GART IOMMU.\n"); | 617 | printk(KERN_INFO "PCI-DMA: using GART IOMMU.\n"); |
@@ -707,26 +676,10 @@ static int __init pci_iommu_init(void) | |||
707 | for (i = EMERGENCY_PAGES; i < iommu_pages; i++) | 676 | for (i = EMERGENCY_PAGES; i < iommu_pages; i++) |
708 | iommu_gatt_base[i] = gart_unmapped_entry; | 677 | iommu_gatt_base[i] = gart_unmapped_entry; |
709 | 678 | ||
710 | for_all_nb(dev) { | 679 | flush_gart(); |
711 | u32 flag; | ||
712 | int cpu = PCI_SLOT(dev->devfn) - 24; | ||
713 | if (cpu >= MAX_NB) | ||
714 | continue; | ||
715 | northbridges[cpu] = dev; | ||
716 | pci_read_config_dword(dev, 0x9c, &flag); /* cache flush word */ | ||
717 | northbridge_flush_word[cpu] = flag; | ||
718 | } | ||
719 | |||
720 | flush_gart(NULL); | ||
721 | |||
722 | dma_ops = &gart_dma_ops; | 680 | dma_ops = &gart_dma_ops; |
723 | |||
724 | return 0; | ||
725 | } | 681 | } |
726 | 682 | ||
727 | /* Must execute after PCI subsystem */ | ||
728 | fs_initcall(pci_iommu_init); | ||
729 | |||
730 | void gart_parse_options(char *p) | 683 | void gart_parse_options(char *p) |
731 | { | 684 | { |
732 | int arg; | 685 | int arg; |