f813caa80570901123fe90cd2b5ef6fd882f9b28
[linux-2.6-microblaze.git] / arch / ia64 / kernel / uncached.c
1 /*
2  * Copyright (C) 2001-2006 Silicon Graphics, Inc.  All rights reserved.
3  *
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
32
33 extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *);
34
35 struct 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 };
41
42 #define MAX_CONVERTED_CHUNKS_PER_NODE   2
43
44 struct uncached_pool uncached_pools[MAX_NUMNODES];
45
46
47 static void uncached_ipi_visibility(void *data)
48 {
49         int status;
50         struct uncached_pool *uc_pool = (struct uncached_pool *)data;
51
52         status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
53         if ((status != PAL_VISIBILITY_OK) &&
54             (status != PAL_VISIBILITY_OK_REMOTE_NEEDED))
55                 atomic_inc(&uc_pool->status);
56 }
57
58
59 static void uncached_ipi_mc_drain(void *data)
60 {
61         int status;
62         struct uncached_pool *uc_pool = (struct uncached_pool *)data;
63
64         status = ia64_pal_mc_drain();
65         if (status != PAL_STATUS_SUCCESS)
66                 atomic_inc(&uc_pool->status);
67 }
68
69
70 /*
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  */
79 static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid)
80 {
81         struct page *page;
82         int status, i, nchunks_added = uc_pool->nchunks_added;
83         unsigned long c_addr, uc_addr;
84
85         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);
96                 return -1;
97         }
98
99         /* attempt to allocate a granule's worth of cached memory pages */
100
101         page = alloc_pages_node(nid, GFP_KERNEL | __GFP_ZERO |
102                                  __GFP_THISNODE | __GFP_NORETRY | __GFP_NOWARN,
103                                 IA64_GRANULE_SHIFT-PAGE_SHIFT);
104         if (!page) {
105                 mutex_unlock(&uc_pool->add_chunk_mutex);
106                 return -1;
107         }
108
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;
113
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
122         flush_tlb_kernel_range(uc_addr, uc_adddr + IA64_GRANULE_SIZE);
123
124         status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
125         if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) {
126                 atomic_set(&uc_pool->status, 0);
127                 status = smp_call_function(uncached_ipi_visibility, uc_pool,
128                                            0, 1);
129                 if (status || atomic_read(&uc_pool->status))
130                         goto failed;
131         } else if (status != PAL_VISIBILITY_OK)
132                 goto failed;
133
134         preempt_disable();
135
136         if (ia64_platform_is("sn2"))
137                 sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE);
138         else
139                 flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
140
141         /* flush the just introduced uncached translation from the TLB */
142         local_flush_tlb_all();
143
144         preempt_enable();
145
146         status = ia64_pal_mc_drain();
147         if (status != PAL_STATUS_SUCCESS)
148                 goto failed;
149         atomic_set(&uc_pool->status, 0);
150         status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 0, 1);
151         if (status || atomic_read(&uc_pool->status))
152                 goto failed;
153
154         /*
155          * The chunk of memory pages has been converted to uncached so now we
156          * can add it to the pool.
157          */
158         status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid);
159         if (status)
160                 goto failed;
161
162         uc_pool->nchunks_added++;
163         mutex_unlock(&uc_pool->add_chunk_mutex);
164         return 0;
165
166         /* failed to convert or add the chunk so give it back to the kernel */
167 failed:
168         for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
169                 ClearPageUncached(&page[i]);
170
171         free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT);
172         mutex_unlock(&uc_pool->add_chunk_mutex);
173         return -1;
174 }
175
176
177 /*
178  * uncached_alloc_page
179  *
180  * @starting_nid: node id of node to start with, or -1
181  *
182  * Allocate 1 uncached page. Allocates on the requested node. If no
183  * uncached pages are available on the requested node, roundrobin starting
184  * with the next higher node.
185  */
186 unsigned long uncached_alloc_page(int starting_nid)
187 {
188         unsigned long uc_addr;
189         struct uncached_pool *uc_pool;
190         int nid;
191
192         if (unlikely(starting_nid >= MAX_NUMNODES))
193                 return 0;
194
195         if (starting_nid < 0)
196                 starting_nid = numa_node_id();
197         nid = starting_nid;
198
199         do {
200                 if (!node_online(nid))
201                         continue;
202                 uc_pool = &uncached_pools[nid];
203                 if (uc_pool->pool == NULL)
204                         continue;
205                 do {
206                         uc_addr = gen_pool_alloc(uc_pool->pool, PAGE_SIZE);
207                         if (uc_addr != 0)
208                                 return uc_addr;
209                 } while (uncached_add_chunk(uc_pool, nid) == 0);
210
211         } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid);
212
213         return 0;
214 }
215 EXPORT_SYMBOL(uncached_alloc_page);
216
217
218 /*
219  * uncached_free_page
220  *
221  * @uc_addr: uncached address of page to free
222  *
223  * Free a single uncached page.
224  */
225 void uncached_free_page(unsigned long uc_addr)
226 {
227         int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET);
228         struct gen_pool *pool = uncached_pools[nid].pool;
229
230         if (unlikely(pool == NULL))
231                 return;
232
233         if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET)
234                 panic("uncached_free_page invalid address %lx\n", uc_addr);
235
236         gen_pool_free(pool, uc_addr, PAGE_SIZE);
237 }
238 EXPORT_SYMBOL(uncached_free_page);
239
240
241 /*
242  * uncached_build_memmap,
243  *
244  * @uc_start: uncached starting address of a chunk of uncached memory
245  * @uc_end: uncached ending address of a chunk of uncached memory
246  * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc())
247  *
248  * Called at boot time to build a map of pages that can be used for
249  * memory special operations.
250  */
251 static int __init uncached_build_memmap(unsigned long uc_start,
252                                         unsigned long uc_end, void *arg)
253 {
254         int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET);
255         struct gen_pool *pool = uncached_pools[nid].pool;
256         size_t size = uc_end - uc_start;
257
258         touch_softlockup_watchdog();
259
260         if (pool != NULL) {
261                 memset((char *)uc_start, 0, size);
262                 (void) gen_pool_add(pool, uc_start, size, nid);
263         }
264         return 0;
265 }
266
267
268 static int __init uncached_init(void)
269 {
270         int nid;
271
272         for_each_online_node(nid) {
273                 uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid);
274                 mutex_init(&uncached_pools[nid].add_chunk_mutex);
275         }
276
277         efi_memmap_walk_uc(uncached_build_memmap, NULL);
278         return 0;
279 }
280
281 __initcall(uncached_init);