]> git.proxmox.com Git - qemu.git/blame - hw/acpi/piix4.c
virtio-net: broken RX filtering logic fixed
[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 20 */
83c9f4ca 21#include "hw/hw.h"
0d09e41a
PB
22#include "hw/i386/pc.h"
23#include "hw/isa/apm.h"
24#include "hw/i2c/pm_smbus.h"
83c9f4ca 25#include "hw/pci/pci.h"
0d09e41a 26#include "hw/acpi/acpi.h"
9c17d615 27#include "sysemu/sysemu.h"
1de7afc9 28#include "qemu/range.h"
022c62cb 29#include "exec/ioport.h"
0d09e41a 30#include "hw/nvram/fw_cfg.h"
022c62cb 31#include "exec/address-spaces.h"
277e9340 32#include "hw/acpi/piix4.h"
93d89f63
IY
33
34//#define DEBUG
35
50d8ff8b
IY
36#ifdef DEBUG
37# define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
38#else
39# define PIIX4_DPRINTF(format, ...) do { } while (0)
40#endif
41
ac404095 42#define GPE_BASE 0xafe0
23910d3f 43#define GPE_LEN 4
c177684c
GH
44
45#define PCI_HOTPLUG_ADDR 0xae00
46#define PCI_HOTPLUG_SIZE 0x000f
ba737541
AW
47#define PCI_UP_BASE 0xae00
48#define PCI_DOWN_BASE 0xae04
ac404095 49#define PCI_EJ_BASE 0xae08
668643b0 50#define PCI_RMV_BASE 0xae0c
ac404095 51
b8622725
IM
52#define PIIX4_PROC_BASE 0xaf00
53#define PIIX4_PROC_LEN 32
54
4441a287 55#define PIIX4_PCI_HOTPLUG_STATUS 2
b8622725 56#define PIIX4_CPU_HOTPLUG_STATUS 4
4441a287 57
ac404095 58struct pci_status {
7faa8075 59 uint32_t up; /* deprecated, maintained for migration compatibility */
ac404095
IY
60 uint32_t down;
61};
62
b8622725
IM
63typedef struct CPUStatus {
64 uint8_t sts[PIIX4_PROC_LEN];
65} CPUStatus;
66
93d89f63 67typedef struct PIIX4PMState {
6a6b5580
AF
68 /*< private >*/
69 PCIDevice parent_obj;
70 /*< public >*/
56e5b2a1 71
af11110b 72 MemoryRegion io;
277e9340
MT
73 uint32_t io_base;
74
b65b93f2 75 MemoryRegion io_gpe;
c177684c 76 MemoryRegion io_pci;
b8622725 77 MemoryRegion io_cpu;
355bf2e5 78 ACPIREGS ar;
93d89f63
IY
79
80 APMState apm;
81
93d89f63 82 PMSMBus smb;
e8ec0571 83 uint32_t smb_io_base;
93d89f63
IY
84
85 qemu_irq irq;
93d89f63
IY
86 qemu_irq smi_irq;
87 int kvm_enabled;
6141dbfe 88 Notifier machine_ready;
d010f91c 89 Notifier powerdown_notifier;
ac404095
IY
90
91 /* for pci hotplug */
ac404095 92 struct pci_status pci0_status;
668643b0 93 uint32_t pci0_hotplug_enable;
7faa8075 94 uint32_t pci0_slot_device_present;
459ae5ea
GN
95
96 uint8_t disable_s3;
97 uint8_t disable_s4;
98 uint8_t s4_val;
b8622725
IM
99
100 CPUStatus gpe_cpu;
101 Notifier cpu_added_notifier;
93d89f63
IY
102} PIIX4PMState;
103
74e445f6
PC
104#define TYPE_PIIX4_PM "PIIX4_PM"
105
106#define PIIX4_PM(obj) \
107 OBJECT_CHECK(PIIX4PMState, (obj), TYPE_PIIX4_PM)
108
56e5b2a1
GH
109static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
110 PCIBus *bus, PIIX4PMState *s);
ac404095 111
93d89f63
IY
112#define ACPI_ENABLE 0xf1
113#define ACPI_DISABLE 0xf0
114
93d89f63
IY
115static void pm_update_sci(PIIX4PMState *s)
116{
117 int sci_level, pmsts;
93d89f63 118
2886be1b 119 pmsts = acpi_pm1_evt_get_sts(&s->ar);
355bf2e5 120 sci_level = (((pmsts & s->ar.pm1.evt.en) &
93d89f63
IY
121 (ACPI_BITMASK_RT_CLOCK_ENABLE |
122 ACPI_BITMASK_POWER_BUTTON_ENABLE |
123 ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
633aa0ac 124 ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
b8622725
IM
125 (((s->ar.gpe.sts[0] & s->ar.gpe.en[0]) &
126 (PIIX4_PCI_HOTPLUG_STATUS | PIIX4_CPU_HOTPLUG_STATUS)) != 0);
633aa0ac 127
93d89f63
IY
128 qemu_set_irq(s->irq, sci_level);
129 /* schedule a timer interruption if needed */
355bf2e5 130 acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
a54d41a8 131 !(pmsts & ACPI_BITMASK_TIMER_STATUS));
93d89f63
IY
132}
133
355bf2e5 134static void pm_tmr_timer(ACPIREGS *ar)
93d89f63 135{
355bf2e5 136 PIIX4PMState *s = container_of(ar, PIIX4PMState, ar);
93d89f63
IY
137 pm_update_sci(s);
138}
139
93d89f63
IY
140static void apm_ctrl_changed(uint32_t val, void *arg)
141{
142 PIIX4PMState *s = arg;
6a6b5580 143 PCIDevice *d = PCI_DEVICE(s);
93d89f63
IY
144
145 /* ACPI specs 3.0, 4.7.2.5 */
355bf2e5 146 acpi_pm1_cnt_update(&s->ar, val == ACPI_ENABLE, val == ACPI_DISABLE);
93d89f63 147
6a6b5580 148 if (d->config[0x5b] & (1 << 1)) {
93d89f63
IY
149 if (s->smi_irq) {
150 qemu_irq_raise(s->smi_irq);
151 }
152 }
153}
154
93d89f63
IY
155static void pm_io_space_update(PIIX4PMState *s)
156{
6a6b5580 157 PCIDevice *d = PCI_DEVICE(s);
93d89f63 158
277e9340
MT
159 s->io_base = le32_to_cpu(*(uint32_t *)(d->config + 0x40));
160 s->io_base &= 0xffc0;
93d89f63 161
af11110b 162 memory_region_transaction_begin();
6a6b5580 163 memory_region_set_enabled(&s->io, d->config[0x80] & 1);
277e9340 164 memory_region_set_address(&s->io, s->io_base);
af11110b 165 memory_region_transaction_commit();
93d89f63
IY
166}
167
24fe083d
GH
168static void smbus_io_space_update(PIIX4PMState *s)
169{
6a6b5580
AF
170 PCIDevice *d = PCI_DEVICE(s);
171
172 s->smb_io_base = le32_to_cpu(*(uint32_t *)(d->config + 0x90));
24fe083d
GH
173 s->smb_io_base &= 0xffc0;
174
175 memory_region_transaction_begin();
6a6b5580 176 memory_region_set_enabled(&s->smb.io, d->config[0xd2] & 1);
24fe083d
GH
177 memory_region_set_address(&s->smb.io, s->smb_io_base);
178 memory_region_transaction_commit();
93d89f63
IY
179}
180
181static void pm_write_config(PCIDevice *d,
182 uint32_t address, uint32_t val, int len)
183{
184 pci_default_write_config(d, address, val, len);
24fe083d
GH
185 if (range_covers_byte(address, len, 0x80) ||
186 ranges_overlap(address, len, 0x40, 4)) {
93d89f63 187 pm_io_space_update((PIIX4PMState *)d);
24fe083d
GH
188 }
189 if (range_covers_byte(address, len, 0xd2) ||
190 ranges_overlap(address, len, 0x90, 4)) {
191 smbus_io_space_update((PIIX4PMState *)d);
192 }
93d89f63
IY
193}
194
7faa8075
AW
195static void vmstate_pci_status_pre_save(void *opaque)
196{
197 struct pci_status *pci0_status = opaque;
198 PIIX4PMState *s = container_of(pci0_status, PIIX4PMState, pci0_status);
199
200 /* We no longer track up, so build a safe value for migrating
201 * to a version that still does... of course these might get lost
202 * by an old buggy implementation, but we try. */
203 pci0_status->up = s->pci0_slot_device_present & s->pci0_hotplug_enable;
204}
205
93d89f63
IY
206static int vmstate_acpi_post_load(void *opaque, int version_id)
207{
208 PIIX4PMState *s = opaque;
209
210 pm_io_space_update(s);
211 return 0;
212}
213
23910d3f
IY
214#define VMSTATE_GPE_ARRAY(_field, _state) \
215 { \
216 .name = (stringify(_field)), \
217 .version_id = 0, \
23910d3f
IY
218 .info = &vmstate_info_uint16, \
219 .size = sizeof(uint16_t), \
b0b873a0 220 .flags = VMS_SINGLE | VMS_POINTER, \
23910d3f
IY
221 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
222 }
223
4cf3e6f3
AW
224static const VMStateDescription vmstate_gpe = {
225 .name = "gpe",
226 .version_id = 1,
227 .minimum_version_id = 1,
228 .minimum_version_id_old = 1,
229 .fields = (VMStateField []) {
23910d3f
IY
230 VMSTATE_GPE_ARRAY(sts, ACPIGPE),
231 VMSTATE_GPE_ARRAY(en, ACPIGPE),
4cf3e6f3
AW
232 VMSTATE_END_OF_LIST()
233 }
234};
235
236static const VMStateDescription vmstate_pci_status = {
237 .name = "pci_status",
238 .version_id = 1,
239 .minimum_version_id = 1,
240 .minimum_version_id_old = 1,
7faa8075 241 .pre_save = vmstate_pci_status_pre_save,
4cf3e6f3
AW
242 .fields = (VMStateField []) {
243 VMSTATE_UINT32(up, struct pci_status),
244 VMSTATE_UINT32(down, struct pci_status),
245 VMSTATE_END_OF_LIST()
246 }
247};
248
b0b873a0
MT
249static int acpi_load_old(QEMUFile *f, void *opaque, int version_id)
250{
251 PIIX4PMState *s = opaque;
252 int ret, i;
253 uint16_t temp;
254
6a6b5580 255 ret = pci_device_load(PCI_DEVICE(s), f);
b0b873a0
MT
256 if (ret < 0) {
257 return ret;
258 }
259 qemu_get_be16s(f, &s->ar.pm1.evt.sts);
260 qemu_get_be16s(f, &s->ar.pm1.evt.en);
261 qemu_get_be16s(f, &s->ar.pm1.cnt.cnt);
262
ded67782 263 ret = vmstate_load_state(f, &vmstate_apm, &s->apm, 1);
b0b873a0
MT
264 if (ret) {
265 return ret;
266 }
267
40daca54 268 timer_get(f, s->ar.tmr.timer);
b0b873a0
MT
269 qemu_get_sbe64s(f, &s->ar.tmr.overflow_time);
270
271 qemu_get_be16s(f, (uint16_t *)s->ar.gpe.sts);
272 for (i = 0; i < 3; i++) {
273 qemu_get_be16s(f, &temp);
274 }
275
276 qemu_get_be16s(f, (uint16_t *)s->ar.gpe.en);
277 for (i = 0; i < 3; i++) {
278 qemu_get_be16s(f, &temp);
279 }
280
ded67782 281 ret = vmstate_load_state(f, &vmstate_pci_status, &s->pci0_status, 1);
b0b873a0
MT
282 return ret;
283}
284
285/* qemu-kvm 1.2 uses version 3 but advertised as 2
286 * To support incoming qemu-kvm 1.2 migration, change version_id
287 * and minimum_version_id to 2 below (which breaks migration from
288 * qemu 1.2).
289 *
290 */
93d89f63
IY
291static const VMStateDescription vmstate_acpi = {
292 .name = "piix4_pm",
b0b873a0
MT
293 .version_id = 3,
294 .minimum_version_id = 3,
93d89f63 295 .minimum_version_id_old = 1,
b0b873a0 296 .load_state_old = acpi_load_old,
93d89f63
IY
297 .post_load = vmstate_acpi_post_load,
298 .fields = (VMStateField []) {
6a6b5580 299 VMSTATE_PCI_DEVICE(parent_obj, PIIX4PMState),
355bf2e5
GH
300 VMSTATE_UINT16(ar.pm1.evt.sts, PIIX4PMState),
301 VMSTATE_UINT16(ar.pm1.evt.en, PIIX4PMState),
302 VMSTATE_UINT16(ar.pm1.cnt.cnt, PIIX4PMState),
93d89f63 303 VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
355bf2e5
GH
304 VMSTATE_TIMER(ar.tmr.timer, PIIX4PMState),
305 VMSTATE_INT64(ar.tmr.overflow_time, PIIX4PMState),
306 VMSTATE_STRUCT(ar.gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
4cf3e6f3
AW
307 VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
308 struct pci_status),
93d89f63
IY
309 VMSTATE_END_OF_LIST()
310 }
311};
312
7faa8075
AW
313static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots)
314{
0866aca1 315 BusChild *kid, *next;
74e445f6 316 BusState *bus = qdev_get_parent_bus(DEVICE(s));
7faa8075 317 int slot = ffs(slots) - 1;
54bfa546 318 bool slot_free = true;
7faa8075
AW
319
320 /* Mark request as complete */
321 s->pci0_status.down &= ~(1U << slot);
322
0866aca1
AL
323 QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
324 DeviceState *qdev = kid->child;
7faa8075
AW
325 PCIDevice *dev = PCI_DEVICE(qdev);
326 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
54bfa546
MT
327 if (PCI_SLOT(dev->devfn) == slot) {
328 if (pc->no_hotplug) {
329 slot_free = false;
330 } else {
02a5c4c9 331 object_unparent(OBJECT(qdev));
54bfa546 332 }
7faa8075
AW
333 }
334 }
54bfa546
MT
335 if (slot_free) {
336 s->pci0_slot_device_present &= ~(1U << slot);
337 }
7faa8075
AW
338}
339
668643b0
MT
340static void piix4_update_hotplug(PIIX4PMState *s)
341{
74e445f6 342 BusState *bus = qdev_get_parent_bus(DEVICE(s));
0866aca1 343 BusChild *kid, *next;
668643b0 344
7faa8075
AW
345 /* Execute any pending removes during reset */
346 while (s->pci0_status.down) {
347 acpi_piix_eject_slot(s, s->pci0_status.down);
348 }
349
668643b0 350 s->pci0_hotplug_enable = ~0;
7faa8075 351 s->pci0_slot_device_present = 0;
668643b0 352
0866aca1
AL
353 QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
354 DeviceState *qdev = kid->child;
40021f08
AL
355 PCIDevice *pdev = PCI_DEVICE(qdev);
356 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev);
668643b0
MT
357 int slot = PCI_SLOT(pdev->devfn);
358
40021f08 359 if (pc->no_hotplug) {
7faa8075 360 s->pci0_hotplug_enable &= ~(1U << slot);
668643b0 361 }
7faa8075
AW
362
363 s->pci0_slot_device_present |= (1U << slot);
668643b0
MT
364 }
365}
366
93d89f63
IY
367static void piix4_reset(void *opaque)
368{
369 PIIX4PMState *s = opaque;
6a6b5580
AF
370 PCIDevice *d = PCI_DEVICE(s);
371 uint8_t *pci_conf = d->config;
93d89f63
IY
372
373 pci_conf[0x58] = 0;
374 pci_conf[0x59] = 0;
375 pci_conf[0x5a] = 0;
376 pci_conf[0x5b] = 0;
377
4d09d37c
GN
378 pci_conf[0x40] = 0x01; /* PM io base read only bit */
379 pci_conf[0x80] = 0;
380
93d89f63
IY
381 if (s->kvm_enabled) {
382 /* Mark SMM as already inited (until KVM supports SMM). */
383 pci_conf[0x5B] = 0x02;
384 }
c046e8c4 385 pm_io_space_update(s);
668643b0 386 piix4_update_hotplug(s);
93d89f63
IY
387}
388
d010f91c 389static void piix4_pm_powerdown_req(Notifier *n, void *opaque)
93d89f63 390{
d010f91c 391 PIIX4PMState *s = container_of(n, PIIX4PMState, powerdown_notifier);
93d89f63 392
355bf2e5
GH
393 assert(s != NULL);
394 acpi_pm1_evt_power_down(&s->ar);
93d89f63
IY
395}
396
9e8dd451 397static void piix4_pm_machine_ready(Notifier *n, void *opaque)
6141dbfe
PB
398{
399 PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
6a6b5580
AF
400 PCIDevice *d = PCI_DEVICE(s);
401 MemoryRegion *io_as = pci_address_space_io(d);
6141dbfe
PB
402 uint8_t *pci_conf;
403
6a6b5580 404 pci_conf = d->config;
b6f32962 405 pci_conf[0x5f] = 0x10 |
3ce10901 406 (memory_region_present(io_as, 0x378) ? 0x80 : 0);
6141dbfe 407 pci_conf[0x63] = 0x60;
3ce10901
PB
408 pci_conf[0x67] = (memory_region_present(io_as, 0x3f8) ? 0x08 : 0) |
409 (memory_region_present(io_as, 0x2f8) ? 0x90 : 0);
6141dbfe
PB
410}
411
277e9340
MT
412static void piix4_pm_add_propeties(PIIX4PMState *s)
413{
414 static const uint8_t acpi_enable_cmd = ACPI_ENABLE;
415 static const uint8_t acpi_disable_cmd = ACPI_DISABLE;
416 static const uint32_t gpe0_blk = GPE_BASE;
417 static const uint32_t gpe0_blk_len = GPE_LEN;
418 static const uint16_t sci_int = 9;
419
420 object_property_add_uint8_ptr(OBJECT(s), ACPI_PM_PROP_ACPI_ENABLE_CMD,
421 &acpi_enable_cmd, NULL);
422 object_property_add_uint8_ptr(OBJECT(s), ACPI_PM_PROP_ACPI_DISABLE_CMD,
423 &acpi_disable_cmd, NULL);
424 object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_GPE0_BLK,
425 &gpe0_blk, NULL);
426 object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_GPE0_BLK_LEN,
427 &gpe0_blk_len, NULL);
428 object_property_add_uint16_ptr(OBJECT(s), ACPI_PM_PROP_SCI_INT,
429 &sci_int, NULL);
430 object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_PM_IO_BASE,
431 &s->io_base, NULL);
432}
433
e8ec0571 434static int piix4_pm_initfn(PCIDevice *dev)
93d89f63 435{
74e445f6 436 PIIX4PMState *s = PIIX4_PM(dev);
93d89f63
IY
437 uint8_t *pci_conf;
438
6a6b5580 439 pci_conf = dev->config;
93d89f63
IY
440 pci_conf[0x06] = 0x80;
441 pci_conf[0x07] = 0x02;
93d89f63 442 pci_conf[0x09] = 0x00;
93d89f63
IY
443 pci_conf[0x3d] = 0x01; // interrupt pin 1
444
93d89f63 445 /* APM */
42d8a3cf 446 apm_init(dev, &s->apm, apm_ctrl_changed, s);
93d89f63 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;
74e445f6 459 pm_smbus_init(DEVICE(dev), &s->smb);
24fe083d 460 memory_region_set_enabled(&s->smb.io, pci_conf[0xd2] & 1);
56e5b2a1
GH
461 memory_region_add_subregion(pci_address_space_io(dev),
462 s->smb_io_base, &s->smb.io);
93d89f63 463
64bde0f3 464 memory_region_init(&s->io, OBJECT(s), "piix4-pm", 64);
af11110b 465 memory_region_set_enabled(&s->io, false);
56e5b2a1
GH
466 memory_region_add_subregion(pci_address_space_io(dev),
467 0, &s->io);
93d89f63 468
77d58b1e 469 acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
b5a7c024 470 acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
560e6396 471 acpi_pm1_cnt_init(&s->ar, &s->io, s->s4_val);
355bf2e5 472 acpi_gpe_init(&s->ar, GPE_LEN);
93d89f63 473
d010f91c
IM
474 s->powerdown_notifier.notify = piix4_pm_powerdown_req;
475 qemu_register_powerdown_notifier(&s->powerdown_notifier);
93d89f63 476
6141dbfe
PB
477 s->machine_ready.notify = piix4_pm_machine_ready;
478 qemu_add_machine_init_done_notifier(&s->machine_ready);
e8ec0571 479 qemu_register_reset(piix4_reset, s);
56e5b2a1
GH
480
481 piix4_acpi_system_hot_add_init(pci_address_space_io(dev), dev->bus, s);
e8ec0571 482
277e9340 483 piix4_pm_add_propeties(s);
e8ec0571
IY
484 return 0;
485}
486
277e9340
MT
487Object *piix4_pm_find(void)
488{
489 bool ambig;
490 Object *o = object_resolve_path_type("", TYPE_PIIX4_PM, &ambig);
491
492 if (ambig || !o) {
493 return NULL;
494 }
495 return o;
496}
497
e8ec0571 498i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
da98c8eb 499 qemu_irq sci_irq, qemu_irq smi_irq,
a88b362c 500 int kvm_enabled, FWCfgState *fw_cfg)
e8ec0571 501{
74e445f6 502 DeviceState *dev;
e8ec0571
IY
503 PIIX4PMState *s;
504
74e445f6
PC
505 dev = DEVICE(pci_create(bus, devfn, TYPE_PIIX4_PM));
506 qdev_prop_set_uint32(dev, "smb_io_base", smb_io_base);
93d89f63 507
74e445f6 508 s = PIIX4_PM(dev);
93d89f63 509 s->irq = sci_irq;
93d89f63 510 s->smi_irq = smi_irq;
e8ec0571
IY
511 s->kvm_enabled = kvm_enabled;
512
74e445f6 513 qdev_init_nofail(dev);
93d89f63 514
459ae5ea
GN
515 if (fw_cfg) {
516 uint8_t suspend[6] = {128, 0, 0, 129, 128, 128};
517 suspend[3] = 1 | ((!s->disable_s3) << 7);
518 suspend[4] = s->s4_val | ((!s->disable_s4) << 7);
519
520 fw_cfg_add_file(fw_cfg, "etc/system-states", g_memdup(suspend, 6), 6);
521 }
522
93d89f63
IY
523 return s->smb.smbus;
524}
525
40021f08
AL
526static Property piix4_pm_properties[] = {
527 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
f854ecc7
MT
528 DEFINE_PROP_UINT8(ACPI_PM_PROP_S3_DISABLED, PIIX4PMState, disable_s3, 0),
529 DEFINE_PROP_UINT8(ACPI_PM_PROP_S4_DISABLED, PIIX4PMState, disable_s4, 0),
530 DEFINE_PROP_UINT8(ACPI_PM_PROP_S4_VAL, PIIX4PMState, s4_val, 2),
40021f08
AL
531 DEFINE_PROP_END_OF_LIST(),
532};
533
534static void piix4_pm_class_init(ObjectClass *klass, void *data)
535{
39bffca2 536 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
537 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
538
539 k->no_hotplug = 1;
540 k->init = piix4_pm_initfn;
541 k->config_write = pm_write_config;
542 k->vendor_id = PCI_VENDOR_ID_INTEL;
543 k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3;
544 k->revision = 0x03;
545 k->class_id = PCI_CLASS_BRIDGE_OTHER;
39bffca2
AL
546 dc->desc = "PM";
547 dc->no_user = 1;
548 dc->vmsd = &vmstate_acpi;
549 dc->props = piix4_pm_properties;
40021f08
AL
550}
551
8c43a6f0 552static const TypeInfo piix4_pm_info = {
74e445f6 553 .name = TYPE_PIIX4_PM,
39bffca2
AL
554 .parent = TYPE_PCI_DEVICE,
555 .instance_size = sizeof(PIIX4PMState),
556 .class_init = piix4_pm_class_init,
e8ec0571
IY
557};
558
83f7d43a 559static void piix4_pm_register_types(void)
e8ec0571 560{
39bffca2 561 type_register_static(&piix4_pm_info);
e8ec0571
IY
562}
563
83f7d43a 564type_init(piix4_pm_register_types)
e8ec0571 565
b65b93f2 566static uint64_t gpe_readb(void *opaque, hwaddr addr, unsigned width)
93d89f63 567{
633aa0ac 568 PIIX4PMState *s = opaque;
355bf2e5 569 uint32_t val = acpi_gpe_ioport_readb(&s->ar, addr);
93d89f63 570
ba275adb 571 PIIX4_DPRINTF("gpe read %" HWADDR_PRIx " == %" PRIu32 "\n", addr, val);
93d89f63
IY
572 return val;
573}
574
b65b93f2
GH
575static void gpe_writeb(void *opaque, hwaddr addr, uint64_t val,
576 unsigned width)
93d89f63 577{
633aa0ac 578 PIIX4PMState *s = opaque;
633aa0ac 579
355bf2e5 580 acpi_gpe_ioport_writeb(&s->ar, addr, val);
633aa0ac 581 pm_update_sci(s);
93d89f63 582
ba275adb 583 PIIX4_DPRINTF("gpe write %" HWADDR_PRIx " <== %" PRIu64 "\n", addr, val);
93d89f63
IY
584}
585
b65b93f2
GH
586static const MemoryRegionOps piix4_gpe_ops = {
587 .read = gpe_readb,
588 .write = gpe_writeb,
589 .valid.min_access_size = 1,
590 .valid.max_access_size = 4,
591 .impl.min_access_size = 1,
592 .impl.max_access_size = 1,
593 .endianness = DEVICE_LITTLE_ENDIAN,
594};
595
c3a29809 596static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
93d89f63 597{
ba737541 598 PIIX4PMState *s = opaque;
c3a29809
HP
599 uint32_t val = 0;
600
601 switch (addr) {
602 case PCI_UP_BASE - PCI_HOTPLUG_ADDR:
603 /* Manufacture an "up" value to cause a device check on any hotplug
604 * slot with a device. Extra device checks are harmless. */
605 val = s->pci0_slot_device_present & s->pci0_hotplug_enable;
ba275adb 606 PIIX4_DPRINTF("pci_up_read %" PRIu32 "\n", val);
c3a29809
HP
607 break;
608 case PCI_DOWN_BASE - PCI_HOTPLUG_ADDR:
609 val = s->pci0_status.down;
ba275adb 610 PIIX4_DPRINTF("pci_down_read %" PRIu32 "\n", val);
c3a29809
HP
611 break;
612 case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
613 /* No feature defined yet */
ba275adb 614 PIIX4_DPRINTF("pci_features_read %" PRIu32 "\n", val);
c3a29809
HP
615 break;
616 case PCI_RMV_BASE - PCI_HOTPLUG_ADDR:
617 val = s->pci0_hotplug_enable;
618 break;
619 default:
620 break;
621 }
ba737541 622
ba737541 623 return val;
93d89f63
IY
624}
625
c3a29809
HP
626static void pci_write(void *opaque, hwaddr addr, uint64_t data,
627 unsigned int size)
93d89f63 628{
c3a29809
HP
629 switch (addr) {
630 case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
631 acpi_piix_eject_slot(opaque, (uint32_t)data);
ba275adb 632 PIIX4_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n",
c3a29809
HP
633 addr, data);
634 break;
635 default:
636 break;
637 }
668643b0
MT
638}
639
c177684c 640static const MemoryRegionOps piix4_pci_ops = {
c3a29809
HP
641 .read = pci_read,
642 .write = pci_write,
c177684c 643 .endianness = DEVICE_LITTLE_ENDIAN,
c3a29809
HP
644 .valid = {
645 .min_access_size = 4,
646 .max_access_size = 4,
647 },
c177684c
GH
648};
649
b8622725
IM
650static uint64_t cpu_status_read(void *opaque, hwaddr addr, unsigned int size)
651{
652 PIIX4PMState *s = opaque;
653 CPUStatus *cpus = &s->gpe_cpu;
654 uint64_t val = cpus->sts[addr];
655
656 return val;
657}
658
659static void cpu_status_write(void *opaque, hwaddr addr, uint64_t data,
660 unsigned int size)
661{
662 /* TODO: implement VCPU removal on guest signal that CPU can be removed */
663}
664
665static const MemoryRegionOps cpu_hotplug_ops = {
666 .read = cpu_status_read,
667 .write = cpu_status_write,
668 .endianness = DEVICE_LITTLE_ENDIAN,
669 .valid = {
670 .min_access_size = 1,
671 .max_access_size = 1,
672 },
673};
674
675typedef enum {
676 PLUG,
677 UNPLUG,
678} HotplugEventType;
679
680static void piix4_cpu_hotplug_req(PIIX4PMState *s, CPUState *cpu,
681 HotplugEventType action)
682{
683 CPUStatus *g = &s->gpe_cpu;
684 ACPIGPE *gpe = &s->ar.gpe;
685 CPUClass *k = CPU_GET_CLASS(cpu);
686 int64_t cpu_id;
687
688 assert(s != NULL);
689
690 *gpe->sts = *gpe->sts | PIIX4_CPU_HOTPLUG_STATUS;
691 cpu_id = k->get_arch_id(CPU(cpu));
692 if (action == PLUG) {
693 g->sts[cpu_id / 8] |= (1 << (cpu_id % 8));
694 } else {
695 g->sts[cpu_id / 8] &= ~(1 << (cpu_id % 8));
696 }
697 pm_update_sci(s);
698}
699
700static void piix4_cpu_added_req(Notifier *n, void *opaque)
701{
702 PIIX4PMState *s = container_of(n, PIIX4PMState, cpu_added_notifier);
703
704 piix4_cpu_hotplug_req(s, CPU(opaque), PLUG);
705}
706
4cff0a59
MT
707static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
708 PCIHotplugState state);
93d89f63 709
56e5b2a1
GH
710static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
711 PCIBus *bus, PIIX4PMState *s)
93d89f63 712{
38fcbd3f
AF
713 CPUState *cpu;
714
64bde0f3
PB
715 memory_region_init_io(&s->io_gpe, OBJECT(s), &piix4_gpe_ops, s,
716 "acpi-gpe0", GPE_LEN);
56e5b2a1 717 memory_region_add_subregion(parent, GPE_BASE, &s->io_gpe);
ac404095 718
64bde0f3
PB
719 memory_region_init_io(&s->io_pci, OBJECT(s), &piix4_pci_ops, s,
720 "acpi-pci-hotplug", PCI_HOTPLUG_SIZE);
56e5b2a1 721 memory_region_add_subregion(parent, PCI_HOTPLUG_ADDR,
c177684c 722 &s->io_pci);
74e445f6 723 pci_bus_hotplug(bus, piix4_device_hotplug, DEVICE(s));
b8622725 724
38fcbd3f
AF
725 CPU_FOREACH(cpu) {
726 CPUClass *cc = CPU_GET_CLASS(cpu);
727 int64_t id = cc->get_arch_id(cpu);
728
729 g_assert((id / 8) < PIIX4_PROC_LEN);
730 s->gpe_cpu.sts[id / 8] |= (1 << (id % 8));
731 }
64bde0f3
PB
732 memory_region_init_io(&s->io_cpu, OBJECT(s), &cpu_hotplug_ops, s,
733 "acpi-cpu-hotplug", PIIX4_PROC_LEN);
b8622725
IM
734 memory_region_add_subregion(parent, PIIX4_PROC_BASE, &s->io_cpu);
735 s->cpu_added_notifier.notify = piix4_cpu_added_req;
736 qemu_register_cpu_added_notifier(&s->cpu_added_notifier);
93d89f63
IY
737}
738
ac404095 739static void enable_device(PIIX4PMState *s, int slot)
93d89f63 740{
355bf2e5 741 s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
7faa8075 742 s->pci0_slot_device_present |= (1U << slot);
93d89f63
IY
743}
744
ac404095 745static void disable_device(PIIX4PMState *s, int slot)
93d89f63 746{
355bf2e5 747 s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
7faa8075 748 s->pci0_status.down |= (1U << slot);
93d89f63
IY
749}
750
4cff0a59
MT
751static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
752 PCIHotplugState state)
93d89f63
IY
753{
754 int slot = PCI_SLOT(dev->devfn);
74e445f6 755 PIIX4PMState *s = PIIX4_PM(qdev);
93d89f63 756
4cff0a59
MT
757 /* Don't send event when device is enabled during qemu machine creation:
758 * it is present on boot, no hotplug event is necessary. We do send an
759 * event when the device is disabled later. */
760 if (state == PCI_COLDPLUG_ENABLED) {
7faa8075 761 s->pci0_slot_device_present |= (1U << slot);
5beb8ad5 762 return 0;
4cff0a59 763 }
5beb8ad5 764
4cff0a59 765 if (state == PCI_HOTPLUG_ENABLED) {
ac404095
IY
766 enable_device(s, slot);
767 } else {
768 disable_device(s, slot);
769 }
633aa0ac
GN
770
771 pm_update_sci(s);
772
93d89f63
IY
773 return 0;
774}