xref: /linux/drivers/gpu/drm/i915/display/intel_plane.c (revision 55fc11ce96e92dbae2aab1d692735f7a9b96fefd)
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * DOC: atomic plane helpers
26  *
27  * The functions here are used by the atomic plane helper functions to
28  * implement legacy plane updates (i.e., drm_plane->update_plane() and
29  * drm_plane->disable_plane()).  This allows plane updates to use the
30  * atomic state infrastructure and perform plane updates as separate
31  * prepare/check/commit/cleanup steps.
32  */
33 
34 #include <linux/dma-fence-chain.h>
35 #include <linux/dma-resv.h>
36 #include <linux/iosys-map.h>
37 
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_blend.h>
40 #include <drm/drm_cache.h>
41 #include <drm/drm_damage_helper.h>
42 #include <drm/drm_fourcc.h>
43 #include <drm/drm_gem.h>
44 #include <drm/drm_gem_atomic_helper.h>
45 #include <drm/drm_panic.h>
46 #include <drm/drm_print.h>
47 
48 #include "i9xx_plane_regs.h"
49 #include "intel_cdclk.h"
50 #include "intel_cursor.h"
51 #include "intel_display_rps.h"
52 #include "intel_display_trace.h"
53 #include "intel_display_types.h"
54 #include "intel_fb.h"
55 #include "intel_fb_pin.h"
56 #include "intel_fbdev.h"
57 #include "intel_panic.h"
58 #include "intel_parent.h"
59 #include "intel_plane.h"
60 #include "intel_psr.h"
61 #include "skl_scaler.h"
62 #include "skl_universal_plane.h"
63 #include "skl_watermark.h"
64 
65 static void intel_plane_state_reset(struct intel_plane_state *plane_state,
66 				    struct intel_plane *plane)
67 {
68 	memset(plane_state, 0, sizeof(*plane_state));
69 
70 	__drm_atomic_helper_plane_state_reset(&plane_state->uapi, &plane->base);
71 
72 	plane_state->scaler_id = -1;
73 }
74 
75 struct intel_plane *intel_plane_alloc(void)
76 {
77 	struct intel_plane_state *plane_state;
78 	struct intel_plane *plane;
79 
80 	plane = kzalloc(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 
1055 		if (DISPLAY_VER(display) == 35)
1056 			vsub = 2;
1057 	} else {
1058 		hsub = fb->format->hsub;
1059 		vsub = fb->format->vsub;
1060 	}
1061 
1062 	if (rotated)
1063 		hsub = vsub = max(hsub, vsub);
1064 
1065 	if (src_x % hsub || src_w % hsub) {
1066 		drm_dbg_kms(display->drm,
1067 			    "[PLANE:%d:%s] src x/w (%u, %u) must be a multiple of %u (rotated: %s)\n",
1068 			    plane->base.base.id, plane->base.name,
1069 			    src_x, src_w, hsub, str_yes_no(rotated));
1070 		return -EINVAL;
1071 	}
1072 
1073 	if (src_y % vsub || src_h % vsub) {
1074 		drm_dbg_kms(display->drm,
1075 			    "[PLANE:%d:%s] src y/h (%u, %u) must be a multiple of %u (rotated: %s)\n",
1076 			    plane->base.base.id, plane->base.name,
1077 			    src_y, src_h, vsub, str_yes_no(rotated));
1078 		return -EINVAL;
1079 	}
1080 
1081 	return 0;
1082 }
1083 
1084 static int add_dma_resv_fences(struct dma_resv *resv,
1085 			       struct drm_plane_state *new_plane_state)
1086 {
1087 	struct dma_fence *fence = dma_fence_get(new_plane_state->fence);
1088 	struct dma_fence *new;
1089 	int ret;
1090 
1091 	ret = dma_resv_get_singleton(resv, dma_resv_usage_rw(false), &new);
1092 	if (ret)
1093 		goto error;
1094 
1095 	if (new && fence) {
1096 		struct dma_fence_chain *chain = dma_fence_chain_alloc();
1097 
1098 		if (!chain) {
1099 			ret = -ENOMEM;
1100 			goto error;
1101 		}
1102 
1103 		dma_fence_chain_init(chain, fence, new, 1);
1104 		fence = &chain->base;
1105 
1106 	} else if (new) {
1107 		fence = new;
1108 	}
1109 
1110 	dma_fence_put(new_plane_state->fence);
1111 	new_plane_state->fence = fence;
1112 	return 0;
1113 
1114 error:
1115 	dma_fence_put(fence);
1116 	return ret;
1117 }
1118 
1119 /**
1120  * intel_prepare_plane_fb - Prepare fb for usage on plane
1121  * @_plane: drm plane to prepare for
1122  * @_new_plane_state: the plane state being prepared
1123  *
1124  * Prepares a framebuffer for usage on a display plane.  Generally this
1125  * involves pinning the underlying object and updating the frontbuffer tracking
1126  * bits.  Some older platforms need special physical address handling for
1127  * cursor planes.
1128  *
1129  * Returns 0 on success, negative error code on failure.
1130  */
1131 static int
1132 intel_prepare_plane_fb(struct drm_plane *_plane,
1133 		       struct drm_plane_state *_new_plane_state)
1134 {
1135 	struct intel_plane *plane = to_intel_plane(_plane);
1136 	struct intel_display *display = to_intel_display(plane);
1137 	struct intel_plane_state *new_plane_state =
1138 		to_intel_plane_state(_new_plane_state);
1139 	struct intel_atomic_state *state =
1140 		to_intel_atomic_state(new_plane_state->uapi.state);
1141 	struct intel_plane_state *old_plane_state =
1142 		intel_atomic_get_old_plane_state(state, plane);
1143 	struct drm_gem_object *obj = intel_fb_bo(new_plane_state->hw.fb);
1144 	struct drm_gem_object *old_obj = intel_fb_bo(old_plane_state->hw.fb);
1145 	int ret;
1146 
1147 	if (old_obj) {
1148 		const struct intel_crtc_state *new_crtc_state =
1149 			intel_atomic_get_new_crtc_state(state,
1150 							to_intel_crtc(old_plane_state->hw.crtc));
1151 
1152 		/* Big Hammer, we also need to ensure that any pending
1153 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1154 		 * current scanout is retired before unpinning the old
1155 		 * framebuffer. Note that we rely on userspace rendering
1156 		 * into the buffer attached to the pipe they are waiting
1157 		 * on. If not, userspace generates a GPU hang with IPEHR
1158 		 * point to the MI_WAIT_FOR_EVENT.
1159 		 *
1160 		 * This should only fail upon a hung GPU, in which case we
1161 		 * can safely continue.
1162 		 */
1163 		if (intel_crtc_needs_modeset(new_crtc_state)) {
1164 			ret = add_dma_resv_fences(old_obj->resv,
1165 						  &new_plane_state->uapi);
1166 			if (ret < 0)
1167 				return ret;
1168 		}
1169 	}
1170 
1171 	if (!obj)
1172 		return 0;
1173 
1174 	ret = intel_plane_pin_fb(new_plane_state, old_plane_state);
1175 	if (ret)
1176 		return ret;
1177 
1178 	ret = drm_gem_plane_helper_prepare_fb(&plane->base, &new_plane_state->uapi);
1179 	if (ret < 0)
1180 		goto unpin_fb;
1181 
1182 	if (new_plane_state->uapi.fence) {
1183 		intel_parent_fence_priority_display(display, new_plane_state->uapi.fence);
1184 		intel_display_rps_boost_after_vblank(new_plane_state->hw.crtc,
1185 						     new_plane_state->uapi.fence);
1186 	}
1187 
1188 	/*
1189 	 * We declare pageflips to be interactive and so merit a small bias
1190 	 * towards upclocking to deliver the frame on time. By only changing
1191 	 * the RPS thresholds to sample more regularly and aim for higher
1192 	 * clocks we can hopefully deliver low power workloads (like kodi)
1193 	 * that are not quite steady state without resorting to forcing
1194 	 * maximum clocks following a vblank miss (see do_rps_boost()).
1195 	 */
1196 	intel_display_rps_mark_interactive(display, state, true);
1197 
1198 	return 0;
1199 
1200 unpin_fb:
1201 	intel_plane_unpin_fb(new_plane_state);
1202 
1203 	return ret;
1204 }
1205 
1206 /**
1207  * intel_cleanup_plane_fb - Cleans up an fb after plane use
1208  * @plane: drm plane to clean up for
1209  * @_old_plane_state: the state from the previous modeset
1210  *
1211  * Cleans up a framebuffer that has just been removed from a plane.
1212  */
1213 static void
1214 intel_cleanup_plane_fb(struct drm_plane *plane,
1215 		       struct drm_plane_state *_old_plane_state)
1216 {
1217 	struct intel_display *display = to_intel_display(plane->dev);
1218 	struct intel_plane_state *old_plane_state =
1219 		to_intel_plane_state(_old_plane_state);
1220 	struct intel_atomic_state *state =
1221 		to_intel_atomic_state(old_plane_state->uapi.state);
1222 	struct drm_gem_object *obj = intel_fb_bo(old_plane_state->hw.fb);
1223 
1224 	if (!obj)
1225 		return;
1226 
1227 	intel_display_rps_mark_interactive(display, state, false);
1228 
1229 	intel_plane_unpin_fb(old_plane_state);
1230 }
1231 
1232 /* Handle Y-tiling, only if DPT is enabled (otherwise disabling tiling is easier)
1233  * All DPT hardware have 128-bytes width tiling, so Y-tile dimension is 32x32
1234  * pixels for 32bits pixels.
1235  */
1236 #define YTILE_WIDTH	32
1237 #define YTILE_HEIGHT	32
1238 #define YTILE_SIZE (YTILE_WIDTH * YTILE_HEIGHT * 4)
1239 
1240 static unsigned int intel_ytile_get_offset(unsigned int width, unsigned int x, unsigned int y)
1241 {
1242 	u32 offset;
1243 	unsigned int swizzle;
1244 	unsigned int width_in_blocks = DIV_ROUND_UP(width, 32);
1245 
1246 	/* Block offset */
1247 	offset = ((y / YTILE_HEIGHT) * width_in_blocks + (x / YTILE_WIDTH)) * YTILE_SIZE;
1248 
1249 	x = x % YTILE_WIDTH;
1250 	y = y % YTILE_HEIGHT;
1251 
1252 	/* bit order inside a block is x4 x3 x2 y4 y3 y2 y1 y0 x1 x0 */
1253 	swizzle = (x & 3) | ((y & 0x1f) << 2) | ((x & 0x1c) << 5);
1254 	offset += swizzle * 4;
1255 	return offset;
1256 }
1257 
1258 static unsigned int intel_4tile_get_offset(unsigned int width, unsigned int x, unsigned int y)
1259 {
1260 	u32 offset;
1261 	unsigned int swizzle;
1262 	unsigned int width_in_blocks = DIV_ROUND_UP(width, 32);
1263 
1264 	/* Block offset */
1265 	offset = ((y / YTILE_HEIGHT) * width_in_blocks + (x / YTILE_WIDTH)) * YTILE_SIZE;
1266 
1267 	x = x % YTILE_WIDTH;
1268 	y = y % YTILE_HEIGHT;
1269 
1270 	/* bit order inside a block is y4 y3 x4 y2 x3 x2 y1 y0 x1 x0 */
1271 	swizzle = (x & 3) | ((y & 3) << 2) | ((x & 0xc) << 2) | (y & 4) << 4 |
1272 		  ((x & 0x10) << 3) | ((y & 0x18) << 5);
1273 	offset += swizzle * 4;
1274 	return offset;
1275 }
1276 
1277 static void intel_panic_flush(struct drm_plane *plane)
1278 {
1279 	struct intel_plane_state *plane_state = to_intel_plane_state(plane->state);
1280 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(plane->state->crtc->state);
1281 	struct intel_plane *iplane = to_intel_plane(plane);
1282 	struct intel_display *display = to_intel_display(iplane);
1283 	struct drm_framebuffer *fb = plane_state->hw.fb;
1284 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1285 
1286 	intel_panic_finish(intel_fb->panic);
1287 
1288 	if (crtc_state->enable_psr2_sel_fetch) {
1289 		/* Force a full update for psr2 */
1290 		intel_psr2_panic_force_full_update(display, crtc_state);
1291 	}
1292 
1293 	/* Flush the cache and don't disable tiling if it's the fbdev framebuffer.*/
1294 	if (intel_fb == intel_fbdev_framebuffer(display->fbdev.fbdev)) {
1295 		struct iosys_map map;
1296 
1297 		intel_fbdev_get_map(display->fbdev.fbdev, &map);
1298 		drm_clflush_virt_range(map.vaddr, fb->pitches[0] * fb->height);
1299 		return;
1300 	}
1301 
1302 	if (fb->modifier && iplane->disable_tiling)
1303 		iplane->disable_tiling(iplane);
1304 }
1305 
1306 static unsigned int (*intel_get_tiling_func(u64 fb_modifier))(unsigned int width,
1307 							      unsigned int x,
1308 							      unsigned int y)
1309 {
1310 	switch (fb_modifier) {
1311 	case I915_FORMAT_MOD_Y_TILED:
1312 	case I915_FORMAT_MOD_Y_TILED_CCS:
1313 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1314 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1315 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1316 		return intel_ytile_get_offset;
1317 	case I915_FORMAT_MOD_4_TILED:
1318 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
1319 	case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
1320 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
1321 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
1322 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
1323 	case I915_FORMAT_MOD_4_TILED_MTL_MC_CCS:
1324 	case I915_FORMAT_MOD_4_TILED_BMG_CCS:
1325 	case I915_FORMAT_MOD_4_TILED_LNL_CCS:
1326 		return intel_4tile_get_offset;
1327 	case I915_FORMAT_MOD_X_TILED:
1328 	case I915_FORMAT_MOD_Yf_TILED:
1329 	case I915_FORMAT_MOD_Yf_TILED_CCS:
1330 	default:
1331 	/* Not supported yet */
1332 		return NULL;
1333 	}
1334 }
1335 
1336 static int intel_get_scanout_buffer(struct drm_plane *plane,
1337 				    struct drm_scanout_buffer *sb)
1338 {
1339 	struct intel_plane_state *plane_state;
1340 	struct drm_gem_object *obj;
1341 	struct drm_framebuffer *fb;
1342 	struct intel_framebuffer *intel_fb;
1343 	struct intel_display *display = to_intel_display(plane->dev);
1344 
1345 	if (!plane->state || !plane->state->fb || !plane->state->visible)
1346 		return -ENODEV;
1347 
1348 	plane_state = to_intel_plane_state(plane->state);
1349 	fb = plane_state->hw.fb;
1350 	intel_fb = to_intel_framebuffer(fb);
1351 
1352 	obj = intel_fb_bo(fb);
1353 	if (!obj)
1354 		return -ENODEV;
1355 
1356 	if (intel_fb == intel_fbdev_framebuffer(display->fbdev.fbdev)) {
1357 		intel_fbdev_get_map(display->fbdev.fbdev, &sb->map[0]);
1358 	} else {
1359 		int ret;
1360 		/* Can't disable tiling if DPT is in use */
1361 		if (intel_fb_uses_dpt(fb)) {
1362 			if (fb->format->cpp[0] != 4)
1363 				return -EOPNOTSUPP;
1364 			intel_fb->panic_tiling = intel_get_tiling_func(fb->modifier);
1365 			if (!intel_fb->panic_tiling)
1366 				return -EOPNOTSUPP;
1367 		}
1368 		sb->private = intel_fb;
1369 		ret = intel_panic_setup(intel_fb->panic, sb);
1370 		if (ret)
1371 			return ret;
1372 	}
1373 	sb->width = fb->width;
1374 	sb->height = fb->height;
1375 	/* Use the generic linear format, because tiling, RC, CCS, CC
1376 	 * will be disabled in disable_tiling()
1377 	 */
1378 	sb->format = drm_format_info(fb->format->format);
1379 	sb->pitch[0] = fb->pitches[0];
1380 
1381 	return 0;
1382 }
1383 
1384 static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1385 	.prepare_fb = intel_prepare_plane_fb,
1386 	.cleanup_fb = intel_cleanup_plane_fb,
1387 };
1388 
1389 static const struct drm_plane_helper_funcs intel_primary_plane_helper_funcs = {
1390 	.prepare_fb = intel_prepare_plane_fb,
1391 	.cleanup_fb = intel_cleanup_plane_fb,
1392 	.get_scanout_buffer = intel_get_scanout_buffer,
1393 	.panic_flush = intel_panic_flush,
1394 };
1395 
1396 void intel_plane_helper_add(struct intel_plane *plane)
1397 {
1398 	if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1399 		drm_plane_helper_add(&plane->base, &intel_primary_plane_helper_funcs);
1400 	else
1401 		drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
1402 }
1403 
1404 void intel_plane_init_cursor_vblank_work(struct intel_plane_state *old_plane_state,
1405 					 struct intel_plane_state *new_plane_state)
1406 {
1407 	if (!old_plane_state->ggtt_vma ||
1408 	    old_plane_state->ggtt_vma == new_plane_state->ggtt_vma)
1409 		return;
1410 
1411 	drm_vblank_work_init(&old_plane_state->unpin_work, old_plane_state->uapi.crtc,
1412 			     intel_cursor_unpin_work);
1413 }
1414 
1415 static void link_nv12_planes(struct intel_crtc_state *crtc_state,
1416 			     struct intel_plane_state *uv_plane_state,
1417 			     struct intel_plane_state *y_plane_state)
1418 {
1419 	struct intel_display *display = to_intel_display(uv_plane_state);
1420 	struct intel_plane *uv_plane = to_intel_plane(uv_plane_state->uapi.plane);
1421 	struct intel_plane *y_plane = to_intel_plane(y_plane_state->uapi.plane);
1422 
1423 	drm_dbg_kms(display->drm, "UV plane [PLANE:%d:%s] using Y plane [PLANE:%d:%s]\n",
1424 		    uv_plane->base.base.id, uv_plane->base.name,
1425 		    y_plane->base.base.id, y_plane->base.name);
1426 
1427 	uv_plane_state->planar_linked_plane = y_plane;
1428 
1429 	y_plane_state->is_y_plane = true;
1430 	y_plane_state->planar_linked_plane = uv_plane;
1431 
1432 	crtc_state->enabled_planes |= BIT(y_plane->id);
1433 	crtc_state->active_planes |= BIT(y_plane->id);
1434 	crtc_state->update_planes |= BIT(y_plane->id);
1435 
1436 	crtc_state->data_rate[y_plane->id] = crtc_state->data_rate_y[uv_plane->id];
1437 	crtc_state->rel_data_rate[y_plane->id] = crtc_state->rel_data_rate_y[uv_plane->id];
1438 
1439 	/* Copy parameters to Y plane */
1440 	intel_plane_copy_hw_state(y_plane_state, uv_plane_state);
1441 	y_plane_state->uapi.src = uv_plane_state->uapi.src;
1442 	y_plane_state->uapi.dst = uv_plane_state->uapi.dst;
1443 
1444 	y_plane_state->ctl = uv_plane_state->ctl;
1445 	y_plane_state->color_ctl = uv_plane_state->color_ctl;
1446 	y_plane_state->view = uv_plane_state->view;
1447 	y_plane_state->decrypt = uv_plane_state->decrypt;
1448 
1449 	icl_link_nv12_planes(uv_plane_state, y_plane_state);
1450 }
1451 
1452 static void unlink_nv12_plane(struct intel_crtc_state *crtc_state,
1453 			      struct intel_plane_state *plane_state)
1454 {
1455 	struct intel_display *display = to_intel_display(plane_state);
1456 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1457 
1458 	plane_state->planar_linked_plane = NULL;
1459 
1460 	if (!plane_state->is_y_plane)
1461 		return;
1462 
1463 	drm_WARN_ON(display->drm, plane_state->uapi.visible);
1464 
1465 	plane_state->is_y_plane = false;
1466 
1467 	crtc_state->enabled_planes &= ~BIT(plane->id);
1468 	crtc_state->active_planes &= ~BIT(plane->id);
1469 	crtc_state->update_planes |= BIT(plane->id);
1470 	crtc_state->data_rate[plane->id] = 0;
1471 	crtc_state->rel_data_rate[plane->id] = 0;
1472 }
1473 
1474 static int icl_check_nv12_planes(struct intel_atomic_state *state,
1475 				 struct intel_crtc *crtc)
1476 {
1477 	struct intel_display *display = to_intel_display(state);
1478 	struct intel_crtc_state *crtc_state =
1479 		intel_atomic_get_new_crtc_state(state, crtc);
1480 	struct intel_plane_state *plane_state;
1481 	struct intel_plane *plane;
1482 	int i;
1483 
1484 	if (DISPLAY_VER(display) < 11)
1485 		return 0;
1486 
1487 	/*
1488 	 * Destroy all old plane links and make the Y plane invisible
1489 	 * in the crtc_state->active_planes mask.
1490 	 */
1491 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1492 		if (plane->pipe != crtc->pipe)
1493 			continue;
1494 
1495 		if (plane_state->planar_linked_plane)
1496 			unlink_nv12_plane(crtc_state, plane_state);
1497 	}
1498 
1499 	if (!crtc_state->nv12_planes)
1500 		return 0;
1501 
1502 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1503 		struct intel_plane_state *y_plane_state = NULL;
1504 		struct intel_plane *y_plane;
1505 
1506 		if (plane->pipe != crtc->pipe)
1507 			continue;
1508 
1509 		if ((crtc_state->nv12_planes & BIT(plane->id)) == 0)
1510 			continue;
1511 
1512 		for_each_intel_plane_on_crtc(display->drm, crtc, y_plane) {
1513 			if (!icl_is_nv12_y_plane(display, y_plane->id))
1514 				continue;
1515 
1516 			if (crtc_state->active_planes & BIT(y_plane->id))
1517 				continue;
1518 
1519 			y_plane_state = intel_atomic_get_plane_state(state, y_plane);
1520 			if (IS_ERR(y_plane_state))
1521 				return PTR_ERR(y_plane_state);
1522 
1523 			break;
1524 		}
1525 
1526 		if (!y_plane_state) {
1527 			drm_dbg_kms(display->drm,
1528 				    "[CRTC:%d:%s] need %d free Y planes for planar YUV\n",
1529 				    crtc->base.base.id, crtc->base.name,
1530 				    hweight8(crtc_state->nv12_planes));
1531 			return -EINVAL;
1532 		}
1533 
1534 		link_nv12_planes(crtc_state, plane_state, y_plane_state);
1535 	}
1536 
1537 	return 0;
1538 }
1539 
1540 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
1541 					  struct intel_crtc *crtc,
1542 					  u8 plane_ids_mask)
1543 {
1544 	struct intel_display *display = to_intel_display(state);
1545 	struct intel_plane *plane;
1546 
1547 	for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
1548 		struct intel_plane_state *plane_state;
1549 
1550 		if ((plane_ids_mask & BIT(plane->id)) == 0)
1551 			continue;
1552 
1553 		plane_state = intel_atomic_get_plane_state(state, plane);
1554 		if (IS_ERR(plane_state))
1555 			return PTR_ERR(plane_state);
1556 	}
1557 
1558 	return 0;
1559 }
1560 
1561 int intel_plane_add_affected(struct intel_atomic_state *state,
1562 			     struct intel_crtc *crtc)
1563 {
1564 	const struct intel_crtc_state *old_crtc_state =
1565 		intel_atomic_get_old_crtc_state(state, crtc);
1566 	const struct intel_crtc_state *new_crtc_state =
1567 		intel_atomic_get_new_crtc_state(state, crtc);
1568 
1569 	return intel_crtc_add_planes_to_state(state, crtc,
1570 					      old_crtc_state->enabled_planes |
1571 					      new_crtc_state->enabled_planes);
1572 }
1573 
1574 static bool active_planes_affects_min_cdclk(struct intel_display *display)
1575 {
1576 	/* See {hsw,vlv,ivb}_plane_ratio() */
1577 	return display->platform.broadwell || display->platform.haswell ||
1578 		display->platform.cherryview || display->platform.valleyview ||
1579 		display->platform.ivybridge;
1580 }
1581 
1582 static u8 intel_joiner_affected_planes(struct intel_atomic_state *state,
1583 				       u8 joined_pipes)
1584 {
1585 	const struct intel_plane_state *plane_state;
1586 	struct intel_plane *plane;
1587 	u8 affected_planes = 0;
1588 	int i;
1589 
1590 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1591 		struct intel_plane *linked = plane_state->planar_linked_plane;
1592 
1593 		if ((joined_pipes & BIT(plane->pipe)) == 0)
1594 			continue;
1595 
1596 		affected_planes |= BIT(plane->id);
1597 		if (linked)
1598 			affected_planes |= BIT(linked->id);
1599 	}
1600 
1601 	return affected_planes;
1602 }
1603 
1604 static int intel_joiner_add_affected_planes(struct intel_atomic_state *state,
1605 					    u8 joined_pipes)
1606 {
1607 	u8 prev_affected_planes, affected_planes = 0;
1608 
1609 	/*
1610 	 * We want all the joined pipes to have the same
1611 	 * set of planes in the atomic state, to make sure
1612 	 * state copying always works correctly, and the
1613 	 * UV<->Y plane linkage is always up to date.
1614 	 * Keep pulling planes in until we've determined
1615 	 * the full set of affected planes. A bit complicated
1616 	 * on account of each pipe being capable of selecting
1617 	 * their own Y planes independently of the other pipes,
1618 	 * and the selection being done from the set of
1619 	 * inactive planes.
1620 	 */
1621 	do {
1622 		struct intel_crtc *crtc;
1623 
1624 		for_each_intel_crtc_in_pipe_mask(state->base.dev, crtc, joined_pipes) {
1625 			int ret;
1626 
1627 			ret = intel_crtc_add_planes_to_state(state, crtc, affected_planes);
1628 			if (ret)
1629 				return ret;
1630 		}
1631 
1632 		prev_affected_planes = affected_planes;
1633 		affected_planes = intel_joiner_affected_planes(state, joined_pipes);
1634 	} while (affected_planes != prev_affected_planes);
1635 
1636 	return 0;
1637 }
1638 
1639 static int intel_add_affected_planes(struct intel_atomic_state *state)
1640 {
1641 	const struct intel_crtc_state *crtc_state;
1642 	struct intel_crtc *crtc;
1643 	int i;
1644 
1645 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1646 		int ret;
1647 
1648 		ret = intel_joiner_add_affected_planes(state, intel_crtc_joined_pipe_mask(crtc_state));
1649 		if (ret)
1650 			return ret;
1651 	}
1652 
1653 	return 0;
1654 }
1655 
1656 int intel_plane_atomic_check(struct intel_atomic_state *state)
1657 {
1658 	struct intel_display *display = to_intel_display(state);
1659 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
1660 	struct intel_plane_state __maybe_unused *plane_state;
1661 	struct intel_plane *plane;
1662 	struct intel_crtc *crtc;
1663 	int i, ret;
1664 
1665 	ret = intel_add_affected_planes(state);
1666 	if (ret)
1667 		return ret;
1668 
1669 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1670 		ret = plane_atomic_check(state, plane);
1671 		if (ret) {
1672 			drm_dbg_atomic(display->drm,
1673 				       "[PLANE:%d:%s] atomic driver check failed\n",
1674 				       plane->base.base.id, plane->base.name);
1675 			return ret;
1676 		}
1677 	}
1678 
1679 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
1680 					    new_crtc_state, i) {
1681 		u8 old_active_planes, new_active_planes;
1682 
1683 		ret = icl_check_nv12_planes(state, crtc);
1684 		if (ret)
1685 			return ret;
1686 
1687 		/*
1688 		 * On some platforms the number of active planes affects
1689 		 * the planes' minimum cdclk calculation. Add such planes
1690 		 * to the state before we compute the minimum cdclk.
1691 		 */
1692 		if (!active_planes_affects_min_cdclk(display))
1693 			continue;
1694 
1695 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1696 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1697 
1698 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
1699 			continue;
1700 
1701 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
1702 		if (ret)
1703 			return ret;
1704 	}
1705 
1706 	for_each_new_intel_plane_in_state(state, plane, plane_state, i)
1707 		intel_plane_calc_min_cdclk(state, plane);
1708 
1709 	return 0;
1710 }
1711