Statistics
| Branch: | Revision:

root / hw / acpi.c @ 563e3c6e

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