2 * Copyright (c) 2010 Advanced Computing Technologies LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
37 #include <vm/vm_param.h>
38 #include <vm/vm_phys.h>
40 #include <contrib/dev/acpica/include/acpi.h>
41 #include <contrib/dev/acpica/include/actables.h>
43 #include <machine/intr_machdep.h>
44 #include <machine/apicvar.h>
46 #include <dev/acpica/acpivar.h>
52 } cpus[MAX_APIC_ID + 1];
54 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
57 static ACPI_TABLE_SRAT *srat;
58 static vm_paddr_t srat_physaddr;
60 static void srat_walk_table(acpi_subtable_handler *handler, void *arg);
63 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
65 ACPI_SRAT_CPU_AFFINITY *cpu;
66 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
67 ACPI_SRAT_MEM_AFFINITY *mem;
70 switch (entry->Type) {
71 case ACPI_SRAT_TYPE_CPU_AFFINITY:
72 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
73 domain = cpu->ProximityDomainLo |
74 cpu->ProximityDomainHi[0] << 8 |
75 cpu->ProximityDomainHi[1] << 16 |
76 cpu->ProximityDomainHi[2] << 24;
78 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
80 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
81 "enabled" : "disabled");
82 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
84 KASSERT(!cpus[cpu->ApicId].enabled,
85 ("Duplicate local APIC ID %u", cpu->ApicId));
86 cpus[cpu->ApicId].domain = domain;
87 cpus[cpu->ApicId].enabled = 1;
89 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
90 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
92 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
93 x2apic->ApicId, x2apic->ProximityDomain,
94 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
95 "enabled" : "disabled");
96 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
98 KASSERT(!cpus[x2apic->ApicId].enabled,
99 ("Duplicate local APIC ID %u", x2apic->ApicId));
100 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
101 cpus[x2apic->ApicId].enabled = 1;
103 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
104 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
107 "SRAT: Found memory domain %d addr %jx len %jx: %s\n",
108 mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
109 (uintmax_t)mem->Length,
110 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
111 "enabled" : "disabled");
112 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
114 if (num_mem == VM_PHYSSEG_MAX) {
115 printf("SRAT: Too many memory regions\n");
120 for (i = 0; i < num_mem; i++) {
121 if (mem_info[i].end <= mem->BaseAddress)
123 if (mem_info[i].start <
124 (mem->BaseAddress + mem->Length)) {
125 printf("SRAT: Overlapping memory entries\n");
131 for (i = num_mem; i > slot; i--)
132 mem_info[i] = mem_info[i - 1];
133 mem_info[slot].start = mem->BaseAddress;
134 mem_info[slot].end = mem->BaseAddress + mem->Length;
135 mem_info[slot].domain = mem->ProximityDomain;
142 * Ensure each memory domain has at least one CPU and that each CPU
143 * has at least one memory domain.
150 for (i = 0; i < num_mem; i++) {
152 for (j = 0; j <= MAX_APIC_ID; j++)
153 if (cpus[j].enabled &&
154 cpus[j].domain == mem_info[i].domain) {
155 cpus[j].has_memory = 1;
159 printf("SRAT: No CPU found for memory domain %d\n",
164 for (i = 0; i <= MAX_APIC_ID; i++)
165 if (cpus[i].enabled && !cpus[i].has_memory) {
166 printf("SRAT: No memory found for CPU %d\n", i);
173 * Check that the SRAT memory regions cover all of the regions in
177 check_phys_avail(void)
182 /* j is the current offset into phys_avail[]. */
183 address = phys_avail[0];
185 for (i = 0; i < num_mem; i++) {
187 * Consume as many phys_avail[] entries as fit in this
190 while (address >= mem_info[i].start &&
191 address <= mem_info[i].end) {
193 * If we cover the rest of this phys_avail[] entry,
194 * advance to the next entry.
196 if (phys_avail[j + 1] <= mem_info[i].end) {
198 if (phys_avail[j] == 0 &&
199 phys_avail[j + 1] == 0) {
202 address = phys_avail[j];
204 address = mem_info[i].end + 1;
207 printf("SRAT: No memory region found for %jx - %jx\n",
208 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
213 * Renumber the memory domains to be compact and zero-based if not
217 renumber_domains(void)
219 int domains[VM_PHYSSEG_MAX];
220 int ndomain, i, j, slot;
222 /* Enumerate all the domains. */
224 for (i = 0; i < num_mem; i++) {
225 /* See if this domain is already known. */
226 for (j = 0; j < ndomain; j++) {
227 if (domains[j] >= mem_info[i].domain)
230 if (j < ndomain && domains[j] == mem_info[i].domain)
233 /* Insert the new domain at slot 'j'. */
235 for (j = ndomain; j > slot; j--)
236 domains[j] = domains[j - 1];
237 domains[slot] = mem_info[i].domain;
240 /* Renumber each domain to its index in the sorted 'domains' list. */
241 for (i = 0; i < ndomain; i++) {
243 * If the domain is already the right value, no need
249 /* Walk the cpu[] and mem_info[] arrays to renumber. */
250 for (j = 0; j < num_mem; j++)
251 if (mem_info[j].domain == domains[i])
252 mem_info[j].domain = i;
253 for (j = 0; j <= MAX_APIC_ID; j++)
254 if (cpus[j].enabled && cpus[j].domain == domains[i])
260 * Look for an ACPI System Resource Affinity Table ("SRAT")
263 parse_srat(void *dummy)
267 if (resource_disabled("srat", 0))
270 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
271 if (srat_physaddr == 0)
275 * Make a pass over the table to populate the cpus[] and
278 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
280 srat_walk_table(srat_parse_entry, &error);
281 acpi_unmap_table(srat);
283 if (error || check_domains() != 0 || check_phys_avail() != 0) {
290 /* Point vm_phys at our memory affinity table. */
291 mem_affinity = mem_info;
293 SYSINIT(parse_srat, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_srat, NULL);
296 srat_walk_table(acpi_subtable_handler *handler, void *arg)
299 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
304 * Setup per-CPU ACPI IDs.
307 srat_set_cpus(void *dummy)
309 struct cpu_info *cpu;
313 if (srat_physaddr == 0)
315 for (i = 0; i < MAXCPU; i++) {
319 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
320 cpu = &cpus[pc->pc_apic_id];
322 panic("SRAT: CPU with APIC ID %u is not known",
324 pc->pc_domain = cpu->domain;
326 printf("SRAT: CPU %u has memory domain %d\n", i,
330 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);