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> |
| 16 | #include <asm/types.h> |
| 17 | #include <asm/mmzone.h> |
| 18 | #include <asm/proto.h> |
| 19 | #include <asm/e820.h> |
| 20 | #include <asm/pci-direct.h> |
| 21 | #include <asm/numa.h> |
| 22 | |
| 23 | static __init int find_northbridge(void) |
| 24 | { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 25 | int num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 27 | for (num = 0; num < 32; num++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | u32 header; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 29 | |
| 30 | header = read_pci_config(0, num, 0, 0x00); |
Joachim Deguara | bb4a1d6 | 2008-01-30 13:34:12 +0100 | [diff] [blame] | 31 | if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)) && |
| 32 | header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) && |
| 33 | header != (PCI_VENDOR_ID_AMD | (0x1300<<16))) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 34 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 36 | header = read_pci_config(0, num, 1, 0x00); |
Joachim Deguara | bb4a1d6 | 2008-01-30 13:34:12 +0100 | [diff] [blame] | 37 | if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) && |
| 38 | header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) && |
| 39 | header != (PCI_VENDOR_ID_AMD | (0x1301<<16))) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 40 | continue; |
| 41 | return num; |
| 42 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 44 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int __init k8_scan_nodes(unsigned long start, unsigned long end) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 48 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | unsigned long prevbase; |
Andi Kleen | abe059e | 2006-03-25 16:29:12 +0100 | [diff] [blame] | 50 | struct bootnode nodes[8]; |
Yinghai Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 51 | int nodeid, i, nb; |
Andi Kleen | 3f098c2 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 52 | unsigned char nodeids[8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | int found = 0; |
| 54 | u32 reg; |
| 55 | unsigned numnodes; |
Yinghai Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 56 | unsigned cores; |
| 57 | unsigned bits; |
| 58 | int j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 60 | if (!early_pci_allowed()) |
| 61 | return -1; |
| 62 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 63 | nb = find_northbridge(); |
| 64 | if (nb < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return nb; |
| 66 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 67 | printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 69 | reg = read_pci_config(0, nb, 0, 0x60); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | numnodes = ((reg >> 4) & 0xF) + 1; |
Andi Kleen | 3bea9c9 | 2007-05-02 19:27:21 +0200 | [diff] [blame] | 71 | if (numnodes <= 1) |
| 72 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | printk(KERN_INFO "Number of nodes %d\n", numnodes); |
| 75 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 76 | memset(&nodes, 0, sizeof(nodes)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | prevbase = 0; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 78 | for (i = 0; i < 8; i++) { |
| 79 | unsigned long base, limit; |
Andi Kleen | 3f098c2 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 80 | u32 nodeid; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | base = read_pci_config(0, nb, 1, 0x40 + i*8); |
| 83 | limit = read_pci_config(0, nb, 1, 0x44 + i*8); |
| 84 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 85 | nodeid = limit & 7; |
Andi Kleen | 3f098c2 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 86 | nodeids[i] = nodeid; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 87 | if ((base & 3) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (i < numnodes) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 89 | printk("Skipping disabled node %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 91 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (nodeid >= numnodes) { |
| 93 | printk("Ignoring excess node %d (%lx:%lx)\n", nodeid, |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 94 | base, limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 96 | } |
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 | if (!limit) { |
| 99 | printk(KERN_INFO "Skipping node entry %d (base %lx)\n", |
| 100 | i, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | continue; |
| 102 | } |
| 103 | if ((base >> 8) & 3 || (limit >> 8) & 3) { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 104 | printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n", |
| 105 | nodeid, (base>>8)&3, (limit>>8) & 3); |
| 106 | return -1; |
| 107 | } |
Suresh Siddha | e3f1cae | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 108 | if (node_isset(nodeid, node_possible_map)) { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 109 | printk(KERN_INFO "Node %d already present. Skipping\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | nodeid); |
| 111 | continue; |
| 112 | } |
| 113 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 114 | limit >>= 16; |
| 115 | limit <<= 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | limit |= (1<<24)-1; |
Magnus Damm | ffd10a2 | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 117 | limit++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | if (limit > end_pfn << PAGE_SHIFT) |
| 120 | limit = end_pfn << PAGE_SHIFT; |
| 121 | if (limit <= base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 123 | |
| 124 | base >>= 16; |
| 125 | base <<= 24; |
| 126 | |
| 127 | if (base < start) |
| 128 | base = start; |
| 129 | if (limit > end) |
| 130 | limit = end; |
| 131 | if (limit == base) { |
| 132 | printk(KERN_ERR "Empty node %d\n", nodeid); |
| 133 | continue; |
| 134 | } |
| 135 | if (limit < base) { |
| 136 | printk(KERN_ERR "Node %d bogus settings %lx-%lx.\n", |
| 137 | nodeid, base, limit); |
| 138 | continue; |
| 139 | } |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | /* Could sort here, but pun for now. Should not happen anyroads. */ |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 142 | if (prevbase > base) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | printk(KERN_ERR "Node map not sorted %lx,%lx\n", |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 144 | prevbase, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return -1; |
| 146 | } |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 147 | |
| 148 | printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n", |
| 149 | nodeid, base, limit); |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | found++; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 152 | |
| 153 | nodes[nodeid].start = base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | nodes[nodeid].end = limit; |
Mel Gorman | 5cb248a | 2006-09-27 01:49:52 -0700 | [diff] [blame] | 155 | e820_register_active_regions(nodeid, |
| 156 | nodes[nodeid].start >> PAGE_SHIFT, |
| 157 | nodes[nodeid].end >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | prevbase = base; |
| 160 | |
Suresh Siddha | e3f1cae | 2007-05-02 19:27:20 +0200 | [diff] [blame] | 161 | node_set(nodeid, node_possible_map); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | if (!found) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 165 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Suresh Siddha | 6ec6e0d | 2008-03-25 10:14:35 -0700 | [diff] [blame] | 167 | memnode_shift = compute_hash_shift(nodes, 8, NULL); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 168 | if (memnode_shift < 0) { |
| 169 | printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n"); |
| 170 | return -1; |
| 171 | } |
| 172 | printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Yinghai Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 174 | /* use the coreid bits from early_identify_cpu */ |
| 175 | bits = boot_cpu_data.x86_coreid_bits; |
| 176 | cores = (1<<bits); |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | for (i = 0; i < 8; i++) { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 179 | if (nodes[i].start != nodes[i].end) { |
Andi Kleen | 3f098c2 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 180 | nodeid = nodeids[i]; |
Yinghai Lu | 3f6e5a1 | 2008-01-30 13:30:39 +0100 | [diff] [blame] | 181 | for (j = 0; j < cores; j++) |
| 182 | apicid_to_node[(nodeid << bits) + j] = i; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 183 | setup_node_bootmem(i, nodes[i].start, nodes[i].end); |
| 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | numa_init_array(); |
| 188 | return 0; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 189 | } |