xref: /dragonfly/sys/dev/drm/i915/intel_fbc.c (revision 0212bfce)
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 /**
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.
30  *
31  * The benefits of FBC are mostly visible with solid backgrounds and
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.
34  *
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.
39  */
40 
41 #include "intel_drv.h"
42 #include "i915_drv.h"
43 
44 static inline bool fbc_supported(struct drm_i915_private *dev_priv)
45 {
46 	return HAS_FBC(dev_priv);
47 }
48 
49 static inline bool fbc_on_pipe_a_only(struct drm_i915_private *dev_priv)
50 {
51 	return IS_HASWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 8;
52 }
53 
54 static inline bool fbc_on_plane_a_only(struct drm_i915_private *dev_priv)
55 {
56 	return INTEL_INFO(dev_priv)->gen < 4;
57 }
58 
59 static inline bool no_fbc_on_multiple_pipes(struct drm_i915_private *dev_priv)
60 {
61 	return INTEL_INFO(dev_priv)->gen <= 3;
62 }
63 
64 /*
65  * In some platforms where the CRTC's x:0/y:0 coordinates doesn't match the
66  * frontbuffer's x:0/y:0 coordinates we lie to the hardware about the plane's
67  * origin so the x and y offsets can actually fit the registers. As a
68  * consequence, the fence doesn't really start exactly at the display plane
69  * address we program because it starts at the real start of the buffer, so we
70  * have to take this into consideration here.
71  */
72 static unsigned int get_crtc_fence_y_offset(struct intel_crtc *crtc)
73 {
74 	return crtc->base.y - crtc->adjusted_y;
75 }
76 
77 /*
78  * For SKL+, the plane source size used by the hardware is based on the value we
79  * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value
80  * we wrote to PIPESRC.
81  */
82 static void intel_fbc_get_plane_source_size(struct intel_fbc_state_cache *cache,
83 					    int *width, int *height)
84 {
85 	int w, h;
86 
87 	if (intel_rotation_90_or_270(cache->plane.rotation)) {
88 		w = cache->plane.src_h;
89 		h = cache->plane.src_w;
90 	} else {
91 		w = cache->plane.src_w;
92 		h = cache->plane.src_h;
93 	}
94 
95 	if (width)
96 		*width = w;
97 	if (height)
98 		*height = h;
99 }
100 
101 static int intel_fbc_calculate_cfb_size(struct drm_i915_private *dev_priv,
102 					struct intel_fbc_state_cache *cache)
103 {
104 	int lines;
105 
106 	intel_fbc_get_plane_source_size(cache, NULL, &lines);
107 	if (INTEL_GEN(dev_priv) == 7)
108 		lines = min(lines, 2048);
109 	else if (INTEL_GEN(dev_priv) >= 8)
110 		lines = min(lines, 2560);
111 
112 	/* Hardware needs the full buffer stride, not just the active area. */
113 	return lines * cache->fb.stride;
114 }
115 
116 static void i8xx_fbc_deactivate(struct drm_i915_private *dev_priv)
117 {
118 	u32 fbc_ctl;
119 
120 	/* Disable compression */
121 	fbc_ctl = I915_READ(FBC_CONTROL);
122 	if ((fbc_ctl & FBC_CTL_EN) == 0)
123 		return;
124 
125 	fbc_ctl &= ~FBC_CTL_EN;
126 	I915_WRITE(FBC_CONTROL, fbc_ctl);
127 
128 	/* Wait for compressing bit to clear */
129 	if (intel_wait_for_register(dev_priv,
130 				    FBC_STATUS, FBC_STAT_COMPRESSING, 0,
131 				    10)) {
132 		DRM_DEBUG_KMS("FBC idle timed out\n");
133 		return;
134 	}
135 }
136 
137 static void i8xx_fbc_activate(struct drm_i915_private *dev_priv)
138 {
139 	struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
140 	int cfb_pitch;
141 	int i;
142 	u32 fbc_ctl;
143 
144 	/* Note: fbc.threshold == 1 for i8xx */
145 	cfb_pitch = params->cfb_size / FBC_LL_SIZE;
146 	if (params->fb.stride < cfb_pitch)
147 		cfb_pitch = params->fb.stride;
148 
149 	/* FBC_CTL wants 32B or 64B units */
150 	if (IS_GEN2(dev_priv))
151 		cfb_pitch = (cfb_pitch / 32) - 1;
152 	else
153 		cfb_pitch = (cfb_pitch / 64) - 1;
154 
155 	/* Clear old tags */
156 	for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
157 		I915_WRITE(FBC_TAG(i), 0);
158 
159 	if (IS_GEN4(dev_priv)) {
160 		u32 fbc_ctl2;
161 
162 		/* Set it up... */
163 		fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
164 		fbc_ctl2 |= FBC_CTL_PLANE(params->crtc.plane);
165 		I915_WRITE(FBC_CONTROL2, fbc_ctl2);
166 		I915_WRITE(FBC_FENCE_OFF, params->crtc.fence_y_offset);
167 	}
168 
169 	/* enable it... */
170 	fbc_ctl = I915_READ(FBC_CONTROL);
171 	fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
172 	fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
173 	if (IS_I945GM(dev_priv))
174 		fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
175 	fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
176 	fbc_ctl |= params->fb.fence_reg;
177 	I915_WRITE(FBC_CONTROL, fbc_ctl);
178 }
179 
180 static bool i8xx_fbc_is_active(struct drm_i915_private *dev_priv)
181 {
182 	return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
183 }
184 
185 static void g4x_fbc_activate(struct drm_i915_private *dev_priv)
186 {
187 	struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
188 	u32 dpfc_ctl;
189 
190 	dpfc_ctl = DPFC_CTL_PLANE(params->crtc.plane) | DPFC_SR_EN;
191 	if (drm_format_plane_cpp(params->fb.pixel_format, 0) == 2)
192 		dpfc_ctl |= DPFC_CTL_LIMIT_2X;
193 	else
194 		dpfc_ctl |= DPFC_CTL_LIMIT_1X;
195 	dpfc_ctl |= DPFC_CTL_FENCE_EN | params->fb.fence_reg;
196 
197 	I915_WRITE(DPFC_FENCE_YOFF, params->crtc.fence_y_offset);
198 
199 	/* enable it... */
200 	I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
201 }
202 
203 static void g4x_fbc_deactivate(struct drm_i915_private *dev_priv)
204 {
205 	u32 dpfc_ctl;
206 
207 	/* Disable compression */
208 	dpfc_ctl = I915_READ(DPFC_CONTROL);
209 	if (dpfc_ctl & DPFC_CTL_EN) {
210 		dpfc_ctl &= ~DPFC_CTL_EN;
211 		I915_WRITE(DPFC_CONTROL, dpfc_ctl);
212 	}
213 }
214 
215 static bool g4x_fbc_is_active(struct drm_i915_private *dev_priv)
216 {
217 	return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
218 }
219 
220 /* This function forces a CFB recompression through the nuke operation. */
221 static void intel_fbc_recompress(struct drm_i915_private *dev_priv)
222 {
223 	I915_WRITE(MSG_FBC_REND_STATE, FBC_REND_NUKE);
224 	POSTING_READ(MSG_FBC_REND_STATE);
225 }
226 
227 static void ilk_fbc_activate(struct drm_i915_private *dev_priv)
228 {
229 	struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
230 	u32 dpfc_ctl;
231 	int threshold = dev_priv->fbc.threshold;
232 
233 	dpfc_ctl = DPFC_CTL_PLANE(params->crtc.plane);
234 	if (drm_format_plane_cpp(params->fb.pixel_format, 0) == 2)
235 		threshold++;
236 
237 	switch (threshold) {
238 	case 4:
239 	case 3:
240 		dpfc_ctl |= DPFC_CTL_LIMIT_4X;
241 		break;
242 	case 2:
243 		dpfc_ctl |= DPFC_CTL_LIMIT_2X;
244 		break;
245 	case 1:
246 		dpfc_ctl |= DPFC_CTL_LIMIT_1X;
247 		break;
248 	}
249 	dpfc_ctl |= DPFC_CTL_FENCE_EN;
250 	if (IS_GEN5(dev_priv))
251 		dpfc_ctl |= params->fb.fence_reg;
252 
253 	I915_WRITE(ILK_DPFC_FENCE_YOFF, params->crtc.fence_y_offset);
254 	I915_WRITE(ILK_FBC_RT_BASE, params->fb.ggtt_offset | ILK_FBC_RT_VALID);
255 	/* enable it... */
256 	I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
257 
258 	if (IS_GEN6(dev_priv)) {
259 		I915_WRITE(SNB_DPFC_CTL_SA,
260 			   SNB_CPU_FENCE_ENABLE | params->fb.fence_reg);
261 		I915_WRITE(DPFC_CPU_FENCE_OFFSET, params->crtc.fence_y_offset);
262 	}
263 
264 	intel_fbc_recompress(dev_priv);
265 }
266 
267 static void ilk_fbc_deactivate(struct drm_i915_private *dev_priv)
268 {
269 	u32 dpfc_ctl;
270 
271 	/* Disable compression */
272 	dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
273 	if (dpfc_ctl & DPFC_CTL_EN) {
274 		dpfc_ctl &= ~DPFC_CTL_EN;
275 		I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
276 	}
277 }
278 
279 static bool ilk_fbc_is_active(struct drm_i915_private *dev_priv)
280 {
281 	return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
282 }
283 
284 static void gen7_fbc_activate(struct drm_i915_private *dev_priv)
285 {
286 	struct intel_fbc_reg_params *params = &dev_priv->fbc.params;
287 	u32 dpfc_ctl;
288 	int threshold = dev_priv->fbc.threshold;
289 
290 	dpfc_ctl = 0;
291 	if (IS_IVYBRIDGE(dev_priv))
292 		dpfc_ctl |= IVB_DPFC_CTL_PLANE(params->crtc.plane);
293 
294 	if (drm_format_plane_cpp(params->fb.pixel_format, 0) == 2)
295 		threshold++;
296 
297 	switch (threshold) {
298 	case 4:
299 	case 3:
300 		dpfc_ctl |= DPFC_CTL_LIMIT_4X;
301 		break;
302 	case 2:
303 		dpfc_ctl |= DPFC_CTL_LIMIT_2X;
304 		break;
305 	case 1:
306 		dpfc_ctl |= DPFC_CTL_LIMIT_1X;
307 		break;
308 	}
309 
310 	dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
311 
312 	if (dev_priv->fbc.false_color)
313 		dpfc_ctl |= FBC_CTL_FALSE_COLOR;
314 
315 	if (IS_IVYBRIDGE(dev_priv)) {
316 		/* WaFbcAsynchFlipDisableFbcQueue:ivb */
317 		I915_WRITE(ILK_DISPLAY_CHICKEN1,
318 			   I915_READ(ILK_DISPLAY_CHICKEN1) |
319 			   ILK_FBCQ_DIS);
320 	} else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
321 		/* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
322 		I915_WRITE(CHICKEN_PIPESL_1(params->crtc.pipe),
323 			   I915_READ(CHICKEN_PIPESL_1(params->crtc.pipe)) |
324 			   HSW_FBCQ_DIS);
325 	}
326 
327 	I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
328 
329 	I915_WRITE(SNB_DPFC_CTL_SA,
330 		   SNB_CPU_FENCE_ENABLE | params->fb.fence_reg);
331 	I915_WRITE(DPFC_CPU_FENCE_OFFSET, params->crtc.fence_y_offset);
332 
333 	intel_fbc_recompress(dev_priv);
334 }
335 
336 static bool intel_fbc_hw_is_active(struct drm_i915_private *dev_priv)
337 {
338 	if (INTEL_INFO(dev_priv)->gen >= 5)
339 		return ilk_fbc_is_active(dev_priv);
340 	else if (IS_GM45(dev_priv))
341 		return g4x_fbc_is_active(dev_priv);
342 	else
343 		return i8xx_fbc_is_active(dev_priv);
344 }
345 
346 static void intel_fbc_hw_activate(struct drm_i915_private *dev_priv)
347 {
348 	struct intel_fbc *fbc = &dev_priv->fbc;
349 
350 	fbc->active = true;
351 
352 	if (INTEL_INFO(dev_priv)->gen >= 7)
353 		gen7_fbc_activate(dev_priv);
354 	else if (INTEL_INFO(dev_priv)->gen >= 5)
355 		ilk_fbc_activate(dev_priv);
356 	else if (IS_GM45(dev_priv))
357 		g4x_fbc_activate(dev_priv);
358 	else
359 		i8xx_fbc_activate(dev_priv);
360 }
361 
362 static void intel_fbc_hw_deactivate(struct drm_i915_private *dev_priv)
363 {
364 	struct intel_fbc *fbc = &dev_priv->fbc;
365 
366 	fbc->active = false;
367 
368 	if (INTEL_INFO(dev_priv)->gen >= 5)
369 		ilk_fbc_deactivate(dev_priv);
370 	else if (IS_GM45(dev_priv))
371 		g4x_fbc_deactivate(dev_priv);
372 	else
373 		i8xx_fbc_deactivate(dev_priv);
374 }
375 
376 /**
377  * intel_fbc_is_active - Is FBC active?
378  * @dev_priv: i915 device instance
379  *
380  * This function is used to verify the current state of FBC.
381  *
382  * FIXME: This should be tracked in the plane config eventually
383  * instead of queried at runtime for most callers.
384  */
385 bool intel_fbc_is_active(struct drm_i915_private *dev_priv)
386 {
387 	return dev_priv->fbc.active;
388 }
389 
390 static void intel_fbc_work_fn(struct work_struct *__work)
391 {
392 	struct drm_i915_private *dev_priv =
393 		container_of(__work, struct drm_i915_private, fbc.work.work);
394 	struct intel_fbc *fbc = &dev_priv->fbc;
395 	struct intel_fbc_work *work = &fbc->work;
396 	struct intel_crtc *crtc = fbc->crtc;
397 	struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[crtc->pipe];
398 
399 	if (drm_crtc_vblank_get(&crtc->base)) {
400 		DRM_ERROR("vblank not available for FBC on pipe %c\n",
401 			  pipe_name(crtc->pipe));
402 
403 		mutex_lock(&fbc->lock);
404 		work->scheduled = false;
405 		mutex_unlock(&fbc->lock);
406 		return;
407 	}
408 
409 retry:
410 	/* Delay the actual enabling to let pageflipping cease and the
411 	 * display to settle before starting the compression. Note that
412 	 * this delay also serves a second purpose: it allows for a
413 	 * vblank to pass after disabling the FBC before we attempt
414 	 * to modify the control registers.
415 	 *
416 	 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
417 	 *
418 	 * It is also worth mentioning that since work->scheduled_vblank can be
419 	 * updated multiple times by the other threads, hitting the timeout is
420 	 * not an error condition. We'll just end up hitting the "goto retry"
421 	 * case below.
422 	 */
423 	wait_event_timeout(vblank->queue,
424 		drm_crtc_vblank_count(&crtc->base) != work->scheduled_vblank,
425 		msecs_to_jiffies(50));
426 
427 	mutex_lock(&fbc->lock);
428 
429 	/* Were we cancelled? */
430 	if (!work->scheduled)
431 		goto out;
432 
433 	/* Were we delayed again while this function was sleeping? */
434 	if (drm_crtc_vblank_count(&crtc->base) == work->scheduled_vblank) {
435 		mutex_unlock(&fbc->lock);
436 		goto retry;
437 	}
438 
439 	intel_fbc_hw_activate(dev_priv);
440 
441 	work->scheduled = false;
442 
443 out:
444 	mutex_unlock(&fbc->lock);
445 	drm_crtc_vblank_put(&crtc->base);
446 }
447 
448 static void intel_fbc_schedule_activation(struct intel_crtc *crtc)
449 {
450 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
451 	struct intel_fbc *fbc = &dev_priv->fbc;
452 	struct intel_fbc_work *work = &fbc->work;
453 
454 	WARN_ON(!mutex_is_locked(&fbc->lock));
455 
456 	if (drm_crtc_vblank_get(&crtc->base)) {
457 		DRM_ERROR("vblank not available for FBC on pipe %c\n",
458 			  pipe_name(crtc->pipe));
459 		return;
460 	}
461 
462 	/* It is useless to call intel_fbc_cancel_work() or cancel_work() in
463 	 * this function since we're not releasing fbc.lock, so it won't have an
464 	 * opportunity to grab it to discover that it was cancelled. So we just
465 	 * update the expected jiffy count. */
466 	work->scheduled = true;
467 	work->scheduled_vblank = drm_crtc_vblank_count(&crtc->base);
468 	drm_crtc_vblank_put(&crtc->base);
469 
470 	schedule_work(&work->work);
471 }
472 
473 static void intel_fbc_deactivate(struct drm_i915_private *dev_priv)
474 {
475 	struct intel_fbc *fbc = &dev_priv->fbc;
476 
477 	WARN_ON(!mutex_is_locked(&fbc->lock));
478 
479 	/* Calling cancel_work() here won't help due to the fact that the work
480 	 * function grabs fbc->lock. Just set scheduled to false so the work
481 	 * function can know it was cancelled. */
482 	fbc->work.scheduled = false;
483 
484 	if (fbc->active)
485 		intel_fbc_hw_deactivate(dev_priv);
486 }
487 
488 static bool multiple_pipes_ok(struct intel_crtc *crtc,
489 			      struct intel_plane_state *plane_state)
490 {
491 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
492 	struct intel_fbc *fbc = &dev_priv->fbc;
493 	enum i915_pipe pipe = crtc->pipe;
494 
495 	/* Don't even bother tracking anything we don't need. */
496 	if (!no_fbc_on_multiple_pipes(dev_priv))
497 		return true;
498 
499 	if (plane_state->visible)
500 		fbc->visible_pipes_mask |= (1 << pipe);
501 	else
502 		fbc->visible_pipes_mask &= ~(1 << pipe);
503 
504 	return (fbc->visible_pipes_mask & ~(1 << pipe)) != 0;
505 }
506 
507 static int find_compression_threshold(struct drm_i915_private *dev_priv,
508 				      struct drm_mm_node *node,
509 				      int size,
510 				      int fb_cpp)
511 {
512 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
513 	int compression_threshold = 1;
514 	int ret;
515 	u64 end;
516 
517 	/* The FBC hardware for BDW/SKL doesn't have access to the stolen
518 	 * reserved range size, so it always assumes the maximum (8mb) is used.
519 	 * If we enable FBC using a CFB on that memory range we'll get FIFO
520 	 * underruns, even if that range is not reserved by the BIOS. */
521 	if (IS_BROADWELL(dev_priv) ||
522 	    IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
523 		end = ggtt->stolen_size - 8 * 1024 * 1024;
524 	else
525 		end = ggtt->stolen_usable_size;
526 
527 	/* HACK: This code depends on what we will do in *_enable_fbc. If that
528 	 * code changes, this code needs to change as well.
529 	 *
530 	 * The enable_fbc code will attempt to use one of our 2 compression
531 	 * thresholds, therefore, in that case, we only have 1 resort.
532 	 */
533 
534 	/* Try to over-allocate to reduce reallocations and fragmentation. */
535 	ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size <<= 1,
536 						   4096, 0, end);
537 	if (ret == 0)
538 		return compression_threshold;
539 
540 again:
541 	/* HW's ability to limit the CFB is 1:4 */
542 	if (compression_threshold > 4 ||
543 	    (fb_cpp == 2 && compression_threshold == 2))
544 		return 0;
545 
546 	ret = i915_gem_stolen_insert_node_in_range(dev_priv, node, size >>= 1,
547 						   4096, 0, end);
548 	if (ret && INTEL_INFO(dev_priv)->gen <= 4) {
549 		return 0;
550 	} else if (ret) {
551 		compression_threshold <<= 1;
552 		goto again;
553 	} else {
554 		return compression_threshold;
555 	}
556 }
557 
558 static int intel_fbc_alloc_cfb(struct intel_crtc *crtc)
559 {
560 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
561 	struct intel_fbc *fbc = &dev_priv->fbc;
562 	struct drm_mm_node *compressed_llb = NULL;
563 	int size, fb_cpp, ret;
564 
565 	WARN_ON(drm_mm_node_allocated(&fbc->compressed_fb));
566 
567 	size = intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache);
568 	fb_cpp = drm_format_plane_cpp(fbc->state_cache.fb.pixel_format, 0);
569 
570 	ret = find_compression_threshold(dev_priv, &fbc->compressed_fb,
571 					 size, fb_cpp);
572 	if (!ret)
573 		goto err_llb;
574 	else if (ret > 1) {
575 		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");
576 
577 	}
578 
579 	fbc->threshold = ret;
580 
581 	if (INTEL_INFO(dev_priv)->gen >= 5)
582 		I915_WRITE(ILK_DPFC_CB_BASE, fbc->compressed_fb.start);
583 	else if (IS_GM45(dev_priv)) {
584 		I915_WRITE(DPFC_CB_BASE, fbc->compressed_fb.start);
585 	} else {
586 		compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
587 		if (!compressed_llb)
588 			goto err_fb;
589 
590 		ret = i915_gem_stolen_insert_node(dev_priv, compressed_llb,
591 						  4096, 4096);
592 		if (ret)
593 			goto err_fb;
594 
595 		fbc->compressed_llb = compressed_llb;
596 
597 		I915_WRITE(FBC_CFB_BASE,
598 			   dev_priv->mm.stolen_base + fbc->compressed_fb.start);
599 		I915_WRITE(FBC_LL_BASE,
600 			   dev_priv->mm.stolen_base + compressed_llb->start);
601 	}
602 
603 	DRM_DEBUG_KMS("reserved %llu bytes of contiguous stolen space for FBC, threshold: %d\n",
604 		      fbc->compressed_fb.size, fbc->threshold);
605 
606 	return 0;
607 
608 err_fb:
609 	kfree(compressed_llb);
610 	i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
611 err_llb:
612 	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);
613 	return -ENOSPC;
614 }
615 
616 static void __intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
617 {
618 	struct intel_fbc *fbc = &dev_priv->fbc;
619 
620 	if (drm_mm_node_allocated(&fbc->compressed_fb))
621 		i915_gem_stolen_remove_node(dev_priv, &fbc->compressed_fb);
622 
623 	if (fbc->compressed_llb) {
624 		i915_gem_stolen_remove_node(dev_priv, fbc->compressed_llb);
625 		kfree(fbc->compressed_llb);
626 	}
627 }
628 
629 void intel_fbc_cleanup_cfb(struct drm_i915_private *dev_priv)
630 {
631 	struct intel_fbc *fbc = &dev_priv->fbc;
632 
633 	if (!fbc_supported(dev_priv))
634 		return;
635 
636 	mutex_lock(&fbc->lock);
637 	__intel_fbc_cleanup_cfb(dev_priv);
638 	mutex_unlock(&fbc->lock);
639 }
640 
641 static bool stride_is_valid(struct drm_i915_private *dev_priv,
642 			    unsigned int stride)
643 {
644 	/* These should have been caught earlier. */
645 	WARN_ON(stride < 512);
646 	WARN_ON((stride & (64 - 1)) != 0);
647 
648 	/* Below are the additional FBC restrictions. */
649 
650 	if (IS_GEN2(dev_priv) || IS_GEN3(dev_priv))
651 		return stride == 4096 || stride == 8192;
652 
653 	if (IS_GEN4(dev_priv) && !IS_G4X(dev_priv) && stride < 2048)
654 		return false;
655 
656 	if (stride > 16384)
657 		return false;
658 
659 	return true;
660 }
661 
662 static bool pixel_format_is_valid(struct drm_i915_private *dev_priv,
663 				  uint32_t pixel_format)
664 {
665 	switch (pixel_format) {
666 	case DRM_FORMAT_XRGB8888:
667 	case DRM_FORMAT_XBGR8888:
668 		return true;
669 	case DRM_FORMAT_XRGB1555:
670 	case DRM_FORMAT_RGB565:
671 		/* 16bpp not supported on gen2 */
672 		if (IS_GEN2(dev_priv))
673 			return false;
674 		/* WaFbcOnly1to1Ratio:ctg */
675 		if (IS_G4X(dev_priv))
676 			return false;
677 		return true;
678 	default:
679 		return false;
680 	}
681 }
682 
683 /*
684  * For some reason, the hardware tracking starts looking at whatever we
685  * programmed as the display plane base address register. It does not look at
686  * the X and Y offset registers. That's why we look at the crtc->adjusted{x,y}
687  * variables instead of just looking at the pipe/plane size.
688  */
689 static bool intel_fbc_hw_tracking_covers_screen(struct intel_crtc *crtc)
690 {
691 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
692 	struct intel_fbc *fbc = &dev_priv->fbc;
693 	unsigned int effective_w, effective_h, max_w, max_h;
694 
695 	if (INTEL_INFO(dev_priv)->gen >= 8 || IS_HASWELL(dev_priv)) {
696 		max_w = 4096;
697 		max_h = 4096;
698 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
699 		max_w = 4096;
700 		max_h = 2048;
701 	} else {
702 		max_w = 2048;
703 		max_h = 1536;
704 	}
705 
706 	intel_fbc_get_plane_source_size(&fbc->state_cache, &effective_w,
707 					&effective_h);
708 	effective_w += crtc->adjusted_x;
709 	effective_h += crtc->adjusted_y;
710 
711 	return effective_w <= max_w && effective_h <= max_h;
712 }
713 
714 static void intel_fbc_update_state_cache(struct intel_crtc *crtc,
715 					 struct intel_crtc_state *crtc_state,
716 					 struct intel_plane_state *plane_state)
717 {
718 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
719 	struct intel_fbc *fbc = &dev_priv->fbc;
720 	struct intel_fbc_state_cache *cache = &fbc->state_cache;
721 	struct drm_framebuffer *fb = plane_state->base.fb;
722 	struct drm_i915_gem_object *obj;
723 
724 	cache->crtc.mode_flags = crtc_state->base.adjusted_mode.flags;
725 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
726 		cache->crtc.hsw_bdw_pixel_rate =
727 			ilk_pipe_pixel_rate(crtc_state);
728 
729 	cache->plane.rotation = plane_state->base.rotation;
730 	cache->plane.src_w = drm_rect_width(&plane_state->src) >> 16;
731 	cache->plane.src_h = drm_rect_height(&plane_state->src) >> 16;
732 	cache->plane.visible = plane_state->visible;
733 
734 	if (!cache->plane.visible)
735 		return;
736 
737 	obj = intel_fb_obj(fb);
738 
739 	/* FIXME: We lack the proper locking here, so only run this on the
740 	 * platforms that need. */
741 	if (IS_GEN(dev_priv, 5, 6))
742 		cache->fb.ilk_ggtt_offset = i915_gem_obj_ggtt_offset(obj);
743 	cache->fb.pixel_format = fb->pixel_format;
744 	cache->fb.stride = fb->pitches[0];
745 	cache->fb.fence_reg = obj->fence_reg;
746 	cache->fb.tiling_mode = obj->tiling_mode;
747 }
748 
749 static bool intel_fbc_can_activate(struct intel_crtc *crtc)
750 {
751 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
752 	struct intel_fbc *fbc = &dev_priv->fbc;
753 	struct intel_fbc_state_cache *cache = &fbc->state_cache;
754 
755 	if (!cache->plane.visible) {
756 		fbc->no_fbc_reason = "primary plane not visible";
757 		return false;
758 	}
759 
760 	if ((cache->crtc.mode_flags & DRM_MODE_FLAG_INTERLACE) ||
761 	    (cache->crtc.mode_flags & DRM_MODE_FLAG_DBLSCAN)) {
762 		fbc->no_fbc_reason = "incompatible mode";
763 		return false;
764 	}
765 
766 	if (!intel_fbc_hw_tracking_covers_screen(crtc)) {
767 		fbc->no_fbc_reason = "mode too large for compression";
768 		return false;
769 	}
770 
771 	/* The use of a CPU fence is mandatory in order to detect writes
772 	 * by the CPU to the scanout and trigger updates to the FBC.
773 	 */
774 	if (cache->fb.tiling_mode != I915_TILING_X ||
775 	    cache->fb.fence_reg == I915_FENCE_REG_NONE) {
776 		fbc->no_fbc_reason = "framebuffer not tiled or fenced";
777 		return false;
778 	}
779 	if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_G4X(dev_priv) &&
780 	    cache->plane.rotation != DRM_ROTATE_0) {
781 		fbc->no_fbc_reason = "rotation unsupported";
782 		return false;
783 	}
784 
785 	if (!stride_is_valid(dev_priv, cache->fb.stride)) {
786 		fbc->no_fbc_reason = "framebuffer stride not supported";
787 		return false;
788 	}
789 
790 	if (!pixel_format_is_valid(dev_priv, cache->fb.pixel_format)) {
791 		fbc->no_fbc_reason = "pixel format is invalid";
792 		return false;
793 	}
794 
795 	/* WaFbcExceedCdClockThreshold:hsw,bdw */
796 	if ((IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) &&
797 	    cache->crtc.hsw_bdw_pixel_rate >= dev_priv->cdclk_freq * 95 / 100) {
798 		fbc->no_fbc_reason = "pixel rate is too big";
799 		return false;
800 	}
801 
802 	/* It is possible for the required CFB size change without a
803 	 * crtc->disable + crtc->enable since it is possible to change the
804 	 * stride without triggering a full modeset. Since we try to
805 	 * over-allocate the CFB, there's a chance we may keep FBC enabled even
806 	 * if this happens, but if we exceed the current CFB size we'll have to
807 	 * disable FBC. Notice that it would be possible to disable FBC, wait
808 	 * for a frame, free the stolen node, then try to reenable FBC in case
809 	 * we didn't get any invalidate/deactivate calls, but this would require
810 	 * a lot of tracking just for a specific case. If we conclude it's an
811 	 * important case, we can implement it later. */
812 	if (intel_fbc_calculate_cfb_size(dev_priv, &fbc->state_cache) >
813 	    fbc->compressed_fb.size * fbc->threshold) {
814 		fbc->no_fbc_reason = "CFB requirements changed";
815 		return false;
816 	}
817 
818 	return true;
819 }
820 
821 static bool intel_fbc_can_choose(struct intel_crtc *crtc)
822 {
823 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
824 	struct intel_fbc *fbc = &dev_priv->fbc;
825 
826 	if (intel_vgpu_active(dev_priv)) {
827 		fbc->no_fbc_reason = "VGPU is active";
828 		return false;
829 	}
830 
831 	if (!i915.enable_fbc) {
832 		fbc->no_fbc_reason = "disabled per module param or by default";
833 		return false;
834 	}
835 
836 	if (fbc_on_pipe_a_only(dev_priv) && crtc->pipe != PIPE_A) {
837 		fbc->no_fbc_reason = "no enabled pipes can have FBC";
838 		return false;
839 	}
840 
841 	if (fbc_on_plane_a_only(dev_priv) && crtc->plane != PLANE_A) {
842 		fbc->no_fbc_reason = "no enabled planes can have FBC";
843 		return false;
844 	}
845 
846 	return true;
847 }
848 
849 static void intel_fbc_get_reg_params(struct intel_crtc *crtc,
850 				     struct intel_fbc_reg_params *params)
851 {
852 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
853 	struct intel_fbc *fbc = &dev_priv->fbc;
854 	struct intel_fbc_state_cache *cache = &fbc->state_cache;
855 
856 	/* Since all our fields are integer types, use memset here so the
857 	 * comparison function can rely on memcmp because the padding will be
858 	 * zero. */
859 	memset(params, 0, sizeof(*params));
860 
861 	params->crtc.pipe = crtc->pipe;
862 	params->crtc.plane = crtc->plane;
863 	params->crtc.fence_y_offset = get_crtc_fence_y_offset(crtc);
864 
865 	params->fb.pixel_format = cache->fb.pixel_format;
866 	params->fb.stride = cache->fb.stride;
867 	params->fb.fence_reg = cache->fb.fence_reg;
868 
869 	params->cfb_size = intel_fbc_calculate_cfb_size(dev_priv, cache);
870 
871 	params->fb.ggtt_offset = cache->fb.ilk_ggtt_offset;
872 }
873 
874 static bool intel_fbc_reg_params_equal(struct intel_fbc_reg_params *params1,
875 				       struct intel_fbc_reg_params *params2)
876 {
877 	/* We can use this since intel_fbc_get_reg_params() does a memset. */
878 	return memcmp(params1, params2, sizeof(*params1)) == 0;
879 }
880 
881 void intel_fbc_pre_update(struct intel_crtc *crtc,
882 			  struct intel_crtc_state *crtc_state,
883 			  struct intel_plane_state *plane_state)
884 {
885 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
886 	struct intel_fbc *fbc = &dev_priv->fbc;
887 
888 	if (!fbc_supported(dev_priv))
889 		return;
890 
891 	mutex_lock(&fbc->lock);
892 
893 	if (!multiple_pipes_ok(crtc, plane_state)) {
894 		fbc->no_fbc_reason = "more than one pipe active";
895 		goto deactivate;
896 	}
897 
898 	if (!fbc->enabled || fbc->crtc != crtc)
899 		goto unlock;
900 
901 	intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
902 
903 deactivate:
904 	intel_fbc_deactivate(dev_priv);
905 unlock:
906 	mutex_unlock(&fbc->lock);
907 }
908 
909 static void __intel_fbc_post_update(struct intel_crtc *crtc)
910 {
911 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
912 	struct intel_fbc *fbc = &dev_priv->fbc;
913 	struct intel_fbc_reg_params old_params;
914 
915 	WARN_ON(!mutex_is_locked(&fbc->lock));
916 
917 	if (!fbc->enabled || fbc->crtc != crtc)
918 		return;
919 
920 	if (!intel_fbc_can_activate(crtc)) {
921 		WARN_ON(fbc->active);
922 		return;
923 	}
924 
925 	old_params = fbc->params;
926 	intel_fbc_get_reg_params(crtc, &fbc->params);
927 
928 	/* If the scanout has not changed, don't modify the FBC settings.
929 	 * Note that we make the fundamental assumption that the fb->obj
930 	 * cannot be unpinned (and have its GTT offset and fence revoked)
931 	 * without first being decoupled from the scanout and FBC disabled.
932 	 */
933 	if (fbc->active &&
934 	    intel_fbc_reg_params_equal(&old_params, &fbc->params))
935 		return;
936 
937 	intel_fbc_deactivate(dev_priv);
938 	intel_fbc_schedule_activation(crtc);
939 	fbc->no_fbc_reason = "FBC enabled (active or scheduled)";
940 }
941 
942 void intel_fbc_post_update(struct intel_crtc *crtc)
943 {
944 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
945 	struct intel_fbc *fbc = &dev_priv->fbc;
946 
947 	if (!fbc_supported(dev_priv))
948 		return;
949 
950 	mutex_lock(&fbc->lock);
951 	__intel_fbc_post_update(crtc);
952 	mutex_unlock(&fbc->lock);
953 }
954 
955 static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
956 {
957 	if (fbc->enabled)
958 		return to_intel_plane(fbc->crtc->base.primary)->frontbuffer_bit;
959 	else
960 		return fbc->possible_framebuffer_bits;
961 }
962 
963 void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
964 			  unsigned int frontbuffer_bits,
965 			  enum fb_op_origin origin)
966 {
967 	struct intel_fbc *fbc = &dev_priv->fbc;
968 
969 	if (!fbc_supported(dev_priv))
970 		return;
971 
972 	if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
973 		return;
974 
975 	mutex_lock(&fbc->lock);
976 
977 	fbc->busy_bits |= intel_fbc_get_frontbuffer_bit(fbc) & frontbuffer_bits;
978 
979 	if (fbc->enabled && fbc->busy_bits)
980 		intel_fbc_deactivate(dev_priv);
981 
982 	mutex_unlock(&fbc->lock);
983 }
984 
985 void intel_fbc_flush(struct drm_i915_private *dev_priv,
986 		     unsigned int frontbuffer_bits, enum fb_op_origin origin)
987 {
988 	struct intel_fbc *fbc = &dev_priv->fbc;
989 
990 	if (!fbc_supported(dev_priv))
991 		return;
992 
993 	mutex_lock(&fbc->lock);
994 
995 	fbc->busy_bits &= ~frontbuffer_bits;
996 
997 	if (origin == ORIGIN_GTT || origin == ORIGIN_FLIP)
998 		goto out;
999 
1000 	if (!fbc->busy_bits && fbc->enabled &&
1001 	    (frontbuffer_bits & intel_fbc_get_frontbuffer_bit(fbc))) {
1002 		if (fbc->active)
1003 			intel_fbc_recompress(dev_priv);
1004 		else
1005 			__intel_fbc_post_update(fbc->crtc);
1006 	}
1007 
1008 out:
1009 	mutex_unlock(&fbc->lock);
1010 }
1011 
1012 /**
1013  * intel_fbc_choose_crtc - select a CRTC to enable FBC on
1014  * @dev_priv: i915 device instance
1015  * @state: the atomic state structure
1016  *
1017  * This function looks at the proposed state for CRTCs and planes, then chooses
1018  * which pipe is going to have FBC by setting intel_crtc_state->enable_fbc to
1019  * true.
1020  *
1021  * Later, intel_fbc_enable is going to look for state->enable_fbc and then maybe
1022  * enable FBC for the chosen CRTC. If it does, it will set dev_priv->fbc.crtc.
1023  */
1024 void intel_fbc_choose_crtc(struct drm_i915_private *dev_priv,
1025 			   struct drm_atomic_state *state)
1026 {
1027 	struct intel_fbc *fbc = &dev_priv->fbc;
1028 	struct drm_crtc *crtc;
1029 	struct drm_crtc_state *crtc_state;
1030 	struct drm_plane *plane;
1031 	struct drm_plane_state *plane_state;
1032 	bool fbc_crtc_present = false;
1033 	int i, j;
1034 
1035 	mutex_lock(&fbc->lock);
1036 
1037 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1038 		if (fbc->crtc == to_intel_crtc(crtc)) {
1039 			fbc_crtc_present = true;
1040 			break;
1041 		}
1042 	}
1043 	/* This atomic commit doesn't involve the CRTC currently tied to FBC. */
1044 	if (!fbc_crtc_present && fbc->crtc != NULL)
1045 		goto out;
1046 
1047 	/* Simply choose the first CRTC that is compatible and has a visible
1048 	 * plane. We could go for fancier schemes such as checking the plane
1049 	 * size, but this would just affect the few platforms that don't tie FBC
1050 	 * to pipe or plane A. */
1051 	for_each_plane_in_state(state, plane, plane_state, i) {
1052 		struct intel_plane_state *intel_plane_state =
1053 			to_intel_plane_state(plane_state);
1054 
1055 		if (!intel_plane_state->visible)
1056 			continue;
1057 
1058 		for_each_crtc_in_state(state, crtc, crtc_state, j) {
1059 			struct intel_crtc_state *intel_crtc_state =
1060 				to_intel_crtc_state(crtc_state);
1061 
1062 			if (plane_state->crtc != crtc)
1063 				continue;
1064 
1065 			if (!intel_fbc_can_choose(to_intel_crtc(crtc)))
1066 				break;
1067 
1068 			intel_crtc_state->enable_fbc = true;
1069 			goto out;
1070 		}
1071 	}
1072 
1073 out:
1074 	mutex_unlock(&fbc->lock);
1075 }
1076 
1077 /**
1078  * intel_fbc_enable: tries to enable FBC on the CRTC
1079  * @crtc: the CRTC
1080  *
1081  * This function checks if the given CRTC was chosen for FBC, then enables it if
1082  * possible. Notice that it doesn't activate FBC. It is valid to call
1083  * intel_fbc_enable multiple times for the same pipe without an
1084  * intel_fbc_disable in the middle, as long as it is deactivated.
1085  */
1086 void intel_fbc_enable(struct intel_crtc *crtc,
1087 		      struct intel_crtc_state *crtc_state,
1088 		      struct intel_plane_state *plane_state)
1089 {
1090 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1091 	struct intel_fbc *fbc = &dev_priv->fbc;
1092 
1093 	if (!fbc_supported(dev_priv))
1094 		return;
1095 
1096 	mutex_lock(&fbc->lock);
1097 
1098 	if (fbc->enabled) {
1099 		WARN_ON(fbc->crtc == NULL);
1100 		if (fbc->crtc == crtc) {
1101 			WARN_ON(!crtc_state->enable_fbc);
1102 			WARN_ON(fbc->active);
1103 		}
1104 		goto out;
1105 	}
1106 
1107 	if (!crtc_state->enable_fbc)
1108 		goto out;
1109 
1110 	WARN_ON(fbc->active);
1111 	WARN_ON(fbc->crtc != NULL);
1112 
1113 	intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
1114 	if (intel_fbc_alloc_cfb(crtc)) {
1115 		fbc->no_fbc_reason = "not enough stolen memory";
1116 		goto out;
1117 	}
1118 
1119 	DRM_DEBUG_KMS("Enabling FBC on pipe %c\n", pipe_name(crtc->pipe));
1120 	fbc->no_fbc_reason = "FBC enabled but not active yet\n";
1121 
1122 	fbc->enabled = true;
1123 	fbc->crtc = crtc;
1124 out:
1125 	mutex_unlock(&fbc->lock);
1126 }
1127 
1128 /**
1129  * __intel_fbc_disable - disable FBC
1130  * @dev_priv: i915 device instance
1131  *
1132  * This is the low level function that actually disables FBC. Callers should
1133  * grab the FBC lock.
1134  */
1135 static void __intel_fbc_disable(struct drm_i915_private *dev_priv)
1136 {
1137 	struct intel_fbc *fbc = &dev_priv->fbc;
1138 	struct intel_crtc *crtc = fbc->crtc;
1139 
1140 	WARN_ON(!mutex_is_locked(&fbc->lock));
1141 	WARN_ON(!fbc->enabled);
1142 	WARN_ON(fbc->active);
1143 	WARN_ON(crtc->active);
1144 
1145 	DRM_DEBUG_KMS("Disabling FBC on pipe %c\n", pipe_name(crtc->pipe));
1146 
1147 	__intel_fbc_cleanup_cfb(dev_priv);
1148 
1149 	fbc->enabled = false;
1150 	fbc->crtc = NULL;
1151 }
1152 
1153 /**
1154  * intel_fbc_disable - disable FBC if it's associated with crtc
1155  * @crtc: the CRTC
1156  *
1157  * This function disables FBC if it's associated with the provided CRTC.
1158  */
1159 void intel_fbc_disable(struct intel_crtc *crtc)
1160 {
1161 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1162 	struct intel_fbc *fbc = &dev_priv->fbc;
1163 
1164 	if (!fbc_supported(dev_priv))
1165 		return;
1166 
1167 	mutex_lock(&fbc->lock);
1168 	if (fbc->crtc == crtc) {
1169 		WARN_ON(!fbc->enabled);
1170 		WARN_ON(fbc->active);
1171 		__intel_fbc_disable(dev_priv);
1172 	}
1173 	mutex_unlock(&fbc->lock);
1174 
1175 	cancel_work_sync(&fbc->work.work);
1176 }
1177 
1178 /**
1179  * intel_fbc_global_disable - globally disable FBC
1180  * @dev_priv: i915 device instance
1181  *
1182  * This function disables FBC regardless of which CRTC is associated with it.
1183  */
1184 void intel_fbc_global_disable(struct drm_i915_private *dev_priv)
1185 {
1186 	struct intel_fbc *fbc = &dev_priv->fbc;
1187 
1188 	if (!fbc_supported(dev_priv))
1189 		return;
1190 
1191 	mutex_lock(&fbc->lock);
1192 	if (fbc->enabled)
1193 		__intel_fbc_disable(dev_priv);
1194 	mutex_unlock(&fbc->lock);
1195 
1196 	cancel_work_sync(&fbc->work.work);
1197 }
1198 
1199 /**
1200  * intel_fbc_init_pipe_state - initialize FBC's CRTC visibility tracking
1201  * @dev_priv: i915 device instance
1202  *
1203  * The FBC code needs to track CRTC visibility since the older platforms can't
1204  * have FBC enabled while multiple pipes are used. This function does the
1205  * initial setup at driver load to make sure FBC is matching the real hardware.
1206  */
1207 void intel_fbc_init_pipe_state(struct drm_i915_private *dev_priv)
1208 {
1209 	struct intel_crtc *crtc;
1210 
1211 	/* Don't even bother tracking anything if we don't need. */
1212 	if (!no_fbc_on_multiple_pipes(dev_priv))
1213 		return;
1214 
1215 	for_each_intel_crtc(&dev_priv->drm, crtc)
1216 		if (intel_crtc_active(&crtc->base) &&
1217 		    to_intel_plane_state(crtc->base.primary->state)->visible)
1218 			dev_priv->fbc.visible_pipes_mask |= (1 << crtc->pipe);
1219 }
1220 
1221 /*
1222  * The DDX driver changes its behavior depending on the value it reads from
1223  * i915.enable_fbc, so sanitize it by translating the default value into either
1224  * 0 or 1 in order to allow it to know what's going on.
1225  *
1226  * Notice that this is done at driver initialization and we still allow user
1227  * space to change the value during runtime without sanitizing it again. IGT
1228  * relies on being able to change i915.enable_fbc at runtime.
1229  */
1230 static int intel_sanitize_fbc_option(struct drm_i915_private *dev_priv)
1231 {
1232 	if (i915.enable_fbc >= 0)
1233 		return !!i915.enable_fbc;
1234 
1235 	if (!HAS_FBC(dev_priv))
1236 		return 0;
1237 
1238 	if (IS_BROADWELL(dev_priv))
1239 		return 1;
1240 
1241 	return 0;
1242 }
1243 
1244 static bool need_fbc_vtd_wa(struct drm_i915_private *dev_priv)
1245 {
1246 #ifdef CONFIG_INTEL_IOMMU
1247 	/* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
1248 	if (intel_iommu_gfx_mapped &&
1249 	    (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))) {
1250 		DRM_INFO("Disabling framebuffer compression (FBC) to prevent screen flicker with VT-d enabled\n");
1251 		return true;
1252 	}
1253 #endif
1254 
1255 	return false;
1256 }
1257 
1258 /**
1259  * intel_fbc_init - Initialize FBC
1260  * @dev_priv: the i915 device
1261  *
1262  * This function might be called during PM init process.
1263  */
1264 void intel_fbc_init(struct drm_i915_private *dev_priv)
1265 {
1266 	struct intel_fbc *fbc = &dev_priv->fbc;
1267 	enum i915_pipe pipe;
1268 
1269 	INIT_WORK(&fbc->work.work, intel_fbc_work_fn);
1270 	lockinit(&fbc->lock, "i915fl", 0, LK_CANRECURSE);
1271 	fbc->enabled = false;
1272 	fbc->active = false;
1273 	fbc->work.scheduled = false;
1274 
1275 	if (need_fbc_vtd_wa(dev_priv))
1276 		mkwrite_device_info(dev_priv)->has_fbc = false;
1277 
1278 	i915.enable_fbc = intel_sanitize_fbc_option(dev_priv);
1279 	DRM_DEBUG_KMS("Sanitized enable_fbc value: %d\n", i915.enable_fbc);
1280 
1281 	if (!HAS_FBC(dev_priv)) {
1282 		fbc->no_fbc_reason = "unsupported by this chipset";
1283 		return;
1284 	}
1285 
1286 	for_each_pipe(dev_priv, pipe) {
1287 		fbc->possible_framebuffer_bits |=
1288 				INTEL_FRONTBUFFER_PRIMARY(pipe);
1289 
1290 		if (fbc_on_pipe_a_only(dev_priv))
1291 			break;
1292 	}
1293 
1294 	/* This value was pulled out of someone's hat */
1295 	if (INTEL_INFO(dev_priv)->gen <= 4 && !IS_GM45(dev_priv))
1296 		I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
1297 
1298 	/* We still don't have any sort of hardware state readout for FBC, so
1299 	 * deactivate it in case the BIOS activated it to make sure software
1300 	 * matches the hardware state. */
1301 	if (intel_fbc_hw_is_active(dev_priv))
1302 		intel_fbc_hw_deactivate(dev_priv);
1303 }
1304