]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/setup_percpu.c
add per_cpu_dyn_array support
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / setup_percpu.c
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>
17
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);
25
26 /* Bitmask of physically existing CPUs */
27 physid_mask_t phys_cpu_present_map;
28 #endif
29
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
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);
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) { }
52 #endif
53
54 #if defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) && defined(CONFIG_X86_SMP)
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) {
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
73         }
74
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
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);
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 }
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 {
143         ssize_t size, old_size;
144         char *ptr;
145         int cpu;
146
147         /* Setup cpu_pda map */
148         setup_cpu_pda_map();
149
150         /* Copy section for each CPU (we discard the original) */
151         old_size = PERCPU_ENOUGH_ROOM;
152         size = old_size + per_cpu_dyn_array_size();
153         printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n",
154                           size);
155
156         for_each_possible_cpu(cpu) {
157 #ifndef CONFIG_NEED_MULTIPLE_NODES
158                 ptr = alloc_bootmem_pages(size);
159 #else
160                 int node = early_cpu_to_node(cpu);
161                 if (!node_online(node) || !NODE_DATA(node)) {
162                         ptr = alloc_bootmem_pages(size);
163                         printk(KERN_INFO
164                                "cpu %d has no node %d or node-local memory\n",
165                                 cpu, node);
166                         if (ptr)
167                                 printk(KERN_DEBUG "per cpu data for cpu%d at %016lx\n",
168                                          cpu, __pa(ptr));
169                 }
170                 else {
171                         ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
172                         if (ptr)
173                                 printk(KERN_DEBUG "per cpu data for cpu%d on node%d at %016lx\n",
174                                          cpu, node, __pa(ptr));
175                 }
176 #endif
177                 per_cpu_offset(cpu) = ptr - __per_cpu_start;
178                 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
179
180                 per_cpu_alloc_dyn_array(cpu, ptr + old_size);
181
182         }
183
184         printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d, nr_node_ids %d\n",
185                 NR_CPUS, nr_cpu_ids, nr_node_ids);
186
187         /* Setup percpu data maps */
188         setup_per_cpu_maps();
189
190         /* Setup node to cpumask map */
191         setup_node_to_cpumask_map();
192 }
193
194 #endif
195
196 #ifdef X86_64_NUMA
197
198 /*
199  * Allocate node_to_cpumask_map based on number of available nodes
200  * Requires node_possible_map to be valid.
201  *
202  * Note: node_to_cpumask() is not valid until after this is done.
203  */
204 static void __init setup_node_to_cpumask_map(void)
205 {
206         unsigned int node, num = 0;
207         cpumask_t *map;
208
209         /* setup nr_node_ids if not done yet */
210         if (nr_node_ids == MAX_NUMNODES) {
211                 for_each_node_mask(node, node_possible_map)
212                         num = node;
213                 nr_node_ids = num + 1;
214         }
215
216         /* allocate the map */
217         map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
218
219         pr_debug(KERN_DEBUG "Node to cpumask map at %p for %d nodes\n",
220                  map, nr_node_ids);
221
222         /* node_to_cpumask() will now work */
223         node_to_cpumask_map = map;
224 }
225
226 void __cpuinit numa_set_node(int cpu, int node)
227 {
228         int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
229
230         if (cpu_pda(cpu) && node != NUMA_NO_NODE)
231                 cpu_pda(cpu)->nodenumber = node;
232
233         if (cpu_to_node_map)
234                 cpu_to_node_map[cpu] = node;
235
236         else if (per_cpu_offset(cpu))
237                 per_cpu(x86_cpu_to_node_map, cpu) = node;
238
239         else
240                 pr_debug("Setting node for non-present cpu %d\n", cpu);
241 }
242
243 void __cpuinit numa_clear_node(int cpu)
244 {
245         numa_set_node(cpu, NUMA_NO_NODE);
246 }
247
248 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
249
250 void __cpuinit numa_add_cpu(int cpu)
251 {
252         cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
253 }
254
255 void __cpuinit numa_remove_cpu(int cpu)
256 {
257         cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
258 }
259
260 #else /* CONFIG_DEBUG_PER_CPU_MAPS */
261
262 /*
263  * --------- debug versions of the numa functions ---------
264  */
265 static void __cpuinit numa_set_cpumask(int cpu, int enable)
266 {
267         int node = cpu_to_node(cpu);
268         cpumask_t *mask;
269         char buf[64];
270
271         if (node_to_cpumask_map == NULL) {
272                 printk(KERN_ERR "node_to_cpumask_map NULL\n");
273                 dump_stack();
274                 return;
275         }
276
277         mask = &node_to_cpumask_map[node];
278         if (enable)
279                 cpu_set(cpu, *mask);
280         else
281                 cpu_clear(cpu, *mask);
282
283         cpulist_scnprintf(buf, sizeof(buf), *mask);
284         printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
285                 enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf);
286  }
287
288 void __cpuinit numa_add_cpu(int cpu)
289 {
290         numa_set_cpumask(cpu, 1);
291 }
292
293 void __cpuinit numa_remove_cpu(int cpu)
294 {
295         numa_set_cpumask(cpu, 0);
296 }
297
298 int cpu_to_node(int cpu)
299 {
300         if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
301                 printk(KERN_WARNING
302                         "cpu_to_node(%d): usage too early!\n", cpu);
303                 dump_stack();
304                 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
305         }
306         return per_cpu(x86_cpu_to_node_map, cpu);
307 }
308 EXPORT_SYMBOL(cpu_to_node);
309
310 /*
311  * Same function as cpu_to_node() but used if called before the
312  * per_cpu areas are setup.
313  */
314 int early_cpu_to_node(int cpu)
315 {
316         if (early_per_cpu_ptr(x86_cpu_to_node_map))
317                 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
318
319         if (!per_cpu_offset(cpu)) {
320                 printk(KERN_WARNING
321                         "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
322                 dump_stack();
323                 return NUMA_NO_NODE;
324         }
325         return per_cpu(x86_cpu_to_node_map, cpu);
326 }
327
328
329 /* empty cpumask */
330 static const cpumask_t cpu_mask_none;
331
332 /*
333  * Returns a pointer to the bitmask of CPUs on Node 'node'.
334  */
335 const cpumask_t *_node_to_cpumask_ptr(int node)
336 {
337         if (node_to_cpumask_map == NULL) {
338                 printk(KERN_WARNING
339                         "_node_to_cpumask_ptr(%d): no node_to_cpumask_map!\n",
340                         node);
341                 dump_stack();
342                 return (const cpumask_t *)&cpu_online_map;
343         }
344         if (node >= nr_node_ids) {
345                 printk(KERN_WARNING
346                         "_node_to_cpumask_ptr(%d): node > nr_node_ids(%d)\n",
347                         node, nr_node_ids);
348                 dump_stack();
349                 return &cpu_mask_none;
350         }
351         return &node_to_cpumask_map[node];
352 }
353 EXPORT_SYMBOL(_node_to_cpumask_ptr);
354
355 /*
356  * Returns a bitmask of CPUs on Node 'node'.
357  *
358  * Side note: this function creates the returned cpumask on the stack
359  * so with a high NR_CPUS count, excessive stack space is used.  The
360  * node_to_cpumask_ptr function should be used whenever possible.
361  */
362 cpumask_t node_to_cpumask(int node)
363 {
364         if (node_to_cpumask_map == NULL) {
365                 printk(KERN_WARNING
366                         "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
367                 dump_stack();
368                 return cpu_online_map;
369         }
370         if (node >= nr_node_ids) {
371                 printk(KERN_WARNING
372                         "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
373                         node, nr_node_ids);
374                 dump_stack();
375                 return cpu_mask_none;
376         }
377         return node_to_cpumask_map[node];
378 }
379 EXPORT_SYMBOL(node_to_cpumask);
380
381 /*
382  * --------- end of debug versions of the numa functions ---------
383  */
384
385 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
386
387 #endif /* X86_64_NUMA */
388