2008-04-08 09:20:43 -07:00
|
|
|
#include <linux/dma-mapping.h>
|
2009-01-09 07:13:15 -07:00
|
|
|
#include <linux/dma-debug.h>
|
2008-04-08 09:20:51 -07:00
|
|
|
#include <linux/dmar.h>
|
2011-05-26 09:22:53 -07:00
|
|
|
#include <linux/export.h>
|
2008-04-08 09:20:54 -07: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 01:04:11 -07:00
|
|
|
#include <linux/gfp.h>
|
2008-04-08 09:20:53 -07:00
|
|
|
#include <linux/pci.h>
|
2009-08-27 06:29:20 -07:00
|
|
|
#include <linux/kmemleak.h>
|
2008-04-08 09:20:51 -07:00
|
|
|
|
2008-04-08 09:20:54 -07:00
|
|
|
#include <asm/proto.h>
|
|
|
|
#include <asm/dma.h>
|
2008-07-10 18:23:42 -07:00
|
|
|
#include <asm/iommu.h>
|
2008-11-27 10:39:15 -07:00
|
|
|
#include <asm/gart.h>
|
2008-04-08 09:20:51 -07:00
|
|
|
#include <asm/calgary.h>
|
2009-11-10 06:37:58 -07:00
|
|
|
#include <asm/x86_init.h>
|
2010-08-26 10:58:05 -07:00
|
|
|
#include <asm/iommu_table.h>
|
2008-04-08 09:20:43 -07:00
|
|
|
|
2008-10-23 16:51:00 -07:00
|
|
|
static int forbid_dac __read_mostly;
|
|
|
|
|
2009-11-14 04:46:36 -07:00
|
|
|
struct dma_map_ops *dma_ops = &nommu_dma_ops;
|
2008-04-08 09:20:50 -07:00
|
|
|
EXPORT_SYMBOL(dma_ops);
|
|
|
|
|
2008-04-27 16:15:58 -07:00
|
|
|
static int iommu_sac_force __read_mostly;
|
2008-04-08 09:20:55 -07:00
|
|
|
|
2008-04-08 09:20:52 -07: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 09:20:56 -07: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 07:00:35 -07:00
|
|
|
/*
|
|
|
|
* This variable becomes 1 if iommu=pt is passed on the kernel command line.
|
2009-10-03 10:45:02 -07:00
|
|
|
* If this variable is 1, IOMMU implementations do no DMA translation for
|
2009-09-01 07:00:35 -07:00
|
|
|
* devices and allow every device to access to whole physical memory. This is
|
2010-01-14 23:16:16 -07:00
|
|
|
* useful if a user wants to use an IOMMU only for KVM device assignment to
|
2009-09-01 07:00:35 -07:00
|
|
|
* guests and not for driver dma translation.
|
|
|
|
*/
|
|
|
|
int iommu_pass_through __read_mostly;
|
2009-04-30 17:57:11 -07:00
|
|
|
|
2010-08-26 10:58:05 -07:00
|
|
|
extern struct iommu_table_entry __iommu_table[], __iommu_table_end[];
|
|
|
|
|
2009-11-08 04:12:14 -07:00
|
|
|
/* Dummy device used for NULL arguments (normally ISA). */
|
2008-08-19 07:32:45 -07:00
|
|
|
struct device x86_dma_fallback_dev = {
|
2008-10-29 18:17:49 -07:00
|
|
|
.init_name = "fallback device",
|
2009-11-08 04:12:14 -07:00
|
|
|
.coherent_dma_mask = ISA_DMA_BIT_MASK,
|
2008-08-19 07:32:45 -07:00
|
|
|
.dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
|
2008-04-09 09:18:10 -07:00
|
|
|
};
|
2008-08-19 07:32:45 -07:00
|
|
|
EXPORT_SYMBOL(x86_dma_fallback_dev);
|
2008-04-09 09:18:10 -07:00
|
|
|
|
2009-01-09 07:13:15 -07:00
|
|
|
/* Number of entries preallocated for DMA-API debugging */
|
|
|
|
#define PREALLOC_DMA_DEBUG_ENTRIES 32768
|
|
|
|
|
2008-04-08 09:20:43 -07: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 09:20:54 -07:00
|
|
|
void __init pci_iommu_alloc(void)
|
|
|
|
{
|
2010-08-26 10:58:05 -07: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 09:20:54 -07:00
|
|
|
|
2010-08-26 10:58:05 -07: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 09:20:54 -07:00
|
|
|
}
|
2008-09-24 04:48:35 -07:00
|
|
|
void *dma_generic_alloc_coherent(struct device *dev, size_t size,
|
2012-03-27 05:28:18 -07:00
|
|
|
dma_addr_t *dma_addr, gfp_t flag,
|
|
|
|
struct dma_attrs *attrs)
|
2008-09-24 04:48:35 -07:00
|
|
|
{
|
|
|
|
unsigned long dma_mask;
|
2012-06-14 05:01:30 -07:00
|
|
|
struct page *page;
|
2011-12-29 05:09:51 -07:00
|
|
|
unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
|
2008-09-24 04:48:35 -07:00
|
|
|
dma_addr_t addr;
|
|
|
|
|
|
|
|
dma_mask = dma_alloc_coherent_mask(dev, flag);
|
|
|
|
|
|
|
|
flag |= __GFP_ZERO;
|
|
|
|
again:
|
2012-06-14 05:01:30 -07:00
|
|
|
page = NULL;
|
2011-12-29 05:09:51 -07:00
|
|
|
if (!(flag & GFP_ATOMIC))
|
|
|
|
page = dma_alloc_from_contiguous(dev, count, get_order(size));
|
|
|
|
if (!page)
|
|
|
|
page = alloc_pages_node(dev_to_node(dev), flag, get_order(size));
|
2008-09-24 04:48:35 -07:00
|
|
|
if (!page)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
addr = page_to_phys(page);
|
2009-07-09 18:04:55 -07:00
|
|
|
if (addr + size > dma_mask) {
|
2008-09-24 04:48:35 -07:00
|
|
|
__free_pages(page, get_order(size));
|
|
|
|
|
2009-04-06 19:01:15 -07:00
|
|
|
if (dma_mask < DMA_BIT_MASK(32) && !(flag & GFP_DMA)) {
|
2008-09-24 04:48:35 -07:00
|
|
|
flag = (flag & ~GFP_DMA32) | GFP_DMA;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dma_addr = addr;
|
|
|
|
return page_address(page);
|
|
|
|
}
|
|
|
|
|
2011-12-29 05:09:51 -07:00
|
|
|
void dma_generic_free_coherent(struct device *dev, size_t size, void *vaddr,
|
|
|
|
dma_addr_t dma_addr, struct dma_attrs *attrs)
|
|
|
|
{
|
|
|
|
unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
|
|
|
|
struct page *page = virt_to_page(vaddr);
|
|
|
|
|
|
|
|
if (!dma_release_from_contiguous(dev, page, count))
|
|
|
|
free_pages((unsigned long)vaddr, get_order(size));
|
|
|
|
}
|
|
|
|
|
2008-04-08 09:20:56 -07:00
|
|
|
/*
|
2011-08-14 17:02:26 -07:00
|
|
|
* See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
|
|
|
|
* parameter documentation.
|
2008-04-08 09:20:56 -07: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 07:41:46 -07:00
|
|
|
forbid_dac = 1;
|
2008-04-08 09:20:56 -07:00
|
|
|
if (!strncmp(p, "usedac", 6)) {
|
|
|
|
forbid_dac = -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_SWIOTLB
|
|
|
|
if (!strncmp(p, "soft", 4))
|
|
|
|
swiotlb = 1;
|
2009-07-01 10:56:16 -07:00
|
|
|
#endif
|
2009-07-30 15:15:18 -07:00
|
|
|
if (!strncmp(p, "pt", 2))
|
2009-04-24 17:30:20 -07:00
|
|
|
iommu_pass_through = 1;
|
2008-04-08 09:20:56 -07: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 09:20:55 -07:00
|
|
|
int dma_supported(struct device *dev, u64 mask)
|
|
|
|
{
|
2009-01-05 07:59:02 -07:00
|
|
|
struct dma_map_ops *ops = get_dma_ops(dev);
|
2008-07-25 19:44:49 -07:00
|
|
|
|
2008-04-08 09:20:55 -07:00
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
if (mask > 0xffffffff && forbid_dac > 0) {
|
2008-05-01 21:02:41 -07:00
|
|
|
dev_info(dev, "PCI: Disallowing DAC for device\n");
|
2008-04-08 09:20:55 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-25 19:44:49 -07:00
|
|
|
if (ops->dma_supported)
|
|
|
|
return ops->dma_supported(dev, mask);
|
2008-04-08 09:20:55 -07: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-06 19:01:18 -07:00
|
|
|
if (mask < DMA_BIT_MASK(24))
|
2008-04-08 09:20:55 -07: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-06 19:01:14 -07:00
|
|
|
if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
|
2008-05-01 21:02:41 -07:00
|
|
|
dev_info(dev, "Force SAC with mask %Lx\n", mask);
|
2008-04-08 09:20:55 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(dma_supported);
|
|
|
|
|
2008-04-08 09:20:51 -07:00
|
|
|
static int __init pci_iommu_init(void)
|
|
|
|
{
|
2010-08-26 10:58:05 -07:00
|
|
|
struct iommu_table_entry *p;
|
2009-01-09 07:13:15 -07:00
|
|
|
dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
|
|
|
|
|
2009-03-16 09:50:28 -07:00
|
|
|
#ifdef CONFIG_PCI
|
|
|
|
dma_debug_add_bus(&pci_bus_type);
|
|
|
|
#endif
|
2009-11-10 03:46:12 -07:00
|
|
|
x86_init.iommu.iommu_init();
|
|
|
|
|
2010-08-26 10:58:05 -07: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 03:46:20 -07:00
|
|
|
|
2008-04-08 09:20:51 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Must execute after PCI subsystem */
|
2009-10-12 04:59:29 -07:00
|
|
|
rootfs_initcall(pci_iommu_init);
|
2008-10-23 16:51:00 -07: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)
|
|
|
|
{
|
2012-02-24 00:46:50 -07:00
|
|
|
if (forbid_dac == 0) {
|
2009-02-24 10:38:22 -07:00
|
|
|
dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
|
2008-10-23 16:51:00 -07:00
|
|
|
forbid_dac = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-02-24 00:46:50 -07:00
|
|
|
DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
|
|
|
|
PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
|
2008-10-23 16:51:00 -07:00
|
|
|
#endif
|