blob: 7a0a555b366af0aa8c56a801cf9f5ecdbd070112 [file] [log] [blame]
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -07001/*
2 * NUMA support, based on the x86 implementation.
3 *
4 * Copyright (C) 2015 Cavium Inc.
5 * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
6 *
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.
10 *
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.
15 *
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/>.
18 */
19
Kefeng Wangf11c7ba2016-09-01 14:54:59 +080020#define pr_fmt(fmt) "NUMA: " fmt
21
Hanjun Guod8b47fc2016-05-24 15:35:44 -070022#include <linux/acpi.h>
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070023#include <linux/memblock.h>
24#include <linux/module.h>
25#include <linux/of.h>
26
Catalin Marinasbfe6c8a2016-08-15 16:33:10 +010027#include <asm/acpi.h>
Zhen Lei7af3a0a2016-09-01 14:55:00 +080028#include <asm/sections.h>
Catalin Marinasbfe6c8a2016-08-15 16:33:10 +010029
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070030struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
31EXPORT_SYMBOL(node_data);
32nodemask_t numa_nodes_parsed __initdata;
33static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
34
35static int numa_distance_cnt;
36static u8 *numa_distance;
Boris Ostrovskyaec03f82016-12-12 23:18:29 -050037bool numa_off;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070038
39static __init int numa_parse_early_param(char *opt)
40{
41 if (!opt)
42 return -EINVAL;
Kefeng Wangf11c7ba2016-09-01 14:54:59 +080043 if (!strncmp(opt, "off", 3))
David Daney34c33372016-05-24 15:35:37 -070044 numa_off = true;
Kefeng Wangf11c7ba2016-09-01 14:54:59 +080045
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070046 return 0;
47}
48early_param("numa", numa_parse_early_param);
49
50cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
51EXPORT_SYMBOL(node_to_cpumask_map);
52
53#ifdef CONFIG_DEBUG_PER_CPU_MAPS
54
55/*
56 * Returns a pointer to the bitmask of CPUs on Node 'node'.
57 */
58const struct cpumask *cpumask_of_node(int node)
59{
60 if (WARN_ON(node >= nr_node_ids))
61 return cpu_none_mask;
62
63 if (WARN_ON(node_to_cpumask_map[node] == NULL))
64 return cpu_online_mask;
65
66 return node_to_cpumask_map[node];
67}
68EXPORT_SYMBOL(cpumask_of_node);
69
70#endif
71
Sudeep Holla97fd6012018-07-06 12:02:43 +010072static void numa_update_cpu(unsigned int cpu, bool remove)
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070073{
Sudeep Holla97fd6012018-07-06 12:02:43 +010074 int nid = cpu_to_node(cpu);
75
76 if (nid == NUMA_NO_NODE)
77 return;
78
79 if (remove)
80 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
81 else
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070082 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
83}
84
Sudeep Holla97fd6012018-07-06 12:02:43 +010085void numa_add_cpu(unsigned int cpu)
86{
87 numa_update_cpu(cpu, false);
88}
89
90void numa_remove_cpu(unsigned int cpu)
91{
92 numa_update_cpu(cpu, true);
93}
94
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070095void numa_clear_node(unsigned int cpu)
96{
Sudeep Holla97fd6012018-07-06 12:02:43 +010097 numa_remove_cpu(cpu);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070098 set_cpu_numa_node(cpu, NUMA_NO_NODE);
99}
100
101/*
102 * Allocate node_to_cpumask_map based on number of available nodes
103 * Requires node_possible_map to be valid.
104 *
105 * Note: cpumask_of_node() is not valid until after this is done.
106 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
107 */
108static void __init setup_node_to_cpumask_map(void)
109{
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700110 int node;
111
112 /* setup nr_node_ids if not done yet */
113 if (nr_node_ids == MAX_NUMNODES)
114 setup_nr_node_ids();
115
116 /* allocate and clear the mapping */
117 for (node = 0; node < nr_node_ids; node++) {
118 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
119 cpumask_clear(node_to_cpumask_map[node]);
120 }
121
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700122 /* cpumask_of_node() will now work */
Alexey Dobriyanb9726c22019-03-05 15:48:26 -0800123 pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700124}
125
126/*
127 * Set the cpu to node and mem mapping
128 */
129void numa_store_cpu_info(unsigned int cpu)
130{
Sudeep Holla97fd6012018-07-06 12:02:43 +0100131 set_cpu_numa_node(cpu, cpu_to_node_map[cpu]);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700132}
133
134void __init early_map_cpu_to_node(unsigned int cpu, int nid)
135{
136 /* fallback to node 0 */
Zhen Lei7ba5f602016-09-01 14:55:04 +0800137 if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700138 nid = 0;
139
140 cpu_to_node_map[cpu] = nid;
Zhen Lei7ba5f602016-09-01 14:55:04 +0800141
142 /*
143 * We should set the numa node of cpu0 as soon as possible, because it
144 * has already been set up online before. cpu_to_node(0) will soon be
145 * called.
146 */
147 if (!cpu)
148 set_cpu_numa_node(cpu, nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700149}
150
Zhen Lei7af3a0a2016-09-01 14:55:00 +0800151#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
152unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
153EXPORT_SYMBOL(__per_cpu_offset);
154
155static int __init early_cpu_to_node(int cpu)
156{
157 return cpu_to_node_map[cpu];
158}
159
160static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
161{
Yisheng Xie26984c32016-10-21 16:13:55 +0800162 return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
Zhen Lei7af3a0a2016-09-01 14:55:00 +0800163}
164
165static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
166 size_t align)
167{
168 int nid = early_cpu_to_node(cpu);
169
Mike Rapoporteb31d552018-10-30 15:08:04 -0700170 return memblock_alloc_try_nid(size, align,
Zhen Lei7af3a0a2016-09-01 14:55:00 +0800171 __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
172}
173
174static void __init pcpu_fc_free(void *ptr, size_t size)
175{
176 memblock_free_early(__pa(ptr), size);
177}
178
179void __init setup_per_cpu_areas(void)
180{
181 unsigned long delta;
182 unsigned int cpu;
183 int rc;
184
185 /*
186 * Always reserve area for module percpu variables. That's
187 * what the legacy allocator did.
188 */
189 rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
190 PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
191 pcpu_cpu_distance,
192 pcpu_fc_alloc, pcpu_fc_free);
193 if (rc < 0)
194 panic("Failed to initialize percpu areas.");
195
196 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
197 for_each_possible_cpu(cpu)
198 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
199}
200#endif
201
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700202/**
203 * numa_add_memblk - Set node id to memblk
204 * @nid: NUMA node ID of the new memblk
205 * @start: Start address of the new memblk
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700206 * @end: End address of the new memblk
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700207 *
208 * RETURNS:
209 * 0 on success, -errno on failure.
210 */
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700211int __init numa_add_memblk(int nid, u64 start, u64 end)
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700212{
213 int ret;
214
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700215 ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700216 if (ret < 0) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800217 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700218 start, (end - 1), nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700219 return ret;
220 }
221
222 node_set(nid, numa_nodes_parsed);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700223 return ret;
224}
225
226/**
227 * Initialize NODE_DATA for a node on the local memory
228 */
229static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
230{
231 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
232 u64 nd_pa;
233 void *nd;
234 int tnid;
235
Punit Agrawalece4b202017-07-20 12:03:59 +0100236 if (start_pfn >= end_pfn)
Hanjun Guo3f7a09f2016-10-21 16:13:56 +0800237 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700238
Mike Rapoport9a8dd702018-10-30 15:07:59 -0700239 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700240 nd = __va(nd_pa);
241
242 /* report and initialize */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800243 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700244 nd_pa, nd_pa + nd_size - 1);
245 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
246 if (tnid != nid)
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800247 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700248
249 node_data[nid] = nd;
250 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
251 NODE_DATA(nid)->node_id = nid;
252 NODE_DATA(nid)->node_start_pfn = start_pfn;
253 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
254}
255
256/**
257 * numa_free_distance
258 *
259 * The current table is freed.
260 */
261void __init numa_free_distance(void)
262{
263 size_t size;
264
265 if (!numa_distance)
266 return;
267
268 size = numa_distance_cnt * numa_distance_cnt *
269 sizeof(numa_distance[0]);
270
271 memblock_free(__pa(numa_distance), size);
272 numa_distance_cnt = 0;
273 numa_distance = NULL;
274}
275
276/**
277 *
278 * Create a new NUMA distance table.
279 *
280 */
281static int __init numa_alloc_distance(void)
282{
283 size_t size;
284 u64 phys;
285 int i, j;
286
287 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
288 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
289 size, PAGE_SIZE);
290 if (WARN_ON(!phys))
291 return -ENOMEM;
292
293 memblock_reserve(phys, size);
294
295 numa_distance = __va(phys);
296 numa_distance_cnt = nr_node_ids;
297
298 /* fill with the default distances */
299 for (i = 0; i < numa_distance_cnt; i++)
300 for (j = 0; j < numa_distance_cnt; j++)
301 numa_distance[i * numa_distance_cnt + j] = i == j ?
302 LOCAL_DISTANCE : REMOTE_DISTANCE;
303
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800304 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700305
306 return 0;
307}
308
309/**
310 * numa_set_distance - Set inter node NUMA distance from node to node.
311 * @from: the 'from' node to set distance
312 * @to: the 'to' node to set distance
313 * @distance: NUMA distance
314 *
315 * Set the distance from node @from to @to to @distance.
316 * If distance table doesn't exist, a warning is printed.
317 *
318 * If @from or @to is higher than the highest known node or lower than zero
319 * or @distance doesn't make sense, the call is ignored.
320 *
321 */
322void __init numa_set_distance(int from, int to, int distance)
323{
324 if (!numa_distance) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800325 pr_warn_once("Warning: distance table not allocated yet\n");
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700326 return;
327 }
328
329 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
330 from < 0 || to < 0) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800331 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700332 from, to, distance);
333 return;
334 }
335
336 if ((u8)distance != distance ||
337 (from == to && distance != LOCAL_DISTANCE)) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800338 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700339 from, to, distance);
340 return;
341 }
342
343 numa_distance[from * numa_distance_cnt + to] = distance;
344}
345
346/**
347 * Return NUMA distance @from to @to
348 */
349int __node_distance(int from, int to)
350{
351 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
352 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
353 return numa_distance[from * numa_distance_cnt + to];
354}
355EXPORT_SYMBOL(__node_distance);
356
357static int __init numa_register_nodes(void)
358{
359 int nid;
360 struct memblock_region *mblk;
361
362 /* Check that valid nid is set to memblks */
363 for_each_memblock(memory, mblk)
364 if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800365 pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700366 mblk->nid, mblk->base,
367 mblk->base + mblk->size - 1);
368 return -EINVAL;
369 }
370
371 /* Finally register nodes. */
372 for_each_node_mask(nid, numa_nodes_parsed) {
373 unsigned long start_pfn, end_pfn;
374
375 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
376 setup_node_data(nid, start_pfn, end_pfn);
377 node_set_online(nid);
378 }
379
380 /* Setup online nodes to actual nodes*/
381 node_possible_map = numa_nodes_parsed;
382
383 return 0;
384}
385
386static int __init numa_init(int (*init_func)(void))
387{
388 int ret;
389
390 nodes_clear(numa_nodes_parsed);
391 nodes_clear(node_possible_map);
392 nodes_clear(node_online_map);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700393
394 ret = numa_alloc_distance();
395 if (ret < 0)
396 return ret;
397
398 ret = init_func();
399 if (ret < 0)
Anshuman Khandual52338082018-09-22 21:09:56 +0530400 goto out_free_distance;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700401
Zhen Lei794224ea2016-09-01 14:54:56 +0800402 if (nodes_empty(numa_nodes_parsed)) {
403 pr_info("No NUMA configuration found\n");
Anshuman Khandual52338082018-09-22 21:09:56 +0530404 ret = -EINVAL;
405 goto out_free_distance;
Zhen Lei794224ea2016-09-01 14:54:56 +0800406 }
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700407
408 ret = numa_register_nodes();
409 if (ret < 0)
Anshuman Khandual52338082018-09-22 21:09:56 +0530410 goto out_free_distance;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700411
412 setup_node_to_cpumask_map();
413
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700414 return 0;
Anshuman Khandual52338082018-09-22 21:09:56 +0530415out_free_distance:
416 numa_free_distance();
417 return ret;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700418}
419
420/**
421 * dummy_numa_init - Fallback dummy NUMA init
422 *
423 * Used if there's no underlying NUMA architecture, NUMA initialization
424 * fails, or NUMA is disabled on the command line.
425 *
426 * Must online at least one node (node 0) and add memory blocks that cover all
427 * allowed memory. It is unlikely that this function fails.
428 */
429static int __init dummy_numa_init(void)
430{
431 int ret;
432 struct memblock_region *mblk;
433
David Daney34c33372016-05-24 15:35:37 -0700434 if (numa_off)
435 pr_info("NUMA disabled\n"); /* Forced off on command line. */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800436 pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
Anshuman Khandual77cfe952018-09-22 21:09:55 +0530437 memblock_start_of_DRAM(), memblock_end_of_DRAM() - 1);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700438
439 for_each_memblock(memory, mblk) {
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700440 ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700441 if (!ret)
442 continue;
443
444 pr_err("NUMA init failed\n");
445 return ret;
446 }
447
David Daney34c33372016-05-24 15:35:37 -0700448 numa_off = true;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700449 return 0;
450}
451
452/**
453 * arm64_numa_init - Initialize NUMA
454 *
455 * Try each configured NUMA initialization method until one succeeds. The
456 * last fallback is dummy single node config encomapssing whole memory.
457 */
458void __init arm64_numa_init(void)
459{
460 if (!numa_off) {
Hanjun Guod8b47fc2016-05-24 15:35:44 -0700461 if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
462 return;
463 if (acpi_disabled && !numa_init(of_numa_init))
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700464 return;
465 }
466
467 numa_init(dummy_numa_init);
468}
Robin Murphy4ab21502018-12-11 18:48:48 +0000469
470/*
471 * We hope that we will be hotplugging memory on nodes we already know about,
472 * such that acpi_get_node() succeeds and we never fall back to this...
473 */
474int memory_add_physaddr_to_nid(u64 addr)
475{
476 pr_warn("Unknown node for memory at 0x%llx, assuming node 0\n", addr);
477 return 0;
478}