blob: 5f03b9e524dd2ab78bdb368afe0994efbc4af9d9 [file] [log] [blame]
Jes Sorensenf14f75b2005-06-21 17:15:02 -07001/*
Dean Nelson929f9722006-06-23 02:03:21 -07002 * Copyright (C) 2001-2006 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>
21#include <linux/slab.h>
22#include <linux/efi.h>
23#include <linux/genalloc.h>
24#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
35#define MAX_UNCACHED_GRANULES 5
36static int allocated_granules;
37
38struct gen_pool *uncached_pool[MAX_NUMNODES];
39
40
41static void uncached_ipi_visibility(void *data)
42{
43 int status;
44
45 status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
46 if ((status != PAL_VISIBILITY_OK) &&
47 (status != PAL_VISIBILITY_OK_REMOTE_NEEDED))
48 printk(KERN_DEBUG "pal_prefetch_visibility() returns %i on "
Jes Sorensen3bd7f012005-12-16 11:00:03 -050049 "CPU %i\n", status, raw_smp_processor_id());
Jes Sorensenf14f75b2005-06-21 17:15:02 -070050}
51
52
53static void uncached_ipi_mc_drain(void *data)
54{
55 int status;
Dean Nelson929f9722006-06-23 02:03:21 -070056
Jes Sorensenf14f75b2005-06-21 17:15:02 -070057 status = ia64_pal_mc_drain();
58 if (status)
59 printk(KERN_WARNING "ia64_pal_mc_drain() failed with %i on "
Jes Sorensen3bd7f012005-12-16 11:00:03 -050060 "CPU %i\n", status, raw_smp_processor_id());
Jes Sorensenf14f75b2005-06-21 17:15:02 -070061}
62
63
Dean Nelson929f9722006-06-23 02:03:21 -070064/*
65 * Add a new chunk of uncached memory pages to the specified pool.
66 *
67 * @pool: pool to add new chunk of uncached memory to
68 * @nid: node id of node to allocate memory from, or -1
69 *
70 * This is accomplished by first allocating a granule of cached memory pages
71 * and then converting them to uncached memory pages.
72 */
73static int uncached_add_chunk(struct gen_pool *pool, int nid)
Jes Sorensenf14f75b2005-06-21 17:15:02 -070074{
75 struct page *page;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070076 int status, i;
Dean Nelson929f9722006-06-23 02:03:21 -070077 unsigned long c_addr, uc_addr;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070078
79 if (allocated_granules >= MAX_UNCACHED_GRANULES)
Dean Nelson929f9722006-06-23 02:03:21 -070080 return -1;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070081
Dean Nelson929f9722006-06-23 02:03:21 -070082 /* attempt to allocate a granule's worth of cached memory pages */
83
84 page = alloc_pages_node(nid, GFP_KERNEL | __GFP_ZERO,
Jes Sorensenf14f75b2005-06-21 17:15:02 -070085 IA64_GRANULE_SHIFT-PAGE_SHIFT);
Jes Sorensenf14f75b2005-06-21 17:15:02 -070086 if (!page)
Dean Nelson929f9722006-06-23 02:03:21 -070087 return -1;
88
89 /* convert the memory pages from cached to uncached */
90
91 c_addr = (unsigned long)page_address(page);
92 uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070093
94 /*
95 * There's a small race here where it's possible for someone to
96 * access the page through /dev/mem halfway through the conversion
97 * to uncached - not sure it's really worth bothering about
98 */
99 for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
100 SetPageUncached(&page[i]);
101
Dean Nelson929f9722006-06-23 02:03:21 -0700102 flush_tlb_kernel_range(uc_addr, uc_adddr + IA64_GRANULE_SIZE);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700103
104 status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700105 if (!status) {
106 status = smp_call_function(uncached_ipi_visibility, NULL, 0, 1);
107 if (status)
Dean Nelson929f9722006-06-23 02:03:21 -0700108 goto failed;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700109 }
110
Dean Nelson929f9722006-06-23 02:03:21 -0700111 preempt_disable();
112
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700113 if (ia64_platform_is("sn2"))
Dean Nelson929f9722006-06-23 02:03:21 -0700114 sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700115 else
Dean Nelson929f9722006-06-23 02:03:21 -0700116 flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
117
118 /* flush the just introduced uncached translation from the TLB */
119 local_flush_tlb_all();
120
121 preempt_enable();
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700122
123 ia64_pal_mc_drain();
124 status = smp_call_function(uncached_ipi_mc_drain, NULL, 0, 1);
125 if (status)
Dean Nelson929f9722006-06-23 02:03:21 -0700126 goto failed;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700127
Dean Nelson929f9722006-06-23 02:03:21 -0700128 /*
129 * The chunk of memory pages has been converted to uncached so now we
130 * can add it to the pool.
131 */
132 status = gen_pool_add(pool, uc_addr, IA64_GRANULE_SIZE, nid);
133 if (status)
134 goto failed;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700135
136 allocated_granules++;
Dean Nelson929f9722006-06-23 02:03:21 -0700137 return 0;
138
139 /* failed to convert or add the chunk so give it back to the kernel */
140failed:
141 for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
142 ClearPageUncached(&page[i]);
143
144 free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT);
145 return -1;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700146}
147
148
149/*
150 * uncached_alloc_page
151 *
Dean Nelson929f9722006-06-23 02:03:21 -0700152 * @starting_nid: node id of node to start with, or -1
153 *
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700154 * Allocate 1 uncached page. Allocates on the requested node. If no
155 * uncached pages are available on the requested node, roundrobin starting
Dean Nelson929f9722006-06-23 02:03:21 -0700156 * with the next higher node.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700157 */
Dean Nelson929f9722006-06-23 02:03:21 -0700158unsigned long uncached_alloc_page(int starting_nid)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700159{
Dean Nelson929f9722006-06-23 02:03:21 -0700160 unsigned long uc_addr;
161 struct gen_pool *pool;
162 int nid;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700163
Dean Nelson929f9722006-06-23 02:03:21 -0700164 if (unlikely(starting_nid >= MAX_NUMNODES))
165 return 0;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700166
Dean Nelson929f9722006-06-23 02:03:21 -0700167 if (starting_nid < 0)
168 starting_nid = numa_node_id();
169 nid = starting_nid;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700170
Dean Nelson929f9722006-06-23 02:03:21 -0700171 do {
172 if (!node_online(nid))
173 continue;
174 pool = uncached_pool[nid];
175 if (pool == NULL)
176 continue;
177 do {
178 uc_addr = gen_pool_alloc(pool, PAGE_SIZE);
179 if (uc_addr != 0)
180 return uc_addr;
181 } while (uncached_add_chunk(pool, nid) == 0);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700182
Dean Nelson929f9722006-06-23 02:03:21 -0700183 } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700184
Dean Nelson929f9722006-06-23 02:03:21 -0700185 return 0;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700186}
187EXPORT_SYMBOL(uncached_alloc_page);
188
189
190/*
191 * uncached_free_page
192 *
Dean Nelson929f9722006-06-23 02:03:21 -0700193 * @uc_addr: uncached address of page to free
194 *
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700195 * Free a single uncached page.
196 */
Dean Nelson929f9722006-06-23 02:03:21 -0700197void uncached_free_page(unsigned long uc_addr)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700198{
Dean Nelson929f9722006-06-23 02:03:21 -0700199 int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET);
200 struct gen_pool *pool = uncached_pool[nid];
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700201
Dean Nelson929f9722006-06-23 02:03:21 -0700202 if (unlikely(pool == NULL))
203 return;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700204
Dean Nelson929f9722006-06-23 02:03:21 -0700205 if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET)
206 panic("uncached_free_page invalid address %lx\n", uc_addr);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700207
Dean Nelson929f9722006-06-23 02:03:21 -0700208 gen_pool_free(pool, uc_addr, PAGE_SIZE);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700209}
210EXPORT_SYMBOL(uncached_free_page);
211
212
213/*
214 * uncached_build_memmap,
215 *
Dean Nelson929f9722006-06-23 02:03:21 -0700216 * @uc_start: uncached starting address of a chunk of uncached memory
217 * @uc_end: uncached ending address of a chunk of uncached memory
218 * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc())
219 *
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700220 * Called at boot time to build a map of pages that can be used for
221 * memory special operations.
222 */
Dean Nelson929f9722006-06-23 02:03:21 -0700223static int __init uncached_build_memmap(unsigned long uc_start,
224 unsigned long uc_end, void *arg)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700225{
Dean Nelson929f9722006-06-23 02:03:21 -0700226 int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET);
227 struct gen_pool *pool = uncached_pool[nid];
228 size_t size = uc_end - uc_start;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700229
John Hawkes386d1d52006-01-18 23:46:53 -0800230 touch_softlockup_watchdog();
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700231
Dean Nelson929f9722006-06-23 02:03:21 -0700232 if (pool != NULL) {
233 memset((char *)uc_start, 0, size);
234 (void) gen_pool_add(pool, uc_start, size, nid);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700235 }
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700236 return 0;
237}
238
239
Dean Nelson929f9722006-06-23 02:03:21 -0700240static int __init uncached_init(void)
241{
242 int nid;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700243
Dean Nelson929f9722006-06-23 02:03:21 -0700244 for_each_online_node(nid) {
245 uncached_pool[nid] = gen_pool_create(PAGE_SHIFT, nid);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700246 }
247
Dean Nelson929f9722006-06-23 02:03:21 -0700248 efi_memmap_walk_uc(uncached_build_memmap, NULL);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700249 return 0;
250}
251
252__initcall(uncached_init);