intel_sprite.c 63.0 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 24 25 26 27 28 29 30 31
/*
 * Copyright © 2011 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.
 *
 * Authors:
 *   Jesse Barnes <jbarnes@virtuousgeek.org>
 *
 * New plane/sprite handling.
 *
 * The older chips had a separate interface for programming plane related
 * registers; newer ones are much simpler and we can use the new DRM plane
 * support.
 */
32
#include <drm/drmP.h>
33
#include <drm/drm_atomic_helper.h>
34 35
#include <drm/drm_crtc.h>
#include <drm/drm_fourcc.h>
36
#include <drm/drm_rect.h>
37
#include <drm/drm_atomic.h>
38
#include <drm/drm_plane_helper.h>
39
#include "intel_drv.h"
40
#include "intel_frontbuffer.h"
41
#include <drm/i915_drm.h>
42
#include "i915_drv.h"
43
#include <drm/drm_color_mgmt.h>
44

45 46
int intel_usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
			     int usecs)
47 48
{
	/* paranoia */
49
	if (!adjusted_mode->crtc_htotal)
50 51
		return 1;

52 53
	return DIV_ROUND_UP(usecs * adjusted_mode->crtc_clock,
			    1000 * adjusted_mode->crtc_htotal);
54 55
}

56 57 58 59 60
/* FIXME: We should instead only take spinlocks once for the entire update
 * instead of once per mmio. */
#if IS_ENABLED(CONFIG_PROVE_LOCKING)
#define VBLANK_EVASION_TIME_US 250
#else
61
#define VBLANK_EVASION_TIME_US 100
62
#endif
63

64 65
/**
 * intel_pipe_update_start() - start update of a set of display registers
66
 * @new_crtc_state: the new crtc state
67 68 69 70 71 72 73
 *
 * Mark the start of an update to pipe registers that should be updated
 * atomically regarding vblank. If the next vblank will happens within
 * the next 100 us, this function waits until the vblank passes.
 *
 * After a successful call to this function, interrupts will be disabled
 * until a subsequent call to intel_pipe_update_end(). That is done to
74
 * avoid random delays.
75
 */
76
void intel_pipe_update_start(const struct intel_crtc_state *new_crtc_state)
77
{
78
	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
79
	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80
	const struct drm_display_mode *adjusted_mode = &new_crtc_state->base.adjusted_mode;
81 82
	long timeout = msecs_to_jiffies_timeout(1);
	int scanline, min, max, vblank_start;
83
	wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
84
	bool need_vlv_dsi_wa = (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
85
		intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI);
86
	DEFINE_WAIT(wait);
87
	u32 psr_status;
88

89 90
	vblank_start = adjusted_mode->crtc_vblank_start;
	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
91 92 93
		vblank_start = DIV_ROUND_UP(vblank_start, 2);

	/* FIXME needs to be calibrated sensibly */
94 95
	min = vblank_start - intel_usecs_to_scanlines(adjusted_mode,
						      VBLANK_EVASION_TIME_US);
96 97 98
	max = vblank_start - 1;

	if (min <= 0 || max <= 0)
99
		goto irq_disable;
100

101
	if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
102 103 104 105 106 107 108
		goto irq_disable;

	/*
	 * Wait for psr to idle out after enabling the VBL interrupts
	 * VBL interrupts will start the PSR exit and prevent a PSR
	 * re-entry as well.
	 */
109 110 111
	if (intel_psr_wait_for_idle(new_crtc_state, &psr_status))
		DRM_ERROR("PSR idle timed out 0x%x, atomic update may fail\n",
			  psr_status);
112 113

	local_irq_disable();
114

115 116 117
	crtc->debug.min_vbl = min;
	crtc->debug.max_vbl = max;
	trace_i915_pipe_update_start(crtc);
118

119 120 121 122 123 124
	for (;;) {
		/*
		 * prepare_to_wait() has a memory barrier, which guarantees
		 * other CPUs can see the task state update by the time we
		 * read the scanline.
		 */
125
		prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
126 127 128 129 130

		scanline = intel_get_crtc_scanline(crtc);
		if (scanline < min || scanline > max)
			break;

131
		if (!timeout) {
132 133 134 135 136 137 138 139 140 141 142 143
			DRM_ERROR("Potential atomic update failure on pipe %c\n",
				  pipe_name(crtc->pipe));
			break;
		}

		local_irq_enable();

		timeout = schedule_timeout(timeout);

		local_irq_disable();
	}

144
	finish_wait(wq, &wait);
145

146
	drm_crtc_vblank_put(&crtc->base);
147

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	/*
	 * On VLV/CHV DSI the scanline counter would appear to
	 * increment approx. 1/3 of a scanline before start of vblank.
	 * The registers still get latched at start of vblank however.
	 * This means we must not write any registers on the first
	 * line of vblank (since not the whole line is actually in
	 * vblank). And unfortunately we can't use the interrupt to
	 * wait here since it will fire too soon. We could use the
	 * frame start interrupt instead since it will fire after the
	 * critical scanline, but that would require more changes
	 * in the interrupt code. So for now we'll just do the nasty
	 * thing and poll for the bad scanline to pass us by.
	 *
	 * FIXME figure out if BXT+ DSI suffers from this as well
	 */
	while (need_vlv_dsi_wa && scanline == vblank_start)
		scanline = intel_get_crtc_scanline(crtc);

166 167
	crtc->debug.scanline_start = scanline;
	crtc->debug.start_vbl_time = ktime_get();
168
	crtc->debug.start_vbl_count = intel_crtc_get_vblank_counter(crtc);
169

170
	trace_i915_pipe_update_vblank_evaded(crtc);
171 172 173 174
	return;

irq_disable:
	local_irq_disable();
175 176
}

177 178
/**
 * intel_pipe_update_end() - end update of a set of display registers
179
 * @new_crtc_state: the new crtc state
180 181 182
 *
 * Mark the end of an update started with intel_pipe_update_start(). This
 * re-enables interrupts and verifies the update was actually completed
183
 * before a vblank.
184
 */
185
void intel_pipe_update_end(struct intel_crtc_state *new_crtc_state)
186
{
187
	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
188
	enum pipe pipe = crtc->pipe;
189
	int scanline_end = intel_get_crtc_scanline(crtc);
190
	u32 end_vbl_count = intel_crtc_get_vblank_counter(crtc);
191
	ktime_t end_vbl_time = ktime_get();
192
	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
193

194
	trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
195

196 197 198 199
	/* We're still in the vblank-evade critical section, this can't race.
	 * Would be slightly nice to just grab the vblank count and arm the
	 * event outside of the critical section - the spinlock might spin for a
	 * while ... */
200
	if (new_crtc_state->base.event) {
201 202 203
		WARN_ON(drm_crtc_vblank_get(&crtc->base) != 0);

		spin_lock(&crtc->base.dev->event_lock);
204
		drm_crtc_arm_vblank_event(&crtc->base, new_crtc_state->base.event);
205 206
		spin_unlock(&crtc->base.dev->event_lock);

207
		new_crtc_state->base.event = NULL;
208 209
	}

210 211
	local_irq_enable();

212 213 214
	if (intel_vgpu_active(dev_priv))
		return;

215 216 217 218 219 220 221 222
	if (crtc->debug.start_vbl_count &&
	    crtc->debug.start_vbl_count != end_vbl_count) {
		DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u) time %lld us, min %d, max %d, scanline start %d, end %d\n",
			  pipe_name(pipe), crtc->debug.start_vbl_count,
			  end_vbl_count,
			  ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time),
			  crtc->debug.min_vbl, crtc->debug.max_vbl,
			  crtc->debug.scanline_start, scanline_end);
223 224 225 226
	}
#ifdef CONFIG_DRM_I915_DEBUG_VBLANK_EVADE
	else if (ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time) >
		 VBLANK_EVASION_TIME_US)
227 228 229 230
		DRM_WARN("Atomic update on pipe (%c) took %lld us, max time under evasion is %u us\n",
			 pipe_name(pipe),
			 ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time),
			 VBLANK_EVASION_TIME_US);
231
#endif
232 233
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
int intel_plane_check_stride(const struct intel_plane_state *plane_state)
{
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
	const struct drm_framebuffer *fb = plane_state->base.fb;
	unsigned int rotation = plane_state->base.rotation;
	u32 stride, max_stride;

	/* FIXME other color planes? */
	stride = plane_state->color_plane[0].stride;
	max_stride = plane->max_stride(plane, fb->format->format,
				       fb->modifier, rotation);

	if (stride > max_stride) {
		DRM_DEBUG_KMS("[FB:%d] stride (%d) exceeds [PLANE:%d:%s] max stride (%d)\n",
			      fb->base.id, stride,
			      plane->base.base.id, plane->base.name, max_stride);
		return -EINVAL;
	}

	return 0;
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
int intel_plane_check_src_coordinates(struct intel_plane_state *plane_state)
{
	const struct drm_framebuffer *fb = plane_state->base.fb;
	struct drm_rect *src = &plane_state->base.src;
	u32 src_x, src_y, src_w, src_h;

	/*
	 * Hardware doesn't handle subpixel coordinates.
	 * Adjust to (macro)pixel boundary, but be careful not to
	 * increase the source viewport size, because that could
	 * push the downscaling factor out of bounds.
	 */
	src_x = src->x1 >> 16;
	src_w = drm_rect_width(src) >> 16;
	src_y = src->y1 >> 16;
	src_h = drm_rect_height(src) >> 16;

	src->x1 = src_x << 16;
	src->x2 = (src_x + src_w) << 16;
	src->y1 = src_y << 16;
	src->y2 = (src_y + src_h) << 16;

	if (fb->format->is_yuv &&
	    (src_x & 1 || src_w & 1)) {
		DRM_DEBUG_KMS("src x/w (%u, %u) must be a multiple of 2 for YUV planes\n",
			      src_x, src_w);
		return -EINVAL;
	}

285 286 287 288 289 290 291 292
	if (fb->format->is_yuv &&
	    fb->format->num_planes > 1 &&
	    (src_y & 1 || src_h & 1)) {
		DRM_DEBUG_KMS("src y/h (%u, %u) must be a multiple of 2 for planar YUV planes\n",
			      src_y, src_h);
		return -EINVAL;
	}

293 294 295
	return 0;
}

296
static unsigned int
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
skl_plane_max_stride(struct intel_plane *plane,
		     u32 pixel_format, u64 modifier,
		     unsigned int rotation)
{
	int cpp = drm_format_plane_cpp(pixel_format, 0);

	/*
	 * "The stride in bytes must not exceed the
	 * of the size of 8K pixels and 32K bytes."
	 */
	if (drm_rotation_90_or_270(rotation))
		return min(8192, 32768 / cpp);
	else
		return min(8192 * cpp, 32768);
}

313
static void
314
skl_program_scaler(struct intel_plane *plane,
315 316 317
		   const struct intel_crtc_state *crtc_state,
		   const struct intel_plane_state *plane_state)
{
318
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
319 320 321 322 323 324 325 326 327 328 329 330
	enum pipe pipe = plane->pipe;
	int scaler_id = plane_state->scaler_id;
	const struct intel_scaler *scaler =
		&crtc_state->scaler_state.scalers[scaler_id];
	int crtc_x = plane_state->base.dst.x1;
	int crtc_y = plane_state->base.dst.y1;
	uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
	uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
	u16 y_hphase, uv_rgb_hphase;
	u16 y_vphase, uv_rgb_vphase;

	/* TODO: handle sub-pixel coordinates */
331 332
	if (plane_state->base.fb->format->format == DRM_FORMAT_NV12 &&
	    !icl_is_hdr_plane(plane)) {
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
		y_hphase = skl_scaler_calc_phase(1, false);
		y_vphase = skl_scaler_calc_phase(1, false);

		/* MPEG2 chroma siting convention */
		uv_rgb_hphase = skl_scaler_calc_phase(2, true);
		uv_rgb_vphase = skl_scaler_calc_phase(2, false);
	} else {
		/* not used */
		y_hphase = 0;
		y_vphase = 0;

		uv_rgb_hphase = skl_scaler_calc_phase(1, false);
		uv_rgb_vphase = skl_scaler_calc_phase(1, false);
	}

	I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id),
349
		      PS_SCALER_EN | PS_PLANE_SEL(plane->id) | scaler->mode);
350 351 352 353 354 355
	I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
	I915_WRITE_FW(SKL_PS_VPHASE(pipe, scaler_id),
		      PS_Y_PHASE(y_vphase) | PS_UV_RGB_PHASE(uv_rgb_vphase));
	I915_WRITE_FW(SKL_PS_HPHASE(pipe, scaler_id),
		      PS_Y_PHASE(y_hphase) | PS_UV_RGB_PHASE(uv_rgb_hphase));
	I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (crtc_x << 16) | crtc_y);
356
	I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (crtc_w << 16) | crtc_h);
357 358
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/* Preoffset values for YUV to RGB Conversion */
#define PREOFF_YUV_TO_RGB_HI		0x1800
#define PREOFF_YUV_TO_RGB_ME		0x1F00
#define PREOFF_YUV_TO_RGB_LO		0x1800

#define  ROFF(x)          (((x) & 0xffff) << 16)
#define  GOFF(x)          (((x) & 0xffff) << 0)
#define  BOFF(x)          (((x) & 0xffff) << 16)

static void
icl_program_input_csc_coeff(const struct intel_crtc_state *crtc_state,
			    const struct intel_plane_state *plane_state)
{
	struct drm_i915_private *dev_priv =
		to_i915(plane_state->base.plane->dev);
	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
	enum pipe pipe = crtc->pipe;
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
	enum plane_id plane_id = plane->id;

	static const u16 input_csc_matrix[][9] = {
		/*
		 * BT.601 full range YCbCr -> full range RGB
		 * The matrix required is :
		 * [1.000, 0.000, 1.371,
		 *  1.000, -0.336, -0.698,
		 *  1.000, 1.732, 0.0000]
		 */
		[DRM_COLOR_YCBCR_BT601] = {
			0x7AF8, 0x7800, 0x0,
			0x8B28, 0x7800, 0x9AC0,
			0x0, 0x7800, 0x7DD8,
		},
		/*
		 * BT.709 full range YCbCr -> full range RGB
		 * The matrix required is :
		 * [1.000, 0.000, 1.574,
		 *  1.000, -0.187, -0.468,
		 *  1.000, 1.855, 0.0000]
		 */
		[DRM_COLOR_YCBCR_BT709] = {
			0x7C98, 0x7800, 0x0,
			0x9EF8, 0x7800, 0xABF8,
			0x0, 0x7800,  0x7ED8,
		},
	};

	/* Matrix for Limited Range to Full Range Conversion */
	static const u16 input_csc_matrix_lr[][9] = {
		/*
		 * BT.601 Limted range YCbCr -> full range RGB
		 * The matrix required is :
		 * [1.164384, 0.000, 1.596370,
		 *  1.138393, -0.382500, -0.794598,
		 *  1.138393, 1.971696, 0.0000]
		 */
		[DRM_COLOR_YCBCR_BT601] = {
			0x7CC8, 0x7950, 0x0,
			0x8CB8, 0x7918, 0x9C40,
			0x0, 0x7918, 0x7FC8,
		},
		/*
		 * BT.709 Limited range YCbCr -> full range RGB
		 * The matrix required is :
		 * [1.164, 0.000, 1.833671,
		 *  1.138393, -0.213249, -0.532909,
		 *  1.138393, 2.112402, 0.0000]
		 */
		[DRM_COLOR_YCBCR_BT709] = {
			0x7EA8, 0x7950, 0x0,
			0x8888, 0x7918, 0xADA8,
			0x0, 0x7918,  0x6870,
		},
	};
	const u16 *csc;

	if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
		csc = input_csc_matrix[plane_state->base.color_encoding];
	else
		csc = input_csc_matrix_lr[plane_state->base.color_encoding];

	I915_WRITE_FW(PLANE_INPUT_CSC_COEFF(pipe, plane_id, 0), ROFF(csc[0]) |
		      GOFF(csc[1]));
	I915_WRITE_FW(PLANE_INPUT_CSC_COEFF(pipe, plane_id, 1), BOFF(csc[2]));
	I915_WRITE_FW(PLANE_INPUT_CSC_COEFF(pipe, plane_id, 2), ROFF(csc[3]) |
		      GOFF(csc[4]));
	I915_WRITE_FW(PLANE_INPUT_CSC_COEFF(pipe, plane_id, 3), BOFF(csc[5]));
	I915_WRITE_FW(PLANE_INPUT_CSC_COEFF(pipe, plane_id, 4), ROFF(csc[6]) |
		      GOFF(csc[7]));
	I915_WRITE_FW(PLANE_INPUT_CSC_COEFF(pipe, plane_id, 5), BOFF(csc[8]));

	I915_WRITE_FW(PLANE_INPUT_CSC_PREOFF(pipe, plane_id, 0),
		      PREOFF_YUV_TO_RGB_HI);
	I915_WRITE_FW(PLANE_INPUT_CSC_PREOFF(pipe, plane_id, 1),
		      PREOFF_YUV_TO_RGB_ME);
	I915_WRITE_FW(PLANE_INPUT_CSC_PREOFF(pipe, plane_id, 2),
		      PREOFF_YUV_TO_RGB_LO);
	I915_WRITE_FW(PLANE_INPUT_CSC_POSTOFF(pipe, plane_id, 0), 0x0);
	I915_WRITE_FW(PLANE_INPUT_CSC_POSTOFF(pipe, plane_id, 1), 0x0);
	I915_WRITE_FW(PLANE_INPUT_CSC_POSTOFF(pipe, plane_id, 2), 0x0);
}

461
static void
462 463 464 465
skl_program_plane(struct intel_plane *plane,
		  const struct intel_crtc_state *crtc_state,
		  const struct intel_plane_state *plane_state,
		  int color_plane, bool slave, u32 plane_ctl)
466
{
467 468 469
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum plane_id plane_id = plane->id;
	enum pipe pipe = plane->pipe;
470
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
471 472
	u32 surf_addr = plane_state->color_plane[color_plane].offset;
	u32 stride = skl_plane_stride(plane_state, color_plane);
473
	u32 aux_stride = skl_plane_stride(plane_state, 1);
474 475
	int crtc_x = plane_state->base.dst.x1;
	int crtc_y = plane_state->base.dst.y1;
476 477
	uint32_t x = plane_state->color_plane[color_plane].x;
	uint32_t y = plane_state->color_plane[color_plane].y;
478 479
	uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
	uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
480
	struct intel_plane *linked = plane_state->linked_plane;
481
	const struct drm_framebuffer *fb = plane_state->base.fb;
482
	unsigned long irqflags;
483
	u32 keymsk = 0, keymax = 0;
484

485 486 487 488
	/* Sizes are 0 based */
	src_w--;
	src_h--;

489 490
	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);

491
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
492
		I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
493
			      plane_state->color_ctl);
494

495 496 497
	if (fb->format->is_yuv && icl_is_hdr_plane(plane))
		icl_program_input_csc_coeff(crtc_state, plane_state);

498
	if (key->flags) {
499
		I915_WRITE_FW(PLANE_KEYVAL(pipe, plane_id), key->min_value);
500 501 502

		keymax |= key->max_value & 0xffffff;
		keymsk |= key->channel_mask & 0x3ffffff;
503 504
	}

505 506 507 508 509 510 511 512
	keymax |= (plane_state->base.alpha >> 8) << PLANE_KEYMAX_ALPHA_SHIFT;

	if (plane_state->base.alpha < 0xff00)
		keymsk |= PLANE_KEYMSK_ALPHA_ENABLE;

	I915_WRITE_FW(PLANE_KEYMAX(pipe, plane_id), keymax);
	I915_WRITE_FW(PLANE_KEYMSK(pipe, plane_id), keymsk);

513 514 515
	I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (y << 16) | x);
	I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
	I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
516
	I915_WRITE_FW(PLANE_AUX_DIST(pipe, plane_id),
517
		      (plane_state->color_plane[1].offset - surf_addr) | aux_stride);
518 519 520 521 522

	if (INTEL_GEN(dev_priv) < 11)
		I915_WRITE_FW(PLANE_AUX_OFFSET(pipe, plane_id),
			      (plane_state->color_plane[1].y << 16) |
			       plane_state->color_plane[1].x);
523

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	if (icl_is_hdr_plane(plane)) {
		u32 cus_ctl = 0;

		if (linked) {
			/* Enable and use MPEG-2 chroma siting */
			cus_ctl = PLANE_CUS_ENABLE |
				PLANE_CUS_HPHASE_0 |
				PLANE_CUS_VPHASE_SIGN_NEGATIVE |
				PLANE_CUS_VPHASE_0_25;

			if (linked->id == PLANE_SPRITE5)
				cus_ctl |= PLANE_CUS_PLANE_7;
			else if (linked->id == PLANE_SPRITE4)
				cus_ctl |= PLANE_CUS_PLANE_6;
			else
				MISSING_CASE(linked->id);
		}

		I915_WRITE_FW(PLANE_CUS_CTL(pipe, plane_id), cus_ctl);
	}

545
	if (plane_state->scaler_id >= 0) {
546
		if (!slave)
547
			skl_program_scaler(plane, crtc_state, plane_state);
548

549
		I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
550
	} else {
551
		I915_WRITE_FW(PLANE_POS(pipe, plane_id), (crtc_y << 16) | crtc_x);
552 553
	}

554 555 556 557 558
	I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
	I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
		      intel_plane_ggtt_offset(plane_state) + surf_addr);

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
559 560
}

561
static void
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
skl_update_plane(struct intel_plane *plane,
		 const struct intel_crtc_state *crtc_state,
		 const struct intel_plane_state *plane_state)
{
	int color_plane = 0;

	if (plane_state->linked_plane) {
		/* Program the UV plane */
		color_plane = 1;
	}

	skl_program_plane(plane, crtc_state, plane_state,
			  color_plane, false, plane_state->ctl);
}

577
static void
578 579 580 581 582 583 584 585
icl_update_slave(struct intel_plane *plane,
		 const struct intel_crtc_state *crtc_state,
		 const struct intel_plane_state *plane_state)
{
	skl_program_plane(plane, crtc_state, plane_state, 0, true,
			  plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE);
}

586
static void
587
skl_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
588
{
589 590 591
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum plane_id plane_id = plane->id;
	enum pipe pipe = plane->pipe;
592 593 594
	unsigned long irqflags;

	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
595

596 597 598 599
	I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
	I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
600 601
}

602
static bool
603 604
skl_plane_get_hw_state(struct intel_plane *plane,
		       enum pipe *pipe)
605 606 607 608 609 610
{
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum intel_display_power_domain power_domain;
	enum plane_id plane_id = plane->id;
	bool ret;

611
	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
612 613 614
	if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
		return false;

615 616 617
	ret = I915_READ(PLANE_CTL(plane->pipe, plane_id)) & PLANE_CTL_ENABLE;

	*pipe = plane->pipe;
618 619 620 621 622 623

	intel_display_power_put(dev_priv, power_domain);

	return ret;
}

624
static void
625
chv_update_csc(const struct intel_plane_state *plane_state)
626
{
627
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
628
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
629
	const struct drm_framebuffer *fb = plane_state->base.fb;
630
	enum plane_id plane_id = plane->id;
631
	/*
632 633 634
	 * |r|   | c0 c1 c2 |   |cr|
	 * |g| = | c3 c4 c5 | x |y |
	 * |b|   | c6 c7 c8 |   |cb|
635
	 *
636
	 * Coefficients are s3.12.
637
	 *
638 639
	 * Cb and Cr apparently come in as signed already, and
	 * we always get full range data in on account of CLRC0/1.
640
	 */
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
	static const s16 csc_matrix[][9] = {
		/* BT.601 full range YCbCr -> full range RGB */
		[DRM_COLOR_YCBCR_BT601] = {
			 5743, 4096,     0,
			-2925, 4096, -1410,
			    0, 4096,  7258,
		},
		/* BT.709 full range YCbCr -> full range RGB */
		[DRM_COLOR_YCBCR_BT709] = {
			 6450, 4096,     0,
			-1917, 4096,  -767,
			    0, 4096,  7601,
		},
	};
	const s16 *csc = csc_matrix[plane_state->base.color_encoding];
656 657

	/* Seems RGB data bypasses the CSC always */
658
	if (!fb->format->is_yuv)
659 660
		return;

661
	I915_WRITE_FW(SPCSCYGOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
662 663 664
	I915_WRITE_FW(SPCSCCBOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
	I915_WRITE_FW(SPCSCCROFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));

665 666 667 668 669
	I915_WRITE_FW(SPCSCC01(plane_id), SPCSC_C1(csc[1]) | SPCSC_C0(csc[0]));
	I915_WRITE_FW(SPCSCC23(plane_id), SPCSC_C1(csc[3]) | SPCSC_C0(csc[2]));
	I915_WRITE_FW(SPCSCC45(plane_id), SPCSC_C1(csc[5]) | SPCSC_C0(csc[4]));
	I915_WRITE_FW(SPCSCC67(plane_id), SPCSC_C1(csc[7]) | SPCSC_C0(csc[6]));
	I915_WRITE_FW(SPCSCC8(plane_id), SPCSC_C0(csc[8]));
670

671 672 673
	I915_WRITE_FW(SPCSCYGICLAMP(plane_id), SPCSC_IMAX(1023) | SPCSC_IMIN(0));
	I915_WRITE_FW(SPCSCCBICLAMP(plane_id), SPCSC_IMAX(512) | SPCSC_IMIN(-512));
	I915_WRITE_FW(SPCSCCRICLAMP(plane_id), SPCSC_IMAX(512) | SPCSC_IMIN(-512));
674 675 676 677

	I915_WRITE_FW(SPCSCYGOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
	I915_WRITE_FW(SPCSCCBOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
	I915_WRITE_FW(SPCSCCROCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
678 679
}

680 681 682 683 684 685 686 687 688 689 690 691 692
#define SIN_0 0
#define COS_0 1

static void
vlv_update_clrc(const struct intel_plane_state *plane_state)
{
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	const struct drm_framebuffer *fb = plane_state->base.fb;
	enum pipe pipe = plane->pipe;
	enum plane_id plane_id = plane->id;
	int contrast, brightness, sh_scale, sh_sin, sh_cos;

693
	if (fb->format->is_yuv &&
694
	    plane_state->base.color_range == DRM_COLOR_YCBCR_LIMITED_RANGE) {
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
		/*
		 * Expand limited range to full range:
		 * Contrast is applied first and is used to expand Y range.
		 * Brightness is applied second and is used to remove the
		 * offset from Y. Saturation/hue is used to expand CbCr range.
		 */
		contrast = DIV_ROUND_CLOSEST(255 << 6, 235 - 16);
		brightness = -DIV_ROUND_CLOSEST(16 * 255, 235 - 16);
		sh_scale = DIV_ROUND_CLOSEST(128 << 7, 240 - 128);
		sh_sin = SIN_0 * sh_scale;
		sh_cos = COS_0 * sh_scale;
	} else {
		/* Pass-through everything. */
		contrast = 1 << 6;
		brightness = 0;
		sh_scale = 1 << 7;
		sh_sin = SIN_0 * sh_scale;
		sh_cos = COS_0 * sh_scale;
	}

	/* FIXME these register are single buffered :( */
	I915_WRITE_FW(SPCLRC0(pipe, plane_id),
		      SP_CONTRAST(contrast) | SP_BRIGHTNESS(brightness));
	I915_WRITE_FW(SPCLRC1(pipe, plane_id),
		      SP_SH_SIN(sh_sin) | SP_SH_COS(sh_cos));
}

722 723
static u32 vlv_sprite_ctl(const struct intel_crtc_state *crtc_state,
			  const struct intel_plane_state *plane_state)
724
{
725
	const struct drm_framebuffer *fb = plane_state->base.fb;
726
	unsigned int rotation = plane_state->base.rotation;
727
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
728
	u32 sprctl;
729

730
	sprctl = SP_ENABLE | SP_GAMMA_ENABLE;
731

V
Ville Syrjälä 已提交
732
	switch (fb->format->format) {
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	case DRM_FORMAT_YUYV:
		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
		break;
	case DRM_FORMAT_YVYU:
		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
		break;
	case DRM_FORMAT_UYVY:
		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
		break;
	case DRM_FORMAT_VYUY:
		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
		break;
	case DRM_FORMAT_RGB565:
		sprctl |= SP_FORMAT_BGR565;
		break;
	case DRM_FORMAT_XRGB8888:
		sprctl |= SP_FORMAT_BGRX8888;
		break;
	case DRM_FORMAT_ARGB8888:
		sprctl |= SP_FORMAT_BGRA8888;
		break;
	case DRM_FORMAT_XBGR2101010:
		sprctl |= SP_FORMAT_RGBX1010102;
		break;
	case DRM_FORMAT_ABGR2101010:
		sprctl |= SP_FORMAT_RGBA1010102;
		break;
	case DRM_FORMAT_XBGR8888:
		sprctl |= SP_FORMAT_RGBX8888;
		break;
	case DRM_FORMAT_ABGR8888:
		sprctl |= SP_FORMAT_RGBA8888;
		break;
	default:
767 768
		MISSING_CASE(fb->format->format);
		return 0;
769 770
	}

771 772 773
	if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
		sprctl |= SP_YUV_FORMAT_BT709;

V
Ville Syrjälä 已提交
774
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
775 776
		sprctl |= SP_TILED;

777
	if (rotation & DRM_MODE_ROTATE_180)
778 779
		sprctl |= SP_ROTATE_180;

780
	if (rotation & DRM_MODE_REFLECT_X)
781 782
		sprctl |= SP_MIRROR;

783 784 785
	if (key->flags & I915_SET_COLORKEY_SOURCE)
		sprctl |= SP_SOURCE_KEY;

786 787 788 789
	return sprctl;
}

static void
790
vlv_update_plane(struct intel_plane *plane,
791 792 793
		 const struct intel_crtc_state *crtc_state,
		 const struct intel_plane_state *plane_state)
{
794 795 796 797
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	const struct drm_framebuffer *fb = plane_state->base.fb;
	enum pipe pipe = plane->pipe;
	enum plane_id plane_id = plane->id;
798
	u32 sprctl = plane_state->ctl;
799
	u32 sprsurf_offset = plane_state->color_plane[0].offset;
800
	u32 linear_offset;
801 802 803 804 805
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
	int crtc_x = plane_state->base.dst.x1;
	int crtc_y = plane_state->base.dst.y1;
	uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
	uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
806 807
	uint32_t x = plane_state->color_plane[0].x;
	uint32_t y = plane_state->color_plane[0].y;
808 809
	unsigned long irqflags;

810 811 812 813
	/* Sizes are 0 based */
	crtc_w--;
	crtc_h--;

814
	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
815

816 817
	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);

818 819
	vlv_update_clrc(plane_state);

820
	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B)
821
		chv_update_csc(plane_state);
822

823
	if (key->flags) {
824 825 826
		I915_WRITE_FW(SPKEYMINVAL(pipe, plane_id), key->min_value);
		I915_WRITE_FW(SPKEYMAXVAL(pipe, plane_id), key->max_value);
		I915_WRITE_FW(SPKEYMSK(pipe, plane_id), key->channel_mask);
827
	}
828 829
	I915_WRITE_FW(SPSTRIDE(pipe, plane_id),
		      plane_state->color_plane[0].stride);
830
	I915_WRITE_FW(SPPOS(pipe, plane_id), (crtc_y << 16) | crtc_x);
831

V
Ville Syrjälä 已提交
832
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
833
		I915_WRITE_FW(SPTILEOFF(pipe, plane_id), (y << 16) | x);
834
	else
835
		I915_WRITE_FW(SPLINOFF(pipe, plane_id), linear_offset);
836

837
	I915_WRITE_FW(SPCONSTALPHA(pipe, plane_id), 0);
838

839 840 841 842 843 844
	I915_WRITE_FW(SPSIZE(pipe, plane_id), (crtc_h << 16) | crtc_w);
	I915_WRITE_FW(SPCNTR(pipe, plane_id), sprctl);
	I915_WRITE_FW(SPSURF(pipe, plane_id),
		      intel_plane_ggtt_offset(plane_state) + sprsurf_offset);

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
845 846 847
}

static void
848
vlv_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
849
{
850 851 852
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum pipe pipe = plane->pipe;
	enum plane_id plane_id = plane->id;
853 854 855
	unsigned long irqflags;

	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
856

857 858 859 860
	I915_WRITE_FW(SPCNTR(pipe, plane_id), 0);
	I915_WRITE_FW(SPSURF(pipe, plane_id), 0);

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
861 862
}

863
static bool
864 865
vlv_plane_get_hw_state(struct intel_plane *plane,
		       enum pipe *pipe)
866 867 868 869 870 871
{
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum intel_display_power_domain power_domain;
	enum plane_id plane_id = plane->id;
	bool ret;

872
	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
873 874 875
	if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
		return false;

876 877 878
	ret = I915_READ(SPCNTR(plane->pipe, plane_id)) & SP_ENABLE;

	*pipe = plane->pipe;
879 880 881 882 883 884

	intel_display_power_put(dev_priv, power_domain);

	return ret;
}

885 886
static u32 ivb_sprite_ctl(const struct intel_crtc_state *crtc_state,
			  const struct intel_plane_state *plane_state)
887
{
888 889 890
	struct drm_i915_private *dev_priv =
		to_i915(plane_state->base.plane->dev);
	const struct drm_framebuffer *fb = plane_state->base.fb;
891
	unsigned int rotation = plane_state->base.rotation;
892
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
893 894 895
	u32 sprctl;

	sprctl = SPRITE_ENABLE | SPRITE_GAMMA_ENABLE;
896

897 898 899 900 901
	if (IS_IVYBRIDGE(dev_priv))
		sprctl |= SPRITE_TRICKLE_FEED_DISABLE;

	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
		sprctl |= SPRITE_PIPE_CSC_ENABLE;
902

V
Ville Syrjälä 已提交
903
	switch (fb->format->format) {
904
	case DRM_FORMAT_XBGR8888:
905
		sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
906 907
		break;
	case DRM_FORMAT_XRGB8888:
908
		sprctl |= SPRITE_FORMAT_RGBX888;
909 910 911 912 913 914 915 916 917 918 919 920 921 922
		break;
	case DRM_FORMAT_YUYV:
		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
		break;
	case DRM_FORMAT_YVYU:
		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
		break;
	case DRM_FORMAT_UYVY:
		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
		break;
	case DRM_FORMAT_VYUY:
		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
		break;
	default:
923 924
		MISSING_CASE(fb->format->format);
		return 0;
925 926
	}

927 928 929
	if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
		sprctl |= SPRITE_YUV_TO_RGB_CSC_FORMAT_BT709;

930 931 932
	if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
		sprctl |= SPRITE_YUV_RANGE_CORRECTION_DISABLE;

V
Ville Syrjälä 已提交
933
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
934 935
		sprctl |= SPRITE_TILED;

936
	if (rotation & DRM_MODE_ROTATE_180)
937 938
		sprctl |= SPRITE_ROTATE_180;

939 940 941 942 943
	if (key->flags & I915_SET_COLORKEY_DESTINATION)
		sprctl |= SPRITE_DEST_KEY;
	else if (key->flags & I915_SET_COLORKEY_SOURCE)
		sprctl |= SPRITE_SOURCE_KEY;

944 945 946 947
	return sprctl;
}

static void
948
ivb_update_plane(struct intel_plane *plane,
949 950 951
		 const struct intel_crtc_state *crtc_state,
		 const struct intel_plane_state *plane_state)
{
952 953 954
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	const struct drm_framebuffer *fb = plane_state->base.fb;
	enum pipe pipe = plane->pipe;
955
	u32 sprctl = plane_state->ctl, sprscale = 0;
956
	u32 sprsurf_offset = plane_state->color_plane[0].offset;
957
	u32 linear_offset;
958 959 960 961 962
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
	int crtc_x = plane_state->base.dst.x1;
	int crtc_y = plane_state->base.dst.y1;
	uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
	uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
963 964
	uint32_t x = plane_state->color_plane[0].x;
	uint32_t y = plane_state->color_plane[0].y;
965 966 967 968
	uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
	uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
	unsigned long irqflags;

969 970 971 972 973 974
	/* Sizes are 0 based */
	src_w--;
	src_h--;
	crtc_w--;
	crtc_h--;

975
	if (crtc_w != src_w || crtc_h != src_h)
976 977
		sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;

978
	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
979

980 981
	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);

982
	if (key->flags) {
983 984 985
		I915_WRITE_FW(SPRKEYVAL(pipe), key->min_value);
		I915_WRITE_FW(SPRKEYMAX(pipe), key->max_value);
		I915_WRITE_FW(SPRKEYMSK(pipe), key->channel_mask);
986 987
	}

988
	I915_WRITE_FW(SPRSTRIDE(pipe), plane_state->color_plane[0].stride);
989
	I915_WRITE_FW(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
990

991 992
	/* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
	 * register */
993
	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
994
		I915_WRITE_FW(SPROFFSET(pipe), (y << 16) | x);
V
Ville Syrjälä 已提交
995
	else if (fb->modifier == I915_FORMAT_MOD_X_TILED)
996
		I915_WRITE_FW(SPRTILEOFF(pipe), (y << 16) | x);
997
	else
998
		I915_WRITE_FW(SPRLINOFF(pipe), linear_offset);
999

1000
	I915_WRITE_FW(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
1001
	if (IS_IVYBRIDGE(dev_priv))
1002 1003 1004 1005 1006 1007
		I915_WRITE_FW(SPRSCALE(pipe), sprscale);
	I915_WRITE_FW(SPRCTL(pipe), sprctl);
	I915_WRITE_FW(SPRSURF(pipe),
		      intel_plane_ggtt_offset(plane_state) + sprsurf_offset);

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1008 1009 1010
}

static void
1011
ivb_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
1012
{
1013 1014
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum pipe pipe = plane->pipe;
1015 1016 1017
	unsigned long irqflags;

	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1018

1019
	I915_WRITE_FW(SPRCTL(pipe), 0);
1020
	/* Can't leave the scaler enabled... */
1021
	if (IS_IVYBRIDGE(dev_priv))
1022 1023 1024 1025
		I915_WRITE_FW(SPRSCALE(pipe), 0);
	I915_WRITE_FW(SPRSURF(pipe), 0);

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1026 1027
}

1028
static bool
1029 1030
ivb_plane_get_hw_state(struct intel_plane *plane,
		       enum pipe *pipe)
1031 1032 1033 1034 1035
{
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum intel_display_power_domain power_domain;
	bool ret;

1036
	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
1037 1038 1039
	if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
		return false;

1040 1041 1042
	ret =  I915_READ(SPRCTL(plane->pipe)) & SPRITE_ENABLE;

	*pipe = plane->pipe;
1043 1044 1045 1046 1047 1048

	intel_display_power_put(dev_priv, power_domain);

	return ret;
}

1049 1050 1051 1052 1053 1054 1055 1056
static unsigned int
g4x_sprite_max_stride(struct intel_plane *plane,
		      u32 pixel_format, u64 modifier,
		      unsigned int rotation)
{
	return 16384;
}

1057
static u32 g4x_sprite_ctl(const struct intel_crtc_state *crtc_state,
1058
			  const struct intel_plane_state *plane_state)
1059
{
1060 1061 1062
	struct drm_i915_private *dev_priv =
		to_i915(plane_state->base.plane->dev);
	const struct drm_framebuffer *fb = plane_state->base.fb;
1063
	unsigned int rotation = plane_state->base.rotation;
1064
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
1065 1066 1067
	u32 dvscntr;

	dvscntr = DVS_ENABLE | DVS_GAMMA_ENABLE;
1068

1069 1070
	if (IS_GEN6(dev_priv))
		dvscntr |= DVS_TRICKLE_FEED_DISABLE;
1071

V
Ville Syrjälä 已提交
1072
	switch (fb->format->format) {
1073
	case DRM_FORMAT_XBGR8888:
1074
		dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
1075 1076
		break;
	case DRM_FORMAT_XRGB8888:
1077
		dvscntr |= DVS_FORMAT_RGBX888;
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
		break;
	case DRM_FORMAT_YUYV:
		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
		break;
	case DRM_FORMAT_YVYU:
		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
		break;
	case DRM_FORMAT_UYVY:
		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
		break;
	case DRM_FORMAT_VYUY:
		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
		break;
	default:
1092 1093
		MISSING_CASE(fb->format->format);
		return 0;
1094 1095
	}

1096 1097 1098
	if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
		dvscntr |= DVS_YUV_FORMAT_BT709;

1099 1100 1101
	if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
		dvscntr |= DVS_YUV_RANGE_CORRECTION_DISABLE;

V
Ville Syrjälä 已提交
1102
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
1103 1104
		dvscntr |= DVS_TILED;

1105
	if (rotation & DRM_MODE_ROTATE_180)
1106 1107
		dvscntr |= DVS_ROTATE_180;

1108 1109 1110 1111 1112
	if (key->flags & I915_SET_COLORKEY_DESTINATION)
		dvscntr |= DVS_DEST_KEY;
	else if (key->flags & I915_SET_COLORKEY_SOURCE)
		dvscntr |= DVS_SOURCE_KEY;

1113 1114 1115 1116
	return dvscntr;
}

static void
1117
g4x_update_plane(struct intel_plane *plane,
1118 1119 1120
		 const struct intel_crtc_state *crtc_state,
		 const struct intel_plane_state *plane_state)
{
1121 1122 1123
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	const struct drm_framebuffer *fb = plane_state->base.fb;
	enum pipe pipe = plane->pipe;
1124
	u32 dvscntr = plane_state->ctl, dvsscale = 0;
1125
	u32 dvssurf_offset = plane_state->color_plane[0].offset;
1126
	u32 linear_offset;
1127 1128 1129 1130 1131
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
	int crtc_x = plane_state->base.dst.x1;
	int crtc_y = plane_state->base.dst.y1;
	uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
	uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
1132 1133
	uint32_t x = plane_state->color_plane[0].x;
	uint32_t y = plane_state->color_plane[0].y;
1134 1135 1136 1137
	uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
	uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
	unsigned long irqflags;

1138 1139 1140 1141 1142 1143
	/* Sizes are 0 based */
	src_w--;
	src_h--;
	crtc_w--;
	crtc_h--;

1144
	if (crtc_w != src_w || crtc_h != src_h)
1145 1146
		dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;

1147
	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
1148

1149 1150
	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);

1151
	if (key->flags) {
1152 1153 1154
		I915_WRITE_FW(DVSKEYVAL(pipe), key->min_value);
		I915_WRITE_FW(DVSKEYMAX(pipe), key->max_value);
		I915_WRITE_FW(DVSKEYMSK(pipe), key->channel_mask);
1155 1156
	}

1157
	I915_WRITE_FW(DVSSTRIDE(pipe), plane_state->color_plane[0].stride);
1158
	I915_WRITE_FW(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
1159

V
Ville Syrjälä 已提交
1160
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
1161
		I915_WRITE_FW(DVSTILEOFF(pipe), (y << 16) | x);
1162
	else
1163 1164 1165 1166 1167 1168 1169 1170 1171
		I915_WRITE_FW(DVSLINOFF(pipe), linear_offset);

	I915_WRITE_FW(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
	I915_WRITE_FW(DVSSCALE(pipe), dvsscale);
	I915_WRITE_FW(DVSCNTR(pipe), dvscntr);
	I915_WRITE_FW(DVSSURF(pipe),
		      intel_plane_ggtt_offset(plane_state) + dvssurf_offset);

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1172 1173 1174
}

static void
1175
g4x_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
1176
{
1177 1178
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum pipe pipe = plane->pipe;
1179
	unsigned long irqflags;
1180

1181 1182 1183
	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);

	I915_WRITE_FW(DVSCNTR(pipe), 0);
1184
	/* Disable the scaler */
1185 1186
	I915_WRITE_FW(DVSSCALE(pipe), 0);
	I915_WRITE_FW(DVSSURF(pipe), 0);
1187

1188
	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1189 1190
}

1191
static bool
1192 1193
g4x_plane_get_hw_state(struct intel_plane *plane,
		       enum pipe *pipe)
1194 1195 1196 1197 1198
{
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum intel_display_power_domain power_domain;
	bool ret;

1199
	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
1200 1201 1202
	if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
		return false;

1203 1204 1205
	ret = I915_READ(DVSCNTR(plane->pipe)) & DVS_ENABLE;

	*pipe = plane->pipe;
1206 1207 1208 1209 1210 1211

	intel_display_power_put(dev_priv, power_domain);

	return ret;
}

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
static bool intel_fb_scalable(const struct drm_framebuffer *fb)
{
	if (!fb)
		return false;

	switch (fb->format->format) {
	case DRM_FORMAT_C8:
		return false;
	default:
		return true;
	}
}

1225
static int
1226 1227
g4x_sprite_check_scaling(struct intel_crtc_state *crtc_state,
			 struct intel_plane_state *plane_state)
1228
{
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
	const struct drm_framebuffer *fb = plane_state->base.fb;
	const struct drm_rect *src = &plane_state->base.src;
	const struct drm_rect *dst = &plane_state->base.dst;
	int src_x, src_y, src_w, src_h, crtc_w, crtc_h;
	const struct drm_display_mode *adjusted_mode =
		&crtc_state->base.adjusted_mode;
	unsigned int cpp = fb->format->cpp[0];
	unsigned int width_bytes;
	int min_width, min_height;

	crtc_w = drm_rect_width(dst);
	crtc_h = drm_rect_height(dst);

	src_x = src->x1 >> 16;
	src_y = src->y1 >> 16;
	src_w = drm_rect_width(src) >> 16;
	src_h = drm_rect_height(src) >> 16;

	if (src_w == crtc_w && src_h == crtc_h)
1248
		return 0;
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259

	min_width = 3;

	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
		if (src_h & 1) {
			DRM_DEBUG_KMS("Source height must be even with interlaced modes\n");
			return -EINVAL;
		}
		min_height = 6;
	} else {
		min_height = 3;
1260
	}
1261

1262 1263 1264 1265 1266 1267
	width_bytes = ((src_x * cpp) & 63) + src_w * cpp;

	if (src_w < min_width || src_h < min_height ||
	    src_w > 2048 || src_h > 2048) {
		DRM_DEBUG_KMS("Source dimensions (%dx%d) exceed hardware limits (%dx%d - %dx%d)\n",
			      src_w, src_h, min_width, min_height, 2048, 2048);
1268
		return -EINVAL;
1269
	}
1270

1271 1272 1273
	if (width_bytes > 4096) {
		DRM_DEBUG_KMS("Fetch width (%d) exceeds hardware max with scaling (%u)\n",
			      width_bytes, 4096);
1274
		return -EINVAL;
1275
	}
1276

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
	if (width_bytes > 4096 || fb->pitches[0] > 4096) {
		DRM_DEBUG_KMS("Stride (%u) exceeds hardware max with scaling (%u)\n",
			      fb->pitches[0], 4096);
		return -EINVAL;
	}

	return 0;
}

static int
g4x_sprite_check(struct intel_crtc_state *crtc_state,
		 struct intel_plane_state *plane_state)
{
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1292 1293
	int min_scale = DRM_PLANE_HELPER_NO_SCALING;
	int max_scale = DRM_PLANE_HELPER_NO_SCALING;
1294 1295
	int ret;

1296 1297 1298 1299 1300 1301 1302 1303
	if (intel_fb_scalable(plane_state->base.fb)) {
		if (INTEL_GEN(dev_priv) < 7) {
			min_scale = 1;
			max_scale = 16 << 16;
		} else if (IS_IVYBRIDGE(dev_priv)) {
			min_scale = 1;
			max_scale = 2 << 16;
		}
1304 1305
	}

1306
	ret = drm_atomic_helper_check_plane_state(&plane_state->base,
1307 1308 1309 1310 1311
						  &crtc_state->base,
						  min_scale, max_scale,
						  true, true);
	if (ret)
		return ret;
1312

1313 1314
	if (!plane_state->base.visible)
		return 0;
1315

1316 1317 1318
	ret = intel_plane_check_src_coordinates(plane_state);
	if (ret)
		return ret;
1319

1320 1321 1322
	ret = g4x_sprite_check_scaling(crtc_state, plane_state);
	if (ret)
		return ret;
1323

1324 1325 1326
	ret = i9xx_check_plane_surface(plane_state);
	if (ret)
		return ret;
1327

1328 1329 1330 1331
	if (INTEL_GEN(dev_priv) >= 7)
		plane_state->ctl = ivb_sprite_ctl(crtc_state, plane_state);
	else
		plane_state->ctl = g4x_sprite_ctl(crtc_state, plane_state);
1332

1333 1334
	return 0;
}
1335

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
int chv_plane_check_rotation(const struct intel_plane_state *plane_state)
{
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	unsigned int rotation = plane_state->base.rotation;

	/* CHV ignores the mirror bit when the rotate bit is set :( */
	if (IS_CHERRYVIEW(dev_priv) &&
	    rotation & DRM_MODE_ROTATE_180 &&
	    rotation & DRM_MODE_REFLECT_X) {
		DRM_DEBUG_KMS("Cannot rotate and reflect at the same time\n");
		return -EINVAL;
	}

	return 0;
}

1353 1354 1355 1356 1357 1358
static int
vlv_sprite_check(struct intel_crtc_state *crtc_state,
		 struct intel_plane_state *plane_state)
{
	int ret;

1359 1360 1361 1362
	ret = chv_plane_check_rotation(plane_state);
	if (ret)
		return ret;

1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
	ret = drm_atomic_helper_check_plane_state(&plane_state->base,
						  &crtc_state->base,
						  DRM_PLANE_HELPER_NO_SCALING,
						  DRM_PLANE_HELPER_NO_SCALING,
						  true, true);
	if (ret)
		return ret;

	if (!plane_state->base.visible)
		return 0;

	ret = intel_plane_check_src_coordinates(plane_state);
	if (ret)
		return ret;

	ret = i9xx_check_plane_surface(plane_state);
	if (ret)
		return ret;

	plane_state->ctl = vlv_sprite_ctl(crtc_state, plane_state);
1383

1384 1385 1386
	return 0;
}

1387 1388 1389
static int skl_plane_check_fb(const struct intel_crtc_state *crtc_state,
			      const struct intel_plane_state *plane_state)
{
1390 1391
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1392 1393 1394 1395 1396 1397 1398 1399
	const struct drm_framebuffer *fb = plane_state->base.fb;
	unsigned int rotation = plane_state->base.rotation;
	struct drm_format_name_buf format_name;

	if (!fb)
		return 0;

	if (rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180) &&
1400
	    is_ccs_modifier(fb->modifier)) {
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
		DRM_DEBUG_KMS("RC support only with 0/180 degree rotation (%x)\n",
			      rotation);
		return -EINVAL;
	}

	if (rotation & DRM_MODE_REFLECT_X &&
	    fb->modifier == DRM_FORMAT_MOD_LINEAR) {
		DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
		return -EINVAL;
	}

	if (drm_rotation_90_or_270(rotation)) {
		if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
		    fb->modifier != I915_FORMAT_MOD_Yf_TILED) {
			DRM_DEBUG_KMS("Y/Yf tiling required for 90/270!\n");
			return -EINVAL;
		}

		/*
1420 1421 1422 1423
		 * 90/270 is not allowed with RGB64 16:16:16:16 and
		 * Indexed 8-bit. RGB 16-bit 5:6:5 is allowed gen11 onwards.
		 * TBD: Add RGB64 case once its added in supported format
		 * list.
1424 1425 1426
		 */
		switch (fb->format->format) {
		case DRM_FORMAT_RGB565:
1427 1428 1429 1430
			if (INTEL_GEN(dev_priv) >= 11)
				break;
			/* fall through */
		case DRM_FORMAT_C8:
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
			DRM_DEBUG_KMS("Unsupported pixel format %s for 90/270!\n",
				      drm_get_format_name(fb->format->format,
							  &format_name));
			return -EINVAL;
		default:
			break;
		}
	}

	/* Y-tiling is not supported in IF-ID Interlace mode */
	if (crtc_state->base.enable &&
	    crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE &&
	    (fb->modifier == I915_FORMAT_MOD_Y_TILED ||
	     fb->modifier == I915_FORMAT_MOD_Yf_TILED ||
	     fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
	     fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS)) {
		DRM_DEBUG_KMS("Y/Yf tiling not supported in IF-ID mode\n");
		return -EINVAL;
	}

	return 0;
}

1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
static int skl_plane_check_dst_coordinates(const struct intel_crtc_state *crtc_state,
					   const struct intel_plane_state *plane_state)
{
	struct drm_i915_private *dev_priv =
		to_i915(plane_state->base.plane->dev);
	int crtc_x = plane_state->base.dst.x1;
	int crtc_w = drm_rect_width(&plane_state->base.dst);
	int pipe_src_w = crtc_state->pipe_src_w;

	/*
	 * Display WA #1175: cnl,glk
	 * Planes other than the cursor may cause FIFO underflow and display
	 * corruption if starting less than 4 pixels from the right edge of
	 * the screen.
	 * Besides the above WA fix the similar problem, where planes other
	 * than the cursor ending less than 4 pixels from the left edge of the
	 * screen may cause FIFO underflow and display corruption.
	 */
	if ((IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
	    (crtc_x + crtc_w < 4 || crtc_x > pipe_src_w - 4)) {
		DRM_DEBUG_KMS("requested plane X %s position %d invalid (valid range %d-%d)\n",
			      crtc_x + crtc_w < 4 ? "end" : "start",
			      crtc_x + crtc_w < 4 ? crtc_x + crtc_w : crtc_x,
			      4, pipe_src_w - 4);
		return -ERANGE;
	}

	return 0;
}

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
static int skl_plane_check_nv12_rotation(const struct intel_plane_state *plane_state)
{
	const struct drm_framebuffer *fb = plane_state->base.fb;
	unsigned int rotation = plane_state->base.rotation;
	int src_w = drm_rect_width(&plane_state->base.src) >> 16;

	/* Display WA #1106 */
	if (fb->format->format == DRM_FORMAT_NV12 && src_w & 3 &&
	    (rotation == DRM_MODE_ROTATE_270 ||
	     rotation == (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90))) {
		DRM_DEBUG_KMS("src width must be multiple of 4 for rotated NV12\n");
		return -EINVAL;
	}

	return 0;
}

1501 1502
static int skl_plane_check(struct intel_crtc_state *crtc_state,
			   struct intel_plane_state *plane_state)
1503 1504 1505
{
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1506 1507 1508
	const struct drm_framebuffer *fb = plane_state->base.fb;
	int min_scale = DRM_PLANE_HELPER_NO_SCALING;
	int max_scale = DRM_PLANE_HELPER_NO_SCALING;
1509 1510
	int ret;

1511 1512 1513 1514
	ret = skl_plane_check_fb(crtc_state, plane_state);
	if (ret)
		return ret;

1515
	/* use scaler when colorkey is not required */
1516
	if (!plane_state->ckey.flags && intel_fb_scalable(fb)) {
1517
		min_scale = 1;
1518
		max_scale = skl_max_scale(crtc_state, fb->format->format);
1519 1520
	}

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
	ret = drm_atomic_helper_check_plane_state(&plane_state->base,
						  &crtc_state->base,
						  min_scale, max_scale,
						  true, true);
	if (ret)
		return ret;

	if (!plane_state->base.visible)
		return 0;

1531 1532 1533 1534
	ret = skl_plane_check_dst_coordinates(crtc_state, plane_state);
	if (ret)
		return ret;

1535 1536 1537 1538
	ret = intel_plane_check_src_coordinates(plane_state);
	if (ret)
		return ret;

1539 1540 1541 1542
	ret = skl_plane_check_nv12_rotation(plane_state);
	if (ret)
		return ret;

1543
	ret = skl_check_plane_surface(plane_state);
1544 1545 1546
	if (ret)
		return ret;

1547 1548 1549 1550
	/* HW only has 8 bits pixel precision, disable plane if invisible */
	if (!(plane_state->base.alpha >> 8))
		plane_state->base.visible = false;

1551 1552
	plane_state->ctl = skl_plane_ctl(crtc_state, plane_state);

1553
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
1554 1555
		plane_state->color_ctl = glk_plane_color_ctl(crtc_state,
							     plane_state);
1556

1557 1558 1559
	return 0;
}

1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
static bool has_dst_key_in_primary_plane(struct drm_i915_private *dev_priv)
{
	return INTEL_GEN(dev_priv) >= 9;
}

static void intel_plane_set_ckey(struct intel_plane_state *plane_state,
				 const struct drm_intel_sprite_colorkey *set)
{
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	struct drm_intel_sprite_colorkey *key = &plane_state->ckey;

	*key = *set;

	/*
	 * We want src key enabled on the
	 * sprite and not on the primary.
	 */
	if (plane->id == PLANE_PRIMARY &&
	    set->flags & I915_SET_COLORKEY_SOURCE)
		key->flags = 0;

	/*
	 * On SKL+ we want dst key enabled on
	 * the primary and not on the sprite.
	 */
	if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_PRIMARY &&
	    set->flags & I915_SET_COLORKEY_DESTINATION)
		key->flags = 0;
}

1591 1592
int intel_sprite_set_colorkey_ioctl(struct drm_device *dev, void *data,
				    struct drm_file *file_priv)
1593
{
1594
	struct drm_i915_private *dev_priv = to_i915(dev);
1595 1596
	struct drm_intel_sprite_colorkey *set = data;
	struct drm_plane *plane;
1597 1598 1599
	struct drm_plane_state *plane_state;
	struct drm_atomic_state *state;
	struct drm_modeset_acquire_ctx ctx;
1600 1601
	int ret = 0;

1602 1603 1604
	/* ignore the pointless "none" flag */
	set->flags &= ~I915_SET_COLORKEY_NONE;

1605 1606 1607
	if (set->flags & ~(I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
		return -EINVAL;

1608 1609 1610 1611
	/* Make sure we don't try to enable both src & dest simultaneously */
	if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
		return -EINVAL;

1612
	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1613 1614 1615
	    set->flags & I915_SET_COLORKEY_DESTINATION)
		return -EINVAL;

1616
	plane = drm_plane_find(dev, file_priv, set->plane_id);
1617 1618
	if (!plane || plane->type != DRM_PLANE_TYPE_OVERLAY)
		return -ENOENT;
1619

1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
	/*
	 * SKL+ only plane 2 can do destination keying against plane 1.
	 * Also multiple planes can't do destination keying on the same
	 * pipe simultaneously.
	 */
	if (INTEL_GEN(dev_priv) >= 9 &&
	    to_intel_plane(plane)->id >= PLANE_SPRITE1 &&
	    set->flags & I915_SET_COLORKEY_DESTINATION)
		return -EINVAL;

1630
	drm_modeset_acquire_init(&ctx, 0);
1631

1632 1633 1634 1635
	state = drm_atomic_state_alloc(plane->dev);
	if (!state) {
		ret = -ENOMEM;
		goto out;
1636
	}
1637 1638 1639 1640 1641
	state->acquire_ctx = &ctx;

	while (1) {
		plane_state = drm_atomic_get_plane_state(state, plane);
		ret = PTR_ERR_OR_ZERO(plane_state);
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
		if (!ret)
			intel_plane_set_ckey(to_intel_plane_state(plane_state), set);

		/*
		 * On some platforms we have to configure
		 * the dst colorkey on the primary plane.
		 */
		if (!ret && has_dst_key_in_primary_plane(dev_priv)) {
			struct intel_crtc *crtc =
				intel_get_crtc_for_pipe(dev_priv,
							to_intel_plane(plane)->pipe);

			plane_state = drm_atomic_get_plane_state(state,
								 crtc->base.primary);
			ret = PTR_ERR_OR_ZERO(plane_state);
			if (!ret)
				intel_plane_set_ckey(to_intel_plane_state(plane_state), set);
1659
		}
1660

1661 1662 1663
		if (!ret)
			ret = drm_atomic_commit(state);

1664 1665
		if (ret != -EDEADLK)
			break;
1666

1667 1668 1669
		drm_atomic_state_clear(state);
		drm_modeset_backoff(&ctx);
	}
1670

1671
	drm_atomic_state_put(state);
1672 1673 1674 1675
out:
	drm_modeset_drop_locks(&ctx);
	drm_modeset_acquire_fini(&ctx);
	return ret;
1676 1677
}

1678
static const uint32_t g4x_plane_formats[] = {
1679 1680 1681 1682 1683 1684 1685
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_VYUY,
};

1686 1687 1688 1689 1690 1691
static const uint64_t i9xx_plane_format_modifiers[] = {
	I915_FORMAT_MOD_X_TILED,
	DRM_FORMAT_MOD_LINEAR,
	DRM_FORMAT_MOD_INVALID
};

1692
static const uint32_t snb_plane_formats[] = {
1693 1694 1695 1696 1697 1698 1699 1700
	DRM_FORMAT_XBGR8888,
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_VYUY,
};

1701
static const uint32_t vlv_plane_formats[] = {
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
	DRM_FORMAT_RGB565,
	DRM_FORMAT_ABGR8888,
	DRM_FORMAT_ARGB8888,
	DRM_FORMAT_XBGR8888,
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_XBGR2101010,
	DRM_FORMAT_ABGR2101010,
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_VYUY,
};

1715 1716
static const uint32_t skl_plane_formats[] = {
	DRM_FORMAT_C8,
1717 1718
	DRM_FORMAT_RGB565,
	DRM_FORMAT_XRGB8888,
1719 1720 1721 1722 1723
	DRM_FORMAT_XBGR8888,
	DRM_FORMAT_ARGB8888,
	DRM_FORMAT_ABGR8888,
	DRM_FORMAT_XRGB2101010,
	DRM_FORMAT_XBGR2101010,
1724 1725 1726 1727 1728 1729
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_VYUY,
};

1730 1731
static const uint32_t skl_planar_formats[] = {
	DRM_FORMAT_C8,
1732 1733
	DRM_FORMAT_RGB565,
	DRM_FORMAT_XRGB8888,
1734 1735 1736 1737 1738
	DRM_FORMAT_XBGR8888,
	DRM_FORMAT_ARGB8888,
	DRM_FORMAT_ABGR8888,
	DRM_FORMAT_XRGB2101010,
	DRM_FORMAT_XBGR2101010,
1739 1740 1741 1742 1743 1744 1745
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_VYUY,
	DRM_FORMAT_NV12,
};

1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
static const uint64_t skl_plane_format_modifiers_noccs[] = {
	I915_FORMAT_MOD_Yf_TILED,
	I915_FORMAT_MOD_Y_TILED,
	I915_FORMAT_MOD_X_TILED,
	DRM_FORMAT_MOD_LINEAR,
	DRM_FORMAT_MOD_INVALID
};

static const uint64_t skl_plane_format_modifiers_ccs[] = {
	I915_FORMAT_MOD_Yf_TILED_CCS,
	I915_FORMAT_MOD_Y_TILED_CCS,
1757 1758
	I915_FORMAT_MOD_Yf_TILED,
	I915_FORMAT_MOD_Y_TILED,
1759 1760 1761 1762 1763
	I915_FORMAT_MOD_X_TILED,
	DRM_FORMAT_MOD_LINEAR,
	DRM_FORMAT_MOD_INVALID
};

1764 1765
static bool g4x_sprite_format_mod_supported(struct drm_plane *_plane,
					    u32 format, u64 modifier)
1766
{
1767 1768 1769 1770 1771 1772 1773 1774
	switch (modifier) {
	case DRM_FORMAT_MOD_LINEAR:
	case I915_FORMAT_MOD_X_TILED:
		break;
	default:
		return false;
	}

1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
	switch (format) {
	case DRM_FORMAT_XRGB8888:
	case DRM_FORMAT_YUYV:
	case DRM_FORMAT_YVYU:
	case DRM_FORMAT_UYVY:
	case DRM_FORMAT_VYUY:
		if (modifier == DRM_FORMAT_MOD_LINEAR ||
		    modifier == I915_FORMAT_MOD_X_TILED)
			return true;
		/* fall through */
	default:
		return false;
	}
}

1790 1791
static bool snb_sprite_format_mod_supported(struct drm_plane *_plane,
					    u32 format, u64 modifier)
1792
{
1793 1794 1795 1796 1797 1798 1799 1800
	switch (modifier) {
	case DRM_FORMAT_MOD_LINEAR:
	case I915_FORMAT_MOD_X_TILED:
		break;
	default:
		return false;
	}

1801
	switch (format) {
1802 1803
	case DRM_FORMAT_XRGB8888:
	case DRM_FORMAT_XBGR8888:
1804 1805 1806 1807
	case DRM_FORMAT_YUYV:
	case DRM_FORMAT_YVYU:
	case DRM_FORMAT_UYVY:
	case DRM_FORMAT_VYUY:
1808 1809 1810 1811 1812 1813 1814 1815 1816
		if (modifier == DRM_FORMAT_MOD_LINEAR ||
		    modifier == I915_FORMAT_MOD_X_TILED)
			return true;
		/* fall through */
	default:
		return false;
	}
}

1817 1818
static bool vlv_sprite_format_mod_supported(struct drm_plane *_plane,
					    u32 format, u64 modifier)
1819
{
1820 1821 1822 1823 1824 1825 1826 1827
	switch (modifier) {
	case DRM_FORMAT_MOD_LINEAR:
	case I915_FORMAT_MOD_X_TILED:
		break;
	default:
		return false;
	}

1828
	switch (format) {
1829
	case DRM_FORMAT_RGB565:
1830
	case DRM_FORMAT_ABGR8888:
1831
	case DRM_FORMAT_ARGB8888:
1832 1833
	case DRM_FORMAT_XBGR8888:
	case DRM_FORMAT_XRGB8888:
1834 1835
	case DRM_FORMAT_XBGR2101010:
	case DRM_FORMAT_ABGR2101010:
1836 1837 1838 1839
	case DRM_FORMAT_YUYV:
	case DRM_FORMAT_YVYU:
	case DRM_FORMAT_UYVY:
	case DRM_FORMAT_VYUY:
1840 1841 1842 1843 1844 1845 1846 1847 1848
		if (modifier == DRM_FORMAT_MOD_LINEAR ||
		    modifier == I915_FORMAT_MOD_X_TILED)
			return true;
		/* fall through */
	default:
		return false;
	}
}

1849 1850
static bool skl_plane_format_mod_supported(struct drm_plane *_plane,
					   u32 format, u64 modifier)
1851
{
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
	struct intel_plane *plane = to_intel_plane(_plane);

	switch (modifier) {
	case DRM_FORMAT_MOD_LINEAR:
	case I915_FORMAT_MOD_X_TILED:
	case I915_FORMAT_MOD_Y_TILED:
	case I915_FORMAT_MOD_Yf_TILED:
		break;
	case I915_FORMAT_MOD_Y_TILED_CCS:
	case I915_FORMAT_MOD_Yf_TILED_CCS:
		if (!plane->has_ccs)
			return false;
		break;
	default:
		return false;
	}

1869 1870 1871 1872 1873
	switch (format) {
	case DRM_FORMAT_XRGB8888:
	case DRM_FORMAT_XBGR8888:
	case DRM_FORMAT_ARGB8888:
	case DRM_FORMAT_ABGR8888:
1874
		if (is_ccs_modifier(modifier))
1875 1876
			return true;
		/* fall through */
1877 1878 1879 1880 1881 1882 1883
	case DRM_FORMAT_RGB565:
	case DRM_FORMAT_XRGB2101010:
	case DRM_FORMAT_XBGR2101010:
	case DRM_FORMAT_YUYV:
	case DRM_FORMAT_YVYU:
	case DRM_FORMAT_UYVY:
	case DRM_FORMAT_VYUY:
1884
	case DRM_FORMAT_NV12:
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
		if (modifier == I915_FORMAT_MOD_Yf_TILED)
			return true;
		/* fall through */
	case DRM_FORMAT_C8:
		if (modifier == DRM_FORMAT_MOD_LINEAR ||
		    modifier == I915_FORMAT_MOD_X_TILED ||
		    modifier == I915_FORMAT_MOD_Y_TILED)
			return true;
		/* fall through */
	default:
		return false;
	}
}

1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
static const struct drm_plane_funcs g4x_sprite_funcs = {
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
	.destroy = intel_plane_destroy,
	.atomic_get_property = intel_plane_atomic_get_property,
	.atomic_set_property = intel_plane_atomic_set_property,
	.atomic_duplicate_state = intel_plane_duplicate_state,
	.atomic_destroy_state = intel_plane_destroy_state,
	.format_mod_supported = g4x_sprite_format_mod_supported,
};
1909

1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
static const struct drm_plane_funcs snb_sprite_funcs = {
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
	.destroy = intel_plane_destroy,
	.atomic_get_property = intel_plane_atomic_get_property,
	.atomic_set_property = intel_plane_atomic_set_property,
	.atomic_duplicate_state = intel_plane_duplicate_state,
	.atomic_destroy_state = intel_plane_destroy_state,
	.format_mod_supported = snb_sprite_format_mod_supported,
};
1920

1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
static const struct drm_plane_funcs vlv_sprite_funcs = {
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
	.destroy = intel_plane_destroy,
	.atomic_get_property = intel_plane_atomic_get_property,
	.atomic_set_property = intel_plane_atomic_set_property,
	.atomic_duplicate_state = intel_plane_duplicate_state,
	.atomic_destroy_state = intel_plane_destroy_state,
	.format_mod_supported = vlv_sprite_format_mod_supported,
};
1931

1932
static const struct drm_plane_funcs skl_plane_funcs = {
1933 1934 1935 1936 1937 1938 1939
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
	.destroy = intel_plane_destroy,
	.atomic_get_property = intel_plane_atomic_get_property,
	.atomic_set_property = intel_plane_atomic_set_property,
	.atomic_duplicate_state = intel_plane_duplicate_state,
	.atomic_destroy_state = intel_plane_destroy_state,
1940
	.format_mod_supported = skl_plane_format_mod_supported,
1941 1942
};

1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
static bool skl_plane_has_fbc(struct drm_i915_private *dev_priv,
			      enum pipe pipe, enum plane_id plane_id)
{
	if (!HAS_FBC(dev_priv))
		return false;

	return pipe == PIPE_A && plane_id == PLANE_PRIMARY;
}

static bool skl_plane_has_planar(struct drm_i915_private *dev_priv,
				 enum pipe pipe, enum plane_id plane_id)
{
	if (INTEL_GEN(dev_priv) >= 11)
1956
		return plane_id <= PLANE_SPRITE3;
1957

1958
	/* Display WA #0870: skl, bxt */
1959 1960 1961
	if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
		return false;

1962
	if (IS_GEN9(dev_priv) && !IS_GEMINILAKE(dev_priv) && pipe == PIPE_C)
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
		return false;

	if (plane_id != PLANE_PRIMARY && plane_id != PLANE_SPRITE0)
		return false;

	return true;
}

static bool skl_plane_has_ccs(struct drm_i915_private *dev_priv,
			      enum pipe pipe, enum plane_id plane_id)
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
{
	if (plane_id == PLANE_CURSOR)
		return false;

	if (INTEL_GEN(dev_priv) >= 10)
		return true;

	if (IS_GEMINILAKE(dev_priv))
		return pipe != PIPE_C;

	return pipe != PIPE_C &&
		(plane_id == PLANE_PRIMARY ||
		 plane_id == PLANE_SPRITE0);
}

1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
struct intel_plane *intel_plane_alloc(void)
{
	struct intel_plane_state *plane_state;
	struct intel_plane *plane;

	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
	if (!plane)
		return ERR_PTR(-ENOMEM);

	plane_state = intel_create_plane_state(&plane->base);
	if (!plane_state) {
		kfree(plane);
		return ERR_PTR(-ENOMEM);
	}

	plane->base.state = &plane_state->base;

	return plane;
}

void intel_plane_free(struct intel_plane *plane)
{
	struct intel_plane_state *plane_state =
		to_intel_plane_state(plane->base.state);

	kfree(plane_state);
	kfree(plane);
}

2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
struct intel_plane *
skl_universal_plane_create(struct drm_i915_private *dev_priv,
			   enum pipe pipe, enum plane_id plane_id)
{
	struct intel_plane *plane;
	enum drm_plane_type plane_type;
	unsigned int supported_rotations;
	unsigned int possible_crtcs;
	const u64 *modifiers;
	const u32 *formats;
	int num_formats;
	int ret;

	plane = intel_plane_alloc();
	if (IS_ERR(plane))
		return plane;

	plane->pipe = pipe;
	plane->id = plane_id;
	plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane_id);

	plane->has_fbc = skl_plane_has_fbc(dev_priv, pipe, plane_id);
	if (plane->has_fbc) {
		struct intel_fbc *fbc = &dev_priv->fbc;

		fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
	}

	plane->max_stride = skl_plane_max_stride;
	plane->update_plane = skl_update_plane;
	plane->disable_plane = skl_disable_plane;
	plane->get_hw_state = skl_plane_get_hw_state;
	plane->check_plane = skl_plane_check;
2050 2051
	if (icl_is_nv12_y_plane(plane_id))
		plane->update_slave = icl_update_slave;
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117

	if (skl_plane_has_planar(dev_priv, pipe, plane_id)) {
		formats = skl_planar_formats;
		num_formats = ARRAY_SIZE(skl_planar_formats);
	} else {
		formats = skl_plane_formats;
		num_formats = ARRAY_SIZE(skl_plane_formats);
	}

	plane->has_ccs = skl_plane_has_ccs(dev_priv, pipe, plane_id);
	if (plane->has_ccs)
		modifiers = skl_plane_format_modifiers_ccs;
	else
		modifiers = skl_plane_format_modifiers_noccs;

	if (plane_id == PLANE_PRIMARY)
		plane_type = DRM_PLANE_TYPE_PRIMARY;
	else
		plane_type = DRM_PLANE_TYPE_OVERLAY;

	possible_crtcs = BIT(pipe);

	ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
				       possible_crtcs, &skl_plane_funcs,
				       formats, num_formats, modifiers,
				       plane_type,
				       "plane %d%c", plane_id + 1,
				       pipe_name(pipe));
	if (ret)
		goto fail;

	supported_rotations =
		DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
		DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;

	if (INTEL_GEN(dev_priv) >= 10)
		supported_rotations |= DRM_MODE_REFLECT_X;

	drm_plane_create_rotation_property(&plane->base,
					   DRM_MODE_ROTATE_0,
					   supported_rotations);

	drm_plane_create_color_properties(&plane->base,
					  BIT(DRM_COLOR_YCBCR_BT601) |
					  BIT(DRM_COLOR_YCBCR_BT709),
					  BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
					  BIT(DRM_COLOR_YCBCR_FULL_RANGE),
					  DRM_COLOR_YCBCR_BT709,
					  DRM_COLOR_YCBCR_LIMITED_RANGE);

	drm_plane_create_alpha_property(&plane->base);
	drm_plane_create_blend_mode_property(&plane->base,
					     BIT(DRM_MODE_BLEND_PIXEL_NONE) |
					     BIT(DRM_MODE_BLEND_PREMULTI) |
					     BIT(DRM_MODE_BLEND_COVERAGE));

	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);

	return plane;

fail:
	intel_plane_free(plane);

	return ERR_PTR(ret);
}

2118
struct intel_plane *
2119
intel_sprite_plane_create(struct drm_i915_private *dev_priv,
2120
			  enum pipe pipe, int sprite)
2121
{
2122
	struct intel_plane *plane;
2123
	const struct drm_plane_funcs *plane_funcs;
2124
	unsigned long possible_crtcs;
2125
	unsigned int supported_rotations;
2126 2127 2128
	const u64 *modifiers;
	const u32 *formats;
	int num_formats;
2129 2130
	int ret;

2131 2132
	if (INTEL_GEN(dev_priv) >= 9)
		return skl_universal_plane_create(dev_priv, pipe,
2133
						  PLANE_SPRITE0 + sprite);
2134

2135 2136 2137
	plane = intel_plane_alloc();
	if (IS_ERR(plane))
		return plane;
2138

2139
	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
2140 2141 2142 2143 2144 2145 2146 2147
		plane->max_stride = i9xx_plane_max_stride;
		plane->update_plane = vlv_update_plane;
		plane->disable_plane = vlv_disable_plane;
		plane->get_hw_state = vlv_plane_get_hw_state;
		plane->check_plane = vlv_sprite_check;

		formats = vlv_plane_formats;
		num_formats = ARRAY_SIZE(vlv_plane_formats);
2148
		modifiers = i9xx_plane_format_modifiers;
2149 2150

		plane_funcs = &vlv_sprite_funcs;
V
Ville Syrjälä 已提交
2151
	} else if (INTEL_GEN(dev_priv) >= 7) {
2152 2153 2154 2155 2156 2157 2158 2159
		plane->max_stride = g4x_sprite_max_stride;
		plane->update_plane = ivb_update_plane;
		plane->disable_plane = ivb_disable_plane;
		plane->get_hw_state = ivb_plane_get_hw_state;
		plane->check_plane = g4x_sprite_check;

		formats = snb_plane_formats;
		num_formats = ARRAY_SIZE(snb_plane_formats);
2160
		modifiers = i9xx_plane_format_modifiers;
2161 2162

		plane_funcs = &snb_sprite_funcs;
V
Ville Syrjälä 已提交
2163
	} else {
2164 2165 2166 2167 2168
		plane->max_stride = g4x_sprite_max_stride;
		plane->update_plane = g4x_update_plane;
		plane->disable_plane = g4x_disable_plane;
		plane->get_hw_state = g4x_plane_get_hw_state;
		plane->check_plane = g4x_sprite_check;
2169

2170
		modifiers = i9xx_plane_format_modifiers;
V
Ville Syrjälä 已提交
2171
		if (IS_GEN6(dev_priv)) {
2172 2173
			formats = snb_plane_formats;
			num_formats = ARRAY_SIZE(snb_plane_formats);
2174 2175

			plane_funcs = &snb_sprite_funcs;
V
Ville Syrjälä 已提交
2176
		} else {
2177 2178
			formats = g4x_plane_formats;
			num_formats = ARRAY_SIZE(g4x_plane_formats);
2179 2180

			plane_funcs = &g4x_sprite_funcs;
2181
		}
2182 2183
	}

2184
	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
2185
		supported_rotations =
2186 2187
			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
			DRM_MODE_REFLECT_X;
2188 2189
	} else {
		supported_rotations =
2190
			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
2191 2192
	}

2193 2194 2195
	plane->pipe = pipe;
	plane->id = PLANE_SPRITE0 + sprite;
	plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
2196

2197
	possible_crtcs = BIT(pipe);
2198

2199
	ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
2200
				       possible_crtcs, plane_funcs,
2201
				       formats, num_formats, modifiers,
2202
				       DRM_PLANE_TYPE_OVERLAY,
2203
				       "sprite %c", sprite_name(pipe, sprite));
2204 2205
	if (ret)
		goto fail;
2206

2207
	drm_plane_create_rotation_property(&plane->base,
2208
					   DRM_MODE_ROTATE_0,
2209
					   supported_rotations);
2210

2211
	drm_plane_create_color_properties(&plane->base,
2212 2213
					  BIT(DRM_COLOR_YCBCR_BT601) |
					  BIT(DRM_COLOR_YCBCR_BT709),
2214 2215
					  BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
					  BIT(DRM_COLOR_YCBCR_FULL_RANGE),
2216
					  DRM_COLOR_YCBCR_BT709,
2217 2218
					  DRM_COLOR_YCBCR_LIMITED_RANGE);

2219
	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
2220

2221
	return plane;
2222 2223

fail:
2224
	intel_plane_free(plane);
2225

2226
	return ERR_PTR(ret);
2227
}