xref: /dragonfly/sys/platform/pc64/acpica/acpi_srat.c (revision b8c93cad)
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 	int enabled = 1;
72 	int disabled = 0;
73 
74 	/*
75 	 * Allow NUMA to be disabled by either setting kern.numa_disable
76 	 * to 1 or by setting hw.acpi.srat.enabled to 0.
77 	 */
78 	kgetenv_int("kern.numa_disable", &disabled);
79 	kgetenv_int("hw.acpi.srat.enabled", &enabled);
80 	if (disabled || enabled == 0)
81 		return;
82 
83 	/*
84 	 * Map the SRAT if it exists
85 	 */
86 	srat_paddr = sdt_search(ACPI_SIG_SRAT);
87 	if (srat_paddr == 0) {
88 		kprintf("srat_probe: can't locate SRAT\n");
89 		return;
90 	}
91 
92 	srat = sdt_sdth_map(srat_paddr);
93 	KKASSERT(srat != NULL);
94 
95 	if (srat->Header.Length < sizeof(*srat)) {
96 		kprintf("acpi: invalid SRAT length %u\n",
97 			srat->Header.Length);
98 		error = EINVAL;
99 		goto done;
100 	}
101 
102 	cpu = NULL;
103 
104 	for (mem = (srat_entry_t *)(srat + 1);
105 	     (char *)mem < (char *)srat + srat->Header.Length;
106 	     mem = (srat_entry_t *)((char *)mem + mem->head.Length)) {
107 		/*
108 		 * Mem scan memory affinity only
109 		 */
110 		if (mem->head.Type != ACPI_SRAT_TYPE_MEMORY_AFFINITY)
111 			continue;
112 		if ((mem->mem.Flags & ACPI_SRAT_MEM_ENABLED) == 0)
113 			continue;
114 
115 		kprintf("MemAffinity %016jx,%ldMB Prox=%u ",
116 			mem->mem.BaseAddress,
117 			mem->mem.Length / (1024 * 1024),
118 			mem->mem.ProximityDomain);
119 
120 		/*
121 		 * Look for associated cpu affinity
122 		 */
123 		if (cpu == NULL ||
124 		    mem->mem.ProximityDomain != cpu->cpu.ProximityDomainLo) {
125 			for (cpu = (srat_entry_t *)(srat + 1);
126 			     (char *)cpu < (char *)srat + srat->Header.Length;
127 			     cpu = (srat_entry_t *)((char *)cpu +
128 						    cpu->head.Length)) {
129 				if (cpu->head.Type !=
130 				    ACPI_SRAT_TYPE_CPU_AFFINITY)
131 					continue;
132 				if ((cpu->cpu.Flags &
133 				     ACPI_SRAT_CPU_USE_AFFINITY) == 0)
134 					continue;
135 				if (mem->mem.ProximityDomain ==
136 				    cpu->cpu.ProximityDomainLo) {
137 					break;
138 				}
139 			}
140 			if ((char *)cpu >= (char *)srat + srat->Header.Length)
141 				cpu = NULL;
142 		}
143 		if (cpu) {
144 			kprintf("CpuApicId %02x Socket %d\n",
145 				cpu->cpu.ApicId,
146 				get_chip_ID_from_APICID(cpu->cpu.ApicId));
147 			vm_numa_organize(mem->mem.BaseAddress,
148 					 mem->mem.Length,
149 				    get_chip_ID_from_APICID(cpu->cpu.ApicId));
150 		} else {
151 			kprintf("(not found)\n");
152 		}
153 	}
154 
155 done:
156 	sdt_sdth_unmap(&srat->Header);
157 }
158 
159 SYSINIT(srat_probe, SI_BOOT2_NUMA, SI_ORDER_FIRST, srat_probe, 0);
160