2
0

acpi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. * Contributions after 2012-01-13 are licensed under the terms of the
  19. * GNU GPL, version 2 or (at your option) any later version.
  20. */
  21. #include "sysemu.h"
  22. #include "hw.h"
  23. #include "pc.h"
  24. #include "acpi.h"
  25. #include "monitor.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;
  39. #define ACPI_TABLE_HDR_SIZE sizeof(struct acpi_table_header)
  40. #define ACPI_TABLE_PFX_SIZE sizeof(uint16_t) /* size of the extra prefix */
  41. static const char dfl_hdr[ACPI_TABLE_HDR_SIZE] =
  42. "\0\0" /* fake _length (2) */
  43. "QEMU\0\0\0\0\1\0" /* sig (4), len(4), revno (1), csum (1) */
  44. "QEMUQEQEMUQEMU\1\0\0\0" /* OEM id (6), table (8), revno (4) */
  45. "QEMU\1\0\0\0" /* ASL compiler ID (4), version (4) */
  46. ;
  47. char *acpi_tables;
  48. size_t acpi_tables_len;
  49. static int acpi_checksum(const uint8_t *data, int len)
  50. {
  51. int sum, i;
  52. sum = 0;
  53. for (i = 0; i < len; i++) {
  54. sum += data[i];
  55. }
  56. return (-sum) & 0xff;
  57. }
  58. /* like strncpy() but zero-fills the tail of destination */
  59. static void strzcpy(char *dst, const char *src, size_t size)
  60. {
  61. size_t len = strlen(src);
  62. if (len >= size) {
  63. len = size;
  64. } else {
  65. memset(dst + len, 0, size - len);
  66. }
  67. memcpy(dst, src, len);
  68. }
  69. /* XXX fixme: this function uses obsolete argument parsing interface */
  70. int acpi_table_add(const char *t)
  71. {
  72. char buf[1024], *p, *f;
  73. unsigned long val;
  74. size_t len, start, allen;
  75. bool has_header;
  76. int changed;
  77. int r;
  78. struct acpi_table_header hdr;
  79. r = 0;
  80. r |= get_param_value(buf, sizeof(buf), "data", t) ? 1 : 0;
  81. r |= get_param_value(buf, sizeof(buf), "file", t) ? 2 : 0;
  82. switch (r) {
  83. case 0:
  84. buf[0] = '\0';
  85. /* fallthrough for default behavior */
  86. case 1:
  87. has_header = false;
  88. break;
  89. case 2:
  90. has_header = true;
  91. break;
  92. default:
  93. fprintf(stderr, "acpitable: both data and file are specified\n");
  94. return -1;
  95. }
  96. if (!acpi_tables) {
  97. allen = sizeof(uint16_t);
  98. acpi_tables = g_malloc0(allen);
  99. } else {
  100. allen = acpi_tables_len;
  101. }
  102. start = allen;
  103. acpi_tables = g_realloc(acpi_tables, start + ACPI_TABLE_HDR_SIZE);
  104. allen += has_header ? ACPI_TABLE_PFX_SIZE : ACPI_TABLE_HDR_SIZE;
  105. /* now read in the data files, reallocating buffer as needed */
  106. for (f = strtok(buf, ":"); f; f = strtok(NULL, ":")) {
  107. int fd = open(f, O_RDONLY);
  108. if (fd < 0) {
  109. fprintf(stderr, "can't open file %s: %s\n", f, strerror(errno));
  110. return -1;
  111. }
  112. for (;;) {
  113. char data[8192];
  114. r = read(fd, data, sizeof(data));
  115. if (r == 0) {
  116. break;
  117. } else if (r > 0) {
  118. acpi_tables = g_realloc(acpi_tables, allen + r);
  119. memcpy(acpi_tables + allen, data, r);
  120. allen += r;
  121. } else if (errno != EINTR) {
  122. fprintf(stderr, "can't read file %s: %s\n",
  123. f, strerror(errno));
  124. close(fd);
  125. return -1;
  126. }
  127. }
  128. close(fd);
  129. }
  130. /* now fill in the header fields */
  131. f = acpi_tables + start; /* start of the table */
  132. changed = 0;
  133. /* copy the header to temp place to align the fields */
  134. memcpy(&hdr, has_header ? f : dfl_hdr, ACPI_TABLE_HDR_SIZE);
  135. /* length of the table minus our prefix */
  136. len = allen - start - ACPI_TABLE_PFX_SIZE;
  137. hdr._length = cpu_to_le16(len);
  138. if (get_param_value(buf, sizeof(buf), "sig", t)) {
  139. strzcpy(hdr.sig, buf, sizeof(hdr.sig));
  140. ++changed;
  141. }
  142. /* length of the table including header, in bytes */
  143. if (has_header) {
  144. /* check if actual length is correct */
  145. val = le32_to_cpu(hdr.length);
  146. if (val != len) {
  147. fprintf(stderr,
  148. "warning: acpitable has wrong length,"
  149. " header says %lu, actual size %zu bytes\n",
  150. val, len);
  151. ++changed;
  152. }
  153. }
  154. /* we may avoid putting length here if has_header is true */
  155. hdr.length = cpu_to_le32(len);
  156. if (get_param_value(buf, sizeof(buf), "rev", t)) {
  157. val = strtoul(buf, &p, 0);
  158. if (val > 255 || *p) {
  159. fprintf(stderr, "acpitable: \"rev=%s\" is invalid\n", buf);
  160. return -1;
  161. }
  162. hdr.revision = (uint8_t)val;
  163. ++changed;
  164. }
  165. if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
  166. strzcpy(hdr.oem_id, buf, sizeof(hdr.oem_id));
  167. ++changed;
  168. }
  169. if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
  170. strzcpy(hdr.oem_table_id, buf, sizeof(hdr.oem_table_id));
  171. ++changed;
  172. }
  173. if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
  174. val = strtol(buf, &p, 0);
  175. if (*p) {
  176. fprintf(stderr, "acpitable: \"oem_rev=%s\" is invalid\n", buf);
  177. return -1;
  178. }
  179. hdr.oem_revision = cpu_to_le32(val);
  180. ++changed;
  181. }
  182. if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
  183. strzcpy(hdr.asl_compiler_id, buf, sizeof(hdr.asl_compiler_id));
  184. ++changed;
  185. }
  186. if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
  187. val = strtol(buf, &p, 0);
  188. if (*p) {
  189. fprintf(stderr, "acpitable: \"%s=%s\" is invalid\n",
  190. "asl_compiler_rev", buf);
  191. return -1;
  192. }
  193. hdr.asl_compiler_revision = cpu_to_le32(val);
  194. ++changed;
  195. }
  196. if (!has_header && !changed) {
  197. fprintf(stderr, "warning: acpitable: no table headers are specified\n");
  198. }
  199. /* now calculate checksum of the table, complete with the header */
  200. /* we may as well leave checksum intact if has_header is true */
  201. /* alternatively there may be a way to set cksum to a given value */
  202. hdr.checksum = 0; /* for checksum calculation */
  203. /* put header back */
  204. memcpy(f, &hdr, sizeof(hdr));
  205. if (changed || !has_header || 1) {
  206. ((struct acpi_table_header *)f)->checksum =
  207. acpi_checksum((uint8_t *)f + ACPI_TABLE_PFX_SIZE, len);
  208. }
  209. /* increase number of tables */
  210. (*(uint16_t *)acpi_tables) =
  211. cpu_to_le32(le32_to_cpu(*(uint16_t *)acpi_tables) + 1);
  212. acpi_tables_len = allen;
  213. return 0;
  214. }
  215. static void acpi_notify_wakeup(Notifier *notifier, void *data)
  216. {
  217. ACPIREGS *ar = container_of(notifier, ACPIREGS, wakeup);
  218. WakeupReason *reason = data;
  219. switch (*reason) {
  220. case QEMU_WAKEUP_REASON_RTC:
  221. ar->pm1.evt.sts |=
  222. (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_RT_CLOCK_STATUS);
  223. break;
  224. case QEMU_WAKEUP_REASON_PMTIMER:
  225. ar->pm1.evt.sts |=
  226. (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_TIMER_STATUS);
  227. break;
  228. case QEMU_WAKEUP_REASON_OTHER:
  229. default:
  230. /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
  231. Pretend that resume was caused by power button */
  232. ar->pm1.evt.sts |=
  233. (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_POWER_BUTTON_STATUS);
  234. break;
  235. }
  236. }
  237. /* ACPI PM1a EVT */
  238. uint16_t acpi_pm1_evt_get_sts(ACPIREGS *ar)
  239. {
  240. int64_t d = acpi_pm_tmr_get_clock();
  241. if (d >= ar->tmr.overflow_time) {
  242. ar->pm1.evt.sts |= ACPI_BITMASK_TIMER_STATUS;
  243. }
  244. return ar->pm1.evt.sts;
  245. }
  246. void acpi_pm1_evt_write_sts(ACPIREGS *ar, uint16_t val)
  247. {
  248. uint16_t pm1_sts = acpi_pm1_evt_get_sts(ar);
  249. if (pm1_sts & val & ACPI_BITMASK_TIMER_STATUS) {
  250. /* if TMRSTS is reset, then compute the new overflow time */
  251. acpi_pm_tmr_calc_overflow_time(ar);
  252. }
  253. ar->pm1.evt.sts &= ~val;
  254. }
  255. void acpi_pm1_evt_write_en(ACPIREGS *ar, uint16_t val)
  256. {
  257. ar->pm1.evt.en = val;
  258. qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_RTC,
  259. val & ACPI_BITMASK_RT_CLOCK_ENABLE);
  260. qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_PMTIMER,
  261. val & ACPI_BITMASK_TIMER_ENABLE);
  262. }
  263. void acpi_pm1_evt_power_down(ACPIREGS *ar)
  264. {
  265. if (ar->pm1.evt.en & ACPI_BITMASK_POWER_BUTTON_ENABLE) {
  266. ar->pm1.evt.sts |= ACPI_BITMASK_POWER_BUTTON_STATUS;
  267. ar->tmr.update_sci(ar);
  268. }
  269. }
  270. void acpi_pm1_evt_reset(ACPIREGS *ar)
  271. {
  272. ar->pm1.evt.sts = 0;
  273. ar->pm1.evt.en = 0;
  274. qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_RTC, 0);
  275. qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_PMTIMER, 0);
  276. }
  277. /* ACPI PM_TMR */
  278. void acpi_pm_tmr_update(ACPIREGS *ar, bool enable)
  279. {
  280. int64_t expire_time;
  281. /* schedule a timer interruption if needed */
  282. if (enable) {
  283. expire_time = muldiv64(ar->tmr.overflow_time, get_ticks_per_sec(),
  284. PM_TIMER_FREQUENCY);
  285. qemu_mod_timer(ar->tmr.timer, expire_time);
  286. } else {
  287. qemu_del_timer(ar->tmr.timer);
  288. }
  289. }
  290. void acpi_pm_tmr_calc_overflow_time(ACPIREGS *ar)
  291. {
  292. int64_t d = acpi_pm_tmr_get_clock();
  293. ar->tmr.overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
  294. }
  295. uint32_t acpi_pm_tmr_get(ACPIREGS *ar)
  296. {
  297. uint32_t d = acpi_pm_tmr_get_clock();
  298. return d & 0xffffff;
  299. }
  300. static void acpi_pm_tmr_timer(void *opaque)
  301. {
  302. ACPIREGS *ar = opaque;
  303. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_PMTIMER);
  304. ar->tmr.update_sci(ar);
  305. }
  306. void acpi_pm_tmr_init(ACPIREGS *ar, acpi_update_sci_fn update_sci)
  307. {
  308. ar->tmr.update_sci = update_sci;
  309. ar->tmr.timer = qemu_new_timer_ns(vm_clock, acpi_pm_tmr_timer, ar);
  310. }
  311. void acpi_pm_tmr_reset(ACPIREGS *ar)
  312. {
  313. ar->tmr.overflow_time = 0;
  314. qemu_del_timer(ar->tmr.timer);
  315. }
  316. /* ACPI PM1aCNT */
  317. void acpi_pm1_cnt_init(ACPIREGS *ar)
  318. {
  319. ar->wakeup.notify = acpi_notify_wakeup;
  320. qemu_register_wakeup_notifier(&ar->wakeup);
  321. }
  322. void acpi_pm1_cnt_write(ACPIREGS *ar, uint16_t val, char s4)
  323. {
  324. ar->pm1.cnt.cnt = val & ~(ACPI_BITMASK_SLEEP_ENABLE);
  325. if (val & ACPI_BITMASK_SLEEP_ENABLE) {
  326. /* change suspend type */
  327. uint16_t sus_typ = (val >> 10) & 7;
  328. switch(sus_typ) {
  329. case 0: /* soft power off */
  330. qemu_system_shutdown_request();
  331. break;
  332. case 1:
  333. qemu_system_suspend_request();
  334. break;
  335. default:
  336. if (sus_typ == s4) { /* S4 request */
  337. monitor_protocol_event(QEVENT_SUSPEND_DISK, NULL);
  338. qemu_system_shutdown_request();
  339. }
  340. break;
  341. }
  342. }
  343. }
  344. void acpi_pm1_cnt_update(ACPIREGS *ar,
  345. bool sci_enable, bool sci_disable)
  346. {
  347. /* ACPI specs 3.0, 4.7.2.5 */
  348. if (sci_enable) {
  349. ar->pm1.cnt.cnt |= ACPI_BITMASK_SCI_ENABLE;
  350. } else if (sci_disable) {
  351. ar->pm1.cnt.cnt &= ~ACPI_BITMASK_SCI_ENABLE;
  352. }
  353. }
  354. void acpi_pm1_cnt_reset(ACPIREGS *ar)
  355. {
  356. ar->pm1.cnt.cnt = 0;
  357. }
  358. /* ACPI GPE */
  359. void acpi_gpe_init(ACPIREGS *ar, uint8_t len)
  360. {
  361. ar->gpe.len = len;
  362. ar->gpe.sts = g_malloc0(len / 2);
  363. ar->gpe.en = g_malloc0(len / 2);
  364. }
  365. void acpi_gpe_blk(ACPIREGS *ar, uint32_t blk)
  366. {
  367. ar->gpe.blk = blk;
  368. }
  369. void acpi_gpe_reset(ACPIREGS *ar)
  370. {
  371. memset(ar->gpe.sts, 0, ar->gpe.len / 2);
  372. memset(ar->gpe.en, 0, ar->gpe.len / 2);
  373. }
  374. static uint8_t *acpi_gpe_ioport_get_ptr(ACPIREGS *ar, uint32_t addr)
  375. {
  376. uint8_t *cur = NULL;
  377. if (addr < ar->gpe.len / 2) {
  378. cur = ar->gpe.sts + addr;
  379. } else if (addr < ar->gpe.len) {
  380. cur = ar->gpe.en + addr - ar->gpe.len / 2;
  381. } else {
  382. abort();
  383. }
  384. return cur;
  385. }
  386. void acpi_gpe_ioport_writeb(ACPIREGS *ar, uint32_t addr, uint32_t val)
  387. {
  388. uint8_t *cur;
  389. addr -= ar->gpe.blk;
  390. cur = acpi_gpe_ioport_get_ptr(ar, addr);
  391. if (addr < ar->gpe.len / 2) {
  392. /* GPE_STS */
  393. *cur = (*cur) & ~val;
  394. } else if (addr < ar->gpe.len) {
  395. /* GPE_EN */
  396. *cur = val;
  397. } else {
  398. abort();
  399. }
  400. }
  401. uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr)
  402. {
  403. uint8_t *cur;
  404. uint32_t val;
  405. addr -= ar->gpe.blk;
  406. cur = acpi_gpe_ioport_get_ptr(ar, addr);
  407. val = 0;
  408. if (cur != NULL) {
  409. val = *cur;
  410. }
  411. return val;
  412. }