intel_fbc.c 46.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright © 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

R
Rodrigo Vivi 已提交
24 25 26 27 28 29
/**
 * DOC: Frame Buffer Compression (FBC)
 *
 * FBC tries to save memory bandwidth (and so power consumption) by
 * compressing the amount of memory used by the display. It is total
 * transparent to user space and completely handled in the kernel.
30 31
 *
 * The benefits of FBC are mostly visible with solid backgrounds and
R
Rodrigo Vivi 已提交
32 33
 * variation-less patterns. It comes from keeping the memory footprint small
 * and having fewer memory pages opened and accessed for refreshing the display.
34
 *
R
Rodrigo Vivi 已提交
35 36 37 38
 * i915 is responsible to reserve stolen memory for FBC and configure its
 * offset on proper registers. The hardware takes care of all
 * compress/decompress. However there are many known cases where we have to
 * forcibly disable it to allow proper screen updates.
39 40
 */

41 42
#include <drm/drm_fourcc.h>

R
Rodrigo Vivi 已提交
43
#include "i915_drv.h"
44
#include "i915_trace.h"
45
#include "i915_vgpu.h"
46
#include "intel_de.h"
47
#include "intel_display_types.h"
48
#include "intel_fbc.h"
49
#include "intel_frontbuffer.h"
R
Rodrigo Vivi 已提交
50

51
struct intel_fbc_funcs {
52 53 54 55 56 57 58
	void (*activate)(struct intel_fbc *fbc);
	void (*deactivate)(struct intel_fbc *fbc);
	bool (*is_active)(struct intel_fbc *fbc);
	bool (*is_compressing)(struct intel_fbc *fbc);
	void (*nuke)(struct intel_fbc *fbc);
	void (*program_cfb)(struct intel_fbc *fbc);
	void (*set_false_color)(struct intel_fbc *fbc, bool enable);
59 60
};

61 62 63 64 65
/*
 * For SKL+, the plane source size used by the hardware is based on the value we
 * write to the PLANE_SIZE register. For BDW-, the hardware looks at the value
 * we wrote to PIPESRC.
 */
66
static void intel_fbc_get_plane_source_size(const struct intel_fbc_state_cache *cache,
67 68 69
					    int *width, int *height)
{
	if (width)
70
		*width = cache->plane.src_w;
71
	if (height)
72
		*height = cache->plane.src_h;
73 74
}

75 76
/* plane stride in pixels */
static unsigned int intel_fbc_plane_stride(const struct intel_plane_state *plane_state)
77
{
78 79 80
	const struct drm_framebuffer *fb = plane_state->hw.fb;
	unsigned int stride;

81
	stride = plane_state->view.color_plane[0].mapping_stride;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	if (!drm_rotation_90_or_270(plane_state->hw.rotation))
		stride /= fb->format->cpp[0];

	return stride;
}

/* plane stride based cfb stride in bytes, assuming 1:1 compression limit */
static unsigned int _intel_fbc_cfb_stride(const struct intel_fbc_state_cache *cache)
{
	unsigned int cpp = 4; /* FBC always 4 bytes per pixel */

	return cache->fb.stride * cpp;
}

/* minimum acceptable cfb stride in bytes, assuming 1:1 compression limit */
97
static unsigned int skl_fbc_min_cfb_stride(struct intel_fbc *fbc,
98
					   const struct intel_fbc_state_cache *cache)
99
{
100
	struct drm_i915_private *i915 = fbc->i915;
101 102 103 104 105 106 107 108
	unsigned int limit = 4; /* 1:4 compression limit is the worst case */
	unsigned int cpp = 4; /* FBC always 4 bytes per pixel */
	unsigned int height = 4; /* FBC segment is 4 lines */
	unsigned int stride;

	/* minimum segment stride we can use */
	stride = cache->plane.src_w * cpp * height / limit;

109 110 111 112 113 114 115
	/*
	 * Wa_16011863758: icl+
	 * Avoid some hardware segment address miscalculation.
	 */
	if (DISPLAY_VER(i915) >= 11)
		stride += 64;

116 117 118 119 120 121 122 123 124 125 126
	/*
	 * At least some of the platforms require each 4 line segment to
	 * be 512 byte aligned. Just do it always for simplicity.
	 */
	stride = ALIGN(stride, 512);

	/* convert back to single line equivalent with 1:1 compression limit */
	return stride * limit / height;
}

/* properly aligned cfb stride in bytes, assuming 1:1 compression limit */
127
static unsigned int intel_fbc_cfb_stride(struct intel_fbc *fbc,
128 129
					 const struct intel_fbc_state_cache *cache)
{
130
	struct drm_i915_private *i915 = fbc->i915;
131 132 133 134 135 136 137
	unsigned int stride = _intel_fbc_cfb_stride(cache);

	/*
	 * At least some of the platforms require each 4 line segment to
	 * be 512 byte aligned. Aligning each line to 512 bytes guarantees
	 * that regardless of the compression limit we choose later.
	 */
138
	if (DISPLAY_VER(i915) >= 9)
139
		return max(ALIGN(stride, 512), skl_fbc_min_cfb_stride(fbc, cache));
140 141 142 143
	else
		return stride;
}

144
static unsigned int intel_fbc_cfb_size(struct intel_fbc *fbc,
145 146
				       const struct intel_fbc_state_cache *cache)
{
147
	struct drm_i915_private *i915 = fbc->i915;
148
	int lines = cache->plane.src_h;
149

V
Ville Syrjälä 已提交
150
	if (DISPLAY_VER(i915) == 7)
151
		lines = min(lines, 2048);
V
Ville Syrjälä 已提交
152
	else if (DISPLAY_VER(i915) >= 8)
153
		lines = min(lines, 2560);
154

155
	return lines * intel_fbc_cfb_stride(fbc, cache);
156 157
}

158
static u32 i8xx_fbc_ctl(struct intel_fbc *fbc)
159 160
{
	const struct intel_fbc_reg_params *params = &fbc->params;
161
	struct drm_i915_private *i915 = fbc->i915;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	unsigned int cfb_stride;
	u32 fbc_ctl;

	cfb_stride = params->cfb_stride / fbc->limit;

	/* FBC_CTL wants 32B or 64B units */
	if (DISPLAY_VER(i915) == 2)
		cfb_stride = (cfb_stride / 32) - 1;
	else
		cfb_stride = (cfb_stride / 64) - 1;

	fbc_ctl = FBC_CTL_PERIODIC |
		FBC_CTL_INTERVAL(params->interval) |
		FBC_CTL_STRIDE(cfb_stride);

	if (IS_I945GM(i915))
		fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */

	if (params->fence_id >= 0)
		fbc_ctl |= FBC_CTL_FENCENO(params->fence_id);

	return fbc_ctl;
}

186
static u32 i965_fbc_ctl2(struct intel_fbc *fbc)
187
{
188
	const struct intel_fbc_reg_params *params = &fbc->params;
189 190 191 192 193 194
	u32 fbc_ctl2;

	fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM |
		FBC_CTL_PLANE(params->crtc.i9xx_plane);

	if (params->fence_id >= 0)
195
		fbc_ctl2 |= FBC_CTL_CPU_FENCE_EN;
196 197 198 199

	return fbc_ctl2;
}

200
static void i8xx_fbc_deactivate(struct intel_fbc *fbc)
201
{
202
	struct drm_i915_private *i915 = fbc->i915;
203 204 205
	u32 fbc_ctl;

	/* Disable compression */
V
Ville Syrjälä 已提交
206
	fbc_ctl = intel_de_read(i915, FBC_CONTROL);
207 208 209 210
	if ((fbc_ctl & FBC_CTL_EN) == 0)
		return;

	fbc_ctl &= ~FBC_CTL_EN;
V
Ville Syrjälä 已提交
211
	intel_de_write(i915, FBC_CONTROL, fbc_ctl);
212 213

	/* Wait for compressing bit to clear */
V
Ville Syrjälä 已提交
214
	if (intel_de_wait_for_clear(i915, FBC_STATUS,
215
				    FBC_STAT_COMPRESSING, 10)) {
V
Ville Syrjälä 已提交
216
		drm_dbg_kms(&i915->drm, "FBC idle timed out\n");
217 218 219 220
		return;
	}
}

221
static void i8xx_fbc_activate(struct intel_fbc *fbc)
222
{
223
	const struct intel_fbc_reg_params *params = &fbc->params;
224
	struct drm_i915_private *i915 = fbc->i915;
225 226 227 228
	int i;

	/* Clear old tags */
	for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
V
Ville Syrjälä 已提交
229
		intel_de_write(i915, FBC_TAG(i), 0);
230

V
Ville Syrjälä 已提交
231 232
	if (DISPLAY_VER(i915) == 4) {
		intel_de_write(i915, FBC_CONTROL2,
233
			       i965_fbc_ctl2(fbc));
V
Ville Syrjälä 已提交
234
		intel_de_write(i915, FBC_FENCE_OFF,
235
			       params->fence_y_offset);
236 237
	}

V
Ville Syrjälä 已提交
238
	intel_de_write(i915, FBC_CONTROL,
239
		       FBC_CTL_EN | i8xx_fbc_ctl(fbc));
240 241
}

242
static bool i8xx_fbc_is_active(struct intel_fbc *fbc)
243
{
244
	return intel_de_read(fbc->i915, FBC_CONTROL) & FBC_CTL_EN;
245 246
}

247
static bool i8xx_fbc_is_compressing(struct intel_fbc *fbc)
248
{
249
	return intel_de_read(fbc->i915, FBC_STATUS) &
250 251 252
		(FBC_STAT_COMPRESSING | FBC_STAT_COMPRESSED);
}

253
static void i8xx_fbc_nuke(struct intel_fbc *fbc)
254
{
255
	struct intel_fbc_reg_params *params = &fbc->params;
256
	enum i9xx_plane_id i9xx_plane = params->crtc.i9xx_plane;
257
	struct drm_i915_private *dev_priv = fbc->i915;
258 259 260 261 262 263 264

	spin_lock_irq(&dev_priv->uncore.lock);
	intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
			  intel_de_read_fw(dev_priv, DSPADDR(i9xx_plane)));
	spin_unlock_irq(&dev_priv->uncore.lock);
}

265
static void i8xx_fbc_program_cfb(struct intel_fbc *fbc)
266
{
267
	struct drm_i915_private *i915 = fbc->i915;
268 269 270 271 272 273 274 275 276 277 278 279

	GEM_BUG_ON(range_overflows_end_t(u64, i915->dsm.start,
					 fbc->compressed_fb.start, U32_MAX));
	GEM_BUG_ON(range_overflows_end_t(u64, i915->dsm.start,
					 fbc->compressed_llb.start, U32_MAX));

	intel_de_write(i915, FBC_CFB_BASE,
		       i915->dsm.start + fbc->compressed_fb.start);
	intel_de_write(i915, FBC_LL_BASE,
		       i915->dsm.start + fbc->compressed_llb.start);
}

280 281 282 283 284
static const struct intel_fbc_funcs i8xx_fbc_funcs = {
	.activate = i8xx_fbc_activate,
	.deactivate = i8xx_fbc_deactivate,
	.is_active = i8xx_fbc_is_active,
	.is_compressing = i8xx_fbc_is_compressing,
285
	.nuke = i8xx_fbc_nuke,
286
	.program_cfb = i8xx_fbc_program_cfb,
287 288
};

289
static void i965_fbc_nuke(struct intel_fbc *fbc)
290
{
291
	struct intel_fbc_reg_params *params = &fbc->params;
292
	enum i9xx_plane_id i9xx_plane = params->crtc.i9xx_plane;
293
	struct drm_i915_private *dev_priv = fbc->i915;
294 295 296 297 298 299 300 301 302 303 304 305 306

	spin_lock_irq(&dev_priv->uncore.lock);
	intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
			  intel_de_read_fw(dev_priv, DSPSURF(i9xx_plane)));
	spin_unlock_irq(&dev_priv->uncore.lock);
}

static const struct intel_fbc_funcs i965_fbc_funcs = {
	.activate = i8xx_fbc_activate,
	.deactivate = i8xx_fbc_deactivate,
	.is_active = i8xx_fbc_is_active,
	.is_compressing = i8xx_fbc_is_compressing,
	.nuke = i965_fbc_nuke,
307
	.program_cfb = i8xx_fbc_program_cfb,
308 309
};

310
static u32 g4x_dpfc_ctl_limit(struct intel_fbc *fbc)
311
{
312
	switch (fbc->limit) {
313
	default:
314
		MISSING_CASE(fbc->limit);
315 316 317 318 319 320 321 322 323 324
		fallthrough;
	case 1:
		return DPFC_CTL_LIMIT_1X;
	case 2:
		return DPFC_CTL_LIMIT_2X;
	case 4:
		return DPFC_CTL_LIMIT_4X;
	}
}

325
static u32 g4x_dpfc_ctl(struct intel_fbc *fbc)
326
{
327 328
	const struct intel_fbc_reg_params *params = &fbc->params;
	struct drm_i915_private *i915 = fbc->i915;
329 330
	u32 dpfc_ctl;

331
	dpfc_ctl = g4x_dpfc_ctl_limit(fbc) |
332
		DPFC_CTL_PLANE_G4X(params->crtc.i9xx_plane);
333

334
	if (IS_G4X(i915))
335
		dpfc_ctl |= DPFC_CTL_SR_EN;
336

337
	if (params->fence_id >= 0) {
338
		dpfc_ctl |= DPFC_CTL_FENCE_EN_G4X;
339 340

		if (DISPLAY_VER(i915) < 6)
341
			dpfc_ctl |= DPFC_CTL_FENCENO(params->fence_id);
342 343 344 345 346
	}

	return dpfc_ctl;
}

347
static void g4x_fbc_activate(struct intel_fbc *fbc)
348
{
349 350
	const struct intel_fbc_reg_params *params = &fbc->params;
	struct drm_i915_private *i915 = fbc->i915;
351

V
Ville Syrjälä 已提交
352
	intel_de_write(i915, DPFC_FENCE_YOFF,
353
		       params->fence_y_offset);
354

V
Ville Syrjälä 已提交
355
	intel_de_write(i915, DPFC_CONTROL,
356
		       DPFC_CTL_EN | g4x_dpfc_ctl(fbc));
357 358
}

359
static void g4x_fbc_deactivate(struct intel_fbc *fbc)
360
{
361
	struct drm_i915_private *i915 = fbc->i915;
362 363 364
	u32 dpfc_ctl;

	/* Disable compression */
V
Ville Syrjälä 已提交
365
	dpfc_ctl = intel_de_read(i915, DPFC_CONTROL);
366 367
	if (dpfc_ctl & DPFC_CTL_EN) {
		dpfc_ctl &= ~DPFC_CTL_EN;
V
Ville Syrjälä 已提交
368
		intel_de_write(i915, DPFC_CONTROL, dpfc_ctl);
369 370 371
	}
}

372
static bool g4x_fbc_is_active(struct intel_fbc *fbc)
373
{
374
	return intel_de_read(fbc->i915, DPFC_CONTROL) & DPFC_CTL_EN;
375 376
}

377
static bool g4x_fbc_is_compressing(struct intel_fbc *fbc)
378
{
379
	return intel_de_read(fbc->i915, DPFC_STATUS) & DPFC_COMP_SEG_MASK;
380 381
}

382
static void g4x_fbc_program_cfb(struct intel_fbc *fbc)
383
{
384
	struct drm_i915_private *i915 = fbc->i915;
385 386 387 388

	intel_de_write(i915, DPFC_CB_BASE, fbc->compressed_fb.start);
}

389 390 391 392 393
static const struct intel_fbc_funcs g4x_fbc_funcs = {
	.activate = g4x_fbc_activate,
	.deactivate = g4x_fbc_deactivate,
	.is_active = g4x_fbc_is_active,
	.is_compressing = g4x_fbc_is_compressing,
394
	.nuke = i965_fbc_nuke,
395
	.program_cfb = g4x_fbc_program_cfb,
396 397
};

398
static void ilk_fbc_activate(struct intel_fbc *fbc)
399
{
400 401
	struct intel_fbc_reg_params *params = &fbc->params;
	struct drm_i915_private *i915 = fbc->i915;
402

V
Ville Syrjälä 已提交
403
	intel_de_write(i915, ILK_DPFC_FENCE_YOFF,
404
		       params->fence_y_offset);
405

V
Ville Syrjälä 已提交
406
	intel_de_write(i915, ILK_DPFC_CONTROL,
407
		       DPFC_CTL_EN | g4x_dpfc_ctl(fbc));
408 409
}

410
static void ilk_fbc_deactivate(struct intel_fbc *fbc)
411
{
412
	struct drm_i915_private *i915 = fbc->i915;
413 414 415
	u32 dpfc_ctl;

	/* Disable compression */
V
Ville Syrjälä 已提交
416
	dpfc_ctl = intel_de_read(i915, ILK_DPFC_CONTROL);
417 418
	if (dpfc_ctl & DPFC_CTL_EN) {
		dpfc_ctl &= ~DPFC_CTL_EN;
V
Ville Syrjälä 已提交
419
		intel_de_write(i915, ILK_DPFC_CONTROL, dpfc_ctl);
420 421 422
	}
}

423
static bool ilk_fbc_is_active(struct intel_fbc *fbc)
424
{
425
	return intel_de_read(fbc->i915, ILK_DPFC_CONTROL) & DPFC_CTL_EN;
426 427
}

428
static bool ilk_fbc_is_compressing(struct intel_fbc *fbc)
429
{
430
	return intel_de_read(fbc->i915, ILK_DPFC_STATUS) & DPFC_COMP_SEG_MASK;
431 432
}

433
static void ilk_fbc_program_cfb(struct intel_fbc *fbc)
434
{
435
	struct drm_i915_private *i915 = fbc->i915;
436 437 438 439

	intel_de_write(i915, ILK_DPFC_CB_BASE, fbc->compressed_fb.start);
}

440 441 442 443 444
static const struct intel_fbc_funcs ilk_fbc_funcs = {
	.activate = ilk_fbc_activate,
	.deactivate = ilk_fbc_deactivate,
	.is_active = ilk_fbc_is_active,
	.is_compressing = ilk_fbc_is_compressing,
445
	.nuke = i965_fbc_nuke,
446
	.program_cfb = ilk_fbc_program_cfb,
447 448
};

449
static void snb_fbc_program_fence(struct intel_fbc *fbc)
450
{
451 452
	const struct intel_fbc_reg_params *params = &fbc->params;
	struct drm_i915_private *i915 = fbc->i915;
453 454 455
	u32 ctl = 0;

	if (params->fence_id >= 0)
456
		ctl = SNB_DPFC_FENCE_EN | SNB_DPFC_FENCENO(params->fence_id);
457 458

	intel_de_write(i915, SNB_DPFC_CTL_SA, ctl);
459
	intel_de_write(i915, SNB_DPFC_CPU_FENCE_OFFSET, params->fence_y_offset);
460 461
}

462
static void snb_fbc_activate(struct intel_fbc *fbc)
463
{
464
	snb_fbc_program_fence(fbc);
465

466
	ilk_fbc_activate(fbc);
467 468
}

469
static void snb_fbc_nuke(struct intel_fbc *fbc)
470
{
471 472
	struct drm_i915_private *i915 = fbc->i915;

V
Ville Syrjälä 已提交
473 474
	intel_de_write(i915, MSG_FBC_REND_STATE, FBC_REND_NUKE);
	intel_de_posting_read(i915, MSG_FBC_REND_STATE);
475 476 477 478 479 480 481 482
}

static const struct intel_fbc_funcs snb_fbc_funcs = {
	.activate = snb_fbc_activate,
	.deactivate = ilk_fbc_deactivate,
	.is_active = ilk_fbc_is_active,
	.is_compressing = ilk_fbc_is_compressing,
	.nuke = snb_fbc_nuke,
483
	.program_cfb = ilk_fbc_program_cfb,
484 485
};

486
static void glk_fbc_program_cfb_stride(struct intel_fbc *fbc)
487
{
488
	const struct intel_fbc_reg_params *params = &fbc->params;
489
	struct drm_i915_private *i915 = fbc->i915;
490
	u32 val = 0;
491

492 493 494
	if (params->override_cfb_stride)
		val |= FBC_STRIDE_OVERRIDE |
			FBC_STRIDE(params->override_cfb_stride / fbc->limit);
495

496 497
	intel_de_write(i915, GLK_FBC_STRIDE, val);
}
498

499
static void skl_fbc_program_cfb_stride(struct intel_fbc *fbc)
500 501
{
	const struct intel_fbc_reg_params *params = &fbc->params;
502
	struct drm_i915_private *i915 = fbc->i915;
503
	u32 val = 0;
504

505 506 507 508
	/* Display WA #0529: skl, kbl, bxt. */
	if (params->override_cfb_stride)
		val |= CHICKEN_FBC_STRIDE_OVERRIDE |
			CHICKEN_FBC_STRIDE(params->override_cfb_stride / fbc->limit);
509

510 511 512 513 514
	intel_de_rmw(i915, CHICKEN_MISC_4,
		     CHICKEN_FBC_STRIDE_OVERRIDE |
		     CHICKEN_FBC_STRIDE_MASK, val);
}

515
static u32 ivb_dpfc_ctl(struct intel_fbc *fbc)
516
{
517 518
	const struct intel_fbc_reg_params *params = &fbc->params;
	struct drm_i915_private *i915 = fbc->i915;
519 520
	u32 dpfc_ctl;

521
	dpfc_ctl = g4x_dpfc_ctl_limit(fbc);
522

523
	if (IS_IVYBRIDGE(i915))
524
		dpfc_ctl |= DPFC_CTL_PLANE_IVB(params->crtc.i9xx_plane);
525

526
	if (params->fence_id >= 0)
527
		dpfc_ctl |= DPFC_CTL_FENCE_EN_IVB;
528

529
	if (fbc->false_color)
530
		dpfc_ctl |= DPFC_CTL_FALSE_COLOR;
531

532 533 534
	return dpfc_ctl;
}

535
static void ivb_fbc_activate(struct intel_fbc *fbc)
536
{
537 538
	struct drm_i915_private *i915 = fbc->i915;

V
Ville Syrjälä 已提交
539
	if (DISPLAY_VER(i915) >= 10)
540
		glk_fbc_program_cfb_stride(fbc);
V
Ville Syrjälä 已提交
541
	else if (DISPLAY_VER(i915) == 9)
542
		skl_fbc_program_cfb_stride(fbc);
543

V
Ville Syrjälä 已提交
544
	if (i915->ggtt.num_fences)
545
		snb_fbc_program_fence(fbc);
546

V
Ville Syrjälä 已提交
547
	intel_de_write(i915, ILK_DPFC_CONTROL,
548
		       DPFC_CTL_EN | ivb_dpfc_ctl(fbc));
549 550
}

551
static bool ivb_fbc_is_compressing(struct intel_fbc *fbc)
552
{
553
	return intel_de_read(fbc->i915, ILK_DPFC_STATUS2) & DPFC_COMP_SEG_MASK_IVB;
554 555
}

556
static void ivb_fbc_set_false_color(struct intel_fbc *fbc,
557 558
				    bool enable)
{
559
	intel_de_rmw(fbc->i915, ILK_DPFC_CONTROL,
560
		     DPFC_CTL_FALSE_COLOR, enable ? DPFC_CTL_FALSE_COLOR : 0);
561 562
}

V
Ville Syrjälä 已提交
563 564
static const struct intel_fbc_funcs ivb_fbc_funcs = {
	.activate = ivb_fbc_activate,
565 566
	.deactivate = ilk_fbc_deactivate,
	.is_active = ilk_fbc_is_active,
V
Ville Syrjälä 已提交
567
	.is_compressing = ivb_fbc_is_compressing,
568
	.nuke = snb_fbc_nuke,
569
	.program_cfb = ilk_fbc_program_cfb,
570
	.set_false_color = ivb_fbc_set_false_color,
571 572
};

573
static bool intel_fbc_hw_is_active(struct intel_fbc *fbc)
574
{
575
	return fbc->funcs->is_active(fbc);
576 577
}

578
static void intel_fbc_hw_activate(struct intel_fbc *fbc)
579
{
580 581
	trace_intel_fbc_activate(fbc->crtc);

582
	fbc->active = true;
583
	fbc->activated = true;
584

585
	fbc->funcs->activate(fbc);
586 587
}

588
static void intel_fbc_hw_deactivate(struct intel_fbc *fbc)
589
{
590 591
	trace_intel_fbc_deactivate(fbc->crtc);

592 593
	fbc->active = false;

594
	fbc->funcs->deactivate(fbc);
595 596
}

597
bool intel_fbc_is_compressing(struct intel_fbc *fbc)
598
{
599
	return fbc->funcs->is_compressing(fbc);
600 601
}

602
static void intel_fbc_nuke(struct intel_fbc *fbc)
603 604 605
{
	trace_intel_fbc_nuke(fbc->crtc);

606
	fbc->funcs->nuke(fbc);
607 608
}

609
int intel_fbc_set_false_color(struct intel_fbc *fbc, bool enable)
610 611 612 613 614 615 616 617
{
	if (!fbc->funcs || !fbc->funcs->set_false_color)
		return -ENODEV;

	mutex_lock(&fbc->lock);

	fbc->false_color = enable;

618
	fbc->funcs->set_false_color(fbc, enable);
619 620 621 622 623 624

	mutex_unlock(&fbc->lock);

	return 0;
}

R
Rodrigo Vivi 已提交
625
/**
626
 * intel_fbc_is_active - Is FBC active?
627
 * @fbc: The FBC instance
R
Rodrigo Vivi 已提交
628 629
 *
 * This function is used to verify the current state of FBC.
D
Daniel Vetter 已提交
630
 *
R
Rodrigo Vivi 已提交
631
 * FIXME: This should be tracked in the plane config eventually
D
Daniel Vetter 已提交
632
 * instead of queried at runtime for most callers.
R
Rodrigo Vivi 已提交
633
 */
634
bool intel_fbc_is_active(struct intel_fbc *fbc)
635
{
636
	return fbc->active;
637 638
}

639
static void intel_fbc_activate(struct intel_fbc *fbc)
640
{
641 642
	intel_fbc_hw_activate(fbc);
	intel_fbc_nuke(fbc);
643 644
}

645
static void intel_fbc_deactivate(struct intel_fbc *fbc, const char *reason)
P
Paulo Zanoni 已提交
646
{
647
	struct drm_i915_private *i915 = fbc->i915;
648

V
Ville Syrjälä 已提交
649
	drm_WARN_ON(&i915->drm, !mutex_is_locked(&fbc->lock));
P
Paulo Zanoni 已提交
650

651
	if (fbc->active)
652
		intel_fbc_hw_deactivate(fbc);
653 654

	fbc->no_fbc_reason = reason;
655 656
}

657 658
static u64 intel_fbc_cfb_base_max(struct drm_i915_private *i915)
{
659
	if (DISPLAY_VER(i915) >= 5 || IS_G4X(i915))
660 661 662 663 664
		return BIT_ULL(28);
	else
		return BIT_ULL(32);
}

V
Ville Syrjälä 已提交
665
static u64 intel_fbc_stolen_end(struct drm_i915_private *i915)
666
{
667 668 669 670 671 672
	u64 end;

	/* The FBC hardware for BDW/SKL doesn't have access to the stolen
	 * reserved range size, so it always assumes the maximum (8mb) is used.
	 * If we enable FBC using a CFB on that memory range we'll get FIFO
	 * underruns, even if that range is not reserved by the BIOS. */
V
Ville Syrjälä 已提交
673 674 675
	if (IS_BROADWELL(i915) ||
	    (DISPLAY_VER(i915) == 9 && !IS_BROXTON(i915)))
		end = resource_size(&i915->dsm) - 8 * 1024 * 1024;
676
	else
677
		end = U64_MAX;
678

V
Ville Syrjälä 已提交
679
	return min(end, intel_fbc_cfb_base_max(i915));
680 681
}

682 683 684 685 686
static int intel_fbc_min_limit(int fb_cpp)
{
	return fb_cpp == 2 ? 2 : 1;
}

V
Ville Syrjälä 已提交
687
static int intel_fbc_max_limit(struct drm_i915_private *i915)
688 689
{
	/* WaFbcOnly1to1Ratio:ctg */
V
Ville Syrjälä 已提交
690
	if (IS_G4X(i915))
691 692
		return 1;

693 694 695 696
	/*
	 * FBC2 can only do 1:1, 1:2, 1:4, we limit
	 * FBC1 to the same out of convenience.
	 */
697
	return 4;
698 699
}

700
static int find_compression_limit(struct intel_fbc *fbc,
701
				  unsigned int size, int min_limit)
702
{
703
	struct drm_i915_private *i915 = fbc->i915;
V
Ville Syrjälä 已提交
704
	u64 end = intel_fbc_stolen_end(i915);
705 706 707
	int ret, limit = min_limit;

	size /= limit;
708 709

	/* Try to over-allocate to reduce reallocations and fragmentation. */
V
Ville Syrjälä 已提交
710
	ret = i915_gem_stolen_insert_node_in_range(i915, &fbc->compressed_fb,
711
						   size <<= 1, 4096, 0, end);
712
	if (ret == 0)
713
		return limit;
714

V
Ville Syrjälä 已提交
715 716
	for (; limit <= intel_fbc_max_limit(i915); limit <<= 1) {
		ret = i915_gem_stolen_insert_node_in_range(i915, &fbc->compressed_fb,
717 718 719
							   size >>= 1, 4096, 0, end);
		if (ret == 0)
			return limit;
720
	}
721 722

	return 0;
723 724
}

725
static int intel_fbc_alloc_cfb(struct intel_fbc *fbc,
726
			       unsigned int size, int min_limit)
727
{
728
	struct drm_i915_private *i915 = fbc->i915;
729
	int ret;
730

V
Ville Syrjälä 已提交
731
	drm_WARN_ON(&i915->drm,
732
		    drm_mm_node_allocated(&fbc->compressed_fb));
V
Ville Syrjälä 已提交
733
	drm_WARN_ON(&i915->drm,
734
		    drm_mm_node_allocated(&fbc->compressed_llb));
735

V
Ville Syrjälä 已提交
736 737
	if (DISPLAY_VER(i915) < 5 && !IS_G4X(i915)) {
		ret = i915_gem_stolen_insert_node(i915, &fbc->compressed_llb,
738 739 740 741 742
						  4096, 4096);
		if (ret)
			goto err;
	}

743
	ret = find_compression_limit(fbc, size, min_limit);
744 745
	if (!ret)
		goto err_llb;
746
	else if (ret > min_limit)
V
Ville Syrjälä 已提交
747
		drm_info_once(&i915->drm,
748
			      "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");
749

750
	fbc->limit = ret;
751

V
Ville Syrjälä 已提交
752
	drm_dbg_kms(&i915->drm,
753 754
		    "reserved %llu bytes of contiguous stolen space for FBC, limit: %d\n",
		    fbc->compressed_fb.size, fbc->limit);
755 756 757 758

	return 0;

err_llb:
759
	if (drm_mm_node_allocated(&fbc->compressed_llb))
V
Ville Syrjälä 已提交
760
		i915_gem_stolen_remove_node(i915, &fbc->compressed_llb);
761
err:
V
Ville Syrjälä 已提交
762 763
	if (drm_mm_initialized(&i915->mm.stolen))
		drm_info_once(&i915->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);
764 765 766
	return -ENOSPC;
}

767
static void intel_fbc_program_cfb(struct intel_fbc *fbc)
768
{
769
	fbc->funcs->program_cfb(fbc);
770 771
}

772
static void __intel_fbc_cleanup_cfb(struct intel_fbc *fbc)
773
{
774
	struct drm_i915_private *i915 = fbc->i915;
775

776
	if (WARN_ON(intel_fbc_hw_is_active(fbc)))
777 778
		return;

779
	if (drm_mm_node_allocated(&fbc->compressed_llb))
V
Ville Syrjälä 已提交
780
		i915_gem_stolen_remove_node(i915, &fbc->compressed_llb);
781
	if (drm_mm_node_allocated(&fbc->compressed_fb))
V
Ville Syrjälä 已提交
782
		i915_gem_stolen_remove_node(i915, &fbc->compressed_fb);
783 784
}

785
void intel_fbc_cleanup(struct drm_i915_private *i915)
P
Paulo Zanoni 已提交
786
{
V
Ville Syrjälä 已提交
787
	struct intel_fbc *fbc = &i915->fbc;
788

V
Ville Syrjälä 已提交
789
	if (!HAS_FBC(i915))
790 791
		return;

792
	mutex_lock(&fbc->lock);
793
	__intel_fbc_cleanup_cfb(fbc);
794
	mutex_unlock(&fbc->lock);
P
Paulo Zanoni 已提交
795 796
}

V
Ville Syrjälä 已提交
797
static bool stride_is_valid(struct drm_i915_private *i915,
798
			    u64 modifier, unsigned int stride)
799
{
800
	/* This should have been caught earlier. */
V
Ville Syrjälä 已提交
801
	if (drm_WARN_ON_ONCE(&i915->drm, (stride & (64 - 1)) != 0))
802
		return false;
803 804

	/* Below are the additional FBC restrictions. */
805 806
	if (stride < 512)
		return false;
807

V
Ville Syrjälä 已提交
808
	if (DISPLAY_VER(i915) == 2 || DISPLAY_VER(i915) == 3)
809 810
		return stride == 4096 || stride == 8192;

V
Ville Syrjälä 已提交
811
	if (DISPLAY_VER(i915) == 4 && !IS_G4X(i915) && stride < 2048)
812 813
		return false;

814
	/* Display WA #1105: skl,bxt,kbl,cfl,glk */
V
Ville Syrjälä 已提交
815
	if ((DISPLAY_VER(i915) == 9 || IS_GEMINILAKE(i915)) &&
816 817 818
	    modifier == DRM_FORMAT_MOD_LINEAR && stride & 511)
		return false;

819 820 821 822 823 824
	if (stride > 16384)
		return false;

	return true;
}

V
Ville Syrjälä 已提交
825
static bool pixel_format_is_valid(struct drm_i915_private *i915,
826
				  u32 pixel_format)
827
{
828
	switch (pixel_format) {
829 830 831 832 833 834
	case DRM_FORMAT_XRGB8888:
	case DRM_FORMAT_XBGR8888:
		return true;
	case DRM_FORMAT_XRGB1555:
	case DRM_FORMAT_RGB565:
		/* 16bpp not supported on gen2 */
V
Ville Syrjälä 已提交
835
		if (DISPLAY_VER(i915) == 2)
836 837
			return false;
		/* WaFbcOnly1to1Ratio:ctg */
V
Ville Syrjälä 已提交
838
		if (IS_G4X(i915))
839 840 841 842 843 844 845
			return false;
		return true;
	default:
		return false;
	}
}

V
Ville Syrjälä 已提交
846
static bool rotation_is_valid(struct drm_i915_private *i915,
847 848
			      u32 pixel_format, unsigned int rotation)
{
V
Ville Syrjälä 已提交
849
	if (DISPLAY_VER(i915) >= 9 && pixel_format == DRM_FORMAT_RGB565 &&
850 851
	    drm_rotation_90_or_270(rotation))
		return false;
V
Ville Syrjälä 已提交
852
	else if (DISPLAY_VER(i915) <= 4 && !IS_G4X(i915) &&
853 854 855 856 857 858
		 rotation != DRM_MODE_ROTATE_0)
		return false;

	return true;
}

859 860 861
/*
 * For some reason, the hardware tracking starts looking at whatever we
 * programmed as the display plane base address register. It does not look at
862 863
 * the X and Y offset registers. That's why we include the src x/y offsets
 * instead of just looking at the plane size.
864
 */
865 866
static bool intel_fbc_hw_tracking_covers_screen(struct intel_fbc *fbc,
						struct intel_crtc *crtc)
867
{
868
	struct drm_i915_private *i915 = fbc->i915;
869
	unsigned int effective_w, effective_h, max_w, max_h;
870

V
Ville Syrjälä 已提交
871
	if (DISPLAY_VER(i915) >= 10) {
872 873
		max_w = 5120;
		max_h = 4096;
V
Ville Syrjälä 已提交
874
	} else if (DISPLAY_VER(i915) >= 8 || IS_HASWELL(i915)) {
875 876
		max_w = 4096;
		max_h = 4096;
V
Ville Syrjälä 已提交
877
	} else if (IS_G4X(i915) || DISPLAY_VER(i915) >= 5) {
878 879 880 881 882 883 884
		max_w = 4096;
		max_h = 2048;
	} else {
		max_w = 2048;
		max_h = 1536;
	}

885 886
	intel_fbc_get_plane_source_size(&fbc->state_cache, &effective_w,
					&effective_h);
887 888
	effective_w += fbc->state_cache.plane.adjusted_x;
	effective_h += fbc->state_cache.plane.adjusted_y;
889 890

	return effective_w <= max_w && effective_h <= max_h;
891 892
}

V
Ville Syrjälä 已提交
893
static bool tiling_is_valid(struct drm_i915_private *i915,
894
			    u64 modifier)
895 896 897 898
{
	switch (modifier) {
	case DRM_FORMAT_MOD_LINEAR:
	case I915_FORMAT_MOD_Y_TILED:
899
	case I915_FORMAT_MOD_Yf_TILED:
V
Ville Syrjälä 已提交
900
		return DISPLAY_VER(i915) >= 9;
901
	case I915_FORMAT_MOD_4_TILED:
902
	case I915_FORMAT_MOD_X_TILED:
903 904 905 906 907 908
		return true;
	default:
		return false;
	}
}

909
static void intel_fbc_update_state_cache(struct intel_crtc *crtc,
910 911
					 const struct intel_crtc_state *crtc_state,
					 const struct intel_plane_state *plane_state)
912
{
V
Ville Syrjälä 已提交
913 914
	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
	struct intel_fbc *fbc = &i915->fbc;
915
	struct intel_fbc_state_cache *cache = &fbc->state_cache;
916
	struct drm_framebuffer *fb = plane_state->hw.fb;
917

918 919 920
	cache->plane.visible = plane_state->uapi.visible;
	if (!cache->plane.visible)
		return;
921

922
	cache->crtc.mode_flags = crtc_state->hw.adjusted_mode.flags;
V
Ville Syrjälä 已提交
923
	if (IS_HASWELL(i915) || IS_BROADWELL(i915))
924
		cache->crtc.hsw_bdw_pixel_rate = crtc_state->pixel_rate;
925

926
	cache->plane.rotation = plane_state->hw.rotation;
927 928 929 930 931
	/*
	 * Src coordinates are already rotated by 270 degrees for
	 * the 90/270 degree plane rotation cases (to match the
	 * GTT mapping), hence no need to account for rotation here.
	 */
932 933
	cache->plane.src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
	cache->plane.src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
934 935
	cache->plane.adjusted_x = plane_state->view.color_plane[0].x;
	cache->plane.adjusted_y = plane_state->view.color_plane[0].y;
936

937
	cache->plane.pixel_blend_mode = plane_state->hw.pixel_blend_mode;
938

939
	cache->fb.format = fb->format;
940
	cache->fb.modifier = fb->modifier;
941
	cache->fb.stride = intel_fbc_plane_stride(plane_state);
942

943 944
	/* FBC1 compression interval: arbitrary choice of 1 second */
	cache->interval = drm_mode_vrefresh(&crtc_state->hw.adjusted_mode);
945

946 947
	cache->fence_y_offset = intel_plane_fence_y_offset(plane_state);

V
Ville Syrjälä 已提交
948
	drm_WARN_ON(&i915->drm, plane_state->flags & PLANE_HAS_FENCE &&
949
		    !plane_state->ggtt_vma->fence);
950 951

	if (plane_state->flags & PLANE_HAS_FENCE &&
952 953
	    plane_state->ggtt_vma->fence)
		cache->fence_id = plane_state->ggtt_vma->fence->id;
954 955
	else
		cache->fence_id = -1;
956 957

	cache->psr2_active = crtc_state->has_psr2;
958 959
}

960
static bool intel_fbc_cfb_size_changed(struct intel_fbc *fbc)
961
{
962
	return intel_fbc_cfb_size(fbc, &fbc->state_cache) >
963
		fbc->compressed_fb.size * fbc->limit;
964 965
}

966
static u16 intel_fbc_override_cfb_stride(struct intel_fbc *fbc,
967
					 const struct intel_fbc_state_cache *cache)
968
{
969
	unsigned int stride = _intel_fbc_cfb_stride(cache);
970
	unsigned int stride_aligned = intel_fbc_cfb_stride(fbc, cache);
971

972 973 974 975 976 977 978 979
	/*
	 * Override stride in 64 byte units per 4 line segment.
	 *
	 * Gen9 hw miscalculates cfb stride for linear as
	 * PLANE_STRIDE*512 instead of PLANE_STRIDE*64, so
	 * we always need to use the override there.
	 */
	if (stride != stride_aligned ||
980
	    (DISPLAY_VER(fbc->i915) == 9 &&
981 982
	     cache->fb.modifier == DRM_FORMAT_MOD_LINEAR))
		return stride_aligned * 4 / 64;
983

984
	return 0;
985 986
}

987
static bool intel_fbc_can_enable(struct intel_fbc *fbc)
988
{
989
	struct drm_i915_private *i915 = fbc->i915;
990

V
Ville Syrjälä 已提交
991
	if (intel_vgpu_active(i915)) {
992 993 994 995
		fbc->no_fbc_reason = "VGPU is active";
		return false;
	}

V
Ville Syrjälä 已提交
996
	if (!i915->params.enable_fbc) {
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
		fbc->no_fbc_reason = "disabled per module param or by default";
		return false;
	}

	if (fbc->underrun_detected) {
		fbc->no_fbc_reason = "underrun detected";
		return false;
	}

	return true;
}

1009 1010
static bool intel_fbc_can_activate(struct intel_crtc *crtc)
{
V
Ville Syrjälä 已提交
1011 1012
	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
	struct intel_fbc *fbc = &i915->fbc;
1013 1014
	struct intel_fbc_state_cache *cache = &fbc->state_cache;

1015
	if (!intel_fbc_can_enable(fbc))
1016 1017
		return false;

1018 1019 1020 1021 1022
	if (!cache->plane.visible) {
		fbc->no_fbc_reason = "primary plane not visible";
		return false;
	}

1023 1024 1025 1026 1027 1028 1029 1030
	/* We don't need to use a state cache here since this information is
	 * global for all CRTC.
	 */
	if (fbc->underrun_detected) {
		fbc->no_fbc_reason = "underrun detected";
		return false;
	}

1031
	if (cache->crtc.mode_flags & DRM_MODE_FLAG_INTERLACE) {
1032
		fbc->no_fbc_reason = "incompatible mode";
1033
		return false;
1034 1035
	}

1036
	if (!intel_fbc_hw_tracking_covers_screen(fbc, crtc)) {
1037
		fbc->no_fbc_reason = "mode too large for compression";
1038
		return false;
1039
	}
1040

1041 1042 1043 1044 1045 1046
	/* The use of a CPU fence is one of two ways to detect writes by the
	 * CPU to the scanout and trigger updates to the FBC.
	 *
	 * The other method is by software tracking (see
	 * intel_fbc_invalidate/flush()), it will manually notify FBC and nuke
	 * the current compressed buffer and recompress it.
1047 1048
	 *
	 * Note that is possible for a tiled surface to be unmappable (and
1049
	 * so have no fence associated with it) due to aperture constraints
1050
	 * at the time of pinning.
1051 1052 1053 1054
	 *
	 * FIXME with 90/270 degree rotation we should use the fence on
	 * the normal GTT view (the rotated view doesn't even have a
	 * fence). Would need changes to the FBC fence Y offset as well.
1055
	 * For now this will effectively disable FBC with 90/270 degree
1056
	 * rotation.
1057
	 */
V
Ville Syrjälä 已提交
1058
	if (DISPLAY_VER(i915) < 9 && cache->fence_id < 0) {
1059 1060
		fbc->no_fbc_reason = "framebuffer not tiled or fenced";
		return false;
1061
	}
1062

V
Ville Syrjälä 已提交
1063
	if (!pixel_format_is_valid(i915, cache->fb.format->format)) {
1064 1065 1066 1067
		fbc->no_fbc_reason = "pixel format is invalid";
		return false;
	}

V
Ville Syrjälä 已提交
1068
	if (!rotation_is_valid(i915, cache->fb.format->format,
1069
			       cache->plane.rotation)) {
1070
		fbc->no_fbc_reason = "rotation unsupported";
1071
		return false;
1072 1073
	}

V
Ville Syrjälä 已提交
1074
	if (!tiling_is_valid(i915, cache->fb.modifier)) {
1075 1076 1077 1078
		fbc->no_fbc_reason = "tiling unsupported";
		return false;
	}

V
Ville Syrjälä 已提交
1079
	if (!stride_is_valid(i915, cache->fb.modifier,
1080
			     cache->fb.stride * cache->fb.format->cpp[0])) {
1081
		fbc->no_fbc_reason = "framebuffer stride not supported";
1082
		return false;
1083 1084
	}

1085 1086 1087 1088 1089 1090
	if (cache->plane.pixel_blend_mode != DRM_MODE_BLEND_PIXEL_NONE &&
	    cache->fb.format->has_alpha) {
		fbc->no_fbc_reason = "per-pixel alpha blending is incompatible with FBC";
		return false;
	}

1091
	/* WaFbcExceedCdClockThreshold:hsw,bdw */
V
Ville Syrjälä 已提交
1092 1093
	if ((IS_HASWELL(i915) || IS_BROADWELL(i915)) &&
	    cache->crtc.hsw_bdw_pixel_rate >= i915->cdclk.hw.cdclk * 95 / 100) {
1094
		fbc->no_fbc_reason = "pixel rate is too big";
1095
		return false;
1096 1097
	}

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	/* It is possible for the required CFB size change without a
	 * crtc->disable + crtc->enable since it is possible to change the
	 * stride without triggering a full modeset. Since we try to
	 * over-allocate the CFB, there's a chance we may keep FBC enabled even
	 * if this happens, but if we exceed the current CFB size we'll have to
	 * disable FBC. Notice that it would be possible to disable FBC, wait
	 * for a frame, free the stolen node, then try to reenable FBC in case
	 * we didn't get any invalidate/deactivate calls, but this would require
	 * a lot of tracking just for a specific case. If we conclude it's an
	 * important case, we can implement it later. */
1108
	if (intel_fbc_cfb_size_changed(fbc)) {
1109
		fbc->no_fbc_reason = "CFB requirements changed";
1110 1111 1112
		return false;
	}

1113 1114 1115 1116 1117
	/*
	 * Work around a problem on GEN9+ HW, where enabling FBC on a plane
	 * having a Y offset that isn't divisible by 4 causes FIFO underrun
	 * and screen flicker.
	 */
V
Ville Syrjälä 已提交
1118
	if (DISPLAY_VER(i915) >= 9 &&
1119 1120 1121 1122 1123
	    (fbc->state_cache.plane.adjusted_y & 3)) {
		fbc->no_fbc_reason = "plane Y offset is misaligned";
		return false;
	}

1124
	/* Wa_22010751166: icl, ehl, tgl, dg1, rkl */
V
Ville Syrjälä 已提交
1125
	if (DISPLAY_VER(i915) >= 11 &&
1126 1127 1128 1129 1130
	    (cache->plane.src_h + cache->plane.adjusted_y) % 4) {
		fbc->no_fbc_reason = "plane height + offset is non-modulo of 4";
		return false;
	}

1131
	/*
1132
	 * Display 12+ is not supporting FBC with PSR2.
1133 1134 1135
	 * Recommendation is to keep this combination disabled
	 * Bspec: 50422 HSD: 14010260002
	 */
V
Ville Syrjälä 已提交
1136
	if (fbc->state_cache.psr2_active && DISPLAY_VER(i915) >= 12) {
1137 1138 1139 1140
		fbc->no_fbc_reason = "not supported with PSR2";
		return false;
	}

1141 1142 1143
	return true;
}

1144 1145
static void intel_fbc_get_reg_params(struct intel_fbc *fbc,
				     struct intel_crtc *crtc)
1146
{
1147 1148
	const struct intel_fbc_state_cache *cache = &fbc->state_cache;
	struct intel_fbc_reg_params *params = &fbc->params;
1149 1150 1151 1152 1153 1154

	/* Since all our fields are integer types, use memset here so the
	 * comparison function can rely on memcmp because the padding will be
	 * zero. */
	memset(params, 0, sizeof(*params));

1155
	params->fence_id = cache->fence_id;
1156
	params->fence_y_offset = cache->fence_y_offset;
1157

1158 1159
	params->interval = cache->interval;

1160
	params->crtc.pipe = crtc->pipe;
V
Ville Syrjälä 已提交
1161
	params->crtc.i9xx_plane = to_intel_plane(crtc->base.primary)->i9xx_plane;
1162

1163
	params->fb.format = cache->fb.format;
1164
	params->fb.modifier = cache->fb.modifier;
1165
	params->fb.stride = cache->fb.stride;
1166

1167 1168 1169
	params->cfb_stride = intel_fbc_cfb_stride(fbc, cache);
	params->cfb_size = intel_fbc_cfb_size(fbc, cache);
	params->override_cfb_stride = intel_fbc_override_cfb_stride(fbc, cache);
1170 1171

	params->plane_visible = cache->plane.visible;
1172 1173
}

1174 1175 1176
static bool intel_fbc_can_flip_nuke(const struct intel_crtc_state *crtc_state)
{
	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
V
Ville Syrjälä 已提交
1177
	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1178
	struct intel_fbc *fbc = &i915->fbc;
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
	const struct intel_fbc_state_cache *cache = &fbc->state_cache;
	const struct intel_fbc_reg_params *params = &fbc->params;

	if (drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
		return false;

	if (!params->plane_visible)
		return false;

	if (!intel_fbc_can_activate(crtc))
		return false;

	if (params->fb.format != cache->fb.format)
		return false;

1194 1195 1196
	if (params->fb.modifier != cache->fb.modifier)
		return false;

1197 1198 1199
	if (params->fb.stride != cache->fb.stride)
		return false;

1200
	if (params->cfb_stride != intel_fbc_cfb_stride(fbc, cache))
1201 1202
		return false;

1203
	if (params->cfb_size != intel_fbc_cfb_size(fbc, cache))
1204 1205
		return false;

1206
	if (params->override_cfb_stride != intel_fbc_override_cfb_stride(fbc, cache))
1207 1208 1209 1210 1211
		return false;

	return true;
}

1212 1213
bool intel_fbc_pre_update(struct intel_atomic_state *state,
			  struct intel_crtc *crtc)
1214
{
1215 1216 1217 1218 1219
	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
	const struct intel_crtc_state *crtc_state =
		intel_atomic_get_new_crtc_state(state, crtc);
	const struct intel_plane_state *plane_state =
		intel_atomic_get_new_plane_state(state, plane);
V
Ville Syrjälä 已提交
1220
	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1221
	struct intel_fbc *fbc = plane->fbc;
1222
	const char *reason = "update pending";
1223
	bool need_vblank_wait = false;
1224

1225
	if (!fbc || !plane_state)
1226 1227
		return need_vblank_wait;

1228
	mutex_lock(&fbc->lock);
1229

V
Ville Syrjälä 已提交
1230
	if (fbc->crtc != crtc)
1231
		goto unlock;
1232

1233
	intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
1234
	fbc->flip_pending = true;
1235

1236
	if (!intel_fbc_can_flip_nuke(crtc_state)) {
1237
		intel_fbc_deactivate(fbc, reason);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252

		/*
		 * Display WA #1198: glk+
		 * Need an extra vblank wait between FBC disable and most plane
		 * updates. Bspec says this is only needed for plane disable, but
		 * that is not true. Touching most plane registers will cause the
		 * corruption to appear. Also SKL/derivatives do not seem to be
		 * affected.
		 *
		 * TODO: could optimize this a bit by sampling the frame
		 * counter when we disable FBC (if it was already done earlier)
		 * and skipping the extra vblank wait before the plane update
		 * if at least one frame has already passed.
		 */
		if (fbc->activated &&
V
Ville Syrjälä 已提交
1253
		    DISPLAY_VER(i915) >= 10)
1254 1255 1256
			need_vblank_wait = true;
		fbc->activated = false;
	}
1257 1258
unlock:
	mutex_unlock(&fbc->lock);
1259 1260

	return need_vblank_wait;
1261 1262
}

1263
static void __intel_fbc_disable(struct intel_fbc *fbc)
1264
{
1265
	struct drm_i915_private *i915 = fbc->i915;
1266 1267
	struct intel_crtc *crtc = fbc->crtc;

V
Ville Syrjälä 已提交
1268 1269 1270
	drm_WARN_ON(&i915->drm, !mutex_is_locked(&fbc->lock));
	drm_WARN_ON(&i915->drm, !fbc->crtc);
	drm_WARN_ON(&i915->drm, fbc->active);
1271

V
Ville Syrjälä 已提交
1272
	drm_dbg_kms(&i915->drm, "Disabling FBC on pipe %c\n",
1273
		    pipe_name(crtc->pipe));
1274

1275
	__intel_fbc_cleanup_cfb(fbc);
1276 1277 1278 1279

	fbc->crtc = NULL;
}

1280
static void __intel_fbc_post_update(struct intel_crtc *crtc)
1281
{
V
Ville Syrjälä 已提交
1282 1283
	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
	struct intel_fbc *fbc = &i915->fbc;
1284

V
Ville Syrjälä 已提交
1285
	drm_WARN_ON(&i915->drm, !mutex_is_locked(&fbc->lock));
1286

V
Ville Syrjälä 已提交
1287
	if (fbc->crtc != crtc)
1288 1289
		return;

1290 1291
	fbc->flip_pending = false;

V
Ville Syrjälä 已提交
1292
	if (!i915->params.enable_fbc) {
1293 1294
		intel_fbc_deactivate(fbc, "disabled at runtime per module param");
		__intel_fbc_disable(fbc);
1295 1296 1297 1298

		return;
	}

1299
	intel_fbc_get_reg_params(fbc, crtc);
1300

1301
	if (!intel_fbc_can_activate(crtc))
1302 1303
		return;

1304
	if (!fbc->busy_bits)
1305
		intel_fbc_activate(fbc);
1306
	else
1307
		intel_fbc_deactivate(fbc, "frontbuffer write");
P
Paulo Zanoni 已提交
1308 1309
}

1310 1311
void intel_fbc_post_update(struct intel_atomic_state *state,
			   struct intel_crtc *crtc)
P
Paulo Zanoni 已提交
1312
{
1313 1314 1315
	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
	const struct intel_plane_state *plane_state =
		intel_atomic_get_new_plane_state(state, plane);
1316
	struct intel_fbc *fbc = plane->fbc;
1317

1318
	if (!fbc || !plane_state)
1319 1320
		return;

1321
	mutex_lock(&fbc->lock);
1322
	__intel_fbc_post_update(crtc);
1323
	mutex_unlock(&fbc->lock);
1324 1325
}

1326 1327
static unsigned int intel_fbc_get_frontbuffer_bit(struct intel_fbc *fbc)
{
V
Ville Syrjälä 已提交
1328
	if (fbc->crtc)
1329 1330 1331 1332 1333
		return to_intel_plane(fbc->crtc->base.primary)->frontbuffer_bit;
	else
		return fbc->possible_framebuffer_bits;
}

V
Ville Syrjälä 已提交
1334
void intel_fbc_invalidate(struct drm_i915_private *i915,
1335 1336 1337
			  unsigned int frontbuffer_bits,
			  enum fb_op_origin origin)
{
V
Ville Syrjälä 已提交
1338
	struct intel_fbc *fbc = &i915->fbc;
1339

V
Ville Syrjälä 已提交
1340
	if (!HAS_FBC(i915))
1341 1342
		return;

1343
	if (origin == ORIGIN_FLIP || origin == ORIGIN_CURSOR_UPDATE)
1344 1345
		return;

1346
	mutex_lock(&fbc->lock);
P
Paulo Zanoni 已提交
1347

1348
	fbc->busy_bits |= intel_fbc_get_frontbuffer_bit(fbc) & frontbuffer_bits;
1349

V
Ville Syrjälä 已提交
1350
	if (fbc->crtc && fbc->busy_bits)
1351
		intel_fbc_deactivate(fbc, "frontbuffer write");
P
Paulo Zanoni 已提交
1352

1353
	mutex_unlock(&fbc->lock);
1354 1355
}

V
Ville Syrjälä 已提交
1356
void intel_fbc_flush(struct drm_i915_private *i915,
1357
		     unsigned int frontbuffer_bits, enum fb_op_origin origin)
1358
{
V
Ville Syrjälä 已提交
1359
	struct intel_fbc *fbc = &i915->fbc;
1360

V
Ville Syrjälä 已提交
1361
	if (!HAS_FBC(i915))
1362 1363
		return;

1364
	mutex_lock(&fbc->lock);
1365

1366
	fbc->busy_bits &= ~frontbuffer_bits;
1367

1368
	if (origin == ORIGIN_FLIP || origin == ORIGIN_CURSOR_UPDATE)
1369 1370
		goto out;

V
Ville Syrjälä 已提交
1371
	if (!fbc->busy_bits && fbc->crtc &&
1372
	    (frontbuffer_bits & intel_fbc_get_frontbuffer_bit(fbc))) {
1373
		if (fbc->active)
1374
			intel_fbc_nuke(fbc);
1375
		else if (!fbc->flip_pending)
1376
			__intel_fbc_post_update(fbc->crtc);
1377
	}
P
Paulo Zanoni 已提交
1378

1379
out:
1380
	mutex_unlock(&fbc->lock);
1381 1382
}

1383 1384
/**
 * intel_fbc_choose_crtc - select a CRTC to enable FBC on
V
Ville Syrjälä 已提交
1385
 * @i915: i915 device instance
1386 1387 1388 1389 1390 1391 1392
 * @state: the atomic state structure
 *
 * This function looks at the proposed state for CRTCs and planes, then chooses
 * which pipe is going to have FBC by setting intel_crtc_state->enable_fbc to
 * true.
 *
 * Later, intel_fbc_enable is going to look for state->enable_fbc and then maybe
V
Ville Syrjälä 已提交
1393
 * enable FBC for the chosen CRTC. If it does, it will set i915->fbc.crtc.
1394
 */
V
Ville Syrjälä 已提交
1395
void intel_fbc_choose_crtc(struct drm_i915_private *i915,
1396
			   struct intel_atomic_state *state)
1397
{
V
Ville Syrjälä 已提交
1398
	struct intel_fbc *fbc = &i915->fbc;
1399 1400
	struct intel_plane *plane;
	struct intel_plane_state *plane_state;
1401
	bool crtc_chosen = false;
1402
	int i;
1403 1404 1405

	mutex_lock(&fbc->lock);

1406 1407
	/* Does this atomic commit involve the CRTC currently tied to FBC? */
	if (fbc->crtc &&
1408
	    !intel_atomic_get_new_crtc_state(state, fbc->crtc))
1409 1410
		goto out;

1411
	if (!intel_fbc_can_enable(fbc))
1412 1413
		goto out;

1414 1415 1416 1417
	/* Simply choose the first CRTC that is compatible and has a visible
	 * plane. We could go for fancier schemes such as checking the plane
	 * size, but this would just affect the few platforms that don't tie FBC
	 * to pipe or plane A. */
1418 1419
	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
		struct intel_crtc_state *crtc_state;
1420
		struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
1421

1422
		if (plane->fbc != fbc)
1423 1424
			continue;

1425
		if (!plane_state->uapi.visible)
1426 1427
			continue;

1428
		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
1429

1430
		crtc_state->enable_fbc = true;
1431
		crtc_chosen = true;
1432
		break;
1433 1434
	}

1435 1436 1437
	if (!crtc_chosen)
		fbc->no_fbc_reason = "no suitable CRTC for FBC";

1438 1439 1440 1441
out:
	mutex_unlock(&fbc->lock);
}

1442 1443 1444
/**
 * intel_fbc_enable: tries to enable FBC on the CRTC
 * @crtc: the CRTC
1445
 * @state: corresponding &drm_crtc_state for @crtc
1446
 *
1447
 * This function checks if the given CRTC was chosen for FBC, then enables it if
1448 1449 1450
 * possible. Notice that it doesn't activate FBC. It is valid to call
 * intel_fbc_enable multiple times for the same pipe without an
 * intel_fbc_disable in the middle, as long as it is deactivated.
1451
 */
1452 1453
static void intel_fbc_enable(struct intel_atomic_state *state,
			     struct intel_crtc *crtc)
1454
{
V
Ville Syrjälä 已提交
1455
	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1456 1457 1458 1459 1460
	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
	const struct intel_crtc_state *crtc_state =
		intel_atomic_get_new_crtc_state(state, crtc);
	const struct intel_plane_state *plane_state =
		intel_atomic_get_new_plane_state(state, plane);
1461 1462
	struct intel_fbc *fbc = plane->fbc;
	struct intel_fbc_state_cache *cache;
1463
	int min_limit;
1464

1465
	if (!fbc || !plane_state)
1466 1467
		return;

1468 1469
	cache = &fbc->state_cache;

1470 1471 1472
	min_limit = intel_fbc_min_limit(plane_state->hw.fb ?
					plane_state->hw.fb->format->cpp[0] : 0);

1473
	mutex_lock(&fbc->lock);
1474

V
Ville Syrjälä 已提交
1475
	if (fbc->crtc) {
1476 1477 1478 1479
		if (fbc->crtc != crtc)
			goto out;

		if (fbc->limit >= min_limit &&
1480
		    !intel_fbc_cfb_size_changed(fbc))
1481
			goto out;
1482

1483
		__intel_fbc_disable(fbc);
1484
	}
1485

V
Ville Syrjälä 已提交
1486
	drm_WARN_ON(&i915->drm, fbc->active);
1487

1488
	intel_fbc_update_state_cache(crtc, crtc_state, plane_state);
1489 1490 1491 1492 1493

	/* FIXME crtc_state->enable_fbc lies :( */
	if (!cache->plane.visible)
		goto out;

1494
	if (intel_fbc_alloc_cfb(fbc, intel_fbc_cfb_size(fbc, cache), min_limit)) {
1495
		cache->plane.visible = false;
1496
		fbc->no_fbc_reason = "not enough stolen memory";
1497 1498 1499
		goto out;
	}

V
Ville Syrjälä 已提交
1500
	drm_dbg_kms(&i915->drm, "Enabling FBC on pipe %c\n",
1501
		    pipe_name(crtc->pipe));
1502
	fbc->no_fbc_reason = "FBC enabled but not active yet\n";
1503

1504
	fbc->crtc = crtc;
1505

1506
	intel_fbc_program_cfb(fbc);
1507
out:
1508
	mutex_unlock(&fbc->lock);
1509 1510 1511
}

/**
1512
 * intel_fbc_disable - disable FBC if it's associated with crtc
1513 1514 1515 1516
 * @crtc: the CRTC
 *
 * This function disables FBC if it's associated with the provided CRTC.
 */
1517
void intel_fbc_disable(struct intel_crtc *crtc)
1518
{
1519
	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1520
	struct intel_fbc *fbc = plane->fbc;
1521

1522
	if (!fbc)
1523 1524
		return;

1525
	mutex_lock(&fbc->lock);
1526
	if (fbc->crtc == crtc)
1527
		__intel_fbc_disable(fbc);
1528
	mutex_unlock(&fbc->lock);
1529 1530
}

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
/**
 * intel_fbc_update: enable/disable FBC on the CRTC
 * @state: atomic state
 * @crtc: the CRTC
 *
 * This function checks if the given CRTC was chosen for FBC, then enables it if
 * possible. Notice that it doesn't activate FBC. It is valid to call
 * intel_fbc_update multiple times for the same pipe without an
 * intel_fbc_disable in the middle.
 */
void intel_fbc_update(struct intel_atomic_state *state,
		      struct intel_crtc *crtc)
{
	const struct intel_crtc_state *crtc_state =
		intel_atomic_get_new_crtc_state(state, crtc);

	if (crtc_state->update_pipe && !crtc_state->enable_fbc)
		intel_fbc_disable(crtc);
	else
		intel_fbc_enable(state, crtc);
}

1553
/**
1554
 * intel_fbc_global_disable - globally disable FBC
V
Ville Syrjälä 已提交
1555
 * @i915: i915 device instance
1556 1557 1558
 *
 * This function disables FBC regardless of which CRTC is associated with it.
 */
V
Ville Syrjälä 已提交
1559
void intel_fbc_global_disable(struct drm_i915_private *i915)
1560
{
V
Ville Syrjälä 已提交
1561
	struct intel_fbc *fbc = &i915->fbc;
1562

V
Ville Syrjälä 已提交
1563
	if (!HAS_FBC(i915))
1564 1565
		return;

1566
	mutex_lock(&fbc->lock);
V
Ville Syrjälä 已提交
1567
	if (fbc->crtc) {
V
Ville Syrjälä 已提交
1568
		drm_WARN_ON(&i915->drm, fbc->crtc->active);
1569
		__intel_fbc_disable(fbc);
1570
	}
1571
	mutex_unlock(&fbc->lock);
1572 1573
}

1574 1575
static void intel_fbc_underrun_work_fn(struct work_struct *work)
{
V
Ville Syrjälä 已提交
1576
	struct drm_i915_private *i915 =
1577
		container_of(work, struct drm_i915_private, fbc.underrun_work);
V
Ville Syrjälä 已提交
1578
	struct intel_fbc *fbc = &i915->fbc;
1579 1580 1581 1582

	mutex_lock(&fbc->lock);

	/* Maybe we were scheduled twice. */
V
Ville Syrjälä 已提交
1583
	if (fbc->underrun_detected || !fbc->crtc)
1584 1585
		goto out;

V
Ville Syrjälä 已提交
1586
	drm_dbg_kms(&i915->drm, "Disabling FBC due to FIFO underrun.\n");
1587 1588
	fbc->underrun_detected = true;

1589
	intel_fbc_deactivate(fbc, "FIFO underrun");
1590 1591 1592 1593
out:
	mutex_unlock(&fbc->lock);
}

1594 1595
/*
 * intel_fbc_reset_underrun - reset FBC fifo underrun status.
1596
 * @fbc: The FBC instance
1597 1598 1599 1600
 *
 * See intel_fbc_handle_fifo_underrun_irq(). For automated testing we
 * want to re-enable FBC after an underrun to increase test coverage.
 */
1601
int intel_fbc_reset_underrun(struct intel_fbc *fbc)
1602
{
1603
	struct drm_i915_private *i915 = fbc->i915;
1604 1605
	int ret;

1606
	cancel_work_sync(&fbc->underrun_work);
1607

1608
	ret = mutex_lock_interruptible(&fbc->lock);
1609 1610 1611
	if (ret)
		return ret;

1612
	if (fbc->underrun_detected) {
V
Ville Syrjälä 已提交
1613
		drm_dbg_kms(&i915->drm,
1614
			    "Re-allowing FBC after fifo underrun\n");
1615
		fbc->no_fbc_reason = "FIFO underrun cleared";
1616 1617
	}

1618 1619
	fbc->underrun_detected = false;
	mutex_unlock(&fbc->lock);
1620 1621 1622 1623

	return 0;
}

1624 1625
/**
 * intel_fbc_handle_fifo_underrun_irq - disable FBC when we get a FIFO underrun
1626
 * @fbc: The FBC instance
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
 *
 * Without FBC, most underruns are harmless and don't really cause too many
 * problems, except for an annoying message on dmesg. With FBC, underruns can
 * become black screens or even worse, especially when paired with bad
 * watermarks. So in order for us to be on the safe side, completely disable FBC
 * in case we ever detect a FIFO underrun on any pipe. An underrun on any pipe
 * already suggests that watermarks may be bad, so try to be as safe as
 * possible.
 *
 * This function is called from the IRQ handler.
 */
1638
void intel_fbc_handle_fifo_underrun_irq(struct intel_fbc *fbc)
1639
{
1640
	if (!HAS_FBC(fbc->i915))
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
		return;

	/* There's no guarantee that underrun_detected won't be set to true
	 * right after this check and before the work is scheduled, but that's
	 * not a problem since we'll check it again under the work function
	 * while FBC is locked. This check here is just to prevent us from
	 * unnecessarily scheduling the work, and it relies on the fact that we
	 * never switch underrun_detect back to false after it's true. */
	if (READ_ONCE(fbc->underrun_detected))
		return;

	schedule_work(&fbc->underrun_work);
}

1655 1656 1657 1658 1659 1660 1661 1662 1663
/*
 * The DDX driver changes its behavior depending on the value it reads from
 * i915.enable_fbc, so sanitize it by translating the default value into either
 * 0 or 1 in order to allow it to know what's going on.
 *
 * Notice that this is done at driver initialization and we still allow user
 * space to change the value during runtime without sanitizing it again. IGT
 * relies on being able to change i915.enable_fbc at runtime.
 */
V
Ville Syrjälä 已提交
1664
static int intel_sanitize_fbc_option(struct drm_i915_private *i915)
1665
{
V
Ville Syrjälä 已提交
1666 1667
	if (i915->params.enable_fbc >= 0)
		return !!i915->params.enable_fbc;
1668

V
Ville Syrjälä 已提交
1669
	if (!HAS_FBC(i915))
1670 1671
		return 0;

V
Ville Syrjälä 已提交
1672
	if (IS_BROADWELL(i915) || DISPLAY_VER(i915) >= 9)
1673 1674 1675 1676 1677
		return 1;

	return 0;
}

V
Ville Syrjälä 已提交
1678
static bool need_fbc_vtd_wa(struct drm_i915_private *i915)
1679 1680
{
	/* WaFbcTurnOffFbcWhenHyperVisorIsUsed:skl,bxt */
1681
	if (intel_vtd_active() &&
V
Ville Syrjälä 已提交
1682 1683
	    (IS_SKYLAKE(i915) || IS_BROXTON(i915))) {
		drm_info(&i915->drm,
1684
			 "Disabling framebuffer compression (FBC) to prevent screen flicker with VT-d enabled\n");
1685 1686 1687 1688 1689 1690
		return true;
	}

	return false;
}

R
Rodrigo Vivi 已提交
1691 1692
/**
 * intel_fbc_init - Initialize FBC
V
Ville Syrjälä 已提交
1693
 * @i915: the i915 device
R
Rodrigo Vivi 已提交
1694 1695 1696
 *
 * This function might be called during PM init process.
 */
V
Ville Syrjälä 已提交
1697
void intel_fbc_init(struct drm_i915_private *i915)
1698
{
V
Ville Syrjälä 已提交
1699
	struct intel_fbc *fbc = &i915->fbc;
1700

1701
	fbc->i915 = i915;
1702
	INIT_WORK(&fbc->underrun_work, intel_fbc_underrun_work_fn);
1703 1704
	mutex_init(&fbc->lock);
	fbc->active = false;
P
Paulo Zanoni 已提交
1705

V
Ville Syrjälä 已提交
1706 1707
	if (!drm_mm_initialized(&i915->mm.stolen))
		mkwrite_device_info(i915)->display.has_fbc = false;
1708

V
Ville Syrjälä 已提交
1709 1710
	if (need_fbc_vtd_wa(i915))
		mkwrite_device_info(i915)->display.has_fbc = false;
1711

V
Ville Syrjälä 已提交
1712 1713 1714
	i915->params.enable_fbc = intel_sanitize_fbc_option(i915);
	drm_dbg_kms(&i915->drm, "Sanitized enable_fbc value: %d\n",
		    i915->params.enable_fbc);
1715

V
Ville Syrjälä 已提交
1716
	if (!HAS_FBC(i915)) {
1717
		fbc->no_fbc_reason = "unsupported by this chipset";
1718 1719 1720
		return;
	}

V
Ville Syrjälä 已提交
1721
	if (DISPLAY_VER(i915) >= 7)
V
Ville Syrjälä 已提交
1722
		fbc->funcs = &ivb_fbc_funcs;
V
Ville Syrjälä 已提交
1723
	else if (DISPLAY_VER(i915) == 6)
1724
		fbc->funcs = &snb_fbc_funcs;
V
Ville Syrjälä 已提交
1725
	else if (DISPLAY_VER(i915) == 5)
1726
		fbc->funcs = &ilk_fbc_funcs;
V
Ville Syrjälä 已提交
1727
	else if (IS_G4X(i915))
1728
		fbc->funcs = &g4x_fbc_funcs;
V
Ville Syrjälä 已提交
1729
	else if (DISPLAY_VER(i915) == 4)
1730
		fbc->funcs = &i965_fbc_funcs;
1731 1732 1733
	else
		fbc->funcs = &i8xx_fbc_funcs;

1734
	/* We still don't have any sort of hardware state readout for FBC, so
1735 1736
	 * deactivate it in case the BIOS activated it to make sure software
	 * matches the hardware state. */
1737 1738
	if (intel_fbc_hw_is_active(fbc))
		intel_fbc_hw_deactivate(fbc);
1739
}