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