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/>
23 struct acpi_table_header
25 char signature [4]; /* ACPI signature (4 ASCII characters) */
26 uint32_t length; /* Length of table, in bytes, including header */
27 uint8_t revision; /* ACPI Specification minor version # */
28 uint8_t checksum; /* To make sum of entire table == 0 */
29 char oem_id [6]; /* OEM identification */
30 char oem_table_id [8]; /* OEM table identification */
31 uint32_t oem_revision; /* OEM revision number */
32 char asl_compiler_id [4]; /* ASL compiler vendor ID */
33 uint32_t asl_compiler_revision; /* ASL compiler revision number */
34 } __attribute__((packed));
37 size_t acpi_tables_len;
39 static int acpi_checksum(const uint8_t *data, int len)
43 for(i = 0; i < len; i++)
48 int acpi_table_add(const char *t)
50 static const char *dfl_id = "QEMUQEMU";
51 char buf[1024], *p, *f;
52 struct acpi_table_header acpi_hdr;
55 struct acpi_table_header *acpi_hdr_p;
58 memset(&acpi_hdr, 0, sizeof(acpi_hdr));
60 if (get_param_value(buf, sizeof(buf), "sig", t)) {
61 strncpy(acpi_hdr.signature, buf, 4);
63 strncpy(acpi_hdr.signature, dfl_id, 4);
65 if (get_param_value(buf, sizeof(buf), "rev", t)) {
66 val = strtoul(buf, &p, 10);
67 if (val > 255 || *p != '\0')
72 acpi_hdr.revision = (int8_t)val;
74 if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
75 strncpy(acpi_hdr.oem_id, buf, 6);
77 strncpy(acpi_hdr.oem_id, dfl_id, 6);
80 if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
81 strncpy(acpi_hdr.oem_table_id, buf, 8);
83 strncpy(acpi_hdr.oem_table_id, dfl_id, 8);
86 if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
87 val = strtol(buf, &p, 10);
93 acpi_hdr.oem_revision = cpu_to_le32(val);
95 if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
96 strncpy(acpi_hdr.asl_compiler_id, buf, 4);
98 strncpy(acpi_hdr.asl_compiler_id, dfl_id, 4);
101 if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
102 val = strtol(buf, &p, 10);
108 acpi_hdr.asl_compiler_revision = cpu_to_le32(val);
110 if (!get_param_value(buf, sizeof(buf), "data", t)) {
114 length = sizeof(acpi_hdr);
119 char *n = strchr(f, ':');
122 if(stat(f, &s) < 0) {
123 fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
134 acpi_tables_len = sizeof(uint16_t);
135 acpi_tables = qemu_mallocz(acpi_tables_len);
137 acpi_tables = qemu_realloc(acpi_tables,
138 acpi_tables_len + sizeof(uint16_t) + length);
139 p = acpi_tables + acpi_tables_len;
140 acpi_tables_len += sizeof(uint16_t) + length;
142 *(uint16_t*)p = cpu_to_le32(length);
143 p += sizeof(uint16_t);
144 memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
145 off = sizeof(acpi_hdr);
151 char *n = strchr(f, ':');
154 fd = open(f, O_RDONLY);
158 if(fstat(fd, &s) < 0) {
163 /* off < length is necessary because file size can be changed
165 while(s.st_size && off < length) {
167 r = read(fd, p + off, s.st_size);
171 } else if ((r < 0 && errno != EINTR) || r == 0) {
183 /* don't pass random value in process to guest */
184 memset(p + off, 0, length - off);
187 acpi_hdr_p = (struct acpi_table_header*)p;
188 acpi_hdr_p->length = cpu_to_le32(length);
189 acpi_hdr_p->checksum = acpi_checksum((uint8_t*)p, length);
190 /* increase number of tables */
191 (*(uint16_t*)acpi_tables) =
192 cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
196 qemu_free(acpi_tables);
203 uint16_t acpi_pm1_evt_get_sts(ACPIPM1EVT *pm1, int64_t overflow_time)
205 int64_t d = acpi_pm_tmr_get_clock();
206 if (d >= overflow_time) {
207 pm1->sts |= ACPI_BITMASK_TIMER_STATUS;
212 void acpi_pm1_evt_write_sts(ACPIPM1EVT *pm1, ACPIPMTimer *tmr, uint16_t val)
214 uint16_t pm1_sts = acpi_pm1_evt_get_sts(pm1, tmr->overflow_time);
215 if (pm1_sts & val & ACPI_BITMASK_TIMER_STATUS) {
216 /* if TMRSTS is reset, then compute the new overflow time */
217 acpi_pm_tmr_calc_overflow_time(tmr);
222 void acpi_pm1_evt_power_down(ACPIPM1EVT *pm1, ACPIPMTimer *tmr)
225 qemu_system_shutdown_request();
226 } else if (pm1->en & ACPI_BITMASK_POWER_BUTTON_ENABLE) {
227 pm1->sts |= ACPI_BITMASK_POWER_BUTTON_STATUS;
228 tmr->update_sci(tmr);
232 void acpi_pm1_evt_reset(ACPIPM1EVT *pm1)
239 void acpi_pm_tmr_update(ACPIPMTimer *tmr, bool enable)
243 /* schedule a timer interruption if needed */
245 expire_time = muldiv64(tmr->overflow_time, get_ticks_per_sec(),
247 qemu_mod_timer(tmr->timer, expire_time);
249 qemu_del_timer(tmr->timer);
253 void acpi_pm_tmr_calc_overflow_time(ACPIPMTimer *tmr)
255 int64_t d = acpi_pm_tmr_get_clock();
256 tmr->overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
259 uint32_t acpi_pm_tmr_get(ACPIPMTimer *tmr)
261 uint32_t d = acpi_pm_tmr_get_clock();;
265 static void acpi_pm_tmr_timer(void *opaque)
267 ACPIPMTimer *tmr = opaque;
268 tmr->update_sci(tmr);
271 void acpi_pm_tmr_init(ACPIPMTimer *tmr, acpi_update_sci_fn update_sci)
273 tmr->update_sci = update_sci;
274 tmr->timer = qemu_new_timer_ns(vm_clock, acpi_pm_tmr_timer, tmr);
277 void acpi_pm_tmr_reset(ACPIPMTimer *tmr)
279 tmr->overflow_time = 0;
280 qemu_del_timer(tmr->timer);