xref: /dragonfly/sys/platform/pc64/acpica/acpi_srat.c (revision 6e316fcd)
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 
44 typedef union srat_entry {
45 	ACPI_SUBTABLE_HEADER	head;
46 	ACPI_SRAT_CPU_AFFINITY	cpu;
47 	ACPI_SRAT_MEM_AFFINITY	mem;
48 	ACPI_SRAT_X2APIC_CPU_AFFINITY	x2apic;
49 	ACPI_SRAT_GICC_AFFINITY	gicc;
50 } srat_entry_t;
51 
52 static void
53 srat_probe(void)
54 {
55 	vm_paddr_t srat_paddr;
56 	ACPI_TABLE_SRAT *srat;
57 	srat_entry_t *mem;
58 	srat_entry_t *cpu;
59 	int error = 0;
60 	int enabled = 1;
61 	int disabled = 0;
62 
63 	/*
64 	 * Allow NUMA to be disabled by either setting kern.numa_disable
65 	 * to 1 or by setting hw.acpi.srat.enabled to 0.
66 	 */
67 	kgetenv_int("kern.numa_disable", &disabled);
68 	kgetenv_int("hw.acpi.srat.enabled", &enabled);
69 	if (disabled || enabled == 0)
70 		return;
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 	vm_numa_organize_finalize();
144 
145 done:
146 	sdt_sdth_unmap(&srat->Header);
147 }
148 
149 SYSINIT(srat_probe, SI_BOOT2_NUMA, SI_ORDER_FIRST, srat_probe, 0);
150