blob: a160d04f7dbe05904f84f60a262ba3b3fbb287d1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
5 *
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
9 *
10 * On SGI IP27 the ARC memory configuration data is completly bogus but
11 * alternate easier to use mechanisms are available.
12 */
13#include <linux/config.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/mm.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>
22#include <asm/page.h>
23#include <asm/sections.h>
24
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>
29
30
31#define PFN_UP(x) (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
32
33#define SLOT_PFNSHIFT (SLOT_SHIFT - PAGE_SHIFT)
34#define PFN_NASIDSHFT (NASID_SHFT - PAGE_SHIFT)
35
36#define SLOT_IGNORED 0xffff
37
38static short __initdata slot_lastfilled_cache[MAX_COMPACT_NODES];
39static unsigned short __initdata slot_psize_cache[MAX_COMPACT_NODES][MAX_MEM_SLOTS];
40static struct bootmem_data __initdata plat_node_bdata[MAX_COMPACT_NODES];
41
42struct node_data *__node_data[MAX_COMPACT_NODES];
43
44EXPORT_SYMBOL(__node_data);
45
46static int fine_mode;
47
48static int is_fine_dirmode(void)
49{
50 return (((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_REGIONSIZE_MASK)
51 >> NSRI_REGIONSIZE_SHFT) & REGIONSIZE_FINE);
52}
53
54static hubreg_t get_region(cnodeid_t cnode)
55{
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;
60}
61
62static hubreg_t region_mask;
63
64static void gen_region_mask(hubreg_t *region_mask)
65{
66 cnodeid_t cnode;
67
68 (*region_mask) = 0;
69 for_each_online_node(cnode) {
70 (*region_mask) |= 1ULL << get_region(cnode);
71 }
72}
73
74#define rou_rflag rou_flags
75
76static int router_distance;
77
78static void router_recurse(klrou_t *router_a, klrou_t *router_b, int depth)
79{
80 klrou_t *router;
81 lboard_t *brd;
82 int port;
83
84 if (router_a->rou_rflag == 1)
85 return;
86
87 if (depth >= router_distance)
88 return;
89
90 router_a->rou_rflag = 1;
91
92 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
93 if (router_a->rou_port[port].port_nasid == INVALID_NASID)
94 continue;
95
96 brd = (lboard_t *)NODE_OFFSET_TO_K0(
97 router_a->rou_port[port].port_nasid,
98 router_a->rou_port[port].port_offset);
99
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;
105 }
106 else
107 router_recurse(router, router_b, depth + 1);
108 }
109 }
110
111 router_a->rou_rflag = 0;
112}
113
114unsigned char __node_distances[MAX_COMPACT_NODES][MAX_COMPACT_NODES];
115
116static int __init compute_node_distance(nasid_t nasid_a, nasid_t nasid_b)
117{
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;
123
124 /* Figure out which routers nodes in question are connected to */
125 for_each_online_node(cnode) {
126 nasid = COMPACT_TO_NASID_NODEID(cnode);
127
128 if (nasid == -1) continue;
129
130 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
131 KLTYPE_ROUTER);
132
133 if (!brd)
134 continue;
135
136 do {
137 if (brd->brd_flags & DUPLICATE_BOARD)
138 continue;
139
140 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
141 router->rou_rflag = 0;
142
143 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
144 if (router->rou_port[port].port_nasid == INVALID_NASID)
145 continue;
146
147 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
148 router->rou_port[port].port_nasid,
149 router->rou_port[port].port_offset);
150
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;
156 }
157 }
158
159 } while ((brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)));
160 }
161
162 if (router_a == NULL) {
163 printk("node_distance: router_a NULL\n");
164 return -1;
165 }
166 if (router_b == NULL) {
167 printk("node_distance: router_b NULL\n");
168 return -1;
169 }
170
171 if (nasid_a == nasid_b)
172 return 0;
173
174 if (router_a == router_b)
175 return 1;
176
177 router_distance = 100;
178 router_recurse(router_a, router_b, 2);
179
180 return router_distance;
181}
182
183static void __init init_topology_matrix(void)
184{
185 nasid_t nasid, nasid2;
186 cnodeid_t row, col;
187
188 for (row = 0; row < MAX_COMPACT_NODES; row++)
189 for (col = 0; col < MAX_COMPACT_NODES; col++)
190 __node_distances[row][col] = -1;
191
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);
198 }
199 }
200}
201
202static void __init dump_topology(void)
203{
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;
211
212 printk("************** Topology ********************\n");
213
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");
223 }
224
225 for_each_online_node(cnode) {
226 nasid = COMPACT_TO_NASID_NODEID(cnode);
227
228 if (nasid == -1) continue;
229
230 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
231 KLTYPE_ROUTER);
232
233 if (!brd)
234 continue;
235
236 do {
237 if (brd->brd_flags & DUPLICATE_BOARD)
238 continue;
239 printk("Router %d:", router_num);
240 router_num++;
241
242 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
243
244 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
245 if (router->rou_port[port].port_nasid == INVALID_NASID)
246 continue;
247
248 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
249 router->rou_port[port].port_nasid,
250 router->rou_port[port].port_offset);
251
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");
256 }
257 printk("\n");
258
259 } while ( (brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)) );
260 }
261}
262
263static pfn_t __init slot_getbasepfn(cnodeid_t cnode, int slot)
264{
265 nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
266
267 return ((pfn_t)nasid << PFN_NASIDSHFT) | (slot << SLOT_PFNSHIFT);
268}
269
270/*
271 * Return the number of pages of memory provided by the given slot
272 * on the specified node.
273 */
274static pfn_t __init slot_getsize(cnodeid_t node, int slot)
275{
276 return (pfn_t) slot_psize_cache[node][slot];
277}
278
279/*
280 * Return highest slot filled
281 */
282static int __init node_getlastslot(cnodeid_t node)
283{
284 return (int) slot_lastfilled_cache[node];
285}
286
287/*
288 * Return the pfn of the last free page of memory on a node.
289 */
290static pfn_t __init node_getmaxclick(cnodeid_t node)
291{
292 pfn_t slot_psize;
293 int slot;
294
295 /*
296 * Start at the top slot. When we find a slot with memory in it,
297 * that's the winner.
298 */
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;
305 }
306 }
307
308 /*
309 * If there's no memory on the node, return 0. This is likely
310 * to cause problems.
311 */
312 return 0;
313}
314
315static pfn_t __init slot_psize_compute(cnodeid_t node, int slot)
316{
317 nasid_t nasid;
318 lboard_t *brd;
319 klmembnk_t *banks;
320 unsigned long size;
321
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;
327
328 /* Get the memory bank structure */
329 banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK);
330 if (!banks)
331 return 0;
332
333 /* Size in _Megabytes_ */
334 size = (unsigned long)banks->membnk_bnksz[slot/4];
335
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;
347 }
348}
349
350static void __init mlreset(void)
351{
352 int i;
353
354 master_nasid = get_nasid();
355 fine_mode = is_fine_dirmode();
356
357 /*
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.
360 */
361#ifdef CONFIG_SMP
362 cpu_node_probe();
363#endif
364
365 init_topology_matrix();
366 dump_topology();
367
368 gen_region_mask(&region_mask);
369
370 setup_replication_mask();
371
372 /*
373 * Set all nodes' calias sizes to 8k
374 */
375 for_each_online_node(i) {
376 nasid_t nasid;
377
378 nasid = COMPACT_TO_NASID_NODEID(i);
379
380 /*
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.
384 */
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
391
392#ifdef LATER
393 /*
394 * Set up all hubs to have a big window pointing at
395 * widget 0. Memory mode, widget 0, offset 0
396 */
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
401 }
402}
403
404static void __init szmem(void)
405{
406 pfn_t slot_psize, slot0sz = 0, nodebytes; /* Hack to detect problem configs */
407 int slot, ignore;
408 cnodeid_t node;
409
410 num_physpages = 0;
411
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;
418 /*
419 * We need to refine the hack when we have replicated
420 * kernel text.
421 */
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;
432 }
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;
438 }
439 }
440}
441
442static void __init node_mem_init(cnodeid_t node)
443{
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;
449
450 /*
451 * Allocate the node data structures on the node first.
452 */
453 __node_data[node] = __va(slot_freepfn << PAGE_SHIFT);
454
455 pd = NODE_DATA(node);
456 pd->bdata = &plat_node_bdata[node];
457
458 cpus_clear(hub_data(node)->h_cpus);
459
460 slot_freepfn += PFN_UP(sizeof(struct pglist_data) +
461 sizeof(struct hub_data));
462
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}
470
471/*
472 * A node with nothing. We use it to avoid any special casing in
473 * node_to_cpumask
474 */
475static struct node_data null_node = {
476 .hub = {
477 .h_cpus = CPU_MASK_NONE
478 }
479};
480
481/*
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.
485 */
486void __init prom_meminit(void)
487{
488 cnodeid_t node;
489
490 mlreset();
491 szmem();
492
493 for (node = 0; node < MAX_COMPACT_NODES; node++) {
494 if (node_online(node)) {
495 node_mem_init(node);
496 continue;
497 }
498 __node_data[node] = &null_node;
499 }
500}
501
502unsigned long __init prom_free_prom_memory(void)
503{
504 /* We got nothing to free here ... */
505 return 0;
506}
507
508extern void pagetable_init(void);
509extern unsigned long setup_zero_pages(void);
510
511void __init paging_init(void)
512{
513 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
514 unsigned node;
515
516 pagetable_init();
517
518 for_each_online_node(node) {
519 pfn_t start_pfn = slot_getbasepfn(node, 0);
520 pfn_t end_pfn = node_getmaxclick(node) + 1;
521
522 zones_size[ZONE_DMA] = end_pfn - start_pfn;
523 free_area_init_node(node, NODE_DATA(node),
524 zones_size, start_pfn, NULL);
525
526 if (end_pfn > max_low_pfn)
527 max_low_pfn = end_pfn;
528 }
529}
530
531void __init mem_init(void)
532{
533 unsigned long codesize, datasize, initsize, tmp;
534 unsigned node;
535
536 high_memory = (void *) __va(num_physpages << PAGE_SHIFT);
537
538 for_each_online_node(node) {
539 unsigned slot, numslots;
540 struct page *end, *p;
541
542 /*
543 * This will free up the bootmem, ie, slot 0 memory.
544 */
545 totalram_pages += free_all_bootmem_node(NODE_DATA(node));
546
547 /*
548 * We need to manually do the other slots.
549 */
550 numslots = node_getlastslot(node);
551 for (slot = 1; slot <= numslots; slot++) {
Dave Hansen408fde82005-06-23 00:07:37 -0700552 p = nid_page_nr(node, slot_getbasepfn(node, slot) -
553 slot_getbasepfn(node, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /*
556 * Free valid memory in current slot.
557 */
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);
563 __free_page(p);
564 totalram_pages++;
565 }
566 }
567 }
568
569 totalram_pages -= setup_zero_pages(); /* This comes from node 0 */
570
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;
574
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),
580 codesize >> 10,
581 (num_physpages - tmp) << (PAGE_SHIFT-10),
582 datasize >> 10,
583 initsize >> 10,
584 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
585}