blob: f75aa2ae54a388e555fb65bba64c29c1158ef74d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static __init int find_northbridge(void)
27{
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010028 int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010030 for (num = 0; num < 32; num++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 u32 header;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010032
33 header = read_pci_config(0, num, 0, 0x00);
Joachim Deguarabb4a1d62008-01-30 13:34:12 +010034 if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)) &&
35 header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) &&
36 header != (PCI_VENDOR_ID_AMD | (0x1300<<16)))
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010037 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010039 header = read_pci_config(0, num, 1, 0x00);
Joachim Deguarabb4a1d62008-01-30 13:34:12 +010040 if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) &&
41 header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) &&
42 header != (PCI_VENDOR_ID_AMD | (0x1301<<16)))
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010043 continue;
44 return num;
45 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010047 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Yinghai Lucbf9bd62008-02-19 03:21:06 -080050static __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 */
Yinghai Lua4caa182008-06-19 12:15:01 -070059#ifdef CONFIG_X86_MPPARSE
Yinghai Lucbf9bd62008-02-19 03:21:06 -080060 early_find_smp_config();
Yinghai Lua4caa182008-06-19 12:15:01 -070061#endif
Yinghai Lucbf9bd62008-02-19 03:21:06 -080062#ifdef CONFIG_ACPI
63 /*
64 * Read APIC information from ACPI tables.
65 */
66 early_acpi_boot_init();
67#endif
Yinghai Lua4caa182008-06-19 12:15:01 -070068#ifdef CONFIG_X86_MPPARSE
Yinghai Lucbf9bd62008-02-19 03:21:06 -080069 /*
70 * get boot-time SMP configuration:
71 */
72 if (smp_found_config)
73 early_get_smp_config();
Yinghai Lua4caa182008-06-19 12:15:01 -070074#endif
Yinghai Lucbf9bd62008-02-19 03:21:06 -080075 early_init_lapic_mapping();
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078int __init k8_scan_nodes(unsigned long start, unsigned long end)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010079{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unsigned long prevbase;
Andi Kleenabe059e2006-03-25 16:29:12 +010081 struct bootnode nodes[8];
Yinghai Lu3f6e5a12008-01-30 13:30:39 +010082 int nodeid, i, nb;
Andi Kleen3f098c22005-09-12 18:49:24 +020083 unsigned char nodeids[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int found = 0;
85 u32 reg;
86 unsigned numnodes;
Yinghai Lu3f6e5a12008-01-30 13:30:39 +010087 unsigned cores;
88 unsigned bits;
89 int j;
Yinghai Lucbf9bd62008-02-19 03:21:06 -080090 unsigned apicid_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Andi Kleen0637a702006-09-26 10:52:41 +020092 if (!early_pci_allowed())
93 return -1;
94
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010095 nb = find_northbridge();
96 if (nb < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return nb;
98
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010099 printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100101 reg = read_pci_config(0, nb, 0, 0x60);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 numnodes = ((reg >> 4) & 0xF) + 1;
Andi Kleen3bea9c92007-05-02 19:27:21 +0200103 if (numnodes <= 1)
104 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 printk(KERN_INFO "Number of nodes %d\n", numnodes);
107
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100108 memset(&nodes, 0, sizeof(nodes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 prevbase = 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100110 for (i = 0; i < 8; i++) {
111 unsigned long base, limit;
Andi Kleen3f098c22005-09-12 18:49:24 +0200112 u32 nodeid;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 base = read_pci_config(0, nb, 1, 0x40 + i*8);
115 limit = read_pci_config(0, nb, 1, 0x44 + i*8);
116
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100117 nodeid = limit & 7;
Andi Kleen3f098c22005-09-12 18:49:24 +0200118 nodeids[i] = nodeid;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100119 if ((base & 3) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (i < numnodes)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100121 printk("Skipping disabled node %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (nodeid >= numnodes) {
125 printk("Ignoring excess node %d (%lx:%lx)\n", nodeid,
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100126 base, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100130 if (!limit) {
131 printk(KERN_INFO "Skipping node entry %d (base %lx)\n",
132 i, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 continue;
134 }
135 if ((base >> 8) & 3 || (limit >> 8) & 3) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100136 printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n",
137 nodeid, (base>>8)&3, (limit>>8) & 3);
138 return -1;
139 }
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200140 if (node_isset(nodeid, node_possible_map)) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100141 printk(KERN_INFO "Node %d already present. Skipping\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 nodeid);
143 continue;
144 }
145
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100146 limit >>= 16;
147 limit <<= 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 limit |= (1<<24)-1;
Magnus Dammffd10a22005-11-05 17:25:54 +0100149 limit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (limit > end_pfn << PAGE_SHIFT)
152 limit = end_pfn << PAGE_SHIFT;
153 if (limit <= base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100155
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* Could sort here, but pun for now. Should not happen anyroads. */
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100174 if (prevbase > base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 printk(KERN_ERR "Node map not sorted %lx,%lx\n",
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100176 prevbase, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return -1;
178 }
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100179
180 printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n",
181 nodeid, base, limit);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 found++;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100184
185 nodes[nodeid].start = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 nodes[nodeid].end = limit;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700187 e820_register_active_regions(nodeid,
188 nodes[nodeid].start >> PAGE_SHIFT,
189 nodes[nodeid].end >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 prevbase = base;
192
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200193 node_set(nodeid, node_possible_map);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (!found)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100197 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700199 memnode_shift = compute_hash_shift(nodes, 8, NULL);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100200 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100206 /* use the coreid bits from early_identify_cpu */
207 bits = boot_cpu_data.x86_coreid_bits;
208 cores = (1<<bits);
Yinghai Lucbf9bd62008-02-19 03:21:06 -0800209 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 }
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 for (i = 0; i < 8; i++) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100219 if (nodes[i].start != nodes[i].end) {
Andi Kleen3f098c22005-09-12 18:49:24 +0200220 nodeid = nodeids[i];
Yinghai Lucbf9bd62008-02-19 03:21:06 -0800221 for (j = apicid_base; j < cores + apicid_base; j++)
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100222 apicid_to_node[(nodeid << bits) + j] = i;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100223 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
227 numa_init_array();
228 return 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100229}