Skip to content

Commit

Permalink
use small lock in:
Browse files Browse the repository at this point in the history
arch/arm/src/imxrt/imxrt_wdog.c
arch/arm/src/kinetis/kinetis_edma.c
arch/arm/src/lc823450/lc823450_dvfs2.c
arch/arm/src/lc823450/lc823450_timer.c
arch/arm/src/lpc54xx/lpc54_lowputc.c

Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 committed Dec 20, 2024
1 parent 59a849a commit d2b3250
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 41 deletions.
17 changes: 11 additions & 6 deletions arch/arm/src/imxrt/imxrt_wdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct imxrt_wdog_lower
const struct watchdog_ops_s *ops; /* Lower half operations */
uint32_t timeout;
uint32_t enabled;
spinlock_t lock;
};

/****************************************************************************
Expand Down Expand Up @@ -239,12 +240,14 @@ static int imxrt_wdog_stop(struct watchdog_lowerhalf_s *lower)

static int imxrt_wdog_keepalive(struct watchdog_lowerhalf_s *lower)
{
irqstate_t flags = spin_lock_irqsave(NULL);
struct imxrt_wdog_lower *priv = (struct imxrt_wdog_lower *)lower;

irqstate_t flags = spin_lock_irqsave(&priv->lock);

putreg16(WDOG_KEEP_ALIVE_KEY1, IMXRT_WDOG1_WSR);
putreg16(WDOG_KEEP_ALIVE_KEY2, IMXRT_WDOG1_WSR);

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);

return OK;
}
Expand Down Expand Up @@ -314,7 +317,7 @@ static int imxrt_wdog_settimeout(struct watchdog_lowerhalf_s *lower,

priv->timeout = timeout;

irqstate_t flags = spin_lock_irqsave(NULL);
irqstate_t flags = spin_lock_irqsave(&priv->lock);

/* write timer value to WCR WT register */

Expand All @@ -328,7 +331,7 @@ static int imxrt_wdog_settimeout(struct watchdog_lowerhalf_s *lower,
putreg16(WDOG_KEEP_ALIVE_KEY1, IMXRT_WDOG1_WSR);
putreg16(WDOG_KEEP_ALIVE_KEY2, IMXRT_WDOG1_WSR);

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);

return OK;
}
Expand Down Expand Up @@ -360,6 +363,8 @@ void imxrt_wdog_initialize(void)
priv->ops = &g_wdgops;
priv->timeout = WDOG_MIN;

spin_lock_init(&g_wdgdev.lock);

/* Register the watchdog driver at the path */

wdinfo("Entry: devpath=%s\n", DEVPATH);
Expand Down Expand Up @@ -395,9 +400,9 @@ void imxrt_wdog_disable_all(void)
putreg16(reg, IMXRT_WDOG2_WCR);
}

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_wdgdev.lock);
putreg32(RTWDOG_UPDATE_KEY, IMXRT_RTWDOG_CNT);
putreg32(0xffff, IMXRT_RTWDOG_TOVAL);
modifyreg32(IMXRT_RTWDOG_CS, RTWDOG_CS_EN, RTWDOG_CS_UPDATE);
leave_critical_section(flags);
spin_unlock_irqrestore(&g_wdgdev.lock, flags);
}
40 changes: 27 additions & 13 deletions arch/arm/src/kinetis/kinetis_edma.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ struct kinetis_edma_s
* Private Data
****************************************************************************/

static spinlock_t g_edma_lock = SP_UNLOCKED;

/* The state of the eDMA */

static struct kinetis_edma_s g_edma =
Expand Down Expand Up @@ -197,15 +199,16 @@ static struct kinetis_edmatcd_s *kinetis_tcd_alloc(void)
* waiting.
*/

flags = enter_critical_section();
nxsem_wait_uninterruptible(&g_edma.dsem);

flags = spin_lock_irqsave(&g_edma_lock);

/* Now there should be a TCD in the free list reserved just for us */

tcd = (struct kinetis_edmatcd_s *)sq_remfirst(&g_tcd_free);
DEBUGASSERT(tcd != NULL);

leave_critical_section(flags);
spin_unlock_irqrestore(&g_edma_lock, flags);
return tcd;
}
#endif
Expand All @@ -219,7 +222,7 @@ static struct kinetis_edmatcd_s *kinetis_tcd_alloc(void)
****************************************************************************/

#if CONFIG_KINETIS_EDMA_NTCD > 0
static void kinetis_tcd_free(struct kinetis_edmatcd_s *tcd)
static void kinetis_tcd_free_nolock(struct kinetis_edmatcd_s *tcd)
{
irqstate_t flags;

Expand All @@ -228,10 +231,24 @@ static void kinetis_tcd_free(struct kinetis_edmatcd_s *tcd)
* a TCD.
*/

flags = spin_lock_irqsave(NULL);
sq_addlast((sq_entry_t *)tcd, &g_tcd_free);
nxsem_post(&g_edma.dsem);
spin_unlock_irqrestore(NULL, flags);
}

static void kinetis_tcd_free(struct kinetis_edmatcd_s *tcd)
{
irqstate_t flags;

/* Add the the TCD to the end of the free list and post the 'dsem',
* possibly waking up another thread that might be waiting for
* a TCD.
*/

flags = spin_lock_irqsave(&g_edma_lock);
sched_lock();
kinetis_tcd_free_nolock(tcd);
sched_unlock();
spin_unlock_irqrestore(&g_edma_lock, flags);
}
#endif

Expand Down Expand Up @@ -469,7 +486,7 @@ static void kinetis_dmaterminate(struct kinetis_dmach_s *dmach, int result)
next = dmach->flags & EDMA_CONFIG_LOOPDEST ?
NULL : (struct kinetis_edmatcd_s *)tcd->dlastsga;

kinetis_tcd_free(tcd);
kinetis_tcd_free_nolock(tcd);
}

dmach->head = NULL;
Expand Down Expand Up @@ -1114,7 +1131,7 @@ int kinetis_dmach_start(DMACH_HANDLE handle, edma_callback_t callback,

/* Save the callback info. This will be invoked when the DMA completes */

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_edma_lock);
dmach->callback = callback;
dmach->arg = arg;

Expand All @@ -1139,7 +1156,7 @@ int kinetis_dmach_start(DMACH_HANDLE handle, edma_callback_t callback,
putreg8(regval8, KINETIS_EDMA_SERQ);
}

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_edma_lock, flags);
return OK;
}

Expand All @@ -1162,14 +1179,11 @@ int kinetis_dmach_start(DMACH_HANDLE handle, edma_callback_t callback,
void kinetis_dmach_stop(DMACH_HANDLE handle)
{
struct kinetis_dmach_s *dmach = (struct kinetis_dmach_s *)handle;
irqstate_t flags;

dmainfo("dmach: %p\n", dmach);
DEBUGASSERT(dmach != NULL);

flags = spin_lock_irqsave(NULL);
kinetis_dmaterminate(dmach, -EINTR);
spin_unlock_irqrestore(NULL, flags);
}

/****************************************************************************
Expand Down Expand Up @@ -1267,7 +1281,7 @@ void kinetis_dmasample(DMACH_HANDLE handle, struct kinetis_dmaregs_s *regs)

/* eDMA Global Registers */

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_edma_lock);

regs->cr = getreg32(KINETIS_EDMA_CR); /* Control */
regs->es = getreg32(KINETIS_EDMA_ES); /* Error Status */
Expand Down Expand Up @@ -1302,7 +1316,7 @@ void kinetis_dmasample(DMACH_HANDLE handle, struct kinetis_dmaregs_s *regs)
regaddr = KINETIS_DMAMUX_CHCFG(chan);
regs->dmamux = getreg32(regaddr); /* Channel configuration */

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_edma_lock, flags);
}
#endif /* CONFIG_DEBUG_DMA */

Expand Down
18 changes: 10 additions & 8 deletions arch/arm/src/lc823450/lc823450_dvfs2.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
* Private Data
****************************************************************************/

static spinlock_t g_dvfs_lock = SP_UNLOCKED;

typedef struct freq_entry
{
uint16_t freq;
Expand Down Expand Up @@ -428,7 +430,7 @@ static void lc823450_dvfs_do_auto(uint32_t idle[])

void lc823450_dvfs_get_idletime(uint64_t idletime[])
{
irqstate_t flags = spin_lock_irqsave(NULL);
irqstate_t flags = spin_lock_irqsave(&g_dvfs_lock);

/* First, copy g_idle_totaltime to the caller */

Expand All @@ -448,7 +450,7 @@ void lc823450_dvfs_get_idletime(uint64_t idletime[])
}
#endif

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_dvfs_lock, flags);
}

/****************************************************************************
Expand Down Expand Up @@ -504,7 +506,7 @@ void lc823450_dvfs_tick_callback(void)

void lc823450_dvfs_enter_idle(void)
{
irqstate_t flags = spin_lock_irqsave(NULL);
irqstate_t flags = spin_lock_irqsave(&g_dvfs_lock);

int me = this_cpu();

Expand Down Expand Up @@ -544,7 +546,7 @@ void lc823450_dvfs_enter_idle(void)
lc823450_dvfs_set_div(_dvfs_cur_idx, 1);

exit_with_error:
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_dvfs_lock, flags);
}

/****************************************************************************
Expand All @@ -554,7 +556,7 @@ void lc823450_dvfs_enter_idle(void)

void lc823450_dvfs_exit_idle(int irq)
{
irqstate_t flags = spin_lock_irqsave(NULL);
irqstate_t flags = spin_lock_irqsave(&g_dvfs_lock);

int me = this_cpu();
uint64_t d;
Expand Down Expand Up @@ -596,7 +598,7 @@ void lc823450_dvfs_exit_idle(int irq)

_dvfs_cpu_is_active[me] = 1;

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_dvfs_lock, flags);
}

/****************************************************************************
Expand Down Expand Up @@ -628,7 +630,7 @@ int lc823450_dvfs_set_freq(int freq)
return -1;
}

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_dvfs_lock);

switch (freq)
{
Expand Down Expand Up @@ -656,6 +658,6 @@ int lc823450_dvfs_set_freq(int freq)
lc823450_dvfs_set_div(idx, 0);
}

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_dvfs_lock, flags);
return ret;
}
24 changes: 13 additions & 11 deletions arch/arm/src/lc823450/lc823450_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ static void hrt_usleep_add(struct hrt_s *phrt);
* Private Data
****************************************************************************/

static spinlock_t g_rtc_lock = SP_UNLOCKED;

#ifdef CHECK_INTERVAL
static bool _timer_val = true;
#endif
Expand Down Expand Up @@ -185,7 +187,7 @@ static void hrt_queue_refresh(void)
struct hrt_s *tmp;
irqstate_t flags;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_rtc_lock);
elapsed = (uint64_t)getreg32(MT20CNT) * (1000 * 1000) * 10 / XT1OSC_CLK;

for (pent = hrt_timer_queue.head; pent; pent = dq_next(pent))
Expand All @@ -204,9 +206,9 @@ static void hrt_queue_refresh(void)
if (tmp->usec <= 0)
{
dq_rem(pent, &hrt_timer_queue);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_rtc_lock, flags);
nxsem_post(&tmp->sem);
flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_rtc_lock);
goto cont;
}
else
Expand All @@ -215,7 +217,7 @@ static void hrt_queue_refresh(void)
}
}

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_rtc_lock, flags);
}
#endif

Expand All @@ -230,15 +232,15 @@ static void hrt_usleep_setup(void)
struct hrt_s *head;
irqstate_t flags;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_rtc_lock);
head = container_of(hrt_timer_queue.head, struct hrt_s, ent);
if (head == NULL)
{
/* MTM2: disable clocking */

modifyreg32(MCLKCNTEXT1, MCLKCNTEXT1_MTM2C_CLKEN, 0x0);
modifyreg32(MCLKCNTEXT1, MCLKCNTEXT1_MTM2_CLKEN, 0x0);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_rtc_lock, flags);
return;
}

Expand All @@ -260,7 +262,7 @@ static void hrt_usleep_setup(void)
/* Enable MTM2-Ch0 */

putreg32(1, MT2OPR);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_rtc_lock, flags);
}
#endif

Expand Down Expand Up @@ -299,7 +301,7 @@ static void hrt_usleep_add(struct hrt_s *phrt)

hrt_queue_refresh();

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_rtc_lock);

/* add phrt to hrt_timer_queue */

Expand All @@ -321,7 +323,7 @@ static void hrt_usleep_add(struct hrt_s *phrt)
dq_addlast(&phrt->ent, &hrt_timer_queue);
}

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_rtc_lock, flags);

hrt_usleep_setup();
}
Expand Down Expand Up @@ -699,7 +701,7 @@ int up_rtc_gettime(struct timespec *tp)
irqstate_t flags;
uint64_t f;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_rtc_lock);

/* Get the elapsed time */

Expand All @@ -710,7 +712,7 @@ int up_rtc_gettime(struct timespec *tp)
f = up_get_timer_fraction();
elapsed += f;

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_rtc_lock, flags);

tmrinfo("elapsed = %lld\n", elapsed);

Expand Down
Loading

0 comments on commit d2b3250

Please sign in to comment.