xref: /linux/drivers/gpu/drm/i915/display/intel_plane.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * DOC: atomic plane helpers
26  *
27  * The functions here are used by the atomic plane helper functions to
28  * implement legacy plane updates (i.e., drm_plane->update_plane() and
29  * drm_plane->disable_plane()).  This allows plane updates to use the
30  * atomic state infrastructure and perform plane updates as separate
31  * prepare/check/commit/cleanup steps.
32  */
33 
34 #include <linux/dma-fence-chain.h>
35 #include <linux/dma-resv.h>
36 #include <linux/iosys-map.h>
37 
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_blend.h>
40 #include <drm/drm_cache.h>
41 #include <drm/drm_damage_helper.h>
42 #include <drm/drm_fourcc.h>
43 #include <drm/drm_gem.h>
44 #include <drm/drm_gem_atomic_helper.h>
45 #include <drm/drm_panic.h>
46 #include <drm/drm_print.h>
47 #include <drm/intel/display_parent_interface.h>
48 
49 #include "i9xx_plane_regs.h"
50 #include "intel_cdclk.h"
51 #include "intel_cursor.h"
52 #include "intel_colorop.h"
53 #include "intel_display_rps.h"
54 #include "intel_display_trace.h"
55 #include "intel_display_types.h"
56 #include "intel_fb.h"
57 #include "intel_fbdev.h"
58 #include "intel_parent.h"
59 #include "intel_plane.h"
60 #include "intel_psr.h"
61 #include "skl_scaler.h"
62 #include "skl_universal_plane.h"
63 #include "skl_watermark.h"
64 
65 static void intel_plane_state_reset(struct intel_plane_state *plane_state,
66 				    struct intel_plane *plane)
67 {
68 	memset(plane_state, 0, sizeof(*plane_state));
69 
70 	__drm_atomic_helper_plane_state_init(&plane_state->uapi, &plane->base);
71 
72 	plane_state->scaler_id = -1;
73 	plane_state->fence_id = -1;
74 }
75 
76 struct intel_plane *intel_plane_alloc(void)
77 {
78 	struct intel_plane_state *plane_state;
79 	struct intel_plane *plane;
80 
81 	plane = kzalloc_obj(*plane);
82 	if (!plane)
83 		return ERR_PTR(-ENOMEM);
84 
85 	plane_state = kzalloc_obj(*plane_state);
86 	if (!plane_state) {
87 		kfree(plane);
88 		return ERR_PTR(-ENOMEM);
89 	}
90 
91 	intel_plane_state_reset(plane_state, plane);
92 
93 	plane->base.state = &plane_state->uapi;
94 
95 	return plane;
96 }
97 
98 void intel_plane_free(struct intel_plane *plane)
99 {
100 	intel_plane_destroy_state(&plane->base, plane->base.state);
101 	kfree(plane);
102 }
103 
104 /**
105  * intel_plane_destroy - destroy a plane
106  * @plane: plane to destroy
107  *
108  * Common destruction function for all types of planes (primary, cursor,
109  * sprite).
110  */
111 void intel_plane_destroy(struct drm_plane *plane)
112 {
113 	drm_plane_cleanup(plane);
114 	kfree(to_intel_plane(plane));
115 }
116 
117 /**
118  * intel_plane_duplicate_state - duplicate plane state
119  * @plane: drm plane
120  *
121  * Allocates and returns a copy of the plane state (both common and
122  * Intel-specific) for the specified plane.
123  *
124  * Returns: The newly allocated plane state, or NULL on failure.
125  */
126 struct drm_plane_state *
127 intel_plane_duplicate_state(struct drm_plane *plane)
128 {
129 	struct intel_plane_state *intel_state;
130 
131 	intel_state = to_intel_plane_state(plane->state);
132 	intel_state = kmemdup(intel_state, sizeof(*intel_state), GFP_KERNEL);
133 
134 	if (!intel_state)
135 		return NULL;
136 
137 	__drm_atomic_helper_plane_duplicate_state(plane, &intel_state->uapi);
138 
139 	intel_state->ggtt_vma = NULL;
140 	intel_state->dpt_vma = NULL;
141 	intel_state->fence_id = -1;
142 	intel_state->damage = DRM_RECT_INIT(0, 0, 0, 0);
143 	intel_state->psr2_sel_fetch_area = DRM_RECT_INIT(0, 0, 0, 0);
144 
145 	/* add reference to fb */
146 	if (intel_state->hw.fb)
147 		drm_framebuffer_get(intel_state->hw.fb);
148 
149 	if (intel_state->hw.degamma_lut)
150 		drm_property_blob_get(intel_state->hw.degamma_lut);
151 	if (intel_state->hw.gamma_lut)
152 		drm_property_blob_get(intel_state->hw.gamma_lut);
153 	if (intel_state->hw.ctm)
154 		drm_property_blob_get(intel_state->hw.ctm);
155 	if (intel_state->hw.lut_3d)
156 		drm_property_blob_get(intel_state->hw.lut_3d);
157 
158 	return &intel_state->uapi;
159 }
160 
161 /**
162  * intel_plane_destroy_state - destroy plane state
163  * @plane: drm plane
164  * @state: state object to destroy
165  *
166  * Destroys the plane state (both common and Intel-specific) for the
167  * specified plane.
168  */
169 void
170 intel_plane_destroy_state(struct drm_plane *plane,
171 			  struct drm_plane_state *state)
172 {
173 	struct intel_plane_state *plane_state = to_intel_plane_state(state);
174 
175 	drm_WARN_ON(plane->dev, plane_state->ggtt_vma);
176 	drm_WARN_ON(plane->dev, plane_state->dpt_vma);
177 
178 	__drm_atomic_helper_plane_destroy_state(&plane_state->uapi);
179 	if (plane_state->hw.fb)
180 		drm_framebuffer_put(plane_state->hw.fb);
181 
182 	if (plane_state->hw.degamma_lut)
183 		drm_property_blob_put(plane_state->hw.degamma_lut);
184 	if (plane_state->hw.gamma_lut)
185 		drm_property_blob_put(plane_state->hw.gamma_lut);
186 	if (plane_state->hw.ctm)
187 		drm_property_blob_put(plane_state->hw.ctm);
188 	if (plane_state->hw.lut_3d)
189 		drm_property_blob_put(plane_state->hw.lut_3d);
190 
191 	kfree(plane_state);
192 }
193 
194 bool intel_plane_needs_low_address(struct intel_display *display)
195 {
196 	/*
197 	 * Valleyview is definitely limited to scanning out the first
198 	 * 512MiB. Lets presume this behaviour was inherited from the
199 	 * g4x display engine and that all earlier gen are similarly
200 	 * limited. Testing suggests that it is a little more
201 	 * complicated than this. For example, Cherryview appears quite
202 	 * happy to scanout from anywhere within its global aperture.
203 	 */
204 	return HAS_GMCH(display);
205 }
206 
207 bool intel_plane_needs_physical(struct intel_plane *plane)
208 {
209 	struct intel_display *display = to_intel_display(plane);
210 
211 	return plane->id == PLANE_CURSOR &&
212 		DISPLAY_INFO(display)->cursor_needs_physical;
213 }
214 
215 bool intel_plane_needs_fence(struct intel_display *display)
216 {
217 	/*
218 	 * pre-i965 planes use the fence for tiled scanout.
219 	 * i965+ planes have their own tiled scanout control bit.
220 	 */
221 	return DISPLAY_VER(display) < 4;
222 }
223 
224 bool intel_plane_can_async_flip(struct intel_plane *plane,
225 				const struct drm_format_info *info,
226 				u64 modifier)
227 {
228 	if (intel_format_info_is_yuv_semiplanar(info, modifier) ||
229 	    info->format == DRM_FORMAT_C8)
230 		return false;
231 
232 	return plane->can_async_flip && plane->can_async_flip(modifier);
233 }
234 
235 bool intel_plane_format_mod_supported_async(struct drm_plane *_plane,
236 					    u32 format, u64 modifier)
237 {
238 	struct intel_plane *plane = to_intel_plane(_plane);
239 	const struct drm_format_info *info;
240 
241 	if (!plane->base.funcs->format_mod_supported(&plane->base, format, modifier))
242 		return false;
243 
244 	info = drm_get_format_info(plane->base.dev, format, modifier);
245 
246 	return intel_plane_can_async_flip(plane, info, modifier);
247 }
248 
249 unsigned int intel_adjusted_rate(const struct drm_rect *src,
250 				 const struct drm_rect *dst,
251 				 unsigned int rate)
252 {
253 	unsigned int src_w, src_h, dst_w, dst_h;
254 
255 	src_w = drm_rect_width(src) >> 16;
256 	src_h = drm_rect_height(src) >> 16;
257 	dst_w = drm_rect_width(dst);
258 	dst_h = drm_rect_height(dst);
259 
260 	/* Downscaling limits the maximum pixel rate */
261 	dst_w = min(src_w, dst_w);
262 	dst_h = min(src_h, dst_h);
263 
264 	return DIV_ROUND_UP_ULL(mul_u32_u32(rate, src_w * src_h),
265 				dst_w * dst_h);
266 }
267 
268 static unsigned int hscale_cdclk(const struct drm_rect *src,
269 				 const struct drm_rect *dst,
270 				 unsigned int ppc)
271 {
272 	unsigned int hscale;
273 
274 	hscale = drm_rect_calc_hscale(src, dst, 0, INT_MAX);
275 	hscale = max(hscale, 0x10000);
276 
277 	/*
278 	 * Double the fractional part due to some 2 PPC granularity issue
279 	 *
280 	 * FIXME: BSpec calls for doubling only the <0.5 fractional part,
281 	 * and rounding it down to a unit fraction. In practice that is
282 	 * not sufficient, and we need a more aggressive CDCLK bump in
283 	 * many cases. The updated formula was derived empirically.
284 	 * This may need to be updated once we have better undestading
285 	 * of what's happening in the hardware...
286 	 */
287 	return (hscale & ~0xffff) + ppc * (hscale & 0xffff);
288 }
289 
290 static unsigned int vscale_cdclk(const struct drm_rect *src,
291 				 const struct drm_rect *dst)
292 {
293 	unsigned int vscale;
294 
295 	vscale = drm_rect_calc_vscale(src, dst, 0, INT_MAX);
296 	vscale = max(vscale, 0x10000);
297 
298 	return vscale;
299 }
300 
301 unsigned int intel_adjusted_rate_cdclk(const struct drm_rect *src,
302 				       const struct drm_rect *dst,
303 				       unsigned int rate,
304 				       unsigned int ppc)
305 {
306 	unsigned int hscale = hscale_cdclk(src, dst, ppc);
307 	unsigned int vscale = vscale_cdclk(src, dst);
308 
309 	return DIV64_U64_ROUND_UP((u64)rate * hscale * vscale, 1ull << 32);
310 }
311 
312 unsigned int intel_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
313 				    const struct intel_plane_state *plane_state)
314 {
315 	/*
316 	 * Note we don't check for plane visibility here as
317 	 * we want to use this when calculating the cursor
318 	 * watermarks even if the cursor is fully offscreen.
319 	 * That depends on the src/dst rectangles being
320 	 * correctly populated whenever the watermark code
321 	 * considers the cursor to be visible, whether or not
322 	 * it is actually visible.
323 	 *
324 	 * See: intel_wm_plane_visible() and intel_check_cursor()
325 	 */
326 
327 	return intel_adjusted_rate(&plane_state->uapi.src,
328 				   &plane_state->uapi.dst,
329 				   crtc_state->pixel_rate);
330 }
331 
332 unsigned int intel_plane_pixel_rate_cdclk(const struct intel_crtc_state *crtc_state,
333 					  const struct intel_plane_state *plane_state)
334 {
335 	struct intel_display *display = to_intel_display(crtc_state);
336 	unsigned int ppc = HAS_2PPC(display) ? 2 : 1;
337 
338 	return intel_adjusted_rate_cdclk(&plane_state->uapi.src,
339 					 &plane_state->uapi.dst,
340 					 crtc_state->pixel_rate_cdclk, ppc);
341 }
342 
343 unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state,
344 				   const struct intel_plane_state *plane_state,
345 				   int color_plane)
346 {
347 	const struct drm_framebuffer *fb = plane_state->hw.fb;
348 
349 	if (!plane_state->uapi.visible)
350 		return 0;
351 
352 	return intel_plane_pixel_rate(crtc_state, plane_state) *
353 		fb->format->cpp[color_plane];
354 }
355 
356 static unsigned int
357 intel_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
358 			       const struct intel_plane_state *plane_state,
359 			       int color_plane)
360 {
361 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
362 	const struct drm_framebuffer *fb = plane_state->hw.fb;
363 	unsigned int rel_data_rate;
364 	int width, height;
365 
366 	if (plane->id == PLANE_CURSOR)
367 		return 0;
368 
369 	if (!plane_state->uapi.visible)
370 		return 0;
371 
372 	/*
373 	 * Src coordinates are already rotated by 270 degrees for
374 	 * the 90/270 degree plane rotation cases (to match the
375 	 * GTT mapping), hence no need to account for rotation here.
376 	 */
377 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
378 	height = drm_rect_height(&plane_state->uapi.src) >> 16;
379 
380 	/* UV plane does 1/2 pixel sub-sampling */
381 	if (color_plane == 1) {
382 		width /= 2;
383 		height /= 2;
384 	}
385 
386 	rel_data_rate =
387 		skl_plane_relative_data_rate(crtc_state, plane, width, height,
388 					     fb->format->cpp[color_plane]);
389 	if (!rel_data_rate)
390 		return 0;
391 
392 	return intel_adjusted_rate(&plane_state->uapi.src,
393 				   &plane_state->uapi.dst,
394 				   rel_data_rate);
395 }
396 
397 static void intel_plane_calc_min_cdclk(struct intel_atomic_state *state,
398 				       struct intel_plane *plane)
399 {
400 	const struct intel_plane_state *plane_state =
401 		intel_atomic_get_new_plane_state(state, plane);
402 	struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
403 	struct intel_crtc_state *new_crtc_state;
404 
405 	if (!plane_state->uapi.visible || !plane->min_cdclk)
406 		return;
407 
408 	new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
409 
410 	new_crtc_state->plane_min_cdclk[plane->id] =
411 		plane->min_cdclk(new_crtc_state, plane_state);
412 }
413 
414 static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state)
415 {
416 	if (plane_state->hw.fb)
417 		drm_framebuffer_put(plane_state->hw.fb);
418 	if (plane_state->hw.degamma_lut)
419 		drm_property_blob_put(plane_state->hw.degamma_lut);
420 	if (plane_state->hw.gamma_lut)
421 		drm_property_blob_put(plane_state->hw.gamma_lut);
422 	if (plane_state->hw.ctm)
423 		drm_property_blob_put(plane_state->hw.ctm);
424 	if (plane_state->hw.lut_3d)
425 		drm_property_blob_put(plane_state->hw.lut_3d);
426 
427 	memset(&plane_state->hw, 0, sizeof(plane_state->hw));
428 }
429 
430 static void
431 intel_plane_copy_uapi_plane_damage(struct intel_plane_state *new_plane_state,
432 				   const struct intel_plane_state *old_uapi_plane_state,
433 				   const struct intel_plane_state *new_uapi_plane_state)
434 {
435 	struct intel_display *display = to_intel_display(new_plane_state);
436 	struct drm_rect *damage = &new_plane_state->damage;
437 
438 	/* damage property tracking enabled from display version 12 onwards */
439 	if (DISPLAY_VER(display) < 12)
440 		return;
441 
442 	if (!drm_atomic_helper_damage_merged(&old_uapi_plane_state->uapi,
443 					     &new_uapi_plane_state->uapi,
444 					     damage))
445 		/* Incase helper fails, mark whole plane region as damage */
446 		*damage = drm_plane_state_src(&new_uapi_plane_state->uapi);
447 }
448 
449 static bool
450 intel_plane_colorop_replace_blob(struct intel_plane_state *plane_state,
451 				 struct intel_colorop *intel_colorop,
452 				 struct drm_property_blob *blob)
453 {
454 	if (intel_colorop->id == INTEL_PLANE_CB_CSC)
455 		return drm_property_replace_blob(&plane_state->hw.ctm, blob);
456 	else if (intel_colorop->id == INTEL_PLANE_CB_PRE_CSC_LUT)
457 		return	drm_property_replace_blob(&plane_state->hw.degamma_lut, blob);
458 	else if (intel_colorop->id == INTEL_PLANE_CB_POST_CSC_LUT)
459 		return drm_property_replace_blob(&plane_state->hw.gamma_lut, blob);
460 	else if (intel_colorop->id == INTEL_PLANE_CB_3DLUT)
461 		return	drm_property_replace_blob(&plane_state->hw.lut_3d, blob);
462 
463 	return false;
464 }
465 
466 static u32
467 fixedmatrix_colorop_to_encoding(enum drm_colorop_fixed_matrix_type fm_type)
468 {
469 	switch (fm_type) {
470 	case DRM_COLOROP_FM_YCBCR709_FULL_RGB:
471 	case DRM_COLOROP_FM_YCBCR709_LIMITED_RGB:
472 		return DRM_COLOR_YCBCR_BT709;
473 
474 	case DRM_COLOROP_FM_YCBCR2020_NC_FULL_RGB:
475 	case DRM_COLOROP_FM_YCBCR2020_NC_LIMITED_RGB:
476 		return DRM_COLOR_YCBCR_BT2020;
477 
478 	case DRM_COLOROP_FM_YCBCR601_FULL_RGB:
479 	case DRM_COLOROP_FM_YCBCR601_LIMITED_RGB:
480 	default:
481 		return DRM_COLOR_YCBCR_BT601;
482 	}
483 }
484 
485 static u32
486 fixedmatrix_colorop_to_range(enum drm_colorop_fixed_matrix_type fm_type)
487 {
488 	switch (fm_type) {
489 	case DRM_COLOROP_FM_YCBCR601_FULL_RGB:
490 	case DRM_COLOROP_FM_YCBCR709_FULL_RGB:
491 	case DRM_COLOROP_FM_YCBCR2020_NC_FULL_RGB:
492 		return DRM_COLOR_YCBCR_FULL_RANGE;
493 
494 	case DRM_COLOROP_FM_YCBCR601_LIMITED_RGB:
495 	case DRM_COLOROP_FM_YCBCR709_LIMITED_RGB:
496 	case DRM_COLOROP_FM_YCBCR2020_NC_LIMITED_RGB:
497 	default:
498 		return DRM_COLOR_YCBCR_LIMITED_RANGE;
499 	}
500 }
501 
502 static void
503 intel_plane_color_copy_uapi_to_hw_state(struct intel_atomic_state *state,
504 					struct intel_plane_state *plane_state,
505 					const struct intel_plane_state *from_plane_state,
506 					struct intel_crtc *crtc)
507 {
508 	struct drm_colorop *iter_colorop, *colorop;
509 	struct drm_colorop_state *new_colorop_state;
510 	struct intel_colorop *intel_colorop;
511 	struct drm_property_blob *blob;
512 	struct intel_crtc_state *new_crtc_state = state ?
513 		intel_atomic_get_new_crtc_state(state, crtc) : NULL;
514 	enum drm_colorop_fixed_matrix_type fm_type;
515 	bool changed = false;
516 	int i = 0;
517 
518 	if (!state)
519 		return;
520 
521 	iter_colorop = from_plane_state->uapi.color_pipeline;
522 
523 	while (iter_colorop) {
524 		for_each_new_colorop_in_state(&state->base, colorop, new_colorop_state, i) {
525 			if (new_colorop_state->colorop == iter_colorop) {
526 				intel_colorop = to_intel_colorop(colorop);
527 				if (intel_colorop->id == INTEL_PLANE_CB_CSC_FF) {
528 					fm_type = new_colorop_state->fixed_matrix_type;
529 
530 					plane_state->hw.csc_ff_enable =
531 						!new_colorop_state->bypass;
532 					plane_state->hw.color_encoding =
533 						fixedmatrix_colorop_to_encoding(fm_type);
534 					plane_state->hw.color_range =
535 						fixedmatrix_colorop_to_range(fm_type);
536 				} else {
537 					blob = new_colorop_state->bypass ?
538 						NULL : new_colorop_state->data;
539 					changed |= intel_plane_colorop_replace_blob(plane_state,
540 									    intel_colorop,
541 									    blob);
542 				}
543 			}
544 		}
545 		iter_colorop = iter_colorop->next;
546 	}
547 
548 	if (new_crtc_state && changed)
549 		new_crtc_state->plane_color_changed = true;
550 }
551 
552 void intel_plane_copy_uapi_to_hw_state(struct intel_atomic_state *state,
553 				       struct intel_plane_state *plane_state,
554 				       const struct intel_plane_state *from_plane_state,
555 				       struct intel_crtc *crtc)
556 {
557 	intel_plane_clear_hw_state(plane_state);
558 
559 	/*
560 	 * For the joiner secondary uapi.crtc will point at
561 	 * the primary crtc. So we explicitly assign the right
562 	 * secondary crtc to hw.crtc. uapi.crtc!=NULL simply
563 	 * indicates the plane is logically enabled on the uapi level.
564 	 */
565 	plane_state->hw.crtc = from_plane_state->uapi.crtc ? &crtc->base : NULL;
566 
567 	plane_state->hw.fb = from_plane_state->uapi.fb;
568 	if (plane_state->hw.fb)
569 		drm_framebuffer_get(plane_state->hw.fb);
570 
571 	plane_state->hw.alpha = from_plane_state->uapi.alpha;
572 	plane_state->hw.pixel_blend_mode =
573 		from_plane_state->uapi.pixel_blend_mode;
574 	plane_state->hw.rotation = from_plane_state->uapi.rotation;
575 	plane_state->hw.color_encoding = from_plane_state->uapi.color_encoding;
576 	plane_state->hw.color_range = from_plane_state->uapi.color_range;
577 	plane_state->hw.scaling_filter = from_plane_state->uapi.scaling_filter;
578 
579 	plane_state->uapi.src = drm_plane_state_src(&from_plane_state->uapi);
580 	plane_state->uapi.dst = drm_plane_state_dest(&from_plane_state->uapi);
581 
582 	intel_plane_color_copy_uapi_to_hw_state(state, plane_state, from_plane_state, crtc);
583 }
584 
585 static void intel_plane_y_copy_hw_state(struct intel_plane_state *y_plane_state,
586 					const struct intel_plane_state *uv_plane_state)
587 {
588 	intel_plane_clear_hw_state(y_plane_state);
589 
590 	y_plane_state->hw.crtc = uv_plane_state->hw.crtc;
591 	y_plane_state->hw.fb = uv_plane_state->hw.fb;
592 	if (y_plane_state->hw.fb)
593 		drm_framebuffer_get(y_plane_state->hw.fb);
594 
595 	y_plane_state->hw.alpha	= uv_plane_state->hw.alpha;
596 	y_plane_state->hw.pixel_blend_mode = uv_plane_state->hw.pixel_blend_mode;
597 	y_plane_state->hw.rotation = uv_plane_state->hw.rotation;
598 	y_plane_state->hw.color_encoding = uv_plane_state->hw.color_encoding;
599 	y_plane_state->hw.color_range = uv_plane_state->hw.color_range;
600 	y_plane_state->hw.scaling_filter = uv_plane_state->hw.scaling_filter;
601 }
602 
603 static void unlink_nv12_plane(struct intel_crtc_state *crtc_state,
604 			      struct intel_plane_state *plane_state)
605 {
606 	struct intel_display *display = to_intel_display(plane_state);
607 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
608 
609 	if (!plane_state->planar_linked_plane)
610 		return;
611 
612 	plane_state->planar_linked_plane = NULL;
613 
614 	if (!plane_state->is_y_plane)
615 		return;
616 
617 	drm_WARN_ON(display->drm, plane_state->uapi.visible);
618 
619 	plane_state->is_y_plane = false;
620 
621 	crtc_state->enabled_planes &= ~BIT(plane->id);
622 	crtc_state->active_planes &= ~BIT(plane->id);
623 	crtc_state->update_planes |= BIT(plane->id);
624 	crtc_state->data_rate[plane->id] = 0;
625 	crtc_state->rel_data_rate[plane->id] = 0;
626 }
627 
628 void intel_plane_set_invisible(struct intel_crtc_state *crtc_state,
629 			       struct intel_plane_state *plane_state)
630 {
631 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
632 
633 	unlink_nv12_plane(crtc_state, plane_state);
634 
635 	crtc_state->active_planes &= ~BIT(plane->id);
636 	crtc_state->scaled_planes &= ~BIT(plane->id);
637 	crtc_state->nv12_planes &= ~BIT(plane->id);
638 	crtc_state->c8_planes &= ~BIT(plane->id);
639 	crtc_state->async_flip_planes &= ~BIT(plane->id);
640 	crtc_state->data_rate[plane->id] = 0;
641 	crtc_state->data_rate_y[plane->id] = 0;
642 	crtc_state->rel_data_rate[plane->id] = 0;
643 	crtc_state->rel_data_rate_y[plane->id] = 0;
644 	crtc_state->plane_min_cdclk[plane->id] = 0;
645 
646 	plane_state->uapi.visible = false;
647 }
648 
649 static bool intel_plane_is_scaled(const struct intel_plane_state *plane_state)
650 {
651 	int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
652 	int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
653 	int dst_w = drm_rect_width(&plane_state->uapi.dst);
654 	int dst_h = drm_rect_height(&plane_state->uapi.dst);
655 
656 	return src_w != dst_w || src_h != dst_h;
657 }
658 
659 static bool intel_plane_do_async_flip(struct intel_plane *plane,
660 				      const struct intel_crtc_state *old_crtc_state,
661 				      const struct intel_crtc_state *new_crtc_state)
662 {
663 	struct intel_display *display = to_intel_display(plane);
664 
665 	if (!plane->async_flip)
666 		return false;
667 
668 	if (!new_crtc_state->uapi.async_flip)
669 		return false;
670 
671 	/*
672 	 * In platforms after DISPLAY13, we might need to override
673 	 * first async flip in order to change watermark levels
674 	 * as part of optimization.
675 	 *
676 	 * And let's do this for all skl+ so that we can eg. change the
677 	 * modifier as well.
678 	 *
679 	 * TODO: For older platforms there is less reason to do this as
680 	 * only X-tile is supported with async flips, though we could
681 	 * extend this so other scanout parameters (stride/etc) could
682 	 * be changed as well...
683 	 */
684 	return DISPLAY_VER(display) < 9 || old_crtc_state->uapi.async_flip;
685 }
686 
687 static bool i9xx_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state,
688 				   const struct intel_plane_state *old_plane_state,
689 				   const struct intel_plane_state *new_plane_state)
690 {
691 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
692 	bool old_visible = old_plane_state->uapi.visible;
693 	bool new_visible = new_plane_state->uapi.visible;
694 	u32 old_ctl = old_plane_state->ctl;
695 	u32 new_ctl = new_plane_state->ctl;
696 	bool modeset, turn_on, turn_off;
697 
698 	if (plane->id == PLANE_CURSOR)
699 		return false;
700 
701 	modeset = intel_crtc_needs_modeset(new_crtc_state);
702 	turn_off = old_visible && (!new_visible || modeset);
703 	turn_on = new_visible && (!old_visible || modeset);
704 
705 	/* Must disable CxSR around plane enable/disable */
706 	if (turn_on || turn_off)
707 		return true;
708 
709 	if (!old_visible || !new_visible)
710 		return false;
711 
712 	/*
713 	 * Most plane control register updates are blocked while in CxSR.
714 	 *
715 	 * Tiling mode is one exception where the primary plane can
716 	 * apparently handle it, whereas the sprites can not (the
717 	 * sprite issue being only relevant on VLV/CHV where CxSR
718 	 * is actually possible with a sprite enabled).
719 	 */
720 	if (plane->id == PLANE_PRIMARY) {
721 		old_ctl &= ~DISP_TILED;
722 		new_ctl &= ~DISP_TILED;
723 	}
724 
725 	return old_ctl != new_ctl;
726 }
727 
728 static bool ilk_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state,
729 				  const struct intel_plane_state *old_plane_state,
730 				  const struct intel_plane_state *new_plane_state)
731 {
732 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
733 	bool old_visible = old_plane_state->uapi.visible;
734 	bool new_visible = new_plane_state->uapi.visible;
735 	bool modeset, turn_on;
736 
737 	if (plane->id == PLANE_CURSOR)
738 		return false;
739 
740 	modeset = intel_crtc_needs_modeset(new_crtc_state);
741 	turn_on = new_visible && (!old_visible || modeset);
742 
743 	/*
744 	 * ILK/SNB DVSACNTR/Sprite Enable
745 	 * IVB SPR_CTL/Sprite Enable
746 	 * "When in Self Refresh Big FIFO mode, a write to enable the
747 	 *  plane will be internally buffered and delayed while Big FIFO
748 	 *  mode is exiting."
749 	 *
750 	 * Which means that enabling the sprite can take an extra frame
751 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
752 	 * down to LP0 and wait for vblank in order to make sure the
753 	 * sprite gets enabled on the next vblank after the register write.
754 	 * Doing otherwise would risk enabling the sprite one frame after
755 	 * we've already signalled flip completion. We can resume LP1+
756 	 * once the sprite has been enabled.
757 	 *
758 	 * With experimental results seems this is needed also for primary
759 	 * plane, not only sprite plane.
760 	 */
761 	if (turn_on)
762 		return true;
763 
764 	/*
765 	 * WaCxSRDisabledForSpriteScaling:ivb
766 	 * IVB SPR_SCALE/Scaling Enable
767 	 * "Low Power watermarks must be disabled for at least one
768 	 *  frame before enabling sprite scaling, and kept disabled
769 	 *  until sprite scaling is disabled."
770 	 *
771 	 * ILK/SNB DVSASCALE/Scaling Enable
772 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
773 	 *  masked off while Big FIFO mode is exiting."
774 	 *
775 	 * Despite the w/a only being listed for IVB we assume that
776 	 * the ILK/SNB note has similar ramifications, hence we apply
777 	 * the w/a on all three platforms.
778 	 */
779 	return !intel_plane_is_scaled(old_plane_state) &&
780 		intel_plane_is_scaled(new_plane_state);
781 }
782 
783 static int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
784 					   struct intel_crtc_state *new_crtc_state,
785 					   const struct intel_plane_state *old_plane_state,
786 					   struct intel_plane_state *new_plane_state)
787 {
788 	struct intel_display *display = to_intel_display(new_crtc_state);
789 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
790 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
791 	bool mode_changed = intel_crtc_needs_modeset(new_crtc_state);
792 	bool was_crtc_enabled = old_crtc_state->hw.active;
793 	bool is_crtc_enabled = new_crtc_state->hw.active;
794 	bool turn_off, turn_on, visible, was_visible;
795 	int ret;
796 
797 	if (DISPLAY_VER(display) >= 9 && plane->id != PLANE_CURSOR) {
798 		ret = skl_update_scaler_plane(new_crtc_state, new_plane_state);
799 		if (ret)
800 			return ret;
801 	}
802 
803 	was_visible = old_plane_state->uapi.visible;
804 	visible = new_plane_state->uapi.visible;
805 
806 	if (!was_crtc_enabled && drm_WARN_ON(display->drm, was_visible))
807 		was_visible = false;
808 
809 	/*
810 	 * Visibility is calculated as if the crtc was on, but
811 	 * after scaler setup everything depends on it being off
812 	 * when the crtc isn't active.
813 	 *
814 	 * FIXME this is wrong for watermarks. Watermarks should also
815 	 * be computed as if the pipe would be active. Perhaps move
816 	 * per-plane wm computation to the .check_plane() hook, and
817 	 * only combine the results from all planes in the current place?
818 	 */
819 	if (!is_crtc_enabled) {
820 		intel_plane_set_invisible(new_crtc_state, new_plane_state);
821 		visible = false;
822 	}
823 
824 	if (!was_visible && !visible)
825 		return 0;
826 
827 	turn_off = was_visible && (!visible || mode_changed);
828 	turn_on = visible && (!was_visible || mode_changed);
829 
830 	drm_dbg_atomic(display->drm,
831 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
832 		       crtc->base.base.id, crtc->base.name,
833 		       plane->base.base.id, plane->base.name,
834 		       was_visible, visible,
835 		       turn_off, turn_on, mode_changed);
836 
837 	if (visible || was_visible)
838 		new_crtc_state->fb_bits |= plane->frontbuffer_bit;
839 
840 	if (HAS_GMCH(display) &&
841 	    i9xx_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
842 		new_crtc_state->disable_cxsr = true;
843 
844 	if ((display->platform.ironlake || display->platform.sandybridge || display->platform.ivybridge) &&
845 	    ilk_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
846 		new_crtc_state->disable_cxsr = true;
847 
848 	if (intel_plane_do_async_flip(plane, old_crtc_state, new_crtc_state))
849 		new_crtc_state->do_async_flip = true;
850 
851 	if (new_crtc_state->uapi.async_flip) {
852 		/*
853 		 * On platforms with double buffered async flip bit we
854 		 * set the bit already one frame early during the sync
855 		 * flip (see {i9xx,skl}_plane_update_arm()). The
856 		 * hardware will therefore be ready to perform a real
857 		 * async flip during the next commit, without having
858 		 * to wait yet another frame for the bit to latch.
859 		 *
860 		 * async_flip_planes bitmask is also used by selective
861 		 * fetch calculation to choose full frame update.
862 		 */
863 		new_crtc_state->async_flip_planes |= BIT(plane->id);
864 	}
865 
866 	return 0;
867 }
868 
869 int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state,
870 					struct intel_crtc_state *new_crtc_state,
871 					const struct intel_plane_state *old_plane_state,
872 					struct intel_plane_state *new_plane_state)
873 {
874 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
875 	const struct drm_framebuffer *fb = new_plane_state->hw.fb;
876 	int ret;
877 
878 	intel_plane_set_invisible(new_crtc_state, new_plane_state);
879 	new_crtc_state->enabled_planes &= ~BIT(plane->id);
880 
881 	if (!new_plane_state->hw.crtc && !old_plane_state->hw.crtc)
882 		return 0;
883 
884 	ret = plane->check_plane(new_crtc_state, new_plane_state);
885 	if (ret)
886 		return ret;
887 
888 	if (fb)
889 		new_crtc_state->enabled_planes |= BIT(plane->id);
890 
891 	/* FIXME pre-g4x don't work like this */
892 	if (new_plane_state->uapi.visible)
893 		new_crtc_state->active_planes |= BIT(plane->id);
894 
895 	if (new_plane_state->uapi.visible &&
896 	    intel_plane_is_scaled(new_plane_state))
897 		new_crtc_state->scaled_planes |= BIT(plane->id);
898 
899 	if (new_plane_state->uapi.visible &&
900 	    intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
901 		new_crtc_state->nv12_planes |= BIT(plane->id);
902 
903 	if (new_plane_state->uapi.visible &&
904 	    fb->format->format == DRM_FORMAT_C8)
905 		new_crtc_state->c8_planes |= BIT(plane->id);
906 
907 	if (new_plane_state->uapi.visible || old_plane_state->uapi.visible)
908 		new_crtc_state->update_planes |= BIT(plane->id);
909 
910 	if (new_plane_state->uapi.visible &&
911 	    intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
912 		new_crtc_state->data_rate_y[plane->id] =
913 			intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
914 		new_crtc_state->data_rate[plane->id] =
915 			intel_plane_data_rate(new_crtc_state, new_plane_state, 1);
916 
917 		new_crtc_state->rel_data_rate_y[plane->id] =
918 			intel_plane_relative_data_rate(new_crtc_state,
919 						       new_plane_state, 0);
920 		new_crtc_state->rel_data_rate[plane->id] =
921 			intel_plane_relative_data_rate(new_crtc_state,
922 						       new_plane_state, 1);
923 	} else if (new_plane_state->uapi.visible) {
924 		new_crtc_state->data_rate[plane->id] =
925 			intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
926 
927 		new_crtc_state->rel_data_rate[plane->id] =
928 			intel_plane_relative_data_rate(new_crtc_state,
929 						       new_plane_state, 0);
930 	}
931 
932 	return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state,
933 					       old_plane_state, new_plane_state);
934 }
935 
936 struct intel_plane *
937 intel_crtc_get_plane(struct intel_crtc *crtc, enum plane_id plane_id)
938 {
939 	struct intel_display *display = to_intel_display(crtc);
940 	struct intel_plane *plane;
941 
942 	for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
943 		if (plane->id == plane_id)
944 			return plane;
945 	}
946 
947 	return NULL;
948 }
949 
950 static int plane_atomic_check(struct intel_atomic_state *state,
951 			      struct intel_plane *plane)
952 {
953 	struct intel_display *display = to_intel_display(state);
954 	struct intel_plane_state *new_plane_state =
955 		intel_atomic_get_new_plane_state(state, plane);
956 	const struct intel_plane_state *old_plane_state =
957 		intel_atomic_get_old_plane_state(state, plane);
958 	const struct intel_plane_state *new_primary_crtc_plane_state;
959 	const struct intel_plane_state *old_primary_crtc_plane_state;
960 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, plane->pipe);
961 	const struct intel_crtc_state *old_crtc_state =
962 		intel_atomic_get_old_crtc_state(state, crtc);
963 	struct intel_crtc_state *new_crtc_state =
964 		intel_atomic_get_new_crtc_state(state, crtc);
965 
966 	if (new_crtc_state && intel_crtc_is_joiner_secondary(new_crtc_state)) {
967 		struct intel_crtc *primary_crtc =
968 			intel_primary_crtc(new_crtc_state);
969 		struct intel_plane *primary_crtc_plane =
970 			intel_crtc_get_plane(primary_crtc, plane->id);
971 
972 		new_primary_crtc_plane_state =
973 			intel_atomic_get_new_plane_state(state, primary_crtc_plane);
974 		old_primary_crtc_plane_state =
975 			intel_atomic_get_old_plane_state(state, primary_crtc_plane);
976 	} else {
977 		new_primary_crtc_plane_state = new_plane_state;
978 		old_primary_crtc_plane_state = old_plane_state;
979 	}
980 
981 	intel_plane_copy_uapi_plane_damage(new_plane_state,
982 					   old_primary_crtc_plane_state,
983 					   new_primary_crtc_plane_state);
984 
985 	intel_plane_copy_uapi_to_hw_state(state,
986 					  new_plane_state,
987 					  new_primary_crtc_plane_state,
988 					  crtc);
989 
990 	new_plane_state->uapi.visible = false;
991 	if (!new_crtc_state)
992 		return 0;
993 
994 	return intel_plane_atomic_check_with_state(old_crtc_state,
995 						   new_crtc_state,
996 						   old_plane_state,
997 						   new_plane_state);
998 }
999 
1000 static struct intel_plane *
1001 skl_next_plane_to_commit(struct intel_atomic_state *state,
1002 			 struct intel_crtc *crtc,
1003 			 struct skl_ddb_entry ddb[I915_MAX_PLANES],
1004 			 struct skl_ddb_entry ddb_y[I915_MAX_PLANES],
1005 			 unsigned int *update_mask)
1006 {
1007 	struct intel_crtc_state *crtc_state =
1008 		intel_atomic_get_new_crtc_state(state, crtc);
1009 	struct intel_plane_state __maybe_unused *plane_state;
1010 	struct intel_plane *plane;
1011 	int i;
1012 
1013 	if (*update_mask == 0)
1014 		return NULL;
1015 
1016 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1017 		enum plane_id plane_id = plane->id;
1018 
1019 		if (crtc->pipe != plane->pipe ||
1020 		    !(*update_mask & BIT(plane_id)))
1021 			continue;
1022 
1023 		if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb[plane_id],
1024 						ddb, I915_MAX_PLANES, plane_id) ||
1025 		    skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
1026 						ddb_y, I915_MAX_PLANES, plane_id))
1027 			continue;
1028 
1029 		*update_mask &= ~BIT(plane_id);
1030 		ddb[plane_id] = crtc_state->wm.skl.plane_ddb[plane_id];
1031 		ddb_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
1032 
1033 		return plane;
1034 	}
1035 
1036 	/* should never happen */
1037 	drm_WARN_ON(state->base.dev, 1);
1038 
1039 	return NULL;
1040 }
1041 
1042 void intel_plane_update_noarm(struct intel_dsb *dsb,
1043 			      struct intel_plane *plane,
1044 			      const struct intel_crtc_state *crtc_state,
1045 			      const struct intel_plane_state *plane_state)
1046 {
1047 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1048 
1049 	trace_intel_plane_update_noarm(plane_state, crtc);
1050 
1051 	if (plane->fbc)
1052 		intel_fbc_dirty_rect_update_noarm(dsb, plane);
1053 
1054 	if (plane->update_noarm)
1055 		plane->update_noarm(dsb, plane, crtc_state, plane_state);
1056 }
1057 
1058 void intel_plane_async_flip(struct intel_dsb *dsb,
1059 			    struct intel_plane *plane,
1060 			    const struct intel_crtc_state *crtc_state,
1061 			    const struct intel_plane_state *plane_state,
1062 			    bool async_flip)
1063 {
1064 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1065 
1066 	trace_intel_plane_async_flip(plane, crtc, async_flip);
1067 	plane->async_flip(dsb, plane, crtc_state, plane_state, async_flip);
1068 }
1069 
1070 void intel_plane_update_arm(struct intel_dsb *dsb,
1071 			    struct intel_plane *plane,
1072 			    const struct intel_crtc_state *crtc_state,
1073 			    const struct intel_plane_state *plane_state)
1074 {
1075 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1076 
1077 	if (crtc_state->do_async_flip && plane->async_flip) {
1078 		intel_plane_async_flip(dsb, plane, crtc_state, plane_state, true);
1079 		return;
1080 	}
1081 
1082 	trace_intel_plane_update_arm(plane_state, crtc);
1083 	plane->update_arm(dsb, plane, crtc_state, plane_state);
1084 }
1085 
1086 void intel_plane_disable_arm(struct intel_dsb *dsb,
1087 			     struct intel_plane *plane,
1088 			     const struct intel_crtc_state *crtc_state)
1089 {
1090 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1091 
1092 	trace_intel_plane_disable_arm(plane, crtc);
1093 	plane->disable_arm(dsb, plane, crtc_state);
1094 }
1095 
1096 void intel_crtc_planes_update_noarm(struct intel_dsb *dsb,
1097 				    struct intel_atomic_state *state,
1098 				    struct intel_crtc *crtc)
1099 {
1100 	struct intel_crtc_state *new_crtc_state =
1101 		intel_atomic_get_new_crtc_state(state, crtc);
1102 	u32 update_mask = new_crtc_state->update_planes;
1103 	struct intel_plane_state *new_plane_state;
1104 	struct intel_plane *plane;
1105 	int i;
1106 
1107 	if (new_crtc_state->do_async_flip)
1108 		return;
1109 
1110 	/*
1111 	 * Since we only write non-arming registers here,
1112 	 * the order does not matter even for skl+.
1113 	 */
1114 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
1115 		if (crtc->pipe != plane->pipe ||
1116 		    !(update_mask & BIT(plane->id)))
1117 			continue;
1118 
1119 		/* TODO: for mailbox updates this should be skipped */
1120 		if (new_plane_state->uapi.visible ||
1121 		    new_plane_state->is_y_plane)
1122 			intel_plane_update_noarm(dsb, plane,
1123 						 new_crtc_state, new_plane_state);
1124 	}
1125 }
1126 
1127 static void skl_crtc_planes_update_arm(struct intel_dsb *dsb,
1128 				       struct intel_atomic_state *state,
1129 				       struct intel_crtc *crtc)
1130 {
1131 	struct intel_crtc_state *old_crtc_state =
1132 		intel_atomic_get_old_crtc_state(state, crtc);
1133 	struct intel_crtc_state *new_crtc_state =
1134 		intel_atomic_get_new_crtc_state(state, crtc);
1135 	struct skl_ddb_entry ddb[I915_MAX_PLANES];
1136 	struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
1137 	u32 update_mask = new_crtc_state->update_planes;
1138 	struct intel_plane *plane;
1139 
1140 	memcpy(ddb, old_crtc_state->wm.skl.plane_ddb,
1141 	       sizeof(old_crtc_state->wm.skl.plane_ddb));
1142 	memcpy(ddb_y, old_crtc_state->wm.skl.plane_ddb_y,
1143 	       sizeof(old_crtc_state->wm.skl.plane_ddb_y));
1144 
1145 	while ((plane = skl_next_plane_to_commit(state, crtc, ddb, ddb_y, &update_mask))) {
1146 		struct intel_plane_state *new_plane_state =
1147 			intel_atomic_get_new_plane_state(state, plane);
1148 
1149 		/*
1150 		 * TODO: for mailbox updates intel_plane_update_noarm()
1151 		 * would have to be called here as well.
1152 		 */
1153 		if (new_plane_state->uapi.visible ||
1154 		    new_plane_state->is_y_plane)
1155 			intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state);
1156 		else
1157 			intel_plane_disable_arm(dsb, plane, new_crtc_state);
1158 	}
1159 }
1160 
1161 static void i9xx_crtc_planes_update_arm(struct intel_dsb *dsb,
1162 					struct intel_atomic_state *state,
1163 					struct intel_crtc *crtc)
1164 {
1165 	struct intel_crtc_state *new_crtc_state =
1166 		intel_atomic_get_new_crtc_state(state, crtc);
1167 	u32 update_mask = new_crtc_state->update_planes;
1168 	struct intel_plane_state *new_plane_state;
1169 	struct intel_plane *plane;
1170 	int i;
1171 
1172 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
1173 		if (crtc->pipe != plane->pipe ||
1174 		    !(update_mask & BIT(plane->id)))
1175 			continue;
1176 
1177 		/*
1178 		 * TODO: for mailbox updates intel_plane_update_noarm()
1179 		 * would have to be called here as well.
1180 		 */
1181 		if (new_plane_state->uapi.visible)
1182 			intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state);
1183 		else
1184 			intel_plane_disable_arm(dsb, plane, new_crtc_state);
1185 	}
1186 }
1187 
1188 void intel_crtc_planes_update_arm(struct intel_dsb *dsb,
1189 				  struct intel_atomic_state *state,
1190 				  struct intel_crtc *crtc)
1191 {
1192 	struct intel_display *display = to_intel_display(state);
1193 
1194 	if (DISPLAY_VER(display) >= 9)
1195 		skl_crtc_planes_update_arm(dsb, state, crtc);
1196 	else
1197 		i9xx_crtc_planes_update_arm(dsb, state, crtc);
1198 }
1199 
1200 int intel_plane_check_clipping(struct intel_plane_state *plane_state,
1201 			       struct intel_crtc_state *crtc_state,
1202 			       int min_scale, int max_scale,
1203 			       bool can_position)
1204 {
1205 	struct intel_display *display = to_intel_display(plane_state);
1206 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1207 	struct drm_framebuffer *fb = plane_state->hw.fb;
1208 	struct drm_rect *src = &plane_state->uapi.src;
1209 	struct drm_rect *dst = &plane_state->uapi.dst;
1210 	const struct drm_rect *clip = &crtc_state->pipe_src;
1211 	unsigned int rotation = plane_state->hw.rotation;
1212 	int hscale, vscale;
1213 
1214 	if (!fb) {
1215 		plane_state->uapi.visible = false;
1216 		return 0;
1217 	}
1218 
1219 	drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
1220 
1221 	/* Check scaling */
1222 	hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
1223 	vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
1224 	if (hscale < 0 || vscale < 0) {
1225 		drm_dbg_kms(display->drm,
1226 			    "[PLANE:%d:%s] invalid scaling "DRM_RECT_FP_FMT " -> " DRM_RECT_FMT "\n",
1227 			    plane->base.base.id, plane->base.name,
1228 			    DRM_RECT_FP_ARG(src), DRM_RECT_ARG(dst));
1229 		return -ERANGE;
1230 	}
1231 
1232 	/*
1233 	 * FIXME: This might need further adjustment for seamless scaling
1234 	 * with phase information, for the 2p2 and 2p1 scenarios.
1235 	 */
1236 	plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, clip);
1237 
1238 	drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
1239 
1240 	if (!can_position && plane_state->uapi.visible &&
1241 	    !drm_rect_equals(dst, clip)) {
1242 		drm_dbg_kms(display->drm,
1243 			    "[PLANE:%d:%s] plane (" DRM_RECT_FMT ") must cover entire CRTC (" DRM_RECT_FMT ")\n",
1244 			    plane->base.base.id, plane->base.name,
1245 			    DRM_RECT_ARG(dst), DRM_RECT_ARG(clip));
1246 		return -EINVAL;
1247 	}
1248 
1249 	/* final plane coordinates will be relative to the plane's pipe */
1250 	drm_rect_translate(dst, -clip->x1, -clip->y1);
1251 
1252 	return 0;
1253 }
1254 
1255 int intel_plane_check_src_coordinates(struct intel_plane_state *plane_state)
1256 {
1257 	struct intel_display *display = to_intel_display(plane_state);
1258 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1259 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1260 	struct drm_rect *src = &plane_state->uapi.src;
1261 	u32 src_x, src_y, src_w, src_h, hsub, vsub;
1262 	bool rotated = drm_rotation_90_or_270(plane_state->hw.rotation);
1263 
1264 	/*
1265 	 * FIXME hsub/vsub vs. block size is a mess. Pre-tgl CCS
1266 	 * abuses hsub/vsub so we can't use them here. But as they
1267 	 * are limited to 32bpp RGB formats we don't actually need
1268 	 * to check anything.
1269 	 */
1270 	if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
1271 	    fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS)
1272 		return 0;
1273 
1274 	/*
1275 	 * Hardware doesn't handle subpixel coordinates.
1276 	 * Adjust to (macro)pixel boundary, but be careful not to
1277 	 * increase the source viewport size, because that could
1278 	 * push the downscaling factor out of bounds.
1279 	 */
1280 	src_x = src->x1 >> 16;
1281 	src_w = drm_rect_width(src) >> 16;
1282 	src_y = src->y1 >> 16;
1283 	src_h = drm_rect_height(src) >> 16;
1284 
1285 	drm_rect_init(src, src_x << 16, src_y << 16,
1286 		      src_w << 16, src_h << 16);
1287 
1288 	if (fb->format->format == DRM_FORMAT_RGB565 && rotated) {
1289 		hsub = 2;
1290 		vsub = 2;
1291 	} else if (DISPLAY_VER(display) >= 20 &&
1292 		   intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
1293 		/*
1294 		 * This allows NV12 and P0xx formats to have odd size and/or odd
1295 		 * source coordinates on DISPLAY_VER(display) >= 20
1296 		 */
1297 		hsub = 1;
1298 		vsub = 1;
1299 
1300 		/* Wa_16023981245 */
1301 		if ((DISPLAY_VERx100(display) == 2000 ||
1302 		     DISPLAY_VERx100(display) == 3000 ||
1303 		     DISPLAY_VERx100(display) == 3002) &&
1304 		     src_x % 2 != 0)
1305 			hsub = 2;
1306 
1307 		if (DISPLAY_VER(display) == 35)
1308 			vsub = 2;
1309 	} else {
1310 		hsub = fb->format->hsub;
1311 		vsub = fb->format->vsub;
1312 	}
1313 
1314 	if (rotated)
1315 		hsub = vsub = max(hsub, vsub);
1316 
1317 	if (src_x % hsub || src_w % hsub) {
1318 		drm_dbg_kms(display->drm,
1319 			    "[PLANE:%d:%s] src x/w (%u, %u) must be a multiple of %u (rotated: %s)\n",
1320 			    plane->base.base.id, plane->base.name,
1321 			    src_x, src_w, hsub, str_yes_no(rotated));
1322 		return -EINVAL;
1323 	}
1324 
1325 	if (src_y % vsub || src_h % vsub) {
1326 		drm_dbg_kms(display->drm,
1327 			    "[PLANE:%d:%s] src y/h (%u, %u) must be a multiple of %u (rotated: %s)\n",
1328 			    plane->base.base.id, plane->base.name,
1329 			    src_y, src_h, vsub, str_yes_no(rotated));
1330 		return -EINVAL;
1331 	}
1332 
1333 	return 0;
1334 }
1335 
1336 static unsigned int
1337 intel_plane_fb_min_alignment(const struct intel_plane_state *plane_state)
1338 {
1339 	const struct intel_framebuffer *fb = to_intel_framebuffer(plane_state->hw.fb);
1340 
1341 	return fb->min_alignment;
1342 }
1343 
1344 static unsigned int
1345 intel_plane_fb_min_phys_alignment(const struct intel_plane_state *plane_state)
1346 {
1347 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1348 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1349 
1350 	if (!intel_plane_needs_physical(plane))
1351 		return 0;
1352 
1353 	return plane->min_alignment(plane, fb, 0);
1354 }
1355 
1356 static unsigned int
1357 intel_plane_fb_vtd_guard(const struct intel_plane_state *plane_state)
1358 {
1359 	return intel_fb_view_vtd_guard(plane_state->hw.fb,
1360 				       &plane_state->view,
1361 				       plane_state->hw.rotation);
1362 }
1363 
1364 int intel_plane_pin_fb(struct intel_plane_state *plane_state,
1365 		       const struct intel_plane_state *old_plane_state)
1366 {
1367 	struct intel_display *display = to_intel_display(plane_state);
1368 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1369 	const struct intel_framebuffer *fb =
1370 		to_intel_framebuffer(plane_state->hw.fb);
1371 	const struct intel_framebuffer *old_fb =
1372 		to_intel_framebuffer(old_plane_state->hw.fb);
1373 	struct i915_vma *ggtt_vma = NULL;
1374 	struct i915_vma *dpt_vma = NULL;
1375 	int fence_id = -1;
1376 	u32 offset = 0;
1377 	int ret;
1378 
1379 	/* hack for xe since it can't keep track of vmas properly */
1380 	ggtt_vma = intel_parent_fb_pin_reuse_vma(display,
1381 						 old_plane_state->ggtt_vma,
1382 						 intel_fb_bo(&old_fb->base),
1383 						 &old_plane_state->view.gtt,
1384 						 intel_fb_bo(&fb->base),
1385 						 &plane_state->view.gtt,
1386 						 &offset);
1387 	if (ggtt_vma)
1388 		goto got_vma;
1389 
1390 	if (!intel_fb_uses_dpt(&fb->base)) {
1391 		struct intel_fb_pin_params pin_params = {
1392 			.view = &plane_state->view.gtt,
1393 			.alignment = intel_plane_fb_min_alignment(plane_state),
1394 			.phys_alignment = intel_plane_fb_min_phys_alignment(plane_state),
1395 			.vtd_guard = intel_plane_fb_vtd_guard(plane_state),
1396 			.needs_cpu_lmem_access = intel_fb_needs_cpu_access(&fb->base),
1397 			.needs_low_address = intel_plane_needs_low_address(display),
1398 			.needs_physical = intel_plane_needs_physical(plane),
1399 			.needs_fence = intel_plane_needs_fence(display),
1400 		};
1401 
1402 		ret = intel_parent_fb_pin_ggtt_pin(display, intel_fb_bo(&fb->base),
1403 						   &pin_params, &ggtt_vma, &offset,
1404 						   intel_plane_uses_fence(plane_state) ? &fence_id : NULL);
1405 	} else {
1406 		struct intel_fb_pin_params pin_params = {
1407 			.view = &plane_state->view.gtt,
1408 			.alignment = intel_plane_fb_min_alignment(plane_state),
1409 			.needs_cpu_lmem_access = intel_fb_needs_cpu_access(&fb->base),
1410 		};
1411 
1412 		ret = intel_parent_fb_pin_dpt_pin(display, intel_fb_bo(&fb->base),
1413 						  fb->dpt, &pin_params,
1414 						  &dpt_vma, &ggtt_vma, &offset);
1415 	}
1416 	if (ret)
1417 		return ret;
1418 
1419 got_vma:
1420 	plane_state->dpt_vma = dpt_vma;
1421 	plane_state->ggtt_vma = ggtt_vma;
1422 	plane_state->fence_id = fence_id;
1423 
1424 	plane_state->surf = offset + plane->surf_offset(plane_state);
1425 
1426 	return 0;
1427 }
1428 
1429 void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
1430 {
1431 	struct intel_display *display = to_intel_display(old_plane_state);
1432 	const struct intel_framebuffer *fb =
1433 		to_intel_framebuffer(old_plane_state->hw.fb);
1434 
1435 	if (!intel_fb_uses_dpt(&fb->base)) {
1436 		intel_parent_fb_pin_ggtt_unpin(display,
1437 					       old_plane_state->ggtt_vma,
1438 					       old_plane_state->fence_id);
1439 
1440 		old_plane_state->ggtt_vma = NULL;
1441 		old_plane_state->fence_id = -1;
1442 	} else {
1443 		intel_parent_fb_pin_dpt_unpin(display, fb->dpt,
1444 					      old_plane_state->dpt_vma,
1445 					      old_plane_state->ggtt_vma);
1446 
1447 		old_plane_state->dpt_vma = NULL;
1448 		old_plane_state->ggtt_vma = NULL;
1449 	}
1450 }
1451 
1452 static int add_dma_resv_fences(struct dma_resv *resv,
1453 			       struct drm_plane_state *new_plane_state)
1454 {
1455 	struct dma_fence *fence = dma_fence_get(new_plane_state->fence);
1456 	struct dma_fence *new;
1457 	int ret;
1458 
1459 	ret = dma_resv_get_singleton(resv, dma_resv_usage_rw(false), &new);
1460 	if (ret)
1461 		goto error;
1462 
1463 	if (new && fence) {
1464 		struct dma_fence_chain *chain = dma_fence_chain_alloc();
1465 
1466 		if (!chain) {
1467 			ret = -ENOMEM;
1468 			goto error;
1469 		}
1470 
1471 		dma_fence_chain_init(chain, fence, new, 1);
1472 		fence = &chain->base;
1473 
1474 	} else if (new) {
1475 		fence = new;
1476 	}
1477 
1478 	dma_fence_put(new_plane_state->fence);
1479 	new_plane_state->fence = fence;
1480 	return 0;
1481 
1482 error:
1483 	dma_fence_put(fence);
1484 	return ret;
1485 }
1486 
1487 /**
1488  * intel_prepare_plane_fb - Prepare fb for usage on plane
1489  * @_plane: drm plane to prepare for
1490  * @_new_plane_state: the plane state being prepared
1491  *
1492  * Prepares a framebuffer for usage on a display plane.  Generally this
1493  * involves pinning the underlying object and updating the frontbuffer tracking
1494  * bits.  Some older platforms need special physical address handling for
1495  * cursor planes.
1496  *
1497  * Returns 0 on success, negative error code on failure.
1498  */
1499 static int
1500 intel_prepare_plane_fb(struct drm_plane *_plane,
1501 		       struct drm_plane_state *_new_plane_state)
1502 {
1503 	struct intel_plane *plane = to_intel_plane(_plane);
1504 	struct intel_display *display = to_intel_display(plane);
1505 	struct intel_plane_state *new_plane_state =
1506 		to_intel_plane_state(_new_plane_state);
1507 	struct intel_atomic_state *state =
1508 		to_intel_atomic_state(new_plane_state->uapi.state);
1509 	struct intel_plane_state *old_plane_state =
1510 		intel_atomic_get_old_plane_state(state, plane);
1511 	struct drm_gem_object *obj = intel_fb_bo(new_plane_state->hw.fb);
1512 	struct drm_gem_object *old_obj = intel_fb_bo(old_plane_state->hw.fb);
1513 	int ret;
1514 
1515 	if (old_obj) {
1516 		const struct intel_crtc_state *new_crtc_state =
1517 			intel_atomic_get_new_crtc_state(state,
1518 							to_intel_crtc(old_plane_state->hw.crtc));
1519 
1520 		/* Big Hammer, we also need to ensure that any pending
1521 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1522 		 * current scanout is retired before unpinning the old
1523 		 * framebuffer. Note that we rely on userspace rendering
1524 		 * into the buffer attached to the pipe they are waiting
1525 		 * on. If not, userspace generates a GPU hang with IPEHR
1526 		 * point to the MI_WAIT_FOR_EVENT.
1527 		 *
1528 		 * This should only fail upon a hung GPU, in which case we
1529 		 * can safely continue.
1530 		 */
1531 		if (intel_crtc_needs_modeset(new_crtc_state)) {
1532 			ret = add_dma_resv_fences(old_obj->resv,
1533 						  &new_plane_state->uapi);
1534 			if (ret < 0)
1535 				return ret;
1536 		}
1537 	}
1538 
1539 	if (!obj)
1540 		return 0;
1541 
1542 	ret = intel_plane_pin_fb(new_plane_state, old_plane_state);
1543 	if (ret)
1544 		return ret;
1545 
1546 	ret = drm_gem_plane_helper_prepare_fb(&plane->base, &new_plane_state->uapi);
1547 	if (ret < 0)
1548 		goto unpin_fb;
1549 
1550 	if (new_plane_state->uapi.fence) {
1551 		intel_parent_fence_priority_display(display, new_plane_state->uapi.fence);
1552 		intel_display_rps_boost_after_vblank(new_plane_state->hw.crtc,
1553 						     new_plane_state->uapi.fence);
1554 	}
1555 
1556 	/*
1557 	 * We declare pageflips to be interactive and so merit a small bias
1558 	 * towards upclocking to deliver the frame on time. By only changing
1559 	 * the RPS thresholds to sample more regularly and aim for higher
1560 	 * clocks we can hopefully deliver low power workloads (like kodi)
1561 	 * that are not quite steady state without resorting to forcing
1562 	 * maximum clocks following a vblank miss (see do_rps_boost()).
1563 	 */
1564 	intel_display_rps_mark_interactive(display, state, true);
1565 
1566 	return 0;
1567 
1568 unpin_fb:
1569 	intel_plane_unpin_fb(new_plane_state);
1570 
1571 	return ret;
1572 }
1573 
1574 /**
1575  * intel_cleanup_plane_fb - Cleans up an fb after plane use
1576  * @plane: drm plane to clean up for
1577  * @_old_plane_state: the state from the previous modeset
1578  *
1579  * Cleans up a framebuffer that has just been removed from a plane.
1580  */
1581 static void
1582 intel_cleanup_plane_fb(struct drm_plane *plane,
1583 		       struct drm_plane_state *_old_plane_state)
1584 {
1585 	struct intel_display *display = to_intel_display(plane->dev);
1586 	struct intel_plane_state *old_plane_state =
1587 		to_intel_plane_state(_old_plane_state);
1588 	struct intel_atomic_state *state =
1589 		to_intel_atomic_state(old_plane_state->uapi.state);
1590 	struct drm_gem_object *obj = intel_fb_bo(old_plane_state->hw.fb);
1591 
1592 	if (!obj)
1593 		return;
1594 
1595 	intel_display_rps_mark_interactive(display, state, false);
1596 
1597 	intel_plane_unpin_fb(old_plane_state);
1598 }
1599 
1600 /* Handle Y-tiling, only if DPT is enabled (otherwise disabling tiling is easier)
1601  * All DPT hardware have 128-bytes width tiling, so Y-tile dimension is 32x32
1602  * pixels for 32bits pixels.
1603  */
1604 #define YTILE_WIDTH	32
1605 #define YTILE_HEIGHT	32
1606 #define YTILE_SIZE (YTILE_WIDTH * YTILE_HEIGHT * 4)
1607 
1608 static unsigned int intel_ytile_get_offset(unsigned int width, unsigned int x, unsigned int y)
1609 {
1610 	u32 offset;
1611 	unsigned int swizzle;
1612 	unsigned int width_in_blocks = DIV_ROUND_UP(width, 32);
1613 
1614 	/* Block offset */
1615 	offset = ((y / YTILE_HEIGHT) * width_in_blocks + (x / YTILE_WIDTH)) * YTILE_SIZE;
1616 
1617 	x = x % YTILE_WIDTH;
1618 	y = y % YTILE_HEIGHT;
1619 
1620 	/* bit order inside a block is x4 x3 x2 y4 y3 y2 y1 y0 x1 x0 */
1621 	swizzle = (x & 3) | ((y & 0x1f) << 2) | ((x & 0x1c) << 5);
1622 	offset += swizzle * 4;
1623 	return offset;
1624 }
1625 
1626 static unsigned int intel_4tile_get_offset(unsigned int width, unsigned int x, unsigned int y)
1627 {
1628 	u32 offset;
1629 	unsigned int swizzle;
1630 	unsigned int width_in_blocks = DIV_ROUND_UP(width, 32);
1631 
1632 	/* Block offset */
1633 	offset = ((y / YTILE_HEIGHT) * width_in_blocks + (x / YTILE_WIDTH)) * YTILE_SIZE;
1634 
1635 	x = x % YTILE_WIDTH;
1636 	y = y % YTILE_HEIGHT;
1637 
1638 	/* bit order inside a block is y4 y3 x4 y2 x3 x2 y1 y0 x1 x0 */
1639 	swizzle = (x & 3) | ((y & 3) << 2) | ((x & 0xc) << 2) | (y & 4) << 4 |
1640 		  ((x & 0x10) << 3) | ((y & 0x18) << 5);
1641 	offset += swizzle * 4;
1642 	return offset;
1643 }
1644 
1645 static void intel_panic_flush(struct drm_plane *_plane)
1646 {
1647 	struct intel_plane *plane = to_intel_plane(_plane);
1648 	struct intel_display *display = to_intel_display(plane);
1649 	const struct intel_plane_state *plane_state = to_intel_plane_state(plane->base.state);
1650 	struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
1651 	const struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
1652 	const struct intel_framebuffer *fb = to_intel_framebuffer(plane_state->hw.fb);
1653 
1654 	intel_parent_panic_finish(display, fb->panic);
1655 
1656 	if (crtc_state->enable_psr2_sel_fetch) {
1657 		/* Force a full update for psr2 */
1658 		intel_psr2_panic_force_full_update(crtc_state);
1659 	}
1660 
1661 	/* Flush the cache and don't disable tiling if it's the fbdev framebuffer.*/
1662 	if (fb == intel_fbdev_framebuffer(display->fbdev.fbdev)) {
1663 		struct iosys_map map;
1664 
1665 		intel_fbdev_get_map(display, &map);
1666 		drm_clflush_virt_range(map.vaddr, fb->base.pitches[0] * fb->base.height);
1667 		return;
1668 	}
1669 
1670 	if (fb->base.modifier != DRM_FORMAT_MOD_LINEAR && plane->disable_tiling)
1671 		plane->disable_tiling(plane);
1672 }
1673 
1674 static unsigned int (*intel_get_tiling_func(u64 fb_modifier))(unsigned int width,
1675 							      unsigned int x,
1676 							      unsigned int y)
1677 {
1678 	switch (fb_modifier) {
1679 	case I915_FORMAT_MOD_Y_TILED:
1680 	case I915_FORMAT_MOD_Y_TILED_CCS:
1681 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1682 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1683 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1684 		return intel_ytile_get_offset;
1685 	case I915_FORMAT_MOD_4_TILED:
1686 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
1687 	case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
1688 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
1689 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
1690 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
1691 	case I915_FORMAT_MOD_4_TILED_MTL_MC_CCS:
1692 	case I915_FORMAT_MOD_4_TILED_BMG_CCS:
1693 	case I915_FORMAT_MOD_4_TILED_LNL_CCS:
1694 		return intel_4tile_get_offset;
1695 	case I915_FORMAT_MOD_X_TILED:
1696 	case I915_FORMAT_MOD_Yf_TILED:
1697 	case I915_FORMAT_MOD_Yf_TILED_CCS:
1698 	default:
1699 	/* Not supported yet */
1700 		return NULL;
1701 	}
1702 }
1703 
1704 static int intel_get_scanout_buffer(struct drm_plane *plane,
1705 				    struct drm_scanout_buffer *sb)
1706 {
1707 	struct intel_plane_state *plane_state;
1708 	struct drm_gem_object *obj;
1709 	struct intel_framebuffer *fb;
1710 	struct intel_display *display = to_intel_display(plane->dev);
1711 
1712 	if (!plane->state || !plane->state->fb || !plane->state->visible)
1713 		return -ENODEV;
1714 
1715 	plane_state = to_intel_plane_state(plane->state);
1716 	fb = to_intel_framebuffer(plane_state->hw.fb);
1717 
1718 	obj = intel_fb_bo(&fb->base);
1719 	if (!obj)
1720 		return -ENODEV;
1721 
1722 	if (fb == intel_fbdev_framebuffer(display->fbdev.fbdev)) {
1723 		intel_fbdev_get_map(display, &sb->map[0]);
1724 	} else {
1725 		unsigned int (*tiling)(unsigned int x, unsigned int y, unsigned int width) = NULL;
1726 		int ret;
1727 		/* Can't disable tiling if DPT is in use */
1728 		if (intel_fb_uses_dpt(&fb->base)) {
1729 			if (fb->base.format->cpp[0] != 4)
1730 				return -EOPNOTSUPP;
1731 			tiling = intel_get_tiling_func(fb->base.modifier);
1732 			if (!tiling)
1733 				return -EOPNOTSUPP;
1734 		}
1735 		ret = intel_parent_panic_setup(display, fb->panic, sb, obj, tiling);
1736 		if (ret)
1737 			return ret;
1738 	}
1739 	sb->width = fb->base.width;
1740 	sb->height = fb->base.height;
1741 	/* Use the generic linear format, because tiling, RC, CCS, CC
1742 	 * will be disabled in disable_tiling()
1743 	 */
1744 	sb->format = drm_format_info(fb->base.format->format);
1745 	sb->pitch[0] = fb->base.pitches[0];
1746 
1747 	return 0;
1748 }
1749 
1750 static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1751 	.prepare_fb = intel_prepare_plane_fb,
1752 	.cleanup_fb = intel_cleanup_plane_fb,
1753 };
1754 
1755 static const struct drm_plane_helper_funcs intel_primary_plane_helper_funcs = {
1756 	.prepare_fb = intel_prepare_plane_fb,
1757 	.cleanup_fb = intel_cleanup_plane_fb,
1758 	.get_scanout_buffer = intel_get_scanout_buffer,
1759 	.panic_flush = intel_panic_flush,
1760 };
1761 
1762 void intel_plane_helper_add(struct intel_plane *plane)
1763 {
1764 	if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1765 		drm_plane_helper_add(&plane->base, &intel_primary_plane_helper_funcs);
1766 	else
1767 		drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
1768 }
1769 
1770 void intel_plane_init_cursor_vblank_work(struct intel_plane_state *old_plane_state,
1771 					 struct intel_plane_state *new_plane_state)
1772 {
1773 	if (!old_plane_state->ggtt_vma ||
1774 	    old_plane_state->ggtt_vma == new_plane_state->ggtt_vma)
1775 		return;
1776 
1777 	drm_vblank_work_init(&old_plane_state->unpin_work, old_plane_state->hw.crtc,
1778 			     intel_cursor_unpin_work);
1779 }
1780 
1781 static void link_nv12_planes(struct intel_crtc_state *crtc_state,
1782 			     struct intel_plane_state *uv_plane_state,
1783 			     struct intel_plane_state *y_plane_state)
1784 {
1785 	struct intel_display *display = to_intel_display(uv_plane_state);
1786 	struct intel_plane *uv_plane = to_intel_plane(uv_plane_state->uapi.plane);
1787 	struct intel_plane *y_plane = to_intel_plane(y_plane_state->uapi.plane);
1788 
1789 	drm_dbg_kms(display->drm, "UV plane [PLANE:%d:%s] using Y plane [PLANE:%d:%s]\n",
1790 		    uv_plane->base.base.id, uv_plane->base.name,
1791 		    y_plane->base.base.id, y_plane->base.name);
1792 
1793 	uv_plane_state->planar_linked_plane = y_plane;
1794 
1795 	y_plane_state->is_y_plane = true;
1796 	y_plane_state->planar_linked_plane = uv_plane;
1797 
1798 	crtc_state->enabled_planes |= BIT(y_plane->id);
1799 	crtc_state->active_planes |= BIT(y_plane->id);
1800 	crtc_state->update_planes |= BIT(y_plane->id);
1801 
1802 	crtc_state->data_rate[y_plane->id] = crtc_state->data_rate_y[uv_plane->id];
1803 	crtc_state->rel_data_rate[y_plane->id] = crtc_state->rel_data_rate_y[uv_plane->id];
1804 
1805 	/* Copy parameters to Y plane */
1806 	intel_plane_y_copy_hw_state(y_plane_state, uv_plane_state);
1807 	y_plane_state->uapi.src = uv_plane_state->uapi.src;
1808 	y_plane_state->uapi.dst = uv_plane_state->uapi.dst;
1809 
1810 	y_plane_state->ctl = uv_plane_state->ctl;
1811 	y_plane_state->color_ctl = uv_plane_state->color_ctl;
1812 	y_plane_state->view = uv_plane_state->view;
1813 	y_plane_state->decrypt = uv_plane_state->decrypt;
1814 
1815 	icl_link_nv12_planes(uv_plane_state, y_plane_state);
1816 }
1817 
1818 static int icl_check_nv12_planes(struct intel_atomic_state *state,
1819 				 struct intel_crtc *crtc)
1820 {
1821 	struct intel_display *display = to_intel_display(state);
1822 	struct intel_crtc_state *crtc_state =
1823 		intel_atomic_get_new_crtc_state(state, crtc);
1824 	struct intel_plane_state *plane_state;
1825 	struct intel_plane *plane;
1826 	int i;
1827 
1828 	if (DISPLAY_VER(display) < 11)
1829 		return 0;
1830 
1831 	if (!crtc_state->nv12_planes)
1832 		return 0;
1833 
1834 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1835 		struct intel_plane_state *y_plane_state = NULL;
1836 		struct intel_plane *y_plane;
1837 
1838 		if (plane->pipe != crtc->pipe)
1839 			continue;
1840 
1841 		if ((crtc_state->nv12_planes & BIT(plane->id)) == 0)
1842 			continue;
1843 
1844 		for_each_intel_plane_on_crtc(display->drm, crtc, y_plane) {
1845 			if (!icl_is_nv12_y_plane(display, y_plane->id))
1846 				continue;
1847 
1848 			if (crtc_state->active_planes & BIT(y_plane->id))
1849 				continue;
1850 
1851 			y_plane_state = intel_atomic_get_plane_state(state, y_plane);
1852 			if (IS_ERR(y_plane_state))
1853 				return PTR_ERR(y_plane_state);
1854 
1855 			break;
1856 		}
1857 
1858 		if (!y_plane_state) {
1859 			drm_dbg_kms(display->drm,
1860 				    "[CRTC:%d:%s] need %d free Y planes for planar YUV\n",
1861 				    crtc->base.base.id, crtc->base.name,
1862 				    hweight8(crtc_state->nv12_planes));
1863 			return -EINVAL;
1864 		}
1865 
1866 		link_nv12_planes(crtc_state, plane_state, y_plane_state);
1867 	}
1868 
1869 	return 0;
1870 }
1871 
1872 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
1873 					  struct intel_crtc *crtc,
1874 					  u8 plane_ids_mask)
1875 {
1876 	struct intel_display *display = to_intel_display(state);
1877 	struct intel_plane *plane;
1878 
1879 	for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
1880 		struct intel_plane_state *plane_state;
1881 
1882 		if ((plane_ids_mask & BIT(plane->id)) == 0)
1883 			continue;
1884 
1885 		plane_state = intel_atomic_get_plane_state(state, plane);
1886 		if (IS_ERR(plane_state))
1887 			return PTR_ERR(plane_state);
1888 	}
1889 
1890 	return 0;
1891 }
1892 
1893 int intel_plane_add_affected(struct intel_atomic_state *state,
1894 			     struct intel_crtc *crtc)
1895 {
1896 	const struct intel_crtc_state *old_crtc_state =
1897 		intel_atomic_get_old_crtc_state(state, crtc);
1898 	const struct intel_crtc_state *new_crtc_state =
1899 		intel_atomic_get_new_crtc_state(state, crtc);
1900 
1901 	return intel_crtc_add_planes_to_state(state, crtc,
1902 					      old_crtc_state->enabled_planes |
1903 					      new_crtc_state->enabled_planes);
1904 }
1905 
1906 static bool active_planes_affects_min_cdclk(struct intel_display *display)
1907 {
1908 	/* See {hsw,vlv,ivb}_plane_ratio() */
1909 	return display->platform.broadwell || display->platform.haswell ||
1910 		display->platform.cherryview || display->platform.valleyview ||
1911 		display->platform.ivybridge;
1912 }
1913 
1914 static u8 intel_joiner_affected_planes(struct intel_atomic_state *state,
1915 				       u8 joined_pipes)
1916 {
1917 	const struct intel_plane_state *plane_state;
1918 	struct intel_plane *plane;
1919 	u8 affected_planes = 0;
1920 	int i;
1921 
1922 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1923 		struct intel_plane *linked = plane_state->planar_linked_plane;
1924 
1925 		if ((joined_pipes & BIT(plane->pipe)) == 0)
1926 			continue;
1927 
1928 		affected_planes |= BIT(plane->id);
1929 		if (linked)
1930 			affected_planes |= BIT(linked->id);
1931 	}
1932 
1933 	return affected_planes;
1934 }
1935 
1936 static int intel_joiner_add_affected_planes(struct intel_atomic_state *state,
1937 					    u8 joined_pipes)
1938 {
1939 	struct intel_display *display = to_intel_display(state);
1940 	u8 prev_affected_planes, affected_planes = 0;
1941 
1942 	/*
1943 	 * We want all the joined pipes to have the same
1944 	 * set of planes in the atomic state, to make sure
1945 	 * state copying always works correctly, and the
1946 	 * UV<->Y plane linkage is always up to date.
1947 	 * Keep pulling planes in until we've determined
1948 	 * the full set of affected planes. A bit complicated
1949 	 * on account of each pipe being capable of selecting
1950 	 * their own Y planes independently of the other pipes,
1951 	 * and the selection being done from the set of
1952 	 * inactive planes.
1953 	 */
1954 	do {
1955 		struct intel_crtc *crtc;
1956 
1957 		for_each_intel_crtc_in_pipe_mask(display, crtc, joined_pipes) {
1958 			int ret;
1959 
1960 			ret = intel_crtc_add_planes_to_state(state, crtc, affected_planes);
1961 			if (ret)
1962 				return ret;
1963 		}
1964 
1965 		prev_affected_planes = affected_planes;
1966 		affected_planes = intel_joiner_affected_planes(state, joined_pipes);
1967 	} while (affected_planes != prev_affected_planes);
1968 
1969 	return 0;
1970 }
1971 
1972 static int intel_add_affected_planes(struct intel_atomic_state *state)
1973 {
1974 	const struct intel_crtc_state *crtc_state;
1975 	struct intel_crtc *crtc;
1976 
1977 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state) {
1978 		int ret;
1979 
1980 		ret = intel_joiner_add_affected_planes(state, intel_crtc_joined_pipe_mask(crtc_state));
1981 		if (ret)
1982 			return ret;
1983 
1984 		/*
1985 		 * Add affected plane here to ensure UV and Y plane linking is
1986 		 * in place if UV plane is added as a part of selective update.
1987 		 */
1988 		if (crtc_state->enable_psr2_sel_fetch) {
1989 			ret = intel_plane_add_affected(state, crtc);
1990 			if (ret)
1991 				return ret;
1992 		}
1993 	}
1994 
1995 	return 0;
1996 }
1997 
1998 int intel_plane_atomic_check(struct intel_atomic_state *state)
1999 {
2000 	struct intel_display *display = to_intel_display(state);
2001 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
2002 	struct intel_plane_state __maybe_unused *plane_state;
2003 	struct intel_plane *plane;
2004 	struct intel_crtc *crtc;
2005 	int i, ret;
2006 
2007 	ret = intel_add_affected_planes(state);
2008 	if (ret)
2009 		return ret;
2010 
2011 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
2012 		ret = plane_atomic_check(state, plane);
2013 		if (ret) {
2014 			drm_dbg_atomic(display->drm,
2015 				       "[PLANE:%d:%s] atomic driver check failed\n",
2016 				       plane->base.base.id, plane->base.name);
2017 			return ret;
2018 		}
2019 	}
2020 
2021 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state) {
2022 		u8 old_active_planes, new_active_planes;
2023 
2024 		ret = icl_check_nv12_planes(state, crtc);
2025 		if (ret)
2026 			return ret;
2027 
2028 		/*
2029 		 * On some platforms the number of active planes affects
2030 		 * the planes' minimum cdclk calculation. Add such planes
2031 		 * to the state before we compute the minimum cdclk.
2032 		 */
2033 		if (!active_planes_affects_min_cdclk(display))
2034 			continue;
2035 
2036 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
2037 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
2038 
2039 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
2040 			continue;
2041 
2042 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
2043 		if (ret)
2044 			return ret;
2045 	}
2046 
2047 	for_each_new_intel_plane_in_state(state, plane, plane_state, i)
2048 		intel_plane_calc_min_cdclk(state, plane);
2049 
2050 	return 0;
2051 }
2052