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/>
30 # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
32 # define PIIX4_DPRINTF(format, ...) do { } while (0)
35 #define ACPI_DBG_IO_ADDR 0xb044
37 #define GPE_BASE 0xafe0
38 #define PCI_BASE 0xae00
39 #define PCI_EJ_BASE 0xae08
42 uint16_t sts
; /* status */
43 uint16_t en
; /* enabled */
51 typedef struct PIIX4PMState
{
60 int64_t tmr_overflow_time
;
72 struct pci_status pci0_status
;
75 static void piix4_acpi_system_hot_add_init(PCIBus
*bus
, PIIX4PMState
*s
);
77 #define ACPI_ENABLE 0xf1
78 #define ACPI_DISABLE 0xf0
80 static uint32_t get_pmtmr(PIIX4PMState
*s
)
83 d
= muldiv64(qemu_get_clock(vm_clock
), PM_TIMER_FREQUENCY
, get_ticks_per_sec());
87 static int get_pmsts(PIIX4PMState
*s
)
91 d
= muldiv64(qemu_get_clock(vm_clock
), PM_TIMER_FREQUENCY
,
93 if (d
>= s
->tmr_overflow_time
)
94 s
->pmsts
|= ACPI_BITMASK_TIMER_STATUS
;
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 (ACPI_BITMASK_RT_CLOCK_ENABLE
|
106 ACPI_BITMASK_POWER_BUTTON_ENABLE
|
107 ACPI_BITMASK_GLOBAL_LOCK_ENABLE
|
108 ACPI_BITMASK_TIMER_ENABLE
)) != 0);
109 qemu_set_irq(s
->irq
, sci_level
);
110 /* schedule a timer interruption if needed */
111 if ((s
->pmen
& ACPI_BITMASK_TIMER_ENABLE
) &&
112 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
)) {
113 expire_time
= muldiv64(s
->tmr_overflow_time
, get_ticks_per_sec(),
115 qemu_mod_timer(s
->tmr_timer
, expire_time
);
117 qemu_del_timer(s
->tmr_timer
);
121 static void pm_tmr_timer(void *opaque
)
123 PIIX4PMState
*s
= opaque
;
127 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
129 PIIX4PMState
*s
= opaque
;
136 pmsts
= get_pmsts(s
);
137 if (pmsts
& val
& ACPI_BITMASK_TIMER_STATUS
) {
138 /* if TMRSTS is reset, then compute the new overflow time */
139 d
= muldiv64(qemu_get_clock(vm_clock
), PM_TIMER_FREQUENCY
,
140 get_ticks_per_sec());
141 s
->tmr_overflow_time
= (d
+ 0x800000LL
) & ~0x7fffffLL
;
154 s
->pmcntrl
= val
& ~(ACPI_BITMASK_SLEEP_ENABLE
);
155 if (val
& ACPI_BITMASK_SLEEP_ENABLE
) {
156 /* change suspend type */
157 sus_typ
= (val
>> 10) & 7;
159 case 0: /* soft power off */
160 qemu_system_shutdown_request();
163 /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
164 Pretend that resume was caused by power button */
165 s
->pmsts
|= (ACPI_BITMASK_WAKE_STATUS
|
166 ACPI_BITMASK_POWER_BUTTON_STATUS
);
167 qemu_system_reset_request();
169 qemu_irq_raise(s
->cmos_s3
);
180 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", addr
, val
);
183 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
185 PIIX4PMState
*s
= opaque
;
203 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", addr
, val
);
207 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
209 // PIIX4PMState *s = opaque;
210 PIIX4_DPRINTF("PM writel port=0x%04x val=0x%08x\n", addr
& 0x3f, val
);
213 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
215 PIIX4PMState
*s
= opaque
;
227 PIIX4_DPRINTF("PM readl port=0x%04x val=0x%08x\n", addr
, val
);
231 static void apm_ctrl_changed(uint32_t val
, void *arg
)
233 PIIX4PMState
*s
= arg
;
235 /* ACPI specs 3.0, 4.7.2.5 */
236 if (val
== ACPI_ENABLE
) {
237 s
->pmcntrl
|= ACPI_BITMASK_SCI_ENABLE
;
238 } else if (val
== ACPI_DISABLE
) {
239 s
->pmcntrl
&= ~ACPI_BITMASK_SCI_ENABLE
;
242 if (s
->dev
.config
[0x5b] & (1 << 1)) {
244 qemu_irq_raise(s
->smi_irq
);
249 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
251 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val
);
254 static void pm_io_space_update(PIIX4PMState
*s
)
258 if (s
->dev
.config
[0x80] & 1) {
259 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
260 pm_io_base
&= 0xffc0;
262 /* XXX: need to improve memory and ioport allocation */
263 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base
);
264 register_ioport_write(pm_io_base
, 64, 2, pm_ioport_writew
, s
);
265 register_ioport_read(pm_io_base
, 64, 2, pm_ioport_readw
, s
);
266 register_ioport_write(pm_io_base
, 64, 4, pm_ioport_writel
, s
);
267 register_ioport_read(pm_io_base
, 64, 4, pm_ioport_readl
, s
);
271 static void pm_write_config(PCIDevice
*d
,
272 uint32_t address
, uint32_t val
, int len
)
274 pci_default_write_config(d
, address
, val
, len
);
275 if (range_covers_byte(address
, len
, 0x80))
276 pm_io_space_update((PIIX4PMState
*)d
);
279 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
281 PIIX4PMState
*s
= opaque
;
283 pm_io_space_update(s
);
287 static const VMStateDescription vmstate_gpe
= {
290 .minimum_version_id
= 1,
291 .minimum_version_id_old
= 1,
292 .fields
= (VMStateField
[]) {
293 VMSTATE_UINT16(sts
, struct gpe_regs
),
294 VMSTATE_UINT16(en
, struct gpe_regs
),
295 VMSTATE_END_OF_LIST()
299 static const VMStateDescription vmstate_pci_status
= {
300 .name
= "pci_status",
302 .minimum_version_id
= 1,
303 .minimum_version_id_old
= 1,
304 .fields
= (VMStateField
[]) {
305 VMSTATE_UINT32(up
, struct pci_status
),
306 VMSTATE_UINT32(down
, struct pci_status
),
307 VMSTATE_END_OF_LIST()
311 static const VMStateDescription vmstate_acpi
= {
314 .minimum_version_id
= 1,
315 .minimum_version_id_old
= 1,
316 .post_load
= vmstate_acpi_post_load
,
317 .fields
= (VMStateField
[]) {
318 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
319 VMSTATE_UINT16(pmsts
, PIIX4PMState
),
320 VMSTATE_UINT16(pmen
, PIIX4PMState
),
321 VMSTATE_UINT16(pmcntrl
, PIIX4PMState
),
322 VMSTATE_STRUCT(apm
, PIIX4PMState
, 0, vmstate_apm
, APMState
),
323 VMSTATE_TIMER(tmr_timer
, PIIX4PMState
),
324 VMSTATE_INT64(tmr_overflow_time
, PIIX4PMState
),
325 VMSTATE_STRUCT(gpe
, PIIX4PMState
, 2, vmstate_gpe
, struct gpe_regs
),
326 VMSTATE_STRUCT(pci0_status
, PIIX4PMState
, 2, vmstate_pci_status
,
328 VMSTATE_END_OF_LIST()
332 static void piix4_reset(void *opaque
)
334 PIIX4PMState
*s
= opaque
;
335 uint8_t *pci_conf
= s
->dev
.config
;
342 if (s
->kvm_enabled
) {
343 /* Mark SMM as already inited (until KVM supports SMM). */
344 pci_conf
[0x5B] = 0x02;
348 static void piix4_powerdown(void *opaque
, int irq
, int power_failing
)
350 PIIX4PMState
*s
= opaque
;
353 qemu_system_shutdown_request();
354 } else if (s
->pmen
& ACPI_BITMASK_POWER_BUTTON_ENABLE
) {
355 s
->pmsts
|= ACPI_BITMASK_POWER_BUTTON_STATUS
;
360 static int piix4_pm_initfn(PCIDevice
*dev
)
362 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
365 pci_conf
= s
->dev
.config
;
366 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
367 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371AB_3
);
368 pci_conf
[0x06] = 0x80;
369 pci_conf
[0x07] = 0x02;
370 pci_conf
[0x08] = 0x03; // revision number
371 pci_conf
[0x09] = 0x00;
372 pci_config_set_class(pci_conf
, PCI_CLASS_BRIDGE_OTHER
);
373 pci_conf
[0x3d] = 0x01; // interrupt pin 1
375 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
378 apm_init(&s
->apm
, apm_ctrl_changed
, s
);
380 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
382 if (s
->kvm_enabled
) {
383 /* Mark SMM as already inited to prevent SMM from running. KVM does not
384 * support SMM mode. */
385 pci_conf
[0x5B] = 0x02;
388 /* XXX: which specification is used ? The i82731AB has different
390 pci_conf
[0x5f] = (parallel_hds
[0] != NULL
? 0x80 : 0) | 0x10;
391 pci_conf
[0x63] = 0x60;
392 pci_conf
[0x67] = (serial_hds
[0] != NULL
? 0x08 : 0) |
393 (serial_hds
[1] != NULL
? 0x90 : 0);
395 pci_conf
[0x90] = s
->smb_io_base
| 1;
396 pci_conf
[0x91] = s
->smb_io_base
>> 8;
397 pci_conf
[0xd2] = 0x09;
398 register_ioport_write(s
->smb_io_base
, 64, 1, smb_ioport_writeb
, &s
->smb
);
399 register_ioport_read(s
->smb_io_base
, 64, 1, smb_ioport_readb
, &s
->smb
);
401 s
->tmr_timer
= qemu_new_timer(vm_clock
, pm_tmr_timer
, s
);
403 qemu_system_powerdown
= *qemu_allocate_irqs(piix4_powerdown
, s
, 1);
405 pm_smbus_init(&s
->dev
.qdev
, &s
->smb
);
406 qemu_register_reset(piix4_reset
, s
);
407 piix4_acpi_system_hot_add_init(dev
->bus
, s
);
412 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
413 qemu_irq sci_irq
, qemu_irq cmos_s3
, qemu_irq smi_irq
,
419 dev
= pci_create(bus
, devfn
, "PIIX4_PM");
420 qdev_prop_set_uint32(&dev
->qdev
, "smb_io_base", smb_io_base
);
422 s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
424 s
->cmos_s3
= cmos_s3
;
425 s
->smi_irq
= smi_irq
;
426 s
->kvm_enabled
= kvm_enabled
;
428 qdev_init_nofail(&dev
->qdev
);
433 static PCIDeviceInfo piix4_pm_info
= {
434 .qdev
.name
= "PIIX4_PM",
436 .qdev
.size
= sizeof(PIIX4PMState
),
437 .qdev
.vmsd
= &vmstate_acpi
,
438 .init
= piix4_pm_initfn
,
439 .config_write
= pm_write_config
,
440 .qdev
.props
= (Property
[]) {
441 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState
, smb_io_base
, 0),
442 DEFINE_PROP_END_OF_LIST(),
446 static void piix4_pm_register(void)
448 pci_qdev_register(&piix4_pm_info
);
451 device_init(piix4_pm_register
);
453 static uint32_t gpe_read_val(uint16_t val
, uint32_t addr
)
456 return (val
>> 8) & 0xff;
460 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
463 struct gpe_regs
*g
= opaque
;
467 val
= gpe_read_val(g
->sts
, addr
);
471 val
= gpe_read_val(g
->en
, addr
);
477 PIIX4_DPRINTF("gpe read %x == %x\n", addr
, val
);
481 static void gpe_write_val(uint16_t *cur
, int addr
, uint32_t val
)
484 *cur
= (*cur
& 0xff) | (val
<< 8);
486 *cur
= (*cur
& 0xff00) | (val
& 0xff);
489 static void gpe_reset_val(uint16_t *cur
, int addr
, uint32_t val
)
491 uint16_t x1
, x0
= val
& 0xff;
492 int shift
= (addr
& 1) ? 8 : 0;
494 x1
= (*cur
>> shift
) & 0xff;
498 *cur
= (*cur
& (0xff << (8 - shift
))) | (x1
<< shift
);
501 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
503 struct gpe_regs
*g
= opaque
;
507 gpe_reset_val(&g
->sts
, addr
, val
);
511 gpe_write_val(&g
->en
, addr
, val
);
517 PIIX4_DPRINTF("gpe write %x <== %d\n", addr
, val
);
520 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
523 struct pci_status
*g
= opaque
;
535 PIIX4_DPRINTF("pcihotplug read %x == %x\n", addr
, val
);
539 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
541 struct pci_status
*g
= opaque
;
551 PIIX4_DPRINTF("pcihotplug write %x <== %d\n", addr
, val
);
554 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
556 PIIX4_DPRINTF("pciej read %x\n", addr
);
560 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
562 BusState
*bus
= opaque
;
563 DeviceState
*qdev
, *next
;
565 int slot
= ffs(val
) - 1;
567 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
568 dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
569 if (PCI_SLOT(dev
->devfn
) == slot
) {
575 PIIX4_DPRINTF("pciej write %x <== %d\n", addr
, val
);
578 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
, int state
);
580 static void piix4_acpi_system_hot_add_init(PCIBus
*bus
, PIIX4PMState
*s
)
582 struct gpe_regs
*gpe
= &s
->gpe
;
583 struct pci_status
*pci0_status
= &s
->pci0_status
;
585 register_ioport_write(GPE_BASE
, 4, 1, gpe_writeb
, gpe
);
586 register_ioport_read(GPE_BASE
, 4, 1, gpe_readb
, gpe
);
588 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, pci0_status
);
589 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, pci0_status
);
591 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, bus
);
592 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, bus
);
594 pci_bus_hotplug(bus
, piix4_device_hotplug
, &s
->dev
.qdev
);
597 static void enable_device(PIIX4PMState
*s
, int slot
)
600 s
->pci0_status
.up
|= (1 << slot
);
603 static void disable_device(PIIX4PMState
*s
, int slot
)
606 s
->pci0_status
.down
|= (1 << slot
);
609 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
, int state
)
611 int slot
= PCI_SLOT(dev
->devfn
);
612 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
,
613 DO_UPCAST(PCIDevice
, qdev
, qdev
));
615 s
->pci0_status
.up
= 0;
616 s
->pci0_status
.down
= 0;
618 enable_device(s
, slot
);
620 disable_device(s
, slot
);
623 qemu_set_irq(s
->irq
, 1);
624 qemu_set_irq(s
->irq
, 0);