2010-01-14 03:21:02 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009-2010 PetaLogix
|
|
|
|
* Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
|
|
|
|
*
|
|
|
|
* Provide default implementations of the DMA mapping callbacks for
|
|
|
|
* directly mapped busses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/dma-mapping.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>
|
2010-01-14 03:21:02 -07:00
|
|
|
#include <linux/dma-debug.h>
|
|
|
|
#include <asm/bug.h>
|
2010-01-20 06:36:24 -07:00
|
|
|
#include <asm/cacheflush.h>
|
2010-01-14 03:21:02 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic direct DMA implementation
|
|
|
|
*
|
|
|
|
* This implementation supports a per-device offset that can be applied if
|
|
|
|
* the address at which memory is visible to devices is not 0. Platform code
|
|
|
|
* can set archdata.dma_data to an unsigned long holding the offset. By
|
|
|
|
* default the offset is PCI_DRAM_OFFSET.
|
|
|
|
*/
|
2010-02-09 01:25:08 -07:00
|
|
|
static inline void __dma_sync_page(unsigned long paddr, unsigned long offset,
|
2010-01-20 06:36:24 -07:00
|
|
|
size_t size, enum dma_data_direction direction)
|
|
|
|
{
|
|
|
|
switch (direction) {
|
|
|
|
case DMA_TO_DEVICE:
|
2011-02-10 00:37:49 -07:00
|
|
|
case DMA_BIDIRECTIONAL:
|
2010-02-09 01:25:08 -07:00
|
|
|
flush_dcache_range(paddr + offset, paddr + offset + size);
|
2010-01-20 06:36:24 -07:00
|
|
|
break;
|
|
|
|
case DMA_FROM_DEVICE:
|
2010-02-09 01:25:08 -07:00
|
|
|
invalidate_dcache_range(paddr + offset, paddr + offset + size);
|
2010-01-20 06:36:24 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-14 03:21:02 -07:00
|
|
|
static unsigned long get_dma_direct_offset(struct device *dev)
|
|
|
|
{
|
2010-03-23 08:37:02 -06:00
|
|
|
if (likely(dev))
|
2010-01-14 03:21:02 -07:00
|
|
|
return (unsigned long)dev->archdata.dma_data;
|
|
|
|
|
|
|
|
return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */
|
|
|
|
}
|
|
|
|
|
2010-03-11 06:15:48 -07:00
|
|
|
#define NOT_COHERENT_CACHE
|
|
|
|
|
|
|
|
static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
|
2010-01-14 03:21:02 -07:00
|
|
|
dma_addr_t *dma_handle, gfp_t flag)
|
|
|
|
{
|
2010-03-11 06:15:48 -07:00
|
|
|
#ifdef NOT_COHERENT_CACHE
|
|
|
|
return consistent_alloc(flag, size, dma_handle);
|
|
|
|
#else
|
2010-01-14 03:21:02 -07:00
|
|
|
void *ret;
|
|
|
|
struct page *page;
|
|
|
|
int node = dev_to_node(dev);
|
|
|
|
|
|
|
|
/* ignore region specifiers */
|
|
|
|
flag &= ~(__GFP_HIGHMEM);
|
|
|
|
|
|
|
|
page = alloc_pages_node(node, flag, get_order(size));
|
|
|
|
if (page == NULL)
|
|
|
|
return NULL;
|
|
|
|
ret = page_address(page);
|
|
|
|
memset(ret, 0, size);
|
|
|
|
*dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev);
|
|
|
|
|
|
|
|
return ret;
|
2010-03-11 06:15:48 -07:00
|
|
|
#endif
|
2010-01-14 03:21:02 -07:00
|
|
|
}
|
|
|
|
|
2010-03-11 06:15:48 -07:00
|
|
|
static void dma_direct_free_coherent(struct device *dev, size_t size,
|
2010-01-14 03:21:02 -07:00
|
|
|
void *vaddr, dma_addr_t dma_handle)
|
|
|
|
{
|
2010-03-11 06:15:48 -07:00
|
|
|
#ifdef NOT_COHERENT_CACHE
|
2010-04-10 09:34:06 -06:00
|
|
|
consistent_free(size, vaddr);
|
2010-03-11 06:15:48 -07:00
|
|
|
#else
|
2010-01-14 03:21:02 -07:00
|
|
|
free_pages((unsigned long)vaddr, get_order(size));
|
2010-03-11 06:15:48 -07:00
|
|
|
#endif
|
2010-01-14 03:21:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
|
|
|
|
int nents, enum dma_data_direction direction,
|
|
|
|
struct dma_attrs *attrs)
|
|
|
|
{
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int i;
|
|
|
|
|
2010-02-08 04:13:10 -07:00
|
|
|
/* FIXME this part of code is untested */
|
2010-01-14 03:21:02 -07:00
|
|
|
for_each_sg(sgl, sg, nents, i) {
|
|
|
|
sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
|
2010-02-08 04:13:10 -07:00
|
|
|
__dma_sync_page(page_to_phys(sg_page(sg)), sg->offset,
|
|
|
|
sg->length, direction);
|
2010-01-14 03:21:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nents;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
|
|
|
|
int nents, enum dma_data_direction direction,
|
|
|
|
struct dma_attrs *attrs)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dma_direct_dma_supported(struct device *dev, u64 mask)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline dma_addr_t dma_direct_map_page(struct device *dev,
|
|
|
|
struct page *page,
|
|
|
|
unsigned long offset,
|
|
|
|
size_t size,
|
2010-01-20 06:36:24 -07:00
|
|
|
enum dma_data_direction direction,
|
2010-01-14 03:21:02 -07:00
|
|
|
struct dma_attrs *attrs)
|
|
|
|
{
|
2010-02-08 04:13:10 -07:00
|
|
|
__dma_sync_page(page_to_phys(page), offset, size, direction);
|
2010-01-14 03:21:02 -07:00
|
|
|
return page_to_phys(page) + offset + get_dma_direct_offset(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void dma_direct_unmap_page(struct device *dev,
|
|
|
|
dma_addr_t dma_address,
|
|
|
|
size_t size,
|
|
|
|
enum dma_data_direction direction,
|
|
|
|
struct dma_attrs *attrs)
|
|
|
|
{
|
2010-02-08 04:13:10 -07:00
|
|
|
/* There is not necessary to do cache cleanup
|
|
|
|
*
|
|
|
|
* phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
|
|
|
|
* dma_address is physical address
|
|
|
|
*/
|
2010-03-11 06:15:48 -07:00
|
|
|
__dma_sync_page(dma_address, 0 , size, direction);
|
2010-01-14 03:21:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct dma_map_ops dma_direct_ops = {
|
|
|
|
.alloc_coherent = dma_direct_alloc_coherent,
|
|
|
|
.free_coherent = dma_direct_free_coherent,
|
|
|
|
.map_sg = dma_direct_map_sg,
|
|
|
|
.unmap_sg = dma_direct_unmap_sg,
|
|
|
|
.dma_supported = dma_direct_dma_supported,
|
|
|
|
.map_page = dma_direct_map_page,
|
|
|
|
.unmap_page = dma_direct_unmap_page,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL(dma_direct_ops);
|
|
|
|
|
|
|
|
/* Number of entries preallocated for DMA-API debugging */
|
|
|
|
#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
|
|
|
|
|
|
|
|
static int __init dma_init(void)
|
|
|
|
{
|
|
|
|
dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fs_initcall(dma_init);
|