Linux 5.7.6
[linux/fpc-iii.git] / arch / mips / sgi-ip27 / ip27-memory.c
bloba45691e6ab90b04de7703c0b5b403cf04f605e86
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 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>
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>
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)
42 int region_shift;
43 u64 region_mask;
44 nasid_t nasid;
46 region_shift = get_region_shift();
47 region_mask = 0;
48 for_each_online_node(nasid)
49 region_mask |= BIT_ULL(nasid >> region_shift);
51 return region_mask;
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)
60 klrou_t *router;
61 lboard_t *brd;
62 int port;
64 if (router_a->rou_rflag == 1)
65 return;
67 if (depth >= router_distance)
68 return;
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)
74 continue;
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;
86 else
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;
101 nasid_t nasid;
102 int port;
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),
107 KLTYPE_ROUTER);
109 if (!brd)
110 continue;
112 do {
113 if (brd->brd_flags & DUPLICATE_BOARD)
114 continue;
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)
121 continue;
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)
129 router_a = router;
130 if (dest_brd->brd_nasid == nasid_b)
131 router_b = router;
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");
146 return 255;
148 if (router_b == NULL) {
149 pr_info("node_distance: router_b NULL\n");
150 return 255;
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)
161 nasid_t row, col;
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)
177 nasid_t nasid;
178 lboard_t *brd, *dest_brd;
179 int port;
180 int router_num = 0;
181 klrou_t *router;
182 nasid_t row, col;
184 pr_info("************** Topology ********************\n");
186 pr_info(" ");
187 for_each_online_node(col)
188 pr_cont("%02d ", col);
189 pr_cont("\n");
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));
194 pr_cont("\n");
197 for_each_online_node(nasid) {
198 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
199 KLTYPE_ROUTER);
201 if (!brd)
202 continue;
204 do {
205 if (brd->brd_flags & DUPLICATE_BOARD)
206 continue;
207 pr_cont("Router %d:", router_num);
208 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)
214 continue;
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)
223 pr_cont(" r");
225 pr_cont("\n");
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)
238 lboard_t *brd;
239 klmembnk_t *banks;
240 unsigned long size;
242 /* Find the node board */
243 brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_IP27);
244 if (!brd)
245 return 0;
247 /* Get the memory bank structure */
248 banks = (klmembnk_t *) find_first_component(brd, KLSTRUCT_MEMBNK);
249 if (!banks)
250 return 0;
252 /* Size in _Megabytes_ */
253 size = (unsigned long)banks->membnk_bnksz[slot/4];
255 /* hack for 128 dimm banks */
256 if (size <= 128) {
257 if (slot % 4 == 0) {
258 size <<= 20; /* size in bytes */
259 return size >> PAGE_SHIFT;
260 } else
261 return 0;
262 } else {
263 size /= 4;
264 size <<= 20;
265 return size >> PAGE_SHIFT;
269 static void __init mlreset(void)
271 u64 region_mask;
272 nasid_t nasid;
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.
280 #ifdef CONFIG_SMP
281 cpu_node_probe();
282 #endif
284 init_topology_matrix();
285 dump_topology();
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);
303 #ifdef LATER
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)));
311 #endif
315 static void __init szmem(void)
317 unsigned long slot_psize, slot0sz = 0, nodebytes; /* Hack to detect problem configs */
318 int slot;
319 nasid_t node;
321 for_each_online_node(node) {
322 nodebytes = 0;
323 for (slot = 0; slot < MAX_MEM_SLOTS; slot++) {
324 slot_psize = slot_psize_compute(node, slot);
325 if (slot == 0)
326 slot0sz = slot_psize;
328 * We need to refine the hack when we have replicated
329 * kernel text.
331 nodebytes += (1LL << SLOT_SHIFT);
333 if (!slot_psize)
334 continue;
336 if ((nodebytes >> PAGE_SHIFT) * (sizeof(struct page)) >
337 (slot0sz << PAGE_SHIFT)) {
338 pr_info("Ignoring slot %d onwards on node %d\n",
339 slot, node);
340 slot = MAX_MEM_SLOTS;
341 continue;
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
377 * cpumask_of_node
379 static struct node_data null_node = {
380 .hub = {
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)
392 nasid_t node;
394 mlreset();
395 szmem();
396 max_low_pfn = PHYS_PFN(memblock_end_of_DRAM());
398 for (node = 0; node < MAX_NUMNODES; node++) {
399 if (node_online(node)) {
400 node_mem_init(node);
401 continue;
403 __node_data[node] = &null_node;
406 memblocks_present();
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, };
420 pagetable_init();
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);
428 memblock_free_all();
429 setup_zero_pages(); /* This comes from node 0 */
430 mem_init_print_info(NULL);