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