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