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