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