xref: /linux/drivers/acpi/numa/srat.c (revision ba6ec09911b805778a2fed6d626bfe77b011a717)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  acpi_numa.c - ACPI NUMA support
4  *
5  *  Copyright (C) 2002 Takayoshi Kochi <t-kochi@bq.jp.nec.com>
6  */
7 
8 #define pr_fmt(fmt) "ACPI: " fmt
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/errno.h>
15 #include <linux/acpi.h>
16 #include <linux/memblock.h>
17 #include <linux/numa.h>
18 #include <linux/nodemask.h>
19 #include <linux/topology.h>
20 #include <linux/numa_memblks.h>
21 
22 static nodemask_t nodes_found_map = NODE_MASK_NONE;
23 
24 /* maps to convert between proximity domain and logical node ID */
25 static int pxm_to_node_map[MAX_PXM_DOMAINS]
26 			= { [0 ... MAX_PXM_DOMAINS - 1] = NUMA_NO_NODE };
27 static int node_to_pxm_map[MAX_NUMNODES]
28 			= { [0 ... MAX_NUMNODES - 1] = PXM_INVAL };
29 
30 unsigned char acpi_srat_revision __initdata;
31 static int acpi_numa __initdata;
32 
33 static int last_real_pxm;
34 
disable_srat(void)35 void __init disable_srat(void)
36 {
37 	acpi_numa = -1;
38 }
39 
pxm_to_node(int pxm)40 int pxm_to_node(int pxm)
41 {
42 	if (pxm < 0 || pxm >= MAX_PXM_DOMAINS || numa_off)
43 		return NUMA_NO_NODE;
44 	return pxm_to_node_map[pxm];
45 }
46 EXPORT_SYMBOL(pxm_to_node);
47 
node_to_pxm(int node)48 int node_to_pxm(int node)
49 {
50 	if (node < 0)
51 		return PXM_INVAL;
52 	return node_to_pxm_map[node];
53 }
54 EXPORT_SYMBOL_GPL(node_to_pxm);
55 
__acpi_map_pxm_to_node(int pxm,int node)56 static void __acpi_map_pxm_to_node(int pxm, int node)
57 {
58 	if (pxm_to_node_map[pxm] == NUMA_NO_NODE || node < pxm_to_node_map[pxm])
59 		pxm_to_node_map[pxm] = node;
60 	if (node_to_pxm_map[node] == PXM_INVAL || pxm < node_to_pxm_map[node])
61 		node_to_pxm_map[node] = pxm;
62 }
63 
acpi_map_pxm_to_node(int pxm)64 int acpi_map_pxm_to_node(int pxm)
65 {
66 	int node;
67 
68 	if (pxm < 0 || pxm >= MAX_PXM_DOMAINS || numa_off)
69 		return NUMA_NO_NODE;
70 
71 	node = pxm_to_node_map[pxm];
72 
73 	if (node == NUMA_NO_NODE) {
74 		node = first_unset_node(nodes_found_map);
75 		if (node >= MAX_NUMNODES)
76 			return NUMA_NO_NODE;
77 		__acpi_map_pxm_to_node(pxm, node);
78 		node_set(node, nodes_found_map);
79 	}
80 
81 	return node;
82 }
83 EXPORT_SYMBOL(acpi_map_pxm_to_node);
84 
85 #ifdef CONFIG_NUMA_EMU
86 /*
87  * Take max_nid - 1 fake-numa nodes into account in both
88  * pxm_to_node_map()/node_to_pxm_map[] tables.
89  */
fix_pxm_node_maps(int max_nid)90 int __init fix_pxm_node_maps(int max_nid)
91 {
92 	static int pxm_to_node_map_copy[MAX_PXM_DOMAINS] __initdata
93 			= { [0 ... MAX_PXM_DOMAINS - 1] = NUMA_NO_NODE };
94 	static int node_to_pxm_map_copy[MAX_NUMNODES] __initdata
95 			= { [0 ... MAX_NUMNODES - 1] = PXM_INVAL };
96 	int i, j, index = -1, count = 0;
97 	nodemask_t nodes_to_enable;
98 
99 	if (numa_off)
100 		return -1;
101 
102 	/* no or incomplete node/PXM mapping set, nothing to do */
103 	if (srat_disabled())
104 		return 0;
105 
106 	/* find fake nodes PXM mapping */
107 	for (i = 0; i < MAX_NUMNODES; i++) {
108 		if (node_to_pxm_map[i] != PXM_INVAL) {
109 			for (j = 0; j <= max_nid; j++) {
110 				if ((emu_nid_to_phys[j] == i) &&
111 				    WARN(node_to_pxm_map_copy[j] != PXM_INVAL,
112 					 "Node %d is already binded to PXM %d\n",
113 					 j, node_to_pxm_map_copy[j]))
114 					return -1;
115 				if (emu_nid_to_phys[j] == i) {
116 					node_to_pxm_map_copy[j] =
117 						node_to_pxm_map[i];
118 					if (j > index)
119 						index = j;
120 					count++;
121 				}
122 			}
123 		}
124 	}
125 	if (index == -1) {
126 		pr_debug("No node/PXM mapping has been set\n");
127 		/* nothing more to be done */
128 		return 0;
129 	}
130 	if (WARN(index != max_nid, "%d max nid  when expected %d\n",
131 		      index, max_nid))
132 		return -1;
133 
134 	nodes_clear(nodes_to_enable);
135 
136 	/* map phys nodes not used for fake nodes */
137 	for (i = 0; i < MAX_NUMNODES; i++) {
138 		if (node_to_pxm_map[i] != PXM_INVAL) {
139 			for (j = 0; j <= max_nid; j++)
140 				if (emu_nid_to_phys[j] == i)
141 					break;
142 			/* fake nodes PXM mapping has been done */
143 			if (j <= max_nid)
144 				continue;
145 			/* find first hole */
146 			for (j = 0;
147 			     j < MAX_NUMNODES &&
148 				 node_to_pxm_map_copy[j] != PXM_INVAL;
149 			     j++)
150 			;
151 			if (WARN(j == MAX_NUMNODES,
152 			    "Number of nodes exceeds MAX_NUMNODES\n"))
153 				return -1;
154 			node_to_pxm_map_copy[j] = node_to_pxm_map[i];
155 			node_set(j, nodes_to_enable);
156 			count++;
157 		}
158 	}
159 
160 	/* creating reverse mapping in pxm_to_node_map[] */
161 	for (i = 0; i < MAX_NUMNODES; i++)
162 		if (node_to_pxm_map_copy[i] != PXM_INVAL &&
163 		    pxm_to_node_map_copy[node_to_pxm_map_copy[i]] == NUMA_NO_NODE)
164 			pxm_to_node_map_copy[node_to_pxm_map_copy[i]] = i;
165 
166 	/* overwrite with new mapping */
167 	for (i = 0; i < MAX_NUMNODES; i++) {
168 		node_to_pxm_map[i] = node_to_pxm_map_copy[i];
169 		pxm_to_node_map[i] = pxm_to_node_map_copy[i];
170 	}
171 
172 	/* enable other nodes found in PXM for hotplug */
173 	nodes_or(numa_nodes_parsed, nodes_to_enable, numa_nodes_parsed);
174 
175 	pr_debug("found %d total number of nodes\n", count);
176 	return 0;
177 }
178 #endif
179 
180 static void __init
acpi_table_print_srat_entry(struct acpi_subtable_header * header)181 acpi_table_print_srat_entry(struct acpi_subtable_header *header)
182 {
183 	switch (header->type) {
184 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
185 		{
186 			struct acpi_srat_cpu_affinity *p =
187 			    (struct acpi_srat_cpu_affinity *)header;
188 			pr_debug("SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
189 				 p->apic_id, p->local_sapic_eid,
190 				 p->proximity_domain_lo,
191 				 (p->flags & ACPI_SRAT_CPU_ENABLED) ?
192 				 "enabled" : "disabled");
193 		}
194 		break;
195 
196 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
197 		{
198 			struct acpi_srat_mem_affinity *p =
199 			    (struct acpi_srat_mem_affinity *)header;
200 			pr_debug("SRAT Memory (0x%llx length 0x%llx) in proximity domain %d %s%s%s\n",
201 				 (unsigned long long)p->base_address,
202 				 (unsigned long long)p->length,
203 				 p->proximity_domain,
204 				 (p->flags & ACPI_SRAT_MEM_ENABLED) ?
205 				 "enabled" : "disabled",
206 				 (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) ?
207 				 " hot-pluggable" : "",
208 				 (p->flags & ACPI_SRAT_MEM_NON_VOLATILE) ?
209 				 " non-volatile" : "");
210 		}
211 		break;
212 
213 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
214 		{
215 			struct acpi_srat_x2apic_cpu_affinity *p =
216 			    (struct acpi_srat_x2apic_cpu_affinity *)header;
217 			pr_debug("SRAT Processor (x2apicid[0x%08x]) in proximity domain %d %s\n",
218 				 p->apic_id,
219 				 p->proximity_domain,
220 				 (p->flags & ACPI_SRAT_CPU_ENABLED) ?
221 				 "enabled" : "disabled");
222 		}
223 		break;
224 
225 	case ACPI_SRAT_TYPE_GICC_AFFINITY:
226 		{
227 			struct acpi_srat_gicc_affinity *p =
228 			    (struct acpi_srat_gicc_affinity *)header;
229 			pr_debug("SRAT Processor (acpi id[0x%04x]) in proximity domain %d %s\n",
230 				 p->acpi_processor_uid,
231 				 p->proximity_domain,
232 				 (p->flags & ACPI_SRAT_GICC_ENABLED) ?
233 				 "enabled" : "disabled");
234 		}
235 		break;
236 
237 	case ACPI_SRAT_TYPE_GENERIC_AFFINITY:
238 	{
239 		struct acpi_srat_generic_affinity *p =
240 			(struct acpi_srat_generic_affinity *)header;
241 
242 		if (p->device_handle_type == 0) {
243 			/*
244 			 * For pci devices this may be the only place they
245 			 * are assigned a proximity domain
246 			 */
247 			pr_debug("SRAT Generic Initiator(Seg:%u BDF:%u) in proximity domain %d %s\n",
248 				 *(u16 *)(&p->device_handle[0]),
249 				 *(u16 *)(&p->device_handle[2]),
250 				 p->proximity_domain,
251 				 (p->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED) ?
252 				"enabled" : "disabled");
253 		} else {
254 			/*
255 			 * In this case we can rely on the device having a
256 			 * proximity domain reference
257 			 */
258 			pr_debug("SRAT Generic Initiator(HID=%.8s UID=%.4s) in proximity domain %d %s\n",
259 				(char *)(&p->device_handle[0]),
260 				(char *)(&p->device_handle[8]),
261 				p->proximity_domain,
262 				(p->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED) ?
263 				"enabled" : "disabled");
264 		}
265 	}
266 	break;
267 
268 	case ACPI_SRAT_TYPE_RINTC_AFFINITY:
269 		{
270 			struct acpi_srat_rintc_affinity *p =
271 			    (struct acpi_srat_rintc_affinity *)header;
272 			pr_debug("SRAT Processor (acpi id[0x%04x]) in proximity domain %d %s\n",
273 				 p->acpi_processor_uid,
274 				 p->proximity_domain,
275 				 (p->flags & ACPI_SRAT_RINTC_ENABLED) ?
276 				 "enabled" : "disabled");
277 		}
278 		break;
279 
280 	default:
281 		pr_warn("Found unsupported SRAT entry (type = 0x%x)\n",
282 			header->type);
283 		break;
284 	}
285 }
286 
287 /*
288  * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
289  * up the NUMA heuristics which wants the local node to have a smaller
290  * distance than the others.
291  * Do some quick checks here and only use the SLIT if it passes.
292  */
slit_valid(struct acpi_table_slit * slit)293 static int __init slit_valid(struct acpi_table_slit *slit)
294 {
295 	int i, j;
296 	int d = slit->locality_count;
297 	for (i = 0; i < d; i++) {
298 		for (j = 0; j < d; j++) {
299 			u8 val = slit->entry[d*i + j];
300 			if (i == j) {
301 				if (val != LOCAL_DISTANCE)
302 					return 0;
303 			} else if (val <= LOCAL_DISTANCE)
304 				return 0;
305 		}
306 	}
307 	return 1;
308 }
309 
bad_srat(void)310 void __init bad_srat(void)
311 {
312 	pr_err("SRAT: SRAT not used.\n");
313 	disable_srat();
314 }
315 
srat_disabled(void)316 int __init srat_disabled(void)
317 {
318 	return acpi_numa < 0;
319 }
320 
numa_fill_memblks(u64 start,u64 end)321 __weak int __init numa_fill_memblks(u64 start, u64 end)
322 {
323 	return NUMA_NO_MEMBLK;
324 }
325 
326 /*
327  * Callback for SLIT parsing.  pxm_to_node() returns NUMA_NO_NODE for
328  * I/O localities since SRAT does not list them.  I/O localities are
329  * not supported at this point.
330  */
acpi_parse_slit(struct acpi_table_header * table)331 static int __init acpi_parse_slit(struct acpi_table_header *table)
332 {
333 	struct acpi_table_slit *slit = (struct acpi_table_slit *)table;
334 	int i, j;
335 
336 	if (!slit_valid(slit)) {
337 		pr_info("SLIT table looks invalid. Not used.\n");
338 		return -EINVAL;
339 	}
340 
341 	for (i = 0; i < slit->locality_count; i++) {
342 		const int from_node = pxm_to_node(i);
343 
344 		if (from_node == NUMA_NO_NODE)
345 			continue;
346 
347 		for (j = 0; j < slit->locality_count; j++) {
348 			const int to_node = pxm_to_node(j);
349 
350 			if (to_node == NUMA_NO_NODE)
351 				continue;
352 
353 			numa_set_distance(from_node, to_node,
354 				slit->entry[slit->locality_count * i + j]);
355 		}
356 	}
357 
358 	return 0;
359 }
360 
361 static int parsed_numa_memblks __initdata;
362 
363 static int __init
acpi_parse_memory_affinity(union acpi_subtable_headers * header,const unsigned long table_end)364 acpi_parse_memory_affinity(union acpi_subtable_headers *header,
365 			   const unsigned long table_end)
366 {
367 	struct acpi_srat_mem_affinity *ma;
368 	u64 start, end;
369 	u32 hotpluggable;
370 	int node, pxm;
371 
372 	ma = (struct acpi_srat_mem_affinity *)header;
373 
374 	acpi_table_print_srat_entry(&header->common);
375 
376 	if (srat_disabled())
377 		return 0;
378 	if (ma->header.length < sizeof(struct acpi_srat_mem_affinity)) {
379 		pr_err("SRAT: Unexpected header length: %d\n",
380 		       ma->header.length);
381 		goto out_err_bad_srat;
382 	}
383 	if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
384 		return 0;
385 	hotpluggable = IS_ENABLED(CONFIG_MEMORY_HOTPLUG) &&
386 		(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE);
387 
388 	start = ma->base_address;
389 	end = start + ma->length;
390 	pxm = ma->proximity_domain;
391 	if (acpi_srat_revision <= 1)
392 		pxm &= 0xff;
393 
394 	node = acpi_map_pxm_to_node(pxm);
395 	if (node == NUMA_NO_NODE) {
396 		pr_err("SRAT: Too many proximity domains.\n");
397 		goto out_err_bad_srat;
398 	}
399 
400 	if (numa_add_memblk(node, start, end) < 0) {
401 		pr_err("SRAT: Failed to add memblk to node %u [mem %#010Lx-%#010Lx]\n",
402 		       node, (unsigned long long) start,
403 		       (unsigned long long) end - 1);
404 		goto out_err_bad_srat;
405 	}
406 
407 	node_set(node, numa_nodes_parsed);
408 
409 	pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
410 		node, pxm,
411 		(unsigned long long) start, (unsigned long long) end - 1,
412 		hotpluggable ? " hotplug" : "",
413 		ma->flags & ACPI_SRAT_MEM_NON_VOLATILE ? " non-volatile" : "");
414 
415 	/* Mark hotplug range in memblock. */
416 	if (hotpluggable && memblock_mark_hotplug(start, ma->length))
417 		pr_warn("SRAT: Failed to mark hotplug range [mem %#010Lx-%#010Lx] in memblock\n",
418 			(unsigned long long)start, (unsigned long long)end - 1);
419 
420 	max_possible_pfn = max(max_possible_pfn, PFN_UP(end - 1));
421 
422 	parsed_numa_memblks++;
423 
424 	return 0;
425 
426 out_err_bad_srat:
427 	/* Just disable SRAT, but do not fail and ignore errors. */
428 	bad_srat();
429 
430 	return 0;
431 }
432 
acpi_parse_cfmws(union acpi_subtable_headers * header,void * arg,const unsigned long table_end)433 static int __init acpi_parse_cfmws(union acpi_subtable_headers *header,
434 				   void *arg, const unsigned long table_end)
435 {
436 	struct acpi_cedt_cfmws *cfmws;
437 	int *fake_pxm = arg;
438 	u64 start, end;
439 	int node;
440 
441 	cfmws = (struct acpi_cedt_cfmws *)header;
442 	start = cfmws->base_hpa;
443 	end = cfmws->base_hpa + cfmws->window_size;
444 
445 	/*
446 	 * The SRAT may have already described NUMA details for all,
447 	 * or a portion of, this CFMWS HPA range. Extend the memblks
448 	 * found for any portion of the window to cover the entire
449 	 * window.
450 	 */
451 	if (!numa_fill_memblks(start, end))
452 		return 0;
453 
454 	/* No SRAT description. Create a new node. */
455 	node = acpi_map_pxm_to_node(*fake_pxm);
456 
457 	if (node == NUMA_NO_NODE) {
458 		pr_err("ACPI NUMA: Too many proximity domains while processing CFMWS.\n");
459 		return -EINVAL;
460 	}
461 
462 	if (numa_add_memblk(node, start, end) < 0) {
463 		/* CXL driver must handle the NUMA_NO_NODE case */
464 		pr_warn("ACPI NUMA: Failed to add memblk for CFMWS node %d [mem %#llx-%#llx]\n",
465 			node, start, end);
466 	}
467 	node_set(node, numa_nodes_parsed);
468 
469 	/* Set the next available fake_pxm value */
470 	(*fake_pxm)++;
471 	return 0;
472 }
473 
474 void __init __weak
acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity * pa)475 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
476 {
477 	pr_warn("Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
478 }
479 
480 static int __init
acpi_parse_x2apic_affinity(union acpi_subtable_headers * header,const unsigned long end)481 acpi_parse_x2apic_affinity(union acpi_subtable_headers *header,
482 			   const unsigned long end)
483 {
484 	struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
485 
486 	processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
487 
488 	acpi_table_print_srat_entry(&header->common);
489 
490 	/* let architecture-dependent part to do it */
491 	acpi_numa_x2apic_affinity_init(processor_affinity);
492 
493 	return 0;
494 }
495 
496 static int __init
acpi_parse_processor_affinity(union acpi_subtable_headers * header,const unsigned long end)497 acpi_parse_processor_affinity(union acpi_subtable_headers *header,
498 			      const unsigned long end)
499 {
500 	struct acpi_srat_cpu_affinity *processor_affinity;
501 
502 	processor_affinity = (struct acpi_srat_cpu_affinity *)header;
503 
504 	acpi_table_print_srat_entry(&header->common);
505 
506 	/* let architecture-dependent part to do it */
507 	acpi_numa_processor_affinity_init(processor_affinity);
508 
509 	return 0;
510 }
511 
512 static int __init
acpi_parse_gicc_affinity(union acpi_subtable_headers * header,const unsigned long end)513 acpi_parse_gicc_affinity(union acpi_subtable_headers *header,
514 			 const unsigned long end)
515 {
516 	struct acpi_srat_gicc_affinity *processor_affinity;
517 
518 	processor_affinity = (struct acpi_srat_gicc_affinity *)header;
519 
520 	acpi_table_print_srat_entry(&header->common);
521 
522 	/* let architecture-dependent part to do it */
523 	acpi_numa_gicc_affinity_init(processor_affinity);
524 
525 	return 0;
526 }
527 
528 #if defined(CONFIG_X86) || defined(CONFIG_ARM64)
529 static int __init
acpi_parse_gi_affinity(union acpi_subtable_headers * header,const unsigned long end)530 acpi_parse_gi_affinity(union acpi_subtable_headers *header,
531 		       const unsigned long end)
532 {
533 	struct acpi_srat_generic_affinity *gi_affinity;
534 	int node;
535 
536 	gi_affinity = (struct acpi_srat_generic_affinity *)header;
537 	if (!gi_affinity)
538 		return -EINVAL;
539 	acpi_table_print_srat_entry(&header->common);
540 
541 	if (!(gi_affinity->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED))
542 		return -EINVAL;
543 
544 	node = acpi_map_pxm_to_node(gi_affinity->proximity_domain);
545 	if (node == NUMA_NO_NODE) {
546 		pr_err("SRAT: Too many proximity domains.\n");
547 		return -EINVAL;
548 	}
549 	node_set(node, numa_nodes_parsed);
550 	node_set_state(node, N_GENERIC_INITIATOR);
551 
552 	return 0;
553 }
554 #else
555 static int __init
acpi_parse_gi_affinity(union acpi_subtable_headers * header,const unsigned long end)556 acpi_parse_gi_affinity(union acpi_subtable_headers *header,
557 		       const unsigned long end)
558 {
559 	return 0;
560 }
561 #endif /* defined(CONFIG_X86) || defined (CONFIG_ARM64) */
562 
563 static int __init
acpi_parse_rintc_affinity(union acpi_subtable_headers * header,const unsigned long end)564 acpi_parse_rintc_affinity(union acpi_subtable_headers *header,
565 			  const unsigned long end)
566 {
567 	struct acpi_srat_rintc_affinity *rintc_affinity;
568 
569 	rintc_affinity = (struct acpi_srat_rintc_affinity *)header;
570 	acpi_table_print_srat_entry(&header->common);
571 
572 	/* let architecture-dependent part to do it */
573 	acpi_numa_rintc_affinity_init(rintc_affinity);
574 
575 	return 0;
576 }
577 
acpi_parse_srat(struct acpi_table_header * table)578 static int __init acpi_parse_srat(struct acpi_table_header *table)
579 {
580 	struct acpi_table_srat *srat = (struct acpi_table_srat *)table;
581 
582 	acpi_srat_revision = srat->header.revision;
583 
584 	/* Real work done in acpi_table_parse_srat below. */
585 
586 	return 0;
587 }
588 
589 static int __init
acpi_table_parse_srat(enum acpi_srat_type id,acpi_tbl_entry_handler handler,unsigned int max_entries)590 acpi_table_parse_srat(enum acpi_srat_type id,
591 		      acpi_tbl_entry_handler handler, unsigned int max_entries)
592 {
593 	return acpi_table_parse_entries(ACPI_SIG_SRAT,
594 					    sizeof(struct acpi_table_srat), id,
595 					    handler, max_entries);
596 }
597 
acpi_numa_init(void)598 int __init acpi_numa_init(void)
599 {
600 	int i, fake_pxm, cnt = 0;
601 
602 	if (acpi_disabled)
603 		return -EINVAL;
604 
605 	/*
606 	 * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
607 	 * SRAT cpu entries could have different order with that in MADT.
608 	 * So go over all cpu entries in SRAT to get apicid to node mapping.
609 	 */
610 
611 	/* SRAT: System Resource Affinity Table */
612 	if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
613 		struct acpi_subtable_proc srat_proc[5];
614 
615 		memset(srat_proc, 0, sizeof(srat_proc));
616 		srat_proc[0].id = ACPI_SRAT_TYPE_CPU_AFFINITY;
617 		srat_proc[0].handler = acpi_parse_processor_affinity;
618 		srat_proc[1].id = ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY;
619 		srat_proc[1].handler = acpi_parse_x2apic_affinity;
620 		srat_proc[2].id = ACPI_SRAT_TYPE_GICC_AFFINITY;
621 		srat_proc[2].handler = acpi_parse_gicc_affinity;
622 		srat_proc[3].id = ACPI_SRAT_TYPE_GENERIC_AFFINITY;
623 		srat_proc[3].handler = acpi_parse_gi_affinity;
624 		srat_proc[4].id = ACPI_SRAT_TYPE_RINTC_AFFINITY;
625 		srat_proc[4].handler = acpi_parse_rintc_affinity;
626 
627 		acpi_table_parse_entries_array(ACPI_SIG_SRAT,
628 					sizeof(struct acpi_table_srat),
629 					srat_proc, ARRAY_SIZE(srat_proc), 0);
630 
631 		cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
632 					    acpi_parse_memory_affinity, 0);
633 	}
634 
635 	/* SLIT: System Locality Information Table */
636 	acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
637 
638 	/*
639 	 * CXL Fixed Memory Window Structures (CFMWS) must be parsed
640 	 * after the SRAT. Create NUMA Nodes for CXL memory ranges that
641 	 * are defined in the CFMWS and not already defined in the SRAT.
642 	 * Initialize a fake_pxm as the first available PXM to emulate.
643 	 */
644 
645 	/* fake_pxm is the next unused PXM value after SRAT parsing */
646 	for (i = 0, fake_pxm = -1; i < MAX_NUMNODES; i++) {
647 		if (node_to_pxm_map[i] > fake_pxm)
648 			fake_pxm = node_to_pxm_map[i];
649 	}
650 	last_real_pxm = fake_pxm;
651 	fake_pxm++;
652 	acpi_table_parse_cedt(ACPI_CEDT_TYPE_CFMWS, acpi_parse_cfmws,
653 			      &fake_pxm);
654 
655 	if (cnt < 0)
656 		return cnt;
657 	else if (!parsed_numa_memblks)
658 		return -ENOENT;
659 	return 0;
660 }
661 
acpi_node_backed_by_real_pxm(int nid)662 bool acpi_node_backed_by_real_pxm(int nid)
663 {
664 	int pxm = node_to_pxm(nid);
665 
666 	return pxm <= last_real_pxm;
667 }
668 EXPORT_SYMBOL_GPL(acpi_node_backed_by_real_pxm);
669 
acpi_get_pxm(acpi_handle h)670 static int acpi_get_pxm(acpi_handle h)
671 {
672 	unsigned long long pxm;
673 	acpi_status status;
674 	acpi_handle handle;
675 	acpi_handle phandle = h;
676 
677 	do {
678 		handle = phandle;
679 		status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
680 		if (ACPI_SUCCESS(status))
681 			return pxm;
682 		status = acpi_get_parent(handle, &phandle);
683 	} while (ACPI_SUCCESS(status));
684 	return -1;
685 }
686 
acpi_get_node(acpi_handle handle)687 int acpi_get_node(acpi_handle handle)
688 {
689 	int pxm;
690 
691 	pxm = acpi_get_pxm(handle);
692 
693 	return pxm_to_node(pxm);
694 }
695 EXPORT_SYMBOL(acpi_get_node);
696