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