1 // SPDX-License-Identifier: GPL-2.0
6 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
8 #include <linux/init.h>
9 #include <linux/kernel.h>
11 #include <linux/mmzone.h>
12 #include <linux/export.h>
13 #include <linux/nodemask.h>
14 #include <linux/swap.h>
15 #include <linux/memblock.h>
16 #include <linux/pfn.h>
17 #include <linux/acpi.h>
18 #include <linux/efi.h>
19 #include <linux/irq.h>
20 #include <linux/pci.h>
21 #include <asm/bootinfo.h>
22 #include <asm/loongson.h>
25 #include <asm/pgalloc.h>
26 #include <asm/sections.h>
30 unsigned char node_distances[MAX_NUMNODES][MAX_NUMNODES];
31 EXPORT_SYMBOL(node_distances);
33 static struct numa_meminfo numa_meminfo;
34 cpumask_t cpus_on_node[MAX_NUMNODES];
35 cpumask_t phys_cpus_on_node[MAX_NUMNODES];
36 EXPORT_SYMBOL(cpus_on_node);
39 * apicid, cpu, node mappings
41 s16 __cpuid_to_node[CONFIG_NR_CPUS] = {
42 [0 ... CONFIG_NR_CPUS - 1] = NUMA_NO_NODE
44 EXPORT_SYMBOL(__cpuid_to_node);
46 nodemask_t numa_nodes_parsed __initdata;
48 #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
49 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
50 EXPORT_SYMBOL(__per_cpu_offset);
52 static int __init pcpu_cpu_to_node(int cpu)
54 return early_cpu_to_node(cpu);
57 static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
59 if (early_cpu_to_node(from) == early_cpu_to_node(to))
60 return LOCAL_DISTANCE;
62 return REMOTE_DISTANCE;
65 void __init pcpu_populate_pte(unsigned long addr)
67 populate_kernel_pte(addr);
70 void __init setup_per_cpu_areas(void)
76 if (pcpu_chosen_fc == PCPU_FC_AUTO) {
78 pcpu_chosen_fc = PCPU_FC_PAGE;
80 pcpu_chosen_fc = PCPU_FC_EMBED;
84 * Always reserve area for module percpu variables. That's
85 * what the legacy allocator did.
87 if (pcpu_chosen_fc != PCPU_FC_PAGE) {
88 rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
89 PERCPU_DYNAMIC_RESERVE, PMD_SIZE,
90 pcpu_cpu_distance, pcpu_cpu_to_node);
92 pr_warn("%s allocator failed (%d), falling back to page size\n",
93 pcpu_fc_names[pcpu_chosen_fc], rc);
96 rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE, pcpu_cpu_to_node);
98 panic("cannot initialize percpu area (err=%d)", rc);
100 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
101 for_each_possible_cpu(cpu)
102 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
107 * Get nodeid by logical cpu number.
108 * __cpuid_to_node maps phyical cpu id to node, so we
109 * should use cpu_logical_map(cpu) to index it.
111 * This routine is only used in early phase during
112 * booting, after setup_per_cpu_areas calling and numa_node
113 * initialization, cpu_to_node will be used instead.
115 int early_cpu_to_node(int cpu)
117 int physid = cpu_logical_map(cpu);
122 return __cpuid_to_node[physid];
125 void __init early_numa_add_cpu(int cpuid, s16 node)
127 int cpu = __cpu_number_map[cpuid];
132 cpumask_set_cpu(cpu, &cpus_on_node[node]);
133 cpumask_set_cpu(cpuid, &phys_cpus_on_node[node]);
136 void numa_add_cpu(unsigned int cpu)
138 int nid = cpu_to_node(cpu);
139 cpumask_set_cpu(cpu, &cpus_on_node[nid]);
142 void numa_remove_cpu(unsigned int cpu)
144 int nid = cpu_to_node(cpu);
145 cpumask_clear_cpu(cpu, &cpus_on_node[nid]);
148 static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
149 struct numa_meminfo *mi)
151 /* ignore zero length blks */
155 /* whine about and ignore invalid blks */
156 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
157 pr_warn("NUMA: Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
158 nid, start, end - 1);
162 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
163 pr_err("NUMA: too many memblk ranges\n");
167 mi->blk[mi->nr_blks].start = PFN_ALIGN(start);
168 mi->blk[mi->nr_blks].end = PFN_ALIGN(end - PAGE_SIZE + 1);
169 mi->blk[mi->nr_blks].nid = nid;
175 * numa_add_memblk - Add one numa_memblk to numa_meminfo
176 * @nid: NUMA node ID of the new memblk
177 * @start: Start address of the new memblk
178 * @end: End address of the new memblk
180 * Add a new memblk to the default numa_meminfo.
183 * 0 on success, -errno on failure.
185 int __init numa_add_memblk(int nid, u64 start, u64 end)
187 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
190 static void __init node_mem_init(unsigned int node)
192 unsigned long start_pfn, end_pfn;
193 unsigned long node_addrspace_offset;
195 node_addrspace_offset = nid_to_addrbase(node);
196 pr_info("Node%d's addrspace_offset is 0x%lx\n",
197 node, node_addrspace_offset);
199 get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
200 pr_info("Node%d: start_pfn=0x%lx, end_pfn=0x%lx\n",
201 node, start_pfn, end_pfn);
203 alloc_node_data(node);
206 #ifdef CONFIG_ACPI_NUMA
208 static void __init add_node_intersection(u32 node, u64 start, u64 size, u32 type)
210 static unsigned long num_physpages;
212 num_physpages += (size >> PAGE_SHIFT);
213 pr_info("Node%d: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx Bytes\n",
214 node, type, start, size);
215 pr_info(" start_pfn:0x%llx, end_pfn:0x%llx, num_physpages:0x%lx\n",
216 start >> PAGE_SHIFT, (start + size) >> PAGE_SHIFT, num_physpages);
217 memblock_set_node(start, size, &memblock.memory, node);
223 * Add a uasable memory region described by BIOS. The
224 * routine gets each intersection between BIOS's region
225 * and node's region, and adds them into node's memblock
229 static void __init add_numamem_region(u64 start, u64 end, u32 type)
235 pr_debug("Invalid region: %016llx-%016llx\n", start, end);
239 for (i = 0; i < numa_meminfo.nr_blks; i++) {
240 struct numa_memblk *mb = &numa_meminfo.blk[i];
246 add_node_intersection(mb->nid, ofs, mb->end - ofs, type);
249 add_node_intersection(mb->nid, ofs, end - ofs, type);
255 static void __init init_node_memblock(void)
258 u64 mem_end, mem_start, mem_size;
259 efi_memory_desc_t *md;
261 /* Parse memory information and activate */
262 for_each_efi_memory_desc(md) {
264 mem_start = md->phys_addr;
265 mem_size = md->num_pages << EFI_PAGE_SHIFT;
266 mem_end = mem_start + mem_size;
269 case EFI_LOADER_CODE:
270 case EFI_LOADER_DATA:
271 case EFI_BOOT_SERVICES_CODE:
272 case EFI_BOOT_SERVICES_DATA:
273 case EFI_PERSISTENT_MEMORY:
274 case EFI_CONVENTIONAL_MEMORY:
275 add_numamem_region(mem_start, mem_end, mem_type);
278 case EFI_UNUSABLE_MEMORY:
279 case EFI_ACPI_RECLAIM_MEMORY:
280 add_numamem_region(mem_start, mem_end, mem_type);
282 case EFI_RESERVED_TYPE:
283 case EFI_RUNTIME_SERVICES_CODE:
284 case EFI_RUNTIME_SERVICES_DATA:
285 case EFI_MEMORY_MAPPED_IO:
286 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
287 pr_info("Resvd: mem_type:%d, mem_start:0x%llx, mem_size:0x%llx Bytes\n",
288 mem_type, mem_start, mem_size);
294 static void __init numa_default_distance(void)
298 for (row = 0; row < MAX_NUMNODES; row++)
299 for (col = 0; col < MAX_NUMNODES; col++) {
301 node_distances[row][col] = LOCAL_DISTANCE;
303 /* We assume that one node per package here!
305 * A SLIT should be used for multiple nodes
306 * per package to override default setting.
308 node_distances[row][col] = REMOTE_DISTANCE;
313 * fake_numa_init() - For Non-ACPI systems
314 * Return: 0 on success, -errno on failure.
316 static int __init fake_numa_init(void)
318 phys_addr_t start = memblock_start_of_DRAM();
319 phys_addr_t end = memblock_end_of_DRAM() - 1;
321 node_set(0, numa_nodes_parsed);
322 pr_info("Faking a node at [mem %pap-%pap]\n", &start, &end);
324 return numa_add_memblk(0, start, end + 1);
327 int __init init_numa_memory(void)
333 for (i = 0; i < NR_CPUS; i++)
334 set_cpuid_to_node(i, NUMA_NO_NODE);
336 numa_default_distance();
337 nodes_clear(numa_nodes_parsed);
338 nodes_clear(node_possible_map);
339 nodes_clear(node_online_map);
340 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
342 /* Parse SRAT and SLIT if provided by firmware. */
343 ret = acpi_disabled ? fake_numa_init() : acpi_numa_init();
347 node_possible_map = numa_nodes_parsed;
348 if (WARN_ON(nodes_empty(node_possible_map)))
351 init_node_memblock();
352 if (!memblock_validate_numa_coverage(SZ_1M))
355 for_each_node_mask(node, node_possible_map) {
357 node_set_online(node);
359 max_low_pfn = PHYS_PFN(memblock_end_of_DRAM());
362 loongson_sysconf.nr_nodes = nr_node_ids;
363 loongson_sysconf.cores_per_node = cpumask_weight(&phys_cpus_on_node[0]);
370 void __init paging_init(void)
373 unsigned long zones_size[MAX_NR_ZONES] = {0, };
375 for_each_online_node(node) {
376 unsigned long start_pfn, end_pfn;
378 get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
380 if (end_pfn > max_low_pfn)
381 max_low_pfn = end_pfn;
383 #ifdef CONFIG_ZONE_DMA32
384 zones_size[ZONE_DMA32] = MAX_DMA32_PFN;
386 zones_size[ZONE_NORMAL] = max_low_pfn;
387 free_area_init(zones_size);
390 void __init mem_init(void)
392 high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
396 int pcibus_to_node(struct pci_bus *bus)
398 return dev_to_node(&bus->dev);
400 EXPORT_SYMBOL(pcibus_to_node);