[SPARC64]: Dynamically grow TSB in response to RSS growth.
[powerpc.git] / include / asm-mips / bitops.h
index 1dc3587..8e80205 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/config.h>
 #include <linux/compiler.h>
 #include <linux/types.h>
+#include <asm/bug.h>
 #include <asm/byteorder.h>             /* sigh ... */
 #include <asm/cpu-features.h>
 
 #define SZLONG_MASK 31UL
 #define __LL           "ll     "
 #define __SC           "sc     "
-#define __SET_MIPS     ".set   mips2   "
 #define cpu_to_lelongp(x) cpu_to_le32p((__u32 *) (x))
 #elif (_MIPS_SZLONG == 64)
 #define SZLONG_LOG 6
 #define SZLONG_MASK 63UL
 #define __LL           "lld    "
 #define __SC           "scd    "
-#define __SET_MIPS     ".set   mips3   "
 #define cpu_to_lelongp(x) cpu_to_le64p((__u64 *) (x))
 #endif
 
@@ -74,7 +73,7 @@ static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
 
        if (cpu_has_llsc && R10000_LLSC_WAR) {
                __asm__ __volatile__(
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL "%0, %1                  # set_bit       \n"
                "       or      %0, %2                                  \n"
                "       " __SC  "%0, %1                                 \n"
@@ -84,7 +83,7 @@ static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
                : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
        } else if (cpu_has_llsc) {
                __asm__ __volatile__(
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL "%0, %1                  # set_bit       \n"
                "       or      %0, %2                                  \n"
                "       " __SC  "%0, %1                                 \n"
@@ -138,7 +137,7 @@ static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
 
        if (cpu_has_llsc && R10000_LLSC_WAR) {
                __asm__ __volatile__(
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL "%0, %1                  # clear_bit     \n"
                "       and     %0, %2                                  \n"
                "       " __SC "%0, %1                                  \n"
@@ -148,7 +147,7 @@ static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
                : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
        } else if (cpu_has_llsc) {
                __asm__ __volatile__(
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL "%0, %1                  # clear_bit     \n"
                "       and     %0, %2                                  \n"
                "       " __SC "%0, %1                                  \n"
@@ -201,7 +200,7 @@ static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
                unsigned long temp;
 
                __asm__ __volatile__(
-               "       " __SET_MIPS "                          \n"
+               "       .set    mips3                           \n"
                "1:     " __LL "%0, %1          # change_bit    \n"
                "       xor     %0, %2                          \n"
                "       " __SC  "%0, %1                         \n"
@@ -214,7 +213,7 @@ static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
                unsigned long temp;
 
                __asm__ __volatile__(
-               "       " __SET_MIPS "                          \n"
+               "       .set    mips3                           \n"
                "1:     " __LL "%0, %1          # change_bit    \n"
                "       xor     %0, %2                          \n"
                "       " __SC  "%0, %1                         \n"
@@ -267,7 +266,7 @@ static inline int test_and_set_bit(unsigned long nr,
                unsigned long temp, res;
 
                __asm__ __volatile__(
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL "%0, %1          # test_and_set_bit      \n"
                "       or      %2, %0, %3                              \n"
                "       " __SC  "%2, %1                                 \n"
@@ -289,7 +288,7 @@ static inline int test_and_set_bit(unsigned long nr,
                __asm__ __volatile__(
                "       .set    push                                    \n"
                "       .set    noreorder                               \n"
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL "%0, %1          # test_and_set_bit      \n"
                "       or      %2, %0, %3                              \n"
                "       " __SC  "%2, %1                                 \n"
@@ -361,7 +360,7 @@ static inline int test_and_clear_bit(unsigned long nr,
                unsigned long temp, res;
 
                __asm__ __volatile__(
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL  "%0, %1         # test_and_clear_bit    \n"
                "       or      %2, %0, %3                              \n"
                "       xor     %2, %3                                  \n"
@@ -384,7 +383,7 @@ static inline int test_and_clear_bit(unsigned long nr,
                __asm__ __volatile__(
                "       .set    push                                    \n"
                "       .set    noreorder                               \n"
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL  "%0, %1         # test_and_clear_bit    \n"
                "       or      %2, %0, %3                              \n"
                "       xor     %2, %3                                  \n"
@@ -457,7 +456,7 @@ static inline int test_and_change_bit(unsigned long nr,
                unsigned long temp, res;
 
                __asm__ __volatile__(
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL  "%0, %1         # test_and_change_bit   \n"
                "       xor     %2, %0, %3                              \n"
                "       " __SC  "%2, %1                                 \n"
@@ -479,7 +478,7 @@ static inline int test_and_change_bit(unsigned long nr,
                __asm__ __volatile__(
                "       .set    push                                    \n"
                "       .set    noreorder                               \n"
-               "       " __SET_MIPS "                                  \n"
+               "       .set    mips3                                   \n"
                "1:     " __LL  "%0, %1         # test_and_change_bit   \n"
                "       xor     %2, %0, %3                              \n"
                "       " __SC  "\t%2, %1                               \n"
@@ -549,22 +548,60 @@ static inline int test_bit(unsigned long nr, const volatile unsigned long *addr)
 }
 
 /*
- * ffz - find first zero in word.
+ * Return the bit position (0..63) of the most significant 1 bit in a word
+ * Returns -1 if no 1 bit exists
+ */
+static inline int __ilog2(unsigned long x)
+{
+       int lz;
+
+       if (sizeof(x) == 4) {
+               __asm__ (
+               "       .set    push                                    \n"
+               "       .set    mips32                                  \n"
+               "       clz     %0, %1                                  \n"
+               "       .set    pop                                     \n"
+               : "=r" (lz)
+               : "r" (x));
+
+               return 31 - lz;
+       }
+
+       BUG_ON(sizeof(x) != 8);
+
+       __asm__ (
+       "       .set    push                                            \n"
+       "       .set    mips64                                          \n"
+       "       dclz    %0, %1                                          \n"
+       "       .set    pop                                             \n"
+       : "=r" (lz)
+       : "r" (x));
+
+       return 63 - lz;
+}
+
+/*
+ * __ffs - find first bit in word.
  * @word: The word to search
  *
- * Undefined if no zero exists, so code should check against ~0UL first.
+ * Returns 0..SZLONG-1
+ * Undefined if no bit exists, so code should check against 0 first.
  */
-static inline unsigned long ffz(unsigned long word)
+static inline unsigned long __ffs(unsigned long word)
 {
+#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
+       return __ilog2(word & -word);
+#else
        int b = 0, s;
 
-       word = ~word;
 #ifdef CONFIG_32BIT
        s = 16; if (word << 16 != 0) s = 0; b += s; word >>= s;
        s =  8; if (word << 24 != 0) s = 0; b += s; word >>= s;
        s =  4; if (word << 28 != 0) s = 0; b += s; word >>= s;
        s =  2; if (word << 30 != 0) s = 0; b += s; word >>= s;
        s =  1; if (word << 31 != 0) s = 0; b += s;
+
+       return b;
 #endif
 #ifdef CONFIG_64BIT
        s = 32; if (word << 32 != 0) s = 0; b += s; word >>= s;
@@ -573,27 +610,98 @@ static inline unsigned long ffz(unsigned long word)
        s =  4; if (word << 60 != 0) s = 0; b += s; word >>= s;
        s =  2; if (word << 62 != 0) s = 0; b += s; word >>= s;
        s =  1; if (word << 63 != 0) s = 0; b += s;
-#endif
 
        return b;
+#endif
+#endif
 }
 
 /*
- * __ffs - find first bit in word.
+ * ffs - find first bit set.
  * @word: The word to search
  *
- * Undefined if no bit exists, so code should check against 0 first.
+ * Returns 1..SZLONG
+ * Returns 0 if no bit exists
  */
-static inline unsigned long __ffs(unsigned long word)
+
+static inline unsigned long ffs(unsigned long word)
 {
-       return ffz(~word);
+       if (!word)
+               return 0;
+
+       return __ffs(word) + 1;
 }
 
 /*
- * fls: find last bit set.
+ * ffz - find first zero in word.
+ * @word: The word to search
+ *
+ * Undefined if no zero exists, so code should check against ~0UL first.
  */
+static inline unsigned long ffz(unsigned long word)
+{
+       return __ffs (~word);
+}
 
-#define fls(x) generic_fls(x)
+/*
+ * fls - find last bit set.
+ * @word: The word to search
+ *
+ * Returns 1..SZLONG
+ * Returns 0 if no bit exists
+ */
+static inline unsigned long fls(unsigned long word)
+{
+#ifdef CONFIG_32BIT
+#ifdef CONFIG_CPU_MIPS32
+       __asm__ ("clz %0, %1" : "=r" (word) : "r" (word));
+
+       return 32 - word;
+#else
+       {
+       int r = 32, s;
+
+       if (word == 0)
+               return 0;
+
+       s = 16; if ((word & 0xffff0000)) s = 0; r -= s; word <<= s;
+       s = 8;  if ((word & 0xff000000)) s = 0; r -= s; word <<= s;
+       s = 4;  if ((word & 0xf0000000)) s = 0; r -= s; word <<= s;
+       s = 2;  if ((word & 0xc0000000)) s = 0; r -= s; word <<= s;
+       s = 1;  if ((word & 0x80000000)) s = 0; r -= s;
+
+       return r;
+       }
+#endif
+#endif /* CONFIG_32BIT */
+
+#ifdef CONFIG_64BIT
+#ifdef CONFIG_CPU_MIPS64
+
+       __asm__ ("dclz %0, %1" : "=r" (word) : "r" (word));
+
+       return 64 - word;
+#else
+       {
+       int r = 64, s;
+
+       if (word == 0)
+               return 0;
+
+       s = 32; if ((word & 0xffffffff00000000UL)) s = 0; r -= s; word <<= s;
+       s = 16; if ((word & 0xffff000000000000UL)) s = 0; r -= s; word <<= s;
+       s = 8;  if ((word & 0xff00000000000000UL)) s = 0; r -= s; word <<= s;
+       s = 4;  if ((word & 0xf000000000000000UL)) s = 0; r -= s; word <<= s;
+       s = 2;  if ((word & 0xc000000000000000UL)) s = 0; r -= s; word <<= s;
+       s = 1;  if ((word & 0x8000000000000000UL)) s = 0; r -= s;
+
+       return r;
+       }
+#endif
+#endif /* CONFIG_64BIT */
+}
+
+#define fls64(x)   generic_fls64(x)
 
 /*
  * find_next_zero_bit - find the first zero bit in a memory region
@@ -729,17 +837,6 @@ static inline int sched_find_first_bit(const unsigned long *b)
 #endif
 }
 
-/*
- * ffs - find first bit set
- * @x: the word to search
- *
- * This is defined the same way as
- * the libc and compiler builtin ffs routines, therefore
- * differs in spirit from the above ffz (man ffs).
- */
-
-#define ffs(x) generic_ffs(x)
-
 /*
  * hweightN - returns the hamming weight of a N-bit word
  * @x: the word to weigh