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"
33 /* i82731AB (PIIX4) compatible power management function */
34 #define PM_FREQ 3579545
36 #define ACPI_DBG_IO_ADDR 0xb044
38 typedef struct PIIX4PMState
{
47 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 static PIIX4PMState
*pm_state
;
73 static uint32_t get_pmtmr(PIIX4PMState
*s
)
76 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
, get_ticks_per_sec());
80 static int get_pmsts(PIIX4PMState
*s
)
84 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
, get_ticks_per_sec());
85 if (d
>= s
->tmr_overflow_time
)
90 static void pm_update_sci(PIIX4PMState
*s
)
96 sci_level
= (((pmsts
& s
->pmen
) &
97 (RTC_EN
| PWRBTN_EN
| GBL_EN
| TMROF_EN
)) != 0);
98 qemu_set_irq(s
->irq
, sci_level
);
99 /* schedule a timer interruption if needed */
100 if ((s
->pmen
& TMROF_EN
) && !(pmsts
& TMROF_EN
)) {
101 expire_time
= muldiv64(s
->tmr_overflow_time
, get_ticks_per_sec(), PM_FREQ
);
102 qemu_mod_timer(s
->tmr_timer
, expire_time
);
104 qemu_del_timer(s
->tmr_timer
);
108 static void pm_tmr_timer(void *opaque
)
110 PIIX4PMState
*s
= opaque
;
114 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
116 PIIX4PMState
*s
= opaque
;
123 pmsts
= get_pmsts(s
);
124 if (pmsts
& val
& TMROF_EN
) {
125 /* if TMRSTS is reset, then compute the new overflow time */
126 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
,
127 get_ticks_per_sec());
128 s
->tmr_overflow_time
= (d
+ 0x800000LL
) & ~0x7fffffLL
;
141 s
->pmcntrl
= val
& ~(SUS_EN
);
143 /* change suspend type */
144 sus_typ
= (val
>> 10) & 7;
146 case 0: /* soft power off */
147 qemu_system_shutdown_request();
150 /* RSM_STS should be set on resume. Pretend that resume
151 was caused by power button */
152 s
->pmsts
|= (RSM_STS
| PWRBTN_STS
);
153 qemu_system_reset_request();
155 qemu_irq_raise(s
->cmos_s3
);
167 printf("PM writew port=0x%04x val=0x%04x\n", addr
, val
);
171 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
173 PIIX4PMState
*s
= opaque
;
192 printf("PM readw port=0x%04x val=0x%04x\n", addr
, val
);
197 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
199 // PIIX4PMState *s = opaque;
202 printf("PM writel port=0x%04x val=0x%08x\n", addr
, val
);
206 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
208 PIIX4PMState
*s
= opaque
;
221 printf("PM readl port=0x%04x val=0x%08x\n", addr
, val
);
226 static void apm_ctrl_changed(uint32_t val
, void *arg
)
228 PIIX4PMState
*s
= arg
;
230 /* ACPI specs 3.0, 4.7.2.5 */
231 if (val
== ACPI_ENABLE
) {
232 s
->pmcntrl
|= SCI_EN
;
233 } else if (val
== ACPI_DISABLE
) {
234 s
->pmcntrl
&= ~SCI_EN
;
237 if (s
->dev
.config
[0x5b] & (1 << 1)) {
239 qemu_irq_raise(s
->smi_irq
);
244 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
247 printf("ACPI: DBG: 0x%08x\n", val
);
251 static void pm_io_space_update(PIIX4PMState
*s
)
255 if (s
->dev
.config
[0x80] & 1) {
256 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
257 pm_io_base
&= 0xffc0;
259 /* XXX: need to improve memory and ioport allocation */
261 printf("PM: mapping to 0x%x\n", pm_io_base
);
263 register_ioport_write(pm_io_base
, 64, 2, pm_ioport_writew
, s
);
264 register_ioport_read(pm_io_base
, 64, 2, pm_ioport_readw
, s
);
265 register_ioport_write(pm_io_base
, 64, 4, pm_ioport_writel
, s
);
266 register_ioport_read(pm_io_base
, 64, 4, pm_ioport_readl
, s
);
270 static void pm_write_config(PCIDevice
*d
,
271 uint32_t address
, uint32_t val
, int len
)
273 pci_default_write_config(d
, address
, val
, len
);
274 if (range_covers_byte(address
, len
, 0x80))
275 pm_io_space_update((PIIX4PMState
*)d
);
278 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
280 PIIX4PMState
*s
= opaque
;
282 pm_io_space_update(s
);
286 static const VMStateDescription vmstate_acpi
= {
289 .minimum_version_id
= 1,
290 .minimum_version_id_old
= 1,
291 .post_load
= vmstate_acpi_post_load
,
292 .fields
= (VMStateField
[]) {
293 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
294 VMSTATE_UINT16(pmsts
, PIIX4PMState
),
295 VMSTATE_UINT16(pmen
, PIIX4PMState
),
296 VMSTATE_UINT16(pmcntrl
, PIIX4PMState
),
297 VMSTATE_STRUCT(apm
, PIIX4PMState
, 0, vmstate_apm
, APMState
),
298 VMSTATE_TIMER(tmr_timer
, PIIX4PMState
),
299 VMSTATE_INT64(tmr_overflow_time
, PIIX4PMState
),
300 VMSTATE_END_OF_LIST()
304 static void piix4_reset(void *opaque
)
306 PIIX4PMState
*s
= opaque
;
307 uint8_t *pci_conf
= s
->dev
.config
;
314 if (s
->kvm_enabled
) {
315 /* Mark SMM as already inited (until KVM supports SMM). */
316 pci_conf
[0x5B] = 0x02;
320 static void piix4_powerdown(void *opaque
, int irq
, int power_failing
)
322 PIIX4PMState
*s
= opaque
;
325 qemu_system_shutdown_request();
326 } else if (s
->pmen
& PWRBTN_EN
) {
327 s
->pmsts
|= PWRBTN_EN
;
332 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
333 qemu_irq sci_irq
, qemu_irq cmos_s3
, qemu_irq smi_irq
,
339 s
= (PIIX4PMState
*)pci_register_device(bus
,
340 "PM", sizeof(PIIX4PMState
),
341 devfn
, NULL
, pm_write_config
);
343 pci_conf
= s
->dev
.config
;
344 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
345 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371AB_3
);
346 pci_conf
[0x06] = 0x80;
347 pci_conf
[0x07] = 0x02;
348 pci_conf
[0x08] = 0x03; // revision number
349 pci_conf
[0x09] = 0x00;
350 pci_config_set_class(pci_conf
, PCI_CLASS_BRIDGE_OTHER
);
351 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
352 pci_conf
[0x3d] = 0x01; // interrupt pin 1
354 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
356 #if defined(TARGET_IA64)
357 pci_conf
[0x40] = 0x41; /* PM io base read only bit */
358 pci_conf
[0x41] = 0x1f;
359 pm_write_config(s
, 0x80, 0x01, 1); /*Set default pm_io_base 0x1f40*/
364 apm_init(&s
->apm
, apm_ctrl_changed
, s
);
366 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
368 s
->kvm_enabled
= kvm_enabled
;
369 if (s
->kvm_enabled
) {
370 /* Mark SMM as already inited to prevent SMM from running. KVM does not
371 * support SMM mode. */
372 pci_conf
[0x5B] = 0x02;
375 /* XXX: which specification is used ? The i82731AB has different
377 pci_conf
[0x5f] = (parallel_hds
[0] != NULL
? 0x80 : 0) | 0x10;
378 pci_conf
[0x63] = 0x60;
379 pci_conf
[0x67] = (serial_hds
[0] != NULL
? 0x08 : 0) |
380 (serial_hds
[1] != NULL
? 0x90 : 0);
382 pci_conf
[0x90] = smb_io_base
| 1;
383 pci_conf
[0x91] = smb_io_base
>> 8;
384 pci_conf
[0xd2] = 0x09;
385 register_ioport_write(smb_io_base
, 64, 1, smb_ioport_writeb
, &s
->smb
);
386 register_ioport_read(smb_io_base
, 64, 1, smb_ioport_readb
, &s
->smb
);
388 s
->tmr_timer
= qemu_new_timer(vm_clock
, pm_tmr_timer
, s
);
390 qemu_system_powerdown
= *qemu_allocate_irqs(piix4_powerdown
, s
, 1);
392 vmstate_register(0, &vmstate_acpi
, s
);
394 pm_smbus_init(NULL
, &s
->smb
);
396 s
->cmos_s3
= cmos_s3
;
397 s
->smi_irq
= smi_irq
;
398 qemu_register_reset(piix4_reset
, s
);
403 #define GPE_BASE 0xafe0
404 #define PROC_BASE 0xaf00
405 #define PCI_BASE 0xae00
406 #define PCI_EJ_BASE 0xae08
409 uint16_t sts
; /* status */
410 uint16_t en
; /* enabled */
411 uint8_t cpus_sts
[32];
419 static struct gpe_regs gpe
;
420 static struct pci_status pci0_status
;
422 static uint32_t gpe_read_val(uint16_t val
, uint32_t addr
)
425 return (val
>> 8) & 0xff;
429 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
432 struct gpe_regs
*g
= opaque
;
434 case PROC_BASE
... PROC_BASE
+31:
435 val
= g
->cpus_sts
[addr
- PROC_BASE
];
440 val
= gpe_read_val(g
->sts
, addr
);
444 val
= gpe_read_val(g
->en
, addr
);
451 printf("gpe read %x == %x\n", addr
, val
);
456 static void gpe_write_val(uint16_t *cur
, int addr
, uint32_t val
)
459 *cur
= (*cur
& 0xff) | (val
<< 8);
461 *cur
= (*cur
& 0xff00) | (val
& 0xff);
464 static void gpe_reset_val(uint16_t *cur
, int addr
, uint32_t val
)
466 uint16_t x1
, x0
= val
& 0xff;
467 int shift
= (addr
& 1) ? 8 : 0;
469 x1
= (*cur
>> shift
) & 0xff;
473 *cur
= (*cur
& (0xff << (8 - shift
))) | (x1
<< shift
);
476 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
478 struct gpe_regs
*g
= opaque
;
480 case PROC_BASE
... PROC_BASE
+ 31:
481 /* don't allow to change cpus_sts from inside a guest */
486 gpe_reset_val(&g
->sts
, addr
, val
);
490 gpe_write_val(&g
->en
, addr
, val
);
497 printf("gpe write %x <== %d\n", addr
, val
);
501 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
504 struct pci_status
*g
= opaque
;
517 printf("pcihotplug read %x == %x\n", addr
, val
);
522 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
524 struct pci_status
*g
= opaque
;
535 printf("pcihotplug write %x <== %d\n", addr
, val
);
539 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
542 printf("pciej read %x\n", addr
);
547 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
549 BusState
*bus
= opaque
;
550 DeviceState
*qdev
, *next
;
552 int slot
= ffs(val
) - 1;
554 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
555 dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
556 if (PCI_SLOT(dev
->devfn
) == slot
) {
563 printf("pciej write %x <== %d\n", addr
, val
);
567 static const char *model
;
569 static int piix4_device_hotplug(PCIDevice
*dev
, int state
);
571 void piix4_acpi_system_hot_add_init(PCIBus
*bus
, const char *cpu_model
)
573 int i
= 0, cpus
= smp_cpus
;
576 gpe
.cpus_sts
[i
++] = (cpus
< 8) ? (1 << cpus
) - 1 : 0xff;
579 register_ioport_write(GPE_BASE
, 4, 1, gpe_writeb
, &gpe
);
580 register_ioport_read(GPE_BASE
, 4, 1, gpe_readb
, &gpe
);
582 register_ioport_write(PROC_BASE
, 32, 1, gpe_writeb
, &gpe
);
583 register_ioport_read(PROC_BASE
, 32, 1, gpe_readb
, &gpe
);
585 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, &pci0_status
);
586 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, &pci0_status
);
588 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, bus
);
589 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, bus
);
593 pci_bus_hotplug(bus
, piix4_device_hotplug
);
596 #if defined(TARGET_I386)
597 static void enable_processor(struct gpe_regs
*g
, int cpu
)
600 g
->cpus_sts
[cpu
/8] |= (1 << (cpu
%8));
603 static void disable_processor(struct gpe_regs
*g
, int cpu
)
606 g
->cpus_sts
[cpu
/8] &= ~(1 << (cpu
%8));
609 void qemu_system_cpu_hot_add(int cpu
, int state
)
613 if (state
&& !qemu_get_cpu(cpu
)) {
614 env
= pc_new_cpu(model
);
616 fprintf(stderr
, "cpu %d creation failed\n", cpu
);
619 env
->cpuid_apic_id
= cpu
;
623 enable_processor(&gpe
, cpu
);
625 disable_processor(&gpe
, cpu
);
627 qemu_set_irq(pm_state
->irq
, 1);
628 qemu_set_irq(pm_state
->irq
, 0);
633 static void enable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
636 p
->up
|= (1 << slot
);
639 static void disable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
642 p
->down
|= (1 << slot
);
645 static int piix4_device_hotplug(PCIDevice
*dev
, int state
)
647 int slot
= PCI_SLOT(dev
->devfn
);
650 pci0_status
.down
= 0;
652 enable_device(&pci0_status
, &gpe
, slot
);
654 disable_device(&pci0_status
, &gpe
, slot
);
656 qemu_set_irq(pm_state
->irq
, 1);
657 qemu_set_irq(pm_state
->irq
, 0);
662 struct acpi_table_header
664 char signature
[4]; /* ACPI signature (4 ASCII characters) */
665 uint32_t length
; /* Length of table, in bytes, including header */
666 uint8_t revision
; /* ACPI Specification minor version # */
667 uint8_t checksum
; /* To make sum of entire table == 0 */
668 char oem_id
[6]; /* OEM identification */
669 char oem_table_id
[8]; /* OEM table identification */
670 uint32_t oem_revision
; /* OEM revision number */
671 char asl_compiler_id
[4]; /* ASL compiler vendor ID */
672 uint32_t asl_compiler_revision
; /* ASL compiler revision number */
673 } __attribute__((packed
));
676 size_t acpi_tables_len
;
678 static int acpi_checksum(const uint8_t *data
, int len
)
682 for(i
= 0; i
< len
; i
++)
684 return (-sum
) & 0xff;
687 int acpi_table_add(const char *t
)
689 static const char *dfl_id
= "QEMUQEMU";
690 char buf
[1024], *p
, *f
;
691 struct acpi_table_header acpi_hdr
;
695 memset(&acpi_hdr
, 0, sizeof(acpi_hdr
));
697 if (get_param_value(buf
, sizeof(buf
), "sig", t
)) {
698 strncpy(acpi_hdr
.signature
, buf
, 4);
700 strncpy(acpi_hdr
.signature
, dfl_id
, 4);
702 if (get_param_value(buf
, sizeof(buf
), "rev", t
)) {
703 val
= strtoul(buf
, &p
, 10);
704 if (val
> 255 || *p
!= '\0')
709 acpi_hdr
.revision
= (int8_t)val
;
711 if (get_param_value(buf
, sizeof(buf
), "oem_id", t
)) {
712 strncpy(acpi_hdr
.oem_id
, buf
, 6);
714 strncpy(acpi_hdr
.oem_id
, dfl_id
, 6);
717 if (get_param_value(buf
, sizeof(buf
), "oem_table_id", t
)) {
718 strncpy(acpi_hdr
.oem_table_id
, buf
, 8);
720 strncpy(acpi_hdr
.oem_table_id
, dfl_id
, 8);
723 if (get_param_value(buf
, sizeof(buf
), "oem_rev", t
)) {
724 val
= strtol(buf
, &p
, 10);
730 acpi_hdr
.oem_revision
= cpu_to_le32(val
);
732 if (get_param_value(buf
, sizeof(buf
), "asl_compiler_id", t
)) {
733 strncpy(acpi_hdr
.asl_compiler_id
, buf
, 4);
735 strncpy(acpi_hdr
.asl_compiler_id
, dfl_id
, 4);
738 if (get_param_value(buf
, sizeof(buf
), "asl_compiler_rev", t
)) {
739 val
= strtol(buf
, &p
, 10);
745 acpi_hdr
.asl_compiler_revision
= cpu_to_le32(val
);
747 if (!get_param_value(buf
, sizeof(buf
), "data", t
)) {
751 acpi_hdr
.length
= sizeof(acpi_hdr
);
756 char *n
= strchr(f
, ':');
759 if(stat(f
, &s
) < 0) {
760 fprintf(stderr
, "Can't stat file '%s': %s\n", f
, strerror(errno
));
763 acpi_hdr
.length
+= s
.st_size
;
771 acpi_tables_len
= sizeof(uint16_t);
772 acpi_tables
= qemu_mallocz(acpi_tables_len
);
774 p
= acpi_tables
+ acpi_tables_len
;
775 acpi_tables_len
+= sizeof(uint16_t) + acpi_hdr
.length
;
776 acpi_tables
= qemu_realloc(acpi_tables
, acpi_tables_len
);
778 acpi_hdr
.length
= cpu_to_le32(acpi_hdr
.length
);
779 *(uint16_t*)p
= acpi_hdr
.length
;
780 p
+= sizeof(uint16_t);
781 memcpy(p
, &acpi_hdr
, sizeof(acpi_hdr
));
782 off
= sizeof(acpi_hdr
);
788 char *n
= strchr(f
, ':');
791 fd
= open(f
, O_RDONLY
);
795 if(fstat(fd
, &s
) < 0) {
802 r
= read(fd
, p
+ off
, s
.st_size
);
806 } else if ((r
< 0 && errno
!= EINTR
) || r
== 0) {
818 ((struct acpi_table_header
*)p
)->checksum
= acpi_checksum((uint8_t*)p
, off
);
819 /* increase number of tables */
820 (*(uint16_t*)acpi_tables
) =
821 cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables
) + 1);
825 qemu_free(acpi_tables
);