numa.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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%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. (p->flags & ACPI_SRAT_MEM_NON_VOLATILE)?
  111. " non-volatile" : ""));
  112. }
  113. #endif /* ACPI_DEBUG_OUTPUT */
  114. break;
  115. case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
  116. #ifdef ACPI_DEBUG_OUTPUT
  117. {
  118. struct acpi_srat_x2apic_cpu_affinity *p =
  119. (struct acpi_srat_x2apic_cpu_affinity *)header;
  120. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  121. "SRAT Processor (x2apicid[0x%08x]) in"
  122. " proximity domain %d %s\n",
  123. p->apic_id,
  124. p->proximity_domain,
  125. (p->flags & ACPI_SRAT_CPU_ENABLED) ?
  126. "enabled" : "disabled"));
  127. }
  128. #endif /* ACPI_DEBUG_OUTPUT */
  129. break;
  130. default:
  131. printk(KERN_WARNING PREFIX
  132. "Found unsupported SRAT entry (type = 0x%x)\n",
  133. header->type);
  134. break;
  135. }
  136. }
  137. /*
  138. * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
  139. * up the NUMA heuristics which wants the local node to have a smaller
  140. * distance than the others.
  141. * Do some quick checks here and only use the SLIT if it passes.
  142. */
  143. static __init int slit_valid(struct acpi_table_slit *slit)
  144. {
  145. int i, j;
  146. int d = slit->locality_count;
  147. for (i = 0; i < d; i++) {
  148. for (j = 0; j < d; j++) {
  149. u8 val = slit->entry[d*i + j];
  150. if (i == j) {
  151. if (val != LOCAL_DISTANCE)
  152. return 0;
  153. } else if (val <= LOCAL_DISTANCE)
  154. return 0;
  155. }
  156. }
  157. return 1;
  158. }
  159. static int __init acpi_parse_slit(struct acpi_table_header *table)
  160. {
  161. struct acpi_table_slit *slit;
  162. if (!table)
  163. return -EINVAL;
  164. slit = (struct acpi_table_slit *)table;
  165. if (!slit_valid(slit)) {
  166. printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
  167. return -EINVAL;
  168. }
  169. acpi_numa_slit_init(slit);
  170. return 0;
  171. }
  172. void __init __attribute__ ((weak))
  173. acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
  174. {
  175. printk(KERN_WARNING PREFIX
  176. "Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
  177. return;
  178. }
  179. static int __init
  180. acpi_parse_x2apic_affinity(struct acpi_subtable_header *header,
  181. const unsigned long end)
  182. {
  183. struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
  184. processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
  185. if (!processor_affinity)
  186. return -EINVAL;
  187. acpi_table_print_srat_entry(header);
  188. /* let architecture-dependent part to do it */
  189. acpi_numa_x2apic_affinity_init(processor_affinity);
  190. return 0;
  191. }
  192. static int __init
  193. acpi_parse_processor_affinity(struct acpi_subtable_header *header,
  194. const unsigned long end)
  195. {
  196. struct acpi_srat_cpu_affinity *processor_affinity;
  197. processor_affinity = (struct acpi_srat_cpu_affinity *)header;
  198. if (!processor_affinity)
  199. return -EINVAL;
  200. acpi_table_print_srat_entry(header);
  201. /* let architecture-dependent part to do it */
  202. acpi_numa_processor_affinity_init(processor_affinity);
  203. return 0;
  204. }
  205. static int __initdata parsed_numa_memblks;
  206. static int __init
  207. acpi_parse_memory_affinity(struct acpi_subtable_header * header,
  208. const unsigned long end)
  209. {
  210. struct acpi_srat_mem_affinity *memory_affinity;
  211. memory_affinity = (struct acpi_srat_mem_affinity *)header;
  212. if (!memory_affinity)
  213. return -EINVAL;
  214. acpi_table_print_srat_entry(header);
  215. /* let architecture-dependent part to do it */
  216. if (!acpi_numa_memory_affinity_init(memory_affinity))
  217. parsed_numa_memblks++;
  218. return 0;
  219. }
  220. static int __init acpi_parse_srat(struct acpi_table_header *table)
  221. {
  222. struct acpi_table_srat *srat;
  223. if (!table)
  224. return -EINVAL;
  225. srat = (struct acpi_table_srat *)table;
  226. acpi_srat_revision = srat->header.revision;
  227. /* Real work done in acpi_table_parse_srat below. */
  228. return 0;
  229. }
  230. static int __init
  231. acpi_table_parse_srat(enum acpi_srat_type id,
  232. acpi_tbl_entry_handler handler, unsigned int max_entries)
  233. {
  234. return acpi_table_parse_entries(ACPI_SIG_SRAT,
  235. sizeof(struct acpi_table_srat), id,
  236. handler, max_entries);
  237. }
  238. int __init acpi_numa_init(void)
  239. {
  240. int cnt = 0;
  241. /*
  242. * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
  243. * SRAT cpu entries could have different order with that in MADT.
  244. * So go over all cpu entries in SRAT to get apicid to node mapping.
  245. */
  246. /* SRAT: Static Resource Affinity Table */
  247. if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
  248. acpi_table_parse_srat(ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY,
  249. acpi_parse_x2apic_affinity, 0);
  250. acpi_table_parse_srat(ACPI_SRAT_TYPE_CPU_AFFINITY,
  251. acpi_parse_processor_affinity, 0);
  252. cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
  253. acpi_parse_memory_affinity,
  254. NR_NODE_MEMBLKS);
  255. }
  256. /* SLIT: System Locality Information Table */
  257. acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
  258. acpi_numa_arch_fixup();
  259. if (cnt < 0)
  260. return cnt;
  261. else if (!parsed_numa_memblks)
  262. return -ENOENT;
  263. return 0;
  264. }
  265. int acpi_get_pxm(acpi_handle h)
  266. {
  267. unsigned long long pxm;
  268. acpi_status status;
  269. acpi_handle handle;
  270. acpi_handle phandle = h;
  271. do {
  272. handle = phandle;
  273. status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
  274. if (ACPI_SUCCESS(status))
  275. return pxm;
  276. status = acpi_get_parent(handle, &phandle);
  277. } while (ACPI_SUCCESS(status));
  278. return -1;
  279. }
  280. int acpi_get_node(acpi_handle *handle)
  281. {
  282. int pxm, node = -1;
  283. pxm = acpi_get_pxm(handle);
  284. if (pxm >= 0 && pxm < MAX_PXM_DOMAINS)
  285. node = acpi_map_pxm_to_node(pxm);
  286. return node;
  287. }
  288. EXPORT_SYMBOL(acpi_get_node);