1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * acpi_numa.c - ACPI NUMA support
8 #define pr_fmt(fmt) "ACPI: " fmt
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>
21 static nodemask_t nodes_found_map = NODE_MASK_NONE;
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 };
29 unsigned char acpi_srat_revision __initdata;
30 static int acpi_numa __initdata;
32 static int last_real_pxm;
34 void __init disable_srat(void)
39 int pxm_to_node(int pxm)
41 if (pxm < 0 || pxm >= MAX_PXM_DOMAINS || numa_off)
43 return pxm_to_node_map[pxm];
45 EXPORT_SYMBOL(pxm_to_node);
47 int node_to_pxm(int node)
51 return node_to_pxm_map[node];
54 static void __acpi_map_pxm_to_node(int pxm, int node)
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;
62 int acpi_map_pxm_to_node(int pxm)
66 if (pxm < 0 || pxm >= MAX_PXM_DOMAINS || numa_off)
69 node = pxm_to_node_map[pxm];
71 if (node == NUMA_NO_NODE) {
72 node = first_unset_node(nodes_found_map);
73 if (node >= MAX_NUMNODES)
75 __acpi_map_pxm_to_node(pxm, node);
76 node_set(node, nodes_found_map);
81 EXPORT_SYMBOL(acpi_map_pxm_to_node);
84 acpi_table_print_srat_entry(struct acpi_subtable_header *header)
86 switch (header->type) {
87 case ACPI_SRAT_TYPE_CPU_AFFINITY:
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");
99 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
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,
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" : "");
116 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
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",
123 (p->flags & ACPI_SRAT_CPU_ENABLED) ?
124 "enabled" : "disabled");
128 case ACPI_SRAT_TYPE_GICC_AFFINITY:
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,
135 (p->flags & ACPI_SRAT_GICC_ENABLED) ?
136 "enabled" : "disabled");
140 case ACPI_SRAT_TYPE_GENERIC_AFFINITY:
142 struct acpi_srat_generic_affinity *p =
143 (struct acpi_srat_generic_affinity *)header;
145 if (p->device_handle_type == 0) {
147 * For pci devices this may be the only place they
148 * are assigned a proximity domain
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]),
154 (p->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED) ?
155 "enabled" : "disabled");
158 * In this case we can rely on the device having a
159 * proximity domain reference
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]),
165 (p->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED) ?
166 "enabled" : "disabled");
171 pr_warn("Found unsupported SRAT entry (type = 0x%x)\n",
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.
183 static int __init slit_valid(struct acpi_table_slit *slit)
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];
191 if (val != LOCAL_DISTANCE)
193 } else if (val <= LOCAL_DISTANCE)
200 void __init bad_srat(void)
202 pr_err("SRAT: SRAT not used.\n");
206 int __init srat_disabled(void)
208 return acpi_numa < 0;
211 __weak int __init numa_fill_memblks(u64 start, u64 end)
213 return NUMA_NO_MEMBLK;
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.
221 static int __init acpi_parse_slit(struct acpi_table_header *table)
223 struct acpi_table_slit *slit = (struct acpi_table_slit *)table;
226 if (!slit_valid(slit)) {
227 pr_info("SLIT table looks invalid. Not used.\n");
231 for (i = 0; i < slit->locality_count; i++) {
232 const int from_node = pxm_to_node(i);
234 if (from_node == NUMA_NO_NODE)
237 for (j = 0; j < slit->locality_count; j++) {
238 const int to_node = pxm_to_node(j);
240 if (to_node == NUMA_NO_NODE)
243 numa_set_distance(from_node, to_node,
244 slit->entry[slit->locality_count * i + j]);
251 static int parsed_numa_memblks __initdata;
254 acpi_parse_memory_affinity(union acpi_subtable_headers *header,
255 const unsigned long table_end)
257 struct acpi_srat_mem_affinity *ma;
262 ma = (struct acpi_srat_mem_affinity *)header;
264 acpi_table_print_srat_entry(&header->common);
268 if (ma->header.length < sizeof(struct acpi_srat_mem_affinity)) {
269 pr_err("SRAT: Unexpected header length: %d\n",
271 goto out_err_bad_srat;
273 if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
275 hotpluggable = IS_ENABLED(CONFIG_MEMORY_HOTPLUG) &&
276 (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE);
278 start = ma->base_address;
279 end = start + ma->length;
280 pxm = ma->proximity_domain;
281 if (acpi_srat_revision <= 1)
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;
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;
297 node_set(node, numa_nodes_parsed);
299 pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
301 (unsigned long long) start, (unsigned long long) end - 1,
302 hotpluggable ? " hotplug" : "",
303 ma->flags & ACPI_SRAT_MEM_NON_VOLATILE ? " non-volatile" : "");
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);
310 max_possible_pfn = max(max_possible_pfn, PFN_UP(end - 1));
312 parsed_numa_memblks++;
317 /* Just disable SRAT, but do not fail and ignore errors. */
323 static int __init acpi_parse_cfmws(union acpi_subtable_headers *header,
324 void *arg, const unsigned long table_end)
326 struct acpi_cedt_cfmws *cfmws;
331 cfmws = (struct acpi_cedt_cfmws *)header;
332 start = cfmws->base_hpa;
333 end = cfmws->base_hpa + cfmws->window_size;
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
341 if (!numa_fill_memblks(start, end))
344 /* No SRAT description. Create a new node. */
345 node = acpi_map_pxm_to_node(*fake_pxm);
347 if (node == NUMA_NO_NODE) {
348 pr_err("ACPI NUMA: Too many proximity domains while processing CFMWS.\n");
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",
357 node_set(node, numa_nodes_parsed);
359 /* Set the next available fake_pxm value */
365 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
367 pr_warn("Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
371 acpi_parse_x2apic_affinity(union acpi_subtable_headers *header,
372 const unsigned long end)
374 struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
376 processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
378 acpi_table_print_srat_entry(&header->common);
380 /* let architecture-dependent part to do it */
381 acpi_numa_x2apic_affinity_init(processor_affinity);
387 acpi_parse_processor_affinity(union acpi_subtable_headers *header,
388 const unsigned long end)
390 struct acpi_srat_cpu_affinity *processor_affinity;
392 processor_affinity = (struct acpi_srat_cpu_affinity *)header;
394 acpi_table_print_srat_entry(&header->common);
396 /* let architecture-dependent part to do it */
397 acpi_numa_processor_affinity_init(processor_affinity);
403 acpi_parse_gicc_affinity(union acpi_subtable_headers *header,
404 const unsigned long end)
406 struct acpi_srat_gicc_affinity *processor_affinity;
408 processor_affinity = (struct acpi_srat_gicc_affinity *)header;
410 acpi_table_print_srat_entry(&header->common);
412 /* let architecture-dependent part to do it */
413 acpi_numa_gicc_affinity_init(processor_affinity);
418 #if defined(CONFIG_X86) || defined(CONFIG_ARM64)
420 acpi_parse_gi_affinity(union acpi_subtable_headers *header,
421 const unsigned long end)
423 struct acpi_srat_generic_affinity *gi_affinity;
426 gi_affinity = (struct acpi_srat_generic_affinity *)header;
429 acpi_table_print_srat_entry(&header->common);
431 if (!(gi_affinity->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED))
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");
439 node_set(node, numa_nodes_parsed);
440 node_set_state(node, N_GENERIC_INITIATOR);
446 acpi_parse_gi_affinity(union acpi_subtable_headers *header,
447 const unsigned long end)
451 #endif /* defined(CONFIG_X86) || defined (CONFIG_ARM64) */
453 static int __init acpi_parse_srat(struct acpi_table_header *table)
455 struct acpi_table_srat *srat = (struct acpi_table_srat *)table;
457 acpi_srat_revision = srat->header.revision;
459 /* Real work done in acpi_table_parse_srat below. */
465 acpi_table_parse_srat(enum acpi_srat_type id,
466 acpi_tbl_entry_handler handler, unsigned int max_entries)
468 return acpi_table_parse_entries(ACPI_SIG_SRAT,
469 sizeof(struct acpi_table_srat), id,
470 handler, max_entries);
473 int __init acpi_numa_init(void)
475 int i, fake_pxm, cnt = 0;
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.
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];
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;
500 acpi_table_parse_entries_array(ACPI_SIG_SRAT,
501 sizeof(struct acpi_table_srat),
502 srat_proc, ARRAY_SIZE(srat_proc), 0);
504 cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
505 acpi_parse_memory_affinity, 0);
508 /* SLIT: System Locality Information Table */
509 acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
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.
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];
523 last_real_pxm = fake_pxm;
525 acpi_table_parse_cedt(ACPI_CEDT_TYPE_CFMWS, acpi_parse_cfmws,
530 else if (!parsed_numa_memblks)
535 bool acpi_node_backed_by_real_pxm(int nid)
537 int pxm = node_to_pxm(nid);
539 return pxm <= last_real_pxm;
541 EXPORT_SYMBOL_GPL(acpi_node_backed_by_real_pxm);
543 static int acpi_get_pxm(acpi_handle h)
545 unsigned long long pxm;
548 acpi_handle phandle = h;
552 status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
553 if (ACPI_SUCCESS(status))
555 status = acpi_get_parent(handle, &phandle);
556 } while (ACPI_SUCCESS(status));
560 int acpi_get_node(acpi_handle handle)
564 pxm = acpi_get_pxm(handle);
566 return pxm_to_node(pxm);
568 EXPORT_SYMBOL(acpi_get_node);