drm/mediatek/mtk_disp_rdma: Strip and demote non-conformant kernel-doc header
[linux-block.git] / drivers / base / arch_numa.c
CommitLineData
caab277b 1// SPDX-License-Identifier: GPL-2.0-only
1a2db300
GK
2/*
3 * NUMA support, based on the x86 implementation.
4 *
5 * Copyright (C) 2015 Cavium Inc.
6 * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
1a2db300
GK
7 */
8
f11c7bac
KW
9#define pr_fmt(fmt) "NUMA: " fmt
10
d8b47fca 11#include <linux/acpi.h>
1a2db300
GK
12#include <linux/memblock.h>
13#include <linux/module.h>
14#include <linux/of.h>
15
7af3a0a9 16#include <asm/sections.h>
bfe6c8a8 17
1a2db300
GK
18struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
19EXPORT_SYMBOL(node_data);
20nodemask_t numa_nodes_parsed __initdata;
21static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
22
23static int numa_distance_cnt;
24static u8 *numa_distance;
aec03f89 25bool numa_off;
1a2db300
GK
26
27static __init int numa_parse_early_param(char *opt)
28{
29 if (!opt)
30 return -EINVAL;
b3e089cd 31 if (str_has_prefix(opt, "off"))
34c33370 32 numa_off = true;
f11c7bac 33
1a2db300
GK
34 return 0;
35}
36early_param("numa", numa_parse_early_param);
37
38cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
39EXPORT_SYMBOL(node_to_cpumask_map);
40
41#ifdef CONFIG_DEBUG_PER_CPU_MAPS
42
43/*
44 * Returns a pointer to the bitmask of CPUs on Node 'node'.
45 */
46const struct cpumask *cpumask_of_node(int node)
47{
a194c5f2
ZL
48
49 if (node == NUMA_NO_NODE)
50 return cpu_all_mask;
51
52 if (WARN_ON(node < 0 || node >= nr_node_ids))
1a2db300
GK
53 return cpu_none_mask;
54
55 if (WARN_ON(node_to_cpumask_map[node] == NULL))
56 return cpu_online_mask;
57
58 return node_to_cpumask_map[node];
59}
60EXPORT_SYMBOL(cpumask_of_node);
61
62#endif
63
97fd6016 64static void numa_update_cpu(unsigned int cpu, bool remove)
1a2db300 65{
97fd6016
SH
66 int nid = cpu_to_node(cpu);
67
68 if (nid == NUMA_NO_NODE)
69 return;
70
71 if (remove)
72 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
73 else
1a2db300
GK
74 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
75}
76
97fd6016 77void numa_add_cpu(unsigned int cpu)
1a2db300 78{
97fd6016
SH
79 numa_update_cpu(cpu, false);
80}
1a2db300 81
97fd6016
SH
82void numa_remove_cpu(unsigned int cpu)
83{
84 numa_update_cpu(cpu, true);
85}
86
87void numa_clear_node(unsigned int cpu)
88{
89 numa_remove_cpu(cpu);
1a2db300
GK
90 set_cpu_numa_node(cpu, NUMA_NO_NODE);
91}
92
93/*
94 * Allocate node_to_cpumask_map based on number of available nodes
95 * Requires node_possible_map to be valid.
96 *
97 * Note: cpumask_of_node() is not valid until after this is done.
98 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
99 */
100static void __init setup_node_to_cpumask_map(void)
101{
1a2db300
GK
102 int node;
103
104 /* setup nr_node_ids if not done yet */
105 if (nr_node_ids == MAX_NUMNODES)
106 setup_nr_node_ids();
107
108 /* allocate and clear the mapping */
109 for (node = 0; node < nr_node_ids; node++) {
110 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
111 cpumask_clear(node_to_cpumask_map[node]);
112 }
113
1a2db300 114 /* cpumask_of_node() will now work */
b9726c26 115 pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
1a2db300
GK
116}
117
118/*
b1ce45e8 119 * Set the cpu to node and mem mapping
1a2db300
GK
120 */
121void numa_store_cpu_info(unsigned int cpu)
122{
97fd6016 123 set_cpu_numa_node(cpu, cpu_to_node_map[cpu]);
1a2db300
GK
124}
125
126void __init early_map_cpu_to_node(unsigned int cpu, int nid)
127{
128 /* fallback to node 0 */
7ba5f605 129 if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
1a2db300
GK
130 nid = 0;
131
132 cpu_to_node_map[cpu] = nid;
7ba5f605
ZL
133
134 /*
135 * We should set the numa node of cpu0 as soon as possible, because it
136 * has already been set up online before. cpu_to_node(0) will soon be
137 * called.
138 */
139 if (!cpu)
140 set_cpu_numa_node(cpu, nid);
1a2db300
GK
141}
142
7af3a0a9
ZL
143#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
144unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
145EXPORT_SYMBOL(__per_cpu_offset);
146
147static int __init early_cpu_to_node(int cpu)
148{
149 return cpu_to_node_map[cpu];
150}
151
152static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
153{
26984c3b 154 return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
7af3a0a9
ZL
155}
156
157static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
158 size_t align)
159{
160 int nid = early_cpu_to_node(cpu);
161
eb31d559 162 return memblock_alloc_try_nid(size, align,
7af3a0a9
ZL
163 __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
164}
165
166static void __init pcpu_fc_free(void *ptr, size_t size)
167{
168 memblock_free_early(__pa(ptr), size);
169}
170
171void __init setup_per_cpu_areas(void)
172{
173 unsigned long delta;
174 unsigned int cpu;
175 int rc;
176
177 /*
178 * Always reserve area for module percpu variables. That's
179 * what the legacy allocator did.
180 */
181 rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
182 PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
183 pcpu_cpu_distance,
184 pcpu_fc_alloc, pcpu_fc_free);
185 if (rc < 0)
186 panic("Failed to initialize percpu areas.");
187
188 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
189 for_each_possible_cpu(cpu)
190 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
191}
192#endif
193
1a2db300 194/**
b1ce45e8 195 * numa_add_memblk() - Set node id to memblk
1a2db300
GK
196 * @nid: NUMA node ID of the new memblk
197 * @start: Start address of the new memblk
8ccbbdaa 198 * @end: End address of the new memblk
1a2db300
GK
199 *
200 * RETURNS:
201 * 0 on success, -errno on failure.
202 */
8ccbbdaa 203int __init numa_add_memblk(int nid, u64 start, u64 end)
1a2db300
GK
204{
205 int ret;
206
8ccbbdaa 207 ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
1a2db300 208 if (ret < 0) {
f11c7bac 209 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
8ccbbdaa 210 start, (end - 1), nid);
1a2db300
GK
211 return ret;
212 }
213
214 node_set(nid, numa_nodes_parsed);
1a2db300
GK
215 return ret;
216}
217
b1ce45e8 218/*
1a2db300
GK
219 * Initialize NODE_DATA for a node on the local memory
220 */
221static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
222{
223 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
224 u64 nd_pa;
225 void *nd;
226 int tnid;
227
ece4b206 228 if (start_pfn >= end_pfn)
3f7a09f4 229 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
1a2db300 230
9a8dd708 231 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
33755574
MR
232 if (!nd_pa)
233 panic("Cannot allocate %zu bytes for node %d data\n",
234 nd_size, nid);
235
1a2db300
GK
236 nd = __va(nd_pa);
237
238 /* report and initialize */
f11c7bac 239 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
1a2db300
GK
240 nd_pa, nd_pa + nd_size - 1);
241 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
242 if (tnid != nid)
f11c7bac 243 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
1a2db300
GK
244
245 node_data[nid] = nd;
246 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
247 NODE_DATA(nid)->node_id = nid;
248 NODE_DATA(nid)->node_start_pfn = start_pfn;
249 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
250}
251
b1ce45e8 252/*
1a2db300
GK
253 * numa_free_distance
254 *
255 * The current table is freed.
256 */
257void __init numa_free_distance(void)
258{
259 size_t size;
260
261 if (!numa_distance)
262 return;
263
264 size = numa_distance_cnt * numa_distance_cnt *
265 sizeof(numa_distance[0]);
266
267 memblock_free(__pa(numa_distance), size);
268 numa_distance_cnt = 0;
269 numa_distance = NULL;
270}
271
b1ce45e8 272/*
1a2db300 273 * Create a new NUMA distance table.
1a2db300
GK
274 */
275static int __init numa_alloc_distance(void)
276{
277 size_t size;
278 u64 phys;
279 int i, j;
280
281 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
282 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
283 size, PAGE_SIZE);
284 if (WARN_ON(!phys))
285 return -ENOMEM;
286
287 memblock_reserve(phys, size);
288
289 numa_distance = __va(phys);
290 numa_distance_cnt = nr_node_ids;
291
292 /* fill with the default distances */
293 for (i = 0; i < numa_distance_cnt; i++)
294 for (j = 0; j < numa_distance_cnt; j++)
295 numa_distance[i * numa_distance_cnt + j] = i == j ?
296 LOCAL_DISTANCE : REMOTE_DISTANCE;
297
f11c7bac 298 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
1a2db300
GK
299
300 return 0;
301}
302
303/**
b1ce45e8 304 * numa_set_distance() - Set inter node NUMA distance from node to node.
1a2db300
GK
305 * @from: the 'from' node to set distance
306 * @to: the 'to' node to set distance
307 * @distance: NUMA distance
308 *
309 * Set the distance from node @from to @to to @distance.
310 * If distance table doesn't exist, a warning is printed.
311 *
312 * If @from or @to is higher than the highest known node or lower than zero
313 * or @distance doesn't make sense, the call is ignored.
1a2db300
GK
314 */
315void __init numa_set_distance(int from, int to, int distance)
316{
317 if (!numa_distance) {
f11c7bac 318 pr_warn_once("Warning: distance table not allocated yet\n");
1a2db300
GK
319 return;
320 }
321
322 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
323 from < 0 || to < 0) {
f11c7bac 324 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
1a2db300
GK
325 from, to, distance);
326 return;
327 }
328
329 if ((u8)distance != distance ||
330 (from == to && distance != LOCAL_DISTANCE)) {
f11c7bac 331 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
1a2db300
GK
332 from, to, distance);
333 return;
334 }
335
336 numa_distance[from * numa_distance_cnt + to] = distance;
337}
338
b1ce45e8 339/*
1a2db300
GK
340 * Return NUMA distance @from to @to
341 */
342int __node_distance(int from, int to)
343{
344 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
345 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
346 return numa_distance[from * numa_distance_cnt + to];
347}
348EXPORT_SYMBOL(__node_distance);
349
350static int __init numa_register_nodes(void)
351{
352 int nid;
353 struct memblock_region *mblk;
354
355 /* Check that valid nid is set to memblks */
cc6de168 356 for_each_mem_region(mblk) {
d622abf7 357 int mblk_nid = memblock_get_region_node(mblk);
4cd48bb3
RD
358 phys_addr_t start = mblk->base;
359 phys_addr_t end = mblk->base + mblk->size - 1;
d622abf7
MR
360
361 if (mblk_nid == NUMA_NO_NODE || mblk_nid >= MAX_NUMNODES) {
4cd48bb3
RD
362 pr_warn("Warning: invalid memblk node %d [mem %pap-%pap]\n",
363 mblk_nid, &start, &end);
1a2db300
GK
364 return -EINVAL;
365 }
d622abf7 366 }
1a2db300
GK
367
368 /* Finally register nodes. */
369 for_each_node_mask(nid, numa_nodes_parsed) {
370 unsigned long start_pfn, end_pfn;
371
372 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
373 setup_node_data(nid, start_pfn, end_pfn);
374 node_set_online(nid);
375 }
376
377 /* Setup online nodes to actual nodes*/
378 node_possible_map = numa_nodes_parsed;
379
380 return 0;
381}
382
383static int __init numa_init(int (*init_func)(void))
384{
385 int ret;
386
387 nodes_clear(numa_nodes_parsed);
388 nodes_clear(node_possible_map);
389 nodes_clear(node_online_map);
1a2db300
GK
390
391 ret = numa_alloc_distance();
392 if (ret < 0)
393 return ret;
394
395 ret = init_func();
396 if (ret < 0)
52338088 397 goto out_free_distance;
1a2db300 398
794224ea
ZL
399 if (nodes_empty(numa_nodes_parsed)) {
400 pr_info("No NUMA configuration found\n");
52338088
AK
401 ret = -EINVAL;
402 goto out_free_distance;
794224ea 403 }
1a2db300
GK
404
405 ret = numa_register_nodes();
406 if (ret < 0)
52338088 407 goto out_free_distance;
1a2db300
GK
408
409 setup_node_to_cpumask_map();
410
1a2db300 411 return 0;
52338088
AK
412out_free_distance:
413 numa_free_distance();
414 return ret;
1a2db300
GK
415}
416
417/**
b1ce45e8 418 * dummy_numa_init() - Fallback dummy NUMA init
1a2db300
GK
419 *
420 * Used if there's no underlying NUMA architecture, NUMA initialization
421 * fails, or NUMA is disabled on the command line.
422 *
423 * Must online at least one node (node 0) and add memory blocks that cover all
424 * allowed memory. It is unlikely that this function fails.
b1ce45e8
QC
425 *
426 * Return: 0 on success, -errno on failure.
1a2db300
GK
427 */
428static int __init dummy_numa_init(void)
429{
ab8f21aa 430 phys_addr_t start = memblock_start_of_DRAM();
4cd48bb3 431 phys_addr_t end = memblock_end_of_DRAM() - 1;
1a2db300 432 int ret;
1a2db300 433
34c33370
DD
434 if (numa_off)
435 pr_info("NUMA disabled\n"); /* Forced off on command line. */
4cd48bb3 436 pr_info("Faking a node at [mem %pap-%pap]\n", &start, &end);
1a2db300 437
4cd48bb3 438 ret = numa_add_memblk(0, start, end + 1);
ab8f21aa 439 if (ret) {
1a2db300
GK
440 pr_err("NUMA init failed\n");
441 return ret;
442 }
443
34c33370 444 numa_off = true;
1a2db300
GK
445 return 0;
446}
447
eb75541f
AP
448#ifdef CONFIG_ACPI_NUMA
449static int __init arch_acpi_numa_init(void)
450{
451 int ret;
452
453 ret = acpi_numa_init();
454 if (ret) {
455 pr_info("Failed to initialise from firmware\n");
456 return ret;
457 }
458
459 return srat_disabled() ? -EINVAL : 0;
460}
461#else
462static int __init arch_acpi_numa_init(void)
463{
464 return -EOPNOTSUPP;
465}
466#endif
467
1a2db300 468/**
eb75541f 469 * arch_numa_init() - Initialize NUMA
1a2db300 470 *
b1ce45e8 471 * Try each configured NUMA initialization method until one succeeds. The
9a747c91 472 * last fallback is dummy single node config encompassing whole memory.
1a2db300 473 */
eb75541f 474void __init arch_numa_init(void)
1a2db300
GK
475{
476 if (!numa_off) {
eb75541f 477 if (!acpi_disabled && !numa_init(arch_acpi_numa_init))
d8b47fca
HG
478 return;
479 if (acpi_disabled && !numa_init(of_numa_init))
1a2db300
GK
480 return;
481 }
482
483 numa_init(dummy_numa_init);
484}