blob: 4d8efb05428db3650983a67c2f00e4f4207fb325 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dynamic DMA mapping support for AMD Hammer.
Ingo Molnar05fccb02008-01-30 13:30:12 +01003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Ingo Molnar05fccb02008-01-30 13:30:12 +01006 * with more than 4GB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * See Documentation/DMA-mapping.txt for the interface specification.
Ingo Molnar05fccb02008-01-30 13:30:12 +01009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright 2002 Andi Kleen, SuSE Labs.
Andi Kleenff7f3642007-10-17 18:04:37 +020011 * Subject to the GNU General Public License v2 only.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070026#include <linux/kdebug.h>
Jens Axboe9ee1bea2007-10-04 09:35:37 +020027#include <linux/scatterlist.h>
FUJITA Tomonorifde9a102008-02-04 22:28:11 -080028#include <linux/iommu-helper.h>
Pavel Machekcd763742008-05-29 00:30:21 -070029#include <linux/sysdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/atomic.h>
31#include <asm/io.h>
32#include <asm/mtrr.h>
33#include <asm/pgtable.h>
34#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090035#include <asm/iommu.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020036#include <asm/gart.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/cacheflush.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010038#include <asm/swiotlb.h>
39#include <asm/dma.h>
Andi Kleena32073b2006-06-26 13:56:40 +020040#include <asm/k8.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Joerg Roedel79da0872007-10-24 12:49:49 +020042static unsigned long iommu_bus_base; /* GART remapping area (physical) */
Ingo Molnar05fccb02008-01-30 13:30:12 +010043static unsigned long iommu_size; /* size of remapping area bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static unsigned long iommu_pages; /* .. and in pages */
45
Ingo Molnar05fccb02008-01-30 13:30:12 +010046static u32 *iommu_gatt_base; /* Remapping table */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Ingo Molnar05fccb02008-01-30 13:30:12 +010048/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055int iommu_fullflush = 1;
56
Ingo Molnar05fccb02008-01-30 13:30:12 +010057/* Allocation bitmap for the remapping area: */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static DEFINE_SPINLOCK(iommu_bitmap_lock);
Ingo Molnar05fccb02008-01-30 13:30:12 +010059/* Guarded by iommu_bitmap_lock: */
60static unsigned long *iommu_gart_bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Ingo Molnar05fccb02008-01-30 13:30:12 +010062static u32 gart_unmapped_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
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
Ingo Molnar05fccb02008-01-30 13:30:12 +010070#define EMERGENCY_PAGES 32 /* = 128KB */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
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 */
Ingo Molnar05fccb02008-01-30 13:30:12 +010083static int need_flush; /* global flush state. set for each gart wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +090085static unsigned long alloc_iommu(struct device *dev, int size,
86 unsigned long align_mask)
Ingo Molnar05fccb02008-01-30 13:30:12 +010087{
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned long offset, flags;
FUJITA Tomonorifde9a102008-02-04 22:28:11 -080089 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;
Prarit Bhargava05d3ed02008-07-21 10:15:22 -040094 boundary_size = ALIGN((unsigned long long)dma_get_seg_boundary(dev) + 1,
FUJITA Tomonorifde9a102008-02-04 22:28:11 -080095 PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Ingo Molnar05fccb02008-01-30 13:30:12 +010097 spin_lock_irqsave(&iommu_bitmap_lock, flags);
FUJITA Tomonorifde9a102008-02-04 22:28:11 -080098 offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, next_bit,
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +090099 size, base_index, boundary_size, align_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (offset == -1) {
101 need_flush = 1;
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800102 offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, 0,
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900103 size, base_index, boundary_size,
104 align_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Ingo Molnar05fccb02008-01-30 13:30:12 +0100106 if (offset != -1) {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100107 next_bit = offset+size;
108 if (next_bit >= iommu_pages) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 next_bit = 0;
110 need_flush = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100111 }
112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (iommu_fullflush)
114 need_flush = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100115 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return offset;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120static void free_iommu(unsigned long offset, int size)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100121{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned long flags;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 spin_lock_irqsave(&iommu_bitmap_lock, flags);
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800125 iommu_area_free(iommu_gart_bitmap, offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Ingo Molnar05fccb02008-01-30 13:30:12 +0100129/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * Use global flush state to avoid races with multiple flushers.
131 */
Andi Kleena32073b2006-06-26 13:56:40 +0200132static void flush_gart(void)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned long flags;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 spin_lock_irqsave(&iommu_bitmap_lock, flags);
Andi Kleena32073b2006-06-26 13:56:40 +0200137 if (need_flush) {
138 k8_flush_garts();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 need_flush = 0;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#ifdef CONFIG_IOMMU_LEAK
145
Ingo Molnar05fccb02008-01-30 13:30:12 +0100146#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/* Debugging aid for drivers that don't free their IOMMU tables */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100159static void **iommu_leak_tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static int leak_trace;
Joerg Roedel79da0872007-10-24 12:49:49 +0200161static int iommu_leak_pages = 20;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100162
Joerg Roedel79da0872007-10-24 12:49:49 +0200163static void dump_leak(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int i;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100166 static int dump;
167
168 if (dump || !iommu_leak_tab)
169 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 dump = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100171 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);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100178 printk_address((unsigned long) iommu_leak_tab[iommu_pages-i], 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100179 printk(KERN_CONT "%c", (i+1)%2 == 0 ? '\n' : ' ');
180 }
181 printk(KERN_DEBUG "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183#else
Ingo Molnar05fccb02008-01-30 13:30:12 +0100184# define SET_LEAK(x)
185# define CLEAR_LEAK(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#endif
187
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100188static void iommu_full(struct device *dev, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Ingo Molnar05fccb02008-01-30 13:30:12 +0100190 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Ran out of IOMMU space for this operation. This is very bad.
192 * Unfortunately the drivers cannot handle this operation properly.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100193 * Return some non mapped prereserved space in the aperture and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * let the Northbridge deal with it. This will result in garbage
195 * in the IO operation. When the size exceeds the prereserved space
Ingo Molnar05fccb02008-01-30 13:30:12 +0100196 * memory corruption will occur or random memory will be DMAed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * out. Hopefully no network devices use single mappings that big.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100198 */
199
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200200 dev_err(dev, "PCI-DMA: Out of IOMMU space for %lu bytes\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100202 if (size > PAGE_SIZE*EMERGENCY_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
204 panic("PCI-DMA: Memory would be corrupted\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100205 if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
206 panic(KERN_ERR
207 "PCI-DMA: Random memory would be DMAed\n");
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#ifdef CONFIG_IOMMU_LEAK
Ingo Molnar05fccb02008-01-30 13:30:12 +0100210 dump_leak();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Ingo Molnar05fccb02008-01-30 13:30:12 +0100214static inline int
215need_iommu(struct device *dev, unsigned long addr, size_t size)
216{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 u64 mask = *dev->dma_mask;
Andi Kleen00edefa2007-02-13 13:26:24 +0100218 int high = addr + size > mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int mmu = high;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100220
221 if (force_iommu)
222 mmu = 1;
223
224 return mmu;
225}
226
227static inline int
228nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
229{
230 u64 mask = *dev->dma_mask;
231 int high = addr + size > mask;
232 int mmu = high;
233
234 return mmu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/* Map a single continuous physical area into the IOMMU.
238 * Caller needs to check if the iommu is needed and flush.
239 */
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100240static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900241 size_t size, int dir, unsigned long align_mask)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100242{
Joerg Roedel87e39ea2008-07-25 14:58:00 +0200243 unsigned long npages = iommu_num_pages(phys_mem, size);
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900244 unsigned long iommu_page = alloc_iommu(dev, npages, align_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int i;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (iommu_page == -1) {
248 if (!nonforced_iommu(dev, phys_mem, size))
Ingo Molnar05fccb02008-01-30 13:30:12 +0100249 return phys_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (panic_on_overflow)
251 panic("dma_map_area overflow %lu bytes\n", size);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100252 iommu_full(dev, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 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
Ingo Molnar05fccb02008-01-30 13:30:12 +0100264static dma_addr_t
Ingo Molnar2be62142008-04-19 19:19:56 +0200265gart_map_simple(struct device *dev, phys_addr_t paddr, size_t size, int dir)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100266{
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900267 dma_addr_t map;
268 unsigned long align_mask;
269
270 align_mask = (1UL << get_order(size)) - 1;
271 map = dma_map_area(dev, paddr, size, dir, align_mask);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100272
Andi Kleena32073b2006-06-26 13:56:40 +0200273 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100274
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100275 return map;
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/* Map a single area into the IOMMU */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100279static dma_addr_t
Ingo Molnar2be62142008-04-19 19:19:56 +0200280gart_map_single(struct device *dev, phys_addr_t paddr, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Ingo Molnar2be62142008-04-19 19:19:56 +0200282 unsigned long bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!dev)
285 dev = &fallback_dev;
286
Ingo Molnar2be62142008-04-19 19:19:56 +0200287 if (!need_iommu(dev, paddr, size))
288 return paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900290 bus = dma_map_area(dev, paddr, size, dir, 0);
291 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100292
293 return bus;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100294}
295
296/*
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200297 * Free a DMA mapping.
298 */
Yinghai Lu1048fa52007-07-21 17:11:23 +0200299static void gart_unmap_single(struct device *dev, dma_addr_t dma_addr,
Ingo Molnar05fccb02008-01-30 13:30:12 +0100300 size_t size, int direction)
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200301{
302 unsigned long iommu_page;
303 int npages;
304 int i;
305
306 if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
307 dma_addr >= iommu_bus_base + iommu_size)
308 return;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100309
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200310 iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
Joerg Roedel87e39ea2008-07-25 14:58:00 +0200311 npages = iommu_num_pages(dma_addr, size);
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200312 for (i = 0; i < npages; i++) {
313 iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
314 CLEAR_LEAK(iommu_page + i);
315 }
316 free_iommu(iommu_page, npages);
317}
318
319/*
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100320 * Wrapper for pci_unmap_single working with scatterlists.
321 */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100322static void
323gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100324{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200325 struct scatterlist *s;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100326 int i;
327
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200328 for_each_sg(sg, s, nents, i) {
Jon Mason60b08c62006-02-26 04:18:22 +0100329 if (!s->dma_length || !s->length)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100330 break;
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200331 gart_unmap_single(dev, s->dma_address, s->dma_length, dir);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100332 }
333}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335/* Fallback for dma_map_sg in case of overflow */
336static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
337 int nents, int dir)
338{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200339 struct scatterlist *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int i;
341
342#ifdef CONFIG_IOMMU_DEBUG
343 printk(KERN_DEBUG "dma_map_sg overflow\n");
344#endif
345
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200346 for_each_sg(sg, s, nents, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200347 unsigned long addr = sg_phys(s);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100348
349 if (nonforced_iommu(dev, addr, s->length)) {
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900350 addr = dma_map_area(dev, addr, s->length, dir, 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100351 if (addr == bad_dma_address) {
352 if (i > 0)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100353 gart_unmap_sg(dev, sg, i, dir);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100354 nents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 sg[0].dma_length = 0;
356 break;
357 }
358 }
359 s->dma_address = addr;
360 s->dma_length = s->length;
361 }
Andi Kleena32073b2006-06-26 13:56:40 +0200362 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return nents;
365}
366
367/* Map multiple scatterlist entries continuous into the first. */
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800368static int __dma_map_cont(struct device *dev, struct scatterlist *start,
369 int nelems, struct scatterlist *sout,
370 unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900372 unsigned long iommu_start = alloc_iommu(dev, pages, 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100373 unsigned long iommu_page = iommu_start;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200374 struct scatterlist *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int i;
376
377 if (iommu_start == -1)
378 return -1;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200379
380 for_each_sg(start, s, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 unsigned long pages, addr;
382 unsigned long phys_addr = s->dma_address;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100383
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200384 BUG_ON(s != start && s->offset);
385 if (s == start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 sout->dma_address = iommu_bus_base;
387 sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
388 sout->dma_length = s->length;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100389 } else {
390 sout->dma_length += s->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 addr = phys_addr;
Joerg Roedel87e39ea2008-07-25 14:58:00 +0200394 pages = iommu_num_pages(s->offset, s->length);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100395 while (pages--) {
396 iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 SET_LEAK(iommu_page);
398 addr += PAGE_SIZE;
399 iommu_page++;
Andi Kleen0d5410642006-02-12 14:34:59 -0800400 }
Ingo Molnar05fccb02008-01-30 13:30:12 +0100401 }
402 BUG_ON(iommu_page - iommu_start != pages);
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
406
Ingo Molnar05fccb02008-01-30 13:30:12 +0100407static inline int
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800408dma_map_cont(struct device *dev, struct scatterlist *start, int nelems,
409 struct scatterlist *sout, unsigned long pages, int need)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200411 if (!need) {
412 BUG_ON(nelems != 1);
FUJITA Tomonorie88a39d2007-10-25 09:13:32 +0200413 sout->dma_address = start->dma_address;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200414 sout->dma_length = start->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200416 }
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800417 return __dma_map_cont(dev, start, nelems, sout, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
Ingo Molnar05fccb02008-01-30 13:30:12 +0100419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
421 * DMA map all entries in a scatterlist.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100422 * Merge chunks that have page aligned sizes into a continuous mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100424static int
425gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200427 struct scatterlist *s, *ps, *start_sg, *sgmap;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100428 int need = 0, nextneed, i, out, start;
429 unsigned long pages = 0;
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800430 unsigned int seg_size;
431 unsigned int max_seg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Ingo Molnar05fccb02008-01-30 13:30:12 +0100433 if (nents == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!dev)
437 dev = &fallback_dev;
438
439 out = 0;
440 start = 0;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200441 start_sg = sgmap = sg;
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800442 seg_size = 0;
443 max_seg_size = dma_get_max_seg_size(dev);
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200444 ps = NULL; /* shut up gcc */
445 for_each_sg(sg, s, nents, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200446 dma_addr_t addr = sg_phys(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Ingo Molnar05fccb02008-01-30 13:30:12 +0100448 s->dma_address = addr;
449 BUG_ON(s->length == 0);
450
451 nextneed = need_iommu(dev, addr, s->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 /* Handle the previous not yet processed entries */
454 if (i > start) {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100455 /*
456 * Can only merge when the last chunk ends on a
457 * page boundary and the new one doesn't have an
458 * offset.
459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!iommu_merge || !nextneed || !need || s->offset ||
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800461 (s->length + seg_size > max_seg_size) ||
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200462 (ps->offset + ps->length) % PAGE_SIZE) {
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800463 if (dma_map_cont(dev, start_sg, i - start,
464 sgmap, pages, need) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 goto error;
466 out++;
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800467 seg_size = 0;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200468 sgmap = sg_next(sgmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 pages = 0;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200470 start = i;
471 start_sg = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 }
474
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800475 seg_size += s->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 need = nextneed;
Joerg Roedel87e39ea2008-07-25 14:58:00 +0200477 pages += iommu_num_pages(s->offset, s->length);
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200478 ps = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800480 if (dma_map_cont(dev, start_sg, i - start, sgmap, pages, need) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto error;
482 out++;
Andi Kleena32073b2006-06-26 13:56:40 +0200483 flush_gart();
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200484 if (out < nents) {
485 sgmap = sg_next(sgmap);
486 sgmap->dma_length = 0;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return out;
489
490error:
Andi Kleena32073b2006-06-26 13:56:40 +0200491 flush_gart();
FUJITA Tomonori53369402007-10-26 13:56:24 +0200492 gart_unmap_sg(dev, sg, out, dir);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100493
Kevin VanMarena1002a42006-02-03 21:51:32 +0100494 /* When it was forced or merged try again in a dumb way */
495 if (force_iommu || iommu_merge) {
496 out = dma_map_sg_nonforce(dev, sg, nents, dir);
497 if (out > 0)
498 return out;
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (panic_on_overflow)
501 panic("dma_map_sg: overflow on %lu pages\n", pages);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100502
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100503 iommu_full(dev, pages << PAGE_SHIFT, dir);
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200504 for_each_sg(sg, s, nents, i)
505 s->dma_address = bad_dma_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 0;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100507}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100509static int no_agp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100512{
513 unsigned long a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Ingo Molnar05fccb02008-01-30 13:30:12 +0100515 if (!iommu_size) {
516 iommu_size = aper_size;
517 if (!no_agp)
518 iommu_size /= 2;
519 }
520
521 a = aper + iommu_size;
Andi Kleen31422c52008-02-04 16:48:08 +0100522 iommu_size -= round_up(a, PMD_PAGE_SIZE) - a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Ingo Molnar05fccb02008-01-30 13:30:12 +0100524 if (iommu_size < 64*1024*1024) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 printk(KERN_WARNING
Ingo Molnar05fccb02008-01-30 13:30:12 +0100526 "PCI-DMA: Warning: Small IOMMU %luMB."
527 " Consider increasing the AGP aperture in BIOS\n",
528 iommu_size >> 20);
529 }
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return iommu_size;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100532}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Ingo Molnar05fccb02008-01-30 13:30:12 +0100534static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
535{
536 unsigned aper_size = 0, aper_base_32, aper_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 u64 aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Pavel Machek3bb6fbf2008-04-15 12:43:57 +0200539 pci_read_config_dword(dev, AMD64_GARTAPERTUREBASE, &aper_base_32);
540 pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &aper_order);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100541 aper_order = (aper_order >> 1) & 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Ingo Molnar05fccb02008-01-30 13:30:12 +0100543 aper_base = aper_base_32 & 0x7fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 aper_base <<= 25;
545
Ingo Molnar05fccb02008-01-30 13:30:12 +0100546 aper_size = (32 * 1024 * 1024) << aper_order;
547 if (aper_base + aper_size > 0x100000000UL || !aper_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 aper_base = 0;
549
550 *size = aper_size;
551 return aper_base;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100552}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200554static void enable_gart_translations(void)
555{
556 int i;
557
558 for (i = 0; i < num_k8_northbridges; i++) {
559 struct pci_dev *dev = k8_northbridges[i];
560
561 enable_gart_translation(dev, __pa(agp_gatt_table));
562 }
563}
564
565/*
566 * If fix_up_north_bridges is set, the north bridges have to be fixed up on
567 * resume in the same way as they are handled in gart_iommu_hole_init().
568 */
569static bool fix_up_north_bridges;
570static u32 aperture_order;
571static u32 aperture_alloc;
572
573void set_up_gart_resume(u32 aper_order, u32 aper_alloc)
574{
575 fix_up_north_bridges = true;
576 aperture_order = aper_order;
577 aperture_alloc = aper_alloc;
578}
579
Pavel Machekcd763742008-05-29 00:30:21 -0700580static int gart_resume(struct sys_device *dev)
581{
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200582 printk(KERN_INFO "PCI-DMA: Resuming GART IOMMU\n");
583
584 if (fix_up_north_bridges) {
585 int i;
586
587 printk(KERN_INFO "PCI-DMA: Restoring GART aperture settings\n");
588
589 for (i = 0; i < num_k8_northbridges; i++) {
590 struct pci_dev *dev = k8_northbridges[i];
591
592 /*
593 * Don't enable translations just yet. That is the next
594 * step. Restore the pre-suspend aperture settings.
595 */
596 pci_write_config_dword(dev, AMD64_GARTAPERTURECTL,
597 aperture_order << 1);
598 pci_write_config_dword(dev, AMD64_GARTAPERTUREBASE,
599 aperture_alloc >> 25);
600 }
601 }
602
603 enable_gart_translations();
604
Pavel Machekcd763742008-05-29 00:30:21 -0700605 return 0;
606}
607
608static int gart_suspend(struct sys_device *dev, pm_message_t state)
609{
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200610 return 0;
Pavel Machekcd763742008-05-29 00:30:21 -0700611}
612
613static struct sysdev_class gart_sysdev_class = {
614 .name = "gart",
615 .suspend = gart_suspend,
616 .resume = gart_resume,
617
618};
619
620static struct sys_device device_gart = {
621 .id = 0,
622 .cls = &gart_sysdev_class,
623};
624
Ingo Molnar05fccb02008-01-30 13:30:12 +0100625/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * Private Northbridge GATT initialization in case we cannot use the
Ingo Molnar05fccb02008-01-30 13:30:12 +0100627 * AGP driver for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
629static __init int init_k8_gatt(struct agp_kern_info *info)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100630{
631 unsigned aper_size, gatt_size, new_aper_size;
632 unsigned aper_base, new_aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct pci_dev *dev;
634 void *gatt;
Pavel Machekcd763742008-05-29 00:30:21 -0700635 int i, error;
Yinghai Lu7ab073b2008-07-12 14:30:35 -0700636 unsigned long start_pfn, end_pfn;
Andi Kleena32073b2006-06-26 13:56:40 +0200637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 printk(KERN_INFO "PCI-DMA: Disabling AGP.\n");
639 aper_size = aper_base = info->aper_size = 0;
Andi Kleena32073b2006-06-26 13:56:40 +0200640 dev = NULL;
641 for (i = 0; i < num_k8_northbridges; i++) {
642 dev = k8_northbridges[i];
Ingo Molnar05fccb02008-01-30 13:30:12 +0100643 new_aper_base = read_aperture(dev, &new_aper_size);
644 if (!new_aper_base)
645 goto nommu;
646
647 if (!aper_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 aper_size = new_aper_size;
649 aper_base = new_aper_base;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100650 }
651 if (aper_size != new_aper_size || aper_base != new_aper_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 goto nommu;
653 }
654 if (!aper_base)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100655 goto nommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 info->aper_base = aper_base;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100657 info->aper_size = aper_size >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Ingo Molnar05fccb02008-01-30 13:30:12 +0100659 gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
660 gatt = (void *)__get_free_pages(GFP_KERNEL, get_order(gatt_size));
661 if (!gatt)
Joachim Deguaracf6387d2007-04-24 13:05:36 +0200662 panic("Cannot allocate GATT table");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100663 if (set_memory_uc((unsigned long)gatt, gatt_size >> PAGE_SHIFT))
Joachim Deguaracf6387d2007-04-24 13:05:36 +0200664 panic("Could not set GART PTEs to uncacheable pages");
Joachim Deguaracf6387d2007-04-24 13:05:36 +0200665
Ingo Molnar05fccb02008-01-30 13:30:12 +0100666 memset(gatt, 0, gatt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 agp_gatt_table = gatt;
Andi Kleena32073b2006-06-26 13:56:40 +0200668
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200669 enable_gart_translations();
Pavel Machekcd763742008-05-29 00:30:21 -0700670
671 error = sysdev_class_register(&gart_sysdev_class);
672 if (!error)
673 error = sysdev_register(&device_gart);
674 if (error)
675 panic("Could not register gart_sysdev -- would corrupt data on next suspend");
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200676
Andi Kleena32073b2006-06-26 13:56:40 +0200677 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100678
679 printk(KERN_INFO "PCI-DMA: aperture base @ %x size %u KB\n",
680 aper_base, aper_size>>10);
Yinghai Lu7ab073b2008-07-12 14:30:35 -0700681
682 /* need to map that range */
683 end_pfn = (aper_base>>PAGE_SHIFT) + (aper_size>>PAGE_SHIFT);
684 if (end_pfn > max_low_pfn_mapped) {
Yinghai Lu32b23e92008-07-13 14:29:41 -0700685 start_pfn = (aper_base>>PAGE_SHIFT);
686 init_memory_mapping(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
Yinghai Lu7ab073b2008-07-12 14:30:35 -0700687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
689
690 nommu:
Ingo Molnar05fccb02008-01-30 13:30:12 +0100691 /* Should not happen anymore */
Pavel Machek8f596102008-04-01 14:24:03 +0200692 printk(KERN_WARNING "PCI-DMA: More than 4GB of RAM and no IOMMU\n"
693 KERN_WARNING "falling back to iommu=soft.\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100694 return -1;
695}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697extern int agp_amd64_init(void);
698
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700699static struct dma_mapping_ops gart_dma_ops = {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100700 .map_single = gart_map_single,
701 .map_simple = gart_map_simple,
702 .unmap_single = gart_unmap_single,
703 .sync_single_for_cpu = NULL,
704 .sync_single_for_device = NULL,
705 .sync_single_range_for_cpu = NULL,
706 .sync_single_range_for_device = NULL,
707 .sync_sg_for_cpu = NULL,
708 .sync_sg_for_device = NULL,
709 .map_sg = gart_map_sg,
710 .unmap_sg = gart_unmap_sg,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100711};
712
Yinghai Lubc2cea62007-07-21 17:11:28 +0200713void gart_iommu_shutdown(void)
714{
715 struct pci_dev *dev;
716 int i;
717
718 if (no_agp && (dma_ops != &gart_dma_ops))
719 return;
720
Ingo Molnar05fccb02008-01-30 13:30:12 +0100721 for (i = 0; i < num_k8_northbridges; i++) {
722 u32 ctl;
Yinghai Lubc2cea62007-07-21 17:11:28 +0200723
Ingo Molnar05fccb02008-01-30 13:30:12 +0100724 dev = k8_northbridges[i];
Pavel Machek3bb6fbf2008-04-15 12:43:57 +0200725 pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &ctl);
Yinghai Lubc2cea62007-07-21 17:11:28 +0200726
Pavel Machek3bb6fbf2008-04-15 12:43:57 +0200727 ctl &= ~GARTEN;
Yinghai Lubc2cea62007-07-21 17:11:28 +0200728
Pavel Machek3bb6fbf2008-04-15 12:43:57 +0200729 pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, ctl);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100730 }
Yinghai Lubc2cea62007-07-21 17:11:28 +0200731}
732
Jon Mason0dc243a2006-06-26 13:58:11 +0200733void __init gart_iommu_init(void)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100734{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct agp_kern_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 unsigned long iommu_start;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100737 unsigned long aper_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned long scratch;
739 long i;
740
Andi Kleena32073b2006-06-26 13:56:40 +0200741 if (cache_k8_northbridges() < 0 || num_k8_northbridges == 0) {
742 printk(KERN_INFO "PCI-GART: No AMD northbridge found.\n");
Jon Mason0dc243a2006-06-26 13:58:11 +0200743 return;
Andi Kleena32073b2006-06-26 13:56:40 +0200744 }
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746#ifndef CONFIG_AGP_AMD64
Ingo Molnar05fccb02008-01-30 13:30:12 +0100747 no_agp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748#else
749 /* Makefile puts PCI initialization via subsys_initcall first. */
750 /* Add other K8 AGP bridge drivers here */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100751 no_agp = no_agp ||
752 (agp_amd64_init() < 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 (agp_copy_info(agp_bridge, &info) < 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100754#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Jon Mason60b08c62006-02-26 04:18:22 +0100756 if (swiotlb)
Jon Mason0dc243a2006-06-26 13:58:11 +0200757 return;
Jon Mason60b08c62006-02-26 04:18:22 +0100758
Jon Mason8d4f6b92006-06-26 13:58:05 +0200759 /* Did we detect a different HW IOMMU? */
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200760 if (iommu_detected && !gart_iommu_aperture)
Jon Mason0dc243a2006-06-26 13:58:11 +0200761 return;
Jon Mason8d4f6b92006-06-26 13:58:05 +0200762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (no_iommu ||
Yinghai Luc987d122008-06-24 22:14:09 -0700764 (!force_iommu && max_pfn <= MAX_DMA32_PFN) ||
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200765 !gart_iommu_aperture ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 (no_agp && init_k8_gatt(&info) < 0)) {
Yinghai Luc987d122008-06-24 22:14:09 -0700767 if (max_pfn > MAX_DMA32_PFN) {
Pavel Machek8f596102008-04-01 14:24:03 +0200768 printk(KERN_WARNING "More than 4GB of memory "
769 "but GART IOMMU not available.\n"
770 KERN_WARNING "falling back to iommu=soft.\n");
Jon Mason5b7b6442006-02-03 21:51:59 +0100771 }
Jon Mason0dc243a2006-06-26 13:58:11 +0200772 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Jon Mason5b7b6442006-02-03 21:51:59 +0100775 printk(KERN_INFO "PCI-DMA: using GART IOMMU.\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100776 aper_size = info.aper_size * 1024 * 1024;
777 iommu_size = check_iommu_size(info.aper_base, aper_size);
778 iommu_pages = iommu_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Ingo Molnar05fccb02008-01-30 13:30:12 +0100780 iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL,
781 get_order(iommu_pages/8));
782 if (!iommu_gart_bitmap)
783 panic("Cannot allocate iommu bitmap\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 memset(iommu_gart_bitmap, 0, iommu_pages/8);
785
786#ifdef CONFIG_IOMMU_LEAK
Ingo Molnar05fccb02008-01-30 13:30:12 +0100787 if (leak_trace) {
788 iommu_leak_tab = (void *)__get_free_pages(GFP_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 get_order(iommu_pages*sizeof(void *)));
Ingo Molnar05fccb02008-01-30 13:30:12 +0100790 if (iommu_leak_tab)
791 memset(iommu_leak_tab, 0, iommu_pages * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 else
Ingo Molnar05fccb02008-01-30 13:30:12 +0100793 printk(KERN_DEBUG
794 "PCI-DMA: Cannot allocate leak trace area\n");
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796#endif
797
Ingo Molnar05fccb02008-01-30 13:30:12 +0100798 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * Out of IOMMU space handling.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100800 * Reserve some invalid pages at the beginning of the GART.
801 */
802 set_bit_string(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Ingo Molnar05fccb02008-01-30 13:30:12 +0100804 agp_memory_reserved = iommu_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 printk(KERN_INFO
806 "PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
Ingo Molnar05fccb02008-01-30 13:30:12 +0100807 iommu_size >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Ingo Molnar05fccb02008-01-30 13:30:12 +0100809 iommu_start = aper_size - iommu_size;
810 iommu_bus_base = info.aper_base + iommu_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 bad_dma_address = iommu_bus_base;
812 iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
813
Ingo Molnar05fccb02008-01-30 13:30:12 +0100814 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * Unmap the IOMMU part of the GART. The alias of the page is
816 * always mapped with cache enabled and there is no full cache
817 * coherency across the GART remapping. The unmapping avoids
818 * automatic prefetches from the CPU allocating cache lines in
819 * there. All CPU accesses are done via the direct mapping to
820 * the backing memory. The GART address is only used by PCI
Ingo Molnar05fccb02008-01-30 13:30:12 +0100821 * devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
Andi Kleen28d6ee42008-02-04 16:48:08 +0100823 set_memory_np((unsigned long)__va(iommu_bus_base),
824 iommu_size >> PAGE_SHIFT);
Ingo Molnar184652e2008-02-14 23:30:20 +0100825 /*
826 * Tricky. The GART table remaps the physical memory range,
827 * so the CPU wont notice potential aliases and if the memory
828 * is remapped to UC later on, we might surprise the PCI devices
829 * with a stray writeout of a cacheline. So play it sure and
830 * do an explicit, full-scale wbinvd() _after_ having marked all
831 * the pages as Not-Present:
832 */
833 wbinvd();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Ingo Molnar05fccb02008-01-30 13:30:12 +0100835 /*
Pavel Machekfa3d3192008-06-26 00:25:43 +0200836 * Try to workaround a bug (thanks to BenH):
Ingo Molnar05fccb02008-01-30 13:30:12 +0100837 * Set unmapped entries to a scratch page instead of 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 * Any prefetches that hit unmapped entries won't get an bus abort
Pavel Machekfa3d3192008-06-26 00:25:43 +0200839 * then. (P2P bridge may be prefetching on DMA reads).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100841 scratch = get_zeroed_page(GFP_KERNEL);
842 if (!scratch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 panic("Cannot allocate iommu scratch page");
844 gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
Ingo Molnar05fccb02008-01-30 13:30:12 +0100845 for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 iommu_gatt_base[i] = gart_unmapped_entry;
847
Andi Kleena32073b2006-06-26 13:56:40 +0200848 flush_gart();
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100849 dma_ops = &gart_dma_ops;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100850}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Sam Ravnborg43999d92007-03-16 21:07:36 +0100852void __init gart_parse_options(char *p)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100853{
854 int arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856#ifdef CONFIG_IOMMU_LEAK
Ingo Molnar05fccb02008-01-30 13:30:12 +0100857 if (!strncmp(p, "leak", 4)) {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100858 leak_trace = 1;
859 p += 4;
860 if (*p == '=') ++p;
861 if (isdigit(*p) && get_option(&p, &arg))
862 iommu_leak_pages = arg;
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864#endif
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100865 if (isdigit(*p) && get_option(&p, &arg))
866 iommu_size = arg;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100867 if (!strncmp(p, "fullflush", 8))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100868 iommu_fullflush = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100869 if (!strncmp(p, "nofullflush", 11))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100870 iommu_fullflush = 0;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100871 if (!strncmp(p, "noagp", 5))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100872 no_agp = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100873 if (!strncmp(p, "noaperture", 10))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100874 fix_aperture = 0;
875 /* duplicated from pci-dma.c */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100876 if (!strncmp(p, "force", 5))
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200877 gart_iommu_aperture_allowed = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100878 if (!strncmp(p, "allowed", 7))
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200879 gart_iommu_aperture_allowed = 1;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100880 if (!strncmp(p, "memaper", 7)) {
881 fallback_aper_force = 1;
882 p += 7;
883 if (*p == '=') {
884 ++p;
885 if (get_option(&p, &arg))
886 fallback_aper_order = arg;
887 }
888 }
889}