tod-tcg.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * TOD (Time Of Day) clock - TCG implementation
  3. *
  4. * Copyright 2018 Red Hat, Inc.
  5. * Author(s): David Hildenbrand <david@redhat.com>
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  8. * See the COPYING file in the top-level directory.
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qapi/error.h"
  12. #include "hw/s390x/tod.h"
  13. #include "qemu/timer.h"
  14. #include "qemu/cutils.h"
  15. #include "qemu/module.h"
  16. #include "cpu.h"
  17. #include "tcg/tcg_s390x.h"
  18. #include "system/rtc.h"
  19. static void qemu_s390_tod_get(const S390TODState *td, S390TOD *tod,
  20. Error **errp)
  21. {
  22. *tod = td->base;
  23. tod->low += time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  24. if (tod->low < td->base.low) {
  25. tod->high++;
  26. }
  27. }
  28. static void qemu_s390_tod_set(S390TODState *td, const S390TOD *tod,
  29. Error **errp)
  30. {
  31. CPUState *cpu;
  32. td->base = *tod;
  33. td->base.low -= time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  34. if (td->base.low > tod->low) {
  35. td->base.high--;
  36. }
  37. /*
  38. * The TOD has been changed and we have to recalculate the CKC values
  39. * for all CPUs. We do this asynchronously, as "SET CLOCK should be
  40. * issued only while all other activity on all CPUs .. has been
  41. * suspended".
  42. */
  43. CPU_FOREACH(cpu) {
  44. async_run_on_cpu(cpu, tcg_s390_tod_updated, RUN_ON_CPU_NULL);
  45. }
  46. }
  47. static void qemu_s390_tod_class_init(ObjectClass *oc, void *data)
  48. {
  49. S390TODClass *tdc = S390_TOD_CLASS(oc);
  50. tdc->get = qemu_s390_tod_get;
  51. tdc->set = qemu_s390_tod_set;
  52. }
  53. static void qemu_s390_tod_init(Object *obj)
  54. {
  55. S390TODState *td = S390_TOD(obj);
  56. struct tm tm;
  57. qemu_get_timedate(&tm, 0);
  58. td->base.high = 0;
  59. td->base.low = TOD_UNIX_EPOCH + (time2tod(mktimegm(&tm)) * 1000000000ULL);
  60. if (td->base.low < TOD_UNIX_EPOCH) {
  61. td->base.high += 1;
  62. }
  63. }
  64. static const TypeInfo qemu_s390_tod_info = {
  65. .name = TYPE_QEMU_S390_TOD,
  66. .parent = TYPE_S390_TOD,
  67. .instance_size = sizeof(S390TODState),
  68. .instance_init = qemu_s390_tod_init,
  69. .class_init = qemu_s390_tod_class_init,
  70. .class_size = sizeof(S390TODClass),
  71. };
  72. static void register_types(void)
  73. {
  74. type_register_static(&qemu_s390_tod_info);
  75. }
  76. type_init(register_types);