1dd540b46SJohn Baldwin /*- 2ebf5747bSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3ebf5747bSPedro F. Giffuni * 4179fa75eSJohn Baldwin * Copyright (c) 2010 Hudson River Trading LLC 5dd540b46SJohn Baldwin * Written by: John H. Baldwin <jhb@FreeBSD.org> 6dd540b46SJohn Baldwin * All rights reserved. 7dd540b46SJohn Baldwin * 8dd540b46SJohn Baldwin * Redistribution and use in source and binary forms, with or without 9dd540b46SJohn Baldwin * modification, are permitted provided that the following conditions 10dd540b46SJohn Baldwin * are met: 11dd540b46SJohn Baldwin * 1. Redistributions of source code must retain the above copyright 12dd540b46SJohn Baldwin * notice, this list of conditions and the following disclaimer. 13dd540b46SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright 14dd540b46SJohn Baldwin * notice, this list of conditions and the following disclaimer in the 15dd540b46SJohn Baldwin * documentation and/or other materials provided with the distribution. 16dd540b46SJohn Baldwin * 17dd540b46SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18dd540b46SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19dd540b46SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20dd540b46SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21dd540b46SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22dd540b46SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23dd540b46SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24dd540b46SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25dd540b46SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26dd540b46SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27dd540b46SJohn Baldwin * SUCH DAMAGE. 28dd540b46SJohn Baldwin */ 29dd540b46SJohn Baldwin 30dd540b46SJohn Baldwin #include <sys/cdefs.h> 31dd540b46SJohn Baldwin __FBSDID("$FreeBSD$"); 32dd540b46SJohn Baldwin 3362d70a81SJohn Baldwin #include "opt_vm.h" 3462d70a81SJohn Baldwin 35dd540b46SJohn Baldwin #include <sys/param.h> 3683c9dea1SGleb Smirnoff #include <sys/systm.h> 37dd540b46SJohn Baldwin #include <sys/bus.h> 38dd540b46SJohn Baldwin #include <sys/kernel.h> 39449c2e92SKonstantin Belousov #include <sys/lock.h> 40449c2e92SKonstantin Belousov #include <sys/mutex.h> 41dd540b46SJohn Baldwin #include <sys/smp.h> 42449c2e92SKonstantin Belousov #include <sys/vmmeter.h> 43dd540b46SJohn Baldwin #include <vm/vm.h> 44dd540b46SJohn Baldwin #include <vm/pmap.h> 45dd540b46SJohn Baldwin #include <vm/vm_param.h> 46449c2e92SKonstantin Belousov #include <vm/vm_page.h> 47dd540b46SJohn Baldwin #include <vm/vm_phys.h> 48dd540b46SJohn Baldwin 49dd540b46SJohn Baldwin #include <contrib/dev/acpica/include/acpi.h> 50493deb39SJung-uk Kim #include <contrib/dev/acpica/include/aclocal.h> 51dd540b46SJohn Baldwin #include <contrib/dev/acpica/include/actables.h> 52dd540b46SJohn Baldwin 53dd540b46SJohn Baldwin #include <machine/intr_machdep.h> 54fd1f83fbSRoger Pau Monné #include <machine/md_var.h> 55e07ef9b0SJohn Baldwin #include <x86/apicvar.h> 56dd540b46SJohn Baldwin 57dd540b46SJohn Baldwin #include <dev/acpica/acpivar.h> 58dd540b46SJohn Baldwin 59941646f5SAttilio Rao #if MAXMEMDOM > 1 6084525e55SRoger Pau Monné static struct cpu_info { 61dd540b46SJohn Baldwin int enabled:1; 62dd540b46SJohn Baldwin int has_memory:1; 63dd540b46SJohn Baldwin int domain; 6484525e55SRoger Pau Monné } *cpus; 65dd540b46SJohn Baldwin 66dd540b46SJohn Baldwin struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1]; 67dd540b46SJohn Baldwin int num_mem; 68dd540b46SJohn Baldwin 69dd540b46SJohn Baldwin static ACPI_TABLE_SRAT *srat; 70dd540b46SJohn Baldwin static vm_paddr_t srat_physaddr; 71dd540b46SJohn Baldwin 7262d70a81SJohn Baldwin static int domain_pxm[MAXMEMDOM]; 7362d70a81SJohn Baldwin static int ndomain; 74fc4f524aSAdrian Chadd 75415d7ccaSAdrian Chadd static ACPI_TABLE_SLIT *slit; 76415d7ccaSAdrian Chadd static vm_paddr_t slit_physaddr; 77415d7ccaSAdrian Chadd static int vm_locality_table[MAXMEMDOM * MAXMEMDOM]; 78415d7ccaSAdrian Chadd 79dd540b46SJohn Baldwin static void srat_walk_table(acpi_subtable_handler *handler, void *arg); 80dd540b46SJohn Baldwin 814d99cfb3SJohn Baldwin /* 82415d7ccaSAdrian Chadd * SLIT parsing. 83415d7ccaSAdrian Chadd */ 84415d7ccaSAdrian Chadd 85415d7ccaSAdrian Chadd static void 86415d7ccaSAdrian Chadd slit_parse_table(ACPI_TABLE_SLIT *s) 87415d7ccaSAdrian Chadd { 88415d7ccaSAdrian Chadd int i, j; 89415d7ccaSAdrian Chadd int i_domain, j_domain; 90415d7ccaSAdrian Chadd int offset = 0; 91415d7ccaSAdrian Chadd uint8_t e; 92415d7ccaSAdrian Chadd 93415d7ccaSAdrian Chadd /* 94415d7ccaSAdrian Chadd * This maps the SLIT data into the VM-domain centric view. 95415d7ccaSAdrian Chadd * There may be sparse entries in the PXM namespace, so 96415d7ccaSAdrian Chadd * remap them to a VM-domain ID and if it doesn't exist, 97415d7ccaSAdrian Chadd * skip it. 98415d7ccaSAdrian Chadd * 99415d7ccaSAdrian Chadd * It should result in a packed 2d array of VM-domain 100415d7ccaSAdrian Chadd * locality information entries. 101415d7ccaSAdrian Chadd */ 102415d7ccaSAdrian Chadd 103415d7ccaSAdrian Chadd if (bootverbose) 104415d7ccaSAdrian Chadd printf("SLIT.Localities: %d\n", (int) s->LocalityCount); 105415d7ccaSAdrian Chadd for (i = 0; i < s->LocalityCount; i++) { 106415d7ccaSAdrian Chadd i_domain = acpi_map_pxm_to_vm_domainid(i); 107415d7ccaSAdrian Chadd if (i_domain < 0) 108415d7ccaSAdrian Chadd continue; 109415d7ccaSAdrian Chadd 110415d7ccaSAdrian Chadd if (bootverbose) 111415d7ccaSAdrian Chadd printf("%d: ", i); 112415d7ccaSAdrian Chadd for (j = 0; j < s->LocalityCount; j++) { 113415d7ccaSAdrian Chadd j_domain = acpi_map_pxm_to_vm_domainid(j); 114415d7ccaSAdrian Chadd if (j_domain < 0) 115415d7ccaSAdrian Chadd continue; 116415d7ccaSAdrian Chadd e = s->Entry[i * s->LocalityCount + j]; 117415d7ccaSAdrian Chadd if (bootverbose) 118415d7ccaSAdrian Chadd printf("%d ", (int) e); 119415d7ccaSAdrian Chadd /* 255 == "no locality information" */ 120415d7ccaSAdrian Chadd if (e == 255) 121415d7ccaSAdrian Chadd vm_locality_table[offset] = -1; 122415d7ccaSAdrian Chadd else 123415d7ccaSAdrian Chadd vm_locality_table[offset] = e; 124415d7ccaSAdrian Chadd offset++; 125415d7ccaSAdrian Chadd } 126415d7ccaSAdrian Chadd if (bootverbose) 127415d7ccaSAdrian Chadd printf("\n"); 128415d7ccaSAdrian Chadd } 129415d7ccaSAdrian Chadd } 130415d7ccaSAdrian Chadd 131415d7ccaSAdrian Chadd /* 132415d7ccaSAdrian Chadd * Look for an ACPI System Locality Distance Information Table ("SLIT") 133415d7ccaSAdrian Chadd */ 134415d7ccaSAdrian Chadd static int 135415d7ccaSAdrian Chadd parse_slit(void) 136415d7ccaSAdrian Chadd { 137415d7ccaSAdrian Chadd 138415d7ccaSAdrian Chadd if (resource_disabled("slit", 0)) { 139415d7ccaSAdrian Chadd return (-1); 140415d7ccaSAdrian Chadd } 141415d7ccaSAdrian Chadd 142415d7ccaSAdrian Chadd slit_physaddr = acpi_find_table(ACPI_SIG_SLIT); 143415d7ccaSAdrian Chadd if (slit_physaddr == 0) { 144415d7ccaSAdrian Chadd return (-1); 145415d7ccaSAdrian Chadd } 146415d7ccaSAdrian Chadd 147415d7ccaSAdrian Chadd /* 148415d7ccaSAdrian Chadd * Make a pass over the table to populate the cpus[] and 149415d7ccaSAdrian Chadd * mem_info[] tables. 150415d7ccaSAdrian Chadd */ 151415d7ccaSAdrian Chadd slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT); 152415d7ccaSAdrian Chadd slit_parse_table(slit); 153415d7ccaSAdrian Chadd acpi_unmap_table(slit); 154415d7ccaSAdrian Chadd slit = NULL; 155415d7ccaSAdrian Chadd 156b6715dabSJeff Roberson #ifdef NUMA 157415d7ccaSAdrian Chadd /* Tell the VM about it! */ 158415d7ccaSAdrian Chadd mem_locality = vm_locality_table; 15962d70a81SJohn Baldwin #endif 160415d7ccaSAdrian Chadd return (0); 161415d7ccaSAdrian Chadd } 162415d7ccaSAdrian Chadd 163415d7ccaSAdrian Chadd /* 164415d7ccaSAdrian Chadd * SRAT parsing. 165415d7ccaSAdrian Chadd */ 166415d7ccaSAdrian Chadd 167415d7ccaSAdrian Chadd /* 1684d99cfb3SJohn Baldwin * Returns true if a memory range overlaps with at least one range in 1694d99cfb3SJohn Baldwin * phys_avail[]. 1704d99cfb3SJohn Baldwin */ 1714d99cfb3SJohn Baldwin static int 1724d99cfb3SJohn Baldwin overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end) 1734d99cfb3SJohn Baldwin { 1744d99cfb3SJohn Baldwin int i; 1754d99cfb3SJohn Baldwin 1764d99cfb3SJohn Baldwin for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) { 177ffc7e53aSAlexander Motin if (phys_avail[i + 1] <= start) 1784d99cfb3SJohn Baldwin continue; 1794d99cfb3SJohn Baldwin if (phys_avail[i] < end) 1804d99cfb3SJohn Baldwin return (1); 1814d99cfb3SJohn Baldwin break; 1824d99cfb3SJohn Baldwin } 1834d99cfb3SJohn Baldwin return (0); 1844d99cfb3SJohn Baldwin 1854d99cfb3SJohn Baldwin } 1864d99cfb3SJohn Baldwin 187dd540b46SJohn Baldwin static void 188dd540b46SJohn Baldwin srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg) 189dd540b46SJohn Baldwin { 190dd540b46SJohn Baldwin ACPI_SRAT_CPU_AFFINITY *cpu; 191dd540b46SJohn Baldwin ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic; 192dd540b46SJohn Baldwin ACPI_SRAT_MEM_AFFINITY *mem; 193dd540b46SJohn Baldwin int domain, i, slot; 194dd540b46SJohn Baldwin 195dd540b46SJohn Baldwin switch (entry->Type) { 196dd540b46SJohn Baldwin case ACPI_SRAT_TYPE_CPU_AFFINITY: 197dd540b46SJohn Baldwin cpu = (ACPI_SRAT_CPU_AFFINITY *)entry; 198dd540b46SJohn Baldwin domain = cpu->ProximityDomainLo | 199dd540b46SJohn Baldwin cpu->ProximityDomainHi[0] << 8 | 200dd540b46SJohn Baldwin cpu->ProximityDomainHi[1] << 16 | 201dd540b46SJohn Baldwin cpu->ProximityDomainHi[2] << 24; 202dd540b46SJohn Baldwin if (bootverbose) 203dd540b46SJohn Baldwin printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 204dd540b46SJohn Baldwin cpu->ApicId, domain, 205dd540b46SJohn Baldwin (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ? 206dd540b46SJohn Baldwin "enabled" : "disabled"); 207dd540b46SJohn Baldwin if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED)) 208dd540b46SJohn Baldwin break; 20984525e55SRoger Pau Monné if (cpu->ApicId > max_apic_id) { 2106e2ab0aeSRoger Pau Monné printf("SRAT: Ignoring local APIC ID %u (too high)\n", 2116e2ab0aeSRoger Pau Monné cpu->ApicId); 2126e2ab0aeSRoger Pau Monné break; 2136e2ab0aeSRoger Pau Monné } 2146e2ab0aeSRoger Pau Monné 2152db0699dSEric van Gyzen if (cpus[cpu->ApicId].enabled) { 2162db0699dSEric van Gyzen printf("SRAT: Duplicate local APIC ID %u\n", 2172db0699dSEric van Gyzen cpu->ApicId); 2182db0699dSEric van Gyzen *(int *)arg = ENXIO; 2192db0699dSEric van Gyzen break; 2202db0699dSEric van Gyzen } 221dd540b46SJohn Baldwin cpus[cpu->ApicId].domain = domain; 222dd540b46SJohn Baldwin cpus[cpu->ApicId].enabled = 1; 223dd540b46SJohn Baldwin break; 224dd540b46SJohn Baldwin case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: 225dd540b46SJohn Baldwin x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry; 226dd540b46SJohn Baldwin if (bootverbose) 227dd540b46SJohn Baldwin printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 228dd540b46SJohn Baldwin x2apic->ApicId, x2apic->ProximityDomain, 229dd540b46SJohn Baldwin (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ? 230dd540b46SJohn Baldwin "enabled" : "disabled"); 231dd540b46SJohn Baldwin if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED)) 232dd540b46SJohn Baldwin break; 23384525e55SRoger Pau Monné if (x2apic->ApicId > max_apic_id) { 2346e2ab0aeSRoger Pau Monné printf("SRAT: Ignoring local APIC ID %u (too high)\n", 2356e2ab0aeSRoger Pau Monné x2apic->ApicId); 2366e2ab0aeSRoger Pau Monné break; 2376e2ab0aeSRoger Pau Monné } 2386e2ab0aeSRoger Pau Monné 239dd540b46SJohn Baldwin KASSERT(!cpus[x2apic->ApicId].enabled, 240dd540b46SJohn Baldwin ("Duplicate local APIC ID %u", x2apic->ApicId)); 241dd540b46SJohn Baldwin cpus[x2apic->ApicId].domain = x2apic->ProximityDomain; 242dd540b46SJohn Baldwin cpus[x2apic->ApicId].enabled = 1; 243dd540b46SJohn Baldwin break; 244dd540b46SJohn Baldwin case ACPI_SRAT_TYPE_MEMORY_AFFINITY: 245dd540b46SJohn Baldwin mem = (ACPI_SRAT_MEM_AFFINITY *)entry; 246dd540b46SJohn Baldwin if (bootverbose) 247dd540b46SJohn Baldwin printf( 2485ab0f0c3SKonstantin Belousov "SRAT: Found memory domain %d addr 0x%jx len 0x%jx: %s\n", 249dd540b46SJohn Baldwin mem->ProximityDomain, (uintmax_t)mem->BaseAddress, 250dd540b46SJohn Baldwin (uintmax_t)mem->Length, 251dd540b46SJohn Baldwin (mem->Flags & ACPI_SRAT_MEM_ENABLED) ? 252dd540b46SJohn Baldwin "enabled" : "disabled"); 253dd540b46SJohn Baldwin if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED)) 254dd540b46SJohn Baldwin break; 2553f289c3fSJeff Roberson if (mem->BaseAddress >= cpu_getmaxphyaddr() || 2563f289c3fSJeff Roberson !overlaps_phys_avail(mem->BaseAddress, 2574d99cfb3SJohn Baldwin mem->BaseAddress + mem->Length)) { 2585ab0f0c3SKonstantin Belousov printf("SRAT: Ignoring memory at addr 0x%jx\n", 2594d99cfb3SJohn Baldwin (uintmax_t)mem->BaseAddress); 2604d99cfb3SJohn Baldwin break; 2614d99cfb3SJohn Baldwin } 262dd540b46SJohn Baldwin if (num_mem == VM_PHYSSEG_MAX) { 263dd540b46SJohn Baldwin printf("SRAT: Too many memory regions\n"); 264dd540b46SJohn Baldwin *(int *)arg = ENXIO; 265dd540b46SJohn Baldwin break; 266dd540b46SJohn Baldwin } 267dd540b46SJohn Baldwin slot = num_mem; 268dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 269dd540b46SJohn Baldwin if (mem_info[i].end <= mem->BaseAddress) 270dd540b46SJohn Baldwin continue; 271dd540b46SJohn Baldwin if (mem_info[i].start < 272dd540b46SJohn Baldwin (mem->BaseAddress + mem->Length)) { 273dd540b46SJohn Baldwin printf("SRAT: Overlapping memory entries\n"); 274dd540b46SJohn Baldwin *(int *)arg = ENXIO; 275dd540b46SJohn Baldwin return; 276dd540b46SJohn Baldwin } 277dd540b46SJohn Baldwin slot = i; 278dd540b46SJohn Baldwin } 279dd540b46SJohn Baldwin for (i = num_mem; i > slot; i--) 280dd540b46SJohn Baldwin mem_info[i] = mem_info[i - 1]; 281dd540b46SJohn Baldwin mem_info[slot].start = mem->BaseAddress; 282dd540b46SJohn Baldwin mem_info[slot].end = mem->BaseAddress + mem->Length; 283dd540b46SJohn Baldwin mem_info[slot].domain = mem->ProximityDomain; 284dd540b46SJohn Baldwin num_mem++; 285dd540b46SJohn Baldwin break; 286dd540b46SJohn Baldwin } 287dd540b46SJohn Baldwin } 288dd540b46SJohn Baldwin 289dd540b46SJohn Baldwin /* 290dd540b46SJohn Baldwin * Ensure each memory domain has at least one CPU and that each CPU 291dd540b46SJohn Baldwin * has at least one memory domain. 292dd540b46SJohn Baldwin */ 293dd540b46SJohn Baldwin static int 294dd540b46SJohn Baldwin check_domains(void) 295dd540b46SJohn Baldwin { 296dd540b46SJohn Baldwin int found, i, j; 297dd540b46SJohn Baldwin 298dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 299dd540b46SJohn Baldwin found = 0; 30084525e55SRoger Pau Monné for (j = 0; j <= max_apic_id; j++) 3016676877bSJohn Baldwin if (cpus[j].enabled && 3026676877bSJohn Baldwin cpus[j].domain == mem_info[i].domain) { 303dd540b46SJohn Baldwin cpus[j].has_memory = 1; 304dd540b46SJohn Baldwin found++; 305dd540b46SJohn Baldwin } 306dd540b46SJohn Baldwin if (!found) { 307dd540b46SJohn Baldwin printf("SRAT: No CPU found for memory domain %d\n", 308dd540b46SJohn Baldwin mem_info[i].domain); 309dd540b46SJohn Baldwin return (ENXIO); 310dd540b46SJohn Baldwin } 311dd540b46SJohn Baldwin } 31284525e55SRoger Pau Monné for (i = 0; i <= max_apic_id; i++) 313dd540b46SJohn Baldwin if (cpus[i].enabled && !cpus[i].has_memory) { 314*30c5525bSAndrew Gallatin found = 0; 315*30c5525bSAndrew Gallatin for (j = 0; j < num_mem && !found; j++) { 316*30c5525bSAndrew Gallatin if (mem_info[j].domain == cpus[i].domain) 317*30c5525bSAndrew Gallatin found = 1; 318*30c5525bSAndrew Gallatin } 319*30c5525bSAndrew Gallatin if (!found) { 320*30c5525bSAndrew Gallatin if (bootverbose) 321*30c5525bSAndrew Gallatin printf("SRAT: mem dom %d is empty\n", 322*30c5525bSAndrew Gallatin cpus[i].domain); 323*30c5525bSAndrew Gallatin mem_info[num_mem].start = 0; 324*30c5525bSAndrew Gallatin mem_info[num_mem].end = 0; 325*30c5525bSAndrew Gallatin mem_info[num_mem].domain = cpus[i].domain; 326*30c5525bSAndrew Gallatin num_mem++; 327*30c5525bSAndrew Gallatin } 328dd540b46SJohn Baldwin } 329dd540b46SJohn Baldwin return (0); 330dd540b46SJohn Baldwin } 331dd540b46SJohn Baldwin 332dd540b46SJohn Baldwin /* 333dd540b46SJohn Baldwin * Check that the SRAT memory regions cover all of the regions in 334dd540b46SJohn Baldwin * phys_avail[]. 335dd540b46SJohn Baldwin */ 336dd540b46SJohn Baldwin static int 337dd540b46SJohn Baldwin check_phys_avail(void) 338dd540b46SJohn Baldwin { 339dd540b46SJohn Baldwin vm_paddr_t address; 340dd540b46SJohn Baldwin int i, j; 341dd540b46SJohn Baldwin 342dd540b46SJohn Baldwin /* j is the current offset into phys_avail[]. */ 343dd540b46SJohn Baldwin address = phys_avail[0]; 344dd540b46SJohn Baldwin j = 0; 345dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 346dd540b46SJohn Baldwin /* 347dd540b46SJohn Baldwin * Consume as many phys_avail[] entries as fit in this 348dd540b46SJohn Baldwin * region. 349dd540b46SJohn Baldwin */ 350dd540b46SJohn Baldwin while (address >= mem_info[i].start && 351dd540b46SJohn Baldwin address <= mem_info[i].end) { 352dd540b46SJohn Baldwin /* 353dd540b46SJohn Baldwin * If we cover the rest of this phys_avail[] entry, 354dd540b46SJohn Baldwin * advance to the next entry. 355dd540b46SJohn Baldwin */ 356dd540b46SJohn Baldwin if (phys_avail[j + 1] <= mem_info[i].end) { 357dd540b46SJohn Baldwin j += 2; 358dd540b46SJohn Baldwin if (phys_avail[j] == 0 && 359dd540b46SJohn Baldwin phys_avail[j + 1] == 0) { 360dd540b46SJohn Baldwin return (0); 361dd540b46SJohn Baldwin } 362dd540b46SJohn Baldwin address = phys_avail[j]; 363dd540b46SJohn Baldwin } else 364dd540b46SJohn Baldwin address = mem_info[i].end + 1; 365dd540b46SJohn Baldwin } 366dd540b46SJohn Baldwin } 3675ab0f0c3SKonstantin Belousov printf("SRAT: No memory region found for 0x%jx - 0x%jx\n", 368dd540b46SJohn Baldwin (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]); 369dd540b46SJohn Baldwin return (ENXIO); 370dd540b46SJohn Baldwin } 371dd540b46SJohn Baldwin 372dd540b46SJohn Baldwin /* 373dd540b46SJohn Baldwin * Renumber the memory domains to be compact and zero-based if not 37428990874SJohn Baldwin * already. Returns an error if there are too many domains. 375dd540b46SJohn Baldwin */ 37628990874SJohn Baldwin static int 377dd540b46SJohn Baldwin renumber_domains(void) 378dd540b46SJohn Baldwin { 3797e226537SAttilio Rao int i, j, slot; 380dd540b46SJohn Baldwin 381dd540b46SJohn Baldwin /* Enumerate all the domains. */ 38262d70a81SJohn Baldwin ndomain = 0; 383dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 384dd540b46SJohn Baldwin /* See if this domain is already known. */ 38562d70a81SJohn Baldwin for (j = 0; j < ndomain; j++) { 38662d70a81SJohn Baldwin if (domain_pxm[j] >= mem_info[i].domain) 387dd540b46SJohn Baldwin break; 388dd540b46SJohn Baldwin } 38962d70a81SJohn Baldwin if (j < ndomain && domain_pxm[j] == mem_info[i].domain) 390dd540b46SJohn Baldwin continue; 391dd540b46SJohn Baldwin 3923765b809SConrad Meyer if (ndomain >= MAXMEMDOM) { 3933765b809SConrad Meyer ndomain = 1; 3943765b809SConrad Meyer printf("SRAT: Too many memory domains\n"); 3953765b809SConrad Meyer return (EFBIG); 3963765b809SConrad Meyer } 3973765b809SConrad Meyer 398dd540b46SJohn Baldwin /* Insert the new domain at slot 'j'. */ 399dd540b46SJohn Baldwin slot = j; 40062d70a81SJohn Baldwin for (j = ndomain; j > slot; j--) 40162d70a81SJohn Baldwin domain_pxm[j] = domain_pxm[j - 1]; 40262d70a81SJohn Baldwin domain_pxm[slot] = mem_info[i].domain; 40362d70a81SJohn Baldwin ndomain++; 404dd540b46SJohn Baldwin } 405dd540b46SJohn Baldwin 40662d70a81SJohn Baldwin /* Renumber each domain to its index in the sorted 'domain_pxm' list. */ 40762d70a81SJohn Baldwin for (i = 0; i < ndomain; i++) { 408dd540b46SJohn Baldwin /* 409dd540b46SJohn Baldwin * If the domain is already the right value, no need 410dd540b46SJohn Baldwin * to renumber. 411dd540b46SJohn Baldwin */ 41262d70a81SJohn Baldwin if (domain_pxm[i] == i) 413dd540b46SJohn Baldwin continue; 414dd540b46SJohn Baldwin 415dd540b46SJohn Baldwin /* Walk the cpu[] and mem_info[] arrays to renumber. */ 416dd540b46SJohn Baldwin for (j = 0; j < num_mem; j++) 41762d70a81SJohn Baldwin if (mem_info[j].domain == domain_pxm[i]) 418dd540b46SJohn Baldwin mem_info[j].domain = i; 41984525e55SRoger Pau Monné for (j = 0; j <= max_apic_id; j++) 42062d70a81SJohn Baldwin if (cpus[j].enabled && cpus[j].domain == domain_pxm[i]) 421dd540b46SJohn Baldwin cpus[j].domain = i; 422dd540b46SJohn Baldwin } 4237e226537SAttilio Rao 42428990874SJohn Baldwin return (0); 425dd540b46SJohn Baldwin } 426dd540b46SJohn Baldwin 427dd540b46SJohn Baldwin /* 428dd540b46SJohn Baldwin * Look for an ACPI System Resource Affinity Table ("SRAT") 429dd540b46SJohn Baldwin */ 430415d7ccaSAdrian Chadd static int 431415d7ccaSAdrian Chadd parse_srat(void) 432dd540b46SJohn Baldwin { 43384525e55SRoger Pau Monné unsigned int idx, size; 43484525e55SRoger Pau Monné vm_paddr_t addr; 435dd540b46SJohn Baldwin int error; 436dd540b46SJohn Baldwin 437dd540b46SJohn Baldwin if (resource_disabled("srat", 0)) 438415d7ccaSAdrian Chadd return (-1); 439dd540b46SJohn Baldwin 440dd540b46SJohn Baldwin srat_physaddr = acpi_find_table(ACPI_SIG_SRAT); 441dd540b46SJohn Baldwin if (srat_physaddr == 0) 442415d7ccaSAdrian Chadd return (-1); 443dd540b46SJohn Baldwin 444dd540b46SJohn Baldwin /* 44584525e55SRoger Pau Monné * Allocate data structure: 44684525e55SRoger Pau Monné * 44784525e55SRoger Pau Monné * Find the last physical memory region and steal some memory from 44884525e55SRoger Pau Monné * it. This is done because at this point in the boot process 44984525e55SRoger Pau Monné * malloc is still not usable. 45084525e55SRoger Pau Monné */ 45184525e55SRoger Pau Monné for (idx = 0; phys_avail[idx + 1] != 0; idx += 2); 45284525e55SRoger Pau Monné KASSERT(idx != 0, ("phys_avail is empty!")); 45384525e55SRoger Pau Monné idx -= 2; 45484525e55SRoger Pau Monné 45584525e55SRoger Pau Monné size = sizeof(*cpus) * (max_apic_id + 1); 45684525e55SRoger Pau Monné addr = trunc_page(phys_avail[idx + 1] - size); 45784525e55SRoger Pau Monné KASSERT(addr >= phys_avail[idx], 45884525e55SRoger Pau Monné ("Not enough memory for SRAT table items")); 45984525e55SRoger Pau Monné phys_avail[idx + 1] = addr - 1; 46084525e55SRoger Pau Monné 461c642d2f5SRoger Pau Monné /* 462c642d2f5SRoger Pau Monné * We cannot rely on PHYS_TO_DMAP because this code is also used in 463c642d2f5SRoger Pau Monné * i386, so use pmap_mapbios to map the memory, this will end up using 464c642d2f5SRoger Pau Monné * the default memory attribute (WB), and the DMAP when available. 465c642d2f5SRoger Pau Monné */ 466c642d2f5SRoger Pau Monné cpus = (struct cpu_info *)pmap_mapbios(addr, size); 46745ff071dSRoger Pau Monné bzero(cpus, size); 46884525e55SRoger Pau Monné 46984525e55SRoger Pau Monné /* 470dd540b46SJohn Baldwin * Make a pass over the table to populate the cpus[] and 471dd540b46SJohn Baldwin * mem_info[] tables. 472dd540b46SJohn Baldwin */ 473dd540b46SJohn Baldwin srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT); 474dd540b46SJohn Baldwin error = 0; 475dd540b46SJohn Baldwin srat_walk_table(srat_parse_entry, &error); 476dd540b46SJohn Baldwin acpi_unmap_table(srat); 477dd540b46SJohn Baldwin srat = NULL; 47828990874SJohn Baldwin if (error || check_domains() != 0 || check_phys_avail() != 0 || 47928990874SJohn Baldwin renumber_domains() != 0) { 480dd540b46SJohn Baldwin srat_physaddr = 0; 481415d7ccaSAdrian Chadd return (-1); 482dd540b46SJohn Baldwin } 483dd540b46SJohn Baldwin 484b6715dabSJeff Roberson #ifdef NUMA 48562d70a81SJohn Baldwin vm_ndomains = ndomain; 486463406acSMark Johnston for (int i = 0; i < vm_ndomains; i++) 487463406acSMark Johnston DOMAINSET_SET(i, &all_domains); 488dd540b46SJohn Baldwin mem_affinity = mem_info; 48962d70a81SJohn Baldwin #endif 490415d7ccaSAdrian Chadd 491415d7ccaSAdrian Chadd return (0); 492dd540b46SJohn Baldwin } 493415d7ccaSAdrian Chadd 494415d7ccaSAdrian Chadd static void 495415d7ccaSAdrian Chadd init_mem_locality(void) 496415d7ccaSAdrian Chadd { 497415d7ccaSAdrian Chadd int i; 498415d7ccaSAdrian Chadd 499415d7ccaSAdrian Chadd /* 5004f5e270aSAdrian Chadd * For now, assume -1 == "no locality information for 501415d7ccaSAdrian Chadd * this pairing. 502415d7ccaSAdrian Chadd */ 503415d7ccaSAdrian Chadd for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++) 504415d7ccaSAdrian Chadd vm_locality_table[i] = -1; 505415d7ccaSAdrian Chadd } 506415d7ccaSAdrian Chadd 507415d7ccaSAdrian Chadd static void 508415d7ccaSAdrian Chadd parse_acpi_tables(void *dummy) 509415d7ccaSAdrian Chadd { 510415d7ccaSAdrian Chadd 511415d7ccaSAdrian Chadd if (parse_srat() < 0) 512415d7ccaSAdrian Chadd return; 513415d7ccaSAdrian Chadd init_mem_locality(); 514415d7ccaSAdrian Chadd (void) parse_slit(); 515415d7ccaSAdrian Chadd } 516415d7ccaSAdrian Chadd SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables, 517415d7ccaSAdrian Chadd NULL); 518dd540b46SJohn Baldwin 519dd540b46SJohn Baldwin static void 520dd540b46SJohn Baldwin srat_walk_table(acpi_subtable_handler *handler, void *arg) 521dd540b46SJohn Baldwin { 522dd540b46SJohn Baldwin 523dd540b46SJohn Baldwin acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length, 524dd540b46SJohn Baldwin handler, arg); 525dd540b46SJohn Baldwin } 526dd540b46SJohn Baldwin 527dd540b46SJohn Baldwin /* 528c0ae6688SJohn Baldwin * Setup per-CPU domain IDs. 529dd540b46SJohn Baldwin */ 530dd540b46SJohn Baldwin static void 531dd540b46SJohn Baldwin srat_set_cpus(void *dummy) 532dd540b46SJohn Baldwin { 533dd540b46SJohn Baldwin struct cpu_info *cpu; 534dd540b46SJohn Baldwin struct pcpu *pc; 535dd540b46SJohn Baldwin u_int i; 536dd540b46SJohn Baldwin 537dd540b46SJohn Baldwin if (srat_physaddr == 0) 538dd540b46SJohn Baldwin return; 539dd540b46SJohn Baldwin for (i = 0; i < MAXCPU; i++) { 540dd540b46SJohn Baldwin if (CPU_ABSENT(i)) 541dd540b46SJohn Baldwin continue; 542dd540b46SJohn Baldwin pc = pcpu_find(i); 543dd540b46SJohn Baldwin KASSERT(pc != NULL, ("no pcpu data for CPU %u", i)); 544dd540b46SJohn Baldwin cpu = &cpus[pc->pc_apic_id]; 545dd540b46SJohn Baldwin if (!cpu->enabled) 546dd540b46SJohn Baldwin panic("SRAT: CPU with APIC ID %u is not known", 547dd540b46SJohn Baldwin pc->pc_apic_id); 548ab3059a8SMatt Macy #ifdef NUMA 549dd540b46SJohn Baldwin pc->pc_domain = cpu->domain; 550ab3059a8SMatt Macy #else 551ab3059a8SMatt Macy pc->pc_domain = 0; 552ab3059a8SMatt Macy #endif 553ab3059a8SMatt Macy CPU_SET(i, &cpuset_domain[pc->pc_domain]); 554dd540b46SJohn Baldwin if (bootverbose) 555dd540b46SJohn Baldwin printf("SRAT: CPU %u has memory domain %d\n", i, 556ab3059a8SMatt Macy pc->pc_domain); 557dd540b46SJohn Baldwin } 558c642d2f5SRoger Pau Monné 559c642d2f5SRoger Pau Monné /* Last usage of the cpus array, unmap it. */ 56072446721SRoger Pau Monné pmap_unmapbios((vm_offset_t)cpus, sizeof(*cpus) * (max_apic_id + 1)); 561c642d2f5SRoger Pau Monné cpus = NULL; 562dd540b46SJohn Baldwin } 563dd540b46SJohn Baldwin SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL); 564fc4f524aSAdrian Chadd 565fc4f524aSAdrian Chadd /* 566fc4f524aSAdrian Chadd * Map a _PXM value to a VM domain ID. 567fc4f524aSAdrian Chadd * 568fc4f524aSAdrian Chadd * Returns the domain ID, or -1 if no domain ID was found. 569fc4f524aSAdrian Chadd */ 570fc4f524aSAdrian Chadd int 571fc4f524aSAdrian Chadd acpi_map_pxm_to_vm_domainid(int pxm) 572fc4f524aSAdrian Chadd { 573fc4f524aSAdrian Chadd int i; 574fc4f524aSAdrian Chadd 57562d70a81SJohn Baldwin for (i = 0; i < ndomain; i++) { 57662d70a81SJohn Baldwin if (domain_pxm[i] == pxm) 577fc4f524aSAdrian Chadd return (i); 578fc4f524aSAdrian Chadd } 579fc4f524aSAdrian Chadd 580fc4f524aSAdrian Chadd return (-1); 581fc4f524aSAdrian Chadd } 582fc4f524aSAdrian Chadd 58362d70a81SJohn Baldwin #else /* MAXMEMDOM == 1 */ 58462d70a81SJohn Baldwin 58562d70a81SJohn Baldwin int 58662d70a81SJohn Baldwin acpi_map_pxm_to_vm_domainid(int pxm) 58762d70a81SJohn Baldwin { 58862d70a81SJohn Baldwin 58962d70a81SJohn Baldwin return (-1); 59062d70a81SJohn Baldwin } 59162d70a81SJohn Baldwin 592941646f5SAttilio Rao #endif /* MAXMEMDOM > 1 */ 593