Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/acpi Add ACPI SRAT parser. This is a part of NUMA su...



details:   https://anonhg.NetBSD.org/src/rev/1fcdb3de7db3
branches:  trunk
changeset: 749287:1fcdb3de7db3
user:      cegger <cegger%NetBSD.org@localhost>
date:      Wed Nov 25 13:17:06 2009 +0000

description:
Add ACPI SRAT parser. This is a part of NUMA support.
Tested on 1-node, 2-node and 8-node machines.
Patch presented on tech-kern@, port-i386@ and port-amd64@.

No comments.

diffstat:

 sys/dev/acpi/acpi_srat.c |  513 +++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/acpi/acpi_srat.h |   98 ++++++++
 sys/dev/acpi/files.acpi  |    3 +-
 3 files changed, 613 insertions(+), 1 deletions(-)

diffs (truncated from 636 to 300 lines):

diff -r a304c3595782 -r 1fcdb3de7db3 sys/dev/acpi/acpi_srat.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/acpi/acpi_srat.c  Wed Nov 25 13:17:06 2009 +0000
@@ -0,0 +1,513 @@
+/* $NetBSD $ */
+
+/*
+ * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christoph Egger.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+
+#include <sys/kmem.h>
+
+#include <dev/acpi/acpica.h>
+#include <dev/acpi/acpivar.h>
+#include <dev/acpi/acpi_srat.h>
+
+static ACPI_TABLE_SRAT *srat;
+
+struct acpisrat_node {
+       acpisrat_nodeid_t nodeid;
+       uint32_t ncpus; /* Number of cpus in this node */
+       struct acpisrat_cpu **cpu; /* Array of cpus */
+       uint32_t nmems; /* Number of memory ranges in this node */
+       struct acpisrat_mem **mem; /* Array of memory ranges */
+};
+
+static uint32_t nnodes; /* Number of NUMA nodes */
+static struct acpisrat_node *node_array; /* Array of NUMA nodes */
+static uint32_t ncpus; /* Number of CPUs */
+static struct acpisrat_cpu *cpu_array; /* Array of cpus */
+static uint32_t nmems; /* Number of Memory ranges */
+static struct acpisrat_mem *mem_array;
+
+
+struct cpulist {
+       struct acpisrat_cpu cpu;
+       TAILQ_ENTRY(cpulist) entry;
+};
+
+static TAILQ_HEAD(, cpulist) cpulisthead;
+
+#define CPU_INIT               TAILQ_INIT(&cpulisthead);
+#define CPU_FOREACH(cpu)       TAILQ_FOREACH(cpu, &cpulisthead, entry)
+#define CPU_ADD(cpu)           TAILQ_INSERT_TAIL(&cpulisthead, cpu, entry)
+#define CPU_REM(cpu)           TAILQ_REMOVE(&cpulisthead, cpu, entry)
+#define CPU_FIRST              TAILQ_FIRST(&cpulisthead)
+
+
+struct memlist {
+       struct acpisrat_mem mem;
+       TAILQ_ENTRY(memlist) entry;
+};
+
+static TAILQ_HEAD(, memlist) memlisthead;
+
+#define MEM_INIT               TAILQ_INIT(&memlisthead)
+#define MEM_FOREACH(mem)       TAILQ_FOREACH(mem, &memlisthead, entry)
+#define MEM_ADD(mem)           TAILQ_INSERT_TAIL(&memlisthead, mem, entry)
+#define MEM_ADD_BEFORE(mem, b) TAILQ_INSERT_BEFORE(b, mem, entry)
+#define MEM_REM(mem)           TAILQ_REMOVE(&memlisthead, mem, entry)
+#define MEM_FIRST              TAILQ_FIRST(&memlisthead)
+
+
+static struct cpulist *
+cpu_alloc(void)
+{
+       return kmem_zalloc(sizeof(struct cpulist), KM_NOSLEEP);
+}
+
+static void
+cpu_free(struct cpulist *c)
+{
+       kmem_free(c, sizeof(struct cpulist));
+}
+
+#if 0
+static struct cpulist *
+cpu_get(acpisrat_nodeid_t nodeid)
+{
+       struct cpulist *tmp;
+
+       CPU_FOREACH(tmp) {
+               if (tmp->cpu.nodeid == nodeid)
+                       return tmp;
+       }
+
+       return NULL;
+}
+#endif
+
+static struct memlist *
+mem_alloc(void)
+{
+       return kmem_zalloc(sizeof(struct memlist), KM_NOSLEEP);
+}
+
+static void
+mem_free(struct memlist *m)
+{
+       kmem_free(m, sizeof(struct memlist));
+}
+
+static struct memlist *
+mem_get(acpisrat_nodeid_t nodeid)
+{
+       struct memlist *tmp;
+
+       MEM_FOREACH(tmp) {
+               if (tmp->mem.nodeid == nodeid)
+                       return tmp;
+       }
+
+       return NULL;
+}
+
+
+bool
+acpisrat_exist(void)
+{
+       ACPI_TABLE_HEADER *table;
+       ACPI_STATUS rv;
+
+       rv = AcpiGetTable(ACPI_SIG_SRAT, 1, (ACPI_TABLE_HEADER **)&table);
+       if (ACPI_FAILURE(rv))
+               return false;
+
+       /* Check if header is valid */
+       if (table == NULL)
+               return false;
+
+       if (table->Length == 0xffffffff)
+               return false;
+
+       srat = (ACPI_TABLE_SRAT *)table;
+
+       return true;
+}
+
+static int
+acpisrat_parse(void)
+{
+       ACPI_SUBTABLE_HEADER *subtable;
+       ACPI_SRAT_CPU_AFFINITY *srat_cpu;
+       ACPI_SRAT_MEM_AFFINITY *srat_mem;
+       ACPI_SRAT_X2APIC_CPU_AFFINITY *srat_x2apic;
+
+       acpisrat_nodeid_t nodeid;
+       struct cpulist *cpuentry = NULL;
+       struct memlist *mementry;
+       uint32_t srat_pos;
+       bool ignore_cpu_affinity = false;
+
+       KASSERT(srat != NULL);
+
+       /* Content starts right after the header */
+       srat_pos = sizeof(ACPI_TABLE_SRAT);
+
+       while (srat_pos < srat->Header.Length) {
+               subtable = (ACPI_SUBTABLE_HEADER *)((char *)srat + srat_pos);
+               srat_pos += subtable->Length;
+
+               switch (subtable->Type) {
+               case ACPI_SRAT_TYPE_CPU_AFFINITY:
+                       if (ignore_cpu_affinity)
+                               continue;
+
+                       srat_cpu = (ACPI_SRAT_CPU_AFFINITY *)subtable;
+                       nodeid = (srat_cpu->ProximityDomainHi[2] << 24) |
+                           (srat_cpu->ProximityDomainHi[1] << 16) |
+                           (srat_cpu->ProximityDomainHi[0] << 8) |
+                           (srat_cpu->ProximityDomainLo);
+
+                       cpuentry = cpu_alloc();
+                       if (cpuentry == NULL)
+                               return ENOMEM;
+                       CPU_ADD(cpuentry);
+
+                       cpuentry->cpu.nodeid = nodeid;
+                       cpuentry->cpu.apicid = srat_cpu->ApicId;
+                       cpuentry->cpu.sapiceid = srat_cpu->LocalSapicEid;
+                       cpuentry->cpu.flags = srat_cpu->Flags;
+                       cpuentry->cpu.clockdomain = srat_cpu->ClockDomain;
+                       break;
+
+               case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
+                       srat_mem = (ACPI_SRAT_MEM_AFFINITY *)subtable;
+                       nodeid = srat_mem->ProximityDomain;
+
+                       mementry = mem_alloc();
+                       if (mementry == NULL)
+                               return ENOMEM;
+                       MEM_ADD(mementry);
+
+                       mementry->mem.nodeid = nodeid;
+                       mementry->mem.baseaddress = srat_mem->BaseAddress;
+                       mementry->mem.length = srat_mem->Length;
+                       mementry->mem.flags = srat_mem->Flags;
+                       break;
+
+               case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
+                       srat_x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)subtable;
+                       nodeid = srat_x2apic->ProximityDomain;
+
+                       /* This table entry overrides
+                        * ACPI_SRAT_TYPE_CPU_AFFINITY.
+                        */
+                       if (!ignore_cpu_affinity) {
+                               struct cpulist *citer;
+                               while ((citer = CPU_FIRST) != NULL) {
+                                       CPU_REM(citer);
+                                       cpu_free(citer);
+                               }
+                               ignore_cpu_affinity = true;
+                       }
+
+                       cpuentry = cpu_alloc();
+                       if (cpuentry == NULL)
+                               return ENOMEM;
+                       CPU_ADD(cpuentry);
+
+                       cpuentry->cpu.nodeid = nodeid;
+                       cpuentry->cpu.apicid = srat_x2apic->ApicId;
+                       cpuentry->cpu.clockdomain = srat_x2apic->ClockDomain;
+                       cpuentry->cpu.flags = srat_x2apic->Flags;
+                       break;
+
+               case ACPI_SRAT_TYPE_RESERVED:
+                       printf("ACPI SRAT subtable reserved, length: 0x%x\n",
+                               subtable->Length);
+                       break;
+               }
+       }
+
+       return 0;
+}
+
+static int
+acpisrat_quirks(void)
+{
+       struct cpulist *citer;
+       struct memlist *mem, *miter;
+
+       /* Some sanity checks. */
+
+       /* Deal with holes in the memory nodes.
+        * BIOS doesn't enlist memory nodes which
+        * don't have any memory modules plugged in.
+        * This behaviour has been observed on AMD machines.
+        *
+        * Do that by searching for CPUs in NUMA nodes
+        * which don't exist in the memory and then insert
+        * a zero memory range for the missing node.
+        */
+       CPU_FOREACH(citer) {
+               mem = mem_get(citer->cpu.nodeid);
+               if (mem != NULL)
+                       continue;
+               mem = mem_alloc();
+               if (mem == NULL)
+                       return ENOMEM;
+               mem->mem.nodeid = citer->cpu.nodeid;
+               /* all other fields are already zero filled */
+
+               MEM_FOREACH(miter) {
+                       if (miter->mem.nodeid < citer->cpu.nodeid)
+                               continue;
+                       MEM_ADD_BEFORE(mem, miter);
+                       break;
+               }



Home | Main Index | Thread Index | Old Index