2
0

sun4v-rtc.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * QEMU sun4v Real Time Clock device
  3. *
  4. * The sun4v_rtc device (sun4v tod clock)
  5. *
  6. * Copyright (c) 2016 Artyom Tarasenko
  7. *
  8. * This code is licensed under the GNU GPL v3 or (at your option) any later
  9. * version.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "hw/sysbus.h"
  13. #include "qemu/module.h"
  14. #include "qemu/timer.h"
  15. #include "hw/rtc/sun4v-rtc.h"
  16. #include "trace.h"
  17. #define TYPE_SUN4V_RTC "sun4v_rtc"
  18. #define SUN4V_RTC(obj) OBJECT_CHECK(Sun4vRtc, (obj), TYPE_SUN4V_RTC)
  19. typedef struct Sun4vRtc {
  20. SysBusDevice parent_obj;
  21. MemoryRegion iomem;
  22. } Sun4vRtc;
  23. static uint64_t sun4v_rtc_read(void *opaque, hwaddr addr,
  24. unsigned size)
  25. {
  26. uint64_t val = get_clock_realtime() / NANOSECONDS_PER_SECOND;
  27. if (!(addr & 4ULL)) {
  28. /* accessing the high 32 bits */
  29. val >>= 32;
  30. }
  31. trace_sun4v_rtc_read(addr, val);
  32. return val;
  33. }
  34. static void sun4v_rtc_write(void *opaque, hwaddr addr,
  35. uint64_t val, unsigned size)
  36. {
  37. trace_sun4v_rtc_write(addr, val);
  38. }
  39. static const MemoryRegionOps sun4v_rtc_ops = {
  40. .read = sun4v_rtc_read,
  41. .write = sun4v_rtc_write,
  42. .endianness = DEVICE_NATIVE_ENDIAN,
  43. };
  44. void sun4v_rtc_init(hwaddr addr)
  45. {
  46. DeviceState *dev;
  47. SysBusDevice *s;
  48. dev = qdev_create(NULL, TYPE_SUN4V_RTC);
  49. s = SYS_BUS_DEVICE(dev);
  50. qdev_init_nofail(dev);
  51. sysbus_mmio_map(s, 0, addr);
  52. }
  53. static void sun4v_rtc_realize(DeviceState *dev, Error **errp)
  54. {
  55. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  56. Sun4vRtc *s = SUN4V_RTC(dev);
  57. memory_region_init_io(&s->iomem, OBJECT(s), &sun4v_rtc_ops, s,
  58. "sun4v-rtc", 0x08ULL);
  59. sysbus_init_mmio(sbd, &s->iomem);
  60. }
  61. static void sun4v_rtc_class_init(ObjectClass *klass, void *data)
  62. {
  63. DeviceClass *dc = DEVICE_CLASS(klass);
  64. dc->realize = sun4v_rtc_realize;
  65. }
  66. static const TypeInfo sun4v_rtc_info = {
  67. .name = TYPE_SUN4V_RTC,
  68. .parent = TYPE_SYS_BUS_DEVICE,
  69. .instance_size = sizeof(Sun4vRtc),
  70. .class_init = sun4v_rtc_class_init,
  71. };
  72. static void sun4v_rtc_register_types(void)
  73. {
  74. type_register_static(&sun4v_rtc_info);
  75. }
  76. type_init(sun4v_rtc_register_types)