cmpxchg.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef __ARCH_M68K_CMPXCHG__
  2. #define __ARCH_M68K_CMPXCHG__
  3. #include <linux/irqflags.h>
  4. struct __xchg_dummy { unsigned long a[100]; };
  5. #define __xg(x) ((volatile struct __xchg_dummy *)(x))
  6. extern unsigned long __invalid_xchg_size(unsigned long, volatile void *, int);
  7. #ifndef CONFIG_RMW_INSNS
  8. static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
  9. {
  10. unsigned long flags, tmp;
  11. local_irq_save(flags);
  12. switch (size) {
  13. case 1:
  14. tmp = *(u8 *)ptr;
  15. *(u8 *)ptr = x;
  16. x = tmp;
  17. break;
  18. case 2:
  19. tmp = *(u16 *)ptr;
  20. *(u16 *)ptr = x;
  21. x = tmp;
  22. break;
  23. case 4:
  24. tmp = *(u32 *)ptr;
  25. *(u32 *)ptr = x;
  26. x = tmp;
  27. break;
  28. default:
  29. tmp = __invalid_xchg_size(x, ptr, size);
  30. break;
  31. }
  32. local_irq_restore(flags);
  33. return x;
  34. }
  35. #else
  36. static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
  37. {
  38. switch (size) {
  39. case 1:
  40. __asm__ __volatile__
  41. ("moveb %2,%0\n\t"
  42. "1:\n\t"
  43. "casb %0,%1,%2\n\t"
  44. "jne 1b"
  45. : "=&d" (x) : "d" (x), "m" (*__xg(ptr)) : "memory");
  46. break;
  47. case 2:
  48. __asm__ __volatile__
  49. ("movew %2,%0\n\t"
  50. "1:\n\t"
  51. "casw %0,%1,%2\n\t"
  52. "jne 1b"
  53. : "=&d" (x) : "d" (x), "m" (*__xg(ptr)) : "memory");
  54. break;
  55. case 4:
  56. __asm__ __volatile__
  57. ("movel %2,%0\n\t"
  58. "1:\n\t"
  59. "casl %0,%1,%2\n\t"
  60. "jne 1b"
  61. : "=&d" (x) : "d" (x), "m" (*__xg(ptr)) : "memory");
  62. break;
  63. default:
  64. x = __invalid_xchg_size(x, ptr, size);
  65. break;
  66. }
  67. return x;
  68. }
  69. #endif
  70. #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  71. #include <asm-generic/cmpxchg-local.h>
  72. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  73. extern unsigned long __invalid_cmpxchg_size(volatile void *,
  74. unsigned long, unsigned long, int);
  75. /*
  76. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  77. * store NEW in MEM. Return the initial value in MEM. Success is
  78. * indicated by comparing RETURN with OLD.
  79. */
  80. #ifdef CONFIG_RMW_INSNS
  81. static inline unsigned long __cmpxchg(volatile void *p, unsigned long old,
  82. unsigned long new, int size)
  83. {
  84. switch (size) {
  85. case 1:
  86. __asm__ __volatile__ ("casb %0,%2,%1"
  87. : "=d" (old), "=m" (*(char *)p)
  88. : "d" (new), "0" (old), "m" (*(char *)p));
  89. break;
  90. case 2:
  91. __asm__ __volatile__ ("casw %0,%2,%1"
  92. : "=d" (old), "=m" (*(short *)p)
  93. : "d" (new), "0" (old), "m" (*(short *)p));
  94. break;
  95. case 4:
  96. __asm__ __volatile__ ("casl %0,%2,%1"
  97. : "=d" (old), "=m" (*(int *)p)
  98. : "d" (new), "0" (old), "m" (*(int *)p));
  99. break;
  100. default:
  101. old = __invalid_cmpxchg_size(p, old, new, size);
  102. break;
  103. }
  104. return old;
  105. }
  106. #define cmpxchg(ptr, o, n) \
  107. ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \
  108. (unsigned long)(n), sizeof(*(ptr))))
  109. #define cmpxchg_local(ptr, o, n) \
  110. ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \
  111. (unsigned long)(n), sizeof(*(ptr))))
  112. #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
  113. #else
  114. /*
  115. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  116. * them available.
  117. */
  118. #define cmpxchg_local(ptr, o, n) \
  119. ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
  120. (unsigned long)(n), sizeof(*(ptr))))
  121. #include <asm-generic/cmpxchg.h>
  122. #endif
  123. #endif /* __ARCH_M68K_CMPXCHG__ */