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