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/config.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
17 #include <linux/mmzone.h>
18 #include <linux/module.h>
19 #include <linux/nodemask.h>
20 #include <linux/swap.h>
21 #include <linux/bootmem.h>
23 #include <asm/sections.h>
25 #include <asm/sn/arch.h>
26 #include <asm/sn/hub.h>
27 #include <asm/sn/klconfig.h>
28 #include <asm/sn/sn_private.h>
31 #define PFN_UP(x) (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
33 #define SLOT_PFNSHIFT (SLOT_SHIFT - PAGE_SHIFT)
34 #define PFN_NASIDSHFT (NASID_SHFT - PAGE_SHIFT)
36 #define SLOT_IGNORED 0xffff
38 static short __initdata slot_lastfilled_cache[MAX_COMPACT_NODES];
39 static unsigned short __initdata slot_psize_cache[MAX_COMPACT_NODES][MAX_MEM_SLOTS];
40 static struct bootmem_data __initdata plat_node_bdata[MAX_COMPACT_NODES];
42 struct node_data *__node_data[MAX_COMPACT_NODES];
44 EXPORT_SYMBOL(__node_data);
48 static int is_fine_dirmode(void)
50 return (((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_REGIONSIZE_MASK)
51 >> NSRI_REGIONSIZE_SHFT) & REGIONSIZE_FINE);
54 static hubreg_t get_region(cnodeid_t cnode)
57 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_FINEREG_SHFT;
59 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_COARSEREG_SHFT;
62 static hubreg_t region_mask;
64 static void gen_region_mask(hubreg_t *region_mask)
69 for_each_online_node(cnode) {
70 (*region_mask) |= 1ULL << get_region(cnode);
74 #define rou_rflag rou_flags
76 static int router_distance;
78 static void router_recurse(klrou_t *router_a, klrou_t *router_b, int depth)
84 if (router_a->rou_rflag == 1)
87 if (depth >= router_distance)
90 router_a->rou_rflag = 1;
92 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
93 if (router_a->rou_port[port].port_nasid == INVALID_NASID)
96 brd = (lboard_t *)NODE_OFFSET_TO_K0(
97 router_a->rou_port[port].port_nasid,
98 router_a->rou_port[port].port_offset);
100 if (brd->brd_type == KLTYPE_ROUTER) {
101 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
102 if (router == router_b) {
103 if (depth < router_distance)
104 router_distance = depth;
107 router_recurse(router, router_b, depth + 1);
111 router_a->rou_rflag = 0;
114 unsigned char __node_distances[MAX_COMPACT_NODES][MAX_COMPACT_NODES];
116 static int __init compute_node_distance(nasid_t nasid_a, nasid_t nasid_b)
118 klrou_t *router, *router_a = NULL, *router_b = NULL;
119 lboard_t *brd, *dest_brd;
124 /* Figure out which routers nodes in question are connected to */
125 for_each_online_node(cnode) {
126 nasid = COMPACT_TO_NASID_NODEID(cnode);
128 if (nasid == -1) continue;
130 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
137 if (brd->brd_flags & DUPLICATE_BOARD)
140 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
141 router->rou_rflag = 0;
143 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
144 if (router->rou_port[port].port_nasid == INVALID_NASID)
147 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
148 router->rou_port[port].port_nasid,
149 router->rou_port[port].port_offset);
151 if (dest_brd->brd_type == KLTYPE_IP27) {
152 if (dest_brd->brd_nasid == nasid_a)
154 if (dest_brd->brd_nasid == nasid_b)
159 } while ((brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)));
162 if (router_a == NULL) {
163 printk("node_distance: router_a NULL\n");
166 if (router_b == NULL) {
167 printk("node_distance: router_b NULL\n");
171 if (nasid_a == nasid_b)
174 if (router_a == router_b)
177 router_distance = 100;
178 router_recurse(router_a, router_b, 2);
180 return router_distance;
183 static void __init init_topology_matrix(void)
185 nasid_t nasid, nasid2;
188 for (row = 0; row < MAX_COMPACT_NODES; row++)
189 for (col = 0; col < MAX_COMPACT_NODES; col++)
190 __node_distances[row][col] = -1;
192 for_each_online_node(row) {
193 nasid = COMPACT_TO_NASID_NODEID(row);
194 for_each_online_node(col) {
195 nasid2 = COMPACT_TO_NASID_NODEID(col);
196 __node_distances[row][col] =
197 compute_node_distance(nasid, nasid2);
202 static void __init dump_topology(void)
206 lboard_t *brd, *dest_brd;
212 printk("************** Topology ********************\n");
215 for_each_online_node(col)
216 printk("%02d ", col);
218 for_each_online_node(row) {
219 printk("%02d ", row);
220 for_each_online_node(col)
221 printk("%2d ", node_distance(row, col));
225 for_each_online_node(cnode) {
226 nasid = COMPACT_TO_NASID_NODEID(cnode);
228 if (nasid == -1) continue;
230 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
237 if (brd->brd_flags & DUPLICATE_BOARD)
239 printk("Router %d:", router_num);
242 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
244 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
245 if (router->rou_port[port].port_nasid == INVALID_NASID)
248 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
249 router->rou_port[port].port_nasid,
250 router->rou_port[port].port_offset);
252 if (dest_brd->brd_type == KLTYPE_IP27)
253 printk(" %d", dest_brd->brd_nasid);
254 if (dest_brd->brd_type == KLTYPE_ROUTER)
259 } while ( (brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)) );
263 static pfn_t __init slot_getbasepfn(cnodeid_t cnode, int slot)
265 nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
267 return ((pfn_t)nasid << PFN_NASIDSHFT) | (slot << SLOT_PFNSHIFT);
271 * Return the number of pages of memory provided by the given slot
272 * on the specified node.
274 static pfn_t __init slot_getsize(cnodeid_t node, int slot)
276 return (pfn_t) slot_psize_cache[node][slot];
280 * Return highest slot filled
282 static int __init node_getlastslot(cnodeid_t node)
284 return (int) slot_lastfilled_cache[node];
288 * Return the pfn of the last free page of memory on a node.
290 static pfn_t __init node_getmaxclick(cnodeid_t node)
296 * Start at the top slot. When we find a slot with memory in it,
299 for (slot = (MAX_MEM_SLOTS - 1); slot >= 0; slot--) {
300 if ((slot_psize = slot_getsize(node, slot))) {
301 if (slot_psize == SLOT_IGNORED)
303 /* Return the basepfn + the slot size, minus 1. */
304 return slot_getbasepfn(node, slot) + slot_psize - 1;
309 * If there's no memory on the node, return 0. This is likely
315 static pfn_t __init slot_psize_compute(cnodeid_t node, int slot)
322 nasid = COMPACT_TO_NASID_NODEID(node);
323 /* Find the node board */
324 brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
328 /* Get the memory bank structure */
329 banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK);
333 /* Size in _Megabytes_ */
334 size = (unsigned long)banks->membnk_bnksz[slot/4];
336 /* hack for 128 dimm banks */
339 size <<= 20; /* size in bytes */
340 return(size >> PAGE_SHIFT);
346 return size >> PAGE_SHIFT;
350 static void __init mlreset(void)
354 master_nasid = get_nasid();
355 fine_mode = is_fine_dirmode();
358 * Probe for all CPUs - this creates the cpumask and sets up the
359 * mapping tables. We need to do this as early as possible.
365 init_topology_matrix();
368 gen_region_mask(®ion_mask);
370 setup_replication_mask();
373 * Set all nodes' calias sizes to 8k
375 for_each_online_node(i) {
378 nasid = COMPACT_TO_NASID_NODEID(i);
381 * Always have node 0 in the region mask, otherwise
382 * CALIAS accesses get exceptions since the hub
383 * thinks it is a node 0 address.
385 REMOTE_HUB_S(nasid, PI_REGION_PRESENT, (region_mask | 1));
386 #ifdef CONFIG_REPLICATE_EXHANDLERS
387 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_8K);
389 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_0);
394 * Set up all hubs to have a big window pointing at
395 * widget 0. Memory mode, widget 0, offset 0
397 REMOTE_HUB_S(nasid, IIO_ITTE(SWIN0_BIGWIN),
398 ((HUB_PIO_MAP_TO_MEM << IIO_ITTE_IOSP_SHIFT) |
399 (0 << IIO_ITTE_WIDGET_SHIFT)));
404 static void __init szmem(void)
406 pfn_t slot_psize, slot0sz = 0, nodebytes; /* Hack to detect problem configs */
412 for_each_online_node(node) {
413 ignore = nodebytes = 0;
414 for (slot = 0; slot < MAX_MEM_SLOTS; slot++) {
415 slot_psize = slot_psize_compute(node, slot);
417 slot0sz = slot_psize;
419 * We need to refine the hack when we have replicated
422 nodebytes += (1LL << SLOT_SHIFT);
423 if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
424 (slot0sz << PAGE_SHIFT))
426 if (ignore && slot_psize) {
427 printk("Ignoring slot %d onwards on node %d\n",
429 slot_psize_cache[node][slot] = SLOT_IGNORED;
430 slot = MAX_MEM_SLOTS;
433 num_physpages += slot_psize;
434 slot_psize_cache[node][slot] =
435 (unsigned short) slot_psize;
437 slot_lastfilled_cache[node] = slot;
442 static void __init node_mem_init(cnodeid_t node)
444 pfn_t slot_firstpfn = slot_getbasepfn(node, 0);
445 pfn_t slot_lastpfn = slot_firstpfn + slot_getsize(node, 0);
446 pfn_t slot_freepfn = node_getfirstfree(node);
447 struct pglist_data *pd;
448 unsigned long bootmap_size;
451 * Allocate the node data structures on the node first.
453 __node_data[node] = __va(slot_freepfn << PAGE_SHIFT);
455 pd = NODE_DATA(node);
456 pd->bdata = &plat_node_bdata[node];
458 cpus_clear(hub_data(node)->h_cpus);
460 slot_freepfn += PFN_UP(sizeof(struct pglist_data) +
461 sizeof(struct hub_data));
463 bootmap_size = init_bootmem_node(NODE_DATA(node), slot_freepfn,
464 slot_firstpfn, slot_lastpfn);
465 free_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
466 (slot_lastpfn - slot_firstpfn) << PAGE_SHIFT);
467 reserve_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
468 ((slot_freepfn - slot_firstpfn) << PAGE_SHIFT) + bootmap_size);
472 * A node with nothing. We use it to avoid any special casing in
475 static struct node_data null_node = {
477 .h_cpus = CPU_MASK_NONE
482 * Currently, the intranode memory hole support assumes that each slot
483 * contains at least 32 MBytes of memory. We assume all bootmem data
484 * fits on the first slot.
486 void __init prom_meminit(void)
493 for (node = 0; node < MAX_COMPACT_NODES; node++) {
494 if (node_online(node)) {
498 __node_data[node] = &null_node;
502 unsigned long __init prom_free_prom_memory(void)
504 /* We got nothing to free here ... */
508 extern void pagetable_init(void);
509 extern unsigned long setup_zero_pages(void);
511 void __init paging_init(void)
513 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
518 for_each_online_node(node) {
519 pfn_t start_pfn = slot_getbasepfn(node, 0);
520 pfn_t end_pfn = node_getmaxclick(node) + 1;
522 zones_size[ZONE_DMA] = end_pfn - start_pfn;
523 free_area_init_node(node, NODE_DATA(node),
524 zones_size, start_pfn, NULL);
526 if (end_pfn > max_low_pfn)
527 max_low_pfn = end_pfn;
531 void __init mem_init(void)
533 unsigned long codesize, datasize, initsize, tmp;
536 high_memory = (void *) __va(num_physpages << PAGE_SHIFT);
538 for_each_online_node(node) {
539 unsigned slot, numslots;
540 struct page *end, *p;
543 * This will free up the bootmem, ie, slot 0 memory.
545 totalram_pages += free_all_bootmem_node(NODE_DATA(node));
548 * We need to manually do the other slots.
550 numslots = node_getlastslot(node);
551 for (slot = 1; slot <= numslots; slot++) {
552 p = nid_page_nr(node, slot_getbasepfn(node, slot) -
553 slot_getbasepfn(node, 0));
556 * Free valid memory in current slot.
558 for (end = p + slot_getsize(node, slot); p < end; p++) {
559 /* if (!page_is_ram(pgnr)) continue; */
560 /* commented out until page_is_ram works */
561 ClearPageReserved(p);
562 set_page_count(p, 1);
569 totalram_pages -= setup_zero_pages(); /* This comes from node 0 */
571 codesize = (unsigned long) &_etext - (unsigned long) &_text;
572 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
573 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
575 tmp = nr_free_pages();
576 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
577 "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
578 tmp << (PAGE_SHIFT-10),
579 num_physpages << (PAGE_SHIFT-10),
581 (num_physpages - tmp) << (PAGE_SHIFT-10),
584 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));