1dd540b46SJohn Baldwin /*- 2179fa75eSJohn Baldwin * Copyright (c) 2010 Hudson River Trading LLC 3dd540b46SJohn Baldwin * Written by: John H. Baldwin <jhb@FreeBSD.org> 4dd540b46SJohn Baldwin * All rights reserved. 5dd540b46SJohn Baldwin * 6dd540b46SJohn Baldwin * Redistribution and use in source and binary forms, with or without 7dd540b46SJohn Baldwin * modification, are permitted provided that the following conditions 8dd540b46SJohn Baldwin * are met: 9dd540b46SJohn Baldwin * 1. Redistributions of source code must retain the above copyright 10dd540b46SJohn Baldwin * notice, this list of conditions and the following disclaimer. 11dd540b46SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright 12dd540b46SJohn Baldwin * notice, this list of conditions and the following disclaimer in the 13dd540b46SJohn Baldwin * documentation and/or other materials provided with the distribution. 14dd540b46SJohn Baldwin * 15dd540b46SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16dd540b46SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17dd540b46SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18dd540b46SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19dd540b46SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20dd540b46SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21dd540b46SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22dd540b46SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23dd540b46SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24dd540b46SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25dd540b46SJohn Baldwin * SUCH DAMAGE. 26dd540b46SJohn Baldwin */ 27dd540b46SJohn Baldwin 28dd540b46SJohn Baldwin #include <sys/cdefs.h> 29dd540b46SJohn Baldwin __FBSDID("$FreeBSD$"); 30dd540b46SJohn Baldwin 31dd540b46SJohn Baldwin #include <sys/param.h> 32dd540b46SJohn Baldwin #include <sys/bus.h> 33dd540b46SJohn Baldwin #include <sys/kernel.h> 34449c2e92SKonstantin Belousov #include <sys/lock.h> 35449c2e92SKonstantin Belousov #include <sys/mutex.h> 36dd540b46SJohn Baldwin #include <sys/smp.h> 37449c2e92SKonstantin Belousov #include <sys/vmmeter.h> 38dd540b46SJohn Baldwin #include <vm/vm.h> 39dd540b46SJohn Baldwin #include <vm/pmap.h> 40dd540b46SJohn Baldwin #include <vm/vm_param.h> 41449c2e92SKonstantin Belousov #include <vm/vm_page.h> 42dd540b46SJohn Baldwin #include <vm/vm_phys.h> 43dd540b46SJohn Baldwin 44dd540b46SJohn Baldwin #include <contrib/dev/acpica/include/acpi.h> 45dd540b46SJohn Baldwin #include <contrib/dev/acpica/include/actables.h> 46dd540b46SJohn Baldwin 47dd540b46SJohn Baldwin #include <machine/intr_machdep.h> 48e07ef9b0SJohn Baldwin #include <x86/apicvar.h> 49dd540b46SJohn Baldwin 50dd540b46SJohn Baldwin #include <dev/acpica/acpivar.h> 51dd540b46SJohn Baldwin 52941646f5SAttilio Rao #if MAXMEMDOM > 1 53dd540b46SJohn Baldwin struct cpu_info { 54dd540b46SJohn Baldwin int enabled:1; 55dd540b46SJohn Baldwin int has_memory:1; 56dd540b46SJohn Baldwin int domain; 57dd540b46SJohn Baldwin } cpus[MAX_APIC_ID + 1]; 58dd540b46SJohn Baldwin 59dd540b46SJohn Baldwin struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1]; 60dd540b46SJohn Baldwin int num_mem; 61dd540b46SJohn Baldwin 62dd540b46SJohn Baldwin static ACPI_TABLE_SRAT *srat; 63dd540b46SJohn Baldwin static vm_paddr_t srat_physaddr; 64dd540b46SJohn Baldwin 65fc4f524aSAdrian Chadd static int vm_domains[VM_PHYSSEG_MAX]; 66fc4f524aSAdrian Chadd 67*415d7ccaSAdrian Chadd static ACPI_TABLE_SLIT *slit; 68*415d7ccaSAdrian Chadd static vm_paddr_t slit_physaddr; 69*415d7ccaSAdrian Chadd static int vm_locality_table[MAXMEMDOM * MAXMEMDOM]; 70*415d7ccaSAdrian Chadd 71dd540b46SJohn Baldwin static void srat_walk_table(acpi_subtable_handler *handler, void *arg); 72dd540b46SJohn Baldwin 734d99cfb3SJohn Baldwin /* 74*415d7ccaSAdrian Chadd * SLIT parsing. 75*415d7ccaSAdrian Chadd */ 76*415d7ccaSAdrian Chadd 77*415d7ccaSAdrian Chadd static void 78*415d7ccaSAdrian Chadd slit_parse_table(ACPI_TABLE_SLIT *s) 79*415d7ccaSAdrian Chadd { 80*415d7ccaSAdrian Chadd int i, j; 81*415d7ccaSAdrian Chadd int i_domain, j_domain; 82*415d7ccaSAdrian Chadd int offset = 0; 83*415d7ccaSAdrian Chadd uint8_t e; 84*415d7ccaSAdrian Chadd 85*415d7ccaSAdrian Chadd /* 86*415d7ccaSAdrian Chadd * This maps the SLIT data into the VM-domain centric view. 87*415d7ccaSAdrian Chadd * There may be sparse entries in the PXM namespace, so 88*415d7ccaSAdrian Chadd * remap them to a VM-domain ID and if it doesn't exist, 89*415d7ccaSAdrian Chadd * skip it. 90*415d7ccaSAdrian Chadd * 91*415d7ccaSAdrian Chadd * It should result in a packed 2d array of VM-domain 92*415d7ccaSAdrian Chadd * locality information entries. 93*415d7ccaSAdrian Chadd */ 94*415d7ccaSAdrian Chadd 95*415d7ccaSAdrian Chadd if (bootverbose) 96*415d7ccaSAdrian Chadd printf("SLIT.Localities: %d\n", (int) s->LocalityCount); 97*415d7ccaSAdrian Chadd for (i = 0; i < s->LocalityCount; i++) { 98*415d7ccaSAdrian Chadd i_domain = acpi_map_pxm_to_vm_domainid(i); 99*415d7ccaSAdrian Chadd if (i_domain < 0) 100*415d7ccaSAdrian Chadd continue; 101*415d7ccaSAdrian Chadd 102*415d7ccaSAdrian Chadd if (bootverbose) 103*415d7ccaSAdrian Chadd printf("%d: ", i); 104*415d7ccaSAdrian Chadd for (j = 0; j < s->LocalityCount; j++) { 105*415d7ccaSAdrian Chadd j_domain = acpi_map_pxm_to_vm_domainid(j); 106*415d7ccaSAdrian Chadd if (j_domain < 0) 107*415d7ccaSAdrian Chadd continue; 108*415d7ccaSAdrian Chadd e = s->Entry[i * s->LocalityCount + j]; 109*415d7ccaSAdrian Chadd if (bootverbose) 110*415d7ccaSAdrian Chadd printf("%d ", (int) e); 111*415d7ccaSAdrian Chadd /* 255 == "no locality information" */ 112*415d7ccaSAdrian Chadd if (e == 255) 113*415d7ccaSAdrian Chadd vm_locality_table[offset] = -1; 114*415d7ccaSAdrian Chadd else 115*415d7ccaSAdrian Chadd vm_locality_table[offset] = e; 116*415d7ccaSAdrian Chadd offset++; 117*415d7ccaSAdrian Chadd } 118*415d7ccaSAdrian Chadd if (bootverbose) 119*415d7ccaSAdrian Chadd printf("\n"); 120*415d7ccaSAdrian Chadd } 121*415d7ccaSAdrian Chadd } 122*415d7ccaSAdrian Chadd 123*415d7ccaSAdrian Chadd /* 124*415d7ccaSAdrian Chadd * Look for an ACPI System Locality Distance Information Table ("SLIT") 125*415d7ccaSAdrian Chadd */ 126*415d7ccaSAdrian Chadd static int 127*415d7ccaSAdrian Chadd parse_slit(void) 128*415d7ccaSAdrian Chadd { 129*415d7ccaSAdrian Chadd 130*415d7ccaSAdrian Chadd if (resource_disabled("slit", 0)) { 131*415d7ccaSAdrian Chadd return (-1); 132*415d7ccaSAdrian Chadd } 133*415d7ccaSAdrian Chadd 134*415d7ccaSAdrian Chadd slit_physaddr = acpi_find_table(ACPI_SIG_SLIT); 135*415d7ccaSAdrian Chadd if (slit_physaddr == 0) { 136*415d7ccaSAdrian Chadd return (-1); 137*415d7ccaSAdrian Chadd } 138*415d7ccaSAdrian Chadd 139*415d7ccaSAdrian Chadd /* 140*415d7ccaSAdrian Chadd * Make a pass over the table to populate the cpus[] and 141*415d7ccaSAdrian Chadd * mem_info[] tables. 142*415d7ccaSAdrian Chadd */ 143*415d7ccaSAdrian Chadd slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT); 144*415d7ccaSAdrian Chadd slit_parse_table(slit); 145*415d7ccaSAdrian Chadd acpi_unmap_table(slit); 146*415d7ccaSAdrian Chadd slit = NULL; 147*415d7ccaSAdrian Chadd 148*415d7ccaSAdrian Chadd /* Tell the VM about it! */ 149*415d7ccaSAdrian Chadd mem_locality = vm_locality_table; 150*415d7ccaSAdrian Chadd return (0); 151*415d7ccaSAdrian Chadd } 152*415d7ccaSAdrian Chadd 153*415d7ccaSAdrian Chadd /* 154*415d7ccaSAdrian Chadd * SRAT parsing. 155*415d7ccaSAdrian Chadd */ 156*415d7ccaSAdrian Chadd 157*415d7ccaSAdrian Chadd /* 1584d99cfb3SJohn Baldwin * Returns true if a memory range overlaps with at least one range in 1594d99cfb3SJohn Baldwin * phys_avail[]. 1604d99cfb3SJohn Baldwin */ 1614d99cfb3SJohn Baldwin static int 1624d99cfb3SJohn Baldwin overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end) 1634d99cfb3SJohn Baldwin { 1644d99cfb3SJohn Baldwin int i; 1654d99cfb3SJohn Baldwin 1664d99cfb3SJohn Baldwin for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) { 1674d99cfb3SJohn Baldwin if (phys_avail[i + 1] < start) 1684d99cfb3SJohn Baldwin continue; 1694d99cfb3SJohn Baldwin if (phys_avail[i] < end) 1704d99cfb3SJohn Baldwin return (1); 1714d99cfb3SJohn Baldwin break; 1724d99cfb3SJohn Baldwin } 1734d99cfb3SJohn Baldwin return (0); 1744d99cfb3SJohn Baldwin 1754d99cfb3SJohn Baldwin } 1764d99cfb3SJohn Baldwin 177dd540b46SJohn Baldwin static void 178dd540b46SJohn Baldwin srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg) 179dd540b46SJohn Baldwin { 180dd540b46SJohn Baldwin ACPI_SRAT_CPU_AFFINITY *cpu; 181dd540b46SJohn Baldwin ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic; 182dd540b46SJohn Baldwin ACPI_SRAT_MEM_AFFINITY *mem; 183dd540b46SJohn Baldwin int domain, i, slot; 184dd540b46SJohn Baldwin 185dd540b46SJohn Baldwin switch (entry->Type) { 186dd540b46SJohn Baldwin case ACPI_SRAT_TYPE_CPU_AFFINITY: 187dd540b46SJohn Baldwin cpu = (ACPI_SRAT_CPU_AFFINITY *)entry; 188dd540b46SJohn Baldwin domain = cpu->ProximityDomainLo | 189dd540b46SJohn Baldwin cpu->ProximityDomainHi[0] << 8 | 190dd540b46SJohn Baldwin cpu->ProximityDomainHi[1] << 16 | 191dd540b46SJohn Baldwin cpu->ProximityDomainHi[2] << 24; 192dd540b46SJohn Baldwin if (bootverbose) 193dd540b46SJohn Baldwin printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 194dd540b46SJohn Baldwin cpu->ApicId, domain, 195dd540b46SJohn Baldwin (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ? 196dd540b46SJohn Baldwin "enabled" : "disabled"); 197dd540b46SJohn Baldwin if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED)) 198dd540b46SJohn Baldwin break; 199dd540b46SJohn Baldwin KASSERT(!cpus[cpu->ApicId].enabled, 200dd540b46SJohn Baldwin ("Duplicate local APIC ID %u", cpu->ApicId)); 201dd540b46SJohn Baldwin cpus[cpu->ApicId].domain = domain; 202dd540b46SJohn Baldwin cpus[cpu->ApicId].enabled = 1; 203dd540b46SJohn Baldwin break; 204dd540b46SJohn Baldwin case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: 205dd540b46SJohn Baldwin x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry; 206dd540b46SJohn Baldwin if (bootverbose) 207dd540b46SJohn Baldwin printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 208dd540b46SJohn Baldwin x2apic->ApicId, x2apic->ProximityDomain, 209dd540b46SJohn Baldwin (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ? 210dd540b46SJohn Baldwin "enabled" : "disabled"); 211dd540b46SJohn Baldwin if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED)) 212dd540b46SJohn Baldwin break; 213dd540b46SJohn Baldwin KASSERT(!cpus[x2apic->ApicId].enabled, 214dd540b46SJohn Baldwin ("Duplicate local APIC ID %u", x2apic->ApicId)); 215dd540b46SJohn Baldwin cpus[x2apic->ApicId].domain = x2apic->ProximityDomain; 216dd540b46SJohn Baldwin cpus[x2apic->ApicId].enabled = 1; 217dd540b46SJohn Baldwin break; 218dd540b46SJohn Baldwin case ACPI_SRAT_TYPE_MEMORY_AFFINITY: 219dd540b46SJohn Baldwin mem = (ACPI_SRAT_MEM_AFFINITY *)entry; 220dd540b46SJohn Baldwin if (bootverbose) 221dd540b46SJohn Baldwin printf( 222dd540b46SJohn Baldwin "SRAT: Found memory domain %d addr %jx len %jx: %s\n", 223dd540b46SJohn Baldwin mem->ProximityDomain, (uintmax_t)mem->BaseAddress, 224dd540b46SJohn Baldwin (uintmax_t)mem->Length, 225dd540b46SJohn Baldwin (mem->Flags & ACPI_SRAT_MEM_ENABLED) ? 226dd540b46SJohn Baldwin "enabled" : "disabled"); 227dd540b46SJohn Baldwin if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED)) 228dd540b46SJohn Baldwin break; 2294d99cfb3SJohn Baldwin if (!overlaps_phys_avail(mem->BaseAddress, 2304d99cfb3SJohn Baldwin mem->BaseAddress + mem->Length)) { 2314d99cfb3SJohn Baldwin printf("SRAT: Ignoring memory at addr %jx\n", 2324d99cfb3SJohn Baldwin (uintmax_t)mem->BaseAddress); 2334d99cfb3SJohn Baldwin break; 2344d99cfb3SJohn Baldwin } 235dd540b46SJohn Baldwin if (num_mem == VM_PHYSSEG_MAX) { 236dd540b46SJohn Baldwin printf("SRAT: Too many memory regions\n"); 237dd540b46SJohn Baldwin *(int *)arg = ENXIO; 238dd540b46SJohn Baldwin break; 239dd540b46SJohn Baldwin } 240dd540b46SJohn Baldwin slot = num_mem; 241dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 242dd540b46SJohn Baldwin if (mem_info[i].end <= mem->BaseAddress) 243dd540b46SJohn Baldwin continue; 244dd540b46SJohn Baldwin if (mem_info[i].start < 245dd540b46SJohn Baldwin (mem->BaseAddress + mem->Length)) { 246dd540b46SJohn Baldwin printf("SRAT: Overlapping memory entries\n"); 247dd540b46SJohn Baldwin *(int *)arg = ENXIO; 248dd540b46SJohn Baldwin return; 249dd540b46SJohn Baldwin } 250dd540b46SJohn Baldwin slot = i; 251dd540b46SJohn Baldwin } 252dd540b46SJohn Baldwin for (i = num_mem; i > slot; i--) 253dd540b46SJohn Baldwin mem_info[i] = mem_info[i - 1]; 254dd540b46SJohn Baldwin mem_info[slot].start = mem->BaseAddress; 255dd540b46SJohn Baldwin mem_info[slot].end = mem->BaseAddress + mem->Length; 256dd540b46SJohn Baldwin mem_info[slot].domain = mem->ProximityDomain; 257dd540b46SJohn Baldwin num_mem++; 258dd540b46SJohn Baldwin break; 259dd540b46SJohn Baldwin } 260dd540b46SJohn Baldwin } 261dd540b46SJohn Baldwin 262dd540b46SJohn Baldwin /* 263dd540b46SJohn Baldwin * Ensure each memory domain has at least one CPU and that each CPU 264dd540b46SJohn Baldwin * has at least one memory domain. 265dd540b46SJohn Baldwin */ 266dd540b46SJohn Baldwin static int 267dd540b46SJohn Baldwin check_domains(void) 268dd540b46SJohn Baldwin { 269dd540b46SJohn Baldwin int found, i, j; 270dd540b46SJohn Baldwin 271dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 272dd540b46SJohn Baldwin found = 0; 273dd540b46SJohn Baldwin for (j = 0; j <= MAX_APIC_ID; j++) 2746676877bSJohn Baldwin if (cpus[j].enabled && 2756676877bSJohn Baldwin cpus[j].domain == mem_info[i].domain) { 276dd540b46SJohn Baldwin cpus[j].has_memory = 1; 277dd540b46SJohn Baldwin found++; 278dd540b46SJohn Baldwin } 279dd540b46SJohn Baldwin if (!found) { 280dd540b46SJohn Baldwin printf("SRAT: No CPU found for memory domain %d\n", 281dd540b46SJohn Baldwin mem_info[i].domain); 282dd540b46SJohn Baldwin return (ENXIO); 283dd540b46SJohn Baldwin } 284dd540b46SJohn Baldwin } 285dd540b46SJohn Baldwin for (i = 0; i <= MAX_APIC_ID; i++) 286dd540b46SJohn Baldwin if (cpus[i].enabled && !cpus[i].has_memory) { 287dd540b46SJohn Baldwin printf("SRAT: No memory found for CPU %d\n", i); 288dd540b46SJohn Baldwin return (ENXIO); 289dd540b46SJohn Baldwin } 290dd540b46SJohn Baldwin return (0); 291dd540b46SJohn Baldwin } 292dd540b46SJohn Baldwin 293dd540b46SJohn Baldwin /* 294dd540b46SJohn Baldwin * Check that the SRAT memory regions cover all of the regions in 295dd540b46SJohn Baldwin * phys_avail[]. 296dd540b46SJohn Baldwin */ 297dd540b46SJohn Baldwin static int 298dd540b46SJohn Baldwin check_phys_avail(void) 299dd540b46SJohn Baldwin { 300dd540b46SJohn Baldwin vm_paddr_t address; 301dd540b46SJohn Baldwin int i, j; 302dd540b46SJohn Baldwin 303dd540b46SJohn Baldwin /* j is the current offset into phys_avail[]. */ 304dd540b46SJohn Baldwin address = phys_avail[0]; 305dd540b46SJohn Baldwin j = 0; 306dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 307dd540b46SJohn Baldwin /* 308dd540b46SJohn Baldwin * Consume as many phys_avail[] entries as fit in this 309dd540b46SJohn Baldwin * region. 310dd540b46SJohn Baldwin */ 311dd540b46SJohn Baldwin while (address >= mem_info[i].start && 312dd540b46SJohn Baldwin address <= mem_info[i].end) { 313dd540b46SJohn Baldwin /* 314dd540b46SJohn Baldwin * If we cover the rest of this phys_avail[] entry, 315dd540b46SJohn Baldwin * advance to the next entry. 316dd540b46SJohn Baldwin */ 317dd540b46SJohn Baldwin if (phys_avail[j + 1] <= mem_info[i].end) { 318dd540b46SJohn Baldwin j += 2; 319dd540b46SJohn Baldwin if (phys_avail[j] == 0 && 320dd540b46SJohn Baldwin phys_avail[j + 1] == 0) { 321dd540b46SJohn Baldwin return (0); 322dd540b46SJohn Baldwin } 323dd540b46SJohn Baldwin address = phys_avail[j]; 324dd540b46SJohn Baldwin } else 325dd540b46SJohn Baldwin address = mem_info[i].end + 1; 326dd540b46SJohn Baldwin } 327dd540b46SJohn Baldwin } 328dd540b46SJohn Baldwin printf("SRAT: No memory region found for %jx - %jx\n", 329dd540b46SJohn Baldwin (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]); 330dd540b46SJohn Baldwin return (ENXIO); 331dd540b46SJohn Baldwin } 332dd540b46SJohn Baldwin 333dd540b46SJohn Baldwin /* 334dd540b46SJohn Baldwin * Renumber the memory domains to be compact and zero-based if not 33528990874SJohn Baldwin * already. Returns an error if there are too many domains. 336dd540b46SJohn Baldwin */ 33728990874SJohn Baldwin static int 338dd540b46SJohn Baldwin renumber_domains(void) 339dd540b46SJohn Baldwin { 3407e226537SAttilio Rao int i, j, slot; 341dd540b46SJohn Baldwin 342dd540b46SJohn Baldwin /* Enumerate all the domains. */ 3437e226537SAttilio Rao vm_ndomains = 0; 344dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 345dd540b46SJohn Baldwin /* See if this domain is already known. */ 3467e226537SAttilio Rao for (j = 0; j < vm_ndomains; j++) { 347fc4f524aSAdrian Chadd if (vm_domains[j] >= mem_info[i].domain) 348dd540b46SJohn Baldwin break; 349dd540b46SJohn Baldwin } 350fc4f524aSAdrian Chadd if (j < vm_ndomains && vm_domains[j] == mem_info[i].domain) 351dd540b46SJohn Baldwin continue; 352dd540b46SJohn Baldwin 353dd540b46SJohn Baldwin /* Insert the new domain at slot 'j'. */ 354dd540b46SJohn Baldwin slot = j; 3557e226537SAttilio Rao for (j = vm_ndomains; j > slot; j--) 356fc4f524aSAdrian Chadd vm_domains[j] = vm_domains[j - 1]; 357fc4f524aSAdrian Chadd vm_domains[slot] = mem_info[i].domain; 3587e226537SAttilio Rao vm_ndomains++; 3597e226537SAttilio Rao if (vm_ndomains > MAXMEMDOM) { 3607e226537SAttilio Rao vm_ndomains = 1; 36128990874SJohn Baldwin printf("SRAT: Too many memory domains\n"); 36228990874SJohn Baldwin return (EFBIG); 36328990874SJohn Baldwin } 364dd540b46SJohn Baldwin } 365dd540b46SJohn Baldwin 366dd540b46SJohn Baldwin /* Renumber each domain to its index in the sorted 'domains' list. */ 3677e226537SAttilio Rao for (i = 0; i < vm_ndomains; i++) { 368dd540b46SJohn Baldwin /* 369dd540b46SJohn Baldwin * If the domain is already the right value, no need 370dd540b46SJohn Baldwin * to renumber. 371dd540b46SJohn Baldwin */ 372fc4f524aSAdrian Chadd if (vm_domains[i] == i) 373dd540b46SJohn Baldwin continue; 374dd540b46SJohn Baldwin 375dd540b46SJohn Baldwin /* Walk the cpu[] and mem_info[] arrays to renumber. */ 376dd540b46SJohn Baldwin for (j = 0; j < num_mem; j++) 377fc4f524aSAdrian Chadd if (mem_info[j].domain == vm_domains[i]) 378dd540b46SJohn Baldwin mem_info[j].domain = i; 379dd540b46SJohn Baldwin for (j = 0; j <= MAX_APIC_ID; j++) 380fc4f524aSAdrian Chadd if (cpus[j].enabled && cpus[j].domain == vm_domains[i]) 381dd540b46SJohn Baldwin cpus[j].domain = i; 382dd540b46SJohn Baldwin } 3837e226537SAttilio Rao KASSERT(vm_ndomains > 0, 3847e226537SAttilio Rao ("renumber_domains: invalid final vm_ndomains setup")); 3857e226537SAttilio Rao 38628990874SJohn Baldwin return (0); 387dd540b46SJohn Baldwin } 388dd540b46SJohn Baldwin 389dd540b46SJohn Baldwin /* 390dd540b46SJohn Baldwin * Look for an ACPI System Resource Affinity Table ("SRAT") 391dd540b46SJohn Baldwin */ 392*415d7ccaSAdrian Chadd static int 393*415d7ccaSAdrian Chadd parse_srat(void) 394dd540b46SJohn Baldwin { 395dd540b46SJohn Baldwin int error; 396dd540b46SJohn Baldwin 397dd540b46SJohn Baldwin if (resource_disabled("srat", 0)) 398*415d7ccaSAdrian Chadd return (-1); 399dd540b46SJohn Baldwin 400dd540b46SJohn Baldwin srat_physaddr = acpi_find_table(ACPI_SIG_SRAT); 401dd540b46SJohn Baldwin if (srat_physaddr == 0) 402*415d7ccaSAdrian Chadd return (-1); 403dd540b46SJohn Baldwin 404dd540b46SJohn Baldwin /* 405dd540b46SJohn Baldwin * Make a pass over the table to populate the cpus[] and 406dd540b46SJohn Baldwin * mem_info[] tables. 407dd540b46SJohn Baldwin */ 408dd540b46SJohn Baldwin srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT); 409dd540b46SJohn Baldwin error = 0; 410dd540b46SJohn Baldwin srat_walk_table(srat_parse_entry, &error); 411dd540b46SJohn Baldwin acpi_unmap_table(srat); 412dd540b46SJohn Baldwin srat = NULL; 41328990874SJohn Baldwin if (error || check_domains() != 0 || check_phys_avail() != 0 || 41428990874SJohn Baldwin renumber_domains() != 0) { 415dd540b46SJohn Baldwin srat_physaddr = 0; 416*415d7ccaSAdrian Chadd return (-1); 417dd540b46SJohn Baldwin } 418dd540b46SJohn Baldwin 419dd540b46SJohn Baldwin /* Point vm_phys at our memory affinity table. */ 420dd540b46SJohn Baldwin mem_affinity = mem_info; 421*415d7ccaSAdrian Chadd 422*415d7ccaSAdrian Chadd return (0); 423dd540b46SJohn Baldwin } 424*415d7ccaSAdrian Chadd 425*415d7ccaSAdrian Chadd static void 426*415d7ccaSAdrian Chadd init_mem_locality(void) 427*415d7ccaSAdrian Chadd { 428*415d7ccaSAdrian Chadd int i; 429*415d7ccaSAdrian Chadd 430*415d7ccaSAdrian Chadd /* 431*415d7ccaSAdrian Chadd * For now, assume 255 == "no locality information for 432*415d7ccaSAdrian Chadd * this pairing. 433*415d7ccaSAdrian Chadd */ 434*415d7ccaSAdrian Chadd for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++) 435*415d7ccaSAdrian Chadd vm_locality_table[i] = -1; 436*415d7ccaSAdrian Chadd } 437*415d7ccaSAdrian Chadd 438*415d7ccaSAdrian Chadd static void 439*415d7ccaSAdrian Chadd parse_acpi_tables(void *dummy) 440*415d7ccaSAdrian Chadd { 441*415d7ccaSAdrian Chadd 442*415d7ccaSAdrian Chadd if (parse_srat() < 0) 443*415d7ccaSAdrian Chadd return; 444*415d7ccaSAdrian Chadd init_mem_locality(); 445*415d7ccaSAdrian Chadd (void) parse_slit(); 446*415d7ccaSAdrian Chadd } 447*415d7ccaSAdrian Chadd SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables, 448*415d7ccaSAdrian Chadd NULL); 449dd540b46SJohn Baldwin 450dd540b46SJohn Baldwin static void 451dd540b46SJohn Baldwin srat_walk_table(acpi_subtable_handler *handler, void *arg) 452dd540b46SJohn Baldwin { 453dd540b46SJohn Baldwin 454dd540b46SJohn Baldwin acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length, 455dd540b46SJohn Baldwin handler, arg); 456dd540b46SJohn Baldwin } 457dd540b46SJohn Baldwin 458dd540b46SJohn Baldwin /* 459c0ae6688SJohn Baldwin * Setup per-CPU domain IDs. 460dd540b46SJohn Baldwin */ 461dd540b46SJohn Baldwin static void 462dd540b46SJohn Baldwin srat_set_cpus(void *dummy) 463dd540b46SJohn Baldwin { 464dd540b46SJohn Baldwin struct cpu_info *cpu; 465dd540b46SJohn Baldwin struct pcpu *pc; 466dd540b46SJohn Baldwin u_int i; 467dd540b46SJohn Baldwin 468dd540b46SJohn Baldwin if (srat_physaddr == 0) 469dd540b46SJohn Baldwin return; 470dd540b46SJohn Baldwin for (i = 0; i < MAXCPU; i++) { 471dd540b46SJohn Baldwin if (CPU_ABSENT(i)) 472dd540b46SJohn Baldwin continue; 473dd540b46SJohn Baldwin pc = pcpu_find(i); 474dd540b46SJohn Baldwin KASSERT(pc != NULL, ("no pcpu data for CPU %u", i)); 475dd540b46SJohn Baldwin cpu = &cpus[pc->pc_apic_id]; 476dd540b46SJohn Baldwin if (!cpu->enabled) 477dd540b46SJohn Baldwin panic("SRAT: CPU with APIC ID %u is not known", 478dd540b46SJohn Baldwin pc->pc_apic_id); 479dd540b46SJohn Baldwin pc->pc_domain = cpu->domain; 480c0ae6688SJohn Baldwin CPU_SET(i, &cpuset_domain[cpu->domain]); 481dd540b46SJohn Baldwin if (bootverbose) 482dd540b46SJohn Baldwin printf("SRAT: CPU %u has memory domain %d\n", i, 483dd540b46SJohn Baldwin cpu->domain); 484dd540b46SJohn Baldwin } 485dd540b46SJohn Baldwin } 486dd540b46SJohn Baldwin SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL); 487fc4f524aSAdrian Chadd 488fc4f524aSAdrian Chadd /* 489fc4f524aSAdrian Chadd * Map a _PXM value to a VM domain ID. 490fc4f524aSAdrian Chadd * 491fc4f524aSAdrian Chadd * Returns the domain ID, or -1 if no domain ID was found. 492fc4f524aSAdrian Chadd */ 493fc4f524aSAdrian Chadd int 494fc4f524aSAdrian Chadd acpi_map_pxm_to_vm_domainid(int pxm) 495fc4f524aSAdrian Chadd { 496fc4f524aSAdrian Chadd int i; 497fc4f524aSAdrian Chadd 498fc4f524aSAdrian Chadd for (i = 0; i < vm_ndomains; i++) { 499fc4f524aSAdrian Chadd if (vm_domains[i] == pxm) 500fc4f524aSAdrian Chadd return (i); 501fc4f524aSAdrian Chadd } 502fc4f524aSAdrian Chadd 503fc4f524aSAdrian Chadd return (-1); 504fc4f524aSAdrian Chadd } 505fc4f524aSAdrian Chadd 506941646f5SAttilio Rao #endif /* MAXMEMDOM > 1 */ 507