2
0

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 "system/system.h"
  21. #include "system/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->lr = value;
  128. s->tick_offset += value - pl031_get_count(s);
  129. qemu_get_timedate(&tm, s->tick_offset);
  130. qapi_event_send_rtc_change(qemu_timedate_diff(&tm), qom_path);
  131. pl031_set_alarm(s);
  132. break;
  133. }
  134. case RTC_MR:
  135. s->mr = value;
  136. pl031_set_alarm(s);
  137. break;
  138. case RTC_IMSC:
  139. s->im = value & 1;
  140. pl031_update(s);
  141. break;
  142. case RTC_ICR:
  143. s->is &= ~value;
  144. pl031_update(s);
  145. break;
  146. case RTC_CR:
  147. /* Written value is ignored. */
  148. break;
  149. case RTC_DR:
  150. case RTC_MIS:
  151. case RTC_RIS:
  152. qemu_log_mask(LOG_GUEST_ERROR,
  153. "pl031: write to read-only register at offset 0x%x\n",
  154. (int)offset);
  155. break;
  156. default:
  157. qemu_log_mask(LOG_GUEST_ERROR,
  158. "pl031_write: Bad offset 0x%x\n", (int)offset);
  159. break;
  160. }
  161. }
  162. static const MemoryRegionOps pl031_ops = {
  163. .read = pl031_read,
  164. .write = pl031_write,
  165. .endianness = DEVICE_NATIVE_ENDIAN,
  166. };
  167. static void pl031_init(Object *obj)
  168. {
  169. PL031State *s = PL031(obj);
  170. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  171. struct tm tm;
  172. memory_region_init_io(&s->iomem, obj, &pl031_ops, s, "pl031", 0x1000);
  173. sysbus_init_mmio(dev, &s->iomem);
  174. sysbus_init_irq(dev, &s->irq);
  175. qemu_get_timedate(&tm, 0);
  176. s->tick_offset = mktimegm(&tm) -
  177. qemu_clock_get_ns(rtc_clock) / NANOSECONDS_PER_SECOND;
  178. s->timer = timer_new_ns(rtc_clock, pl031_interrupt, s);
  179. }
  180. static void pl031_finalize(Object *obj)
  181. {
  182. PL031State *s = PL031(obj);
  183. timer_free(s->timer);
  184. }
  185. static int pl031_pre_save(void *opaque)
  186. {
  187. PL031State *s = opaque;
  188. /*
  189. * The PL031 device model code uses the tick_offset field, which is
  190. * the offset between what the guest RTC should read and what the
  191. * QEMU rtc_clock reads:
  192. * guest_rtc = rtc_clock + tick_offset
  193. * and so
  194. * tick_offset = guest_rtc - rtc_clock
  195. *
  196. * We want to migrate this offset, which sounds straightforward.
  197. * Unfortunately older versions of QEMU migrated a conversion of this
  198. * offset into an offset from the vm_clock. (This was in turn an
  199. * attempt to be compatible with even older QEMU versions, but it
  200. * has incorrect behaviour if the rtc_clock is not the same as the
  201. * vm_clock.) So we put the actual tick_offset into a migration
  202. * subsection, and the backwards-compatible time-relative-to-vm_clock
  203. * in the main migration state.
  204. *
  205. * Calculate base time relative to QEMU_CLOCK_VIRTUAL:
  206. */
  207. int64_t delta = qemu_clock_get_ns(rtc_clock) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  208. s->tick_offset_vmstate = s->tick_offset + delta / NANOSECONDS_PER_SECOND;
  209. return 0;
  210. }
  211. static int pl031_pre_load(void *opaque)
  212. {
  213. PL031State *s = opaque;
  214. s->tick_offset_migrated = false;
  215. return 0;
  216. }
  217. static int pl031_post_load(void *opaque, int version_id)
  218. {
  219. PL031State *s = opaque;
  220. /*
  221. * If we got the tick_offset subsection, then we can just use
  222. * the value in that. Otherwise the source is an older QEMU and
  223. * has given us the offset from the vm_clock; convert it back to
  224. * an offset from the rtc_clock. This will cause time to incorrectly
  225. * go backwards compared to the host RTC, but this is unavoidable.
  226. */
  227. if (!s->tick_offset_migrated) {
  228. int64_t delta = qemu_clock_get_ns(rtc_clock) -
  229. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  230. s->tick_offset = s->tick_offset_vmstate -
  231. delta / NANOSECONDS_PER_SECOND;
  232. }
  233. pl031_set_alarm(s);
  234. return 0;
  235. }
  236. static int pl031_tick_offset_post_load(void *opaque, int version_id)
  237. {
  238. PL031State *s = opaque;
  239. s->tick_offset_migrated = true;
  240. return 0;
  241. }
  242. static bool pl031_tick_offset_needed(void *opaque)
  243. {
  244. PL031State *s = opaque;
  245. return s->migrate_tick_offset;
  246. }
  247. static const VMStateDescription vmstate_pl031_tick_offset = {
  248. .name = "pl031/tick-offset",
  249. .version_id = 1,
  250. .minimum_version_id = 1,
  251. .needed = pl031_tick_offset_needed,
  252. .post_load = pl031_tick_offset_post_load,
  253. .fields = (const VMStateField[]) {
  254. VMSTATE_UINT32(tick_offset, PL031State),
  255. VMSTATE_END_OF_LIST()
  256. }
  257. };
  258. static const VMStateDescription vmstate_pl031 = {
  259. .name = "pl031",
  260. .version_id = 1,
  261. .minimum_version_id = 1,
  262. .pre_save = pl031_pre_save,
  263. .pre_load = pl031_pre_load,
  264. .post_load = pl031_post_load,
  265. .fields = (const VMStateField[]) {
  266. VMSTATE_UINT32(tick_offset_vmstate, PL031State),
  267. VMSTATE_UINT32(mr, PL031State),
  268. VMSTATE_UINT32(lr, PL031State),
  269. VMSTATE_UINT32(cr, PL031State),
  270. VMSTATE_UINT32(im, PL031State),
  271. VMSTATE_UINT32(is, PL031State),
  272. VMSTATE_END_OF_LIST()
  273. },
  274. .subsections = (const VMStateDescription * const []) {
  275. &vmstate_pl031_tick_offset,
  276. NULL
  277. }
  278. };
  279. static const Property pl031_properties[] = {
  280. /*
  281. * True to correctly migrate the tick offset of the RTC. False to
  282. * obtain backward migration compatibility with older QEMU versions,
  283. * at the expense of the guest RTC going backwards compared with the
  284. * host RTC when the VM is saved/restored if using -rtc host.
  285. * (Even if set to 'true' older QEMU can migrate forward to newer QEMU;
  286. * 'false' also permits newer QEMU to migrate to older QEMU.)
  287. */
  288. DEFINE_PROP_BOOL("migrate-tick-offset",
  289. PL031State, migrate_tick_offset, true),
  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)