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