Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / mips / sgi-ip27 / ip27-memory.c
blobbf438d02366e4823aa5849d9592b9163bfb088ae
1 /*
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
4 * for more details.
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>
15 #include <linux/mm.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>
23 #include <asm/page.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 #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);
46 static int fine_mode;
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)
56 if (fine_mode)
57 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_FINEREG_SHFT;
58 else
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)
66 cnodeid_t cnode;
68 (*region_mask) = 0;
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)
80 klrou_t *router;
81 lboard_t *brd;
82 int port;
84 if (router_a->rou_rflag == 1)
85 return;
87 if (depth >= router_distance)
88 return;
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)
94 continue;
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;
106 else
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;
120 cnodeid_t cnode;
121 nasid_t nasid;
122 int port;
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),
131 KLTYPE_ROUTER);
133 if (!brd)
134 continue;
136 do {
137 if (brd->brd_flags & DUPLICATE_BOARD)
138 continue;
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)
145 continue;
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)
153 router_a = router;
154 if (dest_brd->brd_nasid == nasid_b)
155 router_b = router;
159 } while ((brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)));
162 if (router_a == NULL) {
163 printk("node_distance: router_a NULL\n");
164 return -1;
166 if (router_b == NULL) {
167 printk("node_distance: router_b NULL\n");
168 return -1;
171 if (nasid_a == nasid_b)
172 return 0;
174 if (router_a == router_b)
175 return 1;
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;
186 cnodeid_t row, col;
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)
204 nasid_t nasid;
205 cnodeid_t cnode;
206 lboard_t *brd, *dest_brd;
207 int port;
208 int router_num = 0;
209 klrou_t *router;
210 cnodeid_t row, col;
212 printk("************** Topology ********************\n");
214 printk(" ");
215 for_each_online_node(col)
216 printk("%02d ", col);
217 printk("\n");
218 for_each_online_node(row) {
219 printk("%02d ", row);
220 for_each_online_node(col)
221 printk("%2d ", node_distance(row, col));
222 printk("\n");
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),
231 KLTYPE_ROUTER);
233 if (!brd)
234 continue;
236 do {
237 if (brd->brd_flags & DUPLICATE_BOARD)
238 continue;
239 printk("Router %d:", router_num);
240 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)
246 continue;
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)
255 printk(" r");
257 printk("\n");
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)
292 pfn_t slot_psize;
293 int slot;
296 * Start at the top slot. When we find a slot with memory in it,
297 * that's the winner.
299 for (slot = (MAX_MEM_SLOTS - 1); slot >= 0; slot--) {
300 if ((slot_psize = slot_getsize(node, slot))) {
301 if (slot_psize == SLOT_IGNORED)
302 continue;
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
310 * to cause problems.
312 return 0;
315 static pfn_t __init slot_psize_compute(cnodeid_t node, int slot)
317 nasid_t nasid;
318 lboard_t *brd;
319 klmembnk_t *banks;
320 unsigned long size;
322 nasid = COMPACT_TO_NASID_NODEID(node);
323 /* Find the node board */
324 brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
325 if (!brd)
326 return 0;
328 /* Get the memory bank structure */
329 banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK);
330 if (!banks)
331 return 0;
333 /* Size in _Megabytes_ */
334 size = (unsigned long)banks->membnk_bnksz[slot/4];
336 /* hack for 128 dimm banks */
337 if (size <= 128) {
338 if (slot % 4 == 0) {
339 size <<= 20; /* size in bytes */
340 return(size >> PAGE_SHIFT);
341 } else
342 return 0;
343 } else {
344 size /= 4;
345 size <<= 20;
346 return size >> PAGE_SHIFT;
350 static void __init mlreset(void)
352 int i;
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.
361 #ifdef CONFIG_SMP
362 cpu_node_probe();
363 #endif
365 init_topology_matrix();
366 dump_topology();
368 gen_region_mask(&region_mask);
370 setup_replication_mask();
373 * Set all nodes' calias sizes to 8k
375 for_each_online_node(i) {
376 nasid_t nasid;
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);
388 #else
389 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_0);
390 #endif
392 #ifdef LATER
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)));
400 #endif
404 static void __init szmem(void)
406 pfn_t slot_psize, slot0sz = 0, nodebytes; /* Hack to detect problem configs */
407 int slot, ignore;
408 cnodeid_t node;
410 num_physpages = 0;
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);
416 if (slot == 0)
417 slot0sz = slot_psize;
419 * We need to refine the hack when we have replicated
420 * kernel text.
422 nodebytes += (1LL << SLOT_SHIFT);
423 if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
424 (slot0sz << PAGE_SHIFT))
425 ignore = 1;
426 if (ignore && slot_psize) {
427 printk("Ignoring slot %d onwards on node %d\n",
428 slot, node);
429 slot_psize_cache[node][slot] = SLOT_IGNORED;
430 slot = MAX_MEM_SLOTS;
431 continue;
433 num_physpages += slot_psize;
434 slot_psize_cache[node][slot] =
435 (unsigned short) slot_psize;
436 if (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,
469 BOOTMEM_DEFAULT);
473 * A node with nothing. We use it to avoid any special casing in
474 * node_to_cpumask
476 static struct node_data null_node = {
477 .hub = {
478 .h_cpus = CPU_MASK_NONE
483 * Currently, the intranode memory hole support assumes that each slot
484 * contains at least 32 MBytes of memory. We assume all bootmem data
485 * fits on the first slot.
487 void __init prom_meminit(void)
489 cnodeid_t node;
491 mlreset();
492 szmem();
494 for (node = 0; node < MAX_COMPACT_NODES; node++) {
495 if (node_online(node)) {
496 node_mem_init(node);
497 continue;
499 __node_data[node] = &null_node;
503 void __init prom_free_prom_memory(void)
505 /* We got nothing to free here ... */
508 extern unsigned long setup_zero_pages(void);
510 void __init paging_init(void)
512 unsigned long zones_size[MAX_NR_ZONES] = {0, };
513 unsigned node;
515 pagetable_init();
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_NORMAL] = 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;
533 unsigned node;
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);
561 init_page_count(p);
562 __free_page(p);
563 totalram_pages++;
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),
579 codesize >> 10,
580 (num_physpages - tmp) << (PAGE_SHIFT-10),
581 datasize >> 10,
582 initsize >> 10,
583 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));