numa.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NUMA support for s390
  4. *
  5. * Implement NUMA core code.
  6. *
  7. * Copyright IBM Corp. 2015
  8. */
  9. #define KMSG_COMPONENT "numa"
  10. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/mmzone.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/memblock.h>
  15. #include <linux/slab.h>
  16. #include <linux/node.h>
  17. #include <asm/numa.h>
  18. #include "numa_mode.h"
  19. pg_data_t *node_data[MAX_NUMNODES];
  20. EXPORT_SYMBOL(node_data);
  21. cpumask_t node_to_cpumask_map[MAX_NUMNODES];
  22. EXPORT_SYMBOL(node_to_cpumask_map);
  23. static void plain_setup(void)
  24. {
  25. node_set(0, node_possible_map);
  26. }
  27. const struct numa_mode numa_mode_plain = {
  28. .name = "plain",
  29. .setup = plain_setup,
  30. };
  31. static const struct numa_mode *mode = &numa_mode_plain;
  32. int numa_pfn_to_nid(unsigned long pfn)
  33. {
  34. return mode->__pfn_to_nid ? mode->__pfn_to_nid(pfn) : 0;
  35. }
  36. void numa_update_cpu_topology(void)
  37. {
  38. if (mode->update_cpu_topology)
  39. mode->update_cpu_topology();
  40. }
  41. int __node_distance(int a, int b)
  42. {
  43. return mode->distance ? mode->distance(a, b) : 0;
  44. }
  45. EXPORT_SYMBOL(__node_distance);
  46. int numa_debug_enabled;
  47. /*
  48. * alloc_node_data() - Allocate node data
  49. */
  50. static __init pg_data_t *alloc_node_data(void)
  51. {
  52. pg_data_t *res;
  53. res = (pg_data_t *) memblock_phys_alloc(sizeof(pg_data_t), 8);
  54. memset(res, 0, sizeof(pg_data_t));
  55. return res;
  56. }
  57. /*
  58. * numa_setup_memory() - Assign bootmem to nodes
  59. *
  60. * The memory is first added to memblock without any respect to nodes.
  61. * This is fixed before remaining memblock memory is handed over to the
  62. * buddy allocator.
  63. * An important side effect is that large bootmem allocations might easily
  64. * cross node boundaries, which can be needed for large allocations with
  65. * smaller memory stripes in each node (i.e. when using NUMA emulation).
  66. *
  67. * Memory defines nodes:
  68. * Therefore this routine also sets the nodes online with memory.
  69. */
  70. static void __init numa_setup_memory(void)
  71. {
  72. unsigned long cur_base, align, end_of_dram;
  73. int nid = 0;
  74. end_of_dram = memblock_end_of_DRAM();
  75. align = mode->align ? mode->align() : ULONG_MAX;
  76. /*
  77. * Step through all available memory and assign it to the nodes
  78. * indicated by the mode implementation.
  79. * All nodes which are seen here will be set online.
  80. */
  81. cur_base = 0;
  82. do {
  83. nid = numa_pfn_to_nid(PFN_DOWN(cur_base));
  84. node_set_online(nid);
  85. memblock_set_node(cur_base, align, &memblock.memory, nid);
  86. cur_base += align;
  87. } while (cur_base < end_of_dram);
  88. /* Allocate and fill out node_data */
  89. for (nid = 0; nid < MAX_NUMNODES; nid++)
  90. NODE_DATA(nid) = alloc_node_data();
  91. for_each_online_node(nid) {
  92. unsigned long start_pfn, end_pfn;
  93. unsigned long t_start, t_end;
  94. int i;
  95. start_pfn = ULONG_MAX;
  96. end_pfn = 0;
  97. for_each_mem_pfn_range(i, nid, &t_start, &t_end, NULL) {
  98. if (t_start < start_pfn)
  99. start_pfn = t_start;
  100. if (t_end > end_pfn)
  101. end_pfn = t_end;
  102. }
  103. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  104. NODE_DATA(nid)->node_id = nid;
  105. }
  106. }
  107. /*
  108. * numa_setup() - Earliest initialization
  109. *
  110. * Assign the mode and call the mode's setup routine.
  111. */
  112. void __init numa_setup(void)
  113. {
  114. pr_info("NUMA mode: %s\n", mode->name);
  115. nodes_clear(node_possible_map);
  116. /* Initially attach all possible CPUs to node 0. */
  117. cpumask_copy(&node_to_cpumask_map[0], cpu_possible_mask);
  118. if (mode->setup)
  119. mode->setup();
  120. numa_setup_memory();
  121. memblock_dump_all();
  122. }
  123. /*
  124. * numa_init_late() - Initialization initcall
  125. *
  126. * Register NUMA nodes.
  127. */
  128. static int __init numa_init_late(void)
  129. {
  130. int nid;
  131. for_each_online_node(nid)
  132. register_one_node(nid);
  133. return 0;
  134. }
  135. arch_initcall(numa_init_late);
  136. static int __init parse_debug(char *parm)
  137. {
  138. numa_debug_enabled = 1;
  139. return 0;
  140. }
  141. early_param("numa_debug", parse_debug);
  142. static int __init parse_numa(char *parm)
  143. {
  144. if (strcmp(parm, numa_mode_plain.name) == 0)
  145. mode = &numa_mode_plain;
  146. #ifdef CONFIG_NUMA_EMU
  147. if (strcmp(parm, numa_mode_emu.name) == 0)
  148. mode = &numa_mode_emu;
  149. #endif
  150. return 0;
  151. }
  152. early_param("numa", parse_numa);