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