4 * Copyright (c) 2006 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>
21 #include "qemu-timer.h"
28 /* i82731AB (PIIX4) compatible power management function */
29 #define PM_FREQ 3579545
31 #define ACPI_DBG_IO_ADDR 0xb044
33 typedef struct PIIX4PMState
{
41 int64_t tmr_overflow_time
;
57 #define RSM_STS (1 << 15)
58 #define PWRBTN_STS (1 << 8)
59 #define RTC_EN (1 << 10)
60 #define PWRBTN_EN (1 << 8)
61 #define GBL_EN (1 << 5)
62 #define TMROF_EN (1 << 0)
64 #define SCI_EN (1 << 0)
66 #define SUS_EN (1 << 13)
68 #define ACPI_ENABLE 0xf1
69 #define ACPI_DISABLE 0xf0
71 #define SMBHSTSTS 0x00
72 #define SMBHSTCNT 0x02
73 #define SMBHSTCMD 0x03
74 #define SMBHSTADD 0x04
75 #define SMBHSTDAT0 0x05
76 #define SMBHSTDAT1 0x06
77 #define SMBBLKDAT 0x07
79 static PIIX4PMState
*pm_state
;
81 static uint32_t get_pmtmr(PIIX4PMState
*s
)
84 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
, get_ticks_per_sec());
88 static int get_pmsts(PIIX4PMState
*s
)
92 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
, get_ticks_per_sec());
93 if (d
>= s
->tmr_overflow_time
)
98 static void pm_update_sci(PIIX4PMState
*s
)
100 int sci_level
, pmsts
;
103 pmsts
= get_pmsts(s
);
104 sci_level
= (((pmsts
& s
->pmen
) &
105 (RTC_EN
| PWRBTN_EN
| GBL_EN
| TMROF_EN
)) != 0);
106 qemu_set_irq(s
->irq
, sci_level
);
107 /* schedule a timer interruption if needed */
108 if ((s
->pmen
& TMROF_EN
) && !(pmsts
& TMROF_EN
)) {
109 expire_time
= muldiv64(s
->tmr_overflow_time
, get_ticks_per_sec(), PM_FREQ
);
110 qemu_mod_timer(s
->tmr_timer
, expire_time
);
112 qemu_del_timer(s
->tmr_timer
);
116 static void pm_tmr_timer(void *opaque
)
118 PIIX4PMState
*s
= opaque
;
122 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
124 PIIX4PMState
*s
= opaque
;
131 pmsts
= get_pmsts(s
);
132 if (pmsts
& val
& TMROF_EN
) {
133 /* if TMRSTS is reset, then compute the new overflow time */
134 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
,
135 get_ticks_per_sec());
136 s
->tmr_overflow_time
= (d
+ 0x800000LL
) & ~0x7fffffLL
;
149 s
->pmcntrl
= val
& ~(SUS_EN
);
151 /* change suspend type */
152 sus_typ
= (val
>> 10) & 7;
154 case 0: /* soft power off */
155 qemu_system_shutdown_request();
158 /* RSM_STS should be set on resume. Pretend that resume
159 was caused by power button */
160 s
->pmsts
|= (RSM_STS
| PWRBTN_STS
);
161 qemu_system_reset_request();
163 qemu_irq_raise(s
->cmos_s3
);
175 printf("PM writew port=0x%04x val=0x%04x\n", addr
, val
);
179 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
181 PIIX4PMState
*s
= opaque
;
200 printf("PM readw port=0x%04x val=0x%04x\n", addr
, val
);
205 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
207 // PIIX4PMState *s = opaque;
210 printf("PM writel port=0x%04x val=0x%08x\n", addr
, val
);
214 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
216 PIIX4PMState
*s
= opaque
;
229 printf("PM readl port=0x%04x val=0x%08x\n", addr
, val
);
234 static void pm_smi_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
236 PIIX4PMState
*s
= opaque
;
239 printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr
, val
);
244 /* ACPI specs 3.0, 4.7.2.5 */
245 if (val
== ACPI_ENABLE
) {
246 s
->pmcntrl
|= SCI_EN
;
247 } else if (val
== ACPI_DISABLE
) {
248 s
->pmcntrl
&= ~SCI_EN
;
251 if (s
->dev
.config
[0x5b] & (1 << 1)) {
253 qemu_irq_raise(s
->smi_irq
);
261 static uint32_t pm_smi_readb(void *opaque
, uint32_t addr
)
263 PIIX4PMState
*s
= opaque
;
273 printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr
, val
);
278 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
281 printf("ACPI: DBG: 0x%08x\n", val
);
285 static void smb_transaction(PIIX4PMState
*s
)
287 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
288 uint8_t read
= s
->smb_addr
& 0x01;
289 uint8_t cmd
= s
->smb_cmd
;
290 uint8_t addr
= s
->smb_addr
>> 1;
291 i2c_bus
*bus
= s
->smbus
;
294 printf("SMBus trans addr=0x%02x prot=0x%02x\n", addr
, prot
);
298 smbus_quick_command(bus
, addr
, read
);
302 s
->smb_data0
= smbus_receive_byte(bus
, addr
);
304 smbus_send_byte(bus
, addr
, cmd
);
309 s
->smb_data0
= smbus_read_byte(bus
, addr
, cmd
);
311 smbus_write_byte(bus
, addr
, cmd
, s
->smb_data0
);
317 val
= smbus_read_word(bus
, addr
, cmd
);
319 s
->smb_data1
= val
>> 8;
321 smbus_write_word(bus
, addr
, cmd
, (s
->smb_data1
<< 8) | s
->smb_data0
);
326 s
->smb_data0
= smbus_read_block(bus
, addr
, cmd
, s
->smb_data
);
328 smbus_write_block(bus
, addr
, cmd
, s
->smb_data
, s
->smb_data0
);
340 static void smb_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
342 PIIX4PMState
*s
= opaque
;
345 printf("SMB writeb port=0x%04x val=0x%02x\n", addr
, val
);
370 s
->smb_data
[s
->smb_index
++] = val
;
371 if (s
->smb_index
> 31)
379 static uint32_t smb_ioport_readb(void *opaque
, uint32_t addr
)
381 PIIX4PMState
*s
= opaque
;
391 val
= s
->smb_ctl
& 0x1f;
406 val
= s
->smb_data
[s
->smb_index
++];
407 if (s
->smb_index
> 31)
415 printf("SMB readb port=0x%04x val=0x%02x\n", addr
, val
);
420 static void pm_io_space_update(PIIX4PMState
*s
)
424 if (s
->dev
.config
[0x80] & 1) {
425 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
426 pm_io_base
&= 0xffc0;
428 /* XXX: need to improve memory and ioport allocation */
430 printf("PM: mapping to 0x%x\n", pm_io_base
);
432 register_ioport_write(pm_io_base
, 64, 2, pm_ioport_writew
, s
);
433 register_ioport_read(pm_io_base
, 64, 2, pm_ioport_readw
, s
);
434 register_ioport_write(pm_io_base
, 64, 4, pm_ioport_writel
, s
);
435 register_ioport_read(pm_io_base
, 64, 4, pm_ioport_readl
, s
);
439 static void pm_write_config(PCIDevice
*d
,
440 uint32_t address
, uint32_t val
, int len
)
442 pci_default_write_config(d
, address
, val
, len
);
443 if (range_covers_byte(address
, len
, 0x80))
444 pm_io_space_update((PIIX4PMState
*)d
);
447 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
449 PIIX4PMState
*s
= opaque
;
451 pm_io_space_update(s
);
455 static const VMStateDescription vmstate_acpi
= {
458 .minimum_version_id
= 1,
459 .minimum_version_id_old
= 1,
460 .post_load
= vmstate_acpi_post_load
,
461 .fields
= (VMStateField
[]) {
462 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
463 VMSTATE_UINT16(pmsts
, PIIX4PMState
),
464 VMSTATE_UINT16(pmen
, PIIX4PMState
),
465 VMSTATE_UINT16(pmcntrl
, PIIX4PMState
),
466 VMSTATE_UINT8(apmc
, PIIX4PMState
),
467 VMSTATE_UINT8(apms
, PIIX4PMState
),
468 VMSTATE_TIMER(tmr_timer
, PIIX4PMState
),
469 VMSTATE_INT64(tmr_overflow_time
, PIIX4PMState
),
470 VMSTATE_END_OF_LIST()
474 static void piix4_reset(void *opaque
)
476 PIIX4PMState
*s
= opaque
;
477 uint8_t *pci_conf
= s
->dev
.config
;
484 if (s
->kvm_enabled
) {
485 /* Mark SMM as already inited (until KVM supports SMM). */
486 pci_conf
[0x5B] = 0x02;
490 static void piix4_powerdown(void *opaque
, int irq
, int power_failing
)
492 PIIX4PMState
*s
= opaque
;
495 qemu_system_shutdown_request();
496 } else if (s
->pmen
& PWRBTN_EN
) {
497 s
->pmsts
|= PWRBTN_EN
;
502 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
503 qemu_irq sci_irq
, qemu_irq cmos_s3
, qemu_irq smi_irq
,
509 s
= (PIIX4PMState
*)pci_register_device(bus
,
510 "PM", sizeof(PIIX4PMState
),
511 devfn
, NULL
, pm_write_config
);
513 pci_conf
= s
->dev
.config
;
514 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
515 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371AB_3
);
516 pci_conf
[0x06] = 0x80;
517 pci_conf
[0x07] = 0x02;
518 pci_conf
[0x08] = 0x03; // revision number
519 pci_conf
[0x09] = 0x00;
520 pci_config_set_class(pci_conf
, PCI_CLASS_BRIDGE_OTHER
);
521 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
522 pci_conf
[0x3d] = 0x01; // interrupt pin 1
524 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
526 register_ioport_write(0xb2, 2, 1, pm_smi_writeb
, s
);
527 register_ioport_read(0xb2, 2, 1, pm_smi_readb
, s
);
529 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
531 s
->kvm_enabled
= kvm_enabled
;
532 if (s
->kvm_enabled
) {
533 /* Mark SMM as already inited to prevent SMM from running. KVM does not
534 * support SMM mode. */
535 pci_conf
[0x5B] = 0x02;
538 /* XXX: which specification is used ? The i82731AB has different
540 pci_conf
[0x5f] = (parallel_hds
[0] != NULL
? 0x80 : 0) | 0x10;
541 pci_conf
[0x63] = 0x60;
542 pci_conf
[0x67] = (serial_hds
[0] != NULL
? 0x08 : 0) |
543 (serial_hds
[1] != NULL
? 0x90 : 0);
545 pci_conf
[0x90] = smb_io_base
| 1;
546 pci_conf
[0x91] = smb_io_base
>> 8;
547 pci_conf
[0xd2] = 0x09;
548 register_ioport_write(smb_io_base
, 64, 1, smb_ioport_writeb
, s
);
549 register_ioport_read(smb_io_base
, 64, 1, smb_ioport_readb
, s
);
551 s
->tmr_timer
= qemu_new_timer(vm_clock
, pm_tmr_timer
, s
);
553 qemu_system_powerdown
= *qemu_allocate_irqs(piix4_powerdown
, s
, 1);
555 vmstate_register(0, &vmstate_acpi
, s
);
557 s
->smbus
= i2c_init_bus(NULL
, "i2c");
559 s
->cmos_s3
= cmos_s3
;
560 s
->smi_irq
= smi_irq
;
561 qemu_register_reset(piix4_reset
, s
);
566 #define GPE_BASE 0xafe0
567 #define PCI_BASE 0xae00
568 #define PCI_EJ_BASE 0xae08
571 uint16_t sts
; /* status */
572 uint16_t en
; /* enabled */
580 static struct gpe_regs gpe
;
581 static struct pci_status pci0_status
;
583 static uint32_t gpe_read_val(uint16_t val
, uint32_t addr
)
586 return (val
>> 8) & 0xff;
590 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
593 struct gpe_regs
*g
= opaque
;
597 val
= gpe_read_val(g
->sts
, addr
);
601 val
= gpe_read_val(g
->en
, addr
);
608 printf("gpe read %x == %x\n", addr
, val
);
613 static void gpe_write_val(uint16_t *cur
, int addr
, uint32_t val
)
616 *cur
= (*cur
& 0xff) | (val
<< 8);
618 *cur
= (*cur
& 0xff00) | (val
& 0xff);
621 static void gpe_reset_val(uint16_t *cur
, int addr
, uint32_t val
)
623 uint16_t x1
, x0
= val
& 0xff;
624 int shift
= (addr
& 1) ? 8 : 0;
626 x1
= (*cur
>> shift
) & 0xff;
630 *cur
= (*cur
& (0xff << (8 - shift
))) | (x1
<< shift
);
633 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
635 struct gpe_regs
*g
= opaque
;
639 gpe_reset_val(&g
->sts
, addr
, val
);
643 gpe_write_val(&g
->en
, addr
, val
);
650 printf("gpe write %x <== %d\n", addr
, val
);
654 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
657 struct pci_status
*g
= opaque
;
670 printf("pcihotplug read %x == %x\n", addr
, val
);
675 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
677 struct pci_status
*g
= opaque
;
688 printf("pcihotplug write %x <== %d\n", addr
, val
);
692 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
695 printf("pciej read %x\n", addr
);
700 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
702 BusState
*bus
= opaque
;
703 DeviceState
*qdev
, *next
;
705 int slot
= ffs(val
) - 1;
707 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
708 dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
709 if (PCI_SLOT(dev
->devfn
) == slot
) {
716 printf("pciej write %x <== %d\n", addr
, val
);
720 static int piix4_device_hotplug(PCIDevice
*dev
, int state
);
722 void piix4_acpi_system_hot_add_init(PCIBus
*bus
)
724 register_ioport_write(GPE_BASE
, 4, 1, gpe_writeb
, &gpe
);
725 register_ioport_read(GPE_BASE
, 4, 1, gpe_readb
, &gpe
);
727 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, &pci0_status
);
728 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, &pci0_status
);
730 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, bus
);
731 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, bus
);
733 pci_bus_hotplug(bus
, piix4_device_hotplug
);
736 static void enable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
739 p
->up
|= (1 << slot
);
742 static void disable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
745 p
->down
|= (1 << slot
);
748 static int piix4_device_hotplug(PCIDevice
*dev
, int state
)
750 int slot
= PCI_SLOT(dev
->devfn
);
753 pci0_status
.down
= 0;
755 enable_device(&pci0_status
, &gpe
, slot
);
757 disable_device(&pci0_status
, &gpe
, slot
);
759 qemu_set_irq(pm_state
->irq
, 1);
760 qemu_set_irq(pm_state
->irq
, 0);
765 struct acpi_table_header
767 char signature
[4]; /* ACPI signature (4 ASCII characters) */
768 uint32_t length
; /* Length of table, in bytes, including header */
769 uint8_t revision
; /* ACPI Specification minor version # */
770 uint8_t checksum
; /* To make sum of entire table == 0 */
771 char oem_id
[6]; /* OEM identification */
772 char oem_table_id
[8]; /* OEM table identification */
773 uint32_t oem_revision
; /* OEM revision number */
774 char asl_compiler_id
[4]; /* ASL compiler vendor ID */
775 uint32_t asl_compiler_revision
; /* ASL compiler revision number */
776 } __attribute__((packed
));
779 size_t acpi_tables_len
;
781 static int acpi_checksum(const uint8_t *data
, int len
)
785 for(i
= 0; i
< len
; i
++)
787 return (-sum
) & 0xff;
790 int acpi_table_add(const char *t
)
792 static const char *dfl_id
= "QEMUQEMU";
793 char buf
[1024], *p
, *f
;
794 struct acpi_table_header acpi_hdr
;
798 memset(&acpi_hdr
, 0, sizeof(acpi_hdr
));
800 if (get_param_value(buf
, sizeof(buf
), "sig", t
)) {
801 strncpy(acpi_hdr
.signature
, buf
, 4);
803 strncpy(acpi_hdr
.signature
, dfl_id
, 4);
805 if (get_param_value(buf
, sizeof(buf
), "rev", t
)) {
806 val
= strtoul(buf
, &p
, 10);
807 if (val
> 255 || *p
!= '\0')
812 acpi_hdr
.revision
= (int8_t)val
;
814 if (get_param_value(buf
, sizeof(buf
), "oem_id", t
)) {
815 strncpy(acpi_hdr
.oem_id
, buf
, 6);
817 strncpy(acpi_hdr
.oem_id
, dfl_id
, 6);
820 if (get_param_value(buf
, sizeof(buf
), "oem_table_id", t
)) {
821 strncpy(acpi_hdr
.oem_table_id
, buf
, 8);
823 strncpy(acpi_hdr
.oem_table_id
, dfl_id
, 8);
826 if (get_param_value(buf
, sizeof(buf
), "oem_rev", t
)) {
827 val
= strtol(buf
, &p
, 10);
833 acpi_hdr
.oem_revision
= cpu_to_le32(val
);
835 if (get_param_value(buf
, sizeof(buf
), "asl_compiler_id", t
)) {
836 strncpy(acpi_hdr
.asl_compiler_id
, buf
, 4);
838 strncpy(acpi_hdr
.asl_compiler_id
, dfl_id
, 4);
841 if (get_param_value(buf
, sizeof(buf
), "asl_compiler_rev", t
)) {
842 val
= strtol(buf
, &p
, 10);
848 acpi_hdr
.asl_compiler_revision
= cpu_to_le32(val
);
850 if (!get_param_value(buf
, sizeof(buf
), "data", t
)) {
854 acpi_hdr
.length
= sizeof(acpi_hdr
);
859 char *n
= strchr(f
, ':');
862 if(stat(f
, &s
) < 0) {
863 fprintf(stderr
, "Can't stat file '%s': %s\n", f
, strerror(errno
));
866 acpi_hdr
.length
+= s
.st_size
;
874 acpi_tables_len
= sizeof(uint16_t);
875 acpi_tables
= qemu_mallocz(acpi_tables_len
);
877 p
= acpi_tables
+ acpi_tables_len
;
878 acpi_tables_len
+= sizeof(uint16_t) + acpi_hdr
.length
;
879 acpi_tables
= qemu_realloc(acpi_tables
, acpi_tables_len
);
881 acpi_hdr
.length
= cpu_to_le32(acpi_hdr
.length
);
882 *(uint16_t*)p
= acpi_hdr
.length
;
883 p
+= sizeof(uint16_t);
884 memcpy(p
, &acpi_hdr
, sizeof(acpi_hdr
));
885 off
= sizeof(acpi_hdr
);
891 char *n
= strchr(f
, ':');
894 fd
= open(f
, O_RDONLY
);
898 if(fstat(fd
, &s
) < 0) {
905 r
= read(fd
, p
+ off
, s
.st_size
);
909 } else if ((r
< 0 && errno
!= EINTR
) || r
== 0) {
921 ((struct acpi_table_header
*)p
)->checksum
= acpi_checksum((uint8_t*)p
, off
);
922 /* increase number of tables */
923 (*(uint16_t*)acpi_tables
) =
924 cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables
) + 1);
928 qemu_free(acpi_tables
);