Statistics
| Branch: | Revision:

root / hw / acpi_piix4.c @ 83f7d43a

History | View | Annotate | Download (15.8 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.h"
22
#include "pc.h"
23
#include "apm.h"
24
#include "pm_smbus.h"
25
#include "pci.h"
26
#include "acpi.h"
27
#include "sysemu.h"
28
#include "range.h"
29
#include "ioport.h"
30

    
31
//#define DEBUG
32

    
33
#ifdef DEBUG
34
# define PIIX4_DPRINTF(format, ...)     printf(format, ## __VA_ARGS__)
35
#else
36
# define PIIX4_DPRINTF(format, ...)     do { } while (0)
37
#endif
38

    
39
#define ACPI_DBG_IO_ADDR  0xb044
40

    
41
#define GPE_BASE 0xafe0
42
#define GPE_LEN 4
43
#define PCI_BASE 0xae00
44
#define PCI_EJ_BASE 0xae08
45
#define PCI_RMV_BASE 0xae0c
46

    
47
#define PIIX4_PCI_HOTPLUG_STATUS 2
48

    
49
struct pci_status {
50
    uint32_t up;
51
    uint32_t down;
52
};
53

    
54
typedef struct PIIX4PMState {
55
    PCIDevice dev;
56
    IORange ioport;
57
    ACPIPM1EVT pm1a;
58
    ACPIPM1CNT pm1_cnt;
59

    
60
    APMState apm;
61

    
62
    ACPIPMTimer tmr;
63

    
64
    PMSMBus smb;
65
    uint32_t smb_io_base;
66

    
67
    qemu_irq irq;
68
    qemu_irq smi_irq;
69
    int kvm_enabled;
70
    Notifier machine_ready;
71

    
72
    /* for pci hotplug */
73
    ACPIGPE gpe;
74
    struct pci_status pci0_status;
75
    uint32_t pci0_hotplug_enable;
76
} PIIX4PMState;
77

    
78
static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s);
79

    
80
#define ACPI_ENABLE 0xf1
81
#define ACPI_DISABLE 0xf0
82

    
83
static void pm_update_sci(PIIX4PMState *s)
84
{
85
    int sci_level, pmsts;
86

    
87
    pmsts = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
88
    sci_level = (((pmsts & s->pm1a.en) &
89
                  (ACPI_BITMASK_RT_CLOCK_ENABLE |
90
                   ACPI_BITMASK_POWER_BUTTON_ENABLE |
91
                   ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
92
                   ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
93
        (((s->gpe.sts[0] & s->gpe.en[0]) & PIIX4_PCI_HOTPLUG_STATUS) != 0);
94

    
95
    qemu_set_irq(s->irq, sci_level);
96
    /* schedule a timer interruption if needed */
97
    acpi_pm_tmr_update(&s->tmr, (s->pm1a.en & ACPI_BITMASK_TIMER_ENABLE) &&
98
                       !(pmsts & ACPI_BITMASK_TIMER_STATUS));
99
}
100

    
101
static void pm_tmr_timer(ACPIPMTimer *tmr)
102
{
103
    PIIX4PMState *s = container_of(tmr, PIIX4PMState, tmr);
104
    pm_update_sci(s);
105
}
106

    
107
static void pm_ioport_write(IORange *ioport, uint64_t addr, unsigned width,
108
                            uint64_t val)
109
{
110
    PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
111

    
112
    if (width != 2) {
113
        PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
114
                      (unsigned)addr, width, (unsigned)val);
115
    }
116

    
117
    switch(addr) {
118
    case 0x00:
119
        acpi_pm1_evt_write_sts(&s->pm1a, &s->tmr, val);
120
        pm_update_sci(s);
121
        break;
122
    case 0x02:
123
        s->pm1a.en = val;
124
        pm_update_sci(s);
125
        break;
126
    case 0x04:
127
        acpi_pm1_cnt_write(&s->pm1a, &s->pm1_cnt, val);
128
        break;
129
    default:
130
        break;
131
    }
132
    PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr,
133
                  (unsigned int)val);
134
}
135

    
136
static void pm_ioport_read(IORange *ioport, uint64_t addr, unsigned width,
137
                            uint64_t *data)
138
{
139
    PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
140
    uint32_t val;
141

    
142
    switch(addr) {
143
    case 0x00:
144
        val = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
145
        break;
146
    case 0x02:
147
        val = s->pm1a.en;
148
        break;
149
    case 0x04:
150
        val = s->pm1_cnt.cnt;
151
        break;
152
    case 0x08:
153
        val = acpi_pm_tmr_get(&s->tmr);
154
        break;
155
    default:
156
        val = 0;
157
        break;
158
    }
159
    PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr, val);
160
    *data = val;
161
}
162

    
163
static const IORangeOps pm_iorange_ops = {
164
    .read = pm_ioport_read,
165
    .write = pm_ioport_write,
166
};
167

    
168
static void apm_ctrl_changed(uint32_t val, void *arg)
169
{
170
    PIIX4PMState *s = arg;
171

    
172
    /* ACPI specs 3.0, 4.7.2.5 */
173
    acpi_pm1_cnt_update(&s->pm1_cnt, val == ACPI_ENABLE, val == ACPI_DISABLE);
174

    
175
    if (s->dev.config[0x5b] & (1 << 1)) {
176
        if (s->smi_irq) {
177
            qemu_irq_raise(s->smi_irq);
178
        }
179
    }
180
}
181

    
182
static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
183
{
184
    PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val);
185
}
186

    
187
static void pm_io_space_update(PIIX4PMState *s)
188
{
189
    uint32_t pm_io_base;
190

    
191
    if (s->dev.config[0x80] & 1) {
192
        pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
193
        pm_io_base &= 0xffc0;
194

    
195
        /* XXX: need to improve memory and ioport allocation */
196
        PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base);
197
        iorange_init(&s->ioport, &pm_iorange_ops, pm_io_base, 64);
198
        ioport_register(&s->ioport);
199
    }
200
}
201

    
202
static void pm_write_config(PCIDevice *d,
203
                            uint32_t address, uint32_t val, int len)
204
{
205
    pci_default_write_config(d, address, val, len);
206
    if (range_covers_byte(address, len, 0x80))
207
        pm_io_space_update((PIIX4PMState *)d);
208
}
209

    
210
static int vmstate_acpi_post_load(void *opaque, int version_id)
211
{
212
    PIIX4PMState *s = opaque;
213

    
214
    pm_io_space_update(s);
215
    return 0;
216
}
217

    
218
#define VMSTATE_GPE_ARRAY(_field, _state)                            \
219
 {                                                                   \
220
     .name       = (stringify(_field)),                              \
221
     .version_id = 0,                                                \
222
     .num        = GPE_LEN,                                          \
223
     .info       = &vmstate_info_uint16,                             \
224
     .size       = sizeof(uint16_t),                                 \
225
     .flags      = VMS_ARRAY | VMS_POINTER,                          \
226
     .offset     = vmstate_offset_pointer(_state, _field, uint8_t),  \
227
 }
228

    
229
static const VMStateDescription vmstate_gpe = {
230
    .name = "gpe",
231
    .version_id = 1,
232
    .minimum_version_id = 1,
233
    .minimum_version_id_old = 1,
234
    .fields      = (VMStateField []) {
235
        VMSTATE_GPE_ARRAY(sts, ACPIGPE),
236
        VMSTATE_GPE_ARRAY(en, ACPIGPE),
237
        VMSTATE_END_OF_LIST()
238
    }
239
};
240

    
241
static const VMStateDescription vmstate_pci_status = {
242
    .name = "pci_status",
243
    .version_id = 1,
244
    .minimum_version_id = 1,
245
    .minimum_version_id_old = 1,
246
    .fields      = (VMStateField []) {
247
        VMSTATE_UINT32(up, struct pci_status),
248
        VMSTATE_UINT32(down, struct pci_status),
249
        VMSTATE_END_OF_LIST()
250
    }
251
};
252

    
253
static const VMStateDescription vmstate_acpi = {
254
    .name = "piix4_pm",
255
    .version_id = 2,
256
    .minimum_version_id = 1,
257
    .minimum_version_id_old = 1,
258
    .post_load = vmstate_acpi_post_load,
259
    .fields      = (VMStateField []) {
260
        VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
261
        VMSTATE_UINT16(pm1a.sts, PIIX4PMState),
262
        VMSTATE_UINT16(pm1a.en, PIIX4PMState),
263
        VMSTATE_UINT16(pm1_cnt.cnt, PIIX4PMState),
264
        VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
265
        VMSTATE_TIMER(tmr.timer, PIIX4PMState),
266
        VMSTATE_INT64(tmr.overflow_time, PIIX4PMState),
267
        VMSTATE_STRUCT(gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
268
        VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
269
                       struct pci_status),
270
        VMSTATE_END_OF_LIST()
271
    }
272
};
273

    
274
static void piix4_update_hotplug(PIIX4PMState *s)
275
{
276
    PCIDevice *dev = &s->dev;
277
    BusState *bus = qdev_get_parent_bus(&dev->qdev);
278
    DeviceState *qdev, *next;
279

    
280
    s->pci0_hotplug_enable = ~0;
281

    
282
    QTAILQ_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
283
        PCIDevice *pdev = PCI_DEVICE(qdev);
284
        PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev);
285
        int slot = PCI_SLOT(pdev->devfn);
286

    
287
        if (pc->no_hotplug) {
288
            s->pci0_hotplug_enable &= ~(1 << slot);
289
        }
290
    }
291
}
292

    
293
static void piix4_reset(void *opaque)
294
{
295
    PIIX4PMState *s = opaque;
296
    uint8_t *pci_conf = s->dev.config;
297

    
298
    pci_conf[0x58] = 0;
299
    pci_conf[0x59] = 0;
300
    pci_conf[0x5a] = 0;
301
    pci_conf[0x5b] = 0;
302

    
303
    if (s->kvm_enabled) {
304
        /* Mark SMM as already inited (until KVM supports SMM). */
305
        pci_conf[0x5B] = 0x02;
306
    }
307
    piix4_update_hotplug(s);
308
}
309

    
310
static void piix4_powerdown(void *opaque, int irq, int power_failing)
311
{
312
    PIIX4PMState *s = opaque;
313
    ACPIPM1EVT *pm1a = s? &s->pm1a: NULL;
314
    ACPIPMTimer *tmr = s? &s->tmr: NULL;
315

    
316
    acpi_pm1_evt_power_down(pm1a, tmr);
317
}
318

    
319
static void piix4_pm_machine_ready(Notifier *n, void *opaque)
320
{
321
    PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
322
    uint8_t *pci_conf;
323

    
324
    pci_conf = s->dev.config;
325
    pci_conf[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
326
    pci_conf[0x63] = 0x60;
327
    pci_conf[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
328
        (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
329

    
330
}
331

    
332
static int piix4_pm_initfn(PCIDevice *dev)
333
{
334
    PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev);
335
    uint8_t *pci_conf;
336

    
337
    pci_conf = s->dev.config;
338
    pci_conf[0x06] = 0x80;
339
    pci_conf[0x07] = 0x02;
340
    pci_conf[0x09] = 0x00;
341
    pci_conf[0x3d] = 0x01; // interrupt pin 1
342

    
343
    pci_conf[0x40] = 0x01; /* PM io base read only bit */
344

    
345
    /* APM */
346
    apm_init(&s->apm, apm_ctrl_changed, s);
347

    
348
    register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
349

    
350
    if (s->kvm_enabled) {
351
        /* Mark SMM as already inited to prevent SMM from running.  KVM does not
352
         * support SMM mode. */
353
        pci_conf[0x5B] = 0x02;
354
    }
355

    
356
    /* XXX: which specification is used ? The i82731AB has different
357
       mappings */
358
    pci_conf[0x90] = s->smb_io_base | 1;
359
    pci_conf[0x91] = s->smb_io_base >> 8;
360
    pci_conf[0xd2] = 0x09;
361
    register_ioport_write(s->smb_io_base, 64, 1, smb_ioport_writeb, &s->smb);
362
    register_ioport_read(s->smb_io_base, 64, 1, smb_ioport_readb, &s->smb);
363

    
364
    acpi_pm_tmr_init(&s->tmr, pm_tmr_timer);
365
    acpi_gpe_init(&s->gpe, GPE_LEN);
366

    
367
    qemu_system_powerdown = *qemu_allocate_irqs(piix4_powerdown, s, 1);
368

    
369
    pm_smbus_init(&s->dev.qdev, &s->smb);
370
    s->machine_ready.notify = piix4_pm_machine_ready;
371
    qemu_add_machine_init_done_notifier(&s->machine_ready);
372
    qemu_register_reset(piix4_reset, s);
373
    piix4_acpi_system_hot_add_init(dev->bus, s);
374

    
375
    return 0;
376
}
377

    
378
i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
379
                       qemu_irq sci_irq, qemu_irq cmos_s3, qemu_irq smi_irq,
380
                       int kvm_enabled)
381
{
382
    PCIDevice *dev;
383
    PIIX4PMState *s;
384

    
385
    dev = pci_create(bus, devfn, "PIIX4_PM");
386
    qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
387

    
388
    s = DO_UPCAST(PIIX4PMState, dev, dev);
389
    s->irq = sci_irq;
390
    acpi_pm1_cnt_init(&s->pm1_cnt, cmos_s3);
391
    s->smi_irq = smi_irq;
392
    s->kvm_enabled = kvm_enabled;
393

    
394
    qdev_init_nofail(&dev->qdev);
395

    
396
    return s->smb.smbus;
397
}
398

    
399
static Property piix4_pm_properties[] = {
400
    DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
401
    DEFINE_PROP_END_OF_LIST(),
402
};
403

    
404
static void piix4_pm_class_init(ObjectClass *klass, void *data)
405
{
406
    DeviceClass *dc = DEVICE_CLASS(klass);
407
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
408

    
409
    k->no_hotplug = 1;
410
    k->init = piix4_pm_initfn;
411
    k->config_write = pm_write_config;
412
    k->vendor_id = PCI_VENDOR_ID_INTEL;
413
    k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3;
414
    k->revision = 0x03;
415
    k->class_id = PCI_CLASS_BRIDGE_OTHER;
416
    dc->desc = "PM";
417
    dc->no_user = 1;
418
    dc->vmsd = &vmstate_acpi;
419
    dc->props = piix4_pm_properties;
420
}
421

    
422
static TypeInfo piix4_pm_info = {
423
    .name          = "PIIX4_PM",
424
    .parent        = TYPE_PCI_DEVICE,
425
    .instance_size = sizeof(PIIX4PMState),
426
    .class_init    = piix4_pm_class_init,
427
};
428

    
429
static void piix4_pm_register_types(void)
430
{
431
    type_register_static(&piix4_pm_info);
432
}
433

    
434
type_init(piix4_pm_register_types)
435

    
436
static uint32_t gpe_readb(void *opaque, uint32_t addr)
437
{
438
    PIIX4PMState *s = opaque;
439
    uint32_t val = acpi_gpe_ioport_readb(&s->gpe, addr);
440

    
441
    PIIX4_DPRINTF("gpe read %x == %x\n", addr, val);
442
    return val;
443
}
444

    
445
static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
446
{
447
    PIIX4PMState *s = opaque;
448

    
449
    acpi_gpe_ioport_writeb(&s->gpe, addr, val);
450
    pm_update_sci(s);
451

    
452
    PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val);
453
}
454

    
455
static uint32_t pcihotplug_read(void *opaque, uint32_t addr)
456
{
457
    uint32_t val = 0;
458
    struct pci_status *g = opaque;
459
    switch (addr) {
460
        case PCI_BASE:
461
            val = g->up;
462
            break;
463
        case PCI_BASE + 4:
464
            val = g->down;
465
            break;
466
        default:
467
            break;
468
    }
469

    
470
    PIIX4_DPRINTF("pcihotplug read %x == %x\n", addr, val);
471
    return val;
472
}
473

    
474
static void pcihotplug_write(void *opaque, uint32_t addr, uint32_t val)
475
{
476
    struct pci_status *g = opaque;
477
    switch (addr) {
478
        case PCI_BASE:
479
            g->up = val;
480
            break;
481
        case PCI_BASE + 4:
482
            g->down = val;
483
            break;
484
   }
485

    
486
    PIIX4_DPRINTF("pcihotplug write %x <== %d\n", addr, val);
487
}
488

    
489
static uint32_t pciej_read(void *opaque, uint32_t addr)
490
{
491
    PIIX4_DPRINTF("pciej read %x\n", addr);
492
    return 0;
493
}
494

    
495
static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
496
{
497
    BusState *bus = opaque;
498
    DeviceState *qdev, *next;
499
    int slot = ffs(val) - 1;
500

    
501
    QTAILQ_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
502
        PCIDevice *dev = PCI_DEVICE(qdev);
503
        PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
504
        if (PCI_SLOT(dev->devfn) == slot && !pc->no_hotplug) {
505
            qdev_free(qdev);
506
        }
507
    }
508

    
509

    
510
    PIIX4_DPRINTF("pciej write %x <== %d\n", addr, val);
511
}
512

    
513
static uint32_t pcirmv_read(void *opaque, uint32_t addr)
514
{
515
    PIIX4PMState *s = opaque;
516

    
517
    return s->pci0_hotplug_enable;
518
}
519

    
520
static void pcirmv_write(void *opaque, uint32_t addr, uint32_t val)
521
{
522
    return;
523
}
524

    
525
static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
526
                                PCIHotplugState state);
527

    
528
static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s)
529
{
530
    struct pci_status *pci0_status = &s->pci0_status;
531

    
532
    register_ioport_write(GPE_BASE, GPE_LEN, 1, gpe_writeb, s);
533
    register_ioport_read(GPE_BASE, GPE_LEN, 1,  gpe_readb, s);
534
    acpi_gpe_blk(&s->gpe, GPE_BASE);
535

    
536
    register_ioport_write(PCI_BASE, 8, 4, pcihotplug_write, pci0_status);
537
    register_ioport_read(PCI_BASE, 8, 4,  pcihotplug_read, pci0_status);
538

    
539
    register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, bus);
540
    register_ioport_read(PCI_EJ_BASE, 4, 4,  pciej_read, bus);
541

    
542
    register_ioport_write(PCI_RMV_BASE, 4, 4, pcirmv_write, s);
543
    register_ioport_read(PCI_RMV_BASE, 4, 4,  pcirmv_read, s);
544

    
545
    pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev);
546
}
547

    
548
static void enable_device(PIIX4PMState *s, int slot)
549
{
550
    s->gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
551
    s->pci0_status.up |= (1 << slot);
552
}
553

    
554
static void disable_device(PIIX4PMState *s, int slot)
555
{
556
    s->gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
557
    s->pci0_status.down |= (1 << slot);
558
}
559

    
560
static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
561
                                PCIHotplugState state)
562
{
563
    int slot = PCI_SLOT(dev->devfn);
564
    PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
565
                                PCI_DEVICE(qdev));
566

    
567
    /* Don't send event when device is enabled during qemu machine creation:
568
     * it is present on boot, no hotplug event is necessary. We do send an
569
     * event when the device is disabled later. */
570
    if (state == PCI_COLDPLUG_ENABLED) {
571
        return 0;
572
    }
573

    
574
    s->pci0_status.up = 0;
575
    s->pci0_status.down = 0;
576
    if (state == PCI_HOTPLUG_ENABLED) {
577
        enable_device(s, slot);
578
    } else {
579
        disable_device(s, slot);
580
    }
581

    
582
    pm_update_sci(s);
583

    
584
    return 0;
585
}