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