]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
ACPI: RISCV: Add NUMA support based on SRAT and SLIT
authorHaibo Xu <haibo1.xu@intel.com>
Thu, 13 Jun 2024 08:54:33 +0000 (16:54 +0800)
committerPalmer Dabbelt <palmer@rivosinc.com>
Mon, 22 Jul 2024 14:13:06 +0000 (07:13 -0700)
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 <haibo1.xu@intel.com>
Reviewed-by: Sunil V L <sunilvl@ventanamicro.com>
Reviewed-by: Hanjun Guo <guohanjun@huawei.com>
Link: https://lore.kernel.org/r/65dbad1fda08a32922c44886e4581e49b4a2fecc.1718268003.git.haibo1.xu@intel.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/include/asm/acpi.h
arch/riscv/kernel/Makefile
arch/riscv/kernel/acpi.c
arch/riscv/kernel/acpi_numa.c [new file with mode: 0644]
arch/riscv/kernel/setup.c
arch/riscv/kernel/smpboot.c
include/linux/acpi.h

index 7dad0cf9d701f66135257753d0bb047fda59b5d2..e0a1f84404f31cc7fd467d50f9282f54b91860d7 100644 (file)
@@ -61,11 +61,14 @@ static inline void arch_fix_phys_package_id(int num, u32 slot) { }
 
 void acpi_init_rintc_map(void);
 struct acpi_madt_rintc *acpi_cpu_get_madt_rintc(int cpu);
-u32 get_acpi_id_for_cpu(int cpu);
+static inline u32 get_acpi_id_for_cpu(int cpu)
+{
+       return acpi_cpu_get_madt_rintc(cpu)->uid;
+}
+
 int acpi_get_riscv_isa(struct acpi_table_header *table,
                       unsigned int cpu, const char **isa);
 
-static inline int acpi_numa_get_nid(unsigned int cpu) { return NUMA_NO_NODE; }
 void acpi_get_cbo_block_size(struct acpi_table_header *table, u32 *cbom_size,
                             u32 *cboz_size, u32 *cbop_size);
 #else
@@ -87,4 +90,12 @@ static inline void acpi_get_cbo_block_size(struct acpi_table_header *table,
 
 #endif /* CONFIG_ACPI */
 
+#ifdef CONFIG_ACPI_NUMA
+int acpi_numa_get_nid(unsigned int cpu);
+void acpi_map_cpus_to_nodes(void);
+#else
+static inline int acpi_numa_get_nid(unsigned int cpu) { return NUMA_NO_NODE; }
+static inline void acpi_map_cpus_to_nodes(void) { }
+#endif /* CONFIG_ACPI_NUMA */
+
 #endif /*_ASM_ACPI_H*/
index 5b243d46f4b17477a70bbb809a580d60453d8f47..1e2afec141b5eb9069247af99cf90d1f9fe306ff 100644 (file)
@@ -110,3 +110,4 @@ obj-$(CONFIG_COMPAT)                += compat_vdso/
 
 obj-$(CONFIG_64BIT)            += pi/
 obj-$(CONFIG_ACPI)             += acpi.o
+obj-$(CONFIG_ACPI_NUMA)        += acpi_numa.o
index e619edc8b0cc972c1a83809c9f0ebeb10c3331a6..040bdbfea2b424fe7fe21963e9c74e54a3619626 100644 (file)
@@ -191,11 +191,6 @@ struct acpi_madt_rintc *acpi_cpu_get_madt_rintc(int cpu)
        return &cpu_madt_rintc[cpu];
 }
 
-u32 get_acpi_id_for_cpu(int cpu)
-{
-       return acpi_cpu_get_madt_rintc(cpu)->uid;
-}
-
 /*
  * __acpi_map_table() will be called before paging_init(), so early_ioremap()
  * or early_memremap() should be called here to for ACPI table mapping.
diff --git a/arch/riscv/kernel/acpi_numa.c b/arch/riscv/kernel/acpi_numa.c
new file mode 100644 (file)
index 0000000..0231482
--- /dev/null
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ACPI 6.6 based NUMA setup for RISCV
+ * Lots of code was borrowed from arch/arm64/kernel/acpi_numa.c
+ *
+ * Copyright 2004 Andi Kleen, SuSE Labs.
+ * Copyright (C) 2013-2016, Linaro Ltd.
+ *             Author: Hanjun Guo <hanjun.guo@linaro.org>
+ * Copyright (C) 2024 Intel Corporation.
+ *
+ * Reads the ACPI SRAT table to figure out what memory belongs to which CPUs.
+ *
+ * Called from acpi_numa_init while reading the SRAT and SLIT tables.
+ * Assumes all memory regions belonging to a single proximity domain
+ * are in one chunk. Holes between them will be included in the node.
+ */
+
+#define pr_fmt(fmt) "ACPI: NUMA: " fmt
+
+#include <linux/acpi.h>
+#include <linux/bitmap.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+#include <linux/memblock.h>
+#include <linux/mmzone.h>
+#include <linux/module.h>
+#include <linux/topology.h>
+
+#include <asm/numa.h>
+
+static int acpi_early_node_map[NR_CPUS] __initdata = { NUMA_NO_NODE };
+
+int __init acpi_numa_get_nid(unsigned int cpu)
+{
+       return acpi_early_node_map[cpu];
+}
+
+static inline int get_cpu_for_acpi_id(u32 uid)
+{
+       int cpu;
+
+       for (cpu = 0; cpu < nr_cpu_ids; cpu++)
+               if (uid == get_acpi_id_for_cpu(cpu))
+                       return cpu;
+
+       return -EINVAL;
+}
+
+static int __init acpi_parse_rintc_pxm(union acpi_subtable_headers *header,
+                                      const unsigned long end)
+{
+       struct acpi_srat_rintc_affinity *pa;
+       int cpu, pxm, node;
+
+       if (srat_disabled())
+               return -EINVAL;
+
+       pa = (struct acpi_srat_rintc_affinity *)header;
+       if (!pa)
+               return -EINVAL;
+
+       if (!(pa->flags & ACPI_SRAT_RINTC_ENABLED))
+               return 0;
+
+       pxm = pa->proximity_domain;
+       node = pxm_to_node(pxm);
+
+       /*
+        * If we can't map the UID to a logical cpu this
+        * means that the UID is not part of possible cpus
+        * so we do not need a NUMA mapping for it, skip
+        * the SRAT entry and keep parsing.
+        */
+       cpu = get_cpu_for_acpi_id(pa->acpi_processor_uid);
+       if (cpu < 0)
+               return 0;
+
+       acpi_early_node_map[cpu] = node;
+       pr_info("SRAT: PXM %d -> HARTID 0x%lx -> Node %d\n", pxm,
+               cpuid_to_hartid_map(cpu), node);
+
+       return 0;
+}
+
+void __init acpi_map_cpus_to_nodes(void)
+{
+       int i;
+
+       /*
+        * In ACPI, SMP and CPU NUMA information is provided in separate
+        * static tables, namely the MADT and the SRAT.
+        *
+        * Thus, it is simpler to first create the cpu logical map through
+        * an MADT walk and then map the logical cpus to their node ids
+        * as separate steps.
+        */
+       acpi_table_parse_entries(ACPI_SIG_SRAT, sizeof(struct acpi_table_srat),
+                                ACPI_SRAT_TYPE_RINTC_AFFINITY, acpi_parse_rintc_pxm, 0);
+
+       for (i = 0; i < nr_cpu_ids; i++)
+               early_map_cpu_to_node(i, acpi_numa_get_nid(i));
+}
+
+/* Callback for Proximity Domain -> logical node ID mapping */
+void __init acpi_numa_rintc_affinity_init(struct acpi_srat_rintc_affinity *pa)
+{
+       int pxm, node;
+
+       if (srat_disabled())
+               return;
+
+       if (pa->header.length < sizeof(struct acpi_srat_rintc_affinity)) {
+               pr_err("SRAT: Invalid SRAT header length: %d\n", pa->header.length);
+               bad_srat();
+               return;
+       }
+
+       if (!(pa->flags & ACPI_SRAT_RINTC_ENABLED))
+               return;
+
+       pxm = pa->proximity_domain;
+       node = acpi_map_pxm_to_node(pxm);
+
+       if (node == NUMA_NO_NODE) {
+               pr_err("SRAT: Too many proximity domains %d\n", pxm);
+               bad_srat();
+               return;
+       }
+
+       node_set(node, numa_nodes_parsed);
+}
index 4f73c0ae44b25d443a64eb9ae75a01d7372d4fca..a2cde65b69e950d929e3f9a5165d5f72d307e850 100644 (file)
@@ -281,8 +281,10 @@ void __init setup_arch(char **cmdline_p)
        setup_smp();
 #endif
 
-       if (!acpi_disabled)
+       if (!acpi_disabled) {
                acpi_init_rintc_map();
+               acpi_map_cpus_to_nodes();
+       }
 
        riscv_init_cbo_blocksizes();
        riscv_fill_hwcap();
index 1319b29ce3b59bd4daa203acad44919b9d05db24..d49c0bf0e2cd31e3206803e1744cb432b0b876e4 100644 (file)
@@ -96,7 +96,6 @@ static int __init acpi_parse_rintc(union acpi_subtable_headers *header, const un
        if (hart == cpuid_to_hartid_map(0)) {
                BUG_ON(found_boot_cpu);
                found_boot_cpu = true;
-               early_map_cpu_to_node(0, acpi_numa_get_nid(cpu_count));
                return 0;
        }
 
@@ -106,7 +105,6 @@ static int __init acpi_parse_rintc(union acpi_subtable_headers *header, const un
        }
 
        cpuid_to_hartid_map(cpu_count) = hart;
-       early_map_cpu_to_node(cpu_count, acpi_numa_get_nid(cpu_count));
        cpu_count++;
 
        return 0;
index 28c3fb2bef0daacbd524476dc3a4fd97de29b22c..5a2b620ccd58801ffbf4750fc42ef7a74d7836b1 100644 (file)
@@ -264,6 +264,12 @@ static inline void
 acpi_numa_gicc_affinity_init(struct acpi_srat_gicc_affinity *pa) { }
 #endif
 
+#ifdef CONFIG_RISCV
+void acpi_numa_rintc_affinity_init(struct acpi_srat_rintc_affinity *pa);
+#else
+static inline void acpi_numa_rintc_affinity_init(struct acpi_srat_rintc_affinity *pa) { }
+#endif
+
 #ifndef PHYS_CPUID_INVALID
 typedef u32 phys_cpuid_t;
 #define PHYS_CPUID_INVALID (phys_cpuid_t)(-1)