Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Feb 10-13, 2025
Register
Loading...
v5.14.15
   1// SPDX-License-Identifier: MIT
   2/*
   3 * Copyright © 2020 Intel Corporation
   4 */
   5#include <linux/kernel.h>
   6
   7#include <drm/drm_atomic_helper.h>
 
   8#include <drm/drm_fourcc.h>
   9#include <drm/drm_plane_helper.h>
  10
 
 
 
  11#include "intel_atomic.h"
  12#include "intel_atomic_plane.h"
  13#include "intel_de.h"
  14#include "intel_display_types.h"
  15#include "intel_fb.h"
 
  16#include "intel_sprite.h"
  17#include "i9xx_plane.h"
  18
  19/* Primary plane formats for gen <= 3 */
  20static const u32 i8xx_primary_formats[] = {
  21	DRM_FORMAT_C8,
  22	DRM_FORMAT_XRGB1555,
  23	DRM_FORMAT_RGB565,
  24	DRM_FORMAT_XRGB8888,
  25};
  26
  27/* Primary plane formats for ivb (no fp16 due to hw issue) */
  28static const u32 ivb_primary_formats[] = {
  29	DRM_FORMAT_C8,
  30	DRM_FORMAT_RGB565,
  31	DRM_FORMAT_XRGB8888,
  32	DRM_FORMAT_XBGR8888,
  33	DRM_FORMAT_XRGB2101010,
  34	DRM_FORMAT_XBGR2101010,
  35};
  36
  37/* Primary plane formats for gen >= 4, except ivb */
  38static const u32 i965_primary_formats[] = {
  39	DRM_FORMAT_C8,
  40	DRM_FORMAT_RGB565,
  41	DRM_FORMAT_XRGB8888,
  42	DRM_FORMAT_XBGR8888,
  43	DRM_FORMAT_XRGB2101010,
  44	DRM_FORMAT_XBGR2101010,
  45	DRM_FORMAT_XBGR16161616F,
  46};
  47
  48/* Primary plane formats for vlv/chv */
  49static const u32 vlv_primary_formats[] = {
  50	DRM_FORMAT_C8,
  51	DRM_FORMAT_RGB565,
  52	DRM_FORMAT_XRGB8888,
  53	DRM_FORMAT_XBGR8888,
  54	DRM_FORMAT_ARGB8888,
  55	DRM_FORMAT_ABGR8888,
  56	DRM_FORMAT_XRGB2101010,
  57	DRM_FORMAT_XBGR2101010,
  58	DRM_FORMAT_ARGB2101010,
  59	DRM_FORMAT_ABGR2101010,
  60	DRM_FORMAT_XBGR16161616F,
  61};
  62
  63static const u64 i9xx_format_modifiers[] = {
  64	I915_FORMAT_MOD_X_TILED,
  65	DRM_FORMAT_MOD_LINEAR,
  66	DRM_FORMAT_MOD_INVALID
  67};
  68
  69static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
  70					    u32 format, u64 modifier)
  71{
  72	switch (modifier) {
  73	case DRM_FORMAT_MOD_LINEAR:
  74	case I915_FORMAT_MOD_X_TILED:
  75		break;
  76	default:
  77		return false;
  78	}
  79
  80	switch (format) {
  81	case DRM_FORMAT_C8:
  82	case DRM_FORMAT_RGB565:
  83	case DRM_FORMAT_XRGB1555:
  84	case DRM_FORMAT_XRGB8888:
  85		return modifier == DRM_FORMAT_MOD_LINEAR ||
  86			modifier == I915_FORMAT_MOD_X_TILED;
  87	default:
  88		return false;
  89	}
  90}
  91
  92static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
  93					    u32 format, u64 modifier)
  94{
  95	switch (modifier) {
  96	case DRM_FORMAT_MOD_LINEAR:
  97	case I915_FORMAT_MOD_X_TILED:
  98		break;
  99	default:
 100		return false;
 101	}
 102
 103	switch (format) {
 104	case DRM_FORMAT_C8:
 105	case DRM_FORMAT_RGB565:
 106	case DRM_FORMAT_XRGB8888:
 107	case DRM_FORMAT_XBGR8888:
 108	case DRM_FORMAT_ARGB8888:
 109	case DRM_FORMAT_ABGR8888:
 110	case DRM_FORMAT_XRGB2101010:
 111	case DRM_FORMAT_XBGR2101010:
 112	case DRM_FORMAT_ARGB2101010:
 113	case DRM_FORMAT_ABGR2101010:
 114	case DRM_FORMAT_XBGR16161616F:
 115		return modifier == DRM_FORMAT_MOD_LINEAR ||
 116			modifier == I915_FORMAT_MOD_X_TILED;
 117	default:
 118		return false;
 119	}
 120}
 121
 122static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
 123			       enum i9xx_plane_id i9xx_plane)
 124{
 125	if (!HAS_FBC(dev_priv))
 126		return false;
 127
 128	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
 129		return i9xx_plane == PLANE_A; /* tied to pipe A */
 130	else if (IS_IVYBRIDGE(dev_priv))
 131		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
 132			i9xx_plane == PLANE_C;
 133	else if (DISPLAY_VER(dev_priv) >= 4)
 134		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
 135	else
 136		return i9xx_plane == PLANE_A;
 137}
 138
 
 
 
 
 
 
 
 
 
 139static bool i9xx_plane_has_windowing(struct intel_plane *plane)
 140{
 141	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 142	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 143
 144	if (IS_CHERRYVIEW(dev_priv))
 145		return i9xx_plane == PLANE_B;
 146	else if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
 147		return false;
 148	else if (DISPLAY_VER(dev_priv) == 4)
 149		return i9xx_plane == PLANE_C;
 150	else
 151		return i9xx_plane == PLANE_B ||
 152			i9xx_plane == PLANE_C;
 153}
 154
 155static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
 156			  const struct intel_plane_state *plane_state)
 157{
 158	struct drm_i915_private *dev_priv =
 159		to_i915(plane_state->uapi.plane->dev);
 160	const struct drm_framebuffer *fb = plane_state->hw.fb;
 161	unsigned int rotation = plane_state->hw.rotation;
 162	u32 dspcntr;
 163
 164	dspcntr = DISPLAY_PLANE_ENABLE;
 165
 166	if (IS_G4X(dev_priv) || IS_IRONLAKE(dev_priv) ||
 167	    IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv))
 168		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
 169
 170	switch (fb->format->format) {
 171	case DRM_FORMAT_C8:
 172		dspcntr |= DISPPLANE_8BPP;
 173		break;
 174	case DRM_FORMAT_XRGB1555:
 175		dspcntr |= DISPPLANE_BGRX555;
 176		break;
 177	case DRM_FORMAT_ARGB1555:
 178		dspcntr |= DISPPLANE_BGRA555;
 179		break;
 180	case DRM_FORMAT_RGB565:
 181		dspcntr |= DISPPLANE_BGRX565;
 182		break;
 183	case DRM_FORMAT_XRGB8888:
 184		dspcntr |= DISPPLANE_BGRX888;
 185		break;
 186	case DRM_FORMAT_XBGR8888:
 187		dspcntr |= DISPPLANE_RGBX888;
 188		break;
 189	case DRM_FORMAT_ARGB8888:
 190		dspcntr |= DISPPLANE_BGRA888;
 191		break;
 192	case DRM_FORMAT_ABGR8888:
 193		dspcntr |= DISPPLANE_RGBA888;
 194		break;
 195	case DRM_FORMAT_XRGB2101010:
 196		dspcntr |= DISPPLANE_BGRX101010;
 197		break;
 198	case DRM_FORMAT_XBGR2101010:
 199		dspcntr |= DISPPLANE_RGBX101010;
 200		break;
 201	case DRM_FORMAT_ARGB2101010:
 202		dspcntr |= DISPPLANE_BGRA101010;
 203		break;
 204	case DRM_FORMAT_ABGR2101010:
 205		dspcntr |= DISPPLANE_RGBA101010;
 206		break;
 207	case DRM_FORMAT_XBGR16161616F:
 208		dspcntr |= DISPPLANE_RGBX161616;
 209		break;
 210	default:
 211		MISSING_CASE(fb->format->format);
 212		return 0;
 213	}
 214
 215	if (DISPLAY_VER(dev_priv) >= 4 &&
 216	    fb->modifier == I915_FORMAT_MOD_X_TILED)
 217		dspcntr |= DISPPLANE_TILED;
 218
 219	if (rotation & DRM_MODE_ROTATE_180)
 220		dspcntr |= DISPPLANE_ROTATE_180;
 221
 222	if (rotation & DRM_MODE_REFLECT_X)
 223		dspcntr |= DISPPLANE_MIRROR;
 224
 225	return dspcntr;
 226}
 227
 228int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
 229{
 230	struct drm_i915_private *dev_priv =
 231		to_i915(plane_state->uapi.plane->dev);
 232	const struct drm_framebuffer *fb = plane_state->hw.fb;
 233	int src_x, src_y, src_w;
 234	u32 offset;
 235	int ret;
 236
 237	ret = intel_plane_compute_gtt(plane_state);
 238	if (ret)
 239		return ret;
 240
 241	if (!plane_state->uapi.visible)
 242		return 0;
 243
 244	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
 245	src_x = plane_state->uapi.src.x1 >> 16;
 246	src_y = plane_state->uapi.src.y1 >> 16;
 247
 248	/* Undocumented hardware limit on i965/g4x/vlv/chv */
 249	if (HAS_GMCH(dev_priv) && fb->format->cpp[0] == 8 && src_w > 2048)
 250		return -EINVAL;
 251
 252	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
 253
 254	if (DISPLAY_VER(dev_priv) >= 4)
 255		offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
 256							    plane_state, 0);
 257	else
 258		offset = 0;
 259
 260	/*
 261	 * When using an X-tiled surface the plane starts to
 262	 * misbehave if the x offset + width exceeds the stride.
 263	 * hsw/bdw: underrun galore
 264	 * ilk/snb/ivb: wrap to the next tile row mid scanout
 265	 * i965/g4x: so far appear immune to this
 266	 * vlv/chv: TODO check
 267	 *
 268	 * Linear surfaces seem to work just fine, even on hsw/bdw
 269	 * despite them not using the linear offset anymore.
 270	 */
 271	if (DISPLAY_VER(dev_priv) >= 4 && fb->modifier == I915_FORMAT_MOD_X_TILED) {
 272		u32 alignment = intel_surf_alignment(fb, 0);
 273		int cpp = fb->format->cpp[0];
 274
 275		while ((src_x + src_w) * cpp > plane_state->view.color_plane[0].stride) {
 276			if (offset == 0) {
 277				drm_dbg_kms(&dev_priv->drm,
 278					    "Unable to find suitable display surface offset due to X-tiling\n");
 279				return -EINVAL;
 280			}
 281
 282			offset = intel_plane_adjust_aligned_offset(&src_x, &src_y, plane_state, 0,
 283								   offset, offset - alignment);
 284		}
 285	}
 286
 287	/*
 288	 * Put the final coordinates back so that the src
 289	 * coordinate checks will see the right values.
 290	 */
 291	drm_rect_translate_to(&plane_state->uapi.src,
 292			      src_x << 16, src_y << 16);
 293
 294	/* HSW/BDW do this automagically in hardware */
 295	if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
 296		unsigned int rotation = plane_state->hw.rotation;
 297		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
 298		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
 299
 300		if (rotation & DRM_MODE_ROTATE_180) {
 301			src_x += src_w - 1;
 302			src_y += src_h - 1;
 303		} else if (rotation & DRM_MODE_REFLECT_X) {
 304			src_x += src_w - 1;
 305		}
 306	}
 307
 308	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
 309		drm_WARN_ON(&dev_priv->drm, src_x > 8191 || src_y > 4095);
 310	} else if (DISPLAY_VER(dev_priv) >= 4 &&
 311		   fb->modifier == I915_FORMAT_MOD_X_TILED) {
 312		drm_WARN_ON(&dev_priv->drm, src_x > 4095 || src_y > 4095);
 313	}
 314
 315	plane_state->view.color_plane[0].offset = offset;
 316	plane_state->view.color_plane[0].x = src_x;
 317	plane_state->view.color_plane[0].y = src_y;
 318
 319	return 0;
 320}
 321
 322static int
 323i9xx_plane_check(struct intel_crtc_state *crtc_state,
 324		 struct intel_plane_state *plane_state)
 325{
 326	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
 327	int ret;
 328
 329	ret = chv_plane_check_rotation(plane_state);
 330	if (ret)
 331		return ret;
 332
 333	ret = intel_atomic_plane_check_clipping(plane_state, crtc_state,
 334						DRM_PLANE_HELPER_NO_SCALING,
 335						DRM_PLANE_HELPER_NO_SCALING,
 336						i9xx_plane_has_windowing(plane));
 337	if (ret)
 338		return ret;
 339
 340	ret = i9xx_check_plane_surface(plane_state);
 341	if (ret)
 342		return ret;
 343
 344	if (!plane_state->uapi.visible)
 345		return 0;
 346
 347	ret = intel_plane_check_src_coordinates(plane_state);
 348	if (ret)
 349		return ret;
 350
 351	plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
 352
 353	return 0;
 354}
 355
 356static u32 i9xx_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
 357{
 358	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
 359	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
 360	u32 dspcntr = 0;
 361
 362	if (crtc_state->gamma_enable)
 363		dspcntr |= DISPPLANE_GAMMA_ENABLE;
 364
 365	if (crtc_state->csc_enable)
 366		dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
 367
 368	if (DISPLAY_VER(dev_priv) < 5)
 369		dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
 370
 371	return dspcntr;
 372}
 373
 374static void i9xx_plane_ratio(const struct intel_crtc_state *crtc_state,
 375			     const struct intel_plane_state *plane_state,
 376			     unsigned int *num, unsigned int *den)
 377{
 378	const struct drm_framebuffer *fb = plane_state->hw.fb;
 379	unsigned int cpp = fb->format->cpp[0];
 380
 381	/*
 382	 * g4x bspec says 64bpp pixel rate can't exceed 80%
 383	 * of cdclk when the sprite plane is enabled on the
 384	 * same pipe. ilk/snb bspec says 64bpp pixel rate is
 385	 * never allowed to exceed 80% of cdclk. Let's just go
 386	 * with the ilk/snb limit always.
 387	 */
 388	if (cpp == 8) {
 389		*num = 10;
 390		*den = 8;
 391	} else {
 392		*num = 1;
 393		*den = 1;
 394	}
 395}
 396
 397static int i9xx_plane_min_cdclk(const struct intel_crtc_state *crtc_state,
 398				const struct intel_plane_state *plane_state)
 399{
 400	unsigned int pixel_rate;
 401	unsigned int num, den;
 402
 403	/*
 404	 * Note that crtc_state->pixel_rate accounts for both
 405	 * horizontal and vertical panel fitter downscaling factors.
 406	 * Pre-HSW bspec tells us to only consider the horizontal
 407	 * downscaling factor here. We ignore that and just consider
 408	 * both for simplicity.
 409	 */
 410	pixel_rate = crtc_state->pixel_rate;
 411
 412	i9xx_plane_ratio(crtc_state, plane_state, &num, &den);
 413
 414	/* two pixels per clock with double wide pipe */
 415	if (crtc_state->double_wide)
 416		den *= 2;
 417
 418	return DIV_ROUND_UP(pixel_rate * num, den);
 419}
 420
 421static void i9xx_update_plane(struct intel_plane *plane,
 422			      const struct intel_crtc_state *crtc_state,
 423			      const struct intel_plane_state *plane_state)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 424{
 425	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 426	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 427	u32 linear_offset;
 428	int x = plane_state->view.color_plane[0].x;
 429	int y = plane_state->view.color_plane[0].y;
 430	int crtc_x = plane_state->uapi.dst.x1;
 431	int crtc_y = plane_state->uapi.dst.y1;
 432	int crtc_w = drm_rect_width(&plane_state->uapi.dst);
 433	int crtc_h = drm_rect_height(&plane_state->uapi.dst);
 434	unsigned long irqflags;
 435	u32 dspaddr_offset;
 436	u32 dspcntr;
 437
 438	dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
 439
 440	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
 441
 442	if (DISPLAY_VER(dev_priv) >= 4)
 443		dspaddr_offset = plane_state->view.color_plane[0].offset;
 444	else
 445		dspaddr_offset = linear_offset;
 446
 447	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
 448
 449	intel_de_write_fw(dev_priv, DSPSTRIDE(i9xx_plane),
 450			  plane_state->view.color_plane[0].stride);
 
 451
 452	if (DISPLAY_VER(dev_priv) < 4) {
 453		/*
 454		 * PLANE_A doesn't actually have a full window
 455		 * generator but let's assume we still need to
 456		 * program whatever is there.
 457		 */
 458		intel_de_write_fw(dev_priv, DSPPOS(i9xx_plane),
 459				  (crtc_y << 16) | crtc_x);
 460		intel_de_write_fw(dev_priv, DSPSIZE(i9xx_plane),
 461				  ((crtc_h - 1) << 16) | (crtc_w - 1));
 462	} else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
 463		intel_de_write_fw(dev_priv, PRIMPOS(i9xx_plane),
 464				  (crtc_y << 16) | crtc_x);
 465		intel_de_write_fw(dev_priv, PRIMSIZE(i9xx_plane),
 466				  ((crtc_h - 1) << 16) | (crtc_w - 1));
 467		intel_de_write_fw(dev_priv, PRIMCNSTALPHA(i9xx_plane), 0);
 468	}
 469
 470	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
 471		intel_de_write_fw(dev_priv, DSPOFFSET(i9xx_plane),
 472				  (y << 16) | x);
 473	} else if (DISPLAY_VER(dev_priv) >= 4) {
 474		intel_de_write_fw(dev_priv, DSPLINOFF(i9xx_plane),
 475				  linear_offset);
 476		intel_de_write_fw(dev_priv, DSPTILEOFF(i9xx_plane),
 477				  (y << 16) | x);
 478	}
 479
 480	/*
 481	 * The control register self-arms if the plane was previously
 482	 * disabled. Try to make the plane enable atomic by writing
 483	 * the control register just before the surface register.
 484	 */
 485	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
 
 486	if (DISPLAY_VER(dev_priv) >= 4)
 487		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
 488				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
 489	else
 490		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
 491				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
 
 492
 493	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
 
 
 
 
 
 
 
 
 
 
 
 494}
 495
 496static void i9xx_disable_plane(struct intel_plane *plane,
 497			       const struct intel_crtc_state *crtc_state)
 498{
 499	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 500	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 501	unsigned long irqflags;
 502	u32 dspcntr;
 503
 504	/*
 505	 * DSPCNTR pipe gamma enable on g4x+ and pipe csc
 506	 * enable on ilk+ affect the pipe bottom color as
 507	 * well, so we must configure them even if the plane
 508	 * is disabled.
 509	 *
 510	 * On pre-g4x there is no way to gamma correct the
 511	 * pipe bottom color but we'll keep on doing this
 512	 * anyway so that the crtc state readout works correctly.
 513	 */
 514	dspcntr = i9xx_plane_ctl_crtc(crtc_state);
 515
 516	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
 517
 518	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
 
 519	if (DISPLAY_VER(dev_priv) >= 4)
 520		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane), 0);
 521	else
 522		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane), 0);
 523
 524	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
 525}
 526
 527static void
 528g4x_primary_async_flip(struct intel_plane *plane,
 529		       const struct intel_crtc_state *crtc_state,
 530		       const struct intel_plane_state *plane_state,
 531		       bool async_flip)
 532{
 533	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 534	u32 dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
 535	u32 dspaddr_offset = plane_state->view.color_plane[0].offset;
 536	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 537	unsigned long irqflags;
 538
 539	if (async_flip)
 540		dspcntr |= DISPPLANE_ASYNC_FLIP;
 541
 542	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
 543	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
 
 544	intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
 545			  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
 546	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
 547}
 548
 549static void
 550vlv_primary_async_flip(struct intel_plane *plane,
 551		       const struct intel_crtc_state *crtc_state,
 552		       const struct intel_plane_state *plane_state,
 553		       bool async_flip)
 554{
 555	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 556	u32 dspaddr_offset = plane_state->view.color_plane[0].offset;
 557	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 558	unsigned long irqflags;
 559
 560	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
 561	intel_de_write_fw(dev_priv, DSPADDR_VLV(i9xx_plane),
 562			  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
 563	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
 564}
 565
 566static void
 567bdw_primary_enable_flip_done(struct intel_plane *plane)
 568{
 569	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 570	enum pipe pipe = plane->pipe;
 571
 572	spin_lock_irq(&i915->irq_lock);
 573	bdw_enable_pipe_irq(i915, pipe, GEN8_PIPE_PRIMARY_FLIP_DONE);
 574	spin_unlock_irq(&i915->irq_lock);
 575}
 576
 577static void
 578bdw_primary_disable_flip_done(struct intel_plane *plane)
 579{
 580	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 581	enum pipe pipe = plane->pipe;
 582
 583	spin_lock_irq(&i915->irq_lock);
 584	bdw_disable_pipe_irq(i915, pipe, GEN8_PIPE_PRIMARY_FLIP_DONE);
 585	spin_unlock_irq(&i915->irq_lock);
 586}
 587
 588static void
 589ivb_primary_enable_flip_done(struct intel_plane *plane)
 590{
 591	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 592
 593	spin_lock_irq(&i915->irq_lock);
 594	ilk_enable_display_irq(i915, DE_PLANE_FLIP_DONE_IVB(plane->i9xx_plane));
 595	spin_unlock_irq(&i915->irq_lock);
 596}
 597
 598static void
 599ivb_primary_disable_flip_done(struct intel_plane *plane)
 600{
 601	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 602
 603	spin_lock_irq(&i915->irq_lock);
 604	ilk_disable_display_irq(i915, DE_PLANE_FLIP_DONE_IVB(plane->i9xx_plane));
 605	spin_unlock_irq(&i915->irq_lock);
 606}
 607
 608static void
 609ilk_primary_enable_flip_done(struct intel_plane *plane)
 610{
 611	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 612
 613	spin_lock_irq(&i915->irq_lock);
 614	ilk_enable_display_irq(i915, DE_PLANE_FLIP_DONE(plane->i9xx_plane));
 615	spin_unlock_irq(&i915->irq_lock);
 616}
 617
 618static void
 619ilk_primary_disable_flip_done(struct intel_plane *plane)
 620{
 621	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 622
 623	spin_lock_irq(&i915->irq_lock);
 624	ilk_disable_display_irq(i915, DE_PLANE_FLIP_DONE(plane->i9xx_plane));
 625	spin_unlock_irq(&i915->irq_lock);
 626}
 627
 628static void
 629vlv_primary_enable_flip_done(struct intel_plane *plane)
 630{
 631	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 632	enum pipe pipe = plane->pipe;
 633
 634	spin_lock_irq(&i915->irq_lock);
 635	i915_enable_pipestat(i915, pipe, PLANE_FLIP_DONE_INT_STATUS_VLV);
 636	spin_unlock_irq(&i915->irq_lock);
 637}
 638
 639static void
 640vlv_primary_disable_flip_done(struct intel_plane *plane)
 641{
 642	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 643	enum pipe pipe = plane->pipe;
 644
 645	spin_lock_irq(&i915->irq_lock);
 646	i915_disable_pipestat(i915, pipe, PLANE_FLIP_DONE_INT_STATUS_VLV);
 647	spin_unlock_irq(&i915->irq_lock);
 648}
 649
 650static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
 651				    enum pipe *pipe)
 652{
 653	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 654	enum intel_display_power_domain power_domain;
 655	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 656	intel_wakeref_t wakeref;
 657	bool ret;
 658	u32 val;
 659
 660	/*
 661	 * Not 100% correct for planes that can move between pipes,
 662	 * but that's only the case for gen2-4 which don't have any
 663	 * display power wells.
 664	 */
 665	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
 666	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
 667	if (!wakeref)
 668		return false;
 669
 670	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
 671
 672	ret = val & DISPLAY_PLANE_ENABLE;
 673
 674	if (DISPLAY_VER(dev_priv) >= 5)
 675		*pipe = plane->pipe;
 676	else
 677		*pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
 678			DISPPLANE_SEL_PIPE_SHIFT;
 679
 680	intel_display_power_put(dev_priv, power_domain, wakeref);
 681
 682	return ret;
 683}
 684
 685static unsigned int
 686hsw_primary_max_stride(struct intel_plane *plane,
 687		       u32 pixel_format, u64 modifier,
 688		       unsigned int rotation)
 689{
 690	const struct drm_format_info *info = drm_format_info(pixel_format);
 691	int cpp = info->cpp[0];
 692
 693	/* Limit to 8k pixels to guarantee OFFSET.x doesn't get too big. */
 694	return min(8192 * cpp, 32 * 1024);
 695}
 696
 697static unsigned int
 698ilk_primary_max_stride(struct intel_plane *plane,
 699		       u32 pixel_format, u64 modifier,
 700		       unsigned int rotation)
 701{
 702	const struct drm_format_info *info = drm_format_info(pixel_format);
 703	int cpp = info->cpp[0];
 704
 705	/* Limit to 4k pixels to guarantee TILEOFF.x doesn't get too big. */
 706	if (modifier == I915_FORMAT_MOD_X_TILED)
 707		return min(4096 * cpp, 32 * 1024);
 708	else
 709		return 32 * 1024;
 710}
 711
 712unsigned int
 713i965_plane_max_stride(struct intel_plane *plane,
 714		      u32 pixel_format, u64 modifier,
 715		      unsigned int rotation)
 716{
 717	const struct drm_format_info *info = drm_format_info(pixel_format);
 718	int cpp = info->cpp[0];
 719
 720	/* Limit to 4k pixels to guarantee TILEOFF.x doesn't get too big. */
 721	if (modifier == I915_FORMAT_MOD_X_TILED)
 722		return min(4096 * cpp, 16 * 1024);
 723	else
 724		return 32 * 1024;
 725}
 726
 727static unsigned int
 728i9xx_plane_max_stride(struct intel_plane *plane,
 729		      u32 pixel_format, u64 modifier,
 730		      unsigned int rotation)
 731{
 732	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 733
 734	if (DISPLAY_VER(dev_priv) >= 3) {
 735		if (modifier == I915_FORMAT_MOD_X_TILED)
 736			return 8*1024;
 737		else
 738			return 16*1024;
 739	} else {
 740		if (plane->i9xx_plane == PLANE_C)
 741			return 4*1024;
 742		else
 743			return 8*1024;
 744	}
 745}
 746
 747static const struct drm_plane_funcs i965_plane_funcs = {
 748	.update_plane = drm_atomic_helper_update_plane,
 749	.disable_plane = drm_atomic_helper_disable_plane,
 750	.destroy = intel_plane_destroy,
 751	.atomic_duplicate_state = intel_plane_duplicate_state,
 752	.atomic_destroy_state = intel_plane_destroy_state,
 753	.format_mod_supported = i965_plane_format_mod_supported,
 754};
 755
 756static const struct drm_plane_funcs i8xx_plane_funcs = {
 757	.update_plane = drm_atomic_helper_update_plane,
 758	.disable_plane = drm_atomic_helper_disable_plane,
 759	.destroy = intel_plane_destroy,
 760	.atomic_duplicate_state = intel_plane_duplicate_state,
 761	.atomic_destroy_state = intel_plane_destroy_state,
 762	.format_mod_supported = i8xx_plane_format_mod_supported,
 763};
 764
 765struct intel_plane *
 766intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
 767{
 768	struct intel_plane *plane;
 769	const struct drm_plane_funcs *plane_funcs;
 770	unsigned int supported_rotations;
 
 771	const u32 *formats;
 772	int num_formats;
 773	int ret, zpos;
 774
 775	plane = intel_plane_alloc();
 776	if (IS_ERR(plane))
 777		return plane;
 778
 779	plane->pipe = pipe;
 780	/*
 781	 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
 782	 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
 783	 */
 784	if (HAS_FBC(dev_priv) && DISPLAY_VER(dev_priv) < 4 &&
 785	    INTEL_NUM_PIPES(dev_priv) == 2)
 786		plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
 787	else
 788		plane->i9xx_plane = (enum i9xx_plane_id) pipe;
 789	plane->id = PLANE_PRIMARY;
 790	plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
 791
 792	plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
 793	if (plane->has_fbc) {
 794		struct intel_fbc *fbc = &dev_priv->fbc;
 795
 796		fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
 797	}
 798
 799	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
 800		formats = vlv_primary_formats;
 801		num_formats = ARRAY_SIZE(vlv_primary_formats);
 802	} else if (DISPLAY_VER(dev_priv) >= 4) {
 803		/*
 804		 * WaFP16GammaEnabling:ivb
 805		 * "Workaround : When using the 64-bit format, the plane
 806		 *  output on each color channel has one quarter amplitude.
 807		 *  It can be brought up to full amplitude by using pipe
 808		 *  gamma correction or pipe color space conversion to
 809		 *  multiply the plane output by four."
 810		 *
 811		 * There is no dedicated plane gamma for the primary plane,
 812		 * and using the pipe gamma/csc could conflict with other
 813		 * planes, so we choose not to expose fp16 on IVB primary
 814		 * planes. HSW primary planes no longer have this problem.
 815		 */
 816		if (IS_IVYBRIDGE(dev_priv)) {
 817			formats = ivb_primary_formats;
 818			num_formats = ARRAY_SIZE(ivb_primary_formats);
 819		} else {
 820			formats = i965_primary_formats;
 821			num_formats = ARRAY_SIZE(i965_primary_formats);
 822		}
 823	} else {
 824		formats = i8xx_primary_formats;
 825		num_formats = ARRAY_SIZE(i8xx_primary_formats);
 826	}
 827
 828	if (DISPLAY_VER(dev_priv) >= 4)
 829		plane_funcs = &i965_plane_funcs;
 830	else
 831		plane_funcs = &i8xx_plane_funcs;
 832
 833	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
 834		plane->min_cdclk = vlv_plane_min_cdclk;
 835	else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
 836		plane->min_cdclk = hsw_plane_min_cdclk;
 837	else if (IS_IVYBRIDGE(dev_priv))
 838		plane->min_cdclk = ivb_plane_min_cdclk;
 839	else
 840		plane->min_cdclk = i9xx_plane_min_cdclk;
 841
 842	if (HAS_GMCH(dev_priv)) {
 843		if (DISPLAY_VER(dev_priv) >= 4)
 844			plane->max_stride = i965_plane_max_stride;
 845		else
 846			plane->max_stride = i9xx_plane_max_stride;
 847	} else {
 848		if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
 849			plane->max_stride = hsw_primary_max_stride;
 850		else
 851			plane->max_stride = ilk_primary_max_stride;
 852	}
 853
 854	plane->update_plane = i9xx_update_plane;
 855	plane->disable_plane = i9xx_disable_plane;
 
 
 
 
 
 856	plane->get_hw_state = i9xx_plane_get_hw_state;
 857	plane->check_plane = i9xx_plane_check;
 858
 859	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
 860		plane->async_flip = vlv_primary_async_flip;
 861		plane->enable_flip_done = vlv_primary_enable_flip_done;
 862		plane->disable_flip_done = vlv_primary_disable_flip_done;
 863	} else if (IS_BROADWELL(dev_priv)) {
 864		plane->need_async_flip_disable_wa = true;
 865		plane->async_flip = g4x_primary_async_flip;
 866		plane->enable_flip_done = bdw_primary_enable_flip_done;
 867		plane->disable_flip_done = bdw_primary_disable_flip_done;
 868	} else if (DISPLAY_VER(dev_priv) >= 7) {
 869		plane->async_flip = g4x_primary_async_flip;
 870		plane->enable_flip_done = ivb_primary_enable_flip_done;
 871		plane->disable_flip_done = ivb_primary_disable_flip_done;
 872	} else if (DISPLAY_VER(dev_priv) >= 5) {
 873		plane->async_flip = g4x_primary_async_flip;
 874		plane->enable_flip_done = ilk_primary_enable_flip_done;
 875		plane->disable_flip_done = ilk_primary_disable_flip_done;
 876	}
 877
 
 
 878	if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
 879		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
 880					       0, plane_funcs,
 881					       formats, num_formats,
 882					       i9xx_format_modifiers,
 883					       DRM_PLANE_TYPE_PRIMARY,
 884					       "primary %c", pipe_name(pipe));
 885	else
 886		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
 887					       0, plane_funcs,
 888					       formats, num_formats,
 889					       i9xx_format_modifiers,
 890					       DRM_PLANE_TYPE_PRIMARY,
 891					       "plane %c",
 892					       plane_name(plane->i9xx_plane));
 
 
 
 893	if (ret)
 894		goto fail;
 895
 896	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
 897		supported_rotations =
 898			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
 899			DRM_MODE_REFLECT_X;
 900	} else if (DISPLAY_VER(dev_priv) >= 4) {
 901		supported_rotations =
 902			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
 903	} else {
 904		supported_rotations = DRM_MODE_ROTATE_0;
 905	}
 906
 907	if (DISPLAY_VER(dev_priv) >= 4)
 908		drm_plane_create_rotation_property(&plane->base,
 909						   DRM_MODE_ROTATE_0,
 910						   supported_rotations);
 911
 912	zpos = 0;
 913	drm_plane_create_zpos_immutable_property(&plane->base, zpos);
 914
 915	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
 916
 917	return plane;
 918
 919fail:
 920	intel_plane_free(plane);
 921
 922	return ERR_PTR(ret);
 923}
 924
 925static int i9xx_format_to_fourcc(int format)
 926{
 927	switch (format) {
 928	case DISPPLANE_8BPP:
 929		return DRM_FORMAT_C8;
 930	case DISPPLANE_BGRA555:
 931		return DRM_FORMAT_ARGB1555;
 932	case DISPPLANE_BGRX555:
 933		return DRM_FORMAT_XRGB1555;
 934	case DISPPLANE_BGRX565:
 935		return DRM_FORMAT_RGB565;
 936	default:
 937	case DISPPLANE_BGRX888:
 938		return DRM_FORMAT_XRGB8888;
 939	case DISPPLANE_RGBX888:
 940		return DRM_FORMAT_XBGR8888;
 941	case DISPPLANE_BGRA888:
 942		return DRM_FORMAT_ARGB8888;
 943	case DISPPLANE_RGBA888:
 944		return DRM_FORMAT_ABGR8888;
 945	case DISPPLANE_BGRX101010:
 946		return DRM_FORMAT_XRGB2101010;
 947	case DISPPLANE_RGBX101010:
 948		return DRM_FORMAT_XBGR2101010;
 949	case DISPPLANE_BGRA101010:
 950		return DRM_FORMAT_ARGB2101010;
 951	case DISPPLANE_RGBA101010:
 952		return DRM_FORMAT_ABGR2101010;
 953	case DISPPLANE_RGBX161616:
 954		return DRM_FORMAT_XBGR16161616F;
 955	}
 956}
 957
 958void
 959i9xx_get_initial_plane_config(struct intel_crtc *crtc,
 960			      struct intel_initial_plane_config *plane_config)
 961{
 962	struct drm_device *dev = crtc->base.dev;
 963	struct drm_i915_private *dev_priv = to_i915(dev);
 964	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
 965	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 966	enum pipe pipe;
 967	u32 val, base, offset;
 968	int fourcc, pixel_format;
 969	unsigned int aligned_height;
 970	struct drm_framebuffer *fb;
 971	struct intel_framebuffer *intel_fb;
 972
 973	if (!plane->get_hw_state(plane, &pipe))
 974		return;
 975
 976	drm_WARN_ON(dev, pipe != crtc->pipe);
 977
 978	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
 979	if (!intel_fb) {
 980		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
 981		return;
 982	}
 983
 984	fb = &intel_fb->base;
 985
 986	fb->dev = dev;
 987
 988	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
 989
 990	if (DISPLAY_VER(dev_priv) >= 4) {
 991		if (val & DISPPLANE_TILED) {
 992			plane_config->tiling = I915_TILING_X;
 993			fb->modifier = I915_FORMAT_MOD_X_TILED;
 994		}
 995
 996		if (val & DISPPLANE_ROTATE_180)
 997			plane_config->rotation = DRM_MODE_ROTATE_180;
 998	}
 999
1000	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
1001	    val & DISPPLANE_MIRROR)
1002		plane_config->rotation |= DRM_MODE_REFLECT_X;
1003
1004	pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
1005	fourcc = i9xx_format_to_fourcc(pixel_format);
1006	fb->format = drm_format_info(fourcc);
1007
1008	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1009		offset = intel_de_read(dev_priv, DSPOFFSET(i9xx_plane));
1010		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
1011	} else if (DISPLAY_VER(dev_priv) >= 4) {
1012		if (plane_config->tiling)
1013			offset = intel_de_read(dev_priv,
1014					       DSPTILEOFF(i9xx_plane));
1015		else
1016			offset = intel_de_read(dev_priv,
1017					       DSPLINOFF(i9xx_plane));
1018		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
1019	} else {
1020		base = intel_de_read(dev_priv, DSPADDR(i9xx_plane));
1021	}
1022	plane_config->base = base;
1023
1024	val = intel_de_read(dev_priv, PIPESRC(pipe));
1025	fb->width = ((val >> 16) & 0xfff) + 1;
1026	fb->height = ((val >> 0) & 0xfff) + 1;
1027
1028	val = intel_de_read(dev_priv, DSPSTRIDE(i9xx_plane));
1029	fb->pitches[0] = val & 0xffffffc0;
1030
1031	aligned_height = intel_fb_align_height(fb, 0, fb->height);
1032
1033	plane_config->size = fb->pitches[0] * aligned_height;
1034
1035	drm_dbg_kms(&dev_priv->drm,
1036		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
1037		    crtc->base.name, plane->base.name, fb->width, fb->height,
1038		    fb->format->cpp[0] * 8, base, fb->pitches[0],
1039		    plane_config->size);
1040
1041	plane_config->fb = intel_fb;
1042}
v6.2
   1// SPDX-License-Identifier: MIT
   2/*
   3 * Copyright © 2020 Intel Corporation
   4 */
   5#include <linux/kernel.h>
   6
   7#include <drm/drm_atomic_helper.h>
   8#include <drm/drm_blend.h>
   9#include <drm/drm_fourcc.h>
 
  10
  11#include "i915_irq.h"
  12#include "i915_reg.h"
  13#include "i9xx_plane.h"
  14#include "intel_atomic.h"
  15#include "intel_atomic_plane.h"
  16#include "intel_de.h"
  17#include "intel_display_types.h"
  18#include "intel_fb.h"
  19#include "intel_fbc.h"
  20#include "intel_sprite.h"
 
  21
  22/* Primary plane formats for gen <= 3 */
  23static const u32 i8xx_primary_formats[] = {
  24	DRM_FORMAT_C8,
  25	DRM_FORMAT_XRGB1555,
  26	DRM_FORMAT_RGB565,
  27	DRM_FORMAT_XRGB8888,
  28};
  29
  30/* Primary plane formats for ivb (no fp16 due to hw issue) */
  31static const u32 ivb_primary_formats[] = {
  32	DRM_FORMAT_C8,
  33	DRM_FORMAT_RGB565,
  34	DRM_FORMAT_XRGB8888,
  35	DRM_FORMAT_XBGR8888,
  36	DRM_FORMAT_XRGB2101010,
  37	DRM_FORMAT_XBGR2101010,
  38};
  39
  40/* Primary plane formats for gen >= 4, except ivb */
  41static const u32 i965_primary_formats[] = {
  42	DRM_FORMAT_C8,
  43	DRM_FORMAT_RGB565,
  44	DRM_FORMAT_XRGB8888,
  45	DRM_FORMAT_XBGR8888,
  46	DRM_FORMAT_XRGB2101010,
  47	DRM_FORMAT_XBGR2101010,
  48	DRM_FORMAT_XBGR16161616F,
  49};
  50
  51/* Primary plane formats for vlv/chv */
  52static const u32 vlv_primary_formats[] = {
  53	DRM_FORMAT_C8,
  54	DRM_FORMAT_RGB565,
  55	DRM_FORMAT_XRGB8888,
  56	DRM_FORMAT_XBGR8888,
  57	DRM_FORMAT_ARGB8888,
  58	DRM_FORMAT_ABGR8888,
  59	DRM_FORMAT_XRGB2101010,
  60	DRM_FORMAT_XBGR2101010,
  61	DRM_FORMAT_ARGB2101010,
  62	DRM_FORMAT_ABGR2101010,
  63	DRM_FORMAT_XBGR16161616F,
  64};
  65
 
 
 
 
 
 
  66static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
  67					    u32 format, u64 modifier)
  68{
  69	if (!intel_fb_plane_supports_modifier(to_intel_plane(_plane), modifier))
 
 
 
 
  70		return false;
 
  71
  72	switch (format) {
  73	case DRM_FORMAT_C8:
  74	case DRM_FORMAT_RGB565:
  75	case DRM_FORMAT_XRGB1555:
  76	case DRM_FORMAT_XRGB8888:
  77		return modifier == DRM_FORMAT_MOD_LINEAR ||
  78			modifier == I915_FORMAT_MOD_X_TILED;
  79	default:
  80		return false;
  81	}
  82}
  83
  84static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
  85					    u32 format, u64 modifier)
  86{
  87	if (!intel_fb_plane_supports_modifier(to_intel_plane(_plane), modifier))
 
 
 
 
  88		return false;
 
  89
  90	switch (format) {
  91	case DRM_FORMAT_C8:
  92	case DRM_FORMAT_RGB565:
  93	case DRM_FORMAT_XRGB8888:
  94	case DRM_FORMAT_XBGR8888:
  95	case DRM_FORMAT_ARGB8888:
  96	case DRM_FORMAT_ABGR8888:
  97	case DRM_FORMAT_XRGB2101010:
  98	case DRM_FORMAT_XBGR2101010:
  99	case DRM_FORMAT_ARGB2101010:
 100	case DRM_FORMAT_ABGR2101010:
 101	case DRM_FORMAT_XBGR16161616F:
 102		return modifier == DRM_FORMAT_MOD_LINEAR ||
 103			modifier == I915_FORMAT_MOD_X_TILED;
 104	default:
 105		return false;
 106	}
 107}
 108
 109static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
 110			       enum i9xx_plane_id i9xx_plane)
 111{
 112	if (!HAS_FBC(dev_priv))
 113		return false;
 114
 115	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
 116		return i9xx_plane == PLANE_A; /* tied to pipe A */
 117	else if (IS_IVYBRIDGE(dev_priv))
 118		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
 119			i9xx_plane == PLANE_C;
 120	else if (DISPLAY_VER(dev_priv) >= 4)
 121		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
 122	else
 123		return i9xx_plane == PLANE_A;
 124}
 125
 126static struct intel_fbc *i9xx_plane_fbc(struct drm_i915_private *dev_priv,
 127					enum i9xx_plane_id i9xx_plane)
 128{
 129	if (i9xx_plane_has_fbc(dev_priv, i9xx_plane))
 130		return dev_priv->display.fbc[INTEL_FBC_A];
 131	else
 132		return NULL;
 133}
 134
 135static bool i9xx_plane_has_windowing(struct intel_plane *plane)
 136{
 137	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 138	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 139
 140	if (IS_CHERRYVIEW(dev_priv))
 141		return i9xx_plane == PLANE_B;
 142	else if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
 143		return false;
 144	else if (DISPLAY_VER(dev_priv) == 4)
 145		return i9xx_plane == PLANE_C;
 146	else
 147		return i9xx_plane == PLANE_B ||
 148			i9xx_plane == PLANE_C;
 149}
 150
 151static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
 152			  const struct intel_plane_state *plane_state)
 153{
 154	struct drm_i915_private *dev_priv =
 155		to_i915(plane_state->uapi.plane->dev);
 156	const struct drm_framebuffer *fb = plane_state->hw.fb;
 157	unsigned int rotation = plane_state->hw.rotation;
 158	u32 dspcntr;
 159
 160	dspcntr = DISP_ENABLE;
 161
 162	if (IS_G4X(dev_priv) || IS_IRONLAKE(dev_priv) ||
 163	    IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv))
 164		dspcntr |= DISP_TRICKLE_FEED_DISABLE;
 165
 166	switch (fb->format->format) {
 167	case DRM_FORMAT_C8:
 168		dspcntr |= DISP_FORMAT_8BPP;
 169		break;
 170	case DRM_FORMAT_XRGB1555:
 171		dspcntr |= DISP_FORMAT_BGRX555;
 172		break;
 173	case DRM_FORMAT_ARGB1555:
 174		dspcntr |= DISP_FORMAT_BGRA555;
 175		break;
 176	case DRM_FORMAT_RGB565:
 177		dspcntr |= DISP_FORMAT_BGRX565;
 178		break;
 179	case DRM_FORMAT_XRGB8888:
 180		dspcntr |= DISP_FORMAT_BGRX888;
 181		break;
 182	case DRM_FORMAT_XBGR8888:
 183		dspcntr |= DISP_FORMAT_RGBX888;
 184		break;
 185	case DRM_FORMAT_ARGB8888:
 186		dspcntr |= DISP_FORMAT_BGRA888;
 187		break;
 188	case DRM_FORMAT_ABGR8888:
 189		dspcntr |= DISP_FORMAT_RGBA888;
 190		break;
 191	case DRM_FORMAT_XRGB2101010:
 192		dspcntr |= DISP_FORMAT_BGRX101010;
 193		break;
 194	case DRM_FORMAT_XBGR2101010:
 195		dspcntr |= DISP_FORMAT_RGBX101010;
 196		break;
 197	case DRM_FORMAT_ARGB2101010:
 198		dspcntr |= DISP_FORMAT_BGRA101010;
 199		break;
 200	case DRM_FORMAT_ABGR2101010:
 201		dspcntr |= DISP_FORMAT_RGBA101010;
 202		break;
 203	case DRM_FORMAT_XBGR16161616F:
 204		dspcntr |= DISP_FORMAT_RGBX161616;
 205		break;
 206	default:
 207		MISSING_CASE(fb->format->format);
 208		return 0;
 209	}
 210
 211	if (DISPLAY_VER(dev_priv) >= 4 &&
 212	    fb->modifier == I915_FORMAT_MOD_X_TILED)
 213		dspcntr |= DISP_TILED;
 214
 215	if (rotation & DRM_MODE_ROTATE_180)
 216		dspcntr |= DISP_ROTATE_180;
 217
 218	if (rotation & DRM_MODE_REFLECT_X)
 219		dspcntr |= DISP_MIRROR;
 220
 221	return dspcntr;
 222}
 223
 224int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
 225{
 226	struct drm_i915_private *dev_priv =
 227		to_i915(plane_state->uapi.plane->dev);
 228	const struct drm_framebuffer *fb = plane_state->hw.fb;
 229	int src_x, src_y, src_w;
 230	u32 offset;
 231	int ret;
 232
 233	ret = intel_plane_compute_gtt(plane_state);
 234	if (ret)
 235		return ret;
 236
 237	if (!plane_state->uapi.visible)
 238		return 0;
 239
 240	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
 241	src_x = plane_state->uapi.src.x1 >> 16;
 242	src_y = plane_state->uapi.src.y1 >> 16;
 243
 244	/* Undocumented hardware limit on i965/g4x/vlv/chv */
 245	if (HAS_GMCH(dev_priv) && fb->format->cpp[0] == 8 && src_w > 2048)
 246		return -EINVAL;
 247
 248	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
 249
 250	if (DISPLAY_VER(dev_priv) >= 4)
 251		offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
 252							    plane_state, 0);
 253	else
 254		offset = 0;
 255
 256	/*
 257	 * When using an X-tiled surface the plane starts to
 258	 * misbehave if the x offset + width exceeds the stride.
 259	 * hsw/bdw: underrun galore
 260	 * ilk/snb/ivb: wrap to the next tile row mid scanout
 261	 * i965/g4x: so far appear immune to this
 262	 * vlv/chv: TODO check
 263	 *
 264	 * Linear surfaces seem to work just fine, even on hsw/bdw
 265	 * despite them not using the linear offset anymore.
 266	 */
 267	if (DISPLAY_VER(dev_priv) >= 4 && fb->modifier == I915_FORMAT_MOD_X_TILED) {
 268		u32 alignment = intel_surf_alignment(fb, 0);
 269		int cpp = fb->format->cpp[0];
 270
 271		while ((src_x + src_w) * cpp > plane_state->view.color_plane[0].mapping_stride) {
 272			if (offset == 0) {
 273				drm_dbg_kms(&dev_priv->drm,
 274					    "Unable to find suitable display surface offset due to X-tiling\n");
 275				return -EINVAL;
 276			}
 277
 278			offset = intel_plane_adjust_aligned_offset(&src_x, &src_y, plane_state, 0,
 279								   offset, offset - alignment);
 280		}
 281	}
 282
 283	/*
 284	 * Put the final coordinates back so that the src
 285	 * coordinate checks will see the right values.
 286	 */
 287	drm_rect_translate_to(&plane_state->uapi.src,
 288			      src_x << 16, src_y << 16);
 289
 290	/* HSW/BDW do this automagically in hardware */
 291	if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
 292		unsigned int rotation = plane_state->hw.rotation;
 293		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
 294		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
 295
 296		if (rotation & DRM_MODE_ROTATE_180) {
 297			src_x += src_w - 1;
 298			src_y += src_h - 1;
 299		} else if (rotation & DRM_MODE_REFLECT_X) {
 300			src_x += src_w - 1;
 301		}
 302	}
 303
 304	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
 305		drm_WARN_ON(&dev_priv->drm, src_x > 8191 || src_y > 4095);
 306	} else if (DISPLAY_VER(dev_priv) >= 4 &&
 307		   fb->modifier == I915_FORMAT_MOD_X_TILED) {
 308		drm_WARN_ON(&dev_priv->drm, src_x > 4095 || src_y > 4095);
 309	}
 310
 311	plane_state->view.color_plane[0].offset = offset;
 312	plane_state->view.color_plane[0].x = src_x;
 313	plane_state->view.color_plane[0].y = src_y;
 314
 315	return 0;
 316}
 317
 318static int
 319i9xx_plane_check(struct intel_crtc_state *crtc_state,
 320		 struct intel_plane_state *plane_state)
 321{
 322	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
 323	int ret;
 324
 325	ret = chv_plane_check_rotation(plane_state);
 326	if (ret)
 327		return ret;
 328
 329	ret = intel_atomic_plane_check_clipping(plane_state, crtc_state,
 330						DRM_PLANE_NO_SCALING,
 331						DRM_PLANE_NO_SCALING,
 332						i9xx_plane_has_windowing(plane));
 333	if (ret)
 334		return ret;
 335
 336	ret = i9xx_check_plane_surface(plane_state);
 337	if (ret)
 338		return ret;
 339
 340	if (!plane_state->uapi.visible)
 341		return 0;
 342
 343	ret = intel_plane_check_src_coordinates(plane_state);
 344	if (ret)
 345		return ret;
 346
 347	plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
 348
 349	return 0;
 350}
 351
 352static u32 i9xx_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
 353{
 354	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
 355	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
 356	u32 dspcntr = 0;
 357
 358	if (crtc_state->gamma_enable)
 359		dspcntr |= DISP_PIPE_GAMMA_ENABLE;
 360
 361	if (crtc_state->csc_enable)
 362		dspcntr |= DISP_PIPE_CSC_ENABLE;
 363
 364	if (DISPLAY_VER(dev_priv) < 5)
 365		dspcntr |= DISP_PIPE_SEL(crtc->pipe);
 366
 367	return dspcntr;
 368}
 369
 370static void i9xx_plane_ratio(const struct intel_crtc_state *crtc_state,
 371			     const struct intel_plane_state *plane_state,
 372			     unsigned int *num, unsigned int *den)
 373{
 374	const struct drm_framebuffer *fb = plane_state->hw.fb;
 375	unsigned int cpp = fb->format->cpp[0];
 376
 377	/*
 378	 * g4x bspec says 64bpp pixel rate can't exceed 80%
 379	 * of cdclk when the sprite plane is enabled on the
 380	 * same pipe. ilk/snb bspec says 64bpp pixel rate is
 381	 * never allowed to exceed 80% of cdclk. Let's just go
 382	 * with the ilk/snb limit always.
 383	 */
 384	if (cpp == 8) {
 385		*num = 10;
 386		*den = 8;
 387	} else {
 388		*num = 1;
 389		*den = 1;
 390	}
 391}
 392
 393static int i9xx_plane_min_cdclk(const struct intel_crtc_state *crtc_state,
 394				const struct intel_plane_state *plane_state)
 395{
 396	unsigned int pixel_rate;
 397	unsigned int num, den;
 398
 399	/*
 400	 * Note that crtc_state->pixel_rate accounts for both
 401	 * horizontal and vertical panel fitter downscaling factors.
 402	 * Pre-HSW bspec tells us to only consider the horizontal
 403	 * downscaling factor here. We ignore that and just consider
 404	 * both for simplicity.
 405	 */
 406	pixel_rate = crtc_state->pixel_rate;
 407
 408	i9xx_plane_ratio(crtc_state, plane_state, &num, &den);
 409
 410	/* two pixels per clock with double wide pipe */
 411	if (crtc_state->double_wide)
 412		den *= 2;
 413
 414	return DIV_ROUND_UP(pixel_rate * num, den);
 415}
 416
 417static void i9xx_plane_update_noarm(struct intel_plane *plane,
 418				    const struct intel_crtc_state *crtc_state,
 419				    const struct intel_plane_state *plane_state)
 420{
 421	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 422	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 423
 424	intel_de_write_fw(dev_priv, DSPSTRIDE(i9xx_plane),
 425			  plane_state->view.color_plane[0].mapping_stride);
 426
 427	if (DISPLAY_VER(dev_priv) < 4) {
 428		int crtc_x = plane_state->uapi.dst.x1;
 429		int crtc_y = plane_state->uapi.dst.y1;
 430		int crtc_w = drm_rect_width(&plane_state->uapi.dst);
 431		int crtc_h = drm_rect_height(&plane_state->uapi.dst);
 432
 433		/*
 434		 * PLANE_A doesn't actually have a full window
 435		 * generator but let's assume we still need to
 436		 * program whatever is there.
 437		 */
 438		intel_de_write_fw(dev_priv, DSPPOS(i9xx_plane),
 439				  DISP_POS_Y(crtc_y) | DISP_POS_X(crtc_x));
 440		intel_de_write_fw(dev_priv, DSPSIZE(i9xx_plane),
 441				  DISP_HEIGHT(crtc_h - 1) | DISP_WIDTH(crtc_w - 1));
 442	}
 443}
 444
 445static void i9xx_plane_update_arm(struct intel_plane *plane,
 446				  const struct intel_crtc_state *crtc_state,
 447				  const struct intel_plane_state *plane_state)
 448{
 449	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 450	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 
 451	int x = plane_state->view.color_plane[0].x;
 452	int y = plane_state->view.color_plane[0].y;
 453	u32 dspcntr, dspaddr_offset, linear_offset;
 
 
 
 
 
 
 454
 455	dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
 456
 457	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
 458
 459	if (DISPLAY_VER(dev_priv) >= 4)
 460		dspaddr_offset = plane_state->view.color_plane[0].offset;
 461	else
 462		dspaddr_offset = linear_offset;
 463
 464	if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
 465		int crtc_x = plane_state->uapi.dst.x1;
 466		int crtc_y = plane_state->uapi.dst.y1;
 467		int crtc_w = drm_rect_width(&plane_state->uapi.dst);
 468		int crtc_h = drm_rect_height(&plane_state->uapi.dst);
 469
 
 
 
 
 
 
 
 
 
 
 
 470		intel_de_write_fw(dev_priv, PRIMPOS(i9xx_plane),
 471				  PRIM_POS_Y(crtc_y) | PRIM_POS_X(crtc_x));
 472		intel_de_write_fw(dev_priv, PRIMSIZE(i9xx_plane),
 473				  PRIM_HEIGHT(crtc_h - 1) | PRIM_WIDTH(crtc_w - 1));
 474		intel_de_write_fw(dev_priv, PRIMCNSTALPHA(i9xx_plane), 0);
 475	}
 476
 477	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
 478		intel_de_write_fw(dev_priv, DSPOFFSET(i9xx_plane),
 479				  DISP_OFFSET_Y(y) | DISP_OFFSET_X(x));
 480	} else if (DISPLAY_VER(dev_priv) >= 4) {
 481		intel_de_write_fw(dev_priv, DSPLINOFF(i9xx_plane),
 482				  linear_offset);
 483		intel_de_write_fw(dev_priv, DSPTILEOFF(i9xx_plane),
 484				  DISP_OFFSET_Y(y) | DISP_OFFSET_X(x));
 485	}
 486
 487	/*
 488	 * The control register self-arms if the plane was previously
 489	 * disabled. Try to make the plane enable atomic by writing
 490	 * the control register just before the surface register.
 491	 */
 492	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
 493
 494	if (DISPLAY_VER(dev_priv) >= 4)
 495		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
 496				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
 497	else
 498		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
 499				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
 500}
 501
 502static void i830_plane_update_arm(struct intel_plane *plane,
 503				  const struct intel_crtc_state *crtc_state,
 504				  const struct intel_plane_state *plane_state)
 505{
 506	/*
 507	 * On i830/i845 all registers are self-arming [ALM040].
 508	 *
 509	 * Additional breakage on i830 causes register reads to return
 510	 * the last latched value instead of the last written value [ALM026].
 511	 */
 512	i9xx_plane_update_noarm(plane, crtc_state, plane_state);
 513	i9xx_plane_update_arm(plane, crtc_state, plane_state);
 514}
 515
 516static void i9xx_plane_disable_arm(struct intel_plane *plane,
 517				   const struct intel_crtc_state *crtc_state)
 518{
 519	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 520	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 
 521	u32 dspcntr;
 522
 523	/*
 524	 * DSPCNTR pipe gamma enable on g4x+ and pipe csc
 525	 * enable on ilk+ affect the pipe bottom color as
 526	 * well, so we must configure them even if the plane
 527	 * is disabled.
 528	 *
 529	 * On pre-g4x there is no way to gamma correct the
 530	 * pipe bottom color but we'll keep on doing this
 531	 * anyway so that the crtc state readout works correctly.
 532	 */
 533	dspcntr = i9xx_plane_ctl_crtc(crtc_state);
 534
 
 
 535	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
 536
 537	if (DISPLAY_VER(dev_priv) >= 4)
 538		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane), 0);
 539	else
 540		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane), 0);
 
 
 541}
 542
 543static void
 544g4x_primary_async_flip(struct intel_plane *plane,
 545		       const struct intel_crtc_state *crtc_state,
 546		       const struct intel_plane_state *plane_state,
 547		       bool async_flip)
 548{
 549	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 550	u32 dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
 551	u32 dspaddr_offset = plane_state->view.color_plane[0].offset;
 552	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 
 553
 554	if (async_flip)
 555		dspcntr |= DISP_ASYNC_FLIP;
 556
 
 557	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
 558
 559	intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
 560			  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
 
 561}
 562
 563static void
 564vlv_primary_async_flip(struct intel_plane *plane,
 565		       const struct intel_crtc_state *crtc_state,
 566		       const struct intel_plane_state *plane_state,
 567		       bool async_flip)
 568{
 569	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 570	u32 dspaddr_offset = plane_state->view.color_plane[0].offset;
 571	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 
 572
 
 573	intel_de_write_fw(dev_priv, DSPADDR_VLV(i9xx_plane),
 574			  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
 
 575}
 576
 577static void
 578bdw_primary_enable_flip_done(struct intel_plane *plane)
 579{
 580	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 581	enum pipe pipe = plane->pipe;
 582
 583	spin_lock_irq(&i915->irq_lock);
 584	bdw_enable_pipe_irq(i915, pipe, GEN8_PIPE_PRIMARY_FLIP_DONE);
 585	spin_unlock_irq(&i915->irq_lock);
 586}
 587
 588static void
 589bdw_primary_disable_flip_done(struct intel_plane *plane)
 590{
 591	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 592	enum pipe pipe = plane->pipe;
 593
 594	spin_lock_irq(&i915->irq_lock);
 595	bdw_disable_pipe_irq(i915, pipe, GEN8_PIPE_PRIMARY_FLIP_DONE);
 596	spin_unlock_irq(&i915->irq_lock);
 597}
 598
 599static void
 600ivb_primary_enable_flip_done(struct intel_plane *plane)
 601{
 602	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 603
 604	spin_lock_irq(&i915->irq_lock);
 605	ilk_enable_display_irq(i915, DE_PLANE_FLIP_DONE_IVB(plane->i9xx_plane));
 606	spin_unlock_irq(&i915->irq_lock);
 607}
 608
 609static void
 610ivb_primary_disable_flip_done(struct intel_plane *plane)
 611{
 612	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 613
 614	spin_lock_irq(&i915->irq_lock);
 615	ilk_disable_display_irq(i915, DE_PLANE_FLIP_DONE_IVB(plane->i9xx_plane));
 616	spin_unlock_irq(&i915->irq_lock);
 617}
 618
 619static void
 620ilk_primary_enable_flip_done(struct intel_plane *plane)
 621{
 622	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 623
 624	spin_lock_irq(&i915->irq_lock);
 625	ilk_enable_display_irq(i915, DE_PLANE_FLIP_DONE(plane->i9xx_plane));
 626	spin_unlock_irq(&i915->irq_lock);
 627}
 628
 629static void
 630ilk_primary_disable_flip_done(struct intel_plane *plane)
 631{
 632	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 633
 634	spin_lock_irq(&i915->irq_lock);
 635	ilk_disable_display_irq(i915, DE_PLANE_FLIP_DONE(plane->i9xx_plane));
 636	spin_unlock_irq(&i915->irq_lock);
 637}
 638
 639static void
 640vlv_primary_enable_flip_done(struct intel_plane *plane)
 641{
 642	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 643	enum pipe pipe = plane->pipe;
 644
 645	spin_lock_irq(&i915->irq_lock);
 646	i915_enable_pipestat(i915, pipe, PLANE_FLIP_DONE_INT_STATUS_VLV);
 647	spin_unlock_irq(&i915->irq_lock);
 648}
 649
 650static void
 651vlv_primary_disable_flip_done(struct intel_plane *plane)
 652{
 653	struct drm_i915_private *i915 = to_i915(plane->base.dev);
 654	enum pipe pipe = plane->pipe;
 655
 656	spin_lock_irq(&i915->irq_lock);
 657	i915_disable_pipestat(i915, pipe, PLANE_FLIP_DONE_INT_STATUS_VLV);
 658	spin_unlock_irq(&i915->irq_lock);
 659}
 660
 661static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
 662				    enum pipe *pipe)
 663{
 664	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 665	enum intel_display_power_domain power_domain;
 666	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 667	intel_wakeref_t wakeref;
 668	bool ret;
 669	u32 val;
 670
 671	/*
 672	 * Not 100% correct for planes that can move between pipes,
 673	 * but that's only the case for gen2-4 which don't have any
 674	 * display power wells.
 675	 */
 676	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
 677	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
 678	if (!wakeref)
 679		return false;
 680
 681	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
 682
 683	ret = val & DISP_ENABLE;
 684
 685	if (DISPLAY_VER(dev_priv) >= 5)
 686		*pipe = plane->pipe;
 687	else
 688		*pipe = REG_FIELD_GET(DISP_PIPE_SEL_MASK, val);
 
 689
 690	intel_display_power_put(dev_priv, power_domain, wakeref);
 691
 692	return ret;
 693}
 694
 695static unsigned int
 696hsw_primary_max_stride(struct intel_plane *plane,
 697		       u32 pixel_format, u64 modifier,
 698		       unsigned int rotation)
 699{
 700	const struct drm_format_info *info = drm_format_info(pixel_format);
 701	int cpp = info->cpp[0];
 702
 703	/* Limit to 8k pixels to guarantee OFFSET.x doesn't get too big. */
 704	return min(8192 * cpp, 32 * 1024);
 705}
 706
 707static unsigned int
 708ilk_primary_max_stride(struct intel_plane *plane,
 709		       u32 pixel_format, u64 modifier,
 710		       unsigned int rotation)
 711{
 712	const struct drm_format_info *info = drm_format_info(pixel_format);
 713	int cpp = info->cpp[0];
 714
 715	/* Limit to 4k pixels to guarantee TILEOFF.x doesn't get too big. */
 716	if (modifier == I915_FORMAT_MOD_X_TILED)
 717		return min(4096 * cpp, 32 * 1024);
 718	else
 719		return 32 * 1024;
 720}
 721
 722unsigned int
 723i965_plane_max_stride(struct intel_plane *plane,
 724		      u32 pixel_format, u64 modifier,
 725		      unsigned int rotation)
 726{
 727	const struct drm_format_info *info = drm_format_info(pixel_format);
 728	int cpp = info->cpp[0];
 729
 730	/* Limit to 4k pixels to guarantee TILEOFF.x doesn't get too big. */
 731	if (modifier == I915_FORMAT_MOD_X_TILED)
 732		return min(4096 * cpp, 16 * 1024);
 733	else
 734		return 32 * 1024;
 735}
 736
 737static unsigned int
 738i9xx_plane_max_stride(struct intel_plane *plane,
 739		      u32 pixel_format, u64 modifier,
 740		      unsigned int rotation)
 741{
 742	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
 743
 744	if (DISPLAY_VER(dev_priv) >= 3) {
 745		if (modifier == I915_FORMAT_MOD_X_TILED)
 746			return 8*1024;
 747		else
 748			return 16*1024;
 749	} else {
 750		if (plane->i9xx_plane == PLANE_C)
 751			return 4*1024;
 752		else
 753			return 8*1024;
 754	}
 755}
 756
 757static const struct drm_plane_funcs i965_plane_funcs = {
 758	.update_plane = drm_atomic_helper_update_plane,
 759	.disable_plane = drm_atomic_helper_disable_plane,
 760	.destroy = intel_plane_destroy,
 761	.atomic_duplicate_state = intel_plane_duplicate_state,
 762	.atomic_destroy_state = intel_plane_destroy_state,
 763	.format_mod_supported = i965_plane_format_mod_supported,
 764};
 765
 766static const struct drm_plane_funcs i8xx_plane_funcs = {
 767	.update_plane = drm_atomic_helper_update_plane,
 768	.disable_plane = drm_atomic_helper_disable_plane,
 769	.destroy = intel_plane_destroy,
 770	.atomic_duplicate_state = intel_plane_duplicate_state,
 771	.atomic_destroy_state = intel_plane_destroy_state,
 772	.format_mod_supported = i8xx_plane_format_mod_supported,
 773};
 774
 775struct intel_plane *
 776intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
 777{
 778	struct intel_plane *plane;
 779	const struct drm_plane_funcs *plane_funcs;
 780	unsigned int supported_rotations;
 781	const u64 *modifiers;
 782	const u32 *formats;
 783	int num_formats;
 784	int ret, zpos;
 785
 786	plane = intel_plane_alloc();
 787	if (IS_ERR(plane))
 788		return plane;
 789
 790	plane->pipe = pipe;
 791	/*
 792	 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
 793	 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
 794	 */
 795	if (HAS_FBC(dev_priv) && DISPLAY_VER(dev_priv) < 4 &&
 796	    INTEL_NUM_PIPES(dev_priv) == 2)
 797		plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
 798	else
 799		plane->i9xx_plane = (enum i9xx_plane_id) pipe;
 800	plane->id = PLANE_PRIMARY;
 801	plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
 802
 803	intel_fbc_add_plane(i9xx_plane_fbc(dev_priv, plane->i9xx_plane), plane);
 
 
 
 
 
 804
 805	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
 806		formats = vlv_primary_formats;
 807		num_formats = ARRAY_SIZE(vlv_primary_formats);
 808	} else if (DISPLAY_VER(dev_priv) >= 4) {
 809		/*
 810		 * WaFP16GammaEnabling:ivb
 811		 * "Workaround : When using the 64-bit format, the plane
 812		 *  output on each color channel has one quarter amplitude.
 813		 *  It can be brought up to full amplitude by using pipe
 814		 *  gamma correction or pipe color space conversion to
 815		 *  multiply the plane output by four."
 816		 *
 817		 * There is no dedicated plane gamma for the primary plane,
 818		 * and using the pipe gamma/csc could conflict with other
 819		 * planes, so we choose not to expose fp16 on IVB primary
 820		 * planes. HSW primary planes no longer have this problem.
 821		 */
 822		if (IS_IVYBRIDGE(dev_priv)) {
 823			formats = ivb_primary_formats;
 824			num_formats = ARRAY_SIZE(ivb_primary_formats);
 825		} else {
 826			formats = i965_primary_formats;
 827			num_formats = ARRAY_SIZE(i965_primary_formats);
 828		}
 829	} else {
 830		formats = i8xx_primary_formats;
 831		num_formats = ARRAY_SIZE(i8xx_primary_formats);
 832	}
 833
 834	if (DISPLAY_VER(dev_priv) >= 4)
 835		plane_funcs = &i965_plane_funcs;
 836	else
 837		plane_funcs = &i8xx_plane_funcs;
 838
 839	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
 840		plane->min_cdclk = vlv_plane_min_cdclk;
 841	else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
 842		plane->min_cdclk = hsw_plane_min_cdclk;
 843	else if (IS_IVYBRIDGE(dev_priv))
 844		plane->min_cdclk = ivb_plane_min_cdclk;
 845	else
 846		plane->min_cdclk = i9xx_plane_min_cdclk;
 847
 848	if (HAS_GMCH(dev_priv)) {
 849		if (DISPLAY_VER(dev_priv) >= 4)
 850			plane->max_stride = i965_plane_max_stride;
 851		else
 852			plane->max_stride = i9xx_plane_max_stride;
 853	} else {
 854		if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
 855			plane->max_stride = hsw_primary_max_stride;
 856		else
 857			plane->max_stride = ilk_primary_max_stride;
 858	}
 859
 860	if (IS_I830(dev_priv) || IS_I845G(dev_priv)) {
 861		plane->update_arm = i830_plane_update_arm;
 862	} else {
 863		plane->update_noarm = i9xx_plane_update_noarm;
 864		plane->update_arm = i9xx_plane_update_arm;
 865	}
 866	plane->disable_arm = i9xx_plane_disable_arm;
 867	plane->get_hw_state = i9xx_plane_get_hw_state;
 868	plane->check_plane = i9xx_plane_check;
 869
 870	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
 871		plane->async_flip = vlv_primary_async_flip;
 872		plane->enable_flip_done = vlv_primary_enable_flip_done;
 873		plane->disable_flip_done = vlv_primary_disable_flip_done;
 874	} else if (IS_BROADWELL(dev_priv)) {
 875		plane->need_async_flip_disable_wa = true;
 876		plane->async_flip = g4x_primary_async_flip;
 877		plane->enable_flip_done = bdw_primary_enable_flip_done;
 878		plane->disable_flip_done = bdw_primary_disable_flip_done;
 879	} else if (DISPLAY_VER(dev_priv) >= 7) {
 880		plane->async_flip = g4x_primary_async_flip;
 881		plane->enable_flip_done = ivb_primary_enable_flip_done;
 882		plane->disable_flip_done = ivb_primary_disable_flip_done;
 883	} else if (DISPLAY_VER(dev_priv) >= 5) {
 884		plane->async_flip = g4x_primary_async_flip;
 885		plane->enable_flip_done = ilk_primary_enable_flip_done;
 886		plane->disable_flip_done = ilk_primary_disable_flip_done;
 887	}
 888
 889	modifiers = intel_fb_plane_get_modifiers(dev_priv, INTEL_PLANE_CAP_TILING_X);
 890
 891	if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
 892		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
 893					       0, plane_funcs,
 894					       formats, num_formats,
 895					       modifiers,
 896					       DRM_PLANE_TYPE_PRIMARY,
 897					       "primary %c", pipe_name(pipe));
 898	else
 899		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
 900					       0, plane_funcs,
 901					       formats, num_formats,
 902					       modifiers,
 903					       DRM_PLANE_TYPE_PRIMARY,
 904					       "plane %c",
 905					       plane_name(plane->i9xx_plane));
 906
 907	kfree(modifiers);
 908
 909	if (ret)
 910		goto fail;
 911
 912	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
 913		supported_rotations =
 914			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
 915			DRM_MODE_REFLECT_X;
 916	} else if (DISPLAY_VER(dev_priv) >= 4) {
 917		supported_rotations =
 918			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
 919	} else {
 920		supported_rotations = DRM_MODE_ROTATE_0;
 921	}
 922
 923	if (DISPLAY_VER(dev_priv) >= 4)
 924		drm_plane_create_rotation_property(&plane->base,
 925						   DRM_MODE_ROTATE_0,
 926						   supported_rotations);
 927
 928	zpos = 0;
 929	drm_plane_create_zpos_immutable_property(&plane->base, zpos);
 930
 931	intel_plane_helper_add(plane);
 932
 933	return plane;
 934
 935fail:
 936	intel_plane_free(plane);
 937
 938	return ERR_PTR(ret);
 939}
 940
 941static int i9xx_format_to_fourcc(int format)
 942{
 943	switch (format) {
 944	case DISP_FORMAT_8BPP:
 945		return DRM_FORMAT_C8;
 946	case DISP_FORMAT_BGRA555:
 947		return DRM_FORMAT_ARGB1555;
 948	case DISP_FORMAT_BGRX555:
 949		return DRM_FORMAT_XRGB1555;
 950	case DISP_FORMAT_BGRX565:
 951		return DRM_FORMAT_RGB565;
 952	default:
 953	case DISP_FORMAT_BGRX888:
 954		return DRM_FORMAT_XRGB8888;
 955	case DISP_FORMAT_RGBX888:
 956		return DRM_FORMAT_XBGR8888;
 957	case DISP_FORMAT_BGRA888:
 958		return DRM_FORMAT_ARGB8888;
 959	case DISP_FORMAT_RGBA888:
 960		return DRM_FORMAT_ABGR8888;
 961	case DISP_FORMAT_BGRX101010:
 962		return DRM_FORMAT_XRGB2101010;
 963	case DISP_FORMAT_RGBX101010:
 964		return DRM_FORMAT_XBGR2101010;
 965	case DISP_FORMAT_BGRA101010:
 966		return DRM_FORMAT_ARGB2101010;
 967	case DISP_FORMAT_RGBA101010:
 968		return DRM_FORMAT_ABGR2101010;
 969	case DISP_FORMAT_RGBX161616:
 970		return DRM_FORMAT_XBGR16161616F;
 971	}
 972}
 973
 974void
 975i9xx_get_initial_plane_config(struct intel_crtc *crtc,
 976			      struct intel_initial_plane_config *plane_config)
 977{
 978	struct drm_device *dev = crtc->base.dev;
 979	struct drm_i915_private *dev_priv = to_i915(dev);
 980	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
 981	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
 982	enum pipe pipe;
 983	u32 val, base, offset;
 984	int fourcc, pixel_format;
 985	unsigned int aligned_height;
 986	struct drm_framebuffer *fb;
 987	struct intel_framebuffer *intel_fb;
 988
 989	if (!plane->get_hw_state(plane, &pipe))
 990		return;
 991
 992	drm_WARN_ON(dev, pipe != crtc->pipe);
 993
 994	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
 995	if (!intel_fb) {
 996		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
 997		return;
 998	}
 999
1000	fb = &intel_fb->base;
1001
1002	fb->dev = dev;
1003
1004	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
1005
1006	if (DISPLAY_VER(dev_priv) >= 4) {
1007		if (val & DISP_TILED) {
1008			plane_config->tiling = I915_TILING_X;
1009			fb->modifier = I915_FORMAT_MOD_X_TILED;
1010		}
1011
1012		if (val & DISP_ROTATE_180)
1013			plane_config->rotation = DRM_MODE_ROTATE_180;
1014	}
1015
1016	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
1017	    val & DISP_MIRROR)
1018		plane_config->rotation |= DRM_MODE_REFLECT_X;
1019
1020	pixel_format = val & DISP_FORMAT_MASK;
1021	fourcc = i9xx_format_to_fourcc(pixel_format);
1022	fb->format = drm_format_info(fourcc);
1023
1024	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1025		offset = intel_de_read(dev_priv, DSPOFFSET(i9xx_plane));
1026		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & DISP_ADDR_MASK;
1027	} else if (DISPLAY_VER(dev_priv) >= 4) {
1028		if (plane_config->tiling)
1029			offset = intel_de_read(dev_priv,
1030					       DSPTILEOFF(i9xx_plane));
1031		else
1032			offset = intel_de_read(dev_priv,
1033					       DSPLINOFF(i9xx_plane));
1034		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & DISP_ADDR_MASK;
1035	} else {
1036		base = intel_de_read(dev_priv, DSPADDR(i9xx_plane));
1037	}
1038	plane_config->base = base;
1039
1040	val = intel_de_read(dev_priv, PIPESRC(pipe));
1041	fb->width = REG_FIELD_GET(PIPESRC_WIDTH_MASK, val) + 1;
1042	fb->height = REG_FIELD_GET(PIPESRC_HEIGHT_MASK, val) + 1;
1043
1044	val = intel_de_read(dev_priv, DSPSTRIDE(i9xx_plane));
1045	fb->pitches[0] = val & 0xffffffc0;
1046
1047	aligned_height = intel_fb_align_height(fb, 0, fb->height);
1048
1049	plane_config->size = fb->pitches[0] * aligned_height;
1050
1051	drm_dbg_kms(&dev_priv->drm,
1052		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
1053		    crtc->base.name, plane->base.name, fb->width, fb->height,
1054		    fb->format->cpp[0] * 8, base, fb->pitches[0],
1055		    plane_config->size);
1056
1057	plane_config->fb = intel_fb;
1058}