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