blob: 0ea66b532c35fd1c9330734eb61c32f770a82c1b [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{
Thomas Gleixner55d4f222008-05-12 15:43:35 +020077 unsigned numnodes, cores, bits, apicid_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 unsigned long prevbase;
Andi Kleenabe059e2006-03-25 16:29:12 +010079 struct bootnode nodes[8];
Andi Kleen3f098c22005-09-12 18:49:24 +020080 unsigned char nodeids[8];
Thomas Gleixner55d4f222008-05-12 15:43:35 +020081 int i, j, nb, found = 0;
Thomas Gleixnerd34c0892008-05-12 15:43:35 +020082 u32 nodeid, reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Andi Kleen0637a702006-09-26 10:52:41 +020084 if (!early_pci_allowed())
85 return -1;
86
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010087 nb = find_northbridge();
88 if (nb < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return nb;
90
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010091 printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +010093 reg = read_pci_config(0, nb, 0, 0x60);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 numnodes = ((reg >> 4) & 0xF) + 1;
Andi Kleen3bea9c92007-05-02 19:27:21 +020095 if (numnodes <= 1)
96 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 printk(KERN_INFO "Number of nodes %d\n", numnodes);
99
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100100 memset(&nodes, 0, sizeof(nodes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 prevbase = 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100102 for (i = 0; i < 8; i++) {
103 unsigned long base, limit;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 base = read_pci_config(0, nb, 1, 0x40 + i*8);
106 limit = read_pci_config(0, nb, 1, 0x44 + i*8);
107
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100108 nodeid = limit & 7;
Andi Kleen3f098c22005-09-12 18:49:24 +0200109 nodeids[i] = nodeid;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100110 if ((base & 3) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (i < numnodes)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100112 printk("Skipping disabled node %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (nodeid >= numnodes) {
116 printk("Ignoring excess node %d (%lx:%lx)\n", nodeid,
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100117 base, limit);
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
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100121 if (!limit) {
122 printk(KERN_INFO "Skipping node entry %d (base %lx)\n",
123 i, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 continue;
125 }
126 if ((base >> 8) & 3 || (limit >> 8) & 3) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100127 printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n",
128 nodeid, (base>>8)&3, (limit>>8) & 3);
129 return -1;
130 }
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200131 if (node_isset(nodeid, node_possible_map)) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100132 printk(KERN_INFO "Node %d already present. Skipping\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 nodeid);
134 continue;
135 }
136
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100137 limit >>= 16;
138 limit <<= 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 limit |= (1<<24)-1;
Magnus Dammffd10a22005-11-05 17:25:54 +0100140 limit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (limit > end_pfn << PAGE_SHIFT)
143 limit = end_pfn << PAGE_SHIFT;
144 if (limit <= base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 continue;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100146
147 base >>= 16;
148 base <<= 24;
149
150 if (base < start)
151 base = start;
152 if (limit > end)
153 limit = end;
154 if (limit == base) {
155 printk(KERN_ERR "Empty node %d\n", nodeid);
156 continue;
157 }
158 if (limit < base) {
159 printk(KERN_ERR "Node %d bogus settings %lx-%lx.\n",
160 nodeid, base, limit);
161 continue;
162 }
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* Could sort here, but pun for now. Should not happen anyroads. */
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100165 if (prevbase > base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 printk(KERN_ERR "Node map not sorted %lx,%lx\n",
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100167 prevbase, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -1;
169 }
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100170
171 printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n",
172 nodeid, base, limit);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 found++;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100175
176 nodes[nodeid].start = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 nodes[nodeid].end = limit;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700178 e820_register_active_regions(nodeid,
179 nodes[nodeid].start >> PAGE_SHIFT,
180 nodes[nodeid].end >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 prevbase = base;
183
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200184 node_set(nodeid, node_possible_map);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (!found)
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100188 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700190 memnode_shift = compute_hash_shift(nodes, 8, NULL);
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100191 if (memnode_shift < 0) {
192 printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n");
193 return -1;
194 }
195 printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100197 /* use the coreid bits from early_identify_cpu */
198 bits = boot_cpu_data.x86_coreid_bits;
199 cores = (1<<bits);
Yinghai Lucbf9bd62008-02-19 03:21:06 -0800200 apicid_base = 0;
201 /* need to get boot_cpu_id early for system with apicid lifting */
202 early_get_boot_cpu_id();
203 if (boot_cpu_physical_apicid > 0) {
204 printk(KERN_INFO "BSP APIC ID: %02x\n",
205 boot_cpu_physical_apicid);
206 apicid_base = boot_cpu_physical_apicid;
207 }
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 for (i = 0; i < 8; i++) {
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100210 if (nodes[i].start != nodes[i].end) {
Andi Kleen3f098c22005-09-12 18:49:24 +0200211 nodeid = nodeids[i];
Yinghai Lucbf9bd62008-02-19 03:21:06 -0800212 for (j = apicid_base; j < cores + apicid_base; j++)
Yinghai Lu3f6e5a12008-01-30 13:30:39 +0100213 apicid_to_node[(nodeid << bits) + j] = i;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100214 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 numa_init_array();
219 return 0;
Carlos R. Mafra3aa88cd2008-01-30 13:32:36 +0100220}