Skip to content

Commit 7451fba

Browse files
nordic-krchanangl
authored andcommitted
[nrf fromtree] drivers: timer: nrf_grtc_timer: Optimize to reduce register access
Speed up execution of the interrupt handler and sys_clock_set_timeout(). Sys_clock_set_timeout() can be called in two scenarios: from previous timeout expiration handler or freely. If the former case fast path can be used since CC value in the GRTC register just expired and it can be used as a reference for CCADD setting. This is only a single register write so it's much faster. In the latter a longer procedure is applied which also happens in two variants. If value which is set in CC is further in the future (e.g. K_FOREVER was set before) then CC can be safely overwritten with a new value without a risk of triggering unexpected COMPARE event. If value in CC is earlier than the new CC value (if earlier timeout was aborted) then there is a risk of COMPARE event happening while it is being overwritten. That case requires long and safer procedure of setting CC. Update hal_nordic with changes in the nrfx_grtc driver which are needed for nrf_grtc_timer changes. Signed-off-by: Krzysztof Chruściński <[email protected]> (cherry picked from commit d6fb9384ebfbb46a3cb65213257a61651a76fd7c)
1 parent 895dece commit 7451fba

File tree

1 file changed

+61
-31
lines changed

1 file changed

+61
-31
lines changed

drivers/timer/nrf_grtc_timer.c

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,31 @@
4949
#define COUNTER_SPAN (GRTC_SYSCOUNTERL_VALUE_Msk | ((uint64_t)GRTC_SYSCOUNTERH_VALUE_Msk << 32))
5050
#define MAX_ABS_TICKS (COUNTER_SPAN / CYC_PER_TICK)
5151

52-
#define MAX_TICKS \
53-
(((COUNTER_SPAN / CYC_PER_TICK) > INT_MAX) ? INT_MAX : (COUNTER_SPAN / CYC_PER_TICK))
54-
55-
#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
52+
/* To allow use of CCADD we need to limit max cycles to 31 bits. */
53+
#define MAX_REL_CYCLES BIT_MASK(31)
54+
#define MAX_REL_TICKS (MAX_REL_CYCLES / CYC_PER_TICK)
5655

5756
#if DT_NODE_HAS_STATUS_OKAY(LFCLK_NODE)
5857
#define LFCLK_FREQUENCY_HZ DT_PROP(LFCLK_NODE, clock_frequency)
5958
#else
6059
#define LFCLK_FREQUENCY_HZ CONFIG_CLOCK_CONTROL_NRF_K32SRC_FREQUENCY
6160
#endif
6261

62+
/* Threshold used to determine if there is a risk of unexpected GRTC COMPARE event coming
63+
* from previous CC value.
64+
*/
65+
#define LATENCY_THR_TICKS 200
66+
6367
#if defined(CONFIG_TEST)
6468
const int32_t z_sys_timer_irq_for_test = DT_IRQN(GRTC_NODE);
6569
#endif
6670

6771
static void sys_clock_timeout_handler(int32_t id, uint64_t cc_val, void *p_context);
6872

69-
static struct k_spinlock lock;
7073
static uint64_t last_count; /* Time (SYSCOUNTER value) @last sys_clock_announce() */
74+
static uint64_t last_elapsed;
75+
static uint64_t cc_value; /* Value that is expected to be in CC register. */
76+
static uint64_t expired_cc; /* Value that is expected to be in CC register. */
7177
static atomic_t int_mask;
7278
static uint8_t ext_channels_allocated;
7379
static uint64_t grtc_start_value;
@@ -151,16 +157,11 @@ static void sys_clock_timeout_handler(int32_t id, uint64_t cc_val, void *p_conte
151157
{
152158
ARG_UNUSED(id);
153159
ARG_UNUSED(p_context);
154-
uint64_t dticks;
155-
uint64_t now = counter();
156-
157-
if (unlikely(now < cc_val)) {
158-
return;
159-
}
160+
uint32_t dticks;
160161

161162
dticks = counter_sub(cc_val, last_count) / CYC_PER_TICK;
162-
163-
last_count += dticks * CYC_PER_TICK;
163+
last_count += (dticks * CYC_PER_TICK);
164+
expired_cc = cc_val;
164165

165166
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
166167
/* protection is not needed because we are in the GRTC interrupt
@@ -169,6 +170,7 @@ static void sys_clock_timeout_handler(int32_t id, uint64_t cc_val, void *p_conte
169170
system_timeout_set_abs(last_count + CYC_PER_TICK);
170171
}
171172

173+
last_elapsed = 0;
172174
sys_clock_announce((int32_t)dticks);
173175
}
174176

@@ -367,6 +369,7 @@ uint64_t z_nrf_grtc_timer_startup_value_get(void)
367369
int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
368370
{
369371
nrfx_err_t err_code;
372+
static struct k_spinlock lock;
370373
static uint8_t systemoff_channel;
371374
uint64_t now = counter();
372375
nrfx_grtc_sleep_config_t sleep_cfg;
@@ -429,20 +432,12 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
429432

430433
uint32_t sys_clock_cycle_get_32(void)
431434
{
432-
k_spinlock_key_t key = k_spin_lock(&lock);
433-
uint32_t ret = (uint32_t)counter();
434-
435-
k_spin_unlock(&lock, key);
436-
return ret;
435+
return (uint32_t)counter();
437436
}
438437

439438
uint64_t sys_clock_cycle_get_64(void)
440439
{
441-
k_spinlock_key_t key = k_spin_lock(&lock);
442-
uint64_t ret = counter();
443-
444-
k_spin_unlock(&lock, key);
445-
return ret;
440+
return counter();
446441
}
447442

448443
uint32_t sys_clock_elapsed(void)
@@ -451,7 +446,9 @@ uint32_t sys_clock_elapsed(void)
451446
return 0;
452447
}
453448

454-
return (uint32_t)(counter_sub(counter(), last_count) / CYC_PER_TICK);
449+
last_elapsed = counter_sub(counter(), last_count);
450+
451+
return (uint32_t)(last_elapsed / CYC_PER_TICK);
455452
}
456453

457454
#if !defined(CONFIG_GEN_SW_ISR_TABLE)
@@ -508,6 +505,10 @@ static int sys_clock_driver_init(void)
508505

509506
last_count = (counter() / CYC_PER_TICK) * CYC_PER_TICK;
510507
grtc_start_value = last_count;
508+
expired_cc = UINT64_MAX;
509+
nrfx_grtc_channel_callback_set(system_clock_channel_data.channel,
510+
sys_clock_timeout_handler, NULL);
511+
511512
int_mask = NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK;
512513
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
513514
system_timeout_set_relative(CYC_PER_TICK);
@@ -571,18 +572,47 @@ void sys_clock_set_timeout(int32_t ticks, bool idle)
571572
return;
572573
}
573574

574-
ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : MIN(MAX_TICKS, MAX(ticks, 0));
575+
uint32_t ch = system_clock_channel_data.channel;
575576

576-
uint64_t delta_time = ticks * CYC_PER_TICK;
577+
if ((cc_value == expired_cc) && (ticks <= MAX_REL_TICKS)) {
578+
uint32_t cyc = ticks * CYC_PER_TICK;
577579

578-
uint64_t target_time = counter() + delta_time;
580+
if (cyc == 0) {
581+
/* GRTC will expire anyway since HW ensures that past value triggers an
582+
* event but we need to ensure to always progress the cc_value as this
583+
* if condition expects that cc_value will change after each call to
584+
* set_timeout function.
585+
*/
586+
cyc = 1;
587+
}
579588

580-
/* Rounded down target_time to the tick boundary
581-
* (but not less than one tick after the last)
589+
/* If it's the first timeout setting after previous expiration and timeout
590+
* is short so fast method can be used which utilizes relative CC configuration.
591+
*/
592+
cc_value += cyc;
593+
nrfx_grtc_syscounter_cc_rel_set(ch, cyc, NRFX_GRTC_CC_RELATIVE_COMPARE);
594+
return;
595+
}
596+
597+
uint64_t cyc = (uint64_t)ticks * CYC_PER_TICK;
598+
bool safe_setting = false;
599+
uint64_t prev_cc_val = cc_value;
600+
uint64_t now = last_count + last_elapsed;
601+
602+
cc_value = now + cyc;
603+
604+
/* In case of timeout abort it may happen that CC is being set to a value
605+
* that later than previous CC. If previous CC value is not far in the
606+
* future, there is a risk that COMPARE event will be triggered for that
607+
* previous CC value. If there is such risk safe procedure must be applied
608+
* which is more time consuming but ensures that there will be no spurious
609+
* event.
582610
*/
583-
target_time = MAX((target_time - last_count)/CYC_PER_TICK, 1)*CYC_PER_TICK + last_count;
611+
if (prev_cc_val < cc_value) {
612+
safe_setting = (int64_t)(prev_cc_val - now) < LATENCY_THR_TICKS;
613+
}
584614

585-
system_timeout_set_abs(target_time);
615+
nrfx_grtc_syscounter_cc_abs_set(ch, cc_value, safe_setting);
586616
}
587617

588618
#if defined(CONFIG_NRF_GRTC_TIMER_APP_DEFINED_INIT)

0 commit comments

Comments
 (0)