]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
MIPS: bitops: Use BIT_WORD() & BITS_PER_LONG
authorPaul Burton <paul.burton@mips.com>
Tue, 1 Oct 2019 21:53:34 +0000 (21:53 +0000)
committerPaul Burton <paul.burton@mips.com>
Mon, 7 Oct 2019 16:42:55 +0000 (09:42 -0700)
Rather than using custom SZLONG_LOG & SZLONG_MASK macros to shift & mask
a bit index to form word & bit offsets respectively, make use of the
standard BIT_WORD() & BITS_PER_LONG macros for the same purpose.

volatile is added to the definition of pointers to the long-sized word
we'll operate on, in order to prevent the compiler complaining that we
cast away the volatile qualifier of the addr argument. This should have
no effect on generated code, which in the LL/SC case is inline asm
anyway & in the non-LLSC case access is constrained by compiler barriers
provided by raw_local_irq_{save,restore}().

Signed-off-by: Paul Burton <paul.burton@mips.com>
Cc: linux-mips@vger.kernel.org
Cc: Huacai Chen <chenhc@lemote.com>
Cc: Jiaxun Yang <jiaxun.yang@flygoat.com>
Cc: linux-kernel@vger.kernel.org
arch/mips/include/asm/bitops.h
arch/mips/include/asm/llsc.h
arch/mips/lib/bitops.c

index fba0a842b98a3420dbbd5216200f838770925614..d39fca2def602021d38729b3ade86d987f229674 100644 (file)
@@ -87,8 +87,8 @@ int __mips_test_and_change_bit(unsigned long nr,
  */
 static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
 {
-       unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
-       int bit = nr & SZLONG_MASK;
+       volatile unsigned long *m = &addr[BIT_WORD(nr)];
+       int bit = nr % BITS_PER_LONG;
 
        if (!kernel_uses_llsc) {
                __mips_set_bit(nr, addr);
@@ -117,8 +117,8 @@ static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
  */
 static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
 {
-       unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
-       int bit = nr & SZLONG_MASK;
+       volatile unsigned long *m = &addr[BIT_WORD(nr)];
+       int bit = nr % BITS_PER_LONG;
 
        if (!kernel_uses_llsc) {
                __mips_clear_bit(nr, addr);
@@ -160,8 +160,8 @@ static inline void clear_bit_unlock(unsigned long nr, volatile unsigned long *ad
  */
 static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
 {
-       unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
-       int bit = nr & SZLONG_MASK;
+       volatile unsigned long *m = &addr[BIT_WORD(nr)];
+       int bit = nr % BITS_PER_LONG;
 
        if (!kernel_uses_llsc) {
                __mips_change_bit(nr, addr);
@@ -183,8 +183,8 @@ static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
 static inline int test_and_set_bit_lock(unsigned long nr,
        volatile unsigned long *addr)
 {
-       unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
-       int bit = nr & SZLONG_MASK;
+       volatile unsigned long *m = &addr[BIT_WORD(nr)];
+       int bit = nr % BITS_PER_LONG;
        unsigned long res, orig;
 
        if (!kernel_uses_llsc) {
@@ -228,8 +228,8 @@ static inline int test_and_set_bit(unsigned long nr,
 static inline int test_and_clear_bit(unsigned long nr,
        volatile unsigned long *addr)
 {
-       unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
-       int bit = nr & SZLONG_MASK;
+       volatile unsigned long *m = &addr[BIT_WORD(nr)];
+       int bit = nr % BITS_PER_LONG;
        unsigned long res, orig;
 
        smp_mb__before_llsc();
@@ -267,8 +267,8 @@ static inline int test_and_clear_bit(unsigned long nr,
 static inline int test_and_change_bit(unsigned long nr,
        volatile unsigned long *addr)
 {
-       unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
-       int bit = nr & SZLONG_MASK;
+       volatile unsigned long *m = &addr[BIT_WORD(nr)];
+       int bit = nr % BITS_PER_LONG;
        unsigned long res, orig;
 
        smp_mb__before_llsc();
index d240a4a2d1c45134d1e651f2d63aa96e7754837d..c49738bc3bda2dca4e9c3c6b89dc5870dae58623 100644 (file)
 #include <asm/isa-rev.h>
 
 #if _MIPS_SZLONG == 32
-#define SZLONG_LOG 5
-#define SZLONG_MASK 31UL
 #define __LL           "ll     "
 #define __SC           "sc     "
 #define __INS          "ins    "
 #define __EXT          "ext    "
 #elif _MIPS_SZLONG == 64
-#define SZLONG_LOG 6
-#define SZLONG_MASK 63UL
 #define __LL           "lld    "
 #define __SC           "scd    "
 #define __INS          "dins   "
index fba402c0879db0f429682bb2b260671ba1aa1854..116d0bd8b2ae9988a5876bec7407caf0ae89a09e 100644 (file)
@@ -7,6 +7,7 @@
  * Copyright (c) 1999, 2000  Silicon Graphics, Inc.
  */
 #include <linux/bitops.h>
+#include <linux/bits.h>
 #include <linux/irqflags.h>
 #include <linux/export.h>
 
  */
 void __mips_set_bit(unsigned long nr, volatile unsigned long *addr)
 {
-       unsigned long *a = (unsigned long *)addr;
-       unsigned bit = nr & SZLONG_MASK;
+       volatile unsigned long *a = &addr[BIT_WORD(nr)];
+       unsigned int bit = nr % BITS_PER_LONG;
        unsigned long mask;
        unsigned long flags;
 
-       a += nr >> SZLONG_LOG;
        mask = 1UL << bit;
        raw_local_irq_save(flags);
        *a |= mask;
@@ -41,12 +41,11 @@ EXPORT_SYMBOL(__mips_set_bit);
  */
 void __mips_clear_bit(unsigned long nr, volatile unsigned long *addr)
 {
-       unsigned long *a = (unsigned long *)addr;
-       unsigned bit = nr & SZLONG_MASK;
+       volatile unsigned long *a = &addr[BIT_WORD(nr)];
+       unsigned int bit = nr % BITS_PER_LONG;
        unsigned long mask;
        unsigned long flags;
 
-       a += nr >> SZLONG_LOG;
        mask = 1UL << bit;
        raw_local_irq_save(flags);
        *a &= ~mask;
@@ -63,12 +62,11 @@ EXPORT_SYMBOL(__mips_clear_bit);
  */
 void __mips_change_bit(unsigned long nr, volatile unsigned long *addr)
 {
-       unsigned long *a = (unsigned long *)addr;
-       unsigned bit = nr & SZLONG_MASK;
+       volatile unsigned long *a = &addr[BIT_WORD(nr)];
+       unsigned int bit = nr % BITS_PER_LONG;
        unsigned long mask;
        unsigned long flags;
 
-       a += nr >> SZLONG_LOG;
        mask = 1UL << bit;
        raw_local_irq_save(flags);
        *a ^= mask;
@@ -86,13 +84,12 @@ EXPORT_SYMBOL(__mips_change_bit);
 int __mips_test_and_set_bit_lock(unsigned long nr,
                                 volatile unsigned long *addr)
 {
-       unsigned long *a = (unsigned long *)addr;
-       unsigned bit = nr & SZLONG_MASK;
+       volatile unsigned long *a = &addr[BIT_WORD(nr)];
+       unsigned int bit = nr % BITS_PER_LONG;
        unsigned long mask;
        unsigned long flags;
        int res;
 
-       a += nr >> SZLONG_LOG;
        mask = 1UL << bit;
        raw_local_irq_save(flags);
        res = (mask & *a) != 0;
@@ -111,13 +108,12 @@ EXPORT_SYMBOL(__mips_test_and_set_bit_lock);
  */
 int __mips_test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
 {
-       unsigned long *a = (unsigned long *)addr;
-       unsigned bit = nr & SZLONG_MASK;
+       volatile unsigned long *a = &addr[BIT_WORD(nr)];
+       unsigned int bit = nr % BITS_PER_LONG;
        unsigned long mask;
        unsigned long flags;
        int res;
 
-       a += nr >> SZLONG_LOG;
        mask = 1UL << bit;
        raw_local_irq_save(flags);
        res = (mask & *a) != 0;
@@ -136,13 +132,12 @@ EXPORT_SYMBOL(__mips_test_and_clear_bit);
  */
 int __mips_test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
 {
-       unsigned long *a = (unsigned long *)addr;
-       unsigned bit = nr & SZLONG_MASK;
+       volatile unsigned long *a = &addr[BIT_WORD(nr)];
+       unsigned int bit = nr % BITS_PER_LONG;
        unsigned long mask;
        unsigned long flags;
        int res;
 
-       a += nr >> SZLONG_LOG;
        mask = 1UL << bit;
        raw_local_irq_save(flags);
        res = (mask & *a) != 0;