Index: ioemu/hw/piix4acpi.c
===================================================================
--- ioemu.orig/hw/piix4acpi.c	2006-08-17 19:50:02.407869874 +0100
+++ ioemu/hw/piix4acpi.c	2006-08-17 19:50:05.060576667 +0100
@@ -24,31 +24,30 @@
  */
 
 #include "vl.h"
-#define FREQUENCE_PMTIMER  3753425
+#define FREQUENCE_PMTIMER  3579545
 /* acpi register bit define here  */
 
-/* PM1_STS 						*/
-#define TMROF_STS 	  (1 << 0)
-#define BM_STS 	  	  (1 << 4)
-#define GBL_STS 	  (1 << 5)
-#define PWRBTN_STS 	  (1 << 8)
-#define RTC_STS 	  (1 << 10)
+/* PM1_STS */
+#define TMROF_STS         (1 << 0)
+#define BM_STS            (1 << 4)
+#define GBL_STS           (1 << 5)
+#define PWRBTN_STS        (1 << 8)
+#define RTC_STS           (1 << 10)
 #define PRBTNOR_STS       (1 << 11)
-#define WAK_STS 	  (1 << 15)
-/* PM1_EN						*/
+#define WAK_STS           (1 << 15)
+/* PM1_EN */
 #define TMROF_EN          (1 << 0)
 #define GBL_EN            (1 << 5)
 #define PWRBTN_EN         (1 << 8)
-#define RTC_EN   	  (1 << 10)
-/* PM1_CNT						*/
+#define RTC_EN            (1 << 10)
+/* PM1_CNT */
 #define SCI_EN            (1 << 0)
 #define GBL_RLS           (1 << 2)
-#define SLP_EN   	  (1 << 13)
+#define SLP_EN            (1 << 13)
 
 typedef struct AcpiDeviceState AcpiDeviceState;
 AcpiDeviceState *acpi_device_table;
 
-/* Bits of PM1a register define here  */
 typedef struct PM1Event_BLK {
     uint16_t pm1_status; /* pm1a_EVT_BLK */
     uint16_t pm1_enable; /* pm1a_EVT_BLK+2 */
@@ -61,17 +60,11 @@
     uint16_t pm1_enable; /* pm1a_EVT_BLK+2 */
     uint16_t pm1_control; /* pm1a_ECNT_BLK */
     uint32_t pm1_timer; /* pmtmr_BLK */
+    uint64_t old_vmck_ticks; /* using vm_clock counter */
 } PCIAcpiState;
 
 static PCIAcpiState *acpi_state;
 
-static inline void acpi_set_irq(PCIAcpiState *s)
-{
-/* no real SCI event need for now, so comment the following line out */
-/*  pic_set_irq(s->irq, 1); */
-    printf("acpi_set_irq: s->irq %x \n",s->irq);
-}
-
 static void acpi_reset(PCIAcpiState *s)
 {
     uint8_t *pci_conf;
@@ -84,6 +77,7 @@
     s->pm1_enable = 0x00;    /* TMROF_EN should cleared */
     s->pm1_control = SCI_EN; /* SCI_EN */
     s->pm1_timer = 0;
+    s->old_vmck_ticks = qemu_get_clock(vm_clock);
 }
 
 /*byte access  */
@@ -95,8 +89,8 @@
         s->pm1_status = s->pm1_status&!TMROF_STS;
 
     if ((val&GBL_STS)==GBL_STS)
-        s->pm1_status = s->pm1_status&!GBL_STS;     
-    
+        s->pm1_status = s->pm1_status&!GBL_STS;
+
 /*     printf("acpiPm1Status_writeb \n addr %x val:%x pm1_status:%x \n", addr, val,s->pm1_status); */
 }
 
@@ -115,7 +109,7 @@
 {
     PCIAcpiState *s = opaque;
 
-     s->pm1_status = (val<<8)||(s->pm1_status);
+    s->pm1_status = (val<<8)||(s->pm1_status);
 /*     printf("acpiPm1StatusP1_writeb \n addr %x val:%x\n", addr, val); */
 }
 
@@ -220,7 +214,7 @@
         s->pm1_status = s->pm1_status&!TMROF_STS;
 
     if ((val&GBL_STS)==GBL_STS)
-        s->pm1_status = s->pm1_status&!GBL_STS;     
+        s->pm1_status = s->pm1_status&!GBL_STS;
 
 /*    printf("acpiPm1Status_writew \n addr %x val:%x pm1_status:%x \n", addr, val,s->pm1_status); */
 }
@@ -288,13 +282,15 @@
 
 }
 
-static void acpiPm1Event_readl(void *opaque, uint32_t addr)
+static uint32_t acpiPm1Event_readl(void *opaque, uint32_t addr)
 {
     PCIAcpiState *s = opaque;
     uint32_t val;
 
-    val=s->pm1_status|(s->pm1_enable<<16);
+    val = s->pm1_status|(s->pm1_enable<<16);
 /*    printf("acpiPm1Event_readl \n addr %x val:%x\n", addr, val);    */
+
+    return val;
 }
 
 static void acpiPm1Timer_writel(void *opaque, uint32_t addr, uint32_t val)
@@ -302,17 +298,21 @@
     PCIAcpiState *s = opaque;
 
     s->pm1_timer = val;
-/*    printf("acpiPm1Timer_writel \n addr %x val:%x\n", addr, val); */
+    s->old_vmck_ticks = qemu_get_clock(vm_clock) +
+        muldiv64(val, FREQUENCE_PMTIMER, ticks_per_sec);
 }
 
 static uint32_t acpiPm1Timer_readl(void *opaque, uint32_t addr)
 {
     PCIAcpiState *s = opaque;
-    uint32_t val;
+    int64_t current_vmck_ticks = qemu_get_clock(vm_clock);
+    int64_t vmck_ticks_delta = current_vmck_ticks - s->old_vmck_ticks;
 
-    val = s->pm1_timer;
-/*    printf("acpiPm1Timer_readl \n addr %x val:%x\n", addr, val); */
-    return val;
+    if (s->old_vmck_ticks)
+        s->pm1_timer += muldiv64(vmck_ticks_delta, FREQUENCE_PMTIMER,
+                                 ticks_per_sec);
+    s->old_vmck_ticks = current_vmck_ticks;
+    return s->pm1_timer;
 }
 
 static void acpi_map(PCIDevice *pci_dev, int region_num,
@@ -320,7 +320,7 @@
 {
     PCIAcpiState *d = (PCIAcpiState *)pci_dev;
 
-    printf("register acpi io \n");
+    printf("register acpi io\n");
 
     /* Byte access */
     register_ioport_write(addr, 1, 1, acpiPm1Status_writeb, d);
@@ -336,14 +336,14 @@
     register_ioport_write(addr + 4, 1, 1, acpiPm1Control_writeb, d);
     register_ioport_read(addr + 4, 1, 1, acpiPm1Control_readb, d);
     register_ioport_write(addr + 4 + 1, 1, 1, acpiPm1ControlP1_writeb, d);
-    register_ioport_read(addr + 4 +1, 1, 1, acpiPm1ControlP1_readb, d);	
+    register_ioport_read(addr + 4 +1, 1, 1, acpiPm1ControlP1_readb, d);
 
     /* Word access */
     register_ioport_write(addr, 2, 2, acpiPm1Status_writew, d);
     register_ioport_read(addr, 2, 2, acpiPm1Status_readw, d);
 
     register_ioport_write(addr + 2, 2, 2, acpiPm1Enable_writew, d);
-    register_ioport_read(addr + 2, 2, 2, acpiPm1Enable_readw, d); 
+    register_ioport_read(addr + 2, 2, 2, acpiPm1Enable_readw, d);
 
     register_ioport_write(addr + 4, 2, 2, acpiPm1Control_writew, d);
     register_ioport_read(addr + 4, 2, 2, acpiPm1Control_readw, d);
@@ -351,11 +351,10 @@
     /* DWord access */
     register_ioport_write(addr, 4, 4, acpiPm1Event_writel, d);
     register_ioport_read(addr, 4, 4, acpiPm1Event_readl, d);
-		
+
     register_ioport_write(addr + 8, 4, 4, acpiPm1Timer_writel, d);
     register_ioport_read(addr + 8, 4, 4, acpiPm1Timer_readl, d);
 }
-													
 
 /* PIIX4 acpi pci configuration space, func 2 */
 void pci_piix4_acpi_init(PCIBus *bus, int devfn)
@@ -384,5 +383,5 @@
     pci_register_io_region((PCIDevice *)d, 4, 0x10,
                            PCI_ADDRESS_SPACE_IO, acpi_map);
 
-    acpi_reset (d);
+    acpi_reset(d);
 }
