goldfish_rtc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Goldfish virtual platform RTC
  3. *
  4. * Copyright (C) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * For more details on Google Goldfish virtual platform refer:
  7. * https://android.googlesource.com/platform/external/qemu/+/refs/heads/emu-2.0-release/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2 or later, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "qemu/osdep.h"
  22. #include "qemu-common.h"
  23. #include "hw/rtc/goldfish_rtc.h"
  24. #include "migration/vmstate.h"
  25. #include "hw/irq.h"
  26. #include "hw/qdev-properties.h"
  27. #include "hw/sysbus.h"
  28. #include "qemu/bitops.h"
  29. #include "qemu/timer.h"
  30. #include "sysemu/sysemu.h"
  31. #include "qemu/cutils.h"
  32. #include "qemu/log.h"
  33. #include "trace.h"
  34. #define RTC_TIME_LOW 0x00
  35. #define RTC_TIME_HIGH 0x04
  36. #define RTC_ALARM_LOW 0x08
  37. #define RTC_ALARM_HIGH 0x0c
  38. #define RTC_IRQ_ENABLED 0x10
  39. #define RTC_CLEAR_ALARM 0x14
  40. #define RTC_ALARM_STATUS 0x18
  41. #define RTC_CLEAR_INTERRUPT 0x1c
  42. static void goldfish_rtc_update(GoldfishRTCState *s)
  43. {
  44. qemu_set_irq(s->irq, (s->irq_pending & s->irq_enabled) ? 1 : 0);
  45. }
  46. static void goldfish_rtc_interrupt(void *opaque)
  47. {
  48. GoldfishRTCState *s = (GoldfishRTCState *)opaque;
  49. s->alarm_running = 0;
  50. s->irq_pending = 1;
  51. goldfish_rtc_update(s);
  52. }
  53. static uint64_t goldfish_rtc_get_count(GoldfishRTCState *s)
  54. {
  55. return s->tick_offset + (uint64_t)qemu_clock_get_ns(rtc_clock);
  56. }
  57. static void goldfish_rtc_clear_alarm(GoldfishRTCState *s)
  58. {
  59. timer_del(s->timer);
  60. s->alarm_running = 0;
  61. }
  62. static void goldfish_rtc_set_alarm(GoldfishRTCState *s)
  63. {
  64. uint64_t ticks = goldfish_rtc_get_count(s);
  65. uint64_t event = s->alarm_next;
  66. if (event <= ticks) {
  67. goldfish_rtc_clear_alarm(s);
  68. goldfish_rtc_interrupt(s);
  69. } else {
  70. /*
  71. * We should be setting timer expiry to:
  72. * qemu_clock_get_ns(rtc_clock) + (event - ticks)
  73. * but this is equivalent to:
  74. * event - s->tick_offset
  75. */
  76. timer_mod(s->timer, event - s->tick_offset);
  77. s->alarm_running = 1;
  78. }
  79. }
  80. static uint64_t goldfish_rtc_read(void *opaque, hwaddr offset,
  81. unsigned size)
  82. {
  83. GoldfishRTCState *s = opaque;
  84. uint64_t r = 0;
  85. /*
  86. * From the documentation linked at the top of the file:
  87. *
  88. * To read the value, the kernel must perform an IO_READ(TIME_LOW), which
  89. * returns an unsigned 32-bit value, before an IO_READ(TIME_HIGH), which
  90. * returns a signed 32-bit value, corresponding to the higher half of the
  91. * full value.
  92. */
  93. switch (offset) {
  94. case RTC_TIME_LOW:
  95. r = goldfish_rtc_get_count(s);
  96. s->time_high = r >> 32;
  97. r &= 0xffffffff;
  98. break;
  99. case RTC_TIME_HIGH:
  100. r = s->time_high;
  101. break;
  102. case RTC_ALARM_LOW:
  103. r = s->alarm_next & 0xffffffff;
  104. break;
  105. case RTC_ALARM_HIGH:
  106. r = s->alarm_next >> 32;
  107. break;
  108. case RTC_IRQ_ENABLED:
  109. r = s->irq_enabled;
  110. break;
  111. case RTC_ALARM_STATUS:
  112. r = s->alarm_running;
  113. break;
  114. default:
  115. qemu_log_mask(LOG_GUEST_ERROR,
  116. "%s: offset 0x%x is UNIMP.\n", __func__, (uint32_t)offset);
  117. break;
  118. }
  119. trace_goldfish_rtc_read(offset, r);
  120. return r;
  121. }
  122. static void goldfish_rtc_write(void *opaque, hwaddr offset,
  123. uint64_t value, unsigned size)
  124. {
  125. GoldfishRTCState *s = opaque;
  126. uint64_t current_tick, new_tick;
  127. switch (offset) {
  128. case RTC_TIME_LOW:
  129. current_tick = goldfish_rtc_get_count(s);
  130. new_tick = deposit64(current_tick, 0, 32, value);
  131. s->tick_offset += new_tick - current_tick;
  132. break;
  133. case RTC_TIME_HIGH:
  134. current_tick = goldfish_rtc_get_count(s);
  135. new_tick = deposit64(current_tick, 32, 32, value);
  136. s->tick_offset += new_tick - current_tick;
  137. break;
  138. case RTC_ALARM_LOW:
  139. s->alarm_next = deposit64(s->alarm_next, 0, 32, value);
  140. goldfish_rtc_set_alarm(s);
  141. break;
  142. case RTC_ALARM_HIGH:
  143. s->alarm_next = deposit64(s->alarm_next, 32, 32, value);
  144. break;
  145. case RTC_IRQ_ENABLED:
  146. s->irq_enabled = (uint32_t)(value & 0x1);
  147. goldfish_rtc_update(s);
  148. break;
  149. case RTC_CLEAR_ALARM:
  150. goldfish_rtc_clear_alarm(s);
  151. break;
  152. case RTC_CLEAR_INTERRUPT:
  153. s->irq_pending = 0;
  154. goldfish_rtc_update(s);
  155. break;
  156. default:
  157. qemu_log_mask(LOG_GUEST_ERROR,
  158. "%s: offset 0x%x is UNIMP.\n", __func__, (uint32_t)offset);
  159. break;
  160. }
  161. trace_goldfish_rtc_write(offset, value);
  162. }
  163. static int goldfish_rtc_pre_save(void *opaque)
  164. {
  165. uint64_t delta;
  166. GoldfishRTCState *s = opaque;
  167. /*
  168. * We want to migrate this offset, which sounds straightforward.
  169. * Unfortunately, we cannot directly pass tick_offset because
  170. * rtc_clock on destination Host might not be same source Host.
  171. *
  172. * To tackle, this we pass tick_offset relative to vm_clock from
  173. * source Host and make it relative to rtc_clock at destination Host.
  174. */
  175. delta = qemu_clock_get_ns(rtc_clock) -
  176. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  177. s->tick_offset_vmstate = s->tick_offset + delta;
  178. return 0;
  179. }
  180. static int goldfish_rtc_post_load(void *opaque, int version_id)
  181. {
  182. uint64_t delta;
  183. GoldfishRTCState *s = opaque;
  184. /*
  185. * We extract tick_offset from tick_offset_vmstate by doing
  186. * reverse math compared to pre_save() function.
  187. */
  188. delta = qemu_clock_get_ns(rtc_clock) -
  189. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  190. s->tick_offset = s->tick_offset_vmstate - delta;
  191. return 0;
  192. }
  193. static const MemoryRegionOps goldfish_rtc_ops = {
  194. .read = goldfish_rtc_read,
  195. .write = goldfish_rtc_write,
  196. .endianness = DEVICE_NATIVE_ENDIAN,
  197. .valid = {
  198. .min_access_size = 4,
  199. .max_access_size = 4
  200. }
  201. };
  202. static const VMStateDescription goldfish_rtc_vmstate = {
  203. .name = TYPE_GOLDFISH_RTC,
  204. .version_id = 2,
  205. .pre_save = goldfish_rtc_pre_save,
  206. .post_load = goldfish_rtc_post_load,
  207. .fields = (VMStateField[]) {
  208. VMSTATE_UINT64(tick_offset_vmstate, GoldfishRTCState),
  209. VMSTATE_UINT64(alarm_next, GoldfishRTCState),
  210. VMSTATE_UINT32(alarm_running, GoldfishRTCState),
  211. VMSTATE_UINT32(irq_pending, GoldfishRTCState),
  212. VMSTATE_UINT32(irq_enabled, GoldfishRTCState),
  213. VMSTATE_UINT32(time_high, GoldfishRTCState),
  214. VMSTATE_END_OF_LIST()
  215. }
  216. };
  217. static void goldfish_rtc_reset(DeviceState *dev)
  218. {
  219. GoldfishRTCState *s = GOLDFISH_RTC(dev);
  220. struct tm tm;
  221. timer_del(s->timer);
  222. qemu_get_timedate(&tm, 0);
  223. s->tick_offset = mktimegm(&tm);
  224. s->tick_offset *= NANOSECONDS_PER_SECOND;
  225. s->tick_offset -= qemu_clock_get_ns(rtc_clock);
  226. s->tick_offset_vmstate = 0;
  227. s->alarm_next = 0;
  228. s->alarm_running = 0;
  229. s->irq_pending = 0;
  230. s->irq_enabled = 0;
  231. }
  232. static void goldfish_rtc_realize(DeviceState *d, Error **errp)
  233. {
  234. SysBusDevice *dev = SYS_BUS_DEVICE(d);
  235. GoldfishRTCState *s = GOLDFISH_RTC(d);
  236. memory_region_init_io(&s->iomem, OBJECT(s), &goldfish_rtc_ops, s,
  237. "goldfish_rtc", 0x24);
  238. sysbus_init_mmio(dev, &s->iomem);
  239. sysbus_init_irq(dev, &s->irq);
  240. s->timer = timer_new_ns(rtc_clock, goldfish_rtc_interrupt, s);
  241. }
  242. static void goldfish_rtc_class_init(ObjectClass *klass, void *data)
  243. {
  244. DeviceClass *dc = DEVICE_CLASS(klass);
  245. dc->realize = goldfish_rtc_realize;
  246. dc->reset = goldfish_rtc_reset;
  247. dc->vmsd = &goldfish_rtc_vmstate;
  248. }
  249. static const TypeInfo goldfish_rtc_info = {
  250. .name = TYPE_GOLDFISH_RTC,
  251. .parent = TYPE_SYS_BUS_DEVICE,
  252. .instance_size = sizeof(GoldfishRTCState),
  253. .class_init = goldfish_rtc_class_init,
  254. };
  255. static void goldfish_rtc_register_types(void)
  256. {
  257. type_register_static(&goldfish_rtc_info);
  258. }
  259. type_init(goldfish_rtc_register_types)