Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 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 | |
Rodrigo Vivi | 94b8395 | 2014-12-08 06:46:31 -0800 | [diff] [blame] | 24 | /** |
| 25 | * DOC: Frame Buffer Compression (FBC) |
| 26 | * |
| 27 | * FBC tries to save memory bandwidth (and so power consumption) by |
| 28 | * compressing the amount of memory used by the display. It is total |
| 29 | * transparent to user space and completely handled in the kernel. |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 30 | * |
| 31 | * The benefits of FBC are mostly visible with solid backgrounds and |
Rodrigo Vivi | 94b8395 | 2014-12-08 06:46:31 -0800 | [diff] [blame] | 32 | * variation-less patterns. It comes from keeping the memory footprint small |
| 33 | * and having fewer memory pages opened and accessed for refreshing the display. |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 34 | * |
Rodrigo Vivi | 94b8395 | 2014-12-08 06:46:31 -0800 | [diff] [blame] | 35 | * i915 is responsible to reserve stolen memory for FBC and configure its |
| 36 | * offset on proper registers. The hardware takes care of all |
| 37 | * compress/decompress. However there are many known cases where we have to |
| 38 | * forcibly disable it to allow proper screen updates. |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 39 | */ |
| 40 | |
Rodrigo Vivi | 94b8395 | 2014-12-08 06:46:31 -0800 | [diff] [blame] | 41 | #include "intel_drv.h" |
| 42 | #include "i915_drv.h" |
| 43 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 44 | static void i8xx_fbc_disable(struct drm_device *dev) |
| 45 | { |
| 46 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 47 | u32 fbc_ctl; |
| 48 | |
| 49 | dev_priv->fbc.enabled = false; |
| 50 | |
| 51 | /* Disable compression */ |
| 52 | fbc_ctl = I915_READ(FBC_CONTROL); |
| 53 | if ((fbc_ctl & FBC_CTL_EN) == 0) |
| 54 | return; |
| 55 | |
| 56 | fbc_ctl &= ~FBC_CTL_EN; |
| 57 | I915_WRITE(FBC_CONTROL, fbc_ctl); |
| 58 | |
| 59 | /* Wait for compressing bit to clear */ |
| 60 | if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) { |
| 61 | DRM_DEBUG_KMS("FBC idle timed out\n"); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | DRM_DEBUG_KMS("disabled FBC\n"); |
| 66 | } |
| 67 | |
| 68 | static void i8xx_fbc_enable(struct drm_crtc *crtc) |
| 69 | { |
| 70 | struct drm_device *dev = crtc->dev; |
| 71 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 72 | struct drm_framebuffer *fb = crtc->primary->fb; |
| 73 | struct drm_i915_gem_object *obj = intel_fb_obj(fb); |
| 74 | struct intel_crtc *intel_crtc = to_intel_crtc(crtc); |
| 75 | int cfb_pitch; |
| 76 | int i; |
| 77 | u32 fbc_ctl; |
| 78 | |
| 79 | dev_priv->fbc.enabled = true; |
| 80 | |
Jani Nikula | 60ee5cd | 2015-02-05 12:04:27 +0200 | [diff] [blame] | 81 | /* Note: fbc.threshold == 1 for i8xx */ |
| 82 | cfb_pitch = dev_priv->fbc.uncompressed_size / FBC_LL_SIZE; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 83 | if (fb->pitches[0] < cfb_pitch) |
| 84 | cfb_pitch = fb->pitches[0]; |
| 85 | |
| 86 | /* FBC_CTL wants 32B or 64B units */ |
| 87 | if (IS_GEN2(dev)) |
| 88 | cfb_pitch = (cfb_pitch / 32) - 1; |
| 89 | else |
| 90 | cfb_pitch = (cfb_pitch / 64) - 1; |
| 91 | |
| 92 | /* Clear old tags */ |
| 93 | for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++) |
| 94 | I915_WRITE(FBC_TAG + (i * 4), 0); |
| 95 | |
| 96 | if (IS_GEN4(dev)) { |
| 97 | u32 fbc_ctl2; |
| 98 | |
| 99 | /* Set it up... */ |
| 100 | fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE; |
| 101 | fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane); |
| 102 | I915_WRITE(FBC_CONTROL2, fbc_ctl2); |
| 103 | I915_WRITE(FBC_FENCE_OFF, crtc->y); |
| 104 | } |
| 105 | |
| 106 | /* enable it... */ |
| 107 | fbc_ctl = I915_READ(FBC_CONTROL); |
| 108 | fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT; |
| 109 | fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC; |
| 110 | if (IS_I945GM(dev)) |
| 111 | fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */ |
| 112 | fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT; |
| 113 | fbc_ctl |= obj->fence_reg; |
| 114 | I915_WRITE(FBC_CONTROL, fbc_ctl); |
| 115 | |
| 116 | DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n", |
| 117 | cfb_pitch, crtc->y, plane_name(intel_crtc->plane)); |
| 118 | } |
| 119 | |
| 120 | static bool i8xx_fbc_enabled(struct drm_device *dev) |
| 121 | { |
| 122 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 123 | |
| 124 | return I915_READ(FBC_CONTROL) & FBC_CTL_EN; |
| 125 | } |
| 126 | |
| 127 | static void g4x_fbc_enable(struct drm_crtc *crtc) |
| 128 | { |
| 129 | struct drm_device *dev = crtc->dev; |
| 130 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 131 | struct drm_framebuffer *fb = crtc->primary->fb; |
| 132 | struct drm_i915_gem_object *obj = intel_fb_obj(fb); |
| 133 | struct intel_crtc *intel_crtc = to_intel_crtc(crtc); |
| 134 | u32 dpfc_ctl; |
| 135 | |
| 136 | dev_priv->fbc.enabled = true; |
| 137 | |
| 138 | dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN; |
| 139 | if (drm_format_plane_cpp(fb->pixel_format, 0) == 2) |
| 140 | dpfc_ctl |= DPFC_CTL_LIMIT_2X; |
| 141 | else |
| 142 | dpfc_ctl |= DPFC_CTL_LIMIT_1X; |
| 143 | dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg; |
| 144 | |
| 145 | I915_WRITE(DPFC_FENCE_YOFF, crtc->y); |
| 146 | |
| 147 | /* enable it... */ |
| 148 | I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN); |
| 149 | |
| 150 | DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane)); |
| 151 | } |
| 152 | |
| 153 | static void g4x_fbc_disable(struct drm_device *dev) |
| 154 | { |
| 155 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 156 | u32 dpfc_ctl; |
| 157 | |
| 158 | dev_priv->fbc.enabled = false; |
| 159 | |
| 160 | /* Disable compression */ |
| 161 | dpfc_ctl = I915_READ(DPFC_CONTROL); |
| 162 | if (dpfc_ctl & DPFC_CTL_EN) { |
| 163 | dpfc_ctl &= ~DPFC_CTL_EN; |
| 164 | I915_WRITE(DPFC_CONTROL, dpfc_ctl); |
| 165 | |
| 166 | DRM_DEBUG_KMS("disabled FBC\n"); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static bool g4x_fbc_enabled(struct drm_device *dev) |
| 171 | { |
| 172 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 173 | |
| 174 | return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN; |
| 175 | } |
| 176 | |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 177 | static void intel_fbc_nuke(struct drm_i915_private *dev_priv) |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 178 | { |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 179 | I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE); |
| 180 | POSTING_READ(MSG_FBC_REND_STATE); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void ilk_fbc_enable(struct drm_crtc *crtc) |
| 184 | { |
| 185 | struct drm_device *dev = crtc->dev; |
| 186 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 187 | struct drm_framebuffer *fb = crtc->primary->fb; |
| 188 | struct drm_i915_gem_object *obj = intel_fb_obj(fb); |
| 189 | struct intel_crtc *intel_crtc = to_intel_crtc(crtc); |
| 190 | u32 dpfc_ctl; |
Paulo Zanoni | ce65e47 | 2015-06-30 10:53:05 -0300 | [diff] [blame] | 191 | int threshold = dev_priv->fbc.threshold; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 192 | |
| 193 | dev_priv->fbc.enabled = true; |
| 194 | |
| 195 | dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane); |
| 196 | if (drm_format_plane_cpp(fb->pixel_format, 0) == 2) |
Paulo Zanoni | ce65e47 | 2015-06-30 10:53:05 -0300 | [diff] [blame] | 197 | threshold++; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 198 | |
Paulo Zanoni | ce65e47 | 2015-06-30 10:53:05 -0300 | [diff] [blame] | 199 | switch (threshold) { |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 200 | case 4: |
| 201 | case 3: |
| 202 | dpfc_ctl |= DPFC_CTL_LIMIT_4X; |
| 203 | break; |
| 204 | case 2: |
| 205 | dpfc_ctl |= DPFC_CTL_LIMIT_2X; |
| 206 | break; |
| 207 | case 1: |
| 208 | dpfc_ctl |= DPFC_CTL_LIMIT_1X; |
| 209 | break; |
| 210 | } |
| 211 | dpfc_ctl |= DPFC_CTL_FENCE_EN; |
| 212 | if (IS_GEN5(dev)) |
| 213 | dpfc_ctl |= obj->fence_reg; |
| 214 | |
| 215 | I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y); |
| 216 | I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID); |
| 217 | /* enable it... */ |
| 218 | I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN); |
| 219 | |
| 220 | if (IS_GEN6(dev)) { |
| 221 | I915_WRITE(SNB_DPFC_CTL_SA, |
| 222 | SNB_CPU_FENCE_ENABLE | obj->fence_reg); |
| 223 | I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 224 | } |
| 225 | |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 226 | intel_fbc_nuke(dev_priv); |
| 227 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 228 | DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane)); |
| 229 | } |
| 230 | |
| 231 | static void ilk_fbc_disable(struct drm_device *dev) |
| 232 | { |
| 233 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 234 | u32 dpfc_ctl; |
| 235 | |
| 236 | dev_priv->fbc.enabled = false; |
| 237 | |
| 238 | /* Disable compression */ |
| 239 | dpfc_ctl = I915_READ(ILK_DPFC_CONTROL); |
| 240 | if (dpfc_ctl & DPFC_CTL_EN) { |
| 241 | dpfc_ctl &= ~DPFC_CTL_EN; |
| 242 | I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl); |
| 243 | |
| 244 | DRM_DEBUG_KMS("disabled FBC\n"); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static bool ilk_fbc_enabled(struct drm_device *dev) |
| 249 | { |
| 250 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 251 | |
| 252 | return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN; |
| 253 | } |
| 254 | |
| 255 | static void gen7_fbc_enable(struct drm_crtc *crtc) |
| 256 | { |
| 257 | struct drm_device *dev = crtc->dev; |
| 258 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 259 | struct drm_framebuffer *fb = crtc->primary->fb; |
| 260 | struct drm_i915_gem_object *obj = intel_fb_obj(fb); |
| 261 | struct intel_crtc *intel_crtc = to_intel_crtc(crtc); |
| 262 | u32 dpfc_ctl; |
Paulo Zanoni | ce65e47 | 2015-06-30 10:53:05 -0300 | [diff] [blame] | 263 | int threshold = dev_priv->fbc.threshold; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 264 | |
| 265 | dev_priv->fbc.enabled = true; |
| 266 | |
Paulo Zanoni | d8514d6 | 2015-06-12 14:36:21 -0300 | [diff] [blame] | 267 | dpfc_ctl = 0; |
| 268 | if (IS_IVYBRIDGE(dev)) |
| 269 | dpfc_ctl |= IVB_DPFC_CTL_PLANE(intel_crtc->plane); |
| 270 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 271 | if (drm_format_plane_cpp(fb->pixel_format, 0) == 2) |
Paulo Zanoni | ce65e47 | 2015-06-30 10:53:05 -0300 | [diff] [blame] | 272 | threshold++; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 273 | |
Paulo Zanoni | ce65e47 | 2015-06-30 10:53:05 -0300 | [diff] [blame] | 274 | switch (threshold) { |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 275 | case 4: |
| 276 | case 3: |
| 277 | dpfc_ctl |= DPFC_CTL_LIMIT_4X; |
| 278 | break; |
| 279 | case 2: |
| 280 | dpfc_ctl |= DPFC_CTL_LIMIT_2X; |
| 281 | break; |
| 282 | case 1: |
| 283 | dpfc_ctl |= DPFC_CTL_LIMIT_1X; |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN; |
| 288 | |
| 289 | if (dev_priv->fbc.false_color) |
| 290 | dpfc_ctl |= FBC_CTL_FALSE_COLOR; |
| 291 | |
| 292 | I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN); |
| 293 | |
| 294 | if (IS_IVYBRIDGE(dev)) { |
| 295 | /* WaFbcAsynchFlipDisableFbcQueue:ivb */ |
| 296 | I915_WRITE(ILK_DISPLAY_CHICKEN1, |
| 297 | I915_READ(ILK_DISPLAY_CHICKEN1) | |
| 298 | ILK_FBCQ_DIS); |
| 299 | } else { |
| 300 | /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */ |
| 301 | I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe), |
| 302 | I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) | |
| 303 | HSW_FBCQ_DIS); |
| 304 | } |
| 305 | |
| 306 | I915_WRITE(SNB_DPFC_CTL_SA, |
| 307 | SNB_CPU_FENCE_ENABLE | obj->fence_reg); |
| 308 | I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y); |
| 309 | |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 310 | intel_fbc_nuke(dev_priv); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 311 | |
| 312 | DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane)); |
| 313 | } |
| 314 | |
Rodrigo Vivi | 94b8395 | 2014-12-08 06:46:31 -0800 | [diff] [blame] | 315 | /** |
| 316 | * intel_fbc_enabled - Is FBC enabled? |
| 317 | * @dev: the drm_device |
| 318 | * |
| 319 | * This function is used to verify the current state of FBC. |
| 320 | * FIXME: This should be tracked in the plane config eventually |
| 321 | * instead of queried at runtime for most callers. |
| 322 | */ |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 323 | bool intel_fbc_enabled(struct drm_device *dev) |
| 324 | { |
| 325 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 326 | |
| 327 | return dev_priv->fbc.enabled; |
| 328 | } |
| 329 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 330 | static void intel_fbc_work_fn(struct work_struct *__work) |
| 331 | { |
| 332 | struct intel_fbc_work *work = |
| 333 | container_of(to_delayed_work(__work), |
| 334 | struct intel_fbc_work, work); |
| 335 | struct drm_device *dev = work->crtc->dev; |
| 336 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 337 | |
| 338 | mutex_lock(&dev->struct_mutex); |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 339 | mutex_lock(&dev_priv->fbc.lock); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 340 | if (work == dev_priv->fbc.fbc_work) { |
| 341 | /* Double check that we haven't switched fb without cancelling |
| 342 | * the prior work. |
| 343 | */ |
| 344 | if (work->crtc->primary->fb == work->fb) { |
| 345 | dev_priv->display.enable_fbc(work->crtc); |
| 346 | |
Paulo Zanoni | e35fef2 | 2015-02-09 14:46:29 -0200 | [diff] [blame] | 347 | dev_priv->fbc.crtc = to_intel_crtc(work->crtc); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 348 | dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id; |
| 349 | dev_priv->fbc.y = work->crtc->y; |
| 350 | } |
| 351 | |
| 352 | dev_priv->fbc.fbc_work = NULL; |
| 353 | } |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 354 | mutex_unlock(&dev_priv->fbc.lock); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 355 | mutex_unlock(&dev->struct_mutex); |
| 356 | |
| 357 | kfree(work); |
| 358 | } |
| 359 | |
| 360 | static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv) |
| 361 | { |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 362 | WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock)); |
| 363 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 364 | if (dev_priv->fbc.fbc_work == NULL) |
| 365 | return; |
| 366 | |
| 367 | DRM_DEBUG_KMS("cancelling pending FBC enable\n"); |
| 368 | |
| 369 | /* Synchronisation is provided by struct_mutex and checking of |
| 370 | * dev_priv->fbc.fbc_work, so we can perform the cancellation |
| 371 | * entirely asynchronously. |
| 372 | */ |
| 373 | if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work)) |
| 374 | /* tasklet was killed before being run, clean up */ |
| 375 | kfree(dev_priv->fbc.fbc_work); |
| 376 | |
| 377 | /* Mark the work as no longer wanted so that if it does |
| 378 | * wake-up (because the work was already running and waiting |
| 379 | * for our mutex), it will discover that is no longer |
| 380 | * necessary to run. |
| 381 | */ |
| 382 | dev_priv->fbc.fbc_work = NULL; |
| 383 | } |
| 384 | |
| 385 | static void intel_fbc_enable(struct drm_crtc *crtc) |
| 386 | { |
| 387 | struct intel_fbc_work *work; |
| 388 | struct drm_device *dev = crtc->dev; |
| 389 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 390 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 391 | WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock)); |
| 392 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 393 | if (!dev_priv->display.enable_fbc) |
| 394 | return; |
| 395 | |
| 396 | intel_fbc_cancel_work(dev_priv); |
| 397 | |
| 398 | work = kzalloc(sizeof(*work), GFP_KERNEL); |
| 399 | if (work == NULL) { |
| 400 | DRM_ERROR("Failed to allocate FBC work structure\n"); |
| 401 | dev_priv->display.enable_fbc(crtc); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | work->crtc = crtc; |
| 406 | work->fb = crtc->primary->fb; |
| 407 | INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn); |
| 408 | |
| 409 | dev_priv->fbc.fbc_work = work; |
| 410 | |
| 411 | /* Delay the actual enabling to let pageflipping cease and the |
| 412 | * display to settle before starting the compression. Note that |
| 413 | * this delay also serves a second purpose: it allows for a |
| 414 | * vblank to pass after disabling the FBC before we attempt |
| 415 | * to modify the control registers. |
| 416 | * |
| 417 | * A more complicated solution would involve tracking vblanks |
| 418 | * following the termination of the page-flipping sequence |
| 419 | * and indeed performing the enable as a co-routine and not |
| 420 | * waiting synchronously upon the vblank. |
| 421 | * |
| 422 | * WaFbcWaitForVBlankBeforeEnable:ilk,snb |
| 423 | */ |
| 424 | schedule_delayed_work(&work->work, msecs_to_jiffies(50)); |
| 425 | } |
| 426 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 427 | static void __intel_fbc_disable(struct drm_device *dev) |
| 428 | { |
| 429 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 430 | |
| 431 | WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock)); |
| 432 | |
| 433 | intel_fbc_cancel_work(dev_priv); |
| 434 | |
| 435 | if (!dev_priv->display.disable_fbc) |
| 436 | return; |
| 437 | |
| 438 | dev_priv->display.disable_fbc(dev); |
| 439 | dev_priv->fbc.crtc = NULL; |
| 440 | } |
| 441 | |
Rodrigo Vivi | 94b8395 | 2014-12-08 06:46:31 -0800 | [diff] [blame] | 442 | /** |
| 443 | * intel_fbc_disable - disable FBC |
| 444 | * @dev: the drm_device |
| 445 | * |
| 446 | * This function disables FBC. |
| 447 | */ |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 448 | void intel_fbc_disable(struct drm_device *dev) |
| 449 | { |
| 450 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 451 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 452 | mutex_lock(&dev_priv->fbc.lock); |
| 453 | __intel_fbc_disable(dev); |
| 454 | mutex_unlock(&dev_priv->fbc.lock); |
| 455 | } |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 456 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 457 | /* |
| 458 | * intel_fbc_disable_crtc - disable FBC if it's associated with crtc |
| 459 | * @crtc: the CRTC |
| 460 | * |
| 461 | * This function disables FBC if it's associated with the provided CRTC. |
| 462 | */ |
| 463 | void intel_fbc_disable_crtc(struct intel_crtc *crtc) |
| 464 | { |
| 465 | struct drm_device *dev = crtc->base.dev; |
| 466 | struct drm_i915_private *dev_priv = dev->dev_private; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 467 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 468 | mutex_lock(&dev_priv->fbc.lock); |
| 469 | if (dev_priv->fbc.crtc == crtc) |
| 470 | __intel_fbc_disable(dev); |
| 471 | mutex_unlock(&dev_priv->fbc.lock); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 472 | } |
| 473 | |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 474 | const char *intel_no_fbc_reason_str(enum no_fbc_reason reason) |
| 475 | { |
| 476 | switch (reason) { |
| 477 | case FBC_OK: |
| 478 | return "FBC enabled but currently disabled in hardware"; |
| 479 | case FBC_UNSUPPORTED: |
| 480 | return "unsupported by this chipset"; |
| 481 | case FBC_NO_OUTPUT: |
| 482 | return "no output"; |
| 483 | case FBC_STOLEN_TOO_SMALL: |
| 484 | return "not enough stolen memory"; |
| 485 | case FBC_UNSUPPORTED_MODE: |
| 486 | return "mode incompatible with compression"; |
| 487 | case FBC_MODE_TOO_LARGE: |
| 488 | return "mode too large for compression"; |
| 489 | case FBC_BAD_PLANE: |
| 490 | return "FBC unsupported on plane"; |
| 491 | case FBC_NOT_TILED: |
| 492 | return "framebuffer not tiled or fenced"; |
| 493 | case FBC_MULTIPLE_PIPES: |
| 494 | return "more than one pipe active"; |
| 495 | case FBC_MODULE_PARAM: |
| 496 | return "disabled per module param"; |
| 497 | case FBC_CHIP_DEFAULT: |
| 498 | return "disabled per chip default"; |
| 499 | case FBC_ROTATION: |
| 500 | return "rotation unsupported"; |
| 501 | default: |
| 502 | MISSING_CASE(reason); |
| 503 | return "unknown reason"; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | static void set_no_fbc_reason(struct drm_i915_private *dev_priv, |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 508 | enum no_fbc_reason reason) |
| 509 | { |
| 510 | if (dev_priv->fbc.no_fbc_reason == reason) |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 511 | return; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 512 | |
| 513 | dev_priv->fbc.no_fbc_reason = reason; |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 514 | DRM_DEBUG_KMS("Disabling FBC: %s\n", intel_no_fbc_reason_str(reason)); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 515 | } |
| 516 | |
Paulo Zanoni | 9510675 | 2015-02-13 17:23:41 -0200 | [diff] [blame] | 517 | static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv) |
| 518 | { |
Paulo Zanoni | 9510675 | 2015-02-13 17:23:41 -0200 | [diff] [blame] | 519 | struct drm_crtc *crtc = NULL, *tmp_crtc; |
Paulo Zanoni | 68b9214 | 2015-02-13 17:23:42 -0200 | [diff] [blame] | 520 | enum pipe pipe; |
Paulo Zanoni | e489e38 | 2015-02-13 17:23:43 -0200 | [diff] [blame] | 521 | bool pipe_a_only = false, one_pipe_only = false; |
Paulo Zanoni | 9510675 | 2015-02-13 17:23:41 -0200 | [diff] [blame] | 522 | |
Paulo Zanoni | 68b9214 | 2015-02-13 17:23:42 -0200 | [diff] [blame] | 523 | if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8) |
| 524 | pipe_a_only = true; |
Paulo Zanoni | e489e38 | 2015-02-13 17:23:43 -0200 | [diff] [blame] | 525 | else if (INTEL_INFO(dev_priv)->gen <= 4) |
| 526 | one_pipe_only = true; |
Paulo Zanoni | 68b9214 | 2015-02-13 17:23:42 -0200 | [diff] [blame] | 527 | |
| 528 | for_each_pipe(dev_priv, pipe) { |
| 529 | tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe]; |
| 530 | |
Paulo Zanoni | 9510675 | 2015-02-13 17:23:41 -0200 | [diff] [blame] | 531 | if (intel_crtc_active(tmp_crtc) && |
Maarten Lankhorst | b70709a | 2015-04-21 17:12:53 +0300 | [diff] [blame] | 532 | to_intel_plane_state(tmp_crtc->primary->state)->visible) { |
Paulo Zanoni | e489e38 | 2015-02-13 17:23:43 -0200 | [diff] [blame] | 533 | if (one_pipe_only && crtc) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 534 | set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES); |
Paulo Zanoni | 9510675 | 2015-02-13 17:23:41 -0200 | [diff] [blame] | 535 | return NULL; |
| 536 | } |
| 537 | crtc = tmp_crtc; |
| 538 | } |
Paulo Zanoni | 68b9214 | 2015-02-13 17:23:42 -0200 | [diff] [blame] | 539 | |
| 540 | if (pipe_a_only) |
| 541 | break; |
Paulo Zanoni | 9510675 | 2015-02-13 17:23:41 -0200 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | if (!crtc || crtc->primary->fb == NULL) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 545 | set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT); |
Paulo Zanoni | 9510675 | 2015-02-13 17:23:41 -0200 | [diff] [blame] | 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | return crtc; |
| 550 | } |
| 551 | |
Paulo Zanoni | fc78672 | 2015-07-02 19:25:08 -0300 | [diff] [blame] | 552 | static int find_compression_threshold(struct drm_device *dev, |
| 553 | struct drm_mm_node *node, |
| 554 | int size, |
| 555 | int fb_cpp) |
| 556 | { |
| 557 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 558 | int compression_threshold = 1; |
| 559 | int ret; |
| 560 | |
| 561 | /* HACK: This code depends on what we will do in *_enable_fbc. If that |
| 562 | * code changes, this code needs to change as well. |
| 563 | * |
| 564 | * The enable_fbc code will attempt to use one of our 2 compression |
| 565 | * thresholds, therefore, in that case, we only have 1 resort. |
| 566 | */ |
| 567 | |
| 568 | /* Try to over-allocate to reduce reallocations and fragmentation. */ |
| 569 | ret = i915_gem_stolen_insert_node(dev_priv, node, size <<= 1, 4096); |
| 570 | if (ret == 0) |
| 571 | return compression_threshold; |
| 572 | |
| 573 | again: |
| 574 | /* HW's ability to limit the CFB is 1:4 */ |
| 575 | if (compression_threshold > 4 || |
| 576 | (fb_cpp == 2 && compression_threshold == 2)) |
| 577 | return 0; |
| 578 | |
| 579 | ret = i915_gem_stolen_insert_node(dev_priv, node, size >>= 1, 4096); |
| 580 | if (ret && INTEL_INFO(dev)->gen <= 4) { |
| 581 | return 0; |
| 582 | } else if (ret) { |
| 583 | compression_threshold <<= 1; |
| 584 | goto again; |
| 585 | } else { |
| 586 | return compression_threshold; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | static int intel_fbc_alloc_cfb(struct drm_device *dev, int size, int fb_cpp) |
| 591 | { |
| 592 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 593 | struct drm_mm_node *uninitialized_var(compressed_llb); |
| 594 | int ret; |
| 595 | |
| 596 | ret = find_compression_threshold(dev, &dev_priv->fbc.compressed_fb, |
| 597 | size, fb_cpp); |
| 598 | if (!ret) |
| 599 | goto err_llb; |
| 600 | else if (ret > 1) { |
| 601 | DRM_INFO("Reducing the compressed framebuffer size. This may lead to less power savings than a non-reduced-size. Try to increase stolen memory size if available in BIOS.\n"); |
| 602 | |
| 603 | } |
| 604 | |
| 605 | dev_priv->fbc.threshold = ret; |
| 606 | |
| 607 | if (INTEL_INFO(dev_priv)->gen >= 5) |
| 608 | I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start); |
| 609 | else if (IS_GM45(dev)) { |
| 610 | I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start); |
| 611 | } else { |
| 612 | compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL); |
| 613 | if (!compressed_llb) |
| 614 | goto err_fb; |
| 615 | |
| 616 | ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb, |
| 617 | 4096, 4096); |
| 618 | if (ret) |
| 619 | goto err_fb; |
| 620 | |
| 621 | dev_priv->fbc.compressed_llb = compressed_llb; |
| 622 | |
| 623 | I915_WRITE(FBC_CFB_BASE, |
| 624 | dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start); |
| 625 | I915_WRITE(FBC_LL_BASE, |
| 626 | dev_priv->mm.stolen_base + compressed_llb->start); |
| 627 | } |
| 628 | |
| 629 | dev_priv->fbc.uncompressed_size = size; |
| 630 | |
| 631 | DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n", |
| 632 | size); |
| 633 | |
| 634 | return 0; |
| 635 | |
| 636 | err_fb: |
| 637 | kfree(compressed_llb); |
| 638 | i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb); |
| 639 | err_llb: |
| 640 | pr_info_once("drm: not enough stolen space for compressed buffer (need %d more bytes), disabling. Hint: you may be able to increase stolen memory size in the BIOS to avoid this.\n", size); |
| 641 | return -ENOSPC; |
| 642 | } |
| 643 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 644 | static void __intel_fbc_cleanup_cfb(struct drm_device *dev) |
Paulo Zanoni | fc78672 | 2015-07-02 19:25:08 -0300 | [diff] [blame] | 645 | { |
| 646 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 647 | |
| 648 | if (dev_priv->fbc.uncompressed_size == 0) |
| 649 | return; |
| 650 | |
| 651 | i915_gem_stolen_remove_node(dev_priv, &dev_priv->fbc.compressed_fb); |
| 652 | |
| 653 | if (dev_priv->fbc.compressed_llb) { |
| 654 | i915_gem_stolen_remove_node(dev_priv, |
| 655 | dev_priv->fbc.compressed_llb); |
| 656 | kfree(dev_priv->fbc.compressed_llb); |
| 657 | } |
| 658 | |
| 659 | dev_priv->fbc.uncompressed_size = 0; |
| 660 | } |
| 661 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 662 | void intel_fbc_cleanup_cfb(struct drm_device *dev) |
| 663 | { |
| 664 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 665 | |
| 666 | mutex_lock(&dev_priv->fbc.lock); |
| 667 | __intel_fbc_cleanup_cfb(dev); |
| 668 | mutex_unlock(&dev_priv->fbc.lock); |
| 669 | } |
| 670 | |
Paulo Zanoni | fc78672 | 2015-07-02 19:25:08 -0300 | [diff] [blame] | 671 | static int intel_fbc_setup_cfb(struct drm_device *dev, int size, int fb_cpp) |
| 672 | { |
| 673 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 674 | |
| 675 | if (size <= dev_priv->fbc.uncompressed_size) |
| 676 | return 0; |
| 677 | |
| 678 | /* Release any current block */ |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 679 | __intel_fbc_cleanup_cfb(dev); |
Paulo Zanoni | fc78672 | 2015-07-02 19:25:08 -0300 | [diff] [blame] | 680 | |
| 681 | return intel_fbc_alloc_cfb(dev, size, fb_cpp); |
| 682 | } |
| 683 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 684 | /** |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 685 | * __intel_fbc_update - enable/disable FBC as needed, unlocked |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 686 | * @dev: the drm_device |
| 687 | * |
| 688 | * Set up the framebuffer compression hardware at mode set time. We |
| 689 | * enable it if possible: |
| 690 | * - plane A only (on pre-965) |
| 691 | * - no pixel mulitply/line duplication |
| 692 | * - no alpha buffer discard |
| 693 | * - no dual wide |
| 694 | * - framebuffer <= max_hdisplay in width, max_vdisplay in height |
| 695 | * |
| 696 | * We can't assume that any compression will take place (worst case), |
| 697 | * so the compressed buffer has to be the same size as the uncompressed |
| 698 | * one. It also must reside (along with the line length buffer) in |
| 699 | * stolen memory. |
| 700 | * |
| 701 | * We need to enable/disable FBC on a global basis. |
| 702 | */ |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 703 | static void __intel_fbc_update(struct drm_device *dev) |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 704 | { |
| 705 | struct drm_i915_private *dev_priv = dev->dev_private; |
Paulo Zanoni | 9510675 | 2015-02-13 17:23:41 -0200 | [diff] [blame] | 706 | struct drm_crtc *crtc = NULL; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 707 | struct intel_crtc *intel_crtc; |
| 708 | struct drm_framebuffer *fb; |
| 709 | struct drm_i915_gem_object *obj; |
| 710 | const struct drm_display_mode *adjusted_mode; |
| 711 | unsigned int max_width, max_height; |
| 712 | |
Paulo Zanoni | 104618b | 2015-02-09 14:46:28 -0200 | [diff] [blame] | 713 | if (!HAS_FBC(dev)) |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 714 | return; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 715 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 716 | WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock)); |
| 717 | |
Yu Zhang | bd49234 | 2015-02-10 19:05:50 +0800 | [diff] [blame] | 718 | /* disable framebuffer compression in vGPU */ |
| 719 | if (intel_vgpu_active(dev)) |
| 720 | i915.enable_fbc = 0; |
| 721 | |
Paulo Zanoni | 7cc6574 | 2015-02-09 14:46:27 -0200 | [diff] [blame] | 722 | if (i915.enable_fbc < 0) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 723 | set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT); |
Paulo Zanoni | 7cc6574 | 2015-02-09 14:46:27 -0200 | [diff] [blame] | 724 | goto out_disable; |
| 725 | } |
| 726 | |
Rodrigo Vivi | ab585de | 2015-03-24 12:40:09 -0700 | [diff] [blame] | 727 | if (!i915.enable_fbc) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 728 | set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM); |
Paulo Zanoni | 7cc6574 | 2015-02-09 14:46:27 -0200 | [diff] [blame] | 729 | goto out_disable; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /* |
| 733 | * If FBC is already on, we just have to verify that we can |
| 734 | * keep it that way... |
| 735 | * Need to disable if: |
| 736 | * - more than one pipe is active |
| 737 | * - changing FBC params (stride, fence, mode) |
| 738 | * - new fb is too large to fit in compressed buffer |
| 739 | * - going to an unsupported config (interlace, pixel multiply, etc.) |
| 740 | */ |
Paulo Zanoni | 9510675 | 2015-02-13 17:23:41 -0200 | [diff] [blame] | 741 | crtc = intel_fbc_find_crtc(dev_priv); |
| 742 | if (!crtc) |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 743 | goto out_disable; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 744 | |
| 745 | intel_crtc = to_intel_crtc(crtc); |
| 746 | fb = crtc->primary->fb; |
| 747 | obj = intel_fb_obj(fb); |
Ander Conselvan de Oliveira | 6e3c971 | 2015-01-15 14:55:25 +0200 | [diff] [blame] | 748 | adjusted_mode = &intel_crtc->config->base.adjusted_mode; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 749 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 750 | if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) || |
| 751 | (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 752 | set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 753 | goto out_disable; |
| 754 | } |
| 755 | |
| 756 | if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) { |
| 757 | max_width = 4096; |
| 758 | max_height = 4096; |
| 759 | } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) { |
| 760 | max_width = 4096; |
| 761 | max_height = 2048; |
| 762 | } else { |
| 763 | max_width = 2048; |
| 764 | max_height = 1536; |
| 765 | } |
Ander Conselvan de Oliveira | 6e3c971 | 2015-01-15 14:55:25 +0200 | [diff] [blame] | 766 | if (intel_crtc->config->pipe_src_w > max_width || |
| 767 | intel_crtc->config->pipe_src_h > max_height) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 768 | set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 769 | goto out_disable; |
| 770 | } |
| 771 | if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) && |
| 772 | intel_crtc->plane != PLANE_A) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 773 | set_no_fbc_reason(dev_priv, FBC_BAD_PLANE); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 774 | goto out_disable; |
| 775 | } |
| 776 | |
| 777 | /* The use of a CPU fence is mandatory in order to detect writes |
| 778 | * by the CPU to the scanout and trigger updates to the FBC. |
| 779 | */ |
| 780 | if (obj->tiling_mode != I915_TILING_X || |
| 781 | obj->fence_reg == I915_FENCE_REG_NONE) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 782 | set_no_fbc_reason(dev_priv, FBC_NOT_TILED); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 783 | goto out_disable; |
| 784 | } |
| 785 | if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) && |
Matt Roper | 8e7d688 | 2015-01-21 16:35:41 -0800 | [diff] [blame] | 786 | crtc->primary->state->rotation != BIT(DRM_ROTATE_0)) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 787 | set_no_fbc_reason(dev_priv, FBC_ROTATION); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 788 | goto out_disable; |
| 789 | } |
| 790 | |
| 791 | /* If the kernel debugger is active, always disable compression */ |
| 792 | if (in_dbg_master()) |
| 793 | goto out_disable; |
| 794 | |
Paulo Zanoni | fc78672 | 2015-07-02 19:25:08 -0300 | [diff] [blame] | 795 | if (intel_fbc_setup_cfb(dev, obj->base.size, |
| 796 | drm_format_plane_cpp(fb->pixel_format, 0))) { |
Paulo Zanoni | 2e8144a | 2015-06-12 14:36:20 -0300 | [diff] [blame] | 797 | set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 798 | goto out_disable; |
| 799 | } |
| 800 | |
| 801 | /* If the scanout has not changed, don't modify the FBC settings. |
| 802 | * Note that we make the fundamental assumption that the fb->obj |
| 803 | * cannot be unpinned (and have its GTT offset and fence revoked) |
| 804 | * without first being decoupled from the scanout and FBC disabled. |
| 805 | */ |
Paulo Zanoni | e35fef2 | 2015-02-09 14:46:29 -0200 | [diff] [blame] | 806 | if (dev_priv->fbc.crtc == intel_crtc && |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 807 | dev_priv->fbc.fb_id == fb->base.id && |
| 808 | dev_priv->fbc.y == crtc->y) |
| 809 | return; |
| 810 | |
| 811 | if (intel_fbc_enabled(dev)) { |
| 812 | /* We update FBC along two paths, after changing fb/crtc |
| 813 | * configuration (modeswitching) and after page-flipping |
| 814 | * finishes. For the latter, we know that not only did |
| 815 | * we disable the FBC at the start of the page-flip |
| 816 | * sequence, but also more than one vblank has passed. |
| 817 | * |
| 818 | * For the former case of modeswitching, it is possible |
| 819 | * to switch between two FBC valid configurations |
| 820 | * instantaneously so we do need to disable the FBC |
| 821 | * before we can modify its control registers. We also |
| 822 | * have to wait for the next vblank for that to take |
| 823 | * effect. However, since we delay enabling FBC we can |
| 824 | * assume that a vblank has passed since disabling and |
| 825 | * that we can safely alter the registers in the deferred |
| 826 | * callback. |
| 827 | * |
| 828 | * In the scenario that we go from a valid to invalid |
| 829 | * and then back to valid FBC configuration we have |
| 830 | * no strict enforcement that a vblank occurred since |
| 831 | * disabling the FBC. However, along all current pipe |
| 832 | * disabling paths we do need to wait for a vblank at |
| 833 | * some point. And we wait before enabling FBC anyway. |
| 834 | */ |
| 835 | DRM_DEBUG_KMS("disabling active FBC for update\n"); |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 836 | __intel_fbc_disable(dev); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | intel_fbc_enable(crtc); |
| 840 | dev_priv->fbc.no_fbc_reason = FBC_OK; |
| 841 | return; |
| 842 | |
| 843 | out_disable: |
| 844 | /* Multiple disables should be harmless */ |
| 845 | if (intel_fbc_enabled(dev)) { |
| 846 | DRM_DEBUG_KMS("unsupported config, disabling FBC\n"); |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 847 | __intel_fbc_disable(dev); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 848 | } |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 849 | __intel_fbc_cleanup_cfb(dev); |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * intel_fbc_update - enable/disable FBC as needed |
| 854 | * @dev: the drm_device |
| 855 | * |
| 856 | * This function reevaluates the overall state and enables or disables FBC. |
| 857 | */ |
| 858 | void intel_fbc_update(struct drm_device *dev) |
| 859 | { |
| 860 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 861 | |
| 862 | mutex_lock(&dev_priv->fbc.lock); |
| 863 | __intel_fbc_update(dev); |
| 864 | mutex_unlock(&dev_priv->fbc.lock); |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 865 | } |
| 866 | |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 867 | void intel_fbc_invalidate(struct drm_i915_private *dev_priv, |
| 868 | unsigned int frontbuffer_bits, |
| 869 | enum fb_op_origin origin) |
| 870 | { |
| 871 | struct drm_device *dev = dev_priv->dev; |
| 872 | unsigned int fbc_bits; |
| 873 | |
| 874 | if (origin == ORIGIN_GTT) |
| 875 | return; |
| 876 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 877 | mutex_lock(&dev_priv->fbc.lock); |
| 878 | |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 879 | if (dev_priv->fbc.enabled) |
| 880 | fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe); |
| 881 | else if (dev_priv->fbc.fbc_work) |
| 882 | fbc_bits = INTEL_FRONTBUFFER_PRIMARY( |
| 883 | to_intel_crtc(dev_priv->fbc.fbc_work->crtc)->pipe); |
| 884 | else |
| 885 | fbc_bits = dev_priv->fbc.possible_framebuffer_bits; |
| 886 | |
| 887 | dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits); |
| 888 | |
| 889 | if (dev_priv->fbc.busy_bits) |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 890 | __intel_fbc_disable(dev); |
| 891 | |
| 892 | mutex_unlock(&dev_priv->fbc.lock); |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | void intel_fbc_flush(struct drm_i915_private *dev_priv, |
| 896 | unsigned int frontbuffer_bits) |
| 897 | { |
| 898 | struct drm_device *dev = dev_priv->dev; |
| 899 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 900 | mutex_lock(&dev_priv->fbc.lock); |
| 901 | |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 902 | if (!dev_priv->fbc.busy_bits) |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 903 | goto out; |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 904 | |
| 905 | dev_priv->fbc.busy_bits &= ~frontbuffer_bits; |
| 906 | |
| 907 | if (!dev_priv->fbc.busy_bits) |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 908 | __intel_fbc_update(dev); |
| 909 | |
| 910 | out: |
| 911 | mutex_unlock(&dev_priv->fbc.lock); |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 912 | } |
| 913 | |
Rodrigo Vivi | 94b8395 | 2014-12-08 06:46:31 -0800 | [diff] [blame] | 914 | /** |
| 915 | * intel_fbc_init - Initialize FBC |
| 916 | * @dev_priv: the i915 device |
| 917 | * |
| 918 | * This function might be called during PM init process. |
| 919 | */ |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 920 | void intel_fbc_init(struct drm_i915_private *dev_priv) |
| 921 | { |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 922 | enum pipe pipe; |
| 923 | |
Paulo Zanoni | 25ad93f | 2015-07-02 19:25:10 -0300 | [diff] [blame^] | 924 | mutex_init(&dev_priv->fbc.lock); |
| 925 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 926 | if (!HAS_FBC(dev_priv)) { |
| 927 | dev_priv->fbc.enabled = false; |
Paulo Zanoni | 104618b | 2015-02-09 14:46:28 -0200 | [diff] [blame] | 928 | dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED; |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 929 | return; |
| 930 | } |
| 931 | |
Paulo Zanoni | dbef0f1 | 2015-02-13 17:23:46 -0200 | [diff] [blame] | 932 | for_each_pipe(dev_priv, pipe) { |
| 933 | dev_priv->fbc.possible_framebuffer_bits |= |
| 934 | INTEL_FRONTBUFFER_PRIMARY(pipe); |
| 935 | |
| 936 | if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8) |
| 937 | break; |
| 938 | } |
| 939 | |
Rodrigo Vivi | 7ff0ebc | 2014-12-08 14:09:10 -0200 | [diff] [blame] | 940 | if (INTEL_INFO(dev_priv)->gen >= 7) { |
| 941 | dev_priv->display.fbc_enabled = ilk_fbc_enabled; |
| 942 | dev_priv->display.enable_fbc = gen7_fbc_enable; |
| 943 | dev_priv->display.disable_fbc = ilk_fbc_disable; |
| 944 | } else if (INTEL_INFO(dev_priv)->gen >= 5) { |
| 945 | dev_priv->display.fbc_enabled = ilk_fbc_enabled; |
| 946 | dev_priv->display.enable_fbc = ilk_fbc_enable; |
| 947 | dev_priv->display.disable_fbc = ilk_fbc_disable; |
| 948 | } else if (IS_GM45(dev_priv)) { |
| 949 | dev_priv->display.fbc_enabled = g4x_fbc_enabled; |
| 950 | dev_priv->display.enable_fbc = g4x_fbc_enable; |
| 951 | dev_priv->display.disable_fbc = g4x_fbc_disable; |
| 952 | } else { |
| 953 | dev_priv->display.fbc_enabled = i8xx_fbc_enabled; |
| 954 | dev_priv->display.enable_fbc = i8xx_fbc_enable; |
| 955 | dev_priv->display.disable_fbc = i8xx_fbc_disable; |
| 956 | |
| 957 | /* This value was pulled out of someone's hat */ |
| 958 | I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT); |
| 959 | } |
| 960 | |
| 961 | dev_priv->fbc.enabled = dev_priv->display.fbc_enabled(dev_priv->dev); |
| 962 | } |