]> git.proxmox.com Git - qemu.git/blame - hw/acpi.c
monitor: move include files to include/monitor/
[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/>
6b620ca3
PB
17 *
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.
6515b203 20 */
04dc308f 21#include "sysemu.h"
87ecb68b
PB
22#include "hw.h"
23#include "pc.h"
990b150e 24#include "acpi.h"
83c9089e 25#include "monitor/monitor.h"
6515b203 26
104bf02e
MT
27struct acpi_table_header {
28 uint16_t _length; /* our length, not actual part of the hdr */
29 /* XXX why we have 2 length fields here? */
30 char sig[4]; /* ACPI signature (4 ASCII characters) */
8a92ea2f
AL
31 uint32_t length; /* Length of table, in bytes, including header */
32 uint8_t revision; /* ACPI Specification minor version # */
33 uint8_t checksum; /* To make sum of entire table == 0 */
104bf02e
MT
34 char oem_id[6]; /* OEM identification */
35 char oem_table_id[8]; /* OEM table identification */
8a92ea2f 36 uint32_t oem_revision; /* OEM revision number */
104bf02e 37 char asl_compiler_id[4]; /* ASL compiler vendor ID */
8a92ea2f 38 uint32_t asl_compiler_revision; /* ASL compiler revision number */
541dc0d4 39} QEMU_PACKED;
8a92ea2f 40
104bf02e
MT
41#define ACPI_TABLE_HDR_SIZE sizeof(struct acpi_table_header)
42#define ACPI_TABLE_PFX_SIZE sizeof(uint16_t) /* size of the extra prefix */
43
44static const char dfl_hdr[ACPI_TABLE_HDR_SIZE] =
45 "\0\0" /* fake _length (2) */
46 "QEMU\0\0\0\0\1\0" /* sig (4), len(4), revno (1), csum (1) */
47 "QEMUQEQEMUQEMU\1\0\0\0" /* OEM id (6), table (8), revno (4) */
48 "QEMU\1\0\0\0" /* ASL compiler ID (4), version (4) */
49 ;
50
8a92ea2f
AL
51char *acpi_tables;
52size_t acpi_tables_len;
53
54static int acpi_checksum(const uint8_t *data, int len)
55{
56 int sum, i;
57 sum = 0;
104bf02e 58 for (i = 0; i < len; i++) {
8a92ea2f 59 sum += data[i];
104bf02e 60 }
8a92ea2f
AL
61 return (-sum) & 0xff;
62}
63
104bf02e 64/* XXX fixme: this function uses obsolete argument parsing interface */
8a92ea2f
AL
65int acpi_table_add(const char *t)
66{
8a92ea2f 67 char buf[1024], *p, *f;
8a92ea2f 68 unsigned long val;
104bf02e
MT
69 size_t len, start, allen;
70 bool has_header;
71 int changed;
72 int r;
73 struct acpi_table_header hdr;
8a92ea2f 74
104bf02e
MT
75 r = 0;
76 r |= get_param_value(buf, sizeof(buf), "data", t) ? 1 : 0;
77 r |= get_param_value(buf, sizeof(buf), "file", t) ? 2 : 0;
78 switch (r) {
79 case 0:
80 buf[0] = '\0';
81 /* fallthrough for default behavior */
82 case 1:
83 has_header = false;
84 break;
85 case 2:
86 has_header = true;
87 break;
88 default:
89 fprintf(stderr, "acpitable: both data and file are specified\n");
90 return -1;
91 }
92
93 if (!acpi_tables) {
94 allen = sizeof(uint16_t);
7267c094 95 acpi_tables = g_malloc0(allen);
8a92ea2f 96 } else {
104bf02e
MT
97 allen = acpi_tables_len;
98 }
99
100 start = allen;
7267c094 101 acpi_tables = g_realloc(acpi_tables, start + ACPI_TABLE_HDR_SIZE);
104bf02e
MT
102 allen += has_header ? ACPI_TABLE_PFX_SIZE : ACPI_TABLE_HDR_SIZE;
103
104 /* now read in the data files, reallocating buffer as needed */
105
106 for (f = strtok(buf, ":"); f; f = strtok(NULL, ":")) {
107 int fd = open(f, O_RDONLY);
108
109 if (fd < 0) {
110 fprintf(stderr, "can't open file %s: %s\n", f, strerror(errno));
111 return -1;
112 }
113
114 for (;;) {
115 char data[8192];
116 r = read(fd, data, sizeof(data));
117 if (r == 0) {
118 break;
119 } else if (r > 0) {
7267c094 120 acpi_tables = g_realloc(acpi_tables, allen + r);
104bf02e
MT
121 memcpy(acpi_tables + allen, data, r);
122 allen += r;
123 } else if (errno != EINTR) {
124 fprintf(stderr, "can't read file %s: %s\n",
125 f, strerror(errno));
126 close(fd);
127 return -1;
128 }
129 }
130
131 close(fd);
132 }
133
134 /* now fill in the header fields */
135
136 f = acpi_tables + start; /* start of the table */
137 changed = 0;
138
139 /* copy the header to temp place to align the fields */
140 memcpy(&hdr, has_header ? f : dfl_hdr, ACPI_TABLE_HDR_SIZE);
141
142 /* length of the table minus our prefix */
143 len = allen - start - ACPI_TABLE_PFX_SIZE;
144
145 hdr._length = cpu_to_le16(len);
146
147 if (get_param_value(buf, sizeof(buf), "sig", t)) {
3cda3462
JM
148 /* strncpy is justified: the field need not be NUL-terminated. */
149 strncpy(hdr.sig, buf, sizeof(hdr.sig));
104bf02e
MT
150 ++changed;
151 }
152
153 /* length of the table including header, in bytes */
154 if (has_header) {
155 /* check if actual length is correct */
156 val = le32_to_cpu(hdr.length);
157 if (val != len) {
158 fprintf(stderr,
159 "warning: acpitable has wrong length,"
160 " header says %lu, actual size %zu bytes\n",
161 val, len);
162 ++changed;
163 }
8a92ea2f 164 }
104bf02e
MT
165 /* we may avoid putting length here if has_header is true */
166 hdr.length = cpu_to_le32(len);
167
8a92ea2f 168 if (get_param_value(buf, sizeof(buf), "rev", t)) {
104bf02e
MT
169 val = strtoul(buf, &p, 0);
170 if (val > 255 || *p) {
171 fprintf(stderr, "acpitable: \"rev=%s\" is invalid\n", buf);
172 return -1;
173 }
174 hdr.revision = (uint8_t)val;
175 ++changed;
8a92ea2f 176 }
8a92ea2f
AL
177
178 if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
3cda3462
JM
179 /* strncpy is justified: the field need not be NUL-terminated. */
180 strncpy(hdr.oem_id, buf, sizeof(hdr.oem_id));
104bf02e 181 ++changed;
8a92ea2f
AL
182 }
183
184 if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
3cda3462
JM
185 /* strncpy is justified: the field need not be NUL-terminated. */
186 strncpy(hdr.oem_table_id, buf, sizeof(hdr.oem_table_id));
104bf02e 187 ++changed;
8a92ea2f
AL
188 }
189
190 if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
104bf02e
MT
191 val = strtol(buf, &p, 0);
192 if (*p) {
193 fprintf(stderr, "acpitable: \"oem_rev=%s\" is invalid\n", buf);
194 return -1;
195 }
196 hdr.oem_revision = cpu_to_le32(val);
197 ++changed;
8a92ea2f 198 }
8a92ea2f
AL
199
200 if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
3cda3462
JM
201 /* strncpy is justified: the field need not be NUL-terminated. */
202 strncpy(hdr.asl_compiler_id, buf, sizeof(hdr.asl_compiler_id));
104bf02e 203 ++changed;
8a92ea2f
AL
204 }
205
206 if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
104bf02e
MT
207 val = strtol(buf, &p, 0);
208 if (*p) {
209 fprintf(stderr, "acpitable: \"%s=%s\" is invalid\n",
210 "asl_compiler_rev", buf);
211 return -1;
212 }
213 hdr.asl_compiler_revision = cpu_to_le32(val);
214 ++changed;
8a92ea2f
AL
215 }
216
104bf02e
MT
217 if (!has_header && !changed) {
218 fprintf(stderr, "warning: acpitable: no table headers are specified\n");
8a92ea2f
AL
219 }
220
8a92ea2f 221
104bf02e
MT
222 /* now calculate checksum of the table, complete with the header */
223 /* we may as well leave checksum intact if has_header is true */
224 /* alternatively there may be a way to set cksum to a given value */
225 hdr.checksum = 0; /* for checksum calculation */
8a92ea2f 226
104bf02e
MT
227 /* put header back */
228 memcpy(f, &hdr, sizeof(hdr));
229
230 if (changed || !has_header || 1) {
231 ((struct acpi_table_header *)f)->checksum =
232 acpi_checksum((uint8_t *)f + ACPI_TABLE_PFX_SIZE, len);
d729bb9a 233 }
8a92ea2f 234
8a92ea2f 235 /* increase number of tables */
104bf02e
MT
236 (*(uint16_t *)acpi_tables) =
237 cpu_to_le32(le32_to_cpu(*(uint16_t *)acpi_tables) + 1);
238
239 acpi_tables_len = allen;
8a92ea2f 240 return 0;
104bf02e 241
8a92ea2f 242}
a54d41a8 243
da98c8eb
GH
244static void acpi_notify_wakeup(Notifier *notifier, void *data)
245{
246 ACPIREGS *ar = container_of(notifier, ACPIREGS, wakeup);
247 WakeupReason *reason = data;
248
249 switch (*reason) {
62aeb0f7
GH
250 case QEMU_WAKEUP_REASON_RTC:
251 ar->pm1.evt.sts |=
252 (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_RT_CLOCK_STATUS);
253 break;
6595abc0
GH
254 case QEMU_WAKEUP_REASON_PMTIMER:
255 ar->pm1.evt.sts |=
256 (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_TIMER_STATUS);
257 break;
da98c8eb
GH
258 case QEMU_WAKEUP_REASON_OTHER:
259 default:
260 /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
261 Pretend that resume was caused by power button */
262 ar->pm1.evt.sts |=
263 (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_POWER_BUTTON_STATUS);
264 break;
265 }
266}
267
04dc308f 268/* ACPI PM1a EVT */
2886be1b 269uint16_t acpi_pm1_evt_get_sts(ACPIREGS *ar)
04dc308f
IY
270{
271 int64_t d = acpi_pm_tmr_get_clock();
2886be1b 272 if (d >= ar->tmr.overflow_time) {
355bf2e5 273 ar->pm1.evt.sts |= ACPI_BITMASK_TIMER_STATUS;
04dc308f 274 }
355bf2e5 275 return ar->pm1.evt.sts;
04dc308f
IY
276}
277
b5a7c024 278static void acpi_pm1_evt_write_sts(ACPIREGS *ar, uint16_t val)
04dc308f 279{
2886be1b 280 uint16_t pm1_sts = acpi_pm1_evt_get_sts(ar);
04dc308f
IY
281 if (pm1_sts & val & ACPI_BITMASK_TIMER_STATUS) {
282 /* if TMRSTS is reset, then compute the new overflow time */
355bf2e5 283 acpi_pm_tmr_calc_overflow_time(ar);
04dc308f 284 }
355bf2e5 285 ar->pm1.evt.sts &= ~val;
04dc308f
IY
286}
287
b5a7c024 288static void acpi_pm1_evt_write_en(ACPIREGS *ar, uint16_t val)
8283c4f5
GH
289{
290 ar->pm1.evt.en = val;
62aeb0f7
GH
291 qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_RTC,
292 val & ACPI_BITMASK_RT_CLOCK_ENABLE);
6595abc0
GH
293 qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_PMTIMER,
294 val & ACPI_BITMASK_TIMER_ENABLE);
8283c4f5
GH
295}
296
355bf2e5 297void acpi_pm1_evt_power_down(ACPIREGS *ar)
04dc308f 298{
355bf2e5
GH
299 if (ar->pm1.evt.en & ACPI_BITMASK_POWER_BUTTON_ENABLE) {
300 ar->pm1.evt.sts |= ACPI_BITMASK_POWER_BUTTON_STATUS;
301 ar->tmr.update_sci(ar);
04dc308f
IY
302 }
303}
304
355bf2e5 305void acpi_pm1_evt_reset(ACPIREGS *ar)
04dc308f 306{
355bf2e5
GH
307 ar->pm1.evt.sts = 0;
308 ar->pm1.evt.en = 0;
62aeb0f7 309 qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_RTC, 0);
6595abc0 310 qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_PMTIMER, 0);
04dc308f
IY
311}
312
b5a7c024
GH
313static uint64_t acpi_pm_evt_read(void *opaque, hwaddr addr, unsigned width)
314{
315 ACPIREGS *ar = opaque;
316 switch (addr) {
317 case 0:
318 return acpi_pm1_evt_get_sts(ar);
319 case 2:
320 return ar->pm1.evt.en;
321 default:
322 return 0;
323 }
324}
325
326static void acpi_pm_evt_write(void *opaque, hwaddr addr, uint64_t val,
327 unsigned width)
328{
329 ACPIREGS *ar = opaque;
330 switch (addr) {
331 case 0:
332 acpi_pm1_evt_write_sts(ar, val);
333 ar->pm1.evt.update_sci(ar);
334 break;
335 case 2:
336 acpi_pm1_evt_write_en(ar, val);
337 ar->pm1.evt.update_sci(ar);
338 break;
339 }
340}
341
342static const MemoryRegionOps acpi_pm_evt_ops = {
343 .read = acpi_pm_evt_read,
344 .write = acpi_pm_evt_write,
345 .valid.min_access_size = 2,
346 .valid.max_access_size = 2,
347 .endianness = DEVICE_LITTLE_ENDIAN,
348};
349
350void acpi_pm1_evt_init(ACPIREGS *ar, acpi_update_sci_fn update_sci,
351 MemoryRegion *parent)
352{
353 ar->pm1.evt.update_sci = update_sci;
354 memory_region_init_io(&ar->pm1.evt.io, &acpi_pm_evt_ops, ar, "acpi-evt", 4);
355 memory_region_add_subregion(parent, 0, &ar->pm1.evt.io);
356}
357
a54d41a8 358/* ACPI PM_TMR */
355bf2e5 359void acpi_pm_tmr_update(ACPIREGS *ar, bool enable)
a54d41a8
IY
360{
361 int64_t expire_time;
362
363 /* schedule a timer interruption if needed */
364 if (enable) {
355bf2e5 365 expire_time = muldiv64(ar->tmr.overflow_time, get_ticks_per_sec(),
a54d41a8 366 PM_TIMER_FREQUENCY);
355bf2e5 367 qemu_mod_timer(ar->tmr.timer, expire_time);
a54d41a8 368 } else {
355bf2e5 369 qemu_del_timer(ar->tmr.timer);
a54d41a8
IY
370 }
371}
372
355bf2e5 373void acpi_pm_tmr_calc_overflow_time(ACPIREGS *ar)
a54d41a8
IY
374{
375 int64_t d = acpi_pm_tmr_get_clock();
355bf2e5 376 ar->tmr.overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
a54d41a8
IY
377}
378
77d58b1e 379static uint32_t acpi_pm_tmr_get(ACPIREGS *ar)
a54d41a8 380{
3a93113a 381 uint32_t d = acpi_pm_tmr_get_clock();
a54d41a8
IY
382 return d & 0xffffff;
383}
384
385static void acpi_pm_tmr_timer(void *opaque)
386{
355bf2e5 387 ACPIREGS *ar = opaque;
6595abc0 388 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_PMTIMER);
355bf2e5 389 ar->tmr.update_sci(ar);
a54d41a8
IY
390}
391
77d58b1e
GH
392static uint64_t acpi_pm_tmr_read(void *opaque, hwaddr addr, unsigned width)
393{
394 return acpi_pm_tmr_get(opaque);
395}
396
397static const MemoryRegionOps acpi_pm_tmr_ops = {
398 .read = acpi_pm_tmr_read,
399 .valid.min_access_size = 4,
400 .valid.max_access_size = 4,
401 .endianness = DEVICE_LITTLE_ENDIAN,
402};
403
404void acpi_pm_tmr_init(ACPIREGS *ar, acpi_update_sci_fn update_sci,
405 MemoryRegion *parent)
a54d41a8 406{
355bf2e5
GH
407 ar->tmr.update_sci = update_sci;
408 ar->tmr.timer = qemu_new_timer_ns(vm_clock, acpi_pm_tmr_timer, ar);
77d58b1e
GH
409 memory_region_init_io(&ar->tmr.io, &acpi_pm_tmr_ops, ar, "acpi-tmr", 4);
410 memory_region_add_subregion(parent, 8, &ar->tmr.io);
a54d41a8
IY
411}
412
355bf2e5 413void acpi_pm_tmr_reset(ACPIREGS *ar)
a54d41a8 414{
355bf2e5
GH
415 ar->tmr.overflow_time = 0;
416 qemu_del_timer(ar->tmr.timer);
a54d41a8 417}
eaba51c5
IY
418
419/* ACPI PM1aCNT */
afafe4bb 420static void acpi_pm1_cnt_write(ACPIREGS *ar, uint16_t val)
eaba51c5 421{
355bf2e5 422 ar->pm1.cnt.cnt = val & ~(ACPI_BITMASK_SLEEP_ENABLE);
eaba51c5
IY
423
424 if (val & ACPI_BITMASK_SLEEP_ENABLE) {
425 /* change suspend type */
426 uint16_t sus_typ = (val >> 10) & 7;
427 switch(sus_typ) {
428 case 0: /* soft power off */
429 qemu_system_shutdown_request();
430 break;
431 case 1:
da98c8eb
GH
432 qemu_system_suspend_request();
433 break;
eaba51c5 434 default:
afafe4bb 435 if (sus_typ == ar->pm1.cnt.s4_val) { /* S4 request */
25df49f6 436 monitor_protocol_event(QEVENT_SUSPEND_DISK, NULL);
459ae5ea
GN
437 qemu_system_shutdown_request();
438 }
eaba51c5
IY
439 break;
440 }
441 }
442}
443
355bf2e5 444void acpi_pm1_cnt_update(ACPIREGS *ar,
eaba51c5
IY
445 bool sci_enable, bool sci_disable)
446{
447 /* ACPI specs 3.0, 4.7.2.5 */
448 if (sci_enable) {
355bf2e5 449 ar->pm1.cnt.cnt |= ACPI_BITMASK_SCI_ENABLE;
eaba51c5 450 } else if (sci_disable) {
355bf2e5 451 ar->pm1.cnt.cnt &= ~ACPI_BITMASK_SCI_ENABLE;
eaba51c5
IY
452 }
453}
454
afafe4bb
GH
455static uint64_t acpi_pm_cnt_read(void *opaque, hwaddr addr, unsigned width)
456{
457 ACPIREGS *ar = opaque;
458 return ar->pm1.cnt.cnt;
459}
460
461static void acpi_pm_cnt_write(void *opaque, hwaddr addr, uint64_t val,
462 unsigned width)
463{
464 acpi_pm1_cnt_write(opaque, val);
465}
466
467static const MemoryRegionOps acpi_pm_cnt_ops = {
468 .read = acpi_pm_cnt_read,
469 .write = acpi_pm_cnt_write,
470 .valid.min_access_size = 2,
471 .valid.max_access_size = 2,
472 .endianness = DEVICE_LITTLE_ENDIAN,
473};
474
475void acpi_pm1_cnt_init(ACPIREGS *ar, MemoryRegion *parent)
476{
477 ar->wakeup.notify = acpi_notify_wakeup;
478 qemu_register_wakeup_notifier(&ar->wakeup);
479 memory_region_init_io(&ar->pm1.cnt.io, &acpi_pm_cnt_ops, ar, "acpi-cnt", 2);
480 memory_region_add_subregion(parent, 4, &ar->pm1.cnt.io);
481}
482
355bf2e5 483void acpi_pm1_cnt_reset(ACPIREGS *ar)
eaba51c5 484{
355bf2e5 485 ar->pm1.cnt.cnt = 0;
eaba51c5 486}
23910d3f
IY
487
488/* ACPI GPE */
355bf2e5 489void acpi_gpe_init(ACPIREGS *ar, uint8_t len)
23910d3f 490{
355bf2e5
GH
491 ar->gpe.len = len;
492 ar->gpe.sts = g_malloc0(len / 2);
493 ar->gpe.en = g_malloc0(len / 2);
23910d3f
IY
494}
495
355bf2e5 496void acpi_gpe_reset(ACPIREGS *ar)
23910d3f 497{
355bf2e5
GH
498 memset(ar->gpe.sts, 0, ar->gpe.len / 2);
499 memset(ar->gpe.en, 0, ar->gpe.len / 2);
23910d3f
IY
500}
501
355bf2e5 502static uint8_t *acpi_gpe_ioport_get_ptr(ACPIREGS *ar, uint32_t addr)
23910d3f
IY
503{
504 uint8_t *cur = NULL;
505
355bf2e5
GH
506 if (addr < ar->gpe.len / 2) {
507 cur = ar->gpe.sts + addr;
508 } else if (addr < ar->gpe.len) {
509 cur = ar->gpe.en + addr - ar->gpe.len / 2;
23910d3f
IY
510 } else {
511 abort();
512 }
513
514 return cur;
515}
516
355bf2e5 517void acpi_gpe_ioport_writeb(ACPIREGS *ar, uint32_t addr, uint32_t val)
23910d3f
IY
518{
519 uint8_t *cur;
520
355bf2e5
GH
521 cur = acpi_gpe_ioport_get_ptr(ar, addr);
522 if (addr < ar->gpe.len / 2) {
23910d3f
IY
523 /* GPE_STS */
524 *cur = (*cur) & ~val;
355bf2e5 525 } else if (addr < ar->gpe.len) {
23910d3f
IY
526 /* GPE_EN */
527 *cur = val;
528 } else {
529 abort();
530 }
531}
532
355bf2e5 533uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr)
23910d3f
IY
534{
535 uint8_t *cur;
536 uint32_t val;
537
355bf2e5 538 cur = acpi_gpe_ioport_get_ptr(ar, addr);
23910d3f
IY
539 val = 0;
540 if (cur != NULL) {
541 val = *cur;
542 }
543
544 return val;
545}