core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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/hw.h"
  23. #include "hw/i386/pc.h"
  24. #include "hw/acpi/acpi.h"
  25. #include "monitor/monitor.h"
  26. #include "qemu/config-file.h"
  27. #include "qapi/opts-visitor.h"
  28. #include "qapi/dealloc-visitor.h"
  29. #include "qapi-visit.h"
  30. struct acpi_table_header {
  31. uint16_t _length; /* our length, not actual part of the hdr */
  32. /* allows easier parsing for fw_cfg clients */
  33. char sig[4]; /* ACPI signature (4 ASCII characters) */
  34. uint32_t length; /* Length of table, in bytes, including header */
  35. uint8_t revision; /* ACPI Specification minor version # */
  36. uint8_t checksum; /* To make sum of entire table == 0 */
  37. char oem_id[6]; /* OEM identification */
  38. char oem_table_id[8]; /* OEM table identification */
  39. uint32_t oem_revision; /* OEM revision number */
  40. char asl_compiler_id[4]; /* ASL compiler vendor ID */
  41. uint32_t asl_compiler_revision; /* ASL compiler revision number */
  42. } QEMU_PACKED;
  43. #define ACPI_TABLE_HDR_SIZE sizeof(struct acpi_table_header)
  44. #define ACPI_TABLE_PFX_SIZE sizeof(uint16_t) /* size of the extra prefix */
  45. static const char unsigned dfl_hdr[ACPI_TABLE_HDR_SIZE - ACPI_TABLE_PFX_SIZE] =
  46. "QEMU\0\0\0\0\1\0" /* sig (4), len(4), revno (1), csum (1) */
  47. "QEMUQEQEMUQEMU\1\0\0\0" /* OEM id (6), table (8), revno (4) */
  48. "QEMU\1\0\0\0" /* ASL compiler ID (4), version (4) */
  49. ;
  50. char unsigned *acpi_tables;
  51. size_t acpi_tables_len;
  52. static QemuOptsList qemu_acpi_opts = {
  53. .name = "acpi",
  54. .implied_opt_name = "data",
  55. .head = QTAILQ_HEAD_INITIALIZER(qemu_acpi_opts.head),
  56. .desc = { { 0 } } /* validated with OptsVisitor */
  57. };
  58. static void acpi_register_config(void)
  59. {
  60. qemu_add_opts(&qemu_acpi_opts);
  61. }
  62. machine_init(acpi_register_config);
  63. static int acpi_checksum(const uint8_t *data, int len)
  64. {
  65. int sum, i;
  66. sum = 0;
  67. for (i = 0; i < len; i++) {
  68. sum += data[i];
  69. }
  70. return (-sum) & 0xff;
  71. }
  72. /* Install a copy of the ACPI table specified in @blob.
  73. *
  74. * If @has_header is set, @blob starts with the System Description Table Header
  75. * structure. Otherwise, "dfl_hdr" is prepended. In any case, each header field
  76. * is optionally overwritten from @hdrs.
  77. *
  78. * It is valid to call this function with
  79. * (@blob == NULL && bloblen == 0 && !has_header).
  80. *
  81. * @hdrs->file and @hdrs->data are ignored.
  82. *
  83. * SIZE_MAX is considered "infinity" in this function.
  84. *
  85. * The number of tables that can be installed is not limited, but the 16-bit
  86. * counter at the beginning of "acpi_tables" wraps around after UINT16_MAX.
  87. */
  88. static void acpi_table_install(const char unsigned *blob, size_t bloblen,
  89. bool has_header,
  90. const struct AcpiTableOptions *hdrs,
  91. Error **errp)
  92. {
  93. size_t body_start;
  94. const char unsigned *hdr_src;
  95. size_t body_size, acpi_payload_size;
  96. struct acpi_table_header *ext_hdr;
  97. unsigned changed_fields;
  98. /* Calculate where the ACPI table body starts within the blob, plus where
  99. * to copy the ACPI table header from.
  100. */
  101. if (has_header) {
  102. /* _length | ACPI header in blob | blob body
  103. * ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
  104. * ACPI_TABLE_PFX_SIZE sizeof dfl_hdr body_size
  105. * == body_start
  106. *
  107. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  108. * acpi_payload_size == bloblen
  109. */
  110. body_start = sizeof dfl_hdr;
  111. if (bloblen < body_start) {
  112. error_setg(errp, "ACPI table claiming to have header is too "
  113. "short, available: %zu, expected: %zu", bloblen,
  114. body_start);
  115. return;
  116. }
  117. hdr_src = blob;
  118. } else {
  119. /* _length | ACPI header in template | blob body
  120. * ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^
  121. * ACPI_TABLE_PFX_SIZE sizeof dfl_hdr body_size
  122. * == bloblen
  123. *
  124. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  125. * acpi_payload_size
  126. */
  127. body_start = 0;
  128. hdr_src = dfl_hdr;
  129. }
  130. body_size = bloblen - body_start;
  131. acpi_payload_size = sizeof dfl_hdr + body_size;
  132. if (acpi_payload_size > UINT16_MAX) {
  133. error_setg(errp, "ACPI table too big, requested: %zu, max: %u",
  134. acpi_payload_size, (unsigned)UINT16_MAX);
  135. return;
  136. }
  137. /* We won't fail from here on. Initialize / extend the globals. */
  138. if (acpi_tables == NULL) {
  139. acpi_tables_len = sizeof(uint16_t);
  140. acpi_tables = g_malloc0(acpi_tables_len);
  141. }
  142. acpi_tables = g_realloc(acpi_tables, acpi_tables_len +
  143. ACPI_TABLE_PFX_SIZE +
  144. sizeof dfl_hdr + body_size);
  145. ext_hdr = (struct acpi_table_header *)(acpi_tables + acpi_tables_len);
  146. acpi_tables_len += ACPI_TABLE_PFX_SIZE;
  147. memcpy(acpi_tables + acpi_tables_len, hdr_src, sizeof dfl_hdr);
  148. acpi_tables_len += sizeof dfl_hdr;
  149. if (blob != NULL) {
  150. memcpy(acpi_tables + acpi_tables_len, blob + body_start, body_size);
  151. acpi_tables_len += body_size;
  152. }
  153. /* increase number of tables */
  154. stw_le_p(acpi_tables, lduw_le_p(acpi_tables) + 1u);
  155. /* Update the header fields. The strings need not be NUL-terminated. */
  156. changed_fields = 0;
  157. ext_hdr->_length = cpu_to_le16(acpi_payload_size);
  158. if (hdrs->has_sig) {
  159. strncpy(ext_hdr->sig, hdrs->sig, sizeof ext_hdr->sig);
  160. ++changed_fields;
  161. }
  162. if (has_header && le32_to_cpu(ext_hdr->length) != acpi_payload_size) {
  163. fprintf(stderr,
  164. "warning: ACPI table has wrong length, header says "
  165. "%" PRIu32 ", actual size %zu bytes\n",
  166. le32_to_cpu(ext_hdr->length), acpi_payload_size);
  167. }
  168. ext_hdr->length = cpu_to_le32(acpi_payload_size);
  169. if (hdrs->has_rev) {
  170. ext_hdr->revision = hdrs->rev;
  171. ++changed_fields;
  172. }
  173. ext_hdr->checksum = 0;
  174. if (hdrs->has_oem_id) {
  175. strncpy(ext_hdr->oem_id, hdrs->oem_id, sizeof ext_hdr->oem_id);
  176. ++changed_fields;
  177. }
  178. if (hdrs->has_oem_table_id) {
  179. strncpy(ext_hdr->oem_table_id, hdrs->oem_table_id,
  180. sizeof ext_hdr->oem_table_id);
  181. ++changed_fields;
  182. }
  183. if (hdrs->has_oem_rev) {
  184. ext_hdr->oem_revision = cpu_to_le32(hdrs->oem_rev);
  185. ++changed_fields;
  186. }
  187. if (hdrs->has_asl_compiler_id) {
  188. strncpy(ext_hdr->asl_compiler_id, hdrs->asl_compiler_id,
  189. sizeof ext_hdr->asl_compiler_id);
  190. ++changed_fields;
  191. }
  192. if (hdrs->has_asl_compiler_rev) {
  193. ext_hdr->asl_compiler_revision = cpu_to_le32(hdrs->asl_compiler_rev);
  194. ++changed_fields;
  195. }
  196. if (!has_header && changed_fields == 0) {
  197. fprintf(stderr, "warning: ACPI table: no headers are specified\n");
  198. }
  199. /* recalculate checksum */
  200. ext_hdr->checksum = acpi_checksum((const char unsigned *)ext_hdr +
  201. ACPI_TABLE_PFX_SIZE, acpi_payload_size);
  202. }
  203. void acpi_table_add(const QemuOpts *opts, Error **errp)
  204. {
  205. AcpiTableOptions *hdrs = NULL;
  206. Error *err = NULL;
  207. char **pathnames = NULL;
  208. char **cur;
  209. size_t bloblen = 0;
  210. char unsigned *blob = NULL;
  211. {
  212. OptsVisitor *ov;
  213. ov = opts_visitor_new(opts);
  214. visit_type_AcpiTableOptions(opts_get_visitor(ov), &hdrs, NULL, &err);
  215. opts_visitor_cleanup(ov);
  216. }
  217. if (err) {
  218. goto out;
  219. }
  220. if (hdrs->has_file == hdrs->has_data) {
  221. error_setg(&err, "'-acpitable' requires one of 'data' or 'file'");
  222. goto out;
  223. }
  224. pathnames = g_strsplit(hdrs->has_file ? hdrs->file : hdrs->data, ":", 0);
  225. if (pathnames == NULL || pathnames[0] == NULL) {
  226. error_setg(&err, "'-acpitable' requires at least one pathname");
  227. goto out;
  228. }
  229. /* now read in the data files, reallocating buffer as needed */
  230. for (cur = pathnames; *cur; ++cur) {
  231. int fd = open(*cur, O_RDONLY | O_BINARY);
  232. if (fd < 0) {
  233. error_setg(&err, "can't open file %s: %s", *cur, strerror(errno));
  234. goto out;
  235. }
  236. for (;;) {
  237. char unsigned data[8192];
  238. ssize_t r;
  239. r = read(fd, data, sizeof data);
  240. if (r == 0) {
  241. break;
  242. } else if (r > 0) {
  243. blob = g_realloc(blob, bloblen + r);
  244. memcpy(blob + bloblen, data, r);
  245. bloblen += r;
  246. } else if (errno != EINTR) {
  247. error_setg(&err, "can't read file %s: %s",
  248. *cur, strerror(errno));
  249. close(fd);
  250. goto out;
  251. }
  252. }
  253. close(fd);
  254. }
  255. acpi_table_install(blob, bloblen, hdrs->has_file, hdrs, &err);
  256. out:
  257. g_free(blob);
  258. g_strfreev(pathnames);
  259. if (hdrs != NULL) {
  260. QapiDeallocVisitor *dv;
  261. dv = qapi_dealloc_visitor_new();
  262. visit_type_AcpiTableOptions(qapi_dealloc_get_visitor(dv), &hdrs, NULL,
  263. NULL);
  264. qapi_dealloc_visitor_cleanup(dv);
  265. }
  266. error_propagate(errp, err);
  267. }
  268. static bool acpi_table_builtin = false;
  269. void acpi_table_add_builtin(const QemuOpts *opts, Error **errp)
  270. {
  271. acpi_table_builtin = true;
  272. acpi_table_add(opts, errp);
  273. }
  274. unsigned acpi_table_len(void *current)
  275. {
  276. struct acpi_table_header *hdr = current - sizeof(hdr->_length);
  277. return hdr->_length;
  278. }
  279. static
  280. void *acpi_table_hdr(void *h)
  281. {
  282. struct acpi_table_header *hdr = h;
  283. return &hdr->sig;
  284. }
  285. uint8_t *acpi_table_first(void)
  286. {
  287. if (acpi_table_builtin || !acpi_tables) {
  288. return NULL;
  289. }
  290. return acpi_table_hdr(acpi_tables + ACPI_TABLE_PFX_SIZE);
  291. }
  292. uint8_t *acpi_table_next(uint8_t *current)
  293. {
  294. uint8_t *next = current + acpi_table_len(current);
  295. if (next - acpi_tables >= acpi_tables_len) {
  296. return NULL;
  297. } else {
  298. return acpi_table_hdr(next);
  299. }
  300. }
  301. static void acpi_notify_wakeup(Notifier *notifier, void *data)
  302. {
  303. ACPIREGS *ar = container_of(notifier, ACPIREGS, wakeup);
  304. WakeupReason *reason = data;
  305. switch (*reason) {
  306. case QEMU_WAKEUP_REASON_RTC:
  307. ar->pm1.evt.sts |=
  308. (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_RT_CLOCK_STATUS);
  309. break;
  310. case QEMU_WAKEUP_REASON_PMTIMER:
  311. ar->pm1.evt.sts |=
  312. (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_TIMER_STATUS);
  313. break;
  314. case QEMU_WAKEUP_REASON_OTHER:
  315. /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
  316. Pretend that resume was caused by power button */
  317. ar->pm1.evt.sts |=
  318. (ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_POWER_BUTTON_STATUS);
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. /* ACPI PM1a EVT */
  325. uint16_t acpi_pm1_evt_get_sts(ACPIREGS *ar)
  326. {
  327. int64_t d = acpi_pm_tmr_get_clock();
  328. if (d >= ar->tmr.overflow_time) {
  329. ar->pm1.evt.sts |= ACPI_BITMASK_TIMER_STATUS;
  330. }
  331. return ar->pm1.evt.sts;
  332. }
  333. static void acpi_pm1_evt_write_sts(ACPIREGS *ar, uint16_t val)
  334. {
  335. uint16_t pm1_sts = acpi_pm1_evt_get_sts(ar);
  336. if (pm1_sts & val & ACPI_BITMASK_TIMER_STATUS) {
  337. /* if TMRSTS is reset, then compute the new overflow time */
  338. acpi_pm_tmr_calc_overflow_time(ar);
  339. }
  340. ar->pm1.evt.sts &= ~val;
  341. }
  342. static void acpi_pm1_evt_write_en(ACPIREGS *ar, uint16_t val)
  343. {
  344. ar->pm1.evt.en = val;
  345. qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_RTC,
  346. val & ACPI_BITMASK_RT_CLOCK_ENABLE);
  347. qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_PMTIMER,
  348. val & ACPI_BITMASK_TIMER_ENABLE);
  349. }
  350. void acpi_pm1_evt_power_down(ACPIREGS *ar)
  351. {
  352. if (ar->pm1.evt.en & ACPI_BITMASK_POWER_BUTTON_ENABLE) {
  353. ar->pm1.evt.sts |= ACPI_BITMASK_POWER_BUTTON_STATUS;
  354. ar->tmr.update_sci(ar);
  355. }
  356. }
  357. void acpi_pm1_evt_reset(ACPIREGS *ar)
  358. {
  359. ar->pm1.evt.sts = 0;
  360. ar->pm1.evt.en = 0;
  361. qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_RTC, 0);
  362. qemu_system_wakeup_enable(QEMU_WAKEUP_REASON_PMTIMER, 0);
  363. }
  364. static uint64_t acpi_pm_evt_read(void *opaque, hwaddr addr, unsigned width)
  365. {
  366. ACPIREGS *ar = opaque;
  367. switch (addr) {
  368. case 0:
  369. return acpi_pm1_evt_get_sts(ar);
  370. case 2:
  371. return ar->pm1.evt.en;
  372. default:
  373. return 0;
  374. }
  375. }
  376. static void acpi_pm_evt_write(void *opaque, hwaddr addr, uint64_t val,
  377. unsigned width)
  378. {
  379. ACPIREGS *ar = opaque;
  380. switch (addr) {
  381. case 0:
  382. acpi_pm1_evt_write_sts(ar, val);
  383. ar->pm1.evt.update_sci(ar);
  384. break;
  385. case 2:
  386. acpi_pm1_evt_write_en(ar, val);
  387. ar->pm1.evt.update_sci(ar);
  388. break;
  389. }
  390. }
  391. static const MemoryRegionOps acpi_pm_evt_ops = {
  392. .read = acpi_pm_evt_read,
  393. .write = acpi_pm_evt_write,
  394. .valid.min_access_size = 2,
  395. .valid.max_access_size = 2,
  396. .endianness = DEVICE_LITTLE_ENDIAN,
  397. };
  398. void acpi_pm1_evt_init(ACPIREGS *ar, acpi_update_sci_fn update_sci,
  399. MemoryRegion *parent)
  400. {
  401. ar->pm1.evt.update_sci = update_sci;
  402. memory_region_init_io(&ar->pm1.evt.io, memory_region_owner(parent),
  403. &acpi_pm_evt_ops, ar, "acpi-evt", 4);
  404. memory_region_add_subregion(parent, 0, &ar->pm1.evt.io);
  405. }
  406. /* ACPI PM_TMR */
  407. void acpi_pm_tmr_update(ACPIREGS *ar, bool enable)
  408. {
  409. int64_t expire_time;
  410. /* schedule a timer interruption if needed */
  411. if (enable) {
  412. expire_time = muldiv64(ar->tmr.overflow_time, get_ticks_per_sec(),
  413. PM_TIMER_FREQUENCY);
  414. timer_mod(ar->tmr.timer, expire_time);
  415. } else {
  416. timer_del(ar->tmr.timer);
  417. }
  418. }
  419. void acpi_pm_tmr_calc_overflow_time(ACPIREGS *ar)
  420. {
  421. int64_t d = acpi_pm_tmr_get_clock();
  422. ar->tmr.overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
  423. }
  424. static uint32_t acpi_pm_tmr_get(ACPIREGS *ar)
  425. {
  426. uint32_t d = acpi_pm_tmr_get_clock();
  427. return d & 0xffffff;
  428. }
  429. static void acpi_pm_tmr_timer(void *opaque)
  430. {
  431. ACPIREGS *ar = opaque;
  432. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_PMTIMER);
  433. ar->tmr.update_sci(ar);
  434. }
  435. static uint64_t acpi_pm_tmr_read(void *opaque, hwaddr addr, unsigned width)
  436. {
  437. return acpi_pm_tmr_get(opaque);
  438. }
  439. static void acpi_pm_tmr_write(void *opaque, hwaddr addr, uint64_t val,
  440. unsigned width)
  441. {
  442. /* nothing */
  443. }
  444. static const MemoryRegionOps acpi_pm_tmr_ops = {
  445. .read = acpi_pm_tmr_read,
  446. .write = acpi_pm_tmr_write,
  447. .valid.min_access_size = 4,
  448. .valid.max_access_size = 4,
  449. .endianness = DEVICE_LITTLE_ENDIAN,
  450. };
  451. void acpi_pm_tmr_init(ACPIREGS *ar, acpi_update_sci_fn update_sci,
  452. MemoryRegion *parent)
  453. {
  454. ar->tmr.update_sci = update_sci;
  455. ar->tmr.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, acpi_pm_tmr_timer, ar);
  456. memory_region_init_io(&ar->tmr.io, memory_region_owner(parent),
  457. &acpi_pm_tmr_ops, ar, "acpi-tmr", 4);
  458. memory_region_add_subregion(parent, 8, &ar->tmr.io);
  459. }
  460. void acpi_pm_tmr_reset(ACPIREGS *ar)
  461. {
  462. ar->tmr.overflow_time = 0;
  463. timer_del(ar->tmr.timer);
  464. }
  465. /* ACPI PM1aCNT */
  466. static void acpi_pm1_cnt_write(ACPIREGS *ar, uint16_t val)
  467. {
  468. ar->pm1.cnt.cnt = val & ~(ACPI_BITMASK_SLEEP_ENABLE);
  469. if (val & ACPI_BITMASK_SLEEP_ENABLE) {
  470. /* change suspend type */
  471. uint16_t sus_typ = (val >> 10) & 7;
  472. switch(sus_typ) {
  473. case 0: /* soft power off */
  474. qemu_system_shutdown_request();
  475. break;
  476. case 1:
  477. qemu_system_suspend_request();
  478. break;
  479. default:
  480. if (sus_typ == ar->pm1.cnt.s4_val) { /* S4 request */
  481. monitor_protocol_event(QEVENT_SUSPEND_DISK, NULL);
  482. qemu_system_shutdown_request();
  483. }
  484. break;
  485. }
  486. }
  487. }
  488. void acpi_pm1_cnt_update(ACPIREGS *ar,
  489. bool sci_enable, bool sci_disable)
  490. {
  491. /* ACPI specs 3.0, 4.7.2.5 */
  492. if (sci_enable) {
  493. ar->pm1.cnt.cnt |= ACPI_BITMASK_SCI_ENABLE;
  494. } else if (sci_disable) {
  495. ar->pm1.cnt.cnt &= ~ACPI_BITMASK_SCI_ENABLE;
  496. }
  497. }
  498. static uint64_t acpi_pm_cnt_read(void *opaque, hwaddr addr, unsigned width)
  499. {
  500. ACPIREGS *ar = opaque;
  501. return ar->pm1.cnt.cnt;
  502. }
  503. static void acpi_pm_cnt_write(void *opaque, hwaddr addr, uint64_t val,
  504. unsigned width)
  505. {
  506. acpi_pm1_cnt_write(opaque, val);
  507. }
  508. static const MemoryRegionOps acpi_pm_cnt_ops = {
  509. .read = acpi_pm_cnt_read,
  510. .write = acpi_pm_cnt_write,
  511. .valid.min_access_size = 2,
  512. .valid.max_access_size = 2,
  513. .endianness = DEVICE_LITTLE_ENDIAN,
  514. };
  515. void acpi_pm1_cnt_init(ACPIREGS *ar, MemoryRegion *parent, uint8_t s4_val)
  516. {
  517. ar->pm1.cnt.s4_val = s4_val;
  518. ar->wakeup.notify = acpi_notify_wakeup;
  519. qemu_register_wakeup_notifier(&ar->wakeup);
  520. memory_region_init_io(&ar->pm1.cnt.io, memory_region_owner(parent),
  521. &acpi_pm_cnt_ops, ar, "acpi-cnt", 2);
  522. memory_region_add_subregion(parent, 4, &ar->pm1.cnt.io);
  523. }
  524. void acpi_pm1_cnt_reset(ACPIREGS *ar)
  525. {
  526. ar->pm1.cnt.cnt = 0;
  527. }
  528. /* ACPI GPE */
  529. void acpi_gpe_init(ACPIREGS *ar, uint8_t len)
  530. {
  531. ar->gpe.len = len;
  532. ar->gpe.sts = g_malloc0(len / 2);
  533. ar->gpe.en = g_malloc0(len / 2);
  534. }
  535. void acpi_gpe_reset(ACPIREGS *ar)
  536. {
  537. memset(ar->gpe.sts, 0, ar->gpe.len / 2);
  538. memset(ar->gpe.en, 0, ar->gpe.len / 2);
  539. }
  540. static uint8_t *acpi_gpe_ioport_get_ptr(ACPIREGS *ar, uint32_t addr)
  541. {
  542. uint8_t *cur = NULL;
  543. if (addr < ar->gpe.len / 2) {
  544. cur = ar->gpe.sts + addr;
  545. } else if (addr < ar->gpe.len) {
  546. cur = ar->gpe.en + addr - ar->gpe.len / 2;
  547. } else {
  548. abort();
  549. }
  550. return cur;
  551. }
  552. void acpi_gpe_ioport_writeb(ACPIREGS *ar, uint32_t addr, uint32_t val)
  553. {
  554. uint8_t *cur;
  555. cur = acpi_gpe_ioport_get_ptr(ar, addr);
  556. if (addr < ar->gpe.len / 2) {
  557. /* GPE_STS */
  558. *cur = (*cur) & ~val;
  559. } else if (addr < ar->gpe.len) {
  560. /* GPE_EN */
  561. *cur = val;
  562. } else {
  563. abort();
  564. }
  565. }
  566. uint32_t acpi_gpe_ioport_readb(ACPIREGS *ar, uint32_t addr)
  567. {
  568. uint8_t *cur;
  569. uint32_t val;
  570. cur = acpi_gpe_ioport_get_ptr(ar, addr);
  571. val = 0;
  572. if (cur != NULL) {
  573. val = *cur;
  574. }
  575. return val;
  576. }
  577. void acpi_update_sci(ACPIREGS *regs, qemu_irq irq)
  578. {
  579. int sci_level, pm1a_sts;
  580. pm1a_sts = acpi_pm1_evt_get_sts(regs);
  581. sci_level = ((pm1a_sts &
  582. regs->pm1.evt.en & ACPI_BITMASK_PM1_COMMON_ENABLED) != 0) ||
  583. ((regs->gpe.sts[0] & regs->gpe.en[0]) != 0);
  584. qemu_set_irq(irq, sci_level);
  585. /* schedule a timer interruption if needed */
  586. acpi_pm_tmr_update(regs,
  587. (regs->pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
  588. !(pm1a_sts & ACPI_BITMASK_TIMER_STATUS));
  589. }