arch_timer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /*
  26. * These register accessors are marked inline so the compiler can
  27. * nicely work out which register we want, and chuck away the rest of
  28. * the code.
  29. */
  30. static __always_inline
  31. void arch_timer_reg_write_cp15(int access, enum arch_timer_reg reg, u32 val)
  32. {
  33. if (access == ARCH_TIMER_PHYS_ACCESS) {
  34. switch (reg) {
  35. case ARCH_TIMER_REG_CTRL:
  36. asm volatile("msr cntp_ctl_el0, %0" : : "r" (val));
  37. break;
  38. case ARCH_TIMER_REG_TVAL:
  39. asm volatile("msr cntp_tval_el0, %0" : : "r" (val));
  40. break;
  41. }
  42. } else if (access == ARCH_TIMER_VIRT_ACCESS) {
  43. switch (reg) {
  44. case ARCH_TIMER_REG_CTRL:
  45. asm volatile("msr cntv_ctl_el0, %0" : : "r" (val));
  46. break;
  47. case ARCH_TIMER_REG_TVAL:
  48. asm volatile("msr cntv_tval_el0, %0" : : "r" (val));
  49. break;
  50. }
  51. }
  52. isb();
  53. }
  54. static __always_inline
  55. u32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg)
  56. {
  57. u32 val;
  58. if (access == ARCH_TIMER_PHYS_ACCESS) {
  59. switch (reg) {
  60. case ARCH_TIMER_REG_CTRL:
  61. asm volatile("mrs %0, cntp_ctl_el0" : "=r" (val));
  62. break;
  63. case ARCH_TIMER_REG_TVAL:
  64. asm volatile("mrs %0, cntp_tval_el0" : "=r" (val));
  65. break;
  66. }
  67. } else if (access == ARCH_TIMER_VIRT_ACCESS) {
  68. switch (reg) {
  69. case ARCH_TIMER_REG_CTRL:
  70. asm volatile("mrs %0, cntv_ctl_el0" : "=r" (val));
  71. break;
  72. case ARCH_TIMER_REG_TVAL:
  73. asm volatile("mrs %0, cntv_tval_el0" : "=r" (val));
  74. break;
  75. }
  76. }
  77. return val;
  78. }
  79. static inline u32 arch_timer_get_cntfrq(void)
  80. {
  81. u32 val;
  82. asm volatile("mrs %0, cntfrq_el0" : "=r" (val));
  83. return val;
  84. }
  85. static inline u32 arch_timer_get_cntkctl(void)
  86. {
  87. u32 cntkctl;
  88. asm volatile("mrs %0, cntkctl_el1" : "=r" (cntkctl));
  89. return cntkctl;
  90. }
  91. static inline void arch_timer_set_cntkctl(u32 cntkctl)
  92. {
  93. asm volatile("msr cntkctl_el1, %0" : : "r" (cntkctl));
  94. }
  95. static inline void arch_counter_set_user_access(void)
  96. {
  97. u32 cntkctl = arch_timer_get_cntkctl();
  98. /* Disable user access to the timers and the physical counter */
  99. /* Also disable virtual event stream */
  100. cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
  101. | ARCH_TIMER_USR_VT_ACCESS_EN
  102. | ARCH_TIMER_VIRT_EVT_EN
  103. | ARCH_TIMER_USR_PCT_ACCESS_EN);
  104. /* Enable user access to the virtual counter */
  105. cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
  106. arch_timer_set_cntkctl(cntkctl);
  107. }
  108. static inline void arch_timer_evtstrm_enable(int divider)
  109. {
  110. u32 cntkctl = arch_timer_get_cntkctl();
  111. cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
  112. /* Set the divider and enable virtual event stream */
  113. cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
  114. | ARCH_TIMER_VIRT_EVT_EN;
  115. arch_timer_set_cntkctl(cntkctl);
  116. elf_hwcap |= HWCAP_EVTSTRM;
  117. #ifdef CONFIG_COMPAT
  118. compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
  119. #endif
  120. }
  121. static inline u64 arch_counter_get_cntvct(void)
  122. {
  123. u64 cval;
  124. isb();
  125. asm volatile("mrs %0, cntvct_el0" : "=r" (cval));
  126. return cval;
  127. }
  128. static inline int arch_timer_arch_init(void)
  129. {
  130. return 0;
  131. }
  132. #endif