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