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