csr.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2015 Regents of the University of California
  4. *
  5. * Taken from Linux arch/riscv/include/asm/csr.h
  6. */
  7. #ifndef _ASM_RISCV_CSR_H
  8. #define _ASM_RISCV_CSR_H
  9. #include <linux/const.h>
  10. /* Status register flags */
  11. #define SR_SIE _AC(0x00000002, UL) /* Supervisor Interrupt Enable */
  12. #define SR_SPIE _AC(0x00000020, UL) /* Previous Supervisor IE */
  13. #define SR_SPP _AC(0x00000100, UL) /* Previously Supervisor */
  14. #define SR_SUM _AC(0x00040000, UL) /* Supervisor access User Memory */
  15. #define SR_FS _AC(0x00006000, UL) /* Floating-point Status */
  16. #define SR_FS_OFF _AC(0x00000000, UL)
  17. #define SR_FS_INITIAL _AC(0x00002000, UL)
  18. #define SR_FS_CLEAN _AC(0x00004000, UL)
  19. #define SR_FS_DIRTY _AC(0x00006000, UL)
  20. #define SR_XS _AC(0x00018000, UL) /* Extension Status */
  21. #define SR_XS_OFF _AC(0x00000000, UL)
  22. #define SR_XS_INITIAL _AC(0x00008000, UL)
  23. #define SR_XS_CLEAN _AC(0x00010000, UL)
  24. #define SR_XS_DIRTY _AC(0x00018000, UL)
  25. #ifndef CONFIG_64BIT
  26. #define SR_SD _AC(0x80000000, UL) /* FS/XS dirty */
  27. #else
  28. #define SR_SD _AC(0x8000000000000000, UL) /* FS/XS dirty */
  29. #endif
  30. /* SATP flags */
  31. #if __riscv_xlen == 32
  32. #define SATP_PPN _AC(0x003FFFFF, UL)
  33. #define SATP_MODE_32 _AC(0x80000000, UL)
  34. #define SATP_MODE SATP_MODE_32
  35. #else
  36. #define SATP_PPN _AC(0x00000FFFFFFFFFFF, UL)
  37. #define SATP_MODE_39 _AC(0x8000000000000000, UL)
  38. #define SATP_MODE SATP_MODE_39
  39. #endif
  40. /* Interrupt Enable and Interrupt Pending flags */
  41. #define SIE_SSIE _AC(0x00000002, UL) /* Software Interrupt Enable */
  42. #define SIE_STIE _AC(0x00000020, UL) /* Timer Interrupt Enable */
  43. #define EXC_INST_MISALIGNED 0
  44. #define EXC_INST_ACCESS 1
  45. #define EXC_BREAKPOINT 3
  46. #define EXC_LOAD_ACCESS 5
  47. #define EXC_STORE_ACCESS 7
  48. #define EXC_SYSCALL 8
  49. #define EXC_INST_PAGE_FAULT 12
  50. #define EXC_LOAD_PAGE_FAULT 13
  51. #define EXC_STORE_PAGE_FAULT 15
  52. #ifndef __ASSEMBLY__
  53. #define csr_swap(csr, val) \
  54. ({ \
  55. unsigned long __v = (unsigned long)(val); \
  56. __asm__ __volatile__ ("csrrw %0, " #csr ", %1" \
  57. : "=r" (__v) : "rK" (__v) \
  58. : "memory"); \
  59. __v; \
  60. })
  61. #define csr_read(csr) \
  62. ({ \
  63. register unsigned long __v; \
  64. __asm__ __volatile__ ("csrr %0, " #csr \
  65. : "=r" (__v) : \
  66. : "memory"); \
  67. __v; \
  68. })
  69. #define csr_write(csr, val) \
  70. ({ \
  71. unsigned long __v = (unsigned long)(val); \
  72. __asm__ __volatile__ ("csrw " #csr ", %0" \
  73. : : "rK" (__v) \
  74. : "memory"); \
  75. })
  76. #define csr_read_set(csr, val) \
  77. ({ \
  78. unsigned long __v = (unsigned long)(val); \
  79. __asm__ __volatile__ ("csrrs %0, " #csr ", %1" \
  80. : "=r" (__v) : "rK" (__v) \
  81. : "memory"); \
  82. __v; \
  83. })
  84. #define csr_set(csr, val) \
  85. ({ \
  86. unsigned long __v = (unsigned long)(val); \
  87. __asm__ __volatile__ ("csrs " #csr ", %0" \
  88. : : "rK" (__v) \
  89. : "memory"); \
  90. })
  91. #define csr_read_clear(csr, val) \
  92. ({ \
  93. unsigned long __v = (unsigned long)(val); \
  94. __asm__ __volatile__ ("csrrc %0, " #csr ", %1" \
  95. : "=r" (__v) : "rK" (__v) \
  96. : "memory"); \
  97. __v; \
  98. })
  99. #define csr_clear(csr, val) \
  100. ({ \
  101. unsigned long __v = (unsigned long)(val); \
  102. __asm__ __volatile__ ("csrc " #csr ", %0" \
  103. : : "rK" (__v) \
  104. : "memory"); \
  105. })
  106. #endif /* __ASSEMBLY__ */
  107. #endif /* _ASM_RISCV_CSR_H */