GNU Linux-libre 4.19.207-gnu1
[releases.git] / arch / arm64 / mm / numa.c
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
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
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;
38 bool numa_off;
39
40 static __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 }
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
62         if (node == NUMA_NO_NODE)
63                 return cpu_all_mask;
64
65         if (WARN_ON(node < 0 || node >= nr_node_ids))
66                 return cpu_none_mask;
67
68         if (WARN_ON(node_to_cpumask_map[node] == NULL))
69                 return cpu_online_mask;
70
71         return node_to_cpumask_map[node];
72 }
73 EXPORT_SYMBOL(cpumask_of_node);
74
75 #endif
76
77 static void numa_update_cpu(unsigned int cpu, bool remove)
78 {
79         int nid = cpu_to_node(cpu);
80
81         if (nid == NUMA_NO_NODE)
82                 return;
83
84         if (remove)
85                 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
86         else
87                 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
88 }
89
90 void numa_add_cpu(unsigned int cpu)
91 {
92         numa_update_cpu(cpu, false);
93 }
94
95 void numa_remove_cpu(unsigned int cpu)
96 {
97         numa_update_cpu(cpu, true);
98 }
99
100 void numa_clear_node(unsigned int cpu)
101 {
102         numa_remove_cpu(cpu);
103         set_cpu_numa_node(cpu, NUMA_NO_NODE);
104 }
105
106 /*
107  * Allocate node_to_cpumask_map based on number of available nodes
108  * Requires node_possible_map to be valid.
109  *
110  * Note: cpumask_of_node() is not valid until after this is done.
111  * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
112  */
113 static void __init setup_node_to_cpumask_map(void)
114 {
115         int node;
116
117         /* setup nr_node_ids if not done yet */
118         if (nr_node_ids == MAX_NUMNODES)
119                 setup_nr_node_ids();
120
121         /* allocate and clear the mapping */
122         for (node = 0; node < nr_node_ids; node++) {
123                 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
124                 cpumask_clear(node_to_cpumask_map[node]);
125         }
126
127         /* cpumask_of_node() will now work */
128         pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids);
129 }
130
131 /*
132  *  Set the cpu to node and mem mapping
133  */
134 void numa_store_cpu_info(unsigned int cpu)
135 {
136         set_cpu_numa_node(cpu, cpu_to_node_map[cpu]);
137 }
138
139 void __init early_map_cpu_to_node(unsigned int cpu, int nid)
140 {
141         /* fallback to node 0 */
142         if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
143                 nid = 0;
144
145         cpu_to_node_map[cpu] = nid;
146
147         /*
148          * We should set the numa node of cpu0 as soon as possible, because it
149          * has already been set up online before. cpu_to_node(0) will soon be
150          * called.
151          */
152         if (!cpu)
153                 set_cpu_numa_node(cpu, nid);
154 }
155
156 #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
157 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
158 EXPORT_SYMBOL(__per_cpu_offset);
159
160 static int __init early_cpu_to_node(int cpu)
161 {
162         return cpu_to_node_map[cpu];
163 }
164
165 static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
166 {
167         return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
168 }
169
170 static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
171                                        size_t align)
172 {
173         int nid = early_cpu_to_node(cpu);
174
175         return  memblock_virt_alloc_try_nid(size, align,
176                         __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
177 }
178
179 static void __init pcpu_fc_free(void *ptr, size_t size)
180 {
181         memblock_free_early(__pa(ptr), size);
182 }
183
184 void __init setup_per_cpu_areas(void)
185 {
186         unsigned long delta;
187         unsigned int cpu;
188         int rc;
189
190         /*
191          * Always reserve area for module percpu variables.  That's
192          * what the legacy allocator did.
193          */
194         rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
195                                     PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
196                                     pcpu_cpu_distance,
197                                     pcpu_fc_alloc, pcpu_fc_free);
198         if (rc < 0)
199                 panic("Failed to initialize percpu areas.");
200
201         delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
202         for_each_possible_cpu(cpu)
203                 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
204 }
205 #endif
206
207 /**
208  * numa_add_memblk - Set node id to memblk
209  * @nid: NUMA node ID of the new memblk
210  * @start: Start address of the new memblk
211  * @end:  End address of the new memblk
212  *
213  * RETURNS:
214  * 0 on success, -errno on failure.
215  */
216 int __init numa_add_memblk(int nid, u64 start, u64 end)
217 {
218         int ret;
219
220         ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
221         if (ret < 0) {
222                 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
223                         start, (end - 1), nid);
224                 return ret;
225         }
226
227         node_set(nid, numa_nodes_parsed);
228         return ret;
229 }
230
231 /**
232  * Initialize NODE_DATA for a node on the local memory
233  */
234 static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
235 {
236         const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
237         u64 nd_pa;
238         void *nd;
239         int tnid;
240
241         if (start_pfn >= end_pfn)
242                 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
243
244         nd_pa = memblock_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
245         nd = __va(nd_pa);
246
247         /* report and initialize */
248         pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
249                 nd_pa, nd_pa + nd_size - 1);
250         tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
251         if (tnid != nid)
252                 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
253
254         node_data[nid] = nd;
255         memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
256         NODE_DATA(nid)->node_id = nid;
257         NODE_DATA(nid)->node_start_pfn = start_pfn;
258         NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
259 }
260
261 /**
262  * numa_free_distance
263  *
264  * The current table is freed.
265  */
266 void __init numa_free_distance(void)
267 {
268         size_t size;
269
270         if (!numa_distance)
271                 return;
272
273         size = numa_distance_cnt * numa_distance_cnt *
274                 sizeof(numa_distance[0]);
275
276         memblock_free(__pa(numa_distance), size);
277         numa_distance_cnt = 0;
278         numa_distance = NULL;
279 }
280
281 /**
282  *
283  * Create a new NUMA distance table.
284  *
285  */
286 static int __init numa_alloc_distance(void)
287 {
288         size_t size;
289         u64 phys;
290         int i, j;
291
292         size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
293         phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
294                                       size, PAGE_SIZE);
295         if (WARN_ON(!phys))
296                 return -ENOMEM;
297
298         memblock_reserve(phys, size);
299
300         numa_distance = __va(phys);
301         numa_distance_cnt = nr_node_ids;
302
303         /* fill with the default distances */
304         for (i = 0; i < numa_distance_cnt; i++)
305                 for (j = 0; j < numa_distance_cnt; j++)
306                         numa_distance[i * numa_distance_cnt + j] = i == j ?
307                                 LOCAL_DISTANCE : REMOTE_DISTANCE;
308
309         pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
310
311         return 0;
312 }
313
314 /**
315  * numa_set_distance - Set inter node NUMA distance from node to node.
316  * @from: the 'from' node to set distance
317  * @to: the 'to'  node to set distance
318  * @distance: NUMA distance
319  *
320  * Set the distance from node @from to @to to @distance.
321  * If distance table doesn't exist, a warning is printed.
322  *
323  * If @from or @to is higher than the highest known node or lower than zero
324  * or @distance doesn't make sense, the call is ignored.
325  *
326  */
327 void __init numa_set_distance(int from, int to, int distance)
328 {
329         if (!numa_distance) {
330                 pr_warn_once("Warning: distance table not allocated yet\n");
331                 return;
332         }
333
334         if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
335                         from < 0 || to < 0) {
336                 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
337                             from, to, distance);
338                 return;
339         }
340
341         if ((u8)distance != distance ||
342             (from == to && distance != LOCAL_DISTANCE)) {
343                 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
344                              from, to, distance);
345                 return;
346         }
347
348         numa_distance[from * numa_distance_cnt + to] = distance;
349 }
350
351 /**
352  * Return NUMA distance @from to @to
353  */
354 int __node_distance(int from, int to)
355 {
356         if (from >= numa_distance_cnt || to >= numa_distance_cnt)
357                 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
358         return numa_distance[from * numa_distance_cnt + to];
359 }
360 EXPORT_SYMBOL(__node_distance);
361
362 static int __init numa_register_nodes(void)
363 {
364         int nid;
365         struct memblock_region *mblk;
366
367         /* Check that valid nid is set to memblks */
368         for_each_memblock(memory, mblk)
369                 if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
370                         pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
371                                 mblk->nid, mblk->base,
372                                 mblk->base + mblk->size - 1);
373                         return -EINVAL;
374                 }
375
376         /* Finally register nodes. */
377         for_each_node_mask(nid, numa_nodes_parsed) {
378                 unsigned long start_pfn, end_pfn;
379
380                 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
381                 setup_node_data(nid, start_pfn, end_pfn);
382                 node_set_online(nid);
383         }
384
385         /* Setup online nodes to actual nodes*/
386         node_possible_map = numa_nodes_parsed;
387
388         return 0;
389 }
390
391 static int __init numa_init(int (*init_func)(void))
392 {
393         int ret;
394
395         nodes_clear(numa_nodes_parsed);
396         nodes_clear(node_possible_map);
397         nodes_clear(node_online_map);
398         numa_free_distance();
399
400         ret = numa_alloc_distance();
401         if (ret < 0)
402                 return ret;
403
404         ret = init_func();
405         if (ret < 0)
406                 return ret;
407
408         if (nodes_empty(numa_nodes_parsed)) {
409                 pr_info("No NUMA configuration found\n");
410                 return -EINVAL;
411         }
412
413         ret = numa_register_nodes();
414         if (ret < 0)
415                 return ret;
416
417         setup_node_to_cpumask_map();
418
419         return 0;
420 }
421
422 /**
423  * dummy_numa_init - Fallback dummy NUMA init
424  *
425  * Used if there's no underlying NUMA architecture, NUMA initialization
426  * fails, or NUMA is disabled on the command line.
427  *
428  * Must online at least one node (node 0) and add memory blocks that cover all
429  * allowed memory. It is unlikely that this function fails.
430  */
431 static int __init dummy_numa_init(void)
432 {
433         int ret;
434         struct memblock_region *mblk;
435
436         if (numa_off)
437                 pr_info("NUMA disabled\n"); /* Forced off on command line. */
438         pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
439                 memblock_start_of_DRAM(), memblock_end_of_DRAM() - 1);
440
441         for_each_memblock(memory, mblk) {
442                 ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
443                 if (!ret)
444                         continue;
445
446                 pr_err("NUMA init failed\n");
447                 return ret;
448         }
449
450         numa_off = true;
451         return 0;
452 }
453
454 /**
455  * arm64_numa_init - Initialize NUMA
456  *
457  * Try each configured NUMA initialization method until one succeeds.  The
458  * last fallback is dummy single node config encomapssing whole memory.
459  */
460 void __init arm64_numa_init(void)
461 {
462         if (!numa_off) {
463                 if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
464                         return;
465                 if (acpi_disabled && !numa_init(of_numa_init))
466                         return;
467         }
468
469         numa_init(dummy_numa_init);
470 }