2
0

sse-timer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Arm SSE Subsystem System Timer
  3. *
  4. * Copyright (c) 2020 Linaro Limited
  5. * Written by Peter Maydell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 or
  9. * (at your option) any later version.
  10. */
  11. /*
  12. * This is a model of the "System timer" which is documented in
  13. * the Arm SSE-123 Example Subsystem Technical Reference Manual:
  14. * https://developer.arm.com/documentation/101370/latest/
  15. *
  16. * The timer is based around a simple 64-bit incrementing counter
  17. * (readable from CNTPCT_HI/LO). The timer fires when
  18. * Counter - CompareValue >= 0.
  19. * The CompareValue is guest-writable, via CNTP_CVAL_HI/LO.
  20. * CNTP_TVAL is an alternative view of the CompareValue defined by
  21. * TimerValue = CompareValue[31:0] - Counter[31:0]
  22. * which can be both read and written.
  23. * This part is similar to the generic timer in an Arm A-class CPU.
  24. *
  25. * The timer also has a separate auto-increment timer. When this
  26. * timer is enabled, then the AutoIncrValue is set to:
  27. * AutoIncrValue = Reload + Counter
  28. * and this timer fires when
  29. * Counter - AutoIncrValue >= 0
  30. * at which point, an interrupt is generated and the new AutoIncrValue
  31. * is calculated.
  32. * When the auto-increment timer is enabled, interrupt generation
  33. * via the compare/timervalue registers is disabled.
  34. */
  35. #include "qemu/osdep.h"
  36. #include "qemu/log.h"
  37. #include "qemu/timer.h"
  38. #include "qapi/error.h"
  39. #include "trace.h"
  40. #include "hw/timer/sse-timer.h"
  41. #include "hw/timer/sse-counter.h"
  42. #include "hw/sysbus.h"
  43. #include "hw/irq.h"
  44. #include "hw/registerfields.h"
  45. #include "hw/clock.h"
  46. #include "hw/qdev-clock.h"
  47. #include "hw/qdev-properties.h"
  48. #include "migration/vmstate.h"
  49. REG32(CNTPCT_LO, 0x0)
  50. REG32(CNTPCT_HI, 0x4)
  51. REG32(CNTFRQ, 0x10)
  52. REG32(CNTP_CVAL_LO, 0x20)
  53. REG32(CNTP_CVAL_HI, 0x24)
  54. REG32(CNTP_TVAL, 0x28)
  55. REG32(CNTP_CTL, 0x2c)
  56. FIELD(CNTP_CTL, ENABLE, 0, 1)
  57. FIELD(CNTP_CTL, IMASK, 1, 1)
  58. FIELD(CNTP_CTL, ISTATUS, 2, 1)
  59. REG32(CNTP_AIVAL_LO, 0x40)
  60. REG32(CNTP_AIVAL_HI, 0x44)
  61. REG32(CNTP_AIVAL_RELOAD, 0x48)
  62. REG32(CNTP_AIVAL_CTL, 0x4c)
  63. FIELD(CNTP_AIVAL_CTL, EN, 0, 1)
  64. FIELD(CNTP_AIVAL_CTL, CLR, 1, 1)
  65. REG32(CNTP_CFG, 0x50)
  66. FIELD(CNTP_CFG, AIVAL, 0, 4)
  67. #define R_CNTP_CFG_AIVAL_IMPLEMENTED 1
  68. REG32(PID4, 0xFD0)
  69. REG32(PID5, 0xFD4)
  70. REG32(PID6, 0xFD8)
  71. REG32(PID7, 0xFDC)
  72. REG32(PID0, 0xFE0)
  73. REG32(PID1, 0xFE4)
  74. REG32(PID2, 0xFE8)
  75. REG32(PID3, 0xFEC)
  76. REG32(CID0, 0xFF0)
  77. REG32(CID1, 0xFF4)
  78. REG32(CID2, 0xFF8)
  79. REG32(CID3, 0xFFC)
  80. /* PID/CID values */
  81. static const int timer_id[] = {
  82. 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
  83. 0xb7, 0xb0, 0x0b, 0x00, /* PID0..PID3 */
  84. 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
  85. };
  86. static bool sse_is_autoinc(SSETimer *s)
  87. {
  88. return (s->cntp_aival_ctl & R_CNTP_AIVAL_CTL_EN_MASK) != 0;
  89. }
  90. static bool sse_enabled(SSETimer *s)
  91. {
  92. return (s->cntp_ctl & R_CNTP_CTL_ENABLE_MASK) != 0;
  93. }
  94. static uint64_t sse_cntpct(SSETimer *s)
  95. {
  96. /* Return the CNTPCT value for the current time */
  97. return sse_counter_for_timestamp(s->counter,
  98. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  99. }
  100. static bool sse_timer_status(SSETimer *s)
  101. {
  102. /*
  103. * Return true if timer condition is met. This is used for both
  104. * the CNTP_CTL.ISTATUS bit and for whether (unless masked) we
  105. * assert our IRQ.
  106. * The documentation is unclear about the behaviour of ISTATUS when
  107. * in autoincrement mode; we assume that it follows CNTP_AIVAL_CTL.CLR
  108. * (ie whether the autoincrement timer is asserting the interrupt).
  109. */
  110. if (!sse_enabled(s)) {
  111. return false;
  112. }
  113. if (sse_is_autoinc(s)) {
  114. return s->cntp_aival_ctl & R_CNTP_AIVAL_CTL_CLR_MASK;
  115. } else {
  116. return sse_cntpct(s) >= s->cntp_cval;
  117. }
  118. }
  119. static void sse_update_irq(SSETimer *s)
  120. {
  121. bool irqstate = (!(s->cntp_ctl & R_CNTP_CTL_IMASK_MASK) &&
  122. sse_timer_status(s));
  123. qemu_set_irq(s->irq, irqstate);
  124. }
  125. static void sse_set_timer(SSETimer *s, uint64_t nexttick)
  126. {
  127. /* Set the timer to expire at nexttick */
  128. uint64_t expiry = sse_counter_tick_to_time(s->counter, nexttick);
  129. if (expiry <= INT64_MAX) {
  130. timer_mod_ns(&s->timer, expiry);
  131. } else {
  132. /*
  133. * nexttick is so far in the future that it would overflow the
  134. * signed 64-bit range of a QEMUTimer. Since timer_mod_ns()
  135. * expiry times are absolute, not relative, we are never going
  136. * to be able to set the timer to this value, so we must just
  137. * assume that guest execution can never run so long that it
  138. * reaches the theoretical point when the timer fires.
  139. * This is also the code path for "counter is not running",
  140. * which is signalled by expiry == UINT64_MAX.
  141. */
  142. timer_del(&s->timer);
  143. }
  144. }
  145. static void sse_recalc_timer(SSETimer *s)
  146. {
  147. /* Recalculate the normal timer */
  148. uint64_t count, nexttick;
  149. if (sse_is_autoinc(s)) {
  150. return;
  151. }
  152. if (!sse_enabled(s)) {
  153. timer_del(&s->timer);
  154. return;
  155. }
  156. count = sse_cntpct(s);
  157. if (count >= s->cntp_cval) {
  158. /*
  159. * Timer condition already met. In theory we have a transition when
  160. * the count rolls back over to 0, but that is so far in the future
  161. * that it is not representable as a timer_mod() expiry, so in
  162. * fact sse_set_timer() will always just delete the timer.
  163. */
  164. nexttick = UINT64_MAX;
  165. } else {
  166. /* Next transition is when count hits cval */
  167. nexttick = s->cntp_cval;
  168. }
  169. sse_set_timer(s, nexttick);
  170. sse_update_irq(s);
  171. }
  172. static void sse_autoinc(SSETimer *s)
  173. {
  174. /* Auto-increment the AIVAL, and set the timer accordingly */
  175. s->cntp_aival = sse_cntpct(s) + s->cntp_aival_reload;
  176. sse_set_timer(s, s->cntp_aival);
  177. }
  178. static void sse_timer_cb(void *opaque)
  179. {
  180. SSETimer *s = SSE_TIMER(opaque);
  181. if (sse_is_autoinc(s)) {
  182. uint64_t count = sse_cntpct(s);
  183. if (count >= s->cntp_aival) {
  184. /* Timer condition met, set CLR and do another autoinc */
  185. s->cntp_aival_ctl |= R_CNTP_AIVAL_CTL_CLR_MASK;
  186. s->cntp_aival = count + s->cntp_aival_reload;
  187. }
  188. sse_set_timer(s, s->cntp_aival);
  189. sse_update_irq(s);
  190. } else {
  191. sse_recalc_timer(s);
  192. }
  193. }
  194. static uint64_t sse_timer_read(void *opaque, hwaddr offset, unsigned size)
  195. {
  196. SSETimer *s = SSE_TIMER(opaque);
  197. uint64_t r;
  198. switch (offset) {
  199. case A_CNTPCT_LO:
  200. r = extract64(sse_cntpct(s), 0, 32);
  201. break;
  202. case A_CNTPCT_HI:
  203. r = extract64(sse_cntpct(s), 32, 32);
  204. break;
  205. case A_CNTFRQ:
  206. r = s->cntfrq;
  207. break;
  208. case A_CNTP_CVAL_LO:
  209. r = extract64(s->cntp_cval, 0, 32);
  210. break;
  211. case A_CNTP_CVAL_HI:
  212. r = extract64(s->cntp_cval, 32, 32);
  213. break;
  214. case A_CNTP_TVAL:
  215. r = extract64(s->cntp_cval - sse_cntpct(s), 0, 32);
  216. break;
  217. case A_CNTP_CTL:
  218. r = s->cntp_ctl;
  219. if (sse_timer_status(s)) {
  220. r |= R_CNTP_CTL_ISTATUS_MASK;
  221. }
  222. break;
  223. case A_CNTP_AIVAL_LO:
  224. r = extract64(s->cntp_aival, 0, 32);
  225. break;
  226. case A_CNTP_AIVAL_HI:
  227. r = extract64(s->cntp_aival, 32, 32);
  228. break;
  229. case A_CNTP_AIVAL_RELOAD:
  230. r = s->cntp_aival_reload;
  231. break;
  232. case A_CNTP_AIVAL_CTL:
  233. /*
  234. * All the bits of AIVAL_CTL are documented as WO, but this is probably
  235. * a documentation error. We implement them as readable.
  236. */
  237. r = s->cntp_aival_ctl;
  238. break;
  239. case A_CNTP_CFG:
  240. r = R_CNTP_CFG_AIVAL_IMPLEMENTED << R_CNTP_CFG_AIVAL_SHIFT;
  241. break;
  242. case A_PID4 ... A_CID3:
  243. r = timer_id[(offset - A_PID4) / 4];
  244. break;
  245. default:
  246. qemu_log_mask(LOG_GUEST_ERROR,
  247. "SSE System Timer read: bad offset 0x%x",
  248. (unsigned) offset);
  249. r = 0;
  250. break;
  251. }
  252. trace_sse_timer_read(offset, r, size);
  253. return r;
  254. }
  255. static void sse_timer_write(void *opaque, hwaddr offset, uint64_t value,
  256. unsigned size)
  257. {
  258. SSETimer *s = SSE_TIMER(opaque);
  259. trace_sse_timer_write(offset, value, size);
  260. switch (offset) {
  261. case A_CNTFRQ:
  262. s->cntfrq = value;
  263. break;
  264. case A_CNTP_CVAL_LO:
  265. s->cntp_cval = deposit64(s->cntp_cval, 0, 32, value);
  266. sse_recalc_timer(s);
  267. break;
  268. case A_CNTP_CVAL_HI:
  269. s->cntp_cval = deposit64(s->cntp_cval, 32, 32, value);
  270. sse_recalc_timer(s);
  271. break;
  272. case A_CNTP_TVAL:
  273. s->cntp_cval = sse_cntpct(s) + sextract64(value, 0, 32);
  274. sse_recalc_timer(s);
  275. break;
  276. case A_CNTP_CTL:
  277. {
  278. uint32_t old_ctl = s->cntp_ctl;
  279. value &= R_CNTP_CTL_ENABLE_MASK | R_CNTP_CTL_IMASK_MASK;
  280. s->cntp_ctl = value;
  281. if ((old_ctl ^ s->cntp_ctl) & R_CNTP_CTL_ENABLE_MASK) {
  282. if (sse_enabled(s)) {
  283. if (sse_is_autoinc(s)) {
  284. sse_autoinc(s);
  285. } else {
  286. sse_recalc_timer(s);
  287. }
  288. }
  289. }
  290. sse_update_irq(s);
  291. break;
  292. }
  293. case A_CNTP_AIVAL_RELOAD:
  294. s->cntp_aival_reload = value;
  295. break;
  296. case A_CNTP_AIVAL_CTL:
  297. {
  298. uint32_t old_ctl = s->cntp_aival_ctl;
  299. /* EN bit is writable; CLR bit is write-0-to-clear, write-1-ignored */
  300. s->cntp_aival_ctl &= ~R_CNTP_AIVAL_CTL_EN_MASK;
  301. s->cntp_aival_ctl |= value & R_CNTP_AIVAL_CTL_EN_MASK;
  302. if (!(value & R_CNTP_AIVAL_CTL_CLR_MASK)) {
  303. s->cntp_aival_ctl &= ~R_CNTP_AIVAL_CTL_CLR_MASK;
  304. }
  305. if ((old_ctl ^ s->cntp_aival_ctl) & R_CNTP_AIVAL_CTL_EN_MASK) {
  306. /* Auto-increment toggled on/off */
  307. if (sse_enabled(s)) {
  308. if (sse_is_autoinc(s)) {
  309. sse_autoinc(s);
  310. } else {
  311. sse_recalc_timer(s);
  312. }
  313. }
  314. }
  315. sse_update_irq(s);
  316. break;
  317. }
  318. case A_CNTPCT_LO:
  319. case A_CNTPCT_HI:
  320. case A_CNTP_CFG:
  321. case A_CNTP_AIVAL_LO:
  322. case A_CNTP_AIVAL_HI:
  323. case A_PID4 ... A_CID3:
  324. qemu_log_mask(LOG_GUEST_ERROR,
  325. "SSE System Timer write: write to RO offset 0x%x\n",
  326. (unsigned)offset);
  327. break;
  328. default:
  329. qemu_log_mask(LOG_GUEST_ERROR,
  330. "SSE System Timer write: bad offset 0x%x\n",
  331. (unsigned)offset);
  332. break;
  333. }
  334. }
  335. static const MemoryRegionOps sse_timer_ops = {
  336. .read = sse_timer_read,
  337. .write = sse_timer_write,
  338. .endianness = DEVICE_LITTLE_ENDIAN,
  339. .valid.min_access_size = 4,
  340. .valid.max_access_size = 4,
  341. };
  342. static void sse_timer_reset(DeviceState *dev)
  343. {
  344. SSETimer *s = SSE_TIMER(dev);
  345. trace_sse_timer_reset();
  346. timer_del(&s->timer);
  347. s->cntfrq = 0;
  348. s->cntp_ctl = 0;
  349. s->cntp_cval = 0;
  350. s->cntp_aival = 0;
  351. s->cntp_aival_ctl = 0;
  352. s->cntp_aival_reload = 0;
  353. }
  354. static void sse_timer_counter_callback(Notifier *notifier, void *data)
  355. {
  356. SSETimer *s = container_of(notifier, SSETimer, counter_notifier);
  357. /* System counter told us we need to recalculate */
  358. if (sse_enabled(s)) {
  359. if (sse_is_autoinc(s)) {
  360. sse_set_timer(s, s->cntp_aival);
  361. } else {
  362. sse_recalc_timer(s);
  363. }
  364. }
  365. }
  366. static void sse_timer_init(Object *obj)
  367. {
  368. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  369. SSETimer *s = SSE_TIMER(obj);
  370. memory_region_init_io(&s->iomem, obj, &sse_timer_ops,
  371. s, "sse-timer", 0x1000);
  372. sysbus_init_mmio(sbd, &s->iomem);
  373. sysbus_init_irq(sbd, &s->irq);
  374. }
  375. static void sse_timer_realize(DeviceState *dev, Error **errp)
  376. {
  377. SSETimer *s = SSE_TIMER(dev);
  378. if (!s->counter) {
  379. error_setg(errp, "counter property was not set");
  380. return;
  381. }
  382. s->counter_notifier.notify = sse_timer_counter_callback;
  383. sse_counter_register_consumer(s->counter, &s->counter_notifier);
  384. timer_init_ns(&s->timer, QEMU_CLOCK_VIRTUAL, sse_timer_cb, s);
  385. }
  386. static const VMStateDescription sse_timer_vmstate = {
  387. .name = "sse-timer",
  388. .version_id = 1,
  389. .minimum_version_id = 1,
  390. .fields = (const VMStateField[]) {
  391. VMSTATE_TIMER(timer, SSETimer),
  392. VMSTATE_UINT32(cntfrq, SSETimer),
  393. VMSTATE_UINT32(cntp_ctl, SSETimer),
  394. VMSTATE_UINT64(cntp_cval, SSETimer),
  395. VMSTATE_UINT64(cntp_aival, SSETimer),
  396. VMSTATE_UINT32(cntp_aival_ctl, SSETimer),
  397. VMSTATE_UINT32(cntp_aival_reload, SSETimer),
  398. VMSTATE_END_OF_LIST()
  399. }
  400. };
  401. static const Property sse_timer_properties[] = {
  402. DEFINE_PROP_LINK("counter", SSETimer, counter, TYPE_SSE_COUNTER, SSECounter *),
  403. };
  404. static void sse_timer_class_init(ObjectClass *klass, void *data)
  405. {
  406. DeviceClass *dc = DEVICE_CLASS(klass);
  407. dc->realize = sse_timer_realize;
  408. dc->vmsd = &sse_timer_vmstate;
  409. device_class_set_legacy_reset(dc, sse_timer_reset);
  410. device_class_set_props(dc, sse_timer_properties);
  411. }
  412. static const TypeInfo sse_timer_info = {
  413. .name = TYPE_SSE_TIMER,
  414. .parent = TYPE_SYS_BUS_DEVICE,
  415. .instance_size = sizeof(SSETimer),
  416. .instance_init = sse_timer_init,
  417. .class_init = sse_timer_class_init,
  418. };
  419. static void sse_timer_register_types(void)
  420. {
  421. type_register_static(&sse_timer_info);
  422. }
  423. type_init(sse_timer_register_types);