core.c 21 KB

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