xref: /linux/drivers/gpu/drm/i915/display/intel_atomic_plane.c (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
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 <drm/drm_atomic_helper.h>
35 #include <drm/drm_fourcc.h>
36 
37 #include "gt/intel_rps.h"
38 
39 #include "i915_config.h"
40 #include "intel_atomic_plane.h"
41 #include "intel_cdclk.h"
42 #include "intel_display_trace.h"
43 #include "intel_display_types.h"
44 #include "intel_fb.h"
45 #include "intel_fb_pin.h"
46 #include "intel_sprite.h"
47 #include "skl_scaler.h"
48 #include "skl_watermark.h"
49 
50 static void intel_plane_state_reset(struct intel_plane_state *plane_state,
51 				    struct intel_plane *plane)
52 {
53 	memset(plane_state, 0, sizeof(*plane_state));
54 
55 	__drm_atomic_helper_plane_state_reset(&plane_state->uapi, &plane->base);
56 
57 	plane_state->scaler_id = -1;
58 }
59 
60 struct intel_plane *intel_plane_alloc(void)
61 {
62 	struct intel_plane_state *plane_state;
63 	struct intel_plane *plane;
64 
65 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
66 	if (!plane)
67 		return ERR_PTR(-ENOMEM);
68 
69 	plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
70 	if (!plane_state) {
71 		kfree(plane);
72 		return ERR_PTR(-ENOMEM);
73 	}
74 
75 	intel_plane_state_reset(plane_state, plane);
76 
77 	plane->base.state = &plane_state->uapi;
78 
79 	return plane;
80 }
81 
82 void intel_plane_free(struct intel_plane *plane)
83 {
84 	intel_plane_destroy_state(&plane->base, plane->base.state);
85 	kfree(plane);
86 }
87 
88 /**
89  * intel_plane_duplicate_state - duplicate plane state
90  * @plane: drm plane
91  *
92  * Allocates and returns a copy of the plane state (both common and
93  * Intel-specific) for the specified plane.
94  *
95  * Returns: The newly allocated plane state, or NULL on failure.
96  */
97 struct drm_plane_state *
98 intel_plane_duplicate_state(struct drm_plane *plane)
99 {
100 	struct intel_plane_state *intel_state;
101 
102 	intel_state = to_intel_plane_state(plane->state);
103 	intel_state = kmemdup(intel_state, sizeof(*intel_state), GFP_KERNEL);
104 
105 	if (!intel_state)
106 		return NULL;
107 
108 	__drm_atomic_helper_plane_duplicate_state(plane, &intel_state->uapi);
109 
110 	intel_state->ggtt_vma = NULL;
111 	intel_state->dpt_vma = NULL;
112 	intel_state->flags = 0;
113 
114 	/* add reference to fb */
115 	if (intel_state->hw.fb)
116 		drm_framebuffer_get(intel_state->hw.fb);
117 
118 	return &intel_state->uapi;
119 }
120 
121 /**
122  * intel_plane_destroy_state - destroy plane state
123  * @plane: drm plane
124  * @state: state object to destroy
125  *
126  * Destroys the plane state (both common and Intel-specific) for the
127  * specified plane.
128  */
129 void
130 intel_plane_destroy_state(struct drm_plane *plane,
131 			  struct drm_plane_state *state)
132 {
133 	struct intel_plane_state *plane_state = to_intel_plane_state(state);
134 
135 	drm_WARN_ON(plane->dev, plane_state->ggtt_vma);
136 	drm_WARN_ON(plane->dev, plane_state->dpt_vma);
137 
138 	__drm_atomic_helper_plane_destroy_state(&plane_state->uapi);
139 	if (plane_state->hw.fb)
140 		drm_framebuffer_put(plane_state->hw.fb);
141 	kfree(plane_state);
142 }
143 
144 unsigned int intel_adjusted_rate(const struct drm_rect *src,
145 				 const struct drm_rect *dst,
146 				 unsigned int rate)
147 {
148 	unsigned int src_w, src_h, dst_w, dst_h;
149 
150 	src_w = drm_rect_width(src) >> 16;
151 	src_h = drm_rect_height(src) >> 16;
152 	dst_w = drm_rect_width(dst);
153 	dst_h = drm_rect_height(dst);
154 
155 	/* Downscaling limits the maximum pixel rate */
156 	dst_w = min(src_w, dst_w);
157 	dst_h = min(src_h, dst_h);
158 
159 	return DIV_ROUND_UP_ULL(mul_u32_u32(rate, src_w * src_h),
160 				dst_w * dst_h);
161 }
162 
163 unsigned int intel_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
164 				    const struct intel_plane_state *plane_state)
165 {
166 	/*
167 	 * Note we don't check for plane visibility here as
168 	 * we want to use this when calculating the cursor
169 	 * watermarks even if the cursor is fully offscreen.
170 	 * That depends on the src/dst rectangles being
171 	 * correctly populated whenever the watermark code
172 	 * considers the cursor to be visible, whether or not
173 	 * it is actually visible.
174 	 *
175 	 * See: intel_wm_plane_visible() and intel_check_cursor()
176 	 */
177 
178 	return intel_adjusted_rate(&plane_state->uapi.src,
179 				   &plane_state->uapi.dst,
180 				   crtc_state->pixel_rate);
181 }
182 
183 unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state,
184 				   const struct intel_plane_state *plane_state,
185 				   int color_plane)
186 {
187 	const struct drm_framebuffer *fb = plane_state->hw.fb;
188 
189 	if (!plane_state->uapi.visible)
190 		return 0;
191 
192 	return intel_plane_pixel_rate(crtc_state, plane_state) *
193 		fb->format->cpp[color_plane];
194 }
195 
196 static bool
197 use_min_ddb(const struct intel_crtc_state *crtc_state,
198 	    struct intel_plane *plane)
199 {
200 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
201 
202 	return DISPLAY_VER(i915) >= 13 &&
203 	       crtc_state->uapi.async_flip &&
204 	       plane->async_flip;
205 }
206 
207 static unsigned int
208 intel_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
209 			       const struct intel_plane_state *plane_state,
210 			       int color_plane)
211 {
212 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
213 	const struct drm_framebuffer *fb = plane_state->hw.fb;
214 	int width, height;
215 
216 	if (plane->id == PLANE_CURSOR)
217 		return 0;
218 
219 	if (!plane_state->uapi.visible)
220 		return 0;
221 
222 	/*
223 	 * We calculate extra ddb based on ratio plane rate/total data rate
224 	 * in case, in some cases we should not allocate extra ddb for the plane,
225 	 * so do not count its data rate, if this is the case.
226 	 */
227 	if (use_min_ddb(crtc_state, plane))
228 		return 0;
229 
230 	/*
231 	 * Src coordinates are already rotated by 270 degrees for
232 	 * the 90/270 degree plane rotation cases (to match the
233 	 * GTT mapping), hence no need to account for rotation here.
234 	 */
235 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
236 	height = drm_rect_height(&plane_state->uapi.src) >> 16;
237 
238 	/* UV plane does 1/2 pixel sub-sampling */
239 	if (color_plane == 1) {
240 		width /= 2;
241 		height /= 2;
242 	}
243 
244 	return width * height * fb->format->cpp[color_plane];
245 }
246 
247 int intel_plane_calc_min_cdclk(struct intel_atomic_state *state,
248 			       struct intel_plane *plane,
249 			       bool *need_cdclk_calc)
250 {
251 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
252 	const struct intel_plane_state *plane_state =
253 		intel_atomic_get_new_plane_state(state, plane);
254 	struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
255 	const struct intel_cdclk_state *cdclk_state;
256 	const struct intel_crtc_state *old_crtc_state;
257 	struct intel_crtc_state *new_crtc_state;
258 
259 	if (!plane_state->uapi.visible || !plane->min_cdclk)
260 		return 0;
261 
262 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
263 	new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
264 
265 	new_crtc_state->min_cdclk[plane->id] =
266 		plane->min_cdclk(new_crtc_state, plane_state);
267 
268 	/*
269 	 * No need to check against the cdclk state if
270 	 * the min cdclk for the plane doesn't increase.
271 	 *
272 	 * Ie. we only ever increase the cdclk due to plane
273 	 * requirements. This can reduce back and forth
274 	 * display blinking due to constant cdclk changes.
275 	 */
276 	if (new_crtc_state->min_cdclk[plane->id] <=
277 	    old_crtc_state->min_cdclk[plane->id])
278 		return 0;
279 
280 	cdclk_state = intel_atomic_get_cdclk_state(state);
281 	if (IS_ERR(cdclk_state))
282 		return PTR_ERR(cdclk_state);
283 
284 	/*
285 	 * No need to recalculate the cdclk state if
286 	 * the min cdclk for the pipe doesn't increase.
287 	 *
288 	 * Ie. we only ever increase the cdclk due to plane
289 	 * requirements. This can reduce back and forth
290 	 * display blinking due to constant cdclk changes.
291 	 */
292 	if (new_crtc_state->min_cdclk[plane->id] <=
293 	    cdclk_state->min_cdclk[crtc->pipe])
294 		return 0;
295 
296 	drm_dbg_kms(&dev_priv->drm,
297 		    "[PLANE:%d:%s] min cdclk (%d kHz) > [CRTC:%d:%s] min cdclk (%d kHz)\n",
298 		    plane->base.base.id, plane->base.name,
299 		    new_crtc_state->min_cdclk[plane->id],
300 		    crtc->base.base.id, crtc->base.name,
301 		    cdclk_state->min_cdclk[crtc->pipe]);
302 	*need_cdclk_calc = true;
303 
304 	return 0;
305 }
306 
307 static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state)
308 {
309 	if (plane_state->hw.fb)
310 		drm_framebuffer_put(plane_state->hw.fb);
311 
312 	memset(&plane_state->hw, 0, sizeof(plane_state->hw));
313 }
314 
315 void intel_plane_copy_uapi_to_hw_state(struct intel_plane_state *plane_state,
316 				       const struct intel_plane_state *from_plane_state,
317 				       struct intel_crtc *crtc)
318 {
319 	intel_plane_clear_hw_state(plane_state);
320 
321 	/*
322 	 * For the bigjoiner slave uapi.crtc will point at
323 	 * the master crtc. So we explicitly assign the right
324 	 * slave crtc to hw.crtc. uapi.crtc!=NULL simply indicates
325 	 * the plane is logically enabled on the uapi level.
326 	 */
327 	plane_state->hw.crtc = from_plane_state->uapi.crtc ? &crtc->base : NULL;
328 
329 	plane_state->hw.fb = from_plane_state->uapi.fb;
330 	if (plane_state->hw.fb)
331 		drm_framebuffer_get(plane_state->hw.fb);
332 
333 	plane_state->hw.alpha = from_plane_state->uapi.alpha;
334 	plane_state->hw.pixel_blend_mode =
335 		from_plane_state->uapi.pixel_blend_mode;
336 	plane_state->hw.rotation = from_plane_state->uapi.rotation;
337 	plane_state->hw.color_encoding = from_plane_state->uapi.color_encoding;
338 	plane_state->hw.color_range = from_plane_state->uapi.color_range;
339 	plane_state->hw.scaling_filter = from_plane_state->uapi.scaling_filter;
340 
341 	plane_state->uapi.src = drm_plane_state_src(&from_plane_state->uapi);
342 	plane_state->uapi.dst = drm_plane_state_dest(&from_plane_state->uapi);
343 }
344 
345 void intel_plane_copy_hw_state(struct intel_plane_state *plane_state,
346 			       const struct intel_plane_state *from_plane_state)
347 {
348 	intel_plane_clear_hw_state(plane_state);
349 
350 	memcpy(&plane_state->hw, &from_plane_state->hw,
351 	       sizeof(plane_state->hw));
352 
353 	if (plane_state->hw.fb)
354 		drm_framebuffer_get(plane_state->hw.fb);
355 }
356 
357 void intel_plane_set_invisible(struct intel_crtc_state *crtc_state,
358 			       struct intel_plane_state *plane_state)
359 {
360 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
361 
362 	crtc_state->active_planes &= ~BIT(plane->id);
363 	crtc_state->scaled_planes &= ~BIT(plane->id);
364 	crtc_state->nv12_planes &= ~BIT(plane->id);
365 	crtc_state->c8_planes &= ~BIT(plane->id);
366 	crtc_state->data_rate[plane->id] = 0;
367 	crtc_state->data_rate_y[plane->id] = 0;
368 	crtc_state->rel_data_rate[plane->id] = 0;
369 	crtc_state->rel_data_rate_y[plane->id] = 0;
370 	crtc_state->min_cdclk[plane->id] = 0;
371 
372 	plane_state->uapi.visible = false;
373 }
374 
375 /* FIXME nuke when all wm code is atomic */
376 static bool intel_wm_need_update(const struct intel_plane_state *cur,
377 				 struct intel_plane_state *new)
378 {
379 	/* Update watermarks on tiling or size changes. */
380 	if (new->uapi.visible != cur->uapi.visible)
381 		return true;
382 
383 	if (!cur->hw.fb || !new->hw.fb)
384 		return false;
385 
386 	if (cur->hw.fb->modifier != new->hw.fb->modifier ||
387 	    cur->hw.rotation != new->hw.rotation ||
388 	    drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
389 	    drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
390 	    drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
391 	    drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
392 		return true;
393 
394 	return false;
395 }
396 
397 static bool intel_plane_is_scaled(const struct intel_plane_state *plane_state)
398 {
399 	int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
400 	int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
401 	int dst_w = drm_rect_width(&plane_state->uapi.dst);
402 	int dst_h = drm_rect_height(&plane_state->uapi.dst);
403 
404 	return src_w != dst_w || src_h != dst_h;
405 }
406 
407 static bool intel_plane_do_async_flip(struct intel_plane *plane,
408 				      const struct intel_crtc_state *old_crtc_state,
409 				      const struct intel_crtc_state *new_crtc_state)
410 {
411 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
412 
413 	if (!plane->async_flip)
414 		return false;
415 
416 	if (!new_crtc_state->uapi.async_flip)
417 		return false;
418 
419 	/*
420 	 * In platforms after DISPLAY13, we might need to override
421 	 * first async flip in order to change watermark levels
422 	 * as part of optimization.
423 	 * So for those, we are checking if this is a first async flip.
424 	 * For platforms earlier than DISPLAY13 we always do async flip.
425 	 */
426 	return DISPLAY_VER(i915) < 13 || old_crtc_state->uapi.async_flip;
427 }
428 
429 static bool i9xx_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state,
430 				   const struct intel_plane_state *old_plane_state,
431 				   const struct intel_plane_state *new_plane_state)
432 {
433 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
434 	bool old_visible = old_plane_state->uapi.visible;
435 	bool new_visible = new_plane_state->uapi.visible;
436 	u32 old_ctl = old_plane_state->ctl;
437 	u32 new_ctl = new_plane_state->ctl;
438 	bool modeset, turn_on, turn_off;
439 
440 	if (plane->id == PLANE_CURSOR)
441 		return false;
442 
443 	modeset = intel_crtc_needs_modeset(new_crtc_state);
444 	turn_off = old_visible && (!new_visible || modeset);
445 	turn_on = new_visible && (!old_visible || modeset);
446 
447 	/* Must disable CxSR around plane enable/disable */
448 	if (turn_on || turn_off)
449 		return true;
450 
451 	if (!old_visible || !new_visible)
452 		return false;
453 
454 	/*
455 	 * Most plane control register updates are blocked while in CxSR.
456 	 *
457 	 * Tiling mode is one exception where the primary plane can
458 	 * apparently handle it, whereas the sprites can not (the
459 	 * sprite issue being only relevant on VLV/CHV where CxSR
460 	 * is actually possible with a sprite enabled).
461 	 */
462 	if (plane->id == PLANE_PRIMARY) {
463 		old_ctl &= ~DISP_TILED;
464 		new_ctl &= ~DISP_TILED;
465 	}
466 
467 	return old_ctl != new_ctl;
468 }
469 
470 static int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
471 					   struct intel_crtc_state *new_crtc_state,
472 					   const struct intel_plane_state *old_plane_state,
473 					   struct intel_plane_state *new_plane_state)
474 {
475 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
476 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
477 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
478 	bool mode_changed = intel_crtc_needs_modeset(new_crtc_state);
479 	bool was_crtc_enabled = old_crtc_state->hw.active;
480 	bool is_crtc_enabled = new_crtc_state->hw.active;
481 	bool turn_off, turn_on, visible, was_visible;
482 	int ret;
483 
484 	if (DISPLAY_VER(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
485 		ret = skl_update_scaler_plane(new_crtc_state, new_plane_state);
486 		if (ret)
487 			return ret;
488 	}
489 
490 	was_visible = old_plane_state->uapi.visible;
491 	visible = new_plane_state->uapi.visible;
492 
493 	if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
494 		was_visible = false;
495 
496 	/*
497 	 * Visibility is calculated as if the crtc was on, but
498 	 * after scaler setup everything depends on it being off
499 	 * when the crtc isn't active.
500 	 *
501 	 * FIXME this is wrong for watermarks. Watermarks should also
502 	 * be computed as if the pipe would be active. Perhaps move
503 	 * per-plane wm computation to the .check_plane() hook, and
504 	 * only combine the results from all planes in the current place?
505 	 */
506 	if (!is_crtc_enabled) {
507 		intel_plane_set_invisible(new_crtc_state, new_plane_state);
508 		visible = false;
509 	}
510 
511 	if (!was_visible && !visible)
512 		return 0;
513 
514 	turn_off = was_visible && (!visible || mode_changed);
515 	turn_on = visible && (!was_visible || mode_changed);
516 
517 	drm_dbg_atomic(&dev_priv->drm,
518 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
519 		       crtc->base.base.id, crtc->base.name,
520 		       plane->base.base.id, plane->base.name,
521 		       was_visible, visible,
522 		       turn_off, turn_on, mode_changed);
523 
524 	if (turn_on) {
525 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
526 			new_crtc_state->update_wm_pre = true;
527 	} else if (turn_off) {
528 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
529 			new_crtc_state->update_wm_post = true;
530 	} else if (intel_wm_need_update(old_plane_state, new_plane_state)) {
531 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv)) {
532 			/* FIXME bollocks */
533 			new_crtc_state->update_wm_pre = true;
534 			new_crtc_state->update_wm_post = true;
535 		}
536 	}
537 
538 	if (visible || was_visible)
539 		new_crtc_state->fb_bits |= plane->frontbuffer_bit;
540 
541 	if (HAS_GMCH(dev_priv) &&
542 	    i9xx_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
543 		new_crtc_state->disable_cxsr = true;
544 
545 	/*
546 	 * ILK/SNB DVSACNTR/Sprite Enable
547 	 * IVB SPR_CTL/Sprite Enable
548 	 * "When in Self Refresh Big FIFO mode, a write to enable the
549 	 *  plane will be internally buffered and delayed while Big FIFO
550 	 *  mode is exiting."
551 	 *
552 	 * Which means that enabling the sprite can take an extra frame
553 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
554 	 * down to LP0 and wait for vblank in order to make sure the
555 	 * sprite gets enabled on the next vblank after the register write.
556 	 * Doing otherwise would risk enabling the sprite one frame after
557 	 * we've already signalled flip completion. We can resume LP1+
558 	 * once the sprite has been enabled.
559 	 *
560 	 *
561 	 * WaCxSRDisabledForSpriteScaling:ivb
562 	 * IVB SPR_SCALE/Scaling Enable
563 	 * "Low Power watermarks must be disabled for at least one
564 	 *  frame before enabling sprite scaling, and kept disabled
565 	 *  until sprite scaling is disabled."
566 	 *
567 	 * ILK/SNB DVSASCALE/Scaling Enable
568 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
569 	 *  masked off while Big FIFO mode is exiting."
570 	 *
571 	 * Despite the w/a only being listed for IVB we assume that
572 	 * the ILK/SNB note has similar ramifications, hence we apply
573 	 * the w/a on all three platforms.
574 	 *
575 	 * With experimental results seems this is needed also for primary
576 	 * plane, not only sprite plane.
577 	 */
578 	if (plane->id != PLANE_CURSOR &&
579 	    (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) ||
580 	     IS_IVYBRIDGE(dev_priv)) &&
581 	    (turn_on || (!intel_plane_is_scaled(old_plane_state) &&
582 			 intel_plane_is_scaled(new_plane_state))))
583 		new_crtc_state->disable_lp_wm = true;
584 
585 	if (intel_plane_do_async_flip(plane, old_crtc_state, new_crtc_state))
586 		new_crtc_state->do_async_flip = true;
587 
588 	return 0;
589 }
590 
591 int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state,
592 					struct intel_crtc_state *new_crtc_state,
593 					const struct intel_plane_state *old_plane_state,
594 					struct intel_plane_state *new_plane_state)
595 {
596 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
597 	const struct drm_framebuffer *fb = new_plane_state->hw.fb;
598 	int ret;
599 
600 	intel_plane_set_invisible(new_crtc_state, new_plane_state);
601 	new_crtc_state->enabled_planes &= ~BIT(plane->id);
602 
603 	if (!new_plane_state->hw.crtc && !old_plane_state->hw.crtc)
604 		return 0;
605 
606 	ret = plane->check_plane(new_crtc_state, new_plane_state);
607 	if (ret)
608 		return ret;
609 
610 	if (fb)
611 		new_crtc_state->enabled_planes |= BIT(plane->id);
612 
613 	/* FIXME pre-g4x don't work like this */
614 	if (new_plane_state->uapi.visible)
615 		new_crtc_state->active_planes |= BIT(plane->id);
616 
617 	if (new_plane_state->uapi.visible &&
618 	    intel_plane_is_scaled(new_plane_state))
619 		new_crtc_state->scaled_planes |= BIT(plane->id);
620 
621 	if (new_plane_state->uapi.visible &&
622 	    intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
623 		new_crtc_state->nv12_planes |= BIT(plane->id);
624 
625 	if (new_plane_state->uapi.visible &&
626 	    fb->format->format == DRM_FORMAT_C8)
627 		new_crtc_state->c8_planes |= BIT(plane->id);
628 
629 	if (new_plane_state->uapi.visible || old_plane_state->uapi.visible)
630 		new_crtc_state->update_planes |= BIT(plane->id);
631 
632 	if (new_plane_state->uapi.visible &&
633 	    intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
634 		new_crtc_state->data_rate_y[plane->id] =
635 			intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
636 		new_crtc_state->data_rate[plane->id] =
637 			intel_plane_data_rate(new_crtc_state, new_plane_state, 1);
638 
639 		new_crtc_state->rel_data_rate_y[plane->id] =
640 			intel_plane_relative_data_rate(new_crtc_state,
641 						       new_plane_state, 0);
642 		new_crtc_state->rel_data_rate[plane->id] =
643 			intel_plane_relative_data_rate(new_crtc_state,
644 						       new_plane_state, 1);
645 	} else if (new_plane_state->uapi.visible) {
646 		new_crtc_state->data_rate[plane->id] =
647 			intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
648 
649 		new_crtc_state->rel_data_rate[plane->id] =
650 			intel_plane_relative_data_rate(new_crtc_state,
651 						       new_plane_state, 0);
652 	}
653 
654 	return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state,
655 					       old_plane_state, new_plane_state);
656 }
657 
658 static struct intel_plane *
659 intel_crtc_get_plane(struct intel_crtc *crtc, enum plane_id plane_id)
660 {
661 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
662 	struct intel_plane *plane;
663 
664 	for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
665 		if (plane->id == plane_id)
666 			return plane;
667 	}
668 
669 	return NULL;
670 }
671 
672 int intel_plane_atomic_check(struct intel_atomic_state *state,
673 			     struct intel_plane *plane)
674 {
675 	struct drm_i915_private *i915 = to_i915(state->base.dev);
676 	struct intel_plane_state *new_plane_state =
677 		intel_atomic_get_new_plane_state(state, plane);
678 	const struct intel_plane_state *old_plane_state =
679 		intel_atomic_get_old_plane_state(state, plane);
680 	const struct intel_plane_state *new_master_plane_state;
681 	struct intel_crtc *crtc = intel_crtc_for_pipe(i915, plane->pipe);
682 	const struct intel_crtc_state *old_crtc_state =
683 		intel_atomic_get_old_crtc_state(state, crtc);
684 	struct intel_crtc_state *new_crtc_state =
685 		intel_atomic_get_new_crtc_state(state, crtc);
686 
687 	if (new_crtc_state && intel_crtc_is_bigjoiner_slave(new_crtc_state)) {
688 		struct intel_crtc *master_crtc =
689 			intel_master_crtc(new_crtc_state);
690 		struct intel_plane *master_plane =
691 			intel_crtc_get_plane(master_crtc, plane->id);
692 
693 		new_master_plane_state =
694 			intel_atomic_get_new_plane_state(state, master_plane);
695 	} else {
696 		new_master_plane_state = new_plane_state;
697 	}
698 
699 	intel_plane_copy_uapi_to_hw_state(new_plane_state,
700 					  new_master_plane_state,
701 					  crtc);
702 
703 	new_plane_state->uapi.visible = false;
704 	if (!new_crtc_state)
705 		return 0;
706 
707 	return intel_plane_atomic_check_with_state(old_crtc_state,
708 						   new_crtc_state,
709 						   old_plane_state,
710 						   new_plane_state);
711 }
712 
713 static struct intel_plane *
714 skl_next_plane_to_commit(struct intel_atomic_state *state,
715 			 struct intel_crtc *crtc,
716 			 struct skl_ddb_entry ddb[I915_MAX_PLANES],
717 			 struct skl_ddb_entry ddb_y[I915_MAX_PLANES],
718 			 unsigned int *update_mask)
719 {
720 	struct intel_crtc_state *crtc_state =
721 		intel_atomic_get_new_crtc_state(state, crtc);
722 	struct intel_plane_state *plane_state;
723 	struct intel_plane *plane;
724 	int i;
725 
726 	if (*update_mask == 0)
727 		return NULL;
728 
729 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
730 		enum plane_id plane_id = plane->id;
731 
732 		if (crtc->pipe != plane->pipe ||
733 		    !(*update_mask & BIT(plane_id)))
734 			continue;
735 
736 		if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb[plane_id],
737 						ddb, I915_MAX_PLANES, plane_id) ||
738 		    skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
739 						ddb_y, I915_MAX_PLANES, plane_id))
740 			continue;
741 
742 		*update_mask &= ~BIT(plane_id);
743 		ddb[plane_id] = crtc_state->wm.skl.plane_ddb[plane_id];
744 		ddb_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
745 
746 		return plane;
747 	}
748 
749 	/* should never happen */
750 	drm_WARN_ON(state->base.dev, 1);
751 
752 	return NULL;
753 }
754 
755 void intel_plane_update_noarm(struct intel_plane *plane,
756 			      const struct intel_crtc_state *crtc_state,
757 			      const struct intel_plane_state *plane_state)
758 {
759 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
760 
761 	trace_intel_plane_update_noarm(plane, crtc);
762 
763 	if (plane->update_noarm)
764 		plane->update_noarm(plane, crtc_state, plane_state);
765 }
766 
767 void intel_plane_update_arm(struct intel_plane *plane,
768 			    const struct intel_crtc_state *crtc_state,
769 			    const struct intel_plane_state *plane_state)
770 {
771 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
772 
773 	trace_intel_plane_update_arm(plane, crtc);
774 
775 	if (crtc_state->do_async_flip && plane->async_flip)
776 		plane->async_flip(plane, crtc_state, plane_state, true);
777 	else
778 		plane->update_arm(plane, crtc_state, plane_state);
779 }
780 
781 void intel_plane_disable_arm(struct intel_plane *plane,
782 			     const struct intel_crtc_state *crtc_state)
783 {
784 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
785 
786 	trace_intel_plane_disable_arm(plane, crtc);
787 	plane->disable_arm(plane, crtc_state);
788 }
789 
790 void intel_crtc_planes_update_noarm(struct intel_atomic_state *state,
791 				    struct intel_crtc *crtc)
792 {
793 	struct intel_crtc_state *new_crtc_state =
794 		intel_atomic_get_new_crtc_state(state, crtc);
795 	u32 update_mask = new_crtc_state->update_planes;
796 	struct intel_plane_state *new_plane_state;
797 	struct intel_plane *plane;
798 	int i;
799 
800 	if (new_crtc_state->do_async_flip)
801 		return;
802 
803 	/*
804 	 * Since we only write non-arming registers here,
805 	 * the order does not matter even for skl+.
806 	 */
807 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
808 		if (crtc->pipe != plane->pipe ||
809 		    !(update_mask & BIT(plane->id)))
810 			continue;
811 
812 		/* TODO: for mailbox updates this should be skipped */
813 		if (new_plane_state->uapi.visible ||
814 		    new_plane_state->planar_slave)
815 			intel_plane_update_noarm(plane, new_crtc_state, new_plane_state);
816 	}
817 }
818 
819 static void skl_crtc_planes_update_arm(struct intel_atomic_state *state,
820 				       struct intel_crtc *crtc)
821 {
822 	struct intel_crtc_state *old_crtc_state =
823 		intel_atomic_get_old_crtc_state(state, crtc);
824 	struct intel_crtc_state *new_crtc_state =
825 		intel_atomic_get_new_crtc_state(state, crtc);
826 	struct skl_ddb_entry ddb[I915_MAX_PLANES];
827 	struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
828 	u32 update_mask = new_crtc_state->update_planes;
829 	struct intel_plane *plane;
830 
831 	memcpy(ddb, old_crtc_state->wm.skl.plane_ddb,
832 	       sizeof(old_crtc_state->wm.skl.plane_ddb));
833 	memcpy(ddb_y, old_crtc_state->wm.skl.plane_ddb_y,
834 	       sizeof(old_crtc_state->wm.skl.plane_ddb_y));
835 
836 	while ((plane = skl_next_plane_to_commit(state, crtc, ddb, ddb_y, &update_mask))) {
837 		struct intel_plane_state *new_plane_state =
838 			intel_atomic_get_new_plane_state(state, plane);
839 
840 		/*
841 		 * TODO: for mailbox updates intel_plane_update_noarm()
842 		 * would have to be called here as well.
843 		 */
844 		if (new_plane_state->uapi.visible ||
845 		    new_plane_state->planar_slave)
846 			intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
847 		else
848 			intel_plane_disable_arm(plane, new_crtc_state);
849 	}
850 }
851 
852 static void i9xx_crtc_planes_update_arm(struct intel_atomic_state *state,
853 					struct intel_crtc *crtc)
854 {
855 	struct intel_crtc_state *new_crtc_state =
856 		intel_atomic_get_new_crtc_state(state, crtc);
857 	u32 update_mask = new_crtc_state->update_planes;
858 	struct intel_plane_state *new_plane_state;
859 	struct intel_plane *plane;
860 	int i;
861 
862 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
863 		if (crtc->pipe != plane->pipe ||
864 		    !(update_mask & BIT(plane->id)))
865 			continue;
866 
867 		/*
868 		 * TODO: for mailbox updates intel_plane_update_noarm()
869 		 * would have to be called here as well.
870 		 */
871 		if (new_plane_state->uapi.visible)
872 			intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
873 		else
874 			intel_plane_disable_arm(plane, new_crtc_state);
875 	}
876 }
877 
878 void intel_crtc_planes_update_arm(struct intel_atomic_state *state,
879 				  struct intel_crtc *crtc)
880 {
881 	struct drm_i915_private *i915 = to_i915(state->base.dev);
882 
883 	if (DISPLAY_VER(i915) >= 9)
884 		skl_crtc_planes_update_arm(state, crtc);
885 	else
886 		i9xx_crtc_planes_update_arm(state, crtc);
887 }
888 
889 int intel_atomic_plane_check_clipping(struct intel_plane_state *plane_state,
890 				      struct intel_crtc_state *crtc_state,
891 				      int min_scale, int max_scale,
892 				      bool can_position)
893 {
894 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
895 	struct drm_framebuffer *fb = plane_state->hw.fb;
896 	struct drm_rect *src = &plane_state->uapi.src;
897 	struct drm_rect *dst = &plane_state->uapi.dst;
898 	const struct drm_rect *clip = &crtc_state->pipe_src;
899 	unsigned int rotation = plane_state->hw.rotation;
900 	int hscale, vscale;
901 
902 	if (!fb) {
903 		plane_state->uapi.visible = false;
904 		return 0;
905 	}
906 
907 	drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
908 
909 	/* Check scaling */
910 	hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
911 	vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
912 	if (hscale < 0 || vscale < 0) {
913 		drm_dbg_kms(&i915->drm, "Invalid scaling of plane\n");
914 		drm_rect_debug_print("src: ", src, true);
915 		drm_rect_debug_print("dst: ", dst, false);
916 		return -ERANGE;
917 	}
918 
919 	/*
920 	 * FIXME: This might need further adjustment for seamless scaling
921 	 * with phase information, for the 2p2 and 2p1 scenarios.
922 	 */
923 	plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, clip);
924 
925 	drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
926 
927 	if (!can_position && plane_state->uapi.visible &&
928 	    !drm_rect_equals(dst, clip)) {
929 		drm_dbg_kms(&i915->drm, "Plane must cover entire CRTC\n");
930 		drm_rect_debug_print("dst: ", dst, false);
931 		drm_rect_debug_print("clip: ", clip, false);
932 		return -EINVAL;
933 	}
934 
935 	/* final plane coordinates will be relative to the plane's pipe */
936 	drm_rect_translate(dst, -clip->x1, -clip->y1);
937 
938 	return 0;
939 }
940 
941 struct wait_rps_boost {
942 	struct wait_queue_entry wait;
943 
944 	struct drm_crtc *crtc;
945 	struct i915_request *request;
946 };
947 
948 static int do_rps_boost(struct wait_queue_entry *_wait,
949 			unsigned mode, int sync, void *key)
950 {
951 	struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
952 	struct i915_request *rq = wait->request;
953 
954 	/*
955 	 * If we missed the vblank, but the request is already running it
956 	 * is reasonable to assume that it will complete before the next
957 	 * vblank without our intervention, so leave RPS alone.
958 	 */
959 	if (!i915_request_started(rq))
960 		intel_rps_boost(rq);
961 	i915_request_put(rq);
962 
963 	drm_crtc_vblank_put(wait->crtc);
964 
965 	list_del(&wait->wait.entry);
966 	kfree(wait);
967 	return 1;
968 }
969 
970 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
971 				       struct dma_fence *fence)
972 {
973 	struct wait_rps_boost *wait;
974 
975 	if (!dma_fence_is_i915(fence))
976 		return;
977 
978 	if (DISPLAY_VER(to_i915(crtc->dev)) < 6)
979 		return;
980 
981 	if (drm_crtc_vblank_get(crtc))
982 		return;
983 
984 	wait = kmalloc(sizeof(*wait), GFP_KERNEL);
985 	if (!wait) {
986 		drm_crtc_vblank_put(crtc);
987 		return;
988 	}
989 
990 	wait->request = to_request(dma_fence_get(fence));
991 	wait->crtc = crtc;
992 
993 	wait->wait.func = do_rps_boost;
994 	wait->wait.flags = 0;
995 
996 	add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
997 }
998 
999 /**
1000  * intel_prepare_plane_fb - Prepare fb for usage on plane
1001  * @_plane: drm plane to prepare for
1002  * @_new_plane_state: the plane state being prepared
1003  *
1004  * Prepares a framebuffer for usage on a display plane.  Generally this
1005  * involves pinning the underlying object and updating the frontbuffer tracking
1006  * bits.  Some older platforms need special physical address handling for
1007  * cursor planes.
1008  *
1009  * Returns 0 on success, negative error code on failure.
1010  */
1011 static int
1012 intel_prepare_plane_fb(struct drm_plane *_plane,
1013 		       struct drm_plane_state *_new_plane_state)
1014 {
1015 	struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY };
1016 	struct intel_plane *plane = to_intel_plane(_plane);
1017 	struct intel_plane_state *new_plane_state =
1018 		to_intel_plane_state(_new_plane_state);
1019 	struct intel_atomic_state *state =
1020 		to_intel_atomic_state(new_plane_state->uapi.state);
1021 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1022 	const struct intel_plane_state *old_plane_state =
1023 		intel_atomic_get_old_plane_state(state, plane);
1024 	struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
1025 	struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
1026 	int ret;
1027 
1028 	if (old_obj) {
1029 		const struct intel_crtc_state *crtc_state =
1030 			intel_atomic_get_new_crtc_state(state,
1031 							to_intel_crtc(old_plane_state->hw.crtc));
1032 
1033 		/* Big Hammer, we also need to ensure that any pending
1034 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1035 		 * current scanout is retired before unpinning the old
1036 		 * framebuffer. Note that we rely on userspace rendering
1037 		 * into the buffer attached to the pipe they are waiting
1038 		 * on. If not, userspace generates a GPU hang with IPEHR
1039 		 * point to the MI_WAIT_FOR_EVENT.
1040 		 *
1041 		 * This should only fail upon a hung GPU, in which case we
1042 		 * can safely continue.
1043 		 */
1044 		if (intel_crtc_needs_modeset(crtc_state)) {
1045 			ret = i915_sw_fence_await_reservation(&state->commit_ready,
1046 							      old_obj->base.resv,
1047 							      false, 0,
1048 							      GFP_KERNEL);
1049 			if (ret < 0)
1050 				return ret;
1051 		}
1052 	}
1053 
1054 	if (new_plane_state->uapi.fence) { /* explicit fencing */
1055 		i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
1056 					     &attr);
1057 		ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
1058 						    new_plane_state->uapi.fence,
1059 						    i915_fence_timeout(dev_priv),
1060 						    GFP_KERNEL);
1061 		if (ret < 0)
1062 			return ret;
1063 	}
1064 
1065 	if (!obj)
1066 		return 0;
1067 
1068 
1069 	ret = intel_plane_pin_fb(new_plane_state);
1070 	if (ret)
1071 		return ret;
1072 
1073 	i915_gem_object_wait_priority(obj, 0, &attr);
1074 
1075 	if (!new_plane_state->uapi.fence) { /* implicit fencing */
1076 		struct dma_resv_iter cursor;
1077 		struct dma_fence *fence;
1078 
1079 		ret = i915_sw_fence_await_reservation(&state->commit_ready,
1080 						      obj->base.resv, false,
1081 						      i915_fence_timeout(dev_priv),
1082 						      GFP_KERNEL);
1083 		if (ret < 0)
1084 			goto unpin_fb;
1085 
1086 		dma_resv_iter_begin(&cursor, obj->base.resv,
1087 				    DMA_RESV_USAGE_WRITE);
1088 		dma_resv_for_each_fence_unlocked(&cursor, fence) {
1089 			add_rps_boost_after_vblank(new_plane_state->hw.crtc,
1090 						   fence);
1091 		}
1092 		dma_resv_iter_end(&cursor);
1093 	} else {
1094 		add_rps_boost_after_vblank(new_plane_state->hw.crtc,
1095 					   new_plane_state->uapi.fence);
1096 	}
1097 
1098 	/*
1099 	 * We declare pageflips to be interactive and so merit a small bias
1100 	 * towards upclocking to deliver the frame on time. By only changing
1101 	 * the RPS thresholds to sample more regularly and aim for higher
1102 	 * clocks we can hopefully deliver low power workloads (like kodi)
1103 	 * that are not quite steady state without resorting to forcing
1104 	 * maximum clocks following a vblank miss (see do_rps_boost()).
1105 	 */
1106 	if (!state->rps_interactive) {
1107 		intel_rps_mark_interactive(&to_gt(dev_priv)->rps, true);
1108 		state->rps_interactive = true;
1109 	}
1110 
1111 	return 0;
1112 
1113 unpin_fb:
1114 	intel_plane_unpin_fb(new_plane_state);
1115 
1116 	return ret;
1117 }
1118 
1119 /**
1120  * intel_cleanup_plane_fb - Cleans up an fb after plane use
1121  * @plane: drm plane to clean up for
1122  * @_old_plane_state: the state from the previous modeset
1123  *
1124  * Cleans up a framebuffer that has just been removed from a plane.
1125  */
1126 static void
1127 intel_cleanup_plane_fb(struct drm_plane *plane,
1128 		       struct drm_plane_state *_old_plane_state)
1129 {
1130 	struct intel_plane_state *old_plane_state =
1131 		to_intel_plane_state(_old_plane_state);
1132 	struct intel_atomic_state *state =
1133 		to_intel_atomic_state(old_plane_state->uapi.state);
1134 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
1135 	struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
1136 
1137 	if (!obj)
1138 		return;
1139 
1140 	if (state->rps_interactive) {
1141 		intel_rps_mark_interactive(&to_gt(dev_priv)->rps, false);
1142 		state->rps_interactive = false;
1143 	}
1144 
1145 	/* Should only be called after a successful intel_prepare_plane_fb()! */
1146 	intel_plane_unpin_fb(old_plane_state);
1147 }
1148 
1149 static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1150 	.prepare_fb = intel_prepare_plane_fb,
1151 	.cleanup_fb = intel_cleanup_plane_fb,
1152 };
1153 
1154 void intel_plane_helper_add(struct intel_plane *plane)
1155 {
1156 	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
1157 }
1158