srat.c revision 299485
1/*- 2 * Copyright (c) 2010 Hudson River Trading LLC 3 * Written by: John H. Baldwin <jhb@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: stable/10/sys/x86/acpica/srat.c 299485 2016-05-11 22:06:28Z vangyzen $"); 30 31#include <sys/param.h> 32#include <sys/bus.h> 33#include <sys/kernel.h> 34#include <sys/lock.h> 35#include <sys/mutex.h> 36#include <sys/smp.h> 37#include <sys/vmmeter.h> 38#include <vm/vm.h> 39#include <vm/pmap.h> 40#include <vm/vm_param.h> 41#include <vm/vm_page.h> 42#include <vm/vm_phys.h> 43 44#include <contrib/dev/acpica/include/acpi.h> 45#include <contrib/dev/acpica/include/actables.h> 46 47#include <machine/intr_machdep.h> 48#include <machine/apicvar.h> 49 50#include <dev/acpica/acpivar.h> 51 52#if MAXMEMDOM > 1 53struct cpu_info { 54 int enabled:1; 55 int has_memory:1; 56 int domain; 57} cpus[MAX_APIC_ID + 1]; 58 59struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1]; 60int num_mem; 61 62static ACPI_TABLE_SRAT *srat; 63static vm_paddr_t srat_physaddr; 64 65static int vm_domains[VM_PHYSSEG_MAX]; 66 67static void srat_walk_table(acpi_subtable_handler *handler, void *arg); 68 69/* 70 * Returns true if a memory range overlaps with at least one range in 71 * phys_avail[]. 72 */ 73static int 74overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end) 75{ 76 int i; 77 78 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) { 79 if (phys_avail[i + 1] < start) 80 continue; 81 if (phys_avail[i] < end) 82 return (1); 83 break; 84 } 85 return (0); 86 87} 88 89static void 90srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg) 91{ 92 ACPI_SRAT_CPU_AFFINITY *cpu; 93 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic; 94 ACPI_SRAT_MEM_AFFINITY *mem; 95 int domain, i, slot; 96 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; 104 if (bootverbose) 105 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 106 cpu->ApicId, domain, 107 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ? 108 "enabled" : "disabled"); 109 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED)) 110 break; 111 if (cpus[cpu->ApicId].enabled) { 112 printf("SRAT: Duplicate local APIC ID %u\n", 113 cpu->ApicId); 114 *(int *)arg = ENXIO; 115 break; 116 } 117 cpus[cpu->ApicId].domain = domain; 118 cpus[cpu->ApicId].enabled = 1; 119 break; 120 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: 121 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry; 122 if (bootverbose) 123 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 124 x2apic->ApicId, x2apic->ProximityDomain, 125 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ? 126 "enabled" : "disabled"); 127 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED)) 128 break; 129 KASSERT(!cpus[x2apic->ApicId].enabled, 130 ("Duplicate local APIC ID %u", x2apic->ApicId)); 131 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain; 132 cpus[x2apic->ApicId].enabled = 1; 133 break; 134 case ACPI_SRAT_TYPE_MEMORY_AFFINITY: 135 mem = (ACPI_SRAT_MEM_AFFINITY *)entry; 136 if (bootverbose) 137 printf( 138 "SRAT: Found memory domain %d addr %jx len %jx: %s\n", 139 mem->ProximityDomain, (uintmax_t)mem->BaseAddress, 140 (uintmax_t)mem->Length, 141 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ? 142 "enabled" : "disabled"); 143 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED)) 144 break; 145 if (!overlaps_phys_avail(mem->BaseAddress, 146 mem->BaseAddress + mem->Length)) { 147 printf("SRAT: Ignoring memory at addr %jx\n", 148 (uintmax_t)mem->BaseAddress); 149 break; 150 } 151 if (num_mem == VM_PHYSSEG_MAX) { 152 printf("SRAT: Too many memory regions\n"); 153 *(int *)arg = ENXIO; 154 break; 155 } 156 slot = num_mem; 157 for (i = 0; i < num_mem; i++) { 158 if (mem_info[i].end <= mem->BaseAddress) 159 continue; 160 if (mem_info[i].start < 161 (mem->BaseAddress + mem->Length)) { 162 printf("SRAT: Overlapping memory entries\n"); 163 *(int *)arg = ENXIO; 164 return; 165 } 166 slot = i; 167 } 168 for (i = num_mem; i > slot; i--) 169 mem_info[i] = mem_info[i - 1]; 170 mem_info[slot].start = mem->BaseAddress; 171 mem_info[slot].end = mem->BaseAddress + mem->Length; 172 mem_info[slot].domain = mem->ProximityDomain; 173 num_mem++; 174 break; 175 } 176} 177 178/* 179 * Ensure each memory domain has at least one CPU and that each CPU 180 * has at least one memory domain. 181 */ 182static int 183check_domains(void) 184{ 185 int found, i, j; 186 187 for (i = 0; i < num_mem; i++) { 188 found = 0; 189 for (j = 0; j <= MAX_APIC_ID; j++) 190 if (cpus[j].enabled && 191 cpus[j].domain == mem_info[i].domain) { 192 cpus[j].has_memory = 1; 193 found++; 194 } 195 if (!found) { 196 printf("SRAT: No CPU found for memory domain %d\n", 197 mem_info[i].domain); 198 return (ENXIO); 199 } 200 } 201 for (i = 0; i <= MAX_APIC_ID; i++) 202 if (cpus[i].enabled && !cpus[i].has_memory) { 203 printf("SRAT: No memory found for CPU %d\n", i); 204 return (ENXIO); 205 } 206 return (0); 207} 208 209/* 210 * Check that the SRAT memory regions cover all of the regions in 211 * phys_avail[]. 212 */ 213static int 214check_phys_avail(void) 215{ 216 vm_paddr_t address; 217 int i, j; 218 219 /* j is the current offset into phys_avail[]. */ 220 address = phys_avail[0]; 221 j = 0; 222 for (i = 0; i < num_mem; i++) { 223 /* 224 * Consume as many phys_avail[] entries as fit in this 225 * region. 226 */ 227 while (address >= mem_info[i].start && 228 address <= mem_info[i].end) { 229 /* 230 * If we cover the rest of this phys_avail[] entry, 231 * advance to the next entry. 232 */ 233 if (phys_avail[j + 1] <= mem_info[i].end) { 234 j += 2; 235 if (phys_avail[j] == 0 && 236 phys_avail[j + 1] == 0) { 237 return (0); 238 } 239 address = phys_avail[j]; 240 } else 241 address = mem_info[i].end + 1; 242 } 243 } 244 printf("SRAT: No memory region found for %jx - %jx\n", 245 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]); 246 return (ENXIO); 247} 248 249/* 250 * Renumber the memory domains to be compact and zero-based if not 251 * already. Returns an error if there are too many domains. 252 */ 253static int 254renumber_domains(void) 255{ 256 int i, j, slot; 257 258 /* Enumerate all the domains. */ 259 vm_ndomains = 0; 260 for (i = 0; i < num_mem; i++) { 261 /* See if this domain is already known. */ 262 for (j = 0; j < vm_ndomains; j++) { 263 if (vm_domains[j] >= mem_info[i].domain) 264 break; 265 } 266 if (j < vm_ndomains && vm_domains[j] == mem_info[i].domain) 267 continue; 268 269 /* Insert the new domain at slot 'j'. */ 270 slot = j; 271 for (j = vm_ndomains; j > slot; j--) 272 vm_domains[j] = vm_domains[j - 1]; 273 vm_domains[slot] = mem_info[i].domain; 274 vm_ndomains++; 275 if (vm_ndomains > MAXMEMDOM) { 276 vm_ndomains = 1; 277 printf("SRAT: Too many memory domains\n"); 278 return (EFBIG); 279 } 280 } 281 282 /* Renumber each domain to its index in the sorted 'domains' list. */ 283 for (i = 0; i < vm_ndomains; i++) { 284 /* 285 * If the domain is already the right value, no need 286 * to renumber. 287 */ 288 if (vm_domains[i] == i) 289 continue; 290 291 /* Walk the cpu[] and mem_info[] arrays to renumber. */ 292 for (j = 0; j < num_mem; j++) 293 if (mem_info[j].domain == vm_domains[i]) 294 mem_info[j].domain = i; 295 for (j = 0; j <= MAX_APIC_ID; j++) 296 if (cpus[j].enabled && cpus[j].domain == vm_domains[i]) 297 cpus[j].domain = i; 298 } 299 KASSERT(vm_ndomains > 0, 300 ("renumber_domains: invalid final vm_ndomains setup")); 301 302 return (0); 303} 304 305/* 306 * Look for an ACPI System Resource Affinity Table ("SRAT") 307 */ 308static void 309parse_srat(void *dummy) 310{ 311 int error; 312 313 if (resource_disabled("srat", 0)) 314 return; 315 316 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT); 317 if (srat_physaddr == 0) 318 return; 319 320 /* 321 * Make a pass over the table to populate the cpus[] and 322 * mem_info[] tables. 323 */ 324 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT); 325 error = 0; 326 srat_walk_table(srat_parse_entry, &error); 327 acpi_unmap_table(srat); 328 srat = NULL; 329 if (error || check_domains() != 0 || check_phys_avail() != 0 || 330 renumber_domains() != 0) { 331 srat_physaddr = 0; 332 return; 333 } 334 335 /* Point vm_phys at our memory affinity table. */ 336 mem_affinity = mem_info; 337} 338SYSINIT(parse_srat, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_srat, NULL); 339 340static void 341srat_walk_table(acpi_subtable_handler *handler, void *arg) 342{ 343 344 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length, 345 handler, arg); 346} 347 348/* 349 * Setup per-CPU ACPI IDs. 350 */ 351static void 352srat_set_cpus(void *dummy) 353{ 354 struct cpu_info *cpu; 355 struct pcpu *pc; 356 u_int i; 357 358 if (srat_physaddr == 0) 359 return; 360 for (i = 0; i < MAXCPU; i++) { 361 if (CPU_ABSENT(i)) 362 continue; 363 pc = pcpu_find(i); 364 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i)); 365 cpu = &cpus[pc->pc_apic_id]; 366 if (!cpu->enabled) 367 panic("SRAT: CPU with APIC ID %u is not known", 368 pc->pc_apic_id); 369 pc->pc_domain = cpu->domain; 370 if (bootverbose) 371 printf("SRAT: CPU %u has memory domain %d\n", i, 372 cpu->domain); 373 } 374} 375SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL); 376 377/* 378 * Map a _PXM value to a VM domain ID. 379 * 380 * Returns the domain ID, or -1 if no domain ID was found. 381 */ 382int 383acpi_map_pxm_to_vm_domainid(int pxm) 384{ 385 int i; 386 387 for (i = 0; i < vm_ndomains; i++) { 388 if (vm_domains[i] == pxm) 389 return (i); 390 } 391 392 return (-1); 393} 394 395#endif /* MAXMEMDOM > 1 */ 396