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