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