sbi_timer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #ifndef __SBI_TIMER_H__
  10. #define __SBI_TIMER_H__
  11. #include <sbi/sbi_types.h>
  12. /** Timer hardware device */
  13. struct sbi_timer_device {
  14. /** Name of the timer operations */
  15. char name[32];
  16. /** Frequency of timer in HZ */
  17. unsigned long timer_freq;
  18. /** Get free-running timer value */
  19. u64 (*timer_value)(void);
  20. /** Start timer event for current HART */
  21. void (*timer_event_start)(u64 next_event);
  22. /** Stop timer event for current HART */
  23. void (*timer_event_stop)(void);
  24. };
  25. struct sbi_scratch;
  26. /** Generic delay loop of desired granularity */
  27. void sbi_timer_delay_loop(ulong units, u64 unit_freq,
  28. void (*delay_fn)(void *), void *opaque);
  29. /** Provide delay in terms of milliseconds */
  30. static inline void sbi_timer_mdelay(ulong msecs)
  31. {
  32. sbi_timer_delay_loop(msecs, 1000, NULL, NULL);
  33. }
  34. /** Provide delay in terms of microseconds */
  35. static inline void sbi_timer_udelay(ulong usecs)
  36. {
  37. sbi_timer_delay_loop(usecs, 1000000, NULL, NULL);
  38. }
  39. /**
  40. * A blocking function that will wait until @p predicate returns true or
  41. * @p timeout_ms milliseconds elapsed. @p arg will be passed as argument to
  42. * @p predicate function.
  43. *
  44. * @param predicate Pointer to a function that returns true if certain
  45. * condition is met. It shouldn't block the code execution.
  46. * @param arg Argument to pass to @p predicate.
  47. * @param timeout_ms Timeout value in milliseconds. The function will return
  48. * false if @p timeout_ms time period elapsed but still @p predicate doesn't
  49. * return true.
  50. *
  51. * @return true if @p predicate returns true within @p timeout_ms, false
  52. * otherwise.
  53. */
  54. bool sbi_timer_waitms_until(bool (*predicate)(void *), void *arg,
  55. uint64_t timeout_ms);
  56. /** Get timer value for current HART */
  57. u64 sbi_timer_value(void);
  58. /** Get virtualized timer value for current HART */
  59. u64 sbi_timer_virt_value(void);
  60. /** Get timer delta value for current HART */
  61. u64 sbi_timer_get_delta(void);
  62. /** Set timer delta value for current HART */
  63. void sbi_timer_set_delta(ulong delta);
  64. /** Set upper 32-bits of timer delta value for current HART */
  65. void sbi_timer_set_delta_upper(ulong delta_upper);
  66. /** Start timer event for current HART */
  67. void sbi_timer_event_start(u64 next_event);
  68. /** Process timer event for current HART */
  69. void sbi_timer_process(void);
  70. /** Get current timer device */
  71. const struct sbi_timer_device *sbi_timer_get_device(void);
  72. /** Register timer device */
  73. void sbi_timer_set_device(const struct sbi_timer_device *dev);
  74. /* Initialize timer */
  75. int sbi_timer_init(struct sbi_scratch *scratch, bool cold_boot);
  76. /* Exit timer */
  77. void sbi_timer_exit(struct sbi_scratch *scratch);
  78. #endif