2 * NUMA support, based on the x86 implementation.
4 * Copyright (C) 2015 Cavium Inc.
5 * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #define pr_fmt(fmt) "NUMA: " fmt
22 #include <linux/acpi.h>
23 #include <linux/bootmem.h>
24 #include <linux/memblock.h>
25 #include <linux/module.h>
29 #include <asm/sections.h>
31 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
32 EXPORT_SYMBOL(node_data);
33 nodemask_t numa_nodes_parsed __initdata;
34 static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
36 static int numa_distance_cnt;
37 static u8 *numa_distance;
40 static __init int numa_parse_early_param(char *opt)
44 if (!strncmp(opt, "off", 3))
49 early_param("numa", numa_parse_early_param);
51 cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
52 EXPORT_SYMBOL(node_to_cpumask_map);
54 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
57 * Returns a pointer to the bitmask of CPUs on Node 'node'.
59 const struct cpumask *cpumask_of_node(int node)
62 if (node == NUMA_NO_NODE)
65 if (WARN_ON(node < 0 || node >= nr_node_ids))
68 if (WARN_ON(node_to_cpumask_map[node] == NULL))
69 return cpu_online_mask;
71 return node_to_cpumask_map[node];
73 EXPORT_SYMBOL(cpumask_of_node);
77 static void map_cpu_to_node(unsigned int cpu, int nid)
79 set_cpu_numa_node(cpu, nid);
81 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
84 void numa_clear_node(unsigned int cpu)
86 int nid = cpu_to_node(cpu);
89 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
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 %d nodes\n", nr_node_ids);
119 * Set the cpu to node and mem mapping
121 void numa_store_cpu_info(unsigned int cpu)
123 map_cpu_to_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_virt_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_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
234 /* report and initialize */
235 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
236 nd_pa, nd_pa + nd_size - 1);
237 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
239 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
242 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
243 NODE_DATA(nid)->node_id = nid;
244 NODE_DATA(nid)->node_start_pfn = start_pfn;
245 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
251 * The current table is freed.
253 void __init numa_free_distance(void)
260 size = numa_distance_cnt * numa_distance_cnt *
261 sizeof(numa_distance[0]);
263 memblock_free(__pa(numa_distance), size);
264 numa_distance_cnt = 0;
265 numa_distance = NULL;
270 * Create a new NUMA distance table.
273 static int __init numa_alloc_distance(void)
279 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
280 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
285 memblock_reserve(phys, size);
287 numa_distance = __va(phys);
288 numa_distance_cnt = nr_node_ids;
290 /* fill with the default distances */
291 for (i = 0; i < numa_distance_cnt; i++)
292 for (j = 0; j < numa_distance_cnt; j++)
293 numa_distance[i * numa_distance_cnt + j] = i == j ?
294 LOCAL_DISTANCE : REMOTE_DISTANCE;
296 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
302 * numa_set_distance - Set inter node NUMA distance from node to node.
303 * @from: the 'from' node to set distance
304 * @to: the 'to' node to set distance
305 * @distance: NUMA distance
307 * Set the distance from node @from to @to to @distance.
308 * If distance table doesn't exist, a warning is printed.
310 * If @from or @to is higher than the highest known node or lower than zero
311 * or @distance doesn't make sense, the call is ignored.
314 void __init numa_set_distance(int from, int to, int distance)
316 if (!numa_distance) {
317 pr_warn_once("Warning: distance table not allocated yet\n");
321 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
322 from < 0 || to < 0) {
323 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
328 if ((u8)distance != distance ||
329 (from == to && distance != LOCAL_DISTANCE)) {
330 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
335 numa_distance[from * numa_distance_cnt + to] = distance;
339 * Return NUMA distance @from to @to
341 int __node_distance(int from, int to)
343 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
344 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
345 return numa_distance[from * numa_distance_cnt + to];
347 EXPORT_SYMBOL(__node_distance);
349 static int __init numa_register_nodes(void)
352 struct memblock_region *mblk;
354 /* Check that valid nid is set to memblks */
355 for_each_memblock(memory, mblk)
356 if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
357 pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
358 mblk->nid, mblk->base,
359 mblk->base + mblk->size - 1);
363 /* Finally register nodes. */
364 for_each_node_mask(nid, numa_nodes_parsed) {
365 unsigned long start_pfn, end_pfn;
367 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
368 setup_node_data(nid, start_pfn, end_pfn);
369 node_set_online(nid);
372 /* Setup online nodes to actual nodes*/
373 node_possible_map = numa_nodes_parsed;
378 static int __init numa_init(int (*init_func)(void))
382 nodes_clear(numa_nodes_parsed);
383 nodes_clear(node_possible_map);
384 nodes_clear(node_online_map);
385 numa_free_distance();
387 ret = numa_alloc_distance();
395 if (nodes_empty(numa_nodes_parsed)) {
396 pr_info("No NUMA configuration found\n");
400 ret = numa_register_nodes();
404 setup_node_to_cpumask_map();
410 * dummy_numa_init - Fallback dummy NUMA init
412 * Used if there's no underlying NUMA architecture, NUMA initialization
413 * fails, or NUMA is disabled on the command line.
415 * Must online at least one node (node 0) and add memory blocks that cover all
416 * allowed memory. It is unlikely that this function fails.
418 static int __init dummy_numa_init(void)
421 struct memblock_region *mblk;
424 pr_info("NUMA disabled\n"); /* Forced off on command line. */
425 pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
426 memblock_start_of_DRAM(), memblock_end_of_DRAM() - 1);
428 for_each_memblock(memory, mblk) {
429 ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
433 pr_err("NUMA init failed\n");
442 * arm64_numa_init - Initialize NUMA
444 * Try each configured NUMA initialization method until one succeeds. The
445 * last fallback is dummy single node config encomapssing whole memory.
447 void __init arm64_numa_init(void)
450 if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
452 if (acpi_disabled && !numa_init(of_numa_init))
456 numa_init(dummy_numa_init);