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