numa.c 4.1 KB

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