Merge branches 'x86/numa-fixes', 'x86/apic', 'x86/apm', 'x86/bitops', 'x86/build...
[safe/jmp/linux-2.6] / arch / x86 / mm / k8topology_64.c
1 /*
2  * AMD K8 NUMA support.
3  * Discover the memory map and associated nodes.
4  *
5  * This version reads it directly from the K8 northbridge.
6  *
7  * Copyright 2002,2003 Andi Kleen, SuSE Labs.
8  */
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/string.h>
12 #include <linux/module.h>
13 #include <linux/nodemask.h>
14 #include <asm/io.h>
15 #include <linux/pci_ids.h>
16 #include <linux/acpi.h>
17 #include <asm/types.h>
18 #include <asm/mmzone.h>
19 #include <asm/proto.h>
20 #include <asm/e820.h>
21 #include <asm/pci-direct.h>
22 #include <asm/numa.h>
23 #include <asm/mpspec.h>
24 #include <asm/apic.h>
25 #include <asm/k8.h>
26
27 static __init int find_northbridge(void)
28 {
29         int num;
30
31         for (num = 0; num < 32; num++) {
32                 u32 header;
33
34                 header = read_pci_config(0, num, 0, 0x00);
35                 if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)) &&
36                         header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) &&
37                         header != (PCI_VENDOR_ID_AMD | (0x1300<<16)))
38                         continue;
39
40                 header = read_pci_config(0, num, 1, 0x00);
41                 if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) &&
42                         header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) &&
43                         header != (PCI_VENDOR_ID_AMD | (0x1301<<16)))
44                         continue;
45                 return num;
46         }
47
48         return -1;
49 }
50
51 static __init void early_get_boot_cpu_id(void)
52 {
53         /*
54          * need to get boot_cpu_id so can use that to create apicid_to_node
55          * in k8_scan_nodes()
56          */
57         /*
58          * Find possible boot-time SMP configuration:
59          */
60         early_find_smp_config();
61 #ifdef CONFIG_ACPI
62         /*
63          * Read APIC information from ACPI tables.
64          */
65         early_acpi_boot_init();
66 #endif
67         /*
68          * get boot-time SMP configuration:
69          */
70         if (smp_found_config)
71                 early_get_smp_config();
72         early_init_lapic_mapping();
73 }
74
75 int __init k8_scan_nodes(unsigned long start, unsigned long end)
76 {
77         unsigned numnodes, cores, bits, apicid_base;
78         unsigned long prevbase;
79         struct bootnode nodes[8];
80         unsigned char nodeids[8];
81         int i, j, nb, found = 0;
82         u32 nodeid, reg;
83
84         if (!early_pci_allowed())
85                 return -1;
86
87         nb = find_northbridge();
88         if (nb < 0)
89                 return nb;
90
91         printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb);
92
93         reg = read_pci_config(0, nb, 0, 0x60);
94         numnodes = ((reg >> 4) & 0xF) + 1;
95         if (numnodes <= 1)
96                 return -1;
97
98         printk(KERN_INFO "Number of nodes %d\n", numnodes);
99
100         memset(&nodes, 0, sizeof(nodes));
101         prevbase = 0;
102         for (i = 0; i < 8; i++) {
103                 unsigned long base, limit;
104
105                 base = read_pci_config(0, nb, 1, 0x40 + i*8);
106                 limit = read_pci_config(0, nb, 1, 0x44 + i*8);
107
108                 nodeid = limit & 7;
109                 nodeids[i] = nodeid;
110                 if ((base & 3) == 0) {
111                         if (i < numnodes)
112                                 printk("Skipping disabled node %d\n", i);
113                         continue;
114                 }
115                 if (nodeid >= numnodes) {
116                         printk("Ignoring excess node %d (%lx:%lx)\n", nodeid,
117                                base, limit);
118                         continue;
119                 }
120
121                 if (!limit) {
122                         printk(KERN_INFO "Skipping node entry %d (base %lx)\n",
123                                i, base);
124                         continue;
125                 }
126                 if ((base >> 8) & 3 || (limit >> 8) & 3) {
127                         printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n",
128                                nodeid, (base>>8)&3, (limit>>8) & 3);
129                         return -1;
130                 }
131                 if (node_isset(nodeid, node_possible_map)) {
132                         printk(KERN_INFO "Node %d already present. Skipping\n",
133                                nodeid);
134                         continue;
135                 }
136
137                 limit >>= 16;
138                 limit <<= 24;
139                 limit |= (1<<24)-1;
140                 limit++;
141
142                 if (limit > end_pfn << PAGE_SHIFT)
143                         limit = end_pfn << PAGE_SHIFT;
144                 if (limit <= base)
145                         continue;
146
147                 base >>= 16;
148                 base <<= 24;
149
150                 if (base < start)
151                         base = start;
152                 if (limit > end)
153                         limit = end;
154                 if (limit == base) {
155                         printk(KERN_ERR "Empty node %d\n", nodeid);
156                         continue;
157                 }
158                 if (limit < base) {
159                         printk(KERN_ERR "Node %d bogus settings %lx-%lx.\n",
160                                nodeid, base, limit);
161                         continue;
162                 }
163
164                 /* Could sort here, but pun for now. Should not happen anyroads. */
165                 if (prevbase > base) {
166                         printk(KERN_ERR "Node map not sorted %lx,%lx\n",
167                                prevbase, base);
168                         return -1;
169                 }
170
171                 printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n",
172                        nodeid, base, limit);
173
174                 found++;
175
176                 nodes[nodeid].start = base;
177                 nodes[nodeid].end = limit;
178                 e820_register_active_regions(nodeid,
179                                 nodes[nodeid].start >> PAGE_SHIFT,
180                                 nodes[nodeid].end >> PAGE_SHIFT);
181
182                 prevbase = base;
183
184                 node_set(nodeid, node_possible_map);
185         }
186
187         if (!found)
188                 return -1;
189
190         memnode_shift = compute_hash_shift(nodes, 8, NULL);
191         if (memnode_shift < 0) {
192                 printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n");
193                 return -1;
194         }
195         printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift);
196
197         /* use the coreid bits from early_identify_cpu */
198         bits = boot_cpu_data.x86_coreid_bits;
199         cores = (1<<bits);
200         apicid_base = 0;
201         /* need to get boot_cpu_id early for system with apicid lifting */
202         early_get_boot_cpu_id();
203         if (boot_cpu_physical_apicid > 0) {
204                 printk(KERN_INFO "BSP APIC ID: %02x\n",
205                                  boot_cpu_physical_apicid);
206                 apicid_base = boot_cpu_physical_apicid;
207         }
208
209         for (i = 0; i < 8; i++) {
210                 if (nodes[i].start != nodes[i].end) {
211                         nodeid = nodeids[i];
212                         for (j = apicid_base; j < cores + apicid_base; j++)
213                                 apicid_to_node[(nodeid << bits) + j] = i;
214                         setup_node_bootmem(i, nodes[i].start, nodes[i].end);
215                 }
216         }
217
218         numa_init_array();
219         return 0;
220 }