ia64/xen-unstable

view xen/arch/x86/srat.c @ 19633:89e50c449307

ACPI/NUMA: Improve SRAT parsing

This is to properly handle SRAT rev 2 extended proximity domain
values.

Also a first step to eliminate the redundant definitions of
ACPI provided table structures (Linux eliminated all of the duplicates
from include/linux/acpi.h in 2.6.21).

Portions based on a Linux patch from Kurt Garloff <garloff@suse.de>
and Alexey Starikovskiy <astarikovskiy@suse.de>.

IA64 build tested only.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed May 20 16:02:50 2009 +0100 (2009-05-20)
parents 13cba2e78a65
children
line source
1 /*
2 * ACPI 3.0 based NUMA setup
3 * Copyright 2004 Andi Kleen, SuSE Labs.
4 *
5 * Reads the ACPI SRAT table to figure out what memory belongs to which CPUs.
6 *
7 * Called from acpi_numa_init while reading the SRAT and SLIT tables.
8 * Assumes all memory regions belonging to a single proximity domain
9 * are in one chunk. Holes between them will be included in the node.
10 *
11 * Adapted for Xen: Ryan Harper <ryanh@us.ibm.com>
12 */
14 #include <xen/init.h>
15 #include <xen/mm.h>
16 #include <xen/inttypes.h>
17 #include <xen/nodemask.h>
18 #include <xen/acpi.h>
19 #include <xen/numa.h>
20 #include <asm/page.h>
22 static struct acpi_table_slit *acpi_slit;
24 static nodemask_t nodes_parsed __initdata;
25 static nodemask_t nodes_found __initdata;
26 static struct node nodes[MAX_NUMNODES] __initdata;
27 static u8 pxm2node[256] = { [0 ... 255] = 0xff };
29 /* Too small nodes confuse the VM badly. Usually they result
30 from BIOS bugs. */
31 #define NODE_MIN_SIZE (4*1024*1024)
33 static int node_to_pxm(int n);
35 int pxm_to_node(int pxm)
36 {
37 if ((unsigned)pxm >= 256)
38 return -1;
39 /* Extend 0xff to (int)-1 */
40 return (signed char)pxm2node[pxm];
41 }
43 static __init int setup_node(int pxm)
44 {
45 unsigned node = pxm2node[pxm];
46 if (node == 0xff) {
47 if (nodes_weight(nodes_found) >= MAX_NUMNODES)
48 return -1;
49 node = first_unset_node(nodes_found);
50 node_set(node, nodes_found);
51 pxm2node[pxm] = node;
52 }
53 return pxm2node[pxm];
54 }
56 static __init int conflicting_nodes(u64 start, u64 end)
57 {
58 int i;
59 for_each_node_mask(i, nodes_parsed) {
60 struct node *nd = &nodes[i];
61 if (nd->start == nd->end)
62 continue;
63 if (nd->end > start && nd->start < end)
64 return i;
65 if (nd->end == end && nd->start == start)
66 return i;
67 }
68 return -1;
69 }
71 static __init void cutoff_node(int i, u64 start, u64 end)
72 {
73 struct node *nd = &nodes[i];
74 if (nd->start < start) {
75 nd->start = start;
76 if (nd->end < nd->start)
77 nd->start = nd->end;
78 }
79 if (nd->end > end) {
80 nd->end = end;
81 if (nd->start > nd->end)
82 nd->start = nd->end;
83 }
84 }
86 static __init void bad_srat(void)
87 {
88 int i;
89 printk(KERN_ERR "SRAT: SRAT not used.\n");
90 acpi_numa = -1;
91 for (i = 0; i < MAX_LOCAL_APIC; i++)
92 apicid_to_node[i] = NUMA_NO_NODE;
93 }
95 static __init inline int srat_disabled(void)
96 {
97 return numa_off || acpi_numa < 0;
98 }
100 /*
101 * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
102 * up the NUMA heuristics which wants the local node to have a smaller
103 * distance than the others.
104 * Do some quick checks here and only use the SLIT if it passes.
105 */
106 static __init int slit_valid(struct acpi_table_slit *slit)
107 {
108 int i, j;
109 int d = slit->locality_count;
110 for (i = 0; i < d; i++) {
111 for (j = 0; j < d; j++) {
112 u8 val = slit->entry[d*i + j];
113 if (i == j) {
114 if (val != 10)
115 return 0;
116 } else if (val <= 10)
117 return 0;
118 }
119 }
120 return 1;
121 }
123 /* Callback for SLIT parsing */
124 void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
125 {
126 if (!slit_valid(slit)) {
127 printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
128 return;
129 }
130 acpi_slit = slit;
131 }
133 /* Callback for Proximity Domain -> LAPIC mapping */
134 void __init
135 acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
136 {
137 int pxm, node;
138 if (srat_disabled())
139 return;
140 if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
141 bad_srat();
142 return;
143 }
144 if (!(pa->flags & ACPI_SRAT_CPU_ENABLED))
145 return;
146 pxm = pa->proximity_domain_lo;
147 if (srat_rev >= 2) {
148 pxm |= pa->proximity_domain_hi[0] << 8;
149 pxm |= pa->proximity_domain_hi[1] << 16;
150 pxm |= pa->proximity_domain_hi[2] << 24;
151 }
152 node = setup_node(pxm);
153 if (node < 0) {
154 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
155 bad_srat();
156 return;
157 }
158 apicid_to_node[pa->apic_id] = node;
159 acpi_numa = 1;
160 printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
161 pxm, pa->apic_id, node);
162 }
164 /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
165 void __init
166 acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
167 {
168 struct node *nd;
169 u64 start, end;
170 int node, pxm;
171 int i;
173 if (srat_disabled())
174 return;
175 if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
176 bad_srat();
177 return;
178 }
179 if (!(ma->flags & ACPI_SRAT_MEM_ENABLED))
180 return;
181 start = ma->base_address;
182 end = start + ma->length;
183 pxm = ma->proximity_domain;
184 if (srat_rev < 2)
185 pxm &= 0xff;
186 node = setup_node(pxm);
187 if (node < 0) {
188 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
189 bad_srat();
190 return;
191 }
192 /* It is fine to add this area to the nodes data it will be used later*/
193 if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
194 printk(KERN_INFO "SRAT: hot plug zone found %"PRIx64" - %"PRIx64" \n",
195 start, end);
196 i = conflicting_nodes(start, end);
197 if (i == node) {
198 printk(KERN_WARNING
199 "SRAT: Warning: PXM %d (%"PRIx64"-%"PRIx64") overlaps with itself (%"
200 PRIx64"-%"PRIx64")\n", pxm, start, end, nodes[i].start, nodes[i].end);
201 } else if (i >= 0) {
202 printk(KERN_ERR
203 "SRAT: PXM %d (%"PRIx64"-%"PRIx64") overlaps with PXM %d (%"
204 PRIx64"-%"PRIx64")\n", pxm, start, end, node_to_pxm(i),
205 nodes[i].start, nodes[i].end);
206 bad_srat();
207 return;
208 }
209 nd = &nodes[node];
210 if (!node_test_and_set(node, nodes_parsed)) {
211 nd->start = start;
212 nd->end = end;
213 } else {
214 if (start < nd->start)
215 nd->start = start;
216 if (nd->end < end)
217 nd->end = end;
218 }
219 printk(KERN_INFO "SRAT: Node %u PXM %u %"PRIx64"-%"PRIx64"\n", node, pxm,
220 nd->start, nd->end);
221 }
223 /* Sanity check to catch more bad SRATs (they are amazingly common).
224 Make sure the PXMs cover all memory. */
225 static int nodes_cover_memory(void)
226 {
227 int i;
228 u64 pxmram, e820ram;
230 pxmram = 0;
231 for_each_node_mask(i, nodes_parsed) {
232 u64 s = nodes[i].start >> PAGE_SHIFT;
233 u64 e = nodes[i].end >> PAGE_SHIFT;
234 pxmram += e - s;
235 }
237 e820ram = max_page;
238 /* We seem to lose 3 pages somewhere. Allow a bit of slack. */
239 if ((long)(e820ram - pxmram) >= 1*1024*1024) {
240 printk(KERN_ERR "SRAT: PXMs only cover %"PRIu64"MB of your %"
241 PRIu64"MB e820 RAM. Not used.\n",
242 (pxmram << PAGE_SHIFT) >> 20,
243 (e820ram << PAGE_SHIFT) >> 20);
244 return 0;
245 }
246 return 1;
247 }
249 static void unparse_node(int node)
250 {
251 int i;
252 node_clear(node, nodes_parsed);
253 for (i = 0; i < MAX_LOCAL_APIC; i++) {
254 if (apicid_to_node[i] == node)
255 apicid_to_node[i] = NUMA_NO_NODE;
256 }
257 }
259 void __init acpi_numa_arch_fixup(void) {}
261 /* Use the information discovered above to actually set up the nodes. */
262 int __init acpi_scan_nodes(u64 start, u64 end)
263 {
264 int i;
266 /* First clean up the node list */
267 for (i = 0; i < MAX_NUMNODES; i++) {
268 cutoff_node(i, start, end);
269 if ((nodes[i].end - nodes[i].start) < NODE_MIN_SIZE)
270 unparse_node(i);
271 }
273 if (acpi_numa <= 0)
274 return -1;
276 if (!nodes_cover_memory()) {
277 bad_srat();
278 return -1;
279 }
281 memnode_shift = compute_hash_shift(nodes, MAX_NUMNODES);
282 if (memnode_shift < 0) {
283 printk(KERN_ERR
284 "SRAT: No NUMA node hash function found. Contact maintainer\n");
285 bad_srat();
286 return -1;
287 }
289 /* Finally register nodes */
290 for_each_node_mask(i, nodes_parsed)
291 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
292 for (i = 0; i < NR_CPUS; i++) {
293 if (cpu_to_node[i] == NUMA_NO_NODE)
294 continue;
295 if (!node_isset(cpu_to_node[i], nodes_parsed))
296 numa_set_node(i, NUMA_NO_NODE);
297 }
298 numa_init_array();
299 return 0;
300 }
302 static int node_to_pxm(int n)
303 {
304 int i;
305 if (pxm2node[n] == n)
306 return n;
307 for (i = 0; i < 256; i++)
308 if (pxm2node[i] == n)
309 return i;
310 return 0;
311 }
313 int __node_distance(int a, int b)
314 {
315 int index;
317 if (!acpi_slit)
318 return a == b ? 10 : 20;
319 index = acpi_slit->locality_count * node_to_pxm(a);
320 return acpi_slit->entry[index + node_to_pxm(b)];
321 }
323 EXPORT_SYMBOL(__node_distance);