Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux...
[linux-2.6/mini2440.git] / arch / x86 / kernel / setup_percpu.c
blob49f3f709ee1f3ec989916c5f778b8d858689664f
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>
6 #include <linux/kexec.h>
7 #include <linux/crash_dump.h>
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>
14 #include <asm/mpspec.h>
15 #include <asm/apicdef.h>
16 #include <asm/highmem.h>
18 #ifdef CONFIG_X86_LOCAL_APIC
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;
23 unsigned int max_physical_apicid;
24 EXPORT_SYMBOL(boot_cpu_physical_apicid);
26 /* Bitmask of physically existing CPUs */
27 physid_mask_t phys_cpu_present_map;
28 #endif
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);
36 #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
37 #define X86_64_NUMA 1
39 /* map cpu index to node index */
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);
43 /* which logical CPUs are on which nodes */
44 cpumask_t *node_to_cpumask_map;
45 EXPORT_SYMBOL(node_to_cpumask_map);
47 /* setup node_to_cpumask_map */
48 static void __init setup_node_to_cpumask_map(void);
50 #else
51 static inline void setup_node_to_cpumask_map(void) { }
52 #endif
54 #if defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) && defined(CONFIG_X86_SMP)
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.
60 static void __init setup_per_cpu_maps(void)
62 int cpu;
64 for_each_possible_cpu(cpu) {
65 per_cpu(x86_cpu_to_apicid, cpu) =
66 early_per_cpu_map(x86_cpu_to_apicid, cpu);
67 per_cpu(x86_bios_cpu_apicid, cpu) =
68 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
69 #ifdef X86_64_NUMA
70 per_cpu(x86_cpu_to_node_map, cpu) =
71 early_per_cpu_map(x86_cpu_to_node_map, cpu);
72 #endif
75 /* indicate the early static arrays will soon be gone */
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;
80 #endif
83 #ifdef CONFIG_X86_32
85 * Great future not-so-futuristic plan: make i386 and x86_64 do it
86 * the same way
88 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
89 EXPORT_SYMBOL(__per_cpu_offset);
90 static inline void setup_cpu_pda_map(void) { }
92 #elif !defined(CONFIG_SMP)
93 static inline void setup_cpu_pda_map(void) { }
95 #else /* CONFIG_SMP && CONFIG_X86_64 */
98 * Allocate cpu_pda pointer table and array via alloc_bootmem.
100 static void __init setup_cpu_pda_map(void)
102 char *pda;
103 struct x8664_pda **new_cpu_pda;
104 unsigned long size;
105 int cpu;
107 size = roundup(sizeof(struct x8664_pda), cache_line_size());
109 /* allocate cpu_pda array and pointer table */
111 unsigned long tsize = nr_cpu_ids * sizeof(void *);
112 unsigned long asize = size * (nr_cpu_ids - 1);
114 tsize = roundup(tsize, cache_line_size());
115 new_cpu_pda = alloc_bootmem(tsize + asize);
116 pda = (char *)new_cpu_pda + tsize;
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;
126 new_cpu_pda[cpu] = (struct x8664_pda *)pda;
127 new_cpu_pda[cpu]->in_bootmem = 1;
128 pda += size;
131 /* point to new pointer table */
132 _cpu_pda = new_cpu_pda;
134 #endif
137 * Great future plan:
138 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
139 * Always point %gs to its beginning
141 void __init setup_per_cpu_areas(void)
143 ssize_t size, old_size;
144 char *ptr;
145 int cpu;
146 unsigned long align = 1;
148 /* Setup cpu_pda map */
149 setup_cpu_pda_map();
151 /* Copy section for each CPU (we discard the original) */
152 old_size = PERCPU_ENOUGH_ROOM;
153 align = max_t(unsigned long, PAGE_SIZE, align);
154 size = roundup(old_size, align);
156 printk(KERN_INFO
157 "NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
158 NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
160 printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n",
161 size);
163 for_each_possible_cpu(cpu) {
164 #ifndef CONFIG_NEED_MULTIPLE_NODES
165 ptr = __alloc_bootmem(size, align,
166 __pa(MAX_DMA_ADDRESS));
167 #else
168 int node = early_cpu_to_node(cpu);
169 if (!node_online(node) || !NODE_DATA(node)) {
170 ptr = __alloc_bootmem(size, align,
171 __pa(MAX_DMA_ADDRESS));
172 printk(KERN_INFO
173 "cpu %d has no node %d or node-local memory\n",
174 cpu, node);
175 if (ptr)
176 printk(KERN_DEBUG
177 "per cpu data for cpu%d at %016lx\n",
178 cpu, __pa(ptr));
180 else {
181 ptr = __alloc_bootmem_node(NODE_DATA(node), size, align,
182 __pa(MAX_DMA_ADDRESS));
183 if (ptr)
184 printk(KERN_DEBUG
185 "per cpu data for cpu%d on node%d "
186 "at %016lx\n",
187 cpu, node, __pa(ptr));
189 #endif
190 per_cpu_offset(cpu) = ptr - __per_cpu_start;
191 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
194 /* Setup percpu data maps */
195 setup_per_cpu_maps();
197 /* Setup node to cpumask map */
198 setup_node_to_cpumask_map();
201 #endif
203 #ifdef X86_64_NUMA
206 * Allocate node_to_cpumask_map based on number of available nodes
207 * Requires node_possible_map to be valid.
209 * Note: node_to_cpumask() is not valid until after this is done.
211 static void __init setup_node_to_cpumask_map(void)
213 unsigned int node, num = 0;
214 cpumask_t *map;
216 /* setup nr_node_ids if not done yet */
217 if (nr_node_ids == MAX_NUMNODES) {
218 for_each_node_mask(node, node_possible_map)
219 num = node;
220 nr_node_ids = num + 1;
223 /* allocate the map */
224 map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
226 pr_debug("Node to cpumask map at %p for %d nodes\n",
227 map, nr_node_ids);
229 /* node_to_cpumask() will now work */
230 node_to_cpumask_map = map;
233 void __cpuinit numa_set_node(int cpu, int node)
235 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
237 if (cpu_pda(cpu) && node != NUMA_NO_NODE)
238 cpu_pda(cpu)->nodenumber = node;
240 if (cpu_to_node_map)
241 cpu_to_node_map[cpu] = node;
243 else if (per_cpu_offset(cpu))
244 per_cpu(x86_cpu_to_node_map, cpu) = node;
246 else
247 pr_debug("Setting node for non-present cpu %d\n", cpu);
250 void __cpuinit numa_clear_node(int cpu)
252 numa_set_node(cpu, NUMA_NO_NODE);
255 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
257 void __cpuinit numa_add_cpu(int cpu)
259 cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
262 void __cpuinit numa_remove_cpu(int cpu)
264 cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
267 #else /* CONFIG_DEBUG_PER_CPU_MAPS */
270 * --------- debug versions of the numa functions ---------
272 static void __cpuinit numa_set_cpumask(int cpu, int enable)
274 int node = cpu_to_node(cpu);
275 cpumask_t *mask;
276 char buf[64];
278 if (node_to_cpumask_map == NULL) {
279 printk(KERN_ERR "node_to_cpumask_map NULL\n");
280 dump_stack();
281 return;
284 mask = &node_to_cpumask_map[node];
285 if (enable)
286 cpu_set(cpu, *mask);
287 else
288 cpu_clear(cpu, *mask);
290 cpulist_scnprintf(buf, sizeof(buf), mask);
291 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
292 enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf);
295 void __cpuinit numa_add_cpu(int cpu)
297 numa_set_cpumask(cpu, 1);
300 void __cpuinit numa_remove_cpu(int cpu)
302 numa_set_cpumask(cpu, 0);
305 int cpu_to_node(int cpu)
307 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
308 printk(KERN_WARNING
309 "cpu_to_node(%d): usage too early!\n", cpu);
310 dump_stack();
311 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
313 return per_cpu(x86_cpu_to_node_map, cpu);
315 EXPORT_SYMBOL(cpu_to_node);
318 * Same function as cpu_to_node() but used if called before the
319 * per_cpu areas are setup.
321 int early_cpu_to_node(int cpu)
323 if (early_per_cpu_ptr(x86_cpu_to_node_map))
324 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
326 if (!per_cpu_offset(cpu)) {
327 printk(KERN_WARNING
328 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
329 dump_stack();
330 return NUMA_NO_NODE;
332 return per_cpu(x86_cpu_to_node_map, cpu);
336 /* empty cpumask */
337 static const cpumask_t cpu_mask_none;
340 * Returns a pointer to the bitmask of CPUs on Node 'node'.
342 const cpumask_t *cpumask_of_node(int node)
344 if (node_to_cpumask_map == NULL) {
345 printk(KERN_WARNING
346 "cpumask_of_node(%d): no node_to_cpumask_map!\n",
347 node);
348 dump_stack();
349 return (const cpumask_t *)&cpu_online_map;
351 if (node >= nr_node_ids) {
352 printk(KERN_WARNING
353 "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
354 node, nr_node_ids);
355 dump_stack();
356 return &cpu_mask_none;
358 return &node_to_cpumask_map[node];
360 EXPORT_SYMBOL(cpumask_of_node);
363 * Returns a bitmask of CPUs on Node 'node'.
365 * Side note: this function creates the returned cpumask on the stack
366 * so with a high NR_CPUS count, excessive stack space is used. The
367 * node_to_cpumask_ptr function should be used whenever possible.
369 cpumask_t node_to_cpumask(int node)
371 if (node_to_cpumask_map == NULL) {
372 printk(KERN_WARNING
373 "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
374 dump_stack();
375 return cpu_online_map;
377 if (node >= nr_node_ids) {
378 printk(KERN_WARNING
379 "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
380 node, nr_node_ids);
381 dump_stack();
382 return cpu_mask_none;
384 return node_to_cpumask_map[node];
386 EXPORT_SYMBOL(node_to_cpumask);
389 * --------- end of debug versions of the numa functions ---------
392 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
394 #endif /* X86_64_NUMA */