1 /*
2  *  acpi_numa.c - ACPI NUMA support
3  *
4  *  Copyright (C) 2002 Takayoshi Kochi <t-kochi@bq.jp.nec.com>
5  *
6  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; If not, see <http://www.gnu.org/licenses/>.
20  *
21  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22  *
23  */
24 #include <xen/init.h>
25 #include <xen/types.h>
26 #include <xen/errno.h>
27 #include <xen/acpi.h>
28 #include <xen/numa.h>
29 #include <acpi/acmacros.h>
30 
31 #define ACPI_NUMA	0x80000000
32 #define _COMPONENT	ACPI_NUMA
33 ACPI_MODULE_NAME("numa")
34 
35 int __initdata srat_rev;
36 
acpi_table_print_srat_entry(struct acpi_subtable_header * header)37 void __init acpi_table_print_srat_entry(struct acpi_subtable_header * header)
38 {
39 
40 	ACPI_FUNCTION_NAME("acpi_table_print_srat_entry");
41 
42 	if (!header)
43 		return;
44 
45 	switch (header->type) {
46 
47 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
48 #ifdef ACPI_DEBUG_OUTPUT
49 		{
50 			struct acpi_srat_cpu_affinity *p =
51 			    container_of(header, struct acpi_srat_cpu_affinity, header);
52 			u32 proximity_domain = p->proximity_domain_lo;
53 
54 			if (srat_rev >= 2) {
55 				proximity_domain |= p->proximity_domain_hi[0] << 8;
56 				proximity_domain |= p->proximity_domain_hi[1] << 16;
57 				proximity_domain |= p->proximity_domain_hi[2] << 24;
58 			}
59 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
60 					  "SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
61 					  p->apic_id, p->local_sapic_eid,
62 					  proximity_domain,
63 					  p->flags & ACPI_SRAT_CPU_ENABLED
64 					  ? "enabled" : "disabled"));
65 		}
66 #endif				/* ACPI_DEBUG_OUTPUT */
67 		break;
68 
69 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
70 #ifdef ACPI_DEBUG_OUTPUT
71 		{
72 			struct acpi_srat_mem_affinity *p =
73 			    container_of(header, struct acpi_srat_mem_affinity, header);
74 			u32 proximity_domain = p->proximity_domain;
75 
76 			if (srat_rev < 2)
77 				proximity_domain &= 0xff;
78 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
79 					  "SRAT Memory (%#"PRIx64
80 					  " length %#"PRIx64")"
81 					  " in proximity domain %d %s%s\n",
82 					  p->base_address, p->length,
83 					  proximity_domain,
84 					  p->flags & ACPI_SRAT_MEM_ENABLED
85 					  ? "enabled" : "disabled",
86 					  p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE
87 					  ? " hot-pluggable" : ""));
88 		}
89 #endif				/* ACPI_DEBUG_OUTPUT */
90 		break;
91 
92 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
93 #ifdef ACPI_DEBUG_OUTPUT
94 		{
95 			struct acpi_srat_x2apic_cpu_affinity *p =
96 			    (struct acpi_srat_x2apic_cpu_affinity *)header;
97 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
98 					  "SRAT Processor (x2apicid[0x%08x]) in"
99 					  " proximity domain %d %s\n",
100 					  p->apic_id,
101 					  p->proximity_domain,
102 					  (p->flags & ACPI_SRAT_CPU_ENABLED) ?
103 					  "enabled" : "disabled"));
104 		}
105 #endif				/* ACPI_DEBUG_OUTPUT */
106 		break;
107 	default:
108 		printk(KERN_WARNING PREFIX
109 		       "Found unsupported SRAT entry (type = %#x)\n",
110 		       header->type);
111 		break;
112 	}
113 }
114 
acpi_parse_slit(struct acpi_table_header * table)115 static int __init acpi_parse_slit(struct acpi_table_header *table)
116 {
117 	acpi_numa_slit_init((struct acpi_table_slit *)table);
118 
119 	return 0;
120 }
121 
122 static int __init
acpi_parse_x2apic_affinity(struct acpi_subtable_header * header,const unsigned long end)123 acpi_parse_x2apic_affinity(struct acpi_subtable_header *header,
124 			   const unsigned long end)
125 {
126 	const struct acpi_srat_x2apic_cpu_affinity *processor_affinity
127 		= container_of(header, struct acpi_srat_x2apic_cpu_affinity,
128 			       header);
129 
130 	if (!header)
131 		return -EINVAL;
132 
133 	acpi_table_print_srat_entry(header);
134 
135 	/* let architecture-dependent part to do it */
136 	acpi_numa_x2apic_affinity_init(processor_affinity);
137 
138 	return 0;
139 }
140 
141 static int __init
acpi_parse_processor_affinity(struct acpi_subtable_header * header,const unsigned long end)142 acpi_parse_processor_affinity(struct acpi_subtable_header *header,
143 			      const unsigned long end)
144 {
145 	const struct acpi_srat_cpu_affinity *processor_affinity
146 		= container_of(header, struct acpi_srat_cpu_affinity, header);
147 
148 	if (!header)
149 		return -EINVAL;
150 
151 	acpi_table_print_srat_entry(header);
152 
153 	/* let architecture-dependent part to do it */
154 	acpi_numa_processor_affinity_init(processor_affinity);
155 
156 	return 0;
157 }
158 
159 static int __init
acpi_parse_memory_affinity(struct acpi_subtable_header * header,const unsigned long end)160 acpi_parse_memory_affinity(struct acpi_subtable_header *header,
161 			   const unsigned long end)
162 {
163 	const struct acpi_srat_mem_affinity *memory_affinity
164 		= container_of(header, struct acpi_srat_mem_affinity, header);
165 
166 	if (!header)
167 		return -EINVAL;
168 
169 	acpi_table_print_srat_entry(header);
170 
171 	/* let architecture-dependent part to do it */
172 	acpi_numa_memory_affinity_init(memory_affinity);
173 
174 	return 0;
175 }
176 
acpi_parse_srat(struct acpi_table_header * table)177 int __init acpi_parse_srat(struct acpi_table_header *table)
178 {
179 	if (!table)
180 		return -EINVAL;
181 
182 	srat_rev = table->revision;
183 
184 	return 0;
185 }
186 
187 int __init
acpi_table_parse_srat(int id,acpi_madt_entry_handler handler,unsigned int max_entries)188 acpi_table_parse_srat(int id, acpi_madt_entry_handler handler,
189 		      unsigned int max_entries)
190 {
191 	return acpi_table_parse_entries(ACPI_SIG_SRAT,
192 					sizeof(struct acpi_table_srat), id,
193 					handler, max_entries);
194 }
195 
acpi_numa_init(void)196 int __init acpi_numa_init(void)
197 {
198 	/* SRAT: Static Resource Affinity Table */
199 	if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
200 		acpi_table_parse_srat(ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY,
201 				      acpi_parse_x2apic_affinity, 0);
202 		acpi_table_parse_srat(ACPI_SRAT_TYPE_CPU_AFFINITY,
203 				      acpi_parse_processor_affinity, 0);
204 		acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
205 				      acpi_parse_memory_affinity,
206 				      NR_NODE_MEMBLKS);
207 	}
208 
209 	/* SLIT: System Locality Information Table */
210 	acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
211 
212 	acpi_numa_arch_fixup();
213 	return 0;
214 }
215 
216 #if 0
217 int acpi_get_pxm(acpi_handle h)
218 {
219 	unsigned long pxm;
220 	acpi_status status;
221 	acpi_handle handle;
222 	acpi_handle phandle = h;
223 
224 	do {
225 		handle = phandle;
226 		status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
227 		if (ACPI_SUCCESS(status))
228 			return (int)pxm;
229 		status = acpi_get_parent(handle, &phandle);
230 	} while (ACPI_SUCCESS(status));
231 	return -1;
232 }
233 
234 EXPORT_SYMBOL(acpi_get_pxm);
235 #endif
236