intel_runtime_pm.c 15 KB

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