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