bitops.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 1995, Russell King.
  3. * Various bits and pieces copyrights include:
  4. * Linus Torvalds (test_bit).
  5. *
  6. * Copyright (C) 2017 Andes Technology Corporation
  7. * Rick Chen, Andes Technology Corporation <rick@andestech.com>
  8. *
  9. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  10. *
  11. * Please note that the code in this file should never be included
  12. * from user space. Many of these are not implemented in assembler
  13. * since they would be too costly. Also, they require priviledged
  14. * instructions (which are not available from user mode) to ensure
  15. * that they are atomic.
  16. */
  17. #ifndef __ASM_RISCV_BITOPS_H
  18. #define __ASM_RISCV_BITOPS_H
  19. #ifdef __KERNEL__
  20. #include <asm/system.h>
  21. #include <asm-generic/bitops/fls.h>
  22. #include <asm-generic/bitops/__fls.h>
  23. #include <asm-generic/bitops/fls64.h>
  24. #include <asm-generic/bitops/__ffs.h>
  25. #define smp_mb__before_clear_bit() do { } while (0)
  26. #define smp_mb__after_clear_bit() do { } while (0)
  27. /*
  28. * Function prototypes to keep gcc -Wall happy.
  29. */
  30. static inline void __set_bit(int nr, void *addr)
  31. {
  32. int *a = (int *)addr;
  33. int mask;
  34. a += nr >> 5;
  35. mask = 1 << (nr & 0x1f);
  36. *a |= mask;
  37. }
  38. #define PLATFORM__SET_BIT
  39. static inline void __clear_bit(int nr, void *addr)
  40. {
  41. int *a = (int *)addr;
  42. int mask;
  43. a += nr >> 5;
  44. mask = 1 << (nr & 0x1f);
  45. *a &= ~mask;
  46. }
  47. #define PLATFORM__CLEAR_BIT
  48. static inline void __change_bit(int nr, void *addr)
  49. {
  50. int mask;
  51. unsigned long *ADDR = (unsigned long *)addr;
  52. ADDR += nr >> 5;
  53. mask = 1 << (nr & 31);
  54. *ADDR ^= mask;
  55. }
  56. static inline int __test_and_set_bit(int nr, void *addr)
  57. {
  58. int mask, retval;
  59. unsigned int *a = (unsigned int *)addr;
  60. a += nr >> 5;
  61. mask = 1 << (nr & 0x1f);
  62. retval = (mask & *a) != 0;
  63. *a |= mask;
  64. return retval;
  65. }
  66. static inline int __test_and_clear_bit(int nr, void *addr)
  67. {
  68. int mask, retval;
  69. unsigned int *a = (unsigned int *)addr;
  70. a += nr >> 5;
  71. mask = 1 << (nr & 0x1f);
  72. retval = (mask & *a) != 0;
  73. *a &= ~mask;
  74. return retval;
  75. }
  76. static inline int __test_and_change_bit(int nr, void *addr)
  77. {
  78. int mask, retval;
  79. unsigned int *a = (unsigned int *)addr;
  80. a += nr >> 5;
  81. mask = 1 << (nr & 0x1f);
  82. retval = (mask & *a) != 0;
  83. *a ^= mask;
  84. return retval;
  85. }
  86. /*
  87. * This routine doesn't need to be atomic.
  88. */
  89. static inline int test_bit(int nr, const void *addr)
  90. {
  91. return ((unsigned char *)addr)[nr >> 3] & (1U << (nr & 7));
  92. }
  93. /*
  94. * ffz = Find First Zero in word. Undefined if no zero exists,
  95. * so code should check against ~0UL first..
  96. */
  97. static inline unsigned long ffz(unsigned long word)
  98. {
  99. int k;
  100. word = ~word;
  101. k = 31;
  102. if (word & 0x0000ffff) {
  103. k -= 16; word <<= 16;
  104. }
  105. if (word & 0x00ff0000) {
  106. k -= 8; word <<= 8;
  107. }
  108. if (word & 0x0f000000) {
  109. k -= 4; word <<= 4;
  110. }
  111. if (word & 0x30000000) {
  112. k -= 2; word <<= 2;
  113. }
  114. if (word & 0x40000000)
  115. k -= 1;
  116. return k;
  117. }
  118. /*
  119. * ffs: find first bit set. This is defined the same way as
  120. * the libc and compiler builtin ffs routines, therefore
  121. * differs in spirit from the above ffz (man ffs).
  122. */
  123. /*
  124. * redefined in include/linux/bitops.h
  125. * #define ffs(x) generic_ffs(x)
  126. */
  127. /*
  128. * hweightN: returns the hamming weight (i.e. the number
  129. * of bits set) of a N-bit word
  130. */
  131. #define hweight32(x) generic_hweight32(x)
  132. #define hweight16(x) generic_hweight16(x)
  133. #define hweight8(x) generic_hweight8(x)
  134. #define ext2_set_bit test_and_set_bit
  135. #define ext2_clear_bit test_and_clear_bit
  136. #define ext2_test_bit test_bit
  137. #define ext2_find_first_zero_bit find_first_zero_bit
  138. #define ext2_find_next_zero_bit find_next_zero_bit
  139. /* Bitmap functions for the minix filesystem. */
  140. #define minix_test_and_set_bit(nr, addr) test_and_set_bit(nr, addr)
  141. #define minix_set_bit(nr, addr) set_bit(nr, addr)
  142. #define minix_test_and_clear_bit(nr, addr) test_and_clear_bit(nr, addr)
  143. #define minix_test_bit(nr, addr) test_bit(nr, addr)
  144. #define minix_find_first_zero_bit(addr, size) find_first_zero_bit(addr, size)
  145. #endif /* __KERNEL__ */
  146. #endif /* __ASM_RISCV_BITOPS_H */