numa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Common code for 32 and 64-bit NUMA */
  3. #include <linux/acpi.h>
  4. #include <linux/kernel.h>
  5. #include <linux/mm.h>
  6. #include <linux/of.h>
  7. #include <linux/string.h>
  8. #include <linux/init.h>
  9. #include <linux/memblock.h>
  10. #include <linux/mmzone.h>
  11. #include <linux/ctype.h>
  12. #include <linux/nodemask.h>
  13. #include <linux/sched.h>
  14. #include <linux/topology.h>
  15. #include <linux/sort.h>
  16. #include <linux/numa_memblks.h>
  17. #include <asm/e820/api.h>
  18. #include <asm/proto.h>
  19. #include <asm/dma.h>
  20. #include <asm/amd_nb.h>
  21. #include "numa_internal.h"
  22. int numa_off;
  23. static __init int numa_setup(char *opt)
  24. {
  25. if (!opt)
  26. return -EINVAL;
  27. if (!strncmp(opt, "off", 3))
  28. numa_off = 1;
  29. if (!strncmp(opt, "fake=", 5))
  30. return numa_emu_cmdline(opt + 5);
  31. if (!strncmp(opt, "noacpi", 6))
  32. disable_srat();
  33. if (!strncmp(opt, "nohmat", 6))
  34. disable_hmat();
  35. return 0;
  36. }
  37. early_param("numa", numa_setup);
  38. /*
  39. * apicid, cpu, node mappings
  40. */
  41. s16 __apicid_to_node[MAX_LOCAL_APIC] = {
  42. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  43. };
  44. int numa_cpu_node(int cpu)
  45. {
  46. u32 apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
  47. if (apicid != BAD_APICID)
  48. return __apicid_to_node[apicid];
  49. return NUMA_NO_NODE;
  50. }
  51. cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
  52. EXPORT_SYMBOL(node_to_cpumask_map);
  53. /*
  54. * Map cpu index to node index
  55. */
  56. DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
  57. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
  58. void numa_set_node(int cpu, int node)
  59. {
  60. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  61. /* early setting, no percpu area yet */
  62. if (cpu_to_node_map) {
  63. cpu_to_node_map[cpu] = node;
  64. return;
  65. }
  66. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  67. if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
  68. printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
  69. dump_stack();
  70. return;
  71. }
  72. #endif
  73. per_cpu(x86_cpu_to_node_map, cpu) = node;
  74. set_cpu_numa_node(cpu, node);
  75. }
  76. void numa_clear_node(int cpu)
  77. {
  78. numa_set_node(cpu, NUMA_NO_NODE);
  79. }
  80. /*
  81. * Allocate node_to_cpumask_map based on number of available nodes
  82. * Requires node_possible_map to be valid.
  83. *
  84. * Note: cpumask_of_node() is not valid until after this is done.
  85. * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
  86. */
  87. void __init setup_node_to_cpumask_map(void)
  88. {
  89. unsigned int node;
  90. /* setup nr_node_ids if not done yet */
  91. if (nr_node_ids == MAX_NUMNODES)
  92. setup_nr_node_ids();
  93. /* allocate the map */
  94. for (node = 0; node < nr_node_ids; node++)
  95. alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
  96. /* cpumask_of_node() will now work */
  97. pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
  98. }
  99. static int __init numa_register_nodes(void)
  100. {
  101. int nid;
  102. if (!memblock_validate_numa_coverage(SZ_1M))
  103. return -EINVAL;
  104. /* Finally register nodes. */
  105. for_each_node_mask(nid, node_possible_map) {
  106. unsigned long start_pfn, end_pfn;
  107. /*
  108. * Note, get_pfn_range_for_nid() depends on
  109. * memblock_set_node() having already happened
  110. */
  111. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  112. if (start_pfn >= end_pfn)
  113. continue;
  114. alloc_node_data(nid);
  115. node_set_online(nid);
  116. }
  117. /* Dump memblock with node info and return. */
  118. memblock_dump_all();
  119. return 0;
  120. }
  121. /*
  122. * There are unfortunately some poorly designed mainboards around that
  123. * only connect memory to a single CPU. This breaks the 1:1 cpu->node
  124. * mapping. To avoid this fill in the mapping for all possible CPUs,
  125. * as the number of CPUs is not known yet. We round robin the existing
  126. * nodes.
  127. */
  128. static void __init numa_init_array(void)
  129. {
  130. int rr, i;
  131. rr = first_node(node_online_map);
  132. for (i = 0; i < nr_cpu_ids; i++) {
  133. if (early_cpu_to_node(i) != NUMA_NO_NODE)
  134. continue;
  135. numa_set_node(i, rr);
  136. rr = next_node_in(rr, node_online_map);
  137. }
  138. }
  139. static int __init numa_init(int (*init_func)(void))
  140. {
  141. int i;
  142. int ret;
  143. for (i = 0; i < MAX_LOCAL_APIC; i++)
  144. set_apicid_to_node(i, NUMA_NO_NODE);
  145. ret = numa_memblks_init(init_func, /* memblock_force_top_down */ true);
  146. if (ret < 0)
  147. return ret;
  148. ret = numa_register_nodes();
  149. if (ret < 0)
  150. return ret;
  151. for (i = 0; i < nr_cpu_ids; i++) {
  152. int nid = early_cpu_to_node(i);
  153. if (nid == NUMA_NO_NODE)
  154. continue;
  155. if (!node_online(nid))
  156. numa_clear_node(i);
  157. }
  158. numa_init_array();
  159. return 0;
  160. }
  161. /**
  162. * dummy_numa_init - Fallback dummy NUMA init
  163. *
  164. * Used if there's no underlying NUMA architecture, NUMA initialization
  165. * fails, or NUMA is disabled on the command line.
  166. *
  167. * Must online at least one node and add memory blocks that cover all
  168. * allowed memory. This function must not fail.
  169. */
  170. static int __init dummy_numa_init(void)
  171. {
  172. printk(KERN_INFO "%s\n",
  173. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  174. printk(KERN_INFO "Faking a node at [mem %#018Lx-%#018Lx]\n",
  175. 0LLU, PFN_PHYS(max_pfn) - 1);
  176. node_set(0, numa_nodes_parsed);
  177. numa_add_memblk(0, 0, PFN_PHYS(max_pfn));
  178. return 0;
  179. }
  180. /**
  181. * x86_numa_init - Initialize NUMA
  182. *
  183. * Try each configured NUMA initialization method until one succeeds. The
  184. * last fallback is dummy single node config encompassing whole memory and
  185. * never fails.
  186. */
  187. void __init x86_numa_init(void)
  188. {
  189. if (!numa_off) {
  190. #ifdef CONFIG_ACPI_NUMA
  191. if (!numa_init(x86_acpi_numa_init))
  192. return;
  193. #endif
  194. #ifdef CONFIG_AMD_NUMA
  195. if (!numa_init(amd_numa_init))
  196. return;
  197. #endif
  198. if (acpi_disabled && !numa_init(of_numa_init))
  199. return;
  200. }
  201. numa_init(dummy_numa_init);
  202. }
  203. /*
  204. * A node may exist which has one or more Generic Initiators but no CPUs and no
  205. * memory.
  206. *
  207. * This function must be called after init_cpu_to_node(), to ensure that any
  208. * memoryless CPU nodes have already been brought online, and before the
  209. * node_data[nid] is needed for zone list setup in build_all_zonelists().
  210. *
  211. * When this function is called, any nodes containing either memory and/or CPUs
  212. * will already be online and there is no need to do anything extra, even if
  213. * they also contain one or more Generic Initiators.
  214. */
  215. void __init init_gi_nodes(void)
  216. {
  217. int nid;
  218. /*
  219. * Exclude this node from
  220. * bringup_nonboot_cpus
  221. * cpu_up
  222. * __try_online_node
  223. * register_one_node
  224. * because node_subsys is not initialized yet.
  225. * TODO remove dependency on node_online
  226. */
  227. for_each_node_state(nid, N_GENERIC_INITIATOR)
  228. if (!node_online(nid))
  229. node_set_online(nid);
  230. }
  231. /*
  232. * Setup early cpu_to_node.
  233. *
  234. * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
  235. * and apicid_to_node[] tables have valid entries for a CPU.
  236. * This means we skip cpu_to_node[] initialisation for NUMA
  237. * emulation and faking node case (when running a kernel compiled
  238. * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
  239. * is already initialized in a round robin manner at numa_init_array,
  240. * prior to this call, and this initialization is good enough
  241. * for the fake NUMA cases.
  242. *
  243. * Called before the per_cpu areas are setup.
  244. */
  245. void __init init_cpu_to_node(void)
  246. {
  247. int cpu;
  248. u32 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
  249. BUG_ON(cpu_to_apicid == NULL);
  250. for_each_possible_cpu(cpu) {
  251. int node = numa_cpu_node(cpu);
  252. if (node == NUMA_NO_NODE)
  253. continue;
  254. /*
  255. * Exclude this node from
  256. * bringup_nonboot_cpus
  257. * cpu_up
  258. * __try_online_node
  259. * register_one_node
  260. * because node_subsys is not initialized yet.
  261. * TODO remove dependency on node_online
  262. */
  263. if (!node_online(node))
  264. node_set_online(node);
  265. numa_set_node(cpu, node);
  266. }
  267. }
  268. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  269. # ifndef CONFIG_NUMA_EMU
  270. void numa_add_cpu(unsigned int cpu)
  271. {
  272. cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  273. }
  274. void numa_remove_cpu(unsigned int cpu)
  275. {
  276. cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  277. }
  278. # endif /* !CONFIG_NUMA_EMU */
  279. #else /* !CONFIG_DEBUG_PER_CPU_MAPS */
  280. int __cpu_to_node(int cpu)
  281. {
  282. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  283. printk(KERN_WARNING
  284. "cpu_to_node(%d): usage too early!\n", cpu);
  285. dump_stack();
  286. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  287. }
  288. return per_cpu(x86_cpu_to_node_map, cpu);
  289. }
  290. EXPORT_SYMBOL(__cpu_to_node);
  291. /*
  292. * Same function as cpu_to_node() but used if called before the
  293. * per_cpu areas are setup.
  294. */
  295. int early_cpu_to_node(int cpu)
  296. {
  297. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  298. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  299. if (!cpu_possible(cpu)) {
  300. printk(KERN_WARNING
  301. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  302. dump_stack();
  303. return NUMA_NO_NODE;
  304. }
  305. return per_cpu(x86_cpu_to_node_map, cpu);
  306. }
  307. void debug_cpumask_set_cpu(unsigned int cpu, int node, bool enable)
  308. {
  309. struct cpumask *mask;
  310. if (node == NUMA_NO_NODE) {
  311. /* early_cpu_to_node() already emits a warning and trace */
  312. return;
  313. }
  314. mask = node_to_cpumask_map[node];
  315. if (!cpumask_available(mask)) {
  316. pr_err("node_to_cpumask_map[%i] NULL\n", node);
  317. dump_stack();
  318. return;
  319. }
  320. if (enable)
  321. cpumask_set_cpu(cpu, mask);
  322. else
  323. cpumask_clear_cpu(cpu, mask);
  324. printk(KERN_DEBUG "%s cpu %d node %d: mask now %*pbl\n",
  325. enable ? "numa_add_cpu" : "numa_remove_cpu",
  326. cpu, node, cpumask_pr_args(mask));
  327. return;
  328. }
  329. # ifndef CONFIG_NUMA_EMU
  330. static void numa_set_cpumask(int cpu, bool enable)
  331. {
  332. debug_cpumask_set_cpu(cpu, early_cpu_to_node(cpu), enable);
  333. }
  334. void numa_add_cpu(unsigned int cpu)
  335. {
  336. numa_set_cpumask(cpu, true);
  337. }
  338. void numa_remove_cpu(unsigned int cpu)
  339. {
  340. numa_set_cpumask(cpu, false);
  341. }
  342. # endif /* !CONFIG_NUMA_EMU */
  343. /*
  344. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  345. */
  346. const struct cpumask *cpumask_of_node(int node)
  347. {
  348. if ((unsigned)node >= nr_node_ids) {
  349. printk(KERN_WARNING
  350. "cpumask_of_node(%d): (unsigned)node >= nr_node_ids(%u)\n",
  351. node, nr_node_ids);
  352. dump_stack();
  353. return cpu_none_mask;
  354. }
  355. if (!cpumask_available(node_to_cpumask_map[node])) {
  356. printk(KERN_WARNING
  357. "cpumask_of_node(%d): no node_to_cpumask_map!\n",
  358. node);
  359. dump_stack();
  360. return cpu_online_mask;
  361. }
  362. return node_to_cpumask_map[node];
  363. }
  364. EXPORT_SYMBOL(cpumask_of_node);
  365. #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
  366. #ifdef CONFIG_NUMA_EMU
  367. void __init numa_emu_update_cpu_to_node(int *emu_nid_to_phys,
  368. unsigned int nr_emu_nids)
  369. {
  370. int i, j;
  371. /*
  372. * Transform __apicid_to_node table to use emulated nids by
  373. * reverse-mapping phys_nid. The maps should always exist but fall
  374. * back to zero just in case.
  375. */
  376. for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
  377. if (__apicid_to_node[i] == NUMA_NO_NODE)
  378. continue;
  379. for (j = 0; j < nr_emu_nids; j++)
  380. if (__apicid_to_node[i] == emu_nid_to_phys[j])
  381. break;
  382. __apicid_to_node[i] = j < nr_emu_nids ? j : 0;
  383. }
  384. }
  385. u64 __init numa_emu_dma_end(void)
  386. {
  387. return PFN_PHYS(MAX_DMA32_PFN);
  388. }
  389. #endif /* CONFIG_NUMA_EMU */