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
;
51 #define ACPI_ENABLE 0xf1
52 #define ACPI_DISABLE 0xf0
54 static PIIX4PMState
*pm_state
;
56 static uint32_t get_pmtmr(PIIX4PMState
*s
)
59 d
= muldiv64(qemu_get_clock(vm_clock
), PM_TIMER_FREQUENCY
, get_ticks_per_sec());
63 static int get_pmsts(PIIX4PMState
*s
)
67 d
= muldiv64(qemu_get_clock(vm_clock
), PM_TIMER_FREQUENCY
,
69 if (d
>= s
->tmr_overflow_time
)
70 s
->pmsts
|= ACPI_BITMASK_TIMER_STATUS
;
74 static void pm_update_sci(PIIX4PMState
*s
)
80 sci_level
= (((pmsts
& s
->pmen
) &
81 (ACPI_BITMASK_RT_CLOCK_ENABLE
|
82 ACPI_BITMASK_POWER_BUTTON_ENABLE
|
83 ACPI_BITMASK_GLOBAL_LOCK_ENABLE
|
84 ACPI_BITMASK_TIMER_ENABLE
)) != 0);
85 qemu_set_irq(s
->irq
, sci_level
);
86 /* schedule a timer interruption if needed */
87 if ((s
->pmen
& ACPI_BITMASK_TIMER_ENABLE
) &&
88 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
)) {
89 expire_time
= muldiv64(s
->tmr_overflow_time
, get_ticks_per_sec(),
91 qemu_mod_timer(s
->tmr_timer
, expire_time
);
93 qemu_del_timer(s
->tmr_timer
);
97 static void pm_tmr_timer(void *opaque
)
99 PIIX4PMState
*s
= opaque
;
103 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
105 PIIX4PMState
*s
= opaque
;
112 pmsts
= get_pmsts(s
);
113 if (pmsts
& val
& ACPI_BITMASK_TIMER_STATUS
) {
114 /* if TMRSTS is reset, then compute the new overflow time */
115 d
= muldiv64(qemu_get_clock(vm_clock
), PM_TIMER_FREQUENCY
,
116 get_ticks_per_sec());
117 s
->tmr_overflow_time
= (d
+ 0x800000LL
) & ~0x7fffffLL
;
130 s
->pmcntrl
= val
& ~(ACPI_BITMASK_SLEEP_ENABLE
);
131 if (val
& ACPI_BITMASK_SLEEP_ENABLE
) {
132 /* change suspend type */
133 sus_typ
= (val
>> 10) & 7;
135 case 0: /* soft power off */
136 qemu_system_shutdown_request();
139 /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
140 Pretend that resume was caused by power button */
141 s
->pmsts
|= (ACPI_BITMASK_WAKE_STATUS
|
142 ACPI_BITMASK_POWER_BUTTON_STATUS
);
143 qemu_system_reset_request();
145 qemu_irq_raise(s
->cmos_s3
);
157 printf("PM writew port=0x%04x val=0x%04x\n", addr
, val
);
161 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
163 PIIX4PMState
*s
= opaque
;
182 printf("PM readw port=0x%04x val=0x%04x\n", addr
, val
);
187 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
189 // PIIX4PMState *s = opaque;
192 printf("PM writel port=0x%04x val=0x%08x\n", addr
, val
);
196 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
198 PIIX4PMState
*s
= opaque
;
211 printf("PM readl port=0x%04x val=0x%08x\n", addr
, val
);
216 static void apm_ctrl_changed(uint32_t val
, void *arg
)
218 PIIX4PMState
*s
= arg
;
220 /* ACPI specs 3.0, 4.7.2.5 */
221 if (val
== ACPI_ENABLE
) {
222 s
->pmcntrl
|= ACPI_BITMASK_SCI_ENABLE
;
223 } else if (val
== ACPI_DISABLE
) {
224 s
->pmcntrl
&= ~ACPI_BITMASK_SCI_ENABLE
;
227 if (s
->dev
.config
[0x5b] & (1 << 1)) {
229 qemu_irq_raise(s
->smi_irq
);
234 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
237 printf("ACPI: DBG: 0x%08x\n", val
);
241 static void pm_io_space_update(PIIX4PMState
*s
)
245 if (s
->dev
.config
[0x80] & 1) {
246 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
247 pm_io_base
&= 0xffc0;
249 /* XXX: need to improve memory and ioport allocation */
251 printf("PM: mapping to 0x%x\n", pm_io_base
);
253 register_ioport_write(pm_io_base
, 64, 2, pm_ioport_writew
, s
);
254 register_ioport_read(pm_io_base
, 64, 2, pm_ioport_readw
, s
);
255 register_ioport_write(pm_io_base
, 64, 4, pm_ioport_writel
, s
);
256 register_ioport_read(pm_io_base
, 64, 4, pm_ioport_readl
, s
);
260 static void pm_write_config(PCIDevice
*d
,
261 uint32_t address
, uint32_t val
, int len
)
263 pci_default_write_config(d
, address
, val
, len
);
264 if (range_covers_byte(address
, len
, 0x80))
265 pm_io_space_update((PIIX4PMState
*)d
);
268 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
270 PIIX4PMState
*s
= opaque
;
272 pm_io_space_update(s
);
276 static const VMStateDescription vmstate_acpi
= {
279 .minimum_version_id
= 1,
280 .minimum_version_id_old
= 1,
281 .post_load
= vmstate_acpi_post_load
,
282 .fields
= (VMStateField
[]) {
283 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
284 VMSTATE_UINT16(pmsts
, PIIX4PMState
),
285 VMSTATE_UINT16(pmen
, PIIX4PMState
),
286 VMSTATE_UINT16(pmcntrl
, PIIX4PMState
),
287 VMSTATE_STRUCT(apm
, PIIX4PMState
, 0, vmstate_apm
, APMState
),
288 VMSTATE_TIMER(tmr_timer
, PIIX4PMState
),
289 VMSTATE_INT64(tmr_overflow_time
, PIIX4PMState
),
290 VMSTATE_END_OF_LIST()
294 static void piix4_reset(void *opaque
)
296 PIIX4PMState
*s
= opaque
;
297 uint8_t *pci_conf
= s
->dev
.config
;
304 if (s
->kvm_enabled
) {
305 /* Mark SMM as already inited (until KVM supports SMM). */
306 pci_conf
[0x5B] = 0x02;
310 static void piix4_powerdown(void *opaque
, int irq
, int power_failing
)
312 PIIX4PMState
*s
= opaque
;
315 qemu_system_shutdown_request();
316 } else if (s
->pmen
& ACPI_BITMASK_POWER_BUTTON_ENABLE
) {
317 s
->pmsts
|= ACPI_BITMASK_POWER_BUTTON_STATUS
;
322 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
323 qemu_irq sci_irq
, qemu_irq cmos_s3
, qemu_irq smi_irq
,
329 s
= (PIIX4PMState
*)pci_register_device(bus
,
330 "PM", sizeof(PIIX4PMState
),
331 devfn
, NULL
, pm_write_config
);
333 pci_conf
= s
->dev
.config
;
334 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
335 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_82371AB_3
);
336 pci_conf
[0x06] = 0x80;
337 pci_conf
[0x07] = 0x02;
338 pci_conf
[0x08] = 0x03; // revision number
339 pci_conf
[0x09] = 0x00;
340 pci_config_set_class(pci_conf
, PCI_CLASS_BRIDGE_OTHER
);
341 pci_conf
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_NORMAL
; // header_type
342 pci_conf
[0x3d] = 0x01; // interrupt pin 1
344 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
347 apm_init(&s
->apm
, apm_ctrl_changed
, s
);
349 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
351 s
->kvm_enabled
= kvm_enabled
;
352 if (s
->kvm_enabled
) {
353 /* Mark SMM as already inited to prevent SMM from running. KVM does not
354 * support SMM mode. */
355 pci_conf
[0x5B] = 0x02;
358 /* XXX: which specification is used ? The i82731AB has different
360 pci_conf
[0x5f] = (parallel_hds
[0] != NULL
? 0x80 : 0) | 0x10;
361 pci_conf
[0x63] = 0x60;
362 pci_conf
[0x67] = (serial_hds
[0] != NULL
? 0x08 : 0) |
363 (serial_hds
[1] != NULL
? 0x90 : 0);
365 pci_conf
[0x90] = smb_io_base
| 1;
366 pci_conf
[0x91] = smb_io_base
>> 8;
367 pci_conf
[0xd2] = 0x09;
368 register_ioport_write(smb_io_base
, 64, 1, smb_ioport_writeb
, &s
->smb
);
369 register_ioport_read(smb_io_base
, 64, 1, smb_ioport_readb
, &s
->smb
);
371 s
->tmr_timer
= qemu_new_timer(vm_clock
, pm_tmr_timer
, s
);
373 qemu_system_powerdown
= *qemu_allocate_irqs(piix4_powerdown
, s
, 1);
375 vmstate_register(0, &vmstate_acpi
, s
);
377 pm_smbus_init(NULL
, &s
->smb
);
379 s
->cmos_s3
= cmos_s3
;
380 s
->smi_irq
= smi_irq
;
381 qemu_register_reset(piix4_reset
, s
);
386 #define GPE_BASE 0xafe0
387 #define PCI_BASE 0xae00
388 #define PCI_EJ_BASE 0xae08
391 uint16_t sts
; /* status */
392 uint16_t en
; /* enabled */
400 static struct gpe_regs gpe
;
401 static struct pci_status pci0_status
;
403 static uint32_t gpe_read_val(uint16_t val
, uint32_t addr
)
406 return (val
>> 8) & 0xff;
410 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
413 struct gpe_regs
*g
= opaque
;
417 val
= gpe_read_val(g
->sts
, addr
);
421 val
= gpe_read_val(g
->en
, addr
);
428 printf("gpe read %x == %x\n", addr
, val
);
433 static void gpe_write_val(uint16_t *cur
, int addr
, uint32_t val
)
436 *cur
= (*cur
& 0xff) | (val
<< 8);
438 *cur
= (*cur
& 0xff00) | (val
& 0xff);
441 static void gpe_reset_val(uint16_t *cur
, int addr
, uint32_t val
)
443 uint16_t x1
, x0
= val
& 0xff;
444 int shift
= (addr
& 1) ? 8 : 0;
446 x1
= (*cur
>> shift
) & 0xff;
450 *cur
= (*cur
& (0xff << (8 - shift
))) | (x1
<< shift
);
453 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
455 struct gpe_regs
*g
= opaque
;
459 gpe_reset_val(&g
->sts
, addr
, val
);
463 gpe_write_val(&g
->en
, addr
, val
);
470 printf("gpe write %x <== %d\n", addr
, val
);
474 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
477 struct pci_status
*g
= opaque
;
490 printf("pcihotplug read %x == %x\n", addr
, val
);
495 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
497 struct pci_status
*g
= opaque
;
508 printf("pcihotplug write %x <== %d\n", addr
, val
);
512 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
515 printf("pciej read %x\n", addr
);
520 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
522 BusState
*bus
= opaque
;
523 DeviceState
*qdev
, *next
;
525 int slot
= ffs(val
) - 1;
527 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
528 dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
529 if (PCI_SLOT(dev
->devfn
) == slot
) {
536 printf("pciej write %x <== %d\n", addr
, val
);
540 static int piix4_device_hotplug(PCIDevice
*dev
, int state
);
542 void piix4_acpi_system_hot_add_init(PCIBus
*bus
)
544 register_ioport_write(GPE_BASE
, 4, 1, gpe_writeb
, &gpe
);
545 register_ioport_read(GPE_BASE
, 4, 1, gpe_readb
, &gpe
);
547 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, &pci0_status
);
548 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, &pci0_status
);
550 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, bus
);
551 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, bus
);
553 pci_bus_hotplug(bus
, piix4_device_hotplug
);
556 static void enable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
559 p
->up
|= (1 << slot
);
562 static void disable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
565 p
->down
|= (1 << slot
);
568 static int piix4_device_hotplug(PCIDevice
*dev
, int state
)
570 int slot
= PCI_SLOT(dev
->devfn
);
573 pci0_status
.down
= 0;
575 enable_device(&pci0_status
, &gpe
, slot
);
577 disable_device(&pci0_status
, &gpe
, slot
);
579 qemu_set_irq(pm_state
->irq
, 1);
580 qemu_set_irq(pm_state
->irq
, 0);