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
39 #define PCI_BASE 0xae00
40 #define PCI_EJ_BASE 0xae08
41 #define PCI_RMV_BASE 0xae0c
43 #define PIIX4_PCI_HOTPLUG_STATUS 2
50 typedef struct PIIX4PMState
{
69 struct pci_status pci0_status
;
70 uint32_t pci0_hotplug_enable
;
73 static void piix4_acpi_system_hot_add_init(PCIBus
*bus
, PIIX4PMState
*s
);
75 #define ACPI_ENABLE 0xf1
76 #define ACPI_DISABLE 0xf0
78 static void pm_update_sci(PIIX4PMState
*s
)
82 pmsts
= acpi_pm1_evt_get_sts(&s
->pm1a
, s
->tmr
.overflow_time
);
83 sci_level
= (((pmsts
& s
->pm1a
.en
) &
84 (ACPI_BITMASK_RT_CLOCK_ENABLE
|
85 ACPI_BITMASK_POWER_BUTTON_ENABLE
|
86 ACPI_BITMASK_GLOBAL_LOCK_ENABLE
|
87 ACPI_BITMASK_TIMER_ENABLE
)) != 0) ||
88 (((s
->gpe
.sts
[0] & s
->gpe
.en
[0]) & PIIX4_PCI_HOTPLUG_STATUS
) != 0);
90 qemu_set_irq(s
->irq
, sci_level
);
91 /* schedule a timer interruption if needed */
92 acpi_pm_tmr_update(&s
->tmr
, (s
->pm1a
.en
& ACPI_BITMASK_TIMER_ENABLE
) &&
93 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
));
96 static void pm_tmr_timer(ACPIPMTimer
*tmr
)
98 PIIX4PMState
*s
= container_of(tmr
, PIIX4PMState
, tmr
);
102 static void pm_ioport_write(IORange
*ioport
, uint64_t addr
, unsigned width
,
105 PIIX4PMState
*s
= container_of(ioport
, PIIX4PMState
, ioport
);
108 PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
109 (unsigned)addr
, width
, (unsigned)val
);
114 acpi_pm1_evt_write_sts(&s
->pm1a
, &s
->tmr
, val
);
122 acpi_pm1_cnt_write(&s
->pm1a
, &s
->pm1_cnt
, val
);
127 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr
,
131 static void pm_ioport_read(IORange
*ioport
, uint64_t addr
, unsigned width
,
134 PIIX4PMState
*s
= container_of(ioport
, PIIX4PMState
, ioport
);
139 val
= acpi_pm1_evt_get_sts(&s
->pm1a
, s
->tmr
.overflow_time
);
145 val
= s
->pm1_cnt
.cnt
;
148 val
= acpi_pm_tmr_get(&s
->tmr
);
154 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr
, val
);
158 static const IORangeOps pm_iorange_ops
= {
159 .read
= pm_ioport_read
,
160 .write
= pm_ioport_write
,
163 static void apm_ctrl_changed(uint32_t val
, void *arg
)
165 PIIX4PMState
*s
= arg
;
167 /* ACPI specs 3.0, 4.7.2.5 */
168 acpi_pm1_cnt_update(&s
->pm1_cnt
, val
== ACPI_ENABLE
, val
== ACPI_DISABLE
);
170 if (s
->dev
.config
[0x5b] & (1 << 1)) {
172 qemu_irq_raise(s
->smi_irq
);
177 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
179 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val
);
182 static void pm_io_space_update(PIIX4PMState
*s
)
186 if (s
->dev
.config
[0x80] & 1) {
187 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
188 pm_io_base
&= 0xffc0;
190 /* XXX: need to improve memory and ioport allocation */
191 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base
);
192 iorange_init(&s
->ioport
, &pm_iorange_ops
, pm_io_base
, 64);
193 ioport_register(&s
->ioport
);
197 static void pm_write_config(PCIDevice
*d
,
198 uint32_t address
, uint32_t val
, int len
)
200 pci_default_write_config(d
, address
, val
, len
);
201 if (range_covers_byte(address
, len
, 0x80))
202 pm_io_space_update((PIIX4PMState
*)d
);
205 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
207 PIIX4PMState
*s
= opaque
;
209 pm_io_space_update(s
);
213 #define VMSTATE_GPE_ARRAY(_field, _state) \
215 .name = (stringify(_field)), \
218 .info = &vmstate_info_uint16, \
219 .size = sizeof(uint16_t), \
220 .flags = VMS_ARRAY | VMS_POINTER, \
221 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
224 static const VMStateDescription vmstate_gpe
= {
227 .minimum_version_id
= 1,
228 .minimum_version_id_old
= 1,
229 .fields
= (VMStateField
[]) {
230 VMSTATE_GPE_ARRAY(sts
, ACPIGPE
),
231 VMSTATE_GPE_ARRAY(en
, ACPIGPE
),
232 VMSTATE_END_OF_LIST()
236 static const VMStateDescription vmstate_pci_status
= {
237 .name
= "pci_status",
239 .minimum_version_id
= 1,
240 .minimum_version_id_old
= 1,
241 .fields
= (VMStateField
[]) {
242 VMSTATE_UINT32(up
, struct pci_status
),
243 VMSTATE_UINT32(down
, struct pci_status
),
244 VMSTATE_END_OF_LIST()
248 static const VMStateDescription vmstate_acpi
= {
251 .minimum_version_id
= 1,
252 .minimum_version_id_old
= 1,
253 .post_load
= vmstate_acpi_post_load
,
254 .fields
= (VMStateField
[]) {
255 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
256 VMSTATE_UINT16(pm1a
.sts
, PIIX4PMState
),
257 VMSTATE_UINT16(pm1a
.en
, PIIX4PMState
),
258 VMSTATE_UINT16(pm1_cnt
.cnt
, PIIX4PMState
),
259 VMSTATE_STRUCT(apm
, PIIX4PMState
, 0, vmstate_apm
, APMState
),
260 VMSTATE_TIMER(tmr
.timer
, PIIX4PMState
),
261 VMSTATE_INT64(tmr
.overflow_time
, PIIX4PMState
),
262 VMSTATE_STRUCT(gpe
, PIIX4PMState
, 2, vmstate_gpe
, ACPIGPE
),
263 VMSTATE_STRUCT(pci0_status
, PIIX4PMState
, 2, vmstate_pci_status
,
265 VMSTATE_END_OF_LIST()
269 static void piix4_update_hotplug(PIIX4PMState
*s
)
271 PCIDevice
*dev
= &s
->dev
;
272 BusState
*bus
= qdev_get_parent_bus(&dev
->qdev
);
273 DeviceState
*qdev
, *next
;
275 s
->pci0_hotplug_enable
= ~0;
277 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
278 PCIDeviceInfo
*info
= container_of(qdev
->info
, PCIDeviceInfo
, qdev
);
279 PCIDevice
*pdev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
280 int slot
= PCI_SLOT(pdev
->devfn
);
282 if (info
->no_hotplug
) {
283 s
->pci0_hotplug_enable
&= ~(1 << slot
);
288 static void piix4_reset(void *opaque
)
290 PIIX4PMState
*s
= opaque
;
291 uint8_t *pci_conf
= s
->dev
.config
;
298 if (s
->kvm_enabled
) {
299 /* Mark SMM as already inited (until KVM supports SMM). */
300 pci_conf
[0x5B] = 0x02;
302 piix4_update_hotplug(s
);
305 static void piix4_powerdown(void *opaque
, int irq
, int power_failing
)
307 PIIX4PMState
*s
= opaque
;
308 ACPIPM1EVT
*pm1a
= s
? &s
->pm1a
: NULL
;
309 ACPIPMTimer
*tmr
= s
? &s
->tmr
: NULL
;
311 acpi_pm1_evt_power_down(pm1a
, tmr
);
314 static int piix4_pm_initfn(PCIDevice
*dev
)
316 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
319 pci_conf
= s
->dev
.config
;
320 pci_conf
[0x06] = 0x80;
321 pci_conf
[0x07] = 0x02;
322 pci_conf
[0x09] = 0x00;
323 pci_conf
[0x3d] = 0x01; // interrupt pin 1
325 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
328 apm_init(&s
->apm
, apm_ctrl_changed
, s
);
330 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
332 if (s
->kvm_enabled
) {
333 /* Mark SMM as already inited to prevent SMM from running. KVM does not
334 * support SMM mode. */
335 pci_conf
[0x5B] = 0x02;
338 /* XXX: which specification is used ? The i82731AB has different
340 pci_conf
[0x5f] = (parallel_hds
[0] != NULL
? 0x80 : 0) | 0x10;
341 pci_conf
[0x63] = 0x60;
342 pci_conf
[0x67] = (serial_hds
[0] != NULL
? 0x08 : 0) |
343 (serial_hds
[1] != NULL
? 0x90 : 0);
345 pci_conf
[0x90] = s
->smb_io_base
| 1;
346 pci_conf
[0x91] = s
->smb_io_base
>> 8;
347 pci_conf
[0xd2] = 0x09;
348 register_ioport_write(s
->smb_io_base
, 64, 1, smb_ioport_writeb
, &s
->smb
);
349 register_ioport_read(s
->smb_io_base
, 64, 1, smb_ioport_readb
, &s
->smb
);
351 acpi_pm_tmr_init(&s
->tmr
, pm_tmr_timer
);
352 acpi_gpe_init(&s
->gpe
, GPE_LEN
);
354 qemu_system_powerdown
= *qemu_allocate_irqs(piix4_powerdown
, s
, 1);
356 pm_smbus_init(&s
->dev
.qdev
, &s
->smb
);
357 qemu_register_reset(piix4_reset
, s
);
358 piix4_acpi_system_hot_add_init(dev
->bus
, s
);
363 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
364 qemu_irq sci_irq
, qemu_irq cmos_s3
, qemu_irq smi_irq
,
370 dev
= pci_create(bus
, devfn
, "PIIX4_PM");
371 qdev_prop_set_uint32(&dev
->qdev
, "smb_io_base", smb_io_base
);
373 s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
375 acpi_pm1_cnt_init(&s
->pm1_cnt
, cmos_s3
);
376 s
->smi_irq
= smi_irq
;
377 s
->kvm_enabled
= kvm_enabled
;
379 qdev_init_nofail(&dev
->qdev
);
384 static PCIDeviceInfo piix4_pm_info
= {
385 .qdev
.name
= "PIIX4_PM",
387 .qdev
.size
= sizeof(PIIX4PMState
),
388 .qdev
.vmsd
= &vmstate_acpi
,
391 .init
= piix4_pm_initfn
,
392 .config_write
= pm_write_config
,
393 .vendor_id
= PCI_VENDOR_ID_INTEL
,
394 .device_id
= PCI_DEVICE_ID_INTEL_82371AB_3
,
396 .class_id
= PCI_CLASS_BRIDGE_OTHER
,
397 .qdev
.props
= (Property
[]) {
398 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState
, smb_io_base
, 0),
399 DEFINE_PROP_END_OF_LIST(),
403 static void piix4_pm_register(void)
405 pci_qdev_register(&piix4_pm_info
);
408 device_init(piix4_pm_register
);
410 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
412 PIIX4PMState
*s
= opaque
;
413 uint32_t val
= acpi_gpe_ioport_readb(&s
->gpe
, addr
);
415 PIIX4_DPRINTF("gpe read %x == %x\n", addr
, val
);
419 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
421 PIIX4PMState
*s
= opaque
;
423 acpi_gpe_ioport_writeb(&s
->gpe
, addr
, val
);
426 PIIX4_DPRINTF("gpe write %x <== %d\n", addr
, val
);
429 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
432 struct pci_status
*g
= opaque
;
444 PIIX4_DPRINTF("pcihotplug read %x == %x\n", addr
, val
);
448 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
450 struct pci_status
*g
= opaque
;
460 PIIX4_DPRINTF("pcihotplug write %x <== %d\n", addr
, val
);
463 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
465 PIIX4_DPRINTF("pciej read %x\n", addr
);
469 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
471 BusState
*bus
= opaque
;
472 DeviceState
*qdev
, *next
;
474 int slot
= ffs(val
) - 1;
476 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
477 dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
478 if (PCI_SLOT(dev
->devfn
) == slot
) {
484 PIIX4_DPRINTF("pciej write %x <== %d\n", addr
, val
);
487 static uint32_t pcirmv_read(void *opaque
, uint32_t addr
)
489 PIIX4PMState
*s
= opaque
;
491 return s
->pci0_hotplug_enable
;
494 static void pcirmv_write(void *opaque
, uint32_t addr
, uint32_t val
)
499 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
,
500 PCIHotplugState state
);
502 static void piix4_acpi_system_hot_add_init(PCIBus
*bus
, PIIX4PMState
*s
)
504 struct pci_status
*pci0_status
= &s
->pci0_status
;
506 register_ioport_write(GPE_BASE
, GPE_LEN
, 1, gpe_writeb
, s
);
507 register_ioport_read(GPE_BASE
, GPE_LEN
, 1, gpe_readb
, s
);
508 acpi_gpe_blk(&s
->gpe
, GPE_BASE
);
510 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, pci0_status
);
511 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, pci0_status
);
513 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, bus
);
514 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, bus
);
516 register_ioport_write(PCI_RMV_BASE
, 4, 4, pcirmv_write
, s
);
517 register_ioport_read(PCI_RMV_BASE
, 4, 4, pcirmv_read
, s
);
519 pci_bus_hotplug(bus
, piix4_device_hotplug
, &s
->dev
.qdev
);
522 static void enable_device(PIIX4PMState
*s
, int slot
)
524 s
->gpe
.sts
[0] |= PIIX4_PCI_HOTPLUG_STATUS
;
525 s
->pci0_status
.up
|= (1 << slot
);
528 static void disable_device(PIIX4PMState
*s
, int slot
)
530 s
->gpe
.sts
[0] |= PIIX4_PCI_HOTPLUG_STATUS
;
531 s
->pci0_status
.down
|= (1 << slot
);
534 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
,
535 PCIHotplugState state
)
537 int slot
= PCI_SLOT(dev
->devfn
);
538 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
,
539 DO_UPCAST(PCIDevice
, qdev
, qdev
));
541 /* Don't send event when device is enabled during qemu machine creation:
542 * it is present on boot, no hotplug event is necessary. We do send an
543 * event when the device is disabled later. */
544 if (state
== PCI_COLDPLUG_ENABLED
) {
548 s
->pci0_status
.up
= 0;
549 s
->pci0_status
.down
= 0;
550 if (state
== PCI_HOTPLUG_ENABLED
) {
551 enable_device(s
, slot
);
553 disable_device(s
, slot
);