]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/x86/acpica/srat.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / x86 / acpica / srat.c
1 /*-
2  * Copyright (c) 2010 Advanced Computing Technologies 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$");
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
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
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                 KASSERT(!cpus[cpu->ApicId].enabled,
108                     ("Duplicate local APIC ID %u", cpu->ApicId));
109                 cpus[cpu->ApicId].domain = domain;
110                 cpus[cpu->ApicId].enabled = 1;
111                 break;
112         case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
113                 x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)entry;
114                 if (bootverbose)
115                         printf("SRAT: Found CPU APIC ID %u domain %d: %s\n",
116                             x2apic->ApicId, x2apic->ProximityDomain,
117                             (x2apic->Flags & ACPI_SRAT_CPU_ENABLED) ?
118                             "enabled" : "disabled");
119                 if (!(x2apic->Flags & ACPI_SRAT_CPU_ENABLED))
120                         break;
121                 KASSERT(!cpus[x2apic->ApicId].enabled,
122                     ("Duplicate local APIC ID %u", x2apic->ApicId));
123                 cpus[x2apic->ApicId].domain = x2apic->ProximityDomain;
124                 cpus[x2apic->ApicId].enabled = 1;
125                 break;
126         case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
127                 mem = (ACPI_SRAT_MEM_AFFINITY *)entry;
128                 if (bootverbose)
129                         printf(
130                     "SRAT: Found memory domain %d addr %jx len %jx: %s\n",
131                             mem->ProximityDomain, (uintmax_t)mem->BaseAddress,
132                             (uintmax_t)mem->Length,
133                             (mem->Flags & ACPI_SRAT_MEM_ENABLED) ?
134                             "enabled" : "disabled");
135                 if (!(mem->Flags & ACPI_SRAT_MEM_ENABLED))
136                         break;
137                 if (!overlaps_phys_avail(mem->BaseAddress,
138                     mem->BaseAddress + mem->Length)) {
139                         printf("SRAT: Ignoring memory at addr %jx\n",
140                             (uintmax_t)mem->BaseAddress);
141                         break;
142                 }
143                 if (num_mem == VM_PHYSSEG_MAX) {
144                         printf("SRAT: Too many memory regions\n");
145                         *(int *)arg = ENXIO;
146                         break;
147                 }
148                 slot = num_mem;
149                 for (i = 0; i < num_mem; i++) {
150                         if (mem_info[i].end <= mem->BaseAddress)
151                                 continue;
152                         if (mem_info[i].start <
153                             (mem->BaseAddress + mem->Length)) {
154                                 printf("SRAT: Overlapping memory entries\n");
155                                 *(int *)arg = ENXIO;
156                                 return;
157                         }
158                         slot = i;
159                 }
160                 for (i = num_mem; i > slot; i--)
161                         mem_info[i] = mem_info[i - 1];
162                 mem_info[slot].start = mem->BaseAddress;
163                 mem_info[slot].end = mem->BaseAddress + mem->Length;
164                 mem_info[slot].domain = mem->ProximityDomain;
165                 num_mem++;
166                 break;
167         }
168 }
169
170 /*
171  * Ensure each memory domain has at least one CPU and that each CPU
172  * has at least one memory domain.
173  */
174 static int
175 check_domains(void)
176 {
177         int found, i, j;
178
179         for (i = 0; i < num_mem; i++) {
180                 found = 0;
181                 for (j = 0; j <= MAX_APIC_ID; j++)
182                         if (cpus[j].enabled &&
183                             cpus[j].domain == mem_info[i].domain) {
184                                 cpus[j].has_memory = 1;
185                                 found++;
186                         }
187                 if (!found) {
188                         printf("SRAT: No CPU found for memory domain %d\n",
189                             mem_info[i].domain);
190                         return (ENXIO);
191                 }
192         }
193         for (i = 0; i <= MAX_APIC_ID; i++)
194                 if (cpus[i].enabled && !cpus[i].has_memory) {
195                         printf("SRAT: No memory found for CPU %d\n", i);
196                         return (ENXIO);
197                 }
198         return (0);
199 }
200
201 /*
202  * Check that the SRAT memory regions cover all of the regions in
203  * phys_avail[].
204  */
205 static int
206 check_phys_avail(void)
207 {
208         vm_paddr_t address;
209         int i, j;
210
211         /* j is the current offset into phys_avail[]. */
212         address = phys_avail[0];
213         j = 0;
214         for (i = 0; i < num_mem; i++) {
215                 /*
216                  * Consume as many phys_avail[] entries as fit in this
217                  * region.
218                  */
219                 while (address >= mem_info[i].start &&
220                     address <= mem_info[i].end) {
221                         /*
222                          * If we cover the rest of this phys_avail[] entry,
223                          * advance to the next entry.
224                          */
225                         if (phys_avail[j + 1] <= mem_info[i].end) {
226                                 j += 2;
227                                 if (phys_avail[j] == 0 &&
228                                     phys_avail[j + 1] == 0) {
229                                         return (0);
230                                 }
231                                 address = phys_avail[j];
232                         } else
233                                 address = mem_info[i].end + 1;
234                 }
235         }
236         printf("SRAT: No memory region found for %jx - %jx\n",
237             (uintmax_t)phys_avail[j], (uintmax_t)phys_avail[j + 1]);
238         return (ENXIO);
239 }
240
241 /*
242  * Renumber the memory domains to be compact and zero-based if not
243  * already.  Returns an error if there are too many domains.
244  */
245 static int
246 renumber_domains(void)
247 {
248         int domains[VM_PHYSSEG_MAX];
249         int ndomain, i, j, slot;
250
251         /* Enumerate all the domains. */
252         ndomain = 0;
253         for (i = 0; i < num_mem; i++) {
254                 /* See if this domain is already known. */
255                 for (j = 0; j < ndomain; j++) {
256                         if (domains[j] >= mem_info[i].domain)
257                                 break;
258                 }
259                 if (j < ndomain && domains[j] == mem_info[i].domain)
260                         continue;
261
262                 /* Insert the new domain at slot 'j'. */
263                 slot = j;
264                 for (j = ndomain; j > slot; j--)
265                         domains[j] = domains[j - 1];
266                 domains[slot] = mem_info[i].domain;
267                 ndomain++;
268                 if (ndomain > VM_NDOMAIN) {
269                         printf("SRAT: Too many memory domains\n");
270                         return (EFBIG);
271                 }
272         }
273
274         /* Renumber each domain to its index in the sorted 'domains' list. */
275         for (i = 0; i < ndomain; i++) {
276                 /*
277                  * If the domain is already the right value, no need
278                  * to renumber.
279                  */
280                 if (domains[i] == i)
281                         continue;
282
283                 /* Walk the cpu[] and mem_info[] arrays to renumber. */
284                 for (j = 0; j < num_mem; j++)
285                         if (mem_info[j].domain == domains[i])
286                                 mem_info[j].domain = i;
287                 for (j = 0; j <= MAX_APIC_ID; j++)
288                         if (cpus[j].enabled && cpus[j].domain == domains[i])
289                                 cpus[j].domain = i;
290         }
291         return (0);
292 }
293
294 /*
295  * Look for an ACPI System Resource Affinity Table ("SRAT")
296  */
297 static void
298 parse_srat(void *dummy)
299 {
300         int error;
301
302         if (resource_disabled("srat", 0))
303                 return;
304
305         srat_physaddr = acpi_find_table(ACPI_SIG_SRAT);
306         if (srat_physaddr == 0)
307                 return;
308
309         /*
310          * Make a pass over the table to populate the cpus[] and
311          * mem_info[] tables.
312          */
313         srat = acpi_map_table(srat_physaddr, ACPI_SIG_SRAT);
314         error = 0;
315         srat_walk_table(srat_parse_entry, &error);
316         acpi_unmap_table(srat);
317         srat = NULL;
318         if (error || check_domains() != 0 || check_phys_avail() != 0 ||
319             renumber_domains() != 0) {
320                 srat_physaddr = 0;
321                 return;
322         }
323
324         /* Point vm_phys at our memory affinity table. */
325         mem_affinity = mem_info;
326 }
327 SYSINIT(parse_srat, SI_SUB_VM - 1, SI_ORDER_FIRST, parse_srat, NULL);
328
329 static void
330 srat_walk_table(acpi_subtable_handler *handler, void *arg)
331 {
332
333         acpi_walk_subtables(srat + 1, (char *)srat + srat->Header.Length,
334             handler, arg);
335 }
336
337 /*
338  * Setup per-CPU ACPI IDs.
339  */
340 static void
341 srat_set_cpus(void *dummy)
342 {
343         struct cpu_info *cpu;
344         struct pcpu *pc;
345         u_int i;
346
347         if (srat_physaddr == 0)
348                 return;
349         for (i = 0; i < MAXCPU; i++) {
350                 if (CPU_ABSENT(i))
351                         continue;
352                 pc = pcpu_find(i);
353                 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
354                 cpu = &cpus[pc->pc_apic_id];
355                 if (!cpu->enabled)
356                         panic("SRAT: CPU with APIC ID %u is not known",
357                             pc->pc_apic_id);
358                 pc->pc_domain = cpu->domain;
359                 if (bootverbose)
360                         printf("SRAT: CPU %u has memory domain %d\n", i,
361                             cpu->domain);
362         }
363 }
364 SYSINIT(srat_set_cpus, SI_SUB_CPU, SI_ORDER_ANY, srat_set_cpus, NULL);
365 #endif /* VM_NDOMAIN > 1 */