pl031.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * ARM AMBA PrimeCell PL031 RTC
  3. *
  4. * Copyright (c) 2007 CodeSourcery
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Contributions after 2012-01-13 are licensed under the terms of the
  11. * GNU GPL, version 2 or (at your option) any later version.
  12. */
  13. #include "qemu/osdep.h"
  14. #include "hw/rtc/pl031.h"
  15. #include "migration/vmstate.h"
  16. #include "hw/irq.h"
  17. #include "hw/qdev-properties.h"
  18. #include "hw/sysbus.h"
  19. #include "qemu/timer.h"
  20. #include "sysemu/sysemu.h"
  21. #include "sysemu/rtc.h"
  22. #include "qemu/cutils.h"
  23. #include "qemu/log.h"
  24. #include "qemu/module.h"
  25. #include "trace.h"
  26. #include "qapi/qapi-events-misc.h"
  27. #define RTC_DR 0x00 /* Data read register */
  28. #define RTC_MR 0x04 /* Match register */
  29. #define RTC_LR 0x08 /* Data load register */
  30. #define RTC_CR 0x0c /* Control register */
  31. #define RTC_IMSC 0x10 /* Interrupt mask and set register */
  32. #define RTC_RIS 0x14 /* Raw interrupt status register */
  33. #define RTC_MIS 0x18 /* Masked interrupt status register */
  34. #define RTC_ICR 0x1c /* Interrupt clear register */
  35. static const unsigned char pl031_id[] = {
  36. 0x31, 0x10, 0x14, 0x00, /* Device ID */
  37. 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
  38. };
  39. static void pl031_update(PL031State *s)
  40. {
  41. uint32_t flags = s->is & s->im;
  42. trace_pl031_irq_state(flags);
  43. qemu_set_irq(s->irq, flags);
  44. }
  45. static void pl031_interrupt(void * opaque)
  46. {
  47. PL031State *s = (PL031State *)opaque;
  48. s->is = 1;
  49. trace_pl031_alarm_raised();
  50. pl031_update(s);
  51. }
  52. static uint32_t pl031_get_count(PL031State *s)
  53. {
  54. int64_t now = qemu_clock_get_ns(rtc_clock);
  55. return s->tick_offset + now / NANOSECONDS_PER_SECOND;
  56. }
  57. static void pl031_set_alarm(PL031State *s)
  58. {
  59. uint32_t ticks;
  60. /* The timer wraps around. This subtraction also wraps in the same way,
  61. and gives correct results when alarm < now_ticks. */
  62. ticks = s->mr - pl031_get_count(s);
  63. trace_pl031_set_alarm(ticks);
  64. if (ticks == 0) {
  65. timer_del(s->timer);
  66. pl031_interrupt(s);
  67. } else {
  68. int64_t now = qemu_clock_get_ns(rtc_clock);
  69. timer_mod(s->timer, now + (int64_t)ticks * NANOSECONDS_PER_SECOND);
  70. }
  71. }
  72. static uint64_t pl031_read(void *opaque, hwaddr offset,
  73. unsigned size)
  74. {
  75. PL031State *s = (PL031State *)opaque;
  76. uint64_t r;
  77. switch (offset) {
  78. case RTC_DR:
  79. r = pl031_get_count(s);
  80. break;
  81. case RTC_MR:
  82. r = s->mr;
  83. break;
  84. case RTC_IMSC:
  85. r = s->im;
  86. break;
  87. case RTC_RIS:
  88. r = s->is;
  89. break;
  90. case RTC_LR:
  91. r = s->lr;
  92. break;
  93. case RTC_CR:
  94. /* RTC is permanently enabled. */
  95. r = 1;
  96. break;
  97. case RTC_MIS:
  98. r = s->is & s->im;
  99. break;
  100. case 0xfe0 ... 0xfff:
  101. r = pl031_id[(offset - 0xfe0) >> 2];
  102. break;
  103. case RTC_ICR:
  104. qemu_log_mask(LOG_GUEST_ERROR,
  105. "pl031: read of write-only register at offset 0x%x\n",
  106. (int)offset);
  107. r = 0;
  108. break;
  109. default:
  110. qemu_log_mask(LOG_GUEST_ERROR,
  111. "pl031_read: Bad offset 0x%x\n", (int)offset);
  112. r = 0;
  113. break;
  114. }
  115. trace_pl031_read(offset, r);
  116. return r;
  117. }
  118. static void pl031_write(void * opaque, hwaddr offset,
  119. uint64_t value, unsigned size)
  120. {
  121. PL031State *s = (PL031State *)opaque;
  122. trace_pl031_write(offset, value);
  123. switch (offset) {
  124. case RTC_LR: {
  125. g_autofree const char *qom_path = object_get_canonical_path(opaque);
  126. struct tm tm;
  127. s->tick_offset += value - pl031_get_count(s);
  128. qemu_get_timedate(&tm, s->tick_offset);
  129. qapi_event_send_rtc_change(qemu_timedate_diff(&tm), qom_path);
  130. pl031_set_alarm(s);
  131. break;
  132. }
  133. case RTC_MR:
  134. s->mr = value;
  135. pl031_set_alarm(s);
  136. break;
  137. case RTC_IMSC:
  138. s->im = value & 1;
  139. pl031_update(s);
  140. break;
  141. case RTC_ICR:
  142. s->is &= ~value;
  143. pl031_update(s);
  144. break;
  145. case RTC_CR:
  146. /* Written value is ignored. */
  147. break;
  148. case RTC_DR:
  149. case RTC_MIS:
  150. case RTC_RIS:
  151. qemu_log_mask(LOG_GUEST_ERROR,
  152. "pl031: write to read-only register at offset 0x%x\n",
  153. (int)offset);
  154. break;
  155. default:
  156. qemu_log_mask(LOG_GUEST_ERROR,
  157. "pl031_write: Bad offset 0x%x\n", (int)offset);
  158. break;
  159. }
  160. }
  161. static const MemoryRegionOps pl031_ops = {
  162. .read = pl031_read,
  163. .write = pl031_write,
  164. .endianness = DEVICE_NATIVE_ENDIAN,
  165. };
  166. static void pl031_init(Object *obj)
  167. {
  168. PL031State *s = PL031(obj);
  169. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  170. struct tm tm;
  171. memory_region_init_io(&s->iomem, obj, &pl031_ops, s, "pl031", 0x1000);
  172. sysbus_init_mmio(dev, &s->iomem);
  173. sysbus_init_irq(dev, &s->irq);
  174. qemu_get_timedate(&tm, 0);
  175. s->tick_offset = mktimegm(&tm) -
  176. qemu_clock_get_ns(rtc_clock) / NANOSECONDS_PER_SECOND;
  177. s->timer = timer_new_ns(rtc_clock, pl031_interrupt, s);
  178. }
  179. static void pl031_finalize(Object *obj)
  180. {
  181. PL031State *s = PL031(obj);
  182. timer_free(s->timer);
  183. }
  184. static int pl031_pre_save(void *opaque)
  185. {
  186. PL031State *s = opaque;
  187. /*
  188. * The PL031 device model code uses the tick_offset field, which is
  189. * the offset between what the guest RTC should read and what the
  190. * QEMU rtc_clock reads:
  191. * guest_rtc = rtc_clock + tick_offset
  192. * and so
  193. * tick_offset = guest_rtc - rtc_clock
  194. *
  195. * We want to migrate this offset, which sounds straightforward.
  196. * Unfortunately older versions of QEMU migrated a conversion of this
  197. * offset into an offset from the vm_clock. (This was in turn an
  198. * attempt to be compatible with even older QEMU versions, but it
  199. * has incorrect behaviour if the rtc_clock is not the same as the
  200. * vm_clock.) So we put the actual tick_offset into a migration
  201. * subsection, and the backwards-compatible time-relative-to-vm_clock
  202. * in the main migration state.
  203. *
  204. * Calculate base time relative to QEMU_CLOCK_VIRTUAL:
  205. */
  206. int64_t delta = qemu_clock_get_ns(rtc_clock) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  207. s->tick_offset_vmstate = s->tick_offset + delta / NANOSECONDS_PER_SECOND;
  208. return 0;
  209. }
  210. static int pl031_pre_load(void *opaque)
  211. {
  212. PL031State *s = opaque;
  213. s->tick_offset_migrated = false;
  214. return 0;
  215. }
  216. static int pl031_post_load(void *opaque, int version_id)
  217. {
  218. PL031State *s = opaque;
  219. /*
  220. * If we got the tick_offset subsection, then we can just use
  221. * the value in that. Otherwise the source is an older QEMU and
  222. * has given us the offset from the vm_clock; convert it back to
  223. * an offset from the rtc_clock. This will cause time to incorrectly
  224. * go backwards compared to the host RTC, but this is unavoidable.
  225. */
  226. if (!s->tick_offset_migrated) {
  227. int64_t delta = qemu_clock_get_ns(rtc_clock) -
  228. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  229. s->tick_offset = s->tick_offset_vmstate -
  230. delta / NANOSECONDS_PER_SECOND;
  231. }
  232. pl031_set_alarm(s);
  233. return 0;
  234. }
  235. static int pl031_tick_offset_post_load(void *opaque, int version_id)
  236. {
  237. PL031State *s = opaque;
  238. s->tick_offset_migrated = true;
  239. return 0;
  240. }
  241. static bool pl031_tick_offset_needed(void *opaque)
  242. {
  243. PL031State *s = opaque;
  244. return s->migrate_tick_offset;
  245. }
  246. static const VMStateDescription vmstate_pl031_tick_offset = {
  247. .name = "pl031/tick-offset",
  248. .version_id = 1,
  249. .minimum_version_id = 1,
  250. .needed = pl031_tick_offset_needed,
  251. .post_load = pl031_tick_offset_post_load,
  252. .fields = (VMStateField[]) {
  253. VMSTATE_UINT32(tick_offset, PL031State),
  254. VMSTATE_END_OF_LIST()
  255. }
  256. };
  257. static const VMStateDescription vmstate_pl031 = {
  258. .name = "pl031",
  259. .version_id = 1,
  260. .minimum_version_id = 1,
  261. .pre_save = pl031_pre_save,
  262. .pre_load = pl031_pre_load,
  263. .post_load = pl031_post_load,
  264. .fields = (VMStateField[]) {
  265. VMSTATE_UINT32(tick_offset_vmstate, PL031State),
  266. VMSTATE_UINT32(mr, PL031State),
  267. VMSTATE_UINT32(lr, PL031State),
  268. VMSTATE_UINT32(cr, PL031State),
  269. VMSTATE_UINT32(im, PL031State),
  270. VMSTATE_UINT32(is, PL031State),
  271. VMSTATE_END_OF_LIST()
  272. },
  273. .subsections = (const VMStateDescription*[]) {
  274. &vmstate_pl031_tick_offset,
  275. NULL
  276. }
  277. };
  278. static Property pl031_properties[] = {
  279. /*
  280. * True to correctly migrate the tick offset of the RTC. False to
  281. * obtain backward migration compatibility with older QEMU versions,
  282. * at the expense of the guest RTC going backwards compared with the
  283. * host RTC when the VM is saved/restored if using -rtc host.
  284. * (Even if set to 'true' older QEMU can migrate forward to newer QEMU;
  285. * 'false' also permits newer QEMU to migrate to older QEMU.)
  286. */
  287. DEFINE_PROP_BOOL("migrate-tick-offset",
  288. PL031State, migrate_tick_offset, true),
  289. DEFINE_PROP_END_OF_LIST()
  290. };
  291. static void pl031_class_init(ObjectClass *klass, void *data)
  292. {
  293. DeviceClass *dc = DEVICE_CLASS(klass);
  294. dc->vmsd = &vmstate_pl031;
  295. device_class_set_props(dc, pl031_properties);
  296. }
  297. static const TypeInfo pl031_info = {
  298. .name = TYPE_PL031,
  299. .parent = TYPE_SYS_BUS_DEVICE,
  300. .instance_size = sizeof(PL031State),
  301. .instance_init = pl031_init,
  302. .instance_finalize = pl031_finalize,
  303. .class_init = pl031_class_init,
  304. };
  305. static void pl031_register_types(void)
  306. {
  307. type_register_static(&pl031_info);
  308. }
  309. type_init(pl031_register_types)