1 /*-
2 * Copyright (c) 2010 Hudson River Trading LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: stable/9/sys/x86/acpica/srat.c 299486 2016-05-11 22:07:05Z vangyzen $");
30
31 #include "opt_vm.h"
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/smp.h>
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 #include <vm/vm_param.h>
40 #include <vm/vm_phys.h>
41
42 #include <contrib/dev/acpica/include/acpi.h>
43 #include <contrib/dev/acpica/include/actables.h>
44
45 #include <machine/intr_machdep.h>
46 #include <machine/apicvar.h>
47
48 #include <dev/acpica/acpivar.h>
49
50 #if VM_NDOMAIN > 1
51 struct cpu_info {
52 int enabled:1;
53 int has_memory:1;
54 int domain;
55 } cpus[MAX_APIC_ID + 1];
56
57 struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
58 int num_mem;
59
60 static ACPI_TABLE_SRAT *srat;
61 static vm_paddr_t srat_physaddr;
62
63 static void srat_walk_table(acpi_subtable_handler *handler, void *arg);
64
65 /*
66 * Returns true if a memory range overlaps with at least one range in
67 * phys_avail[].
68 */
69 static int
overlaps_phys_avail(vm_paddr_t start,vm_paddr_t end)70 overlaps_phys_avail(vm_paddr_t start, vm_paddr_t end)
71 {
72 int i;
73
74 for (i = 0; phys_avail[i] != 0 && phys_avail[i + 1] != 0; i += 2) {
75 if (phys_avail[i + 1] < start)
76 continue;
77 if (phys_avail[i] < end)
78 return (1);
79 break;
80 }
81 return (0);
82
83 }
84
85 static void
srat_parse_entry(ACPI_SUBTABLE_HEADER * entry,void * arg)86 srat_parse_entry(ACPI_SUBTABLE_HEADER *entry, void *arg)
87 {
88 ACPI_SRAT_CPU_AFFINITY *cpu;
89 ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
90 ACPI_SRAT_MEM_AFFINITY *mem;
91 int domain, i, slot;
92
93 switch (entry->Type) {
94 case ACPI_SRAT_TYPE_CPU_AFFINITY:
95 cpu = (ACPI_SRAT_CPU_AFFINITY *)entry;
96 domain = cpu->ProximityDomainLo |
97 cpu->ProximityDomainHi[0] << 8 |
98 cpu->ProximityDomainHi[1] << 16 |
99 cpu->ProximityDomainHi[2] << 24;
100 if (bootverbose)
101 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
102 cpu->ApicId, domain,
103 (cpu->Flags & ACPI_SRAT_CPU_ENABLED) ?
104 "enabled" : "disabled");
105 if (!(cpu->Flags & ACPI_SRAT_CPU_ENABLED))
106 break;
107 if (cpus[cpu->ApicId].enabled) {
108 printf("SRAT: Duplicate local APIC ID %u\n",
109 cpu->ApicId);
110 *(int *)arg = ENXIO;
111 break;
112 }
113 cpus[cpu->ApicId].domain = domain;
114 cpus[cpu->ApicId].enabled = 1;
115 break;
116 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
117 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
118 if (bootverbose)
119 printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
120 x2apic->ApicId, x2apic->ProximityDomain,
121 (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
122 "enabled" : "disabled");
123 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
124 break;
125 KASSERT(!cpus[x2apic->ApicId].enabled,
126 ("Duplicate local APIC ID %u", x2apic->ApicId));
127 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
128 cpus[x2apic->ApicId].enabled = 1;
129 break;
130 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
131 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
132 if (bootverbose)
133 printf(
134 "SRAT: Found memory domain %d addr %jx len %jx: %s\n",
135 mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
136 (uintmax_t)mem->Length,
137 (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
138 "enabled" : "disabled");
139 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
140 break;
141 if (!overlaps_phys_avail(mem->BaseAddress,
142 mem->BaseAddress + mem->Length)) {
143 printf("SRAT: Ignoring memory at addr %jx\n",
144 (uintmax_t)mem->BaseAddress);
145 break;
146 }
147 if (num_mem == VM_PHYSSEG_MAX) {
148 printf("SRAT: Too many memory regions\n");
149 *(int *)arg = ENXIO;
150 break;
151 }
152 slot = num_mem;
153 for (i = 0; i < num_mem; i++) {
154 if (mem_info[i].end <= mem->BaseAddress)
155 continue;
156 if (mem_info[i].start <
157 (mem->BaseAddress + mem->Length)) {
158 printf("SRAT: Overlapping memory entries\n");
159 *(int *)arg = ENXIO;
160 return;
161 }
162 slot = i;
163 }
164 for (i = num_mem; i > slot; i--)
165 mem_info[i] = mem_info[i - 1];
166 mem_info[slot].start = mem->BaseAddress;
167 mem_info[slot].end = mem->BaseAddress + mem->Length;
168 mem_info[slot].domain = mem->ProximityDomain;
169 num_mem++;
170 break;
171 }
172 }
173
174 /*
175 * Ensure each memory domain has at least one CPU and that each CPU
176 * has at least one memory domain.
177 */
178 static int
check_domains(void)179 check_domains(void)
180 {
181 int found, i, j;
182
183 for (i = 0; i < num_mem; i++) {
184 found = 0;
185 for (j = 0; j <= MAX_APIC_ID; j++)
186 if (cpus[j].enabled &&
187 cpus[j].domain == mem_info[i].domain) {
188 cpus[j].has_memory = 1;
189 found++;
190 }
191 if (!found) {
192 printf("SRAT: No CPU found for memory domain %d\n",
193 mem_info[i].domain);
194 return (ENXIO);
195 }
196 }
197 for (i = 0; i <= MAX_APIC_ID; i++)
198 if (cpus[i].enabled && !cpus[i].has_memory) {
199 printf("SRAT: No memory found for CPU %d\n", i);
200 return (ENXIO);
201 }
202 return (0);
203 }
204
205 /*
206 * Check that the SRAT memory regions cover all of the regions in
207 * phys_avail[].
208 */
209 static int
check_phys_avail(void)210 check_phys_avail(void)
211 {
212 vm_paddr_t address;
213 int i, j;
214
215 /* j is the current offset into phys_avail[]. */
216 address = phys_avail[0];
217 j = 0;
218 for (i = 0; i < num_mem; i++) {
219 /*
220 * Consume as many phys_avail[] entries as fit in this
221 * region.
222 */
223 while (address >= mem_info[i].start &&
224 address <= mem_info[i].end) {
225 /*
226 * If we cover the rest of this phys_avail[] entry,
227 * advance to the next entry.
228 */
229 if (phys_avail[j + 1] <= mem_info[i].end) {
230 j += 2;
231 if (phys_avail[j] == 0 &&
232 phys_avail[j + 1] == 0) {
233 return (0);
234 }
235 address = phys_avail[j];
236 } else
237 address = mem_info[i].end + 1;
238 }
239 }
240 printf("SRAT: No memory region found for %jx - %jx\n",
241 (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
242 return (ENXIO);
243 }
244
245 /*
246 * Renumber the memory domains to be compact and zero-based if not
247 * already. Returns an error if there are too many domains.
248 */
249 static int
renumber_domains(void)250 renumber_domains(void)
251 {
252 int domains[VM_PHYSSEG_MAX];
253 int ndomain, i, j, slot;
254
255 /* Enumerate all the domains. */
256 ndomain = 0;
257 for (i = 0; i < num_mem; i++) {
258 /* See if this domain is already known. */
259 for (j = 0; j < ndomain; j++) {
260 if (domains[j] >= mem_info[i].domain)
261 break;
262 }
263 if (j < ndomain && domains[j] == mem_info[i].domain)
264 continue;
265
266 /* Insert the new domain at slot 'j'. */
267 slot = j;
268 for (j = ndomain; j > slot; j--)
269 domains[j] = domains[j - 1];
270 domains[slot] = mem_info[i].domain;
271 ndomain++;
272 if (ndomain > VM_NDOMAIN) {
273 printf("SRAT: Too many memory domains\n");
274 return (EFBIG);
275 }
276 }
277
278 /* Renumber each domain to its index in the sorted 'domains' list. */
279 for (i = 0; i < ndomain; i++) {
280 /*
281 * If the domain is already the right value, no need
282 * to renumber.
283 */
284 if (domains[i] == i)
285 continue;
286
287 /* Walk the cpu[] and mem_info[] arrays to renumber. */
288 for (j = 0; j < num_mem; j++)
289 if (mem_info[j].domain == domains[i])
290 mem_info[j].domain = i;
291 for (j = 0; j <= MAX_APIC_ID; j++)
292 if (cpus[j].enabled && cpus[j].domain == domains[i])
293 cpus[j].domain = i;
294 }
295 return (0);
296 }
297
298 /*
299 * Look for an ACPI System Resource Affinity Table ("SRAT")
300 */
301 static void
parse_srat(void * dummy)302 parse_srat(void *dummy)
303 {
304 int error;
305
306 if (resource_disabled("srat", 0))
307 return;
308
309 srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
310 if (srat_physaddr == 0)
311 return;
312
313 /*
314 * Make a pass over the table to populate the cpus[] and
315 * mem_info[] tables.
316 */
317 srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
318 error = 0;
319 srat_walk_table(srat_parse_entry, &error);
320 acpi_unmap_table(srat);
321 srat = NULL;
322 if (error || check_domains() != 0 || check_phys_avail() != 0 ||
323 renumber_domains() != 0) {
324 srat_physaddr = 0;
325 return;
326 }
327
328 /* Point vm_phys at our memory affinity table. */
329 mem_affinity = mem_info;
330 }
331 SYSINIT(parse_srat, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_srat, NULL);
332
333 static void
srat_walk_table(acpi_subtable_handler * handler,void * arg)334 srat_walk_table(acpi_subtable_handler *handler, void *arg)
335 {
336
337 acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
338 handler, arg);
339 }
340
341 /*
342 * Setup per-CPU ACPI IDs.
343 */
344 static void
srat_set_cpus(void * dummy)345 srat_set_cpus(void *dummy)
346 {
347 struct cpu_info *cpu;
348 struct pcpu *pc;
349 u_int i;
350
351 if (srat_physaddr == 0)
352 return;
353 for (i = 0; i < MAXCPU; i++) {
354 if (CPU_ABSENT(i))
355 continue;
356 pc = pcpu_find(i);
357 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
358 cpu = &cpus[pc->pc_apic_id];
359 if (!cpu->enabled)
360 panic("SRAT: CPU with APIC ID %u is not known",
361 pc->pc_apic_id);
362 pc->pc_domain = cpu->domain;
363 if (bootverbose)
364 printf("SRAT: CPU %u has memory domain %d\n", i,
365 cpu->domain);
366 }
367 }
368 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);
369 #endif /* VM_NDOMAIN > 1 */
370