kernel - Add NUMA awareness to vm_page_alloc() and related functions
[dragonfly.git] / sys / platform / pc64 / acpica / acpi_srat.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <vm/vm_page.h>
40
41 #include "acpi.h"
42 #include "acpi_sdt_var.h"
43 #include "acpi_sci_var.h"
44
45 extern int naps;
46
47 #define MADT_VPRINTF(fmt, arg...) \
48 do { \
49         if (bootverbose) \
50                 kprintf("ACPI MADT: " fmt , ##arg); \
51 } while (0)
52
53 #define MADT_INT_BUS_ISA        0
54
55 typedef union srat_entry {
56         ACPI_SUBTABLE_HEADER    head;
57         ACPI_SRAT_CPU_AFFINITY  cpu;
58         ACPI_SRAT_MEM_AFFINITY  mem;
59         ACPI_SRAT_X2APIC_CPU_AFFINITY   x2apic;
60         ACPI_SRAT_GICC_AFFINITY gicc;
61 } srat_entry_t;
62
63 static void
64 srat_probe(void)
65 {
66         vm_paddr_t srat_paddr;
67         ACPI_TABLE_SRAT *srat;
68         srat_entry_t *mem;
69         srat_entry_t *cpu;
70         int error = 0;
71
72         /*
73          * Map the SRAT if it exists
74          */
75         srat_paddr = sdt_search(ACPI_SIG_SRAT);
76         if (srat_paddr == 0) {
77                 kprintf("srat_probe: can't locate SRAT\n");
78                 return;
79         }
80
81         srat = sdt_sdth_map(srat_paddr);
82         KKASSERT(srat != NULL);
83
84         if (srat->Header.Length < sizeof(*srat)) {
85                 kprintf("acpi: invalid SRAT length %u\n",
86                         srat->Header.Length);
87                 error = EINVAL;
88                 goto done;
89         }
90
91         cpu = NULL;
92
93         for (mem = (srat_entry_t *)(srat + 1);
94              (char *)mem < (char *)srat + srat->Header.Length;
95              mem = (srat_entry_t *)((char *)mem + mem->head.Length)) {
96                 /*
97                  * Mem scan memory affinity only
98                  */
99                 if (mem->head.Type != ACPI_SRAT_TYPE_MEMORY_AFFINITY)
100                         continue;
101                 if ((mem->mem.Flags & ACPI_SRAT_MEM_ENABLED) == 0)
102                         continue;
103
104                 kprintf("MemAffinity %016jx,%ldMB Prox=%u ",
105                         mem->mem.BaseAddress,
106                         mem->mem.Length / (1024 * 1024),
107                         mem->mem.ProximityDomain);
108
109                 /*
110                  * Look for associated cpu affinity
111                  */
112                 if (cpu == NULL ||
113                     mem->mem.ProximityDomain != cpu->cpu.ProximityDomainLo) {
114                         for (cpu = (srat_entry_t *)(srat + 1);
115                              (char *)cpu < (char *)srat + srat->Header.Length;
116                              cpu = (srat_entry_t *)((char *)cpu +
117                                                     cpu->head.Length)) {
118                                 if (cpu->head.Type !=
119                                     ACPI_SRAT_TYPE_CPU_AFFINITY)
120                                         continue;
121                                 if ((cpu->cpu.Flags &
122                                      ACPI_SRAT_CPU_USE_AFFINITY) == 0)
123                                         continue;
124                                 if (mem->mem.ProximityDomain ==
125                                     cpu->cpu.ProximityDomainLo) {
126                                         break;
127                                 }
128                         }
129                         if ((char *)cpu >= (char *)srat + srat->Header.Length)
130                                 cpu = NULL;
131                 }
132                 if (cpu) {
133                         kprintf("CpuApicId %02x Socket %d\n",
134                                 cpu->cpu.ApicId,
135                                 get_chip_ID_from_APICID(cpu->cpu.ApicId));
136                         vm_numa_organize(mem->mem.BaseAddress,
137                                          mem->mem.Length,
138                                     get_chip_ID_from_APICID(cpu->cpu.ApicId));
139                 } else {
140                         kprintf("(not found)\n");
141                 }
142         }
143
144 done:
145         sdt_sdth_unmap(&srat->Header);
146 }
147
148 SYSINIT(srat_probe, SI_BOOT2_NUMA, SI_ORDER_FIRST, srat_probe, 0);