blob: 8185189d34a2038f6e4be74cc302b261df3a8e69 [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>
Andi Kleen68a3a7f2006-04-07 19:49:18 +020018#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070019#include <linux/memblock.h>
Andi Kleen68a3a7f2006-04-07 19:49:18 +020020#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/proto.h>
22#include <asm/numa.h>
Andi Kleen8a6fdd32006-01-11 22:44:39 +010023#include <asm/e820.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010024#include <asm/apic.h>
Ingo Molnar4ec71fa2009-01-21 10:24:27 +010025#include <asm/uv/uv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Andi Kleenc31fbb12006-09-26 10:52:33 +020027int acpi_numa __initdata;
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static struct acpi_table_slit *acpi_slit;
30
Keith Mannthey4942e992006-09-30 23:27:06 -070031static struct bootnode nodes_add[MAX_NUMNODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33static __init int setup_node(int pxm)
34{
Yasunori Goto762834e2006-06-23 02:03:19 -070035 return acpi_map_pxm_to_node(pxm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static __init void bad_srat(void)
39{
40 printk(KERN_ERR "SRAT: SRAT not used.\n");
41 acpi_numa = -1;
Tejun Heo91556232011-02-16 17:11:09 +010042 memset(nodes_add, 0, sizeof(nodes_add));
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45static __init inline int srat_disabled(void)
46{
Tejun Heoffe77a42011-02-16 12:13:06 +010047 return acpi_numa < 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50/* Callback for SLIT parsing */
51void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
52{
Yinghai Luf302a5bb2008-07-10 20:36:37 -070053 unsigned length;
54 unsigned long phys;
55
56 length = slit->header.length;
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070057 phys = memblock_find_in_range(0, max_pfn_mapped<<PAGE_SHIFT, length,
Yinghai Luf302a5bb2008-07-10 20:36:37 -070058 PAGE_SIZE);
59
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070060 if (phys == MEMBLOCK_ERROR)
Yinghai Luf302a5bb2008-07-10 20:36:37 -070061 panic(" Can not save slit!\n");
62
63 acpi_slit = __va(phys);
64 memcpy(acpi_slit, slit, length);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070065 memblock_x86_reserve_range(phys, phys + length, "ACPI SLIT");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Suresh Siddha7237d3d2009-03-30 13:55:30 -080068/* Callback for Proximity Domain -> x2APIC mapping */
69void __init
70acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
71{
72 int pxm, node;
73 int apic_id;
74
75 if (srat_disabled())
76 return;
77 if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
78 bad_srat();
79 return;
80 }
81 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
82 return;
83 pxm = pa->proximity_domain;
84 node = setup_node(pxm);
85 if (node < 0) {
86 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
87 bad_srat();
88 return;
89 }
90
91 apic_id = pa->apic_id;
Yinghai Lud3bd0582010-12-16 19:09:58 -080092 if (apic_id >= MAX_LOCAL_APIC) {
93 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
94 return;
95 }
Tejun Heobbc9e2f2011-01-23 14:37:39 +010096 set_apicid_to_node(apic_id, node);
Tejun Heo92d4a432011-02-16 17:11:09 +010097 node_set(node, numa_nodes_parsed);
Suresh Siddha7237d3d2009-03-30 13:55:30 -080098 acpi_numa = 1;
Yinghai Lu163d3862009-11-21 00:23:37 -080099 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u\n",
Suresh Siddha7237d3d2009-03-30 13:55:30 -0800100 pxm, apic_id, node);
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* Callback for Proximity Domain -> LAPIC mapping */
104void __init
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300105acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int pxm, node;
travis@sgi.comef970012008-01-30 13:33:10 +0100108 int apic_id;
109
Andi Kleend22fe802006-02-03 21:51:26 +0100110 if (srat_disabled())
111 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300112 if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
Andi Kleenfad79062006-05-15 18:19:44 +0200113 bad_srat();
Andi Kleend22fe802006-02-03 21:51:26 +0100114 return;
115 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300116 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300118 pxm = pa->proximity_domain_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 node = setup_node(pxm);
120 if (node < 0) {
121 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
122 bad_srat();
123 return;
124 }
Yinghai Lubeafe912008-02-16 23:00:22 -0800125
Jack Steiner2e420602008-09-23 15:37:13 -0500126 if (get_uv_system_type() >= UV_X2APIC)
Jack Steinera65d1d62008-03-28 14:12:08 -0500127 apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
128 else
129 apic_id = pa->apic_id;
Yinghai Lud3bd0582010-12-16 19:09:58 -0800130
131 if (apic_id >= MAX_LOCAL_APIC) {
132 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
133 return;
134 }
135
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100136 set_apicid_to_node(apic_id, node);
Tejun Heo92d4a432011-02-16 17:11:09 +0100137 node_set(node, numa_nodes_parsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 acpi_numa = 1;
Yinghai Lu163d3862009-11-21 00:23:37 -0800139 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u\n",
travis@sgi.comef970012008-01-30 13:33:10 +0100140 pxm, apic_id, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Keith Mannthey71efa8f2006-09-30 23:27:05 -0700143#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
144static inline int save_add_info(void) {return 1;}
145#else
146static inline int save_add_info(void) {return 0;}
147#endif
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200148/*
Yinghai Lu888a5892009-05-15 13:59:37 -0700149 * Update nodes_add[]
150 * This code supports one contiguous hot add area per node
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200151 */
Yinghai Lu888a5892009-05-15 13:59:37 -0700152static void __init
153update_nodes_add(int node, unsigned long start, unsigned long end)
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200154{
155 unsigned long s_pfn = start >> PAGE_SHIFT;
156 unsigned long e_pfn = end >> PAGE_SHIFT;
Yinghai Lu888a5892009-05-15 13:59:37 -0700157 int changed = 0;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200158 struct bootnode *nd = &nodes_add[node];
159
160 /* I had some trouble with strange memory hotadd regions breaking
161 the boot. Be very strict here and reject anything unexpected.
162 If you want working memory hotadd write correct SRATs.
163
164 The node size check is a basic sanity check to guard against
165 mistakes */
166 if ((signed long)(end - start) < NODE_MIN_SIZE) {
167 printk(KERN_ERR "SRAT: Hotplug area too small\n");
Yinghai Lu888a5892009-05-15 13:59:37 -0700168 return;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200169 }
170
171 /* This check might be a bit too strict, but I'm keeping it for now. */
Mel Gorman5cb248a2006-09-27 01:49:52 -0700172 if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
Mel Gorman9c7cd682006-09-27 01:49:58 -0700173 printk(KERN_ERR
174 "SRAT: Hotplug area %lu -> %lu has existing memory\n",
175 s_pfn, e_pfn);
Yinghai Lu888a5892009-05-15 13:59:37 -0700176 return;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200177 }
178
179 /* Looks good */
180
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200181 if (nd->start == nd->end) {
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300182 nd->start = start;
183 nd->end = end;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200184 changed = 1;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300185 } else {
186 if (nd->start == end) {
187 nd->start = start;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200188 changed = 1;
189 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300190 if (nd->end == start) {
191 nd->end = end;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200192 changed = 1;
193 }
194 if (!changed)
195 printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300196 }
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200197
David Rientjes3a5fc0e2010-01-20 12:10:47 -0800198 if (changed) {
Tejun Heo92d4a432011-02-16 17:11:09 +0100199 node_set(node, numa_nodes_parsed);
Yinghai Lu888a5892009-05-15 13:59:37 -0700200 printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n",
201 nd->start, nd->end);
David Rientjes3a5fc0e2010-01-20 12:10:47 -0800202 }
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200203}
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
206void __init
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300207acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 unsigned long start, end;
210 int node, pxm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Andi Kleend22fe802006-02-03 21:51:26 +0100212 if (srat_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300214 if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
Andi Kleend22fe802006-02-03 21:51:26 +0100215 bad_srat();
216 return;
217 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300218 if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
Andi Kleend22fe802006-02-03 21:51:26 +0100219 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300220
221 if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200222 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300223 start = ma->base_address;
224 end = start + ma->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 pxm = ma->proximity_domain;
226 node = setup_node(pxm);
227 if (node < 0) {
228 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
229 bad_srat();
230 return;
231 }
Tejun Heoef396ec2011-02-16 17:11:07 +0100232
233 if (numa_add_memblk(node, start, end) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 bad_srat();
235 return;
236 }
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200237
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700238 printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
239 start, end);
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200240
Tejun Heo91556232011-02-16 17:11:09 +0100241 if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE))
242 node_set(node, mem_nodes_parsed);
243 else
Yinghai Lu888a5892009-05-15 13:59:37 -0700244 update_nodes_add(node, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247void __init acpi_numa_arch_fixup(void) {}
248
Tejun Heoa9aec562011-02-16 12:13:06 +0100249int __init x86_acpi_numa_init(void)
250{
251 int ret;
252
253 ret = acpi_numa_init();
254 if (ret < 0)
255 return ret;
256 return srat_disabled() ? -EINVAL : 0;
257}
258
David Rientjes3484d792007-07-21 17:10:32 +0200259#ifdef CONFIG_NUMA_EMU
travis@sgi.comef970012008-01-30 13:33:10 +0100260static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
261 [0 ... MAX_NUMNODES-1] = PXM_INVAL
262};
travis@sgi.com602a54a2008-01-30 13:33:21 +0100263static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
travis@sgi.comef970012008-01-30 13:33:10 +0100264 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
265};
David Rientjes3484d792007-07-21 17:10:32 +0200266
267/*
268 * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
269 * mappings that respect the real ACPI topology but reflect our emulated
270 * environment. For each emulated node, we find which real node it appears on
271 * and create PXM to NID mappings for those fake nodes which mirror that
272 * locality. SLIT will now represent the correct distances between emulated
273 * nodes as a result of the real topology.
274 */
275void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
276{
David Rientjes08705b82007-07-21 17:10:33 +0200277 int i, j;
David Rientjes3484d792007-07-21 17:10:32 +0200278
David Rientjes3484d792007-07-21 17:10:32 +0200279 for (i = 0; i < num_nodes; i++) {
280 int nid, pxm;
281
282 nid = find_node_by_addr(fake_nodes[i].start);
283 if (nid == NUMA_NO_NODE)
284 continue;
285 pxm = node_to_pxm(nid);
286 if (pxm == PXM_INVAL)
287 continue;
288 fake_node_to_pxm_map[i] = pxm;
David Rientjes08705b82007-07-21 17:10:33 +0200289 /*
290 * For each apicid_to_node mapping that exists for this real
291 * node, it must now point to the fake node ID.
292 */
293 for (j = 0; j < MAX_LOCAL_APIC; j++)
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100294 if (__apicid_to_node[j] == nid &&
David Rientjesb0c4d952010-05-06 02:24:34 -0700295 fake_apicid_to_node[j] == NUMA_NO_NODE)
David Rientjes08705b82007-07-21 17:10:33 +0200296 fake_apicid_to_node[j] = i;
David Rientjes3484d792007-07-21 17:10:32 +0200297 }
David Rientjesa387e952010-12-22 17:23:56 -0800298
299 /*
300 * If there are apicid-to-node mappings for physical nodes that do not
301 * have a corresponding emulated node, it should default to a guaranteed
302 * value.
303 */
304 for (i = 0; i < MAX_LOCAL_APIC; i++)
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100305 if (__apicid_to_node[i] != NUMA_NO_NODE &&
David Rientjesa387e952010-12-22 17:23:56 -0800306 fake_apicid_to_node[i] == NUMA_NO_NODE)
307 fake_apicid_to_node[i] = 0;
308
David Rientjes3484d792007-07-21 17:10:32 +0200309 for (i = 0; i < num_nodes; i++)
310 __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100311 memcpy(__apicid_to_node, fake_apicid_to_node, sizeof(__apicid_to_node));
David Rientjes3484d792007-07-21 17:10:32 +0200312
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100313 nodes_clear(mem_nodes_parsed);
David Rientjes3484d792007-07-21 17:10:32 +0200314 for (i = 0; i < num_nodes; i++)
315 if (fake_nodes[i].start != fake_nodes[i].end)
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100316 node_set(i, mem_nodes_parsed);
David Rientjes3484d792007-07-21 17:10:32 +0200317}
318
319static int null_slit_node_compare(int a, int b)
320{
321 return node_to_pxm(a) == node_to_pxm(b);
322}
323#else
324static int null_slit_node_compare(int a, int b)
325{
326 return a == b;
327}
328#endif /* CONFIG_NUMA_EMU */
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330int __node_distance(int a, int b)
331{
332 int index;
333
334 if (!acpi_slit)
David Rientjes3484d792007-07-21 17:10:32 +0200335 return null_slit_node_compare(a, b) ? LOCAL_DISTANCE :
336 REMOTE_DISTANCE;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300337 index = acpi_slit->locality_count * node_to_pxm(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return acpi_slit->entry[index + node_to_pxm(b)];
339}
340
341EXPORT_SYMBOL(__node_distance);
Keith Mannthey4942e992006-09-30 23:27:06 -0700342
Thomas Gleixner6a1673a2008-05-12 15:43:38 +0200343#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
Keith Mannthey4942e992006-09-30 23:27:06 -0700344int memory_add_physaddr_to_nid(u64 start)
345{
346 int i, ret = 0;
347
348 for_each_node(i)
349 if (nodes_add[i].start <= start && nodes_add[i].end > start)
350 ret = i;
351
352 return ret;
353}
Keith Mannthey8c2676a2006-09-30 23:27:07 -0700354EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
Thomas Gleixner6a1673a2008-05-12 15:43:38 +0200355#endif