srat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * acpi_numa.c - ACPI NUMA support
  4. *
  5. * Copyright (C) 2002 Takayoshi Kochi <[email protected]>
  6. */
  7. #define pr_fmt(fmt) "ACPI: " fmt
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/errno.h>
  13. #include <linux/acpi.h>
  14. #include <linux/memblock.h>
  15. #include <linux/numa.h>
  16. #include <linux/nodemask.h>
  17. #include <linux/topology.h>
  18. static nodemask_t nodes_found_map = NODE_MASK_NONE;
  19. /* maps to convert between proximity domain and logical node ID */
  20. static int pxm_to_node_map[MAX_PXM_DOMAINS]
  21. = { [0 ... MAX_PXM_DOMAINS - 1] = NUMA_NO_NODE };
  22. static int node_to_pxm_map[MAX_NUMNODES]
  23. = { [0 ... MAX_NUMNODES - 1] = PXM_INVAL };
  24. unsigned char acpi_srat_revision __initdata;
  25. static int acpi_numa __initdata;
  26. void __init disable_srat(void)
  27. {
  28. acpi_numa = -1;
  29. }
  30. int pxm_to_node(int pxm)
  31. {
  32. if (pxm < 0 || pxm >= MAX_PXM_DOMAINS || numa_off)
  33. return NUMA_NO_NODE;
  34. return pxm_to_node_map[pxm];
  35. }
  36. EXPORT_SYMBOL(pxm_to_node);
  37. int node_to_pxm(int node)
  38. {
  39. if (node < 0)
  40. return PXM_INVAL;
  41. return node_to_pxm_map[node];
  42. }
  43. static void __acpi_map_pxm_to_node(int pxm, int node)
  44. {
  45. if (pxm_to_node_map[pxm] == NUMA_NO_NODE || node < pxm_to_node_map[pxm])
  46. pxm_to_node_map[pxm] = node;
  47. if (node_to_pxm_map[node] == PXM_INVAL || pxm < node_to_pxm_map[node])
  48. node_to_pxm_map[node] = pxm;
  49. }
  50. int acpi_map_pxm_to_node(int pxm)
  51. {
  52. int node;
  53. if (pxm < 0 || pxm >= MAX_PXM_DOMAINS || numa_off)
  54. return NUMA_NO_NODE;
  55. node = pxm_to_node_map[pxm];
  56. if (node == NUMA_NO_NODE) {
  57. if (nodes_weight(nodes_found_map) >= MAX_NUMNODES)
  58. return NUMA_NO_NODE;
  59. node = first_unset_node(nodes_found_map);
  60. __acpi_map_pxm_to_node(pxm, node);
  61. node_set(node, nodes_found_map);
  62. }
  63. return node;
  64. }
  65. EXPORT_SYMBOL(acpi_map_pxm_to_node);
  66. static void __init
  67. acpi_table_print_srat_entry(struct acpi_subtable_header *header)
  68. {
  69. switch (header->type) {
  70. case ACPI_SRAT_TYPE_CPU_AFFINITY:
  71. {
  72. struct acpi_srat_cpu_affinity *p =
  73. (struct acpi_srat_cpu_affinity *)header;
  74. pr_debug("SRAT Processor (id[0x%02x] eid[0x%02x]) in proximity domain %d %s\n",
  75. p->apic_id, p->local_sapic_eid,
  76. p->proximity_domain_lo,
  77. (p->flags & ACPI_SRAT_CPU_ENABLED) ?
  78. "enabled" : "disabled");
  79. }
  80. break;
  81. case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
  82. {
  83. struct acpi_srat_mem_affinity *p =
  84. (struct acpi_srat_mem_affinity *)header;
  85. pr_debug("SRAT Memory (0x%llx length 0x%llx) in proximity domain %d %s%s%s\n",
  86. (unsigned long long)p->base_address,
  87. (unsigned long long)p->length,
  88. p->proximity_domain,
  89. (p->flags & ACPI_SRAT_MEM_ENABLED) ?
  90. "enabled" : "disabled",
  91. (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) ?
  92. " hot-pluggable" : "",
  93. (p->flags & ACPI_SRAT_MEM_NON_VOLATILE) ?
  94. " non-volatile" : "");
  95. }
  96. break;
  97. case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
  98. {
  99. struct acpi_srat_x2apic_cpu_affinity *p =
  100. (struct acpi_srat_x2apic_cpu_affinity *)header;
  101. pr_debug("SRAT Processor (x2apicid[0x%08x]) in proximity domain %d %s\n",
  102. p->apic_id,
  103. p->proximity_domain,
  104. (p->flags & ACPI_SRAT_CPU_ENABLED) ?
  105. "enabled" : "disabled");
  106. }
  107. break;
  108. case ACPI_SRAT_TYPE_GICC_AFFINITY:
  109. {
  110. struct acpi_srat_gicc_affinity *p =
  111. (struct acpi_srat_gicc_affinity *)header;
  112. pr_debug("SRAT Processor (acpi id[0x%04x]) in proximity domain %d %s\n",
  113. p->acpi_processor_uid,
  114. p->proximity_domain,
  115. (p->flags & ACPI_SRAT_GICC_ENABLED) ?
  116. "enabled" : "disabled");
  117. }
  118. break;
  119. case ACPI_SRAT_TYPE_GENERIC_AFFINITY:
  120. {
  121. struct acpi_srat_generic_affinity *p =
  122. (struct acpi_srat_generic_affinity *)header;
  123. if (p->device_handle_type == 0) {
  124. /*
  125. * For pci devices this may be the only place they
  126. * are assigned a proximity domain
  127. */
  128. pr_debug("SRAT Generic Initiator(Seg:%u BDF:%u) in proximity domain %d %s\n",
  129. *(u16 *)(&p->device_handle[0]),
  130. *(u16 *)(&p->device_handle[2]),
  131. p->proximity_domain,
  132. (p->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED) ?
  133. "enabled" : "disabled");
  134. } else {
  135. /*
  136. * In this case we can rely on the device having a
  137. * proximity domain reference
  138. */
  139. pr_debug("SRAT Generic Initiator(HID=%.8s UID=%.4s) in proximity domain %d %s\n",
  140. (char *)(&p->device_handle[0]),
  141. (char *)(&p->device_handle[8]),
  142. p->proximity_domain,
  143. (p->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED) ?
  144. "enabled" : "disabled");
  145. }
  146. }
  147. break;
  148. default:
  149. pr_warn("Found unsupported SRAT entry (type = 0x%x)\n",
  150. header->type);
  151. break;
  152. }
  153. }
  154. /*
  155. * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
  156. * up the NUMA heuristics which wants the local node to have a smaller
  157. * distance than the others.
  158. * Do some quick checks here and only use the SLIT if it passes.
  159. */
  160. static int __init slit_valid(struct acpi_table_slit *slit)
  161. {
  162. int i, j;
  163. int d = slit->locality_count;
  164. for (i = 0; i < d; i++) {
  165. for (j = 0; j < d; j++) {
  166. u8 val = slit->entry[d*i + j];
  167. if (i == j) {
  168. if (val != LOCAL_DISTANCE)
  169. return 0;
  170. } else if (val <= LOCAL_DISTANCE)
  171. return 0;
  172. }
  173. }
  174. return 1;
  175. }
  176. void __init bad_srat(void)
  177. {
  178. pr_err("SRAT: SRAT not used.\n");
  179. disable_srat();
  180. }
  181. int __init srat_disabled(void)
  182. {
  183. return acpi_numa < 0;
  184. }
  185. #if defined(CONFIG_X86) || defined(CONFIG_ARM64) || defined(CONFIG_LOONGARCH)
  186. /*
  187. * Callback for SLIT parsing. pxm_to_node() returns NUMA_NO_NODE for
  188. * I/O localities since SRAT does not list them. I/O localities are
  189. * not supported at this point.
  190. */
  191. void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
  192. {
  193. int i, j;
  194. for (i = 0; i < slit->locality_count; i++) {
  195. const int from_node = pxm_to_node(i);
  196. if (from_node == NUMA_NO_NODE)
  197. continue;
  198. for (j = 0; j < slit->locality_count; j++) {
  199. const int to_node = pxm_to_node(j);
  200. if (to_node == NUMA_NO_NODE)
  201. continue;
  202. numa_set_distance(from_node, to_node,
  203. slit->entry[slit->locality_count * i + j]);
  204. }
  205. }
  206. }
  207. /*
  208. * Default callback for parsing of the Proximity Domain <-> Memory
  209. * Area mappings
  210. */
  211. int __init
  212. acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
  213. {
  214. u64 start, end;
  215. u32 hotpluggable;
  216. int node, pxm;
  217. if (srat_disabled())
  218. goto out_err;
  219. if (ma->header.length < sizeof(struct acpi_srat_mem_affinity)) {
  220. pr_err("SRAT: Unexpected header length: %d\n",
  221. ma->header.length);
  222. goto out_err_bad_srat;
  223. }
  224. if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
  225. goto out_err;
  226. hotpluggable = IS_ENABLED(CONFIG_MEMORY_HOTPLUG) &&
  227. (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE);
  228. start = ma->base_address;
  229. end = start + ma->length;
  230. pxm = ma->proximity_domain;
  231. if (acpi_srat_revision <= 1)
  232. pxm &= 0xff;
  233. node = acpi_map_pxm_to_node(pxm);
  234. if (node == NUMA_NO_NODE) {
  235. pr_err("SRAT: Too many proximity domains.\n");
  236. goto out_err_bad_srat;
  237. }
  238. if (numa_add_memblk(node, start, end) < 0) {
  239. pr_err("SRAT: Failed to add memblk to node %u [mem %#010Lx-%#010Lx]\n",
  240. node, (unsigned long long) start,
  241. (unsigned long long) end - 1);
  242. goto out_err_bad_srat;
  243. }
  244. node_set(node, numa_nodes_parsed);
  245. pr_info("SRAT: Node %u PXM %u [mem %#010Lx-%#010Lx]%s%s\n",
  246. node, pxm,
  247. (unsigned long long) start, (unsigned long long) end - 1,
  248. hotpluggable ? " hotplug" : "",
  249. ma->flags & ACPI_SRAT_MEM_NON_VOLATILE ? " non-volatile" : "");
  250. /* Mark hotplug range in memblock. */
  251. if (hotpluggable && memblock_mark_hotplug(start, ma->length))
  252. pr_warn("SRAT: Failed to mark hotplug range [mem %#010Lx-%#010Lx] in memblock\n",
  253. (unsigned long long)start, (unsigned long long)end - 1);
  254. max_possible_pfn = max(max_possible_pfn, PFN_UP(end - 1));
  255. return 0;
  256. out_err_bad_srat:
  257. bad_srat();
  258. out_err:
  259. return -EINVAL;
  260. }
  261. static int __init acpi_parse_cfmws(union acpi_subtable_headers *header,
  262. void *arg, const unsigned long table_end)
  263. {
  264. struct acpi_cedt_cfmws *cfmws;
  265. int *fake_pxm = arg;
  266. u64 start, end;
  267. int node;
  268. cfmws = (struct acpi_cedt_cfmws *)header;
  269. start = cfmws->base_hpa;
  270. end = cfmws->base_hpa + cfmws->window_size;
  271. /*
  272. * The SRAT may have already described NUMA details for all,
  273. * or a portion of, this CFMWS HPA range. Extend the memblks
  274. * found for any portion of the window to cover the entire
  275. * window.
  276. */
  277. if (!numa_fill_memblks(start, end))
  278. return 0;
  279. /* No SRAT description. Create a new node. */
  280. node = acpi_map_pxm_to_node(*fake_pxm);
  281. if (node == NUMA_NO_NODE) {
  282. pr_err("ACPI NUMA: Too many proximity domains while processing CFMWS.\n");
  283. return -EINVAL;
  284. }
  285. if (numa_add_memblk(node, start, end) < 0) {
  286. /* CXL driver must handle the NUMA_NO_NODE case */
  287. pr_warn("ACPI NUMA: Failed to add memblk for CFMWS node %d [mem %#llx-%#llx]\n",
  288. node, start, end);
  289. }
  290. node_set(node, numa_nodes_parsed);
  291. /* Set the next available fake_pxm value */
  292. (*fake_pxm)++;
  293. return 0;
  294. }
  295. #else
  296. static int __init acpi_parse_cfmws(union acpi_subtable_headers *header,
  297. void *arg, const unsigned long table_end)
  298. {
  299. return 0;
  300. }
  301. #endif /* defined(CONFIG_X86) || defined (CONFIG_ARM64) */
  302. static int __init acpi_parse_slit(struct acpi_table_header *table)
  303. {
  304. struct acpi_table_slit *slit = (struct acpi_table_slit *)table;
  305. if (!slit_valid(slit)) {
  306. pr_info("SLIT table looks invalid. Not used.\n");
  307. return -EINVAL;
  308. }
  309. acpi_numa_slit_init(slit);
  310. return 0;
  311. }
  312. void __init __weak
  313. acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
  314. {
  315. pr_warn("Found unsupported x2apic [0x%08x] SRAT entry\n", pa->apic_id);
  316. }
  317. static int __init
  318. acpi_parse_x2apic_affinity(union acpi_subtable_headers *header,
  319. const unsigned long end)
  320. {
  321. struct acpi_srat_x2apic_cpu_affinity *processor_affinity;
  322. processor_affinity = (struct acpi_srat_x2apic_cpu_affinity *)header;
  323. acpi_table_print_srat_entry(&header->common);
  324. /* let architecture-dependent part to do it */
  325. acpi_numa_x2apic_affinity_init(processor_affinity);
  326. return 0;
  327. }
  328. static int __init
  329. acpi_parse_processor_affinity(union acpi_subtable_headers *header,
  330. const unsigned long end)
  331. {
  332. struct acpi_srat_cpu_affinity *processor_affinity;
  333. processor_affinity = (struct acpi_srat_cpu_affinity *)header;
  334. acpi_table_print_srat_entry(&header->common);
  335. /* let architecture-dependent part to do it */
  336. acpi_numa_processor_affinity_init(processor_affinity);
  337. return 0;
  338. }
  339. static int __init
  340. acpi_parse_gicc_affinity(union acpi_subtable_headers *header,
  341. const unsigned long end)
  342. {
  343. struct acpi_srat_gicc_affinity *processor_affinity;
  344. processor_affinity = (struct acpi_srat_gicc_affinity *)header;
  345. acpi_table_print_srat_entry(&header->common);
  346. /* let architecture-dependent part to do it */
  347. acpi_numa_gicc_affinity_init(processor_affinity);
  348. return 0;
  349. }
  350. #if defined(CONFIG_X86) || defined(CONFIG_ARM64)
  351. static int __init
  352. acpi_parse_gi_affinity(union acpi_subtable_headers *header,
  353. const unsigned long end)
  354. {
  355. struct acpi_srat_generic_affinity *gi_affinity;
  356. int node;
  357. gi_affinity = (struct acpi_srat_generic_affinity *)header;
  358. if (!gi_affinity)
  359. return -EINVAL;
  360. acpi_table_print_srat_entry(&header->common);
  361. if (!(gi_affinity->flags & ACPI_SRAT_GENERIC_AFFINITY_ENABLED))
  362. return -EINVAL;
  363. node = acpi_map_pxm_to_node(gi_affinity->proximity_domain);
  364. if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
  365. pr_err("SRAT: Too many proximity domains.\n");
  366. return -EINVAL;
  367. }
  368. node_set(node, numa_nodes_parsed);
  369. node_set_state(node, N_GENERIC_INITIATOR);
  370. return 0;
  371. }
  372. #else
  373. static int __init
  374. acpi_parse_gi_affinity(union acpi_subtable_headers *header,
  375. const unsigned long end)
  376. {
  377. return 0;
  378. }
  379. #endif /* defined(CONFIG_X86) || defined (CONFIG_ARM64) */
  380. static int __initdata parsed_numa_memblks;
  381. static int __init
  382. acpi_parse_memory_affinity(union acpi_subtable_headers * header,
  383. const unsigned long end)
  384. {
  385. struct acpi_srat_mem_affinity *memory_affinity;
  386. memory_affinity = (struct acpi_srat_mem_affinity *)header;
  387. acpi_table_print_srat_entry(&header->common);
  388. /* let architecture-dependent part to do it */
  389. if (!acpi_numa_memory_affinity_init(memory_affinity))
  390. parsed_numa_memblks++;
  391. return 0;
  392. }
  393. static int __init acpi_parse_srat(struct acpi_table_header *table)
  394. {
  395. struct acpi_table_srat *srat = (struct acpi_table_srat *)table;
  396. acpi_srat_revision = srat->header.revision;
  397. /* Real work done in acpi_table_parse_srat below. */
  398. return 0;
  399. }
  400. static int __init
  401. acpi_table_parse_srat(enum acpi_srat_type id,
  402. acpi_tbl_entry_handler handler, unsigned int max_entries)
  403. {
  404. return acpi_table_parse_entries(ACPI_SIG_SRAT,
  405. sizeof(struct acpi_table_srat), id,
  406. handler, max_entries);
  407. }
  408. int __init acpi_numa_init(void)
  409. {
  410. int i, fake_pxm, cnt = 0;
  411. if (acpi_disabled)
  412. return -EINVAL;
  413. /*
  414. * Should not limit number with cpu num that is from NR_CPUS or nr_cpus=
  415. * SRAT cpu entries could have different order with that in MADT.
  416. * So go over all cpu entries in SRAT to get apicid to node mapping.
  417. */
  418. /* SRAT: System Resource Affinity Table */
  419. if (!acpi_table_parse(ACPI_SIG_SRAT, acpi_parse_srat)) {
  420. struct acpi_subtable_proc srat_proc[4];
  421. memset(srat_proc, 0, sizeof(srat_proc));
  422. srat_proc[0].id = ACPI_SRAT_TYPE_CPU_AFFINITY;
  423. srat_proc[0].handler = acpi_parse_processor_affinity;
  424. srat_proc[1].id = ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY;
  425. srat_proc[1].handler = acpi_parse_x2apic_affinity;
  426. srat_proc[2].id = ACPI_SRAT_TYPE_GICC_AFFINITY;
  427. srat_proc[2].handler = acpi_parse_gicc_affinity;
  428. srat_proc[3].id = ACPI_SRAT_TYPE_GENERIC_AFFINITY;
  429. srat_proc[3].handler = acpi_parse_gi_affinity;
  430. acpi_table_parse_entries_array(ACPI_SIG_SRAT,
  431. sizeof(struct acpi_table_srat),
  432. srat_proc, ARRAY_SIZE(srat_proc), 0);
  433. cnt = acpi_table_parse_srat(ACPI_SRAT_TYPE_MEMORY_AFFINITY,
  434. acpi_parse_memory_affinity, 0);
  435. }
  436. /* SLIT: System Locality Information Table */
  437. acpi_table_parse(ACPI_SIG_SLIT, acpi_parse_slit);
  438. /*
  439. * CXL Fixed Memory Window Structures (CFMWS) must be parsed
  440. * after the SRAT. Create NUMA Nodes for CXL memory ranges that
  441. * are defined in the CFMWS and not already defined in the SRAT.
  442. * Initialize a fake_pxm as the first available PXM to emulate.
  443. */
  444. /* fake_pxm is the next unused PXM value after SRAT parsing */
  445. for (i = 0, fake_pxm = -1; i < MAX_NUMNODES - 1; i++) {
  446. if (node_to_pxm_map[i] > fake_pxm)
  447. fake_pxm = node_to_pxm_map[i];
  448. }
  449. fake_pxm++;
  450. acpi_table_parse_cedt(ACPI_CEDT_TYPE_CFMWS, acpi_parse_cfmws,
  451. &fake_pxm);
  452. if (cnt < 0)
  453. return cnt;
  454. else if (!parsed_numa_memblks)
  455. return -ENOENT;
  456. return 0;
  457. }
  458. static int acpi_get_pxm(acpi_handle h)
  459. {
  460. unsigned long long pxm;
  461. acpi_status status;
  462. acpi_handle handle;
  463. acpi_handle phandle = h;
  464. do {
  465. handle = phandle;
  466. status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
  467. if (ACPI_SUCCESS(status))
  468. return pxm;
  469. status = acpi_get_parent(handle, &phandle);
  470. } while (ACPI_SUCCESS(status));
  471. return -1;
  472. }
  473. int acpi_get_node(acpi_handle handle)
  474. {
  475. int pxm;
  476. pxm = acpi_get_pxm(handle);
  477. return pxm_to_node(pxm);
  478. }
  479. EXPORT_SYMBOL(acpi_get_node);