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 if (cpus[cpu->ApicId].enabled) { 205 printf("SRAT: Duplicate local APIC ID %u\n", 206 cpu->ApicId); 207 *(int *)arg = ENXIO; 208 break; 209 } 210 cpus[cpu->ApicId].domain = domain; 211 cpus[cpu->ApicId].enabled = 1; 212 break; 213 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: 214 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry; 215 if (bootverbose) 216 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 217 x2apic->ApicId, x2apic->ProximityDomain, 218 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ? 219 "enabled" : "disabled"); 220 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED)) 221 break; 222 KASSERT(!cpus[x2apic->ApicId].enabled, 223 ("Duplicate local APIC ID %u", x2apic->ApicId)); 224 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain; 225 cpus[x2apic->ApicId].enabled = 1; 226 break; 227 case ACPI_SRAT_TYPE_MEMORY_AFFINITY: 228 mem = (ACPI_SRAT_MEM_AFFINITY *)entry; 229 if (bootverbose) 230 printf( 231 "SRAT: Found memory domain %d addr %jx len %jx: %s\n", 232 mem->ProximityDomain, (uintmax_t)mem->BaseAddress, 233 (uintmax_t)mem->Length, 234 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ? 235 "enabled" : "disabled"); 236 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED)) 237 break; 238 if (!overlaps_phys_avail(mem->BaseAddress, 239 mem->BaseAddress + mem->Length)) { 240 printf("SRAT: Ignoring memory at addr %jx\n", 241 (uintmax_t)mem->BaseAddress); 242 break; 243 } 244 if (num_mem == VM_PHYSSEG_MAX) { 245 printf("SRAT: Too many memory regions\n"); 246 *(int *)arg = ENXIO; 247 break; 248 } 249 slot = num_mem; 250 for (i = 0; i < num_mem; i++) { 251 if (mem_info[i].end <= mem->BaseAddress) 252 continue; 253 if (mem_info[i].start < 254 (mem->BaseAddress + mem->Length)) { 255 printf("SRAT: Overlapping memory entries\n"); 256 *(int *)arg = ENXIO; 257 return; 258 } 259 slot = i; 260 } 261 for (i = num_mem; i > slot; i--) 262 mem_info[i] = mem_info[i - 1]; 263 mem_info[slot].start = mem->BaseAddress; 264 mem_info[slot].end = mem->BaseAddress + mem->Length; 265 mem_info[slot].domain = mem->ProximityDomain; 266 num_mem++; 267 break; 268 } 269 } 270 271 /* 272 * Ensure each memory domain has at least one CPU and that each CPU 273 * has at least one memory domain. 274 */ 275 static int 276 check_domains(void) 277 { 278 int found, i, j; 279 280 for (i = 0; i < num_mem; i++) { 281 found = 0; 282 for (j = 0; j <= MAX_APIC_ID; j++) 283 if (cpus[j].enabled && 284 cpus[j].domain == mem_info[i].domain) { 285 cpus[j].has_memory = 1; 286 found++; 287 } 288 if (!found) { 289 printf("SRAT: No CPU found for memory domain %d\n", 290 mem_info[i].domain); 291 return (ENXIO); 292 } 293 } 294 for (i = 0; i <= MAX_APIC_ID; i++) 295 if (cpus[i].enabled && !cpus[i].has_memory) { 296 printf("SRAT: No memory found for CPU %d\n", i); 297 return (ENXIO); 298 } 299 return (0); 300 } 301 302 /* 303 * Check that the SRAT memory regions cover all of the regions in 304 * phys_avail[]. 305 */ 306 static int 307 check_phys_avail(void) 308 { 309 vm_paddr_t address; 310 int i, j; 311 312 /* j is the current offset into phys_avail[]. */ 313 address = phys_avail[0]; 314 j = 0; 315 for (i = 0; i < num_mem; i++) { 316 /* 317 * Consume as many phys_avail[] entries as fit in this 318 * region. 319 */ 320 while (address >= mem_info[i].start && 321 address <= mem_info[i].end) { 322 /* 323 * If we cover the rest of this phys_avail[] entry, 324 * advance to the next entry. 325 */ 326 if (phys_avail[j + 1] <= mem_info[i].end) { 327 j += 2; 328 if (phys_avail[j] == 0 && 329 phys_avail[j + 1] == 0) { 330 return (0); 331 } 332 address = phys_avail[j]; 333 } else 334 address = mem_info[i].end + 1; 335 } 336 } 337 printf("SRAT: No memory region found for %jx - %jx\n", 338 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]); 339 return (ENXIO); 340 } 341 342 /* 343 * Renumber the memory domains to be compact and zero-based if not 344 * already. Returns an error if there are too many domains. 345 */ 346 static int 347 renumber_domains(void) 348 { 349 int i, j, slot; 350 351 /* Enumerate all the domains. */ 352 ndomain = 0; 353 for (i = 0; i < num_mem; i++) { 354 /* See if this domain is already known. */ 355 for (j = 0; j < ndomain; j++) { 356 if (domain_pxm[j] >= mem_info[i].domain) 357 break; 358 } 359 if (j < ndomain && domain_pxm[j] == mem_info[i].domain) 360 continue; 361 362 if (ndomain >= MAXMEMDOM) { 363 ndomain = 1; 364 printf("SRAT: Too many memory domains\n"); 365 return (EFBIG); 366 } 367 368 /* Insert the new domain at slot 'j'. */ 369 slot = j; 370 for (j = ndomain; j > slot; j--) 371 domain_pxm[j] = domain_pxm[j - 1]; 372 domain_pxm[slot] = mem_info[i].domain; 373 ndomain++; 374 } 375 376 /* Renumber each domain to its index in the sorted 'domain_pxm' list. */ 377 for (i = 0; i < ndomain; i++) { 378 /* 379 * If the domain is already the right value, no need 380 * to renumber. 381 */ 382 if (domain_pxm[i] == i) 383 continue; 384 385 /* Walk the cpu[] and mem_info[] arrays to renumber. */ 386 for (j = 0; j < num_mem; j++) 387 if (mem_info[j].domain == domain_pxm[i]) 388 mem_info[j].domain = i; 389 for (j = 0; j <= MAX_APIC_ID; j++) 390 if (cpus[j].enabled && cpus[j].domain == domain_pxm[i]) 391 cpus[j].domain = i; 392 } 393 394 return (0); 395 } 396 397 /* 398 * Look for an ACPI System Resource Affinity Table ("SRAT") 399 */ 400 static int 401 parse_srat(void) 402 { 403 int error; 404 405 if (resource_disabled("srat", 0)) 406 return (-1); 407 408 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT); 409 if (srat_physaddr == 0) 410 return (-1); 411 412 /* 413 * Make a pass over the table to populate the cpus[] and 414 * mem_info[] tables. 415 */ 416 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT); 417 error = 0; 418 srat_walk_table(srat_parse_entry, &error); 419 acpi_unmap_table(srat); 420 srat = NULL; 421 if (error || check_domains() != 0 || check_phys_avail() != 0 || 422 renumber_domains() != 0) { 423 srat_physaddr = 0; 424 return (-1); 425 } 426 427 #ifdef VM_NUMA_ALLOC 428 /* Point vm_phys at our memory affinity table. */ 429 vm_ndomains = ndomain; 430 mem_affinity = mem_info; 431 #endif 432 433 return (0); 434 } 435 436 static void 437 init_mem_locality(void) 438 { 439 int i; 440 441 /* 442 * For now, assume -1 == "no locality information for 443 * this pairing. 444 */ 445 for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++) 446 vm_locality_table[i] = -1; 447 } 448 449 static void 450 parse_acpi_tables(void *dummy) 451 { 452 453 if (parse_srat() < 0) 454 return; 455 init_mem_locality(); 456 (void) parse_slit(); 457 } 458 SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables, 459 NULL); 460 461 static void 462 srat_walk_table(acpi_subtable_handler *handler, void *arg) 463 { 464 465 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length, 466 handler, arg); 467 } 468 469 /* 470 * Setup per-CPU domain IDs. 471 */ 472 static void 473 srat_set_cpus(void *dummy) 474 { 475 struct cpu_info *cpu; 476 struct pcpu *pc; 477 u_int i; 478 479 if (srat_physaddr == 0) 480 return; 481 for (i = 0; i < MAXCPU; i++) { 482 if (CPU_ABSENT(i)) 483 continue; 484 pc = pcpu_find(i); 485 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i)); 486 cpu = &cpus[pc->pc_apic_id]; 487 if (!cpu->enabled) 488 panic("SRAT: CPU with APIC ID %u is not known", 489 pc->pc_apic_id); 490 pc->pc_domain = cpu->domain; 491 CPU_SET(i, &cpuset_domain[cpu->domain]); 492 if (bootverbose) 493 printf("SRAT: CPU %u has memory domain %d\n", i, 494 cpu->domain); 495 } 496 } 497 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL); 498 499 /* 500 * Map a _PXM value to a VM domain ID. 501 * 502 * Returns the domain ID, or -1 if no domain ID was found. 503 */ 504 int 505 acpi_map_pxm_to_vm_domainid(int pxm) 506 { 507 int i; 508 509 for (i = 0; i < ndomain; i++) { 510 if (domain_pxm[i] == pxm) 511 return (i); 512 } 513 514 return (-1); 515 } 516 517 #else /* MAXMEMDOM == 1 */ 518 519 int 520 acpi_map_pxm_to_vm_domainid(int pxm) 521 { 522 523 return (-1); 524 } 525 526 #endif /* MAXMEMDOM > 1 */ 527