acpi: don't pass overflow_time to acpi_pm1_evt_get_sts
[qemu/wangdongxu.git] / hw / acpi.c
blob1129335599d538fbc259a628f5d2a07f8064180b
1 /*
2 * ACPI implementation
4 * Copyright (c) 2006 Fabrice Bellard
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.
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.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>
18 * Contributions after 2012-01-13 are licensed under the terms of the
19 * GNU GPL, version 2 or (at your option) any later version.
21 #include "sysemu.h"
22 #include "hw.h"
23 #include "pc.h"
24 #include "acpi.h"
26 struct acpi_table_header {
27 uint16_t _length; /* our length, not actual part of the hdr */
28 /* XXX why we have 2 length fields here? */
29 char sig[4]; /* ACPI signature (4 ASCII characters) */
30 uint32_t length; /* Length of table, in bytes, including header */
31 uint8_t revision; /* ACPI Specification minor version # */
32 uint8_t checksum; /* To make sum of entire table == 0 */
33 char oem_id[6]; /* OEM identification */
34 char oem_table_id[8]; /* OEM table identification */
35 uint32_t oem_revision; /* OEM revision number */
36 char asl_compiler_id[4]; /* ASL compiler vendor ID */
37 uint32_t asl_compiler_revision; /* ASL compiler revision number */
38 } QEMU_PACKED;
40 #define ACPI_TABLE_HDR_SIZE sizeof(struct acpi_table_header)
41 #define ACPI_TABLE_PFX_SIZE sizeof(uint16_t) /* size of the extra prefix */
43 static const char dfl_hdr[ACPI_TABLE_HDR_SIZE] =
44 "\0\0" /* fake _length (2) */
45 "QEMU\0\0\0\0\1\0" /* sig (4), len(4), revno (1), csum (1) */
46 "QEMUQEQEMUQEMU\1\0\0\0" /* OEM id (6), table (8), revno (4) */
47 "QEMU\1\0\0\0" /* ASL compiler ID (4), version (4) */
50 char *acpi_tables;
51 size_t acpi_tables_len;
53 static int acpi_checksum(const uint8_t *data, int len)
55 int sum, i;
56 sum = 0;
57 for (i = 0; i < len; i++) {
58 sum += data[i];
60 return (-sum) & 0xff;
63 /* like strncpy() but zero-fills the tail of destination */
64 static void strzcpy(char *dst, const char *src, size_t size)
66 size_t len = strlen(src);
67 if (len >= size) {
68 len = size;
69 } else {
70 memset(dst + len, 0, size - len);
72 memcpy(dst, src, len);
75 /* XXX fixme: this function uses obsolete argument parsing interface */
76 int acpi_table_add(const char *t)
78 char buf[1024], *p, *f;
79 unsigned long val;
80 size_t len, start, allen;
81 bool has_header;
82 int changed;
83 int r;
84 struct acpi_table_header hdr;
86 r = 0;
87 r |= get_param_value(buf, sizeof(buf), "data", t) ? 1 : 0;
88 r |= get_param_value(buf, sizeof(buf), "file", t) ? 2 : 0;
89 switch (r) {
90 case 0:
91 buf[0] = '\0';
92 /* fallthrough for default behavior */
93 case 1:
94 has_header = false;
95 break;
96 case 2:
97 has_header = true;
98 break;
99 default:
100 fprintf(stderr, "acpitable: both data and file are specified\n");
101 return -1;
104 if (!acpi_tables) {
105 allen = sizeof(uint16_t);
106 acpi_tables = g_malloc0(allen);
107 } else {
108 allen = acpi_tables_len;
111 start = allen;
112 acpi_tables = g_realloc(acpi_tables, start + ACPI_TABLE_HDR_SIZE);
113 allen += has_header ? ACPI_TABLE_PFX_SIZE : ACPI_TABLE_HDR_SIZE;
115 /* now read in the data files, reallocating buffer as needed */
117 for (f = strtok(buf, ":"); f; f = strtok(NULL, ":")) {
118 int fd = open(f, O_RDONLY);
120 if (fd < 0) {
121 fprintf(stderr, "can't open file %s: %s\n", f, strerror(errno));
122 return -1;
125 for (;;) {
126 char data[8192];
127 r = read(fd, data, sizeof(data));
128 if (r == 0) {
129 break;
130 } else if (r > 0) {
131 acpi_tables = g_realloc(acpi_tables, allen + r);
132 memcpy(acpi_tables + allen, data, r);
133 allen += r;
134 } else if (errno != EINTR) {
135 fprintf(stderr, "can't read file %s: %s\n",
136 f, strerror(errno));
137 close(fd);
138 return -1;
142 close(fd);
145 /* now fill in the header fields */
147 f = acpi_tables + start; /* start of the table */
148 changed = 0;
150 /* copy the header to temp place to align the fields */
151 memcpy(&hdr, has_header ? f : dfl_hdr, ACPI_TABLE_HDR_SIZE);
153 /* length of the table minus our prefix */
154 len = allen - start - ACPI_TABLE_PFX_SIZE;
156 hdr._length = cpu_to_le16(len);
158 if (get_param_value(buf, sizeof(buf), "sig", t)) {
159 strzcpy(hdr.sig, buf, sizeof(hdr.sig));
160 ++changed;
163 /* length of the table including header, in bytes */
164 if (has_header) {
165 /* check if actual length is correct */
166 val = le32_to_cpu(hdr.length);
167 if (val != len) {
168 fprintf(stderr,
169 "warning: acpitable has wrong length,"
170 " header says %lu, actual size %zu bytes\n",
171 val, len);
172 ++changed;
175 /* we may avoid putting length here if has_header is true */
176 hdr.length = cpu_to_le32(len);
178 if (get_param_value(buf, sizeof(buf), "rev", t)) {
179 val = strtoul(buf, &p, 0);
180 if (val > 255 || *p) {
181 fprintf(stderr, "acpitable: \"rev=%s\" is invalid\n", buf);
182 return -1;
184 hdr.revision = (uint8_t)val;
185 ++changed;
188 if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
189 strzcpy(hdr.oem_id, buf, sizeof(hdr.oem_id));
190 ++changed;
193 if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
194 strzcpy(hdr.oem_table_id, buf, sizeof(hdr.oem_table_id));
195 ++changed;
198 if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
199 val = strtol(buf, &p, 0);
200 if (*p) {
201 fprintf(stderr, "acpitable: \"oem_rev=%s\" is invalid\n", buf);
202 return -1;
204 hdr.oem_revision = cpu_to_le32(val);
205 ++changed;
208 if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
209 strzcpy(hdr.asl_compiler_id, buf, sizeof(hdr.asl_compiler_id));
210 ++changed;
213 if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
214 val = strtol(buf, &p, 0);
215 if (*p) {
216 fprintf(stderr, "acpitable: \"%s=%s\" is invalid\n",
217 "asl_compiler_rev", buf);
218 return -1;
220 hdr.asl_compiler_revision = cpu_to_le32(val);
221 ++changed;
224 if (!has_header && !changed) {
225 fprintf(stderr, "warning: acpitable: no table headers are specified\n");
229 /* now calculate checksum of the table, complete with the header */
230 /* we may as well leave checksum intact if has_header is true */
231 /* alternatively there may be a way to set cksum to a given value */
232 hdr.checksum = 0; /* for checksum calculation */
234 /* put header back */
235 memcpy(f, &hdr, sizeof(hdr));
237 if (changed || !has_header || 1) {
238 ((struct acpi_table_header *)f)->checksum =
239 acpi_checksum((uint8_t *)f + ACPI_TABLE_PFX_SIZE, len);
242 /* increase number of tables */
243 (*(uint16_t *)acpi_tables) =
244 cpu_to_le32(le32_to_cpu(*(uint16_t *)acpi_tables) + 1);
246 acpi_tables_len = allen;
247 return 0;
251 /* ACPI PM1a EVT */
252 uint16_t acpi_pm1_evt_get_sts(ACPIREGS *ar)
254 int64_t d = acpi_pm_tmr_get_clock();
255 if (d >= ar->tmr.overflow_time) {
256 ar->pm1.evt.sts |= ACPI_BITMASK_TIMER_STATUS;
258 return ar->pm1.evt.sts;
261 void acpi_pm1_evt_write_sts(ACPIREGS *ar, uint16_t val)
263 uint16_t pm1_sts = acpi_pm1_evt_get_sts(ar);
264 if (pm1_sts & val & ACPI_BITMASK_TIMER_STATUS) {
265 /* if TMRSTS is reset, then compute the new overflow time */
266 acpi_pm_tmr_calc_overflow_time(ar);
268 ar->pm1.evt.sts &= ~val;
271 void acpi_pm1_evt_power_down(ACPIREGS *ar)
273 if (ar->pm1.evt.en & ACPI_BITMASK_POWER_BUTTON_ENABLE) {
274 ar->pm1.evt.sts |= ACPI_BITMASK_POWER_BUTTON_STATUS;
275 ar->tmr.update_sci(ar);
279 void acpi_pm1_evt_reset(ACPIREGS *ar)
281 ar->pm1.evt.sts = 0;
282 ar->pm1.evt.en = 0;
285 /* ACPI PM_TMR */
286 void acpi_pm_tmr_update(ACPIREGS *ar, bool enable)
288 int64_t expire_time;
290 /* schedule a timer interruption if needed */
291 if (enable) {
292 expire_time = muldiv64(ar->tmr.overflow_time, get_ticks_per_sec(),
293 PM_TIMER_FREQUENCY);
294 qemu_mod_timer(ar->tmr.timer, expire_time);
295 } else {
296 qemu_del_timer(ar->tmr.timer);
300 void acpi_pm_tmr_calc_overflow_time(ACPIREGS *ar)
302 int64_t d = acpi_pm_tmr_get_clock();
303 ar->tmr.overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
306 uint32_t acpi_pm_tmr_get(ACPIREGS *ar)
308 uint32_t d = acpi_pm_tmr_get_clock();
309 return d & 0xffffff;
312 static void acpi_pm_tmr_timer(void *opaque)
314 ACPIREGS *ar = opaque;
315 ar->tmr.update_sci(ar);
318 void acpi_pm_tmr_init(ACPIREGS *ar, acpi_update_sci_fn update_sci)
320 ar->tmr.update_sci = update_sci;
321 ar->tmr.timer = qemu_new_timer_ns(vm_clock, acpi_pm_tmr_timer, ar);
324 void acpi_pm_tmr_reset(ACPIREGS *ar)
326 ar->tmr.overflow_time = 0;
327 qemu_del_timer(ar->tmr.timer);
330 /* ACPI PM1aCNT */
331 void acpi_pm1_cnt_init(ACPIREGS *ar, qemu_irq cmos_s3)
333 ar->pm1.cnt.cmos_s3 = cmos_s3;
336 void acpi_pm1_cnt_write(ACPIREGS *ar, uint16_t val)
338 ar->pm1.cnt.cnt = val & ~(ACPI_BITMASK_SLEEP_ENABLE);
340 if (val & ACPI_BITMASK_SLEEP_ENABLE) {
341 /* change suspend type */
342 uint16_t sus_typ = (val >> 10) & 7;
343 switch(sus_typ) {
344 case 0: /* soft power off */
345 qemu_system_shutdown_request();
346 break;
347 case 1:
348 /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
349 Pretend that resume was caused by power button */
350 ar->pm1.evt.sts |=
351 (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_POWER_BUTTON_STATUS);
352 qemu_system_reset_request();
353 qemu_irq_raise(ar->pm1.cnt.cmos_s3);
354 default:
355 break;
360 void acpi_pm1_cnt_update(ACPIREGS *ar,
361 bool sci_enable, bool sci_disable)
363 /* ACPI specs 3.0, 4.7.2.5 */
364 if (sci_enable) {
365 ar->pm1.cnt.cnt |= ACPI_BITMASK_SCI_ENABLE;
366 } else if (sci_disable) {
367 ar->pm1.cnt.cnt &= ~ACPI_BITMASK_SCI_ENABLE;
371 void acpi_pm1_cnt_reset(ACPIREGS *ar)
373 ar->pm1.cnt.cnt = 0;
374 if (ar->pm1.cnt.cmos_s3) {
375 qemu_irq_lower(ar->pm1.cnt.cmos_s3);
379 /* ACPI GPE */
380 void acpi_gpe_init(ACPIREGS *ar, uint8_t len)
382 ar->gpe.len = len;
383 ar->gpe.sts = g_malloc0(len / 2);
384 ar->gpe.en = g_malloc0(len / 2);
387 void acpi_gpe_blk(ACPIREGS *ar, uint32_t blk)
389 ar->gpe.blk = blk;
392 void acpi_gpe_reset(ACPIREGS *ar)
394 memset(ar->gpe.sts, 0, ar->gpe.len / 2);
395 memset(ar->gpe.en, 0, ar->gpe.len / 2);
398 static uint8_t *acpi_gpe_ioport_get_ptr(ACPIREGS *ar, uint32_t addr)
400 uint8_t *cur = NULL;
402 if (addr < ar->gpe.len / 2) {
403 cur = ar->gpe.sts + addr;
404 } else if (addr < ar->gpe.len) {
405 cur = ar->gpe.en + addr - ar->gpe.len / 2;
406 } else {
407 abort();
410 return cur;
413 void acpi_gpe_ioport_writeb(ACPIREGS *ar, uint32_t addr, uint32_t val)
415 uint8_t *cur;
417 addr -= ar->gpe.blk;
418 cur = acpi_gpe_ioport_get_ptr(ar, addr);
419 if (addr < ar->gpe.len / 2) {
420 /* GPE_STS */
421 *cur = (*cur) & ~val;
422 } else if (addr < ar->gpe.len) {
423 /* GPE_EN */
424 *cur = val;
425 } else {
426 abort();
430 uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr)
432 uint8_t *cur;
433 uint32_t val;
435 addr -= ar->gpe.blk;
436 cur = acpi_gpe_ioport_get_ptr(ar, addr);
437 val = 0;
438 if (cur != NULL) {
439 val = *cur;
442 return val;