ACPICA: Fixes for parameter validation.
[powerpc.git] / include / asm-mips / bitops.h
index a1728f8..06445de 100644 (file)
@@ -3,59 +3,40 @@
  * License.  See the file "COPYING" in the main directory of this archive
  * for more details.
  *
- * Copyright (c) 1994 - 1997, 1999, 2000  Ralf Baechle (ralf@gnu.org)
+ * Copyright (c) 1994 - 1997, 1999, 2000, 06  Ralf Baechle (ralf@linux-mips.org)
  * Copyright (c) 1999, 2000  Silicon Graphics, Inc.
  */
 #ifndef _ASM_BITOPS_H
 #define _ASM_BITOPS_H
 
-#include <linux/config.h>
 #include <linux/compiler.h>
+#include <linux/irqflags.h>
 #include <linux/types.h>
+#include <asm/barrier.h>
 #include <asm/bug.h>
 #include <asm/byteorder.h>             /* sigh ... */
 #include <asm/cpu-features.h>
+#include <asm/sgidefs.h>
+#include <asm/war.h>
 
 #if (_MIPS_SZLONG == 32)
 #define SZLONG_LOG 5
 #define SZLONG_MASK 31UL
 #define __LL           "ll     "
 #define __SC           "sc     "
-#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 cpu_to_lelongp(x) cpu_to_le64p((__u64 *) (x))
 #endif
 
-#ifdef __KERNEL__
-
-#include <asm/interrupt.h>
-#include <asm/sgidefs.h>
-#include <asm/war.h>
-
 /*
  * clear_bit() doesn't provide any barrier for the compiler.
  */
 #define smp_mb__before_clear_bit()     smp_mb()
 #define smp_mb__after_clear_bit()      smp_mb()
 
-/*
- * Only disable interrupt for kernel mode stuff to keep usermode stuff
- * that dares to use kernel include files alive.
- */
-
-#define __bi_flags                     unsigned long flags
-#define __bi_local_irq_save(x)         local_irq_save(x)
-#define __bi_local_irq_restore(x)      local_irq_restore(x)
-#else
-#define __bi_flags
-#define __bi_local_irq_save(x)
-#define __bi_local_irq_restore(x)
-#endif /* __KERNEL__ */
-
 /*
  * set_bit - Atomically set a bit in memory
  * @nr: the bit to set
@@ -94,13 +75,13 @@ static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
        } else {
                volatile unsigned long *a = addr;
                unsigned long mask;
-               __bi_flags;
+               unsigned long flags;
 
                a += nr >> SZLONG_LOG;
                mask = 1UL << (nr & SZLONG_MASK);
-               __bi_local_irq_save(flags);
+               local_irq_save(flags);
                *a |= mask;
-               __bi_local_irq_restore(flags);
+               local_irq_restore(flags);
        }
 }
 
@@ -142,13 +123,13 @@ static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
        } else {
                volatile unsigned long *a = addr;
                unsigned long mask;
-               __bi_flags;
+               unsigned long flags;
 
                a += nr >> SZLONG_LOG;
                mask = 1UL << (nr & SZLONG_MASK);
-               __bi_local_irq_save(flags);
+               local_irq_save(flags);
                *a &= ~mask;
-               __bi_local_irq_restore(flags);
+               local_irq_restore(flags);
        }
 }
 
@@ -192,13 +173,13 @@ static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
        } else {
                volatile unsigned long *a = addr;
                unsigned long mask;
-               __bi_flags;
+               unsigned long flags;
 
                a += nr >> SZLONG_LOG;
                mask = 1UL << (nr & SZLONG_MASK);
-               __bi_local_irq_save(flags);
+               local_irq_save(flags);
                *a ^= mask;
-               __bi_local_irq_restore(flags);
+               local_irq_restore(flags);
        }
 }
 
@@ -224,9 +205,6 @@ static inline int test_and_set_bit(unsigned long nr,
                "       " __SC  "%2, %1                                 \n"
                "       beqzl   %2, 1b                                  \n"
                "       and     %2, %0, %3                              \n"
-#ifdef CONFIG_SMP
-               "       sync                                            \n"
-#endif
                "       .set    mips0                                   \n"
                : "=&r" (temp), "=m" (*m), "=&r" (res)
                : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
@@ -246,9 +224,6 @@ static inline int test_and_set_bit(unsigned long nr,
                "       " __SC  "%2, %1                                 \n"
                "       beqz    %2, 1b                                  \n"
                "        and    %2, %0, %3                              \n"
-#ifdef CONFIG_SMP
-               "       sync                                            \n"
-#endif
                "       .set    pop                                     \n"
                : "=&r" (temp), "=m" (*m), "=&r" (res)
                : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
@@ -259,17 +234,19 @@ static inline int test_and_set_bit(unsigned long nr,
                volatile unsigned long *a = addr;
                unsigned long mask;
                int retval;
-               __bi_flags;
+               unsigned long flags;
 
                a += nr >> SZLONG_LOG;
                mask = 1UL << (nr & SZLONG_MASK);
-               __bi_local_irq_save(flags);
+               local_irq_save(flags);
                retval = (mask & *a) != 0;
                *a |= mask;
-               __bi_local_irq_restore(flags);
+               local_irq_restore(flags);
 
                return retval;
        }
+
+       smp_mb();
 }
 
 /*
@@ -295,9 +272,6 @@ static inline int test_and_clear_bit(unsigned long nr,
                "       " __SC  "%2, %1                                 \n"
                "       beqzl   %2, 1b                                  \n"
                "       and     %2, %0, %3                              \n"
-#ifdef CONFIG_SMP
-               "       sync                                            \n"
-#endif
                "       .set    mips0                                   \n"
                : "=&r" (temp), "=m" (*m), "=&r" (res)
                : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
@@ -318,9 +292,6 @@ static inline int test_and_clear_bit(unsigned long nr,
                "       " __SC  "%2, %1                                 \n"
                "       beqz    %2, 1b                                  \n"
                "        and    %2, %0, %3                              \n"
-#ifdef CONFIG_SMP
-               "       sync                                            \n"
-#endif
                "       .set    pop                                     \n"
                : "=&r" (temp), "=m" (*m), "=&r" (res)
                : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
@@ -331,17 +302,19 @@ static inline int test_and_clear_bit(unsigned long nr,
                volatile unsigned long *a = addr;
                unsigned long mask;
                int retval;
-               __bi_flags;
+               unsigned long flags;
 
                a += nr >> SZLONG_LOG;
                mask = 1UL << (nr & SZLONG_MASK);
-               __bi_local_irq_save(flags);
+               local_irq_save(flags);
                retval = (mask & *a) != 0;
                *a &= ~mask;
-               __bi_local_irq_restore(flags);
+               local_irq_restore(flags);
 
                return retval;
        }
+
+       smp_mb();
 }
 
 /*
@@ -366,9 +339,6 @@ static inline int test_and_change_bit(unsigned long nr,
                "       " __SC  "%2, %1                                 \n"
                "       beqzl   %2, 1b                                  \n"
                "       and     %2, %0, %3                              \n"
-#ifdef CONFIG_SMP
-               "       sync                                            \n"
-#endif
                "       .set    mips0                                   \n"
                : "=&r" (temp), "=m" (*m), "=&r" (res)
                : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
@@ -388,9 +358,6 @@ static inline int test_and_change_bit(unsigned long nr,
                "       " __SC  "\t%2, %1                               \n"
                "       beqz    %2, 1b                                  \n"
                "        and    %2, %0, %3                              \n"
-#ifdef CONFIG_SMP
-               "       sync                                            \n"
-#endif
                "       .set    pop                                     \n"
                : "=&r" (temp), "=m" (*m), "=&r" (res)
                : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
@@ -400,22 +367,20 @@ static inline int test_and_change_bit(unsigned long nr,
        } else {
                volatile unsigned long *a = addr;
                unsigned long mask, retval;
-               __bi_flags;
+               unsigned long flags;
 
                a += nr >> SZLONG_LOG;
                mask = 1UL << (nr & SZLONG_MASK);
-               __bi_local_irq_save(flags);
+               local_irq_save(flags);
                retval = (mask & *a) != 0;
                *a ^= mask;
-               __bi_local_irq_restore(flags);
+               local_irq_restore(flags);
 
                return retval;
        }
-}
 
-#undef __bi_flags
-#undef __bi_local_irq_save
-#undef __bi_local_irq_restore
+       smp_mb();
+}
 
 #include <asm-generic/bitops/non-atomic.h>
 
@@ -467,64 +432,56 @@ static inline unsigned long __ffs(unsigned long word)
 }
 
 /*
- * ffs - find first bit set.
+ * fls - find last bit set.
  * @word: The word to search
  *
- * Returns 1..SZLONG
- * Returns 0 if no bit exists
+ * This is defined the same way as ffs.
+ * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  */
-
-static inline unsigned long ffs(unsigned long word)
+static inline int fls(int word)
 {
-       if (!word)
-               return 0;
+       __asm__ ("clz %0, %1" : "=r" (word) : "r" (word));
 
-       return __ffs(word) + 1;
+       return 32 - word;
 }
 
-/*
- * 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)
+#if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPS64)
+static inline int fls64(__u64 word)
 {
-       return __ffs (~word);
+       __asm__ ("dclz %0, %1" : "=r" (word) : "r" (word));
+
+       return 64 - word;
 }
+#else
+#include <asm-generic/bitops/fls64.h>
+#endif
 
 /*
- * fls - find last bit set.
+ * ffs - find first bit set.
  * @word: The word to search
  *
- * Returns 1..SZLONG
- * Returns 0 if no bit exists
+ * This is defined the same way as
+ * the libc and compiler builtin ffs routines, therefore
+ * differs in spirit from the above ffz (man ffs).
  */
-static inline unsigned long fls(unsigned long word)
+static inline int ffs(int word)
 {
-#ifdef CONFIG_CPU_MIPS32
-       __asm__ ("clz %0, %1" : "=r" (word) : "r" (word));
-
-       return 32 - word;
-#endif
-
-#ifdef CONFIG_CPU_MIPS64
-       __asm__ ("dclz %0, %1" : "=r" (word) : "r" (word));
+       if (!word)
+               return 0;
 
-       return 64 - word;
-#endif
+       return fls(word & -word);
 }
 
 #else
 
 #include <asm-generic/bitops/__ffs.h>
 #include <asm-generic/bitops/ffs.h>
-#include <asm-generic/bitops/ffz.h>
 #include <asm-generic/bitops/fls.h>
+#include <asm-generic/bitops/fls64.h>
 
 #endif /*defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) */
 
-#include <asm-generic/bitops/fls64.h>
+#include <asm-generic/bitops/ffz.h>
 #include <asm-generic/bitops/find.h>
 
 #ifdef __KERNEL__