hwmon: (jc42) Change detection class
[linux/fpc-iii.git] / arch / x86 / xen / smp.c
blob41038c01de403e48c71052ab615dcf7f647f2653
1 /*
2 * Xen SMP support
4 * This file implements the Xen versions of smp_ops. SMP under Xen is
5 * very straightforward. Bringing a CPU up is simply a matter of
6 * loading its initial context and setting it running.
8 * IPIs are handled through the Xen event mechanism.
10 * Because virtual CPUs can be scheduled onto any real CPU, there's no
11 * useful topology information for the kernel to make use of. As a
12 * result, all CPUs are treated as if they're single-core and
13 * single-threaded.
15 #include <linux/sched.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/smp.h>
20 #include <asm/paravirt.h>
21 #include <asm/desc.h>
22 #include <asm/pgtable.h>
23 #include <asm/cpu.h>
25 #include <xen/interface/xen.h>
26 #include <xen/interface/vcpu.h>
28 #include <asm/xen/interface.h>
29 #include <asm/xen/hypercall.h>
31 #include <xen/xen.h>
32 #include <xen/page.h>
33 #include <xen/events.h>
35 #include "xen-ops.h"
36 #include "mmu.h"
38 cpumask_var_t xen_cpu_initialized_map;
40 static DEFINE_PER_CPU(int, xen_resched_irq);
41 static DEFINE_PER_CPU(int, xen_callfunc_irq);
42 static DEFINE_PER_CPU(int, xen_callfuncsingle_irq);
43 static DEFINE_PER_CPU(int, xen_debug_irq) = -1;
45 static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
46 static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
49 * Reschedule call back.
51 static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
53 inc_irq_stat(irq_resched_count);
54 scheduler_ipi();
56 return IRQ_HANDLED;
59 static void __cpuinit cpu_bringup(void)
61 int cpu = smp_processor_id();
63 cpu_init();
64 touch_softlockup_watchdog();
65 preempt_disable();
67 xen_enable_sysenter();
68 xen_enable_syscall();
70 cpu = smp_processor_id();
71 smp_store_cpu_info(cpu);
72 cpu_data(cpu).x86_max_cores = 1;
73 set_cpu_sibling_map(cpu);
75 xen_setup_cpu_clockevents();
77 set_cpu_online(cpu, true);
78 percpu_write(cpu_state, CPU_ONLINE);
79 wmb();
81 /* We can take interrupts now: we're officially "up". */
82 local_irq_enable();
84 wmb(); /* make sure everything is out */
87 static void __cpuinit cpu_bringup_and_idle(void)
89 cpu_bringup();
90 cpu_idle();
93 static int xen_smp_intr_init(unsigned int cpu)
95 int rc;
96 const char *resched_name, *callfunc_name, *debug_name;
98 resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
99 rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
100 cpu,
101 xen_reschedule_interrupt,
102 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
103 resched_name,
104 NULL);
105 if (rc < 0)
106 goto fail;
107 per_cpu(xen_resched_irq, cpu) = rc;
109 callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
110 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
111 cpu,
112 xen_call_function_interrupt,
113 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
114 callfunc_name,
115 NULL);
116 if (rc < 0)
117 goto fail;
118 per_cpu(xen_callfunc_irq, cpu) = rc;
120 debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
121 rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
122 IRQF_DISABLED | IRQF_PERCPU | IRQF_NOBALANCING,
123 debug_name, NULL);
124 if (rc < 0)
125 goto fail;
126 per_cpu(xen_debug_irq, cpu) = rc;
128 callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
129 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
130 cpu,
131 xen_call_function_single_interrupt,
132 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
133 callfunc_name,
134 NULL);
135 if (rc < 0)
136 goto fail;
137 per_cpu(xen_callfuncsingle_irq, cpu) = rc;
139 return 0;
141 fail:
142 if (per_cpu(xen_resched_irq, cpu) >= 0)
143 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL);
144 if (per_cpu(xen_callfunc_irq, cpu) >= 0)
145 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL);
146 if (per_cpu(xen_debug_irq, cpu) >= 0)
147 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL);
148 if (per_cpu(xen_callfuncsingle_irq, cpu) >= 0)
149 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu),
150 NULL);
152 return rc;
155 static void __init xen_fill_possible_map(void)
157 int i, rc;
159 if (xen_initial_domain())
160 return;
162 for (i = 0; i < nr_cpu_ids; i++) {
163 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
164 if (rc >= 0) {
165 num_processors++;
166 set_cpu_possible(i, true);
171 static void __init xen_filter_cpu_maps(void)
173 int i, rc;
175 if (!xen_initial_domain())
176 return;
178 num_processors = 0;
179 disabled_cpus = 0;
180 for (i = 0; i < nr_cpu_ids; i++) {
181 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
182 if (rc >= 0) {
183 num_processors++;
184 set_cpu_possible(i, true);
185 } else {
186 set_cpu_possible(i, false);
187 set_cpu_present(i, false);
192 static void __init xen_smp_prepare_boot_cpu(void)
194 BUG_ON(smp_processor_id() != 0);
195 native_smp_prepare_boot_cpu();
197 /* We've switched to the "real" per-cpu gdt, so make sure the
198 old memory can be recycled */
199 make_lowmem_page_readwrite(xen_initial_gdt);
201 xen_filter_cpu_maps();
202 xen_setup_vcpu_info_placement();
205 static void __init xen_smp_prepare_cpus(unsigned int max_cpus)
207 unsigned cpu;
209 xen_init_lock_cpu(0);
211 smp_store_cpu_info(0);
212 cpu_data(0).x86_max_cores = 1;
213 set_cpu_sibling_map(0);
215 if (xen_smp_intr_init(0))
216 BUG();
218 if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
219 panic("could not allocate xen_cpu_initialized_map\n");
221 cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
223 /* Restrict the possible_map according to max_cpus. */
224 while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
225 for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
226 continue;
227 set_cpu_possible(cpu, false);
230 for_each_possible_cpu (cpu) {
231 struct task_struct *idle;
233 if (cpu == 0)
234 continue;
236 idle = fork_idle(cpu);
237 if (IS_ERR(idle))
238 panic("failed fork for CPU %d", cpu);
240 set_cpu_present(cpu, true);
244 static int __cpuinit
245 cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
247 struct vcpu_guest_context *ctxt;
248 struct desc_struct *gdt;
249 unsigned long gdt_mfn;
251 if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
252 return 0;
254 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
255 if (ctxt == NULL)
256 return -ENOMEM;
258 gdt = get_cpu_gdt_table(cpu);
260 ctxt->flags = VGCF_IN_KERNEL;
261 ctxt->user_regs.ds = __USER_DS;
262 ctxt->user_regs.es = __USER_DS;
263 ctxt->user_regs.ss = __KERNEL_DS;
264 #ifdef CONFIG_X86_32
265 ctxt->user_regs.fs = __KERNEL_PERCPU;
266 ctxt->user_regs.gs = __KERNEL_STACK_CANARY;
267 #else
268 ctxt->gs_base_kernel = per_cpu_offset(cpu);
269 #endif
270 ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
271 ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
273 memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
275 xen_copy_trap_info(ctxt->trap_ctxt);
277 ctxt->ldt_ents = 0;
279 BUG_ON((unsigned long)gdt & ~PAGE_MASK);
281 gdt_mfn = arbitrary_virt_to_mfn(gdt);
282 make_lowmem_page_readonly(gdt);
283 make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
285 ctxt->gdt_frames[0] = gdt_mfn;
286 ctxt->gdt_ents = GDT_ENTRIES;
288 ctxt->user_regs.cs = __KERNEL_CS;
289 ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
291 ctxt->kernel_ss = __KERNEL_DS;
292 ctxt->kernel_sp = idle->thread.sp0;
294 #ifdef CONFIG_X86_32
295 ctxt->event_callback_cs = __KERNEL_CS;
296 ctxt->failsafe_callback_cs = __KERNEL_CS;
297 #endif
298 ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
299 ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
301 per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
302 ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
304 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
305 BUG();
307 kfree(ctxt);
308 return 0;
311 static int __cpuinit xen_cpu_up(unsigned int cpu)
313 struct task_struct *idle = idle_task(cpu);
314 int rc;
316 per_cpu(current_task, cpu) = idle;
317 #ifdef CONFIG_X86_32
318 irq_ctx_init(cpu);
319 #else
320 clear_tsk_thread_flag(idle, TIF_FORK);
321 per_cpu(kernel_stack, cpu) =
322 (unsigned long)task_stack_page(idle) -
323 KERNEL_STACK_OFFSET + THREAD_SIZE;
324 #endif
325 xen_setup_runstate_info(cpu);
326 xen_setup_timer(cpu);
327 xen_init_lock_cpu(cpu);
329 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
331 /* make sure interrupts start blocked */
332 per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
334 rc = cpu_initialize_context(cpu, idle);
335 if (rc)
336 return rc;
338 if (num_online_cpus() == 1)
339 alternatives_smp_switch(1);
341 rc = xen_smp_intr_init(cpu);
342 if (rc)
343 return rc;
345 rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
346 BUG_ON(rc);
348 while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
349 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
350 barrier();
353 return 0;
356 static void xen_smp_cpus_done(unsigned int max_cpus)
360 #ifdef CONFIG_HOTPLUG_CPU
361 static int xen_cpu_disable(void)
363 unsigned int cpu = smp_processor_id();
364 if (cpu == 0)
365 return -EBUSY;
367 cpu_disable_common();
369 load_cr3(swapper_pg_dir);
370 return 0;
373 static void xen_cpu_die(unsigned int cpu)
375 while (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) {
376 current->state = TASK_UNINTERRUPTIBLE;
377 schedule_timeout(HZ/10);
379 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL);
380 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL);
381 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL);
382 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu), NULL);
383 xen_uninit_lock_cpu(cpu);
384 xen_teardown_timer(cpu);
386 if (num_online_cpus() == 1)
387 alternatives_smp_switch(0);
390 static void __cpuinit xen_play_dead(void) /* used only with HOTPLUG_CPU */
392 play_dead_common();
393 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
394 cpu_bringup();
397 #else /* !CONFIG_HOTPLUG_CPU */
398 static int xen_cpu_disable(void)
400 return -ENOSYS;
403 static void xen_cpu_die(unsigned int cpu)
405 BUG();
408 static void xen_play_dead(void)
410 BUG();
413 #endif
414 static void stop_self(void *v)
416 int cpu = smp_processor_id();
418 /* make sure we're not pinning something down */
419 load_cr3(swapper_pg_dir);
420 /* should set up a minimal gdt */
422 set_cpu_online(cpu, false);
424 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
425 BUG();
428 static void xen_stop_other_cpus(int wait)
430 smp_call_function(stop_self, NULL, wait);
433 static void xen_smp_send_reschedule(int cpu)
435 xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
438 static void xen_send_IPI_mask(const struct cpumask *mask,
439 enum ipi_vector vector)
441 unsigned cpu;
443 for_each_cpu_and(cpu, mask, cpu_online_mask)
444 xen_send_IPI_one(cpu, vector);
447 static void xen_smp_send_call_function_ipi(const struct cpumask *mask)
449 int cpu;
451 xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
453 /* Make sure other vcpus get a chance to run if they need to. */
454 for_each_cpu(cpu, mask) {
455 if (xen_vcpu_stolen(cpu)) {
456 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
457 break;
462 static void xen_smp_send_call_function_single_ipi(int cpu)
464 xen_send_IPI_mask(cpumask_of(cpu),
465 XEN_CALL_FUNCTION_SINGLE_VECTOR);
468 static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
470 irq_enter();
471 generic_smp_call_function_interrupt();
472 inc_irq_stat(irq_call_count);
473 irq_exit();
475 return IRQ_HANDLED;
478 static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
480 irq_enter();
481 generic_smp_call_function_single_interrupt();
482 inc_irq_stat(irq_call_count);
483 irq_exit();
485 return IRQ_HANDLED;
488 static const struct smp_ops xen_smp_ops __initconst = {
489 .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
490 .smp_prepare_cpus = xen_smp_prepare_cpus,
491 .smp_cpus_done = xen_smp_cpus_done,
493 .cpu_up = xen_cpu_up,
494 .cpu_die = xen_cpu_die,
495 .cpu_disable = xen_cpu_disable,
496 .play_dead = xen_play_dead,
498 .stop_other_cpus = xen_stop_other_cpus,
499 .smp_send_reschedule = xen_smp_send_reschedule,
501 .send_call_func_ipi = xen_smp_send_call_function_ipi,
502 .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
505 void __init xen_smp_init(void)
507 smp_ops = xen_smp_ops;
508 xen_fill_possible_map();
509 xen_init_spinlocks();
512 static void __init xen_hvm_smp_prepare_cpus(unsigned int max_cpus)
514 native_smp_prepare_cpus(max_cpus);
515 WARN_ON(xen_smp_intr_init(0));
517 if (!xen_have_vector_callback)
518 return;
519 xen_init_lock_cpu(0);
520 xen_init_spinlocks();
523 static int __cpuinit xen_hvm_cpu_up(unsigned int cpu)
525 int rc;
526 rc = native_cpu_up(cpu);
527 WARN_ON (xen_smp_intr_init(cpu));
528 return rc;
531 static void xen_hvm_cpu_die(unsigned int cpu)
533 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL);
534 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL);
535 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL);
536 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu), NULL);
537 native_cpu_die(cpu);
540 void __init xen_hvm_smp_init(void)
542 smp_ops.smp_prepare_cpus = xen_hvm_smp_prepare_cpus;
543 smp_ops.smp_send_reschedule = xen_smp_send_reschedule;
544 smp_ops.cpu_up = xen_hvm_cpu_up;
545 smp_ops.cpu_die = xen_hvm_cpu_die;
546 smp_ops.send_call_func_ipi = xen_smp_send_call_function_ipi;
547 smp_ops.send_call_func_single_ipi = xen_smp_send_call_function_single_ipi;