Commit | Line | Data |
---|---|---|
1a2db300 GK |
1 | /* |
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 | ||
f11c7bac KW |
20 | #define pr_fmt(fmt) "NUMA: " fmt |
21 | ||
d8b47fca | 22 | #include <linux/acpi.h> |
1a2db300 GK |
23 | #include <linux/memblock.h> |
24 | #include <linux/module.h> | |
25 | #include <linux/of.h> | |
26 | ||
bfe6c8a8 | 27 | #include <asm/acpi.h> |
7af3a0a9 | 28 | #include <asm/sections.h> |
bfe6c8a8 | 29 | |
1a2db300 GK |
30 | struct pglist_data *node_data[MAX_NUMNODES] __read_mostly; |
31 | EXPORT_SYMBOL(node_data); | |
32 | nodemask_t numa_nodes_parsed __initdata; | |
33 | static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE }; | |
34 | ||
35 | static int numa_distance_cnt; | |
36 | static u8 *numa_distance; | |
aec03f89 | 37 | bool numa_off; |
1a2db300 GK |
38 | |
39 | static __init int numa_parse_early_param(char *opt) | |
40 | { | |
41 | if (!opt) | |
42 | return -EINVAL; | |
f11c7bac | 43 | if (!strncmp(opt, "off", 3)) |
34c33370 | 44 | numa_off = true; |
f11c7bac | 45 | |
1a2db300 GK |
46 | return 0; |
47 | } | |
48 | early_param("numa", numa_parse_early_param); | |
49 | ||
50 | cpumask_var_t node_to_cpumask_map[MAX_NUMNODES]; | |
51 | EXPORT_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 | */ | |
58 | const 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 | } | |
68 | EXPORT_SYMBOL(cpumask_of_node); | |
69 | ||
70 | #endif | |
71 | ||
97fd6016 | 72 | static void numa_update_cpu(unsigned int cpu, bool remove) |
1a2db300 | 73 | { |
97fd6016 SH |
74 | 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 | |
1a2db300 GK |
82 | cpumask_set_cpu(cpu, node_to_cpumask_map[nid]); |
83 | } | |
84 | ||
97fd6016 | 85 | void numa_add_cpu(unsigned int cpu) |
1a2db300 | 86 | { |
97fd6016 SH |
87 | numa_update_cpu(cpu, false); |
88 | } | |
1a2db300 | 89 | |
97fd6016 SH |
90 | void numa_remove_cpu(unsigned int cpu) |
91 | { | |
92 | numa_update_cpu(cpu, true); | |
93 | } | |
94 | ||
95 | void numa_clear_node(unsigned int cpu) | |
96 | { | |
97 | numa_remove_cpu(cpu); | |
1a2db300 GK |
98 | 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 | */ | |
108 | static void __init setup_node_to_cpumask_map(void) | |
109 | { | |
1a2db300 GK |
110 | 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 | ||
1a2db300 | 122 | /* cpumask_of_node() will now work */ |
b9726c26 | 123 | pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids); |
1a2db300 GK |
124 | } |
125 | ||
126 | /* | |
b1ce45e8 | 127 | * Set the cpu to node and mem mapping |
1a2db300 GK |
128 | */ |
129 | void numa_store_cpu_info(unsigned int cpu) | |
130 | { | |
97fd6016 | 131 | set_cpu_numa_node(cpu, cpu_to_node_map[cpu]); |
1a2db300 GK |
132 | } |
133 | ||
134 | void __init early_map_cpu_to_node(unsigned int cpu, int nid) | |
135 | { | |
136 | /* fallback to node 0 */ | |
7ba5f605 | 137 | if (nid < 0 || nid >= MAX_NUMNODES || numa_off) |
1a2db300 GK |
138 | nid = 0; |
139 | ||
140 | cpu_to_node_map[cpu] = nid; | |
7ba5f605 ZL |
141 | |
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); | |
1a2db300 GK |
149 | } |
150 | ||
7af3a0a9 ZL |
151 | #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA |
152 | unsigned long __per_cpu_offset[NR_CPUS] __read_mostly; | |
153 | EXPORT_SYMBOL(__per_cpu_offset); | |
154 | ||
155 | static int __init early_cpu_to_node(int cpu) | |
156 | { | |
157 | return cpu_to_node_map[cpu]; | |
158 | } | |
159 | ||
160 | static int __init pcpu_cpu_distance(unsigned int from, unsigned int to) | |
161 | { | |
26984c3b | 162 | return node_distance(early_cpu_to_node(from), early_cpu_to_node(to)); |
7af3a0a9 ZL |
163 | } |
164 | ||
165 | static 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 | ||
eb31d559 | 170 | return memblock_alloc_try_nid(size, align, |
7af3a0a9 ZL |
171 | __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid); |
172 | } | |
173 | ||
174 | static void __init pcpu_fc_free(void *ptr, size_t size) | |
175 | { | |
176 | memblock_free_early(__pa(ptr), size); | |
177 | } | |
178 | ||
179 | void __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 | ||
1a2db300 | 202 | /** |
b1ce45e8 | 203 | * numa_add_memblk() - Set node id to memblk |
1a2db300 GK |
204 | * @nid: NUMA node ID of the new memblk |
205 | * @start: Start address of the new memblk | |
8ccbbdaa | 206 | * @end: End address of the new memblk |
1a2db300 GK |
207 | * |
208 | * RETURNS: | |
209 | * 0 on success, -errno on failure. | |
210 | */ | |
8ccbbdaa | 211 | int __init numa_add_memblk(int nid, u64 start, u64 end) |
1a2db300 GK |
212 | { |
213 | int ret; | |
214 | ||
8ccbbdaa | 215 | ret = memblock_set_node(start, (end - start), &memblock.memory, nid); |
1a2db300 | 216 | if (ret < 0) { |
f11c7bac | 217 | pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n", |
8ccbbdaa | 218 | start, (end - 1), nid); |
1a2db300 GK |
219 | return ret; |
220 | } | |
221 | ||
222 | node_set(nid, numa_nodes_parsed); | |
1a2db300 GK |
223 | return ret; |
224 | } | |
225 | ||
b1ce45e8 | 226 | /* |
1a2db300 GK |
227 | * Initialize NODE_DATA for a node on the local memory |
228 | */ | |
229 | static 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 | ||
ece4b206 | 236 | if (start_pfn >= end_pfn) |
3f7a09f4 | 237 | pr_info("Initmem setup node %d [<memory-less node>]\n", nid); |
1a2db300 | 238 | |
9a8dd708 | 239 | nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid); |
33755574 MR |
240 | if (!nd_pa) |
241 | panic("Cannot allocate %zu bytes for node %d data\n", | |
242 | nd_size, nid); | |
243 | ||
1a2db300 GK |
244 | nd = __va(nd_pa); |
245 | ||
246 | /* report and initialize */ | |
f11c7bac | 247 | pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n", |
1a2db300 GK |
248 | nd_pa, nd_pa + nd_size - 1); |
249 | tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT); | |
250 | if (tnid != nid) | |
f11c7bac | 251 | pr_info("NODE_DATA(%d) on node %d\n", nid, tnid); |
1a2db300 GK |
252 | |
253 | node_data[nid] = nd; | |
254 | memset(NODE_DATA(nid), 0, sizeof(pg_data_t)); | |
255 | NODE_DATA(nid)->node_id = nid; | |
256 | NODE_DATA(nid)->node_start_pfn = start_pfn; | |
257 | NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn; | |
258 | } | |
259 | ||
b1ce45e8 | 260 | /* |
1a2db300 GK |
261 | * numa_free_distance |
262 | * | |
263 | * The current table is freed. | |
264 | */ | |
265 | void __init numa_free_distance(void) | |
266 | { | |
267 | size_t size; | |
268 | ||
269 | if (!numa_distance) | |
270 | return; | |
271 | ||
272 | size = numa_distance_cnt * numa_distance_cnt * | |
273 | sizeof(numa_distance[0]); | |
274 | ||
275 | memblock_free(__pa(numa_distance), size); | |
276 | numa_distance_cnt = 0; | |
277 | numa_distance = NULL; | |
278 | } | |
279 | ||
b1ce45e8 | 280 | /* |
1a2db300 | 281 | * Create a new NUMA distance table. |
1a2db300 GK |
282 | */ |
283 | static int __init numa_alloc_distance(void) | |
284 | { | |
285 | size_t size; | |
286 | u64 phys; | |
287 | int i, j; | |
288 | ||
289 | size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]); | |
290 | phys = memblock_find_in_range(0, PFN_PHYS(max_pfn), | |
291 | size, PAGE_SIZE); | |
292 | if (WARN_ON(!phys)) | |
293 | return -ENOMEM; | |
294 | ||
295 | memblock_reserve(phys, size); | |
296 | ||
297 | numa_distance = __va(phys); | |
298 | numa_distance_cnt = nr_node_ids; | |
299 | ||
300 | /* fill with the default distances */ | |
301 | for (i = 0; i < numa_distance_cnt; i++) | |
302 | for (j = 0; j < numa_distance_cnt; j++) | |
303 | numa_distance[i * numa_distance_cnt + j] = i == j ? | |
304 | LOCAL_DISTANCE : REMOTE_DISTANCE; | |
305 | ||
f11c7bac | 306 | pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt); |
1a2db300 GK |
307 | |
308 | return 0; | |
309 | } | |
310 | ||
311 | /** | |
b1ce45e8 | 312 | * numa_set_distance() - Set inter node NUMA distance from node to node. |
1a2db300 GK |
313 | * @from: the 'from' node to set distance |
314 | * @to: the 'to' node to set distance | |
315 | * @distance: NUMA distance | |
316 | * | |
317 | * Set the distance from node @from to @to to @distance. | |
318 | * If distance table doesn't exist, a warning is printed. | |
319 | * | |
320 | * If @from or @to is higher than the highest known node or lower than zero | |
321 | * or @distance doesn't make sense, the call is ignored. | |
1a2db300 GK |
322 | */ |
323 | void __init numa_set_distance(int from, int to, int distance) | |
324 | { | |
325 | if (!numa_distance) { | |
f11c7bac | 326 | pr_warn_once("Warning: distance table not allocated yet\n"); |
1a2db300 GK |
327 | return; |
328 | } | |
329 | ||
330 | if (from >= numa_distance_cnt || to >= numa_distance_cnt || | |
331 | from < 0 || to < 0) { | |
f11c7bac | 332 | pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n", |
1a2db300 GK |
333 | from, to, distance); |
334 | return; | |
335 | } | |
336 | ||
337 | if ((u8)distance != distance || | |
338 | (from == to && distance != LOCAL_DISTANCE)) { | |
f11c7bac | 339 | pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n", |
1a2db300 GK |
340 | from, to, distance); |
341 | return; | |
342 | } | |
343 | ||
344 | numa_distance[from * numa_distance_cnt + to] = distance; | |
345 | } | |
346 | ||
b1ce45e8 | 347 | /* |
1a2db300 GK |
348 | * Return NUMA distance @from to @to |
349 | */ | |
350 | int __node_distance(int from, int to) | |
351 | { | |
352 | if (from >= numa_distance_cnt || to >= numa_distance_cnt) | |
353 | return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE; | |
354 | return numa_distance[from * numa_distance_cnt + to]; | |
355 | } | |
356 | EXPORT_SYMBOL(__node_distance); | |
357 | ||
358 | static int __init numa_register_nodes(void) | |
359 | { | |
360 | int nid; | |
361 | struct memblock_region *mblk; | |
362 | ||
363 | /* Check that valid nid is set to memblks */ | |
364 | for_each_memblock(memory, mblk) | |
365 | if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) { | |
f11c7bac | 366 | pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n", |
1a2db300 GK |
367 | mblk->nid, mblk->base, |
368 | mblk->base + mblk->size - 1); | |
369 | return -EINVAL; | |
370 | } | |
371 | ||
372 | /* Finally register nodes. */ | |
373 | for_each_node_mask(nid, numa_nodes_parsed) { | |
374 | unsigned long start_pfn, end_pfn; | |
375 | ||
376 | get_pfn_range_for_nid(nid, &start_pfn, &end_pfn); | |
377 | setup_node_data(nid, start_pfn, end_pfn); | |
378 | node_set_online(nid); | |
379 | } | |
380 | ||
381 | /* Setup online nodes to actual nodes*/ | |
382 | node_possible_map = numa_nodes_parsed; | |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
387 | static int __init numa_init(int (*init_func)(void)) | |
388 | { | |
389 | int ret; | |
390 | ||
391 | nodes_clear(numa_nodes_parsed); | |
392 | nodes_clear(node_possible_map); | |
393 | nodes_clear(node_online_map); | |
1a2db300 GK |
394 | |
395 | ret = numa_alloc_distance(); | |
396 | if (ret < 0) | |
397 | return ret; | |
398 | ||
399 | ret = init_func(); | |
400 | if (ret < 0) | |
52338088 | 401 | goto out_free_distance; |
1a2db300 | 402 | |
794224ea ZL |
403 | if (nodes_empty(numa_nodes_parsed)) { |
404 | pr_info("No NUMA configuration found\n"); | |
52338088 AK |
405 | ret = -EINVAL; |
406 | goto out_free_distance; | |
794224ea | 407 | } |
1a2db300 GK |
408 | |
409 | ret = numa_register_nodes(); | |
410 | if (ret < 0) | |
52338088 | 411 | goto out_free_distance; |
1a2db300 GK |
412 | |
413 | setup_node_to_cpumask_map(); | |
414 | ||
1a2db300 | 415 | return 0; |
52338088 AK |
416 | out_free_distance: |
417 | numa_free_distance(); | |
418 | return ret; | |
1a2db300 GK |
419 | } |
420 | ||
421 | /** | |
b1ce45e8 | 422 | * dummy_numa_init() - Fallback dummy NUMA init |
1a2db300 GK |
423 | * |
424 | * Used if there's no underlying NUMA architecture, NUMA initialization | |
425 | * fails, or NUMA is disabled on the command line. | |
426 | * | |
427 | * Must online at least one node (node 0) and add memory blocks that cover all | |
428 | * allowed memory. It is unlikely that this function fails. | |
b1ce45e8 QC |
429 | * |
430 | * Return: 0 on success, -errno on failure. | |
1a2db300 GK |
431 | */ |
432 | static int __init dummy_numa_init(void) | |
433 | { | |
434 | int ret; | |
435 | struct memblock_region *mblk; | |
436 | ||
34c33370 DD |
437 | if (numa_off) |
438 | pr_info("NUMA disabled\n"); /* Forced off on command line. */ | |
f11c7bac | 439 | pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n", |
77cfe950 | 440 | memblock_start_of_DRAM(), memblock_end_of_DRAM() - 1); |
1a2db300 GK |
441 | |
442 | for_each_memblock(memory, mblk) { | |
8ccbbdaa | 443 | ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size); |
1a2db300 GK |
444 | if (!ret) |
445 | continue; | |
446 | ||
447 | pr_err("NUMA init failed\n"); | |
448 | return ret; | |
449 | } | |
450 | ||
34c33370 | 451 | numa_off = true; |
1a2db300 GK |
452 | return 0; |
453 | } | |
454 | ||
455 | /** | |
b1ce45e8 | 456 | * arm64_numa_init() - Initialize NUMA |
1a2db300 | 457 | * |
b1ce45e8 | 458 | * Try each configured NUMA initialization method until one succeeds. The |
1a2db300 GK |
459 | * last fallback is dummy single node config encomapssing whole memory. |
460 | */ | |
461 | void __init arm64_numa_init(void) | |
462 | { | |
463 | if (!numa_off) { | |
d8b47fca HG |
464 | if (!acpi_disabled && !numa_init(arm64_acpi_numa_init)) |
465 | return; | |
466 | if (acpi_disabled && !numa_init(of_numa_init)) | |
1a2db300 GK |
467 | return; |
468 | } | |
469 | ||
470 | numa_init(dummy_numa_init); | |
471 | } | |
4ab21506 RM |
472 | |
473 | /* | |
474 | * We hope that we will be hotplugging memory on nodes we already know about, | |
475 | * such that acpi_get_node() succeeds and we never fall back to this... | |
476 | */ | |
477 | int memory_add_physaddr_to_nid(u64 addr) | |
478 | { | |
479 | pr_warn("Unknown node for memory at 0x%llx, assuming node 0\n", addr); | |
480 | return 0; | |
481 | } |