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