aspeed_timer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * ASPEED AST2400 Timer
  3. *
  4. * Andrew Jeffery <andrew@aj.id.au>
  5. *
  6. * Copyright (C) 2016 IBM Corp.
  7. *
  8. * This code is licensed under the GPL version 2 or later. See
  9. * the COPYING file in the top-level directory.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "hw/sysbus.h"
  13. #include "hw/timer/aspeed_timer.h"
  14. #include "qemu-common.h"
  15. #include "qemu/bitops.h"
  16. #include "qemu/timer.h"
  17. #include "qemu/log.h"
  18. #include "trace.h"
  19. #define TIMER_NR_REGS 4
  20. #define TIMER_CTRL_BITS 4
  21. #define TIMER_CTRL_MASK ((1 << TIMER_CTRL_BITS) - 1)
  22. #define TIMER_CLOCK_USE_EXT true
  23. #define TIMER_CLOCK_EXT_HZ 1000000
  24. #define TIMER_CLOCK_USE_APB false
  25. #define TIMER_CLOCK_APB_HZ 24000000
  26. #define TIMER_REG_STATUS 0
  27. #define TIMER_REG_RELOAD 1
  28. #define TIMER_REG_MATCH_FIRST 2
  29. #define TIMER_REG_MATCH_SECOND 3
  30. #define TIMER_FIRST_CAP_PULSE 4
  31. enum timer_ctrl_op {
  32. op_enable = 0,
  33. op_external_clock,
  34. op_overflow_interrupt,
  35. op_pulse_enable
  36. };
  37. /**
  38. * Avoid mutual references between AspeedTimerCtrlState and AspeedTimer
  39. * structs, as it's a waste of memory. The ptimer BH callback needs to know
  40. * whether a specific AspeedTimer is enabled, but this information is held in
  41. * AspeedTimerCtrlState. So, provide a helper to hoist ourselves from an
  42. * arbitrary AspeedTimer to AspeedTimerCtrlState.
  43. */
  44. static inline AspeedTimerCtrlState *timer_to_ctrl(AspeedTimer *t)
  45. {
  46. const AspeedTimer (*timers)[] = (void *)t - (t->id * sizeof(*t));
  47. return container_of(timers, AspeedTimerCtrlState, timers);
  48. }
  49. static inline bool timer_ctrl_status(AspeedTimer *t, enum timer_ctrl_op op)
  50. {
  51. return !!(timer_to_ctrl(t)->ctrl & BIT(t->id * TIMER_CTRL_BITS + op));
  52. }
  53. static inline bool timer_enabled(AspeedTimer *t)
  54. {
  55. return timer_ctrl_status(t, op_enable);
  56. }
  57. static inline bool timer_overflow_interrupt(AspeedTimer *t)
  58. {
  59. return timer_ctrl_status(t, op_overflow_interrupt);
  60. }
  61. static inline bool timer_can_pulse(AspeedTimer *t)
  62. {
  63. return t->id >= TIMER_FIRST_CAP_PULSE;
  64. }
  65. static inline bool timer_external_clock(AspeedTimer *t)
  66. {
  67. return timer_ctrl_status(t, op_external_clock);
  68. }
  69. static uint32_t clock_rates[] = { TIMER_CLOCK_APB_HZ, TIMER_CLOCK_EXT_HZ };
  70. static inline uint32_t calculate_rate(struct AspeedTimer *t)
  71. {
  72. return clock_rates[timer_external_clock(t)];
  73. }
  74. static inline uint32_t calculate_ticks(struct AspeedTimer *t, uint64_t now_ns)
  75. {
  76. uint64_t delta_ns = now_ns - MIN(now_ns, t->start);
  77. uint32_t rate = calculate_rate(t);
  78. uint64_t ticks = muldiv64(delta_ns, rate, NANOSECONDS_PER_SECOND);
  79. return t->reload - MIN(t->reload, ticks);
  80. }
  81. static inline uint64_t calculate_time(struct AspeedTimer *t, uint32_t ticks)
  82. {
  83. uint64_t delta_ns;
  84. uint64_t delta_ticks;
  85. delta_ticks = t->reload - MIN(t->reload, ticks);
  86. delta_ns = muldiv64(delta_ticks, NANOSECONDS_PER_SECOND, calculate_rate(t));
  87. return t->start + delta_ns;
  88. }
  89. static uint64_t calculate_next(struct AspeedTimer *t)
  90. {
  91. uint64_t next = 0;
  92. uint32_t rate = calculate_rate(t);
  93. while (!next) {
  94. /* We don't know the relationship between the values in the match
  95. * registers, so sort using MAX/MIN/zero. We sort in that order as the
  96. * timer counts down to zero. */
  97. uint64_t seq[] = {
  98. calculate_time(t, MAX(t->match[0], t->match[1])),
  99. calculate_time(t, MIN(t->match[0], t->match[1])),
  100. calculate_time(t, 0),
  101. };
  102. uint64_t reload_ns;
  103. uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  104. if (now < seq[0]) {
  105. next = seq[0];
  106. } else if (now < seq[1]) {
  107. next = seq[1];
  108. } else if (now < seq[2]) {
  109. next = seq[2];
  110. } else if (t->reload) {
  111. reload_ns = muldiv64(t->reload, NANOSECONDS_PER_SECOND, rate);
  112. t->start = now - ((now - t->start) % reload_ns);
  113. } else {
  114. /* no reload value, return 0 */
  115. break;
  116. }
  117. }
  118. return next;
  119. }
  120. static void aspeed_timer_mod(AspeedTimer *t)
  121. {
  122. uint64_t next = calculate_next(t);
  123. if (next) {
  124. timer_mod(&t->timer, next);
  125. }
  126. }
  127. static void aspeed_timer_expire(void *opaque)
  128. {
  129. AspeedTimer *t = opaque;
  130. bool interrupt = false;
  131. uint32_t ticks;
  132. if (!timer_enabled(t)) {
  133. return;
  134. }
  135. ticks = calculate_ticks(t, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  136. if (!ticks) {
  137. interrupt = timer_overflow_interrupt(t) || !t->match[0] || !t->match[1];
  138. } else if (ticks <= MIN(t->match[0], t->match[1])) {
  139. interrupt = true;
  140. } else if (ticks <= MAX(t->match[0], t->match[1])) {
  141. interrupt = true;
  142. }
  143. if (interrupt) {
  144. t->level = !t->level;
  145. qemu_set_irq(t->irq, t->level);
  146. }
  147. aspeed_timer_mod(t);
  148. }
  149. static uint64_t aspeed_timer_get_value(AspeedTimer *t, int reg)
  150. {
  151. uint64_t value;
  152. switch (reg) {
  153. case TIMER_REG_STATUS:
  154. value = calculate_ticks(t, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  155. break;
  156. case TIMER_REG_RELOAD:
  157. value = t->reload;
  158. break;
  159. case TIMER_REG_MATCH_FIRST:
  160. case TIMER_REG_MATCH_SECOND:
  161. value = t->match[reg - 2];
  162. break;
  163. default:
  164. qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n",
  165. __func__, reg);
  166. value = 0;
  167. break;
  168. }
  169. return value;
  170. }
  171. static uint64_t aspeed_timer_read(void *opaque, hwaddr offset, unsigned size)
  172. {
  173. AspeedTimerCtrlState *s = opaque;
  174. const int reg = (offset & 0xf) / 4;
  175. uint64_t value;
  176. switch (offset) {
  177. case 0x30: /* Control Register */
  178. value = s->ctrl;
  179. break;
  180. case 0x34: /* Control Register 2 */
  181. value = s->ctrl2;
  182. break;
  183. case 0x00 ... 0x2c: /* Timers 1 - 4 */
  184. value = aspeed_timer_get_value(&s->timers[(offset >> 4)], reg);
  185. break;
  186. case 0x40 ... 0x8c: /* Timers 5 - 8 */
  187. value = aspeed_timer_get_value(&s->timers[(offset >> 4) - 1], reg);
  188. break;
  189. /* Illegal */
  190. case 0x38:
  191. case 0x3C:
  192. default:
  193. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  194. __func__, offset);
  195. value = 0;
  196. break;
  197. }
  198. trace_aspeed_timer_read(offset, size, value);
  199. return value;
  200. }
  201. static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg,
  202. uint32_t value)
  203. {
  204. AspeedTimer *t;
  205. uint32_t old_reload;
  206. trace_aspeed_timer_set_value(timer, reg, value);
  207. t = &s->timers[timer];
  208. switch (reg) {
  209. case TIMER_REG_RELOAD:
  210. old_reload = t->reload;
  211. t->reload = value;
  212. /* If the reload value was not previously set, or zero, and
  213. * the current value is valid, try to start the timer if it is
  214. * enabled.
  215. */
  216. if (old_reload || !t->reload) {
  217. break;
  218. }
  219. case TIMER_REG_STATUS:
  220. if (timer_enabled(t)) {
  221. uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  222. int64_t delta = (int64_t) value - (int64_t) calculate_ticks(t, now);
  223. uint32_t rate = calculate_rate(t);
  224. t->start += muldiv64(delta, NANOSECONDS_PER_SECOND, rate);
  225. aspeed_timer_mod(t);
  226. }
  227. break;
  228. case TIMER_REG_MATCH_FIRST:
  229. case TIMER_REG_MATCH_SECOND:
  230. t->match[reg - 2] = value;
  231. if (timer_enabled(t)) {
  232. aspeed_timer_mod(t);
  233. }
  234. break;
  235. default:
  236. qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n",
  237. __func__, reg);
  238. break;
  239. }
  240. }
  241. /* Control register operations are broken out into helpers that can be
  242. * explicitly called on aspeed_timer_reset(), but also from
  243. * aspeed_timer_ctrl_op().
  244. */
  245. static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable)
  246. {
  247. trace_aspeed_timer_ctrl_enable(t->id, enable);
  248. if (enable) {
  249. t->start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  250. aspeed_timer_mod(t);
  251. } else {
  252. timer_del(&t->timer);
  253. }
  254. }
  255. static void aspeed_timer_ctrl_external_clock(AspeedTimer *t, bool enable)
  256. {
  257. trace_aspeed_timer_ctrl_external_clock(t->id, enable);
  258. }
  259. static void aspeed_timer_ctrl_overflow_interrupt(AspeedTimer *t, bool enable)
  260. {
  261. trace_aspeed_timer_ctrl_overflow_interrupt(t->id, enable);
  262. }
  263. static void aspeed_timer_ctrl_pulse_enable(AspeedTimer *t, bool enable)
  264. {
  265. if (timer_can_pulse(t)) {
  266. trace_aspeed_timer_ctrl_pulse_enable(t->id, enable);
  267. } else {
  268. qemu_log_mask(LOG_GUEST_ERROR,
  269. "%s: Timer does not support pulse mode\n", __func__);
  270. }
  271. }
  272. /**
  273. * Given the actions are fixed in number and completely described in helper
  274. * functions, dispatch with a lookup table rather than manage control flow with
  275. * a switch statement.
  276. */
  277. static void (*const ctrl_ops[])(AspeedTimer *, bool) = {
  278. [op_enable] = aspeed_timer_ctrl_enable,
  279. [op_external_clock] = aspeed_timer_ctrl_external_clock,
  280. [op_overflow_interrupt] = aspeed_timer_ctrl_overflow_interrupt,
  281. [op_pulse_enable] = aspeed_timer_ctrl_pulse_enable,
  282. };
  283. /**
  284. * Conditionally affect changes chosen by a timer's control bit.
  285. *
  286. * The aspeed_timer_ctrl_op() interface is convenient for the
  287. * aspeed_timer_set_ctrl() function as the "no change" early exit can be
  288. * calculated for all operations, which cleans up the caller code. However the
  289. * interface isn't convenient for the reset function where we want to enter a
  290. * specific state without artificially constructing old and new values that
  291. * will fall through the change guard (and motivates extracting the actions
  292. * out to helper functions).
  293. *
  294. * @t: The timer to manipulate
  295. * @op: The type of operation to be performed
  296. * @old: The old state of the timer's control bits
  297. * @new: The incoming state for the timer's control bits
  298. */
  299. static void aspeed_timer_ctrl_op(AspeedTimer *t, enum timer_ctrl_op op,
  300. uint8_t old, uint8_t new)
  301. {
  302. const uint8_t mask = BIT(op);
  303. const bool enable = !!(new & mask);
  304. const bool changed = ((old ^ new) & mask);
  305. if (!changed) {
  306. return;
  307. }
  308. ctrl_ops[op](t, enable);
  309. }
  310. static void aspeed_timer_set_ctrl(AspeedTimerCtrlState *s, uint32_t reg)
  311. {
  312. int i;
  313. int shift;
  314. uint8_t t_old, t_new;
  315. AspeedTimer *t;
  316. const uint8_t enable_mask = BIT(op_enable);
  317. /* Handle a dependency between the 'enable' and remaining three
  318. * configuration bits - i.e. if more than one bit in the control set has
  319. * changed, including the 'enable' bit, then we want either disable the
  320. * timer and perform configuration, or perform configuration and then
  321. * enable the timer
  322. */
  323. for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
  324. t = &s->timers[i];
  325. shift = (i * TIMER_CTRL_BITS);
  326. t_old = (s->ctrl >> shift) & TIMER_CTRL_MASK;
  327. t_new = (reg >> shift) & TIMER_CTRL_MASK;
  328. /* If we are disabling, do so first */
  329. if ((t_old & enable_mask) && !(t_new & enable_mask)) {
  330. aspeed_timer_ctrl_enable(t, false);
  331. }
  332. aspeed_timer_ctrl_op(t, op_external_clock, t_old, t_new);
  333. aspeed_timer_ctrl_op(t, op_overflow_interrupt, t_old, t_new);
  334. aspeed_timer_ctrl_op(t, op_pulse_enable, t_old, t_new);
  335. /* If we are enabling, do so last */
  336. if (!(t_old & enable_mask) && (t_new & enable_mask)) {
  337. aspeed_timer_ctrl_enable(t, true);
  338. }
  339. }
  340. s->ctrl = reg;
  341. }
  342. static void aspeed_timer_set_ctrl2(AspeedTimerCtrlState *s, uint32_t value)
  343. {
  344. trace_aspeed_timer_set_ctrl2(value);
  345. }
  346. static void aspeed_timer_write(void *opaque, hwaddr offset, uint64_t value,
  347. unsigned size)
  348. {
  349. const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
  350. const int reg = (offset & 0xf) / 4;
  351. AspeedTimerCtrlState *s = opaque;
  352. switch (offset) {
  353. /* Control Registers */
  354. case 0x30:
  355. aspeed_timer_set_ctrl(s, tv);
  356. break;
  357. case 0x34:
  358. aspeed_timer_set_ctrl2(s, tv);
  359. break;
  360. /* Timer Registers */
  361. case 0x00 ... 0x2c:
  362. aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS), reg, tv);
  363. break;
  364. case 0x40 ... 0x8c:
  365. aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS) - 1, reg, tv);
  366. break;
  367. /* Illegal */
  368. case 0x38:
  369. case 0x3C:
  370. default:
  371. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  372. __func__, offset);
  373. break;
  374. }
  375. }
  376. static const MemoryRegionOps aspeed_timer_ops = {
  377. .read = aspeed_timer_read,
  378. .write = aspeed_timer_write,
  379. .endianness = DEVICE_LITTLE_ENDIAN,
  380. .valid.min_access_size = 4,
  381. .valid.max_access_size = 4,
  382. .valid.unaligned = false,
  383. };
  384. static void aspeed_init_one_timer(AspeedTimerCtrlState *s, uint8_t id)
  385. {
  386. AspeedTimer *t = &s->timers[id];
  387. t->id = id;
  388. timer_init_ns(&t->timer, QEMU_CLOCK_VIRTUAL, aspeed_timer_expire, t);
  389. }
  390. static void aspeed_timer_realize(DeviceState *dev, Error **errp)
  391. {
  392. int i;
  393. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  394. AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
  395. for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
  396. aspeed_init_one_timer(s, i);
  397. sysbus_init_irq(sbd, &s->timers[i].irq);
  398. }
  399. memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_timer_ops, s,
  400. TYPE_ASPEED_TIMER, 0x1000);
  401. sysbus_init_mmio(sbd, &s->iomem);
  402. }
  403. static void aspeed_timer_reset(DeviceState *dev)
  404. {
  405. int i;
  406. AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
  407. for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
  408. AspeedTimer *t = &s->timers[i];
  409. /* Explicitly call helpers to avoid any conditional behaviour through
  410. * aspeed_timer_set_ctrl().
  411. */
  412. aspeed_timer_ctrl_enable(t, false);
  413. aspeed_timer_ctrl_external_clock(t, TIMER_CLOCK_USE_APB);
  414. aspeed_timer_ctrl_overflow_interrupt(t, false);
  415. aspeed_timer_ctrl_pulse_enable(t, false);
  416. t->level = 0;
  417. t->reload = 0;
  418. t->match[0] = 0;
  419. t->match[1] = 0;
  420. }
  421. s->ctrl = 0;
  422. s->ctrl2 = 0;
  423. }
  424. static const VMStateDescription vmstate_aspeed_timer = {
  425. .name = "aspeed.timer",
  426. .version_id = 2,
  427. .minimum_version_id = 2,
  428. .fields = (VMStateField[]) {
  429. VMSTATE_UINT8(id, AspeedTimer),
  430. VMSTATE_INT32(level, AspeedTimer),
  431. VMSTATE_TIMER(timer, AspeedTimer),
  432. VMSTATE_UINT32(reload, AspeedTimer),
  433. VMSTATE_UINT32_ARRAY(match, AspeedTimer, 2),
  434. VMSTATE_END_OF_LIST()
  435. }
  436. };
  437. static const VMStateDescription vmstate_aspeed_timer_state = {
  438. .name = "aspeed.timerctrl",
  439. .version_id = 1,
  440. .minimum_version_id = 1,
  441. .fields = (VMStateField[]) {
  442. VMSTATE_UINT32(ctrl, AspeedTimerCtrlState),
  443. VMSTATE_UINT32(ctrl2, AspeedTimerCtrlState),
  444. VMSTATE_STRUCT_ARRAY(timers, AspeedTimerCtrlState,
  445. ASPEED_TIMER_NR_TIMERS, 1, vmstate_aspeed_timer,
  446. AspeedTimer),
  447. VMSTATE_END_OF_LIST()
  448. }
  449. };
  450. static void timer_class_init(ObjectClass *klass, void *data)
  451. {
  452. DeviceClass *dc = DEVICE_CLASS(klass);
  453. dc->realize = aspeed_timer_realize;
  454. dc->reset = aspeed_timer_reset;
  455. dc->desc = "ASPEED Timer";
  456. dc->vmsd = &vmstate_aspeed_timer_state;
  457. }
  458. static const TypeInfo aspeed_timer_info = {
  459. .name = TYPE_ASPEED_TIMER,
  460. .parent = TYPE_SYS_BUS_DEVICE,
  461. .instance_size = sizeof(AspeedTimerCtrlState),
  462. .class_init = timer_class_init,
  463. };
  464. static void aspeed_timer_register_types(void)
  465. {
  466. type_register_static(&aspeed_timer_info);
  467. }
  468. type_init(aspeed_timer_register_types)