blob: f3976da36721a94353dc68e6dd737dc3697f7aec [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>
Jes Sorensenf14f75b2005-06-21 17:15:02 -070026#include <asm/pgtable.h>
Arun Sharma600634972011-07-26 16:09:06 -070027#include <linux/atomic.h>
Jes Sorensenf14f75b2005-06-21 17:15:02 -070028#include <asm/tlbflush.h>
29#include <asm/sn/arch.h>
30
Jes Sorensenf14f75b2005-06-21 17:15:02 -070031
Dean Nelson929f9722006-06-23 02:03:21 -070032extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *);
Jes Sorensenf14f75b2005-06-21 17:15:02 -070033
Dean Nelsoneca79942006-06-28 13:50:09 -050034struct uncached_pool {
35 struct gen_pool *pool;
36 struct mutex add_chunk_mutex; /* serialize adding a converted chunk */
37 int nchunks_added; /* #of converted chunks added to pool */
38 atomic_t status; /* smp called function's return status*/
39};
Jes Sorensenf14f75b2005-06-21 17:15:02 -070040
Dean Nelsoneca79942006-06-28 13:50:09 -050041#define MAX_CONVERTED_CHUNKS_PER_NODE 2
42
43struct uncached_pool uncached_pools[MAX_NUMNODES];
Jes Sorensenf14f75b2005-06-21 17:15:02 -070044
45
46static void uncached_ipi_visibility(void *data)
47{
48 int status;
Dean Nelsoneca79942006-06-28 13:50:09 -050049 struct uncached_pool *uc_pool = (struct uncached_pool *)data;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070050
51 status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
52 if ((status != PAL_VISIBILITY_OK) &&
53 (status != PAL_VISIBILITY_OK_REMOTE_NEEDED))
Dean Nelsoneca79942006-06-28 13:50:09 -050054 atomic_inc(&uc_pool->status);
Jes Sorensenf14f75b2005-06-21 17:15:02 -070055}
56
57
58static void uncached_ipi_mc_drain(void *data)
59{
60 int status;
Dean Nelsoneca79942006-06-28 13:50:09 -050061 struct uncached_pool *uc_pool = (struct uncached_pool *)data;
Dean Nelson929f9722006-06-23 02:03:21 -070062
Jes Sorensenf14f75b2005-06-21 17:15:02 -070063 status = ia64_pal_mc_drain();
Dean Nelsoneca79942006-06-28 13:50:09 -050064 if (status != PAL_STATUS_SUCCESS)
65 atomic_inc(&uc_pool->status);
Jes Sorensenf14f75b2005-06-21 17:15:02 -070066}
67
68
Dean Nelson929f9722006-06-23 02:03:21 -070069/*
70 * Add a new chunk of uncached memory pages to the specified pool.
71 *
72 * @pool: pool to add new chunk of uncached memory to
73 * @nid: node id of node to allocate memory from, or -1
74 *
75 * This is accomplished by first allocating a granule of cached memory pages
76 * and then converting them to uncached memory pages.
77 */
Dean Nelsoneca79942006-06-28 13:50:09 -050078static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid)
Jes Sorensenf14f75b2005-06-21 17:15:02 -070079{
80 struct page *page;
Dean Nelsoneca79942006-06-28 13:50:09 -050081 int status, i, nchunks_added = uc_pool->nchunks_added;
Dean Nelson929f9722006-06-23 02:03:21 -070082 unsigned long c_addr, uc_addr;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070083
Dean Nelsoneca79942006-06-28 13:50:09 -050084 if (mutex_lock_interruptible(&uc_pool->add_chunk_mutex) != 0)
85 return -1; /* interrupted by a signal */
86
87 if (uc_pool->nchunks_added > nchunks_added) {
88 /* someone added a new chunk while we were waiting */
89 mutex_unlock(&uc_pool->add_chunk_mutex);
90 return 0;
91 }
92
93 if (uc_pool->nchunks_added >= MAX_CONVERTED_CHUNKS_PER_NODE) {
94 mutex_unlock(&uc_pool->add_chunk_mutex);
Dean Nelson929f9722006-06-23 02:03:21 -070095 return -1;
Dean Nelsoneca79942006-06-28 13:50:09 -050096 }
Jes Sorensenf14f75b2005-06-21 17:15:02 -070097
Dean Nelson929f9722006-06-23 02:03:21 -070098 /* attempt to allocate a granule's worth of cached memory pages */
99
Vlastimil Babka96db8002015-09-08 15:03:50 -0700100 page = __alloc_pages_node(nid,
Johannes Weinere97ca8e2014-03-10 15:49:43 -0700101 GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE,
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700102 IA64_GRANULE_SHIFT-PAGE_SHIFT);
Dean Nelsoneca79942006-06-28 13:50:09 -0500103 if (!page) {
104 mutex_unlock(&uc_pool->add_chunk_mutex);
Dean Nelson929f9722006-06-23 02:03:21 -0700105 return -1;
Dean Nelsoneca79942006-06-28 13:50:09 -0500106 }
Dean Nelson929f9722006-06-23 02:03:21 -0700107
108 /* convert the memory pages from cached to uncached */
109
110 c_addr = (unsigned long)page_address(page);
111 uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700112
113 /*
114 * There's a small race here where it's possible for someone to
115 * access the page through /dev/mem halfway through the conversion
116 * to uncached - not sure it's really worth bothering about
117 */
118 for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
119 SetPageUncached(&page[i]);
120
Jan Beulich285fbd62007-12-19 12:30:30 -0800121 flush_tlb_kernel_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700122
123 status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
Dean Nelsoneca79942006-06-28 13:50:09 -0500124 if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) {
125 atomic_set(&uc_pool->status, 0);
Jens Axboe8691e5a2008-06-06 11:18:06 +0200126 status = smp_call_function(uncached_ipi_visibility, uc_pool, 1);
Dean Nelsoneca79942006-06-28 13:50:09 -0500127 if (status || atomic_read(&uc_pool->status))
Dean Nelson929f9722006-06-23 02:03:21 -0700128 goto failed;
Dean Nelsoneca79942006-06-28 13:50:09 -0500129 } else if (status != PAL_VISIBILITY_OK)
130 goto failed;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700131
Dean Nelson929f9722006-06-23 02:03:21 -0700132 preempt_disable();
133
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700134 if (ia64_platform_is("sn2"))
Dean Nelson929f9722006-06-23 02:03:21 -0700135 sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700136 else
Dean Nelson929f9722006-06-23 02:03:21 -0700137 flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
138
139 /* flush the just introduced uncached translation from the TLB */
140 local_flush_tlb_all();
141
142 preempt_enable();
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700143
Dean Nelsoneca79942006-06-28 13:50:09 -0500144 status = ia64_pal_mc_drain();
145 if (status != PAL_STATUS_SUCCESS)
146 goto failed;
147 atomic_set(&uc_pool->status, 0);
Jens Axboe8691e5a2008-06-06 11:18:06 +0200148 status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 1);
Dean Nelsoneca79942006-06-28 13:50:09 -0500149 if (status || atomic_read(&uc_pool->status))
Dean Nelson929f9722006-06-23 02:03:21 -0700150 goto failed;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700151
Dean Nelson929f9722006-06-23 02:03:21 -0700152 /*
153 * The chunk of memory pages has been converted to uncached so now we
154 * can add it to the pool.
155 */
Dean Nelsoneca79942006-06-28 13:50:09 -0500156 status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid);
Dean Nelson929f9722006-06-23 02:03:21 -0700157 if (status)
158 goto failed;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700159
Dean Nelsoneca79942006-06-28 13:50:09 -0500160 uc_pool->nchunks_added++;
161 mutex_unlock(&uc_pool->add_chunk_mutex);
Dean Nelson929f9722006-06-23 02:03:21 -0700162 return 0;
163
164 /* failed to convert or add the chunk so give it back to the kernel */
165failed:
166 for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
167 ClearPageUncached(&page[i]);
168
169 free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT);
Dean Nelsoneca79942006-06-28 13:50:09 -0500170 mutex_unlock(&uc_pool->add_chunk_mutex);
Dean Nelson929f9722006-06-23 02:03:21 -0700171 return -1;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700172}
173
174
175/*
176 * uncached_alloc_page
177 *
Dean Nelson929f9722006-06-23 02:03:21 -0700178 * @starting_nid: node id of node to start with, or -1
Dean Nelsone4a064d2008-04-25 15:22:19 -0500179 * @n_pages: number of contiguous pages to allocate
Dean Nelson929f9722006-06-23 02:03:21 -0700180 *
Dean Nelsone4a064d2008-04-25 15:22:19 -0500181 * Allocate the specified number of contiguous uncached pages on the
182 * the requested node. If not enough contiguous uncached pages are available
183 * on the requested node, roundrobin starting with the next higher node.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700184 */
Dean Nelsone4a064d2008-04-25 15:22:19 -0500185unsigned long uncached_alloc_page(int starting_nid, int n_pages)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700186{
Dean Nelson929f9722006-06-23 02:03:21 -0700187 unsigned long uc_addr;
Dean Nelsoneca79942006-06-28 13:50:09 -0500188 struct uncached_pool *uc_pool;
Dean Nelson929f9722006-06-23 02:03:21 -0700189 int nid;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700190
Dean Nelson929f9722006-06-23 02:03:21 -0700191 if (unlikely(starting_nid >= MAX_NUMNODES))
192 return 0;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700193
Dean Nelson929f9722006-06-23 02:03:21 -0700194 if (starting_nid < 0)
195 starting_nid = numa_node_id();
196 nid = starting_nid;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700197
Dean Nelson929f9722006-06-23 02:03:21 -0700198 do {
Christoph Lameter2dca53a2007-10-16 01:25:33 -0700199 if (!node_state(nid, N_HIGH_MEMORY))
Dean Nelson929f9722006-06-23 02:03:21 -0700200 continue;
Dean Nelsoneca79942006-06-28 13:50:09 -0500201 uc_pool = &uncached_pools[nid];
202 if (uc_pool->pool == NULL)
Dean Nelson929f9722006-06-23 02:03:21 -0700203 continue;
204 do {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500205 uc_addr = gen_pool_alloc(uc_pool->pool,
206 n_pages * PAGE_SIZE);
Dean Nelson929f9722006-06-23 02:03:21 -0700207 if (uc_addr != 0)
208 return uc_addr;
Dean Nelsoneca79942006-06-28 13:50:09 -0500209 } while (uncached_add_chunk(uc_pool, nid) == 0);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700210
Dean Nelson929f9722006-06-23 02:03:21 -0700211 } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700212
Dean Nelson929f9722006-06-23 02:03:21 -0700213 return 0;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700214}
215EXPORT_SYMBOL(uncached_alloc_page);
216
217
218/*
219 * uncached_free_page
220 *
Dean Nelsone4a064d2008-04-25 15:22:19 -0500221 * @uc_addr: uncached address of first page to free
222 * @n_pages: number of contiguous pages to free
Dean Nelson929f9722006-06-23 02:03:21 -0700223 *
Dean Nelsone4a064d2008-04-25 15:22:19 -0500224 * Free the specified number of uncached pages.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700225 */
Dean Nelsone4a064d2008-04-25 15:22:19 -0500226void uncached_free_page(unsigned long uc_addr, int n_pages)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700227{
Dean Nelson929f9722006-06-23 02:03:21 -0700228 int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET);
Dean Nelsoneca79942006-06-28 13:50:09 -0500229 struct gen_pool *pool = uncached_pools[nid].pool;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700230
Dean Nelson929f9722006-06-23 02:03:21 -0700231 if (unlikely(pool == NULL))
232 return;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700233
Dean Nelson929f9722006-06-23 02:03:21 -0700234 if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET)
235 panic("uncached_free_page invalid address %lx\n", uc_addr);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700236
Dean Nelsone4a064d2008-04-25 15:22:19 -0500237 gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700238}
239EXPORT_SYMBOL(uncached_free_page);
240
241
242/*
243 * uncached_build_memmap,
244 *
Dean Nelson929f9722006-06-23 02:03:21 -0700245 * @uc_start: uncached starting address of a chunk of uncached memory
246 * @uc_end: uncached ending address of a chunk of uncached memory
247 * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc())
248 *
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700249 * Called at boot time to build a map of pages that can be used for
250 * memory special operations.
251 */
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700252static int __init uncached_build_memmap(u64 uc_start, u64 uc_end, void *arg)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700253{
Dean Nelson929f9722006-06-23 02:03:21 -0700254 int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET);
Dean Nelsoneca79942006-06-28 13:50:09 -0500255 struct gen_pool *pool = uncached_pools[nid].pool;
Dean Nelson929f9722006-06-23 02:03:21 -0700256 size_t size = uc_end - uc_start;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700257
John Hawkes386d1d52006-01-18 23:46:53 -0800258 touch_softlockup_watchdog();
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700259
Dean Nelson929f9722006-06-23 02:03:21 -0700260 if (pool != NULL) {
261 memset((char *)uc_start, 0, size);
262 (void) gen_pool_add(pool, uc_start, size, nid);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700263 }
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700264 return 0;
265}
266
267
Dean Nelson929f9722006-06-23 02:03:21 -0700268static int __init uncached_init(void)
269{
270 int nid;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700271
Christoph Lameter2dca53a2007-10-16 01:25:33 -0700272 for_each_node_state(nid, N_ONLINE) {
Dean Nelsoneca79942006-06-28 13:50:09 -0500273 uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid);
274 mutex_init(&uncached_pools[nid].add_chunk_mutex);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700275 }
276
Dean Nelson929f9722006-06-23 02:03:21 -0700277 efi_memmap_walk_uc(uncached_build_memmap, NULL);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700278 return 0;
279}
280
281__initcall(uncached_init);