forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_runtime.c
327 lines (261 loc) · 6.73 KB
/
device_runtime.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright (c) 2018 Intel Corporation.
* Copyright (c) 2021 Nordic Semiconductor ASA.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/pm/device.h>
#include <zephyr/pm/device_runtime.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(pm_device, CONFIG_PM_DEVICE_LOG_LEVEL);
#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
#define PM_DOMAIN(_pm) \
(_pm)->domain
#else
#define PM_DOMAIN(_pm) NULL
#endif
/**
* @brief Suspend a device
*
* @note Asynchronous operations are not supported when in pre-kernel mode. In
* this case, the async flag will be always forced to be false, and so the
* the function will be blocking.
*
* @funcprops \pre_kernel_ok
*
* @param dev Device instance.
* @param async Perform operation asynchronously.
*
* @retval 0 If device has been suspended or queued for suspend.
* @retval -EALREADY If device is already suspended (can only happen if get/put
* calls are unbalanced).
* @retval -errno Other negative errno, result of the action callback.
*/
static int runtime_suspend(const struct device *dev, bool async)
{
int ret = 0;
struct pm_device *pm = dev->pm;
if (k_is_pre_kernel()) {
async = false;
} else {
(void)k_mutex_lock(&pm->lock, K_FOREVER);
}
if (!atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED)) {
goto unlock;
}
if (pm->usage == 0U) {
LOG_WRN("Unbalanced suspend");
ret = -EALREADY;
goto unlock;
}
pm->usage--;
if (pm->usage > 0U) {
goto unlock;
}
if (async && !k_is_pre_kernel()) {
/* queue suspend */
pm->state = PM_DEVICE_STATE_SUSPENDING;
(void)k_work_schedule(&pm->work, K_NO_WAIT);
} else {
/* suspend now */
ret = pm->action_cb(pm->dev, PM_DEVICE_ACTION_SUSPEND);
if (ret < 0) {
pm->usage++;
goto unlock;
}
pm->state = PM_DEVICE_STATE_SUSPENDED;
}
unlock:
if (!k_is_pre_kernel()) {
k_mutex_unlock(&pm->lock);
}
return ret;
}
static void runtime_suspend_work(struct k_work *work)
{
int ret;
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
struct pm_device *pm = CONTAINER_OF(dwork, struct pm_device, work);
ret = pm->action_cb(pm->dev, PM_DEVICE_ACTION_SUSPEND);
(void)k_mutex_lock(&pm->lock, K_FOREVER);
if (ret < 0) {
pm->usage++;
pm->state = PM_DEVICE_STATE_ACTIVE;
} else {
pm->state = PM_DEVICE_STATE_SUSPENDED;
}
k_condvar_broadcast(&pm->condvar);
k_mutex_unlock(&pm->lock);
/*
* On async put, we have to suspend the domain when the device
* finishes its operation
*/
if (PM_DOMAIN(pm) != NULL) {
(void)pm_device_runtime_put(PM_DOMAIN(pm));
}
__ASSERT(ret == 0, "Could not suspend device (%d)", ret);
}
int pm_device_runtime_get(const struct device *dev)
{
int ret = 0;
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return -ENOTSUP;
}
SYS_PORT_TRACING_FUNC_ENTER(pm, device_runtime_get, dev);
if (!k_is_pre_kernel()) {
(void)k_mutex_lock(&pm->lock, K_FOREVER);
}
if (!atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED)) {
goto unlock;
}
/*
* If the device is under a power domain, the domain has to be get
* first.
*/
if (PM_DOMAIN(pm) != NULL) {
ret = pm_device_runtime_get(PM_DOMAIN(pm));
if (ret != 0) {
goto unlock;
}
/* Check if powering up this device failed */
if (atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_TURN_ON_FAILED)) {
(void)pm_device_runtime_put(PM_DOMAIN(pm));
ret = -EAGAIN;
goto unlock;
}
}
pm->usage++;
if (!k_is_pre_kernel()) {
/* wait until possible async suspend is completed */
while (pm->state == PM_DEVICE_STATE_SUSPENDING) {
(void)k_condvar_wait(&pm->condvar, &pm->lock, K_FOREVER);
}
}
if (pm->usage > 1U) {
goto unlock;
}
ret = pm->action_cb(pm->dev, PM_DEVICE_ACTION_RESUME);
if (ret < 0) {
pm->usage--;
goto unlock;
}
pm->state = PM_DEVICE_STATE_ACTIVE;
unlock:
if (!k_is_pre_kernel()) {
k_mutex_unlock(&pm->lock);
}
SYS_PORT_TRACING_FUNC_EXIT(pm, device_runtime_get, dev, ret);
return ret;
}
int pm_device_runtime_put(const struct device *dev)
{
int ret;
if (dev->pm == NULL) {
return -ENOTSUP;
}
SYS_PORT_TRACING_FUNC_ENTER(pm, device_runtime_put, dev);
ret = runtime_suspend(dev, false);
/*
* Now put the domain
*/
if ((ret == 0) && PM_DOMAIN(dev->pm) != NULL) {
ret = pm_device_runtime_put(PM_DOMAIN(dev->pm));
}
SYS_PORT_TRACING_FUNC_EXIT(pm, device_runtime_put, dev, ret);
return ret;
}
int pm_device_runtime_put_async(const struct device *dev)
{
int ret;
if (dev->pm == NULL) {
return -ENOTSUP;
}
SYS_PORT_TRACING_FUNC_ENTER(pm, device_runtime_put_async, dev);
ret = runtime_suspend(dev, true);
SYS_PORT_TRACING_FUNC_EXIT(pm, device_runtime_put_async, dev, ret);
return ret;
}
int pm_device_runtime_enable(const struct device *dev)
{
int ret = 0;
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return -ENOTSUP;
}
SYS_PORT_TRACING_FUNC_ENTER(pm, device_runtime_enable, dev);
if (pm_device_state_is_locked(dev)) {
ret = -EPERM;
goto end;
}
if (!k_is_pre_kernel()) {
(void)k_mutex_lock(&pm->lock, K_FOREVER);
}
if (atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED)) {
goto unlock;
}
/* lazy init of PM fields */
if (pm->dev == NULL) {
pm->dev = dev;
k_work_init_delayable(&pm->work, runtime_suspend_work);
}
if (pm->state == PM_DEVICE_STATE_ACTIVE) {
ret = pm->action_cb(pm->dev, PM_DEVICE_ACTION_SUSPEND);
if (ret < 0) {
goto unlock;
}
pm->state = PM_DEVICE_STATE_SUSPENDED;
}
pm->usage = 0U;
atomic_set_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED);
unlock:
if (!k_is_pre_kernel()) {
k_mutex_unlock(&pm->lock);
}
end:
SYS_PORT_TRACING_FUNC_EXIT(pm, device_runtime_enable, dev, ret);
return ret;
}
int pm_device_runtime_disable(const struct device *dev)
{
int ret = 0;
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return -ENOTSUP;
}
SYS_PORT_TRACING_FUNC_ENTER(pm, device_runtime_disable, dev);
if (!k_is_pre_kernel()) {
(void)k_mutex_lock(&pm->lock, K_FOREVER);
}
if (!atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED)) {
goto unlock;
}
/* wait until possible async suspend is completed */
if (!k_is_pre_kernel()) {
while (pm->state == PM_DEVICE_STATE_SUSPENDING) {
(void)k_condvar_wait(&pm->condvar, &pm->lock,
K_FOREVER);
}
}
/* wake up the device if suspended */
if (pm->state == PM_DEVICE_STATE_SUSPENDED) {
ret = pm->action_cb(pm->dev, PM_DEVICE_ACTION_RESUME);
if (ret < 0) {
goto unlock;
}
pm->state = PM_DEVICE_STATE_ACTIVE;
}
atomic_clear_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED);
unlock:
if (!k_is_pre_kernel()) {
k_mutex_unlock(&pm->lock);
}
SYS_PORT_TRACING_FUNC_EXIT(pm, device_runtime_disable, dev, ret);
return ret;
}
bool pm_device_runtime_is_enabled(const struct device *dev)
{
struct pm_device *pm = dev->pm;
return pm && atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED);
}