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