cmpxchg.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * xchg/cmpxchg operations for the Hexagon architecture
  4. *
  5. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  6. */
  7. #ifndef _ASM_CMPXCHG_H
  8. #define _ASM_CMPXCHG_H
  9. /*
  10. * __xchg - atomically exchange a register and a memory location
  11. * @x: value to swap
  12. * @ptr: pointer to memory
  13. * @size: size of the value
  14. *
  15. * Only 4 bytes supported currently.
  16. *
  17. * Note: there was an errata for V2 about .new's and memw_locked.
  18. *
  19. */
  20. static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
  21. int size)
  22. {
  23. unsigned long retval;
  24. /* Can't seem to use printk or panic here, so just stop */
  25. if (size != 4) do { asm volatile("brkpt;\n"); } while (1);
  26. __asm__ __volatile__ (
  27. "1: %0 = memw_locked(%1);\n" /* load into retval */
  28. " memw_locked(%1,P0) = %2;\n" /* store into memory */
  29. " if (!P0) jump 1b;\n"
  30. : "=&r" (retval)
  31. : "r" (ptr), "r" (x)
  32. : "memory", "p0"
  33. );
  34. return retval;
  35. }
  36. /*
  37. * Atomically swap the contents of a register with memory. Should be atomic
  38. * between multiple CPU's and within interrupts on the same CPU.
  39. */
  40. #define arch_xchg(ptr, v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), \
  41. sizeof(*(ptr))))
  42. /*
  43. * see rt-mutex-design.txt; cmpxchg supposedly checks if *ptr == A and swaps.
  44. * looks just like atomic_cmpxchg on our arch currently with a bunch of
  45. * variable casting.
  46. */
  47. #define arch_cmpxchg(ptr, old, new) \
  48. ({ \
  49. __typeof__(ptr) __ptr = (ptr); \
  50. __typeof__(*(ptr)) __old = (old); \
  51. __typeof__(*(ptr)) __new = (new); \
  52. __typeof__(*(ptr)) __oldval = 0; \
  53. \
  54. asm volatile( \
  55. "1: %0 = memw_locked(%1);\n" \
  56. " { P0 = cmp.eq(%0,%2);\n" \
  57. " if (!P0.new) jump:nt 2f; }\n" \
  58. " memw_locked(%1,p0) = %3;\n" \
  59. " if (!P0) jump 1b;\n" \
  60. "2:\n" \
  61. : "=&r" (__oldval) \
  62. : "r" (__ptr), "r" (__old), "r" (__new) \
  63. : "memory", "p0" \
  64. ); \
  65. __oldval; \
  66. })
  67. #endif /* _ASM_CMPXCHG_H */