1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2010 Hudson River Trading LLC 5 * Written by: John H. Baldwin <jhb@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_vm.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/bus.h> 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/smp.h> 42 #include <sys/vmmeter.h> 43 #include <vm/vm.h> 44 #include <vm/pmap.h> 45 #include <vm/vm_param.h> 46 #include <vm/vm_page.h> 47 #include <vm/vm_phys.h> 48 49 #include <contrib/dev/acpica/include/acpi.h> 50 #include <contrib/dev/acpica/include/aclocal.h> 51 #include <contrib/dev/acpica/include/actables.h> 52 53 #include <machine/intr_machdep.h> 54 #include <machine/md_var.h> 55 #include <x86/apicvar.h> 56 57 #include <dev/acpica/acpivar.h> 58 59 #if MAXMEMDOM > 1 60 static struct cpu_info { 61 int enabled:1; 62 int has_memory:1; 63 int domain; 64 } *cpus; 65 66 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1]; 67 int num_mem; 68 69 static ACPI_TABLE_SRAT *srat; 70 static vm_paddr_t srat_physaddr; 71 72 static int domain_pxm[MAXMEMDOM]; 73 static int ndomain; 74 75 static ACPI_TABLE_SLIT *slit; 76 static vm_paddr_t slit_physaddr; 77 static int vm_locality_table[MAXMEMDOM * MAXMEMDOM]; 78 79 static void srat_walk_table(acpi_subtable_handler *handler, void *arg); 80 81 /* 82 * SLIT parsing. 83 */ 84 85 static void 86 slit_parse_table(ACPI_TABLE_SLIT *s) 87 { 88 int i, j; 89 int i_domain, j_domain; 90 int offset = 0; 91 uint8_t e; 92 93 /* 94 * This maps the SLIT data into the VM-domain centric view. 95 * There may be sparse entries in the PXM namespace, so 96 * remap them to a VM-domain ID and if it doesn't exist, 97 * skip it. 98 * 99 * It should result in a packed 2d array of VM-domain 100 * locality information entries. 101 */ 102 103 if (bootverbose) 104 printf("SLIT.Localities: %d\n", (int) s->LocalityCount); 105 for (i = 0; i < s->LocalityCount; i++) { 106 i_domain = acpi_map_pxm_to_vm_domainid(i); 107 if (i_domain < 0) 108 continue; 109 110 if (bootverbose) 111 printf("%d: ", i); 112 for (j = 0; j < s->LocalityCount; j++) { 113 j_domain = acpi_map_pxm_to_vm_domainid(j); 114 if (j_domain < 0) 115 continue; 116 e = s->Entry[i * s->LocalityCount + j]; 117 if (bootverbose) 118 printf("%d ", (int) e); 119 /* 255 == "no locality information" */ 120 if (e == 255) 121 vm_locality_table[offset] = -1; 122 else 123 vm_locality_table[offset] = e; 124 offset++; 125 } 126 if (bootverbose) 127 printf("\n"); 128 } 129 } 130 131 /* 132 * Look for an ACPI System Locality Distance Information Table ("SLIT") 133 */ 134 static int 135 parse_slit(void) 136 { 137 138 if (resource_disabled("slit", 0)) { 139 return (-1); 140 } 141 142 slit_physaddr = acpi_find_table(ACPI_SIG_SLIT); 143 if (slit_physaddr == 0) { 144 return (-1); 145 } 146 147 /* 148 * Make a pass over the table to populate the cpus[] and 149 * mem_info[] tables. 150 */ 151 slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT); 152 slit_parse_table(slit); 153 acpi_unmap_table(slit); 154 slit = NULL; 155 156 #ifdef NUMA 157 /* Tell the VM about it! */ 158 mem_locality = vm_locality_table; 159 #endif 160 return (0); 161 } 162 163 /* 164 * SRAT parsing. 165 */ 166 167 /* 168 * Returns true if a memory range overlaps with at least one range in 169 * phys_avail[]. 170 */ 171 static int 172 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end) 173 { 174 int i; 175 176 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) { 177 if (phys_avail[i + 1] <= start) 178 continue; 179 if (phys_avail[i] < end) 180 return (1); 181 break; 182 } 183 return (0); 184 185 } 186 187 static void 188 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg) 189 { 190 ACPI_SRAT_CPU_AFFINITY *cpu; 191 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic; 192 ACPI_SRAT_MEM_AFFINITY *mem; 193 int domain, i, slot; 194 195 switch (entry->Type) { 196 case ACPI_SRAT_TYPE_CPU_AFFINITY: 197 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry; 198 domain = cpu->ProximityDomainLo | 199 cpu->ProximityDomainHi[0] << 8 | 200 cpu->ProximityDomainHi[1] << 16 | 201 cpu->ProximityDomainHi[2] << 24; 202 if (bootverbose) 203 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 204 cpu->ApicId, domain, 205 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ? 206 "enabled" : "disabled"); 207 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED)) 208 break; 209 if (cpu->ApicId > max_apic_id) { 210 printf("SRAT: Ignoring local APIC ID %u (too high)\n", 211 cpu->ApicId); 212 break; 213 } 214 215 if (cpus[cpu->ApicId].enabled) { 216 printf("SRAT: Duplicate local APIC ID %u\n", 217 cpu->ApicId); 218 *(int *)arg = ENXIO; 219 break; 220 } 221 cpus[cpu->ApicId].domain = domain; 222 cpus[cpu->ApicId].enabled = 1; 223 break; 224 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: 225 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry; 226 if (bootverbose) 227 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 228 x2apic->ApicId, x2apic->ProximityDomain, 229 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ? 230 "enabled" : "disabled"); 231 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED)) 232 break; 233 if (x2apic->ApicId > max_apic_id) { 234 printf("SRAT: Ignoring local APIC ID %u (too high)\n", 235 x2apic->ApicId); 236 break; 237 } 238 239 KASSERT(!cpus[x2apic->ApicId].enabled, 240 ("Duplicate local APIC ID %u", x2apic->ApicId)); 241 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain; 242 cpus[x2apic->ApicId].enabled = 1; 243 break; 244 case ACPI_SRAT_TYPE_MEMORY_AFFINITY: 245 mem = (ACPI_SRAT_MEM_AFFINITY *)entry; 246 if (bootverbose) 247 printf( 248 "SRAT: Found memory domain %d addr 0x%jx len 0x%jx: %s\n", 249 mem->ProximityDomain, (uintmax_t)mem->BaseAddress, 250 (uintmax_t)mem->Length, 251 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ? 252 "enabled" : "disabled"); 253 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED)) 254 break; 255 if (mem->BaseAddress >= cpu_getmaxphyaddr() || 256 !overlaps_phys_avail(mem->BaseAddress, 257 mem->BaseAddress + mem->Length)) { 258 printf("SRAT: Ignoring memory at addr 0x%jx\n", 259 (uintmax_t)mem->BaseAddress); 260 break; 261 } 262 if (num_mem == VM_PHYSSEG_MAX) { 263 printf("SRAT: Too many memory regions\n"); 264 *(int *)arg = ENXIO; 265 break; 266 } 267 slot = num_mem; 268 for (i = 0; i < num_mem; i++) { 269 if (mem_info[i].end <= mem->BaseAddress) 270 continue; 271 if (mem_info[i].start < 272 (mem->BaseAddress + mem->Length)) { 273 printf("SRAT: Overlapping memory entries\n"); 274 *(int *)arg = ENXIO; 275 return; 276 } 277 slot = i; 278 } 279 for (i = num_mem; i > slot; i--) 280 mem_info[i] = mem_info[i - 1]; 281 mem_info[slot].start = mem->BaseAddress; 282 mem_info[slot].end = mem->BaseAddress + mem->Length; 283 mem_info[slot].domain = mem->ProximityDomain; 284 num_mem++; 285 break; 286 } 287 } 288 289 /* 290 * Ensure each memory domain has at least one CPU and that each CPU 291 * has at least one memory domain. 292 */ 293 static int 294 check_domains(void) 295 { 296 int found, i, j; 297 298 for (i = 0; i < num_mem; i++) { 299 found = 0; 300 for (j = 0; j <= max_apic_id; j++) 301 if (cpus[j].enabled && 302 cpus[j].domain == mem_info[i].domain) { 303 cpus[j].has_memory = 1; 304 found++; 305 } 306 if (!found) { 307 printf("SRAT: No CPU found for memory domain %d\n", 308 mem_info[i].domain); 309 return (ENXIO); 310 } 311 } 312 for (i = 0; i <= max_apic_id; i++) 313 if (cpus[i].enabled && !cpus[i].has_memory) { 314 found = 0; 315 for (j = 0; j < num_mem && !found; j++) { 316 if (mem_info[j].domain == cpus[i].domain) 317 found = 1; 318 } 319 if (!found) { 320 if (bootverbose) 321 printf("SRAT: mem dom %d is empty\n", 322 cpus[i].domain); 323 mem_info[num_mem].start = 0; 324 mem_info[num_mem].end = 0; 325 mem_info[num_mem].domain = cpus[i].domain; 326 num_mem++; 327 } 328 } 329 return (0); 330 } 331 332 /* 333 * Check that the SRAT memory regions cover all of the regions in 334 * phys_avail[]. 335 */ 336 static int 337 check_phys_avail(void) 338 { 339 vm_paddr_t address; 340 int i, j; 341 342 /* j is the current offset into phys_avail[]. */ 343 address = phys_avail[0]; 344 j = 0; 345 for (i = 0; i < num_mem; i++) { 346 /* 347 * Consume as many phys_avail[] entries as fit in this 348 * region. 349 */ 350 while (address >= mem_info[i].start && 351 address <= mem_info[i].end) { 352 /* 353 * If we cover the rest of this phys_avail[] entry, 354 * advance to the next entry. 355 */ 356 if (phys_avail[j + 1] <= mem_info[i].end) { 357 j += 2; 358 if (phys_avail[j] == 0 && 359 phys_avail[j + 1] == 0) { 360 return (0); 361 } 362 address = phys_avail[j]; 363 } else 364 address = mem_info[i].end + 1; 365 } 366 } 367 printf("SRAT: No memory region found for 0x%jx - 0x%jx\n", 368 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]); 369 return (ENXIO); 370 } 371 372 /* 373 * Renumber the memory domains to be compact and zero-based if not 374 * already. Returns an error if there are too many domains. 375 */ 376 static int 377 renumber_domains(void) 378 { 379 int i, j, slot; 380 381 /* Enumerate all the domains. */ 382 ndomain = 0; 383 for (i = 0; i < num_mem; i++) { 384 /* See if this domain is already known. */ 385 for (j = 0; j < ndomain; j++) { 386 if (domain_pxm[j] >= mem_info[i].domain) 387 break; 388 } 389 if (j < ndomain && domain_pxm[j] == mem_info[i].domain) 390 continue; 391 392 if (ndomain >= MAXMEMDOM) { 393 ndomain = 1; 394 printf("SRAT: Too many memory domains\n"); 395 return (EFBIG); 396 } 397 398 /* Insert the new domain at slot 'j'. */ 399 slot = j; 400 for (j = ndomain; j > slot; j--) 401 domain_pxm[j] = domain_pxm[j - 1]; 402 domain_pxm[slot] = mem_info[i].domain; 403 ndomain++; 404 } 405 406 /* Renumber each domain to its index in the sorted 'domain_pxm' list. */ 407 for (i = 0; i < ndomain; i++) { 408 /* 409 * If the domain is already the right value, no need 410 * to renumber. 411 */ 412 if (domain_pxm[i] == i) 413 continue; 414 415 /* Walk the cpu[] and mem_info[] arrays to renumber. */ 416 for (j = 0; j < num_mem; j++) 417 if (mem_info[j].domain == domain_pxm[i]) 418 mem_info[j].domain = i; 419 for (j = 0; j <= max_apic_id; j++) 420 if (cpus[j].enabled && cpus[j].domain == domain_pxm[i]) 421 cpus[j].domain = i; 422 } 423 424 return (0); 425 } 426 427 /* 428 * Look for an ACPI System Resource Affinity Table ("SRAT") 429 */ 430 static int 431 parse_srat(void) 432 { 433 unsigned int idx, size; 434 vm_paddr_t addr; 435 int error; 436 437 if (resource_disabled("srat", 0)) 438 return (-1); 439 440 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT); 441 if (srat_physaddr == 0) 442 return (-1); 443 444 /* 445 * Allocate data structure: 446 * 447 * Find the last physical memory region and steal some memory from 448 * it. This is done because at this point in the boot process 449 * malloc is still not usable. 450 */ 451 for (idx = 0; phys_avail[idx + 1] != 0; idx += 2); 452 KASSERT(idx != 0, ("phys_avail is empty!")); 453 idx -= 2; 454 455 size = sizeof(*cpus) * (max_apic_id + 1); 456 addr = trunc_page(phys_avail[idx + 1] - size); 457 KASSERT(addr >= phys_avail[idx], 458 ("Not enough memory for SRAT table items")); 459 phys_avail[idx + 1] = addr - 1; 460 461 /* 462 * We cannot rely on PHYS_TO_DMAP because this code is also used in 463 * i386, so use pmap_mapbios to map the memory, this will end up using 464 * the default memory attribute (WB), and the DMAP when available. 465 */ 466 cpus = (struct cpu_info *)pmap_mapbios(addr, size); 467 bzero(cpus, size); 468 469 /* 470 * Make a pass over the table to populate the cpus[] and 471 * mem_info[] tables. 472 */ 473 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT); 474 error = 0; 475 srat_walk_table(srat_parse_entry, &error); 476 acpi_unmap_table(srat); 477 srat = NULL; 478 if (error || check_domains() != 0 || check_phys_avail() != 0 || 479 renumber_domains() != 0) { 480 srat_physaddr = 0; 481 return (-1); 482 } 483 484 #ifdef NUMA 485 vm_ndomains = ndomain; 486 for (int i = 0; i < vm_ndomains; i++) 487 DOMAINSET_SET(i, &all_domains); 488 mem_affinity = mem_info; 489 #endif 490 491 return (0); 492 } 493 494 static void 495 init_mem_locality(void) 496 { 497 int i; 498 499 /* 500 * For now, assume -1 == "no locality information for 501 * this pairing. 502 */ 503 for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++) 504 vm_locality_table[i] = -1; 505 } 506 507 static void 508 parse_acpi_tables(void *dummy) 509 { 510 511 if (parse_srat() < 0) 512 return; 513 init_mem_locality(); 514 (void) parse_slit(); 515 } 516 SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables, 517 NULL); 518 519 static void 520 srat_walk_table(acpi_subtable_handler *handler, void *arg) 521 { 522 523 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length, 524 handler, arg); 525 } 526 527 /* 528 * Setup per-CPU domain IDs. 529 */ 530 static void 531 srat_set_cpus(void *dummy) 532 { 533 struct cpu_info *cpu; 534 struct pcpu *pc; 535 u_int i; 536 537 if (srat_physaddr == 0) 538 return; 539 for (i = 0; i < MAXCPU; i++) { 540 if (CPU_ABSENT(i)) 541 continue; 542 pc = pcpu_find(i); 543 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i)); 544 cpu = &cpus[pc->pc_apic_id]; 545 if (!cpu->enabled) 546 panic("SRAT: CPU with APIC ID %u is not known", 547 pc->pc_apic_id); 548 #ifdef NUMA 549 pc->pc_domain = cpu->domain; 550 #else 551 pc->pc_domain = 0; 552 #endif 553 CPU_SET(i, &cpuset_domain[pc->pc_domain]); 554 if (bootverbose) 555 printf("SRAT: CPU %u has memory domain %d\n", i, 556 pc->pc_domain); 557 } 558 559 /* Last usage of the cpus array, unmap it. */ 560 pmap_unmapbios((vm_offset_t)cpus, sizeof(*cpus) * (max_apic_id + 1)); 561 cpus = NULL; 562 } 563 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL); 564 565 /* 566 * Map a _PXM value to a VM domain ID. 567 * 568 * Returns the domain ID, or -1 if no domain ID was found. 569 */ 570 int 571 acpi_map_pxm_to_vm_domainid(int pxm) 572 { 573 int i; 574 575 for (i = 0; i < ndomain; i++) { 576 if (domain_pxm[i] == pxm) 577 return (i); 578 } 579 580 return (-1); 581 } 582 583 #else /* MAXMEMDOM == 1 */ 584 585 int 586 acpi_map_pxm_to_vm_domainid(int pxm) 587 { 588 589 return (-1); 590 } 591 592 #endif /* MAXMEMDOM > 1 */ 593