ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/kernel/smpboot.c @ 7431:aabc33c3c0ac

Fix -xen builds.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Tue Oct 18 19:28:16 2005 +0100 (2005-10-18)
parents 3233e3921fdd
children e813ac9f7d23
line source
1 /*
2 * Xen SMP booting functions
3 *
4 * See arch/i386/kernel/smpboot.c for copyright and credits for derived
5 * portions of this file.
6 */
8 #include <linux/module.h>
9 #include <linux/config.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.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>
21 #include <asm/desc.h>
22 #include <asm/arch_hooks.h>
23 #include <asm/pgalloc.h>
24 #include <asm-xen/evtchn.h>
25 #include <asm-xen/xen-public/vcpu.h>
26 #include <asm-xen/xenbus.h>
28 #ifdef CONFIG_SMP_ALTERNATIVES
29 #include <asm/smp_alt.h>
30 #endif
32 extern irqreturn_t smp_reschedule_interrupt(int, void *, struct pt_regs *);
33 extern irqreturn_t smp_call_function_interrupt(int, void *, struct pt_regs *);
35 extern void local_setup_timer(unsigned int cpu);
36 extern void local_teardown_timer(unsigned int cpu);
38 extern void hypervisor_callback(void);
39 extern void failsafe_callback(void);
40 extern void system_call(void);
41 extern void smp_trap_init(trap_info_t *);
43 extern cpumask_t cpu_initialized;
45 /* Number of siblings per CPU package */
46 int smp_num_siblings = 1;
47 int phys_proc_id[NR_CPUS]; /* Package ID of each logical CPU */
48 EXPORT_SYMBOL(phys_proc_id);
49 int cpu_core_id[NR_CPUS]; /* Core ID of each logical CPU */
50 EXPORT_SYMBOL(cpu_core_id);
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 };
62 #endif
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 };
71 void *xquad_portio;
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);
77 #if defined(__i386__)
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;
82 #endif
84 void __init smp_alloc_memory(void)
85 {
86 }
88 static void xen_smp_intr_init(unsigned int cpu)
89 {
90 per_cpu(resched_irq, cpu) =
91 bind_ipi_to_irq(RESCHEDULE_VECTOR, cpu);
92 sprintf(resched_name[cpu], "resched%d", cpu);
93 BUG_ON(request_irq(per_cpu(resched_irq, cpu), smp_reschedule_interrupt,
94 SA_INTERRUPT, resched_name[cpu], NULL));
96 per_cpu(callfunc_irq, cpu) =
97 bind_ipi_to_irq(CALL_FUNCTION_VECTOR, cpu);
98 sprintf(callfunc_name[cpu], "callfunc%d", cpu);
99 BUG_ON(request_irq(per_cpu(callfunc_irq, cpu),
100 smp_call_function_interrupt,
101 SA_INTERRUPT, callfunc_name[cpu], NULL));
103 if (cpu != 0)
104 local_setup_timer(cpu);
105 }
107 #ifdef CONFIG_HOTPLUG_CPU
108 static void xen_smp_intr_exit(unsigned int cpu)
109 {
110 if (cpu != 0)
111 local_teardown_timer(cpu);
113 free_irq(per_cpu(resched_irq, cpu), NULL);
114 unbind_ipi_from_irq(RESCHEDULE_VECTOR, cpu);
116 free_irq(per_cpu(callfunc_irq, cpu), NULL);
117 unbind_ipi_from_irq(CALL_FUNCTION_VECTOR, cpu);
118 }
119 #endif
121 static void cpu_bringup(void)
122 {
123 if (!cpu_isset(smp_processor_id(), cpu_initialized))
124 cpu_init();
125 local_irq_enable();
126 cpu_idle();
127 }
129 void vcpu_prepare(int vcpu)
130 {
131 vcpu_guest_context_t ctxt;
132 struct task_struct *idle = idle_task(vcpu);
134 if (vcpu == 0)
135 return;
137 memset(&ctxt, 0, sizeof(ctxt));
139 ctxt.flags = VGCF_IN_KERNEL;
140 ctxt.user_regs.ds = __USER_DS;
141 ctxt.user_regs.es = __USER_DS;
142 ctxt.user_regs.fs = 0;
143 ctxt.user_regs.gs = 0;
144 ctxt.user_regs.ss = __KERNEL_DS;
145 ctxt.user_regs.eip = (unsigned long)cpu_bringup;
146 ctxt.user_regs.eflags = X86_EFLAGS_IF | 0x1000; /* IOPL_RING1 */
148 memset(&ctxt.fpu_ctxt, 0, sizeof(ctxt.fpu_ctxt));
150 smp_trap_init(ctxt.trap_ctxt);
152 ctxt.ldt_ents = 0;
154 ctxt.gdt_frames[0] = virt_to_mfn(cpu_gdt_descr[vcpu].address);
155 ctxt.gdt_ents = cpu_gdt_descr[vcpu].size / 8;
157 #ifdef __i386__
158 ctxt.user_regs.cs = __KERNEL_CS;
159 ctxt.user_regs.esp = idle->thread.esp;
161 ctxt.kernel_ss = __KERNEL_DS;
162 ctxt.kernel_sp = idle->thread.esp0;
164 ctxt.event_callback_cs = __KERNEL_CS;
165 ctxt.event_callback_eip = (unsigned long)hypervisor_callback;
166 ctxt.failsafe_callback_cs = __KERNEL_CS;
167 ctxt.failsafe_callback_eip = (unsigned long)failsafe_callback;
169 ctxt.ctrlreg[3] = virt_to_mfn(swapper_pg_dir) << PAGE_SHIFT;
170 #else
171 ctxt.user_regs.cs = __KERNEL_CS | 3;
172 ctxt.user_regs.esp = idle->thread.rsp;
174 ctxt.kernel_ss = __KERNEL_DS;
175 ctxt.kernel_sp = idle->thread.rsp0;
177 ctxt.event_callback_eip = (unsigned long)hypervisor_callback;
178 ctxt.failsafe_callback_eip = (unsigned long)failsafe_callback;
179 ctxt.syscall_callback_eip = (unsigned long)system_call;
181 ctxt.ctrlreg[3] = virt_to_mfn(init_level4_pgt) << PAGE_SHIFT;
182 #endif
184 BUG_ON(HYPERVISOR_vcpu_op(VCPUOP_initialise, vcpu, &ctxt));
185 }
187 void __init smp_prepare_cpus(unsigned int max_cpus)
188 {
189 int cpu, rc;
190 struct task_struct *idle;
192 if (max_cpus == 0)
193 return;
195 xen_smp_intr_init(0);
197 for (cpu = 1; cpu < max_cpus; cpu++) {
198 rc = HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL);
199 if (rc == -ENOENT)
200 break;
201 BUG_ON(rc != 0);
203 cpu_data[cpu] = boot_cpu_data;
204 cpu_2_logical_apicid[cpu] = cpu;
205 x86_cpu_to_apicid[cpu] = cpu;
207 idle = fork_idle(cpu);
208 if (IS_ERR(idle))
209 panic("failed fork for CPU %d", cpu);
211 irq_ctx_init(cpu);
213 cpu_gdt_descr[cpu].address =
214 __get_free_page(GFP_KERNEL|__GFP_ZERO);
215 BUG_ON(cpu_gdt_descr[0].size > PAGE_SIZE);
216 cpu_gdt_descr[cpu].size = cpu_gdt_descr[0].size;
217 memcpy((void *)cpu_gdt_descr[cpu].address,
218 (void *)cpu_gdt_descr[0].address,
219 cpu_gdt_descr[0].size);
220 make_page_readonly((void *)cpu_gdt_descr[cpu].address);
222 cpu_set(cpu, cpu_possible_map);
223 if (xen_start_info->flags & SIF_INITDOMAIN)
224 cpu_set(cpu, cpu_present_map);
226 vcpu_prepare(cpu);
227 }
229 /* Currently, Xen gives no dynamic NUMA/HT info. */
230 for (cpu = 0; cpu < NR_CPUS; cpu++) {
231 cpus_clear(cpu_sibling_map[cpu]);
232 cpus_clear(cpu_core_map[cpu]);
233 }
235 #ifdef CONFIG_X86_IO_APIC
236 /*
237 * Here we can be sure that there is an IO-APIC in the system. Let's
238 * go and set it up:
239 */
240 if (!skip_ioapic_setup && nr_ioapics)
241 setup_IO_APIC();
242 #endif
243 }
245 void __devinit smp_prepare_boot_cpu(void)
246 {
247 cpu_possible_map = cpumask_of_cpu(0);
248 cpu_present_map = cpumask_of_cpu(0);
249 cpu_online_map = cpumask_of_cpu(0);
251 cpu_data[0] = boot_cpu_data;
252 cpu_2_logical_apicid[0] = 0;
253 x86_cpu_to_apicid[0] = 0;
255 current_thread_info()->cpu = 0;
256 cpus_clear(cpu_sibling_map[0]);
257 cpu_set(0, cpu_sibling_map[0]);
259 cpus_clear(cpu_core_map[0]);
260 cpu_set(0, cpu_core_map[0]);
261 }
263 static void vcpu_hotplug(unsigned int cpu)
264 {
265 int err;
266 char dir[32], state[32];
268 if ((cpu >= NR_CPUS) || !cpu_possible(cpu))
269 return;
271 sprintf(dir, "cpu/%d", cpu);
272 err = xenbus_scanf(NULL, dir, "availability", "%s", state);
273 if (err != 1) {
274 printk(KERN_ERR "XENBUS: Unable to read cpu state\n");
275 return;
276 }
278 if (strcmp(state, "online") == 0) {
279 cpu_set(cpu, cpu_present_map);
280 (void)cpu_up(cpu);
281 } else if (strcmp(state, "offline") == 0) {
282 #ifdef CONFIG_HOTPLUG_CPU
283 (void)cpu_down(cpu);
284 #else
285 printk(KERN_INFO "Ignoring CPU%d hotplug request\n", cpu);
286 #endif
287 } else {
288 printk(KERN_ERR "XENBUS: unknown state(%s) on CPU%d\n",
289 state, cpu);
290 }
291 }
293 static void handle_vcpu_hotplug_event(
294 struct xenbus_watch *watch, const char **vec, unsigned int len)
295 {
296 int cpu;
297 char *cpustr;
298 const char *node = vec[XS_WATCH_PATH];
300 if ((cpustr = strstr(node, "cpu/")) != NULL) {
301 sscanf(cpustr, "cpu/%d", &cpu);
302 vcpu_hotplug(cpu);
303 }
304 }
306 static int setup_cpu_watcher(struct notifier_block *notifier,
307 unsigned long event, void *data)
308 {
309 int i;
311 static struct xenbus_watch cpu_watch = {
312 .node = "cpu",
313 .callback = handle_vcpu_hotplug_event };
314 (void)register_xenbus_watch(&cpu_watch);
316 if (!(xen_start_info->flags & SIF_INITDOMAIN)) {
317 for_each_cpu(i)
318 vcpu_hotplug(i);
319 printk(KERN_INFO "Brought up %ld CPUs\n",
320 (long)num_online_cpus());
321 }
323 return NOTIFY_DONE;
324 }
326 static int __init setup_vcpu_hotplug_event(void)
327 {
328 static struct notifier_block xsn_cpu = {
329 .notifier_call = setup_cpu_watcher };
330 register_xenstore_notifier(&xsn_cpu);
331 return 0;
332 }
334 subsys_initcall(setup_vcpu_hotplug_event);
336 #ifdef CONFIG_HOTPLUG_CPU
338 int __cpu_disable(void)
339 {
340 cpumask_t map = cpu_online_map;
341 int cpu = smp_processor_id();
343 if (cpu == 0)
344 return -EBUSY;
346 cpu_clear(cpu, map);
347 fixup_irqs(map);
348 cpu_clear(cpu, cpu_online_map);
350 return 0;
351 }
353 void __cpu_die(unsigned int cpu)
354 {
355 while (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpu, NULL)) {
356 current->state = TASK_UNINTERRUPTIBLE;
357 schedule_timeout(HZ/10);
358 }
360 xen_smp_intr_exit(cpu);
362 #ifdef CONFIG_SMP_ALTERNATIVES
363 if (num_online_cpus() == 1)
364 unprepare_for_smp();
365 #endif
366 }
368 #else /* !CONFIG_HOTPLUG_CPU */
370 int __cpu_disable(void)
371 {
372 return -ENOSYS;
373 }
375 void __cpu_die(unsigned int cpu)
376 {
377 BUG();
378 }
380 #endif /* CONFIG_HOTPLUG_CPU */
382 int __devinit __cpu_up(unsigned int cpu)
383 {
384 #ifdef CONFIG_SMP_ALTERNATIVES
385 if (num_online_cpus() == 1)
386 prepare_for_smp();
387 #endif
389 xen_smp_intr_init(cpu);
390 cpu_set(cpu, cpu_online_map);
391 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL);
393 return 0;
394 }
396 void __init smp_cpus_done(unsigned int max_cpus)
397 {
398 }
400 /*
401 * Local variables:
402 * c-file-style: "linux"
403 * indent-tabs-mode: t
404 * c-indent-level: 8
405 * c-basic-offset: 8
406 * tab-width: 8
407 * End:
408 */