Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 1 | /* |
| 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 Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 11 | #include <linux/bits.h> |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 12 | #include <linux/mutex.h> |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame] | 13 | #include <linux/refcount.h> |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 14 | #include <linux/stackdepot.h> |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame] | 15 | #include <linux/timer.h> |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 16 | #include <linux/workqueue.h> |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 17 | |
Chris Wilson | fb993aa | 2019-06-21 19:38:01 +0100 | [diff] [blame] | 18 | #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 Spurio | 58a111f | 2019-06-13 16:21:56 -0700 | [diff] [blame] | 24 | struct intel_runtime_pm; |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 25 | struct intel_wakeref; |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 26 | |
| 27 | typedef depot_stack_handle_t intel_wakeref_t; |
| 28 | |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 29 | struct 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 Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 37 | struct intel_wakeref { |
| 38 | atomic_t count; |
| 39 | struct mutex mutex; |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 40 | |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 41 | intel_wakeref_t wakeref; |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 42 | |
| 43 | struct intel_runtime_pm *rpm; |
| 44 | const struct intel_wakeref_ops *ops; |
| 45 | |
| 46 | struct work_struct work; |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | void __intel_wakeref_init(struct intel_wakeref *wf, |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 50 | struct intel_runtime_pm *rpm, |
| 51 | const struct intel_wakeref_ops *ops, |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 52 | struct lock_class_key *key); |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 53 | #define intel_wakeref_init(wf, rpm, ops) do { \ |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 54 | static struct lock_class_key __key; \ |
| 55 | \ |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 56 | __intel_wakeref_init((wf), (rpm), (ops), &__key); \ |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 57 | } while (0) |
| 58 | |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 59 | int __intel_wakeref_get_first(struct intel_wakeref *wf); |
| 60 | void __intel_wakeref_put_last(struct intel_wakeref *wf); |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 61 | |
| 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 | */ |
| 78 | static inline int |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 79 | intel_wakeref_get(struct intel_wakeref *wf) |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 80 | { |
| 81 | if (unlikely(!atomic_inc_not_zero(&wf->count))) |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 82 | return __intel_wakeref_get_first(wf); |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /** |
Chris Wilson | de5147b | 2019-06-26 16:45:47 +0100 | [diff] [blame] | 88 | * 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 | */ |
| 96 | static inline bool |
| 97 | intel_wakeref_get_if_active(struct intel_wakeref *wf) |
| 98 | { |
| 99 | return atomic_inc_not_zero(&wf->count); |
| 100 | } |
| 101 | |
| 102 | /** |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 103 | * 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 Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 118 | static inline void |
| 119 | intel_wakeref_put(struct intel_wakeref *wf) |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 120 | { |
Chris Wilson | fb993aa | 2019-06-21 19:38:01 +0100 | [diff] [blame] | 121 | INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0); |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 122 | if (unlikely(!atomic_add_unless(&wf->count, -1, 1))) |
| 123 | __intel_wakeref_put_last(wf); |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 124 | } |
| 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 | */ |
| 134 | static inline void |
| 135 | intel_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 | */ |
| 147 | static inline void |
| 148 | intel_wakeref_unlock(struct intel_wakeref *wf) |
| 149 | __releases(wf->mutex) |
| 150 | { |
| 151 | mutex_unlock(&wf->mutex); |
| 152 | } |
| 153 | |
| 154 | /** |
Chris Wilson | 5f22e5b | 2019-06-25 14:01:14 +0100 | [diff] [blame] | 155 | * intel_wakeref_is_active: Query whether the wakeref is currently held |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 156 | * @wf: the wakeref |
| 157 | * |
| 158 | * Returns: true if the wakeref is currently held. |
| 159 | */ |
| 160 | static inline bool |
Chris Wilson | 5f22e5b | 2019-06-25 14:01:14 +0100 | [diff] [blame] | 161 | intel_wakeref_is_active(const struct intel_wakeref *wf) |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 162 | { |
Chris Wilson | 7ee280a | 2019-05-03 12:52:14 +0100 | [diff] [blame] | 163 | return READ_ONCE(wf->wakeref); |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Chris Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 166 | /** |
Chris Wilson | a79ca65 | 2019-08-13 20:07:05 +0100 | [diff] [blame] | 167 | * __intel_wakeref_defer_park: Defer the current park callback |
| 168 | * @wf: the wakeref |
| 169 | */ |
| 170 | static 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 Wilson | c7302f2 | 2019-08-08 21:27:58 +0100 | [diff] [blame] | 178 | * 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 | */ |
| 188 | int intel_wakeref_wait_for_idle(struct intel_wakeref *wf); |
| 189 | |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame] | 190 | struct intel_wakeref_auto { |
Daniele Ceraolo Spurio | 58a111f | 2019-06-13 16:21:56 -0700 | [diff] [blame] | 191 | struct intel_runtime_pm *rpm; |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame] | 192 | 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 | */ |
| 213 | void intel_wakeref_auto(struct intel_wakeref_auto *wf, unsigned long timeout); |
| 214 | |
| 215 | void intel_wakeref_auto_init(struct intel_wakeref_auto *wf, |
Daniele Ceraolo Spurio | 58a111f | 2019-06-13 16:21:56 -0700 | [diff] [blame] | 216 | struct intel_runtime_pm *rpm); |
Chris Wilson | b27e35a | 2019-05-27 12:51:14 +0100 | [diff] [blame] | 217 | void intel_wakeref_auto_fini(struct intel_wakeref_auto *wf); |
| 218 | |
Chris Wilson | d91e657 | 2019-04-24 21:07:13 +0100 | [diff] [blame] | 219 | #endif /* INTEL_WAKEREF_H */ |