2
0

mips_gictimer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2016 Imagination Technologies
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/timer.h"
  10. #include "hw/timer/mips_gictimer.h"
  11. #define TIMER_PERIOD 10 /* 10 ns period for 100 Mhz frequency */
  12. uint32_t mips_gictimer_get_freq(MIPSGICTimerState *gic)
  13. {
  14. return NANOSECONDS_PER_SECOND / TIMER_PERIOD;
  15. }
  16. static void gic_vptimer_update(MIPSGICTimerState *gictimer,
  17. uint32_t vp_index, uint64_t now)
  18. {
  19. uint64_t next;
  20. uint32_t wait;
  21. wait = gictimer->vptimers[vp_index].comparelo - gictimer->sh_counterlo -
  22. (uint32_t)(now / TIMER_PERIOD);
  23. next = now + (uint64_t)wait * TIMER_PERIOD;
  24. timer_mod(gictimer->vptimers[vp_index].qtimer, next);
  25. }
  26. static void gic_vptimer_expire(MIPSGICTimerState *gictimer, uint32_t vp_index,
  27. uint64_t now)
  28. {
  29. if (gictimer->countstop) {
  30. /* timer stopped */
  31. return;
  32. }
  33. gictimer->cb(gictimer->opaque, vp_index);
  34. gic_vptimer_update(gictimer, vp_index, now);
  35. }
  36. static void gic_vptimer_cb(void *opaque)
  37. {
  38. MIPSGICTimerVPState *vptimer = opaque;
  39. MIPSGICTimerState *gictimer = vptimer->gictimer;
  40. gic_vptimer_expire(gictimer, vptimer->vp_index,
  41. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  42. }
  43. uint32_t mips_gictimer_get_sh_count(MIPSGICTimerState *gictimer)
  44. {
  45. int i;
  46. if (gictimer->countstop) {
  47. return gictimer->sh_counterlo;
  48. } else {
  49. uint64_t now;
  50. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  51. for (i = 0; i < gictimer->num_vps; i++) {
  52. if (timer_pending(gictimer->vptimers[i].qtimer)
  53. && timer_expired(gictimer->vptimers[i].qtimer, now)) {
  54. /* The timer has already expired. */
  55. gic_vptimer_expire(gictimer, i, now);
  56. }
  57. }
  58. return gictimer->sh_counterlo + (uint32_t)(now / TIMER_PERIOD);
  59. }
  60. }
  61. void mips_gictimer_store_sh_count(MIPSGICTimerState *gictimer, uint64_t count)
  62. {
  63. int i;
  64. uint64_t now;
  65. if (gictimer->countstop || !gictimer->vptimers[0].qtimer) {
  66. gictimer->sh_counterlo = count;
  67. } else {
  68. /* Store new count register */
  69. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  70. gictimer->sh_counterlo = count - (uint32_t)(now / TIMER_PERIOD);
  71. /* Update timer timer */
  72. for (i = 0; i < gictimer->num_vps; i++) {
  73. gic_vptimer_update(gictimer, i, now);
  74. }
  75. }
  76. }
  77. uint32_t mips_gictimer_get_vp_compare(MIPSGICTimerState *gictimer,
  78. uint32_t vp_index)
  79. {
  80. return gictimer->vptimers[vp_index].comparelo;
  81. }
  82. void mips_gictimer_store_vp_compare(MIPSGICTimerState *gictimer,
  83. uint32_t vp_index, uint64_t compare)
  84. {
  85. gictimer->vptimers[vp_index].comparelo = (uint32_t) compare;
  86. gic_vptimer_update(gictimer, vp_index,
  87. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  88. }
  89. uint8_t mips_gictimer_get_countstop(MIPSGICTimerState *gictimer)
  90. {
  91. return gictimer->countstop;
  92. }
  93. void mips_gictimer_start_count(MIPSGICTimerState *gictimer)
  94. {
  95. gictimer->countstop = 0;
  96. mips_gictimer_store_sh_count(gictimer, gictimer->sh_counterlo);
  97. }
  98. void mips_gictimer_stop_count(MIPSGICTimerState *gictimer)
  99. {
  100. int i;
  101. gictimer->countstop = 1;
  102. /* Store the current value */
  103. gictimer->sh_counterlo +=
  104. (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / TIMER_PERIOD);
  105. for (i = 0; i < gictimer->num_vps; i++) {
  106. timer_del(gictimer->vptimers[i].qtimer);
  107. }
  108. }
  109. MIPSGICTimerState *mips_gictimer_init(void *opaque, uint32_t nvps,
  110. MIPSGICTimerCB *cb)
  111. {
  112. int i;
  113. MIPSGICTimerState *gictimer = g_new(MIPSGICTimerState, 1);
  114. gictimer->vptimers = g_new(MIPSGICTimerVPState, nvps);
  115. gictimer->countstop = 1;
  116. gictimer->num_vps = nvps;
  117. gictimer->opaque = opaque;
  118. gictimer->cb = cb;
  119. for (i = 0; i < nvps; i++) {
  120. gictimer->vptimers[i].gictimer = gictimer;
  121. gictimer->vptimers[i].vp_index = i;
  122. gictimer->vptimers[i].qtimer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  123. &gic_vptimer_cb,
  124. &gictimer->vptimers[i]);
  125. }
  126. return gictimer;
  127. }