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