Synchronization.S 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //------------------------------------------------------------------------------
  2. //
  3. // RISC-V synchronization functions.
  4. //
  5. // Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
  6. //
  7. // SPDX-License-Identifier: BSD-2-Clause-Patent
  8. //
  9. //------------------------------------------------------------------------------
  10. #include <Base.h>
  11. .data
  12. .text
  13. .align 3
  14. .global ASM_PFX(InternalSyncCompareExchange32)
  15. .global ASM_PFX(InternalSyncCompareExchange64)
  16. .global ASM_PFX(InternalSyncIncrement)
  17. .global ASM_PFX(InternalSyncDecrement)
  18. //
  19. // ompare and xchange a 32-bit value.
  20. //
  21. // @param a0 : Pointer to 32-bit value.
  22. // @param a1 : Compare value.
  23. // @param a2 : Exchange value.
  24. //
  25. ASM_PFX (InternalSyncCompareExchange32):
  26. lr.w a3, (a0) // Load the value from a0 and make
  27. // the reservation of address.
  28. bne a3, a1, exit
  29. sc.w a3, a2, (a0) // Write the value back to the address.
  30. mv a3, a1
  31. exit:
  32. mv a0, a3
  33. ret
  34. //
  35. // Compare and xchange a 64-bit value.
  36. //
  37. // @param a0 : Pointer to 64-bit value.
  38. // @param a1 : Compare value.
  39. // @param a2 : Exchange value.
  40. //
  41. ASM_PFX (InternalSyncCompareExchange64):
  42. lr.d a3, (a0) // Load the value from a0 and make
  43. // the reservation of address.
  44. bne a3, a1, exit
  45. sc.d a3, a2, (a0) // Write the value back to the address.
  46. mv a3, a1
  47. exit2:
  48. mv a0, a3
  49. ret
  50. //
  51. // Performs an atomic increment of an 32-bit unsigned integer.
  52. //
  53. // @param a0 : Pointer to 32-bit value.
  54. //
  55. ASM_PFX (InternalSyncIncrement):
  56. li a1, 1
  57. amoadd.w a2, a1, (a0)
  58. mv a0, a2
  59. ret
  60. //
  61. // Performs an atomic decrement of an 32-bit unsigned integer.
  62. //
  63. // @param a0 : Pointer to 32-bit value.
  64. //
  65. ASM_PFX (InternalSyncDecrement):
  66. li a1, -1
  67. amoadd.w a2, a1, (a0)
  68. mv a0, a2
  69. ret