2 * acpi_numa.c - ACPI NUMA support
4 * Copyright (C) 2002 Takayoshi Kochi <t-kochi@bq.jp.nec.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/acpi.h>
27 #include <linux/numa.h>
28 #include <linux/nodemask.h>
29 #include <linux/topology.h>
31 #define PREFIX "ACPI: "
33 #define ACPI_NUMA 0x80000000
34 #define _COMPONENT ACPI_NUMA
35 ACPI_MODULE_NAME("numa");
37 static nodemask_t nodes_found_map = NODE_MASK_NONE;
39 /* maps to convert between proximity domain and logical node ID */
40 static int pxm_to_node_map[MAX_PXM_DOMAINS]
41 = { [0 ... MAX_PXM_DOMAINS - 1] = NUMA_NO_NODE };
42 static int node_to_pxm_map[MAX_NUMNODES]
43 = { [0 ... MAX_NUMNODES - 1] = PXM_INVAL };
45 unsigned char acpi_srat_revision __initdata;
47 int pxm_to_node(int pxm)
51 return pxm_to_node_map[pxm];
54 int node_to_pxm(int node)
58 return node_to_pxm_map[node];
61 static void __acpi_map_pxm_to_node(int pxm, int node)
63 if (pxm_to_node_map[pxm] == NUMA_NO_NODE || node < pxm_to_node_map[pxm])
64 pxm_to_node_map[pxm] = node;
65 if (node_to_pxm_map[node] == PXM_INVAL || pxm < node_to_pxm_map[node])
66 node_to_pxm_map[node] = pxm;
69 int acpi_map_pxm_to_node(int pxm)
73 if (pxm < 0 || pxm >= MAX_PXM_DOMAINS)
76 node = pxm_to_node_map[pxm];
78 if (node == NUMA_NO_NODE) {
79 if (nodes_weight(nodes_found_map) >= MAX_NUMNODES)
81 node = first_unset_node(nodes_found_map);
82 __acpi_map_pxm_to_node(pxm, node);
83 node_set(node, nodes_found_map);
90 * acpi_map_pxm_to_online_node - Map proximity ID to online node
91 * @pxm: ACPI proximity ID
93 * This is similar to acpi_map_pxm_to_node(), but always returns an online
94 * node. When the mapped node from a given proximity ID is offline, it
95 * looks up the node distance table and returns the nearest online node.
97 * ACPI device drivers, which are called after the NUMA initialization has
98 * completed in the kernel, can call this interface to obtain their device
99 * NUMA topology from ACPI tables. Such drivers do not have to deal with
100 * offline nodes. A node may be offline when a device proximity ID is
101 * unique, SRAT memory entry does not exist, or NUMA is disabled, ex.
104 int acpi_map_pxm_to_online_node(int pxm)
108 node = acpi_map_pxm_to_node(pxm);
110 if (node == NUMA_NO_NODE)
114 if (!node_online(node)) {
115 int min_dist = INT_MAX, dist, n;
117 for_each_online_node(n) {
118 dist = node_distance(node, n);
119 if (dist < min_dist) {
128 EXPORT_SYMBOL(acpi_map_pxm_to_online_node);
131 acpi_table_print_srat_entry(struct acpi_subtable_header *header)
134 ACPI_FUNCTION_NAME("acpi_table_print_srat_entry");
139 switch (header->type) {
141 case ACPI_SRAT_TYPE_CPU_AFFINITY:
142 #ifdef ACPI_DEBUG_OUTPUT
144 struct acpi_srat_cpu_affinity *p =
145 (struct acpi_srat_cpu_affinity *)header;
146 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
147 "SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
148 p->apic_id, p->local_sapic_eid,
149 p->proximity_domain_lo,
150 (p->flags & ACPI_SRAT_CPU_ENABLED)?
151 "enabled" : "disabled"));
153 #endif /* ACPI_DEBUG_OUTPUT */
156 case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
157 #ifdef ACPI_DEBUG_OUTPUT
159 struct acpi_srat_mem_affinity *p =
160 (struct acpi_srat_mem_affinity *)header;
161 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
162 "SRAT Memory (0x%lx length 0x%lx) in proximity domain %d %s%s%s\n",
163 (unsigned long)p->base_address,
164 (unsigned long)p->length,
166 (p->flags & ACPI_SRAT_MEM_ENABLED)?
167 "enabled" : "disabled",
168 (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)?
169 " hot-pluggable" : "",
170 (p->flags & ACPI_SRAT_MEM_NON_VOLATILE)?
171 " non-volatile" : ""));
173 #endif /* ACPI_DEBUG_OUTPUT */
176 case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
177 #ifdef ACPI_DEBUG_OUTPUT
179 struct acpi_srat_x2apic_cpu_affinity *p =
180 (struct acpi_srat_x2apic_cpu_affinity *)header;
181 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
182 "SRAT Processor (x2apicid[0x%08x]) in"
183 " proximity domain %d %s\n",
186 (p->flags & ACPI_SRAT_CPU_ENABLED) ?
187 "enabled" : "disabled"));
189 #endif /* ACPI_DEBUG_OUTPUT */
192 printk(KERN_WARNING PREFIX
193 "Found unsupported SRAT entry (type = 0x%x)\n",
200 * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
201 * up the NUMA heuristics which wants the local node to have a smaller
202 * distance than the others.
203 * Do some quick checks here and only use the SLIT if it passes.
205 static int __init slit_valid(struct acpi_table_slit *slit)
208 int d = slit->locality_count;
209 for (i = 0; i < d; i++) {
210 for (j = 0; j < d; j++) {
211 u8 val = slit->entry[d*i + j];
213 if (val != LOCAL_DISTANCE)
215 } else if (val <= LOCAL_DISTANCE)
222 static int __init acpi_parse_slit(struct acpi_table_header *table)
224 struct acpi_table_slit *slit = (struct acpi_table_slit *)table;
226 if (!slit_valid(slit)) {
227 printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
230 acpi_numa_slit_init(slit);
236 acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
238 printk(KERN_WARNING PREFIX
239 "Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
245 acpi_parse_x2apic_affinity(struct acpi_subtable_header *header,
246 const unsigned long end)
248 struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
250 processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
251 if (!processor_affinity)
254 acpi_table_print_srat_entry(header);
256 /* let architecture-dependent part to do it */
257 acpi_numa_x2apic_affinity_init(processor_affinity);
263 acpi_parse_processor_affinity(struct acpi_subtable_header *header,
264 const unsigned long end)
266 struct acpi_srat_cpu_affinity *processor_affinity;
268 processor_affinity = (struct acpi_srat_cpu_affinity *)header;
269 if (!processor_affinity)
272 acpi_table_print_srat_entry(header);
274 /* let architecture-dependent part to do it */
275 acpi_numa_processor_affinity_init(processor_affinity);
280 static int __initdata parsed_numa_memblks;
283 acpi_parse_memory_affinity(struct acpi_subtable_header * header,
284 const unsigned long end)
286 struct acpi_srat_mem_affinity *memory_affinity;
288 memory_affinity = (struct acpi_srat_mem_affinity *)header;
289 if (!memory_affinity)
292 acpi_table_print_srat_entry(header);
294 /* let architecture-dependent part to do it */
295 if (!acpi_numa_memory_affinity_init(memory_affinity))
296 parsed_numa_memblks++;
300 static int __init acpi_parse_srat(struct acpi_table_header *table)
302 struct acpi_table_srat *srat = (struct acpi_table_srat *)table;
304 acpi_srat_revision = srat->header.revision;
306 /* Real work done in acpi_table_parse_srat below. */
312 acpi_table_parse_srat(enum acpi_srat_type id,
313 acpi_tbl_entry_handler handler, unsigned int max_entries)
315 return acpi_table_parse_entries(ACPI_SIG_SRAT,
316 sizeof(struct acpi_table_srat), id,
317 handler, max_entries);
320 int __init acpi_numa_init(void)
325 * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
326 * SRAT cpu entries could have different order with that in MADT.
327 * So go over all cpu entries in SRAT to get apicid to node mapping.
330 /* SRAT: Static Resource Affinity Table */
331 if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
332 struct acpi_subtable_proc srat_proc[2];
334 memset(srat_proc, 0, sizeof(srat_proc));
335 srat_proc[0].id = ACPI_SRAT_TYPE_CPU_AFFINITY;
336 srat_proc[0].handler = acpi_parse_processor_affinity;
337 srat_proc[1].id = ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY;
338 srat_proc[1].handler = acpi_parse_x2apic_affinity;
340 acpi_table_parse_entries_array(ACPI_SIG_SRAT,
341 sizeof(struct acpi_table_srat),
342 srat_proc, ARRAY_SIZE(srat_proc), 0);
344 cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
345 acpi_parse_memory_affinity,
349 /* SLIT: System Locality Information Table */
350 acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
352 acpi_numa_arch_fixup();
356 else if (!parsed_numa_memblks)
361 static int acpi_get_pxm(acpi_handle h)
363 unsigned long long pxm;
366 acpi_handle phandle = h;
370 status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
371 if (ACPI_SUCCESS(status))
373 status = acpi_get_parent(handle, &phandle);
374 } while (ACPI_SUCCESS(status));
378 int acpi_get_node(acpi_handle handle)
382 pxm = acpi_get_pxm(handle);
384 return acpi_map_pxm_to_node(pxm);
386 EXPORT_SYMBOL(acpi_get_node);