2 * QEMU lowRISC Ibex Timer device
4 * Copyright (c) 2021 Western Digital
6 * For details check the documentation here:
7 * https://docs.opentitan.org/hw/ip/rv_timer/doc/
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "qemu/osdep.h"
30 #include "qemu/timer.h"
31 #include "hw/timer/ibex_timer.h"
33 #include "hw/qdev-properties.h"
34 #include "target/riscv/cpu.h"
35 #include "migration/vmstate.h"
38 FIELD(CTRL
, ACTIVE
, 0, 1)
40 FIELD(CFG0
, PRESCALE
, 0, 12)
41 FIELD(CFG0
, STEP
, 16, 8)
44 REG32(COMPARE_LOWER0
, 0x10C)
45 REG32(COMPARE_UPPER0
, 0x110)
46 REG32(INTR_ENABLE
, 0x114)
47 FIELD(INTR_ENABLE
, IE_0
, 0, 1)
48 REG32(INTR_STATE
, 0x118)
49 FIELD(INTR_STATE
, IS_0
, 0, 1)
50 REG32(INTR_TEST
, 0x11C)
51 FIELD(INTR_TEST
, T_0
, 0, 1)
53 static uint64_t cpu_riscv_read_rtc(uint32_t timebase_freq
)
55 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
56 timebase_freq
, NANOSECONDS_PER_SECOND
);
59 static void ibex_timer_update_irqs(IbexTimerState
*s
)
61 CPUState
*cs
= qemu_get_cpu(0);
62 RISCVCPU
*cpu
= RISCV_CPU(cs
);
63 uint64_t value
= s
->timer_compare_lower0
|
64 ((uint64_t)s
->timer_compare_upper0
<< 32);
66 uint64_t now
= cpu_riscv_read_rtc(s
->timebase_freq
);
68 if (!(s
->timer_ctrl
& R_CTRL_ACTIVE_MASK
)) {
69 /* Timer isn't active */
73 /* Update the CPUs mtimecmp */
74 cpu
->env
.timecmp
= value
;
76 if (cpu
->env
.timecmp
<= now
) {
78 * If the mtimecmp was in the past raise the interrupt now.
80 riscv_cpu_update_mip(cpu
, MIP_MTIP
, BOOL_TO_MASK(1));
81 if (s
->timer_intr_enable
& R_INTR_ENABLE_IE_0_MASK
) {
82 s
->timer_intr_state
|= R_INTR_STATE_IS_0_MASK
;
83 qemu_set_irq(s
->irq
, true);
88 /* Setup a timer to trigger the interrupt in the future */
89 riscv_cpu_update_mip(cpu
, MIP_MTIP
, BOOL_TO_MASK(0));
90 qemu_set_irq(s
->irq
, false);
92 diff
= cpu
->env
.timecmp
- now
;
93 next
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
95 NANOSECONDS_PER_SECOND
,
98 if (next
< qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)) {
99 /* We overflowed the timer, just set it as large as we can */
100 timer_mod(cpu
->env
.timer
, 0x7FFFFFFFFFFFFFFF);
102 timer_mod(cpu
->env
.timer
, next
);
106 static void ibex_timer_cb(void *opaque
)
108 IbexTimerState
*s
= opaque
;
109 CPUState
*cs
= qemu_get_cpu(0);
110 RISCVCPU
*cpu
= RISCV_CPU(cs
);
112 riscv_cpu_update_mip(cpu
, MIP_MTIP
, BOOL_TO_MASK(1));
113 if (s
->timer_intr_enable
& R_INTR_ENABLE_IE_0_MASK
) {
114 s
->timer_intr_state
|= R_INTR_STATE_IS_0_MASK
;
115 qemu_set_irq(s
->irq
, true);
119 static void ibex_timer_reset(DeviceState
*dev
)
121 IbexTimerState
*s
= IBEX_TIMER(dev
);
123 CPUState
*cpu
= qemu_get_cpu(0);
124 CPURISCVState
*env
= cpu
->env_ptr
;
125 env
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
129 s
->timer_ctrl
= 0x00000000;
130 s
->timer_cfg0
= 0x00010000;
131 s
->timer_compare_lower0
= 0xFFFFFFFF;
132 s
->timer_compare_upper0
= 0xFFFFFFFF;
133 s
->timer_intr_enable
= 0x00000000;
134 s
->timer_intr_state
= 0x00000000;
135 s
->timer_intr_test
= 0x00000000;
137 ibex_timer_update_irqs(s
);
140 static uint64_t ibex_timer_read(void *opaque
, hwaddr addr
,
143 IbexTimerState
*s
= opaque
;
144 uint64_t now
= cpu_riscv_read_rtc(s
->timebase_freq
);
145 uint64_t retvalue
= 0;
149 retvalue
= s
->timer_ctrl
;
152 retvalue
= s
->timer_cfg0
;
158 retvalue
= now
>> 32;
160 case R_COMPARE_LOWER0
:
161 retvalue
= s
->timer_compare_lower0
;
163 case R_COMPARE_UPPER0
:
164 retvalue
= s
->timer_compare_upper0
;
167 retvalue
= s
->timer_intr_enable
;
170 retvalue
= s
->timer_intr_state
;
173 retvalue
= s
->timer_intr_test
;
176 qemu_log_mask(LOG_GUEST_ERROR
,
177 "%s: Bad offset 0x%"HWADDR_PRIx
"\n", __func__
, addr
);
184 static void ibex_timer_write(void *opaque
, hwaddr addr
,
185 uint64_t val64
, unsigned int size
)
187 IbexTimerState
*s
= opaque
;
188 uint32_t val
= val64
;
195 qemu_log_mask(LOG_UNIMP
, "Changing prescale or step not supported");
199 qemu_log_mask(LOG_UNIMP
, "Changing timer value is not supported");
202 qemu_log_mask(LOG_UNIMP
, "Changing timer value is not supported");
204 case R_COMPARE_LOWER0
:
205 s
->timer_compare_lower0
= val
;
206 ibex_timer_update_irqs(s
);
208 case R_COMPARE_UPPER0
:
209 s
->timer_compare_upper0
= val
;
210 ibex_timer_update_irqs(s
);
213 s
->timer_intr_enable
= val
;
216 /* Write 1 to clear */
217 s
->timer_intr_state
&= ~val
;
220 s
->timer_intr_test
= val
;
221 if (s
->timer_intr_enable
&
223 R_INTR_ENABLE_IE_0_MASK
) {
224 s
->timer_intr_state
|= R_INTR_STATE_IS_0_MASK
;
225 qemu_set_irq(s
->irq
, true);
229 qemu_log_mask(LOG_GUEST_ERROR
,
230 "%s: Bad offset 0x%"HWADDR_PRIx
"\n", __func__
, addr
);
234 static const MemoryRegionOps ibex_timer_ops
= {
235 .read
= ibex_timer_read
,
236 .write
= ibex_timer_write
,
237 .endianness
= DEVICE_NATIVE_ENDIAN
,
238 .impl
.min_access_size
= 4,
239 .impl
.max_access_size
= 4,
242 static int ibex_timer_post_load(void *opaque
, int version_id
)
244 IbexTimerState
*s
= opaque
;
246 ibex_timer_update_irqs(s
);
250 static const VMStateDescription vmstate_ibex_timer
= {
251 .name
= TYPE_IBEX_TIMER
,
253 .minimum_version_id
= 1,
254 .post_load
= ibex_timer_post_load
,
255 .fields
= (VMStateField
[]) {
256 VMSTATE_UINT32(timer_ctrl
, IbexTimerState
),
257 VMSTATE_UINT32(timer_cfg0
, IbexTimerState
),
258 VMSTATE_UINT32(timer_compare_lower0
, IbexTimerState
),
259 VMSTATE_UINT32(timer_compare_upper0
, IbexTimerState
),
260 VMSTATE_UINT32(timer_intr_enable
, IbexTimerState
),
261 VMSTATE_UINT32(timer_intr_state
, IbexTimerState
),
262 VMSTATE_UINT32(timer_intr_test
, IbexTimerState
),
263 VMSTATE_END_OF_LIST()
267 static Property ibex_timer_properties
[] = {
268 DEFINE_PROP_UINT32("timebase-freq", IbexTimerState
, timebase_freq
, 10000),
269 DEFINE_PROP_END_OF_LIST(),
272 static void ibex_timer_init(Object
*obj
)
274 IbexTimerState
*s
= IBEX_TIMER(obj
);
276 sysbus_init_irq(SYS_BUS_DEVICE(obj
), &s
->irq
);
278 memory_region_init_io(&s
->mmio
, obj
, &ibex_timer_ops
, s
,
279 TYPE_IBEX_TIMER
, 0x400);
280 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->mmio
);
283 static void ibex_timer_class_init(ObjectClass
*klass
, void *data
)
285 DeviceClass
*dc
= DEVICE_CLASS(klass
);
287 dc
->reset
= ibex_timer_reset
;
288 dc
->vmsd
= &vmstate_ibex_timer
;
289 device_class_set_props(dc
, ibex_timer_properties
);
292 static const TypeInfo ibex_timer_info
= {
293 .name
= TYPE_IBEX_TIMER
,
294 .parent
= TYPE_SYS_BUS_DEVICE
,
295 .instance_size
= sizeof(IbexTimerState
),
296 .instance_init
= ibex_timer_init
,
297 .class_init
= ibex_timer_class_init
,
300 static void ibex_timer_register_types(void)
302 type_register_static(&ibex_timer_info
);
305 type_init(ibex_timer_register_types
)