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 59*a3a61674SJayachandran C. /* 60*a3a61674SJayachandran C. * Parse and use proximity information in SRAT and SLIT. 61*a3a61674SJayachandran C. */ 62*a3a61674SJayachandran C. int acpi_pxm_init(int ncpus, vm_paddr_t maxphys); 63*a3a61674SJayachandran C. void acpi_pxm_parse_tables(void); 64*a3a61674SJayachandran C. void acpi_pxm_set_mem_locality(void); 65*a3a61674SJayachandran C. void acpi_pxm_set_cpu_locality(void); 66*a3a61674SJayachandran C. void acpi_pxm_free(void); 67*a3a61674SJayachandran C. 68941646f5SAttilio Rao #if MAXMEMDOM > 1 6984525e55SRoger Pau Monné static struct cpu_info { 70dd540b46SJohn Baldwin int enabled:1; 71dd540b46SJohn Baldwin int has_memory:1; 72dd540b46SJohn Baldwin int domain; 7384525e55SRoger Pau Monné } *cpus; 74dd540b46SJohn Baldwin 75*a3a61674SJayachandran C. static int max_cpus; 76*a3a61674SJayachandran C. static int last_cpu; 77*a3a61674SJayachandran C. 78dd540b46SJohn Baldwin struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1]; 79dd540b46SJohn Baldwin int num_mem; 80dd540b46SJohn Baldwin 81dd540b46SJohn Baldwin static ACPI_TABLE_SRAT *srat; 82dd540b46SJohn Baldwin static vm_paddr_t srat_physaddr; 83dd540b46SJohn Baldwin 8462d70a81SJohn Baldwin static int domain_pxm[MAXMEMDOM]; 8562d70a81SJohn Baldwin static int ndomain; 86*a3a61674SJayachandran C. static vm_paddr_t maxphyaddr; 87fc4f524aSAdrian Chadd 88415d7ccaSAdrian Chadd static ACPI_TABLE_SLIT *slit; 89415d7ccaSAdrian Chadd static vm_paddr_t slit_physaddr; 90415d7ccaSAdrian Chadd static int vm_locality_table[MAXMEMDOM * MAXMEMDOM]; 91415d7ccaSAdrian Chadd 92dd540b46SJohn Baldwin static void srat_walk_table(acpi_subtable_handler *handler, void *arg); 93dd540b46SJohn Baldwin 944d99cfb3SJohn Baldwin /* 95415d7ccaSAdrian Chadd * SLIT parsing. 96415d7ccaSAdrian Chadd */ 97415d7ccaSAdrian Chadd 98415d7ccaSAdrian Chadd static void 99415d7ccaSAdrian Chadd slit_parse_table(ACPI_TABLE_SLIT *s) 100415d7ccaSAdrian Chadd { 101415d7ccaSAdrian Chadd int i, j; 102415d7ccaSAdrian Chadd int i_domain, j_domain; 103415d7ccaSAdrian Chadd int offset = 0; 104415d7ccaSAdrian Chadd uint8_t e; 105415d7ccaSAdrian Chadd 106415d7ccaSAdrian Chadd /* 107415d7ccaSAdrian Chadd * This maps the SLIT data into the VM-domain centric view. 108415d7ccaSAdrian Chadd * There may be sparse entries in the PXM namespace, so 109415d7ccaSAdrian Chadd * remap them to a VM-domain ID and if it doesn't exist, 110415d7ccaSAdrian Chadd * skip it. 111415d7ccaSAdrian Chadd * 112415d7ccaSAdrian Chadd * It should result in a packed 2d array of VM-domain 113415d7ccaSAdrian Chadd * locality information entries. 114415d7ccaSAdrian Chadd */ 115415d7ccaSAdrian Chadd 116415d7ccaSAdrian Chadd if (bootverbose) 117415d7ccaSAdrian Chadd printf("SLIT.Localities: %d\n", (int) s->LocalityCount); 118415d7ccaSAdrian Chadd for (i = 0; i < s->LocalityCount; i++) { 119415d7ccaSAdrian Chadd i_domain = acpi_map_pxm_to_vm_domainid(i); 120415d7ccaSAdrian Chadd if (i_domain < 0) 121415d7ccaSAdrian Chadd continue; 122415d7ccaSAdrian Chadd 123415d7ccaSAdrian Chadd if (bootverbose) 124415d7ccaSAdrian Chadd printf("%d: ", i); 125415d7ccaSAdrian Chadd for (j = 0; j < s->LocalityCount; j++) { 126415d7ccaSAdrian Chadd j_domain = acpi_map_pxm_to_vm_domainid(j); 127415d7ccaSAdrian Chadd if (j_domain < 0) 128415d7ccaSAdrian Chadd continue; 129415d7ccaSAdrian Chadd e = s->Entry[i * s->LocalityCount + j]; 130415d7ccaSAdrian Chadd if (bootverbose) 131415d7ccaSAdrian Chadd printf("%d ", (int) e); 132415d7ccaSAdrian Chadd /* 255 == "no locality information" */ 133415d7ccaSAdrian Chadd if (e == 255) 134415d7ccaSAdrian Chadd vm_locality_table[offset] = -1; 135415d7ccaSAdrian Chadd else 136415d7ccaSAdrian Chadd vm_locality_table[offset] = e; 137415d7ccaSAdrian Chadd offset++; 138415d7ccaSAdrian Chadd } 139415d7ccaSAdrian Chadd if (bootverbose) 140415d7ccaSAdrian Chadd printf("\n"); 141415d7ccaSAdrian Chadd } 142415d7ccaSAdrian Chadd } 143415d7ccaSAdrian Chadd 144415d7ccaSAdrian Chadd /* 145415d7ccaSAdrian Chadd * Look for an ACPI System Locality Distance Information Table ("SLIT") 146415d7ccaSAdrian Chadd */ 147415d7ccaSAdrian Chadd static int 148415d7ccaSAdrian Chadd parse_slit(void) 149415d7ccaSAdrian Chadd { 150415d7ccaSAdrian Chadd 151415d7ccaSAdrian Chadd if (resource_disabled("slit", 0)) { 152415d7ccaSAdrian Chadd return (-1); 153415d7ccaSAdrian Chadd } 154415d7ccaSAdrian Chadd 155415d7ccaSAdrian Chadd slit_physaddr = acpi_find_table(ACPI_SIG_SLIT); 156415d7ccaSAdrian Chadd if (slit_physaddr == 0) { 157415d7ccaSAdrian Chadd return (-1); 158415d7ccaSAdrian Chadd } 159415d7ccaSAdrian Chadd 160415d7ccaSAdrian Chadd /* 161415d7ccaSAdrian Chadd * Make a pass over the table to populate the cpus[] and 162415d7ccaSAdrian Chadd * mem_info[] tables. 163415d7ccaSAdrian Chadd */ 164415d7ccaSAdrian Chadd slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT); 165415d7ccaSAdrian Chadd slit_parse_table(slit); 166415d7ccaSAdrian Chadd acpi_unmap_table(slit); 167415d7ccaSAdrian Chadd slit = NULL; 168415d7ccaSAdrian Chadd 169415d7ccaSAdrian Chadd return (0); 170415d7ccaSAdrian Chadd } 171415d7ccaSAdrian Chadd 172415d7ccaSAdrian Chadd /* 173415d7ccaSAdrian Chadd * SRAT parsing. 174415d7ccaSAdrian Chadd */ 175415d7ccaSAdrian Chadd 176415d7ccaSAdrian Chadd /* 1774d99cfb3SJohn Baldwin * Returns true if a memory range overlaps with at least one range in 1784d99cfb3SJohn Baldwin * phys_avail[]. 1794d99cfb3SJohn Baldwin */ 1804d99cfb3SJohn Baldwin static int 1814d99cfb3SJohn Baldwin overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end) 1824d99cfb3SJohn Baldwin { 1834d99cfb3SJohn Baldwin int i; 1844d99cfb3SJohn Baldwin 1854d99cfb3SJohn Baldwin for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) { 186ffc7e53aSAlexander Motin if (phys_avail[i + 1] <= start) 1874d99cfb3SJohn Baldwin continue; 1884d99cfb3SJohn Baldwin if (phys_avail[i] < end) 1894d99cfb3SJohn Baldwin return (1); 1904d99cfb3SJohn Baldwin break; 1914d99cfb3SJohn Baldwin } 1924d99cfb3SJohn Baldwin return (0); 193*a3a61674SJayachandran C. } 1944d99cfb3SJohn Baldwin 195*a3a61674SJayachandran C. /* 196*a3a61674SJayachandran C. * Find CPU by processor ID (APIC ID on x86). 197*a3a61674SJayachandran C. */ 198*a3a61674SJayachandran C. static struct cpu_info * 199*a3a61674SJayachandran C. cpu_find(int cpuid) 200*a3a61674SJayachandran C. { 201*a3a61674SJayachandran C. 202*a3a61674SJayachandran C. if (cpuid <= last_cpu && cpus[cpuid].enabled) 203*a3a61674SJayachandran C. return (&cpus[cpuid]); 204*a3a61674SJayachandran C. return (NULL); 205*a3a61674SJayachandran C. } 206*a3a61674SJayachandran C. 207*a3a61674SJayachandran C. /* 208*a3a61674SJayachandran C. * Find CPU by pcpu pointer. 209*a3a61674SJayachandran C. */ 210*a3a61674SJayachandran C. static struct cpu_info * 211*a3a61674SJayachandran C. cpu_get_info(struct pcpu *pc) 212*a3a61674SJayachandran C. { 213*a3a61674SJayachandran C. struct cpu_info *cpup; 214*a3a61674SJayachandran C. int id; 215*a3a61674SJayachandran C. 216*a3a61674SJayachandran C. id = pc->pc_apic_id; 217*a3a61674SJayachandran C. cpup = cpu_find(id); 218*a3a61674SJayachandran C. if (cpup == NULL) 219*a3a61674SJayachandran C. panic("SRAT: CPU with APIC ID %u is not known", id); 220*a3a61674SJayachandran C. return (cpup); 221*a3a61674SJayachandran C. } 222*a3a61674SJayachandran C. 223*a3a61674SJayachandran C. /* 224*a3a61674SJayachandran C. * Add proximity information for a new CPU. 225*a3a61674SJayachandran C. */ 226*a3a61674SJayachandran C. static struct cpu_info * 227*a3a61674SJayachandran C. cpu_add(int cpuid, int domain) 228*a3a61674SJayachandran C. { 229*a3a61674SJayachandran C. struct cpu_info *cpup; 230*a3a61674SJayachandran C. 231*a3a61674SJayachandran C. if (cpuid >= max_cpus) 232*a3a61674SJayachandran C. return (NULL); 233*a3a61674SJayachandran C. last_cpu = imax(last_cpu, cpuid); 234*a3a61674SJayachandran C. cpup = &cpus[cpuid]; 235*a3a61674SJayachandran C. cpup->domain = domain; 236*a3a61674SJayachandran C. cpup->enabled = 1; 237*a3a61674SJayachandran C. return (cpup); 2384d99cfb3SJohn Baldwin } 2394d99cfb3SJohn Baldwin 240dd540b46SJohn Baldwin static void 241dd540b46SJohn Baldwin srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg) 242dd540b46SJohn Baldwin { 243dd540b46SJohn Baldwin ACPI_SRAT_CPU_AFFINITY *cpu; 244dd540b46SJohn Baldwin ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic; 245dd540b46SJohn Baldwin ACPI_SRAT_MEM_AFFINITY *mem; 246*a3a61674SJayachandran C. static struct cpu_info *cpup; 247dd540b46SJohn Baldwin int domain, i, slot; 248dd540b46SJohn Baldwin 249dd540b46SJohn Baldwin switch (entry->Type) { 250dd540b46SJohn Baldwin case ACPI_SRAT_TYPE_CPU_AFFINITY: 251dd540b46SJohn Baldwin cpu = (ACPI_SRAT_CPU_AFFINITY *)entry; 252dd540b46SJohn Baldwin domain = cpu->ProximityDomainLo | 253dd540b46SJohn Baldwin cpu->ProximityDomainHi[0] << 8 | 254dd540b46SJohn Baldwin cpu->ProximityDomainHi[1] << 16 | 255dd540b46SJohn Baldwin cpu->ProximityDomainHi[2] << 24; 256dd540b46SJohn Baldwin if (bootverbose) 257dd540b46SJohn Baldwin printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 258dd540b46SJohn Baldwin cpu->ApicId, domain, 259dd540b46SJohn Baldwin (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ? 260dd540b46SJohn Baldwin "enabled" : "disabled"); 261dd540b46SJohn Baldwin if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED)) 262dd540b46SJohn Baldwin break; 263*a3a61674SJayachandran C. cpup = cpu_find(cpu->ApicId); 264*a3a61674SJayachandran C. if (cpup != NULL) { 2652db0699dSEric van Gyzen printf("SRAT: Duplicate local APIC ID %u\n", 2662db0699dSEric van Gyzen cpu->ApicId); 2672db0699dSEric van Gyzen *(int *)arg = ENXIO; 2682db0699dSEric van Gyzen break; 2692db0699dSEric van Gyzen } 270*a3a61674SJayachandran C. cpup = cpu_add(cpu->ApicId, domain); 271*a3a61674SJayachandran C. if (cpup == NULL) 272*a3a61674SJayachandran C. printf("SRAT: Ignoring local APIC ID %u (too high)\n", 273*a3a61674SJayachandran C. cpu->ApicId); 274dd540b46SJohn Baldwin break; 275dd540b46SJohn Baldwin case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: 276dd540b46SJohn Baldwin x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry; 277dd540b46SJohn Baldwin if (bootverbose) 278dd540b46SJohn Baldwin printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 279dd540b46SJohn Baldwin x2apic->ApicId, x2apic->ProximityDomain, 280dd540b46SJohn Baldwin (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ? 281dd540b46SJohn Baldwin "enabled" : "disabled"); 282dd540b46SJohn Baldwin if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED)) 283dd540b46SJohn Baldwin break; 284*a3a61674SJayachandran C. KASSERT(cpu_find(x2apic->ApicId) == NULL, 285*a3a61674SJayachandran C. ("Duplicate local APIC ID %u", x2apic->ApicId)); 286*a3a61674SJayachandran C. cpup = cpu_add(x2apic->ApicId, x2apic->ProximityDomain); 287*a3a61674SJayachandran C. if (cpup == NULL) 2886e2ab0aeSRoger Pau Monné printf("SRAT: Ignoring local APIC ID %u (too high)\n", 2896e2ab0aeSRoger Pau Monné x2apic->ApicId); 2906e2ab0aeSRoger Pau Monné break; 291dd540b46SJohn Baldwin case ACPI_SRAT_TYPE_MEMORY_AFFINITY: 292dd540b46SJohn Baldwin mem = (ACPI_SRAT_MEM_AFFINITY *)entry; 293dd540b46SJohn Baldwin if (bootverbose) 294dd540b46SJohn Baldwin printf( 2955ab0f0c3SKonstantin Belousov "SRAT: Found memory domain %d addr 0x%jx len 0x%jx: %s\n", 296dd540b46SJohn Baldwin mem->ProximityDomain, (uintmax_t)mem->BaseAddress, 297dd540b46SJohn Baldwin (uintmax_t)mem->Length, 298dd540b46SJohn Baldwin (mem->Flags & ACPI_SRAT_MEM_ENABLED) ? 299dd540b46SJohn Baldwin "enabled" : "disabled"); 300dd540b46SJohn Baldwin if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED)) 301dd540b46SJohn Baldwin break; 302*a3a61674SJayachandran C. if (mem->BaseAddress >= maxphyaddr || 3033f289c3fSJeff Roberson !overlaps_phys_avail(mem->BaseAddress, 3044d99cfb3SJohn Baldwin mem->BaseAddress + mem->Length)) { 3055ab0f0c3SKonstantin Belousov printf("SRAT: Ignoring memory at addr 0x%jx\n", 3064d99cfb3SJohn Baldwin (uintmax_t)mem->BaseAddress); 3074d99cfb3SJohn Baldwin break; 3084d99cfb3SJohn Baldwin } 309dd540b46SJohn Baldwin if (num_mem == VM_PHYSSEG_MAX) { 310dd540b46SJohn Baldwin printf("SRAT: Too many memory regions\n"); 311dd540b46SJohn Baldwin *(int *)arg = ENXIO; 312dd540b46SJohn Baldwin break; 313dd540b46SJohn Baldwin } 314dd540b46SJohn Baldwin slot = num_mem; 315dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 316dd540b46SJohn Baldwin if (mem_info[i].end <= mem->BaseAddress) 317dd540b46SJohn Baldwin continue; 318dd540b46SJohn Baldwin if (mem_info[i].start < 319dd540b46SJohn Baldwin (mem->BaseAddress + mem->Length)) { 320dd540b46SJohn Baldwin printf("SRAT: Overlapping memory entries\n"); 321dd540b46SJohn Baldwin *(int *)arg = ENXIO; 322dd540b46SJohn Baldwin return; 323dd540b46SJohn Baldwin } 324dd540b46SJohn Baldwin slot = i; 325dd540b46SJohn Baldwin } 326dd540b46SJohn Baldwin for (i = num_mem; i > slot; i--) 327dd540b46SJohn Baldwin mem_info[i] = mem_info[i - 1]; 328dd540b46SJohn Baldwin mem_info[slot].start = mem->BaseAddress; 329dd540b46SJohn Baldwin mem_info[slot].end = mem->BaseAddress + mem->Length; 330dd540b46SJohn Baldwin mem_info[slot].domain = mem->ProximityDomain; 331dd540b46SJohn Baldwin num_mem++; 332dd540b46SJohn Baldwin break; 333dd540b46SJohn Baldwin } 334dd540b46SJohn Baldwin } 335dd540b46SJohn Baldwin 336dd540b46SJohn Baldwin /* 337dd540b46SJohn Baldwin * Ensure each memory domain has at least one CPU and that each CPU 338dd540b46SJohn Baldwin * has at least one memory domain. 339dd540b46SJohn Baldwin */ 340dd540b46SJohn Baldwin static int 341dd540b46SJohn Baldwin check_domains(void) 342dd540b46SJohn Baldwin { 343dd540b46SJohn Baldwin int found, i, j; 344dd540b46SJohn Baldwin 345dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 346dd540b46SJohn Baldwin found = 0; 347*a3a61674SJayachandran C. for (j = 0; j <= last_cpu; j++) 3486676877bSJohn Baldwin if (cpus[j].enabled && 3496676877bSJohn Baldwin cpus[j].domain == mem_info[i].domain) { 350dd540b46SJohn Baldwin cpus[j].has_memory = 1; 351dd540b46SJohn Baldwin found++; 352dd540b46SJohn Baldwin } 353dd540b46SJohn Baldwin if (!found) { 354dd540b46SJohn Baldwin printf("SRAT: No CPU found for memory domain %d\n", 355dd540b46SJohn Baldwin mem_info[i].domain); 356dd540b46SJohn Baldwin return (ENXIO); 357dd540b46SJohn Baldwin } 358dd540b46SJohn Baldwin } 359*a3a61674SJayachandran C. for (i = 0; i <= last_cpu; i++) 360dd540b46SJohn Baldwin if (cpus[i].enabled && !cpus[i].has_memory) { 36130c5525bSAndrew Gallatin found = 0; 36230c5525bSAndrew Gallatin for (j = 0; j < num_mem && !found; j++) { 36330c5525bSAndrew Gallatin if (mem_info[j].domain == cpus[i].domain) 36430c5525bSAndrew Gallatin found = 1; 36530c5525bSAndrew Gallatin } 36630c5525bSAndrew Gallatin if (!found) { 36730c5525bSAndrew Gallatin if (bootverbose) 36830c5525bSAndrew Gallatin printf("SRAT: mem dom %d is empty\n", 36930c5525bSAndrew Gallatin cpus[i].domain); 37030c5525bSAndrew Gallatin mem_info[num_mem].start = 0; 37130c5525bSAndrew Gallatin mem_info[num_mem].end = 0; 37230c5525bSAndrew Gallatin mem_info[num_mem].domain = cpus[i].domain; 37330c5525bSAndrew Gallatin num_mem++; 37430c5525bSAndrew Gallatin } 375dd540b46SJohn Baldwin } 376dd540b46SJohn Baldwin return (0); 377dd540b46SJohn Baldwin } 378dd540b46SJohn Baldwin 379dd540b46SJohn Baldwin /* 380dd540b46SJohn Baldwin * Check that the SRAT memory regions cover all of the regions in 381dd540b46SJohn Baldwin * phys_avail[]. 382dd540b46SJohn Baldwin */ 383dd540b46SJohn Baldwin static int 384dd540b46SJohn Baldwin check_phys_avail(void) 385dd540b46SJohn Baldwin { 386dd540b46SJohn Baldwin vm_paddr_t address; 387dd540b46SJohn Baldwin int i, j; 388dd540b46SJohn Baldwin 389dd540b46SJohn Baldwin /* j is the current offset into phys_avail[]. */ 390dd540b46SJohn Baldwin address = phys_avail[0]; 391dd540b46SJohn Baldwin j = 0; 392dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 393dd540b46SJohn Baldwin /* 394dd540b46SJohn Baldwin * Consume as many phys_avail[] entries as fit in this 395dd540b46SJohn Baldwin * region. 396dd540b46SJohn Baldwin */ 397dd540b46SJohn Baldwin while (address >= mem_info[i].start && 398dd540b46SJohn Baldwin address <= mem_info[i].end) { 399dd540b46SJohn Baldwin /* 400dd540b46SJohn Baldwin * If we cover the rest of this phys_avail[] entry, 401dd540b46SJohn Baldwin * advance to the next entry. 402dd540b46SJohn Baldwin */ 403dd540b46SJohn Baldwin if (phys_avail[j + 1] <= mem_info[i].end) { 404dd540b46SJohn Baldwin j += 2; 405dd540b46SJohn Baldwin if (phys_avail[j] == 0 && 406dd540b46SJohn Baldwin phys_avail[j + 1] == 0) { 407dd540b46SJohn Baldwin return (0); 408dd540b46SJohn Baldwin } 409dd540b46SJohn Baldwin address = phys_avail[j]; 410dd540b46SJohn Baldwin } else 411dd540b46SJohn Baldwin address = mem_info[i].end + 1; 412dd540b46SJohn Baldwin } 413dd540b46SJohn Baldwin } 4145ab0f0c3SKonstantin Belousov printf("SRAT: No memory region found for 0x%jx - 0x%jx\n", 415dd540b46SJohn Baldwin (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]); 416dd540b46SJohn Baldwin return (ENXIO); 417dd540b46SJohn Baldwin } 418dd540b46SJohn Baldwin 419dd540b46SJohn Baldwin /* 420dd540b46SJohn Baldwin * Renumber the memory domains to be compact and zero-based if not 42128990874SJohn Baldwin * already. Returns an error if there are too many domains. 422dd540b46SJohn Baldwin */ 42328990874SJohn Baldwin static int 424dd540b46SJohn Baldwin renumber_domains(void) 425dd540b46SJohn Baldwin { 4267e226537SAttilio Rao int i, j, slot; 427dd540b46SJohn Baldwin 428dd540b46SJohn Baldwin /* Enumerate all the domains. */ 42962d70a81SJohn Baldwin ndomain = 0; 430dd540b46SJohn Baldwin for (i = 0; i < num_mem; i++) { 431dd540b46SJohn Baldwin /* See if this domain is already known. */ 43262d70a81SJohn Baldwin for (j = 0; j < ndomain; j++) { 43362d70a81SJohn Baldwin if (domain_pxm[j] >= mem_info[i].domain) 434dd540b46SJohn Baldwin break; 435dd540b46SJohn Baldwin } 43662d70a81SJohn Baldwin if (j < ndomain && domain_pxm[j] == mem_info[i].domain) 437dd540b46SJohn Baldwin continue; 438dd540b46SJohn Baldwin 4393765b809SConrad Meyer if (ndomain >= MAXMEMDOM) { 4403765b809SConrad Meyer ndomain = 1; 4413765b809SConrad Meyer printf("SRAT: Too many memory domains\n"); 4423765b809SConrad Meyer return (EFBIG); 4433765b809SConrad Meyer } 4443765b809SConrad Meyer 445dd540b46SJohn Baldwin /* Insert the new domain at slot 'j'. */ 446dd540b46SJohn Baldwin slot = j; 44762d70a81SJohn Baldwin for (j = ndomain; j > slot; j--) 44862d70a81SJohn Baldwin domain_pxm[j] = domain_pxm[j - 1]; 44962d70a81SJohn Baldwin domain_pxm[slot] = mem_info[i].domain; 45062d70a81SJohn Baldwin ndomain++; 451dd540b46SJohn Baldwin } 452dd540b46SJohn Baldwin 45362d70a81SJohn Baldwin /* Renumber each domain to its index in the sorted 'domain_pxm' list. */ 45462d70a81SJohn Baldwin for (i = 0; i < ndomain; i++) { 455dd540b46SJohn Baldwin /* 456dd540b46SJohn Baldwin * If the domain is already the right value, no need 457dd540b46SJohn Baldwin * to renumber. 458dd540b46SJohn Baldwin */ 45962d70a81SJohn Baldwin if (domain_pxm[i] == i) 460dd540b46SJohn Baldwin continue; 461dd540b46SJohn Baldwin 462dd540b46SJohn Baldwin /* Walk the cpu[] and mem_info[] arrays to renumber. */ 463dd540b46SJohn Baldwin for (j = 0; j < num_mem; j++) 46462d70a81SJohn Baldwin if (mem_info[j].domain == domain_pxm[i]) 465dd540b46SJohn Baldwin mem_info[j].domain = i; 466*a3a61674SJayachandran C. for (j = 0; j <= last_cpu; j++) 46762d70a81SJohn Baldwin if (cpus[j].enabled && cpus[j].domain == domain_pxm[i]) 468dd540b46SJohn Baldwin cpus[j].domain = i; 469dd540b46SJohn Baldwin } 4707e226537SAttilio Rao 47128990874SJohn Baldwin return (0); 472dd540b46SJohn Baldwin } 473dd540b46SJohn Baldwin 474dd540b46SJohn Baldwin /* 475*a3a61674SJayachandran C. * Look for an ACPI System Resource Affinity Table ("SRAT"), 476*a3a61674SJayachandran C. * allocate space for cpu information, and initialize globals. 477dd540b46SJohn Baldwin */ 478*a3a61674SJayachandran C. int 479*a3a61674SJayachandran C. acpi_pxm_init(int ncpus, vm_paddr_t maxphys) 480dd540b46SJohn Baldwin { 48184525e55SRoger Pau Monné unsigned int idx, size; 48284525e55SRoger Pau Monné vm_paddr_t addr; 483dd540b46SJohn Baldwin 484dd540b46SJohn Baldwin if (resource_disabled("srat", 0)) 485415d7ccaSAdrian Chadd return (-1); 486dd540b46SJohn Baldwin 487*a3a61674SJayachandran C. max_cpus = ncpus; 488*a3a61674SJayachandran C. last_cpu = -1; 489*a3a61674SJayachandran C. maxphyaddr = maxphys; 490dd540b46SJohn Baldwin srat_physaddr = acpi_find_table(ACPI_SIG_SRAT); 491dd540b46SJohn Baldwin if (srat_physaddr == 0) 492415d7ccaSAdrian Chadd return (-1); 493dd540b46SJohn Baldwin 494dd540b46SJohn Baldwin /* 49584525e55SRoger Pau Monné * Allocate data structure: 49684525e55SRoger Pau Monné * 49784525e55SRoger Pau Monné * Find the last physical memory region and steal some memory from 49884525e55SRoger Pau Monné * it. This is done because at this point in the boot process 49984525e55SRoger Pau Monné * malloc is still not usable. 50084525e55SRoger Pau Monné */ 50184525e55SRoger Pau Monné for (idx = 0; phys_avail[idx + 1] != 0; idx += 2); 50284525e55SRoger Pau Monné KASSERT(idx != 0, ("phys_avail is empty!")); 50384525e55SRoger Pau Monné idx -= 2; 50484525e55SRoger Pau Monné 505*a3a61674SJayachandran C. size = sizeof(*cpus) * max_cpus; 50684525e55SRoger Pau Monné addr = trunc_page(phys_avail[idx + 1] - size); 50784525e55SRoger Pau Monné KASSERT(addr >= phys_avail[idx], 50884525e55SRoger Pau Monné ("Not enough memory for SRAT table items")); 50984525e55SRoger Pau Monné phys_avail[idx + 1] = addr - 1; 51084525e55SRoger Pau Monné 511c642d2f5SRoger Pau Monné /* 512c642d2f5SRoger Pau Monné * We cannot rely on PHYS_TO_DMAP because this code is also used in 513c642d2f5SRoger Pau Monné * i386, so use pmap_mapbios to map the memory, this will end up using 514c642d2f5SRoger Pau Monné * the default memory attribute (WB), and the DMAP when available. 515c642d2f5SRoger Pau Monné */ 516c642d2f5SRoger Pau Monné cpus = (struct cpu_info *)pmap_mapbios(addr, size); 51745ff071dSRoger Pau Monné bzero(cpus, size); 518*a3a61674SJayachandran C. return (0); 519*a3a61674SJayachandran C. } 520*a3a61674SJayachandran C. 521*a3a61674SJayachandran C. static int 522*a3a61674SJayachandran C. parse_srat(void) 523*a3a61674SJayachandran C. { 524*a3a61674SJayachandran C. int error; 52584525e55SRoger Pau Monné 52684525e55SRoger Pau Monné /* 527dd540b46SJohn Baldwin * Make a pass over the table to populate the cpus[] and 528dd540b46SJohn Baldwin * mem_info[] tables. 529dd540b46SJohn Baldwin */ 530dd540b46SJohn Baldwin srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT); 531dd540b46SJohn Baldwin error = 0; 532dd540b46SJohn Baldwin srat_walk_table(srat_parse_entry, &error); 533dd540b46SJohn Baldwin acpi_unmap_table(srat); 534dd540b46SJohn Baldwin srat = NULL; 53528990874SJohn Baldwin if (error || check_domains() != 0 || check_phys_avail() != 0 || 53628990874SJohn Baldwin renumber_domains() != 0) { 537dd540b46SJohn Baldwin srat_physaddr = 0; 538415d7ccaSAdrian Chadd return (-1); 539dd540b46SJohn Baldwin } 540dd540b46SJohn Baldwin 541415d7ccaSAdrian Chadd return (0); 542dd540b46SJohn Baldwin } 543415d7ccaSAdrian Chadd 544415d7ccaSAdrian Chadd static void 545415d7ccaSAdrian Chadd init_mem_locality(void) 546415d7ccaSAdrian Chadd { 547415d7ccaSAdrian Chadd int i; 548415d7ccaSAdrian Chadd 549415d7ccaSAdrian Chadd /* 5504f5e270aSAdrian Chadd * For now, assume -1 == "no locality information for 551415d7ccaSAdrian Chadd * this pairing. 552415d7ccaSAdrian Chadd */ 553415d7ccaSAdrian Chadd for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++) 554415d7ccaSAdrian Chadd vm_locality_table[i] = -1; 555415d7ccaSAdrian Chadd } 556415d7ccaSAdrian Chadd 557*a3a61674SJayachandran C. /* 558*a3a61674SJayachandran C. * Parse SRAT and SLIT to save proximity info. Don't do 559*a3a61674SJayachandran C. * anything if SRAT is not available. 560*a3a61674SJayachandran C. */ 561*a3a61674SJayachandran C. void 562*a3a61674SJayachandran C. acpi_pxm_parse_tables(void) 563415d7ccaSAdrian Chadd { 564415d7ccaSAdrian Chadd 565*a3a61674SJayachandran C. if (srat_physaddr == 0) 566*a3a61674SJayachandran C. return; 567415d7ccaSAdrian Chadd if (parse_srat() < 0) 568415d7ccaSAdrian Chadd return; 569415d7ccaSAdrian Chadd init_mem_locality(); 570415d7ccaSAdrian Chadd (void)parse_slit(); 571*a3a61674SJayachandran C. } 572*a3a61674SJayachandran C. 573*a3a61674SJayachandran C. /* 574*a3a61674SJayachandran C. * Use saved data from SRAT/SLIT to update memory locality. 575*a3a61674SJayachandran C. */ 576*a3a61674SJayachandran C. void 577*a3a61674SJayachandran C. acpi_pxm_set_mem_locality(void) 578*a3a61674SJayachandran C. { 579*a3a61674SJayachandran C. 580*a3a61674SJayachandran C. if (srat_physaddr == 0) 581*a3a61674SJayachandran C. return; 582662e7fa8SMark Johnston vm_phys_register_domains(ndomain, mem_info, vm_locality_table); 583415d7ccaSAdrian Chadd } 584*a3a61674SJayachandran C. 585*a3a61674SJayachandran C. static void 586*a3a61674SJayachandran C. parse_acpi_tables(void *dummy) 587*a3a61674SJayachandran C. { 588*a3a61674SJayachandran C. 589*a3a61674SJayachandran C. acpi_pxm_init(max_apic_id + 1, cpu_getmaxphyaddr()); 590*a3a61674SJayachandran C. acpi_pxm_parse_tables(); 591*a3a61674SJayachandran C. acpi_pxm_set_mem_locality(); 592*a3a61674SJayachandran C. } 593415d7ccaSAdrian Chadd SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables, 594415d7ccaSAdrian Chadd NULL); 595dd540b46SJohn Baldwin 596dd540b46SJohn Baldwin static void 597dd540b46SJohn Baldwin srat_walk_table(acpi_subtable_handler *handler, void *arg) 598dd540b46SJohn Baldwin { 599dd540b46SJohn Baldwin 600dd540b46SJohn Baldwin acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length, 601dd540b46SJohn Baldwin handler, arg); 602dd540b46SJohn Baldwin } 603dd540b46SJohn Baldwin 604dd540b46SJohn Baldwin /* 605*a3a61674SJayachandran C. * Setup per-CPU domain IDs from information saved in 'cpus'. 606dd540b46SJohn Baldwin */ 607*a3a61674SJayachandran C. void 608*a3a61674SJayachandran C. acpi_pxm_set_cpu_locality(void) 609dd540b46SJohn Baldwin { 610dd540b46SJohn Baldwin struct cpu_info *cpu; 611dd540b46SJohn Baldwin struct pcpu *pc; 612dd540b46SJohn Baldwin u_int i; 613dd540b46SJohn Baldwin 614dd540b46SJohn Baldwin if (srat_physaddr == 0) 615dd540b46SJohn Baldwin return; 616dd540b46SJohn Baldwin for (i = 0; i < MAXCPU; i++) { 617dd540b46SJohn Baldwin if (CPU_ABSENT(i)) 618dd540b46SJohn Baldwin continue; 619dd540b46SJohn Baldwin pc = pcpu_find(i); 620dd540b46SJohn Baldwin KASSERT(pc != NULL, ("no pcpu data for CPU %u", i)); 621*a3a61674SJayachandran C. cpu = cpu_get_info(pc); 622b61f3142SMark Johnston pc->pc_domain = vm_ndomains > 1 ? cpu->domain : 0; 623ab3059a8SMatt Macy CPU_SET(i, &cpuset_domain[pc->pc_domain]); 624dd540b46SJohn Baldwin if (bootverbose) 625dd540b46SJohn Baldwin printf("SRAT: CPU %u has memory domain %d\n", i, 626ab3059a8SMatt Macy pc->pc_domain); 627dd540b46SJohn Baldwin } 628*a3a61674SJayachandran C. } 629c642d2f5SRoger Pau Monné 630*a3a61674SJayachandran C. /* 631*a3a61674SJayachandran C. * Free data structures allocated during acpi_pxm_init. 632*a3a61674SJayachandran C. */ 633*a3a61674SJayachandran C. void 634*a3a61674SJayachandran C. acpi_pxm_free(void) 635*a3a61674SJayachandran C. { 636*a3a61674SJayachandran C. 637*a3a61674SJayachandran C. if (srat_physaddr == 0) 638*a3a61674SJayachandran C. return; 639*a3a61674SJayachandran C. pmap_unmapbios((vm_offset_t)cpus, sizeof(*cpus) * max_cpus); 640*a3a61674SJayachandran C. srat_physaddr = 0; 641c642d2f5SRoger Pau Monné cpus = NULL; 642dd540b46SJohn Baldwin } 643*a3a61674SJayachandran C. 644*a3a61674SJayachandran C. static void 645*a3a61674SJayachandran C. srat_set_cpus(void *dummy) 646*a3a61674SJayachandran C. { 647*a3a61674SJayachandran C. 648*a3a61674SJayachandran C. acpi_pxm_set_cpu_locality(); 649*a3a61674SJayachandran C. acpi_pxm_free(); 650*a3a61674SJayachandran C. } 651dd540b46SJohn Baldwin SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL); 652fc4f524aSAdrian Chadd 653fc4f524aSAdrian Chadd /* 654fc4f524aSAdrian Chadd * Map a _PXM value to a VM domain ID. 655fc4f524aSAdrian Chadd * 656fc4f524aSAdrian Chadd * Returns the domain ID, or -1 if no domain ID was found. 657fc4f524aSAdrian Chadd */ 658fc4f524aSAdrian Chadd int 659fc4f524aSAdrian Chadd acpi_map_pxm_to_vm_domainid(int pxm) 660fc4f524aSAdrian Chadd { 661fc4f524aSAdrian Chadd int i; 662fc4f524aSAdrian Chadd 66362d70a81SJohn Baldwin for (i = 0; i < ndomain; i++) { 66462d70a81SJohn Baldwin if (domain_pxm[i] == pxm) 665b61f3142SMark Johnston return (vm_ndomains > 1 ? i : 0); 666fc4f524aSAdrian Chadd } 667fc4f524aSAdrian Chadd 668fc4f524aSAdrian Chadd return (-1); 669fc4f524aSAdrian Chadd } 670fc4f524aSAdrian Chadd 67162d70a81SJohn Baldwin #else /* MAXMEMDOM == 1 */ 67262d70a81SJohn Baldwin 67362d70a81SJohn Baldwin int 67462d70a81SJohn Baldwin acpi_map_pxm_to_vm_domainid(int pxm) 67562d70a81SJohn Baldwin { 67662d70a81SJohn Baldwin 67762d70a81SJohn Baldwin return (-1); 67862d70a81SJohn Baldwin } 67962d70a81SJohn Baldwin 680941646f5SAttilio Rao #endif /* MAXMEMDOM > 1 */ 681