numa.c 10.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8
/*
 * NUMA support, based on the x86 implementation.
 *
 * Copyright (C) 2015 Cavium Inc.
 * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
 */

Kefeng Wang's avatar
Kefeng Wang committed
9 10
#define pr_fmt(fmt) "NUMA: " fmt

11
#include <linux/acpi.h>
12 13 14 15
#include <linux/memblock.h>
#include <linux/module.h>
#include <linux/of.h>

16
#include <asm/acpi.h>
17
#include <asm/sections.h>
18

19 20 21 22 23 24 25
struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
EXPORT_SYMBOL(node_data);
nodemask_t numa_nodes_parsed __initdata;
static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };

static int numa_distance_cnt;
static u8 *numa_distance;
26
bool numa_off;
27 28 29 30 31

static __init int numa_parse_early_param(char *opt)
{
	if (!opt)
		return -EINVAL;
32
	if (str_has_prefix(opt, "off"))
33
		numa_off = true;
Kefeng Wang's avatar
Kefeng Wang committed
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	return 0;
}
early_param("numa", numa_parse_early_param);

cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
EXPORT_SYMBOL(node_to_cpumask_map);

#ifdef CONFIG_DEBUG_PER_CPU_MAPS

/*
 * Returns a pointer to the bitmask of CPUs on Node 'node'.
 */
const struct cpumask *cpumask_of_node(int node)
{
	if (WARN_ON(node >= nr_node_ids))
		return cpu_none_mask;

	if (WARN_ON(node_to_cpumask_map[node] == NULL))
		return cpu_online_mask;

	return node_to_cpumask_map[node];
}
EXPORT_SYMBOL(cpumask_of_node);

#endif

61
static void numa_update_cpu(unsigned int cpu, bool remove)
62
{
63 64 65 66 67 68 69 70
	int nid = cpu_to_node(cpu);

	if (nid == NUMA_NO_NODE)
		return;

	if (remove)
		cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
	else
71 72 73
		cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
}

74
void numa_add_cpu(unsigned int cpu)
75
{
76 77
	numa_update_cpu(cpu, false);
}
78

79 80 81 82 83 84 85 86
void numa_remove_cpu(unsigned int cpu)
{
	numa_update_cpu(cpu, true);
}

void numa_clear_node(unsigned int cpu)
{
	numa_remove_cpu(cpu);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	set_cpu_numa_node(cpu, NUMA_NO_NODE);
}

/*
 * Allocate node_to_cpumask_map based on number of available nodes
 * Requires node_possible_map to be valid.
 *
 * Note: cpumask_of_node() is not valid until after this is done.
 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
 */
static void __init setup_node_to_cpumask_map(void)
{
	int node;

	/* setup nr_node_ids if not done yet */
	if (nr_node_ids == MAX_NUMNODES)
		setup_nr_node_ids();

	/* allocate and clear the mapping */
	for (node = 0; node < nr_node_ids; node++) {
		alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
		cpumask_clear(node_to_cpumask_map[node]);
	}

	/* cpumask_of_node() will now work */
112
	pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
113 114 115
}

/*
116
 * Set the cpu to node and mem mapping
117 118 119
 */
void numa_store_cpu_info(unsigned int cpu)
{
120
	set_cpu_numa_node(cpu, cpu_to_node_map[cpu]);
121 122 123 124 125
}

void __init early_map_cpu_to_node(unsigned int cpu, int nid)
{
	/* fallback to node 0 */
126
	if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
127 128 129
		nid = 0;

	cpu_to_node_map[cpu] = nid;
130 131 132 133 134 135 136 137

	/*
	 * We should set the numa node of cpu0 as soon as possible, because it
	 * has already been set up online before. cpu_to_node(0) will soon be
	 * called.
	 */
	if (!cpu)
		set_cpu_numa_node(cpu, nid);
138 139
}

140 141 142 143 144 145 146 147 148 149 150
#ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
EXPORT_SYMBOL(__per_cpu_offset);

static int __init early_cpu_to_node(int cpu)
{
	return cpu_to_node_map[cpu];
}

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));
152 153 154 155 156 157 158
}

static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
				       size_t align)
{
	int nid = early_cpu_to_node(cpu);

159
	return  memblock_alloc_try_nid(size, align,
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
			__pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
}

static void __init pcpu_fc_free(void *ptr, size_t size)
{
	memblock_free_early(__pa(ptr), size);
}

void __init setup_per_cpu_areas(void)
{
	unsigned long delta;
	unsigned int cpu;
	int rc;

	/*
	 * Always reserve area for module percpu variables.  That's
	 * what the legacy allocator did.
	 */
	rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
				    PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
				    pcpu_cpu_distance,
				    pcpu_fc_alloc, pcpu_fc_free);
	if (rc < 0)
		panic("Failed to initialize percpu areas.");

	delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
	for_each_possible_cpu(cpu)
		__per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
}
#endif

191
/**
192
 * numa_add_memblk() - Set node id to memblk
193 194
 * @nid: NUMA node ID of the new memblk
 * @start: Start address of the new memblk
195
 * @end:  End address of the new memblk
196 197 198 199
 *
 * RETURNS:
 * 0 on success, -errno on failure.
 */
200
int __init numa_add_memblk(int nid, u64 start, u64 end)
201 202 203
{
	int ret;

204
	ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
205
	if (ret < 0) {
Kefeng Wang's avatar
Kefeng Wang committed
206
		pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
207
			start, (end - 1), nid);
208 209 210 211 212 213 214
		return ret;
	}

	node_set(nid, numa_nodes_parsed);
	return ret;
}

215
/*
216 217 218 219 220 221 222 223 224
 * Initialize NODE_DATA for a node on the local memory
 */
static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
{
	const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
	u64 nd_pa;
	void *nd;
	int tnid;

225
	if (start_pfn >= end_pfn)
226
		pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
227

228
	nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
229 230 231 232
	if (!nd_pa)
		panic("Cannot allocate %zu bytes for node %d data\n",
		      nd_size, nid);

233 234 235
	nd = __va(nd_pa);

	/* report and initialize */
Kefeng Wang's avatar
Kefeng Wang committed
236
	pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
237 238 239
		nd_pa, nd_pa + nd_size - 1);
	tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
	if (tnid != nid)
Kefeng Wang's avatar
Kefeng Wang committed
240
		pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
241 242 243 244 245 246 247 248

	node_data[nid] = nd;
	memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
	NODE_DATA(nid)->node_id = nid;
	NODE_DATA(nid)->node_start_pfn = start_pfn;
	NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
}

249
/*
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
 * numa_free_distance
 *
 * The current table is freed.
 */
void __init numa_free_distance(void)
{
	size_t size;

	if (!numa_distance)
		return;

	size = numa_distance_cnt * numa_distance_cnt *
		sizeof(numa_distance[0]);

	memblock_free(__pa(numa_distance), size);
	numa_distance_cnt = 0;
	numa_distance = NULL;
}

269
/*
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
 * Create a new NUMA distance table.
 */
static int __init numa_alloc_distance(void)
{
	size_t size;
	u64 phys;
	int i, j;

	size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
	phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
				      size, PAGE_SIZE);
	if (WARN_ON(!phys))
		return -ENOMEM;

	memblock_reserve(phys, size);

	numa_distance = __va(phys);
	numa_distance_cnt = nr_node_ids;

	/* fill with the default distances */
	for (i = 0; i < numa_distance_cnt; i++)
		for (j = 0; j < numa_distance_cnt; j++)
			numa_distance[i * numa_distance_cnt + j] = i == j ?
				LOCAL_DISTANCE : REMOTE_DISTANCE;

Kefeng Wang's avatar
Kefeng Wang committed
295
	pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
296 297 298 299 300

	return 0;
}

/**
301
 * numa_set_distance() - Set inter node NUMA distance from node to node.
302 303 304 305 306 307 308 309 310 311 312 313 314
 * @from: the 'from' node to set distance
 * @to: the 'to'  node to set distance
 * @distance: NUMA distance
 *
 * Set the distance from node @from to @to to @distance.
 * If distance table doesn't exist, a warning is printed.
 *
 * If @from or @to is higher than the highest known node or lower than zero
 * or @distance doesn't make sense, the call is ignored.
 */
void __init numa_set_distance(int from, int to, int distance)
{
	if (!numa_distance) {
Kefeng Wang's avatar
Kefeng Wang committed
315
		pr_warn_once("Warning: distance table not allocated yet\n");
316 317 318 319 320
		return;
	}

	if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
			from < 0 || to < 0) {
Kefeng Wang's avatar
Kefeng Wang committed
321
		pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
322 323 324 325 326 327
			    from, to, distance);
		return;
	}

	if ((u8)distance != distance ||
	    (from == to && distance != LOCAL_DISTANCE)) {
Kefeng Wang's avatar
Kefeng Wang committed
328
		pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
329 330 331 332 333 334 335
			     from, to, distance);
		return;
	}

	numa_distance[from * numa_distance_cnt + to] = distance;
}

336
/*
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
 * Return NUMA distance @from to @to
 */
int __node_distance(int from, int to)
{
	if (from >= numa_distance_cnt || to >= numa_distance_cnt)
		return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
	return numa_distance[from * numa_distance_cnt + to];
}
EXPORT_SYMBOL(__node_distance);

static int __init numa_register_nodes(void)
{
	int nid;
	struct memblock_region *mblk;

	/* Check that valid nid is set to memblks */
	for_each_memblock(memory, mblk)
		if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
Kefeng Wang's avatar
Kefeng Wang committed
355
			pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
				mblk->nid, mblk->base,
				mblk->base + mblk->size - 1);
			return -EINVAL;
		}

	/* Finally register nodes. */
	for_each_node_mask(nid, numa_nodes_parsed) {
		unsigned long start_pfn, end_pfn;

		get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
		setup_node_data(nid, start_pfn, end_pfn);
		node_set_online(nid);
	}

	/* Setup online nodes to actual nodes*/
	node_possible_map = numa_nodes_parsed;

	return 0;
}

static int __init numa_init(int (*init_func)(void))
{
	int ret;

	nodes_clear(numa_nodes_parsed);
	nodes_clear(node_possible_map);
	nodes_clear(node_online_map);

	ret = numa_alloc_distance();
	if (ret < 0)
		return ret;

	ret = init_func();
	if (ret < 0)
390
		goto out_free_distance;
391

392 393
	if (nodes_empty(numa_nodes_parsed)) {
		pr_info("No NUMA configuration found\n");
394 395
		ret = -EINVAL;
		goto out_free_distance;
396
	}
397 398 399

	ret = numa_register_nodes();
	if (ret < 0)
400
		goto out_free_distance;
401 402 403 404

	setup_node_to_cpumask_map();

	return 0;
405 406 407
out_free_distance:
	numa_free_distance();
	return ret;
408 409 410
}

/**
411
 * dummy_numa_init() - Fallback dummy NUMA init
412 413 414 415 416 417
 *
 * Used if there's no underlying NUMA architecture, NUMA initialization
 * fails, or NUMA is disabled on the command line.
 *
 * Must online at least one node (node 0) and add memory blocks that cover all
 * allowed memory. It is unlikely that this function fails.
418 419
 *
 * Return: 0 on success, -errno on failure.
420 421 422 423 424 425
 */
static int __init dummy_numa_init(void)
{
	int ret;
	struct memblock_region *mblk;

426 427
	if (numa_off)
		pr_info("NUMA disabled\n"); /* Forced off on command line. */
Kefeng Wang's avatar
Kefeng Wang committed
428
	pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
429
		memblock_start_of_DRAM(), memblock_end_of_DRAM() - 1);
430 431

	for_each_memblock(memory, mblk) {
432
		ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
433 434 435 436 437 438 439
		if (!ret)
			continue;

		pr_err("NUMA init failed\n");
		return ret;
	}

440
	numa_off = true;
441 442 443 444
	return 0;
}

/**
445
 * arm64_numa_init() - Initialize NUMA
446
 *
447
 * Try each configured NUMA initialization method until one succeeds. The
448 449 450 451 452
 * last fallback is dummy single node config encomapssing whole memory.
 */
void __init arm64_numa_init(void)
{
	if (!numa_off) {
453 454 455
		if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
			return;
		if (acpi_disabled && !numa_init(of_numa_init))
456 457 458 459 460
			return;
	}

	numa_init(dummy_numa_init);
}
461 462 463 464 465 466 467 468 469 470

/*
 * We hope that we will be hotplugging memory on nodes we already know about,
 * such that acpi_get_node() succeeds and we never fall back to this...
 */
int memory_add_physaddr_to_nid(u64 addr)
{
	pr_warn("Unknown node for memory at 0x%llx, assuming node 0\n", addr);
	return 0;
}