cmpxchg.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Atomic operations that C can't guarantee us. Useful for
  3. * resource counting etc.
  4. *
  5. * But use these as seldom as possible since they are slower than
  6. * regular operations.
  7. *
  8. * Copyright (C) 2004-2006 Atmel Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #ifndef __ASM_AVR32_CMPXCHG_H
  15. #define __ASM_AVR32_CMPXCHG_H
  16. #define xchg(ptr,x) \
  17. ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  18. extern void __xchg_called_with_bad_pointer(void);
  19. static inline unsigned long xchg_u32(u32 val, volatile u32 *m)
  20. {
  21. u32 ret;
  22. asm volatile("xchg %[ret], %[m], %[val]"
  23. : [ret] "=&r"(ret), "=m"(*m)
  24. : "m"(*m), [m] "r"(m), [val] "r"(val)
  25. : "memory");
  26. return ret;
  27. }
  28. static inline unsigned long __xchg(unsigned long x,
  29. volatile void *ptr,
  30. int size)
  31. {
  32. switch(size) {
  33. case 4:
  34. return xchg_u32(x, ptr);
  35. default:
  36. __xchg_called_with_bad_pointer();
  37. return x;
  38. }
  39. }
  40. static inline unsigned long __cmpxchg_u32(volatile int *m, unsigned long old,
  41. unsigned long new)
  42. {
  43. __u32 ret;
  44. asm volatile(
  45. "1: ssrf 5\n"
  46. " ld.w %[ret], %[m]\n"
  47. " cp.w %[ret], %[old]\n"
  48. " brne 2f\n"
  49. " stcond %[m], %[new]\n"
  50. " brne 1b\n"
  51. "2:\n"
  52. : [ret] "=&r"(ret), [m] "=m"(*m)
  53. : "m"(m), [old] "ir"(old), [new] "r"(new)
  54. : "memory", "cc");
  55. return ret;
  56. }
  57. extern unsigned long __cmpxchg_u64_unsupported_on_32bit_kernels(
  58. volatile int * m, unsigned long old, unsigned long new);
  59. #define __cmpxchg_u64 __cmpxchg_u64_unsupported_on_32bit_kernels
  60. /* This function doesn't exist, so you'll get a linker error
  61. if something tries to do an invalid cmpxchg(). */
  62. extern void __cmpxchg_called_with_bad_pointer(void);
  63. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  64. unsigned long new, int size)
  65. {
  66. switch (size) {
  67. case 4:
  68. return __cmpxchg_u32(ptr, old, new);
  69. case 8:
  70. return __cmpxchg_u64(ptr, old, new);
  71. }
  72. __cmpxchg_called_with_bad_pointer();
  73. return old;
  74. }
  75. #define cmpxchg(ptr, old, new) \
  76. ((typeof(*(ptr)))__cmpxchg((ptr), (unsigned long)(old), \
  77. (unsigned long)(new), \
  78. sizeof(*(ptr))))
  79. #include <asm-generic/cmpxchg-local.h>
  80. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  81. unsigned long old,
  82. unsigned long new, int size)
  83. {
  84. switch (size) {
  85. case 4:
  86. return __cmpxchg_u32(ptr, old, new);
  87. default:
  88. return __cmpxchg_local_generic(ptr, old, new, size);
  89. }
  90. return old;
  91. }
  92. #define cmpxchg_local(ptr, old, new) \
  93. ((typeof(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(old), \
  94. (unsigned long)(new), \
  95. sizeof(*(ptr))))
  96. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  97. #endif /* __ASM_AVR32_CMPXCHG_H */