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 #include <drm/drm_plane_helper.h>
37 
38 #include "gt/intel_rps.h"
39 
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_pm.h"
47 #include "intel_sprite.h"
48 #include "skl_scaler.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 {
186 	const struct drm_framebuffer *fb = plane_state->hw.fb;
187 	unsigned int cpp;
188 	unsigned int pixel_rate;
189 
190 	if (!plane_state->uapi.visible)
191 		return 0;
192 
193 	pixel_rate = intel_plane_pixel_rate(crtc_state, plane_state);
194 
195 	cpp = fb->format->cpp[0];
196 
197 	/*
198 	 * Based on HSD#:1408715493
199 	 * NV12 cpp == 4, P010 cpp == 8
200 	 *
201 	 * FIXME what is the logic behind this?
202 	 */
203 	if (fb->format->is_yuv && fb->format->num_planes > 1)
204 		cpp *= 4;
205 
206 	return pixel_rate * cpp;
207 }
208 
209 int intel_plane_calc_min_cdclk(struct intel_atomic_state *state,
210 			       struct intel_plane *plane,
211 			       bool *need_cdclk_calc)
212 {
213 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
214 	const struct intel_plane_state *plane_state =
215 		intel_atomic_get_new_plane_state(state, plane);
216 	struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
217 	const struct intel_cdclk_state *cdclk_state;
218 	const struct intel_crtc_state *old_crtc_state;
219 	struct intel_crtc_state *new_crtc_state;
220 
221 	if (!plane_state->uapi.visible || !plane->min_cdclk)
222 		return 0;
223 
224 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
225 	new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
226 
227 	new_crtc_state->min_cdclk[plane->id] =
228 		plane->min_cdclk(new_crtc_state, plane_state);
229 
230 	/*
231 	 * No need to check against the cdclk state if
232 	 * the min cdclk for the plane doesn't increase.
233 	 *
234 	 * Ie. we only ever increase the cdclk due to plane
235 	 * requirements. This can reduce back and forth
236 	 * display blinking due to constant cdclk changes.
237 	 */
238 	if (new_crtc_state->min_cdclk[plane->id] <=
239 	    old_crtc_state->min_cdclk[plane->id])
240 		return 0;
241 
242 	cdclk_state = intel_atomic_get_cdclk_state(state);
243 	if (IS_ERR(cdclk_state))
244 		return PTR_ERR(cdclk_state);
245 
246 	/*
247 	 * No need to recalculate the cdclk state if
248 	 * the min cdclk for the pipe doesn't increase.
249 	 *
250 	 * Ie. we only ever increase the cdclk due to plane
251 	 * requirements. This can reduce back and forth
252 	 * display blinking due to constant cdclk changes.
253 	 */
254 	if (new_crtc_state->min_cdclk[plane->id] <=
255 	    cdclk_state->min_cdclk[crtc->pipe])
256 		return 0;
257 
258 	drm_dbg_kms(&dev_priv->drm,
259 		    "[PLANE:%d:%s] min cdclk (%d kHz) > [CRTC:%d:%s] min cdclk (%d kHz)\n",
260 		    plane->base.base.id, plane->base.name,
261 		    new_crtc_state->min_cdclk[plane->id],
262 		    crtc->base.base.id, crtc->base.name,
263 		    cdclk_state->min_cdclk[crtc->pipe]);
264 	*need_cdclk_calc = true;
265 
266 	return 0;
267 }
268 
269 static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state)
270 {
271 	if (plane_state->hw.fb)
272 		drm_framebuffer_put(plane_state->hw.fb);
273 
274 	memset(&plane_state->hw, 0, sizeof(plane_state->hw));
275 }
276 
277 void intel_plane_copy_uapi_to_hw_state(struct intel_plane_state *plane_state,
278 				       const struct intel_plane_state *from_plane_state,
279 				       struct intel_crtc *crtc)
280 {
281 	intel_plane_clear_hw_state(plane_state);
282 
283 	/*
284 	 * For the bigjoiner slave uapi.crtc will point at
285 	 * the master crtc. So we explicitly assign the right
286 	 * slave crtc to hw.crtc. uapi.crtc!=NULL simply indicates
287 	 * the plane is logically enabled on the uapi level.
288 	 */
289 	plane_state->hw.crtc = from_plane_state->uapi.crtc ? &crtc->base : NULL;
290 
291 	plane_state->hw.fb = from_plane_state->uapi.fb;
292 	if (plane_state->hw.fb)
293 		drm_framebuffer_get(plane_state->hw.fb);
294 
295 	plane_state->hw.alpha = from_plane_state->uapi.alpha;
296 	plane_state->hw.pixel_blend_mode =
297 		from_plane_state->uapi.pixel_blend_mode;
298 	plane_state->hw.rotation = from_plane_state->uapi.rotation;
299 	plane_state->hw.color_encoding = from_plane_state->uapi.color_encoding;
300 	plane_state->hw.color_range = from_plane_state->uapi.color_range;
301 	plane_state->hw.scaling_filter = from_plane_state->uapi.scaling_filter;
302 
303 	plane_state->uapi.src = drm_plane_state_src(&from_plane_state->uapi);
304 	plane_state->uapi.dst = drm_plane_state_dest(&from_plane_state->uapi);
305 }
306 
307 void intel_plane_copy_hw_state(struct intel_plane_state *plane_state,
308 			       const struct intel_plane_state *from_plane_state)
309 {
310 	intel_plane_clear_hw_state(plane_state);
311 
312 	memcpy(&plane_state->hw, &from_plane_state->hw,
313 	       sizeof(plane_state->hw));
314 
315 	if (plane_state->hw.fb)
316 		drm_framebuffer_get(plane_state->hw.fb);
317 }
318 
319 void intel_plane_set_invisible(struct intel_crtc_state *crtc_state,
320 			       struct intel_plane_state *plane_state)
321 {
322 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
323 
324 	crtc_state->active_planes &= ~BIT(plane->id);
325 	crtc_state->scaled_planes &= ~BIT(plane->id);
326 	crtc_state->nv12_planes &= ~BIT(plane->id);
327 	crtc_state->c8_planes &= ~BIT(plane->id);
328 	crtc_state->data_rate[plane->id] = 0;
329 	crtc_state->min_cdclk[plane->id] = 0;
330 
331 	plane_state->uapi.visible = false;
332 }
333 
334 /* FIXME nuke when all wm code is atomic */
335 static bool intel_wm_need_update(const struct intel_plane_state *cur,
336 				 struct intel_plane_state *new)
337 {
338 	/* Update watermarks on tiling or size changes. */
339 	if (new->uapi.visible != cur->uapi.visible)
340 		return true;
341 
342 	if (!cur->hw.fb || !new->hw.fb)
343 		return false;
344 
345 	if (cur->hw.fb->modifier != new->hw.fb->modifier ||
346 	    cur->hw.rotation != new->hw.rotation ||
347 	    drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
348 	    drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
349 	    drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
350 	    drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
351 		return true;
352 
353 	return false;
354 }
355 
356 static bool intel_plane_is_scaled(const struct intel_plane_state *plane_state)
357 {
358 	int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
359 	int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
360 	int dst_w = drm_rect_width(&plane_state->uapi.dst);
361 	int dst_h = drm_rect_height(&plane_state->uapi.dst);
362 
363 	return src_w != dst_w || src_h != dst_h;
364 }
365 
366 static bool intel_plane_do_async_flip(struct intel_plane *plane,
367 				      const struct intel_crtc_state *old_crtc_state,
368 				      const struct intel_crtc_state *new_crtc_state)
369 {
370 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
371 
372 	if (!plane->async_flip)
373 		return false;
374 
375 	if (!new_crtc_state->uapi.async_flip)
376 		return false;
377 
378 	/*
379 	 * In platforms after DISPLAY13, we might need to override
380 	 * first async flip in order to change watermark levels
381 	 * as part of optimization.
382 	 * So for those, we are checking if this is a first async flip.
383 	 * For platforms earlier than DISPLAY13 we always do async flip.
384 	 */
385 	return DISPLAY_VER(i915) < 13 || old_crtc_state->uapi.async_flip;
386 }
387 
388 static int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
389 					   struct intel_crtc_state *new_crtc_state,
390 					   const struct intel_plane_state *old_plane_state,
391 					   struct intel_plane_state *new_plane_state)
392 {
393 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
394 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
395 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
396 	bool mode_changed = intel_crtc_needs_modeset(new_crtc_state);
397 	bool was_crtc_enabled = old_crtc_state->hw.active;
398 	bool is_crtc_enabled = new_crtc_state->hw.active;
399 	bool turn_off, turn_on, visible, was_visible;
400 	int ret;
401 
402 	if (DISPLAY_VER(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
403 		ret = skl_update_scaler_plane(new_crtc_state, new_plane_state);
404 		if (ret)
405 			return ret;
406 	}
407 
408 	was_visible = old_plane_state->uapi.visible;
409 	visible = new_plane_state->uapi.visible;
410 
411 	if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
412 		was_visible = false;
413 
414 	/*
415 	 * Visibility is calculated as if the crtc was on, but
416 	 * after scaler setup everything depends on it being off
417 	 * when the crtc isn't active.
418 	 *
419 	 * FIXME this is wrong for watermarks. Watermarks should also
420 	 * be computed as if the pipe would be active. Perhaps move
421 	 * per-plane wm computation to the .check_plane() hook, and
422 	 * only combine the results from all planes in the current place?
423 	 */
424 	if (!is_crtc_enabled) {
425 		intel_plane_set_invisible(new_crtc_state, new_plane_state);
426 		visible = false;
427 	}
428 
429 	if (!was_visible && !visible)
430 		return 0;
431 
432 	turn_off = was_visible && (!visible || mode_changed);
433 	turn_on = visible && (!was_visible || mode_changed);
434 
435 	drm_dbg_atomic(&dev_priv->drm,
436 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
437 		       crtc->base.base.id, crtc->base.name,
438 		       plane->base.base.id, plane->base.name,
439 		       was_visible, visible,
440 		       turn_off, turn_on, mode_changed);
441 
442 	if (turn_on) {
443 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
444 			new_crtc_state->update_wm_pre = true;
445 
446 		/* must disable cxsr around plane enable/disable */
447 		if (plane->id != PLANE_CURSOR)
448 			new_crtc_state->disable_cxsr = true;
449 	} else if (turn_off) {
450 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
451 			new_crtc_state->update_wm_post = true;
452 
453 		/* must disable cxsr around plane enable/disable */
454 		if (plane->id != PLANE_CURSOR)
455 			new_crtc_state->disable_cxsr = true;
456 	} else if (intel_wm_need_update(old_plane_state, new_plane_state)) {
457 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv)) {
458 			/* FIXME bollocks */
459 			new_crtc_state->update_wm_pre = true;
460 			new_crtc_state->update_wm_post = true;
461 		}
462 	}
463 
464 	if (visible || was_visible)
465 		new_crtc_state->fb_bits |= plane->frontbuffer_bit;
466 
467 	/*
468 	 * ILK/SNB DVSACNTR/Sprite Enable
469 	 * IVB SPR_CTL/Sprite Enable
470 	 * "When in Self Refresh Big FIFO mode, a write to enable the
471 	 *  plane will be internally buffered and delayed while Big FIFO
472 	 *  mode is exiting."
473 	 *
474 	 * Which means that enabling the sprite can take an extra frame
475 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
476 	 * down to LP0 and wait for vblank in order to make sure the
477 	 * sprite gets enabled on the next vblank after the register write.
478 	 * Doing otherwise would risk enabling the sprite one frame after
479 	 * we've already signalled flip completion. We can resume LP1+
480 	 * once the sprite has been enabled.
481 	 *
482 	 *
483 	 * WaCxSRDisabledForSpriteScaling:ivb
484 	 * IVB SPR_SCALE/Scaling Enable
485 	 * "Low Power watermarks must be disabled for at least one
486 	 *  frame before enabling sprite scaling, and kept disabled
487 	 *  until sprite scaling is disabled."
488 	 *
489 	 * ILK/SNB DVSASCALE/Scaling Enable
490 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
491 	 *  masked off while Big FIFO mode is exiting."
492 	 *
493 	 * Despite the w/a only being listed for IVB we assume that
494 	 * the ILK/SNB note has similar ramifications, hence we apply
495 	 * the w/a on all three platforms.
496 	 *
497 	 * With experimental results seems this is needed also for primary
498 	 * plane, not only sprite plane.
499 	 */
500 	if (plane->id != PLANE_CURSOR &&
501 	    (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) ||
502 	     IS_IVYBRIDGE(dev_priv)) &&
503 	    (turn_on || (!intel_plane_is_scaled(old_plane_state) &&
504 			 intel_plane_is_scaled(new_plane_state))))
505 		new_crtc_state->disable_lp_wm = true;
506 
507 	if (intel_plane_do_async_flip(plane, old_crtc_state, new_crtc_state))
508 		new_crtc_state->do_async_flip = true;
509 
510 	return 0;
511 }
512 
513 int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state,
514 					struct intel_crtc_state *new_crtc_state,
515 					const struct intel_plane_state *old_plane_state,
516 					struct intel_plane_state *new_plane_state)
517 {
518 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
519 	const struct drm_framebuffer *fb = new_plane_state->hw.fb;
520 	int ret;
521 
522 	intel_plane_set_invisible(new_crtc_state, new_plane_state);
523 	new_crtc_state->enabled_planes &= ~BIT(plane->id);
524 
525 	if (!new_plane_state->hw.crtc && !old_plane_state->hw.crtc)
526 		return 0;
527 
528 	ret = plane->check_plane(new_crtc_state, new_plane_state);
529 	if (ret)
530 		return ret;
531 
532 	if (fb)
533 		new_crtc_state->enabled_planes |= BIT(plane->id);
534 
535 	/* FIXME pre-g4x don't work like this */
536 	if (new_plane_state->uapi.visible)
537 		new_crtc_state->active_planes |= BIT(plane->id);
538 
539 	if (new_plane_state->uapi.visible &&
540 	    intel_plane_is_scaled(new_plane_state))
541 		new_crtc_state->scaled_planes |= BIT(plane->id);
542 
543 	if (new_plane_state->uapi.visible &&
544 	    intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
545 		new_crtc_state->nv12_planes |= BIT(plane->id);
546 
547 	if (new_plane_state->uapi.visible &&
548 	    fb->format->format == DRM_FORMAT_C8)
549 		new_crtc_state->c8_planes |= BIT(plane->id);
550 
551 	if (new_plane_state->uapi.visible || old_plane_state->uapi.visible)
552 		new_crtc_state->update_planes |= BIT(plane->id);
553 
554 	new_crtc_state->data_rate[plane->id] =
555 		intel_plane_data_rate(new_crtc_state, new_plane_state);
556 
557 	return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state,
558 					       old_plane_state, new_plane_state);
559 }
560 
561 static struct intel_plane *
562 intel_crtc_get_plane(struct intel_crtc *crtc, enum plane_id plane_id)
563 {
564 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
565 	struct intel_plane *plane;
566 
567 	for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
568 		if (plane->id == plane_id)
569 			return plane;
570 	}
571 
572 	return NULL;
573 }
574 
575 int intel_plane_atomic_check(struct intel_atomic_state *state,
576 			     struct intel_plane *plane)
577 {
578 	struct drm_i915_private *i915 = to_i915(state->base.dev);
579 	struct intel_plane_state *new_plane_state =
580 		intel_atomic_get_new_plane_state(state, plane);
581 	const struct intel_plane_state *old_plane_state =
582 		intel_atomic_get_old_plane_state(state, plane);
583 	const struct intel_plane_state *new_master_plane_state;
584 	struct intel_crtc *crtc = intel_crtc_for_pipe(i915, plane->pipe);
585 	const struct intel_crtc_state *old_crtc_state =
586 		intel_atomic_get_old_crtc_state(state, crtc);
587 	struct intel_crtc_state *new_crtc_state =
588 		intel_atomic_get_new_crtc_state(state, crtc);
589 
590 	if (new_crtc_state && intel_crtc_is_bigjoiner_slave(new_crtc_state)) {
591 		struct intel_crtc *master_crtc =
592 			intel_master_crtc(new_crtc_state);
593 		struct intel_plane *master_plane =
594 			intel_crtc_get_plane(master_crtc, plane->id);
595 
596 		new_master_plane_state =
597 			intel_atomic_get_new_plane_state(state, master_plane);
598 	} else {
599 		new_master_plane_state = new_plane_state;
600 	}
601 
602 	intel_plane_copy_uapi_to_hw_state(new_plane_state,
603 					  new_master_plane_state,
604 					  crtc);
605 
606 	new_plane_state->uapi.visible = false;
607 	if (!new_crtc_state)
608 		return 0;
609 
610 	return intel_plane_atomic_check_with_state(old_crtc_state,
611 						   new_crtc_state,
612 						   old_plane_state,
613 						   new_plane_state);
614 }
615 
616 static struct intel_plane *
617 skl_next_plane_to_commit(struct intel_atomic_state *state,
618 			 struct intel_crtc *crtc,
619 			 struct skl_ddb_entry entries_y[I915_MAX_PLANES],
620 			 struct skl_ddb_entry entries_uv[I915_MAX_PLANES],
621 			 unsigned int *update_mask)
622 {
623 	struct intel_crtc_state *crtc_state =
624 		intel_atomic_get_new_crtc_state(state, crtc);
625 	struct intel_plane_state *plane_state;
626 	struct intel_plane *plane;
627 	int i;
628 
629 	if (*update_mask == 0)
630 		return NULL;
631 
632 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
633 		enum plane_id plane_id = plane->id;
634 
635 		if (crtc->pipe != plane->pipe ||
636 		    !(*update_mask & BIT(plane_id)))
637 			continue;
638 
639 		if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
640 						entries_y,
641 						I915_MAX_PLANES, plane_id) ||
642 		    skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_uv[plane_id],
643 						entries_uv,
644 						I915_MAX_PLANES, plane_id))
645 			continue;
646 
647 		*update_mask &= ~BIT(plane_id);
648 		entries_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
649 		entries_uv[plane_id] = crtc_state->wm.skl.plane_ddb_uv[plane_id];
650 
651 		return plane;
652 	}
653 
654 	/* should never happen */
655 	drm_WARN_ON(state->base.dev, 1);
656 
657 	return NULL;
658 }
659 
660 void intel_plane_update_noarm(struct intel_plane *plane,
661 			      const struct intel_crtc_state *crtc_state,
662 			      const struct intel_plane_state *plane_state)
663 {
664 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
665 
666 	trace_intel_plane_update_noarm(&plane->base, crtc);
667 
668 	if (plane->update_noarm)
669 		plane->update_noarm(plane, crtc_state, plane_state);
670 }
671 
672 void intel_plane_update_arm(struct intel_plane *plane,
673 			    const struct intel_crtc_state *crtc_state,
674 			    const struct intel_plane_state *plane_state)
675 {
676 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
677 
678 	trace_intel_plane_update_arm(&plane->base, crtc);
679 
680 	if (crtc_state->do_async_flip && plane->async_flip)
681 		plane->async_flip(plane, crtc_state, plane_state, true);
682 	else
683 		plane->update_arm(plane, crtc_state, plane_state);
684 }
685 
686 void intel_plane_disable_arm(struct intel_plane *plane,
687 			     const struct intel_crtc_state *crtc_state)
688 {
689 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
690 
691 	trace_intel_plane_disable_arm(&plane->base, crtc);
692 	plane->disable_arm(plane, crtc_state);
693 }
694 
695 void intel_crtc_planes_update_noarm(struct intel_atomic_state *state,
696 				    struct intel_crtc *crtc)
697 {
698 	struct intel_crtc_state *new_crtc_state =
699 		intel_atomic_get_new_crtc_state(state, crtc);
700 	u32 update_mask = new_crtc_state->update_planes;
701 	struct intel_plane_state *new_plane_state;
702 	struct intel_plane *plane;
703 	int i;
704 
705 	if (new_crtc_state->do_async_flip)
706 		return;
707 
708 	/*
709 	 * Since we only write non-arming registers here,
710 	 * the order does not matter even for skl+.
711 	 */
712 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
713 		if (crtc->pipe != plane->pipe ||
714 		    !(update_mask & BIT(plane->id)))
715 			continue;
716 
717 		/* TODO: for mailbox updates this should be skipped */
718 		if (new_plane_state->uapi.visible ||
719 		    new_plane_state->planar_slave)
720 			intel_plane_update_noarm(plane, new_crtc_state, new_plane_state);
721 	}
722 }
723 
724 static void skl_crtc_planes_update_arm(struct intel_atomic_state *state,
725 				       struct intel_crtc *crtc)
726 {
727 	struct intel_crtc_state *old_crtc_state =
728 		intel_atomic_get_old_crtc_state(state, crtc);
729 	struct intel_crtc_state *new_crtc_state =
730 		intel_atomic_get_new_crtc_state(state, crtc);
731 	struct skl_ddb_entry entries_y[I915_MAX_PLANES];
732 	struct skl_ddb_entry entries_uv[I915_MAX_PLANES];
733 	u32 update_mask = new_crtc_state->update_planes;
734 	struct intel_plane *plane;
735 
736 	memcpy(entries_y, old_crtc_state->wm.skl.plane_ddb_y,
737 	       sizeof(old_crtc_state->wm.skl.plane_ddb_y));
738 	memcpy(entries_uv, old_crtc_state->wm.skl.plane_ddb_uv,
739 	       sizeof(old_crtc_state->wm.skl.plane_ddb_uv));
740 
741 	while ((plane = skl_next_plane_to_commit(state, crtc,
742 						 entries_y, entries_uv,
743 						 &update_mask))) {
744 		struct intel_plane_state *new_plane_state =
745 			intel_atomic_get_new_plane_state(state, plane);
746 
747 		/*
748 		 * TODO: for mailbox updates intel_plane_update_noarm()
749 		 * would have to be called here as well.
750 		 */
751 		if (new_plane_state->uapi.visible ||
752 		    new_plane_state->planar_slave)
753 			intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
754 		else
755 			intel_plane_disable_arm(plane, new_crtc_state);
756 	}
757 }
758 
759 static void i9xx_crtc_planes_update_arm(struct intel_atomic_state *state,
760 					struct intel_crtc *crtc)
761 {
762 	struct intel_crtc_state *new_crtc_state =
763 		intel_atomic_get_new_crtc_state(state, crtc);
764 	u32 update_mask = new_crtc_state->update_planes;
765 	struct intel_plane_state *new_plane_state;
766 	struct intel_plane *plane;
767 	int i;
768 
769 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
770 		if (crtc->pipe != plane->pipe ||
771 		    !(update_mask & BIT(plane->id)))
772 			continue;
773 
774 		/*
775 		 * TODO: for mailbox updates intel_plane_update_noarm()
776 		 * would have to be called here as well.
777 		 */
778 		if (new_plane_state->uapi.visible)
779 			intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
780 		else
781 			intel_plane_disable_arm(plane, new_crtc_state);
782 	}
783 }
784 
785 void intel_crtc_planes_update_arm(struct intel_atomic_state *state,
786 				  struct intel_crtc *crtc)
787 {
788 	struct drm_i915_private *i915 = to_i915(state->base.dev);
789 
790 	if (DISPLAY_VER(i915) >= 9)
791 		skl_crtc_planes_update_arm(state, crtc);
792 	else
793 		i9xx_crtc_planes_update_arm(state, crtc);
794 }
795 
796 int intel_atomic_plane_check_clipping(struct intel_plane_state *plane_state,
797 				      struct intel_crtc_state *crtc_state,
798 				      int min_scale, int max_scale,
799 				      bool can_position)
800 {
801 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
802 	struct drm_framebuffer *fb = plane_state->hw.fb;
803 	struct drm_rect *src = &plane_state->uapi.src;
804 	struct drm_rect *dst = &plane_state->uapi.dst;
805 	unsigned int rotation = plane_state->hw.rotation;
806 	struct drm_rect clip = {};
807 	int hscale, vscale;
808 
809 	if (!fb) {
810 		plane_state->uapi.visible = false;
811 		return 0;
812 	}
813 
814 	drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
815 
816 	/* Check scaling */
817 	hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
818 	vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
819 	if (hscale < 0 || vscale < 0) {
820 		drm_dbg_kms(&i915->drm, "Invalid scaling of plane\n");
821 		drm_rect_debug_print("src: ", src, true);
822 		drm_rect_debug_print("dst: ", dst, false);
823 		return -ERANGE;
824 	}
825 
826 	if (crtc_state->hw.enable) {
827 		clip.x2 = crtc_state->pipe_src_w;
828 		clip.y2 = crtc_state->pipe_src_h;
829 	}
830 
831 	/* right side of the image is on the slave crtc, adjust dst to match */
832 	if (intel_crtc_is_bigjoiner_slave(crtc_state))
833 		drm_rect_translate(dst, -crtc_state->pipe_src_w, 0);
834 
835 	/*
836 	 * FIXME: This might need further adjustment for seamless scaling
837 	 * with phase information, for the 2p2 and 2p1 scenarios.
838 	 */
839 	plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, &clip);
840 
841 	drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
842 
843 	if (!can_position && plane_state->uapi.visible &&
844 	    !drm_rect_equals(dst, &clip)) {
845 		drm_dbg_kms(&i915->drm, "Plane must cover entire CRTC\n");
846 		drm_rect_debug_print("dst: ", dst, false);
847 		drm_rect_debug_print("clip: ", &clip, false);
848 		return -EINVAL;
849 	}
850 
851 	return 0;
852 }
853 
854 struct wait_rps_boost {
855 	struct wait_queue_entry wait;
856 
857 	struct drm_crtc *crtc;
858 	struct i915_request *request;
859 };
860 
861 static int do_rps_boost(struct wait_queue_entry *_wait,
862 			unsigned mode, int sync, void *key)
863 {
864 	struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
865 	struct i915_request *rq = wait->request;
866 
867 	/*
868 	 * If we missed the vblank, but the request is already running it
869 	 * is reasonable to assume that it will complete before the next
870 	 * vblank without our intervention, so leave RPS alone.
871 	 */
872 	if (!i915_request_started(rq))
873 		intel_rps_boost(rq);
874 	i915_request_put(rq);
875 
876 	drm_crtc_vblank_put(wait->crtc);
877 
878 	list_del(&wait->wait.entry);
879 	kfree(wait);
880 	return 1;
881 }
882 
883 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
884 				       struct dma_fence *fence)
885 {
886 	struct wait_rps_boost *wait;
887 
888 	if (!dma_fence_is_i915(fence))
889 		return;
890 
891 	if (DISPLAY_VER(to_i915(crtc->dev)) < 6)
892 		return;
893 
894 	if (drm_crtc_vblank_get(crtc))
895 		return;
896 
897 	wait = kmalloc(sizeof(*wait), GFP_KERNEL);
898 	if (!wait) {
899 		drm_crtc_vblank_put(crtc);
900 		return;
901 	}
902 
903 	wait->request = to_request(dma_fence_get(fence));
904 	wait->crtc = crtc;
905 
906 	wait->wait.func = do_rps_boost;
907 	wait->wait.flags = 0;
908 
909 	add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
910 }
911 
912 /**
913  * intel_prepare_plane_fb - Prepare fb for usage on plane
914  * @_plane: drm plane to prepare for
915  * @_new_plane_state: the plane state being prepared
916  *
917  * Prepares a framebuffer for usage on a display plane.  Generally this
918  * involves pinning the underlying object and updating the frontbuffer tracking
919  * bits.  Some older platforms need special physical address handling for
920  * cursor planes.
921  *
922  * Returns 0 on success, negative error code on failure.
923  */
924 static int
925 intel_prepare_plane_fb(struct drm_plane *_plane,
926 		       struct drm_plane_state *_new_plane_state)
927 {
928 	struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY };
929 	struct intel_plane *plane = to_intel_plane(_plane);
930 	struct intel_plane_state *new_plane_state =
931 		to_intel_plane_state(_new_plane_state);
932 	struct intel_atomic_state *state =
933 		to_intel_atomic_state(new_plane_state->uapi.state);
934 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
935 	const struct intel_plane_state *old_plane_state =
936 		intel_atomic_get_old_plane_state(state, plane);
937 	struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
938 	struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
939 	int ret;
940 
941 	if (old_obj) {
942 		const struct intel_crtc_state *crtc_state =
943 			intel_atomic_get_new_crtc_state(state,
944 							to_intel_crtc(old_plane_state->hw.crtc));
945 
946 		/* Big Hammer, we also need to ensure that any pending
947 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
948 		 * current scanout is retired before unpinning the old
949 		 * framebuffer. Note that we rely on userspace rendering
950 		 * into the buffer attached to the pipe they are waiting
951 		 * on. If not, userspace generates a GPU hang with IPEHR
952 		 * point to the MI_WAIT_FOR_EVENT.
953 		 *
954 		 * This should only fail upon a hung GPU, in which case we
955 		 * can safely continue.
956 		 */
957 		if (intel_crtc_needs_modeset(crtc_state)) {
958 			ret = i915_sw_fence_await_reservation(&state->commit_ready,
959 							      old_obj->base.resv, NULL,
960 							      false, 0,
961 							      GFP_KERNEL);
962 			if (ret < 0)
963 				return ret;
964 		}
965 	}
966 
967 	if (new_plane_state->uapi.fence) { /* explicit fencing */
968 		i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
969 					     &attr);
970 		ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
971 						    new_plane_state->uapi.fence,
972 						    i915_fence_timeout(dev_priv),
973 						    GFP_KERNEL);
974 		if (ret < 0)
975 			return ret;
976 	}
977 
978 	if (!obj)
979 		return 0;
980 
981 
982 	ret = intel_plane_pin_fb(new_plane_state);
983 	if (ret)
984 		return ret;
985 
986 	i915_gem_object_wait_priority(obj, 0, &attr);
987 
988 	if (!new_plane_state->uapi.fence) { /* implicit fencing */
989 		struct dma_resv_iter cursor;
990 		struct dma_fence *fence;
991 
992 		ret = i915_sw_fence_await_reservation(&state->commit_ready,
993 						      obj->base.resv, NULL,
994 						      false,
995 						      i915_fence_timeout(dev_priv),
996 						      GFP_KERNEL);
997 		if (ret < 0)
998 			goto unpin_fb;
999 
1000 		dma_resv_iter_begin(&cursor, obj->base.resv, false);
1001 		dma_resv_for_each_fence_unlocked(&cursor, fence) {
1002 			add_rps_boost_after_vblank(new_plane_state->hw.crtc,
1003 						   fence);
1004 		}
1005 		dma_resv_iter_end(&cursor);
1006 	} else {
1007 		add_rps_boost_after_vblank(new_plane_state->hw.crtc,
1008 					   new_plane_state->uapi.fence);
1009 	}
1010 
1011 	/*
1012 	 * We declare pageflips to be interactive and so merit a small bias
1013 	 * towards upclocking to deliver the frame on time. By only changing
1014 	 * the RPS thresholds to sample more regularly and aim for higher
1015 	 * clocks we can hopefully deliver low power workloads (like kodi)
1016 	 * that are not quite steady state without resorting to forcing
1017 	 * maximum clocks following a vblank miss (see do_rps_boost()).
1018 	 */
1019 	if (!state->rps_interactive) {
1020 		intel_rps_mark_interactive(&to_gt(dev_priv)->rps, true);
1021 		state->rps_interactive = true;
1022 	}
1023 
1024 	return 0;
1025 
1026 unpin_fb:
1027 	intel_plane_unpin_fb(new_plane_state);
1028 
1029 	return ret;
1030 }
1031 
1032 /**
1033  * intel_cleanup_plane_fb - Cleans up an fb after plane use
1034  * @plane: drm plane to clean up for
1035  * @_old_plane_state: the state from the previous modeset
1036  *
1037  * Cleans up a framebuffer that has just been removed from a plane.
1038  */
1039 static void
1040 intel_cleanup_plane_fb(struct drm_plane *plane,
1041 		       struct drm_plane_state *_old_plane_state)
1042 {
1043 	struct intel_plane_state *old_plane_state =
1044 		to_intel_plane_state(_old_plane_state);
1045 	struct intel_atomic_state *state =
1046 		to_intel_atomic_state(old_plane_state->uapi.state);
1047 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
1048 	struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
1049 
1050 	if (!obj)
1051 		return;
1052 
1053 	if (state->rps_interactive) {
1054 		intel_rps_mark_interactive(&to_gt(dev_priv)->rps, false);
1055 		state->rps_interactive = false;
1056 	}
1057 
1058 	/* Should only be called after a successful intel_prepare_plane_fb()! */
1059 	intel_plane_unpin_fb(old_plane_state);
1060 }
1061 
1062 static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1063 	.prepare_fb = intel_prepare_plane_fb,
1064 	.cleanup_fb = intel_cleanup_plane_fb,
1065 };
1066 
1067 void intel_plane_helper_add(struct intel_plane *plane)
1068 {
1069 	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
1070 }
1071