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 
37 #include <drm/drm_atomic_helper.h>
38 #include <drm/drm_blend.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_gem.h>
41 #include <drm/drm_gem_atomic_helper.h>
42 
43 #include "i915_drv.h"
44 #include "i915_config.h"
45 #include "i9xx_plane_regs.h"
46 #include "intel_atomic_plane.h"
47 #include "intel_cdclk.h"
48 #include "intel_cursor.h"
49 #include "intel_display_rps.h"
50 #include "intel_display_trace.h"
51 #include "intel_display_types.h"
52 #include "intel_fb.h"
53 #include "intel_fb_pin.h"
54 #include "skl_scaler.h"
55 #include "skl_universal_plane.h"
56 #include "skl_watermark.h"
57 
58 static void intel_plane_state_reset(struct intel_plane_state *plane_state,
59 				    struct intel_plane *plane)
60 {
61 	memset(plane_state, 0, sizeof(*plane_state));
62 
63 	__drm_atomic_helper_plane_state_reset(&plane_state->uapi, &plane->base);
64 
65 	plane_state->scaler_id = -1;
66 }
67 
68 struct intel_plane *intel_plane_alloc(void)
69 {
70 	struct intel_plane_state *plane_state;
71 	struct intel_plane *plane;
72 
73 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
74 	if (!plane)
75 		return ERR_PTR(-ENOMEM);
76 
77 	plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
78 	if (!plane_state) {
79 		kfree(plane);
80 		return ERR_PTR(-ENOMEM);
81 	}
82 
83 	intel_plane_state_reset(plane_state, plane);
84 
85 	plane->base.state = &plane_state->uapi;
86 
87 	return plane;
88 }
89 
90 void intel_plane_free(struct intel_plane *plane)
91 {
92 	intel_plane_destroy_state(&plane->base, plane->base.state);
93 	kfree(plane);
94 }
95 
96 /**
97  * intel_plane_destroy - destroy a plane
98  * @plane: plane to destroy
99  *
100  * Common destruction function for all types of planes (primary, cursor,
101  * sprite).
102  */
103 void intel_plane_destroy(struct drm_plane *plane)
104 {
105 	drm_plane_cleanup(plane);
106 	kfree(to_intel_plane(plane));
107 }
108 
109 /**
110  * intel_plane_duplicate_state - duplicate plane state
111  * @plane: drm plane
112  *
113  * Allocates and returns a copy of the plane state (both common and
114  * Intel-specific) for the specified plane.
115  *
116  * Returns: The newly allocated plane state, or NULL on failure.
117  */
118 struct drm_plane_state *
119 intel_plane_duplicate_state(struct drm_plane *plane)
120 {
121 	struct intel_plane_state *intel_state;
122 
123 	intel_state = to_intel_plane_state(plane->state);
124 	intel_state = kmemdup(intel_state, sizeof(*intel_state), GFP_KERNEL);
125 
126 	if (!intel_state)
127 		return NULL;
128 
129 	__drm_atomic_helper_plane_duplicate_state(plane, &intel_state->uapi);
130 
131 	intel_state->ggtt_vma = NULL;
132 	intel_state->dpt_vma = NULL;
133 	intel_state->flags = 0;
134 
135 	/* add reference to fb */
136 	if (intel_state->hw.fb)
137 		drm_framebuffer_get(intel_state->hw.fb);
138 
139 	return &intel_state->uapi;
140 }
141 
142 /**
143  * intel_plane_destroy_state - destroy plane state
144  * @plane: drm plane
145  * @state: state object to destroy
146  *
147  * Destroys the plane state (both common and Intel-specific) for the
148  * specified plane.
149  */
150 void
151 intel_plane_destroy_state(struct drm_plane *plane,
152 			  struct drm_plane_state *state)
153 {
154 	struct intel_plane_state *plane_state = to_intel_plane_state(state);
155 
156 	drm_WARN_ON(plane->dev, plane_state->ggtt_vma);
157 	drm_WARN_ON(plane->dev, plane_state->dpt_vma);
158 
159 	__drm_atomic_helper_plane_destroy_state(&plane_state->uapi);
160 	if (plane_state->hw.fb)
161 		drm_framebuffer_put(plane_state->hw.fb);
162 	kfree(plane_state);
163 }
164 
165 bool intel_plane_needs_physical(struct intel_plane *plane)
166 {
167 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
168 
169 	return plane->id == PLANE_CURSOR &&
170 		DISPLAY_INFO(i915)->cursor_needs_physical;
171 }
172 
173 bool intel_plane_can_async_flip(struct intel_plane *plane, u64 modifier)
174 {
175 	return plane->can_async_flip && plane->can_async_flip(modifier);
176 }
177 
178 unsigned int intel_adjusted_rate(const struct drm_rect *src,
179 				 const struct drm_rect *dst,
180 				 unsigned int rate)
181 {
182 	unsigned int src_w, src_h, dst_w, dst_h;
183 
184 	src_w = drm_rect_width(src) >> 16;
185 	src_h = drm_rect_height(src) >> 16;
186 	dst_w = drm_rect_width(dst);
187 	dst_h = drm_rect_height(dst);
188 
189 	/* Downscaling limits the maximum pixel rate */
190 	dst_w = min(src_w, dst_w);
191 	dst_h = min(src_h, dst_h);
192 
193 	return DIV_ROUND_UP_ULL(mul_u32_u32(rate, src_w * src_h),
194 				dst_w * dst_h);
195 }
196 
197 unsigned int intel_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
198 				    const struct intel_plane_state *plane_state)
199 {
200 	/*
201 	 * Note we don't check for plane visibility here as
202 	 * we want to use this when calculating the cursor
203 	 * watermarks even if the cursor is fully offscreen.
204 	 * That depends on the src/dst rectangles being
205 	 * correctly populated whenever the watermark code
206 	 * considers the cursor to be visible, whether or not
207 	 * it is actually visible.
208 	 *
209 	 * See: intel_wm_plane_visible() and intel_check_cursor()
210 	 */
211 
212 	return intel_adjusted_rate(&plane_state->uapi.src,
213 				   &plane_state->uapi.dst,
214 				   crtc_state->pixel_rate);
215 }
216 
217 unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state,
218 				   const struct intel_plane_state *plane_state,
219 				   int color_plane)
220 {
221 	const struct drm_framebuffer *fb = plane_state->hw.fb;
222 
223 	if (!plane_state->uapi.visible)
224 		return 0;
225 
226 	return intel_plane_pixel_rate(crtc_state, plane_state) *
227 		fb->format->cpp[color_plane];
228 }
229 
230 static unsigned int
231 intel_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
232 			       const struct intel_plane_state *plane_state,
233 			       int color_plane)
234 {
235 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
236 	const struct drm_framebuffer *fb = plane_state->hw.fb;
237 	unsigned int rel_data_rate;
238 	int width, height;
239 
240 	if (plane->id == PLANE_CURSOR)
241 		return 0;
242 
243 	if (!plane_state->uapi.visible)
244 		return 0;
245 
246 	/*
247 	 * Src coordinates are already rotated by 270 degrees for
248 	 * the 90/270 degree plane rotation cases (to match the
249 	 * GTT mapping), hence no need to account for rotation here.
250 	 */
251 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
252 	height = drm_rect_height(&plane_state->uapi.src) >> 16;
253 
254 	/* UV plane does 1/2 pixel sub-sampling */
255 	if (color_plane == 1) {
256 		width /= 2;
257 		height /= 2;
258 	}
259 
260 	rel_data_rate =
261 		skl_plane_relative_data_rate(crtc_state, plane, width, height,
262 					     fb->format->cpp[color_plane]);
263 	if (!rel_data_rate)
264 		return 0;
265 
266 	return intel_adjusted_rate(&plane_state->uapi.src,
267 				   &plane_state->uapi.dst,
268 				   rel_data_rate);
269 }
270 
271 int intel_plane_calc_min_cdclk(struct intel_atomic_state *state,
272 			       struct intel_plane *plane,
273 			       bool *need_cdclk_calc)
274 {
275 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
276 	const struct intel_plane_state *plane_state =
277 		intel_atomic_get_new_plane_state(state, plane);
278 	struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
279 	const struct intel_cdclk_state *cdclk_state;
280 	const struct intel_crtc_state *old_crtc_state;
281 	struct intel_crtc_state *new_crtc_state;
282 
283 	if (!plane_state->uapi.visible || !plane->min_cdclk)
284 		return 0;
285 
286 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
287 	new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
288 
289 	new_crtc_state->min_cdclk[plane->id] =
290 		plane->min_cdclk(new_crtc_state, plane_state);
291 
292 	/*
293 	 * No need to check against the cdclk state if
294 	 * the min cdclk for the plane doesn't increase.
295 	 *
296 	 * Ie. we only ever increase the cdclk due to plane
297 	 * requirements. This can reduce back and forth
298 	 * display blinking due to constant cdclk changes.
299 	 */
300 	if (new_crtc_state->min_cdclk[plane->id] <=
301 	    old_crtc_state->min_cdclk[plane->id])
302 		return 0;
303 
304 	cdclk_state = intel_atomic_get_cdclk_state(state);
305 	if (IS_ERR(cdclk_state))
306 		return PTR_ERR(cdclk_state);
307 
308 	/*
309 	 * No need to recalculate the cdclk state if
310 	 * the min cdclk for the pipe doesn't increase.
311 	 *
312 	 * Ie. we only ever increase the cdclk due to plane
313 	 * requirements. This can reduce back and forth
314 	 * display blinking due to constant cdclk changes.
315 	 */
316 	if (new_crtc_state->min_cdclk[plane->id] <=
317 	    cdclk_state->min_cdclk[crtc->pipe])
318 		return 0;
319 
320 	drm_dbg_kms(&dev_priv->drm,
321 		    "[PLANE:%d:%s] min cdclk (%d kHz) > [CRTC:%d:%s] min cdclk (%d kHz)\n",
322 		    plane->base.base.id, plane->base.name,
323 		    new_crtc_state->min_cdclk[plane->id],
324 		    crtc->base.base.id, crtc->base.name,
325 		    cdclk_state->min_cdclk[crtc->pipe]);
326 	*need_cdclk_calc = true;
327 
328 	return 0;
329 }
330 
331 static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state)
332 {
333 	if (plane_state->hw.fb)
334 		drm_framebuffer_put(plane_state->hw.fb);
335 
336 	memset(&plane_state->hw, 0, sizeof(plane_state->hw));
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->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 drm_i915_private *i915 = to_i915(plane->base.dev);
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(i915) < 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_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
540 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
541 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
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(dev_priv) >= 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(&dev_priv->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(&dev_priv->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(dev_priv) &&
592 	    i9xx_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
593 		new_crtc_state->disable_cxsr = true;
594 
595 	if ((IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv)) &&
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 drm_i915_private *i915 = to_i915(crtc->base.dev);
689 	struct intel_plane *plane;
690 
691 	for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
692 		if (plane->id == plane_id)
693 			return plane;
694 	}
695 
696 	return NULL;
697 }
698 
699 int intel_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 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, plane->pipe);
709 	const struct intel_crtc_state *old_crtc_state =
710 		intel_atomic_get_old_crtc_state(state, crtc);
711 	struct intel_crtc_state *new_crtc_state =
712 		intel_atomic_get_new_crtc_state(state, crtc);
713 
714 	if (new_crtc_state && intel_crtc_is_joiner_secondary(new_crtc_state)) {
715 		struct intel_crtc *primary_crtc =
716 			intel_primary_crtc(new_crtc_state);
717 		struct intel_plane *primary_crtc_plane =
718 			intel_crtc_get_plane(primary_crtc, plane->id);
719 
720 		new_primary_crtc_plane_state =
721 			intel_atomic_get_new_plane_state(state, primary_crtc_plane);
722 	} else {
723 		new_primary_crtc_plane_state = new_plane_state;
724 	}
725 
726 	intel_plane_copy_uapi_to_hw_state(new_plane_state,
727 					  new_primary_crtc_plane_state,
728 					  crtc);
729 
730 	new_plane_state->uapi.visible = false;
731 	if (!new_crtc_state)
732 		return 0;
733 
734 	return intel_plane_atomic_check_with_state(old_crtc_state,
735 						   new_crtc_state,
736 						   old_plane_state,
737 						   new_plane_state);
738 }
739 
740 static struct intel_plane *
741 skl_next_plane_to_commit(struct intel_atomic_state *state,
742 			 struct intel_crtc *crtc,
743 			 struct skl_ddb_entry ddb[I915_MAX_PLANES],
744 			 struct skl_ddb_entry ddb_y[I915_MAX_PLANES],
745 			 unsigned int *update_mask)
746 {
747 	struct intel_crtc_state *crtc_state =
748 		intel_atomic_get_new_crtc_state(state, crtc);
749 	struct intel_plane_state __maybe_unused *plane_state;
750 	struct intel_plane *plane;
751 	int i;
752 
753 	if (*update_mask == 0)
754 		return NULL;
755 
756 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
757 		enum plane_id plane_id = plane->id;
758 
759 		if (crtc->pipe != plane->pipe ||
760 		    !(*update_mask & BIT(plane_id)))
761 			continue;
762 
763 		if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb[plane_id],
764 						ddb, I915_MAX_PLANES, plane_id) ||
765 		    skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
766 						ddb_y, I915_MAX_PLANES, plane_id))
767 			continue;
768 
769 		*update_mask &= ~BIT(plane_id);
770 		ddb[plane_id] = crtc_state->wm.skl.plane_ddb[plane_id];
771 		ddb_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
772 
773 		return plane;
774 	}
775 
776 	/* should never happen */
777 	drm_WARN_ON(state->base.dev, 1);
778 
779 	return NULL;
780 }
781 
782 void intel_plane_update_noarm(struct intel_dsb *dsb,
783 			      struct intel_plane *plane,
784 			      const struct intel_crtc_state *crtc_state,
785 			      const struct intel_plane_state *plane_state)
786 {
787 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
788 
789 	trace_intel_plane_update_noarm(plane_state, crtc);
790 
791 	if (plane->update_noarm)
792 		plane->update_noarm(dsb, plane, crtc_state, plane_state);
793 }
794 
795 void intel_plane_async_flip(struct intel_dsb *dsb,
796 			    struct intel_plane *plane,
797 			    const struct intel_crtc_state *crtc_state,
798 			    const struct intel_plane_state *plane_state,
799 			    bool async_flip)
800 {
801 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
802 
803 	trace_intel_plane_async_flip(plane, crtc, async_flip);
804 	plane->async_flip(dsb, plane, crtc_state, plane_state, async_flip);
805 }
806 
807 void intel_plane_update_arm(struct intel_dsb *dsb,
808 			    struct intel_plane *plane,
809 			    const struct intel_crtc_state *crtc_state,
810 			    const struct intel_plane_state *plane_state)
811 {
812 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
813 
814 	if (crtc_state->do_async_flip && plane->async_flip) {
815 		intel_plane_async_flip(dsb, plane, crtc_state, plane_state, true);
816 		return;
817 	}
818 
819 	trace_intel_plane_update_arm(plane_state, crtc);
820 	plane->update_arm(dsb, plane, crtc_state, plane_state);
821 }
822 
823 void intel_plane_disable_arm(struct intel_dsb *dsb,
824 			     struct intel_plane *plane,
825 			     const struct intel_crtc_state *crtc_state)
826 {
827 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
828 
829 	trace_intel_plane_disable_arm(plane, crtc);
830 	plane->disable_arm(dsb, plane, crtc_state);
831 }
832 
833 void intel_crtc_planes_update_noarm(struct intel_dsb *dsb,
834 				    struct intel_atomic_state *state,
835 				    struct intel_crtc *crtc)
836 {
837 	struct intel_crtc_state *new_crtc_state =
838 		intel_atomic_get_new_crtc_state(state, crtc);
839 	u32 update_mask = new_crtc_state->update_planes;
840 	struct intel_plane_state *new_plane_state;
841 	struct intel_plane *plane;
842 	int i;
843 
844 	if (new_crtc_state->do_async_flip)
845 		return;
846 
847 	/*
848 	 * Since we only write non-arming registers here,
849 	 * the order does not matter even for skl+.
850 	 */
851 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
852 		if (crtc->pipe != plane->pipe ||
853 		    !(update_mask & BIT(plane->id)))
854 			continue;
855 
856 		/* TODO: for mailbox updates this should be skipped */
857 		if (new_plane_state->uapi.visible ||
858 		    new_plane_state->is_y_plane)
859 			intel_plane_update_noarm(dsb, plane,
860 						 new_crtc_state, new_plane_state);
861 	}
862 }
863 
864 static void skl_crtc_planes_update_arm(struct intel_dsb *dsb,
865 				       struct intel_atomic_state *state,
866 				       struct intel_crtc *crtc)
867 {
868 	struct intel_crtc_state *old_crtc_state =
869 		intel_atomic_get_old_crtc_state(state, crtc);
870 	struct intel_crtc_state *new_crtc_state =
871 		intel_atomic_get_new_crtc_state(state, crtc);
872 	struct skl_ddb_entry ddb[I915_MAX_PLANES];
873 	struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
874 	u32 update_mask = new_crtc_state->update_planes;
875 	struct intel_plane *plane;
876 
877 	memcpy(ddb, old_crtc_state->wm.skl.plane_ddb,
878 	       sizeof(old_crtc_state->wm.skl.plane_ddb));
879 	memcpy(ddb_y, old_crtc_state->wm.skl.plane_ddb_y,
880 	       sizeof(old_crtc_state->wm.skl.plane_ddb_y));
881 
882 	while ((plane = skl_next_plane_to_commit(state, crtc, ddb, ddb_y, &update_mask))) {
883 		struct intel_plane_state *new_plane_state =
884 			intel_atomic_get_new_plane_state(state, plane);
885 
886 		/*
887 		 * TODO: for mailbox updates intel_plane_update_noarm()
888 		 * would have to be called here as well.
889 		 */
890 		if (new_plane_state->uapi.visible ||
891 		    new_plane_state->is_y_plane)
892 			intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state);
893 		else
894 			intel_plane_disable_arm(dsb, plane, new_crtc_state);
895 	}
896 }
897 
898 static void i9xx_crtc_planes_update_arm(struct intel_dsb *dsb,
899 					struct intel_atomic_state *state,
900 					struct intel_crtc *crtc)
901 {
902 	struct intel_crtc_state *new_crtc_state =
903 		intel_atomic_get_new_crtc_state(state, crtc);
904 	u32 update_mask = new_crtc_state->update_planes;
905 	struct intel_plane_state *new_plane_state;
906 	struct intel_plane *plane;
907 	int i;
908 
909 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
910 		if (crtc->pipe != plane->pipe ||
911 		    !(update_mask & BIT(plane->id)))
912 			continue;
913 
914 		/*
915 		 * TODO: for mailbox updates intel_plane_update_noarm()
916 		 * would have to be called here as well.
917 		 */
918 		if (new_plane_state->uapi.visible)
919 			intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state);
920 		else
921 			intel_plane_disable_arm(dsb, plane, new_crtc_state);
922 	}
923 }
924 
925 void intel_crtc_planes_update_arm(struct intel_dsb *dsb,
926 				  struct intel_atomic_state *state,
927 				  struct intel_crtc *crtc)
928 {
929 	struct drm_i915_private *i915 = to_i915(state->base.dev);
930 
931 	if (DISPLAY_VER(i915) >= 9)
932 		skl_crtc_planes_update_arm(dsb, state, crtc);
933 	else
934 		i9xx_crtc_planes_update_arm(dsb, state, crtc);
935 }
936 
937 int intel_atomic_plane_check_clipping(struct intel_plane_state *plane_state,
938 				      struct intel_crtc_state *crtc_state,
939 				      int min_scale, int max_scale,
940 				      bool can_position)
941 {
942 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
943 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
944 	struct drm_framebuffer *fb = plane_state->hw.fb;
945 	struct drm_rect *src = &plane_state->uapi.src;
946 	struct drm_rect *dst = &plane_state->uapi.dst;
947 	const struct drm_rect *clip = &crtc_state->pipe_src;
948 	unsigned int rotation = plane_state->hw.rotation;
949 	int hscale, vscale;
950 
951 	if (!fb) {
952 		plane_state->uapi.visible = false;
953 		return 0;
954 	}
955 
956 	drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
957 
958 	/* Check scaling */
959 	hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
960 	vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
961 	if (hscale < 0 || vscale < 0) {
962 		drm_dbg_kms(&i915->drm,
963 			    "[PLANE:%d:%s] invalid scaling "DRM_RECT_FP_FMT " -> " DRM_RECT_FMT "\n",
964 			    plane->base.base.id, plane->base.name,
965 			    DRM_RECT_FP_ARG(src), DRM_RECT_ARG(dst));
966 		return -ERANGE;
967 	}
968 
969 	/*
970 	 * FIXME: This might need further adjustment for seamless scaling
971 	 * with phase information, for the 2p2 and 2p1 scenarios.
972 	 */
973 	plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, clip);
974 
975 	drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
976 
977 	if (!can_position && plane_state->uapi.visible &&
978 	    !drm_rect_equals(dst, clip)) {
979 		drm_dbg_kms(&i915->drm,
980 			    "[PLANE:%d:%s] plane (" DRM_RECT_FMT ") must cover entire CRTC (" DRM_RECT_FMT ")\n",
981 			    plane->base.base.id, plane->base.name,
982 			    DRM_RECT_ARG(dst), DRM_RECT_ARG(clip));
983 		return -EINVAL;
984 	}
985 
986 	/* final plane coordinates will be relative to the plane's pipe */
987 	drm_rect_translate(dst, -clip->x1, -clip->y1);
988 
989 	return 0;
990 }
991 
992 int intel_plane_check_src_coordinates(struct intel_plane_state *plane_state)
993 {
994 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
995 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
996 	const struct drm_framebuffer *fb = plane_state->hw.fb;
997 	struct drm_rect *src = &plane_state->uapi.src;
998 	u32 src_x, src_y, src_w, src_h, hsub, vsub;
999 	bool rotated = drm_rotation_90_or_270(plane_state->hw.rotation);
1000 
1001 	/*
1002 	 * FIXME hsub/vsub vs. block size is a mess. Pre-tgl CCS
1003 	 * abuses hsub/vsub so we can't use them here. But as they
1004 	 * are limited to 32bpp RGB formats we don't actually need
1005 	 * to check anything.
1006 	 */
1007 	if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
1008 	    fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS)
1009 		return 0;
1010 
1011 	/*
1012 	 * Hardware doesn't handle subpixel coordinates.
1013 	 * Adjust to (macro)pixel boundary, but be careful not to
1014 	 * increase the source viewport size, because that could
1015 	 * push the downscaling factor out of bounds.
1016 	 */
1017 	src_x = src->x1 >> 16;
1018 	src_w = drm_rect_width(src) >> 16;
1019 	src_y = src->y1 >> 16;
1020 	src_h = drm_rect_height(src) >> 16;
1021 
1022 	drm_rect_init(src, src_x << 16, src_y << 16,
1023 		      src_w << 16, src_h << 16);
1024 
1025 	if (fb->format->format == DRM_FORMAT_RGB565 && rotated) {
1026 		hsub = 2;
1027 		vsub = 2;
1028 	} else if (DISPLAY_VER(i915) >= 20 &&
1029 		   intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
1030 		/*
1031 		 * This allows NV12 and P0xx formats to have odd size and/or odd
1032 		 * source coordinates on DISPLAY_VER(i915) >= 20
1033 		 */
1034 		hsub = 1;
1035 		vsub = 1;
1036 
1037 		/* Wa_16023981245 */
1038 		if ((DISPLAY_VERx100(i915) == 2000 ||
1039 		     DISPLAY_VERx100(i915) == 3000) &&
1040 		     src_x % 2 != 0)
1041 			hsub = 2;
1042 	} else {
1043 		hsub = fb->format->hsub;
1044 		vsub = fb->format->vsub;
1045 	}
1046 
1047 	if (rotated)
1048 		hsub = vsub = max(hsub, vsub);
1049 
1050 	if (src_x % hsub || src_w % hsub) {
1051 		drm_dbg_kms(&i915->drm,
1052 			    "[PLANE:%d:%s] src x/w (%u, %u) must be a multiple of %u (rotated: %s)\n",
1053 			    plane->base.base.id, plane->base.name,
1054 			    src_x, src_w, hsub, str_yes_no(rotated));
1055 		return -EINVAL;
1056 	}
1057 
1058 	if (src_y % vsub || src_h % vsub) {
1059 		drm_dbg_kms(&i915->drm,
1060 			    "[PLANE:%d:%s] src y/h (%u, %u) must be a multiple of %u (rotated: %s)\n",
1061 			    plane->base.base.id, plane->base.name,
1062 			    src_y, src_h, vsub, str_yes_no(rotated));
1063 		return -EINVAL;
1064 	}
1065 
1066 	return 0;
1067 }
1068 
1069 static int add_dma_resv_fences(struct dma_resv *resv,
1070 			       struct drm_plane_state *new_plane_state)
1071 {
1072 	struct dma_fence *fence = dma_fence_get(new_plane_state->fence);
1073 	struct dma_fence *new;
1074 	int ret;
1075 
1076 	ret = dma_resv_get_singleton(resv, dma_resv_usage_rw(false), &new);
1077 	if (ret)
1078 		goto error;
1079 
1080 	if (new && fence) {
1081 		struct dma_fence_chain *chain = dma_fence_chain_alloc();
1082 
1083 		if (!chain) {
1084 			ret = -ENOMEM;
1085 			goto error;
1086 		}
1087 
1088 		dma_fence_chain_init(chain, fence, new, 1);
1089 		fence = &chain->base;
1090 
1091 	} else if (new) {
1092 		fence = new;
1093 	}
1094 
1095 	dma_fence_put(new_plane_state->fence);
1096 	new_plane_state->fence = fence;
1097 	return 0;
1098 
1099 error:
1100 	dma_fence_put(fence);
1101 	return ret;
1102 }
1103 
1104 /**
1105  * intel_prepare_plane_fb - Prepare fb for usage on plane
1106  * @_plane: drm plane to prepare for
1107  * @_new_plane_state: the plane state being prepared
1108  *
1109  * Prepares a framebuffer for usage on a display plane.  Generally this
1110  * involves pinning the underlying object and updating the frontbuffer tracking
1111  * bits.  Some older platforms need special physical address handling for
1112  * cursor planes.
1113  *
1114  * Returns 0 on success, negative error code on failure.
1115  */
1116 static int
1117 intel_prepare_plane_fb(struct drm_plane *_plane,
1118 		       struct drm_plane_state *_new_plane_state)
1119 {
1120 	struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY };
1121 	struct intel_plane *plane = to_intel_plane(_plane);
1122 	struct intel_plane_state *new_plane_state =
1123 		to_intel_plane_state(_new_plane_state);
1124 	struct intel_atomic_state *state =
1125 		to_intel_atomic_state(new_plane_state->uapi.state);
1126 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1127 	struct intel_plane_state *old_plane_state =
1128 		intel_atomic_get_old_plane_state(state, plane);
1129 	struct drm_gem_object *obj = intel_fb_bo(new_plane_state->hw.fb);
1130 	struct drm_gem_object *old_obj = intel_fb_bo(old_plane_state->hw.fb);
1131 	int ret;
1132 
1133 	if (old_obj) {
1134 		const struct intel_crtc_state *new_crtc_state =
1135 			intel_atomic_get_new_crtc_state(state,
1136 							to_intel_crtc(old_plane_state->hw.crtc));
1137 
1138 		/* Big Hammer, we also need to ensure that any pending
1139 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1140 		 * current scanout is retired before unpinning the old
1141 		 * framebuffer. Note that we rely on userspace rendering
1142 		 * into the buffer attached to the pipe they are waiting
1143 		 * on. If not, userspace generates a GPU hang with IPEHR
1144 		 * point to the MI_WAIT_FOR_EVENT.
1145 		 *
1146 		 * This should only fail upon a hung GPU, in which case we
1147 		 * can safely continue.
1148 		 */
1149 		if (intel_crtc_needs_modeset(new_crtc_state)) {
1150 			ret = add_dma_resv_fences(old_obj->resv,
1151 						  &new_plane_state->uapi);
1152 			if (ret < 0)
1153 				return ret;
1154 		}
1155 	}
1156 
1157 	if (!obj)
1158 		return 0;
1159 
1160 	ret = intel_plane_pin_fb(new_plane_state, old_plane_state);
1161 	if (ret)
1162 		return ret;
1163 
1164 	ret = drm_gem_plane_helper_prepare_fb(&plane->base, &new_plane_state->uapi);
1165 	if (ret < 0)
1166 		goto unpin_fb;
1167 
1168 	if (new_plane_state->uapi.fence) {
1169 		i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
1170 					     &attr);
1171 
1172 		intel_display_rps_boost_after_vblank(new_plane_state->hw.crtc,
1173 						     new_plane_state->uapi.fence);
1174 	}
1175 
1176 	/*
1177 	 * We declare pageflips to be interactive and so merit a small bias
1178 	 * towards upclocking to deliver the frame on time. By only changing
1179 	 * the RPS thresholds to sample more regularly and aim for higher
1180 	 * clocks we can hopefully deliver low power workloads (like kodi)
1181 	 * that are not quite steady state without resorting to forcing
1182 	 * maximum clocks following a vblank miss (see do_rps_boost()).
1183 	 */
1184 	intel_display_rps_mark_interactive(dev_priv, state, true);
1185 
1186 	return 0;
1187 
1188 unpin_fb:
1189 	intel_plane_unpin_fb(new_plane_state);
1190 
1191 	return ret;
1192 }
1193 
1194 /**
1195  * intel_cleanup_plane_fb - Cleans up an fb after plane use
1196  * @plane: drm plane to clean up for
1197  * @_old_plane_state: the state from the previous modeset
1198  *
1199  * Cleans up a framebuffer that has just been removed from a plane.
1200  */
1201 static void
1202 intel_cleanup_plane_fb(struct drm_plane *plane,
1203 		       struct drm_plane_state *_old_plane_state)
1204 {
1205 	struct intel_plane_state *old_plane_state =
1206 		to_intel_plane_state(_old_plane_state);
1207 	struct intel_atomic_state *state =
1208 		to_intel_atomic_state(old_plane_state->uapi.state);
1209 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
1210 	struct drm_gem_object *obj = intel_fb_bo(old_plane_state->hw.fb);
1211 
1212 	if (!obj)
1213 		return;
1214 
1215 	intel_display_rps_mark_interactive(dev_priv, state, false);
1216 
1217 	intel_plane_unpin_fb(old_plane_state);
1218 }
1219 
1220 static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1221 	.prepare_fb = intel_prepare_plane_fb,
1222 	.cleanup_fb = intel_cleanup_plane_fb,
1223 };
1224 
1225 void intel_plane_helper_add(struct intel_plane *plane)
1226 {
1227 	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
1228 }
1229 
1230 void intel_plane_init_cursor_vblank_work(struct intel_plane_state *old_plane_state,
1231 					 struct intel_plane_state *new_plane_state)
1232 {
1233 	if (!old_plane_state->ggtt_vma ||
1234 	    old_plane_state->ggtt_vma == new_plane_state->ggtt_vma)
1235 		return;
1236 
1237 	drm_vblank_work_init(&old_plane_state->unpin_work, old_plane_state->uapi.crtc,
1238 			     intel_cursor_unpin_work);
1239 }
1240 
1241 static void link_nv12_planes(struct intel_crtc_state *crtc_state,
1242 			     struct intel_plane_state *uv_plane_state,
1243 			     struct intel_plane_state *y_plane_state)
1244 {
1245 	struct intel_display *display = to_intel_display(uv_plane_state);
1246 	struct intel_plane *uv_plane = to_intel_plane(uv_plane_state->uapi.plane);
1247 	struct intel_plane *y_plane = to_intel_plane(y_plane_state->uapi.plane);
1248 
1249 	drm_dbg_kms(display->drm, "UV plane [PLANE:%d:%s] using Y plane [PLANE:%d:%s]\n",
1250 		    uv_plane->base.base.id, uv_plane->base.name,
1251 		    y_plane->base.base.id, y_plane->base.name);
1252 
1253 	uv_plane_state->planar_linked_plane = y_plane;
1254 
1255 	y_plane_state->is_y_plane = true;
1256 	y_plane_state->planar_linked_plane = uv_plane;
1257 
1258 	crtc_state->enabled_planes |= BIT(y_plane->id);
1259 	crtc_state->active_planes |= BIT(y_plane->id);
1260 	crtc_state->update_planes |= BIT(y_plane->id);
1261 
1262 	crtc_state->data_rate[y_plane->id] = crtc_state->data_rate_y[uv_plane->id];
1263 	crtc_state->rel_data_rate[y_plane->id] = crtc_state->rel_data_rate_y[uv_plane->id];
1264 
1265 	/* Copy parameters to Y plane */
1266 	intel_plane_copy_hw_state(y_plane_state, uv_plane_state);
1267 	y_plane_state->uapi.src = uv_plane_state->uapi.src;
1268 	y_plane_state->uapi.dst = uv_plane_state->uapi.dst;
1269 
1270 	y_plane_state->ctl = uv_plane_state->ctl;
1271 	y_plane_state->color_ctl = uv_plane_state->color_ctl;
1272 	y_plane_state->view = uv_plane_state->view;
1273 	y_plane_state->decrypt = uv_plane_state->decrypt;
1274 
1275 	icl_link_nv12_planes(uv_plane_state, y_plane_state);
1276 }
1277 
1278 static void unlink_nv12_plane(struct intel_crtc_state *crtc_state,
1279 			      struct intel_plane_state *plane_state)
1280 {
1281 	struct intel_display *display = to_intel_display(plane_state);
1282 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1283 
1284 	plane_state->planar_linked_plane = NULL;
1285 
1286 	if (!plane_state->is_y_plane)
1287 		return;
1288 
1289 	drm_WARN_ON(display->drm, plane_state->uapi.visible);
1290 
1291 	plane_state->is_y_plane = false;
1292 
1293 	crtc_state->enabled_planes &= ~BIT(plane->id);
1294 	crtc_state->active_planes &= ~BIT(plane->id);
1295 	crtc_state->update_planes |= BIT(plane->id);
1296 	crtc_state->data_rate[plane->id] = 0;
1297 	crtc_state->rel_data_rate[plane->id] = 0;
1298 }
1299 
1300 static int icl_check_nv12_planes(struct intel_atomic_state *state,
1301 				 struct intel_crtc *crtc)
1302 {
1303 	struct intel_display *display = to_intel_display(state);
1304 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1305 	struct intel_crtc_state *crtc_state =
1306 		intel_atomic_get_new_crtc_state(state, crtc);
1307 	struct intel_plane_state *plane_state;
1308 	struct intel_plane *plane;
1309 	int i;
1310 
1311 	if (DISPLAY_VER(dev_priv) < 11)
1312 		return 0;
1313 
1314 	/*
1315 	 * Destroy all old plane links and make the Y plane invisible
1316 	 * in the crtc_state->active_planes mask.
1317 	 */
1318 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1319 		if (plane->pipe != crtc->pipe)
1320 			continue;
1321 
1322 		if (plane_state->planar_linked_plane)
1323 			unlink_nv12_plane(crtc_state, plane_state);
1324 	}
1325 
1326 	if (!crtc_state->nv12_planes)
1327 		return 0;
1328 
1329 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1330 		struct intel_plane_state *y_plane_state = NULL;
1331 		struct intel_plane *y_plane;
1332 
1333 		if (plane->pipe != crtc->pipe)
1334 			continue;
1335 
1336 		if ((crtc_state->nv12_planes & BIT(plane->id)) == 0)
1337 			continue;
1338 
1339 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, y_plane) {
1340 			if (!icl_is_nv12_y_plane(display, y_plane->id))
1341 				continue;
1342 
1343 			if (crtc_state->active_planes & BIT(y_plane->id))
1344 				continue;
1345 
1346 			y_plane_state = intel_atomic_get_plane_state(state, y_plane);
1347 			if (IS_ERR(y_plane_state))
1348 				return PTR_ERR(y_plane_state);
1349 
1350 			break;
1351 		}
1352 
1353 		if (!y_plane_state) {
1354 			drm_dbg_kms(&dev_priv->drm,
1355 				    "[CRTC:%d:%s] need %d free Y planes for planar YUV\n",
1356 				    crtc->base.base.id, crtc->base.name,
1357 				    hweight8(crtc_state->nv12_planes));
1358 			return -EINVAL;
1359 		}
1360 
1361 		link_nv12_planes(crtc_state, plane_state, y_plane_state);
1362 	}
1363 
1364 	return 0;
1365 }
1366 
1367 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
1368 					  struct intel_crtc *crtc,
1369 					  u8 plane_ids_mask)
1370 {
1371 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1372 	struct intel_plane *plane;
1373 
1374 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
1375 		struct intel_plane_state *plane_state;
1376 
1377 		if ((plane_ids_mask & BIT(plane->id)) == 0)
1378 			continue;
1379 
1380 		plane_state = intel_atomic_get_plane_state(state, plane);
1381 		if (IS_ERR(plane_state))
1382 			return PTR_ERR(plane_state);
1383 	}
1384 
1385 	return 0;
1386 }
1387 
1388 int intel_atomic_add_affected_planes(struct intel_atomic_state *state,
1389 				     struct intel_crtc *crtc)
1390 {
1391 	const struct intel_crtc_state *old_crtc_state =
1392 		intel_atomic_get_old_crtc_state(state, crtc);
1393 	const struct intel_crtc_state *new_crtc_state =
1394 		intel_atomic_get_new_crtc_state(state, crtc);
1395 
1396 	return intel_crtc_add_planes_to_state(state, crtc,
1397 					      old_crtc_state->enabled_planes |
1398 					      new_crtc_state->enabled_planes);
1399 }
1400 
1401 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
1402 {
1403 	/* See {hsw,vlv,ivb}_plane_ratio() */
1404 	return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
1405 		IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
1406 		IS_IVYBRIDGE(dev_priv);
1407 }
1408 
1409 static u8 intel_joiner_affected_planes(struct intel_atomic_state *state,
1410 				       u8 joined_pipes)
1411 {
1412 	const struct intel_plane_state *plane_state;
1413 	struct intel_plane *plane;
1414 	u8 affected_planes = 0;
1415 	int i;
1416 
1417 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1418 		struct intel_plane *linked = plane_state->planar_linked_plane;
1419 
1420 		if ((joined_pipes & BIT(plane->pipe)) == 0)
1421 			continue;
1422 
1423 		affected_planes |= BIT(plane->id);
1424 		if (linked)
1425 			affected_planes |= BIT(linked->id);
1426 	}
1427 
1428 	return affected_planes;
1429 }
1430 
1431 static int intel_joiner_add_affected_planes(struct intel_atomic_state *state,
1432 					    u8 joined_pipes)
1433 {
1434 	u8 prev_affected_planes, affected_planes = 0;
1435 
1436 	/*
1437 	 * We want all the joined pipes to have the same
1438 	 * set of planes in the atomic state, to make sure
1439 	 * state copying always works correctly, and the
1440 	 * UV<->Y plane linkage is always up to date.
1441 	 * Keep pulling planes in until we've determined
1442 	 * the full set of affected planes. A bit complicated
1443 	 * on account of each pipe being capable of selecting
1444 	 * their own Y planes independently of the other pipes,
1445 	 * and the selection being done from the set of
1446 	 * inactive planes.
1447 	 */
1448 	do {
1449 		struct intel_crtc *crtc;
1450 
1451 		for_each_intel_crtc_in_pipe_mask(state->base.dev, crtc, joined_pipes) {
1452 			int ret;
1453 
1454 			ret = intel_crtc_add_planes_to_state(state, crtc, affected_planes);
1455 			if (ret)
1456 				return ret;
1457 		}
1458 
1459 		prev_affected_planes = affected_planes;
1460 		affected_planes = intel_joiner_affected_planes(state, joined_pipes);
1461 	} while (affected_planes != prev_affected_planes);
1462 
1463 	return 0;
1464 }
1465 
1466 static int intel_add_affected_planes(struct intel_atomic_state *state)
1467 {
1468 	const struct intel_crtc_state *crtc_state;
1469 	struct intel_crtc *crtc;
1470 	int i;
1471 
1472 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1473 		int ret;
1474 
1475 		ret = intel_joiner_add_affected_planes(state, intel_crtc_joined_pipe_mask(crtc_state));
1476 		if (ret)
1477 			return ret;
1478 	}
1479 
1480 	return 0;
1481 }
1482 
1483 int intel_atomic_check_planes(struct intel_atomic_state *state)
1484 {
1485 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1486 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
1487 	struct intel_plane_state __maybe_unused *plane_state;
1488 	struct intel_plane *plane;
1489 	struct intel_crtc *crtc;
1490 	int i, ret;
1491 
1492 	ret = intel_add_affected_planes(state);
1493 	if (ret)
1494 		return ret;
1495 
1496 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1497 		ret = intel_plane_atomic_check(state, plane);
1498 		if (ret) {
1499 			drm_dbg_atomic(&dev_priv->drm,
1500 				       "[PLANE:%d:%s] atomic driver check failed\n",
1501 				       plane->base.base.id, plane->base.name);
1502 			return ret;
1503 		}
1504 	}
1505 
1506 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
1507 					    new_crtc_state, i) {
1508 		u8 old_active_planes, new_active_planes;
1509 
1510 		ret = icl_check_nv12_planes(state, crtc);
1511 		if (ret)
1512 			return ret;
1513 
1514 		/*
1515 		 * On some platforms the number of active planes affects
1516 		 * the planes' minimum cdclk calculation. Add such planes
1517 		 * to the state before we compute the minimum cdclk.
1518 		 */
1519 		if (!active_planes_affects_min_cdclk(dev_priv))
1520 			continue;
1521 
1522 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1523 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1524 
1525 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
1526 			continue;
1527 
1528 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
1529 		if (ret)
1530 			return ret;
1531 	}
1532 
1533 	return 0;
1534 }
1535