pl031.c 9.3 KB

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