barrier.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Based on arch/arm/include/asm/barrier.h
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. * Copyright (C) 2013 Regents of the University of California
  6. * Copyright (C) 2017 SiFive
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef _ASM_RISCV_BARRIER_H
  21. #define _ASM_RISCV_BARRIER_H
  22. #ifndef __ASSEMBLY__
  23. #define nop() __asm__ __volatile__ ("nop")
  24. #define RISCV_FENCE(p, s) \
  25. __asm__ __volatile__ ("fence " #p "," #s : : : "memory")
  26. /* These barriers need to enforce ordering on both devices or memory. */
  27. #define mb() RISCV_FENCE(iorw,iorw)
  28. #define rmb() RISCV_FENCE(ir,ir)
  29. #define wmb() RISCV_FENCE(ow,ow)
  30. /* These barriers do not need to enforce ordering on devices, just memory. */
  31. #define __smp_mb() RISCV_FENCE(rw,rw)
  32. #define __smp_rmb() RISCV_FENCE(r,r)
  33. #define __smp_wmb() RISCV_FENCE(w,w)
  34. #define __smp_store_release(p, v) \
  35. do { \
  36. compiletime_assert_atomic_type(*p); \
  37. RISCV_FENCE(rw,w); \
  38. WRITE_ONCE(*p, v); \
  39. } while (0)
  40. #define __smp_load_acquire(p) \
  41. ({ \
  42. typeof(*p) ___p1 = READ_ONCE(*p); \
  43. compiletime_assert_atomic_type(*p); \
  44. RISCV_FENCE(r,rw); \
  45. ___p1; \
  46. })
  47. /*
  48. * This is a very specific barrier: it's currently only used in two places in
  49. * the kernel, both in the scheduler. See include/linux/spinlock.h for the two
  50. * orderings it guarantees, but the "critical section is RCsc" guarantee
  51. * mandates a barrier on RISC-V. The sequence looks like:
  52. *
  53. * lr.aq lock
  54. * sc lock <= LOCKED
  55. * smp_mb__after_spinlock()
  56. * // critical section
  57. * lr lock
  58. * sc.rl lock <= UNLOCKED
  59. *
  60. * The AQ/RL pair provides a RCpc critical section, but there's not really any
  61. * way we can take advantage of that here because the ordering is only enforced
  62. * on that one lock. Thus, we're just doing a full fence.
  63. */
  64. #define smp_mb__after_spinlock() RISCV_FENCE(rw,rw)
  65. #include <asm-generic/barrier.h>
  66. #endif /* __ASSEMBLY__ */
  67. #endif /* _ASM_RISCV_BARRIER_H */