intel_sprite.c 54.8 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 43
#include "i915_drv.h"

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

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

55 56 57 58 59
/* 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
60
#define VBLANK_EVASION_TIME_US 100
61
#endif
62

63 64
/**
 * intel_pipe_update_start() - start update of a set of display registers
65
 * @new_crtc_state: the new crtc state
66 67 68 69 70 71 72
 *
 * 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
73
 * avoid random delays.
74
 */
75
void intel_pipe_update_start(const struct intel_crtc_state *new_crtc_state)
76
{
77
	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
78
	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79
	const struct drm_display_mode *adjusted_mode = &new_crtc_state->base.adjusted_mode;
80 81
	long timeout = msecs_to_jiffies_timeout(1);
	int scanline, min, max, vblank_start;
82
	wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
83
	bool need_vlv_dsi_wa = (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
84
		intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI);
85
	DEFINE_WAIT(wait);
86
	u32 psr_status;
87

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

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

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

100
	if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
101 102 103 104 105 106 107
		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.
	 */
108 109 110
	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);
111 112

	local_irq_disable();
113

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

118 119 120 121 122 123
	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.
		 */
124
		prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
125 126 127 128 129

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

130
		if (!timeout) {
131 132 133 134 135 136 137 138 139 140 141 142
			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();
	}

143
	finish_wait(wq, &wait);
144

145
	drm_crtc_vblank_put(&crtc->base);
146

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	/*
	 * 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);

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

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

irq_disable:
	local_irq_disable();
174 175
}

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

193
	trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
194

195 196 197 198
	/* 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 ... */
199
	if (new_crtc_state->base.event) {
200 201 202
		WARN_ON(drm_crtc_vblank_get(&crtc->base) != 0);

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

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

209 210
	local_irq_enable();

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

214 215 216 217 218 219 220 221
	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);
222 223 224 225
	}
#ifdef CONFIG_DRM_I915_DEBUG_VBLANK_EVADE
	else if (ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time) >
		 VBLANK_EVASION_TIME_US)
226 227 228 229
		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);
230
#endif
231 232
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
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;
}

255 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 285 286 287
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 &&
	    fb->format->format != DRM_FORMAT_NV12 &&
	    (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;
	}

	return 0;
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
unsigned int
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);
}

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
static void
skl_program_scaler(struct drm_i915_private *dev_priv,
		   struct intel_plane *plane,
		   const struct intel_crtc_state *crtc_state,
		   const struct intel_plane_state *plane_state)
{
	enum plane_id plane_id = plane->id;
	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;

	/* Sizes are 0 based */
	crtc_w--;
	crtc_h--;

	/* TODO: handle sub-pixel coordinates */
	if (plane_state->base.fb->format->format == DRM_FORMAT_NV12) {
		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),
		      PS_SCALER_EN | PS_PLANE_SEL(plane_id) | scaler->mode);
	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);
	I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id),
		      ((crtc_w + 1) << 16)|(crtc_h + 1));
}

356
void
357
skl_update_plane(struct intel_plane *plane,
358 359
		 const struct intel_crtc_state *crtc_state,
		 const struct intel_plane_state *plane_state)
360
{
361 362 363
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum plane_id plane_id = plane->id;
	enum pipe pipe = plane->pipe;
364
	u32 plane_ctl = plane_state->ctl;
365
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
366
	u32 surf_addr = plane_state->color_plane[0].offset;
367 368
	u32 stride = skl_plane_stride(plane_state, 0);
	u32 aux_stride = skl_plane_stride(plane_state, 1);
369 370
	int crtc_x = plane_state->base.dst.x1;
	int crtc_y = plane_state->base.dst.y1;
371 372
	uint32_t x = plane_state->color_plane[0].x;
	uint32_t y = plane_state->color_plane[0].y;
373 374
	uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
	uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
375
	unsigned long irqflags;
376

377 378 379 380
	/* Sizes are 0 based */
	src_w--;
	src_h--;

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

383
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
384
		I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
385
			      plane_state->color_ctl);
386

387
	if (key->flags) {
388 389 390
		I915_WRITE_FW(PLANE_KEYVAL(pipe, plane_id), key->min_value);
		I915_WRITE_FW(PLANE_KEYMAX(pipe, plane_id), key->max_value);
		I915_WRITE_FW(PLANE_KEYMSK(pipe, plane_id), key->channel_mask);
391 392
	}

393 394 395
	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);
396
	I915_WRITE_FW(PLANE_AUX_DIST(pipe, plane_id),
397
		      (plane_state->color_plane[1].offset - surf_addr) | aux_stride);
398
	I915_WRITE_FW(PLANE_AUX_OFFSET(pipe, plane_id),
399 400
		      (plane_state->color_plane[1].y << 16) |
		      plane_state->color_plane[1].x);
401 402

	/* program plane scaler */
403
	if (plane_state->scaler_id >= 0) {
404
		skl_program_scaler(dev_priv, plane, crtc_state, plane_state);
405

406
		I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
407
	} else {
408
		I915_WRITE_FW(PLANE_POS(pipe, plane_id), (crtc_y << 16) | crtc_x);
409 410
	}

411 412 413 414 415 416
	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);
	POSTING_READ_FW(PLANE_SURF(pipe, plane_id));

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
417 418
}

419
void
420
skl_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
421
{
422 423 424
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum plane_id plane_id = plane->id;
	enum pipe pipe = plane->pipe;
425 426 427
	unsigned long irqflags;

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

429
	I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
430

431 432 433 434
	I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
	POSTING_READ_FW(PLANE_SURF(pipe, plane_id));

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
435 436
}

437
bool
438 439
skl_plane_get_hw_state(struct intel_plane *plane,
		       enum pipe *pipe)
440 441 442 443 444 445
{
	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;

446
	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
447 448 449
	if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
		return false;

450 451 452
	ret = I915_READ(PLANE_CTL(plane->pipe, plane_id)) & PLANE_CTL_ENABLE;

	*pipe = plane->pipe;
453 454 455 456 457 458

	intel_display_power_put(dev_priv, power_domain);

	return ret;
}

459
static void
460
chv_update_csc(const struct intel_plane_state *plane_state)
461
{
462
	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
463
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
464
	const struct drm_framebuffer *fb = plane_state->base.fb;
465
	enum plane_id plane_id = plane->id;
466
	/*
467 468 469
	 * |r|   | c0 c1 c2 |   |cr|
	 * |g| = | c3 c4 c5 | x |y |
	 * |b|   | c6 c7 c8 |   |cb|
470
	 *
471
	 * Coefficients are s3.12.
472
	 *
473 474
	 * Cb and Cr apparently come in as signed already, and
	 * we always get full range data in on account of CLRC0/1.
475
	 */
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
	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];
491 492

	/* Seems RGB data bypasses the CSC always */
493
	if (!fb->format->is_yuv)
494 495
		return;

496
	I915_WRITE_FW(SPCSCYGOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
497 498 499
	I915_WRITE_FW(SPCSCCBOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
	I915_WRITE_FW(SPCSCCROFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));

500 501 502 503 504
	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]));
505

506 507 508
	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));
509 510 511 512

	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));
513 514
}

515 516 517 518 519 520 521 522 523 524 525 526 527
#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;

528
	if (fb->format->is_yuv &&
529
	    plane_state->base.color_range == DRM_COLOR_YCBCR_LIMITED_RANGE) {
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
		/*
		 * 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));
}

557 558
static u32 vlv_sprite_ctl(const struct intel_crtc_state *crtc_state,
			  const struct intel_plane_state *plane_state)
559
{
560
	const struct drm_framebuffer *fb = plane_state->base.fb;
561
	unsigned int rotation = plane_state->base.rotation;
562
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
563
	u32 sprctl;
564

565
	sprctl = SP_ENABLE | SP_GAMMA_ENABLE;
566

V
Ville Syrjälä 已提交
567
	switch (fb->format->format) {
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	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:
602 603
		MISSING_CASE(fb->format->format);
		return 0;
604 605
	}

606 607 608
	if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
		sprctl |= SP_YUV_FORMAT_BT709;

V
Ville Syrjälä 已提交
609
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
610 611
		sprctl |= SP_TILED;

612
	if (rotation & DRM_MODE_ROTATE_180)
613 614
		sprctl |= SP_ROTATE_180;

615
	if (rotation & DRM_MODE_REFLECT_X)
616 617
		sprctl |= SP_MIRROR;

618 619 620
	if (key->flags & I915_SET_COLORKEY_SOURCE)
		sprctl |= SP_SOURCE_KEY;

621 622 623 624
	return sprctl;
}

static void
625
vlv_update_plane(struct intel_plane *plane,
626 627 628
		 const struct intel_crtc_state *crtc_state,
		 const struct intel_plane_state *plane_state)
{
629 630 631 632
	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;
633
	u32 sprctl = plane_state->ctl;
634
	u32 sprsurf_offset = plane_state->color_plane[0].offset;
635
	u32 linear_offset;
636 637 638 639 640
	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);
641 642
	uint32_t x = plane_state->color_plane[0].x;
	uint32_t y = plane_state->color_plane[0].y;
643 644
	unsigned long irqflags;

645 646 647 648
	/* Sizes are 0 based */
	crtc_w--;
	crtc_h--;

649
	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
650

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

653 654
	vlv_update_clrc(plane_state);

655
	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B)
656
		chv_update_csc(plane_state);
657

658
	if (key->flags) {
659 660 661
		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);
662
	}
663 664
	I915_WRITE_FW(SPSTRIDE(pipe, plane_id),
		      plane_state->color_plane[0].stride);
665
	I915_WRITE_FW(SPPOS(pipe, plane_id), (crtc_y << 16) | crtc_x);
666

V
Ville Syrjälä 已提交
667
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
668
		I915_WRITE_FW(SPTILEOFF(pipe, plane_id), (y << 16) | x);
669
	else
670
		I915_WRITE_FW(SPLINOFF(pipe, plane_id), linear_offset);
671

672
	I915_WRITE_FW(SPCONSTALPHA(pipe, plane_id), 0);
673

674 675 676 677 678 679 680
	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);
	POSTING_READ_FW(SPSURF(pipe, plane_id));

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
681 682 683
}

static void
684
vlv_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
685
{
686 687 688
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum pipe pipe = plane->pipe;
	enum plane_id plane_id = plane->id;
689 690 691
	unsigned long irqflags;

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

693
	I915_WRITE_FW(SPCNTR(pipe, plane_id), 0);
694

695 696 697 698
	I915_WRITE_FW(SPSURF(pipe, plane_id), 0);
	POSTING_READ_FW(SPSURF(pipe, plane_id));

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
699 700
}

701
static bool
702 703
vlv_plane_get_hw_state(struct intel_plane *plane,
		       enum pipe *pipe)
704 705 706 707 708 709
{
	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;

710
	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
711 712 713
	if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
		return false;

714 715 716
	ret = I915_READ(SPCNTR(plane->pipe, plane_id)) & SP_ENABLE;

	*pipe = plane->pipe;
717 718 719 720 721 722

	intel_display_power_put(dev_priv, power_domain);

	return ret;
}

723 724
static u32 ivb_sprite_ctl(const struct intel_crtc_state *crtc_state,
			  const struct intel_plane_state *plane_state)
725
{
726 727 728
	struct drm_i915_private *dev_priv =
		to_i915(plane_state->base.plane->dev);
	const struct drm_framebuffer *fb = plane_state->base.fb;
729
	unsigned int rotation = plane_state->base.rotation;
730
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
731 732 733
	u32 sprctl;

	sprctl = SPRITE_ENABLE | SPRITE_GAMMA_ENABLE;
734

735 736 737 738 739
	if (IS_IVYBRIDGE(dev_priv))
		sprctl |= SPRITE_TRICKLE_FEED_DISABLE;

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

V
Ville Syrjälä 已提交
741
	switch (fb->format->format) {
742
	case DRM_FORMAT_XBGR8888:
743
		sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
744 745
		break;
	case DRM_FORMAT_XRGB8888:
746
		sprctl |= SPRITE_FORMAT_RGBX888;
747 748 749 750 751 752 753 754 755 756 757 758 759 760
		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:
761 762
		MISSING_CASE(fb->format->format);
		return 0;
763 764
	}

765 766 767
	if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
		sprctl |= SPRITE_YUV_TO_RGB_CSC_FORMAT_BT709;

768 769 770
	if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
		sprctl |= SPRITE_YUV_RANGE_CORRECTION_DISABLE;

V
Ville Syrjälä 已提交
771
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
772 773
		sprctl |= SPRITE_TILED;

774
	if (rotation & DRM_MODE_ROTATE_180)
775 776
		sprctl |= SPRITE_ROTATE_180;

777 778 779 780 781
	if (key->flags & I915_SET_COLORKEY_DESTINATION)
		sprctl |= SPRITE_DEST_KEY;
	else if (key->flags & I915_SET_COLORKEY_SOURCE)
		sprctl |= SPRITE_SOURCE_KEY;

782 783 784 785
	return sprctl;
}

static void
786
ivb_update_plane(struct intel_plane *plane,
787 788 789
		 const struct intel_crtc_state *crtc_state,
		 const struct intel_plane_state *plane_state)
{
790 791 792
	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;
793
	u32 sprctl = plane_state->ctl, sprscale = 0;
794
	u32 sprsurf_offset = plane_state->color_plane[0].offset;
795
	u32 linear_offset;
796 797 798 799 800
	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);
801 802
	uint32_t x = plane_state->color_plane[0].x;
	uint32_t y = plane_state->color_plane[0].y;
803 804 805 806
	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;

807 808 809 810 811 812
	/* Sizes are 0 based */
	src_w--;
	src_h--;
	crtc_w--;
	crtc_h--;

813
	if (crtc_w != src_w || crtc_h != src_h)
814 815
		sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;

816
	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
817

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

820
	if (key->flags) {
821 822 823
		I915_WRITE_FW(SPRKEYVAL(pipe), key->min_value);
		I915_WRITE_FW(SPRKEYMAX(pipe), key->max_value);
		I915_WRITE_FW(SPRKEYMSK(pipe), key->channel_mask);
824 825
	}

826
	I915_WRITE_FW(SPRSTRIDE(pipe), plane_state->color_plane[0].stride);
827
	I915_WRITE_FW(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
828

829 830
	/* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
	 * register */
831
	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
832
		I915_WRITE_FW(SPROFFSET(pipe), (y << 16) | x);
V
Ville Syrjälä 已提交
833
	else if (fb->modifier == I915_FORMAT_MOD_X_TILED)
834
		I915_WRITE_FW(SPRTILEOFF(pipe), (y << 16) | x);
835
	else
836
		I915_WRITE_FW(SPRLINOFF(pipe), linear_offset);
837

838
	I915_WRITE_FW(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
839
	if (IS_IVYBRIDGE(dev_priv))
840 841 842 843 844 845 846
		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);
	POSTING_READ_FW(SPRSURF(pipe));

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
847 848 849
}

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

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

858
	I915_WRITE_FW(SPRCTL(pipe), 0);
859
	/* Can't leave the scaler enabled... */
860
	if (IS_IVYBRIDGE(dev_priv))
861
		I915_WRITE_FW(SPRSCALE(pipe), 0);
862

863 864 865 866
	I915_WRITE_FW(SPRSURF(pipe), 0);
	POSTING_READ_FW(SPRSURF(pipe));

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
867 868
}

869
static bool
870 871
ivb_plane_get_hw_state(struct intel_plane *plane,
		       enum pipe *pipe)
872 873 874 875 876
{
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum intel_display_power_domain power_domain;
	bool ret;

877
	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
878 879 880
	if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
		return false;

881 882 883
	ret =  I915_READ(SPRCTL(plane->pipe)) & SPRITE_ENABLE;

	*pipe = plane->pipe;
884 885 886 887 888 889

	intel_display_power_put(dev_priv, power_domain);

	return ret;
}

890 891 892 893 894 895 896 897
static unsigned int
g4x_sprite_max_stride(struct intel_plane *plane,
		      u32 pixel_format, u64 modifier,
		      unsigned int rotation)
{
	return 16384;
}

898
static u32 g4x_sprite_ctl(const struct intel_crtc_state *crtc_state,
899
			  const struct intel_plane_state *plane_state)
900
{
901 902 903
	struct drm_i915_private *dev_priv =
		to_i915(plane_state->base.plane->dev);
	const struct drm_framebuffer *fb = plane_state->base.fb;
904
	unsigned int rotation = plane_state->base.rotation;
905
	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
906 907 908
	u32 dvscntr;

	dvscntr = DVS_ENABLE | DVS_GAMMA_ENABLE;
909

910 911
	if (IS_GEN6(dev_priv))
		dvscntr |= DVS_TRICKLE_FEED_DISABLE;
912

V
Ville Syrjälä 已提交
913
	switch (fb->format->format) {
914
	case DRM_FORMAT_XBGR8888:
915
		dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
916 917
		break;
	case DRM_FORMAT_XRGB8888:
918
		dvscntr |= DVS_FORMAT_RGBX888;
919 920 921 922 923 924 925 926 927 928 929 930 931 932
		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:
933 934
		MISSING_CASE(fb->format->format);
		return 0;
935 936
	}

937 938 939
	if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
		dvscntr |= DVS_YUV_FORMAT_BT709;

940 941 942
	if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
		dvscntr |= DVS_YUV_RANGE_CORRECTION_DISABLE;

V
Ville Syrjälä 已提交
943
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
944 945
		dvscntr |= DVS_TILED;

946
	if (rotation & DRM_MODE_ROTATE_180)
947 948
		dvscntr |= DVS_ROTATE_180;

949 950 951 952 953
	if (key->flags & I915_SET_COLORKEY_DESTINATION)
		dvscntr |= DVS_DEST_KEY;
	else if (key->flags & I915_SET_COLORKEY_SOURCE)
		dvscntr |= DVS_SOURCE_KEY;

954 955 956 957
	return dvscntr;
}

static void
958
g4x_update_plane(struct intel_plane *plane,
959 960 961
		 const struct intel_crtc_state *crtc_state,
		 const struct intel_plane_state *plane_state)
{
962 963 964
	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;
965
	u32 dvscntr = plane_state->ctl, dvsscale = 0;
966
	u32 dvssurf_offset = plane_state->color_plane[0].offset;
967
	u32 linear_offset;
968 969 970 971 972
	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);
973 974
	uint32_t x = plane_state->color_plane[0].x;
	uint32_t y = plane_state->color_plane[0].y;
975 976 977 978
	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;

979 980 981 982 983 984
	/* Sizes are 0 based */
	src_w--;
	src_h--;
	crtc_w--;
	crtc_h--;

985
	if (crtc_w != src_w || crtc_h != src_h)
986 987
		dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;

988
	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
989

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

992
	if (key->flags) {
993 994 995
		I915_WRITE_FW(DVSKEYVAL(pipe), key->min_value);
		I915_WRITE_FW(DVSKEYMAX(pipe), key->max_value);
		I915_WRITE_FW(DVSKEYMSK(pipe), key->channel_mask);
996 997
	}

998
	I915_WRITE_FW(DVSSTRIDE(pipe), plane_state->color_plane[0].stride);
999
	I915_WRITE_FW(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
1000

V
Ville Syrjälä 已提交
1001
	if (fb->modifier == I915_FORMAT_MOD_X_TILED)
1002
		I915_WRITE_FW(DVSTILEOFF(pipe), (y << 16) | x);
1003
	else
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
		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);
	POSTING_READ_FW(DVSSURF(pipe));

	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1014 1015 1016
}

static void
1017
g4x_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
1018
{
1019 1020
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum pipe pipe = plane->pipe;
1021
	unsigned long irqflags;
1022

1023 1024 1025
	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);

	I915_WRITE_FW(DVSCNTR(pipe), 0);
1026
	/* Disable the scaler */
1027 1028 1029 1030
	I915_WRITE_FW(DVSSCALE(pipe), 0);

	I915_WRITE_FW(DVSSURF(pipe), 0);
	POSTING_READ_FW(DVSSURF(pipe));
1031

1032
	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1033 1034
}

1035
static bool
1036 1037
g4x_plane_get_hw_state(struct intel_plane *plane,
		       enum pipe *pipe)
1038 1039 1040 1041 1042
{
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
	enum intel_display_power_domain power_domain;
	bool ret;

1043
	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
1044 1045 1046
	if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
		return false;

1047 1048 1049
	ret = I915_READ(DVSCNTR(plane->pipe)) & DVS_ENABLE;

	*pipe = plane->pipe;
1050 1051 1052 1053 1054 1055

	intel_display_power_put(dev_priv, power_domain);

	return ret;
}

1056
static int
1057 1058
g4x_sprite_check_scaling(struct intel_crtc_state *crtc_state,
			 struct intel_plane_state *plane_state)
1059
{
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	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)
1079
		return 0;
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

	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;
1091
	}
1092

1093 1094 1095 1096 1097 1098
	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);
1099
		return -EINVAL;
1100
	}
1101

1102 1103 1104
	if (width_bytes > 4096) {
		DRM_DEBUG_KMS("Fetch width (%d) exceeds hardware max with scaling (%u)\n",
			      width_bytes, 4096);
1105
		return -EINVAL;
1106
	}
1107

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
	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);
	int max_scale, min_scale;
	int ret;

	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;
1132
	} else {
1133 1134
		min_scale = DRM_PLANE_HELPER_NO_SCALING;
		max_scale = DRM_PLANE_HELPER_NO_SCALING;
1135 1136
	}

1137
	ret = drm_atomic_helper_check_plane_state(&plane_state->base,
1138 1139 1140 1141 1142
						  &crtc_state->base,
						  min_scale, max_scale,
						  true, true);
	if (ret)
		return ret;
1143

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

1147 1148 1149
	ret = intel_plane_check_src_coordinates(plane_state);
	if (ret)
		return ret;
1150

1151 1152 1153
	ret = g4x_sprite_check_scaling(crtc_state, plane_state);
	if (ret)
		return ret;
1154

1155 1156 1157
	ret = i9xx_check_plane_surface(plane_state);
	if (ret)
		return ret;
1158

1159 1160 1161 1162
	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);
1163

1164 1165
	return 0;
}
1166

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
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;
}

1184 1185 1186 1187 1188 1189
static int
vlv_sprite_check(struct intel_crtc_state *crtc_state,
		 struct intel_plane_state *plane_state)
{
	int ret;

1190 1191 1192 1193
	ret = chv_plane_check_rotation(plane_state);
	if (ret)
		return ret;

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
	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);
1214

1215 1216 1217
	return 0;
}

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
static int skl_plane_check_fb(const struct intel_crtc_state *crtc_state,
			      const struct intel_plane_state *plane_state)
{
	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) &&
1229
	    is_ccs_modifier(fb->modifier)) {
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
		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;
		}

		/*
		 * 90/270 is not allowed with RGB64 16:16:16:16,
		 * RGB 16-bit 5:6:5, and Indexed 8-bit.
		 * TBD: Add RGB64 case once its added in supported format list.
		 */
		switch (fb->format->format) {
		case DRM_FORMAT_C8:
		case DRM_FORMAT_RGB565:
			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;
}

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
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;
}

1309 1310 1311 1312 1313 1314 1315 1316
int skl_plane_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);
	int max_scale, min_scale;
	int ret;

1317 1318 1319 1320
	ret = skl_plane_check_fb(crtc_state, plane_state);
	if (ret)
		return ret;

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
	/* use scaler when colorkey is not required */
	if (!plane_state->ckey.flags) {
		const struct drm_framebuffer *fb = plane_state->base.fb;

		min_scale = 1;
		max_scale = skl_max_scale(crtc_state,
					  fb ? fb->format->format : 0);
	} else {
		min_scale = DRM_PLANE_HELPER_NO_SCALING;
		max_scale = DRM_PLANE_HELPER_NO_SCALING;
1331 1332
	}

1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
	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;

1343 1344 1345 1346
	ret = skl_plane_check_dst_coordinates(crtc_state, plane_state);
	if (ret)
		return ret;

1347 1348 1349 1350
	ret = intel_plane_check_src_coordinates(plane_state);
	if (ret)
		return ret;

1351
	ret = skl_check_plane_surface(plane_state);
1352 1353 1354 1355 1356
	if (ret)
		return ret;

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

1357
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
1358 1359
		plane_state->color_ctl = glk_plane_color_ctl(crtc_state,
							     plane_state);
1360

1361 1362 1363
	return 0;
}

1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
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;
}

1395 1396
int intel_sprite_set_colorkey_ioctl(struct drm_device *dev, void *data,
				    struct drm_file *file_priv)
1397
{
1398
	struct drm_i915_private *dev_priv = to_i915(dev);
1399 1400
	struct drm_intel_sprite_colorkey *set = data;
	struct drm_plane *plane;
1401 1402 1403
	struct drm_plane_state *plane_state;
	struct drm_atomic_state *state;
	struct drm_modeset_acquire_ctx ctx;
1404 1405
	int ret = 0;

1406 1407 1408
	/* ignore the pointless "none" flag */
	set->flags &= ~I915_SET_COLORKEY_NONE;

1409 1410 1411
	if (set->flags & ~(I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
		return -EINVAL;

1412 1413 1414 1415
	/* 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;

1416
	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1417 1418 1419
	    set->flags & I915_SET_COLORKEY_DESTINATION)
		return -EINVAL;

1420
	plane = drm_plane_find(dev, file_priv, set->plane_id);
1421 1422
	if (!plane || plane->type != DRM_PLANE_TYPE_OVERLAY)
		return -ENOENT;
1423

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
	/*
	 * 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;

1434
	drm_modeset_acquire_init(&ctx, 0);
1435

1436 1437 1438 1439
	state = drm_atomic_state_alloc(plane->dev);
	if (!state) {
		ret = -ENOMEM;
		goto out;
1440
	}
1441 1442 1443 1444 1445
	state->acquire_ctx = &ctx;

	while (1) {
		plane_state = drm_atomic_get_plane_state(state, plane);
		ret = PTR_ERR_OR_ZERO(plane_state);
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
		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);
1463
		}
1464

1465 1466 1467
		if (!ret)
			ret = drm_atomic_commit(state);

1468 1469
		if (ret != -EDEADLK)
			break;
1470

1471 1472 1473
		drm_atomic_state_clear(state);
		drm_modeset_backoff(&ctx);
	}
1474

1475
	drm_atomic_state_put(state);
1476 1477 1478 1479
out:
	drm_modeset_drop_locks(&ctx);
	drm_modeset_acquire_fini(&ctx);
	return ret;
1480 1481
}

1482
static const uint32_t g4x_plane_formats[] = {
1483 1484 1485 1486 1487 1488 1489
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_VYUY,
};

1490 1491 1492 1493 1494 1495
static const uint64_t i9xx_plane_format_modifiers[] = {
	I915_FORMAT_MOD_X_TILED,
	DRM_FORMAT_MOD_LINEAR,
	DRM_FORMAT_MOD_INVALID
};

1496
static const uint32_t snb_plane_formats[] = {
1497 1498 1499 1500 1501 1502 1503 1504
	DRM_FORMAT_XBGR8888,
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_VYUY,
};

1505
static const uint32_t vlv_plane_formats[] = {
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
	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,
};

1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
static uint32_t skl_plane_formats[] = {
	DRM_FORMAT_RGB565,
	DRM_FORMAT_ABGR8888,
	DRM_FORMAT_ARGB8888,
	DRM_FORMAT_XBGR8888,
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_VYUY,
};

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
static uint32_t skl_planar_formats[] = {
	DRM_FORMAT_RGB565,
	DRM_FORMAT_ABGR8888,
	DRM_FORMAT_ARGB8888,
	DRM_FORMAT_XBGR8888,
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_VYUY,
	DRM_FORMAT_NV12,
};

1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
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,
1555 1556
	I915_FORMAT_MOD_Yf_TILED,
	I915_FORMAT_MOD_Y_TILED,
1557 1558 1559 1560 1561
	I915_FORMAT_MOD_X_TILED,
	DRM_FORMAT_MOD_LINEAR,
	DRM_FORMAT_MOD_INVALID
};

1562 1563
static bool g4x_sprite_format_mod_supported(struct drm_plane *_plane,
					    u32 format, u64 modifier)
1564
{
1565 1566 1567 1568 1569 1570 1571 1572
	switch (modifier) {
	case DRM_FORMAT_MOD_LINEAR:
	case I915_FORMAT_MOD_X_TILED:
		break;
	default:
		return false;
	}

1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
	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;
	}
}

1588 1589
static bool snb_sprite_format_mod_supported(struct drm_plane *_plane,
					    u32 format, u64 modifier)
1590
{
1591 1592 1593 1594 1595 1596 1597 1598
	switch (modifier) {
	case DRM_FORMAT_MOD_LINEAR:
	case I915_FORMAT_MOD_X_TILED:
		break;
	default:
		return false;
	}

1599
	switch (format) {
1600 1601
	case DRM_FORMAT_XRGB8888:
	case DRM_FORMAT_XBGR8888:
1602 1603 1604 1605
	case DRM_FORMAT_YUYV:
	case DRM_FORMAT_YVYU:
	case DRM_FORMAT_UYVY:
	case DRM_FORMAT_VYUY:
1606 1607 1608 1609 1610 1611 1612 1613 1614
		if (modifier == DRM_FORMAT_MOD_LINEAR ||
		    modifier == I915_FORMAT_MOD_X_TILED)
			return true;
		/* fall through */
	default:
		return false;
	}
}

1615 1616
static bool vlv_sprite_format_mod_supported(struct drm_plane *_plane,
					    u32 format, u64 modifier)
1617
{
1618 1619 1620 1621 1622 1623 1624 1625
	switch (modifier) {
	case DRM_FORMAT_MOD_LINEAR:
	case I915_FORMAT_MOD_X_TILED:
		break;
	default:
		return false;
	}

1626
	switch (format) {
1627
	case DRM_FORMAT_RGB565:
1628
	case DRM_FORMAT_ABGR8888:
1629
	case DRM_FORMAT_ARGB8888:
1630 1631
	case DRM_FORMAT_XBGR8888:
	case DRM_FORMAT_XRGB8888:
1632 1633
	case DRM_FORMAT_XBGR2101010:
	case DRM_FORMAT_ABGR2101010:
1634 1635 1636 1637
	case DRM_FORMAT_YUYV:
	case DRM_FORMAT_YVYU:
	case DRM_FORMAT_UYVY:
	case DRM_FORMAT_VYUY:
1638 1639 1640 1641 1642 1643 1644 1645 1646
		if (modifier == DRM_FORMAT_MOD_LINEAR ||
		    modifier == I915_FORMAT_MOD_X_TILED)
			return true;
		/* fall through */
	default:
		return false;
	}
}

1647 1648
static bool skl_plane_format_mod_supported(struct drm_plane *_plane,
					   u32 format, u64 modifier)
1649
{
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
	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;
	}

1667 1668 1669 1670 1671
	switch (format) {
	case DRM_FORMAT_XRGB8888:
	case DRM_FORMAT_XBGR8888:
	case DRM_FORMAT_ARGB8888:
	case DRM_FORMAT_ABGR8888:
1672
		if (is_ccs_modifier(modifier))
1673 1674
			return true;
		/* fall through */
1675 1676 1677 1678 1679 1680 1681
	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:
1682
	case DRM_FORMAT_NV12:
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
		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;
	}
}

1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
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,
};
1707

1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
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,
};
1718

1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
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,
};
1729

1730
static const struct drm_plane_funcs skl_plane_funcs = {
1731 1732 1733 1734 1735 1736 1737
	.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,
1738
	.format_mod_supported = skl_plane_format_mod_supported,
1739 1740
};

1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
bool skl_plane_has_ccs(struct drm_i915_private *dev_priv,
		       enum pipe pipe, enum plane_id plane_id)
{
	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);
}

1758
struct intel_plane *
1759 1760
intel_sprite_plane_create(struct drm_i915_private *dev_priv,
			  enum pipe pipe, int plane)
1761
{
1762 1763
	struct intel_plane *intel_plane = NULL;
	struct intel_plane_state *state = NULL;
1764
	const struct drm_plane_funcs *plane_funcs;
1765
	unsigned long possible_crtcs;
1766
	const uint32_t *plane_formats;
1767
	const uint64_t *modifiers;
1768
	unsigned int supported_rotations;
1769
	int num_plane_formats;
1770 1771
	int ret;

1772
	intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
1773 1774 1775 1776
	if (!intel_plane) {
		ret = -ENOMEM;
		goto fail;
	}
1777

1778 1779
	state = intel_create_plane_state(&intel_plane->base);
	if (!state) {
1780 1781
		ret = -ENOMEM;
		goto fail;
1782
	}
1783
	intel_plane->base.state = &state->base;
1784

1785
	if (INTEL_GEN(dev_priv) >= 9) {
1786 1787
		state->scaler_id = -1;

1788 1789 1790
		intel_plane->has_ccs = skl_plane_has_ccs(dev_priv, pipe,
							 PLANE_SPRITE0 + plane);

1791
		intel_plane->max_stride = skl_plane_max_stride;
1792 1793
		intel_plane->update_plane = skl_update_plane;
		intel_plane->disable_plane = skl_disable_plane;
1794
		intel_plane->get_hw_state = skl_plane_get_hw_state;
1795
		intel_plane->check_plane = skl_plane_check;
1796

1797 1798 1799 1800 1801 1802 1803 1804
		if (skl_plane_has_planar(dev_priv, pipe,
					 PLANE_SPRITE0 + plane)) {
			plane_formats = skl_planar_formats;
			num_plane_formats = ARRAY_SIZE(skl_planar_formats);
		} else {
			plane_formats = skl_plane_formats;
			num_plane_formats = ARRAY_SIZE(skl_plane_formats);
		}
1805

1806
		if (intel_plane->has_ccs)
1807 1808 1809
			modifiers = skl_plane_format_modifiers_ccs;
		else
			modifiers = skl_plane_format_modifiers_noccs;
1810 1811

		plane_funcs = &skl_plane_funcs;
V
Ville Syrjälä 已提交
1812
	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1813
		intel_plane->max_stride = i9xx_plane_max_stride;
V
Ville Syrjälä 已提交
1814 1815
		intel_plane->update_plane = vlv_update_plane;
		intel_plane->disable_plane = vlv_disable_plane;
1816
		intel_plane->get_hw_state = vlv_plane_get_hw_state;
1817
		intel_plane->check_plane = vlv_sprite_check;
1818

V
Ville Syrjälä 已提交
1819 1820
		plane_formats = vlv_plane_formats;
		num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
1821
		modifiers = i9xx_plane_format_modifiers;
1822 1823

		plane_funcs = &vlv_sprite_funcs;
V
Ville Syrjälä 已提交
1824
	} else if (INTEL_GEN(dev_priv) >= 7) {
1825
		intel_plane->max_stride = g4x_sprite_max_stride;
V
Ville Syrjälä 已提交
1826 1827
		intel_plane->update_plane = ivb_update_plane;
		intel_plane->disable_plane = ivb_disable_plane;
1828
		intel_plane->get_hw_state = ivb_plane_get_hw_state;
1829
		intel_plane->check_plane = g4x_sprite_check;
1830

V
Ville Syrjälä 已提交
1831 1832
		plane_formats = snb_plane_formats;
		num_plane_formats = ARRAY_SIZE(snb_plane_formats);
1833
		modifiers = i9xx_plane_format_modifiers;
1834 1835

		plane_funcs = &snb_sprite_funcs;
V
Ville Syrjälä 已提交
1836
	} else {
1837
		intel_plane->max_stride = g4x_sprite_max_stride;
1838 1839
		intel_plane->update_plane = g4x_update_plane;
		intel_plane->disable_plane = g4x_disable_plane;
1840
		intel_plane->get_hw_state = g4x_plane_get_hw_state;
1841
		intel_plane->check_plane = g4x_sprite_check;
1842

1843
		modifiers = i9xx_plane_format_modifiers;
V
Ville Syrjälä 已提交
1844
		if (IS_GEN6(dev_priv)) {
1845 1846
			plane_formats = snb_plane_formats;
			num_plane_formats = ARRAY_SIZE(snb_plane_formats);
1847 1848

			plane_funcs = &snb_sprite_funcs;
V
Ville Syrjälä 已提交
1849
		} else {
1850 1851
			plane_formats = g4x_plane_formats;
			num_plane_formats = ARRAY_SIZE(g4x_plane_formats);
1852 1853

			plane_funcs = &g4x_sprite_funcs;
1854
		}
1855 1856
	}

1857
	if (INTEL_GEN(dev_priv) >= 9) {
1858
		supported_rotations =
1859 1860
			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
			DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
1861 1862
	} else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
		supported_rotations =
1863 1864
			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
			DRM_MODE_REFLECT_X;
1865 1866
	} else {
		supported_rotations =
1867
			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
1868 1869
	}

1870
	intel_plane->pipe = pipe;
1871
	intel_plane->i9xx_plane = plane;
1872
	intel_plane->id = PLANE_SPRITE0 + plane;
1873
	intel_plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, intel_plane->id);
1874

1875
	possible_crtcs = (1 << pipe);
1876

V
Ville Syrjälä 已提交
1877
	if (INTEL_GEN(dev_priv) >= 9)
1878
		ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
1879
					       possible_crtcs, plane_funcs,
1880
					       plane_formats, num_plane_formats,
1881 1882
					       modifiers,
					       DRM_PLANE_TYPE_OVERLAY,
1883 1884
					       "plane %d%c", plane + 2, pipe_name(pipe));
	else
1885
		ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
1886
					       possible_crtcs, plane_funcs,
1887
					       plane_formats, num_plane_formats,
1888 1889
					       modifiers,
					       DRM_PLANE_TYPE_OVERLAY,
1890
					       "sprite %c", sprite_name(pipe, plane));
1891 1892
	if (ret)
		goto fail;
1893

1894
	drm_plane_create_rotation_property(&intel_plane->base,
1895
					   DRM_MODE_ROTATE_0,
1896
					   supported_rotations);
1897

1898 1899 1900
	drm_plane_create_color_properties(&intel_plane->base,
					  BIT(DRM_COLOR_YCBCR_BT601) |
					  BIT(DRM_COLOR_YCBCR_BT709),
1901 1902
					  BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
					  BIT(DRM_COLOR_YCBCR_FULL_RANGE),
1903
					  DRM_COLOR_YCBCR_BT709,
1904 1905
					  DRM_COLOR_YCBCR_LIMITED_RANGE);

1906 1907
	drm_plane_helper_add(&intel_plane->base, &intel_plane_helper_funcs);

1908
	return intel_plane;
1909 1910 1911 1912 1913

fail:
	kfree(state);
	kfree(intel_plane);

1914
	return ERR_PTR(ret);
1915
}