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 ACPI_DBG_IO_ADDR 0xb044
32 typedef struct PIIX4PMState
{
41 int64_t tmr_overflow_time
;
52 #define ACPI_ENABLE 0xf1
53 #define ACPI_DISABLE 0xf0
55 static PIIX4PMState
*pm_state
;
57 static uint32_t get_pmtmr(PIIX4PMState
*s
)
60 d
= muldiv64(qemu_get_clock(vm_clock
), PM_TIMER_FREQUENCY
, get_ticks_per_sec());
64 static int get_pmsts(PIIX4PMState
*s
)
68 d
= muldiv64(qemu_get_clock(vm_clock
), PM_TIMER_FREQUENCY
,
70 if (d
>= s
->tmr_overflow_time
)
71 s
->pmsts
|= ACPI_BITMASK_TIMER_STATUS
;
75 static void pm_update_sci(PIIX4PMState
*s
)
81 sci_level
= (((pmsts
& s
->pmen
) &
82 (ACPI_BITMASK_RT_CLOCK_ENABLE
|
83 ACPI_BITMASK_POWER_BUTTON_ENABLE
|
84 ACPI_BITMASK_GLOBAL_LOCK_ENABLE
|
85 ACPI_BITMASK_TIMER_ENABLE
)) != 0);
86 qemu_set_irq(s
->irq
, sci_level
);
87 /* schedule a timer interruption if needed */
88 if ((s
->pmen
& ACPI_BITMASK_TIMER_ENABLE
) &&
89 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
)) {
90 expire_time
= muldiv64(s
->tmr_overflow_time
, get_ticks_per_sec(),
92 qemu_mod_timer(s
->tmr_timer
, expire_time
);
94 qemu_del_timer(s
->tmr_timer
);
98 static void pm_tmr_timer(void *opaque
)
100 PIIX4PMState
*s
= opaque
;
104 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
106 PIIX4PMState
*s
= opaque
;
113 pmsts
= get_pmsts(s
);
114 if (pmsts
& val
& ACPI_BITMASK_TIMER_STATUS
) {
115 /* if TMRSTS is reset, then compute the new overflow time */
116 d
= muldiv64(qemu_get_clock(vm_clock
), PM_TIMER_FREQUENCY
,
117 get_ticks_per_sec());
118 s
->tmr_overflow_time
= (d
+ 0x800000LL
) & ~0x7fffffLL
;
131 s
->pmcntrl
= val
& ~(ACPI_BITMASK_SLEEP_ENABLE
);
132 if (val
& ACPI_BITMASK_SLEEP_ENABLE
) {
133 /* change suspend type */
134 sus_typ
= (val
>> 10) & 7;
136 case 0: /* soft power off */
137 qemu_system_shutdown_request();
140 /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
141 Pretend that resume was caused by power button */
142 s
->pmsts
|= (ACPI_BITMASK_WAKE_STATUS
|
143 ACPI_BITMASK_POWER_BUTTON_STATUS
);
144 qemu_system_reset_request();
146 qemu_irq_raise(s
->cmos_s3
);
158 printf("PM writew port=0x%04x val=0x%04x\n", addr
, val
);
162 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
164 PIIX4PMState
*s
= opaque
;
183 printf("PM readw port=0x%04x val=0x%04x\n", addr
, val
);
188 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
190 // PIIX4PMState *s = opaque;
193 printf("PM writel port=0x%04x val=0x%08x\n", addr
, val
);
197 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
199 PIIX4PMState
*s
= opaque
;
212 printf("PM readl port=0x%04x val=0x%08x\n", addr
, val
);
217 static void apm_ctrl_changed(uint32_t val
, void *arg
)
219 PIIX4PMState
*s
= arg
;
221 /* ACPI specs 3.0, 4.7.2.5 */
222 if (val
== ACPI_ENABLE
) {
223 s
->pmcntrl
|= ACPI_BITMASK_SCI_ENABLE
;
224 } else if (val
== ACPI_DISABLE
) {
225 s
->pmcntrl
&= ~ACPI_BITMASK_SCI_ENABLE
;
228 if (s
->dev
.config
[0x5b] & (1 << 1)) {
230 qemu_irq_raise(s
->smi_irq
);
235 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
238 printf("ACPI: DBG: 0x%08x\n", val
);
242 static void pm_io_space_update(PIIX4PMState
*s
)
246 if (s
->dev
.config
[0x80] & 1) {
247 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
248 pm_io_base
&= 0xffc0;
250 /* XXX: need to improve memory and ioport allocation */
252 printf("PM: mapping to 0x%x\n", pm_io_base
);
254 register_ioport_write(pm_io_base
, 64, 2, pm_ioport_writew
, s
);
255 register_ioport_read(pm_io_base
, 64, 2, pm_ioport_readw
, s
);
256 register_ioport_write(pm_io_base
, 64, 4, pm_ioport_writel
, s
);
257 register_ioport_read(pm_io_base
, 64, 4, pm_ioport_readl
, s
);
261 static void pm_write_config(PCIDevice
*d
,
262 uint32_t address
, uint32_t val
, int len
)
264 pci_default_write_config(d
, address
, val
, len
);
265 if (range_covers_byte(address
, len
, 0x80))
266 pm_io_space_update((PIIX4PMState
*)d
);
269 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
271 PIIX4PMState
*s
= opaque
;
273 pm_io_space_update(s
);
277 static const VMStateDescription vmstate_acpi
= {
280 .minimum_version_id
= 1,
281 .minimum_version_id_old
= 1,
282 .post_load
= vmstate_acpi_post_load
,
283 .fields
= (VMStateField
[]) {
284 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
285 VMSTATE_UINT16(pmsts
, PIIX4PMState
),
286 VMSTATE_UINT16(pmen
, PIIX4PMState
),
287 VMSTATE_UINT16(pmcntrl
, PIIX4PMState
),
288 VMSTATE_STRUCT(apm
, PIIX4PMState
, 0, vmstate_apm
, APMState
),
289 VMSTATE_TIMER(tmr_timer
, PIIX4PMState
),
290 VMSTATE_INT64(tmr_overflow_time
, PIIX4PMState
),
291 VMSTATE_END_OF_LIST()
295 static void piix4_reset(void *opaque
)
297 PIIX4PMState
*s
= opaque
;
298 uint8_t *pci_conf
= s
->dev
.config
;
305 if (s
->kvm_enabled
) {
306 /* Mark SMM as already inited (until KVM supports SMM). */
307 pci_conf
[0x5B] = 0x02;
311 static void piix4_powerdown(void *opaque
, int irq
, int power_failing
)
313 PIIX4PMState
*s
= opaque
;
316 qemu_system_shutdown_request();
317 } else if (s
->pmen
& ACPI_BITMASK_POWER_BUTTON_ENABLE
) {
318 s
->pmsts
|= ACPI_BITMASK_POWER_BUTTON_STATUS
;
323 static int piix4_pm_initfn(PCIDevice
*dev
)
325 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
329 pci_conf
= s
->dev
.config
;
330 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
331 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371AB_3
);
332 pci_conf
[0x06] = 0x80;
333 pci_conf
[0x07] = 0x02;
334 pci_conf
[0x08] = 0x03; // revision number
335 pci_conf
[0x09] = 0x00;
336 pci_config_set_class(pci_conf
, PCI_CLASS_BRIDGE_OTHER
);
337 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
338 pci_conf
[0x3d] = 0x01; // interrupt pin 1
340 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
343 apm_init(&s
->apm
, apm_ctrl_changed
, s
);
345 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
347 if (s
->kvm_enabled
) {
348 /* Mark SMM as already inited to prevent SMM from running. KVM does not
349 * support SMM mode. */
350 pci_conf
[0x5B] = 0x02;
353 /* XXX: which specification is used ? The i82731AB has different
355 pci_conf
[0x5f] = (parallel_hds
[0] != NULL
? 0x80 : 0) | 0x10;
356 pci_conf
[0x63] = 0x60;
357 pci_conf
[0x67] = (serial_hds
[0] != NULL
? 0x08 : 0) |
358 (serial_hds
[1] != NULL
? 0x90 : 0);
360 pci_conf
[0x90] = s
->smb_io_base
| 1;
361 pci_conf
[0x91] = s
->smb_io_base
>> 8;
362 pci_conf
[0xd2] = 0x09;
363 register_ioport_write(s
->smb_io_base
, 64, 1, smb_ioport_writeb
, &s
->smb
);
364 register_ioport_read(s
->smb_io_base
, 64, 1, smb_ioport_readb
, &s
->smb
);
366 s
->tmr_timer
= qemu_new_timer(vm_clock
, pm_tmr_timer
, s
);
368 qemu_system_powerdown
= *qemu_allocate_irqs(piix4_powerdown
, s
, 1);
370 pm_smbus_init(&s
->dev
.qdev
, &s
->smb
);
371 qemu_register_reset(piix4_reset
, s
);
376 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
377 qemu_irq sci_irq
, qemu_irq cmos_s3
, qemu_irq smi_irq
,
383 dev
= pci_create(bus
, devfn
, "PIIX4_PM");
384 qdev_prop_set_uint32(&dev
->qdev
, "smb_io_base", smb_io_base
);
386 s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
388 s
->cmos_s3
= cmos_s3
;
389 s
->smi_irq
= smi_irq
;
390 s
->kvm_enabled
= kvm_enabled
;
392 qdev_init_nofail(&dev
->qdev
);
397 static PCIDeviceInfo piix4_pm_info
= {
398 .qdev
.name
= "PIIX4_PM",
400 .qdev
.size
= sizeof(PIIX4PMState
),
401 .qdev
.vmsd
= &vmstate_acpi
,
402 .init
= piix4_pm_initfn
,
403 .config_write
= pm_write_config
,
404 .qdev
.props
= (Property
[]) {
405 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState
, smb_io_base
, 0),
406 DEFINE_PROP_END_OF_LIST(),
410 static void piix4_pm_register(void)
412 pci_qdev_register(&piix4_pm_info
);
415 device_init(piix4_pm_register
);
417 #define GPE_BASE 0xafe0
418 #define PCI_BASE 0xae00
419 #define PCI_EJ_BASE 0xae08
422 uint16_t sts
; /* status */
423 uint16_t en
; /* enabled */
431 static struct gpe_regs gpe
;
432 static struct pci_status pci0_status
;
434 static uint32_t gpe_read_val(uint16_t val
, uint32_t addr
)
437 return (val
>> 8) & 0xff;
441 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
444 struct gpe_regs
*g
= opaque
;
448 val
= gpe_read_val(g
->sts
, addr
);
452 val
= gpe_read_val(g
->en
, addr
);
459 printf("gpe read %x == %x\n", addr
, val
);
464 static void gpe_write_val(uint16_t *cur
, int addr
, uint32_t val
)
467 *cur
= (*cur
& 0xff) | (val
<< 8);
469 *cur
= (*cur
& 0xff00) | (val
& 0xff);
472 static void gpe_reset_val(uint16_t *cur
, int addr
, uint32_t val
)
474 uint16_t x1
, x0
= val
& 0xff;
475 int shift
= (addr
& 1) ? 8 : 0;
477 x1
= (*cur
>> shift
) & 0xff;
481 *cur
= (*cur
& (0xff << (8 - shift
))) | (x1
<< shift
);
484 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
486 struct gpe_regs
*g
= opaque
;
490 gpe_reset_val(&g
->sts
, addr
, val
);
494 gpe_write_val(&g
->en
, addr
, val
);
501 printf("gpe write %x <== %d\n", addr
, val
);
505 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
508 struct pci_status
*g
= opaque
;
521 printf("pcihotplug read %x == %x\n", addr
, val
);
526 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
528 struct pci_status
*g
= opaque
;
539 printf("pcihotplug write %x <== %d\n", addr
, val
);
543 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
546 printf("pciej read %x\n", addr
);
551 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
553 BusState
*bus
= opaque
;
554 DeviceState
*qdev
, *next
;
556 int slot
= ffs(val
) - 1;
558 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
559 dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
560 if (PCI_SLOT(dev
->devfn
) == slot
) {
567 printf("pciej write %x <== %d\n", addr
, val
);
571 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
, int state
);
573 void piix4_acpi_system_hot_add_init(PCIBus
*bus
)
575 register_ioport_write(GPE_BASE
, 4, 1, gpe_writeb
, &gpe
);
576 register_ioport_read(GPE_BASE
, 4, 1, gpe_readb
, &gpe
);
578 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, &pci0_status
);
579 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, &pci0_status
);
581 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, bus
);
582 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, bus
);
584 pci_bus_hotplug(bus
, piix4_device_hotplug
, NULL
);
587 static void enable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
590 p
->up
|= (1 << slot
);
593 static void disable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
596 p
->down
|= (1 << slot
);
599 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
, int state
)
601 int slot
= PCI_SLOT(dev
->devfn
);
604 pci0_status
.down
= 0;
606 enable_device(&pci0_status
, &gpe
, slot
);
608 disable_device(&pci0_status
, &gpe
, slot
);
610 qemu_set_irq(pm_state
->irq
, 1);
611 qemu_set_irq(pm_state
->irq
, 0);