2
0

goldfish_rtc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "hw/rtc/goldfish_rtc.h"
  23. #include "migration/vmstate.h"
  24. #include "hw/irq.h"
  25. #include "hw/qdev-properties.h"
  26. #include "hw/sysbus.h"
  27. #include "qemu/bitops.h"
  28. #include "qemu/timer.h"
  29. #include "sysemu/sysemu.h"
  30. #include "sysemu/rtc.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. goldfish_rtc_set_alarm(s);
  192. return 0;
  193. }
  194. static const MemoryRegionOps goldfish_rtc_ops[2] = {
  195. [false] = {
  196. .read = goldfish_rtc_read,
  197. .write = goldfish_rtc_write,
  198. .endianness = DEVICE_LITTLE_ENDIAN,
  199. .valid = {
  200. .min_access_size = 4,
  201. .max_access_size = 4
  202. }
  203. },
  204. [true] = {
  205. .read = goldfish_rtc_read,
  206. .write = goldfish_rtc_write,
  207. .endianness = DEVICE_BIG_ENDIAN,
  208. .valid = {
  209. .min_access_size = 4,
  210. .max_access_size = 4
  211. }
  212. },
  213. };
  214. static const VMStateDescription goldfish_rtc_vmstate = {
  215. .name = TYPE_GOLDFISH_RTC,
  216. .version_id = 2,
  217. .pre_save = goldfish_rtc_pre_save,
  218. .post_load = goldfish_rtc_post_load,
  219. .fields = (VMStateField[]) {
  220. VMSTATE_UINT64(tick_offset_vmstate, GoldfishRTCState),
  221. VMSTATE_UINT64(alarm_next, GoldfishRTCState),
  222. VMSTATE_UINT32(alarm_running, GoldfishRTCState),
  223. VMSTATE_UINT32(irq_pending, GoldfishRTCState),
  224. VMSTATE_UINT32(irq_enabled, GoldfishRTCState),
  225. VMSTATE_UINT32(time_high, GoldfishRTCState),
  226. VMSTATE_END_OF_LIST()
  227. }
  228. };
  229. static void goldfish_rtc_reset(DeviceState *dev)
  230. {
  231. GoldfishRTCState *s = GOLDFISH_RTC(dev);
  232. struct tm tm;
  233. timer_del(s->timer);
  234. qemu_get_timedate(&tm, 0);
  235. s->tick_offset = mktimegm(&tm);
  236. s->tick_offset *= NANOSECONDS_PER_SECOND;
  237. s->tick_offset -= qemu_clock_get_ns(rtc_clock);
  238. s->tick_offset_vmstate = 0;
  239. s->alarm_next = 0;
  240. s->alarm_running = 0;
  241. s->irq_pending = 0;
  242. s->irq_enabled = 0;
  243. }
  244. static void goldfish_rtc_realize(DeviceState *d, Error **errp)
  245. {
  246. SysBusDevice *dev = SYS_BUS_DEVICE(d);
  247. GoldfishRTCState *s = GOLDFISH_RTC(d);
  248. memory_region_init_io(&s->iomem, OBJECT(s),
  249. &goldfish_rtc_ops[s->big_endian], s,
  250. "goldfish_rtc", 0x24);
  251. sysbus_init_mmio(dev, &s->iomem);
  252. sysbus_init_irq(dev, &s->irq);
  253. s->timer = timer_new_ns(rtc_clock, goldfish_rtc_interrupt, s);
  254. }
  255. static Property goldfish_rtc_properties[] = {
  256. DEFINE_PROP_BOOL("big-endian", GoldfishRTCState, big_endian,
  257. false),
  258. DEFINE_PROP_END_OF_LIST(),
  259. };
  260. static void goldfish_rtc_class_init(ObjectClass *klass, void *data)
  261. {
  262. DeviceClass *dc = DEVICE_CLASS(klass);
  263. device_class_set_props(dc, goldfish_rtc_properties);
  264. dc->realize = goldfish_rtc_realize;
  265. dc->reset = goldfish_rtc_reset;
  266. dc->vmsd = &goldfish_rtc_vmstate;
  267. }
  268. static const TypeInfo goldfish_rtc_info = {
  269. .name = TYPE_GOLDFISH_RTC,
  270. .parent = TYPE_SYS_BUS_DEVICE,
  271. .instance_size = sizeof(GoldfishRTCState),
  272. .class_init = goldfish_rtc_class_init,
  273. };
  274. static void goldfish_rtc_register_types(void)
  275. {
  276. type_register_static(&goldfish_rtc_info);
  277. }
  278. type_init(goldfish_rtc_register_types)