intel_runtime_pm.c 17 KB

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