GNU Linux-libre 4.9.337-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 static 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 map_cpu_to_node(unsigned int cpu, int nid)
78 {
79         set_cpu_numa_node(cpu, nid);
80         if (nid >= 0)
81                 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
82 }
83
84 void numa_clear_node(unsigned int cpu)
85 {
86         int nid = cpu_to_node(cpu);
87
88         if (nid >= 0)
89                 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
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  */
100 static void __init setup_node_to_cpumask_map(void)
101 {
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
114         /* cpumask_of_node() will now work */
115         pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids);
116 }
117
118 /*
119  *  Set the cpu to node and mem mapping
120  */
121 void numa_store_cpu_info(unsigned int cpu)
122 {
123         map_cpu_to_node(cpu, cpu_to_node_map[cpu]);
124 }
125
126 void __init early_map_cpu_to_node(unsigned int cpu, int nid)
127 {
128         /* fallback to node 0 */
129         if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
130                 nid = 0;
131
132         cpu_to_node_map[cpu] = nid;
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);
141 }
142
143 #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
144 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
145 EXPORT_SYMBOL(__per_cpu_offset);
146
147 static int __init early_cpu_to_node(int cpu)
148 {
149         return cpu_to_node_map[cpu];
150 }
151
152 static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
153 {
154         return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
155 }
156
157 static 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
162         return  memblock_virt_alloc_try_nid(size, align,
163                         __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
164 }
165
166 static void __init pcpu_fc_free(void *ptr, size_t size)
167 {
168         memblock_free_early(__pa(ptr), size);
169 }
170
171 void __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
194 /**
195  * numa_add_memblk - Set node id to memblk
196  * @nid: NUMA node ID of the new memblk
197  * @start: Start address of the new memblk
198  * @end:  End address of the new memblk
199  *
200  * RETURNS:
201  * 0 on success, -errno on failure.
202  */
203 int __init numa_add_memblk(int nid, u64 start, u64 end)
204 {
205         int ret;
206
207         ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
208         if (ret < 0) {
209                 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
210                         start, (end - 1), nid);
211                 return ret;
212         }
213
214         node_set(nid, numa_nodes_parsed);
215         pr_info("Adding memblock [0x%llx - 0x%llx] on node %d\n",
216                         start, (end - 1), nid);
217         return ret;
218 }
219
220 /**
221  * Initialize NODE_DATA for a node on the local memory
222  */
223 static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
224 {
225         const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
226         u64 nd_pa;
227         void *nd;
228         int tnid;
229
230         if (start_pfn < end_pfn)
231                 pr_info("Initmem setup node %d [mem %#010Lx-%#010Lx]\n", nid,
232                         start_pfn << PAGE_SHIFT, (end_pfn << PAGE_SHIFT) - 1);
233         else
234                 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
235
236         nd_pa = memblock_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
237         nd = __va(nd_pa);
238
239         /* report and initialize */
240         pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
241                 nd_pa, nd_pa + nd_size - 1);
242         tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
243         if (tnid != nid)
244                 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
245
246         node_data[nid] = nd;
247         memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
248         NODE_DATA(nid)->node_id = nid;
249         NODE_DATA(nid)->node_start_pfn = start_pfn;
250         NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
251 }
252
253 /**
254  * numa_free_distance
255  *
256  * The current table is freed.
257  */
258 void __init numa_free_distance(void)
259 {
260         size_t size;
261
262         if (!numa_distance)
263                 return;
264
265         size = numa_distance_cnt * numa_distance_cnt *
266                 sizeof(numa_distance[0]);
267
268         memblock_free(__pa(numa_distance), size);
269         numa_distance_cnt = 0;
270         numa_distance = NULL;
271 }
272
273 /**
274  *
275  * Create a new NUMA distance table.
276  *
277  */
278 static int __init numa_alloc_distance(void)
279 {
280         size_t size;
281         u64 phys;
282         int i, j;
283
284         size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
285         phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
286                                       size, PAGE_SIZE);
287         if (WARN_ON(!phys))
288                 return -ENOMEM;
289
290         memblock_reserve(phys, size);
291
292         numa_distance = __va(phys);
293         numa_distance_cnt = nr_node_ids;
294
295         /* fill with the default distances */
296         for (i = 0; i < numa_distance_cnt; i++)
297                 for (j = 0; j < numa_distance_cnt; j++)
298                         numa_distance[i * numa_distance_cnt + j] = i == j ?
299                                 LOCAL_DISTANCE : REMOTE_DISTANCE;
300
301         pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
302
303         return 0;
304 }
305
306 /**
307  * numa_set_distance - Set inter node NUMA distance from node to node.
308  * @from: the 'from' node to set distance
309  * @to: the 'to'  node to set distance
310  * @distance: NUMA distance
311  *
312  * Set the distance from node @from to @to to @distance.
313  * If distance table doesn't exist, a warning is printed.
314  *
315  * If @from or @to is higher than the highest known node or lower than zero
316  * or @distance doesn't make sense, the call is ignored.
317  *
318  */
319 void __init numa_set_distance(int from, int to, int distance)
320 {
321         if (!numa_distance) {
322                 pr_warn_once("Warning: distance table not allocated yet\n");
323                 return;
324         }
325
326         if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
327                         from < 0 || to < 0) {
328                 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
329                             from, to, distance);
330                 return;
331         }
332
333         if ((u8)distance != distance ||
334             (from == to && distance != LOCAL_DISTANCE)) {
335                 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
336                              from, to, distance);
337                 return;
338         }
339
340         numa_distance[from * numa_distance_cnt + to] = distance;
341 }
342
343 /**
344  * Return NUMA distance @from to @to
345  */
346 int __node_distance(int from, int to)
347 {
348         if (from >= numa_distance_cnt || to >= numa_distance_cnt)
349                 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
350         return numa_distance[from * numa_distance_cnt + to];
351 }
352 EXPORT_SYMBOL(__node_distance);
353
354 static int __init numa_register_nodes(void)
355 {
356         int nid;
357         struct memblock_region *mblk;
358
359         /* Check that valid nid is set to memblks */
360         for_each_memblock(memory, mblk)
361                 if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
362                         pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
363                                 mblk->nid, mblk->base,
364                                 mblk->base + mblk->size - 1);
365                         return -EINVAL;
366                 }
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
383 static 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);
390         numa_free_distance();
391
392         ret = numa_alloc_distance();
393         if (ret < 0)
394                 return ret;
395
396         ret = init_func();
397         if (ret < 0)
398                 return ret;
399
400         if (nodes_empty(numa_nodes_parsed)) {
401                 pr_info("No NUMA configuration found\n");
402                 return -EINVAL;
403         }
404
405         ret = numa_register_nodes();
406         if (ret < 0)
407                 return ret;
408
409         setup_node_to_cpumask_map();
410
411         return 0;
412 }
413
414 /**
415  * dummy_numa_init - Fallback dummy NUMA init
416  *
417  * Used if there's no underlying NUMA architecture, NUMA initialization
418  * fails, or NUMA is disabled on the command line.
419  *
420  * Must online at least one node (node 0) and add memory blocks that cover all
421  * allowed memory. It is unlikely that this function fails.
422  */
423 static int __init dummy_numa_init(void)
424 {
425         int ret;
426         struct memblock_region *mblk;
427
428         if (numa_off)
429                 pr_info("NUMA disabled\n"); /* Forced off on command line. */
430         pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
431                 memblock_start_of_DRAM(), memblock_end_of_DRAM() - 1);
432
433         for_each_memblock(memory, mblk) {
434                 ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
435                 if (!ret)
436                         continue;
437
438                 pr_err("NUMA init failed\n");
439                 return ret;
440         }
441
442         numa_off = true;
443         return 0;
444 }
445
446 /**
447  * arm64_numa_init - Initialize NUMA
448  *
449  * Try each configured NUMA initialization method until one succeeds.  The
450  * last fallback is dummy single node config encomapssing whole memory.
451  */
452 void __init arm64_numa_init(void)
453 {
454         if (!numa_off) {
455                 if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
456                         return;
457                 if (acpi_disabled && !numa_init(of_numa_init))
458                         return;
459         }
460
461         numa_init(dummy_numa_init);
462 }