numa.c 6.9 KB

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