numa.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/errno.h>
  26. #include <linux/acpi.h>
  27. #include <linux/numa.h>
  28. #include <linux/nodemask.h>
  29. #include <linux/topology.h>
  30. #define PREFIX "ACPI: "
  31. #define ACPI_NUMA 0x80000000
  32. #define _COMPONENT ACPI_NUMA
  33. ACPI_MODULE_NAME("numa");
  34. static nodemask_t nodes_found_map = NODE_MASK_NONE;
  35. /* maps to convert between proximity domain and logical node ID */
  36. static int pxm_to_node_map[MAX_PXM_DOMAINS]
  37. = { [0 ... MAX_PXM_DOMAINS - 1] = NUMA_NO_NODE };
  38. static int node_to_pxm_map[MAX_NUMNODES]
  39. = { [0 ... MAX_NUMNODES - 1] = PXM_INVAL };
  40. unsigned char acpi_srat_revision __initdata;
  41. int pxm_to_node(int pxm)
  42. {
  43. if (pxm < 0)
  44. return NUMA_NO_NODE;
  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. static void __acpi_map_pxm_to_node(int pxm, int node)
  54. {
  55. if (pxm_to_node_map[pxm] == NUMA_NO_NODE || node < pxm_to_node_map[pxm])
  56. pxm_to_node_map[pxm] = node;
  57. if (node_to_pxm_map[node] == PXM_INVAL || pxm < node_to_pxm_map[node])
  58. node_to_pxm_map[node] = pxm;
  59. }
  60. int acpi_map_pxm_to_node(int pxm)
  61. {
  62. int node;
  63. if (pxm < 0 || pxm >= MAX_PXM_DOMAINS)
  64. return NUMA_NO_NODE;
  65. node = pxm_to_node_map[pxm];
  66. if (node == NUMA_NO_NODE) {
  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. /**
  76. * acpi_map_pxm_to_online_node - Map proximity ID to online node
  77. * @pxm: ACPI proximity ID
  78. *
  79. * This is similar to acpi_map_pxm_to_node(), but always returns an online
  80. * node. When the mapped node from a given proximity ID is offline, it
  81. * looks up the node distance table and returns the nearest online node.
  82. *
  83. * ACPI device drivers, which are called after the NUMA initialization has
  84. * completed in the kernel, can call this interface to obtain their device
  85. * NUMA topology from ACPI tables. Such drivers do not have to deal with
  86. * offline nodes. A node may be offline when a device proximity ID is
  87. * unique, SRAT memory entry does not exist, or NUMA is disabled, ex.
  88. * "numa=off" on x86.
  89. */
  90. int acpi_map_pxm_to_online_node(int pxm)
  91. {
  92. int node, min_node;
  93. node = acpi_map_pxm_to_node(pxm);
  94. if (node == NUMA_NO_NODE)
  95. node = 0;
  96. min_node = node;
  97. if (!node_online(node)) {
  98. int min_dist = INT_MAX, dist, n;
  99. for_each_online_node(n) {
  100. dist = node_distance(node, n);
  101. if (dist < min_dist) {
  102. min_dist = dist;
  103. min_node = n;
  104. }
  105. }
  106. }
  107. return min_node;
  108. }
  109. EXPORT_SYMBOL(acpi_map_pxm_to_online_node);
  110. static void __init
  111. acpi_table_print_srat_entry(struct acpi_subtable_header *header)
  112. {
  113. ACPI_FUNCTION_NAME("acpi_table_print_srat_entry");
  114. if (!header)
  115. return;
  116. switch (header->type) {
  117. case ACPI_SRAT_TYPE_CPU_AFFINITY:
  118. #ifdef ACPI_DEBUG_OUTPUT
  119. {
  120. struct acpi_srat_cpu_affinity *p =
  121. (struct acpi_srat_cpu_affinity *)header;
  122. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  123. "SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
  124. p->apic_id, p->local_sapic_eid,
  125. p->proximity_domain_lo,
  126. (p->flags & ACPI_SRAT_CPU_ENABLED)?
  127. "enabled" : "disabled"));
  128. }
  129. #endif /* ACPI_DEBUG_OUTPUT */
  130. break;
  131. case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
  132. #ifdef ACPI_DEBUG_OUTPUT
  133. {
  134. struct acpi_srat_mem_affinity *p =
  135. (struct acpi_srat_mem_affinity *)header;
  136. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  137. "SRAT Memory (0x%lx length 0x%lx) in proximity domain %d %s%s%s\n",
  138. (unsigned long)p->base_address,
  139. (unsigned long)p->length,
  140. p->proximity_domain,
  141. (p->flags & ACPI_SRAT_MEM_ENABLED)?
  142. "enabled" : "disabled",
  143. (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)?
  144. " hot-pluggable" : "",
  145. (p->flags & ACPI_SRAT_MEM_NON_VOLATILE)?
  146. " non-volatile" : ""));
  147. }
  148. #endif /* ACPI_DEBUG_OUTPUT */
  149. break;
  150. case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
  151. #ifdef ACPI_DEBUG_OUTPUT
  152. {
  153. struct acpi_srat_x2apic_cpu_affinity *p =
  154. (struct acpi_srat_x2apic_cpu_affinity *)header;
  155. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  156. "SRAT Processor (x2apicid[0x%08x]) in"
  157. " proximity domain %d %s\n",
  158. p->apic_id,
  159. p->proximity_domain,
  160. (p->flags & ACPI_SRAT_CPU_ENABLED) ?
  161. "enabled" : "disabled"));
  162. }
  163. #endif /* ACPI_DEBUG_OUTPUT */
  164. break;
  165. default:
  166. printk(KERN_WARNING PREFIX
  167. "Found unsupported SRAT entry (type = 0x%x)\n",
  168. header->type);
  169. break;
  170. }
  171. }
  172. /*
  173. * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
  174. * up the NUMA heuristics which wants the local node to have a smaller
  175. * distance than the others.
  176. * Do some quick checks here and only use the SLIT if it passes.
  177. */
  178. static int __init slit_valid(struct acpi_table_slit *slit)
  179. {
  180. int i, j;
  181. int d = slit->locality_count;
  182. for (i = 0; i < d; i++) {
  183. for (j = 0; j < d; j++) {
  184. u8 val = slit->entry[d*i + j];
  185. if (i == j) {
  186. if (val != LOCAL_DISTANCE)
  187. return 0;
  188. } else if (val <= LOCAL_DISTANCE)
  189. return 0;
  190. }
  191. }
  192. return 1;
  193. }
  194. static int __init acpi_parse_slit(struct acpi_table_header *table)
  195. {
  196. struct acpi_table_slit *slit = (struct acpi_table_slit *)table;
  197. if (!slit_valid(slit)) {
  198. printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
  199. return -EINVAL;
  200. }
  201. acpi_numa_slit_init(slit);
  202. return 0;
  203. }
  204. void __init __weak
  205. acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
  206. {
  207. printk(KERN_WARNING PREFIX
  208. "Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
  209. return;
  210. }
  211. static int __init
  212. acpi_parse_x2apic_affinity(struct acpi_subtable_header *header,
  213. const unsigned long end)
  214. {
  215. struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
  216. processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
  217. if (!processor_affinity)
  218. return -EINVAL;
  219. acpi_table_print_srat_entry(header);
  220. /* let architecture-dependent part to do it */
  221. acpi_numa_x2apic_affinity_init(processor_affinity);
  222. return 0;
  223. }
  224. static int __init
  225. acpi_parse_processor_affinity(struct acpi_subtable_header *header,
  226. const unsigned long end)
  227. {
  228. struct acpi_srat_cpu_affinity *processor_affinity;
  229. processor_affinity = (struct acpi_srat_cpu_affinity *)header;
  230. if (!processor_affinity)
  231. return -EINVAL;
  232. acpi_table_print_srat_entry(header);
  233. /* let architecture-dependent part to do it */
  234. acpi_numa_processor_affinity_init(processor_affinity);
  235. return 0;
  236. }
  237. static int __initdata parsed_numa_memblks;
  238. static int __init
  239. acpi_parse_memory_affinity(struct acpi_subtable_header * header,
  240. const unsigned long end)
  241. {
  242. struct acpi_srat_mem_affinity *memory_affinity;
  243. memory_affinity = (struct acpi_srat_mem_affinity *)header;
  244. if (!memory_affinity)
  245. return -EINVAL;
  246. acpi_table_print_srat_entry(header);
  247. /* let architecture-dependent part to do it */
  248. if (!acpi_numa_memory_affinity_init(memory_affinity))
  249. parsed_numa_memblks++;
  250. return 0;
  251. }
  252. static int __init acpi_parse_srat(struct acpi_table_header *table)
  253. {
  254. struct acpi_table_srat *srat = (struct acpi_table_srat *)table;
  255. acpi_srat_revision = srat->header.revision;
  256. /* Real work done in acpi_table_parse_srat below. */
  257. return 0;
  258. }
  259. static int __init
  260. acpi_table_parse_srat(enum acpi_srat_type id,
  261. acpi_tbl_entry_handler handler, unsigned int max_entries)
  262. {
  263. return acpi_table_parse_entries(ACPI_SIG_SRAT,
  264. sizeof(struct acpi_table_srat), id,
  265. handler, max_entries);
  266. }
  267. int __init acpi_numa_init(void)
  268. {
  269. int cnt = 0;
  270. /*
  271. * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
  272. * SRAT cpu entries could have different order with that in MADT.
  273. * So go over all cpu entries in SRAT to get apicid to node mapping.
  274. */
  275. /* SRAT: Static Resource Affinity Table */
  276. if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
  277. struct acpi_subtable_proc srat_proc[2];
  278. memset(srat_proc, 0, sizeof(srat_proc));
  279. srat_proc[0].id = ACPI_SRAT_TYPE_CPU_AFFINITY;
  280. srat_proc[0].handler = acpi_parse_processor_affinity;
  281. srat_proc[1].id = ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY;
  282. srat_proc[1].handler = acpi_parse_x2apic_affinity;
  283. acpi_table_parse_entries_array(ACPI_SIG_SRAT,
  284. sizeof(struct acpi_table_srat),
  285. srat_proc, ARRAY_SIZE(srat_proc), 0);
  286. cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
  287. acpi_parse_memory_affinity,
  288. NR_NODE_MEMBLKS);
  289. }
  290. /* SLIT: System Locality Information Table */
  291. acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
  292. acpi_numa_arch_fixup();
  293. if (cnt < 0)
  294. return cnt;
  295. else if (!parsed_numa_memblks)
  296. return -ENOENT;
  297. return 0;
  298. }
  299. static int acpi_get_pxm(acpi_handle h)
  300. {
  301. unsigned long long pxm;
  302. acpi_status status;
  303. acpi_handle handle;
  304. acpi_handle phandle = h;
  305. do {
  306. handle = phandle;
  307. status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
  308. if (ACPI_SUCCESS(status))
  309. return pxm;
  310. status = acpi_get_parent(handle, &phandle);
  311. } while (ACPI_SUCCESS(status));
  312. return -1;
  313. }
  314. int acpi_get_node(acpi_handle handle)
  315. {
  316. int pxm;
  317. pxm = acpi_get_pxm(handle);
  318. return acpi_map_pxm_to_node(pxm);
  319. }
  320. EXPORT_SYMBOL(acpi_get_node);