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> |
Ingo Molnar | 66441bd | 2017-01-27 10:27:10 +0100 | [diff] [blame] | 22 | #include <asm/e820/api.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 55 | int __init amd_numa_init(void) |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 56 | { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 57 | u64 start = PFN_PHYS(0); |
| 58 | u64 end = PFN_PHYS(max_pfn); |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 59 | unsigned numnodes; |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 60 | u64 prevbase; |
Tejun Heo | 45fe6c7 | 2011-02-16 12:13:07 +0100 | [diff] [blame] | 61 | int i, j, nb; |
Thomas Gleixner | d34c089 | 2008-05-12 15:43:35 +0200 | [diff] [blame] | 62 | u32 nodeid, reg; |
Tejun Heo | 45fe6c7 | 2011-02-16 12:13:07 +0100 | [diff] [blame] | 63 | unsigned int bits, cores, apicid_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 65 | if (!early_pci_allowed()) |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 66 | return -EINVAL; |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 67 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 68 | nb = find_northbridge(); |
| 69 | if (nb < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return nb; |
| 71 | |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 72 | pr_info("Scanning NUMA topology in Northbridge %d\n", nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 74 | reg = read_pci_config(0, nb, 0, 0x60); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | numnodes = ((reg >> 4) & 0xF) + 1; |
Andi Kleen | 3bea9c9 | 2007-05-02 19:27:21 +0200 | [diff] [blame] | 76 | if (numnodes <= 1) |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 77 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 79 | pr_info("Number of physical nodes %d\n", numnodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | prevbase = 0; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 82 | for (i = 0; i < 8; i++) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 83 | u64 base, limit; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | base = read_pci_config(0, nb, 1, 0x40 + i*8); |
| 86 | limit = read_pci_config(0, nb, 1, 0x44 + i*8); |
| 87 | |
David Rientjes | f51bf30 | 2010-12-22 17:23:51 -0800 | [diff] [blame] | 88 | nodeids[i] = nodeid = limit & 7; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 89 | if ((base & 3) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (i < numnodes) |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 91 | pr_info("Skipping disabled node %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 93 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (nodeid >= numnodes) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 95 | pr_info("Ignoring excess node %d (%Lx:%Lx)\n", nodeid, |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 96 | base, limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | continue; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 100 | if (!limit) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 101 | pr_info("Skipping node entry %d (base %Lx)\n", |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 102 | i, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | continue; |
| 104 | } |
| 105 | if ((base >> 8) & 3 || (limit >> 8) & 3) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 106 | pr_err("Node %d using interleaving mode %Lx/%Lx\n", |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 107 | nodeid, (base >> 8) & 3, (limit >> 8) & 3); |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 108 | return -EINVAL; |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 109 | } |
Tejun Heo | 4697bdc | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 110 | if (node_isset(nodeid, numa_nodes_parsed)) { |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 111 | pr_info("Node %d already present, skipping\n", |
| 112 | nodeid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | continue; |
| 114 | } |
| 115 | |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 116 | limit >>= 16; |
Magnus Damm | ffd10a2 | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 117 | limit++; |
Martin Bundgaard | 7e9a2f0 | 2013-03-14 19:34:35 +0100 | [diff] [blame] | 118 | limit <<= 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 120 | if (limit > end) |
| 121 | limit = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (limit <= base) |
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 | |
| 125 | base >>= 16; |
| 126 | base <<= 24; |
| 127 | |
| 128 | if (base < start) |
| 129 | base = start; |
| 130 | if (limit > end) |
| 131 | limit = end; |
| 132 | if (limit == base) { |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 133 | pr_err("Empty node %d\n", nodeid); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 134 | continue; |
| 135 | } |
| 136 | if (limit < base) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 137 | pr_err("Node %d bogus settings %Lx-%Lx.\n", |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 138 | nodeid, base, limit); |
| 139 | continue; |
| 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* Could sort here, but pun for now. Should not happen anyroads. */ |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 143 | if (prevbase > base) { |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 144 | pr_err("Node map not sorted %Lx,%Lx\n", |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 145 | prevbase, base); |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 146 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 148 | |
Tejun Heo | 2706a0b | 2011-05-02 17:24:48 +0200 | [diff] [blame] | 149 | pr_info("Node %d MemBase %016Lx Limit %016Lx\n", |
David Rientjes | 1af5ba5 | 2009-09-25 15:19:47 -0700 | [diff] [blame] | 150 | nodeid, base, limit); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | prevbase = base; |
Tejun Heo | 9155623 | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 153 | numa_add_memblk(nodeid, base, limit); |
Tejun Heo | 92d4a43 | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 154 | node_set(nodeid, numa_nodes_parsed); |
Carlos R. Mafra | 3aa88cd | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Tejun Heo | 4697bdc | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 157 | if (!nodes_weight(numa_nodes_parsed)) |
Tejun Heo | 940fed2 | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 158 | return -ENOENT; |
Tejun Heo | 45fe6c7 | 2011-02-16 12:13:07 +0100 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * We seem to have valid NUMA configuration. Map apicids to nodes |
| 162 | * using the coreid bits from early_identify_cpu. |
| 163 | */ |
| 164 | bits = boot_cpu_data.x86_coreid_bits; |
| 165 | cores = 1 << bits; |
| 166 | apicid_base = 0; |
| 167 | |
Baoquan He | a91bf71 | 2016-08-12 14:57:12 +0800 | [diff] [blame] | 168 | /* |
| 169 | * get boot-time SMP configuration: |
| 170 | */ |
| 171 | early_get_smp_config(); |
| 172 | |
Tejun Heo | 45fe6c7 | 2011-02-16 12:13:07 +0100 | [diff] [blame] | 173 | if (boot_cpu_physical_apicid > 0) { |
| 174 | pr_info("BSP APIC ID: %02x\n", boot_cpu_physical_apicid); |
| 175 | apicid_base = boot_cpu_physical_apicid; |
| 176 | } |
| 177 | |
Tejun Heo | 92d4a43 | 2011-02-16 17:11:09 +0100 | [diff] [blame] | 178 | for_each_node_mask(i, numa_nodes_parsed) |
Tejun Heo | 45fe6c7 | 2011-02-16 12:13:07 +0100 | [diff] [blame] | 179 | for (j = apicid_base; j < cores + apicid_base; j++) |
| 180 | set_apicid_to_node((i << bits) + j, i); |
| 181 | |
David Rientjes | 8ee2deb | 2009-09-25 15:20:00 -0700 | [diff] [blame] | 182 | return 0; |
| 183 | } |