blob: 268f8255280f8d41b4a777573b91e96ad1186a23 [file] [log] [blame]
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * AMD K8 NUMA support.
3 * Discover the memory map and associated nodes.
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +01004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This version reads it directly from the K8 northbridge.
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +01006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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>
Yinghai Lucbf9bd62008-02-19 03:21:06 -080016#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
Yinghai Lucbf9bd62008-02-19 03:21:06 -080023#include <asm/mpspec.h>
24#include <asm/apic.h>
Thomas Gleixner0eafe232008-05-12 15:43:35 +020025#include <asm/k8.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static __init int find_northbridge(void)
28{
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010029 int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010031 for (num = 0; num < 32; num++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 u32 header;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010033
34 header = read_pci_config(0, num, 0, 0x00);
Joachim Deguarabb4a1d62008-01-30 13:34:12 +010035 if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)) &&
36 header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) &&
37 header != (PCI_VENDOR_ID_AMD | (0x1300<<16)))
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010038 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010040 header = read_pci_config(0, num, 1, 0x00);
Joachim Deguarabb4a1d62008-01-30 13:34:12 +010041 if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) &&
42 header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) &&
43 header != (PCI_VENDOR_ID_AMD | (0x1301<<16)))
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010044 continue;
45 return num;
46 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010048 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Yinghai Lucbf9bd62008-02-19 03:21:06 -080051static __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 */
Yinghai Lua4caa182008-06-19 12:15:01 -070060#ifdef CONFIG_X86_MPPARSE
Yinghai Lucbf9bd62008-02-19 03:21:06 -080061 early_find_smp_config();
Yinghai Lua4caa182008-06-19 12:15:01 -070062#endif
Yinghai Lucbf9bd62008-02-19 03:21:06 -080063#ifdef CONFIG_ACPI
64 /*
65 * Read APIC information from ACPI tables.
66 */
67 early_acpi_boot_init();
68#endif
Yinghai Lua4caa182008-06-19 12:15:01 -070069#ifdef CONFIG_X86_MPPARSE
Yinghai Lucbf9bd62008-02-19 03:21:06 -080070 /*
71 * get boot-time SMP configuration:
72 */
73 if (smp_found_config)
74 early_get_smp_config();
Yinghai Lua4caa182008-06-19 12:15:01 -070075#endif
Yinghai Lucbf9bd62008-02-19 03:21:06 -080076 early_init_lapic_mapping();
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079int __init k8_scan_nodes(unsigned long start, unsigned long end)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010080{
Thomas Gleixner55d4f222008-05-12 15:43:35 +020081 unsigned numnodes, cores, bits, apicid_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 unsigned long prevbase;
Andi Kleenabe059e2006-03-25 16:29:12 +010083 struct bootnode nodes[8];
Thomas Gleixner55d4f222008-05-12 15:43:35 +020084 int i, j, nb, found = 0;
Thomas Gleixnerd34c0892008-05-12 15:43:35 +020085 u32 nodeid, reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Andi Kleen0637a702006-09-26 10:52:41 +020087 if (!early_pci_allowed())
88 return -1;
89
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010090 nb = find_northbridge();
91 if (nb < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return nb;
93
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010094 printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010096 reg = read_pci_config(0, nb, 0, 0x60);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 numnodes = ((reg >> 4) & 0xF) + 1;
Andi Kleen3bea9c92007-05-02 19:27:21 +020098 if (numnodes <= 1)
99 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 printk(KERN_INFO "Number of nodes %d\n", numnodes);
102
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100103 memset(&nodes, 0, sizeof(nodes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 prevbase = 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100105 for (i = 0; i < 8; i++) {
106 unsigned long base, limit;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 base = read_pci_config(0, nb, 1, 0x40 + i*8);
109 limit = read_pci_config(0, nb, 1, 0x44 + i*8);
110
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100111 nodeid = limit & 7;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100112 if ((base & 3) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (i < numnodes)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100114 printk("Skipping disabled node %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (nodeid >= numnodes) {
118 printk("Ignoring excess node %d (%lx:%lx)\n", nodeid,
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100119 base, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100123 if (!limit) {
124 printk(KERN_INFO "Skipping node entry %d (base %lx)\n",
125 i, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 continue;
127 }
128 if ((base >> 8) & 3 || (limit >> 8) & 3) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100129 printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n",
130 nodeid, (base>>8)&3, (limit>>8) & 3);
131 return -1;
132 }
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200133 if (node_isset(nodeid, node_possible_map)) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100134 printk(KERN_INFO "Node %d already present. Skipping\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 nodeid);
136 continue;
137 }
138
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100139 limit >>= 16;
140 limit <<= 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 limit |= (1<<24)-1;
Magnus Dammffd10a22005-11-05 17:25:54 +0100142 limit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Yinghai Luc987d122008-06-24 22:14:09 -0700144 if (limit > max_pfn << PAGE_SHIFT)
145 limit = max_pfn << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (limit <= base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100148
149 base >>= 16;
150 base <<= 24;
151
152 if (base < start)
153 base = start;
154 if (limit > end)
155 limit = end;
156 if (limit == base) {
157 printk(KERN_ERR "Empty node %d\n", nodeid);
158 continue;
159 }
160 if (limit < base) {
161 printk(KERN_ERR "Node %d bogus settings %lx-%lx.\n",
162 nodeid, base, limit);
163 continue;
164 }
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* Could sort here, but pun for now. Should not happen anyroads. */
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100167 if (prevbase > base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 printk(KERN_ERR "Node map not sorted %lx,%lx\n",
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100169 prevbase, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -1;
171 }
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100172
173 printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n",
174 nodeid, base, limit);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 found++;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100177
178 nodes[nodeid].start = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 nodes[nodeid].end = limit;
180
181 prevbase = base;
182
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200183 node_set(nodeid, node_possible_map);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 if (!found)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100187 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700189 memnode_shift = compute_hash_shift(nodes, 8, NULL);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100190 if (memnode_shift < 0) {
191 printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n");
192 return -1;
193 }
194 printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100196 /* use the coreid bits from early_identify_cpu */
197 bits = boot_cpu_data.x86_coreid_bits;
198 cores = (1<<bits);
Yinghai Lucbf9bd62008-02-19 03:21:06 -0800199 apicid_base = 0;
200 /* need to get boot_cpu_id early for system with apicid lifting */
201 early_get_boot_cpu_id();
202 if (boot_cpu_physical_apicid > 0) {
203 printk(KERN_INFO "BSP APIC ID: %02x\n",
204 boot_cpu_physical_apicid);
205 apicid_base = boot_cpu_physical_apicid;
206 }
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 for (i = 0; i < 8; i++) {
Yinghai Lu40bcc692009-01-05 18:39:01 -0800209 if (nodes[i].start == nodes[i].end)
210 continue;
211
212 e820_register_active_regions(i,
213 nodes[i].start >> PAGE_SHIFT,
214 nodes[i].end >> PAGE_SHIFT);
215 for (j = apicid_base; j < cores + apicid_base; j++)
216 apicid_to_node[(i << bits) + j] = i;
217 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
220 numa_init_array();
221 return 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100222}