]> git.proxmox.com Git - mirror_qemu.git/blame - hw/acpi_piix4.c
qdev: Connect busses with their parent devices
[mirror_qemu.git] / hw / acpi_piix4.c
CommitLineData
93d89f63
IY
1/*
2 * ACPI implementation
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
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.
9 *
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.
14 *
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/>
6b620ca3
PB
17 *
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.
93d89f63
IY
20 */
21#include "hw.h"
22#include "pc.h"
23#include "apm.h"
24#include "pm_smbus.h"
25#include "pci.h"
93d89f63 26#include "acpi.h"
666daa68 27#include "sysemu.h"
bf1b0071 28#include "range.h"
6141dbfe 29#include "ioport.h"
93d89f63
IY
30
31//#define DEBUG
32
50d8ff8b
IY
33#ifdef DEBUG
34# define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
35#else
36# define PIIX4_DPRINTF(format, ...) do { } while (0)
37#endif
38
93d89f63
IY
39#define ACPI_DBG_IO_ADDR 0xb044
40
ac404095 41#define GPE_BASE 0xafe0
23910d3f 42#define GPE_LEN 4
ba737541
AW
43#define PCI_UP_BASE 0xae00
44#define PCI_DOWN_BASE 0xae04
ac404095 45#define PCI_EJ_BASE 0xae08
668643b0 46#define PCI_RMV_BASE 0xae0c
ac404095 47
4441a287
GN
48#define PIIX4_PCI_HOTPLUG_STATUS 2
49
ac404095 50struct pci_status {
7faa8075 51 uint32_t up; /* deprecated, maintained for migration compatibility */
ac404095
IY
52 uint32_t down;
53};
54
93d89f63
IY
55typedef struct PIIX4PMState {
56 PCIDevice dev;
2871a3f6 57 IORange ioport;
355bf2e5 58 ACPIREGS ar;
93d89f63
IY
59
60 APMState apm;
61
93d89f63 62 PMSMBus smb;
e8ec0571 63 uint32_t smb_io_base;
93d89f63
IY
64
65 qemu_irq irq;
93d89f63
IY
66 qemu_irq smi_irq;
67 int kvm_enabled;
6141dbfe 68 Notifier machine_ready;
ac404095
IY
69
70 /* for pci hotplug */
ac404095 71 struct pci_status pci0_status;
668643b0 72 uint32_t pci0_hotplug_enable;
7faa8075 73 uint32_t pci0_slot_device_present;
93d89f63
IY
74} PIIX4PMState;
75
ac404095
IY
76static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s);
77
93d89f63
IY
78#define ACPI_ENABLE 0xf1
79#define ACPI_DISABLE 0xf0
80
93d89f63
IY
81static void pm_update_sci(PIIX4PMState *s)
82{
83 int sci_level, pmsts;
93d89f63 84
2886be1b 85 pmsts = acpi_pm1_evt_get_sts(&s->ar);
355bf2e5 86 sci_level = (((pmsts & s->ar.pm1.evt.en) &
93d89f63
IY
87 (ACPI_BITMASK_RT_CLOCK_ENABLE |
88 ACPI_BITMASK_POWER_BUTTON_ENABLE |
89 ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
633aa0ac 90 ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
355bf2e5
GH
91 (((s->ar.gpe.sts[0] & s->ar.gpe.en[0])
92 & PIIX4_PCI_HOTPLUG_STATUS) != 0);
633aa0ac 93
93d89f63
IY
94 qemu_set_irq(s->irq, sci_level);
95 /* schedule a timer interruption if needed */
355bf2e5 96 acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
a54d41a8 97 !(pmsts & ACPI_BITMASK_TIMER_STATUS));
93d89f63
IY
98}
99
355bf2e5 100static void pm_tmr_timer(ACPIREGS *ar)
93d89f63 101{
355bf2e5 102 PIIX4PMState *s = container_of(ar, PIIX4PMState, ar);
93d89f63
IY
103 pm_update_sci(s);
104}
105
2871a3f6
AK
106static void pm_ioport_write(IORange *ioport, uint64_t addr, unsigned width,
107 uint64_t val)
93d89f63 108{
2871a3f6
AK
109 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
110
111 if (width != 2) {
112 PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
113 (unsigned)addr, width, (unsigned)val);
114 }
115
93d89f63
IY
116 switch(addr) {
117 case 0x00:
355bf2e5 118 acpi_pm1_evt_write_sts(&s->ar, val);
04dc308f 119 pm_update_sci(s);
93d89f63
IY
120 break;
121 case 0x02:
8283c4f5 122 acpi_pm1_evt_write_en(&s->ar, val);
93d89f63
IY
123 pm_update_sci(s);
124 break;
125 case 0x04:
355bf2e5 126 acpi_pm1_cnt_write(&s->ar, val);
93d89f63
IY
127 break;
128 default:
129 break;
130 }
59df4c11
WC
131 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr,
132 (unsigned int)val);
93d89f63
IY
133}
134
2871a3f6
AK
135static void pm_ioport_read(IORange *ioport, uint64_t addr, unsigned width,
136 uint64_t *data)
93d89f63 137{
2871a3f6 138 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
93d89f63
IY
139 uint32_t val;
140
93d89f63
IY
141 switch(addr) {
142 case 0x00:
2886be1b 143 val = acpi_pm1_evt_get_sts(&s->ar);
93d89f63
IY
144 break;
145 case 0x02:
355bf2e5 146 val = s->ar.pm1.evt.en;
93d89f63
IY
147 break;
148 case 0x04:
355bf2e5 149 val = s->ar.pm1.cnt.cnt;
93d89f63 150 break;
93d89f63 151 case 0x08:
355bf2e5 152 val = acpi_pm_tmr_get(&s->ar);
93d89f63
IY
153 break;
154 default:
155 val = 0;
156 break;
157 }
59df4c11 158 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr, val);
2871a3f6 159 *data = val;
93d89f63
IY
160}
161
2871a3f6
AK
162static const IORangeOps pm_iorange_ops = {
163 .read = pm_ioport_read,
164 .write = pm_ioport_write,
165};
166
93d89f63
IY
167static void apm_ctrl_changed(uint32_t val, void *arg)
168{
169 PIIX4PMState *s = arg;
170
171 /* ACPI specs 3.0, 4.7.2.5 */
355bf2e5 172 acpi_pm1_cnt_update(&s->ar, val == ACPI_ENABLE, val == ACPI_DISABLE);
93d89f63
IY
173
174 if (s->dev.config[0x5b] & (1 << 1)) {
175 if (s->smi_irq) {
176 qemu_irq_raise(s->smi_irq);
177 }
178 }
179}
180
181static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
182{
50d8ff8b 183 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val);
93d89f63
IY
184}
185
186static void pm_io_space_update(PIIX4PMState *s)
187{
188 uint32_t pm_io_base;
189
190 if (s->dev.config[0x80] & 1) {
191 pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
192 pm_io_base &= 0xffc0;
193
194 /* XXX: need to improve memory and ioport allocation */
50d8ff8b 195 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base);
2871a3f6
AK
196 iorange_init(&s->ioport, &pm_iorange_ops, pm_io_base, 64);
197 ioport_register(&s->ioport);
93d89f63
IY
198 }
199}
200
201static void pm_write_config(PCIDevice *d,
202 uint32_t address, uint32_t val, int len)
203{
204 pci_default_write_config(d, address, val, len);
205 if (range_covers_byte(address, len, 0x80))
206 pm_io_space_update((PIIX4PMState *)d);
207}
208
7faa8075
AW
209static void vmstate_pci_status_pre_save(void *opaque)
210{
211 struct pci_status *pci0_status = opaque;
212 PIIX4PMState *s = container_of(pci0_status, PIIX4PMState, pci0_status);
213
214 /* We no longer track up, so build a safe value for migrating
215 * to a version that still does... of course these might get lost
216 * by an old buggy implementation, but we try. */
217 pci0_status->up = s->pci0_slot_device_present & s->pci0_hotplug_enable;
218}
219
93d89f63
IY
220static int vmstate_acpi_post_load(void *opaque, int version_id)
221{
222 PIIX4PMState *s = opaque;
223
224 pm_io_space_update(s);
225 return 0;
226}
227
23910d3f
IY
228#define VMSTATE_GPE_ARRAY(_field, _state) \
229 { \
230 .name = (stringify(_field)), \
231 .version_id = 0, \
232 .num = GPE_LEN, \
233 .info = &vmstate_info_uint16, \
234 .size = sizeof(uint16_t), \
235 .flags = VMS_ARRAY | VMS_POINTER, \
236 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
237 }
238
4cf3e6f3
AW
239static const VMStateDescription vmstate_gpe = {
240 .name = "gpe",
241 .version_id = 1,
242 .minimum_version_id = 1,
243 .minimum_version_id_old = 1,
244 .fields = (VMStateField []) {
23910d3f
IY
245 VMSTATE_GPE_ARRAY(sts, ACPIGPE),
246 VMSTATE_GPE_ARRAY(en, ACPIGPE),
4cf3e6f3
AW
247 VMSTATE_END_OF_LIST()
248 }
249};
250
251static const VMStateDescription vmstate_pci_status = {
252 .name = "pci_status",
253 .version_id = 1,
254 .minimum_version_id = 1,
255 .minimum_version_id_old = 1,
7faa8075 256 .pre_save = vmstate_pci_status_pre_save,
4cf3e6f3
AW
257 .fields = (VMStateField []) {
258 VMSTATE_UINT32(up, struct pci_status),
259 VMSTATE_UINT32(down, struct pci_status),
260 VMSTATE_END_OF_LIST()
261 }
262};
263
93d89f63
IY
264static const VMStateDescription vmstate_acpi = {
265 .name = "piix4_pm",
4cf3e6f3 266 .version_id = 2,
93d89f63
IY
267 .minimum_version_id = 1,
268 .minimum_version_id_old = 1,
269 .post_load = vmstate_acpi_post_load,
270 .fields = (VMStateField []) {
271 VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
355bf2e5
GH
272 VMSTATE_UINT16(ar.pm1.evt.sts, PIIX4PMState),
273 VMSTATE_UINT16(ar.pm1.evt.en, PIIX4PMState),
274 VMSTATE_UINT16(ar.pm1.cnt.cnt, PIIX4PMState),
93d89f63 275 VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
355bf2e5
GH
276 VMSTATE_TIMER(ar.tmr.timer, PIIX4PMState),
277 VMSTATE_INT64(ar.tmr.overflow_time, PIIX4PMState),
278 VMSTATE_STRUCT(ar.gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
4cf3e6f3
AW
279 VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
280 struct pci_status),
93d89f63
IY
281 VMSTATE_END_OF_LIST()
282 }
283};
284
7faa8075
AW
285static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots)
286{
287 DeviceState *qdev, *next;
288 BusState *bus = qdev_get_parent_bus(&s->dev.qdev);
289 int slot = ffs(slots) - 1;
54bfa546 290 bool slot_free = true;
7faa8075
AW
291
292 /* Mark request as complete */
293 s->pci0_status.down &= ~(1U << slot);
294
295 QTAILQ_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
296 PCIDevice *dev = PCI_DEVICE(qdev);
297 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
54bfa546
MT
298 if (PCI_SLOT(dev->devfn) == slot) {
299 if (pc->no_hotplug) {
300 slot_free = false;
301 } else {
a6de8ed8 302 object_unparent(OBJECT(dev));
54bfa546
MT
303 qdev_free(qdev);
304 }
7faa8075
AW
305 }
306 }
54bfa546
MT
307 if (slot_free) {
308 s->pci0_slot_device_present &= ~(1U << slot);
309 }
7faa8075
AW
310}
311
668643b0
MT
312static void piix4_update_hotplug(PIIX4PMState *s)
313{
314 PCIDevice *dev = &s->dev;
315 BusState *bus = qdev_get_parent_bus(&dev->qdev);
316 DeviceState *qdev, *next;
317
7faa8075
AW
318 /* Execute any pending removes during reset */
319 while (s->pci0_status.down) {
320 acpi_piix_eject_slot(s, s->pci0_status.down);
321 }
322
668643b0 323 s->pci0_hotplug_enable = ~0;
7faa8075 324 s->pci0_slot_device_present = 0;
668643b0 325
d8bb00d6 326 QTAILQ_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
40021f08
AL
327 PCIDevice *pdev = PCI_DEVICE(qdev);
328 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev);
668643b0
MT
329 int slot = PCI_SLOT(pdev->devfn);
330
40021f08 331 if (pc->no_hotplug) {
7faa8075 332 s->pci0_hotplug_enable &= ~(1U << slot);
668643b0 333 }
7faa8075
AW
334
335 s->pci0_slot_device_present |= (1U << slot);
668643b0
MT
336 }
337}
338
93d89f63
IY
339static void piix4_reset(void *opaque)
340{
341 PIIX4PMState *s = opaque;
342 uint8_t *pci_conf = s->dev.config;
343
344 pci_conf[0x58] = 0;
345 pci_conf[0x59] = 0;
346 pci_conf[0x5a] = 0;
347 pci_conf[0x5b] = 0;
348
349 if (s->kvm_enabled) {
350 /* Mark SMM as already inited (until KVM supports SMM). */
351 pci_conf[0x5B] = 0x02;
352 }
668643b0 353 piix4_update_hotplug(s);
93d89f63
IY
354}
355
356static void piix4_powerdown(void *opaque, int irq, int power_failing)
357{
358 PIIX4PMState *s = opaque;
359
355bf2e5
GH
360 assert(s != NULL);
361 acpi_pm1_evt_power_down(&s->ar);
93d89f63
IY
362}
363
9e8dd451 364static void piix4_pm_machine_ready(Notifier *n, void *opaque)
6141dbfe
PB
365{
366 PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
367 uint8_t *pci_conf;
368
369 pci_conf = s->dev.config;
370 pci_conf[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
371 pci_conf[0x63] = 0x60;
372 pci_conf[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
373 (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
374
375}
376
e8ec0571 377static int piix4_pm_initfn(PCIDevice *dev)
93d89f63 378{
e8ec0571 379 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev);
93d89f63
IY
380 uint8_t *pci_conf;
381
93d89f63 382 pci_conf = s->dev.config;
93d89f63
IY
383 pci_conf[0x06] = 0x80;
384 pci_conf[0x07] = 0x02;
93d89f63 385 pci_conf[0x09] = 0x00;
93d89f63
IY
386 pci_conf[0x3d] = 0x01; // interrupt pin 1
387
388 pci_conf[0x40] = 0x01; /* PM io base read only bit */
389
390 /* APM */
391 apm_init(&s->apm, apm_ctrl_changed, s);
392
393 register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
394
93d89f63
IY
395 if (s->kvm_enabled) {
396 /* Mark SMM as already inited to prevent SMM from running. KVM does not
397 * support SMM mode. */
398 pci_conf[0x5B] = 0x02;
399 }
400
401 /* XXX: which specification is used ? The i82731AB has different
402 mappings */
e8ec0571
IY
403 pci_conf[0x90] = s->smb_io_base | 1;
404 pci_conf[0x91] = s->smb_io_base >> 8;
93d89f63 405 pci_conf[0xd2] = 0x09;
e8ec0571
IY
406 register_ioport_write(s->smb_io_base, 64, 1, smb_ioport_writeb, &s->smb);
407 register_ioport_read(s->smb_io_base, 64, 1, smb_ioport_readb, &s->smb);
93d89f63 408
355bf2e5
GH
409 acpi_pm_tmr_init(&s->ar, pm_tmr_timer);
410 acpi_gpe_init(&s->ar, GPE_LEN);
93d89f63
IY
411
412 qemu_system_powerdown = *qemu_allocate_irqs(piix4_powerdown, s, 1);
413
e8ec0571 414 pm_smbus_init(&s->dev.qdev, &s->smb);
6141dbfe
PB
415 s->machine_ready.notify = piix4_pm_machine_ready;
416 qemu_add_machine_init_done_notifier(&s->machine_ready);
e8ec0571 417 qemu_register_reset(piix4_reset, s);
ac404095 418 piix4_acpi_system_hot_add_init(dev->bus, s);
e8ec0571
IY
419
420 return 0;
421}
422
423i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
da98c8eb 424 qemu_irq sci_irq, qemu_irq smi_irq,
e8ec0571
IY
425 int kvm_enabled)
426{
427 PCIDevice *dev;
428 PIIX4PMState *s;
429
430 dev = pci_create(bus, devfn, "PIIX4_PM");
431 qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
93d89f63 432
e8ec0571 433 s = DO_UPCAST(PIIX4PMState, dev, dev);
93d89f63 434 s->irq = sci_irq;
da98c8eb 435 acpi_pm1_cnt_init(&s->ar);
93d89f63 436 s->smi_irq = smi_irq;
e8ec0571
IY
437 s->kvm_enabled = kvm_enabled;
438
439 qdev_init_nofail(&dev->qdev);
93d89f63
IY
440
441 return s->smb.smbus;
442}
443
40021f08
AL
444static Property piix4_pm_properties[] = {
445 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
446 DEFINE_PROP_END_OF_LIST(),
447};
448
449static void piix4_pm_class_init(ObjectClass *klass, void *data)
450{
39bffca2 451 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
452 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
453
454 k->no_hotplug = 1;
455 k->init = piix4_pm_initfn;
456 k->config_write = pm_write_config;
457 k->vendor_id = PCI_VENDOR_ID_INTEL;
458 k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3;
459 k->revision = 0x03;
460 k->class_id = PCI_CLASS_BRIDGE_OTHER;
39bffca2
AL
461 dc->desc = "PM";
462 dc->no_user = 1;
463 dc->vmsd = &vmstate_acpi;
464 dc->props = piix4_pm_properties;
40021f08
AL
465}
466
39bffca2
AL
467static TypeInfo piix4_pm_info = {
468 .name = "PIIX4_PM",
469 .parent = TYPE_PCI_DEVICE,
470 .instance_size = sizeof(PIIX4PMState),
471 .class_init = piix4_pm_class_init,
e8ec0571
IY
472};
473
83f7d43a 474static void piix4_pm_register_types(void)
e8ec0571 475{
39bffca2 476 type_register_static(&piix4_pm_info);
e8ec0571
IY
477}
478
83f7d43a 479type_init(piix4_pm_register_types)
e8ec0571 480
93d89f63
IY
481static uint32_t gpe_readb(void *opaque, uint32_t addr)
482{
633aa0ac 483 PIIX4PMState *s = opaque;
355bf2e5 484 uint32_t val = acpi_gpe_ioport_readb(&s->ar, addr);
93d89f63 485
50d8ff8b 486 PIIX4_DPRINTF("gpe read %x == %x\n", addr, val);
93d89f63
IY
487 return val;
488}
489
93d89f63
IY
490static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
491{
633aa0ac 492 PIIX4PMState *s = opaque;
633aa0ac 493
355bf2e5 494 acpi_gpe_ioport_writeb(&s->ar, addr, val);
633aa0ac 495 pm_update_sci(s);
93d89f63 496
50d8ff8b 497 PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val);
93d89f63
IY
498}
499
ba737541 500static uint32_t pci_up_read(void *opaque, uint32_t addr)
93d89f63 501{
ba737541 502 PIIX4PMState *s = opaque;
7faa8075
AW
503 uint32_t val;
504
505 /* Manufacture an "up" value to cause a device check on any hotplug
506 * slot with a device. Extra device checks are harmless. */
507 val = s->pci0_slot_device_present & s->pci0_hotplug_enable;
93d89f63 508
ba737541 509 PIIX4_DPRINTF("pci_up_read %x\n", val);
93d89f63
IY
510 return val;
511}
512
ba737541 513static uint32_t pci_down_read(void *opaque, uint32_t addr)
93d89f63 514{
ba737541
AW
515 PIIX4PMState *s = opaque;
516 uint32_t val = s->pci0_status.down;
517
518 PIIX4_DPRINTF("pci_down_read %x\n", val);
519 return val;
93d89f63
IY
520}
521
9290f364 522static uint32_t pci_features_read(void *opaque, uint32_t addr)
93d89f63 523{
9290f364
AW
524 /* No feature defined yet */
525 PIIX4_DPRINTF("pci_features_read %x\n", 0);
93d89f63
IY
526 return 0;
527}
528
529static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
530{
7faa8075 531 acpi_piix_eject_slot(opaque, val);
93d89f63 532
50d8ff8b 533 PIIX4_DPRINTF("pciej write %x <== %d\n", addr, val);
93d89f63
IY
534}
535
668643b0
MT
536static uint32_t pcirmv_read(void *opaque, uint32_t addr)
537{
538 PIIX4PMState *s = opaque;
539
540 return s->pci0_hotplug_enable;
541}
542
4cff0a59
MT
543static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
544 PCIHotplugState state);
93d89f63 545
ac404095 546static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s)
93d89f63 547{
93d89f63 548
23910d3f
IY
549 register_ioport_write(GPE_BASE, GPE_LEN, 1, gpe_writeb, s);
550 register_ioport_read(GPE_BASE, GPE_LEN, 1, gpe_readb, s);
355bf2e5 551 acpi_gpe_blk(&s->ar, GPE_BASE);
ac404095 552
ba737541
AW
553 register_ioport_read(PCI_UP_BASE, 4, 4, pci_up_read, s);
554 register_ioport_read(PCI_DOWN_BASE, 4, 4, pci_down_read, s);
93d89f63 555
7faa8075 556 register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, s);
9290f364 557 register_ioport_read(PCI_EJ_BASE, 4, 4, pci_features_read, s);
93d89f63 558
668643b0
MT
559 register_ioport_read(PCI_RMV_BASE, 4, 4, pcirmv_read, s);
560
ac404095 561 pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev);
93d89f63
IY
562}
563
ac404095 564static void enable_device(PIIX4PMState *s, int slot)
93d89f63 565{
355bf2e5 566 s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
7faa8075 567 s->pci0_slot_device_present |= (1U << slot);
93d89f63
IY
568}
569
ac404095 570static void disable_device(PIIX4PMState *s, int slot)
93d89f63 571{
355bf2e5 572 s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
7faa8075 573 s->pci0_status.down |= (1U << slot);
93d89f63
IY
574}
575
4cff0a59
MT
576static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
577 PCIHotplugState state)
93d89f63
IY
578{
579 int slot = PCI_SLOT(dev->devfn);
ac404095 580 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
40021f08 581 PCI_DEVICE(qdev));
93d89f63 582
4cff0a59
MT
583 /* Don't send event when device is enabled during qemu machine creation:
584 * it is present on boot, no hotplug event is necessary. We do send an
585 * event when the device is disabled later. */
586 if (state == PCI_COLDPLUG_ENABLED) {
7faa8075 587 s->pci0_slot_device_present |= (1U << slot);
5beb8ad5 588 return 0;
4cff0a59 589 }
5beb8ad5 590
4cff0a59 591 if (state == PCI_HOTPLUG_ENABLED) {
ac404095
IY
592 enable_device(s, slot);
593 } else {
594 disable_device(s, slot);
595 }
633aa0ac
GN
596
597 pm_update_sci(s);
598
93d89f63
IY
599 return 0;
600}