goldfish_rtc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "system/system.h"
  30. #include "system/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_post_load(void *opaque, int version_id)
  164. {
  165. GoldfishRTCState *s = opaque;
  166. if (version_id < 3) {
  167. /*
  168. * Previous versions didn't migrate tick_offset directly. Instead, they
  169. * migrated tick_offset_vmstate, which is a recalculation based on
  170. * QEMU_CLOCK_VIRTUAL. We use tick_offset_vmstate when migrating from
  171. * older versions.
  172. */
  173. uint64_t delta = qemu_clock_get_ns(rtc_clock) -
  174. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  175. s->tick_offset = s->tick_offset_vmstate - delta;
  176. }
  177. goldfish_rtc_set_alarm(s);
  178. return 0;
  179. }
  180. static const MemoryRegionOps goldfish_rtc_ops[2] = {
  181. [false] = {
  182. .read = goldfish_rtc_read,
  183. .write = goldfish_rtc_write,
  184. .endianness = DEVICE_LITTLE_ENDIAN,
  185. .valid = {
  186. .min_access_size = 4,
  187. .max_access_size = 4
  188. }
  189. },
  190. [true] = {
  191. .read = goldfish_rtc_read,
  192. .write = goldfish_rtc_write,
  193. .endianness = DEVICE_BIG_ENDIAN,
  194. .valid = {
  195. .min_access_size = 4,
  196. .max_access_size = 4
  197. }
  198. },
  199. };
  200. static const VMStateDescription goldfish_rtc_vmstate = {
  201. .name = TYPE_GOLDFISH_RTC,
  202. .version_id = 3,
  203. .post_load = goldfish_rtc_post_load,
  204. .fields = (const VMStateField[]) {
  205. VMSTATE_UINT64(tick_offset_vmstate, GoldfishRTCState),
  206. VMSTATE_UINT64(alarm_next, GoldfishRTCState),
  207. VMSTATE_UINT32(alarm_running, GoldfishRTCState),
  208. VMSTATE_UINT32(irq_pending, GoldfishRTCState),
  209. VMSTATE_UINT32(irq_enabled, GoldfishRTCState),
  210. VMSTATE_UINT32(time_high, GoldfishRTCState),
  211. VMSTATE_UINT64_V(tick_offset, GoldfishRTCState, 3),
  212. VMSTATE_END_OF_LIST()
  213. }
  214. };
  215. static void goldfish_rtc_reset(DeviceState *dev)
  216. {
  217. GoldfishRTCState *s = GOLDFISH_RTC(dev);
  218. timer_del(s->timer);
  219. s->alarm_next = 0;
  220. s->alarm_running = 0;
  221. s->irq_pending = 0;
  222. s->irq_enabled = 0;
  223. }
  224. static void goldfish_rtc_realize(DeviceState *d, Error **errp)
  225. {
  226. SysBusDevice *dev = SYS_BUS_DEVICE(d);
  227. GoldfishRTCState *s = GOLDFISH_RTC(d);
  228. struct tm tm;
  229. memory_region_init_io(&s->iomem, OBJECT(s),
  230. &goldfish_rtc_ops[s->big_endian], s,
  231. "goldfish_rtc", 0x24);
  232. sysbus_init_mmio(dev, &s->iomem);
  233. sysbus_init_irq(dev, &s->irq);
  234. s->timer = timer_new_ns(rtc_clock, goldfish_rtc_interrupt, s);
  235. qemu_get_timedate(&tm, 0);
  236. s->tick_offset = mktimegm(&tm);
  237. s->tick_offset *= NANOSECONDS_PER_SECOND;
  238. s->tick_offset -= qemu_clock_get_ns(rtc_clock);
  239. }
  240. static const Property goldfish_rtc_properties[] = {
  241. DEFINE_PROP_BOOL("big-endian", GoldfishRTCState, big_endian,
  242. false),
  243. };
  244. static void goldfish_rtc_class_init(ObjectClass *klass, void *data)
  245. {
  246. DeviceClass *dc = DEVICE_CLASS(klass);
  247. device_class_set_props(dc, goldfish_rtc_properties);
  248. dc->realize = goldfish_rtc_realize;
  249. device_class_set_legacy_reset(dc, goldfish_rtc_reset);
  250. dc->vmsd = &goldfish_rtc_vmstate;
  251. }
  252. static const TypeInfo goldfish_rtc_info = {
  253. .name = TYPE_GOLDFISH_RTC,
  254. .parent = TYPE_SYS_BUS_DEVICE,
  255. .instance_size = sizeof(GoldfishRTCState),
  256. .class_init = goldfish_rtc_class_init,
  257. };
  258. static void goldfish_rtc_register_types(void)
  259. {
  260. type_register_static(&goldfish_rtc_info);
  261. }
  262. type_init(goldfish_rtc_register_types)