2010-02-22 19:16:08 +08:00
|
|
|
/*
|
|
|
|
* Microblaze support for cache consistent memory.
|
|
|
|
* Copyright (C) 2010 Michal Simek <monstr@monstr.eu>
|
|
|
|
* Copyright (C) 2010 PetaLogix
|
|
|
|
* Copyright (C) 2005 John Williams <jwilliams@itee.uq.edu.au>
|
|
|
|
*
|
|
|
|
* Based on PowerPC version derived from arch/arm/mm/consistent.c
|
|
|
|
* Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
|
|
|
|
* Copyright (C) 2000 Russell King
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2013-02-01 20:10:35 +08:00
|
|
|
#include <linux/export.h>
|
2010-02-22 19:16:08 +08:00
|
|
|
#include <linux/signal.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/mman.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/swap.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/delay.h>
|
2018-10-31 06:09:49 +08:00
|
|
|
#include <linux/memblock.h>
|
2010-02-22 19:16:08 +08:00
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/interrupt.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>
|
2018-07-19 20:54:39 +08:00
|
|
|
#include <linux/dma-noncoherent.h>
|
2010-02-22 19:16:08 +08:00
|
|
|
|
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/hardirq.h>
|
2012-12-27 17:40:38 +08:00
|
|
|
#include <linux/mmu_context.h>
|
2010-02-22 19:16:08 +08:00
|
|
|
#include <asm/mmu.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/cpuinfo.h>
|
2010-04-10 23:34:06 +08:00
|
|
|
#include <asm/tlbflush.h>
|
2010-02-22 19:16:08 +08:00
|
|
|
|
|
|
|
#ifndef CONFIG_MMU
|
|
|
|
/* I have to use dcache values because I can't relate on ram size */
|
2010-04-10 23:34:06 +08:00
|
|
|
# define UNCACHED_SHADOW_MASK (cpuinfo.dcache_high - cpuinfo.dcache_base + 1)
|
|
|
|
#endif
|
2010-02-22 19:16:08 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Consistent memory allocators. Used for DMA devices that want to
|
|
|
|
* share uncached memory with the processor core.
|
|
|
|
* My crufty no-MMU approach is simple. In the HW platform we can optionally
|
|
|
|
* mirror the DDR up above the processor cacheable region. So, memory accessed
|
|
|
|
* in this mirror region will not be cached. It's alloced from the same
|
|
|
|
* pool as normal memory, but the handle we return is shifted up into the
|
|
|
|
* uncached region. This will no doubt cause big problems if memory allocated
|
|
|
|
* here is not also freed properly. -- JW
|
|
|
|
*/
|
2018-07-19 20:54:39 +08:00
|
|
|
void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
|
|
|
|
gfp_t gfp, unsigned long attrs)
|
2010-02-22 19:16:08 +08:00
|
|
|
{
|
2010-04-10 23:34:06 +08:00
|
|
|
unsigned long order, vaddr;
|
|
|
|
void *ret;
|
|
|
|
unsigned int i, err = 0;
|
|
|
|
struct page *page, *end;
|
2010-02-22 19:16:08 +08:00
|
|
|
|
2010-04-10 23:34:06 +08:00
|
|
|
#ifdef CONFIG_MMU
|
2010-02-22 19:16:08 +08:00
|
|
|
phys_addr_t pa;
|
|
|
|
struct vm_struct *area;
|
2010-04-10 23:34:06 +08:00
|
|
|
unsigned long va;
|
|
|
|
#endif
|
2010-02-22 19:16:08 +08:00
|
|
|
|
|
|
|
if (in_interrupt())
|
|
|
|
BUG();
|
|
|
|
|
|
|
|
/* Only allocate page size areas. */
|
|
|
|
size = PAGE_ALIGN(size);
|
|
|
|
order = get_order(size);
|
|
|
|
|
2010-04-10 23:34:06 +08:00
|
|
|
vaddr = __get_free_pages(gfp, order);
|
|
|
|
if (!vaddr)
|
2010-02-22 19:16:08 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we need to ensure that there are no cachelines in use,
|
|
|
|
* or worse dirty in this area.
|
|
|
|
*/
|
2010-04-10 23:34:06 +08:00
|
|
|
flush_dcache_range(virt_to_phys((void *)vaddr),
|
|
|
|
virt_to_phys((void *)vaddr) + size);
|
|
|
|
|
|
|
|
#ifndef CONFIG_MMU
|
|
|
|
ret = (void *)vaddr;
|
|
|
|
/*
|
|
|
|
* Here's the magic! Note if the uncached shadow is not implemented,
|
|
|
|
* it's up to the calling code to also test that condition and make
|
|
|
|
* other arranegments, such as manually flushing the cache and so on.
|
|
|
|
*/
|
|
|
|
# ifdef CONFIG_XILINX_UNCACHED_SHADOW
|
|
|
|
ret = (void *)((unsigned) ret | UNCACHED_SHADOW_MASK);
|
|
|
|
# endif
|
|
|
|
if ((unsigned int)ret > cpuinfo.dcache_base &&
|
|
|
|
(unsigned int)ret < cpuinfo.dcache_high)
|
2012-12-27 17:40:38 +08:00
|
|
|
pr_warn("ERROR: Your cache coherent area is CACHED!!!\n");
|
2010-02-22 19:16:08 +08:00
|
|
|
|
2010-04-10 23:34:06 +08:00
|
|
|
/* dma_handle is same as physical (shadowed) address */
|
|
|
|
*dma_handle = (dma_addr_t)ret;
|
|
|
|
#else
|
2010-02-22 19:16:08 +08:00
|
|
|
/* Allocate some common virtual space to map the new pages. */
|
|
|
|
area = get_vm_area(size, VM_ALLOC);
|
2010-04-10 23:34:06 +08:00
|
|
|
if (!area) {
|
|
|
|
free_pages(vaddr, order);
|
2010-02-22 19:16:08 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
va = (unsigned long) area->addr;
|
|
|
|
ret = (void *)va;
|
|
|
|
|
|
|
|
/* This gives us the real physical address of the first page. */
|
2013-02-07 22:12:24 +08:00
|
|
|
*dma_handle = pa = __virt_to_phys(vaddr);
|
2010-04-10 23:34:06 +08:00
|
|
|
#endif
|
2010-02-22 19:16:08 +08:00
|
|
|
|
|
|
|
/*
|
2010-04-10 23:34:06 +08:00
|
|
|
* free wasted pages. We skip the first page since we know
|
|
|
|
* that it will have count = 1 and won't require freeing.
|
|
|
|
* We also mark the pages in use as reserved so that
|
|
|
|
* remap_page_range works.
|
2010-02-22 19:16:08 +08:00
|
|
|
*/
|
2010-04-10 23:34:06 +08:00
|
|
|
page = virt_to_page(vaddr);
|
|
|
|
end = page + (1 << order);
|
|
|
|
|
|
|
|
split_page(page, order);
|
|
|
|
|
|
|
|
for (i = 0; i < size && err == 0; i += PAGE_SIZE) {
|
|
|
|
#ifdef CONFIG_MMU
|
|
|
|
/* MS: This is the whole magic - use cache inhibit pages */
|
|
|
|
err = map_page(va + i, pa + i, _PAGE_KERNEL | _PAGE_NO_CACHE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SetPageReserved(page);
|
|
|
|
page++;
|
2010-02-22 19:16:08 +08:00
|
|
|
}
|
|
|
|
|
2010-04-10 23:34:06 +08:00
|
|
|
/* Free the otherwise unused pages. */
|
|
|
|
while (page < end) {
|
|
|
|
__free_page(page);
|
|
|
|
page++;
|
|
|
|
}
|
2010-02-22 19:16:08 +08:00
|
|
|
|
|
|
|
if (err) {
|
2010-04-10 23:34:06 +08:00
|
|
|
free_pages(vaddr, order);
|
2010-02-22 19:16:08 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-03 23:07:28 +08:00
|
|
|
#ifdef CONFIG_MMU
|
|
|
|
static pte_t *consistent_virt_to_pte(void *vaddr)
|
|
|
|
{
|
|
|
|
unsigned long addr = (unsigned long)vaddr;
|
|
|
|
|
|
|
|
return pte_offset_kernel(pmd_offset(pgd_offset_k(addr), addr), addr);
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:55:28 +08:00
|
|
|
long arch_dma_coherent_to_pfn(struct device *dev, void *vaddr,
|
|
|
|
dma_addr_t dma_addr)
|
2014-12-03 23:07:28 +08:00
|
|
|
{
|
|
|
|
pte_t *ptep = consistent_virt_to_pte(vaddr);
|
|
|
|
|
|
|
|
if (pte_none(*ptep) || !pte_present(*ptep))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return pte_pfn(*ptep);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-22 19:16:08 +08:00
|
|
|
/*
|
|
|
|
* free page(s) as defined by the above mapping.
|
|
|
|
*/
|
2018-07-19 20:54:39 +08:00
|
|
|
void arch_dma_free(struct device *dev, size_t size, void *vaddr,
|
|
|
|
dma_addr_t dma_addr, unsigned long attrs)
|
2010-02-22 19:16:08 +08:00
|
|
|
{
|
2010-04-10 23:34:06 +08:00
|
|
|
struct page *page;
|
|
|
|
|
2010-02-22 19:16:08 +08:00
|
|
|
if (in_interrupt())
|
|
|
|
BUG();
|
|
|
|
|
2010-04-10 23:34:06 +08:00
|
|
|
size = PAGE_ALIGN(size);
|
|
|
|
|
|
|
|
#ifndef CONFIG_MMU
|
2010-02-22 19:16:08 +08:00
|
|
|
/* Clear SHADOW_MASK bit in address, and free as per usual */
|
2010-04-10 23:34:06 +08:00
|
|
|
# ifdef CONFIG_XILINX_UNCACHED_SHADOW
|
2010-02-22 19:16:08 +08:00
|
|
|
vaddr = (void *)((unsigned)vaddr & ~UNCACHED_SHADOW_MASK);
|
2010-04-10 23:34:06 +08:00
|
|
|
# endif
|
|
|
|
page = virt_to_page(vaddr);
|
|
|
|
|
|
|
|
do {
|
2013-11-13 07:07:13 +08:00
|
|
|
__free_reserved_page(page);
|
2010-04-10 23:34:06 +08:00
|
|
|
page++;
|
|
|
|
} while (size -= PAGE_SIZE);
|
|
|
|
#else
|
|
|
|
do {
|
2014-12-03 23:07:28 +08:00
|
|
|
pte_t *ptep = consistent_virt_to_pte(vaddr);
|
2010-04-10 23:34:06 +08:00
|
|
|
unsigned long pfn;
|
|
|
|
|
|
|
|
if (!pte_none(*ptep) && pte_present(*ptep)) {
|
|
|
|
pfn = pte_pfn(*ptep);
|
|
|
|
pte_clear(&init_mm, (unsigned int)vaddr, ptep);
|
|
|
|
if (pfn_valid(pfn)) {
|
|
|
|
page = pfn_to_page(pfn);
|
2013-11-13 07:07:13 +08:00
|
|
|
__free_reserved_page(page);
|
2010-04-10 23:34:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vaddr += PAGE_SIZE;
|
|
|
|
} while (size -= PAGE_SIZE);
|
|
|
|
|
|
|
|
/* flush tlb */
|
|
|
|
flush_tlb_all();
|
2010-02-22 19:16:08 +08:00
|
|
|
#endif
|
|
|
|
}
|