]> git.proxmox.com Git - qemu.git/blame - hw/acpi.c
PPC: Fix TLB invalidation bug within the PPC interrupt handler.
[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"
6515b203 25
104bf02e
MT
26struct 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) */
8a92ea2f
AL
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 */
104bf02e
MT
33 char oem_id[6]; /* OEM identification */
34 char oem_table_id[8]; /* OEM table identification */
8a92ea2f 35 uint32_t oem_revision; /* OEM revision number */
104bf02e 36 char asl_compiler_id[4]; /* ASL compiler vendor ID */
8a92ea2f 37 uint32_t asl_compiler_revision; /* ASL compiler revision number */
541dc0d4 38} QEMU_PACKED;
8a92ea2f 39
104bf02e
MT
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 */
42
43static 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) */
48 ;
49
8a92ea2f
AL
50char *acpi_tables;
51size_t acpi_tables_len;
52
53static int acpi_checksum(const uint8_t *data, int len)
54{
55 int sum, i;
56 sum = 0;
104bf02e 57 for (i = 0; i < len; i++) {
8a92ea2f 58 sum += data[i];
104bf02e 59 }
8a92ea2f
AL
60 return (-sum) & 0xff;
61}
62
104bf02e
MT
63/* like strncpy() but zero-fills the tail of destination */
64static void strzcpy(char *dst, const char *src, size_t size)
65{
66 size_t len = strlen(src);
67 if (len >= size) {
68 len = size;
69 } else {
70 memset(dst + len, 0, size - len);
71 }
72 memcpy(dst, src, len);
73}
74
75/* XXX fixme: this function uses obsolete argument parsing interface */
8a92ea2f
AL
76int acpi_table_add(const char *t)
77{
8a92ea2f 78 char buf[1024], *p, *f;
8a92ea2f 79 unsigned long val;
104bf02e
MT
80 size_t len, start, allen;
81 bool has_header;
82 int changed;
83 int r;
84 struct acpi_table_header hdr;
8a92ea2f 85
104bf02e
MT
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;
102 }
103
104 if (!acpi_tables) {
105 allen = sizeof(uint16_t);
7267c094 106 acpi_tables = g_malloc0(allen);
8a92ea2f 107 } else {
104bf02e
MT
108 allen = acpi_tables_len;
109 }
110
111 start = allen;
7267c094 112 acpi_tables = g_realloc(acpi_tables, start + ACPI_TABLE_HDR_SIZE);
104bf02e
MT
113 allen += has_header ? ACPI_TABLE_PFX_SIZE : ACPI_TABLE_HDR_SIZE;
114
115 /* now read in the data files, reallocating buffer as needed */
116
117 for (f = strtok(buf, ":"); f; f = strtok(NULL, ":")) {
118 int fd = open(f, O_RDONLY);
119
120 if (fd < 0) {
121 fprintf(stderr, "can't open file %s: %s\n", f, strerror(errno));
122 return -1;
123 }
124
125 for (;;) {
126 char data[8192];
127 r = read(fd, data, sizeof(data));
128 if (r == 0) {
129 break;
130 } else if (r > 0) {
7267c094 131 acpi_tables = g_realloc(acpi_tables, allen + r);
104bf02e
MT
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;
139 }
140 }
141
142 close(fd);
143 }
144
145 /* now fill in the header fields */
146
147 f = acpi_tables + start; /* start of the table */
148 changed = 0;
149
150 /* copy the header to temp place to align the fields */
151 memcpy(&hdr, has_header ? f : dfl_hdr, ACPI_TABLE_HDR_SIZE);
152
153 /* length of the table minus our prefix */
154 len = allen - start - ACPI_TABLE_PFX_SIZE;
155
156 hdr._length = cpu_to_le16(len);
157
158 if (get_param_value(buf, sizeof(buf), "sig", t)) {
159 strzcpy(hdr.sig, buf, sizeof(hdr.sig));
160 ++changed;
161 }
162
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;
173 }
8a92ea2f 174 }
104bf02e
MT
175 /* we may avoid putting length here if has_header is true */
176 hdr.length = cpu_to_le32(len);
177
8a92ea2f 178 if (get_param_value(buf, sizeof(buf), "rev", t)) {
104bf02e
MT
179 val = strtoul(buf, &p, 0);
180 if (val > 255 || *p) {
181 fprintf(stderr, "acpitable: \"rev=%s\" is invalid\n", buf);
182 return -1;
183 }
184 hdr.revision = (uint8_t)val;
185 ++changed;
8a92ea2f 186 }
8a92ea2f
AL
187
188 if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
104bf02e
MT
189 strzcpy(hdr.oem_id, buf, sizeof(hdr.oem_id));
190 ++changed;
8a92ea2f
AL
191 }
192
193 if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
104bf02e
MT
194 strzcpy(hdr.oem_table_id, buf, sizeof(hdr.oem_table_id));
195 ++changed;
8a92ea2f
AL
196 }
197
198 if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
104bf02e
MT
199 val = strtol(buf, &p, 0);
200 if (*p) {
201 fprintf(stderr, "acpitable: \"oem_rev=%s\" is invalid\n", buf);
202 return -1;
203 }
204 hdr.oem_revision = cpu_to_le32(val);
205 ++changed;
8a92ea2f 206 }
8a92ea2f
AL
207
208 if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
104bf02e
MT
209 strzcpy(hdr.asl_compiler_id, buf, sizeof(hdr.asl_compiler_id));
210 ++changed;
8a92ea2f
AL
211 }
212
213 if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
104bf02e
MT
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;
219 }
220 hdr.asl_compiler_revision = cpu_to_le32(val);
221 ++changed;
8a92ea2f
AL
222 }
223
104bf02e
MT
224 if (!has_header && !changed) {
225 fprintf(stderr, "warning: acpitable: no table headers are specified\n");
8a92ea2f
AL
226 }
227
8a92ea2f 228
104bf02e
MT
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 */
8a92ea2f 233
104bf02e
MT
234 /* put header back */
235 memcpy(f, &hdr, sizeof(hdr));
236
237 if (changed || !has_header || 1) {
238 ((struct acpi_table_header *)f)->checksum =
239 acpi_checksum((uint8_t *)f + ACPI_TABLE_PFX_SIZE, len);
d729bb9a 240 }
8a92ea2f 241
8a92ea2f 242 /* increase number of tables */
104bf02e
MT
243 (*(uint16_t *)acpi_tables) =
244 cpu_to_le32(le32_to_cpu(*(uint16_t *)acpi_tables) + 1);
245
246 acpi_tables_len = allen;
8a92ea2f 247 return 0;
104bf02e 248
8a92ea2f 249}
a54d41a8 250
da98c8eb
GH
251static void acpi_notify_wakeup(Notifier *notifier, void *data)
252{
253 ACPIREGS *ar = container_of(notifier, ACPIREGS, wakeup);
254 WakeupReason *reason = data;
255
256 switch (*reason) {
62aeb0f7
GH
257 case QEMU_WAKEUP_REASON_RTC:
258 ar->pm1.evt.sts |=
259 (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_RT_CLOCK_STATUS);
260 break;
6595abc0
GH
261 case QEMU_WAKEUP_REASON_PMTIMER:
262 ar->pm1.evt.sts |=
263 (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_TIMER_STATUS);
264 break;
da98c8eb
GH
265 case QEMU_WAKEUP_REASON_OTHER:
266 default:
267 /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
268 Pretend that resume was caused by power button */
269 ar->pm1.evt.sts |=
270 (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_POWER_BUTTON_STATUS);
271 break;
272 }
273}
274
04dc308f 275/* ACPI PM1a EVT */
2886be1b 276uint16_t acpi_pm1_evt_get_sts(ACPIREGS *ar)
04dc308f
IY
277{
278 int64_t d = acpi_pm_tmr_get_clock();
2886be1b 279 if (d >= ar->tmr.overflow_time) {
355bf2e5 280 ar->pm1.evt.sts |= ACPI_BITMASK_TIMER_STATUS;
04dc308f 281 }
355bf2e5 282 return ar->pm1.evt.sts;
04dc308f
IY
283}
284
355bf2e5 285void acpi_pm1_evt_write_sts(ACPIREGS *ar, uint16_t val)
04dc308f 286{
2886be1b 287 uint16_t pm1_sts = acpi_pm1_evt_get_sts(ar);
04dc308f
IY
288 if (pm1_sts & val & ACPI_BITMASK_TIMER_STATUS) {
289 /* if TMRSTS is reset, then compute the new overflow time */
355bf2e5 290 acpi_pm_tmr_calc_overflow_time(ar);
04dc308f 291 }
355bf2e5 292 ar->pm1.evt.sts &= ~val;
04dc308f
IY
293}
294
8283c4f5
GH
295void acpi_pm1_evt_write_en(ACPIREGS *ar, uint16_t val)
296{
297 ar->pm1.evt.en = val;
62aeb0f7
GH
298 qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_RTC,
299 val & ACPI_BITMASK_RT_CLOCK_ENABLE);
6595abc0
GH
300 qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_PMTIMER,
301 val & ACPI_BITMASK_TIMER_ENABLE);
8283c4f5
GH
302}
303
355bf2e5 304void acpi_pm1_evt_power_down(ACPIREGS *ar)
04dc308f 305{
355bf2e5
GH
306 if (ar->pm1.evt.en & ACPI_BITMASK_POWER_BUTTON_ENABLE) {
307 ar->pm1.evt.sts |= ACPI_BITMASK_POWER_BUTTON_STATUS;
308 ar->tmr.update_sci(ar);
04dc308f
IY
309 }
310}
311
355bf2e5 312void acpi_pm1_evt_reset(ACPIREGS *ar)
04dc308f 313{
355bf2e5
GH
314 ar->pm1.evt.sts = 0;
315 ar->pm1.evt.en = 0;
62aeb0f7 316 qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_RTC, 0);
6595abc0 317 qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_PMTIMER, 0);
04dc308f
IY
318}
319
a54d41a8 320/* ACPI PM_TMR */
355bf2e5 321void acpi_pm_tmr_update(ACPIREGS *ar, bool enable)
a54d41a8
IY
322{
323 int64_t expire_time;
324
325 /* schedule a timer interruption if needed */
326 if (enable) {
355bf2e5 327 expire_time = muldiv64(ar->tmr.overflow_time, get_ticks_per_sec(),
a54d41a8 328 PM_TIMER_FREQUENCY);
355bf2e5 329 qemu_mod_timer(ar->tmr.timer, expire_time);
a54d41a8 330 } else {
355bf2e5 331 qemu_del_timer(ar->tmr.timer);
a54d41a8
IY
332 }
333}
334
355bf2e5 335void acpi_pm_tmr_calc_overflow_time(ACPIREGS *ar)
a54d41a8
IY
336{
337 int64_t d = acpi_pm_tmr_get_clock();
355bf2e5 338 ar->tmr.overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
a54d41a8
IY
339}
340
355bf2e5 341uint32_t acpi_pm_tmr_get(ACPIREGS *ar)
a54d41a8 342{
3a93113a 343 uint32_t d = acpi_pm_tmr_get_clock();
a54d41a8
IY
344 return d & 0xffffff;
345}
346
347static void acpi_pm_tmr_timer(void *opaque)
348{
355bf2e5 349 ACPIREGS *ar = opaque;
6595abc0 350 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_PMTIMER);
355bf2e5 351 ar->tmr.update_sci(ar);
a54d41a8
IY
352}
353
355bf2e5 354void acpi_pm_tmr_init(ACPIREGS *ar, acpi_update_sci_fn update_sci)
a54d41a8 355{
355bf2e5
GH
356 ar->tmr.update_sci = update_sci;
357 ar->tmr.timer = qemu_new_timer_ns(vm_clock, acpi_pm_tmr_timer, ar);
a54d41a8
IY
358}
359
355bf2e5 360void acpi_pm_tmr_reset(ACPIREGS *ar)
a54d41a8 361{
355bf2e5
GH
362 ar->tmr.overflow_time = 0;
363 qemu_del_timer(ar->tmr.timer);
a54d41a8 364}
eaba51c5
IY
365
366/* ACPI PM1aCNT */
da98c8eb 367void acpi_pm1_cnt_init(ACPIREGS *ar)
eaba51c5 368{
da98c8eb
GH
369 ar->wakeup.notify = acpi_notify_wakeup;
370 qemu_register_wakeup_notifier(&ar->wakeup);
eaba51c5
IY
371}
372
355bf2e5 373void acpi_pm1_cnt_write(ACPIREGS *ar, uint16_t val)
eaba51c5 374{
355bf2e5 375 ar->pm1.cnt.cnt = val & ~(ACPI_BITMASK_SLEEP_ENABLE);
eaba51c5
IY
376
377 if (val & ACPI_BITMASK_SLEEP_ENABLE) {
378 /* change suspend type */
379 uint16_t sus_typ = (val >> 10) & 7;
380 switch(sus_typ) {
381 case 0: /* soft power off */
382 qemu_system_shutdown_request();
383 break;
384 case 1:
da98c8eb
GH
385 qemu_system_suspend_request();
386 break;
eaba51c5
IY
387 default:
388 break;
389 }
390 }
391}
392
355bf2e5 393void acpi_pm1_cnt_update(ACPIREGS *ar,
eaba51c5
IY
394 bool sci_enable, bool sci_disable)
395{
396 /* ACPI specs 3.0, 4.7.2.5 */
397 if (sci_enable) {
355bf2e5 398 ar->pm1.cnt.cnt |= ACPI_BITMASK_SCI_ENABLE;
eaba51c5 399 } else if (sci_disable) {
355bf2e5 400 ar->pm1.cnt.cnt &= ~ACPI_BITMASK_SCI_ENABLE;
eaba51c5
IY
401 }
402}
403
355bf2e5 404void acpi_pm1_cnt_reset(ACPIREGS *ar)
eaba51c5 405{
355bf2e5 406 ar->pm1.cnt.cnt = 0;
eaba51c5 407}
23910d3f
IY
408
409/* ACPI GPE */
355bf2e5 410void acpi_gpe_init(ACPIREGS *ar, uint8_t len)
23910d3f 411{
355bf2e5
GH
412 ar->gpe.len = len;
413 ar->gpe.sts = g_malloc0(len / 2);
414 ar->gpe.en = g_malloc0(len / 2);
23910d3f
IY
415}
416
355bf2e5 417void acpi_gpe_blk(ACPIREGS *ar, uint32_t blk)
23910d3f 418{
355bf2e5 419 ar->gpe.blk = blk;
23910d3f
IY
420}
421
355bf2e5 422void acpi_gpe_reset(ACPIREGS *ar)
23910d3f 423{
355bf2e5
GH
424 memset(ar->gpe.sts, 0, ar->gpe.len / 2);
425 memset(ar->gpe.en, 0, ar->gpe.len / 2);
23910d3f
IY
426}
427
355bf2e5 428static uint8_t *acpi_gpe_ioport_get_ptr(ACPIREGS *ar, uint32_t addr)
23910d3f
IY
429{
430 uint8_t *cur = NULL;
431
355bf2e5
GH
432 if (addr < ar->gpe.len / 2) {
433 cur = ar->gpe.sts + addr;
434 } else if (addr < ar->gpe.len) {
435 cur = ar->gpe.en + addr - ar->gpe.len / 2;
23910d3f
IY
436 } else {
437 abort();
438 }
439
440 return cur;
441}
442
355bf2e5 443void acpi_gpe_ioport_writeb(ACPIREGS *ar, uint32_t addr, uint32_t val)
23910d3f
IY
444{
445 uint8_t *cur;
446
355bf2e5
GH
447 addr -= ar->gpe.blk;
448 cur = acpi_gpe_ioport_get_ptr(ar, addr);
449 if (addr < ar->gpe.len / 2) {
23910d3f
IY
450 /* GPE_STS */
451 *cur = (*cur) & ~val;
355bf2e5 452 } else if (addr < ar->gpe.len) {
23910d3f
IY
453 /* GPE_EN */
454 *cur = val;
455 } else {
456 abort();
457 }
458}
459
355bf2e5 460uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr)
23910d3f
IY
461{
462 uint8_t *cur;
463 uint32_t val;
464
355bf2e5
GH
465 addr -= ar->gpe.blk;
466 cur = acpi_gpe_ioport_get_ptr(ar, addr);
23910d3f
IY
467 val = 0;
468 if (cur != NULL) {
469 val = *cur;
470 }
471
472 return val;
473}