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