barrier.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Based on arch/arm/include/asm/barrier.h
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. * Copyright (C) 2013 Regents of the University of California
  7. * Copyright (C) 2017 SiFive
  8. */
  9. #ifndef _ASM_RISCV_BARRIER_H
  10. #define _ASM_RISCV_BARRIER_H
  11. #ifndef __ASSEMBLY__
  12. #include <asm/cmpxchg.h>
  13. #include <asm/fence.h>
  14. #define nop() __asm__ __volatile__ ("nop")
  15. #define __nops(n) ".rept " #n "\nnop\n.endr\n"
  16. #define nops(n) __asm__ __volatile__ (__nops(n))
  17. /* These barriers need to enforce ordering on both devices or memory. */
  18. #define __mb() RISCV_FENCE(iorw, iorw)
  19. #define __rmb() RISCV_FENCE(ir, ir)
  20. #define __wmb() RISCV_FENCE(ow, ow)
  21. /* These barriers do not need to enforce ordering on devices, just memory. */
  22. #define __smp_mb() RISCV_FENCE(rw, rw)
  23. #define __smp_rmb() RISCV_FENCE(r, r)
  24. #define __smp_wmb() RISCV_FENCE(w, w)
  25. /*
  26. * This is a very specific barrier: it's currently only used in two places in
  27. * the kernel, both in the scheduler. See include/linux/spinlock.h for the two
  28. * orderings it guarantees, but the "critical section is RCsc" guarantee
  29. * mandates a barrier on RISC-V. The sequence looks like:
  30. *
  31. * lr.aq lock
  32. * sc lock <= LOCKED
  33. * smp_mb__after_spinlock()
  34. * // critical section
  35. * lr lock
  36. * sc.rl lock <= UNLOCKED
  37. *
  38. * The AQ/RL pair provides a RCpc critical section, but there's not really any
  39. * way we can take advantage of that here because the ordering is only enforced
  40. * on that one lock. Thus, we're just doing a full fence.
  41. *
  42. * Since we allow writeX to be called from preemptive regions we need at least
  43. * an "o" in the predecessor set to ensure device writes are visible before the
  44. * task is marked as available for scheduling on a new hart. While I don't see
  45. * any concrete reason we need a full IO fence, it seems safer to just upgrade
  46. * this in order to avoid any IO crossing a scheduling boundary. In both
  47. * instances the scheduler pairs this with an mb(), so nothing is necessary on
  48. * the new hart.
  49. */
  50. #define smp_mb__after_spinlock() RISCV_FENCE(iorw, iorw)
  51. #define __smp_store_release(p, v) \
  52. do { \
  53. compiletime_assert_atomic_type(*p); \
  54. RISCV_FENCE(rw, w); \
  55. WRITE_ONCE(*p, v); \
  56. } while (0)
  57. #define __smp_load_acquire(p) \
  58. ({ \
  59. typeof(*p) ___p1 = READ_ONCE(*p); \
  60. compiletime_assert_atomic_type(*p); \
  61. RISCV_FENCE(r, rw); \
  62. ___p1; \
  63. })
  64. #ifdef CONFIG_RISCV_ISA_ZAWRS
  65. #define smp_cond_load_relaxed(ptr, cond_expr) ({ \
  66. typeof(ptr) __PTR = (ptr); \
  67. __unqual_scalar_typeof(*ptr) VAL; \
  68. for (;;) { \
  69. VAL = READ_ONCE(*__PTR); \
  70. if (cond_expr) \
  71. break; \
  72. __cmpwait_relaxed(ptr, VAL); \
  73. } \
  74. (typeof(*ptr))VAL; \
  75. })
  76. #endif
  77. #include <asm-generic/barrier.h>
  78. #endif /* __ASSEMBLY__ */
  79. #endif /* _ASM_RISCV_BARRIER_H */