riscv_locks.c 773 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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" RISCV_ACQUIRE_BARRIER
  20. : "=r"(busy), "+A"(lock->lock)
  21. : "r"(tmp)
  22. : "memory");
  23. return !busy;
  24. }
  25. void spin_lock(spinlock_t *lock)
  26. {
  27. while (1) {
  28. if (spin_lock_check(lock))
  29. continue;
  30. if (spin_trylock(lock))
  31. break;
  32. }
  33. }
  34. void spin_unlock(spinlock_t *lock)
  35. {
  36. __smp_store_release(&lock->lock, __RISCV_SPIN_UNLOCKED);
  37. }