2 * Copyright (c) 2010 Hudson River Trading 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>
35 #include <sys/mutex.h>
37 #include <sys/vmmeter.h>
40 #include <vm/vm_param.h>
41 #include <vm/vm_page.h>
42 #include <vm/vm_phys.h>
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/actables.h>
47 #include <machine/intr_machdep.h>
48 #include <machine/apicvar.h>
50 #include <dev/acpica/acpivar.h>
57 } cpus[MAX_APIC_ID + 1];
59 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
62 static ACPI_TABLE_SRAT *srat;
63 static vm_paddr_t srat_physaddr;
65 static int vm_domains[VM_PHYSSEG_MAX];
67 static void srat_walk_table(acpi_subtable_handler *handler, void *arg);
70 * Returns true if a memory range overlaps with at least one range in
74 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end)
78 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) {
79 if (phys_avail[i + 1] < start)
81 if (phys_avail[i] < end)
90 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
92 ACPI_SRAT_CPU_AFFINITY *cpu;
93 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
94 ACPI_SRAT_MEM_AFFINITY *mem;
97 switch (entry->Type) {
98 case ACPI_SRAT_TYPE_CPU_AFFINITY:
99 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
100 domain = cpu->ProximityDomainLo |
101 cpu->ProximityDomainHi[0] << 8 |
102 cpu->ProximityDomainHi[1] << 16 |
103 cpu->ProximityDomainHi[2] << 24;
105 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
107 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
108 "enabled" : "disabled");
109 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
111 KASSERT(!cpus[cpu->ApicId].enabled,
112 ("Duplicate local APIC ID %u", cpu->ApicId));
113 cpus[cpu->ApicId].domain = domain;
114 cpus[cpu->ApicId].enabled = 1;
116 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
117 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
119 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
120 x2apic->ApicId, x2apic->ProximityDomain,
121 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
122 "enabled" : "disabled");
123 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
125 KASSERT(!cpus[x2apic->ApicId].enabled,
126 ("Duplicate local APIC ID %u", x2apic->ApicId));
127 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
128 cpus[x2apic->ApicId].enabled = 1;
130 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
131 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
134 "SRAT: Found memory domain %d addr %jx len %jx: %s\n",
135 mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
136 (uintmax_t)mem->Length,
137 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
138 "enabled" : "disabled");
139 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
141 if (!overlaps_phys_avail(mem->BaseAddress,
142 mem->BaseAddress + mem->Length)) {
143 printf("SRAT: Ignoring memory at addr %jx\n",
144 (uintmax_t)mem->BaseAddress);
147 if (num_mem == VM_PHYSSEG_MAX) {
148 printf("SRAT: Too many memory regions\n");
153 for (i = 0; i < num_mem; i++) {
154 if (mem_info[i].end <= mem->BaseAddress)
156 if (mem_info[i].start <
157 (mem->BaseAddress + mem->Length)) {
158 printf("SRAT: Overlapping memory entries\n");
164 for (i = num_mem; i > slot; i--)
165 mem_info[i] = mem_info[i - 1];
166 mem_info[slot].start = mem->BaseAddress;
167 mem_info[slot].end = mem->BaseAddress + mem->Length;
168 mem_info[slot].domain = mem->ProximityDomain;
175 * Ensure each memory domain has at least one CPU and that each CPU
176 * has at least one memory domain.
183 for (i = 0; i < num_mem; i++) {
185 for (j = 0; j <= MAX_APIC_ID; j++)
186 if (cpus[j].enabled &&
187 cpus[j].domain == mem_info[i].domain) {
188 cpus[j].has_memory = 1;
192 printf("SRAT: No CPU found for memory domain %d\n",
197 for (i = 0; i <= MAX_APIC_ID; i++)
198 if (cpus[i].enabled && !cpus[i].has_memory) {
199 printf("SRAT: No memory found for CPU %d\n", i);
206 * Check that the SRAT memory regions cover all of the regions in
210 check_phys_avail(void)
215 /* j is the current offset into phys_avail[]. */
216 address = phys_avail[0];
218 for (i = 0; i < num_mem; i++) {
220 * Consume as many phys_avail[] entries as fit in this
223 while (address >= mem_info[i].start &&
224 address <= mem_info[i].end) {
226 * If we cover the rest of this phys_avail[] entry,
227 * advance to the next entry.
229 if (phys_avail[j + 1] <= mem_info[i].end) {
231 if (phys_avail[j] == 0 &&
232 phys_avail[j + 1] == 0) {
235 address = phys_avail[j];
237 address = mem_info[i].end + 1;
240 printf("SRAT: No memory region found for %jx - %jx\n",
241 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
246 * Renumber the memory domains to be compact and zero-based if not
247 * already. Returns an error if there are too many domains.
250 renumber_domains(void)
254 /* Enumerate all the domains. */
256 for (i = 0; i < num_mem; i++) {
257 /* See if this domain is already known. */
258 for (j = 0; j < vm_ndomains; j++) {
259 if (vm_domains[j] >= mem_info[i].domain)
262 if (j < vm_ndomains && vm_domains[j] == mem_info[i].domain)
265 /* Insert the new domain at slot 'j'. */
267 for (j = vm_ndomains; j > slot; j--)
268 vm_domains[j] = vm_domains[j - 1];
269 vm_domains[slot] = mem_info[i].domain;
271 if (vm_ndomains > MAXMEMDOM) {
273 printf("SRAT: Too many memory domains\n");
278 /* Renumber each domain to its index in the sorted 'domains' list. */
279 for (i = 0; i < vm_ndomains; i++) {
281 * If the domain is already the right value, no need
284 if (vm_domains[i] == i)
287 /* Walk the cpu[] and mem_info[] arrays to renumber. */
288 for (j = 0; j < num_mem; j++)
289 if (mem_info[j].domain == vm_domains[i])
290 mem_info[j].domain = i;
291 for (j = 0; j <= MAX_APIC_ID; j++)
292 if (cpus[j].enabled && cpus[j].domain == vm_domains[i])
295 KASSERT(vm_ndomains > 0,
296 ("renumber_domains: invalid final vm_ndomains setup"));
302 * Look for an ACPI System Resource Affinity Table ("SRAT")
305 parse_srat(void *dummy)
309 if (resource_disabled("srat", 0))
312 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
313 if (srat_physaddr == 0)
317 * Make a pass over the table to populate the cpus[] and
320 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
322 srat_walk_table(srat_parse_entry, &error);
323 acpi_unmap_table(srat);
325 if (error || check_domains() != 0 || check_phys_avail() != 0 ||
326 renumber_domains() != 0) {
331 /* Point vm_phys at our memory affinity table. */
332 mem_affinity = mem_info;
334 SYSINIT(parse_srat, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_srat, NULL);
337 srat_walk_table(acpi_subtable_handler *handler, void *arg)
340 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
345 * Setup per-CPU ACPI IDs.
348 srat_set_cpus(void *dummy)
350 struct cpu_info *cpu;
354 if (srat_physaddr == 0)
356 for (i = 0; i < MAXCPU; i++) {
360 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
361 cpu = &cpus[pc->pc_apic_id];
363 panic("SRAT: CPU with APIC ID %u is not known",
365 pc->pc_domain = cpu->domain;
367 printf("SRAT: CPU %u has memory domain %d\n", i,
371 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);
374 * Map a _PXM value to a VM domain ID.
376 * Returns the domain ID, or -1 if no domain ID was found.
379 acpi_map_pxm_to_vm_domainid(int pxm)
383 for (i = 0; i < vm_ndomains; i++) {
384 if (vm_domains[i] == pxm)
391 #endif /* MAXMEMDOM > 1 */