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/bootmem.h> |
24 | #include <linux/memblock.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/of.h> | |
27 | ||
bfe6c8a8 | 28 | #include <asm/acpi.h> |
7af3a0a9 | 29 | #include <asm/sections.h> |
bfe6c8a8 | 30 | |
1a2db300 GK |
31 | struct pglist_data *node_data[MAX_NUMNODES] __read_mostly; |
32 | EXPORT_SYMBOL(node_data); | |
33 | nodemask_t numa_nodes_parsed __initdata; | |
34 | static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE }; | |
35 | ||
36 | static int numa_distance_cnt; | |
37 | static u8 *numa_distance; | |
34c33370 | 38 | static bool numa_off; |
1a2db300 GK |
39 | |
40 | static __init int numa_parse_early_param(char *opt) | |
41 | { | |
42 | if (!opt) | |
43 | return -EINVAL; | |
f11c7bac | 44 | if (!strncmp(opt, "off", 3)) |
34c33370 | 45 | numa_off = true; |
f11c7bac | 46 | |
1a2db300 GK |
47 | return 0; |
48 | } | |
49 | early_param("numa", numa_parse_early_param); | |
50 | ||
51 | cpumask_var_t node_to_cpumask_map[MAX_NUMNODES]; | |
52 | EXPORT_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 | */ | |
59 | const 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 | } | |
69 | EXPORT_SYMBOL(cpumask_of_node); | |
70 | ||
71 | #endif | |
72 | ||
73 | static 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 | ||
80 | void 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 | */ | |
96 | static void __init setup_node_to_cpumask_map(void) | |
97 | { | |
1a2db300 GK |
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 | ||
1a2db300 | 110 | /* cpumask_of_node() will now work */ |
f11c7bac | 111 | pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids); |
1a2db300 GK |
112 | } |
113 | ||
114 | /* | |
115 | * Set the cpu to node and mem mapping | |
116 | */ | |
117 | void numa_store_cpu_info(unsigned int cpu) | |
118 | { | |
7ba5f605 | 119 | map_cpu_to_node(cpu, cpu_to_node_map[cpu]); |
1a2db300 GK |
120 | } |
121 | ||
122 | void __init early_map_cpu_to_node(unsigned int cpu, int nid) | |
123 | { | |
124 | /* fallback to node 0 */ | |
7ba5f605 | 125 | if (nid < 0 || nid >= MAX_NUMNODES || numa_off) |
1a2db300 GK |
126 | nid = 0; |
127 | ||
128 | cpu_to_node_map[cpu] = nid; | |
7ba5f605 ZL |
129 | |
130 | /* | |
131 | * We should set the numa node of cpu0 as soon as possible, because it | |
132 | * has already been set up online before. cpu_to_node(0) will soon be | |
133 | * called. | |
134 | */ | |
135 | if (!cpu) | |
136 | set_cpu_numa_node(cpu, nid); | |
1a2db300 GK |
137 | } |
138 | ||
7af3a0a9 ZL |
139 | #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA |
140 | unsigned long __per_cpu_offset[NR_CPUS] __read_mostly; | |
141 | EXPORT_SYMBOL(__per_cpu_offset); | |
142 | ||
143 | static int __init early_cpu_to_node(int cpu) | |
144 | { | |
145 | return cpu_to_node_map[cpu]; | |
146 | } | |
147 | ||
148 | static int __init pcpu_cpu_distance(unsigned int from, unsigned int to) | |
149 | { | |
150 | return node_distance(from, to); | |
151 | } | |
152 | ||
153 | static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, | |
154 | size_t align) | |
155 | { | |
156 | int nid = early_cpu_to_node(cpu); | |
157 | ||
158 | return memblock_virt_alloc_try_nid(size, align, | |
159 | __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid); | |
160 | } | |
161 | ||
162 | static void __init pcpu_fc_free(void *ptr, size_t size) | |
163 | { | |
164 | memblock_free_early(__pa(ptr), size); | |
165 | } | |
166 | ||
167 | void __init setup_per_cpu_areas(void) | |
168 | { | |
169 | unsigned long delta; | |
170 | unsigned int cpu; | |
171 | int rc; | |
172 | ||
173 | /* | |
174 | * Always reserve area for module percpu variables. That's | |
175 | * what the legacy allocator did. | |
176 | */ | |
177 | rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE, | |
178 | PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, | |
179 | pcpu_cpu_distance, | |
180 | pcpu_fc_alloc, pcpu_fc_free); | |
181 | if (rc < 0) | |
182 | panic("Failed to initialize percpu areas."); | |
183 | ||
184 | delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; | |
185 | for_each_possible_cpu(cpu) | |
186 | __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu]; | |
187 | } | |
188 | #endif | |
189 | ||
1a2db300 GK |
190 | /** |
191 | * numa_add_memblk - Set node id to memblk | |
192 | * @nid: NUMA node ID of the new memblk | |
193 | * @start: Start address of the new memblk | |
8ccbbdaa | 194 | * @end: End address of the new memblk |
1a2db300 GK |
195 | * |
196 | * RETURNS: | |
197 | * 0 on success, -errno on failure. | |
198 | */ | |
8ccbbdaa | 199 | int __init numa_add_memblk(int nid, u64 start, u64 end) |
1a2db300 GK |
200 | { |
201 | int ret; | |
202 | ||
8ccbbdaa | 203 | ret = memblock_set_node(start, (end - start), &memblock.memory, nid); |
1a2db300 | 204 | if (ret < 0) { |
f11c7bac | 205 | pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n", |
8ccbbdaa | 206 | start, (end - 1), nid); |
1a2db300 GK |
207 | return ret; |
208 | } | |
209 | ||
210 | node_set(nid, numa_nodes_parsed); | |
f11c7bac | 211 | pr_info("Adding memblock [0x%llx - 0x%llx] on node %d\n", |
8ccbbdaa | 212 | start, (end - 1), nid); |
1a2db300 GK |
213 | return ret; |
214 | } | |
215 | ||
216 | /** | |
217 | * Initialize NODE_DATA for a node on the local memory | |
218 | */ | |
219 | static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn) | |
220 | { | |
221 | const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES); | |
222 | u64 nd_pa; | |
223 | void *nd; | |
224 | int tnid; | |
225 | ||
f11c7bac KW |
226 | pr_info("Initmem setup node %d [mem %#010Lx-%#010Lx]\n", |
227 | nid, start_pfn << PAGE_SHIFT, (end_pfn << PAGE_SHIFT) - 1); | |
1a2db300 GK |
228 | |
229 | nd_pa = memblock_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid); | |
230 | nd = __va(nd_pa); | |
231 | ||
232 | /* report and initialize */ | |
f11c7bac | 233 | pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n", |
1a2db300 GK |
234 | nd_pa, nd_pa + nd_size - 1); |
235 | tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT); | |
236 | if (tnid != nid) | |
f11c7bac | 237 | pr_info("NODE_DATA(%d) on node %d\n", nid, tnid); |
1a2db300 GK |
238 | |
239 | node_data[nid] = nd; | |
240 | memset(NODE_DATA(nid), 0, sizeof(pg_data_t)); | |
241 | NODE_DATA(nid)->node_id = nid; | |
242 | NODE_DATA(nid)->node_start_pfn = start_pfn; | |
243 | NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn; | |
244 | } | |
245 | ||
246 | /** | |
247 | * numa_free_distance | |
248 | * | |
249 | * The current table is freed. | |
250 | */ | |
251 | void __init numa_free_distance(void) | |
252 | { | |
253 | size_t size; | |
254 | ||
255 | if (!numa_distance) | |
256 | return; | |
257 | ||
258 | size = numa_distance_cnt * numa_distance_cnt * | |
259 | sizeof(numa_distance[0]); | |
260 | ||
261 | memblock_free(__pa(numa_distance), size); | |
262 | numa_distance_cnt = 0; | |
263 | numa_distance = NULL; | |
264 | } | |
265 | ||
266 | /** | |
267 | * | |
268 | * Create a new NUMA distance table. | |
269 | * | |
270 | */ | |
271 | static int __init numa_alloc_distance(void) | |
272 | { | |
273 | size_t size; | |
274 | u64 phys; | |
275 | int i, j; | |
276 | ||
277 | size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]); | |
278 | phys = memblock_find_in_range(0, PFN_PHYS(max_pfn), | |
279 | size, PAGE_SIZE); | |
280 | if (WARN_ON(!phys)) | |
281 | return -ENOMEM; | |
282 | ||
283 | memblock_reserve(phys, size); | |
284 | ||
285 | numa_distance = __va(phys); | |
286 | numa_distance_cnt = nr_node_ids; | |
287 | ||
288 | /* fill with the default distances */ | |
289 | for (i = 0; i < numa_distance_cnt; i++) | |
290 | for (j = 0; j < numa_distance_cnt; j++) | |
291 | numa_distance[i * numa_distance_cnt + j] = i == j ? | |
292 | LOCAL_DISTANCE : REMOTE_DISTANCE; | |
293 | ||
f11c7bac | 294 | pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt); |
1a2db300 GK |
295 | |
296 | return 0; | |
297 | } | |
298 | ||
299 | /** | |
300 | * numa_set_distance - Set inter node NUMA distance from node to node. | |
301 | * @from: the 'from' node to set distance | |
302 | * @to: the 'to' node to set distance | |
303 | * @distance: NUMA distance | |
304 | * | |
305 | * Set the distance from node @from to @to to @distance. | |
306 | * If distance table doesn't exist, a warning is printed. | |
307 | * | |
308 | * If @from or @to is higher than the highest known node or lower than zero | |
309 | * or @distance doesn't make sense, the call is ignored. | |
310 | * | |
311 | */ | |
312 | void __init numa_set_distance(int from, int to, int distance) | |
313 | { | |
314 | if (!numa_distance) { | |
f11c7bac | 315 | pr_warn_once("Warning: distance table not allocated yet\n"); |
1a2db300 GK |
316 | return; |
317 | } | |
318 | ||
319 | if (from >= numa_distance_cnt || to >= numa_distance_cnt || | |
320 | from < 0 || to < 0) { | |
f11c7bac | 321 | pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n", |
1a2db300 GK |
322 | from, to, distance); |
323 | return; | |
324 | } | |
325 | ||
326 | if ((u8)distance != distance || | |
327 | (from == to && distance != LOCAL_DISTANCE)) { | |
f11c7bac | 328 | pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n", |
1a2db300 GK |
329 | from, to, distance); |
330 | return; | |
331 | } | |
332 | ||
333 | numa_distance[from * numa_distance_cnt + to] = distance; | |
334 | } | |
335 | ||
336 | /** | |
337 | * Return NUMA distance @from to @to | |
338 | */ | |
339 | int __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 | } | |
345 | EXPORT_SYMBOL(__node_distance); | |
346 | ||
347 | static 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 */ | |
353 | for_each_memblock(memory, mblk) | |
354 | if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) { | |
f11c7bac | 355 | pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n", |
1a2db300 GK |
356 | mblk->nid, mblk->base, |
357 | mblk->base + mblk->size - 1); | |
358 | return -EINVAL; | |
359 | } | |
360 | ||
361 | /* Finally register nodes. */ | |
362 | for_each_node_mask(nid, numa_nodes_parsed) { | |
363 | unsigned long start_pfn, end_pfn; | |
364 | ||
365 | get_pfn_range_for_nid(nid, &start_pfn, &end_pfn); | |
366 | setup_node_data(nid, start_pfn, end_pfn); | |
367 | node_set_online(nid); | |
368 | } | |
369 | ||
370 | /* Setup online nodes to actual nodes*/ | |
371 | node_possible_map = numa_nodes_parsed; | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
376 | static int __init numa_init(int (*init_func)(void)) | |
377 | { | |
378 | int ret; | |
379 | ||
380 | nodes_clear(numa_nodes_parsed); | |
381 | nodes_clear(node_possible_map); | |
382 | nodes_clear(node_online_map); | |
383 | numa_free_distance(); | |
384 | ||
385 | ret = numa_alloc_distance(); | |
386 | if (ret < 0) | |
387 | return ret; | |
388 | ||
389 | ret = init_func(); | |
390 | if (ret < 0) | |
391 | return ret; | |
392 | ||
794224ea ZL |
393 | if (nodes_empty(numa_nodes_parsed)) { |
394 | pr_info("No NUMA configuration found\n"); | |
1a2db300 | 395 | return -EINVAL; |
794224ea | 396 | } |
1a2db300 GK |
397 | |
398 | ret = numa_register_nodes(); | |
399 | if (ret < 0) | |
400 | return ret; | |
401 | ||
402 | setup_node_to_cpumask_map(); | |
403 | ||
1a2db300 GK |
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 | */ | |
416 | static int __init dummy_numa_init(void) | |
417 | { | |
418 | int ret; | |
419 | struct memblock_region *mblk; | |
420 | ||
34c33370 DD |
421 | if (numa_off) |
422 | pr_info("NUMA disabled\n"); /* Forced off on command line. */ | |
f11c7bac KW |
423 | pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n", |
424 | 0LLU, PFN_PHYS(max_pfn) - 1); | |
1a2db300 GK |
425 | |
426 | for_each_memblock(memory, mblk) { | |
8ccbbdaa | 427 | ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size); |
1a2db300 GK |
428 | if (!ret) |
429 | continue; | |
430 | ||
431 | pr_err("NUMA init failed\n"); | |
432 | return ret; | |
433 | } | |
434 | ||
34c33370 | 435 | numa_off = true; |
1a2db300 GK |
436 | 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 | */ | |
445 | void __init arm64_numa_init(void) | |
446 | { | |
447 | if (!numa_off) { | |
d8b47fca HG |
448 | if (!acpi_disabled && !numa_init(arm64_acpi_numa_init)) |
449 | return; | |
450 | if (acpi_disabled && !numa_init(of_numa_init)) | |
1a2db300 GK |
451 | return; |
452 | } | |
453 | ||
454 | numa_init(dummy_numa_init); | |
455 | } |