2 * Copyright © 2012-2014 Intel Corporation
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:
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
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
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
29 #include <linux/pm_runtime.h>
31 #include <drm/drm_print.h>
34 #include "i915_trace.h"
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.
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.
53 static struct drm_i915_private *rpm_to_i915(struct intel_runtime_pm *rpm)
55 return container_of(rpm, struct drm_i915_private, runtime_pm);
58 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
60 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
62 ref_tracker_dir_init(&rpm->debug, INTEL_REFTRACK_DEAD_COUNT, dev_name(rpm->kdev));
65 static intel_wakeref_t
66 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
68 if (!rpm->available || rpm->no_wakeref_tracking)
71 return intel_ref_tracker_alloc(&rpm->debug);
74 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
75 intel_wakeref_t wakeref)
77 if (!rpm->available || rpm->no_wakeref_tracking)
80 intel_ref_tracker_free(&rpm->debug, wakeref);
83 static void untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
85 ref_tracker_dir_exit(&rpm->debug);
89 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
93 if (!atomic_dec_and_lock_irqsave(&rpm->wakeref_count,
98 ref_tracker_dir_print_locked(&rpm->debug, INTEL_REFTRACK_PRINT_LIMIT);
99 spin_unlock_irqrestore(&rpm->debug.lock, flags);
102 void print_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
103 struct drm_printer *p)
105 intel_ref_tracker_show(&rpm->debug, p);
110 static void init_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
114 static intel_wakeref_t
115 track_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm)
120 static void untrack_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
121 intel_wakeref_t wakeref)
126 __intel_wakeref_dec_and_check_tracking(struct intel_runtime_pm *rpm)
128 atomic_dec(&rpm->wakeref_count);
132 untrack_all_intel_runtime_pm_wakerefs(struct intel_runtime_pm *rpm)
139 intel_runtime_pm_acquire(struct intel_runtime_pm *rpm, bool wakelock)
142 atomic_add(1 + INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
143 assert_rpm_wakelock_held(rpm);
145 atomic_inc(&rpm->wakeref_count);
146 assert_rpm_raw_wakeref_held(rpm);
151 intel_runtime_pm_release(struct intel_runtime_pm *rpm, int wakelock)
154 assert_rpm_wakelock_held(rpm);
155 atomic_sub(INTEL_RPM_WAKELOCK_BIAS, &rpm->wakeref_count);
157 assert_rpm_raw_wakeref_held(rpm);
160 __intel_wakeref_dec_and_check_tracking(rpm);
163 static intel_wakeref_t __intel_runtime_pm_get(struct intel_runtime_pm *rpm,
166 struct drm_i915_private *i915 = rpm_to_i915(rpm);
169 ret = pm_runtime_get_sync(rpm->kdev);
170 drm_WARN_ONCE(&i915->drm, ret < 0,
171 "pm_runtime_get_sync() failed: %d\n", ret);
173 intel_runtime_pm_acquire(rpm, wakelock);
175 return track_intel_runtime_pm_wakeref(rpm);
179 * intel_runtime_pm_get_raw - grab a raw runtime pm reference
180 * @rpm: the intel_runtime_pm structure
182 * This is the unlocked version of intel_display_power_is_enabled() and should
183 * only be used from error capture and recovery code where deadlocks are
185 * This function grabs a device-level runtime pm reference (mostly used for
186 * asynchronous PM management from display code) and ensures that it is powered
187 * up. Raw references are not considered during wakelock assert checks.
189 * Any runtime pm reference obtained by this function must have a symmetric
190 * call to intel_runtime_pm_put_raw() to release the reference again.
192 * Returns: the wakeref cookie to pass to intel_runtime_pm_put_raw(), evaluates
193 * as True if the wakeref was acquired, or False otherwise.
195 intel_wakeref_t intel_runtime_pm_get_raw(struct intel_runtime_pm *rpm)
197 return __intel_runtime_pm_get(rpm, false);
201 * intel_runtime_pm_get - grab a runtime pm reference
202 * @rpm: the intel_runtime_pm structure
204 * This function grabs a device-level runtime pm reference (mostly used for GEM
205 * code to ensure the GTT or GT is on) and ensures that it is powered up.
207 * Any runtime pm reference obtained by this function must have a symmetric
208 * call to intel_runtime_pm_put() to release the reference again.
210 * Returns: the wakeref cookie to pass to intel_runtime_pm_put()
212 intel_wakeref_t intel_runtime_pm_get(struct intel_runtime_pm *rpm)
214 return __intel_runtime_pm_get(rpm, true);
218 * __intel_runtime_pm_get_if_active - grab a runtime pm reference if device is active
219 * @rpm: the intel_runtime_pm structure
220 * @ignore_usecount: get a ref even if dev->power.usage_count is 0
222 * This function grabs a device-level runtime pm reference if the device is
223 * already active and ensures that it is powered up. It is illegal to try
224 * and access the HW should intel_runtime_pm_get_if_active() report failure.
226 * If @ignore_usecount is true, a reference will be acquired even if there is no
227 * user requiring the device to be powered up (dev->power.usage_count == 0).
228 * If the function returns false in this case then it's guaranteed that the
229 * device's runtime suspend hook has been called already or that it will be
230 * called (and hence it's also guaranteed that the device's runtime resume
231 * hook will be called eventually).
233 * Any runtime pm reference obtained by this function must have a symmetric
234 * call to intel_runtime_pm_put() to release the reference again.
236 * Returns: the wakeref cookie to pass to intel_runtime_pm_put(), evaluates
237 * as True if the wakeref was acquired, or False otherwise.
239 static intel_wakeref_t __intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm,
240 bool ignore_usecount)
242 if (IS_ENABLED(CONFIG_PM)) {
244 * In cases runtime PM is disabled by the RPM core and we get
245 * an -EINVAL return value we are not supposed to call this
246 * function, since the power state is undefined. This applies
247 * atm to the late/early system suspend/resume handlers.
249 if ((ignore_usecount &&
250 pm_runtime_get_if_active(rpm->kdev) <= 0) ||
252 pm_runtime_get_if_in_use(rpm->kdev) <= 0))
256 intel_runtime_pm_acquire(rpm, true);
258 return track_intel_runtime_pm_wakeref(rpm);
261 intel_wakeref_t intel_runtime_pm_get_if_in_use(struct intel_runtime_pm *rpm)
263 return __intel_runtime_pm_get_if_active(rpm, false);
266 intel_wakeref_t intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm)
268 return __intel_runtime_pm_get_if_active(rpm, true);
272 * intel_runtime_pm_get_noresume - grab a runtime pm reference
273 * @rpm: the intel_runtime_pm structure
275 * This function grabs a device-level runtime pm reference.
277 * It will _not_ resume the device but instead only get an extra wakeref.
278 * Therefore it is only valid to call this functions from contexts where
279 * the device is known to be active and with another wakeref previously hold.
281 * Any runtime pm reference obtained by this function must have a symmetric
282 * call to intel_runtime_pm_put() to release the reference again.
284 * Returns: the wakeref cookie to pass to intel_runtime_pm_put()
286 intel_wakeref_t intel_runtime_pm_get_noresume(struct intel_runtime_pm *rpm)
288 assert_rpm_raw_wakeref_held(rpm);
289 pm_runtime_get_noresume(rpm->kdev);
291 intel_runtime_pm_acquire(rpm, true);
293 return track_intel_runtime_pm_wakeref(rpm);
296 static void __intel_runtime_pm_put(struct intel_runtime_pm *rpm,
297 intel_wakeref_t wref,
300 struct device *kdev = rpm->kdev;
302 untrack_intel_runtime_pm_wakeref(rpm, wref);
304 intel_runtime_pm_release(rpm, wakelock);
306 pm_runtime_mark_last_busy(kdev);
307 pm_runtime_put_autosuspend(kdev);
311 * intel_runtime_pm_put_raw - release a raw runtime pm reference
312 * @rpm: the intel_runtime_pm structure
313 * @wref: wakeref acquired for the reference that is being released
315 * This function drops the device-level runtime pm reference obtained by
316 * intel_runtime_pm_get_raw() and might power down the corresponding
317 * hardware block right away if this is the last reference.
320 intel_runtime_pm_put_raw(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
322 __intel_runtime_pm_put(rpm, wref, false);
326 * intel_runtime_pm_put_unchecked - release an unchecked runtime pm reference
327 * @rpm: the intel_runtime_pm structure
329 * This function drops the device-level runtime pm reference obtained by
330 * intel_runtime_pm_get() and might power down the corresponding
331 * hardware block right away if this is the last reference.
333 * This function exists only for historical reasons and should be avoided in
334 * new code, as the correctness of its use cannot be checked. Always use
335 * intel_runtime_pm_put() instead.
337 void intel_runtime_pm_put_unchecked(struct intel_runtime_pm *rpm)
339 __intel_runtime_pm_put(rpm, -1, true);
342 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
344 * intel_runtime_pm_put - release a runtime pm reference
345 * @rpm: the intel_runtime_pm structure
346 * @wref: wakeref acquired for the reference that is being released
348 * This function drops the device-level runtime pm reference obtained by
349 * intel_runtime_pm_get() and might power down the corresponding
350 * hardware block right away if this is the last reference.
352 void intel_runtime_pm_put(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
354 __intel_runtime_pm_put(rpm, wref, true);
359 * intel_runtime_pm_enable - enable runtime pm
360 * @rpm: the intel_runtime_pm structure
362 * This function enables runtime pm at the end of the driver load sequence.
364 * Note that this function does currently not enable runtime pm for the
365 * subordinate display power domains. That is done by
366 * intel_power_domains_enable().
368 void intel_runtime_pm_enable(struct intel_runtime_pm *rpm)
370 struct drm_i915_private *i915 = rpm_to_i915(rpm);
371 struct device *kdev = rpm->kdev;
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.
381 dev_pm_set_driver_flags(kdev, DPM_FLAG_NO_DIRECT_COMPLETE);
383 pm_runtime_set_autosuspend_delay(kdev, 10000); /* 10s */
384 pm_runtime_mark_last_busy(kdev);
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.
392 if (!rpm->available) {
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);
400 pm_runtime_use_autosuspend(kdev);
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.
411 pm_runtime_allow(kdev);
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().
418 pm_runtime_put_autosuspend(kdev);
421 void intel_runtime_pm_disable(struct intel_runtime_pm *rpm)
423 struct drm_i915_private *i915 = rpm_to_i915(rpm);
424 struct device *kdev = rpm->kdev;
426 /* Transfer rpm ownership back to core */
427 drm_WARN(&i915->drm, pm_runtime_get_sync(kdev) < 0,
428 "Failed to pass rpm ownership back to core\n");
430 pm_runtime_dont_use_autosuspend(kdev);
433 pm_runtime_put(kdev);
436 void intel_runtime_pm_driver_release(struct intel_runtime_pm *rpm)
438 struct drm_i915_private *i915 = rpm_to_i915(rpm);
439 int count = atomic_read(&rpm->wakeref_count);
441 intel_wakeref_auto_fini(&rpm->userfault_wakeref);
443 drm_WARN(&i915->drm, count,
444 "i915 raw-wakerefs=%d wakelocks=%d on cleanup\n",
445 intel_rpm_raw_wakeref_count(count),
446 intel_rpm_wakelock_count(count));
449 void intel_runtime_pm_driver_last_release(struct intel_runtime_pm *rpm)
451 intel_runtime_pm_driver_release(rpm);
452 untrack_all_intel_runtime_pm_wakerefs(rpm);
455 void intel_runtime_pm_init_early(struct intel_runtime_pm *rpm)
457 struct drm_i915_private *i915 = rpm_to_i915(rpm);
458 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
459 struct device *kdev = &pdev->dev;
462 rpm->available = HAS_RUNTIME_PM(i915);
463 atomic_set(&rpm->wakeref_count, 0);
465 init_intel_runtime_pm_wakeref(rpm);
466 INIT_LIST_HEAD(&rpm->lmem_userfault_list);
467 spin_lock_init(&rpm->lmem_userfault_lock);
468 intel_wakeref_auto_init(&rpm->userfault_wakeref, i915);