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 <[email protected]>
25  *    Daniel Vetter <[email protected]>
26  *
27  */
28 
29 #include <linux/pm_runtime.h>
30 
31 #include <drm/drm_print.h>
32 
33 #include "i915_drv.h"
34 #include "i915_trace.h"
35 
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 
53 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
54 
55 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
56 {
57 	ref_tracker_dir_init(&rpm->debug, INTEL_REFTRACK_DEAD_COUNT, dev_name(rpm->kdev));
58 }
59 
60 static intel_wakeref_t
61 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
62 {
63 	if (!rpm->available || rpm->no_wakeref_tracking)
64 		return -1;
65 
66 	return intel_ref_tracker_alloc(&rpm->debug);
67 }
68 
69 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
70 					     intel_wakeref_t wakeref)
71 {
72 	if (!rpm->available || rpm->no_wakeref_tracking)
73 		return;
74 
75 	intel_ref_tracker_free(&rpm->debug, wakeref);
76 }
77 
78 static void untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
79 {
80 	ref_tracker_dir_exit(&rpm->debug);
81 }
82 
83 static noinline void
84 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
85 {
86 	unsigned long flags;
87 
88 	if (!atomic_dec_and_lock_irqsave(&rpm->wakeref_count,
89 					 &rpm->debug.lock,
90 					 flags))
91 		return;
92 
93 	ref_tracker_dir_print_locked(&rpm->debug, INTEL_REFTRACK_PRINT_LIMIT);
94 	spin_unlock_irqrestore(&rpm->debug.lock, flags);
95 }
96 
97 void print_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
98 				    struct drm_printer *p)
99 {
100 	intel_ref_tracker_show(&rpm->debug, p);
101 }
102 
103 #else
104 
105 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
106 {
107 }
108 
109 static intel_wakeref_t
110 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
111 {
112 	return -1;
113 }
114 
115 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
116 					     intel_wakeref_t wakeref)
117 {
118 }
119 
120 static void
121 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
122 {
123 	atomic_dec(&rpm->wakeref_count);
124 }
125 
126 static void
127 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
128 {
129 }
130 
131 #endif
132 
133 static void
134 intel_runtime_pm_acquire(struct intel_runtime_pm *rpm, bool wakelock)
135 {
136 	if (wakelock) {
137 		atomic_add(1 + INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
138 		assert_rpm_wakelock_held(rpm);
139 	} else {
140 		atomic_inc(&rpm->wakeref_count);
141 		assert_rpm_raw_wakeref_held(rpm);
142 	}
143 }
144 
145 static void
146 intel_runtime_pm_release(struct intel_runtime_pm *rpm, int wakelock)
147 {
148 	if (wakelock) {
149 		assert_rpm_wakelock_held(rpm);
150 		atomic_sub(INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
151 	} else {
152 		assert_rpm_raw_wakeref_held(rpm);
153 	}
154 
155 	__intel_wakeref_dec_and_check_tracking(rpm);
156 }
157 
158 static intel_wakeref_t __intel_runtime_pm_get(struct intel_runtime_pm *rpm,
159 					      bool wakelock)
160 {
161 	struct drm_i915_private *i915 = container_of(rpm,
162 						     struct drm_i915_private,
163 						     runtime_pm);
164 	int ret;
165 
166 	ret = pm_runtime_get_sync(rpm->kdev);
167 	drm_WARN_ONCE(&i915->drm, ret < 0,
168 		      "pm_runtime_get_sync() failed: %d\n", ret);
169 
170 	intel_runtime_pm_acquire(rpm, wakelock);
171 
172 	return track_intel_runtime_pm_wakeref(rpm);
173 }
174 
175 /**
176  * intel_runtime_pm_get_raw - grab a raw runtime pm reference
177  * @rpm: the intel_runtime_pm structure
178  *
179  * This is the unlocked version of intel_display_power_is_enabled() and should
180  * only be used from error capture and recovery code where deadlocks are
181  * possible.
182  * This function grabs a device-level runtime pm reference (mostly used for
183  * asynchronous PM management from display code) and ensures that it is powered
184  * up. Raw references are not considered during wakelock assert checks.
185  *
186  * Any runtime pm reference obtained by this function must have a symmetric
187  * call to intel_runtime_pm_put_raw() to release the reference again.
188  *
189  * Returns: the wakeref cookie to pass to intel_runtime_pm_put_raw(), evaluates
190  * as True if the wakeref was acquired, or False otherwise.
191  */
192 intel_wakeref_t intel_runtime_pm_get_raw(struct intel_runtime_pm *rpm)
193 {
194 	return __intel_runtime_pm_get(rpm, false);
195 }
196 
197 /**
198  * intel_runtime_pm_get - grab a runtime pm reference
199  * @rpm: the intel_runtime_pm structure
200  *
201  * This function grabs a device-level runtime pm reference (mostly used for GEM
202  * code to ensure the GTT or GT is on) and ensures that it is powered up.
203  *
204  * Any runtime pm reference obtained by this function must have a symmetric
205  * call to intel_runtime_pm_put() to release the reference again.
206  *
207  * Returns: the wakeref cookie to pass to intel_runtime_pm_put()
208  */
209 intel_wakeref_t intel_runtime_pm_get(struct intel_runtime_pm *rpm)
210 {
211 	return __intel_runtime_pm_get(rpm, true);
212 }
213 
214 /**
215  * __intel_runtime_pm_get_if_active - grab a runtime pm reference if device is active
216  * @rpm: the intel_runtime_pm structure
217  * @ignore_usecount: get a ref even if dev->power.usage_count is 0
218  *
219  * This function grabs a device-level runtime pm reference if the device is
220  * already active and ensures that it is powered up. It is illegal to try
221  * and access the HW should intel_runtime_pm_get_if_active() report failure.
222  *
223  * If @ignore_usecount is true, a reference will be acquired even if there is no
224  * user requiring the device to be powered up (dev->power.usage_count == 0).
225  * If the function returns false in this case then it's guaranteed that the
226  * device's runtime suspend hook has been called already or that it will be
227  * called (and hence it's also guaranteed that the device's runtime resume
228  * hook will be called eventually).
229  *
230  * Any runtime pm reference obtained by this function must have a symmetric
231  * call to intel_runtime_pm_put() to release the reference again.
232  *
233  * Returns: the wakeref cookie to pass to intel_runtime_pm_put(), evaluates
234  * as True if the wakeref was acquired, or False otherwise.
235  */
236 static intel_wakeref_t __intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm,
237 							bool ignore_usecount)
238 {
239 	if (IS_ENABLED(CONFIG_PM)) {
240 		/*
241 		 * In cases runtime PM is disabled by the RPM core and we get
242 		 * an -EINVAL return value we are not supposed to call this
243 		 * function, since the power state is undefined. This applies
244 		 * atm to the late/early system suspend/resume handlers.
245 		 */
246 		if (pm_runtime_get_if_active(rpm->kdev, ignore_usecount) <= 0)
247 			return 0;
248 	}
249 
250 	intel_runtime_pm_acquire(rpm, true);
251 
252 	return track_intel_runtime_pm_wakeref(rpm);
253 }
254 
255 intel_wakeref_t intel_runtime_pm_get_if_in_use(struct intel_runtime_pm *rpm)
256 {
257 	return __intel_runtime_pm_get_if_active(rpm, false);
258 }
259 
260 intel_wakeref_t intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm)
261 {
262 	return __intel_runtime_pm_get_if_active(rpm, true);
263 }
264 
265 /**
266  * intel_runtime_pm_get_noresume - grab a runtime pm reference
267  * @rpm: the intel_runtime_pm structure
268  *
269  * This function grabs a device-level runtime pm reference (mostly used for GEM
270  * code to ensure the GTT or GT is on).
271  *
272  * It will _not_ power up the device but instead only check that it's powered
273  * on.  Therefore it is only valid to call this functions from contexts where
274  * the device is known to be powered up and where trying to power it up would
275  * result in hilarity and deadlocks. That pretty much means only the system
276  * suspend/resume code where this is used to grab runtime pm references for
277  * delayed setup down in work items.
278  *
279  * Any runtime pm reference obtained by this function must have a symmetric
280  * call to intel_runtime_pm_put() to release the reference again.
281  *
282  * Returns: the wakeref cookie to pass to intel_runtime_pm_put()
283  */
284 intel_wakeref_t intel_runtime_pm_get_noresume(struct intel_runtime_pm *rpm)
285 {
286 	assert_rpm_wakelock_held(rpm);
287 	pm_runtime_get_noresume(rpm->kdev);
288 
289 	intel_runtime_pm_acquire(rpm, true);
290 
291 	return track_intel_runtime_pm_wakeref(rpm);
292 }
293 
294 static void __intel_runtime_pm_put(struct intel_runtime_pm *rpm,
295 				   intel_wakeref_t wref,
296 				   bool wakelock)
297 {
298 	struct device *kdev = rpm->kdev;
299 
300 	untrack_intel_runtime_pm_wakeref(rpm, wref);
301 
302 	intel_runtime_pm_release(rpm, wakelock);
303 
304 	pm_runtime_mark_last_busy(kdev);
305 	pm_runtime_put_autosuspend(kdev);
306 }
307 
308 /**
309  * intel_runtime_pm_put_raw - release a raw runtime pm reference
310  * @rpm: the intel_runtime_pm structure
311  * @wref: wakeref acquired for the reference that is being released
312  *
313  * This function drops the device-level runtime pm reference obtained by
314  * intel_runtime_pm_get_raw() and might power down the corresponding
315  * hardware block right away if this is the last reference.
316  */
317 void
318 intel_runtime_pm_put_raw(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
319 {
320 	__intel_runtime_pm_put(rpm, wref, false);
321 }
322 
323 /**
324  * intel_runtime_pm_put_unchecked - release an unchecked runtime pm reference
325  * @rpm: the intel_runtime_pm structure
326  *
327  * This function drops the device-level runtime pm reference obtained by
328  * intel_runtime_pm_get() and might power down the corresponding
329  * hardware block right away if this is the last reference.
330  *
331  * This function exists only for historical reasons and should be avoided in
332  * new code, as the correctness of its use cannot be checked. Always use
333  * intel_runtime_pm_put() instead.
334  */
335 void intel_runtime_pm_put_unchecked(struct intel_runtime_pm *rpm)
336 {
337 	__intel_runtime_pm_put(rpm, -1, true);
338 }
339 
340 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
341 /**
342  * intel_runtime_pm_put - release a runtime pm reference
343  * @rpm: the intel_runtime_pm structure
344  * @wref: wakeref acquired for the reference that is being released
345  *
346  * This function drops the device-level runtime pm reference obtained by
347  * intel_runtime_pm_get() and might power down the corresponding
348  * hardware block right away if this is the last reference.
349  */
350 void intel_runtime_pm_put(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
351 {
352 	__intel_runtime_pm_put(rpm, wref, true);
353 }
354 #endif
355 
356 /**
357  * intel_runtime_pm_enable - enable runtime pm
358  * @rpm: the intel_runtime_pm structure
359  *
360  * This function enables runtime pm at the end of the driver load sequence.
361  *
362  * Note that this function does currently not enable runtime pm for the
363  * subordinate display power domains. That is done by
364  * intel_power_domains_enable().
365  */
366 void intel_runtime_pm_enable(struct intel_runtime_pm *rpm)
367 {
368 	struct drm_i915_private *i915 = container_of(rpm,
369 						     struct drm_i915_private,
370 						     runtime_pm);
371 	struct device *kdev = rpm->kdev;
372 
373 	/*
374 	 * Disable the system suspend direct complete optimization, which can
375 	 * leave the device suspended skipping the driver's suspend handlers
376 	 * if the device was already runtime suspended. This is needed due to
377 	 * the difference in our runtime and system suspend sequence and
378 	 * becaue the HDA driver may require us to enable the audio power
379 	 * domain during system suspend.
380 	 */
381 	dev_pm_set_driver_flags(kdev, DPM_FLAG_NO_DIRECT_COMPLETE);
382 
383 	pm_runtime_set_autosuspend_delay(kdev, 10000); /* 10s */
384 	pm_runtime_mark_last_busy(kdev);
385 
386 	/*
387 	 * Take a permanent reference to disable the RPM functionality and drop
388 	 * it only when unloading the driver. Use the low level get/put helpers,
389 	 * so the driver's own RPM reference tracking asserts also work on
390 	 * platforms without RPM support.
391 	 */
392 	if (!rpm->available) {
393 		int ret;
394 
395 		pm_runtime_dont_use_autosuspend(kdev);
396 		ret = pm_runtime_get_sync(kdev);
397 		drm_WARN(&i915->drm, ret < 0,
398 			 "pm_runtime_get_sync() failed: %d\n", ret);
399 	} else {
400 		pm_runtime_use_autosuspend(kdev);
401 	}
402 
403 	/*
404 	 *  FIXME: Temp hammer to keep autosupend disable on lmem supported platforms.
405 	 *  As per PCIe specs 5.3.1.4.1, all iomem read write request over a PCIe
406 	 *  function will be unsupported in case PCIe endpoint function is in D3.
407 	 *  Let's keep i915 autosuspend control 'on' till we fix all known issue
408 	 *  with lmem access in D3.
409 	 */
410 	if (!IS_DGFX(i915))
411 		pm_runtime_allow(kdev);
412 
413 	/*
414 	 * The core calls the driver load handler with an RPM reference held.
415 	 * We drop that here and will reacquire it during unloading in
416 	 * intel_power_domains_fini().
417 	 */
418 	pm_runtime_put_autosuspend(kdev);
419 }
420 
421 void intel_runtime_pm_disable(struct intel_runtime_pm *rpm)
422 {
423 	struct drm_i915_private *i915 = container_of(rpm,
424 						     struct drm_i915_private,
425 						     runtime_pm);
426 	struct device *kdev = rpm->kdev;
427 
428 	/* Transfer rpm ownership back to core */
429 	drm_WARN(&i915->drm, pm_runtime_get_sync(kdev) < 0,
430 		 "Failed to pass rpm ownership back to core\n");
431 
432 	pm_runtime_dont_use_autosuspend(kdev);
433 
434 	if (!rpm->available)
435 		pm_runtime_put(kdev);
436 }
437 
438 void intel_runtime_pm_driver_release(struct intel_runtime_pm *rpm)
439 {
440 	struct drm_i915_private *i915 = container_of(rpm,
441 						     struct drm_i915_private,
442 						     runtime_pm);
443 	int count = atomic_read(&rpm->wakeref_count);
444 
445 	intel_wakeref_auto_fini(&rpm->userfault_wakeref);
446 
447 	drm_WARN(&i915->drm, count,
448 		 "i915 raw-wakerefs=%d wakelocks=%d on cleanup\n",
449 		 intel_rpm_raw_wakeref_count(count),
450 		 intel_rpm_wakelock_count(count));
451 }
452 
453 void intel_runtime_pm_driver_last_release(struct intel_runtime_pm *rpm)
454 {
455 	intel_runtime_pm_driver_release(rpm);
456 	untrack_all_intel_runtime_pm_wakerefs(rpm);
457 }
458 
459 void intel_runtime_pm_init_early(struct intel_runtime_pm *rpm)
460 {
461 	struct drm_i915_private *i915 =
462 			container_of(rpm, struct drm_i915_private, runtime_pm);
463 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
464 	struct device *kdev = &pdev->dev;
465 
466 	rpm->kdev = kdev;
467 	rpm->available = HAS_RUNTIME_PM(i915);
468 	atomic_set(&rpm->wakeref_count, 0);
469 
470 	init_intel_runtime_pm_wakeref(rpm);
471 	INIT_LIST_HEAD(&rpm->lmem_userfault_list);
472 	spin_lock_init(&rpm->lmem_userfault_lock);
473 	intel_wakeref_auto_init(&rpm->userfault_wakeref, i915);
474 }
475