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 completely bogus but
11 * alternate easier to use mechanisms are available.
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/memblock.h>
17 #include <linux/mmzone.h>
18 #include <linux/export.h>
19 #include <linux/nodemask.h>
20 #include <linux/swap.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/agent.h>
29 #include <asm/sn/klconfig.h>
31 #include "ip27-common.h"
33 #define SLOT_PFNSHIFT (SLOT_SHIFT - PAGE_SHIFT)
34 #define PFN_NASIDSHFT (NASID_SHFT - PAGE_SHIFT)
36 struct node_data
*__node_data
[MAX_NUMNODES
];
38 EXPORT_SYMBOL(__node_data
);
40 static u64
gen_region_mask(void)
46 region_shift
= get_region_shift();
48 for_each_online_node(nasid
)
49 region_mask
|= BIT_ULL(nasid
>> region_shift
);
54 #define rou_rflag rou_flags
56 static int router_distance
;
58 static void router_recurse(klrou_t
*router_a
, klrou_t
*router_b
, int depth
)
64 if (router_a
->rou_rflag
== 1)
67 if (depth
>= router_distance
)
70 router_a
->rou_rflag
= 1;
72 for (port
= 1; port
<= MAX_ROUTER_PORTS
; port
++) {
73 if (router_a
->rou_port
[port
].port_nasid
== INVALID_NASID
)
76 brd
= (lboard_t
*)NODE_OFFSET_TO_K0(
77 router_a
->rou_port
[port
].port_nasid
,
78 router_a
->rou_port
[port
].port_offset
);
80 if (brd
->brd_type
== KLTYPE_ROUTER
) {
81 router
= (klrou_t
*)NODE_OFFSET_TO_K0(NASID_GET(brd
), brd
->brd_compts
[0]);
82 if (router
== router_b
) {
83 if (depth
< router_distance
)
84 router_distance
= depth
;
87 router_recurse(router
, router_b
, depth
+ 1);
91 router_a
->rou_rflag
= 0;
94 unsigned char __node_distances
[MAX_NUMNODES
][MAX_NUMNODES
];
95 EXPORT_SYMBOL(__node_distances
);
97 static int __init
compute_node_distance(nasid_t nasid_a
, nasid_t nasid_b
)
99 klrou_t
*router
, *router_a
= NULL
, *router_b
= NULL
;
100 lboard_t
*brd
, *dest_brd
;
104 /* Figure out which routers nodes in question are connected to */
105 for_each_online_node(nasid
) {
106 brd
= find_lboard_class((lboard_t
*)KL_CONFIG_INFO(nasid
),
113 if (brd
->brd_flags
& DUPLICATE_BOARD
)
116 router
= (klrou_t
*)NODE_OFFSET_TO_K0(NASID_GET(brd
), brd
->brd_compts
[0]);
117 router
->rou_rflag
= 0;
119 for (port
= 1; port
<= MAX_ROUTER_PORTS
; port
++) {
120 if (router
->rou_port
[port
].port_nasid
== INVALID_NASID
)
123 dest_brd
= (lboard_t
*)NODE_OFFSET_TO_K0(
124 router
->rou_port
[port
].port_nasid
,
125 router
->rou_port
[port
].port_offset
);
127 if (dest_brd
->brd_type
== KLTYPE_IP27
) {
128 if (dest_brd
->brd_nasid
== nasid_a
)
130 if (dest_brd
->brd_nasid
== nasid_b
)
135 } while ((brd
= find_lboard_class(KLCF_NEXT(brd
), KLTYPE_ROUTER
)));
138 if (nasid_a
== nasid_b
)
139 return LOCAL_DISTANCE
;
141 if (router_a
== router_b
)
142 return LOCAL_DISTANCE
+ 1;
144 if (router_a
== NULL
) {
145 pr_info("node_distance: router_a NULL\n");
148 if (router_b
== NULL
) {
149 pr_info("node_distance: router_b NULL\n");
153 router_distance
= 100;
154 router_recurse(router_a
, router_b
, 2);
156 return LOCAL_DISTANCE
+ router_distance
;
159 static void __init
init_topology_matrix(void)
163 for (row
= 0; row
< MAX_NUMNODES
; row
++)
164 for (col
= 0; col
< MAX_NUMNODES
; col
++)
165 __node_distances
[row
][col
] = -1;
167 for_each_online_node(row
) {
168 for_each_online_node(col
) {
169 __node_distances
[row
][col
] =
170 compute_node_distance(row
, col
);
175 static void __init
dump_topology(void)
178 lboard_t
*brd
, *dest_brd
;
184 pr_info("************** Topology ********************\n");
187 for_each_online_node(col
)
188 pr_cont("%02d ", col
);
190 for_each_online_node(row
) {
191 pr_info("%02d ", row
);
192 for_each_online_node(col
)
193 pr_cont("%2d ", node_distance(row
, col
));
197 for_each_online_node(nasid
) {
198 brd
= find_lboard_class((lboard_t
*)KL_CONFIG_INFO(nasid
),
205 if (brd
->brd_flags
& DUPLICATE_BOARD
)
207 pr_cont("Router %d:", router_num
);
210 router
= (klrou_t
*)NODE_OFFSET_TO_K0(NASID_GET(brd
), brd
->brd_compts
[0]);
212 for (port
= 1; port
<= MAX_ROUTER_PORTS
; port
++) {
213 if (router
->rou_port
[port
].port_nasid
== INVALID_NASID
)
216 dest_brd
= (lboard_t
*)NODE_OFFSET_TO_K0(
217 router
->rou_port
[port
].port_nasid
,
218 router
->rou_port
[port
].port_offset
);
220 if (dest_brd
->brd_type
== KLTYPE_IP27
)
221 pr_cont(" %d", dest_brd
->brd_nasid
);
222 if (dest_brd
->brd_type
== KLTYPE_ROUTER
)
227 } while ( (brd
= find_lboard_class(KLCF_NEXT(brd
), KLTYPE_ROUTER
)) );
231 static unsigned long __init
slot_getbasepfn(nasid_t nasid
, int slot
)
233 return ((unsigned long)nasid
<< PFN_NASIDSHFT
) | (slot
<< SLOT_PFNSHIFT
);
236 static unsigned long __init
slot_psize_compute(nasid_t nasid
, int slot
)
242 /* Find the node board */
243 brd
= find_lboard((lboard_t
*)KL_CONFIG_INFO(nasid
), KLTYPE_IP27
);
247 /* Get the memory bank structure */
248 banks
= (klmembnk_t
*) find_first_component(brd
, KLSTRUCT_MEMBNK
);
252 /* Size in _Megabytes_ */
253 size
= (unsigned long)banks
->membnk_bnksz
[slot
/4];
255 /* hack for 128 dimm banks */
258 size
<<= 20; /* size in bytes */
259 return size
>> PAGE_SHIFT
;
265 return size
>> PAGE_SHIFT
;
269 static void __init
mlreset(void)
274 master_nasid
= get_nasid();
277 * Probe for all CPUs - this creates the cpumask and sets up the
278 * mapping tables. We need to do this as early as possible.
284 init_topology_matrix();
287 region_mask
= gen_region_mask();
289 setup_replication_mask();
292 * Set all nodes' calias sizes to 8k
294 for_each_online_node(nasid
) {
296 * Always have node 0 in the region mask, otherwise
297 * CALIAS accesses get exceptions since the hub
298 * thinks it is a node 0 address.
300 REMOTE_HUB_S(nasid
, PI_REGION_PRESENT
, (region_mask
| 1));
301 REMOTE_HUB_S(nasid
, PI_CALIAS_SIZE
, PI_CALIAS_SIZE_0
);
305 * Set up all hubs to have a big window pointing at
306 * widget 0. Memory mode, widget 0, offset 0
308 REMOTE_HUB_S(nasid
, IIO_ITTE(SWIN0_BIGWIN
),
309 ((HUB_PIO_MAP_TO_MEM
<< IIO_ITTE_IOSP_SHIFT
) |
310 (0 << IIO_ITTE_WIDGET_SHIFT
)));
315 static void __init
szmem(void)
317 unsigned long slot_psize
, slot0sz
= 0, nodebytes
; /* Hack to detect problem configs */
321 for_each_online_node(node
) {
323 for (slot
= 0; slot
< MAX_MEM_SLOTS
; slot
++) {
324 slot_psize
= slot_psize_compute(node
, slot
);
326 slot0sz
= slot_psize
;
328 * We need to refine the hack when we have replicated
331 nodebytes
+= (1LL << SLOT_SHIFT
);
336 if ((nodebytes
>> PAGE_SHIFT
) * (sizeof(struct page
)) >
337 (slot0sz
<< PAGE_SHIFT
)) {
338 pr_info("Ignoring slot %d onwards on node %d\n",
340 slot
= MAX_MEM_SLOTS
;
343 memblock_add_node(PFN_PHYS(slot_getbasepfn(node
, slot
)),
344 PFN_PHYS(slot_psize
), node
);
349 static void __init
node_mem_init(nasid_t node
)
351 unsigned long slot_firstpfn
= slot_getbasepfn(node
, 0);
352 unsigned long slot_freepfn
= node_getfirstfree(node
);
353 unsigned long start_pfn
, end_pfn
;
355 get_pfn_range_for_nid(node
, &start_pfn
, &end_pfn
);
358 * Allocate the node data structures on the node first.
360 __node_data
[node
] = __va(slot_freepfn
<< PAGE_SHIFT
);
361 memset(__node_data
[node
], 0, PAGE_SIZE
);
363 NODE_DATA(node
)->node_start_pfn
= start_pfn
;
364 NODE_DATA(node
)->node_spanned_pages
= end_pfn
- start_pfn
;
366 cpumask_clear(&hub_data(node
)->h_cpus
);
368 slot_freepfn
+= PFN_UP(sizeof(struct pglist_data
) +
369 sizeof(struct hub_data
));
371 memblock_reserve(slot_firstpfn
<< PAGE_SHIFT
,
372 ((slot_freepfn
- slot_firstpfn
) << PAGE_SHIFT
));
376 * A node with nothing. We use it to avoid any special casing in
379 static struct node_data null_node
= {
381 .h_cpus
= CPU_MASK_NONE
386 * Currently, the intranode memory hole support assumes that each slot
387 * contains at least 32 MBytes of memory. We assume all bootmem data
388 * fits on the first slot.
390 void __init
prom_meminit(void)
396 max_low_pfn
= PHYS_PFN(memblock_end_of_DRAM());
398 for (node
= 0; node
< MAX_NUMNODES
; node
++) {
399 if (node_online(node
)) {
403 __node_data
[node
] = &null_node
;
409 void __init
prom_free_prom_memory(void)
411 /* We got nothing to free here ... */
414 extern void setup_zero_pages(void);
416 void __init
paging_init(void)
418 unsigned long zones_size
[MAX_NR_ZONES
] = {0, };
421 zones_size
[ZONE_NORMAL
] = max_low_pfn
;
422 free_area_init_nodes(zones_size
);
425 void __init
mem_init(void)
427 high_memory
= (void *) __va(get_num_physpages() << PAGE_SHIFT
);
429 setup_zero_pages(); /* This comes from node 0 */
430 mem_init_print_info(NULL
);