ich9_timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * QEMU ICH9 Timer emulation
  3. *
  4. * Copyright (c) 2024 Dominic Prinz <git@dprinz.de>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "hw/core/cpu.h"
  11. #include "hw/pci/pci.h"
  12. #include "hw/southbridge/ich9.h"
  13. #include "qemu/timer.h"
  14. #include "hw/acpi/ich9_timer.h"
  15. void ich9_pm_update_swsmi_timer(ICH9LPCPMRegs *pm, bool enable)
  16. {
  17. uint16_t swsmi_rate_sel;
  18. int64_t expire_time;
  19. ICH9LPCState *lpc;
  20. if (enable) {
  21. lpc = container_of(pm, ICH9LPCState, pm);
  22. swsmi_rate_sel =
  23. (pci_get_word(lpc->d.config + ICH9_LPC_GEN_PMCON_3) & 0xc0) >> 6;
  24. if (swsmi_rate_sel == 0) {
  25. expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1500000LL;
  26. } else {
  27. expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  28. 8 * (1 << swsmi_rate_sel) * 1000000LL;
  29. }
  30. timer_mod(pm->swsmi_timer, expire_time);
  31. } else {
  32. timer_del(pm->swsmi_timer);
  33. }
  34. }
  35. static void ich9_pm_swsmi_timer_expired(void *opaque)
  36. {
  37. ICH9LPCPMRegs *pm = opaque;
  38. pm->smi_sts |= ICH9_PMIO_SMI_STS_SWSMI_STS;
  39. ich9_generate_smi();
  40. ich9_pm_update_swsmi_timer(pm, pm->smi_en & ICH9_PMIO_SMI_EN_SWSMI_EN);
  41. }
  42. void ich9_pm_swsmi_timer_init(ICH9LPCPMRegs *pm)
  43. {
  44. pm->smi_sts_wmask |= ICH9_PMIO_SMI_STS_SWSMI_STS;
  45. pm->swsmi_timer =
  46. timer_new_ns(QEMU_CLOCK_VIRTUAL, ich9_pm_swsmi_timer_expired, pm);
  47. }
  48. void ich9_pm_update_periodic_timer(ICH9LPCPMRegs *pm, bool enable)
  49. {
  50. uint16_t per_smi_sel;
  51. int64_t expire_time;
  52. ICH9LPCState *lpc;
  53. if (enable) {
  54. lpc = container_of(pm, ICH9LPCState, pm);
  55. per_smi_sel = pci_get_word(lpc->d.config + ICH9_LPC_GEN_PMCON_1) & 3;
  56. expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  57. 8 * (1 << (3 - per_smi_sel)) * NANOSECONDS_PER_SECOND;
  58. timer_mod(pm->periodic_timer, expire_time);
  59. } else {
  60. timer_del(pm->periodic_timer);
  61. }
  62. }
  63. static void ich9_pm_periodic_timer_expired(void *opaque)
  64. {
  65. ICH9LPCPMRegs *pm = opaque;
  66. pm->smi_sts = ICH9_PMIO_SMI_STS_PERIODIC_STS;
  67. ich9_generate_smi();
  68. ich9_pm_update_periodic_timer(pm,
  69. pm->smi_en & ICH9_PMIO_SMI_EN_PERIODIC_EN);
  70. }
  71. void ich9_pm_periodic_timer_init(ICH9LPCPMRegs *pm)
  72. {
  73. pm->smi_sts_wmask |= ICH9_PMIO_SMI_STS_PERIODIC_STS;
  74. pm->periodic_timer =
  75. timer_new_ns(QEMU_CLOCK_VIRTUAL, ich9_pm_periodic_timer_expired, pm);
  76. }