2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Hudson River Trading LLC
5 * Written by: John H. Baldwin <jhb@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/mutex.h>
42 #include <sys/vmmeter.h>
45 #include <vm/vm_param.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_phys.h>
49 #include <contrib/dev/acpica/include/acpi.h>
50 #include <contrib/dev/acpica/include/aclocal.h>
51 #include <contrib/dev/acpica/include/actables.h>
53 #include <machine/md_var.h>
55 #include <dev/acpica/acpivar.h>
58 static struct cpu_info {
68 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
71 static ACPI_TABLE_SRAT *srat;
72 static vm_paddr_t srat_physaddr;
74 static int domain_pxm[MAXMEMDOM];
76 static vm_paddr_t maxphyaddr;
78 static ACPI_TABLE_SLIT *slit;
79 static vm_paddr_t slit_physaddr;
80 static int vm_locality_table[MAXMEMDOM * MAXMEMDOM];
82 static void srat_walk_table(acpi_subtable_handler *handler, void *arg);
89 slit_parse_table(ACPI_TABLE_SLIT *s)
92 int i_domain, j_domain;
97 * This maps the SLIT data into the VM-domain centric view.
98 * There may be sparse entries in the PXM namespace, so
99 * remap them to a VM-domain ID and if it doesn't exist,
102 * It should result in a packed 2d array of VM-domain
103 * locality information entries.
107 printf("SLIT.Localities: %d\n", (int) s->LocalityCount);
108 for (i = 0; i < s->LocalityCount; i++) {
109 i_domain = acpi_map_pxm_to_vm_domainid(i);
115 for (j = 0; j < s->LocalityCount; j++) {
116 j_domain = acpi_map_pxm_to_vm_domainid(j);
119 e = s->Entry[i * s->LocalityCount + j];
121 printf("%d ", (int) e);
122 /* 255 == "no locality information" */
124 vm_locality_table[offset] = -1;
126 vm_locality_table[offset] = e;
135 * Look for an ACPI System Locality Distance Information Table ("SLIT")
141 if (resource_disabled("slit", 0)) {
145 slit_physaddr = acpi_find_table(ACPI_SIG_SLIT);
146 if (slit_physaddr == 0) {
151 * Make a pass over the table to populate the cpus[] and
154 slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT);
155 slit_parse_table(slit);
156 acpi_unmap_table(slit);
167 * Returns true if a memory range overlaps with at least one range in
171 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end)
175 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) {
176 if (phys_avail[i + 1] <= start)
178 if (phys_avail[i] < end)
186 * On x86 we can use the cpuid to index the cpus array, but on arm64
187 * we have an ACPI Processor UID with a larger range.
189 * Use this variable to indicate if the cpus can be stored by index.
192 static const int cpus_use_indexing = 0;
194 static const int cpus_use_indexing = 1;
198 * Find CPU by processor ID (APIC ID on x86, Processor UID on arm64)
200 static struct cpu_info *
205 if (cpus_use_indexing) {
206 if (cpuid <= last_cpu && cpus[cpuid].enabled)
207 return (&cpus[cpuid]);
209 for (i = 0; i <= last_cpu; i++)
210 if (cpus[i].id == cpuid)
217 * Find CPU by pcpu pointer.
219 static struct cpu_info *
220 cpu_get_info(struct pcpu *pc)
222 struct cpu_info *cpup;
232 panic("SRAT: CPU with ID %u is not known", id);
237 * Add proximity information for a new CPU.
239 static struct cpu_info *
240 cpu_add(int cpuid, int domain)
242 struct cpu_info *cpup;
244 if (cpus_use_indexing) {
245 if (cpuid >= max_cpus)
247 last_cpu = imax(last_cpu, cpuid);
250 if (last_cpu >= max_cpus - 1)
252 cpup = &cpus[++last_cpu];
254 cpup->domain = domain;
261 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
263 ACPI_SRAT_CPU_AFFINITY *cpu;
264 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
265 ACPI_SRAT_MEM_AFFINITY *mem;
266 ACPI_SRAT_GICC_AFFINITY *gicc;
267 static struct cpu_info *cpup;
270 switch (entry->Type) {
271 case ACPI_SRAT_TYPE_CPU_AFFINITY:
272 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
273 domain = cpu->ProximityDomainLo |
274 cpu->ProximityDomainHi[0] << 8 |
275 cpu->ProximityDomainHi[1] << 16 |
276 cpu->ProximityDomainHi[2] << 24;
278 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
280 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
281 "enabled" : "disabled");
282 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
284 cpup = cpu_find(cpu->ApicId);
286 printf("SRAT: Duplicate local APIC ID %u\n",
291 cpup = cpu_add(cpu->ApicId, domain);
293 printf("SRAT: Ignoring local APIC ID %u (too high)\n",
296 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
297 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
299 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
300 x2apic->ApicId, x2apic->ProximityDomain,
301 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
302 "enabled" : "disabled");
303 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
305 KASSERT(cpu_find(x2apic->ApicId) == NULL,
306 ("Duplicate local APIC ID %u", x2apic->ApicId));
307 cpup = cpu_add(x2apic->ApicId, x2apic->ProximityDomain);
309 printf("SRAT: Ignoring local APIC ID %u (too high)\n",
312 case ACPI_SRAT_TYPE_GICC_AFFINITY:
313 gicc = (ACPI_SRAT_GICC_AFFINITY *)entry;
315 printf("SRAT: Found CPU UID %u domain %d: %s\n",
316 gicc->AcpiProcessorUid, gicc->ProximityDomain,
317 (gicc->Flags & ACPI_SRAT_GICC_ENABLED) ?
318 "enabled" : "disabled");
319 if (!(gicc->Flags & ACPI_SRAT_GICC_ENABLED))
321 KASSERT(cpu_find(gicc->AcpiProcessorUid) == NULL,
322 ("Duplicate CPU UID %u", gicc->AcpiProcessorUid));
323 cpup = cpu_add(gicc->AcpiProcessorUid, gicc->ProximityDomain);
325 printf("SRAT: Ignoring CPU UID %u (too high)\n",
326 gicc->AcpiProcessorUid);
328 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
329 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
332 "SRAT: Found memory domain %d addr 0x%jx len 0x%jx: %s\n",
333 mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
334 (uintmax_t)mem->Length,
335 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
336 "enabled" : "disabled");
337 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
339 if (mem->BaseAddress >= maxphyaddr ||
340 !overlaps_phys_avail(mem->BaseAddress,
341 mem->BaseAddress + mem->Length)) {
342 printf("SRAT: Ignoring memory at addr 0x%jx\n",
343 (uintmax_t)mem->BaseAddress);
346 if (num_mem == VM_PHYSSEG_MAX) {
347 printf("SRAT: Too many memory regions\n");
352 for (i = 0; i < num_mem; i++) {
353 if (mem_info[i].end <= mem->BaseAddress)
355 if (mem_info[i].start <
356 (mem->BaseAddress + mem->Length)) {
357 printf("SRAT: Overlapping memory entries\n");
363 for (i = num_mem; i > slot; i--)
364 mem_info[i] = mem_info[i - 1];
365 mem_info[slot].start = mem->BaseAddress;
366 mem_info[slot].end = mem->BaseAddress + mem->Length;
367 mem_info[slot].domain = mem->ProximityDomain;
374 * Ensure each memory domain has at least one CPU and that each CPU
375 * has at least one memory domain.
382 for (i = 0; i < num_mem; i++) {
384 for (j = 0; j <= last_cpu; j++)
385 if (cpus[j].enabled &&
386 cpus[j].domain == mem_info[i].domain) {
387 cpus[j].has_memory = 1;
391 printf("SRAT: No CPU found for memory domain %d\n",
396 for (i = 0; i <= last_cpu; i++)
397 if (cpus[i].enabled && !cpus[i].has_memory) {
399 for (j = 0; j < num_mem && !found; j++) {
400 if (mem_info[j].domain == cpus[i].domain)
405 printf("SRAT: mem dom %d is empty\n",
407 mem_info[num_mem].start = 0;
408 mem_info[num_mem].end = 0;
409 mem_info[num_mem].domain = cpus[i].domain;
417 * Check that the SRAT memory regions cover all of the regions in
421 check_phys_avail(void)
426 /* j is the current offset into phys_avail[]. */
427 address = phys_avail[0];
429 for (i = 0; i < num_mem; i++) {
431 * Consume as many phys_avail[] entries as fit in this
434 while (address >= mem_info[i].start &&
435 address <= mem_info[i].end) {
437 * If we cover the rest of this phys_avail[] entry,
438 * advance to the next entry.
440 if (phys_avail[j + 1] <= mem_info[i].end) {
442 if (phys_avail[j] == 0 &&
443 phys_avail[j + 1] == 0) {
446 address = phys_avail[j];
448 address = mem_info[i].end + 1;
451 printf("SRAT: No memory region found for 0x%jx - 0x%jx\n",
452 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
457 * Renumber the memory domains to be compact and zero-based if not
458 * already. Returns an error if there are too many domains.
461 renumber_domains(void)
465 /* Enumerate all the domains. */
467 for (i = 0; i < num_mem; i++) {
468 /* See if this domain is already known. */
469 for (j = 0; j < ndomain; j++) {
470 if (domain_pxm[j] >= mem_info[i].domain)
473 if (j < ndomain && domain_pxm[j] == mem_info[i].domain)
476 if (ndomain >= MAXMEMDOM) {
478 printf("SRAT: Too many memory domains\n");
482 /* Insert the new domain at slot 'j'. */
484 for (j = ndomain; j > slot; j--)
485 domain_pxm[j] = domain_pxm[j - 1];
486 domain_pxm[slot] = mem_info[i].domain;
490 /* Renumber each domain to its index in the sorted 'domain_pxm' list. */
491 for (i = 0; i < ndomain; i++) {
493 * If the domain is already the right value, no need
496 if (domain_pxm[i] == i)
499 /* Walk the cpu[] and mem_info[] arrays to renumber. */
500 for (j = 0; j < num_mem; j++)
501 if (mem_info[j].domain == domain_pxm[i])
502 mem_info[j].domain = i;
503 for (j = 0; j <= last_cpu; j++)
504 if (cpus[j].enabled && cpus[j].domain == domain_pxm[i])
512 * Look for an ACPI System Resource Affinity Table ("SRAT"),
513 * allocate space for cpu information, and initialize globals.
516 acpi_pxm_init(int ncpus, vm_paddr_t maxphys)
518 unsigned int idx, size;
521 if (resource_disabled("srat", 0))
526 maxphyaddr = maxphys;
527 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
528 if (srat_physaddr == 0)
532 * Allocate data structure:
534 * Find the last physical memory region and steal some memory from
535 * it. This is done because at this point in the boot process
536 * malloc is still not usable.
538 for (idx = 0; phys_avail[idx + 1] != 0; idx += 2);
539 KASSERT(idx != 0, ("phys_avail is empty!"));
542 size = sizeof(*cpus) * max_cpus;
543 addr = trunc_page(phys_avail[idx + 1] - size);
544 KASSERT(addr >= phys_avail[idx],
545 ("Not enough memory for SRAT table items"));
546 phys_avail[idx + 1] = addr - 1;
549 * We cannot rely on PHYS_TO_DMAP because this code is also used in
550 * i386, so use pmap_mapbios to map the memory, this will end up using
551 * the default memory attribute (WB), and the DMAP when available.
553 cpus = (struct cpu_info *)pmap_mapbios(addr, size);
564 * Make a pass over the table to populate the cpus[] and
567 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
569 srat_walk_table(srat_parse_entry, &error);
570 acpi_unmap_table(srat);
572 if (error || check_domains() != 0 || check_phys_avail() != 0 ||
573 renumber_domains() != 0) {
582 init_mem_locality(void)
587 * For now, assume -1 == "no locality information for
590 for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++)
591 vm_locality_table[i] = -1;
595 * Parse SRAT and SLIT to save proximity info. Don't do
596 * anything if SRAT is not available.
599 acpi_pxm_parse_tables(void)
602 if (srat_physaddr == 0)
604 if (parse_srat() < 0)
611 * Use saved data from SRAT/SLIT to update memory locality.
614 acpi_pxm_set_mem_locality(void)
617 if (srat_physaddr == 0)
619 vm_phys_register_domains(ndomain, mem_info, vm_locality_table);
623 srat_walk_table(acpi_subtable_handler *handler, void *arg)
626 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
631 * Setup per-CPU domain IDs from information saved in 'cpus'.
634 acpi_pxm_set_cpu_locality(void)
636 struct cpu_info *cpu;
640 if (srat_physaddr == 0)
642 for (i = 0; i < MAXCPU; i++) {
646 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
647 cpu = cpu_get_info(pc);
648 pc->pc_domain = vm_ndomains > 1 ? cpu->domain : 0;
649 CPU_SET(i, &cpuset_domain[pc->pc_domain]);
651 printf("SRAT: CPU %u has memory domain %d\n", i,
657 acpi_pxm_get_cpu_locality(int apic_id)
659 struct cpu_info *cpu;
661 cpu = cpu_find(apic_id);
663 panic("SRAT: CPU with ID %u is not known", apic_id);
664 return (cpu->domain);
668 * Free data structures allocated during acpi_pxm_init.
674 if (srat_physaddr == 0)
676 pmap_unmapbios((vm_offset_t)cpus, sizeof(*cpus) * max_cpus);
682 * Map a _PXM value to a VM domain ID.
684 * Returns the domain ID, or -1 if no domain ID was found.
687 acpi_map_pxm_to_vm_domainid(int pxm)
691 for (i = 0; i < ndomain; i++) {
692 if (domain_pxm[i] == pxm)
693 return (vm_ndomains > 1 ? i : 0);
699 #else /* MAXMEMDOM == 1 */
702 acpi_map_pxm_to_vm_domainid(int pxm)
708 #endif /* MAXMEMDOM > 1 */