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