staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / arch / arm64 / mm / numa.c
blob4f241cc7cc3b1262dfe0d083e3dc48468bd62c11
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * NUMA support, based on the x86 implementation.
5 * Copyright (C) 2015 Cavium Inc.
6 * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
7 */
9 #define pr_fmt(fmt) "NUMA: " fmt
11 #include <linux/acpi.h>
12 #include <linux/memblock.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
16 #include <asm/acpi.h>
17 #include <asm/sections.h>
19 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
20 EXPORT_SYMBOL(node_data);
21 nodemask_t numa_nodes_parsed __initdata;
22 static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
24 static int numa_distance_cnt;
25 static u8 *numa_distance;
26 bool numa_off;
28 static __init int numa_parse_early_param(char *opt)
30 if (!opt)
31 return -EINVAL;
32 if (!strncmp(opt, "off", 3))
33 numa_off = true;
35 return 0;
37 early_param("numa", numa_parse_early_param);
39 cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
40 EXPORT_SYMBOL(node_to_cpumask_map);
42 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
45 * Returns a pointer to the bitmask of CPUs on Node 'node'.
47 const struct cpumask *cpumask_of_node(int node)
49 if (WARN_ON(node >= nr_node_ids))
50 return cpu_none_mask;
52 if (WARN_ON(node_to_cpumask_map[node] == NULL))
53 return cpu_online_mask;
55 return node_to_cpumask_map[node];
57 EXPORT_SYMBOL(cpumask_of_node);
59 #endif
61 static void numa_update_cpu(unsigned int cpu, bool remove)
63 int nid = cpu_to_node(cpu);
65 if (nid == NUMA_NO_NODE)
66 return;
68 if (remove)
69 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
70 else
71 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
74 void numa_add_cpu(unsigned int cpu)
76 numa_update_cpu(cpu, false);
79 void numa_remove_cpu(unsigned int cpu)
81 numa_update_cpu(cpu, true);
84 void numa_clear_node(unsigned int cpu)
86 numa_remove_cpu(cpu);
87 set_cpu_numa_node(cpu, NUMA_NO_NODE);
91 * Allocate node_to_cpumask_map based on number of available nodes
92 * Requires node_possible_map to be valid.
94 * Note: cpumask_of_node() is not valid until after this is done.
95 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
97 static void __init setup_node_to_cpumask_map(void)
99 int node;
101 /* setup nr_node_ids if not done yet */
102 if (nr_node_ids == MAX_NUMNODES)
103 setup_nr_node_ids();
105 /* allocate and clear the mapping */
106 for (node = 0; node < nr_node_ids; node++) {
107 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
108 cpumask_clear(node_to_cpumask_map[node]);
111 /* cpumask_of_node() will now work */
112 pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
116 * Set the cpu to node and mem mapping
118 void numa_store_cpu_info(unsigned int cpu)
120 set_cpu_numa_node(cpu, cpu_to_node_map[cpu]);
123 void __init early_map_cpu_to_node(unsigned int cpu, int nid)
125 /* fallback to node 0 */
126 if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
127 nid = 0;
129 cpu_to_node_map[cpu] = nid;
132 * We should set the numa node of cpu0 as soon as possible, because it
133 * has already been set up online before. cpu_to_node(0) will soon be
134 * called.
136 if (!cpu)
137 set_cpu_numa_node(cpu, nid);
140 #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
141 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
142 EXPORT_SYMBOL(__per_cpu_offset);
144 static int __init early_cpu_to_node(int cpu)
146 return cpu_to_node_map[cpu];
149 static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
151 return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
154 static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
155 size_t align)
157 int nid = early_cpu_to_node(cpu);
159 return memblock_alloc_try_nid(size, align,
160 __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
163 static void __init pcpu_fc_free(void *ptr, size_t size)
165 memblock_free_early(__pa(ptr), size);
168 void __init setup_per_cpu_areas(void)
170 unsigned long delta;
171 unsigned int cpu;
172 int rc;
175 * Always reserve area for module percpu variables. That's
176 * what the legacy allocator did.
178 rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
179 PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
180 pcpu_cpu_distance,
181 pcpu_fc_alloc, pcpu_fc_free);
182 if (rc < 0)
183 panic("Failed to initialize percpu areas.");
185 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
186 for_each_possible_cpu(cpu)
187 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
189 #endif
192 * numa_add_memblk() - Set node id to memblk
193 * @nid: NUMA node ID of the new memblk
194 * @start: Start address of the new memblk
195 * @end: End address of the new memblk
197 * RETURNS:
198 * 0 on success, -errno on failure.
200 int __init numa_add_memblk(int nid, u64 start, u64 end)
202 int ret;
204 ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
205 if (ret < 0) {
206 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
207 start, (end - 1), nid);
208 return ret;
211 node_set(nid, numa_nodes_parsed);
212 return ret;
216 * Initialize NODE_DATA for a node on the local memory
218 static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
220 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
221 u64 nd_pa;
222 void *nd;
223 int tnid;
225 if (start_pfn >= end_pfn)
226 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
228 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
229 if (!nd_pa)
230 panic("Cannot allocate %zu bytes for node %d data\n",
231 nd_size, nid);
233 nd = __va(nd_pa);
235 /* report and initialize */
236 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
237 nd_pa, nd_pa + nd_size - 1);
238 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
239 if (tnid != nid)
240 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
242 node_data[nid] = nd;
243 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
244 NODE_DATA(nid)->node_id = nid;
245 NODE_DATA(nid)->node_start_pfn = start_pfn;
246 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
250 * numa_free_distance
252 * The current table is freed.
254 void __init numa_free_distance(void)
256 size_t size;
258 if (!numa_distance)
259 return;
261 size = numa_distance_cnt * numa_distance_cnt *
262 sizeof(numa_distance[0]);
264 memblock_free(__pa(numa_distance), size);
265 numa_distance_cnt = 0;
266 numa_distance = NULL;
270 * Create a new NUMA distance table.
272 static int __init numa_alloc_distance(void)
274 size_t size;
275 u64 phys;
276 int i, j;
278 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
279 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
280 size, PAGE_SIZE);
281 if (WARN_ON(!phys))
282 return -ENOMEM;
284 memblock_reserve(phys, size);
286 numa_distance = __va(phys);
287 numa_distance_cnt = nr_node_ids;
289 /* fill with the default distances */
290 for (i = 0; i < numa_distance_cnt; i++)
291 for (j = 0; j < numa_distance_cnt; j++)
292 numa_distance[i * numa_distance_cnt + j] = i == j ?
293 LOCAL_DISTANCE : REMOTE_DISTANCE;
295 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
297 return 0;
301 * numa_set_distance() - Set inter node NUMA distance from node to node.
302 * @from: the 'from' node to set distance
303 * @to: the 'to' node to set distance
304 * @distance: NUMA distance
306 * Set the distance from node @from to @to to @distance.
307 * If distance table doesn't exist, a warning is printed.
309 * If @from or @to is higher than the highest known node or lower than zero
310 * or @distance doesn't make sense, the call is ignored.
312 void __init numa_set_distance(int from, int to, int distance)
314 if (!numa_distance) {
315 pr_warn_once("Warning: distance table not allocated yet\n");
316 return;
319 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
320 from < 0 || to < 0) {
321 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
322 from, to, distance);
323 return;
326 if ((u8)distance != distance ||
327 (from == to && distance != LOCAL_DISTANCE)) {
328 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
329 from, to, distance);
330 return;
333 numa_distance[from * numa_distance_cnt + to] = distance;
337 * Return NUMA distance @from to @to
339 int __node_distance(int from, int to)
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];
345 EXPORT_SYMBOL(__node_distance);
347 static int __init numa_register_nodes(void)
349 int nid;
350 struct memblock_region *mblk;
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) {
355 pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
356 mblk->nid, mblk->base,
357 mblk->base + mblk->size - 1);
358 return -EINVAL;
361 /* Finally register nodes. */
362 for_each_node_mask(nid, numa_nodes_parsed) {
363 unsigned long start_pfn, end_pfn;
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);
370 /* Setup online nodes to actual nodes*/
371 node_possible_map = numa_nodes_parsed;
373 return 0;
376 static int __init numa_init(int (*init_func)(void))
378 int ret;
380 nodes_clear(numa_nodes_parsed);
381 nodes_clear(node_possible_map);
382 nodes_clear(node_online_map);
384 ret = numa_alloc_distance();
385 if (ret < 0)
386 return ret;
388 ret = init_func();
389 if (ret < 0)
390 goto out_free_distance;
392 if (nodes_empty(numa_nodes_parsed)) {
393 pr_info("No NUMA configuration found\n");
394 ret = -EINVAL;
395 goto out_free_distance;
398 ret = numa_register_nodes();
399 if (ret < 0)
400 goto out_free_distance;
402 setup_node_to_cpumask_map();
404 return 0;
405 out_free_distance:
406 numa_free_distance();
407 return ret;
411 * dummy_numa_init() - Fallback dummy NUMA init
413 * Used if there's no underlying NUMA architecture, NUMA initialization
414 * fails, or NUMA is disabled on the command line.
416 * Must online at least one node (node 0) and add memory blocks that cover all
417 * allowed memory. It is unlikely that this function fails.
419 * Return: 0 on success, -errno on failure.
421 static int __init dummy_numa_init(void)
423 int ret;
424 struct memblock_region *mblk;
426 if (numa_off)
427 pr_info("NUMA disabled\n"); /* Forced off on command line. */
428 pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
429 memblock_start_of_DRAM(), memblock_end_of_DRAM() - 1);
431 for_each_memblock(memory, mblk) {
432 ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
433 if (!ret)
434 continue;
436 pr_err("NUMA init failed\n");
437 return ret;
440 numa_off = true;
441 return 0;
445 * arm64_numa_init() - Initialize NUMA
447 * Try each configured NUMA initialization method until one succeeds. The
448 * last fallback is dummy single node config encomapssing whole memory.
450 void __init arm64_numa_init(void)
452 if (!numa_off) {
453 if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
454 return;
455 if (acpi_disabled && !numa_init(of_numa_init))
456 return;
459 numa_init(dummy_numa_init);
463 * We hope that we will be hotplugging memory on nodes we already know about,
464 * such that acpi_get_node() succeeds and we never fall back to this...
466 int memory_add_physaddr_to_nid(u64 addr)
468 pr_warn("Unknown node for memory at 0x%llx, assuming node 0\n", addr);
469 return 0;