use small lock in following files:

arch/arm64/src/a64/a64_twi.c
arch/arm64/src/imx9/imx9_gpioirq.c
arch/arm64/src/imx9/imx9_lpi2c.c
arch/arm64/src/imx9/imx9_usbdev.c
arch/arm64/src/imx9/imx9_usdhc.c

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit is contained in:
wangzhi16 2025-01-03 14:28:53 +08:00 committed by Alan C. Assis
parent 7c24ef438e
commit bbaf1ff9fd
5 changed files with 68 additions and 42 deletions

View file

@ -44,6 +44,7 @@
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <arch/board/board.h>
#include "arm64_internal.h"
@ -166,6 +167,7 @@ struct a64_twi_priv_s
int refs; /* Reference count */
mutex_t lock; /* Mutual exclusion mutex */
spinlock_t spinlock; /* Spinlock */
sem_t waitsem; /* Wait for TWI transfer completion */
struct wdog_s timeout; /* Watchdog to recover from bus hangs */
volatile int result; /* The result of the transfer */
@ -288,6 +290,7 @@ static struct a64_twi_priv_s a64_twi0_priv =
.config = &a64_twi0_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
.waitsem = SEM_INITIALIZER(0),
.intstate = INTSTATE_IDLE,
.msgc = 0,
@ -324,6 +327,7 @@ static struct a64_twi_priv_s a64_twi1_priv =
.config = &a64_twi1_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
.waitsem = SEM_INITIALIZER(0),
.intstate = INTSTATE_IDLE,
.msgc = 0,
@ -360,6 +364,7 @@ static struct a64_twi_priv_s a64_twi2_priv =
.config = &a64_twi2_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
.waitsem = SEM_INITIALIZER(0),
.intstate = INTSTATE_IDLE,
.msgc = 0,
@ -396,6 +401,7 @@ static struct a64_twi_priv_s a64_rtwi_priv =
.config = &a64_rtwi_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
.waitsem = SEM_INITIALIZER(0),
.intstate = INTSTATE_IDLE,
.msgc = 0,
@ -1226,7 +1232,7 @@ static int a64_twi_isr_process(struct a64_twi_priv_s *priv)
*/
#ifdef CONFIG_I2C_POLLED
irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
#endif
/* Transmit a byte */
@ -1235,7 +1241,7 @@ static int a64_twi_isr_process(struct a64_twi_priv_s *priv)
priv->dcnt--;
#ifdef CONFIG_I2C_POLLED
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
#endif
break;
@ -1558,7 +1564,7 @@ static int twi_transfer(struct i2c_master_s *dev,
twi_setclock(priv, msgs->frequency);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
/* Initiate the transfer. The rest will be handled from interrupt
* logic. Interrupts must be disabled to prevent re-entrance from the
@ -1612,7 +1618,7 @@ out:
/* Release the port for re-use by other clients */
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
nxmutex_unlock(&priv->lock);
return ret;
}
@ -1820,7 +1826,7 @@ set_clk:
static void twi_hw_initialize(struct a64_twi_priv_s *priv)
{
irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
i2cinfo("TWI%d Initializing\n", priv->config->twi);
@ -1856,7 +1862,7 @@ static void twi_hw_initialize(struct a64_twi_priv_s *priv)
twi_softreset(priv);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
}
/****************************************************************************

View file

@ -33,6 +33,7 @@
#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include "arm64_internal.h"
#include "imx9_gpio.h"
@ -62,6 +63,7 @@ struct imx9_portisr_s
****************************************************************************/
static struct imx9_portisr_s g_isrtab[IMX9_GPIO_NPORTS];
static spinlock_t g_isrlock = SP_UNLOCKED;
/****************************************************************************
* Private Functions
@ -197,12 +199,12 @@ int imx9_gpioirq_attach(gpio_pinset_t pinset, xcpt_t isr, void *arg)
/* Atomically change the handler */
irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&g_isrlock);
g_isrtab[port].pins[pin].isr = isr;
g_isrtab[port].pins[pin].arg = arg;
leave_critical_section(flags);
spin_unlock_irqrestore(&g_isrlock, flags);
return OK;
}

View file

@ -38,6 +38,7 @@
#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/clock.h>
#include <nuttx/mutex.h>
#include <nuttx/semaphore.h>
@ -206,6 +207,7 @@ struct imx9_lpi2c_priv_s
int refs; /* Reference count */
mutex_t lock; /* Mutual exclusion mutex */
spinlock_t spinlock; /* Spinlock */
#ifndef CONFIG_I2C_POLLED
sem_t sem_isr; /* Interrupt wait semaphore */
#endif
@ -364,6 +366,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c1_priv =
.config = &imx9_lpi2c1_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
@ -412,6 +415,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c2_priv =
.config = &imx9_lpi2c2_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
@ -460,6 +464,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c3_priv =
.config = &imx9_lpi2c3_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
@ -508,6 +513,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c4_priv =
.config = &imx9_lpi2c4_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
@ -556,6 +562,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c5_priv =
.config = &imx9_lpi2c5_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
@ -604,6 +611,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c6_priv =
.config = &imx9_lpi2c6_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
@ -652,6 +660,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c7_priv =
.config = &imx9_lpi2c7_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
@ -700,6 +709,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c8_priv =
.config = &imx9_lpi2c8_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
@ -1595,7 +1605,7 @@ static int imx9_lpi2c_isr_process(struct imx9_lpi2c_priv_s *priv)
*/
#ifdef CONFIG_I2C_POLLED
irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
#endif
/* Receive a byte */
@ -1610,7 +1620,7 @@ static int imx9_lpi2c_isr_process(struct imx9_lpi2c_priv_s *priv)
}
#ifdef CONFIG_I2C_POLLED
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
#endif
}
@ -2376,7 +2386,7 @@ struct i2c_master_s *imx9_i2cbus_initialize(int port)
* power-up hardware and configure GPIOs.
*/
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
if ((volatile int)priv->refs++ == 0)
{
@ -2397,7 +2407,7 @@ struct i2c_master_s *imx9_i2cbus_initialize(int port)
#endif
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
return (struct i2c_master_s *)priv;
}
@ -2424,7 +2434,7 @@ int imx9_i2cbus_uninitialize(struct i2c_master_s *dev)
return ERROR;
}
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
if (--priv->refs > 0)
{
@ -2432,7 +2442,7 @@ int imx9_i2cbus_uninitialize(struct i2c_master_s *dev)
return OK;
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
/* Disable power and other HW resource (GPIO's) */

View file

@ -42,6 +42,7 @@
#include <nuttx/usb/usbdev_trace.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <arch/board/board.h>
#include <imx9_usbdev.h>
@ -360,6 +361,7 @@ struct imx9_ep_s
*/
struct usbdev_ep_s ep; /* Standard endpoint structure */
spinlock_t spinlock; /* Spinlock */
/* IMX9XX-specific fields */
@ -390,6 +392,7 @@ struct imx9_usb_s
*/
struct usbdev_s usbdev;
spinlock_t spinlock; /* Spinlock */
/* The bound device class driver */
@ -2297,7 +2300,7 @@ static int imx9_epdisable(struct usbdev_ep_s *ep)
usbtrace(TRACE_EPDISABLE, privep->epphy);
flags = enter_critical_section();
flags = spin_lock_irqsave(&privep->spinlock);
/* Disable Endpoint */
@ -2316,7 +2319,7 @@ static int imx9_epdisable(struct usbdev_ep_s *ep)
/* Cancel any ongoing activity */
imx9_cancelrequests(privep, -ESHUTDOWN);
spin_unlock_irqrestore(&privep->spinlock, flags);
leave_critical_section(flags);
return OK;
@ -2475,7 +2478,7 @@ static int imx9_epsubmit(struct usbdev_ep_s *ep,
/* Disable Interrupts */
flags = enter_critical_section();
flags = spin_lock_irqsave(&privep->spinlock);
/* If we are stalled, then drop all requests on the floor */
@ -2502,7 +2505,7 @@ static int imx9_epsubmit(struct usbdev_ep_s *ep,
}
}
leave_critical_section(flags);
spin_unlock_irqrestore(&privep->spinlock, flags);
return ret;
}
@ -2530,7 +2533,7 @@ static int imx9_epcancel(struct usbdev_ep_s *ep,
usbtrace(TRACE_EPCANCEL, privep->epphy);
flags = enter_critical_section();
flags = spin_lock_irqsave(&privep->spinlock);
/* FIXME: if the request is the first, then we need to flush the EP
* otherwise just remove it from the list
@ -2539,7 +2542,7 @@ static int imx9_epcancel(struct usbdev_ep_s *ep,
*/
imx9_cancelrequests(privep, -ESHUTDOWN);
leave_critical_section(flags);
spin_unlock_irqrestore(&privep->spinlock, flags);
return OK;
}
@ -2559,7 +2562,7 @@ static int imx9_epstall(struct usbdev_ep_s *ep, bool resume)
/* STALL or RESUME the endpoint */
flags = enter_critical_section();
flags = spin_lock_irqsave(&privep->spinlock);
usbtrace(resume ? TRACE_EPRESUME : TRACE_EPSTALL, privep->epphy);
uint32_t offs = IMX9_USBDEV_ENDPTCTRL_OFFSET(privep->epphy >> 1);
@ -2583,7 +2586,7 @@ static int imx9_epstall(struct usbdev_ep_s *ep, bool resume)
imx9_modifyreg(priv, offs, 0, ctrl_xs);
}
leave_critical_section(flags);
spin_unlock_irqrestore(&privep->spinlock, flags);
return OK;
}
@ -2687,7 +2690,7 @@ static struct usbdev_ep_s *imx9_allocep(struct usbdev_s *dev,
{
/* Yes.. now see if any of the request endpoints are available */
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
epset &= priv->epavail;
if (epset)
{
@ -2703,7 +2706,7 @@ static struct usbdev_ep_s *imx9_allocep(struct usbdev_s *dev,
/* Mark endpoint no longer available */
priv->epavail &= ~bit;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
/* And return the pointer to the standard endpoint
* structure
@ -2716,7 +2719,7 @@ static struct usbdev_ep_s *imx9_allocep(struct usbdev_s *dev,
/* Shouldn't get here */
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
}
usbtrace(TRACE_DEVERROR(IMX9_TRACEERR_NOEP), (uint16_t)eplog);
@ -2736,7 +2739,8 @@ static void imx9_freeep(struct usbdev_s *dev,
{
struct imx9_usb_s *priv = (struct imx9_usb_s *)dev;
struct imx9_ep_s *privep = (struct imx9_ep_s *)ep;
irqstate_t flags;
irqstate_t flags_1;
irqstate_t flags_2;
usbtrace(TRACE_DEVFREEEP, (uint16_t)privep->epphy);
@ -2744,9 +2748,11 @@ static void imx9_freeep(struct usbdev_s *dev,
{
/* Mark the endpoint as available */
flags = enter_critical_section();
flags_1 = spin_lock_irqsave(&priv->spinlock);
flags_2 = spin_lock_irqsave(&privep->spinlock);
priv->epavail |= (1 << privep->epphy);
leave_critical_section(flags);
spin_unlock_irqrestore(&privep->spinlock, flags_2);
spin_unlock_irqrestore(&priv->spinlock, flags_1);
}
}
@ -2795,9 +2801,9 @@ static int imx9_wakeup(struct usbdev_s *dev)
usbtrace(TRACE_DEVWAKEUP, 0);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
imx9_modifyreg(priv, IMX9_USBDEV_PORTSC1_OFFSET, 0, USBDEV_PRTSC1_FPR);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
return OK;
}
@ -2841,7 +2847,7 @@ static int imx9_pullup(struct usbdev_s *dev, bool enable)
usbtrace(TRACE_DEVPULLUP, (uint16_t)enable);
irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
if (enable)
{
imx9_modifyreg(priv, IMX9_USBDEV_USBCMD_OFFSET, 0, USBDEV_USBCMD_RS);
@ -2858,7 +2864,7 @@ static int imx9_pullup(struct usbdev_s *dev, bool enable)
imx9_modifyreg(priv, IMX9_USBDEV_USBCMD_OFFSET, USBDEV_USBCMD_RS, 0);
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
return OK;
}
@ -2927,7 +2933,7 @@ void arm64_usbinitialize(void)
int i;
irqstate_t flags;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
/* Initialize the device state structure */
@ -3015,7 +3021,7 @@ void arm64_usbinitialize(void)
irq_attach(IMX9_IRQ_USB1 + priv->id, imx9_usbinterrupt, priv);
up_enable_irq(IMX9_IRQ_USB1 + priv->id);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
/* Reset/Re-initialize the USB hardware */
@ -3039,7 +3045,7 @@ void arm64_usbuninitialize(void)
usbdev_unregister(priv->driver);
}
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
/* Disconnect device */
@ -3066,7 +3072,7 @@ void arm64_usbuninitialize(void)
imx9_ccm_gate_on(CCM_LPCG_USB_CONTROLLER, false);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
}
/****************************************************************************

View file

@ -43,6 +43,7 @@
#include <nuttx/mmcsd.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <arch/board/board.h>
#include "chip.h"
@ -177,6 +178,7 @@
struct imx9_dev_s
{
struct sdio_dev_s dev; /* Standard, base SDIO interface */
spinlock_t spinlock; /* Spinlock */
/* Imx9-specific extensions */
@ -562,7 +564,7 @@ static void imx9_configwaitints(struct imx9_dev_s *priv, uint32_t waitints,
* operation.
*/
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
priv->waitevents = waitevents;
priv->wkupevent = wkupevent;
priv->waitints = waitints;
@ -572,7 +574,7 @@ static void imx9_configwaitints(struct imx9_dev_s *priv, uint32_t waitints,
#endif
putreg32(priv->xfrints | priv->waitints | priv->cintints,
priv->addr + IMX9_USDHC_IRQSIGEN_OFFSET);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
}
/****************************************************************************
@ -594,11 +596,11 @@ static void imx9_configxfrints(struct imx9_dev_s *priv, uint32_t xfrints)
{
irqstate_t flags;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
priv->xfrints = xfrints;
putreg32(priv->xfrints | priv->waitints | priv->cintints,
priv->addr + IMX9_USDHC_IRQSIGEN_OFFSET);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
}
/****************************************************************************
@ -3289,11 +3291,11 @@ void imx9_usdhc_set_sdio_card_isr(struct sdio_dev_s *dev,
}
#endif
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);
regval = getreg32(priv->addr + IMX9_USDHC_IRQSIGEN_OFFSET);
regval = (regval & ~USDHC_INT_CINT) | priv->cintints;
putreg32(regval, priv->addr + IMX9_USDHC_IRQSIGEN_OFFSET);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
}
/****************************************************************************