x86-64, NUMA: Implement generic node distance handling
[cascardo/linux.git] / arch / x86 / mm / srat_64.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 <linux/bootmem.h>
19 #include <linux/memblock.h>
20 #include <linux/mm.h>
21 #include <asm/proto.h>
22 #include <asm/numa.h>
23 #include <asm/e820.h>
24 #include <asm/apic.h>
25 #include <asm/uv/uv.h>
26
27 int acpi_numa __initdata;
28
29 static struct acpi_table_slit *acpi_slit;
30
31 static struct bootnode nodes_add[MAX_NUMNODES];
32
33 static __init int setup_node(int pxm)
34 {
35         return acpi_map_pxm_to_node(pxm);
36 }
37
38 static __init void bad_srat(void)
39 {
40         printk(KERN_ERR "SRAT: SRAT not used.\n");
41         acpi_numa = -1;
42         memset(nodes_add, 0, sizeof(nodes_add));
43 }
44
45 static __init inline int srat_disabled(void)
46 {
47         return acpi_numa < 0;
48 }
49
50 /* Callback for SLIT parsing */
51 void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
52 {
53         int i, j;
54         unsigned length;
55         unsigned long phys;
56
57         for (i = 0; i < slit->locality_count; i++)
58                 for (j = 0; j < slit->locality_count; j++)
59                         numa_set_distance(pxm_to_node(i), pxm_to_node(j),
60                                 slit->entry[slit->locality_count * i + j]);
61
62         /* acpi_slit is used only by emulation */
63         length = slit->header.length;
64         phys = memblock_find_in_range(0, max_pfn_mapped<<PAGE_SHIFT, length,
65                  PAGE_SIZE);
66
67         if (phys == MEMBLOCK_ERROR)
68                 panic(" Can not save slit!\n");
69
70         acpi_slit = __va(phys);
71         memcpy(acpi_slit, slit, length);
72         memblock_x86_reserve_range(phys, phys + length, "ACPI SLIT");
73 }
74
75 /* Callback for Proximity Domain -> x2APIC mapping */
76 void __init
77 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
78 {
79         int pxm, node;
80         int apic_id;
81
82         if (srat_disabled())
83                 return;
84         if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
85                 bad_srat();
86                 return;
87         }
88         if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
89                 return;
90         pxm = pa->proximity_domain;
91         node = setup_node(pxm);
92         if (node < 0) {
93                 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
94                 bad_srat();
95                 return;
96         }
97
98         apic_id = pa->apic_id;
99         if (apic_id >= MAX_LOCAL_APIC) {
100                 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
101                 return;
102         }
103         set_apicid_to_node(apic_id, node);
104         node_set(node, numa_nodes_parsed);
105         acpi_numa = 1;
106         printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u\n",
107                pxm, apic_id, node);
108 }
109
110 /* Callback for Proximity Domain -> LAPIC mapping */
111 void __init
112 acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
113 {
114         int pxm, node;
115         int apic_id;
116
117         if (srat_disabled())
118                 return;
119         if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
120                 bad_srat();
121                 return;
122         }
123         if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
124                 return;
125         pxm = pa->proximity_domain_lo;
126         node = setup_node(pxm);
127         if (node < 0) {
128                 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
129                 bad_srat();
130                 return;
131         }
132
133         if (get_uv_system_type() >= UV_X2APIC)
134                 apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
135         else
136                 apic_id = pa->apic_id;
137
138         if (apic_id >= MAX_LOCAL_APIC) {
139                 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
140                 return;
141         }
142
143         set_apicid_to_node(apic_id, node);
144         node_set(node, numa_nodes_parsed);
145         acpi_numa = 1;
146         printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u\n",
147                pxm, apic_id, node);
148 }
149
150 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
151 static inline int save_add_info(void) {return 1;}
152 #else
153 static inline int save_add_info(void) {return 0;}
154 #endif
155 /*
156  * Update nodes_add[]
157  * This code supports one contiguous hot add area per node
158  */
159 static void __init
160 update_nodes_add(int node, unsigned long start, unsigned long end)
161 {
162         unsigned long s_pfn = start >> PAGE_SHIFT;
163         unsigned long e_pfn = end >> PAGE_SHIFT;
164         int changed = 0;
165         struct bootnode *nd = &nodes_add[node];
166
167         /* I had some trouble with strange memory hotadd regions breaking
168            the boot. Be very strict here and reject anything unexpected.
169            If you want working memory hotadd write correct SRATs.
170
171            The node size check is a basic sanity check to guard against
172            mistakes */
173         if ((signed long)(end - start) < NODE_MIN_SIZE) {
174                 printk(KERN_ERR "SRAT: Hotplug area too small\n");
175                 return;
176         }
177
178         /* This check might be a bit too strict, but I'm keeping it for now. */
179         if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
180                 printk(KERN_ERR
181                         "SRAT: Hotplug area %lu -> %lu has existing memory\n",
182                         s_pfn, e_pfn);
183                 return;
184         }
185
186         /* Looks good */
187
188         if (nd->start == nd->end) {
189                 nd->start = start;
190                 nd->end = end;
191                 changed = 1;
192         } else {
193                 if (nd->start == end) {
194                         nd->start = start;
195                         changed = 1;
196                 }
197                 if (nd->end == start) {
198                         nd->end = end;
199                         changed = 1;
200                 }
201                 if (!changed)
202                         printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
203         }
204
205         if (changed) {
206                 node_set(node, numa_nodes_parsed);
207                 printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n",
208                                  nd->start, nd->end);
209         }
210 }
211
212 /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
213 void __init
214 acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
215 {
216         unsigned long start, end;
217         int node, pxm;
218
219         if (srat_disabled())
220                 return;
221         if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
222                 bad_srat();
223                 return;
224         }
225         if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
226                 return;
227
228         if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
229                 return;
230         start = ma->base_address;
231         end = start + ma->length;
232         pxm = ma->proximity_domain;
233         node = setup_node(pxm);
234         if (node < 0) {
235                 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
236                 bad_srat();
237                 return;
238         }
239
240         if (numa_add_memblk(node, start, end) < 0) {
241                 bad_srat();
242                 return;
243         }
244
245         printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
246                start, end);
247
248         if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
249                 update_nodes_add(node, start, end);
250 }
251
252 void __init acpi_numa_arch_fixup(void) {}
253
254 int __init x86_acpi_numa_init(void)
255 {
256         int ret;
257
258         ret = acpi_numa_init();
259         if (ret < 0)
260                 return ret;
261         return srat_disabled() ? -EINVAL : 0;
262 }
263
264 #ifdef CONFIG_NUMA_EMU
265 static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
266         [0 ... MAX_NUMNODES-1] = PXM_INVAL
267 };
268 static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
269         [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
270 };
271
272 /*
273  * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
274  * mappings that respect the real ACPI topology but reflect our emulated
275  * environment.  For each emulated node, we find which real node it appears on
276  * and create PXM to NID mappings for those fake nodes which mirror that
277  * locality.  SLIT will now represent the correct distances between emulated
278  * nodes as a result of the real topology.
279  */
280 void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
281 {
282         int i, j;
283
284         for (i = 0; i < num_nodes; i++) {
285                 int nid, pxm;
286
287                 nid = find_node_by_addr(fake_nodes[i].start);
288                 if (nid == NUMA_NO_NODE)
289                         continue;
290                 pxm = node_to_pxm(nid);
291                 if (pxm == PXM_INVAL)
292                         continue;
293                 fake_node_to_pxm_map[i] = pxm;
294                 /*
295                  * For each apicid_to_node mapping that exists for this real
296                  * node, it must now point to the fake node ID.
297                  */
298                 for (j = 0; j < MAX_LOCAL_APIC; j++)
299                         if (__apicid_to_node[j] == nid &&
300                             fake_apicid_to_node[j] == NUMA_NO_NODE)
301                                 fake_apicid_to_node[j] = i;
302         }
303
304         /*
305          * If there are apicid-to-node mappings for physical nodes that do not
306          * have a corresponding emulated node, it should default to a guaranteed
307          * value.
308          */
309         for (i = 0; i < MAX_LOCAL_APIC; i++)
310                 if (__apicid_to_node[i] != NUMA_NO_NODE &&
311                     fake_apicid_to_node[i] == NUMA_NO_NODE)
312                         fake_apicid_to_node[i] = 0;
313
314         for (i = 0; i < num_nodes; i++)
315                 __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
316         memcpy(__apicid_to_node, fake_apicid_to_node, sizeof(__apicid_to_node));
317
318         for (i = 0; i < num_nodes; i++)
319                 if (fake_nodes[i].start != fake_nodes[i].end)
320                         node_set(i, numa_nodes_parsed);
321 }
322
323 int acpi_emu_node_distance(int a, int b)
324 {
325         int index;
326
327         if (!acpi_slit)
328                 return node_to_pxm(a) == node_to_pxm(b) ?
329                         LOCAL_DISTANCE : REMOTE_DISTANCE;
330         index = acpi_slit->locality_count * node_to_pxm(a);
331         return acpi_slit->entry[index + node_to_pxm(b)];
332 }
333 #endif /* CONFIG_NUMA_EMU */
334
335 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
336 int memory_add_physaddr_to_nid(u64 start)
337 {
338         int i, ret = 0;
339
340         for_each_node(i)
341                 if (nodes_add[i].start <= start && nodes_add[i].end > start)
342                         ret = i;
343
344         return ret;
345 }
346 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
347 #endif