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