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