numa.c 3.9 KB

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