2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* Dynamic DMA mapping support for AMD Hammer.
|
2008-01-30 05:30:12 -07:00
|
|
|
*
|
2005-04-16 16:20:36 -06: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 05:30:12 -07:00
|
|
|
* with more than 4GB.
|
2005-04-16 16:20:36 -06:00
|
|
|
*
|
2009-01-29 17:28:02 -07:00
|
|
|
* See Documentation/PCI/PCI-DMA-mapping.txt for the interface specification.
|
2008-01-30 05:30:12 -07:00
|
|
|
*
|
2005-04-16 16:20:36 -06:00
|
|
|
* Copyright 2002 Andi Kleen, SuSE Labs.
|
2007-10-17 10:04:37 -06:00
|
|
|
* Subject to the GNU General Public License v2 only.
|
2005-04-16 16:20:36 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/agp_backend.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/mm.h>
|
2009-10-07 07:09:06 -06:00
|
|
|
#include <linux/sched.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/topology.h>
|
|
|
|
#include <linux/interrupt.h>
|
2009-12-15 17:48:28 -07:00
|
|
|
#include <linux/bitmap.h>
|
2007-05-08 01:27:03 -06:00
|
|
|
#include <linux/kdebug.h>
|
2007-10-04 01:35:37 -06:00
|
|
|
#include <linux/scatterlist.h>
|
2008-02-04 23:28:11 -07:00
|
|
|
#include <linux/iommu-helper.h>
|
2011-03-23 15:15:54 -06:00
|
|
|
#include <linux/syscore_ops.h>
|
2008-09-25 04:13:53 -06:00
|
|
|
#include <linux/io.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 02:04:11 -06:00
|
|
|
#include <linux/gfp.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <asm/atomic.h>
|
|
|
|
#include <asm/mtrr.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/proto.h>
|
2008-07-10 19:23:42 -06:00
|
|
|
#include <asm/iommu.h>
|
2007-10-24 04:49:47 -06:00
|
|
|
#include <asm/gart.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <asm/cacheflush.h>
|
2006-01-11 14:44:42 -07:00
|
|
|
#include <asm/swiotlb.h>
|
|
|
|
#include <asm/dma.h>
|
2010-09-17 10:03:43 -06:00
|
|
|
#include <asm/amd_nb.h>
|
2009-10-27 01:34:44 -06:00
|
|
|
#include <asm/x86_init.h>
|
2010-08-26 11:58:03 -06:00
|
|
|
#include <asm/iommu_table.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-10-24 04:49:49 -06:00
|
|
|
static unsigned long iommu_bus_base; /* GART remapping area (physical) */
|
2008-01-30 05:30:12 -07:00
|
|
|
static unsigned long iommu_size; /* size of remapping area bytes */
|
2005-04-16 16:20:36 -06:00
|
|
|
static unsigned long iommu_pages; /* .. and in pages */
|
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
static u32 *iommu_gatt_base; /* Remapping table */
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
static dma_addr_t bad_dma_addr;
|
|
|
|
|
2008-01-30 05:30:12 -07: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).
|
|
|
|
*/
|
2008-12-29 08:08:09 -07:00
|
|
|
static int iommu_fullflush = 1;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
/* Allocation bitmap for the remapping area: */
|
2005-04-16 16:20:36 -06:00
|
|
|
static DEFINE_SPINLOCK(iommu_bitmap_lock);
|
2008-01-30 05:30:12 -07:00
|
|
|
/* Guarded by iommu_bitmap_lock: */
|
|
|
|
static unsigned long *iommu_gart_bitmap;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
static u32 gart_unmapped_entry;
|
2005-04-16 16:20:36 -06: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 05:30:12 -07:00
|
|
|
#define EMERGENCY_PAGES 32 /* = 128KB */
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
#ifdef CONFIG_AGP
|
|
|
|
#define AGPEXTERN extern
|
|
|
|
#else
|
|
|
|
#define AGPEXTERN
|
|
|
|
#endif
|
|
|
|
|
2011-04-18 07:45:46 -06:00
|
|
|
/* GART can only remap to physical addresses < 1TB */
|
|
|
|
#define GART_MAX_PHYS_ADDR (1ULL << 40)
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* 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-09-25 04:13:54 -06:00
|
|
|
static bool need_flush; /* global flush state. set for each gart wrap */
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-08-17 09:36:18 -06:00
|
|
|
static unsigned long alloc_iommu(struct device *dev, int size,
|
|
|
|
unsigned long align_mask)
|
2008-01-30 05:30:12 -07:00
|
|
|
{
|
2005-04-16 16:20:36 -06:00
|
|
|
unsigned long offset, flags;
|
2008-02-04 23:28:11 -07:00
|
|
|
unsigned long boundary_size;
|
|
|
|
unsigned long base_index;
|
|
|
|
|
|
|
|
base_index = ALIGN(iommu_bus_base & dma_get_seg_boundary(dev),
|
|
|
|
PAGE_SIZE) >> PAGE_SHIFT;
|
2009-11-15 05:19:52 -07:00
|
|
|
boundary_size = ALIGN((u64)dma_get_seg_boundary(dev) + 1,
|
2008-02-04 23:28:11 -07:00
|
|
|
PAGE_SIZE) >> PAGE_SHIFT;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
spin_lock_irqsave(&iommu_bitmap_lock, flags);
|
2008-02-04 23:28:11 -07:00
|
|
|
offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, next_bit,
|
2008-08-17 09:36:18 -06:00
|
|
|
size, base_index, boundary_size, align_mask);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (offset == -1) {
|
2008-09-25 04:13:54 -06:00
|
|
|
need_flush = true;
|
2008-02-04 23:28:11 -07:00
|
|
|
offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, 0,
|
2008-08-17 09:36:18 -06:00
|
|
|
size, base_index, boundary_size,
|
|
|
|
align_mask);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2008-01-30 05:30:12 -07:00
|
|
|
if (offset != -1) {
|
|
|
|
next_bit = offset+size;
|
|
|
|
if (next_bit >= iommu_pages) {
|
2005-04-16 16:20:36 -06:00
|
|
|
next_bit = 0;
|
2008-09-25 04:13:54 -06:00
|
|
|
need_flush = true;
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
if (iommu_fullflush)
|
2008-09-25 04:13:54 -06:00
|
|
|
need_flush = true;
|
2008-01-30 05:30:12 -07:00
|
|
|
spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
return offset;
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
static void free_iommu(unsigned long offset, int size)
|
2008-01-30 05:30:12 -07:00
|
|
|
{
|
2005-04-16 16:20:36 -06:00
|
|
|
unsigned long flags;
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
spin_lock_irqsave(&iommu_bitmap_lock, flags);
|
2009-12-15 17:48:28 -07:00
|
|
|
bitmap_clear(iommu_gart_bitmap, offset, size);
|
2008-12-02 12:16:03 -07:00
|
|
|
if (offset >= next_bit)
|
|
|
|
next_bit = offset + size;
|
2005-04-16 16:20:36 -06:00
|
|
|
spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
/*
|
2005-04-16 16:20:36 -06:00
|
|
|
* Use global flush state to avoid races with multiple flushers.
|
|
|
|
*/
|
2006-06-26 05:56:40 -06:00
|
|
|
static void flush_gart(void)
|
2008-01-30 05:30:12 -07:00
|
|
|
{
|
2005-04-16 16:20:36 -06:00
|
|
|
unsigned long flags;
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
spin_lock_irqsave(&iommu_bitmap_lock, flags);
|
2006-06-26 05:56:40 -06:00
|
|
|
if (need_flush) {
|
2010-10-29 09:14:30 -06:00
|
|
|
amd_flush_garts();
|
2008-09-25 04:13:54 -06:00
|
|
|
need_flush = false;
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
#ifdef CONFIG_IOMMU_LEAK
|
|
|
|
/* Debugging aid for drivers that don't free their IOMMU tables */
|
|
|
|
static int leak_trace;
|
2007-10-24 04:49:49 -06:00
|
|
|
static int iommu_leak_pages = 20;
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2007-10-24 04:49:49 -06:00
|
|
|
static void dump_leak(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-01-30 05:30:12 -07:00
|
|
|
static int dump;
|
|
|
|
|
2009-04-13 18:43:19 -06:00
|
|
|
if (dump)
|
2008-01-30 05:30:12 -07:00
|
|
|
return;
|
2005-04-16 16:20:36 -06:00
|
|
|
dump = 1;
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2009-04-13 18:43:19 -06:00
|
|
|
show_stack(NULL, NULL);
|
|
|
|
debug_dma_dump_mappings(NULL);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-01-11 14:44:42 -07:00
|
|
|
static void iommu_full(struct device *dev, size_t size, int dir)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-01-30 05:30:12 -07:00
|
|
|
/*
|
2005-04-16 16:20:36 -06:00
|
|
|
* Ran out of IOMMU space for this operation. This is very bad.
|
|
|
|
* Unfortunately the drivers cannot handle this operation properly.
|
2008-01-30 05:30:12 -07:00
|
|
|
* Return some non mapped prereserved space in the aperture and
|
2005-04-16 16:20:36 -06: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 05:30:12 -07:00
|
|
|
* memory corruption will occur or random memory will be DMAed
|
2005-04-16 16:20:36 -06:00
|
|
|
* out. Hopefully no network devices use single mappings that big.
|
2008-01-30 05:30:12 -07:00
|
|
|
*/
|
|
|
|
|
2008-05-01 22:02:41 -06:00
|
|
|
dev_err(dev, "PCI-DMA: Out of IOMMU space for %lu bytes\n", size);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-01-11 14:44:42 -07:00
|
|
|
if (size > PAGE_SIZE*EMERGENCY_PAGES) {
|
2005-04-16 16:20:36 -06:00
|
|
|
if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
|
|
|
|
panic("PCI-DMA: Memory would be corrupted\n");
|
2008-01-30 05:30:12 -07:00
|
|
|
if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
|
|
|
|
panic(KERN_ERR
|
|
|
|
"PCI-DMA: Random memory would be DMAed\n");
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
#ifdef CONFIG_IOMMU_LEAK
|
2008-01-30 05:30:12 -07:00
|
|
|
dump_leak();
|
2005-04-16 16:20:36 -06:00
|
|
|
#endif
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
static inline int
|
|
|
|
need_iommu(struct device *dev, unsigned long addr, size_t size)
|
|
|
|
{
|
2009-07-09 19:04:55 -06:00
|
|
|
return force_iommu || !dma_capable(dev, addr, size);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
static inline int
|
|
|
|
nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
|
|
|
|
{
|
2009-07-09 19:04:55 -06:00
|
|
|
return !dma_capable(dev, addr, size);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Map a single continuous physical area into the IOMMU.
|
|
|
|
* Caller needs to check if the iommu is needed and flush.
|
|
|
|
*/
|
2006-01-11 14:44:42 -07:00
|
|
|
static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
|
2008-08-17 09:36:18 -06:00
|
|
|
size_t size, int dir, unsigned long align_mask)
|
2008-01-30 05:30:12 -07:00
|
|
|
{
|
2008-10-15 23:02:11 -06:00
|
|
|
unsigned long npages = iommu_num_pages(phys_mem, size, PAGE_SIZE);
|
2011-04-18 07:45:46 -06:00
|
|
|
unsigned long iommu_page;
|
2005-04-16 16:20:36 -06:00
|
|
|
int i;
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2011-04-18 07:45:46 -06:00
|
|
|
if (unlikely(phys_mem + size > GART_MAX_PHYS_ADDR))
|
|
|
|
return bad_dma_addr;
|
|
|
|
|
|
|
|
iommu_page = alloc_iommu(dev, npages, align_mask);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (iommu_page == -1) {
|
|
|
|
if (!nonforced_iommu(dev, phys_mem, size))
|
2008-01-30 05:30:12 -07:00
|
|
|
return phys_mem;
|
2005-04-16 16:20:36 -06:00
|
|
|
if (panic_on_overflow)
|
|
|
|
panic("dma_map_area overflow %lu bytes\n", size);
|
2006-01-11 14:44:42 -07:00
|
|
|
iommu_full(dev, size, dir);
|
2009-11-15 05:19:52 -07:00
|
|
|
return bad_dma_addr;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < npages; i++) {
|
|
|
|
iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
|
|
|
|
phys_mem += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map a single area into the IOMMU */
|
2009-01-05 07:47:23 -07:00
|
|
|
static dma_addr_t gart_map_page(struct device *dev, struct page *page,
|
|
|
|
unsigned long offset, size_t size,
|
|
|
|
enum dma_data_direction dir,
|
|
|
|
struct dma_attrs *attrs)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-04-19 11:19:56 -06:00
|
|
|
unsigned long bus;
|
2009-01-05 07:47:23 -07:00
|
|
|
phys_addr_t paddr = page_to_phys(page) + offset;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (!dev)
|
2008-08-19 08:32:45 -06:00
|
|
|
dev = &x86_dma_fallback_dev;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-04-19 11:19:56 -06:00
|
|
|
if (!need_iommu(dev, paddr, size))
|
|
|
|
return paddr;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-08-17 09:36:18 -06:00
|
|
|
bus = dma_map_area(dev, paddr, size, dir, 0);
|
|
|
|
flush_gart();
|
2008-01-30 05:30:12 -07:00
|
|
|
|
|
|
|
return bus;
|
2006-01-11 14:44:42 -07:00
|
|
|
}
|
|
|
|
|
2006-06-26 05:56:37 -06:00
|
|
|
/*
|
|
|
|
* Free a DMA mapping.
|
|
|
|
*/
|
2009-01-05 07:47:23 -07:00
|
|
|
static void gart_unmap_page(struct device *dev, dma_addr_t dma_addr,
|
|
|
|
size_t size, enum dma_data_direction dir,
|
|
|
|
struct dma_attrs *attrs)
|
2006-06-26 05:56:37 -06: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 05:30:12 -07:00
|
|
|
|
2006-06-26 05:56:37 -06:00
|
|
|
iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
|
2008-10-15 23:02:11 -06:00
|
|
|
npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
|
2006-06-26 05:56:37 -06:00
|
|
|
for (i = 0; i < npages; i++) {
|
|
|
|
iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
|
|
|
|
}
|
|
|
|
free_iommu(iommu_page, npages);
|
|
|
|
}
|
|
|
|
|
2006-01-11 14:44:42 -07:00
|
|
|
/*
|
|
|
|
* Wrapper for pci_unmap_single working with scatterlists.
|
|
|
|
*/
|
2009-01-05 07:59:02 -07:00
|
|
|
static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
|
|
|
|
enum dma_data_direction dir, struct dma_attrs *attrs)
|
2006-01-11 14:44:42 -07:00
|
|
|
{
|
2007-10-04 01:35:37 -06:00
|
|
|
struct scatterlist *s;
|
2006-01-11 14:44:42 -07:00
|
|
|
int i;
|
|
|
|
|
2007-10-04 01:35:37 -06:00
|
|
|
for_each_sg(sg, s, nents, i) {
|
2006-02-25 20:18:22 -07:00
|
|
|
if (!s->dma_length || !s->length)
|
2006-01-11 14:44:42 -07:00
|
|
|
break;
|
2009-01-05 07:47:28 -07:00
|
|
|
gart_unmap_page(dev, s->dma_address, s->dma_length, dir, NULL);
|
2006-01-11 14:44:42 -07:00
|
|
|
}
|
|
|
|
}
|
2005-04-16 16:20:36 -06: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 01:35:37 -06:00
|
|
|
struct scatterlist *s;
|
2005-04-16 16:20:36 -06:00
|
|
|
int i;
|
|
|
|
|
|
|
|
#ifdef CONFIG_IOMMU_DEBUG
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_debug("dma_map_sg overflow\n");
|
2005-04-16 16:20:36 -06:00
|
|
|
#endif
|
|
|
|
|
2007-10-04 01:35:37 -06:00
|
|
|
for_each_sg(sg, s, nents, i) {
|
2007-10-22 12:02:46 -06:00
|
|
|
unsigned long addr = sg_phys(s);
|
2008-01-30 05:30:12 -07:00
|
|
|
|
|
|
|
if (nonforced_iommu(dev, addr, s->length)) {
|
2008-08-17 09:36:18 -06:00
|
|
|
addr = dma_map_area(dev, addr, s->length, dir, 0);
|
2009-11-15 05:19:52 -07:00
|
|
|
if (addr == bad_dma_addr) {
|
2008-01-30 05:30:12 -07:00
|
|
|
if (i > 0)
|
2009-01-05 07:59:02 -07:00
|
|
|
gart_unmap_sg(dev, sg, i, dir, NULL);
|
2008-01-30 05:30:12 -07:00
|
|
|
nents = 0;
|
2005-04-16 16:20:36 -06:00
|
|
|
sg[0].dma_length = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->dma_address = addr;
|
|
|
|
s->dma_length = s->length;
|
|
|
|
}
|
2006-06-26 05:56:40 -06:00
|
|
|
flush_gart();
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
return nents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map multiple scatterlist entries continuous into the first. */
|
2008-02-04 23:28:11 -07:00
|
|
|
static int __dma_map_cont(struct device *dev, struct scatterlist *start,
|
|
|
|
int nelems, struct scatterlist *sout,
|
|
|
|
unsigned long pages)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-08-17 09:36:18 -06:00
|
|
|
unsigned long iommu_start = alloc_iommu(dev, pages, 0);
|
2008-01-30 05:30:12 -07:00
|
|
|
unsigned long iommu_page = iommu_start;
|
2007-10-04 01:35:37 -06:00
|
|
|
struct scatterlist *s;
|
2005-04-16 16:20:36 -06:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (iommu_start == -1)
|
|
|
|
return -1;
|
2007-10-04 01:35:37 -06:00
|
|
|
|
|
|
|
for_each_sg(start, s, nelems, i) {
|
2005-04-16 16:20:36 -06:00
|
|
|
unsigned long pages, addr;
|
|
|
|
unsigned long phys_addr = s->dma_address;
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2007-10-04 01:35:37 -06:00
|
|
|
BUG_ON(s != start && s->offset);
|
|
|
|
if (s == start) {
|
2005-04-16 16:20:36 -06:00
|
|
|
sout->dma_address = iommu_bus_base;
|
|
|
|
sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
|
|
|
|
sout->dma_length = s->length;
|
2008-01-30 05:30:12 -07:00
|
|
|
} else {
|
|
|
|
sout->dma_length += s->length;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
addr = phys_addr;
|
2008-10-15 23:02:11 -06:00
|
|
|
pages = iommu_num_pages(s->offset, s->length, PAGE_SIZE);
|
2008-01-30 05:30:12 -07:00
|
|
|
while (pages--) {
|
|
|
|
iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
|
2005-04-16 16:20:36 -06:00
|
|
|
addr += PAGE_SIZE;
|
|
|
|
iommu_page++;
|
2006-02-12 15:34:59 -07:00
|
|
|
}
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
|
|
|
BUG_ON(iommu_page - iommu_start != pages);
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
static inline int
|
2008-02-04 23:28:11 -07:00
|
|
|
dma_map_cont(struct device *dev, struct scatterlist *start, int nelems,
|
|
|
|
struct scatterlist *sout, unsigned long pages, int need)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-10-04 01:35:37 -06:00
|
|
|
if (!need) {
|
|
|
|
BUG_ON(nelems != 1);
|
2007-10-25 01:13:32 -06:00
|
|
|
sout->dma_address = start->dma_address;
|
2007-10-04 01:35:37 -06:00
|
|
|
sout->dma_length = start->length;
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
2007-10-04 01:35:37 -06:00
|
|
|
}
|
2008-02-04 23:28:11 -07:00
|
|
|
return __dma_map_cont(dev, start, nelems, sout, pages);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* DMA map all entries in a scatterlist.
|
2008-01-30 05:30:12 -07:00
|
|
|
* Merge chunks that have page aligned sizes into a continuous mapping.
|
2005-04-16 16:20:36 -06:00
|
|
|
*/
|
2009-01-05 07:59:02 -07:00
|
|
|
static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,
|
|
|
|
enum dma_data_direction dir, struct dma_attrs *attrs)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-10-04 01:35:37 -06:00
|
|
|
struct scatterlist *s, *ps, *start_sg, *sgmap;
|
2008-01-30 05:30:12 -07:00
|
|
|
int need = 0, nextneed, i, out, start;
|
|
|
|
unsigned long pages = 0;
|
2008-02-04 23:27:56 -07:00
|
|
|
unsigned int seg_size;
|
|
|
|
unsigned int max_seg_size;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
if (nents == 0)
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!dev)
|
2008-08-19 08:32:45 -06:00
|
|
|
dev = &x86_dma_fallback_dev;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
out = 0;
|
|
|
|
start = 0;
|
|
|
|
start_sg = sg;
|
|
|
|
sgmap = sg;
|
|
|
|
seg_size = 0;
|
|
|
|
max_seg_size = dma_get_max_seg_size(dev);
|
|
|
|
ps = NULL; /* shut up gcc */
|
|
|
|
|
2007-10-04 01:35:37 -06:00
|
|
|
for_each_sg(sg, s, nents, i) {
|
2007-10-22 12:02:46 -06:00
|
|
|
dma_addr_t addr = sg_phys(s);
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
s->dma_address = addr;
|
2008-01-30 05:30:12 -07:00
|
|
|
BUG_ON(s->length == 0);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
nextneed = need_iommu(dev, addr, s->length);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* Handle the previous not yet processed entries */
|
|
|
|
if (i > start) {
|
2008-01-30 05:30:12 -07:00
|
|
|
/*
|
|
|
|
* Can only merge when the last chunk ends on a
|
|
|
|
* page boundary and the new one doesn't have an
|
|
|
|
* offset.
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
if (!iommu_merge || !nextneed || !need || s->offset ||
|
2008-02-04 23:27:56 -07:00
|
|
|
(s->length + seg_size > max_seg_size) ||
|
2007-10-04 01:35:37 -06:00
|
|
|
(ps->offset + ps->length) % PAGE_SIZE) {
|
2008-02-04 23:28:11 -07:00
|
|
|
if (dma_map_cont(dev, start_sg, i - start,
|
|
|
|
sgmap, pages, need) < 0)
|
2005-04-16 16:20:36 -06:00
|
|
|
goto error;
|
|
|
|
out++;
|
2009-11-15 05:19:52 -07:00
|
|
|
|
|
|
|
seg_size = 0;
|
|
|
|
sgmap = sg_next(sgmap);
|
|
|
|
pages = 0;
|
|
|
|
start = i;
|
|
|
|
start_sg = s;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-04 23:27:56 -07:00
|
|
|
seg_size += s->length;
|
2005-04-16 16:20:36 -06:00
|
|
|
need = nextneed;
|
2008-10-15 23:02:11 -06:00
|
|
|
pages += iommu_num_pages(s->offset, s->length, PAGE_SIZE);
|
2007-10-04 01:35:37 -06:00
|
|
|
ps = s;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2008-02-04 23:28:11 -07:00
|
|
|
if (dma_map_cont(dev, start_sg, i - start, sgmap, pages, need) < 0)
|
2005-04-16 16:20:36 -06:00
|
|
|
goto error;
|
|
|
|
out++;
|
2006-06-26 05:56:40 -06:00
|
|
|
flush_gart();
|
2007-10-04 01:35:37 -06:00
|
|
|
if (out < nents) {
|
|
|
|
sgmap = sg_next(sgmap);
|
|
|
|
sgmap->dma_length = 0;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
return out;
|
|
|
|
|
|
|
|
error:
|
2006-06-26 05:56:40 -06:00
|
|
|
flush_gart();
|
2009-01-05 07:59:02 -07:00
|
|
|
gart_unmap_sg(dev, sg, out, dir, NULL);
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2006-02-03 13:51:32 -07: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-16 16:20:36 -06:00
|
|
|
if (panic_on_overflow)
|
|
|
|
panic("dma_map_sg: overflow on %lu pages\n", pages);
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2006-01-11 14:44:42 -07:00
|
|
|
iommu_full(dev, pages << PAGE_SHIFT, dir);
|
2007-10-04 01:35:37 -06:00
|
|
|
for_each_sg(sg, s, nents, i)
|
2009-11-15 05:19:52 -07:00
|
|
|
s->dma_address = bad_dma_addr;
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-08-19 08:32:39 -06:00
|
|
|
/* allocate and map a coherent mapping */
|
|
|
|
static void *
|
|
|
|
gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr,
|
|
|
|
gfp_t flag)
|
|
|
|
{
|
2008-09-11 08:08:48 -06:00
|
|
|
dma_addr_t paddr;
|
2008-08-22 01:29:10 -06:00
|
|
|
unsigned long align_mask;
|
2008-09-24 05:48:37 -06:00
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
if (force_iommu && !(flag & GFP_DMA)) {
|
|
|
|
flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
|
|
|
|
page = alloc_pages(flag | __GFP_ZERO, get_order(size));
|
|
|
|
if (!page)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
align_mask = (1UL << get_order(size)) - 1;
|
|
|
|
paddr = dma_map_area(dev, page_to_phys(page), size,
|
|
|
|
DMA_BIDIRECTIONAL, align_mask);
|
|
|
|
|
|
|
|
flush_gart();
|
2009-11-15 05:19:52 -07:00
|
|
|
if (paddr != bad_dma_addr) {
|
2008-09-24 05:48:37 -06:00
|
|
|
*dma_addr = paddr;
|
|
|
|
return page_address(page);
|
|
|
|
}
|
|
|
|
__free_pages(page, get_order(size));
|
|
|
|
} else
|
|
|
|
return dma_generic_alloc_coherent(dev, size, dma_addr, flag);
|
2008-08-19 08:32:39 -06:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-19 08:32:40 -06:00
|
|
|
/* free a coherent mapping */
|
|
|
|
static void
|
|
|
|
gart_free_coherent(struct device *dev, size_t size, void *vaddr,
|
|
|
|
dma_addr_t dma_addr)
|
|
|
|
{
|
2009-01-05 07:47:28 -07:00
|
|
|
gart_unmap_page(dev, dma_addr, size, DMA_BIDIRECTIONAL, NULL);
|
2008-08-19 08:32:40 -06:00
|
|
|
free_pages((unsigned long)vaddr, get_order(size));
|
|
|
|
}
|
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
static int gart_mapping_error(struct device *dev, dma_addr_t dma_addr)
|
|
|
|
{
|
|
|
|
return (dma_addr == bad_dma_addr);
|
|
|
|
}
|
|
|
|
|
2006-01-11 14:44:42 -07:00
|
|
|
static int no_agp;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
|
2008-01-30 05:30:12 -07:00
|
|
|
{
|
|
|
|
unsigned long a;
|
|
|
|
|
|
|
|
if (!iommu_size) {
|
|
|
|
iommu_size = aper_size;
|
|
|
|
if (!no_agp)
|
|
|
|
iommu_size /= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = aper + iommu_size;
|
2008-02-04 08:48:08 -07:00
|
|
|
iommu_size -= round_up(a, PMD_PAGE_SIZE) - a;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
if (iommu_size < 64*1024*1024) {
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_warning(
|
2008-01-30 05:30:12 -07:00
|
|
|
"PCI-DMA: Warning: Small IOMMU %luMB."
|
|
|
|
" Consider increasing the AGP aperture in BIOS\n",
|
|
|
|
iommu_size >> 20);
|
|
|
|
}
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
return iommu_size;
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
|
|
|
|
{
|
|
|
|
unsigned aper_size = 0, aper_base_32, aper_order;
|
2005-04-16 16:20:36 -06:00
|
|
|
u64 aper_base;
|
|
|
|
|
2008-04-15 04:43:57 -06:00
|
|
|
pci_read_config_dword(dev, AMD64_GARTAPERTUREBASE, &aper_base_32);
|
|
|
|
pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &aper_order);
|
2008-01-30 05:30:12 -07:00
|
|
|
aper_order = (aper_order >> 1) & 7;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
aper_base = aper_base_32 & 0x7fff;
|
2005-04-16 16:20:36 -06:00
|
|
|
aper_base <<= 25;
|
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
aper_size = (32 * 1024 * 1024) << aper_order;
|
|
|
|
if (aper_base + aper_size > 0x100000000UL || !aper_size)
|
2005-04-16 16:20:36 -06:00
|
|
|
aper_base = 0;
|
|
|
|
|
|
|
|
*size = aper_size;
|
|
|
|
return aper_base;
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-06-09 16:10:48 -06:00
|
|
|
static void enable_gart_translations(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2010-10-29 09:14:31 -06:00
|
|
|
if (!amd_nb_has_feature(AMD_NB_GART))
|
2010-09-17 10:02:54 -06:00
|
|
|
return;
|
|
|
|
|
2010-10-29 09:14:31 -06:00
|
|
|
for (i = 0; i < amd_nb_num(); i++) {
|
|
|
|
struct pci_dev *dev = node_to_amd_nb(i)->misc;
|
2008-06-09 16:10:48 -06:00
|
|
|
|
|
|
|
enable_gart_translation(dev, __pa(agp_gatt_table));
|
|
|
|
}
|
2010-04-07 04:57:35 -06:00
|
|
|
|
|
|
|
/* Flush the GART-TLB to remove stale entries */
|
2010-10-29 09:14:30 -06:00
|
|
|
amd_flush_garts();
|
2008-06-09 16:10:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If fix_up_north_bridges is set, the north bridges have to be fixed up on
|
|
|
|
* resume in the same way as they are handled in gart_iommu_hole_init().
|
|
|
|
*/
|
|
|
|
static bool fix_up_north_bridges;
|
|
|
|
static u32 aperture_order;
|
|
|
|
static u32 aperture_alloc;
|
|
|
|
|
|
|
|
void set_up_gart_resume(u32 aper_order, u32 aper_alloc)
|
|
|
|
{
|
|
|
|
fix_up_north_bridges = true;
|
|
|
|
aperture_order = aper_order;
|
|
|
|
aperture_alloc = aper_alloc;
|
|
|
|
}
|
|
|
|
|
2011-03-23 15:15:54 -06:00
|
|
|
static void gart_fixup_northbridges(void)
|
2008-05-29 01:30:21 -06:00
|
|
|
{
|
2009-11-15 05:19:52 -07:00
|
|
|
int i;
|
2008-06-09 16:10:48 -06:00
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
if (!fix_up_north_bridges)
|
|
|
|
return;
|
2008-06-09 16:10:48 -06:00
|
|
|
|
2010-10-29 09:14:31 -06:00
|
|
|
if (!amd_nb_has_feature(AMD_NB_GART))
|
2010-09-17 10:02:54 -06:00
|
|
|
return;
|
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_info("PCI-DMA: Restoring GART aperture settings\n");
|
2008-06-09 16:10:48 -06:00
|
|
|
|
2010-10-29 09:14:31 -06:00
|
|
|
for (i = 0; i < amd_nb_num(); i++) {
|
|
|
|
struct pci_dev *dev = node_to_amd_nb(i)->misc;
|
2008-06-09 16:10:48 -06:00
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
/*
|
|
|
|
* Don't enable translations just yet. That is the next
|
|
|
|
* step. Restore the pre-suspend aperture settings.
|
|
|
|
*/
|
2010-09-03 10:39:40 -06:00
|
|
|
gart_set_size_and_enable(dev, aperture_order);
|
2009-11-15 05:19:52 -07:00
|
|
|
pci_write_config_dword(dev, AMD64_GARTAPERTUREBASE, aperture_alloc >> 25);
|
2008-06-09 16:10:48 -06:00
|
|
|
}
|
2009-11-15 05:19:52 -07:00
|
|
|
}
|
|
|
|
|
2011-03-23 15:15:54 -06:00
|
|
|
static void gart_resume(void)
|
2009-11-15 05:19:52 -07:00
|
|
|
{
|
|
|
|
pr_info("PCI-DMA: Resuming GART IOMMU\n");
|
|
|
|
|
2011-03-23 15:15:54 -06:00
|
|
|
gart_fixup_northbridges();
|
2008-06-09 16:10:48 -06:00
|
|
|
|
|
|
|
enable_gart_translations();
|
2008-05-29 01:30:21 -06:00
|
|
|
}
|
|
|
|
|
2011-03-23 15:15:54 -06:00
|
|
|
static struct syscore_ops gart_syscore_ops = {
|
2009-11-15 05:19:52 -07:00
|
|
|
.resume = gart_resume,
|
2008-05-29 01:30:21 -06:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
/*
|
2005-04-16 16:20:36 -06:00
|
|
|
* Private Northbridge GATT initialization in case we cannot use the
|
2008-01-30 05:30:12 -07:00
|
|
|
* AGP driver for some reason.
|
2005-04-16 16:20:36 -06:00
|
|
|
*/
|
2010-10-29 09:14:30 -06:00
|
|
|
static __init int init_amd_gatt(struct agp_kern_info *info)
|
2008-01-30 05:30:12 -07:00
|
|
|
{
|
|
|
|
unsigned aper_size, gatt_size, new_aper_size;
|
|
|
|
unsigned aper_base, new_aper_base;
|
2005-04-16 16:20:36 -06:00
|
|
|
struct pci_dev *dev;
|
|
|
|
void *gatt;
|
2011-03-23 15:15:54 -06:00
|
|
|
int i;
|
2006-06-26 05:56:40 -06:00
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_info("PCI-DMA: Disabling AGP.\n");
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
aper_size = aper_base = info->aper_size = 0;
|
2006-06-26 05:56:40 -06:00
|
|
|
dev = NULL;
|
2010-10-29 09:14:31 -06:00
|
|
|
for (i = 0; i < amd_nb_num(); i++) {
|
|
|
|
dev = node_to_amd_nb(i)->misc;
|
2008-01-30 05:30:12 -07:00
|
|
|
new_aper_base = read_aperture(dev, &new_aper_size);
|
|
|
|
if (!new_aper_base)
|
|
|
|
goto nommu;
|
|
|
|
|
|
|
|
if (!aper_base) {
|
2005-04-16 16:20:36 -06:00
|
|
|
aper_size = new_aper_size;
|
|
|
|
aper_base = new_aper_base;
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
|
|
|
if (aper_size != new_aper_size || aper_base != new_aper_base)
|
2005-04-16 16:20:36 -06:00
|
|
|
goto nommu;
|
|
|
|
}
|
|
|
|
if (!aper_base)
|
2008-01-30 05:30:12 -07:00
|
|
|
goto nommu;
|
2009-11-15 05:19:52 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
info->aper_base = aper_base;
|
2008-01-30 05:30:12 -07:00
|
|
|
info->aper_size = aper_size >> 20;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
|
2008-09-25 04:42:12 -06:00
|
|
|
gatt = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
|
|
|
|
get_order(gatt_size));
|
2008-01-30 05:30:12 -07:00
|
|
|
if (!gatt)
|
2007-04-24 05:05:36 -06:00
|
|
|
panic("Cannot allocate GATT table");
|
2008-01-30 05:34:06 -07:00
|
|
|
if (set_memory_uc((unsigned long)gatt, gatt_size >> PAGE_SHIFT))
|
2007-04-24 05:05:36 -06:00
|
|
|
panic("Could not set GART PTEs to uncacheable pages");
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
agp_gatt_table = gatt;
|
2006-06-26 05:56:40 -06:00
|
|
|
|
2011-03-23 15:15:54 -06:00
|
|
|
register_syscore_ops(&gart_syscore_ops);
|
2008-06-09 16:10:48 -06:00
|
|
|
|
2006-06-26 05:56:40 -06:00
|
|
|
flush_gart();
|
2008-01-30 05:30:12 -07:00
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_info("PCI-DMA: aperture base @ %x size %u KB\n",
|
2008-01-30 05:30:12 -07:00
|
|
|
aper_base, aper_size>>10);
|
2008-07-12 15:30:35 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
nommu:
|
2008-01-30 05:30:12 -07:00
|
|
|
/* Should not happen anymore */
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_warning("PCI-DMA: More than 4GB of RAM and no IOMMU\n"
|
2009-07-06 14:05:40 -06:00
|
|
|
"falling back to iommu=soft.\n");
|
2008-01-30 05:30:12 -07:00
|
|
|
return -1;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2009-01-05 07:59:02 -07:00
|
|
|
static struct dma_map_ops gart_dma_ops = {
|
2008-01-30 05:30:12 -07:00
|
|
|
.map_sg = gart_map_sg,
|
|
|
|
.unmap_sg = gart_unmap_sg,
|
2009-01-05 07:47:23 -07:00
|
|
|
.map_page = gart_map_page,
|
|
|
|
.unmap_page = gart_unmap_page,
|
2008-08-19 08:32:39 -06:00
|
|
|
.alloc_coherent = gart_alloc_coherent,
|
2008-08-19 08:32:40 -06:00
|
|
|
.free_coherent = gart_free_coherent,
|
2009-11-15 05:19:52 -07:00
|
|
|
.mapping_error = gart_mapping_error,
|
2006-01-11 14:44:42 -07:00
|
|
|
};
|
|
|
|
|
2009-10-27 01:34:44 -06:00
|
|
|
static void gart_iommu_shutdown(void)
|
2007-07-21 09:11:28 -06:00
|
|
|
{
|
|
|
|
struct pci_dev *dev;
|
|
|
|
int i;
|
|
|
|
|
2009-12-13 19:52:15 -07:00
|
|
|
/* don't shutdown it if there is AGP installed */
|
|
|
|
if (!no_agp)
|
2007-07-21 09:11:28 -06:00
|
|
|
return;
|
|
|
|
|
2010-10-29 09:14:31 -06:00
|
|
|
if (!amd_nb_has_feature(AMD_NB_GART))
|
2010-09-17 10:02:54 -06:00
|
|
|
return;
|
|
|
|
|
2010-10-29 09:14:31 -06:00
|
|
|
for (i = 0; i < amd_nb_num(); i++) {
|
2008-01-30 05:30:12 -07:00
|
|
|
u32 ctl;
|
2007-07-21 09:11:28 -06:00
|
|
|
|
2010-10-29 09:14:31 -06:00
|
|
|
dev = node_to_amd_nb(i)->misc;
|
2008-04-15 04:43:57 -06:00
|
|
|
pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &ctl);
|
2007-07-21 09:11:28 -06:00
|
|
|
|
2008-04-15 04:43:57 -06:00
|
|
|
ctl &= ~GARTEN;
|
2007-07-21 09:11:28 -06:00
|
|
|
|
2008-04-15 04:43:57 -06:00
|
|
|
pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, ctl);
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2007-07-21 09:11:28 -06:00
|
|
|
}
|
|
|
|
|
2009-11-10 03:46:14 -07:00
|
|
|
int __init gart_iommu_init(void)
|
2008-01-30 05:30:12 -07:00
|
|
|
{
|
2005-04-16 16:20:36 -06:00
|
|
|
struct agp_kern_info info;
|
|
|
|
unsigned long iommu_start;
|
2008-10-04 16:55:12 -06:00
|
|
|
unsigned long aper_base, aper_size;
|
|
|
|
unsigned long start_pfn, end_pfn;
|
2005-04-16 16:20:36 -06:00
|
|
|
unsigned long scratch;
|
|
|
|
long i;
|
|
|
|
|
2010-10-29 09:14:31 -06:00
|
|
|
if (!amd_nb_has_feature(AMD_NB_GART))
|
2009-11-10 03:46:14 -07:00
|
|
|
return 0;
|
2006-06-26 05:56:40 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
#ifndef CONFIG_AGP_AMD64
|
2008-01-30 05:30:12 -07:00
|
|
|
no_agp = 1;
|
2005-04-16 16:20:36 -06:00
|
|
|
#else
|
|
|
|
/* Makefile puts PCI initialization via subsys_initcall first. */
|
2010-10-29 09:14:30 -06:00
|
|
|
/* Add other AMD AGP bridge drivers here */
|
2008-01-30 05:30:12 -07:00
|
|
|
no_agp = no_agp ||
|
|
|
|
(agp_amd64_init() < 0) ||
|
2005-04-16 16:20:36 -06:00
|
|
|
(agp_copy_info(agp_bridge, &info) < 0);
|
2008-01-30 05:30:12 -07:00
|
|
|
#endif
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (no_iommu ||
|
2008-06-24 23:14:09 -06:00
|
|
|
(!force_iommu && max_pfn <= MAX_DMA32_PFN) ||
|
2007-10-24 04:49:50 -06:00
|
|
|
!gart_iommu_aperture ||
|
2010-10-29 09:14:30 -06:00
|
|
|
(no_agp && init_amd_gatt(&info) < 0)) {
|
2008-06-24 23:14:09 -06:00
|
|
|
if (max_pfn > MAX_DMA32_PFN) {
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_warning("More than 4GB of memory but GART IOMMU not available.\n");
|
|
|
|
pr_warning("falling back to iommu=soft.\n");
|
2006-02-03 13:51:59 -07:00
|
|
|
}
|
2009-11-10 03:46:14 -07:00
|
|
|
return 0;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-10-04 16:55:12 -06:00
|
|
|
/* need to map that range */
|
2009-11-15 05:19:52 -07:00
|
|
|
aper_size = info.aper_size << 20;
|
|
|
|
aper_base = info.aper_base;
|
|
|
|
end_pfn = (aper_base>>PAGE_SHIFT) + (aper_size>>PAGE_SHIFT);
|
|
|
|
|
2008-10-04 16:55:12 -06:00
|
|
|
if (end_pfn > max_low_pfn_mapped) {
|
|
|
|
start_pfn = (aper_base>>PAGE_SHIFT);
|
|
|
|
init_memory_mapping(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
|
|
|
|
}
|
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_info("PCI-DMA: using GART IOMMU.\n");
|
2008-01-30 05:30:12 -07:00
|
|
|
iommu_size = check_iommu_size(info.aper_base, aper_size);
|
|
|
|
iommu_pages = iommu_size >> PAGE_SHIFT;
|
|
|
|
|
2008-09-25 04:42:12 -06:00
|
|
|
iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
|
2008-01-30 05:30:12 -07:00
|
|
|
get_order(iommu_pages/8));
|
|
|
|
if (!iommu_gart_bitmap)
|
|
|
|
panic("Cannot allocate iommu bitmap\n");
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
#ifdef CONFIG_IOMMU_LEAK
|
2008-01-30 05:30:12 -07:00
|
|
|
if (leak_trace) {
|
2009-04-13 18:43:19 -06:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = dma_debug_resize_entries(iommu_pages);
|
|
|
|
if (ret)
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_debug("PCI-DMA: Cannot trace all the entries\n");
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
#endif
|
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
/*
|
2005-04-16 16:20:36 -06:00
|
|
|
* Out of IOMMU space handling.
|
2008-01-30 05:30:12 -07:00
|
|
|
* Reserve some invalid pages at the beginning of the GART.
|
|
|
|
*/
|
2009-12-15 17:48:28 -07:00
|
|
|
bitmap_set(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
pr_info("PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
|
2008-01-30 05:30:12 -07:00
|
|
|
iommu_size >> 20);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2009-11-15 05:19:52 -07:00
|
|
|
agp_memory_reserved = iommu_size;
|
|
|
|
iommu_start = aper_size - iommu_size;
|
|
|
|
iommu_bus_base = info.aper_base + iommu_start;
|
|
|
|
bad_dma_addr = iommu_bus_base;
|
|
|
|
iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
/*
|
2005-04-16 16:20:36 -06: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 05:30:12 -07:00
|
|
|
* devices.
|
2005-04-16 16:20:36 -06:00
|
|
|
*/
|
2008-02-04 08:48:08 -07:00
|
|
|
set_memory_np((unsigned long)__va(iommu_bus_base),
|
|
|
|
iommu_size >> PAGE_SHIFT);
|
2008-02-14 15:30:20 -07:00
|
|
|
/*
|
|
|
|
* Tricky. The GART table remaps the physical memory range,
|
|
|
|
* so the CPU wont notice potential aliases and if the memory
|
|
|
|
* is remapped to UC later on, we might surprise the PCI devices
|
|
|
|
* with a stray writeout of a cacheline. So play it sure and
|
|
|
|
* do an explicit, full-scale wbinvd() _after_ having marked all
|
|
|
|
* the pages as Not-Present:
|
|
|
|
*/
|
|
|
|
wbinvd();
|
2009-11-15 05:19:52 -07:00
|
|
|
|
2009-07-05 14:50:52 -06:00
|
|
|
/*
|
|
|
|
* Now all caches are flushed and we can safely enable
|
|
|
|
* GART hardware. Doing it early leaves the possibility
|
|
|
|
* of stale cache entries that can lead to GART PTE
|
|
|
|
* errors.
|
|
|
|
*/
|
|
|
|
enable_gart_translations();
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-01-30 05:30:12 -07:00
|
|
|
/*
|
2008-06-25 16:25:43 -06:00
|
|
|
* Try to workaround a bug (thanks to BenH):
|
2008-01-30 05:30:12 -07:00
|
|
|
* Set unmapped entries to a scratch page instead of 0.
|
2005-04-16 16:20:36 -06:00
|
|
|
* Any prefetches that hit unmapped entries won't get an bus abort
|
2008-06-25 16:25:43 -06:00
|
|
|
* then. (P2P bridge may be prefetching on DMA reads).
|
2005-04-16 16:20:36 -06:00
|
|
|
*/
|
2008-01-30 05:30:12 -07:00
|
|
|
scratch = get_zeroed_page(GFP_KERNEL);
|
|
|
|
if (!scratch)
|
2005-04-16 16:20:36 -06:00
|
|
|
panic("Cannot allocate iommu scratch page");
|
|
|
|
gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
|
2008-01-30 05:30:12 -07:00
|
|
|
for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
|
2005-04-16 16:20:36 -06:00
|
|
|
iommu_gatt_base[i] = gart_unmapped_entry;
|
|
|
|
|
2006-06-26 05:56:40 -06:00
|
|
|
flush_gart();
|
2006-01-11 14:44:42 -07:00
|
|
|
dma_ops = &gart_dma_ops;
|
2009-10-27 01:34:44 -06:00
|
|
|
x86_platform.iommu_shutdown = gart_iommu_shutdown;
|
2009-11-10 03:46:20 -07:00
|
|
|
swiotlb = 0;
|
2009-11-10 03:46:14 -07:00
|
|
|
|
|
|
|
return 0;
|
2008-01-30 05:30:12 -07:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-03-16 14:07:36 -06:00
|
|
|
void __init gart_parse_options(char *p)
|
2006-01-11 14:44:42 -07:00
|
|
|
{
|
|
|
|
int arg;
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
#ifdef CONFIG_IOMMU_LEAK
|
2008-01-30 05:30:12 -07:00
|
|
|
if (!strncmp(p, "leak", 4)) {
|
2006-01-11 14:44:42 -07:00
|
|
|
leak_trace = 1;
|
|
|
|
p += 4;
|
2008-09-25 04:13:53 -06:00
|
|
|
if (*p == '=')
|
|
|
|
++p;
|
2006-01-11 14:44:42 -07:00
|
|
|
if (isdigit(*p) && get_option(&p, &arg))
|
|
|
|
iommu_leak_pages = arg;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
#endif
|
2006-01-11 14:44:42 -07:00
|
|
|
if (isdigit(*p) && get_option(&p, &arg))
|
|
|
|
iommu_size = arg;
|
2009-11-09 18:58:50 -07:00
|
|
|
if (!strncmp(p, "fullflush", 9))
|
2006-01-11 14:44:42 -07:00
|
|
|
iommu_fullflush = 1;
|
2008-01-30 05:30:12 -07:00
|
|
|
if (!strncmp(p, "nofullflush", 11))
|
2006-01-11 14:44:42 -07:00
|
|
|
iommu_fullflush = 0;
|
2008-01-30 05:30:12 -07:00
|
|
|
if (!strncmp(p, "noagp", 5))
|
2006-01-11 14:44:42 -07:00
|
|
|
no_agp = 1;
|
2008-01-30 05:30:12 -07:00
|
|
|
if (!strncmp(p, "noaperture", 10))
|
2006-01-11 14:44:42 -07:00
|
|
|
fix_aperture = 0;
|
|
|
|
/* duplicated from pci-dma.c */
|
2008-01-30 05:30:12 -07:00
|
|
|
if (!strncmp(p, "force", 5))
|
2007-10-24 04:49:50 -06:00
|
|
|
gart_iommu_aperture_allowed = 1;
|
2008-01-30 05:30:12 -07:00
|
|
|
if (!strncmp(p, "allowed", 7))
|
2007-10-24 04:49:50 -06:00
|
|
|
gart_iommu_aperture_allowed = 1;
|
2006-01-11 14:44:42 -07:00
|
|
|
if (!strncmp(p, "memaper", 7)) {
|
|
|
|
fallback_aper_force = 1;
|
|
|
|
p += 7;
|
|
|
|
if (*p == '=') {
|
|
|
|
++p;
|
|
|
|
if (get_option(&p, &arg))
|
|
|
|
fallback_aper_order = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-26 11:58:03 -06:00
|
|
|
IOMMU_INIT_POST(gart_iommu_hole_init);
|