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