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)
40 printk(KERN_ERR "SRAT: SRAT not used.\n");
42 memset(nodes_add, 0, sizeof(nodes_add));
45 static __init inline int srat_disabled(void)
50 /* Callback for SLIT parsing */
51 void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
57 for (i = 0; i < slit->locality_count; i++)
58 for (j = 0; j < slit->locality_count; j++)
59 numa_set_distance(pxm_to_node(i), pxm_to_node(j),
60 slit->entry[slit->locality_count * i + j]);
62 /* acpi_slit is used only by emulation */
63 length = slit->header.length;
64 phys = memblock_find_in_range(0, max_pfn_mapped<<PAGE_SHIFT, length,
67 if (phys == MEMBLOCK_ERROR)
68 panic(" Can not save slit!\n");
70 acpi_slit = __va(phys);
71 memcpy(acpi_slit, slit, length);
72 memblock_x86_reserve_range(phys, phys + length, "ACPI SLIT");
75 /* Callback for Proximity Domain -> x2APIC mapping */
77 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
84 if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
88 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
90 pxm = pa->proximity_domain;
91 node = setup_node(pxm);
93 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
98 apic_id = pa->apic_id;
99 if (apic_id >= MAX_LOCAL_APIC) {
100 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
103 set_apicid_to_node(apic_id, node);
104 node_set(node, numa_nodes_parsed);
106 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u\n",
110 /* Callback for Proximity Domain -> LAPIC mapping */
112 acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
119 if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
123 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
125 pxm = pa->proximity_domain_lo;
126 node = setup_node(pxm);
128 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
133 if (get_uv_system_type() >= UV_X2APIC)
134 apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
136 apic_id = pa->apic_id;
138 if (apic_id >= MAX_LOCAL_APIC) {
139 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
143 set_apicid_to_node(apic_id, node);
144 node_set(node, numa_nodes_parsed);
146 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u\n",
150 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
151 static inline int save_add_info(void) {return 1;}
153 static inline int save_add_info(void) {return 0;}
157 * This code supports one contiguous hot add area per node
160 update_nodes_add(int node, unsigned long start, unsigned long end)
162 unsigned long s_pfn = start >> PAGE_SHIFT;
163 unsigned long e_pfn = end >> PAGE_SHIFT;
165 struct bootnode *nd = &nodes_add[node];
167 /* I had some trouble with strange memory hotadd regions breaking
168 the boot. Be very strict here and reject anything unexpected.
169 If you want working memory hotadd write correct SRATs.
171 The node size check is a basic sanity check to guard against
173 if ((signed long)(end - start) < NODE_MIN_SIZE) {
174 printk(KERN_ERR "SRAT: Hotplug area too small\n");
178 /* This check might be a bit too strict, but I'm keeping it for now. */
179 if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
181 "SRAT: Hotplug area %lu -> %lu has existing memory\n",
188 if (nd->start == nd->end) {
193 if (nd->start == end) {
197 if (nd->end == start) {
202 printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
206 node_set(node, numa_nodes_parsed);
207 printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n",
212 /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
214 acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
216 unsigned long start, end;
221 if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
225 if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
228 if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
230 start = ma->base_address;
231 end = start + ma->length;
232 pxm = ma->proximity_domain;
233 node = setup_node(pxm);
235 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
240 if (numa_add_memblk(node, start, end) < 0) {
245 printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
248 if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
249 update_nodes_add(node, start, end);
252 void __init acpi_numa_arch_fixup(void) {}
254 int __init x86_acpi_numa_init(void)
258 ret = acpi_numa_init();
261 return srat_disabled() ? -EINVAL : 0;
264 #ifdef CONFIG_NUMA_EMU
265 static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
266 [0 ... MAX_NUMNODES-1] = PXM_INVAL
268 static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
269 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
273 * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
274 * mappings that respect the real ACPI topology but reflect our emulated
275 * environment. For each emulated node, we find which real node it appears on
276 * and create PXM to NID mappings for those fake nodes which mirror that
277 * locality. SLIT will now represent the correct distances between emulated
278 * nodes as a result of the real topology.
280 void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
284 for (i = 0; i < num_nodes; i++) {
287 nid = find_node_by_addr(fake_nodes[i].start);
288 if (nid == NUMA_NO_NODE)
290 pxm = node_to_pxm(nid);
291 if (pxm == PXM_INVAL)
293 fake_node_to_pxm_map[i] = pxm;
295 * For each apicid_to_node mapping that exists for this real
296 * node, it must now point to the fake node ID.
298 for (j = 0; j < MAX_LOCAL_APIC; j++)
299 if (__apicid_to_node[j] == nid &&
300 fake_apicid_to_node[j] == NUMA_NO_NODE)
301 fake_apicid_to_node[j] = i;
305 * If there are apicid-to-node mappings for physical nodes that do not
306 * have a corresponding emulated node, it should default to a guaranteed
309 for (i = 0; i < MAX_LOCAL_APIC; i++)
310 if (__apicid_to_node[i] != NUMA_NO_NODE &&
311 fake_apicid_to_node[i] == NUMA_NO_NODE)
312 fake_apicid_to_node[i] = 0;
314 for (i = 0; i < num_nodes; i++)
315 __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
316 memcpy(__apicid_to_node, fake_apicid_to_node, sizeof(__apicid_to_node));
318 for (i = 0; i < num_nodes; i++)
319 if (fake_nodes[i].start != fake_nodes[i].end)
320 node_set(i, numa_nodes_parsed);
323 int acpi_emu_node_distance(int a, int b)
328 return node_to_pxm(a) == node_to_pxm(b) ?
329 LOCAL_DISTANCE : REMOTE_DISTANCE;
330 index = acpi_slit->locality_count * node_to_pxm(a);
331 return acpi_slit->entry[index + node_to_pxm(b)];
333 #endif /* CONFIG_NUMA_EMU */
335 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
336 int memory_add_physaddr_to_nid(u64 start)
341 if (nodes_add[i].start <= start && nodes_add[i].end > start)
346 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);