riscv_locks.c 779 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #include <sbi/riscv_barrier.h>
  10. #include <sbi/riscv_locks.h>
  11. int spin_lock_check(spinlock_t *lock)
  12. {
  13. return (lock->lock == __RISCV_SPIN_UNLOCKED) ? 0 : 1;
  14. }
  15. int spin_trylock(spinlock_t *lock)
  16. {
  17. int tmp = 1, busy;
  18. __asm__ __volatile__ (
  19. " amoswap.w %0, %2, %1\n"
  20. RISCV_ACQUIRE_BARRIER
  21. : "=r" (busy), "+A" (lock->lock)
  22. : "r" (tmp)
  23. : "memory");
  24. return !busy;
  25. }
  26. void spin_lock(spinlock_t *lock)
  27. {
  28. while (1) {
  29. if (spin_lock_check(lock))
  30. continue;
  31. if (spin_trylock(lock))
  32. break;
  33. }
  34. }
  35. void spin_unlock(spinlock_t *lock)
  36. {
  37. __smp_store_release(&lock->lock, __RISCV_SPIN_UNLOCKED);
  38. }