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.
34 # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
36 # define PIIX4_DPRINTF(format, ...) do { } while (0)
39 #define ACPI_DBG_IO_ADDR 0xb044
41 #define GPE_BASE 0xafe0
42 #define PROC_BASE 0xaf00
44 #define PCI_BASE 0xae00
45 #define PCI_EJ_BASE 0xae08
46 #define PCI_RMV_BASE 0xae0c
48 #define PIIX4_CPU_HOTPLUG_STATUS 4
49 #define PIIX4_PCI_HOTPLUG_STATUS 2
60 typedef struct PIIX4PMState
{
76 Notifier machine_ready
;
80 struct gpe_regs gpe_cpu
;
81 struct pci_status pci0_status
;
82 uint32_t pci0_hotplug_enable
;
85 static void piix4_acpi_system_hot_add_init(PCIBus
*bus
, PIIX4PMState
*s
);
87 #define ACPI_ENABLE 0xf1
88 #define ACPI_DISABLE 0xf0
90 static void pm_update_sci(PIIX4PMState
*s
)
94 pmsts
= acpi_pm1_evt_get_sts(&s
->pm1a
, s
->tmr
.overflow_time
);
95 sci_level
= (((pmsts
& s
->pm1a
.en
) &
96 (ACPI_BITMASK_RT_CLOCK_ENABLE
|
97 ACPI_BITMASK_POWER_BUTTON_ENABLE
|
98 ACPI_BITMASK_GLOBAL_LOCK_ENABLE
|
99 ACPI_BITMASK_TIMER_ENABLE
)) != 0) ||
100 (((s
->gpe
.sts
[0] & s
->gpe
.en
[0]) & PIIX4_PCI_HOTPLUG_STATUS
) != 0);
102 qemu_set_irq(s
->irq
, sci_level
);
103 /* schedule a timer interruption if needed */
104 acpi_pm_tmr_update(&s
->tmr
, (s
->pm1a
.en
& ACPI_BITMASK_TIMER_ENABLE
) &&
105 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
));
108 static void pm_tmr_timer(ACPIPMTimer
*tmr
)
110 PIIX4PMState
*s
= container_of(tmr
, PIIX4PMState
, tmr
);
114 static void pm_ioport_write(IORange
*ioport
, uint64_t addr
, unsigned width
,
117 PIIX4PMState
*s
= container_of(ioport
, PIIX4PMState
, ioport
);
120 PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
121 (unsigned)addr
, width
, (unsigned)val
);
126 acpi_pm1_evt_write_sts(&s
->pm1a
, &s
->tmr
, val
);
134 acpi_pm1_cnt_write(&s
->pm1a
, &s
->pm1_cnt
, val
);
139 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr
,
143 static void pm_ioport_read(IORange
*ioport
, uint64_t addr
, unsigned width
,
146 PIIX4PMState
*s
= container_of(ioport
, PIIX4PMState
, ioport
);
151 val
= acpi_pm1_evt_get_sts(&s
->pm1a
, s
->tmr
.overflow_time
);
157 val
= s
->pm1_cnt
.cnt
;
160 val
= acpi_pm_tmr_get(&s
->tmr
);
166 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr
, val
);
170 static const IORangeOps pm_iorange_ops
= {
171 .read
= pm_ioport_read
,
172 .write
= pm_ioport_write
,
175 static void apm_ctrl_changed(uint32_t val
, void *arg
)
177 PIIX4PMState
*s
= arg
;
179 /* ACPI specs 3.0, 4.7.2.5 */
180 acpi_pm1_cnt_update(&s
->pm1_cnt
, val
== ACPI_ENABLE
, val
== ACPI_DISABLE
);
182 if (s
->dev
.config
[0x5b] & (1 << 1)) {
184 qemu_irq_raise(s
->smi_irq
);
189 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
191 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val
);
194 static void pm_io_space_update(PIIX4PMState
*s
)
198 if (s
->dev
.config
[0x80] & 1) {
199 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
200 pm_io_base
&= 0xffc0;
202 /* XXX: need to improve memory and ioport allocation */
203 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base
);
204 iorange_init(&s
->ioport
, &pm_iorange_ops
, pm_io_base
, 64);
205 ioport_register(&s
->ioport
);
209 static void pm_write_config(PCIDevice
*d
,
210 uint32_t address
, uint32_t val
, int len
)
212 pci_default_write_config(d
, address
, val
, len
);
213 if (range_covers_byte(address
, len
, 0x80))
214 pm_io_space_update((PIIX4PMState
*)d
);
217 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
219 PIIX4PMState
*s
= opaque
;
221 pm_io_space_update(s
);
225 #define VMSTATE_GPE_ARRAY(_field, _state) \
227 .name = (stringify(_field)), \
229 .info = &vmstate_info_uint16, \
230 .size = sizeof(uint16_t), \
231 .flags = VMS_SINGLE | VMS_POINTER, \
232 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
235 static const VMStateDescription vmstate_gpe
= {
238 .minimum_version_id
= 1,
239 .minimum_version_id_old
= 1,
240 .fields
= (VMStateField
[]) {
241 VMSTATE_GPE_ARRAY(sts
, ACPIGPE
),
242 VMSTATE_GPE_ARRAY(en
, ACPIGPE
),
243 VMSTATE_END_OF_LIST()
247 static const VMStateDescription vmstate_pci_status
= {
248 .name
= "pci_status",
250 .minimum_version_id
= 1,
251 .minimum_version_id_old
= 1,
252 .fields
= (VMStateField
[]) {
253 VMSTATE_UINT32(up
, struct pci_status
),
254 VMSTATE_UINT32(down
, struct pci_status
),
255 VMSTATE_END_OF_LIST()
259 static const VMStateDescription vmstate_acpi
= {
262 .minimum_version_id
= 1,
263 .minimum_version_id_old
= 1,
264 .post_load
= vmstate_acpi_post_load
,
265 .fields
= (VMStateField
[]) {
266 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
267 VMSTATE_UINT16(pm1a
.sts
, PIIX4PMState
),
268 VMSTATE_UINT16(pm1a
.en
, PIIX4PMState
),
269 VMSTATE_UINT16(pm1_cnt
.cnt
, PIIX4PMState
),
270 VMSTATE_STRUCT(apm
, PIIX4PMState
, 0, vmstate_apm
, APMState
),
271 VMSTATE_TIMER(tmr
.timer
, PIIX4PMState
),
272 VMSTATE_INT64(tmr
.overflow_time
, PIIX4PMState
),
273 VMSTATE_STRUCT(gpe
, PIIX4PMState
, 2, vmstate_gpe
, ACPIGPE
),
274 VMSTATE_STRUCT(pci0_status
, PIIX4PMState
, 2, vmstate_pci_status
,
276 VMSTATE_END_OF_LIST()
280 static void piix4_update_hotplug(PIIX4PMState
*s
)
282 PCIDevice
*dev
= &s
->dev
;
283 BusState
*bus
= qdev_get_parent_bus(&dev
->qdev
);
284 DeviceState
*qdev
, *next
;
286 s
->pci0_hotplug_enable
= ~0;
288 QTAILQ_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
289 PCIDevice
*pdev
= PCI_DEVICE(qdev
);
290 PCIDeviceClass
*pc
= PCI_DEVICE_GET_CLASS(pdev
);
291 int slot
= PCI_SLOT(pdev
->devfn
);
293 if (pc
->no_hotplug
) {
294 s
->pci0_hotplug_enable
&= ~(1 << slot
);
299 static void piix4_reset(void *opaque
)
301 PIIX4PMState
*s
= opaque
;
302 uint8_t *pci_conf
= s
->dev
.config
;
309 if (s
->kvm_enabled
) {
310 /* Mark SMM as already inited (until KVM supports SMM). */
311 pci_conf
[0x5B] = 0x02;
313 piix4_update_hotplug(s
);
316 static void piix4_powerdown(void *opaque
, int irq
, int power_failing
)
318 PIIX4PMState
*s
= opaque
;
319 ACPIPM1EVT
*pm1a
= s
? &s
->pm1a
: NULL
;
320 ACPIPMTimer
*tmr
= s
? &s
->tmr
: NULL
;
322 acpi_pm1_evt_power_down(pm1a
, tmr
);
325 static void piix4_pm_machine_ready(Notifier
*n
, void *opaque
)
327 PIIX4PMState
*s
= container_of(n
, PIIX4PMState
, machine_ready
);
330 pci_conf
= s
->dev
.config
;
331 pci_conf
[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
332 pci_conf
[0x63] = 0x60;
333 pci_conf
[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
334 (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
338 static PIIX4PMState
*global_piix4_pm_state
; /* cpu hotadd */
340 static int piix4_pm_initfn(PCIDevice
*dev
)
342 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
346 global_piix4_pm_state
= s
;
348 pci_conf
= s
->dev
.config
;
349 pci_conf
[0x06] = 0x80;
350 pci_conf
[0x07] = 0x02;
351 pci_conf
[0x09] = 0x00;
352 pci_conf
[0x3d] = 0x01; // interrupt pin 1
354 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
357 apm_init(&s
->apm
, apm_ctrl_changed
, s
);
359 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
361 if (s
->kvm_enabled
) {
362 /* Mark SMM as already inited to prevent SMM from running. KVM does not
363 * support SMM mode. */
364 pci_conf
[0x5B] = 0x02;
367 /* XXX: which specification is used ? The i82731AB has different
369 pci_conf
[0x90] = s
->smb_io_base
| 1;
370 pci_conf
[0x91] = s
->smb_io_base
>> 8;
371 pci_conf
[0xd2] = 0x09;
372 register_ioport_write(s
->smb_io_base
, 64, 1, smb_ioport_writeb
, &s
->smb
);
373 register_ioport_read(s
->smb_io_base
, 64, 1, smb_ioport_readb
, &s
->smb
);
375 acpi_pm_tmr_init(&s
->tmr
, pm_tmr_timer
);
376 acpi_gpe_init(&s
->gpe
, GPE_LEN
);
378 qemu_system_powerdown
= *qemu_allocate_irqs(piix4_powerdown
, s
, 1);
380 pm_smbus_init(&s
->dev
.qdev
, &s
->smb
);
381 s
->machine_ready
.notify
= piix4_pm_machine_ready
;
382 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
383 qemu_register_reset(piix4_reset
, s
);
384 piix4_acpi_system_hot_add_init(dev
->bus
, s
);
389 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
390 qemu_irq sci_irq
, qemu_irq cmos_s3
, qemu_irq smi_irq
,
396 dev
= pci_create(bus
, devfn
, "PIIX4_PM");
397 qdev_prop_set_uint32(&dev
->qdev
, "smb_io_base", smb_io_base
);
399 s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
401 acpi_pm1_cnt_init(&s
->pm1_cnt
, cmos_s3
);
402 s
->smi_irq
= smi_irq
;
403 s
->kvm_enabled
= kvm_enabled
;
405 qdev_init_nofail(&dev
->qdev
);
410 static Property piix4_pm_properties
[] = {
411 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState
, smb_io_base
, 0),
412 DEFINE_PROP_END_OF_LIST(),
415 static void piix4_pm_class_init(ObjectClass
*klass
, void *data
)
417 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
420 k
->init
= piix4_pm_initfn
;
421 k
->config_write
= pm_write_config
;
422 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
423 k
->device_id
= PCI_DEVICE_ID_INTEL_82371AB_3
;
425 k
->class_id
= PCI_CLASS_BRIDGE_OTHER
;
428 static DeviceInfo piix4_pm_info
= {
431 .size
= sizeof(PIIX4PMState
),
432 .vmsd
= &vmstate_acpi
,
434 .props
= piix4_pm_properties
,
435 .class_init
= piix4_pm_class_init
,
438 static void piix4_pm_register(void)
440 pci_qdev_register(&piix4_pm_info
);
443 device_init(piix4_pm_register
);
445 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
447 PIIX4PMState
*s
= opaque
;
449 struct gpe_regs
*g
= &s
->gpe_cpu
;
452 case PROC_BASE
... PROC_BASE
+31:
453 val
= g
->cpus_sts
[addr
- PROC_BASE
];
456 val
= acpi_gpe_ioport_readb(&s
->gpe
, addr
);
459 PIIX4_DPRINTF("gpe read %x == %x\n", addr
, val
);
463 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
465 PIIX4PMState
*s
= opaque
;
467 acpi_gpe_ioport_writeb(&s
->gpe
, addr
, val
);
470 PIIX4_DPRINTF("gpe write %x <== %d\n", addr
, val
);
473 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
476 struct pci_status
*g
= opaque
;
488 PIIX4_DPRINTF("pcihotplug read %x == %x\n", addr
, val
);
492 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
494 struct pci_status
*g
= opaque
;
504 PIIX4_DPRINTF("pcihotplug write %x <== %d\n", addr
, val
);
507 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
509 PIIX4_DPRINTF("pciej read %x\n", addr
);
513 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
515 BusState
*bus
= opaque
;
516 DeviceState
*qdev
, *next
;
517 int slot
= ffs(val
) - 1;
519 QTAILQ_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
520 PCIDevice
*dev
= PCI_DEVICE(qdev
);
521 PCIDeviceClass
*pc
= PCI_DEVICE_GET_CLASS(dev
);
522 if (PCI_SLOT(dev
->devfn
) == slot
&& !pc
->no_hotplug
) {
528 PIIX4_DPRINTF("pciej write %x <== %d\n", addr
, val
);
531 static uint32_t pcirmv_read(void *opaque
, uint32_t addr
)
533 PIIX4PMState
*s
= opaque
;
535 return s
->pci0_hotplug_enable
;
538 static void pcirmv_write(void *opaque
, uint32_t addr
, uint32_t val
)
543 extern const char *global_cpu_model
;
545 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
,
546 PCIHotplugState state
);
548 static void piix4_acpi_system_hot_add_init(PCIBus
*bus
, PIIX4PMState
*s
)
550 struct pci_status
*pci0_status
= &s
->pci0_status
;
551 int i
= 0, cpus
= smp_cpus
;
554 s
->gpe_cpu
.cpus_sts
[i
++] = (cpus
< 8) ? (1 << cpus
) - 1 : 0xff;
558 register_ioport_write(GPE_BASE
, GPE_LEN
, 1, gpe_writeb
, s
);
559 register_ioport_read(GPE_BASE
, GPE_LEN
, 1, gpe_readb
, s
);
560 acpi_gpe_blk(&s
->gpe
, GPE_BASE
);
562 register_ioport_write(PROC_BASE
, 32, 1, gpe_writeb
, s
);
563 register_ioport_read(PROC_BASE
, 32, 1, gpe_readb
, s
);
565 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, pci0_status
);
566 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, pci0_status
);
568 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, bus
);
569 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, bus
);
571 register_ioport_write(PCI_RMV_BASE
, 4, 4, pcirmv_write
, s
);
572 register_ioport_read(PCI_RMV_BASE
, 4, 4, pcirmv_read
, s
);
574 pci_bus_hotplug(bus
, piix4_device_hotplug
, &s
->dev
.qdev
);
577 #if defined(TARGET_I386)
578 static void enable_processor(PIIX4PMState
*s
, int cpu
)
580 struct gpe_regs
*g
= &s
->gpe_cpu
;
581 ACPIGPE
*gpe
= &s
->gpe
;
583 *gpe
->sts
= *gpe
->sts
| PIIX4_CPU_HOTPLUG_STATUS
;
584 g
->cpus_sts
[cpu
/8] |= (1 << (cpu
%8));
587 static void disable_processor(PIIX4PMState
*s
, int cpu
)
589 struct gpe_regs
*g
= &s
->gpe_cpu
;
590 ACPIGPE
*gpe
= &s
->gpe
;
592 *gpe
->sts
= *gpe
->sts
| PIIX4_CPU_HOTPLUG_STATUS
;
593 g
->cpus_sts
[cpu
/8] &= ~(1 << (cpu
%8));
596 void qemu_system_cpu_hot_add(int cpu
, int state
)
599 PIIX4PMState
*s
= global_piix4_pm_state
;
601 if (state
&& !qemu_get_cpu(cpu
)) {
602 env
= pc_new_cpu(global_cpu_model
);
604 fprintf(stderr
, "cpu %d creation failed\n", cpu
);
607 env
->cpuid_apic_id
= cpu
;
611 enable_processor(s
, cpu
);
613 disable_processor(s
, cpu
);
619 static void enable_device(PIIX4PMState
*s
, int slot
)
621 s
->gpe
.sts
[0] |= PIIX4_PCI_HOTPLUG_STATUS
;
622 s
->pci0_status
.up
|= (1 << slot
);
625 static void disable_device(PIIX4PMState
*s
, int slot
)
627 s
->gpe
.sts
[0] |= PIIX4_PCI_HOTPLUG_STATUS
;
628 s
->pci0_status
.down
|= (1 << slot
);
631 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
,
632 PCIHotplugState state
)
634 int slot
= PCI_SLOT(dev
->devfn
);
635 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
,
638 /* Don't send event when device is enabled during qemu machine creation:
639 * it is present on boot, no hotplug event is necessary. We do send an
640 * event when the device is disabled later. */
641 if (state
== PCI_COLDPLUG_ENABLED
) {
645 s
->pci0_status
.up
= 0;
646 s
->pci0_status
.down
= 0;
647 if (state
== PCI_HOTPLUG_ENABLED
) {
648 enable_device(s
, slot
);
650 disable_device(s
, slot
);