spapr_rtc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
  3. *
  4. * RTAS Real Time Clock
  5. *
  6. * Copyright (c) 2010-2011 David Gibson, IBM Corporation.
  7. * Copyright 2014 David Gibson, Red Hat.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "qemu/osdep.h"
  28. #include "qemu-common.h"
  29. #include "cpu.h"
  30. #include "qemu/timer.h"
  31. #include "sysemu/sysemu.h"
  32. #include "hw/ppc/spapr.h"
  33. #include "migration/vmstate.h"
  34. #include "qapi/error.h"
  35. #include "qapi/qapi-events-misc-target.h"
  36. #include "qemu/cutils.h"
  37. #include "qemu/module.h"
  38. void spapr_rtc_read(SpaprRtcState *rtc, struct tm *tm, uint32_t *ns)
  39. {
  40. int64_t host_ns = qemu_clock_get_ns(rtc_clock);
  41. int64_t guest_ns;
  42. time_t guest_s;
  43. assert(rtc);
  44. guest_ns = host_ns + rtc->ns_offset;
  45. guest_s = guest_ns / NANOSECONDS_PER_SECOND;
  46. if (tm) {
  47. gmtime_r(&guest_s, tm);
  48. }
  49. if (ns) {
  50. *ns = guest_ns;
  51. }
  52. }
  53. int spapr_rtc_import_offset(SpaprRtcState *rtc, int64_t legacy_offset)
  54. {
  55. if (!rtc) {
  56. return -ENODEV;
  57. }
  58. rtc->ns_offset = legacy_offset * NANOSECONDS_PER_SECOND;
  59. return 0;
  60. }
  61. static void rtas_get_time_of_day(PowerPCCPU *cpu, SpaprMachineState *spapr,
  62. uint32_t token, uint32_t nargs,
  63. target_ulong args,
  64. uint32_t nret, target_ulong rets)
  65. {
  66. struct tm tm;
  67. uint32_t ns;
  68. if ((nargs != 0) || (nret != 8)) {
  69. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  70. return;
  71. }
  72. spapr_rtc_read(&spapr->rtc, &tm, &ns);
  73. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  74. rtas_st(rets, 1, tm.tm_year + 1900);
  75. rtas_st(rets, 2, tm.tm_mon + 1);
  76. rtas_st(rets, 3, tm.tm_mday);
  77. rtas_st(rets, 4, tm.tm_hour);
  78. rtas_st(rets, 5, tm.tm_min);
  79. rtas_st(rets, 6, tm.tm_sec);
  80. rtas_st(rets, 7, ns);
  81. }
  82. static void rtas_set_time_of_day(PowerPCCPU *cpu, SpaprMachineState *spapr,
  83. uint32_t token, uint32_t nargs,
  84. target_ulong args,
  85. uint32_t nret, target_ulong rets)
  86. {
  87. SpaprRtcState *rtc = &spapr->rtc;
  88. struct tm tm;
  89. time_t new_s;
  90. int64_t host_ns;
  91. if ((nargs != 7) || (nret != 1)) {
  92. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  93. return;
  94. }
  95. tm.tm_year = rtas_ld(args, 0) - 1900;
  96. tm.tm_mon = rtas_ld(args, 1) - 1;
  97. tm.tm_mday = rtas_ld(args, 2);
  98. tm.tm_hour = rtas_ld(args, 3);
  99. tm.tm_min = rtas_ld(args, 4);
  100. tm.tm_sec = rtas_ld(args, 5);
  101. new_s = mktimegm(&tm);
  102. if (new_s == -1) {
  103. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  104. return;
  105. }
  106. /* Generate a monitor event for the change */
  107. qapi_event_send_rtc_change(qemu_timedate_diff(&tm));
  108. host_ns = qemu_clock_get_ns(rtc_clock);
  109. rtc->ns_offset = (new_s * NANOSECONDS_PER_SECOND) - host_ns;
  110. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  111. }
  112. static void spapr_rtc_qom_date(Object *obj, struct tm *current_tm, Error **errp)
  113. {
  114. spapr_rtc_read(SPAPR_RTC(obj), current_tm, NULL);
  115. }
  116. static void spapr_rtc_realize(DeviceState *dev, Error **errp)
  117. {
  118. SpaprRtcState *rtc = SPAPR_RTC(dev);
  119. struct tm tm;
  120. time_t host_s;
  121. int64_t rtc_ns;
  122. /* Initialize the RTAS RTC from host time */
  123. qemu_get_timedate(&tm, 0);
  124. host_s = mktimegm(&tm);
  125. rtc_ns = qemu_clock_get_ns(rtc_clock);
  126. rtc->ns_offset = host_s * NANOSECONDS_PER_SECOND - rtc_ns;
  127. object_property_add_tm(OBJECT(rtc), "date", spapr_rtc_qom_date, NULL);
  128. }
  129. static const VMStateDescription vmstate_spapr_rtc = {
  130. .name = "spapr/rtc",
  131. .version_id = 1,
  132. .minimum_version_id = 1,
  133. .fields = (VMStateField[]) {
  134. VMSTATE_INT64(ns_offset, SpaprRtcState),
  135. VMSTATE_END_OF_LIST()
  136. },
  137. };
  138. static void spapr_rtc_class_init(ObjectClass *oc, void *data)
  139. {
  140. DeviceClass *dc = DEVICE_CLASS(oc);
  141. dc->realize = spapr_rtc_realize;
  142. dc->vmsd = &vmstate_spapr_rtc;
  143. /* Reason: This is an internal device only for handling the hypercalls */
  144. dc->user_creatable = false;
  145. spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day",
  146. rtas_get_time_of_day);
  147. spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day",
  148. rtas_set_time_of_day);
  149. }
  150. static const TypeInfo spapr_rtc_info = {
  151. .name = TYPE_SPAPR_RTC,
  152. .parent = TYPE_DEVICE,
  153. .instance_size = sizeof(SpaprRtcState),
  154. .class_init = spapr_rtc_class_init,
  155. };
  156. static void spapr_rtc_register_types(void)
  157. {
  158. type_register_static(&spapr_rtc_info);
  159. }
  160. type_init(spapr_rtc_register_types)