2 * ACPI 3.0 based NUMA setup
3 * Copyright 2004 Andi Kleen, SuSE Labs.
5 * Reads the ACPI SRAT table to figure out what memory belongs to which CPUs.
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.
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 <linux/bootmem.h>
19 #include <linux/memblock.h>
21 #include <asm/proto.h>
25 #include <asm/uv/uv.h>
27 int acpi_numa __initdata;
29 static struct acpi_table_slit *acpi_slit;
31 static struct bootnode nodes_add[MAX_NUMNODES];
33 static __init int setup_node(int pxm)
35 return acpi_map_pxm_to_node(pxm);
38 static __init void bad_srat(void)
41 printk(KERN_ERR "SRAT: SRAT not used.\n");
43 for (i = 0; i < MAX_NUMNODES; i++) {
44 numa_nodes[i].start = numa_nodes[i].end = 0;
45 nodes_add[i].start = nodes_add[i].end = 0;
49 static __init inline int srat_disabled(void)
54 /* Callback for SLIT parsing */
55 void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
60 length = slit->header.length;
61 phys = memblock_find_in_range(0, max_pfn_mapped<<PAGE_SHIFT, length,
64 if (phys == MEMBLOCK_ERROR)
65 panic(" Can not save slit!\n");
67 acpi_slit = __va(phys);
68 memcpy(acpi_slit, slit, length);
69 memblock_x86_reserve_range(phys, phys + length, "ACPI SLIT");
72 /* Callback for Proximity Domain -> x2APIC mapping */
74 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
81 if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
85 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
87 pxm = pa->proximity_domain;
88 node = setup_node(pxm);
90 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
95 apic_id = pa->apic_id;
96 if (apic_id >= MAX_LOCAL_APIC) {
97 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
100 set_apicid_to_node(apic_id, node);
101 node_set(node, cpu_nodes_parsed);
103 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u\n",
107 /* Callback for Proximity Domain -> LAPIC mapping */
109 acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
116 if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
120 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
122 pxm = pa->proximity_domain_lo;
123 node = setup_node(pxm);
125 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
130 if (get_uv_system_type() >= UV_X2APIC)
131 apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
133 apic_id = pa->apic_id;
135 if (apic_id >= MAX_LOCAL_APIC) {
136 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
140 set_apicid_to_node(apic_id, node);
141 node_set(node, cpu_nodes_parsed);
143 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u\n",
147 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
148 static inline int save_add_info(void) {return 1;}
150 static inline int save_add_info(void) {return 0;}
154 * This code supports one contiguous hot add area per node
157 update_nodes_add(int node, unsigned long start, unsigned long end)
159 unsigned long s_pfn = start >> PAGE_SHIFT;
160 unsigned long e_pfn = end >> PAGE_SHIFT;
162 struct bootnode *nd = &nodes_add[node];
164 /* I had some trouble with strange memory hotadd regions breaking
165 the boot. Be very strict here and reject anything unexpected.
166 If you want working memory hotadd write correct SRATs.
168 The node size check is a basic sanity check to guard against
170 if ((signed long)(end - start) < NODE_MIN_SIZE) {
171 printk(KERN_ERR "SRAT: Hotplug area too small\n");
175 /* This check might be a bit too strict, but I'm keeping it for now. */
176 if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
178 "SRAT: Hotplug area %lu -> %lu has existing memory\n",
185 if (nd->start == nd->end) {
190 if (nd->start == end) {
194 if (nd->end == start) {
199 printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
203 node_set(node, cpu_nodes_parsed);
204 printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n",
209 /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
211 acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
214 unsigned long start, end;
219 if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
223 if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
226 if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
228 start = ma->base_address;
229 end = start + ma->length;
230 pxm = ma->proximity_domain;
231 node = setup_node(pxm);
233 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
238 if (numa_add_memblk(node, start, end) < 0) {
243 printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
246 if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)) {
247 nd = &numa_nodes[node];
248 if (!node_test_and_set(node, mem_nodes_parsed)) {
252 if (start < nd->start)
258 update_nodes_add(node, start, end);
261 void __init acpi_numa_arch_fixup(void) {}
263 int __init x86_acpi_numa_init(void)
267 ret = acpi_numa_init();
270 return srat_disabled() ? -EINVAL : 0;
273 /* Use the information discovered above to actually set up the nodes. */
274 int __init acpi_scan_nodes(void)
281 #ifdef CONFIG_NUMA_EMU
282 static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
283 [0 ... MAX_NUMNODES-1] = PXM_INVAL
285 static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
286 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
288 static int __init find_node_by_addr(unsigned long addr)
290 int ret = NUMA_NO_NODE;
293 for_each_node_mask(i, mem_nodes_parsed) {
295 * Find the real node that this emulated node appears on. For
296 * the sake of simplicity, we only use a real node's starting
297 * address to determine which emulated node it appears on.
299 if (addr >= numa_nodes[i].start && addr < numa_nodes[i].end) {
308 * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
309 * mappings that respect the real ACPI topology but reflect our emulated
310 * environment. For each emulated node, we find which real node it appears on
311 * and create PXM to NID mappings for those fake nodes which mirror that
312 * locality. SLIT will now represent the correct distances between emulated
313 * nodes as a result of the real topology.
315 void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
319 for (i = 0; i < num_nodes; i++) {
322 nid = find_node_by_addr(fake_nodes[i].start);
323 if (nid == NUMA_NO_NODE)
325 pxm = node_to_pxm(nid);
326 if (pxm == PXM_INVAL)
328 fake_node_to_pxm_map[i] = pxm;
330 * For each apicid_to_node mapping that exists for this real
331 * node, it must now point to the fake node ID.
333 for (j = 0; j < MAX_LOCAL_APIC; j++)
334 if (__apicid_to_node[j] == nid &&
335 fake_apicid_to_node[j] == NUMA_NO_NODE)
336 fake_apicid_to_node[j] = i;
340 * If there are apicid-to-node mappings for physical nodes that do not
341 * have a corresponding emulated node, it should default to a guaranteed
344 for (i = 0; i < MAX_LOCAL_APIC; i++)
345 if (__apicid_to_node[i] != NUMA_NO_NODE &&
346 fake_apicid_to_node[i] == NUMA_NO_NODE)
347 fake_apicid_to_node[i] = 0;
349 for (i = 0; i < num_nodes; i++)
350 __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
351 memcpy(__apicid_to_node, fake_apicid_to_node, sizeof(__apicid_to_node));
353 nodes_clear(mem_nodes_parsed);
354 for (i = 0; i < num_nodes; i++)
355 if (fake_nodes[i].start != fake_nodes[i].end)
356 node_set(i, mem_nodes_parsed);
359 static int null_slit_node_compare(int a, int b)
361 return node_to_pxm(a) == node_to_pxm(b);
364 static int null_slit_node_compare(int a, int b)
368 #endif /* CONFIG_NUMA_EMU */
370 int __node_distance(int a, int b)
375 return null_slit_node_compare(a, b) ? LOCAL_DISTANCE :
377 index = acpi_slit->locality_count * node_to_pxm(a);
378 return acpi_slit->entry[index + node_to_pxm(b)];
381 EXPORT_SYMBOL(__node_distance);
383 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
384 int memory_add_physaddr_to_nid(u64 start)
389 if (nodes_add[i].start <= start && nodes_add[i].end > start)
394 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);