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