Statistics
| Branch: | Revision:

root / hw / acpi.c @ 33ac7f16

History | View | Annotate | Download (12.1 kB)

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