forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait.h
More file actions
215 lines (179 loc) · 5.4 KB
/
wait.h
File metadata and controls
215 lines (179 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2016, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Intel Corporation nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
*
* Simple wait for event completion and signaling with timeouts.
*/
#ifndef __INCLUDE_WAIT__
#define __INCLUDE_WAIT__
#include <stdint.h>
#include <errno.h>
#include <arch/wait.h>
#include <sof/io.h>
#include <sof/debug.h>
#include <sof/work.h>
#include <sof/timer.h>
#include <sof/interrupt.h>
#include <sof/trace.h>
#include <sof/lock.h>
#include <sof/clk.h>
#include <platform/clk.h>
#include <platform/interrupt.h>
#include <sof/drivers/timer.h>
#include <platform/platform.h>
#if DEBUG_LOCKS
#define wait_atomic_check \
if (lock_dbg_atomic) { \
trace_error_atomic(TRACE_CLASS_WAIT, "atm"); \
}
#else
#define wait_atomic_check
#endif
#define DEFAULT_TRY_TIMES 8
typedef struct {
uint32_t complete;
struct work work;
uint64_t timeout;
} completion_t;
void arch_wait_for_interrupt(int level);
static inline void wait_for_interrupt(int level)
{
tracev_event(TRACE_CLASS_WAIT, "WFE");
wait_atomic_check;
arch_wait_for_interrupt(level);
tracev_event(TRACE_CLASS_WAIT, "WFX");
}
static uint64_t _wait_cb(void *data, uint64_t delay)
{
volatile completion_t *wc = (volatile completion_t*)data;
wc->timeout = 1;
return 0;
}
static inline uint32_t wait_is_completed(completion_t *comp)
{
volatile completion_t *c = (volatile completion_t *)comp;
return c->complete;
}
static inline void wait_completed(completion_t *comp)
{
volatile completion_t *c = (volatile completion_t *)comp;
c->complete = 1;
}
static inline void wait_init(completion_t *comp)
{
volatile completion_t *c = (volatile completion_t *)comp;
c->complete = 0;
work_init(&comp->work, _wait_cb, comp, WORK_ASYNC);
}
static inline void wait_clear(completion_t *comp)
{
volatile completion_t *c = (volatile completion_t *)comp;
c->complete = 0;
}
/* simple interrupt based wait for completion */
static inline void wait_for_completion(completion_t *comp)
{
/* check for completion after every wake from IRQ */
while (comp->complete == 0)
wait_for_interrupt(0);
}
/* simple interrupt based wait for completion with timeout */
static inline int wait_for_completion_timeout(completion_t *comp)
{
volatile completion_t *c = (volatile completion_t *)comp;
work_schedule_default(&comp->work, comp->timeout);
comp->timeout = 0;
/* check for completion after every wake from IRQ */
while (1) {
if (c->complete || c->timeout)
break;
wait_for_interrupt(0);
}
/* did we complete */
if (c->complete) {
/* no timeout so cancel work and return 0 */
work_cancel_default(&comp->work);
return 0;
} else {
/* timeout */
trace_error_value(c->timeout);
trace_error_value(c->complete);
return -ETIME;
}
}
/**
* \brief Waits at least passed number of clocks.
* \param[in] number_of_clks Minimum number of clocks to wait.
*/
static inline void wait_delay(uint64_t number_of_clks)
{
uint64_t current = platform_timer_get(platform_timer);
while ((platform_timer_get(platform_timer) - current) < number_of_clks)
idelay(PLATFORM_DEFAULT_DELAY);
}
static inline int poll_for_completion_delay(completion_t *comp, uint64_t us)
{
uint64_t tick = clock_ms_to_ticks(PLATFORM_DEFAULT_CLOCK, 1) *
us / 1000;
uint32_t tries = DEFAULT_TRY_TIMES;
uint64_t delta = tick / tries;
if (!delta) {
delta = us;
tries = 1;
}
while (!wait_is_completed(comp)) {
if (!tries--) {
trace_error(TRACE_CLASS_WAIT, "ewt");
return -EIO;
}
wait_delay(delta);
}
return 0;
}
static inline int poll_for_register_delay(uint32_t reg,
uint32_t mask,
uint32_t val, uint64_t us)
{
uint64_t tick = clock_ms_to_ticks(PLATFORM_DEFAULT_CLOCK, 1) *
us / 1000;
uint32_t tries = DEFAULT_TRY_TIMES;
uint64_t delta = tick / tries;
if (!delta) {
delta = us;
tries = 1;
}
while ((io_reg_read(reg) & mask) != val) {
if (!tries--) {
trace_error(TRACE_CLASS_WAIT, "ewt");
return -EIO;
}
wait_delay(delta);
}
return 0;
}
#endif