numa.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <acpi/acpi_bus.h>
  33. #define PREFIX "ACPI: "
  34. #define ACPI_NUMA 0x80000000
  35. #define _COMPONENT ACPI_NUMA
  36. ACPI_MODULE_NAME("numa");
  37. static nodemask_t nodes_found_map = NODE_MASK_NONE;
  38. /* maps to convert between proximity domain and logical node ID */
  39. static int pxm_to_node_map[MAX_PXM_DOMAINS]
  40. = { [0 ... MAX_PXM_DOMAINS - 1] = NUMA_NO_NODE };
  41. static int node_to_pxm_map[MAX_NUMNODES]
  42. = { [0 ... MAX_NUMNODES - 1] = PXM_INVAL };
  43. int pxm_to_node(int pxm)
  44. {
  45. if (pxm < 0)
  46. return NUMA_NO_NODE;
  47. return pxm_to_node_map[pxm];
  48. }
  49. int node_to_pxm(int node)
  50. {
  51. if (node < 0)
  52. return PXM_INVAL;
  53. return node_to_pxm_map[node];
  54. }
  55. void __acpi_map_pxm_to_node(int pxm, int node)
  56. {
  57. pxm_to_node_map[pxm] = node;
  58. node_to_pxm_map[node] = pxm;
  59. }
  60. int acpi_map_pxm_to_node(int pxm)
  61. {
  62. int node = pxm_to_node_map[pxm];
  63. if (node < 0) {
  64. if (nodes_weight(nodes_found_map) >= MAX_NUMNODES)
  65. return NUMA_NO_NODE;
  66. node = first_unset_node(nodes_found_map);
  67. __acpi_map_pxm_to_node(pxm, node);
  68. node_set(node, nodes_found_map);
  69. }
  70. return node;
  71. }
  72. static void __init
  73. acpi_table_print_srat_entry(struct acpi_subtable_header *header)
  74. {
  75. ACPI_FUNCTION_NAME("acpi_table_print_srat_entry");
  76. if (!header)
  77. return;
  78. switch (header->type) {
  79. case ACPI_SRAT_TYPE_CPU_AFFINITY:
  80. #ifdef ACPI_DEBUG_OUTPUT
  81. {
  82. struct acpi_srat_cpu_affinity *p =
  83. (struct acpi_srat_cpu_affinity *)header;
  84. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  85. "SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
  86. p->apic_id, p->local_sapic_eid,
  87. p->proximity_domain_lo,
  88. (p->flags & ACPI_SRAT_CPU_ENABLED)?
  89. "enabled" : "disabled"));
  90. }
  91. #endif /* ACPI_DEBUG_OUTPUT */
  92. break;
  93. case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
  94. #ifdef ACPI_DEBUG_OUTPUT
  95. {
  96. struct acpi_srat_mem_affinity *p =
  97. (struct acpi_srat_mem_affinity *)header;
  98. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  99. "SRAT Memory (0x%lx length 0x%lx) in proximity domain %d %s%s\n",
  100. (unsigned long)p->base_address,
  101. (unsigned long)p->length,
  102. p->proximity_domain,
  103. (p->flags & ACPI_SRAT_MEM_ENABLED)?
  104. "enabled" : "disabled",
  105. (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)?
  106. " hot-pluggable" : ""));
  107. }
  108. #endif /* ACPI_DEBUG_OUTPUT */
  109. break;
  110. case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
  111. #ifdef ACPI_DEBUG_OUTPUT
  112. {
  113. struct acpi_srat_x2apic_cpu_affinity *p =
  114. (struct acpi_srat_x2apic_cpu_affinity *)header;
  115. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  116. "SRAT Processor (x2apicid[0x%08x]) in"
  117. " proximity domain %d %s\n",
  118. p->apic_id,
  119. p->proximity_domain,
  120. (p->flags & ACPI_SRAT_CPU_ENABLED) ?
  121. "enabled" : "disabled"));
  122. }
  123. #endif /* ACPI_DEBUG_OUTPUT */
  124. break;
  125. default:
  126. printk(KERN_WARNING PREFIX
  127. "Found unsupported SRAT entry (type = 0x%x)\n",
  128. header->type);
  129. break;
  130. }
  131. }
  132. /*
  133. * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
  134. * up the NUMA heuristics which wants the local node to have a smaller
  135. * distance than the others.
  136. * Do some quick checks here and only use the SLIT if it passes.
  137. */
  138. static __init int slit_valid(struct acpi_table_slit *slit)
  139. {
  140. int i, j;
  141. int d = slit->locality_count;
  142. for (i = 0; i < d; i++) {
  143. for (j = 0; j < d; j++) {
  144. u8 val = slit->entry[d*i + j];
  145. if (i == j) {
  146. if (val != LOCAL_DISTANCE)
  147. return 0;
  148. } else if (val <= LOCAL_DISTANCE)
  149. return 0;
  150. }
  151. }
  152. return 1;
  153. }
  154. static int __init acpi_parse_slit(struct acpi_table_header *table)
  155. {
  156. struct acpi_table_slit *slit;
  157. if (!table)
  158. return -EINVAL;
  159. slit = (struct acpi_table_slit *)table;
  160. if (!slit_valid(slit)) {
  161. printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
  162. return -EINVAL;
  163. }
  164. acpi_numa_slit_init(slit);
  165. return 0;
  166. }
  167. void __init __attribute__ ((weak))
  168. acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
  169. {
  170. printk(KERN_WARNING PREFIX
  171. "Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
  172. return;
  173. }
  174. static int __init
  175. acpi_parse_x2apic_affinity(struct acpi_subtable_header *header,
  176. const unsigned long end)
  177. {
  178. struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
  179. processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
  180. if (!processor_affinity)
  181. return -EINVAL;
  182. acpi_table_print_srat_entry(header);
  183. /* let architecture-dependent part to do it */
  184. acpi_numa_x2apic_affinity_init(processor_affinity);
  185. return 0;
  186. }
  187. static int __init
  188. acpi_parse_processor_affinity(struct acpi_subtable_header *header,
  189. const unsigned long end)
  190. {
  191. struct acpi_srat_cpu_affinity *processor_affinity;
  192. processor_affinity = (struct acpi_srat_cpu_affinity *)header;
  193. if (!processor_affinity)
  194. return -EINVAL;
  195. acpi_table_print_srat_entry(header);
  196. /* let architecture-dependent part to do it */
  197. acpi_numa_processor_affinity_init(processor_affinity);
  198. return 0;
  199. }
  200. static int __init
  201. acpi_parse_memory_affinity(struct acpi_subtable_header * header,
  202. const unsigned long end)
  203. {
  204. struct acpi_srat_mem_affinity *memory_affinity;
  205. memory_affinity = (struct acpi_srat_mem_affinity *)header;
  206. if (!memory_affinity)
  207. return -EINVAL;
  208. acpi_table_print_srat_entry(header);
  209. /* let architecture-dependent part to do it */
  210. acpi_numa_memory_affinity_init(memory_affinity);
  211. return 0;
  212. }
  213. static int __init acpi_parse_srat(struct acpi_table_header *table)
  214. {
  215. struct acpi_table_srat *srat;
  216. if (!table)
  217. return -EINVAL;
  218. srat = (struct acpi_table_srat *)table;
  219. return 0;
  220. }
  221. static int __init
  222. acpi_table_parse_srat(enum acpi_srat_type id,
  223. acpi_table_entry_handler handler, unsigned int max_entries)
  224. {
  225. return acpi_table_parse_entries(ACPI_SIG_SRAT,
  226. sizeof(struct acpi_table_srat), id,
  227. handler, max_entries);
  228. }
  229. int __init acpi_numa_init(void)
  230. {
  231. int ret = 0;
  232. /* SRAT: Static Resource Affinity Table */
  233. if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
  234. acpi_table_parse_srat(ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY,
  235. acpi_parse_x2apic_affinity, NR_CPUS);
  236. acpi_table_parse_srat(ACPI_SRAT_TYPE_CPU_AFFINITY,
  237. acpi_parse_processor_affinity, NR_CPUS);
  238. ret = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
  239. acpi_parse_memory_affinity,
  240. NR_NODE_MEMBLKS);
  241. }
  242. /* SLIT: System Locality Information Table */
  243. acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
  244. acpi_numa_arch_fixup();
  245. return ret;
  246. }
  247. int acpi_get_pxm(acpi_handle h)
  248. {
  249. unsigned long long pxm;
  250. acpi_status status;
  251. acpi_handle handle;
  252. acpi_handle phandle = h;
  253. do {
  254. handle = phandle;
  255. status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
  256. if (ACPI_SUCCESS(status))
  257. return pxm;
  258. status = acpi_get_parent(handle, &phandle);
  259. } while (ACPI_SUCCESS(status));
  260. return -1;
  261. }
  262. int acpi_get_node(acpi_handle *handle)
  263. {
  264. int pxm, node = -1;
  265. pxm = acpi_get_pxm(handle);
  266. if (pxm >= 0 && pxm < MAX_PXM_DOMAINS)
  267. node = acpi_map_pxm_to_node(pxm);
  268. return node;
  269. }
  270. EXPORT_SYMBOL(acpi_get_node);