Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / base / arch_numa.c
blobe187016764265873e8fadb4703f67a4ac457a2ae
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * NUMA support, based on the x86 implementation.
5 * Copyright (C) 2015 Cavium Inc.
6 * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
7 */
9 #define pr_fmt(fmt) "NUMA: " fmt
11 #include <linux/acpi.h>
12 #include <linux/memblock.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/numa_memblks.h>
17 #include <asm/sections.h>
19 static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
21 bool numa_off;
23 static __init int numa_parse_early_param(char *opt)
25 if (!opt)
26 return -EINVAL;
27 if (str_has_prefix(opt, "off"))
28 numa_off = true;
29 if (!strncmp(opt, "fake=", 5))
30 return numa_emu_cmdline(opt + 5);
32 return 0;
34 early_param("numa", numa_parse_early_param);
36 cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
37 EXPORT_SYMBOL(node_to_cpumask_map);
39 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
42 * Returns a pointer to the bitmask of CPUs on Node 'node'.
44 const struct cpumask *cpumask_of_node(int node)
47 if (node == NUMA_NO_NODE)
48 return cpu_all_mask;
50 if (WARN_ON(node < 0 || node >= nr_node_ids))
51 return cpu_none_mask;
53 if (WARN_ON(node_to_cpumask_map[node] == NULL))
54 return cpu_online_mask;
56 return node_to_cpumask_map[node];
58 EXPORT_SYMBOL(cpumask_of_node);
60 #endif
62 #ifndef CONFIG_NUMA_EMU
63 static void numa_update_cpu(unsigned int cpu, bool remove)
65 int nid = cpu_to_node(cpu);
67 if (nid == NUMA_NO_NODE)
68 return;
70 if (remove)
71 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
72 else
73 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
76 void numa_add_cpu(unsigned int cpu)
78 numa_update_cpu(cpu, false);
81 void numa_remove_cpu(unsigned int cpu)
83 numa_update_cpu(cpu, true);
85 #endif
87 void numa_clear_node(unsigned int cpu)
89 numa_remove_cpu(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)
102 int node;
104 /* setup nr_node_ids if not done yet */
105 if (nr_node_ids == MAX_NUMNODES)
106 setup_nr_node_ids();
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)
130 nid = 0;
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
137 * called.
139 if (!cpu)
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 int 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 void __init setup_per_cpu_areas(void)
159 unsigned long delta;
160 unsigned int cpu;
161 int rc = -EINVAL;
163 if (pcpu_chosen_fc != PCPU_FC_PAGE) {
165 * Always reserve area for module percpu variables. That's
166 * what the legacy allocator did.
168 rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
169 PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
170 pcpu_cpu_distance,
171 early_cpu_to_node);
172 #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
173 if (rc < 0)
174 pr_warn("PERCPU: %s allocator failed (%d), falling back to page size\n",
175 pcpu_fc_names[pcpu_chosen_fc], rc);
176 #endif
179 #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
180 if (rc < 0)
181 rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE, early_cpu_to_node);
182 #endif
183 if (rc < 0)
184 panic("Failed to initialize percpu areas (err=%d).", rc);
186 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
187 for_each_possible_cpu(cpu)
188 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
190 #endif
193 * Initialize NODE_DATA for a node on the local memory
195 static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
197 if (start_pfn >= end_pfn)
198 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
200 alloc_node_data(nid);
202 NODE_DATA(nid)->node_id = nid;
203 NODE_DATA(nid)->node_start_pfn = start_pfn;
204 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
207 static int __init numa_register_nodes(void)
209 int nid;
211 /* Finally register nodes. */
212 for_each_node_mask(nid, numa_nodes_parsed) {
213 unsigned long start_pfn, end_pfn;
215 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
216 setup_node_data(nid, start_pfn, end_pfn);
217 node_set_online(nid);
220 /* Setup online nodes to actual nodes*/
221 node_possible_map = numa_nodes_parsed;
223 return 0;
226 static int __init numa_init(int (*init_func)(void))
228 int ret;
230 nodes_clear(numa_nodes_parsed);
231 nodes_clear(node_possible_map);
232 nodes_clear(node_online_map);
234 ret = numa_memblks_init(init_func, /* memblock_force_top_down */ false);
235 if (ret < 0)
236 goto out_free_distance;
238 if (nodes_empty(numa_nodes_parsed)) {
239 pr_info("No NUMA configuration found\n");
240 ret = -EINVAL;
241 goto out_free_distance;
244 ret = numa_register_nodes();
245 if (ret < 0)
246 goto out_free_distance;
248 setup_node_to_cpumask_map();
250 return 0;
251 out_free_distance:
252 numa_reset_distance();
253 return ret;
257 * dummy_numa_init() - Fallback dummy NUMA init
259 * Used if there's no underlying NUMA architecture, NUMA initialization
260 * fails, or NUMA is disabled on the command line.
262 * Must online at least one node (node 0) and add memory blocks that cover all
263 * allowed memory. It is unlikely that this function fails.
265 * Return: 0 on success, -errno on failure.
267 static int __init dummy_numa_init(void)
269 phys_addr_t start = memblock_start_of_DRAM();
270 phys_addr_t end = memblock_end_of_DRAM() - 1;
271 int ret;
273 if (numa_off)
274 pr_info("NUMA disabled\n"); /* Forced off on command line. */
275 pr_info("Faking a node at [mem %pap-%pap]\n", &start, &end);
277 ret = numa_add_memblk(0, start, end + 1);
278 if (ret) {
279 pr_err("NUMA init failed\n");
280 return ret;
282 node_set(0, numa_nodes_parsed);
284 numa_off = true;
285 return 0;
288 #ifdef CONFIG_ACPI_NUMA
289 static int __init arch_acpi_numa_init(void)
291 int ret;
293 ret = acpi_numa_init();
294 if (ret) {
295 pr_debug("Failed to initialise from firmware\n");
296 return ret;
299 return srat_disabled() ? -EINVAL : 0;
301 #else
302 static int __init arch_acpi_numa_init(void)
304 return -EOPNOTSUPP;
306 #endif
309 * arch_numa_init() - Initialize NUMA
311 * Try each configured NUMA initialization method until one succeeds. The
312 * last fallback is dummy single node config encompassing whole memory.
314 void __init arch_numa_init(void)
316 if (!numa_off) {
317 if (!acpi_disabled && !numa_init(arch_acpi_numa_init))
318 return;
319 if (acpi_disabled && !numa_init(of_numa_init))
320 return;
323 numa_init(dummy_numa_init);
326 #ifdef CONFIG_NUMA_EMU
327 void __init numa_emu_update_cpu_to_node(int *emu_nid_to_phys,
328 unsigned int nr_emu_nids)
330 int i, j;
333 * Transform cpu_to_node_map table to use emulated nids by
334 * reverse-mapping phys_nid. The maps should always exist but fall
335 * back to zero just in case.
337 for (i = 0; i < ARRAY_SIZE(cpu_to_node_map); i++) {
338 if (cpu_to_node_map[i] == NUMA_NO_NODE)
339 continue;
340 for (j = 0; j < nr_emu_nids; j++)
341 if (cpu_to_node_map[i] == emu_nid_to_phys[j])
342 break;
343 cpu_to_node_map[i] = j < nr_emu_nids ? j : 0;
347 u64 __init numa_emu_dma_end(void)
349 return memblock_start_of_DRAM() + SZ_4G;
352 void debug_cpumask_set_cpu(unsigned int cpu, int node, bool enable)
354 struct cpumask *mask;
356 if (node == NUMA_NO_NODE)
357 return;
359 mask = node_to_cpumask_map[node];
360 if (!cpumask_available(mask)) {
361 pr_err("node_to_cpumask_map[%i] NULL\n", node);
362 dump_stack();
363 return;
366 if (enable)
367 cpumask_set_cpu(cpu, mask);
368 else
369 cpumask_clear_cpu(cpu, mask);
371 pr_debug("%s cpu %d node %d: mask now %*pbl\n",
372 enable ? "numa_add_cpu" : "numa_remove_cpu",
373 cpu, node, cpumask_pr_args(mask));
375 #endif /* CONFIG_NUMA_EMU */