Statistics
| Branch: | Revision:

root / hw / acpi.c @ 06113719

History | View | Annotate | Download (22.6 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
#include "hw.h"
19
#include "pc.h"
20
#include "pci.h"
21
#include "qemu-timer.h"
22
#include "sysemu.h"
23
#include "i2c.h"
24
#include "smbus.h"
25
#include "kvm.h"
26

    
27
//#define DEBUG
28

    
29
/* i82731AB (PIIX4) compatible power management function */
30
#define PM_FREQ 3579545
31

    
32
#define ACPI_DBG_IO_ADDR  0xb044
33

    
34
typedef struct PIIX4PMState {
35
    PCIDevice dev;
36
    uint16_t pmsts;
37
    uint16_t pmen;
38
    uint16_t pmcntrl;
39
    uint8_t apmc;
40
    uint8_t apms;
41
    QEMUTimer *tmr_timer;
42
    int64_t tmr_overflow_time;
43
    i2c_bus *smbus;
44
    uint8_t smb_stat;
45
    uint8_t smb_ctl;
46
    uint8_t smb_cmd;
47
    uint8_t smb_addr;
48
    uint8_t smb_data0;
49
    uint8_t smb_data1;
50
    uint8_t smb_data[32];
51
    uint8_t smb_index;
52
    qemu_irq irq;
53
} PIIX4PMState;
54

    
55
#define RSM_STS (1 << 15)
56
#define PWRBTN_STS (1 << 8)
57
#define RTC_EN (1 << 10)
58
#define PWRBTN_EN (1 << 8)
59
#define GBL_EN (1 << 5)
60
#define TMROF_EN (1 << 0)
61

    
62
#define SCI_EN (1 << 0)
63

    
64
#define SUS_EN (1 << 13)
65

    
66
#define ACPI_ENABLE 0xf1
67
#define ACPI_DISABLE 0xf0
68

    
69
#define SMBHSTSTS 0x00
70
#define SMBHSTCNT 0x02
71
#define SMBHSTCMD 0x03
72
#define SMBHSTADD 0x04
73
#define SMBHSTDAT0 0x05
74
#define SMBHSTDAT1 0x06
75
#define SMBBLKDAT 0x07
76

    
77
static PIIX4PMState *pm_state;
78

    
79
static uint32_t get_pmtmr(PIIX4PMState *s)
80
{
81
    uint32_t d;
82
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
83
    return d & 0xffffff;
84
}
85

    
86
static int get_pmsts(PIIX4PMState *s)
87
{
88
    int64_t d;
89
    int pmsts;
90
    pmsts = s->pmsts;
91
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
92
    if (d >= s->tmr_overflow_time)
93
        s->pmsts |= TMROF_EN;
94
    return s->pmsts;
95
}
96

    
97
static void pm_update_sci(PIIX4PMState *s)
98
{
99
    int sci_level, pmsts;
100
    int64_t expire_time;
101

    
102
    pmsts = get_pmsts(s);
103
    sci_level = (((pmsts & s->pmen) &
104
                  (RTC_EN | PWRBTN_EN | GBL_EN | TMROF_EN)) != 0);
105
    qemu_set_irq(s->irq, sci_level);
106
    /* schedule a timer interruption if needed */
107
    if ((s->pmen & TMROF_EN) && !(pmsts & TMROF_EN)) {
108
        expire_time = muldiv64(s->tmr_overflow_time, ticks_per_sec, PM_FREQ);
109
        qemu_mod_timer(s->tmr_timer, expire_time);
110
    } else {
111
        qemu_del_timer(s->tmr_timer);
112
    }
113
}
114

    
115
static void pm_tmr_timer(void *opaque)
116
{
117
    PIIX4PMState *s = opaque;
118
    pm_update_sci(s);
119
}
120

    
121
static void pm_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
122
{
123
    PIIX4PMState *s = opaque;
124
    addr &= 0x3f;
125
    switch(addr) {
126
    case 0x00:
127
        {
128
            int64_t d;
129
            int pmsts;
130
            pmsts = get_pmsts(s);
131
            if (pmsts & val & TMROF_EN) {
132
                /* if TMRSTS is reset, then compute the new overflow time */
133
                d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
134
                s->tmr_overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
135
            }
136
            s->pmsts &= ~val;
137
            pm_update_sci(s);
138
        }
139
        break;
140
    case 0x02:
141
        s->pmen = val;
142
        pm_update_sci(s);
143
        break;
144
    case 0x04:
145
        {
146
            int sus_typ;
147
            s->pmcntrl = val & ~(SUS_EN);
148
            if (val & SUS_EN) {
149
                /* change suspend type */
150
                sus_typ = (val >> 10) & 7;
151
                switch(sus_typ) {
152
                case 0: /* soft power off */
153
                    qemu_system_shutdown_request();
154
                    break;
155
                case 1:
156
                    /* RSM_STS should be set on resume. Pretend that resume
157
                       was caused by power button */
158
                    s->pmsts |= (RSM_STS | PWRBTN_STS);
159
                    qemu_system_reset_request();
160
#if defined(TARGET_I386)
161
                    cmos_set_s3_resume();
162
#endif
163
                default:
164
                    break;
165
                }
166
            }
167
        }
168
        break;
169
    default:
170
        break;
171
    }
172
#ifdef DEBUG
173
    printf("PM writew port=0x%04x val=0x%04x\n", addr, val);
174
#endif
175
}
176

    
177
static uint32_t pm_ioport_readw(void *opaque, uint32_t addr)
178
{
179
    PIIX4PMState *s = opaque;
180
    uint32_t val;
181

    
182
    addr &= 0x3f;
183
    switch(addr) {
184
    case 0x00:
185
        val = get_pmsts(s);
186
        break;
187
    case 0x02:
188
        val = s->pmen;
189
        break;
190
    case 0x04:
191
        val = s->pmcntrl;
192
        break;
193
    default:
194
        val = 0;
195
        break;
196
    }
197
#ifdef DEBUG
198
    printf("PM readw port=0x%04x val=0x%04x\n", addr, val);
199
#endif
200
    return val;
201
}
202

    
203
static void pm_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
204
{
205
    //    PIIX4PMState *s = opaque;
206
    addr &= 0x3f;
207
#ifdef DEBUG
208
    printf("PM writel port=0x%04x val=0x%08x\n", addr, val);
209
#endif
210
}
211

    
212
static uint32_t pm_ioport_readl(void *opaque, uint32_t addr)
213
{
214
    PIIX4PMState *s = opaque;
215
    uint32_t val;
216

    
217
    addr &= 0x3f;
218
    switch(addr) {
219
    case 0x08:
220
        val = get_pmtmr(s);
221
        break;
222
    default:
223
        val = 0;
224
        break;
225
    }
226
#ifdef DEBUG
227
    printf("PM readl port=0x%04x val=0x%08x\n", addr, val);
228
#endif
229
    return val;
230
}
231

    
232
static void pm_smi_writeb(void *opaque, uint32_t addr, uint32_t val)
233
{
234
    PIIX4PMState *s = opaque;
235
    addr &= 1;
236
#ifdef DEBUG
237
    printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr, val);
238
#endif
239
    if (addr == 0) {
240
        s->apmc = val;
241

    
242
        /* ACPI specs 3.0, 4.7.2.5 */
243
        if (val == ACPI_ENABLE) {
244
            s->pmcntrl |= SCI_EN;
245
        } else if (val == ACPI_DISABLE) {
246
            s->pmcntrl &= ~SCI_EN;
247
        }
248

    
249
        if (s->dev.config[0x5b] & (1 << 1)) {
250
            cpu_interrupt(first_cpu, CPU_INTERRUPT_SMI);
251
        }
252
    } else {
253
        s->apms = val;
254
    }
255
}
256

    
257
static uint32_t pm_smi_readb(void *opaque, uint32_t addr)
258
{
259
    PIIX4PMState *s = opaque;
260
    uint32_t val;
261

    
262
    addr &= 1;
263
    if (addr == 0) {
264
        val = s->apmc;
265
    } else {
266
        val = s->apms;
267
    }
268
#ifdef DEBUG
269
    printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr, val);
270
#endif
271
    return val;
272
}
273

    
274
static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
275
{
276
#if defined(DEBUG)
277
    printf("ACPI: DBG: 0x%08x\n", val);
278
#endif
279
}
280

    
281
static void smb_transaction(PIIX4PMState *s)
282
{
283
    uint8_t prot = (s->smb_ctl >> 2) & 0x07;
284
    uint8_t read = s->smb_addr & 0x01;
285
    uint8_t cmd = s->smb_cmd;
286
    uint8_t addr = s->smb_addr >> 1;
287
    i2c_bus *bus = s->smbus;
288

    
289
#ifdef DEBUG
290
    printf("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
291
#endif
292
    switch(prot) {
293
    case 0x0:
294
        smbus_quick_command(bus, addr, read);
295
        break;
296
    case 0x1:
297
        if (read) {
298
            s->smb_data0 = smbus_receive_byte(bus, addr);
299
        } else {
300
            smbus_send_byte(bus, addr, cmd);
301
        }
302
        break;
303
    case 0x2:
304
        if (read) {
305
            s->smb_data0 = smbus_read_byte(bus, addr, cmd);
306
        } else {
307
            smbus_write_byte(bus, addr, cmd, s->smb_data0);
308
        }
309
        break;
310
    case 0x3:
311
        if (read) {
312
            uint16_t val;
313
            val = smbus_read_word(bus, addr, cmd);
314
            s->smb_data0 = val;
315
            s->smb_data1 = val >> 8;
316
        } else {
317
            smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
318
        }
319
        break;
320
    case 0x5:
321
        if (read) {
322
            s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
323
        } else {
324
            smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
325
        }
326
        break;
327
    default:
328
        goto error;
329
    }
330
    return;
331

    
332
  error:
333
    s->smb_stat |= 0x04;
334
}
335

    
336
static void smb_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
337
{
338
    PIIX4PMState *s = opaque;
339
    addr &= 0x3f;
340
#ifdef DEBUG
341
    printf("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
342
#endif
343
    switch(addr) {
344
    case SMBHSTSTS:
345
        s->smb_stat = 0;
346
        s->smb_index = 0;
347
        break;
348
    case SMBHSTCNT:
349
        s->smb_ctl = val;
350
        if (val & 0x40)
351
            smb_transaction(s);
352
        break;
353
    case SMBHSTCMD:
354
        s->smb_cmd = val;
355
        break;
356
    case SMBHSTADD:
357
        s->smb_addr = val;
358
        break;
359
    case SMBHSTDAT0:
360
        s->smb_data0 = val;
361
        break;
362
    case SMBHSTDAT1:
363
        s->smb_data1 = val;
364
        break;
365
    case SMBBLKDAT:
366
        s->smb_data[s->smb_index++] = val;
367
        if (s->smb_index > 31)
368
            s->smb_index = 0;
369
        break;
370
    default:
371
        break;
372
    }
373
}
374

    
375
static uint32_t smb_ioport_readb(void *opaque, uint32_t addr)
376
{
377
    PIIX4PMState *s = opaque;
378
    uint32_t val;
379

    
380
    addr &= 0x3f;
381
    switch(addr) {
382
    case SMBHSTSTS:
383
        val = s->smb_stat;
384
        break;
385
    case SMBHSTCNT:
386
        s->smb_index = 0;
387
        val = s->smb_ctl & 0x1f;
388
        break;
389
    case SMBHSTCMD:
390
        val = s->smb_cmd;
391
        break;
392
    case SMBHSTADD:
393
        val = s->smb_addr;
394
        break;
395
    case SMBHSTDAT0:
396
        val = s->smb_data0;
397
        break;
398
    case SMBHSTDAT1:
399
        val = s->smb_data1;
400
        break;
401
    case SMBBLKDAT:
402
        val = s->smb_data[s->smb_index++];
403
        if (s->smb_index > 31)
404
            s->smb_index = 0;
405
        break;
406
    default:
407
        val = 0;
408
        break;
409
    }
410
#ifdef DEBUG
411
    printf("SMB readb port=0x%04x val=0x%02x\n", addr, val);
412
#endif
413
    return val;
414
}
415

    
416
static void pm_io_space_update(PIIX4PMState *s)
417
{
418
    uint32_t pm_io_base;
419

    
420
    if (s->dev.config[0x80] & 1) {
421
        pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
422
        pm_io_base &= 0xffc0;
423

    
424
        /* XXX: need to improve memory and ioport allocation */
425
#if defined(DEBUG)
426
        printf("PM: mapping to 0x%x\n", pm_io_base);
427
#endif
428
        register_ioport_write(pm_io_base, 64, 2, pm_ioport_writew, s);
429
        register_ioport_read(pm_io_base, 64, 2, pm_ioport_readw, s);
430
        register_ioport_write(pm_io_base, 64, 4, pm_ioport_writel, s);
431
        register_ioport_read(pm_io_base, 64, 4, pm_ioport_readl, s);
432
    }
433
}
434

    
435
static void pm_write_config(PCIDevice *d,
436
                            uint32_t address, uint32_t val, int len)
437
{
438
    pci_default_write_config(d, address, val, len);
439
    if (address == 0x80)
440
        pm_io_space_update((PIIX4PMState *)d);
441
}
442

    
443
static int vmstate_acpi_after_load(void *opaque)
444
{
445
    PIIX4PMState *s = opaque;
446

    
447
    pm_io_space_update(s);
448
    return 0;
449
}
450

    
451
static const VMStateDescription vmstate_acpi = {
452
    .name = "piix4_pm",
453
    .version_id = 1,
454
    .minimum_version_id = 1,
455
    .minimum_version_id_old = 1,
456
    .run_after_load = vmstate_acpi_after_load,
457
    .fields      = (VMStateField []) {
458
        VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
459
        VMSTATE_UINT16(pmsts, PIIX4PMState),
460
        VMSTATE_UINT16(pmen, PIIX4PMState),
461
        VMSTATE_UINT16(pmcntrl, PIIX4PMState),
462
        VMSTATE_UINT8(apmc, PIIX4PMState),
463
        VMSTATE_UINT8(apms, PIIX4PMState),
464
        VMSTATE_TIMER(tmr_timer, PIIX4PMState),
465
        VMSTATE_INT64(tmr_overflow_time, PIIX4PMState),
466
        VMSTATE_END_OF_LIST()
467
    }
468
};
469

    
470
static void piix4_reset(void *opaque)
471
{
472
    PIIX4PMState *s = opaque;
473
    uint8_t *pci_conf = s->dev.config;
474

    
475
    pci_conf[0x58] = 0;
476
    pci_conf[0x59] = 0;
477
    pci_conf[0x5a] = 0;
478
    pci_conf[0x5b] = 0;
479

    
480
    if (kvm_enabled()) {
481
        /* Mark SMM as already inited (until KVM supports SMM). */
482
        pci_conf[0x5B] = 0x02;
483
    }
484
}
485

    
486
static void piix4_powerdown(void *opaque, int irq, int power_failing)
487
{
488
#if defined(TARGET_I386)
489
    PIIX4PMState *s = opaque;
490

    
491
    if (!s) {
492
        qemu_system_shutdown_request();
493
    } else if (s->pmen & PWRBTN_EN) {
494
        s->pmsts |= PWRBTN_EN;
495
        pm_update_sci(s);
496
    }
497
#endif
498
}
499

    
500
i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
501
                       qemu_irq sci_irq)
502
{
503
    PIIX4PMState *s;
504
    uint8_t *pci_conf;
505

    
506
    s = (PIIX4PMState *)pci_register_device(bus,
507
                                         "PM", sizeof(PIIX4PMState),
508
                                         devfn, NULL, pm_write_config);
509
    pm_state = s;
510
    pci_conf = s->dev.config;
511
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
512
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_3);
513
    pci_conf[0x06] = 0x80;
514
    pci_conf[0x07] = 0x02;
515
    pci_conf[0x08] = 0x03; // revision number
516
    pci_conf[0x09] = 0x00;
517
    pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);
518
    pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
519
    pci_conf[0x3d] = 0x01; // interrupt pin 1
520

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

    
523
    register_ioport_write(0xb2, 2, 1, pm_smi_writeb, s);
524
    register_ioport_read(0xb2, 2, 1, pm_smi_readb, s);
525

    
526
    register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
527

    
528
    if (kvm_enabled()) {
529
        /* Mark SMM as already inited to prevent SMM from running.  KVM does not
530
         * support SMM mode. */
531
        pci_conf[0x5B] = 0x02;
532
    }
533

    
534
    /* XXX: which specification is used ? The i82731AB has different
535
       mappings */
536
    pci_conf[0x5f] = (parallel_hds[0] != NULL ? 0x80 : 0) | 0x10;
537
    pci_conf[0x63] = 0x60;
538
    pci_conf[0x67] = (serial_hds[0] != NULL ? 0x08 : 0) |
539
        (serial_hds[1] != NULL ? 0x90 : 0);
540

    
541
    pci_conf[0x90] = smb_io_base | 1;
542
    pci_conf[0x91] = smb_io_base >> 8;
543
    pci_conf[0xd2] = 0x09;
544
    register_ioport_write(smb_io_base, 64, 1, smb_ioport_writeb, s);
545
    register_ioport_read(smb_io_base, 64, 1, smb_ioport_readb, s);
546

    
547
    s->tmr_timer = qemu_new_timer(vm_clock, pm_tmr_timer, s);
548

    
549
    qemu_system_powerdown = *qemu_allocate_irqs(piix4_powerdown, s, 1);
550

    
551
    vmstate_register(0, &vmstate_acpi, s);
552

    
553
    s->smbus = i2c_init_bus(NULL, "i2c");
554
    s->irq = sci_irq;
555
    qemu_register_reset(piix4_reset, s);
556

    
557
    return s->smbus;
558
}
559

    
560
#define GPE_BASE 0xafe0
561
#define PCI_BASE 0xae00
562
#define PCI_EJ_BASE 0xae08
563

    
564
struct gpe_regs {
565
    uint16_t sts; /* status */
566
    uint16_t en;  /* enabled */
567
};
568

    
569
struct pci_status {
570
    uint32_t up;
571
    uint32_t down;
572
};
573

    
574
static struct gpe_regs gpe;
575
static struct pci_status pci0_status;
576

    
577
static uint32_t gpe_read_val(uint16_t val, uint32_t addr)
578
{
579
    if (addr & 1)
580
        return (val >> 8) & 0xff;
581
    return val & 0xff;
582
}
583

    
584
static uint32_t gpe_readb(void *opaque, uint32_t addr)
585
{
586
    uint32_t val = 0;
587
    struct gpe_regs *g = opaque;
588
    switch (addr) {
589
        case GPE_BASE:
590
        case GPE_BASE + 1:
591
            val = gpe_read_val(g->sts, addr);
592
            break;
593
        case GPE_BASE + 2:
594
        case GPE_BASE + 3:
595
            val = gpe_read_val(g->en, addr);
596
            break;
597
        default:
598
            break;
599
    }
600

    
601
#if defined(DEBUG)
602
    printf("gpe read %x == %x\n", addr, val);
603
#endif
604
    return val;
605
}
606

    
607
static void gpe_write_val(uint16_t *cur, int addr, uint32_t val)
608
{
609
    if (addr & 1)
610
        *cur = (*cur & 0xff) | (val << 8);
611
    else
612
        *cur = (*cur & 0xff00) | (val & 0xff);
613
}
614

    
615
static void gpe_reset_val(uint16_t *cur, int addr, uint32_t val)
616
{
617
    uint16_t x1, x0 = val & 0xff;
618
    int shift = (addr & 1) ? 8 : 0;
619

    
620
    x1 = (*cur >> shift) & 0xff;
621

    
622
    x1 = x1 & ~x0;
623

    
624
    *cur = (*cur & (0xff << (8 - shift))) | (x1 << shift);
625
}
626

    
627
static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
628
{
629
    struct gpe_regs *g = opaque;
630
    switch (addr) {
631
        case GPE_BASE:
632
        case GPE_BASE + 1:
633
            gpe_reset_val(&g->sts, addr, val);
634
            break;
635
        case GPE_BASE + 2:
636
        case GPE_BASE + 3:
637
            gpe_write_val(&g->en, addr, val);
638
            break;
639
        default:
640
            break;
641
   }
642

    
643
#if defined(DEBUG)
644
    printf("gpe write %x <== %d\n", addr, val);
645
#endif
646
}
647

    
648
static uint32_t pcihotplug_read(void *opaque, uint32_t addr)
649
{
650
    uint32_t val = 0;
651
    struct pci_status *g = opaque;
652
    switch (addr) {
653
        case PCI_BASE:
654
            val = g->up;
655
            break;
656
        case PCI_BASE + 4:
657
            val = g->down;
658
            break;
659
        default:
660
            break;
661
    }
662

    
663
#if defined(DEBUG)
664
    printf("pcihotplug read %x == %x\n", addr, val);
665
#endif
666
    return val;
667
}
668

    
669
static void pcihotplug_write(void *opaque, uint32_t addr, uint32_t val)
670
{
671
    struct pci_status *g = opaque;
672
    switch (addr) {
673
        case PCI_BASE:
674
            g->up = val;
675
            break;
676
        case PCI_BASE + 4:
677
            g->down = val;
678
            break;
679
   }
680

    
681
#if defined(DEBUG)
682
    printf("pcihotplug write %x <== %d\n", addr, val);
683
#endif
684
}
685

    
686
static uint32_t pciej_read(void *opaque, uint32_t addr)
687
{
688
#if defined(DEBUG)
689
    printf("pciej read %x\n", addr);
690
#endif
691
    return 0;
692
}
693

    
694
static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
695
{
696
#if defined (TARGET_I386)
697
    int slot = ffs(val) - 1;
698

    
699
    pci_device_hot_remove_success(0, slot);
700
#endif
701

    
702
#if defined(DEBUG)
703
    printf("pciej write %x <== %d\n", addr, val);
704
#endif
705
}
706

    
707
static void piix4_device_hot_add(int bus, int slot, int state);
708

    
709
void piix4_acpi_system_hot_add_init(void)
710
{
711
    register_ioport_write(GPE_BASE, 4, 1, gpe_writeb, &gpe);
712
    register_ioport_read(GPE_BASE, 4, 1,  gpe_readb, &gpe);
713

    
714
    register_ioport_write(PCI_BASE, 8, 4, pcihotplug_write, &pci0_status);
715
    register_ioport_read(PCI_BASE, 8, 4,  pcihotplug_read, &pci0_status);
716

    
717
    register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, NULL);
718
    register_ioport_read(PCI_EJ_BASE, 4, 4,  pciej_read, NULL);
719

    
720
    qemu_system_device_hot_add_register(piix4_device_hot_add);
721
}
722

    
723
static void enable_device(struct pci_status *p, struct gpe_regs *g, int slot)
724
{
725
    g->sts |= 2;
726
    p->up |= (1 << slot);
727
}
728

    
729
static void disable_device(struct pci_status *p, struct gpe_regs *g, int slot)
730
{
731
    g->sts |= 2;
732
    p->down |= (1 << slot);
733
}
734

    
735
static void piix4_device_hot_add(int bus, int slot, int state)
736
{
737
    pci0_status.up = 0;
738
    pci0_status.down = 0;
739
    if (state)
740
        enable_device(&pci0_status, &gpe, slot);
741
    else
742
        disable_device(&pci0_status, &gpe, slot);
743
    if (gpe.en & 2) {
744
        qemu_set_irq(pm_state->irq, 1);
745
        qemu_set_irq(pm_state->irq, 0);
746
    }
747
}
748

    
749
static qemu_system_device_hot_add_t device_hot_add_callback;
750
void qemu_system_device_hot_add_register(qemu_system_device_hot_add_t callback)
751
{
752
    device_hot_add_callback = callback;
753
}
754

    
755
void qemu_system_device_hot_add(int pcibus, int slot, int state)
756
{
757
    if (device_hot_add_callback)
758
        device_hot_add_callback(pcibus, slot, state);
759
}
760

    
761
struct acpi_table_header
762
{
763
    char signature [4];    /* ACPI signature (4 ASCII characters) */
764
    uint32_t length;          /* Length of table, in bytes, including header */
765
    uint8_t revision;         /* ACPI Specification minor version # */
766
    uint8_t checksum;         /* To make sum of entire table == 0 */
767
    char oem_id [6];       /* OEM identification */
768
    char oem_table_id [8]; /* OEM table identification */
769
    uint32_t oem_revision;    /* OEM revision number */
770
    char asl_compiler_id [4]; /* ASL compiler vendor ID */
771
    uint32_t asl_compiler_revision; /* ASL compiler revision number */
772
} __attribute__((packed));
773

    
774
char *acpi_tables;
775
size_t acpi_tables_len;
776

    
777
static int acpi_checksum(const uint8_t *data, int len)
778
{
779
    int sum, i;
780
    sum = 0;
781
    for(i = 0; i < len; i++)
782
        sum += data[i];
783
    return (-sum) & 0xff;
784
}
785

    
786
int acpi_table_add(const char *t)
787
{
788
    static const char *dfl_id = "QEMUQEMU";
789
    char buf[1024], *p, *f;
790
    struct acpi_table_header acpi_hdr;
791
    unsigned long val;
792
    size_t off;
793

    
794
    memset(&acpi_hdr, 0, sizeof(acpi_hdr));
795
  
796
    if (get_param_value(buf, sizeof(buf), "sig", t)) {
797
        strncpy(acpi_hdr.signature, buf, 4);
798
    } else {
799
        strncpy(acpi_hdr.signature, dfl_id, 4);
800
    }
801
    if (get_param_value(buf, sizeof(buf), "rev", t)) {
802
        val = strtoul(buf, &p, 10);
803
        if (val > 255 || *p != '\0')
804
            goto out;
805
    } else {
806
        val = 1;
807
    }
808
    acpi_hdr.revision = (int8_t)val;
809

    
810
    if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
811
        strncpy(acpi_hdr.oem_id, buf, 6);
812
    } else {
813
        strncpy(acpi_hdr.oem_id, dfl_id, 6);
814
    }
815

    
816
    if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
817
        strncpy(acpi_hdr.oem_table_id, buf, 8);
818
    } else {
819
        strncpy(acpi_hdr.oem_table_id, dfl_id, 8);
820
    }
821

    
822
    if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
823
        val = strtol(buf, &p, 10);
824
        if(*p != '\0')
825
            goto out;
826
    } else {
827
        val = 1;
828
    }
829
    acpi_hdr.oem_revision = cpu_to_le32(val);
830

    
831
    if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
832
        strncpy(acpi_hdr.asl_compiler_id, buf, 4);
833
    } else {
834
        strncpy(acpi_hdr.asl_compiler_id, dfl_id, 4);
835
    }
836

    
837
    if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
838
        val = strtol(buf, &p, 10);
839
        if(*p != '\0')
840
            goto out;
841
    } else {
842
        val = 1;
843
    }
844
    acpi_hdr.asl_compiler_revision = cpu_to_le32(val);
845
    
846
    if (!get_param_value(buf, sizeof(buf), "data", t)) {
847
         buf[0] = '\0';
848
    }
849

    
850
    acpi_hdr.length = sizeof(acpi_hdr);
851

    
852
    f = buf;
853
    while (buf[0]) {
854
        struct stat s;
855
        char *n = strchr(f, ':');
856
        if (n)
857
            *n = '\0';
858
        if(stat(f, &s) < 0) {
859
            fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
860
            goto out;
861
        }
862
        acpi_hdr.length += s.st_size;
863
        if (!n)
864
            break;
865
        *n = ':';
866
        f = n + 1;
867
    }
868

    
869
    if (!acpi_tables) {
870
        acpi_tables_len = sizeof(uint16_t);
871
        acpi_tables = qemu_mallocz(acpi_tables_len);
872
    }
873
    p = acpi_tables + acpi_tables_len;
874
    acpi_tables_len += sizeof(uint16_t) + acpi_hdr.length;
875
    acpi_tables = qemu_realloc(acpi_tables, acpi_tables_len);
876

    
877
    acpi_hdr.length = cpu_to_le32(acpi_hdr.length);
878
    *(uint16_t*)p = acpi_hdr.length;
879
    p += sizeof(uint16_t);
880
    memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
881
    off = sizeof(acpi_hdr);
882

    
883
    f = buf;
884
    while (buf[0]) {
885
        struct stat s;
886
        int fd;
887
        char *n = strchr(f, ':');
888
        if (n)
889
            *n = '\0';
890
        fd = open(f, O_RDONLY);
891

    
892
        if(fd < 0)
893
            goto out;
894
        if(fstat(fd, &s) < 0) {
895
            close(fd);
896
            goto out;
897
        }
898

    
899
        do {
900
            int r;
901
            r = read(fd, p + off, s.st_size);
902
            if (r > 0) {
903
                off += r;
904
                s.st_size -= r;
905
            } else if ((r < 0 && errno != EINTR) || r == 0) {
906
                close(fd);
907
                goto out;
908
            }
909
        } while(s.st_size);
910

    
911
        close(fd);
912
        if (!n)
913
            break;
914
        f = n + 1;
915
    }
916

    
917
    ((struct acpi_table_header*)p)->checksum = acpi_checksum((uint8_t*)p, off);
918
    /* increase number of tables */
919
    (*(uint16_t*)acpi_tables) =
920
            cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
921
    return 0;
922
out:
923
    if (acpi_tables) {
924
        free(acpi_tables);
925
        acpi_tables = NULL;
926
    }
927
    return -1;
928
}