numa.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. #define pr_fmt(fmt) "ACPI: " fmt
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/types.h>
  26. #include <linux/errno.h>
  27. #include <linux/acpi.h>
  28. #include <linux/bootmem.h>
  29. #include <linux/memblock.h>
  30. #include <linux/numa.h>
  31. #include <linux/nodemask.h>
  32. #include <linux/topology.h>
  33. static nodemask_t nodes_found_map = NODE_MASK_NONE;
  34. /* maps to convert between proximity domain and logical node ID */
  35. static int pxm_to_node_map[MAX_PXM_DOMAINS]
  36. = { [0 ... MAX_PXM_DOMAINS - 1] = NUMA_NO_NODE };
  37. static int node_to_pxm_map[MAX_NUMNODES]
  38. = { [0 ... MAX_NUMNODES - 1] = PXM_INVAL };
  39. unsigned char acpi_srat_revision __initdata;
  40. int acpi_numa __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. switch (header->type) {
  113. case ACPI_SRAT_TYPE_CPU_AFFINITY:
  114. {
  115. struct acpi_srat_cpu_affinity *p =
  116. (struct acpi_srat_cpu_affinity *)header;
  117. pr_debug("SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
  118. p->apic_id, p->local_sapic_eid,
  119. p->proximity_domain_lo,
  120. (p->flags & ACPI_SRAT_CPU_ENABLED) ?
  121. "enabled" : "disabled");
  122. }
  123. break;
  124. case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
  125. {
  126. struct acpi_srat_mem_affinity *p =
  127. (struct acpi_srat_mem_affinity *)header;
  128. pr_debug("SRAT Memory (0x%lx length 0x%lx) in proximity domain %d %s%s%s\n",
  129. (unsigned long)p->base_address,
  130. (unsigned long)p->length,
  131. p->proximity_domain,
  132. (p->flags & ACPI_SRAT_MEM_ENABLED) ?
  133. "enabled" : "disabled",
  134. (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) ?
  135. " hot-pluggable" : "",
  136. (p->flags & ACPI_SRAT_MEM_NON_VOLATILE) ?
  137. " non-volatile" : "");
  138. }
  139. break;
  140. case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
  141. {
  142. struct acpi_srat_x2apic_cpu_affinity *p =
  143. (struct acpi_srat_x2apic_cpu_affinity *)header;
  144. pr_debug("SRAT Processor (x2apicid[0x%08x]) in proximity domain %d %s\n",
  145. p->apic_id,
  146. p->proximity_domain,
  147. (p->flags & ACPI_SRAT_CPU_ENABLED) ?
  148. "enabled" : "disabled");
  149. }
  150. break;
  151. case ACPI_SRAT_TYPE_GICC_AFFINITY:
  152. {
  153. struct acpi_srat_gicc_affinity *p =
  154. (struct acpi_srat_gicc_affinity *)header;
  155. pr_debug("SRAT Processor (acpi id[0x%04x]) in proximity domain %d %s\n",
  156. p->acpi_processor_uid,
  157. p->proximity_domain,
  158. (p->flags & ACPI_SRAT_GICC_ENABLED) ?
  159. "enabled" : "disabled");
  160. }
  161. break;
  162. default:
  163. pr_warn("Found unsupported SRAT entry (type = 0x%x)\n",
  164. header->type);
  165. break;
  166. }
  167. }
  168. /*
  169. * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
  170. * up the NUMA heuristics which wants the local node to have a smaller
  171. * distance than the others.
  172. * Do some quick checks here and only use the SLIT if it passes.
  173. */
  174. static int __init slit_valid(struct acpi_table_slit *slit)
  175. {
  176. int i, j;
  177. int d = slit->locality_count;
  178. for (i = 0; i < d; i++) {
  179. for (j = 0; j < d; j++) {
  180. u8 val = slit->entry[d*i + j];
  181. if (i == j) {
  182. if (val != LOCAL_DISTANCE)
  183. return 0;
  184. } else if (val <= LOCAL_DISTANCE)
  185. return 0;
  186. }
  187. }
  188. return 1;
  189. }
  190. void __init bad_srat(void)
  191. {
  192. pr_err("SRAT: SRAT not used.\n");
  193. acpi_numa = -1;
  194. }
  195. int __init srat_disabled(void)
  196. {
  197. return acpi_numa < 0;
  198. }
  199. #if defined(CONFIG_X86) || defined(CONFIG_ARM64)
  200. /*
  201. * Callback for SLIT parsing. pxm_to_node() returns NUMA_NO_NODE for
  202. * I/O localities since SRAT does not list them. I/O localities are
  203. * not supported at this point.
  204. */
  205. void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
  206. {
  207. int i, j;
  208. for (i = 0; i < slit->locality_count; i++) {
  209. const int from_node = pxm_to_node(i);
  210. if (from_node == NUMA_NO_NODE)
  211. continue;
  212. for (j = 0; j < slit->locality_count; j++) {
  213. const int to_node = pxm_to_node(j);
  214. if (to_node == NUMA_NO_NODE)
  215. continue;
  216. numa_set_distance(from_node, to_node,
  217. slit->entry[slit->locality_count * i + j]);
  218. }
  219. }
  220. }
  221. /*
  222. * Default callback for parsing of the Proximity Domain <-> Memory
  223. * Area mappings
  224. */
  225. int __init
  226. acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
  227. {
  228. u64 start, end;
  229. u32 hotpluggable;
  230. int node, pxm;
  231. if (srat_disabled())
  232. goto out_err;
  233. if (ma->header.length < sizeof(struct acpi_srat_mem_affinity)) {
  234. pr_err("SRAT: Unexpected header length: %d\n",
  235. ma->header.length);
  236. goto out_err_bad_srat;
  237. }
  238. if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
  239. goto out_err;
  240. hotpluggable = ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE;
  241. if (hotpluggable && !IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
  242. goto out_err;
  243. start = ma->base_address;
  244. end = start + ma->length;
  245. pxm = ma->proximity_domain;
  246. if (acpi_srat_revision <= 1)
  247. pxm &= 0xff;
  248. node = acpi_map_pxm_to_node(pxm);
  249. if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
  250. pr_err("SRAT: Too many proximity domains.\n");
  251. goto out_err_bad_srat;
  252. }
  253. if (numa_add_memblk(node, start, end) < 0) {
  254. pr_err("SRAT: Failed to add memblk to node %u [mem %#010Lx-%#010Lx]\n",
  255. node, (unsigned long long) start,
  256. (unsigned long long) end - 1);
  257. goto out_err_bad_srat;
  258. }
  259. node_set(node, numa_nodes_parsed);
  260. pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
  261. node, pxm,
  262. (unsigned long long) start, (unsigned long long) end - 1,
  263. hotpluggable ? " hotplug" : "",
  264. ma->flags & ACPI_SRAT_MEM_NON_VOLATILE ? " non-volatile" : "");
  265. /* Mark hotplug range in memblock. */
  266. if (hotpluggable && memblock_mark_hotplug(start, ma->length))
  267. pr_warn("SRAT: Failed to mark hotplug range [mem %#010Lx-%#010Lx] in memblock\n",
  268. (unsigned long long)start, (unsigned long long)end - 1);
  269. max_possible_pfn = max(max_possible_pfn, PFN_UP(end - 1));
  270. return 0;
  271. out_err_bad_srat:
  272. bad_srat();
  273. out_err:
  274. return -EINVAL;
  275. }
  276. #endif /* defined(CONFIG_X86) || defined (CONFIG_ARM64) */
  277. static int __init acpi_parse_slit(struct acpi_table_header *table)
  278. {
  279. struct acpi_table_slit *slit = (struct acpi_table_slit *)table;
  280. if (!slit_valid(slit)) {
  281. pr_info("SLIT table looks invalid. Not used.\n");
  282. return -EINVAL;
  283. }
  284. acpi_numa_slit_init(slit);
  285. return 0;
  286. }
  287. void __init __weak
  288. acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
  289. {
  290. pr_warn("Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
  291. }
  292. static int __init
  293. acpi_parse_x2apic_affinity(struct acpi_subtable_header *header,
  294. const unsigned long end)
  295. {
  296. struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
  297. processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
  298. if (!processor_affinity)
  299. return -EINVAL;
  300. acpi_table_print_srat_entry(header);
  301. /* let architecture-dependent part to do it */
  302. acpi_numa_x2apic_affinity_init(processor_affinity);
  303. return 0;
  304. }
  305. static int __init
  306. acpi_parse_processor_affinity(struct acpi_subtable_header *header,
  307. const unsigned long end)
  308. {
  309. struct acpi_srat_cpu_affinity *processor_affinity;
  310. processor_affinity = (struct acpi_srat_cpu_affinity *)header;
  311. if (!processor_affinity)
  312. return -EINVAL;
  313. acpi_table_print_srat_entry(header);
  314. /* let architecture-dependent part to do it */
  315. acpi_numa_processor_affinity_init(processor_affinity);
  316. return 0;
  317. }
  318. static int __init
  319. acpi_parse_gicc_affinity(struct acpi_subtable_header *header,
  320. const unsigned long end)
  321. {
  322. struct acpi_srat_gicc_affinity *processor_affinity;
  323. processor_affinity = (struct acpi_srat_gicc_affinity *)header;
  324. if (!processor_affinity)
  325. return -EINVAL;
  326. acpi_table_print_srat_entry(header);
  327. /* let architecture-dependent part to do it */
  328. acpi_numa_gicc_affinity_init(processor_affinity);
  329. return 0;
  330. }
  331. static int __initdata parsed_numa_memblks;
  332. static int __init
  333. acpi_parse_memory_affinity(struct acpi_subtable_header * header,
  334. const unsigned long end)
  335. {
  336. struct acpi_srat_mem_affinity *memory_affinity;
  337. memory_affinity = (struct acpi_srat_mem_affinity *)header;
  338. if (!memory_affinity)
  339. return -EINVAL;
  340. acpi_table_print_srat_entry(header);
  341. /* let architecture-dependent part to do it */
  342. if (!acpi_numa_memory_affinity_init(memory_affinity))
  343. parsed_numa_memblks++;
  344. return 0;
  345. }
  346. static int __init acpi_parse_srat(struct acpi_table_header *table)
  347. {
  348. struct acpi_table_srat *srat = (struct acpi_table_srat *)table;
  349. acpi_srat_revision = srat->header.revision;
  350. /* Real work done in acpi_table_parse_srat below. */
  351. return 0;
  352. }
  353. static int __init
  354. acpi_table_parse_srat(enum acpi_srat_type id,
  355. acpi_tbl_entry_handler handler, unsigned int max_entries)
  356. {
  357. return acpi_table_parse_entries(ACPI_SIG_SRAT,
  358. sizeof(struct acpi_table_srat), id,
  359. handler, max_entries);
  360. }
  361. int __init acpi_numa_init(void)
  362. {
  363. int cnt = 0;
  364. if (acpi_disabled)
  365. return -EINVAL;
  366. /*
  367. * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
  368. * SRAT cpu entries could have different order with that in MADT.
  369. * So go over all cpu entries in SRAT to get apicid to node mapping.
  370. */
  371. /* SRAT: Static Resource Affinity Table */
  372. if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
  373. struct acpi_subtable_proc srat_proc[3];
  374. memset(srat_proc, 0, sizeof(srat_proc));
  375. srat_proc[0].id = ACPI_SRAT_TYPE_CPU_AFFINITY;
  376. srat_proc[0].handler = acpi_parse_processor_affinity;
  377. srat_proc[1].id = ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY;
  378. srat_proc[1].handler = acpi_parse_x2apic_affinity;
  379. srat_proc[2].id = ACPI_SRAT_TYPE_GICC_AFFINITY;
  380. srat_proc[2].handler = acpi_parse_gicc_affinity;
  381. acpi_table_parse_entries_array(ACPI_SIG_SRAT,
  382. sizeof(struct acpi_table_srat),
  383. srat_proc, ARRAY_SIZE(srat_proc), 0);
  384. cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
  385. acpi_parse_memory_affinity,
  386. NR_NODE_MEMBLKS);
  387. }
  388. /* SLIT: System Locality Information Table */
  389. acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
  390. if (cnt < 0)
  391. return cnt;
  392. else if (!parsed_numa_memblks)
  393. return -ENOENT;
  394. return 0;
  395. }
  396. static int acpi_get_pxm(acpi_handle h)
  397. {
  398. unsigned long long pxm;
  399. acpi_status status;
  400. acpi_handle handle;
  401. acpi_handle phandle = h;
  402. do {
  403. handle = phandle;
  404. status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
  405. if (ACPI_SUCCESS(status))
  406. return pxm;
  407. status = acpi_get_parent(handle, &phandle);
  408. } while (ACPI_SUCCESS(status));
  409. return -1;
  410. }
  411. int acpi_get_node(acpi_handle handle)
  412. {
  413. int pxm;
  414. pxm = acpi_get_pxm(handle);
  415. return acpi_map_pxm_to_node(pxm);
  416. }
  417. EXPORT_SYMBOL(acpi_get_node);