2 * Copyright (c) 2010 Hudson River Trading LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/mutex.h>
40 #include <sys/vmmeter.h>
43 #include <vm/vm_param.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_phys.h>
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/aclocal.h>
49 #include <contrib/dev/acpica/include/actables.h>
51 #include <machine/intr_machdep.h>
52 #include <x86/apicvar.h>
54 #include <dev/acpica/acpivar.h>
61 } cpus[MAX_APIC_ID + 1];
63 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
66 static ACPI_TABLE_SRAT *srat;
67 static vm_paddr_t srat_physaddr;
69 static int domain_pxm[MAXMEMDOM];
72 static ACPI_TABLE_SLIT *slit;
73 static vm_paddr_t slit_physaddr;
74 static int vm_locality_table[MAXMEMDOM * MAXMEMDOM];
76 static void srat_walk_table(acpi_subtable_handler *handler, void *arg);
83 slit_parse_table(ACPI_TABLE_SLIT *s)
86 int i_domain, j_domain;
91 * This maps the SLIT data into the VM-domain centric view.
92 * There may be sparse entries in the PXM namespace, so
93 * remap them to a VM-domain ID and if it doesn't exist,
96 * It should result in a packed 2d array of VM-domain
97 * locality information entries.
101 printf("SLIT.Localities: %d\n", (int) s->LocalityCount);
102 for (i = 0; i < s->LocalityCount; i++) {
103 i_domain = acpi_map_pxm_to_vm_domainid(i);
109 for (j = 0; j < s->LocalityCount; j++) {
110 j_domain = acpi_map_pxm_to_vm_domainid(j);
113 e = s->Entry[i * s->LocalityCount + j];
115 printf("%d ", (int) e);
116 /* 255 == "no locality information" */
118 vm_locality_table[offset] = -1;
120 vm_locality_table[offset] = e;
129 * Look for an ACPI System Locality Distance Information Table ("SLIT")
135 if (resource_disabled("slit", 0)) {
139 slit_physaddr = acpi_find_table(ACPI_SIG_SLIT);
140 if (slit_physaddr == 0) {
145 * Make a pass over the table to populate the cpus[] and
148 slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT);
149 slit_parse_table(slit);
150 acpi_unmap_table(slit);
154 /* Tell the VM about it! */
155 mem_locality = vm_locality_table;
165 * Returns true if a memory range overlaps with at least one range in
169 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end)
173 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) {
174 if (phys_avail[i + 1] < start)
176 if (phys_avail[i] < end)
185 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
187 ACPI_SRAT_CPU_AFFINITY *cpu;
188 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
189 ACPI_SRAT_MEM_AFFINITY *mem;
192 switch (entry->Type) {
193 case ACPI_SRAT_TYPE_CPU_AFFINITY:
194 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
195 domain = cpu->ProximityDomainLo |
196 cpu->ProximityDomainHi[0] << 8 |
197 cpu->ProximityDomainHi[1] << 16 |
198 cpu->ProximityDomainHi[2] << 24;
200 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
202 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
203 "enabled" : "disabled");
204 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
206 if (cpu->ApicId > MAX_APIC_ID) {
207 printf("SRAT: Ignoring local APIC ID %u (too high)\n",
212 if (cpus[cpu->ApicId].enabled) {
213 printf("SRAT: Duplicate local APIC ID %u\n",
218 cpus[cpu->ApicId].domain = domain;
219 cpus[cpu->ApicId].enabled = 1;
221 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
222 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
224 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
225 x2apic->ApicId, x2apic->ProximityDomain,
226 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
227 "enabled" : "disabled");
228 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
230 if (x2apic->ApicId > MAX_APIC_ID) {
231 printf("SRAT: Ignoring local APIC ID %u (too high)\n",
236 KASSERT(!cpus[x2apic->ApicId].enabled,
237 ("Duplicate local APIC ID %u", x2apic->ApicId));
238 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
239 cpus[x2apic->ApicId].enabled = 1;
241 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
242 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
245 "SRAT: Found memory domain %d addr 0x%jx len 0x%jx: %s\n",
246 mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
247 (uintmax_t)mem->Length,
248 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
249 "enabled" : "disabled");
250 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
252 if (!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);
258 if (num_mem == VM_PHYSSEG_MAX) {
259 printf("SRAT: Too many memory regions\n");
264 for (i = 0; i < num_mem; i++) {
265 if (mem_info[i].end <= mem->BaseAddress)
267 if (mem_info[i].start <
268 (mem->BaseAddress + mem->Length)) {
269 printf("SRAT: Overlapping memory entries\n");
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;
286 * Ensure each memory domain has at least one CPU and that each CPU
287 * has at least one memory domain.
294 for (i = 0; i < num_mem; i++) {
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;
303 printf("SRAT: No CPU found for memory domain %d\n",
308 for (i = 0; i <= MAX_APIC_ID; i++)
309 if (cpus[i].enabled && !cpus[i].has_memory) {
310 printf("SRAT: No memory found for CPU %d\n", i);
317 * Check that the SRAT memory regions cover all of the regions in
321 check_phys_avail(void)
326 /* j is the current offset into phys_avail[]. */
327 address = phys_avail[0];
329 for (i = 0; i < num_mem; i++) {
331 * Consume as many phys_avail[] entries as fit in this
334 while (address >= mem_info[i].start &&
335 address <= mem_info[i].end) {
337 * If we cover the rest of this phys_avail[] entry,
338 * advance to the next entry.
340 if (phys_avail[j + 1] <= mem_info[i].end) {
342 if (phys_avail[j] == 0 &&
343 phys_avail[j + 1] == 0) {
346 address = phys_avail[j];
348 address = mem_info[i].end + 1;
351 printf("SRAT: No memory region found for 0x%jx - 0x%jx\n",
352 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
357 * Renumber the memory domains to be compact and zero-based if not
358 * already. Returns an error if there are too many domains.
361 renumber_domains(void)
365 /* Enumerate all the domains. */
367 for (i = 0; i < num_mem; i++) {
368 /* See if this domain is already known. */
369 for (j = 0; j < ndomain; j++) {
370 if (domain_pxm[j] >= mem_info[i].domain)
373 if (j < ndomain && domain_pxm[j] == mem_info[i].domain)
376 if (ndomain >= MAXMEMDOM) {
378 printf("SRAT: Too many memory domains\n");
382 /* Insert the new domain at slot 'j'. */
384 for (j = ndomain; j > slot; j--)
385 domain_pxm[j] = domain_pxm[j - 1];
386 domain_pxm[slot] = mem_info[i].domain;
390 /* Renumber each domain to its index in the sorted 'domain_pxm' list. */
391 for (i = 0; i < ndomain; i++) {
393 * If the domain is already the right value, no need
396 if (domain_pxm[i] == i)
399 /* Walk the cpu[] and mem_info[] arrays to renumber. */
400 for (j = 0; j < num_mem; j++)
401 if (mem_info[j].domain == domain_pxm[i])
402 mem_info[j].domain = i;
403 for (j = 0; j <= MAX_APIC_ID; j++)
404 if (cpus[j].enabled && cpus[j].domain == domain_pxm[i])
412 * Look for an ACPI System Resource Affinity Table ("SRAT")
419 if (resource_disabled("srat", 0))
422 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
423 if (srat_physaddr == 0)
427 * Make a pass over the table to populate the cpus[] and
430 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
432 srat_walk_table(srat_parse_entry, &error);
433 acpi_unmap_table(srat);
435 if (error || check_domains() != 0 || check_phys_avail() != 0 ||
436 renumber_domains() != 0) {
442 /* Point vm_phys at our memory affinity table. */
443 vm_ndomains = ndomain;
444 mem_affinity = mem_info;
451 init_mem_locality(void)
456 * For now, assume -1 == "no locality information for
459 for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++)
460 vm_locality_table[i] = -1;
464 parse_acpi_tables(void *dummy)
467 if (parse_srat() < 0)
472 SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables,
476 srat_walk_table(acpi_subtable_handler *handler, void *arg)
479 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
484 * Setup per-CPU domain IDs.
487 srat_set_cpus(void *dummy)
489 struct cpu_info *cpu;
493 if (srat_physaddr == 0)
495 for (i = 0; i < MAXCPU; i++) {
499 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
500 cpu = &cpus[pc->pc_apic_id];
502 panic("SRAT: CPU with APIC ID %u is not known",
504 pc->pc_domain = cpu->domain;
505 CPU_SET(i, &cpuset_domain[cpu->domain]);
507 printf("SRAT: CPU %u has memory domain %d\n", i,
511 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);
514 * Map a _PXM value to a VM domain ID.
516 * Returns the domain ID, or -1 if no domain ID was found.
519 acpi_map_pxm_to_vm_domainid(int pxm)
523 for (i = 0; i < ndomain; i++) {
524 if (domain_pxm[i] == pxm)
531 #else /* MAXMEMDOM == 1 */
534 acpi_map_pxm_to_vm_domainid(int pxm)
540 #endif /* MAXMEMDOM > 1 */