xref: /openbmc/linux/arch/mips/loongson64/numa.c (revision 3a35093a)
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 
29 static struct pglist_data prealloc__node_data[MAX_NUMNODES];
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 szmem(unsigned int node)
85 {
86 	u32 i, mem_type;
87 	static unsigned long num_physpages;
88 	u64 node_id, node_psize, start_pfn, end_pfn, mem_start, mem_size;
89 
90 	/* Parse memory information and activate */
91 	for (i = 0; i < loongson_memmap->nr_map; i++) {
92 		node_id = loongson_memmap->map[i].node_id;
93 		if (node_id != node)
94 			continue;
95 
96 		mem_type = loongson_memmap->map[i].mem_type;
97 		mem_size = loongson_memmap->map[i].mem_size;
98 		mem_start = loongson_memmap->map[i].mem_start;
99 
100 		switch (mem_type) {
101 		case SYSTEM_RAM_LOW:
102 			start_pfn = ((node_id << 44) + mem_start) >> PAGE_SHIFT;
103 			node_psize = (mem_size << 20) >> PAGE_SHIFT;
104 			end_pfn  = start_pfn + node_psize;
105 			num_physpages += node_psize;
106 			pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
107 				(u32)node_id, mem_type, mem_start, mem_size);
108 			pr_info("       start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
109 				start_pfn, end_pfn, num_physpages);
110 			memblock_add_node(PFN_PHYS(start_pfn),
111 				PFN_PHYS(node_psize), node);
112 			break;
113 		case SYSTEM_RAM_HIGH:
114 			start_pfn = ((node_id << 44) + mem_start) >> PAGE_SHIFT;
115 			node_psize = (mem_size << 20) >> PAGE_SHIFT;
116 			end_pfn  = start_pfn + node_psize;
117 			num_physpages += node_psize;
118 			pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
119 				(u32)node_id, mem_type, mem_start, mem_size);
120 			pr_info("       start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
121 				start_pfn, end_pfn, num_physpages);
122 			memblock_add_node(PFN_PHYS(start_pfn),
123 				PFN_PHYS(node_psize), node);
124 			break;
125 		case SYSTEM_RAM_RESERVED:
126 			pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
127 				(u32)node_id, mem_type, mem_start, mem_size);
128 			memblock_reserve(((node_id << 44) + mem_start),
129 				mem_size << 20);
130 			break;
131 		}
132 	}
133 }
134 
135 static void __init node_mem_init(unsigned int node)
136 {
137 	unsigned long node_addrspace_offset;
138 	unsigned long start_pfn, end_pfn;
139 
140 	node_addrspace_offset = nid_to_addrbase(node);
141 	pr_info("Node%d's addrspace_offset is 0x%lx\n",
142 			node, node_addrspace_offset);
143 
144 	get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
145 	pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n",
146 		node, start_pfn, end_pfn);
147 
148 	__node_data[node] = prealloc__node_data + node;
149 
150 	NODE_DATA(node)->node_start_pfn = start_pfn;
151 	NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn;
152 
153 	if (node == 0) {
154 		/* kernel start address */
155 		unsigned long kernel_start_pfn = PFN_DOWN(__pa_symbol(&_text));
156 
157 		/* kernel end address */
158 		unsigned long kernel_end_pfn = PFN_UP(__pa_symbol(&_end));
159 
160 		/* used by finalize_initrd() */
161 		max_low_pfn = end_pfn;
162 
163 		/* Reserve the kernel text/data/bss */
164 		memblock_reserve(kernel_start_pfn << PAGE_SHIFT,
165 				 ((kernel_end_pfn - kernel_start_pfn) << PAGE_SHIFT));
166 
167 		/* Reserve 0xfe000000~0xffffffff for RS780E integrated GPU */
168 		if (node_end_pfn(0) >= (0xffffffff >> PAGE_SHIFT))
169 			memblock_reserve((node_addrspace_offset | 0xfe000000),
170 					 32 << 20);
171 	}
172 }
173 
174 static __init void prom_meminit(void)
175 {
176 	unsigned int node, cpu, active_cpu = 0;
177 
178 	cpu_node_probe();
179 	init_topology_matrix();
180 
181 	for (node = 0; node < loongson_sysconf.nr_nodes; node++) {
182 		if (node_online(node)) {
183 			szmem(node);
184 			node_mem_init(node);
185 			cpumask_clear(&__node_cpumask[node]);
186 		}
187 	}
188 	max_low_pfn = PHYS_PFN(memblock_end_of_DRAM());
189 
190 	for (cpu = 0; cpu < loongson_sysconf.nr_cpus; cpu++) {
191 		node = cpu / loongson_sysconf.cores_per_node;
192 		if (node >= num_online_nodes())
193 			node = 0;
194 
195 		if (loongson_sysconf.reserved_cpus_mask & (1<<cpu))
196 			continue;
197 
198 		cpumask_set_cpu(active_cpu, &__node_cpumask[node]);
199 		pr_info("NUMA: set cpumask cpu %d on node %d\n", active_cpu, node);
200 
201 		active_cpu++;
202 	}
203 }
204 
205 void __init paging_init(void)
206 {
207 	unsigned long zones_size[MAX_NR_ZONES] = {0, };
208 
209 	pagetable_init();
210 	zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
211 	zones_size[ZONE_NORMAL] = max_low_pfn;
212 	free_area_init(zones_size);
213 }
214 
215 void __init mem_init(void)
216 {
217 	high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT);
218 	memblock_free_all();
219 	setup_zero_pages();	/* This comes from node 0 */
220 	mem_init_print_info(NULL);
221 }
222 
223 /* All PCI device belongs to logical Node-0 */
224 int pcibus_to_node(struct pci_bus *bus)
225 {
226 	return 0;
227 }
228 EXPORT_SYMBOL(pcibus_to_node);
229 
230 void __init prom_init_numa_memory(void)
231 {
232 	pr_info("CP0_Config3: CP0 16.3 (0x%x)\n", read_c0_config3());
233 	pr_info("CP0_PageGrain: CP0 5.1 (0x%x)\n", read_c0_pagegrain());
234 	prom_meminit();
235 }
236 EXPORT_SYMBOL(prom_init_numa_memory);
237