kvm: qemu: conditionally bypass user level init/sipi handling

This patch bypasses user level init/sipi handling when in-kernel
APIC is used.

Signed-off-by: Qing He <qing.he@intel.com>
Signed-off-by: Xin Li <xin.b.li@intel.com>

---
 bios/rombios32.c |    5 +++--
 qemu/exec.c      |    3 ++-
 qemu/hw/apic.c   |    4 ++--
 qemu/qemu-kvm.c  |   10 ++++++----
 4 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/bios/rombios32.c b/bios/rombios32.c
index faf771e..c8c1e74 100755
--- a/bios/rombios32.c
+++ b/bios/rombios32.c
@@ -356,7 +356,8 @@ void delay_ms(int n)
 {
     int i, j;
     for(i = 0; i < n; i++) {
-#ifdef BX_QEMU
+#if 0
+//#ifdef BX_QEMU
         /* approximative ! */
         for(j = 0; j < 1000000; j++);
 #else
@@ -442,7 +443,7 @@ void smp_probe(void)
         sipi_vector = AP_BOOT_ADDR >> 12;
         writel(APIC_BASE + APIC_ICR_LOW, 0x000C4600 | sipi_vector);
         
-        delay_ms(10);
+        delay_ms(2000);
 
         smp_cpus = readw((void *)CPU_COUNT_ADDR);
     }
diff --git a/qemu/exec.c b/qemu/exec.c
index 2883b07..21764bc 100644
--- a/qemu/exec.c
+++ b/qemu/exec.c
@@ -78,6 +78,7 @@
 
 #ifdef USE_KVM
 extern int kvm_allowed;
+extern kvm_context_t kvm_context;
 #endif
 
 TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
@@ -1149,7 +1150,7 @@ void cpu_interrupt(CPUState *env, int mask)
 
     env->interrupt_request |= mask;
 #ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_allowed && !kvm_irqchip_in_kernel(kvm_context))
 	kvm_update_interrupt_request(env);
 #endif
     /* if the cpu is currently executing code, we must unlink it and
diff --git a/qemu/hw/apic.c b/qemu/hw/apic.c
index 30dacaa..f479061 100644
--- a/qemu/hw/apic.c
+++ b/qemu/hw/apic.c
@@ -406,7 +406,7 @@ static void apic_init_ipi(APICState *s)
     s->initial_count_load_time = 0;
     s->next_time = 0;
 #ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_allowed && !kvm_irqchip_in_kernel(kvm_context))
 	if (s->cpu_env)
 	    kvm_apic_init(s->cpu_env);
 #endif
@@ -423,7 +423,7 @@ static void apic_startup(APICState *s, int vector_num)
                            0xffff, 0);
     env->hflags &= ~HF_HALTED_MASK;
 #if USE_KVM
-    if (kvm_allowed)
+    if (kvm_allowed && !kvm_irqchip_in_kernel(kvm_context))
 	kvm_update_after_sipi(env);
 #endif
 }
diff --git a/qemu/qemu-kvm.c b/qemu/qemu-kvm.c
index 53f6845..dd33942 100644
--- a/qemu/qemu-kvm.c
+++ b/qemu/qemu-kvm.c
@@ -623,7 +623,8 @@ static void kvm_main_loop_wait(CPUState *env, int timeout)
     if (env->cpu_index == 0)
 	kvm_eat_signals(env, timeout);
     else {
-	if (timeout || vcpu_info[env->cpu_index].stopped) {
+ 	if (!kvm_irqchip_in_kernel(kvm_context) &&
+	    (timeout || vcpu_info[env->cpu_index].stopped)) {
 	    sigset_t set;
 	    int n;
 
@@ -737,9 +738,9 @@ static int kvm_main_loop_cpu(CPUState *env)
 	    kvm_main_loop_wait(env, 10);
 	if (env->interrupt_request & CPU_INTERRUPT_HARD)
 	    env->hflags &= ~HF_HALTED_MASK;
-	if (info->sipi_needed)
+	if (!kvm_irqchip_in_kernel(kvm_context) && info->sipi_needed)
 	    update_regs_for_sipi(env);
-	if (info->init)
+	if (!kvm_irqchip_in_kernel(kvm_context) && info->init)
 	    update_regs_for_init(env);
 	if (!(env->hflags & HF_HALTED_MASK) && !info->init)
 	    kvm_cpu_exec(env);
@@ -794,7 +795,8 @@ int kvm_init_ap(void)
     kvm_add_signal(SIGIO);
     kvm_add_signal(SIGALRM);
     kvm_add_signal(SIGUSR2);
-    kvm_add_signal(SIG_IPI);
+    if (!kvm_irqchip_in_kernel(kvm_context))
+        kvm_add_signal(SIG_IPI);
 
     vcpu_env = first_cpu;
     signal(SIG_IPI, sig_ipi_handler);

