blob: 087064d5dcc1ba649dc567ca010b69c7053149b0 [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/bootmem.h>
24#include <linux/memblock.h>
25#include <linux/module.h>
26#include <linux/of.h>
27
Catalin Marinasbfe6c8a2016-08-15 16:33:10 +010028#include <asm/acpi.h>
Zhen Lei7af3a0a2016-09-01 14:55:00 +080029#include <asm/sections.h>
Catalin Marinasbfe6c8a2016-08-15 16:33:10 +010030
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070031struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
32EXPORT_SYMBOL(node_data);
33nodemask_t numa_nodes_parsed __initdata;
34static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
35
36static int numa_distance_cnt;
37static u8 *numa_distance;
David Daney34c33372016-05-24 15:35:37 -070038static bool numa_off;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070039
40static __init int numa_parse_early_param(char *opt)
41{
42 if (!opt)
43 return -EINVAL;
Kefeng Wangf11c7ba2016-09-01 14:54:59 +080044 if (!strncmp(opt, "off", 3))
David Daney34c33372016-05-24 15:35:37 -070045 numa_off = true;
Kefeng Wangf11c7ba2016-09-01 14:54:59 +080046
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -070047 return 0;
48}
49early_param("numa", numa_parse_early_param);
50
51cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
52EXPORT_SYMBOL(node_to_cpumask_map);
53
54#ifdef CONFIG_DEBUG_PER_CPU_MAPS
55
56/*
57 * Returns a pointer to the bitmask of CPUs on Node 'node'.
58 */
59const struct cpumask *cpumask_of_node(int node)
60{
61 if (WARN_ON(node >= nr_node_ids))
62 return cpu_none_mask;
63
64 if (WARN_ON(node_to_cpumask_map[node] == NULL))
65 return cpu_online_mask;
66
67 return node_to_cpumask_map[node];
68}
69EXPORT_SYMBOL(cpumask_of_node);
70
71#endif
72
73static void map_cpu_to_node(unsigned int cpu, int nid)
74{
75 set_cpu_numa_node(cpu, nid);
76 if (nid >= 0)
77 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
78}
79
80void numa_clear_node(unsigned int cpu)
81{
82 int nid = cpu_to_node(cpu);
83
84 if (nid >= 0)
85 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
86 set_cpu_numa_node(cpu, NUMA_NO_NODE);
87}
88
89/*
90 * Allocate node_to_cpumask_map based on number of available nodes
91 * Requires node_possible_map to be valid.
92 *
93 * Note: cpumask_of_node() is not valid until after this is done.
94 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
95 */
96static void __init setup_node_to_cpumask_map(void)
97{
98 unsigned int cpu;
99 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
111 for_each_possible_cpu(cpu)
112 set_cpu_numa_node(cpu, NUMA_NO_NODE);
113
114 /* cpumask_of_node() will now work */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800115 pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700116}
117
118/*
119 * Set the cpu to node and mem mapping
120 */
121void numa_store_cpu_info(unsigned int cpu)
122{
123 map_cpu_to_node(cpu, numa_off ? 0 : cpu_to_node_map[cpu]);
124}
125
126void __init early_map_cpu_to_node(unsigned int cpu, int nid)
127{
128 /* fallback to node 0 */
129 if (nid < 0 || nid >= MAX_NUMNODES)
130 nid = 0;
131
132 cpu_to_node_map[cpu] = nid;
133}
134
Zhen Lei7af3a0a2016-09-01 14:55:00 +0800135#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
136unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
137EXPORT_SYMBOL(__per_cpu_offset);
138
139static int __init early_cpu_to_node(int cpu)
140{
141 return cpu_to_node_map[cpu];
142}
143
144static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
145{
146 return node_distance(from, to);
147}
148
149static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
150 size_t align)
151{
152 int nid = early_cpu_to_node(cpu);
153
154 return memblock_virt_alloc_try_nid(size, align,
155 __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
156}
157
158static void __init pcpu_fc_free(void *ptr, size_t size)
159{
160 memblock_free_early(__pa(ptr), size);
161}
162
163void __init setup_per_cpu_areas(void)
164{
165 unsigned long delta;
166 unsigned int cpu;
167 int rc;
168
169 /*
170 * Always reserve area for module percpu variables. That's
171 * what the legacy allocator did.
172 */
173 rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
174 PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
175 pcpu_cpu_distance,
176 pcpu_fc_alloc, pcpu_fc_free);
177 if (rc < 0)
178 panic("Failed to initialize percpu areas.");
179
180 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
181 for_each_possible_cpu(cpu)
182 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
183}
184#endif
185
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700186/**
187 * numa_add_memblk - Set node id to memblk
188 * @nid: NUMA node ID of the new memblk
189 * @start: Start address of the new memblk
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700190 * @end: End address of the new memblk
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700191 *
192 * RETURNS:
193 * 0 on success, -errno on failure.
194 */
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700195int __init numa_add_memblk(int nid, u64 start, u64 end)
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700196{
197 int ret;
198
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700199 ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700200 if (ret < 0) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800201 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700202 start, (end - 1), nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700203 return ret;
204 }
205
206 node_set(nid, numa_nodes_parsed);
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800207 pr_info("Adding memblock [0x%llx - 0x%llx] on node %d\n",
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700208 start, (end - 1), nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700209 return ret;
210}
211
212/**
213 * Initialize NODE_DATA for a node on the local memory
214 */
215static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
216{
217 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
218 u64 nd_pa;
219 void *nd;
220 int tnid;
221
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800222 pr_info("Initmem setup node %d [mem %#010Lx-%#010Lx]\n",
223 nid, start_pfn << PAGE_SHIFT, (end_pfn << PAGE_SHIFT) - 1);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700224
225 nd_pa = memblock_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
226 nd = __va(nd_pa);
227
228 /* report and initialize */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800229 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700230 nd_pa, nd_pa + nd_size - 1);
231 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
232 if (tnid != nid)
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800233 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700234
235 node_data[nid] = nd;
236 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
237 NODE_DATA(nid)->node_id = nid;
238 NODE_DATA(nid)->node_start_pfn = start_pfn;
239 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
240}
241
242/**
243 * numa_free_distance
244 *
245 * The current table is freed.
246 */
247void __init numa_free_distance(void)
248{
249 size_t size;
250
251 if (!numa_distance)
252 return;
253
254 size = numa_distance_cnt * numa_distance_cnt *
255 sizeof(numa_distance[0]);
256
257 memblock_free(__pa(numa_distance), size);
258 numa_distance_cnt = 0;
259 numa_distance = NULL;
260}
261
262/**
263 *
264 * Create a new NUMA distance table.
265 *
266 */
267static int __init numa_alloc_distance(void)
268{
269 size_t size;
270 u64 phys;
271 int i, j;
272
273 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
274 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
275 size, PAGE_SIZE);
276 if (WARN_ON(!phys))
277 return -ENOMEM;
278
279 memblock_reserve(phys, size);
280
281 numa_distance = __va(phys);
282 numa_distance_cnt = nr_node_ids;
283
284 /* fill with the default distances */
285 for (i = 0; i < numa_distance_cnt; i++)
286 for (j = 0; j < numa_distance_cnt; j++)
287 numa_distance[i * numa_distance_cnt + j] = i == j ?
288 LOCAL_DISTANCE : REMOTE_DISTANCE;
289
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800290 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700291
292 return 0;
293}
294
295/**
296 * numa_set_distance - Set inter node NUMA distance from node to node.
297 * @from: the 'from' node to set distance
298 * @to: the 'to' node to set distance
299 * @distance: NUMA distance
300 *
301 * Set the distance from node @from to @to to @distance.
302 * If distance table doesn't exist, a warning is printed.
303 *
304 * If @from or @to is higher than the highest known node or lower than zero
305 * or @distance doesn't make sense, the call is ignored.
306 *
307 */
308void __init numa_set_distance(int from, int to, int distance)
309{
310 if (!numa_distance) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800311 pr_warn_once("Warning: distance table not allocated yet\n");
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700312 return;
313 }
314
315 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
316 from < 0 || to < 0) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800317 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700318 from, to, distance);
319 return;
320 }
321
322 if ((u8)distance != distance ||
323 (from == to && distance != LOCAL_DISTANCE)) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800324 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700325 from, to, distance);
326 return;
327 }
328
329 numa_distance[from * numa_distance_cnt + to] = distance;
330}
331
332/**
333 * Return NUMA distance @from to @to
334 */
335int __node_distance(int from, int to)
336{
337 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
338 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
339 return numa_distance[from * numa_distance_cnt + to];
340}
341EXPORT_SYMBOL(__node_distance);
342
343static int __init numa_register_nodes(void)
344{
345 int nid;
346 struct memblock_region *mblk;
347
348 /* Check that valid nid is set to memblks */
349 for_each_memblock(memory, mblk)
350 if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800351 pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700352 mblk->nid, mblk->base,
353 mblk->base + mblk->size - 1);
354 return -EINVAL;
355 }
356
357 /* Finally register nodes. */
358 for_each_node_mask(nid, numa_nodes_parsed) {
359 unsigned long start_pfn, end_pfn;
360
361 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
362 setup_node_data(nid, start_pfn, end_pfn);
363 node_set_online(nid);
364 }
365
366 /* Setup online nodes to actual nodes*/
367 node_possible_map = numa_nodes_parsed;
368
369 return 0;
370}
371
372static int __init numa_init(int (*init_func)(void))
373{
374 int ret;
375
376 nodes_clear(numa_nodes_parsed);
377 nodes_clear(node_possible_map);
378 nodes_clear(node_online_map);
379 numa_free_distance();
380
381 ret = numa_alloc_distance();
382 if (ret < 0)
383 return ret;
384
385 ret = init_func();
386 if (ret < 0)
387 return ret;
388
Zhen Lei794224ea2016-09-01 14:54:56 +0800389 if (nodes_empty(numa_nodes_parsed)) {
390 pr_info("No NUMA configuration found\n");
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700391 return -EINVAL;
Zhen Lei794224ea2016-09-01 14:54:56 +0800392 }
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700393
394 ret = numa_register_nodes();
395 if (ret < 0)
396 return ret;
397
398 setup_node_to_cpumask_map();
399
400 /* init boot processor */
401 cpu_to_node_map[0] = 0;
402 map_cpu_to_node(0, 0);
403
404 return 0;
405}
406
407/**
408 * dummy_numa_init - Fallback dummy NUMA init
409 *
410 * Used if there's no underlying NUMA architecture, NUMA initialization
411 * fails, or NUMA is disabled on the command line.
412 *
413 * Must online at least one node (node 0) and add memory blocks that cover all
414 * allowed memory. It is unlikely that this function fails.
415 */
416static int __init dummy_numa_init(void)
417{
418 int ret;
419 struct memblock_region *mblk;
420
David Daney34c33372016-05-24 15:35:37 -0700421 if (numa_off)
422 pr_info("NUMA disabled\n"); /* Forced off on command line. */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800423 pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
424 0LLU, PFN_PHYS(max_pfn) - 1);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700425
426 for_each_memblock(memory, mblk) {
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700427 ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700428 if (!ret)
429 continue;
430
431 pr_err("NUMA init failed\n");
432 return ret;
433 }
434
David Daney34c33372016-05-24 15:35:37 -0700435 numa_off = true;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700436 return 0;
437}
438
439/**
440 * arm64_numa_init - Initialize NUMA
441 *
442 * Try each configured NUMA initialization method until one succeeds. The
443 * last fallback is dummy single node config encomapssing whole memory.
444 */
445void __init arm64_numa_init(void)
446{
447 if (!numa_off) {
Hanjun Guod8b47fc2016-05-24 15:35:44 -0700448 if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
449 return;
450 if (acpi_disabled && !numa_init(of_numa_init))
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700451 return;
452 }
453
454 numa_init(dummy_numa_init);
455}