blob: 9e55b9badb4bc1ed97d79ba0654d47f672d0e071 [file] [log] [blame]
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02001/*
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 Vivi94b839572014-12-08 06:46:31 -080024/**
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 Vivi7ff0ebc2014-12-08 14:09:10 -020030 *
31 * The benefits of FBC are mostly visible with solid backgrounds and
Rodrigo Vivi94b839572014-12-08 06:46:31 -080032 * 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 Vivi7ff0ebc2014-12-08 14:09:10 -020034 *
Rodrigo Vivi94b839572014-12-08 06:46:31 -080035 * 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 Vivi7ff0ebc2014-12-08 14:09:10 -020039 */
40
Rodrigo Vivi94b839572014-12-08 06:46:31 -080041#include "intel_drv.h"
42#include "i915_drv.h"
43
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020044static 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
68static 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 Nikula60ee5cd2015-02-05 12:04:27 +020081 /* Note: fbc.threshold == 1 for i8xx */
82 cfb_pitch = dev_priv->fbc.uncompressed_size / FBC_LL_SIZE;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020083 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
120static 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
127static 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
153static 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
170static 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 Zanonidbef0f12015-02-13 17:23:46 -0200177static void intel_fbc_nuke(struct drm_i915_private *dev_priv)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200178{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200179 I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
180 POSTING_READ(MSG_FBC_REND_STATE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200181}
182
183static 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 Zanonice65e472015-06-30 10:53:05 -0300191 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200192
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 Zanonice65e472015-06-30 10:53:05 -0300197 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200198
Paulo Zanonice65e472015-06-30 10:53:05 -0300199 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200200 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 Vivi7ff0ebc2014-12-08 14:09:10 -0200224 }
225
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200226 intel_fbc_nuke(dev_priv);
227
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200228 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
229}
230
231static 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
248static 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
255static 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 Zanonice65e472015-06-30 10:53:05 -0300263 int threshold = dev_priv->fbc.threshold;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200264
265 dev_priv->fbc.enabled = true;
266
Paulo Zanonid8514d62015-06-12 14:36:21 -0300267 dpfc_ctl = 0;
268 if (IS_IVYBRIDGE(dev))
269 dpfc_ctl |= IVB_DPFC_CTL_PLANE(intel_crtc->plane);
270
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200271 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Paulo Zanonice65e472015-06-30 10:53:05 -0300272 threshold++;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200273
Paulo Zanonice65e472015-06-30 10:53:05 -0300274 switch (threshold) {
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200275 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 Zanonidbef0f12015-02-13 17:23:46 -0200310 intel_fbc_nuke(dev_priv);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200311
312 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
313}
314
Rodrigo Vivi94b839572014-12-08 06:46:31 -0800315/**
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 Vivi7ff0ebc2014-12-08 14:09:10 -0200323bool 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 Vivi7ff0ebc2014-12-08 14:09:10 -0200330static 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);
339 if (work == dev_priv->fbc.fbc_work) {
340 /* Double check that we haven't switched fb without cancelling
341 * the prior work.
342 */
343 if (work->crtc->primary->fb == work->fb) {
344 dev_priv->display.enable_fbc(work->crtc);
345
Paulo Zanonie35fef22015-02-09 14:46:29 -0200346 dev_priv->fbc.crtc = to_intel_crtc(work->crtc);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200347 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
348 dev_priv->fbc.y = work->crtc->y;
349 }
350
351 dev_priv->fbc.fbc_work = NULL;
352 }
353 mutex_unlock(&dev->struct_mutex);
354
355 kfree(work);
356}
357
358static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
359{
360 if (dev_priv->fbc.fbc_work == NULL)
361 return;
362
363 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
364
365 /* Synchronisation is provided by struct_mutex and checking of
366 * dev_priv->fbc.fbc_work, so we can perform the cancellation
367 * entirely asynchronously.
368 */
369 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
370 /* tasklet was killed before being run, clean up */
371 kfree(dev_priv->fbc.fbc_work);
372
373 /* Mark the work as no longer wanted so that if it does
374 * wake-up (because the work was already running and waiting
375 * for our mutex), it will discover that is no longer
376 * necessary to run.
377 */
378 dev_priv->fbc.fbc_work = NULL;
379}
380
381static void intel_fbc_enable(struct drm_crtc *crtc)
382{
383 struct intel_fbc_work *work;
384 struct drm_device *dev = crtc->dev;
385 struct drm_i915_private *dev_priv = dev->dev_private;
386
387 if (!dev_priv->display.enable_fbc)
388 return;
389
390 intel_fbc_cancel_work(dev_priv);
391
392 work = kzalloc(sizeof(*work), GFP_KERNEL);
393 if (work == NULL) {
394 DRM_ERROR("Failed to allocate FBC work structure\n");
395 dev_priv->display.enable_fbc(crtc);
396 return;
397 }
398
399 work->crtc = crtc;
400 work->fb = crtc->primary->fb;
401 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
402
403 dev_priv->fbc.fbc_work = work;
404
405 /* Delay the actual enabling to let pageflipping cease and the
406 * display to settle before starting the compression. Note that
407 * this delay also serves a second purpose: it allows for a
408 * vblank to pass after disabling the FBC before we attempt
409 * to modify the control registers.
410 *
411 * A more complicated solution would involve tracking vblanks
412 * following the termination of the page-flipping sequence
413 * and indeed performing the enable as a co-routine and not
414 * waiting synchronously upon the vblank.
415 *
416 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
417 */
418 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
419}
420
Rodrigo Vivi94b839572014-12-08 06:46:31 -0800421/**
422 * intel_fbc_disable - disable FBC
423 * @dev: the drm_device
424 *
425 * This function disables FBC.
426 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200427void intel_fbc_disable(struct drm_device *dev)
428{
429 struct drm_i915_private *dev_priv = dev->dev_private;
430
431 intel_fbc_cancel_work(dev_priv);
432
433 if (!dev_priv->display.disable_fbc)
434 return;
435
436 dev_priv->display.disable_fbc(dev);
Paulo Zanonie35fef22015-02-09 14:46:29 -0200437 dev_priv->fbc.crtc = NULL;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200438}
439
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300440const char *intel_no_fbc_reason_str(enum no_fbc_reason reason)
441{
442 switch (reason) {
443 case FBC_OK:
444 return "FBC enabled but currently disabled in hardware";
445 case FBC_UNSUPPORTED:
446 return "unsupported by this chipset";
447 case FBC_NO_OUTPUT:
448 return "no output";
449 case FBC_STOLEN_TOO_SMALL:
450 return "not enough stolen memory";
451 case FBC_UNSUPPORTED_MODE:
452 return "mode incompatible with compression";
453 case FBC_MODE_TOO_LARGE:
454 return "mode too large for compression";
455 case FBC_BAD_PLANE:
456 return "FBC unsupported on plane";
457 case FBC_NOT_TILED:
458 return "framebuffer not tiled or fenced";
459 case FBC_MULTIPLE_PIPES:
460 return "more than one pipe active";
461 case FBC_MODULE_PARAM:
462 return "disabled per module param";
463 case FBC_CHIP_DEFAULT:
464 return "disabled per chip default";
465 case FBC_ROTATION:
466 return "rotation unsupported";
467 default:
468 MISSING_CASE(reason);
469 return "unknown reason";
470 }
471}
472
473static void set_no_fbc_reason(struct drm_i915_private *dev_priv,
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200474 enum no_fbc_reason reason)
475{
476 if (dev_priv->fbc.no_fbc_reason == reason)
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300477 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200478
479 dev_priv->fbc.no_fbc_reason = reason;
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300480 DRM_DEBUG_KMS("Disabling FBC: %s\n", intel_no_fbc_reason_str(reason));
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200481}
482
Paulo Zanoni95106752015-02-13 17:23:41 -0200483static struct drm_crtc *intel_fbc_find_crtc(struct drm_i915_private *dev_priv)
484{
Paulo Zanoni95106752015-02-13 17:23:41 -0200485 struct drm_crtc *crtc = NULL, *tmp_crtc;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200486 enum pipe pipe;
Paulo Zanonie489e382015-02-13 17:23:43 -0200487 bool pipe_a_only = false, one_pipe_only = false;
Paulo Zanoni95106752015-02-13 17:23:41 -0200488
Paulo Zanoni68b92142015-02-13 17:23:42 -0200489 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
490 pipe_a_only = true;
Paulo Zanonie489e382015-02-13 17:23:43 -0200491 else if (INTEL_INFO(dev_priv)->gen <= 4)
492 one_pipe_only = true;
Paulo Zanoni68b92142015-02-13 17:23:42 -0200493
494 for_each_pipe(dev_priv, pipe) {
495 tmp_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
496
Paulo Zanoni95106752015-02-13 17:23:41 -0200497 if (intel_crtc_active(tmp_crtc) &&
Maarten Lankhorstb70709a2015-04-21 17:12:53 +0300498 to_intel_plane_state(tmp_crtc->primary->state)->visible) {
Paulo Zanonie489e382015-02-13 17:23:43 -0200499 if (one_pipe_only && crtc) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300500 set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES);
Paulo Zanoni95106752015-02-13 17:23:41 -0200501 return NULL;
502 }
503 crtc = tmp_crtc;
504 }
Paulo Zanoni68b92142015-02-13 17:23:42 -0200505
506 if (pipe_a_only)
507 break;
Paulo Zanoni95106752015-02-13 17:23:41 -0200508 }
509
510 if (!crtc || crtc->primary->fb == NULL) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300511 set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT);
Paulo Zanoni95106752015-02-13 17:23:41 -0200512 return NULL;
513 }
514
515 return crtc;
516}
517
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200518/**
519 * intel_fbc_update - enable/disable FBC as needed
520 * @dev: the drm_device
521 *
522 * Set up the framebuffer compression hardware at mode set time. We
523 * enable it if possible:
524 * - plane A only (on pre-965)
525 * - no pixel mulitply/line duplication
526 * - no alpha buffer discard
527 * - no dual wide
528 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
529 *
530 * We can't assume that any compression will take place (worst case),
531 * so the compressed buffer has to be the same size as the uncompressed
532 * one. It also must reside (along with the line length buffer) in
533 * stolen memory.
534 *
535 * We need to enable/disable FBC on a global basis.
536 */
537void intel_fbc_update(struct drm_device *dev)
538{
539 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni95106752015-02-13 17:23:41 -0200540 struct drm_crtc *crtc = NULL;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200541 struct intel_crtc *intel_crtc;
542 struct drm_framebuffer *fb;
543 struct drm_i915_gem_object *obj;
544 const struct drm_display_mode *adjusted_mode;
545 unsigned int max_width, max_height;
546
Paulo Zanoni104618b2015-02-09 14:46:28 -0200547 if (!HAS_FBC(dev))
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200548 return;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200549
Yu Zhangbd492342015-02-10 19:05:50 +0800550 /* disable framebuffer compression in vGPU */
551 if (intel_vgpu_active(dev))
552 i915.enable_fbc = 0;
553
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200554 if (i915.enable_fbc < 0) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300555 set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200556 goto out_disable;
557 }
558
Rodrigo Viviab585de2015-03-24 12:40:09 -0700559 if (!i915.enable_fbc) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300560 set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM);
Paulo Zanoni7cc65742015-02-09 14:46:27 -0200561 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200562 }
563
564 /*
565 * If FBC is already on, we just have to verify that we can
566 * keep it that way...
567 * Need to disable if:
568 * - more than one pipe is active
569 * - changing FBC params (stride, fence, mode)
570 * - new fb is too large to fit in compressed buffer
571 * - going to an unsupported config (interlace, pixel multiply, etc.)
572 */
Paulo Zanoni95106752015-02-13 17:23:41 -0200573 crtc = intel_fbc_find_crtc(dev_priv);
574 if (!crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200575 goto out_disable;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200576
577 intel_crtc = to_intel_crtc(crtc);
578 fb = crtc->primary->fb;
579 obj = intel_fb_obj(fb);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200580 adjusted_mode = &intel_crtc->config->base.adjusted_mode;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200581
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200582 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
583 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300584 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200585 goto out_disable;
586 }
587
588 if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
589 max_width = 4096;
590 max_height = 4096;
591 } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
592 max_width = 4096;
593 max_height = 2048;
594 } else {
595 max_width = 2048;
596 max_height = 1536;
597 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200598 if (intel_crtc->config->pipe_src_w > max_width ||
599 intel_crtc->config->pipe_src_h > max_height) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300600 set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200601 goto out_disable;
602 }
603 if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
604 intel_crtc->plane != PLANE_A) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300605 set_no_fbc_reason(dev_priv, FBC_BAD_PLANE);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200606 goto out_disable;
607 }
608
609 /* The use of a CPU fence is mandatory in order to detect writes
610 * by the CPU to the scanout and trigger updates to the FBC.
611 */
612 if (obj->tiling_mode != I915_TILING_X ||
613 obj->fence_reg == I915_FENCE_REG_NONE) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300614 set_no_fbc_reason(dev_priv, FBC_NOT_TILED);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200615 goto out_disable;
616 }
617 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -0800618 crtc->primary->state->rotation != BIT(DRM_ROTATE_0)) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300619 set_no_fbc_reason(dev_priv, FBC_ROTATION);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200620 goto out_disable;
621 }
622
623 /* If the kernel debugger is active, always disable compression */
624 if (in_dbg_master())
625 goto out_disable;
626
627 if (i915_gem_stolen_setup_compression(dev, obj->base.size,
628 drm_format_plane_cpp(fb->pixel_format, 0))) {
Paulo Zanoni2e8144a2015-06-12 14:36:20 -0300629 set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200630 goto out_disable;
631 }
632
633 /* If the scanout has not changed, don't modify the FBC settings.
634 * Note that we make the fundamental assumption that the fb->obj
635 * cannot be unpinned (and have its GTT offset and fence revoked)
636 * without first being decoupled from the scanout and FBC disabled.
637 */
Paulo Zanonie35fef22015-02-09 14:46:29 -0200638 if (dev_priv->fbc.crtc == intel_crtc &&
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200639 dev_priv->fbc.fb_id == fb->base.id &&
640 dev_priv->fbc.y == crtc->y)
641 return;
642
643 if (intel_fbc_enabled(dev)) {
644 /* We update FBC along two paths, after changing fb/crtc
645 * configuration (modeswitching) and after page-flipping
646 * finishes. For the latter, we know that not only did
647 * we disable the FBC at the start of the page-flip
648 * sequence, but also more than one vblank has passed.
649 *
650 * For the former case of modeswitching, it is possible
651 * to switch between two FBC valid configurations
652 * instantaneously so we do need to disable the FBC
653 * before we can modify its control registers. We also
654 * have to wait for the next vblank for that to take
655 * effect. However, since we delay enabling FBC we can
656 * assume that a vblank has passed since disabling and
657 * that we can safely alter the registers in the deferred
658 * callback.
659 *
660 * In the scenario that we go from a valid to invalid
661 * and then back to valid FBC configuration we have
662 * no strict enforcement that a vblank occurred since
663 * disabling the FBC. However, along all current pipe
664 * disabling paths we do need to wait for a vblank at
665 * some point. And we wait before enabling FBC anyway.
666 */
667 DRM_DEBUG_KMS("disabling active FBC for update\n");
668 intel_fbc_disable(dev);
669 }
670
671 intel_fbc_enable(crtc);
672 dev_priv->fbc.no_fbc_reason = FBC_OK;
673 return;
674
675out_disable:
676 /* Multiple disables should be harmless */
677 if (intel_fbc_enabled(dev)) {
678 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
679 intel_fbc_disable(dev);
680 }
681 i915_gem_stolen_cleanup_compression(dev);
682}
683
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200684void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
685 unsigned int frontbuffer_bits,
686 enum fb_op_origin origin)
687{
688 struct drm_device *dev = dev_priv->dev;
689 unsigned int fbc_bits;
690
691 if (origin == ORIGIN_GTT)
692 return;
693
694 if (dev_priv->fbc.enabled)
695 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
696 else if (dev_priv->fbc.fbc_work)
697 fbc_bits = INTEL_FRONTBUFFER_PRIMARY(
698 to_intel_crtc(dev_priv->fbc.fbc_work->crtc)->pipe);
699 else
700 fbc_bits = dev_priv->fbc.possible_framebuffer_bits;
701
702 dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
703
704 if (dev_priv->fbc.busy_bits)
705 intel_fbc_disable(dev);
706}
707
708void intel_fbc_flush(struct drm_i915_private *dev_priv,
709 unsigned int frontbuffer_bits)
710{
711 struct drm_device *dev = dev_priv->dev;
712
713 if (!dev_priv->fbc.busy_bits)
714 return;
715
716 dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
717
718 if (!dev_priv->fbc.busy_bits)
719 intel_fbc_update(dev);
720}
721
Rodrigo Vivi94b839572014-12-08 06:46:31 -0800722/**
723 * intel_fbc_init - Initialize FBC
724 * @dev_priv: the i915 device
725 *
726 * This function might be called during PM init process.
727 */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200728void intel_fbc_init(struct drm_i915_private *dev_priv)
729{
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200730 enum pipe pipe;
731
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200732 if (!HAS_FBC(dev_priv)) {
733 dev_priv->fbc.enabled = false;
Paulo Zanoni104618b2015-02-09 14:46:28 -0200734 dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED;
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200735 return;
736 }
737
Paulo Zanonidbef0f12015-02-13 17:23:46 -0200738 for_each_pipe(dev_priv, pipe) {
739 dev_priv->fbc.possible_framebuffer_bits |=
740 INTEL_FRONTBUFFER_PRIMARY(pipe);
741
742 if (IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8)
743 break;
744 }
745
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -0200746 if (INTEL_INFO(dev_priv)->gen >= 7) {
747 dev_priv->display.fbc_enabled = ilk_fbc_enabled;
748 dev_priv->display.enable_fbc = gen7_fbc_enable;
749 dev_priv->display.disable_fbc = ilk_fbc_disable;
750 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
751 dev_priv->display.fbc_enabled = ilk_fbc_enabled;
752 dev_priv->display.enable_fbc = ilk_fbc_enable;
753 dev_priv->display.disable_fbc = ilk_fbc_disable;
754 } else if (IS_GM45(dev_priv)) {
755 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
756 dev_priv->display.enable_fbc = g4x_fbc_enable;
757 dev_priv->display.disable_fbc = g4x_fbc_disable;
758 } else {
759 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
760 dev_priv->display.enable_fbc = i8xx_fbc_enable;
761 dev_priv->display.disable_fbc = i8xx_fbc_disable;
762
763 /* This value was pulled out of someone's hat */
764 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
765 }
766
767 dev_priv->fbc.enabled = dev_priv->display.fbc_enabled(dev_priv->dev);
768}