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;
268 uint64_t base, length;
271 switch (entry->Type) {
272 case ACPI_SRAT_TYPE_CPU_AFFINITY:
273 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
274 domain = cpu->ProximityDomainLo |
275 cpu->ProximityDomainHi[0] << 8 |
276 cpu->ProximityDomainHi[1] << 16 |
277 cpu->ProximityDomainHi[2] << 24;
279 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
281 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
282 "enabled" : "disabled");
283 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
285 cpup = cpu_find(cpu->ApicId);
287 printf("SRAT: Duplicate local APIC ID %u\n",
292 cpup = cpu_add(cpu->ApicId, domain);
294 printf("SRAT: Ignoring local APIC ID %u (too high)\n",
297 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
298 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
300 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
301 x2apic->ApicId, x2apic->ProximityDomain,
302 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
303 "enabled" : "disabled");
304 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
306 KASSERT(cpu_find(x2apic->ApicId) == NULL,
307 ("Duplicate local APIC ID %u", x2apic->ApicId));
308 cpup = cpu_add(x2apic->ApicId, x2apic->ProximityDomain);
310 printf("SRAT: Ignoring local APIC ID %u (too high)\n",
313 case ACPI_SRAT_TYPE_GICC_AFFINITY:
314 gicc = (ACPI_SRAT_GICC_AFFINITY *)entry;
316 printf("SRAT: Found CPU UID %u domain %d: %s\n",
317 gicc->AcpiProcessorUid, gicc->ProximityDomain,
318 (gicc->Flags & ACPI_SRAT_GICC_ENABLED) ?
319 "enabled" : "disabled");
320 if (!(gicc->Flags & ACPI_SRAT_GICC_ENABLED))
322 KASSERT(cpu_find(gicc->AcpiProcessorUid) == NULL,
323 ("Duplicate CPU UID %u", gicc->AcpiProcessorUid));
324 cpup = cpu_add(gicc->AcpiProcessorUid, gicc->ProximityDomain);
326 printf("SRAT: Ignoring CPU UID %u (too high)\n",
327 gicc->AcpiProcessorUid);
329 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
330 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
331 base = mem->BaseAddress;
332 length = mem->Length;
333 domain = mem->ProximityDomain;
337 "SRAT: Found memory domain %d addr 0x%jx len 0x%jx: %s\n",
338 domain, (uintmax_t)base, (uintmax_t)length,
339 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
340 "enabled" : "disabled");
341 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
343 if (base >= maxphyaddr ||
344 !overlaps_phys_avail(base, base + length)) {
345 printf("SRAT: Ignoring memory at addr 0x%jx\n",
349 if (num_mem == VM_PHYSSEG_MAX) {
350 printf("SRAT: Too many memory regions\n");
355 for (i = 0; i < num_mem; i++) {
356 if (mem_info[i].domain == domain) {
357 /* Try to extend an existing segment. */
358 if (base == mem_info[i].end) {
359 mem_info[i].end += length;
362 if (base + length == mem_info[i].start) {
363 mem_info[i].start -= length;
367 if (mem_info[i].end <= base)
369 if (mem_info[i].start < base + length) {
370 printf("SRAT: Overlapping memory entries\n");
376 for (i = num_mem; i > slot; i--)
377 mem_info[i] = mem_info[i - 1];
378 mem_info[slot].start = base;
379 mem_info[slot].end = base + length;
380 mem_info[slot].domain = domain;
387 * Ensure each memory domain has at least one CPU and that each CPU
388 * has at least one memory domain.
395 for (i = 0; i < num_mem; i++) {
397 for (j = 0; j <= last_cpu; j++)
398 if (cpus[j].enabled &&
399 cpus[j].domain == mem_info[i].domain) {
400 cpus[j].has_memory = 1;
404 printf("SRAT: No CPU found for memory domain %d\n",
409 for (i = 0; i <= last_cpu; i++)
410 if (cpus[i].enabled && !cpus[i].has_memory) {
412 for (j = 0; j < num_mem && !found; j++) {
413 if (mem_info[j].domain == cpus[i].domain)
418 printf("SRAT: mem dom %d is empty\n",
420 mem_info[num_mem].start = 0;
421 mem_info[num_mem].end = 0;
422 mem_info[num_mem].domain = cpus[i].domain;
430 * Check that the SRAT memory regions cover all of the regions in
434 check_phys_avail(void)
439 /* j is the current offset into phys_avail[]. */
440 address = phys_avail[0];
442 for (i = 0; i < num_mem; i++) {
444 * Consume as many phys_avail[] entries as fit in this
447 while (address >= mem_info[i].start &&
448 address <= mem_info[i].end) {
450 * If we cover the rest of this phys_avail[] entry,
451 * advance to the next entry.
453 if (phys_avail[j + 1] <= mem_info[i].end) {
455 if (phys_avail[j] == 0 &&
456 phys_avail[j + 1] == 0) {
459 address = phys_avail[j];
461 address = mem_info[i].end + 1;
464 printf("SRAT: No memory region found for 0x%jx - 0x%jx\n",
465 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
470 * Renumber the memory domains to be compact and zero-based if not
471 * already. Returns an error if there are too many domains.
474 renumber_domains(void)
478 /* Enumerate all the domains. */
480 for (i = 0; i < num_mem; i++) {
481 /* See if this domain is already known. */
482 for (j = 0; j < ndomain; j++) {
483 if (domain_pxm[j] >= mem_info[i].domain)
486 if (j < ndomain && domain_pxm[j] == mem_info[i].domain)
489 if (ndomain >= MAXMEMDOM) {
491 printf("SRAT: Too many memory domains\n");
495 /* Insert the new domain at slot 'j'. */
497 for (j = ndomain; j > slot; j--)
498 domain_pxm[j] = domain_pxm[j - 1];
499 domain_pxm[slot] = mem_info[i].domain;
503 /* Renumber each domain to its index in the sorted 'domain_pxm' list. */
504 for (i = 0; i < ndomain; i++) {
506 * If the domain is already the right value, no need
509 if (domain_pxm[i] == i)
512 /* Walk the cpu[] and mem_info[] arrays to renumber. */
513 for (j = 0; j < num_mem; j++)
514 if (mem_info[j].domain == domain_pxm[i])
515 mem_info[j].domain = i;
516 for (j = 0; j <= last_cpu; j++)
517 if (cpus[j].enabled && cpus[j].domain == domain_pxm[i])
525 * Look for an ACPI System Resource Affinity Table ("SRAT"),
526 * allocate space for cpu information, and initialize globals.
529 acpi_pxm_init(int ncpus, vm_paddr_t maxphys)
531 unsigned int idx, size;
534 if (resource_disabled("srat", 0))
539 maxphyaddr = maxphys;
540 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
541 if (srat_physaddr == 0)
545 * Allocate data structure:
547 * Find the last physical memory region and steal some memory from
548 * it. This is done because at this point in the boot process
549 * malloc is still not usable.
551 for (idx = 0; phys_avail[idx + 1] != 0; idx += 2);
552 KASSERT(idx != 0, ("phys_avail is empty!"));
555 size = sizeof(*cpus) * max_cpus;
556 addr = trunc_page(phys_avail[idx + 1] - size);
557 KASSERT(addr >= phys_avail[idx],
558 ("Not enough memory for SRAT table items"));
559 phys_avail[idx + 1] = addr - 1;
562 * We cannot rely on PHYS_TO_DMAP because this code is also used in
563 * i386, so use pmap_mapbios to map the memory, this will end up using
564 * the default memory attribute (WB), and the DMAP when available.
566 cpus = (struct cpu_info *)pmap_mapbios(addr, size);
577 * Make a pass over the table to populate the cpus[] and
580 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
582 srat_walk_table(srat_parse_entry, &error);
583 acpi_unmap_table(srat);
585 if (error || check_domains() != 0 || check_phys_avail() != 0 ||
586 renumber_domains() != 0) {
595 init_mem_locality(void)
600 * For now, assume -1 == "no locality information for
603 for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++)
604 vm_locality_table[i] = -1;
608 * Parse SRAT and SLIT to save proximity info. Don't do
609 * anything if SRAT is not available.
612 acpi_pxm_parse_tables(void)
615 if (srat_physaddr == 0)
617 if (parse_srat() < 0)
624 * Use saved data from SRAT/SLIT to update memory locality.
627 acpi_pxm_set_mem_locality(void)
630 if (srat_physaddr == 0)
632 vm_phys_register_domains(ndomain, mem_info, vm_locality_table);
636 srat_walk_table(acpi_subtable_handler *handler, void *arg)
639 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
644 * Set up per-CPU domain IDs from information saved in 'cpus' and tear down data
645 * structures allocated by acpi_pxm_init().
648 acpi_pxm_set_cpu_locality(void)
650 struct cpu_info *cpu;
654 if (srat_physaddr == 0)
656 for (i = 0; i < MAXCPU; i++) {
660 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
661 cpu = cpu_get_info(pc);
662 pc->pc_domain = vm_ndomains > 1 ? cpu->domain : 0;
663 CPU_SET(i, &cpuset_domain[pc->pc_domain]);
665 printf("SRAT: CPU %u has memory domain %d\n", i,
668 /* XXXMJ the page is leaked. */
669 pmap_unmapbios((vm_offset_t)cpus, sizeof(*cpus) * max_cpus);
675 acpi_pxm_get_cpu_locality(int apic_id)
677 struct cpu_info *cpu;
679 cpu = cpu_find(apic_id);
681 panic("SRAT: CPU with ID %u is not known", apic_id);
682 return (cpu->domain);
686 * Map a _PXM value to a VM domain ID.
688 * Returns the domain ID, or -1 if no domain ID was found.
691 acpi_map_pxm_to_vm_domainid(int pxm)
695 for (i = 0; i < ndomain; i++) {
696 if (domain_pxm[i] == pxm)
697 return (vm_ndomains > 1 ? i : 0);
703 #else /* MAXMEMDOM == 1 */
706 acpi_map_pxm_to_vm_domainid(int pxm)
712 #endif /* MAXMEMDOM > 1 */