x86: rename setup.c to setup_percpu.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 #ifndef CONFIG_DEBUG_BOOT_PARAMS
19 struct boot_params __initdata boot_params;
20 #else
21 struct boot_params boot_params;
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 unsigned int max_physical_apicid;
30 EXPORT_SYMBOL(boot_cpu_physical_apicid);
31
32 /* Bitmask of physically existing CPUs */
33 physid_mask_t phys_cpu_present_map;
34 #endif
35
36 /* map cpu index to physical APIC ID */
37 DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
38 DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
39 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
40 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
41
42 #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
43 #define X86_64_NUMA     1
44
45 /* map cpu index to node index */
46 DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
47 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
48
49 /* which logical CPUs are on which nodes */
50 cpumask_t *node_to_cpumask_map;
51 EXPORT_SYMBOL(node_to_cpumask_map);
52
53 /* setup node_to_cpumask_map */
54 static void __init setup_node_to_cpumask_map(void);
55
56 #else
57 static inline void setup_node_to_cpumask_map(void) { }
58 #endif
59
60 #if defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) && defined(CONFIG_X86_SMP)
61 /*
62  * Copy data used in early init routines from the initial arrays to the
63  * per cpu data areas.  These arrays then become expendable and the
64  * *_early_ptr's are zeroed indicating that the static arrays are gone.
65  */
66 static void __init setup_per_cpu_maps(void)
67 {
68         int cpu;
69
70         for_each_possible_cpu(cpu) {
71                 per_cpu(x86_cpu_to_apicid, cpu) =
72                                 early_per_cpu_map(x86_cpu_to_apicid, cpu);
73                 per_cpu(x86_bios_cpu_apicid, cpu) =
74                                 early_per_cpu_map(x86_bios_cpu_apicid, cpu);
75 #ifdef X86_64_NUMA
76                 per_cpu(x86_cpu_to_node_map, cpu) =
77                                 early_per_cpu_map(x86_cpu_to_node_map, cpu);
78 #endif
79         }
80
81         /* indicate the early static arrays will soon be gone */
82         early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
83         early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
84 #ifdef X86_64_NUMA
85         early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
86 #endif
87 }
88
89 #ifdef CONFIG_HAVE_CPUMASK_OF_CPU_MAP
90 cpumask_t *cpumask_of_cpu_map __read_mostly;
91 EXPORT_SYMBOL(cpumask_of_cpu_map);
92
93 /* requires nr_cpu_ids to be initialized */
94 static void __init setup_cpumask_of_cpu(void)
95 {
96         int i;
97
98         /* alloc_bootmem zeroes memory */
99         cpumask_of_cpu_map = alloc_bootmem_low(sizeof(cpumask_t) * nr_cpu_ids);
100         for (i = 0; i < nr_cpu_ids; i++)
101                 cpu_set(i, cpumask_of_cpu_map[i]);
102 }
103 #else
104 static inline void setup_cpumask_of_cpu(void) { }
105 #endif
106
107 #ifdef CONFIG_X86_32
108 /*
109  * Great future not-so-futuristic plan: make i386 and x86_64 do it
110  * the same way
111  */
112 unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
113 EXPORT_SYMBOL(__per_cpu_offset);
114 static inline void setup_cpu_pda_map(void) { }
115
116 #elif !defined(CONFIG_SMP)
117 static inline void setup_cpu_pda_map(void) { }
118
119 #else /* CONFIG_SMP && CONFIG_X86_64 */
120
121 /*
122  * Allocate cpu_pda pointer table and array via alloc_bootmem.
123  */
124 static void __init setup_cpu_pda_map(void)
125 {
126         char *pda;
127         struct x8664_pda **new_cpu_pda;
128         unsigned long size;
129         int cpu;
130
131         size = roundup(sizeof(struct x8664_pda), cache_line_size());
132
133         /* allocate cpu_pda array and pointer table */
134         {
135                 unsigned long tsize = nr_cpu_ids * sizeof(void *);
136                 unsigned long asize = size * (nr_cpu_ids - 1);
137
138                 tsize = roundup(tsize, cache_line_size());
139                 new_cpu_pda = alloc_bootmem(tsize + asize);
140                 pda = (char *)new_cpu_pda + tsize;
141         }
142
143         /* initialize pointer table to static pda's */
144         for_each_possible_cpu(cpu) {
145                 if (cpu == 0) {
146                         /* leave boot cpu pda in place */
147                         new_cpu_pda[0] = cpu_pda(0);
148                         continue;
149                 }
150                 new_cpu_pda[cpu] = (struct x8664_pda *)pda;
151                 new_cpu_pda[cpu]->in_bootmem = 1;
152                 pda += size;
153         }
154
155         /* point to new pointer table */
156         _cpu_pda = new_cpu_pda;
157 }
158 #endif
159
160 /*
161  * Great future plan:
162  * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
163  * Always point %gs to its beginning
164  */
165 void __init setup_per_cpu_areas(void)
166 {
167         ssize_t size = PERCPU_ENOUGH_ROOM;
168         char *ptr;
169         int cpu;
170
171         /* no processor from mptable or madt */
172         if (!num_processors)
173                 num_processors = 1;
174
175 #ifdef CONFIG_HOTPLUG_CPU
176         prefill_possible_map();
177 #else
178         nr_cpu_ids = num_processors;
179 #endif
180
181         /* Setup cpu_pda map */
182         setup_cpu_pda_map();
183
184         /* Copy section for each CPU (we discard the original) */
185         size = PERCPU_ENOUGH_ROOM;
186         printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n",
187                           size);
188
189         for_each_possible_cpu(cpu) {
190 #ifndef CONFIG_NEED_MULTIPLE_NODES
191                 ptr = alloc_bootmem_pages(size);
192 #else
193                 int node = early_cpu_to_node(cpu);
194                 if (!node_online(node) || !NODE_DATA(node)) {
195                         ptr = alloc_bootmem_pages(size);
196                         printk(KERN_INFO
197                                "cpu %d has no node %d or node-local memory\n",
198                                 cpu, node);
199                 }
200                 else
201                         ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
202 #endif
203                 per_cpu_offset(cpu) = ptr - __per_cpu_start;
204                 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
205
206         }
207
208         printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d, nr_node_ids %d\n",
209                 NR_CPUS, nr_cpu_ids, nr_node_ids);
210
211         /* Setup percpu data maps */
212         setup_per_cpu_maps();
213
214         /* Setup node to cpumask map */
215         setup_node_to_cpumask_map();
216
217         /* Setup cpumask_of_cpu map */
218         setup_cpumask_of_cpu();
219 }
220
221 #endif
222
223 void __init parse_setup_data(void)
224 {
225         struct setup_data *data;
226         u64 pa_data;
227
228         if (boot_params.hdr.version < 0x0209)
229                 return;
230         pa_data = boot_params.hdr.setup_data;
231         while (pa_data) {
232                 data = early_ioremap(pa_data, PAGE_SIZE);
233                 switch (data->type) {
234                 case SETUP_E820_EXT:
235                         parse_e820_ext(data, pa_data);
236                         break;
237                 default:
238                         break;
239                 }
240 #ifndef CONFIG_DEBUG_BOOT_PARAMS
241                 free_early(pa_data, pa_data+sizeof(*data)+data->len);
242 #endif
243                 pa_data = data->next;
244                 early_iounmap(data, PAGE_SIZE);
245         }
246 }
247
248 #ifdef X86_64_NUMA
249
250 /*
251  * Allocate node_to_cpumask_map based on number of available nodes
252  * Requires node_possible_map to be valid.
253  *
254  * Note: node_to_cpumask() is not valid until after this is done.
255  */
256 static void __init setup_node_to_cpumask_map(void)
257 {
258         unsigned int node, num = 0;
259         cpumask_t *map;
260
261         /* setup nr_node_ids if not done yet */
262         if (nr_node_ids == MAX_NUMNODES) {
263                 for_each_node_mask(node, node_possible_map)
264                         num = node;
265                 nr_node_ids = num + 1;
266         }
267
268         /* allocate the map */
269         map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
270
271         Dprintk(KERN_DEBUG "Node to cpumask map at %p for %d nodes\n",
272                 map, nr_node_ids);
273
274         /* node_to_cpumask() will now work */
275         node_to_cpumask_map = map;
276 }
277
278 void __cpuinit numa_set_node(int cpu, int node)
279 {
280         int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
281
282         if (cpu_pda(cpu) && node != NUMA_NO_NODE)
283                 cpu_pda(cpu)->nodenumber = node;
284
285         if (cpu_to_node_map)
286                 cpu_to_node_map[cpu] = node;
287
288         else if (per_cpu_offset(cpu))
289                 per_cpu(x86_cpu_to_node_map, cpu) = node;
290
291         else
292                 Dprintk(KERN_INFO "Setting node for non-present cpu %d\n", cpu);
293 }
294
295 void __cpuinit numa_clear_node(int cpu)
296 {
297         numa_set_node(cpu, NUMA_NO_NODE);
298 }
299
300 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
301
302 void __cpuinit numa_add_cpu(int cpu)
303 {
304         cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
305 }
306
307 void __cpuinit numa_remove_cpu(int cpu)
308 {
309         cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
310 }
311
312 #else /* CONFIG_DEBUG_PER_CPU_MAPS */
313
314 /*
315  * --------- debug versions of the numa functions ---------
316  */
317 static void __cpuinit numa_set_cpumask(int cpu, int enable)
318 {
319         int node = cpu_to_node(cpu);
320         cpumask_t *mask;
321         char buf[64];
322
323         if (node_to_cpumask_map == NULL) {
324                 printk(KERN_ERR "node_to_cpumask_map NULL\n");
325                 dump_stack();
326                 return;
327         }
328
329         mask = &node_to_cpumask_map[node];
330         if (enable)
331                 cpu_set(cpu, *mask);
332         else
333                 cpu_clear(cpu, *mask);
334
335         cpulist_scnprintf(buf, sizeof(buf), *mask);
336         printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
337                 enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf);
338  }
339
340 void __cpuinit numa_add_cpu(int cpu)
341 {
342         numa_set_cpumask(cpu, 1);
343 }
344
345 void __cpuinit numa_remove_cpu(int cpu)
346 {
347         numa_set_cpumask(cpu, 0);
348 }
349
350 int cpu_to_node(int cpu)
351 {
352         if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
353                 printk(KERN_WARNING
354                         "cpu_to_node(%d): usage too early!\n", cpu);
355                 dump_stack();
356                 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
357         }
358         return per_cpu(x86_cpu_to_node_map, cpu);
359 }
360 EXPORT_SYMBOL(cpu_to_node);
361
362 /*
363  * Same function as cpu_to_node() but used if called before the
364  * per_cpu areas are setup.
365  */
366 int early_cpu_to_node(int cpu)
367 {
368         if (early_per_cpu_ptr(x86_cpu_to_node_map))
369                 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
370
371         if (!per_cpu_offset(cpu)) {
372                 printk(KERN_WARNING
373                         "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
374                 dump_stack();
375                 return NUMA_NO_NODE;
376         }
377         return per_cpu(x86_cpu_to_node_map, cpu);
378 }
379
380 /*
381  * Returns a pointer to the bitmask of CPUs on Node 'node'.
382  */
383 cpumask_t *_node_to_cpumask_ptr(int node)
384 {
385         if (node_to_cpumask_map == NULL) {
386                 printk(KERN_WARNING
387                         "_node_to_cpumask_ptr(%d): no node_to_cpumask_map!\n",
388                         node);
389                 dump_stack();
390                 return &cpu_online_map;
391         }
392         BUG_ON(node >= nr_node_ids);
393         return &node_to_cpumask_map[node];
394 }
395 EXPORT_SYMBOL(_node_to_cpumask_ptr);
396
397 /*
398  * Returns a bitmask of CPUs on Node 'node'.
399  */
400 cpumask_t node_to_cpumask(int node)
401 {
402         if (node_to_cpumask_map == NULL) {
403                 printk(KERN_WARNING
404                         "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
405                 dump_stack();
406                 return cpu_online_map;
407         }
408         BUG_ON(node >= nr_node_ids);
409         return node_to_cpumask_map[node];
410 }
411 EXPORT_SYMBOL(node_to_cpumask);
412
413 /*
414  * --------- end of debug versions of the numa functions ---------
415  */
416
417 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
418
419 #endif /* X86_64_NUMA */
420
421
422 /*
423  * --------- Crashkernel reservation ------------------------------
424  */
425
426 static inline unsigned long long get_total_mem(void)
427 {
428         unsigned long long total;
429
430         total = max_low_pfn - min_low_pfn;
431 #ifdef CONFIG_HIGHMEM
432         total += highend_pfn - highstart_pfn;
433 #endif
434
435         return total << PAGE_SHIFT;
436 }
437
438 #ifdef CONFIG_KEXEC
439 void __init reserve_crashkernel(void)
440 {
441         unsigned long long total_mem;
442         unsigned long long crash_size, crash_base;
443         int ret;
444
445         total_mem = get_total_mem();
446
447         ret = parse_crashkernel(boot_command_line, total_mem,
448                         &crash_size, &crash_base);
449         if (ret == 0 && crash_size > 0) {
450                 if (crash_base <= 0) {
451                         printk(KERN_INFO "crashkernel reservation failed - "
452                                         "you have to specify a base address\n");
453                         return;
454                 }
455
456                 if (reserve_bootmem_generic(crash_base, crash_size,
457                                         BOOTMEM_EXCLUSIVE) < 0) {
458                         printk(KERN_INFO "crashkernel reservation failed - "
459                                         "memory is in use\n");
460                         return;
461                 }
462
463                 printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
464                                 "for crashkernel (System RAM: %ldMB)\n",
465                                 (unsigned long)(crash_size >> 20),
466                                 (unsigned long)(crash_base >> 20),
467                                 (unsigned long)(total_mem >> 20));
468
469                 crashk_res.start = crash_base;
470                 crashk_res.end   = crash_base + crash_size - 1;
471                 insert_resource(&iomem_resource, &crashk_res);
472         }
473 }
474 #else
475 void __init reserve_crashkernel(void)
476 {}
477 #endif
478 static struct resource standard_io_resources[] = {
479         { .name = "dma1", .start = 0x00, .end = 0x1f,
480                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
481         { .name = "pic1", .start = 0x20, .end = 0x21,
482                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
483         { .name = "timer0", .start = 0x40, .end = 0x43,
484                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
485         { .name = "timer1", .start = 0x50, .end = 0x53,
486                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
487         { .name = "keyboard", .start = 0x60, .end = 0x60,
488                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
489         { .name = "keyboard", .start = 0x64, .end = 0x64,
490                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
491         { .name = "dma page reg", .start = 0x80, .end = 0x8f,
492                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
493         { .name = "pic2", .start = 0xa0, .end = 0xa1,
494                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
495         { .name = "dma2", .start = 0xc0, .end = 0xdf,
496                 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
497         { .name = "fpu", .start = 0xf0, .end = 0xff,
498                 .flags = IORESOURCE_BUSY | IORESOURCE_IO }
499 };
500
501 void __init reserve_standard_io_resources(void)
502 {
503         int i;
504
505         /* request I/O space for devices used on all i[345]86 PCs */
506         for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
507                 request_resource(&ioport_resource, &standard_io_resources[i]);
508
509 }
510
511 #ifdef CONFIG_PROC_VMCORE
512 /* elfcorehdr= specifies the location of elf core header
513  * stored by the crashed kernel. This option will be passed
514  * by kexec loader to the capture kernel.
515  */
516 static int __init setup_elfcorehdr(char *arg)
517 {
518         char *end;
519         if (!arg)
520                 return -EINVAL;
521         elfcorehdr_addr = memparse(arg, &end);
522         return end > arg ? 0 : -EINVAL;
523 }
524 early_param("elfcorehdr", setup_elfcorehdr);
525 #endif
526
527
528