]> Git Repo - qemu.git/blob - hw/acpi.c
block: drive_init(): Improve CHS setting error message
[qemu.git] / hw / acpi.c
1 /*
2  * ACPI implementation
3  *
4  * Copyright (c) 2006 Fabrice Bellard
5  *
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
16  * License along with this library; if not, see <http://www.gnu.org/licenses/>
17  */
18 #include "sysemu.h"
19 #include "hw.h"
20 #include "pc.h"
21 #include "acpi.h"
22
23 struct acpi_table_header
24 {
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));
35
36 char *acpi_tables;
37 size_t acpi_tables_len;
38
39 static int acpi_checksum(const uint8_t *data, int len)
40 {
41     int sum, i;
42     sum = 0;
43     for(i = 0; i < len; i++)
44         sum += data[i];
45     return (-sum) & 0xff;
46 }
47
48 int acpi_table_add(const char *t)
49 {
50     static const char *dfl_id = "QEMUQEMU";
51     char buf[1024], *p, *f;
52     struct acpi_table_header acpi_hdr;
53     unsigned long val;
54     uint32_t length;
55     struct acpi_table_header *acpi_hdr_p;
56     size_t off;
57
58     memset(&acpi_hdr, 0, sizeof(acpi_hdr));
59   
60     if (get_param_value(buf, sizeof(buf), "sig", t)) {
61         strncpy(acpi_hdr.signature, buf, 4);
62     } else {
63         strncpy(acpi_hdr.signature, dfl_id, 4);
64     }
65     if (get_param_value(buf, sizeof(buf), "rev", t)) {
66         val = strtoul(buf, &p, 10);
67         if (val > 255 || *p != '\0')
68             goto out;
69     } else {
70         val = 1;
71     }
72     acpi_hdr.revision = (int8_t)val;
73
74     if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
75         strncpy(acpi_hdr.oem_id, buf, 6);
76     } else {
77         strncpy(acpi_hdr.oem_id, dfl_id, 6);
78     }
79
80     if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
81         strncpy(acpi_hdr.oem_table_id, buf, 8);
82     } else {
83         strncpy(acpi_hdr.oem_table_id, dfl_id, 8);
84     }
85
86     if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
87         val = strtol(buf, &p, 10);
88         if(*p != '\0')
89             goto out;
90     } else {
91         val = 1;
92     }
93     acpi_hdr.oem_revision = cpu_to_le32(val);
94
95     if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
96         strncpy(acpi_hdr.asl_compiler_id, buf, 4);
97     } else {
98         strncpy(acpi_hdr.asl_compiler_id, dfl_id, 4);
99     }
100
101     if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
102         val = strtol(buf, &p, 10);
103         if(*p != '\0')
104             goto out;
105     } else {
106         val = 1;
107     }
108     acpi_hdr.asl_compiler_revision = cpu_to_le32(val);
109     
110     if (!get_param_value(buf, sizeof(buf), "data", t)) {
111          buf[0] = '\0';
112     }
113
114     length = sizeof(acpi_hdr);
115
116     f = buf;
117     while (buf[0]) {
118         struct stat s;
119         char *n = strchr(f, ':');
120         if (n)
121             *n = '\0';
122         if(stat(f, &s) < 0) {
123             fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
124             goto out;
125         }
126         length += s.st_size;
127         if (!n)
128             break;
129         *n = ':';
130         f = n + 1;
131     }
132
133     if (!acpi_tables) {
134         acpi_tables_len = sizeof(uint16_t);
135         acpi_tables = qemu_mallocz(acpi_tables_len);
136     }
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;
141
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);
146
147     f = buf;
148     while (buf[0]) {
149         struct stat s;
150         int fd;
151         char *n = strchr(f, ':');
152         if (n)
153             *n = '\0';
154         fd = open(f, O_RDONLY);
155
156         if(fd < 0)
157             goto out;
158         if(fstat(fd, &s) < 0) {
159             close(fd);
160             goto out;
161         }
162
163         /* off < length is necessary because file size can be changed
164            under our foot */
165         while(s.st_size && off < length) {
166             int r;
167             r = read(fd, p + off, s.st_size);
168             if (r > 0) {
169                 off += r;
170                 s.st_size -= r;
171             } else if ((r < 0 && errno != EINTR) || r == 0) {
172                 close(fd);
173                 goto out;
174             }
175         }
176
177         close(fd);
178         if (!n)
179             break;
180         f = n + 1;
181     }
182     if (off < length) {
183         /* don't pass random value in process to guest */
184         memset(p + off, 0, length - off);
185     }
186
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);
193     return 0;
194 out:
195     if (acpi_tables) {
196         qemu_free(acpi_tables);
197         acpi_tables = NULL;
198     }
199     return -1;
200 }
201
202 /* ACPI PM1a EVT */
203 uint16_t acpi_pm1_evt_get_sts(ACPIPM1EVT *pm1, int64_t overflow_time)
204 {
205     int64_t d = acpi_pm_tmr_get_clock();
206     if (d >= overflow_time) {
207         pm1->sts |= ACPI_BITMASK_TIMER_STATUS;
208     }
209     return pm1->sts;
210 }
211
212 void acpi_pm1_evt_write_sts(ACPIPM1EVT *pm1, ACPIPMTimer *tmr, uint16_t val)
213 {
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);
218     }
219     pm1->sts &= ~val;
220 }
221
222 void acpi_pm1_evt_power_down(ACPIPM1EVT *pm1, ACPIPMTimer *tmr)
223 {
224     if (!pm1) {
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);
229     }
230 }
231
232 void acpi_pm1_evt_reset(ACPIPM1EVT *pm1)
233 {
234     pm1->sts = 0;
235     pm1->en = 0;
236 }
237
238 /* ACPI PM_TMR */
239 void acpi_pm_tmr_update(ACPIPMTimer *tmr, bool enable)
240 {
241     int64_t expire_time;
242
243     /* schedule a timer interruption if needed */
244     if (enable) {
245         expire_time = muldiv64(tmr->overflow_time, get_ticks_per_sec(),
246                                PM_TIMER_FREQUENCY);
247         qemu_mod_timer(tmr->timer, expire_time);
248     } else {
249         qemu_del_timer(tmr->timer);
250     }
251 }
252
253 void acpi_pm_tmr_calc_overflow_time(ACPIPMTimer *tmr)
254 {
255     int64_t d = acpi_pm_tmr_get_clock();
256     tmr->overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
257 }
258
259 uint32_t acpi_pm_tmr_get(ACPIPMTimer *tmr)
260 {
261     uint32_t d = acpi_pm_tmr_get_clock();;
262     return d & 0xffffff;
263 }
264
265 static void acpi_pm_tmr_timer(void *opaque)
266 {
267     ACPIPMTimer *tmr = opaque;
268     tmr->update_sci(tmr);
269 }
270
271 void acpi_pm_tmr_init(ACPIPMTimer *tmr, acpi_update_sci_fn update_sci)
272 {
273     tmr->update_sci = update_sci;
274     tmr->timer = qemu_new_timer_ns(vm_clock, acpi_pm_tmr_timer, tmr);
275 }
276
277 void acpi_pm_tmr_reset(ACPIPMTimer *tmr)
278 {
279     tmr->overflow_time = 0;
280     qemu_del_timer(tmr->timer);
281 }
282
283 /* ACPI PM1aCNT */
284 void acpi_pm1_cnt_init(ACPIPM1CNT *pm1_cnt, qemu_irq cmos_s3)
285 {
286     pm1_cnt->cmos_s3 = cmos_s3;
287 }
288
289 void acpi_pm1_cnt_write(ACPIPM1EVT *pm1a, ACPIPM1CNT *pm1_cnt, uint16_t val)
290 {
291     pm1_cnt->cnt = val & ~(ACPI_BITMASK_SLEEP_ENABLE);
292
293     if (val & ACPI_BITMASK_SLEEP_ENABLE) {
294         /* change suspend type */
295         uint16_t sus_typ = (val >> 10) & 7;
296         switch(sus_typ) {
297         case 0: /* soft power off */
298             qemu_system_shutdown_request();
299             break;
300         case 1:
301             /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
302                Pretend that resume was caused by power button */
303             pm1a->sts |=
304                 (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_POWER_BUTTON_STATUS);
305             qemu_system_reset_request();
306             qemu_irq_raise(pm1_cnt->cmos_s3);
307         default:
308             break;
309         }
310     }
311 }
312
313 void acpi_pm1_cnt_update(ACPIPM1CNT *pm1_cnt,
314                          bool sci_enable, bool sci_disable)
315 {
316     /* ACPI specs 3.0, 4.7.2.5 */
317     if (sci_enable) {
318         pm1_cnt->cnt |= ACPI_BITMASK_SCI_ENABLE;
319     } else if (sci_disable) {
320         pm1_cnt->cnt &= ~ACPI_BITMASK_SCI_ENABLE;
321     }
322 }
323
324 void acpi_pm1_cnt_reset(ACPIPM1CNT *pm1_cnt)
325 {
326     pm1_cnt->cnt = 0;
327     if (pm1_cnt->cmos_s3) {
328         qemu_irq_lower(pm1_cnt->cmos_s3);
329     }
330 }
331
332 /* ACPI GPE */
333 void acpi_gpe_init(ACPIGPE *gpe, uint8_t len)
334 {
335     gpe->len = len;
336     gpe->sts = qemu_mallocz(len / 2);
337     gpe->en = qemu_mallocz(len / 2);
338 }
339
340 void acpi_gpe_blk(ACPIGPE *gpe, uint32_t blk)
341 {
342     gpe->blk = blk;
343 }
344
345 void acpi_gpe_reset(ACPIGPE *gpe)
346 {
347     memset(gpe->sts, 0, gpe->len / 2);
348     memset(gpe->en, 0, gpe->len / 2);
349 }
350
351 static uint8_t *acpi_gpe_ioport_get_ptr(ACPIGPE *gpe, uint32_t addr)
352 {
353     uint8_t *cur = NULL;
354
355     if (addr < gpe->len / 2) {
356         cur = gpe->sts + addr;
357     } else if (addr < gpe->len) {
358         cur = gpe->en + addr - gpe->len / 2;
359     } else {
360         abort();
361     }
362
363     return cur;
364 }
365
366 void acpi_gpe_ioport_writeb(ACPIGPE *gpe, uint32_t addr, uint32_t val)
367 {
368     uint8_t *cur;
369
370     addr -= gpe->blk;
371     cur = acpi_gpe_ioport_get_ptr(gpe, addr);
372     if (addr < gpe->len / 2) {
373         /* GPE_STS */
374         *cur = (*cur) & ~val;
375     } else if (addr < gpe->len) {
376         /* GPE_EN */
377         *cur = val;
378     } else {
379         abort();
380     }
381 }
382
383 uint32_t acpi_gpe_ioport_readb(ACPIGPE *gpe, uint32_t addr)
384 {
385     uint8_t *cur;
386     uint32_t val;
387
388     addr -= gpe->blk;
389     cur = acpi_gpe_ioport_get_ptr(gpe, addr);
390     val = 0;
391     if (cur != NULL) {
392         val = *cur;
393     }
394
395     return val;
396 }
This page took 0.046259 seconds and 4 git commands to generate.