x86, numa: Fix cpu to node mapping for sparse node ids
[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 nodemask_t nodes_parsed __initdata;
32 static nodemask_t cpu_nodes_parsed __initdata;
33 static struct bootnode nodes[MAX_NUMNODES] __initdata;
34 static struct bootnode nodes_add[MAX_NUMNODES];
35
36 static int num_node_memblks __initdata;
37 static struct bootnode node_memblk_range[NR_NODE_MEMBLKS] __initdata;
38 static int memblk_nodeid[NR_NODE_MEMBLKS] __initdata;
39
40 static __init int setup_node(int pxm)
41 {
42         return acpi_map_pxm_to_node(pxm);
43 }
44
45 static __init int conflicting_memblks(unsigned long start, unsigned long end)
46 {
47         int i;
48         for (i = 0; i < num_node_memblks; i++) {
49                 struct bootnode *nd = &node_memblk_range[i];
50                 if (nd->start == nd->end)
51                         continue;
52                 if (nd->end > start && nd->start < end)
53                         return memblk_nodeid[i];
54                 if (nd->end == end && nd->start == start)
55                         return memblk_nodeid[i];
56         }
57         return -1;
58 }
59
60 static __init void cutoff_node(int i, unsigned long start, unsigned long end)
61 {
62         struct bootnode *nd = &nodes[i];
63
64         if (nd->start < start) {
65                 nd->start = start;
66                 if (nd->end < nd->start)
67                         nd->start = nd->end;
68         }
69         if (nd->end > 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         int i;
79         printk(KERN_ERR "SRAT: SRAT not used.\n");
80         acpi_numa = -1;
81         for (i = 0; i < MAX_LOCAL_APIC; i++)
82                 apicid_to_node[i] = NUMA_NO_NODE;
83         for (i = 0; i < MAX_NUMNODES; i++) {
84                 nodes[i].start = nodes[i].end = 0;
85                 nodes_add[i].start = nodes_add[i].end = 0;
86         }
87         remove_all_active_ranges();
88 }
89
90 static __init inline int srat_disabled(void)
91 {
92         return numa_off || acpi_numa < 0;
93 }
94
95 /* Callback for SLIT parsing */
96 void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
97 {
98         unsigned length;
99         unsigned long phys;
100
101         length = slit->header.length;
102         phys = memblock_find_in_range(0, max_pfn_mapped<<PAGE_SHIFT, length,
103                  PAGE_SIZE);
104
105         if (phys == MEMBLOCK_ERROR)
106                 panic(" Can not save slit!\n");
107
108         acpi_slit = __va(phys);
109         memcpy(acpi_slit, slit, length);
110         memblock_x86_reserve_range(phys, phys + length, "ACPI SLIT");
111 }
112
113 /* Callback for Proximity Domain -> x2APIC mapping */
114 void __init
115 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
116 {
117         int pxm, node;
118         int apic_id;
119
120         if (srat_disabled())
121                 return;
122         if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
123                 bad_srat();
124                 return;
125         }
126         if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
127                 return;
128         pxm = pa->proximity_domain;
129         node = setup_node(pxm);
130         if (node < 0) {
131                 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
132                 bad_srat();
133                 return;
134         }
135
136         apic_id = pa->apic_id;
137         apicid_to_node[apic_id] = node;
138         node_set(node, cpu_nodes_parsed);
139         acpi_numa = 1;
140         printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u\n",
141                pxm, apic_id, node);
142 }
143
144 /* Callback for Proximity Domain -> LAPIC mapping */
145 void __init
146 acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
147 {
148         int pxm, node;
149         int apic_id;
150
151         if (srat_disabled())
152                 return;
153         if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
154                 bad_srat();
155                 return;
156         }
157         if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
158                 return;
159         pxm = pa->proximity_domain_lo;
160         node = setup_node(pxm);
161         if (node < 0) {
162                 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
163                 bad_srat();
164                 return;
165         }
166
167         if (get_uv_system_type() >= UV_X2APIC)
168                 apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
169         else
170                 apic_id = pa->apic_id;
171         apicid_to_node[apic_id] = node;
172         node_set(node, cpu_nodes_parsed);
173         acpi_numa = 1;
174         printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u\n",
175                pxm, apic_id, node);
176 }
177
178 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
179 static inline int save_add_info(void) {return 1;}
180 #else
181 static inline int save_add_info(void) {return 0;}
182 #endif
183 /*
184  * Update nodes_add[]
185  * This code supports one contiguous hot add area per node
186  */
187 static void __init
188 update_nodes_add(int node, unsigned long start, unsigned long end)
189 {
190         unsigned long s_pfn = start >> PAGE_SHIFT;
191         unsigned long e_pfn = end >> PAGE_SHIFT;
192         int changed = 0;
193         struct bootnode *nd = &nodes_add[node];
194
195         /* I had some trouble with strange memory hotadd regions breaking
196            the boot. Be very strict here and reject anything unexpected.
197            If you want working memory hotadd write correct SRATs.
198
199            The node size check is a basic sanity check to guard against
200            mistakes */
201         if ((signed long)(end - start) < NODE_MIN_SIZE) {
202                 printk(KERN_ERR "SRAT: Hotplug area too small\n");
203                 return;
204         }
205
206         /* This check might be a bit too strict, but I'm keeping it for now. */
207         if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
208                 printk(KERN_ERR
209                         "SRAT: Hotplug area %lu -> %lu has existing memory\n",
210                         s_pfn, e_pfn);
211                 return;
212         }
213
214         /* Looks good */
215
216         if (nd->start == nd->end) {
217                 nd->start = start;
218                 nd->end = end;
219                 changed = 1;
220         } else {
221                 if (nd->start == end) {
222                         nd->start = start;
223                         changed = 1;
224                 }
225                 if (nd->end == start) {
226                         nd->end = end;
227                         changed = 1;
228                 }
229                 if (!changed)
230                         printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
231         }
232
233         if (changed) {
234                 node_set(node, cpu_nodes_parsed);
235                 printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n",
236                                  nd->start, nd->end);
237         }
238 }
239
240 /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
241 void __init
242 acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
243 {
244         struct bootnode *nd, oldnode;
245         unsigned long start, end;
246         int node, pxm;
247         int i;
248
249         if (srat_disabled())
250                 return;
251         if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
252                 bad_srat();
253                 return;
254         }
255         if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
256                 return;
257
258         if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
259                 return;
260         start = ma->base_address;
261         end = start + ma->length;
262         pxm = ma->proximity_domain;
263         node = setup_node(pxm);
264         if (node < 0) {
265                 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
266                 bad_srat();
267                 return;
268         }
269         i = conflicting_memblks(start, end);
270         if (i == node) {
271                 printk(KERN_WARNING
272                 "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
273                         pxm, start, end, nodes[i].start, nodes[i].end);
274         } else if (i >= 0) {
275                 printk(KERN_ERR
276                        "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
277                        pxm, start, end, node_to_pxm(i),
278                         nodes[i].start, nodes[i].end);
279                 bad_srat();
280                 return;
281         }
282         nd = &nodes[node];
283         oldnode = *nd;
284         if (!node_test_and_set(node, nodes_parsed)) {
285                 nd->start = start;
286                 nd->end = end;
287         } else {
288                 if (start < nd->start)
289                         nd->start = start;
290                 if (nd->end < end)
291                         nd->end = end;
292         }
293
294         printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
295                start, end);
296
297         if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) {
298                 update_nodes_add(node, start, end);
299                 /* restore nodes[node] */
300                 *nd = oldnode;
301                 if ((nd->start | nd->end) == 0)
302                         node_clear(node, nodes_parsed);
303         }
304
305         node_memblk_range[num_node_memblks].start = start;
306         node_memblk_range[num_node_memblks].end = end;
307         memblk_nodeid[num_node_memblks] = node;
308         num_node_memblks++;
309 }
310
311 /* Sanity check to catch more bad SRATs (they are amazingly common).
312    Make sure the PXMs cover all memory. */
313 static int __init nodes_cover_memory(const struct bootnode *nodes)
314 {
315         int i;
316         unsigned long pxmram, e820ram;
317
318         pxmram = 0;
319         for_each_node_mask(i, nodes_parsed) {
320                 unsigned long s = nodes[i].start >> PAGE_SHIFT;
321                 unsigned long e = nodes[i].end >> PAGE_SHIFT;
322                 pxmram += e - s;
323                 pxmram -= __absent_pages_in_range(i, s, e);
324                 if ((long)pxmram < 0)
325                         pxmram = 0;
326         }
327
328         e820ram = max_pfn - (memblock_x86_hole_size(0, max_pfn<<PAGE_SHIFT)>>PAGE_SHIFT);
329         /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
330         if ((long)(e820ram - pxmram) >= (1<<(20 - PAGE_SHIFT))) {
331                 printk(KERN_ERR
332         "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n",
333                         (pxmram << PAGE_SHIFT) >> 20,
334                         (e820ram << PAGE_SHIFT) >> 20);
335                 return 0;
336         }
337         return 1;
338 }
339
340 void __init acpi_numa_arch_fixup(void) {}
341
342 #ifdef CONFIG_NUMA_EMU
343 void __init acpi_get_nodes(struct bootnode *physnodes, unsigned long start,
344                                 unsigned long end)
345 {
346         int i;
347
348         for_each_node_mask(i, nodes_parsed) {
349                 cutoff_node(i, start, end);
350                 physnodes[i].start = nodes[i].start;
351                 physnodes[i].end = nodes[i].end;
352         }
353 }
354 #endif /* CONFIG_NUMA_EMU */
355
356 /* Use the information discovered above to actually set up the nodes. */
357 int __init acpi_scan_nodes(unsigned long start, unsigned long end)
358 {
359         int i;
360
361         if (acpi_numa <= 0)
362                 return -1;
363
364         /* First clean up the node list */
365         for (i = 0; i < MAX_NUMNODES; i++)
366                 cutoff_node(i, start, end);
367
368         /*
369          * Join together blocks on the same node, holes between
370          * which don't overlap with memory on other nodes.
371          */
372         for (i = 0; i < num_node_memblks; ++i) {
373                 int j, k;
374
375                 for (j = i + 1; j < num_node_memblks; ++j) {
376                         unsigned long start, end;
377
378                         if (memblk_nodeid[i] != memblk_nodeid[j])
379                                 continue;
380                         start = min(node_memblk_range[i].end,
381                                     node_memblk_range[j].end);
382                         end = max(node_memblk_range[i].start,
383                                   node_memblk_range[j].start);
384                         for (k = 0; k < num_node_memblks; ++k) {
385                                 if (memblk_nodeid[i] == memblk_nodeid[k])
386                                         continue;
387                                 if (start < node_memblk_range[k].end &&
388                                     end > node_memblk_range[k].start)
389                                         break;
390                         }
391                         if (k < num_node_memblks)
392                                 continue;
393                         start = min(node_memblk_range[i].start,
394                                     node_memblk_range[j].start);
395                         end = max(node_memblk_range[i].end,
396                                   node_memblk_range[j].end);
397                         printk(KERN_INFO "SRAT: Node %d "
398                                "[%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
399                                memblk_nodeid[i],
400                                node_memblk_range[i].start,
401                                node_memblk_range[i].end,
402                                node_memblk_range[j].start,
403                                node_memblk_range[j].end,
404                                start, end);
405                         node_memblk_range[i].start = start;
406                         node_memblk_range[i].end = end;
407                         k = --num_node_memblks - j;
408                         memmove(memblk_nodeid + j, memblk_nodeid + j+1,
409                                 k * sizeof(*memblk_nodeid));
410                         memmove(node_memblk_range + j, node_memblk_range + j+1,
411                                 k * sizeof(*node_memblk_range));
412                         --j;
413                 }
414         }
415
416         memnode_shift = compute_hash_shift(node_memblk_range, num_node_memblks,
417                                            memblk_nodeid);
418         if (memnode_shift < 0) {
419                 printk(KERN_ERR
420                      "SRAT: No NUMA node hash function found. Contact maintainer\n");
421                 bad_srat();
422                 return -1;
423         }
424
425         for (i = 0; i < num_node_memblks; i++)
426                 memblock_x86_register_active_regions(memblk_nodeid[i],
427                                 node_memblk_range[i].start >> PAGE_SHIFT,
428                                 node_memblk_range[i].end >> PAGE_SHIFT);
429
430         /* for out of order entries in SRAT */
431         sort_node_map();
432         if (!nodes_cover_memory(nodes)) {
433                 bad_srat();
434                 return -1;
435         }
436
437         /* Account for nodes with cpus and no memory */
438         nodes_or(node_possible_map, nodes_parsed, cpu_nodes_parsed);
439
440         /* Finally register nodes */
441         for_each_node_mask(i, node_possible_map)
442                 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
443         /* Try again in case setup_node_bootmem missed one due
444            to missing bootmem */
445         for_each_node_mask(i, node_possible_map)
446                 if (!node_online(i))
447                         setup_node_bootmem(i, nodes[i].start, nodes[i].end);
448
449         for (i = 0; i < nr_cpu_ids; i++) {
450                 int node = early_cpu_to_node(i);
451
452                 if (node == NUMA_NO_NODE)
453                         continue;
454                 if (!node_online(node))
455                         numa_clear_node(i);
456         }
457         numa_init_array();
458         return 0;
459 }
460
461 #ifdef CONFIG_NUMA_EMU
462 static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
463         [0 ... MAX_NUMNODES-1] = PXM_INVAL
464 };
465 static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
466         [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
467 };
468 static int __init find_node_by_addr(unsigned long addr)
469 {
470         int ret = NUMA_NO_NODE;
471         int i;
472
473         for_each_node_mask(i, nodes_parsed) {
474                 /*
475                  * Find the real node that this emulated node appears on.  For
476                  * the sake of simplicity, we only use a real node's starting
477                  * address to determine which emulated node it appears on.
478                  */
479                 if (addr >= nodes[i].start && addr < nodes[i].end) {
480                         ret = i;
481                         break;
482                 }
483         }
484         return ret;
485 }
486
487 /*
488  * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
489  * mappings that respect the real ACPI topology but reflect our emulated
490  * environment.  For each emulated node, we find which real node it appears on
491  * and create PXM to NID mappings for those fake nodes which mirror that
492  * locality.  SLIT will now represent the correct distances between emulated
493  * nodes as a result of the real topology.
494  */
495 void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
496 {
497         int i, j;
498
499         for (i = 0; i < num_nodes; i++) {
500                 int nid, pxm;
501
502                 nid = find_node_by_addr(fake_nodes[i].start);
503                 if (nid == NUMA_NO_NODE)
504                         continue;
505                 pxm = node_to_pxm(nid);
506                 if (pxm == PXM_INVAL)
507                         continue;
508                 fake_node_to_pxm_map[i] = pxm;
509                 /*
510                  * For each apicid_to_node mapping that exists for this real
511                  * node, it must now point to the fake node ID.
512                  */
513                 for (j = 0; j < MAX_LOCAL_APIC; j++)
514                         if (apicid_to_node[j] == nid &&
515                             fake_apicid_to_node[j] == NUMA_NO_NODE)
516                                 fake_apicid_to_node[j] = i;
517         }
518
519         /*
520          * If there are apicid-to-node mappings for physical nodes that do not
521          * have a corresponding emulated node, it should default to a guaranteed
522          * value.
523          */
524         for (i = 0; i < MAX_LOCAL_APIC; i++)
525                 if (apicid_to_node[i] != NUMA_NO_NODE &&
526                     fake_apicid_to_node[i] == NUMA_NO_NODE)
527                         fake_apicid_to_node[i] = 0;
528
529         for (i = 0; i < num_nodes; i++)
530                 __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
531         memcpy(apicid_to_node, fake_apicid_to_node, sizeof(apicid_to_node));
532
533         nodes_clear(nodes_parsed);
534         for (i = 0; i < num_nodes; i++)
535                 if (fake_nodes[i].start != fake_nodes[i].end)
536                         node_set(i, nodes_parsed);
537 }
538
539 static int null_slit_node_compare(int a, int b)
540 {
541         return node_to_pxm(a) == node_to_pxm(b);
542 }
543 #else
544 static int null_slit_node_compare(int a, int b)
545 {
546         return a == b;
547 }
548 #endif /* CONFIG_NUMA_EMU */
549
550 int __node_distance(int a, int b)
551 {
552         int index;
553
554         if (!acpi_slit)
555                 return null_slit_node_compare(a, b) ? LOCAL_DISTANCE :
556                                                       REMOTE_DISTANCE;
557         index = acpi_slit->locality_count * node_to_pxm(a);
558         return acpi_slit->entry[index + node_to_pxm(b)];
559 }
560
561 EXPORT_SYMBOL(__node_distance);
562
563 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
564 int memory_add_physaddr_to_nid(u64 start)
565 {
566         int i, ret = 0;
567
568         for_each_node(i)
569                 if (nodes_add[i].start <= start && nodes_add[i].end > start)
570                         ret = i;
571
572         return ret;
573 }
574 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
575 #endif