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