Statistics
| Branch: | Revision:

root / hw / acpi / piix4.c @ e4f308bb

History | View | Annotate | Download (22.2 kB)

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/>
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.
20
 */
21
#include "hw/hw.h"
22
#include "hw/i386/pc.h"
23
#include "hw/isa/apm.h"
24
#include "hw/i2c/pm_smbus.h"
25
#include "hw/pci/pci.h"
26
#include "hw/acpi/acpi.h"
27
#include "sysemu/sysemu.h"
28
#include "qemu/range.h"
29
#include "exec/ioport.h"
30
#include "hw/nvram/fw_cfg.h"
31
#include "exec/address-spaces.h"
32
#include "hw/acpi/piix4.h"
33

    
34
//#define DEBUG
35

    
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

    
42
#define GPE_BASE 0xafe0
43
#define GPE_LEN 4
44

    
45
#define PCI_HOTPLUG_ADDR 0xae00
46
#define PCI_HOTPLUG_SIZE 0x000f
47
#define PCI_UP_BASE 0xae00
48
#define PCI_DOWN_BASE 0xae04
49
#define PCI_EJ_BASE 0xae08
50
#define PCI_RMV_BASE 0xae0c
51

    
52
#define PIIX4_PROC_BASE 0xaf00
53
#define PIIX4_PROC_LEN 32
54

    
55
#define PIIX4_PCI_HOTPLUG_STATUS 2
56
#define PIIX4_CPU_HOTPLUG_STATUS 4
57

    
58
struct pci_status {
59
    uint32_t up; /* deprecated, maintained for migration compatibility */
60
    uint32_t down;
61
};
62

    
63
typedef struct CPUStatus {
64
    uint8_t sts[PIIX4_PROC_LEN];
65
} CPUStatus;
66

    
67
typedef struct PIIX4PMState {
68
    /*< private >*/
69
    PCIDevice parent_obj;
70
    /*< public >*/
71

    
72
    MemoryRegion io;
73
    uint32_t io_base;
74

    
75
    MemoryRegion io_gpe;
76
    MemoryRegion io_pci;
77
    MemoryRegion io_cpu;
78
    ACPIREGS ar;
79

    
80
    APMState apm;
81

    
82
    PMSMBus smb;
83
    uint32_t smb_io_base;
84

    
85
    qemu_irq irq;
86
    qemu_irq smi_irq;
87
    int kvm_enabled;
88
    Notifier machine_ready;
89
    Notifier powerdown_notifier;
90

    
91
    /* for pci hotplug */
92
    struct pci_status pci0_status;
93
    uint32_t pci0_hotplug_enable;
94
    uint32_t pci0_slot_device_present;
95

    
96
    uint8_t disable_s3;
97
    uint8_t disable_s4;
98
    uint8_t s4_val;
99

    
100
    CPUStatus gpe_cpu;
101
    Notifier cpu_added_notifier;
102
} PIIX4PMState;
103

    
104
#define TYPE_PIIX4_PM "PIIX4_PM"
105

    
106
#define PIIX4_PM(obj) \
107
    OBJECT_CHECK(PIIX4PMState, (obj), TYPE_PIIX4_PM)
108

    
109
static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
110
                                           PCIBus *bus, PIIX4PMState *s);
111

    
112
#define ACPI_ENABLE 0xf1
113
#define ACPI_DISABLE 0xf0
114

    
115
static void pm_update_sci(PIIX4PMState *s)
116
{
117
    int sci_level, pmsts;
118

    
119
    pmsts = acpi_pm1_evt_get_sts(&s->ar);
120
    sci_level = (((pmsts & s->ar.pm1.evt.en) &
121
                  (ACPI_BITMASK_RT_CLOCK_ENABLE |
122
                   ACPI_BITMASK_POWER_BUTTON_ENABLE |
123
                   ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
124
                   ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
125
        ((s->ar.gpe.sts[0] & s->ar.gpe.en[0]) != 0);
126

    
127
    qemu_set_irq(s->irq, sci_level);
128
    /* schedule a timer interruption if needed */
129
    acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
130
                       !(pmsts & ACPI_BITMASK_TIMER_STATUS));
131
}
132

    
133
static void pm_tmr_timer(ACPIREGS *ar)
134
{
135
    PIIX4PMState *s = container_of(ar, PIIX4PMState, ar);
136
    pm_update_sci(s);
137
}
138

    
139
static void apm_ctrl_changed(uint32_t val, void *arg)
140
{
141
    PIIX4PMState *s = arg;
142
    PCIDevice *d = PCI_DEVICE(s);
143

    
144
    /* ACPI specs 3.0, 4.7.2.5 */
145
    acpi_pm1_cnt_update(&s->ar, val == ACPI_ENABLE, val == ACPI_DISABLE);
146

    
147
    if (d->config[0x5b] & (1 << 1)) {
148
        if (s->smi_irq) {
149
            qemu_irq_raise(s->smi_irq);
150
        }
151
    }
152
}
153

    
154
static void pm_io_space_update(PIIX4PMState *s)
155
{
156
    PCIDevice *d = PCI_DEVICE(s);
157

    
158
    s->io_base = le32_to_cpu(*(uint32_t *)(d->config + 0x40));
159
    s->io_base &= 0xffc0;
160

    
161
    memory_region_transaction_begin();
162
    memory_region_set_enabled(&s->io, d->config[0x80] & 1);
163
    memory_region_set_address(&s->io, s->io_base);
164
    memory_region_transaction_commit();
165
}
166

    
167
static void smbus_io_space_update(PIIX4PMState *s)
168
{
169
    PCIDevice *d = PCI_DEVICE(s);
170

    
171
    s->smb_io_base = le32_to_cpu(*(uint32_t *)(d->config + 0x90));
172
    s->smb_io_base &= 0xffc0;
173

    
174
    memory_region_transaction_begin();
175
    memory_region_set_enabled(&s->smb.io, d->config[0xd2] & 1);
176
    memory_region_set_address(&s->smb.io, s->smb_io_base);
177
    memory_region_transaction_commit();
178
}
179

    
180
static void pm_write_config(PCIDevice *d,
181
                            uint32_t address, uint32_t val, int len)
182
{
183
    pci_default_write_config(d, address, val, len);
184
    if (range_covers_byte(address, len, 0x80) ||
185
        ranges_overlap(address, len, 0x40, 4)) {
186
        pm_io_space_update((PIIX4PMState *)d);
187
    }
188
    if (range_covers_byte(address, len, 0xd2) ||
189
        ranges_overlap(address, len, 0x90, 4)) {
190
        smbus_io_space_update((PIIX4PMState *)d);
191
    }
192
}
193

    
194
static void vmstate_pci_status_pre_save(void *opaque)
195
{
196
    struct pci_status *pci0_status = opaque;
197
    PIIX4PMState *s = container_of(pci0_status, PIIX4PMState, pci0_status);
198

    
199
    /* We no longer track up, so build a safe value for migrating
200
     * to a version that still does... of course these might get lost
201
     * by an old buggy implementation, but we try. */
202
    pci0_status->up = s->pci0_slot_device_present & s->pci0_hotplug_enable;
203
}
204

    
205
static int vmstate_acpi_post_load(void *opaque, int version_id)
206
{
207
    PIIX4PMState *s = opaque;
208

    
209
    pm_io_space_update(s);
210
    return 0;
211
}
212

    
213
#define VMSTATE_GPE_ARRAY(_field, _state)                            \
214
 {                                                                   \
215
     .name       = (stringify(_field)),                              \
216
     .version_id = 0,                                                \
217
     .info       = &vmstate_info_uint16,                             \
218
     .size       = sizeof(uint16_t),                                 \
219
     .flags      = VMS_SINGLE | VMS_POINTER,                         \
220
     .offset     = vmstate_offset_pointer(_state, _field, uint8_t),  \
221
 }
222

    
223
static const VMStateDescription vmstate_gpe = {
224
    .name = "gpe",
225
    .version_id = 1,
226
    .minimum_version_id = 1,
227
    .minimum_version_id_old = 1,
228
    .fields      = (VMStateField []) {
229
        VMSTATE_GPE_ARRAY(sts, ACPIGPE),
230
        VMSTATE_GPE_ARRAY(en, ACPIGPE),
231
        VMSTATE_END_OF_LIST()
232
    }
233
};
234

    
235
static const VMStateDescription vmstate_pci_status = {
236
    .name = "pci_status",
237
    .version_id = 1,
238
    .minimum_version_id = 1,
239
    .minimum_version_id_old = 1,
240
    .pre_save = vmstate_pci_status_pre_save,
241
    .fields      = (VMStateField []) {
242
        VMSTATE_UINT32(up, struct pci_status),
243
        VMSTATE_UINT32(down, struct pci_status),
244
        VMSTATE_END_OF_LIST()
245
    }
246
};
247

    
248
static int acpi_load_old(QEMUFile *f, void *opaque, int version_id)
249
{
250
    PIIX4PMState *s = opaque;
251
    int ret, i;
252
    uint16_t temp;
253

    
254
    ret = pci_device_load(PCI_DEVICE(s), f);
255
    if (ret < 0) {
256
        return ret;
257
    }
258
    qemu_get_be16s(f, &s->ar.pm1.evt.sts);
259
    qemu_get_be16s(f, &s->ar.pm1.evt.en);
260
    qemu_get_be16s(f, &s->ar.pm1.cnt.cnt);
261

    
262
    ret = vmstate_load_state(f, &vmstate_apm, &s->apm, 1);
263
    if (ret) {
264
        return ret;
265
    }
266

    
267
    timer_get(f, s->ar.tmr.timer);
268
    qemu_get_sbe64s(f, &s->ar.tmr.overflow_time);
269

    
270
    qemu_get_be16s(f, (uint16_t *)s->ar.gpe.sts);
271
    for (i = 0; i < 3; i++) {
272
        qemu_get_be16s(f, &temp);
273
    }
274

    
275
    qemu_get_be16s(f, (uint16_t *)s->ar.gpe.en);
276
    for (i = 0; i < 3; i++) {
277
        qemu_get_be16s(f, &temp);
278
    }
279

    
280
    ret = vmstate_load_state(f, &vmstate_pci_status, &s->pci0_status, 1);
281
    return ret;
282
}
283

    
284
/* qemu-kvm 1.2 uses version 3 but advertised as 2
285
 * To support incoming qemu-kvm 1.2 migration, change version_id
286
 * and minimum_version_id to 2 below (which breaks migration from
287
 * qemu 1.2).
288
 *
289
 */
290
static const VMStateDescription vmstate_acpi = {
291
    .name = "piix4_pm",
292
    .version_id = 3,
293
    .minimum_version_id = 3,
294
    .minimum_version_id_old = 1,
295
    .load_state_old = acpi_load_old,
296
    .post_load = vmstate_acpi_post_load,
297
    .fields      = (VMStateField []) {
298
        VMSTATE_PCI_DEVICE(parent_obj, PIIX4PMState),
299
        VMSTATE_UINT16(ar.pm1.evt.sts, PIIX4PMState),
300
        VMSTATE_UINT16(ar.pm1.evt.en, PIIX4PMState),
301
        VMSTATE_UINT16(ar.pm1.cnt.cnt, PIIX4PMState),
302
        VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
303
        VMSTATE_TIMER(ar.tmr.timer, PIIX4PMState),
304
        VMSTATE_INT64(ar.tmr.overflow_time, PIIX4PMState),
305
        VMSTATE_STRUCT(ar.gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
306
        VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
307
                       struct pci_status),
308
        VMSTATE_END_OF_LIST()
309
    }
310
};
311

    
312
static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots)
313
{
314
    BusChild *kid, *next;
315
    BusState *bus = qdev_get_parent_bus(DEVICE(s));
316
    int slot = ffs(slots) - 1;
317
    bool slot_free = true;
318

    
319
    /* Mark request as complete */
320
    s->pci0_status.down &= ~(1U << slot);
321

    
322
    QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
323
        DeviceState *qdev = kid->child;
324
        PCIDevice *dev = PCI_DEVICE(qdev);
325
        PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
326
        if (PCI_SLOT(dev->devfn) == slot) {
327
            if (pc->no_hotplug) {
328
                slot_free = false;
329
            } else {
330
                object_unparent(OBJECT(qdev));
331
            }
332
        }
333
    }
334
    if (slot_free) {
335
        s->pci0_slot_device_present &= ~(1U << slot);
336
    }
337
}
338

    
339
static void piix4_update_hotplug(PIIX4PMState *s)
340
{
341
    BusState *bus = qdev_get_parent_bus(DEVICE(s));
342
    BusChild *kid, *next;
343

    
344
    /* Execute any pending removes during reset */
345
    while (s->pci0_status.down) {
346
        acpi_piix_eject_slot(s, s->pci0_status.down);
347
    }
348

    
349
    s->pci0_hotplug_enable = ~0;
350
    s->pci0_slot_device_present = 0;
351

    
352
    QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
353
        DeviceState *qdev = kid->child;
354
        PCIDevice *pdev = PCI_DEVICE(qdev);
355
        PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev);
356
        int slot = PCI_SLOT(pdev->devfn);
357

    
358
        if (pc->no_hotplug) {
359
            s->pci0_hotplug_enable &= ~(1U << slot);
360
        }
361

    
362
        s->pci0_slot_device_present |= (1U << slot);
363
    }
364
}
365

    
366
static void piix4_reset(void *opaque)
367
{
368
    PIIX4PMState *s = opaque;
369
    PCIDevice *d = PCI_DEVICE(s);
370
    uint8_t *pci_conf = d->config;
371

    
372
    pci_conf[0x58] = 0;
373
    pci_conf[0x59] = 0;
374
    pci_conf[0x5a] = 0;
375
    pci_conf[0x5b] = 0;
376

    
377
    pci_conf[0x40] = 0x01; /* PM io base read only bit */
378
    pci_conf[0x80] = 0;
379

    
380
    if (s->kvm_enabled) {
381
        /* Mark SMM as already inited (until KVM supports SMM). */
382
        pci_conf[0x5B] = 0x02;
383
    }
384
    pm_io_space_update(s);
385
    piix4_update_hotplug(s);
386
}
387

    
388
static void piix4_pm_powerdown_req(Notifier *n, void *opaque)
389
{
390
    PIIX4PMState *s = container_of(n, PIIX4PMState, powerdown_notifier);
391

    
392
    assert(s != NULL);
393
    acpi_pm1_evt_power_down(&s->ar);
394
}
395

    
396
static void piix4_pm_machine_ready(Notifier *n, void *opaque)
397
{
398
    PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
399
    PCIDevice *d = PCI_DEVICE(s);
400
    MemoryRegion *io_as = pci_address_space_io(d);
401
    uint8_t *pci_conf;
402

    
403
    pci_conf = d->config;
404
    pci_conf[0x5f] = 0x10 |
405
        (memory_region_present(io_as, 0x378) ? 0x80 : 0);
406
    pci_conf[0x63] = 0x60;
407
    pci_conf[0x67] = (memory_region_present(io_as, 0x3f8) ? 0x08 : 0) |
408
        (memory_region_present(io_as, 0x2f8) ? 0x90 : 0);
409
}
410

    
411
static void piix4_pm_add_propeties(PIIX4PMState *s)
412
{
413
    static const uint8_t acpi_enable_cmd = ACPI_ENABLE;
414
    static const uint8_t acpi_disable_cmd = ACPI_DISABLE;
415
    static const uint32_t gpe0_blk = GPE_BASE;
416
    static const uint32_t gpe0_blk_len = GPE_LEN;
417
    static const uint16_t sci_int = 9;
418

    
419
    object_property_add_uint8_ptr(OBJECT(s), ACPI_PM_PROP_ACPI_ENABLE_CMD,
420
                                  &acpi_enable_cmd, NULL);
421
    object_property_add_uint8_ptr(OBJECT(s), ACPI_PM_PROP_ACPI_DISABLE_CMD,
422
                                  &acpi_disable_cmd, NULL);
423
    object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_GPE0_BLK,
424
                                  &gpe0_blk, NULL);
425
    object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_GPE0_BLK_LEN,
426
                                  &gpe0_blk_len, NULL);
427
    object_property_add_uint16_ptr(OBJECT(s), ACPI_PM_PROP_SCI_INT,
428
                                  &sci_int, NULL);
429
    object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_PM_IO_BASE,
430
                                  &s->io_base, NULL);
431
}
432

    
433
static int piix4_pm_initfn(PCIDevice *dev)
434
{
435
    PIIX4PMState *s = PIIX4_PM(dev);
436
    uint8_t *pci_conf;
437

    
438
    pci_conf = dev->config;
439
    pci_conf[0x06] = 0x80;
440
    pci_conf[0x07] = 0x02;
441
    pci_conf[0x09] = 0x00;
442
    pci_conf[0x3d] = 0x01; // interrupt pin 1
443

    
444
    /* APM */
445
    apm_init(dev, &s->apm, apm_ctrl_changed, s);
446

    
447
    if (s->kvm_enabled) {
448
        /* Mark SMM as already inited to prevent SMM from running.  KVM does not
449
         * support SMM mode. */
450
        pci_conf[0x5B] = 0x02;
451
    }
452

    
453
    /* XXX: which specification is used ? The i82731AB has different
454
       mappings */
455
    pci_conf[0x90] = s->smb_io_base | 1;
456
    pci_conf[0x91] = s->smb_io_base >> 8;
457
    pci_conf[0xd2] = 0x09;
458
    pm_smbus_init(DEVICE(dev), &s->smb);
459
    memory_region_set_enabled(&s->smb.io, pci_conf[0xd2] & 1);
460
    memory_region_add_subregion(pci_address_space_io(dev),
461
                                s->smb_io_base, &s->smb.io);
462

    
463
    memory_region_init(&s->io, OBJECT(s), "piix4-pm", 64);
464
    memory_region_set_enabled(&s->io, false);
465
    memory_region_add_subregion(pci_address_space_io(dev),
466
                                0, &s->io);
467

    
468
    acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
469
    acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
470
    acpi_pm1_cnt_init(&s->ar, &s->io, s->s4_val);
471
    acpi_gpe_init(&s->ar, GPE_LEN);
472

    
473
    s->powerdown_notifier.notify = piix4_pm_powerdown_req;
474
    qemu_register_powerdown_notifier(&s->powerdown_notifier);
475

    
476
    s->machine_ready.notify = piix4_pm_machine_ready;
477
    qemu_add_machine_init_done_notifier(&s->machine_ready);
478
    qemu_register_reset(piix4_reset, s);
479

    
480
    piix4_acpi_system_hot_add_init(pci_address_space_io(dev), dev->bus, s);
481

    
482
    piix4_pm_add_propeties(s);
483
    return 0;
484
}
485

    
486
Object *piix4_pm_find(void)
487
{
488
    bool ambig;
489
    Object *o = object_resolve_path_type("", TYPE_PIIX4_PM, &ambig);
490

    
491
    if (ambig || !o) {
492
        return NULL;
493
    }
494
    return o;
495
}
496

    
497
i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
498
                       qemu_irq sci_irq, qemu_irq smi_irq,
499
                       int kvm_enabled, FWCfgState *fw_cfg)
500
{
501
    DeviceState *dev;
502
    PIIX4PMState *s;
503

    
504
    dev = DEVICE(pci_create(bus, devfn, TYPE_PIIX4_PM));
505
    qdev_prop_set_uint32(dev, "smb_io_base", smb_io_base);
506

    
507
    s = PIIX4_PM(dev);
508
    s->irq = sci_irq;
509
    s->smi_irq = smi_irq;
510
    s->kvm_enabled = kvm_enabled;
511

    
512
    qdev_init_nofail(dev);
513

    
514
    if (fw_cfg) {
515
        uint8_t suspend[6] = {128, 0, 0, 129, 128, 128};
516
        suspend[3] = 1 | ((!s->disable_s3) << 7);
517
        suspend[4] = s->s4_val | ((!s->disable_s4) << 7);
518

    
519
        fw_cfg_add_file(fw_cfg, "etc/system-states", g_memdup(suspend, 6), 6);
520
    }
521

    
522
    return s->smb.smbus;
523
}
524

    
525
static Property piix4_pm_properties[] = {
526
    DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
527
    DEFINE_PROP_UINT8(ACPI_PM_PROP_S3_DISABLED, PIIX4PMState, disable_s3, 0),
528
    DEFINE_PROP_UINT8(ACPI_PM_PROP_S4_DISABLED, PIIX4PMState, disable_s4, 0),
529
    DEFINE_PROP_UINT8(ACPI_PM_PROP_S4_VAL, PIIX4PMState, s4_val, 2),
530
    DEFINE_PROP_END_OF_LIST(),
531
};
532

    
533
static void piix4_pm_class_init(ObjectClass *klass, void *data)
534
{
535
    DeviceClass *dc = DEVICE_CLASS(klass);
536
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
537

    
538
    k->no_hotplug = 1;
539
    k->init = piix4_pm_initfn;
540
    k->config_write = pm_write_config;
541
    k->vendor_id = PCI_VENDOR_ID_INTEL;
542
    k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3;
543
    k->revision = 0x03;
544
    k->class_id = PCI_CLASS_BRIDGE_OTHER;
545
    dc->desc = "PM";
546
    dc->no_user = 1;
547
    dc->vmsd = &vmstate_acpi;
548
    dc->props = piix4_pm_properties;
549
}
550

    
551
static const TypeInfo piix4_pm_info = {
552
    .name          = TYPE_PIIX4_PM,
553
    .parent        = TYPE_PCI_DEVICE,
554
    .instance_size = sizeof(PIIX4PMState),
555
    .class_init    = piix4_pm_class_init,
556
};
557

    
558
static void piix4_pm_register_types(void)
559
{
560
    type_register_static(&piix4_pm_info);
561
}
562

    
563
type_init(piix4_pm_register_types)
564

    
565
static uint64_t gpe_readb(void *opaque, hwaddr addr, unsigned width)
566
{
567
    PIIX4PMState *s = opaque;
568
    uint32_t val = acpi_gpe_ioport_readb(&s->ar, addr);
569

    
570
    PIIX4_DPRINTF("gpe read %" HWADDR_PRIx " == %" PRIu32 "\n", addr, val);
571
    return val;
572
}
573

    
574
static void gpe_writeb(void *opaque, hwaddr addr, uint64_t val,
575
                       unsigned width)
576
{
577
    PIIX4PMState *s = opaque;
578

    
579
    acpi_gpe_ioport_writeb(&s->ar, addr, val);
580
    pm_update_sci(s);
581

    
582
    PIIX4_DPRINTF("gpe write %" HWADDR_PRIx " <== %" PRIu64 "\n", addr, val);
583
}
584

    
585
static const MemoryRegionOps piix4_gpe_ops = {
586
    .read = gpe_readb,
587
    .write = gpe_writeb,
588
    .valid.min_access_size = 1,
589
    .valid.max_access_size = 4,
590
    .impl.min_access_size = 1,
591
    .impl.max_access_size = 1,
592
    .endianness = DEVICE_LITTLE_ENDIAN,
593
};
594

    
595
static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
596
{
597
    PIIX4PMState *s = opaque;
598
    uint32_t val = 0;
599

    
600
    switch (addr) {
601
    case PCI_UP_BASE - PCI_HOTPLUG_ADDR:
602
        /* Manufacture an "up" value to cause a device check on any hotplug
603
         * slot with a device.  Extra device checks are harmless. */
604
        val = s->pci0_slot_device_present & s->pci0_hotplug_enable;
605
        PIIX4_DPRINTF("pci_up_read %" PRIu32 "\n", val);
606
        break;
607
    case PCI_DOWN_BASE - PCI_HOTPLUG_ADDR:
608
        val = s->pci0_status.down;
609
        PIIX4_DPRINTF("pci_down_read %" PRIu32 "\n", val);
610
        break;
611
    case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
612
        /* No feature defined yet */
613
        PIIX4_DPRINTF("pci_features_read %" PRIu32 "\n", val);
614
        break;
615
    case PCI_RMV_BASE - PCI_HOTPLUG_ADDR:
616
        val = s->pci0_hotplug_enable;
617
        break;
618
    default:
619
        break;
620
    }
621

    
622
    return val;
623
}
624

    
625
static void pci_write(void *opaque, hwaddr addr, uint64_t data,
626
                      unsigned int size)
627
{
628
    switch (addr) {
629
    case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
630
        acpi_piix_eject_slot(opaque, (uint32_t)data);
631
        PIIX4_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n",
632
                      addr, data);
633
        break;
634
    default:
635
        break;
636
    }
637
}
638

    
639
static const MemoryRegionOps piix4_pci_ops = {
640
    .read = pci_read,
641
    .write = pci_write,
642
    .endianness = DEVICE_LITTLE_ENDIAN,
643
    .valid = {
644
        .min_access_size = 4,
645
        .max_access_size = 4,
646
    },
647
};
648

    
649
static uint64_t cpu_status_read(void *opaque, hwaddr addr, unsigned int size)
650
{
651
    PIIX4PMState *s = opaque;
652
    CPUStatus *cpus = &s->gpe_cpu;
653
    uint64_t val = cpus->sts[addr];
654

    
655
    return val;
656
}
657

    
658
static void cpu_status_write(void *opaque, hwaddr addr, uint64_t data,
659
                             unsigned int size)
660
{
661
    /* TODO: implement VCPU removal on guest signal that CPU can be removed */
662
}
663

    
664
static const MemoryRegionOps cpu_hotplug_ops = {
665
    .read = cpu_status_read,
666
    .write = cpu_status_write,
667
    .endianness = DEVICE_LITTLE_ENDIAN,
668
    .valid = {
669
        .min_access_size = 1,
670
        .max_access_size = 1,
671
    },
672
};
673

    
674
typedef enum {
675
    PLUG,
676
    UNPLUG,
677
} HotplugEventType;
678

    
679
static void piix4_cpu_hotplug_req(PIIX4PMState *s, CPUState *cpu,
680
                                  HotplugEventType action)
681
{
682
    CPUStatus *g = &s->gpe_cpu;
683
    ACPIGPE *gpe = &s->ar.gpe;
684
    CPUClass *k = CPU_GET_CLASS(cpu);
685
    int64_t cpu_id;
686

    
687
    assert(s != NULL);
688

    
689
    *gpe->sts = *gpe->sts | PIIX4_CPU_HOTPLUG_STATUS;
690
    cpu_id = k->get_arch_id(CPU(cpu));
691
    if (action == PLUG) {
692
        g->sts[cpu_id / 8] |= (1 << (cpu_id % 8));
693
    } else {
694
        g->sts[cpu_id / 8] &= ~(1 << (cpu_id % 8));
695
    }
696
    pm_update_sci(s);
697
}
698

    
699
static void piix4_cpu_added_req(Notifier *n, void *opaque)
700
{
701
    PIIX4PMState *s = container_of(n, PIIX4PMState, cpu_added_notifier);
702

    
703
    piix4_cpu_hotplug_req(s, CPU(opaque), PLUG);
704
}
705

    
706
static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
707
                                PCIHotplugState state);
708

    
709
static void piix4_acpi_system_hot_add_init(MemoryRegion *parent,
710
                                           PCIBus *bus, PIIX4PMState *s)
711
{
712
    CPUState *cpu;
713

    
714
    memory_region_init_io(&s->io_gpe, OBJECT(s), &piix4_gpe_ops, s,
715
                          "acpi-gpe0", GPE_LEN);
716
    memory_region_add_subregion(parent, GPE_BASE, &s->io_gpe);
717

    
718
    memory_region_init_io(&s->io_pci, OBJECT(s), &piix4_pci_ops, s,
719
                          "acpi-pci-hotplug", PCI_HOTPLUG_SIZE);
720
    memory_region_add_subregion(parent, PCI_HOTPLUG_ADDR,
721
                                &s->io_pci);
722
    pci_bus_hotplug(bus, piix4_device_hotplug, DEVICE(s));
723

    
724
    CPU_FOREACH(cpu) {
725
        CPUClass *cc = CPU_GET_CLASS(cpu);
726
        int64_t id = cc->get_arch_id(cpu);
727

    
728
        g_assert((id / 8) < PIIX4_PROC_LEN);
729
        s->gpe_cpu.sts[id / 8] |= (1 << (id % 8));
730
    }
731
    memory_region_init_io(&s->io_cpu, OBJECT(s), &cpu_hotplug_ops, s,
732
                          "acpi-cpu-hotplug", PIIX4_PROC_LEN);
733
    memory_region_add_subregion(parent, PIIX4_PROC_BASE, &s->io_cpu);
734
    s->cpu_added_notifier.notify = piix4_cpu_added_req;
735
    qemu_register_cpu_added_notifier(&s->cpu_added_notifier);
736
}
737

    
738
static void enable_device(PIIX4PMState *s, int slot)
739
{
740
    s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
741
    s->pci0_slot_device_present |= (1U << slot);
742
}
743

    
744
static void disable_device(PIIX4PMState *s, int slot)
745
{
746
    s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
747
    s->pci0_status.down |= (1U << slot);
748
}
749

    
750
static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
751
                                PCIHotplugState state)
752
{
753
    int slot = PCI_SLOT(dev->devfn);
754
    PIIX4PMState *s = PIIX4_PM(qdev);
755

    
756
    /* Don't send event when device is enabled during qemu machine creation:
757
     * it is present on boot, no hotplug event is necessary. We do send an
758
     * event when the device is disabled later. */
759
    if (state == PCI_COLDPLUG_ENABLED) {
760
        s->pci0_slot_device_present |= (1U << slot);
761
        return 0;
762
    }
763

    
764
    if (state == PCI_HOTPLUG_ENABLED) {
765
        enable_device(s, slot);
766
    } else {
767
        disable_device(s, slot);
768
    }
769

    
770
    pm_update_sci(s);
771

    
772
    return 0;
773
}