Statistics
| Branch: | Revision:

root / hw / acpi.c @ fc0bdd99

History | View | Annotate | Download (19.5 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 "pm_smbus.h"
21
#include "pci.h"
22
#include "qemu-timer.h"
23
#include "sysemu.h"
24
#include "i2c.h"
25
#include "smbus.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

    
44
    PMSMBus smb;
45

    
46
    qemu_irq irq;
47
    qemu_irq cmos_s3;
48
    qemu_irq smi_irq;
49
    int kvm_enabled;
50
} PIIX4PMState;
51

    
52
#define RSM_STS (1 << 15)
53
#define PWRBTN_STS (1 << 8)
54
#define RTC_EN (1 << 10)
55
#define PWRBTN_EN (1 << 8)
56
#define GBL_EN (1 << 5)
57
#define TMROF_EN (1 << 0)
58

    
59
#define SCI_EN (1 << 0)
60

    
61
#define SUS_EN (1 << 13)
62

    
63
#define ACPI_ENABLE 0xf1
64
#define ACPI_DISABLE 0xf0
65

    
66
static PIIX4PMState *pm_state;
67

    
68
static uint32_t get_pmtmr(PIIX4PMState *s)
69
{
70
    uint32_t d;
71
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, get_ticks_per_sec());
72
    return d & 0xffffff;
73
}
74

    
75
static int get_pmsts(PIIX4PMState *s)
76
{
77
    int64_t d;
78

    
79
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, get_ticks_per_sec());
80
    if (d >= s->tmr_overflow_time)
81
        s->pmsts |= TMROF_EN;
82
    return s->pmsts;
83
}
84

    
85
static void pm_update_sci(PIIX4PMState *s)
86
{
87
    int sci_level, pmsts;
88
    int64_t expire_time;
89

    
90
    pmsts = get_pmsts(s);
91
    sci_level = (((pmsts & s->pmen) &
92
                  (RTC_EN | PWRBTN_EN | GBL_EN | TMROF_EN)) != 0);
93
    qemu_set_irq(s->irq, sci_level);
94
    /* schedule a timer interruption if needed */
95
    if ((s->pmen & TMROF_EN) && !(pmsts & TMROF_EN)) {
96
        expire_time = muldiv64(s->tmr_overflow_time, get_ticks_per_sec(), PM_FREQ);
97
        qemu_mod_timer(s->tmr_timer, expire_time);
98
    } else {
99
        qemu_del_timer(s->tmr_timer);
100
    }
101
}
102

    
103
static void pm_tmr_timer(void *opaque)
104
{
105
    PIIX4PMState *s = opaque;
106
    pm_update_sci(s);
107
}
108

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

    
166
static uint32_t pm_ioport_readw(void *opaque, uint32_t addr)
167
{
168
    PIIX4PMState *s = opaque;
169
    uint32_t val;
170

    
171
    addr &= 0x3f;
172
    switch(addr) {
173
    case 0x00:
174
        val = get_pmsts(s);
175
        break;
176
    case 0x02:
177
        val = s->pmen;
178
        break;
179
    case 0x04:
180
        val = s->pmcntrl;
181
        break;
182
    default:
183
        val = 0;
184
        break;
185
    }
186
#ifdef DEBUG
187
    printf("PM readw port=0x%04x val=0x%04x\n", addr, val);
188
#endif
189
    return val;
190
}
191

    
192
static void pm_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
193
{
194
    //    PIIX4PMState *s = opaque;
195
#ifdef DEBUG
196
    addr &= 0x3f;
197
    printf("PM writel port=0x%04x val=0x%08x\n", addr, val);
198
#endif
199
}
200

    
201
static uint32_t pm_ioport_readl(void *opaque, uint32_t addr)
202
{
203
    PIIX4PMState *s = opaque;
204
    uint32_t val;
205

    
206
    addr &= 0x3f;
207
    switch(addr) {
208
    case 0x08:
209
        val = get_pmtmr(s);
210
        break;
211
    default:
212
        val = 0;
213
        break;
214
    }
215
#ifdef DEBUG
216
    printf("PM readl port=0x%04x val=0x%08x\n", addr, val);
217
#endif
218
    return val;
219
}
220

    
221
static void pm_smi_writeb(void *opaque, uint32_t addr, uint32_t val)
222
{
223
    PIIX4PMState *s = opaque;
224
    addr &= 1;
225
#ifdef DEBUG
226
    printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr, val);
227
#endif
228
    if (addr == 0) {
229
        s->apmc = val;
230

    
231
        /* ACPI specs 3.0, 4.7.2.5 */
232
        if (val == ACPI_ENABLE) {
233
            s->pmcntrl |= SCI_EN;
234
        } else if (val == ACPI_DISABLE) {
235
            s->pmcntrl &= ~SCI_EN;
236
        }
237

    
238
        if (s->dev.config[0x5b] & (1 << 1)) {
239
            if (s->smi_irq) {
240
                qemu_irq_raise(s->smi_irq);
241
            }
242
        }
243
    } else {
244
        s->apms = val;
245
    }
246
}
247

    
248
static uint32_t pm_smi_readb(void *opaque, uint32_t addr)
249
{
250
    PIIX4PMState *s = opaque;
251
    uint32_t val;
252

    
253
    addr &= 1;
254
    if (addr == 0) {
255
        val = s->apmc;
256
    } else {
257
        val = s->apms;
258
    }
259
#ifdef DEBUG
260
    printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr, val);
261
#endif
262
    return val;
263
}
264

    
265
static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
266
{
267
#if defined(DEBUG)
268
    printf("ACPI: DBG: 0x%08x\n", val);
269
#endif
270
}
271

    
272
static void pm_io_space_update(PIIX4PMState *s)
273
{
274
    uint32_t pm_io_base;
275

    
276
    if (s->dev.config[0x80] & 1) {
277
        pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
278
        pm_io_base &= 0xffc0;
279

    
280
        /* XXX: need to improve memory and ioport allocation */
281
#if defined(DEBUG)
282
        printf("PM: mapping to 0x%x\n", pm_io_base);
283
#endif
284
        register_ioport_write(pm_io_base, 64, 2, pm_ioport_writew, s);
285
        register_ioport_read(pm_io_base, 64, 2, pm_ioport_readw, s);
286
        register_ioport_write(pm_io_base, 64, 4, pm_ioport_writel, s);
287
        register_ioport_read(pm_io_base, 64, 4, pm_ioport_readl, s);
288
    }
289
}
290

    
291
static void pm_write_config(PCIDevice *d,
292
                            uint32_t address, uint32_t val, int len)
293
{
294
    pci_default_write_config(d, address, val, len);
295
    if (range_covers_byte(address, len, 0x80))
296
        pm_io_space_update((PIIX4PMState *)d);
297
}
298

    
299
static int vmstate_acpi_post_load(void *opaque, int version_id)
300
{
301
    PIIX4PMState *s = opaque;
302

    
303
    pm_io_space_update(s);
304
    return 0;
305
}
306

    
307
static const VMStateDescription vmstate_acpi = {
308
    .name = "piix4_pm",
309
    .version_id = 1,
310
    .minimum_version_id = 1,
311
    .minimum_version_id_old = 1,
312
    .post_load = vmstate_acpi_post_load,
313
    .fields      = (VMStateField []) {
314
        VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
315
        VMSTATE_UINT16(pmsts, PIIX4PMState),
316
        VMSTATE_UINT16(pmen, PIIX4PMState),
317
        VMSTATE_UINT16(pmcntrl, PIIX4PMState),
318
        VMSTATE_UINT8(apmc, PIIX4PMState),
319
        VMSTATE_UINT8(apms, PIIX4PMState),
320
        VMSTATE_TIMER(tmr_timer, PIIX4PMState),
321
        VMSTATE_INT64(tmr_overflow_time, PIIX4PMState),
322
        VMSTATE_END_OF_LIST()
323
    }
324
};
325

    
326
static void piix4_reset(void *opaque)
327
{
328
    PIIX4PMState *s = opaque;
329
    uint8_t *pci_conf = s->dev.config;
330

    
331
    pci_conf[0x58] = 0;
332
    pci_conf[0x59] = 0;
333
    pci_conf[0x5a] = 0;
334
    pci_conf[0x5b] = 0;
335

    
336
    if (s->kvm_enabled) {
337
        /* Mark SMM as already inited (until KVM supports SMM). */
338
        pci_conf[0x5B] = 0x02;
339
    }
340
}
341

    
342
static void piix4_powerdown(void *opaque, int irq, int power_failing)
343
{
344
    PIIX4PMState *s = opaque;
345

    
346
    if (!s) {
347
        qemu_system_shutdown_request();
348
    } else if (s->pmen & PWRBTN_EN) {
349
        s->pmsts |= PWRBTN_EN;
350
        pm_update_sci(s);
351
    }
352
}
353

    
354
i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
355
                       qemu_irq sci_irq, qemu_irq cmos_s3, qemu_irq smi_irq,
356
                       int kvm_enabled)
357
{
358
    PIIX4PMState *s;
359
    uint8_t *pci_conf;
360

    
361
    s = (PIIX4PMState *)pci_register_device(bus,
362
                                         "PM", sizeof(PIIX4PMState),
363
                                         devfn, NULL, pm_write_config);
364
    pm_state = s;
365
    pci_conf = s->dev.config;
366
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
367
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_3);
368
    pci_conf[0x06] = 0x80;
369
    pci_conf[0x07] = 0x02;
370
    pci_conf[0x08] = 0x03; // revision number
371
    pci_conf[0x09] = 0x00;
372
    pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);
373
    pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
374
    pci_conf[0x3d] = 0x01; // interrupt pin 1
375

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

    
378
    register_ioport_write(0xb2, 2, 1, pm_smi_writeb, s);
379
    register_ioport_read(0xb2, 2, 1, pm_smi_readb, s);
380

    
381
    register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
382

    
383
    s->kvm_enabled = kvm_enabled;
384
    if (s->kvm_enabled) {
385
        /* Mark SMM as already inited to prevent SMM from running.  KVM does not
386
         * support SMM mode. */
387
        pci_conf[0x5B] = 0x02;
388
    }
389

    
390
    /* XXX: which specification is used ? The i82731AB has different
391
       mappings */
392
    pci_conf[0x5f] = (parallel_hds[0] != NULL ? 0x80 : 0) | 0x10;
393
    pci_conf[0x63] = 0x60;
394
    pci_conf[0x67] = (serial_hds[0] != NULL ? 0x08 : 0) |
395
        (serial_hds[1] != NULL ? 0x90 : 0);
396

    
397
    pci_conf[0x90] = smb_io_base | 1;
398
    pci_conf[0x91] = smb_io_base >> 8;
399
    pci_conf[0xd2] = 0x09;
400
    register_ioport_write(smb_io_base, 64, 1, smb_ioport_writeb, &s->smb);
401
    register_ioport_read(smb_io_base, 64, 1, smb_ioport_readb, &s->smb);
402

    
403
    s->tmr_timer = qemu_new_timer(vm_clock, pm_tmr_timer, s);
404

    
405
    qemu_system_powerdown = *qemu_allocate_irqs(piix4_powerdown, s, 1);
406

    
407
    vmstate_register(0, &vmstate_acpi, s);
408

    
409
    pm_smbus_init(NULL, &s->smb);
410
    s->irq = sci_irq;
411
    s->cmos_s3 = cmos_s3;
412
    s->smi_irq = smi_irq;
413
    qemu_register_reset(piix4_reset, s);
414

    
415
    return s->smb.smbus;
416
}
417

    
418
#define GPE_BASE 0xafe0
419
#define PCI_BASE 0xae00
420
#define PCI_EJ_BASE 0xae08
421

    
422
struct gpe_regs {
423
    uint16_t sts; /* status */
424
    uint16_t en;  /* enabled */
425
};
426

    
427
struct pci_status {
428
    uint32_t up;
429
    uint32_t down;
430
};
431

    
432
static struct gpe_regs gpe;
433
static struct pci_status pci0_status;
434

    
435
static uint32_t gpe_read_val(uint16_t val, uint32_t addr)
436
{
437
    if (addr & 1)
438
        return (val >> 8) & 0xff;
439
    return val & 0xff;
440
}
441

    
442
static uint32_t gpe_readb(void *opaque, uint32_t addr)
443
{
444
    uint32_t val = 0;
445
    struct gpe_regs *g = opaque;
446
    switch (addr) {
447
        case GPE_BASE:
448
        case GPE_BASE + 1:
449
            val = gpe_read_val(g->sts, addr);
450
            break;
451
        case GPE_BASE + 2:
452
        case GPE_BASE + 3:
453
            val = gpe_read_val(g->en, addr);
454
            break;
455
        default:
456
            break;
457
    }
458

    
459
#if defined(DEBUG)
460
    printf("gpe read %x == %x\n", addr, val);
461
#endif
462
    return val;
463
}
464

    
465
static void gpe_write_val(uint16_t *cur, int addr, uint32_t val)
466
{
467
    if (addr & 1)
468
        *cur = (*cur & 0xff) | (val << 8);
469
    else
470
        *cur = (*cur & 0xff00) | (val & 0xff);
471
}
472

    
473
static void gpe_reset_val(uint16_t *cur, int addr, uint32_t val)
474
{
475
    uint16_t x1, x0 = val & 0xff;
476
    int shift = (addr & 1) ? 8 : 0;
477

    
478
    x1 = (*cur >> shift) & 0xff;
479

    
480
    x1 = x1 & ~x0;
481

    
482
    *cur = (*cur & (0xff << (8 - shift))) | (x1 << shift);
483
}
484

    
485
static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
486
{
487
    struct gpe_regs *g = opaque;
488
    switch (addr) {
489
        case GPE_BASE:
490
        case GPE_BASE + 1:
491
            gpe_reset_val(&g->sts, addr, val);
492
            break;
493
        case GPE_BASE + 2:
494
        case GPE_BASE + 3:
495
            gpe_write_val(&g->en, addr, val);
496
            break;
497
        default:
498
            break;
499
   }
500

    
501
#if defined(DEBUG)
502
    printf("gpe write %x <== %d\n", addr, val);
503
#endif
504
}
505

    
506
static uint32_t pcihotplug_read(void *opaque, uint32_t addr)
507
{
508
    uint32_t val = 0;
509
    struct pci_status *g = opaque;
510
    switch (addr) {
511
        case PCI_BASE:
512
            val = g->up;
513
            break;
514
        case PCI_BASE + 4:
515
            val = g->down;
516
            break;
517
        default:
518
            break;
519
    }
520

    
521
#if defined(DEBUG)
522
    printf("pcihotplug read %x == %x\n", addr, val);
523
#endif
524
    return val;
525
}
526

    
527
static void pcihotplug_write(void *opaque, uint32_t addr, uint32_t val)
528
{
529
    struct pci_status *g = opaque;
530
    switch (addr) {
531
        case PCI_BASE:
532
            g->up = val;
533
            break;
534
        case PCI_BASE + 4:
535
            g->down = val;
536
            break;
537
   }
538

    
539
#if defined(DEBUG)
540
    printf("pcihotplug write %x <== %d\n", addr, val);
541
#endif
542
}
543

    
544
static uint32_t pciej_read(void *opaque, uint32_t addr)
545
{
546
#if defined(DEBUG)
547
    printf("pciej read %x\n", addr);
548
#endif
549
    return 0;
550
}
551

    
552
static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
553
{
554
    BusState *bus = opaque;
555
    DeviceState *qdev, *next;
556
    PCIDevice *dev;
557
    int slot = ffs(val) - 1;
558

    
559
    QLIST_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
560
        dev = DO_UPCAST(PCIDevice, qdev, qdev);
561
        if (PCI_SLOT(dev->devfn) == slot) {
562
            qdev_free(qdev);
563
        }
564
    }
565

    
566

    
567
#if defined(DEBUG)
568
    printf("pciej write %x <== %d\n", addr, val);
569
#endif
570
}
571

    
572
static int piix4_device_hotplug(PCIDevice *dev, int state);
573

    
574
void piix4_acpi_system_hot_add_init(PCIBus *bus)
575
{
576
    register_ioport_write(GPE_BASE, 4, 1, gpe_writeb, &gpe);
577
    register_ioport_read(GPE_BASE, 4, 1,  gpe_readb, &gpe);
578

    
579
    register_ioport_write(PCI_BASE, 8, 4, pcihotplug_write, &pci0_status);
580
    register_ioport_read(PCI_BASE, 8, 4,  pcihotplug_read, &pci0_status);
581

    
582
    register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, bus);
583
    register_ioport_read(PCI_EJ_BASE, 4, 4,  pciej_read, bus);
584

    
585
    pci_bus_hotplug(bus, piix4_device_hotplug);
586
}
587

    
588
static void enable_device(struct pci_status *p, struct gpe_regs *g, int slot)
589
{
590
    g->sts |= 2;
591
    p->up |= (1 << slot);
592
}
593

    
594
static void disable_device(struct pci_status *p, struct gpe_regs *g, int slot)
595
{
596
    g->sts |= 2;
597
    p->down |= (1 << slot);
598
}
599

    
600
static int piix4_device_hotplug(PCIDevice *dev, int state)
601
{
602
    int slot = PCI_SLOT(dev->devfn);
603

    
604
    pci0_status.up = 0;
605
    pci0_status.down = 0;
606
    if (state)
607
        enable_device(&pci0_status, &gpe, slot);
608
    else
609
        disable_device(&pci0_status, &gpe, slot);
610
    if (gpe.en & 2) {
611
        qemu_set_irq(pm_state->irq, 1);
612
        qemu_set_irq(pm_state->irq, 0);
613
    }
614
    return 0;
615
}
616

    
617
struct acpi_table_header
618
{
619
    char signature [4];    /* ACPI signature (4 ASCII characters) */
620
    uint32_t length;          /* Length of table, in bytes, including header */
621
    uint8_t revision;         /* ACPI Specification minor version # */
622
    uint8_t checksum;         /* To make sum of entire table == 0 */
623
    char oem_id [6];       /* OEM identification */
624
    char oem_table_id [8]; /* OEM table identification */
625
    uint32_t oem_revision;    /* OEM revision number */
626
    char asl_compiler_id [4]; /* ASL compiler vendor ID */
627
    uint32_t asl_compiler_revision; /* ASL compiler revision number */
628
} __attribute__((packed));
629

    
630
char *acpi_tables;
631
size_t acpi_tables_len;
632

    
633
static int acpi_checksum(const uint8_t *data, int len)
634
{
635
    int sum, i;
636
    sum = 0;
637
    for(i = 0; i < len; i++)
638
        sum += data[i];
639
    return (-sum) & 0xff;
640
}
641

    
642
int acpi_table_add(const char *t)
643
{
644
    static const char *dfl_id = "QEMUQEMU";
645
    char buf[1024], *p, *f;
646
    struct acpi_table_header acpi_hdr;
647
    unsigned long val;
648
    size_t off;
649

    
650
    memset(&acpi_hdr, 0, sizeof(acpi_hdr));
651
  
652
    if (get_param_value(buf, sizeof(buf), "sig", t)) {
653
        strncpy(acpi_hdr.signature, buf, 4);
654
    } else {
655
        strncpy(acpi_hdr.signature, dfl_id, 4);
656
    }
657
    if (get_param_value(buf, sizeof(buf), "rev", t)) {
658
        val = strtoul(buf, &p, 10);
659
        if (val > 255 || *p != '\0')
660
            goto out;
661
    } else {
662
        val = 1;
663
    }
664
    acpi_hdr.revision = (int8_t)val;
665

    
666
    if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
667
        strncpy(acpi_hdr.oem_id, buf, 6);
668
    } else {
669
        strncpy(acpi_hdr.oem_id, dfl_id, 6);
670
    }
671

    
672
    if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
673
        strncpy(acpi_hdr.oem_table_id, buf, 8);
674
    } else {
675
        strncpy(acpi_hdr.oem_table_id, dfl_id, 8);
676
    }
677

    
678
    if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
679
        val = strtol(buf, &p, 10);
680
        if(*p != '\0')
681
            goto out;
682
    } else {
683
        val = 1;
684
    }
685
    acpi_hdr.oem_revision = cpu_to_le32(val);
686

    
687
    if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
688
        strncpy(acpi_hdr.asl_compiler_id, buf, 4);
689
    } else {
690
        strncpy(acpi_hdr.asl_compiler_id, dfl_id, 4);
691
    }
692

    
693
    if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
694
        val = strtol(buf, &p, 10);
695
        if(*p != '\0')
696
            goto out;
697
    } else {
698
        val = 1;
699
    }
700
    acpi_hdr.asl_compiler_revision = cpu_to_le32(val);
701
    
702
    if (!get_param_value(buf, sizeof(buf), "data", t)) {
703
         buf[0] = '\0';
704
    }
705

    
706
    acpi_hdr.length = sizeof(acpi_hdr);
707

    
708
    f = buf;
709
    while (buf[0]) {
710
        struct stat s;
711
        char *n = strchr(f, ':');
712
        if (n)
713
            *n = '\0';
714
        if(stat(f, &s) < 0) {
715
            fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
716
            goto out;
717
        }
718
        acpi_hdr.length += s.st_size;
719
        if (!n)
720
            break;
721
        *n = ':';
722
        f = n + 1;
723
    }
724

    
725
    if (!acpi_tables) {
726
        acpi_tables_len = sizeof(uint16_t);
727
        acpi_tables = qemu_mallocz(acpi_tables_len);
728
    }
729
    p = acpi_tables + acpi_tables_len;
730
    acpi_tables_len += sizeof(uint16_t) + acpi_hdr.length;
731
    acpi_tables = qemu_realloc(acpi_tables, acpi_tables_len);
732

    
733
    acpi_hdr.length = cpu_to_le32(acpi_hdr.length);
734
    *(uint16_t*)p = acpi_hdr.length;
735
    p += sizeof(uint16_t);
736
    memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
737
    off = sizeof(acpi_hdr);
738

    
739
    f = buf;
740
    while (buf[0]) {
741
        struct stat s;
742
        int fd;
743
        char *n = strchr(f, ':');
744
        if (n)
745
            *n = '\0';
746
        fd = open(f, O_RDONLY);
747

    
748
        if(fd < 0)
749
            goto out;
750
        if(fstat(fd, &s) < 0) {
751
            close(fd);
752
            goto out;
753
        }
754

    
755
        do {
756
            int r;
757
            r = read(fd, p + off, s.st_size);
758
            if (r > 0) {
759
                off += r;
760
                s.st_size -= r;
761
            } else if ((r < 0 && errno != EINTR) || r == 0) {
762
                close(fd);
763
                goto out;
764
            }
765
        } while(s.st_size);
766

    
767
        close(fd);
768
        if (!n)
769
            break;
770
        f = n + 1;
771
    }
772

    
773
    ((struct acpi_table_header*)p)->checksum = acpi_checksum((uint8_t*)p, off);
774
    /* increase number of tables */
775
    (*(uint16_t*)acpi_tables) =
776
            cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
777
    return 0;
778
out:
779
    if (acpi_tables) {
780
        qemu_free(acpi_tables);
781
        acpi_tables = NULL;
782
    }
783
    return -1;
784
}