xref: /freebsd/sys/x86/acpica/srat.c (revision 2db0699d8842200bdf3e6dd612716af715bd3717)
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>
34dd540b46SJohn Baldwin #include <sys/bus.h>
35dd540b46SJohn Baldwin #include <sys/kernel.h>
36449c2e92SKonstantin Belousov #include <sys/lock.h>
37449c2e92SKonstantin Belousov #include <sys/mutex.h>
38dd540b46SJohn Baldwin #include <sys/smp.h>
39449c2e92SKonstantin Belousov #include <sys/vmmeter.h>
40dd540b46SJohn Baldwin #include <vm/vm.h>
41dd540b46SJohn Baldwin #include <vm/pmap.h>
42dd540b46SJohn Baldwin #include <vm/vm_param.h>
43449c2e92SKonstantin Belousov #include <vm/vm_page.h>
44dd540b46SJohn Baldwin #include <vm/vm_phys.h>
45dd540b46SJohn Baldwin 
46dd540b46SJohn Baldwin #include <contrib/dev/acpica/include/acpi.h>
47dd540b46SJohn Baldwin #include <contrib/dev/acpica/include/actables.h>
48dd540b46SJohn Baldwin 
49dd540b46SJohn Baldwin #include <machine/intr_machdep.h>
50e07ef9b0SJohn Baldwin #include <x86/apicvar.h>
51dd540b46SJohn Baldwin 
52dd540b46SJohn Baldwin #include <dev/acpica/acpivar.h>
53dd540b46SJohn Baldwin 
54941646f5SAttilio Rao #if MAXMEMDOM > 1
55dd540b46SJohn Baldwin struct cpu_info {
56dd540b46SJohn Baldwin 	int enabled:1;
57dd540b46SJohn Baldwin 	int has_memory:1;
58dd540b46SJohn Baldwin 	int domain;
59dd540b46SJohn Baldwin } cpus[MAX_APIC_ID + 1];
60dd540b46SJohn Baldwin 
61dd540b46SJohn Baldwin struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
62dd540b46SJohn Baldwin int num_mem;
63dd540b46SJohn Baldwin 
64dd540b46SJohn Baldwin static ACPI_TABLE_SRAT *srat;
65dd540b46SJohn Baldwin static vm_paddr_t srat_physaddr;
66dd540b46SJohn Baldwin 
6762d70a81SJohn Baldwin static int domain_pxm[MAXMEMDOM];
6862d70a81SJohn Baldwin static int ndomain;
69fc4f524aSAdrian Chadd 
70415d7ccaSAdrian Chadd static ACPI_TABLE_SLIT *slit;
71415d7ccaSAdrian Chadd static vm_paddr_t slit_physaddr;
72415d7ccaSAdrian Chadd static int vm_locality_table[MAXMEMDOM * MAXMEMDOM];
73415d7ccaSAdrian Chadd 
74dd540b46SJohn Baldwin static void	srat_walk_table(acpi_subtable_handler *handler, void *arg);
75dd540b46SJohn Baldwin 
764d99cfb3SJohn Baldwin /*
77415d7ccaSAdrian Chadd  * SLIT parsing.
78415d7ccaSAdrian Chadd  */
79415d7ccaSAdrian Chadd 
80415d7ccaSAdrian Chadd static void
81415d7ccaSAdrian Chadd slit_parse_table(ACPI_TABLE_SLIT *s)
82415d7ccaSAdrian Chadd {
83415d7ccaSAdrian Chadd 	int i, j;
84415d7ccaSAdrian Chadd 	int i_domain, j_domain;
85415d7ccaSAdrian Chadd 	int offset = 0;
86415d7ccaSAdrian Chadd 	uint8_t e;
87415d7ccaSAdrian Chadd 
88415d7ccaSAdrian Chadd 	/*
89415d7ccaSAdrian Chadd 	 * This maps the SLIT data into the VM-domain centric view.
90415d7ccaSAdrian Chadd 	 * There may be sparse entries in the PXM namespace, so
91415d7ccaSAdrian Chadd 	 * remap them to a VM-domain ID and if it doesn't exist,
92415d7ccaSAdrian Chadd 	 * skip it.
93415d7ccaSAdrian Chadd 	 *
94415d7ccaSAdrian Chadd 	 * It should result in a packed 2d array of VM-domain
95415d7ccaSAdrian Chadd 	 * locality information entries.
96415d7ccaSAdrian Chadd 	 */
97415d7ccaSAdrian Chadd 
98415d7ccaSAdrian Chadd 	if (bootverbose)
99415d7ccaSAdrian Chadd 		printf("SLIT.Localities: %d\n", (int) s->LocalityCount);
100415d7ccaSAdrian Chadd 	for (i = 0; i < s->LocalityCount; i++) {
101415d7ccaSAdrian Chadd 		i_domain = acpi_map_pxm_to_vm_domainid(i);
102415d7ccaSAdrian Chadd 		if (i_domain < 0)
103415d7ccaSAdrian Chadd 			continue;
104415d7ccaSAdrian Chadd 
105415d7ccaSAdrian Chadd 		if (bootverbose)
106415d7ccaSAdrian Chadd 			printf("%d: ", i);
107415d7ccaSAdrian Chadd 		for (j = 0; j < s->LocalityCount; j++) {
108415d7ccaSAdrian Chadd 			j_domain = acpi_map_pxm_to_vm_domainid(j);
109415d7ccaSAdrian Chadd 			if (j_domain < 0)
110415d7ccaSAdrian Chadd 				continue;
111415d7ccaSAdrian Chadd 			e = s->Entry[i * s->LocalityCount + j];
112415d7ccaSAdrian Chadd 			if (bootverbose)
113415d7ccaSAdrian Chadd 				printf("%d ", (int) e);
114415d7ccaSAdrian Chadd 			/* 255 == "no locality information" */
115415d7ccaSAdrian Chadd 			if (e == 255)
116415d7ccaSAdrian Chadd 				vm_locality_table[offset] = -1;
117415d7ccaSAdrian Chadd 			else
118415d7ccaSAdrian Chadd 				vm_locality_table[offset] = e;
119415d7ccaSAdrian Chadd 			offset++;
120415d7ccaSAdrian Chadd 		}
121415d7ccaSAdrian Chadd 		if (bootverbose)
122415d7ccaSAdrian Chadd 			printf("\n");
123415d7ccaSAdrian Chadd 	}
124415d7ccaSAdrian Chadd }
125415d7ccaSAdrian Chadd 
126415d7ccaSAdrian Chadd /*
127415d7ccaSAdrian Chadd  * Look for an ACPI System Locality Distance Information Table ("SLIT")
128415d7ccaSAdrian Chadd  */
129415d7ccaSAdrian Chadd static int
130415d7ccaSAdrian Chadd parse_slit(void)
131415d7ccaSAdrian Chadd {
132415d7ccaSAdrian Chadd 
133415d7ccaSAdrian Chadd 	if (resource_disabled("slit", 0)) {
134415d7ccaSAdrian Chadd 		return (-1);
135415d7ccaSAdrian Chadd 	}
136415d7ccaSAdrian Chadd 
137415d7ccaSAdrian Chadd 	slit_physaddr = acpi_find_table(ACPI_SIG_SLIT);
138415d7ccaSAdrian Chadd 	if (slit_physaddr == 0) {
139415d7ccaSAdrian Chadd 		return (-1);
140415d7ccaSAdrian Chadd 	}
141415d7ccaSAdrian Chadd 
142415d7ccaSAdrian Chadd 	/*
143415d7ccaSAdrian Chadd 	 * Make a pass over the table to populate the cpus[] and
144415d7ccaSAdrian Chadd 	 * mem_info[] tables.
145415d7ccaSAdrian Chadd 	 */
146415d7ccaSAdrian Chadd 	slit = acpi_map_table(slit_physaddr, ACPI_SIG_SLIT);
147415d7ccaSAdrian Chadd 	slit_parse_table(slit);
148415d7ccaSAdrian Chadd 	acpi_unmap_table(slit);
149415d7ccaSAdrian Chadd 	slit = NULL;
150415d7ccaSAdrian Chadd 
15162d70a81SJohn Baldwin #ifdef VM_NUMA_ALLOC
152415d7ccaSAdrian Chadd 	/* Tell the VM about it! */
153415d7ccaSAdrian Chadd 	mem_locality = vm_locality_table;
15462d70a81SJohn Baldwin #endif
155415d7ccaSAdrian Chadd 	return (0);
156415d7ccaSAdrian Chadd }
157415d7ccaSAdrian Chadd 
158415d7ccaSAdrian Chadd /*
159415d7ccaSAdrian Chadd  * SRAT parsing.
160415d7ccaSAdrian Chadd  */
161415d7ccaSAdrian Chadd 
162415d7ccaSAdrian Chadd /*
1634d99cfb3SJohn Baldwin  * Returns true if a memory range overlaps with at least one range in
1644d99cfb3SJohn Baldwin  * phys_avail[].
1654d99cfb3SJohn Baldwin  */
1664d99cfb3SJohn Baldwin static int
1674d99cfb3SJohn Baldwin overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end)
1684d99cfb3SJohn Baldwin {
1694d99cfb3SJohn Baldwin 	int i;
1704d99cfb3SJohn Baldwin 
1714d99cfb3SJohn Baldwin 	for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) {
1724d99cfb3SJohn Baldwin 		if (phys_avail[i + 1] < start)
1734d99cfb3SJohn Baldwin 			continue;
1744d99cfb3SJohn Baldwin 		if (phys_avail[i] < end)
1754d99cfb3SJohn Baldwin 			return (1);
1764d99cfb3SJohn Baldwin 		break;
1774d99cfb3SJohn Baldwin 	}
1784d99cfb3SJohn Baldwin 	return (0);
1794d99cfb3SJohn Baldwin 
1804d99cfb3SJohn Baldwin }
1814d99cfb3SJohn Baldwin 
182dd540b46SJohn Baldwin static void
183dd540b46SJohn Baldwin srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
184dd540b46SJohn Baldwin {
185dd540b46SJohn Baldwin 	ACPI_SRAT_CPU_AFFINITY *cpu;
186dd540b46SJohn Baldwin 	ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
187dd540b46SJohn Baldwin 	ACPI_SRAT_MEM_AFFINITY *mem;
188dd540b46SJohn Baldwin 	int domain, i, slot;
189dd540b46SJohn Baldwin 
190dd540b46SJohn Baldwin 	switch (entry->Type) {
191dd540b46SJohn Baldwin 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
192dd540b46SJohn Baldwin 		cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
193dd540b46SJohn Baldwin 		domain = cpu->ProximityDomainLo |
194dd540b46SJohn Baldwin 		    cpu->ProximityDomainHi[0] << 8 |
195dd540b46SJohn Baldwin 		    cpu->ProximityDomainHi[1] << 16 |
196dd540b46SJohn Baldwin 		    cpu->ProximityDomainHi[2] << 24;
197dd540b46SJohn Baldwin 		if (bootverbose)
198dd540b46SJohn Baldwin 			printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
199dd540b46SJohn Baldwin 			    cpu->ApicId, domain,
200dd540b46SJohn Baldwin 			    (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
201dd540b46SJohn Baldwin 			    "enabled" : "disabled");
202dd540b46SJohn Baldwin 		if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
203dd540b46SJohn Baldwin 			break;
204*2db0699dSEric van Gyzen 		if (cpus[cpu->ApicId].enabled) {
205*2db0699dSEric van Gyzen 			printf("SRAT: Duplicate local APIC ID %u\n",
206*2db0699dSEric van Gyzen 			    cpu->ApicId);
207*2db0699dSEric van Gyzen 			*(int *)arg = ENXIO;
208*2db0699dSEric van Gyzen 			break;
209*2db0699dSEric van Gyzen 		}
210dd540b46SJohn Baldwin 		cpus[cpu->ApicId].domain = domain;
211dd540b46SJohn Baldwin 		cpus[cpu->ApicId].enabled = 1;
212dd540b46SJohn Baldwin 		break;
213dd540b46SJohn Baldwin 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
214dd540b46SJohn Baldwin 		x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
215dd540b46SJohn Baldwin 		if (bootverbose)
216dd540b46SJohn Baldwin 			printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
217dd540b46SJohn Baldwin 			    x2apic->ApicId, x2apic->ProximityDomain,
218dd540b46SJohn Baldwin 			    (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
219dd540b46SJohn Baldwin 			    "enabled" : "disabled");
220dd540b46SJohn Baldwin 		if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
221dd540b46SJohn Baldwin 			break;
222dd540b46SJohn Baldwin 		KASSERT(!cpus[x2apic->ApicId].enabled,
223dd540b46SJohn Baldwin 		    ("Duplicate local APIC ID %u", x2apic->ApicId));
224dd540b46SJohn Baldwin 		cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
225dd540b46SJohn Baldwin 		cpus[x2apic->ApicId].enabled = 1;
226dd540b46SJohn Baldwin 		break;
227dd540b46SJohn Baldwin 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
228dd540b46SJohn Baldwin 		mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
229dd540b46SJohn Baldwin 		if (bootverbose)
230dd540b46SJohn Baldwin 			printf(
231dd540b46SJohn Baldwin 		    "SRAT: Found memory domain %d addr %jx len %jx: %s\n",
232dd540b46SJohn Baldwin 			    mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
233dd540b46SJohn Baldwin 			    (uintmax_t)mem->Length,
234dd540b46SJohn Baldwin 			    (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
235dd540b46SJohn Baldwin 			    "enabled" : "disabled");
236dd540b46SJohn Baldwin 		if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
237dd540b46SJohn Baldwin 			break;
2384d99cfb3SJohn Baldwin 		if (!overlaps_phys_avail(mem->BaseAddress,
2394d99cfb3SJohn Baldwin 		    mem->BaseAddress + mem->Length)) {
2404d99cfb3SJohn Baldwin 			printf("SRAT: Ignoring memory at addr %jx\n",
2414d99cfb3SJohn Baldwin 			    (uintmax_t)mem->BaseAddress);
2424d99cfb3SJohn Baldwin 			break;
2434d99cfb3SJohn Baldwin 		}
244dd540b46SJohn Baldwin 		if (num_mem == VM_PHYSSEG_MAX) {
245dd540b46SJohn Baldwin 			printf("SRAT: Too many memory regions\n");
246dd540b46SJohn Baldwin 			*(int *)arg = ENXIO;
247dd540b46SJohn Baldwin 			break;
248dd540b46SJohn Baldwin 		}
249dd540b46SJohn Baldwin 		slot = num_mem;
250dd540b46SJohn Baldwin 		for (i = 0; i < num_mem; i++) {
251dd540b46SJohn Baldwin 			if (mem_info[i].end <= mem->BaseAddress)
252dd540b46SJohn Baldwin 				continue;
253dd540b46SJohn Baldwin 			if (mem_info[i].start <
254dd540b46SJohn Baldwin 			    (mem->BaseAddress + mem->Length)) {
255dd540b46SJohn Baldwin 				printf("SRAT: Overlapping memory entries\n");
256dd540b46SJohn Baldwin 				*(int *)arg = ENXIO;
257dd540b46SJohn Baldwin 				return;
258dd540b46SJohn Baldwin 			}
259dd540b46SJohn Baldwin 			slot = i;
260dd540b46SJohn Baldwin 		}
261dd540b46SJohn Baldwin 		for (i = num_mem; i > slot; i--)
262dd540b46SJohn Baldwin 			mem_info[i] = mem_info[i - 1];
263dd540b46SJohn Baldwin 		mem_info[slot].start = mem->BaseAddress;
264dd540b46SJohn Baldwin 		mem_info[slot].end = mem->BaseAddress + mem->Length;
265dd540b46SJohn Baldwin 		mem_info[slot].domain = mem->ProximityDomain;
266dd540b46SJohn Baldwin 		num_mem++;
267dd540b46SJohn Baldwin 		break;
268dd540b46SJohn Baldwin 	}
269dd540b46SJohn Baldwin }
270dd540b46SJohn Baldwin 
271dd540b46SJohn Baldwin /*
272dd540b46SJohn Baldwin  * Ensure each memory domain has at least one CPU and that each CPU
273dd540b46SJohn Baldwin  * has at least one memory domain.
274dd540b46SJohn Baldwin  */
275dd540b46SJohn Baldwin static int
276dd540b46SJohn Baldwin check_domains(void)
277dd540b46SJohn Baldwin {
278dd540b46SJohn Baldwin 	int found, i, j;
279dd540b46SJohn Baldwin 
280dd540b46SJohn Baldwin 	for (i = 0; i < num_mem; i++) {
281dd540b46SJohn Baldwin 		found = 0;
282dd540b46SJohn Baldwin 		for (j = 0; j <= MAX_APIC_ID; j++)
2836676877bSJohn Baldwin 			if (cpus[j].enabled &&
2846676877bSJohn Baldwin 			    cpus[j].domain == mem_info[i].domain) {
285dd540b46SJohn Baldwin 				cpus[j].has_memory = 1;
286dd540b46SJohn Baldwin 				found++;
287dd540b46SJohn Baldwin 			}
288dd540b46SJohn Baldwin 		if (!found) {
289dd540b46SJohn Baldwin 			printf("SRAT: No CPU found for memory domain %d\n",
290dd540b46SJohn Baldwin 			    mem_info[i].domain);
291dd540b46SJohn Baldwin 			return (ENXIO);
292dd540b46SJohn Baldwin 		}
293dd540b46SJohn Baldwin 	}
294dd540b46SJohn Baldwin 	for (i = 0; i <= MAX_APIC_ID; i++)
295dd540b46SJohn Baldwin 		if (cpus[i].enabled && !cpus[i].has_memory) {
296dd540b46SJohn Baldwin 			printf("SRAT: No memory found for CPU %d\n", i);
297dd540b46SJohn Baldwin 			return (ENXIO);
298dd540b46SJohn Baldwin 		}
299dd540b46SJohn Baldwin 	return (0);
300dd540b46SJohn Baldwin }
301dd540b46SJohn Baldwin 
302dd540b46SJohn Baldwin /*
303dd540b46SJohn Baldwin  * Check that the SRAT memory regions cover all of the regions in
304dd540b46SJohn Baldwin  * phys_avail[].
305dd540b46SJohn Baldwin  */
306dd540b46SJohn Baldwin static int
307dd540b46SJohn Baldwin check_phys_avail(void)
308dd540b46SJohn Baldwin {
309dd540b46SJohn Baldwin 	vm_paddr_t address;
310dd540b46SJohn Baldwin 	int i, j;
311dd540b46SJohn Baldwin 
312dd540b46SJohn Baldwin 	/* j is the current offset into phys_avail[]. */
313dd540b46SJohn Baldwin 	address = phys_avail[0];
314dd540b46SJohn Baldwin 	j = 0;
315dd540b46SJohn Baldwin 	for (i = 0; i < num_mem; i++) {
316dd540b46SJohn Baldwin 		/*
317dd540b46SJohn Baldwin 		 * Consume as many phys_avail[] entries as fit in this
318dd540b46SJohn Baldwin 		 * region.
319dd540b46SJohn Baldwin 		 */
320dd540b46SJohn Baldwin 		while (address >= mem_info[i].start &&
321dd540b46SJohn Baldwin 		    address <= mem_info[i].end) {
322dd540b46SJohn Baldwin 			/*
323dd540b46SJohn Baldwin 			 * If we cover the rest of this phys_avail[] entry,
324dd540b46SJohn Baldwin 			 * advance to the next entry.
325dd540b46SJohn Baldwin 			 */
326dd540b46SJohn Baldwin 			if (phys_avail[j + 1] <= mem_info[i].end) {
327dd540b46SJohn Baldwin 				j += 2;
328dd540b46SJohn Baldwin 				if (phys_avail[j] == 0 &&
329dd540b46SJohn Baldwin 				    phys_avail[j + 1] == 0) {
330dd540b46SJohn Baldwin 					return (0);
331dd540b46SJohn Baldwin 				}
332dd540b46SJohn Baldwin 				address = phys_avail[j];
333dd540b46SJohn Baldwin 			} else
334dd540b46SJohn Baldwin 				address = mem_info[i].end + 1;
335dd540b46SJohn Baldwin 		}
336dd540b46SJohn Baldwin 	}
337dd540b46SJohn Baldwin 	printf("SRAT: No memory region found for %jx - %jx\n",
338dd540b46SJohn Baldwin 	    (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
339dd540b46SJohn Baldwin 	return (ENXIO);
340dd540b46SJohn Baldwin }
341dd540b46SJohn Baldwin 
342dd540b46SJohn Baldwin /*
343dd540b46SJohn Baldwin  * Renumber the memory domains to be compact and zero-based if not
34428990874SJohn Baldwin  * already.  Returns an error if there are too many domains.
345dd540b46SJohn Baldwin  */
34628990874SJohn Baldwin static int
347dd540b46SJohn Baldwin renumber_domains(void)
348dd540b46SJohn Baldwin {
3497e226537SAttilio Rao 	int i, j, slot;
350dd540b46SJohn Baldwin 
351dd540b46SJohn Baldwin 	/* Enumerate all the domains. */
35262d70a81SJohn Baldwin 	ndomain = 0;
353dd540b46SJohn Baldwin 	for (i = 0; i < num_mem; i++) {
354dd540b46SJohn Baldwin 		/* See if this domain is already known. */
35562d70a81SJohn Baldwin 		for (j = 0; j < ndomain; j++) {
35662d70a81SJohn Baldwin 			if (domain_pxm[j] >= mem_info[i].domain)
357dd540b46SJohn Baldwin 				break;
358dd540b46SJohn Baldwin 		}
35962d70a81SJohn Baldwin 		if (j < ndomain && domain_pxm[j] == mem_info[i].domain)
360dd540b46SJohn Baldwin 			continue;
361dd540b46SJohn Baldwin 
3623765b809SConrad Meyer 		if (ndomain >= MAXMEMDOM) {
3633765b809SConrad Meyer 			ndomain = 1;
3643765b809SConrad Meyer 			printf("SRAT: Too many memory domains\n");
3653765b809SConrad Meyer 			return (EFBIG);
3663765b809SConrad Meyer 		}
3673765b809SConrad Meyer 
368dd540b46SJohn Baldwin 		/* Insert the new domain at slot 'j'. */
369dd540b46SJohn Baldwin 		slot = j;
37062d70a81SJohn Baldwin 		for (j = ndomain; j > slot; j--)
37162d70a81SJohn Baldwin 			domain_pxm[j] = domain_pxm[j - 1];
37262d70a81SJohn Baldwin 		domain_pxm[slot] = mem_info[i].domain;
37362d70a81SJohn Baldwin 		ndomain++;
374dd540b46SJohn Baldwin 	}
375dd540b46SJohn Baldwin 
37662d70a81SJohn Baldwin 	/* Renumber each domain to its index in the sorted 'domain_pxm' list. */
37762d70a81SJohn Baldwin 	for (i = 0; i < ndomain; i++) {
378dd540b46SJohn Baldwin 		/*
379dd540b46SJohn Baldwin 		 * If the domain is already the right value, no need
380dd540b46SJohn Baldwin 		 * to renumber.
381dd540b46SJohn Baldwin 		 */
38262d70a81SJohn Baldwin 		if (domain_pxm[i] == i)
383dd540b46SJohn Baldwin 			continue;
384dd540b46SJohn Baldwin 
385dd540b46SJohn Baldwin 		/* Walk the cpu[] and mem_info[] arrays to renumber. */
386dd540b46SJohn Baldwin 		for (j = 0; j < num_mem; j++)
38762d70a81SJohn Baldwin 			if (mem_info[j].domain == domain_pxm[i])
388dd540b46SJohn Baldwin 				mem_info[j].domain = i;
389dd540b46SJohn Baldwin 		for (j = 0; j <= MAX_APIC_ID; j++)
39062d70a81SJohn Baldwin 			if (cpus[j].enabled && cpus[j].domain == domain_pxm[i])
391dd540b46SJohn Baldwin 				cpus[j].domain = i;
392dd540b46SJohn Baldwin 	}
3937e226537SAttilio Rao 
39428990874SJohn Baldwin 	return (0);
395dd540b46SJohn Baldwin }
396dd540b46SJohn Baldwin 
397dd540b46SJohn Baldwin /*
398dd540b46SJohn Baldwin  * Look for an ACPI System Resource Affinity Table ("SRAT")
399dd540b46SJohn Baldwin  */
400415d7ccaSAdrian Chadd static int
401415d7ccaSAdrian Chadd parse_srat(void)
402dd540b46SJohn Baldwin {
403dd540b46SJohn Baldwin 	int error;
404dd540b46SJohn Baldwin 
405dd540b46SJohn Baldwin 	if (resource_disabled("srat", 0))
406415d7ccaSAdrian Chadd 		return (-1);
407dd540b46SJohn Baldwin 
408dd540b46SJohn Baldwin 	srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
409dd540b46SJohn Baldwin 	if (srat_physaddr == 0)
410415d7ccaSAdrian Chadd 		return (-1);
411dd540b46SJohn Baldwin 
412dd540b46SJohn Baldwin 	/*
413dd540b46SJohn Baldwin 	 * Make a pass over the table to populate the cpus[] and
414dd540b46SJohn Baldwin 	 * mem_info[] tables.
415dd540b46SJohn Baldwin 	 */
416dd540b46SJohn Baldwin 	srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
417dd540b46SJohn Baldwin 	error = 0;
418dd540b46SJohn Baldwin 	srat_walk_table(srat_parse_entry, &error);
419dd540b46SJohn Baldwin 	acpi_unmap_table(srat);
420dd540b46SJohn Baldwin 	srat = NULL;
42128990874SJohn Baldwin 	if (error || check_domains() != 0 || check_phys_avail() != 0 ||
42228990874SJohn Baldwin 	    renumber_domains() != 0) {
423dd540b46SJohn Baldwin 		srat_physaddr = 0;
424415d7ccaSAdrian Chadd 		return (-1);
425dd540b46SJohn Baldwin 	}
426dd540b46SJohn Baldwin 
42762d70a81SJohn Baldwin #ifdef VM_NUMA_ALLOC
428dd540b46SJohn Baldwin 	/* Point vm_phys at our memory affinity table. */
42962d70a81SJohn Baldwin 	vm_ndomains = ndomain;
430dd540b46SJohn Baldwin 	mem_affinity = mem_info;
43162d70a81SJohn Baldwin #endif
432415d7ccaSAdrian Chadd 
433415d7ccaSAdrian Chadd 	return (0);
434dd540b46SJohn Baldwin }
435415d7ccaSAdrian Chadd 
436415d7ccaSAdrian Chadd static void
437415d7ccaSAdrian Chadd init_mem_locality(void)
438415d7ccaSAdrian Chadd {
439415d7ccaSAdrian Chadd 	int i;
440415d7ccaSAdrian Chadd 
441415d7ccaSAdrian Chadd 	/*
4424f5e270aSAdrian Chadd 	 * For now, assume -1 == "no locality information for
443415d7ccaSAdrian Chadd 	 * this pairing.
444415d7ccaSAdrian Chadd 	 */
445415d7ccaSAdrian Chadd 	for (i = 0; i < MAXMEMDOM * MAXMEMDOM; i++)
446415d7ccaSAdrian Chadd 		vm_locality_table[i] = -1;
447415d7ccaSAdrian Chadd }
448415d7ccaSAdrian Chadd 
449415d7ccaSAdrian Chadd static void
450415d7ccaSAdrian Chadd parse_acpi_tables(void *dummy)
451415d7ccaSAdrian Chadd {
452415d7ccaSAdrian Chadd 
453415d7ccaSAdrian Chadd 	if (parse_srat() < 0)
454415d7ccaSAdrian Chadd 		return;
455415d7ccaSAdrian Chadd 	init_mem_locality();
456415d7ccaSAdrian Chadd 	(void) parse_slit();
457415d7ccaSAdrian Chadd }
458415d7ccaSAdrian Chadd SYSINIT(parse_acpi_tables, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_acpi_tables,
459415d7ccaSAdrian Chadd     NULL);
460dd540b46SJohn Baldwin 
461dd540b46SJohn Baldwin static void
462dd540b46SJohn Baldwin srat_walk_table(acpi_subtable_handler *handler, void *arg)
463dd540b46SJohn Baldwin {
464dd540b46SJohn Baldwin 
465dd540b46SJohn Baldwin 	acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
466dd540b46SJohn Baldwin 	    handler, arg);
467dd540b46SJohn Baldwin }
468dd540b46SJohn Baldwin 
469dd540b46SJohn Baldwin /*
470c0ae6688SJohn Baldwin  * Setup per-CPU domain IDs.
471dd540b46SJohn Baldwin  */
472dd540b46SJohn Baldwin static void
473dd540b46SJohn Baldwin srat_set_cpus(void *dummy)
474dd540b46SJohn Baldwin {
475dd540b46SJohn Baldwin 	struct cpu_info *cpu;
476dd540b46SJohn Baldwin 	struct pcpu *pc;
477dd540b46SJohn Baldwin 	u_int i;
478dd540b46SJohn Baldwin 
479dd540b46SJohn Baldwin 	if (srat_physaddr == 0)
480dd540b46SJohn Baldwin 		return;
481dd540b46SJohn Baldwin 	for (i = 0; i < MAXCPU; i++) {
482dd540b46SJohn Baldwin 		if (CPU_ABSENT(i))
483dd540b46SJohn Baldwin 			continue;
484dd540b46SJohn Baldwin 		pc = pcpu_find(i);
485dd540b46SJohn Baldwin 		KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
486dd540b46SJohn Baldwin 		cpu = &cpus[pc->pc_apic_id];
487dd540b46SJohn Baldwin 		if (!cpu->enabled)
488dd540b46SJohn Baldwin 			panic("SRAT: CPU with APIC ID %u is not known",
489dd540b46SJohn Baldwin 			    pc->pc_apic_id);
490dd540b46SJohn Baldwin 		pc->pc_domain = cpu->domain;
491c0ae6688SJohn Baldwin 		CPU_SET(i, &cpuset_domain[cpu->domain]);
492dd540b46SJohn Baldwin 		if (bootverbose)
493dd540b46SJohn Baldwin 			printf("SRAT: CPU %u has memory domain %d\n", i,
494dd540b46SJohn Baldwin 			    cpu->domain);
495dd540b46SJohn Baldwin 	}
496dd540b46SJohn Baldwin }
497dd540b46SJohn Baldwin SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);
498fc4f524aSAdrian Chadd 
499fc4f524aSAdrian Chadd /*
500fc4f524aSAdrian Chadd  * Map a _PXM value to a VM domain ID.
501fc4f524aSAdrian Chadd  *
502fc4f524aSAdrian Chadd  * Returns the domain ID, or -1 if no domain ID was found.
503fc4f524aSAdrian Chadd  */
504fc4f524aSAdrian Chadd int
505fc4f524aSAdrian Chadd acpi_map_pxm_to_vm_domainid(int pxm)
506fc4f524aSAdrian Chadd {
507fc4f524aSAdrian Chadd 	int i;
508fc4f524aSAdrian Chadd 
50962d70a81SJohn Baldwin 	for (i = 0; i < ndomain; i++) {
51062d70a81SJohn Baldwin 		if (domain_pxm[i] == pxm)
511fc4f524aSAdrian Chadd 			return (i);
512fc4f524aSAdrian Chadd 	}
513fc4f524aSAdrian Chadd 
514fc4f524aSAdrian Chadd 	return (-1);
515fc4f524aSAdrian Chadd }
516fc4f524aSAdrian Chadd 
51762d70a81SJohn Baldwin #else /* MAXMEMDOM == 1 */
51862d70a81SJohn Baldwin 
51962d70a81SJohn Baldwin int
52062d70a81SJohn Baldwin acpi_map_pxm_to_vm_domainid(int pxm)
52162d70a81SJohn Baldwin {
52262d70a81SJohn Baldwin 
52362d70a81SJohn Baldwin 	return (-1);
52462d70a81SJohn Baldwin }
52562d70a81SJohn Baldwin 
526941646f5SAttilio Rao #endif /* MAXMEMDOM > 1 */
527