blob: 970ed579d4e4e86c6265828335b35e35024cfc38 [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
David Rientjes8ee2deb2009-09-25 15:20:00 -070027static struct bootnode __initdata nodes[8];
28static nodemask_t __initdata nodes_parsed = NODE_MASK_NONE;
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static __init int find_northbridge(void)
31{
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010032 int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010034 for (num = 0; num < 32; num++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 u32 header;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010036
37 header = read_pci_config(0, num, 0, 0x00);
Joachim Deguarabb4a1d62008-01-30 13:34:12 +010038 if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)) &&
39 header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) &&
40 header != (PCI_VENDOR_ID_AMD | (0x1300<<16)))
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010041 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010043 header = read_pci_config(0, num, 1, 0x00);
Joachim Deguarabb4a1d62008-01-30 13:34:12 +010044 if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) &&
45 header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) &&
46 header != (PCI_VENDOR_ID_AMD | (0x1301<<16)))
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010047 continue;
48 return num;
49 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010051 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Yinghai Lucbf9bd62008-02-19 03:21:06 -080054static __init void early_get_boot_cpu_id(void)
55{
56 /*
57 * need to get boot_cpu_id so can use that to create apicid_to_node
58 * in k8_scan_nodes()
59 */
Yinghai Lua4caa182008-06-19 12:15:01 -070060#ifdef CONFIG_X86_MPPARSE
Yinghai Lucbf9bd62008-02-19 03:21:06 -080061 /*
62 * get boot-time SMP configuration:
63 */
64 if (smp_found_config)
65 early_get_smp_config();
Yinghai Lua4caa182008-06-19 12:15:01 -070066#endif
Yinghai Lucbf9bd62008-02-19 03:21:06 -080067 early_init_lapic_mapping();
68}
69
David Rientjes8ee2deb2009-09-25 15:20:00 -070070int __init k8_get_nodes(struct bootnode *physnodes)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010071{
David Rientjes8ee2deb2009-09-25 15:20:00 -070072 int i;
73 int ret = 0;
74
75 for_each_node_mask(i, nodes_parsed) {
76 physnodes[ret].start = nodes[i].start;
77 physnodes[ret].end = nodes[i].end;
78 ret++;
79 }
80 return ret;
81}
82
83int __init k8_numa_init(unsigned long start_pfn, unsigned long end_pfn)
84{
85 unsigned long start = PFN_PHYS(start_pfn);
86 unsigned long end = PFN_PHYS(end_pfn);
87 unsigned numnodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned long prevbase;
David Rientjes8ee2deb2009-09-25 15:20:00 -070089 int i, nb, found = 0;
Thomas Gleixnerd34c0892008-05-12 15:43:35 +020090 u32 nodeid, reg;
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
David Rientjes1af5ba52009-09-25 15:19:47 -070099 pr_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
David Rientjes8ee2deb2009-09-25 15:20:00 -0700106 pr_info("Number of physical nodes %d\n", numnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 prevbase = 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100109 for (i = 0; i < 8; i++) {
110 unsigned long base, limit;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 base = read_pci_config(0, nb, 1, 0x40 + i*8);
113 limit = read_pci_config(0, nb, 1, 0x44 + i*8);
114
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100115 nodeid = limit & 7;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100116 if ((base & 3) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (i < numnodes)
David Rientjes1af5ba52009-09-25 15:19:47 -0700118 pr_info("Skipping disabled node %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (nodeid >= numnodes) {
David Rientjes1af5ba52009-09-25 15:19:47 -0700122 pr_info("Ignoring excess node %d (%lx:%lx)\n", nodeid,
123 base, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100127 if (!limit) {
David Rientjes1af5ba52009-09-25 15:19:47 -0700128 pr_info("Skipping node entry %d (base %lx)\n",
129 i, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 continue;
131 }
132 if ((base >> 8) & 3 || (limit >> 8) & 3) {
David Rientjes1af5ba52009-09-25 15:19:47 -0700133 pr_err("Node %d using interleaving mode %lx/%lx\n",
134 nodeid, (base >> 8) & 3, (limit >> 8) & 3);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100135 return -1;
136 }
David Rientjes8ee2deb2009-09-25 15:20:00 -0700137 if (node_isset(nodeid, nodes_parsed)) {
David Rientjes1af5ba52009-09-25 15:19:47 -0700138 pr_info("Node %d already present, skipping\n",
139 nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 continue;
141 }
142
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100143 limit >>= 16;
144 limit <<= 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 limit |= (1<<24)-1;
Magnus Dammffd10a22005-11-05 17:25:54 +0100146 limit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
David Rientjes8ee2deb2009-09-25 15:20:00 -0700148 if (limit > end)
149 limit = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (limit <= base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100152
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) {
David Rientjes1af5ba52009-09-25 15:19:47 -0700161 pr_err("Empty node %d\n", nodeid);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100162 continue;
163 }
164 if (limit < base) {
David Rientjes1af5ba52009-09-25 15:19:47 -0700165 pr_err("Node %d bogus settings %lx-%lx.\n",
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100166 nodeid, base, limit);
167 continue;
168 }
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* Could sort here, but pun for now. Should not happen anyroads. */
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100171 if (prevbase > base) {
David Rientjes1af5ba52009-09-25 15:19:47 -0700172 pr_err("Node map not sorted %lx,%lx\n",
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100173 prevbase, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -1;
175 }
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100176
David Rientjes1af5ba52009-09-25 15:19:47 -0700177 pr_info("Node %d MemBase %016lx Limit %016lx\n",
178 nodeid, base, limit);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 found++;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100181
182 nodes[nodeid].start = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 nodes[nodeid].end = limit;
184
185 prevbase = base;
186
David Rientjes8ee2deb2009-09-25 15:20:00 -0700187 node_set(nodeid, nodes_parsed);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 if (!found)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100191 return -1;
David Rientjes8ee2deb2009-09-25 15:20:00 -0700192 return 0;
193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
David Rientjes8ee2deb2009-09-25 15:20:00 -0700195int __init k8_scan_nodes(void)
196{
197 unsigned int bits;
198 unsigned int cores;
199 unsigned int apicid_base;
200 int i;
201
202 BUG_ON(nodes_empty(nodes_parsed));
203 node_possible_map = nodes_parsed;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700204 memnode_shift = compute_hash_shift(nodes, 8, NULL);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100205 if (memnode_shift < 0) {
David Rientjes1af5ba52009-09-25 15:19:47 -0700206 pr_err("No NUMA node hash function found. Contact maintainer\n");
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100207 return -1;
208 }
David Rientjes1af5ba52009-09-25 15:19:47 -0700209 pr_info("Using node hash shift of %d\n", memnode_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100211 /* use the coreid bits from early_identify_cpu */
212 bits = boot_cpu_data.x86_coreid_bits;
213 cores = (1<<bits);
Yinghai Lucbf9bd62008-02-19 03:21:06 -0800214 apicid_base = 0;
215 /* need to get boot_cpu_id early for system with apicid lifting */
216 early_get_boot_cpu_id();
217 if (boot_cpu_physical_apicid > 0) {
David Rientjes1af5ba52009-09-25 15:19:47 -0700218 pr_info("BSP APIC ID: %02x\n", boot_cpu_physical_apicid);
Yinghai Lucbf9bd62008-02-19 03:21:06 -0800219 apicid_base = boot_cpu_physical_apicid;
220 }
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100221
David Rientjes8ee2deb2009-09-25 15:20:00 -0700222 for_each_node_mask(i, node_possible_map) {
223 int j;
Yinghai Lu40bcc692009-01-05 18:39:01 -0800224
225 e820_register_active_regions(i,
226 nodes[i].start >> PAGE_SHIFT,
227 nodes[i].end >> PAGE_SHIFT);
228 for (j = apicid_base; j < cores + apicid_base; j++)
229 apicid_to_node[(i << bits) + j] = i;
230 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
233 numa_init_array();
234 return 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100235}