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