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