xref: /openbmc/linux/drivers/acpi/numa/srat.c (revision 15e3ae36)
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 int acpi_numa __initdata;
31 
32 int pxm_to_node(int pxm)
33 {
34 	if (pxm < 0)
35 		return NUMA_NO_NODE;
36 	return pxm_to_node_map[pxm];
37 }
38 
39 int node_to_pxm(int node)
40 {
41 	if (node < 0)
42 		return PXM_INVAL;
43 	return node_to_pxm_map[node];
44 }
45 
46 static void __acpi_map_pxm_to_node(int pxm, int node)
47 {
48 	if (pxm_to_node_map[pxm] == NUMA_NO_NODE || node < pxm_to_node_map[pxm])
49 		pxm_to_node_map[pxm] = node;
50 	if (node_to_pxm_map[node] == PXM_INVAL || pxm < node_to_pxm_map[node])
51 		node_to_pxm_map[node] = pxm;
52 }
53 
54 int acpi_map_pxm_to_node(int pxm)
55 {
56 	int node;
57 
58 	if (pxm < 0 || pxm >= MAX_PXM_DOMAINS || numa_off)
59 		return NUMA_NO_NODE;
60 
61 	node = pxm_to_node_map[pxm];
62 
63 	if (node == NUMA_NO_NODE) {
64 		if (nodes_weight(nodes_found_map) >= MAX_NUMNODES)
65 			return NUMA_NO_NODE;
66 		node = first_unset_node(nodes_found_map);
67 		__acpi_map_pxm_to_node(pxm, node);
68 		node_set(node, nodes_found_map);
69 	}
70 
71 	return node;
72 }
73 EXPORT_SYMBOL(acpi_map_pxm_to_node);
74 
75 static void __init
76 acpi_table_print_srat_entry(struct acpi_subtable_header *header)
77 {
78 	switch (header->type) {
79 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
80 		{
81 			struct acpi_srat_cpu_affinity *p =
82 			    (struct acpi_srat_cpu_affinity *)header;
83 			pr_debug("SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
84 				 p->apic_id, p->local_sapic_eid,
85 				 p->proximity_domain_lo,
86 				 (p->flags & ACPI_SRAT_CPU_ENABLED) ?
87 				 "enabled" : "disabled");
88 		}
89 		break;
90 
91 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
92 		{
93 			struct acpi_srat_mem_affinity *p =
94 			    (struct acpi_srat_mem_affinity *)header;
95 			pr_debug("SRAT Memory (0x%llx length 0x%llx) in proximity domain %d %s%s%s\n",
96 				 (unsigned long long)p->base_address,
97 				 (unsigned long long)p->length,
98 				 p->proximity_domain,
99 				 (p->flags & ACPI_SRAT_MEM_ENABLED) ?
100 				 "enabled" : "disabled",
101 				 (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) ?
102 				 " hot-pluggable" : "",
103 				 (p->flags & ACPI_SRAT_MEM_NON_VOLATILE) ?
104 				 " non-volatile" : "");
105 		}
106 		break;
107 
108 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
109 		{
110 			struct acpi_srat_x2apic_cpu_affinity *p =
111 			    (struct acpi_srat_x2apic_cpu_affinity *)header;
112 			pr_debug("SRAT Processor (x2apicid[0x%08x]) in proximity domain %d %s\n",
113 				 p->apic_id,
114 				 p->proximity_domain,
115 				 (p->flags & ACPI_SRAT_CPU_ENABLED) ?
116 				 "enabled" : "disabled");
117 		}
118 		break;
119 
120 	case ACPI_SRAT_TYPE_GICC_AFFINITY:
121 		{
122 			struct acpi_srat_gicc_affinity *p =
123 			    (struct acpi_srat_gicc_affinity *)header;
124 			pr_debug("SRAT Processor (acpi id[0x%04x]) in proximity domain %d %s\n",
125 				 p->acpi_processor_uid,
126 				 p->proximity_domain,
127 				 (p->flags & ACPI_SRAT_GICC_ENABLED) ?
128 				 "enabled" : "disabled");
129 		}
130 		break;
131 
132 	default:
133 		pr_warn("Found unsupported SRAT entry (type = 0x%x)\n",
134 			header->type);
135 		break;
136 	}
137 }
138 
139 /*
140  * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
141  * up the NUMA heuristics which wants the local node to have a smaller
142  * distance than the others.
143  * Do some quick checks here and only use the SLIT if it passes.
144  */
145 static int __init slit_valid(struct acpi_table_slit *slit)
146 {
147 	int i, j;
148 	int d = slit->locality_count;
149 	for (i = 0; i < d; i++) {
150 		for (j = 0; j < d; j++)  {
151 			u8 val = slit->entry[d*i + j];
152 			if (i == j) {
153 				if (val != LOCAL_DISTANCE)
154 					return 0;
155 			} else if (val <= LOCAL_DISTANCE)
156 				return 0;
157 		}
158 	}
159 	return 1;
160 }
161 
162 void __init bad_srat(void)
163 {
164 	pr_err("SRAT: SRAT not used.\n");
165 	acpi_numa = -1;
166 }
167 
168 int __init srat_disabled(void)
169 {
170 	return acpi_numa < 0;
171 }
172 
173 #if defined(CONFIG_X86) || defined(CONFIG_ARM64)
174 /*
175  * Callback for SLIT parsing.  pxm_to_node() returns NUMA_NO_NODE for
176  * I/O localities since SRAT does not list them.  I/O localities are
177  * not supported at this point.
178  */
179 void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
180 {
181 	int i, j;
182 
183 	for (i = 0; i < slit->locality_count; i++) {
184 		const int from_node = pxm_to_node(i);
185 
186 		if (from_node == NUMA_NO_NODE)
187 			continue;
188 
189 		for (j = 0; j < slit->locality_count; j++) {
190 			const int to_node = pxm_to_node(j);
191 
192 			if (to_node == NUMA_NO_NODE)
193 				continue;
194 
195 			numa_set_distance(from_node, to_node,
196 				slit->entry[slit->locality_count * i + j]);
197 		}
198 	}
199 }
200 
201 /*
202  * Default callback for parsing of the Proximity Domain <-> Memory
203  * Area mappings
204  */
205 int __init
206 acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
207 {
208 	u64 start, end;
209 	u32 hotpluggable;
210 	int node, pxm;
211 
212 	if (srat_disabled())
213 		goto out_err;
214 	if (ma->header.length < sizeof(struct acpi_srat_mem_affinity)) {
215 		pr_err("SRAT: Unexpected header length: %d\n",
216 		       ma->header.length);
217 		goto out_err_bad_srat;
218 	}
219 	if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
220 		goto out_err;
221 	hotpluggable = ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE;
222 	if (hotpluggable && !IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
223 		goto out_err;
224 
225 	start = ma->base_address;
226 	end = start + ma->length;
227 	pxm = ma->proximity_domain;
228 	if (acpi_srat_revision <= 1)
229 		pxm &= 0xff;
230 
231 	node = acpi_map_pxm_to_node(pxm);
232 	if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
233 		pr_err("SRAT: Too many proximity domains.\n");
234 		goto out_err_bad_srat;
235 	}
236 
237 	if (numa_add_memblk(node, start, end) < 0) {
238 		pr_err("SRAT: Failed to add memblk to node %u [mem %#010Lx-%#010Lx]\n",
239 		       node, (unsigned long long) start,
240 		       (unsigned long long) end - 1);
241 		goto out_err_bad_srat;
242 	}
243 
244 	node_set(node, numa_nodes_parsed);
245 
246 	pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
247 		node, pxm,
248 		(unsigned long long) start, (unsigned long long) end - 1,
249 		hotpluggable ? " hotplug" : "",
250 		ma->flags & ACPI_SRAT_MEM_NON_VOLATILE ? " non-volatile" : "");
251 
252 	/* Mark hotplug range in memblock. */
253 	if (hotpluggable && memblock_mark_hotplug(start, ma->length))
254 		pr_warn("SRAT: Failed to mark hotplug range [mem %#010Lx-%#010Lx] in memblock\n",
255 			(unsigned long long)start, (unsigned long long)end - 1);
256 
257 	max_possible_pfn = max(max_possible_pfn, PFN_UP(end - 1));
258 
259 	return 0;
260 out_err_bad_srat:
261 	bad_srat();
262 out_err:
263 	return -EINVAL;
264 }
265 #endif /* defined(CONFIG_X86) || defined (CONFIG_ARM64) */
266 
267 static int __init acpi_parse_slit(struct acpi_table_header *table)
268 {
269 	struct acpi_table_slit *slit = (struct acpi_table_slit *)table;
270 
271 	if (!slit_valid(slit)) {
272 		pr_info("SLIT table looks invalid. Not used.\n");
273 		return -EINVAL;
274 	}
275 	acpi_numa_slit_init(slit);
276 
277 	return 0;
278 }
279 
280 void __init __weak
281 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
282 {
283 	pr_warn("Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
284 }
285 
286 static int __init
287 acpi_parse_x2apic_affinity(union acpi_subtable_headers *header,
288 			   const unsigned long end)
289 {
290 	struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
291 
292 	processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
293 	if (!processor_affinity)
294 		return -EINVAL;
295 
296 	acpi_table_print_srat_entry(&header->common);
297 
298 	/* let architecture-dependent part to do it */
299 	acpi_numa_x2apic_affinity_init(processor_affinity);
300 
301 	return 0;
302 }
303 
304 static int __init
305 acpi_parse_processor_affinity(union acpi_subtable_headers *header,
306 			      const unsigned long end)
307 {
308 	struct acpi_srat_cpu_affinity *processor_affinity;
309 
310 	processor_affinity = (struct acpi_srat_cpu_affinity *)header;
311 	if (!processor_affinity)
312 		return -EINVAL;
313 
314 	acpi_table_print_srat_entry(&header->common);
315 
316 	/* let architecture-dependent part to do it */
317 	acpi_numa_processor_affinity_init(processor_affinity);
318 
319 	return 0;
320 }
321 
322 static int __init
323 acpi_parse_gicc_affinity(union acpi_subtable_headers *header,
324 			 const unsigned long end)
325 {
326 	struct acpi_srat_gicc_affinity *processor_affinity;
327 
328 	processor_affinity = (struct acpi_srat_gicc_affinity *)header;
329 	if (!processor_affinity)
330 		return -EINVAL;
331 
332 	acpi_table_print_srat_entry(&header->common);
333 
334 	/* let architecture-dependent part to do it */
335 	acpi_numa_gicc_affinity_init(processor_affinity);
336 
337 	return 0;
338 }
339 
340 static int __initdata parsed_numa_memblks;
341 
342 static int __init
343 acpi_parse_memory_affinity(union acpi_subtable_headers * header,
344 			   const unsigned long end)
345 {
346 	struct acpi_srat_mem_affinity *memory_affinity;
347 
348 	memory_affinity = (struct acpi_srat_mem_affinity *)header;
349 	if (!memory_affinity)
350 		return -EINVAL;
351 
352 	acpi_table_print_srat_entry(&header->common);
353 
354 	/* let architecture-dependent part to do it */
355 	if (!acpi_numa_memory_affinity_init(memory_affinity))
356 		parsed_numa_memblks++;
357 	return 0;
358 }
359 
360 static int __init acpi_parse_srat(struct acpi_table_header *table)
361 {
362 	struct acpi_table_srat *srat = (struct acpi_table_srat *)table;
363 
364 	acpi_srat_revision = srat->header.revision;
365 
366 	/* Real work done in acpi_table_parse_srat below. */
367 
368 	return 0;
369 }
370 
371 static int __init
372 acpi_table_parse_srat(enum acpi_srat_type id,
373 		      acpi_tbl_entry_handler handler, unsigned int max_entries)
374 {
375 	return acpi_table_parse_entries(ACPI_SIG_SRAT,
376 					    sizeof(struct acpi_table_srat), id,
377 					    handler, max_entries);
378 }
379 
380 int __init acpi_numa_init(void)
381 {
382 	int cnt = 0;
383 
384 	if (acpi_disabled)
385 		return -EINVAL;
386 
387 	/*
388 	 * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
389 	 * SRAT cpu entries could have different order with that in MADT.
390 	 * So go over all cpu entries in SRAT to get apicid to node mapping.
391 	 */
392 
393 	/* SRAT: System Resource Affinity Table */
394 	if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
395 		struct acpi_subtable_proc srat_proc[3];
396 
397 		memset(srat_proc, 0, sizeof(srat_proc));
398 		srat_proc[0].id = ACPI_SRAT_TYPE_CPU_AFFINITY;
399 		srat_proc[0].handler = acpi_parse_processor_affinity;
400 		srat_proc[1].id = ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY;
401 		srat_proc[1].handler = acpi_parse_x2apic_affinity;
402 		srat_proc[2].id = ACPI_SRAT_TYPE_GICC_AFFINITY;
403 		srat_proc[2].handler = acpi_parse_gicc_affinity;
404 
405 		acpi_table_parse_entries_array(ACPI_SIG_SRAT,
406 					sizeof(struct acpi_table_srat),
407 					srat_proc, ARRAY_SIZE(srat_proc), 0);
408 
409 		cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
410 					    acpi_parse_memory_affinity, 0);
411 	}
412 
413 	/* SLIT: System Locality Information Table */
414 	acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
415 
416 	if (cnt < 0)
417 		return cnt;
418 	else if (!parsed_numa_memblks)
419 		return -ENOENT;
420 	return 0;
421 }
422 
423 static int acpi_get_pxm(acpi_handle h)
424 {
425 	unsigned long long pxm;
426 	acpi_status status;
427 	acpi_handle handle;
428 	acpi_handle phandle = h;
429 
430 	do {
431 		handle = phandle;
432 		status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
433 		if (ACPI_SUCCESS(status))
434 			return pxm;
435 		status = acpi_get_parent(handle, &phandle);
436 	} while (ACPI_SUCCESS(status));
437 	return -1;
438 }
439 
440 int acpi_get_node(acpi_handle handle)
441 {
442 	int pxm;
443 
444 	pxm = acpi_get_pxm(handle);
445 
446 	return acpi_map_pxm_to_node(pxm);
447 }
448 EXPORT_SYMBOL(acpi_get_node);
449