blob: 5f0c972a80fb433d08b474fdb21e7da7b0d589ae [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 Wilsonc7302f22019-08-08 21:27:58 +010011#include <linux/bits.h>
Chris Wilsond91e6572019-04-24 21:07:13 +010012#include <linux/mutex.h>
Chris Wilsonb27e35a2019-05-27 12:51:14 +010013#include <linux/refcount.h>
Chris Wilsond91e6572019-04-24 21:07:13 +010014#include <linux/stackdepot.h>
Chris Wilsonb27e35a2019-05-27 12:51:14 +010015#include <linux/timer.h>
Chris Wilsonc7302f22019-08-08 21:27:58 +010016#include <linux/workqueue.h>
Chris Wilsond91e6572019-04-24 21:07:13 +010017
Chris Wilsonfb993aa2019-06-21 19:38:01 +010018#if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
19#define INTEL_WAKEREF_BUG_ON(expr) BUG_ON(expr)
20#else
21#define INTEL_WAKEREF_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
22#endif
23
Daniele Ceraolo Spurio58a111f2019-06-13 16:21:56 -070024struct intel_runtime_pm;
Chris Wilsonc7302f22019-08-08 21:27:58 +010025struct intel_wakeref;
Chris Wilsond91e6572019-04-24 21:07:13 +010026
27typedef depot_stack_handle_t intel_wakeref_t;
28
Chris Wilsonc7302f22019-08-08 21:27:58 +010029struct intel_wakeref_ops {
30 int (*get)(struct intel_wakeref *wf);
31 int (*put)(struct intel_wakeref *wf);
32
33 unsigned long flags;
34#define INTEL_WAKEREF_PUT_ASYNC BIT(0)
35};
36
Chris Wilsond91e6572019-04-24 21:07:13 +010037struct intel_wakeref {
38 atomic_t count;
39 struct mutex mutex;
Chris Wilsonc7302f22019-08-08 21:27:58 +010040
Chris Wilsond91e6572019-04-24 21:07:13 +010041 intel_wakeref_t wakeref;
Chris Wilsonc7302f22019-08-08 21:27:58 +010042
43 struct intel_runtime_pm *rpm;
44 const struct intel_wakeref_ops *ops;
45
46 struct work_struct work;
Chris Wilsond91e6572019-04-24 21:07:13 +010047};
48
49void __intel_wakeref_init(struct intel_wakeref *wf,
Chris Wilsonc7302f22019-08-08 21:27:58 +010050 struct intel_runtime_pm *rpm,
51 const struct intel_wakeref_ops *ops,
Chris Wilsond91e6572019-04-24 21:07:13 +010052 struct lock_class_key *key);
Chris Wilsonc7302f22019-08-08 21:27:58 +010053#define intel_wakeref_init(wf, rpm, ops) do { \
Chris Wilsond91e6572019-04-24 21:07:13 +010054 static struct lock_class_key __key; \
55 \
Chris Wilsonc7302f22019-08-08 21:27:58 +010056 __intel_wakeref_init((wf), (rpm), (ops), &__key); \
Chris Wilsond91e6572019-04-24 21:07:13 +010057} while (0)
58
Chris Wilsonc7302f22019-08-08 21:27:58 +010059int __intel_wakeref_get_first(struct intel_wakeref *wf);
60void __intel_wakeref_put_last(struct intel_wakeref *wf);
Chris Wilsond91e6572019-04-24 21:07:13 +010061
62/**
63 * intel_wakeref_get: Acquire the wakeref
64 * @i915: the drm_i915_private device
65 * @wf: the wakeref
66 * @fn: callback for acquired the wakeref, called only on first acquire.
67 *
68 * Acquire a hold on the wakeref. The first user to do so, will acquire
69 * the runtime pm wakeref and then call the @fn underneath the wakeref
70 * mutex.
71 *
72 * Note that @fn is allowed to fail, in which case the runtime-pm wakeref
73 * will be released and the acquisition unwound, and an error reported.
74 *
75 * Returns: 0 if the wakeref was acquired successfully, or a negative error
76 * code otherwise.
77 */
78static inline int
Chris Wilsonc7302f22019-08-08 21:27:58 +010079intel_wakeref_get(struct intel_wakeref *wf)
Chris Wilsond91e6572019-04-24 21:07:13 +010080{
81 if (unlikely(!atomic_inc_not_zero(&wf->count)))
Chris Wilsonc7302f22019-08-08 21:27:58 +010082 return __intel_wakeref_get_first(wf);
Chris Wilsond91e6572019-04-24 21:07:13 +010083
84 return 0;
85}
86
87/**
Chris Wilsonde5147b2019-06-26 16:45:47 +010088 * intel_wakeref_get_if_in_use: Acquire the wakeref
89 * @wf: the wakeref
90 *
91 * Acquire a hold on the wakeref, but only if the wakeref is already
92 * active.
93 *
94 * Returns: true if the wakeref was acquired, false otherwise.
95 */
96static inline bool
97intel_wakeref_get_if_active(struct intel_wakeref *wf)
98{
99 return atomic_inc_not_zero(&wf->count);
100}
101
102/**
Chris Wilsond91e6572019-04-24 21:07:13 +0100103 * intel_wakeref_put: Release the wakeref
104 * @i915: the drm_i915_private device
105 * @wf: the wakeref
106 * @fn: callback for releasing the wakeref, called only on final release.
107 *
108 * Release our hold on the wakeref. When there are no more users,
109 * the runtime pm wakeref will be released after the @fn callback is called
110 * underneath the wakeref mutex.
111 *
112 * Note that @fn is allowed to fail, in which case the runtime-pm wakeref
113 * is retained and an error reported.
114 *
115 * Returns: 0 if the wakeref was released successfully, or a negative error
116 * code otherwise.
117 */
Chris Wilsonc7302f22019-08-08 21:27:58 +0100118static inline void
119intel_wakeref_put(struct intel_wakeref *wf)
Chris Wilsond91e6572019-04-24 21:07:13 +0100120{
Chris Wilsonfb993aa2019-06-21 19:38:01 +0100121 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
Chris Wilsonc7302f22019-08-08 21:27:58 +0100122 if (unlikely(!atomic_add_unless(&wf->count, -1, 1)))
123 __intel_wakeref_put_last(wf);
Chris Wilsond91e6572019-04-24 21:07:13 +0100124}
125
126/**
127 * intel_wakeref_lock: Lock the wakeref (mutex)
128 * @wf: the wakeref
129 *
130 * Locks the wakeref to prevent it being acquired or released. New users
131 * can still adjust the counter, but the wakeref itself (and callback)
132 * cannot be acquired or released.
133 */
134static inline void
135intel_wakeref_lock(struct intel_wakeref *wf)
136 __acquires(wf->mutex)
137{
138 mutex_lock(&wf->mutex);
139}
140
141/**
142 * intel_wakeref_unlock: Unlock the wakeref
143 * @wf: the wakeref
144 *
145 * Releases a previously acquired intel_wakeref_lock().
146 */
147static inline void
148intel_wakeref_unlock(struct intel_wakeref *wf)
149 __releases(wf->mutex)
150{
151 mutex_unlock(&wf->mutex);
152}
153
154/**
Chris Wilson5f22e5b2019-06-25 14:01:14 +0100155 * intel_wakeref_is_active: Query whether the wakeref is currently held
Chris Wilsond91e6572019-04-24 21:07:13 +0100156 * @wf: the wakeref
157 *
158 * Returns: true if the wakeref is currently held.
159 */
160static inline bool
Chris Wilson5f22e5b2019-06-25 14:01:14 +0100161intel_wakeref_is_active(const struct intel_wakeref *wf)
Chris Wilsond91e6572019-04-24 21:07:13 +0100162{
Chris Wilson7ee280a2019-05-03 12:52:14 +0100163 return READ_ONCE(wf->wakeref);
Chris Wilsond91e6572019-04-24 21:07:13 +0100164}
165
Chris Wilsonc7302f22019-08-08 21:27:58 +0100166/**
Chris Wilsona79ca652019-08-13 20:07:05 +0100167 * __intel_wakeref_defer_park: Defer the current park callback
168 * @wf: the wakeref
169 */
170static inline void
171__intel_wakeref_defer_park(struct intel_wakeref *wf)
172{
173 INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count));
174 atomic_set_release(&wf->count, 1);
175}
176
177/**
Chris Wilsonc7302f22019-08-08 21:27:58 +0100178 * intel_wakeref_wait_for_idle: Wait until the wakeref is idle
179 * @wf: the wakeref
180 *
181 * Wait for the earlier asynchronous release of the wakeref. Note
182 * this will wait for any third party as well, so make sure you only wait
183 * when you have control over the wakeref and trust no one else is acquiring
184 * it.
185 *
186 * Return: 0 on success, error code if killed.
187 */
188int intel_wakeref_wait_for_idle(struct intel_wakeref *wf);
189
Chris Wilsonb27e35a2019-05-27 12:51:14 +0100190struct intel_wakeref_auto {
Daniele Ceraolo Spurio58a111f2019-06-13 16:21:56 -0700191 struct intel_runtime_pm *rpm;
Chris Wilsonb27e35a2019-05-27 12:51:14 +0100192 struct timer_list timer;
193 intel_wakeref_t wakeref;
194 spinlock_t lock;
195 refcount_t count;
196};
197
198/**
199 * intel_wakeref_auto: Delay the runtime-pm autosuspend
200 * @wf: the wakeref
201 * @timeout: relative timeout in jiffies
202 *
203 * The runtime-pm core uses a suspend delay after the last wakeref
204 * is released before triggering runtime suspend of the device. That
205 * delay is configurable via sysfs with little regard to the device
206 * characteristics. Instead, we want to tune the autosuspend based on our
207 * HW knowledge. intel_wakeref_auto() delays the sleep by the supplied
208 * timeout.
209 *
210 * Pass @timeout = 0 to cancel a previous autosuspend by executing the
211 * suspend immediately.
212 */
213void intel_wakeref_auto(struct intel_wakeref_auto *wf, unsigned long timeout);
214
215void intel_wakeref_auto_init(struct intel_wakeref_auto *wf,
Daniele Ceraolo Spurio58a111f2019-06-13 16:21:56 -0700216 struct intel_runtime_pm *rpm);
Chris Wilsonb27e35a2019-05-27 12:51:14 +0100217void intel_wakeref_auto_fini(struct intel_wakeref_auto *wf);
218
Chris Wilsond91e6572019-04-24 21:07:13 +0100219#endif /* INTEL_WAKEREF_H */