Skip to content

Commit eabd9db

Browse files
xiaobo55xpalmer-dabbelt
authored andcommitted
ACPI: RISCV: Add NUMA support based on SRAT and SLIT
Add acpi_numa.c file to enable parse NUMA information from ACPI SRAT and SLIT tables. SRAT table provide CPUs(Hart) and memory nodes to proximity domain mapping, while SLIT table provide the distance metrics between proximity domains. Signed-off-by: Haibo Xu <[email protected]> Reviewed-by: Sunil V L <[email protected]> Reviewed-by: Hanjun Guo <[email protected]> Link: https://lore.kernel.org/r/65dbad1fda08a32922c44886e4581e49b4a2fecc.1718268003.git.haibo1.xu@intel.com Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 1613e60 commit eabd9db

File tree

7 files changed

+154
-10
lines changed

7 files changed

+154
-10
lines changed

arch/riscv/include/asm/acpi.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ static inline void arch_fix_phys_package_id(int num, u32 slot) { }
6161

6262
void acpi_init_rintc_map(void);
6363
struct acpi_madt_rintc *acpi_cpu_get_madt_rintc(int cpu);
64-
u32 get_acpi_id_for_cpu(int cpu);
64+
static inline u32 get_acpi_id_for_cpu(int cpu)
65+
{
66+
return acpi_cpu_get_madt_rintc(cpu)->uid;
67+
}
68+
6569
int acpi_get_riscv_isa(struct acpi_table_header *table,
6670
unsigned int cpu, const char **isa);
6771

68-
static inline int acpi_numa_get_nid(unsigned int cpu) { return NUMA_NO_NODE; }
6972
void acpi_get_cbo_block_size(struct acpi_table_header *table, u32 *cbom_size,
7073
u32 *cboz_size, u32 *cbop_size);
7174
#else
@@ -87,4 +90,12 @@ static inline void acpi_get_cbo_block_size(struct acpi_table_header *table,
8790

8891
#endif /* CONFIG_ACPI */
8992

93+
#ifdef CONFIG_ACPI_NUMA
94+
int acpi_numa_get_nid(unsigned int cpu);
95+
void acpi_map_cpus_to_nodes(void);
96+
#else
97+
static inline int acpi_numa_get_nid(unsigned int cpu) { return NUMA_NO_NODE; }
98+
static inline void acpi_map_cpus_to_nodes(void) { }
99+
#endif /* CONFIG_ACPI_NUMA */
100+
90101
#endif /*_ASM_ACPI_H*/

arch/riscv/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,4 @@ obj-$(CONFIG_COMPAT) += compat_vdso/
110110

111111
obj-$(CONFIG_64BIT) += pi/
112112
obj-$(CONFIG_ACPI) += acpi.o
113+
obj-$(CONFIG_ACPI_NUMA) += acpi_numa.o

arch/riscv/kernel/acpi.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,6 @@ struct acpi_madt_rintc *acpi_cpu_get_madt_rintc(int cpu)
191191
return &cpu_madt_rintc[cpu];
192192
}
193193

194-
u32 get_acpi_id_for_cpu(int cpu)
195-
{
196-
return acpi_cpu_get_madt_rintc(cpu)->uid;
197-
}
198-
199194
/*
200195
* __acpi_map_table() will be called before paging_init(), so early_ioremap()
201196
* or early_memremap() should be called here to for ACPI table mapping.

arch/riscv/kernel/acpi_numa.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* ACPI 6.6 based NUMA setup for RISCV
4+
* Lots of code was borrowed from arch/arm64/kernel/acpi_numa.c
5+
*
6+
* Copyright 2004 Andi Kleen, SuSE Labs.
7+
* Copyright (C) 2013-2016, Linaro Ltd.
8+
* Author: Hanjun Guo <[email protected]>
9+
* Copyright (C) 2024 Intel Corporation.
10+
*
11+
* Reads the ACPI SRAT table to figure out what memory belongs to which CPUs.
12+
*
13+
* Called from acpi_numa_init while reading the SRAT and SLIT tables.
14+
* Assumes all memory regions belonging to a single proximity domain
15+
* are in one chunk. Holes between them will be included in the node.
16+
*/
17+
18+
#define pr_fmt(fmt) "ACPI: NUMA: " fmt
19+
20+
#include <linux/acpi.h>
21+
#include <linux/bitmap.h>
22+
#include <linux/kernel.h>
23+
#include <linux/mm.h>
24+
#include <linux/memblock.h>
25+
#include <linux/mmzone.h>
26+
#include <linux/module.h>
27+
#include <linux/topology.h>
28+
29+
#include <asm/numa.h>
30+
31+
static int acpi_early_node_map[NR_CPUS] __initdata = { NUMA_NO_NODE };
32+
33+
int __init acpi_numa_get_nid(unsigned int cpu)
34+
{
35+
return acpi_early_node_map[cpu];
36+
}
37+
38+
static inline int get_cpu_for_acpi_id(u32 uid)
39+
{
40+
int cpu;
41+
42+
for (cpu = 0; cpu < nr_cpu_ids; cpu++)
43+
if (uid == get_acpi_id_for_cpu(cpu))
44+
return cpu;
45+
46+
return -EINVAL;
47+
}
48+
49+
static int __init acpi_parse_rintc_pxm(union acpi_subtable_headers *header,
50+
const unsigned long end)
51+
{
52+
struct acpi_srat_rintc_affinity *pa;
53+
int cpu, pxm, node;
54+
55+
if (srat_disabled())
56+
return -EINVAL;
57+
58+
pa = (struct acpi_srat_rintc_affinity *)header;
59+
if (!pa)
60+
return -EINVAL;
61+
62+
if (!(pa->flags & ACPI_SRAT_RINTC_ENABLED))
63+
return 0;
64+
65+
pxm = pa->proximity_domain;
66+
node = pxm_to_node(pxm);
67+
68+
/*
69+
* If we can't map the UID to a logical cpu this
70+
* means that the UID is not part of possible cpus
71+
* so we do not need a NUMA mapping for it, skip
72+
* the SRAT entry and keep parsing.
73+
*/
74+
cpu = get_cpu_for_acpi_id(pa->acpi_processor_uid);
75+
if (cpu < 0)
76+
return 0;
77+
78+
acpi_early_node_map[cpu] = node;
79+
pr_info("SRAT: PXM %d -> HARTID 0x%lx -> Node %d\n", pxm,
80+
cpuid_to_hartid_map(cpu), node);
81+
82+
return 0;
83+
}
84+
85+
void __init acpi_map_cpus_to_nodes(void)
86+
{
87+
int i;
88+
89+
/*
90+
* In ACPI, SMP and CPU NUMA information is provided in separate
91+
* static tables, namely the MADT and the SRAT.
92+
*
93+
* Thus, it is simpler to first create the cpu logical map through
94+
* an MADT walk and then map the logical cpus to their node ids
95+
* as separate steps.
96+
*/
97+
acpi_table_parse_entries(ACPI_SIG_SRAT, sizeof(struct acpi_table_srat),
98+
ACPI_SRAT_TYPE_RINTC_AFFINITY, acpi_parse_rintc_pxm, 0);
99+
100+
for (i = 0; i < nr_cpu_ids; i++)
101+
early_map_cpu_to_node(i, acpi_numa_get_nid(i));
102+
}
103+
104+
/* Callback for Proximity Domain -> logical node ID mapping */
105+
void __init acpi_numa_rintc_affinity_init(struct acpi_srat_rintc_affinity *pa)
106+
{
107+
int pxm, node;
108+
109+
if (srat_disabled())
110+
return;
111+
112+
if (pa->header.length < sizeof(struct acpi_srat_rintc_affinity)) {
113+
pr_err("SRAT: Invalid SRAT header length: %d\n", pa->header.length);
114+
bad_srat();
115+
return;
116+
}
117+
118+
if (!(pa->flags & ACPI_SRAT_RINTC_ENABLED))
119+
return;
120+
121+
pxm = pa->proximity_domain;
122+
node = acpi_map_pxm_to_node(pxm);
123+
124+
if (node == NUMA_NO_NODE) {
125+
pr_err("SRAT: Too many proximity domains %d\n", pxm);
126+
bad_srat();
127+
return;
128+
}
129+
130+
node_set(node, numa_nodes_parsed);
131+
}

arch/riscv/kernel/setup.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,10 @@ void __init setup_arch(char **cmdline_p)
281281
setup_smp();
282282
#endif
283283

284-
if (!acpi_disabled)
284+
if (!acpi_disabled) {
285285
acpi_init_rintc_map();
286+
acpi_map_cpus_to_nodes();
287+
}
286288

287289
riscv_init_cbo_blocksizes();
288290
riscv_fill_hwcap();

arch/riscv/kernel/smpboot.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ static int __init acpi_parse_rintc(union acpi_subtable_headers *header, const un
9696
if (hart == cpuid_to_hartid_map(0)) {
9797
BUG_ON(found_boot_cpu);
9898
found_boot_cpu = true;
99-
early_map_cpu_to_node(0, acpi_numa_get_nid(cpu_count));
10099
return 0;
101100
}
102101

@@ -106,7 +105,6 @@ static int __init acpi_parse_rintc(union acpi_subtable_headers *header, const un
106105
}
107106

108107
cpuid_to_hartid_map(cpu_count) = hart;
109-
early_map_cpu_to_node(cpu_count, acpi_numa_get_nid(cpu_count));
110108
cpu_count++;
111109

112110
return 0;

include/linux/acpi.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ static inline void
264264
acpi_numa_gicc_affinity_init(struct acpi_srat_gicc_affinity *pa) { }
265265
#endif
266266

267+
#ifdef CONFIG_RISCV
268+
void acpi_numa_rintc_affinity_init(struct acpi_srat_rintc_affinity *pa);
269+
#else
270+
static inline void acpi_numa_rintc_affinity_init(struct acpi_srat_rintc_affinity *pa) { }
271+
#endif
272+
267273
#ifndef PHYS_CPUID_INVALID
268274
typedef u32 phys_cpuid_t;
269275
#define PHYS_CPUID_INVALID (phys_cpuid_t)(-1)

0 commit comments

Comments
 (0)