blob: 762b46ab14d50dc3695f474423d50e0bd9e8b70a [file] [log] [blame]
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001/*
2 * Xen SMP support
3 *
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.
7 *
8 * IPIs are handled through the Xen event mechanism.
9 *
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.
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070014 */
15#include <linux/sched.h>
16#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070018#include <linux/smp.h>
19
20#include <asm/paravirt.h>
21#include <asm/desc.h>
22#include <asm/pgtable.h>
23#include <asm/cpu.h>
24
25#include <xen/interface/xen.h>
26#include <xen/interface/vcpu.h>
27
28#include <asm/xen/interface.h>
29#include <asm/xen/hypercall.h>
30
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +010031#include <xen/xen.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070032#include <xen/page.h>
33#include <xen/events.h>
34
35#include "xen-ops.h"
36#include "mmu.h"
37
Mike Travisb78936e2008-12-16 17:33:57 -080038cpumask_var_t xen_cpu_initialized_map;
Jens Axboe3b16cf82008-06-26 11:21:54 +020039
Tejun Heoc6e22f92009-10-29 22:34:13 +090040static DEFINE_PER_CPU(int, xen_resched_irq);
41static DEFINE_PER_CPU(int, xen_callfunc_irq);
42static DEFINE_PER_CPU(int, xen_callfuncsingle_irq);
43static DEFINE_PER_CPU(int, xen_debug_irq) = -1;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070044
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070045static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
Jens Axboe3b16cf82008-06-26 11:21:54 +020046static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070047
48/*
Peter Zijlstra184748c2011-04-05 17:23:39 +020049 * Reschedule call back.
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070050 */
51static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
52{
Brian Gerst1b437c82009-01-19 00:38:57 +090053 inc_irq_stat(irq_resched_count);
Peter Zijlstra184748c2011-04-05 17:23:39 +020054 scheduler_ipi();
Jeremy Fitzhardinge38bb5ab2008-05-26 23:31:16 +010055
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070056 return IRQ_HANDLED;
57}
58
Alex Nixond68d82a2008-08-22 11:52:15 +010059static __cpuinit void cpu_bringup(void)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070060{
61 int cpu = smp_processor_id();
62
63 cpu_init();
Alex Nixond68d82a2008-08-22 11:52:15 +010064 touch_softlockup_watchdog();
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -070065 preempt_disable();
66
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -070067 xen_enable_sysenter();
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -070068 xen_enable_syscall();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070069
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -070070 cpu = smp_processor_id();
71 smp_store_cpu_info(cpu);
72 cpu_data(cpu).x86_max_cores = 1;
73 set_cpu_sibling_map(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070074
75 xen_setup_cpu_clockevents();
76
Rusty Russelld7d37562009-11-03 14:58:38 +103077 set_cpu_online(cpu, true);
Ingo Molnar6dbde352009-01-15 22:15:53 +090078 percpu_write(cpu_state, CPU_ONLINE);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -070079 wmb();
80
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070081 /* We can take interrupts now: we're officially "up". */
82 local_irq_enable();
83
84 wmb(); /* make sure everything is out */
Alex Nixond68d82a2008-08-22 11:52:15 +010085}
86
87static __cpuinit void cpu_bringup_and_idle(void)
88{
89 cpu_bringup();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070090 cpu_idle();
91}
92
93static int xen_smp_intr_init(unsigned int cpu)
94{
95 int rc;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -070096 const char *resched_name, *callfunc_name, *debug_name;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070097
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;
Tejun Heoc6e22f92009-10-29 22:34:13 +0900107 per_cpu(xen_resched_irq, cpu) = rc;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700108
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;
Tejun Heoc6e22f92009-10-29 22:34:13 +0900118 per_cpu(xen_callfunc_irq, cpu) = rc;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700119
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700120 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;
Tejun Heoc6e22f92009-10-29 22:34:13 +0900126 per_cpu(xen_debug_irq, cpu) = rc;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700127
Jens Axboe3b16cf82008-06-26 11:21:54 +0200128 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;
Tejun Heoc6e22f92009-10-29 22:34:13 +0900137 per_cpu(xen_callfuncsingle_irq, cpu) = rc;
Jens Axboe3b16cf82008-06-26 11:21:54 +0200138
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700139 return 0;
140
141 fail:
Tejun Heoc6e22f92009-10-29 22:34:13 +0900142 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);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200151
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700152 return rc;
153}
154
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700155static void __init xen_fill_possible_map(void)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700156{
157 int i, rc;
158
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100159 if (xen_initial_domain())
160 return;
161
Mike Travise7986732008-12-16 17:33:52 -0800162 for (i = 0; i < nr_cpu_ids; i++) {
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700163 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
Jeremy Fitzhardinge4560a292008-07-08 15:06:56 -0700164 if (rc >= 0) {
165 num_processors++;
Rusty Russell4f062892009-03-13 14:49:54 +1030166 set_cpu_possible(i, true);
Jeremy Fitzhardinge4560a292008-07-08 15:06:56 -0700167 }
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700168 }
169}
170
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100171static void __init xen_filter_cpu_maps(void)
172{
173 int i, rc;
174
175 if (!xen_initial_domain())
176 return;
177
Stefano Stabellini801fd142010-09-23 12:06:25 +0100178 num_processors = 0;
179 disabled_cpus = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700180 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);
Stefano Stabellini801fd142010-09-23 12:06:25 +0100185 } else {
186 set_cpu_possible(i, false);
187 set_cpu_present(i, false);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700188 }
189 }
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700190}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700191
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700192static void __init xen_smp_prepare_boot_cpu(void)
193{
194 BUG_ON(smp_processor_id() != 0);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700195 native_smp_prepare_boot_cpu();
196
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700197 /* We've switched to the "real" per-cpu gdt, so make sure the
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700198 old memory can be recycled */
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -0800199 make_lowmem_page_readwrite(xen_initial_gdt);
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700200
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100201 xen_filter_cpu_maps();
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700202 xen_setup_vcpu_info_placement();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700203}
204
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700205static void __init xen_smp_prepare_cpus(unsigned int max_cpus)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700206{
207 unsigned cpu;
208
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700209 xen_init_lock_cpu(0);
210
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700211 smp_store_cpu_info(0);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700212 cpu_data(0).x86_max_cores = 1;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700213 set_cpu_sibling_map(0);
214
215 if (xen_smp_intr_init(0))
216 BUG();
217
Mike Travisb78936e2008-12-16 17:33:57 -0800218 if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
219 panic("could not allocate xen_cpu_initialized_map\n");
220
221 cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700222
223 /* Restrict the possible_map according to max_cpus. */
224 while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
Mike Travise7986732008-12-16 17:33:52 -0800225 for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700226 continue;
Rusty Russell4f062892009-03-13 14:49:54 +1030227 set_cpu_possible(cpu, false);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700228 }
229
230 for_each_possible_cpu (cpu) {
231 struct task_struct *idle;
232
233 if (cpu == 0)
234 continue;
235
236 idle = fork_idle(cpu);
237 if (IS_ERR(idle))
238 panic("failed fork for CPU %d", cpu);
239
Rusty Russell4f062892009-03-13 14:49:54 +1030240 set_cpu_present(cpu, true);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700241 }
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700242}
243
244static __cpuinit int
245cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
246{
247 struct vcpu_guest_context *ctxt;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700248 struct desc_struct *gdt;
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800249 unsigned long gdt_mfn;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700250
Mike Travisb78936e2008-12-16 17:33:57 -0800251 if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700252 return 0;
253
254 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
255 if (ctxt == NULL)
256 return -ENOMEM;
257
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700258 gdt = get_cpu_gdt_table(cpu);
259
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700260 ctxt->flags = VGCF_IN_KERNEL;
261 ctxt->user_regs.ds = __USER_DS;
262 ctxt->user_regs.es = __USER_DS;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700263 ctxt->user_regs.ss = __KERNEL_DS;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700264#ifdef CONFIG_X86_32
265 ctxt->user_regs.fs = __KERNEL_PERCPU;
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700266 ctxt->user_regs.gs = __KERNEL_STACK_CANARY;
Jeremy Fitzhardinge795f99b2009-01-30 17:47:54 +0900267#else
268 ctxt->gs_base_kernel = per_cpu_offset(cpu);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700269#endif
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700270 ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
271 ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
272
273 memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
274
275 xen_copy_trap_info(ctxt->trap_ctxt);
276
277 ctxt->ldt_ents = 0;
278
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700279 BUG_ON((unsigned long)gdt & ~PAGE_MASK);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700280
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800281 gdt_mfn = arbitrary_virt_to_mfn(gdt);
282 make_lowmem_page_readonly(gdt);
283 make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
284
285 ctxt->gdt_frames[0] = gdt_mfn;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700286 ctxt->gdt_ents = GDT_ENTRIES;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700287
288 ctxt->user_regs.cs = __KERNEL_CS;
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100289 ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700290
291 ctxt->kernel_ss = __KERNEL_DS;
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100292 ctxt->kernel_sp = idle->thread.sp0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700293
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700294#ifdef CONFIG_X86_32
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700295 ctxt->event_callback_cs = __KERNEL_CS;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700296 ctxt->failsafe_callback_cs = __KERNEL_CS;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700297#endif
298 ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700299 ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
300
301 per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
302 ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
303
304 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
305 BUG();
306
307 kfree(ctxt);
308 return 0;
309}
310
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700311static int __cpuinit xen_cpu_up(unsigned int cpu)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700312{
313 struct task_struct *idle = idle_task(cpu);
314 int rc;
315
Brian Gerstc6f5e0a2009-01-19 00:38:58 +0900316 per_cpu(current_task, cpu) = idle;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700317#ifdef CONFIG_X86_32
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700318 irq_ctx_init(cpu);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700319#else
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700320 clear_tsk_thread_flag(idle, TIF_FORK);
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -0800321 per_cpu(kernel_stack, cpu) =
322 (unsigned long)task_stack_page(idle) -
323 KERNEL_STACK_OFFSET + THREAD_SIZE;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700324#endif
Ian Campbell02889672009-11-24 09:32:48 -0800325 xen_setup_runstate_info(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700326 xen_setup_timer(cpu);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700327 xen_init_lock_cpu(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700328
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700329 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
330
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700331 /* make sure interrupts start blocked */
332 per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
333
334 rc = cpu_initialize_context(cpu, idle);
335 if (rc)
336 return rc;
337
338 if (num_online_cpus() == 1)
339 alternatives_smp_switch(1);
340
341 rc = xen_smp_intr_init(cpu);
342 if (rc)
343 return rc;
344
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700345 rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
346 BUG_ON(rc);
347
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700348 while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
Hannes Eder1207cf8e2009-03-05 20:13:57 +0100349 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700350 barrier();
351 }
352
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700353 return 0;
354}
355
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700356static void xen_smp_cpus_done(unsigned int max_cpus)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700357{
358}
359
Alex Nixon27371462008-09-08 13:43:33 +0100360#ifdef CONFIG_HOTPLUG_CPU
Alex Nixon26fd1052008-09-08 13:43:34 +0100361static int xen_cpu_disable(void)
Alex Nixond68d82a2008-08-22 11:52:15 +0100362{
363 unsigned int cpu = smp_processor_id();
364 if (cpu == 0)
365 return -EBUSY;
366
367 cpu_disable_common();
368
369 load_cr3(swapper_pg_dir);
370 return 0;
371}
372
Alex Nixon26fd1052008-09-08 13:43:34 +0100373static void xen_cpu_die(unsigned int cpu)
Alex Nixond68d82a2008-08-22 11:52:15 +0100374{
375 while (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) {
376 current->state = TASK_UNINTERRUPTIBLE;
377 schedule_timeout(HZ/10);
378 }
Tejun Heoc6e22f92009-10-29 22:34:13 +0900379 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);
Alex Nixond68d82a2008-08-22 11:52:15 +0100383 xen_uninit_lock_cpu(cpu);
384 xen_teardown_timer(cpu);
385
386 if (num_online_cpus() == 1)
387 alternatives_smp_switch(0);
388}
389
Robert P. J. Day71709242009-12-28 11:50:29 -0500390static void __cpuinit xen_play_dead(void) /* used only with HOTPLUG_CPU */
Alex Nixond68d82a2008-08-22 11:52:15 +0100391{
392 play_dead_common();
393 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
394 cpu_bringup();
395}
396
Alex Nixon27371462008-09-08 13:43:33 +0100397#else /* !CONFIG_HOTPLUG_CPU */
Alex Nixon26fd1052008-09-08 13:43:34 +0100398static int xen_cpu_disable(void)
Alex Nixon27371462008-09-08 13:43:33 +0100399{
400 return -ENOSYS;
401}
402
Alex Nixon26fd1052008-09-08 13:43:34 +0100403static void xen_cpu_die(unsigned int cpu)
Alex Nixon27371462008-09-08 13:43:33 +0100404{
405 BUG();
406}
407
Alex Nixon26fd1052008-09-08 13:43:34 +0100408static void xen_play_dead(void)
Alex Nixon27371462008-09-08 13:43:33 +0100409{
410 BUG();
411}
412
413#endif
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700414static void stop_self(void *v)
415{
416 int cpu = smp_processor_id();
417
418 /* make sure we're not pinning something down */
419 load_cr3(swapper_pg_dir);
420 /* should set up a minimal gdt */
421
Ian Campbell086748e2010-08-03 14:55:14 -0700422 set_cpu_online(cpu, false);
423
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700424 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
425 BUG();
426}
427
Alok Kataria76fac072010-10-11 14:37:08 -0700428static void xen_stop_other_cpus(int wait)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700429{
Alok Kataria76fac072010-10-11 14:37:08 -0700430 smp_call_function(stop_self, NULL, wait);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700431}
432
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700433static void xen_smp_send_reschedule(int cpu)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700434{
435 xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
436}
437
Mike Travisbcda0162008-12-16 17:33:59 -0800438static void xen_send_IPI_mask(const struct cpumask *mask,
439 enum ipi_vector vector)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700440{
441 unsigned cpu;
442
Mike Travisbcda0162008-12-16 17:33:59 -0800443 for_each_cpu_and(cpu, mask, cpu_online_mask)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700444 xen_send_IPI_one(cpu, vector);
445}
446
Mike Travisbcda0162008-12-16 17:33:59 -0800447static void xen_smp_send_call_function_ipi(const struct cpumask *mask)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200448{
449 int cpu;
450
451 xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
452
453 /* Make sure other vcpus get a chance to run if they need to. */
Mike Travisbcda0162008-12-16 17:33:59 -0800454 for_each_cpu(cpu, mask) {
Jens Axboe3b16cf82008-06-26 11:21:54 +0200455 if (xen_vcpu_stolen(cpu)) {
Hannes Eder1207cf8e2009-03-05 20:13:57 +0100456 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200457 break;
458 }
459 }
460}
461
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700462static void xen_smp_send_call_function_single_ipi(int cpu)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200463{
Mike Travisbcda0162008-12-16 17:33:59 -0800464 xen_send_IPI_mask(cpumask_of(cpu),
Mike Travise7986732008-12-16 17:33:52 -0800465 XEN_CALL_FUNCTION_SINGLE_VECTOR);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200466}
467
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700468static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
469{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700470 irq_enter();
Jens Axboe3b16cf82008-06-26 11:21:54 +0200471 generic_smp_call_function_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900472 inc_irq_stat(irq_call_count);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700473 irq_exit();
474
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700475 return IRQ_HANDLED;
476}
477
Jens Axboe3b16cf82008-06-26 11:21:54 +0200478static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700479{
Jens Axboe3b16cf82008-06-26 11:21:54 +0200480 irq_enter();
481 generic_smp_call_function_single_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900482 inc_irq_stat(irq_call_count);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200483 irq_exit();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700484
Jens Axboe3b16cf82008-06-26 11:21:54 +0200485 return IRQ_HANDLED;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700486}
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700487
488static const struct smp_ops xen_smp_ops __initdata = {
489 .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
490 .smp_prepare_cpus = xen_smp_prepare_cpus,
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700491 .smp_cpus_done = xen_smp_cpus_done,
492
Alex Nixond68d82a2008-08-22 11:52:15 +0100493 .cpu_up = xen_cpu_up,
494 .cpu_die = xen_cpu_die,
495 .cpu_disable = xen_cpu_disable,
496 .play_dead = xen_play_dead,
497
Alok Kataria76fac072010-10-11 14:37:08 -0700498 .stop_other_cpus = xen_stop_other_cpus,
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700499 .smp_send_reschedule = xen_smp_send_reschedule,
500
501 .send_call_func_ipi = xen_smp_send_call_function_ipi,
502 .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
503};
504
505void __init xen_smp_init(void)
506{
507 smp_ops = xen_smp_ops;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700508 xen_fill_possible_map();
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700509 xen_init_spinlocks();
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700510}
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000511
512static void __init xen_hvm_smp_prepare_cpus(unsigned int max_cpus)
513{
514 native_smp_prepare_cpus(max_cpus);
515 WARN_ON(xen_smp_intr_init(0));
516
517 if (!xen_have_vector_callback)
518 return;
519 xen_init_lock_cpu(0);
520 xen_init_spinlocks();
521}
522
523static int __cpuinit xen_hvm_cpu_up(unsigned int cpu)
524{
525 int rc;
526 rc = native_cpu_up(cpu);
527 WARN_ON (xen_smp_intr_init(cpu));
528 return rc;
529}
530
531static void xen_hvm_cpu_die(unsigned int cpu)
532{
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);
538}
539
540void __init xen_hvm_smp_init(void)
541{
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;
548}