2005-06-22 00:15:02 +00:00
|
|
|
/*
|
2008-04-25 20:22:19 +00:00
|
|
|
* Copyright (C) 2001-2008 Silicon Graphics, Inc. All rights reserved.
|
2005-06-22 00:15:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of version 2 of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* A simple uncached page allocator using the generic allocator. This
|
|
|
|
* allocator first utilizes the spare (spill) pages found in the EFI
|
|
|
|
* memmap and will then start converting cached pages to uncached ones
|
|
|
|
* at a granule at a time. Node awareness is implemented by having a
|
|
|
|
* pool of pages per node.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/efi.h>
|
|
|
|
#include <linux/genalloc.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 08:04:11 +00:00
|
|
|
#include <linux/gfp.h>
|
2005-06-22 00:15:02 +00:00
|
|
|
#include <asm/page.h>
|
|
|
|
#include <asm/pal.h>
|
|
|
|
#include <asm/system.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/atomic.h>
|
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
#include <asm/sn/arch.h>
|
|
|
|
|
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *);
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-28 18:50:09 +00:00
|
|
|
struct uncached_pool {
|
|
|
|
struct gen_pool *pool;
|
|
|
|
struct mutex add_chunk_mutex; /* serialize adding a converted chunk */
|
|
|
|
int nchunks_added; /* #of converted chunks added to pool */
|
|
|
|
atomic_t status; /* smp called function's return status*/
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_CONVERTED_CHUNKS_PER_NODE 2
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-28 18:50:09 +00:00
|
|
|
struct uncached_pool uncached_pools[MAX_NUMNODES];
|
2005-06-22 00:15:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void uncached_ipi_visibility(void *data)
|
|
|
|
{
|
|
|
|
int status;
|
2006-06-28 18:50:09 +00:00
|
|
|
struct uncached_pool *uc_pool = (struct uncached_pool *)data;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
|
|
|
status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
|
|
|
|
if ((status != PAL_VISIBILITY_OK) &&
|
|
|
|
(status != PAL_VISIBILITY_OK_REMOTE_NEEDED))
|
2006-06-28 18:50:09 +00:00
|
|
|
atomic_inc(&uc_pool->status);
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void uncached_ipi_mc_drain(void *data)
|
|
|
|
{
|
|
|
|
int status;
|
2006-06-28 18:50:09 +00:00
|
|
|
struct uncached_pool *uc_pool = (struct uncached_pool *)data;
|
2006-06-23 09:03:21 +00:00
|
|
|
|
2005-06-22 00:15:02 +00:00
|
|
|
status = ia64_pal_mc_drain();
|
2006-06-28 18:50:09 +00:00
|
|
|
if (status != PAL_STATUS_SUCCESS)
|
|
|
|
atomic_inc(&uc_pool->status);
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
/*
|
|
|
|
* Add a new chunk of uncached memory pages to the specified pool.
|
|
|
|
*
|
|
|
|
* @pool: pool to add new chunk of uncached memory to
|
|
|
|
* @nid: node id of node to allocate memory from, or -1
|
|
|
|
*
|
|
|
|
* This is accomplished by first allocating a granule of cached memory pages
|
|
|
|
* and then converting them to uncached memory pages.
|
|
|
|
*/
|
2006-06-28 18:50:09 +00:00
|
|
|
static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid)
|
2005-06-22 00:15:02 +00:00
|
|
|
{
|
|
|
|
struct page *page;
|
2006-06-28 18:50:09 +00:00
|
|
|
int status, i, nchunks_added = uc_pool->nchunks_added;
|
2006-06-23 09:03:21 +00:00
|
|
|
unsigned long c_addr, uc_addr;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-28 18:50:09 +00:00
|
|
|
if (mutex_lock_interruptible(&uc_pool->add_chunk_mutex) != 0)
|
|
|
|
return -1; /* interrupted by a signal */
|
|
|
|
|
|
|
|
if (uc_pool->nchunks_added > nchunks_added) {
|
|
|
|
/* someone added a new chunk while we were waiting */
|
|
|
|
mutex_unlock(&uc_pool->add_chunk_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uc_pool->nchunks_added >= MAX_CONVERTED_CHUNKS_PER_NODE) {
|
|
|
|
mutex_unlock(&uc_pool->add_chunk_mutex);
|
2006-06-23 09:03:21 +00:00
|
|
|
return -1;
|
2006-06-28 18:50:09 +00:00
|
|
|
}
|
2006-06-23 09:03:21 +00:00
|
|
|
|
|
|
|
/* attempt to allocate a granule's worth of cached memory pages */
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2009-06-16 22:31:54 +00:00
|
|
|
page = alloc_pages_exact_node(nid,
|
|
|
|
GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
|
2005-06-22 00:15:02 +00:00
|
|
|
IA64_GRANULE_SHIFT-PAGE_SHIFT);
|
2006-06-28 18:50:09 +00:00
|
|
|
if (!page) {
|
|
|
|
mutex_unlock(&uc_pool->add_chunk_mutex);
|
2006-06-23 09:03:21 +00:00
|
|
|
return -1;
|
2006-06-28 18:50:09 +00:00
|
|
|
}
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
/* convert the memory pages from cached to uncached */
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
c_addr = (unsigned long)page_address(page);
|
|
|
|
uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There's a small race here where it's possible for someone to
|
|
|
|
* access the page through /dev/mem halfway through the conversion
|
|
|
|
* to uncached - not sure it's really worth bothering about
|
|
|
|
*/
|
|
|
|
for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
|
|
|
|
SetPageUncached(&page[i]);
|
|
|
|
|
2007-12-19 20:30:30 +00:00
|
|
|
flush_tlb_kernel_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
|
2005-06-22 00:15:02 +00:00
|
|
|
|
|
|
|
status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
|
2006-06-28 18:50:09 +00:00
|
|
|
if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) {
|
|
|
|
atomic_set(&uc_pool->status, 0);
|
2008-06-06 09:18:06 +00:00
|
|
|
status = smp_call_function(uncached_ipi_visibility, uc_pool, 1);
|
2006-06-28 18:50:09 +00:00
|
|
|
if (status || atomic_read(&uc_pool->status))
|
2006-06-23 09:03:21 +00:00
|
|
|
goto failed;
|
2006-06-28 18:50:09 +00:00
|
|
|
} else if (status != PAL_VISIBILITY_OK)
|
|
|
|
goto failed;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
preempt_disable();
|
|
|
|
|
2005-06-22 00:15:02 +00:00
|
|
|
if (ia64_platform_is("sn2"))
|
2006-06-23 09:03:21 +00:00
|
|
|
sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE);
|
2005-06-22 00:15:02 +00:00
|
|
|
else
|
2006-06-23 09:03:21 +00:00
|
|
|
flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
|
|
|
|
|
|
|
|
/* flush the just introduced uncached translation from the TLB */
|
|
|
|
local_flush_tlb_all();
|
|
|
|
|
|
|
|
preempt_enable();
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-28 18:50:09 +00:00
|
|
|
status = ia64_pal_mc_drain();
|
|
|
|
if (status != PAL_STATUS_SUCCESS)
|
|
|
|
goto failed;
|
|
|
|
atomic_set(&uc_pool->status, 0);
|
2008-06-06 09:18:06 +00:00
|
|
|
status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 1);
|
2006-06-28 18:50:09 +00:00
|
|
|
if (status || atomic_read(&uc_pool->status))
|
2006-06-23 09:03:21 +00:00
|
|
|
goto failed;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
/*
|
|
|
|
* The chunk of memory pages has been converted to uncached so now we
|
|
|
|
* can add it to the pool.
|
|
|
|
*/
|
2006-06-28 18:50:09 +00:00
|
|
|
status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid);
|
2006-06-23 09:03:21 +00:00
|
|
|
if (status)
|
|
|
|
goto failed;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-28 18:50:09 +00:00
|
|
|
uc_pool->nchunks_added++;
|
|
|
|
mutex_unlock(&uc_pool->add_chunk_mutex);
|
2006-06-23 09:03:21 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* failed to convert or add the chunk so give it back to the kernel */
|
|
|
|
failed:
|
|
|
|
for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
|
|
|
|
ClearPageUncached(&page[i]);
|
|
|
|
|
|
|
|
free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT);
|
2006-06-28 18:50:09 +00:00
|
|
|
mutex_unlock(&uc_pool->add_chunk_mutex);
|
2006-06-23 09:03:21 +00:00
|
|
|
return -1;
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uncached_alloc_page
|
|
|
|
*
|
2006-06-23 09:03:21 +00:00
|
|
|
* @starting_nid: node id of node to start with, or -1
|
2008-04-25 20:22:19 +00:00
|
|
|
* @n_pages: number of contiguous pages to allocate
|
2006-06-23 09:03:21 +00:00
|
|
|
*
|
2008-04-25 20:22:19 +00:00
|
|
|
* Allocate the specified number of contiguous uncached pages on the
|
|
|
|
* the requested node. If not enough contiguous uncached pages are available
|
|
|
|
* on the requested node, roundrobin starting with the next higher node.
|
2005-06-22 00:15:02 +00:00
|
|
|
*/
|
2008-04-25 20:22:19 +00:00
|
|
|
unsigned long uncached_alloc_page(int starting_nid, int n_pages)
|
2005-06-22 00:15:02 +00:00
|
|
|
{
|
2006-06-23 09:03:21 +00:00
|
|
|
unsigned long uc_addr;
|
2006-06-28 18:50:09 +00:00
|
|
|
struct uncached_pool *uc_pool;
|
2006-06-23 09:03:21 +00:00
|
|
|
int nid;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
if (unlikely(starting_nid >= MAX_NUMNODES))
|
|
|
|
return 0;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
if (starting_nid < 0)
|
|
|
|
starting_nid = numa_node_id();
|
|
|
|
nid = starting_nid;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
do {
|
2007-10-16 08:25:33 +00:00
|
|
|
if (!node_state(nid, N_HIGH_MEMORY))
|
2006-06-23 09:03:21 +00:00
|
|
|
continue;
|
2006-06-28 18:50:09 +00:00
|
|
|
uc_pool = &uncached_pools[nid];
|
|
|
|
if (uc_pool->pool == NULL)
|
2006-06-23 09:03:21 +00:00
|
|
|
continue;
|
|
|
|
do {
|
2008-04-25 20:22:19 +00:00
|
|
|
uc_addr = gen_pool_alloc(uc_pool->pool,
|
|
|
|
n_pages * PAGE_SIZE);
|
2006-06-23 09:03:21 +00:00
|
|
|
if (uc_addr != 0)
|
|
|
|
return uc_addr;
|
2006-06-28 18:50:09 +00:00
|
|
|
} while (uncached_add_chunk(uc_pool, nid) == 0);
|
2006-06-23 09:03:21 +00:00
|
|
|
|
|
|
|
} while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid);
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
return 0;
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(uncached_alloc_page);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uncached_free_page
|
|
|
|
*
|
2008-04-25 20:22:19 +00:00
|
|
|
* @uc_addr: uncached address of first page to free
|
|
|
|
* @n_pages: number of contiguous pages to free
|
2006-06-23 09:03:21 +00:00
|
|
|
*
|
2008-04-25 20:22:19 +00:00
|
|
|
* Free the specified number of uncached pages.
|
2005-06-22 00:15:02 +00:00
|
|
|
*/
|
2008-04-25 20:22:19 +00:00
|
|
|
void uncached_free_page(unsigned long uc_addr, int n_pages)
|
2005-06-22 00:15:02 +00:00
|
|
|
{
|
2006-06-23 09:03:21 +00:00
|
|
|
int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET);
|
2006-06-28 18:50:09 +00:00
|
|
|
struct gen_pool *pool = uncached_pools[nid].pool;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
if (unlikely(pool == NULL))
|
|
|
|
return;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET)
|
|
|
|
panic("uncached_free_page invalid address %lx\n", uc_addr);
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2008-04-25 20:22:19 +00:00
|
|
|
gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE);
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(uncached_free_page);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uncached_build_memmap,
|
|
|
|
*
|
2006-06-23 09:03:21 +00:00
|
|
|
* @uc_start: uncached starting address of a chunk of uncached memory
|
|
|
|
* @uc_end: uncached ending address of a chunk of uncached memory
|
|
|
|
* @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc())
|
|
|
|
*
|
2005-06-22 00:15:02 +00:00
|
|
|
* Called at boot time to build a map of pages that can be used for
|
|
|
|
* memory special operations.
|
|
|
|
*/
|
2009-05-22 20:49:49 +00:00
|
|
|
static int __init uncached_build_memmap(u64 uc_start, u64 uc_end, void *arg)
|
2005-06-22 00:15:02 +00:00
|
|
|
{
|
2006-06-23 09:03:21 +00:00
|
|
|
int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET);
|
2006-06-28 18:50:09 +00:00
|
|
|
struct gen_pool *pool = uncached_pools[nid].pool;
|
2006-06-23 09:03:21 +00:00
|
|
|
size_t size = uc_end - uc_start;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-01-19 07:46:53 +00:00
|
|
|
touch_softlockup_watchdog();
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
if (pool != NULL) {
|
|
|
|
memset((char *)uc_start, 0, size);
|
|
|
|
(void) gen_pool_add(pool, uc_start, size, nid);
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
static int __init uncached_init(void)
|
|
|
|
{
|
|
|
|
int nid;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2007-10-16 08:25:33 +00:00
|
|
|
for_each_node_state(nid, N_ONLINE) {
|
2006-06-28 18:50:09 +00:00
|
|
|
uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid);
|
|
|
|
mutex_init(&uncached_pools[nid].add_chunk_mutex);
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
efi_memmap_walk_uc(uncached_build_memmap, NULL);
|
2005-06-22 00:15:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__initcall(uncached_init);
|