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>
35 #include <sys/kernel.h>
37 #include <sys/mutex.h>
39 #include <sys/vmmeter.h>
42 #include <vm/vm_param.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_phys.h>
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/actables.h>
49 #include <machine/intr_machdep.h>
50 #include <x86/apicvar.h>
52 #include <dev/acpica/acpivar.h>
59 } cpus[MAX_APIC_ID + 1];
61 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
64 static ACPI_TABLE_SRAT *srat;
65 static vm_paddr_t srat_physaddr;
67 static int domain_pxm[MAXMEMDOM];
70 static ACPI_TABLE_SLIT *slit;
71 static vm_paddr_t slit_physaddr;
72 static int vm_locality_table[MAXMEMDOM * MAXMEMDOM];
74 static void srat_walk_table(acpi_subtable_handler *handler, void *arg);
81 slit_parse_table(ACPI_TABLE_SLIT *s)
84 int i_domain, j_domain;
89 * This maps the SLIT data into the VM-domain centric view.
90 * There may be sparse entries in the PXM namespace, so
91 * remap them to a VM-domain ID and if it doesn't exist,
94 * It should result in a packed 2d array of VM-domain
95 * locality information entries.
99 printf("SLIT.Localities: %d\n", (int) s->LocalityCount);
100 for (i = 0; i < s->LocalityCount; i++) {
101 i_domain = acpi_map_pxm_to_vm_domainid(i);
107 for (j = 0; j < s->LocalityCount; j++) {
108 j_domain = acpi_map_pxm_to_vm_domainid(j);
111 e = s->Entry[i * s->LocalityCount + j];
113 printf("%d ", (int) e);
114 /* 255 == "no locality information" */
116 vm_locality_table[offset] = -1;
118 vm_locality_table[offset] = e;
127 * Look for an ACPI System Locality Distance Information Table ("SLIT")
133 if (resource_disabled("slit", 0)) {
137 slit_physaddr = acpi_find_table(ACPI_SIG_SLIT);
138 if (slit_physaddr == 0) {
143 * Make a pass over the table to populate the cpus[] and
146 slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT);
147 slit_parse_table(slit);
148 acpi_unmap_table(slit);
152 /* Tell the VM about it! */
153 mem_locality = vm_locality_table;
163 * Returns true if a memory range overlaps with at least one range in
167 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end)
171 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) {
172 if (phys_avail[i + 1] < start)
174 if (phys_avail[i] < end)
183 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
185 ACPI_SRAT_CPU_AFFINITY *cpu;
186 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
187 ACPI_SRAT_MEM_AFFINITY *mem;
190 switch (entry->Type) {
191 case ACPI_SRAT_TYPE_CPU_AFFINITY:
192 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
193 domain = cpu->ProximityDomainLo |
194 cpu->ProximityDomainHi[0] << 8 |
195 cpu->ProximityDomainHi[1] << 16 |
196 cpu->ProximityDomainHi[2] << 24;
198 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
200 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
201 "enabled" : "disabled");
202 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
204 if (cpus[cpu->ApicId].enabled) {
205 printf("SRAT: Duplicate local APIC ID %u\n",
210 cpus[cpu->ApicId].domain = domain;
211 cpus[cpu->ApicId].enabled = 1;
213 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
214 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
216 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
217 x2apic->ApicId, x2apic->ProximityDomain,
218 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
219 "enabled" : "disabled");
220 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
222 KASSERT(!cpus[x2apic->ApicId].enabled,
223 ("Duplicate local APIC ID %u", x2apic->ApicId));
224 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
225 cpus[x2apic->ApicId].enabled = 1;
227 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
228 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
231 "SRAT: Found memory domain %d addr %jx len %jx: %s\n",
232 mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
233 (uintmax_t)mem->Length,
234 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
235 "enabled" : "disabled");
236 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
238 if (!overlaps_phys_avail(mem->BaseAddress,
239 mem->BaseAddress + mem->Length)) {
240 printf("SRAT: Ignoring memory at addr %jx\n",
241 (uintmax_t)mem->BaseAddress);
244 if (num_mem == VM_PHYSSEG_MAX) {
245 printf("SRAT: Too many memory regions\n");
250 for (i = 0; i < num_mem; i++) {
251 if (mem_info[i].end <= mem->BaseAddress)
253 if (mem_info[i].start <
254 (mem->BaseAddress + mem->Length)) {
255 printf("SRAT: Overlapping memory entries\n");
261 for (i = num_mem; i > slot; i--)
262 mem_info[i] = mem_info[i - 1];
263 mem_info[slot].start = mem->BaseAddress;
264 mem_info[slot].end = mem->BaseAddress + mem->Length;
265 mem_info[slot].domain = mem->ProximityDomain;
272 * Ensure each memory domain has at least one CPU and that each CPU
273 * has at least one memory domain.
280 for (i = 0; i < num_mem; i++) {
282 for (j = 0; j <= MAX_APIC_ID; j++)
283 if (cpus[j].enabled &&
284 cpus[j].domain == mem_info[i].domain) {
285 cpus[j].has_memory = 1;
289 printf("SRAT: No CPU found for memory domain %d\n",
294 for (i = 0; i <= MAX_APIC_ID; i++)
295 if (cpus[i].enabled && !cpus[i].has_memory) {
296 printf("SRAT: No memory found for CPU %d\n", i);
303 * Check that the SRAT memory regions cover all of the regions in
307 check_phys_avail(void)
312 /* j is the current offset into phys_avail[]. */
313 address = phys_avail[0];
315 for (i = 0; i < num_mem; i++) {
317 * Consume as many phys_avail[] entries as fit in this
320 while (address >= mem_info[i].start &&
321 address <= mem_info[i].end) {
323 * If we cover the rest of this phys_avail[] entry,
324 * advance to the next entry.
326 if (phys_avail[j + 1] <= mem_info[i].end) {
328 if (phys_avail[j] == 0 &&
329 phys_avail[j + 1] == 0) {
332 address = phys_avail[j];
334 address = mem_info[i].end + 1;
337 printf("SRAT: No memory region found for %jx - %jx\n",
338 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
343 * Renumber the memory domains to be compact and zero-based if not
344 * already. Returns an error if there are too many domains.
347 renumber_domains(void)
351 /* Enumerate all the domains. */
353 for (i = 0; i < num_mem; i++) {
354 /* See if this domain is already known. */
355 for (j = 0; j < ndomain; j++) {
356 if (domain_pxm[j] >= mem_info[i].domain)
359 if (j < ndomain && domain_pxm[j] == mem_info[i].domain)
362 if (ndomain >= MAXMEMDOM) {
364 printf("SRAT: Too many memory domains\n");
368 /* Insert the new domain at slot 'j'. */
370 for (j = ndomain; j > slot; j--)
371 domain_pxm[j] = domain_pxm[j - 1];
372 domain_pxm[slot] = mem_info[i].domain;
376 /* Renumber each domain to its index in the sorted 'domain_pxm' list. */
377 for (i = 0; i < ndomain; i++) {
379 * If the domain is already the right value, no need
382 if (domain_pxm[i] == i)
385 /* Walk the cpu[] and mem_info[] arrays to renumber. */
386 for (j = 0; j < num_mem; j++)
387 if (mem_info[j].domain == domain_pxm[i])
388 mem_info[j].domain = i;
389 for (j = 0; j <= MAX_APIC_ID; j++)
390 if (cpus[j].enabled && cpus[j].domain == domain_pxm[i])
398 * Look for an ACPI System Resource Affinity Table ("SRAT")
405 if (resource_disabled("srat", 0))
408 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
409 if (srat_physaddr == 0)
413 * Make a pass over the table to populate the cpus[] and
416 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
418 srat_walk_table(srat_parse_entry, &error);
419 acpi_unmap_table(srat);
421 if (error || check_domains() != 0 || check_phys_avail() != 0 ||
422 renumber_domains() != 0) {
428 /* Point vm_phys at our memory affinity table. */
429 vm_ndomains = ndomain;
430 mem_affinity = mem_info;
437 init_mem_locality(void)
442 * For now, assume -1 == "no locality information for
445 for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++)
446 vm_locality_table[i] = -1;
450 parse_acpi_tables(void *dummy)
453 if (parse_srat() < 0)
458 SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables,
462 srat_walk_table(acpi_subtable_handler *handler, void *arg)
465 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
470 * Setup per-CPU domain IDs.
473 srat_set_cpus(void *dummy)
475 struct cpu_info *cpu;
479 if (srat_physaddr == 0)
481 for (i = 0; i < MAXCPU; i++) {
485 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
486 cpu = &cpus[pc->pc_apic_id];
488 panic("SRAT: CPU with APIC ID %u is not known",
490 pc->pc_domain = cpu->domain;
491 CPU_SET(i, &cpuset_domain[cpu->domain]);
493 printf("SRAT: CPU %u has memory domain %d\n", i,
497 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);
500 * Map a _PXM value to a VM domain ID.
502 * Returns the domain ID, or -1 if no domain ID was found.
505 acpi_map_pxm_to_vm_domainid(int pxm)
509 for (i = 0; i < ndomain; i++) {
510 if (domain_pxm[i] == pxm)
517 #else /* MAXMEMDOM == 1 */
520 acpi_map_pxm_to_vm_domainid(int pxm)
526 #endif /* MAXMEMDOM > 1 */