xref: /freebsd/sys/x86/acpica/srat.c (revision 83c9dea1bac40c7c7cbde4ccb3d747134311ab5a)
1dd540b46SJohn Baldwin /*-
2179fa75eSJohn Baldwin  * Copyright (c) 2010 Hudson River Trading LLC
3dd540b46SJohn Baldwin  * Written by: John H. Baldwin <jhb@FreeBSD.org>
4dd540b46SJohn Baldwin  * All rights reserved.
5dd540b46SJohn Baldwin  *
6dd540b46SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
7dd540b46SJohn Baldwin  * modification, are permitted provided that the following conditions
8dd540b46SJohn Baldwin  * are met:
9dd540b46SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
10dd540b46SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
11dd540b46SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
12dd540b46SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
13dd540b46SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
14dd540b46SJohn Baldwin  *
15dd540b46SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16dd540b46SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17dd540b46SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18dd540b46SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19dd540b46SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20dd540b46SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21dd540b46SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22dd540b46SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23dd540b46SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24dd540b46SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25dd540b46SJohn Baldwin  * SUCH DAMAGE.
26dd540b46SJohn Baldwin  */
27dd540b46SJohn Baldwin 
28dd540b46SJohn Baldwin #include <sys/cdefs.h>
29dd540b46SJohn Baldwin __FBSDID("$FreeBSD$");
30dd540b46SJohn Baldwin 
3162d70a81SJohn Baldwin #include "opt_vm.h"
3262d70a81SJohn Baldwin 
33dd540b46SJohn Baldwin #include <sys/param.h>
34*83c9dea1SGleb Smirnoff #include <sys/systm.h>
35dd540b46SJohn Baldwin #include <sys/bus.h>
36dd540b46SJohn Baldwin #include <sys/kernel.h>
37449c2e92SKonstantin Belousov #include <sys/lock.h>
38449c2e92SKonstantin Belousov #include <sys/mutex.h>
39dd540b46SJohn Baldwin #include <sys/smp.h>
40449c2e92SKonstantin Belousov #include <sys/vmmeter.h>
41dd540b46SJohn Baldwin #include <vm/vm.h>
42dd540b46SJohn Baldwin #include <vm/pmap.h>
43dd540b46SJohn Baldwin #include <vm/vm_param.h>
44449c2e92SKonstantin Belousov #include <vm/vm_page.h>
45dd540b46SJohn Baldwin #include <vm/vm_phys.h>
46dd540b46SJohn Baldwin 
47dd540b46SJohn Baldwin #include <contrib/dev/acpica/include/acpi.h>
48493deb39SJung-uk Kim #include <contrib/dev/acpica/include/aclocal.h>
49dd540b46SJohn Baldwin #include <contrib/dev/acpica/include/actables.h>
50dd540b46SJohn Baldwin 
51dd540b46SJohn Baldwin #include <machine/intr_machdep.h>
52e07ef9b0SJohn Baldwin #include <x86/apicvar.h>
53dd540b46SJohn Baldwin 
54dd540b46SJohn Baldwin #include <dev/acpica/acpivar.h>
55dd540b46SJohn Baldwin 
56941646f5SAttilio Rao #if MAXMEMDOM > 1
57dd540b46SJohn Baldwin struct cpu_info {
58dd540b46SJohn Baldwin 	int enabled:1;
59dd540b46SJohn Baldwin 	int has_memory:1;
60dd540b46SJohn Baldwin 	int domain;
61dd540b46SJohn Baldwin } cpus[MAX_APIC_ID + 1];
62dd540b46SJohn Baldwin 
63dd540b46SJohn Baldwin struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
64dd540b46SJohn Baldwin int num_mem;
65dd540b46SJohn Baldwin 
66dd540b46SJohn Baldwin static ACPI_TABLE_SRAT *srat;
67dd540b46SJohn Baldwin static vm_paddr_t srat_physaddr;
68dd540b46SJohn Baldwin 
6962d70a81SJohn Baldwin static int domain_pxm[MAXMEMDOM];
7062d70a81SJohn Baldwin static int ndomain;
71fc4f524aSAdrian Chadd 
72415d7ccaSAdrian Chadd static ACPI_TABLE_SLIT *slit;
73415d7ccaSAdrian Chadd static vm_paddr_t slit_physaddr;
74415d7ccaSAdrian Chadd static int vm_locality_table[MAXMEMDOM * MAXMEMDOM];
75415d7ccaSAdrian Chadd 
76dd540b46SJohn Baldwin static void	srat_walk_table(acpi_subtable_handler *handler, void *arg);
77dd540b46SJohn Baldwin 
784d99cfb3SJohn Baldwin /*
79415d7ccaSAdrian Chadd  * SLIT parsing.
80415d7ccaSAdrian Chadd  */
81415d7ccaSAdrian Chadd 
82415d7ccaSAdrian Chadd static void
83415d7ccaSAdrian Chadd slit_parse_table(ACPI_TABLE_SLIT *s)
84415d7ccaSAdrian Chadd {
85415d7ccaSAdrian Chadd 	int i, j;
86415d7ccaSAdrian Chadd 	int i_domain, j_domain;
87415d7ccaSAdrian Chadd 	int offset = 0;
88415d7ccaSAdrian Chadd 	uint8_t e;
89415d7ccaSAdrian Chadd 
90415d7ccaSAdrian Chadd 	/*
91415d7ccaSAdrian Chadd 	 * This maps the SLIT data into the VM-domain centric view.
92415d7ccaSAdrian Chadd 	 * There may be sparse entries in the PXM namespace, so
93415d7ccaSAdrian Chadd 	 * remap them to a VM-domain ID and if it doesn't exist,
94415d7ccaSAdrian Chadd 	 * skip it.
95415d7ccaSAdrian Chadd 	 *
96415d7ccaSAdrian Chadd 	 * It should result in a packed 2d array of VM-domain
97415d7ccaSAdrian Chadd 	 * locality information entries.
98415d7ccaSAdrian Chadd 	 */
99415d7ccaSAdrian Chadd 
100415d7ccaSAdrian Chadd 	if (bootverbose)
101415d7ccaSAdrian Chadd 		printf("SLIT.Localities: %d\n", (int) s->LocalityCount);
102415d7ccaSAdrian Chadd 	for (i = 0; i < s->LocalityCount; i++) {
103415d7ccaSAdrian Chadd 		i_domain = acpi_map_pxm_to_vm_domainid(i);
104415d7ccaSAdrian Chadd 		if (i_domain < 0)
105415d7ccaSAdrian Chadd 			continue;
106415d7ccaSAdrian Chadd 
107415d7ccaSAdrian Chadd 		if (bootverbose)
108415d7ccaSAdrian Chadd 			printf("%d: ", i);
109415d7ccaSAdrian Chadd 		for (j = 0; j < s->LocalityCount; j++) {
110415d7ccaSAdrian Chadd 			j_domain = acpi_map_pxm_to_vm_domainid(j);
111415d7ccaSAdrian Chadd 			if (j_domain < 0)
112415d7ccaSAdrian Chadd 				continue;
113415d7ccaSAdrian Chadd 			e = s->Entry[i * s->LocalityCount + j];
114415d7ccaSAdrian Chadd 			if (bootverbose)
115415d7ccaSAdrian Chadd 				printf("%d ", (int) e);
116415d7ccaSAdrian Chadd 			/* 255 == "no locality information" */
117415d7ccaSAdrian Chadd 			if (e == 255)
118415d7ccaSAdrian Chadd 				vm_locality_table[offset] = -1;
119415d7ccaSAdrian Chadd 			else
120415d7ccaSAdrian Chadd 				vm_locality_table[offset] = e;
121415d7ccaSAdrian Chadd 			offset++;
122415d7ccaSAdrian Chadd 		}
123415d7ccaSAdrian Chadd 		if (bootverbose)
124415d7ccaSAdrian Chadd 			printf("\n");
125415d7ccaSAdrian Chadd 	}
126415d7ccaSAdrian Chadd }
127415d7ccaSAdrian Chadd 
128415d7ccaSAdrian Chadd /*
129415d7ccaSAdrian Chadd  * Look for an ACPI System Locality Distance Information Table ("SLIT")
130415d7ccaSAdrian Chadd  */
131415d7ccaSAdrian Chadd static int
132415d7ccaSAdrian Chadd parse_slit(void)
133415d7ccaSAdrian Chadd {
134415d7ccaSAdrian Chadd 
135415d7ccaSAdrian Chadd 	if (resource_disabled("slit", 0)) {
136415d7ccaSAdrian Chadd 		return (-1);
137415d7ccaSAdrian Chadd 	}
138415d7ccaSAdrian Chadd 
139415d7ccaSAdrian Chadd 	slit_physaddr = acpi_find_table(ACPI_SIG_SLIT);
140415d7ccaSAdrian Chadd 	if (slit_physaddr == 0) {
141415d7ccaSAdrian Chadd 		return (-1);
142415d7ccaSAdrian Chadd 	}
143415d7ccaSAdrian Chadd 
144415d7ccaSAdrian Chadd 	/*
145415d7ccaSAdrian Chadd 	 * Make a pass over the table to populate the cpus[] and
146415d7ccaSAdrian Chadd 	 * mem_info[] tables.
147415d7ccaSAdrian Chadd 	 */
148415d7ccaSAdrian Chadd 	slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT);
149415d7ccaSAdrian Chadd 	slit_parse_table(slit);
150415d7ccaSAdrian Chadd 	acpi_unmap_table(slit);
151415d7ccaSAdrian Chadd 	slit = NULL;
152415d7ccaSAdrian Chadd 
15362d70a81SJohn Baldwin #ifdef VM_NUMA_ALLOC
154415d7ccaSAdrian Chadd 	/* Tell the VM about it! */
155415d7ccaSAdrian Chadd 	mem_locality = vm_locality_table;
15662d70a81SJohn Baldwin #endif
157415d7ccaSAdrian Chadd 	return (0);
158415d7ccaSAdrian Chadd }
159415d7ccaSAdrian Chadd 
160415d7ccaSAdrian Chadd /*
161415d7ccaSAdrian Chadd  * SRAT parsing.
162415d7ccaSAdrian Chadd  */
163415d7ccaSAdrian Chadd 
164415d7ccaSAdrian Chadd /*
1654d99cfb3SJohn Baldwin  * Returns true if a memory range overlaps with at least one range in
1664d99cfb3SJohn Baldwin  * phys_avail[].
1674d99cfb3SJohn Baldwin  */
1684d99cfb3SJohn Baldwin static int
1694d99cfb3SJohn Baldwin overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end)
1704d99cfb3SJohn Baldwin {
1714d99cfb3SJohn Baldwin 	int i;
1724d99cfb3SJohn Baldwin 
1734d99cfb3SJohn Baldwin 	for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) {
1744d99cfb3SJohn Baldwin 		if (phys_avail[i + 1] < start)
1754d99cfb3SJohn Baldwin 			continue;
1764d99cfb3SJohn Baldwin 		if (phys_avail[i] < end)
1774d99cfb3SJohn Baldwin 			return (1);
1784d99cfb3SJohn Baldwin 		break;
1794d99cfb3SJohn Baldwin 	}
1804d99cfb3SJohn Baldwin 	return (0);
1814d99cfb3SJohn Baldwin 
1824d99cfb3SJohn Baldwin }
1834d99cfb3SJohn Baldwin 
184dd540b46SJohn Baldwin static void
185dd540b46SJohn Baldwin srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
186dd540b46SJohn Baldwin {
187dd540b46SJohn Baldwin 	ACPI_SRAT_CPU_AFFINITY *cpu;
188dd540b46SJohn Baldwin 	ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
189dd540b46SJohn Baldwin 	ACPI_SRAT_MEM_AFFINITY *mem;
190dd540b46SJohn Baldwin 	int domain, i, slot;
191dd540b46SJohn Baldwin 
192dd540b46SJohn Baldwin 	switch (entry->Type) {
193dd540b46SJohn Baldwin 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
194dd540b46SJohn Baldwin 		cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
195dd540b46SJohn Baldwin 		domain = cpu->ProximityDomainLo |
196dd540b46SJohn Baldwin 		    cpu->ProximityDomainHi[0] << 8 |
197dd540b46SJohn Baldwin 		    cpu->ProximityDomainHi[1] << 16 |
198dd540b46SJohn Baldwin 		    cpu->ProximityDomainHi[2] << 24;
199dd540b46SJohn Baldwin 		if (bootverbose)
200dd540b46SJohn Baldwin 			printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
201dd540b46SJohn Baldwin 			    cpu->ApicId, domain,
202dd540b46SJohn Baldwin 			    (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
203dd540b46SJohn Baldwin 			    "enabled" : "disabled");
204dd540b46SJohn Baldwin 		if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
205dd540b46SJohn Baldwin 			break;
2066e2ab0aeSRoger Pau Monné 		if (cpu->ApicId > MAX_APIC_ID) {
2076e2ab0aeSRoger Pau Monné 			printf("SRAT: Ignoring local APIC ID %u (too high)\n",
2086e2ab0aeSRoger Pau Monné 			    cpu->ApicId);
2096e2ab0aeSRoger Pau Monné 			break;
2106e2ab0aeSRoger Pau Monné 		}
2116e2ab0aeSRoger Pau Monné 
2122db0699dSEric van Gyzen 		if (cpus[cpu->ApicId].enabled) {
2132db0699dSEric van Gyzen 			printf("SRAT: Duplicate local APIC ID %u\n",
2142db0699dSEric van Gyzen 			    cpu->ApicId);
2152db0699dSEric van Gyzen 			*(int *)arg = ENXIO;
2162db0699dSEric van Gyzen 			break;
2172db0699dSEric van Gyzen 		}
218dd540b46SJohn Baldwin 		cpus[cpu->ApicId].domain = domain;
219dd540b46SJohn Baldwin 		cpus[cpu->ApicId].enabled = 1;
220dd540b46SJohn Baldwin 		break;
221dd540b46SJohn Baldwin 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
222dd540b46SJohn Baldwin 		x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
223dd540b46SJohn Baldwin 		if (bootverbose)
224dd540b46SJohn Baldwin 			printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
225dd540b46SJohn Baldwin 			    x2apic->ApicId, x2apic->ProximityDomain,
226dd540b46SJohn Baldwin 			    (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
227dd540b46SJohn Baldwin 			    "enabled" : "disabled");
228dd540b46SJohn Baldwin 		if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
229dd540b46SJohn Baldwin 			break;
2306e2ab0aeSRoger Pau Monné 		if (x2apic->ApicId > MAX_APIC_ID) {
2316e2ab0aeSRoger Pau Monné 			printf("SRAT: Ignoring local APIC ID %u (too high)\n",
2326e2ab0aeSRoger Pau Monné 			    x2apic->ApicId);
2336e2ab0aeSRoger Pau Monné 			break;
2346e2ab0aeSRoger Pau Monné 		}
2356e2ab0aeSRoger Pau Monné 
236dd540b46SJohn Baldwin 		KASSERT(!cpus[x2apic->ApicId].enabled,
237dd540b46SJohn Baldwin 		    ("Duplicate local APIC ID %u", x2apic->ApicId));
238dd540b46SJohn Baldwin 		cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
239dd540b46SJohn Baldwin 		cpus[x2apic->ApicId].enabled = 1;
240dd540b46SJohn Baldwin 		break;
241dd540b46SJohn Baldwin 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
242dd540b46SJohn Baldwin 		mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
243dd540b46SJohn Baldwin 		if (bootverbose)
244dd540b46SJohn Baldwin 			printf(
2455ab0f0c3SKonstantin Belousov 		    "SRAT: Found memory domain %d addr 0x%jx len 0x%jx: %s\n",
246dd540b46SJohn Baldwin 			    mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
247dd540b46SJohn Baldwin 			    (uintmax_t)mem->Length,
248dd540b46SJohn Baldwin 			    (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
249dd540b46SJohn Baldwin 			    "enabled" : "disabled");
250dd540b46SJohn Baldwin 		if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
251dd540b46SJohn Baldwin 			break;
2524d99cfb3SJohn Baldwin 		if (!overlaps_phys_avail(mem->BaseAddress,
2534d99cfb3SJohn Baldwin 		    mem->BaseAddress + mem->Length)) {
2545ab0f0c3SKonstantin Belousov 			printf("SRAT: Ignoring memory at addr 0x%jx\n",
2554d99cfb3SJohn Baldwin 			    (uintmax_t)mem->BaseAddress);
2564d99cfb3SJohn Baldwin 			break;
2574d99cfb3SJohn Baldwin 		}
258dd540b46SJohn Baldwin 		if (num_mem == VM_PHYSSEG_MAX) {
259dd540b46SJohn Baldwin 			printf("SRAT: Too many memory regions\n");
260dd540b46SJohn Baldwin 			*(int *)arg = ENXIO;
261dd540b46SJohn Baldwin 			break;
262dd540b46SJohn Baldwin 		}
263dd540b46SJohn Baldwin 		slot = num_mem;
264dd540b46SJohn Baldwin 		for (i = 0; i < num_mem; i++) {
265dd540b46SJohn Baldwin 			if (mem_info[i].end <= mem->BaseAddress)
266dd540b46SJohn Baldwin 				continue;
267dd540b46SJohn Baldwin 			if (mem_info[i].start <
268dd540b46SJohn Baldwin 			    (mem->BaseAddress + mem->Length)) {
269dd540b46SJohn Baldwin 				printf("SRAT: Overlapping memory entries\n");
270dd540b46SJohn Baldwin 				*(int *)arg = ENXIO;
271dd540b46SJohn Baldwin 				return;
272dd540b46SJohn Baldwin 			}
273dd540b46SJohn Baldwin 			slot = i;
274dd540b46SJohn Baldwin 		}
275dd540b46SJohn Baldwin 		for (i = num_mem; i > slot; i--)
276dd540b46SJohn Baldwin 			mem_info[i] = mem_info[i - 1];
277dd540b46SJohn Baldwin 		mem_info[slot].start = mem->BaseAddress;
278dd540b46SJohn Baldwin 		mem_info[slot].end = mem->BaseAddress + mem->Length;
279dd540b46SJohn Baldwin 		mem_info[slot].domain = mem->ProximityDomain;
280dd540b46SJohn Baldwin 		num_mem++;
281dd540b46SJohn Baldwin 		break;
282dd540b46SJohn Baldwin 	}
283dd540b46SJohn Baldwin }
284dd540b46SJohn Baldwin 
285dd540b46SJohn Baldwin /*
286dd540b46SJohn Baldwin  * Ensure each memory domain has at least one CPU and that each CPU
287dd540b46SJohn Baldwin  * has at least one memory domain.
288dd540b46SJohn Baldwin  */
289dd540b46SJohn Baldwin static int
290dd540b46SJohn Baldwin check_domains(void)
291dd540b46SJohn Baldwin {
292dd540b46SJohn Baldwin 	int found, i, j;
293dd540b46SJohn Baldwin 
294dd540b46SJohn Baldwin 	for (i = 0; i < num_mem; i++) {
295dd540b46SJohn Baldwin 		found = 0;
296dd540b46SJohn Baldwin 		for (j = 0; j <= MAX_APIC_ID; j++)
2976676877bSJohn Baldwin 			if (cpus[j].enabled &&
2986676877bSJohn Baldwin 			    cpus[j].domain == mem_info[i].domain) {
299dd540b46SJohn Baldwin 				cpus[j].has_memory = 1;
300dd540b46SJohn Baldwin 				found++;
301dd540b46SJohn Baldwin 			}
302dd540b46SJohn Baldwin 		if (!found) {
303dd540b46SJohn Baldwin 			printf("SRAT: No CPU found for memory domain %d\n",
304dd540b46SJohn Baldwin 			    mem_info[i].domain);
305dd540b46SJohn Baldwin 			return (ENXIO);
306dd540b46SJohn Baldwin 		}
307dd540b46SJohn Baldwin 	}
308dd540b46SJohn Baldwin 	for (i = 0; i <= MAX_APIC_ID; i++)
309dd540b46SJohn Baldwin 		if (cpus[i].enabled && !cpus[i].has_memory) {
310dd540b46SJohn Baldwin 			printf("SRAT: No memory found for CPU %d\n", i);
311dd540b46SJohn Baldwin 			return (ENXIO);
312dd540b46SJohn Baldwin 		}
313dd540b46SJohn Baldwin 	return (0);
314dd540b46SJohn Baldwin }
315dd540b46SJohn Baldwin 
316dd540b46SJohn Baldwin /*
317dd540b46SJohn Baldwin  * Check that the SRAT memory regions cover all of the regions in
318dd540b46SJohn Baldwin  * phys_avail[].
319dd540b46SJohn Baldwin  */
320dd540b46SJohn Baldwin static int
321dd540b46SJohn Baldwin check_phys_avail(void)
322dd540b46SJohn Baldwin {
323dd540b46SJohn Baldwin 	vm_paddr_t address;
324dd540b46SJohn Baldwin 	int i, j;
325dd540b46SJohn Baldwin 
326dd540b46SJohn Baldwin 	/* j is the current offset into phys_avail[]. */
327dd540b46SJohn Baldwin 	address = phys_avail[0];
328dd540b46SJohn Baldwin 	j = 0;
329dd540b46SJohn Baldwin 	for (i = 0; i < num_mem; i++) {
330dd540b46SJohn Baldwin 		/*
331dd540b46SJohn Baldwin 		 * Consume as many phys_avail[] entries as fit in this
332dd540b46SJohn Baldwin 		 * region.
333dd540b46SJohn Baldwin 		 */
334dd540b46SJohn Baldwin 		while (address >= mem_info[i].start &&
335dd540b46SJohn Baldwin 		    address <= mem_info[i].end) {
336dd540b46SJohn Baldwin 			/*
337dd540b46SJohn Baldwin 			 * If we cover the rest of this phys_avail[] entry,
338dd540b46SJohn Baldwin 			 * advance to the next entry.
339dd540b46SJohn Baldwin 			 */
340dd540b46SJohn Baldwin 			if (phys_avail[j + 1] <= mem_info[i].end) {
341dd540b46SJohn Baldwin 				j += 2;
342dd540b46SJohn Baldwin 				if (phys_avail[j] == 0 &&
343dd540b46SJohn Baldwin 				    phys_avail[j + 1] == 0) {
344dd540b46SJohn Baldwin 					return (0);
345dd540b46SJohn Baldwin 				}
346dd540b46SJohn Baldwin 				address = phys_avail[j];
347dd540b46SJohn Baldwin 			} else
348dd540b46SJohn Baldwin 				address = mem_info[i].end + 1;
349dd540b46SJohn Baldwin 		}
350dd540b46SJohn Baldwin 	}
3515ab0f0c3SKonstantin Belousov 	printf("SRAT: No memory region found for 0x%jx - 0x%jx\n",
352dd540b46SJohn Baldwin 	    (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
353dd540b46SJohn Baldwin 	return (ENXIO);
354dd540b46SJohn Baldwin }
355dd540b46SJohn Baldwin 
356dd540b46SJohn Baldwin /*
357dd540b46SJohn Baldwin  * Renumber the memory domains to be compact and zero-based if not
35828990874SJohn Baldwin  * already.  Returns an error if there are too many domains.
359dd540b46SJohn Baldwin  */
36028990874SJohn Baldwin static int
361dd540b46SJohn Baldwin renumber_domains(void)
362dd540b46SJohn Baldwin {
3637e226537SAttilio Rao 	int i, j, slot;
364dd540b46SJohn Baldwin 
365dd540b46SJohn Baldwin 	/* Enumerate all the domains. */
36662d70a81SJohn Baldwin 	ndomain = 0;
367dd540b46SJohn Baldwin 	for (i = 0; i < num_mem; i++) {
368dd540b46SJohn Baldwin 		/* See if this domain is already known. */
36962d70a81SJohn Baldwin 		for (j = 0; j < ndomain; j++) {
37062d70a81SJohn Baldwin 			if (domain_pxm[j] >= mem_info[i].domain)
371dd540b46SJohn Baldwin 				break;
372dd540b46SJohn Baldwin 		}
37362d70a81SJohn Baldwin 		if (j < ndomain && domain_pxm[j] == mem_info[i].domain)
374dd540b46SJohn Baldwin 			continue;
375dd540b46SJohn Baldwin 
3763765b809SConrad Meyer 		if (ndomain >= MAXMEMDOM) {
3773765b809SConrad Meyer 			ndomain = 1;
3783765b809SConrad Meyer 			printf("SRAT: Too many memory domains\n");
3793765b809SConrad Meyer 			return (EFBIG);
3803765b809SConrad Meyer 		}
3813765b809SConrad Meyer 
382dd540b46SJohn Baldwin 		/* Insert the new domain at slot 'j'. */
383dd540b46SJohn Baldwin 		slot = j;
38462d70a81SJohn Baldwin 		for (j = ndomain; j > slot; j--)
38562d70a81SJohn Baldwin 			domain_pxm[j] = domain_pxm[j - 1];
38662d70a81SJohn Baldwin 		domain_pxm[slot] = mem_info[i].domain;
38762d70a81SJohn Baldwin 		ndomain++;
388dd540b46SJohn Baldwin 	}
389dd540b46SJohn Baldwin 
39062d70a81SJohn Baldwin 	/* Renumber each domain to its index in the sorted 'domain_pxm' list. */
39162d70a81SJohn Baldwin 	for (i = 0; i < ndomain; i++) {
392dd540b46SJohn Baldwin 		/*
393dd540b46SJohn Baldwin 		 * If the domain is already the right value, no need
394dd540b46SJohn Baldwin 		 * to renumber.
395dd540b46SJohn Baldwin 		 */
39662d70a81SJohn Baldwin 		if (domain_pxm[i] == i)
397dd540b46SJohn Baldwin 			continue;
398dd540b46SJohn Baldwin 
399dd540b46SJohn Baldwin 		/* Walk the cpu[] and mem_info[] arrays to renumber. */
400dd540b46SJohn Baldwin 		for (j = 0; j < num_mem; j++)
40162d70a81SJohn Baldwin 			if (mem_info[j].domain == domain_pxm[i])
402dd540b46SJohn Baldwin 				mem_info[j].domain = i;
403dd540b46SJohn Baldwin 		for (j = 0; j <= MAX_APIC_ID; j++)
40462d70a81SJohn Baldwin 			if (cpus[j].enabled && cpus[j].domain == domain_pxm[i])
405dd540b46SJohn Baldwin 				cpus[j].domain = i;
406dd540b46SJohn Baldwin 	}
4077e226537SAttilio Rao 
40828990874SJohn Baldwin 	return (0);
409dd540b46SJohn Baldwin }
410dd540b46SJohn Baldwin 
411dd540b46SJohn Baldwin /*
412dd540b46SJohn Baldwin  * Look for an ACPI System Resource Affinity Table ("SRAT")
413dd540b46SJohn Baldwin  */
414415d7ccaSAdrian Chadd static int
415415d7ccaSAdrian Chadd parse_srat(void)
416dd540b46SJohn Baldwin {
417dd540b46SJohn Baldwin 	int error;
418dd540b46SJohn Baldwin 
419dd540b46SJohn Baldwin 	if (resource_disabled("srat", 0))
420415d7ccaSAdrian Chadd 		return (-1);
421dd540b46SJohn Baldwin 
422dd540b46SJohn Baldwin 	srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
423dd540b46SJohn Baldwin 	if (srat_physaddr == 0)
424415d7ccaSAdrian Chadd 		return (-1);
425dd540b46SJohn Baldwin 
426dd540b46SJohn Baldwin 	/*
427dd540b46SJohn Baldwin 	 * Make a pass over the table to populate the cpus[] and
428dd540b46SJohn Baldwin 	 * mem_info[] tables.
429dd540b46SJohn Baldwin 	 */
430dd540b46SJohn Baldwin 	srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
431dd540b46SJohn Baldwin 	error = 0;
432dd540b46SJohn Baldwin 	srat_walk_table(srat_parse_entry, &error);
433dd540b46SJohn Baldwin 	acpi_unmap_table(srat);
434dd540b46SJohn Baldwin 	srat = NULL;
43528990874SJohn Baldwin 	if (error || check_domains() != 0 || check_phys_avail() != 0 ||
43628990874SJohn Baldwin 	    renumber_domains() != 0) {
437dd540b46SJohn Baldwin 		srat_physaddr = 0;
438415d7ccaSAdrian Chadd 		return (-1);
439dd540b46SJohn Baldwin 	}
440dd540b46SJohn Baldwin 
44162d70a81SJohn Baldwin #ifdef VM_NUMA_ALLOC
442dd540b46SJohn Baldwin 	/* Point vm_phys at our memory affinity table. */
44362d70a81SJohn Baldwin 	vm_ndomains = ndomain;
444dd540b46SJohn Baldwin 	mem_affinity = mem_info;
44562d70a81SJohn Baldwin #endif
446415d7ccaSAdrian Chadd 
447415d7ccaSAdrian Chadd 	return (0);
448dd540b46SJohn Baldwin }
449415d7ccaSAdrian Chadd 
450415d7ccaSAdrian Chadd static void
451415d7ccaSAdrian Chadd init_mem_locality(void)
452415d7ccaSAdrian Chadd {
453415d7ccaSAdrian Chadd 	int i;
454415d7ccaSAdrian Chadd 
455415d7ccaSAdrian Chadd 	/*
4564f5e270aSAdrian Chadd 	 * For now, assume -1 == "no locality information for
457415d7ccaSAdrian Chadd 	 * this pairing.
458415d7ccaSAdrian Chadd 	 */
459415d7ccaSAdrian Chadd 	for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++)
460415d7ccaSAdrian Chadd 		vm_locality_table[i] = -1;
461415d7ccaSAdrian Chadd }
462415d7ccaSAdrian Chadd 
463415d7ccaSAdrian Chadd static void
464415d7ccaSAdrian Chadd parse_acpi_tables(void *dummy)
465415d7ccaSAdrian Chadd {
466415d7ccaSAdrian Chadd 
467415d7ccaSAdrian Chadd 	if (parse_srat() < 0)
468415d7ccaSAdrian Chadd 		return;
469415d7ccaSAdrian Chadd 	init_mem_locality();
470415d7ccaSAdrian Chadd 	(void) parse_slit();
471415d7ccaSAdrian Chadd }
472415d7ccaSAdrian Chadd SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables,
473415d7ccaSAdrian Chadd     NULL);
474dd540b46SJohn Baldwin 
475dd540b46SJohn Baldwin static void
476dd540b46SJohn Baldwin srat_walk_table(acpi_subtable_handler *handler, void *arg)
477dd540b46SJohn Baldwin {
478dd540b46SJohn Baldwin 
479dd540b46SJohn Baldwin 	acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
480dd540b46SJohn Baldwin 	    handler, arg);
481dd540b46SJohn Baldwin }
482dd540b46SJohn Baldwin 
483dd540b46SJohn Baldwin /*
484c0ae6688SJohn Baldwin  * Setup per-CPU domain IDs.
485dd540b46SJohn Baldwin  */
486dd540b46SJohn Baldwin static void
487dd540b46SJohn Baldwin srat_set_cpus(void *dummy)
488dd540b46SJohn Baldwin {
489dd540b46SJohn Baldwin 	struct cpu_info *cpu;
490dd540b46SJohn Baldwin 	struct pcpu *pc;
491dd540b46SJohn Baldwin 	u_int i;
492dd540b46SJohn Baldwin 
493dd540b46SJohn Baldwin 	if (srat_physaddr == 0)
494dd540b46SJohn Baldwin 		return;
495dd540b46SJohn Baldwin 	for (i = 0; i < MAXCPU; i++) {
496dd540b46SJohn Baldwin 		if (CPU_ABSENT(i))
497dd540b46SJohn Baldwin 			continue;
498dd540b46SJohn Baldwin 		pc = pcpu_find(i);
499dd540b46SJohn Baldwin 		KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
500dd540b46SJohn Baldwin 		cpu = &cpus[pc->pc_apic_id];
501dd540b46SJohn Baldwin 		if (!cpu->enabled)
502dd540b46SJohn Baldwin 			panic("SRAT: CPU with APIC ID %u is not known",
503dd540b46SJohn Baldwin 			    pc->pc_apic_id);
504dd540b46SJohn Baldwin 		pc->pc_domain = cpu->domain;
505c0ae6688SJohn Baldwin 		CPU_SET(i, &cpuset_domain[cpu->domain]);
506dd540b46SJohn Baldwin 		if (bootverbose)
507dd540b46SJohn Baldwin 			printf("SRAT: CPU %u has memory domain %d\n", i,
508dd540b46SJohn Baldwin 			    cpu->domain);
509dd540b46SJohn Baldwin 	}
510dd540b46SJohn Baldwin }
511dd540b46SJohn Baldwin SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);
512fc4f524aSAdrian Chadd 
513fc4f524aSAdrian Chadd /*
514fc4f524aSAdrian Chadd  * Map a _PXM value to a VM domain ID.
515fc4f524aSAdrian Chadd  *
516fc4f524aSAdrian Chadd  * Returns the domain ID, or -1 if no domain ID was found.
517fc4f524aSAdrian Chadd  */
518fc4f524aSAdrian Chadd int
519fc4f524aSAdrian Chadd acpi_map_pxm_to_vm_domainid(int pxm)
520fc4f524aSAdrian Chadd {
521fc4f524aSAdrian Chadd 	int i;
522fc4f524aSAdrian Chadd 
52362d70a81SJohn Baldwin 	for (i = 0; i < ndomain; i++) {
52462d70a81SJohn Baldwin 		if (domain_pxm[i] == pxm)
525fc4f524aSAdrian Chadd 			return (i);
526fc4f524aSAdrian Chadd 	}
527fc4f524aSAdrian Chadd 
528fc4f524aSAdrian Chadd 	return (-1);
529fc4f524aSAdrian Chadd }
530fc4f524aSAdrian Chadd 
53162d70a81SJohn Baldwin #else /* MAXMEMDOM == 1 */
53262d70a81SJohn Baldwin 
53362d70a81SJohn Baldwin int
53462d70a81SJohn Baldwin acpi_map_pxm_to_vm_domainid(int pxm)
53562d70a81SJohn Baldwin {
53662d70a81SJohn Baldwin 
53762d70a81SJohn Baldwin 	return (-1);
53862d70a81SJohn Baldwin }
53962d70a81SJohn Baldwin 
540941646f5SAttilio Rao #endif /* MAXMEMDOM > 1 */
541