blob: 5ee700f0844de67675bae79b8138ffdcf6131a94 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/atomic.h>
29#include <asm/io.h>
30#include <asm/mtrr.h>
31#include <asm/pgtable.h>
32#include <asm/proto.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020033#include <asm/gart.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/cacheflush.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010035#include <asm/swiotlb.h>
36#include <asm/dma.h>
Andi Kleena32073b2006-06-26 13:56:40 +020037#include <asm/k8.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Joerg Roedel79da0872007-10-24 12:49:49 +020039static unsigned long iommu_bus_base; /* GART remapping area (physical) */
Ingo Molnar05fccb02008-01-30 13:30:12 +010040static unsigned long iommu_size; /* size of remapping area bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static unsigned long iommu_pages; /* .. and in pages */
42
Ingo Molnar05fccb02008-01-30 13:30:12 +010043static u32 *iommu_gatt_base; /* Remapping table */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Ingo Molnar05fccb02008-01-30 13:30:12 +010045/*
46 * If this is disabled the IOMMU will use an optimized flushing strategy
47 * of only flushing when an mapping is reused. With it true the GART is
48 * flushed for every mapping. Problem is that doing the lazy flush seems
49 * to trigger bugs with some popular PCI cards, in particular 3ware (but
50 * has been also also seen with Qlogic at least).
51 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052int iommu_fullflush = 1;
53
Ingo Molnar05fccb02008-01-30 13:30:12 +010054/* Allocation bitmap for the remapping area: */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static DEFINE_SPINLOCK(iommu_bitmap_lock);
Ingo Molnar05fccb02008-01-30 13:30:12 +010056/* Guarded by iommu_bitmap_lock: */
57static unsigned long *iommu_gart_bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ingo Molnar05fccb02008-01-30 13:30:12 +010059static u32 gart_unmapped_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define GPTE_VALID 1
62#define GPTE_COHERENT 2
63#define GPTE_ENCODE(x) \
64 (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
65#define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
66
Ingo Molnar05fccb02008-01-30 13:30:12 +010067#define to_pages(addr, size) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 (round_up(((addr) & ~PAGE_MASK) + (size), PAGE_SIZE) >> PAGE_SHIFT)
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
Ingo Molnar05fccb02008-01-30 13:30:12 +010085static unsigned long alloc_iommu(int size)
86{
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned long offset, flags;
88
Ingo Molnar05fccb02008-01-30 13:30:12 +010089 spin_lock_irqsave(&iommu_bitmap_lock, flags);
90 offset = find_next_zero_string(iommu_gart_bitmap, next_bit,
91 iommu_pages, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (offset == -1) {
93 need_flush = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +010094 offset = find_next_zero_string(iommu_gart_bitmap, 0,
95 iommu_pages, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Ingo Molnar05fccb02008-01-30 13:30:12 +010097 if (offset != -1) {
98 set_bit_string(iommu_gart_bitmap, offset, size);
99 next_bit = offset+size;
100 if (next_bit >= iommu_pages) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 next_bit = 0;
102 need_flush = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100103 }
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (iommu_fullflush)
106 need_flush = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100107 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return offset;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112static void free_iommu(unsigned long offset, int size)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100113{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned long flags;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 spin_lock_irqsave(&iommu_bitmap_lock, flags);
117 __clear_bit_string(iommu_gart_bitmap, offset, size);
118 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Ingo Molnar05fccb02008-01-30 13:30:12 +0100121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * Use global flush state to avoid races with multiple flushers.
123 */
Andi Kleena32073b2006-06-26 13:56:40 +0200124static void flush_gart(void)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100125{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned long flags;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 spin_lock_irqsave(&iommu_bitmap_lock, flags);
Andi Kleena32073b2006-06-26 13:56:40 +0200129 if (need_flush) {
130 k8_flush_garts();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 need_flush = 0;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#ifdef CONFIG_IOMMU_LEAK
137
Ingo Molnar05fccb02008-01-30 13:30:12 +0100138#define SET_LEAK(x) \
139 do { \
140 if (iommu_leak_tab) \
141 iommu_leak_tab[x] = __builtin_return_address(0);\
142 } while (0)
143
144#define CLEAR_LEAK(x) \
145 do { \
146 if (iommu_leak_tab) \
147 iommu_leak_tab[x] = NULL; \
148 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/* Debugging aid for drivers that don't free their IOMMU tables */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100151static void **iommu_leak_tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int leak_trace;
Joerg Roedel79da0872007-10-24 12:49:49 +0200153static int iommu_leak_pages = 20;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100154
Joerg Roedel79da0872007-10-24 12:49:49 +0200155static void dump_leak(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int i;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100158 static int dump;
159
160 if (dump || !iommu_leak_tab)
161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 dump = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100163 show_stack(NULL, NULL);
164
165 /* Very crude. dump some from the end of the table too */
166 printk(KERN_DEBUG "Dumping %d pages from end of IOMMU:\n",
167 iommu_leak_pages);
168 for (i = 0; i < iommu_leak_pages; i += 2) {
169 printk(KERN_DEBUG "%lu: ", iommu_pages-i);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100170 printk_address((unsigned long) iommu_leak_tab[iommu_pages-i], 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100171 printk(KERN_CONT "%c", (i+1)%2 == 0 ? '\n' : ' ');
172 }
173 printk(KERN_DEBUG "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175#else
Ingo Molnar05fccb02008-01-30 13:30:12 +0100176# define SET_LEAK(x)
177# define CLEAR_LEAK(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif
179
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100180static void iommu_full(struct device *dev, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Ingo Molnar05fccb02008-01-30 13:30:12 +0100182 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * Ran out of IOMMU space for this operation. This is very bad.
184 * Unfortunately the drivers cannot handle this operation properly.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100185 * Return some non mapped prereserved space in the aperture and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * let the Northbridge deal with it. This will result in garbage
187 * in the IO operation. When the size exceeds the prereserved space
Ingo Molnar05fccb02008-01-30 13:30:12 +0100188 * memory corruption will occur or random memory will be DMAed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * out. Hopefully no network devices use single mappings that big.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100190 */
191
192 printk(KERN_ERR
193 "PCI-DMA: Out of IOMMU space for %lu bytes at device %s\n",
194 size, dev->bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100196 if (size > PAGE_SIZE*EMERGENCY_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
198 panic("PCI-DMA: Memory would be corrupted\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100199 if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
200 panic(KERN_ERR
201 "PCI-DMA: Random memory would be DMAed\n");
202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#ifdef CONFIG_IOMMU_LEAK
Ingo Molnar05fccb02008-01-30 13:30:12 +0100204 dump_leak();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Ingo Molnar05fccb02008-01-30 13:30:12 +0100208static inline int
209need_iommu(struct device *dev, unsigned long addr, size_t size)
210{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 u64 mask = *dev->dma_mask;
Andi Kleen00edefa2007-02-13 13:26:24 +0100212 int high = addr + size > mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int mmu = high;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100214
215 if (force_iommu)
216 mmu = 1;
217
218 return mmu;
219}
220
221static inline int
222nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
223{
224 u64 mask = *dev->dma_mask;
225 int high = addr + size > mask;
226 int mmu = high;
227
228 return mmu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231/* Map a single continuous physical area into the IOMMU.
232 * Caller needs to check if the iommu is needed and flush.
233 */
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100234static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
235 size_t size, int dir)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100236{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 unsigned long npages = to_pages(phys_mem, size);
238 unsigned long iommu_page = alloc_iommu(npages);
239 int i;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (iommu_page == -1) {
242 if (!nonforced_iommu(dev, phys_mem, size))
Ingo Molnar05fccb02008-01-30 13:30:12 +0100243 return phys_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (panic_on_overflow)
245 panic("dma_map_area overflow %lu bytes\n", size);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100246 iommu_full(dev, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return bad_dma_address;
248 }
249
250 for (i = 0; i < npages; i++) {
251 iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
252 SET_LEAK(iommu_page + i);
253 phys_mem += PAGE_SIZE;
254 }
255 return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
256}
257
Ingo Molnar05fccb02008-01-30 13:30:12 +0100258static dma_addr_t
259gart_map_simple(struct device *dev, char *buf, size_t size, int dir)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100260{
261 dma_addr_t map = dma_map_area(dev, virt_to_bus(buf), size, dir);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100262
Andi Kleena32073b2006-06-26 13:56:40 +0200263 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100264
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100265 return map;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/* Map a single area into the IOMMU */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100269static dma_addr_t
270gart_map_single(struct device *dev, void *addr, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 unsigned long phys_mem, bus;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (!dev)
275 dev = &fallback_dev;
276
Ingo Molnar05fccb02008-01-30 13:30:12 +0100277 phys_mem = virt_to_phys(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!need_iommu(dev, phys_mem, size))
Ingo Molnar05fccb02008-01-30 13:30:12 +0100279 return phys_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100281 bus = gart_map_simple(dev, addr, size, dir);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100282
283 return bus;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100284}
285
286/*
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200287 * Free a DMA mapping.
288 */
Yinghai Lu1048fa52007-07-21 17:11:23 +0200289static void gart_unmap_single(struct device *dev, dma_addr_t dma_addr,
Ingo Molnar05fccb02008-01-30 13:30:12 +0100290 size_t size, int direction)
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200291{
292 unsigned long iommu_page;
293 int npages;
294 int i;
295
296 if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
297 dma_addr >= iommu_bus_base + iommu_size)
298 return;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100299
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200300 iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
301 npages = to_pages(dma_addr, size);
302 for (i = 0; i < npages; i++) {
303 iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
304 CLEAR_LEAK(iommu_page + i);
305 }
306 free_iommu(iommu_page, npages);
307}
308
309/*
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100310 * Wrapper for pci_unmap_single working with scatterlists.
311 */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100312static void
313gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100314{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200315 struct scatterlist *s;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100316 int i;
317
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200318 for_each_sg(sg, s, nents, i) {
Jon Mason60b08c62006-02-26 04:18:22 +0100319 if (!s->dma_length || !s->length)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100320 break;
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200321 gart_unmap_single(dev, s->dma_address, s->dma_length, dir);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100322 }
323}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325/* Fallback for dma_map_sg in case of overflow */
326static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
327 int nents, int dir)
328{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200329 struct scatterlist *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int i;
331
332#ifdef CONFIG_IOMMU_DEBUG
333 printk(KERN_DEBUG "dma_map_sg overflow\n");
334#endif
335
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200336 for_each_sg(sg, s, nents, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200337 unsigned long addr = sg_phys(s);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100338
339 if (nonforced_iommu(dev, addr, s->length)) {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100340 addr = dma_map_area(dev, addr, s->length, dir);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100341 if (addr == bad_dma_address) {
342 if (i > 0)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100343 gart_unmap_sg(dev, sg, i, dir);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100344 nents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 sg[0].dma_length = 0;
346 break;
347 }
348 }
349 s->dma_address = addr;
350 s->dma_length = s->length;
351 }
Andi Kleena32073b2006-06-26 13:56:40 +0200352 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return nents;
355}
356
357/* Map multiple scatterlist entries continuous into the first. */
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200358static int __dma_map_cont(struct scatterlist *start, int nelems,
Ingo Molnar05fccb02008-01-30 13:30:12 +0100359 struct scatterlist *sout, unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 unsigned long iommu_start = alloc_iommu(pages);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100362 unsigned long iommu_page = iommu_start;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200363 struct scatterlist *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int i;
365
366 if (iommu_start == -1)
367 return -1;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200368
369 for_each_sg(start, s, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned long pages, addr;
371 unsigned long phys_addr = s->dma_address;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100372
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200373 BUG_ON(s != start && s->offset);
374 if (s == start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 sout->dma_address = iommu_bus_base;
376 sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
377 sout->dma_length = s->length;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100378 } else {
379 sout->dma_length += s->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
382 addr = phys_addr;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100383 pages = to_pages(s->offset, s->length);
384 while (pages--) {
385 iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 SET_LEAK(iommu_page);
387 addr += PAGE_SIZE;
388 iommu_page++;
Andi Kleen0d5410642006-02-12 14:34:59 -0800389 }
Ingo Molnar05fccb02008-01-30 13:30:12 +0100390 }
391 BUG_ON(iommu_page - iommu_start != pages);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 0;
394}
395
Ingo Molnar05fccb02008-01-30 13:30:12 +0100396static inline int
397dma_map_cont(struct scatterlist *start, int nelems, struct scatterlist *sout,
398 unsigned long pages, int need)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200400 if (!need) {
401 BUG_ON(nelems != 1);
FUJITA Tomonorie88a39d2007-10-25 09:13:32 +0200402 sout->dma_address = start->dma_address;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200403 sout->dma_length = start->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200405 }
406 return __dma_map_cont(start, nelems, sout, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
Ingo Molnar05fccb02008-01-30 13:30:12 +0100408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/*
410 * DMA map all entries in a scatterlist.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100411 * Merge chunks that have page aligned sizes into a continuous mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100413static int
414gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200416 struct scatterlist *s, *ps, *start_sg, *sgmap;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100417 int need = 0, nextneed, i, out, start;
418 unsigned long pages = 0;
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800419 unsigned int seg_size;
420 unsigned int max_seg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Ingo Molnar05fccb02008-01-30 13:30:12 +0100422 if (nents == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (!dev)
426 dev = &fallback_dev;
427
428 out = 0;
429 start = 0;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200430 start_sg = sgmap = sg;
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800431 seg_size = 0;
432 max_seg_size = dma_get_max_seg_size(dev);
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200433 ps = NULL; /* shut up gcc */
434 for_each_sg(sg, s, nents, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200435 dma_addr_t addr = sg_phys(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Ingo Molnar05fccb02008-01-30 13:30:12 +0100437 s->dma_address = addr;
438 BUG_ON(s->length == 0);
439
440 nextneed = need_iommu(dev, addr, s->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* Handle the previous not yet processed entries */
443 if (i > start) {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100444 /*
445 * Can only merge when the last chunk ends on a
446 * page boundary and the new one doesn't have an
447 * offset.
448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!iommu_merge || !nextneed || !need || s->offset ||
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800450 (s->length + seg_size > max_seg_size) ||
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200451 (ps->offset + ps->length) % PAGE_SIZE) {
452 if (dma_map_cont(start_sg, i - start, sgmap,
453 pages, need) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto error;
455 out++;
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800456 seg_size = 0;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200457 sgmap = sg_next(sgmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 pages = 0;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200459 start = i;
460 start_sg = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 }
463
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800464 seg_size += s->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 need = nextneed;
466 pages += to_pages(s->offset, s->length);
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200467 ps = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200469 if (dma_map_cont(start_sg, i - start, sgmap, pages, need) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto error;
471 out++;
Andi Kleena32073b2006-06-26 13:56:40 +0200472 flush_gart();
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200473 if (out < nents) {
474 sgmap = sg_next(sgmap);
475 sgmap->dma_length = 0;
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return out;
478
479error:
Andi Kleena32073b2006-06-26 13:56:40 +0200480 flush_gart();
FUJITA Tomonori53369402007-10-26 13:56:24 +0200481 gart_unmap_sg(dev, sg, out, dir);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100482
Kevin VanMarena1002a42006-02-03 21:51:32 +0100483 /* When it was forced or merged try again in a dumb way */
484 if (force_iommu || iommu_merge) {
485 out = dma_map_sg_nonforce(dev, sg, nents, dir);
486 if (out > 0)
487 return out;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (panic_on_overflow)
490 panic("dma_map_sg: overflow on %lu pages\n", pages);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100491
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100492 iommu_full(dev, pages << PAGE_SHIFT, dir);
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200493 for_each_sg(sg, s, nents, i)
494 s->dma_address = bad_dma_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100496}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100498static int no_agp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100501{
502 unsigned long a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Ingo Molnar05fccb02008-01-30 13:30:12 +0100504 if (!iommu_size) {
505 iommu_size = aper_size;
506 if (!no_agp)
507 iommu_size /= 2;
508 }
509
510 a = aper + iommu_size;
Andi Kleen31422c52008-02-04 16:48:08 +0100511 iommu_size -= round_up(a, PMD_PAGE_SIZE) - a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Ingo Molnar05fccb02008-01-30 13:30:12 +0100513 if (iommu_size < 64*1024*1024) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 printk(KERN_WARNING
Ingo Molnar05fccb02008-01-30 13:30:12 +0100515 "PCI-DMA: Warning: Small IOMMU %luMB."
516 " Consider increasing the AGP aperture in BIOS\n",
517 iommu_size >> 20);
518 }
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return iommu_size;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100521}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Ingo Molnar05fccb02008-01-30 13:30:12 +0100523static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
524{
525 unsigned aper_size = 0, aper_base_32, aper_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 u64 aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Ingo Molnar05fccb02008-01-30 13:30:12 +0100528 pci_read_config_dword(dev, 0x94, &aper_base_32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 pci_read_config_dword(dev, 0x90, &aper_order);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100530 aper_order = (aper_order >> 1) & 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Ingo Molnar05fccb02008-01-30 13:30:12 +0100532 aper_base = aper_base_32 & 0x7fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 aper_base <<= 25;
534
Ingo Molnar05fccb02008-01-30 13:30:12 +0100535 aper_size = (32 * 1024 * 1024) << aper_order;
536 if (aper_base + aper_size > 0x100000000UL || !aper_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 aper_base = 0;
538
539 *size = aper_size;
540 return aper_base;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100541}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Ingo Molnar05fccb02008-01-30 13:30:12 +0100543/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * Private Northbridge GATT initialization in case we cannot use the
Ingo Molnar05fccb02008-01-30 13:30:12 +0100545 * AGP driver for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
547static __init int init_k8_gatt(struct agp_kern_info *info)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100548{
549 unsigned aper_size, gatt_size, new_aper_size;
550 unsigned aper_base, new_aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct pci_dev *dev;
552 void *gatt;
Andi Kleena32073b2006-06-26 13:56:40 +0200553 int i;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 printk(KERN_INFO "PCI-DMA: Disabling AGP.\n");
556 aper_size = aper_base = info->aper_size = 0;
Andi Kleena32073b2006-06-26 13:56:40 +0200557 dev = NULL;
558 for (i = 0; i < num_k8_northbridges; i++) {
559 dev = k8_northbridges[i];
Ingo Molnar05fccb02008-01-30 13:30:12 +0100560 new_aper_base = read_aperture(dev, &new_aper_size);
561 if (!new_aper_base)
562 goto nommu;
563
564 if (!aper_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 aper_size = new_aper_size;
566 aper_base = new_aper_base;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100567 }
568 if (aper_size != new_aper_size || aper_base != new_aper_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto nommu;
570 }
571 if (!aper_base)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100572 goto nommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 info->aper_base = aper_base;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100574 info->aper_size = aper_size >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Ingo Molnar05fccb02008-01-30 13:30:12 +0100576 gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
577 gatt = (void *)__get_free_pages(GFP_KERNEL, get_order(gatt_size));
578 if (!gatt)
Joachim Deguaracf6387d2007-04-24 13:05:36 +0200579 panic("Cannot allocate GATT table");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100580 if (set_memory_uc((unsigned long)gatt, gatt_size >> PAGE_SHIFT))
Joachim Deguaracf6387d2007-04-24 13:05:36 +0200581 panic("Could not set GART PTEs to uncacheable pages");
Joachim Deguaracf6387d2007-04-24 13:05:36 +0200582
Ingo Molnar05fccb02008-01-30 13:30:12 +0100583 memset(gatt, 0, gatt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 agp_gatt_table = gatt;
Andi Kleena32073b2006-06-26 13:56:40 +0200585
586 for (i = 0; i < num_k8_northbridges; i++) {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100587 u32 gatt_reg;
588 u32 ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Andi Kleena32073b2006-06-26 13:56:40 +0200590 dev = k8_northbridges[i];
Ingo Molnar05fccb02008-01-30 13:30:12 +0100591 gatt_reg = __pa(gatt) >> 12;
592 gatt_reg <<= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 pci_write_config_dword(dev, 0x98, gatt_reg);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100594 pci_read_config_dword(dev, 0x90, &ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 ctl |= 1;
597 ctl &= ~((1<<4) | (1<<5));
598
Ingo Molnar05fccb02008-01-30 13:30:12 +0100599 pci_write_config_dword(dev, 0x90, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Andi Kleena32073b2006-06-26 13:56:40 +0200601 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100602
603 printk(KERN_INFO "PCI-DMA: aperture base @ %x size %u KB\n",
604 aper_base, aper_size>>10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
606
607 nommu:
Ingo Molnar05fccb02008-01-30 13:30:12 +0100608 /* Should not happen anymore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 printk(KERN_ERR "PCI-DMA: More than 4GB of RAM and no IOMMU\n"
Andi Kleenf46ace62006-01-11 22:43:27 +0100610 KERN_ERR "PCI-DMA: 32bit PCI IO may malfunction.\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100611 return -1;
612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614extern int agp_amd64_init(void);
615
Stephen Hemmingere6584502007-05-02 19:27:06 +0200616static const struct dma_mapping_ops gart_dma_ops = {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100617 .mapping_error = NULL,
618 .map_single = gart_map_single,
619 .map_simple = gart_map_simple,
620 .unmap_single = gart_unmap_single,
621 .sync_single_for_cpu = NULL,
622 .sync_single_for_device = NULL,
623 .sync_single_range_for_cpu = NULL,
624 .sync_single_range_for_device = NULL,
625 .sync_sg_for_cpu = NULL,
626 .sync_sg_for_device = NULL,
627 .map_sg = gart_map_sg,
628 .unmap_sg = gart_unmap_sg,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100629};
630
Yinghai Lubc2cea62007-07-21 17:11:28 +0200631void gart_iommu_shutdown(void)
632{
633 struct pci_dev *dev;
634 int i;
635
636 if (no_agp && (dma_ops != &gart_dma_ops))
637 return;
638
Ingo Molnar05fccb02008-01-30 13:30:12 +0100639 for (i = 0; i < num_k8_northbridges; i++) {
640 u32 ctl;
Yinghai Lubc2cea62007-07-21 17:11:28 +0200641
Ingo Molnar05fccb02008-01-30 13:30:12 +0100642 dev = k8_northbridges[i];
643 pci_read_config_dword(dev, 0x90, &ctl);
Yinghai Lubc2cea62007-07-21 17:11:28 +0200644
Ingo Molnar05fccb02008-01-30 13:30:12 +0100645 ctl &= ~1;
Yinghai Lubc2cea62007-07-21 17:11:28 +0200646
Ingo Molnar05fccb02008-01-30 13:30:12 +0100647 pci_write_config_dword(dev, 0x90, ctl);
648 }
Yinghai Lubc2cea62007-07-21 17:11:28 +0200649}
650
Jon Mason0dc243a2006-06-26 13:58:11 +0200651void __init gart_iommu_init(void)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct agp_kern_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 unsigned long iommu_start;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100655 unsigned long aper_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 unsigned long scratch;
657 long i;
658
Andi Kleena32073b2006-06-26 13:56:40 +0200659 if (cache_k8_northbridges() < 0 || num_k8_northbridges == 0) {
660 printk(KERN_INFO "PCI-GART: No AMD northbridge found.\n");
Jon Mason0dc243a2006-06-26 13:58:11 +0200661 return;
Andi Kleena32073b2006-06-26 13:56:40 +0200662 }
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#ifndef CONFIG_AGP_AMD64
Ingo Molnar05fccb02008-01-30 13:30:12 +0100665 no_agp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666#else
667 /* Makefile puts PCI initialization via subsys_initcall first. */
668 /* Add other K8 AGP bridge drivers here */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100669 no_agp = no_agp ||
670 (agp_amd64_init() < 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 (agp_copy_info(agp_bridge, &info) < 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100672#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Jon Mason60b08c62006-02-26 04:18:22 +0100674 if (swiotlb)
Jon Mason0dc243a2006-06-26 13:58:11 +0200675 return;
Jon Mason60b08c62006-02-26 04:18:22 +0100676
Jon Mason8d4f6b92006-06-26 13:58:05 +0200677 /* Did we detect a different HW IOMMU? */
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200678 if (iommu_detected && !gart_iommu_aperture)
Jon Mason0dc243a2006-06-26 13:58:11 +0200679 return;
Jon Mason8d4f6b92006-06-26 13:58:05 +0200680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (no_iommu ||
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100682 (!force_iommu && end_pfn <= MAX_DMA32_PFN) ||
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200683 !gart_iommu_aperture ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 (no_agp && init_k8_gatt(&info) < 0)) {
Jon Mason5b7b6442006-02-03 21:51:59 +0100685 if (end_pfn > MAX_DMA32_PFN) {
686 printk(KERN_ERR "WARNING more than 4GB of memory "
Andi Kleen3807fd42006-12-07 02:14:13 +0100687 "but GART IOMMU not available.\n"
Andi Kleendc9a7192006-05-30 22:47:48 +0200688 KERN_ERR "WARNING 32bit PCI may malfunction.\n");
Jon Mason5b7b6442006-02-03 21:51:59 +0100689 }
Jon Mason0dc243a2006-06-26 13:58:11 +0200690 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
Jon Mason5b7b6442006-02-03 21:51:59 +0100693 printk(KERN_INFO "PCI-DMA: using GART IOMMU.\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100694 aper_size = info.aper_size * 1024 * 1024;
695 iommu_size = check_iommu_size(info.aper_base, aper_size);
696 iommu_pages = iommu_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Ingo Molnar05fccb02008-01-30 13:30:12 +0100698 iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL,
699 get_order(iommu_pages/8));
700 if (!iommu_gart_bitmap)
701 panic("Cannot allocate iommu bitmap\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 memset(iommu_gart_bitmap, 0, iommu_pages/8);
703
704#ifdef CONFIG_IOMMU_LEAK
Ingo Molnar05fccb02008-01-30 13:30:12 +0100705 if (leak_trace) {
706 iommu_leak_tab = (void *)__get_free_pages(GFP_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 get_order(iommu_pages*sizeof(void *)));
Ingo Molnar05fccb02008-01-30 13:30:12 +0100708 if (iommu_leak_tab)
709 memset(iommu_leak_tab, 0, iommu_pages * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 else
Ingo Molnar05fccb02008-01-30 13:30:12 +0100711 printk(KERN_DEBUG
712 "PCI-DMA: Cannot allocate leak trace area\n");
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714#endif
715
Ingo Molnar05fccb02008-01-30 13:30:12 +0100716 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * Out of IOMMU space handling.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100718 * Reserve some invalid pages at the beginning of the GART.
719 */
720 set_bit_string(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Ingo Molnar05fccb02008-01-30 13:30:12 +0100722 agp_memory_reserved = iommu_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 printk(KERN_INFO
724 "PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
Ingo Molnar05fccb02008-01-30 13:30:12 +0100725 iommu_size >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Ingo Molnar05fccb02008-01-30 13:30:12 +0100727 iommu_start = aper_size - iommu_size;
728 iommu_bus_base = info.aper_base + iommu_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 bad_dma_address = iommu_bus_base;
730 iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
731
Ingo Molnar05fccb02008-01-30 13:30:12 +0100732 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * Unmap the IOMMU part of the GART. The alias of the page is
734 * always mapped with cache enabled and there is no full cache
735 * coherency across the GART remapping. The unmapping avoids
736 * automatic prefetches from the CPU allocating cache lines in
737 * there. All CPU accesses are done via the direct mapping to
738 * the backing memory. The GART address is only used by PCI
Ingo Molnar05fccb02008-01-30 13:30:12 +0100739 * devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Andi Kleen28d6ee42008-02-04 16:48:08 +0100741 set_memory_np((unsigned long)__va(iommu_bus_base),
742 iommu_size >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Ingo Molnar05fccb02008-01-30 13:30:12 +0100744 /*
745 * Try to workaround a bug (thanks to BenH)
746 * Set unmapped entries to a scratch page instead of 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * Any prefetches that hit unmapped entries won't get an bus abort
748 * then.
749 */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100750 scratch = get_zeroed_page(GFP_KERNEL);
751 if (!scratch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 panic("Cannot allocate iommu scratch page");
753 gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
Ingo Molnar05fccb02008-01-30 13:30:12 +0100754 for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 iommu_gatt_base[i] = gart_unmapped_entry;
756
Andi Kleena32073b2006-06-26 13:56:40 +0200757 flush_gart();
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100758 dma_ops = &gart_dma_ops;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100759}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Sam Ravnborg43999d92007-03-16 21:07:36 +0100761void __init gart_parse_options(char *p)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100762{
763 int arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765#ifdef CONFIG_IOMMU_LEAK
Ingo Molnar05fccb02008-01-30 13:30:12 +0100766 if (!strncmp(p, "leak", 4)) {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100767 leak_trace = 1;
768 p += 4;
769 if (*p == '=') ++p;
770 if (isdigit(*p) && get_option(&p, &arg))
771 iommu_leak_pages = arg;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#endif
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100774 if (isdigit(*p) && get_option(&p, &arg))
775 iommu_size = arg;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100776 if (!strncmp(p, "fullflush", 8))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100777 iommu_fullflush = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100778 if (!strncmp(p, "nofullflush", 11))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100779 iommu_fullflush = 0;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100780 if (!strncmp(p, "noagp", 5))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100781 no_agp = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100782 if (!strncmp(p, "noaperture", 10))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100783 fix_aperture = 0;
784 /* duplicated from pci-dma.c */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100785 if (!strncmp(p, "force", 5))
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200786 gart_iommu_aperture_allowed = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100787 if (!strncmp(p, "allowed", 7))
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200788 gart_iommu_aperture_allowed = 1;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100789 if (!strncmp(p, "memaper", 7)) {
790 fallback_aper_force = 1;
791 p += 7;
792 if (*p == '=') {
793 ++p;
794 if (get_option(&p, &arg))
795 fallback_aper_order = arg;
796 }
797 }
798}