Merge branch 'linus' into x86/urgent
[linux-2.6] / arch / x86 / xen / smp.c
1 /*
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.
14  *
15  * This does not handle HOTPLUG_CPU yet.
16  */
17 #include <linux/sched.h>
18 #include <linux/err.h>
19 #include <linux/smp.h>
20
21 #include <asm/paravirt.h>
22 #include <asm/desc.h>
23 #include <asm/pgtable.h>
24 #include <asm/cpu.h>
25
26 #include <xen/interface/xen.h>
27 #include <xen/interface/vcpu.h>
28
29 #include <asm/xen/interface.h>
30 #include <asm/xen/hypercall.h>
31
32 #include <xen/page.h>
33 #include <xen/events.h>
34
35 #include "xen-ops.h"
36 #include "mmu.h"
37
38 cpumask_t xen_cpu_initialized_map;
39 static DEFINE_PER_CPU(int, resched_irq) = -1;
40 static DEFINE_PER_CPU(int, callfunc_irq) = -1;
41 static DEFINE_PER_CPU(int, debug_irq) = -1;
42
43 /*
44  * Structure and data for smp_call_function(). This is designed to minimise
45  * static memory requirements. It also looks cleaner.
46  */
47 static DEFINE_SPINLOCK(call_lock);
48
49 struct call_data_struct {
50         void (*func) (void *info);
51         void *info;
52         atomic_t started;
53         atomic_t finished;
54         int wait;
55 };
56
57 static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
58
59 static struct call_data_struct *call_data;
60
61 /*
62  * Reschedule call back. Nothing to do,
63  * all the work is done automatically when
64  * we return from the interrupt.
65  */
66 static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
67 {
68 #ifdef CONFIG_X86_32
69         __get_cpu_var(irq_stat).irq_resched_count++;
70 #else
71         add_pda(irq_resched_count, 1);
72 #endif
73
74         return IRQ_HANDLED;
75 }
76
77 static __cpuinit void cpu_bringup_and_idle(void)
78 {
79         int cpu = smp_processor_id();
80
81         cpu_init();
82         xen_enable_sysenter();
83
84         preempt_disable();
85         per_cpu(cpu_state, cpu) = CPU_ONLINE;
86
87         xen_setup_cpu_clockevents();
88
89         /* We can take interrupts now: we're officially "up". */
90         local_irq_enable();
91
92         wmb();                  /* make sure everything is out */
93         cpu_idle();
94 }
95
96 static int xen_smp_intr_init(unsigned int cpu)
97 {
98         int rc;
99         const char *resched_name, *callfunc_name, *debug_name;
100
101         resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
102         rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
103                                     cpu,
104                                     xen_reschedule_interrupt,
105                                     IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
106                                     resched_name,
107                                     NULL);
108         if (rc < 0)
109                 goto fail;
110         per_cpu(resched_irq, cpu) = rc;
111
112         callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
113         rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
114                                     cpu,
115                                     xen_call_function_interrupt,
116                                     IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
117                                     callfunc_name,
118                                     NULL);
119         if (rc < 0)
120                 goto fail;
121         per_cpu(callfunc_irq, cpu) = rc;
122
123         debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
124         rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
125                                      IRQF_DISABLED | IRQF_PERCPU | IRQF_NOBALANCING,
126                                      debug_name, NULL);
127         if (rc < 0)
128                 goto fail;
129         per_cpu(debug_irq, cpu) = rc;
130
131         return 0;
132
133  fail:
134         if (per_cpu(resched_irq, cpu) >= 0)
135                 unbind_from_irqhandler(per_cpu(resched_irq, cpu), NULL);
136         if (per_cpu(callfunc_irq, cpu) >= 0)
137                 unbind_from_irqhandler(per_cpu(callfunc_irq, cpu), NULL);
138         if (per_cpu(debug_irq, cpu) >= 0)
139                 unbind_from_irqhandler(per_cpu(debug_irq, cpu), NULL);
140         return rc;
141 }
142
143 void __init xen_fill_possible_map(void)
144 {
145         int i, rc;
146
147         for (i = 0; i < NR_CPUS; i++) {
148                 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, i, NULL);
149                 if (rc >= 0)
150                         cpu_set(i, cpu_possible_map);
151         }
152 }
153
154 void __init xen_smp_prepare_boot_cpu(void)
155 {
156         int cpu;
157
158         BUG_ON(smp_processor_id() != 0);
159         native_smp_prepare_boot_cpu();
160
161         /* We've switched to the "real" per-cpu gdt, so make sure the
162            old memory can be recycled */
163         make_lowmem_page_readwrite(&per_cpu__gdt_page);
164
165         for_each_possible_cpu(cpu) {
166                 cpus_clear(per_cpu(cpu_sibling_map, cpu));
167                 /*
168                  * cpu_core_map lives in a per cpu area that is cleared
169                  * when the per cpu array is allocated.
170                  *
171                  * cpus_clear(per_cpu(cpu_core_map, cpu));
172                  */
173         }
174
175         xen_setup_vcpu_info_placement();
176 }
177
178 void __init xen_smp_prepare_cpus(unsigned int max_cpus)
179 {
180         unsigned cpu;
181
182         for_each_possible_cpu(cpu) {
183                 cpus_clear(per_cpu(cpu_sibling_map, cpu));
184                 /*
185                  * cpu_core_ map will be zeroed when the per
186                  * cpu area is allocated.
187                  *
188                  * cpus_clear(per_cpu(cpu_core_map, cpu));
189                  */
190         }
191
192         smp_store_cpu_info(0);
193         set_cpu_sibling_map(0);
194
195         if (xen_smp_intr_init(0))
196                 BUG();
197
198         xen_cpu_initialized_map = cpumask_of_cpu(0);
199
200         /* Restrict the possible_map according to max_cpus. */
201         while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
202                 for (cpu = NR_CPUS - 1; !cpu_possible(cpu); cpu--)
203                         continue;
204                 cpu_clear(cpu, cpu_possible_map);
205         }
206
207         for_each_possible_cpu (cpu) {
208                 struct task_struct *idle;
209
210                 if (cpu == 0)
211                         continue;
212
213                 idle = fork_idle(cpu);
214                 if (IS_ERR(idle))
215                         panic("failed fork for CPU %d", cpu);
216
217                 cpu_set(cpu, cpu_present_map);
218         }
219
220         //init_xenbus_allowed_cpumask();
221 }
222
223 static __cpuinit int
224 cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
225 {
226         struct vcpu_guest_context *ctxt;
227         struct gdt_page *gdt = &per_cpu(gdt_page, cpu);
228
229         if (cpu_test_and_set(cpu, xen_cpu_initialized_map))
230                 return 0;
231
232         ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
233         if (ctxt == NULL)
234                 return -ENOMEM;
235
236         ctxt->flags = VGCF_IN_KERNEL;
237         ctxt->user_regs.ds = __USER_DS;
238         ctxt->user_regs.es = __USER_DS;
239         ctxt->user_regs.fs = __KERNEL_PERCPU;
240         ctxt->user_regs.gs = 0;
241         ctxt->user_regs.ss = __KERNEL_DS;
242         ctxt->user_regs.eip = (unsigned long)cpu_bringup_and_idle;
243         ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
244
245         memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
246
247         xen_copy_trap_info(ctxt->trap_ctxt);
248
249         ctxt->ldt_ents = 0;
250
251         BUG_ON((unsigned long)gdt->gdt & ~PAGE_MASK);
252         make_lowmem_page_readonly(gdt->gdt);
253
254         ctxt->gdt_frames[0] = virt_to_mfn(gdt->gdt);
255         ctxt->gdt_ents      = ARRAY_SIZE(gdt->gdt);
256
257         ctxt->user_regs.cs = __KERNEL_CS;
258         ctxt->user_regs.esp = idle->thread.sp0 - sizeof(struct pt_regs);
259
260         ctxt->kernel_ss = __KERNEL_DS;
261         ctxt->kernel_sp = idle->thread.sp0;
262
263         ctxt->event_callback_cs     = __KERNEL_CS;
264         ctxt->event_callback_eip    = (unsigned long)xen_hypervisor_callback;
265         ctxt->failsafe_callback_cs  = __KERNEL_CS;
266         ctxt->failsafe_callback_eip = (unsigned long)xen_failsafe_callback;
267
268         per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
269         ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
270
271         if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
272                 BUG();
273
274         kfree(ctxt);
275         return 0;
276 }
277
278 int __cpuinit xen_cpu_up(unsigned int cpu)
279 {
280         struct task_struct *idle = idle_task(cpu);
281         int rc;
282
283 #if 0
284         rc = cpu_up_check(cpu);
285         if (rc)
286                 return rc;
287 #endif
288
289         init_gdt(cpu);
290         per_cpu(current_task, cpu) = idle;
291         irq_ctx_init(cpu);
292         xen_setup_timer(cpu);
293
294         /* make sure interrupts start blocked */
295         per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
296
297         rc = cpu_initialize_context(cpu, idle);
298         if (rc)
299                 return rc;
300
301         if (num_online_cpus() == 1)
302                 alternatives_smp_switch(1);
303
304         rc = xen_smp_intr_init(cpu);
305         if (rc)
306                 return rc;
307
308         smp_store_cpu_info(cpu);
309         set_cpu_sibling_map(cpu);
310         /* This must be done before setting cpu_online_map */
311         wmb();
312
313         cpu_set(cpu, cpu_online_map);
314
315         rc = HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
316         BUG_ON(rc);
317
318         return 0;
319 }
320
321 void xen_smp_cpus_done(unsigned int max_cpus)
322 {
323 }
324
325 static void stop_self(void *v)
326 {
327         int cpu = smp_processor_id();
328
329         /* make sure we're not pinning something down */
330         load_cr3(swapper_pg_dir);
331         /* should set up a minimal gdt */
332
333         HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL);
334         BUG();
335 }
336
337 void xen_smp_send_stop(void)
338 {
339         smp_call_function(stop_self, NULL, 0, 0);
340 }
341
342 void xen_smp_send_reschedule(int cpu)
343 {
344         xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
345 }
346
347
348 static void xen_send_IPI_mask(cpumask_t mask, enum ipi_vector vector)
349 {
350         unsigned cpu;
351
352         cpus_and(mask, mask, cpu_online_map);
353
354         for_each_cpu_mask(cpu, mask)
355                 xen_send_IPI_one(cpu, vector);
356 }
357
358 static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
359 {
360         void (*func) (void *info) = call_data->func;
361         void *info = call_data->info;
362         int wait = call_data->wait;
363
364         /*
365          * Notify initiating CPU that I've grabbed the data and am
366          * about to execute the function
367          */
368         mb();
369         atomic_inc(&call_data->started);
370         /*
371          * At this point the info structure may be out of scope unless wait==1
372          */
373         irq_enter();
374         (*func)(info);
375         __get_cpu_var(irq_stat).irq_call_count++;
376         irq_exit();
377
378         if (wait) {
379                 mb();           /* commit everything before setting finished */
380                 atomic_inc(&call_data->finished);
381         }
382
383         return IRQ_HANDLED;
384 }
385
386 int xen_smp_call_function_mask(cpumask_t mask, void (*func)(void *),
387                                void *info, int wait)
388 {
389         struct call_data_struct data;
390         int cpus, cpu;
391         bool yield;
392
393         /* Holding any lock stops cpus from going down. */
394         spin_lock(&call_lock);
395
396         cpu_clear(smp_processor_id(), mask);
397
398         cpus = cpus_weight(mask);
399         if (!cpus) {
400                 spin_unlock(&call_lock);
401                 return 0;
402         }
403
404         /* Can deadlock when called with interrupts disabled */
405         WARN_ON(irqs_disabled());
406
407         data.func = func;
408         data.info = info;
409         atomic_set(&data.started, 0);
410         data.wait = wait;
411         if (wait)
412                 atomic_set(&data.finished, 0);
413
414         call_data = &data;
415         mb();                   /* write everything before IPI */
416
417         /* Send a message to other CPUs and wait for them to respond */
418         xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
419
420         /* Make sure other vcpus get a chance to run if they need to. */
421         yield = false;
422         for_each_cpu_mask(cpu, mask)
423                 if (xen_vcpu_stolen(cpu))
424                         yield = true;
425
426         if (yield)
427                 HYPERVISOR_sched_op(SCHEDOP_yield, 0);
428
429         /* Wait for response */
430         while (atomic_read(&data.started) != cpus ||
431                (wait && atomic_read(&data.finished) != cpus))
432                 cpu_relax();
433
434         spin_unlock(&call_lock);
435
436         return 0;
437 }