Skip to content

Commit 46c2d46

Browse files
hujun260xiaoxiang781216
authored andcommitted
use small lock in:
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]>
1 parent b3813bd commit 46c2d46

File tree

6 files changed

+79
-62
lines changed

6 files changed

+79
-62
lines changed

arch/arm/src/imxrt/imxrt_wdog.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct imxrt_wdog_lower
7373
const struct watchdog_ops_s *ops; /* Lower half operations */
7474
uint32_t timeout;
7575
uint32_t enabled;
76+
spinlock_t lock;
7677
};
7778

7879
/****************************************************************************
@@ -239,12 +240,14 @@ static int imxrt_wdog_stop(struct watchdog_lowerhalf_s *lower)
239240

240241
static int imxrt_wdog_keepalive(struct watchdog_lowerhalf_s *lower)
241242
{
242-
irqstate_t flags = spin_lock_irqsave(NULL);
243+
struct imxrt_wdog_lower *priv = (struct imxrt_wdog_lower *)lower;
244+
245+
irqstate_t flags = spin_lock_irqsave(&priv->lock);
243246

244247
putreg16(WDOG_KEEP_ALIVE_KEY1, IMXRT_WDOG1_WSR);
245248
putreg16(WDOG_KEEP_ALIVE_KEY2, IMXRT_WDOG1_WSR);
246249

247-
spin_unlock_irqrestore(NULL, flags);
250+
spin_unlock_irqrestore(&priv->lock, flags);
248251

249252
return OK;
250253
}
@@ -314,7 +317,7 @@ static int imxrt_wdog_settimeout(struct watchdog_lowerhalf_s *lower,
314317

315318
priv->timeout = timeout;
316319

317-
irqstate_t flags = spin_lock_irqsave(NULL);
320+
irqstate_t flags = spin_lock_irqsave(&priv->lock);
318321

319322
/* write timer value to WCR WT register */
320323

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

331-
spin_unlock_irqrestore(NULL, flags);
334+
spin_unlock_irqrestore(&priv->lock, flags);
332335

333336
return OK;
334337
}
@@ -360,14 +363,14 @@ void imxrt_wdog_initialize(void)
360363
priv->ops = &g_wdgops;
361364
priv->timeout = WDOG_MIN;
362365

366+
spin_lock_init(&g_wdgdev.lock);
367+
363368
/* Register the watchdog driver at the path */
364369

365370
wdinfo("Entry: devpath=%s\n", DEVPATH);
366371
watchdog_register(DEVPATH, (struct watchdog_lowerhalf_s *)priv);
367372
}
368373

369-
#endif /* CONFIG_WATCHDOG && CONFIG_IMXRT_WDOG */
370-
371374
/****************************************************************************
372375
* Name: imxrt_wdog_disable
373376
*
@@ -395,9 +398,11 @@ void imxrt_wdog_disable_all(void)
395398
putreg16(reg, IMXRT_WDOG2_WCR);
396399
}
397400

398-
flags = enter_critical_section();
401+
flags = spin_lock_irqsave(&g_wdgdev.lock);
399402
putreg32(RTWDOG_UPDATE_KEY, IMXRT_RTWDOG_CNT);
400403
putreg32(0xffff, IMXRT_RTWDOG_TOVAL);
401404
modifyreg32(IMXRT_RTWDOG_CS, RTWDOG_CS_EN, RTWDOG_CS_UPDATE);
402-
leave_critical_section(flags);
405+
spin_unlock_irqrestore(&g_wdgdev.lock, flags);
403406
}
407+
408+
#endif /* CONFIG_WATCHDOG && CONFIG_IMXRT_WDOG */

arch/arm/src/kinetis/kinetis_edma.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ struct kinetis_edma_s
155155
static struct kinetis_edma_s g_edma =
156156
{
157157
.chlock = NXMUTEX_INITIALIZER,
158+
.lock = SP_UNLOCKED,
158159
#if CONFIG_KINETIS_EDMA_NTCD > 0
159160
.dsem = SEM_INITIALIZER(CONFIG_KINETIS_EDMA_NTCD),
160161
#endif
@@ -197,15 +198,16 @@ static struct kinetis_edmatcd_s *kinetis_tcd_alloc(void)
197198
* waiting.
198199
*/
199200

200-
flags = enter_critical_section();
201201
nxsem_wait_uninterruptible(&g_edma.dsem);
202202

203+
flags = spin_lock_irqsave(&g_edma.lock);
204+
203205
/* Now there should be a TCD in the free list reserved just for us */
204206

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

208-
leave_critical_section(flags);
210+
spin_unlock_irqrestore(&g_edma.lock, flags);
209211
return tcd;
210212
}
211213
#endif
@@ -219,7 +221,7 @@ static struct kinetis_edmatcd_s *kinetis_tcd_alloc(void)
219221
****************************************************************************/
220222

221223
#if CONFIG_KINETIS_EDMA_NTCD > 0
222-
static void kinetis_tcd_free(struct kinetis_edmatcd_s *tcd)
224+
static void kinetis_tcd_free_nolock(struct kinetis_edmatcd_s *tcd)
223225
{
224226
irqstate_t flags;
225227

@@ -228,10 +230,24 @@ static void kinetis_tcd_free(struct kinetis_edmatcd_s *tcd)
228230
* a TCD.
229231
*/
230232

231-
flags = spin_lock_irqsave(NULL);
232233
sq_addlast((sq_entry_t *)tcd, &g_tcd_free);
233234
nxsem_post(&g_edma.dsem);
234-
spin_unlock_irqrestore(NULL, flags);
235+
}
236+
237+
static void kinetis_tcd_free(struct kinetis_edmatcd_s *tcd)
238+
{
239+
irqstate_t flags;
240+
241+
/* Add the the TCD to the end of the free list and post the 'dsem',
242+
* possibly waking up another thread that might be waiting for
243+
* a TCD.
244+
*/
245+
246+
flags = spin_lock_irqsave(&g_edma.lock);
247+
sched_lock();
248+
kinetis_tcd_free_nolock(tcd);
249+
spin_unlock_irqrestore(&g_edma.lock, flags);
250+
sched_unlock();
235251
}
236252
#endif
237253

@@ -435,9 +451,13 @@ static void kinetis_dmaterminate(struct kinetis_dmach_s *dmach, int result)
435451
struct kinetis_edmatcd_s *next;
436452
#endif
437453
uintptr_t regaddr;
454+
irqstate_t flags;
438455
uint8_t regval8;
439456
uint8_t chan;
440457

458+
flags = spin_lock_irqsave(&g_edma.lock);
459+
sched_lock();
460+
441461
/* Disable channel ERROR interrupts */
442462

443463
chan = dmach->chan;
@@ -469,7 +489,7 @@ static void kinetis_dmaterminate(struct kinetis_dmach_s *dmach, int result)
469489
next = dmach->flags & EDMA_CONFIG_LOOPDEST ?
470490
NULL : (struct kinetis_edmatcd_s *)tcd->dlastsga;
471491

472-
kinetis_tcd_free(tcd);
492+
kinetis_tcd_free_nolock(tcd);
473493
}
474494

475495
dmach->head = NULL;
@@ -486,6 +506,9 @@ static void kinetis_dmaterminate(struct kinetis_dmach_s *dmach, int result)
486506
dmach->callback = NULL;
487507
dmach->arg = NULL;
488508
dmach->state = KINETIS_DMA_IDLE;
509+
510+
spin_unlock_irqrestore(&g_edma.lock, flags);
511+
sched_unlock();
489512
}
490513

491514
/****************************************************************************
@@ -1114,7 +1137,7 @@ int kinetis_dmach_start(DMACH_HANDLE handle, edma_callback_t callback,
11141137

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

1117-
flags = spin_lock_irqsave(NULL);
1140+
flags = spin_lock_irqsave(&g_edma.lock);
11181141
dmach->callback = callback;
11191142
dmach->arg = arg;
11201143

@@ -1139,7 +1162,7 @@ int kinetis_dmach_start(DMACH_HANDLE handle, edma_callback_t callback,
11391162
putreg8(regval8, KINETIS_EDMA_SERQ);
11401163
}
11411164

1142-
spin_unlock_irqrestore(NULL, flags);
1165+
spin_unlock_irqrestore(&g_edma.lock, flags);
11431166
return OK;
11441167
}
11451168

@@ -1162,14 +1185,11 @@ int kinetis_dmach_start(DMACH_HANDLE handle, edma_callback_t callback,
11621185
void kinetis_dmach_stop(DMACH_HANDLE handle)
11631186
{
11641187
struct kinetis_dmach_s *dmach = (struct kinetis_dmach_s *)handle;
1165-
irqstate_t flags;
11661188

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

1170-
flags = spin_lock_irqsave(NULL);
11711192
kinetis_dmaterminate(dmach, -EINTR);
1172-
spin_unlock_irqrestore(NULL, flags);
11731193
}
11741194

11751195
/****************************************************************************
@@ -1267,7 +1287,7 @@ void kinetis_dmasample(DMACH_HANDLE handle, struct kinetis_dmaregs_s *regs)
12671287

12681288
/* eDMA Global Registers */
12691289

1270-
flags = spin_lock_irqsave(NULL);
1290+
flags = spin_lock_irqsave(&g_edma.lock);
12711291

12721292
regs->cr = getreg32(KINETIS_EDMA_CR); /* Control */
12731293
regs->es = getreg32(KINETIS_EDMA_ES); /* Error Status */
@@ -1302,7 +1322,7 @@ void kinetis_dmasample(DMACH_HANDLE handle, struct kinetis_dmaregs_s *regs)
13021322
regaddr = KINETIS_DMAMUX_CHCFG(chan);
13031323
regs->dmamux = getreg32(regaddr); /* Channel configuration */
13041324

1305-
spin_unlock_irqrestore(NULL, flags);
1325+
spin_unlock_irqrestore(&g_edma.lock, flags);
13061326
}
13071327
#endif /* CONFIG_DEBUG_DMA */
13081328

arch/arm/src/lc823450/lc823450_dvfs2.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
* Private Data
6969
****************************************************************************/
7070

71+
static spinlock_t g_dvfs_lock = SP_UNLOCKED;
72+
7173
typedef struct freq_entry
7274
{
7375
uint16_t freq;
@@ -428,7 +430,7 @@ static void lc823450_dvfs_do_auto(uint32_t idle[])
428430

429431
void lc823450_dvfs_get_idletime(uint64_t idletime[])
430432
{
431-
irqstate_t flags = spin_lock_irqsave(NULL);
433+
irqstate_t flags = spin_lock_irqsave(&g_dvfs_lock);
432434

433435
/* First, copy g_idle_totaltime to the caller */
434436

@@ -448,7 +450,7 @@ void lc823450_dvfs_get_idletime(uint64_t idletime[])
448450
}
449451
#endif
450452

451-
spin_unlock_irqrestore(NULL, flags);
453+
spin_unlock_irqrestore(&g_dvfs_lock, flags);
452454
}
453455

454456
/****************************************************************************
@@ -504,7 +506,7 @@ void lc823450_dvfs_tick_callback(void)
504506

505507
void lc823450_dvfs_enter_idle(void)
506508
{
507-
irqstate_t flags = spin_lock_irqsave(NULL);
509+
irqstate_t flags = spin_lock_irqsave(&g_dvfs_lock);
508510

509511
int me = this_cpu();
510512

@@ -544,7 +546,7 @@ void lc823450_dvfs_enter_idle(void)
544546
lc823450_dvfs_set_div(_dvfs_cur_idx, 1);
545547

546548
exit_with_error:
547-
spin_unlock_irqrestore(NULL, flags);
549+
spin_unlock_irqrestore(&g_dvfs_lock, flags);
548550
}
549551

550552
/****************************************************************************
@@ -554,7 +556,7 @@ void lc823450_dvfs_enter_idle(void)
554556

555557
void lc823450_dvfs_exit_idle(int irq)
556558
{
557-
irqstate_t flags = spin_lock_irqsave(NULL);
559+
irqstate_t flags = spin_lock_irqsave(&g_dvfs_lock);
558560

559561
int me = this_cpu();
560562
uint64_t d;
@@ -596,7 +598,7 @@ void lc823450_dvfs_exit_idle(int irq)
596598

597599
_dvfs_cpu_is_active[me] = 1;
598600

599-
spin_unlock_irqrestore(NULL, flags);
601+
spin_unlock_irqrestore(&g_dvfs_lock, flags);
600602
}
601603

602604
/****************************************************************************
@@ -628,7 +630,7 @@ int lc823450_dvfs_set_freq(int freq)
628630
return -1;
629631
}
630632

631-
flags = spin_lock_irqsave(NULL);
633+
flags = spin_lock_irqsave(&g_dvfs_lock);
632634

633635
switch (freq)
634636
{
@@ -656,6 +658,6 @@ int lc823450_dvfs_set_freq(int freq)
656658
lc823450_dvfs_set_div(idx, 0);
657659
}
658660

659-
spin_unlock_irqrestore(NULL, flags);
661+
spin_unlock_irqrestore(&g_dvfs_lock, flags);
660662
return ret;
661663
}

0 commit comments

Comments
 (0)