blob: 9f69d28652f8f0c799b65f7450172aa86dbab9b8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
21static struct acpi_table_slit *acpi_slit;
22
23static nodemask_t nodes_parsed __initdata;
24static nodemask_t nodes_found __initdata;
25static struct node nodes[MAX_NUMNODES] __initdata;
26static __u8 pxm2node[256] = { [0 ... 255] = 0xff };
27
Andi Kleen05d1fa42005-09-12 18:49:24 +020028static int node_to_pxm(int n);
29
Andi Kleen69e1a332005-09-12 18:49:24 +020030int pxm_to_node(int pxm)
31{
32 if ((unsigned)pxm >= 256)
33 return 0;
34 return pxm2node[pxm];
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static __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
50static __init int conflicting_nodes(unsigned long start, unsigned long end)
51{
52 int i;
Andi Kleen4b6a4552005-09-12 18:49:25 +020053 for_each_node_mask(i, nodes_parsed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct node *nd = &nodes[i];
55 if (nd->start == nd->end)
56 continue;
57 if (nd->end > start && nd->start < end)
Andi Kleen05d1fa42005-09-12 18:49:24 +020058 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (nd->end == end && nd->start == start)
Andi Kleen05d1fa42005-09-12 18:49:24 +020060 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
62 return -1;
63}
64
65static __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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 nd->end = end;
75 if (nd->start > nd->end)
76 nd->start = nd->end;
77 }
78}
79
80static __init void bad_srat(void)
81{
Andi Kleen2bce2b52005-09-12 18:49:25 +020082 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 printk(KERN_ERR "SRAT: SRAT not used.\n");
84 acpi_numa = -1;
Andi Kleen2bce2b52005-09-12 18:49:25 +020085 for (i = 0; i < MAX_LOCAL_APIC; i++)
86 apicid_to_node[i] = NUMA_NO_NODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89static __init inline int srat_disabled(void)
90{
91 return numa_off || acpi_numa < 0;
92}
93
Andi Kleen1584b892006-01-11 22:43:42 +010094/*
95 * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
96 * up the NUMA heuristics which wants the local node to have a smaller
97 * distance than the others.
98 * Do some quick checks here and only use the SLIT if it passes.
99 */
100static __init int slit_valid(struct acpi_table_slit *slit)
101{
102 int i, j;
103 int d = slit->localities;
104 for (i = 0; i < d; i++) {
105 for (j = 0; j < d; j++) {
106 u8 val = slit->entry[d*i + j];
107 if (i == j) {
108 if (val != 10)
109 return 0;
110 } else if (val <= 10)
111 return 0;
112 }
113 }
114 return 1;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* Callback for SLIT parsing */
118void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
119{
Andi Kleen1584b892006-01-11 22:43:42 +0100120 if (!slit_valid(slit)) {
121 printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
122 return;
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 acpi_slit = slit;
125}
126
127/* Callback for Proximity Domain -> LAPIC mapping */
128void __init
129acpi_numa_processor_affinity_init(struct acpi_table_processor_affinity *pa)
130{
131 int pxm, node;
132 if (srat_disabled() || pa->flags.enabled == 0)
133 return;
134 pxm = pa->proximity_domain;
135 node = setup_node(pxm);
136 if (node < 0) {
137 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
138 bad_srat();
139 return;
140 }
Andi Kleen0b07e982005-09-12 18:49:24 +0200141 apicid_to_node[pa->apic_id] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 acpi_numa = 1;
Andi Kleen0b07e982005-09-12 18:49:24 +0200143 printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
144 pxm, pa->apic_id, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
148void __init
149acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma)
150{
151 struct node *nd;
152 unsigned long start, end;
153 int node, pxm;
154 int i;
155
156 if (srat_disabled() || ma->flags.enabled == 0)
157 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 pxm = ma->proximity_domain;
159 node = setup_node(pxm);
160 if (node < 0) {
161 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
162 bad_srat();
163 return;
164 }
165 start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32);
166 end = start + (ma->length_lo | ((u64)ma->length_hi << 32));
Andi Kleen69cb62e2005-07-28 21:15:39 -0700167 /* It is fine to add this area to the nodes data it will be used later*/
168 if (ma->flags.hot_pluggable == 1)
169 printk(KERN_INFO "SRAT: hot plug zone found %lx - %lx \n",
170 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 i = conflicting_nodes(start, end);
Andi Kleen05d1fa42005-09-12 18:49:24 +0200172 if (i == node) {
173 printk(KERN_WARNING
174 "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
175 pxm, start, end, nodes[i].start, nodes[i].end);
176 } else if (i >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 printk(KERN_ERR
Andi Kleen05d1fa42005-09-12 18:49:24 +0200178 "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
179 pxm, start, end, node_to_pxm(i),
180 nodes[i].start, nodes[i].end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 bad_srat();
182 return;
183 }
184 nd = &nodes[node];
185 if (!node_test_and_set(node, nodes_parsed)) {
186 nd->start = start;
187 nd->end = end;
188 } else {
189 if (start < nd->start)
190 nd->start = start;
191 if (nd->end < end)
192 nd->end = end;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm,
195 nd->start, nd->end);
196}
197
198void __init acpi_numa_arch_fixup(void) {}
199
200/* Use the information discovered above to actually set up the nodes. */
201int __init acpi_scan_nodes(unsigned long start, unsigned long end)
202{
203 int i;
204 if (acpi_numa <= 0)
205 return -1;
Andi Kleene58e0d02005-09-12 18:49:25 +0200206
207 /* First clean up the node list */
208 for_each_node_mask(i, nodes_parsed) {
209 cutoff_node(i, start, end);
210 if (nodes[i].start == nodes[i].end)
211 node_clear(i, nodes_parsed);
212 }
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 memnode_shift = compute_hash_shift(nodes, nodes_weight(nodes_parsed));
215 if (memnode_shift < 0) {
216 printk(KERN_ERR
217 "SRAT: No NUMA node hash function found. Contact maintainer\n");
218 bad_srat();
219 return -1;
220 }
Andi Kleene58e0d02005-09-12 18:49:25 +0200221
222 /* Finally register nodes */
223 for_each_node_mask(i, nodes_parsed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 for (i = 0; i < NR_CPUS; i++) {
226 if (cpu_to_node[i] == NUMA_NO_NODE)
227 continue;
228 if (!node_isset(cpu_to_node[i], nodes_parsed))
Andi Kleen69d81fc2005-11-05 17:25:53 +0100229 numa_set_node(i, NUMA_NO_NODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 numa_init_array();
232 return 0;
233}
234
Andi Kleen05d1fa42005-09-12 18:49:24 +0200235static int node_to_pxm(int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i;
238 if (pxm2node[n] == n)
239 return n;
240 for (i = 0; i < 256; i++)
241 if (pxm2node[i] == n)
242 return i;
243 return 0;
244}
245
246int __node_distance(int a, int b)
247{
248 int index;
249
250 if (!acpi_slit)
251 return a == b ? 10 : 20;
252 index = acpi_slit->localities * node_to_pxm(a);
253 return acpi_slit->entry[index + node_to_pxm(b)];
254}
255
256EXPORT_SYMBOL(__node_distance);