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/>
18 * Contributions after 2012-01-13 are licensed under the terms of the
19 * GNU GPL, version 2 or (at your option) any later version.
35 # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
37 # define PIIX4_DPRINTF(format, ...) do { } while (0)
40 #define ACPI_DBG_IO_ADDR 0xb044
42 #define GPE_BASE 0xafe0
44 #define PCI_UP_BASE 0xae00
45 #define PCI_DOWN_BASE 0xae04
46 #define PCI_EJ_BASE 0xae08
47 #define PCI_RMV_BASE 0xae0c
49 #define PIIX4_PCI_HOTPLUG_STATUS 2
52 uint32_t up
; /* deprecated, maintained for migration compatibility */
56 typedef struct PIIX4PMState
{
69 Notifier machine_ready
;
70 Notifier powerdown_notifier
;
73 struct pci_status pci0_status
;
74 uint32_t pci0_hotplug_enable
;
75 uint32_t pci0_slot_device_present
;
82 static void piix4_acpi_system_hot_add_init(PCIBus
*bus
, PIIX4PMState
*s
);
84 #define ACPI_ENABLE 0xf1
85 #define ACPI_DISABLE 0xf0
87 static void pm_update_sci(PIIX4PMState
*s
)
91 pmsts
= acpi_pm1_evt_get_sts(&s
->ar
);
92 sci_level
= (((pmsts
& s
->ar
.pm1
.evt
.en
) &
93 (ACPI_BITMASK_RT_CLOCK_ENABLE
|
94 ACPI_BITMASK_POWER_BUTTON_ENABLE
|
95 ACPI_BITMASK_GLOBAL_LOCK_ENABLE
|
96 ACPI_BITMASK_TIMER_ENABLE
)) != 0) ||
97 (((s
->ar
.gpe
.sts
[0] & s
->ar
.gpe
.en
[0])
98 & PIIX4_PCI_HOTPLUG_STATUS
) != 0);
100 qemu_set_irq(s
->irq
, sci_level
);
101 /* schedule a timer interruption if needed */
102 acpi_pm_tmr_update(&s
->ar
, (s
->ar
.pm1
.evt
.en
& ACPI_BITMASK_TIMER_ENABLE
) &&
103 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
));
106 static void pm_tmr_timer(ACPIREGS
*ar
)
108 PIIX4PMState
*s
= container_of(ar
, PIIX4PMState
, ar
);
112 static void pm_ioport_write(IORange
*ioport
, uint64_t addr
, unsigned width
,
115 PIIX4PMState
*s
= container_of(ioport
, PIIX4PMState
, ioport
);
118 PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
119 (unsigned)addr
, width
, (unsigned)val
);
124 acpi_pm1_evt_write_sts(&s
->ar
, val
);
128 acpi_pm1_evt_write_en(&s
->ar
, val
);
132 acpi_pm1_cnt_write(&s
->ar
, val
, s
->s4_val
);
137 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr
,
141 static void pm_ioport_read(IORange
*ioport
, uint64_t addr
, unsigned width
,
144 PIIX4PMState
*s
= container_of(ioport
, PIIX4PMState
, ioport
);
149 val
= acpi_pm1_evt_get_sts(&s
->ar
);
152 val
= s
->ar
.pm1
.evt
.en
;
155 val
= s
->ar
.pm1
.cnt
.cnt
;
158 val
= acpi_pm_tmr_get(&s
->ar
);
164 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr
, val
);
168 static const IORangeOps pm_iorange_ops
= {
169 .read
= pm_ioport_read
,
170 .write
= pm_ioport_write
,
173 static void apm_ctrl_changed(uint32_t val
, void *arg
)
175 PIIX4PMState
*s
= arg
;
177 /* ACPI specs 3.0, 4.7.2.5 */
178 acpi_pm1_cnt_update(&s
->ar
, val
== ACPI_ENABLE
, val
== ACPI_DISABLE
);
180 if (s
->dev
.config
[0x5b] & (1 << 1)) {
182 qemu_irq_raise(s
->smi_irq
);
187 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
189 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val
);
192 static void pm_io_space_update(PIIX4PMState
*s
)
196 if (s
->dev
.config
[0x80] & 1) {
197 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
198 pm_io_base
&= 0xffc0;
200 /* XXX: need to improve memory and ioport allocation */
201 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base
);
202 iorange_init(&s
->ioport
, &pm_iorange_ops
, pm_io_base
, 64);
203 ioport_register(&s
->ioport
);
207 static void pm_write_config(PCIDevice
*d
,
208 uint32_t address
, uint32_t val
, int len
)
210 pci_default_write_config(d
, address
, val
, len
);
211 if (range_covers_byte(address
, len
, 0x80))
212 pm_io_space_update((PIIX4PMState
*)d
);
215 static void vmstate_pci_status_pre_save(void *opaque
)
217 struct pci_status
*pci0_status
= opaque
;
218 PIIX4PMState
*s
= container_of(pci0_status
, PIIX4PMState
, pci0_status
);
220 /* We no longer track up, so build a safe value for migrating
221 * to a version that still does... of course these might get lost
222 * by an old buggy implementation, but we try. */
223 pci0_status
->up
= s
->pci0_slot_device_present
& s
->pci0_hotplug_enable
;
226 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
228 PIIX4PMState
*s
= opaque
;
230 pm_io_space_update(s
);
234 #define VMSTATE_GPE_ARRAY(_field, _state) \
236 .name = (stringify(_field)), \
239 .info = &vmstate_info_uint16, \
240 .size = sizeof(uint16_t), \
241 .flags = VMS_ARRAY | VMS_POINTER, \
242 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
245 static const VMStateDescription vmstate_gpe
= {
248 .minimum_version_id
= 1,
249 .minimum_version_id_old
= 1,
250 .fields
= (VMStateField
[]) {
251 VMSTATE_GPE_ARRAY(sts
, ACPIGPE
),
252 VMSTATE_GPE_ARRAY(en
, ACPIGPE
),
253 VMSTATE_END_OF_LIST()
257 static const VMStateDescription vmstate_pci_status
= {
258 .name
= "pci_status",
260 .minimum_version_id
= 1,
261 .minimum_version_id_old
= 1,
262 .pre_save
= vmstate_pci_status_pre_save
,
263 .fields
= (VMStateField
[]) {
264 VMSTATE_UINT32(up
, struct pci_status
),
265 VMSTATE_UINT32(down
, struct pci_status
),
266 VMSTATE_END_OF_LIST()
270 static const VMStateDescription vmstate_acpi
= {
273 .minimum_version_id
= 1,
274 .minimum_version_id_old
= 1,
275 .post_load
= vmstate_acpi_post_load
,
276 .fields
= (VMStateField
[]) {
277 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
278 VMSTATE_UINT16(ar
.pm1
.evt
.sts
, PIIX4PMState
),
279 VMSTATE_UINT16(ar
.pm1
.evt
.en
, PIIX4PMState
),
280 VMSTATE_UINT16(ar
.pm1
.cnt
.cnt
, PIIX4PMState
),
281 VMSTATE_STRUCT(apm
, PIIX4PMState
, 0, vmstate_apm
, APMState
),
282 VMSTATE_TIMER(ar
.tmr
.timer
, PIIX4PMState
),
283 VMSTATE_INT64(ar
.tmr
.overflow_time
, PIIX4PMState
),
284 VMSTATE_STRUCT(ar
.gpe
, PIIX4PMState
, 2, vmstate_gpe
, ACPIGPE
),
285 VMSTATE_STRUCT(pci0_status
, PIIX4PMState
, 2, vmstate_pci_status
,
287 VMSTATE_END_OF_LIST()
291 static void acpi_piix_eject_slot(PIIX4PMState
*s
, unsigned slots
)
293 BusChild
*kid
, *next
;
294 BusState
*bus
= qdev_get_parent_bus(&s
->dev
.qdev
);
295 int slot
= ffs(slots
) - 1;
296 bool slot_free
= true;
298 /* Mark request as complete */
299 s
->pci0_status
.down
&= ~(1U << slot
);
301 QTAILQ_FOREACH_SAFE(kid
, &bus
->children
, sibling
, next
) {
302 DeviceState
*qdev
= kid
->child
;
303 PCIDevice
*dev
= PCI_DEVICE(qdev
);
304 PCIDeviceClass
*pc
= PCI_DEVICE_GET_CLASS(dev
);
305 if (PCI_SLOT(dev
->devfn
) == slot
) {
306 if (pc
->no_hotplug
) {
314 s
->pci0_slot_device_present
&= ~(1U << slot
);
318 static void piix4_update_hotplug(PIIX4PMState
*s
)
320 PCIDevice
*dev
= &s
->dev
;
321 BusState
*bus
= qdev_get_parent_bus(&dev
->qdev
);
322 BusChild
*kid
, *next
;
324 /* Execute any pending removes during reset */
325 while (s
->pci0_status
.down
) {
326 acpi_piix_eject_slot(s
, s
->pci0_status
.down
);
329 s
->pci0_hotplug_enable
= ~0;
330 s
->pci0_slot_device_present
= 0;
332 QTAILQ_FOREACH_SAFE(kid
, &bus
->children
, sibling
, next
) {
333 DeviceState
*qdev
= kid
->child
;
334 PCIDevice
*pdev
= PCI_DEVICE(qdev
);
335 PCIDeviceClass
*pc
= PCI_DEVICE_GET_CLASS(pdev
);
336 int slot
= PCI_SLOT(pdev
->devfn
);
338 if (pc
->no_hotplug
) {
339 s
->pci0_hotplug_enable
&= ~(1U << slot
);
342 s
->pci0_slot_device_present
|= (1U << slot
);
346 static void piix4_reset(void *opaque
)
348 PIIX4PMState
*s
= opaque
;
349 uint8_t *pci_conf
= s
->dev
.config
;
356 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
359 if (s
->kvm_enabled
) {
360 /* Mark SMM as already inited (until KVM supports SMM). */
361 pci_conf
[0x5B] = 0x02;
363 piix4_update_hotplug(s
);
366 static void piix4_pm_powerdown_req(Notifier
*n
, void *opaque
)
368 PIIX4PMState
*s
= container_of(n
, PIIX4PMState
, powerdown_notifier
);
371 acpi_pm1_evt_power_down(&s
->ar
);
374 static void piix4_pm_machine_ready(Notifier
*n
, void *opaque
)
376 PIIX4PMState
*s
= container_of(n
, PIIX4PMState
, machine_ready
);
379 pci_conf
= s
->dev
.config
;
380 pci_conf
[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
381 pci_conf
[0x63] = 0x60;
382 pci_conf
[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
383 (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
387 static int piix4_pm_initfn(PCIDevice
*dev
)
389 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
392 pci_conf
= s
->dev
.config
;
393 pci_conf
[0x06] = 0x80;
394 pci_conf
[0x07] = 0x02;
395 pci_conf
[0x09] = 0x00;
396 pci_conf
[0x3d] = 0x01; // interrupt pin 1
399 apm_init(&s
->apm
, apm_ctrl_changed
, s
);
401 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
403 if (s
->kvm_enabled
) {
404 /* Mark SMM as already inited to prevent SMM from running. KVM does not
405 * support SMM mode. */
406 pci_conf
[0x5B] = 0x02;
409 /* XXX: which specification is used ? The i82731AB has different
411 pci_conf
[0x90] = s
->smb_io_base
| 1;
412 pci_conf
[0x91] = s
->smb_io_base
>> 8;
413 pci_conf
[0xd2] = 0x09;
414 register_ioport_write(s
->smb_io_base
, 64, 1, smb_ioport_writeb
, &s
->smb
);
415 register_ioport_read(s
->smb_io_base
, 64, 1, smb_ioport_readb
, &s
->smb
);
417 acpi_pm_tmr_init(&s
->ar
, pm_tmr_timer
);
418 acpi_gpe_init(&s
->ar
, GPE_LEN
);
420 s
->powerdown_notifier
.notify
= piix4_pm_powerdown_req
;
421 qemu_register_powerdown_notifier(&s
->powerdown_notifier
);
423 pm_smbus_init(&s
->dev
.qdev
, &s
->smb
);
424 s
->machine_ready
.notify
= piix4_pm_machine_ready
;
425 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
426 qemu_register_reset(piix4_reset
, s
);
427 piix4_acpi_system_hot_add_init(dev
->bus
, s
);
432 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
433 qemu_irq sci_irq
, qemu_irq smi_irq
,
434 int kvm_enabled
, void *fw_cfg
)
439 dev
= pci_create(bus
, devfn
, "PIIX4_PM");
440 qdev_prop_set_uint32(&dev
->qdev
, "smb_io_base", smb_io_base
);
442 s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
444 acpi_pm1_cnt_init(&s
->ar
);
445 s
->smi_irq
= smi_irq
;
446 s
->kvm_enabled
= kvm_enabled
;
448 qdev_init_nofail(&dev
->qdev
);
451 uint8_t suspend
[6] = {128, 0, 0, 129, 128, 128};
452 suspend
[3] = 1 | ((!s
->disable_s3
) << 7);
453 suspend
[4] = s
->s4_val
| ((!s
->disable_s4
) << 7);
455 fw_cfg_add_file(fw_cfg
, "etc/system-states", g_memdup(suspend
, 6), 6);
461 static Property piix4_pm_properties
[] = {
462 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState
, smb_io_base
, 0),
463 DEFINE_PROP_UINT8("disable_s3", PIIX4PMState
, disable_s3
, 0),
464 DEFINE_PROP_UINT8("disable_s4", PIIX4PMState
, disable_s4
, 0),
465 DEFINE_PROP_UINT8("s4_val", PIIX4PMState
, s4_val
, 2),
466 DEFINE_PROP_END_OF_LIST(),
469 static void piix4_pm_class_init(ObjectClass
*klass
, void *data
)
471 DeviceClass
*dc
= DEVICE_CLASS(klass
);
472 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
475 k
->init
= piix4_pm_initfn
;
476 k
->config_write
= pm_write_config
;
477 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
478 k
->device_id
= PCI_DEVICE_ID_INTEL_82371AB_3
;
480 k
->class_id
= PCI_CLASS_BRIDGE_OTHER
;
483 dc
->vmsd
= &vmstate_acpi
;
484 dc
->props
= piix4_pm_properties
;
487 static TypeInfo piix4_pm_info
= {
489 .parent
= TYPE_PCI_DEVICE
,
490 .instance_size
= sizeof(PIIX4PMState
),
491 .class_init
= piix4_pm_class_init
,
494 static void piix4_pm_register_types(void)
496 type_register_static(&piix4_pm_info
);
499 type_init(piix4_pm_register_types
)
501 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
503 PIIX4PMState
*s
= opaque
;
504 uint32_t val
= acpi_gpe_ioport_readb(&s
->ar
, addr
);
506 PIIX4_DPRINTF("gpe read %x == %x\n", addr
, val
);
510 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
512 PIIX4PMState
*s
= opaque
;
514 acpi_gpe_ioport_writeb(&s
->ar
, addr
, val
);
517 PIIX4_DPRINTF("gpe write %x <== %d\n", addr
, val
);
520 static uint32_t pci_up_read(void *opaque
, uint32_t addr
)
522 PIIX4PMState
*s
= opaque
;
525 /* Manufacture an "up" value to cause a device check on any hotplug
526 * slot with a device. Extra device checks are harmless. */
527 val
= s
->pci0_slot_device_present
& s
->pci0_hotplug_enable
;
529 PIIX4_DPRINTF("pci_up_read %x\n", val
);
533 static uint32_t pci_down_read(void *opaque
, uint32_t addr
)
535 PIIX4PMState
*s
= opaque
;
536 uint32_t val
= s
->pci0_status
.down
;
538 PIIX4_DPRINTF("pci_down_read %x\n", val
);
542 static uint32_t pci_features_read(void *opaque
, uint32_t addr
)
544 /* No feature defined yet */
545 PIIX4_DPRINTF("pci_features_read %x\n", 0);
549 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
551 acpi_piix_eject_slot(opaque
, val
);
553 PIIX4_DPRINTF("pciej write %x <== %d\n", addr
, val
);
556 static uint32_t pcirmv_read(void *opaque
, uint32_t addr
)
558 PIIX4PMState
*s
= opaque
;
560 return s
->pci0_hotplug_enable
;
563 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
,
564 PCIHotplugState state
);
566 static void piix4_acpi_system_hot_add_init(PCIBus
*bus
, PIIX4PMState
*s
)
569 register_ioport_write(GPE_BASE
, GPE_LEN
, 1, gpe_writeb
, s
);
570 register_ioport_read(GPE_BASE
, GPE_LEN
, 1, gpe_readb
, s
);
571 acpi_gpe_blk(&s
->ar
, GPE_BASE
);
573 register_ioport_read(PCI_UP_BASE
, 4, 4, pci_up_read
, s
);
574 register_ioport_read(PCI_DOWN_BASE
, 4, 4, pci_down_read
, s
);
576 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, s
);
577 register_ioport_read(PCI_EJ_BASE
, 4, 4, pci_features_read
, s
);
579 register_ioport_read(PCI_RMV_BASE
, 4, 4, pcirmv_read
, s
);
581 pci_bus_hotplug(bus
, piix4_device_hotplug
, &s
->dev
.qdev
);
584 static void enable_device(PIIX4PMState
*s
, int slot
)
586 s
->ar
.gpe
.sts
[0] |= PIIX4_PCI_HOTPLUG_STATUS
;
587 s
->pci0_slot_device_present
|= (1U << slot
);
590 static void disable_device(PIIX4PMState
*s
, int slot
)
592 s
->ar
.gpe
.sts
[0] |= PIIX4_PCI_HOTPLUG_STATUS
;
593 s
->pci0_status
.down
|= (1U << slot
);
596 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
,
597 PCIHotplugState state
)
599 int slot
= PCI_SLOT(dev
->devfn
);
600 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
,
603 /* Don't send event when device is enabled during qemu machine creation:
604 * it is present on boot, no hotplug event is necessary. We do send an
605 * event when the device is disabled later. */
606 if (state
== PCI_COLDPLUG_ENABLED
) {
607 s
->pci0_slot_device_present
|= (1U << slot
);
611 if (state
== PCI_HOTPLUG_ENABLED
) {
612 enable_device(s
, slot
);
614 disable_device(s
, slot
);