]> git.proxmox.com Git - qemu.git/blame - hw/acpi.c
qxl: create slots on post_load in vga state
[qemu.git] / hw / acpi.c
CommitLineData
6515b203
FB
1/*
2 * ACPI implementation
5fafdf24 3 *
6515b203 4 * Copyright (c) 2006 Fabrice Bellard
5fafdf24 5 *
6515b203
FB
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2 as published by the Free Software Foundation.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
8167ee88 16 * License along with this library; if not, see <http://www.gnu.org/licenses/>
6515b203 17 */
04dc308f 18#include "sysemu.h"
87ecb68b
PB
19#include "hw.h"
20#include "pc.h"
990b150e 21#include "acpi.h"
6515b203 22
104bf02e
MT
23struct acpi_table_header {
24 uint16_t _length; /* our length, not actual part of the hdr */
25 /* XXX why we have 2 length fields here? */
26 char sig[4]; /* ACPI signature (4 ASCII characters) */
8a92ea2f
AL
27 uint32_t length; /* Length of table, in bytes, including header */
28 uint8_t revision; /* ACPI Specification minor version # */
29 uint8_t checksum; /* To make sum of entire table == 0 */
104bf02e
MT
30 char oem_id[6]; /* OEM identification */
31 char oem_table_id[8]; /* OEM table identification */
8a92ea2f 32 uint32_t oem_revision; /* OEM revision number */
104bf02e 33 char asl_compiler_id[4]; /* ASL compiler vendor ID */
8a92ea2f 34 uint32_t asl_compiler_revision; /* ASL compiler revision number */
541dc0d4 35} QEMU_PACKED;
8a92ea2f 36
104bf02e
MT
37#define ACPI_TABLE_HDR_SIZE sizeof(struct acpi_table_header)
38#define ACPI_TABLE_PFX_SIZE sizeof(uint16_t) /* size of the extra prefix */
39
40static const char dfl_hdr[ACPI_TABLE_HDR_SIZE] =
41 "\0\0" /* fake _length (2) */
42 "QEMU\0\0\0\0\1\0" /* sig (4), len(4), revno (1), csum (1) */
43 "QEMUQEQEMUQEMU\1\0\0\0" /* OEM id (6), table (8), revno (4) */
44 "QEMU\1\0\0\0" /* ASL compiler ID (4), version (4) */
45 ;
46
8a92ea2f
AL
47char *acpi_tables;
48size_t acpi_tables_len;
49
50static int acpi_checksum(const uint8_t *data, int len)
51{
52 int sum, i;
53 sum = 0;
104bf02e 54 for (i = 0; i < len; i++) {
8a92ea2f 55 sum += data[i];
104bf02e 56 }
8a92ea2f
AL
57 return (-sum) & 0xff;
58}
59
104bf02e
MT
60/* like strncpy() but zero-fills the tail of destination */
61static void strzcpy(char *dst, const char *src, size_t size)
62{
63 size_t len = strlen(src);
64 if (len >= size) {
65 len = size;
66 } else {
67 memset(dst + len, 0, size - len);
68 }
69 memcpy(dst, src, len);
70}
71
72/* XXX fixme: this function uses obsolete argument parsing interface */
8a92ea2f
AL
73int acpi_table_add(const char *t)
74{
8a92ea2f 75 char buf[1024], *p, *f;
8a92ea2f 76 unsigned long val;
104bf02e
MT
77 size_t len, start, allen;
78 bool has_header;
79 int changed;
80 int r;
81 struct acpi_table_header hdr;
8a92ea2f 82
104bf02e
MT
83 r = 0;
84 r |= get_param_value(buf, sizeof(buf), "data", t) ? 1 : 0;
85 r |= get_param_value(buf, sizeof(buf), "file", t) ? 2 : 0;
86 switch (r) {
87 case 0:
88 buf[0] = '\0';
89 /* fallthrough for default behavior */
90 case 1:
91 has_header = false;
92 break;
93 case 2:
94 has_header = true;
95 break;
96 default:
97 fprintf(stderr, "acpitable: both data and file are specified\n");
98 return -1;
99 }
100
101 if (!acpi_tables) {
102 allen = sizeof(uint16_t);
7267c094 103 acpi_tables = g_malloc0(allen);
8a92ea2f 104 } else {
104bf02e
MT
105 allen = acpi_tables_len;
106 }
107
108 start = allen;
7267c094 109 acpi_tables = g_realloc(acpi_tables, start + ACPI_TABLE_HDR_SIZE);
104bf02e
MT
110 allen += has_header ? ACPI_TABLE_PFX_SIZE : ACPI_TABLE_HDR_SIZE;
111
112 /* now read in the data files, reallocating buffer as needed */
113
114 for (f = strtok(buf, ":"); f; f = strtok(NULL, ":")) {
115 int fd = open(f, O_RDONLY);
116
117 if (fd < 0) {
118 fprintf(stderr, "can't open file %s: %s\n", f, strerror(errno));
119 return -1;
120 }
121
122 for (;;) {
123 char data[8192];
124 r = read(fd, data, sizeof(data));
125 if (r == 0) {
126 break;
127 } else if (r > 0) {
7267c094 128 acpi_tables = g_realloc(acpi_tables, allen + r);
104bf02e
MT
129 memcpy(acpi_tables + allen, data, r);
130 allen += r;
131 } else if (errno != EINTR) {
132 fprintf(stderr, "can't read file %s: %s\n",
133 f, strerror(errno));
134 close(fd);
135 return -1;
136 }
137 }
138
139 close(fd);
140 }
141
142 /* now fill in the header fields */
143
144 f = acpi_tables + start; /* start of the table */
145 changed = 0;
146
147 /* copy the header to temp place to align the fields */
148 memcpy(&hdr, has_header ? f : dfl_hdr, ACPI_TABLE_HDR_SIZE);
149
150 /* length of the table minus our prefix */
151 len = allen - start - ACPI_TABLE_PFX_SIZE;
152
153 hdr._length = cpu_to_le16(len);
154
155 if (get_param_value(buf, sizeof(buf), "sig", t)) {
156 strzcpy(hdr.sig, buf, sizeof(hdr.sig));
157 ++changed;
158 }
159
160 /* length of the table including header, in bytes */
161 if (has_header) {
162 /* check if actual length is correct */
163 val = le32_to_cpu(hdr.length);
164 if (val != len) {
165 fprintf(stderr,
166 "warning: acpitable has wrong length,"
167 " header says %lu, actual size %zu bytes\n",
168 val, len);
169 ++changed;
170 }
8a92ea2f 171 }
104bf02e
MT
172 /* we may avoid putting length here if has_header is true */
173 hdr.length = cpu_to_le32(len);
174
8a92ea2f 175 if (get_param_value(buf, sizeof(buf), "rev", t)) {
104bf02e
MT
176 val = strtoul(buf, &p, 0);
177 if (val > 255 || *p) {
178 fprintf(stderr, "acpitable: \"rev=%s\" is invalid\n", buf);
179 return -1;
180 }
181 hdr.revision = (uint8_t)val;
182 ++changed;
8a92ea2f 183 }
8a92ea2f
AL
184
185 if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
104bf02e
MT
186 strzcpy(hdr.oem_id, buf, sizeof(hdr.oem_id));
187 ++changed;
8a92ea2f
AL
188 }
189
190 if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
104bf02e
MT
191 strzcpy(hdr.oem_table_id, buf, sizeof(hdr.oem_table_id));
192 ++changed;
8a92ea2f
AL
193 }
194
195 if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
104bf02e
MT
196 val = strtol(buf, &p, 0);
197 if (*p) {
198 fprintf(stderr, "acpitable: \"oem_rev=%s\" is invalid\n", buf);
199 return -1;
200 }
201 hdr.oem_revision = cpu_to_le32(val);
202 ++changed;
8a92ea2f 203 }
8a92ea2f
AL
204
205 if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
104bf02e
MT
206 strzcpy(hdr.asl_compiler_id, buf, sizeof(hdr.asl_compiler_id));
207 ++changed;
8a92ea2f
AL
208 }
209
210 if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
104bf02e
MT
211 val = strtol(buf, &p, 0);
212 if (*p) {
213 fprintf(stderr, "acpitable: \"%s=%s\" is invalid\n",
214 "asl_compiler_rev", buf);
215 return -1;
216 }
217 hdr.asl_compiler_revision = cpu_to_le32(val);
218 ++changed;
8a92ea2f
AL
219 }
220
104bf02e
MT
221 if (!has_header && !changed) {
222 fprintf(stderr, "warning: acpitable: no table headers are specified\n");
8a92ea2f
AL
223 }
224
8a92ea2f 225
104bf02e
MT
226 /* now calculate checksum of the table, complete with the header */
227 /* we may as well leave checksum intact if has_header is true */
228 /* alternatively there may be a way to set cksum to a given value */
229 hdr.checksum = 0; /* for checksum calculation */
8a92ea2f 230
104bf02e
MT
231 /* put header back */
232 memcpy(f, &hdr, sizeof(hdr));
233
234 if (changed || !has_header || 1) {
235 ((struct acpi_table_header *)f)->checksum =
236 acpi_checksum((uint8_t *)f + ACPI_TABLE_PFX_SIZE, len);
d729bb9a 237 }
8a92ea2f 238
8a92ea2f 239 /* increase number of tables */
104bf02e
MT
240 (*(uint16_t *)acpi_tables) =
241 cpu_to_le32(le32_to_cpu(*(uint16_t *)acpi_tables) + 1);
242
243 acpi_tables_len = allen;
8a92ea2f 244 return 0;
104bf02e 245
8a92ea2f 246}
a54d41a8 247
04dc308f
IY
248/* ACPI PM1a EVT */
249uint16_t acpi_pm1_evt_get_sts(ACPIPM1EVT *pm1, int64_t overflow_time)
250{
251 int64_t d = acpi_pm_tmr_get_clock();
252 if (d >= overflow_time) {
253 pm1->sts |= ACPI_BITMASK_TIMER_STATUS;
254 }
255 return pm1->sts;
256}
257
258void acpi_pm1_evt_write_sts(ACPIPM1EVT *pm1, ACPIPMTimer *tmr, uint16_t val)
259{
260 uint16_t pm1_sts = acpi_pm1_evt_get_sts(pm1, tmr->overflow_time);
261 if (pm1_sts & val & ACPI_BITMASK_TIMER_STATUS) {
262 /* if TMRSTS is reset, then compute the new overflow time */
263 acpi_pm_tmr_calc_overflow_time(tmr);
264 }
265 pm1->sts &= ~val;
266}
267
268void acpi_pm1_evt_power_down(ACPIPM1EVT *pm1, ACPIPMTimer *tmr)
269{
270 if (!pm1) {
271 qemu_system_shutdown_request();
272 } else if (pm1->en & ACPI_BITMASK_POWER_BUTTON_ENABLE) {
273 pm1->sts |= ACPI_BITMASK_POWER_BUTTON_STATUS;
274 tmr->update_sci(tmr);
275 }
276}
277
278void acpi_pm1_evt_reset(ACPIPM1EVT *pm1)
279{
280 pm1->sts = 0;
281 pm1->en = 0;
282}
283
a54d41a8
IY
284/* ACPI PM_TMR */
285void acpi_pm_tmr_update(ACPIPMTimer *tmr, bool enable)
286{
287 int64_t expire_time;
288
289 /* schedule a timer interruption if needed */
290 if (enable) {
291 expire_time = muldiv64(tmr->overflow_time, get_ticks_per_sec(),
292 PM_TIMER_FREQUENCY);
293 qemu_mod_timer(tmr->timer, expire_time);
294 } else {
295 qemu_del_timer(tmr->timer);
296 }
297}
298
299void acpi_pm_tmr_calc_overflow_time(ACPIPMTimer *tmr)
300{
301 int64_t d = acpi_pm_tmr_get_clock();
302 tmr->overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
303}
304
305uint32_t acpi_pm_tmr_get(ACPIPMTimer *tmr)
306{
307 uint32_t d = acpi_pm_tmr_get_clock();;
308 return d & 0xffffff;
309}
310
311static void acpi_pm_tmr_timer(void *opaque)
312{
313 ACPIPMTimer *tmr = opaque;
314 tmr->update_sci(tmr);
315}
316
317void acpi_pm_tmr_init(ACPIPMTimer *tmr, acpi_update_sci_fn update_sci)
318{
319 tmr->update_sci = update_sci;
320 tmr->timer = qemu_new_timer_ns(vm_clock, acpi_pm_tmr_timer, tmr);
321}
322
323void acpi_pm_tmr_reset(ACPIPMTimer *tmr)
324{
325 tmr->overflow_time = 0;
326 qemu_del_timer(tmr->timer);
327}
eaba51c5
IY
328
329/* ACPI PM1aCNT */
330void acpi_pm1_cnt_init(ACPIPM1CNT *pm1_cnt, qemu_irq cmos_s3)
331{
332 pm1_cnt->cmos_s3 = cmos_s3;
333}
334
335void acpi_pm1_cnt_write(ACPIPM1EVT *pm1a, ACPIPM1CNT *pm1_cnt, uint16_t val)
336{
337 pm1_cnt->cnt = val & ~(ACPI_BITMASK_SLEEP_ENABLE);
338
339 if (val & ACPI_BITMASK_SLEEP_ENABLE) {
340 /* change suspend type */
341 uint16_t sus_typ = (val >> 10) & 7;
342 switch(sus_typ) {
343 case 0: /* soft power off */
344 qemu_system_shutdown_request();
345 break;
346 case 1:
347 /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
348 Pretend that resume was caused by power button */
349 pm1a->sts |=
350 (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_POWER_BUTTON_STATUS);
351 qemu_system_reset_request();
352 qemu_irq_raise(pm1_cnt->cmos_s3);
353 default:
354 break;
355 }
356 }
357}
358
359void acpi_pm1_cnt_update(ACPIPM1CNT *pm1_cnt,
360 bool sci_enable, bool sci_disable)
361{
362 /* ACPI specs 3.0, 4.7.2.5 */
363 if (sci_enable) {
364 pm1_cnt->cnt |= ACPI_BITMASK_SCI_ENABLE;
365 } else if (sci_disable) {
366 pm1_cnt->cnt &= ~ACPI_BITMASK_SCI_ENABLE;
367 }
368}
369
370void acpi_pm1_cnt_reset(ACPIPM1CNT *pm1_cnt)
371{
372 pm1_cnt->cnt = 0;
373 if (pm1_cnt->cmos_s3) {
374 qemu_irq_lower(pm1_cnt->cmos_s3);
375 }
376}
23910d3f
IY
377
378/* ACPI GPE */
379void acpi_gpe_init(ACPIGPE *gpe, uint8_t len)
380{
381 gpe->len = len;
7267c094
AL
382 gpe->sts = g_malloc0(len / 2);
383 gpe->en = g_malloc0(len / 2);
23910d3f
IY
384}
385
386void acpi_gpe_blk(ACPIGPE *gpe, uint32_t blk)
387{
388 gpe->blk = blk;
389}
390
391void acpi_gpe_reset(ACPIGPE *gpe)
392{
393 memset(gpe->sts, 0, gpe->len / 2);
394 memset(gpe->en, 0, gpe->len / 2);
395}
396
397static uint8_t *acpi_gpe_ioport_get_ptr(ACPIGPE *gpe, uint32_t addr)
398{
399 uint8_t *cur = NULL;
400
401 if (addr < gpe->len / 2) {
402 cur = gpe->sts + addr;
403 } else if (addr < gpe->len) {
54f8e61d 404 cur = gpe->en + addr - gpe->len / 2;
23910d3f
IY
405 } else {
406 abort();
407 }
408
409 return cur;
410}
411
412void acpi_gpe_ioport_writeb(ACPIGPE *gpe, uint32_t addr, uint32_t val)
413{
414 uint8_t *cur;
415
416 addr -= gpe->blk;
417 cur = acpi_gpe_ioport_get_ptr(gpe, addr);
418 if (addr < gpe->len / 2) {
419 /* GPE_STS */
420 *cur = (*cur) & ~val;
421 } else if (addr < gpe->len) {
422 /* GPE_EN */
423 *cur = val;
424 } else {
425 abort();
426 }
427}
428
429uint32_t acpi_gpe_ioport_readb(ACPIGPE *gpe, uint32_t addr)
430{
431 uint8_t *cur;
432 uint32_t val;
433
434 addr -= gpe->blk;
435 cur = acpi_gpe_ioport_get_ptr(gpe, addr);
436 val = 0;
437 if (cur != NULL) {
438 val = *cur;
439 }
440
441 return val;
442}