blob: bf63de72b643f7b919190efe93e0159c32048a71 [file] [log] [blame]
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -03001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/init.h>
4#include <linux/bootmem.h>
5#include <linux/percpu.h>
Bernhard Walle1ecd2762008-06-20 15:38:22 +02006#include <linux/kexec.h>
Yinghai Lu17b4cce2008-06-21 21:02:20 -07007#include <linux/crash_dump.h>
Jaswinder Singh Rajput8a87dd92009-01-04 17:04:26 +05308#include <linux/smp.h>
9#include <linux/topology.h>
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030010#include <asm/sections.h>
11#include <asm/processor.h>
12#include <asm/setup.h>
Alexey Starikovskiy0fc09062008-04-04 23:40:48 +040013#include <asm/mpspec.h>
Alexey Starikovskiy76eb4132008-04-04 23:40:41 +040014#include <asm/apicdef.h>
Bernhard Walle1ecd2762008-06-20 15:38:22 +020015#include <asm/highmem.h>
Jaswinder Singh Rajput06879032009-01-10 12:17:37 +053016#include <asm/cpumask.h>
Alexey Starikovskiy76eb4132008-04-04 23:40:41 +040017
James Bottomleyf8955eb2008-05-10 09:01:48 -050018#ifdef CONFIG_X86_LOCAL_APIC
Alexey Starikovskiy2fe60142008-04-04 23:41:44 +040019unsigned int num_processors;
20unsigned disabled_cpus __cpuinitdata;
21/* Processor that is doing the boot up */
22unsigned int boot_cpu_physical_apicid = -1U;
23EXPORT_SYMBOL(boot_cpu_physical_apicid);
Jaswinder Singh Rajput8a87dd92009-01-04 17:04:26 +053024unsigned int max_physical_apicid;
Alexey Starikovskiy2fe60142008-04-04 23:41:44 +040025
Alexey Starikovskiy0fc09062008-04-04 23:40:48 +040026/* Bitmask of physically existing CPUs */
27physid_mask_t phys_cpu_present_map;
James Bottomleyf8955eb2008-05-10 09:01:48 -050028#endif
Alexey Starikovskiy0fc09062008-04-04 23:40:48 +040029
Mike Travis23ca4bb2008-05-12 21:21:12 +020030/* map cpu index to physical APIC ID */
31DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
32DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
33EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
34EXPORT_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
Mike Travis7891a242008-05-12 21:21:12 +020039/* map cpu index to node index */
Mike Travis23ca4bb2008-05-12 21:21:12 +020040DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
41EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
Mike Travis9f248bd2008-05-12 21:21:12 +020042
43/* which logical CPUs are on which nodes */
44cpumask_t *node_to_cpumask_map;
45EXPORT_SYMBOL(node_to_cpumask_map);
46
47/* setup node_to_cpumask_map */
48static void __init setup_node_to_cpumask_map(void);
49
50#else
51static inline void setup_node_to_cpumask_map(void) { }
Mike Travis23ca4bb2008-05-12 21:21:12 +020052#endif
53
James Bottomleyf8955eb2008-05-10 09:01:48 -050054#if defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) && defined(CONFIG_X86_SMP)
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030055/*
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 */
60static void __init setup_per_cpu_maps(void)
61{
62 int cpu;
63
64 for_each_possible_cpu(cpu) {
Mike Travis23ca4bb2008-05-12 21:21:12 +020065 per_cpu(x86_cpu_to_apicid, cpu) =
66 early_per_cpu_map(x86_cpu_to_apicid, cpu);
Mike Travisb447a462008-03-25 15:06:51 -070067 per_cpu(x86_bios_cpu_apicid, cpu) =
Mike Travis23ca4bb2008-05-12 21:21:12 +020068 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
69#ifdef X86_64_NUMA
Mike Travisb447a462008-03-25 15:06:51 -070070 per_cpu(x86_cpu_to_node_map, cpu) =
Mike Travis23ca4bb2008-05-12 21:21:12 +020071 early_per_cpu_map(x86_cpu_to_node_map, cpu);
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030072#endif
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030073 }
74
75 /* indicate the early static arrays will soon be gone */
Mike Travis23ca4bb2008-05-12 21:21:12 +020076 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;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -030080#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 */
88unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
89EXPORT_SYMBOL(__per_cpu_offset);
Mike Travis3461b0a2008-05-12 21:21:13 +020090static inline void setup_cpu_pda_map(void) { }
91
92#elif !defined(CONFIG_SMP)
93static 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 */
100static 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}
Mike Travisc2d1cec2009-01-04 05:18:03 -0800134
135#endif /* CONFIG_SMP && CONFIG_X86_64 */
136
137#ifdef CONFIG_X86_64
138
139/* correctly size the local cpu masks */
140static void setup_cpu_local_masks(void)
141{
142 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
143 alloc_bootmem_cpumask_var(&cpu_callin_mask);
144 alloc_bootmem_cpumask_var(&cpu_callout_mask);
145 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
146}
147
148#else /* CONFIG_X86_32 */
149
150static inline void setup_cpu_local_masks(void)
151{
152}
153
154#endif /* CONFIG_X86_32 */
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300155
156/*
157 * Great future plan:
158 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
159 * Always point %gs to its beginning
160 */
161void __init setup_per_cpu_areas(void)
162{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200163 ssize_t size, old_size;
Mike Travis3461b0a2008-05-12 21:21:13 +0200164 char *ptr;
165 int cpu;
Yinghai Lu1f8ff032008-08-19 20:49:45 -0700166 unsigned long align = 1;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300167
Mike Travis3461b0a2008-05-12 21:21:13 +0200168 /* Setup cpu_pda map */
169 setup_cpu_pda_map();
170
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300171 /* Copy section for each CPU (we discard the original) */
Yinghai Lu1f3fcd42008-08-19 20:49:44 -0700172 old_size = PERCPU_ENOUGH_ROOM;
Yinghai Lu1f8ff032008-08-19 20:49:45 -0700173 align = max_t(unsigned long, PAGE_SIZE, align);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200174 size = roundup(old_size, align);
Mike Travisa1681962008-12-16 17:33:53 -0800175
Cyrill Gorcunovab143982009-01-02 21:51:32 +0300176 pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
Mike Travisa1681962008-12-16 17:33:53 -0800177 NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
178
Cyrill Gorcunovab143982009-01-02 21:51:32 +0300179 pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
Mike Travisb447a462008-03-25 15:06:51 -0700180
Mike Travis3461b0a2008-05-12 21:21:13 +0200181 for_each_possible_cpu(cpu) {
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300182#ifndef CONFIG_NEED_MULTIPLE_NODES
Yinghai Lu1f8ff032008-08-19 20:49:45 -0700183 ptr = __alloc_bootmem(size, align,
184 __pa(MAX_DMA_ADDRESS));
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300185#else
Mike Travis3461b0a2008-05-12 21:21:13 +0200186 int node = early_cpu_to_node(cpu);
Mike Travisb447a462008-03-25 15:06:51 -0700187 if (!node_online(node) || !NODE_DATA(node)) {
Yinghai Lu1f8ff032008-08-19 20:49:45 -0700188 ptr = __alloc_bootmem(size, align,
189 __pa(MAX_DMA_ADDRESS));
Cyrill Gorcunovab143982009-01-02 21:51:32 +0300190 pr_info("cpu %d has no node %d or node-local memory\n",
Mike Travis3461b0a2008-05-12 21:21:13 +0200191 cpu, node);
Cyrill Gorcunovab143982009-01-02 21:51:32 +0300192 pr_debug("per cpu data for cpu%d at %016lx\n",
193 cpu, __pa(ptr));
194 } else {
Yinghai Lu1f8ff032008-08-19 20:49:45 -0700195 ptr = __alloc_bootmem_node(NODE_DATA(node), size, align,
196 __pa(MAX_DMA_ADDRESS));
Cyrill Gorcunovab143982009-01-02 21:51:32 +0300197 pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
198 cpu, node, __pa(ptr));
Yinghai Lua677f582008-07-29 00:37:10 -0700199 }
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300200#endif
Mike Travis3461b0a2008-05-12 21:21:13 +0200201 per_cpu_offset(cpu) = ptr - __per_cpu_start;
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300202 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
203 }
204
Mike Travisb447a462008-03-25 15:06:51 -0700205 /* Setup percpu data maps */
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300206 setup_per_cpu_maps();
Mike Travis9f0e8d02008-04-04 18:11:01 -0700207
Mike Travis9f248bd2008-05-12 21:21:12 +0200208 /* Setup node to cpumask map */
209 setup_node_to_cpumask_map();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800210
211 /* Setup cpu initialized, callin, callout masks */
212 setup_cpu_local_masks();
Glauber de Oliveira Costa4fe29a82008-03-19 14:25:23 -0300213}
214
215#endif
Huang, Yingc45a7072008-06-02 14:26:25 +0800216
Mike Travis23ca4bb2008-05-12 21:21:12 +0200217#ifdef X86_64_NUMA
Mike Travis9f248bd2008-05-12 21:21:12 +0200218
219/*
220 * Allocate node_to_cpumask_map based on number of available nodes
221 * Requires node_possible_map to be valid.
222 *
223 * Note: node_to_cpumask() is not valid until after this is done.
224 */
225static void __init setup_node_to_cpumask_map(void)
226{
227 unsigned int node, num = 0;
228 cpumask_t *map;
229
230 /* setup nr_node_ids if not done yet */
231 if (nr_node_ids == MAX_NUMNODES) {
232 for_each_node_mask(node, node_possible_map)
233 num = node;
234 nr_node_ids = num + 1;
235 }
236
237 /* allocate the map */
238 map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
239
Gustavo F. Padovan55410792008-10-15 10:37:04 -0300240 pr_debug("Node to cpumask map at %p for %d nodes\n",
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200241 map, nr_node_ids);
Mike Travis9f248bd2008-05-12 21:21:12 +0200242
243 /* node_to_cpumask() will now work */
244 node_to_cpumask_map = map;
245}
246
Mike Travis23ca4bb2008-05-12 21:21:12 +0200247void __cpuinit numa_set_node(int cpu, int node)
248{
249 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
250
Mike Travis3461b0a2008-05-12 21:21:13 +0200251 if (cpu_pda(cpu) && node != NUMA_NO_NODE)
Mike Travis7891a242008-05-12 21:21:12 +0200252 cpu_pda(cpu)->nodenumber = node;
253
Mike Travis23ca4bb2008-05-12 21:21:12 +0200254 if (cpu_to_node_map)
255 cpu_to_node_map[cpu] = node;
256
257 else if (per_cpu_offset(cpu))
258 per_cpu(x86_cpu_to_node_map, cpu) = node;
259
260 else
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200261 pr_debug("Setting node for non-present cpu %d\n", cpu);
Mike Travis23ca4bb2008-05-12 21:21:12 +0200262}
263
264void __cpuinit numa_clear_node(int cpu)
265{
266 numa_set_node(cpu, NUMA_NO_NODE);
267}
268
Mike Travis9f248bd2008-05-12 21:21:12 +0200269#ifndef CONFIG_DEBUG_PER_CPU_MAPS
270
Mike Travis23ca4bb2008-05-12 21:21:12 +0200271void __cpuinit numa_add_cpu(int cpu)
272{
273 cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
274}
275
276void __cpuinit numa_remove_cpu(int cpu)
277{
278 cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
279}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200280
Mike Travis9f248bd2008-05-12 21:21:12 +0200281#else /* CONFIG_DEBUG_PER_CPU_MAPS */
282
283/*
284 * --------- debug versions of the numa functions ---------
285 */
286static void __cpuinit numa_set_cpumask(int cpu, int enable)
287{
288 int node = cpu_to_node(cpu);
289 cpumask_t *mask;
290 char buf[64];
291
292 if (node_to_cpumask_map == NULL) {
293 printk(KERN_ERR "node_to_cpumask_map NULL\n");
294 dump_stack();
295 return;
296 }
297
298 mask = &node_to_cpumask_map[node];
299 if (enable)
300 cpu_set(cpu, *mask);
301 else
302 cpu_clear(cpu, *mask);
303
Rusty Russell29c01772008-12-13 21:20:25 +1030304 cpulist_scnprintf(buf, sizeof(buf), mask);
Mike Travis9f248bd2008-05-12 21:21:12 +0200305 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
Jaswinder Singh Rajput8a87dd92009-01-04 17:04:26 +0530306 enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf);
307}
Mike Travis9f248bd2008-05-12 21:21:12 +0200308
309void __cpuinit numa_add_cpu(int cpu)
310{
311 numa_set_cpumask(cpu, 1);
312}
313
314void __cpuinit numa_remove_cpu(int cpu)
315{
316 numa_set_cpumask(cpu, 0);
317}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200318
319int cpu_to_node(int cpu)
320{
321 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
322 printk(KERN_WARNING
323 "cpu_to_node(%d): usage too early!\n", cpu);
324 dump_stack();
325 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
326 }
327 return per_cpu(x86_cpu_to_node_map, cpu);
328}
329EXPORT_SYMBOL(cpu_to_node);
330
Mike Travis9f248bd2008-05-12 21:21:12 +0200331/*
332 * Same function as cpu_to_node() but used if called before the
333 * per_cpu areas are setup.
334 */
Mike Travis23ca4bb2008-05-12 21:21:12 +0200335int early_cpu_to_node(int cpu)
336{
337 if (early_per_cpu_ptr(x86_cpu_to_node_map))
338 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
339
340 if (!per_cpu_offset(cpu)) {
341 printk(KERN_WARNING
342 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
Mike Travis9f248bd2008-05-12 21:21:12 +0200343 dump_stack();
Mike Travis23ca4bb2008-05-12 21:21:12 +0200344 return NUMA_NO_NODE;
345 }
346 return per_cpu(x86_cpu_to_node_map, cpu);
347}
Mike Travis9f248bd2008-05-12 21:21:12 +0200348
Mike Travis6a2f47c2008-06-27 10:10:13 -0700349
350/* empty cpumask */
351static const cpumask_t cpu_mask_none;
352
Mike Travis9f248bd2008-05-12 21:21:12 +0200353/*
354 * Returns a pointer to the bitmask of CPUs on Node 'node'.
355 */
Rusty Russell393d68f2008-12-26 22:23:38 +1030356const cpumask_t *cpumask_of_node(int node)
Mike Travis9f248bd2008-05-12 21:21:12 +0200357{
358 if (node_to_cpumask_map == NULL) {
359 printk(KERN_WARNING
Rusty Russell393d68f2008-12-26 22:23:38 +1030360 "cpumask_of_node(%d): no node_to_cpumask_map!\n",
Mike Travis9f248bd2008-05-12 21:21:12 +0200361 node);
362 dump_stack();
Mike Travis11369f352008-07-08 14:35:21 -0700363 return (const cpumask_t *)&cpu_online_map;
Mike Travis9f248bd2008-05-12 21:21:12 +0200364 }
Mike Travis6a2f47c2008-06-27 10:10:13 -0700365 if (node >= nr_node_ids) {
366 printk(KERN_WARNING
Rusty Russell393d68f2008-12-26 22:23:38 +1030367 "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
Mike Travis6a2f47c2008-06-27 10:10:13 -0700368 node, nr_node_ids);
369 dump_stack();
Mike Travis11369f352008-07-08 14:35:21 -0700370 return &cpu_mask_none;
Mike Travis6a2f47c2008-06-27 10:10:13 -0700371 }
Mike Travis11369f352008-07-08 14:35:21 -0700372 return &node_to_cpumask_map[node];
Mike Travis9f248bd2008-05-12 21:21:12 +0200373}
Rusty Russell393d68f2008-12-26 22:23:38 +1030374EXPORT_SYMBOL(cpumask_of_node);
Mike Travis9f248bd2008-05-12 21:21:12 +0200375
376/*
377 * Returns a bitmask of CPUs on Node 'node'.
Mike Travis6a2f47c2008-06-27 10:10:13 -0700378 *
379 * Side note: this function creates the returned cpumask on the stack
380 * so with a high NR_CPUS count, excessive stack space is used. The
381 * node_to_cpumask_ptr function should be used whenever possible.
Mike Travis9f248bd2008-05-12 21:21:12 +0200382 */
383cpumask_t node_to_cpumask(int node)
384{
385 if (node_to_cpumask_map == NULL) {
386 printk(KERN_WARNING
387 "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
388 dump_stack();
389 return cpu_online_map;
390 }
Mike Travis6a2f47c2008-06-27 10:10:13 -0700391 if (node >= nr_node_ids) {
392 printk(KERN_WARNING
393 "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
394 node, nr_node_ids);
395 dump_stack();
396 return cpu_mask_none;
397 }
Mike Travis9f248bd2008-05-12 21:21:12 +0200398 return node_to_cpumask_map[node];
399}
400EXPORT_SYMBOL(node_to_cpumask);
401
402/*
403 * --------- end of debug versions of the numa functions ---------
404 */
405
406#endif /* CONFIG_DEBUG_PER_CPU_MAPS */
407
408#endif /* X86_64_NUMA */
Bernhard Walle1ecd2762008-06-20 15:38:22 +0200409