aspeed_timer.c 16 KB

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