Use small lock to protect resources related to timers in arch risc-v, xtensa and tricore.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit is contained in:
wangzhi16 2025-01-20 16:40:43 +08:00 committed by Alan C. Assis
parent 4b91d76869
commit ecaddbb0aa
6 changed files with 136 additions and 83 deletions

View file

@ -34,6 +34,7 @@
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sched.h>
#include <nuttx/nuttx.h>
#include <nuttx/irq.h>
@ -218,7 +219,8 @@ static int IRAM_ATTR esp_hr_timer_isr(int irq, void *context, void *arg)
systimer_ll_clear_alarm_int(priv->hal.dev, SYSTIMER_ALARM_ESPTIMER);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
sched_lock();
/* Check if there is a timer running */
@ -288,7 +290,8 @@ static int IRAM_ATTR esp_hr_timer_isr(int irq, void *context, void *arg)
}
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
return OK;
}
@ -710,7 +713,7 @@ void IRAM_ATTR esp_hr_timer_calibration(uint64_t time_us)
int esp_hr_timer_init(void)
{
struct esp_hr_timer_context_s *priv;
struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
int pid;
if (g_hr_timer_initialized)
@ -720,6 +723,8 @@ int esp_hr_timer_init(void)
return OK;
}
spin_lock_init(&priv->lock);
pid = kthread_create(CONFIG_ESPRESSIF_HR_TIMER_TASK_NAME,
CONFIG_ESPRESSIF_HR_TIMER_TASK_PRIORITY,
CONFIG_ESPRESSIF_HR_TIMER_TASK_STACK_SIZE,
@ -732,8 +737,6 @@ int esp_hr_timer_init(void)
return pid;
}
priv = &g_hr_timer_context;
list_initialize(&priv->runlist);
list_initialize(&priv->toutlist);

View file

@ -34,9 +34,10 @@
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <sched.h>
#include <nuttx/nuttx.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/kthread.h>
#include <nuttx/kmalloc.h>
#include <nuttx/semaphore.h>
@ -76,6 +77,7 @@ struct esp32c3_rt_priv_s
struct list_node runlist;
struct list_node toutlist;
struct esp32c3_tim_dev_s *timer;
spinlock_t lock;
};
/****************************************************************************
@ -109,18 +111,15 @@ static struct esp32c3_rt_priv_s g_rt_priv =
*
****************************************************************************/
static void start_rt_timer(struct rt_timer_s *timer,
uint64_t timeout,
bool repeat)
static void start_rt_timer_nolock(struct rt_timer_s *timer,
uint64_t timeout,
bool repeat)
{
irqstate_t flags;
struct rt_timer_s *p;
bool inserted = false;
uint64_t counter;
struct esp32c3_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
/* Only idle timer can be started */
if (timer->state == RT_TIMER_IDLE)
@ -178,8 +177,18 @@ static void start_rt_timer(struct rt_timer_s *timer,
ESP32C3_TIM_SETALRM(priv->timer, true);
}
}
}
leave_critical_section(flags);
static void start_rt_timer(struct rt_timer_s *timer,
uint64_t timeout,
bool repeat)
{
irqstate_t flags;
struct esp32c3_rt_priv_s *priv = &g_rt_priv;
flags = spin_lock_irqsave(&priv->lock);
start_rt_timer_nolock(timer, timeout, repeat);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@ -197,16 +206,13 @@ static void start_rt_timer(struct rt_timer_s *timer,
*
****************************************************************************/
static void stop_rt_timer(struct rt_timer_s *timer)
static void stop_rt_timer_nolock(struct rt_timer_s *timer)
{
irqstate_t flags;
bool ishead;
struct rt_timer_s *next_timer;
uint64_t alarm;
struct esp32c3_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
/* "start" function can set the timer's repeat flag, and "stop" function
* should remove this flag.
*/
@ -253,8 +259,16 @@ static void stop_rt_timer(struct rt_timer_s *timer)
}
}
}
}
leave_critical_section(flags);
static void stop_rt_timer(struct rt_timer_s *timer)
{
irqstate_t flags;
struct esp32c3_rt_priv_s *priv = &g_rt_priv;
flags = spin_lock_irqsave(&priv->lock);
stop_rt_timer_nolock(timer);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@ -280,11 +294,12 @@ static void delete_rt_timer(struct rt_timer_s *timer)
struct esp32c3_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
sched_lock();
if (timer->state == RT_TIMER_READY)
{
stop_rt_timer(timer);
stop_rt_timer_nolock(timer);
}
else if (timer->state == RT_TIMER_TIMEOUT)
{
@ -307,7 +322,8 @@ static void delete_rt_timer(struct rt_timer_s *timer)
}
exit:
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
}
/****************************************************************************
@ -345,7 +361,7 @@ static int rt_timer_thread(int argc, char *argv[])
ASSERT(0);
}
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
/* Process all the timers in list */
@ -368,7 +384,7 @@ static int rt_timer_thread(int argc, char *argv[])
timer->state = RT_TIMER_IDLE;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
if (raw_state == RT_TIMER_TIMEOUT)
{
@ -381,7 +397,7 @@ static int rt_timer_thread(int argc, char *argv[])
/* Enter critical section for next scanning list */
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
if (raw_state == RT_TIMER_TIMEOUT)
{
@ -389,12 +405,12 @@ static int rt_timer_thread(int argc, char *argv[])
if (timer->flags & RT_TIMER_REPEAT)
{
start_rt_timer(timer, timer->timeout, true);
start_rt_timer_nolock(timer, timer->timeout, true);
}
}
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
return 0;
@ -430,7 +446,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
ESP32C3_TIM_ACKINT(priv->timer);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
sched_lock();
/* Check if there is a timer running */
@ -489,7 +506,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
}
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
return 0;
}
@ -645,7 +663,7 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
struct esp32c3_rt_priv_s *priv = &g_rt_priv;
uint64_t alarm_value = 0;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
ESP32C3_TIM_GETCTR(priv->timer, &counter);
counter = CYCLES_TO_USEC(counter);
@ -661,7 +679,7 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
alarm_value -= counter;
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return alarm_value;
}
@ -686,13 +704,13 @@ void IRAM_ATTR rt_timer_calibration(uint64_t time_us)
struct esp32c3_rt_priv_s *priv = &g_rt_priv;
irqstate_t flags;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
ESP32C3_TIM_GETCTR(priv->timer, &counter);
counter = CYCLES_TO_USEC(counter);
counter += time_us;
ESP32C3_TIM_SETCTR(priv->timer, USEC_TO_CYCLES(counter));
ESP32C3_TIM_RLD_NOW(priv->timer);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@ -715,6 +733,7 @@ int esp32c3_rt_timer_init(void)
irqstate_t flags;
struct esp32c3_rt_priv_s *priv = &g_rt_priv;
spin_lock_init(&priv->lock);
priv->timer = esp32c3_tim_init(ESP32C3_RT_TIMER);
if (priv->timer == NULL)
{
@ -739,7 +758,7 @@ int esp32c3_rt_timer_init(void)
priv->pid = (pid_t)pid;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
/* ESP32-C3 hardware timer configuration:
* 1 count = 1/16 us
@ -756,7 +775,7 @@ int esp32c3_rt_timer_init(void)
ESP32C3_TIM_ENABLEINT(priv->timer);
ESP32C3_TIM_START(priv->timer);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return 0;
}
@ -780,7 +799,7 @@ void esp32c3_rt_timer_deinit(void)
irqstate_t flags;
struct esp32c3_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
ESP32C3_TIM_STOP(priv->timer);
ESP32C3_TIM_DISABLEINT(priv->timer);
@ -788,7 +807,7 @@ void esp32c3_rt_timer_deinit(void)
esp32c3_tim_deinit(priv->timer);
priv->timer = NULL;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
if (priv->pid != INVALID_PROCESS_ID)
{

View file

@ -24,7 +24,7 @@
* Included Files
****************************************************************************/
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/kmalloc.h>
#include <nuttx/timers/oneshot.h>
@ -51,6 +51,7 @@ struct tricore_systimer_lowerhalf_s
uint64_t alarm;
oneshot_callback_t callback;
void *arg;
spinlock_t lock;
};
/****************************************************************************
@ -94,11 +95,11 @@ tricore_systimer_get_time(struct tricore_systimer_lowerhalf_s *priv)
irqstate_t flags;
uint64_t ticks;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
ticks = IfxStm_get(priv->tbase);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return ticks;
}
@ -109,11 +110,11 @@ tricore_systimer_set_timecmp(struct tricore_systimer_lowerhalf_s *priv,
{
irqstate_t flags;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
IfxStm_updateCompare(priv->tbase, IfxStm_Comparator_0, value);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@ -315,6 +316,7 @@ tricore_systimer_initialize(volatile void *tbase, int irq, uint64_t freq)
priv->tbase = tbase;
priv->freq = freq;
spin_lock_init(&priv->lock);
IfxStm_setCompareControl(tbase,
IfxStm_Comparator_0,

View file

@ -263,7 +263,7 @@ static void delete_rt_timer(struct rt_timer_s *timer)
irqstate_t flags;
struct esp32_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
if (timer->state == RT_TIMER_READY)
{
@ -282,7 +282,7 @@ static void delete_rt_timer(struct rt_timer_s *timer)
timer->state = RT_TIMER_DELETE;
exit:
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@ -684,6 +684,7 @@ int esp32_rt_timer_init(void)
struct esp32_tim_dev_s *tim;
struct esp32_rt_priv_s *priv = &g_rt_priv;
spin_lock_init(&priv->lock);
tim = esp32_tim_init(ESP32_RT_TIMER);
if (!tim)
{
@ -709,7 +710,7 @@ int esp32_rt_timer_init(void)
priv->timer = tim;
priv->pid = (pid_t)pid;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
/* ESP32 hardware timer configuration:
* - 1 counter = 1us
@ -727,7 +728,7 @@ int esp32_rt_timer_init(void)
ESP32_TIM_START(tim);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return 0;
}

View file

@ -32,9 +32,10 @@
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <sched.h>
#include <nuttx/nuttx.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/kthread.h>
#include <nuttx/kmalloc.h>
#include <nuttx/semaphore.h>
@ -80,6 +81,7 @@ struct esp32s2_rt_priv_s
struct list_node runlist;
struct list_node toutlist;
struct esp32s2_tim_dev_s *timer;
spinlock_t lock;
};
/****************************************************************************
@ -114,18 +116,15 @@ static struct esp32s2_rt_priv_s g_rt_priv =
*
****************************************************************************/
static void start_rt_timer(struct rt_timer_s *timer,
uint64_t timeout,
bool repeat)
static void start_rt_timer_nolock(struct rt_timer_s *timer,
uint64_t timeout,
bool repeat)
{
irqstate_t flags;
struct rt_timer_s *temp_p;
bool inserted = false;
uint64_t counter;
struct esp32s2_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
/* Only idle timer can be started */
if (timer->state == RT_TIMER_IDLE)
@ -188,8 +187,20 @@ static void start_rt_timer(struct rt_timer_s *timer,
tmrwarn("WARN: Timer not in idle mode.\n"\
"Only idle timer can be started!\n");
}
}
leave_critical_section(flags);
static void start_rt_timer(struct rt_timer_s *timer,
uint64_t timeout,
bool repeat)
{
irqstate_t flags;
struct esp32s2_rt_priv_s *priv = &g_rt_priv;
flags = spin_lock_irqsave(&priv->lock);
sched_lock();
start_rt_timer_nolock(timer, timeout, repeat);
spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
}
/****************************************************************************
@ -207,16 +218,13 @@ static void start_rt_timer(struct rt_timer_s *timer,
*
****************************************************************************/
static void stop_rt_timer(struct rt_timer_s *timer)
static void stop_rt_timer_nolock(struct rt_timer_s *timer)
{
irqstate_t flags;
bool ishead;
struct rt_timer_s *next_timer;
uint64_t alarm;
struct esp32s2_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
/* "start" function can set the timer's repeat flag, and "stop" function
* should remove this flag.
*/
@ -263,8 +271,16 @@ static void stop_rt_timer(struct rt_timer_s *timer)
}
}
}
}
leave_critical_section(flags);
static void stop_rt_timer(struct rt_timer_s *timer)
{
irqstate_t flags;
struct esp32s2_rt_priv_s *priv = &g_rt_priv;
flags = spin_lock_irqsave(&priv->lock);
stop_rt_timer_nolock(timer);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@ -289,11 +305,12 @@ static void delete_rt_timer(struct rt_timer_s *timer)
irqstate_t flags;
struct esp32s2_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
sched_lock();
if (timer->state == RT_TIMER_READY)
{
stop_rt_timer(timer);
stop_rt_timer_nolock(timer);
}
else if (timer->state == RT_TIMER_TIMEOUT)
{
@ -316,7 +333,8 @@ static void delete_rt_timer(struct rt_timer_s *timer)
}
exit:
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
}
/****************************************************************************
@ -354,7 +372,7 @@ static int rt_timer_thread(int argc, char *argv[])
ASSERT(0);
}
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
/* Process all the timers in list */
@ -377,7 +395,7 @@ static int rt_timer_thread(int argc, char *argv[])
timer->state = RT_TIMER_IDLE;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
if (raw_state == RT_TIMER_TIMEOUT)
{
@ -390,7 +408,7 @@ static int rt_timer_thread(int argc, char *argv[])
/* Enter critical section for next scanning list */
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
if (raw_state == RT_TIMER_TIMEOUT)
{
@ -398,12 +416,12 @@ static int rt_timer_thread(int argc, char *argv[])
if (timer->flags & RT_TIMER_REPEAT)
{
start_rt_timer(timer, timer->timeout, true);
start_rt_timer_nolock(timer, timer->timeout, true);
}
}
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
return 0;
@ -439,7 +457,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
ESP32S2_TIM_ACKINT(priv->timer);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
sched_lock();
/* Check if there is a timer running */
@ -498,7 +517,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
}
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
return 0;
}
@ -654,7 +674,7 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
struct esp32s2_rt_priv_s *priv = &g_rt_priv;
uint64_t alarm_value = 0;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
ESP32S2_TIM_GETCTR(priv->timer, &counter);
counter = CYCLES_TO_USEC(counter);
@ -670,7 +690,7 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
alarm_value -= counter;
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return alarm_value;
}
@ -695,13 +715,13 @@ void IRAM_ATTR rt_timer_calibration(uint64_t time_us)
struct esp32s2_rt_priv_s *priv = &g_rt_priv;
irqstate_t flags;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
ESP32S2_TIM_GETCTR(priv->timer, &counter);
counter = CYCLES_TO_USEC(counter);
counter += time_us;
ESP32S2_TIM_SETCTR(priv->timer, USEC_TO_CYCLES(counter));
ESP32S2_TIM_RLD_NOW(priv->timer);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@ -728,6 +748,7 @@ int esp32s2_rt_timer_init(void)
uint16_t pre;
uint16_t ticks;
spin_lock_init(&priv->lock);
tim = esp32s2_tim_init(SYSTIMER_COMP0);
if (tim == NULL)
@ -754,7 +775,7 @@ int esp32s2_rt_timer_init(void)
priv->pid = (pid_t)pid;
priv->timer = tim;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
/* ESP32-S2 hardware timer configuration, acc. TRM V1.0
* Systimer is clocked by APB_CLK.
@ -798,7 +819,7 @@ int esp32s2_rt_timer_init(void)
ESP32S2_TIM_SETISR(priv->timer, rt_timer_isr, NULL);
ESP32S2_TIM_ENABLEINT(priv->timer);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return 0;
}
@ -822,14 +843,14 @@ void esp32s2_rt_timer_deinit(void)
irqstate_t flags;
struct esp32s2_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
ESP32S2_TIM_DISABLEINT(priv->timer);
ESP32S2_TIM_SETISR(priv->timer, NULL, NULL);
esp32s2_tim_deinit(priv->timer);
priv->timer = NULL;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
if (priv->pid != INVALID_PROCESS_ID)
{

View file

@ -32,9 +32,10 @@
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sched.h>
#include <nuttx/nuttx.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/kmalloc.h>
#include <nuttx/kthread.h>
#include <nuttx/semaphore.h>
@ -611,7 +612,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
modifyreg32(SYSTIMER_INT_CLR_REG, 0, SYSTIMER_TARGET2_INT_CLR);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
sched_lock();
/* Check if there is a timer running */
@ -672,7 +674,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
}
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
return OK;
}
@ -805,7 +808,8 @@ void esp32s3_rt_timer_delete(struct rt_timer_s *timer)
irqstate_t flags;
struct esp32s3_rt_priv_s *priv = &g_rt_priv;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
sched_lock();
if (timer->state == RT_TIMER_READY)
{
@ -832,7 +836,8 @@ void esp32s3_rt_timer_delete(struct rt_timer_s *timer)
}
exit:
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
}
/****************************************************************************
@ -949,6 +954,8 @@ int esp32s3_rt_timer_init(void)
irqstate_t flags;
struct esp32s3_rt_priv_s *priv = &g_rt_priv;
spin_lock_init(&priv->lock);
pid = kthread_create(RT_TIMER_TASK_NAME,
RT_TIMER_TASK_PRIORITY,
RT_TIMER_TASK_STACK_SIZE,
@ -965,7 +972,7 @@ int esp32s3_rt_timer_init(void)
priv->pid = (pid_t)pid;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
/* ESP32-S3 hardware timer configuration:
* 1 count = 1/16 us
@ -1007,7 +1014,7 @@ int esp32s3_rt_timer_init(void)
modifyreg32(SYSTIMER_CONF_REG, 0, SYSTIMER_TIMER_UNIT1_WORK_EN);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return OK;
}