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, 05 by Ralf Baechle (ralf@linux-mips.org)
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/module.h>
18 #include <linux/nodemask.h>
19 #include <linux/swap.h>
20 #include <linux/bootmem.h>
21 #include <linux/pfn.h>
22 #include <linux/highmem.h>
24 #include <asm/pgalloc.h>
25 #include <asm/sections.h>
27 #include <asm/sn/arch.h>
28 #include <asm/sn/hub.h>
29 #include <asm/sn/klconfig.h>
30 #include <asm/sn/sn_private.h>
33 #define SLOT_PFNSHIFT (SLOT_SHIFT - PAGE_SHIFT)
34 #define PFN_NASIDSHFT (NASID_SHFT - PAGE_SHIFT)
36 static struct bootmem_data __initdata plat_node_bdata[MAX_COMPACT_NODES];
38 struct node_data *__node_data[MAX_COMPACT_NODES];
40 EXPORT_SYMBOL(__node_data);
44 static int is_fine_dirmode(void)
46 return (((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_REGIONSIZE_MASK)
47 >> NSRI_REGIONSIZE_SHFT) & REGIONSIZE_FINE);
50 static hubreg_t get_region(cnodeid_t cnode)
53 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_FINEREG_SHFT;
55 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_COARSEREG_SHFT;
58 static hubreg_t region_mask;
60 static void gen_region_mask(hubreg_t *region_mask)
65 for_each_online_node(cnode) {
66 (*region_mask) |= 1ULL << get_region(cnode);
70 #define rou_rflag rou_flags
72 static int router_distance;
74 static void router_recurse(klrou_t *router_a, klrou_t *router_b, int depth)
80 if (router_a->rou_rflag == 1)
83 if (depth >= router_distance)
86 router_a->rou_rflag = 1;
88 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
89 if (router_a->rou_port[port].port_nasid == INVALID_NASID)
92 brd = (lboard_t *)NODE_OFFSET_TO_K0(
93 router_a->rou_port[port].port_nasid,
94 router_a->rou_port[port].port_offset);
96 if (brd->brd_type == KLTYPE_ROUTER) {
97 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
98 if (router == router_b) {
99 if (depth < router_distance)
100 router_distance = depth;
103 router_recurse(router, router_b, depth + 1);
107 router_a->rou_rflag = 0;
110 unsigned char __node_distances[MAX_COMPACT_NODES][MAX_COMPACT_NODES];
112 static int __init compute_node_distance(nasid_t nasid_a, nasid_t nasid_b)
114 klrou_t *router, *router_a = NULL, *router_b = NULL;
115 lboard_t *brd, *dest_brd;
120 /* Figure out which routers nodes in question are connected to */
121 for_each_online_node(cnode) {
122 nasid = COMPACT_TO_NASID_NODEID(cnode);
124 if (nasid == -1) continue;
126 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
133 if (brd->brd_flags & DUPLICATE_BOARD)
136 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
137 router->rou_rflag = 0;
139 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
140 if (router->rou_port[port].port_nasid == INVALID_NASID)
143 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
144 router->rou_port[port].port_nasid,
145 router->rou_port[port].port_offset);
147 if (dest_brd->brd_type == KLTYPE_IP27) {
148 if (dest_brd->brd_nasid == nasid_a)
150 if (dest_brd->brd_nasid == nasid_b)
155 } while ((brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)));
158 if (router_a == NULL) {
159 printk("node_distance: router_a NULL\n");
162 if (router_b == NULL) {
163 printk("node_distance: router_b NULL\n");
167 if (nasid_a == nasid_b)
170 if (router_a == router_b)
173 router_distance = 100;
174 router_recurse(router_a, router_b, 2);
176 return router_distance;
179 static void __init init_topology_matrix(void)
181 nasid_t nasid, nasid2;
184 for (row = 0; row < MAX_COMPACT_NODES; row++)
185 for (col = 0; col < MAX_COMPACT_NODES; col++)
186 __node_distances[row][col] = -1;
188 for_each_online_node(row) {
189 nasid = COMPACT_TO_NASID_NODEID(row);
190 for_each_online_node(col) {
191 nasid2 = COMPACT_TO_NASID_NODEID(col);
192 __node_distances[row][col] =
193 compute_node_distance(nasid, nasid2);
198 static void __init dump_topology(void)
202 lboard_t *brd, *dest_brd;
208 printk("************** Topology ********************\n");
211 for_each_online_node(col)
212 printk("%02d ", col);
214 for_each_online_node(row) {
215 printk("%02d ", row);
216 for_each_online_node(col)
217 printk("%2d ", node_distance(row, col));
221 for_each_online_node(cnode) {
222 nasid = COMPACT_TO_NASID_NODEID(cnode);
224 if (nasid == -1) continue;
226 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
233 if (brd->brd_flags & DUPLICATE_BOARD)
235 printk("Router %d:", router_num);
238 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
240 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
241 if (router->rou_port[port].port_nasid == INVALID_NASID)
244 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
245 router->rou_port[port].port_nasid,
246 router->rou_port[port].port_offset);
248 if (dest_brd->brd_type == KLTYPE_IP27)
249 printk(" %d", dest_brd->brd_nasid);
250 if (dest_brd->brd_type == KLTYPE_ROUTER)
255 } while ( (brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)) );
259 static pfn_t __init slot_getbasepfn(cnodeid_t cnode, int slot)
261 nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
263 return ((pfn_t)nasid << PFN_NASIDSHFT) | (slot << SLOT_PFNSHIFT);
266 static pfn_t __init slot_psize_compute(cnodeid_t node, int slot)
273 nasid = COMPACT_TO_NASID_NODEID(node);
274 /* Find the node board */
275 brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
279 /* Get the memory bank structure */
280 banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK);
284 /* Size in _Megabytes_ */
285 size = (unsigned long)banks->membnk_bnksz[slot/4];
287 /* hack for 128 dimm banks */
290 size <<= 20; /* size in bytes */
291 return(size >> PAGE_SHIFT);
297 return size >> PAGE_SHIFT;
301 static void __init mlreset(void)
305 master_nasid = get_nasid();
306 fine_mode = is_fine_dirmode();
309 * Probe for all CPUs - this creates the cpumask and sets up the
310 * mapping tables. We need to do this as early as possible.
316 init_topology_matrix();
319 gen_region_mask(®ion_mask);
321 setup_replication_mask();
324 * Set all nodes' calias sizes to 8k
326 for_each_online_node(i) {
329 nasid = COMPACT_TO_NASID_NODEID(i);
332 * Always have node 0 in the region mask, otherwise
333 * CALIAS accesses get exceptions since the hub
334 * thinks it is a node 0 address.
336 REMOTE_HUB_S(nasid, PI_REGION_PRESENT, (region_mask | 1));
337 #ifdef CONFIG_REPLICATE_EXHANDLERS
338 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_8K);
340 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_0);
345 * Set up all hubs to have a big window pointing at
346 * widget 0. Memory mode, widget 0, offset 0
348 REMOTE_HUB_S(nasid, IIO_ITTE(SWIN0_BIGWIN),
349 ((HUB_PIO_MAP_TO_MEM << IIO_ITTE_IOSP_SHIFT) |
350 (0 << IIO_ITTE_WIDGET_SHIFT)));
355 static void __init szmem(void)
357 pfn_t slot_psize, slot0sz = 0, nodebytes; /* Hack to detect problem configs */
363 for_each_online_node(node) {
365 for (slot = 0; slot < MAX_MEM_SLOTS; slot++) {
366 slot_psize = slot_psize_compute(node, slot);
368 slot0sz = slot_psize;
370 * We need to refine the hack when we have replicated
373 nodebytes += (1LL << SLOT_SHIFT);
378 if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
379 (slot0sz << PAGE_SHIFT)) {
380 printk("Ignoring slot %d onwards on node %d\n",
382 slot = MAX_MEM_SLOTS;
385 num_physpages += slot_psize;
386 add_active_range(node, slot_getbasepfn(node, slot),
387 slot_getbasepfn(node, slot) + slot_psize);
392 static void __init node_mem_init(cnodeid_t node)
394 pfn_t slot_firstpfn = slot_getbasepfn(node, 0);
395 pfn_t slot_freepfn = node_getfirstfree(node);
396 unsigned long bootmap_size;
397 pfn_t start_pfn, end_pfn;
399 get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
402 * Allocate the node data structures on the node first.
404 __node_data[node] = __va(slot_freepfn << PAGE_SHIFT);
406 NODE_DATA(node)->bdata = &plat_node_bdata[node];
407 NODE_DATA(node)->node_start_pfn = start_pfn;
408 NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn;
410 cpus_clear(hub_data(node)->h_cpus);
412 slot_freepfn += PFN_UP(sizeof(struct pglist_data) +
413 sizeof(struct hub_data));
415 bootmap_size = init_bootmem_node(NODE_DATA(node), slot_freepfn,
417 free_bootmem_with_active_regions(node, end_pfn);
418 reserve_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
419 ((slot_freepfn - slot_firstpfn) << PAGE_SHIFT) + bootmap_size,
421 sparse_memory_present_with_active_regions(node);
425 * A node with nothing. We use it to avoid any special casing in
428 static struct node_data null_node = {
430 .h_cpus = CPU_MASK_NONE
435 * Currently, the intranode memory hole support assumes that each slot
436 * contains at least 32 MBytes of memory. We assume all bootmem data
437 * fits on the first slot.
439 void __init prom_meminit(void)
446 for (node = 0; node < MAX_COMPACT_NODES; node++) {
447 if (node_online(node)) {
451 __node_data[node] = &null_node;
455 void __init prom_free_prom_memory(void)
457 /* We got nothing to free here ... */
460 extern unsigned long setup_zero_pages(void);
462 void __init paging_init(void)
464 unsigned long zones_size[MAX_NR_ZONES] = {0, };
469 for_each_online_node(node) {
470 pfn_t start_pfn, end_pfn;
472 get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
474 if (end_pfn > max_low_pfn)
475 max_low_pfn = end_pfn;
477 zones_size[ZONE_NORMAL] = max_low_pfn;
478 free_area_init_nodes(zones_size);
481 void __init mem_init(void)
483 unsigned long codesize, datasize, initsize, tmp;
486 high_memory = (void *) __va(num_physpages << PAGE_SHIFT);
488 for_each_online_node(node) {
490 * This will free up the bootmem, ie, slot 0 memory.
492 totalram_pages += free_all_bootmem_node(NODE_DATA(node));
495 totalram_pages -= setup_zero_pages(); /* This comes from node 0 */
497 codesize = (unsigned long) &_etext - (unsigned long) &_text;
498 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
499 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
501 tmp = nr_free_pages();
502 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
503 "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
504 tmp << (PAGE_SHIFT-10),
505 num_physpages << (PAGE_SHIFT-10),
507 (num_physpages - tmp) << (PAGE_SHIFT-10),
510 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));