core.c 21 KB

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