csr.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_IE _AC(0x00000002, UL) /* Interrupt Enable */
  18. #define SR_PIE _AC(0x00000020, UL) /* Previous IE */
  19. #define SR_PS _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. /* SPTBR flags */
  37. #if __riscv_xlen == 32
  38. #define SPTBR_PPN _AC(0x003FFFFF, UL)
  39. #define SPTBR_MODE_32 _AC(0x80000000, UL)
  40. #define SPTBR_MODE SPTBR_MODE_32
  41. #else
  42. #define SPTBR_PPN _AC(0x00000FFFFFFFFFFF, UL)
  43. #define SPTBR_MODE_39 _AC(0x8000000000000000, UL)
  44. #define SPTBR_MODE SPTBR_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 EXC_INST_MISALIGNED 0
  50. #define EXC_INST_ACCESS 1
  51. #define EXC_BREAKPOINT 3
  52. #define EXC_LOAD_ACCESS 5
  53. #define EXC_STORE_ACCESS 7
  54. #define EXC_SYSCALL 8
  55. #define EXC_INST_PAGE_FAULT 12
  56. #define EXC_LOAD_PAGE_FAULT 13
  57. #define EXC_STORE_PAGE_FAULT 15
  58. #ifndef __ASSEMBLY__
  59. #define csr_swap(csr, val) \
  60. ({ \
  61. unsigned long __v = (unsigned long)(val); \
  62. __asm__ __volatile__ ("csrrw %0, " #csr ", %1" \
  63. : "=r" (__v) : "rK" (__v) \
  64. : "memory"); \
  65. __v; \
  66. })
  67. #define csr_read(csr) \
  68. ({ \
  69. register unsigned long __v; \
  70. __asm__ __volatile__ ("csrr %0, " #csr \
  71. : "=r" (__v) : \
  72. : "memory"); \
  73. __v; \
  74. })
  75. #define csr_write(csr, val) \
  76. ({ \
  77. unsigned long __v = (unsigned long)(val); \
  78. __asm__ __volatile__ ("csrw " #csr ", %0" \
  79. : : "rK" (__v) \
  80. : "memory"); \
  81. })
  82. #define csr_read_set(csr, val) \
  83. ({ \
  84. unsigned long __v = (unsigned long)(val); \
  85. __asm__ __volatile__ ("csrrs %0, " #csr ", %1" \
  86. : "=r" (__v) : "rK" (__v) \
  87. : "memory"); \
  88. __v; \
  89. })
  90. #define csr_set(csr, val) \
  91. ({ \
  92. unsigned long __v = (unsigned long)(val); \
  93. __asm__ __volatile__ ("csrs " #csr ", %0" \
  94. : : "rK" (__v) \
  95. : "memory"); \
  96. })
  97. #define csr_read_clear(csr, val) \
  98. ({ \
  99. unsigned long __v = (unsigned long)(val); \
  100. __asm__ __volatile__ ("csrrc %0, " #csr ", %1" \
  101. : "=r" (__v) : "rK" (__v) \
  102. : "memory"); \
  103. __v; \
  104. })
  105. #define csr_clear(csr, val) \
  106. ({ \
  107. unsigned long __v = (unsigned long)(val); \
  108. __asm__ __volatile__ ("csrc " #csr ", %0" \
  109. : : "rK" (__v) \
  110. : "memory"); \
  111. })
  112. #endif /* __ASSEMBLY__ */
  113. #endif /* _ASM_RISCV_CSR_H */