numa.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * acpi_numa.c - ACPI NUMA support
  3. *
  4. * Copyright (C) 2002 Takayoshi Kochi <t-kochi@bq.jp.nec.com>
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  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.
  12. *
  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.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/types.h>
  29. #include <linux/errno.h>
  30. #include <linux/acpi.h>
  31. #include <linux/numa.h>
  32. #include <linux/nodemask.h>
  33. #include <linux/topology.h>
  34. #define PREFIX "ACPI: "
  35. #define ACPI_NUMA 0x80000000
  36. #define _COMPONENT ACPI_NUMA
  37. ACPI_MODULE_NAME("numa");
  38. 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 };
  44. unsigned char acpi_srat_revision __initdata;
  45. int pxm_to_node(int pxm)
  46. {
  47. if (pxm < 0)
  48. return NUMA_NO_NODE;
  49. return pxm_to_node_map[pxm];
  50. }
  51. int node_to_pxm(int node)
  52. {
  53. if (node < 0)
  54. return PXM_INVAL;
  55. return node_to_pxm_map[node];
  56. }
  57. static void __acpi_map_pxm_to_node(int pxm, int node)
  58. {
  59. if (pxm_to_node_map[pxm] == NUMA_NO_NODE || node < pxm_to_node_map[pxm])
  60. pxm_to_node_map[pxm] = node;
  61. if (node_to_pxm_map[node] == PXM_INVAL || pxm < node_to_pxm_map[node])
  62. node_to_pxm_map[node] = pxm;
  63. }
  64. int acpi_map_pxm_to_node(int pxm)
  65. {
  66. int node;
  67. if (pxm < 0 || pxm >= MAX_PXM_DOMAINS)
  68. return NUMA_NO_NODE;
  69. node = pxm_to_node_map[pxm];
  70. if (node == NUMA_NO_NODE) {
  71. if (nodes_weight(nodes_found_map) >= MAX_NUMNODES)
  72. return NUMA_NO_NODE;
  73. node = first_unset_node(nodes_found_map);
  74. __acpi_map_pxm_to_node(pxm, node);
  75. node_set(node, nodes_found_map);
  76. }
  77. return node;
  78. }
  79. /**
  80. * acpi_map_pxm_to_online_node - Map proximity ID to online node
  81. * @pxm: ACPI proximity ID
  82. *
  83. * This is similar to acpi_map_pxm_to_node(), but always returns an online
  84. * node. When the mapped node from a given proximity ID is offline, it
  85. * looks up the node distance table and returns the nearest online node.
  86. *
  87. * ACPI device drivers, which are called after the NUMA initialization has
  88. * completed in the kernel, can call this interface to obtain their device
  89. * NUMA topology from ACPI tables. Such drivers do not have to deal with
  90. * offline nodes. A node may be offline when a device proximity ID is
  91. * unique, SRAT memory entry does not exist, or NUMA is disabled, ex.
  92. * "numa=off" on x86.
  93. */
  94. int acpi_map_pxm_to_online_node(int pxm)
  95. {
  96. int node, n, dist, min_dist;
  97. node = acpi_map_pxm_to_node(pxm);
  98. if (node == NUMA_NO_NODE)
  99. node = 0;
  100. if (!node_online(node)) {
  101. min_dist = INT_MAX;
  102. for_each_online_node(n) {
  103. dist = node_distance(node, n);
  104. if (dist < min_dist) {
  105. min_dist = dist;
  106. node = n;
  107. }
  108. }
  109. }
  110. return node;
  111. }
  112. EXPORT_SYMBOL(acpi_map_pxm_to_online_node);
  113. static void __init
  114. acpi_table_print_srat_entry(struct acpi_subtable_header *header)
  115. {
  116. ACPI_FUNCTION_NAME("acpi_table_print_srat_entry");
  117. if (!header)
  118. return;
  119. switch (header->type) {
  120. case ACPI_SRAT_TYPE_CPU_AFFINITY:
  121. #ifdef ACPI_DEBUG_OUTPUT
  122. {
  123. struct acpi_srat_cpu_affinity *p =
  124. (struct acpi_srat_cpu_affinity *)header;
  125. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  126. "SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
  127. p->apic_id, p->local_sapic_eid,
  128. p->proximity_domain_lo,
  129. (p->flags & ACPI_SRAT_CPU_ENABLED)?
  130. "enabled" : "disabled"));
  131. }
  132. #endif /* ACPI_DEBUG_OUTPUT */
  133. break;
  134. case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
  135. #ifdef ACPI_DEBUG_OUTPUT
  136. {
  137. struct acpi_srat_mem_affinity *p =
  138. (struct acpi_srat_mem_affinity *)header;
  139. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  140. "SRAT Memory (0x%lx length 0x%lx) in proximity domain %d %s%s%s\n",
  141. (unsigned long)p->base_address,
  142. (unsigned long)p->length,
  143. p->proximity_domain,
  144. (p->flags & ACPI_SRAT_MEM_ENABLED)?
  145. "enabled" : "disabled",
  146. (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)?
  147. " hot-pluggable" : "",
  148. (p->flags & ACPI_SRAT_MEM_NON_VOLATILE)?
  149. " non-volatile" : ""));
  150. }
  151. #endif /* ACPI_DEBUG_OUTPUT */
  152. break;
  153. case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
  154. #ifdef ACPI_DEBUG_OUTPUT
  155. {
  156. struct acpi_srat_x2apic_cpu_affinity *p =
  157. (struct acpi_srat_x2apic_cpu_affinity *)header;
  158. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  159. "SRAT Processor (x2apicid[0x%08x]) in"
  160. " proximity domain %d %s\n",
  161. p->apic_id,
  162. p->proximity_domain,
  163. (p->flags & ACPI_SRAT_CPU_ENABLED) ?
  164. "enabled" : "disabled"));
  165. }
  166. #endif /* ACPI_DEBUG_OUTPUT */
  167. break;
  168. default:
  169. printk(KERN_WARNING PREFIX
  170. "Found unsupported SRAT entry (type = 0x%x)\n",
  171. header->type);
  172. break;
  173. }
  174. }
  175. /*
  176. * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
  177. * up the NUMA heuristics which wants the local node to have a smaller
  178. * distance than the others.
  179. * Do some quick checks here and only use the SLIT if it passes.
  180. */
  181. static int __init slit_valid(struct acpi_table_slit *slit)
  182. {
  183. int i, j;
  184. int d = slit->locality_count;
  185. for (i = 0; i < d; i++) {
  186. for (j = 0; j < d; j++) {
  187. u8 val = slit->entry[d*i + j];
  188. if (i == j) {
  189. if (val != LOCAL_DISTANCE)
  190. return 0;
  191. } else if (val <= LOCAL_DISTANCE)
  192. return 0;
  193. }
  194. }
  195. return 1;
  196. }
  197. static int __init acpi_parse_slit(struct acpi_table_header *table)
  198. {
  199. struct acpi_table_slit *slit = (struct acpi_table_slit *)table;
  200. if (!slit_valid(slit)) {
  201. printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
  202. return -EINVAL;
  203. }
  204. acpi_numa_slit_init(slit);
  205. return 0;
  206. }
  207. void __init __weak
  208. acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
  209. {
  210. printk(KERN_WARNING PREFIX
  211. "Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
  212. return;
  213. }
  214. static int __init
  215. acpi_parse_x2apic_affinity(struct acpi_subtable_header *header,
  216. const unsigned long end)
  217. {
  218. struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
  219. processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
  220. if (!processor_affinity)
  221. return -EINVAL;
  222. acpi_table_print_srat_entry(header);
  223. /* let architecture-dependent part to do it */
  224. acpi_numa_x2apic_affinity_init(processor_affinity);
  225. return 0;
  226. }
  227. static int __init
  228. acpi_parse_processor_affinity(struct acpi_subtable_header *header,
  229. const unsigned long end)
  230. {
  231. struct acpi_srat_cpu_affinity *processor_affinity;
  232. processor_affinity = (struct acpi_srat_cpu_affinity *)header;
  233. if (!processor_affinity)
  234. return -EINVAL;
  235. acpi_table_print_srat_entry(header);
  236. /* let architecture-dependent part to do it */
  237. acpi_numa_processor_affinity_init(processor_affinity);
  238. return 0;
  239. }
  240. static int __initdata parsed_numa_memblks;
  241. static int __init
  242. acpi_parse_memory_affinity(struct acpi_subtable_header * header,
  243. const unsigned long end)
  244. {
  245. struct acpi_srat_mem_affinity *memory_affinity;
  246. memory_affinity = (struct acpi_srat_mem_affinity *)header;
  247. if (!memory_affinity)
  248. return -EINVAL;
  249. acpi_table_print_srat_entry(header);
  250. /* let architecture-dependent part to do it */
  251. if (!acpi_numa_memory_affinity_init(memory_affinity))
  252. parsed_numa_memblks++;
  253. return 0;
  254. }
  255. static int __init acpi_parse_srat(struct acpi_table_header *table)
  256. {
  257. struct acpi_table_srat *srat = (struct acpi_table_srat *)table;
  258. acpi_srat_revision = srat->header.revision;
  259. /* Real work done in acpi_table_parse_srat below. */
  260. return 0;
  261. }
  262. static int __init
  263. acpi_table_parse_srat(enum acpi_srat_type id,
  264. acpi_tbl_entry_handler handler, unsigned int max_entries)
  265. {
  266. return acpi_table_parse_entries(ACPI_SIG_SRAT,
  267. sizeof(struct acpi_table_srat), id,
  268. handler, max_entries);
  269. }
  270. int __init acpi_numa_init(void)
  271. {
  272. int cnt = 0;
  273. /*
  274. * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
  275. * SRAT cpu entries could have different order with that in MADT.
  276. * So go over all cpu entries in SRAT to get apicid to node mapping.
  277. */
  278. /* SRAT: Static Resource Affinity Table */
  279. if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
  280. acpi_table_parse_srat(ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY,
  281. acpi_parse_x2apic_affinity, 0);
  282. acpi_table_parse_srat(ACPI_SRAT_TYPE_CPU_AFFINITY,
  283. acpi_parse_processor_affinity, 0);
  284. cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
  285. acpi_parse_memory_affinity,
  286. NR_NODE_MEMBLKS);
  287. }
  288. /* SLIT: System Locality Information Table */
  289. acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
  290. acpi_numa_arch_fixup();
  291. if (cnt < 0)
  292. return cnt;
  293. else if (!parsed_numa_memblks)
  294. return -ENOENT;
  295. return 0;
  296. }
  297. static int acpi_get_pxm(acpi_handle h)
  298. {
  299. unsigned long long pxm;
  300. acpi_status status;
  301. acpi_handle handle;
  302. acpi_handle phandle = h;
  303. do {
  304. handle = phandle;
  305. status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
  306. if (ACPI_SUCCESS(status))
  307. return pxm;
  308. status = acpi_get_parent(handle, &phandle);
  309. } while (ACPI_SUCCESS(status));
  310. return -1;
  311. }
  312. int acpi_get_node(acpi_handle handle)
  313. {
  314. int pxm;
  315. pxm = acpi_get_pxm(handle);
  316. return acpi_map_pxm_to_node(pxm);
  317. }
  318. EXPORT_SYMBOL(acpi_get_node);