vserver 1.9.5.x5
[linux-2.6.git] / arch / i386 / mm / discontig.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/config.h>
26 #include <linux/mm.h>
27 #include <linux/bootmem.h>
28 #include <linux/mmzone.h>
29 #include <linux/highmem.h>
30 #include <linux/initrd.h>
31 #include <linux/nodemask.h>
32 #include <asm/e820.h>
33 #include <asm/setup.h>
34 #include <asm/mmzone.h>
35 #include <bios_ebda.h>
36
37 struct pglist_data *node_data[MAX_NUMNODES];
38 bootmem_data_t node0_bdata;
39
40 /*
41  * numa interface - we expect the numa architecture specfic code to have
42  *                  populated the following initialisation.
43  *
44  * 1) node_online_map  - the map of all nodes configured (online) in the system
45  * 2) physnode_map     - the mapping between a pfn and owning node
46  * 3) node_start_pfn   - the starting page frame number for a node
47  * 3) node_end_pfn     - the ending page fram number for a node
48  */
49
50 /*
51  * physnode_map keeps track of the physical memory layout of a generic
52  * numa node on a 256Mb break (each element of the array will
53  * represent 256Mb of memory and will be marked by the node id.  so,
54  * if the first gig is on node 0, and the second gig is on node 1
55  * physnode_map will contain:
56  *
57  *     physnode_map[0-3] = 0;
58  *     physnode_map[4-7] = 1;
59  *     physnode_map[8- ] = -1;
60  */
61 s8 physnode_map[MAX_ELEMENTS] = { [0 ... (MAX_ELEMENTS - 1)] = -1};
62
63 unsigned long node_start_pfn[MAX_NUMNODES];
64 unsigned long node_end_pfn[MAX_NUMNODES];
65
66 extern unsigned long find_max_low_pfn(void);
67 extern void find_max_pfn(void);
68 extern void one_highpage_init(struct page *, int, int);
69
70 extern struct e820map e820;
71 extern unsigned long init_pg_tables_end;
72 extern unsigned long highend_pfn, highstart_pfn;
73 extern unsigned long max_low_pfn;
74 extern unsigned long totalram_pages;
75 extern unsigned long totalhigh_pages;
76
77 #define LARGE_PAGE_BYTES (PTRS_PER_PTE * PAGE_SIZE)
78
79 unsigned long node_remap_start_pfn[MAX_NUMNODES];
80 unsigned long node_remap_size[MAX_NUMNODES];
81 unsigned long node_remap_offset[MAX_NUMNODES];
82 void *node_remap_start_vaddr[MAX_NUMNODES];
83 void set_pmd_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags);
84
85 /*
86  * FLAT - support for basic PC memory model with discontig enabled, essentially
87  *        a single node with all available processors in it with a flat
88  *        memory map.
89  */
90 int __init get_memcfg_numa_flat(void)
91 {
92         printk("NUMA - single node, flat memory mode\n");
93
94         /* Run the memory configuration and find the top of memory. */
95         find_max_pfn();
96         node_start_pfn[0] = 0;
97         node_end_pfn[0] = max_pfn;
98
99         /* Indicate there is one node available. */
100         nodes_clear(node_online_map);
101         node_set_online(0);
102         return 1;
103 }
104
105 /*
106  * Find the highest page frame number we have available for the node
107  */
108 static void __init find_max_pfn_node(int nid)
109 {
110         if (node_end_pfn[nid] > max_pfn)
111                 node_end_pfn[nid] = max_pfn;
112         /*
113          * if a user has given mem=XXXX, then we need to make sure 
114          * that the node _starts_ before that, too, not just ends
115          */
116         if (node_start_pfn[nid] > max_pfn)
117                 node_start_pfn[nid] = max_pfn;
118         if (node_start_pfn[nid] > node_end_pfn[nid])
119                 BUG();
120 }
121
122 /* 
123  * Allocate memory for the pg_data_t for this node via a crude pre-bootmem
124  * method.  For node zero take this from the bottom of memory, for
125  * subsequent nodes place them at node_remap_start_vaddr which contains
126  * node local data in physically node local memory.  See setup_memory()
127  * for details.
128  */
129 static void __init allocate_pgdat(int nid)
130 {
131         if (nid)
132                 NODE_DATA(nid) = (pg_data_t *)node_remap_start_vaddr[nid];
133         else {
134                 NODE_DATA(nid) = (pg_data_t *)(__va(min_low_pfn << PAGE_SHIFT));
135                 min_low_pfn += PFN_UP(sizeof(pg_data_t));
136                 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
137         }
138 }
139
140 /*
141  * Register fully available low RAM pages with the bootmem allocator.
142  */
143 static void __init register_bootmem_low_pages(unsigned long system_max_low_pfn)
144 {
145         int i;
146
147         for (i = 0; i < e820.nr_map; i++) {
148                 unsigned long curr_pfn, last_pfn, size;
149                 /*
150                  * Reserve usable low memory
151                  */
152                 if (e820.map[i].type != E820_RAM)
153                         continue;
154                 /*
155                  * We are rounding up the start address of usable memory:
156                  */
157                 curr_pfn = PFN_UP(e820.map[i].addr);
158                 if (curr_pfn >= system_max_low_pfn)
159                         continue;
160                 /*
161                  * ... and at the end of the usable range downwards:
162                  */
163                 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
164
165                 if (last_pfn > system_max_low_pfn)
166                         last_pfn = system_max_low_pfn;
167
168                 /*
169                  * .. finally, did all the rounding and playing
170                  * around just make the area go away?
171                  */
172                 if (last_pfn <= curr_pfn)
173                         continue;
174
175                 size = last_pfn - curr_pfn;
176                 free_bootmem_node(NODE_DATA(0), PFN_PHYS(curr_pfn), PFN_PHYS(size));
177         }
178 }
179
180 void __init remap_numa_kva(void)
181 {
182         void *vaddr;
183         unsigned long pfn;
184         int node;
185
186         for_each_online_node(node) {
187                 if (node == 0)
188                         continue;
189                 for (pfn=0; pfn < node_remap_size[node]; pfn += PTRS_PER_PTE) {
190                         vaddr = node_remap_start_vaddr[node]+(pfn<<PAGE_SHIFT);
191                         set_pmd_pfn((ulong) vaddr, 
192                                 node_remap_start_pfn[node] + pfn, 
193                                 PAGE_KERNEL_LARGE);
194                 }
195         }
196 }
197
198 static unsigned long calculate_numa_remap_pages(void)
199 {
200         int nid;
201         unsigned long size, reserve_pages = 0;
202
203         for_each_online_node(nid) {
204                 if (nid == 0)
205                         continue;
206                 /* calculate the size of the mem_map needed in bytes */
207                 size = (node_end_pfn[nid] - node_start_pfn[nid] + 1) 
208                         * sizeof(struct page) + sizeof(pg_data_t);
209                 /* convert size to large (pmd size) pages, rounding up */
210                 size = (size + LARGE_PAGE_BYTES - 1) / LARGE_PAGE_BYTES;
211                 /* now the roundup is correct, convert to PAGE_SIZE pages */
212                 size = size * PTRS_PER_PTE;
213                 printk("Reserving %ld pages of KVA for lmem_map of node %d\n",
214                                 size, nid);
215                 node_remap_size[nid] = size;
216                 reserve_pages += size;
217                 node_remap_offset[nid] = reserve_pages;
218                 printk("Shrinking node %d from %ld pages to %ld pages\n",
219                         nid, node_end_pfn[nid], node_end_pfn[nid] - size);
220                 node_end_pfn[nid] -= size;
221                 node_remap_start_pfn[nid] = node_end_pfn[nid];
222         }
223         printk("Reserving total of %ld pages for numa KVA remap\n",
224                         reserve_pages);
225         return reserve_pages;
226 }
227
228 /*
229  * workaround for Dell systems that neglect to reserve EBDA
230  */
231 static void __init reserve_ebda_region_node(void)
232 {
233         unsigned int addr;
234         addr = get_bios_ebda();
235         if (addr)
236                 reserve_bootmem_node(NODE_DATA(0), addr, PAGE_SIZE);
237 }
238
239 unsigned long __init setup_memory(void)
240 {
241         int nid;
242         unsigned long bootmap_size, system_start_pfn, system_max_low_pfn;
243         unsigned long reserve_pages, pfn;
244
245         /*
246          * When mapping a NUMA machine we allocate the node_mem_map arrays
247          * from node local memory.  They are then mapped directly into KVA
248          * between zone normal and vmalloc space.  Calculate the size of
249          * this space and use it to adjust the boundry between ZONE_NORMAL
250          * and ZONE_HIGHMEM.
251          */
252         get_memcfg_numa();
253
254         /* Fill in the physnode_map */
255         for_each_online_node(nid) {
256                 printk("Node: %d, start_pfn: %ld, end_pfn: %ld\n",
257                                 nid, node_start_pfn[nid], node_end_pfn[nid]);
258                 printk("  Setting physnode_map array to node %d for pfns:\n  ",
259                                 nid);
260                 for (pfn = node_start_pfn[nid]; pfn < node_end_pfn[nid];
261                                         pfn += PAGES_PER_ELEMENT) {
262                         physnode_map[pfn / PAGES_PER_ELEMENT] = nid;
263                         printk("%ld ", pfn);
264                 }
265                 printk("\n");
266         }
267
268         reserve_pages = calculate_numa_remap_pages();
269
270         /* partially used pages are not usable - thus round upwards */
271         system_start_pfn = min_low_pfn = PFN_UP(init_pg_tables_end);
272
273         find_max_pfn();
274         system_max_low_pfn = max_low_pfn = find_max_low_pfn() - reserve_pages;
275         printk("reserve_pages = %ld find_max_low_pfn() ~ %ld\n",
276                         reserve_pages, max_low_pfn + reserve_pages);
277         printk("max_pfn = %ld\n", max_pfn);
278 #ifdef CONFIG_HIGHMEM
279         highstart_pfn = highend_pfn = max_pfn;
280         if (max_pfn > system_max_low_pfn)
281                 highstart_pfn = system_max_low_pfn;
282         printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
283                pages_to_mb(highend_pfn - highstart_pfn));
284 #endif
285         printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
286                         pages_to_mb(system_max_low_pfn));
287         printk("min_low_pfn = %ld, max_low_pfn = %ld, highstart_pfn = %ld\n", 
288                         min_low_pfn, max_low_pfn, highstart_pfn);
289
290         printk("Low memory ends at vaddr %08lx\n",
291                         (ulong) pfn_to_kaddr(max_low_pfn));
292         for_each_online_node(nid) {
293                 node_remap_start_vaddr[nid] = pfn_to_kaddr(
294                         (highstart_pfn + reserve_pages) - node_remap_offset[nid]);
295                 allocate_pgdat(nid);
296                 printk ("node %d will remap to vaddr %08lx - %08lx\n", nid,
297                         (ulong) node_remap_start_vaddr[nid],
298                         (ulong) pfn_to_kaddr(highstart_pfn + reserve_pages
299                             - node_remap_offset[nid] + node_remap_size[nid]));
300         }
301         printk("High memory starts at vaddr %08lx\n",
302                         (ulong) pfn_to_kaddr(highstart_pfn));
303         vmalloc_earlyreserve = reserve_pages * PAGE_SIZE;
304         for_each_online_node(nid)
305                 find_max_pfn_node(nid);
306
307         NODE_DATA(0)->bdata = &node0_bdata;
308
309         /*
310          * Initialize the boot-time allocator (with low memory only):
311          */
312         bootmap_size = init_bootmem_node(NODE_DATA(0), min_low_pfn, 0, system_max_low_pfn);
313
314         register_bootmem_low_pages(system_max_low_pfn);
315
316         /*
317          * Reserve the bootmem bitmap itself as well. We do this in two
318          * steps (first step was init_bootmem()) because this catches
319          * the (very unlikely) case of us accidentally initializing the
320          * bootmem allocator with an invalid RAM area.
321          */
322         reserve_bootmem_node(NODE_DATA(0), HIGH_MEMORY, (PFN_PHYS(min_low_pfn) +
323                  bootmap_size + PAGE_SIZE-1) - (HIGH_MEMORY));
324
325         /*
326          * reserve physical page 0 - it's a special BIOS page on many boxes,
327          * enabling clean reboots, SMP operation, laptop functions.
328          */
329         reserve_bootmem_node(NODE_DATA(0), 0, PAGE_SIZE);
330
331         /*
332          * But first pinch a few for the stack/trampoline stuff
333          * FIXME: Don't need the extra page at 4K, but need to fix
334          * trampoline before removing it. (see the GDT stuff)
335          */
336         reserve_bootmem_node(NODE_DATA(0), PAGE_SIZE, PAGE_SIZE);
337
338         /* reserve EBDA region, it's a 4K region */
339         reserve_ebda_region_node();
340
341 #ifdef CONFIG_ACPI_SLEEP
342         /*
343          * Reserve low memory region for sleep support.
344          */
345         acpi_reserve_bootmem();
346 #endif
347
348         /*
349          * Find and reserve possible boot-time SMP configuration:
350          */
351         find_smp_config();
352
353 #ifdef CONFIG_BLK_DEV_INITRD
354         if (LOADER_TYPE && INITRD_START) {
355                 if (INITRD_START + INITRD_SIZE <= (system_max_low_pfn << PAGE_SHIFT)) {
356                         reserve_bootmem_node(NODE_DATA(0), INITRD_START, INITRD_SIZE);
357                         initrd_start =
358                                 INITRD_START ? INITRD_START + PAGE_OFFSET : 0;
359                         initrd_end = initrd_start+INITRD_SIZE;
360                 }
361                 else {
362                         printk(KERN_ERR "initrd extends beyond end of memory "
363                             "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
364                             INITRD_START + INITRD_SIZE,
365                             system_max_low_pfn << PAGE_SHIFT);
366                         initrd_start = 0;
367                 }
368         }
369 #endif
370         return system_max_low_pfn;
371 }
372
373 void __init zone_sizes_init(void)
374 {
375         int nid;
376
377         /*
378          * Insert nodes into pgdat_list backward so they appear in order.
379          * Clobber node 0's links and NULL out pgdat_list before starting.
380          */
381         pgdat_list = NULL;
382         for (nid = MAX_NUMNODES - 1; nid >= 0; nid--) {
383                 if (!node_online(nid))
384                         continue;
385                 if (nid)
386                         memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
387                 NODE_DATA(nid)->pgdat_next = pgdat_list;
388                 pgdat_list = NODE_DATA(nid);
389         }
390
391         for_each_online_node(nid) {
392                 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
393                 unsigned long *zholes_size;
394                 unsigned int max_dma;
395
396                 unsigned long low = max_low_pfn;
397                 unsigned long start = node_start_pfn[nid];
398                 unsigned long high = node_end_pfn[nid];
399
400                 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
401
402                 if (start > low) {
403 #ifdef CONFIG_HIGHMEM
404                         BUG_ON(start > high);
405                         zones_size[ZONE_HIGHMEM] = high - start;
406 #endif
407                 } else {
408                         if (low < max_dma)
409                                 zones_size[ZONE_DMA] = low;
410                         else {
411                                 BUG_ON(max_dma > low);
412                                 BUG_ON(low > high);
413                                 zones_size[ZONE_DMA] = max_dma;
414                                 zones_size[ZONE_NORMAL] = low - max_dma;
415 #ifdef CONFIG_HIGHMEM
416                                 zones_size[ZONE_HIGHMEM] = high - low;
417 #endif
418                         }
419                 }
420                 zholes_size = get_zholes_size(nid);
421                 /*
422                  * We let the lmem_map for node 0 be allocated from the
423                  * normal bootmem allocator, but other nodes come from the
424                  * remapped KVA area - mbligh
425                  */
426                 if (!nid)
427                         free_area_init_node(nid, NODE_DATA(nid),
428                                         zones_size, start, zholes_size);
429                 else {
430                         unsigned long lmem_map;
431                         lmem_map = (unsigned long)node_remap_start_vaddr[nid];
432                         lmem_map += sizeof(pg_data_t) + PAGE_SIZE - 1;
433                         lmem_map &= PAGE_MASK;
434                         NODE_DATA(nid)->node_mem_map = (struct page *)lmem_map;
435                         free_area_init_node(nid, NODE_DATA(nid), zones_size,
436                                 start, zholes_size);
437                 }
438         }
439         return;
440 }
441
442 void __init set_highmem_pages_init(int bad_ppro) 
443 {
444 #ifdef CONFIG_HIGHMEM
445         struct zone *zone;
446
447         for_each_zone(zone) {
448                 unsigned long node_pfn, node_high_size, zone_start_pfn;
449                 struct page * zone_mem_map;
450                 
451                 if (!is_highmem(zone))
452                         continue;
453
454                 printk("Initializing %s for node %d\n", zone->name,
455                         zone->zone_pgdat->node_id);
456
457                 node_high_size = zone->spanned_pages;
458                 zone_mem_map = zone->zone_mem_map;
459                 zone_start_pfn = zone->zone_start_pfn;
460
461                 for (node_pfn = 0; node_pfn < node_high_size; node_pfn++) {
462                         one_highpage_init((struct page *)(zone_mem_map + node_pfn),
463                                           zone_start_pfn + node_pfn, bad_ppro);
464                 }
465         }
466         totalram_pages += totalhigh_pages;
467 #endif
468 }
469
470 void __init set_max_mapnr_init(void)
471 {
472 #ifdef CONFIG_HIGHMEM
473         num_physpages = highend_pfn;
474 #else
475         num_physpages = max_low_pfn;
476 #endif
477 }