bitops_32.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #ifndef _ASM_TILE_BITOPS_32_H
  15. #define _ASM_TILE_BITOPS_32_H
  16. #include <linux/compiler.h>
  17. #include <asm/barrier.h>
  18. /* Tile-specific routines to support <asm/bitops.h>. */
  19. unsigned long _atomic_or(volatile unsigned long *p, unsigned long mask);
  20. unsigned long _atomic_andn(volatile unsigned long *p, unsigned long mask);
  21. unsigned long _atomic_xor(volatile unsigned long *p, unsigned long mask);
  22. /**
  23. * set_bit - Atomically set a bit in memory
  24. * @nr: the bit to set
  25. * @addr: the address to start counting from
  26. *
  27. * This function is atomic and may not be reordered.
  28. * See __set_bit() if you do not require the atomic guarantees.
  29. * Note that @nr may be almost arbitrarily large; this function is not
  30. * restricted to acting on a single-word quantity.
  31. */
  32. static inline void set_bit(unsigned nr, volatile unsigned long *addr)
  33. {
  34. _atomic_or(addr + BIT_WORD(nr), BIT_MASK(nr));
  35. }
  36. /**
  37. * clear_bit - Clears a bit in memory
  38. * @nr: Bit to clear
  39. * @addr: Address to start counting from
  40. *
  41. * clear_bit() is atomic and may not be reordered.
  42. * See __clear_bit() if you do not require the atomic guarantees.
  43. * Note that @nr may be almost arbitrarily large; this function is not
  44. * restricted to acting on a single-word quantity.
  45. *
  46. * clear_bit() may not contain a memory barrier, so if it is used for
  47. * locking purposes, you should call smp_mb__before_atomic() and/or
  48. * smp_mb__after_atomic() to ensure changes are visible on other cpus.
  49. */
  50. static inline void clear_bit(unsigned nr, volatile unsigned long *addr)
  51. {
  52. _atomic_andn(addr + BIT_WORD(nr), BIT_MASK(nr));
  53. }
  54. /**
  55. * change_bit - Toggle a bit in memory
  56. * @nr: Bit to change
  57. * @addr: Address to start counting from
  58. *
  59. * change_bit() is atomic and may not be reordered.
  60. * See __change_bit() if you do not require the atomic guarantees.
  61. * Note that @nr may be almost arbitrarily large; this function is not
  62. * restricted to acting on a single-word quantity.
  63. */
  64. static inline void change_bit(unsigned nr, volatile unsigned long *addr)
  65. {
  66. _atomic_xor(addr + BIT_WORD(nr), BIT_MASK(nr));
  67. }
  68. /**
  69. * test_and_set_bit - Set a bit and return its old value
  70. * @nr: Bit to set
  71. * @addr: Address to count from
  72. *
  73. * This operation is atomic and cannot be reordered.
  74. * It also implies a memory barrier.
  75. */
  76. static inline int test_and_set_bit(unsigned nr, volatile unsigned long *addr)
  77. {
  78. unsigned long mask = BIT_MASK(nr);
  79. addr += BIT_WORD(nr);
  80. smp_mb(); /* barrier for proper semantics */
  81. return (_atomic_or(addr, mask) & mask) != 0;
  82. }
  83. /**
  84. * test_and_clear_bit - Clear a bit and return its old value
  85. * @nr: Bit to clear
  86. * @addr: Address to count from
  87. *
  88. * This operation is atomic and cannot be reordered.
  89. * It also implies a memory barrier.
  90. */
  91. static inline int test_and_clear_bit(unsigned nr, volatile unsigned long *addr)
  92. {
  93. unsigned long mask = BIT_MASK(nr);
  94. addr += BIT_WORD(nr);
  95. smp_mb(); /* barrier for proper semantics */
  96. return (_atomic_andn(addr, mask) & mask) != 0;
  97. }
  98. /**
  99. * test_and_change_bit - Change a bit and return its old value
  100. * @nr: Bit to change
  101. * @addr: Address to count from
  102. *
  103. * This operation is atomic and cannot be reordered.
  104. * It also implies a memory barrier.
  105. */
  106. static inline int test_and_change_bit(unsigned nr,
  107. volatile unsigned long *addr)
  108. {
  109. unsigned long mask = BIT_MASK(nr);
  110. addr += BIT_WORD(nr);
  111. smp_mb(); /* barrier for proper semantics */
  112. return (_atomic_xor(addr, mask) & mask) != 0;
  113. }
  114. #include <asm-generic/bitops/ext2-atomic.h>
  115. #endif /* _ASM_TILE_BITOPS_32_H */