1 // SPDX-License-Identifier: GPL-2.0-only
3 * NUMA support, based on the x86 implementation.
5 * Copyright (C) 2015 Cavium Inc.
6 * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
9 #define pr_fmt(fmt) "NUMA: " fmt
11 #include <linux/acpi.h>
12 #include <linux/memblock.h>
13 #include <linux/module.h>
16 #include <asm/sections.h>
18 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
19 EXPORT_SYMBOL(node_data);
20 nodemask_t numa_nodes_parsed __initdata;
21 static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
23 static int numa_distance_cnt;
24 static u8 *numa_distance;
27 static __init int numa_parse_early_param(char *opt)
31 if (str_has_prefix(opt, "off"))
36 early_param("numa", numa_parse_early_param);
38 cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
39 EXPORT_SYMBOL(node_to_cpumask_map);
41 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
44 * Returns a pointer to the bitmask of CPUs on Node 'node'.
46 const struct cpumask *cpumask_of_node(int node)
49 if (node == NUMA_NO_NODE)
52 if (WARN_ON(node < 0 || node >= nr_node_ids))
55 if (WARN_ON(node_to_cpumask_map[node] == NULL))
56 return cpu_online_mask;
58 return node_to_cpumask_map[node];
60 EXPORT_SYMBOL(cpumask_of_node);
64 static void numa_update_cpu(unsigned int cpu, bool remove)
66 int nid = cpu_to_node(cpu);
68 if (nid == NUMA_NO_NODE)
72 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
74 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
77 void numa_add_cpu(unsigned int cpu)
79 numa_update_cpu(cpu, false);
82 void numa_remove_cpu(unsigned int cpu)
84 numa_update_cpu(cpu, true);
87 void numa_clear_node(unsigned int cpu)
90 set_cpu_numa_node(cpu, NUMA_NO_NODE);
94 * Allocate node_to_cpumask_map based on number of available nodes
95 * Requires node_possible_map to be valid.
97 * Note: cpumask_of_node() is not valid until after this is done.
98 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
100 static void __init setup_node_to_cpumask_map(void)
104 /* setup nr_node_ids if not done yet */
105 if (nr_node_ids == MAX_NUMNODES)
108 /* allocate and clear the mapping */
109 for (node = 0; node < nr_node_ids; node++) {
110 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
111 cpumask_clear(node_to_cpumask_map[node]);
114 /* cpumask_of_node() will now work */
115 pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
119 * Set the cpu to node and mem mapping
121 void numa_store_cpu_info(unsigned int cpu)
123 set_cpu_numa_node(cpu, cpu_to_node_map[cpu]);
126 void __init early_map_cpu_to_node(unsigned int cpu, int nid)
128 /* fallback to node 0 */
129 if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
132 cpu_to_node_map[cpu] = nid;
135 * We should set the numa node of cpu0 as soon as possible, because it
136 * has already been set up online before. cpu_to_node(0) will soon be
140 set_cpu_numa_node(cpu, nid);
143 #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
144 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
145 EXPORT_SYMBOL(__per_cpu_offset);
147 static int __init early_cpu_to_node(int cpu)
149 return cpu_to_node_map[cpu];
152 static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
154 return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
157 static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
160 int nid = early_cpu_to_node(cpu);
162 return memblock_alloc_try_nid(size, align,
163 __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
166 static void __init pcpu_fc_free(void *ptr, size_t size)
168 memblock_free_early(__pa(ptr), size);
171 void __init setup_per_cpu_areas(void)
178 * Always reserve area for module percpu variables. That's
179 * what the legacy allocator did.
181 rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
182 PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
184 pcpu_fc_alloc, pcpu_fc_free);
186 panic("Failed to initialize percpu areas.");
188 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
189 for_each_possible_cpu(cpu)
190 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
195 * numa_add_memblk() - Set node id to memblk
196 * @nid: NUMA node ID of the new memblk
197 * @start: Start address of the new memblk
198 * @end: End address of the new memblk
201 * 0 on success, -errno on failure.
203 int __init numa_add_memblk(int nid, u64 start, u64 end)
207 ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
209 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
210 start, (end - 1), nid);
214 node_set(nid, numa_nodes_parsed);
219 * Initialize NODE_DATA for a node on the local memory
221 static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
223 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
228 if (start_pfn >= end_pfn)
229 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
231 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
233 panic("Cannot allocate %zu bytes for node %d data\n",
238 /* report and initialize */
239 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
240 nd_pa, nd_pa + nd_size - 1);
241 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
243 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
246 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
247 NODE_DATA(nid)->node_id = nid;
248 NODE_DATA(nid)->node_start_pfn = start_pfn;
249 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
255 * The current table is freed.
257 void __init numa_free_distance(void)
264 size = numa_distance_cnt * numa_distance_cnt *
265 sizeof(numa_distance[0]);
267 memblock_free_ptr(numa_distance, size);
268 numa_distance_cnt = 0;
269 numa_distance = NULL;
273 * Create a new NUMA distance table.
275 static int __init numa_alloc_distance(void)
281 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
282 phys = memblock_phys_alloc_range(size, PAGE_SIZE, 0, PFN_PHYS(max_pfn));
286 numa_distance = __va(phys);
287 numa_distance_cnt = nr_node_ids;
289 /* fill with the default distances */
290 for (i = 0; i < numa_distance_cnt; i++)
291 for (j = 0; j < numa_distance_cnt; j++)
292 numa_distance[i * numa_distance_cnt + j] = i == j ?
293 LOCAL_DISTANCE : REMOTE_DISTANCE;
295 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
301 * numa_set_distance() - Set inter node NUMA distance from node to node.
302 * @from: the 'from' node to set distance
303 * @to: the 'to' node to set distance
304 * @distance: NUMA distance
306 * Set the distance from node @from to @to to @distance.
307 * If distance table doesn't exist, a warning is printed.
309 * If @from or @to is higher than the highest known node or lower than zero
310 * or @distance doesn't make sense, the call is ignored.
312 void __init numa_set_distance(int from, int to, int distance)
314 if (!numa_distance) {
315 pr_warn_once("Warning: distance table not allocated yet\n");
319 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
320 from < 0 || to < 0) {
321 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
326 if ((u8)distance != distance ||
327 (from == to && distance != LOCAL_DISTANCE)) {
328 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
333 numa_distance[from * numa_distance_cnt + to] = distance;
337 * Return NUMA distance @from to @to
339 int __node_distance(int from, int to)
341 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
342 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
343 return numa_distance[from * numa_distance_cnt + to];
345 EXPORT_SYMBOL(__node_distance);
347 static int __init numa_register_nodes(void)
350 struct memblock_region *mblk;
352 /* Check that valid nid is set to memblks */
353 for_each_mem_region(mblk) {
354 int mblk_nid = memblock_get_region_node(mblk);
355 phys_addr_t start = mblk->base;
356 phys_addr_t end = mblk->base + mblk->size - 1;
358 if (mblk_nid == NUMA_NO_NODE || mblk_nid >= MAX_NUMNODES) {
359 pr_warn("Warning: invalid memblk node %d [mem %pap-%pap]\n",
360 mblk_nid, &start, &end);
365 /* Finally register nodes. */
366 for_each_node_mask(nid, numa_nodes_parsed) {
367 unsigned long start_pfn, end_pfn;
369 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
370 setup_node_data(nid, start_pfn, end_pfn);
371 node_set_online(nid);
374 /* Setup online nodes to actual nodes*/
375 node_possible_map = numa_nodes_parsed;
380 static int __init numa_init(int (*init_func)(void))
384 nodes_clear(numa_nodes_parsed);
385 nodes_clear(node_possible_map);
386 nodes_clear(node_online_map);
388 ret = numa_alloc_distance();
394 goto out_free_distance;
396 if (nodes_empty(numa_nodes_parsed)) {
397 pr_info("No NUMA configuration found\n");
399 goto out_free_distance;
402 ret = numa_register_nodes();
404 goto out_free_distance;
406 setup_node_to_cpumask_map();
410 numa_free_distance();
415 * dummy_numa_init() - Fallback dummy NUMA init
417 * Used if there's no underlying NUMA architecture, NUMA initialization
418 * fails, or NUMA is disabled on the command line.
420 * Must online at least one node (node 0) and add memory blocks that cover all
421 * allowed memory. It is unlikely that this function fails.
423 * Return: 0 on success, -errno on failure.
425 static int __init dummy_numa_init(void)
427 phys_addr_t start = memblock_start_of_DRAM();
428 phys_addr_t end = memblock_end_of_DRAM() - 1;
432 pr_info("NUMA disabled\n"); /* Forced off on command line. */
433 pr_info("Faking a node at [mem %pap-%pap]\n", &start, &end);
435 ret = numa_add_memblk(0, start, end + 1);
437 pr_err("NUMA init failed\n");
445 #ifdef CONFIG_ACPI_NUMA
446 static int __init arch_acpi_numa_init(void)
450 ret = acpi_numa_init();
452 pr_info("Failed to initialise from firmware\n");
456 return srat_disabled() ? -EINVAL : 0;
459 static int __init arch_acpi_numa_init(void)
466 * arch_numa_init() - Initialize NUMA
468 * Try each configured NUMA initialization method until one succeeds. The
469 * last fallback is dummy single node config encompassing whole memory.
471 void __init arch_numa_init(void)
474 if (!acpi_disabled && !numa_init(arch_acpi_numa_init))
476 if (acpi_disabled && !numa_init(of_numa_init))
480 numa_init(dummy_numa_init);