1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010 Hudson River Trading LLC
5 * Written by: John H. Baldwin <[email protected]>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_vm.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/smp.h>
42 #include <sys/vmmeter.h>
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <vm/vm_param.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_phys.h>
48
49 #include <contrib/dev/acpica/include/acpi.h>
50 #include <contrib/dev/acpica/include/aclocal.h>
51 #include <contrib/dev/acpica/include/actables.h>
52
53 #include <machine/intr_machdep.h>
54 #include <machine/md_var.h>
55 #include <x86/apicvar.h>
56
57 #include <dev/acpica/acpivar.h>
58
59 #if MAXMEMDOM > 1
60 static struct cpu_info {
61 int enabled:1;
62 int has_memory:1;
63 int domain;
64 } *cpus;
65
66 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
67 int num_mem;
68
69 static ACPI_TABLE_SRAT *srat;
70 static vm_paddr_t srat_physaddr;
71
72 static int domain_pxm[MAXMEMDOM];
73 static int ndomain;
74
75 static ACPI_TABLE_SLIT *slit;
76 static vm_paddr_t slit_physaddr;
77 static int vm_locality_table[MAXMEMDOM * MAXMEMDOM];
78
79 static void srat_walk_table(acpi_subtable_handler *handler, void *arg);
80
81 /*
82 * SLIT parsing.
83 */
84
85 static void
slit_parse_table(ACPI_TABLE_SLIT * s)86 slit_parse_table(ACPI_TABLE_SLIT *s)
87 {
88 int i, j;
89 int i_domain, j_domain;
90 int offset = 0;
91 uint8_t e;
92
93 /*
94 * This maps the SLIT data into the VM-domain centric view.
95 * There may be sparse entries in the PXM namespace, so
96 * remap them to a VM-domain ID and if it doesn't exist,
97 * skip it.
98 *
99 * It should result in a packed 2d array of VM-domain
100 * locality information entries.
101 */
102
103 if (bootverbose)
104 printf("SLIT.Localities: %d\n", (int) s->LocalityCount);
105 for (i = 0; i < s->LocalityCount; i++) {
106 i_domain = acpi_map_pxm_to_vm_domainid(i);
107 if (i_domain < 0)
108 continue;
109
110 if (bootverbose)
111 printf("%d: ", i);
112 for (j = 0; j < s->LocalityCount; j++) {
113 j_domain = acpi_map_pxm_to_vm_domainid(j);
114 if (j_domain < 0)
115 continue;
116 e = s->Entry[i * s->LocalityCount + j];
117 if (bootverbose)
118 printf("%d ", (int) e);
119 /* 255 == "no locality information" */
120 if (e == 255)
121 vm_locality_table[offset] = -1;
122 else
123 vm_locality_table[offset] = e;
124 offset++;
125 }
126 if (bootverbose)
127 printf("\n");
128 }
129 }
130
131 /*
132 * Look for an ACPI System Locality Distance Information Table ("SLIT")
133 */
134 static int
parse_slit(void)135 parse_slit(void)
136 {
137
138 if (resource_disabled("slit", 0)) {
139 return (-1);
140 }
141
142 slit_physaddr = acpi_find_table(ACPI_SIG_SLIT);
143 if (slit_physaddr == 0) {
144 return (-1);
145 }
146
147 /*
148 * Make a pass over the table to populate the cpus[] and
149 * mem_info[] tables.
150 */
151 slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT);
152 slit_parse_table(slit);
153 acpi_unmap_table(slit);
154 slit = NULL;
155
156 return (0);
157 }
158
159 /*
160 * SRAT parsing.
161 */
162
163 /*
164 * Returns true if a memory range overlaps with at least one range in
165 * phys_avail[].
166 */
167 static int
overlaps_phys_avail(vm_paddr_t start,vm_paddr_t end)168 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end)
169 {
170 int i;
171
172 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) {
173 if (phys_avail[i + 1] <= start)
174 continue;
175 if (phys_avail[i] < end)
176 return (1);
177 break;
178 }
179 return (0);
180
181 }
182
183 static void
srat_parse_entry(ACPI_SUBTABLE_HEADER * entry,void * arg)184 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
185 {
186 ACPI_SRAT_CPU_AFFINITY *cpu;
187 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
188 ACPI_SRAT_MEM_AFFINITY *mem;
189 int domain, i, slot;
190
191 switch (entry->Type) {
192 case ACPI_SRAT_TYPE_CPU_AFFINITY:
193 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
194 domain = cpu->ProximityDomainLo |
195 cpu->ProximityDomainHi[0] << 8 |
196 cpu->ProximityDomainHi[1] << 16 |
197 cpu->ProximityDomainHi[2] << 24;
198 if (bootverbose)
199 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
200 cpu->ApicId, domain,
201 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
202 "enabled" : "disabled");
203 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
204 break;
205 if (cpu->ApicId > max_apic_id) {
206 printf("SRAT: Ignoring local APIC ID %u (too high)\n",
207 cpu->ApicId);
208 break;
209 }
210
211 if (cpus[cpu->ApicId].enabled) {
212 printf("SRAT: Duplicate local APIC ID %u\n",
213 cpu->ApicId);
214 *(int *)arg = ENXIO;
215 break;
216 }
217 cpus[cpu->ApicId].domain = domain;
218 cpus[cpu->ApicId].enabled = 1;
219 break;
220 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
221 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
222 if (bootverbose)
223 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
224 x2apic->ApicId, x2apic->ProximityDomain,
225 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
226 "enabled" : "disabled");
227 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
228 break;
229 if (x2apic->ApicId > max_apic_id) {
230 printf("SRAT: Ignoring local APIC ID %u (too high)\n",
231 x2apic->ApicId);
232 break;
233 }
234
235 KASSERT(!cpus[x2apic->ApicId].enabled,
236 ("Duplicate local APIC ID %u", x2apic->ApicId));
237 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
238 cpus[x2apic->ApicId].enabled = 1;
239 break;
240 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
241 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
242 if (bootverbose)
243 printf(
244 "SRAT: Found memory domain %d addr 0x%jx len 0x%jx: %s\n",
245 mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
246 (uintmax_t)mem->Length,
247 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
248 "enabled" : "disabled");
249 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
250 break;
251 if (mem->BaseAddress >= cpu_getmaxphyaddr() ||
252 !overlaps_phys_avail(mem->BaseAddress,
253 mem->BaseAddress + mem->Length)) {
254 printf("SRAT: Ignoring memory at addr 0x%jx\n",
255 (uintmax_t)mem->BaseAddress);
256 break;
257 }
258 if (num_mem == VM_PHYSSEG_MAX) {
259 printf("SRAT: Too many memory regions\n");
260 *(int *)arg = ENXIO;
261 break;
262 }
263 slot = num_mem;
264 for (i = 0; i < num_mem; i++) {
265 if (mem_info[i].end <= mem->BaseAddress)
266 continue;
267 if (mem_info[i].start <
268 (mem->BaseAddress + mem->Length)) {
269 printf("SRAT: Overlapping memory entries\n");
270 *(int *)arg = ENXIO;
271 return;
272 }
273 slot = i;
274 }
275 for (i = num_mem; i > slot; i--)
276 mem_info[i] = mem_info[i - 1];
277 mem_info[slot].start = mem->BaseAddress;
278 mem_info[slot].end = mem->BaseAddress + mem->Length;
279 mem_info[slot].domain = mem->ProximityDomain;
280 num_mem++;
281 break;
282 }
283 }
284
285 /*
286 * Ensure each memory domain has at least one CPU and that each CPU
287 * has at least one memory domain.
288 */
289 static int
check_domains(void)290 check_domains(void)
291 {
292 int found, i, j;
293
294 for (i = 0; i < num_mem; i++) {
295 found = 0;
296 for (j = 0; j <= max_apic_id; j++)
297 if (cpus[j].enabled &&
298 cpus[j].domain == mem_info[i].domain) {
299 cpus[j].has_memory = 1;
300 found++;
301 }
302 if (!found) {
303 printf("SRAT: No CPU found for memory domain %d\n",
304 mem_info[i].domain);
305 return (ENXIO);
306 }
307 }
308 for (i = 0; i <= max_apic_id; i++)
309 if (cpus[i].enabled && !cpus[i].has_memory) {
310 found = 0;
311 for (j = 0; j < num_mem && !found; j++) {
312 if (mem_info[j].domain == cpus[i].domain)
313 found = 1;
314 }
315 if (!found) {
316 if (bootverbose)
317 printf("SRAT: mem dom %d is empty\n",
318 cpus[i].domain);
319 mem_info[num_mem].start = 0;
320 mem_info[num_mem].end = 0;
321 mem_info[num_mem].domain = cpus[i].domain;
322 num_mem++;
323 }
324 }
325 return (0);
326 }
327
328 /*
329 * Check that the SRAT memory regions cover all of the regions in
330 * phys_avail[].
331 */
332 static int
check_phys_avail(void)333 check_phys_avail(void)
334 {
335 vm_paddr_t address;
336 int i, j;
337
338 /* j is the current offset into phys_avail[]. */
339 address = phys_avail[0];
340 j = 0;
341 for (i = 0; i < num_mem; i++) {
342 /*
343 * Consume as many phys_avail[] entries as fit in this
344 * region.
345 */
346 while (address >= mem_info[i].start &&
347 address <= mem_info[i].end) {
348 /*
349 * If we cover the rest of this phys_avail[] entry,
350 * advance to the next entry.
351 */
352 if (phys_avail[j + 1] <= mem_info[i].end) {
353 j += 2;
354 if (phys_avail[j] == 0 &&
355 phys_avail[j + 1] == 0) {
356 return (0);
357 }
358 address = phys_avail[j];
359 } else
360 address = mem_info[i].end + 1;
361 }
362 }
363 printf("SRAT: No memory region found for 0x%jx - 0x%jx\n",
364 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
365 return (ENXIO);
366 }
367
368 /*
369 * Renumber the memory domains to be compact and zero-based if not
370 * already. Returns an error if there are too many domains.
371 */
372 static int
renumber_domains(void)373 renumber_domains(void)
374 {
375 int i, j, slot;
376
377 /* Enumerate all the domains. */
378 ndomain = 0;
379 for (i = 0; i < num_mem; i++) {
380 /* See if this domain is already known. */
381 for (j = 0; j < ndomain; j++) {
382 if (domain_pxm[j] >= mem_info[i].domain)
383 break;
384 }
385 if (j < ndomain && domain_pxm[j] == mem_info[i].domain)
386 continue;
387
388 if (ndomain >= MAXMEMDOM) {
389 ndomain = 1;
390 printf("SRAT: Too many memory domains\n");
391 return (EFBIG);
392 }
393
394 /* Insert the new domain at slot 'j'. */
395 slot = j;
396 for (j = ndomain; j > slot; j--)
397 domain_pxm[j] = domain_pxm[j - 1];
398 domain_pxm[slot] = mem_info[i].domain;
399 ndomain++;
400 }
401
402 /* Renumber each domain to its index in the sorted 'domain_pxm' list. */
403 for (i = 0; i < ndomain; i++) {
404 /*
405 * If the domain is already the right value, no need
406 * to renumber.
407 */
408 if (domain_pxm[i] == i)
409 continue;
410
411 /* Walk the cpu[] and mem_info[] arrays to renumber. */
412 for (j = 0; j < num_mem; j++)
413 if (mem_info[j].domain == domain_pxm[i])
414 mem_info[j].domain = i;
415 for (j = 0; j <= max_apic_id; j++)
416 if (cpus[j].enabled && cpus[j].domain == domain_pxm[i])
417 cpus[j].domain = i;
418 }
419
420 return (0);
421 }
422
423 /*
424 * Look for an ACPI System Resource Affinity Table ("SRAT")
425 */
426 static int
parse_srat(void)427 parse_srat(void)
428 {
429 unsigned int idx, size;
430 vm_paddr_t addr;
431 int error;
432
433 if (resource_disabled("srat", 0))
434 return (-1);
435
436 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
437 if (srat_physaddr == 0)
438 return (-1);
439
440 /*
441 * Allocate data structure:
442 *
443 * Find the last physical memory region and steal some memory from
444 * it. This is done because at this point in the boot process
445 * malloc is still not usable.
446 */
447 for (idx = 0; phys_avail[idx + 1] != 0; idx += 2);
448 KASSERT(idx != 0, ("phys_avail is empty!"));
449 idx -= 2;
450
451 size = sizeof(*cpus) * (max_apic_id + 1);
452 addr = trunc_page(phys_avail[idx + 1] - size);
453 KASSERT(addr >= phys_avail[idx],
454 ("Not enough memory for SRAT table items"));
455 phys_avail[idx + 1] = addr - 1;
456
457 /*
458 * We cannot rely on PHYS_TO_DMAP because this code is also used in
459 * i386, so use pmap_mapbios to map the memory, this will end up using
460 * the default memory attribute (WB), and the DMAP when available.
461 */
462 cpus = (struct cpu_info *)pmap_mapbios(addr, size);
463 bzero(cpus, size);
464
465 /*
466 * Make a pass over the table to populate the cpus[] and
467 * mem_info[] tables.
468 */
469 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
470 error = 0;
471 srat_walk_table(srat_parse_entry, &error);
472 acpi_unmap_table(srat);
473 srat = NULL;
474 if (error || check_domains() != 0 || check_phys_avail() != 0 ||
475 renumber_domains() != 0) {
476 srat_physaddr = 0;
477 return (-1);
478 }
479
480 return (0);
481 }
482
483 static void
init_mem_locality(void)484 init_mem_locality(void)
485 {
486 int i;
487
488 /*
489 * For now, assume -1 == "no locality information for
490 * this pairing.
491 */
492 for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++)
493 vm_locality_table[i] = -1;
494 }
495
496 static void
parse_acpi_tables(void * dummy)497 parse_acpi_tables(void *dummy)
498 {
499
500 if (parse_srat() < 0)
501 return;
502 init_mem_locality();
503 (void)parse_slit();
504 vm_phys_register_domains(ndomain, mem_info, vm_locality_table);
505 }
506 SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables,
507 NULL);
508
509 static void
srat_walk_table(acpi_subtable_handler * handler,void * arg)510 srat_walk_table(acpi_subtable_handler *handler, void *arg)
511 {
512
513 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
514 handler, arg);
515 }
516
517 /*
518 * Setup per-CPU domain IDs.
519 */
520 static void
srat_set_cpus(void * dummy)521 srat_set_cpus(void *dummy)
522 {
523 struct cpu_info *cpu;
524 struct pcpu *pc;
525 u_int i;
526
527 if (srat_physaddr == 0)
528 return;
529 for (i = 0; i < MAXCPU; i++) {
530 if (CPU_ABSENT(i))
531 continue;
532 pc = pcpu_find(i);
533 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
534 cpu = &cpus[pc->pc_apic_id];
535 if (!cpu->enabled)
536 panic("SRAT: CPU with APIC ID %u is not known",
537 pc->pc_apic_id);
538 pc->pc_domain = vm_ndomains > 1 ? cpu->domain : 0;
539 CPU_SET(i, &cpuset_domain[pc->pc_domain]);
540 if (bootverbose)
541 printf("SRAT: CPU %u has memory domain %d\n", i,
542 pc->pc_domain);
543 }
544
545 /* Last usage of the cpus array, unmap it. */
546 pmap_unmapbios((vm_offset_t)cpus, sizeof(*cpus) * (max_apic_id + 1));
547 cpus = NULL;
548 }
549 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);
550
551 /*
552 * Map a _PXM value to a VM domain ID.
553 *
554 * Returns the domain ID, or -1 if no domain ID was found.
555 */
556 int
acpi_map_pxm_to_vm_domainid(int pxm)557 acpi_map_pxm_to_vm_domainid(int pxm)
558 {
559 int i;
560
561 for (i = 0; i < ndomain; i++) {
562 if (domain_pxm[i] == pxm)
563 return (vm_ndomains > 1 ? i : 0);
564 }
565
566 return (-1);
567 }
568
569 #else /* MAXMEMDOM == 1 */
570
571 int
acpi_map_pxm_to_vm_domainid(int pxm)572 acpi_map_pxm_to_vm_domainid(int pxm)
573 {
574
575 return (-1);
576 }
577
578 #endif /* MAXMEMDOM > 1 */
579