Statistics
| Branch: | Revision:

root / hw / acpi.c @ e57ec016

History | View | Annotate | Download (12.5 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 6515b203 bellard
 * License along with this library; if not, write to the Free Software
17 6515b203 bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 6515b203 bellard
 */
19 87ecb68b pbrook
#include "hw.h"
20 87ecb68b pbrook
#include "pc.h"
21 87ecb68b pbrook
#include "pci.h"
22 87ecb68b pbrook
#include "qemu-timer.h"
23 87ecb68b pbrook
#include "sysemu.h"
24 87ecb68b pbrook
#include "i2c.h"
25 87ecb68b pbrook
#include "smbus.h"
26 6515b203 bellard
27 6515b203 bellard
//#define DEBUG
28 6515b203 bellard
29 6515b203 bellard
/* i82731AB (PIIX4) compatible power management function */
30 6515b203 bellard
#define PM_FREQ 3579545
31 6515b203 bellard
32 6515b203 bellard
#define ACPI_DBG_IO_ADDR  0xb044
33 6515b203 bellard
34 6515b203 bellard
typedef struct PIIX4PMState {
35 6515b203 bellard
    PCIDevice dev;
36 6515b203 bellard
    uint16_t pmsts;
37 6515b203 bellard
    uint16_t pmen;
38 6515b203 bellard
    uint16_t pmcntrl;
39 ab1e34ad bellard
    uint8_t apmc;
40 ab1e34ad bellard
    uint8_t apms;
41 6515b203 bellard
    QEMUTimer *tmr_timer;
42 6515b203 bellard
    int64_t tmr_overflow_time;
43 0ff596d0 pbrook
    i2c_bus *smbus;
44 3fffc223 ths
    uint8_t smb_stat;
45 3fffc223 ths
    uint8_t smb_ctl;
46 3fffc223 ths
    uint8_t smb_cmd;
47 3fffc223 ths
    uint8_t smb_addr;
48 3fffc223 ths
    uint8_t smb_data0;
49 3fffc223 ths
    uint8_t smb_data1;
50 3fffc223 ths
    uint8_t smb_data[32];
51 3fffc223 ths
    uint8_t smb_index;
52 6515b203 bellard
} PIIX4PMState;
53 6515b203 bellard
54 6515b203 bellard
#define RTC_EN (1 << 10)
55 6515b203 bellard
#define PWRBTN_EN (1 << 8)
56 6515b203 bellard
#define GBL_EN (1 << 5)
57 6515b203 bellard
#define TMROF_EN (1 << 0)
58 6515b203 bellard
59 6515b203 bellard
#define SCI_EN (1 << 0)
60 6515b203 bellard
61 6515b203 bellard
#define SUS_EN (1 << 13)
62 6515b203 bellard
63 24bc1cbc ths
#define ACPI_ENABLE 0xf1
64 24bc1cbc ths
#define ACPI_DISABLE 0xf0
65 24bc1cbc ths
66 3fffc223 ths
#define SMBHSTSTS 0x00
67 3fffc223 ths
#define SMBHSTCNT 0x02
68 3fffc223 ths
#define SMBHSTCMD 0x03
69 3fffc223 ths
#define SMBHSTADD 0x04
70 3fffc223 ths
#define SMBHSTDAT0 0x05
71 3fffc223 ths
#define SMBHSTDAT1 0x06
72 3fffc223 ths
#define SMBBLKDAT 0x07
73 3fffc223 ths
74 6515b203 bellard
static uint32_t get_pmtmr(PIIX4PMState *s)
75 6515b203 bellard
{
76 6515b203 bellard
    uint32_t d;
77 6515b203 bellard
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
78 6515b203 bellard
    return d & 0xffffff;
79 6515b203 bellard
}
80 6515b203 bellard
81 6515b203 bellard
static int get_pmsts(PIIX4PMState *s)
82 6515b203 bellard
{
83 6515b203 bellard
    int64_t d;
84 6515b203 bellard
    int pmsts;
85 6515b203 bellard
    pmsts = s->pmsts;
86 6515b203 bellard
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
87 6515b203 bellard
    if (d >= s->tmr_overflow_time)
88 6515b203 bellard
        s->pmsts |= TMROF_EN;
89 6515b203 bellard
    return pmsts;
90 6515b203 bellard
}
91 6515b203 bellard
92 6515b203 bellard
static void pm_update_sci(PIIX4PMState *s)
93 6515b203 bellard
{
94 6515b203 bellard
    int sci_level, pmsts;
95 6515b203 bellard
    int64_t expire_time;
96 3b46e624 ths
97 6515b203 bellard
    pmsts = get_pmsts(s);
98 5fafdf24 ths
    sci_level = (((pmsts & s->pmen) &
99 6515b203 bellard
                  (RTC_EN | PWRBTN_EN | GBL_EN | TMROF_EN)) != 0);
100 d537cf6c pbrook
    qemu_set_irq(s->dev.irq[0], sci_level);
101 6515b203 bellard
    /* schedule a timer interruption if needed */
102 6515b203 bellard
    if ((s->pmen & TMROF_EN) && !(pmsts & TMROF_EN)) {
103 6515b203 bellard
        expire_time = muldiv64(s->tmr_overflow_time, ticks_per_sec, PM_FREQ);
104 6515b203 bellard
        qemu_mod_timer(s->tmr_timer, expire_time);
105 6515b203 bellard
    } else {
106 6515b203 bellard
        qemu_del_timer(s->tmr_timer);
107 6515b203 bellard
    }
108 6515b203 bellard
}
109 6515b203 bellard
110 6515b203 bellard
static void pm_tmr_timer(void *opaque)
111 6515b203 bellard
{
112 6515b203 bellard
    PIIX4PMState *s = opaque;
113 6515b203 bellard
    pm_update_sci(s);
114 6515b203 bellard
}
115 6515b203 bellard
116 6515b203 bellard
static void pm_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
117 6515b203 bellard
{
118 6515b203 bellard
    PIIX4PMState *s = opaque;
119 6515b203 bellard
    addr &= 0x3f;
120 6515b203 bellard
    switch(addr) {
121 6515b203 bellard
    case 0x00:
122 6515b203 bellard
        {
123 6515b203 bellard
            int64_t d;
124 6515b203 bellard
            int pmsts;
125 6515b203 bellard
            pmsts = get_pmsts(s);
126 6515b203 bellard
            if (pmsts & val & TMROF_EN) {
127 6515b203 bellard
                /* if TMRSTS is reset, then compute the new overflow time */
128 6515b203 bellard
                d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
129 6515b203 bellard
                s->tmr_overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
130 6515b203 bellard
            }
131 6515b203 bellard
            s->pmsts &= ~val;
132 6515b203 bellard
            pm_update_sci(s);
133 6515b203 bellard
        }
134 6515b203 bellard
        break;
135 6515b203 bellard
    case 0x02:
136 6515b203 bellard
        s->pmen = val;
137 6515b203 bellard
        pm_update_sci(s);
138 6515b203 bellard
        break;
139 6515b203 bellard
    case 0x04:
140 6515b203 bellard
        {
141 6515b203 bellard
            int sus_typ;
142 6515b203 bellard
            s->pmcntrl = val & ~(SUS_EN);
143 6515b203 bellard
            if (val & SUS_EN) {
144 6515b203 bellard
                /* change suspend type */
145 6515b203 bellard
                sus_typ = (val >> 10) & 3;
146 6515b203 bellard
                switch(sus_typ) {
147 6515b203 bellard
                case 0: /* soft power off */
148 6515b203 bellard
                    qemu_system_shutdown_request();
149 6515b203 bellard
                    break;
150 6515b203 bellard
                default:
151 6515b203 bellard
                    break;
152 6515b203 bellard
                }
153 6515b203 bellard
            }
154 6515b203 bellard
        }
155 6515b203 bellard
        break;
156 6515b203 bellard
    default:
157 6515b203 bellard
        break;
158 6515b203 bellard
    }
159 6515b203 bellard
#ifdef DEBUG
160 6515b203 bellard
    printf("PM writew port=0x%04x val=0x%04x\n", addr, val);
161 6515b203 bellard
#endif
162 6515b203 bellard
}
163 6515b203 bellard
164 6515b203 bellard
static uint32_t pm_ioport_readw(void *opaque, uint32_t addr)
165 6515b203 bellard
{
166 6515b203 bellard
    PIIX4PMState *s = opaque;
167 6515b203 bellard
    uint32_t val;
168 6515b203 bellard
169 6515b203 bellard
    addr &= 0x3f;
170 6515b203 bellard
    switch(addr) {
171 6515b203 bellard
    case 0x00:
172 6515b203 bellard
        val = get_pmsts(s);
173 6515b203 bellard
        break;
174 6515b203 bellard
    case 0x02:
175 6515b203 bellard
        val = s->pmen;
176 6515b203 bellard
        break;
177 6515b203 bellard
    case 0x04:
178 6515b203 bellard
        val = s->pmcntrl;
179 6515b203 bellard
        break;
180 6515b203 bellard
    default:
181 6515b203 bellard
        val = 0;
182 6515b203 bellard
        break;
183 6515b203 bellard
    }
184 6515b203 bellard
#ifdef DEBUG
185 6515b203 bellard
    printf("PM readw port=0x%04x val=0x%04x\n", addr, val);
186 6515b203 bellard
#endif
187 6515b203 bellard
    return val;
188 6515b203 bellard
}
189 6515b203 bellard
190 6515b203 bellard
static void pm_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
191 6515b203 bellard
{
192 6515b203 bellard
    //    PIIX4PMState *s = opaque;
193 6515b203 bellard
    addr &= 0x3f;
194 6515b203 bellard
#ifdef DEBUG
195 6515b203 bellard
    printf("PM writel port=0x%04x val=0x%08x\n", addr, val);
196 6515b203 bellard
#endif
197 6515b203 bellard
}
198 6515b203 bellard
199 6515b203 bellard
static uint32_t pm_ioport_readl(void *opaque, uint32_t addr)
200 6515b203 bellard
{
201 6515b203 bellard
    PIIX4PMState *s = opaque;
202 6515b203 bellard
    uint32_t val;
203 6515b203 bellard
204 6515b203 bellard
    addr &= 0x3f;
205 6515b203 bellard
    switch(addr) {
206 6515b203 bellard
    case 0x08:
207 6515b203 bellard
        val = get_pmtmr(s);
208 6515b203 bellard
        break;
209 6515b203 bellard
    default:
210 6515b203 bellard
        val = 0;
211 6515b203 bellard
        break;
212 6515b203 bellard
    }
213 6515b203 bellard
#ifdef DEBUG
214 6515b203 bellard
    printf("PM readl port=0x%04x val=0x%08x\n", addr, val);
215 6515b203 bellard
#endif
216 6515b203 bellard
    return val;
217 6515b203 bellard
}
218 6515b203 bellard
219 ab1e34ad bellard
static void pm_smi_writeb(void *opaque, uint32_t addr, uint32_t val)
220 6515b203 bellard
{
221 6515b203 bellard
    PIIX4PMState *s = opaque;
222 ab1e34ad bellard
    addr &= 1;
223 6515b203 bellard
#ifdef DEBUG
224 ab1e34ad bellard
    printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr, val);
225 6515b203 bellard
#endif
226 ab1e34ad bellard
    if (addr == 0) {
227 ab1e34ad bellard
        s->apmc = val;
228 24bc1cbc ths
229 24bc1cbc ths
        /* ACPI specs 3.0, 4.7.2.5 */
230 24bc1cbc ths
        if (val == ACPI_ENABLE) {
231 24bc1cbc ths
            s->pmcntrl |= SCI_EN;
232 24bc1cbc ths
        } else if (val == ACPI_DISABLE) {
233 24bc1cbc ths
            s->pmcntrl &= ~SCI_EN;
234 24bc1cbc ths
        }
235 24bc1cbc ths
236 47d02f6d bellard
        if (s->dev.config[0x5b] & (1 << 1)) {
237 47d02f6d bellard
            cpu_interrupt(first_cpu, CPU_INTERRUPT_SMI);
238 ab1e34ad bellard
        }
239 ab1e34ad bellard
    } else {
240 ab1e34ad bellard
        s->apms = val;
241 6515b203 bellard
    }
242 6515b203 bellard
}
243 6515b203 bellard
244 ab1e34ad bellard
static uint32_t pm_smi_readb(void *opaque, uint32_t addr)
245 ab1e34ad bellard
{
246 ab1e34ad bellard
    PIIX4PMState *s = opaque;
247 ab1e34ad bellard
    uint32_t val;
248 3b46e624 ths
249 ab1e34ad bellard
    addr &= 1;
250 ab1e34ad bellard
    if (addr == 0) {
251 ab1e34ad bellard
        val = s->apmc;
252 ab1e34ad bellard
    } else {
253 ab1e34ad bellard
        val = s->apms;
254 ab1e34ad bellard
    }
255 ab1e34ad bellard
#ifdef DEBUG
256 ab1e34ad bellard
    printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr, val);
257 ab1e34ad bellard
#endif
258 ab1e34ad bellard
    return val;
259 ab1e34ad bellard
}
260 ab1e34ad bellard
261 6515b203 bellard
static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
262 6515b203 bellard
{
263 6515b203 bellard
#if defined(DEBUG)
264 6515b203 bellard
    printf("ACPI: DBG: 0x%08x\n", val);
265 6515b203 bellard
#endif
266 6515b203 bellard
}
267 6515b203 bellard
268 3fffc223 ths
static void smb_transaction(PIIX4PMState *s)
269 3fffc223 ths
{
270 3fffc223 ths
    uint8_t prot = (s->smb_ctl >> 2) & 0x07;
271 3fffc223 ths
    uint8_t read = s->smb_addr & 0x01;
272 3fffc223 ths
    uint8_t cmd = s->smb_cmd;
273 3fffc223 ths
    uint8_t addr = s->smb_addr >> 1;
274 0ff596d0 pbrook
    i2c_bus *bus = s->smbus;
275 3fffc223 ths
276 3fffc223 ths
#ifdef DEBUG
277 3fffc223 ths
    printf("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
278 3fffc223 ths
#endif
279 3fffc223 ths
    switch(prot) {
280 3fffc223 ths
    case 0x0:
281 0ff596d0 pbrook
        smbus_quick_command(bus, addr, read);
282 3fffc223 ths
        break;
283 3fffc223 ths
    case 0x1:
284 3fffc223 ths
        if (read) {
285 0ff596d0 pbrook
            s->smb_data0 = smbus_receive_byte(bus, addr);
286 0ff596d0 pbrook
        } else {
287 0ff596d0 pbrook
            smbus_send_byte(bus, addr, cmd);
288 3fffc223 ths
        }
289 3fffc223 ths
        break;
290 3fffc223 ths
    case 0x2:
291 3fffc223 ths
        if (read) {
292 0ff596d0 pbrook
            s->smb_data0 = smbus_read_byte(bus, addr, cmd);
293 0ff596d0 pbrook
        } else {
294 0ff596d0 pbrook
            smbus_write_byte(bus, addr, cmd, s->smb_data0);
295 3fffc223 ths
        }
296 3fffc223 ths
        break;
297 3fffc223 ths
    case 0x3:
298 3fffc223 ths
        if (read) {
299 3fffc223 ths
            uint16_t val;
300 0ff596d0 pbrook
            val = smbus_read_word(bus, addr, cmd);
301 3fffc223 ths
            s->smb_data0 = val;
302 3fffc223 ths
            s->smb_data1 = val >> 8;
303 0ff596d0 pbrook
        } else {
304 0ff596d0 pbrook
            smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
305 3fffc223 ths
        }
306 3fffc223 ths
        break;
307 3fffc223 ths
    case 0x5:
308 3fffc223 ths
        if (read) {
309 0ff596d0 pbrook
            s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
310 0ff596d0 pbrook
        } else {
311 0ff596d0 pbrook
            smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
312 3fffc223 ths
        }
313 3fffc223 ths
        break;
314 3fffc223 ths
    default:
315 3fffc223 ths
        goto error;
316 3fffc223 ths
    }
317 3fffc223 ths
    return;
318 3fffc223 ths
319 3fffc223 ths
  error:
320 3fffc223 ths
    s->smb_stat |= 0x04;
321 3fffc223 ths
}
322 3fffc223 ths
323 3fffc223 ths
static void smb_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
324 3fffc223 ths
{
325 3fffc223 ths
    PIIX4PMState *s = opaque;
326 3fffc223 ths
    addr &= 0x3f;
327 3fffc223 ths
#ifdef DEBUG
328 3fffc223 ths
    printf("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
329 3fffc223 ths
#endif
330 3fffc223 ths
    switch(addr) {
331 3fffc223 ths
    case SMBHSTSTS:
332 3fffc223 ths
        s->smb_stat = 0;
333 3fffc223 ths
        s->smb_index = 0;
334 3fffc223 ths
        break;
335 3fffc223 ths
    case SMBHSTCNT:
336 3fffc223 ths
        s->smb_ctl = val;
337 3fffc223 ths
        if (val & 0x40)
338 3fffc223 ths
            smb_transaction(s);
339 3fffc223 ths
        break;
340 3fffc223 ths
    case SMBHSTCMD:
341 3fffc223 ths
        s->smb_cmd = val;
342 3fffc223 ths
        break;
343 3fffc223 ths
    case SMBHSTADD:
344 3fffc223 ths
        s->smb_addr = val;
345 3fffc223 ths
        break;
346 3fffc223 ths
    case SMBHSTDAT0:
347 3fffc223 ths
        s->smb_data0 = val;
348 3fffc223 ths
        break;
349 3fffc223 ths
    case SMBHSTDAT1:
350 3fffc223 ths
        s->smb_data1 = val;
351 3fffc223 ths
        break;
352 3fffc223 ths
    case SMBBLKDAT:
353 3fffc223 ths
        s->smb_data[s->smb_index++] = val;
354 3fffc223 ths
        if (s->smb_index > 31)
355 3fffc223 ths
            s->smb_index = 0;
356 3fffc223 ths
        break;
357 3fffc223 ths
    default:
358 3fffc223 ths
        break;
359 3fffc223 ths
    }
360 3fffc223 ths
}
361 3fffc223 ths
362 3fffc223 ths
static uint32_t smb_ioport_readb(void *opaque, uint32_t addr)
363 3fffc223 ths
{
364 3fffc223 ths
    PIIX4PMState *s = opaque;
365 3fffc223 ths
    uint32_t val;
366 3fffc223 ths
367 3fffc223 ths
    addr &= 0x3f;
368 3fffc223 ths
    switch(addr) {
369 3fffc223 ths
    case SMBHSTSTS:
370 3fffc223 ths
        val = s->smb_stat;
371 3fffc223 ths
        break;
372 3fffc223 ths
    case SMBHSTCNT:
373 3fffc223 ths
        s->smb_index = 0;
374 3fffc223 ths
        val = s->smb_ctl & 0x1f;
375 3fffc223 ths
        break;
376 3fffc223 ths
    case SMBHSTCMD:
377 3fffc223 ths
        val = s->smb_cmd;
378 3fffc223 ths
        break;
379 3fffc223 ths
    case SMBHSTADD:
380 3fffc223 ths
        val = s->smb_addr;
381 3fffc223 ths
        break;
382 3fffc223 ths
    case SMBHSTDAT0:
383 3fffc223 ths
        val = s->smb_data0;
384 3fffc223 ths
        break;
385 3fffc223 ths
    case SMBHSTDAT1:
386 3fffc223 ths
        val = s->smb_data1;
387 3fffc223 ths
        break;
388 3fffc223 ths
    case SMBBLKDAT:
389 3fffc223 ths
        val = s->smb_data[s->smb_index++];
390 3fffc223 ths
        if (s->smb_index > 31)
391 3fffc223 ths
            s->smb_index = 0;
392 3fffc223 ths
        break;
393 3fffc223 ths
    default:
394 3fffc223 ths
        val = 0;
395 3fffc223 ths
        break;
396 3fffc223 ths
    }
397 3fffc223 ths
#ifdef DEBUG
398 3fffc223 ths
    printf("SMB readb port=0x%04x val=0x%02x\n", addr, val);
399 3fffc223 ths
#endif
400 3fffc223 ths
    return val;
401 3fffc223 ths
}
402 3fffc223 ths
403 ab1e34ad bellard
static void pm_io_space_update(PIIX4PMState *s)
404 ab1e34ad bellard
{
405 ab1e34ad bellard
    uint32_t pm_io_base;
406 ab1e34ad bellard
407 ab1e34ad bellard
    if (s->dev.config[0x80] & 1) {
408 ab1e34ad bellard
        pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
409 bf367b54 ths
        pm_io_base &= 0xffc0;
410 ab1e34ad bellard
411 ab1e34ad bellard
        /* XXX: need to improve memory and ioport allocation */
412 ab1e34ad bellard
#if defined(DEBUG)
413 ab1e34ad bellard
        printf("PM: mapping to 0x%x\n", pm_io_base);
414 ab1e34ad bellard
#endif
415 ab1e34ad bellard
        register_ioport_write(pm_io_base, 64, 2, pm_ioport_writew, s);
416 ab1e34ad bellard
        register_ioport_read(pm_io_base, 64, 2, pm_ioport_readw, s);
417 ab1e34ad bellard
        register_ioport_write(pm_io_base, 64, 4, pm_ioport_writel, s);
418 ab1e34ad bellard
        register_ioport_read(pm_io_base, 64, 4, pm_ioport_readl, s);
419 ab1e34ad bellard
    }
420 ab1e34ad bellard
}
421 ab1e34ad bellard
422 5fafdf24 ths
static void pm_write_config(PCIDevice *d,
423 ab1e34ad bellard
                            uint32_t address, uint32_t val, int len)
424 ab1e34ad bellard
{
425 ab1e34ad bellard
    pci_default_write_config(d, address, val, len);
426 ab1e34ad bellard
    if (address == 0x80)
427 ab1e34ad bellard
        pm_io_space_update((PIIX4PMState *)d);
428 ab1e34ad bellard
}
429 ab1e34ad bellard
430 ab1e34ad bellard
static void pm_save(QEMUFile* f,void *opaque)
431 ab1e34ad bellard
{
432 ab1e34ad bellard
    PIIX4PMState *s = opaque;
433 ab1e34ad bellard
434 ab1e34ad bellard
    pci_device_save(&s->dev, f);
435 ab1e34ad bellard
436 ab1e34ad bellard
    qemu_put_be16s(f, &s->pmsts);
437 ab1e34ad bellard
    qemu_put_be16s(f, &s->pmen);
438 ab1e34ad bellard
    qemu_put_be16s(f, &s->pmcntrl);
439 ab1e34ad bellard
    qemu_put_8s(f, &s->apmc);
440 ab1e34ad bellard
    qemu_put_8s(f, &s->apms);
441 ab1e34ad bellard
    qemu_put_timer(f, s->tmr_timer);
442 ab1e34ad bellard
    qemu_put_be64s(f, &s->tmr_overflow_time);
443 ab1e34ad bellard
}
444 ab1e34ad bellard
445 ab1e34ad bellard
static int pm_load(QEMUFile* f,void* opaque,int version_id)
446 ab1e34ad bellard
{
447 ab1e34ad bellard
    PIIX4PMState *s = opaque;
448 ab1e34ad bellard
    int ret;
449 ab1e34ad bellard
450 ab1e34ad bellard
    if (version_id > 1)
451 ab1e34ad bellard
        return -EINVAL;
452 ab1e34ad bellard
453 ab1e34ad bellard
    ret = pci_device_load(&s->dev, f);
454 ab1e34ad bellard
    if (ret < 0)
455 ab1e34ad bellard
        return ret;
456 ab1e34ad bellard
457 ab1e34ad bellard
    qemu_get_be16s(f, &s->pmsts);
458 ab1e34ad bellard
    qemu_get_be16s(f, &s->pmen);
459 ab1e34ad bellard
    qemu_get_be16s(f, &s->pmcntrl);
460 ab1e34ad bellard
    qemu_get_8s(f, &s->apmc);
461 ab1e34ad bellard
    qemu_get_8s(f, &s->apms);
462 ab1e34ad bellard
    qemu_get_timer(f, s->tmr_timer);
463 ab1e34ad bellard
    qemu_get_be64s(f, &s->tmr_overflow_time);
464 ab1e34ad bellard
465 ab1e34ad bellard
    pm_io_space_update(s);
466 ab1e34ad bellard
467 ab1e34ad bellard
    return 0;
468 ab1e34ad bellard
}
469 ab1e34ad bellard
470 7b717336 ths
i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base)
471 6515b203 bellard
{
472 6515b203 bellard
    PIIX4PMState *s;
473 6515b203 bellard
    uint8_t *pci_conf;
474 6515b203 bellard
475 6515b203 bellard
    s = (PIIX4PMState *)pci_register_device(bus,
476 6515b203 bellard
                                         "PM", sizeof(PIIX4PMState),
477 ab1e34ad bellard
                                         devfn, NULL, pm_write_config);
478 6515b203 bellard
    pci_conf = s->dev.config;
479 6515b203 bellard
    pci_conf[0x00] = 0x86;
480 6515b203 bellard
    pci_conf[0x01] = 0x80;
481 6515b203 bellard
    pci_conf[0x02] = 0x13;
482 7ef4da1c bellard
    pci_conf[0x03] = 0x71;
483 bf367b54 ths
    pci_conf[0x06] = 0x80;
484 bf367b54 ths
    pci_conf[0x07] = 0x02;
485 6515b203 bellard
    pci_conf[0x08] = 0x00; // revision number
486 6515b203 bellard
    pci_conf[0x09] = 0x00;
487 6515b203 bellard
    pci_conf[0x0a] = 0x80; // other bridge device
488 6515b203 bellard
    pci_conf[0x0b] = 0x06; // bridge device
489 6515b203 bellard
    pci_conf[0x0e] = 0x00; // header_type
490 6515b203 bellard
    pci_conf[0x3d] = 0x01; // interrupt pin 1
491 3b46e624 ths
492 ab1e34ad bellard
    pci_conf[0x40] = 0x01; /* PM io base read only bit */
493 3b46e624 ths
494 ab1e34ad bellard
    register_ioport_write(0xb2, 2, 1, pm_smi_writeb, s);
495 ab1e34ad bellard
    register_ioport_read(0xb2, 2, 1, pm_smi_readb, s);
496 ab1e34ad bellard
497 6515b203 bellard
    register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
498 6515b203 bellard
499 1ce549ab bellard
    /* XXX: which specification is used ? The i82731AB has different
500 1ce549ab bellard
       mappings */
501 1ce549ab bellard
    pci_conf[0x5f] = (parallel_hds[0] != NULL ? 0x80 : 0) | 0x10;
502 1ce549ab bellard
    pci_conf[0x63] = 0x60;
503 1ce549ab bellard
    pci_conf[0x67] = (serial_hds[0] != NULL ? 0x08 : 0) |
504 1ce549ab bellard
        (serial_hds[1] != NULL ? 0x90 : 0);
505 1ce549ab bellard
506 3fffc223 ths
    pci_conf[0x90] = smb_io_base | 1;
507 3fffc223 ths
    pci_conf[0x91] = smb_io_base >> 8;
508 3fffc223 ths
    pci_conf[0xd2] = 0x09;
509 3fffc223 ths
    register_ioport_write(smb_io_base, 64, 1, smb_ioport_writeb, s);
510 3fffc223 ths
    register_ioport_read(smb_io_base, 64, 1, smb_ioport_readb, s);
511 3fffc223 ths
512 6515b203 bellard
    s->tmr_timer = qemu_new_timer(vm_clock, pm_tmr_timer, s);
513 6515b203 bellard
514 ab1e34ad bellard
    register_savevm("piix4_pm", 0, 1, pm_save, pm_load, s);
515 3fffc223 ths
516 0ff596d0 pbrook
    s->smbus = i2c_init_bus();
517 0ff596d0 pbrook
    return s->smbus;
518 6515b203 bellard
}