csr.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2015 Regents of the University of California
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #ifndef _ASM_RISCV_CSR_H
  14. #define _ASM_RISCV_CSR_H
  15. #include <linux/const.h>
  16. /* Status register flags */
  17. #define SR_SIE _AC(0x00000002, UL) /* Supervisor Interrupt Enable */
  18. #define SR_SPIE _AC(0x00000020, UL) /* Previous Supervisor IE */
  19. #define SR_SPP _AC(0x00000100, UL) /* Previously Supervisor */
  20. #define SR_SUM _AC(0x00040000, UL) /* Supervisor may access User Memory */
  21. #define SR_FS _AC(0x00006000, UL) /* Floating-point Status */
  22. #define SR_FS_OFF _AC(0x00000000, UL)
  23. #define SR_FS_INITIAL _AC(0x00002000, UL)
  24. #define SR_FS_CLEAN _AC(0x00004000, UL)
  25. #define SR_FS_DIRTY _AC(0x00006000, UL)
  26. #define SR_XS _AC(0x00018000, UL) /* Extension Status */
  27. #define SR_XS_OFF _AC(0x00000000, UL)
  28. #define SR_XS_INITIAL _AC(0x00008000, UL)
  29. #define SR_XS_CLEAN _AC(0x00010000, UL)
  30. #define SR_XS_DIRTY _AC(0x00018000, UL)
  31. #ifndef CONFIG_64BIT
  32. #define SR_SD _AC(0x80000000, UL) /* FS/XS dirty */
  33. #else
  34. #define SR_SD _AC(0x8000000000000000, UL) /* FS/XS dirty */
  35. #endif
  36. /* SATP flags */
  37. #if __riscv_xlen == 32
  38. #define SATP_PPN _AC(0x003FFFFF, UL)
  39. #define SATP_MODE_32 _AC(0x80000000, UL)
  40. #define SATP_MODE SATP_MODE_32
  41. #else
  42. #define SATP_PPN _AC(0x00000FFFFFFFFFFF, UL)
  43. #define SATP_MODE_39 _AC(0x8000000000000000, UL)
  44. #define SATP_MODE SATP_MODE_39
  45. #endif
  46. /* Interrupt Enable and Interrupt Pending flags */
  47. #define SIE_SSIE _AC(0x00000002, UL) /* Software Interrupt Enable */
  48. #define SIE_STIE _AC(0x00000020, UL) /* Timer Interrupt Enable */
  49. #define SIE_SEIE _AC(0x00000200, UL) /* External Interrupt Enable */
  50. #define EXC_INST_MISALIGNED 0
  51. #define EXC_INST_ACCESS 1
  52. #define EXC_BREAKPOINT 3
  53. #define EXC_LOAD_ACCESS 5
  54. #define EXC_STORE_ACCESS 7
  55. #define EXC_SYSCALL 8
  56. #define EXC_INST_PAGE_FAULT 12
  57. #define EXC_LOAD_PAGE_FAULT 13
  58. #define EXC_STORE_PAGE_FAULT 15
  59. #ifndef __ASSEMBLY__
  60. #define csr_swap(csr, val) \
  61. ({ \
  62. unsigned long __v = (unsigned long)(val); \
  63. __asm__ __volatile__ ("csrrw %0, " #csr ", %1" \
  64. : "=r" (__v) : "rK" (__v) \
  65. : "memory"); \
  66. __v; \
  67. })
  68. #define csr_read(csr) \
  69. ({ \
  70. register unsigned long __v; \
  71. __asm__ __volatile__ ("csrr %0, " #csr \
  72. : "=r" (__v) : \
  73. : "memory"); \
  74. __v; \
  75. })
  76. #define csr_write(csr, val) \
  77. ({ \
  78. unsigned long __v = (unsigned long)(val); \
  79. __asm__ __volatile__ ("csrw " #csr ", %0" \
  80. : : "rK" (__v) \
  81. : "memory"); \
  82. })
  83. #define csr_read_set(csr, val) \
  84. ({ \
  85. unsigned long __v = (unsigned long)(val); \
  86. __asm__ __volatile__ ("csrrs %0, " #csr ", %1" \
  87. : "=r" (__v) : "rK" (__v) \
  88. : "memory"); \
  89. __v; \
  90. })
  91. #define csr_set(csr, val) \
  92. ({ \
  93. unsigned long __v = (unsigned long)(val); \
  94. __asm__ __volatile__ ("csrs " #csr ", %0" \
  95. : : "rK" (__v) \
  96. : "memory"); \
  97. })
  98. #define csr_read_clear(csr, val) \
  99. ({ \
  100. unsigned long __v = (unsigned long)(val); \
  101. __asm__ __volatile__ ("csrrc %0, " #csr ", %1" \
  102. : "=r" (__v) : "rK" (__v) \
  103. : "memory"); \
  104. __v; \
  105. })
  106. #define csr_clear(csr, val) \
  107. ({ \
  108. unsigned long __v = (unsigned long)(val); \
  109. __asm__ __volatile__ ("csrc " #csr ", %0" \
  110. : : "rK" (__v) \
  111. : "memory"); \
  112. })
  113. #endif /* __ASSEMBLY__ */
  114. #endif /* _ASM_RISCV_CSR_H */