2 * Xen SMP booting functions
4 * See arch/i386/kernel/smpboot.c for copyright and credits for derived
5 * portions of this file.
8 #include <linux/module.h>
9 #include <linux/config.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/smp_lock.h>
16 #include <linux/irq.h>
17 #include <linux/bootmem.h>
18 #include <linux/notifier.h>
19 #include <linux/cpu.h>
20 #include <linux/percpu.h>
22 #include <asm/arch_hooks.h>
23 #include <asm/pgalloc.h>
24 #include <xen/evtchn.h>
25 #include <xen/interface/vcpu.h>
26 #include <xen/cpu_hotplug.h>
27 #include <xen/xenbus.h>
29 extern irqreturn_t smp_reschedule_interrupt(int, void *, struct pt_regs *);
30 extern irqreturn_t smp_call_function_interrupt(int, void *, struct pt_regs *);
32 extern void local_setup_timer(unsigned int cpu);
33 extern void local_teardown_timer(unsigned int cpu);
35 extern void hypervisor_callback(void);
36 extern void failsafe_callback(void);
37 extern void system_call(void);
38 extern void smp_trap_init(trap_info_t *);
40 /* Number of siblings per CPU package */
41 int smp_num_siblings = 1;
42 int phys_proc_id[NR_CPUS]; /* Package ID of each logical CPU */
43 EXPORT_SYMBOL(phys_proc_id);
44 int cpu_core_id[NR_CPUS]; /* Core ID of each logical CPU */
45 EXPORT_SYMBOL(cpu_core_id);
47 int cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
48 #elif defined(__x86_64__)
49 u8 cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
52 cpumask_t cpu_online_map;
53 EXPORT_SYMBOL(cpu_online_map);
54 cpumask_t cpu_possible_map;
55 EXPORT_SYMBOL(cpu_possible_map);
57 struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
58 EXPORT_SYMBOL(cpu_data);
60 #ifdef CONFIG_HOTPLUG_CPU
61 DEFINE_PER_CPU(int, cpu_state) = { 0 };
64 static DEFINE_PER_CPU(int, resched_irq);
65 static DEFINE_PER_CPU(int, callfunc_irq);
66 static char resched_name[NR_CPUS][15];
67 static char callfunc_name[NR_CPUS][15];
69 u8 cpu_2_logical_apicid[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
73 cpumask_t cpu_sibling_map[NR_CPUS] __cacheline_aligned;
74 cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
75 EXPORT_SYMBOL(cpu_core_map);
78 u8 x86_cpu_to_apicid[NR_CPUS] = { [0 ... NR_CPUS-1] = 0xff };
79 EXPORT_SYMBOL(x86_cpu_to_apicid);
80 #elif !defined(CONFIG_X86_IO_APIC)
81 unsigned int maxcpus = NR_CPUS;
84 void __init prefill_possible_map(void)
88 for (i = 0; i < NR_CPUS; i++) {
89 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
91 cpu_set(i, cpu_possible_map);
95 void __init smp_alloc_memory(void)
100 set_cpu_sibling_map(int cpu)
102 phys_proc_id[cpu] = cpu;
103 cpu_core_id[cpu] = 0;
105 cpu_sibling_map[cpu] = cpumask_of_cpu(cpu);
106 cpu_core_map[cpu] = cpumask_of_cpu(cpu);
108 cpu_data[cpu].booted_cores = 1;
111 static void xen_smp_intr_init(unsigned int cpu)
113 sprintf(resched_name[cpu], "resched%d", cpu);
114 per_cpu(resched_irq, cpu) =
115 bind_ipi_to_irqhandler(
118 smp_reschedule_interrupt,
122 BUG_ON(per_cpu(resched_irq, cpu) < 0);
124 sprintf(callfunc_name[cpu], "callfunc%d", cpu);
125 per_cpu(callfunc_irq, cpu) =
126 bind_ipi_to_irqhandler(
127 CALL_FUNCTION_VECTOR,
129 smp_call_function_interrupt,
133 BUG_ON(per_cpu(callfunc_irq, cpu) < 0);
136 local_setup_timer(cpu);
139 #ifdef CONFIG_HOTPLUG_CPU
140 static void xen_smp_intr_exit(unsigned int cpu)
143 local_teardown_timer(cpu);
145 unbind_from_irqhandler(per_cpu(resched_irq, cpu), NULL);
146 unbind_from_irqhandler(per_cpu(callfunc_irq, cpu), NULL);
150 void cpu_bringup(void)
153 touch_softlockup_watchdog();
158 static void cpu_bringup_and_idle(void)
164 void cpu_initialize_context(unsigned int cpu)
166 vcpu_guest_context_t ctxt;
167 struct task_struct *idle = idle_task(cpu);
169 struct desc_ptr *gdt_descr = &cpu_gdt_descr[cpu];
171 struct Xgt_desc_struct *gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
177 memset(&ctxt, 0, sizeof(ctxt));
179 ctxt.flags = VGCF_IN_KERNEL;
180 ctxt.user_regs.ds = __USER_DS;
181 ctxt.user_regs.es = __USER_DS;
182 ctxt.user_regs.fs = 0;
183 ctxt.user_regs.gs = 0;
184 ctxt.user_regs.ss = __KERNEL_DS;
185 ctxt.user_regs.eip = (unsigned long)cpu_bringup_and_idle;
186 ctxt.user_regs.eflags = X86_EFLAGS_IF | 0x1000; /* IOPL_RING1 */
188 memset(&ctxt.fpu_ctxt, 0, sizeof(ctxt.fpu_ctxt));
190 smp_trap_init(ctxt.trap_ctxt);
194 ctxt.gdt_frames[0] = virt_to_mfn(gdt_descr->address);
195 ctxt.gdt_ents = gdt_descr->size / 8;
198 ctxt.user_regs.cs = __KERNEL_CS;
199 ctxt.user_regs.esp = idle->thread.esp0 - sizeof(struct pt_regs);
201 ctxt.kernel_ss = __KERNEL_DS;
202 ctxt.kernel_sp = idle->thread.esp0;
204 ctxt.event_callback_cs = __KERNEL_CS;
205 ctxt.event_callback_eip = (unsigned long)hypervisor_callback;
206 ctxt.failsafe_callback_cs = __KERNEL_CS;
207 ctxt.failsafe_callback_eip = (unsigned long)failsafe_callback;
209 ctxt.ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
210 #else /* __x86_64__ */
211 ctxt.user_regs.cs = __KERNEL_CS;
212 ctxt.user_regs.esp = idle->thread.rsp0 - sizeof(struct pt_regs);
214 ctxt.kernel_ss = __KERNEL_DS;
215 ctxt.kernel_sp = idle->thread.rsp0;
217 ctxt.event_callback_eip = (unsigned long)hypervisor_callback;
218 ctxt.failsafe_callback_eip = (unsigned long)failsafe_callback;
219 ctxt.syscall_callback_eip = (unsigned long)system_call;
221 ctxt.ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(init_level4_pgt));
223 ctxt.gs_base_kernel = (unsigned long)(cpu_pda(cpu));
226 BUG_ON(HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, &ctxt));
229 void __init smp_prepare_cpus(unsigned int max_cpus)
232 struct task_struct *idle;
234 struct desc_ptr *gdt_descr;
236 struct Xgt_desc_struct *gdt_descr;
239 boot_cpu_data.apicid = 0;
240 cpu_data[0] = boot_cpu_data;
242 cpu_2_logical_apicid[0] = 0;
243 x86_cpu_to_apicid[0] = 0;
245 current_thread_info()->cpu = 0;
247 for (cpu = 0; cpu < NR_CPUS; cpu++) {
248 cpus_clear(cpu_sibling_map[cpu]);
249 cpus_clear(cpu_core_map[cpu]);
252 set_cpu_sibling_map(0);
254 xen_smp_intr_init(0);
256 for_each_cpu_mask (cpu, cpu_possible_map) {
261 gdt_descr = &cpu_gdt_descr[cpu];
263 gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
265 gdt_descr->address = get_zeroed_page(GFP_KERNEL);
266 if (unlikely(!gdt_descr->address)) {
267 printk(KERN_CRIT "CPU%d failed to allocate GDT\n", cpu);
270 gdt_descr->size = GDT_SIZE;
271 memcpy((void *)gdt_descr->address, cpu_gdt_table, GDT_SIZE);
273 (void *)gdt_descr->address,
274 XENFEAT_writable_descriptor_tables);
276 cpu_data[cpu] = boot_cpu_data;
277 cpu_data[cpu].apicid = cpu;
279 cpu_2_logical_apicid[cpu] = cpu;
280 x86_cpu_to_apicid[cpu] = cpu;
282 idle = fork_idle(cpu);
284 panic("failed fork for CPU %d", cpu);
287 cpu_pda(cpu)->pcurrent = idle;
288 cpu_pda(cpu)->cpunumber = cpu;
289 clear_ti_thread_flag(idle->thread_info, TIF_FORK);
294 #ifdef CONFIG_HOTPLUG_CPU
295 if (xen_start_info->flags & SIF_INITDOMAIN)
296 cpu_set(cpu, cpu_present_map);
298 cpu_set(cpu, cpu_present_map);
301 cpu_initialize_context(cpu);
304 init_xenbus_allowed_cpumask();
306 /* Currently, Xen gives no dynamic NUMA/HT info. */
307 for (cpu = 1; cpu < NR_CPUS; cpu++) {
308 cpu_sibling_map[cpu] = cpumask_of_cpu(cpu);
309 cpu_core_map[cpu] = cpumask_of_cpu(cpu);
312 #ifdef CONFIG_X86_IO_APIC
314 * Here we can be sure that there is an IO-APIC in the system. Let's
317 if (!skip_ioapic_setup && nr_ioapics)
322 void __devinit smp_prepare_boot_cpu(void)
326 #ifdef CONFIG_HOTPLUG_CPU
329 * Initialize cpu_present_map late to skip SMP boot code in init/main.c.
330 * But do it early enough to catch critical for_each_present_cpu() loops
331 * in i386-specific code.
333 static int __init initialize_cpu_present_map(void)
335 cpu_present_map = cpu_possible_map;
338 core_initcall(initialize_cpu_present_map);
341 remove_siblinginfo(int cpu)
343 phys_proc_id[cpu] = BAD_APICID;
344 cpu_core_id[cpu] = BAD_APICID;
346 cpus_clear(cpu_sibling_map[cpu]);
347 cpus_clear(cpu_core_map[cpu]);
349 cpu_data[cpu].booted_cores = 0;
352 int __cpu_disable(void)
354 cpumask_t map = cpu_online_map;
355 int cpu = smp_processor_id();
360 remove_siblinginfo(cpu);
364 cpu_clear(cpu, cpu_online_map);
369 void __cpu_die(unsigned int cpu)
371 while (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) {
372 current->state = TASK_UNINTERRUPTIBLE;
373 schedule_timeout(HZ/10);
376 xen_smp_intr_exit(cpu);
379 if (num_online_cpus() == 1)
380 alternatives_smp_switch(0);
384 #else /* !CONFIG_HOTPLUG_CPU */
386 int __cpu_disable(void)
391 void __cpu_die(unsigned int cpu)
396 #endif /* CONFIG_HOTPLUG_CPU */
398 int __devinit __cpu_up(unsigned int cpu)
402 rc = cpu_up_check(cpu);
407 if (num_online_cpus() == 1)
408 alternatives_smp_switch(1);
411 /* This must be done before setting cpu_online_map */
412 set_cpu_sibling_map(cpu);
415 xen_smp_intr_init(cpu);
416 cpu_set(cpu, cpu_online_map);
418 rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
424 void __init smp_cpus_done(unsigned int max_cpus)
428 #ifndef CONFIG_X86_LOCAL_APIC
429 int setup_profiling_timer(unsigned int multiplier)