1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define pr_fmt(fmt) "NUMA: " fmt
21
22#include <linux/acpi.h>
23#include <linux/bootmem.h>
24#include <linux/memblock.h>
25#include <linux/module.h>
26#include <linux/of.h>
27
28#include <asm/acpi.h>
29#include <asm/sections.h>
30
31struct 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;
38bool numa_off;
39
40static __init int numa_parse_early_param(char *opt)
41{
42 if (!opt)
43 return -EINVAL;
44 if (!strncmp(opt, "off", 3))
45 numa_off = true;
46
47 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
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
91
92
93
94
95
96static void __init setup_node_to_cpumask_map(void)
97{
98 int node;
99
100
101 if (nr_node_ids == MAX_NUMNODES)
102 setup_nr_node_ids();
103
104
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
111 pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids);
112}
113
114
115
116
117void numa_store_cpu_info(unsigned int cpu)
118{
119 map_cpu_to_node(cpu, cpu_to_node_map[cpu]);
120}
121
122void __init early_map_cpu_to_node(unsigned int cpu, int nid)
123{
124
125 if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
126 nid = 0;
127
128 cpu_to_node_map[cpu] = nid;
129
130
131
132
133
134
135 if (!cpu)
136 set_cpu_numa_node(cpu, nid);
137}
138
139#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
140unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
141EXPORT_SYMBOL(__per_cpu_offset);
142
143static int __init early_cpu_to_node(int cpu)
144{
145 return cpu_to_node_map[cpu];
146}
147
148static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
149{
150 return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
151}
152
153static 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
162static void __init pcpu_fc_free(void *ptr, size_t size)
163{
164 memblock_free_early(__pa(ptr), size);
165}
166
167void __init setup_per_cpu_areas(void)
168{
169 unsigned long delta;
170 unsigned int cpu;
171 int rc;
172
173
174
175
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
190
191
192
193
194
195
196
197
198
199int __init numa_add_memblk(int nid, u64 start, u64 end)
200{
201 int ret;
202
203 ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
204 if (ret < 0) {
205 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
206 start, (end - 1), nid);
207 return ret;
208 }
209
210 node_set(nid, numa_nodes_parsed);
211 return ret;
212}
213
214
215
216
217static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
218{
219 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
220 u64 nd_pa;
221 void *nd;
222 int tnid;
223
224 if (start_pfn >= end_pfn)
225 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
226
227 nd_pa = memblock_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
228 nd = __va(nd_pa);
229
230
231 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
232 nd_pa, nd_pa + nd_size - 1);
233 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
234 if (tnid != nid)
235 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
236
237 node_data[nid] = nd;
238 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
239 NODE_DATA(nid)->node_id = nid;
240 NODE_DATA(nid)->node_start_pfn = start_pfn;
241 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
242}
243
244
245
246
247
248
249void __init numa_free_distance(void)
250{
251 size_t size;
252
253 if (!numa_distance)
254 return;
255
256 size = numa_distance_cnt * numa_distance_cnt *
257 sizeof(numa_distance[0]);
258
259 memblock_free(__pa(numa_distance), size);
260 numa_distance_cnt = 0;
261 numa_distance = NULL;
262}
263
264
265
266
267
268
269static int __init numa_alloc_distance(void)
270{
271 size_t size;
272 u64 phys;
273 int i, j;
274
275 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
276 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
277 size, PAGE_SIZE);
278 if (WARN_ON(!phys))
279 return -ENOMEM;
280
281 memblock_reserve(phys, size);
282
283 numa_distance = __va(phys);
284 numa_distance_cnt = nr_node_ids;
285
286
287 for (i = 0; i < numa_distance_cnt; i++)
288 for (j = 0; j < numa_distance_cnt; j++)
289 numa_distance[i * numa_distance_cnt + j] = i == j ?
290 LOCAL_DISTANCE : REMOTE_DISTANCE;
291
292 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
293
294 return 0;
295}
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310void __init numa_set_distance(int from, int to, int distance)
311{
312 if (!numa_distance) {
313 pr_warn_once("Warning: distance table not allocated yet\n");
314 return;
315 }
316
317 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
318 from < 0 || to < 0) {
319 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
320 from, to, distance);
321 return;
322 }
323
324 if ((u8)distance != distance ||
325 (from == to && distance != LOCAL_DISTANCE)) {
326 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
327 from, to, distance);
328 return;
329 }
330
331 numa_distance[from * numa_distance_cnt + to] = distance;
332}
333
334
335
336
337int __node_distance(int from, int to)
338{
339 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
340 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
341 return numa_distance[from * numa_distance_cnt + to];
342}
343EXPORT_SYMBOL(__node_distance);
344
345static int __init numa_register_nodes(void)
346{
347 int nid;
348 struct memblock_region *mblk;
349
350
351 for_each_memblock(memory, mblk)
352 if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
353 pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
354 mblk->nid, mblk->base,
355 mblk->base + mblk->size - 1);
356 return -EINVAL;
357 }
358
359
360 for_each_node_mask(nid, numa_nodes_parsed) {
361 unsigned long start_pfn, end_pfn;
362
363 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
364 setup_node_data(nid, start_pfn, end_pfn);
365 node_set_online(nid);
366 }
367
368
369 node_possible_map = numa_nodes_parsed;
370
371 return 0;
372}
373
374static int __init numa_init(int (*init_func)(void))
375{
376 int ret;
377
378 nodes_clear(numa_nodes_parsed);
379 nodes_clear(node_possible_map);
380 nodes_clear(node_online_map);
381 numa_free_distance();
382
383 ret = numa_alloc_distance();
384 if (ret < 0)
385 return ret;
386
387 ret = init_func();
388 if (ret < 0)
389 return ret;
390
391 if (nodes_empty(numa_nodes_parsed)) {
392 pr_info("No NUMA configuration found\n");
393 return -EINVAL;
394 }
395
396 ret = numa_register_nodes();
397 if (ret < 0)
398 return ret;
399
400 setup_node_to_cpumask_map();
401
402 return 0;
403}
404
405
406
407
408
409
410
411
412
413
414static int __init dummy_numa_init(void)
415{
416 int ret;
417 struct memblock_region *mblk;
418
419 if (numa_off)
420 pr_info("NUMA disabled\n");
421 pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
422 0LLU, PFN_PHYS(max_pfn) - 1);
423
424 for_each_memblock(memory, mblk) {
425 ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
426 if (!ret)
427 continue;
428
429 pr_err("NUMA init failed\n");
430 return ret;
431 }
432
433 numa_off = true;
434 return 0;
435}
436
437
438
439
440
441
442
443void __init arm64_numa_init(void)
444{
445 if (!numa_off) {
446 if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
447 return;
448 if (acpi_disabled && !numa_init(of_numa_init))
449 return;
450 }
451
452 numa_init(dummy_numa_init);
453}
454