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