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