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/sections.h>
26 #include <asm/sn/arch.h>
27 #include <asm/sn/hub.h>
28 #include <asm/sn/klconfig.h>
29 #include <asm/sn/sn_private.h>
32 #define SLOT_PFNSHIFT (SLOT_SHIFT - PAGE_SHIFT)
33 #define PFN_NASIDSHFT (NASID_SHFT - PAGE_SHIFT)
35 #define SLOT_IGNORED 0xffff
37 static short __initdata slot_lastfilled_cache[MAX_COMPACT_NODES];
38 static unsigned short __initdata slot_psize_cache[MAX_COMPACT_NODES][MAX_MEM_SLOTS];
39 static struct bootmem_data __initdata plat_node_bdata[MAX_COMPACT_NODES];
41 struct node_data *__node_data[MAX_COMPACT_NODES];
43 EXPORT_SYMBOL(__node_data);
47 static int is_fine_dirmode(void)
49 return (((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_REGIONSIZE_MASK)
50 >> NSRI_REGIONSIZE_SHFT) & REGIONSIZE_FINE);
53 static hubreg_t get_region(cnodeid_t cnode)
56 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_FINEREG_SHFT;
58 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_COARSEREG_SHFT;
61 static hubreg_t region_mask;
63 static void gen_region_mask(hubreg_t *region_mask)
68 for_each_online_node(cnode) {
69 (*region_mask) |= 1ULL << get_region(cnode);
73 #define rou_rflag rou_flags
75 static int router_distance;
77 static void router_recurse(klrou_t *router_a, klrou_t *router_b, int depth)
83 if (router_a->rou_rflag == 1)
86 if (depth >= router_distance)
89 router_a->rou_rflag = 1;
91 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
92 if (router_a->rou_port[port].port_nasid == INVALID_NASID)
95 brd = (lboard_t *)NODE_OFFSET_TO_K0(
96 router_a->rou_port[port].port_nasid,
97 router_a->rou_port[port].port_offset);
99 if (brd->brd_type == KLTYPE_ROUTER) {
100 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
101 if (router == router_b) {
102 if (depth < router_distance)
103 router_distance = depth;
106 router_recurse(router, router_b, depth + 1);
110 router_a->rou_rflag = 0;
113 unsigned char __node_distances[MAX_COMPACT_NODES][MAX_COMPACT_NODES];
115 static int __init compute_node_distance(nasid_t nasid_a, nasid_t nasid_b)
117 klrou_t *router, *router_a = NULL, *router_b = NULL;
118 lboard_t *brd, *dest_brd;
123 /* Figure out which routers nodes in question are connected to */
124 for_each_online_node(cnode) {
125 nasid = COMPACT_TO_NASID_NODEID(cnode);
127 if (nasid == -1) continue;
129 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
136 if (brd->brd_flags & DUPLICATE_BOARD)
139 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
140 router->rou_rflag = 0;
142 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
143 if (router->rou_port[port].port_nasid == INVALID_NASID)
146 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
147 router->rou_port[port].port_nasid,
148 router->rou_port[port].port_offset);
150 if (dest_brd->brd_type == KLTYPE_IP27) {
151 if (dest_brd->brd_nasid == nasid_a)
153 if (dest_brd->brd_nasid == nasid_b)
158 } while ((brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)));
161 if (router_a == NULL) {
162 printk("node_distance: router_a NULL\n");
165 if (router_b == NULL) {
166 printk("node_distance: router_b NULL\n");
170 if (nasid_a == nasid_b)
173 if (router_a == router_b)
176 router_distance = 100;
177 router_recurse(router_a, router_b, 2);
179 return router_distance;
182 static void __init init_topology_matrix(void)
184 nasid_t nasid, nasid2;
187 for (row = 0; row < MAX_COMPACT_NODES; row++)
188 for (col = 0; col < MAX_COMPACT_NODES; col++)
189 __node_distances[row][col] = -1;
191 for_each_online_node(row) {
192 nasid = COMPACT_TO_NASID_NODEID(row);
193 for_each_online_node(col) {
194 nasid2 = COMPACT_TO_NASID_NODEID(col);
195 __node_distances[row][col] =
196 compute_node_distance(nasid, nasid2);
201 static void __init dump_topology(void)
205 lboard_t *brd, *dest_brd;
211 printk("************** Topology ********************\n");
214 for_each_online_node(col)
215 printk("%02d ", col);
217 for_each_online_node(row) {
218 printk("%02d ", row);
219 for_each_online_node(col)
220 printk("%2d ", node_distance(row, col));
224 for_each_online_node(cnode) {
225 nasid = COMPACT_TO_NASID_NODEID(cnode);
227 if (nasid == -1) continue;
229 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
236 if (brd->brd_flags & DUPLICATE_BOARD)
238 printk("Router %d:", router_num);
241 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
243 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
244 if (router->rou_port[port].port_nasid == INVALID_NASID)
247 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
248 router->rou_port[port].port_nasid,
249 router->rou_port[port].port_offset);
251 if (dest_brd->brd_type == KLTYPE_IP27)
252 printk(" %d", dest_brd->brd_nasid);
253 if (dest_brd->brd_type == KLTYPE_ROUTER)
258 } while ( (brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)) );
262 static pfn_t __init slot_getbasepfn(cnodeid_t cnode, int slot)
264 nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
266 return ((pfn_t)nasid << PFN_NASIDSHFT) | (slot << SLOT_PFNSHIFT);
270 * Return the number of pages of memory provided by the given slot
271 * on the specified node.
273 static pfn_t __init slot_getsize(cnodeid_t node, int slot)
275 return (pfn_t) slot_psize_cache[node][slot];
279 * Return highest slot filled
281 static int __init node_getlastslot(cnodeid_t node)
283 return (int) slot_lastfilled_cache[node];
287 * Return the pfn of the last free page of memory on a node.
289 static pfn_t __init node_getmaxclick(cnodeid_t node)
295 * Start at the top slot. When we find a slot with memory in it,
298 for (slot = (MAX_MEM_SLOTS - 1); slot >= 0; slot--) {
299 if ((slot_psize = slot_getsize(node, slot))) {
300 if (slot_psize == SLOT_IGNORED)
302 /* Return the basepfn + the slot size, minus 1. */
303 return slot_getbasepfn(node, slot) + slot_psize - 1;
308 * If there's no memory on the node, return 0. This is likely
314 static pfn_t __init slot_psize_compute(cnodeid_t node, int slot)
321 nasid = COMPACT_TO_NASID_NODEID(node);
322 /* Find the node board */
323 brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
327 /* Get the memory bank structure */
328 banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK);
332 /* Size in _Megabytes_ */
333 size = (unsigned long)banks->membnk_bnksz[slot/4];
335 /* hack for 128 dimm banks */
338 size <<= 20; /* size in bytes */
339 return(size >> PAGE_SHIFT);
345 return size >> PAGE_SHIFT;
349 static void __init mlreset(void)
353 master_nasid = get_nasid();
354 fine_mode = is_fine_dirmode();
357 * Probe for all CPUs - this creates the cpumask and sets up the
358 * mapping tables. We need to do this as early as possible.
364 init_topology_matrix();
367 gen_region_mask(®ion_mask);
369 setup_replication_mask();
372 * Set all nodes' calias sizes to 8k
374 for_each_online_node(i) {
377 nasid = COMPACT_TO_NASID_NODEID(i);
380 * Always have node 0 in the region mask, otherwise
381 * CALIAS accesses get exceptions since the hub
382 * thinks it is a node 0 address.
384 REMOTE_HUB_S(nasid, PI_REGION_PRESENT, (region_mask | 1));
385 #ifdef CONFIG_REPLICATE_EXHANDLERS
386 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_8K);
388 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_0);
393 * Set up all hubs to have a big window pointing at
394 * widget 0. Memory mode, widget 0, offset 0
396 REMOTE_HUB_S(nasid, IIO_ITTE(SWIN0_BIGWIN),
397 ((HUB_PIO_MAP_TO_MEM << IIO_ITTE_IOSP_SHIFT) |
398 (0 << IIO_ITTE_WIDGET_SHIFT)));
403 static void __init szmem(void)
405 pfn_t slot_psize, slot0sz = 0, nodebytes; /* Hack to detect problem configs */
411 for_each_online_node(node) {
412 ignore = nodebytes = 0;
413 for (slot = 0; slot < MAX_MEM_SLOTS; slot++) {
414 slot_psize = slot_psize_compute(node, slot);
416 slot0sz = slot_psize;
418 * We need to refine the hack when we have replicated
421 nodebytes += (1LL << SLOT_SHIFT);
422 if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
423 (slot0sz << PAGE_SHIFT))
425 if (ignore && slot_psize) {
426 printk("Ignoring slot %d onwards on node %d\n",
428 slot_psize_cache[node][slot] = SLOT_IGNORED;
429 slot = MAX_MEM_SLOTS;
432 num_physpages += slot_psize;
433 slot_psize_cache[node][slot] =
434 (unsigned short) slot_psize;
436 slot_lastfilled_cache[node] = slot;
441 static void __init node_mem_init(cnodeid_t node)
443 pfn_t slot_firstpfn = slot_getbasepfn(node, 0);
444 pfn_t slot_lastpfn = slot_firstpfn + slot_getsize(node, 0);
445 pfn_t slot_freepfn = node_getfirstfree(node);
446 struct pglist_data *pd;
447 unsigned long bootmap_size;
450 * Allocate the node data structures on the node first.
452 __node_data[node] = __va(slot_freepfn << PAGE_SHIFT);
454 pd = NODE_DATA(node);
455 pd->bdata = &plat_node_bdata[node];
457 cpus_clear(hub_data(node)->h_cpus);
459 slot_freepfn += PFN_UP(sizeof(struct pglist_data) +
460 sizeof(struct hub_data));
462 bootmap_size = init_bootmem_node(NODE_DATA(node), slot_freepfn,
463 slot_firstpfn, slot_lastpfn);
464 free_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
465 (slot_lastpfn - slot_firstpfn) << PAGE_SHIFT);
466 reserve_bootmem_node(NODE_DATA(node), slot_firstpfn << PAGE_SHIFT,
467 ((slot_freepfn - slot_firstpfn) << PAGE_SHIFT) + bootmap_size);
471 * A node with nothing. We use it to avoid any special casing in
474 static struct node_data null_node = {
476 .h_cpus = CPU_MASK_NONE
481 * Currently, the intranode memory hole support assumes that each slot
482 * contains at least 32 MBytes of memory. We assume all bootmem data
483 * fits on the first slot.
485 void __init prom_meminit(void)
492 for (node = 0; node < MAX_COMPACT_NODES; node++) {
493 if (node_online(node)) {
497 __node_data[node] = &null_node;
501 unsigned long __init prom_free_prom_memory(void)
503 /* We got nothing to free here ... */
507 extern void pagetable_init(void);
508 extern unsigned long setup_zero_pages(void);
510 void __init paging_init(void)
512 unsigned long zones_size[MAX_NR_ZONES] = {0, };
517 for_each_online_node(node) {
518 pfn_t start_pfn = slot_getbasepfn(node, 0);
519 pfn_t end_pfn = node_getmaxclick(node) + 1;
521 zones_size[ZONE_DMA] = end_pfn - start_pfn;
522 free_area_init_node(node, NODE_DATA(node),
523 zones_size, start_pfn, NULL);
525 if (end_pfn > max_low_pfn)
526 max_low_pfn = end_pfn;
530 void __init mem_init(void)
532 unsigned long codesize, datasize, initsize, tmp;
535 high_memory = (void *) __va(num_physpages << PAGE_SHIFT);
537 for_each_online_node(node) {
538 unsigned slot, numslots;
539 struct page *end, *p;
542 * This will free up the bootmem, ie, slot 0 memory.
544 totalram_pages += free_all_bootmem_node(NODE_DATA(node));
547 * We need to manually do the other slots.
549 numslots = node_getlastslot(node);
550 for (slot = 1; slot <= numslots; slot++) {
551 p = nid_page_nr(node, slot_getbasepfn(node, slot) -
552 slot_getbasepfn(node, 0));
555 * Free valid memory in current slot.
557 for (end = p + slot_getsize(node, slot); p < end; p++) {
558 /* if (!page_is_ram(pgnr)) continue; */
559 /* commented out until page_is_ram works */
560 ClearPageReserved(p);
568 totalram_pages -= setup_zero_pages(); /* This comes from node 0 */
570 codesize = (unsigned long) &_etext - (unsigned long) &_text;
571 datasize = (unsigned long) &_edata - (unsigned long) &_etext;
572 initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
574 tmp = nr_free_pages();
575 printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, "
576 "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n",
577 tmp << (PAGE_SHIFT-10),
578 num_physpages << (PAGE_SHIFT-10),
580 (num_physpages - tmp) << (PAGE_SHIFT-10),
583 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));