Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * AMD K8 NUMA support. |
| 3 | * Discover the memory map and associated nodes. |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * This version reads it directly from the K8 northbridge. |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 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 Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 16 | #include <linux/acpi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #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 Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 23 | #include <asm/mpspec.h> |
| 24 | #include <asm/apic.h> |
Thomas Gleixner | 0eafe23 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 25 | #include <asm/k8.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | static __init int find_northbridge(void) |
| 28 | { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 29 | int num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 31 | for (num = 0; num < 32; num++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | u32 header; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 33 | |
| 34 | header = read_pci_config(0, num, 0, 0x00); |
Joachim Deguara | bb4a1d6 | 2008-01-30 13:34:12 +0100 | [diff] [blame] | 35 | 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. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 38 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 40 | header = read_pci_config(0, num, 1, 0x00); |
Joachim Deguara | bb4a1d6 | 2008-01-30 13:34:12 +0100 | [diff] [blame] | 41 | 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. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 44 | continue; |
| 45 | return num; |
| 46 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 48 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 51 | static __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 Lu | a4caa18 | 2008-06-19 12:15:01 -0700 | [diff] [blame] | 60 | #ifdef CONFIG_X86_MPPARSE |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 61 | early_find_smp_config(); |
Yinghai Lu | a4caa18 | 2008-06-19 12:15:01 -0700 | [diff] [blame] | 62 | #endif |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 63 | #ifdef CONFIG_ACPI |
| 64 | /* |
| 65 | * Read APIC information from ACPI tables. |
| 66 | */ |
| 67 | early_acpi_boot_init(); |
| 68 | #endif |
Yinghai Lu | a4caa18 | 2008-06-19 12:15:01 -0700 | [diff] [blame] | 69 | #ifdef CONFIG_X86_MPPARSE |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 70 | /* |
| 71 | * get boot-time SMP configuration: |
| 72 | */ |
| 73 | if (smp_found_config) |
| 74 | early_get_smp_config(); |
Yinghai Lu | a4caa18 | 2008-06-19 12:15:01 -0700 | [diff] [blame] | 75 | #endif |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 76 | early_init_lapic_mapping(); |
| 77 | } |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | int __init k8_scan_nodes(unsigned long start, unsigned long end) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 80 | { |
Thomas Gleixner | 55d4f22 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 81 | unsigned numnodes, cores, bits, apicid_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | unsigned long prevbase; |
Andi Kleen | abe059e | 2006-03-25 16:29:12 +0100 | [diff] [blame] | 83 | struct bootnode nodes[8]; |
Thomas Gleixner | 55d4f22 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 84 | int i, j, nb, found = 0; |
Thomas Gleixner | d34c089 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 85 | u32 nodeid, reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 87 | if (!early_pci_allowed()) |
| 88 | return -1; |
| 89 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 90 | nb = find_northbridge(); |
| 91 | if (nb < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return nb; |
| 93 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 94 | printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 96 | reg = read_pci_config(0, nb, 0, 0x60); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | numnodes = ((reg >> 4) & 0xF) + 1; |
Andi Kleen | 3bea9c9 | 2007-05-02 19:27:21 +0200 | [diff] [blame] | 98 | if (numnodes <= 1) |
| 99 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | printk(KERN_INFO "Number of nodes %d\n", numnodes); |
| 102 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 103 | memset(&nodes, 0, sizeof(nodes)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | prevbase = 0; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 105 | for (i = 0; i < 8; i++) { |
| 106 | unsigned long base, limit; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | base = read_pci_config(0, nb, 1, 0x40 + i*8); |
| 109 | limit = read_pci_config(0, nb, 1, 0x44 + i*8); |
| 110 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 111 | nodeid = limit & 7; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 112 | if ((base & 3) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | if (i < numnodes) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 114 | printk("Skipping disabled node %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | if (nodeid >= numnodes) { |
| 118 | printk("Ignoring excess node %d (%lx:%lx)\n", nodeid, |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 119 | base, limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 123 | if (!limit) { |
| 124 | printk(KERN_INFO "Skipping node entry %d (base %lx)\n", |
| 125 | i, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | continue; |
| 127 | } |
| 128 | if ((base >> 8) & 3 || (limit >> 8) & 3) { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 129 | printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n", |
| 130 | nodeid, (base>>8)&3, (limit>>8) & 3); |
| 131 | return -1; |
| 132 | } |
Suresh Siddha | e3f1cae | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 133 | if (node_isset(nodeid, node_possible_map)) { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 134 | printk(KERN_INFO "Node %d already present. Skipping\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | nodeid); |
| 136 | continue; |
| 137 | } |
| 138 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 139 | limit >>= 16; |
| 140 | limit <<= 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | limit |= (1<<24)-1; |
Magnus Damm | ffd10a2 | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 142 | limit++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 144 | if (limit > max_pfn << PAGE_SHIFT) |
| 145 | limit = max_pfn << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (limit <= base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 148 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* Could sort here, but pun for now. Should not happen anyroads. */ |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 167 | if (prevbase > base) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | printk(KERN_ERR "Node map not sorted %lx,%lx\n", |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 169 | prevbase, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return -1; |
| 171 | } |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 172 | |
| 173 | printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n", |
| 174 | nodeid, base, limit); |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | found++; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 177 | |
| 178 | nodes[nodeid].start = base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | nodes[nodeid].end = limit; |
| 180 | |
| 181 | prevbase = base; |
| 182 | |
Suresh Siddha | e3f1cae | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 183 | node_set(nodeid, node_possible_map); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | if (!found) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 187 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Suresh Siddha | 6ec6e0d | 2008-03-25 10:14:35 -0700 | [diff] [blame] | 189 | memnode_shift = compute_hash_shift(nodes, 8, NULL); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 190 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Yinghai Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 196 | /* use the coreid bits from early_identify_cpu */ |
| 197 | bits = boot_cpu_data.x86_coreid_bits; |
| 198 | cores = (1<<bits); |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 199 | 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 Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | for (i = 0; i < 8; i++) { |
Yinghai Lu | 40bcc69 | 2009-01-05 18:39:01 -0800 | [diff] [blame] | 209 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | numa_init_array(); |
| 221 | return 0; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 222 | } |