cmpxchg.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2004 Microtronix Datacom Ltd.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. */
  8. #ifndef _ASM_NIOS2_CMPXCHG_H
  9. #define _ASM_NIOS2_CMPXCHG_H
  10. #include <linux/irqflags.h>
  11. #define xchg(ptr, x) \
  12. ((__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr), sizeof(*(ptr))))
  13. struct __xchg_dummy { unsigned long a[100]; };
  14. #define __xg(x) ((volatile struct __xchg_dummy *)(x))
  15. static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
  16. int size)
  17. {
  18. unsigned long tmp, flags;
  19. local_irq_save(flags);
  20. switch (size) {
  21. case 1:
  22. __asm__ __volatile__(
  23. "ldb %0, %2\n"
  24. "stb %1, %2\n"
  25. : "=&r" (tmp)
  26. : "r" (x), "m" (*__xg(ptr))
  27. : "memory");
  28. break;
  29. case 2:
  30. __asm__ __volatile__(
  31. "ldh %0, %2\n"
  32. "sth %1, %2\n"
  33. : "=&r" (tmp)
  34. : "r" (x), "m" (*__xg(ptr))
  35. : "memory");
  36. break;
  37. case 4:
  38. __asm__ __volatile__(
  39. "ldw %0, %2\n"
  40. "stw %1, %2\n"
  41. : "=&r" (tmp)
  42. : "r" (x), "m" (*__xg(ptr))
  43. : "memory");
  44. break;
  45. }
  46. local_irq_restore(flags);
  47. return tmp;
  48. }
  49. #include <asm-generic/cmpxchg.h>
  50. #include <asm-generic/cmpxchg-local.h>
  51. #endif /* _ASM_NIOS2_CMPXCHG_H */