arch_timer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * arch/arm64/include/asm/arch_timer.h
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. * Author: Marc Zyngier <marc.zyngier@arm.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __ASM_ARCH_TIMER_H
  20. #define __ASM_ARCH_TIMER_H
  21. #include <asm/barrier.h>
  22. #include <linux/init.h>
  23. #include <linux/types.h>
  24. #include <clocksource/arm_arch_timer.h>
  25. static inline void arch_timer_reg_write(int access, int reg, u32 val)
  26. {
  27. if (access == ARCH_TIMER_PHYS_ACCESS) {
  28. switch (reg) {
  29. case ARCH_TIMER_REG_CTRL:
  30. asm volatile("msr cntp_ctl_el0, %0" : : "r" (val));
  31. break;
  32. case ARCH_TIMER_REG_TVAL:
  33. asm volatile("msr cntp_tval_el0, %0" : : "r" (val));
  34. break;
  35. default:
  36. BUILD_BUG();
  37. }
  38. } else if (access == ARCH_TIMER_VIRT_ACCESS) {
  39. switch (reg) {
  40. case ARCH_TIMER_REG_CTRL:
  41. asm volatile("msr cntv_ctl_el0, %0" : : "r" (val));
  42. break;
  43. case ARCH_TIMER_REG_TVAL:
  44. asm volatile("msr cntv_tval_el0, %0" : : "r" (val));
  45. break;
  46. default:
  47. BUILD_BUG();
  48. }
  49. } else {
  50. BUILD_BUG();
  51. }
  52. isb();
  53. }
  54. static inline u32 arch_timer_reg_read(int access, int reg)
  55. {
  56. u32 val;
  57. if (access == ARCH_TIMER_PHYS_ACCESS) {
  58. switch (reg) {
  59. case ARCH_TIMER_REG_CTRL:
  60. asm volatile("mrs %0, cntp_ctl_el0" : "=r" (val));
  61. break;
  62. case ARCH_TIMER_REG_TVAL:
  63. asm volatile("mrs %0, cntp_tval_el0" : "=r" (val));
  64. break;
  65. default:
  66. BUILD_BUG();
  67. }
  68. } else if (access == ARCH_TIMER_VIRT_ACCESS) {
  69. switch (reg) {
  70. case ARCH_TIMER_REG_CTRL:
  71. asm volatile("mrs %0, cntv_ctl_el0" : "=r" (val));
  72. break;
  73. case ARCH_TIMER_REG_TVAL:
  74. asm volatile("mrs %0, cntv_tval_el0" : "=r" (val));
  75. break;
  76. default:
  77. BUILD_BUG();
  78. }
  79. } else {
  80. BUILD_BUG();
  81. }
  82. return val;
  83. }
  84. static inline u32 arch_timer_get_cntfrq(void)
  85. {
  86. u32 val;
  87. asm volatile("mrs %0, cntfrq_el0" : "=r" (val));
  88. return val;
  89. }
  90. static inline void __cpuinit arch_counter_set_user_access(void)
  91. {
  92. u32 cntkctl;
  93. /* Disable user access to the timers and the physical counter. */
  94. asm volatile("mrs %0, cntkctl_el1" : "=r" (cntkctl));
  95. cntkctl &= ~((3 << 8) | (1 << 0));
  96. /* Enable user access to the virtual counter and frequency. */
  97. cntkctl |= (1 << 1);
  98. asm volatile("msr cntkctl_el1, %0" : : "r" (cntkctl));
  99. }
  100. static inline u64 arch_counter_get_cntpct(void)
  101. {
  102. u64 cval;
  103. isb();
  104. asm volatile("mrs %0, cntpct_el0" : "=r" (cval));
  105. return cval;
  106. }
  107. static inline u64 arch_counter_get_cntvct(void)
  108. {
  109. u64 cval;
  110. isb();
  111. asm volatile("mrs %0, cntvct_el0" : "=r" (cval));
  112. return cval;
  113. }
  114. #endif