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