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