1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * acpi_numa.c - ACPI NUMA support
5 * Copyright (C) 2002 Takayoshi Kochi <t-kochi@bq.jp.nec.com>
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 void __init
disable_srat(void)
37 int pxm_to_node(int pxm
)
39 if (pxm
< 0 || pxm
>= MAX_PXM_DOMAINS
|| numa_off
)
41 return pxm_to_node_map
[pxm
];
43 EXPORT_SYMBOL(pxm_to_node
);
45 int node_to_pxm(int node
)
49 return node_to_pxm_map
[node
];
52 static void __acpi_map_pxm_to_node(int pxm
, int node
)
54 if (pxm_to_node_map
[pxm
] == NUMA_NO_NODE
|| node
< pxm_to_node_map
[pxm
])
55 pxm_to_node_map
[pxm
] = node
;
56 if (node_to_pxm_map
[node
] == PXM_INVAL
|| pxm
< node_to_pxm_map
[node
])
57 node_to_pxm_map
[node
] = pxm
;
60 int acpi_map_pxm_to_node(int pxm
)
64 if (pxm
< 0 || pxm
>= MAX_PXM_DOMAINS
|| numa_off
)
67 node
= pxm_to_node_map
[pxm
];
69 if (node
== NUMA_NO_NODE
) {
70 if (nodes_weight(nodes_found_map
) >= MAX_NUMNODES
)
72 node
= first_unset_node(nodes_found_map
);
73 __acpi_map_pxm_to_node(pxm
, node
);
74 node_set(node
, nodes_found_map
);
79 EXPORT_SYMBOL(acpi_map_pxm_to_node
);
82 acpi_table_print_srat_entry(struct acpi_subtable_header
*header
)
84 switch (header
->type
) {
85 case ACPI_SRAT_TYPE_CPU_AFFINITY
:
87 struct acpi_srat_cpu_affinity
*p
=
88 (struct acpi_srat_cpu_affinity
*)header
;
89 pr_debug("SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
90 p
->apic_id
, p
->local_sapic_eid
,
91 p
->proximity_domain_lo
,
92 (p
->flags
& ACPI_SRAT_CPU_ENABLED
) ?
93 "enabled" : "disabled");
97 case ACPI_SRAT_TYPE_MEMORY_AFFINITY
:
99 struct acpi_srat_mem_affinity
*p
=
100 (struct acpi_srat_mem_affinity
*)header
;
101 pr_debug("SRAT Memory (0x%llx length 0x%llx) in proximity domain %d %s%s%s\n",
102 (unsigned long long)p
->base_address
,
103 (unsigned long long)p
->length
,
105 (p
->flags
& ACPI_SRAT_MEM_ENABLED
) ?
106 "enabled" : "disabled",
107 (p
->flags
& ACPI_SRAT_MEM_HOT_PLUGGABLE
) ?
108 " hot-pluggable" : "",
109 (p
->flags
& ACPI_SRAT_MEM_NON_VOLATILE
) ?
110 " non-volatile" : "");
114 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY
:
116 struct acpi_srat_x2apic_cpu_affinity
*p
=
117 (struct acpi_srat_x2apic_cpu_affinity
*)header
;
118 pr_debug("SRAT Processor (x2apicid[0x%08x]) in proximity domain %d %s\n",
121 (p
->flags
& ACPI_SRAT_CPU_ENABLED
) ?
122 "enabled" : "disabled");
126 case ACPI_SRAT_TYPE_GICC_AFFINITY
:
128 struct acpi_srat_gicc_affinity
*p
=
129 (struct acpi_srat_gicc_affinity
*)header
;
130 pr_debug("SRAT Processor (acpi id[0x%04x]) in proximity domain %d %s\n",
131 p
->acpi_processor_uid
,
133 (p
->flags
& ACPI_SRAT_GICC_ENABLED
) ?
134 "enabled" : "disabled");
138 case ACPI_SRAT_TYPE_GENERIC_AFFINITY
:
140 struct acpi_srat_generic_affinity
*p
=
141 (struct acpi_srat_generic_affinity
*)header
;
143 if (p
->device_handle_type
== 0) {
145 * For pci devices this may be the only place they
146 * are assigned a proximity domain
148 pr_debug("SRAT Generic Initiator(Seg:%u BDF:%u) in proximity domain %d %s\n",
149 *(u16
*)(&p
->device_handle
[0]),
150 *(u16
*)(&p
->device_handle
[2]),
152 (p
->flags
& ACPI_SRAT_GENERIC_AFFINITY_ENABLED
) ?
153 "enabled" : "disabled");
156 * In this case we can rely on the device having a
157 * proximity domain reference
159 pr_debug("SRAT Generic Initiator(HID=%.8s UID=%.4s) in proximity domain %d %s\n",
160 (char *)(&p
->device_handle
[0]),
161 (char *)(&p
->device_handle
[8]),
163 (p
->flags
& ACPI_SRAT_GENERIC_AFFINITY_ENABLED
) ?
164 "enabled" : "disabled");
169 pr_warn("Found unsupported SRAT entry (type = 0x%x)\n",
176 * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
177 * up the NUMA heuristics which wants the local node to have a smaller
178 * distance than the others.
179 * Do some quick checks here and only use the SLIT if it passes.
181 static int __init
slit_valid(struct acpi_table_slit
*slit
)
184 int d
= slit
->locality_count
;
185 for (i
= 0; i
< d
; i
++) {
186 for (j
= 0; j
< d
; j
++) {
187 u8 val
= slit
->entry
[d
*i
+ j
];
189 if (val
!= LOCAL_DISTANCE
)
191 } else if (val
<= LOCAL_DISTANCE
)
198 void __init
bad_srat(void)
200 pr_err("SRAT: SRAT not used.\n");
204 int __init
srat_disabled(void)
206 return acpi_numa
< 0;
209 #if defined(CONFIG_X86) || defined(CONFIG_ARM64)
211 * Callback for SLIT parsing. pxm_to_node() returns NUMA_NO_NODE for
212 * I/O localities since SRAT does not list them. I/O localities are
213 * not supported at this point.
215 void __init
acpi_numa_slit_init(struct acpi_table_slit
*slit
)
219 for (i
= 0; i
< slit
->locality_count
; i
++) {
220 const int from_node
= pxm_to_node(i
);
222 if (from_node
== NUMA_NO_NODE
)
225 for (j
= 0; j
< slit
->locality_count
; j
++) {
226 const int to_node
= pxm_to_node(j
);
228 if (to_node
== NUMA_NO_NODE
)
231 numa_set_distance(from_node
, to_node
,
232 slit
->entry
[slit
->locality_count
* i
+ j
]);
238 * Default callback for parsing of the Proximity Domain <-> Memory
242 acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity
*ma
)
250 if (ma
->header
.length
< sizeof(struct acpi_srat_mem_affinity
)) {
251 pr_err("SRAT: Unexpected header length: %d\n",
253 goto out_err_bad_srat
;
255 if ((ma
->flags
& ACPI_SRAT_MEM_ENABLED
) == 0)
257 hotpluggable
= ma
->flags
& ACPI_SRAT_MEM_HOT_PLUGGABLE
;
258 if (hotpluggable
&& !IS_ENABLED(CONFIG_MEMORY_HOTPLUG
))
261 start
= ma
->base_address
;
262 end
= start
+ ma
->length
;
263 pxm
= ma
->proximity_domain
;
264 if (acpi_srat_revision
<= 1)
267 node
= acpi_map_pxm_to_node(pxm
);
268 if (node
== NUMA_NO_NODE
) {
269 pr_err("SRAT: Too many proximity domains.\n");
270 goto out_err_bad_srat
;
273 if (numa_add_memblk(node
, start
, end
) < 0) {
274 pr_err("SRAT: Failed to add memblk to node %u [mem %#010Lx-%#010Lx]\n",
275 node
, (unsigned long long) start
,
276 (unsigned long long) end
- 1);
277 goto out_err_bad_srat
;
280 node_set(node
, numa_nodes_parsed
);
282 pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
284 (unsigned long long) start
, (unsigned long long) end
- 1,
285 hotpluggable
? " hotplug" : "",
286 ma
->flags
& ACPI_SRAT_MEM_NON_VOLATILE
? " non-volatile" : "");
288 /* Mark hotplug range in memblock. */
289 if (hotpluggable
&& memblock_mark_hotplug(start
, ma
->length
))
290 pr_warn("SRAT: Failed to mark hotplug range [mem %#010Lx-%#010Lx] in memblock\n",
291 (unsigned long long)start
, (unsigned long long)end
- 1);
293 max_possible_pfn
= max(max_possible_pfn
, PFN_UP(end
- 1));
301 #endif /* defined(CONFIG_X86) || defined (CONFIG_ARM64) */
303 static int __init
acpi_parse_slit(struct acpi_table_header
*table
)
305 struct acpi_table_slit
*slit
= (struct acpi_table_slit
*)table
;
307 if (!slit_valid(slit
)) {
308 pr_info("SLIT table looks invalid. Not used.\n");
311 acpi_numa_slit_init(slit
);
317 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity
*pa
)
319 pr_warn("Found unsupported x2apic [0x%08x] SRAT entry\n", pa
->apic_id
);
323 acpi_parse_x2apic_affinity(union acpi_subtable_headers
*header
,
324 const unsigned long end
)
326 struct acpi_srat_x2apic_cpu_affinity
*processor_affinity
;
328 processor_affinity
= (struct acpi_srat_x2apic_cpu_affinity
*)header
;
330 acpi_table_print_srat_entry(&header
->common
);
332 /* let architecture-dependent part to do it */
333 acpi_numa_x2apic_affinity_init(processor_affinity
);
339 acpi_parse_processor_affinity(union acpi_subtable_headers
*header
,
340 const unsigned long end
)
342 struct acpi_srat_cpu_affinity
*processor_affinity
;
344 processor_affinity
= (struct acpi_srat_cpu_affinity
*)header
;
346 acpi_table_print_srat_entry(&header
->common
);
348 /* let architecture-dependent part to do it */
349 acpi_numa_processor_affinity_init(processor_affinity
);
355 acpi_parse_gicc_affinity(union acpi_subtable_headers
*header
,
356 const unsigned long end
)
358 struct acpi_srat_gicc_affinity
*processor_affinity
;
360 processor_affinity
= (struct acpi_srat_gicc_affinity
*)header
;
362 acpi_table_print_srat_entry(&header
->common
);
364 /* let architecture-dependent part to do it */
365 acpi_numa_gicc_affinity_init(processor_affinity
);
370 #if defined(CONFIG_X86) || defined(CONFIG_ARM64)
372 acpi_parse_gi_affinity(union acpi_subtable_headers
*header
,
373 const unsigned long end
)
375 struct acpi_srat_generic_affinity
*gi_affinity
;
378 gi_affinity
= (struct acpi_srat_generic_affinity
*)header
;
381 acpi_table_print_srat_entry(&header
->common
);
383 if (!(gi_affinity
->flags
& ACPI_SRAT_GENERIC_AFFINITY_ENABLED
))
386 node
= acpi_map_pxm_to_node(gi_affinity
->proximity_domain
);
387 if (node
== NUMA_NO_NODE
|| node
>= MAX_NUMNODES
) {
388 pr_err("SRAT: Too many proximity domains.\n");
391 node_set(node
, numa_nodes_parsed
);
392 node_set_state(node
, N_GENERIC_INITIATOR
);
398 acpi_parse_gi_affinity(union acpi_subtable_headers
*header
,
399 const unsigned long end
)
403 #endif /* defined(CONFIG_X86) || defined (CONFIG_ARM64) */
405 static int __initdata parsed_numa_memblks
;
408 acpi_parse_memory_affinity(union acpi_subtable_headers
* header
,
409 const unsigned long end
)
411 struct acpi_srat_mem_affinity
*memory_affinity
;
413 memory_affinity
= (struct acpi_srat_mem_affinity
*)header
;
415 acpi_table_print_srat_entry(&header
->common
);
417 /* let architecture-dependent part to do it */
418 if (!acpi_numa_memory_affinity_init(memory_affinity
))
419 parsed_numa_memblks
++;
423 static int __init
acpi_parse_srat(struct acpi_table_header
*table
)
425 struct acpi_table_srat
*srat
= (struct acpi_table_srat
*)table
;
427 acpi_srat_revision
= srat
->header
.revision
;
429 /* Real work done in acpi_table_parse_srat below. */
435 acpi_table_parse_srat(enum acpi_srat_type id
,
436 acpi_tbl_entry_handler handler
, unsigned int max_entries
)
438 return acpi_table_parse_entries(ACPI_SIG_SRAT
,
439 sizeof(struct acpi_table_srat
), id
,
440 handler
, max_entries
);
443 int __init
acpi_numa_init(void)
451 * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
452 * SRAT cpu entries could have different order with that in MADT.
453 * So go over all cpu entries in SRAT to get apicid to node mapping.
456 /* SRAT: System Resource Affinity Table */
457 if (!acpi_table_parse(ACPI_SIG_SRAT
, acpi_parse_srat
)) {
458 struct acpi_subtable_proc srat_proc
[4];
460 memset(srat_proc
, 0, sizeof(srat_proc
));
461 srat_proc
[0].id
= ACPI_SRAT_TYPE_CPU_AFFINITY
;
462 srat_proc
[0].handler
= acpi_parse_processor_affinity
;
463 srat_proc
[1].id
= ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY
;
464 srat_proc
[1].handler
= acpi_parse_x2apic_affinity
;
465 srat_proc
[2].id
= ACPI_SRAT_TYPE_GICC_AFFINITY
;
466 srat_proc
[2].handler
= acpi_parse_gicc_affinity
;
467 srat_proc
[3].id
= ACPI_SRAT_TYPE_GENERIC_AFFINITY
;
468 srat_proc
[3].handler
= acpi_parse_gi_affinity
;
470 acpi_table_parse_entries_array(ACPI_SIG_SRAT
,
471 sizeof(struct acpi_table_srat
),
472 srat_proc
, ARRAY_SIZE(srat_proc
), 0);
474 cnt
= acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY
,
475 acpi_parse_memory_affinity
, 0);
478 /* SLIT: System Locality Information Table */
479 acpi_table_parse(ACPI_SIG_SLIT
, acpi_parse_slit
);
483 else if (!parsed_numa_memblks
)
488 static int acpi_get_pxm(acpi_handle h
)
490 unsigned long long pxm
;
493 acpi_handle phandle
= h
;
497 status
= acpi_evaluate_integer(handle
, "_PXM", NULL
, &pxm
);
498 if (ACPI_SUCCESS(status
))
500 status
= acpi_get_parent(handle
, &phandle
);
501 } while (ACPI_SUCCESS(status
));
505 int acpi_get_node(acpi_handle handle
)
509 pxm
= acpi_get_pxm(handle
);
511 return pxm_to_node(pxm
);
513 EXPORT_SYMBOL(acpi_get_node
);