treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[muen/linux.git] / arch / mips / loongson64 / loongson-3 / numa.c
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 node_data prealloc__node_data[MAX_NUMNODES];
30 unsigned char __node_distances[MAX_NUMNODES][MAX_NUMNODES];
31 EXPORT_SYMBOL(__node_distances);
32 struct node_data *__node_data[MAX_NUMNODES];
33 EXPORT_SYMBOL(__node_data);
34
35 static void enable_lpa(void)
36 {
37         unsigned long value;
38
39         value = __read_32bit_c0_register($16, 3);
40         value |= 0x00000080;
41         __write_32bit_c0_register($16, 3, value);
42         value = __read_32bit_c0_register($16, 3);
43         pr_info("CP0_Config3: CP0 16.3 (0x%lx)\n", value);
44
45         value = __read_32bit_c0_register($5, 1);
46         value |= 0x20000000;
47         __write_32bit_c0_register($5, 1, value);
48         value = __read_32bit_c0_register($5, 1);
49         pr_info("CP0_PageGrain: CP0 5.1 (0x%lx)\n", value);
50 }
51
52 static void cpu_node_probe(void)
53 {
54         int i;
55
56         nodes_clear(node_possible_map);
57         nodes_clear(node_online_map);
58         for (i = 0; i < loongson_sysconf.nr_nodes; i++) {
59                 node_set_state(num_online_nodes(), N_POSSIBLE);
60                 node_set_online(num_online_nodes());
61         }
62
63         pr_info("NUMA: Discovered %d cpus on %d nodes\n",
64                 loongson_sysconf.nr_cpus, num_online_nodes());
65 }
66
67 static int __init compute_node_distance(int row, int col)
68 {
69         int package_row = row * loongson_sysconf.cores_per_node /
70                                 loongson_sysconf.cores_per_package;
71         int package_col = col * loongson_sysconf.cores_per_node /
72                                 loongson_sysconf.cores_per_package;
73
74         if (col == row)
75                 return 0;
76         else if (package_row == package_col)
77                 return 40;
78         else
79                 return 100;
80 }
81
82 static void __init init_topology_matrix(void)
83 {
84         int row, col;
85
86         for (row = 0; row < MAX_NUMNODES; row++)
87                 for (col = 0; col < MAX_NUMNODES; col++)
88                         __node_distances[row][col] = -1;
89
90         for_each_online_node(row) {
91                 for_each_online_node(col) {
92                         __node_distances[row][col] =
93                                 compute_node_distance(row, col);
94                 }
95         }
96 }
97
98 static unsigned long nid_to_addroffset(unsigned int nid)
99 {
100         unsigned long result;
101         switch (nid) {
102         case 0:
103         default:
104                 result = NODE0_ADDRSPACE_OFFSET;
105                 break;
106         case 1:
107                 result = NODE1_ADDRSPACE_OFFSET;
108                 break;
109         case 2:
110                 result = NODE2_ADDRSPACE_OFFSET;
111                 break;
112         case 3:
113                 result = NODE3_ADDRSPACE_OFFSET;
114                 break;
115         }
116         return result;
117 }
118
119 static void __init szmem(unsigned int node)
120 {
121         u32 i, mem_type;
122         static unsigned long num_physpages = 0;
123         u64 node_id, node_psize, start_pfn, end_pfn, mem_start, mem_size;
124
125         /* Parse memory information and activate */
126         for (i = 0; i < loongson_memmap->nr_map; i++) {
127                 node_id = loongson_memmap->map[i].node_id;
128                 if (node_id != node)
129                         continue;
130
131                 mem_type = loongson_memmap->map[i].mem_type;
132                 mem_size = loongson_memmap->map[i].mem_size;
133                 mem_start = loongson_memmap->map[i].mem_start;
134
135                 switch (mem_type) {
136                 case SYSTEM_RAM_LOW:
137                         start_pfn = ((node_id << 44) + mem_start) >> PAGE_SHIFT;
138                         node_psize = (mem_size << 20) >> PAGE_SHIFT;
139                         end_pfn  = start_pfn + node_psize;
140                         num_physpages += node_psize;
141                         pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
142                                 (u32)node_id, mem_type, mem_start, mem_size);
143                         pr_info("       start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
144                                 start_pfn, end_pfn, num_physpages);
145                         add_memory_region((node_id << 44) + mem_start,
146                                 (u64)mem_size << 20, BOOT_MEM_RAM);
147                         memblock_add_node(PFN_PHYS(start_pfn),
148                                 PFN_PHYS(end_pfn - start_pfn), node);
149                         break;
150                 case SYSTEM_RAM_HIGH:
151                         start_pfn = ((node_id << 44) + mem_start) >> PAGE_SHIFT;
152                         node_psize = (mem_size << 20) >> PAGE_SHIFT;
153                         end_pfn  = start_pfn + node_psize;
154                         num_physpages += node_psize;
155                         pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
156                                 (u32)node_id, mem_type, mem_start, mem_size);
157                         pr_info("       start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
158                                 start_pfn, end_pfn, num_physpages);
159                         add_memory_region((node_id << 44) + mem_start,
160                                 (u64)mem_size << 20, BOOT_MEM_RAM);
161                         memblock_add_node(PFN_PHYS(start_pfn),
162                                 PFN_PHYS(end_pfn - start_pfn), node);
163                         break;
164                 case SYSTEM_RAM_RESERVED:
165                         pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx MB\n",
166                                 (u32)node_id, mem_type, mem_start, mem_size);
167                         add_memory_region((node_id << 44) + mem_start,
168                                 (u64)mem_size << 20, BOOT_MEM_RESERVED);
169                         memblock_reserve(((node_id << 44) + mem_start),
170                                 mem_size << 20);
171                         break;
172                 }
173         }
174 }
175
176 static void __init node_mem_init(unsigned int node)
177 {
178         unsigned long node_addrspace_offset;
179         unsigned long start_pfn, end_pfn;
180
181         node_addrspace_offset = nid_to_addroffset(node);
182         pr_info("Node%d's addrspace_offset is 0x%lx\n",
183                         node, node_addrspace_offset);
184
185         get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
186         pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n",
187                 node, start_pfn, end_pfn);
188
189         __node_data[node] = prealloc__node_data + node;
190
191         NODE_DATA(node)->node_start_pfn = start_pfn;
192         NODE_DATA(node)->node_spanned_pages = end_pfn - start_pfn;
193
194         free_bootmem_with_active_regions(node, end_pfn);
195
196         if (node == 0) {
197                 /* kernel end address */
198                 unsigned long kernel_end_pfn = PFN_UP(__pa_symbol(&_end));
199
200                 /* used by finalize_initrd() */
201                 max_low_pfn = end_pfn;
202
203                 /* Reserve the kernel text/data/bss */
204                 memblock_reserve(start_pfn << PAGE_SHIFT,
205                                  ((kernel_end_pfn - start_pfn) << PAGE_SHIFT));
206
207                 /* Reserve 0xfe000000~0xffffffff for RS780E integrated GPU */
208                 if (node_end_pfn(0) >= (0xffffffff >> PAGE_SHIFT))
209                         memblock_reserve((node_addrspace_offset | 0xfe000000),
210                                          32 << 20);
211         }
212
213         sparse_memory_present_with_active_regions(node);
214 }
215
216 static __init void prom_meminit(void)
217 {
218         unsigned int node, cpu, active_cpu = 0;
219
220         cpu_node_probe();
221         init_topology_matrix();
222
223         for (node = 0; node < loongson_sysconf.nr_nodes; node++) {
224                 if (node_online(node)) {
225                         szmem(node);
226                         node_mem_init(node);
227                         cpumask_clear(&__node_data[(node)]->cpumask);
228                 }
229         }
230         max_low_pfn = PHYS_PFN(memblock_end_of_DRAM());
231
232         for (cpu = 0; cpu < loongson_sysconf.nr_cpus; cpu++) {
233                 node = cpu / loongson_sysconf.cores_per_node;
234                 if (node >= num_online_nodes())
235                         node = 0;
236
237                 if (loongson_sysconf.reserved_cpus_mask & (1<<cpu))
238                         continue;
239
240                 cpumask_set_cpu(active_cpu, &__node_data[(node)]->cpumask);
241                 pr_info("NUMA: set cpumask cpu %d on node %d\n", active_cpu, node);
242
243                 active_cpu++;
244         }
245 }
246
247 void __init paging_init(void)
248 {
249         unsigned long zones_size[MAX_NR_ZONES] = {0, };
250
251         pagetable_init();
252 #ifdef CONFIG_ZONE_DMA32
253         zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
254 #endif
255         zones_size[ZONE_NORMAL] = max_low_pfn;
256         free_area_init_nodes(zones_size);
257 }
258
259 void __init mem_init(void)
260 {
261         high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT);
262         memblock_free_all();
263         setup_zero_pages();     /* This comes from node 0 */
264         mem_init_print_info(NULL);
265 }
266
267 /* All PCI device belongs to logical Node-0 */
268 int pcibus_to_node(struct pci_bus *bus)
269 {
270         return 0;
271 }
272 EXPORT_SYMBOL(pcibus_to_node);
273
274 void __init prom_init_numa_memory(void)
275 {
276         enable_lpa();
277         prom_meminit();
278 }
279 EXPORT_SYMBOL(prom_init_numa_memory);