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/>
23 #include "qemu-timer.h"
30 /* i82731AB (PIIX4) compatible power management function */
31 #define PM_FREQ 3579545
33 #define ACPI_DBG_IO_ADDR 0xb044
35 typedef struct PIIX4PMState
{
44 int64_t tmr_overflow_time
;
54 #define RSM_STS (1 << 15)
55 #define PWRBTN_STS (1 << 8)
56 #define RTC_EN (1 << 10)
57 #define PWRBTN_EN (1 << 8)
58 #define GBL_EN (1 << 5)
59 #define TMROF_EN (1 << 0)
61 #define SCI_EN (1 << 0)
63 #define SUS_EN (1 << 13)
65 #define ACPI_ENABLE 0xf1
66 #define ACPI_DISABLE 0xf0
68 static PIIX4PMState
*pm_state
;
70 static uint32_t get_pmtmr(PIIX4PMState
*s
)
73 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
, get_ticks_per_sec());
77 static int get_pmsts(PIIX4PMState
*s
)
81 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
, get_ticks_per_sec());
82 if (d
>= s
->tmr_overflow_time
)
87 static void pm_update_sci(PIIX4PMState
*s
)
93 sci_level
= (((pmsts
& s
->pmen
) &
94 (RTC_EN
| PWRBTN_EN
| GBL_EN
| TMROF_EN
)) != 0);
95 qemu_set_irq(s
->irq
, sci_level
);
96 /* schedule a timer interruption if needed */
97 if ((s
->pmen
& TMROF_EN
) && !(pmsts
& TMROF_EN
)) {
98 expire_time
= muldiv64(s
->tmr_overflow_time
, get_ticks_per_sec(), PM_FREQ
);
99 qemu_mod_timer(s
->tmr_timer
, expire_time
);
101 qemu_del_timer(s
->tmr_timer
);
105 static void pm_tmr_timer(void *opaque
)
107 PIIX4PMState
*s
= opaque
;
111 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
113 PIIX4PMState
*s
= opaque
;
120 pmsts
= get_pmsts(s
);
121 if (pmsts
& val
& TMROF_EN
) {
122 /* if TMRSTS is reset, then compute the new overflow time */
123 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
,
124 get_ticks_per_sec());
125 s
->tmr_overflow_time
= (d
+ 0x800000LL
) & ~0x7fffffLL
;
138 s
->pmcntrl
= val
& ~(SUS_EN
);
140 /* change suspend type */
141 sus_typ
= (val
>> 10) & 7;
143 case 0: /* soft power off */
144 qemu_system_shutdown_request();
147 /* RSM_STS should be set on resume. Pretend that resume
148 was caused by power button */
149 s
->pmsts
|= (RSM_STS
| PWRBTN_STS
);
150 qemu_system_reset_request();
152 qemu_irq_raise(s
->cmos_s3
);
164 printf("PM writew port=0x%04x val=0x%04x\n", addr
, val
);
168 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
170 PIIX4PMState
*s
= opaque
;
189 printf("PM readw port=0x%04x val=0x%04x\n", addr
, val
);
194 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
196 // PIIX4PMState *s = opaque;
199 printf("PM writel port=0x%04x val=0x%08x\n", addr
, val
);
203 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
205 PIIX4PMState
*s
= opaque
;
218 printf("PM readl port=0x%04x val=0x%08x\n", addr
, val
);
223 static void apm_ctrl_changed(uint32_t val
, void *arg
)
225 PIIX4PMState
*s
= arg
;
227 /* ACPI specs 3.0, 4.7.2.5 */
228 if (val
== ACPI_ENABLE
) {
229 s
->pmcntrl
|= SCI_EN
;
230 } else if (val
== ACPI_DISABLE
) {
231 s
->pmcntrl
&= ~SCI_EN
;
234 if (s
->dev
.config
[0x5b] & (1 << 1)) {
236 qemu_irq_raise(s
->smi_irq
);
241 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
244 printf("ACPI: DBG: 0x%08x\n", val
);
248 static void pm_io_space_update(PIIX4PMState
*s
)
252 if (s
->dev
.config
[0x80] & 1) {
253 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
254 pm_io_base
&= 0xffc0;
256 /* XXX: need to improve memory and ioport allocation */
258 printf("PM: mapping to 0x%x\n", pm_io_base
);
260 register_ioport_write(pm_io_base
, 64, 2, pm_ioport_writew
, s
);
261 register_ioport_read(pm_io_base
, 64, 2, pm_ioport_readw
, s
);
262 register_ioport_write(pm_io_base
, 64, 4, pm_ioport_writel
, s
);
263 register_ioport_read(pm_io_base
, 64, 4, pm_ioport_readl
, s
);
267 static void pm_write_config(PCIDevice
*d
,
268 uint32_t address
, uint32_t val
, int len
)
270 pci_default_write_config(d
, address
, val
, len
);
271 if (range_covers_byte(address
, len
, 0x80))
272 pm_io_space_update((PIIX4PMState
*)d
);
275 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
277 PIIX4PMState
*s
= opaque
;
279 pm_io_space_update(s
);
283 static const VMStateDescription vmstate_acpi
= {
286 .minimum_version_id
= 1,
287 .minimum_version_id_old
= 1,
288 .post_load
= vmstate_acpi_post_load
,
289 .fields
= (VMStateField
[]) {
290 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
291 VMSTATE_UINT16(pmsts
, PIIX4PMState
),
292 VMSTATE_UINT16(pmen
, PIIX4PMState
),
293 VMSTATE_UINT16(pmcntrl
, PIIX4PMState
),
294 VMSTATE_STRUCT(apm
, PIIX4PMState
, 0, vmstate_apm
, APMState
),
295 VMSTATE_TIMER(tmr_timer
, PIIX4PMState
),
296 VMSTATE_INT64(tmr_overflow_time
, PIIX4PMState
),
297 VMSTATE_END_OF_LIST()
301 static void piix4_reset(void *opaque
)
303 PIIX4PMState
*s
= opaque
;
304 uint8_t *pci_conf
= s
->dev
.config
;
311 if (s
->kvm_enabled
) {
312 /* Mark SMM as already inited (until KVM supports SMM). */
313 pci_conf
[0x5B] = 0x02;
317 static void piix4_powerdown(void *opaque
, int irq
, int power_failing
)
319 PIIX4PMState
*s
= opaque
;
322 qemu_system_shutdown_request();
323 } else if (s
->pmen
& PWRBTN_EN
) {
324 s
->pmsts
|= PWRBTN_EN
;
329 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
330 qemu_irq sci_irq
, qemu_irq cmos_s3
, qemu_irq smi_irq
,
336 s
= (PIIX4PMState
*)pci_register_device(bus
,
337 "PM", sizeof(PIIX4PMState
),
338 devfn
, NULL
, pm_write_config
);
340 pci_conf
= s
->dev
.config
;
341 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
342 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371AB_3
);
343 pci_conf
[0x06] = 0x80;
344 pci_conf
[0x07] = 0x02;
345 pci_conf
[0x08] = 0x03; // revision number
346 pci_conf
[0x09] = 0x00;
347 pci_config_set_class(pci_conf
, PCI_CLASS_BRIDGE_OTHER
);
348 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
349 pci_conf
[0x3d] = 0x01; // interrupt pin 1
351 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
354 apm_init(&s
->apm
, apm_ctrl_changed
, s
);
356 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
358 s
->kvm_enabled
= kvm_enabled
;
359 if (s
->kvm_enabled
) {
360 /* Mark SMM as already inited to prevent SMM from running. KVM does not
361 * support SMM mode. */
362 pci_conf
[0x5B] = 0x02;
365 /* XXX: which specification is used ? The i82731AB has different
367 pci_conf
[0x5f] = (parallel_hds
[0] != NULL
? 0x80 : 0) | 0x10;
368 pci_conf
[0x63] = 0x60;
369 pci_conf
[0x67] = (serial_hds
[0] != NULL
? 0x08 : 0) |
370 (serial_hds
[1] != NULL
? 0x90 : 0);
372 pci_conf
[0x90] = smb_io_base
| 1;
373 pci_conf
[0x91] = smb_io_base
>> 8;
374 pci_conf
[0xd2] = 0x09;
375 register_ioport_write(smb_io_base
, 64, 1, smb_ioport_writeb
, &s
->smb
);
376 register_ioport_read(smb_io_base
, 64, 1, smb_ioport_readb
, &s
->smb
);
378 s
->tmr_timer
= qemu_new_timer(vm_clock
, pm_tmr_timer
, s
);
380 qemu_system_powerdown
= *qemu_allocate_irqs(piix4_powerdown
, s
, 1);
382 vmstate_register(0, &vmstate_acpi
, s
);
384 pm_smbus_init(NULL
, &s
->smb
);
386 s
->cmos_s3
= cmos_s3
;
387 s
->smi_irq
= smi_irq
;
388 qemu_register_reset(piix4_reset
, s
);
393 #define GPE_BASE 0xafe0
394 #define PCI_BASE 0xae00
395 #define PCI_EJ_BASE 0xae08
398 uint16_t sts
; /* status */
399 uint16_t en
; /* enabled */
407 static struct gpe_regs gpe
;
408 static struct pci_status pci0_status
;
410 static uint32_t gpe_read_val(uint16_t val
, uint32_t addr
)
413 return (val
>> 8) & 0xff;
417 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
420 struct gpe_regs
*g
= opaque
;
424 val
= gpe_read_val(g
->sts
, addr
);
428 val
= gpe_read_val(g
->en
, addr
);
435 printf("gpe read %x == %x\n", addr
, val
);
440 static void gpe_write_val(uint16_t *cur
, int addr
, uint32_t val
)
443 *cur
= (*cur
& 0xff) | (val
<< 8);
445 *cur
= (*cur
& 0xff00) | (val
& 0xff);
448 static void gpe_reset_val(uint16_t *cur
, int addr
, uint32_t val
)
450 uint16_t x1
, x0
= val
& 0xff;
451 int shift
= (addr
& 1) ? 8 : 0;
453 x1
= (*cur
>> shift
) & 0xff;
457 *cur
= (*cur
& (0xff << (8 - shift
))) | (x1
<< shift
);
460 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
462 struct gpe_regs
*g
= opaque
;
466 gpe_reset_val(&g
->sts
, addr
, val
);
470 gpe_write_val(&g
->en
, addr
, val
);
477 printf("gpe write %x <== %d\n", addr
, val
);
481 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
484 struct pci_status
*g
= opaque
;
497 printf("pcihotplug read %x == %x\n", addr
, val
);
502 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
504 struct pci_status
*g
= opaque
;
515 printf("pcihotplug write %x <== %d\n", addr
, val
);
519 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
522 printf("pciej read %x\n", addr
);
527 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
529 BusState
*bus
= opaque
;
530 DeviceState
*qdev
, *next
;
532 int slot
= ffs(val
) - 1;
534 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
535 dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
536 if (PCI_SLOT(dev
->devfn
) == slot
) {
543 printf("pciej write %x <== %d\n", addr
, val
);
547 static int piix4_device_hotplug(PCIDevice
*dev
, int state
);
549 void piix4_acpi_system_hot_add_init(PCIBus
*bus
)
551 register_ioport_write(GPE_BASE
, 4, 1, gpe_writeb
, &gpe
);
552 register_ioport_read(GPE_BASE
, 4, 1, gpe_readb
, &gpe
);
554 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, &pci0_status
);
555 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, &pci0_status
);
557 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, bus
);
558 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, bus
);
560 pci_bus_hotplug(bus
, piix4_device_hotplug
);
563 static void enable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
566 p
->up
|= (1 << slot
);
569 static void disable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
572 p
->down
|= (1 << slot
);
575 static int piix4_device_hotplug(PCIDevice
*dev
, int state
)
577 int slot
= PCI_SLOT(dev
->devfn
);
580 pci0_status
.down
= 0;
582 enable_device(&pci0_status
, &gpe
, slot
);
584 disable_device(&pci0_status
, &gpe
, slot
);
586 qemu_set_irq(pm_state
->irq
, 1);
587 qemu_set_irq(pm_state
->irq
, 0);
592 struct acpi_table_header
594 char signature
[4]; /* ACPI signature (4 ASCII characters) */
595 uint32_t length
; /* Length of table, in bytes, including header */
596 uint8_t revision
; /* ACPI Specification minor version # */
597 uint8_t checksum
; /* To make sum of entire table == 0 */
598 char oem_id
[6]; /* OEM identification */
599 char oem_table_id
[8]; /* OEM table identification */
600 uint32_t oem_revision
; /* OEM revision number */
601 char asl_compiler_id
[4]; /* ASL compiler vendor ID */
602 uint32_t asl_compiler_revision
; /* ASL compiler revision number */
603 } __attribute__((packed
));
606 size_t acpi_tables_len
;
608 static int acpi_checksum(const uint8_t *data
, int len
)
612 for(i
= 0; i
< len
; i
++)
614 return (-sum
) & 0xff;
617 int acpi_table_add(const char *t
)
619 static const char *dfl_id
= "QEMUQEMU";
620 char buf
[1024], *p
, *f
;
621 struct acpi_table_header acpi_hdr
;
625 memset(&acpi_hdr
, 0, sizeof(acpi_hdr
));
627 if (get_param_value(buf
, sizeof(buf
), "sig", t
)) {
628 strncpy(acpi_hdr
.signature
, buf
, 4);
630 strncpy(acpi_hdr
.signature
, dfl_id
, 4);
632 if (get_param_value(buf
, sizeof(buf
), "rev", t
)) {
633 val
= strtoul(buf
, &p
, 10);
634 if (val
> 255 || *p
!= '\0')
639 acpi_hdr
.revision
= (int8_t)val
;
641 if (get_param_value(buf
, sizeof(buf
), "oem_id", t
)) {
642 strncpy(acpi_hdr
.oem_id
, buf
, 6);
644 strncpy(acpi_hdr
.oem_id
, dfl_id
, 6);
647 if (get_param_value(buf
, sizeof(buf
), "oem_table_id", t
)) {
648 strncpy(acpi_hdr
.oem_table_id
, buf
, 8);
650 strncpy(acpi_hdr
.oem_table_id
, dfl_id
, 8);
653 if (get_param_value(buf
, sizeof(buf
), "oem_rev", t
)) {
654 val
= strtol(buf
, &p
, 10);
660 acpi_hdr
.oem_revision
= cpu_to_le32(val
);
662 if (get_param_value(buf
, sizeof(buf
), "asl_compiler_id", t
)) {
663 strncpy(acpi_hdr
.asl_compiler_id
, buf
, 4);
665 strncpy(acpi_hdr
.asl_compiler_id
, dfl_id
, 4);
668 if (get_param_value(buf
, sizeof(buf
), "asl_compiler_rev", t
)) {
669 val
= strtol(buf
, &p
, 10);
675 acpi_hdr
.asl_compiler_revision
= cpu_to_le32(val
);
677 if (!get_param_value(buf
, sizeof(buf
), "data", t
)) {
681 acpi_hdr
.length
= sizeof(acpi_hdr
);
686 char *n
= strchr(f
, ':');
689 if(stat(f
, &s
) < 0) {
690 fprintf(stderr
, "Can't stat file '%s': %s\n", f
, strerror(errno
));
693 acpi_hdr
.length
+= s
.st_size
;
701 acpi_tables_len
= sizeof(uint16_t);
702 acpi_tables
= qemu_mallocz(acpi_tables_len
);
704 p
= acpi_tables
+ acpi_tables_len
;
705 acpi_tables_len
+= sizeof(uint16_t) + acpi_hdr
.length
;
706 acpi_tables
= qemu_realloc(acpi_tables
, acpi_tables_len
);
708 acpi_hdr
.length
= cpu_to_le32(acpi_hdr
.length
);
709 *(uint16_t*)p
= acpi_hdr
.length
;
710 p
+= sizeof(uint16_t);
711 memcpy(p
, &acpi_hdr
, sizeof(acpi_hdr
));
712 off
= sizeof(acpi_hdr
);
718 char *n
= strchr(f
, ':');
721 fd
= open(f
, O_RDONLY
);
725 if(fstat(fd
, &s
) < 0) {
732 r
= read(fd
, p
+ off
, s
.st_size
);
736 } else if ((r
< 0 && errno
!= EINTR
) || r
== 0) {
748 ((struct acpi_table_header
*)p
)->checksum
= acpi_checksum((uint8_t*)p
, off
);
749 /* increase number of tables */
750 (*(uint16_t*)acpi_tables
) =
751 cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables
) + 1);
755 qemu_free(acpi_tables
);