1c7f9edd8SMatthew Dillon /*
2c7f9edd8SMatthew Dillon * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3c7f9edd8SMatthew Dillon *
4c7f9edd8SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
5c7f9edd8SMatthew Dillon * by Sepherosa Ziehau <sepherosa@gmail.com>
6c7f9edd8SMatthew Dillon *
7c7f9edd8SMatthew Dillon * Redistribution and use in source and binary forms, with or without
8c7f9edd8SMatthew Dillon * modification, are permitted provided that the following conditions
9c7f9edd8SMatthew Dillon * are met:
10c7f9edd8SMatthew Dillon *
11c7f9edd8SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
12c7f9edd8SMatthew Dillon * notice, this list of conditions and the following disclaimer.
13c7f9edd8SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
14c7f9edd8SMatthew Dillon * notice, this list of conditions and the following disclaimer in
15c7f9edd8SMatthew Dillon * the documentation and/or other materials provided with the
16c7f9edd8SMatthew Dillon * distribution.
17c7f9edd8SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
18c7f9edd8SMatthew Dillon * contributors may be used to endorse or promote products derived
19c7f9edd8SMatthew Dillon * from this software without specific, prior written permission.
20c7f9edd8SMatthew Dillon *
21c7f9edd8SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22c7f9edd8SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23c7f9edd8SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24c7f9edd8SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25c7f9edd8SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26c7f9edd8SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27c7f9edd8SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28c7f9edd8SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29c7f9edd8SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30c7f9edd8SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31c7f9edd8SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32c7f9edd8SMatthew Dillon * SUCH DAMAGE.
33c7f9edd8SMatthew Dillon */
34c7f9edd8SMatthew Dillon
35c7f9edd8SMatthew Dillon #include <sys/param.h>
36c7f9edd8SMatthew Dillon #include <sys/bus.h>
37c7f9edd8SMatthew Dillon #include <sys/kernel.h>
38c7f9edd8SMatthew Dillon #include <sys/systm.h>
39c7f9edd8SMatthew Dillon #include <vm/vm_page.h>
40c7f9edd8SMatthew Dillon
41c7f9edd8SMatthew Dillon #include "acpi.h"
42c7f9edd8SMatthew Dillon #include "acpi_sdt_var.h"
43c7f9edd8SMatthew Dillon
44c7f9edd8SMatthew Dillon typedef union srat_entry {
45c7f9edd8SMatthew Dillon ACPI_SUBTABLE_HEADER head;
46c7f9edd8SMatthew Dillon ACPI_SRAT_CPU_AFFINITY cpu;
47c7f9edd8SMatthew Dillon ACPI_SRAT_MEM_AFFINITY mem;
48c7f9edd8SMatthew Dillon ACPI_SRAT_X2APIC_CPU_AFFINITY x2apic;
49c7f9edd8SMatthew Dillon ACPI_SRAT_GICC_AFFINITY gicc;
50c7f9edd8SMatthew Dillon } srat_entry_t;
51c7f9edd8SMatthew Dillon
52c7f9edd8SMatthew Dillon static void
srat_probe(void)53c7f9edd8SMatthew Dillon srat_probe(void)
54c7f9edd8SMatthew Dillon {
55c7f9edd8SMatthew Dillon vm_paddr_t srat_paddr;
56c7f9edd8SMatthew Dillon ACPI_TABLE_SRAT *srat;
57c7f9edd8SMatthew Dillon srat_entry_t *mem;
58c7f9edd8SMatthew Dillon srat_entry_t *cpu;
59c7f9edd8SMatthew Dillon int error = 0;
606f2099feSMatthew Dillon int enabled = 1;
616f2099feSMatthew Dillon int disabled = 0;
626f2099feSMatthew Dillon
636f2099feSMatthew Dillon /*
646f2099feSMatthew Dillon * Allow NUMA to be disabled by either setting kern.numa_disable
656f2099feSMatthew Dillon * to 1 or by setting hw.acpi.srat.enabled to 0.
666f2099feSMatthew Dillon */
676f2099feSMatthew Dillon kgetenv_int("kern.numa_disable", &disabled);
686f2099feSMatthew Dillon kgetenv_int("hw.acpi.srat.enabled", &enabled);
696f2099feSMatthew Dillon if (disabled || enabled == 0)
706f2099feSMatthew Dillon return;
71c7f9edd8SMatthew Dillon
72c7f9edd8SMatthew Dillon /*
73c7f9edd8SMatthew Dillon * Map the SRAT if it exists
74c7f9edd8SMatthew Dillon */
75c7f9edd8SMatthew Dillon srat_paddr = sdt_search(ACPI_SIG_SRAT);
76c7f9edd8SMatthew Dillon if (srat_paddr == 0) {
77c7f9edd8SMatthew Dillon kprintf("srat_probe: can't locate SRAT\n");
78c7f9edd8SMatthew Dillon return;
79c7f9edd8SMatthew Dillon }
80c7f9edd8SMatthew Dillon
81c7f9edd8SMatthew Dillon srat = sdt_sdth_map(srat_paddr);
82c7f9edd8SMatthew Dillon KKASSERT(srat != NULL);
83c7f9edd8SMatthew Dillon
84c7f9edd8SMatthew Dillon if (srat->Header.Length < sizeof(*srat)) {
85c7f9edd8SMatthew Dillon kprintf("acpi: invalid SRAT length %u\n",
86c7f9edd8SMatthew Dillon srat->Header.Length);
87c7f9edd8SMatthew Dillon error = EINVAL;
88c7f9edd8SMatthew Dillon goto done;
89c7f9edd8SMatthew Dillon }
90c7f9edd8SMatthew Dillon
91c7f9edd8SMatthew Dillon cpu = NULL;
92c7f9edd8SMatthew Dillon
93c7f9edd8SMatthew Dillon for (mem = (srat_entry_t *)(srat + 1);
94c7f9edd8SMatthew Dillon (char *)mem < (char *)srat + srat->Header.Length;
95c7f9edd8SMatthew Dillon mem = (srat_entry_t *)((char *)mem + mem->head.Length)) {
96c7f9edd8SMatthew Dillon /*
97c7f9edd8SMatthew Dillon * Mem scan memory affinity only
98c7f9edd8SMatthew Dillon */
99c7f9edd8SMatthew Dillon if (mem->head.Type != ACPI_SRAT_TYPE_MEMORY_AFFINITY)
100c7f9edd8SMatthew Dillon continue;
101c7f9edd8SMatthew Dillon if ((mem->mem.Flags & ACPI_SRAT_MEM_ENABLED) == 0)
102c7f9edd8SMatthew Dillon continue;
103c7f9edd8SMatthew Dillon
104c7f9edd8SMatthew Dillon kprintf("MemAffinity %016jx,%ldMB Prox=%u ",
105c7f9edd8SMatthew Dillon mem->mem.BaseAddress,
106c7f9edd8SMatthew Dillon mem->mem.Length / (1024 * 1024),
107c7f9edd8SMatthew Dillon mem->mem.ProximityDomain);
108c7f9edd8SMatthew Dillon
109c7f9edd8SMatthew Dillon /*
110c7f9edd8SMatthew Dillon * Look for associated cpu affinity
111c7f9edd8SMatthew Dillon */
112c7f9edd8SMatthew Dillon if (cpu == NULL ||
113c7f9edd8SMatthew Dillon mem->mem.ProximityDomain != cpu->cpu.ProximityDomainLo) {
114c7f9edd8SMatthew Dillon for (cpu = (srat_entry_t *)(srat + 1);
115c7f9edd8SMatthew Dillon (char *)cpu < (char *)srat + srat->Header.Length;
116c7f9edd8SMatthew Dillon cpu = (srat_entry_t *)((char *)cpu +
117c7f9edd8SMatthew Dillon cpu->head.Length)) {
118c7f9edd8SMatthew Dillon if (cpu->head.Type !=
119c7f9edd8SMatthew Dillon ACPI_SRAT_TYPE_CPU_AFFINITY)
120c7f9edd8SMatthew Dillon continue;
121c7f9edd8SMatthew Dillon if ((cpu->cpu.Flags &
122c7f9edd8SMatthew Dillon ACPI_SRAT_CPU_USE_AFFINITY) == 0)
123c7f9edd8SMatthew Dillon continue;
124c7f9edd8SMatthew Dillon if (mem->mem.ProximityDomain ==
125c7f9edd8SMatthew Dillon cpu->cpu.ProximityDomainLo) {
126c7f9edd8SMatthew Dillon break;
127c7f9edd8SMatthew Dillon }
128c7f9edd8SMatthew Dillon }
129c7f9edd8SMatthew Dillon if ((char *)cpu >= (char *)srat + srat->Header.Length)
130c7f9edd8SMatthew Dillon cpu = NULL;
131c7f9edd8SMatthew Dillon }
132c7f9edd8SMatthew Dillon if (cpu) {
133c7f9edd8SMatthew Dillon kprintf("CpuApicId %02x Socket %d\n",
134c7f9edd8SMatthew Dillon cpu->cpu.ApicId,
135c7f9edd8SMatthew Dillon get_chip_ID_from_APICID(cpu->cpu.ApicId));
136c7f9edd8SMatthew Dillon vm_numa_organize(mem->mem.BaseAddress,
137c7f9edd8SMatthew Dillon mem->mem.Length,
138c7f9edd8SMatthew Dillon get_chip_ID_from_APICID(cpu->cpu.ApicId));
139c7f9edd8SMatthew Dillon } else {
140c7f9edd8SMatthew Dillon kprintf("(not found)\n");
141c7f9edd8SMatthew Dillon }
142c7f9edd8SMatthew Dillon }
143*8e5d7c42SMatthew Dillon vm_numa_organize_finalize();
144c7f9edd8SMatthew Dillon
145c7f9edd8SMatthew Dillon done:
146c7f9edd8SMatthew Dillon sdt_sdth_unmap(&srat->Header);
147c7f9edd8SMatthew Dillon }
148c7f9edd8SMatthew Dillon
149c7f9edd8SMatthew Dillon SYSINIT(srat_probe, SI_BOOT2_NUMA, SI_ORDER_FIRST, srat_probe, 0);
150