blob: 0e75b537de0cce405124ca8d124630cfa5d9342f [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>
29
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;
David Daney34c33372016-05-24 15:35:37 -070037static bool 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
72static void map_cpu_to_node(unsigned int cpu, int nid)
73{
74 set_cpu_numa_node(cpu, nid);
75 if (nid >= 0)
76 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
77}
78
79void numa_clear_node(unsigned int cpu)
80{
81 int nid = cpu_to_node(cpu);
82
83 if (nid >= 0)
84 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
85 set_cpu_numa_node(cpu, NUMA_NO_NODE);
86}
87
88/*
89 * Allocate node_to_cpumask_map based on number of available nodes
90 * Requires node_possible_map to be valid.
91 *
92 * Note: cpumask_of_node() is not valid until after this is done.
93 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
94 */
95static void __init setup_node_to_cpumask_map(void)
96{
97 unsigned int cpu;
98 int node;
99
100 /* setup nr_node_ids if not done yet */
101 if (nr_node_ids == MAX_NUMNODES)
102 setup_nr_node_ids();
103
104 /* allocate and clear the mapping */
105 for (node = 0; node < nr_node_ids; node++) {
106 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
107 cpumask_clear(node_to_cpumask_map[node]);
108 }
109
110 for_each_possible_cpu(cpu)
111 set_cpu_numa_node(cpu, NUMA_NO_NODE);
112
113 /* cpumask_of_node() will now work */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800114 pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700115}
116
117/*
118 * Set the cpu to node and mem mapping
119 */
120void numa_store_cpu_info(unsigned int cpu)
121{
122 map_cpu_to_node(cpu, numa_off ? 0 : cpu_to_node_map[cpu]);
123}
124
125void __init early_map_cpu_to_node(unsigned int cpu, int nid)
126{
127 /* fallback to node 0 */
128 if (nid < 0 || nid >= MAX_NUMNODES)
129 nid = 0;
130
131 cpu_to_node_map[cpu] = nid;
132}
133
134/**
135 * numa_add_memblk - Set node id to memblk
136 * @nid: NUMA node ID of the new memblk
137 * @start: Start address of the new memblk
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700138 * @end: End address of the new memblk
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700139 *
140 * RETURNS:
141 * 0 on success, -errno on failure.
142 */
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700143int __init numa_add_memblk(int nid, u64 start, u64 end)
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700144{
145 int ret;
146
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700147 ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700148 if (ret < 0) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800149 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700150 start, (end - 1), nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700151 return ret;
152 }
153
154 node_set(nid, numa_nodes_parsed);
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800155 pr_info("Adding memblock [0x%llx - 0x%llx] on node %d\n",
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700156 start, (end - 1), nid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700157 return ret;
158}
159
160/**
161 * Initialize NODE_DATA for a node on the local memory
162 */
163static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
164{
165 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
166 u64 nd_pa;
167 void *nd;
168 int tnid;
169
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800170 pr_info("Initmem setup node %d [mem %#010Lx-%#010Lx]\n",
171 nid, start_pfn << PAGE_SHIFT, (end_pfn << PAGE_SHIFT) - 1);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700172
173 nd_pa = memblock_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
174 nd = __va(nd_pa);
175
176 /* report and initialize */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800177 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700178 nd_pa, nd_pa + nd_size - 1);
179 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
180 if (tnid != nid)
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800181 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700182
183 node_data[nid] = nd;
184 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
185 NODE_DATA(nid)->node_id = nid;
186 NODE_DATA(nid)->node_start_pfn = start_pfn;
187 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
188}
189
190/**
191 * numa_free_distance
192 *
193 * The current table is freed.
194 */
195void __init numa_free_distance(void)
196{
197 size_t size;
198
199 if (!numa_distance)
200 return;
201
202 size = numa_distance_cnt * numa_distance_cnt *
203 sizeof(numa_distance[0]);
204
205 memblock_free(__pa(numa_distance), size);
206 numa_distance_cnt = 0;
207 numa_distance = NULL;
208}
209
210/**
211 *
212 * Create a new NUMA distance table.
213 *
214 */
215static int __init numa_alloc_distance(void)
216{
217 size_t size;
218 u64 phys;
219 int i, j;
220
221 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
222 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
223 size, PAGE_SIZE);
224 if (WARN_ON(!phys))
225 return -ENOMEM;
226
227 memblock_reserve(phys, size);
228
229 numa_distance = __va(phys);
230 numa_distance_cnt = nr_node_ids;
231
232 /* fill with the default distances */
233 for (i = 0; i < numa_distance_cnt; i++)
234 for (j = 0; j < numa_distance_cnt; j++)
235 numa_distance[i * numa_distance_cnt + j] = i == j ?
236 LOCAL_DISTANCE : REMOTE_DISTANCE;
237
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800238 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700239
240 return 0;
241}
242
243/**
244 * numa_set_distance - Set inter node NUMA distance from node to node.
245 * @from: the 'from' node to set distance
246 * @to: the 'to' node to set distance
247 * @distance: NUMA distance
248 *
249 * Set the distance from node @from to @to to @distance.
250 * If distance table doesn't exist, a warning is printed.
251 *
252 * If @from or @to is higher than the highest known node or lower than zero
253 * or @distance doesn't make sense, the call is ignored.
254 *
255 */
256void __init numa_set_distance(int from, int to, int distance)
257{
258 if (!numa_distance) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800259 pr_warn_once("Warning: distance table not allocated yet\n");
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700260 return;
261 }
262
263 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
264 from < 0 || to < 0) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800265 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700266 from, to, distance);
267 return;
268 }
269
270 if ((u8)distance != distance ||
271 (from == to && distance != LOCAL_DISTANCE)) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800272 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700273 from, to, distance);
274 return;
275 }
276
277 numa_distance[from * numa_distance_cnt + to] = distance;
278}
279
280/**
281 * Return NUMA distance @from to @to
282 */
283int __node_distance(int from, int to)
284{
285 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
286 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
287 return numa_distance[from * numa_distance_cnt + to];
288}
289EXPORT_SYMBOL(__node_distance);
290
291static int __init numa_register_nodes(void)
292{
293 int nid;
294 struct memblock_region *mblk;
295
296 /* Check that valid nid is set to memblks */
297 for_each_memblock(memory, mblk)
298 if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800299 pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700300 mblk->nid, mblk->base,
301 mblk->base + mblk->size - 1);
302 return -EINVAL;
303 }
304
305 /* Finally register nodes. */
306 for_each_node_mask(nid, numa_nodes_parsed) {
307 unsigned long start_pfn, end_pfn;
308
309 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
310 setup_node_data(nid, start_pfn, end_pfn);
311 node_set_online(nid);
312 }
313
314 /* Setup online nodes to actual nodes*/
315 node_possible_map = numa_nodes_parsed;
316
317 return 0;
318}
319
320static int __init numa_init(int (*init_func)(void))
321{
322 int ret;
323
324 nodes_clear(numa_nodes_parsed);
325 nodes_clear(node_possible_map);
326 nodes_clear(node_online_map);
327 numa_free_distance();
328
329 ret = numa_alloc_distance();
330 if (ret < 0)
331 return ret;
332
333 ret = init_func();
334 if (ret < 0)
335 return ret;
336
Zhen Lei794224ea2016-09-01 14:54:56 +0800337 if (nodes_empty(numa_nodes_parsed)) {
338 pr_info("No NUMA configuration found\n");
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700339 return -EINVAL;
Zhen Lei794224ea2016-09-01 14:54:56 +0800340 }
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700341
342 ret = numa_register_nodes();
343 if (ret < 0)
344 return ret;
345
346 setup_node_to_cpumask_map();
347
348 /* init boot processor */
349 cpu_to_node_map[0] = 0;
350 map_cpu_to_node(0, 0);
351
352 return 0;
353}
354
355/**
356 * dummy_numa_init - Fallback dummy NUMA init
357 *
358 * Used if there's no underlying NUMA architecture, NUMA initialization
359 * fails, or NUMA is disabled on the command line.
360 *
361 * Must online at least one node (node 0) and add memory blocks that cover all
362 * allowed memory. It is unlikely that this function fails.
363 */
364static int __init dummy_numa_init(void)
365{
366 int ret;
367 struct memblock_region *mblk;
368
David Daney34c33372016-05-24 15:35:37 -0700369 if (numa_off)
370 pr_info("NUMA disabled\n"); /* Forced off on command line. */
Kefeng Wangf11c7ba2016-09-01 14:54:59 +0800371 pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
372 0LLU, PFN_PHYS(max_pfn) - 1);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700373
374 for_each_memblock(memory, mblk) {
Hanjun Guo8ccbbda2016-05-24 15:35:36 -0700375 ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700376 if (!ret)
377 continue;
378
379 pr_err("NUMA init failed\n");
380 return ret;
381 }
382
David Daney34c33372016-05-24 15:35:37 -0700383 numa_off = true;
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700384 return 0;
385}
386
387/**
388 * arm64_numa_init - Initialize NUMA
389 *
390 * Try each configured NUMA initialization method until one succeeds. The
391 * last fallback is dummy single node config encomapssing whole memory.
392 */
393void __init arm64_numa_init(void)
394{
395 if (!numa_off) {
Hanjun Guod8b47fc2016-05-24 15:35:44 -0700396 if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
397 return;
398 if (acpi_disabled && !numa_init(of_numa_init))
Ganapatrao Kulkarni1a2db302016-04-08 15:50:27 -0700399 return;
400 }
401
402 numa_init(dummy_numa_init);
403}