aspeed_timer.c 15 KB

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