apm.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * QEMU PC APM controller Emulation
  3. * This is split out from acpi.c
  4. *
  5. * Copyright (c) 2006 Fabrice Bellard
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License version 2.1 as published by the Free Software Foundation.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  18. *
  19. * Contributions after 2012-01-13 are licensed under the terms of the
  20. * GNU GPL, version 2 or (at your option) any later version.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "hw/isa/apm.h"
  24. #include "hw/pci/pci.h"
  25. #include "migration/vmstate.h"
  26. #include "trace.h"
  27. /* fixed I/O location */
  28. #define APM_STS_IOPORT 0xb3
  29. static void apm_ioport_writeb(void *opaque, hwaddr addr, uint64_t val,
  30. unsigned size)
  31. {
  32. APMState *apm = opaque;
  33. addr &= 1;
  34. trace_apm_io_write(addr, val);
  35. if (addr == 0) {
  36. apm->apmc = val;
  37. if (apm->callback) {
  38. (apm->callback)(val, apm->arg);
  39. }
  40. } else {
  41. apm->apms = val;
  42. }
  43. }
  44. static uint64_t apm_ioport_readb(void *opaque, hwaddr addr, unsigned size)
  45. {
  46. APMState *apm = opaque;
  47. uint32_t val;
  48. addr &= 1;
  49. if (addr == 0) {
  50. val = apm->apmc;
  51. } else {
  52. val = apm->apms;
  53. }
  54. trace_apm_io_read(addr, val);
  55. return val;
  56. }
  57. const VMStateDescription vmstate_apm = {
  58. .name = "APM State",
  59. .version_id = 1,
  60. .minimum_version_id = 1,
  61. .fields = (const VMStateField[]) {
  62. VMSTATE_UINT8(apmc, APMState),
  63. VMSTATE_UINT8(apms, APMState),
  64. VMSTATE_END_OF_LIST()
  65. }
  66. };
  67. static const MemoryRegionOps apm_ops = {
  68. .read = apm_ioport_readb,
  69. .write = apm_ioport_writeb,
  70. .impl = {
  71. .min_access_size = 1,
  72. .max_access_size = 1,
  73. },
  74. };
  75. void apm_init(PCIDevice *dev, APMState *apm, apm_ctrl_changed_t callback,
  76. void *arg)
  77. {
  78. apm->callback = callback;
  79. apm->arg = arg;
  80. /* ioport 0xb2, 0xb3 */
  81. memory_region_init_io(&apm->io, OBJECT(dev), &apm_ops, apm, "apm-io", 2);
  82. memory_region_add_subregion(pci_address_space_io(dev), APM_CNT_IOPORT,
  83. &apm->io);
  84. }