This commit was generated by cvs2svn to compensate for changes in r517,
[linux-2.6.git] / arch / x86_64 / mm / k8topology.c
1 /* 
2  * AMD K8 NUMA support.
3  * Discover the memory map and associated nodes.
4  * 
5  * Doesn't use the ACPI SRAT table because it has a questionable license.
6  * Instead the northbridge registers are read directly. 
7  * XXX in 2.5 we could use the generic SRAT code
8  * 
9  * Copyright 2002,2003 Andi Kleen, SuSE Labs.
10  */
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/string.h>
14 #include <linux/module.h>
15 #include <linux/nodemask.h>
16 #include <asm/io.h>
17 #include <linux/pci_ids.h>
18 #include <asm/types.h>
19 #include <asm/mmzone.h>
20 #include <asm/proto.h>
21 #include <asm/e820.h>
22 #include <asm/pci-direct.h>
23 #include <asm/numa.h>
24
25 static __init int find_northbridge(void)
26 {
27         int num; 
28
29         for (num = 0; num < 32; num++) { 
30                 u32 header;
31                 
32                 header = read_pci_config(0, num, 0, 0x00);  
33                 if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)))
34                         continue;       
35
36                 header = read_pci_config(0, num, 1, 0x00); 
37                 if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)))
38                         continue;       
39                 return num; 
40         } 
41
42         return -1;      
43 }
44
45 int __init k8_scan_nodes(unsigned long start, unsigned long end)
46
47         unsigned long prevbase;
48         struct node nodes[MAXNODE];
49         int nodeid, i, nb; 
50         int found = 0;
51         u32 reg;
52
53         nb = find_northbridge(); 
54         if (nb < 0) 
55                 return nb;
56
57         printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb); 
58
59         reg = read_pci_config(0, nb, 0, 0x60); 
60         numnodes =  ((reg >> 4) & 7) + 1; 
61
62         printk(KERN_INFO "Number of nodes %d (%x)\n", numnodes, reg);
63
64         memset(&nodes,0,sizeof(nodes)); 
65         prevbase = 0;
66         for (i = 0; i < 8; i++) { 
67                 unsigned long base,limit; 
68
69                 base = read_pci_config(0, nb, 1, 0x40 + i*8);
70                 limit = read_pci_config(0, nb, 1, 0x44 + i*8);
71
72                 nodeid = limit & 7; 
73                 if ((base & 3) == 0) { 
74                         if (i < numnodes) 
75                                 printk("Skipping disabled node %d\n", i); 
76                         continue;
77                 } 
78                 if (nodeid >= numnodes) { 
79                         printk("Ignoring excess node %d (%lx:%lx)\n", nodeid,
80                                base, limit); 
81                         continue;
82                 } 
83
84                 if (!limit) { 
85                         printk(KERN_INFO "Skipping node entry %d (base %lx)\n", i,
86                                base);
87                         continue;
88                 }
89                 if ((base >> 8) & 3 || (limit >> 8) & 3) {
90                         printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n", 
91                                nodeid, (base>>8)&3, (limit>>8) & 3); 
92                         return -1; 
93                 }       
94                 if (node_online(nodeid)) { 
95                         printk(KERN_INFO "Node %d already present. Skipping\n", 
96                                nodeid);
97                         continue;
98                 }
99
100                 limit >>= 16; 
101                 limit <<= 24; 
102                 limit |= (1<<24)-1;
103
104                 if (limit > end_pfn << PAGE_SHIFT)
105                         limit = end_pfn << PAGE_SHIFT;
106                 if (limit <= base)
107                         continue; 
108                         
109                 base >>= 16;
110                 base <<= 24; 
111
112                 if (base < start) 
113                         base = start; 
114                 if (limit > end) 
115                         limit = end; 
116                 if (limit == base) { 
117                         printk(KERN_ERR "Empty node %d\n", nodeid); 
118                         continue; 
119                 }
120                 if (limit < base) { 
121                         printk(KERN_ERR "Node %d bogus settings %lx-%lx.\n",
122                                nodeid, base, limit);                           
123                         continue;
124                 } 
125                 
126                 /* Could sort here, but pun for now. Should not happen anyroads. */
127                 if (prevbase > base) { 
128                         printk(KERN_ERR "Node map not sorted %lx,%lx\n",
129                                prevbase,base);
130                         return -1;
131                 }
132                         
133                 printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n", 
134                        nodeid, base, limit); 
135                 
136                 found++;
137                 
138                 nodes[nodeid].start = base; 
139                 nodes[nodeid].end = limit;
140
141                 prevbase = base;
142         } 
143
144         if (!found)
145                 return -1; 
146
147         memnode_shift = compute_hash_shift(nodes);
148         if (memnode_shift < 0) { 
149                 printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n"); 
150                 return -1; 
151         } 
152         printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift); 
153
154         for (i = 0; i < MAXNODE; i++) { 
155                 if (nodes[i].start != nodes[i].end) { 
156                         /* assume 1:1 NODE:CPU */
157                         cpu_to_node[i] = i; 
158                 setup_node_bootmem(i, nodes[i].start, nodes[i].end); 
159         } 
160         }
161
162         numa_init_array();
163         return 0;
164