0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) "NUMA: " fmt
0010
0011 #include <linux/acpi.h>
0012 #include <linux/memblock.h>
0013 #include <linux/module.h>
0014 #include <linux/of.h>
0015
0016 #include <asm/sections.h>
0017
0018 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
0019 EXPORT_SYMBOL(node_data);
0020 nodemask_t numa_nodes_parsed __initdata;
0021 static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
0022
0023 static int numa_distance_cnt;
0024 static u8 *numa_distance;
0025 bool numa_off;
0026
0027 static __init int numa_parse_early_param(char *opt)
0028 {
0029 if (!opt)
0030 return -EINVAL;
0031 if (str_has_prefix(opt, "off"))
0032 numa_off = true;
0033
0034 return 0;
0035 }
0036 early_param("numa", numa_parse_early_param);
0037
0038 cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
0039 EXPORT_SYMBOL(node_to_cpumask_map);
0040
0041 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
0042
0043
0044
0045
0046 const struct cpumask *cpumask_of_node(int node)
0047 {
0048
0049 if (node == NUMA_NO_NODE)
0050 return cpu_all_mask;
0051
0052 if (WARN_ON(node < 0 || node >= nr_node_ids))
0053 return cpu_none_mask;
0054
0055 if (WARN_ON(node_to_cpumask_map[node] == NULL))
0056 return cpu_online_mask;
0057
0058 return node_to_cpumask_map[node];
0059 }
0060 EXPORT_SYMBOL(cpumask_of_node);
0061
0062 #endif
0063
0064 static void numa_update_cpu(unsigned int cpu, bool remove)
0065 {
0066 int nid = cpu_to_node(cpu);
0067
0068 if (nid == NUMA_NO_NODE)
0069 return;
0070
0071 if (remove)
0072 cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
0073 else
0074 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
0075 }
0076
0077 void numa_add_cpu(unsigned int cpu)
0078 {
0079 numa_update_cpu(cpu, false);
0080 }
0081
0082 void numa_remove_cpu(unsigned int cpu)
0083 {
0084 numa_update_cpu(cpu, true);
0085 }
0086
0087 void numa_clear_node(unsigned int cpu)
0088 {
0089 numa_remove_cpu(cpu);
0090 set_cpu_numa_node(cpu, NUMA_NO_NODE);
0091 }
0092
0093
0094
0095
0096
0097
0098
0099
0100 static void __init setup_node_to_cpumask_map(void)
0101 {
0102 int node;
0103
0104
0105 if (nr_node_ids == MAX_NUMNODES)
0106 setup_nr_node_ids();
0107
0108
0109 for (node = 0; node < nr_node_ids; node++) {
0110 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
0111 cpumask_clear(node_to_cpumask_map[node]);
0112 }
0113
0114
0115 pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
0116 }
0117
0118
0119
0120
0121 void numa_store_cpu_info(unsigned int cpu)
0122 {
0123 set_cpu_numa_node(cpu, cpu_to_node_map[cpu]);
0124 }
0125
0126 void __init early_map_cpu_to_node(unsigned int cpu, int nid)
0127 {
0128
0129 if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
0130 nid = 0;
0131
0132 cpu_to_node_map[cpu] = nid;
0133
0134
0135
0136
0137
0138
0139 if (!cpu)
0140 set_cpu_numa_node(cpu, nid);
0141 }
0142
0143 #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
0144 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
0145 EXPORT_SYMBOL(__per_cpu_offset);
0146
0147 static int __init early_cpu_to_node(int cpu)
0148 {
0149 return cpu_to_node_map[cpu];
0150 }
0151
0152 static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
0153 {
0154 return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
0155 }
0156
0157 void __init setup_per_cpu_areas(void)
0158 {
0159 unsigned long delta;
0160 unsigned int cpu;
0161 int rc = -EINVAL;
0162
0163 if (pcpu_chosen_fc != PCPU_FC_PAGE) {
0164
0165
0166
0167
0168 rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
0169 PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
0170 pcpu_cpu_distance,
0171 early_cpu_to_node);
0172 #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
0173 if (rc < 0)
0174 pr_warn("PERCPU: %s allocator failed (%d), falling back to page size\n",
0175 pcpu_fc_names[pcpu_chosen_fc], rc);
0176 #endif
0177 }
0178
0179 #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
0180 if (rc < 0)
0181 rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE, early_cpu_to_node);
0182 #endif
0183 if (rc < 0)
0184 panic("Failed to initialize percpu areas (err=%d).", rc);
0185
0186 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
0187 for_each_possible_cpu(cpu)
0188 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
0189 }
0190 #endif
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201 int __init numa_add_memblk(int nid, u64 start, u64 end)
0202 {
0203 int ret;
0204
0205 ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
0206 if (ret < 0) {
0207 pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
0208 start, (end - 1), nid);
0209 return ret;
0210 }
0211
0212 node_set(nid, numa_nodes_parsed);
0213 return ret;
0214 }
0215
0216
0217
0218
0219 static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
0220 {
0221 const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
0222 u64 nd_pa;
0223 void *nd;
0224 int tnid;
0225
0226 if (start_pfn >= end_pfn)
0227 pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
0228
0229 nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
0230 if (!nd_pa)
0231 panic("Cannot allocate %zu bytes for node %d data\n",
0232 nd_size, nid);
0233
0234 nd = __va(nd_pa);
0235
0236
0237 pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
0238 nd_pa, nd_pa + nd_size - 1);
0239 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
0240 if (tnid != nid)
0241 pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
0242
0243 node_data[nid] = nd;
0244 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
0245 NODE_DATA(nid)->node_id = nid;
0246 NODE_DATA(nid)->node_start_pfn = start_pfn;
0247 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
0248 }
0249
0250
0251
0252
0253
0254
0255 void __init numa_free_distance(void)
0256 {
0257 size_t size;
0258
0259 if (!numa_distance)
0260 return;
0261
0262 size = numa_distance_cnt * numa_distance_cnt *
0263 sizeof(numa_distance[0]);
0264
0265 memblock_free(numa_distance, size);
0266 numa_distance_cnt = 0;
0267 numa_distance = NULL;
0268 }
0269
0270
0271
0272
0273 static int __init numa_alloc_distance(void)
0274 {
0275 size_t size;
0276 int i, j;
0277
0278 size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
0279 numa_distance = memblock_alloc(size, PAGE_SIZE);
0280 if (WARN_ON(!numa_distance))
0281 return -ENOMEM;
0282
0283 numa_distance_cnt = nr_node_ids;
0284
0285
0286 for (i = 0; i < numa_distance_cnt; i++)
0287 for (j = 0; j < numa_distance_cnt; j++)
0288 numa_distance[i * numa_distance_cnt + j] = i == j ?
0289 LOCAL_DISTANCE : REMOTE_DISTANCE;
0290
0291 pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
0292
0293 return 0;
0294 }
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308 void __init numa_set_distance(int from, int to, int distance)
0309 {
0310 if (!numa_distance) {
0311 pr_warn_once("Warning: distance table not allocated yet\n");
0312 return;
0313 }
0314
0315 if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
0316 from < 0 || to < 0) {
0317 pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
0318 from, to, distance);
0319 return;
0320 }
0321
0322 if ((u8)distance != distance ||
0323 (from == to && distance != LOCAL_DISTANCE)) {
0324 pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
0325 from, to, distance);
0326 return;
0327 }
0328
0329 numa_distance[from * numa_distance_cnt + to] = distance;
0330 }
0331
0332
0333
0334
0335 int __node_distance(int from, int to)
0336 {
0337 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
0338 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
0339 return numa_distance[from * numa_distance_cnt + to];
0340 }
0341 EXPORT_SYMBOL(__node_distance);
0342
0343 static int __init numa_register_nodes(void)
0344 {
0345 int nid;
0346 struct memblock_region *mblk;
0347
0348
0349 for_each_mem_region(mblk) {
0350 int mblk_nid = memblock_get_region_node(mblk);
0351 phys_addr_t start = mblk->base;
0352 phys_addr_t end = mblk->base + mblk->size - 1;
0353
0354 if (mblk_nid == NUMA_NO_NODE || mblk_nid >= MAX_NUMNODES) {
0355 pr_warn("Warning: invalid memblk node %d [mem %pap-%pap]\n",
0356 mblk_nid, &start, &end);
0357 return -EINVAL;
0358 }
0359 }
0360
0361
0362 for_each_node_mask(nid, numa_nodes_parsed) {
0363 unsigned long start_pfn, end_pfn;
0364
0365 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
0366 setup_node_data(nid, start_pfn, end_pfn);
0367 node_set_online(nid);
0368 }
0369
0370
0371 node_possible_map = numa_nodes_parsed;
0372
0373 return 0;
0374 }
0375
0376 static int __init numa_init(int (*init_func)(void))
0377 {
0378 int ret;
0379
0380 nodes_clear(numa_nodes_parsed);
0381 nodes_clear(node_possible_map);
0382 nodes_clear(node_online_map);
0383
0384 ret = numa_alloc_distance();
0385 if (ret < 0)
0386 return ret;
0387
0388 ret = init_func();
0389 if (ret < 0)
0390 goto out_free_distance;
0391
0392 if (nodes_empty(numa_nodes_parsed)) {
0393 pr_info("No NUMA configuration found\n");
0394 ret = -EINVAL;
0395 goto out_free_distance;
0396 }
0397
0398 ret = numa_register_nodes();
0399 if (ret < 0)
0400 goto out_free_distance;
0401
0402 setup_node_to_cpumask_map();
0403
0404 return 0;
0405 out_free_distance:
0406 numa_free_distance();
0407 return ret;
0408 }
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421 static int __init dummy_numa_init(void)
0422 {
0423 phys_addr_t start = memblock_start_of_DRAM();
0424 phys_addr_t end = memblock_end_of_DRAM() - 1;
0425 int ret;
0426
0427 if (numa_off)
0428 pr_info("NUMA disabled\n");
0429 pr_info("Faking a node at [mem %pap-%pap]\n", &start, &end);
0430
0431 ret = numa_add_memblk(0, start, end + 1);
0432 if (ret) {
0433 pr_err("NUMA init failed\n");
0434 return ret;
0435 }
0436
0437 numa_off = true;
0438 return 0;
0439 }
0440
0441 #ifdef CONFIG_ACPI_NUMA
0442 static int __init arch_acpi_numa_init(void)
0443 {
0444 int ret;
0445
0446 ret = acpi_numa_init();
0447 if (ret) {
0448 pr_info("Failed to initialise from firmware\n");
0449 return ret;
0450 }
0451
0452 return srat_disabled() ? -EINVAL : 0;
0453 }
0454 #else
0455 static int __init arch_acpi_numa_init(void)
0456 {
0457 return -EOPNOTSUPP;
0458 }
0459 #endif
0460
0461
0462
0463
0464
0465
0466
0467 void __init arch_numa_init(void)
0468 {
0469 if (!numa_off) {
0470 if (!acpi_disabled && !numa_init(arch_acpi_numa_init))
0471 return;
0472 if (acpi_disabled && !numa_init(of_numa_init))
0473 return;
0474 }
0475
0476 numa_init(dummy_numa_init);
0477 }