blob: 545c8f277c46013f6b6326d03e23fda372d99d70 [file] [log] [blame]
Chris Wilsond91e6572019-04-24 21:07:13 +01001/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright © 2019 Intel Corporation
5 */
6
7#ifndef INTEL_WAKEREF_H
8#define INTEL_WAKEREF_H
9
10#include <linux/atomic.h>
Chris Wilsone9037e72020-03-23 10:32:21 +000011#include <linux/bitfield.h>
Chris Wilsonc7302f22019-08-08 21:27:58 +010012#include <linux/bits.h>
Chris Wilson07779a72019-11-20 12:54:33 +000013#include <linux/lockdep.h>
Chris Wilsond91e6572019-04-24 21:07:13 +010014#include <linux/mutex.h>
Chris Wilsonb27e35a2019-05-27 12:51:14 +010015#include <linux/refcount.h>
Chris Wilsond91e6572019-04-24 21:07:13 +010016#include <linux/stackdepot.h>
Chris Wilsonb27e35a2019-05-27 12:51:14 +010017#include <linux/timer.h>
Chris Wilsonc7302f22019-08-08 21:27:58 +010018#include <linux/workqueue.h>
Chris Wilsond91e6572019-04-24 21:07:13 +010019
Chris Wilsonfb993aa2019-06-21 19:38:01 +010020#if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
21#define INTEL_WAKEREF_BUG_ON(expr) BUG_ON(expr)
22#else
23#define INTEL_WAKEREF_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
24#endif
25
Daniele Ceraolo Spurio58a111f2019-06-13 16:21:56 -070026struct intel_runtime_pm;
Chris Wilsonc7302f22019-08-08 21:27:58 +010027struct intel_wakeref;
Chris Wilsond91e6572019-04-24 21:07:13 +010028
29typedef depot_stack_handle_t intel_wakeref_t;
30
Chris Wilsonc7302f22019-08-08 21:27:58 +010031struct intel_wakeref_ops {
32 int (*get)(struct intel_wakeref *wf);
33 int (*put)(struct intel_wakeref *wf);
Chris Wilsonc7302f22019-08-08 21:27:58 +010034};
35
Chris Wilsond91e6572019-04-24 21:07:13 +010036struct intel_wakeref {
37 atomic_t count;
38 struct mutex mutex;
Chris Wilsonc7302f22019-08-08 21:27:58 +010039
Chris Wilsond91e6572019-04-24 21:07:13 +010040 intel_wakeref_t wakeref;
Chris Wilsonc7302f22019-08-08 21:27:58 +010041
42 struct intel_runtime_pm *rpm;
43 const struct intel_wakeref_ops *ops;
44
Chris Wilsone9037e72020-03-23 10:32:21 +000045 struct delayed_work work;
Chris Wilsond91e6572019-04-24 21:07:13 +010046};
47
Chris Wilsoncdd280b12020-01-02 23:16:04 +000048struct intel_wakeref_lockclass {
49 struct lock_class_key mutex;
50 struct lock_class_key work;
51};
52
Chris Wilsond91e6572019-04-24 21:07:13 +010053void __intel_wakeref_init(struct intel_wakeref *wf,
Chris Wilsonc7302f22019-08-08 21:27:58 +010054 struct intel_runtime_pm *rpm,
55 const struct intel_wakeref_ops *ops,
Chris Wilsoncdd280b12020-01-02 23:16:04 +000056 struct intel_wakeref_lockclass *key);
Chris Wilsonc7302f22019-08-08 21:27:58 +010057#define intel_wakeref_init(wf, rpm, ops) do { \
Chris Wilsoncdd280b12020-01-02 23:16:04 +000058 static struct intel_wakeref_lockclass __key; \
Chris Wilsond91e6572019-04-24 21:07:13 +010059 \
Chris Wilsonc7302f22019-08-08 21:27:58 +010060 __intel_wakeref_init((wf), (rpm), (ops), &__key); \
Chris Wilsond91e6572019-04-24 21:07:13 +010061} while (0)
62
Chris Wilsonc7302f22019-08-08 21:27:58 +010063int __intel_wakeref_get_first(struct intel_wakeref *wf);
Chris Wilson07779a72019-11-20 12:54:33 +000064void __intel_wakeref_put_last(struct intel_wakeref *wf, unsigned long flags);
Chris Wilsond91e6572019-04-24 21:07:13 +010065
66/**
67 * intel_wakeref_get: Acquire the wakeref
Chris Wilsond91e6572019-04-24 21:07:13 +010068 * @wf: the wakeref
Chris Wilsond91e6572019-04-24 21:07:13 +010069 *
70 * Acquire a hold on the wakeref. The first user to do so, will acquire
71 * the runtime pm wakeref and then call the @fn underneath the wakeref
72 * mutex.
73 *
74 * Note that @fn is allowed to fail, in which case the runtime-pm wakeref
75 * will be released and the acquisition unwound, and an error reported.
76 *
77 * Returns: 0 if the wakeref was acquired successfully, or a negative error
78 * code otherwise.
79 */
80static inline int
Chris Wilsonc7302f22019-08-08 21:27:58 +010081intel_wakeref_get(struct intel_wakeref *wf)
Chris Wilsond91e6572019-04-24 21:07:13 +010082{
Chris Wilson93b0e8f2019-11-21 13:05:28 +000083 might_sleep();
Chris Wilsond91e6572019-04-24 21:07:13 +010084 if (unlikely(!atomic_inc_not_zero(&wf->count)))
Chris Wilsonc7302f22019-08-08 21:27:58 +010085 return __intel_wakeref_get_first(wf);
Chris Wilsond91e6572019-04-24 21:07:13 +010086
87 return 0;
88}
89
90/**
Chris Wilson93b0e8f2019-11-21 13:05:28 +000091 * __intel_wakeref_get: Acquire the wakeref, again
92 * @wf: the wakeref
93 *
94 * Increment the wakeref counter, only valid if it is already held by
95 * the caller.
96 *
97 * See intel_wakeref_get().
98 */
99static inline void
100__intel_wakeref_get(struct intel_wakeref *wf)
101{
102 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
103 atomic_inc(&wf->count);
104}
105
106/**
Chris Wilsonde5147b2019-06-26 16:45:47 +0100107 * intel_wakeref_get_if_in_use: Acquire the wakeref
108 * @wf: the wakeref
109 *
110 * Acquire a hold on the wakeref, but only if the wakeref is already
111 * active.
112 *
113 * Returns: true if the wakeref was acquired, false otherwise.
114 */
115static inline bool
116intel_wakeref_get_if_active(struct intel_wakeref *wf)
117{
118 return atomic_inc_not_zero(&wf->count);
119}
120
Chris Wilsone9037e72020-03-23 10:32:21 +0000121enum {
122 INTEL_WAKEREF_PUT_ASYNC_BIT = 0,
123 __INTEL_WAKEREF_PUT_LAST_BIT__
124};
125
Chris Wilsonde5147b2019-06-26 16:45:47 +0100126/**
Chris Wilson07779a72019-11-20 12:54:33 +0000127 * intel_wakeref_put_flags: Release the wakeref
Chris Wilsond91e6572019-04-24 21:07:13 +0100128 * @wf: the wakeref
Chris Wilson07779a72019-11-20 12:54:33 +0000129 * @flags: control flags
Chris Wilsond91e6572019-04-24 21:07:13 +0100130 *
131 * Release our hold on the wakeref. When there are no more users,
132 * the runtime pm wakeref will be released after the @fn callback is called
133 * underneath the wakeref mutex.
134 *
135 * Note that @fn is allowed to fail, in which case the runtime-pm wakeref
136 * is retained and an error reported.
137 *
138 * Returns: 0 if the wakeref was released successfully, or a negative error
139 * code otherwise.
140 */
Chris Wilsonc7302f22019-08-08 21:27:58 +0100141static inline void
Chris Wilson07779a72019-11-20 12:54:33 +0000142__intel_wakeref_put(struct intel_wakeref *wf, unsigned long flags)
Chris Wilsone9037e72020-03-23 10:32:21 +0000143#define INTEL_WAKEREF_PUT_ASYNC BIT(INTEL_WAKEREF_PUT_ASYNC_BIT)
144#define INTEL_WAKEREF_PUT_DELAY \
145 GENMASK(BITS_PER_LONG - 1, __INTEL_WAKEREF_PUT_LAST_BIT__)
Chris Wilsond91e6572019-04-24 21:07:13 +0100146{
Chris Wilsonfb993aa2019-06-21 19:38:01 +0100147 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
Chris Wilsonc7302f22019-08-08 21:27:58 +0100148 if (unlikely(!atomic_add_unless(&wf->count, -1, 1)))
Chris Wilson07779a72019-11-20 12:54:33 +0000149 __intel_wakeref_put_last(wf, flags);
150}
151
152static inline void
153intel_wakeref_put(struct intel_wakeref *wf)
154{
155 might_sleep();
156 __intel_wakeref_put(wf, 0);
157}
158
159static inline void
160intel_wakeref_put_async(struct intel_wakeref *wf)
161{
162 __intel_wakeref_put(wf, INTEL_WAKEREF_PUT_ASYNC);
Chris Wilsond91e6572019-04-24 21:07:13 +0100163}
164
Chris Wilsone9037e72020-03-23 10:32:21 +0000165static inline void
166intel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
167{
168 __intel_wakeref_put(wf,
169 INTEL_WAKEREF_PUT_ASYNC |
170 FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
171}
172
Chris Wilsond91e6572019-04-24 21:07:13 +0100173/**
174 * intel_wakeref_lock: Lock the wakeref (mutex)
175 * @wf: the wakeref
176 *
177 * Locks the wakeref to prevent it being acquired or released. New users
178 * can still adjust the counter, but the wakeref itself (and callback)
179 * cannot be acquired or released.
180 */
181static inline void
182intel_wakeref_lock(struct intel_wakeref *wf)
183 __acquires(wf->mutex)
184{
185 mutex_lock(&wf->mutex);
186}
187
188/**
189 * intel_wakeref_unlock: Unlock the wakeref
190 * @wf: the wakeref
191 *
192 * Releases a previously acquired intel_wakeref_lock().
193 */
194static inline void
195intel_wakeref_unlock(struct intel_wakeref *wf)
196 __releases(wf->mutex)
197{
198 mutex_unlock(&wf->mutex);
199}
200
201/**
Chris Wilsonf4ba0702019-11-18 23:02:46 +0000202 * intel_wakeref_unlock_wait: Wait until the active callback is complete
203 * @wf: the wakeref
204 *
205 * Waits for the active callback (under the @wf->mutex or another CPU) is
206 * complete.
207 */
208static inline void
209intel_wakeref_unlock_wait(struct intel_wakeref *wf)
210{
211 mutex_lock(&wf->mutex);
212 mutex_unlock(&wf->mutex);
Chris Wilsone9037e72020-03-23 10:32:21 +0000213 flush_delayed_work(&wf->work);
Chris Wilsonf4ba0702019-11-18 23:02:46 +0000214}
215
216/**
Chris Wilson5f22e5b2019-06-25 14:01:14 +0100217 * intel_wakeref_is_active: Query whether the wakeref is currently held
Chris Wilsond91e6572019-04-24 21:07:13 +0100218 * @wf: the wakeref
219 *
220 * Returns: true if the wakeref is currently held.
221 */
222static inline bool
Chris Wilson5f22e5b2019-06-25 14:01:14 +0100223intel_wakeref_is_active(const struct intel_wakeref *wf)
Chris Wilsond91e6572019-04-24 21:07:13 +0100224{
Chris Wilson7ee280a2019-05-03 12:52:14 +0100225 return READ_ONCE(wf->wakeref);
Chris Wilsond91e6572019-04-24 21:07:13 +0100226}
227
Chris Wilsonc7302f22019-08-08 21:27:58 +0100228/**
Chris Wilsona79ca652019-08-13 20:07:05 +0100229 * __intel_wakeref_defer_park: Defer the current park callback
230 * @wf: the wakeref
231 */
232static inline void
233__intel_wakeref_defer_park(struct intel_wakeref *wf)
234{
Chris Wilson07779a72019-11-20 12:54:33 +0000235 lockdep_assert_held(&wf->mutex);
Chris Wilsona79ca652019-08-13 20:07:05 +0100236 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count));
237 atomic_set_release(&wf->count, 1);
238}
239
240/**
Chris Wilsonc7302f22019-08-08 21:27:58 +0100241 * intel_wakeref_wait_for_idle: Wait until the wakeref is idle
242 * @wf: the wakeref
243 *
244 * Wait for the earlier asynchronous release of the wakeref. Note
245 * this will wait for any third party as well, so make sure you only wait
246 * when you have control over the wakeref and trust no one else is acquiring
247 * it.
248 *
249 * Return: 0 on success, error code if killed.
250 */
251int intel_wakeref_wait_for_idle(struct intel_wakeref *wf);
252
Chris Wilsonb27e35a2019-05-27 12:51:14 +0100253struct intel_wakeref_auto {
Daniele Ceraolo Spurio58a111f2019-06-13 16:21:56 -0700254 struct intel_runtime_pm *rpm;
Chris Wilsonb27e35a2019-05-27 12:51:14 +0100255 struct timer_list timer;
256 intel_wakeref_t wakeref;
257 spinlock_t lock;
258 refcount_t count;
259};
260
261/**
262 * intel_wakeref_auto: Delay the runtime-pm autosuspend
263 * @wf: the wakeref
264 * @timeout: relative timeout in jiffies
265 *
266 * The runtime-pm core uses a suspend delay after the last wakeref
267 * is released before triggering runtime suspend of the device. That
268 * delay is configurable via sysfs with little regard to the device
269 * characteristics. Instead, we want to tune the autosuspend based on our
270 * HW knowledge. intel_wakeref_auto() delays the sleep by the supplied
271 * timeout.
272 *
273 * Pass @timeout = 0 to cancel a previous autosuspend by executing the
274 * suspend immediately.
275 */
276void intel_wakeref_auto(struct intel_wakeref_auto *wf, unsigned long timeout);
277
278void intel_wakeref_auto_init(struct intel_wakeref_auto *wf,
Daniele Ceraolo Spurio58a111f2019-06-13 16:21:56 -0700279 struct intel_runtime_pm *rpm);
Chris Wilsonb27e35a2019-05-27 12:51:14 +0100280void intel_wakeref_auto_fini(struct intel_wakeref_auto *wf);
281
Chris Wilsond91e6572019-04-24 21:07:13 +0100282#endif /* INTEL_WAKEREF_H */