Skip to content

Commit 639d644

Browse files
Laurent Pinchartkumargala
authored andcommitted
cpm2: Fix race condition in CPM2 GPIO library.
The CPM2 GPIO library code uses the non thread-safe clrbits32/setbits32 macros. This patch protects them with a spinlock. Signed-off-by: Laurent Pinchart <laurentp@cse-semaphore.com> Signed-off-by: Kumar Gala <galak@kernel.crashing.org>
1 parent 61a4e9e commit 639d644

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

‎arch/powerpc/sysdev/cpm_common.c‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,51 +254,66 @@ static int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio)
254254
return !!(in_be32(&iop->dat) & pin_mask);
255255
}
256256

257-
static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
257+
static void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,
258+
int value)
258259
{
259-
struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
260260
struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
261261
struct cpm2_ioports __iomem *iop = mm_gc->regs;
262-
unsigned long flags;
263-
u32 pin_mask = 1 << (31 - gpio);
264-
265-
spin_lock_irqsave(&cpm2_gc->lock, flags);
266262

267263
if (value)
268264
cpm2_gc->cpdata |= pin_mask;
269265
else
270266
cpm2_gc->cpdata &= ~pin_mask;
271267

272268
out_be32(&iop->dat, cpm2_gc->cpdata);
269+
}
270+
271+
static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
272+
{
273+
struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
274+
struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
275+
unsigned long flags;
276+
u32 pin_mask = 1 << (31 - gpio);
277+
278+
spin_lock_irqsave(&cpm2_gc->lock, flags);
279+
280+
__cpm2_gpio32_set(mm_gc, pin_mask, value);
273281

274282
spin_unlock_irqrestore(&cpm2_gc->lock, flags);
275283
}
276284

277285
static int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
278286
{
279287
struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
288+
struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
280289
struct cpm2_ioports __iomem *iop = mm_gc->regs;
281-
u32 pin_mask;
290+
unsigned long flags;
291+
u32 pin_mask = 1 << (31 - gpio);
282292

283-
pin_mask = 1 << (31 - gpio);
293+
spin_lock_irqsave(&cpm2_gc->lock, flags);
284294

285295
setbits32(&iop->dir, pin_mask);
296+
__cpm2_gpio32_set(mm_gc, pin_mask, val);
286297

287-
cpm2_gpio32_set(gc, gpio, val);
298+
spin_unlock_irqrestore(&cpm2_gc->lock, flags);
288299

289300
return 0;
290301
}
291302

292303
static int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)
293304
{
294305
struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
306+
struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
295307
struct cpm2_ioports __iomem *iop = mm_gc->regs;
296-
u32 pin_mask;
308+
unsigned long flags;
309+
u32 pin_mask = 1 << (31 - gpio);
297310

298-
pin_mask = 1 << (31 - gpio);
311+
spin_lock_irqsave(&cpm2_gc->lock, flags);
299312

300313
clrbits32(&iop->dir, pin_mask);
301314

315+
spin_unlock_irqrestore(&cpm2_gc->lock, flags);
316+
302317
return 0;
303318
}
304319

0 commit comments

Comments
 (0)