arch_timer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 void arch_counter_set_user_access(void)
  86. {
  87. u32 cntkctl;
  88. /* Disable user access to the timers and the physical counter. */
  89. asm volatile("mrs %0, cntkctl_el1" : "=r" (cntkctl));
  90. cntkctl &= ~((3 << 8) | (1 << 0));
  91. /* Enable user access to the virtual counter and frequency. */
  92. cntkctl |= (1 << 1);
  93. asm volatile("msr cntkctl_el1, %0" : : "r" (cntkctl));
  94. }
  95. static inline u64 arch_counter_get_cntvct(void)
  96. {
  97. u64 cval;
  98. isb();
  99. asm volatile("mrs %0, cntvct_el0" : "=r" (cval));
  100. return cval;
  101. }
  102. static inline int arch_timer_arch_init(void)
  103. {
  104. return 0;
  105. }
  106. #endif