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