blob: 36c6c4a91353e3309f7f9a1f9ba69e7995489161 [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 */
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070075int __init k8_scan_nodes(unsigned long start, unsigned long end)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010076{
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned long prevbase;
Andi Kleenabe059e2006-03-25 16:29:12 +010078 struct bootnode nodes[8];
Thomas Gleixnerd34c0892008-05-12 15:43:35 +020079 int i, nb;
Andi Kleen3f098c22005-09-12 18:49:24 +020080 unsigned char nodeids[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int found = 0;
Thomas Gleixnerd34c0892008-05-12 15:43:35 +020082 u32 nodeid, reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned numnodes;
Yinghai Lu3f6e5a12008-01-30 13:30:39 +010084 unsigned cores;
85 unsigned bits;
86 int j;
Yinghai Lucbf9bd62008-02-19 03:21:06 -080087 unsigned apicid_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Andi Kleen0637a702006-09-26 10:52:41 +020089 if (!early_pci_allowed())
90 return -1;
91
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010092 nb = find_northbridge();
93 if (nb < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return nb;
95
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010096 printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010098 reg = read_pci_config(0, nb, 0, 0x60);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 numnodes = ((reg >> 4) & 0xF) + 1;
Andi Kleen3bea9c92007-05-02 19:27:21 +0200100 if (numnodes <= 1)
101 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 printk(KERN_INFO "Number of nodes %d\n", numnodes);
104
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100105 memset(&nodes, 0, sizeof(nodes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 prevbase = 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100107 for (i = 0; i < 8; i++) {
108 unsigned long base, limit;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 base = read_pci_config(0, nb, 1, 0x40 + i*8);
111 limit = read_pci_config(0, nb, 1, 0x44 + i*8);
112
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100113 nodeid = limit & 7;
Andi Kleen3f098c22005-09-12 18:49:24 +0200114 nodeids[i] = nodeid;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100115 if ((base & 3) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (i < numnodes)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100117 printk("Skipping disabled node %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (nodeid >= numnodes) {
121 printk("Ignoring excess node %d (%lx:%lx)\n", nodeid,
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100122 base, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100126 if (!limit) {
127 printk(KERN_INFO "Skipping node entry %d (base %lx)\n",
128 i, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 continue;
130 }
131 if ((base >> 8) & 3 || (limit >> 8) & 3) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100132 printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n",
133 nodeid, (base>>8)&3, (limit>>8) & 3);
134 return -1;
135 }
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200136 if (node_isset(nodeid, node_possible_map)) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100137 printk(KERN_INFO "Node %d already present. Skipping\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 nodeid);
139 continue;
140 }
141
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100142 limit >>= 16;
143 limit <<= 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 limit |= (1<<24)-1;
Magnus Dammffd10a22005-11-05 17:25:54 +0100145 limit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 if (limit > end_pfn << PAGE_SHIFT)
148 limit = end_pfn << PAGE_SHIFT;
149 if (limit <= base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100151
152 base >>= 16;
153 base <<= 24;
154
155 if (base < start)
156 base = start;
157 if (limit > end)
158 limit = end;
159 if (limit == base) {
160 printk(KERN_ERR "Empty node %d\n", nodeid);
161 continue;
162 }
163 if (limit < base) {
164 printk(KERN_ERR "Node %d bogus settings %lx-%lx.\n",
165 nodeid, base, limit);
166 continue;
167 }
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* Could sort here, but pun for now. Should not happen anyroads. */
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100170 if (prevbase > base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 printk(KERN_ERR "Node map not sorted %lx,%lx\n",
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100172 prevbase, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -1;
174 }
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100175
176 printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n",
177 nodeid, base, limit);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 found++;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100180
181 nodes[nodeid].start = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 nodes[nodeid].end = limit;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700183 e820_register_active_regions(nodeid,
184 nodes[nodeid].start >> PAGE_SHIFT,
185 nodes[nodeid].end >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 prevbase = base;
188
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200189 node_set(nodeid, node_possible_map);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (!found)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100193 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700195 memnode_shift = compute_hash_shift(nodes, 8, NULL);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100196 if (memnode_shift < 0) {
197 printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n");
198 return -1;
199 }
200 printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100202 /* use the coreid bits from early_identify_cpu */
203 bits = boot_cpu_data.x86_coreid_bits;
204 cores = (1<<bits);
Yinghai Lucbf9bd62008-02-19 03:21:06 -0800205 apicid_base = 0;
206 /* need to get boot_cpu_id early for system with apicid lifting */
207 early_get_boot_cpu_id();
208 if (boot_cpu_physical_apicid > 0) {
209 printk(KERN_INFO "BSP APIC ID: %02x\n",
210 boot_cpu_physical_apicid);
211 apicid_base = boot_cpu_physical_apicid;
212 }
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 for (i = 0; i < 8; i++) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100215 if (nodes[i].start != nodes[i].end) {
Andi Kleen3f098c22005-09-12 18:49:24 +0200216 nodeid = nodeids[i];
Yinghai Lucbf9bd62008-02-19 03:21:06 -0800217 for (j = apicid_base; j < cores + apicid_base; j++)
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100218 apicid_to_node[(nodeid << bits) + j] = i;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100219 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
223 numa_init_array();
224 return 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100225}