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 | */ |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int __init k8_scan_nodes(unsigned long start, unsigned long end) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 76 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | unsigned long prevbase; |
Andi Kleen | abe059e | 2006-03-25 16:29:12 +0100 | [diff] [blame] | 78 | struct bootnode nodes[8]; |
Thomas Gleixner | d34c089 | 2008-05-12 15:43:35 +0200 | [diff] [blame^] | 79 | int i, nb; |
Andi Kleen | 3f098c2 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 80 | unsigned char nodeids[8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | int found = 0; |
Thomas Gleixner | d34c089 | 2008-05-12 15:43:35 +0200 | [diff] [blame^] | 82 | u32 nodeid, reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | unsigned numnodes; |
Yinghai Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 84 | unsigned cores; |
| 85 | unsigned bits; |
| 86 | int j; |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 87 | unsigned apicid_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 89 | if (!early_pci_allowed()) |
| 90 | return -1; |
| 91 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 92 | nb = find_northbridge(); |
| 93 | if (nb < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return nb; |
| 95 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 96 | printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 98 | reg = read_pci_config(0, nb, 0, 0x60); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | numnodes = ((reg >> 4) & 0xF) + 1; |
Andi Kleen | 3bea9c9 | 2007-05-02 19:27:21 +0200 | [diff] [blame] | 100 | if (numnodes <= 1) |
| 101 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | printk(KERN_INFO "Number of nodes %d\n", numnodes); |
| 104 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 105 | memset(&nodes, 0, sizeof(nodes)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | prevbase = 0; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 107 | for (i = 0; i < 8; i++) { |
| 108 | unsigned long base, limit; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | base = read_pci_config(0, nb, 1, 0x40 + i*8); |
| 111 | limit = read_pci_config(0, nb, 1, 0x44 + i*8); |
| 112 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 113 | nodeid = limit & 7; |
Andi Kleen | 3f098c2 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 114 | nodeids[i] = nodeid; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 115 | if ((base & 3) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (i < numnodes) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 117 | printk("Skipping disabled node %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | if (nodeid >= numnodes) { |
| 121 | printk("Ignoring excess node %d (%lx:%lx)\n", nodeid, |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 122 | base, limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 126 | if (!limit) { |
| 127 | printk(KERN_INFO "Skipping node entry %d (base %lx)\n", |
| 128 | i, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | continue; |
| 130 | } |
| 131 | if ((base >> 8) & 3 || (limit >> 8) & 3) { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 132 | printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n", |
| 133 | nodeid, (base>>8)&3, (limit>>8) & 3); |
| 134 | return -1; |
| 135 | } |
Suresh Siddha | e3f1cae | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 136 | if (node_isset(nodeid, node_possible_map)) { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 137 | printk(KERN_INFO "Node %d already present. Skipping\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | nodeid); |
| 139 | continue; |
| 140 | } |
| 141 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 142 | limit >>= 16; |
| 143 | limit <<= 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | limit |= (1<<24)-1; |
Magnus Damm | ffd10a2 | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 145 | limit++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | if (limit > end_pfn << PAGE_SHIFT) |
| 148 | limit = end_pfn << PAGE_SHIFT; |
| 149 | if (limit <= base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 151 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | /* Could sort here, but pun for now. Should not happen anyroads. */ |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 170 | if (prevbase > base) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | printk(KERN_ERR "Node map not sorted %lx,%lx\n", |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 172 | prevbase, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return -1; |
| 174 | } |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 175 | |
| 176 | printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n", |
| 177 | nodeid, base, limit); |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | found++; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 180 | |
| 181 | nodes[nodeid].start = base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | nodes[nodeid].end = limit; |
Mel Gorman | 5cb248a | 2006-09-27 01:49:52 -0700 | [diff] [blame] | 183 | e820_register_active_regions(nodeid, |
| 184 | nodes[nodeid].start >> PAGE_SHIFT, |
| 185 | nodes[nodeid].end >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | prevbase = base; |
| 188 | |
Suresh Siddha | e3f1cae | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 189 | node_set(nodeid, node_possible_map); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | if (!found) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 193 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Suresh Siddha | 6ec6e0d | 2008-03-25 10:14:35 -0700 | [diff] [blame] | 195 | memnode_shift = compute_hash_shift(nodes, 8, NULL); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 196 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Yinghai Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 202 | /* use the coreid bits from early_identify_cpu */ |
| 203 | bits = boot_cpu_data.x86_coreid_bits; |
| 204 | cores = (1<<bits); |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 205 | 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 Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | for (i = 0; i < 8; i++) { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 215 | if (nodes[i].start != nodes[i].end) { |
Andi Kleen | 3f098c2 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 216 | nodeid = nodeids[i]; |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 217 | for (j = apicid_base; j < cores + apicid_base; j++) |
Yinghai Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 218 | apicid_to_node[(nodeid << bits) + j] = i; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 219 | setup_node_bootmem(i, nodes[i].start, nodes[i].end); |
| 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | numa_init_array(); |
| 224 | return 0; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 225 | } |