word-at-a-time.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_ARM_WORD_AT_A_TIME_H
  3. #define __ASM_ARM_WORD_AT_A_TIME_H
  4. #ifndef __ARMEB__
  5. /*
  6. * Little-endian word-at-a-time zero byte handling.
  7. * Heavily based on the x86 algorithm.
  8. */
  9. #include <linux/kernel.h>
  10. struct word_at_a_time {
  11. const unsigned long one_bits, high_bits;
  12. };
  13. #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
  14. static inline unsigned long has_zero(unsigned long a, unsigned long *bits,
  15. const struct word_at_a_time *c)
  16. {
  17. unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
  18. *bits = mask;
  19. return mask;
  20. }
  21. #define prep_zero_mask(a, bits, c) (bits)
  22. static inline unsigned long create_zero_mask(unsigned long bits)
  23. {
  24. bits = (bits - 1) & ~bits;
  25. return bits >> 7;
  26. }
  27. static inline unsigned long find_zero(unsigned long mask)
  28. {
  29. unsigned long ret;
  30. #if __LINUX_ARM_ARCH__ >= 5
  31. /* We have clz available. */
  32. ret = fls(mask) >> 3;
  33. #else
  34. /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
  35. ret = (0x0ff0001 + mask) >> 23;
  36. /* Fix the 1 for 00 case */
  37. ret &= mask;
  38. #endif
  39. return ret;
  40. }
  41. #define zero_bytemask(mask) (mask)
  42. #else /* __ARMEB__ */
  43. #include <asm-generic/word-at-a-time.h>
  44. #endif
  45. #ifdef CONFIG_DCACHE_WORD_ACCESS
  46. /*
  47. * Load an unaligned word from kernel space.
  48. *
  49. * In the (very unlikely) case of the word being a page-crosser
  50. * and the next page not being mapped, take the exception and
  51. * return zeroes in the non-existing part.
  52. */
  53. static inline unsigned long load_unaligned_zeropad(const void *addr)
  54. {
  55. unsigned long ret, offset;
  56. /* Load word from unaligned pointer addr */
  57. asm(
  58. "1: ldr %0, [%2]\n"
  59. "2:\n"
  60. " .pushsection .text.fixup,\"ax\"\n"
  61. " .align 2\n"
  62. "3: and %1, %2, #0x3\n"
  63. " bic %2, %2, #0x3\n"
  64. " ldr %0, [%2]\n"
  65. " lsl %1, %1, #0x3\n"
  66. #ifndef __ARMEB__
  67. " lsr %0, %0, %1\n"
  68. #else
  69. " lsl %0, %0, %1\n"
  70. #endif
  71. " b 2b\n"
  72. " .popsection\n"
  73. " .pushsection __ex_table,\"a\"\n"
  74. " .align 3\n"
  75. " .long 1b, 3b\n"
  76. " .popsection"
  77. : "=&r" (ret), "=&r" (offset)
  78. : "r" (addr), "Qo" (*(unsigned long *)addr));
  79. return ret;
  80. }
  81. #endif /* DCACHE_WORD_ACCESS */
  82. #endif /* __ASM_ARM_WORD_AT_A_TIME_H */