blob: aafcee3e3f7ef7bada8dafb67c5e42b0d0a3569f [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -07002/*
3 * NUMA support, based on the x86 implementation.
4 *
5 * Copyright (C) 2015 Cavium Inc.
6 * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -07007 */
8
Kefeng Wangf11c7ba2016-09-01 14:54:59 +08009#define pr_fmt(fmt) "NUMA: " fmt
10
Hanjun Guod8b47fc2016-05-24 15:35:44 -070011#include <linux/acpi.h>
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070012#include <linux/memblock.h>
13#include <linux/module.h>
14#include <linux/of.h>
15
Catalin Marinasbfe6c8a2016-08-15 16:33:10 +010016#include <asm/acpi.h>
Zhen Lei7af3a0a2016-09-01 14:55:00 +080017#include <asm/sections.h>
Catalin Marinasbfe6c8a2016-08-15 16:33:10 +010018
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070019struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
20EXPORT_SYMBOL(node_data);
21nodemask_t numa_nodes_parsed __initdata;
22static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
23
24static int numa_distance_cnt;
25static u8 *numa_distance;
Boris Ostrovskyaec03f82016-12-12 23:18:29 -050026bool numa_off;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070027
28static __init int numa_parse_early_param(char *opt)
29{
30 if (!opt)
31 return -EINVAL;
Chuhong Yuanb3e089c2019-07-30 10:44:15 +080032 if (str_has_prefix(opt, "off"))
David Daney34c33372016-05-24 15:35:37 -070033 numa_off = true;
Kefeng Wangf11c7ba2016-09-01 14:54:59 +080034
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070035 return 0;
36}
37early_param("numa", numa_parse_early_param);
38
39cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
40EXPORT_SYMBOL(node_to_cpumask_map);
41
42#ifdef CONFIG_DEBUG_PER_CPU_MAPS
43
44/*
45 * Returns a pointer to the bitmask of CPUs on Node 'node'.
46 */
47const struct cpumask *cpumask_of_node(int node)
48{
49 if (WARN_ON(node >= nr_node_ids))
50 return cpu_none_mask;
51
52 if (WARN_ON(node_to_cpumask_map[node] == NULL))
53 return cpu_online_mask;
54
55 return node_to_cpumask_map[node];
56}
57EXPORT_SYMBOL(cpumask_of_node);
58
59#endif
60
Sudeep Holla97fd6012018-07-06 12:02:43 +010061static void numa_update_cpu(unsigned int cpu, bool remove)
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070062{
Sudeep Holla97fd6012018-07-06 12:02:43 +010063 int nid = cpu_to_node(cpu);
64
65 if (nid == NUMA_NO_NODE)
66 return;
67
68 if (remove)
69 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
70 else
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070071 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
72}
73
Sudeep Holla97fd6012018-07-06 12:02:43 +010074void numa_add_cpu(unsigned int cpu)
75{
76 numa_update_cpu(cpu, false);
77}
78
79void numa_remove_cpu(unsigned int cpu)
80{
81 numa_update_cpu(cpu, true);
82}
83
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070084void numa_clear_node(unsigned int cpu)
85{
Sudeep Holla97fd6012018-07-06 12:02:43 +010086 numa_remove_cpu(cpu);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070087 set_cpu_numa_node(cpu, NUMA_NO_NODE);
88}
89
90/*
91 * Allocate node_to_cpumask_map based on number of available nodes
92 * Requires node_possible_map to be valid.
93 *
94 * Note: cpumask_of_node() is not valid until after this is done.
95 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
96 */
97static void __init setup_node_to_cpumask_map(void)
98{
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070099 int node;
100
101 /* setup nr_node_ids if not done yet */
102 if (nr_node_ids == MAX_NUMNODES)
103 setup_nr_node_ids();
104
105 /* allocate and clear the mapping */
106 for (node = 0; node < nr_node_ids; node++) {
107 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
108 cpumask_clear(node_to_cpumask_map[node]);
109 }
110
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700111 /* cpumask_of_node() will now work */
Alexey Dobriyanb9726c22019-03-05 15:48:26 -0800112 pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700113}
114
115/*
Qian Caib1ce45e2019-03-14 12:16:19 -0400116 * Set the cpu to node and mem mapping
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700117 */
118void numa_store_cpu_info(unsigned int cpu)
119{
Sudeep Holla97fd6012018-07-06 12:02:43 +0100120 set_cpu_numa_node(cpu, cpu_to_node_map[cpu]);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700121}
122
123void __init early_map_cpu_to_node(unsigned int cpu, int nid)
124{
125 /* fallback to node 0 */
Zhen Lei7ba5f602016-09-01 14:55:04 +0800126 if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700127 nid = 0;
128
129 cpu_to_node_map[cpu] = nid;
Zhen Lei7ba5f602016-09-01 14:55:04 +0800130
131 /*
132 * We should set the numa node of cpu0 as soon as possible, because it
133 * has already been set up online before. cpu_to_node(0) will soon be
134 * called.
135 */
136 if (!cpu)
137 set_cpu_numa_node(cpu, nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700138}
139
Zhen Lei7af3a0a2016-09-01 14:55:00 +0800140#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
141unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
142EXPORT_SYMBOL(__per_cpu_offset);
143
144static int __init early_cpu_to_node(int cpu)
145{
146 return cpu_to_node_map[cpu];
147}
148
149static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
150{
Yisheng Xie26984c32016-10-21 16:13:55 +0800151 return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
Zhen Lei7af3a0a2016-09-01 14:55:00 +0800152}
153
154static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
155 size_t align)
156{
157 int nid = early_cpu_to_node(cpu);
158
Mike Rapoporteb31d552018-10-30 15:08:04 -0700159 return memblock_alloc_try_nid(size, align,
Zhen Lei7af3a0a2016-09-01 14:55:00 +0800160 __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
161}
162
163static void __init pcpu_fc_free(void *ptr, size_t size)
164{
165 memblock_free_early(__pa(ptr), size);
166}
167
168void __init setup_per_cpu_areas(void)
169{
170 unsigned long delta;
171 unsigned int cpu;
172 int rc;
173
174 /*
175 * Always reserve area for module percpu variables. That's
176 * what the legacy allocator did.
177 */
178 rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
179 PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
180 pcpu_cpu_distance,
181 pcpu_fc_alloc, pcpu_fc_free);
182 if (rc < 0)
183 panic("Failed to initialize percpu areas.");
184
185 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
186 for_each_possible_cpu(cpu)
187 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
188}
189#endif
190
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700191/**
Qian Caib1ce45e2019-03-14 12:16:19 -0400192 * numa_add_memblk() - Set node id to memblk
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700193 * @nid: NUMA node ID of the new memblk
194 * @start: Start address of the new memblk
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700195 * @end: End address of the new memblk
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700196 *
197 * RETURNS:
198 * 0 on success, -errno on failure.
199 */
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700200int __init numa_add_memblk(int nid, u64 start, u64 end)
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700201{
202 int ret;
203
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700204 ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700205 if (ret < 0) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800206 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700207 start, (end - 1), nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700208 return ret;
209 }
210
211 node_set(nid, numa_nodes_parsed);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700212 return ret;
213}
214
Qian Caib1ce45e2019-03-14 12:16:19 -0400215/*
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700216 * Initialize NODE_DATA for a node on the local memory
217 */
218static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
219{
220 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
221 u64 nd_pa;
222 void *nd;
223 int tnid;
224
Punit Agrawalece4b202017-07-20 12:03:59 +0100225 if (start_pfn >= end_pfn)
Hanjun Guo3f7a09f2016-10-21 16:13:56 +0800226 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700227
Mike Rapoport9a8dd702018-10-30 15:07:59 -0700228 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
Mike Rapoport33755572019-03-11 23:29:21 -0700229 if (!nd_pa)
230 panic("Cannot allocate %zu bytes for node %d data\n",
231 nd_size, nid);
232
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700233 nd = __va(nd_pa);
234
235 /* report and initialize */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800236 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700237 nd_pa, nd_pa + nd_size - 1);
238 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
239 if (tnid != nid)
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800240 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700241
242 node_data[nid] = nd;
243 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
244 NODE_DATA(nid)->node_id = nid;
245 NODE_DATA(nid)->node_start_pfn = start_pfn;
246 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
247}
248
Qian Caib1ce45e2019-03-14 12:16:19 -0400249/*
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700250 * numa_free_distance
251 *
252 * The current table is freed.
253 */
254void __init numa_free_distance(void)
255{
256 size_t size;
257
258 if (!numa_distance)
259 return;
260
261 size = numa_distance_cnt * numa_distance_cnt *
262 sizeof(numa_distance[0]);
263
264 memblock_free(__pa(numa_distance), size);
265 numa_distance_cnt = 0;
266 numa_distance = NULL;
267}
268
Qian Caib1ce45e2019-03-14 12:16:19 -0400269/*
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700270 * Create a new NUMA distance table.
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700271 */
272static int __init numa_alloc_distance(void)
273{
274 size_t size;
275 u64 phys;
276 int i, j;
277
278 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
279 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
280 size, PAGE_SIZE);
281 if (WARN_ON(!phys))
282 return -ENOMEM;
283
284 memblock_reserve(phys, size);
285
286 numa_distance = __va(phys);
287 numa_distance_cnt = nr_node_ids;
288
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;
294
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800295 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700296
297 return 0;
298}
299
300/**
Qian Caib1ce45e2019-03-14 12:16:19 -0400301 * numa_set_distance() - Set inter node NUMA distance from node to node.
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700302 * @from: the 'from' node to set distance
303 * @to: the 'to' node to set distance
304 * @distance: NUMA distance
305 *
306 * Set the distance from node @from to @to to @distance.
307 * If distance table doesn't exist, a warning is printed.
308 *
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.
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700311 */
312void __init numa_set_distance(int from, int to, int distance)
313{
314 if (!numa_distance) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800315 pr_warn_once("Warning: distance table not allocated yet\n");
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700316 return;
317 }
318
319 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
320 from < 0 || to < 0) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800321 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700322 from, to, distance);
323 return;
324 }
325
326 if ((u8)distance != distance ||
327 (from == to && distance != LOCAL_DISTANCE)) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800328 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700329 from, to, distance);
330 return;
331 }
332
333 numa_distance[from * numa_distance_cnt + to] = distance;
334}
335
Qian Caib1ce45e2019-03-14 12:16:19 -0400336/*
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700337 * Return NUMA distance @from to @to
338 */
339int __node_distance(int from, int to)
340{
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];
344}
345EXPORT_SYMBOL(__node_distance);
346
347static int __init numa_register_nodes(void)
348{
349 int nid;
350 struct memblock_region *mblk;
351
352 /* Check that valid nid is set to memblks */
Mike Rapoportd622abf2020-06-03 15:56:53 -0700353 for_each_memblock(memory, mblk) {
354 int mblk_nid = memblock_get_region_node(mblk);
355
356 if (mblk_nid == NUMA_NO_NODE || mblk_nid >= MAX_NUMNODES) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800357 pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
Mike Rapoportd622abf2020-06-03 15:56:53 -0700358 mblk_nid, mblk->base,
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700359 mblk->base + mblk->size - 1);
360 return -EINVAL;
361 }
Mike Rapoportd622abf2020-06-03 15:56:53 -0700362 }
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700363
364 /* Finally register nodes. */
365 for_each_node_mask(nid, numa_nodes_parsed) {
366 unsigned long start_pfn, end_pfn;
367
368 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
369 setup_node_data(nid, start_pfn, end_pfn);
370 node_set_online(nid);
371 }
372
373 /* Setup online nodes to actual nodes*/
374 node_possible_map = numa_nodes_parsed;
375
376 return 0;
377}
378
379static int __init numa_init(int (*init_func)(void))
380{
381 int ret;
382
383 nodes_clear(numa_nodes_parsed);
384 nodes_clear(node_possible_map);
385 nodes_clear(node_online_map);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700386
387 ret = numa_alloc_distance();
388 if (ret < 0)
389 return ret;
390
391 ret = init_func();
392 if (ret < 0)
Anshuman Khandual52338082018-09-22 21:09:56 +0530393 goto out_free_distance;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700394
Zhen Lei794224ea2016-09-01 14:54:56 +0800395 if (nodes_empty(numa_nodes_parsed)) {
396 pr_info("No NUMA configuration found\n");
Anshuman Khandual52338082018-09-22 21:09:56 +0530397 ret = -EINVAL;
398 goto out_free_distance;
Zhen Lei794224ea2016-09-01 14:54:56 +0800399 }
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700400
401 ret = numa_register_nodes();
402 if (ret < 0)
Anshuman Khandual52338082018-09-22 21:09:56 +0530403 goto out_free_distance;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700404
405 setup_node_to_cpumask_map();
406
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700407 return 0;
Anshuman Khandual52338082018-09-22 21:09:56 +0530408out_free_distance:
409 numa_free_distance();
410 return ret;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700411}
412
413/**
Qian Caib1ce45e2019-03-14 12:16:19 -0400414 * dummy_numa_init() - Fallback dummy NUMA init
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700415 *
416 * Used if there's no underlying NUMA architecture, NUMA initialization
417 * fails, or NUMA is disabled on the command line.
418 *
419 * Must online at least one node (node 0) and add memory blocks that cover all
420 * allowed memory. It is unlikely that this function fails.
Qian Caib1ce45e2019-03-14 12:16:19 -0400421 *
422 * Return: 0 on success, -errno on failure.
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700423 */
424static int __init dummy_numa_init(void)
425{
426 int ret;
427 struct memblock_region *mblk;
428
David Daney34c33372016-05-24 15:35:37 -0700429 if (numa_off)
430 pr_info("NUMA disabled\n"); /* Forced off on command line. */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800431 pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
Anshuman Khandual77cfe952018-09-22 21:09:55 +0530432 memblock_start_of_DRAM(), memblock_end_of_DRAM() - 1);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700433
434 for_each_memblock(memory, mblk) {
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700435 ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700436 if (!ret)
437 continue;
438
439 pr_err("NUMA init failed\n");
440 return ret;
441 }
442
David Daney34c33372016-05-24 15:35:37 -0700443 numa_off = true;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700444 return 0;
445}
446
447/**
Qian Caib1ce45e2019-03-14 12:16:19 -0400448 * arm64_numa_init() - Initialize NUMA
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700449 *
Qian Caib1ce45e2019-03-14 12:16:19 -0400450 * Try each configured NUMA initialization method until one succeeds. The
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700451 * last fallback is dummy single node config encomapssing whole memory.
452 */
453void __init arm64_numa_init(void)
454{
455 if (!numa_off) {
Hanjun Guod8b47fc2016-05-24 15:35:44 -0700456 if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
457 return;
458 if (acpi_disabled && !numa_init(of_numa_init))
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700459 return;
460 }
461
462 numa_init(dummy_numa_init);
463}
Robin Murphy4ab21502018-12-11 18:48:48 +0000464
465/*
466 * We hope that we will be hotplugging memory on nodes we already know about,
467 * such that acpi_get_node() succeeds and we never fall back to this...
468 */
469int memory_add_physaddr_to_nid(u64 addr)
470{
471 pr_warn("Unknown node for memory at 0x%llx, assuming node 0\n", addr);
472 return 0;
473}