Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 1 | /* |
Hans Rosenfeld | eec1d4f | 2010-10-29 17:14:30 +0200 | [diff] [blame] | 2 | * AMD NUMA support. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Discover the memory map and associated nodes. |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 4 | * |
Hans Rosenfeld | eec1d4f | 2010-10-29 17:14:30 +0200 | [diff] [blame] | 5 | * This version reads it directly from the AMD 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> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 14 | #include <linux/memblock.h> |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 15 | #include <linux/bootmem.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <linux/pci_ids.h> |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 19 | #include <linux/acpi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/types.h> |
| 21 | #include <asm/mmzone.h> |
| 22 | #include <asm/proto.h> |
| 23 | #include <asm/e820.h> |
| 24 | #include <asm/pci-direct.h> |
| 25 | #include <asm/numa.h> |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 26 | #include <asm/mpspec.h> |
| 27 | #include <asm/apic.h> |
Andreas Herrmann | 23ac4ae | 2010-09-17 18:03:43 +0200 | [diff] [blame] | 28 | #include <asm/amd_nb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
David Rientjes | f51bf30 | 2010-12-22 17:23:51 -0800 | [diff] [blame] | 30 | static unsigned char __initdata nodeids[8]; |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static __init int find_northbridge(void) |
| 33 | { |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 34 | int num; |
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 | for (num = 0; num < 32; num++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | u32 header; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 38 | |
| 39 | header = read_pci_config(0, num, 0, 0x00); |
Joachim Deguara | bb4a1d6 | 2008-01-30 13:34:12 +0100 | [diff] [blame] | 40 | if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)) && |
| 41 | header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) && |
| 42 | header != (PCI_VENDOR_ID_AMD | (0x1300<<16))) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 43 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 45 | header = read_pci_config(0, num, 1, 0x00); |
Joachim Deguara | bb4a1d6 | 2008-01-30 13:34:12 +0100 | [diff] [blame] | 46 | if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) && |
| 47 | header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) && |
| 48 | header != (PCI_VENDOR_ID_AMD | (0x1301<<16))) |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 49 | continue; |
| 50 | return num; |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 53 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 56 | static __init void early_get_boot_cpu_id(void) |
| 57 | { |
| 58 | /* |
Robert Richter | f6e9456c | 2010-07-21 19:03:58 +0200 | [diff] [blame] | 59 | * need to get the APIC ID of the BSP so can use that to |
Hans Rosenfeld | eec1d4f | 2010-10-29 17:14:30 +0200 | [diff] [blame] | 60 | * create apicid_to_node in amd_scan_nodes() |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 61 | */ |
Yinghai Lu | a4caa18 | 2008-06-19 12:15:01 -0700 | [diff] [blame] | 62 | #ifdef CONFIG_X86_MPPARSE |
Yinghai Lu | cbf9bd6 | 2008-02-19 03:21:06 -0800 | [diff] [blame] | 63 | /* |
| 64 | * get boot-time SMP configuration: |
| 65 | */ |
| 66 | if (smp_found_config) |
| 67 | early_get_smp_config(); |
Yinghai Lu | a4caa18 | 2008-06-19 12:15:01 -0700 | [diff] [blame] | 68 | #endif |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 71 | int __init amd_numa_init(void) |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 72 | { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 73 | u64 start = PFN_PHYS(0); |
| 74 | u64 end = PFN_PHYS(max_pfn); |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 75 | unsigned numnodes; |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 76 | u64 prevbase; |
Tejun Heo | 45fe6c7 | 2011-02-16 12:13:07 +0100 | [diff] [blame] | 77 | int i, j, nb; |
Thomas Gleixner | d34c089 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 78 | u32 nodeid, reg; |
Tejun Heo | 45fe6c7 | 2011-02-16 12:13:07 +0100 | [diff] [blame] | 79 | unsigned int bits, cores, apicid_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 81 | if (!early_pci_allowed()) |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 82 | return -EINVAL; |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 83 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 84 | nb = find_northbridge(); |
| 85 | if (nb < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return nb; |
| 87 | |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 88 | pr_info("Scanning NUMA topology in Northbridge %d\n", nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 90 | reg = read_pci_config(0, nb, 0, 0x60); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | numnodes = ((reg >> 4) & 0xF) + 1; |
Andi Kleen | 3bea9c9 | 2007-05-02 19:27:21 +0200 | [diff] [blame] | 92 | if (numnodes <= 1) |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 93 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 95 | pr_info("Number of physical nodes %d\n", numnodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | prevbase = 0; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 98 | for (i = 0; i < 8; i++) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 99 | u64 base, limit; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | base = read_pci_config(0, nb, 1, 0x40 + i*8); |
| 102 | limit = read_pci_config(0, nb, 1, 0x44 + i*8); |
| 103 | |
David Rientjes | f51bf30 | 2010-12-22 17:23:51 -0800 | [diff] [blame] | 104 | nodeids[i] = nodeid = limit & 7; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 105 | if ((base & 3) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (i < numnodes) |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 107 | pr_info("Skipping disabled node %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | continue; |
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 | if (nodeid >= numnodes) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 111 | pr_info("Ignoring excess node %d (%Lx:%Lx)\n", nodeid, |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 112 | base, limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 116 | if (!limit) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 117 | pr_info("Skipping node entry %d (base %Lx)\n", |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 118 | i, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | continue; |
| 120 | } |
| 121 | if ((base >> 8) & 3 || (limit >> 8) & 3) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 122 | pr_err("Node %d using interleaving mode %Lx/%Lx\n", |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 123 | nodeid, (base >> 8) & 3, (limit >> 8) & 3); |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 124 | return -EINVAL; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 125 | } |
Tejun Heo | 4697bdc | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 126 | if (node_isset(nodeid, numa_nodes_parsed)) { |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 127 | pr_info("Node %d already present, skipping\n", |
| 128 | nodeid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | continue; |
| 130 | } |
| 131 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 132 | limit >>= 16; |
| 133 | limit <<= 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | limit |= (1<<24)-1; |
Magnus Damm | ffd10a2 | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 135 | limit++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 137 | if (limit > end) |
| 138 | limit = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (limit <= base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 141 | |
| 142 | base >>= 16; |
| 143 | base <<= 24; |
| 144 | |
| 145 | if (base < start) |
| 146 | base = start; |
| 147 | if (limit > end) |
| 148 | limit = end; |
| 149 | if (limit == base) { |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 150 | pr_err("Empty node %d\n", nodeid); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 151 | continue; |
| 152 | } |
| 153 | if (limit < base) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 154 | pr_err("Node %d bogus settings %Lx-%Lx.\n", |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 155 | nodeid, base, limit); |
| 156 | continue; |
| 157 | } |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* Could sort here, but pun for now. Should not happen anyroads. */ |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 160 | if (prevbase > base) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 161 | pr_err("Node map not sorted %Lx,%Lx\n", |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 162 | prevbase, base); |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 163 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 165 | |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 166 | pr_info("Node %d MemBase %016Lx Limit %016Lx\n", |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 167 | nodeid, base, limit); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | prevbase = base; |
Tejun Heo | 9155623 | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 170 | numa_add_memblk(nodeid, base, limit); |
Tejun Heo | 92d4a43 | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 171 | node_set(nodeid, numa_nodes_parsed); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Tejun Heo | 4697bdc | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 174 | if (!nodes_weight(numa_nodes_parsed)) |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 175 | return -ENOENT; |
Tejun Heo | 45fe6c7 | 2011-02-16 12:13:07 +0100 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * We seem to have valid NUMA configuration. Map apicids to nodes |
| 179 | * using the coreid bits from early_identify_cpu. |
| 180 | */ |
| 181 | bits = boot_cpu_data.x86_coreid_bits; |
| 182 | cores = 1 << bits; |
| 183 | apicid_base = 0; |
| 184 | |
| 185 | /* get the APIC ID of the BSP early for systems with apicid lifting */ |
| 186 | early_get_boot_cpu_id(); |
| 187 | if (boot_cpu_physical_apicid > 0) { |
| 188 | pr_info("BSP APIC ID: %02x\n", boot_cpu_physical_apicid); |
| 189 | apicid_base = boot_cpu_physical_apicid; |
| 190 | } |
| 191 | |
Tejun Heo | 92d4a43 | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 192 | for_each_node_mask(i, numa_nodes_parsed) |
Tejun Heo | 45fe6c7 | 2011-02-16 12:13:07 +0100 | [diff] [blame] | 193 | for (j = apicid_base; j < cores + apicid_base; j++) |
| 194 | set_apicid_to_node((i << bits) + j, i); |
| 195 | |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 196 | return 0; |
| 197 | } |