cputimer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * QEMU OpenRISC timer support
  3. *
  4. * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
  5. * Zhizhou Zhang <etouzh@gmail.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "cpu.h"
  22. #include "migration/vmstate.h"
  23. #include "qemu/timer.h"
  24. #define TIMER_PERIOD 50 /* 50 ns period for 20 MHz timer */
  25. /* Tick Timer global state to allow all cores to be in sync */
  26. typedef struct OR1KTimerState {
  27. uint32_t ttcr;
  28. uint64_t last_clk;
  29. } OR1KTimerState;
  30. static OR1KTimerState *or1k_timer;
  31. void cpu_openrisc_count_set(OpenRISCCPU *cpu, uint32_t val)
  32. {
  33. or1k_timer->ttcr = val;
  34. }
  35. uint32_t cpu_openrisc_count_get(OpenRISCCPU *cpu)
  36. {
  37. return or1k_timer->ttcr;
  38. }
  39. /* Add elapsed ticks to ttcr */
  40. void cpu_openrisc_count_update(OpenRISCCPU *cpu)
  41. {
  42. uint64_t now;
  43. if (!cpu->env.is_counting) {
  44. return;
  45. }
  46. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  47. or1k_timer->ttcr += (uint32_t)((now - or1k_timer->last_clk)
  48. / TIMER_PERIOD);
  49. or1k_timer->last_clk = now;
  50. }
  51. /* Update the next timeout time as difference between ttmr and ttcr */
  52. void cpu_openrisc_timer_update(OpenRISCCPU *cpu)
  53. {
  54. uint32_t wait;
  55. uint64_t now, next;
  56. if (!cpu->env.is_counting) {
  57. return;
  58. }
  59. cpu_openrisc_count_update(cpu);
  60. now = or1k_timer->last_clk;
  61. if ((cpu->env.ttmr & TTMR_TP) <= (or1k_timer->ttcr & TTMR_TP)) {
  62. wait = TTMR_TP - (or1k_timer->ttcr & TTMR_TP) + 1;
  63. wait += cpu->env.ttmr & TTMR_TP;
  64. } else {
  65. wait = (cpu->env.ttmr & TTMR_TP) - (or1k_timer->ttcr & TTMR_TP);
  66. }
  67. next = now + (uint64_t)wait * TIMER_PERIOD;
  68. timer_mod(cpu->env.timer, next);
  69. }
  70. void cpu_openrisc_count_start(OpenRISCCPU *cpu)
  71. {
  72. cpu->env.is_counting = 1;
  73. cpu_openrisc_count_update(cpu);
  74. }
  75. void cpu_openrisc_count_stop(OpenRISCCPU *cpu)
  76. {
  77. timer_del(cpu->env.timer);
  78. cpu_openrisc_count_update(cpu);
  79. cpu->env.is_counting = 0;
  80. }
  81. static void openrisc_timer_cb(void *opaque)
  82. {
  83. OpenRISCCPU *cpu = opaque;
  84. if ((cpu->env.ttmr & TTMR_IE) &&
  85. timer_expired(cpu->env.timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL))) {
  86. CPUState *cs = CPU(cpu);
  87. cpu->env.ttmr |= TTMR_IP;
  88. cs->interrupt_request |= CPU_INTERRUPT_TIMER;
  89. }
  90. switch (cpu->env.ttmr & TTMR_M) {
  91. case TIMER_NONE:
  92. break;
  93. case TIMER_INTR:
  94. or1k_timer->ttcr = 0;
  95. break;
  96. case TIMER_SHOT:
  97. cpu_openrisc_count_stop(cpu);
  98. break;
  99. case TIMER_CONT:
  100. break;
  101. }
  102. cpu_openrisc_timer_update(cpu);
  103. qemu_cpu_kick(CPU(cpu));
  104. }
  105. static const VMStateDescription vmstate_or1k_timer = {
  106. .name = "or1k_timer",
  107. .version_id = 1,
  108. .minimum_version_id = 1,
  109. .fields = (VMStateField[]) {
  110. VMSTATE_UINT32(ttcr, OR1KTimerState),
  111. VMSTATE_UINT64(last_clk, OR1KTimerState),
  112. VMSTATE_END_OF_LIST()
  113. }
  114. };
  115. void cpu_openrisc_clock_init(OpenRISCCPU *cpu)
  116. {
  117. cpu->env.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &openrisc_timer_cb, cpu);
  118. cpu->env.ttmr = 0x00000000;
  119. if (or1k_timer == NULL) {
  120. or1k_timer = g_new0(OR1KTimerState, 1);
  121. vmstate_register(NULL, 0, &vmstate_or1k_timer, or1k_timer);
  122. }
  123. }