[PATCH] x86_64: Make node boundaries consistent
[safe/jmp/linux-2.6] / arch / x86_64 / mm / srat.c
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
12 #include <linux/kernel.h>
13 #include <linux/acpi.h>
14 #include <linux/mmzone.h>
15 #include <linux/bitmap.h>
16 #include <linux/module.h>
17 #include <linux/topology.h>
18 #include <asm/proto.h>
19 #include <asm/numa.h>
20
21 static struct acpi_table_slit *acpi_slit;
22
23 static nodemask_t nodes_parsed __initdata;
24 static nodemask_t nodes_found __initdata;
25 static struct node nodes[MAX_NUMNODES] __initdata;
26 static __u8  pxm2node[256] = { [0 ... 255] = 0xff };
27
28 static int node_to_pxm(int n);
29
30 int pxm_to_node(int pxm)
31 {
32         if ((unsigned)pxm >= 256)
33                 return 0;
34         return pxm2node[pxm];
35 }
36
37 static __init int setup_node(int pxm)
38 {
39         unsigned node = pxm2node[pxm];
40         if (node == 0xff) {
41                 if (nodes_weight(nodes_found) >= MAX_NUMNODES)
42                         return -1;
43                 node = first_unset_node(nodes_found); 
44                 node_set(node, nodes_found);
45                 pxm2node[pxm] = node;
46         }
47         return pxm2node[pxm];
48 }
49
50 static __init int conflicting_nodes(unsigned long start, unsigned long end)
51 {
52         int i;
53         for_each_node_mask(i, nodes_parsed) {
54                 struct node *nd = &nodes[i];
55                 if (nd->start == nd->end)
56                         continue;
57                 if (nd->end > start && nd->start < end)
58                         return i;
59                 if (nd->end == end && nd->start == start)
60                         return i;
61         }
62         return -1;
63 }
64
65 static __init void cutoff_node(int i, unsigned long start, unsigned long end)
66 {
67         struct node *nd = &nodes[i];
68         if (nd->start < start) {
69                 nd->start = start;
70                 if (nd->end < nd->start)
71                         nd->start = nd->end;
72         }
73         if (nd->end > end) {
74                 nd->end = end;
75                 if (nd->start > nd->end)
76                         nd->start = nd->end;
77         }
78 }
79
80 static __init void bad_srat(void)
81 {
82         int i;
83         printk(KERN_ERR "SRAT: SRAT not used.\n");
84         acpi_numa = -1;
85         for (i = 0; i < MAX_LOCAL_APIC; i++)
86                 apicid_to_node[i] = NUMA_NO_NODE;
87 }
88
89 static __init inline int srat_disabled(void)
90 {
91         return numa_off || acpi_numa < 0;
92 }
93
94 /* Callback for SLIT parsing */
95 void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
96 {
97         acpi_slit = slit;
98 }
99
100 /* Callback for Proximity Domain -> LAPIC mapping */
101 void __init
102 acpi_numa_processor_affinity_init(struct acpi_table_processor_affinity *pa)
103 {
104         int pxm, node;
105         if (srat_disabled() || pa->flags.enabled == 0)
106                 return;
107         pxm = pa->proximity_domain;
108         node = setup_node(pxm);
109         if (node < 0) {
110                 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
111                 bad_srat();
112                 return;
113         }
114         apicid_to_node[pa->apic_id] = node;
115         acpi_numa = 1;
116         printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
117                pxm, pa->apic_id, node);
118 }
119
120 /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
121 void __init
122 acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma)
123 {
124         struct node *nd;
125         unsigned long start, end;
126         int node, pxm;
127         int i;
128
129         if (srat_disabled() || ma->flags.enabled == 0)
130                 return;
131         pxm = ma->proximity_domain;
132         node = setup_node(pxm);
133         if (node < 0) {
134                 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
135                 bad_srat();
136                 return;
137         }
138         start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32);
139         end = start + (ma->length_lo | ((u64)ma->length_hi << 32));
140         /* It is fine to add this area to the nodes data it will be used later*/
141         if (ma->flags.hot_pluggable == 1)
142                 printk(KERN_INFO "SRAT: hot plug zone found %lx - %lx \n",
143                                 start, end);
144         i = conflicting_nodes(start, end);
145         if (i == node) {
146                 printk(KERN_WARNING
147                 "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
148                         pxm, start, end, nodes[i].start, nodes[i].end);
149         } else if (i >= 0) {
150                 printk(KERN_ERR
151                        "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
152                        pxm, start, end, node_to_pxm(i),
153                         nodes[i].start, nodes[i].end);
154                 bad_srat();
155                 return;
156         }
157         nd = &nodes[node];
158         if (!node_test_and_set(node, nodes_parsed)) {
159                 nd->start = start;
160                 nd->end = end;
161         } else {
162                 if (start < nd->start)
163                         nd->start = start;
164                 if (nd->end < end)
165                         nd->end = end;
166         }
167         printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm,
168                nd->start, nd->end);
169 }
170
171 void __init acpi_numa_arch_fixup(void) {}
172
173 /* Use the information discovered above to actually set up the nodes. */
174 int __init acpi_scan_nodes(unsigned long start, unsigned long end)
175 {
176         int i;
177         if (acpi_numa <= 0)
178                 return -1;
179
180         /* First clean up the node list */
181         for_each_node_mask(i, nodes_parsed) {
182                 cutoff_node(i, start, end);
183                 if (nodes[i].start == nodes[i].end)
184                         node_clear(i, nodes_parsed);
185         }
186
187         memnode_shift = compute_hash_shift(nodes, nodes_weight(nodes_parsed));
188         if (memnode_shift < 0) {
189                 printk(KERN_ERR
190                      "SRAT: No NUMA node hash function found. Contact maintainer\n");
191                 bad_srat();
192                 return -1;
193         }
194
195         /* Finally register nodes */
196         for_each_node_mask(i, nodes_parsed)
197                 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
198         for (i = 0; i < NR_CPUS; i++) { 
199                 if (cpu_to_node[i] == NUMA_NO_NODE)
200                         continue;
201                 if (!node_isset(cpu_to_node[i], nodes_parsed))
202                         numa_set_node(i, NUMA_NO_NODE);
203         }
204         numa_init_array();
205         return 0;
206 }
207
208 static int node_to_pxm(int n)
209 {
210        int i;
211        if (pxm2node[n] == n)
212                return n;
213        for (i = 0; i < 256; i++)
214                if (pxm2node[i] == n)
215                        return i;
216        return 0;
217 }
218
219 int __node_distance(int a, int b)
220 {
221         int index;
222
223         if (!acpi_slit)
224                 return a == b ? 10 : 20;
225         index = acpi_slit->localities * node_to_pxm(a);
226         return acpi_slit->entry[index + node_to_pxm(b)];
227 }
228
229 EXPORT_SYMBOL(__node_distance);