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. |
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 completely bogus but |
11 | * alternate easier to use mechanisms are available. |
12 | */ |
13 | #include <linux/init.h> |
14 | #include <linux/kernel.h> |
15 | #include <linux/memblock.h> |
16 | #include <linux/mm.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> |
23 | #include <asm/page.h> |
24 | #include <asm/pgalloc.h> |
25 | #include <asm/sections.h> |
26 | #include <asm/sgialib.h> |
27 | |
28 | #include <asm/sn/arch.h> |
29 | #include <asm/sn/agent.h> |
30 | #include <asm/sn/klconfig.h> |
31 | |
32 | #include "ip27-common.h" |
33 | |
34 | #define SLOT_PFNSHIFT (SLOT_SHIFT - PAGE_SHIFT) |
35 | #define PFN_NASIDSHFT (NASID_SHFT - PAGE_SHIFT) |
36 | |
37 | struct node_data *__node_data[MAX_NUMNODES]; |
38 | |
39 | EXPORT_SYMBOL(__node_data); |
40 | |
41 | static u64 gen_region_mask(void) |
42 | { |
43 | int region_shift; |
44 | u64 region_mask; |
45 | nasid_t nasid; |
46 | |
47 | region_shift = get_region_shift(); |
48 | region_mask = 0; |
49 | for_each_online_node(nasid) |
50 | region_mask |= BIT_ULL(nasid >> region_shift); |
51 | |
52 | return region_mask; |
53 | } |
54 | |
55 | #define rou_rflag rou_flags |
56 | |
57 | static int router_distance; |
58 | |
59 | static void router_recurse(klrou_t *router_a, klrou_t *router_b, int depth) |
60 | { |
61 | klrou_t *router; |
62 | lboard_t *brd; |
63 | int port; |
64 | |
65 | if (router_a->rou_rflag == 1) |
66 | return; |
67 | |
68 | if (depth >= router_distance) |
69 | return; |
70 | |
71 | router_a->rou_rflag = 1; |
72 | |
73 | for (port = 1; port <= MAX_ROUTER_PORTS; port++) { |
74 | if (router_a->rou_port[port].port_nasid == INVALID_NASID) |
75 | continue; |
76 | |
77 | brd = (lboard_t *)NODE_OFFSET_TO_K0( |
78 | router_a->rou_port[port].port_nasid, |
79 | router_a->rou_port[port].port_offset); |
80 | |
81 | if (brd->brd_type == KLTYPE_ROUTER) { |
82 | router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]); |
83 | if (router == router_b) { |
84 | if (depth < router_distance) |
85 | router_distance = depth; |
86 | } |
87 | else |
88 | router_recurse(router, router_b, depth + 1); |
89 | } |
90 | } |
91 | |
92 | router_a->rou_rflag = 0; |
93 | } |
94 | |
95 | unsigned char __node_distances[MAX_NUMNODES][MAX_NUMNODES]; |
96 | EXPORT_SYMBOL(__node_distances); |
97 | |
98 | static int __init compute_node_distance(nasid_t nasid_a, nasid_t nasid_b) |
99 | { |
100 | klrou_t *router, *router_a = NULL, *router_b = NULL; |
101 | lboard_t *brd, *dest_brd; |
102 | nasid_t nasid; |
103 | int port; |
104 | |
105 | /* Figure out which routers nodes in question are connected to */ |
106 | for_each_online_node(nasid) { |
107 | brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid), |
108 | KLTYPE_ROUTER); |
109 | |
110 | if (!brd) |
111 | continue; |
112 | |
113 | do { |
114 | if (brd->brd_flags & DUPLICATE_BOARD) |
115 | continue; |
116 | |
117 | router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]); |
118 | router->rou_rflag = 0; |
119 | |
120 | for (port = 1; port <= MAX_ROUTER_PORTS; port++) { |
121 | if (router->rou_port[port].port_nasid == INVALID_NASID) |
122 | continue; |
123 | |
124 | dest_brd = (lboard_t *)NODE_OFFSET_TO_K0( |
125 | router->rou_port[port].port_nasid, |
126 | router->rou_port[port].port_offset); |
127 | |
128 | if (dest_brd->brd_type == KLTYPE_IP27) { |
129 | if (dest_brd->brd_nasid == nasid_a) |
130 | router_a = router; |
131 | if (dest_brd->brd_nasid == nasid_b) |
132 | router_b = router; |
133 | } |
134 | } |
135 | |
136 | } while ((brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER))); |
137 | } |
138 | |
139 | if (nasid_a == nasid_b) |
140 | return LOCAL_DISTANCE; |
141 | |
142 | if (router_a == router_b) |
143 | return LOCAL_DISTANCE + 1; |
144 | |
145 | if (router_a == NULL) { |
146 | pr_info("node_distance: router_a NULL\n" ); |
147 | return 255; |
148 | } |
149 | if (router_b == NULL) { |
150 | pr_info("node_distance: router_b NULL\n" ); |
151 | return 255; |
152 | } |
153 | |
154 | router_distance = 100; |
155 | router_recurse(router_a, router_b, 2); |
156 | |
157 | return LOCAL_DISTANCE + router_distance; |
158 | } |
159 | |
160 | static void __init init_topology_matrix(void) |
161 | { |
162 | nasid_t row, col; |
163 | |
164 | for (row = 0; row < MAX_NUMNODES; row++) |
165 | for (col = 0; col < MAX_NUMNODES; col++) |
166 | __node_distances[row][col] = -1; |
167 | |
168 | for_each_online_node(row) { |
169 | for_each_online_node(col) { |
170 | __node_distances[row][col] = |
171 | compute_node_distance(row, col); |
172 | } |
173 | } |
174 | } |
175 | |
176 | static void __init dump_topology(void) |
177 | { |
178 | nasid_t nasid; |
179 | lboard_t *brd, *dest_brd; |
180 | int port; |
181 | int router_num = 0; |
182 | klrou_t *router; |
183 | nasid_t row, col; |
184 | |
185 | pr_info("************** Topology ********************\n" ); |
186 | |
187 | pr_info(" " ); |
188 | for_each_online_node(col) |
189 | pr_cont("%02d " , col); |
190 | pr_cont("\n" ); |
191 | for_each_online_node(row) { |
192 | pr_info("%02d " , row); |
193 | for_each_online_node(col) |
194 | pr_cont("%2d " , node_distance(row, col)); |
195 | pr_cont("\n" ); |
196 | } |
197 | |
198 | for_each_online_node(nasid) { |
199 | brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid), |
200 | KLTYPE_ROUTER); |
201 | |
202 | if (!brd) |
203 | continue; |
204 | |
205 | do { |
206 | if (brd->brd_flags & DUPLICATE_BOARD) |
207 | continue; |
208 | pr_cont("Router %d:" , router_num); |
209 | router_num++; |
210 | |
211 | router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]); |
212 | |
213 | for (port = 1; port <= MAX_ROUTER_PORTS; port++) { |
214 | if (router->rou_port[port].port_nasid == INVALID_NASID) |
215 | continue; |
216 | |
217 | dest_brd = (lboard_t *)NODE_OFFSET_TO_K0( |
218 | router->rou_port[port].port_nasid, |
219 | router->rou_port[port].port_offset); |
220 | |
221 | if (dest_brd->brd_type == KLTYPE_IP27) |
222 | pr_cont(" %d" , dest_brd->brd_nasid); |
223 | if (dest_brd->brd_type == KLTYPE_ROUTER) |
224 | pr_cont(" r" ); |
225 | } |
226 | pr_cont("\n" ); |
227 | |
228 | } while ( (brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)) ); |
229 | } |
230 | } |
231 | |
232 | static unsigned long __init slot_getbasepfn(nasid_t nasid, int slot) |
233 | { |
234 | return ((unsigned long)nasid << PFN_NASIDSHFT) | (slot << SLOT_PFNSHIFT); |
235 | } |
236 | |
237 | static unsigned long __init slot_psize_compute(nasid_t nasid, int slot) |
238 | { |
239 | lboard_t *brd; |
240 | klmembnk_t *banks; |
241 | unsigned long size; |
242 | |
243 | /* Find the node board */ |
244 | brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27); |
245 | if (!brd) |
246 | return 0; |
247 | |
248 | /* Get the memory bank structure */ |
249 | banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK); |
250 | if (!banks) |
251 | return 0; |
252 | |
253 | /* Size in _Megabytes_ */ |
254 | size = (unsigned long)banks->membnk_bnksz[slot/4]; |
255 | |
256 | /* hack for 128 dimm banks */ |
257 | if (size <= 128) { |
258 | if (slot % 4 == 0) { |
259 | size <<= 20; /* size in bytes */ |
260 | return size >> PAGE_SHIFT; |
261 | } else |
262 | return 0; |
263 | } else { |
264 | size /= 4; |
265 | size <<= 20; |
266 | return size >> PAGE_SHIFT; |
267 | } |
268 | } |
269 | |
270 | static void __init mlreset(void) |
271 | { |
272 | u64 region_mask; |
273 | nasid_t nasid; |
274 | |
275 | master_nasid = get_nasid(); |
276 | |
277 | /* |
278 | * Probe for all CPUs - this creates the cpumask and sets up the |
279 | * mapping tables. We need to do this as early as possible. |
280 | */ |
281 | #ifdef CONFIG_SMP |
282 | cpu_node_probe(); |
283 | #endif |
284 | |
285 | init_topology_matrix(); |
286 | dump_topology(); |
287 | |
288 | region_mask = gen_region_mask(); |
289 | |
290 | setup_replication_mask(); |
291 | |
292 | /* |
293 | * Set all nodes' calias sizes to 8k |
294 | */ |
295 | for_each_online_node(nasid) { |
296 | /* |
297 | * Always have node 0 in the region mask, otherwise |
298 | * CALIAS accesses get exceptions since the hub |
299 | * thinks it is a node 0 address. |
300 | */ |
301 | REMOTE_HUB_S(nasid, PI_REGION_PRESENT, (region_mask | 1)); |
302 | REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_0); |
303 | |
304 | #ifdef LATER |
305 | /* |
306 | * Set up all hubs to have a big window pointing at |
307 | * widget 0. Memory mode, widget 0, offset 0 |
308 | */ |
309 | REMOTE_HUB_S(nasid, IIO_ITTE(SWIN0_BIGWIN), |
310 | ((HUB_PIO_MAP_TO_MEM << IIO_ITTE_IOSP_SHIFT) | |
311 | (0 << IIO_ITTE_WIDGET_SHIFT))); |
312 | #endif |
313 | } |
314 | } |
315 | |
316 | static void __init szmem(void) |
317 | { |
318 | unsigned long slot_psize, slot0sz = 0, nodebytes; /* Hack to detect problem configs */ |
319 | int slot; |
320 | nasid_t node; |
321 | |
322 | for_each_online_node(node) { |
323 | nodebytes = 0; |
324 | for (slot = 0; slot < MAX_MEM_SLOTS; slot++) { |
325 | slot_psize = slot_psize_compute(node, slot); |
326 | if (slot == 0) |
327 | slot0sz = slot_psize; |
328 | /* |
329 | * We need to refine the hack when we have replicated |
330 | * kernel text. |
331 | */ |
332 | nodebytes += (1LL << SLOT_SHIFT); |
333 | |
334 | if (!slot_psize) |
335 | continue; |
336 | |
337 | if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) > |
338 | (slot0sz << PAGE_SHIFT)) { |
339 | pr_info("Ignoring slot %d onwards on node %d\n" , |
340 | slot, node); |
341 | slot = MAX_MEM_SLOTS; |
342 | continue; |
343 | } |
344 | memblock_add_node(PFN_PHYS(slot_getbasepfn(node, slot)), |
345 | PFN_PHYS(slot_psize), node, |
346 | MEMBLOCK_NONE); |
347 | } |
348 | } |
349 | } |
350 | |
351 | static void __init node_mem_init(nasid_t node) |
352 | { |
353 | unsigned long slot_firstpfn = slot_getbasepfn(node, 0); |
354 | unsigned long slot_freepfn = node_getfirstfree(node); |
355 | unsigned long start_pfn, end_pfn; |
356 | |
357 | get_pfn_range_for_nid(nid: node, start_pfn: &start_pfn, end_pfn: &end_pfn); |
358 | |
359 | /* |
360 | * Allocate the node data structures on the node first. |
361 | */ |
362 | __node_data[node] = __va(slot_freepfn << PAGE_SHIFT); |
363 | memset(__node_data[node], 0, PAGE_SIZE); |
364 | |
365 | NODE_DATA(node)->node_start_pfn = start_pfn; |
366 | NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn; |
367 | |
368 | cpumask_clear(dstp: &hub_data(node)->h_cpus); |
369 | |
370 | slot_freepfn += PFN_UP(sizeof(struct pglist_data) + |
371 | sizeof(struct hub_data)); |
372 | |
373 | memblock_reserve(base: slot_firstpfn << PAGE_SHIFT, |
374 | size: ((slot_freepfn - slot_firstpfn) << PAGE_SHIFT)); |
375 | } |
376 | |
377 | /* |
378 | * A node with nothing. We use it to avoid any special casing in |
379 | * cpumask_of_node |
380 | */ |
381 | static struct node_data null_node = { |
382 | .hub = { |
383 | .h_cpus = CPU_MASK_NONE |
384 | } |
385 | }; |
386 | |
387 | /* |
388 | * Currently, the intranode memory hole support assumes that each slot |
389 | * contains at least 32 MBytes of memory. We assume all bootmem data |
390 | * fits on the first slot. |
391 | */ |
392 | void __init prom_meminit(void) |
393 | { |
394 | nasid_t node; |
395 | |
396 | mlreset(); |
397 | szmem(); |
398 | max_low_pfn = PHYS_PFN(memblock_end_of_DRAM()); |
399 | |
400 | for (node = 0; node < MAX_NUMNODES; node++) { |
401 | if (node_online(node)) { |
402 | node_mem_init(node); |
403 | continue; |
404 | } |
405 | __node_data[node] = &null_node; |
406 | } |
407 | } |
408 | |
409 | extern void setup_zero_pages(void); |
410 | |
411 | void __init paging_init(void) |
412 | { |
413 | unsigned long zones_size[MAX_NR_ZONES] = {0, }; |
414 | |
415 | pagetable_init(); |
416 | zones_size[ZONE_NORMAL] = max_low_pfn; |
417 | free_area_init(max_zone_pfn: zones_size); |
418 | } |
419 | |
420 | void __init mem_init(void) |
421 | { |
422 | high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT); |
423 | memblock_free_all(); |
424 | setup_zero_pages(); /* This comes from node 0 */ |
425 | } |
426 | |
427 | pg_data_t * __init arch_alloc_nodedata(int nid) |
428 | { |
429 | return memblock_alloc(sizeof(pg_data_t), SMP_CACHE_BYTES); |
430 | } |
431 | |
432 | void arch_refresh_nodedata(int nid, pg_data_t *pgdat) |
433 | { |
434 | __node_data[nid] = (struct node_data *)pgdat; |
435 | } |
436 | |