1 /* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright(c) 2018 Intel Corporation. All rights reserved.
4 *
5 * Author: Janusz Jankowski <janusz.jankowski@linux.intel.com>
6 */
7
8 #ifndef __SOF_DRIVERS_TIMER_H__
9 #define __SOF_DRIVERS_TIMER_H__
10
11 #include <arch/drivers/timer.h>
12 #include <rtos/clk.h>
13 #include <sof/lib/cpu.h>
14 #include <rtos/sof.h>
15 #include <sof/platform.h>
16 #include <stdint.h>
17
18 struct comp_dev;
19 struct sof_ipc_stream_posn;
20
21 #define TIMER0 0
22 #define TIMER1 1
23 #define TIMER2 2
24 #define TIMER3 3
25 #define TIMER4 4
26
27 int timer_register(struct timer *timer, void (*handler)(void *arg), void *arg);
28 void timer_unregister(struct timer *timer, void *arg);
29 void timer_enable(struct timer *timer, void *arg, int core);
30 void timer_disable(struct timer *timer, void *arg, int core);
31
timer_get(void)32 static inline struct timer *timer_get(void)
33 {
34 return sof_get()->platform_timer;
35 }
36
cpu_timer_get(void)37 static inline struct timer *cpu_timer_get(void)
38 {
39 return &(sof_get()->cpu_timers[cpu_get_id()]);
40 }
41
timer_set(struct timer * timer,uint64_t ticks)42 static inline int64_t timer_set(struct timer *timer, uint64_t ticks)
43 {
44 return arch_timer_set(timer, ticks);
45 }
46
47 void timer_set_ms(struct timer *timer, unsigned int ms);
48
timer_clear(struct timer * timer)49 static inline void timer_clear(struct timer *timer)
50 {
51 arch_timer_clear(timer);
52 }
53
54 unsigned int timer_get_count(struct timer *timer);
55
56 unsigned int timer_get_count_delta(struct timer *timer);
57
timer_get_system(struct timer * timer)58 static inline uint64_t timer_get_system(struct timer *timer)
59 {
60 return arch_timer_get_system(timer);
61 }
62
63 int64_t platform_timer_set(struct timer *timer, uint64_t ticks);
64 void platform_timer_clear(struct timer *timer);
65 uint64_t platform_timer_get(struct timer *timer);
66 uint64_t platform_timer_get_atomic(struct timer *timer);
67
platform_safe_get_time(struct timer * timer)68 static inline uint64_t platform_safe_get_time(struct timer *timer)
69 {
70 /* Default to something small but at least 1.0 microsecond so it
71 * does not look like an uninitialized zero; not even when the
72 * user does not request any microseconds decimals. See
73 * DEFAULT_CLOCK constant in logger.c
74 */
75 return timer ? platform_timer_get(timer) : 50;
76 }
77
78 void platform_timer_start(struct timer *timer);
79 void platform_timer_stop(struct timer *timer);
80
k_ms_to_cyc_ceil64(uint64_t ms)81 static inline uint64_t k_ms_to_cyc_ceil64(uint64_t ms)
82 {
83 return clock_ms_to_ticks(PLATFORM_DEFAULT_CLOCK, ms);
84 }
85
k_us_to_cyc_ceil64(uint64_t us)86 static inline uint64_t k_us_to_cyc_ceil64(uint64_t us)
87 {
88 return clock_us_to_ticks(PLATFORM_DEFAULT_CLOCK, us);
89 }
90
k_ns_to_cyc_near64(uint64_t ns)91 static inline uint64_t k_ns_to_cyc_near64(uint64_t ns)
92 {
93 return clock_ns_to_ticks(PLATFORM_DEFAULT_CLOCK, ns);
94 }
95
k_cyc_to_ms_near64(uint64_t ticks)96 static inline uint64_t k_cyc_to_ms_near64(uint64_t ticks)
97 {
98 return ticks / clock_ms_to_ticks(PLATFORM_DEFAULT_CLOCK, 1);
99 }
100
k_cyc_to_us_near64(uint64_t ticks)101 static inline uint64_t k_cyc_to_us_near64(uint64_t ticks)
102 {
103 return ticks / clock_us_to_ticks(PLATFORM_DEFAULT_CLOCK, 1);
104 }
105
sof_cycle_get_64(void)106 static inline uint64_t sof_cycle_get_64(void)
107 {
108 return platform_timer_get(timer_get());
109 }
110
sof_cycle_get_64_atomic(void)111 static inline uint64_t sof_cycle_get_64_atomic(void)
112 {
113 return platform_timer_get_atomic(timer_get());
114 }
115
sof_cycle_get_64_safe(void)116 static inline uint64_t sof_cycle_get_64_safe(void)
117 {
118 return platform_safe_get_time(timer_get());
119 }
120
121 /* get timestamp for host stream DMA position */
122 void platform_host_timestamp(struct comp_dev *host,
123 struct sof_ipc_stream_posn *posn);
124
125 /* get timestamp for DAI stream DMA position */
126 void platform_dai_timestamp(struct comp_dev *dai,
127 struct sof_ipc_stream_posn *posn);
128
129 /* get current wallclock for componnent */
130 void platform_dai_wallclock(struct comp_dev *dai, uint64_t *wallclock);
131
132 #endif /* __SOF_DRIVERS_TIMER_H__ */
133