Commit | Line | Data |
---|---|---|
9c065a7d DV |
1 | /* |
2 | * Copyright © 2012-2014 Intel Corporation | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice (including the next | |
12 | * paragraph) shall be included in all copies or substantial portions of the | |
13 | * Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | * | |
23 | * Authors: | |
24 | * Eugeni Dodonov <eugeni.dodonov@intel.com> | |
25 | * Daniel Vetter <daniel.vetter@ffwll.ch> | |
26 | * | |
27 | */ | |
28 | ||
29 | #include <linux/pm_runtime.h> | |
30 | #include <linux/vgaarb.h> | |
31 | ||
bd780f37 CW |
32 | #include <drm/drm_print.h> |
33 | ||
9c065a7d | 34 | #include "i915_drv.h" |
9c065a7d | 35 | |
e4e7684f DV |
36 | /** |
37 | * DOC: runtime pm | |
38 | * | |
39 | * The i915 driver supports dynamic enabling and disabling of entire hardware | |
40 | * blocks at runtime. This is especially important on the display side where | |
41 | * software is supposed to control many power gates manually on recent hardware, | |
42 | * since on the GT side a lot of the power management is done by the hardware. | |
43 | * But even there some manual control at the device level is required. | |
44 | * | |
45 | * Since i915 supports a diverse set of platforms with a unified codebase and | |
46 | * hardware engineers just love to shuffle functionality around between power | |
47 | * domains there's a sizeable amount of indirection required. This file provides | |
48 | * generic functions to the driver for grabbing and releasing references for | |
49 | * abstract power domains. It then maps those to the actual power wells | |
50 | * present for a given platform. | |
51 | */ | |
52 | ||
bd780f37 CW |
53 | #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM) |
54 | ||
55 | #include <linux/sort.h> | |
56 | ||
57 | #define STACKDEPTH 8 | |
58 | ||
59 | static noinline depot_stack_handle_t __save_depot_stack(void) | |
60 | { | |
61 | unsigned long entries[STACKDEPTH]; | |
487f3c7f | 62 | unsigned int n; |
bd780f37 | 63 | |
487f3c7f TG |
64 | n = stack_trace_save(entries, ARRAY_SIZE(entries), 1); |
65 | return stack_depot_save(entries, n, GFP_NOWAIT | __GFP_NOWARN); | |
bd780f37 CW |
66 | } |
67 | ||
68 | static void __print_depot_stack(depot_stack_handle_t stack, | |
69 | char *buf, int sz, int indent) | |
70 | { | |
487f3c7f TG |
71 | unsigned long *entries; |
72 | unsigned int nr_entries; | |
bd780f37 | 73 | |
487f3c7f TG |
74 | nr_entries = stack_depot_fetch(stack, &entries); |
75 | stack_trace_snprint(buf, sz, entries, nr_entries, indent); | |
bd780f37 CW |
76 | } |
77 | ||
d5b6c275 | 78 | static void init_intel_runtime_pm_wakeref(struct i915_runtime_pm *rpm) |
bd780f37 | 79 | { |
bd780f37 CW |
80 | spin_lock_init(&rpm->debug.lock); |
81 | } | |
82 | ||
16e4dd03 | 83 | static noinline depot_stack_handle_t |
d5b6c275 | 84 | track_intel_runtime_pm_wakeref(struct i915_runtime_pm *rpm) |
bd780f37 | 85 | { |
bd780f37 CW |
86 | depot_stack_handle_t stack, *stacks; |
87 | unsigned long flags; | |
88 | ||
d5b6c275 | 89 | if (!rpm->available) |
16e4dd03 | 90 | return -1; |
bd780f37 CW |
91 | |
92 | stack = __save_depot_stack(); | |
93 | if (!stack) | |
16e4dd03 | 94 | return -1; |
bd780f37 CW |
95 | |
96 | spin_lock_irqsave(&rpm->debug.lock, flags); | |
97 | ||
98 | if (!rpm->debug.count) | |
99 | rpm->debug.last_acquire = stack; | |
100 | ||
101 | stacks = krealloc(rpm->debug.owners, | |
102 | (rpm->debug.count + 1) * sizeof(*stacks), | |
103 | GFP_NOWAIT | __GFP_NOWARN); | |
104 | if (stacks) { | |
105 | stacks[rpm->debug.count++] = stack; | |
106 | rpm->debug.owners = stacks; | |
16e4dd03 CW |
107 | } else { |
108 | stack = -1; | |
bd780f37 CW |
109 | } |
110 | ||
111 | spin_unlock_irqrestore(&rpm->debug.lock, flags); | |
16e4dd03 CW |
112 | |
113 | return stack; | |
114 | } | |
115 | ||
d5b6c275 | 116 | static void untrack_intel_runtime_pm_wakeref(struct i915_runtime_pm *rpm, |
4547c255 | 117 | depot_stack_handle_t stack) |
16e4dd03 | 118 | { |
16e4dd03 CW |
119 | unsigned long flags, n; |
120 | bool found = false; | |
121 | ||
122 | if (unlikely(stack == -1)) | |
123 | return; | |
124 | ||
125 | spin_lock_irqsave(&rpm->debug.lock, flags); | |
126 | for (n = rpm->debug.count; n--; ) { | |
127 | if (rpm->debug.owners[n] == stack) { | |
128 | memmove(rpm->debug.owners + n, | |
129 | rpm->debug.owners + n + 1, | |
130 | (--rpm->debug.count - n) * sizeof(stack)); | |
131 | found = true; | |
132 | break; | |
133 | } | |
134 | } | |
135 | spin_unlock_irqrestore(&rpm->debug.lock, flags); | |
136 | ||
137 | if (WARN(!found, | |
138 | "Unmatched wakeref (tracking %lu), count %u\n", | |
139 | rpm->debug.count, atomic_read(&rpm->wakeref_count))) { | |
140 | char *buf; | |
141 | ||
2e1e5c55 | 142 | buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN); |
16e4dd03 CW |
143 | if (!buf) |
144 | return; | |
145 | ||
146 | __print_depot_stack(stack, buf, PAGE_SIZE, 2); | |
147 | DRM_DEBUG_DRIVER("wakeref %x from\n%s", stack, buf); | |
148 | ||
149 | stack = READ_ONCE(rpm->debug.last_release); | |
150 | if (stack) { | |
151 | __print_depot_stack(stack, buf, PAGE_SIZE, 2); | |
152 | DRM_DEBUG_DRIVER("wakeref last released at\n%s", buf); | |
153 | } | |
154 | ||
155 | kfree(buf); | |
156 | } | |
bd780f37 CW |
157 | } |
158 | ||
159 | static int cmphandle(const void *_a, const void *_b) | |
160 | { | |
161 | const depot_stack_handle_t * const a = _a, * const b = _b; | |
162 | ||
163 | if (*a < *b) | |
164 | return -1; | |
165 | else if (*a > *b) | |
166 | return 1; | |
167 | else | |
168 | return 0; | |
169 | } | |
170 | ||
171 | static void | |
172 | __print_intel_runtime_pm_wakeref(struct drm_printer *p, | |
173 | const struct intel_runtime_pm_debug *dbg) | |
174 | { | |
175 | unsigned long i; | |
176 | char *buf; | |
177 | ||
2e1e5c55 | 178 | buf = kmalloc(PAGE_SIZE, GFP_NOWAIT | __GFP_NOWARN); |
bd780f37 CW |
179 | if (!buf) |
180 | return; | |
181 | ||
182 | if (dbg->last_acquire) { | |
183 | __print_depot_stack(dbg->last_acquire, buf, PAGE_SIZE, 2); | |
184 | drm_printf(p, "Wakeref last acquired:\n%s", buf); | |
185 | } | |
186 | ||
187 | if (dbg->last_release) { | |
188 | __print_depot_stack(dbg->last_release, buf, PAGE_SIZE, 2); | |
189 | drm_printf(p, "Wakeref last released:\n%s", buf); | |
190 | } | |
191 | ||
192 | drm_printf(p, "Wakeref count: %lu\n", dbg->count); | |
193 | ||
194 | sort(dbg->owners, dbg->count, sizeof(*dbg->owners), cmphandle, NULL); | |
195 | ||
196 | for (i = 0; i < dbg->count; i++) { | |
197 | depot_stack_handle_t stack = dbg->owners[i]; | |
198 | unsigned long rep; | |
199 | ||
200 | rep = 1; | |
201 | while (i + 1 < dbg->count && dbg->owners[i + 1] == stack) | |
202 | rep++, i++; | |
203 | __print_depot_stack(stack, buf, PAGE_SIZE, 2); | |
204 | drm_printf(p, "Wakeref x%lu taken at:\n%s", rep, buf); | |
205 | } | |
206 | ||
207 | kfree(buf); | |
208 | } | |
209 | ||
210 | static noinline void | |
dbf99c1f ID |
211 | __untrack_all_wakerefs(struct intel_runtime_pm_debug *debug, |
212 | struct intel_runtime_pm_debug *saved) | |
213 | { | |
214 | *saved = *debug; | |
215 | ||
216 | debug->owners = NULL; | |
217 | debug->count = 0; | |
218 | debug->last_release = __save_depot_stack(); | |
219 | } | |
220 | ||
221 | static void | |
222 | dump_and_free_wakeref_tracking(struct intel_runtime_pm_debug *debug) | |
223 | { | |
224 | struct drm_printer p; | |
225 | ||
226 | if (!debug->count) | |
227 | return; | |
228 | ||
229 | p = drm_debug_printer("i915"); | |
230 | __print_intel_runtime_pm_wakeref(&p, debug); | |
231 | ||
232 | kfree(debug->owners); | |
233 | } | |
234 | ||
bd780f37 | 235 | static noinline void |
d5b6c275 | 236 | __intel_wakeref_dec_and_check_tracking(struct i915_runtime_pm *rpm) |
bd780f37 | 237 | { |
bd780f37 | 238 | struct intel_runtime_pm_debug dbg = {}; |
bd780f37 CW |
239 | unsigned long flags; |
240 | ||
dbf99c1f ID |
241 | if (!atomic_dec_and_lock_irqsave(&rpm->wakeref_count, |
242 | &rpm->debug.lock, | |
243 | flags)) | |
244 | return; | |
bd780f37 | 245 | |
dbf99c1f ID |
246 | __untrack_all_wakerefs(&rpm->debug, &dbg); |
247 | spin_unlock_irqrestore(&rpm->debug.lock, flags); | |
bd780f37 | 248 | |
dbf99c1f ID |
249 | dump_and_free_wakeref_tracking(&dbg); |
250 | } | |
bd780f37 | 251 | |
dbf99c1f | 252 | static noinline void |
d5b6c275 | 253 | untrack_all_intel_runtime_pm_wakerefs(struct i915_runtime_pm *rpm) |
dbf99c1f | 254 | { |
dbf99c1f ID |
255 | struct intel_runtime_pm_debug dbg = {}; |
256 | unsigned long flags; | |
bd780f37 | 257 | |
dbf99c1f ID |
258 | spin_lock_irqsave(&rpm->debug.lock, flags); |
259 | __untrack_all_wakerefs(&rpm->debug, &dbg); | |
260 | spin_unlock_irqrestore(&rpm->debug.lock, flags); | |
261 | ||
262 | dump_and_free_wakeref_tracking(&dbg); | |
bd780f37 CW |
263 | } |
264 | ||
265 | void print_intel_runtime_pm_wakeref(struct drm_i915_private *i915, | |
266 | struct drm_printer *p) | |
267 | { | |
268 | struct intel_runtime_pm_debug dbg = {}; | |
269 | ||
270 | do { | |
271 | struct i915_runtime_pm *rpm = &i915->runtime_pm; | |
272 | unsigned long alloc = dbg.count; | |
273 | depot_stack_handle_t *s; | |
274 | ||
275 | spin_lock_irq(&rpm->debug.lock); | |
276 | dbg.count = rpm->debug.count; | |
277 | if (dbg.count <= alloc) { | |
278 | memcpy(dbg.owners, | |
279 | rpm->debug.owners, | |
280 | dbg.count * sizeof(*s)); | |
281 | } | |
282 | dbg.last_acquire = rpm->debug.last_acquire; | |
283 | dbg.last_release = rpm->debug.last_release; | |
284 | spin_unlock_irq(&rpm->debug.lock); | |
285 | if (dbg.count <= alloc) | |
286 | break; | |
287 | ||
2e1e5c55 CW |
288 | s = krealloc(dbg.owners, |
289 | dbg.count * sizeof(*s), | |
290 | GFP_NOWAIT | __GFP_NOWARN); | |
bd780f37 CW |
291 | if (!s) |
292 | goto out; | |
293 | ||
294 | dbg.owners = s; | |
295 | } while (1); | |
296 | ||
297 | __print_intel_runtime_pm_wakeref(p, &dbg); | |
298 | ||
299 | out: | |
300 | kfree(dbg.owners); | |
301 | } | |
302 | ||
303 | #else | |
304 | ||
d5b6c275 | 305 | static void init_intel_runtime_pm_wakeref(struct i915_runtime_pm *rpm) |
bd780f37 CW |
306 | { |
307 | } | |
308 | ||
16e4dd03 | 309 | static depot_stack_handle_t |
d5b6c275 | 310 | track_intel_runtime_pm_wakeref(struct i915_runtime_pm *rpm) |
bd780f37 | 311 | { |
16e4dd03 | 312 | return -1; |
bd780f37 CW |
313 | } |
314 | ||
d5b6c275 | 315 | static void untrack_intel_runtime_pm_wakeref(struct i915_runtime_pm *rpm, |
4547c255 ID |
316 | intel_wakeref_t wref) |
317 | { | |
318 | } | |
319 | ||
320 | static void | |
d5b6c275 | 321 | __intel_wakeref_dec_and_check_tracking(struct i915_runtime_pm *rpm) |
bd780f37 | 322 | { |
d5b6c275 | 323 | atomic_dec(&rpm->wakeref_count); |
bd780f37 CW |
324 | } |
325 | ||
dbf99c1f | 326 | static void |
d5b6c275 | 327 | untrack_all_intel_runtime_pm_wakerefs(struct i915_runtime_pm *rpm) |
dbf99c1f ID |
328 | { |
329 | } | |
330 | ||
bd780f37 CW |
331 | #endif |
332 | ||
4547c255 | 333 | static void |
d5b6c275 | 334 | intel_runtime_pm_acquire(struct i915_runtime_pm *rpm, bool wakelock) |
4547c255 | 335 | { |
4547c255 ID |
336 | if (wakelock) { |
337 | atomic_add(1 + INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count); | |
87b391b9 | 338 | assert_rpm_wakelock_held(rpm); |
4547c255 ID |
339 | } else { |
340 | atomic_inc(&rpm->wakeref_count); | |
87b391b9 | 341 | assert_rpm_raw_wakeref_held(rpm); |
4547c255 ID |
342 | } |
343 | } | |
344 | ||
345 | static void | |
d5b6c275 | 346 | intel_runtime_pm_release(struct i915_runtime_pm *rpm, int wakelock) |
4547c255 | 347 | { |
4547c255 | 348 | if (wakelock) { |
87b391b9 | 349 | assert_rpm_wakelock_held(rpm); |
4547c255 ID |
350 | atomic_sub(INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count); |
351 | } else { | |
87b391b9 | 352 | assert_rpm_raw_wakeref_held(rpm); |
4547c255 ID |
353 | } |
354 | ||
d5b6c275 | 355 | __intel_wakeref_dec_and_check_tracking(rpm); |
4547c255 ID |
356 | } |
357 | ||
d5b6c275 | 358 | static intel_wakeref_t __intel_runtime_pm_get(struct i915_runtime_pm *rpm, |
7645b19d | 359 | bool wakelock) |
dcddab3a | 360 | { |
7645b19d | 361 | int ret; |
dcddab3a | 362 | |
d5b6c275 | 363 | ret = pm_runtime_get_sync(rpm->kdev); |
7645b19d | 364 | WARN_ONCE(ret < 0, "pm_runtime_get_sync() failed: %d\n", ret); |
b409ca95 | 365 | |
d5b6c275 | 366 | intel_runtime_pm_acquire(rpm, wakelock); |
b409ca95 | 367 | |
d5b6c275 | 368 | return track_intel_runtime_pm_wakeref(rpm); |
b409ca95 ID |
369 | } |
370 | ||
e4e7684f | 371 | /** |
7645b19d DCS |
372 | * intel_runtime_pm_get_raw - grab a raw runtime pm reference |
373 | * @i915: i915 device instance | |
e4e7684f DV |
374 | * |
375 | * This is the unlocked version of intel_display_power_is_enabled() and should | |
376 | * only be used from error capture and recovery code where deadlocks are | |
377 | * possible. | |
7645b19d DCS |
378 | * This function grabs a device-level runtime pm reference (mostly used for |
379 | * asynchronous PM management from display code) and ensures that it is powered | |
380 | * up. Raw references are not considered during wakelock assert checks. | |
e4e7684f | 381 | * |
7645b19d DCS |
382 | * Any runtime pm reference obtained by this function must have a symmetric |
383 | * call to intel_runtime_pm_put_raw() to release the reference again. | |
384 | * | |
385 | * Returns: the wakeref cookie to pass to intel_runtime_pm_put_raw(), evaluates | |
386 | * as True if the wakeref was acquired, or False otherwise. | |
e4e7684f | 387 | */ |
9c065a7d | 388 | |
7645b19d DCS |
389 | intel_wakeref_t intel_runtime_pm_get_raw(struct drm_i915_private *i915) |
390 | { | |
d5b6c275 | 391 | return __intel_runtime_pm_get(&i915->runtime_pm, false); |
9c065a7d DV |
392 | } |
393 | ||
e4e7684f | 394 | /** |
7645b19d DCS |
395 | * intel_runtime_pm_get - grab a runtime pm reference |
396 | * @i915: i915 device instance | |
e4e7684f | 397 | * |
7645b19d DCS |
398 | * This function grabs a device-level runtime pm reference (mostly used for GEM |
399 | * code to ensure the GTT or GT is on) and ensures that it is powered up. | |
e4e7684f | 400 | * |
7645b19d DCS |
401 | * Any runtime pm reference obtained by this function must have a symmetric |
402 | * call to intel_runtime_pm_put() to release the reference again. | |
e4e7684f | 403 | * |
7645b19d | 404 | * Returns: the wakeref cookie to pass to intel_runtime_pm_put() |
e4e7684f | 405 | */ |
7645b19d | 406 | intel_wakeref_t intel_runtime_pm_get(struct drm_i915_private *i915) |
9c065a7d | 407 | { |
d5b6c275 | 408 | return __intel_runtime_pm_get(&i915->runtime_pm, true); |
9c065a7d DV |
409 | } |
410 | ||
7645b19d DCS |
411 | /** |
412 | * intel_runtime_pm_get_if_in_use - grab a runtime pm reference if device in use | |
413 | * @i915: i915 device instance | |
414 | * | |
415 | * This function grabs a device-level runtime pm reference if the device is | |
416 | * already in use and ensures that it is powered up. It is illegal to try | |
417 | * and access the HW should intel_runtime_pm_get_if_in_use() report failure. | |
418 | * | |
419 | * Any runtime pm reference obtained by this function must have a symmetric | |
420 | * call to intel_runtime_pm_put() to release the reference again. | |
421 | * | |
422 | * Returns: the wakeref cookie to pass to intel_runtime_pm_put(), evaluates | |
423 | * as True if the wakeref was acquired, or False otherwise. | |
9c065a7d | 424 | */ |
7645b19d | 425 | intel_wakeref_t intel_runtime_pm_get_if_in_use(struct drm_i915_private *i915) |
9c065a7d | 426 | { |
d5b6c275 | 427 | struct i915_runtime_pm *rpm = &i915->runtime_pm; |
1af474fe | 428 | |
d5b6c275 | 429 | if (IS_ENABLED(CONFIG_PM)) { |
b2891eb2 | 430 | /* |
7645b19d DCS |
431 | * In cases runtime PM is disabled by the RPM core and we get |
432 | * an -EINVAL return value we are not supposed to call this | |
433 | * function, since the power state is undefined. This applies | |
434 | * atm to the late/early system suspend/resume handlers. | |
b2891eb2 | 435 | */ |
d5b6c275 | 436 | if (pm_runtime_get_if_in_use(rpm->kdev) <= 0) |
7645b19d | 437 | return 0; |
ffd7e32d | 438 | } |
664326f8 | 439 | |
d5b6c275 | 440 | intel_runtime_pm_acquire(rpm, true); |
da2f41d1 | 441 | |
d5b6c275 | 442 | return track_intel_runtime_pm_wakeref(rpm); |
da2f41d1 ID |
443 | } |
444 | ||
13e1592f | 445 | /** |
7645b19d DCS |
446 | * intel_runtime_pm_get_noresume - grab a runtime pm reference |
447 | * @i915: i915 device instance | |
448 | * | |
449 | * This function grabs a device-level runtime pm reference (mostly used for GEM | |
450 | * code to ensure the GTT or GT is on). | |
451 | * | |
452 | * It will _not_ power up the device but instead only check that it's powered | |
453 | * on. Therefore it is only valid to call this functions from contexts where | |
454 | * the device is known to be powered up and where trying to power it up would | |
455 | * result in hilarity and deadlocks. That pretty much means only the system | |
456 | * suspend/resume code where this is used to grab runtime pm references for | |
457 | * delayed setup down in work items. | |
13e1592f | 458 | * |
7645b19d DCS |
459 | * Any runtime pm reference obtained by this function must have a symmetric |
460 | * call to intel_runtime_pm_put() to release the reference again. | |
13e1592f | 461 | * |
7645b19d | 462 | * Returns: the wakeref cookie to pass to intel_runtime_pm_put() |
13e1592f | 463 | */ |
7645b19d | 464 | intel_wakeref_t intel_runtime_pm_get_noresume(struct drm_i915_private *i915) |
dc174300 | 465 | { |
d5b6c275 | 466 | struct i915_runtime_pm *rpm = &i915->runtime_pm; |
5aefb239 | 467 | |
87b391b9 | 468 | assert_rpm_wakelock_held(rpm); |
d5b6c275 | 469 | pm_runtime_get_noresume(rpm->kdev); |
5aefb239 | 470 | |
d5b6c275 | 471 | intel_runtime_pm_acquire(rpm, true); |
5aefb239 | 472 | |
d5b6c275 | 473 | return track_intel_runtime_pm_wakeref(rpm); |
5aefb239 SS |
474 | } |
475 | ||
d5b6c275 | 476 | static void __intel_runtime_pm_put(struct i915_runtime_pm *rpm, |
7645b19d DCS |
477 | intel_wakeref_t wref, |
478 | bool wakelock) | |
5aefb239 | 479 | { |
d5b6c275 | 480 | struct device *kdev = rpm->kdev; |
53421c2f | 481 | |
d5b6c275 | 482 | untrack_intel_runtime_pm_wakeref(rpm, wref); |
dc174300 | 483 | |
d5b6c275 | 484 | intel_runtime_pm_release(rpm, wakelock); |
93c7cb6c | 485 | |
7645b19d DCS |
486 | pm_runtime_mark_last_busy(kdev); |
487 | pm_runtime_put_autosuspend(kdev); | |
93c7cb6c SS |
488 | } |
489 | ||
7645b19d DCS |
490 | /** |
491 | * intel_runtime_pm_put_raw - release a raw runtime pm reference | |
492 | * @i915: i915 device instance | |
493 | * @wref: wakeref acquired for the reference that is being released | |
494 | * | |
495 | * This function drops the device-level runtime pm reference obtained by | |
496 | * intel_runtime_pm_get_raw() and might power down the corresponding | |
497 | * hardware block right away if this is the last reference. | |
498 | */ | |
499 | void | |
500 | intel_runtime_pm_put_raw(struct drm_i915_private *i915, intel_wakeref_t wref) | |
93c7cb6c | 501 | { |
d5b6c275 | 502 | __intel_runtime_pm_put(&i915->runtime_pm, wref, false); |
f75a1985 SS |
503 | } |
504 | ||
e4e7684f | 505 | /** |
4547c255 | 506 | * intel_runtime_pm_put_unchecked - release an unchecked runtime pm reference |
bd780f37 | 507 | * @i915: i915 device instance |
e4e7684f DV |
508 | * |
509 | * This function drops the device-level runtime pm reference obtained by | |
510 | * intel_runtime_pm_get() and might power down the corresponding | |
511 | * hardware block right away if this is the last reference. | |
4547c255 ID |
512 | * |
513 | * This function exists only for historical reasons and should be avoided in | |
514 | * new code, as the correctness of its use cannot be checked. Always use | |
515 | * intel_runtime_pm_put() instead. | |
e4e7684f | 516 | */ |
16e4dd03 | 517 | void intel_runtime_pm_put_unchecked(struct drm_i915_private *i915) |
9c065a7d | 518 | { |
d5b6c275 | 519 | __intel_runtime_pm_put(&i915->runtime_pm, -1, true); |
9c065a7d DV |
520 | } |
521 | ||
16e4dd03 | 522 | #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM) |
4547c255 ID |
523 | /** |
524 | * intel_runtime_pm_put - release a runtime pm reference | |
525 | * @i915: i915 device instance | |
526 | * @wref: wakeref acquired for the reference that is being released | |
527 | * | |
528 | * This function drops the device-level runtime pm reference obtained by | |
529 | * intel_runtime_pm_get() and might power down the corresponding | |
530 | * hardware block right away if this is the last reference. | |
531 | */ | |
16e4dd03 CW |
532 | void intel_runtime_pm_put(struct drm_i915_private *i915, intel_wakeref_t wref) |
533 | { | |
d5b6c275 | 534 | __intel_runtime_pm_put(&i915->runtime_pm, wref, true); |
16e4dd03 CW |
535 | } |
536 | #endif | |
537 | ||
e4e7684f DV |
538 | /** |
539 | * intel_runtime_pm_enable - enable runtime pm | |
bd780f37 | 540 | * @i915: i915 device instance |
e4e7684f DV |
541 | * |
542 | * This function enables runtime pm at the end of the driver load sequence. | |
543 | * | |
544 | * Note that this function does currently not enable runtime pm for the | |
2cd9a689 ID |
545 | * subordinate display power domains. That is done by |
546 | * intel_power_domains_enable(). | |
e4e7684f | 547 | */ |
bd780f37 | 548 | void intel_runtime_pm_enable(struct drm_i915_private *i915) |
9c065a7d | 549 | { |
d5b6c275 DCS |
550 | struct i915_runtime_pm *rpm = &i915->runtime_pm; |
551 | struct device *kdev = rpm->kdev; | |
9c065a7d | 552 | |
07d80572 CW |
553 | /* |
554 | * Disable the system suspend direct complete optimization, which can | |
555 | * leave the device suspended skipping the driver's suspend handlers | |
556 | * if the device was already runtime suspended. This is needed due to | |
557 | * the difference in our runtime and system suspend sequence and | |
558 | * becaue the HDA driver may require us to enable the audio power | |
559 | * domain during system suspend. | |
560 | */ | |
561 | dev_pm_set_driver_flags(kdev, DPM_FLAG_NEVER_SKIP); | |
562 | ||
c49d13ee DW |
563 | pm_runtime_set_autosuspend_delay(kdev, 10000); /* 10s */ |
564 | pm_runtime_mark_last_busy(kdev); | |
cbc68dc9 | 565 | |
25b181b4 ID |
566 | /* |
567 | * Take a permanent reference to disable the RPM functionality and drop | |
568 | * it only when unloading the driver. Use the low level get/put helpers, | |
569 | * so the driver's own RPM reference tracking asserts also work on | |
570 | * platforms without RPM support. | |
571 | */ | |
d5b6c275 | 572 | if (!rpm->available) { |
f5073824 ID |
573 | int ret; |
574 | ||
c49d13ee | 575 | pm_runtime_dont_use_autosuspend(kdev); |
f5073824 ID |
576 | ret = pm_runtime_get_sync(kdev); |
577 | WARN(ret < 0, "pm_runtime_get_sync() failed: %d\n", ret); | |
cbc68dc9 | 578 | } else { |
c49d13ee | 579 | pm_runtime_use_autosuspend(kdev); |
cbc68dc9 | 580 | } |
9c065a7d | 581 | |
aabee1bb ID |
582 | /* |
583 | * The core calls the driver load handler with an RPM reference held. | |
584 | * We drop that here and will reacquire it during unloading in | |
585 | * intel_power_domains_fini(). | |
586 | */ | |
c49d13ee | 587 | pm_runtime_put_autosuspend(kdev); |
9c065a7d | 588 | } |
07d80572 | 589 | |
bd780f37 | 590 | void intel_runtime_pm_disable(struct drm_i915_private *i915) |
07d80572 | 591 | { |
d5b6c275 DCS |
592 | struct i915_runtime_pm *rpm = &i915->runtime_pm; |
593 | struct device *kdev = rpm->kdev; | |
07d80572 CW |
594 | |
595 | /* Transfer rpm ownership back to core */ | |
bd780f37 | 596 | WARN(pm_runtime_get_sync(kdev) < 0, |
07d80572 CW |
597 | "Failed to pass rpm ownership back to core\n"); |
598 | ||
599 | pm_runtime_dont_use_autosuspend(kdev); | |
600 | ||
d5b6c275 | 601 | if (!rpm->available) |
07d80572 CW |
602 | pm_runtime_put(kdev); |
603 | } | |
bd780f37 CW |
604 | |
605 | void intel_runtime_pm_cleanup(struct drm_i915_private *i915) | |
606 | { | |
607 | struct i915_runtime_pm *rpm = &i915->runtime_pm; | |
dbf99c1f | 608 | int count = atomic_read(&rpm->wakeref_count); |
bd780f37 | 609 | |
bd780f37 | 610 | WARN(count, |
4547c255 ID |
611 | "i915 raw-wakerefs=%d wakelocks=%d on cleanup\n", |
612 | intel_rpm_raw_wakeref_count(count), | |
613 | intel_rpm_wakelock_count(count)); | |
bd780f37 | 614 | |
d5b6c275 | 615 | untrack_all_intel_runtime_pm_wakerefs(rpm); |
bd780f37 CW |
616 | } |
617 | ||
618 | void intel_runtime_pm_init_early(struct drm_i915_private *i915) | |
619 | { | |
d5b6c275 DCS |
620 | struct i915_runtime_pm *rpm = &i915->runtime_pm; |
621 | struct pci_dev *pdev = i915->drm.pdev; | |
622 | struct device *kdev = &pdev->dev; | |
623 | ||
624 | rpm->kdev = kdev; | |
625 | rpm->available = HAS_RUNTIME_PM(i915); | |
626 | ||
627 | init_intel_runtime_pm_wakeref(rpm); | |
bd780f37 | 628 | } |