Statistics
| Branch: | Revision:

root / hw / acpi.c @ aa71cf80

History | View | Annotate | Download (13.6 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 fad6cb1a aurel32
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 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 7ba1e619 aliguori
#include "kvm.h"
27 6515b203 bellard
28 6515b203 bellard
//#define DEBUG
29 6515b203 bellard
30 6515b203 bellard
/* i82731AB (PIIX4) compatible power management function */
31 6515b203 bellard
#define PM_FREQ 3579545
32 6515b203 bellard
33 6515b203 bellard
#define ACPI_DBG_IO_ADDR  0xb044
34 6515b203 bellard
35 6515b203 bellard
typedef struct PIIX4PMState {
36 6515b203 bellard
    PCIDevice dev;
37 6515b203 bellard
    uint16_t pmsts;
38 6515b203 bellard
    uint16_t pmen;
39 6515b203 bellard
    uint16_t pmcntrl;
40 ab1e34ad bellard
    uint8_t apmc;
41 ab1e34ad bellard
    uint8_t apms;
42 6515b203 bellard
    QEMUTimer *tmr_timer;
43 6515b203 bellard
    int64_t tmr_overflow_time;
44 0ff596d0 pbrook
    i2c_bus *smbus;
45 3fffc223 ths
    uint8_t smb_stat;
46 3fffc223 ths
    uint8_t smb_ctl;
47 3fffc223 ths
    uint8_t smb_cmd;
48 3fffc223 ths
    uint8_t smb_addr;
49 3fffc223 ths
    uint8_t smb_data0;
50 3fffc223 ths
    uint8_t smb_data1;
51 3fffc223 ths
    uint8_t smb_data[32];
52 3fffc223 ths
    uint8_t smb_index;
53 cf7a2fe2 aurel32
    qemu_irq irq;
54 6515b203 bellard
} PIIX4PMState;
55 6515b203 bellard
56 0bacd130 aliguori
#define RSM_STS (1 << 15)
57 0bacd130 aliguori
#define PWRBTN_STS (1 << 8)
58 6515b203 bellard
#define RTC_EN (1 << 10)
59 6515b203 bellard
#define PWRBTN_EN (1 << 8)
60 6515b203 bellard
#define GBL_EN (1 << 5)
61 6515b203 bellard
#define TMROF_EN (1 << 0)
62 6515b203 bellard
63 6515b203 bellard
#define SCI_EN (1 << 0)
64 6515b203 bellard
65 6515b203 bellard
#define SUS_EN (1 << 13)
66 6515b203 bellard
67 24bc1cbc ths
#define ACPI_ENABLE 0xf1
68 24bc1cbc ths
#define ACPI_DISABLE 0xf0
69 24bc1cbc ths
70 3fffc223 ths
#define SMBHSTSTS 0x00
71 3fffc223 ths
#define SMBHSTCNT 0x02
72 3fffc223 ths
#define SMBHSTCMD 0x03
73 3fffc223 ths
#define SMBHSTADD 0x04
74 3fffc223 ths
#define SMBHSTDAT0 0x05
75 3fffc223 ths
#define SMBHSTDAT1 0x06
76 3fffc223 ths
#define SMBBLKDAT 0x07
77 3fffc223 ths
78 9669d3c5 aurel32
static PIIX4PMState *pm_state;
79 cf7a2fe2 aurel32
80 6515b203 bellard
static uint32_t get_pmtmr(PIIX4PMState *s)
81 6515b203 bellard
{
82 7546c016 balrog
    uint32_t d;
83 7546c016 balrog
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
84 7546c016 balrog
    return d & 0xffffff;
85 6515b203 bellard
}
86 6515b203 bellard
87 6515b203 bellard
static int get_pmsts(PIIX4PMState *s)
88 6515b203 bellard
{
89 7546c016 balrog
    int64_t d;
90 7546c016 balrog
    int pmsts;
91 7546c016 balrog
    pmsts = s->pmsts;
92 7546c016 balrog
    d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, 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 7546c016 balrog
        expire_time = muldiv64(s->tmr_overflow_time, 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 7546c016 balrog
                d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec);
135 7546c016 balrog
                s->tmr_overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
136 7546c016 balrog
            }
137 7546c016 balrog
            s->pmsts &= ~val;
138 7546c016 balrog
            pm_update_sci(s);
139 7546c016 balrog
        }
140 6515b203 bellard
        break;
141 6515b203 bellard
    case 0x02:
142 6515b203 bellard
        s->pmen = val;
143 6515b203 bellard
        pm_update_sci(s);
144 6515b203 bellard
        break;
145 6515b203 bellard
    case 0x04:
146 6515b203 bellard
        {
147 6515b203 bellard
            int sus_typ;
148 6515b203 bellard
            s->pmcntrl = val & ~(SUS_EN);
149 6515b203 bellard
            if (val & SUS_EN) {
150 6515b203 bellard
                /* change suspend type */
151 f99ed40a aurel32
                sus_typ = (val >> 10) & 7;
152 6515b203 bellard
                switch(sus_typ) {
153 6515b203 bellard
                case 0: /* soft power off */
154 6515b203 bellard
                    qemu_system_shutdown_request();
155 6515b203 bellard
                    break;
156 0bacd130 aliguori
                case 1:
157 0bacd130 aliguori
                    /* RSM_STS should be set on resume. Pretend that resume
158 0bacd130 aliguori
                       was caused by power button */
159 0bacd130 aliguori
                    s->pmsts |= (RSM_STS | PWRBTN_STS);
160 0bacd130 aliguori
                    qemu_system_reset_request();
161 0bacd130 aliguori
#if defined(TARGET_I386)
162 0bacd130 aliguori
                    cmos_set_s3_resume();
163 0bacd130 aliguori
#endif
164 6515b203 bellard
                default:
165 6515b203 bellard
                    break;
166 6515b203 bellard
                }
167 6515b203 bellard
            }
168 6515b203 bellard
        }
169 6515b203 bellard
        break;
170 6515b203 bellard
    default:
171 6515b203 bellard
        break;
172 6515b203 bellard
    }
173 6515b203 bellard
#ifdef DEBUG
174 6515b203 bellard
    printf("PM writew port=0x%04x val=0x%04x\n", addr, val);
175 6515b203 bellard
#endif
176 6515b203 bellard
}
177 6515b203 bellard
178 6515b203 bellard
static uint32_t pm_ioport_readw(void *opaque, uint32_t addr)
179 6515b203 bellard
{
180 6515b203 bellard
    PIIX4PMState *s = opaque;
181 6515b203 bellard
    uint32_t val;
182 6515b203 bellard
183 6515b203 bellard
    addr &= 0x3f;
184 6515b203 bellard
    switch(addr) {
185 6515b203 bellard
    case 0x00:
186 6515b203 bellard
        val = get_pmsts(s);
187 6515b203 bellard
        break;
188 6515b203 bellard
    case 0x02:
189 6515b203 bellard
        val = s->pmen;
190 6515b203 bellard
        break;
191 6515b203 bellard
    case 0x04:
192 6515b203 bellard
        val = s->pmcntrl;
193 6515b203 bellard
        break;
194 6515b203 bellard
    default:
195 6515b203 bellard
        val = 0;
196 6515b203 bellard
        break;
197 6515b203 bellard
    }
198 6515b203 bellard
#ifdef DEBUG
199 6515b203 bellard
    printf("PM readw port=0x%04x val=0x%04x\n", addr, val);
200 6515b203 bellard
#endif
201 6515b203 bellard
    return val;
202 6515b203 bellard
}
203 6515b203 bellard
204 6515b203 bellard
static void pm_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
205 6515b203 bellard
{
206 6515b203 bellard
    //    PIIX4PMState *s = opaque;
207 6515b203 bellard
    addr &= 0x3f;
208 6515b203 bellard
#ifdef DEBUG
209 6515b203 bellard
    printf("PM writel port=0x%04x val=0x%08x\n", addr, val);
210 6515b203 bellard
#endif
211 6515b203 bellard
}
212 6515b203 bellard
213 6515b203 bellard
static uint32_t pm_ioport_readl(void *opaque, uint32_t addr)
214 6515b203 bellard
{
215 6515b203 bellard
    PIIX4PMState *s = opaque;
216 6515b203 bellard
    uint32_t val;
217 6515b203 bellard
218 6515b203 bellard
    addr &= 0x3f;
219 6515b203 bellard
    switch(addr) {
220 6515b203 bellard
    case 0x08:
221 6515b203 bellard
        val = get_pmtmr(s);
222 6515b203 bellard
        break;
223 6515b203 bellard
    default:
224 6515b203 bellard
        val = 0;
225 6515b203 bellard
        break;
226 6515b203 bellard
    }
227 6515b203 bellard
#ifdef DEBUG
228 6515b203 bellard
    printf("PM readl port=0x%04x val=0x%08x\n", addr, val);
229 6515b203 bellard
#endif
230 6515b203 bellard
    return val;
231 6515b203 bellard
}
232 6515b203 bellard
233 ab1e34ad bellard
static void pm_smi_writeb(void *opaque, uint32_t addr, uint32_t val)
234 6515b203 bellard
{
235 6515b203 bellard
    PIIX4PMState *s = opaque;
236 ab1e34ad bellard
    addr &= 1;
237 6515b203 bellard
#ifdef DEBUG
238 ab1e34ad bellard
    printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr, val);
239 6515b203 bellard
#endif
240 ab1e34ad bellard
    if (addr == 0) {
241 ab1e34ad bellard
        s->apmc = val;
242 24bc1cbc ths
243 24bc1cbc ths
        /* ACPI specs 3.0, 4.7.2.5 */
244 24bc1cbc ths
        if (val == ACPI_ENABLE) {
245 24bc1cbc ths
            s->pmcntrl |= SCI_EN;
246 24bc1cbc ths
        } else if (val == ACPI_DISABLE) {
247 24bc1cbc ths
            s->pmcntrl &= ~SCI_EN;
248 24bc1cbc ths
        }
249 24bc1cbc ths
250 47d02f6d bellard
        if (s->dev.config[0x5b] & (1 << 1)) {
251 47d02f6d bellard
            cpu_interrupt(first_cpu, CPU_INTERRUPT_SMI);
252 ab1e34ad bellard
        }
253 ab1e34ad bellard
    } else {
254 ab1e34ad bellard
        s->apms = val;
255 6515b203 bellard
    }
256 6515b203 bellard
}
257 6515b203 bellard
258 ab1e34ad bellard
static uint32_t pm_smi_readb(void *opaque, uint32_t addr)
259 ab1e34ad bellard
{
260 ab1e34ad bellard
    PIIX4PMState *s = opaque;
261 ab1e34ad bellard
    uint32_t val;
262 3b46e624 ths
263 ab1e34ad bellard
    addr &= 1;
264 ab1e34ad bellard
    if (addr == 0) {
265 ab1e34ad bellard
        val = s->apmc;
266 ab1e34ad bellard
    } else {
267 ab1e34ad bellard
        val = s->apms;
268 ab1e34ad bellard
    }
269 ab1e34ad bellard
#ifdef DEBUG
270 ab1e34ad bellard
    printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr, val);
271 ab1e34ad bellard
#endif
272 ab1e34ad bellard
    return val;
273 ab1e34ad bellard
}
274 ab1e34ad bellard
275 6515b203 bellard
static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
276 6515b203 bellard
{
277 6515b203 bellard
#if defined(DEBUG)
278 6515b203 bellard
    printf("ACPI: DBG: 0x%08x\n", val);
279 6515b203 bellard
#endif
280 6515b203 bellard
}
281 6515b203 bellard
282 3fffc223 ths
static void smb_transaction(PIIX4PMState *s)
283 3fffc223 ths
{
284 3fffc223 ths
    uint8_t prot = (s->smb_ctl >> 2) & 0x07;
285 3fffc223 ths
    uint8_t read = s->smb_addr & 0x01;
286 3fffc223 ths
    uint8_t cmd = s->smb_cmd;
287 3fffc223 ths
    uint8_t addr = s->smb_addr >> 1;
288 0ff596d0 pbrook
    i2c_bus *bus = s->smbus;
289 3fffc223 ths
290 3fffc223 ths
#ifdef DEBUG
291 3fffc223 ths
    printf("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
292 3fffc223 ths
#endif
293 3fffc223 ths
    switch(prot) {
294 3fffc223 ths
    case 0x0:
295 0ff596d0 pbrook
        smbus_quick_command(bus, addr, read);
296 3fffc223 ths
        break;
297 3fffc223 ths
    case 0x1:
298 3fffc223 ths
        if (read) {
299 0ff596d0 pbrook
            s->smb_data0 = smbus_receive_byte(bus, addr);
300 0ff596d0 pbrook
        } else {
301 0ff596d0 pbrook
            smbus_send_byte(bus, addr, cmd);
302 3fffc223 ths
        }
303 3fffc223 ths
        break;
304 3fffc223 ths
    case 0x2:
305 3fffc223 ths
        if (read) {
306 0ff596d0 pbrook
            s->smb_data0 = smbus_read_byte(bus, addr, cmd);
307 0ff596d0 pbrook
        } else {
308 0ff596d0 pbrook
            smbus_write_byte(bus, addr, cmd, s->smb_data0);
309 3fffc223 ths
        }
310 3fffc223 ths
        break;
311 3fffc223 ths
    case 0x3:
312 3fffc223 ths
        if (read) {
313 3fffc223 ths
            uint16_t val;
314 0ff596d0 pbrook
            val = smbus_read_word(bus, addr, cmd);
315 3fffc223 ths
            s->smb_data0 = val;
316 3fffc223 ths
            s->smb_data1 = val >> 8;
317 0ff596d0 pbrook
        } else {
318 0ff596d0 pbrook
            smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
319 3fffc223 ths
        }
320 3fffc223 ths
        break;
321 3fffc223 ths
    case 0x5:
322 3fffc223 ths
        if (read) {
323 0ff596d0 pbrook
            s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
324 0ff596d0 pbrook
        } else {
325 0ff596d0 pbrook
            smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
326 3fffc223 ths
        }
327 3fffc223 ths
        break;
328 3fffc223 ths
    default:
329 3fffc223 ths
        goto error;
330 3fffc223 ths
    }
331 3fffc223 ths
    return;
332 3fffc223 ths
333 3fffc223 ths
  error:
334 3fffc223 ths
    s->smb_stat |= 0x04;
335 3fffc223 ths
}
336 3fffc223 ths
337 3fffc223 ths
static void smb_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
338 3fffc223 ths
{
339 3fffc223 ths
    PIIX4PMState *s = opaque;
340 3fffc223 ths
    addr &= 0x3f;
341 3fffc223 ths
#ifdef DEBUG
342 3fffc223 ths
    printf("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
343 3fffc223 ths
#endif
344 3fffc223 ths
    switch(addr) {
345 3fffc223 ths
    case SMBHSTSTS:
346 3fffc223 ths
        s->smb_stat = 0;
347 3fffc223 ths
        s->smb_index = 0;
348 3fffc223 ths
        break;
349 3fffc223 ths
    case SMBHSTCNT:
350 3fffc223 ths
        s->smb_ctl = val;
351 3fffc223 ths
        if (val & 0x40)
352 3fffc223 ths
            smb_transaction(s);
353 3fffc223 ths
        break;
354 3fffc223 ths
    case SMBHSTCMD:
355 3fffc223 ths
        s->smb_cmd = val;
356 3fffc223 ths
        break;
357 3fffc223 ths
    case SMBHSTADD:
358 3fffc223 ths
        s->smb_addr = val;
359 3fffc223 ths
        break;
360 3fffc223 ths
    case SMBHSTDAT0:
361 3fffc223 ths
        s->smb_data0 = val;
362 3fffc223 ths
        break;
363 3fffc223 ths
    case SMBHSTDAT1:
364 3fffc223 ths
        s->smb_data1 = val;
365 3fffc223 ths
        break;
366 3fffc223 ths
    case SMBBLKDAT:
367 3fffc223 ths
        s->smb_data[s->smb_index++] = val;
368 3fffc223 ths
        if (s->smb_index > 31)
369 3fffc223 ths
            s->smb_index = 0;
370 3fffc223 ths
        break;
371 3fffc223 ths
    default:
372 3fffc223 ths
        break;
373 3fffc223 ths
    }
374 3fffc223 ths
}
375 3fffc223 ths
376 3fffc223 ths
static uint32_t smb_ioport_readb(void *opaque, uint32_t addr)
377 3fffc223 ths
{
378 3fffc223 ths
    PIIX4PMState *s = opaque;
379 3fffc223 ths
    uint32_t val;
380 3fffc223 ths
381 3fffc223 ths
    addr &= 0x3f;
382 3fffc223 ths
    switch(addr) {
383 3fffc223 ths
    case SMBHSTSTS:
384 3fffc223 ths
        val = s->smb_stat;
385 3fffc223 ths
        break;
386 3fffc223 ths
    case SMBHSTCNT:
387 3fffc223 ths
        s->smb_index = 0;
388 3fffc223 ths
        val = s->smb_ctl & 0x1f;
389 3fffc223 ths
        break;
390 3fffc223 ths
    case SMBHSTCMD:
391 3fffc223 ths
        val = s->smb_cmd;
392 3fffc223 ths
        break;
393 3fffc223 ths
    case SMBHSTADD:
394 3fffc223 ths
        val = s->smb_addr;
395 3fffc223 ths
        break;
396 3fffc223 ths
    case SMBHSTDAT0:
397 3fffc223 ths
        val = s->smb_data0;
398 3fffc223 ths
        break;
399 3fffc223 ths
    case SMBHSTDAT1:
400 3fffc223 ths
        val = s->smb_data1;
401 3fffc223 ths
        break;
402 3fffc223 ths
    case SMBBLKDAT:
403 3fffc223 ths
        val = s->smb_data[s->smb_index++];
404 3fffc223 ths
        if (s->smb_index > 31)
405 3fffc223 ths
            s->smb_index = 0;
406 3fffc223 ths
        break;
407 3fffc223 ths
    default:
408 3fffc223 ths
        val = 0;
409 3fffc223 ths
        break;
410 3fffc223 ths
    }
411 3fffc223 ths
#ifdef DEBUG
412 3fffc223 ths
    printf("SMB readb port=0x%04x val=0x%02x\n", addr, val);
413 3fffc223 ths
#endif
414 3fffc223 ths
    return val;
415 3fffc223 ths
}
416 3fffc223 ths
417 ab1e34ad bellard
static void pm_io_space_update(PIIX4PMState *s)
418 ab1e34ad bellard
{
419 ab1e34ad bellard
    uint32_t pm_io_base;
420 ab1e34ad bellard
421 ab1e34ad bellard
    if (s->dev.config[0x80] & 1) {
422 ab1e34ad bellard
        pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
423 bf367b54 ths
        pm_io_base &= 0xffc0;
424 ab1e34ad bellard
425 ab1e34ad bellard
        /* XXX: need to improve memory and ioport allocation */
426 ab1e34ad bellard
#if defined(DEBUG)
427 ab1e34ad bellard
        printf("PM: mapping to 0x%x\n", pm_io_base);
428 ab1e34ad bellard
#endif
429 ab1e34ad bellard
        register_ioport_write(pm_io_base, 64, 2, pm_ioport_writew, s);
430 ab1e34ad bellard
        register_ioport_read(pm_io_base, 64, 2, pm_ioport_readw, s);
431 ab1e34ad bellard
        register_ioport_write(pm_io_base, 64, 4, pm_ioport_writel, s);
432 ab1e34ad bellard
        register_ioport_read(pm_io_base, 64, 4, pm_ioport_readl, s);
433 ab1e34ad bellard
    }
434 ab1e34ad bellard
}
435 ab1e34ad bellard
436 5fafdf24 ths
static void pm_write_config(PCIDevice *d,
437 ab1e34ad bellard
                            uint32_t address, uint32_t val, int len)
438 ab1e34ad bellard
{
439 ab1e34ad bellard
    pci_default_write_config(d, address, val, len);
440 ab1e34ad bellard
    if (address == 0x80)
441 ab1e34ad bellard
        pm_io_space_update((PIIX4PMState *)d);
442 ab1e34ad bellard
}
443 ab1e34ad bellard
444 ab1e34ad bellard
static void pm_save(QEMUFile* f,void *opaque)
445 ab1e34ad bellard
{
446 ab1e34ad bellard
    PIIX4PMState *s = opaque;
447 ab1e34ad bellard
448 ab1e34ad bellard
    pci_device_save(&s->dev, f);
449 ab1e34ad bellard
450 ab1e34ad bellard
    qemu_put_be16s(f, &s->pmsts);
451 ab1e34ad bellard
    qemu_put_be16s(f, &s->pmen);
452 ab1e34ad bellard
    qemu_put_be16s(f, &s->pmcntrl);
453 ab1e34ad bellard
    qemu_put_8s(f, &s->apmc);
454 ab1e34ad bellard
    qemu_put_8s(f, &s->apms);
455 ab1e34ad bellard
    qemu_put_timer(f, s->tmr_timer);
456 bee8d684 ths
    qemu_put_be64(f, s->tmr_overflow_time);
457 ab1e34ad bellard
}
458 ab1e34ad bellard
459 ab1e34ad bellard
static int pm_load(QEMUFile* f,void* opaque,int version_id)
460 ab1e34ad bellard
{
461 ab1e34ad bellard
    PIIX4PMState *s = opaque;
462 ab1e34ad bellard
    int ret;
463 ab1e34ad bellard
464 ab1e34ad bellard
    if (version_id > 1)
465 ab1e34ad bellard
        return -EINVAL;
466 ab1e34ad bellard
467 ab1e34ad bellard
    ret = pci_device_load(&s->dev, f);
468 ab1e34ad bellard
    if (ret < 0)
469 ab1e34ad bellard
        return ret;
470 ab1e34ad bellard
471 ab1e34ad bellard
    qemu_get_be16s(f, &s->pmsts);
472 ab1e34ad bellard
    qemu_get_be16s(f, &s->pmen);
473 ab1e34ad bellard
    qemu_get_be16s(f, &s->pmcntrl);
474 ab1e34ad bellard
    qemu_get_8s(f, &s->apmc);
475 ab1e34ad bellard
    qemu_get_8s(f, &s->apms);
476 ab1e34ad bellard
    qemu_get_timer(f, s->tmr_timer);
477 bee8d684 ths
    s->tmr_overflow_time=qemu_get_be64(f);
478 ab1e34ad bellard
479 ab1e34ad bellard
    pm_io_space_update(s);
480 ab1e34ad bellard
481 ab1e34ad bellard
    return 0;
482 ab1e34ad bellard
}
483 ab1e34ad bellard
484 0bacd130 aliguori
static void piix4_reset(void *opaque)
485 0bacd130 aliguori
{
486 0bacd130 aliguori
        PIIX4PMState *s = opaque;
487 0bacd130 aliguori
        uint8_t *pci_conf = s->dev.config;
488 0bacd130 aliguori
489 0bacd130 aliguori
        pci_conf[0x58] = 0;
490 0bacd130 aliguori
        pci_conf[0x59] = 0;
491 0bacd130 aliguori
        pci_conf[0x5a] = 0;
492 0bacd130 aliguori
        pci_conf[0x5b] = 0;
493 0bacd130 aliguori
}
494 0bacd130 aliguori
495 cf7a2fe2 aurel32
i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
496 cf7a2fe2 aurel32
                       qemu_irq sci_irq)
497 6515b203 bellard
{
498 6515b203 bellard
    PIIX4PMState *s;
499 6515b203 bellard
    uint8_t *pci_conf;
500 6515b203 bellard
501 6515b203 bellard
    s = (PIIX4PMState *)pci_register_device(bus,
502 6515b203 bellard
                                         "PM", sizeof(PIIX4PMState),
503 ab1e34ad bellard
                                         devfn, NULL, pm_write_config);
504 cf7a2fe2 aurel32
    pm_state = s;
505 6515b203 bellard
    pci_conf = s->dev.config;
506 deb54399 aliguori
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
507 deb54399 aliguori
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_3);
508 bf367b54 ths
    pci_conf[0x06] = 0x80;
509 bf367b54 ths
    pci_conf[0x07] = 0x02;
510 a78b03cb balrog
    pci_conf[0x08] = 0x03; // revision number
511 6515b203 bellard
    pci_conf[0x09] = 0x00;
512 173a543b blueswir1
    pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);
513 6515b203 bellard
    pci_conf[0x0e] = 0x00; // header_type
514 6515b203 bellard
    pci_conf[0x3d] = 0x01; // interrupt pin 1
515 3b46e624 ths
516 ab1e34ad bellard
    pci_conf[0x40] = 0x01; /* PM io base read only bit */
517 3b46e624 ths
518 ab1e34ad bellard
    register_ioport_write(0xb2, 2, 1, pm_smi_writeb, s);
519 ab1e34ad bellard
    register_ioport_read(0xb2, 2, 1, pm_smi_readb, s);
520 ab1e34ad bellard
521 6515b203 bellard
    register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
522 6515b203 bellard
523 7ba1e619 aliguori
    if (kvm_enabled()) {
524 7ba1e619 aliguori
        /* Mark SMM as already inited to prevent SMM from running.  KVM does not
525 7ba1e619 aliguori
         * support SMM mode. */
526 7ba1e619 aliguori
        pci_conf[0x5B] = 0x02;
527 7ba1e619 aliguori
    }
528 7ba1e619 aliguori
529 1ce549ab bellard
    /* XXX: which specification is used ? The i82731AB has different
530 1ce549ab bellard
       mappings */
531 1ce549ab bellard
    pci_conf[0x5f] = (parallel_hds[0] != NULL ? 0x80 : 0) | 0x10;
532 1ce549ab bellard
    pci_conf[0x63] = 0x60;
533 1ce549ab bellard
    pci_conf[0x67] = (serial_hds[0] != NULL ? 0x08 : 0) |
534 1ce549ab bellard
        (serial_hds[1] != NULL ? 0x90 : 0);
535 1ce549ab bellard
536 3fffc223 ths
    pci_conf[0x90] = smb_io_base | 1;
537 3fffc223 ths
    pci_conf[0x91] = smb_io_base >> 8;
538 3fffc223 ths
    pci_conf[0xd2] = 0x09;
539 3fffc223 ths
    register_ioport_write(smb_io_base, 64, 1, smb_ioport_writeb, s);
540 3fffc223 ths
    register_ioport_read(smb_io_base, 64, 1, smb_ioport_readb, s);
541 3fffc223 ths
542 6515b203 bellard
    s->tmr_timer = qemu_new_timer(vm_clock, pm_tmr_timer, s);
543 6515b203 bellard
544 ab1e34ad bellard
    register_savevm("piix4_pm", 0, 1, pm_save, pm_load, s);
545 3fffc223 ths
546 0ff596d0 pbrook
    s->smbus = i2c_init_bus();
547 cf7a2fe2 aurel32
    s->irq = sci_irq;
548 0bacd130 aliguori
    qemu_register_reset(piix4_reset, s);
549 0bacd130 aliguori
550 0ff596d0 pbrook
    return s->smbus;
551 6515b203 bellard
}
552 cf7a2fe2 aurel32
553 cf7a2fe2 aurel32
#if defined(TARGET_I386)
554 cf7a2fe2 aurel32
void qemu_system_powerdown(void)
555 cf7a2fe2 aurel32
{
556 9669d3c5 aurel32
    if (!pm_state) {
557 9669d3c5 aurel32
        qemu_system_shutdown_request();
558 9669d3c5 aurel32
    } else if (pm_state->pmen & PWRBTN_EN) {
559 cf7a2fe2 aurel32
        pm_state->pmsts |= PWRBTN_EN;
560 cf7a2fe2 aurel32
        pm_update_sci(pm_state);
561 cf7a2fe2 aurel32
    }
562 cf7a2fe2 aurel32
}
563 cf7a2fe2 aurel32
#endif