Commit | Line | Data |
---|---|---|
4fe29a85 GOC |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
3 | #include <linux/init.h> | |
4 | #include <linux/bootmem.h> | |
5 | #include <linux/percpu.h> | |
1ecd2765 | 6 | #include <linux/kexec.h> |
17b4cceb | 7 | #include <linux/crash_dump.h> |
4fe29a85 GOC |
8 | #include <asm/smp.h> |
9 | #include <asm/percpu.h> | |
10 | #include <asm/sections.h> | |
11 | #include <asm/processor.h> | |
12 | #include <asm/setup.h> | |
13 | #include <asm/topology.h> | |
0fc0906e | 14 | #include <asm/mpspec.h> |
76eb4131 | 15 | #include <asm/apicdef.h> |
1ecd2765 | 16 | #include <asm/highmem.h> |
76eb4131 | 17 | |
f8955ebe | 18 | #ifdef CONFIG_X86_LOCAL_APIC |
2fe60147 AS |
19 | unsigned int num_processors; |
20 | unsigned disabled_cpus __cpuinitdata; | |
21 | /* Processor that is doing the boot up */ | |
22 | unsigned int boot_cpu_physical_apicid = -1U; | |
e0da3364 | 23 | unsigned int max_physical_apicid; |
2fe60147 AS |
24 | EXPORT_SYMBOL(boot_cpu_physical_apicid); |
25 | ||
0fc0906e AS |
26 | /* Bitmask of physically existing CPUs */ |
27 | physid_mask_t phys_cpu_present_map; | |
f8955ebe | 28 | #endif |
0fc0906e | 29 | |
23ca4bba MT |
30 | /* map cpu index to physical APIC ID */ |
31 | DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID); | |
32 | DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID); | |
33 | EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid); | |
34 | EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid); | |
35 | ||
36 | #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64) | |
37 | #define X86_64_NUMA 1 | |
38 | ||
7891a24e | 39 | /* map cpu index to node index */ |
23ca4bba MT |
40 | DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE); |
41 | EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map); | |
9f248bde MT |
42 | |
43 | /* which logical CPUs are on which nodes */ | |
44 | cpumask_t *node_to_cpumask_map; | |
45 | EXPORT_SYMBOL(node_to_cpumask_map); | |
46 | ||
47 | /* setup node_to_cpumask_map */ | |
48 | static void __init setup_node_to_cpumask_map(void); | |
49 | ||
50 | #else | |
51 | static inline void setup_node_to_cpumask_map(void) { } | |
23ca4bba MT |
52 | #endif |
53 | ||
f8955ebe | 54 | #if defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) && defined(CONFIG_X86_SMP) |
4fe29a85 GOC |
55 | /* |
56 | * Copy data used in early init routines from the initial arrays to the | |
57 | * per cpu data areas. These arrays then become expendable and the | |
58 | * *_early_ptr's are zeroed indicating that the static arrays are gone. | |
59 | */ | |
60 | static void __init setup_per_cpu_maps(void) | |
61 | { | |
62 | int cpu; | |
63 | ||
64 | for_each_possible_cpu(cpu) { | |
23ca4bba MT |
65 | per_cpu(x86_cpu_to_apicid, cpu) = |
66 | early_per_cpu_map(x86_cpu_to_apicid, cpu); | |
b447a468 | 67 | per_cpu(x86_bios_cpu_apicid, cpu) = |
23ca4bba MT |
68 | early_per_cpu_map(x86_bios_cpu_apicid, cpu); |
69 | #ifdef X86_64_NUMA | |
b447a468 | 70 | per_cpu(x86_cpu_to_node_map, cpu) = |
23ca4bba | 71 | early_per_cpu_map(x86_cpu_to_node_map, cpu); |
4fe29a85 GOC |
72 | #endif |
73 | } | |
74 | ||
75 | /* indicate the early static arrays will soon be gone */ | |
23ca4bba MT |
76 | early_per_cpu_ptr(x86_cpu_to_apicid) = NULL; |
77 | early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL; | |
78 | #ifdef X86_64_NUMA | |
79 | early_per_cpu_ptr(x86_cpu_to_node_map) = NULL; | |
4fe29a85 GOC |
80 | #endif |
81 | } | |
82 | ||
83 | #ifdef CONFIG_X86_32 | |
84 | /* | |
85 | * Great future not-so-futuristic plan: make i386 and x86_64 do it | |
86 | * the same way | |
87 | */ | |
88 | unsigned long __per_cpu_offset[NR_CPUS] __read_mostly; | |
89 | EXPORT_SYMBOL(__per_cpu_offset); | |
3461b0af MT |
90 | static inline void setup_cpu_pda_map(void) { } |
91 | ||
92 | #elif !defined(CONFIG_SMP) | |
93 | static inline void setup_cpu_pda_map(void) { } | |
94 | ||
95 | #else /* CONFIG_SMP && CONFIG_X86_64 */ | |
96 | ||
97 | /* | |
98 | * Allocate cpu_pda pointer table and array via alloc_bootmem. | |
99 | */ | |
100 | static void __init setup_cpu_pda_map(void) | |
101 | { | |
102 | char *pda; | |
103 | struct x8664_pda **new_cpu_pda; | |
104 | unsigned long size; | |
105 | int cpu; | |
106 | ||
107 | size = roundup(sizeof(struct x8664_pda), cache_line_size()); | |
108 | ||
109 | /* allocate cpu_pda array and pointer table */ | |
110 | { | |
111 | unsigned long tsize = nr_cpu_ids * sizeof(void *); | |
112 | unsigned long asize = size * (nr_cpu_ids - 1); | |
113 | ||
114 | tsize = roundup(tsize, cache_line_size()); | |
115 | new_cpu_pda = alloc_bootmem(tsize + asize); | |
116 | pda = (char *)new_cpu_pda + tsize; | |
117 | } | |
118 | ||
119 | /* initialize pointer table to static pda's */ | |
120 | for_each_possible_cpu(cpu) { | |
121 | if (cpu == 0) { | |
122 | /* leave boot cpu pda in place */ | |
123 | new_cpu_pda[0] = cpu_pda(0); | |
124 | continue; | |
125 | } | |
126 | new_cpu_pda[cpu] = (struct x8664_pda *)pda; | |
127 | new_cpu_pda[cpu]->in_bootmem = 1; | |
128 | pda += size; | |
129 | } | |
130 | ||
131 | /* point to new pointer table */ | |
132 | _cpu_pda = new_cpu_pda; | |
133 | } | |
4fe29a85 GOC |
134 | #endif |
135 | ||
136 | /* | |
137 | * Great future plan: | |
138 | * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data. | |
139 | * Always point %gs to its beginning | |
140 | */ | |
141 | void __init setup_per_cpu_areas(void) | |
142 | { | |
1f8ff037 | 143 | ssize_t size, old_size, da_size; |
3461b0af MT |
144 | char *ptr; |
145 | int cpu; | |
1f8ff037 | 146 | unsigned long align = 1; |
4fe29a85 | 147 | |
3461b0af MT |
148 | /* Setup cpu_pda map */ |
149 | setup_cpu_pda_map(); | |
150 | ||
4fe29a85 | 151 | /* Copy section for each CPU (we discard the original) */ |
1f3fcd4b | 152 | old_size = PERCPU_ENOUGH_ROOM; |
1f8ff037 YL |
153 | da_size = per_cpu_dyn_array_size(&align); |
154 | align = max_t(unsigned long, PAGE_SIZE, align); | |
155 | size = roundup(old_size + da_size, align); | |
053713f5 | 156 | printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n", |
4fe29a85 | 157 | size); |
b447a468 | 158 | |
3461b0af | 159 | for_each_possible_cpu(cpu) { |
4fe29a85 | 160 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
1f8ff037 YL |
161 | ptr = __alloc_bootmem(size, align, |
162 | __pa(MAX_DMA_ADDRESS)); | |
4fe29a85 | 163 | #else |
3461b0af | 164 | int node = early_cpu_to_node(cpu); |
b447a468 | 165 | if (!node_online(node) || !NODE_DATA(node)) { |
1f8ff037 YL |
166 | ptr = __alloc_bootmem(size, align, |
167 | __pa(MAX_DMA_ADDRESS)); | |
b447a468 | 168 | printk(KERN_INFO |
23ca4bba | 169 | "cpu %d has no node %d or node-local memory\n", |
3461b0af | 170 | cpu, node); |
a677f58a YL |
171 | if (ptr) |
172 | printk(KERN_DEBUG "per cpu data for cpu%d at %016lx\n", | |
173 | cpu, __pa(ptr)); | |
b447a468 | 174 | } |
a677f58a | 175 | else { |
1f8ff037 YL |
176 | ptr = __alloc_bootmem_node(NODE_DATA(node), size, align, |
177 | __pa(MAX_DMA_ADDRESS)); | |
a677f58a YL |
178 | if (ptr) |
179 | printk(KERN_DEBUG "per cpu data for cpu%d on node%d at %016lx\n", | |
180 | cpu, node, __pa(ptr)); | |
181 | } | |
4fe29a85 | 182 | #endif |
3461b0af | 183 | per_cpu_offset(cpu) = ptr - __per_cpu_start; |
4fe29a85 | 184 | memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start); |
9f0e8d04 | 185 | |
1f3fcd4b YL |
186 | per_cpu_alloc_dyn_array(cpu, ptr + old_size); |
187 | ||
4fe29a85 GOC |
188 | } |
189 | ||
9f248bde MT |
190 | printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d, nr_node_ids %d\n", |
191 | NR_CPUS, nr_cpu_ids, nr_node_ids); | |
9f0e8d04 | 192 | |
b447a468 | 193 | /* Setup percpu data maps */ |
4fe29a85 | 194 | setup_per_cpu_maps(); |
9f0e8d04 | 195 | |
9f248bde MT |
196 | /* Setup node to cpumask map */ |
197 | setup_node_to_cpumask_map(); | |
4fe29a85 GOC |
198 | } |
199 | ||
200 | #endif | |
c45a707d | 201 | |
23ca4bba | 202 | #ifdef X86_64_NUMA |
9f248bde MT |
203 | |
204 | /* | |
205 | * Allocate node_to_cpumask_map based on number of available nodes | |
206 | * Requires node_possible_map to be valid. | |
207 | * | |
208 | * Note: node_to_cpumask() is not valid until after this is done. | |
209 | */ | |
210 | static void __init setup_node_to_cpumask_map(void) | |
211 | { | |
212 | unsigned int node, num = 0; | |
213 | cpumask_t *map; | |
214 | ||
215 | /* setup nr_node_ids if not done yet */ | |
216 | if (nr_node_ids == MAX_NUMNODES) { | |
217 | for_each_node_mask(node, node_possible_map) | |
218 | num = node; | |
219 | nr_node_ids = num + 1; | |
220 | } | |
221 | ||
222 | /* allocate the map */ | |
223 | map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t)); | |
224 | ||
cfc1b9a6 TG |
225 | pr_debug(KERN_DEBUG "Node to cpumask map at %p for %d nodes\n", |
226 | map, nr_node_ids); | |
9f248bde MT |
227 | |
228 | /* node_to_cpumask() will now work */ | |
229 | node_to_cpumask_map = map; | |
230 | } | |
231 | ||
23ca4bba MT |
232 | void __cpuinit numa_set_node(int cpu, int node) |
233 | { | |
234 | int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map); | |
235 | ||
3461b0af | 236 | if (cpu_pda(cpu) && node != NUMA_NO_NODE) |
7891a24e MT |
237 | cpu_pda(cpu)->nodenumber = node; |
238 | ||
23ca4bba MT |
239 | if (cpu_to_node_map) |
240 | cpu_to_node_map[cpu] = node; | |
241 | ||
242 | else if (per_cpu_offset(cpu)) | |
243 | per_cpu(x86_cpu_to_node_map, cpu) = node; | |
244 | ||
245 | else | |
cfc1b9a6 | 246 | pr_debug("Setting node for non-present cpu %d\n", cpu); |
23ca4bba MT |
247 | } |
248 | ||
249 | void __cpuinit numa_clear_node(int cpu) | |
250 | { | |
251 | numa_set_node(cpu, NUMA_NO_NODE); | |
252 | } | |
253 | ||
9f248bde MT |
254 | #ifndef CONFIG_DEBUG_PER_CPU_MAPS |
255 | ||
23ca4bba MT |
256 | void __cpuinit numa_add_cpu(int cpu) |
257 | { | |
258 | cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]); | |
259 | } | |
260 | ||
261 | void __cpuinit numa_remove_cpu(int cpu) | |
262 | { | |
263 | cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]); | |
264 | } | |
23ca4bba | 265 | |
9f248bde MT |
266 | #else /* CONFIG_DEBUG_PER_CPU_MAPS */ |
267 | ||
268 | /* | |
269 | * --------- debug versions of the numa functions --------- | |
270 | */ | |
271 | static void __cpuinit numa_set_cpumask(int cpu, int enable) | |
272 | { | |
273 | int node = cpu_to_node(cpu); | |
274 | cpumask_t *mask; | |
275 | char buf[64]; | |
276 | ||
277 | if (node_to_cpumask_map == NULL) { | |
278 | printk(KERN_ERR "node_to_cpumask_map NULL\n"); | |
279 | dump_stack(); | |
280 | return; | |
281 | } | |
282 | ||
283 | mask = &node_to_cpumask_map[node]; | |
284 | if (enable) | |
285 | cpu_set(cpu, *mask); | |
286 | else | |
287 | cpu_clear(cpu, *mask); | |
288 | ||
289 | cpulist_scnprintf(buf, sizeof(buf), *mask); | |
290 | printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n", | |
291 | enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf); | |
292 | } | |
293 | ||
294 | void __cpuinit numa_add_cpu(int cpu) | |
295 | { | |
296 | numa_set_cpumask(cpu, 1); | |
297 | } | |
298 | ||
299 | void __cpuinit numa_remove_cpu(int cpu) | |
300 | { | |
301 | numa_set_cpumask(cpu, 0); | |
302 | } | |
23ca4bba MT |
303 | |
304 | int cpu_to_node(int cpu) | |
305 | { | |
306 | if (early_per_cpu_ptr(x86_cpu_to_node_map)) { | |
307 | printk(KERN_WARNING | |
308 | "cpu_to_node(%d): usage too early!\n", cpu); | |
309 | dump_stack(); | |
310 | return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu]; | |
311 | } | |
312 | return per_cpu(x86_cpu_to_node_map, cpu); | |
313 | } | |
314 | EXPORT_SYMBOL(cpu_to_node); | |
315 | ||
9f248bde MT |
316 | /* |
317 | * Same function as cpu_to_node() but used if called before the | |
318 | * per_cpu areas are setup. | |
319 | */ | |
23ca4bba MT |
320 | int early_cpu_to_node(int cpu) |
321 | { | |
322 | if (early_per_cpu_ptr(x86_cpu_to_node_map)) | |
323 | return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu]; | |
324 | ||
325 | if (!per_cpu_offset(cpu)) { | |
326 | printk(KERN_WARNING | |
327 | "early_cpu_to_node(%d): no per_cpu area!\n", cpu); | |
9f248bde | 328 | dump_stack(); |
23ca4bba MT |
329 | return NUMA_NO_NODE; |
330 | } | |
331 | return per_cpu(x86_cpu_to_node_map, cpu); | |
332 | } | |
9f248bde | 333 | |
6a2f47ca MT |
334 | |
335 | /* empty cpumask */ | |
336 | static const cpumask_t cpu_mask_none; | |
337 | ||
9f248bde MT |
338 | /* |
339 | * Returns a pointer to the bitmask of CPUs on Node 'node'. | |
340 | */ | |
11369f35 | 341 | const cpumask_t *_node_to_cpumask_ptr(int node) |
9f248bde MT |
342 | { |
343 | if (node_to_cpumask_map == NULL) { | |
344 | printk(KERN_WARNING | |
345 | "_node_to_cpumask_ptr(%d): no node_to_cpumask_map!\n", | |
346 | node); | |
347 | dump_stack(); | |
11369f35 | 348 | return (const cpumask_t *)&cpu_online_map; |
9f248bde | 349 | } |
6a2f47ca MT |
350 | if (node >= nr_node_ids) { |
351 | printk(KERN_WARNING | |
352 | "_node_to_cpumask_ptr(%d): node > nr_node_ids(%d)\n", | |
353 | node, nr_node_ids); | |
354 | dump_stack(); | |
11369f35 | 355 | return &cpu_mask_none; |
6a2f47ca | 356 | } |
11369f35 | 357 | return &node_to_cpumask_map[node]; |
9f248bde MT |
358 | } |
359 | EXPORT_SYMBOL(_node_to_cpumask_ptr); | |
360 | ||
361 | /* | |
362 | * Returns a bitmask of CPUs on Node 'node'. | |
6a2f47ca MT |
363 | * |
364 | * Side note: this function creates the returned cpumask on the stack | |
365 | * so with a high NR_CPUS count, excessive stack space is used. The | |
366 | * node_to_cpumask_ptr function should be used whenever possible. | |
9f248bde MT |
367 | */ |
368 | cpumask_t node_to_cpumask(int node) | |
369 | { | |
370 | if (node_to_cpumask_map == NULL) { | |
371 | printk(KERN_WARNING | |
372 | "node_to_cpumask(%d): no node_to_cpumask_map!\n", node); | |
373 | dump_stack(); | |
374 | return cpu_online_map; | |
375 | } | |
6a2f47ca MT |
376 | if (node >= nr_node_ids) { |
377 | printk(KERN_WARNING | |
378 | "node_to_cpumask(%d): node > nr_node_ids(%d)\n", | |
379 | node, nr_node_ids); | |
380 | dump_stack(); | |
381 | return cpu_mask_none; | |
382 | } | |
9f248bde MT |
383 | return node_to_cpumask_map[node]; |
384 | } | |
385 | EXPORT_SYMBOL(node_to_cpumask); | |
386 | ||
387 | /* | |
388 | * --------- end of debug versions of the numa functions --------- | |
389 | */ | |
390 | ||
391 | #endif /* CONFIG_DEBUG_PER_CPU_MAPS */ | |
392 | ||
393 | #endif /* X86_64_NUMA */ | |
1ecd2765 | 394 |