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/aclocal.h>
48 #include <contrib/dev/acpica/include/actables.h>
50 #include <machine/intr_machdep.h>
51 #include <x86/apicvar.h>
53 #include <dev/acpica/acpivar.h>
60 } cpus[MAX_APIC_ID + 1];
62 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
65 static ACPI_TABLE_SRAT *srat;
66 static vm_paddr_t srat_physaddr;
68 static int domain_pxm[MAXMEMDOM];
71 static ACPI_TABLE_SLIT *slit;
72 static vm_paddr_t slit_physaddr;
73 static int vm_locality_table[MAXMEMDOM * MAXMEMDOM];
75 static void srat_walk_table(acpi_subtable_handler *handler, void *arg);
82 slit_parse_table(ACPI_TABLE_SLIT *s)
85 int i_domain, j_domain;
90 * This maps the SLIT data into the VM-domain centric view.
91 * There may be sparse entries in the PXM namespace, so
92 * remap them to a VM-domain ID and if it doesn't exist,
95 * It should result in a packed 2d array of VM-domain
96 * locality information entries.
100 printf("SLIT.Localities: %d\n", (int) s->LocalityCount);
101 for (i = 0; i < s->LocalityCount; i++) {
102 i_domain = acpi_map_pxm_to_vm_domainid(i);
108 for (j = 0; j < s->LocalityCount; j++) {
109 j_domain = acpi_map_pxm_to_vm_domainid(j);
112 e = s->Entry[i * s->LocalityCount + j];
114 printf("%d ", (int) e);
115 /* 255 == "no locality information" */
117 vm_locality_table[offset] = -1;
119 vm_locality_table[offset] = e;
128 * Look for an ACPI System Locality Distance Information Table ("SLIT")
134 if (resource_disabled("slit", 0)) {
138 slit_physaddr = acpi_find_table(ACPI_SIG_SLIT);
139 if (slit_physaddr == 0) {
144 * Make a pass over the table to populate the cpus[] and
147 slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT);
148 slit_parse_table(slit);
149 acpi_unmap_table(slit);
153 /* Tell the VM about it! */
154 mem_locality = vm_locality_table;
164 * Returns true if a memory range overlaps with at least one range in
168 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end)
172 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) {
173 if (phys_avail[i + 1] < start)
175 if (phys_avail[i] < end)
184 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
186 ACPI_SRAT_CPU_AFFINITY *cpu;
187 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
188 ACPI_SRAT_MEM_AFFINITY *mem;
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;
199 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
201 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
202 "enabled" : "disabled");
203 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
205 if (cpus[cpu->ApicId].enabled) {
206 printf("SRAT: Duplicate local APIC ID %u\n",
211 cpus[cpu->ApicId].domain = domain;
212 cpus[cpu->ApicId].enabled = 1;
214 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
215 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
217 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
218 x2apic->ApicId, x2apic->ProximityDomain,
219 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
220 "enabled" : "disabled");
221 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
223 KASSERT(!cpus[x2apic->ApicId].enabled,
224 ("Duplicate local APIC ID %u", x2apic->ApicId));
225 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
226 cpus[x2apic->ApicId].enabled = 1;
228 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
229 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
232 "SRAT: Found memory domain %d addr %jx len %jx: %s\n",
233 mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
234 (uintmax_t)mem->Length,
235 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
236 "enabled" : "disabled");
237 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
239 if (!overlaps_phys_avail(mem->BaseAddress,
240 mem->BaseAddress + mem->Length)) {
241 printf("SRAT: Ignoring memory at addr %jx\n",
242 (uintmax_t)mem->BaseAddress);
245 if (num_mem == VM_PHYSSEG_MAX) {
246 printf("SRAT: Too many memory regions\n");
251 for (i = 0; i < num_mem; i++) {
252 if (mem_info[i].end <= mem->BaseAddress)
254 if (mem_info[i].start <
255 (mem->BaseAddress + mem->Length)) {
256 printf("SRAT: Overlapping memory entries\n");
262 for (i = num_mem; i > slot; i--)
263 mem_info[i] = mem_info[i - 1];
264 mem_info[slot].start = mem->BaseAddress;
265 mem_info[slot].end = mem->BaseAddress + mem->Length;
266 mem_info[slot].domain = mem->ProximityDomain;
273 * Ensure each memory domain has at least one CPU and that each CPU
274 * has at least one memory domain.
281 for (i = 0; i < num_mem; i++) {
283 for (j = 0; j <= MAX_APIC_ID; j++)
284 if (cpus[j].enabled &&
285 cpus[j].domain == mem_info[i].domain) {
286 cpus[j].has_memory = 1;
290 printf("SRAT: No CPU found for memory domain %d\n",
295 for (i = 0; i <= MAX_APIC_ID; i++)
296 if (cpus[i].enabled && !cpus[i].has_memory) {
297 printf("SRAT: No memory found for CPU %d\n", i);
304 * Check that the SRAT memory regions cover all of the regions in
308 check_phys_avail(void)
313 /* j is the current offset into phys_avail[]. */
314 address = phys_avail[0];
316 for (i = 0; i < num_mem; i++) {
318 * Consume as many phys_avail[] entries as fit in this
321 while (address >= mem_info[i].start &&
322 address <= mem_info[i].end) {
324 * If we cover the rest of this phys_avail[] entry,
325 * advance to the next entry.
327 if (phys_avail[j + 1] <= mem_info[i].end) {
329 if (phys_avail[j] == 0 &&
330 phys_avail[j + 1] == 0) {
333 address = phys_avail[j];
335 address = mem_info[i].end + 1;
338 printf("SRAT: No memory region found for %jx - %jx\n",
339 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
344 * Renumber the memory domains to be compact and zero-based if not
345 * already. Returns an error if there are too many domains.
348 renumber_domains(void)
352 /* Enumerate all the domains. */
354 for (i = 0; i < num_mem; i++) {
355 /* See if this domain is already known. */
356 for (j = 0; j < ndomain; j++) {
357 if (domain_pxm[j] >= mem_info[i].domain)
360 if (j < ndomain && domain_pxm[j] == mem_info[i].domain)
363 if (ndomain >= MAXMEMDOM) {
365 printf("SRAT: Too many memory domains\n");
369 /* Insert the new domain at slot 'j'. */
371 for (j = ndomain; j > slot; j--)
372 domain_pxm[j] = domain_pxm[j - 1];
373 domain_pxm[slot] = mem_info[i].domain;
377 /* Renumber each domain to its index in the sorted 'domain_pxm' list. */
378 for (i = 0; i < ndomain; i++) {
380 * If the domain is already the right value, no need
383 if (domain_pxm[i] == i)
386 /* Walk the cpu[] and mem_info[] arrays to renumber. */
387 for (j = 0; j < num_mem; j++)
388 if (mem_info[j].domain == domain_pxm[i])
389 mem_info[j].domain = i;
390 for (j = 0; j <= MAX_APIC_ID; j++)
391 if (cpus[j].enabled && cpus[j].domain == domain_pxm[i])
399 * Look for an ACPI System Resource Affinity Table ("SRAT")
406 if (resource_disabled("srat", 0))
409 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
410 if (srat_physaddr == 0)
414 * Make a pass over the table to populate the cpus[] and
417 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
419 srat_walk_table(srat_parse_entry, &error);
420 acpi_unmap_table(srat);
422 if (error || check_domains() != 0 || check_phys_avail() != 0 ||
423 renumber_domains() != 0) {
429 /* Point vm_phys at our memory affinity table. */
430 vm_ndomains = ndomain;
431 mem_affinity = mem_info;
438 init_mem_locality(void)
443 * For now, assume -1 == "no locality information for
446 for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++)
447 vm_locality_table[i] = -1;
451 parse_acpi_tables(void *dummy)
454 if (parse_srat() < 0)
459 SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables,
463 srat_walk_table(acpi_subtable_handler *handler, void *arg)
466 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
471 * Setup per-CPU domain IDs.
474 srat_set_cpus(void *dummy)
476 struct cpu_info *cpu;
480 if (srat_physaddr == 0)
482 for (i = 0; i < MAXCPU; i++) {
486 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
487 cpu = &cpus[pc->pc_apic_id];
489 panic("SRAT: CPU with APIC ID %u is not known",
491 pc->pc_domain = cpu->domain;
492 CPU_SET(i, &cpuset_domain[cpu->domain]);
494 printf("SRAT: CPU %u has memory domain %d\n", i,
498 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);
501 * Map a _PXM value to a VM domain ID.
503 * Returns the domain ID, or -1 if no domain ID was found.
506 acpi_map_pxm_to_vm_domainid(int pxm)
510 for (i = 0; i < ndomain; i++) {
511 if (domain_pxm[i] == pxm)
518 #else /* MAXMEMDOM == 1 */
521 acpi_map_pxm_to_vm_domainid(int pxm)
527 #endif /* MAXMEMDOM > 1 */