timex.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. */
  5. #ifndef _ASM_RISCV_TIMEX_H
  6. #define _ASM_RISCV_TIMEX_H
  7. #include <asm/csr.h>
  8. typedef unsigned long cycles_t;
  9. #ifdef CONFIG_RISCV_M_MODE
  10. #include <asm/clint.h>
  11. #ifdef CONFIG_64BIT
  12. static inline cycles_t get_cycles(void)
  13. {
  14. return readq_relaxed(clint_time_val);
  15. }
  16. #else /* !CONFIG_64BIT */
  17. static inline u32 get_cycles(void)
  18. {
  19. return readl_relaxed(((u32 *)clint_time_val));
  20. }
  21. #define get_cycles get_cycles
  22. static inline u32 get_cycles_hi(void)
  23. {
  24. return readl_relaxed(((u32 *)clint_time_val) + 1);
  25. }
  26. #define get_cycles_hi get_cycles_hi
  27. #endif /* CONFIG_64BIT */
  28. /*
  29. * Much like MIPS, we may not have a viable counter to use at an early point
  30. * in the boot process. Unfortunately we don't have a fallback, so instead
  31. * we just return 0.
  32. */
  33. static inline unsigned long random_get_entropy(void)
  34. {
  35. if (unlikely(clint_time_val == NULL))
  36. return random_get_entropy_fallback();
  37. return get_cycles();
  38. }
  39. #define random_get_entropy() random_get_entropy()
  40. #else /* CONFIG_RISCV_M_MODE */
  41. static inline cycles_t get_cycles(void)
  42. {
  43. return csr_read(CSR_TIME);
  44. }
  45. #define get_cycles get_cycles
  46. static inline u32 get_cycles_hi(void)
  47. {
  48. return csr_read(CSR_TIMEH);
  49. }
  50. #define get_cycles_hi get_cycles_hi
  51. #endif /* !CONFIG_RISCV_M_MODE */
  52. #ifdef CONFIG_64BIT
  53. static inline u64 get_cycles64(void)
  54. {
  55. return get_cycles();
  56. }
  57. #else /* CONFIG_64BIT */
  58. static inline u64 get_cycles64(void)
  59. {
  60. u32 hi, lo;
  61. do {
  62. hi = get_cycles_hi();
  63. lo = get_cycles();
  64. } while (hi != get_cycles_hi());
  65. return ((u64)hi << 32) | lo;
  66. }
  67. #endif /* CONFIG_64BIT */
  68. #define ARCH_HAS_READ_CURRENT_TIMER
  69. static inline int read_current_timer(unsigned long *timer_val)
  70. {
  71. *timer_val = get_cycles();
  72. return 0;
  73. }
  74. extern void time_init(void);
  75. #endif /* _ASM_RISCV_TIMEX_H */