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