2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Dynamic DMA mapping support for AMD Hammer.
|
2008-01-30 20:30:12 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Use the integrated AGP GART in the Hammer northbridge as an IOMMU for PCI.
|
|
|
|
* This allows to use PCI devices that only support 32bit addresses on systems
|
2008-01-30 20:30:12 +08:00
|
|
|
* with more than 4GB.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* See Documentation/DMA-mapping.txt for the interface specification.
|
2008-01-30 20:30:12 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Copyright 2002 Andi Kleen, SuSE Labs.
|
2007-10-18 00:04:37 +08:00
|
|
|
* Subject to the GNU General Public License v2 only.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/agp_backend.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/topology.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/bitops.h>
|
2007-05-08 15:27:03 +08:00
|
|
|
#include <linux/kdebug.h>
|
2007-10-04 15:35:37 +08:00
|
|
|
#include <linux/scatterlist.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/atomic.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/mtrr.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/proto.h>
|
2007-10-24 18:49:47 +08:00
|
|
|
#include <asm/gart.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/cacheflush.h>
|
2006-01-12 05:44:42 +08:00
|
|
|
#include <asm/swiotlb.h>
|
|
|
|
#include <asm/dma.h>
|
2006-06-26 19:56:40 +08:00
|
|
|
#include <asm/k8.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-24 18:49:49 +08:00
|
|
|
static unsigned long iommu_bus_base; /* GART remapping area (physical) */
|
2008-01-30 20:30:12 +08:00
|
|
|
static unsigned long iommu_size; /* size of remapping area bytes */
|
2005-04-17 06:20:36 +08:00
|
|
|
static unsigned long iommu_pages; /* .. and in pages */
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
static u32 *iommu_gatt_base; /* Remapping table */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
/*
|
|
|
|
* If this is disabled the IOMMU will use an optimized flushing strategy
|
|
|
|
* of only flushing when an mapping is reused. With it true the GART is
|
|
|
|
* flushed for every mapping. Problem is that doing the lazy flush seems
|
|
|
|
* to trigger bugs with some popular PCI cards, in particular 3ware (but
|
|
|
|
* has been also also seen with Qlogic at least).
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
int iommu_fullflush = 1;
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
/* Allocation bitmap for the remapping area: */
|
2005-04-17 06:20:36 +08:00
|
|
|
static DEFINE_SPINLOCK(iommu_bitmap_lock);
|
2008-01-30 20:30:12 +08:00
|
|
|
/* Guarded by iommu_bitmap_lock: */
|
|
|
|
static unsigned long *iommu_gart_bitmap;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
static u32 gart_unmapped_entry;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#define GPTE_VALID 1
|
|
|
|
#define GPTE_COHERENT 2
|
|
|
|
#define GPTE_ENCODE(x) \
|
|
|
|
(((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
|
|
|
|
#define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
#define to_pages(addr, size) \
|
2005-04-17 06:20:36 +08:00
|
|
|
(round_up(((addr) & ~PAGE_MASK) + (size), PAGE_SIZE) >> PAGE_SHIFT)
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
#define EMERGENCY_PAGES 32 /* = 128KB */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_AGP
|
|
|
|
#define AGPEXTERN extern
|
|
|
|
#else
|
|
|
|
#define AGPEXTERN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* backdoor interface to AGP driver */
|
|
|
|
AGPEXTERN int agp_memory_reserved;
|
|
|
|
AGPEXTERN __u32 *agp_gatt_table;
|
|
|
|
|
|
|
|
static unsigned long next_bit; /* protected by iommu_bitmap_lock */
|
2008-01-30 20:30:12 +08:00
|
|
|
static int need_flush; /* global flush state. set for each gart wrap */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
static unsigned long alloc_iommu(int size)
|
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long offset, flags;
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
spin_lock_irqsave(&iommu_bitmap_lock, flags);
|
|
|
|
offset = find_next_zero_string(iommu_gart_bitmap, next_bit,
|
|
|
|
iommu_pages, size);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (offset == -1) {
|
|
|
|
need_flush = 1;
|
2008-01-30 20:30:12 +08:00
|
|
|
offset = find_next_zero_string(iommu_gart_bitmap, 0,
|
|
|
|
iommu_pages, size);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-01-30 20:30:12 +08:00
|
|
|
if (offset != -1) {
|
|
|
|
set_bit_string(iommu_gart_bitmap, offset, size);
|
|
|
|
next_bit = offset+size;
|
|
|
|
if (next_bit >= iommu_pages) {
|
2005-04-17 06:20:36 +08:00
|
|
|
next_bit = 0;
|
|
|
|
need_flush = 1;
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
if (iommu_fullflush)
|
|
|
|
need_flush = 1;
|
2008-01-30 20:30:12 +08:00
|
|
|
spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return offset;
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static void free_iommu(unsigned long offset, int size)
|
2008-01-30 20:30:12 +08:00
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long flags;
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_irqsave(&iommu_bitmap_lock, flags);
|
|
|
|
__clear_bit_string(iommu_gart_bitmap, offset, size);
|
|
|
|
spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Use global flush state to avoid races with multiple flushers.
|
|
|
|
*/
|
2006-06-26 19:56:40 +08:00
|
|
|
static void flush_gart(void)
|
2008-01-30 20:30:12 +08:00
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long flags;
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_irqsave(&iommu_bitmap_lock, flags);
|
2006-06-26 19:56:40 +08:00
|
|
|
if (need_flush) {
|
|
|
|
k8_flush_garts();
|
2005-04-17 06:20:36 +08:00
|
|
|
need_flush = 0;
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_IOMMU_LEAK
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
#define SET_LEAK(x) \
|
|
|
|
do { \
|
|
|
|
if (iommu_leak_tab) \
|
|
|
|
iommu_leak_tab[x] = __builtin_return_address(0);\
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define CLEAR_LEAK(x) \
|
|
|
|
do { \
|
|
|
|
if (iommu_leak_tab) \
|
|
|
|
iommu_leak_tab[x] = NULL; \
|
|
|
|
} while (0)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Debugging aid for drivers that don't free their IOMMU tables */
|
2008-01-30 20:30:12 +08:00
|
|
|
static void **iommu_leak_tab;
|
2005-04-17 06:20:36 +08:00
|
|
|
static int leak_trace;
|
2007-10-24 18:49:49 +08:00
|
|
|
static int iommu_leak_pages = 20;
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2007-10-24 18:49:49 +08:00
|
|
|
static void dump_leak(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int i;
|
2008-01-30 20:30:12 +08:00
|
|
|
static int dump;
|
|
|
|
|
|
|
|
if (dump || !iommu_leak_tab)
|
|
|
|
return;
|
2005-04-17 06:20:36 +08:00
|
|
|
dump = 1;
|
2008-01-30 20:30:12 +08:00
|
|
|
show_stack(NULL, NULL);
|
|
|
|
|
|
|
|
/* Very crude. dump some from the end of the table too */
|
|
|
|
printk(KERN_DEBUG "Dumping %d pages from end of IOMMU:\n",
|
|
|
|
iommu_leak_pages);
|
|
|
|
for (i = 0; i < iommu_leak_pages; i += 2) {
|
|
|
|
printk(KERN_DEBUG "%lu: ", iommu_pages-i);
|
2005-04-17 06:20:36 +08:00
|
|
|
printk_address((unsigned long) iommu_leak_tab[iommu_pages-i]);
|
2008-01-30 20:30:12 +08:00
|
|
|
printk(KERN_CONT "%c", (i+1)%2 == 0 ? '\n' : ' ');
|
|
|
|
}
|
|
|
|
printk(KERN_DEBUG "\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
#else
|
2008-01-30 20:30:12 +08:00
|
|
|
# define SET_LEAK(x)
|
|
|
|
# define CLEAR_LEAK(x)
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
2006-01-12 05:44:42 +08:00
|
|
|
static void iommu_full(struct device *dev, size_t size, int dir)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-01-30 20:30:12 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Ran out of IOMMU space for this operation. This is very bad.
|
|
|
|
* Unfortunately the drivers cannot handle this operation properly.
|
2008-01-30 20:30:12 +08:00
|
|
|
* Return some non mapped prereserved space in the aperture and
|
2005-04-17 06:20:36 +08:00
|
|
|
* let the Northbridge deal with it. This will result in garbage
|
|
|
|
* in the IO operation. When the size exceeds the prereserved space
|
2008-01-30 20:30:12 +08:00
|
|
|
* memory corruption will occur or random memory will be DMAed
|
2005-04-17 06:20:36 +08:00
|
|
|
* out. Hopefully no network devices use single mappings that big.
|
2008-01-30 20:30:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
printk(KERN_ERR
|
|
|
|
"PCI-DMA: Out of IOMMU space for %lu bytes at device %s\n",
|
|
|
|
size, dev->bus_id);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-12 05:44:42 +08:00
|
|
|
if (size > PAGE_SIZE*EMERGENCY_PAGES) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
|
|
|
|
panic("PCI-DMA: Memory would be corrupted\n");
|
2008-01-30 20:30:12 +08:00
|
|
|
if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
|
|
|
|
panic(KERN_ERR
|
|
|
|
"PCI-DMA: Random memory would be DMAed\n");
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
#ifdef CONFIG_IOMMU_LEAK
|
2008-01-30 20:30:12 +08:00
|
|
|
dump_leak();
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
static inline int
|
|
|
|
need_iommu(struct device *dev, unsigned long addr, size_t size)
|
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
u64 mask = *dev->dma_mask;
|
2007-02-13 20:26:24 +08:00
|
|
|
int high = addr + size > mask;
|
2005-04-17 06:20:36 +08:00
|
|
|
int mmu = high;
|
2008-01-30 20:30:12 +08:00
|
|
|
|
|
|
|
if (force_iommu)
|
|
|
|
mmu = 1;
|
|
|
|
|
|
|
|
return mmu;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
static inline int
|
|
|
|
nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
|
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
u64 mask = *dev->dma_mask;
|
2007-02-13 20:26:24 +08:00
|
|
|
int high = addr + size > mask;
|
2005-04-17 06:20:36 +08:00
|
|
|
int mmu = high;
|
2008-01-30 20:30:12 +08:00
|
|
|
|
|
|
|
return mmu;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Map a single continuous physical area into the IOMMU.
|
|
|
|
* Caller needs to check if the iommu is needed and flush.
|
|
|
|
*/
|
2006-01-12 05:44:42 +08:00
|
|
|
static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
|
|
|
|
size_t size, int dir)
|
2008-01-30 20:30:12 +08:00
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long npages = to_pages(phys_mem, size);
|
|
|
|
unsigned long iommu_page = alloc_iommu(npages);
|
|
|
|
int i;
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (iommu_page == -1) {
|
|
|
|
if (!nonforced_iommu(dev, phys_mem, size))
|
2008-01-30 20:30:12 +08:00
|
|
|
return phys_mem;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (panic_on_overflow)
|
|
|
|
panic("dma_map_area overflow %lu bytes\n", size);
|
2006-01-12 05:44:42 +08:00
|
|
|
iommu_full(dev, size, dir);
|
2005-04-17 06:20:36 +08:00
|
|
|
return bad_dma_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < npages; i++) {
|
|
|
|
iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
|
|
|
|
SET_LEAK(iommu_page + i);
|
|
|
|
phys_mem += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
|
|
|
|
}
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
static dma_addr_t
|
|
|
|
gart_map_simple(struct device *dev, char *buf, size_t size, int dir)
|
2006-01-12 05:44:42 +08:00
|
|
|
{
|
|
|
|
dma_addr_t map = dma_map_area(dev, virt_to_bus(buf), size, dir);
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2006-06-26 19:56:40 +08:00
|
|
|
flush_gart();
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2006-01-12 05:44:42 +08:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Map a single area into the IOMMU */
|
2008-01-30 20:30:12 +08:00
|
|
|
static dma_addr_t
|
|
|
|
gart_map_single(struct device *dev, void *addr, size_t size, int dir)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long phys_mem, bus;
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
dev = &fallback_dev;
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
phys_mem = virt_to_phys(addr);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!need_iommu(dev, phys_mem, size))
|
2008-01-30 20:30:12 +08:00
|
|
|
return phys_mem;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-12 05:44:42 +08:00
|
|
|
bus = gart_map_simple(dev, addr, size, dir);
|
2008-01-30 20:30:12 +08:00
|
|
|
|
|
|
|
return bus;
|
2006-01-12 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2006-06-26 19:56:37 +08:00
|
|
|
/*
|
|
|
|
* Free a DMA mapping.
|
|
|
|
*/
|
2007-07-21 23:11:23 +08:00
|
|
|
static void gart_unmap_single(struct device *dev, dma_addr_t dma_addr,
|
2008-01-30 20:30:12 +08:00
|
|
|
size_t size, int direction)
|
2006-06-26 19:56:37 +08:00
|
|
|
{
|
|
|
|
unsigned long iommu_page;
|
|
|
|
int npages;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
|
|
|
|
dma_addr >= iommu_bus_base + iommu_size)
|
|
|
|
return;
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2006-06-26 19:56:37 +08:00
|
|
|
iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
|
|
|
|
npages = to_pages(dma_addr, size);
|
|
|
|
for (i = 0; i < npages; i++) {
|
|
|
|
iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
|
|
|
|
CLEAR_LEAK(iommu_page + i);
|
|
|
|
}
|
|
|
|
free_iommu(iommu_page, npages);
|
|
|
|
}
|
|
|
|
|
2006-01-12 05:44:42 +08:00
|
|
|
/*
|
|
|
|
* Wrapper for pci_unmap_single working with scatterlists.
|
|
|
|
*/
|
2008-01-30 20:30:12 +08:00
|
|
|
static void
|
|
|
|
gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
|
2006-01-12 05:44:42 +08:00
|
|
|
{
|
2007-10-04 15:35:37 +08:00
|
|
|
struct scatterlist *s;
|
2006-01-12 05:44:42 +08:00
|
|
|
int i;
|
|
|
|
|
2007-10-04 15:35:37 +08:00
|
|
|
for_each_sg(sg, s, nents, i) {
|
2006-02-26 11:18:22 +08:00
|
|
|
if (!s->dma_length || !s->length)
|
2006-01-12 05:44:42 +08:00
|
|
|
break;
|
2006-06-26 19:56:37 +08:00
|
|
|
gart_unmap_single(dev, s->dma_address, s->dma_length, dir);
|
2006-01-12 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Fallback for dma_map_sg in case of overflow */
|
|
|
|
static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
|
|
|
|
int nents, int dir)
|
|
|
|
{
|
2007-10-04 15:35:37 +08:00
|
|
|
struct scatterlist *s;
|
2005-04-17 06:20:36 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
#ifdef CONFIG_IOMMU_DEBUG
|
|
|
|
printk(KERN_DEBUG "dma_map_sg overflow\n");
|
|
|
|
#endif
|
|
|
|
|
2007-10-04 15:35:37 +08:00
|
|
|
for_each_sg(sg, s, nents, i) {
|
2007-10-23 02:02:46 +08:00
|
|
|
unsigned long addr = sg_phys(s);
|
2008-01-30 20:30:12 +08:00
|
|
|
|
|
|
|
if (nonforced_iommu(dev, addr, s->length)) {
|
2006-01-12 05:44:42 +08:00
|
|
|
addr = dma_map_area(dev, addr, s->length, dir);
|
2008-01-30 20:30:12 +08:00
|
|
|
if (addr == bad_dma_address) {
|
|
|
|
if (i > 0)
|
2006-01-12 05:44:42 +08:00
|
|
|
gart_unmap_sg(dev, sg, i, dir);
|
2008-01-30 20:30:12 +08:00
|
|
|
nents = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
sg[0].dma_length = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->dma_address = addr;
|
|
|
|
s->dma_length = s->length;
|
|
|
|
}
|
2006-06-26 19:56:40 +08:00
|
|
|
flush_gart();
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return nents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map multiple scatterlist entries continuous into the first. */
|
2007-10-04 15:35:37 +08:00
|
|
|
static int __dma_map_cont(struct scatterlist *start, int nelems,
|
2008-01-30 20:30:12 +08:00
|
|
|
struct scatterlist *sout, unsigned long pages)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long iommu_start = alloc_iommu(pages);
|
2008-01-30 20:30:12 +08:00
|
|
|
unsigned long iommu_page = iommu_start;
|
2007-10-04 15:35:37 +08:00
|
|
|
struct scatterlist *s;
|
2005-04-17 06:20:36 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (iommu_start == -1)
|
|
|
|
return -1;
|
2007-10-04 15:35:37 +08:00
|
|
|
|
|
|
|
for_each_sg(start, s, nelems, i) {
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long pages, addr;
|
|
|
|
unsigned long phys_addr = s->dma_address;
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2007-10-04 15:35:37 +08:00
|
|
|
BUG_ON(s != start && s->offset);
|
|
|
|
if (s == start) {
|
2005-04-17 06:20:36 +08:00
|
|
|
sout->dma_address = iommu_bus_base;
|
|
|
|
sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
|
|
|
|
sout->dma_length = s->length;
|
2008-01-30 20:30:12 +08:00
|
|
|
} else {
|
|
|
|
sout->dma_length += s->length;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
addr = phys_addr;
|
2008-01-30 20:30:12 +08:00
|
|
|
pages = to_pages(s->offset, s->length);
|
|
|
|
while (pages--) {
|
|
|
|
iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
|
2005-04-17 06:20:36 +08:00
|
|
|
SET_LEAK(iommu_page);
|
|
|
|
addr += PAGE_SIZE;
|
|
|
|
iommu_page++;
|
2006-02-13 06:34:59 +08:00
|
|
|
}
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
|
|
|
BUG_ON(iommu_page - iommu_start != pages);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
static inline int
|
|
|
|
dma_map_cont(struct scatterlist *start, int nelems, struct scatterlist *sout,
|
|
|
|
unsigned long pages, int need)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-10-04 15:35:37 +08:00
|
|
|
if (!need) {
|
|
|
|
BUG_ON(nelems != 1);
|
2007-10-25 15:13:32 +08:00
|
|
|
sout->dma_address = start->dma_address;
|
2007-10-04 15:35:37 +08:00
|
|
|
sout->dma_length = start->length;
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
2007-10-04 15:35:37 +08:00
|
|
|
}
|
|
|
|
return __dma_map_cont(start, nelems, sout, pages);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* DMA map all entries in a scatterlist.
|
2008-01-30 20:30:12 +08:00
|
|
|
* Merge chunks that have page aligned sizes into a continuous mapping.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2008-01-30 20:30:12 +08:00
|
|
|
static int
|
|
|
|
gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-10-04 15:35:37 +08:00
|
|
|
struct scatterlist *s, *ps, *start_sg, *sgmap;
|
2008-01-30 20:30:12 +08:00
|
|
|
int need = 0, nextneed, i, out, start;
|
|
|
|
unsigned long pages = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
if (nents == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
dev = &fallback_dev;
|
|
|
|
|
|
|
|
out = 0;
|
|
|
|
start = 0;
|
2007-10-04 15:35:37 +08:00
|
|
|
start_sg = sgmap = sg;
|
|
|
|
ps = NULL; /* shut up gcc */
|
|
|
|
for_each_sg(sg, s, nents, i) {
|
2007-10-23 02:02:46 +08:00
|
|
|
dma_addr_t addr = sg_phys(s);
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
s->dma_address = addr;
|
2008-01-30 20:30:12 +08:00
|
|
|
BUG_ON(s->length == 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
nextneed = need_iommu(dev, addr, s->length);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Handle the previous not yet processed entries */
|
|
|
|
if (i > start) {
|
2008-01-30 20:30:12 +08:00
|
|
|
/*
|
|
|
|
* Can only merge when the last chunk ends on a
|
|
|
|
* page boundary and the new one doesn't have an
|
|
|
|
* offset.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!iommu_merge || !nextneed || !need || s->offset ||
|
2007-10-04 15:35:37 +08:00
|
|
|
(ps->offset + ps->length) % PAGE_SIZE) {
|
|
|
|
if (dma_map_cont(start_sg, i - start, sgmap,
|
|
|
|
pages, need) < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto error;
|
|
|
|
out++;
|
2007-10-04 15:35:37 +08:00
|
|
|
sgmap = sg_next(sgmap);
|
2005-04-17 06:20:36 +08:00
|
|
|
pages = 0;
|
2007-10-04 15:35:37 +08:00
|
|
|
start = i;
|
|
|
|
start_sg = s;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
need = nextneed;
|
|
|
|
pages += to_pages(s->offset, s->length);
|
2007-10-04 15:35:37 +08:00
|
|
|
ps = s;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-10-04 15:35:37 +08:00
|
|
|
if (dma_map_cont(start_sg, i - start, sgmap, pages, need) < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto error;
|
|
|
|
out++;
|
2006-06-26 19:56:40 +08:00
|
|
|
flush_gart();
|
2007-10-04 15:35:37 +08:00
|
|
|
if (out < nents) {
|
|
|
|
sgmap = sg_next(sgmap);
|
|
|
|
sgmap->dma_length = 0;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
return out;
|
|
|
|
|
|
|
|
error:
|
2006-06-26 19:56:40 +08:00
|
|
|
flush_gart();
|
2007-10-26 19:56:24 +08:00
|
|
|
gart_unmap_sg(dev, sg, out, dir);
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2006-02-04 04:51:32 +08:00
|
|
|
/* When it was forced or merged try again in a dumb way */
|
|
|
|
if (force_iommu || iommu_merge) {
|
|
|
|
out = dma_map_sg_nonforce(dev, sg, nents, dir);
|
|
|
|
if (out > 0)
|
|
|
|
return out;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
if (panic_on_overflow)
|
|
|
|
panic("dma_map_sg: overflow on %lu pages\n", pages);
|
2008-01-30 20:30:12 +08:00
|
|
|
|
2006-01-12 05:44:42 +08:00
|
|
|
iommu_full(dev, pages << PAGE_SHIFT, dir);
|
2007-10-04 15:35:37 +08:00
|
|
|
for_each_sg(sg, s, nents, i)
|
|
|
|
s->dma_address = bad_dma_address;
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-12 05:44:42 +08:00
|
|
|
static int no_agp;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
|
2008-01-30 20:30:12 +08:00
|
|
|
{
|
|
|
|
unsigned long a;
|
|
|
|
|
|
|
|
if (!iommu_size) {
|
|
|
|
iommu_size = aper_size;
|
|
|
|
if (!no_agp)
|
|
|
|
iommu_size /= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = aper + iommu_size;
|
2005-04-17 06:20:36 +08:00
|
|
|
iommu_size -= round_up(a, LARGE_PAGE_SIZE) - a;
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
if (iommu_size < 64*1024*1024) {
|
2005-04-17 06:20:36 +08:00
|
|
|
printk(KERN_WARNING
|
2008-01-30 20:30:12 +08:00
|
|
|
"PCI-DMA: Warning: Small IOMMU %luMB."
|
|
|
|
" Consider increasing the AGP aperture in BIOS\n",
|
|
|
|
iommu_size >> 20);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return iommu_size;
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
|
|
|
|
{
|
|
|
|
unsigned aper_size = 0, aper_base_32, aper_order;
|
2005-04-17 06:20:36 +08:00
|
|
|
u64 aper_base;
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
pci_read_config_dword(dev, 0x94, &aper_base_32);
|
2005-04-17 06:20:36 +08:00
|
|
|
pci_read_config_dword(dev, 0x90, &aper_order);
|
2008-01-30 20:30:12 +08:00
|
|
|
aper_order = (aper_order >> 1) & 7;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
aper_base = aper_base_32 & 0x7fff;
|
2005-04-17 06:20:36 +08:00
|
|
|
aper_base <<= 25;
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
aper_size = (32 * 1024 * 1024) << aper_order;
|
|
|
|
if (aper_base + aper_size > 0x100000000UL || !aper_size)
|
2005-04-17 06:20:36 +08:00
|
|
|
aper_base = 0;
|
|
|
|
|
|
|
|
*size = aper_size;
|
|
|
|
return aper_base;
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Private Northbridge GATT initialization in case we cannot use the
|
2008-01-30 20:30:12 +08:00
|
|
|
* AGP driver for some reason.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
static __init int init_k8_gatt(struct agp_kern_info *info)
|
2008-01-30 20:30:12 +08:00
|
|
|
{
|
|
|
|
unsigned aper_size, gatt_size, new_aper_size;
|
|
|
|
unsigned aper_base, new_aper_base;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct pci_dev *dev;
|
|
|
|
void *gatt;
|
2006-06-26 19:56:40 +08:00
|
|
|
int i;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
printk(KERN_INFO "PCI-DMA: Disabling AGP.\n");
|
|
|
|
aper_size = aper_base = info->aper_size = 0;
|
2006-06-26 19:56:40 +08:00
|
|
|
dev = NULL;
|
|
|
|
for (i = 0; i < num_k8_northbridges; i++) {
|
|
|
|
dev = k8_northbridges[i];
|
2008-01-30 20:30:12 +08:00
|
|
|
new_aper_base = read_aperture(dev, &new_aper_size);
|
|
|
|
if (!new_aper_base)
|
|
|
|
goto nommu;
|
|
|
|
|
|
|
|
if (!aper_base) {
|
2005-04-17 06:20:36 +08:00
|
|
|
aper_size = new_aper_size;
|
|
|
|
aper_base = new_aper_base;
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
|
|
|
if (aper_size != new_aper_size || aper_base != new_aper_base)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto nommu;
|
|
|
|
}
|
|
|
|
if (!aper_base)
|
2008-01-30 20:30:12 +08:00
|
|
|
goto nommu;
|
2005-04-17 06:20:36 +08:00
|
|
|
info->aper_base = aper_base;
|
2008-01-30 20:30:12 +08:00
|
|
|
info->aper_size = aper_size >> 20;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
|
|
|
|
gatt = (void *)__get_free_pages(GFP_KERNEL, get_order(gatt_size));
|
|
|
|
if (!gatt)
|
2007-04-24 19:05:36 +08:00
|
|
|
panic("Cannot allocate GATT table");
|
2008-01-30 20:30:12 +08:00
|
|
|
if (change_page_attr_addr((unsigned long)gatt, gatt_size >> PAGE_SHIFT,
|
|
|
|
PAGE_KERNEL_NOCACHE))
|
2007-04-24 19:05:36 +08:00
|
|
|
panic("Could not set GART PTEs to uncacheable pages");
|
|
|
|
global_flush_tlb();
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
memset(gatt, 0, gatt_size);
|
2005-04-17 06:20:36 +08:00
|
|
|
agp_gatt_table = gatt;
|
2006-06-26 19:56:40 +08:00
|
|
|
|
|
|
|
for (i = 0; i < num_k8_northbridges; i++) {
|
2008-01-30 20:30:12 +08:00
|
|
|
u32 gatt_reg;
|
|
|
|
u32 ctl;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-06-26 19:56:40 +08:00
|
|
|
dev = k8_northbridges[i];
|
2008-01-30 20:30:12 +08:00
|
|
|
gatt_reg = __pa(gatt) >> 12;
|
|
|
|
gatt_reg <<= 4;
|
2005-04-17 06:20:36 +08:00
|
|
|
pci_write_config_dword(dev, 0x98, gatt_reg);
|
2008-01-30 20:30:12 +08:00
|
|
|
pci_read_config_dword(dev, 0x90, &ctl);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
ctl |= 1;
|
|
|
|
ctl &= ~((1<<4) | (1<<5));
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
pci_write_config_dword(dev, 0x90, ctl);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-06-26 19:56:40 +08:00
|
|
|
flush_gart();
|
2008-01-30 20:30:12 +08:00
|
|
|
|
|
|
|
printk(KERN_INFO "PCI-DMA: aperture base @ %x size %u KB\n",
|
|
|
|
aper_base, aper_size>>10);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
nommu:
|
2008-01-30 20:30:12 +08:00
|
|
|
/* Should not happen anymore */
|
2005-04-17 06:20:36 +08:00
|
|
|
printk(KERN_ERR "PCI-DMA: More than 4GB of RAM and no IOMMU\n"
|
2006-01-12 05:43:27 +08:00
|
|
|
KERN_ERR "PCI-DMA: 32bit PCI IO may malfunction.\n");
|
2008-01-30 20:30:12 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
extern int agp_amd64_init(void);
|
|
|
|
|
2007-05-03 01:27:06 +08:00
|
|
|
static const struct dma_mapping_ops gart_dma_ops = {
|
2008-01-30 20:30:12 +08:00
|
|
|
.mapping_error = NULL,
|
|
|
|
.map_single = gart_map_single,
|
|
|
|
.map_simple = gart_map_simple,
|
|
|
|
.unmap_single = gart_unmap_single,
|
|
|
|
.sync_single_for_cpu = NULL,
|
|
|
|
.sync_single_for_device = NULL,
|
|
|
|
.sync_single_range_for_cpu = NULL,
|
|
|
|
.sync_single_range_for_device = NULL,
|
|
|
|
.sync_sg_for_cpu = NULL,
|
|
|
|
.sync_sg_for_device = NULL,
|
|
|
|
.map_sg = gart_map_sg,
|
|
|
|
.unmap_sg = gart_unmap_sg,
|
2006-01-12 05:44:42 +08:00
|
|
|
};
|
|
|
|
|
2007-07-21 23:11:28 +08:00
|
|
|
void gart_iommu_shutdown(void)
|
|
|
|
{
|
|
|
|
struct pci_dev *dev;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (no_agp && (dma_ops != &gart_dma_ops))
|
|
|
|
return;
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
for (i = 0; i < num_k8_northbridges; i++) {
|
|
|
|
u32 ctl;
|
2007-07-21 23:11:28 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
dev = k8_northbridges[i];
|
|
|
|
pci_read_config_dword(dev, 0x90, &ctl);
|
2007-07-21 23:11:28 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
ctl &= ~1;
|
2007-07-21 23:11:28 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
pci_write_config_dword(dev, 0x90, ctl);
|
|
|
|
}
|
2007-07-21 23:11:28 +08:00
|
|
|
}
|
|
|
|
|
2006-06-26 19:58:11 +08:00
|
|
|
void __init gart_iommu_init(void)
|
2008-01-30 20:30:12 +08:00
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
struct agp_kern_info info;
|
|
|
|
unsigned long iommu_start;
|
2008-01-30 20:30:12 +08:00
|
|
|
unsigned long aper_size;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long scratch;
|
|
|
|
long i;
|
|
|
|
|
2006-06-26 19:56:40 +08:00
|
|
|
if (cache_k8_northbridges() < 0 || num_k8_northbridges == 0) {
|
|
|
|
printk(KERN_INFO "PCI-GART: No AMD northbridge found.\n");
|
2006-06-26 19:58:11 +08:00
|
|
|
return;
|
2006-06-26 19:56:40 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#ifndef CONFIG_AGP_AMD64
|
2008-01-30 20:30:12 +08:00
|
|
|
no_agp = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
#else
|
|
|
|
/* Makefile puts PCI initialization via subsys_initcall first. */
|
|
|
|
/* Add other K8 AGP bridge drivers here */
|
2008-01-30 20:30:12 +08:00
|
|
|
no_agp = no_agp ||
|
|
|
|
(agp_amd64_init() < 0) ||
|
2005-04-17 06:20:36 +08:00
|
|
|
(agp_copy_info(agp_bridge, &info) < 0);
|
2008-01-30 20:30:12 +08:00
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-02-26 11:18:22 +08:00
|
|
|
if (swiotlb)
|
2006-06-26 19:58:11 +08:00
|
|
|
return;
|
2006-02-26 11:18:22 +08:00
|
|
|
|
2006-06-26 19:58:05 +08:00
|
|
|
/* Did we detect a different HW IOMMU? */
|
2007-10-24 18:49:50 +08:00
|
|
|
if (iommu_detected && !gart_iommu_aperture)
|
2006-06-26 19:58:11 +08:00
|
|
|
return;
|
2006-06-26 19:58:05 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (no_iommu ||
|
2006-01-12 05:44:42 +08:00
|
|
|
(!force_iommu && end_pfn <= MAX_DMA32_PFN) ||
|
2007-10-24 18:49:50 +08:00
|
|
|
!gart_iommu_aperture ||
|
2005-04-17 06:20:36 +08:00
|
|
|
(no_agp && init_k8_gatt(&info) < 0)) {
|
2006-02-04 04:51:59 +08:00
|
|
|
if (end_pfn > MAX_DMA32_PFN) {
|
|
|
|
printk(KERN_ERR "WARNING more than 4GB of memory "
|
2006-12-07 09:14:13 +08:00
|
|
|
"but GART IOMMU not available.\n"
|
2006-05-31 04:47:48 +08:00
|
|
|
KERN_ERR "WARNING 32bit PCI may malfunction.\n");
|
2006-02-04 04:51:59 +08:00
|
|
|
}
|
2006-06-26 19:58:11 +08:00
|
|
|
return;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-02-04 04:51:59 +08:00
|
|
|
printk(KERN_INFO "PCI-DMA: using GART IOMMU.\n");
|
2008-01-30 20:30:12 +08:00
|
|
|
aper_size = info.aper_size * 1024 * 1024;
|
|
|
|
iommu_size = check_iommu_size(info.aper_base, aper_size);
|
|
|
|
iommu_pages = iommu_size >> PAGE_SHIFT;
|
|
|
|
|
|
|
|
iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL,
|
|
|
|
get_order(iommu_pages/8));
|
|
|
|
if (!iommu_gart_bitmap)
|
|
|
|
panic("Cannot allocate iommu bitmap\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
memset(iommu_gart_bitmap, 0, iommu_pages/8);
|
|
|
|
|
|
|
|
#ifdef CONFIG_IOMMU_LEAK
|
2008-01-30 20:30:12 +08:00
|
|
|
if (leak_trace) {
|
|
|
|
iommu_leak_tab = (void *)__get_free_pages(GFP_KERNEL,
|
2005-04-17 06:20:36 +08:00
|
|
|
get_order(iommu_pages*sizeof(void *)));
|
2008-01-30 20:30:12 +08:00
|
|
|
if (iommu_leak_tab)
|
|
|
|
memset(iommu_leak_tab, 0, iommu_pages * 8);
|
2005-04-17 06:20:36 +08:00
|
|
|
else
|
2008-01-30 20:30:12 +08:00
|
|
|
printk(KERN_DEBUG
|
|
|
|
"PCI-DMA: Cannot allocate leak trace area\n");
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Out of IOMMU space handling.
|
2008-01-30 20:30:12 +08:00
|
|
|
* Reserve some invalid pages at the beginning of the GART.
|
|
|
|
*/
|
|
|
|
set_bit_string(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
agp_memory_reserved = iommu_size;
|
2005-04-17 06:20:36 +08:00
|
|
|
printk(KERN_INFO
|
|
|
|
"PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
|
2008-01-30 20:30:12 +08:00
|
|
|
iommu_size >> 20);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
iommu_start = aper_size - iommu_size;
|
|
|
|
iommu_bus_base = info.aper_base + iommu_start;
|
2005-04-17 06:20:36 +08:00
|
|
|
bad_dma_address = iommu_bus_base;
|
|
|
|
iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Unmap the IOMMU part of the GART. The alias of the page is
|
|
|
|
* always mapped with cache enabled and there is no full cache
|
|
|
|
* coherency across the GART remapping. The unmapping avoids
|
|
|
|
* automatic prefetches from the CPU allocating cache lines in
|
|
|
|
* there. All CPU accesses are done via the direct mapping to
|
|
|
|
* the backing memory. The GART address is only used by PCI
|
2008-01-30 20:30:12 +08:00
|
|
|
* devices.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
clear_kernel_mapping((unsigned long)__va(iommu_bus_base), iommu_size);
|
|
|
|
|
2008-01-30 20:30:12 +08:00
|
|
|
/*
|
|
|
|
* Try to workaround a bug (thanks to BenH)
|
|
|
|
* Set unmapped entries to a scratch page instead of 0.
|
2005-04-17 06:20:36 +08:00
|
|
|
* Any prefetches that hit unmapped entries won't get an bus abort
|
|
|
|
* then.
|
|
|
|
*/
|
2008-01-30 20:30:12 +08:00
|
|
|
scratch = get_zeroed_page(GFP_KERNEL);
|
|
|
|
if (!scratch)
|
2005-04-17 06:20:36 +08:00
|
|
|
panic("Cannot allocate iommu scratch page");
|
|
|
|
gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
|
2008-01-30 20:30:12 +08:00
|
|
|
for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
|
2005-04-17 06:20:36 +08:00
|
|
|
iommu_gatt_base[i] = gart_unmapped_entry;
|
|
|
|
|
2006-06-26 19:56:40 +08:00
|
|
|
flush_gart();
|
2006-01-12 05:44:42 +08:00
|
|
|
dma_ops = &gart_dma_ops;
|
2008-01-30 20:30:12 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-17 04:07:36 +08:00
|
|
|
void __init gart_parse_options(char *p)
|
2006-01-12 05:44:42 +08:00
|
|
|
{
|
|
|
|
int arg;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#ifdef CONFIG_IOMMU_LEAK
|
2008-01-30 20:30:12 +08:00
|
|
|
if (!strncmp(p, "leak", 4)) {
|
2006-01-12 05:44:42 +08:00
|
|
|
leak_trace = 1;
|
|
|
|
p += 4;
|
|
|
|
if (*p == '=') ++p;
|
|
|
|
if (isdigit(*p) && get_option(&p, &arg))
|
|
|
|
iommu_leak_pages = arg;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
2006-01-12 05:44:42 +08:00
|
|
|
if (isdigit(*p) && get_option(&p, &arg))
|
|
|
|
iommu_size = arg;
|
2008-01-30 20:30:12 +08:00
|
|
|
if (!strncmp(p, "fullflush", 8))
|
2006-01-12 05:44:42 +08:00
|
|
|
iommu_fullflush = 1;
|
2008-01-30 20:30:12 +08:00
|
|
|
if (!strncmp(p, "nofullflush", 11))
|
2006-01-12 05:44:42 +08:00
|
|
|
iommu_fullflush = 0;
|
2008-01-30 20:30:12 +08:00
|
|
|
if (!strncmp(p, "noagp", 5))
|
2006-01-12 05:44:42 +08:00
|
|
|
no_agp = 1;
|
2008-01-30 20:30:12 +08:00
|
|
|
if (!strncmp(p, "noaperture", 10))
|
2006-01-12 05:44:42 +08:00
|
|
|
fix_aperture = 0;
|
|
|
|
/* duplicated from pci-dma.c */
|
2008-01-30 20:30:12 +08:00
|
|
|
if (!strncmp(p, "force", 5))
|
2007-10-24 18:49:50 +08:00
|
|
|
gart_iommu_aperture_allowed = 1;
|
2008-01-30 20:30:12 +08:00
|
|
|
if (!strncmp(p, "allowed", 7))
|
2007-10-24 18:49:50 +08:00
|
|
|
gart_iommu_aperture_allowed = 1;
|
2006-01-12 05:44:42 +08:00
|
|
|
if (!strncmp(p, "memaper", 7)) {
|
|
|
|
fallback_aper_force = 1;
|
|
|
|
p += 7;
|
|
|
|
if (*p == '=') {
|
|
|
|
++p;
|
|
|
|
if (get_option(&p, &arg))
|
|
|
|
fallback_aper_order = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|