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 authored and xiaoxiang781216 committed Dec 23, 2024
1 parent b3813bd commit 46c2d46
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 62 deletions.
21 changes: 13 additions & 8 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,14 +363,14 @@ 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);
watchdog_register(DEVPATH, (struct watchdog_lowerhalf_s *)priv);
}

#endif /* CONFIG_WATCHDOG && CONFIG_IMXRT_WDOG */

/****************************************************************************
* Name: imxrt_wdog_disable
*
Expand Down Expand Up @@ -395,9 +398,11 @@ 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);
}

#endif /* CONFIG_WATCHDOG && CONFIG_IMXRT_WDOG */
46 changes: 33 additions & 13 deletions arch/arm/src/kinetis/kinetis_edma.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ struct kinetis_edma_s
static struct kinetis_edma_s g_edma =
{
.chlock = NXMUTEX_INITIALIZER,
.lock = SP_UNLOCKED,
#if CONFIG_KINETIS_EDMA_NTCD > 0
.dsem = SEM_INITIALIZER(CONFIG_KINETIS_EDMA_NTCD),
#endif
Expand Down Expand Up @@ -197,15 +198,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 +221,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 +230,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);
spin_unlock_irqrestore(&g_edma.lock, flags);
sched_unlock();
}
#endif

Expand Down Expand Up @@ -435,9 +451,13 @@ static void kinetis_dmaterminate(struct kinetis_dmach_s *dmach, int result)
struct kinetis_edmatcd_s *next;
#endif
uintptr_t regaddr;
irqstate_t flags;
uint8_t regval8;
uint8_t chan;

flags = spin_lock_irqsave(&g_edma.lock);
sched_lock();

/* Disable channel ERROR interrupts */

chan = dmach->chan;
Expand Down Expand Up @@ -469,7 +489,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 All @@ -486,6 +506,9 @@ static void kinetis_dmaterminate(struct kinetis_dmach_s *dmach, int result)
dmach->callback = NULL;
dmach->arg = NULL;
dmach->state = KINETIS_DMA_IDLE;

spin_unlock_irqrestore(&g_edma.lock, flags);
sched_unlock();
}

/****************************************************************************
Expand Down Expand Up @@ -1114,7 +1137,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 +1162,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 +1185,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 +1287,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 +1322,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;
}
Loading

0 comments on commit 46c2d46

Please sign in to comment.