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; |
| 26 | static __u8 pxm2node[256] = { [0 ... 255] = 0xff }; |
| 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) |
| 33 | return 0; |
| 34 | return pxm2node[pxm]; |
| 35 | } |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | static __init int setup_node(int pxm) |
| 38 | { |
| 39 | unsigned node = pxm2node[pxm]; |
| 40 | if (node == 0xff) { |
| 41 | if (nodes_weight(nodes_found) >= MAX_NUMNODES) |
| 42 | return -1; |
| 43 | node = first_unset_node(nodes_found); |
| 44 | node_set(node, nodes_found); |
| 45 | pxm2node[pxm] = node; |
| 46 | } |
| 47 | return pxm2node[pxm]; |
| 48 | } |
| 49 | |
| 50 | static __init int conflicting_nodes(unsigned long start, unsigned long end) |
| 51 | { |
| 52 | int i; |
| 53 | for_each_online_node(i) { |
| 54 | struct node *nd = &nodes[i]; |
| 55 | if (nd->start == nd->end) |
| 56 | continue; |
| 57 | if (nd->end > start && nd->start < end) |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame^] | 58 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (nd->end == end && nd->start == start) |
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 | } |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | static __init void cutoff_node(int i, unsigned long start, unsigned long end) |
| 66 | { |
| 67 | struct node *nd = &nodes[i]; |
| 68 | if (nd->start < start) { |
| 69 | nd->start = start; |
| 70 | if (nd->end < nd->start) |
| 71 | nd->start = nd->end; |
| 72 | } |
| 73 | if (nd->end > end) { |
| 74 | if (!(end & 0xfff)) |
| 75 | end--; |
| 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 | { |
| 84 | printk(KERN_ERR "SRAT: SRAT not used.\n"); |
| 85 | acpi_numa = -1; |
| 86 | } |
| 87 | |
| 88 | static __init inline int srat_disabled(void) |
| 89 | { |
| 90 | return numa_off || acpi_numa < 0; |
| 91 | } |
| 92 | |
| 93 | /* Callback for SLIT parsing */ |
| 94 | void __init acpi_numa_slit_init(struct acpi_table_slit *slit) |
| 95 | { |
| 96 | acpi_slit = slit; |
| 97 | } |
| 98 | |
| 99 | /* Callback for Proximity Domain -> LAPIC mapping */ |
| 100 | void __init |
| 101 | acpi_numa_processor_affinity_init(struct acpi_table_processor_affinity *pa) |
| 102 | { |
| 103 | int pxm, node; |
| 104 | if (srat_disabled() || pa->flags.enabled == 0) |
| 105 | return; |
| 106 | pxm = pa->proximity_domain; |
| 107 | node = setup_node(pxm); |
| 108 | if (node < 0) { |
| 109 | printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm); |
| 110 | bad_srat(); |
| 111 | return; |
| 112 | } |
Andi Kleen | 0b07e98 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 113 | apicid_to_node[pa->apic_id] = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | acpi_numa = 1; |
Andi Kleen | 0b07e98 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 115 | printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n", |
| 116 | pxm, pa->apic_id, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */ |
| 120 | void __init |
| 121 | acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma) |
| 122 | { |
| 123 | struct node *nd; |
| 124 | unsigned long start, end; |
| 125 | int node, pxm; |
| 126 | int i; |
| 127 | |
| 128 | if (srat_disabled() || ma->flags.enabled == 0) |
| 129 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | pxm = ma->proximity_domain; |
| 131 | node = setup_node(pxm); |
| 132 | if (node < 0) { |
| 133 | printk(KERN_ERR "SRAT: Too many proximity domains.\n"); |
| 134 | bad_srat(); |
| 135 | return; |
| 136 | } |
| 137 | start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32); |
| 138 | end = start + (ma->length_lo | ((u64)ma->length_hi << 32)); |
Andi Kleen | 69cb62e | 2005-07-28 21:15:39 -0700 | [diff] [blame] | 139 | /* It is fine to add this area to the nodes data it will be used later*/ |
| 140 | if (ma->flags.hot_pluggable == 1) |
| 141 | printk(KERN_INFO "SRAT: hot plug zone found %lx - %lx \n", |
| 142 | start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | i = conflicting_nodes(start, end); |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame^] | 144 | if (i == node) { |
| 145 | printk(KERN_WARNING |
| 146 | "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n", |
| 147 | pxm, start, end, nodes[i].start, nodes[i].end); |
| 148 | } else if (i >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | printk(KERN_ERR |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame^] | 150 | "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n", |
| 151 | pxm, start, end, node_to_pxm(i), |
| 152 | nodes[i].start, nodes[i].end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | bad_srat(); |
| 154 | return; |
| 155 | } |
| 156 | nd = &nodes[node]; |
| 157 | if (!node_test_and_set(node, nodes_parsed)) { |
| 158 | nd->start = start; |
| 159 | nd->end = end; |
| 160 | } else { |
| 161 | if (start < nd->start) |
| 162 | nd->start = start; |
| 163 | if (nd->end < end) |
| 164 | nd->end = end; |
| 165 | } |
| 166 | if (!(nd->end & 0xfff)) |
| 167 | nd->end--; |
| 168 | printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm, |
| 169 | nd->start, nd->end); |
| 170 | } |
| 171 | |
| 172 | void __init acpi_numa_arch_fixup(void) {} |
| 173 | |
| 174 | /* Use the information discovered above to actually set up the nodes. */ |
| 175 | int __init acpi_scan_nodes(unsigned long start, unsigned long end) |
| 176 | { |
| 177 | int i; |
| 178 | if (acpi_numa <= 0) |
| 179 | return -1; |
| 180 | memnode_shift = compute_hash_shift(nodes, nodes_weight(nodes_parsed)); |
| 181 | if (memnode_shift < 0) { |
| 182 | printk(KERN_ERR |
| 183 | "SRAT: No NUMA node hash function found. Contact maintainer\n"); |
| 184 | bad_srat(); |
| 185 | return -1; |
| 186 | } |
| 187 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 188 | if (!node_isset(i, nodes_parsed)) |
| 189 | continue; |
| 190 | cutoff_node(i, start, end); |
| 191 | if (nodes[i].start == nodes[i].end) { |
| 192 | node_clear(i, nodes_parsed); |
| 193 | continue; |
| 194 | } |
| 195 | setup_node_bootmem(i, nodes[i].start, nodes[i].end); |
| 196 | } |
| 197 | for (i = 0; i < NR_CPUS; i++) { |
| 198 | if (cpu_to_node[i] == NUMA_NO_NODE) |
| 199 | continue; |
| 200 | if (!node_isset(cpu_to_node[i], nodes_parsed)) |
| 201 | cpu_to_node[i] = NUMA_NO_NODE; |
| 202 | } |
| 203 | numa_init_array(); |
| 204 | return 0; |
| 205 | } |
| 206 | |
Andi Kleen | 05d1fa4 | 2005-09-12 18:49:24 +0200 | [diff] [blame^] | 207 | static int node_to_pxm(int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | int i; |
| 210 | if (pxm2node[n] == n) |
| 211 | return n; |
| 212 | for (i = 0; i < 256; i++) |
| 213 | if (pxm2node[i] == n) |
| 214 | return i; |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | int __node_distance(int a, int b) |
| 219 | { |
| 220 | int index; |
| 221 | |
| 222 | if (!acpi_slit) |
| 223 | return a == b ? 10 : 20; |
| 224 | index = acpi_slit->localities * node_to_pxm(a); |
| 225 | return acpi_slit->entry[index + node_to_pxm(b)]; |
| 226 | } |
| 227 | |
| 228 | EXPORT_SYMBOL(__node_distance); |