Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ACPI 3.0 based NUMA setup |
| 3 | * Copyright 2004 Andi Kleen, SuSE Labs. |
| 4 | * |
| 5 | * Reads the ACPI SRAT table to figure out what memory belongs to which CPUs. |
| 6 | * |
| 7 | * Called from acpi_numa_init while reading the SRAT and SLIT tables. |
| 8 | * Assumes all memory regions belonging to a single proximity domain |
| 9 | * are in one chunk. Holes between them will be included in the node. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/acpi.h> |
| 14 | #include <linux/mmzone.h> |
| 15 | #include <linux/bitmap.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/topology.h> |
| 18 | #include <asm/proto.h> |
| 19 | #include <asm/numa.h> |
Andi Kleen | 8a6fdd3 | 2006-01-11 22:44:39 +0100 | [diff] [blame] | 20 | #include <asm/e820.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | static struct acpi_table_slit *acpi_slit; |
| 23 | |
| 24 | static nodemask_t nodes_parsed __initdata; |
| 25 | static nodemask_t nodes_found __initdata; |
| 26 | static struct node nodes[MAX_NUMNODES] __initdata; |
Andi Kleen | e4e9407 | 2006-01-11 22:43:48 +0100 | [diff] [blame] | 27 | static u8 pxm2node[256] = { [0 ... 255] = 0xff }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Andi Kleen | 9391a3f | 2006-02-03 21:51:17 +0100 | [diff] [blame^] | 29 | /* Too small nodes confuse the VM badly. Usually they result |
| 30 | from BIOS bugs. */ |
| 31 | #define NODE_MIN_SIZE (4*1024*1024) |
| 32 | |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 33 | static int node_to_pxm(int n); |
| 34 | |
Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 35 | int pxm_to_node(int pxm) |
| 36 | { |
| 37 | if ((unsigned)pxm >= 256) |
Andi Kleen | e4e9407 | 2006-01-11 22:43:48 +0100 | [diff] [blame] | 38 | return -1; |
| 39 | /* Extend 0xff to (int)-1 */ |
| 40 | return (signed char)pxm2node[pxm]; |
Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | static __init int setup_node(int pxm) |
| 44 | { |
| 45 | unsigned node = pxm2node[pxm]; |
| 46 | if (node == 0xff) { |
| 47 | if (nodes_weight(nodes_found) >= MAX_NUMNODES) |
| 48 | return -1; |
| 49 | node = first_unset_node(nodes_found); |
| 50 | node_set(node, nodes_found); |
| 51 | pxm2node[pxm] = node; |
| 52 | } |
| 53 | return pxm2node[pxm]; |
| 54 | } |
| 55 | |
| 56 | static __init int conflicting_nodes(unsigned long start, unsigned long end) |
| 57 | { |
| 58 | int i; |
Andi Kleen | 4b6a455 | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 59 | for_each_node_mask(i, nodes_parsed) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct node *nd = &nodes[i]; |
| 61 | if (nd->start == nd->end) |
| 62 | continue; |
| 63 | if (nd->end > start && nd->start < end) |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 64 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if (nd->end == end && nd->start == start) |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 66 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | static __init void cutoff_node(int i, unsigned long start, unsigned long end) |
| 72 | { |
| 73 | struct node *nd = &nodes[i]; |
| 74 | if (nd->start < start) { |
| 75 | nd->start = start; |
| 76 | if (nd->end < nd->start) |
| 77 | nd->start = nd->end; |
| 78 | } |
| 79 | if (nd->end > end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | nd->end = end; |
| 81 | if (nd->start > nd->end) |
| 82 | nd->start = nd->end; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static __init void bad_srat(void) |
| 87 | { |
Andi Kleen | 2bce2b5 | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 88 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | printk(KERN_ERR "SRAT: SRAT not used.\n"); |
| 90 | acpi_numa = -1; |
Andi Kleen | 2bce2b5 | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 91 | for (i = 0; i < MAX_LOCAL_APIC; i++) |
| 92 | apicid_to_node[i] = NUMA_NO_NODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static __init inline int srat_disabled(void) |
| 96 | { |
| 97 | return numa_off || acpi_numa < 0; |
| 98 | } |
| 99 | |
Andi Kleen | 1584b89 | 2006-01-11 22:43:42 +0100 | [diff] [blame] | 100 | /* |
| 101 | * A lot of BIOS fill in 10 (= no distance) everywhere. This messes |
| 102 | * up the NUMA heuristics which wants the local node to have a smaller |
| 103 | * distance than the others. |
| 104 | * Do some quick checks here and only use the SLIT if it passes. |
| 105 | */ |
| 106 | static __init int slit_valid(struct acpi_table_slit *slit) |
| 107 | { |
| 108 | int i, j; |
| 109 | int d = slit->localities; |
| 110 | for (i = 0; i < d; i++) { |
| 111 | for (j = 0; j < d; j++) { |
| 112 | u8 val = slit->entry[d*i + j]; |
| 113 | if (i == j) { |
| 114 | if (val != 10) |
| 115 | return 0; |
| 116 | } else if (val <= 10) |
| 117 | return 0; |
| 118 | } |
| 119 | } |
| 120 | return 1; |
| 121 | } |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* Callback for SLIT parsing */ |
| 124 | void __init acpi_numa_slit_init(struct acpi_table_slit *slit) |
| 125 | { |
Andi Kleen | 1584b89 | 2006-01-11 22:43:42 +0100 | [diff] [blame] | 126 | if (!slit_valid(slit)) { |
| 127 | printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n"); |
| 128 | return; |
| 129 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | acpi_slit = slit; |
| 131 | } |
| 132 | |
| 133 | /* Callback for Proximity Domain -> LAPIC mapping */ |
| 134 | void __init |
| 135 | acpi_numa_processor_affinity_init(struct acpi_table_processor_affinity *pa) |
| 136 | { |
| 137 | int pxm, node; |
| 138 | if (srat_disabled() || pa->flags.enabled == 0) |
| 139 | return; |
| 140 | pxm = pa->proximity_domain; |
| 141 | node = setup_node(pxm); |
| 142 | if (node < 0) { |
| 143 | printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm); |
| 144 | bad_srat(); |
| 145 | return; |
| 146 | } |
Andi Kleen | 0b07e98 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 147 | apicid_to_node[pa->apic_id] = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | acpi_numa = 1; |
Andi Kleen | 0b07e98 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 149 | printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n", |
| 150 | pxm, pa->apic_id, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */ |
| 154 | void __init |
| 155 | acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma) |
| 156 | { |
| 157 | struct node *nd; |
| 158 | unsigned long start, end; |
| 159 | int node, pxm; |
| 160 | int i; |
| 161 | |
| 162 | if (srat_disabled() || ma->flags.enabled == 0) |
| 163 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | pxm = ma->proximity_domain; |
| 165 | node = setup_node(pxm); |
| 166 | if (node < 0) { |
| 167 | printk(KERN_ERR "SRAT: Too many proximity domains.\n"); |
| 168 | bad_srat(); |
| 169 | return; |
| 170 | } |
| 171 | start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32); |
| 172 | end = start + (ma->length_lo | ((u64)ma->length_hi << 32)); |
Andi Kleen | 69cb62e | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 173 | /* It is fine to add this area to the nodes data it will be used later*/ |
| 174 | if (ma->flags.hot_pluggable == 1) |
| 175 | printk(KERN_INFO "SRAT: hot plug zone found %lx - %lx \n", |
| 176 | start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | i = conflicting_nodes(start, end); |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 178 | if (i == node) { |
| 179 | printk(KERN_WARNING |
| 180 | "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n", |
| 181 | pxm, start, end, nodes[i].start, nodes[i].end); |
| 182 | } else if (i >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | printk(KERN_ERR |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 184 | "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n", |
| 185 | pxm, start, end, node_to_pxm(i), |
| 186 | nodes[i].start, nodes[i].end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | bad_srat(); |
| 188 | return; |
| 189 | } |
| 190 | nd = &nodes[node]; |
| 191 | if (!node_test_and_set(node, nodes_parsed)) { |
| 192 | nd->start = start; |
| 193 | nd->end = end; |
| 194 | } else { |
| 195 | if (start < nd->start) |
| 196 | nd->start = start; |
| 197 | if (nd->end < end) |
| 198 | nd->end = end; |
| 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm, |
| 201 | nd->start, nd->end); |
| 202 | } |
| 203 | |
Andi Kleen | 8a6fdd3 | 2006-01-11 22:44:39 +0100 | [diff] [blame] | 204 | /* Sanity check to catch more bad SRATs (they are amazingly common). |
| 205 | Make sure the PXMs cover all memory. */ |
| 206 | static int nodes_cover_memory(void) |
| 207 | { |
| 208 | int i; |
| 209 | unsigned long pxmram, e820ram; |
| 210 | |
| 211 | pxmram = 0; |
| 212 | for_each_node_mask(i, nodes_parsed) { |
| 213 | unsigned long s = nodes[i].start >> PAGE_SHIFT; |
| 214 | unsigned long e = nodes[i].end >> PAGE_SHIFT; |
| 215 | pxmram += e - s; |
| 216 | pxmram -= e820_hole_size(s, e); |
| 217 | } |
| 218 | |
| 219 | e820ram = end_pfn - e820_hole_size(0, end_pfn); |
| 220 | if (pxmram < e820ram) { |
| 221 | printk(KERN_ERR |
| 222 | "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n", |
| 223 | (pxmram << PAGE_SHIFT) >> 20, |
| 224 | (e820ram << PAGE_SHIFT) >> 20); |
| 225 | return 0; |
| 226 | } |
| 227 | return 1; |
| 228 | } |
| 229 | |
Andi Kleen | 9391a3f | 2006-02-03 21:51:17 +0100 | [diff] [blame^] | 230 | static void unparse_node(int node) |
| 231 | { |
| 232 | int i; |
| 233 | node_clear(node, nodes_parsed); |
| 234 | for (i = 0; i < MAX_LOCAL_APIC; i++) { |
| 235 | if (apicid_to_node[i] == node) |
| 236 | apicid_to_node[i] = NUMA_NO_NODE; |
| 237 | } |
| 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | void __init acpi_numa_arch_fixup(void) {} |
| 241 | |
| 242 | /* Use the information discovered above to actually set up the nodes. */ |
| 243 | int __init acpi_scan_nodes(unsigned long start, unsigned long end) |
| 244 | { |
| 245 | int i; |
Andi Kleen | 8a6fdd3 | 2006-01-11 22:44:39 +0100 | [diff] [blame] | 246 | |
Andi Kleen | 9391a3f | 2006-02-03 21:51:17 +0100 | [diff] [blame^] | 247 | /* First clean up the node list */ |
| 248 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 249 | cutoff_node(i, start, end); |
| 250 | if ((nodes[i].end - nodes[i].start) < NODE_MIN_SIZE) |
| 251 | unparse_node(i); |
| 252 | } |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | if (acpi_numa <= 0) |
| 255 | return -1; |
Andi Kleen | e58e0d0 | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 256 | |
Andi Kleen | 8a6fdd3 | 2006-01-11 22:44:39 +0100 | [diff] [blame] | 257 | if (!nodes_cover_memory()) { |
| 258 | bad_srat(); |
| 259 | return -1; |
| 260 | } |
| 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | memnode_shift = compute_hash_shift(nodes, nodes_weight(nodes_parsed)); |
| 263 | if (memnode_shift < 0) { |
| 264 | printk(KERN_ERR |
| 265 | "SRAT: No NUMA node hash function found. Contact maintainer\n"); |
| 266 | bad_srat(); |
| 267 | return -1; |
| 268 | } |
Andi Kleen | e58e0d0 | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 269 | |
| 270 | /* Finally register nodes */ |
| 271 | for_each_node_mask(i, nodes_parsed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | setup_node_bootmem(i, nodes[i].start, nodes[i].end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | for (i = 0; i < NR_CPUS; i++) { |
| 274 | if (cpu_to_node[i] == NUMA_NO_NODE) |
| 275 | continue; |
| 276 | if (!node_isset(cpu_to_node[i], nodes_parsed)) |
Andi Kleen | 69d81fc | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 277 | numa_set_node(i, NUMA_NO_NODE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | numa_init_array(); |
| 280 | return 0; |
| 281 | } |
| 282 | |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 283 | static int node_to_pxm(int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
| 285 | int i; |
| 286 | if (pxm2node[n] == n) |
| 287 | return n; |
| 288 | for (i = 0; i < 256; i++) |
| 289 | if (pxm2node[i] == n) |
| 290 | return i; |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | int __node_distance(int a, int b) |
| 295 | { |
| 296 | int index; |
| 297 | |
| 298 | if (!acpi_slit) |
| 299 | return a == b ? 10 : 20; |
| 300 | index = acpi_slit->localities * node_to_pxm(a); |
| 301 | return acpi_slit->entry[index + node_to_pxm(b)]; |
| 302 | } |
| 303 | |
| 304 | EXPORT_SYMBOL(__node_distance); |