2 * Arm SSE Subsystem System Counter
4 * Copyright (c) 2020 Linaro Limited
5 * Written by Peter Maydell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 or
9 * (at your option) any later version.
13 * This is a model of the "System counter" which is documented in
14 * the Arm SSE-123 Example Subsystem Technical Reference Manual:
15 * https://developer.arm.com/documentation/101370/latest/
17 * The system counter is a non-stop 64-bit up-counter. It provides
18 * this count value to other devices like the SSE system timer,
19 * which are driven by this system timestamp rather than directly
20 * from a clock. Internally to the counter the count is actually
21 * 88-bit precision (64.24 fixed point), with a programmable scale factor.
23 * The hardware has the optional feature that it supports dynamic
24 * clock switching, where two clock inputs are connected, and which
25 * one is used is selected via a CLKSEL input signal. Since the
26 * users of this device in QEMU don't use this feature, we only model
27 * the HWCLKSW=0 configuration.
29 #include "qemu/osdep.h"
31 #include "qemu/timer.h"
32 #include "qapi/error.h"
34 #include "hw/timer/sse-counter.h"
35 #include "hw/sysbus.h"
36 #include "hw/registerfields.h"
38 #include "hw/qdev-clock.h"
39 #include "migration/vmstate.h"
41 /* Registers in the control frame */
43 FIELD(CNTCR
, EN
, 0, 1)
44 FIELD(CNTCR
, HDBG
, 1, 1)
45 FIELD(CNTCR
, SCEN
, 2, 1)
46 FIELD(CNTCR
, INTRMASK
, 3, 1)
47 FIELD(CNTCR
, PSLVERRDIS
, 4, 1)
48 FIELD(CNTCR
, INTRCLR
, 5, 1)
50 * Although CNTCR defines interrupt-related bits, the counter doesn't
51 * appear to actually have an interrupt output. So INTRCLR is
52 * effectively a RAZ/WI bit, as are the reserved bits [31:6].
54 #define CNTCR_VALID_MASK (R_CNTCR_EN_MASK | R_CNTCR_HDBG_MASK | \
55 R_CNTCR_SCEN_MASK | R_CNTCR_INTRMASK_MASK | \
56 R_CNTCR_PSLVERRDIS_MASK)
60 REG32(CNTSCR
, 0x10) /* Aliased with CNTSCR0 */
62 FIELD(CNTID
, CNTSC
, 0, 4)
63 FIELD(CNTID
, CNTCS
, 16, 1)
64 FIELD(CNTID
, CNTSELCLK
, 17, 2)
65 FIELD(CNTID
, CNTSCR_OVR
, 19, 1)
69 /* Registers in the status frame */
70 REG32(STATUS_CNTCV_LO
, 0x0)
71 REG32(STATUS_CNTCV_HI
, 0x4)
73 /* Standard ID registers, present in both frames */
88 static const int control_id
[] = {
89 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
90 0xba, 0xb0, 0x0b, 0x00, /* PID0..PID3 */
91 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
94 static const int status_id
[] = {
95 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
96 0xbb, 0xb0, 0x0b, 0x00, /* PID0..PID3 */
97 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
100 static void sse_counter_notify_users(SSECounter
*s
)
103 * Notify users of the count timestamp that they may
104 * need to recalculate.
106 notifier_list_notify(&s
->notifier_list
, NULL
);
109 static bool sse_counter_enabled(SSECounter
*s
)
111 return (s
->cntcr
& R_CNTCR_EN_MASK
) != 0;
114 uint64_t sse_counter_tick_to_time(SSECounter
*s
, uint64_t tick
)
116 if (!sse_counter_enabled(s
)) {
120 tick
-= s
->ticks_then
;
122 if (s
->cntcr
& R_CNTCR_SCEN_MASK
) {
123 /* Adjust the tick count to account for the scale factor */
124 tick
= muldiv64(tick
, 0x01000000, s
->cntscr0
);
127 return s
->ns_then
+ clock_ticks_to_ns(s
->clk
, tick
);
130 void sse_counter_register_consumer(SSECounter
*s
, Notifier
*notifier
)
133 * For the moment we assume that both we and the devices
134 * which consume us last for the life of the simulation,
135 * and so there is no mechanism for removing a notifier.
137 notifier_list_add(&s
->notifier_list
, notifier
);
140 uint64_t sse_counter_for_timestamp(SSECounter
*s
, uint64_t now
)
142 /* Return the CNTCV value for a particular timestamp (clock ns value). */
145 if (!sse_counter_enabled(s
)) {
146 /* Counter is disabled and does not increment */
147 return s
->ticks_then
;
150 ticks
= clock_ns_to_ticks(s
->clk
, now
- s
->ns_then
);
151 if (s
->cntcr
& R_CNTCR_SCEN_MASK
) {
153 * Scaling is enabled. The CNTSCR value is the amount added to
154 * the underlying 88-bit counter for every tick of the
155 * underlying clock; CNTCV is the top 64 bits of that full
156 * 88-bit value. Multiplying the tick count by CNTSCR tells us
157 * how much the full 88-bit counter has moved on; we then
158 * divide that by 0x01000000 to find out how much the 64-bit
159 * visible portion has advanced. muldiv64() gives us the
160 * necessary at-least-88-bit precision for the intermediate
163 ticks
= muldiv64(ticks
, s
->cntscr0
, 0x01000000);
165 return s
->ticks_then
+ ticks
;
168 static uint64_t sse_cntcv(SSECounter
*s
)
170 /* Return the CNTCV value for the current time */
171 return sse_counter_for_timestamp(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
174 static void sse_write_cntcv(SSECounter
*s
, uint32_t value
, unsigned startbit
)
177 * Write one 32-bit half of the counter value; startbit is the
178 * bit position of this half in the 64-bit word, either 0 or 32.
180 uint64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
181 uint64_t cntcv
= sse_counter_for_timestamp(s
, now
);
183 cntcv
= deposit64(cntcv
, startbit
, 32, value
);
184 s
->ticks_then
= cntcv
;
186 sse_counter_notify_users(s
);
189 static uint64_t sse_counter_control_read(void *opaque
, hwaddr offset
,
192 SSECounter
*s
= SSE_COUNTER(opaque
);
201 * The only bit here is DBGH, indicating that the counter has been
202 * halted via the Halt-on-Debug signal. We don't implement halting
203 * debug, so the whole register always reads as zero.
208 r
= extract64(sse_cntcv(s
), 0, 32);
211 r
= extract64(sse_cntcv(s
), 32, 32);
215 * For our implementation:
216 * - CNTSCR can only be written when CNTCR.EN == 0
217 * - HWCLKSW=0, so selected clock is always CLK0
218 * - counter scaling is implemented
220 r
= (1 << R_CNTID_CNTSELCLK_SHIFT
) | (1 << R_CNTID_CNTSC_SHIFT
);
227 /* If HWCLKSW == 0, CNTSCR1 is RAZ/WI */
230 case A_PID4
... A_CID3
:
231 r
= control_id
[(offset
- A_PID4
) / 4];
234 qemu_log_mask(LOG_GUEST_ERROR
,
235 "SSE System Counter control frame read: bad offset 0x%x",
241 trace_sse_counter_control_read(offset
, r
, size
);
245 static void sse_counter_control_write(void *opaque
, hwaddr offset
,
246 uint64_t value
, unsigned size
)
248 SSECounter
*s
= SSE_COUNTER(opaque
);
250 trace_sse_counter_control_write(offset
, value
, size
);
255 * Although CNTCR defines interrupt-related bits, the counter doesn't
256 * appear to actually have an interrupt output. So INTRCLR is
257 * effectively a RAZ/WI bit, as are the reserved bits [31:6].
258 * The documentation does not explicitly say so, but we assume
259 * that changing the scale factor while the counter is enabled
260 * by toggling CNTCR.SCEN has the same behaviour (making the counter
261 * value UNKNOWN) as changing it by writing to CNTSCR, and so we
262 * don't need to try to recalculate for that case.
264 value
&= CNTCR_VALID_MASK
;
265 if ((value
^ s
->cntcr
) & R_CNTCR_EN_MASK
) {
267 * Whether the counter is being enabled or disabled, the
268 * required action is the same: sync the (ns_then, ticks_then)
271 uint64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
272 s
->ticks_then
= sse_counter_for_timestamp(s
, now
);
274 sse_counter_notify_users(s
);
279 sse_write_cntcv(s
, value
, 0);
282 sse_write_cntcv(s
, value
, 32);
287 * If the scale registers are changed when the counter is enabled,
288 * the count value becomes UNKNOWN. So we don't try to recalculate
289 * anything here but only do it on a write to CNTCR.EN.
294 /* If HWCLKSW == 0, CNTSCR1 is RAZ/WI */
298 case A_PID4
... A_CID3
:
299 qemu_log_mask(LOG_GUEST_ERROR
,
300 "SSE System Counter control frame: write to RO offset 0x%x\n",
304 qemu_log_mask(LOG_GUEST_ERROR
,
305 "SSE System Counter control frame: write to bad offset 0x%x\n",
311 static uint64_t sse_counter_status_read(void *opaque
, hwaddr offset
,
314 SSECounter
*s
= SSE_COUNTER(opaque
);
318 case A_STATUS_CNTCV_LO
:
319 r
= extract64(sse_cntcv(s
), 0, 32);
321 case A_STATUS_CNTCV_HI
:
322 r
= extract64(sse_cntcv(s
), 32, 32);
324 case A_PID4
... A_CID3
:
325 r
= status_id
[(offset
- A_PID4
) / 4];
328 qemu_log_mask(LOG_GUEST_ERROR
,
329 "SSE System Counter status frame read: bad offset 0x%x",
335 trace_sse_counter_status_read(offset
, r
, size
);
339 static void sse_counter_status_write(void *opaque
, hwaddr offset
,
340 uint64_t value
, unsigned size
)
342 trace_sse_counter_status_write(offset
, value
, size
);
345 case A_STATUS_CNTCV_LO
:
346 case A_STATUS_CNTCV_HI
:
347 case A_PID4
... A_CID3
:
348 qemu_log_mask(LOG_GUEST_ERROR
,
349 "SSE System Counter status frame: write to RO offset 0x%x\n",
353 qemu_log_mask(LOG_GUEST_ERROR
,
354 "SSE System Counter status frame: write to bad offset 0x%x\n",
360 static const MemoryRegionOps sse_counter_control_ops
= {
361 .read
= sse_counter_control_read
,
362 .write
= sse_counter_control_write
,
363 .endianness
= DEVICE_LITTLE_ENDIAN
,
364 .valid
.min_access_size
= 4,
365 .valid
.max_access_size
= 4,
368 static const MemoryRegionOps sse_counter_status_ops
= {
369 .read
= sse_counter_status_read
,
370 .write
= sse_counter_status_write
,
371 .endianness
= DEVICE_LITTLE_ENDIAN
,
372 .valid
.min_access_size
= 4,
373 .valid
.max_access_size
= 4,
376 static void sse_counter_reset(DeviceState
*dev
)
378 SSECounter
*s
= SSE_COUNTER(dev
);
380 trace_sse_counter_reset();
383 s
->cntscr0
= 0x01000000;
384 s
->ns_then
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
388 static void sse_clk_callback(void *opaque
, ClockEvent event
)
390 SSECounter
*s
= SSE_COUNTER(opaque
);
396 * Before the clock period updates, set (ticks_then, ns_then)
397 * to the current time and tick count (as calculated with
398 * the old clock period).
400 if (sse_counter_enabled(s
)) {
401 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
402 s
->ticks_then
= sse_counter_for_timestamp(s
, now
);
407 sse_counter_notify_users(s
);
414 static void sse_counter_init(Object
*obj
)
416 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
417 SSECounter
*s
= SSE_COUNTER(obj
);
419 notifier_list_init(&s
->notifier_list
);
421 s
->clk
= qdev_init_clock_in(DEVICE(obj
), "CLK", sse_clk_callback
, s
,
422 ClockPreUpdate
| ClockUpdate
);
423 memory_region_init_io(&s
->control_mr
, obj
, &sse_counter_control_ops
,
424 s
, "sse-counter-control", 0x1000);
425 memory_region_init_io(&s
->status_mr
, obj
, &sse_counter_status_ops
,
426 s
, "sse-counter-status", 0x1000);
427 sysbus_init_mmio(sbd
, &s
->control_mr
);
428 sysbus_init_mmio(sbd
, &s
->status_mr
);
431 static void sse_counter_realize(DeviceState
*dev
, Error
**errp
)
433 SSECounter
*s
= SSE_COUNTER(dev
);
435 if (!clock_has_source(s
->clk
)) {
436 error_setg(errp
, "SSE system counter: CLK must be connected");
441 static const VMStateDescription sse_counter_vmstate
= {
442 .name
= "sse-counter",
444 .minimum_version_id
= 1,
445 .fields
= (VMStateField
[]) {
446 VMSTATE_CLOCK(clk
, SSECounter
),
447 VMSTATE_END_OF_LIST()
451 static void sse_counter_class_init(ObjectClass
*klass
, void *data
)
453 DeviceClass
*dc
= DEVICE_CLASS(klass
);
455 dc
->realize
= sse_counter_realize
;
456 dc
->vmsd
= &sse_counter_vmstate
;
457 dc
->reset
= sse_counter_reset
;
460 static const TypeInfo sse_counter_info
= {
461 .name
= TYPE_SSE_COUNTER
,
462 .parent
= TYPE_SYS_BUS_DEVICE
,
463 .instance_size
= sizeof(SSECounter
),
464 .instance_init
= sse_counter_init
,
465 .class_init
= sse_counter_class_init
,
468 static void sse_counter_register_types(void)
470 type_register_static(&sse_counter_info
);
473 type_init(sse_counter_register_types
);