Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * NUMA support, based on the x86 implementation. |
| 4 | * |
| 5 | * Copyright (C) 2015 Cavium Inc. |
| 6 | * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com> |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 9 | #define pr_fmt(fmt) "NUMA: " fmt |
| 10 | |
Hanjun Guo | d8b47fc | 2016-05-24 15:35:44 -0700 | [diff] [blame] | 11 | #include <linux/acpi.h> |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 12 | #include <linux/memblock.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/of.h> |
| 15 | |
Catalin Marinas | bfe6c8a | 2016-08-15 16:33:10 +0100 | [diff] [blame] | 16 | #include <asm/acpi.h> |
Zhen Lei | 7af3a0a | 2016-09-01 14:55:00 +0800 | [diff] [blame] | 17 | #include <asm/sections.h> |
Catalin Marinas | bfe6c8a | 2016-08-15 16:33:10 +0100 | [diff] [blame] | 18 | |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 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 }; |
| 23 | |
| 24 | static int numa_distance_cnt; |
| 25 | static u8 *numa_distance; |
Boris Ostrovsky | aec03f8 | 2016-12-12 23:18:29 -0500 | [diff] [blame] | 26 | bool numa_off; |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 27 | |
| 28 | static __init int numa_parse_early_param(char *opt) |
| 29 | { |
| 30 | if (!opt) |
| 31 | return -EINVAL; |
Chuhong Yuan | b3e089c | 2019-07-30 10:44:15 +0800 | [diff] [blame] | 32 | if (str_has_prefix(opt, "off")) |
David Daney | 34c3337 | 2016-05-24 15:35:37 -0700 | [diff] [blame] | 33 | numa_off = true; |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 34 | |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | early_param("numa", numa_parse_early_param); |
| 38 | |
| 39 | cpumask_var_t node_to_cpumask_map[MAX_NUMNODES]; |
| 40 | EXPORT_SYMBOL(node_to_cpumask_map); |
| 41 | |
| 42 | #ifdef CONFIG_DEBUG_PER_CPU_MAPS |
| 43 | |
| 44 | /* |
| 45 | * Returns a pointer to the bitmask of CPUs on Node 'node'. |
| 46 | */ |
| 47 | const struct cpumask *cpumask_of_node(int node) |
| 48 | { |
Zhengyuan Liu | a194c5f | 2020-09-21 10:39:36 +0800 | [diff] [blame] | 49 | |
| 50 | if (node == NUMA_NO_NODE) |
| 51 | return cpu_all_mask; |
| 52 | |
| 53 | if (WARN_ON(node < 0 || node >= nr_node_ids)) |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 54 | return cpu_none_mask; |
| 55 | |
| 56 | if (WARN_ON(node_to_cpumask_map[node] == NULL)) |
| 57 | return cpu_online_mask; |
| 58 | |
| 59 | return node_to_cpumask_map[node]; |
| 60 | } |
| 61 | EXPORT_SYMBOL(cpumask_of_node); |
| 62 | |
| 63 | #endif |
| 64 | |
Sudeep Holla | 97fd601 | 2018-07-06 12:02:43 +0100 | [diff] [blame] | 65 | static void numa_update_cpu(unsigned int cpu, bool remove) |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 66 | { |
Sudeep Holla | 97fd601 | 2018-07-06 12:02:43 +0100 | [diff] [blame] | 67 | int nid = cpu_to_node(cpu); |
| 68 | |
| 69 | if (nid == NUMA_NO_NODE) |
| 70 | return; |
| 71 | |
| 72 | if (remove) |
| 73 | cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]); |
| 74 | else |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 75 | cpumask_set_cpu(cpu, node_to_cpumask_map[nid]); |
| 76 | } |
| 77 | |
Sudeep Holla | 97fd601 | 2018-07-06 12:02:43 +0100 | [diff] [blame] | 78 | void numa_add_cpu(unsigned int cpu) |
| 79 | { |
| 80 | numa_update_cpu(cpu, false); |
| 81 | } |
| 82 | |
| 83 | void numa_remove_cpu(unsigned int cpu) |
| 84 | { |
| 85 | numa_update_cpu(cpu, true); |
| 86 | } |
| 87 | |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 88 | void numa_clear_node(unsigned int cpu) |
| 89 | { |
Sudeep Holla | 97fd601 | 2018-07-06 12:02:43 +0100 | [diff] [blame] | 90 | numa_remove_cpu(cpu); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 91 | set_cpu_numa_node(cpu, NUMA_NO_NODE); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Allocate node_to_cpumask_map based on number of available nodes |
| 96 | * Requires node_possible_map to be valid. |
| 97 | * |
| 98 | * Note: cpumask_of_node() is not valid until after this is done. |
| 99 | * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.) |
| 100 | */ |
| 101 | static void __init setup_node_to_cpumask_map(void) |
| 102 | { |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 103 | int node; |
| 104 | |
| 105 | /* setup nr_node_ids if not done yet */ |
| 106 | if (nr_node_ids == MAX_NUMNODES) |
| 107 | setup_nr_node_ids(); |
| 108 | |
| 109 | /* allocate and clear the mapping */ |
| 110 | for (node = 0; node < nr_node_ids; node++) { |
| 111 | alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]); |
| 112 | cpumask_clear(node_to_cpumask_map[node]); |
| 113 | } |
| 114 | |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 115 | /* cpumask_of_node() will now work */ |
Alexey Dobriyan | b9726c2 | 2019-03-05 15:48:26 -0800 | [diff] [blame] | 116 | pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 120 | * Set the cpu to node and mem mapping |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 121 | */ |
| 122 | void numa_store_cpu_info(unsigned int cpu) |
| 123 | { |
Sudeep Holla | 97fd601 | 2018-07-06 12:02:43 +0100 | [diff] [blame] | 124 | set_cpu_numa_node(cpu, cpu_to_node_map[cpu]); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void __init early_map_cpu_to_node(unsigned int cpu, int nid) |
| 128 | { |
| 129 | /* fallback to node 0 */ |
Zhen Lei | 7ba5f60 | 2016-09-01 14:55:04 +0800 | [diff] [blame] | 130 | if (nid < 0 || nid >= MAX_NUMNODES || numa_off) |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 131 | nid = 0; |
| 132 | |
| 133 | cpu_to_node_map[cpu] = nid; |
Zhen Lei | 7ba5f60 | 2016-09-01 14:55:04 +0800 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * We should set the numa node of cpu0 as soon as possible, because it |
| 137 | * has already been set up online before. cpu_to_node(0) will soon be |
| 138 | * called. |
| 139 | */ |
| 140 | if (!cpu) |
| 141 | set_cpu_numa_node(cpu, nid); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Zhen Lei | 7af3a0a | 2016-09-01 14:55:00 +0800 | [diff] [blame] | 144 | #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA |
| 145 | unsigned long __per_cpu_offset[NR_CPUS] __read_mostly; |
| 146 | EXPORT_SYMBOL(__per_cpu_offset); |
| 147 | |
| 148 | static int __init early_cpu_to_node(int cpu) |
| 149 | { |
| 150 | return cpu_to_node_map[cpu]; |
| 151 | } |
| 152 | |
| 153 | static int __init pcpu_cpu_distance(unsigned int from, unsigned int to) |
| 154 | { |
Yisheng Xie | 26984c3 | 2016-10-21 16:13:55 +0800 | [diff] [blame] | 155 | return node_distance(early_cpu_to_node(from), early_cpu_to_node(to)); |
Zhen Lei | 7af3a0a | 2016-09-01 14:55:00 +0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size, |
| 159 | size_t align) |
| 160 | { |
| 161 | int nid = early_cpu_to_node(cpu); |
| 162 | |
Mike Rapoport | eb31d55 | 2018-10-30 15:08:04 -0700 | [diff] [blame] | 163 | return memblock_alloc_try_nid(size, align, |
Zhen Lei | 7af3a0a | 2016-09-01 14:55:00 +0800 | [diff] [blame] | 164 | __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid); |
| 165 | } |
| 166 | |
| 167 | static void __init pcpu_fc_free(void *ptr, size_t size) |
| 168 | { |
| 169 | memblock_free_early(__pa(ptr), size); |
| 170 | } |
| 171 | |
| 172 | void __init setup_per_cpu_areas(void) |
| 173 | { |
| 174 | unsigned long delta; |
| 175 | unsigned int cpu; |
| 176 | int rc; |
| 177 | |
| 178 | /* |
| 179 | * Always reserve area for module percpu variables. That's |
| 180 | * what the legacy allocator did. |
| 181 | */ |
| 182 | rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE, |
| 183 | PERCPU_DYNAMIC_RESERVE, PAGE_SIZE, |
| 184 | pcpu_cpu_distance, |
| 185 | pcpu_fc_alloc, pcpu_fc_free); |
| 186 | if (rc < 0) |
| 187 | panic("Failed to initialize percpu areas."); |
| 188 | |
| 189 | delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; |
| 190 | for_each_possible_cpu(cpu) |
| 191 | __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu]; |
| 192 | } |
| 193 | #endif |
| 194 | |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 195 | /** |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 196 | * numa_add_memblk() - Set node id to memblk |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 197 | * @nid: NUMA node ID of the new memblk |
| 198 | * @start: Start address of the new memblk |
Hanjun Guo | 8ccbbda | 2016-05-24 15:35:36 -0700 | [diff] [blame] | 199 | * @end: End address of the new memblk |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 200 | * |
| 201 | * RETURNS: |
| 202 | * 0 on success, -errno on failure. |
| 203 | */ |
Hanjun Guo | 8ccbbda | 2016-05-24 15:35:36 -0700 | [diff] [blame] | 204 | int __init numa_add_memblk(int nid, u64 start, u64 end) |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 205 | { |
| 206 | int ret; |
| 207 | |
Hanjun Guo | 8ccbbda | 2016-05-24 15:35:36 -0700 | [diff] [blame] | 208 | ret = memblock_set_node(start, (end - start), &memblock.memory, nid); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 209 | if (ret < 0) { |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 210 | pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n", |
Hanjun Guo | 8ccbbda | 2016-05-24 15:35:36 -0700 | [diff] [blame] | 211 | start, (end - 1), nid); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | node_set(nid, numa_nodes_parsed); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 216 | return ret; |
| 217 | } |
| 218 | |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 219 | /* |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 220 | * Initialize NODE_DATA for a node on the local memory |
| 221 | */ |
| 222 | static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn) |
| 223 | { |
| 224 | const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES); |
| 225 | u64 nd_pa; |
| 226 | void *nd; |
| 227 | int tnid; |
| 228 | |
Punit Agrawal | ece4b20 | 2017-07-20 12:03:59 +0100 | [diff] [blame] | 229 | if (start_pfn >= end_pfn) |
Hanjun Guo | 3f7a09f | 2016-10-21 16:13:56 +0800 | [diff] [blame] | 230 | pr_info("Initmem setup node %d [<memory-less node>]\n", nid); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 231 | |
Mike Rapoport | 9a8dd70 | 2018-10-30 15:07:59 -0700 | [diff] [blame] | 232 | nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid); |
Mike Rapoport | 3375557 | 2019-03-11 23:29:21 -0700 | [diff] [blame] | 233 | if (!nd_pa) |
| 234 | panic("Cannot allocate %zu bytes for node %d data\n", |
| 235 | nd_size, nid); |
| 236 | |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 237 | nd = __va(nd_pa); |
| 238 | |
| 239 | /* report and initialize */ |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 240 | pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n", |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 241 | nd_pa, nd_pa + nd_size - 1); |
| 242 | tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT); |
| 243 | if (tnid != nid) |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 244 | pr_info("NODE_DATA(%d) on node %d\n", nid, tnid); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 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 | |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 253 | /* |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 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 | |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 273 | /* |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 274 | * Create a new NUMA distance table. |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 275 | */ |
| 276 | static int __init numa_alloc_distance(void) |
| 277 | { |
| 278 | size_t size; |
| 279 | u64 phys; |
| 280 | int i, j; |
| 281 | |
| 282 | size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]); |
| 283 | phys = memblock_find_in_range(0, PFN_PHYS(max_pfn), |
| 284 | size, PAGE_SIZE); |
| 285 | if (WARN_ON(!phys)) |
| 286 | return -ENOMEM; |
| 287 | |
| 288 | memblock_reserve(phys, size); |
| 289 | |
| 290 | numa_distance = __va(phys); |
| 291 | numa_distance_cnt = nr_node_ids; |
| 292 | |
| 293 | /* fill with the default distances */ |
| 294 | for (i = 0; i < numa_distance_cnt; i++) |
| 295 | for (j = 0; j < numa_distance_cnt; j++) |
| 296 | numa_distance[i * numa_distance_cnt + j] = i == j ? |
| 297 | LOCAL_DISTANCE : REMOTE_DISTANCE; |
| 298 | |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 299 | pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /** |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 305 | * numa_set_distance() - Set inter node NUMA distance from node to node. |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 306 | * @from: the 'from' node to set distance |
| 307 | * @to: the 'to' node to set distance |
| 308 | * @distance: NUMA distance |
| 309 | * |
| 310 | * Set the distance from node @from to @to to @distance. |
| 311 | * If distance table doesn't exist, a warning is printed. |
| 312 | * |
| 313 | * If @from or @to is higher than the highest known node or lower than zero |
| 314 | * or @distance doesn't make sense, the call is ignored. |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 315 | */ |
| 316 | void __init numa_set_distance(int from, int to, int distance) |
| 317 | { |
| 318 | if (!numa_distance) { |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 319 | pr_warn_once("Warning: distance table not allocated yet\n"); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | |
| 323 | if (from >= numa_distance_cnt || to >= numa_distance_cnt || |
| 324 | from < 0 || to < 0) { |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 325 | pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n", |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 326 | from, to, distance); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | if ((u8)distance != distance || |
| 331 | (from == to && distance != LOCAL_DISTANCE)) { |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 332 | pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n", |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 333 | from, to, distance); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | numa_distance[from * numa_distance_cnt + to] = distance; |
| 338 | } |
| 339 | |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 340 | /* |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 341 | * Return NUMA distance @from to @to |
| 342 | */ |
| 343 | int __node_distance(int from, int to) |
| 344 | { |
| 345 | if (from >= numa_distance_cnt || to >= numa_distance_cnt) |
| 346 | return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE; |
| 347 | return numa_distance[from * numa_distance_cnt + to]; |
| 348 | } |
| 349 | EXPORT_SYMBOL(__node_distance); |
| 350 | |
| 351 | static int __init numa_register_nodes(void) |
| 352 | { |
| 353 | int nid; |
| 354 | struct memblock_region *mblk; |
| 355 | |
| 356 | /* Check that valid nid is set to memblks */ |
Mike Rapoport | cc6de16 | 2020-10-13 16:58:30 -0700 | [diff] [blame^] | 357 | for_each_mem_region(mblk) { |
Mike Rapoport | d622abf | 2020-06-03 15:56:53 -0700 | [diff] [blame] | 358 | int mblk_nid = memblock_get_region_node(mblk); |
| 359 | |
| 360 | if (mblk_nid == NUMA_NO_NODE || mblk_nid >= MAX_NUMNODES) { |
Kefeng Wang | f11c7ba | 2016-09-01 14:54:59 +0800 | [diff] [blame] | 361 | pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n", |
Mike Rapoport | d622abf | 2020-06-03 15:56:53 -0700 | [diff] [blame] | 362 | mblk_nid, mblk->base, |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 363 | mblk->base + mblk->size - 1); |
| 364 | return -EINVAL; |
| 365 | } |
Mike Rapoport | d622abf | 2020-06-03 15:56:53 -0700 | [diff] [blame] | 366 | } |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 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); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 390 | |
| 391 | ret = numa_alloc_distance(); |
| 392 | if (ret < 0) |
| 393 | return ret; |
| 394 | |
| 395 | ret = init_func(); |
| 396 | if (ret < 0) |
Anshuman Khandual | 5233808 | 2018-09-22 21:09:56 +0530 | [diff] [blame] | 397 | goto out_free_distance; |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 398 | |
Zhen Lei | 794224ea | 2016-09-01 14:54:56 +0800 | [diff] [blame] | 399 | if (nodes_empty(numa_nodes_parsed)) { |
| 400 | pr_info("No NUMA configuration found\n"); |
Anshuman Khandual | 5233808 | 2018-09-22 21:09:56 +0530 | [diff] [blame] | 401 | ret = -EINVAL; |
| 402 | goto out_free_distance; |
Zhen Lei | 794224ea | 2016-09-01 14:54:56 +0800 | [diff] [blame] | 403 | } |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 404 | |
| 405 | ret = numa_register_nodes(); |
| 406 | if (ret < 0) |
Anshuman Khandual | 5233808 | 2018-09-22 21:09:56 +0530 | [diff] [blame] | 407 | goto out_free_distance; |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 408 | |
| 409 | setup_node_to_cpumask_map(); |
| 410 | |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 411 | return 0; |
Anshuman Khandual | 5233808 | 2018-09-22 21:09:56 +0530 | [diff] [blame] | 412 | out_free_distance: |
| 413 | numa_free_distance(); |
| 414 | return ret; |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /** |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 418 | * dummy_numa_init() - Fallback dummy NUMA init |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 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. |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 425 | * |
| 426 | * Return: 0 on success, -errno on failure. |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 427 | */ |
| 428 | static int __init dummy_numa_init(void) |
| 429 | { |
Mike Rapoport | ab8f21a | 2020-10-13 16:57:31 -0700 | [diff] [blame] | 430 | phys_addr_t start = memblock_start_of_DRAM(); |
| 431 | phys_addr_t end = memblock_end_of_DRAM(); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 432 | int ret; |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 433 | |
David Daney | 34c3337 | 2016-05-24 15:35:37 -0700 | [diff] [blame] | 434 | if (numa_off) |
| 435 | pr_info("NUMA disabled\n"); /* Forced off on command line. */ |
Mike Rapoport | ab8f21a | 2020-10-13 16:57:31 -0700 | [diff] [blame] | 436 | pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n", start, end - 1); |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 437 | |
Mike Rapoport | ab8f21a | 2020-10-13 16:57:31 -0700 | [diff] [blame] | 438 | ret = numa_add_memblk(0, start, end); |
| 439 | if (ret) { |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 440 | pr_err("NUMA init failed\n"); |
| 441 | return ret; |
| 442 | } |
| 443 | |
David Daney | 34c3337 | 2016-05-24 15:35:37 -0700 | [diff] [blame] | 444 | numa_off = true; |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | /** |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 449 | * arm64_numa_init() - Initialize NUMA |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 450 | * |
Qian Cai | b1ce45e | 2019-03-14 12:16:19 -0400 | [diff] [blame] | 451 | * Try each configured NUMA initialization method until one succeeds. The |
Yanfei Xu | 9a747c9 | 2020-09-01 17:11:54 +0800 | [diff] [blame] | 452 | * last fallback is dummy single node config encompassing whole memory. |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 453 | */ |
| 454 | void __init arm64_numa_init(void) |
| 455 | { |
| 456 | if (!numa_off) { |
Hanjun Guo | d8b47fc | 2016-05-24 15:35:44 -0700 | [diff] [blame] | 457 | if (!acpi_disabled && !numa_init(arm64_acpi_numa_init)) |
| 458 | return; |
| 459 | if (acpi_disabled && !numa_init(of_numa_init)) |
Ganapatrao Kulkarni | 1a2db30 | 2016-04-08 15:50:27 -0700 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
| 463 | numa_init(dummy_numa_init); |
| 464 | } |