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