2008-04-08 18:20:43 +02:00
|
|
|
#include <linux/dma-mapping.h>
|
2009-01-09 15:13:15 +01:00
|
|
|
#include <linux/dma-debug.h>
|
2008-04-08 18:20:51 +02:00
|
|
|
#include <linux/dmar.h>
|
2011-05-26 18:22:53 +02:00
|
|
|
#include <linux/export.h>
|
2008-04-08 18:20:54 +02:00
|
|
|
#include <linux/bootmem.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 09:04:11 +01:00
|
|
|
#include <linux/gfp.h>
|
2008-04-08 18:20:53 +02:00
|
|
|
#include <linux/pci.h>
|
2009-08-27 15:29:20 +02:00
|
|
|
#include <linux/kmemleak.h>
|
2008-04-08 18:20:51 +02:00
|
|
|
|
2008-04-08 18:20:54 +02:00
|
|
|
#include <asm/proto.h>
|
|
|
|
#include <asm/dma.h>
|
2008-07-11 03:23:42 +02:00
|
|
|
#include <asm/iommu.h>
|
2008-11-27 18:39:15 +01:00
|
|
|
#include <asm/gart.h>
|
2008-04-08 18:20:51 +02:00
|
|
|
#include <asm/calgary.h>
|
2009-11-10 14:37:58 +01:00
|
|
|
#include <asm/x86_init.h>
|
2010-08-26 19:58:05 +02:00
|
|
|
#include <asm/iommu_table.h>
|
2008-04-08 18:20:43 +02:00
|
|
|
|
2008-10-24 01:51:00 +02:00
|
|
|
static int forbid_dac __read_mostly;
|
|
|
|
|
2009-11-14 12:46:36 +01:00
|
|
|
struct dma_map_ops *dma_ops = &nommu_dma_ops;
|
2008-04-08 18:20:50 +02:00
|
|
|
EXPORT_SYMBOL(dma_ops);
|
|
|
|
|
2008-04-28 01:15:58 +02:00
|
|
|
static int iommu_sac_force __read_mostly;
|
2008-04-08 18:20:55 +02:00
|
|
|
|
2008-04-08 18:20:52 +02:00
|
|
|
#ifdef CONFIG_IOMMU_DEBUG
|
|
|
|
int panic_on_overflow __read_mostly = 1;
|
|
|
|
int force_iommu __read_mostly = 1;
|
|
|
|
#else
|
|
|
|
int panic_on_overflow __read_mostly = 0;
|
|
|
|
int force_iommu __read_mostly = 0;
|
|
|
|
#endif
|
|
|
|
|
2008-04-08 18:20:56 +02:00
|
|
|
int iommu_merge __read_mostly = 0;
|
|
|
|
|
|
|
|
int no_iommu __read_mostly;
|
|
|
|
/* Set this to 1 if there is a HW IOMMU in the system */
|
|
|
|
int iommu_detected __read_mostly = 0;
|
|
|
|
|
2009-09-01 16:00:35 +02:00
|
|
|
/*
|
|
|
|
* This variable becomes 1 if iommu=pt is passed on the kernel command line.
|
2009-10-03 19:45:02 +02:00
|
|
|
* If this variable is 1, IOMMU implementations do no DMA translation for
|
2009-09-01 16:00:35 +02:00
|
|
|
* devices and allow every device to access to whole physical memory. This is
|
2010-01-15 07:16:16 +01:00
|
|
|
* useful if a user wants to use an IOMMU only for KVM device assignment to
|
2009-09-01 16:00:35 +02:00
|
|
|
* guests and not for driver dma translation.
|
|
|
|
*/
|
|
|
|
int iommu_pass_through __read_mostly;
|
2009-05-01 02:57:11 +02:00
|
|
|
|
2011-10-21 21:56:24 +02:00
|
|
|
/*
|
|
|
|
* Group multi-function PCI devices into a single device-group for the
|
|
|
|
* iommu_device_group interface. This tells the iommu driver to pretend
|
|
|
|
* it cannot distinguish between functions of a device, exposing only one
|
|
|
|
* group for the device. Useful for disallowing use of individual PCI
|
|
|
|
* functions from userspace drivers.
|
|
|
|
*/
|
|
|
|
int iommu_group_mf __read_mostly;
|
|
|
|
|
2010-08-26 19:58:05 +02:00
|
|
|
extern struct iommu_table_entry __iommu_table[], __iommu_table_end[];
|
|
|
|
|
2009-11-08 12:12:14 +01:00
|
|
|
/* Dummy device used for NULL arguments (normally ISA). */
|
2008-08-19 16:32:45 +02:00
|
|
|
struct device x86_dma_fallback_dev = {
|
2008-10-30 02:17:49 +01:00
|
|
|
.init_name = "fallback device",
|
2009-11-08 12:12:14 +01:00
|
|
|
.coherent_dma_mask = ISA_DMA_BIT_MASK,
|
2008-08-19 16:32:45 +02:00
|
|
|
.dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
|
2008-04-09 18:18:10 +02:00
|
|
|
};
|
2008-08-19 16:32:45 +02:00
|
|
|
EXPORT_SYMBOL(x86_dma_fallback_dev);
|
2008-04-09 18:18:10 +02:00
|
|
|
|
2009-01-09 15:13:15 +01:00
|
|
|
/* Number of entries preallocated for DMA-API debugging */
|
|
|
|
#define PREALLOC_DMA_DEBUG_ENTRIES 32768
|
|
|
|
|
2008-04-08 18:20:43 +02:00
|
|
|
int dma_set_mask(struct device *dev, u64 mask)
|
|
|
|
{
|
|
|
|
if (!dev->dma_mask || !dma_supported(dev, mask))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
*dev->dma_mask = mask;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_set_mask);
|
|
|
|
|
2008-04-08 18:20:54 +02:00
|
|
|
void __init pci_iommu_alloc(void)
|
|
|
|
{
|
2010-08-26 19:58:05 +02:00
|
|
|
struct iommu_table_entry *p;
|
|
|
|
|
|
|
|
sort_iommu_table(__iommu_table, __iommu_table_end);
|
|
|
|
check_iommu_entries(__iommu_table, __iommu_table_end);
|
2008-04-08 18:20:54 +02:00
|
|
|
|
2010-08-26 19:58:05 +02:00
|
|
|
for (p = __iommu_table; p < __iommu_table_end; p++) {
|
|
|
|
if (p && p->detect && p->detect() > 0) {
|
|
|
|
p->flags |= IOMMU_DETECTED;
|
|
|
|
if (p->early_init)
|
|
|
|
p->early_init();
|
|
|
|
if (p->flags & IOMMU_FINISH_IF_DETECTED)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-08 18:20:54 +02:00
|
|
|
}
|
2008-09-24 13:48:35 +02:00
|
|
|
void *dma_generic_alloc_coherent(struct device *dev, size_t size,
|
|
|
|
dma_addr_t *dma_addr, gfp_t flag)
|
|
|
|
{
|
|
|
|
unsigned long dma_mask;
|
|
|
|
struct page *page;
|
|
|
|
dma_addr_t addr;
|
|
|
|
|
|
|
|
dma_mask = dma_alloc_coherent_mask(dev, flag);
|
|
|
|
|
|
|
|
flag |= __GFP_ZERO;
|
|
|
|
again:
|
|
|
|
page = alloc_pages_node(dev_to_node(dev), flag, get_order(size));
|
|
|
|
if (!page)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
addr = page_to_phys(page);
|
2009-07-10 03:04:55 +02:00
|
|
|
if (addr + size > dma_mask) {
|
2008-09-24 13:48:35 +02:00
|
|
|
__free_pages(page, get_order(size));
|
|
|
|
|
2009-04-07 04:01:15 +02:00
|
|
|
if (dma_mask < DMA_BIT_MASK(32) && !(flag & GFP_DMA)) {
|
2008-09-24 13:48:35 +02:00
|
|
|
flag = (flag & ~GFP_DMA32) | GFP_DMA;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dma_addr = addr;
|
|
|
|
return page_address(page);
|
|
|
|
}
|
|
|
|
|
2008-04-08 18:20:56 +02:00
|
|
|
/*
|
2011-08-15 02:02:26 +02:00
|
|
|
* See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
|
|
|
|
* parameter documentation.
|
2008-04-08 18:20:56 +02:00
|
|
|
*/
|
|
|
|
static __init int iommu_setup(char *p)
|
|
|
|
{
|
|
|
|
iommu_merge = 1;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
if (!strncmp(p, "off", 3))
|
|
|
|
no_iommu = 1;
|
|
|
|
/* gart_parse_options has more force support */
|
|
|
|
if (!strncmp(p, "force", 5))
|
|
|
|
force_iommu = 1;
|
|
|
|
if (!strncmp(p, "noforce", 7)) {
|
|
|
|
iommu_merge = 0;
|
|
|
|
force_iommu = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(p, "biomerge", 8)) {
|
|
|
|
iommu_merge = 1;
|
|
|
|
force_iommu = 1;
|
|
|
|
}
|
|
|
|
if (!strncmp(p, "panic", 5))
|
|
|
|
panic_on_overflow = 1;
|
|
|
|
if (!strncmp(p, "nopanic", 7))
|
|
|
|
panic_on_overflow = 0;
|
|
|
|
if (!strncmp(p, "merge", 5)) {
|
|
|
|
iommu_merge = 1;
|
|
|
|
force_iommu = 1;
|
|
|
|
}
|
|
|
|
if (!strncmp(p, "nomerge", 7))
|
|
|
|
iommu_merge = 0;
|
|
|
|
if (!strncmp(p, "forcesac", 8))
|
|
|
|
iommu_sac_force = 1;
|
|
|
|
if (!strncmp(p, "allowdac", 8))
|
|
|
|
forbid_dac = 0;
|
|
|
|
if (!strncmp(p, "nodac", 5))
|
2009-10-26 15:41:46 +01:00
|
|
|
forbid_dac = 1;
|
2008-04-08 18:20:56 +02:00
|
|
|
if (!strncmp(p, "usedac", 6)) {
|
|
|
|
forbid_dac = -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_SWIOTLB
|
|
|
|
if (!strncmp(p, "soft", 4))
|
|
|
|
swiotlb = 1;
|
2009-07-01 19:56:16 +02:00
|
|
|
#endif
|
2009-07-31 00:15:18 +02:00
|
|
|
if (!strncmp(p, "pt", 2))
|
2009-04-25 02:30:20 +02:00
|
|
|
iommu_pass_through = 1;
|
2011-10-21 21:56:24 +02:00
|
|
|
if (!strncmp(p, "group_mf", 8))
|
|
|
|
iommu_group_mf = 1;
|
2008-04-08 18:20:56 +02:00
|
|
|
|
|
|
|
gart_parse_options(p);
|
|
|
|
|
|
|
|
#ifdef CONFIG_CALGARY_IOMMU
|
|
|
|
if (!strncmp(p, "calgary", 7))
|
|
|
|
use_calgary = 1;
|
|
|
|
#endif /* CONFIG_CALGARY_IOMMU */
|
|
|
|
|
|
|
|
p += strcspn(p, ",");
|
|
|
|
if (*p == ',')
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_param("iommu", iommu_setup);
|
|
|
|
|
2008-04-08 18:20:55 +02:00
|
|
|
int dma_supported(struct device *dev, u64 mask)
|
|
|
|
{
|
2009-01-05 15:59:02 +01:00
|
|
|
struct dma_map_ops *ops = get_dma_ops(dev);
|
2008-07-26 04:44:49 +02:00
|
|
|
|
2008-04-08 18:20:55 +02:00
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
if (mask > 0xffffffff && forbid_dac > 0) {
|
2008-05-02 06:02:41 +02:00
|
|
|
dev_info(dev, "PCI: Disallowing DAC for device\n");
|
2008-04-08 18:20:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-26 04:44:49 +02:00
|
|
|
if (ops->dma_supported)
|
|
|
|
return ops->dma_supported(dev, mask);
|
2008-04-08 18:20:55 +02:00
|
|
|
|
|
|
|
/* Copied from i386. Doesn't make much sense, because it will
|
|
|
|
only work for pci_alloc_coherent.
|
|
|
|
The caller just has to use GFP_DMA in this case. */
|
2009-04-07 04:01:18 +02:00
|
|
|
if (mask < DMA_BIT_MASK(24))
|
2008-04-08 18:20:55 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Tell the device to use SAC when IOMMU force is on. This
|
|
|
|
allows the driver to use cheaper accesses in some cases.
|
|
|
|
|
|
|
|
Problem with this is that if we overflow the IOMMU area and
|
|
|
|
return DAC as fallback address the device may not handle it
|
|
|
|
correctly.
|
|
|
|
|
|
|
|
As a special case some controllers have a 39bit address
|
|
|
|
mode that is as efficient as 32bit (aic79xx). Don't force
|
|
|
|
SAC for these. Assume all masks <= 40 bits are of this
|
|
|
|
type. Normally this doesn't make any difference, but gives
|
|
|
|
more gentle handling of IOMMU overflow. */
|
2009-04-07 04:01:14 +02:00
|
|
|
if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
|
2008-05-02 06:02:41 +02:00
|
|
|
dev_info(dev, "Force SAC with mask %Lx\n", mask);
|
2008-04-08 18:20:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_supported);
|
|
|
|
|
2008-04-08 18:20:51 +02:00
|
|
|
static int __init pci_iommu_init(void)
|
|
|
|
{
|
2010-08-26 19:58:05 +02:00
|
|
|
struct iommu_table_entry *p;
|
2009-01-09 15:13:15 +01:00
|
|
|
dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
|
|
|
|
|
2009-03-16 17:50:28 +01:00
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
dma_debug_add_bus(&pci_bus_type);
|
|
|
|
#endif
|
2009-11-10 11:46:12 +01:00
|
|
|
x86_init.iommu.iommu_init();
|
|
|
|
|
2010-08-26 19:58:05 +02:00
|
|
|
for (p = __iommu_table; p < __iommu_table_end; p++) {
|
|
|
|
if (p && (p->flags & IOMMU_DETECTED) && p->late_init)
|
|
|
|
p->late_init();
|
|
|
|
}
|
2009-11-10 11:46:20 +01:00
|
|
|
|
2008-04-08 18:20:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Must execute after PCI subsystem */
|
2009-10-12 13:59:29 +02:00
|
|
|
rootfs_initcall(pci_iommu_init);
|
2008-10-24 01:51:00 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
/* Many VIA bridges seem to corrupt data for DAC. Disable it here */
|
|
|
|
|
|
|
|
static __devinit void via_no_dac(struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
|
2009-02-24 18:38:22 +01:00
|
|
|
dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
|
2008-10-24 01:51:00 +02:00
|
|
|
forbid_dac = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
|
|
|
|
#endif
|