numa.c 8.1 KB

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