spapr_rtc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/timer.h"
  29. #include "system/system.h"
  30. #include "system/rtc.h"
  31. #include "hw/ppc/spapr.h"
  32. #include "migration/vmstate.h"
  33. #include "qapi/error.h"
  34. #include "qapi/qapi-events-misc.h"
  35. #include "qemu/cutils.h"
  36. #include "qemu/module.h"
  37. void spapr_rtc_read(SpaprRtcState *rtc, struct tm *tm, uint32_t *ns)
  38. {
  39. int64_t host_ns = qemu_clock_get_ns(rtc_clock);
  40. int64_t guest_ns;
  41. time_t guest_s;
  42. assert(rtc);
  43. guest_ns = host_ns + rtc->ns_offset;
  44. guest_s = guest_ns / NANOSECONDS_PER_SECOND;
  45. if (tm) {
  46. gmtime_r(&guest_s, tm);
  47. }
  48. if (ns) {
  49. *ns = guest_ns;
  50. }
  51. }
  52. int spapr_rtc_import_offset(SpaprRtcState *rtc, int64_t legacy_offset)
  53. {
  54. if (!rtc) {
  55. return -ENODEV;
  56. }
  57. rtc->ns_offset = legacy_offset * NANOSECONDS_PER_SECOND;
  58. return 0;
  59. }
  60. static void rtas_get_time_of_day(PowerPCCPU *cpu, SpaprMachineState *spapr,
  61. uint32_t token, uint32_t nargs,
  62. target_ulong args,
  63. uint32_t nret, target_ulong rets)
  64. {
  65. struct tm tm;
  66. uint32_t ns;
  67. if ((nargs != 0) || (nret != 8)) {
  68. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  69. return;
  70. }
  71. spapr_rtc_read(&spapr->rtc, &tm, &ns);
  72. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  73. rtas_st(rets, 1, tm.tm_year + 1900);
  74. rtas_st(rets, 2, tm.tm_mon + 1);
  75. rtas_st(rets, 3, tm.tm_mday);
  76. rtas_st(rets, 4, tm.tm_hour);
  77. rtas_st(rets, 5, tm.tm_min);
  78. rtas_st(rets, 6, tm.tm_sec);
  79. rtas_st(rets, 7, ns);
  80. }
  81. static void rtas_set_time_of_day(PowerPCCPU *cpu, SpaprMachineState *spapr,
  82. uint32_t token, uint32_t nargs,
  83. target_ulong args,
  84. uint32_t nret, target_ulong rets)
  85. {
  86. SpaprRtcState *rtc = &spapr->rtc;
  87. g_autofree const char *qom_path = NULL;
  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. qom_path = object_get_canonical_path(OBJECT(rtc));
  108. qapi_event_send_rtc_change(qemu_timedate_diff(&tm), qom_path);
  109. host_ns = qemu_clock_get_ns(rtc_clock);
  110. rtc->ns_offset = (new_s * NANOSECONDS_PER_SECOND) - host_ns;
  111. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  112. }
  113. static void spapr_rtc_qom_date(Object *obj, struct tm *current_tm, Error **errp)
  114. {
  115. spapr_rtc_read(SPAPR_RTC(obj), current_tm, NULL);
  116. }
  117. static void spapr_rtc_realize(DeviceState *dev, Error **errp)
  118. {
  119. SpaprRtcState *rtc = SPAPR_RTC(dev);
  120. struct tm tm;
  121. time_t host_s;
  122. int64_t rtc_ns;
  123. /* Initialize the RTAS RTC from host time */
  124. qemu_get_timedate(&tm, 0);
  125. host_s = mktimegm(&tm);
  126. rtc_ns = qemu_clock_get_ns(rtc_clock);
  127. rtc->ns_offset = host_s * NANOSECONDS_PER_SECOND - rtc_ns;
  128. object_property_add_tm(OBJECT(rtc), "date", spapr_rtc_qom_date);
  129. }
  130. static const VMStateDescription vmstate_spapr_rtc = {
  131. .name = "spapr/rtc",
  132. .version_id = 1,
  133. .minimum_version_id = 1,
  134. .fields = (const VMStateField[]) {
  135. VMSTATE_INT64(ns_offset, SpaprRtcState),
  136. VMSTATE_END_OF_LIST()
  137. },
  138. };
  139. static void spapr_rtc_class_init(ObjectClass *oc, void *data)
  140. {
  141. DeviceClass *dc = DEVICE_CLASS(oc);
  142. dc->realize = spapr_rtc_realize;
  143. dc->vmsd = &vmstate_spapr_rtc;
  144. /* Reason: This is an internal device only for handling the hypercalls */
  145. dc->user_creatable = false;
  146. spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day",
  147. rtas_get_time_of_day);
  148. spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day",
  149. rtas_set_time_of_day);
  150. }
  151. static const TypeInfo spapr_rtc_info = {
  152. .name = TYPE_SPAPR_RTC,
  153. .parent = TYPE_DEVICE,
  154. .instance_size = sizeof(SpaprRtcState),
  155. .class_init = spapr_rtc_class_init,
  156. };
  157. static void spapr_rtc_register_types(void)
  158. {
  159. type_register_static(&spapr_rtc_info);
  160. }
  161. type_init(spapr_rtc_register_types)