cmpxchg.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * xchg/cmpxchg operations for the Hexagon architecture
  3. *
  4. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. */
  21. #ifndef _ASM_CMPXCHG_H
  22. #define _ASM_CMPXCHG_H
  23. /*
  24. * __xchg - atomically exchange a register and a memory location
  25. * @x: value to swap
  26. * @ptr: pointer to memory
  27. * @size: size of the value
  28. *
  29. * Only 4 bytes supported currently.
  30. *
  31. * Note: there was an errata for V2 about .new's and memw_locked.
  32. *
  33. */
  34. static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
  35. int size)
  36. {
  37. unsigned long retval;
  38. /* Can't seem to use printk or panic here, so just stop */
  39. if (size != 4) do { asm volatile("brkpt;\n"); } while (1);
  40. __asm__ __volatile__ (
  41. "1: %0 = memw_locked(%1);\n" /* load into retval */
  42. " memw_locked(%1,P0) = %2;\n" /* store into memory */
  43. " if !P0 jump 1b;\n"
  44. : "=&r" (retval)
  45. : "r" (ptr), "r" (x)
  46. : "memory", "p0"
  47. );
  48. return retval;
  49. }
  50. /*
  51. * Atomically swap the contents of a register with memory. Should be atomic
  52. * between multiple CPU's and within interrupts on the same CPU.
  53. */
  54. #define xchg(ptr, v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), \
  55. sizeof(*(ptr))))
  56. /*
  57. * see rt-mutex-design.txt; cmpxchg supposedly checks if *ptr == A and swaps.
  58. * looks just like atomic_cmpxchg on our arch currently with a bunch of
  59. * variable casting.
  60. */
  61. #define cmpxchg(ptr, old, new) \
  62. ({ \
  63. __typeof__(ptr) __ptr = (ptr); \
  64. __typeof__(*(ptr)) __old = (old); \
  65. __typeof__(*(ptr)) __new = (new); \
  66. __typeof__(*(ptr)) __oldval = 0; \
  67. \
  68. asm volatile( \
  69. "1: %0 = memw_locked(%1);\n" \
  70. " { P0 = cmp.eq(%0,%2);\n" \
  71. " if (!P0.new) jump:nt 2f; }\n" \
  72. " memw_locked(%1,p0) = %3;\n" \
  73. " if (!P0) jump 1b;\n" \
  74. "2:\n" \
  75. : "=&r" (__oldval) \
  76. : "r" (__ptr), "r" (__old), "r" (__new) \
  77. : "memory", "p0" \
  78. ); \
  79. __oldval; \
  80. })
  81. #endif /* _ASM_CMPXCHG_H */