x86: move back crashkernel back to setup.c
[safe/jmp/linux-2.6] / 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_HAVE_CPUMASK_OF_CPU_MAP
84 cpumask_t *cpumask_of_cpu_map __read_mostly;
85 EXPORT_SYMBOL(cpumask_of_cpu_map);
86
87 /* requires nr_cpu_ids to be initialized */
88 static void __init setup_cpumask_of_cpu(void)
89 {
90         int i;
91
92         /* alloc_bootmem zeroes memory */
93         cpumask_of_cpu_map = alloc_bootmem_low(sizeof(cpumask_t) * nr_cpu_ids);
94         for (i = 0; i < nr_cpu_ids; i++)
95                 cpu_set(i, cpumask_of_cpu_map[i]);
96 }
97 #else
98 static inline void setup_cpumask_of_cpu(void) { }
99 #endif
100
101 #ifdef CONFIG_X86_32
102 /*
103  * Great future not-so-futuristic plan: make i386 and x86_64 do it
104  * the same way
105  */
106 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
107 EXPORT_SYMBOL(__per_cpu_offset);
108 static inline void setup_cpu_pda_map(void) { }
109
110 #elif !defined(CONFIG_SMP)
111 static inline void setup_cpu_pda_map(void) { }
112
113 #else /* CONFIG_SMP && CONFIG_X86_64 */
114
115 /*
116  * Allocate cpu_pda pointer table and array via alloc_bootmem.
117  */
118 static void __init setup_cpu_pda_map(void)
119 {
120         char *pda;
121         struct x8664_pda **new_cpu_pda;
122         unsigned long size;
123         int cpu;
124
125         size = roundup(sizeof(struct x8664_pda), cache_line_size());
126
127         /* allocate cpu_pda array and pointer table */
128         {
129                 unsigned long tsize = nr_cpu_ids * sizeof(void *);
130                 unsigned long asize = size * (nr_cpu_ids - 1);
131
132                 tsize = roundup(tsize, cache_line_size());
133                 new_cpu_pda = alloc_bootmem(tsize + asize);
134                 pda = (char *)new_cpu_pda + tsize;
135         }
136
137         /* initialize pointer table to static pda's */
138         for_each_possible_cpu(cpu) {
139                 if (cpu == 0) {
140                         /* leave boot cpu pda in place */
141                         new_cpu_pda[0] = cpu_pda(0);
142                         continue;
143                 }
144                 new_cpu_pda[cpu] = (struct x8664_pda *)pda;
145                 new_cpu_pda[cpu]->in_bootmem = 1;
146                 pda += size;
147         }
148
149         /* point to new pointer table */
150         _cpu_pda = new_cpu_pda;
151 }
152 #endif
153
154 /*
155  * Great future plan:
156  * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
157  * Always point %gs to its beginning
158  */
159 void __init setup_per_cpu_areas(void)
160 {
161         ssize_t size = PERCPU_ENOUGH_ROOM;
162         char *ptr;
163         int cpu;
164
165         /* no processor from mptable or madt */
166         if (!num_processors)
167                 num_processors = 1;
168
169 #ifdef CONFIG_HOTPLUG_CPU
170         prefill_possible_map();
171 #else
172         nr_cpu_ids = num_processors;
173 #endif
174
175         /* Setup cpu_pda map */
176         setup_cpu_pda_map();
177
178         /* Copy section for each CPU (we discard the original) */
179         size = PERCPU_ENOUGH_ROOM;
180         printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n",
181                           size);
182
183         for_each_possible_cpu(cpu) {
184 #ifndef CONFIG_NEED_MULTIPLE_NODES
185                 ptr = alloc_bootmem_pages(size);
186 #else
187                 int node = early_cpu_to_node(cpu);
188                 if (!node_online(node) || !NODE_DATA(node)) {
189                         ptr = alloc_bootmem_pages(size);
190                         printk(KERN_INFO
191                                "cpu %d has no node %d or node-local memory\n",
192                                 cpu, node);
193                 }
194                 else
195                         ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
196 #endif
197                 per_cpu_offset(cpu) = ptr - __per_cpu_start;
198                 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
199
200         }
201
202         printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d, nr_node_ids %d\n",
203                 NR_CPUS, nr_cpu_ids, nr_node_ids);
204
205         /* Setup percpu data maps */
206         setup_per_cpu_maps();
207
208         /* Setup node to cpumask map */
209         setup_node_to_cpumask_map();
210
211         /* Setup cpumask_of_cpu map */
212         setup_cpumask_of_cpu();
213 }
214
215 #endif
216
217 #ifdef X86_64_NUMA
218
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  */
225 static 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
240         Dprintk(KERN_DEBUG "Node to cpumask map at %p for %d nodes\n",
241                 map, nr_node_ids);
242
243         /* node_to_cpumask() will now work */
244         node_to_cpumask_map = map;
245 }
246
247 void __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
251         if (cpu_pda(cpu) && node != NUMA_NO_NODE)
252                 cpu_pda(cpu)->nodenumber = node;
253
254         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
261                 Dprintk(KERN_INFO "Setting node for non-present cpu %d\n", cpu);
262 }
263
264 void __cpuinit numa_clear_node(int cpu)
265 {
266         numa_set_node(cpu, NUMA_NO_NODE);
267 }
268
269 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
270
271 void __cpuinit numa_add_cpu(int cpu)
272 {
273         cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
274 }
275
276 void __cpuinit numa_remove_cpu(int cpu)
277 {
278         cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
279 }
280
281 #else /* CONFIG_DEBUG_PER_CPU_MAPS */
282
283 /*
284  * --------- debug versions of the numa functions ---------
285  */
286 static 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
304         cpulist_scnprintf(buf, sizeof(buf), *mask);
305         printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
306                 enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf);
307  }
308
309 void __cpuinit numa_add_cpu(int cpu)
310 {
311         numa_set_cpumask(cpu, 1);
312 }
313
314 void __cpuinit numa_remove_cpu(int cpu)
315 {
316         numa_set_cpumask(cpu, 0);
317 }
318
319 int 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 }
329 EXPORT_SYMBOL(cpu_to_node);
330
331 /*
332  * Same function as cpu_to_node() but used if called before the
333  * per_cpu areas are setup.
334  */
335 int 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);
343                 dump_stack();
344                 return NUMA_NO_NODE;
345         }
346         return per_cpu(x86_cpu_to_node_map, cpu);
347 }
348
349 /*
350  * Returns a pointer to the bitmask of CPUs on Node 'node'.
351  */
352 cpumask_t *_node_to_cpumask_ptr(int node)
353 {
354         if (node_to_cpumask_map == NULL) {
355                 printk(KERN_WARNING
356                         "_node_to_cpumask_ptr(%d): no node_to_cpumask_map!\n",
357                         node);
358                 dump_stack();
359                 return &cpu_online_map;
360         }
361         BUG_ON(node >= nr_node_ids);
362         return &node_to_cpumask_map[node];
363 }
364 EXPORT_SYMBOL(_node_to_cpumask_ptr);
365
366 /*
367  * Returns a bitmask of CPUs on Node 'node'.
368  */
369 cpumask_t node_to_cpumask(int node)
370 {
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;
376         }
377         BUG_ON(node >= nr_node_ids);
378         return node_to_cpumask_map[node];
379 }
380 EXPORT_SYMBOL(node_to_cpumask);
381
382 /*
383  * --------- end of debug versions of the numa functions ---------
384  */
385
386 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
387
388 #endif /* X86_64_NUMA */
389
390 static struct resource standard_io_resources[] = {
391         { .name = "dma1", .start = 0x00, .end = 0x1f,
392                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
393         { .name = "pic1", .start = 0x20, .end = 0x21,
394                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
395         { .name = "timer0", .start = 0x40, .end = 0x43,
396                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
397         { .name = "timer1", .start = 0x50, .end = 0x53,
398                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
399         { .name = "keyboard", .start = 0x60, .end = 0x60,
400                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
401         { .name = "keyboard", .start = 0x64, .end = 0x64,
402                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
403         { .name = "dma page reg", .start = 0x80, .end = 0x8f,
404                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
405         { .name = "pic2", .start = 0xa0, .end = 0xa1,
406                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
407         { .name = "dma2", .start = 0xc0, .end = 0xdf,
408                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
409         { .name = "fpu", .start = 0xf0, .end = 0xff,
410                 .flags = IORESOURCE_BUSY | IORESOURCE_IO }
411 };
412
413 void __init reserve_standard_io_resources(void)
414 {
415         int i;
416
417         /* request I/O space for devices used on all i[345]86 PCs */
418         for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
419                 request_resource(&ioport_resource, &standard_io_resources[i]);
420
421 }
422
423 #ifdef CONFIG_PROC_VMCORE
424 /* elfcorehdr= specifies the location of elf core header
425  * stored by the crashed kernel. This option will be passed
426  * by kexec loader to the capture kernel.
427  */
428 static int __init setup_elfcorehdr(char *arg)
429 {
430         char *end;
431         if (!arg)
432                 return -EINVAL;
433         elfcorehdr_addr = memparse(arg, &end);
434         return end > arg ? 0 : -EINVAL;
435 }
436 early_param("elfcorehdr", setup_elfcorehdr);
437 #endif
438
439
440