blob: c4696d217ce0bfe5216393cd4ad572194937175d [file] [log] [blame]
Jes Sorensenf14f75b2005-06-21 17:15:02 -07001/*
Dean Nelsone4a064d2008-04-25 15:22:19 -05002 * Copyright (C) 2001-2008 Silicon Graphics, Inc. All rights reserved.
Jes Sorensenf14f75b2005-06-21 17:15:02 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License
6 * as published by the Free Software Foundation.
7 *
8 * A simple uncached page allocator using the generic allocator. This
9 * allocator first utilizes the spare (spill) pages found in the EFI
10 * memmap and will then start converting cached pages to uncached ones
11 * at a granule at a time. Node awareness is implemented by having a
12 * pool of pages per node.
13 */
14
15#include <linux/types.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/errno.h>
20#include <linux/string.h>
Jes Sorensenf14f75b2005-06-21 17:15:02 -070021#include <linux/efi.h>
22#include <linux/genalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Jes Sorensenf14f75b2005-06-21 17:15:02 -070024#include <asm/page.h>
25#include <asm/pal.h>
26#include <asm/system.h>
27#include <asm/pgtable.h>
28#include <asm/atomic.h>
29#include <asm/tlbflush.h>
30#include <asm/sn/arch.h>
31
Jes Sorensenf14f75b2005-06-21 17:15:02 -070032
Dean Nelson929f9722006-06-23 02:03:21 -070033extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *);
Jes Sorensenf14f75b2005-06-21 17:15:02 -070034
Dean Nelsoneca7994f2006-06-28 13:50:09 -050035struct uncached_pool {
36 struct gen_pool *pool;
37 struct mutex add_chunk_mutex; /* serialize adding a converted chunk */
38 int nchunks_added; /* #of converted chunks added to pool */
39 atomic_t status; /* smp called function's return status*/
40};
Jes Sorensenf14f75b2005-06-21 17:15:02 -070041
Dean Nelsoneca7994f2006-06-28 13:50:09 -050042#define MAX_CONVERTED_CHUNKS_PER_NODE 2
43
44struct uncached_pool uncached_pools[MAX_NUMNODES];
Jes Sorensenf14f75b2005-06-21 17:15:02 -070045
46
47static void uncached_ipi_visibility(void *data)
48{
49 int status;
Dean Nelsoneca7994f2006-06-28 13:50:09 -050050 struct uncached_pool *uc_pool = (struct uncached_pool *)data;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070051
52 status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
53 if ((status != PAL_VISIBILITY_OK) &&
54 (status != PAL_VISIBILITY_OK_REMOTE_NEEDED))
Dean Nelsoneca7994f2006-06-28 13:50:09 -050055 atomic_inc(&uc_pool->status);
Jes Sorensenf14f75b2005-06-21 17:15:02 -070056}
57
58
59static void uncached_ipi_mc_drain(void *data)
60{
61 int status;
Dean Nelsoneca7994f2006-06-28 13:50:09 -050062 struct uncached_pool *uc_pool = (struct uncached_pool *)data;
Dean Nelson929f9722006-06-23 02:03:21 -070063
Jes Sorensenf14f75b2005-06-21 17:15:02 -070064 status = ia64_pal_mc_drain();
Dean Nelsoneca7994f2006-06-28 13:50:09 -050065 if (status != PAL_STATUS_SUCCESS)
66 atomic_inc(&uc_pool->status);
Jes Sorensenf14f75b2005-06-21 17:15:02 -070067}
68
69
Dean Nelson929f9722006-06-23 02:03:21 -070070/*
71 * Add a new chunk of uncached memory pages to the specified pool.
72 *
73 * @pool: pool to add new chunk of uncached memory to
74 * @nid: node id of node to allocate memory from, or -1
75 *
76 * This is accomplished by first allocating a granule of cached memory pages
77 * and then converting them to uncached memory pages.
78 */
Dean Nelsoneca7994f2006-06-28 13:50:09 -050079static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid)
Jes Sorensenf14f75b2005-06-21 17:15:02 -070080{
81 struct page *page;
Dean Nelsoneca7994f2006-06-28 13:50:09 -050082 int status, i, nchunks_added = uc_pool->nchunks_added;
Dean Nelson929f9722006-06-23 02:03:21 -070083 unsigned long c_addr, uc_addr;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070084
Dean Nelsoneca7994f2006-06-28 13:50:09 -050085 if (mutex_lock_interruptible(&uc_pool->add_chunk_mutex) != 0)
86 return -1; /* interrupted by a signal */
87
88 if (uc_pool->nchunks_added > nchunks_added) {
89 /* someone added a new chunk while we were waiting */
90 mutex_unlock(&uc_pool->add_chunk_mutex);
91 return 0;
92 }
93
94 if (uc_pool->nchunks_added >= MAX_CONVERTED_CHUNKS_PER_NODE) {
95 mutex_unlock(&uc_pool->add_chunk_mutex);
Dean Nelson929f9722006-06-23 02:03:21 -070096 return -1;
Dean Nelsoneca7994f2006-06-28 13:50:09 -050097 }
Jes Sorensenf14f75b2005-06-21 17:15:02 -070098
Dean Nelson929f9722006-06-23 02:03:21 -070099 /* attempt to allocate a granule's worth of cached memory pages */
100
Mel Gorman6484eb32009-06-16 15:31:54 -0700101 page = alloc_pages_exact_node(nid,
102 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700103 IA64_GRANULE_SHIFT-PAGE_SHIFT);
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500104 if (!page) {
105 mutex_unlock(&uc_pool->add_chunk_mutex);
Dean Nelson929f9722006-06-23 02:03:21 -0700106 return -1;
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500107 }
Dean Nelson929f9722006-06-23 02:03:21 -0700108
109 /* convert the memory pages from cached to uncached */
110
111 c_addr = (unsigned long)page_address(page);
112 uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700113
114 /*
115 * There's a small race here where it's possible for someone to
116 * access the page through /dev/mem halfway through the conversion
117 * to uncached - not sure it's really worth bothering about
118 */
119 for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
120 SetPageUncached(&page[i]);
121
Jan Beulich285fbd62007-12-19 12:30:30 -0800122 flush_tlb_kernel_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700123
124 status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500125 if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) {
126 atomic_set(&uc_pool->status, 0);
Jens Axboe8691e5a2008-06-06 11:18:06 +0200127 status = smp_call_function(uncached_ipi_visibility, uc_pool, 1);
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500128 if (status || atomic_read(&uc_pool->status))
Dean Nelson929f9722006-06-23 02:03:21 -0700129 goto failed;
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500130 } else if (status != PAL_VISIBILITY_OK)
131 goto failed;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700132
Dean Nelson929f9722006-06-23 02:03:21 -0700133 preempt_disable();
134
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700135 if (ia64_platform_is("sn2"))
Dean Nelson929f9722006-06-23 02:03:21 -0700136 sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700137 else
Dean Nelson929f9722006-06-23 02:03:21 -0700138 flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
139
140 /* flush the just introduced uncached translation from the TLB */
141 local_flush_tlb_all();
142
143 preempt_enable();
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700144
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500145 status = ia64_pal_mc_drain();
146 if (status != PAL_STATUS_SUCCESS)
147 goto failed;
148 atomic_set(&uc_pool->status, 0);
Jens Axboe8691e5a2008-06-06 11:18:06 +0200149 status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 1);
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500150 if (status || atomic_read(&uc_pool->status))
Dean Nelson929f9722006-06-23 02:03:21 -0700151 goto failed;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700152
Dean Nelson929f9722006-06-23 02:03:21 -0700153 /*
154 * The chunk of memory pages has been converted to uncached so now we
155 * can add it to the pool.
156 */
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500157 status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid);
Dean Nelson929f9722006-06-23 02:03:21 -0700158 if (status)
159 goto failed;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700160
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500161 uc_pool->nchunks_added++;
162 mutex_unlock(&uc_pool->add_chunk_mutex);
Dean Nelson929f9722006-06-23 02:03:21 -0700163 return 0;
164
165 /* failed to convert or add the chunk so give it back to the kernel */
166failed:
167 for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
168 ClearPageUncached(&page[i]);
169
170 free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT);
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500171 mutex_unlock(&uc_pool->add_chunk_mutex);
Dean Nelson929f9722006-06-23 02:03:21 -0700172 return -1;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700173}
174
175
176/*
177 * uncached_alloc_page
178 *
Dean Nelson929f9722006-06-23 02:03:21 -0700179 * @starting_nid: node id of node to start with, or -1
Dean Nelsone4a064d2008-04-25 15:22:19 -0500180 * @n_pages: number of contiguous pages to allocate
Dean Nelson929f9722006-06-23 02:03:21 -0700181 *
Dean Nelsone4a064d2008-04-25 15:22:19 -0500182 * Allocate the specified number of contiguous uncached pages on the
183 * the requested node. If not enough contiguous uncached pages are available
184 * on the requested node, roundrobin starting with the next higher node.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700185 */
Dean Nelsone4a064d2008-04-25 15:22:19 -0500186unsigned long uncached_alloc_page(int starting_nid, int n_pages)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700187{
Dean Nelson929f9722006-06-23 02:03:21 -0700188 unsigned long uc_addr;
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500189 struct uncached_pool *uc_pool;
Dean Nelson929f9722006-06-23 02:03:21 -0700190 int nid;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700191
Dean Nelson929f9722006-06-23 02:03:21 -0700192 if (unlikely(starting_nid >= MAX_NUMNODES))
193 return 0;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700194
Dean Nelson929f9722006-06-23 02:03:21 -0700195 if (starting_nid < 0)
196 starting_nid = numa_node_id();
197 nid = starting_nid;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700198
Dean Nelson929f9722006-06-23 02:03:21 -0700199 do {
Christoph Lameter2dca53a2007-10-16 01:25:33 -0700200 if (!node_state(nid, N_HIGH_MEMORY))
Dean Nelson929f9722006-06-23 02:03:21 -0700201 continue;
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500202 uc_pool = &uncached_pools[nid];
203 if (uc_pool->pool == NULL)
Dean Nelson929f9722006-06-23 02:03:21 -0700204 continue;
205 do {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500206 uc_addr = gen_pool_alloc(uc_pool->pool,
207 n_pages * PAGE_SIZE);
Dean Nelson929f9722006-06-23 02:03:21 -0700208 if (uc_addr != 0)
209 return uc_addr;
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500210 } while (uncached_add_chunk(uc_pool, nid) == 0);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700211
Dean Nelson929f9722006-06-23 02:03:21 -0700212 } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700213
Dean Nelson929f9722006-06-23 02:03:21 -0700214 return 0;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700215}
216EXPORT_SYMBOL(uncached_alloc_page);
217
218
219/*
220 * uncached_free_page
221 *
Dean Nelsone4a064d2008-04-25 15:22:19 -0500222 * @uc_addr: uncached address of first page to free
223 * @n_pages: number of contiguous pages to free
Dean Nelson929f9722006-06-23 02:03:21 -0700224 *
Dean Nelsone4a064d2008-04-25 15:22:19 -0500225 * Free the specified number of uncached pages.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700226 */
Dean Nelsone4a064d2008-04-25 15:22:19 -0500227void uncached_free_page(unsigned long uc_addr, int n_pages)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700228{
Dean Nelson929f9722006-06-23 02:03:21 -0700229 int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET);
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500230 struct gen_pool *pool = uncached_pools[nid].pool;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700231
Dean Nelson929f9722006-06-23 02:03:21 -0700232 if (unlikely(pool == NULL))
233 return;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700234
Dean Nelson929f9722006-06-23 02:03:21 -0700235 if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET)
236 panic("uncached_free_page invalid address %lx\n", uc_addr);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700237
Dean Nelsone4a064d2008-04-25 15:22:19 -0500238 gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700239}
240EXPORT_SYMBOL(uncached_free_page);
241
242
243/*
244 * uncached_build_memmap,
245 *
Dean Nelson929f9722006-06-23 02:03:21 -0700246 * @uc_start: uncached starting address of a chunk of uncached memory
247 * @uc_end: uncached ending address of a chunk of uncached memory
248 * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc())
249 *
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700250 * Called at boot time to build a map of pages that can be used for
251 * memory special operations.
252 */
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700253static int __init uncached_build_memmap(u64 uc_start, u64 uc_end, void *arg)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700254{
Dean Nelson929f9722006-06-23 02:03:21 -0700255 int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET);
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500256 struct gen_pool *pool = uncached_pools[nid].pool;
Dean Nelson929f9722006-06-23 02:03:21 -0700257 size_t size = uc_end - uc_start;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700258
John Hawkes386d1d52006-01-18 23:46:53 -0800259 touch_softlockup_watchdog();
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700260
Dean Nelson929f9722006-06-23 02:03:21 -0700261 if (pool != NULL) {
262 memset((char *)uc_start, 0, size);
263 (void) gen_pool_add(pool, uc_start, size, nid);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700264 }
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700265 return 0;
266}
267
268
Dean Nelson929f9722006-06-23 02:03:21 -0700269static int __init uncached_init(void)
270{
271 int nid;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700272
Christoph Lameter2dca53a2007-10-16 01:25:33 -0700273 for_each_node_state(nid, N_ONLINE) {
Dean Nelsoneca7994f2006-06-28 13:50:09 -0500274 uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid);
275 mutex_init(&uncached_pools[nid].add_chunk_mutex);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700276 }
277
Dean Nelson929f9722006-06-23 02:03:21 -0700278 efi_memmap_walk_uc(uncached_build_memmap, NULL);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700279 return 0;
280}
281
282__initcall(uncached_init);