xref: /openbmc/linux/arch/x86/mm/numa.c (revision d32fd6bb9f2bc8178cdd65ebec1ad670a8bfa241)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
271ee73e7SRusty Russell /* Common code for 32 and 64-bit NUMA */
3e84025e2SDavid Daney #include <linux/acpi.h>
4a4106eaeSTejun Heo #include <linux/kernel.h>
5a4106eaeSTejun Heo #include <linux/mm.h>
6a4106eaeSTejun Heo #include <linux/string.h>
7a4106eaeSTejun Heo #include <linux/init.h>
8a4106eaeSTejun Heo #include <linux/memblock.h>
9a4106eaeSTejun Heo #include <linux/mmzone.h>
10a4106eaeSTejun Heo #include <linux/ctype.h>
11a4106eaeSTejun Heo #include <linux/nodemask.h>
12a4106eaeSTejun Heo #include <linux/sched.h>
13a4106eaeSTejun Heo #include <linux/topology.h>
14eb38c5edSAlison Schofield #include <linux/sort.h>
15a4106eaeSTejun Heo 
1666441bd3SIngo Molnar #include <asm/e820/api.h>
17a4106eaeSTejun Heo #include <asm/proto.h>
18a4106eaeSTejun Heo #include <asm/dma.h>
19a4106eaeSTejun Heo #include <asm/amd_nb.h>
20a4106eaeSTejun Heo 
21a4106eaeSTejun Heo #include "numa_internal.h"
2290321602SJan Beulich 
23aec03f89SBoris Ostrovsky int numa_off;
24e6df595bSTejun Heo nodemask_t numa_nodes_parsed __initdata;
2590321602SJan Beulich 
26a4106eaeSTejun Heo struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
27a4106eaeSTejun Heo EXPORT_SYMBOL(node_data);
28a4106eaeSTejun Heo 
291e5d8e1eSDan Williams static struct numa_meminfo numa_meminfo __initdata_or_meminfo;
305d30f92eSDan Williams static struct numa_meminfo numa_reserved_meminfo __initdata_or_meminfo;
31a4106eaeSTejun Heo 
32a4106eaeSTejun Heo static int numa_distance_cnt;
33a4106eaeSTejun Heo static u8 *numa_distance;
34a4106eaeSTejun Heo 
numa_setup(char * opt)3590321602SJan Beulich static __init int numa_setup(char *opt)
3690321602SJan Beulich {
3790321602SJan Beulich 	if (!opt)
3890321602SJan Beulich 		return -EINVAL;
3990321602SJan Beulich 	if (!strncmp(opt, "off", 3))
4090321602SJan Beulich 		numa_off = 1;
4190321602SJan Beulich 	if (!strncmp(opt, "fake=", 5))
422dd57d34SDan Williams 		return numa_emu_cmdline(opt + 5);
4390321602SJan Beulich 	if (!strncmp(opt, "noacpi", 6))
442dd57d34SDan Williams 		disable_srat();
453b0d3101SDan Williams 	if (!strncmp(opt, "nohmat", 6))
463b0d3101SDan Williams 		disable_hmat();
4790321602SJan Beulich 	return 0;
4890321602SJan Beulich }
4990321602SJan Beulich early_param("numa", numa_setup);
5071ee73e7SRusty Russell 
5171ee73e7SRusty Russell /*
52bbc9e2f4STejun Heo  * apicid, cpu, node mappings
5371ee73e7SRusty Russell  */
54c4c60524SWen Congyang s16 __apicid_to_node[MAX_LOCAL_APIC] = {
55bbc9e2f4STejun Heo 	[0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
56bbc9e2f4STejun Heo };
57bbc9e2f4STejun Heo 
numa_cpu_node(int cpu)58148f9bb8SPaul Gortmaker int numa_cpu_node(int cpu)
596bd26273STejun Heo {
606bd26273STejun Heo 	int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
616bd26273STejun Heo 
626bd26273STejun Heo 	if (apicid != BAD_APICID)
636bd26273STejun Heo 		return __apicid_to_node[apicid];
646bd26273STejun Heo 	return NUMA_NO_NODE;
656bd26273STejun Heo }
666bd26273STejun Heo 
67c032ef60SRusty Russell cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
6871ee73e7SRusty Russell EXPORT_SYMBOL(node_to_cpumask_map);
6971ee73e7SRusty Russell 
7071ee73e7SRusty Russell /*
71645a7919STejun Heo  * Map cpu index to node index
72645a7919STejun Heo  */
73645a7919STejun Heo DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
74645a7919STejun Heo EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
75645a7919STejun Heo 
numa_set_node(int cpu,int node)76e13fe869SWen Congyang void numa_set_node(int cpu, int node)
77645a7919STejun Heo {
78645a7919STejun Heo 	int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
79645a7919STejun Heo 
80645a7919STejun Heo 	/* early setting, no percpu area yet */
81645a7919STejun Heo 	if (cpu_to_node_map) {
82645a7919STejun Heo 		cpu_to_node_map[cpu] = node;
83645a7919STejun Heo 		return;
84645a7919STejun Heo 	}
85645a7919STejun Heo 
86645a7919STejun Heo #ifdef CONFIG_DEBUG_PER_CPU_MAPS
87645a7919STejun Heo 	if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
88645a7919STejun Heo 		printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
89645a7919STejun Heo 		dump_stack();
90645a7919STejun Heo 		return;
91645a7919STejun Heo 	}
92645a7919STejun Heo #endif
93645a7919STejun Heo 	per_cpu(x86_cpu_to_node_map, cpu) = node;
94645a7919STejun Heo 
95645a7919STejun Heo 	set_cpu_numa_node(cpu, node);
96645a7919STejun Heo }
97645a7919STejun Heo 
numa_clear_node(int cpu)98e13fe869SWen Congyang void numa_clear_node(int cpu)
99645a7919STejun Heo {
100645a7919STejun Heo 	numa_set_node(cpu, NUMA_NO_NODE);
101645a7919STejun Heo }
102645a7919STejun Heo 
103645a7919STejun Heo /*
10471ee73e7SRusty Russell  * Allocate node_to_cpumask_map based on number of available nodes
10571ee73e7SRusty Russell  * Requires node_possible_map to be valid.
10671ee73e7SRusty Russell  *
1079512938bSWanlong Gao  * Note: cpumask_of_node() is not valid until after this is done.
10871ee73e7SRusty Russell  * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
10971ee73e7SRusty Russell  */
setup_node_to_cpumask_map(void)11071ee73e7SRusty Russell void __init setup_node_to_cpumask_map(void)
11171ee73e7SRusty Russell {
112d2ad351eSCody P Schafer 	unsigned int node;
11371ee73e7SRusty Russell 
11471ee73e7SRusty Russell 	/* setup nr_node_ids if not done yet */
115d2ad351eSCody P Schafer 	if (nr_node_ids == MAX_NUMNODES)
116d2ad351eSCody P Schafer 		setup_nr_node_ids();
11771ee73e7SRusty Russell 
11871ee73e7SRusty Russell 	/* allocate the map */
119c032ef60SRusty Russell 	for (node = 0; node < nr_node_ids; node++)
120c032ef60SRusty Russell 		alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
12171ee73e7SRusty Russell 
122c032ef60SRusty Russell 	/* cpumask_of_node() will now work */
123b9726c26SAlexey Dobriyan 	pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
12471ee73e7SRusty Russell }
12571ee73e7SRusty Russell 
numa_add_memblk_to(int nid,u64 start,u64 end,struct numa_meminfo * mi)126a4106eaeSTejun Heo static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
127a4106eaeSTejun Heo 				     struct numa_meminfo *mi)
128a4106eaeSTejun Heo {
129a4106eaeSTejun Heo 	/* ignore zero length blks */
130a4106eaeSTejun Heo 	if (start == end)
131a4106eaeSTejun Heo 		return 0;
132a4106eaeSTejun Heo 
133a4106eaeSTejun Heo 	/* whine about and ignore invalid blks */
134a4106eaeSTejun Heo 	if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
1351de392f5SJoe Perches 		pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
136365811d6SBjorn Helgaas 			nid, start, end - 1);
137a4106eaeSTejun Heo 		return 0;
138a4106eaeSTejun Heo 	}
139a4106eaeSTejun Heo 
140a4106eaeSTejun Heo 	if (mi->nr_blks >= NR_NODE_MEMBLKS) {
1411de392f5SJoe Perches 		pr_err("too many memblk ranges\n");
142a4106eaeSTejun Heo 		return -EINVAL;
143a4106eaeSTejun Heo 	}
144a4106eaeSTejun Heo 
145a4106eaeSTejun Heo 	mi->blk[mi->nr_blks].start = start;
146a4106eaeSTejun Heo 	mi->blk[mi->nr_blks].end = end;
147a4106eaeSTejun Heo 	mi->blk[mi->nr_blks].nid = nid;
148a4106eaeSTejun Heo 	mi->nr_blks++;
149a4106eaeSTejun Heo 	return 0;
150a4106eaeSTejun Heo }
151a4106eaeSTejun Heo 
152a4106eaeSTejun Heo /**
153a4106eaeSTejun Heo  * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
154a4106eaeSTejun Heo  * @idx: Index of memblk to remove
155a4106eaeSTejun Heo  * @mi: numa_meminfo to remove memblk from
156a4106eaeSTejun Heo  *
157a4106eaeSTejun Heo  * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
158a4106eaeSTejun Heo  * decrementing @mi->nr_blks.
159a4106eaeSTejun Heo  */
numa_remove_memblk_from(int idx,struct numa_meminfo * mi)160a4106eaeSTejun Heo void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
161a4106eaeSTejun Heo {
162a4106eaeSTejun Heo 	mi->nr_blks--;
163a4106eaeSTejun Heo 	memmove(&mi->blk[idx], &mi->blk[idx + 1],
164a4106eaeSTejun Heo 		(mi->nr_blks - idx) * sizeof(mi->blk[0]));
165a4106eaeSTejun Heo }
166a4106eaeSTejun Heo 
167a4106eaeSTejun Heo /**
1685d30f92eSDan Williams  * numa_move_tail_memblk - Move a numa_memblk from one numa_meminfo to another
1695d30f92eSDan Williams  * @dst: numa_meminfo to append block to
1705d30f92eSDan Williams  * @idx: Index of memblk to remove
1715d30f92eSDan Williams  * @src: numa_meminfo to remove memblk from
1725d30f92eSDan Williams  */
numa_move_tail_memblk(struct numa_meminfo * dst,int idx,struct numa_meminfo * src)1735d30f92eSDan Williams static void __init numa_move_tail_memblk(struct numa_meminfo *dst, int idx,
1745d30f92eSDan Williams 					 struct numa_meminfo *src)
1755d30f92eSDan Williams {
1765d30f92eSDan Williams 	dst->blk[dst->nr_blks++] = src->blk[idx];
1775d30f92eSDan Williams 	numa_remove_memblk_from(idx, src);
1785d30f92eSDan Williams }
1795d30f92eSDan Williams 
1805d30f92eSDan Williams /**
181a4106eaeSTejun Heo  * numa_add_memblk - Add one numa_memblk to numa_meminfo
182a4106eaeSTejun Heo  * @nid: NUMA node ID of the new memblk
183a4106eaeSTejun Heo  * @start: Start address of the new memblk
184a4106eaeSTejun Heo  * @end: End address of the new memblk
185a4106eaeSTejun Heo  *
186a4106eaeSTejun Heo  * Add a new memblk to the default numa_meminfo.
187a4106eaeSTejun Heo  *
188a4106eaeSTejun Heo  * RETURNS:
189a4106eaeSTejun Heo  * 0 on success, -errno on failure.
190a4106eaeSTejun Heo  */
numa_add_memblk(int nid,u64 start,u64 end)191a4106eaeSTejun Heo int __init numa_add_memblk(int nid, u64 start, u64 end)
192a4106eaeSTejun Heo {
193a4106eaeSTejun Heo 	return numa_add_memblk_to(nid, start, end, &numa_meminfo);
194a4106eaeSTejun Heo }
195a4106eaeSTejun Heo 
1968b375f64SLuiz Capitulino /* Allocate NODE_DATA for a node on the local memory */
alloc_node_data(int nid)1978b375f64SLuiz Capitulino static void __init alloc_node_data(int nid)
198a4106eaeSTejun Heo {
199a4106eaeSTejun Heo 	const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
20038f3e1caSTejun Heo 	u64 nd_pa;
2017888e96bSTejun Heo 	void *nd;
202a4106eaeSTejun Heo 	int tnid;
203a4106eaeSTejun Heo 
204a4106eaeSTejun Heo 	/*
20507f4207aSH. Peter Anvin 	 * Allocate node data.  Try node-local memory and then any node.
20607f4207aSH. Peter Anvin 	 * Never allocate in DMA zone.
207a4106eaeSTejun Heo 	 */
20842b46aefSMike Rapoport 	nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
209f3d815cbSLans Zhang 	if (!nd_pa) {
21043dac8f6SWei Yang 		pr_err("Cannot find %zu bytes in any node (initial node: %d)\n",
21120e6926dSYinghai Lu 		       nd_size, nid);
212a4106eaeSTejun Heo 		return;
213a4106eaeSTejun Heo 	}
2147888e96bSTejun Heo 	nd = __va(nd_pa);
215a4106eaeSTejun Heo 
216a4106eaeSTejun Heo 	/* report and initialize */
2178b375f64SLuiz Capitulino 	printk(KERN_INFO "NODE_DATA(%d) allocated [mem %#010Lx-%#010Lx]\n", nid,
21807f4207aSH. Peter Anvin 	       nd_pa, nd_pa + nd_size - 1);
219a4106eaeSTejun Heo 	tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
22007f4207aSH. Peter Anvin 	if (tnid != nid)
221a4106eaeSTejun Heo 		printk(KERN_INFO "    NODE_DATA(%d) on node %d\n", nid, tnid);
222a4106eaeSTejun Heo 
2237888e96bSTejun Heo 	node_data[nid] = nd;
224a4106eaeSTejun Heo 	memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
225a4106eaeSTejun Heo 
226a4106eaeSTejun Heo 	node_set_online(nid);
227a4106eaeSTejun Heo }
228a4106eaeSTejun Heo 
229a4106eaeSTejun Heo /**
230a4106eaeSTejun Heo  * numa_cleanup_meminfo - Cleanup a numa_meminfo
231a4106eaeSTejun Heo  * @mi: numa_meminfo to clean up
232a4106eaeSTejun Heo  *
23343dac8f6SWei Yang  * Sanitize @mi by merging and removing unnecessary memblks.  Also check for
234a4106eaeSTejun Heo  * conflicts and clear unused memblks.
235a4106eaeSTejun Heo  *
236a4106eaeSTejun Heo  * RETURNS:
237a4106eaeSTejun Heo  * 0 on success, -errno on failure.
238a4106eaeSTejun Heo  */
numa_cleanup_meminfo(struct numa_meminfo * mi)239a4106eaeSTejun Heo int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
240a4106eaeSTejun Heo {
241a4106eaeSTejun Heo 	const u64 low = 0;
24238f3e1caSTejun Heo 	const u64 high = PFN_PHYS(max_pfn);
243a4106eaeSTejun Heo 	int i, j, k;
244a4106eaeSTejun Heo 
245e5a10c1bSYinghai Lu 	/* first, trim all entries */
246a4106eaeSTejun Heo 	for (i = 0; i < mi->nr_blks; i++) {
247a4106eaeSTejun Heo 		struct numa_memblk *bi = &mi->blk[i];
248a4106eaeSTejun Heo 
2495d30f92eSDan Williams 		/* move / save reserved memory ranges */
2505d30f92eSDan Williams 		if (!memblock_overlaps_region(&memblock.memory,
2515d30f92eSDan Williams 					bi->start, bi->end - bi->start)) {
2525d30f92eSDan Williams 			numa_move_tail_memblk(&numa_reserved_meminfo, i--, mi);
2535d30f92eSDan Williams 			continue;
2545d30f92eSDan Williams 		}
2555d30f92eSDan Williams 
2565d30f92eSDan Williams 		/* make sure all non-reserved blocks are inside the limits */
257a4106eaeSTejun Heo 		bi->start = max(bi->start, low);
25828e5e44aSFan Du 
25928e5e44aSFan Du 		/* preserve info for non-RAM areas above 'max_pfn': */
26028e5e44aSFan Du 		if (bi->end > high) {
26128e5e44aSFan Du 			numa_add_memblk_to(bi->nid, high, bi->end,
26228e5e44aSFan Du 					   &numa_reserved_meminfo);
26328e5e44aSFan Du 			bi->end = high;
26428e5e44aSFan Du 		}
265a4106eaeSTejun Heo 
2665d30f92eSDan Williams 		/* and there's no empty block */
2675d30f92eSDan Williams 		if (bi->start >= bi->end)
268a4106eaeSTejun Heo 			numa_remove_memblk_from(i--, mi);
269a4106eaeSTejun Heo 	}
270a4106eaeSTejun Heo 
271e5a10c1bSYinghai Lu 	/* merge neighboring / overlapping entries */
272e5a10c1bSYinghai Lu 	for (i = 0; i < mi->nr_blks; i++) {
273e5a10c1bSYinghai Lu 		struct numa_memblk *bi = &mi->blk[i];
274e5a10c1bSYinghai Lu 
275a4106eaeSTejun Heo 		for (j = i + 1; j < mi->nr_blks; j++) {
276a4106eaeSTejun Heo 			struct numa_memblk *bj = &mi->blk[j];
27738f3e1caSTejun Heo 			u64 start, end;
278a4106eaeSTejun Heo 
279a4106eaeSTejun Heo 			/*
280a4106eaeSTejun Heo 			 * See whether there are overlapping blocks.  Whine
281a4106eaeSTejun Heo 			 * about but allow overlaps of the same nid.  They
282a4106eaeSTejun Heo 			 * will be merged below.
283a4106eaeSTejun Heo 			 */
284a4106eaeSTejun Heo 			if (bi->end > bj->start && bi->start < bj->end) {
285a4106eaeSTejun Heo 				if (bi->nid != bj->nid) {
2861de392f5SJoe Perches 					pr_err("node %d [mem %#010Lx-%#010Lx] overlaps with node %d [mem %#010Lx-%#010Lx]\n",
287365811d6SBjorn Helgaas 					       bi->nid, bi->start, bi->end - 1,
288365811d6SBjorn Helgaas 					       bj->nid, bj->start, bj->end - 1);
289a4106eaeSTejun Heo 					return -EINVAL;
290a4106eaeSTejun Heo 				}
2911de392f5SJoe Perches 				pr_warn("Warning: node %d [mem %#010Lx-%#010Lx] overlaps with itself [mem %#010Lx-%#010Lx]\n",
292365811d6SBjorn Helgaas 					bi->nid, bi->start, bi->end - 1,
293365811d6SBjorn Helgaas 					bj->start, bj->end - 1);
294a4106eaeSTejun Heo 			}
295a4106eaeSTejun Heo 
296a4106eaeSTejun Heo 			/*
297a4106eaeSTejun Heo 			 * Join together blocks on the same node, holes
298a4106eaeSTejun Heo 			 * between which don't overlap with memory on other
299a4106eaeSTejun Heo 			 * nodes.
300a4106eaeSTejun Heo 			 */
301a4106eaeSTejun Heo 			if (bi->nid != bj->nid)
302a4106eaeSTejun Heo 				continue;
303e5a10c1bSYinghai Lu 			start = min(bi->start, bj->start);
304e5a10c1bSYinghai Lu 			end = max(bi->end, bj->end);
305a4106eaeSTejun Heo 			for (k = 0; k < mi->nr_blks; k++) {
306a4106eaeSTejun Heo 				struct numa_memblk *bk = &mi->blk[k];
307a4106eaeSTejun Heo 
308a4106eaeSTejun Heo 				if (bi->nid == bk->nid)
309a4106eaeSTejun Heo 					continue;
310a4106eaeSTejun Heo 				if (start < bk->end && end > bk->start)
311a4106eaeSTejun Heo 					break;
312a4106eaeSTejun Heo 			}
313a4106eaeSTejun Heo 			if (k < mi->nr_blks)
314a4106eaeSTejun Heo 				continue;
315365811d6SBjorn Helgaas 			printk(KERN_INFO "NUMA: Node %d [mem %#010Lx-%#010Lx] + [mem %#010Lx-%#010Lx] -> [mem %#010Lx-%#010Lx]\n",
316365811d6SBjorn Helgaas 			       bi->nid, bi->start, bi->end - 1, bj->start,
317365811d6SBjorn Helgaas 			       bj->end - 1, start, end - 1);
318a4106eaeSTejun Heo 			bi->start = start;
319a4106eaeSTejun Heo 			bi->end = end;
320a4106eaeSTejun Heo 			numa_remove_memblk_from(j--, mi);
321a4106eaeSTejun Heo 		}
322a4106eaeSTejun Heo 	}
323a4106eaeSTejun Heo 
324e5a10c1bSYinghai Lu 	/* clear unused ones */
325a4106eaeSTejun Heo 	for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
326a4106eaeSTejun Heo 		mi->blk[i].start = mi->blk[i].end = 0;
327a4106eaeSTejun Heo 		mi->blk[i].nid = NUMA_NO_NODE;
328a4106eaeSTejun Heo 	}
329a4106eaeSTejun Heo 
330a4106eaeSTejun Heo 	return 0;
331a4106eaeSTejun Heo }
332a4106eaeSTejun Heo 
333b678c91aSThomas Gleixner /*
334b678c91aSThomas Gleixner  * Set nodes, which have memory in @mi, in *@nodemask.
335b678c91aSThomas Gleixner  */
numa_nodemask_from_meminfo(nodemask_t * nodemask,const struct numa_meminfo * mi)336b678c91aSThomas Gleixner static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
337b678c91aSThomas Gleixner 					      const struct numa_meminfo *mi)
338b678c91aSThomas Gleixner {
339b678c91aSThomas Gleixner 	int i;
340b678c91aSThomas Gleixner 
341b678c91aSThomas Gleixner 	for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
342b678c91aSThomas Gleixner 		if (mi->blk[i].start != mi->blk[i].end &&
343b678c91aSThomas Gleixner 		    mi->blk[i].nid != NUMA_NO_NODE)
344b678c91aSThomas Gleixner 			node_set(mi->blk[i].nid, *nodemask);
345b678c91aSThomas Gleixner }
346b678c91aSThomas Gleixner 
347a4106eaeSTejun Heo /**
348a4106eaeSTejun Heo  * numa_reset_distance - Reset NUMA distance table
349a4106eaeSTejun Heo  *
350a4106eaeSTejun Heo  * The current table is freed.  The next numa_set_distance() call will
351a4106eaeSTejun Heo  * create a new one.
352a4106eaeSTejun Heo  */
numa_reset_distance(void)353a4106eaeSTejun Heo void __init numa_reset_distance(void)
354a4106eaeSTejun Heo {
355a4106eaeSTejun Heo 	size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
356a4106eaeSTejun Heo 
357a4106eaeSTejun Heo 	/* numa_distance could be 1LU marking allocation failure, test cnt */
358a4106eaeSTejun Heo 	if (numa_distance_cnt)
3594421cca0SMike Rapoport 		memblock_free(numa_distance, size);
360a4106eaeSTejun Heo 	numa_distance_cnt = 0;
361a4106eaeSTejun Heo 	numa_distance = NULL;	/* enable table creation */
362a4106eaeSTejun Heo }
363a4106eaeSTejun Heo 
numa_alloc_distance(void)364a4106eaeSTejun Heo static int __init numa_alloc_distance(void)
365a4106eaeSTejun Heo {
366b678c91aSThomas Gleixner 	nodemask_t nodes_parsed;
367a4106eaeSTejun Heo 	size_t size;
368a4106eaeSTejun Heo 	int i, j, cnt = 0;
369a4106eaeSTejun Heo 	u64 phys;
370a4106eaeSTejun Heo 
371a4106eaeSTejun Heo 	/* size the new table and allocate it */
372b678c91aSThomas Gleixner 	nodes_parsed = numa_nodes_parsed;
373b678c91aSThomas Gleixner 	numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
374b678c91aSThomas Gleixner 
375b678c91aSThomas Gleixner 	for_each_node_mask(i, nodes_parsed)
376a4106eaeSTejun Heo 		cnt = i;
377a4106eaeSTejun Heo 	cnt++;
378a4106eaeSTejun Heo 	size = cnt * cnt * sizeof(numa_distance[0]);
379a4106eaeSTejun Heo 
380a7259df7SMike Rapoport 	phys = memblock_phys_alloc_range(size, PAGE_SIZE, 0,
381a7259df7SMike Rapoport 					 PFN_PHYS(max_pfn_mapped));
3821f5026a7STejun Heo 	if (!phys) {
3831de392f5SJoe Perches 		pr_warn("Warning: can't allocate distance table!\n");
384a4106eaeSTejun Heo 		/* don't retry until explicitly reset */
385a4106eaeSTejun Heo 		numa_distance = (void *)1LU;
386a4106eaeSTejun Heo 		return -ENOMEM;
387a4106eaeSTejun Heo 	}
388a4106eaeSTejun Heo 
389a4106eaeSTejun Heo 	numa_distance = __va(phys);
390a4106eaeSTejun Heo 	numa_distance_cnt = cnt;
391a4106eaeSTejun Heo 
392a4106eaeSTejun Heo 	/* fill with the default distances */
393a4106eaeSTejun Heo 	for (i = 0; i < cnt; i++)
394a4106eaeSTejun Heo 		for (j = 0; j < cnt; j++)
395a4106eaeSTejun Heo 			numa_distance[i * cnt + j] = i == j ?
396a4106eaeSTejun Heo 				LOCAL_DISTANCE : REMOTE_DISTANCE;
397a4106eaeSTejun Heo 	printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
398a4106eaeSTejun Heo 
399a4106eaeSTejun Heo 	return 0;
400a4106eaeSTejun Heo }
401a4106eaeSTejun Heo 
402a4106eaeSTejun Heo /**
403a4106eaeSTejun Heo  * numa_set_distance - Set NUMA distance from one NUMA to another
404a4106eaeSTejun Heo  * @from: the 'from' node to set distance
405a4106eaeSTejun Heo  * @to: the 'to'  node to set distance
406a4106eaeSTejun Heo  * @distance: NUMA distance
407a4106eaeSTejun Heo  *
408a4106eaeSTejun Heo  * Set the distance from node @from to @to to @distance.  If distance table
409a4106eaeSTejun Heo  * doesn't exist, one which is large enough to accommodate all the currently
410a4106eaeSTejun Heo  * known nodes will be created.
411a4106eaeSTejun Heo  *
412a4106eaeSTejun Heo  * If such table cannot be allocated, a warning is printed and further
413a4106eaeSTejun Heo  * calls are ignored until the distance table is reset with
414a4106eaeSTejun Heo  * numa_reset_distance().
415a4106eaeSTejun Heo  *
41654eed6cbSPetr Holasek  * If @from or @to is higher than the highest known node or lower than zero
41754eed6cbSPetr Holasek  * at the time of table creation or @distance doesn't make sense, the call
41854eed6cbSPetr Holasek  * is ignored.
419a4106eaeSTejun Heo  * This is to allow simplification of specific NUMA config implementations.
420a4106eaeSTejun Heo  */
numa_set_distance(int from,int to,int distance)421a4106eaeSTejun Heo void __init numa_set_distance(int from, int to, int distance)
422a4106eaeSTejun Heo {
423a4106eaeSTejun Heo 	if (!numa_distance && numa_alloc_distance() < 0)
424a4106eaeSTejun Heo 		return;
425a4106eaeSTejun Heo 
42654eed6cbSPetr Holasek 	if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
42754eed6cbSPetr Holasek 			from < 0 || to < 0) {
4281de392f5SJoe Perches 		pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
429a4106eaeSTejun Heo 			     from, to, distance);
430a4106eaeSTejun Heo 		return;
431a4106eaeSTejun Heo 	}
432a4106eaeSTejun Heo 
433a4106eaeSTejun Heo 	if ((u8)distance != distance ||
434a4106eaeSTejun Heo 	    (from == to && distance != LOCAL_DISTANCE)) {
4351de392f5SJoe Perches 		pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
436a4106eaeSTejun Heo 			     from, to, distance);
437a4106eaeSTejun Heo 		return;
438a4106eaeSTejun Heo 	}
439a4106eaeSTejun Heo 
440a4106eaeSTejun Heo 	numa_distance[from * numa_distance_cnt + to] = distance;
441a4106eaeSTejun Heo }
442a4106eaeSTejun Heo 
__node_distance(int from,int to)443a4106eaeSTejun Heo int __node_distance(int from, int to)
444a4106eaeSTejun Heo {
445a4106eaeSTejun Heo 	if (from >= numa_distance_cnt || to >= numa_distance_cnt)
446a4106eaeSTejun Heo 		return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
447a4106eaeSTejun Heo 	return numa_distance[from * numa_distance_cnt + to];
448a4106eaeSTejun Heo }
449a4106eaeSTejun Heo EXPORT_SYMBOL(__node_distance);
450a4106eaeSTejun Heo 
451a4106eaeSTejun Heo /*
452c1a0bf34SIngo Molnar  * Mark all currently memblock-reserved physical memory (which covers the
453c1a0bf34SIngo Molnar  * kernel's own memory ranges) as hot-unswappable.
454c1a0bf34SIngo Molnar  */
numa_clear_kernel_node_hotplug(void)455bd5cfb89SXishi Qiu static void __init numa_clear_kernel_node_hotplug(void)
456bd5cfb89SXishi Qiu {
457c1a0bf34SIngo Molnar 	nodemask_t reserved_nodemask = NODE_MASK_NONE;
458c1a0bf34SIngo Molnar 	struct memblock_region *mb_region;
459c1a0bf34SIngo Molnar 	int i;
460bd5cfb89SXishi Qiu 
461bd5cfb89SXishi Qiu 	/*
462c1a0bf34SIngo Molnar 	 * We have to do some preprocessing of memblock regions, to
463c1a0bf34SIngo Molnar 	 * make them suitable for reservation.
464c1a0bf34SIngo Molnar 	 *
465bd5cfb89SXishi Qiu 	 * At this time, all memory regions reserved by memblock are
466c1a0bf34SIngo Molnar 	 * used by the kernel, but those regions are not split up
467c1a0bf34SIngo Molnar 	 * along node boundaries yet, and don't necessarily have their
468c1a0bf34SIngo Molnar 	 * node ID set yet either.
469c1a0bf34SIngo Molnar 	 *
470c1a0bf34SIngo Molnar 	 * So iterate over all memory known to the x86 architecture,
471c1a0bf34SIngo Molnar 	 * and use those ranges to set the nid in memblock.reserved.
472c1a0bf34SIngo Molnar 	 * This will split up the memblock regions along node
473c1a0bf34SIngo Molnar 	 * boundaries and will set the node IDs as well.
474bd5cfb89SXishi Qiu 	 */
475bd5cfb89SXishi Qiu 	for (i = 0; i < numa_meminfo.nr_blks; i++) {
476c1a0bf34SIngo Molnar 		struct numa_memblk *mb = numa_meminfo.blk + i;
4775f7ee246SIngo Molnar 		int ret;
478bd5cfb89SXishi Qiu 
4795f7ee246SIngo Molnar 		ret = memblock_set_node(mb->start, mb->end - mb->start, &memblock.reserved, mb->nid);
4805f7ee246SIngo Molnar 		WARN_ON_ONCE(ret);
481bd5cfb89SXishi Qiu 	}
482bd5cfb89SXishi Qiu 
48322ef882eSDave Young 	/*
484c1a0bf34SIngo Molnar 	 * Now go over all reserved memblock regions, to construct a
485c1a0bf34SIngo Molnar 	 * node mask of all kernel reserved memory areas.
48622ef882eSDave Young 	 *
487c1a0bf34SIngo Molnar 	 * [ Note, when booting with mem=nn[kMG] or in a kdump kernel,
488c1a0bf34SIngo Molnar 	 *   numa_meminfo might not include all memblock.reserved
489c1a0bf34SIngo Molnar 	 *   memory ranges, because quirks such as trim_snb_memory()
490c1a0bf34SIngo Molnar 	 *   reserve specific pages for Sandy Bridge graphics. ]
49122ef882eSDave Young 	 */
492cc6de168SMike Rapoport 	for_each_reserved_mem_region(mb_region) {
493d622abf7SMike Rapoport 		int nid = memblock_get_region_node(mb_region);
494d622abf7SMike Rapoport 
495*e026530eSJan Beulich 		if (nid != NUMA_NO_NODE)
496d622abf7SMike Rapoport 			node_set(nid, reserved_nodemask);
497c1a0bf34SIngo Molnar 	}
498bd5cfb89SXishi Qiu 
499c1a0bf34SIngo Molnar 	/*
500c1a0bf34SIngo Molnar 	 * Finally, clear the MEMBLOCK_HOTPLUG flag for all memory
501c1a0bf34SIngo Molnar 	 * belonging to the reserved node mask.
502c1a0bf34SIngo Molnar 	 *
503c1a0bf34SIngo Molnar 	 * Note that this will include memory regions that reside
504c1a0bf34SIngo Molnar 	 * on nodes that contain kernel memory - entire nodes
505c1a0bf34SIngo Molnar 	 * become hot-unpluggable:
506c1a0bf34SIngo Molnar 	 */
507bd5cfb89SXishi Qiu 	for (i = 0; i < numa_meminfo.nr_blks; i++) {
508c1a0bf34SIngo Molnar 		struct numa_memblk *mb = numa_meminfo.blk + i;
509c1a0bf34SIngo Molnar 
510c1a0bf34SIngo Molnar 		if (!node_isset(mb->nid, reserved_nodemask))
511bd5cfb89SXishi Qiu 			continue;
512bd5cfb89SXishi Qiu 
513c1a0bf34SIngo Molnar 		memblock_clear_hotplug(mb->start, mb->end - mb->start);
514bd5cfb89SXishi Qiu 	}
515bd5cfb89SXishi Qiu }
516bd5cfb89SXishi Qiu 
numa_register_memblks(struct numa_meminfo * mi)517a4106eaeSTejun Heo static int __init numa_register_memblks(struct numa_meminfo *mi)
518a4106eaeSTejun Heo {
519a4106eaeSTejun Heo 	int i, nid;
520a4106eaeSTejun Heo 
521a4106eaeSTejun Heo 	/* Account for nodes with cpus and no memory */
522a4106eaeSTejun Heo 	node_possible_map = numa_nodes_parsed;
523b678c91aSThomas Gleixner 	numa_nodemask_from_meminfo(&node_possible_map, mi);
524a4106eaeSTejun Heo 	if (WARN_ON(nodes_empty(node_possible_map)))
525a4106eaeSTejun Heo 		return -EINVAL;
526a4106eaeSTejun Heo 
5270608f70cSTejun Heo 	for (i = 0; i < mi->nr_blks; i++) {
5280608f70cSTejun Heo 		struct numa_memblk *mb = &mi->blk[i];
529e7e8de59STang Chen 		memblock_set_node(mb->start, mb->end - mb->start,
530e7e8de59STang Chen 				  &memblock.memory, mb->nid);
5310608f70cSTejun Heo 	}
5321e01979cSTejun Heo 
5331e01979cSTejun Heo 	/*
534bd5cfb89SXishi Qiu 	 * At very early time, the kernel have to use some memory such as
535bd5cfb89SXishi Qiu 	 * loading the kernel image. We cannot prevent this anyway. So any
536bd5cfb89SXishi Qiu 	 * node the kernel resides in should be un-hotpluggable.
537bd5cfb89SXishi Qiu 	 *
538bd5cfb89SXishi Qiu 	 * And when we come here, alloc node data won't fail.
539bd5cfb89SXishi Qiu 	 */
540bd5cfb89SXishi Qiu 	numa_clear_kernel_node_hotplug();
541bd5cfb89SXishi Qiu 
542bd5cfb89SXishi Qiu 	/*
5431e01979cSTejun Heo 	 * If sections array is gonna be used for pfn -> nid mapping, check
5441e01979cSTejun Heo 	 * whether its granularity is fine enough.
5451e01979cSTejun Heo 	 */
546aecfd220SKees Cook 	if (IS_ENABLED(NODE_NOT_IN_PAGE_FLAGS)) {
547aecfd220SKees Cook 		unsigned long pfn_align = node_map_pfn_alignment();
548aecfd220SKees Cook 
5491e01979cSTejun Heo 		if (pfn_align && pfn_align < PAGES_PER_SECTION) {
550aecfd220SKees Cook 			pr_warn("Node alignment %LuMB < min %LuMB, rejecting NUMA config\n",
5511e01979cSTejun Heo 				PFN_PHYS(pfn_align) >> 20,
5521e01979cSTejun Heo 				PFN_PHYS(PAGES_PER_SECTION) >> 20);
5531e01979cSTejun Heo 			return -EINVAL;
5541e01979cSTejun Heo 		}
555aecfd220SKees Cook 	}
5566fdc7705SLiam Ni 
5576fdc7705SLiam Ni 	if (!memblock_validate_numa_coverage(SZ_1M))
558a4106eaeSTejun Heo 		return -EINVAL;
559a4106eaeSTejun Heo 
560a4106eaeSTejun Heo 	/* Finally register nodes. */
561a4106eaeSTejun Heo 	for_each_node_mask(nid, node_possible_map) {
56238f3e1caSTejun Heo 		u64 start = PFN_PHYS(max_pfn);
563a4106eaeSTejun Heo 		u64 end = 0;
564a4106eaeSTejun Heo 
565a4106eaeSTejun Heo 		for (i = 0; i < mi->nr_blks; i++) {
566a4106eaeSTejun Heo 			if (nid != mi->blk[i].nid)
567a4106eaeSTejun Heo 				continue;
568a4106eaeSTejun Heo 			start = min(mi->blk[i].start, start);
569a4106eaeSTejun Heo 			end = max(mi->blk[i].end, end);
570a4106eaeSTejun Heo 		}
571a4106eaeSTejun Heo 
5728b375f64SLuiz Capitulino 		if (start >= end)
5738b375f64SLuiz Capitulino 			continue;
5748b375f64SLuiz Capitulino 
5758b375f64SLuiz Capitulino 		alloc_node_data(nid);
576a4106eaeSTejun Heo 	}
577a4106eaeSTejun Heo 
5780608f70cSTejun Heo 	/* Dump memblock with node info and return. */
5790608f70cSTejun Heo 	memblock_dump_all();
580a4106eaeSTejun Heo 	return 0;
581a4106eaeSTejun Heo }
582a4106eaeSTejun Heo 
5838db78cc4STejun Heo /*
5848db78cc4STejun Heo  * There are unfortunately some poorly designed mainboards around that
5858db78cc4STejun Heo  * only connect memory to a single CPU. This breaks the 1:1 cpu->node
5868db78cc4STejun Heo  * mapping. To avoid this fill in the mapping for all possible CPUs,
5878db78cc4STejun Heo  * as the number of CPUs is not known yet. We round robin the existing
5888db78cc4STejun Heo  * nodes.
5898db78cc4STejun Heo  */
numa_init_array(void)590752d4f37STejun Heo static void __init numa_init_array(void)
5918db78cc4STejun Heo {
5928db78cc4STejun Heo 	int rr, i;
5938db78cc4STejun Heo 
5948db78cc4STejun Heo 	rr = first_node(node_online_map);
5958db78cc4STejun Heo 	for (i = 0; i < nr_cpu_ids; i++) {
5968db78cc4STejun Heo 		if (early_cpu_to_node(i) != NUMA_NO_NODE)
5978db78cc4STejun Heo 			continue;
5988db78cc4STejun Heo 		numa_set_node(i, rr);
5990edaf86cSAndrew Morton 		rr = next_node_in(rr, node_online_map);
6008db78cc4STejun Heo 	}
6018db78cc4STejun Heo }
6028db78cc4STejun Heo 
numa_init(int (* init_func)(void))603a4106eaeSTejun Heo static int __init numa_init(int (*init_func)(void))
604a4106eaeSTejun Heo {
605a4106eaeSTejun Heo 	int i;
606a4106eaeSTejun Heo 	int ret;
607a4106eaeSTejun Heo 
608a4106eaeSTejun Heo 	for (i = 0; i < MAX_LOCAL_APIC; i++)
609a4106eaeSTejun Heo 		set_apicid_to_node(i, NUMA_NO_NODE);
610a4106eaeSTejun Heo 
61120e6926dSYinghai Lu 	nodes_clear(numa_nodes_parsed);
612a4106eaeSTejun Heo 	nodes_clear(node_possible_map);
613a4106eaeSTejun Heo 	nodes_clear(node_online_map);
61420e6926dSYinghai Lu 	memset(&numa_meminfo, 0, sizeof(numa_meminfo));
615e7e8de59STang Chen 	WARN_ON(memblock_set_node(0, ULLONG_MAX, &memblock.memory,
616*e026530eSJan Beulich 				  NUMA_NO_NODE));
617a0acda91STang Chen 	WARN_ON(memblock_set_node(0, ULLONG_MAX, &memblock.reserved,
618*e026530eSJan Beulich 				  NUMA_NO_NODE));
61905d1d8cbSTang Chen 	/* In case that parsing SRAT failed. */
62005d1d8cbSTang Chen 	WARN_ON(memblock_clear_hotplug(0, ULLONG_MAX));
621a4106eaeSTejun Heo 	numa_reset_distance();
622a4106eaeSTejun Heo 
623a4106eaeSTejun Heo 	ret = init_func();
624a4106eaeSTejun Heo 	if (ret < 0)
625a4106eaeSTejun Heo 		return ret;
626c5320926STang Chen 
627c5320926STang Chen 	/*
628c5320926STang Chen 	 * We reset memblock back to the top-down direction
629c5320926STang Chen 	 * here because if we configured ACPI_NUMA, we have
630c5320926STang Chen 	 * parsed SRAT in init_func(). It is ok to have the
631c5320926STang Chen 	 * reset here even if we did't configure ACPI_NUMA
632c5320926STang Chen 	 * or acpi numa init fails and fallbacks to dummy
633c5320926STang Chen 	 * numa init.
634c5320926STang Chen 	 */
635c5320926STang Chen 	memblock_set_bottom_up(false);
636c5320926STang Chen 
637a4106eaeSTejun Heo 	ret = numa_cleanup_meminfo(&numa_meminfo);
638a4106eaeSTejun Heo 	if (ret < 0)
639a4106eaeSTejun Heo 		return ret;
640a4106eaeSTejun Heo 
641a4106eaeSTejun Heo 	numa_emulation(&numa_meminfo, numa_distance_cnt);
642a4106eaeSTejun Heo 
643a4106eaeSTejun Heo 	ret = numa_register_memblks(&numa_meminfo);
644a4106eaeSTejun Heo 	if (ret < 0)
645a4106eaeSTejun Heo 		return ret;
646a4106eaeSTejun Heo 
647a4106eaeSTejun Heo 	for (i = 0; i < nr_cpu_ids; i++) {
648a4106eaeSTejun Heo 		int nid = early_cpu_to_node(i);
649a4106eaeSTejun Heo 
650a4106eaeSTejun Heo 		if (nid == NUMA_NO_NODE)
651a4106eaeSTejun Heo 			continue;
652a4106eaeSTejun Heo 		if (!node_online(nid))
653a4106eaeSTejun Heo 			numa_clear_node(i);
654a4106eaeSTejun Heo 	}
655a4106eaeSTejun Heo 	numa_init_array();
656a0acda91STang Chen 
657a4106eaeSTejun Heo 	return 0;
658a4106eaeSTejun Heo }
659a4106eaeSTejun Heo 
660a4106eaeSTejun Heo /**
661a4106eaeSTejun Heo  * dummy_numa_init - Fallback dummy NUMA init
662a4106eaeSTejun Heo  *
663a4106eaeSTejun Heo  * Used if there's no underlying NUMA architecture, NUMA initialization
664a4106eaeSTejun Heo  * fails, or NUMA is disabled on the command line.
665a4106eaeSTejun Heo  *
666a4106eaeSTejun Heo  * Must online at least one node and add memory blocks that cover all
667a4106eaeSTejun Heo  * allowed memory.  This function must not fail.
668a4106eaeSTejun Heo  */
dummy_numa_init(void)669a4106eaeSTejun Heo static int __init dummy_numa_init(void)
670a4106eaeSTejun Heo {
671a4106eaeSTejun Heo 	printk(KERN_INFO "%s\n",
672a4106eaeSTejun Heo 	       numa_off ? "NUMA turned off" : "No NUMA configuration found");
673365811d6SBjorn Helgaas 	printk(KERN_INFO "Faking a node at [mem %#018Lx-%#018Lx]\n",
674365811d6SBjorn Helgaas 	       0LLU, PFN_PHYS(max_pfn) - 1);
675a4106eaeSTejun Heo 
676a4106eaeSTejun Heo 	node_set(0, numa_nodes_parsed);
67738f3e1caSTejun Heo 	numa_add_memblk(0, 0, PFN_PHYS(max_pfn));
678a4106eaeSTejun Heo 
679a4106eaeSTejun Heo 	return 0;
680a4106eaeSTejun Heo }
681a4106eaeSTejun Heo 
682a4106eaeSTejun Heo /**
683a4106eaeSTejun Heo  * x86_numa_init - Initialize NUMA
684a4106eaeSTejun Heo  *
685a4106eaeSTejun Heo  * Try each configured NUMA initialization method until one succeeds.  The
68611a98f37SCao jin  * last fallback is dummy single node config encompassing whole memory and
687a4106eaeSTejun Heo  * never fails.
688a4106eaeSTejun Heo  */
x86_numa_init(void)689a4106eaeSTejun Heo void __init x86_numa_init(void)
690a4106eaeSTejun Heo {
691a4106eaeSTejun Heo 	if (!numa_off) {
692a4106eaeSTejun Heo #ifdef CONFIG_ACPI_NUMA
693a4106eaeSTejun Heo 		if (!numa_init(x86_acpi_numa_init))
694a4106eaeSTejun Heo 			return;
695a4106eaeSTejun Heo #endif
696a4106eaeSTejun Heo #ifdef CONFIG_AMD_NUMA
697a4106eaeSTejun Heo 		if (!numa_init(amd_numa_init))
698a4106eaeSTejun Heo 			return;
699a4106eaeSTejun Heo #endif
700a4106eaeSTejun Heo 	}
701a4106eaeSTejun Heo 
702a4106eaeSTejun Heo 	numa_init(dummy_numa_init);
703a4106eaeSTejun Heo }
704a4106eaeSTejun Heo 
7058db78cc4STejun Heo 
7068db78cc4STejun Heo /*
70773bf7382SJonathan Cameron  * A node may exist which has one or more Generic Initiators but no CPUs and no
70873bf7382SJonathan Cameron  * memory.
70973bf7382SJonathan Cameron  *
71073bf7382SJonathan Cameron  * This function must be called after init_cpu_to_node(), to ensure that any
71173bf7382SJonathan Cameron  * memoryless CPU nodes have already been brought online, and before the
71273bf7382SJonathan Cameron  * node_data[nid] is needed for zone list setup in build_all_zonelists().
71373bf7382SJonathan Cameron  *
71473bf7382SJonathan Cameron  * When this function is called, any nodes containing either memory and/or CPUs
71573bf7382SJonathan Cameron  * will already be online and there is no need to do anything extra, even if
71673bf7382SJonathan Cameron  * they also contain one or more Generic Initiators.
71773bf7382SJonathan Cameron  */
init_gi_nodes(void)71873bf7382SJonathan Cameron void __init init_gi_nodes(void)
71973bf7382SJonathan Cameron {
72073bf7382SJonathan Cameron 	int nid;
72173bf7382SJonathan Cameron 
7221ca75fa7SOscar Salvador 	/*
7231ca75fa7SOscar Salvador 	 * Exclude this node from
7241ca75fa7SOscar Salvador 	 * bringup_nonboot_cpus
7251ca75fa7SOscar Salvador 	 *  cpu_up
7261ca75fa7SOscar Salvador 	 *   __try_online_node
7271ca75fa7SOscar Salvador 	 *    register_one_node
7281ca75fa7SOscar Salvador 	 * because node_subsys is not initialized yet.
7291ca75fa7SOscar Salvador 	 * TODO remove dependency on node_online
7301ca75fa7SOscar Salvador 	 */
73173bf7382SJonathan Cameron 	for_each_node_state(nid, N_GENERIC_INITIATOR)
73273bf7382SJonathan Cameron 		if (!node_online(nid))
7331ca75fa7SOscar Salvador 			node_set_online(nid);
73473bf7382SJonathan Cameron }
73573bf7382SJonathan Cameron 
73673bf7382SJonathan Cameron /*
7378db78cc4STejun Heo  * Setup early cpu_to_node.
7388db78cc4STejun Heo  *
7398db78cc4STejun Heo  * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
7408db78cc4STejun Heo  * and apicid_to_node[] tables have valid entries for a CPU.
7418db78cc4STejun Heo  * This means we skip cpu_to_node[] initialisation for NUMA
7428db78cc4STejun Heo  * emulation and faking node case (when running a kernel compiled
7438db78cc4STejun Heo  * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
7448db78cc4STejun Heo  * is already initialized in a round robin manner at numa_init_array,
7458db78cc4STejun Heo  * prior to this call, and this initialization is good enough
7468db78cc4STejun Heo  * for the fake NUMA cases.
7478db78cc4STejun Heo  *
7488db78cc4STejun Heo  * Called before the per_cpu areas are setup.
7498db78cc4STejun Heo  */
init_cpu_to_node(void)7508db78cc4STejun Heo void __init init_cpu_to_node(void)
7518db78cc4STejun Heo {
7528db78cc4STejun Heo 	int cpu;
7538db78cc4STejun Heo 	u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
7548db78cc4STejun Heo 
7558db78cc4STejun Heo 	BUG_ON(cpu_to_apicid == NULL);
7568db78cc4STejun Heo 
7578db78cc4STejun Heo 	for_each_possible_cpu(cpu) {
7588db78cc4STejun Heo 		int node = numa_cpu_node(cpu);
7598db78cc4STejun Heo 
7608db78cc4STejun Heo 		if (node == NUMA_NO_NODE)
7618db78cc4STejun Heo 			continue;
7622532fc31STang Chen 
7631ca75fa7SOscar Salvador 		/*
7641ca75fa7SOscar Salvador 		 * Exclude this node from
7651ca75fa7SOscar Salvador 		 * bringup_nonboot_cpus
7661ca75fa7SOscar Salvador 		 *  cpu_up
7671ca75fa7SOscar Salvador 		 *   __try_online_node
7681ca75fa7SOscar Salvador 		 *    register_one_node
7691ca75fa7SOscar Salvador 		 * because node_subsys is not initialized yet.
7701ca75fa7SOscar Salvador 		 * TODO remove dependency on node_online
7711ca75fa7SOscar Salvador 		 */
7728db78cc4STejun Heo 		if (!node_online(node))
7731ca75fa7SOscar Salvador 			node_set_online(node);
7742532fc31STang Chen 
7758db78cc4STejun Heo 		numa_set_node(cpu, node);
7768db78cc4STejun Heo 	}
7778db78cc4STejun Heo }
7788db78cc4STejun Heo 
779de2d9445STejun Heo #ifndef CONFIG_DEBUG_PER_CPU_MAPS
780de2d9445STejun Heo 
781de2d9445STejun Heo # ifndef CONFIG_NUMA_EMU
numa_add_cpu(int cpu)782148f9bb8SPaul Gortmaker void numa_add_cpu(int cpu)
783de2d9445STejun Heo {
784de2d9445STejun Heo 	cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
785de2d9445STejun Heo }
786de2d9445STejun Heo 
numa_remove_cpu(int cpu)787148f9bb8SPaul Gortmaker void numa_remove_cpu(int cpu)
788de2d9445STejun Heo {
789de2d9445STejun Heo 	cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
790de2d9445STejun Heo }
791de2d9445STejun Heo # endif	/* !CONFIG_NUMA_EMU */
792de2d9445STejun Heo 
793de2d9445STejun Heo #else	/* !CONFIG_DEBUG_PER_CPU_MAPS */
794645a7919STejun Heo 
__cpu_to_node(int cpu)795645a7919STejun Heo int __cpu_to_node(int cpu)
796645a7919STejun Heo {
797645a7919STejun Heo 	if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
798645a7919STejun Heo 		printk(KERN_WARNING
799645a7919STejun Heo 			"cpu_to_node(%d): usage too early!\n", cpu);
800645a7919STejun Heo 		dump_stack();
801645a7919STejun Heo 		return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
802645a7919STejun Heo 	}
803645a7919STejun Heo 	return per_cpu(x86_cpu_to_node_map, cpu);
804645a7919STejun Heo }
805645a7919STejun Heo EXPORT_SYMBOL(__cpu_to_node);
806645a7919STejun Heo 
807645a7919STejun Heo /*
808645a7919STejun Heo  * Same function as cpu_to_node() but used if called before the
809645a7919STejun Heo  * per_cpu areas are setup.
810645a7919STejun Heo  */
early_cpu_to_node(int cpu)811645a7919STejun Heo int early_cpu_to_node(int cpu)
812645a7919STejun Heo {
813645a7919STejun Heo 	if (early_per_cpu_ptr(x86_cpu_to_node_map))
814645a7919STejun Heo 		return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
815645a7919STejun Heo 
816645a7919STejun Heo 	if (!cpu_possible(cpu)) {
817645a7919STejun Heo 		printk(KERN_WARNING
818645a7919STejun Heo 			"early_cpu_to_node(%d): no per_cpu area!\n", cpu);
819645a7919STejun Heo 		dump_stack();
820645a7919STejun Heo 		return NUMA_NO_NODE;
821645a7919STejun Heo 	}
822645a7919STejun Heo 	return per_cpu(x86_cpu_to_node_map, cpu);
823645a7919STejun Heo }
824645a7919STejun Heo 
debug_cpumask_set_cpu(int cpu,int node,bool enable)8257a6c6547SDavid Rientjes void debug_cpumask_set_cpu(int cpu, int node, bool enable)
826de2d9445STejun Heo {
827de2d9445STejun Heo 	struct cpumask *mask;
828de2d9445STejun Heo 
82914392fd3SDavid Rientjes 	if (node == NUMA_NO_NODE) {
83014392fd3SDavid Rientjes 		/* early_cpu_to_node() already emits a warning and trace */
8317a6c6547SDavid Rientjes 		return;
83214392fd3SDavid Rientjes 	}
833de2d9445STejun Heo 	mask = node_to_cpumask_map[node];
834625395c4SSiddh Raman Pant 	if (!cpumask_available(mask)) {
835de2d9445STejun Heo 		pr_err("node_to_cpumask_map[%i] NULL\n", node);
836de2d9445STejun Heo 		dump_stack();
837de2d9445STejun Heo 		return;
8387a6c6547SDavid Rientjes 	}
839de2d9445STejun Heo 
840de2d9445STejun Heo 	if (enable)
841de2d9445STejun Heo 		cpumask_set_cpu(cpu, mask);
842de2d9445STejun Heo 	else
843de2d9445STejun Heo 		cpumask_clear_cpu(cpu, mask);
8447a6c6547SDavid Rientjes 
845bf58b487STejun Heo 	printk(KERN_DEBUG "%s cpu %d node %d: mask now %*pbl\n",
8467a6c6547SDavid Rientjes 		enable ? "numa_add_cpu" : "numa_remove_cpu",
847bf58b487STejun Heo 		cpu, node, cpumask_pr_args(mask));
8487a6c6547SDavid Rientjes 	return;
8497a6c6547SDavid Rientjes }
8507a6c6547SDavid Rientjes 
8517a6c6547SDavid Rientjes # ifndef CONFIG_NUMA_EMU
numa_set_cpumask(int cpu,bool enable)852148f9bb8SPaul Gortmaker static void numa_set_cpumask(int cpu, bool enable)
8537a6c6547SDavid Rientjes {
8547a6c6547SDavid Rientjes 	debug_cpumask_set_cpu(cpu, early_cpu_to_node(cpu), enable);
855de2d9445STejun Heo }
856de2d9445STejun Heo 
numa_add_cpu(int cpu)857148f9bb8SPaul Gortmaker void numa_add_cpu(int cpu)
858de2d9445STejun Heo {
8597a6c6547SDavid Rientjes 	numa_set_cpumask(cpu, true);
860de2d9445STejun Heo }
861de2d9445STejun Heo 
numa_remove_cpu(int cpu)862148f9bb8SPaul Gortmaker void numa_remove_cpu(int cpu)
863de2d9445STejun Heo {
8647a6c6547SDavid Rientjes 	numa_set_cpumask(cpu, false);
865de2d9445STejun Heo }
866de2d9445STejun Heo # endif	/* !CONFIG_NUMA_EMU */
867de2d9445STejun Heo 
86871ee73e7SRusty Russell /*
86971ee73e7SRusty Russell  * Returns a pointer to the bitmask of CPUs on Node 'node'.
87071ee73e7SRusty Russell  */
cpumask_of_node(int node)87173e907deSRusty Russell const struct cpumask *cpumask_of_node(int node)
87271ee73e7SRusty Russell {
873bc04a049SPeter Zijlstra 	if ((unsigned)node >= nr_node_ids) {
87471ee73e7SRusty Russell 		printk(KERN_WARNING
875bc04a049SPeter Zijlstra 			"cpumask_of_node(%d): (unsigned)node >= nr_node_ids(%u)\n",
87671ee73e7SRusty Russell 			node, nr_node_ids);
87771ee73e7SRusty Russell 		dump_stack();
87871ee73e7SRusty Russell 		return cpu_none_mask;
87971ee73e7SRusty Russell 	}
880625395c4SSiddh Raman Pant 	if (!cpumask_available(node_to_cpumask_map[node])) {
881c032ef60SRusty Russell 		printk(KERN_WARNING
882c032ef60SRusty Russell 			"cpumask_of_node(%d): no node_to_cpumask_map!\n",
883c032ef60SRusty Russell 			node);
884c032ef60SRusty Russell 		dump_stack();
885c032ef60SRusty Russell 		return cpu_online_mask;
886c032ef60SRusty Russell 	}
8870b966252SRusty Russell 	return node_to_cpumask_map[node];
88871ee73e7SRusty Russell }
88971ee73e7SRusty Russell EXPORT_SYMBOL(cpumask_of_node);
890645a7919STejun Heo 
891de2d9445STejun Heo #endif	/* !CONFIG_DEBUG_PER_CPU_MAPS */
892a4106eaeSTejun Heo 
8935d30f92eSDan Williams #ifdef CONFIG_NUMA_KEEP_MEMINFO
meminfo_to_nid(struct numa_meminfo * mi,u64 start)8945d30f92eSDan Williams static int meminfo_to_nid(struct numa_meminfo *mi, u64 start)
895a4106eaeSTejun Heo {
896a4106eaeSTejun Heo 	int i;
897a4106eaeSTejun Heo 
898a4106eaeSTejun Heo 	for (i = 0; i < mi->nr_blks; i++)
899a4106eaeSTejun Heo 		if (mi->blk[i].start <= start && mi->blk[i].end > start)
9005d30f92eSDan Williams 			return mi->blk[i].nid;
9015d30f92eSDan Williams 	return NUMA_NO_NODE;
9025d30f92eSDan Williams }
9035d30f92eSDan Williams 
phys_to_target_node(phys_addr_t start)9045d30f92eSDan Williams int phys_to_target_node(phys_addr_t start)
9055d30f92eSDan Williams {
9065d30f92eSDan Williams 	int nid = meminfo_to_nid(&numa_meminfo, start);
9075d30f92eSDan Williams 
9085d30f92eSDan Williams 	/*
9095d30f92eSDan Williams 	 * Prefer online nodes, but if reserved memory might be
9105d30f92eSDan Williams 	 * hot-added continue the search with reserved ranges.
9115d30f92eSDan Williams 	 */
9125d30f92eSDan Williams 	if (nid != NUMA_NO_NODE)
9135d30f92eSDan Williams 		return nid;
9145d30f92eSDan Williams 
9155d30f92eSDan Williams 	return meminfo_to_nid(&numa_reserved_meminfo, start);
9165d30f92eSDan Williams }
917a927bd6bSDan Williams EXPORT_SYMBOL_GPL(phys_to_target_node);
9185d30f92eSDan Williams 
memory_add_physaddr_to_nid(u64 start)9195d30f92eSDan Williams int memory_add_physaddr_to_nid(u64 start)
9205d30f92eSDan Williams {
9215d30f92eSDan Williams 	int nid = meminfo_to_nid(&numa_meminfo, start);
9225d30f92eSDan Williams 
9235d30f92eSDan Williams 	if (nid == NUMA_NO_NODE)
9245d30f92eSDan Williams 		nid = numa_meminfo.blk[0].nid;
925a4106eaeSTejun Heo 	return nid;
926a4106eaeSTejun Heo }
927a927bd6bSDan Williams EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
928eb38c5edSAlison Schofield 
929db921483SRobert Richter #endif
930db921483SRobert Richter 
cmp_memblk(const void * a,const void * b)931eb38c5edSAlison Schofield static int __init cmp_memblk(const void *a, const void *b)
932eb38c5edSAlison Schofield {
933eb38c5edSAlison Schofield 	const struct numa_memblk *ma = *(const struct numa_memblk **)a;
934eb38c5edSAlison Schofield 	const struct numa_memblk *mb = *(const struct numa_memblk **)b;
935eb38c5edSAlison Schofield 
9363a0060d2SAlison Schofield 	return (ma->start > mb->start) - (ma->start < mb->start);
937eb38c5edSAlison Schofield }
938eb38c5edSAlison Schofield 
939eb38c5edSAlison Schofield static struct numa_memblk *numa_memblk_list[NR_NODE_MEMBLKS] __initdata;
940eb38c5edSAlison Schofield 
941eb38c5edSAlison Schofield /**
942eb38c5edSAlison Schofield  * numa_fill_memblks - Fill gaps in numa_meminfo memblks
943eb38c5edSAlison Schofield  * @start: address to begin fill
944eb38c5edSAlison Schofield  * @end: address to end fill
945eb38c5edSAlison Schofield  *
946b5bf39cdSAlison Schofield  * Find and extend numa_meminfo memblks to cover the physical
947b5bf39cdSAlison Schofield  * address range @start-@end
948eb38c5edSAlison Schofield  *
949eb38c5edSAlison Schofield  * RETURNS:
950eb38c5edSAlison Schofield  * 0		  : Success
951b5bf39cdSAlison Schofield  * NUMA_NO_MEMBLK : No memblks exist in address range @start-@end
952eb38c5edSAlison Schofield  */
953eb38c5edSAlison Schofield 
numa_fill_memblks(u64 start,u64 end)954eb38c5edSAlison Schofield int __init numa_fill_memblks(u64 start, u64 end)
955eb38c5edSAlison Schofield {
956eb38c5edSAlison Schofield 	struct numa_memblk **blk = &numa_memblk_list[0];
957eb38c5edSAlison Schofield 	struct numa_meminfo *mi = &numa_meminfo;
958eb38c5edSAlison Schofield 	int count = 0;
959eb38c5edSAlison Schofield 	u64 prev_end;
960eb38c5edSAlison Schofield 
961eb38c5edSAlison Schofield 	/*
962eb38c5edSAlison Schofield 	 * Create a list of pointers to numa_meminfo memblks that
963b5bf39cdSAlison Schofield 	 * overlap start, end. The list is used to make in-place
964b5bf39cdSAlison Schofield 	 * changes that fill out the numa_meminfo memblks.
965eb38c5edSAlison Schofield 	 */
966eb38c5edSAlison Schofield 	for (int i = 0; i < mi->nr_blks; i++) {
967eb38c5edSAlison Schofield 		struct numa_memblk *bi = &mi->blk[i];
968eb38c5edSAlison Schofield 
969b5bf39cdSAlison Schofield 		if (memblock_addrs_overlap(start, end - start, bi->start,
970b5bf39cdSAlison Schofield 					   bi->end - bi->start)) {
971eb38c5edSAlison Schofield 			blk[count] = &mi->blk[i];
972eb38c5edSAlison Schofield 			count++;
973eb38c5edSAlison Schofield 		}
974eb38c5edSAlison Schofield 	}
975eb38c5edSAlison Schofield 	if (!count)
976eb38c5edSAlison Schofield 		return NUMA_NO_MEMBLK;
977eb38c5edSAlison Schofield 
978eb38c5edSAlison Schofield 	/* Sort the list of pointers in memblk->start order */
979eb38c5edSAlison Schofield 	sort(&blk[0], count, sizeof(blk[0]), cmp_memblk, NULL);
980eb38c5edSAlison Schofield 
981eb38c5edSAlison Schofield 	/* Make sure the first/last memblks include start/end */
982eb38c5edSAlison Schofield 	blk[0]->start = min(blk[0]->start, start);
983eb38c5edSAlison Schofield 	blk[count - 1]->end = max(blk[count - 1]->end, end);
984eb38c5edSAlison Schofield 
985eb38c5edSAlison Schofield 	/*
986eb38c5edSAlison Schofield 	 * Fill any gaps by tracking the previous memblks
987eb38c5edSAlison Schofield 	 * end address and backfilling to it if needed.
988eb38c5edSAlison Schofield 	 */
989eb38c5edSAlison Schofield 	prev_end = blk[0]->end;
990eb38c5edSAlison Schofield 	for (int i = 1; i < count; i++) {
991eb38c5edSAlison Schofield 		struct numa_memblk *curr = blk[i];
992eb38c5edSAlison Schofield 
993eb38c5edSAlison Schofield 		if (prev_end >= curr->start) {
994eb38c5edSAlison Schofield 			if (prev_end < curr->end)
995eb38c5edSAlison Schofield 				prev_end = curr->end;
996eb38c5edSAlison Schofield 		} else {
997eb38c5edSAlison Schofield 			curr->start = prev_end;
998eb38c5edSAlison Schofield 			prev_end = curr->end;
999eb38c5edSAlison Schofield 		}
1000eb38c5edSAlison Schofield 	}
1001eb38c5edSAlison Schofield 	return 0;
1002eb38c5edSAlison Schofield }
1003