blob: 0503c0c493a9a64bf44b41fb237831cb290203fc [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
Konrad Rzeszutek Wilked467e62011-09-01 09:48:27 -040035#include <xen/hvc-console.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070036#include "xen-ops.h"
37#include "mmu.h"
38
Mike Travisb78936e2008-12-16 17:33:57 -080039cpumask_var_t xen_cpu_initialized_map;
Jens Axboe3b16cf82008-06-26 11:21:54 +020040
Tejun Heoc6e22f92009-10-29 22:34:13 +090041static DEFINE_PER_CPU(int, xen_resched_irq);
42static DEFINE_PER_CPU(int, xen_callfunc_irq);
43static DEFINE_PER_CPU(int, xen_callfuncsingle_irq);
44static DEFINE_PER_CPU(int, xen_debug_irq) = -1;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070045
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070046static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
Jens Axboe3b16cf82008-06-26 11:21:54 +020047static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070048
49/*
Peter Zijlstra184748c2011-04-05 17:23:39 +020050 * Reschedule call back.
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070051 */
52static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
53{
Brian Gerst1b437c82009-01-19 00:38:57 +090054 inc_irq_stat(irq_resched_count);
Peter Zijlstra184748c2011-04-05 17:23:39 +020055 scheduler_ipi();
Jeremy Fitzhardinge38bb5ab2008-05-26 23:31:16 +010056
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070057 return IRQ_HANDLED;
58}
59
Daniel Kiperb53cede2011-05-04 20:18:05 +020060static void __cpuinit cpu_bringup(void)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070061{
Srivatsa S. Bhate8c9e782012-03-22 18:29:24 +053062 int cpu;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070063
64 cpu_init();
Alex Nixond68d82a2008-08-22 11:52:15 +010065 touch_softlockup_watchdog();
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -070066 preempt_disable();
67
Jeremy Fitzhardingee2a81ba2008-03-17 16:37:17 -070068 xen_enable_sysenter();
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -070069 xen_enable_syscall();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070070
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -070071 cpu = smp_processor_id();
72 smp_store_cpu_info(cpu);
73 cpu_data(cpu).x86_max_cores = 1;
74 set_cpu_sibling_map(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070075
76 xen_setup_cpu_clockevents();
77
Konrad Rzeszutek Wilk106b4432012-03-21 13:03:45 -040078 notify_cpu_starting(cpu);
79
80 ipi_call_lock();
Rusty Russelld7d37562009-11-03 14:58:38 +103081 set_cpu_online(cpu, true);
Konrad Rzeszutek Wilk106b4432012-03-21 13:03:45 -040082 ipi_call_unlock();
83
Alex Shi2113f462012-01-13 23:53:35 +080084 this_cpu_write(cpu_state, CPU_ONLINE);
Konrad Rzeszutek Wilk106b4432012-03-21 13:03:45 -040085
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -070086 wmb();
87
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070088 /* We can take interrupts now: we're officially "up". */
89 local_irq_enable();
90
91 wmb(); /* make sure everything is out */
Alex Nixond68d82a2008-08-22 11:52:15 +010092}
93
Daniel Kiperb53cede2011-05-04 20:18:05 +020094static void __cpuinit cpu_bringup_and_idle(void)
Alex Nixond68d82a2008-08-22 11:52:15 +010095{
96 cpu_bringup();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070097 cpu_idle();
98}
99
100static int xen_smp_intr_init(unsigned int cpu)
101{
102 int rc;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700103 const char *resched_name, *callfunc_name, *debug_name;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700104
105 resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
106 rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
107 cpu,
108 xen_reschedule_interrupt,
109 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
110 resched_name,
111 NULL);
112 if (rc < 0)
113 goto fail;
Tejun Heoc6e22f92009-10-29 22:34:13 +0900114 per_cpu(xen_resched_irq, cpu) = rc;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700115
116 callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
117 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
118 cpu,
119 xen_call_function_interrupt,
120 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
121 callfunc_name,
122 NULL);
123 if (rc < 0)
124 goto fail;
Tejun Heoc6e22f92009-10-29 22:34:13 +0900125 per_cpu(xen_callfunc_irq, cpu) = rc;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700126
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700127 debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
128 rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
129 IRQF_DISABLED | IRQF_PERCPU | IRQF_NOBALANCING,
130 debug_name, NULL);
131 if (rc < 0)
132 goto fail;
Tejun Heoc6e22f92009-10-29 22:34:13 +0900133 per_cpu(xen_debug_irq, cpu) = rc;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700134
Jens Axboe3b16cf82008-06-26 11:21:54 +0200135 callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
136 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
137 cpu,
138 xen_call_function_single_interrupt,
139 IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
140 callfunc_name,
141 NULL);
142 if (rc < 0)
143 goto fail;
Tejun Heoc6e22f92009-10-29 22:34:13 +0900144 per_cpu(xen_callfuncsingle_irq, cpu) = rc;
Jens Axboe3b16cf82008-06-26 11:21:54 +0200145
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700146 return 0;
147
148 fail:
Tejun Heoc6e22f92009-10-29 22:34:13 +0900149 if (per_cpu(xen_resched_irq, cpu) >= 0)
150 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL);
151 if (per_cpu(xen_callfunc_irq, cpu) >= 0)
152 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL);
153 if (per_cpu(xen_debug_irq, cpu) >= 0)
154 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL);
155 if (per_cpu(xen_callfuncsingle_irq, cpu) >= 0)
156 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu),
157 NULL);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200158
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700159 return rc;
160}
161
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700162static void __init xen_fill_possible_map(void)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700163{
164 int i, rc;
165
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100166 if (xen_initial_domain())
167 return;
168
Mike Travise7986732008-12-16 17:33:52 -0800169 for (i = 0; i < nr_cpu_ids; i++) {
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700170 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
Jeremy Fitzhardinge4560a292008-07-08 15:06:56 -0700171 if (rc >= 0) {
172 num_processors++;
Rusty Russell4f062892009-03-13 14:49:54 +1030173 set_cpu_possible(i, true);
Jeremy Fitzhardinge4560a292008-07-08 15:06:56 -0700174 }
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700175 }
176}
177
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100178static void __init xen_filter_cpu_maps(void)
179{
180 int i, rc;
Konrad Rzeszutek Wilkcf405ae2012-04-26 13:50:03 -0400181 unsigned int subtract = 0;
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100182
183 if (!xen_initial_domain())
184 return;
185
Stefano Stabellini801fd142010-09-23 12:06:25 +0100186 num_processors = 0;
187 disabled_cpus = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700188 for (i = 0; i < nr_cpu_ids; i++) {
189 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
190 if (rc >= 0) {
191 num_processors++;
192 set_cpu_possible(i, true);
Stefano Stabellini801fd142010-09-23 12:06:25 +0100193 } else {
194 set_cpu_possible(i, false);
195 set_cpu_present(i, false);
Konrad Rzeszutek Wilkcf405ae2012-04-26 13:50:03 -0400196 subtract++;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700197 }
198 }
Konrad Rzeszutek Wilkcf405ae2012-04-26 13:50:03 -0400199#ifdef CONFIG_HOTPLUG_CPU
200 /* This is akin to using 'nr_cpus' on the Linux command line.
201 * Which is OK as when we use 'dom0_max_vcpus=X' we can only
202 * have up to X, while nr_cpu_ids is greater than X. This
203 * normally is not a problem, except when CPU hotplugging
204 * is involved and then there might be more than X CPUs
205 * in the guest - which will not work as there is no
206 * hypercall to expand the max number of VCPUs an already
207 * running guest has. So cap it up to X. */
208 if (subtract)
209 nr_cpu_ids = nr_cpu_ids - subtract;
210#endif
211
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700212}
213
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700214static void __init xen_smp_prepare_boot_cpu(void)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700215{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700216 BUG_ON(smp_processor_id() != 0);
217 native_smp_prepare_boot_cpu();
218
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700219 /* We've switched to the "real" per-cpu gdt, so make sure the
220 old memory can be recycled */
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -0800221 make_lowmem_page_readwrite(xen_initial_gdt);
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700222
Stefano Stabelliniea5b8f72010-10-26 17:28:33 +0100223 xen_filter_cpu_maps();
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700224 xen_setup_vcpu_info_placement();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700225}
226
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700227static void __init xen_smp_prepare_cpus(unsigned int max_cpus)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700228{
229 unsigned cpu;
Andrew Jones900cba82009-12-18 10:31:31 +0100230 unsigned int i;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700231
Konrad Rzeszutek Wilked467e62011-09-01 09:48:27 -0400232 if (skip_ioapic_setup) {
233 char *m = (max_cpus == 0) ?
234 "The nosmp parameter is incompatible with Xen; " \
235 "use Xen dom0_max_vcpus=1 parameter" :
236 "The noapic parameter is incompatible with Xen";
237
238 xen_raw_printk(m);
239 panic(m);
240 }
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700241 xen_init_lock_cpu(0);
242
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700243 smp_store_cpu_info(0);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700244 cpu_data(0).x86_max_cores = 1;
Andrew Jones900cba82009-12-18 10:31:31 +0100245
246 for_each_possible_cpu(i) {
247 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
248 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
249 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
250 }
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700251 set_cpu_sibling_map(0);
252
253 if (xen_smp_intr_init(0))
254 BUG();
255
Mike Travisb78936e2008-12-16 17:33:57 -0800256 if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
257 panic("could not allocate xen_cpu_initialized_map\n");
258
259 cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700260
261 /* Restrict the possible_map according to max_cpus. */
262 while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
Mike Travise7986732008-12-16 17:33:52 -0800263 for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700264 continue;
Rusty Russell4f062892009-03-13 14:49:54 +1030265 set_cpu_possible(cpu, false);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700266 }
267
268 for_each_possible_cpu (cpu) {
269 struct task_struct *idle;
270
271 if (cpu == 0)
272 continue;
273
274 idle = fork_idle(cpu);
275 if (IS_ERR(idle))
276 panic("failed fork for CPU %d", cpu);
277
Rusty Russell4f062892009-03-13 14:49:54 +1030278 set_cpu_present(cpu, true);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700279 }
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700280}
281
Daniel Kiperb53cede2011-05-04 20:18:05 +0200282static int __cpuinit
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700283cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
284{
285 struct vcpu_guest_context *ctxt;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700286 struct desc_struct *gdt;
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800287 unsigned long gdt_mfn;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700288
Mike Travisb78936e2008-12-16 17:33:57 -0800289 if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700290 return 0;
291
292 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
293 if (ctxt == NULL)
294 return -ENOMEM;
295
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700296 gdt = get_cpu_gdt_table(cpu);
297
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700298 ctxt->flags = VGCF_IN_KERNEL;
299 ctxt->user_regs.ds = __USER_DS;
300 ctxt->user_regs.es = __USER_DS;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700301 ctxt->user_regs.ss = __KERNEL_DS;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700302#ifdef CONFIG_X86_32
303 ctxt->user_regs.fs = __KERNEL_PERCPU;
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700304 ctxt->user_regs.gs = __KERNEL_STACK_CANARY;
Jeremy Fitzhardinge795f99b2009-01-30 17:47:54 +0900305#else
306 ctxt->gs_base_kernel = per_cpu_offset(cpu);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700307#endif
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700308 ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
309 ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
310
311 memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
312
313 xen_copy_trap_info(ctxt->trap_ctxt);
314
315 ctxt->ldt_ents = 0;
316
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700317 BUG_ON((unsigned long)gdt & ~PAGE_MASK);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700318
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800319 gdt_mfn = arbitrary_virt_to_mfn(gdt);
320 make_lowmem_page_readonly(gdt);
321 make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
322
323 ctxt->gdt_frames[0] = gdt_mfn;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700324 ctxt->gdt_ents = GDT_ENTRIES;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700325
326 ctxt->user_regs.cs = __KERNEL_CS;
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100327 ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700328
329 ctxt->kernel_ss = __KERNEL_DS;
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100330 ctxt->kernel_sp = idle->thread.sp0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700331
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700332#ifdef CONFIG_X86_32
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700333 ctxt->event_callback_cs = __KERNEL_CS;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700334 ctxt->failsafe_callback_cs = __KERNEL_CS;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700335#endif
336 ctxt->event_callback_eip = (unsigned long)xen_hypervisor_callback;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700337 ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
338
339 per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
340 ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
341
342 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
343 BUG();
344
345 kfree(ctxt);
346 return 0;
347}
348
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700349static int __cpuinit xen_cpu_up(unsigned int cpu)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700350{
351 struct task_struct *idle = idle_task(cpu);
352 int rc;
353
Brian Gerstc6f5e0a2009-01-19 00:38:58 +0900354 per_cpu(current_task, cpu) = idle;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700355#ifdef CONFIG_X86_32
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700356 irq_ctx_init(cpu);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700357#else
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700358 clear_tsk_thread_flag(idle, TIF_FORK);
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -0800359 per_cpu(kernel_stack, cpu) =
360 (unsigned long)task_stack_page(idle) -
361 KERNEL_STACK_OFFSET + THREAD_SIZE;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700362#endif
Ian Campbell02889672009-11-24 09:32:48 -0800363 xen_setup_runstate_info(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700364 xen_setup_timer(cpu);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700365 xen_init_lock_cpu(cpu);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700366
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700367 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
368
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700369 /* make sure interrupts start blocked */
370 per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
371
372 rc = cpu_initialize_context(cpu, idle);
373 if (rc)
374 return rc;
375
376 if (num_online_cpus() == 1)
377 alternatives_smp_switch(1);
378
379 rc = xen_smp_intr_init(cpu);
380 if (rc)
381 return rc;
382
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700383 rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
384 BUG_ON(rc);
385
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700386 while(per_cpu(cpu_state, cpu) != CPU_ONLINE) {
Hannes Eder1207cf8e2009-03-05 20:13:57 +0100387 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700388 barrier();
389 }
390
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700391 return 0;
392}
393
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700394static void xen_smp_cpus_done(unsigned int max_cpus)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700395{
396}
397
Alex Nixon27371462008-09-08 13:43:33 +0100398#ifdef CONFIG_HOTPLUG_CPU
Alex Nixon26fd1052008-09-08 13:43:34 +0100399static int xen_cpu_disable(void)
Alex Nixond68d82a2008-08-22 11:52:15 +0100400{
401 unsigned int cpu = smp_processor_id();
402 if (cpu == 0)
403 return -EBUSY;
404
405 cpu_disable_common();
406
407 load_cr3(swapper_pg_dir);
408 return 0;
409}
410
Alex Nixon26fd1052008-09-08 13:43:34 +0100411static void xen_cpu_die(unsigned int cpu)
Alex Nixond68d82a2008-08-22 11:52:15 +0100412{
413 while (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) {
414 current->state = TASK_UNINTERRUPTIBLE;
415 schedule_timeout(HZ/10);
416 }
Tejun Heoc6e22f92009-10-29 22:34:13 +0900417 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL);
418 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL);
419 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL);
420 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu), NULL);
Alex Nixond68d82a2008-08-22 11:52:15 +0100421 xen_uninit_lock_cpu(cpu);
422 xen_teardown_timer(cpu);
423
424 if (num_online_cpus() == 1)
425 alternatives_smp_switch(0);
426}
427
Robert P. J. Day71709242009-12-28 11:50:29 -0500428static void __cpuinit xen_play_dead(void) /* used only with HOTPLUG_CPU */
Alex Nixond68d82a2008-08-22 11:52:15 +0100429{
430 play_dead_common();
431 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
432 cpu_bringup();
Konrad Rzeszutek Wilk41bd9562012-02-01 15:56:54 -0500433 /*
434 * Balance out the preempt calls - as we are running in cpu_idle
435 * loop which has been called at bootup from cpu_bringup_and_idle.
436 * The cpucpu_bringup_and_idle called cpu_bringup which made a
437 * preempt_disable() So this preempt_enable will balance it out.
438 */
439 preempt_enable();
Alex Nixond68d82a2008-08-22 11:52:15 +0100440}
441
Alex Nixon27371462008-09-08 13:43:33 +0100442#else /* !CONFIG_HOTPLUG_CPU */
Alex Nixon26fd1052008-09-08 13:43:34 +0100443static int xen_cpu_disable(void)
Alex Nixon27371462008-09-08 13:43:33 +0100444{
445 return -ENOSYS;
446}
447
Alex Nixon26fd1052008-09-08 13:43:34 +0100448static void xen_cpu_die(unsigned int cpu)
Alex Nixon27371462008-09-08 13:43:33 +0100449{
450 BUG();
451}
452
Alex Nixon26fd1052008-09-08 13:43:34 +0100453static void xen_play_dead(void)
Alex Nixon27371462008-09-08 13:43:33 +0100454{
455 BUG();
456}
457
458#endif
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700459static void stop_self(void *v)
460{
461 int cpu = smp_processor_id();
462
463 /* make sure we're not pinning something down */
464 load_cr3(swapper_pg_dir);
465 /* should set up a minimal gdt */
466
Ian Campbell086748e2010-08-03 14:55:14 -0700467 set_cpu_online(cpu, false);
468
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700469 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
470 BUG();
471}
472
Alok Kataria76fac072010-10-11 14:37:08 -0700473static void xen_stop_other_cpus(int wait)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700474{
Alok Kataria76fac072010-10-11 14:37:08 -0700475 smp_call_function(stop_self, NULL, wait);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700476}
477
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700478static void xen_smp_send_reschedule(int cpu)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700479{
480 xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
481}
482
Mike Travisbcda0162008-12-16 17:33:59 -0800483static void xen_send_IPI_mask(const struct cpumask *mask,
484 enum ipi_vector vector)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700485{
486 unsigned cpu;
487
Mike Travisbcda0162008-12-16 17:33:59 -0800488 for_each_cpu_and(cpu, mask, cpu_online_mask)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700489 xen_send_IPI_one(cpu, vector);
490}
491
Mike Travisbcda0162008-12-16 17:33:59 -0800492static void xen_smp_send_call_function_ipi(const struct cpumask *mask)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200493{
494 int cpu;
495
496 xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
497
498 /* Make sure other vcpus get a chance to run if they need to. */
Mike Travisbcda0162008-12-16 17:33:59 -0800499 for_each_cpu(cpu, mask) {
Jens Axboe3b16cf82008-06-26 11:21:54 +0200500 if (xen_vcpu_stolen(cpu)) {
Hannes Eder1207cf8e2009-03-05 20:13:57 +0100501 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200502 break;
503 }
504 }
505}
506
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700507static void xen_smp_send_call_function_single_ipi(int cpu)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200508{
Mike Travisbcda0162008-12-16 17:33:59 -0800509 xen_send_IPI_mask(cpumask_of(cpu),
Mike Travise7986732008-12-16 17:33:52 -0800510 XEN_CALL_FUNCTION_SINGLE_VECTOR);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200511}
512
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700513static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
514{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700515 irq_enter();
Jens Axboe3b16cf82008-06-26 11:21:54 +0200516 generic_smp_call_function_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900517 inc_irq_stat(irq_call_count);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700518 irq_exit();
519
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700520 return IRQ_HANDLED;
521}
522
Jens Axboe3b16cf82008-06-26 11:21:54 +0200523static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700524{
Jens Axboe3b16cf82008-06-26 11:21:54 +0200525 irq_enter();
526 generic_smp_call_function_single_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900527 inc_irq_stat(irq_call_count);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200528 irq_exit();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700529
Jens Axboe3b16cf82008-06-26 11:21:54 +0200530 return IRQ_HANDLED;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700531}
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700532
Daniel Kiperb53cede2011-05-04 20:18:05 +0200533static const struct smp_ops xen_smp_ops __initconst = {
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700534 .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
535 .smp_prepare_cpus = xen_smp_prepare_cpus,
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700536 .smp_cpus_done = xen_smp_cpus_done,
537
Alex Nixond68d82a2008-08-22 11:52:15 +0100538 .cpu_up = xen_cpu_up,
539 .cpu_die = xen_cpu_die,
540 .cpu_disable = xen_cpu_disable,
541 .play_dead = xen_play_dead,
542
Alok Kataria76fac072010-10-11 14:37:08 -0700543 .stop_other_cpus = xen_stop_other_cpus,
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700544 .smp_send_reschedule = xen_smp_send_reschedule,
545
546 .send_call_func_ipi = xen_smp_send_call_function_ipi,
547 .send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
548};
549
550void __init xen_smp_init(void)
551{
552 smp_ops = xen_smp_ops;
Jeremy Fitzhardingec7b75942008-07-08 15:06:43 -0700553 xen_fill_possible_map();
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700554 xen_init_spinlocks();
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -0700555}
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000556
557static void __init xen_hvm_smp_prepare_cpus(unsigned int max_cpus)
558{
559 native_smp_prepare_cpus(max_cpus);
560 WARN_ON(xen_smp_intr_init(0));
561
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000562 xen_init_lock_cpu(0);
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000563}
564
565static int __cpuinit xen_hvm_cpu_up(unsigned int cpu)
566{
567 int rc;
568 rc = native_cpu_up(cpu);
569 WARN_ON (xen_smp_intr_init(cpu));
570 return rc;
571}
572
573static void xen_hvm_cpu_die(unsigned int cpu)
574{
575 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu), NULL);
576 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu), NULL);
577 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu), NULL);
578 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu), NULL);
579 native_cpu_die(cpu);
580}
581
582void __init xen_hvm_smp_init(void)
583{
Stefano Stabellini3c05c4b2011-08-17 15:15:00 +0200584 if (!xen_have_vector_callback)
585 return;
Stefano Stabellini99bbb3a2010-12-02 17:55:10 +0000586 smp_ops.smp_prepare_cpus = xen_hvm_smp_prepare_cpus;
587 smp_ops.smp_send_reschedule = xen_smp_send_reschedule;
588 smp_ops.cpu_up = xen_hvm_cpu_up;
589 smp_ops.cpu_die = xen_hvm_cpu_die;
590 smp_ops.send_call_func_ipi = xen_smp_send_call_function_ipi;
591 smp_ops.send_call_func_single_ipi = xen_smp_send_call_function_single_ipi;
592}