Statistics
| Branch: | Revision:

root / hw / acpi.c @ 22ed1d34

History | View | Annotate | Download (22.8 kB)

1 6515b203 bellard
/*
2 6515b203 bellard
 * ACPI implementation
3 5fafdf24 ths
 *
4 6515b203 bellard
 * Copyright (c) 2006 Fabrice Bellard
5 5fafdf24 ths
 *
6 6515b203 bellard
 * This library is free software; you can redistribute it and/or
7 6515b203 bellard
 * modify it under the terms of the GNU Lesser General Public
8 6515b203 bellard
 * License version 2 as published by the Free Software Foundation.
9 6515b203 bellard
 *
10 6515b203 bellard
 * This library is distributed in the hope that it will be useful,
11 6515b203 bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 6515b203 bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 6515b203 bellard
 * Lesser General Public License for more details.
14 6515b203 bellard
 *
15 6515b203 bellard
 * You should have received a copy of the GNU Lesser General Public
16 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>
17 6515b203 bellard
 */
18 87ecb68b pbrook
#include "hw.h"
19 87ecb68b pbrook
#include "pc.h"
20 87ecb68b pbrook
#include "pci.h"
21 87ecb68b pbrook
#include "qemu-timer.h"
22 87ecb68b pbrook
#include "sysemu.h"
23 87ecb68b pbrook
#include "i2c.h"
24 87ecb68b pbrook
#include "smbus.h"
25 6515b203 bellard
26 6515b203 bellard
//#define DEBUG
27 6515b203 bellard
28 6515b203 bellard
/* i82731AB (PIIX4) compatible power management function */
29 6515b203 bellard
#define PM_FREQ 3579545
30 6515b203 bellard
31 6515b203 bellard
#define ACPI_DBG_IO_ADDR  0xb044
32 6515b203 bellard
33 6515b203 bellard
typedef struct PIIX4PMState {
34 6515b203 bellard
    PCIDevice dev;
35 6515b203 bellard
    uint16_t pmsts;
36 6515b203 bellard
    uint16_t pmen;
37 6515b203 bellard
    uint16_t pmcntrl;
38 ab1e34ad bellard
    uint8_t apmc;
39 ab1e34ad bellard
    uint8_t apms;
40 6515b203 bellard
    QEMUTimer *tmr_timer;
41 6515b203 bellard
    int64_t tmr_overflow_time;
42 0ff596d0 pbrook
    i2c_bus *smbus;
43 3fffc223 ths
    uint8_t smb_stat;
44 3fffc223 ths
    uint8_t smb_ctl;
45 3fffc223 ths
    uint8_t smb_cmd;
46 3fffc223 ths
    uint8_t smb_addr;
47 3fffc223 ths
    uint8_t smb_data0;
48 3fffc223 ths
    uint8_t smb_data1;
49 3fffc223 ths
    uint8_t smb_data[32];
50 3fffc223 ths
    uint8_t smb_index;
51 cf7a2fe2 aurel32
    qemu_irq irq;
52 53b67b30 Blue Swirl
    qemu_irq cmos_s3;
53 53b67b30 Blue Swirl
    qemu_irq smi_irq;
54 53b67b30 Blue Swirl
    int kvm_enabled;
55 6515b203 bellard
} PIIX4PMState;
56 6515b203 bellard
57 0bacd130 aliguori
#define RSM_STS (1 << 15)
58 0bacd130 aliguori
#define PWRBTN_STS (1 << 8)
59 6515b203 bellard
#define RTC_EN (1 << 10)
60 6515b203 bellard
#define PWRBTN_EN (1 << 8)
61 6515b203 bellard
#define GBL_EN (1 << 5)
62 6515b203 bellard
#define TMROF_EN (1 << 0)
63 6515b203 bellard
64 6515b203 bellard
#define SCI_EN (1 << 0)
65 6515b203 bellard
66 6515b203 bellard
#define SUS_EN (1 << 13)
67 6515b203 bellard
68 24bc1cbc ths
#define ACPI_ENABLE 0xf1
69 24bc1cbc ths
#define ACPI_DISABLE 0xf0
70 24bc1cbc ths
71 3fffc223 ths
#define SMBHSTSTS 0x00
72 3fffc223 ths
#define SMBHSTCNT 0x02
73 3fffc223 ths
#define SMBHSTCMD 0x03
74 3fffc223 ths
#define SMBHSTADD 0x04
75 3fffc223 ths
#define SMBHSTDAT0 0x05
76 3fffc223 ths
#define SMBHSTDAT1 0x06
77 3fffc223 ths
#define SMBBLKDAT 0x07
78 3fffc223 ths
79 9669d3c5 aurel32
static PIIX4PMState *pm_state;
80 cf7a2fe2 aurel32
81 6515b203 bellard
static uint32_t get_pmtmr(PIIX4PMState *s)
82 6515b203 bellard
{
83 7546c016 balrog
    uint32_t d;
84 6ee093c9 Juan Quintela
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, get_ticks_per_sec());
85 7546c016 balrog
    return d & 0xffffff;
86 6515b203 bellard
}
87 6515b203 bellard
88 6515b203 bellard
static int get_pmsts(PIIX4PMState *s)
89 6515b203 bellard
{
90 7546c016 balrog
    int64_t d;
91 7f5b7d3e Blue Swirl
92 6ee093c9 Juan Quintela
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, get_ticks_per_sec());
93 7546c016 balrog
    if (d >= s->tmr_overflow_time)
94 7546c016 balrog
        s->pmsts |= TMROF_EN;
95 055479fe aliguori
    return s->pmsts;
96 6515b203 bellard
}
97 6515b203 bellard
98 6515b203 bellard
static void pm_update_sci(PIIX4PMState *s)
99 6515b203 bellard
{
100 7546c016 balrog
    int sci_level, pmsts;
101 7546c016 balrog
    int64_t expire_time;
102 7546c016 balrog
103 7546c016 balrog
    pmsts = get_pmsts(s);
104 7546c016 balrog
    sci_level = (((pmsts & s->pmen) &
105 7546c016 balrog
                  (RTC_EN | PWRBTN_EN | GBL_EN | TMROF_EN)) != 0);
106 7546c016 balrog
    qemu_set_irq(s->irq, sci_level);
107 7546c016 balrog
    /* schedule a timer interruption if needed */
108 7546c016 balrog
    if ((s->pmen & TMROF_EN) && !(pmsts & TMROF_EN)) {
109 6ee093c9 Juan Quintela
        expire_time = muldiv64(s->tmr_overflow_time, get_ticks_per_sec(), PM_FREQ);
110 7546c016 balrog
        qemu_mod_timer(s->tmr_timer, expire_time);
111 7546c016 balrog
    } else {
112 7546c016 balrog
        qemu_del_timer(s->tmr_timer);
113 7546c016 balrog
    }
114 6515b203 bellard
}
115 6515b203 bellard
116 6515b203 bellard
static void pm_tmr_timer(void *opaque)
117 6515b203 bellard
{
118 6515b203 bellard
    PIIX4PMState *s = opaque;
119 7546c016 balrog
    pm_update_sci(s);
120 6515b203 bellard
}
121 6515b203 bellard
122 6515b203 bellard
static void pm_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
123 6515b203 bellard
{
124 6515b203 bellard
    PIIX4PMState *s = opaque;
125 6515b203 bellard
    addr &= 0x3f;
126 6515b203 bellard
    switch(addr) {
127 6515b203 bellard
    case 0x00:
128 7546c016 balrog
        {
129 7546c016 balrog
            int64_t d;
130 7546c016 balrog
            int pmsts;
131 7546c016 balrog
            pmsts = get_pmsts(s);
132 7546c016 balrog
            if (pmsts & val & TMROF_EN) {
133 7546c016 balrog
                /* if TMRSTS is reset, then compute the new overflow time */
134 6ee093c9 Juan Quintela
                d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ,
135 6ee093c9 Juan Quintela
                             get_ticks_per_sec());
136 7546c016 balrog
                s->tmr_overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
137 7546c016 balrog
            }
138 7546c016 balrog
            s->pmsts &= ~val;
139 7546c016 balrog
            pm_update_sci(s);
140 7546c016 balrog
        }
141 6515b203 bellard
        break;
142 6515b203 bellard
    case 0x02:
143 6515b203 bellard
        s->pmen = val;
144 6515b203 bellard
        pm_update_sci(s);
145 6515b203 bellard
        break;
146 6515b203 bellard
    case 0x04:
147 6515b203 bellard
        {
148 6515b203 bellard
            int sus_typ;
149 6515b203 bellard
            s->pmcntrl = val & ~(SUS_EN);
150 6515b203 bellard
            if (val & SUS_EN) {
151 6515b203 bellard
                /* change suspend type */
152 f99ed40a aurel32
                sus_typ = (val >> 10) & 7;
153 6515b203 bellard
                switch(sus_typ) {
154 6515b203 bellard
                case 0: /* soft power off */
155 6515b203 bellard
                    qemu_system_shutdown_request();
156 6515b203 bellard
                    break;
157 0bacd130 aliguori
                case 1:
158 0bacd130 aliguori
                    /* RSM_STS should be set on resume. Pretend that resume
159 0bacd130 aliguori
                       was caused by power button */
160 0bacd130 aliguori
                    s->pmsts |= (RSM_STS | PWRBTN_STS);
161 0bacd130 aliguori
                    qemu_system_reset_request();
162 53b67b30 Blue Swirl
                    if (s->cmos_s3) {
163 53b67b30 Blue Swirl
                        qemu_irq_raise(s->cmos_s3);
164 53b67b30 Blue Swirl
                    }
165 6515b203 bellard
                default:
166 6515b203 bellard
                    break;
167 6515b203 bellard
                }
168 6515b203 bellard
            }
169 6515b203 bellard
        }
170 6515b203 bellard
        break;
171 6515b203 bellard
    default:
172 6515b203 bellard
        break;
173 6515b203 bellard
    }
174 6515b203 bellard
#ifdef DEBUG
175 6515b203 bellard
    printf("PM writew port=0x%04x val=0x%04x\n", addr, val);
176 6515b203 bellard
#endif
177 6515b203 bellard
}
178 6515b203 bellard
179 6515b203 bellard
static uint32_t pm_ioport_readw(void *opaque, uint32_t addr)
180 6515b203 bellard
{
181 6515b203 bellard
    PIIX4PMState *s = opaque;
182 6515b203 bellard
    uint32_t val;
183 6515b203 bellard
184 6515b203 bellard
    addr &= 0x3f;
185 6515b203 bellard
    switch(addr) {
186 6515b203 bellard
    case 0x00:
187 6515b203 bellard
        val = get_pmsts(s);
188 6515b203 bellard
        break;
189 6515b203 bellard
    case 0x02:
190 6515b203 bellard
        val = s->pmen;
191 6515b203 bellard
        break;
192 6515b203 bellard
    case 0x04:
193 6515b203 bellard
        val = s->pmcntrl;
194 6515b203 bellard
        break;
195 6515b203 bellard
    default:
196 6515b203 bellard
        val = 0;
197 6515b203 bellard
        break;
198 6515b203 bellard
    }
199 6515b203 bellard
#ifdef DEBUG
200 6515b203 bellard
    printf("PM readw port=0x%04x val=0x%04x\n", addr, val);
201 6515b203 bellard
#endif
202 6515b203 bellard
    return val;
203 6515b203 bellard
}
204 6515b203 bellard
205 6515b203 bellard
static void pm_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
206 6515b203 bellard
{
207 6515b203 bellard
    //    PIIX4PMState *s = opaque;
208 6515b203 bellard
#ifdef DEBUG
209 7f5b7d3e Blue Swirl
    addr &= 0x3f;
210 6515b203 bellard
    printf("PM writel port=0x%04x val=0x%08x\n", addr, val);
211 6515b203 bellard
#endif
212 6515b203 bellard
}
213 6515b203 bellard
214 6515b203 bellard
static uint32_t pm_ioport_readl(void *opaque, uint32_t addr)
215 6515b203 bellard
{
216 6515b203 bellard
    PIIX4PMState *s = opaque;
217 6515b203 bellard
    uint32_t val;
218 6515b203 bellard
219 6515b203 bellard
    addr &= 0x3f;
220 6515b203 bellard
    switch(addr) {
221 6515b203 bellard
    case 0x08:
222 6515b203 bellard
        val = get_pmtmr(s);
223 6515b203 bellard
        break;
224 6515b203 bellard
    default:
225 6515b203 bellard
        val = 0;
226 6515b203 bellard
        break;
227 6515b203 bellard
    }
228 6515b203 bellard
#ifdef DEBUG
229 6515b203 bellard
    printf("PM readl port=0x%04x val=0x%08x\n", addr, val);
230 6515b203 bellard
#endif
231 6515b203 bellard
    return val;
232 6515b203 bellard
}
233 6515b203 bellard
234 ab1e34ad bellard
static void pm_smi_writeb(void *opaque, uint32_t addr, uint32_t val)
235 6515b203 bellard
{
236 6515b203 bellard
    PIIX4PMState *s = opaque;
237 ab1e34ad bellard
    addr &= 1;
238 6515b203 bellard
#ifdef DEBUG
239 ab1e34ad bellard
    printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr, val);
240 6515b203 bellard
#endif
241 ab1e34ad bellard
    if (addr == 0) {
242 ab1e34ad bellard
        s->apmc = val;
243 24bc1cbc ths
244 24bc1cbc ths
        /* ACPI specs 3.0, 4.7.2.5 */
245 24bc1cbc ths
        if (val == ACPI_ENABLE) {
246 24bc1cbc ths
            s->pmcntrl |= SCI_EN;
247 24bc1cbc ths
        } else if (val == ACPI_DISABLE) {
248 24bc1cbc ths
            s->pmcntrl &= ~SCI_EN;
249 24bc1cbc ths
        }
250 24bc1cbc ths
251 47d02f6d bellard
        if (s->dev.config[0x5b] & (1 << 1)) {
252 53b67b30 Blue Swirl
            if (s->smi_irq) {
253 53b67b30 Blue Swirl
                qemu_irq_raise(s->smi_irq);
254 53b67b30 Blue Swirl
            }
255 ab1e34ad bellard
        }
256 ab1e34ad bellard
    } else {
257 ab1e34ad bellard
        s->apms = val;
258 6515b203 bellard
    }
259 6515b203 bellard
}
260 6515b203 bellard
261 ab1e34ad bellard
static uint32_t pm_smi_readb(void *opaque, uint32_t addr)
262 ab1e34ad bellard
{
263 ab1e34ad bellard
    PIIX4PMState *s = opaque;
264 ab1e34ad bellard
    uint32_t val;
265 3b46e624 ths
266 ab1e34ad bellard
    addr &= 1;
267 ab1e34ad bellard
    if (addr == 0) {
268 ab1e34ad bellard
        val = s->apmc;
269 ab1e34ad bellard
    } else {
270 ab1e34ad bellard
        val = s->apms;
271 ab1e34ad bellard
    }
272 ab1e34ad bellard
#ifdef DEBUG
273 ab1e34ad bellard
    printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr, val);
274 ab1e34ad bellard
#endif
275 ab1e34ad bellard
    return val;
276 ab1e34ad bellard
}
277 ab1e34ad bellard
278 6515b203 bellard
static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
279 6515b203 bellard
{
280 6515b203 bellard
#if defined(DEBUG)
281 6515b203 bellard
    printf("ACPI: DBG: 0x%08x\n", val);
282 6515b203 bellard
#endif
283 6515b203 bellard
}
284 6515b203 bellard
285 3fffc223 ths
static void smb_transaction(PIIX4PMState *s)
286 3fffc223 ths
{
287 3fffc223 ths
    uint8_t prot = (s->smb_ctl >> 2) & 0x07;
288 3fffc223 ths
    uint8_t read = s->smb_addr & 0x01;
289 3fffc223 ths
    uint8_t cmd = s->smb_cmd;
290 3fffc223 ths
    uint8_t addr = s->smb_addr >> 1;
291 0ff596d0 pbrook
    i2c_bus *bus = s->smbus;
292 3fffc223 ths
293 3fffc223 ths
#ifdef DEBUG
294 3fffc223 ths
    printf("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
295 3fffc223 ths
#endif
296 3fffc223 ths
    switch(prot) {
297 3fffc223 ths
    case 0x0:
298 0ff596d0 pbrook
        smbus_quick_command(bus, addr, read);
299 3fffc223 ths
        break;
300 3fffc223 ths
    case 0x1:
301 3fffc223 ths
        if (read) {
302 0ff596d0 pbrook
            s->smb_data0 = smbus_receive_byte(bus, addr);
303 0ff596d0 pbrook
        } else {
304 0ff596d0 pbrook
            smbus_send_byte(bus, addr, cmd);
305 3fffc223 ths
        }
306 3fffc223 ths
        break;
307 3fffc223 ths
    case 0x2:
308 3fffc223 ths
        if (read) {
309 0ff596d0 pbrook
            s->smb_data0 = smbus_read_byte(bus, addr, cmd);
310 0ff596d0 pbrook
        } else {
311 0ff596d0 pbrook
            smbus_write_byte(bus, addr, cmd, s->smb_data0);
312 3fffc223 ths
        }
313 3fffc223 ths
        break;
314 3fffc223 ths
    case 0x3:
315 3fffc223 ths
        if (read) {
316 3fffc223 ths
            uint16_t val;
317 0ff596d0 pbrook
            val = smbus_read_word(bus, addr, cmd);
318 3fffc223 ths
            s->smb_data0 = val;
319 3fffc223 ths
            s->smb_data1 = val >> 8;
320 0ff596d0 pbrook
        } else {
321 0ff596d0 pbrook
            smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
322 3fffc223 ths
        }
323 3fffc223 ths
        break;
324 3fffc223 ths
    case 0x5:
325 3fffc223 ths
        if (read) {
326 0ff596d0 pbrook
            s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
327 0ff596d0 pbrook
        } else {
328 0ff596d0 pbrook
            smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
329 3fffc223 ths
        }
330 3fffc223 ths
        break;
331 3fffc223 ths
    default:
332 3fffc223 ths
        goto error;
333 3fffc223 ths
    }
334 3fffc223 ths
    return;
335 3fffc223 ths
336 3fffc223 ths
  error:
337 3fffc223 ths
    s->smb_stat |= 0x04;
338 3fffc223 ths
}
339 3fffc223 ths
340 3fffc223 ths
static void smb_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
341 3fffc223 ths
{
342 3fffc223 ths
    PIIX4PMState *s = opaque;
343 3fffc223 ths
    addr &= 0x3f;
344 3fffc223 ths
#ifdef DEBUG
345 3fffc223 ths
    printf("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
346 3fffc223 ths
#endif
347 3fffc223 ths
    switch(addr) {
348 3fffc223 ths
    case SMBHSTSTS:
349 3fffc223 ths
        s->smb_stat = 0;
350 3fffc223 ths
        s->smb_index = 0;
351 3fffc223 ths
        break;
352 3fffc223 ths
    case SMBHSTCNT:
353 3fffc223 ths
        s->smb_ctl = val;
354 3fffc223 ths
        if (val & 0x40)
355 3fffc223 ths
            smb_transaction(s);
356 3fffc223 ths
        break;
357 3fffc223 ths
    case SMBHSTCMD:
358 3fffc223 ths
        s->smb_cmd = val;
359 3fffc223 ths
        break;
360 3fffc223 ths
    case SMBHSTADD:
361 3fffc223 ths
        s->smb_addr = val;
362 3fffc223 ths
        break;
363 3fffc223 ths
    case SMBHSTDAT0:
364 3fffc223 ths
        s->smb_data0 = val;
365 3fffc223 ths
        break;
366 3fffc223 ths
    case SMBHSTDAT1:
367 3fffc223 ths
        s->smb_data1 = val;
368 3fffc223 ths
        break;
369 3fffc223 ths
    case SMBBLKDAT:
370 3fffc223 ths
        s->smb_data[s->smb_index++] = val;
371 3fffc223 ths
        if (s->smb_index > 31)
372 3fffc223 ths
            s->smb_index = 0;
373 3fffc223 ths
        break;
374 3fffc223 ths
    default:
375 3fffc223 ths
        break;
376 3fffc223 ths
    }
377 3fffc223 ths
}
378 3fffc223 ths
379 3fffc223 ths
static uint32_t smb_ioport_readb(void *opaque, uint32_t addr)
380 3fffc223 ths
{
381 3fffc223 ths
    PIIX4PMState *s = opaque;
382 3fffc223 ths
    uint32_t val;
383 3fffc223 ths
384 3fffc223 ths
    addr &= 0x3f;
385 3fffc223 ths
    switch(addr) {
386 3fffc223 ths
    case SMBHSTSTS:
387 3fffc223 ths
        val = s->smb_stat;
388 3fffc223 ths
        break;
389 3fffc223 ths
    case SMBHSTCNT:
390 3fffc223 ths
        s->smb_index = 0;
391 3fffc223 ths
        val = s->smb_ctl & 0x1f;
392 3fffc223 ths
        break;
393 3fffc223 ths
    case SMBHSTCMD:
394 3fffc223 ths
        val = s->smb_cmd;
395 3fffc223 ths
        break;
396 3fffc223 ths
    case SMBHSTADD:
397 3fffc223 ths
        val = s->smb_addr;
398 3fffc223 ths
        break;
399 3fffc223 ths
    case SMBHSTDAT0:
400 3fffc223 ths
        val = s->smb_data0;
401 3fffc223 ths
        break;
402 3fffc223 ths
    case SMBHSTDAT1:
403 3fffc223 ths
        val = s->smb_data1;
404 3fffc223 ths
        break;
405 3fffc223 ths
    case SMBBLKDAT:
406 3fffc223 ths
        val = s->smb_data[s->smb_index++];
407 3fffc223 ths
        if (s->smb_index > 31)
408 3fffc223 ths
            s->smb_index = 0;
409 3fffc223 ths
        break;
410 3fffc223 ths
    default:
411 3fffc223 ths
        val = 0;
412 3fffc223 ths
        break;
413 3fffc223 ths
    }
414 3fffc223 ths
#ifdef DEBUG
415 3fffc223 ths
    printf("SMB readb port=0x%04x val=0x%02x\n", addr, val);
416 3fffc223 ths
#endif
417 3fffc223 ths
    return val;
418 3fffc223 ths
}
419 3fffc223 ths
420 ab1e34ad bellard
static void pm_io_space_update(PIIX4PMState *s)
421 ab1e34ad bellard
{
422 ab1e34ad bellard
    uint32_t pm_io_base;
423 ab1e34ad bellard
424 ab1e34ad bellard
    if (s->dev.config[0x80] & 1) {
425 ab1e34ad bellard
        pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
426 bf367b54 ths
        pm_io_base &= 0xffc0;
427 ab1e34ad bellard
428 ab1e34ad bellard
        /* XXX: need to improve memory and ioport allocation */
429 ab1e34ad bellard
#if defined(DEBUG)
430 ab1e34ad bellard
        printf("PM: mapping to 0x%x\n", pm_io_base);
431 ab1e34ad bellard
#endif
432 ab1e34ad bellard
        register_ioport_write(pm_io_base, 64, 2, pm_ioport_writew, s);
433 ab1e34ad bellard
        register_ioport_read(pm_io_base, 64, 2, pm_ioport_readw, s);
434 ab1e34ad bellard
        register_ioport_write(pm_io_base, 64, 4, pm_ioport_writel, s);
435 ab1e34ad bellard
        register_ioport_read(pm_io_base, 64, 4, pm_ioport_readl, s);
436 ab1e34ad bellard
    }
437 ab1e34ad bellard
}
438 ab1e34ad bellard
439 5fafdf24 ths
static void pm_write_config(PCIDevice *d,
440 ab1e34ad bellard
                            uint32_t address, uint32_t val, int len)
441 ab1e34ad bellard
{
442 ab1e34ad bellard
    pci_default_write_config(d, address, val, len);
443 a40e3411 Isaku Yamahata
    if (range_covers_byte(address, len, 0x80))
444 ab1e34ad bellard
        pm_io_space_update((PIIX4PMState *)d);
445 ab1e34ad bellard
}
446 ab1e34ad bellard
447 e59fb374 Juan Quintela
static int vmstate_acpi_post_load(void *opaque, int version_id)
448 ab1e34ad bellard
{
449 ab1e34ad bellard
    PIIX4PMState *s = opaque;
450 ab1e34ad bellard
451 ab1e34ad bellard
    pm_io_space_update(s);
452 ab1e34ad bellard
    return 0;
453 ab1e34ad bellard
}
454 ab1e34ad bellard
455 76dec49f Juan Quintela
static const VMStateDescription vmstate_acpi = {
456 76dec49f Juan Quintela
    .name = "piix4_pm",
457 76dec49f Juan Quintela
    .version_id = 1,
458 76dec49f Juan Quintela
    .minimum_version_id = 1,
459 76dec49f Juan Quintela
    .minimum_version_id_old = 1,
460 752ff2fa Juan Quintela
    .post_load = vmstate_acpi_post_load,
461 76dec49f Juan Quintela
    .fields      = (VMStateField []) {
462 76dec49f Juan Quintela
        VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
463 76dec49f Juan Quintela
        VMSTATE_UINT16(pmsts, PIIX4PMState),
464 76dec49f Juan Quintela
        VMSTATE_UINT16(pmen, PIIX4PMState),
465 76dec49f Juan Quintela
        VMSTATE_UINT16(pmcntrl, PIIX4PMState),
466 76dec49f Juan Quintela
        VMSTATE_UINT8(apmc, PIIX4PMState),
467 76dec49f Juan Quintela
        VMSTATE_UINT8(apms, PIIX4PMState),
468 76dec49f Juan Quintela
        VMSTATE_TIMER(tmr_timer, PIIX4PMState),
469 76dec49f Juan Quintela
        VMSTATE_INT64(tmr_overflow_time, PIIX4PMState),
470 76dec49f Juan Quintela
        VMSTATE_END_OF_LIST()
471 76dec49f Juan Quintela
    }
472 76dec49f Juan Quintela
};
473 76dec49f Juan Quintela
474 0bacd130 aliguori
static void piix4_reset(void *opaque)
475 0bacd130 aliguori
{
476 3c892168 aliguori
    PIIX4PMState *s = opaque;
477 3c892168 aliguori
    uint8_t *pci_conf = s->dev.config;
478 3c892168 aliguori
479 3c892168 aliguori
    pci_conf[0x58] = 0;
480 3c892168 aliguori
    pci_conf[0x59] = 0;
481 3c892168 aliguori
    pci_conf[0x5a] = 0;
482 3c892168 aliguori
    pci_conf[0x5b] = 0;
483 0bacd130 aliguori
484 53b67b30 Blue Swirl
    if (s->kvm_enabled) {
485 3c892168 aliguori
        /* Mark SMM as already inited (until KVM supports SMM). */
486 3c892168 aliguori
        pci_conf[0x5B] = 0x02;
487 3c892168 aliguori
    }
488 0bacd130 aliguori
}
489 0bacd130 aliguori
490 d9c32310 Blue Swirl
static void piix4_powerdown(void *opaque, int irq, int power_failing)
491 d9c32310 Blue Swirl
{
492 d9c32310 Blue Swirl
    PIIX4PMState *s = opaque;
493 d9c32310 Blue Swirl
494 d9c32310 Blue Swirl
    if (!s) {
495 d9c32310 Blue Swirl
        qemu_system_shutdown_request();
496 d9c32310 Blue Swirl
    } else if (s->pmen & PWRBTN_EN) {
497 d9c32310 Blue Swirl
        s->pmsts |= PWRBTN_EN;
498 d9c32310 Blue Swirl
        pm_update_sci(s);
499 d9c32310 Blue Swirl
    }
500 d9c32310 Blue Swirl
}
501 d9c32310 Blue Swirl
502 cf7a2fe2 aurel32
i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
503 53b67b30 Blue Swirl
                       qemu_irq sci_irq, qemu_irq cmos_s3, qemu_irq smi_irq,
504 53b67b30 Blue Swirl
                       int kvm_enabled)
505 6515b203 bellard
{
506 6515b203 bellard
    PIIX4PMState *s;
507 6515b203 bellard
    uint8_t *pci_conf;
508 6515b203 bellard
509 6515b203 bellard
    s = (PIIX4PMState *)pci_register_device(bus,
510 6515b203 bellard
                                         "PM", sizeof(PIIX4PMState),
511 ab1e34ad bellard
                                         devfn, NULL, pm_write_config);
512 cf7a2fe2 aurel32
    pm_state = s;
513 6515b203 bellard
    pci_conf = s->dev.config;
514 deb54399 aliguori
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
515 deb54399 aliguori
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_3);
516 bf367b54 ths
    pci_conf[0x06] = 0x80;
517 bf367b54 ths
    pci_conf[0x07] = 0x02;
518 a78b03cb balrog
    pci_conf[0x08] = 0x03; // revision number
519 6515b203 bellard
    pci_conf[0x09] = 0x00;
520 173a543b blueswir1
    pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);
521 6407f373 Isaku Yamahata
    pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
522 6515b203 bellard
    pci_conf[0x3d] = 0x01; // interrupt pin 1
523 3b46e624 ths
524 ab1e34ad bellard
    pci_conf[0x40] = 0x01; /* PM io base read only bit */
525 3b46e624 ths
526 ab1e34ad bellard
    register_ioport_write(0xb2, 2, 1, pm_smi_writeb, s);
527 ab1e34ad bellard
    register_ioport_read(0xb2, 2, 1, pm_smi_readb, s);
528 ab1e34ad bellard
529 6515b203 bellard
    register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
530 6515b203 bellard
531 b33612d0 Anthony Liguori
    s->kvm_enabled = kvm_enabled;
532 b33612d0 Anthony Liguori
    if (s->kvm_enabled) {
533 7ba1e619 aliguori
        /* Mark SMM as already inited to prevent SMM from running.  KVM does not
534 7ba1e619 aliguori
         * support SMM mode. */
535 7ba1e619 aliguori
        pci_conf[0x5B] = 0x02;
536 7ba1e619 aliguori
    }
537 7ba1e619 aliguori
538 1ce549ab bellard
    /* XXX: which specification is used ? The i82731AB has different
539 1ce549ab bellard
       mappings */
540 1ce549ab bellard
    pci_conf[0x5f] = (parallel_hds[0] != NULL ? 0x80 : 0) | 0x10;
541 1ce549ab bellard
    pci_conf[0x63] = 0x60;
542 1ce549ab bellard
    pci_conf[0x67] = (serial_hds[0] != NULL ? 0x08 : 0) |
543 1ce549ab bellard
        (serial_hds[1] != NULL ? 0x90 : 0);
544 1ce549ab bellard
545 3fffc223 ths
    pci_conf[0x90] = smb_io_base | 1;
546 3fffc223 ths
    pci_conf[0x91] = smb_io_base >> 8;
547 3fffc223 ths
    pci_conf[0xd2] = 0x09;
548 3fffc223 ths
    register_ioport_write(smb_io_base, 64, 1, smb_ioport_writeb, s);
549 3fffc223 ths
    register_ioport_read(smb_io_base, 64, 1, smb_ioport_readb, s);
550 3fffc223 ths
551 6515b203 bellard
    s->tmr_timer = qemu_new_timer(vm_clock, pm_tmr_timer, s);
552 6515b203 bellard
553 d9c32310 Blue Swirl
    qemu_system_powerdown = *qemu_allocate_irqs(piix4_powerdown, s, 1);
554 d9c32310 Blue Swirl
555 76dec49f Juan Quintela
    vmstate_register(0, &vmstate_acpi, s);
556 3fffc223 ths
557 02e2da45 Paul Brook
    s->smbus = i2c_init_bus(NULL, "i2c");
558 cf7a2fe2 aurel32
    s->irq = sci_irq;
559 53b67b30 Blue Swirl
    s->cmos_s3 = cmos_s3;
560 53b67b30 Blue Swirl
    s->smi_irq = smi_irq;
561 a08d4367 Jan Kiszka
    qemu_register_reset(piix4_reset, s);
562 0bacd130 aliguori
563 0ff596d0 pbrook
    return s->smbus;
564 6515b203 bellard
}
565 cf7a2fe2 aurel32
566 5e3cb534 aliguori
#define GPE_BASE 0xafe0
567 ca2c72be aliguori
#define PCI_BASE 0xae00
568 ca2c72be aliguori
#define PCI_EJ_BASE 0xae08
569 5e3cb534 aliguori
570 5e3cb534 aliguori
struct gpe_regs {
571 5e3cb534 aliguori
    uint16_t sts; /* status */
572 5e3cb534 aliguori
    uint16_t en;  /* enabled */
573 5e3cb534 aliguori
};
574 5e3cb534 aliguori
575 ca2c72be aliguori
struct pci_status {
576 ca2c72be aliguori
    uint32_t up;
577 ca2c72be aliguori
    uint32_t down;
578 ca2c72be aliguori
};
579 ca2c72be aliguori
580 5e3cb534 aliguori
static struct gpe_regs gpe;
581 ca2c72be aliguori
static struct pci_status pci0_status;
582 5e3cb534 aliguori
583 6eb011b0 aliguori
static uint32_t gpe_read_val(uint16_t val, uint32_t addr)
584 6eb011b0 aliguori
{
585 6eb011b0 aliguori
    if (addr & 1)
586 6eb011b0 aliguori
        return (val >> 8) & 0xff;
587 6eb011b0 aliguori
    return val & 0xff;
588 6eb011b0 aliguori
}
589 6eb011b0 aliguori
590 5e3cb534 aliguori
static uint32_t gpe_readb(void *opaque, uint32_t addr)
591 5e3cb534 aliguori
{
592 5e3cb534 aliguori
    uint32_t val = 0;
593 5e3cb534 aliguori
    struct gpe_regs *g = opaque;
594 5e3cb534 aliguori
    switch (addr) {
595 5e3cb534 aliguori
        case GPE_BASE:
596 5e3cb534 aliguori
        case GPE_BASE + 1:
597 6eb011b0 aliguori
            val = gpe_read_val(g->sts, addr);
598 5e3cb534 aliguori
            break;
599 5e3cb534 aliguori
        case GPE_BASE + 2:
600 5e3cb534 aliguori
        case GPE_BASE + 3:
601 6eb011b0 aliguori
            val = gpe_read_val(g->en, addr);
602 5e3cb534 aliguori
            break;
603 5e3cb534 aliguori
        default:
604 5e3cb534 aliguori
            break;
605 5e3cb534 aliguori
    }
606 5e3cb534 aliguori
607 5e3cb534 aliguori
#if defined(DEBUG)
608 f654d9e2 Alex Williamson
    printf("gpe read %x == %x\n", addr, val);
609 5e3cb534 aliguori
#endif
610 5e3cb534 aliguori
    return val;
611 5e3cb534 aliguori
}
612 5e3cb534 aliguori
613 6eb011b0 aliguori
static void gpe_write_val(uint16_t *cur, int addr, uint32_t val)
614 6eb011b0 aliguori
{
615 6eb011b0 aliguori
    if (addr & 1)
616 6eb011b0 aliguori
        *cur = (*cur & 0xff) | (val << 8);
617 6eb011b0 aliguori
    else
618 6eb011b0 aliguori
        *cur = (*cur & 0xff00) | (val & 0xff);
619 6eb011b0 aliguori
}
620 6eb011b0 aliguori
621 6eb011b0 aliguori
static void gpe_reset_val(uint16_t *cur, int addr, uint32_t val)
622 6eb011b0 aliguori
{
623 6eb011b0 aliguori
    uint16_t x1, x0 = val & 0xff;
624 6eb011b0 aliguori
    int shift = (addr & 1) ? 8 : 0;
625 6eb011b0 aliguori
626 6eb011b0 aliguori
    x1 = (*cur >> shift) & 0xff;
627 6eb011b0 aliguori
628 6eb011b0 aliguori
    x1 = x1 & ~x0;
629 6eb011b0 aliguori
630 6eb011b0 aliguori
    *cur = (*cur & (0xff << (8 - shift))) | (x1 << shift);
631 6eb011b0 aliguori
}
632 6eb011b0 aliguori
633 5e3cb534 aliguori
static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
634 5e3cb534 aliguori
{
635 5e3cb534 aliguori
    struct gpe_regs *g = opaque;
636 5e3cb534 aliguori
    switch (addr) {
637 5e3cb534 aliguori
        case GPE_BASE:
638 5e3cb534 aliguori
        case GPE_BASE + 1:
639 6eb011b0 aliguori
            gpe_reset_val(&g->sts, addr, val);
640 5e3cb534 aliguori
            break;
641 5e3cb534 aliguori
        case GPE_BASE + 2:
642 5e3cb534 aliguori
        case GPE_BASE + 3:
643 6eb011b0 aliguori
            gpe_write_val(&g->en, addr, val);
644 5e3cb534 aliguori
            break;
645 5e3cb534 aliguori
        default:
646 5e3cb534 aliguori
            break;
647 5e3cb534 aliguori
   }
648 5e3cb534 aliguori
649 5e3cb534 aliguori
#if defined(DEBUG)
650 f654d9e2 Alex Williamson
    printf("gpe write %x <== %d\n", addr, val);
651 5e3cb534 aliguori
#endif
652 5e3cb534 aliguori
}
653 5e3cb534 aliguori
654 ca2c72be aliguori
static uint32_t pcihotplug_read(void *opaque, uint32_t addr)
655 ca2c72be aliguori
{
656 ca2c72be aliguori
    uint32_t val = 0;
657 ca2c72be aliguori
    struct pci_status *g = opaque;
658 ca2c72be aliguori
    switch (addr) {
659 ca2c72be aliguori
        case PCI_BASE:
660 ca2c72be aliguori
            val = g->up;
661 ca2c72be aliguori
            break;
662 ca2c72be aliguori
        case PCI_BASE + 4:
663 ca2c72be aliguori
            val = g->down;
664 ca2c72be aliguori
            break;
665 ca2c72be aliguori
        default:
666 ca2c72be aliguori
            break;
667 ca2c72be aliguori
    }
668 ca2c72be aliguori
669 ca2c72be aliguori
#if defined(DEBUG)
670 f654d9e2 Alex Williamson
    printf("pcihotplug read %x == %x\n", addr, val);
671 ca2c72be aliguori
#endif
672 ca2c72be aliguori
    return val;
673 ca2c72be aliguori
}
674 ca2c72be aliguori
675 ca2c72be aliguori
static void pcihotplug_write(void *opaque, uint32_t addr, uint32_t val)
676 ca2c72be aliguori
{
677 ca2c72be aliguori
    struct pci_status *g = opaque;
678 ca2c72be aliguori
    switch (addr) {
679 ca2c72be aliguori
        case PCI_BASE:
680 ca2c72be aliguori
            g->up = val;
681 ca2c72be aliguori
            break;
682 ca2c72be aliguori
        case PCI_BASE + 4:
683 ca2c72be aliguori
            g->down = val;
684 ca2c72be aliguori
            break;
685 ca2c72be aliguori
   }
686 ca2c72be aliguori
687 ca2c72be aliguori
#if defined(DEBUG)
688 f654d9e2 Alex Williamson
    printf("pcihotplug write %x <== %d\n", addr, val);
689 ca2c72be aliguori
#endif
690 ca2c72be aliguori
}
691 ca2c72be aliguori
692 ca2c72be aliguori
static uint32_t pciej_read(void *opaque, uint32_t addr)
693 ca2c72be aliguori
{
694 ca2c72be aliguori
#if defined(DEBUG)
695 f654d9e2 Alex Williamson
    printf("pciej read %x\n", addr);
696 ca2c72be aliguori
#endif
697 ca2c72be aliguori
    return 0;
698 ca2c72be aliguori
}
699 ca2c72be aliguori
700 ca2c72be aliguori
static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
701 ca2c72be aliguori
{
702 3f84865a Gerd Hoffmann
    BusState *bus = opaque;
703 98449371 Mark McLoughlin
    DeviceState *qdev, *next;
704 3f84865a Gerd Hoffmann
    PCIDevice *dev;
705 ca2c72be aliguori
    int slot = ffs(val) - 1;
706 ca2c72be aliguori
707 98449371 Mark McLoughlin
    QLIST_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
708 3f84865a Gerd Hoffmann
        dev = DO_UPCAST(PCIDevice, qdev, qdev);
709 3f84865a Gerd Hoffmann
        if (PCI_SLOT(dev->devfn) == slot) {
710 3f84865a Gerd Hoffmann
            qdev_free(qdev);
711 3f84865a Gerd Hoffmann
        }
712 3f84865a Gerd Hoffmann
    }
713 3f84865a Gerd Hoffmann
714 6f338c34 aliguori
715 ca2c72be aliguori
#if defined(DEBUG)
716 f654d9e2 Alex Williamson
    printf("pciej write %x <== %d\n", addr, val);
717 ca2c72be aliguori
#endif
718 ca2c72be aliguori
}
719 ca2c72be aliguori
720 3f84865a Gerd Hoffmann
static int piix4_device_hotplug(PCIDevice *dev, int state);
721 9d5e77a2 Isaku Yamahata
722 3f84865a Gerd Hoffmann
void piix4_acpi_system_hot_add_init(PCIBus *bus)
723 5e3cb534 aliguori
{
724 5e3cb534 aliguori
    register_ioport_write(GPE_BASE, 4, 1, gpe_writeb, &gpe);
725 5e3cb534 aliguori
    register_ioport_read(GPE_BASE, 4, 1,  gpe_readb, &gpe);
726 5e3cb534 aliguori
727 ca2c72be aliguori
    register_ioport_write(PCI_BASE, 8, 4, pcihotplug_write, &pci0_status);
728 ca2c72be aliguori
    register_ioport_read(PCI_BASE, 8, 4,  pcihotplug_read, &pci0_status);
729 ca2c72be aliguori
730 3f84865a Gerd Hoffmann
    register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, bus);
731 3f84865a Gerd Hoffmann
    register_ioport_read(PCI_EJ_BASE, 4, 4,  pciej_read, bus);
732 9d5e77a2 Isaku Yamahata
733 3f84865a Gerd Hoffmann
    pci_bus_hotplug(bus, piix4_device_hotplug);
734 ca2c72be aliguori
}
735 ca2c72be aliguori
736 ca2c72be aliguori
static void enable_device(struct pci_status *p, struct gpe_regs *g, int slot)
737 ca2c72be aliguori
{
738 ca2c72be aliguori
    g->sts |= 2;
739 ca2c72be aliguori
    p->up |= (1 << slot);
740 ca2c72be aliguori
}
741 ca2c72be aliguori
742 ca2c72be aliguori
static void disable_device(struct pci_status *p, struct gpe_regs *g, int slot)
743 ca2c72be aliguori
{
744 ca2c72be aliguori
    g->sts |= 2;
745 ca2c72be aliguori
    p->down |= (1 << slot);
746 ca2c72be aliguori
}
747 ca2c72be aliguori
748 3f84865a Gerd Hoffmann
static int piix4_device_hotplug(PCIDevice *dev, int state)
749 ca2c72be aliguori
{
750 3f84865a Gerd Hoffmann
    int slot = PCI_SLOT(dev->devfn);
751 3f84865a Gerd Hoffmann
752 ca2c72be aliguori
    pci0_status.up = 0;
753 ca2c72be aliguori
    pci0_status.down = 0;
754 ca2c72be aliguori
    if (state)
755 ca2c72be aliguori
        enable_device(&pci0_status, &gpe, slot);
756 ca2c72be aliguori
    else
757 ca2c72be aliguori
        disable_device(&pci0_status, &gpe, slot);
758 1f0711e2 aliguori
    if (gpe.en & 2) {
759 1f0711e2 aliguori
        qemu_set_irq(pm_state->irq, 1);
760 1f0711e2 aliguori
        qemu_set_irq(pm_state->irq, 0);
761 1f0711e2 aliguori
    }
762 3f84865a Gerd Hoffmann
    return 0;
763 9d5e77a2 Isaku Yamahata
}
764 9d5e77a2 Isaku Yamahata
765 8a92ea2f aliguori
struct acpi_table_header
766 8a92ea2f aliguori
{
767 8a92ea2f aliguori
    char signature [4];    /* ACPI signature (4 ASCII characters) */
768 8a92ea2f aliguori
    uint32_t length;          /* Length of table, in bytes, including header */
769 8a92ea2f aliguori
    uint8_t revision;         /* ACPI Specification minor version # */
770 8a92ea2f aliguori
    uint8_t checksum;         /* To make sum of entire table == 0 */
771 8a92ea2f aliguori
    char oem_id [6];       /* OEM identification */
772 8a92ea2f aliguori
    char oem_table_id [8]; /* OEM table identification */
773 8a92ea2f aliguori
    uint32_t oem_revision;    /* OEM revision number */
774 8a92ea2f aliguori
    char asl_compiler_id [4]; /* ASL compiler vendor ID */
775 8a92ea2f aliguori
    uint32_t asl_compiler_revision; /* ASL compiler revision number */
776 8a92ea2f aliguori
} __attribute__((packed));
777 8a92ea2f aliguori
778 8a92ea2f aliguori
char *acpi_tables;
779 8a92ea2f aliguori
size_t acpi_tables_len;
780 8a92ea2f aliguori
781 8a92ea2f aliguori
static int acpi_checksum(const uint8_t *data, int len)
782 8a92ea2f aliguori
{
783 8a92ea2f aliguori
    int sum, i;
784 8a92ea2f aliguori
    sum = 0;
785 8a92ea2f aliguori
    for(i = 0; i < len; i++)
786 8a92ea2f aliguori
        sum += data[i];
787 8a92ea2f aliguori
    return (-sum) & 0xff;
788 8a92ea2f aliguori
}
789 8a92ea2f aliguori
790 8a92ea2f aliguori
int acpi_table_add(const char *t)
791 8a92ea2f aliguori
{
792 8a92ea2f aliguori
    static const char *dfl_id = "QEMUQEMU";
793 8a92ea2f aliguori
    char buf[1024], *p, *f;
794 8a92ea2f aliguori
    struct acpi_table_header acpi_hdr;
795 8a92ea2f aliguori
    unsigned long val;
796 8a92ea2f aliguori
    size_t off;
797 8a92ea2f aliguori
798 8a92ea2f aliguori
    memset(&acpi_hdr, 0, sizeof(acpi_hdr));
799 8a92ea2f aliguori
  
800 8a92ea2f aliguori
    if (get_param_value(buf, sizeof(buf), "sig", t)) {
801 8a92ea2f aliguori
        strncpy(acpi_hdr.signature, buf, 4);
802 8a92ea2f aliguori
    } else {
803 8a92ea2f aliguori
        strncpy(acpi_hdr.signature, dfl_id, 4);
804 8a92ea2f aliguori
    }
805 8a92ea2f aliguori
    if (get_param_value(buf, sizeof(buf), "rev", t)) {
806 8a92ea2f aliguori
        val = strtoul(buf, &p, 10);
807 8a92ea2f aliguori
        if (val > 255 || *p != '\0')
808 8a92ea2f aliguori
            goto out;
809 8a92ea2f aliguori
    } else {
810 8a92ea2f aliguori
        val = 1;
811 8a92ea2f aliguori
    }
812 8a92ea2f aliguori
    acpi_hdr.revision = (int8_t)val;
813 8a92ea2f aliguori
814 8a92ea2f aliguori
    if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
815 8a92ea2f aliguori
        strncpy(acpi_hdr.oem_id, buf, 6);
816 8a92ea2f aliguori
    } else {
817 8a92ea2f aliguori
        strncpy(acpi_hdr.oem_id, dfl_id, 6);
818 8a92ea2f aliguori
    }
819 8a92ea2f aliguori
820 8a92ea2f aliguori
    if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
821 8a92ea2f aliguori
        strncpy(acpi_hdr.oem_table_id, buf, 8);
822 8a92ea2f aliguori
    } else {
823 8a92ea2f aliguori
        strncpy(acpi_hdr.oem_table_id, dfl_id, 8);
824 8a92ea2f aliguori
    }
825 8a92ea2f aliguori
826 8a92ea2f aliguori
    if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
827 8a92ea2f aliguori
        val = strtol(buf, &p, 10);
828 8a92ea2f aliguori
        if(*p != '\0')
829 8a92ea2f aliguori
            goto out;
830 8a92ea2f aliguori
    } else {
831 8a92ea2f aliguori
        val = 1;
832 8a92ea2f aliguori
    }
833 8a92ea2f aliguori
    acpi_hdr.oem_revision = cpu_to_le32(val);
834 8a92ea2f aliguori
835 8a92ea2f aliguori
    if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
836 8a92ea2f aliguori
        strncpy(acpi_hdr.asl_compiler_id, buf, 4);
837 8a92ea2f aliguori
    } else {
838 8a92ea2f aliguori
        strncpy(acpi_hdr.asl_compiler_id, dfl_id, 4);
839 8a92ea2f aliguori
    }
840 8a92ea2f aliguori
841 8a92ea2f aliguori
    if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
842 8a92ea2f aliguori
        val = strtol(buf, &p, 10);
843 8a92ea2f aliguori
        if(*p != '\0')
844 8a92ea2f aliguori
            goto out;
845 8a92ea2f aliguori
    } else {
846 8a92ea2f aliguori
        val = 1;
847 8a92ea2f aliguori
    }
848 8a92ea2f aliguori
    acpi_hdr.asl_compiler_revision = cpu_to_le32(val);
849 8a92ea2f aliguori
    
850 8a92ea2f aliguori
    if (!get_param_value(buf, sizeof(buf), "data", t)) {
851 8a92ea2f aliguori
         buf[0] = '\0';
852 8a92ea2f aliguori
    }
853 8a92ea2f aliguori
854 8a92ea2f aliguori
    acpi_hdr.length = sizeof(acpi_hdr);
855 8a92ea2f aliguori
856 8a92ea2f aliguori
    f = buf;
857 8a92ea2f aliguori
    while (buf[0]) {
858 8a92ea2f aliguori
        struct stat s;
859 54042bcf aliguori
        char *n = strchr(f, ':');
860 8a92ea2f aliguori
        if (n)
861 8a92ea2f aliguori
            *n = '\0';
862 8a92ea2f aliguori
        if(stat(f, &s) < 0) {
863 8a92ea2f aliguori
            fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
864 8a92ea2f aliguori
            goto out;
865 8a92ea2f aliguori
        }
866 8a92ea2f aliguori
        acpi_hdr.length += s.st_size;
867 8a92ea2f aliguori
        if (!n)
868 8a92ea2f aliguori
            break;
869 8a92ea2f aliguori
        *n = ':';
870 8a92ea2f aliguori
        f = n + 1;
871 8a92ea2f aliguori
    }
872 8a92ea2f aliguori
873 8a92ea2f aliguori
    if (!acpi_tables) {
874 8a92ea2f aliguori
        acpi_tables_len = sizeof(uint16_t);
875 8a92ea2f aliguori
        acpi_tables = qemu_mallocz(acpi_tables_len);
876 8a92ea2f aliguori
    }
877 8a92ea2f aliguori
    p = acpi_tables + acpi_tables_len;
878 8a92ea2f aliguori
    acpi_tables_len += sizeof(uint16_t) + acpi_hdr.length;
879 8a92ea2f aliguori
    acpi_tables = qemu_realloc(acpi_tables, acpi_tables_len);
880 8a92ea2f aliguori
881 8a92ea2f aliguori
    acpi_hdr.length = cpu_to_le32(acpi_hdr.length);
882 8a92ea2f aliguori
    *(uint16_t*)p = acpi_hdr.length;
883 8a92ea2f aliguori
    p += sizeof(uint16_t);
884 8a92ea2f aliguori
    memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
885 8a92ea2f aliguori
    off = sizeof(acpi_hdr);
886 8a92ea2f aliguori
887 8a92ea2f aliguori
    f = buf;
888 8a92ea2f aliguori
    while (buf[0]) {
889 8a92ea2f aliguori
        struct stat s;
890 8a92ea2f aliguori
        int fd;
891 54042bcf aliguori
        char *n = strchr(f, ':');
892 8a92ea2f aliguori
        if (n)
893 8a92ea2f aliguori
            *n = '\0';
894 8a92ea2f aliguori
        fd = open(f, O_RDONLY);
895 8a92ea2f aliguori
896 8a92ea2f aliguori
        if(fd < 0)
897 8a92ea2f aliguori
            goto out;
898 8a92ea2f aliguori
        if(fstat(fd, &s) < 0) {
899 8a92ea2f aliguori
            close(fd);
900 8a92ea2f aliguori
            goto out;
901 8a92ea2f aliguori
        }
902 8a92ea2f aliguori
903 8a92ea2f aliguori
        do {
904 8a92ea2f aliguori
            int r;
905 8a92ea2f aliguori
            r = read(fd, p + off, s.st_size);
906 8a92ea2f aliguori
            if (r > 0) {
907 8a92ea2f aliguori
                off += r;
908 8a92ea2f aliguori
                s.st_size -= r;
909 8a92ea2f aliguori
            } else if ((r < 0 && errno != EINTR) || r == 0) {
910 8a92ea2f aliguori
                close(fd);
911 8a92ea2f aliguori
                goto out;
912 8a92ea2f aliguori
            }
913 8a92ea2f aliguori
        } while(s.st_size);
914 8a92ea2f aliguori
915 8a92ea2f aliguori
        close(fd);
916 8a92ea2f aliguori
        if (!n)
917 8a92ea2f aliguori
            break;
918 8a92ea2f aliguori
        f = n + 1;
919 8a92ea2f aliguori
    }
920 8a92ea2f aliguori
921 8a92ea2f aliguori
    ((struct acpi_table_header*)p)->checksum = acpi_checksum((uint8_t*)p, off);
922 8a92ea2f aliguori
    /* increase number of tables */
923 8a92ea2f aliguori
    (*(uint16_t*)acpi_tables) =
924 8a92ea2f aliguori
            cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
925 8a92ea2f aliguori
    return 0;
926 8a92ea2f aliguori
out:
927 8a92ea2f aliguori
    if (acpi_tables) {
928 b2538b4b Jean-Christophe DUBOIS
        qemu_free(acpi_tables);
929 8a92ea2f aliguori
        acpi_tables = NULL;
930 8a92ea2f aliguori
    }
931 8a92ea2f aliguori
    return -1;
932 8a92ea2f aliguori
}