2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2000 by Ralf Baechle
7 * Copyright (C) 2000 by Silicon Graphics, Inc.
8 * Copyright (C) 2004 by Christoph Hellwig
10 * On SGI IP27 the ARC memory configuration data is completly bogus but
11 * alternate easier to use mechanisms are available.
13 #include <linux/init.h>
14 #include <linux/kernel.h>
16 #include <linux/mmzone.h>
17 #include <linux/swap.h>
18 #include <linux/bootmem.h>
20 #include <asm/sections.h>
22 #include <asm/sn/arch.h>
23 #include <asm/sn/hub.h>
24 #include <asm/sn/klconfig.h>
25 #include <asm/sn/sn_private.h>
28 #define PFN_UP(x) (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
30 #define SLOT_PFNSHIFT (SLOT_SHIFT - PAGE_SHIFT)
31 #define PFN_NASIDSHFT (NASID_SHFT - PAGE_SHIFT)
33 #define SLOT_IGNORED 0xffff
35 static short __initdata slot_lastfilled_cache[MAX_COMPACT_NODES];
36 static unsigned short __initdata slot_psize_cache[MAX_COMPACT_NODES][MAX_MEM_SLOTS];
37 static struct bootmem_data __initdata plat_node_bdata[MAX_COMPACT_NODES];
39 struct pglist_data *node_data[MAX_COMPACT_NODES];
40 struct hub_data *hub_data[MAX_COMPACT_NODES];
42 static pfn_t __init slot_getbasepfn(cnodeid_t cnode, int slot)
44 nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
46 return ((pfn_t)nasid << PFN_NASIDSHFT) | (slot << SLOT_PFNSHIFT);
50 * Return the number of pages of memory provided by the given slot
51 * on the specified node.
53 static pfn_t __init slot_getsize(cnodeid_t node, int slot)
55 return (pfn_t) slot_psize_cache[node][slot];
59 * Return highest slot filled
61 static int __init node_getlastslot(cnodeid_t node)
63 return (int) slot_lastfilled_cache[node];
67 * Return the pfn of the last free page of memory on a node.
69 static pfn_t __init node_getmaxclick(cnodeid_t node)
75 * Start at the top slot. When we find a slot with memory in it,
78 for (slot = (MAX_MEM_SLOTS - 1); slot >= 0; slot--) {
79 if ((slot_psize = slot_getsize(node, slot))) {
80 if (slot_psize == SLOT_IGNORED)
82 /* Return the basepfn + the slot size, minus 1. */
83 return slot_getbasepfn(node, slot) + slot_psize - 1;
88 * If there's no memory on the node, return 0. This is likely
94 static pfn_t __init slot_psize_compute(cnodeid_t node, int slot)
101 nasid = COMPACT_TO_NASID_NODEID(node);
102 /* Find the node board */
103 brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
107 /* Get the memory bank structure */
108 banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK);
112 /* Size in _Megabytes_ */
113 size = (unsigned long)banks->membnk_bnksz[slot/4];
115 /* hack for 128 dimm banks */
118 size <<= 20; /* size in bytes */
119 return(size >> PAGE_SHIFT);
125 return size >> PAGE_SHIFT;
129 static void __init szmem(void)
131 pfn_t slot_psize, slot0sz = 0, nodebytes; /* Hack to detect problem configs */
137 for (node = 0; node < numnodes; node++) {
138 ignore = nodebytes = 0;
139 for (slot = 0; slot < MAX_MEM_SLOTS; slot++) {
140 slot_psize = slot_psize_compute(node, slot);
142 slot0sz = slot_psize;
144 * We need to refine the hack when we have replicated
147 nodebytes += (1LL << SLOT_SHIFT);
148 if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
149 (slot0sz << PAGE_SHIFT))
151 if (ignore && slot_psize) {
152 printk("Ignoring slot %d onwards on node %d\n",
154 slot_psize_cache[node][slot] = SLOT_IGNORED;
155 slot = MAX_MEM_SLOTS;
158 num_physpages += slot_psize;
159 slot_psize_cache[node][slot] =
160 (unsigned short) slot_psize;
162 slot_lastfilled_cache[node] = slot;
168 * Currently, the intranode memory hole support assumes that each slot
169 * contains at least 32 MBytes of memory. We assume all bootmem data
170 * fits on the first slot.
172 void __init prom_meminit(void)
179 for (node = 0; node < numnodes; node++) {
180 pfn_t slot_firstpfn = slot_getbasepfn(node, 0);
181 pfn_t slot_lastpfn = slot_firstpfn + slot_getsize(node, 0);
182 pfn_t slot_freepfn = node_getfirstfree(node);
183 unsigned long bootmap_size;
186 * Allocate the node data structures on the node first.
188 node_data[node] = __va(slot_freepfn << PAGE_SHIFT);
189 node_data[node]->bdata = &plat_node_bdata[node];
191 hub_data[node] = (struct hub_data *)(node_data[node] + 1);
193 cpus_clear(hub_data[node]->h_cpus);
195 slot_freepfn += PFN_UP(sizeof(struct pglist_data) +
196 sizeof(struct hub_data));
198 bootmap_size = init_bootmem_node(NODE_DATA(node), slot_freepfn,
199 slot_firstpfn, slot_lastpfn);
200 free_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
201 (slot_lastpfn - slot_firstpfn) << PAGE_SHIFT);
202 reserve_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
203 ((slot_freepfn - slot_firstpfn) << PAGE_SHIFT) + bootmap_size);
207 unsigned long __init prom_free_prom_memory(void)
209 /* We got nothing to free here ... */
213 extern void pagetable_init(void);
214 extern unsigned long setup_zero_pages(void);
216 void __init paging_init(void)
218 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
223 for (node = 0; node < numnodes; node++) {
224 pfn_t start_pfn = slot_getbasepfn(node, 0);
225 pfn_t end_pfn = node_getmaxclick(node) + 1;
227 zones_size[ZONE_DMA] = end_pfn - start_pfn;
228 free_area_init_node(node, NODE_DATA(node),
229 zones_size, start_pfn, NULL);
231 if (end_pfn > max_low_pfn)
232 max_low_pfn = end_pfn;
236 void __init mem_init(void)
238 unsigned long codesize, datasize, initsize, tmp;
241 high_memory = (void *) __va(num_physpages << PAGE_SHIFT);
243 for (node = 0; node < numnodes; node++) {
244 unsigned slot, numslots;
245 struct page *end, *p;
248 * This will free up the bootmem, ie, slot 0 memory.
250 totalram_pages += free_all_bootmem_node(NODE_DATA(node));
253 * We need to manually do the other slots.
255 numslots = node_getlastslot(node);
256 for (slot = 1; slot <= numslots; slot++) {
257 p = NODE_DATA(node)->node_mem_map +
258 (slot_getbasepfn(node, slot) -
259 slot_getbasepfn(node, 0));
262 * Free valid memory in current slot.
264 for (end = p + slot_getsize(node, slot); p < end; p++) {
265 /* if (!page_is_ram(pgnr)) continue; */
266 /* commented out until page_is_ram works */
267 ClearPageReserved(p);
268 set_page_count(p, 1);
275 totalram_pages -= setup_zero_pages(); /* This comes from node 0 */
277 codesize = (unsigned long) &_etext - (unsigned long) &_text;
278 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
279 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
281 tmp = nr_free_pages();
282 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
283 "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
284 tmp << (PAGE_SHIFT-10),
285 num_physpages << (PAGE_SHIFT-10),
287 (num_physpages - tmp) << (PAGE_SHIFT-10),
290 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));