Merge branch 'bkl-removal' into next
[pandora-kernel.git] / arch / x86 / mm / discontig_32.c
1 /*
2  * Written by: Patricia Gaughen <gone@us.ibm.com>, IBM Corporation
3  * August 2002: added remote node KVA remap - Martin J. Bligh 
4  *
5  * Copyright (C) 2002, IBM Corp.
6  *
7  * All rights reserved.          
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
17  * NON INFRINGEMENT.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <linux/mm.h>
26 #include <linux/bootmem.h>
27 #include <linux/mmzone.h>
28 #include <linux/highmem.h>
29 #include <linux/initrd.h>
30 #include <linux/nodemask.h>
31 #include <linux/module.h>
32 #include <linux/kexec.h>
33 #include <linux/pfn.h>
34 #include <linux/swap.h>
35 #include <linux/acpi.h>
36
37 #include <asm/e820.h>
38 #include <asm/setup.h>
39 #include <asm/mmzone.h>
40 #include <asm/bios_ebda.h>
41 #include <asm/proto.h>
42
43 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
44 EXPORT_SYMBOL(node_data);
45 static bootmem_data_t node0_bdata;
46
47 /*
48  * numa interface - we expect the numa architecture specific code to have
49  *                  populated the following initialisation.
50  *
51  * 1) node_online_map  - the map of all nodes configured (online) in the system
52  * 2) node_start_pfn   - the starting page frame number for a node
53  * 3) node_end_pfn     - the ending page fram number for a node
54  */
55 unsigned long node_start_pfn[MAX_NUMNODES] __read_mostly;
56 unsigned long node_end_pfn[MAX_NUMNODES] __read_mostly;
57
58
59 #ifdef CONFIG_DISCONTIGMEM
60 /*
61  * 4) physnode_map     - the mapping between a pfn and owning node
62  * physnode_map keeps track of the physical memory layout of a generic
63  * numa node on a 64Mb break (each element of the array will
64  * represent 64Mb of memory and will be marked by the node id.  so,
65  * if the first gig is on node 0, and the second gig is on node 1
66  * physnode_map will contain:
67  *
68  *     physnode_map[0-15] = 0;
69  *     physnode_map[16-31] = 1;
70  *     physnode_map[32- ] = -1;
71  */
72 s8 physnode_map[MAX_ELEMENTS] __read_mostly = { [0 ... (MAX_ELEMENTS - 1)] = -1};
73 EXPORT_SYMBOL(physnode_map);
74
75 void memory_present(int nid, unsigned long start, unsigned long end)
76 {
77         unsigned long pfn;
78
79         printk(KERN_INFO "Node: %d, start_pfn: %lx, end_pfn: %lx\n",
80                         nid, start, end);
81         printk(KERN_DEBUG "  Setting physnode_map array to node %d for pfns:\n", nid);
82         printk(KERN_DEBUG "  ");
83         for (pfn = start; pfn < end; pfn += PAGES_PER_ELEMENT) {
84                 physnode_map[pfn / PAGES_PER_ELEMENT] = nid;
85                 printk(KERN_CONT "%lx ", pfn);
86         }
87         printk(KERN_CONT "\n");
88 }
89
90 unsigned long node_memmap_size_bytes(int nid, unsigned long start_pfn,
91                                               unsigned long end_pfn)
92 {
93         unsigned long nr_pages = end_pfn - start_pfn;
94
95         if (!nr_pages)
96                 return 0;
97
98         return (nr_pages + 1) * sizeof(struct page);
99 }
100 #endif
101
102 extern unsigned long find_max_low_pfn(void);
103 extern unsigned long highend_pfn, highstart_pfn;
104
105 #define LARGE_PAGE_BYTES (PTRS_PER_PTE * PAGE_SIZE)
106
107 unsigned long node_remap_size[MAX_NUMNODES];
108 static void *node_remap_start_vaddr[MAX_NUMNODES];
109 void set_pmd_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags);
110
111 static unsigned long kva_start_pfn;
112 static unsigned long kva_pages;
113 /*
114  * FLAT - support for basic PC memory model with discontig enabled, essentially
115  *        a single node with all available processors in it with a flat
116  *        memory map.
117  */
118 int __init get_memcfg_numa_flat(void)
119 {
120         printk(KERN_DEBUG "NUMA - single node, flat memory mode\n");
121
122         node_start_pfn[0] = 0;
123         node_end_pfn[0] = max_pfn;
124         e820_register_active_regions(0, 0, max_pfn);
125         memory_present(0, 0, max_pfn);
126         node_remap_size[0] = node_memmap_size_bytes(0, 0, max_pfn);
127
128         /* Indicate there is one node available. */
129         nodes_clear(node_online_map);
130         node_set_online(0);
131         return 1;
132 }
133
134 /*
135  * Find the highest page frame number we have available for the node
136  */
137 static void __init propagate_e820_map_node(int nid)
138 {
139         if (node_end_pfn[nid] > max_pfn)
140                 node_end_pfn[nid] = max_pfn;
141         /*
142          * if a user has given mem=XXXX, then we need to make sure 
143          * that the node _starts_ before that, too, not just ends
144          */
145         if (node_start_pfn[nid] > max_pfn)
146                 node_start_pfn[nid] = max_pfn;
147         BUG_ON(node_start_pfn[nid] > node_end_pfn[nid]);
148 }
149
150 /* 
151  * Allocate memory for the pg_data_t for this node via a crude pre-bootmem
152  * method.  For node zero take this from the bottom of memory, for
153  * subsequent nodes place them at node_remap_start_vaddr which contains
154  * node local data in physically node local memory.  See setup_memory()
155  * for details.
156  */
157 static void __init allocate_pgdat(int nid)
158 {
159         char buf[16];
160
161         if (node_has_online_mem(nid) && node_remap_start_vaddr[nid])
162                 NODE_DATA(nid) = (pg_data_t *)node_remap_start_vaddr[nid];
163         else {
164                 unsigned long pgdat_phys;
165                 pgdat_phys = find_e820_area(min_low_pfn<<PAGE_SHIFT,
166                                  max_pfn_mapped<<PAGE_SHIFT,
167                                  sizeof(pg_data_t),
168                                  PAGE_SIZE);
169                 NODE_DATA(nid) = (pg_data_t *)(pfn_to_kaddr(pgdat_phys>>PAGE_SHIFT));
170                 memset(buf, 0, sizeof(buf));
171                 sprintf(buf, "NODE_DATA %d",  nid);
172                 reserve_early(pgdat_phys, pgdat_phys + sizeof(pg_data_t), buf);
173         }
174         printk(KERN_DEBUG "allocate_pgdat: node %d NODE_DATA %08lx\n",
175                 nid, (unsigned long)NODE_DATA(nid));
176 }
177
178 /*
179  * In the DISCONTIGMEM and SPARSEMEM memory model, a portion of the kernel
180  * virtual address space (KVA) is reserved and portions of nodes are mapped
181  * using it. This is to allow node-local memory to be allocated for
182  * structures that would normally require ZONE_NORMAL. The memory is
183  * allocated with alloc_remap() and callers should be prepared to allocate
184  * from the bootmem allocator instead.
185  */
186 static unsigned long node_remap_start_pfn[MAX_NUMNODES];
187 static void *node_remap_end_vaddr[MAX_NUMNODES];
188 static void *node_remap_alloc_vaddr[MAX_NUMNODES];
189 static unsigned long node_remap_offset[MAX_NUMNODES];
190
191 void *alloc_remap(int nid, unsigned long size)
192 {
193         void *allocation = node_remap_alloc_vaddr[nid];
194
195         size = ALIGN(size, L1_CACHE_BYTES);
196
197         if (!allocation || (allocation + size) >= node_remap_end_vaddr[nid])
198                 return 0;
199
200         node_remap_alloc_vaddr[nid] += size;
201         memset(allocation, 0, size);
202
203         return allocation;
204 }
205
206 static void __init remap_numa_kva(void)
207 {
208         void *vaddr;
209         unsigned long pfn;
210         int node;
211
212         for_each_online_node(node) {
213                 printk(KERN_DEBUG "remap_numa_kva: node %d\n", node);
214                 for (pfn=0; pfn < node_remap_size[node]; pfn += PTRS_PER_PTE) {
215                         vaddr = node_remap_start_vaddr[node]+(pfn<<PAGE_SHIFT);
216                         printk(KERN_DEBUG "remap_numa_kva: %08lx to pfn %08lx\n",
217                                 (unsigned long)vaddr,
218                                 node_remap_start_pfn[node] + pfn);
219                         set_pmd_pfn((ulong) vaddr, 
220                                 node_remap_start_pfn[node] + pfn, 
221                                 PAGE_KERNEL_LARGE);
222                 }
223         }
224 }
225
226 static unsigned long calculate_numa_remap_pages(void)
227 {
228         int nid;
229         unsigned long size, reserve_pages = 0;
230
231         for_each_online_node(nid) {
232                 u64 node_kva_target;
233                 u64 node_kva_final;
234
235                 /*
236                  * The acpi/srat node info can show hot-add memroy zones
237                  * where memory could be added but not currently present.
238                  */
239                 printk(KERN_DEBUG "node %d pfn: [%lx - %lx]\n",
240                         nid, node_start_pfn[nid], node_end_pfn[nid]);
241                 if (node_start_pfn[nid] > max_pfn)
242                         continue;
243                 if (!node_end_pfn[nid])
244                         continue;
245                 if (node_end_pfn[nid] > max_pfn)
246                         node_end_pfn[nid] = max_pfn;
247
248                 /* ensure the remap includes space for the pgdat. */
249                 size = node_remap_size[nid] + sizeof(pg_data_t);
250
251                 /* convert size to large (pmd size) pages, rounding up */
252                 size = (size + LARGE_PAGE_BYTES - 1) / LARGE_PAGE_BYTES;
253                 /* now the roundup is correct, convert to PAGE_SIZE pages */
254                 size = size * PTRS_PER_PTE;
255
256                 node_kva_target = round_down(node_end_pfn[nid] - size,
257                                                  PTRS_PER_PTE);
258                 node_kva_target <<= PAGE_SHIFT;
259                 do {
260                         node_kva_final = find_e820_area(node_kva_target,
261                                         ((u64)node_end_pfn[nid])<<PAGE_SHIFT,
262                                                 ((u64)size)<<PAGE_SHIFT,
263                                                 LARGE_PAGE_BYTES);
264                         node_kva_target -= LARGE_PAGE_BYTES;
265                 } while (node_kva_final == -1ULL &&
266                          (node_kva_target>>PAGE_SHIFT) > (node_start_pfn[nid]));
267
268                 if (node_kva_final == -1ULL)
269                         panic("Can not get kva ram\n");
270
271                 node_remap_size[nid] = size;
272                 node_remap_offset[nid] = reserve_pages;
273                 reserve_pages += size;
274                 printk(KERN_DEBUG "Reserving %ld pages of KVA for lmem_map of"
275                                   " node %d at %llx\n",
276                                 size, nid, node_kva_final>>PAGE_SHIFT);
277
278                 /*
279                  *  prevent kva address below max_low_pfn want it on system
280                  *  with less memory later.
281                  *  layout will be: KVA address , KVA RAM
282                  *
283                  *  we are supposed to only record the one less then max_low_pfn
284                  *  but we could have some hole in high memory, and it will only
285                  *  check page_is_ram(pfn) && !page_is_reserved_early(pfn) to decide
286                  *  to use it as free.
287                  *  So reserve_early here, hope we don't run out of that array
288                  */
289                 reserve_early(node_kva_final,
290                               node_kva_final+(((u64)size)<<PAGE_SHIFT),
291                               "KVA RAM");
292
293                 node_remap_start_pfn[nid] = node_kva_final>>PAGE_SHIFT;
294                 remove_active_range(nid, node_remap_start_pfn[nid],
295                                          node_remap_start_pfn[nid] + size);
296         }
297         printk(KERN_INFO "Reserving total of %lx pages for numa KVA remap\n",
298                         reserve_pages);
299         return reserve_pages;
300 }
301
302 static void init_remap_allocator(int nid)
303 {
304         node_remap_start_vaddr[nid] = pfn_to_kaddr(
305                         kva_start_pfn + node_remap_offset[nid]);
306         node_remap_end_vaddr[nid] = node_remap_start_vaddr[nid] +
307                 (node_remap_size[nid] * PAGE_SIZE);
308         node_remap_alloc_vaddr[nid] = node_remap_start_vaddr[nid] +
309                 ALIGN(sizeof(pg_data_t), PAGE_SIZE);
310
311         printk(KERN_DEBUG "node %d will remap to vaddr %08lx - %08lx\n", nid,
312                 (ulong) node_remap_start_vaddr[nid],
313                 (ulong) node_remap_end_vaddr[nid]);
314 }
315
316 void __init initmem_init(unsigned long start_pfn,
317                                   unsigned long end_pfn)
318 {
319         int nid;
320         long kva_target_pfn;
321
322         /*
323          * When mapping a NUMA machine we allocate the node_mem_map arrays
324          * from node local memory.  They are then mapped directly into KVA
325          * between zone normal and vmalloc space.  Calculate the size of
326          * this space and use it to adjust the boundary between ZONE_NORMAL
327          * and ZONE_HIGHMEM.
328          */
329
330         get_memcfg_numa();
331
332         kva_pages = round_up(calculate_numa_remap_pages(), PTRS_PER_PTE);
333
334         kva_target_pfn = round_down(max_low_pfn - kva_pages, PTRS_PER_PTE);
335         do {
336                 kva_start_pfn = find_e820_area(kva_target_pfn<<PAGE_SHIFT,
337                                         max_low_pfn<<PAGE_SHIFT,
338                                         kva_pages<<PAGE_SHIFT,
339                                         PTRS_PER_PTE<<PAGE_SHIFT) >> PAGE_SHIFT;
340                 kva_target_pfn -= PTRS_PER_PTE;
341         } while (kva_start_pfn == -1UL && kva_target_pfn > min_low_pfn);
342
343         if (kva_start_pfn == -1UL)
344                 panic("Can not get kva space\n");
345
346         printk(KERN_INFO "kva_start_pfn ~ %lx max_low_pfn ~ %lx\n",
347                 kva_start_pfn, max_low_pfn);
348         printk(KERN_INFO "max_pfn = %lx\n", max_pfn);
349
350         /* avoid clash with initrd */
351         reserve_early(kva_start_pfn<<PAGE_SHIFT,
352                       (kva_start_pfn + kva_pages)<<PAGE_SHIFT,
353                      "KVA PG");
354 #ifdef CONFIG_HIGHMEM
355         highstart_pfn = highend_pfn = max_pfn;
356         if (max_pfn > max_low_pfn)
357                 highstart_pfn = max_low_pfn;
358         printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
359                pages_to_mb(highend_pfn - highstart_pfn));
360         num_physpages = highend_pfn;
361         high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
362 #else
363         num_physpages = max_low_pfn;
364         high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
365 #endif
366         printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
367                         pages_to_mb(max_low_pfn));
368         printk(KERN_DEBUG "max_low_pfn = %lx, highstart_pfn = %lx\n",
369                         max_low_pfn, highstart_pfn);
370
371         printk(KERN_DEBUG "Low memory ends at vaddr %08lx\n",
372                         (ulong) pfn_to_kaddr(max_low_pfn));
373         for_each_online_node(nid) {
374                 init_remap_allocator(nid);
375
376                 allocate_pgdat(nid);
377         }
378         remap_numa_kva();
379
380         printk(KERN_DEBUG "High memory starts at vaddr %08lx\n",
381                         (ulong) pfn_to_kaddr(highstart_pfn));
382         for_each_online_node(nid)
383                 propagate_e820_map_node(nid);
384
385         for_each_online_node(nid)
386                 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
387
388         NODE_DATA(0)->bdata = &node0_bdata;
389         setup_bootmem_allocator();
390 }
391
392 void __init set_highmem_pages_init(void)
393 {
394 #ifdef CONFIG_HIGHMEM
395         struct zone *zone;
396         int nid;
397
398         for_each_zone(zone) {
399                 unsigned long zone_start_pfn, zone_end_pfn;
400
401                 if (!is_highmem(zone))
402                         continue;
403
404                 zone_start_pfn = zone->zone_start_pfn;
405                 zone_end_pfn = zone_start_pfn + zone->spanned_pages;
406
407                 nid = zone_to_nid(zone);
408                 printk(KERN_INFO "Initializing %s for node %d (%08lx:%08lx)\n",
409                                 zone->name, nid, zone_start_pfn, zone_end_pfn);
410
411                 add_highpages_with_active_regions(nid, zone_start_pfn,
412                                  zone_end_pfn);
413         }
414         totalram_pages += totalhigh_pages;
415 #endif
416 }
417
418 #ifdef CONFIG_MEMORY_HOTPLUG
419 static int paddr_to_nid(u64 addr)
420 {
421         int nid;
422         unsigned long pfn = PFN_DOWN(addr);
423
424         for_each_node(nid)
425                 if (node_start_pfn[nid] <= pfn &&
426                     pfn < node_end_pfn[nid])
427                         return nid;
428
429         return -1;
430 }
431
432 /*
433  * This function is used to ask node id BEFORE memmap and mem_section's
434  * initialization (pfn_to_nid() can't be used yet).
435  * If _PXM is not defined on ACPI's DSDT, node id must be found by this.
436  */
437 int memory_add_physaddr_to_nid(u64 addr)
438 {
439         int nid = paddr_to_nid(addr);
440         return (nid >= 0) ? nid : 0;
441 }
442
443 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
444 #endif
445