/spare/repo/libata-dev branch 'v2.6.13'
[linux-2.6] / arch / alpha / mm / numa.c
1 /*
2  *  linux/arch/alpha/mm/numa.c
3  *
4  *  DISCONTIGMEM NUMA alpha support.
5  *
6  *  Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
7  */
8
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/bootmem.h>
14 #include <linux/swap.h>
15 #include <linux/initrd.h>
16
17 #include <asm/hwrpb.h>
18 #include <asm/pgalloc.h>
19
20 pg_data_t node_data[MAX_NUMNODES];
21 bootmem_data_t node_bdata[MAX_NUMNODES];
22
23 #undef DEBUG_DISCONTIG
24 #ifdef DEBUG_DISCONTIG
25 #define DBGDCONT(args...) printk(args)
26 #else
27 #define DBGDCONT(args...)
28 #endif
29
30 #define PFN_UP(x)       (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
31 #define PFN_DOWN(x)     ((x) >> PAGE_SHIFT)
32 #define PFN_PHYS(x)     ((x) << PAGE_SHIFT)
33 #define for_each_mem_cluster(memdesc, cluster, i)               \
34         for ((cluster) = (memdesc)->cluster, (i) = 0;           \
35              (i) < (memdesc)->numclusters; (i)++, (cluster)++)
36
37 static void __init show_mem_layout(void)
38 {
39         struct memclust_struct * cluster;
40         struct memdesc_struct * memdesc;
41         int i;
42
43         /* Find free clusters, and init and free the bootmem accordingly.  */
44         memdesc = (struct memdesc_struct *)
45           (hwrpb->mddt_offset + (unsigned long) hwrpb);
46
47         printk("Raw memory layout:\n");
48         for_each_mem_cluster(memdesc, cluster, i) {
49                 printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
50                        i, cluster->usage, cluster->start_pfn,
51                        cluster->start_pfn + cluster->numpages);
52         }
53 }
54
55 static void __init
56 setup_memory_node(int nid, void *kernel_end)
57 {
58         extern unsigned long mem_size_limit;
59         struct memclust_struct * cluster;
60         struct memdesc_struct * memdesc;
61         unsigned long start_kernel_pfn, end_kernel_pfn;
62         unsigned long bootmap_size, bootmap_pages, bootmap_start;
63         unsigned long start, end;
64         unsigned long node_pfn_start, node_pfn_end;
65         unsigned long node_min_pfn, node_max_pfn;
66         int i;
67         unsigned long node_datasz = PFN_UP(sizeof(pg_data_t));
68         int show_init = 0;
69
70         /* Find the bounds of current node */
71         node_pfn_start = (node_mem_start(nid)) >> PAGE_SHIFT;
72         node_pfn_end = node_pfn_start + (node_mem_size(nid) >> PAGE_SHIFT);
73         
74         /* Find free clusters, and init and free the bootmem accordingly.  */
75         memdesc = (struct memdesc_struct *)
76           (hwrpb->mddt_offset + (unsigned long) hwrpb);
77
78         /* find the bounds of this node (node_min_pfn/node_max_pfn) */
79         node_min_pfn = ~0UL;
80         node_max_pfn = 0UL;
81         for_each_mem_cluster(memdesc, cluster, i) {
82                 /* Bit 0 is console/PALcode reserved.  Bit 1 is
83                    non-volatile memory -- we might want to mark
84                    this for later.  */
85                 if (cluster->usage & 3)
86                         continue;
87
88                 start = cluster->start_pfn;
89                 end = start + cluster->numpages;
90
91                 if (start >= node_pfn_end || end <= node_pfn_start)
92                         continue;
93
94                 if (!show_init) {
95                         show_init = 1;
96                         printk("Initializing bootmem allocator on Node ID %d\n", nid);
97                 }
98                 printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
99                        i, cluster->usage, cluster->start_pfn,
100                        cluster->start_pfn + cluster->numpages);
101
102                 if (start < node_pfn_start)
103                         start = node_pfn_start;
104                 if (end > node_pfn_end)
105                         end = node_pfn_end;
106
107                 if (start < node_min_pfn)
108                         node_min_pfn = start;
109                 if (end > node_max_pfn)
110                         node_max_pfn = end;
111         }
112
113         if (mem_size_limit && node_max_pfn > mem_size_limit) {
114                 static int msg_shown = 0;
115                 if (!msg_shown) {
116                         msg_shown = 1;
117                         printk("setup: forcing memory size to %ldK (from %ldK).\n",
118                                mem_size_limit << (PAGE_SHIFT - 10),
119                                node_max_pfn    << (PAGE_SHIFT - 10));
120                 }
121                 node_max_pfn = mem_size_limit;
122         }
123
124         if (node_min_pfn >= node_max_pfn)
125                 return;
126
127         /* Update global {min,max}_low_pfn from node information. */
128         if (node_min_pfn < min_low_pfn)
129                 min_low_pfn = node_min_pfn;
130         if (node_max_pfn > max_low_pfn)
131                 max_pfn = max_low_pfn = node_max_pfn;
132
133         num_physpages += node_max_pfn - node_min_pfn;
134
135 #if 0 /* we'll try this one again in a little while */
136         /* Cute trick to make sure our local node data is on local memory */
137         node_data[nid] = (pg_data_t *)(__va(node_min_pfn << PAGE_SHIFT));
138 #endif
139         /* Quasi-mark the pg_data_t as in-use */
140         node_min_pfn += node_datasz;
141         if (node_min_pfn >= node_max_pfn) {
142                 printk(" not enough mem to reserve NODE_DATA");
143                 return;
144         }
145         NODE_DATA(nid)->bdata = &node_bdata[nid];
146
147         printk(" Detected node memory:   start %8lu, end %8lu\n",
148                node_min_pfn, node_max_pfn);
149
150         DBGDCONT(" DISCONTIG: node_data[%d]   is at 0x%p\n", nid, NODE_DATA(nid));
151         DBGDCONT(" DISCONTIG: NODE_DATA(%d)->bdata is at 0x%p\n", nid, NODE_DATA(nid)->bdata);
152
153         /* Find the bounds of kernel memory.  */
154         start_kernel_pfn = PFN_DOWN(KERNEL_START_PHYS);
155         end_kernel_pfn = PFN_UP(virt_to_phys(kernel_end));
156         bootmap_start = -1;
157
158         if (!nid && (node_max_pfn < end_kernel_pfn || node_min_pfn > start_kernel_pfn))
159                 panic("kernel loaded out of ram");
160
161         /* Zone start phys-addr must be 2^(MAX_ORDER-1) aligned.
162            Note that we round this down, not up - node memory
163            has much larger alignment than 8Mb, so it's safe. */
164         node_min_pfn &= ~((1UL << (MAX_ORDER-1))-1);
165
166         /* We need to know how many physically contiguous pages
167            we'll need for the bootmap.  */
168         bootmap_pages = bootmem_bootmap_pages(node_max_pfn-node_min_pfn);
169
170         /* Now find a good region where to allocate the bootmap.  */
171         for_each_mem_cluster(memdesc, cluster, i) {
172                 if (cluster->usage & 3)
173                         continue;
174
175                 start = cluster->start_pfn;
176                 end = start + cluster->numpages;
177
178                 if (start >= node_max_pfn || end <= node_min_pfn)
179                         continue;
180
181                 if (end > node_max_pfn)
182                         end = node_max_pfn;
183                 if (start < node_min_pfn)
184                         start = node_min_pfn;
185
186                 if (start < start_kernel_pfn) {
187                         if (end > end_kernel_pfn
188                             && end - end_kernel_pfn >= bootmap_pages) {
189                                 bootmap_start = end_kernel_pfn;
190                                 break;
191                         } else if (end > start_kernel_pfn)
192                                 end = start_kernel_pfn;
193                 } else if (start < end_kernel_pfn)
194                         start = end_kernel_pfn;
195                 if (end - start >= bootmap_pages) {
196                         bootmap_start = start;
197                         break;
198                 }
199         }
200
201         if (bootmap_start == -1)
202                 panic("couldn't find a contigous place for the bootmap");
203
204         /* Allocate the bootmap and mark the whole MM as reserved.  */
205         bootmap_size = init_bootmem_node(NODE_DATA(nid), bootmap_start,
206                                          node_min_pfn, node_max_pfn);
207         DBGDCONT(" bootmap_start %lu, bootmap_size %lu, bootmap_pages %lu\n",
208                  bootmap_start, bootmap_size, bootmap_pages);
209
210         /* Mark the free regions.  */
211         for_each_mem_cluster(memdesc, cluster, i) {
212                 if (cluster->usage & 3)
213                         continue;
214
215                 start = cluster->start_pfn;
216                 end = cluster->start_pfn + cluster->numpages;
217
218                 if (start >= node_max_pfn || end <= node_min_pfn)
219                         continue;
220
221                 if (end > node_max_pfn)
222                         end = node_max_pfn;
223                 if (start < node_min_pfn)
224                         start = node_min_pfn;
225
226                 if (start < start_kernel_pfn) {
227                         if (end > end_kernel_pfn) {
228                                 free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start),
229                                              (PFN_PHYS(start_kernel_pfn)
230                                               - PFN_PHYS(start)));
231                                 printk(" freeing pages %ld:%ld\n",
232                                        start, start_kernel_pfn);
233                                 start = end_kernel_pfn;
234                         } else if (end > start_kernel_pfn)
235                                 end = start_kernel_pfn;
236                 } else if (start < end_kernel_pfn)
237                         start = end_kernel_pfn;
238                 if (start >= end)
239                         continue;
240
241                 free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start), PFN_PHYS(end) - PFN_PHYS(start));
242                 printk(" freeing pages %ld:%ld\n", start, end);
243         }
244
245         /* Reserve the bootmap memory.  */
246         reserve_bootmem_node(NODE_DATA(nid), PFN_PHYS(bootmap_start), bootmap_size);
247         printk(" reserving pages %ld:%ld\n", bootmap_start, bootmap_start+PFN_UP(bootmap_size));
248
249         node_set_online(nid);
250 }
251
252 void __init
253 setup_memory(void *kernel_end)
254 {
255         int nid;
256
257         show_mem_layout();
258
259         nodes_clear(node_online_map);
260
261         min_low_pfn = ~0UL;
262         max_low_pfn = 0UL;
263         for (nid = 0; nid < MAX_NUMNODES; nid++)
264                 setup_memory_node(nid, kernel_end);
265
266 #ifdef CONFIG_BLK_DEV_INITRD
267         initrd_start = INITRD_START;
268         if (initrd_start) {
269                 extern void *move_initrd(unsigned long);
270
271                 initrd_end = initrd_start+INITRD_SIZE;
272                 printk("Initial ramdisk at: 0x%p (%lu bytes)\n",
273                        (void *) initrd_start, INITRD_SIZE);
274
275                 if ((void *)initrd_end > phys_to_virt(PFN_PHYS(max_low_pfn))) {
276                         if (!move_initrd(PFN_PHYS(max_low_pfn)))
277                                 printk("initrd extends beyond end of memory "
278                                        "(0x%08lx > 0x%p)\ndisabling initrd\n",
279                                        initrd_end,
280                                        phys_to_virt(PFN_PHYS(max_low_pfn)));
281                 } else {
282                         nid = kvaddr_to_nid(initrd_start);
283                         reserve_bootmem_node(NODE_DATA(nid),
284                                              virt_to_phys((void *)initrd_start),
285                                              INITRD_SIZE);
286                 }
287         }
288 #endif /* CONFIG_BLK_DEV_INITRD */
289 }
290
291 void __init paging_init(void)
292 {
293         unsigned int    nid;
294         unsigned long   zones_size[MAX_NR_ZONES] = {0, };
295         unsigned long   dma_local_pfn;
296
297         /*
298          * The old global MAX_DMA_ADDRESS per-arch API doesn't fit
299          * in the NUMA model, for now we convert it to a pfn and
300          * we interpret this pfn as a local per-node information.
301          * This issue isn't very important since none of these machines
302          * have legacy ISA slots anyways.
303          */
304         dma_local_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
305
306         for_each_online_node(nid) {
307                 unsigned long start_pfn = node_bdata[nid].node_boot_start >> PAGE_SHIFT;
308                 unsigned long end_pfn = node_bdata[nid].node_low_pfn;
309
310                 if (dma_local_pfn >= end_pfn - start_pfn)
311                         zones_size[ZONE_DMA] = end_pfn - start_pfn;
312                 else {
313                         zones_size[ZONE_DMA] = dma_local_pfn;
314                         zones_size[ZONE_NORMAL] = (end_pfn - start_pfn) - dma_local_pfn;
315                 }
316                 free_area_init_node(nid, NODE_DATA(nid), zones_size, start_pfn, NULL);
317         }
318
319         /* Initialize the kernel's ZERO_PGE. */
320         memset((void *)ZERO_PGE, 0, PAGE_SIZE);
321 }
322
323 void __init mem_init(void)
324 {
325         unsigned long codesize, reservedpages, datasize, initsize, pfn;
326         extern int page_is_ram(unsigned long) __init;
327         extern char _text, _etext, _data, _edata;
328         extern char __init_begin, __init_end;
329         unsigned long nid, i;
330         high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
331
332         reservedpages = 0;
333         for_each_online_node(nid) {
334                 /*
335                  * This will free up the bootmem, ie, slot 0 memory
336                  */
337                 totalram_pages += free_all_bootmem_node(NODE_DATA(nid));
338
339                 pfn = NODE_DATA(nid)->node_start_pfn;
340                 for (i = 0; i < node_spanned_pages(nid); i++, pfn++)
341                         if (page_is_ram(pfn) &&
342                             PageReserved(nid_page_nr(nid, i)))
343                                 reservedpages++;
344         }
345
346         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
347         datasize =  (unsigned long) &_edata - (unsigned long) &_data;
348         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
349
350         printk("Memory: %luk/%luk available (%luk kernel code, %luk reserved, "
351                "%luk data, %luk init)\n",
352                (unsigned long)nr_free_pages() << (PAGE_SHIFT-10),
353                num_physpages << (PAGE_SHIFT-10),
354                codesize >> 10,
355                reservedpages << (PAGE_SHIFT-10),
356                datasize >> 10,
357                initsize >> 10);
358 #if 0
359         mem_stress();
360 #endif
361 }
362
363 void
364 show_mem(void)
365 {
366         long i,free = 0,total = 0,reserved = 0;
367         long shared = 0, cached = 0;
368         int nid;
369
370         printk("\nMem-info:\n");
371         show_free_areas();
372         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
373         for_each_online_node(nid) {
374                 i = node_spanned_pages(nid);
375                 while (i-- > 0) {
376                         struct page *page = nid_page_nr(nid, i);
377                         total++;
378                         if (PageReserved(page))
379                                 reserved++;
380                         else if (PageSwapCache(page))
381                                 cached++;
382                         else if (!page_count(page))
383                                 free++;
384                         else
385                                 shared += page_count(page) - 1;
386                 }
387         }
388         printk("%ld pages of RAM\n",total);
389         printk("%ld free pages\n",free);
390         printk("%ld reserved pages\n",reserved);
391         printk("%ld pages shared\n",shared);
392         printk("%ld pages swap cached\n",cached);
393 }