14 #include <linux/types.h>
15 #include <linux/ctype.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
22 #include <linux/pci.h>
23 #include <linux/module.h>
26 #include <linux/bitmap.h>
35 #include <asm/pgtable.h>
37 #include <asm/iommu.h>
39 #include <asm/cacheflush.h>
40 #include <asm/swiotlb.h>
44 #include <asm/iommu_table.h>
46 static unsigned long iommu_bus_base;
47 static unsigned long iommu_size;
48 static unsigned long iommu_pages;
50 static u32 *iommu_gatt_base;
61 static int iommu_fullflush = 1;
66 static unsigned long *iommu_gart_bitmap;
68 static u32 gart_unmapped_entry;
71 #define GPTE_COHERENT 2
72 #define GPTE_ENCODE(x) \
73 (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
74 #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
76 #define EMERGENCY_PAGES 32
79 #define AGPEXTERN extern
85 #define GART_MAX_PHYS_ADDR (1ULL << 40)
91 static unsigned long next_bit;
92 static bool need_flush;
94 static unsigned long alloc_iommu(
struct device *
dev,
int size,
95 unsigned long align_mask)
98 unsigned long boundary_size;
99 unsigned long base_index;
101 base_index =
ALIGN(iommu_bus_base & dma_get_seg_boundary(dev),
103 boundary_size =
ALIGN((
u64)dma_get_seg_boundary(dev) + 1,
108 size, base_index, boundary_size, align_mask);
112 size, base_index, boundary_size,
116 next_bit = offset+
size;
117 if (next_bit >= iommu_pages) {
124 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
129 static void free_iommu(
unsigned long offset,
int size)
135 if (offset >= next_bit)
136 next_bit = offset +
size;
137 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
143 static void flush_gart(
void)
152 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
155 #ifdef CONFIG_IOMMU_LEAK
157 static int leak_trace;
158 static int iommu_leak_pages = 20;
160 static void dump_leak(
void)
173 static void iommu_full(
struct device *
dev,
size_t size,
int dir)
185 dev_err(dev,
"PCI-DMA: Out of IOMMU space for %lu bytes\n", size);
189 panic(
"PCI-DMA: Memory would be corrupted\n");
192 "PCI-DMA: Random memory would be DMAed\n");
194 #ifdef CONFIG_IOMMU_LEAK
200 need_iommu(
struct device *dev,
unsigned long addr,
size_t size)
206 nonforced_iommu(
struct device *dev,
unsigned long addr,
size_t size)
215 size_t size,
int dir,
unsigned long align_mask)
217 unsigned long npages = iommu_num_pages(phys_mem, size,
PAGE_SIZE);
218 unsigned long iommu_page;
224 iommu_page = alloc_iommu(dev, npages, align_mask);
225 if (iommu_page == -1) {
226 if (!nonforced_iommu(dev, phys_mem, size))
228 if (panic_on_overflow)
229 panic(
"dma_map_area overflow %lu bytes\n", size);
230 iommu_full(dev, size, dir);
234 for (i = 0; i < npages; i++) {
235 iommu_gatt_base[iommu_page +
i] =
GPTE_ENCODE(phys_mem);
243 unsigned long offset,
size_t size,
253 if (!need_iommu(dev, paddr, size))
256 bus = dma_map_area(dev, paddr, size, dir, 0);
269 unsigned long iommu_page;
273 if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*
PAGE_SIZE ||
274 dma_addr >= iommu_bus_base + iommu_size)
277 iommu_page = (dma_addr - iommu_bus_base)>>
PAGE_SHIFT;
278 npages = iommu_num_pages(dma_addr, size,
PAGE_SIZE);
279 for (i = 0; i < npages; i++) {
280 iommu_gatt_base[iommu_page +
i] = gart_unmapped_entry;
282 free_iommu(iommu_page, npages);
295 if (!s->dma_length || !s->
length)
308 #ifdef CONFIG_IOMMU_DEBUG
315 if (nonforced_iommu(dev, addr, s->
length)) {
316 addr = dma_map_area(dev, addr, s->
length, dir, 0);
317 if (addr == bad_dma_addr) {
319 gart_unmap_sg(dev, sg, i, dir,
NULL);
321 sg[0].dma_length = 0;
326 s->dma_length = s->
length;
338 unsigned long iommu_start = alloc_iommu(dev, pages, 0);
339 unsigned long iommu_page = iommu_start;
343 if (iommu_start == -1)
354 sout->dma_length = s->
length;
356 sout->dma_length += s->
length;
367 BUG_ON(iommu_page - iommu_start != pages);
374 struct scatterlist *sout,
unsigned long pages,
int need)
379 sout->dma_length = start->
length;
382 return __dma_map_cont(dev, start, nelems, sout, pages);
394 unsigned long pages = 0;
395 unsigned int seg_size;
396 unsigned int max_seg_size;
409 max_seg_size = dma_get_max_seg_size(dev);
418 nextneed = need_iommu(dev, addr, s->
length);
427 if (!iommu_merge || !nextneed || !need || s->
offset ||
428 (s->
length + seg_size > max_seg_size) ||
430 if (dma_map_cont(dev, start_sg, i - start,
431 sgmap, pages, need) < 0)
448 if (dma_map_cont(dev, start_sg, i - start, sgmap, pages, need) < 0)
454 sgmap->dma_length = 0;
460 gart_unmap_sg(dev, sg, out, dir,
NULL);
463 if (force_iommu || iommu_merge) {
464 out = dma_map_sg_nonforce(dev, sg, nents, dir);
468 if (panic_on_overflow)
469 panic(
"dma_map_sg: overflow on %lu pages\n", pages);
483 unsigned long align_mask;
486 if (force_iommu && !(flag &
GFP_DMA)) {
497 if (paddr != bad_dma_addr) {
511 gart_free_coherent(
struct device *dev,
size_t size,
void *
vaddr,
520 return (dma_addr == bad_dma_addr);
525 static __init unsigned long check_iommu_size(
unsigned long aper,
u64 aper_size)
530 iommu_size = aper_size;
535 a = aper + iommu_size;
538 if (iommu_size < 64*1024*1024) {
540 "PCI-DMA: Warning: Small IOMMU %luMB."
541 " Consider increasing the AGP aperture in BIOS\n",
550 unsigned aper_size = 0, aper_base_32, aper_order;
555 aper_order = (aper_order >> 1) & 7;
557 aper_base = aper_base_32 & 0x7fff;
560 aper_size = (32 * 1024 * 1024) << aper_order;
561 if (aper_base + aper_size > 0x100000000UL || !aper_size)
568 static void enable_gart_translations(
void)
589 static bool fix_up_north_bridges;
590 static u32 aperture_order;
591 static u32 aperture_alloc;
595 fix_up_north_bridges =
true;
596 aperture_order = aper_order;
597 aperture_alloc = aper_alloc;
600 static void gart_fixup_northbridges(
void)
604 if (!fix_up_north_bridges)
610 pr_info(
"PCI-DMA: Restoring GART aperture settings\n");
619 gart_set_size_and_enable(dev, aperture_order);
624 static void gart_resume(
void)
626 pr_info(
"PCI-DMA: Resuming GART IOMMU\n");
628 gart_fixup_northbridges();
630 enable_gart_translations();
634 .resume = gart_resume,
644 unsigned aper_size, gatt_size, new_aper_size;
645 unsigned aper_base, new_aper_base;
650 pr_info(
"PCI-DMA: Disabling AGP.\n");
652 aper_size = aper_base = info->
aper_size = 0;
656 new_aper_base = read_aperture(dev, &new_aper_size);
661 aper_size = new_aper_size;
662 aper_base = new_aper_base;
664 if (aper_size != new_aper_size || aper_base != new_aper_base)
677 panic(
"Cannot allocate GATT table");
679 panic(
"Could not set GART PTEs to uncacheable pages");
687 pr_info(
"PCI-DMA: aperture base @ %x size %u KB\n",
688 aper_base, aper_size>>10);
694 pr_warning(
"PCI-DMA: More than 4GB of RAM and no IOMMU\n"
695 "falling back to iommu=soft.\n");
700 .map_sg = gart_map_sg,
701 .unmap_sg = gart_unmap_sg,
702 .map_page = gart_map_page,
703 .unmap_page = gart_unmap_page,
704 .alloc = gart_alloc_coherent,
705 .free = gart_free_coherent,
706 .mapping_error = gart_mapping_error,
709 static void gart_iommu_shutdown(
void)
736 unsigned long iommu_start;
738 unsigned long start_pfn, end_pfn;
745 #ifndef CONFIG_AGP_AMD64
758 (no_agp && init_amd_gatt(&info) < 0)) {
760 pr_warning(
"More than 4GB of memory but GART IOMMU not available.\n");
776 pr_info(
"PCI-DMA: using GART IOMMU.\n");
777 iommu_size = check_iommu_size(info.
aper_base, aper_size);
782 if (!iommu_gart_bitmap)
783 panic(
"Cannot allocate iommu bitmap\n");
785 #ifdef CONFIG_IOMMU_LEAK
791 pr_debug(
"PCI-DMA: Cannot trace all the entries\n");
799 bitmap_set(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
801 pr_info(
"PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
805 iommu_start = aper_size - iommu_size;
806 iommu_bus_base = info.
aper_base + iommu_start;
807 bad_dma_addr = iommu_bus_base;
820 iommu_size >> PAGE_SHIFT);
837 enable_gart_translations();
847 panic(
"Cannot allocate iommu scratch page");
849 for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
850 iommu_gatt_base[i] = gart_unmapped_entry;
864 #ifdef CONFIG_IOMMU_LEAK
871 iommu_leak_pages =
arg;
876 if (!
strncmp(p,
"fullflush", 9))
878 if (!
strncmp(p,
"nofullflush", 11))
882 if (!
strncmp(p,
"noaperture", 10))
889 if (!
strncmp(p,
"memaper", 7)) {