xref: /openbmc/linux/arch/mips/loongson64/numa.c (revision b92dd117)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2010 Loongson Inc. & Lemote Inc. &
4  *                    Institute of Computing Technology
5  * Author:  Xiang Gao, gaoxiang@ict.ac.cn
6  *          Huacai Chen, chenhc@lemote.com
7  *          Xiaofu Meng, Shuangshuang Zhang
8  */
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/mmzone.h>
13 #include <linux/export.h>
14 #include <linux/nodemask.h>
15 #include <linux/swap.h>
16 #include <linux/memblock.h>
17 #include <linux/pfn.h>
18 #include <linux/highmem.h>
19 #include <asm/page.h>
20 #include <asm/pgalloc.h>
21 #include <asm/sections.h>
22 #include <linux/irq.h>
23 #include <asm/bootinfo.h>
24 #include <asm/mc146818-time.h>
25 #include <asm/time.h>
26 #include <asm/wbflush.h>
27 #include <boot_param.h>
28 #include <loongson.h>
29 
30 unsigned char __node_distances[MAX_NUMNODES][MAX_NUMNODES];
31 EXPORT_SYMBOL(__node_distances);
32 struct pglist_data *__node_data[MAX_NUMNODES];
33 EXPORT_SYMBOL(__node_data);
34 
35 cpumask_t __node_cpumask[MAX_NUMNODES];
36 EXPORT_SYMBOL(__node_cpumask);
37 
38 static void cpu_node_probe(void)
39 {
40 	int i;
41 
42 	nodes_clear(node_possible_map);
43 	nodes_clear(node_online_map);
44 	for (i = 0; i < loongson_sysconf.nr_nodes; i++) {
45 		node_set_state(num_online_nodes(), N_POSSIBLE);
46 		node_set_online(num_online_nodes());
47 	}
48 
49 	pr_info("NUMA: Discovered %d cpus on %d nodes\n",
50 		loongson_sysconf.nr_cpus, num_online_nodes());
51 }
52 
53 static int __init compute_node_distance(int row, int col)
54 {
55 	int package_row = row * loongson_sysconf.cores_per_node /
56 				loongson_sysconf.cores_per_package;
57 	int package_col = col * loongson_sysconf.cores_per_node /
58 				loongson_sysconf.cores_per_package;
59 
60 	if (col == row)
61 		return LOCAL_DISTANCE;
62 	else if (package_row == package_col)
63 		return 40;
64 	else
65 		return 100;
66 }
67 
68 static void __init init_topology_matrix(void)
69 {
70 	int row, col;
71 
72 	for (row = 0; row < MAX_NUMNODES; row++)
73 		for (col = 0; col < MAX_NUMNODES; col++)
74 			__node_distances[row][col] = -1;
75 
76 	for_each_online_node(row) {
77 		for_each_online_node(col) {
78 			__node_distances[row][col] =
79 				compute_node_distance(row, col);
80 		}
81 	}
82 }
83 
84 static void __init node_mem_init(unsigned int node)
85 {
86 	struct pglist_data *nd;
87 	unsigned long node_addrspace_offset;
88 	unsigned long start_pfn, end_pfn;
89 	unsigned long nd_pa;
90 	int tnid;
91 	const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
92 
93 	node_addrspace_offset = nid_to_addrbase(node);
94 	pr_info("Node%d's addrspace_offset is 0x%lx\n",
95 			node, node_addrspace_offset);
96 
97 	get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
98 	pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n",
99 		node, start_pfn, end_pfn);
100 
101 	nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, node);
102 	if (!nd_pa)
103 		panic("Cannot allocate %zu bytes for node %d data\n",
104 		      nd_size, node);
105 	nd = __va(nd_pa);
106 	memset(nd, 0, sizeof(struct pglist_data));
107 	tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
108 	if (tnid != node)
109 		pr_info("NODE_DATA(%d) on node %d\n", node, tnid);
110 	__node_data[node] = nd;
111 	NODE_DATA(node)->node_start_pfn = start_pfn;
112 	NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn;
113 
114 	if (node == 0) {
115 		/* kernel start address */
116 		unsigned long kernel_start_pfn = PFN_DOWN(__pa_symbol(&_text));
117 
118 		/* kernel end address */
119 		unsigned long kernel_end_pfn = PFN_UP(__pa_symbol(&_end));
120 
121 		/* used by finalize_initrd() */
122 		max_low_pfn = end_pfn;
123 
124 		/* Reserve the kernel text/data/bss */
125 		memblock_reserve(kernel_start_pfn << PAGE_SHIFT,
126 				 ((kernel_end_pfn - kernel_start_pfn) << PAGE_SHIFT));
127 
128 		/* Reserve 0xfe000000~0xffffffff for RS780E integrated GPU */
129 		if (node_end_pfn(0) >= (0xffffffff >> PAGE_SHIFT))
130 			memblock_reserve((node_addrspace_offset | 0xfe000000),
131 					 32 << 20);
132 
133 		/* Reserve pfn range 0~node[0]->node_start_pfn */
134 		memblock_reserve(0, PAGE_SIZE * start_pfn);
135 	}
136 }
137 
138 static __init void prom_meminit(void)
139 {
140 	unsigned int node, cpu, active_cpu = 0;
141 
142 	cpu_node_probe();
143 	init_topology_matrix();
144 
145 	for (node = 0; node < loongson_sysconf.nr_nodes; node++) {
146 		if (node_online(node)) {
147 			szmem(node);
148 			node_mem_init(node);
149 			cpumask_clear(&__node_cpumask[node]);
150 		}
151 	}
152 	max_low_pfn = PHYS_PFN(memblock_end_of_DRAM());
153 
154 	for (cpu = 0; cpu < loongson_sysconf.nr_cpus; cpu++) {
155 		node = cpu / loongson_sysconf.cores_per_node;
156 		if (node >= num_online_nodes())
157 			node = 0;
158 
159 		if (loongson_sysconf.reserved_cpus_mask & (1<<cpu))
160 			continue;
161 
162 		cpumask_set_cpu(active_cpu, &__node_cpumask[node]);
163 		pr_info("NUMA: set cpumask cpu %d on node %d\n", active_cpu, node);
164 
165 		active_cpu++;
166 	}
167 }
168 
169 void __init paging_init(void)
170 {
171 	unsigned long zones_size[MAX_NR_ZONES] = {0, };
172 
173 	pagetable_init();
174 	zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
175 	zones_size[ZONE_NORMAL] = max_low_pfn;
176 	free_area_init(zones_size);
177 }
178 
179 void __init mem_init(void)
180 {
181 	high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT);
182 	memblock_free_all();
183 	setup_zero_pages();	/* This comes from node 0 */
184 }
185 
186 /* All PCI device belongs to logical Node-0 */
187 int pcibus_to_node(struct pci_bus *bus)
188 {
189 	return 0;
190 }
191 EXPORT_SYMBOL(pcibus_to_node);
192 
193 void __init prom_init_numa_memory(void)
194 {
195 	pr_info("CP0_Config3: CP0 16.3 (0x%x)\n", read_c0_config3());
196 	pr_info("CP0_PageGrain: CP0 5.1 (0x%x)\n", read_c0_pagegrain());
197 	prom_meminit();
198 }
199 
200 pg_data_t * __init arch_alloc_nodedata(int nid)
201 {
202 	return memblock_alloc(sizeof(pg_data_t), SMP_CACHE_BYTES);
203 }
204 
205 void arch_refresh_nodedata(int nid, pg_data_t *pgdat)
206 {
207 	__node_data[nid] = pgdat;
208 }
209