1
2
3
4
5
6
7
8
9#include <linux/types.h>
10#include <linux/kernel.h>
11#include <linux/mm.h>
12#include <linux/bootmem.h>
13#include <linux/swap.h>
14#include <linux/initrd.h>
15#include <linux/pfn.h>
16#include <linux/module.h>
17
18#include <asm/hwrpb.h>
19#include <asm/pgalloc.h>
20#include <asm/sections.h>
21
22pg_data_t node_data[MAX_NUMNODES];
23EXPORT_SYMBOL(node_data);
24
25#undef DEBUG_DISCONTIG
26#ifdef DEBUG_DISCONTIG
27#define DBGDCONT(args...) printk(args)
28#else
29#define DBGDCONT(args...)
30#endif
31
32#define for_each_mem_cluster(memdesc, _cluster, i) \
33 for ((_cluster) = (memdesc)->cluster, (i) = 0; \
34 (i) < (memdesc)->numclusters; (i)++, (_cluster)++)
35
36static void __init show_mem_layout(void)
37{
38 struct memclust_struct * cluster;
39 struct memdesc_struct * memdesc;
40 int i;
41
42
43 memdesc = (struct memdesc_struct *)
44 (hwrpb->mddt_offset + (unsigned long) hwrpb);
45
46 printk("Raw memory layout:\n");
47 for_each_mem_cluster(memdesc, cluster, i) {
48 printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
49 i, cluster->usage, cluster->start_pfn,
50 cluster->start_pfn + cluster->numpages);
51 }
52}
53
54static void __init
55setup_memory_node(int nid, void *kernel_end)
56{
57 extern unsigned long mem_size_limit;
58 struct memclust_struct * cluster;
59 struct memdesc_struct * memdesc;
60 unsigned long start_kernel_pfn, end_kernel_pfn;
61 unsigned long bootmap_size, bootmap_pages, bootmap_start;
62 unsigned long start, end;
63 unsigned long node_pfn_start, node_pfn_end;
64 unsigned long node_min_pfn, node_max_pfn;
65 int i;
66 unsigned long node_datasz = PFN_UP(sizeof(pg_data_t));
67 int show_init = 0;
68
69
70 node_pfn_start = (node_mem_start(nid)) >> PAGE_SHIFT;
71 node_pfn_end = node_pfn_start + (node_mem_size(nid) >> PAGE_SHIFT);
72
73
74 memdesc = (struct memdesc_struct *)
75 (hwrpb->mddt_offset + (unsigned long) hwrpb);
76
77
78 node_min_pfn = ~0UL;
79 node_max_pfn = 0UL;
80 for_each_mem_cluster(memdesc, cluster, i) {
81
82
83
84 if (cluster->usage & 3)
85 continue;
86
87 start = cluster->start_pfn;
88 end = start + cluster->numpages;
89
90 if (start >= node_pfn_end || end <= node_pfn_start)
91 continue;
92
93 if (!show_init) {
94 show_init = 1;
95 printk("Initializing bootmem allocator on Node ID %d\n", nid);
96 }
97 printk(" memcluster %2d, usage %1lx, start %8lu, end %8lu\n",
98 i, cluster->usage, cluster->start_pfn,
99 cluster->start_pfn + cluster->numpages);
100
101 if (start < node_pfn_start)
102 start = node_pfn_start;
103 if (end > node_pfn_end)
104 end = node_pfn_end;
105
106 if (start < node_min_pfn)
107 node_min_pfn = start;
108 if (end > node_max_pfn)
109 node_max_pfn = end;
110 }
111
112 if (mem_size_limit && node_max_pfn > mem_size_limit) {
113 static int msg_shown = 0;
114 if (!msg_shown) {
115 msg_shown = 1;
116 printk("setup: forcing memory size to %ldK (from %ldK).\n",
117 mem_size_limit << (PAGE_SHIFT - 10),
118 node_max_pfn << (PAGE_SHIFT - 10));
119 }
120 node_max_pfn = mem_size_limit;
121 }
122
123 if (node_min_pfn >= node_max_pfn)
124 return;
125
126
127 if (node_min_pfn < min_low_pfn)
128 min_low_pfn = node_min_pfn;
129 if (node_max_pfn > max_low_pfn)
130 max_pfn = max_low_pfn = node_max_pfn;
131
132 num_physpages += node_max_pfn - node_min_pfn;
133
134#if 0
135
136 node_data[nid] = (pg_data_t *)(__va(node_min_pfn << PAGE_SHIFT));
137#endif
138
139 node_min_pfn += node_datasz;
140 if (node_min_pfn >= node_max_pfn) {
141 printk(" not enough mem to reserve NODE_DATA");
142 return;
143 }
144 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
145
146 printk(" Detected node memory: start %8lu, end %8lu\n",
147 node_min_pfn, node_max_pfn);
148
149 DBGDCONT(" DISCONTIG: node_data[%d] is at 0x%p\n", nid, NODE_DATA(nid));
150 DBGDCONT(" DISCONTIG: NODE_DATA(%d)->bdata is at 0x%p\n", nid, NODE_DATA(nid)->bdata);
151
152
153 start_kernel_pfn = PFN_DOWN(KERNEL_START_PHYS);
154 end_kernel_pfn = PFN_UP(virt_to_phys(kernel_end));
155 bootmap_start = -1;
156
157 if (!nid && (node_max_pfn < end_kernel_pfn || node_min_pfn > start_kernel_pfn))
158 panic("kernel loaded out of ram");
159
160
161
162
163 node_min_pfn &= ~((1UL << (MAX_ORDER-1))-1);
164
165
166
167 bootmap_pages = bootmem_bootmap_pages(node_max_pfn-node_min_pfn);
168
169
170 for_each_mem_cluster(memdesc, cluster, i) {
171 if (cluster->usage & 3)
172 continue;
173
174 start = cluster->start_pfn;
175 end = start + cluster->numpages;
176
177 if (start >= node_max_pfn || end <= node_min_pfn)
178 continue;
179
180 if (end > node_max_pfn)
181 end = node_max_pfn;
182 if (start < node_min_pfn)
183 start = node_min_pfn;
184
185 if (start < start_kernel_pfn) {
186 if (end > end_kernel_pfn
187 && end - end_kernel_pfn >= bootmap_pages) {
188 bootmap_start = end_kernel_pfn;
189 break;
190 } else if (end > start_kernel_pfn)
191 end = start_kernel_pfn;
192 } else if (start < end_kernel_pfn)
193 start = end_kernel_pfn;
194 if (end - start >= bootmap_pages) {
195 bootmap_start = start;
196 break;
197 }
198 }
199
200 if (bootmap_start == -1)
201 panic("couldn't find a contiguous place for the bootmap");
202
203
204 bootmap_size = init_bootmem_node(NODE_DATA(nid), bootmap_start,
205 node_min_pfn, node_max_pfn);
206 DBGDCONT(" bootmap_start %lu, bootmap_size %lu, bootmap_pages %lu\n",
207 bootmap_start, bootmap_size, bootmap_pages);
208
209
210 for_each_mem_cluster(memdesc, cluster, i) {
211 if (cluster->usage & 3)
212 continue;
213
214 start = cluster->start_pfn;
215 end = cluster->start_pfn + cluster->numpages;
216
217 if (start >= node_max_pfn || end <= node_min_pfn)
218 continue;
219
220 if (end > node_max_pfn)
221 end = node_max_pfn;
222 if (start < node_min_pfn)
223 start = node_min_pfn;
224
225 if (start < start_kernel_pfn) {
226 if (end > end_kernel_pfn) {
227 free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start),
228 (PFN_PHYS(start_kernel_pfn)
229 - PFN_PHYS(start)));
230 printk(" freeing pages %ld:%ld\n",
231 start, start_kernel_pfn);
232 start = end_kernel_pfn;
233 } else if (end > start_kernel_pfn)
234 end = start_kernel_pfn;
235 } else if (start < end_kernel_pfn)
236 start = end_kernel_pfn;
237 if (start >= end)
238 continue;
239
240 free_bootmem_node(NODE_DATA(nid), PFN_PHYS(start), PFN_PHYS(end) - PFN_PHYS(start));
241 printk(" freeing pages %ld:%ld\n", start, end);
242 }
243
244
245 reserve_bootmem_node(NODE_DATA(nid), PFN_PHYS(bootmap_start),
246 bootmap_size, BOOTMEM_DEFAULT);
247 printk(" reserving pages %ld:%ld\n", bootmap_start, bootmap_start+PFN_UP(bootmap_size));
248
249 node_set_online(nid);
250}
251
252void __init
253setup_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, BOOTMEM_DEFAULT);
286 }
287 }
288#endif
289}
290
291void __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
299
300
301
302
303
304 dma_local_pfn = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
305
306 for_each_online_node(nid) {
307 bootmem_data_t *bdata = &bootmem_node_data[nid];
308 unsigned long start_pfn = bdata->node_min_pfn;
309 unsigned long end_pfn = bdata->node_low_pfn;
310
311 if (dma_local_pfn >= end_pfn - start_pfn)
312 zones_size[ZONE_DMA] = end_pfn - start_pfn;
313 else {
314 zones_size[ZONE_DMA] = dma_local_pfn;
315 zones_size[ZONE_NORMAL] = (end_pfn - start_pfn) - dma_local_pfn;
316 }
317 node_set_state(nid, N_NORMAL_MEMORY);
318 free_area_init_node(nid, zones_size, start_pfn, NULL);
319 }
320
321
322 memset((void *)ZERO_PGE, 0, PAGE_SIZE);
323}
324
325void __init mem_init(void)
326{
327 unsigned long codesize, reservedpages, datasize, initsize, pfn;
328 extern int page_is_ram(unsigned long) __init;
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
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 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