drm/i915: wait for actual vblank, not just 20ms

Waiting for a hard coded 20ms isn't always enough to make sure a vblank
period has actually occurred, so add code to make sure we really have
passed through a vblank period (or that the pipe is off when disabling).

This prevents problems with mode setting and link training, and seems to
fix a bug like https://bugs.freedesktop.org/show_bug.cgi?id=29278, but
on an HP 8440p instead. Hopefully also fixes
https://bugs.freedesktop.org/show_bug.cgi?id=29141.

Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
Signed-off-by: Eric Anholt <eric@anholt.net>

authored by Jesse Barnes and committed by Eric Anholt 9d0498a2 d240f20f

+69 -30
+1
drivers/gpu/drm/i915/i915_reg.h
··· 2081 2081 #define PIPE_DITHER_TYPE_ST01 (1 << 2) 2082 2082 /* Pipe A */ 2083 2083 #define PIPEADSL 0x70000 2084 + #define DSL_LINEMASK 0x00000fff 2084 2085 #define PIPEACONF 0x70008 2085 2086 #define PIPEACONF_ENABLE (1<<31) 2086 2087 #define PIPEACONF_DISABLE 0
+1 -1
drivers/gpu/drm/i915/intel_crt.c
··· 328 328 I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER); 329 329 /* Wait for next Vblank to substitue 330 330 * border color for Color info */ 331 - intel_wait_for_vblank(dev); 331 + intel_wait_for_vblank(dev, pipe); 332 332 st00 = I915_READ8(VGA_MSR_WRITE); 333 333 status = ((st00 & (1 << 4)) != 0) ? 334 334 connector_status_connected :
+56 -22
drivers/gpu/drm/i915/intel_display.c
··· 977 977 return true; 978 978 } 979 979 980 - void 981 - intel_wait_for_vblank(struct drm_device *dev) 980 + /** 981 + * intel_wait_for_vblank - wait for vblank on a given pipe 982 + * @dev: drm device 983 + * @pipe: pipe to wait for 984 + * 985 + * Wait for vblank to occur on a given pipe. Needed for various bits of 986 + * mode setting code. 987 + */ 988 + void intel_wait_for_vblank(struct drm_device *dev, int pipe) 982 989 { 983 - /* Wait for 20ms, i.e. one cycle at 50hz. */ 984 - if (in_dbg_master()) 985 - mdelay(20); /* The kernel debugger cannot call msleep() */ 986 - else 987 - msleep(20); 990 + struct drm_i915_private *dev_priv = dev->dev_private; 991 + int pipestat_reg = (pipe == 0 ? PIPEASTAT : PIPEBSTAT); 992 + 993 + /* Wait for vblank interrupt bit to set */ 994 + if (wait_for((I915_READ(pipestat_reg) & 995 + PIPE_VBLANK_INTERRUPT_STATUS) == 0, 996 + 50, 0)) 997 + DRM_DEBUG_KMS("vblank wait timed out\n"); 998 + } 999 + 1000 + /** 1001 + * intel_wait_for_vblank_off - wait for vblank after disabling a pipe 1002 + * @dev: drm device 1003 + * @pipe: pipe to wait for 1004 + * 1005 + * After disabling a pipe, we can't wait for vblank in the usual way, 1006 + * spinning on the vblank interrupt status bit, since we won't actually 1007 + * see an interrupt when the pipe is disabled. 1008 + * 1009 + * So this function waits for the display line value to settle (it 1010 + * usually ends up stopping at the start of the next frame). 1011 + */ 1012 + void intel_wait_for_vblank_off(struct drm_device *dev, int pipe) 1013 + { 1014 + struct drm_i915_private *dev_priv = dev->dev_private; 1015 + int pipedsl_reg = (pipe == 0 ? PIPEADSL : PIPEBDSL); 1016 + unsigned long timeout = jiffies + msecs_to_jiffies(100); 1017 + u32 last_line; 1018 + 1019 + /* Wait for the display line to settle */ 1020 + do { 1021 + last_line = I915_READ(pipedsl_reg) & DSL_LINEMASK; 1022 + mdelay(5); 1023 + } while (((I915_READ(pipedsl_reg) & DSL_LINEMASK) != last_line) && 1024 + time_after(timeout, jiffies)); 1025 + 1026 + if (time_after(jiffies, timeout)) 1027 + DRM_DEBUG_KMS("vblank wait timed out\n"); 988 1028 } 989 1029 990 1030 /* Parameters have changed, update FBC info */ ··· 1097 1057 return; 1098 1058 } 1099 1059 1100 - intel_wait_for_vblank(dev); 1101 - 1102 1060 DRM_DEBUG_KMS("disabled FBC\n"); 1103 1061 } 1104 1062 ··· 1153 1115 dpfc_ctl = I915_READ(DPFC_CONTROL); 1154 1116 dpfc_ctl &= ~DPFC_CTL_EN; 1155 1117 I915_WRITE(DPFC_CONTROL, dpfc_ctl); 1156 - intel_wait_for_vblank(dev); 1157 1118 1158 1119 DRM_DEBUG_KMS("disabled FBC\n"); 1159 1120 } ··· 1213 1176 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL); 1214 1177 dpfc_ctl &= ~DPFC_CTL_EN; 1215 1178 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl); 1216 - intel_wait_for_vblank(dev); 1217 1179 1218 1180 DRM_DEBUG_KMS("disabled FBC\n"); 1219 1181 } ··· 1511 1475 if ((IS_I965G(dev) || plane == 0)) 1512 1476 intel_update_fbc(crtc, &crtc->mode); 1513 1477 1514 - intel_wait_for_vblank(dev); 1478 + intel_wait_for_vblank(dev, intel_crtc->pipe); 1515 1479 intel_increase_pllclock(crtc, true); 1516 1480 1517 1481 return 0; ··· 1629 1593 if ((IS_I965G(dev) || plane == 0)) 1630 1594 intel_update_fbc(crtc, &crtc->mode); 1631 1595 1632 - intel_wait_for_vblank(dev); 1596 + intel_wait_for_vblank(dev, pipe); 1633 1597 1634 1598 if (old_fb) { 1635 1599 intel_fb = to_intel_framebuffer(old_fb); ··· 2379 2343 I915_READ(dspbase_reg); 2380 2344 } 2381 2345 2382 - if (!IS_I9XX(dev)) { 2383 - /* Wait for vblank for the disable to take effect */ 2384 - intel_wait_for_vblank(dev); 2385 - } 2346 + /* Wait for vblank for the disable to take effect */ 2347 + intel_wait_for_vblank_off(dev, pipe); 2386 2348 2387 2349 /* Don't disable pipe A or pipe A PLLs if needed */ 2388 2350 if (pipeconf_reg == PIPEACONF && ··· 2395 2361 } 2396 2362 2397 2363 /* Wait for vblank for the disable to take effect. */ 2398 - intel_wait_for_vblank(dev); 2364 + intel_wait_for_vblank_off(dev, pipe); 2399 2365 2400 2366 temp = I915_READ(dpll_reg); 2401 2367 if ((temp & DPLL_VCO_ENABLE) != 0) { ··· 4130 4096 I915_WRITE(pipeconf_reg, pipeconf); 4131 4097 I915_READ(pipeconf_reg); 4132 4098 4133 - intel_wait_for_vblank(dev); 4099 + intel_wait_for_vblank(dev, pipe); 4134 4100 4135 4101 if (IS_IRONLAKE(dev)) { 4136 4102 /* enable address swizzle for tiling buffer */ ··· 4542 4508 encoder_funcs->commit(encoder); 4543 4509 } 4544 4510 /* let the connector get through one full cycle before testing */ 4545 - intel_wait_for_vblank(dev); 4511 + intel_wait_for_vblank(dev, intel_crtc->pipe); 4546 4512 4547 4513 return crtc; 4548 4514 } ··· 4747 4713 dpll &= ~DISPLAY_RATE_SELECT_FPA1; 4748 4714 I915_WRITE(dpll_reg, dpll); 4749 4715 dpll = I915_READ(dpll_reg); 4750 - intel_wait_for_vblank(dev); 4716 + intel_wait_for_vblank(dev, pipe); 4751 4717 dpll = I915_READ(dpll_reg); 4752 4718 if (dpll & DISPLAY_RATE_SELECT_FPA1) 4753 4719 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n"); ··· 4791 4757 dpll |= DISPLAY_RATE_SELECT_FPA1; 4792 4758 I915_WRITE(dpll_reg, dpll); 4793 4759 dpll = I915_READ(dpll_reg); 4794 - intel_wait_for_vblank(dev); 4760 + intel_wait_for_vblank(dev, pipe); 4795 4761 dpll = I915_READ(dpll_reg); 4796 4762 if (!(dpll & DISPLAY_RATE_SELECT_FPA1)) 4797 4763 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
+2 -1
drivers/gpu/drm/i915/intel_dp.c
··· 1145 1145 { 1146 1146 struct drm_device *dev = intel_dp->base.enc.dev; 1147 1147 struct drm_i915_private *dev_priv = dev->dev_private; 1148 + struct intel_crtc *intel_crtc = to_intel_crtc(intel_dp->base.enc.crtc); 1148 1149 int ret; 1149 1150 1150 1151 I915_WRITE(intel_dp->output_reg, dp_reg_value); 1151 1152 POSTING_READ(intel_dp->output_reg); 1152 1153 if (first) 1153 - intel_wait_for_vblank(dev); 1154 + intel_wait_for_vblank(dev, intel_crtc->pipe); 1154 1155 1155 1156 intel_dp_aux_native_write_1(intel_dp, 1156 1157 DP_TRAINING_PATTERN_SET,
+2 -1
drivers/gpu/drm/i915/intel_drv.h
··· 219 219 struct drm_crtc *crtc); 220 220 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data, 221 221 struct drm_file *file_priv); 222 - extern void intel_wait_for_vblank(struct drm_device *dev); 222 + extern void intel_wait_for_vblank_off(struct drm_device *dev, int pipe); 223 + extern void intel_wait_for_vblank(struct drm_device *dev, int pipe); 223 224 extern struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe); 224 225 extern struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder, 225 226 struct drm_connector *connector,
+2 -1
drivers/gpu/drm/i915/intel_sdvo.c
··· 1218 1218 struct drm_device *dev = encoder->dev; 1219 1219 struct drm_i915_private *dev_priv = dev->dev_private; 1220 1220 struct intel_sdvo *intel_sdvo = enc_to_intel_sdvo(encoder); 1221 + struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc); 1221 1222 u32 temp; 1222 1223 1223 1224 if (mode != DRM_MODE_DPMS_ON) { ··· 1241 1240 if ((temp & SDVO_ENABLE) == 0) 1242 1241 intel_sdvo_write_sdvox(intel_sdvo, temp | SDVO_ENABLE); 1243 1242 for (i = 0; i < 2; i++) 1244 - intel_wait_for_vblank(dev); 1243 + intel_wait_for_vblank(dev, intel_crtc->pipe); 1245 1244 1246 1245 status = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2); 1247 1246 /* Warn if the device reported failure to sync.
+5 -4
drivers/gpu/drm/i915/intel_tv.c
··· 1158 1158 1159 1159 /* Wait for vblank for the disable to take effect */ 1160 1160 if (!IS_I9XX(dev)) 1161 - intel_wait_for_vblank(dev); 1161 + intel_wait_for_vblank(dev, intel_crtc->pipe); 1162 1162 1163 1163 I915_WRITE(pipeconf_reg, pipeconf & ~PIPEACONF_ENABLE); 1164 1164 /* Wait for vblank for the disable to take effect. */ 1165 - intel_wait_for_vblank(dev); 1165 + intel_wait_for_vblank(dev, intel_crtc->pipe); 1166 1166 1167 1167 /* Filter ctl must be set before TV_WIN_SIZE */ 1168 1168 I915_WRITE(TV_FILTER_CTL_1, TV_AUTO_SCALE); ··· 1231 1231 struct drm_encoder *encoder = &intel_tv->base.enc; 1232 1232 struct drm_device *dev = encoder->dev; 1233 1233 struct drm_i915_private *dev_priv = dev->dev_private; 1234 + struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc); 1234 1235 unsigned long irqflags; 1235 1236 u32 tv_ctl, save_tv_ctl; 1236 1237 u32 tv_dac, save_tv_dac; ··· 1268 1267 DAC_C_0_7_V); 1269 1268 I915_WRITE(TV_CTL, tv_ctl); 1270 1269 I915_WRITE(TV_DAC, tv_dac); 1271 - intel_wait_for_vblank(dev); 1270 + intel_wait_for_vblank(dev, intel_crtc->pipe); 1272 1271 tv_dac = I915_READ(TV_DAC); 1273 1272 I915_WRITE(TV_DAC, save_tv_dac); 1274 1273 I915_WRITE(TV_CTL, save_tv_ctl); 1275 - intel_wait_for_vblank(dev); 1274 + intel_wait_for_vblank(dev, intel_crtc->pipe); 1276 1275 /* 1277 1276 * A B C 1278 1277 * 0 1 1 Composite