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