KVM: Fix ioapic edge-triggered interrupts

- clear irr after service
- do not service after unmasking; wait for a new edge

Signed-off-by: Avi Kivity <avi@qumranet.com>
diff --git a/drivers/kvm/ioapic.c b/drivers/kvm/ioapic.c
index b0d3843..9eb5058 100644
--- a/drivers/kvm/ioapic.c
+++ b/drivers/kvm/ioapic.c
@@ -90,6 +90,8 @@
 		if (pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
 			pent->fields.remote_irr = 1;
 	}
+	if (!pent->fields.trig_mode)
+		ioapic->irr &= ~(1 << idx);
 }
 
 static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
@@ -121,7 +123,8 @@
 			ioapic->redirtbl[index].bits |= (u32) val;
 			ioapic->redirtbl[index].fields.remote_irr = 0;
 		}
-		ioapic_service(ioapic, index);
+		if (ioapic->irr & (1 << index))
+			ioapic_service(ioapic, index);
 		break;
 	}
 }