numa.c 8.4 KB

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