root / hw / acpi.c @ 42fc925e
History | View | Annotate | Download (8.7 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 | ab1e34ad | bellard | #define USE_SMM
|
23 | 6515b203 | bellard | |
24 | 6515b203 | bellard | /* i82731AB (PIIX4) compatible power management function */
|
25 | 6515b203 | bellard | #define PM_FREQ 3579545 |
26 | 6515b203 | bellard | |
27 | 6515b203 | bellard | #define ACPI_DBG_IO_ADDR 0xb044 |
28 | 6515b203 | bellard | |
29 | 6515b203 | bellard | typedef struct PIIX4PMState { |
30 | 6515b203 | bellard | PCIDevice dev; |
31 | 6515b203 | bellard | uint16_t pmsts; |
32 | 6515b203 | bellard | uint16_t pmen; |
33 | 6515b203 | bellard | uint16_t pmcntrl; |
34 | ab1e34ad | bellard | uint8_t apmc; |
35 | ab1e34ad | bellard | uint8_t apms; |
36 | 6515b203 | bellard | QEMUTimer *tmr_timer; |
37 | 6515b203 | bellard | int64_t tmr_overflow_time; |
38 | 6515b203 | bellard | } PIIX4PMState; |
39 | 6515b203 | bellard | |
40 | 6515b203 | bellard | #define RTC_EN (1 << 10) |
41 | 6515b203 | bellard | #define PWRBTN_EN (1 << 8) |
42 | 6515b203 | bellard | #define GBL_EN (1 << 5) |
43 | 6515b203 | bellard | #define TMROF_EN (1 << 0) |
44 | 6515b203 | bellard | |
45 | 6515b203 | bellard | #define SCI_EN (1 << 0) |
46 | 6515b203 | bellard | |
47 | 6515b203 | bellard | #define SUS_EN (1 << 13) |
48 | 6515b203 | bellard | |
49 | 6515b203 | bellard | static uint32_t get_pmtmr(PIIX4PMState *s)
|
50 | 6515b203 | bellard | { |
51 | 6515b203 | bellard | uint32_t d; |
52 | 6515b203 | bellard | d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec); |
53 | 6515b203 | bellard | return d & 0xffffff; |
54 | 6515b203 | bellard | } |
55 | 6515b203 | bellard | |
56 | 6515b203 | bellard | static int get_pmsts(PIIX4PMState *s) |
57 | 6515b203 | bellard | { |
58 | 6515b203 | bellard | int64_t d; |
59 | 6515b203 | bellard | int pmsts;
|
60 | 6515b203 | bellard | pmsts = s->pmsts; |
61 | 6515b203 | bellard | d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec); |
62 | 6515b203 | bellard | if (d >= s->tmr_overflow_time)
|
63 | 6515b203 | bellard | s->pmsts |= TMROF_EN; |
64 | 6515b203 | bellard | return pmsts;
|
65 | 6515b203 | bellard | } |
66 | 6515b203 | bellard | |
67 | 6515b203 | bellard | static void pm_update_sci(PIIX4PMState *s) |
68 | 6515b203 | bellard | { |
69 | 6515b203 | bellard | int sci_level, pmsts;
|
70 | 6515b203 | bellard | int64_t expire_time; |
71 | 6515b203 | bellard | |
72 | 6515b203 | bellard | pmsts = get_pmsts(s); |
73 | 6515b203 | bellard | sci_level = (((pmsts & s->pmen) & |
74 | 6515b203 | bellard | (RTC_EN | PWRBTN_EN | GBL_EN | TMROF_EN)) != 0);
|
75 | 6515b203 | bellard | pci_set_irq(&s->dev, 0, sci_level);
|
76 | 6515b203 | bellard | /* schedule a timer interruption if needed */
|
77 | 6515b203 | bellard | if ((s->pmen & TMROF_EN) && !(pmsts & TMROF_EN)) {
|
78 | 6515b203 | bellard | expire_time = muldiv64(s->tmr_overflow_time, ticks_per_sec, PM_FREQ); |
79 | 6515b203 | bellard | qemu_mod_timer(s->tmr_timer, expire_time); |
80 | 6515b203 | bellard | } else {
|
81 | 6515b203 | bellard | qemu_del_timer(s->tmr_timer); |
82 | 6515b203 | bellard | } |
83 | 6515b203 | bellard | } |
84 | 6515b203 | bellard | |
85 | 6515b203 | bellard | static void pm_tmr_timer(void *opaque) |
86 | 6515b203 | bellard | { |
87 | 6515b203 | bellard | PIIX4PMState *s = opaque; |
88 | 6515b203 | bellard | pm_update_sci(s); |
89 | 6515b203 | bellard | } |
90 | 6515b203 | bellard | |
91 | 6515b203 | bellard | static void pm_ioport_writew(void *opaque, uint32_t addr, uint32_t val) |
92 | 6515b203 | bellard | { |
93 | 6515b203 | bellard | PIIX4PMState *s = opaque; |
94 | 6515b203 | bellard | addr &= 0x3f;
|
95 | 6515b203 | bellard | switch(addr) {
|
96 | 6515b203 | bellard | case 0x00: |
97 | 6515b203 | bellard | { |
98 | 6515b203 | bellard | int64_t d; |
99 | 6515b203 | bellard | int pmsts;
|
100 | 6515b203 | bellard | pmsts = get_pmsts(s); |
101 | 6515b203 | bellard | if (pmsts & val & TMROF_EN) {
|
102 | 6515b203 | bellard | /* if TMRSTS is reset, then compute the new overflow time */
|
103 | 6515b203 | bellard | d = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec); |
104 | 6515b203 | bellard | s->tmr_overflow_time = (d + 0x800000LL) & ~0x7fffffLL; |
105 | 6515b203 | bellard | } |
106 | 6515b203 | bellard | s->pmsts &= ~val; |
107 | 6515b203 | bellard | pm_update_sci(s); |
108 | 6515b203 | bellard | } |
109 | 6515b203 | bellard | break;
|
110 | 6515b203 | bellard | case 0x02: |
111 | 6515b203 | bellard | s->pmen = val; |
112 | 6515b203 | bellard | pm_update_sci(s); |
113 | 6515b203 | bellard | break;
|
114 | 6515b203 | bellard | case 0x04: |
115 | 6515b203 | bellard | { |
116 | 6515b203 | bellard | int sus_typ;
|
117 | 6515b203 | bellard | s->pmcntrl = val & ~(SUS_EN); |
118 | 6515b203 | bellard | if (val & SUS_EN) {
|
119 | 6515b203 | bellard | /* change suspend type */
|
120 | 6515b203 | bellard | sus_typ = (val >> 10) & 3; |
121 | 6515b203 | bellard | switch(sus_typ) {
|
122 | 6515b203 | bellard | case 0: /* soft power off */ |
123 | 6515b203 | bellard | qemu_system_shutdown_request(); |
124 | 6515b203 | bellard | break;
|
125 | 6515b203 | bellard | default:
|
126 | 6515b203 | bellard | break;
|
127 | 6515b203 | bellard | } |
128 | 6515b203 | bellard | } |
129 | 6515b203 | bellard | } |
130 | 6515b203 | bellard | break;
|
131 | 6515b203 | bellard | default:
|
132 | 6515b203 | bellard | break;
|
133 | 6515b203 | bellard | } |
134 | 6515b203 | bellard | #ifdef DEBUG
|
135 | 6515b203 | bellard | printf("PM writew port=0x%04x val=0x%04x\n", addr, val);
|
136 | 6515b203 | bellard | #endif
|
137 | 6515b203 | bellard | } |
138 | 6515b203 | bellard | |
139 | 6515b203 | bellard | static uint32_t pm_ioport_readw(void *opaque, uint32_t addr) |
140 | 6515b203 | bellard | { |
141 | 6515b203 | bellard | PIIX4PMState *s = opaque; |
142 | 6515b203 | bellard | uint32_t val; |
143 | 6515b203 | bellard | |
144 | 6515b203 | bellard | addr &= 0x3f;
|
145 | 6515b203 | bellard | switch(addr) {
|
146 | 6515b203 | bellard | case 0x00: |
147 | 6515b203 | bellard | val = get_pmsts(s); |
148 | 6515b203 | bellard | break;
|
149 | 6515b203 | bellard | case 0x02: |
150 | 6515b203 | bellard | val = s->pmen; |
151 | 6515b203 | bellard | break;
|
152 | 6515b203 | bellard | case 0x04: |
153 | 6515b203 | bellard | val = s->pmcntrl; |
154 | 6515b203 | bellard | break;
|
155 | 6515b203 | bellard | default:
|
156 | 6515b203 | bellard | val = 0;
|
157 | 6515b203 | bellard | break;
|
158 | 6515b203 | bellard | } |
159 | 6515b203 | bellard | #ifdef DEBUG
|
160 | 6515b203 | bellard | printf("PM readw port=0x%04x val=0x%04x\n", addr, val);
|
161 | 6515b203 | bellard | #endif
|
162 | 6515b203 | bellard | return val;
|
163 | 6515b203 | bellard | } |
164 | 6515b203 | bellard | |
165 | 6515b203 | bellard | static void pm_ioport_writel(void *opaque, uint32_t addr, uint32_t val) |
166 | 6515b203 | bellard | { |
167 | 6515b203 | bellard | // PIIX4PMState *s = opaque;
|
168 | 6515b203 | bellard | addr &= 0x3f;
|
169 | 6515b203 | bellard | #ifdef DEBUG
|
170 | 6515b203 | bellard | printf("PM writel port=0x%04x val=0x%08x\n", addr, val);
|
171 | 6515b203 | bellard | #endif
|
172 | 6515b203 | bellard | } |
173 | 6515b203 | bellard | |
174 | 6515b203 | bellard | static uint32_t pm_ioport_readl(void *opaque, uint32_t addr) |
175 | 6515b203 | bellard | { |
176 | 6515b203 | bellard | PIIX4PMState *s = opaque; |
177 | 6515b203 | bellard | uint32_t val; |
178 | 6515b203 | bellard | |
179 | 6515b203 | bellard | addr &= 0x3f;
|
180 | 6515b203 | bellard | switch(addr) {
|
181 | 6515b203 | bellard | case 0x08: |
182 | 6515b203 | bellard | val = get_pmtmr(s); |
183 | 6515b203 | bellard | break;
|
184 | 6515b203 | bellard | default:
|
185 | 6515b203 | bellard | val = 0;
|
186 | 6515b203 | bellard | break;
|
187 | 6515b203 | bellard | } |
188 | 6515b203 | bellard | #ifdef DEBUG
|
189 | 6515b203 | bellard | printf("PM readl port=0x%04x val=0x%08x\n", addr, val);
|
190 | 6515b203 | bellard | #endif
|
191 | 6515b203 | bellard | return val;
|
192 | 6515b203 | bellard | } |
193 | 6515b203 | bellard | |
194 | ab1e34ad | bellard | static void pm_smi_writeb(void *opaque, uint32_t addr, uint32_t val) |
195 | 6515b203 | bellard | { |
196 | 6515b203 | bellard | PIIX4PMState *s = opaque; |
197 | ab1e34ad | bellard | addr &= 1;
|
198 | 6515b203 | bellard | #ifdef DEBUG
|
199 | ab1e34ad | bellard | printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr, val);
|
200 | 6515b203 | bellard | #endif
|
201 | ab1e34ad | bellard | if (addr == 0) { |
202 | ab1e34ad | bellard | s->apmc = val; |
203 | ab1e34ad | bellard | #ifdef USE_SMM
|
204 | ab1e34ad | bellard | cpu_interrupt(first_cpu, CPU_INTERRUPT_SMI); |
205 | ab1e34ad | bellard | #else
|
206 | ab1e34ad | bellard | /* emulation of what the SMM BIOS should do */
|
207 | ab1e34ad | bellard | switch(val) {
|
208 | ab1e34ad | bellard | case 0xf0: /* ACPI disable */ |
209 | ab1e34ad | bellard | s->pmcntrl &= ~SCI_EN; |
210 | ab1e34ad | bellard | break;
|
211 | ab1e34ad | bellard | case 0xf1: /* ACPI enable */ |
212 | ab1e34ad | bellard | s->pmcntrl |= SCI_EN; |
213 | ab1e34ad | bellard | break;
|
214 | ab1e34ad | bellard | } |
215 | ab1e34ad | bellard | #endif
|
216 | ab1e34ad | bellard | } else {
|
217 | ab1e34ad | bellard | s->apms = val; |
218 | 6515b203 | bellard | } |
219 | 6515b203 | bellard | } |
220 | 6515b203 | bellard | |
221 | ab1e34ad | bellard | static uint32_t pm_smi_readb(void *opaque, uint32_t addr) |
222 | ab1e34ad | bellard | { |
223 | ab1e34ad | bellard | PIIX4PMState *s = opaque; |
224 | ab1e34ad | bellard | uint32_t val; |
225 | ab1e34ad | bellard | |
226 | ab1e34ad | bellard | addr &= 1;
|
227 | ab1e34ad | bellard | if (addr == 0) { |
228 | ab1e34ad | bellard | val = s->apmc; |
229 | ab1e34ad | bellard | } else {
|
230 | ab1e34ad | bellard | val = s->apms; |
231 | ab1e34ad | bellard | } |
232 | ab1e34ad | bellard | #ifdef DEBUG
|
233 | ab1e34ad | bellard | printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr, val);
|
234 | ab1e34ad | bellard | #endif
|
235 | ab1e34ad | bellard | return val;
|
236 | ab1e34ad | bellard | } |
237 | ab1e34ad | bellard | |
238 | 6515b203 | bellard | static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val) |
239 | 6515b203 | bellard | { |
240 | 6515b203 | bellard | #if defined(DEBUG)
|
241 | 6515b203 | bellard | printf("ACPI: DBG: 0x%08x\n", val);
|
242 | 6515b203 | bellard | #endif
|
243 | 6515b203 | bellard | } |
244 | 6515b203 | bellard | |
245 | ab1e34ad | bellard | static void pm_io_space_update(PIIX4PMState *s) |
246 | ab1e34ad | bellard | { |
247 | ab1e34ad | bellard | uint32_t pm_io_base; |
248 | ab1e34ad | bellard | |
249 | ab1e34ad | bellard | if (s->dev.config[0x80] & 1) { |
250 | ab1e34ad | bellard | pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
|
251 | ab1e34ad | bellard | pm_io_base &= 0xfffe;
|
252 | ab1e34ad | bellard | |
253 | ab1e34ad | bellard | /* XXX: need to improve memory and ioport allocation */
|
254 | ab1e34ad | bellard | #if defined(DEBUG)
|
255 | ab1e34ad | bellard | printf("PM: mapping to 0x%x\n", pm_io_base);
|
256 | ab1e34ad | bellard | #endif
|
257 | ab1e34ad | bellard | register_ioport_write(pm_io_base, 64, 2, pm_ioport_writew, s); |
258 | ab1e34ad | bellard | register_ioport_read(pm_io_base, 64, 2, pm_ioport_readw, s); |
259 | ab1e34ad | bellard | register_ioport_write(pm_io_base, 64, 4, pm_ioport_writel, s); |
260 | ab1e34ad | bellard | register_ioport_read(pm_io_base, 64, 4, pm_ioport_readl, s); |
261 | ab1e34ad | bellard | } |
262 | ab1e34ad | bellard | } |
263 | ab1e34ad | bellard | |
264 | ab1e34ad | bellard | static void pm_write_config(PCIDevice *d, |
265 | ab1e34ad | bellard | uint32_t address, uint32_t val, int len)
|
266 | ab1e34ad | bellard | { |
267 | ab1e34ad | bellard | pci_default_write_config(d, address, val, len); |
268 | ab1e34ad | bellard | if (address == 0x80) |
269 | ab1e34ad | bellard | pm_io_space_update((PIIX4PMState *)d); |
270 | ab1e34ad | bellard | } |
271 | ab1e34ad | bellard | |
272 | ab1e34ad | bellard | static void pm_save(QEMUFile* f,void *opaque) |
273 | ab1e34ad | bellard | { |
274 | ab1e34ad | bellard | PIIX4PMState *s = opaque; |
275 | ab1e34ad | bellard | |
276 | ab1e34ad | bellard | pci_device_save(&s->dev, f); |
277 | ab1e34ad | bellard | |
278 | ab1e34ad | bellard | qemu_put_be16s(f, &s->pmsts); |
279 | ab1e34ad | bellard | qemu_put_be16s(f, &s->pmen); |
280 | ab1e34ad | bellard | qemu_put_be16s(f, &s->pmcntrl); |
281 | ab1e34ad | bellard | qemu_put_8s(f, &s->apmc); |
282 | ab1e34ad | bellard | qemu_put_8s(f, &s->apms); |
283 | ab1e34ad | bellard | qemu_put_timer(f, s->tmr_timer); |
284 | ab1e34ad | bellard | qemu_put_be64s(f, &s->tmr_overflow_time); |
285 | ab1e34ad | bellard | } |
286 | ab1e34ad | bellard | |
287 | ab1e34ad | bellard | static int pm_load(QEMUFile* f,void* opaque,int version_id) |
288 | ab1e34ad | bellard | { |
289 | ab1e34ad | bellard | PIIX4PMState *s = opaque; |
290 | ab1e34ad | bellard | int ret;
|
291 | ab1e34ad | bellard | |
292 | ab1e34ad | bellard | if (version_id > 1) |
293 | ab1e34ad | bellard | return -EINVAL;
|
294 | ab1e34ad | bellard | |
295 | ab1e34ad | bellard | ret = pci_device_load(&s->dev, f); |
296 | ab1e34ad | bellard | if (ret < 0) |
297 | ab1e34ad | bellard | return ret;
|
298 | ab1e34ad | bellard | |
299 | ab1e34ad | bellard | qemu_get_be16s(f, &s->pmsts); |
300 | ab1e34ad | bellard | qemu_get_be16s(f, &s->pmen); |
301 | ab1e34ad | bellard | qemu_get_be16s(f, &s->pmcntrl); |
302 | ab1e34ad | bellard | qemu_get_8s(f, &s->apmc); |
303 | ab1e34ad | bellard | qemu_get_8s(f, &s->apms); |
304 | ab1e34ad | bellard | qemu_get_timer(f, s->tmr_timer); |
305 | ab1e34ad | bellard | qemu_get_be64s(f, &s->tmr_overflow_time); |
306 | ab1e34ad | bellard | |
307 | ab1e34ad | bellard | pm_io_space_update(s); |
308 | ab1e34ad | bellard | |
309 | ab1e34ad | bellard | return 0; |
310 | ab1e34ad | bellard | } |
311 | ab1e34ad | bellard | |
312 | 502a5395 | pbrook | void piix4_pm_init(PCIBus *bus, int devfn) |
313 | 6515b203 | bellard | { |
314 | 6515b203 | bellard | PIIX4PMState *s; |
315 | 6515b203 | bellard | uint8_t *pci_conf; |
316 | 6515b203 | bellard | |
317 | 6515b203 | bellard | s = (PIIX4PMState *)pci_register_device(bus, |
318 | 6515b203 | bellard | "PM", sizeof(PIIX4PMState), |
319 | ab1e34ad | bellard | devfn, NULL, pm_write_config);
|
320 | 6515b203 | bellard | pci_conf = s->dev.config; |
321 | 6515b203 | bellard | pci_conf[0x00] = 0x86; |
322 | 6515b203 | bellard | pci_conf[0x01] = 0x80; |
323 | 6515b203 | bellard | pci_conf[0x02] = 0x13; |
324 | 7ef4da1c | bellard | pci_conf[0x03] = 0x71; |
325 | 6515b203 | bellard | pci_conf[0x08] = 0x00; // revision number |
326 | 6515b203 | bellard | pci_conf[0x09] = 0x00; |
327 | 6515b203 | bellard | pci_conf[0x0a] = 0x80; // other bridge device |
328 | 6515b203 | bellard | pci_conf[0x0b] = 0x06; // bridge device |
329 | 6515b203 | bellard | pci_conf[0x0e] = 0x00; // header_type |
330 | 6515b203 | bellard | pci_conf[0x3d] = 0x01; // interrupt pin 1 |
331 | 6515b203 | bellard | |
332 | ab1e34ad | bellard | pci_conf[0x40] = 0x01; /* PM io base read only bit */ |
333 | 6515b203 | bellard | |
334 | ab1e34ad | bellard | register_ioport_write(0xb2, 2, 1, pm_smi_writeb, s); |
335 | ab1e34ad | bellard | register_ioport_read(0xb2, 2, 1, pm_smi_readb, s); |
336 | ab1e34ad | bellard | |
337 | 6515b203 | bellard | register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s); |
338 | 6515b203 | bellard | |
339 | 1ce549ab | bellard | /* XXX: which specification is used ? The i82731AB has different
|
340 | 1ce549ab | bellard | mappings */
|
341 | 1ce549ab | bellard | pci_conf[0x5f] = (parallel_hds[0] != NULL ? 0x80 : 0) | 0x10; |
342 | 1ce549ab | bellard | pci_conf[0x63] = 0x60; |
343 | 1ce549ab | bellard | pci_conf[0x67] = (serial_hds[0] != NULL ? 0x08 : 0) | |
344 | 1ce549ab | bellard | (serial_hds[1] != NULL ? 0x90 : 0); |
345 | 1ce549ab | bellard | |
346 | 6515b203 | bellard | s->tmr_timer = qemu_new_timer(vm_clock, pm_tmr_timer, s); |
347 | 6515b203 | bellard | |
348 | ab1e34ad | bellard | register_savevm("piix4_pm", 0, 1, pm_save, pm_load, s); |
349 | 6515b203 | bellard | } |