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/>
31 # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
33 # define PIIX4_DPRINTF(format, ...) do { } while (0)
36 #define ACPI_DBG_IO_ADDR 0xb044
38 #define GPE_BASE 0xafe0
39 #define PROC_BASE 0xaf00
41 #define PCI_BASE 0xae00
42 #define PCI_EJ_BASE 0xae08
43 #define PCI_RMV_BASE 0xae0c
45 #define PIIX4_CPU_HOTPLUG_STATUS 4
46 #define PIIX4_PCI_HOTPLUG_STATUS 2
57 typedef struct PIIX4PMState
{
73 Notifier machine_ready
;
77 struct gpe_regs gpe_cpu
;
78 struct pci_status pci0_status
;
79 uint32_t pci0_hotplug_enable
;
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
->pm1a
, s
->tmr
.overflow_time
);
92 sci_level
= (((pmsts
& s
->pm1a
.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
->gpe
.sts
[0] & s
->gpe
.en
[0]) & PIIX4_PCI_HOTPLUG_STATUS
) != 0);
99 qemu_set_irq(s
->irq
, sci_level
);
100 /* schedule a timer interruption if needed */
101 acpi_pm_tmr_update(&s
->tmr
, (s
->pm1a
.en
& ACPI_BITMASK_TIMER_ENABLE
) &&
102 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
));
105 static void pm_tmr_timer(ACPIPMTimer
*tmr
)
107 PIIX4PMState
*s
= container_of(tmr
, PIIX4PMState
, tmr
);
111 static void pm_ioport_write(IORange
*ioport
, uint64_t addr
, unsigned width
,
114 PIIX4PMState
*s
= container_of(ioport
, PIIX4PMState
, ioport
);
117 PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
118 (unsigned)addr
, width
, (unsigned)val
);
123 acpi_pm1_evt_write_sts(&s
->pm1a
, &s
->tmr
, val
);
131 acpi_pm1_cnt_write(&s
->pm1a
, &s
->pm1_cnt
, val
);
136 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr
,
140 static void pm_ioport_read(IORange
*ioport
, uint64_t addr
, unsigned width
,
143 PIIX4PMState
*s
= container_of(ioport
, PIIX4PMState
, ioport
);
148 val
= acpi_pm1_evt_get_sts(&s
->pm1a
, s
->tmr
.overflow_time
);
154 val
= s
->pm1_cnt
.cnt
;
157 val
= acpi_pm_tmr_get(&s
->tmr
);
163 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr
, val
);
167 static const IORangeOps pm_iorange_ops
= {
168 .read
= pm_ioport_read
,
169 .write
= pm_ioport_write
,
172 static void apm_ctrl_changed(uint32_t val
, void *arg
)
174 PIIX4PMState
*s
= arg
;
176 /* ACPI specs 3.0, 4.7.2.5 */
177 acpi_pm1_cnt_update(&s
->pm1_cnt
, val
== ACPI_ENABLE
, val
== ACPI_DISABLE
);
179 if (s
->dev
.config
[0x5b] & (1 << 1)) {
181 qemu_irq_raise(s
->smi_irq
);
186 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
188 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val
);
191 static void pm_io_space_update(PIIX4PMState
*s
)
195 if (s
->dev
.config
[0x80] & 1) {
196 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
197 pm_io_base
&= 0xffc0;
199 /* XXX: need to improve memory and ioport allocation */
200 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base
);
201 iorange_init(&s
->ioport
, &pm_iorange_ops
, pm_io_base
, 64);
202 ioport_register(&s
->ioport
);
206 static void pm_write_config(PCIDevice
*d
,
207 uint32_t address
, uint32_t val
, int len
)
209 pci_default_write_config(d
, address
, val
, len
);
210 if (range_covers_byte(address
, len
, 0x80))
211 pm_io_space_update((PIIX4PMState
*)d
);
214 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
216 PIIX4PMState
*s
= opaque
;
218 pm_io_space_update(s
);
222 #define VMSTATE_GPE_ARRAY(_field, _state) \
224 .name = (stringify(_field)), \
226 .info = &vmstate_info_uint16, \
227 .size = sizeof(uint16_t), \
228 .flags = VMS_SINGLE | VMS_POINTER, \
229 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
232 static const VMStateDescription vmstate_gpe
= {
235 .minimum_version_id
= 1,
236 .minimum_version_id_old
= 1,
237 .fields
= (VMStateField
[]) {
238 VMSTATE_GPE_ARRAY(sts
, ACPIGPE
),
239 VMSTATE_GPE_ARRAY(en
, ACPIGPE
),
240 VMSTATE_END_OF_LIST()
244 static const VMStateDescription vmstate_pci_status
= {
245 .name
= "pci_status",
247 .minimum_version_id
= 1,
248 .minimum_version_id_old
= 1,
249 .fields
= (VMStateField
[]) {
250 VMSTATE_UINT32(up
, struct pci_status
),
251 VMSTATE_UINT32(down
, struct pci_status
),
252 VMSTATE_END_OF_LIST()
256 static const VMStateDescription vmstate_acpi
= {
259 .minimum_version_id
= 1,
260 .minimum_version_id_old
= 1,
261 .post_load
= vmstate_acpi_post_load
,
262 .fields
= (VMStateField
[]) {
263 VMSTATE_PCI_DEVICE(dev
, PIIX4PMState
),
264 VMSTATE_UINT16(pm1a
.sts
, PIIX4PMState
),
265 VMSTATE_UINT16(pm1a
.en
, PIIX4PMState
),
266 VMSTATE_UINT16(pm1_cnt
.cnt
, PIIX4PMState
),
267 VMSTATE_STRUCT(apm
, PIIX4PMState
, 0, vmstate_apm
, APMState
),
268 VMSTATE_TIMER(tmr
.timer
, PIIX4PMState
),
269 VMSTATE_INT64(tmr
.overflow_time
, PIIX4PMState
),
270 VMSTATE_STRUCT(gpe
, PIIX4PMState
, 2, vmstate_gpe
, ACPIGPE
),
271 VMSTATE_STRUCT(pci0_status
, PIIX4PMState
, 2, vmstate_pci_status
,
273 VMSTATE_END_OF_LIST()
277 static void piix4_update_hotplug(PIIX4PMState
*s
)
279 PCIDevice
*dev
= &s
->dev
;
280 BusState
*bus
= qdev_get_parent_bus(&dev
->qdev
);
281 DeviceState
*qdev
, *next
;
283 s
->pci0_hotplug_enable
= ~0;
285 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
286 PCIDeviceInfo
*info
= container_of(qdev
->info
, PCIDeviceInfo
, qdev
);
287 PCIDevice
*pdev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
288 int slot
= PCI_SLOT(pdev
->devfn
);
290 if (info
->no_hotplug
) {
291 s
->pci0_hotplug_enable
&= ~(1 << slot
);
296 static void piix4_reset(void *opaque
)
298 PIIX4PMState
*s
= opaque
;
299 uint8_t *pci_conf
= s
->dev
.config
;
306 if (s
->kvm_enabled
) {
307 /* Mark SMM as already inited (until KVM supports SMM). */
308 pci_conf
[0x5B] = 0x02;
310 piix4_update_hotplug(s
);
313 static void piix4_powerdown(void *opaque
, int irq
, int power_failing
)
315 PIIX4PMState
*s
= opaque
;
316 ACPIPM1EVT
*pm1a
= s
? &s
->pm1a
: NULL
;
317 ACPIPMTimer
*tmr
= s
? &s
->tmr
: NULL
;
319 acpi_pm1_evt_power_down(pm1a
, tmr
);
322 static void piix4_pm_machine_ready(Notifier
*n
, void *opaque
)
324 PIIX4PMState
*s
= container_of(n
, PIIX4PMState
, machine_ready
);
327 pci_conf
= s
->dev
.config
;
328 pci_conf
[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
329 pci_conf
[0x63] = 0x60;
330 pci_conf
[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
331 (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
335 static PIIX4PMState
*global_piix4_pm_state
; /* cpu hotadd */
337 static int piix4_pm_initfn(PCIDevice
*dev
)
339 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
343 global_piix4_pm_state
= s
;
345 pci_conf
= s
->dev
.config
;
346 pci_conf
[0x06] = 0x80;
347 pci_conf
[0x07] = 0x02;
348 pci_conf
[0x09] = 0x00;
349 pci_conf
[0x3d] = 0x01; // interrupt pin 1
351 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
353 #if defined(TARGET_IA64)
354 pci_conf
[0x40] = 0x41; /* PM io base read only bit */
355 pci_conf
[0x41] = 0x1f;
356 pm_write_config(s
, 0x80, 0x01, 1); /*Set default pm_io_base 0x1f40*/
361 apm_init(&s
->apm
, apm_ctrl_changed
, s
);
363 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
365 if (s
->kvm_enabled
) {
366 /* Mark SMM as already inited to prevent SMM from running. KVM does not
367 * support SMM mode. */
368 pci_conf
[0x5B] = 0x02;
371 /* XXX: which specification is used ? The i82731AB has different
373 pci_conf
[0x90] = s
->smb_io_base
| 1;
374 pci_conf
[0x91] = s
->smb_io_base
>> 8;
375 pci_conf
[0xd2] = 0x09;
376 register_ioport_write(s
->smb_io_base
, 64, 1, smb_ioport_writeb
, &s
->smb
);
377 register_ioport_read(s
->smb_io_base
, 64, 1, smb_ioport_readb
, &s
->smb
);
379 acpi_pm_tmr_init(&s
->tmr
, pm_tmr_timer
);
380 acpi_gpe_init(&s
->gpe
, GPE_LEN
);
382 qemu_system_powerdown
= *qemu_allocate_irqs(piix4_powerdown
, s
, 1);
384 pm_smbus_init(&s
->dev
.qdev
, &s
->smb
);
385 s
->machine_ready
.notify
= piix4_pm_machine_ready
;
386 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
387 qemu_register_reset(piix4_reset
, s
);
388 piix4_acpi_system_hot_add_init(dev
->bus
, s
);
393 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
394 qemu_irq sci_irq
, qemu_irq cmos_s3
, qemu_irq smi_irq
,
400 dev
= pci_create(bus
, devfn
, "PIIX4_PM");
401 qdev_prop_set_uint32(&dev
->qdev
, "smb_io_base", smb_io_base
);
403 s
= DO_UPCAST(PIIX4PMState
, dev
, dev
);
405 acpi_pm1_cnt_init(&s
->pm1_cnt
, cmos_s3
);
406 s
->smi_irq
= smi_irq
;
407 s
->kvm_enabled
= kvm_enabled
;
409 qdev_init_nofail(&dev
->qdev
);
414 static PCIDeviceInfo piix4_pm_info
= {
415 .qdev
.name
= "PIIX4_PM",
417 .qdev
.size
= sizeof(PIIX4PMState
),
418 .qdev
.vmsd
= &vmstate_acpi
,
421 .init
= piix4_pm_initfn
,
422 .config_write
= pm_write_config
,
423 .vendor_id
= PCI_VENDOR_ID_INTEL
,
424 .device_id
= PCI_DEVICE_ID_INTEL_82371AB_3
,
426 .class_id
= PCI_CLASS_BRIDGE_OTHER
,
427 .qdev
.props
= (Property
[]) {
428 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState
, smb_io_base
, 0),
429 DEFINE_PROP_END_OF_LIST(),
433 static void piix4_pm_register(void)
435 pci_qdev_register(&piix4_pm_info
);
438 device_init(piix4_pm_register
);
440 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
442 PIIX4PMState
*s
= opaque
;
444 struct gpe_regs
*g
= &s
->gpe_cpu
;
447 case PROC_BASE
... PROC_BASE
+31:
448 val
= g
->cpus_sts
[addr
- PROC_BASE
];
451 val
= acpi_gpe_ioport_readb(&s
->gpe
, addr
);
454 PIIX4_DPRINTF("gpe read %x == %x\n", addr
, val
);
458 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
460 PIIX4PMState
*s
= opaque
;
462 acpi_gpe_ioport_writeb(&s
->gpe
, addr
, val
);
465 PIIX4_DPRINTF("gpe write %x <== %d\n", addr
, val
);
468 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
471 struct pci_status
*g
= opaque
;
483 PIIX4_DPRINTF("pcihotplug read %x == %x\n", addr
, val
);
487 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
489 struct pci_status
*g
= opaque
;
499 PIIX4_DPRINTF("pcihotplug write %x <== %d\n", addr
, val
);
502 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
504 PIIX4_DPRINTF("pciej read %x\n", addr
);
508 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
510 BusState
*bus
= opaque
;
511 DeviceState
*qdev
, *next
;
514 int slot
= ffs(val
) - 1;
516 QLIST_FOREACH_SAFE(qdev
, &bus
->children
, sibling
, next
) {
517 dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
518 info
= container_of(qdev
->info
, PCIDeviceInfo
, qdev
);
519 if (PCI_SLOT(dev
->devfn
) == slot
&& !info
->no_hotplug
) {
525 PIIX4_DPRINTF("pciej write %x <== %d\n", addr
, val
);
528 static uint32_t pcirmv_read(void *opaque
, uint32_t addr
)
530 PIIX4PMState
*s
= opaque
;
532 return s
->pci0_hotplug_enable
;
535 static void pcirmv_write(void *opaque
, uint32_t addr
, uint32_t val
)
540 extern const char *global_cpu_model
;
542 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
,
543 PCIHotplugState state
);
545 static void piix4_acpi_system_hot_add_init(PCIBus
*bus
, PIIX4PMState
*s
)
547 struct pci_status
*pci0_status
= &s
->pci0_status
;
548 int i
= 0, cpus
= smp_cpus
;
551 s
->gpe_cpu
.cpus_sts
[i
++] = (cpus
< 8) ? (1 << cpus
) - 1 : 0xff;
555 register_ioport_write(GPE_BASE
, GPE_LEN
, 1, gpe_writeb
, s
);
556 register_ioport_read(GPE_BASE
, GPE_LEN
, 1, gpe_readb
, s
);
557 acpi_gpe_blk(&s
->gpe
, GPE_BASE
);
559 register_ioport_write(PROC_BASE
, 32, 1, gpe_writeb
, s
);
560 register_ioport_read(PROC_BASE
, 32, 1, gpe_readb
, s
);
562 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, pci0_status
);
563 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, pci0_status
);
565 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, bus
);
566 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, bus
);
568 register_ioport_write(PCI_RMV_BASE
, 4, 4, pcirmv_write
, s
);
569 register_ioport_read(PCI_RMV_BASE
, 4, 4, pcirmv_read
, s
);
571 pci_bus_hotplug(bus
, piix4_device_hotplug
, &s
->dev
.qdev
);
574 #if defined(TARGET_I386)
575 static void enable_processor(PIIX4PMState
*s
, int cpu
)
577 struct gpe_regs
*g
= &s
->gpe_cpu
;
578 ACPIGPE
*gpe
= &s
->gpe
;
580 *gpe
->sts
= *gpe
->sts
| PIIX4_CPU_HOTPLUG_STATUS
;
581 g
->cpus_sts
[cpu
/8] |= (1 << (cpu
%8));
584 static void disable_processor(PIIX4PMState
*s
, int cpu
)
586 struct gpe_regs
*g
= &s
->gpe_cpu
;
587 ACPIGPE
*gpe
= &s
->gpe
;
589 *gpe
->sts
= *gpe
->sts
| PIIX4_CPU_HOTPLUG_STATUS
;
590 g
->cpus_sts
[cpu
/8] &= ~(1 << (cpu
%8));
593 void qemu_system_cpu_hot_add(int cpu
, int state
)
596 PIIX4PMState
*s
= global_piix4_pm_state
;
598 if (state
&& !qemu_get_cpu(cpu
)) {
599 env
= pc_new_cpu(global_cpu_model
);
601 fprintf(stderr
, "cpu %d creation failed\n", cpu
);
604 env
->cpuid_apic_id
= cpu
;
608 enable_processor(s
, cpu
);
610 disable_processor(s
, cpu
);
616 static void enable_device(PIIX4PMState
*s
, int slot
)
618 s
->gpe
.sts
[0] |= PIIX4_PCI_HOTPLUG_STATUS
;
619 s
->pci0_status
.up
|= (1 << slot
);
622 static void disable_device(PIIX4PMState
*s
, int slot
)
624 s
->gpe
.sts
[0] |= PIIX4_PCI_HOTPLUG_STATUS
;
625 s
->pci0_status
.down
|= (1 << slot
);
628 static int piix4_device_hotplug(DeviceState
*qdev
, PCIDevice
*dev
,
629 PCIHotplugState state
)
631 int slot
= PCI_SLOT(dev
->devfn
);
632 PIIX4PMState
*s
= DO_UPCAST(PIIX4PMState
, dev
,
633 DO_UPCAST(PCIDevice
, qdev
, qdev
));
635 /* Don't send event when device is enabled during qemu machine creation:
636 * it is present on boot, no hotplug event is necessary. We do send an
637 * event when the device is disabled later. */
638 if (state
== PCI_COLDPLUG_ENABLED
) {
642 s
->pci0_status
.up
= 0;
643 s
->pci0_status
.down
= 0;
644 if (state
== PCI_HOTPLUG_ENABLED
) {
645 enable_device(s
, slot
);
647 disable_device(s
, slot
);