cmpxchg-xchg.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef __ASM_SH_CMPXCHG_XCHG_H
  2. #define __ASM_SH_CMPXCHG_XCHG_H
  3. /*
  4. * Copyright (C) 2016 Red Hat, Inc.
  5. * Author: Michael S. Tsirkin <[email protected]>
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2. See the
  8. * file "COPYING" in the main directory of this archive for more details.
  9. */
  10. #include <linux/bitops.h>
  11. #include <asm/byteorder.h>
  12. /*
  13. * Portable implementations of 1 and 2 byte xchg using a 4 byte cmpxchg.
  14. * Note: this header isn't self-contained: before including it, __cmpxchg_u32
  15. * must be defined first.
  16. */
  17. static inline u32 __xchg_cmpxchg(volatile void *ptr, u32 x, int size)
  18. {
  19. int off = (unsigned long)ptr % sizeof(u32);
  20. volatile u32 *p = ptr - off;
  21. #ifdef __BIG_ENDIAN
  22. int bitoff = (sizeof(u32) - size - off) * BITS_PER_BYTE;
  23. #else
  24. int bitoff = off * BITS_PER_BYTE;
  25. #endif
  26. u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;
  27. u32 oldv, newv;
  28. u32 ret;
  29. do {
  30. oldv = READ_ONCE(*p);
  31. ret = (oldv & bitmask) >> bitoff;
  32. newv = (oldv & ~bitmask) | (x << bitoff);
  33. } while (__cmpxchg_u32(p, oldv, newv) != oldv);
  34. return ret;
  35. }
  36. static inline unsigned long xchg_u16(volatile u16 *m, unsigned long val)
  37. {
  38. return __xchg_cmpxchg(m, val, sizeof *m);
  39. }
  40. static inline unsigned long xchg_u8(volatile u8 *m, unsigned long val)
  41. {
  42. return __xchg_cmpxchg(m, val, sizeof *m);
  43. }
  44. #endif /* __ASM_SH_CMPXCHG_XCHG_H */