arm_generic.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * arch/arm64/include/asm/arm_generic.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_ARM_GENERIC_H
  20. #define __ASM_ARM_GENERIC_H
  21. #include <linux/clocksource.h>
  22. #define ARCH_TIMER_CTRL_ENABLE (1 << 0)
  23. #define ARCH_TIMER_CTRL_IMASK (1 << 1)
  24. #define ARCH_TIMER_CTRL_ISTATUS (1 << 2)
  25. #define ARCH_TIMER_REG_CTRL 0
  26. #define ARCH_TIMER_REG_FREQ 1
  27. #define ARCH_TIMER_REG_TVAL 2
  28. static inline void arch_timer_reg_write(int reg, u32 val)
  29. {
  30. switch (reg) {
  31. case ARCH_TIMER_REG_CTRL:
  32. asm volatile("msr cntp_ctl_el0, %0" : : "r" (val));
  33. break;
  34. case ARCH_TIMER_REG_TVAL:
  35. asm volatile("msr cntp_tval_el0, %0" : : "r" (val));
  36. break;
  37. default:
  38. BUILD_BUG();
  39. }
  40. isb();
  41. }
  42. static inline u32 arch_timer_reg_read(int reg)
  43. {
  44. u32 val;
  45. switch (reg) {
  46. case ARCH_TIMER_REG_CTRL:
  47. asm volatile("mrs %0, cntp_ctl_el0" : "=r" (val));
  48. break;
  49. case ARCH_TIMER_REG_FREQ:
  50. asm volatile("mrs %0, cntfrq_el0" : "=r" (val));
  51. break;
  52. case ARCH_TIMER_REG_TVAL:
  53. asm volatile("mrs %0, cntp_tval_el0" : "=r" (val));
  54. break;
  55. default:
  56. BUILD_BUG();
  57. }
  58. return val;
  59. }
  60. static inline void __cpuinit arch_counter_enable_user_access(void)
  61. {
  62. u32 cntkctl;
  63. /* Disable user access to the timers and the physical counter. */
  64. asm volatile("mrs %0, cntkctl_el1" : "=r" (cntkctl));
  65. cntkctl &= ~((3 << 8) | (1 << 0));
  66. /* Enable user access to the virtual counter and frequency. */
  67. cntkctl |= (1 << 1);
  68. asm volatile("msr cntkctl_el1, %0" : : "r" (cntkctl));
  69. }
  70. static inline cycle_t arch_counter_get_cntpct(void)
  71. {
  72. cycle_t cval;
  73. asm volatile("mrs %0, cntpct_el0" : "=r" (cval));
  74. return cval;
  75. }
  76. static inline cycle_t arch_counter_get_cntvct(void)
  77. {
  78. cycle_t cval;
  79. asm volatile("mrs %0, cntvct_el0" : "=r" (cval));
  80. return cval;
  81. }
  82. #endif