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$"); 30 31 #include "opt_vm.h" 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/smp.h> 39 #include <sys/vmmeter.h> 40 #include <vm/vm.h> 41 #include <vm/pmap.h> 42 #include <vm/vm_param.h> 43 #include <vm/vm_page.h> 44 #include <vm/vm_phys.h> 45 46 #include <contrib/dev/acpica/include/acpi.h> 47 #include <contrib/dev/acpica/include/actables.h> 48 49 #include <machine/intr_machdep.h> 50 #include <x86/apicvar.h> 51 52 #include <dev/acpica/acpivar.h> 53 54 #if MAXMEMDOM > 1 55 struct cpu_info { 56 int enabled:1; 57 int has_memory:1; 58 int domain; 59 } cpus[MAX_APIC_ID + 1]; 60 61 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1]; 62 int num_mem; 63 64 static ACPI_TABLE_SRAT *srat; 65 static vm_paddr_t srat_physaddr; 66 67 static int domain_pxm[MAXMEMDOM]; 68 static int ndomain; 69 70 static ACPI_TABLE_SLIT *slit; 71 static vm_paddr_t slit_physaddr; 72 static int vm_locality_table[MAXMEMDOM * MAXMEMDOM]; 73 74 static void srat_walk_table(acpi_subtable_handler *handler, void *arg); 75 76 /* 77 * SLIT parsing. 78 */ 79 80 static void 81 slit_parse_table(ACPI_TABLE_SLIT *s) 82 { 83 int i, j; 84 int i_domain, j_domain; 85 int offset = 0; 86 uint8_t e; 87 88 /* 89 * This maps the SLIT data into the VM-domain centric view. 90 * There may be sparse entries in the PXM namespace, so 91 * remap them to a VM-domain ID and if it doesn't exist, 92 * skip it. 93 * 94 * It should result in a packed 2d array of VM-domain 95 * locality information entries. 96 */ 97 98 if (bootverbose) 99 printf("SLIT.Localities: %d\n", (int) s->LocalityCount); 100 for (i = 0; i < s->LocalityCount; i++) { 101 i_domain = acpi_map_pxm_to_vm_domainid(i); 102 if (i_domain < 0) 103 continue; 104 105 if (bootverbose) 106 printf("%d: ", i); 107 for (j = 0; j < s->LocalityCount; j++) { 108 j_domain = acpi_map_pxm_to_vm_domainid(j); 109 if (j_domain < 0) 110 continue; 111 e = s->Entry[i * s->LocalityCount + j]; 112 if (bootverbose) 113 printf("%d ", (int) e); 114 /* 255 == "no locality information" */ 115 if (e == 255) 116 vm_locality_table[offset] = -1; 117 else 118 vm_locality_table[offset] = e; 119 offset++; 120 } 121 if (bootverbose) 122 printf("\n"); 123 } 124 } 125 126 /* 127 * Look for an ACPI System Locality Distance Information Table ("SLIT") 128 */ 129 static int 130 parse_slit(void) 131 { 132 133 if (resource_disabled("slit", 0)) { 134 return (-1); 135 } 136 137 slit_physaddr = acpi_find_table(ACPI_SIG_SLIT); 138 if (slit_physaddr == 0) { 139 return (-1); 140 } 141 142 /* 143 * Make a pass over the table to populate the cpus[] and 144 * mem_info[] tables. 145 */ 146 slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT); 147 slit_parse_table(slit); 148 acpi_unmap_table(slit); 149 slit = NULL; 150 151 #ifdef VM_NUMA_ALLOC 152 /* Tell the VM about it! */ 153 mem_locality = vm_locality_table; 154 #endif 155 return (0); 156 } 157 158 /* 159 * SRAT parsing. 160 */ 161 162 /* 163 * Returns true if a memory range overlaps with at least one range in 164 * phys_avail[]. 165 */ 166 static int 167 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end) 168 { 169 int i; 170 171 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) { 172 if (phys_avail[i + 1] < start) 173 continue; 174 if (phys_avail[i] < end) 175 return (1); 176 break; 177 } 178 return (0); 179 180 } 181 182 static void 183 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg) 184 { 185 ACPI_SRAT_CPU_AFFINITY *cpu; 186 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic; 187 ACPI_SRAT_MEM_AFFINITY *mem; 188 int domain, i, slot; 189 190 switch (entry->Type) { 191 case ACPI_SRAT_TYPE_CPU_AFFINITY: 192 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry; 193 domain = cpu->ProximityDomainLo | 194 cpu->ProximityDomainHi[0] << 8 | 195 cpu->ProximityDomainHi[1] << 16 | 196 cpu->ProximityDomainHi[2] << 24; 197 if (bootverbose) 198 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 199 cpu->ApicId, domain, 200 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ? 201 "enabled" : "disabled"); 202 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED)) 203 break; 204 KASSERT(!cpus[cpu->ApicId].enabled, 205 ("Duplicate local APIC ID %u", cpu->ApicId)); 206 cpus[cpu->ApicId].domain = domain; 207 cpus[cpu->ApicId].enabled = 1; 208 break; 209 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: 210 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry; 211 if (bootverbose) 212 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 213 x2apic->ApicId, x2apic->ProximityDomain, 214 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ? 215 "enabled" : "disabled"); 216 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED)) 217 break; 218 KASSERT(!cpus[x2apic->ApicId].enabled, 219 ("Duplicate local APIC ID %u", x2apic->ApicId)); 220 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain; 221 cpus[x2apic->ApicId].enabled = 1; 222 break; 223 case ACPI_SRAT_TYPE_MEMORY_AFFINITY: 224 mem = (ACPI_SRAT_MEM_AFFINITY *)entry; 225 if (bootverbose) 226 printf( 227 "SRAT: Found memory domain %d addr %jx len %jx: %s\n", 228 mem->ProximityDomain, (uintmax_t)mem->BaseAddress, 229 (uintmax_t)mem->Length, 230 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ? 231 "enabled" : "disabled"); 232 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED)) 233 break; 234 if (!overlaps_phys_avail(mem->BaseAddress, 235 mem->BaseAddress + mem->Length)) { 236 printf("SRAT: Ignoring memory at addr %jx\n", 237 (uintmax_t)mem->BaseAddress); 238 break; 239 } 240 if (num_mem == VM_PHYSSEG_MAX) { 241 printf("SRAT: Too many memory regions\n"); 242 *(int *)arg = ENXIO; 243 break; 244 } 245 slot = num_mem; 246 for (i = 0; i < num_mem; i++) { 247 if (mem_info[i].end <= mem->BaseAddress) 248 continue; 249 if (mem_info[i].start < 250 (mem->BaseAddress + mem->Length)) { 251 printf("SRAT: Overlapping memory entries\n"); 252 *(int *)arg = ENXIO; 253 return; 254 } 255 slot = i; 256 } 257 for (i = num_mem; i > slot; i--) 258 mem_info[i] = mem_info[i - 1]; 259 mem_info[slot].start = mem->BaseAddress; 260 mem_info[slot].end = mem->BaseAddress + mem->Length; 261 mem_info[slot].domain = mem->ProximityDomain; 262 num_mem++; 263 break; 264 } 265 } 266 267 /* 268 * Ensure each memory domain has at least one CPU and that each CPU 269 * has at least one memory domain. 270 */ 271 static int 272 check_domains(void) 273 { 274 int found, i, j; 275 276 for (i = 0; i < num_mem; i++) { 277 found = 0; 278 for (j = 0; j <= MAX_APIC_ID; j++) 279 if (cpus[j].enabled && 280 cpus[j].domain == mem_info[i].domain) { 281 cpus[j].has_memory = 1; 282 found++; 283 } 284 if (!found) { 285 printf("SRAT: No CPU found for memory domain %d\n", 286 mem_info[i].domain); 287 return (ENXIO); 288 } 289 } 290 for (i = 0; i <= MAX_APIC_ID; i++) 291 if (cpus[i].enabled && !cpus[i].has_memory) { 292 printf("SRAT: No memory found for CPU %d\n", i); 293 return (ENXIO); 294 } 295 return (0); 296 } 297 298 /* 299 * Check that the SRAT memory regions cover all of the regions in 300 * phys_avail[]. 301 */ 302 static int 303 check_phys_avail(void) 304 { 305 vm_paddr_t address; 306 int i, j; 307 308 /* j is the current offset into phys_avail[]. */ 309 address = phys_avail[0]; 310 j = 0; 311 for (i = 0; i < num_mem; i++) { 312 /* 313 * Consume as many phys_avail[] entries as fit in this 314 * region. 315 */ 316 while (address >= mem_info[i].start && 317 address <= mem_info[i].end) { 318 /* 319 * If we cover the rest of this phys_avail[] entry, 320 * advance to the next entry. 321 */ 322 if (phys_avail[j + 1] <= mem_info[i].end) { 323 j += 2; 324 if (phys_avail[j] == 0 && 325 phys_avail[j + 1] == 0) { 326 return (0); 327 } 328 address = phys_avail[j]; 329 } else 330 address = mem_info[i].end + 1; 331 } 332 } 333 printf("SRAT: No memory region found for %jx - %jx\n", 334 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]); 335 return (ENXIO); 336 } 337 338 /* 339 * Renumber the memory domains to be compact and zero-based if not 340 * already. Returns an error if there are too many domains. 341 */ 342 static int 343 renumber_domains(void) 344 { 345 int i, j, slot; 346 347 /* Enumerate all the domains. */ 348 ndomain = 0; 349 for (i = 0; i < num_mem; i++) { 350 /* See if this domain is already known. */ 351 for (j = 0; j < ndomain; j++) { 352 if (domain_pxm[j] >= mem_info[i].domain) 353 break; 354 } 355 if (j < ndomain && domain_pxm[j] == mem_info[i].domain) 356 continue; 357 358 /* Insert the new domain at slot 'j'. */ 359 slot = j; 360 for (j = ndomain; j > slot; j--) 361 domain_pxm[j] = domain_pxm[j - 1]; 362 domain_pxm[slot] = mem_info[i].domain; 363 ndomain++; 364 if (ndomain > MAXMEMDOM) { 365 ndomain = 1; 366 printf("SRAT: Too many memory domains\n"); 367 return (EFBIG); 368 } 369 } 370 371 /* Renumber each domain to its index in the sorted 'domain_pxm' list. */ 372 for (i = 0; i < ndomain; i++) { 373 /* 374 * If the domain is already the right value, no need 375 * to renumber. 376 */ 377 if (domain_pxm[i] == i) 378 continue; 379 380 /* Walk the cpu[] and mem_info[] arrays to renumber. */ 381 for (j = 0; j < num_mem; j++) 382 if (mem_info[j].domain == domain_pxm[i]) 383 mem_info[j].domain = i; 384 for (j = 0; j <= MAX_APIC_ID; j++) 385 if (cpus[j].enabled && cpus[j].domain == domain_pxm[i]) 386 cpus[j].domain = i; 387 } 388 389 return (0); 390 } 391 392 /* 393 * Look for an ACPI System Resource Affinity Table ("SRAT") 394 */ 395 static int 396 parse_srat(void) 397 { 398 int error; 399 400 if (resource_disabled("srat", 0)) 401 return (-1); 402 403 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT); 404 if (srat_physaddr == 0) 405 return (-1); 406 407 /* 408 * Make a pass over the table to populate the cpus[] and 409 * mem_info[] tables. 410 */ 411 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT); 412 error = 0; 413 srat_walk_table(srat_parse_entry, &error); 414 acpi_unmap_table(srat); 415 srat = NULL; 416 if (error || check_domains() != 0 || check_phys_avail() != 0 || 417 renumber_domains() != 0) { 418 srat_physaddr = 0; 419 return (-1); 420 } 421 422 #ifdef VM_NUMA_ALLOC 423 /* Point vm_phys at our memory affinity table. */ 424 vm_ndomains = ndomain; 425 mem_affinity = mem_info; 426 #endif 427 428 return (0); 429 } 430 431 static void 432 init_mem_locality(void) 433 { 434 int i; 435 436 /* 437 * For now, assume -1 == "no locality information for 438 * this pairing. 439 */ 440 for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++) 441 vm_locality_table[i] = -1; 442 } 443 444 static void 445 parse_acpi_tables(void *dummy) 446 { 447 448 if (parse_srat() < 0) 449 return; 450 init_mem_locality(); 451 (void) parse_slit(); 452 } 453 SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables, 454 NULL); 455 456 static void 457 srat_walk_table(acpi_subtable_handler *handler, void *arg) 458 { 459 460 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length, 461 handler, arg); 462 } 463 464 /* 465 * Setup per-CPU domain IDs. 466 */ 467 static void 468 srat_set_cpus(void *dummy) 469 { 470 struct cpu_info *cpu; 471 struct pcpu *pc; 472 u_int i; 473 474 if (srat_physaddr == 0) 475 return; 476 for (i = 0; i < MAXCPU; i++) { 477 if (CPU_ABSENT(i)) 478 continue; 479 pc = pcpu_find(i); 480 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i)); 481 cpu = &cpus[pc->pc_apic_id]; 482 if (!cpu->enabled) 483 panic("SRAT: CPU with APIC ID %u is not known", 484 pc->pc_apic_id); 485 pc->pc_domain = cpu->domain; 486 CPU_SET(i, &cpuset_domain[cpu->domain]); 487 if (bootverbose) 488 printf("SRAT: CPU %u has memory domain %d\n", i, 489 cpu->domain); 490 } 491 } 492 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL); 493 494 /* 495 * Map a _PXM value to a VM domain ID. 496 * 497 * Returns the domain ID, or -1 if no domain ID was found. 498 */ 499 int 500 acpi_map_pxm_to_vm_domainid(int pxm) 501 { 502 int i; 503 504 for (i = 0; i < ndomain; i++) { 505 if (domain_pxm[i] == pxm) 506 return (i); 507 } 508 509 return (-1); 510 } 511 512 #else /* MAXMEMDOM == 1 */ 513 514 int 515 acpi_map_pxm_to_vm_domainid(int pxm) 516 { 517 518 return (-1); 519 } 520 521 #endif /* MAXMEMDOM > 1 */ 522