Skip to content

Commit b70eb30

Browse files
paulburtonralfbaechle
authored andcommitted
MIPS: cmpxchg: Implement 1 byte & 2 byte xchg()
Implement 1 & 2 byte xchg() using read-modify-write atop a 4 byte cmpxchg(). This allows us to support these atomic operations despite the MIPS ISA only providing for 4 & 8 byte atomic operations. This is required in order to support queued spinlocks (qspinlock) in a later patch, since these make use of a 2 byte xchg() in their slow path. Signed-off-by: Paul Burton <paul.burton@imgtec.com> Cc: linux-mips@linux-mips.org Patchwork: https://patchwork.linux-mips.org/patch/16354/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 8263db4 commit b70eb30

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎arch/mips/include/asm/cmpxchg.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,16 @@ extern unsigned long __xchg_called_with_bad_pointer(void)
7070
__ret; \
7171
})
7272

73+
extern unsigned long __xchg_small(volatile void *ptr, unsigned long val,
74+
unsigned int size);
75+
7376
static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
7477
{
7578
switch (size) {
79+
case 1:
80+
case 2:
81+
return __xchg_small(ptr, x, size);
82+
7683
case 4:
7784
return __xchg_asm("ll", "sc", (volatile u32 *)ptr, x);
7885

@@ -91,8 +98,6 @@ static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int siz
9198
({ \
9299
__typeof__(*(ptr)) __res; \
93100
\
94-
BUILD_BUG_ON(sizeof(*(ptr)) & ~0xc); \
95-
\
96101
smp_mb__before_llsc(); \
97102
\
98103
__res = (__typeof__(*(ptr))) \

‎arch/mips/kernel/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
extra-y := head.o vmlinux.lds
66

7-
obj-y += cpu-probe.o branch.o elf.o entry.o genex.o idle.o irq.o \
7+
obj-y += cmpxchg.o cpu-probe.o branch.o elf.o entry.o genex.o idle.o irq.o \
88
process.o prom.o ptrace.o reset.o setup.o signal.o \
99
syscall.o time.o topology.o traps.o unaligned.o watch.o \
1010
vdso.o cacheinfo.o

‎arch/mips/kernel/cmpxchg.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2017 Imagination Technologies
3+
* Author: Paul Burton <paul.burton@imgtec.com>
4+
*
5+
* This program is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License as published by the
7+
* Free Software Foundation; either version 2 of the License, or (at your
8+
* option) any later version.
9+
*/
10+
11+
#include <linux/bitops.h>
12+
#include <asm/cmpxchg.h>
13+
14+
unsigned long __xchg_small(volatile void *ptr, unsigned long val, unsigned int size)
15+
{
16+
u32 old32, new32, load32, mask;
17+
volatile u32 *ptr32;
18+
unsigned int shift;
19+
20+
/* Check that ptr is naturally aligned */
21+
WARN_ON((unsigned long)ptr & (size - 1));
22+
23+
/* Mask value to the correct size. */
24+
mask = GENMASK((size * BITS_PER_BYTE) - 1, 0);
25+
val &= mask;
26+
27+
/*
28+
* Calculate a shift & mask that correspond to the value we wish to
29+
* exchange within the naturally aligned 4 byte integerthat includes
30+
* it.
31+
*/
32+
shift = (unsigned long)ptr & 0x3;
33+
if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
34+
shift ^= sizeof(u32) - size;
35+
shift *= BITS_PER_BYTE;
36+
mask <<= shift;
37+
38+
/*
39+
* Calculate a pointer to the naturally aligned 4 byte integer that
40+
* includes our byte of interest, and load its value.
41+
*/
42+
ptr32 = (volatile u32 *)((unsigned long)ptr & ~0x3);
43+
load32 = *ptr32;
44+
45+
do {
46+
old32 = load32;
47+
new32 = (load32 & ~mask) | (val << shift);
48+
load32 = cmpxchg(ptr32, old32, new32);
49+
} while (load32 != old32);
50+
51+
return (load32 & mask) >> shift;
52+
}

0 commit comments

Comments
 (0)