2
0

acpi.c 15 KB

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