]> git.proxmox.com Git - mirror_qemu.git/blame - hw/acpi/piix4.c
hvf: Introduce hvf vcpu struct
[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
61f3c91a 8 * License version 2.1 as published by the Free Software Foundation.
93d89f63
IY
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 */
71e8a915 21
b6a0aa05 22#include "qemu/osdep.h"
0d09e41a 23#include "hw/i386/pc.h"
fff123b8 24#include "hw/southbridge/piix.h"
64552b6b 25#include "hw/irq.h"
0d09e41a
PB
26#include "hw/isa/apm.h"
27#include "hw/i2c/pm_smbus.h"
83c9f4ca 28#include "hw/pci/pci.h"
a27bd6c7 29#include "hw/qdev-properties.h"
0d09e41a 30#include "hw/acpi/acpi.h"
54d31236 31#include "sysemu/runstate.h"
9c17d615 32#include "sysemu/sysemu.h"
da278d58 33#include "sysemu/xen.h"
da34e65c 34#include "qapi/error.h"
1de7afc9 35#include "qemu/range.h"
9e047b98 36#include "hw/acpi/pcihp.h"
81cea5e7 37#include "hw/acpi/cpu_hotplug.h"
5e1b5d93 38#include "hw/acpi/cpu.h"
c24d5e0b 39#include "hw/hotplug.h"
34774320 40#include "hw/mem/pc-dimm.h"
132a908b 41#include "hw/mem/nvdimm.h"
34774320 42#include "hw/acpi/memory_hotplug.h"
43f50410 43#include "hw/acpi/acpi_dev_interface.h"
d6454270 44#include "migration/vmstate.h"
2e5b09fd 45#include "hw/core/cpu.h"
b37d56ec 46#include "trace.h"
db1015e9 47#include "qom/object.h"
50d8ff8b 48
ac404095 49#define GPE_BASE 0xafe0
23910d3f 50#define GPE_LEN 4
c177684c 51
ac404095 52struct pci_status {
7faa8075 53 uint32_t up; /* deprecated, maintained for migration compatibility */
ac404095
IY
54 uint32_t down;
55};
56
db1015e9 57struct PIIX4PMState {
6a6b5580
AF
58 /*< private >*/
59 PCIDevice parent_obj;
60 /*< public >*/
56e5b2a1 61
af11110b 62 MemoryRegion io;
277e9340
MT
63 uint32_t io_base;
64
b65b93f2 65 MemoryRegion io_gpe;
355bf2e5 66 ACPIREGS ar;
93d89f63
IY
67
68 APMState apm;
69
93d89f63 70 PMSMBus smb;
e8ec0571 71 uint32_t smb_io_base;
93d89f63
IY
72
73 qemu_irq irq;
93d89f63 74 qemu_irq smi_irq;
61e66c62 75 int smm_enabled;
24cd04fc 76 bool smm_compat;
6141dbfe 77 Notifier machine_ready;
d010f91c 78 Notifier powerdown_notifier;
ac404095 79
9e047b98 80 AcpiPciHpState acpi_pci_hotplug;
0affda04 81 bool use_acpi_hotplug_bridge;
3d7e78aa 82 bool use_acpi_root_pci_hotplug;
9e047b98 83
459ae5ea
GN
84 uint8_t disable_s3;
85 uint8_t disable_s4;
86 uint8_t s4_val;
b8622725 87
16bcab97 88 bool cpu_hotplug_legacy;
81cea5e7 89 AcpiCpuHotplug gpe_cpu;
5e1b5d93 90 CPUHotplugState cpuhp_state;
34774320
IM
91
92 MemHotplugState acpi_memory_hotplug;
db1015e9 93};
93d89f63 94
8063396b 95OBJECT_DECLARE_SIMPLE_TYPE(PIIX4PMState, PIIX4_PM)
74e445f6 96
56e5b2a1
GH
97static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
98 PCIBus *bus, PIIX4PMState *s);
ac404095 99
93d89f63
IY
100#define ACPI_ENABLE 0xf1
101#define ACPI_DISABLE 0xf0
102
355bf2e5 103static void pm_tmr_timer(ACPIREGS *ar)
93d89f63 104{
355bf2e5 105 PIIX4PMState *s = container_of(ar, PIIX4PMState, ar);
06313503 106 acpi_update_sci(&s->ar, s->irq);
93d89f63
IY
107}
108
93d89f63
IY
109static void apm_ctrl_changed(uint32_t val, void *arg)
110{
111 PIIX4PMState *s = arg;
6a6b5580 112 PCIDevice *d = PCI_DEVICE(s);
93d89f63
IY
113
114 /* ACPI specs 3.0, 4.7.2.5 */
355bf2e5 115 acpi_pm1_cnt_update(&s->ar, val == ACPI_ENABLE, val == ACPI_DISABLE);
afd6895b
PB
116 if (val == ACPI_ENABLE || val == ACPI_DISABLE) {
117 return;
118 }
93d89f63 119
6a6b5580 120 if (d->config[0x5b] & (1 << 1)) {
93d89f63
IY
121 if (s->smi_irq) {
122 qemu_irq_raise(s->smi_irq);
123 }
124 }
125}
126
93d89f63
IY
127static void pm_io_space_update(PIIX4PMState *s)
128{
6a6b5580 129 PCIDevice *d = PCI_DEVICE(s);
93d89f63 130
277e9340
MT
131 s->io_base = le32_to_cpu(*(uint32_t *)(d->config + 0x40));
132 s->io_base &= 0xffc0;
93d89f63 133
af11110b 134 memory_region_transaction_begin();
6a6b5580 135 memory_region_set_enabled(&s->io, d->config[0x80] & 1);
277e9340 136 memory_region_set_address(&s->io, s->io_base);
af11110b 137 memory_region_transaction_commit();
93d89f63
IY
138}
139
24fe083d
GH
140static void smbus_io_space_update(PIIX4PMState *s)
141{
6a6b5580
AF
142 PCIDevice *d = PCI_DEVICE(s);
143
144 s->smb_io_base = le32_to_cpu(*(uint32_t *)(d->config + 0x90));
24fe083d
GH
145 s->smb_io_base &= 0xffc0;
146
147 memory_region_transaction_begin();
6a6b5580 148 memory_region_set_enabled(&s->smb.io, d->config[0xd2] & 1);
24fe083d
GH
149 memory_region_set_address(&s->smb.io, s->smb_io_base);
150 memory_region_transaction_commit();
93d89f63
IY
151}
152
153static void pm_write_config(PCIDevice *d,
154 uint32_t address, uint32_t val, int len)
155{
156 pci_default_write_config(d, address, val, len);
24fe083d
GH
157 if (range_covers_byte(address, len, 0x80) ||
158 ranges_overlap(address, len, 0x40, 4)) {
93d89f63 159 pm_io_space_update((PIIX4PMState *)d);
24fe083d
GH
160 }
161 if (range_covers_byte(address, len, 0xd2) ||
162 ranges_overlap(address, len, 0x90, 4)) {
163 smbus_io_space_update((PIIX4PMState *)d);
164 }
93d89f63
IY
165}
166
167static int vmstate_acpi_post_load(void *opaque, int version_id)
168{
169 PIIX4PMState *s = opaque;
170
171 pm_io_space_update(s);
2b4e573c 172 smbus_io_space_update(s);
93d89f63
IY
173 return 0;
174}
175
23910d3f
IY
176#define VMSTATE_GPE_ARRAY(_field, _state) \
177 { \
178 .name = (stringify(_field)), \
179 .version_id = 0, \
23910d3f
IY
180 .info = &vmstate_info_uint16, \
181 .size = sizeof(uint16_t), \
b0b873a0 182 .flags = VMS_SINGLE | VMS_POINTER, \
23910d3f
IY
183 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
184 }
185
4cf3e6f3
AW
186static const VMStateDescription vmstate_gpe = {
187 .name = "gpe",
188 .version_id = 1,
189 .minimum_version_id = 1,
d49805ae 190 .fields = (VMStateField[]) {
23910d3f
IY
191 VMSTATE_GPE_ARRAY(sts, ACPIGPE),
192 VMSTATE_GPE_ARRAY(en, ACPIGPE),
4cf3e6f3
AW
193 VMSTATE_END_OF_LIST()
194 }
195};
196
197static const VMStateDescription vmstate_pci_status = {
198 .name = "pci_status",
199 .version_id = 1,
200 .minimum_version_id = 1,
d49805ae 201 .fields = (VMStateField[]) {
e358edc8
IM
202 VMSTATE_UINT32(up, struct AcpiPciHpPciStatus),
203 VMSTATE_UINT32(down, struct AcpiPciHpPciStatus),
4cf3e6f3
AW
204 VMSTATE_END_OF_LIST()
205 }
206};
207
0affda04 208static bool vmstate_test_use_acpi_hotplug_bridge(void *opaque, int version_id)
9e047b98
MT
209{
210 PIIX4PMState *s = opaque;
0affda04 211 return s->use_acpi_hotplug_bridge;
9e047b98
MT
212}
213
0affda04
AS
214static bool vmstate_test_no_use_acpi_hotplug_bridge(void *opaque,
215 int version_id)
9e047b98
MT
216{
217 PIIX4PMState *s = opaque;
0affda04 218 return !s->use_acpi_hotplug_bridge;
9e047b98
MT
219}
220
f816a62d
IM
221static bool vmstate_test_use_memhp(void *opaque)
222{
223 PIIX4PMState *s = opaque;
224 return s->acpi_memory_hotplug.is_enabled;
225}
226
227static const VMStateDescription vmstate_memhp_state = {
228 .name = "piix4_pm/memhp",
229 .version_id = 1,
230 .minimum_version_id = 1,
231 .minimum_version_id_old = 1,
5cd8cada 232 .needed = vmstate_test_use_memhp,
f816a62d
IM
233 .fields = (VMStateField[]) {
234 VMSTATE_MEMORY_HOTPLUG(acpi_memory_hotplug, PIIX4PMState),
235 VMSTATE_END_OF_LIST()
236 }
237};
238
679dd1a9
IM
239static bool vmstate_test_use_cpuhp(void *opaque)
240{
241 PIIX4PMState *s = opaque;
242 return !s->cpu_hotplug_legacy;
243}
244
245static int vmstate_cpuhp_pre_load(void *opaque)
246{
247 Object *obj = OBJECT(opaque);
5325cc34 248 object_property_set_bool(obj, "cpu-hotplug-legacy", false, &error_abort);
679dd1a9
IM
249 return 0;
250}
251
252static const VMStateDescription vmstate_cpuhp_state = {
253 .name = "piix4_pm/cpuhp",
254 .version_id = 1,
255 .minimum_version_id = 1,
256 .minimum_version_id_old = 1,
257 .needed = vmstate_test_use_cpuhp,
258 .pre_load = vmstate_cpuhp_pre_load,
259 .fields = (VMStateField[]) {
260 VMSTATE_CPU_HOTPLUG(cpuhp_state, PIIX4PMState),
261 VMSTATE_END_OF_LIST()
262 }
263};
264
4ab2f2a8
CM
265static bool piix4_vmstate_need_smbus(void *opaque, int version_id)
266{
267 return pm_smbus_vmstate_needed();
268}
269
b0b873a0
MT
270/* qemu-kvm 1.2 uses version 3 but advertised as 2
271 * To support incoming qemu-kvm 1.2 migration, change version_id
272 * and minimum_version_id to 2 below (which breaks migration from
273 * qemu 1.2).
274 *
275 */
93d89f63
IY
276static const VMStateDescription vmstate_acpi = {
277 .name = "piix4_pm",
b0b873a0
MT
278 .version_id = 3,
279 .minimum_version_id = 3,
93d89f63 280 .post_load = vmstate_acpi_post_load,
d49805ae 281 .fields = (VMStateField[]) {
6a6b5580 282 VMSTATE_PCI_DEVICE(parent_obj, PIIX4PMState),
355bf2e5
GH
283 VMSTATE_UINT16(ar.pm1.evt.sts, PIIX4PMState),
284 VMSTATE_UINT16(ar.pm1.evt.en, PIIX4PMState),
285 VMSTATE_UINT16(ar.pm1.cnt.cnt, PIIX4PMState),
93d89f63 286 VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
4ab2f2a8
CM
287 VMSTATE_STRUCT_TEST(smb, PIIX4PMState, piix4_vmstate_need_smbus, 3,
288 pmsmb_vmstate, PMSMBus),
e720677e 289 VMSTATE_TIMER_PTR(ar.tmr.timer, PIIX4PMState),
355bf2e5
GH
290 VMSTATE_INT64(ar.tmr.overflow_time, PIIX4PMState),
291 VMSTATE_STRUCT(ar.gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
e358edc8
IM
292 VMSTATE_STRUCT_TEST(
293 acpi_pci_hotplug.acpi_pcihp_pci_status[ACPI_PCIHP_BSEL_DEFAULT],
294 PIIX4PMState,
0affda04 295 vmstate_test_no_use_acpi_hotplug_bridge,
e358edc8
IM
296 2, vmstate_pci_status,
297 struct AcpiPciHpPciStatus),
9e047b98 298 VMSTATE_PCI_HOTPLUG(acpi_pci_hotplug, PIIX4PMState,
b32bd763
IM
299 vmstate_test_use_acpi_hotplug_bridge,
300 vmstate_acpi_pcihp_use_acpi_index),
93d89f63 301 VMSTATE_END_OF_LIST()
f816a62d 302 },
5cd8cada
JQ
303 .subsections = (const VMStateDescription*[]) {
304 &vmstate_memhp_state,
679dd1a9 305 &vmstate_cpuhp_state,
5cd8cada 306 NULL
93d89f63
IY
307 }
308};
309
217e8ef9 310static void piix4_pm_reset(DeviceState *dev)
93d89f63 311{
217e8ef9 312 PIIX4PMState *s = PIIX4_PM(dev);
6a6b5580
AF
313 PCIDevice *d = PCI_DEVICE(s);
314 uint8_t *pci_conf = d->config;
93d89f63
IY
315
316 pci_conf[0x58] = 0;
317 pci_conf[0x59] = 0;
318 pci_conf[0x5a] = 0;
319 pci_conf[0x5b] = 0;
320
4d09d37c
GN
321 pci_conf[0x40] = 0x01; /* PM io base read only bit */
322 pci_conf[0x80] = 0;
323
61e66c62 324 if (!s->smm_enabled) {
93d89f63
IY
325 /* Mark SMM as already inited (until KVM supports SMM). */
326 pci_conf[0x5B] = 0x02;
327 }
0fd74325
IY
328
329 acpi_pm1_evt_reset(&s->ar);
330 acpi_pm1_cnt_reset(&s->ar);
331 acpi_pm_tmr_reset(&s->ar);
332 acpi_gpe_reset(&s->ar);
333 acpi_update_sci(&s->ar, s->irq);
334
c046e8c4 335 pm_io_space_update(s);
3d7e78aa 336 acpi_pcihp_reset(&s->acpi_pci_hotplug, !s->use_acpi_root_pci_hotplug);
93d89f63
IY
337}
338
d010f91c 339static void piix4_pm_powerdown_req(Notifier *n, void *opaque)
93d89f63 340{
d010f91c 341 PIIX4PMState *s = container_of(n, PIIX4PMState, powerdown_notifier);
93d89f63 342
355bf2e5
GH
343 assert(s != NULL);
344 acpi_pm1_evt_power_down(&s->ar);
93d89f63
IY
345}
346
ec266f40
DH
347static void piix4_device_pre_plug_cb(HotplugHandler *hotplug_dev,
348 DeviceState *dev, Error **errp)
349{
9040e6df
WY
350 PIIX4PMState *s = PIIX4_PM(hotplug_dev);
351
ec266f40
DH
352 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
353 acpi_pcihp_device_pre_plug_cb(hotplug_dev, dev, errp);
9040e6df
WY
354 } else if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
355 if (!s->acpi_memory_hotplug.is_enabled) {
356 error_setg(errp,
357 "memory hotplug is not enabled: %s.memory-hotplug-support "
358 "is not set", object_get_typename(OBJECT(s)));
359 }
360 } else if (
ec266f40
DH
361 !object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
362 error_setg(errp, "acpi: device pre plug request for not supported"
363 " device type: %s", object_get_typename(OBJECT(dev)));
364 }
365}
366
f1adc360
IM
367static void piix4_device_plug_cb(HotplugHandler *hotplug_dev,
368 DeviceState *dev, Error **errp)
9e047b98 369{
c24d5e0b 370 PIIX4PMState *s = PIIX4_PM(hotplug_dev);
f1adc360 371
9040e6df 372 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
75f27498
XG
373 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
374 nvdimm_acpi_plug_cb(hotplug_dev, dev);
375 } else {
376 acpi_memory_plug_cb(hotplug_dev, &s->acpi_memory_hotplug,
377 dev, errp);
378 }
34774320 379 } else if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
2bed1ba7 380 acpi_pcihp_device_plug_cb(hotplug_dev, &s->acpi_pci_hotplug, dev, errp);
5e1b5d93
IM
381 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
382 if (s->cpu_hotplug_legacy) {
383 legacy_acpi_cpu_plug_cb(hotplug_dev, &s->gpe_cpu, dev, errp);
384 } else {
385 acpi_cpu_plug_cb(hotplug_dev, &s->cpuhp_state, dev, errp);
386 }
f1adc360 387 } else {
ec266f40 388 g_assert_not_reached();
f1adc360 389 }
c24d5e0b 390}
9e047b98 391
14d5a28f
IM
392static void piix4_device_unplug_request_cb(HotplugHandler *hotplug_dev,
393 DeviceState *dev, Error **errp)
c24d5e0b
IM
394{
395 PIIX4PMState *s = PIIX4_PM(hotplug_dev);
f1adc360 396
64fec58e
TC
397 if (s->acpi_memory_hotplug.is_enabled &&
398 object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
0058c082 399 acpi_memory_unplug_request_cb(hotplug_dev, &s->acpi_memory_hotplug,
64fec58e
TC
400 dev, errp);
401 } else if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
c97adf3c
DH
402 acpi_pcihp_device_unplug_request_cb(hotplug_dev, &s->acpi_pci_hotplug,
403 dev, errp);
8872c25a
IM
404 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU) &&
405 !s->cpu_hotplug_legacy) {
406 acpi_cpu_unplug_request_cb(hotplug_dev, &s->cpuhp_state, dev, errp);
f1adc360
IM
407 } else {
408 error_setg(errp, "acpi: device unplug request for not supported device"
409 " type: %s", object_get_typename(OBJECT(dev)));
410 }
9e047b98
MT
411}
412
c0e57a60
TC
413static void piix4_device_unplug_cb(HotplugHandler *hotplug_dev,
414 DeviceState *dev, Error **errp)
415{
f7d3e29d
TC
416 PIIX4PMState *s = PIIX4_PM(hotplug_dev);
417
418 if (s->acpi_memory_hotplug.is_enabled &&
419 object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
420 acpi_memory_unplug_cb(&s->acpi_memory_hotplug, dev, errp);
c97adf3c
DH
421 } else if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
422 acpi_pcihp_device_unplug_cb(hotplug_dev, &s->acpi_pci_hotplug, dev,
423 errp);
8872c25a
IM
424 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU) &&
425 !s->cpu_hotplug_legacy) {
426 acpi_cpu_unplug_cb(&s->cpuhp_state, dev, errp);
f7d3e29d
TC
427 } else {
428 error_setg(errp, "acpi: device unplug for not supported device"
429 " type: %s", object_get_typename(OBJECT(dev)));
430 }
c0e57a60
TC
431}
432
9e8dd451 433static void piix4_pm_machine_ready(Notifier *n, void *opaque)
6141dbfe
PB
434{
435 PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
6a6b5580
AF
436 PCIDevice *d = PCI_DEVICE(s);
437 MemoryRegion *io_as = pci_address_space_io(d);
6141dbfe
PB
438 uint8_t *pci_conf;
439
6a6b5580 440 pci_conf = d->config;
b6f32962 441 pci_conf[0x5f] = 0x10 |
3ce10901 442 (memory_region_present(io_as, 0x378) ? 0x80 : 0);
6141dbfe 443 pci_conf[0x63] = 0x60;
3ce10901
PB
444 pci_conf[0x67] = (memory_region_present(io_as, 0x3f8) ? 0x08 : 0) |
445 (memory_region_present(io_as, 0x2f8) ? 0x90 : 0);
6141dbfe
PB
446}
447
5ad1037c 448static void piix4_pm_add_properties(PIIX4PMState *s)
277e9340
MT
449{
450 static const uint8_t acpi_enable_cmd = ACPI_ENABLE;
451 static const uint8_t acpi_disable_cmd = ACPI_DISABLE;
452 static const uint32_t gpe0_blk = GPE_BASE;
453 static const uint32_t gpe0_blk_len = GPE_LEN;
454 static const uint16_t sci_int = 9;
455
456 object_property_add_uint8_ptr(OBJECT(s), ACPI_PM_PROP_ACPI_ENABLE_CMD,
d2623129 457 &acpi_enable_cmd, OBJ_PROP_FLAG_READ);
277e9340 458 object_property_add_uint8_ptr(OBJECT(s), ACPI_PM_PROP_ACPI_DISABLE_CMD,
d2623129 459 &acpi_disable_cmd, OBJ_PROP_FLAG_READ);
277e9340 460 object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_GPE0_BLK,
d2623129 461 &gpe0_blk, OBJ_PROP_FLAG_READ);
277e9340 462 object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_GPE0_BLK_LEN,
d2623129 463 &gpe0_blk_len, OBJ_PROP_FLAG_READ);
277e9340 464 object_property_add_uint16_ptr(OBJECT(s), ACPI_PM_PROP_SCI_INT,
d2623129 465 &sci_int, OBJ_PROP_FLAG_READ);
277e9340 466 object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_PM_IO_BASE,
d2623129 467 &s->io_base, OBJ_PROP_FLAG_READ);
277e9340
MT
468}
469
9af21dbe 470static void piix4_pm_realize(PCIDevice *dev, Error **errp)
93d89f63 471{
74e445f6 472 PIIX4PMState *s = PIIX4_PM(dev);
93d89f63
IY
473 uint8_t *pci_conf;
474
6a6b5580 475 pci_conf = dev->config;
93d89f63
IY
476 pci_conf[0x06] = 0x80;
477 pci_conf[0x07] = 0x02;
93d89f63 478 pci_conf[0x09] = 0x00;
93d89f63
IY
479 pci_conf[0x3d] = 0x01; // interrupt pin 1
480
93d89f63 481 /* APM */
42d8a3cf 482 apm_init(dev, &s->apm, apm_ctrl_changed, s);
93d89f63 483
61e66c62 484 if (!s->smm_enabled) {
93d89f63
IY
485 /* Mark SMM as already inited to prevent SMM from running. KVM does not
486 * support SMM mode. */
487 pci_conf[0x5B] = 0x02;
488 }
489
490 /* XXX: which specification is used ? The i82731AB has different
491 mappings */
e8ec0571
IY
492 pci_conf[0x90] = s->smb_io_base | 1;
493 pci_conf[0x91] = s->smb_io_base >> 8;
93d89f63 494 pci_conf[0xd2] = 0x09;
45726b6e 495 pm_smbus_init(DEVICE(dev), &s->smb, true);
24fe083d 496 memory_region_set_enabled(&s->smb.io, pci_conf[0xd2] & 1);
56e5b2a1
GH
497 memory_region_add_subregion(pci_address_space_io(dev),
498 s->smb_io_base, &s->smb.io);
93d89f63 499
64bde0f3 500 memory_region_init(&s->io, OBJECT(s), "piix4-pm", 64);
af11110b 501 memory_region_set_enabled(&s->io, false);
56e5b2a1
GH
502 memory_region_add_subregion(pci_address_space_io(dev),
503 0, &s->io);
93d89f63 504
77d58b1e 505 acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
b5a7c024 506 acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
6be8cf56
IY
507 acpi_pm1_cnt_init(&s->ar, &s->io, s->disable_s3, s->disable_s4, s->s4_val,
508 !s->smm_compat && !s->smm_enabled);
355bf2e5 509 acpi_gpe_init(&s->ar, GPE_LEN);
93d89f63 510
d010f91c
IM
511 s->powerdown_notifier.notify = piix4_pm_powerdown_req;
512 qemu_register_powerdown_notifier(&s->powerdown_notifier);
93d89f63 513
6141dbfe
PB
514 s->machine_ready.notify = piix4_pm_machine_ready;
515 qemu_add_machine_init_done_notifier(&s->machine_ready);
56e5b2a1 516
fd56e061
DG
517 piix4_acpi_system_hot_add_init(pci_address_space_io(dev),
518 pci_get_bus(dev), s);
9bc6bfdf 519 qbus_set_hotplug_handler(BUS(pci_get_bus(dev)), OBJECT(s));
e8ec0571 520
5ad1037c 521 piix4_pm_add_properties(s);
e8ec0571
IY
522}
523
a5c82852
AF
524I2CBus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
525 qemu_irq sci_irq, qemu_irq smi_irq,
61e66c62 526 int smm_enabled, DeviceState **piix4_pm)
e8ec0571 527{
9307d06d 528 PCIDevice *pci_dev;
74e445f6 529 DeviceState *dev;
e8ec0571
IY
530 PIIX4PMState *s;
531
9307d06d
MA
532 pci_dev = pci_new(devfn, TYPE_PIIX4_PM);
533 dev = DEVICE(pci_dev);
74e445f6 534 qdev_prop_set_uint32(dev, "smb_io_base", smb_io_base);
781bbd6b
IM
535 if (piix4_pm) {
536 *piix4_pm = dev;
537 }
93d89f63 538
74e445f6 539 s = PIIX4_PM(dev);
93d89f63 540 s->irq = sci_irq;
93d89f63 541 s->smi_irq = smi_irq;
61e66c62 542 s->smm_enabled = smm_enabled;
91ab2ed7 543 if (xen_enabled()) {
0affda04 544 s->use_acpi_hotplug_bridge = false;
91ab2ed7 545 }
e8ec0571 546
9307d06d 547 pci_realize_and_unref(pci_dev, bus, &error_fatal);
93d89f63
IY
548
549 return s->smb.smbus;
550}
551
b65b93f2 552static uint64_t gpe_readb(void *opaque, hwaddr addr, unsigned width)
93d89f63 553{
633aa0ac 554 PIIX4PMState *s = opaque;
355bf2e5 555 uint32_t val = acpi_gpe_ioport_readb(&s->ar, addr);
93d89f63 556
b37d56ec 557 trace_piix4_gpe_readb(addr, width, val);
93d89f63
IY
558 return val;
559}
560
b65b93f2
GH
561static void gpe_writeb(void *opaque, hwaddr addr, uint64_t val,
562 unsigned width)
93d89f63 563{
633aa0ac 564 PIIX4PMState *s = opaque;
633aa0ac 565
b37d56ec 566 trace_piix4_gpe_writeb(addr, width, val);
355bf2e5 567 acpi_gpe_ioport_writeb(&s->ar, addr, val);
06313503 568 acpi_update_sci(&s->ar, s->irq);
93d89f63
IY
569}
570
b65b93f2
GH
571static const MemoryRegionOps piix4_gpe_ops = {
572 .read = gpe_readb,
573 .write = gpe_writeb,
574 .valid.min_access_size = 1,
575 .valid.max_access_size = 4,
576 .impl.min_access_size = 1,
577 .impl.max_access_size = 1,
578 .endianness = DEVICE_LITTLE_ENDIAN,
579};
580
16bcab97
IM
581
582static bool piix4_get_cpu_hotplug_legacy(Object *obj, Error **errp)
583{
584 PIIX4PMState *s = PIIX4_PM(obj);
585
586 return s->cpu_hotplug_legacy;
587}
588
589static void piix4_set_cpu_hotplug_legacy(Object *obj, bool value, Error **errp)
590{
591 PIIX4PMState *s = PIIX4_PM(obj);
592
679dd1a9
IM
593 assert(!value);
594 if (s->cpu_hotplug_legacy && value == false) {
595 acpi_switch_to_modern_cphp(&s->gpe_cpu, &s->cpuhp_state,
596 PIIX4_CPU_HOTPLUG_IO_BASE);
597 }
16bcab97
IM
598 s->cpu_hotplug_legacy = value;
599}
600
56e5b2a1
GH
601static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
602 PCIBus *bus, PIIX4PMState *s)
93d89f63 603{
64bde0f3
PB
604 memory_region_init_io(&s->io_gpe, OBJECT(s), &piix4_gpe_ops, s,
605 "acpi-gpe0", GPE_LEN);
56e5b2a1 606 memory_region_add_subregion(parent, GPE_BASE, &s->io_gpe);
ac404095 607
df4008c9
AS
608 if (s->use_acpi_hotplug_bridge || s->use_acpi_root_pci_hotplug) {
609 acpi_pcihp_init(OBJECT(s), &s->acpi_pci_hotplug, bus, parent,
610 s->use_acpi_hotplug_bridge);
611 }
b8622725 612
16bcab97
IM
613 s->cpu_hotplug_legacy = true;
614 object_property_add_bool(OBJECT(s), "cpu-hotplug-legacy",
615 piix4_get_cpu_hotplug_legacy,
d2623129 616 piix4_set_cpu_hotplug_legacy);
96e3e12b
IM
617 legacy_acpi_cpu_hotplug_init(parent, OBJECT(s), &s->gpe_cpu,
618 PIIX4_CPU_HOTPLUG_IO_BASE);
34774320
IM
619
620 if (s->acpi_memory_hotplug.is_enabled) {
80db0e78
IM
621 acpi_memory_hotplug_init(parent, OBJECT(s), &s->acpi_memory_hotplug,
622 ACPI_MEMORY_HOTPLUG_BASE);
34774320 623 }
93d89f63 624}
5fdae20c 625
43f50410
IM
626static void piix4_ospm_status(AcpiDeviceIf *adev, ACPIOSTInfoList ***list)
627{
628 PIIX4PMState *s = PIIX4_PM(adev);
629
630 acpi_memory_ospm_status(&s->acpi_memory_hotplug, list);
76623d00
IM
631 if (!s->cpu_hotplug_legacy) {
632 acpi_cpu_ospm_status(&s->cpuhp_state, list);
633 }
43f50410
IM
634}
635
eaf23bf7
IM
636static void piix4_send_gpe(AcpiDeviceIf *adev, AcpiEventStatusBits ev)
637{
638 PIIX4PMState *s = PIIX4_PM(adev);
639
640 acpi_send_gpe_event(&s->ar, s->irq, ev);
641}
642
5fdae20c
IM
643static Property piix4_pm_properties[] = {
644 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
645 DEFINE_PROP_UINT8(ACPI_PM_PROP_S3_DISABLED, PIIX4PMState, disable_s3, 0),
646 DEFINE_PROP_UINT8(ACPI_PM_PROP_S4_DISABLED, PIIX4PMState, disable_s4, 0),
647 DEFINE_PROP_UINT8(ACPI_PM_PROP_S4_VAL, PIIX4PMState, s4_val, 2),
648 DEFINE_PROP_BOOL("acpi-pci-hotplug-with-bridge-support", PIIX4PMState,
0affda04 649 use_acpi_hotplug_bridge, true),
3d7e78aa
AS
650 DEFINE_PROP_BOOL("acpi-root-pci-hotplug", PIIX4PMState,
651 use_acpi_root_pci_hotplug, true),
34774320
IM
652 DEFINE_PROP_BOOL("memory-hotplug-support", PIIX4PMState,
653 acpi_memory_hotplug.is_enabled, true),
24cd04fc 654 DEFINE_PROP_BOOL("smm-compat", PIIX4PMState, smm_compat, false),
5fdae20c
IM
655 DEFINE_PROP_END_OF_LIST(),
656};
657
658static void piix4_pm_class_init(ObjectClass *klass, void *data)
659{
660 DeviceClass *dc = DEVICE_CLASS(klass);
661 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
c24d5e0b 662 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
43f50410 663 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_CLASS(klass);
5fdae20c 664
9af21dbe 665 k->realize = piix4_pm_realize;
5fdae20c
IM
666 k->config_write = pm_write_config;
667 k->vendor_id = PCI_VENDOR_ID_INTEL;
668 k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3;
669 k->revision = 0x03;
670 k->class_id = PCI_CLASS_BRIDGE_OTHER;
217e8ef9 671 dc->reset = piix4_pm_reset;
5fdae20c
IM
672 dc->desc = "PM";
673 dc->vmsd = &vmstate_acpi;
4f67d30b 674 device_class_set_props(dc, piix4_pm_properties);
5fdae20c
IM
675 /*
676 * Reason: part of PIIX4 southbridge, needs to be wired up,
677 * e.g. by mips_malta_init()
678 */
e90f2a8c 679 dc->user_creatable = false;
2897ae02 680 dc->hotpluggable = false;
ec266f40 681 hc->pre_plug = piix4_device_pre_plug_cb;
f1adc360 682 hc->plug = piix4_device_plug_cb;
14d5a28f 683 hc->unplug_request = piix4_device_unplug_request_cb;
c0e57a60 684 hc->unplug = piix4_device_unplug_cb;
43f50410 685 adevc->ospm_status = piix4_ospm_status;
eaf23bf7 686 adevc->send_event = piix4_send_gpe;
ac35f13b 687 adevc->madt_cpu = pc_madt_cpu_entry;
5fdae20c
IM
688}
689
690static const TypeInfo piix4_pm_info = {
691 .name = TYPE_PIIX4_PM,
692 .parent = TYPE_PCI_DEVICE,
693 .instance_size = sizeof(PIIX4PMState),
694 .class_init = piix4_pm_class_init,
c24d5e0b
IM
695 .interfaces = (InterfaceInfo[]) {
696 { TYPE_HOTPLUG_HANDLER },
43f50410 697 { TYPE_ACPI_DEVICE_IF },
fd3b02c8 698 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
c24d5e0b
IM
699 { }
700 }
5fdae20c
IM
701};
702
703static void piix4_pm_register_types(void)
704{
705 type_register_static(&piix4_pm_info);
706}
707
708type_init(piix4_pm_register_types)