intel_psr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * Copyright © 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
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * DOC: Panel Self Refresh (PSR/SRD)
  25. *
  26. * Since Haswell Display controller supports Panel Self-Refresh on display
  27. * panels witch have a remote frame buffer (RFB) implemented according to PSR
  28. * spec in eDP1.3. PSR feature allows the display to go to lower standby states
  29. * when system is idle but display is on as it eliminates display refresh
  30. * request to DDR memory completely as long as the frame buffer for that
  31. * display is unchanged.
  32. *
  33. * Panel Self Refresh must be supported by both Hardware (source) and
  34. * Panel (sink).
  35. *
  36. * PSR saves power by caching the framebuffer in the panel RFB, which allows us
  37. * to power down the link and memory controller. For DSI panels the same idea
  38. * is called "manual mode".
  39. *
  40. * The implementation uses the hardware-based PSR support which automatically
  41. * enters/exits self-refresh mode. The hardware takes care of sending the
  42. * required DP aux message and could even retrain the link (that part isn't
  43. * enabled yet though). The hardware also keeps track of any frontbuffer
  44. * changes to know when to exit self-refresh mode again. Unfortunately that
  45. * part doesn't work too well, hence why the i915 PSR support uses the
  46. * software frontbuffer tracking to make sure it doesn't miss a screen
  47. * update. For this integration intel_psr_invalidate() and intel_psr_flush()
  48. * get called by the frontbuffer tracking code. Note that because of locking
  49. * issues the self-refresh re-enable code is done from a work queue, which
  50. * must be correctly synchronized/cancelled when shutting down the pipe."
  51. */
  52. #include <drm/drmP.h>
  53. #include "intel_drv.h"
  54. #include "i915_drv.h"
  55. static bool is_edp_psr(struct intel_dp *intel_dp)
  56. {
  57. return intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED;
  58. }
  59. static bool vlv_is_psr_active_on_pipe(struct drm_device *dev, int pipe)
  60. {
  61. struct drm_i915_private *dev_priv = dev->dev_private;
  62. uint32_t val;
  63. val = I915_READ(VLV_PSRSTAT(pipe)) &
  64. VLV_EDP_PSR_CURR_STATE_MASK;
  65. return (val == VLV_EDP_PSR_ACTIVE_NORFB_UP) ||
  66. (val == VLV_EDP_PSR_ACTIVE_SF_UPDATE);
  67. }
  68. static void intel_psr_write_vsc(struct intel_dp *intel_dp,
  69. struct edp_vsc_psr *vsc_psr)
  70. {
  71. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  72. struct drm_device *dev = dig_port->base.base.dev;
  73. struct drm_i915_private *dev_priv = dev->dev_private;
  74. struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
  75. u32 ctl_reg = HSW_TVIDEO_DIP_CTL(crtc->config->cpu_transcoder);
  76. u32 data_reg = HSW_TVIDEO_DIP_VSC_DATA(crtc->config->cpu_transcoder);
  77. uint32_t *data = (uint32_t *) vsc_psr;
  78. unsigned int i;
  79. /* As per BSPec (Pipe Video Data Island Packet), we need to disable
  80. the video DIP being updated before program video DIP data buffer
  81. registers for DIP being updated. */
  82. I915_WRITE(ctl_reg, 0);
  83. POSTING_READ(ctl_reg);
  84. for (i = 0; i < VIDEO_DIP_VSC_DATA_SIZE; i += 4) {
  85. if (i < sizeof(struct edp_vsc_psr))
  86. I915_WRITE(data_reg + i, *data++);
  87. else
  88. I915_WRITE(data_reg + i, 0);
  89. }
  90. I915_WRITE(ctl_reg, VIDEO_DIP_ENABLE_VSC_HSW);
  91. POSTING_READ(ctl_reg);
  92. }
  93. static void vlv_psr_setup_vsc(struct intel_dp *intel_dp)
  94. {
  95. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  96. struct drm_device *dev = intel_dig_port->base.base.dev;
  97. struct drm_i915_private *dev_priv = dev->dev_private;
  98. struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
  99. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  100. uint32_t val;
  101. /* VLV auto-generate VSC package as per EDP 1.3 spec, Table 3.10 */
  102. val = I915_READ(VLV_VSCSDP(pipe));
  103. val &= ~VLV_EDP_PSR_SDP_FREQ_MASK;
  104. val |= VLV_EDP_PSR_SDP_FREQ_EVFRAME;
  105. I915_WRITE(VLV_VSCSDP(pipe), val);
  106. }
  107. static void hsw_psr_setup_vsc(struct intel_dp *intel_dp)
  108. {
  109. struct edp_vsc_psr psr_vsc;
  110. /* Prepare VSC packet as per EDP 1.3 spec, Table 3.10 */
  111. memset(&psr_vsc, 0, sizeof(psr_vsc));
  112. psr_vsc.sdp_header.HB0 = 0;
  113. psr_vsc.sdp_header.HB1 = 0x7;
  114. psr_vsc.sdp_header.HB2 = 0x2;
  115. psr_vsc.sdp_header.HB3 = 0x8;
  116. intel_psr_write_vsc(intel_dp, &psr_vsc);
  117. }
  118. static void vlv_psr_enable_sink(struct intel_dp *intel_dp)
  119. {
  120. drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG,
  121. DP_PSR_ENABLE);
  122. }
  123. static void hsw_psr_enable_sink(struct intel_dp *intel_dp)
  124. {
  125. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  126. struct drm_device *dev = dig_port->base.base.dev;
  127. struct drm_i915_private *dev_priv = dev->dev_private;
  128. uint32_t aux_clock_divider;
  129. uint32_t aux_data_reg, aux_ctl_reg;
  130. int precharge = 0x3;
  131. static const uint8_t aux_msg[] = {
  132. [0] = DP_AUX_NATIVE_WRITE << 4,
  133. [1] = DP_SET_POWER >> 8,
  134. [2] = DP_SET_POWER & 0xff,
  135. [3] = 1 - 1,
  136. [4] = DP_SET_POWER_D0,
  137. };
  138. int i;
  139. BUILD_BUG_ON(sizeof(aux_msg) > 20);
  140. aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, 0);
  141. /* Enable PSR in sink */
  142. if (dev_priv->psr.link_standby)
  143. drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG,
  144. DP_PSR_ENABLE | DP_PSR_MAIN_LINK_ACTIVE);
  145. else
  146. drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG,
  147. DP_PSR_ENABLE & ~DP_PSR_MAIN_LINK_ACTIVE);
  148. aux_data_reg = (INTEL_INFO(dev)->gen >= 9) ?
  149. DPA_AUX_CH_DATA1 : EDP_PSR_AUX_DATA1(dev);
  150. aux_ctl_reg = (INTEL_INFO(dev)->gen >= 9) ?
  151. DPA_AUX_CH_CTL : EDP_PSR_AUX_CTL(dev);
  152. /* Setup AUX registers */
  153. for (i = 0; i < sizeof(aux_msg); i += 4)
  154. I915_WRITE(aux_data_reg + i,
  155. intel_dp_pack_aux(&aux_msg[i], sizeof(aux_msg) - i));
  156. if (INTEL_INFO(dev)->gen >= 9) {
  157. uint32_t val;
  158. val = I915_READ(aux_ctl_reg);
  159. val &= ~DP_AUX_CH_CTL_TIME_OUT_MASK;
  160. val |= DP_AUX_CH_CTL_TIME_OUT_1600us;
  161. val &= ~DP_AUX_CH_CTL_MESSAGE_SIZE_MASK;
  162. val |= (sizeof(aux_msg) << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  163. /* Use hardcoded data values for PSR */
  164. val &= ~DP_AUX_CH_CTL_PSR_DATA_AUX_REG_SKL;
  165. I915_WRITE(aux_ctl_reg, val);
  166. } else {
  167. I915_WRITE(aux_ctl_reg,
  168. DP_AUX_CH_CTL_TIME_OUT_400us |
  169. (sizeof(aux_msg) << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  170. (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
  171. (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT));
  172. }
  173. }
  174. static void vlv_psr_enable_source(struct intel_dp *intel_dp)
  175. {
  176. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  177. struct drm_device *dev = dig_port->base.base.dev;
  178. struct drm_i915_private *dev_priv = dev->dev_private;
  179. struct drm_crtc *crtc = dig_port->base.base.crtc;
  180. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  181. /* Transition from PSR_state 0 to PSR_state 1, i.e. PSR Inactive */
  182. I915_WRITE(VLV_PSRCTL(pipe),
  183. VLV_EDP_PSR_MODE_SW_TIMER |
  184. VLV_EDP_PSR_SRC_TRANSMITTER_STATE |
  185. VLV_EDP_PSR_ENABLE);
  186. }
  187. static void vlv_psr_activate(struct intel_dp *intel_dp)
  188. {
  189. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  190. struct drm_device *dev = dig_port->base.base.dev;
  191. struct drm_i915_private *dev_priv = dev->dev_private;
  192. struct drm_crtc *crtc = dig_port->base.base.crtc;
  193. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  194. /* Let's do the transition from PSR_state 1 to PSR_state 2
  195. * that is PSR transition to active - static frame transmission.
  196. * Then Hardware is responsible for the transition to PSR_state 3
  197. * that is PSR active - no Remote Frame Buffer (RFB) update.
  198. */
  199. I915_WRITE(VLV_PSRCTL(pipe), I915_READ(VLV_PSRCTL(pipe)) |
  200. VLV_EDP_PSR_ACTIVE_ENTRY);
  201. }
  202. static void hsw_psr_enable_source(struct intel_dp *intel_dp)
  203. {
  204. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  205. struct drm_device *dev = dig_port->base.base.dev;
  206. struct drm_i915_private *dev_priv = dev->dev_private;
  207. uint32_t max_sleep_time = 0x1f;
  208. /* Lately it was identified that depending on panel idle frame count
  209. * calculated at HW can be off by 1. So let's use what came
  210. * from VBT + 1 and at minimum 2 to be on the safe side.
  211. */
  212. uint32_t idle_frames = dev_priv->vbt.psr.idle_frames ?
  213. dev_priv->vbt.psr.idle_frames + 1 : 2;
  214. uint32_t val = 0x0;
  215. const uint32_t link_entry_time = EDP_PSR_MIN_LINK_ENTRY_TIME_8_LINES;
  216. if (dev_priv->psr.link_standby) {
  217. val |= EDP_PSR_LINK_STANDBY;
  218. val |= EDP_PSR_TP2_TP3_TIME_0us;
  219. val |= EDP_PSR_TP1_TIME_0us;
  220. val |= EDP_PSR_SKIP_AUX_EXIT;
  221. } else
  222. val |= EDP_PSR_LINK_DISABLE;
  223. I915_WRITE(EDP_PSR_CTL(dev), val |
  224. (IS_BROADWELL(dev) ? 0 : link_entry_time) |
  225. max_sleep_time << EDP_PSR_MAX_SLEEP_TIME_SHIFT |
  226. idle_frames << EDP_PSR_IDLE_FRAME_SHIFT |
  227. EDP_PSR_ENABLE);
  228. }
  229. static bool intel_psr_match_conditions(struct intel_dp *intel_dp)
  230. {
  231. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  232. struct drm_device *dev = dig_port->base.base.dev;
  233. struct drm_i915_private *dev_priv = dev->dev_private;
  234. struct drm_crtc *crtc = dig_port->base.base.crtc;
  235. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  236. lockdep_assert_held(&dev_priv->psr.lock);
  237. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  238. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  239. dev_priv->psr.source_ok = false;
  240. if (IS_HASWELL(dev) && dig_port->port != PORT_A) {
  241. DRM_DEBUG_KMS("HSW ties PSR to DDI A (eDP)\n");
  242. return false;
  243. }
  244. if (!i915.enable_psr) {
  245. DRM_DEBUG_KMS("PSR disable by flag\n");
  246. return false;
  247. }
  248. if (IS_HASWELL(dev) &&
  249. I915_READ(HSW_STEREO_3D_CTL(intel_crtc->config->cpu_transcoder)) &
  250. S3D_ENABLE) {
  251. DRM_DEBUG_KMS("PSR condition failed: Stereo 3D is Enabled\n");
  252. return false;
  253. }
  254. if (IS_HASWELL(dev) &&
  255. intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  256. DRM_DEBUG_KMS("PSR condition failed: Interlaced is Enabled\n");
  257. return false;
  258. }
  259. dev_priv->psr.source_ok = true;
  260. return true;
  261. }
  262. static void intel_psr_activate(struct intel_dp *intel_dp)
  263. {
  264. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  265. struct drm_device *dev = intel_dig_port->base.base.dev;
  266. struct drm_i915_private *dev_priv = dev->dev_private;
  267. WARN_ON(I915_READ(EDP_PSR_CTL(dev)) & EDP_PSR_ENABLE);
  268. WARN_ON(dev_priv->psr.active);
  269. lockdep_assert_held(&dev_priv->psr.lock);
  270. /* Enable/Re-enable PSR on the host */
  271. if (HAS_DDI(dev))
  272. /* On HSW+ after we enable PSR on source it will activate it
  273. * as soon as it match configure idle_frame count. So
  274. * we just actually enable it here on activation time.
  275. */
  276. hsw_psr_enable_source(intel_dp);
  277. else
  278. vlv_psr_activate(intel_dp);
  279. dev_priv->psr.active = true;
  280. }
  281. /**
  282. * intel_psr_enable - Enable PSR
  283. * @intel_dp: Intel DP
  284. *
  285. * This function can only be called after the pipe is fully trained and enabled.
  286. */
  287. void intel_psr_enable(struct intel_dp *intel_dp)
  288. {
  289. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  290. struct drm_device *dev = intel_dig_port->base.base.dev;
  291. struct drm_i915_private *dev_priv = dev->dev_private;
  292. if (!HAS_PSR(dev)) {
  293. DRM_DEBUG_KMS("PSR not supported on this platform\n");
  294. return;
  295. }
  296. if (!is_edp_psr(intel_dp)) {
  297. DRM_DEBUG_KMS("PSR not supported by this panel\n");
  298. return;
  299. }
  300. mutex_lock(&dev_priv->psr.lock);
  301. if (dev_priv->psr.enabled) {
  302. DRM_DEBUG_KMS("PSR already in use\n");
  303. goto unlock;
  304. }
  305. if (!intel_psr_match_conditions(intel_dp))
  306. goto unlock;
  307. /* First we check VBT, but we must respect sink and source
  308. * known restrictions */
  309. dev_priv->psr.link_standby = dev_priv->vbt.psr.full_link;
  310. if ((intel_dp->psr_dpcd[1] & DP_PSR_NO_TRAIN_ON_EXIT) ||
  311. (IS_BROADWELL(dev) && intel_dig_port->port != PORT_A))
  312. dev_priv->psr.link_standby = true;
  313. dev_priv->psr.busy_frontbuffer_bits = 0;
  314. if (HAS_DDI(dev)) {
  315. hsw_psr_setup_vsc(intel_dp);
  316. /* Avoid continuous PSR exit by masking memup and hpd */
  317. I915_WRITE(EDP_PSR_DEBUG_CTL(dev), EDP_PSR_DEBUG_MASK_MEMUP |
  318. EDP_PSR_DEBUG_MASK_HPD | EDP_PSR_DEBUG_MASK_LPSP);
  319. /* Enable PSR on the panel */
  320. hsw_psr_enable_sink(intel_dp);
  321. if (INTEL_INFO(dev)->gen >= 9)
  322. intel_psr_activate(intel_dp);
  323. } else {
  324. vlv_psr_setup_vsc(intel_dp);
  325. /* Enable PSR on the panel */
  326. vlv_psr_enable_sink(intel_dp);
  327. /* On HSW+ enable_source also means go to PSR entry/active
  328. * state as soon as idle_frame achieved and here would be
  329. * to soon. However on VLV enable_source just enable PSR
  330. * but let it on inactive state. So we might do this prior
  331. * to active transition, i.e. here.
  332. */
  333. vlv_psr_enable_source(intel_dp);
  334. }
  335. dev_priv->psr.enabled = intel_dp;
  336. unlock:
  337. mutex_unlock(&dev_priv->psr.lock);
  338. }
  339. static void vlv_psr_disable(struct intel_dp *intel_dp)
  340. {
  341. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  342. struct drm_device *dev = intel_dig_port->base.base.dev;
  343. struct drm_i915_private *dev_priv = dev->dev_private;
  344. struct intel_crtc *intel_crtc =
  345. to_intel_crtc(intel_dig_port->base.base.crtc);
  346. uint32_t val;
  347. if (dev_priv->psr.active) {
  348. /* Put VLV PSR back to PSR_state 0 that is PSR Disabled. */
  349. if (wait_for((I915_READ(VLV_PSRSTAT(intel_crtc->pipe)) &
  350. VLV_EDP_PSR_IN_TRANS) == 0, 1))
  351. WARN(1, "PSR transition took longer than expected\n");
  352. val = I915_READ(VLV_PSRCTL(intel_crtc->pipe));
  353. val &= ~VLV_EDP_PSR_ACTIVE_ENTRY;
  354. val &= ~VLV_EDP_PSR_ENABLE;
  355. val &= ~VLV_EDP_PSR_MODE_MASK;
  356. I915_WRITE(VLV_PSRCTL(intel_crtc->pipe), val);
  357. dev_priv->psr.active = false;
  358. } else {
  359. WARN_ON(vlv_is_psr_active_on_pipe(dev, intel_crtc->pipe));
  360. }
  361. }
  362. static void hsw_psr_disable(struct intel_dp *intel_dp)
  363. {
  364. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  365. struct drm_device *dev = intel_dig_port->base.base.dev;
  366. struct drm_i915_private *dev_priv = dev->dev_private;
  367. if (dev_priv->psr.active) {
  368. I915_WRITE(EDP_PSR_CTL(dev),
  369. I915_READ(EDP_PSR_CTL(dev)) & ~EDP_PSR_ENABLE);
  370. /* Wait till PSR is idle */
  371. if (_wait_for((I915_READ(EDP_PSR_STATUS_CTL(dev)) &
  372. EDP_PSR_STATUS_STATE_MASK) == 0, 2000, 10))
  373. DRM_ERROR("Timed out waiting for PSR Idle State\n");
  374. dev_priv->psr.active = false;
  375. } else {
  376. WARN_ON(I915_READ(EDP_PSR_CTL(dev)) & EDP_PSR_ENABLE);
  377. }
  378. }
  379. /**
  380. * intel_psr_disable - Disable PSR
  381. * @intel_dp: Intel DP
  382. *
  383. * This function needs to be called before disabling pipe.
  384. */
  385. void intel_psr_disable(struct intel_dp *intel_dp)
  386. {
  387. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  388. struct drm_device *dev = intel_dig_port->base.base.dev;
  389. struct drm_i915_private *dev_priv = dev->dev_private;
  390. mutex_lock(&dev_priv->psr.lock);
  391. if (!dev_priv->psr.enabled) {
  392. mutex_unlock(&dev_priv->psr.lock);
  393. return;
  394. }
  395. if (HAS_DDI(dev))
  396. hsw_psr_disable(intel_dp);
  397. else
  398. vlv_psr_disable(intel_dp);
  399. dev_priv->psr.enabled = NULL;
  400. mutex_unlock(&dev_priv->psr.lock);
  401. cancel_delayed_work_sync(&dev_priv->psr.work);
  402. }
  403. static void intel_psr_work(struct work_struct *work)
  404. {
  405. struct drm_i915_private *dev_priv =
  406. container_of(work, typeof(*dev_priv), psr.work.work);
  407. struct intel_dp *intel_dp = dev_priv->psr.enabled;
  408. struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
  409. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  410. /* We have to make sure PSR is ready for re-enable
  411. * otherwise it keeps disabled until next full enable/disable cycle.
  412. * PSR might take some time to get fully disabled
  413. * and be ready for re-enable.
  414. */
  415. if (HAS_DDI(dev_priv->dev)) {
  416. if (wait_for((I915_READ(EDP_PSR_STATUS_CTL(dev_priv->dev)) &
  417. EDP_PSR_STATUS_STATE_MASK) == 0, 50)) {
  418. DRM_ERROR("Timed out waiting for PSR Idle for re-enable\n");
  419. return;
  420. }
  421. } else {
  422. if (wait_for((I915_READ(VLV_PSRSTAT(pipe)) &
  423. VLV_EDP_PSR_IN_TRANS) == 0, 1)) {
  424. DRM_ERROR("Timed out waiting for PSR Idle for re-enable\n");
  425. return;
  426. }
  427. }
  428. mutex_lock(&dev_priv->psr.lock);
  429. intel_dp = dev_priv->psr.enabled;
  430. if (!intel_dp)
  431. goto unlock;
  432. /*
  433. * The delayed work can race with an invalidate hence we need to
  434. * recheck. Since psr_flush first clears this and then reschedules we
  435. * won't ever miss a flush when bailing out here.
  436. */
  437. if (dev_priv->psr.busy_frontbuffer_bits)
  438. goto unlock;
  439. intel_psr_activate(intel_dp);
  440. unlock:
  441. mutex_unlock(&dev_priv->psr.lock);
  442. }
  443. static void intel_psr_exit(struct drm_device *dev)
  444. {
  445. struct drm_i915_private *dev_priv = dev->dev_private;
  446. struct intel_dp *intel_dp = dev_priv->psr.enabled;
  447. struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
  448. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  449. u32 val;
  450. if (!dev_priv->psr.active)
  451. return;
  452. if (HAS_DDI(dev)) {
  453. val = I915_READ(EDP_PSR_CTL(dev));
  454. WARN_ON(!(val & EDP_PSR_ENABLE));
  455. I915_WRITE(EDP_PSR_CTL(dev), val & ~EDP_PSR_ENABLE);
  456. } else {
  457. val = I915_READ(VLV_PSRCTL(pipe));
  458. /* Here we do the transition from PSR_state 3 to PSR_state 5
  459. * directly once PSR State 4 that is active with single frame
  460. * update can be skipped. PSR_state 5 that is PSR exit then
  461. * Hardware is responsible to transition back to PSR_state 1
  462. * that is PSR inactive. Same state after
  463. * vlv_edp_psr_enable_source.
  464. */
  465. val &= ~VLV_EDP_PSR_ACTIVE_ENTRY;
  466. I915_WRITE(VLV_PSRCTL(pipe), val);
  467. /* Send AUX wake up - Spec says after transitioning to PSR
  468. * active we have to send AUX wake up by writing 01h in DPCD
  469. * 600h of sink device.
  470. * XXX: This might slow down the transition, but without this
  471. * HW doesn't complete the transition to PSR_state 1 and we
  472. * never get the screen updated.
  473. */
  474. drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
  475. DP_SET_POWER_D0);
  476. }
  477. dev_priv->psr.active = false;
  478. }
  479. /**
  480. * intel_psr_invalidate - Invalidade PSR
  481. * @dev: DRM device
  482. * @frontbuffer_bits: frontbuffer plane tracking bits
  483. *
  484. * Since the hardware frontbuffer tracking has gaps we need to integrate
  485. * with the software frontbuffer tracking. This function gets called every
  486. * time frontbuffer rendering starts and a buffer gets dirtied. PSR must be
  487. * disabled if the frontbuffer mask contains a buffer relevant to PSR.
  488. *
  489. * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits."
  490. */
  491. void intel_psr_invalidate(struct drm_device *dev,
  492. unsigned frontbuffer_bits)
  493. {
  494. struct drm_i915_private *dev_priv = dev->dev_private;
  495. struct drm_crtc *crtc;
  496. enum pipe pipe;
  497. mutex_lock(&dev_priv->psr.lock);
  498. if (!dev_priv->psr.enabled) {
  499. mutex_unlock(&dev_priv->psr.lock);
  500. return;
  501. }
  502. crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
  503. pipe = to_intel_crtc(crtc)->pipe;
  504. intel_psr_exit(dev);
  505. frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
  506. dev_priv->psr.busy_frontbuffer_bits |= frontbuffer_bits;
  507. mutex_unlock(&dev_priv->psr.lock);
  508. }
  509. /**
  510. * intel_psr_flush - Flush PSR
  511. * @dev: DRM device
  512. * @frontbuffer_bits: frontbuffer plane tracking bits
  513. *
  514. * Since the hardware frontbuffer tracking has gaps we need to integrate
  515. * with the software frontbuffer tracking. This function gets called every
  516. * time frontbuffer rendering has completed and flushed out to memory. PSR
  517. * can be enabled again if no other frontbuffer relevant to PSR is dirty.
  518. *
  519. * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits.
  520. */
  521. void intel_psr_flush(struct drm_device *dev,
  522. unsigned frontbuffer_bits)
  523. {
  524. struct drm_i915_private *dev_priv = dev->dev_private;
  525. struct drm_crtc *crtc;
  526. enum pipe pipe;
  527. mutex_lock(&dev_priv->psr.lock);
  528. if (!dev_priv->psr.enabled) {
  529. mutex_unlock(&dev_priv->psr.lock);
  530. return;
  531. }
  532. crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
  533. pipe = to_intel_crtc(crtc)->pipe;
  534. dev_priv->psr.busy_frontbuffer_bits &= ~frontbuffer_bits;
  535. /*
  536. * On Haswell sprite plane updates don't result in a psr invalidating
  537. * signal in the hardware. Which means we need to manually fake this in
  538. * software for all flushes, not just when we've seen a preceding
  539. * invalidation through frontbuffer rendering.
  540. */
  541. if (IS_HASWELL(dev) &&
  542. (frontbuffer_bits & INTEL_FRONTBUFFER_SPRITE(pipe)))
  543. intel_psr_exit(dev);
  544. /*
  545. * On Valleyview and Cherryview we don't use hardware tracking so
  546. * any plane updates or cursor moves don't result in a PSR
  547. * invalidating. Which means we need to manually fake this in
  548. * software for all flushes, not just when we've seen a preceding
  549. * invalidation through frontbuffer rendering. */
  550. if (!HAS_DDI(dev))
  551. intel_psr_exit(dev);
  552. if (!dev_priv->psr.active && !dev_priv->psr.busy_frontbuffer_bits)
  553. schedule_delayed_work(&dev_priv->psr.work,
  554. msecs_to_jiffies(100));
  555. mutex_unlock(&dev_priv->psr.lock);
  556. }
  557. /**
  558. * intel_psr_init - Init basic PSR work and mutex.
  559. * @dev: DRM device
  560. *
  561. * This function is called only once at driver load to initialize basic
  562. * PSR stuff.
  563. */
  564. void intel_psr_init(struct drm_device *dev)
  565. {
  566. struct drm_i915_private *dev_priv = dev->dev_private;
  567. INIT_DELAYED_WORK(&dev_priv->psr.work, intel_psr_work);
  568. mutex_init(&dev_priv->psr.lock);
  569. }