1 /*- 2 * Copyright (c) 2010 Advanced Computing Technologies 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 void srat_walk_table(acpi_subtable_handler *handler, void *arg); 66 67 /* 68 * Returns true if a memory range overlaps with at least one range in 69 * phys_avail[]. 70 */ 71 static int 72 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end) 73 { 74 int i; 75 76 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) { 77 if (phys_avail[i + 1] < start) 78 continue; 79 if (phys_avail[i] < end) 80 return (1); 81 break; 82 } 83 return (0); 84 85 } 86 87 static void 88 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg) 89 { 90 ACPI_SRAT_CPU_AFFINITY *cpu; 91 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic; 92 ACPI_SRAT_MEM_AFFINITY *mem; 93 int domain, i, slot; 94 95 switch (entry->Type) { 96 case ACPI_SRAT_TYPE_CPU_AFFINITY: 97 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry; 98 domain = cpu->ProximityDomainLo | 99 cpu->ProximityDomainHi[0] << 8 | 100 cpu->ProximityDomainHi[1] << 16 | 101 cpu->ProximityDomainHi[2] << 24; 102 if (bootverbose) 103 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 104 cpu->ApicId, domain, 105 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ? 106 "enabled" : "disabled"); 107 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED)) 108 break; 109 KASSERT(!cpus[cpu->ApicId].enabled, 110 ("Duplicate local APIC ID %u", cpu->ApicId)); 111 cpus[cpu->ApicId].domain = domain; 112 cpus[cpu->ApicId].enabled = 1; 113 break; 114 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: 115 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry; 116 if (bootverbose) 117 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n", 118 x2apic->ApicId, x2apic->ProximityDomain, 119 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ? 120 "enabled" : "disabled"); 121 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED)) 122 break; 123 KASSERT(!cpus[x2apic->ApicId].enabled, 124 ("Duplicate local APIC ID %u", x2apic->ApicId)); 125 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain; 126 cpus[x2apic->ApicId].enabled = 1; 127 break; 128 case ACPI_SRAT_TYPE_MEMORY_AFFINITY: 129 mem = (ACPI_SRAT_MEM_AFFINITY *)entry; 130 if (bootverbose) 131 printf( 132 "SRAT: Found memory domain %d addr %jx len %jx: %s\n", 133 mem->ProximityDomain, (uintmax_t)mem->BaseAddress, 134 (uintmax_t)mem->Length, 135 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ? 136 "enabled" : "disabled"); 137 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED)) 138 break; 139 if (!overlaps_phys_avail(mem->BaseAddress, 140 mem->BaseAddress + mem->Length)) { 141 printf("SRAT: Ignoring memory at addr %jx\n", 142 (uintmax_t)mem->BaseAddress); 143 break; 144 } 145 if (num_mem == VM_PHYSSEG_MAX) { 146 printf("SRAT: Too many memory regions\n"); 147 *(int *)arg = ENXIO; 148 break; 149 } 150 slot = num_mem; 151 for (i = 0; i < num_mem; i++) { 152 if (mem_info[i].end <= mem->BaseAddress) 153 continue; 154 if (mem_info[i].start < 155 (mem->BaseAddress + mem->Length)) { 156 printf("SRAT: Overlapping memory entries\n"); 157 *(int *)arg = ENXIO; 158 return; 159 } 160 slot = i; 161 } 162 for (i = num_mem; i > slot; i--) 163 mem_info[i] = mem_info[i - 1]; 164 mem_info[slot].start = mem->BaseAddress; 165 mem_info[slot].end = mem->BaseAddress + mem->Length; 166 mem_info[slot].domain = mem->ProximityDomain; 167 num_mem++; 168 break; 169 } 170 } 171 172 /* 173 * Ensure each memory domain has at least one CPU and that each CPU 174 * has at least one memory domain. 175 */ 176 static int 177 check_domains(void) 178 { 179 int found, i, j; 180 181 for (i = 0; i < num_mem; i++) { 182 found = 0; 183 for (j = 0; j <= MAX_APIC_ID; j++) 184 if (cpus[j].enabled && 185 cpus[j].domain == mem_info[i].domain) { 186 cpus[j].has_memory = 1; 187 found++; 188 } 189 if (!found) { 190 printf("SRAT: No CPU found for memory domain %d\n", 191 mem_info[i].domain); 192 return (ENXIO); 193 } 194 } 195 for (i = 0; i <= MAX_APIC_ID; i++) 196 if (cpus[i].enabled && !cpus[i].has_memory) { 197 printf("SRAT: No memory found for CPU %d\n", i); 198 return (ENXIO); 199 } 200 return (0); 201 } 202 203 /* 204 * Check that the SRAT memory regions cover all of the regions in 205 * phys_avail[]. 206 */ 207 static int 208 check_phys_avail(void) 209 { 210 vm_paddr_t address; 211 int i, j; 212 213 /* j is the current offset into phys_avail[]. */ 214 address = phys_avail[0]; 215 j = 0; 216 for (i = 0; i < num_mem; i++) { 217 /* 218 * Consume as many phys_avail[] entries as fit in this 219 * region. 220 */ 221 while (address >= mem_info[i].start && 222 address <= mem_info[i].end) { 223 /* 224 * If we cover the rest of this phys_avail[] entry, 225 * advance to the next entry. 226 */ 227 if (phys_avail[j + 1] <= mem_info[i].end) { 228 j += 2; 229 if (phys_avail[j] == 0 && 230 phys_avail[j + 1] == 0) { 231 return (0); 232 } 233 address = phys_avail[j]; 234 } else 235 address = mem_info[i].end + 1; 236 } 237 } 238 printf("SRAT: No memory region found for %jx - %jx\n", 239 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]); 240 return (ENXIO); 241 } 242 243 /* 244 * Renumber the memory domains to be compact and zero-based if not 245 * already. Returns an error if there are too many domains. 246 */ 247 static int 248 renumber_domains(void) 249 { 250 int domains[VM_PHYSSEG_MAX]; 251 int i, j, slot; 252 253 /* Enumerate all the domains. */ 254 vm_ndomains = 0; 255 for (i = 0; i < num_mem; i++) { 256 /* See if this domain is already known. */ 257 for (j = 0; j < vm_ndomains; j++) { 258 if (domains[j] >= mem_info[i].domain) 259 break; 260 } 261 if (j < vm_ndomains && domains[j] == mem_info[i].domain) 262 continue; 263 264 /* Insert the new domain at slot 'j'. */ 265 slot = j; 266 for (j = vm_ndomains; j > slot; j--) 267 domains[j] = domains[j - 1]; 268 domains[slot] = mem_info[i].domain; 269 vm_ndomains++; 270 if (vm_ndomains > MAXMEMDOM) { 271 vm_ndomains = 1; 272 printf("SRAT: Too many memory domains\n"); 273 return (EFBIG); 274 } 275 } 276 277 /* Renumber each domain to its index in the sorted 'domains' list. */ 278 for (i = 0; i < vm_ndomains; i++) { 279 /* 280 * If the domain is already the right value, no need 281 * to renumber. 282 */ 283 if (domains[i] == i) 284 continue; 285 286 /* Walk the cpu[] and mem_info[] arrays to renumber. */ 287 for (j = 0; j < num_mem; j++) 288 if (mem_info[j].domain == domains[i]) 289 mem_info[j].domain = i; 290 for (j = 0; j <= MAX_APIC_ID; j++) 291 if (cpus[j].enabled && cpus[j].domain == domains[i]) 292 cpus[j].domain = i; 293 } 294 KASSERT(vm_ndomains > 0, 295 ("renumber_domains: invalid final vm_ndomains setup")); 296 297 return (0); 298 } 299 300 /* 301 * Look for an ACPI System Resource Affinity Table ("SRAT") 302 */ 303 static void 304 parse_srat(void *dummy) 305 { 306 int error; 307 308 if (resource_disabled("srat", 0)) 309 return; 310 311 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT); 312 if (srat_physaddr == 0) 313 return; 314 315 /* 316 * Make a pass over the table to populate the cpus[] and 317 * mem_info[] tables. 318 */ 319 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT); 320 error = 0; 321 srat_walk_table(srat_parse_entry, &error); 322 acpi_unmap_table(srat); 323 srat = NULL; 324 if (error || check_domains() != 0 || check_phys_avail() != 0 || 325 renumber_domains() != 0) { 326 srat_physaddr = 0; 327 return; 328 } 329 330 /* Point vm_phys at our memory affinity table. */ 331 mem_affinity = mem_info; 332 } 333 SYSINIT(parse_srat, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_srat, NULL); 334 335 static void 336 srat_walk_table(acpi_subtable_handler *handler, void *arg) 337 { 338 339 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length, 340 handler, arg); 341 } 342 343 /* 344 * Setup per-CPU ACPI IDs. 345 */ 346 static void 347 srat_set_cpus(void *dummy) 348 { 349 struct cpu_info *cpu; 350 struct pcpu *pc; 351 u_int i; 352 353 if (srat_physaddr == 0) 354 return; 355 for (i = 0; i < MAXCPU; i++) { 356 if (CPU_ABSENT(i)) 357 continue; 358 pc = pcpu_find(i); 359 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i)); 360 cpu = &cpus[pc->pc_apic_id]; 361 if (!cpu->enabled) 362 panic("SRAT: CPU with APIC ID %u is not known", 363 pc->pc_apic_id); 364 pc->pc_domain = cpu->domain; 365 if (bootverbose) 366 printf("SRAT: CPU %u has memory domain %d\n", i, 367 cpu->domain); 368 } 369 } 370 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL); 371 #endif /* MAXMEMDOM > 1 */ 372