csr.h 3.3 KB

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