numa.c 9.5 KB

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