xref: /openbsd-src/sys/dev/pci/drm/i915/display/intel_atomic_plane.c (revision f005ef32267c16bdb134f0e9fa4477dbe07c263a)
1c349dbc7Sjsg /*
2c349dbc7Sjsg  * Copyright © 2014 Intel Corporation
3c349dbc7Sjsg  *
4c349dbc7Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
5c349dbc7Sjsg  * copy of this software and associated documentation files (the "Software"),
6c349dbc7Sjsg  * to deal in the Software without restriction, including without limitation
7c349dbc7Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c349dbc7Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
9c349dbc7Sjsg  * Software is furnished to do so, subject to the following conditions:
10c349dbc7Sjsg  *
11c349dbc7Sjsg  * The above copyright notice and this permission notice (including the next
12c349dbc7Sjsg  * paragraph) shall be included in all copies or substantial portions of the
13c349dbc7Sjsg  * Software.
14c349dbc7Sjsg  *
15c349dbc7Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16c349dbc7Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17c349dbc7Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18c349dbc7Sjsg  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19c349dbc7Sjsg  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20c349dbc7Sjsg  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21c349dbc7Sjsg  * DEALINGS IN THE SOFTWARE.
22c349dbc7Sjsg  */
23c349dbc7Sjsg 
24c349dbc7Sjsg /**
25c349dbc7Sjsg  * DOC: atomic plane helpers
26c349dbc7Sjsg  *
27c349dbc7Sjsg  * The functions here are used by the atomic plane helper functions to
28c349dbc7Sjsg  * implement legacy plane updates (i.e., drm_plane->update_plane() and
29c349dbc7Sjsg  * drm_plane->disable_plane()).  This allows plane updates to use the
30c349dbc7Sjsg  * atomic state infrastructure and perform plane updates as separate
31c349dbc7Sjsg  * prepare/check/commit/cleanup steps.
32c349dbc7Sjsg  */
33c349dbc7Sjsg 
34c349dbc7Sjsg #include <drm/drm_atomic_helper.h>
35*f005ef32Sjsg #include <drm/drm_blend.h>
36c349dbc7Sjsg #include <drm/drm_fourcc.h>
37c349dbc7Sjsg 
38*f005ef32Sjsg #include "i915_config.h"
39*f005ef32Sjsg #include "i915_reg.h"
40c349dbc7Sjsg #include "intel_atomic_plane.h"
41c349dbc7Sjsg #include "intel_cdclk.h"
42*f005ef32Sjsg #include "intel_display_rps.h"
431bb76ff1Sjsg #include "intel_display_trace.h"
44c349dbc7Sjsg #include "intel_display_types.h"
451bb76ff1Sjsg #include "intel_fb.h"
461bb76ff1Sjsg #include "intel_fb_pin.h"
471bb76ff1Sjsg #include "skl_scaler.h"
481bb76ff1Sjsg #include "skl_watermark.h"
49c349dbc7Sjsg 
intel_plane_state_reset(struct intel_plane_state * plane_state,struct intel_plane * plane)50c349dbc7Sjsg static void intel_plane_state_reset(struct intel_plane_state *plane_state,
51c349dbc7Sjsg 				    struct intel_plane *plane)
52c349dbc7Sjsg {
53c349dbc7Sjsg 	memset(plane_state, 0, sizeof(*plane_state));
54c349dbc7Sjsg 
55c349dbc7Sjsg 	__drm_atomic_helper_plane_state_reset(&plane_state->uapi, &plane->base);
56c349dbc7Sjsg 
57c349dbc7Sjsg 	plane_state->scaler_id = -1;
58c349dbc7Sjsg }
59c349dbc7Sjsg 
intel_plane_alloc(void)60c349dbc7Sjsg struct intel_plane *intel_plane_alloc(void)
61c349dbc7Sjsg {
62c349dbc7Sjsg 	struct intel_plane_state *plane_state;
63c349dbc7Sjsg 	struct intel_plane *plane;
64c349dbc7Sjsg 
65c349dbc7Sjsg 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
66c349dbc7Sjsg 	if (!plane)
67c349dbc7Sjsg 		return ERR_PTR(-ENOMEM);
68c349dbc7Sjsg 
69c349dbc7Sjsg 	plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
70c349dbc7Sjsg 	if (!plane_state) {
71c349dbc7Sjsg 		kfree(plane);
72c349dbc7Sjsg 		return ERR_PTR(-ENOMEM);
73c349dbc7Sjsg 	}
74c349dbc7Sjsg 
75c349dbc7Sjsg 	intel_plane_state_reset(plane_state, plane);
76c349dbc7Sjsg 
77c349dbc7Sjsg 	plane->base.state = &plane_state->uapi;
78c349dbc7Sjsg 
79c349dbc7Sjsg 	return plane;
80c349dbc7Sjsg }
81c349dbc7Sjsg 
intel_plane_free(struct intel_plane * plane)82c349dbc7Sjsg void intel_plane_free(struct intel_plane *plane)
83c349dbc7Sjsg {
84c349dbc7Sjsg 	intel_plane_destroy_state(&plane->base, plane->base.state);
85c349dbc7Sjsg 	kfree(plane);
86c349dbc7Sjsg }
87c349dbc7Sjsg 
88c349dbc7Sjsg /**
89c349dbc7Sjsg  * intel_plane_duplicate_state - duplicate plane state
90c349dbc7Sjsg  * @plane: drm plane
91c349dbc7Sjsg  *
92c349dbc7Sjsg  * Allocates and returns a copy of the plane state (both common and
93c349dbc7Sjsg  * Intel-specific) for the specified plane.
94c349dbc7Sjsg  *
95c349dbc7Sjsg  * Returns: The newly allocated plane state, or NULL on failure.
96c349dbc7Sjsg  */
97c349dbc7Sjsg struct drm_plane_state *
intel_plane_duplicate_state(struct drm_plane * plane)98c349dbc7Sjsg intel_plane_duplicate_state(struct drm_plane *plane)
99c349dbc7Sjsg {
100c349dbc7Sjsg 	struct intel_plane_state *intel_state;
101c349dbc7Sjsg 
102c349dbc7Sjsg 	intel_state = to_intel_plane_state(plane->state);
103c349dbc7Sjsg 	intel_state = kmemdup(intel_state, sizeof(*intel_state), GFP_KERNEL);
104c349dbc7Sjsg 
105c349dbc7Sjsg 	if (!intel_state)
106c349dbc7Sjsg 		return NULL;
107c349dbc7Sjsg 
108c349dbc7Sjsg 	__drm_atomic_helper_plane_duplicate_state(plane, &intel_state->uapi);
109c349dbc7Sjsg 
1105ca02815Sjsg 	intel_state->ggtt_vma = NULL;
1115ca02815Sjsg 	intel_state->dpt_vma = NULL;
112c349dbc7Sjsg 	intel_state->flags = 0;
113c349dbc7Sjsg 
114c349dbc7Sjsg 	/* add reference to fb */
115c349dbc7Sjsg 	if (intel_state->hw.fb)
116c349dbc7Sjsg 		drm_framebuffer_get(intel_state->hw.fb);
117c349dbc7Sjsg 
118c349dbc7Sjsg 	return &intel_state->uapi;
119c349dbc7Sjsg }
120c349dbc7Sjsg 
121c349dbc7Sjsg /**
122c349dbc7Sjsg  * intel_plane_destroy_state - destroy plane state
123c349dbc7Sjsg  * @plane: drm plane
124c349dbc7Sjsg  * @state: state object to destroy
125c349dbc7Sjsg  *
126c349dbc7Sjsg  * Destroys the plane state (both common and Intel-specific) for the
127c349dbc7Sjsg  * specified plane.
128c349dbc7Sjsg  */
129c349dbc7Sjsg void
intel_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)130c349dbc7Sjsg intel_plane_destroy_state(struct drm_plane *plane,
131c349dbc7Sjsg 			  struct drm_plane_state *state)
132c349dbc7Sjsg {
133c349dbc7Sjsg 	struct intel_plane_state *plane_state = to_intel_plane_state(state);
1345ca02815Sjsg 
1355ca02815Sjsg 	drm_WARN_ON(plane->dev, plane_state->ggtt_vma);
1365ca02815Sjsg 	drm_WARN_ON(plane->dev, plane_state->dpt_vma);
137c349dbc7Sjsg 
138c349dbc7Sjsg 	__drm_atomic_helper_plane_destroy_state(&plane_state->uapi);
139c349dbc7Sjsg 	if (plane_state->hw.fb)
140c349dbc7Sjsg 		drm_framebuffer_put(plane_state->hw.fb);
141c349dbc7Sjsg 	kfree(plane_state);
142c349dbc7Sjsg }
143c349dbc7Sjsg 
intel_adjusted_rate(const struct drm_rect * src,const struct drm_rect * dst,unsigned int rate)1445ca02815Sjsg unsigned int intel_adjusted_rate(const struct drm_rect *src,
1455ca02815Sjsg 				 const struct drm_rect *dst,
1465ca02815Sjsg 				 unsigned int rate)
147c349dbc7Sjsg {
148c349dbc7Sjsg 	unsigned int src_w, src_h, dst_w, dst_h;
149c349dbc7Sjsg 
1505ca02815Sjsg 	src_w = drm_rect_width(src) >> 16;
1515ca02815Sjsg 	src_h = drm_rect_height(src) >> 16;
1525ca02815Sjsg 	dst_w = drm_rect_width(dst);
1535ca02815Sjsg 	dst_h = drm_rect_height(dst);
154c349dbc7Sjsg 
155c349dbc7Sjsg 	/* Downscaling limits the maximum pixel rate */
156c349dbc7Sjsg 	dst_w = min(src_w, dst_w);
157c349dbc7Sjsg 	dst_h = min(src_h, dst_h);
158c349dbc7Sjsg 
1595ca02815Sjsg 	return DIV_ROUND_UP_ULL(mul_u32_u32(rate, src_w * src_h),
160c349dbc7Sjsg 				dst_w * dst_h);
161c349dbc7Sjsg }
162c349dbc7Sjsg 
intel_plane_pixel_rate(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)1635ca02815Sjsg unsigned int intel_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
1645ca02815Sjsg 				    const struct intel_plane_state *plane_state)
1655ca02815Sjsg {
1665ca02815Sjsg 	/*
1675ca02815Sjsg 	 * Note we don't check for plane visibility here as
1685ca02815Sjsg 	 * we want to use this when calculating the cursor
1695ca02815Sjsg 	 * watermarks even if the cursor is fully offscreen.
1705ca02815Sjsg 	 * That depends on the src/dst rectangles being
1715ca02815Sjsg 	 * correctly populated whenever the watermark code
1725ca02815Sjsg 	 * considers the cursor to be visible, whether or not
1735ca02815Sjsg 	 * it is actually visible.
1745ca02815Sjsg 	 *
1755ca02815Sjsg 	 * See: intel_wm_plane_visible() and intel_check_cursor()
1765ca02815Sjsg 	 */
1775ca02815Sjsg 
1785ca02815Sjsg 	return intel_adjusted_rate(&plane_state->uapi.src,
1795ca02815Sjsg 				   &plane_state->uapi.dst,
1805ca02815Sjsg 				   crtc_state->pixel_rate);
1815ca02815Sjsg }
1825ca02815Sjsg 
intel_plane_data_rate(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,int color_plane)183c349dbc7Sjsg unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state,
1841bb76ff1Sjsg 				   const struct intel_plane_state *plane_state,
1851bb76ff1Sjsg 				   int color_plane)
186c349dbc7Sjsg {
187c349dbc7Sjsg 	const struct drm_framebuffer *fb = plane_state->hw.fb;
188c349dbc7Sjsg 
189c349dbc7Sjsg 	if (!plane_state->uapi.visible)
190c349dbc7Sjsg 		return 0;
191c349dbc7Sjsg 
1921bb76ff1Sjsg 	return intel_plane_pixel_rate(crtc_state, plane_state) *
1931bb76ff1Sjsg 		fb->format->cpp[color_plane];
1941bb76ff1Sjsg }
195c349dbc7Sjsg 
1961bb76ff1Sjsg static bool
use_min_ddb(const struct intel_crtc_state * crtc_state,struct intel_plane * plane)1971bb76ff1Sjsg use_min_ddb(const struct intel_crtc_state *crtc_state,
1981bb76ff1Sjsg 	    struct intel_plane *plane)
1991bb76ff1Sjsg {
2001bb76ff1Sjsg 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
2011bb76ff1Sjsg 
2021bb76ff1Sjsg 	return DISPLAY_VER(i915) >= 13 &&
2031bb76ff1Sjsg 	       crtc_state->uapi.async_flip &&
2041bb76ff1Sjsg 	       plane->async_flip;
2051bb76ff1Sjsg }
2061bb76ff1Sjsg 
2071bb76ff1Sjsg static unsigned int
intel_plane_relative_data_rate(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,int color_plane)2081bb76ff1Sjsg intel_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
2091bb76ff1Sjsg 			       const struct intel_plane_state *plane_state,
2101bb76ff1Sjsg 			       int color_plane)
2111bb76ff1Sjsg {
2121bb76ff1Sjsg 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2131bb76ff1Sjsg 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2141bb76ff1Sjsg 	int width, height;
215*f005ef32Sjsg 	unsigned int rel_data_rate;
2161bb76ff1Sjsg 
2171bb76ff1Sjsg 	if (plane->id == PLANE_CURSOR)
2181bb76ff1Sjsg 		return 0;
2191bb76ff1Sjsg 
2201bb76ff1Sjsg 	if (!plane_state->uapi.visible)
2211bb76ff1Sjsg 		return 0;
222c349dbc7Sjsg 
223c349dbc7Sjsg 	/*
2241bb76ff1Sjsg 	 * We calculate extra ddb based on ratio plane rate/total data rate
2251bb76ff1Sjsg 	 * in case, in some cases we should not allocate extra ddb for the plane,
2261bb76ff1Sjsg 	 * so do not count its data rate, if this is the case.
227c349dbc7Sjsg 	 */
2281bb76ff1Sjsg 	if (use_min_ddb(crtc_state, plane))
2291bb76ff1Sjsg 		return 0;
230c349dbc7Sjsg 
2311bb76ff1Sjsg 	/*
2321bb76ff1Sjsg 	 * Src coordinates are already rotated by 270 degrees for
2331bb76ff1Sjsg 	 * the 90/270 degree plane rotation cases (to match the
2341bb76ff1Sjsg 	 * GTT mapping), hence no need to account for rotation here.
2351bb76ff1Sjsg 	 */
2361bb76ff1Sjsg 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
2371bb76ff1Sjsg 	height = drm_rect_height(&plane_state->uapi.src) >> 16;
2381bb76ff1Sjsg 
2391bb76ff1Sjsg 	/* UV plane does 1/2 pixel sub-sampling */
2401bb76ff1Sjsg 	if (color_plane == 1) {
2411bb76ff1Sjsg 		width /= 2;
2421bb76ff1Sjsg 		height /= 2;
2431bb76ff1Sjsg 	}
2441bb76ff1Sjsg 
245*f005ef32Sjsg 	rel_data_rate = width * height * fb->format->cpp[color_plane];
246*f005ef32Sjsg 
247*f005ef32Sjsg 	return intel_adjusted_rate(&plane_state->uapi.src,
248*f005ef32Sjsg 				   &plane_state->uapi.dst,
249*f005ef32Sjsg 				   rel_data_rate);
250c349dbc7Sjsg }
251c349dbc7Sjsg 
intel_plane_calc_min_cdclk(struct intel_atomic_state * state,struct intel_plane * plane,bool * need_cdclk_calc)252c349dbc7Sjsg int intel_plane_calc_min_cdclk(struct intel_atomic_state *state,
253c349dbc7Sjsg 			       struct intel_plane *plane,
254c349dbc7Sjsg 			       bool *need_cdclk_calc)
255c349dbc7Sjsg {
256c349dbc7Sjsg 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
257c349dbc7Sjsg 	const struct intel_plane_state *plane_state =
258c349dbc7Sjsg 		intel_atomic_get_new_plane_state(state, plane);
259c349dbc7Sjsg 	struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
260c349dbc7Sjsg 	const struct intel_cdclk_state *cdclk_state;
261c349dbc7Sjsg 	const struct intel_crtc_state *old_crtc_state;
262c349dbc7Sjsg 	struct intel_crtc_state *new_crtc_state;
263c349dbc7Sjsg 
264c349dbc7Sjsg 	if (!plane_state->uapi.visible || !plane->min_cdclk)
265c349dbc7Sjsg 		return 0;
266c349dbc7Sjsg 
267c349dbc7Sjsg 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
268c349dbc7Sjsg 	new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
269c349dbc7Sjsg 
270c349dbc7Sjsg 	new_crtc_state->min_cdclk[plane->id] =
271c349dbc7Sjsg 		plane->min_cdclk(new_crtc_state, plane_state);
272c349dbc7Sjsg 
273c349dbc7Sjsg 	/*
274c349dbc7Sjsg 	 * No need to check against the cdclk state if
275c349dbc7Sjsg 	 * the min cdclk for the plane doesn't increase.
276c349dbc7Sjsg 	 *
277c349dbc7Sjsg 	 * Ie. we only ever increase the cdclk due to plane
278c349dbc7Sjsg 	 * requirements. This can reduce back and forth
279c349dbc7Sjsg 	 * display blinking due to constant cdclk changes.
280c349dbc7Sjsg 	 */
281c349dbc7Sjsg 	if (new_crtc_state->min_cdclk[plane->id] <=
282c349dbc7Sjsg 	    old_crtc_state->min_cdclk[plane->id])
283c349dbc7Sjsg 		return 0;
284c349dbc7Sjsg 
285c349dbc7Sjsg 	cdclk_state = intel_atomic_get_cdclk_state(state);
286c349dbc7Sjsg 	if (IS_ERR(cdclk_state))
287c349dbc7Sjsg 		return PTR_ERR(cdclk_state);
288c349dbc7Sjsg 
289c349dbc7Sjsg 	/*
290c349dbc7Sjsg 	 * No need to recalculate the cdclk state if
291c349dbc7Sjsg 	 * the min cdclk for the pipe doesn't increase.
292c349dbc7Sjsg 	 *
293c349dbc7Sjsg 	 * Ie. we only ever increase the cdclk due to plane
294c349dbc7Sjsg 	 * requirements. This can reduce back and forth
295c349dbc7Sjsg 	 * display blinking due to constant cdclk changes.
296c349dbc7Sjsg 	 */
297c349dbc7Sjsg 	if (new_crtc_state->min_cdclk[plane->id] <=
298c349dbc7Sjsg 	    cdclk_state->min_cdclk[crtc->pipe])
299c349dbc7Sjsg 		return 0;
300c349dbc7Sjsg 
301c349dbc7Sjsg 	drm_dbg_kms(&dev_priv->drm,
302c349dbc7Sjsg 		    "[PLANE:%d:%s] min cdclk (%d kHz) > [CRTC:%d:%s] min cdclk (%d kHz)\n",
303c349dbc7Sjsg 		    plane->base.base.id, plane->base.name,
304c349dbc7Sjsg 		    new_crtc_state->min_cdclk[plane->id],
305c349dbc7Sjsg 		    crtc->base.base.id, crtc->base.name,
306c349dbc7Sjsg 		    cdclk_state->min_cdclk[crtc->pipe]);
307c349dbc7Sjsg 	*need_cdclk_calc = true;
308c349dbc7Sjsg 
309c349dbc7Sjsg 	return 0;
310c349dbc7Sjsg }
311c349dbc7Sjsg 
intel_plane_clear_hw_state(struct intel_plane_state * plane_state)312c349dbc7Sjsg static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state)
313c349dbc7Sjsg {
314c349dbc7Sjsg 	if (plane_state->hw.fb)
315c349dbc7Sjsg 		drm_framebuffer_put(plane_state->hw.fb);
316c349dbc7Sjsg 
317c349dbc7Sjsg 	memset(&plane_state->hw, 0, sizeof(plane_state->hw));
318c349dbc7Sjsg }
319c349dbc7Sjsg 
intel_plane_copy_uapi_to_hw_state(struct intel_plane_state * plane_state,const struct intel_plane_state * from_plane_state,struct intel_crtc * crtc)320c349dbc7Sjsg void intel_plane_copy_uapi_to_hw_state(struct intel_plane_state *plane_state,
3215ca02815Sjsg 				       const struct intel_plane_state *from_plane_state,
3225ca02815Sjsg 				       struct intel_crtc *crtc)
323c349dbc7Sjsg {
324c349dbc7Sjsg 	intel_plane_clear_hw_state(plane_state);
325c349dbc7Sjsg 
3265ca02815Sjsg 	/*
3275ca02815Sjsg 	 * For the bigjoiner slave uapi.crtc will point at
3285ca02815Sjsg 	 * the master crtc. So we explicitly assign the right
3295ca02815Sjsg 	 * slave crtc to hw.crtc. uapi.crtc!=NULL simply indicates
3305ca02815Sjsg 	 * the plane is logically enabled on the uapi level.
3315ca02815Sjsg 	 */
3325ca02815Sjsg 	plane_state->hw.crtc = from_plane_state->uapi.crtc ? &crtc->base : NULL;
3335ca02815Sjsg 
334c349dbc7Sjsg 	plane_state->hw.fb = from_plane_state->uapi.fb;
335c349dbc7Sjsg 	if (plane_state->hw.fb)
336c349dbc7Sjsg 		drm_framebuffer_get(plane_state->hw.fb);
337c349dbc7Sjsg 
338c349dbc7Sjsg 	plane_state->hw.alpha = from_plane_state->uapi.alpha;
339c349dbc7Sjsg 	plane_state->hw.pixel_blend_mode =
340c349dbc7Sjsg 		from_plane_state->uapi.pixel_blend_mode;
341c349dbc7Sjsg 	plane_state->hw.rotation = from_plane_state->uapi.rotation;
342c349dbc7Sjsg 	plane_state->hw.color_encoding = from_plane_state->uapi.color_encoding;
343c349dbc7Sjsg 	plane_state->hw.color_range = from_plane_state->uapi.color_range;
3445ca02815Sjsg 	plane_state->hw.scaling_filter = from_plane_state->uapi.scaling_filter;
3455ca02815Sjsg 
3465ca02815Sjsg 	plane_state->uapi.src = drm_plane_state_src(&from_plane_state->uapi);
3475ca02815Sjsg 	plane_state->uapi.dst = drm_plane_state_dest(&from_plane_state->uapi);
3485ca02815Sjsg }
3495ca02815Sjsg 
intel_plane_copy_hw_state(struct intel_plane_state * plane_state,const struct intel_plane_state * from_plane_state)3505ca02815Sjsg void intel_plane_copy_hw_state(struct intel_plane_state *plane_state,
3515ca02815Sjsg 			       const struct intel_plane_state *from_plane_state)
3525ca02815Sjsg {
3535ca02815Sjsg 	intel_plane_clear_hw_state(plane_state);
3545ca02815Sjsg 
3555ca02815Sjsg 	memcpy(&plane_state->hw, &from_plane_state->hw,
3565ca02815Sjsg 	       sizeof(plane_state->hw));
3575ca02815Sjsg 
3585ca02815Sjsg 	if (plane_state->hw.fb)
3595ca02815Sjsg 		drm_framebuffer_get(plane_state->hw.fb);
360c349dbc7Sjsg }
361c349dbc7Sjsg 
intel_plane_set_invisible(struct intel_crtc_state * crtc_state,struct intel_plane_state * plane_state)362ad8b1aafSjsg void intel_plane_set_invisible(struct intel_crtc_state *crtc_state,
363ad8b1aafSjsg 			       struct intel_plane_state *plane_state)
364ad8b1aafSjsg {
365ad8b1aafSjsg 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
366ad8b1aafSjsg 
367ad8b1aafSjsg 	crtc_state->active_planes &= ~BIT(plane->id);
3681bb76ff1Sjsg 	crtc_state->scaled_planes &= ~BIT(plane->id);
369ad8b1aafSjsg 	crtc_state->nv12_planes &= ~BIT(plane->id);
370ad8b1aafSjsg 	crtc_state->c8_planes &= ~BIT(plane->id);
371*f005ef32Sjsg 	crtc_state->async_flip_planes &= ~BIT(plane->id);
372ad8b1aafSjsg 	crtc_state->data_rate[plane->id] = 0;
3731bb76ff1Sjsg 	crtc_state->data_rate_y[plane->id] = 0;
3741bb76ff1Sjsg 	crtc_state->rel_data_rate[plane->id] = 0;
3751bb76ff1Sjsg 	crtc_state->rel_data_rate_y[plane->id] = 0;
376ad8b1aafSjsg 	crtc_state->min_cdclk[plane->id] = 0;
377ad8b1aafSjsg 
378ad8b1aafSjsg 	plane_state->uapi.visible = false;
379ad8b1aafSjsg }
380ad8b1aafSjsg 
3811bb76ff1Sjsg /* FIXME nuke when all wm code is atomic */
intel_wm_need_update(const struct intel_plane_state * cur,struct intel_plane_state * new)3821bb76ff1Sjsg static bool intel_wm_need_update(const struct intel_plane_state *cur,
3831bb76ff1Sjsg 				 struct intel_plane_state *new)
3841bb76ff1Sjsg {
3851bb76ff1Sjsg 	/* Update watermarks on tiling or size changes. */
3861bb76ff1Sjsg 	if (new->uapi.visible != cur->uapi.visible)
3871bb76ff1Sjsg 		return true;
3881bb76ff1Sjsg 
3891bb76ff1Sjsg 	if (!cur->hw.fb || !new->hw.fb)
3901bb76ff1Sjsg 		return false;
3911bb76ff1Sjsg 
3921bb76ff1Sjsg 	if (cur->hw.fb->modifier != new->hw.fb->modifier ||
3931bb76ff1Sjsg 	    cur->hw.rotation != new->hw.rotation ||
3941bb76ff1Sjsg 	    drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
3951bb76ff1Sjsg 	    drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
3961bb76ff1Sjsg 	    drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
3971bb76ff1Sjsg 	    drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
3981bb76ff1Sjsg 		return true;
3991bb76ff1Sjsg 
4001bb76ff1Sjsg 	return false;
4011bb76ff1Sjsg }
4021bb76ff1Sjsg 
intel_plane_is_scaled(const struct intel_plane_state * plane_state)4031bb76ff1Sjsg static bool intel_plane_is_scaled(const struct intel_plane_state *plane_state)
4041bb76ff1Sjsg {
4051bb76ff1Sjsg 	int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4061bb76ff1Sjsg 	int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4071bb76ff1Sjsg 	int dst_w = drm_rect_width(&plane_state->uapi.dst);
4081bb76ff1Sjsg 	int dst_h = drm_rect_height(&plane_state->uapi.dst);
4091bb76ff1Sjsg 
4101bb76ff1Sjsg 	return src_w != dst_w || src_h != dst_h;
4111bb76ff1Sjsg }
4121bb76ff1Sjsg 
intel_plane_do_async_flip(struct intel_plane * plane,const struct intel_crtc_state * old_crtc_state,const struct intel_crtc_state * new_crtc_state)4131bb76ff1Sjsg static bool intel_plane_do_async_flip(struct intel_plane *plane,
4141bb76ff1Sjsg 				      const struct intel_crtc_state *old_crtc_state,
4151bb76ff1Sjsg 				      const struct intel_crtc_state *new_crtc_state)
4161bb76ff1Sjsg {
4171bb76ff1Sjsg 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
4181bb76ff1Sjsg 
4191bb76ff1Sjsg 	if (!plane->async_flip)
4201bb76ff1Sjsg 		return false;
4211bb76ff1Sjsg 
4221bb76ff1Sjsg 	if (!new_crtc_state->uapi.async_flip)
4231bb76ff1Sjsg 		return false;
4241bb76ff1Sjsg 
4251bb76ff1Sjsg 	/*
4261bb76ff1Sjsg 	 * In platforms after DISPLAY13, we might need to override
4271bb76ff1Sjsg 	 * first async flip in order to change watermark levels
4281bb76ff1Sjsg 	 * as part of optimization.
4291bb76ff1Sjsg 	 * So for those, we are checking if this is a first async flip.
4301bb76ff1Sjsg 	 * For platforms earlier than DISPLAY13 we always do async flip.
4311bb76ff1Sjsg 	 */
4321bb76ff1Sjsg 	return DISPLAY_VER(i915) < 13 || old_crtc_state->uapi.async_flip;
4331bb76ff1Sjsg }
4341bb76ff1Sjsg 
i9xx_must_disable_cxsr(const struct intel_crtc_state * new_crtc_state,const struct intel_plane_state * old_plane_state,const struct intel_plane_state * new_plane_state)435*f005ef32Sjsg static bool i9xx_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state,
436*f005ef32Sjsg 				   const struct intel_plane_state *old_plane_state,
437*f005ef32Sjsg 				   const struct intel_plane_state *new_plane_state)
438*f005ef32Sjsg {
439*f005ef32Sjsg 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
440*f005ef32Sjsg 	bool old_visible = old_plane_state->uapi.visible;
441*f005ef32Sjsg 	bool new_visible = new_plane_state->uapi.visible;
442*f005ef32Sjsg 	u32 old_ctl = old_plane_state->ctl;
443*f005ef32Sjsg 	u32 new_ctl = new_plane_state->ctl;
444*f005ef32Sjsg 	bool modeset, turn_on, turn_off;
445*f005ef32Sjsg 
446*f005ef32Sjsg 	if (plane->id == PLANE_CURSOR)
447*f005ef32Sjsg 		return false;
448*f005ef32Sjsg 
449*f005ef32Sjsg 	modeset = intel_crtc_needs_modeset(new_crtc_state);
450*f005ef32Sjsg 	turn_off = old_visible && (!new_visible || modeset);
451*f005ef32Sjsg 	turn_on = new_visible && (!old_visible || modeset);
452*f005ef32Sjsg 
453*f005ef32Sjsg 	/* Must disable CxSR around plane enable/disable */
454*f005ef32Sjsg 	if (turn_on || turn_off)
455*f005ef32Sjsg 		return true;
456*f005ef32Sjsg 
457*f005ef32Sjsg 	if (!old_visible || !new_visible)
458*f005ef32Sjsg 		return false;
459*f005ef32Sjsg 
460*f005ef32Sjsg 	/*
461*f005ef32Sjsg 	 * Most plane control register updates are blocked while in CxSR.
462*f005ef32Sjsg 	 *
463*f005ef32Sjsg 	 * Tiling mode is one exception where the primary plane can
464*f005ef32Sjsg 	 * apparently handle it, whereas the sprites can not (the
465*f005ef32Sjsg 	 * sprite issue being only relevant on VLV/CHV where CxSR
466*f005ef32Sjsg 	 * is actually possible with a sprite enabled).
467*f005ef32Sjsg 	 */
468*f005ef32Sjsg 	if (plane->id == PLANE_PRIMARY) {
469*f005ef32Sjsg 		old_ctl &= ~DISP_TILED;
470*f005ef32Sjsg 		new_ctl &= ~DISP_TILED;
471*f005ef32Sjsg 	}
472*f005ef32Sjsg 
473*f005ef32Sjsg 	return old_ctl != new_ctl;
474*f005ef32Sjsg }
475*f005ef32Sjsg 
intel_plane_atomic_calc_changes(const struct intel_crtc_state * old_crtc_state,struct intel_crtc_state * new_crtc_state,const struct intel_plane_state * old_plane_state,struct intel_plane_state * new_plane_state)4761bb76ff1Sjsg static int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
4771bb76ff1Sjsg 					   struct intel_crtc_state *new_crtc_state,
4781bb76ff1Sjsg 					   const struct intel_plane_state *old_plane_state,
4791bb76ff1Sjsg 					   struct intel_plane_state *new_plane_state)
4801bb76ff1Sjsg {
4811bb76ff1Sjsg 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
4821bb76ff1Sjsg 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
4831bb76ff1Sjsg 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4841bb76ff1Sjsg 	bool mode_changed = intel_crtc_needs_modeset(new_crtc_state);
4851bb76ff1Sjsg 	bool was_crtc_enabled = old_crtc_state->hw.active;
4861bb76ff1Sjsg 	bool is_crtc_enabled = new_crtc_state->hw.active;
4871bb76ff1Sjsg 	bool turn_off, turn_on, visible, was_visible;
4881bb76ff1Sjsg 	int ret;
4891bb76ff1Sjsg 
4901bb76ff1Sjsg 	if (DISPLAY_VER(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
4911bb76ff1Sjsg 		ret = skl_update_scaler_plane(new_crtc_state, new_plane_state);
4921bb76ff1Sjsg 		if (ret)
4931bb76ff1Sjsg 			return ret;
4941bb76ff1Sjsg 	}
4951bb76ff1Sjsg 
4961bb76ff1Sjsg 	was_visible = old_plane_state->uapi.visible;
4971bb76ff1Sjsg 	visible = new_plane_state->uapi.visible;
4981bb76ff1Sjsg 
4991bb76ff1Sjsg 	if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
5001bb76ff1Sjsg 		was_visible = false;
5011bb76ff1Sjsg 
5021bb76ff1Sjsg 	/*
5031bb76ff1Sjsg 	 * Visibility is calculated as if the crtc was on, but
5041bb76ff1Sjsg 	 * after scaler setup everything depends on it being off
5051bb76ff1Sjsg 	 * when the crtc isn't active.
5061bb76ff1Sjsg 	 *
5071bb76ff1Sjsg 	 * FIXME this is wrong for watermarks. Watermarks should also
5081bb76ff1Sjsg 	 * be computed as if the pipe would be active. Perhaps move
5091bb76ff1Sjsg 	 * per-plane wm computation to the .check_plane() hook, and
5101bb76ff1Sjsg 	 * only combine the results from all planes in the current place?
5111bb76ff1Sjsg 	 */
5121bb76ff1Sjsg 	if (!is_crtc_enabled) {
5131bb76ff1Sjsg 		intel_plane_set_invisible(new_crtc_state, new_plane_state);
5141bb76ff1Sjsg 		visible = false;
5151bb76ff1Sjsg 	}
5161bb76ff1Sjsg 
5171bb76ff1Sjsg 	if (!was_visible && !visible)
5181bb76ff1Sjsg 		return 0;
5191bb76ff1Sjsg 
5201bb76ff1Sjsg 	turn_off = was_visible && (!visible || mode_changed);
5211bb76ff1Sjsg 	turn_on = visible && (!was_visible || mode_changed);
5221bb76ff1Sjsg 
5231bb76ff1Sjsg 	drm_dbg_atomic(&dev_priv->drm,
5241bb76ff1Sjsg 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
5251bb76ff1Sjsg 		       crtc->base.base.id, crtc->base.name,
5261bb76ff1Sjsg 		       plane->base.base.id, plane->base.name,
5271bb76ff1Sjsg 		       was_visible, visible,
5281bb76ff1Sjsg 		       turn_off, turn_on, mode_changed);
5291bb76ff1Sjsg 
5301bb76ff1Sjsg 	if (turn_on) {
5311bb76ff1Sjsg 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
5321bb76ff1Sjsg 			new_crtc_state->update_wm_pre = true;
5331bb76ff1Sjsg 	} else if (turn_off) {
5341bb76ff1Sjsg 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
5351bb76ff1Sjsg 			new_crtc_state->update_wm_post = true;
5361bb76ff1Sjsg 	} else if (intel_wm_need_update(old_plane_state, new_plane_state)) {
5371bb76ff1Sjsg 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv)) {
5381bb76ff1Sjsg 			/* FIXME bollocks */
5391bb76ff1Sjsg 			new_crtc_state->update_wm_pre = true;
5401bb76ff1Sjsg 			new_crtc_state->update_wm_post = true;
5411bb76ff1Sjsg 		}
5421bb76ff1Sjsg 	}
5431bb76ff1Sjsg 
5441bb76ff1Sjsg 	if (visible || was_visible)
5451bb76ff1Sjsg 		new_crtc_state->fb_bits |= plane->frontbuffer_bit;
5461bb76ff1Sjsg 
547*f005ef32Sjsg 	if (HAS_GMCH(dev_priv) &&
548*f005ef32Sjsg 	    i9xx_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
549*f005ef32Sjsg 		new_crtc_state->disable_cxsr = true;
550*f005ef32Sjsg 
5511bb76ff1Sjsg 	/*
5521bb76ff1Sjsg 	 * ILK/SNB DVSACNTR/Sprite Enable
5531bb76ff1Sjsg 	 * IVB SPR_CTL/Sprite Enable
5541bb76ff1Sjsg 	 * "When in Self Refresh Big FIFO mode, a write to enable the
5551bb76ff1Sjsg 	 *  plane will be internally buffered and delayed while Big FIFO
5561bb76ff1Sjsg 	 *  mode is exiting."
5571bb76ff1Sjsg 	 *
5581bb76ff1Sjsg 	 * Which means that enabling the sprite can take an extra frame
5591bb76ff1Sjsg 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
5601bb76ff1Sjsg 	 * down to LP0 and wait for vblank in order to make sure the
5611bb76ff1Sjsg 	 * sprite gets enabled on the next vblank after the register write.
5621bb76ff1Sjsg 	 * Doing otherwise would risk enabling the sprite one frame after
5631bb76ff1Sjsg 	 * we've already signalled flip completion. We can resume LP1+
5641bb76ff1Sjsg 	 * once the sprite has been enabled.
5651bb76ff1Sjsg 	 *
5661bb76ff1Sjsg 	 *
5671bb76ff1Sjsg 	 * WaCxSRDisabledForSpriteScaling:ivb
5681bb76ff1Sjsg 	 * IVB SPR_SCALE/Scaling Enable
5691bb76ff1Sjsg 	 * "Low Power watermarks must be disabled for at least one
5701bb76ff1Sjsg 	 *  frame before enabling sprite scaling, and kept disabled
5711bb76ff1Sjsg 	 *  until sprite scaling is disabled."
5721bb76ff1Sjsg 	 *
5731bb76ff1Sjsg 	 * ILK/SNB DVSASCALE/Scaling Enable
5741bb76ff1Sjsg 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
5751bb76ff1Sjsg 	 *  masked off while Big FIFO mode is exiting."
5761bb76ff1Sjsg 	 *
5771bb76ff1Sjsg 	 * Despite the w/a only being listed for IVB we assume that
5781bb76ff1Sjsg 	 * the ILK/SNB note has similar ramifications, hence we apply
5791bb76ff1Sjsg 	 * the w/a on all three platforms.
5801bb76ff1Sjsg 	 *
5811bb76ff1Sjsg 	 * With experimental results seems this is needed also for primary
5821bb76ff1Sjsg 	 * plane, not only sprite plane.
5831bb76ff1Sjsg 	 */
5841bb76ff1Sjsg 	if (plane->id != PLANE_CURSOR &&
5851bb76ff1Sjsg 	    (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) ||
5861bb76ff1Sjsg 	     IS_IVYBRIDGE(dev_priv)) &&
5871bb76ff1Sjsg 	    (turn_on || (!intel_plane_is_scaled(old_plane_state) &&
5881bb76ff1Sjsg 			 intel_plane_is_scaled(new_plane_state))))
5891bb76ff1Sjsg 		new_crtc_state->disable_lp_wm = true;
5901bb76ff1Sjsg 
591*f005ef32Sjsg 	if (intel_plane_do_async_flip(plane, old_crtc_state, new_crtc_state)) {
5921bb76ff1Sjsg 		new_crtc_state->do_async_flip = true;
593*f005ef32Sjsg 		new_crtc_state->async_flip_planes |= BIT(plane->id);
594*f005ef32Sjsg 	}
5951bb76ff1Sjsg 
5961bb76ff1Sjsg 	return 0;
5971bb76ff1Sjsg }
5981bb76ff1Sjsg 
intel_plane_atomic_check_with_state(const struct intel_crtc_state * old_crtc_state,struct intel_crtc_state * new_crtc_state,const struct intel_plane_state * old_plane_state,struct intel_plane_state * new_plane_state)599c349dbc7Sjsg int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state,
600c349dbc7Sjsg 					struct intel_crtc_state *new_crtc_state,
601c349dbc7Sjsg 					const struct intel_plane_state *old_plane_state,
602c349dbc7Sjsg 					struct intel_plane_state *new_plane_state)
603c349dbc7Sjsg {
604c349dbc7Sjsg 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
605c349dbc7Sjsg 	const struct drm_framebuffer *fb = new_plane_state->hw.fb;
606c349dbc7Sjsg 	int ret;
607c349dbc7Sjsg 
608ad8b1aafSjsg 	intel_plane_set_invisible(new_crtc_state, new_plane_state);
6095ca02815Sjsg 	new_crtc_state->enabled_planes &= ~BIT(plane->id);
610c349dbc7Sjsg 
611c349dbc7Sjsg 	if (!new_plane_state->hw.crtc && !old_plane_state->hw.crtc)
612c349dbc7Sjsg 		return 0;
613c349dbc7Sjsg 
614c349dbc7Sjsg 	ret = plane->check_plane(new_crtc_state, new_plane_state);
615c349dbc7Sjsg 	if (ret)
616c349dbc7Sjsg 		return ret;
617c349dbc7Sjsg 
6185ca02815Sjsg 	if (fb)
6195ca02815Sjsg 		new_crtc_state->enabled_planes |= BIT(plane->id);
6205ca02815Sjsg 
621c349dbc7Sjsg 	/* FIXME pre-g4x don't work like this */
622c349dbc7Sjsg 	if (new_plane_state->uapi.visible)
623c349dbc7Sjsg 		new_crtc_state->active_planes |= BIT(plane->id);
624c349dbc7Sjsg 
625c349dbc7Sjsg 	if (new_plane_state->uapi.visible &&
6261bb76ff1Sjsg 	    intel_plane_is_scaled(new_plane_state))
6271bb76ff1Sjsg 		new_crtc_state->scaled_planes |= BIT(plane->id);
6281bb76ff1Sjsg 
6291bb76ff1Sjsg 	if (new_plane_state->uapi.visible &&
630c349dbc7Sjsg 	    intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
631c349dbc7Sjsg 		new_crtc_state->nv12_planes |= BIT(plane->id);
632c349dbc7Sjsg 
633c349dbc7Sjsg 	if (new_plane_state->uapi.visible &&
634c349dbc7Sjsg 	    fb->format->format == DRM_FORMAT_C8)
635c349dbc7Sjsg 		new_crtc_state->c8_planes |= BIT(plane->id);
636c349dbc7Sjsg 
637c349dbc7Sjsg 	if (new_plane_state->uapi.visible || old_plane_state->uapi.visible)
638c349dbc7Sjsg 		new_crtc_state->update_planes |= BIT(plane->id);
639c349dbc7Sjsg 
6401bb76ff1Sjsg 	if (new_plane_state->uapi.visible &&
6411bb76ff1Sjsg 	    intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
6421bb76ff1Sjsg 		new_crtc_state->data_rate_y[plane->id] =
6431bb76ff1Sjsg 			intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
644c349dbc7Sjsg 		new_crtc_state->data_rate[plane->id] =
6451bb76ff1Sjsg 			intel_plane_data_rate(new_crtc_state, new_plane_state, 1);
6461bb76ff1Sjsg 
6471bb76ff1Sjsg 		new_crtc_state->rel_data_rate_y[plane->id] =
6481bb76ff1Sjsg 			intel_plane_relative_data_rate(new_crtc_state,
6491bb76ff1Sjsg 						       new_plane_state, 0);
6501bb76ff1Sjsg 		new_crtc_state->rel_data_rate[plane->id] =
6511bb76ff1Sjsg 			intel_plane_relative_data_rate(new_crtc_state,
6521bb76ff1Sjsg 						       new_plane_state, 1);
6531bb76ff1Sjsg 	} else if (new_plane_state->uapi.visible) {
6541bb76ff1Sjsg 		new_crtc_state->data_rate[plane->id] =
6551bb76ff1Sjsg 			intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
6561bb76ff1Sjsg 
6571bb76ff1Sjsg 		new_crtc_state->rel_data_rate[plane->id] =
6581bb76ff1Sjsg 			intel_plane_relative_data_rate(new_crtc_state,
6591bb76ff1Sjsg 						       new_plane_state, 0);
6601bb76ff1Sjsg 	}
661c349dbc7Sjsg 
662c349dbc7Sjsg 	return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state,
663c349dbc7Sjsg 					       old_plane_state, new_plane_state);
664c349dbc7Sjsg }
665c349dbc7Sjsg 
6665ca02815Sjsg static struct intel_plane *
intel_crtc_get_plane(struct intel_crtc * crtc,enum plane_id plane_id)6675ca02815Sjsg intel_crtc_get_plane(struct intel_crtc *crtc, enum plane_id plane_id)
668c349dbc7Sjsg {
6695ca02815Sjsg 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
6705ca02815Sjsg 	struct intel_plane *plane;
671c349dbc7Sjsg 
6725ca02815Sjsg 	for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
6735ca02815Sjsg 		if (plane->id == plane_id)
6745ca02815Sjsg 			return plane;
6755ca02815Sjsg 	}
676c349dbc7Sjsg 
677c349dbc7Sjsg 	return NULL;
678c349dbc7Sjsg }
679c349dbc7Sjsg 
intel_plane_atomic_check(struct intel_atomic_state * state,struct intel_plane * plane)680c349dbc7Sjsg int intel_plane_atomic_check(struct intel_atomic_state *state,
681c349dbc7Sjsg 			     struct intel_plane *plane)
682c349dbc7Sjsg {
6835ca02815Sjsg 	struct drm_i915_private *i915 = to_i915(state->base.dev);
684c349dbc7Sjsg 	struct intel_plane_state *new_plane_state =
685c349dbc7Sjsg 		intel_atomic_get_new_plane_state(state, plane);
686c349dbc7Sjsg 	const struct intel_plane_state *old_plane_state =
687c349dbc7Sjsg 		intel_atomic_get_old_plane_state(state, plane);
6885ca02815Sjsg 	const struct intel_plane_state *new_master_plane_state;
6891bb76ff1Sjsg 	struct intel_crtc *crtc = intel_crtc_for_pipe(i915, plane->pipe);
6905ca02815Sjsg 	const struct intel_crtc_state *old_crtc_state =
6915ca02815Sjsg 		intel_atomic_get_old_crtc_state(state, crtc);
6925ca02815Sjsg 	struct intel_crtc_state *new_crtc_state =
6935ca02815Sjsg 		intel_atomic_get_new_crtc_state(state, crtc);
694c349dbc7Sjsg 
6951bb76ff1Sjsg 	if (new_crtc_state && intel_crtc_is_bigjoiner_slave(new_crtc_state)) {
6961bb76ff1Sjsg 		struct intel_crtc *master_crtc =
6971bb76ff1Sjsg 			intel_master_crtc(new_crtc_state);
6985ca02815Sjsg 		struct intel_plane *master_plane =
6991bb76ff1Sjsg 			intel_crtc_get_plane(master_crtc, plane->id);
7005ca02815Sjsg 
7015ca02815Sjsg 		new_master_plane_state =
7025ca02815Sjsg 			intel_atomic_get_new_plane_state(state, master_plane);
7035ca02815Sjsg 	} else {
7045ca02815Sjsg 		new_master_plane_state = new_plane_state;
7055ca02815Sjsg 	}
7065ca02815Sjsg 
7075ca02815Sjsg 	intel_plane_copy_uapi_to_hw_state(new_plane_state,
7085ca02815Sjsg 					  new_master_plane_state,
7095ca02815Sjsg 					  crtc);
7105ca02815Sjsg 
711c349dbc7Sjsg 	new_plane_state->uapi.visible = false;
7125ca02815Sjsg 	if (!new_crtc_state)
713c349dbc7Sjsg 		return 0;
714c349dbc7Sjsg 
715c349dbc7Sjsg 	return intel_plane_atomic_check_with_state(old_crtc_state,
716c349dbc7Sjsg 						   new_crtc_state,
717c349dbc7Sjsg 						   old_plane_state,
718c349dbc7Sjsg 						   new_plane_state);
719c349dbc7Sjsg }
720c349dbc7Sjsg 
721c349dbc7Sjsg static struct intel_plane *
skl_next_plane_to_commit(struct intel_atomic_state * state,struct intel_crtc * crtc,struct skl_ddb_entry ddb[I915_MAX_PLANES],struct skl_ddb_entry ddb_y[I915_MAX_PLANES],unsigned int * update_mask)722c349dbc7Sjsg skl_next_plane_to_commit(struct intel_atomic_state *state,
723c349dbc7Sjsg 			 struct intel_crtc *crtc,
7241bb76ff1Sjsg 			 struct skl_ddb_entry ddb[I915_MAX_PLANES],
7251bb76ff1Sjsg 			 struct skl_ddb_entry ddb_y[I915_MAX_PLANES],
726c349dbc7Sjsg 			 unsigned int *update_mask)
727c349dbc7Sjsg {
728c349dbc7Sjsg 	struct intel_crtc_state *crtc_state =
729c349dbc7Sjsg 		intel_atomic_get_new_crtc_state(state, crtc);
730*f005ef32Sjsg 	struct intel_plane_state __maybe_unused *plane_state;
731c349dbc7Sjsg 	struct intel_plane *plane;
732c349dbc7Sjsg 	int i;
733c349dbc7Sjsg 
734c349dbc7Sjsg 	if (*update_mask == 0)
735c349dbc7Sjsg 		return NULL;
736c349dbc7Sjsg 
737c349dbc7Sjsg 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
738c349dbc7Sjsg 		enum plane_id plane_id = plane->id;
739c349dbc7Sjsg 
740c349dbc7Sjsg 		if (crtc->pipe != plane->pipe ||
741c349dbc7Sjsg 		    !(*update_mask & BIT(plane_id)))
742c349dbc7Sjsg 			continue;
743c349dbc7Sjsg 
7441bb76ff1Sjsg 		if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb[plane_id],
7451bb76ff1Sjsg 						ddb, I915_MAX_PLANES, plane_id) ||
7461bb76ff1Sjsg 		    skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
7471bb76ff1Sjsg 						ddb_y, I915_MAX_PLANES, plane_id))
748c349dbc7Sjsg 			continue;
749c349dbc7Sjsg 
750c349dbc7Sjsg 		*update_mask &= ~BIT(plane_id);
7511bb76ff1Sjsg 		ddb[plane_id] = crtc_state->wm.skl.plane_ddb[plane_id];
7521bb76ff1Sjsg 		ddb_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
753c349dbc7Sjsg 
754c349dbc7Sjsg 		return plane;
755c349dbc7Sjsg 	}
756c349dbc7Sjsg 
757c349dbc7Sjsg 	/* should never happen */
758ad8b1aafSjsg 	drm_WARN_ON(state->base.dev, 1);
759c349dbc7Sjsg 
760c349dbc7Sjsg 	return NULL;
761c349dbc7Sjsg }
762c349dbc7Sjsg 
intel_plane_update_noarm(struct intel_plane * plane,const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)7631bb76ff1Sjsg void intel_plane_update_noarm(struct intel_plane *plane,
764c349dbc7Sjsg 			      const struct intel_crtc_state *crtc_state,
765c349dbc7Sjsg 			      const struct intel_plane_state *plane_state)
766c349dbc7Sjsg {
767c349dbc7Sjsg 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
768c349dbc7Sjsg 
769*f005ef32Sjsg 	trace_intel_plane_update_noarm(plane, crtc);
7705ca02815Sjsg 
7711bb76ff1Sjsg 	if (plane->update_noarm)
7721bb76ff1Sjsg 		plane->update_noarm(plane, crtc_state, plane_state);
773c349dbc7Sjsg }
774c349dbc7Sjsg 
intel_plane_update_arm(struct intel_plane * plane,const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)7751bb76ff1Sjsg void intel_plane_update_arm(struct intel_plane *plane,
7761bb76ff1Sjsg 			    const struct intel_crtc_state *crtc_state,
7771bb76ff1Sjsg 			    const struct intel_plane_state *plane_state)
7781bb76ff1Sjsg {
7791bb76ff1Sjsg 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7801bb76ff1Sjsg 
781*f005ef32Sjsg 	trace_intel_plane_update_arm(plane, crtc);
7821bb76ff1Sjsg 
7831bb76ff1Sjsg 	if (crtc_state->do_async_flip && plane->async_flip)
7841bb76ff1Sjsg 		plane->async_flip(plane, crtc_state, plane_state, true);
7851bb76ff1Sjsg 	else
7861bb76ff1Sjsg 		plane->update_arm(plane, crtc_state, plane_state);
7871bb76ff1Sjsg }
7881bb76ff1Sjsg 
intel_plane_disable_arm(struct intel_plane * plane,const struct intel_crtc_state * crtc_state)7891bb76ff1Sjsg void intel_plane_disable_arm(struct intel_plane *plane,
790c349dbc7Sjsg 			     const struct intel_crtc_state *crtc_state)
791c349dbc7Sjsg {
792c349dbc7Sjsg 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
793c349dbc7Sjsg 
794*f005ef32Sjsg 	trace_intel_plane_disable_arm(plane, crtc);
7951bb76ff1Sjsg 	plane->disable_arm(plane, crtc_state);
796c349dbc7Sjsg }
797c349dbc7Sjsg 
intel_crtc_planes_update_noarm(struct intel_atomic_state * state,struct intel_crtc * crtc)7981bb76ff1Sjsg void intel_crtc_planes_update_noarm(struct intel_atomic_state *state,
7991bb76ff1Sjsg 				    struct intel_crtc *crtc)
8001bb76ff1Sjsg {
8011bb76ff1Sjsg 	struct intel_crtc_state *new_crtc_state =
8021bb76ff1Sjsg 		intel_atomic_get_new_crtc_state(state, crtc);
8031bb76ff1Sjsg 	u32 update_mask = new_crtc_state->update_planes;
8041bb76ff1Sjsg 	struct intel_plane_state *new_plane_state;
8051bb76ff1Sjsg 	struct intel_plane *plane;
8061bb76ff1Sjsg 	int i;
8071bb76ff1Sjsg 
8081bb76ff1Sjsg 	if (new_crtc_state->do_async_flip)
8091bb76ff1Sjsg 		return;
8101bb76ff1Sjsg 
8111bb76ff1Sjsg 	/*
8121bb76ff1Sjsg 	 * Since we only write non-arming registers here,
8131bb76ff1Sjsg 	 * the order does not matter even for skl+.
8141bb76ff1Sjsg 	 */
8151bb76ff1Sjsg 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
8161bb76ff1Sjsg 		if (crtc->pipe != plane->pipe ||
8171bb76ff1Sjsg 		    !(update_mask & BIT(plane->id)))
8181bb76ff1Sjsg 			continue;
8191bb76ff1Sjsg 
8201bb76ff1Sjsg 		/* TODO: for mailbox updates this should be skipped */
8211bb76ff1Sjsg 		if (new_plane_state->uapi.visible ||
8221bb76ff1Sjsg 		    new_plane_state->planar_slave)
8231bb76ff1Sjsg 			intel_plane_update_noarm(plane, new_crtc_state, new_plane_state);
8241bb76ff1Sjsg 	}
8251bb76ff1Sjsg }
8261bb76ff1Sjsg 
skl_crtc_planes_update_arm(struct intel_atomic_state * state,struct intel_crtc * crtc)8271bb76ff1Sjsg static void skl_crtc_planes_update_arm(struct intel_atomic_state *state,
828c349dbc7Sjsg 				       struct intel_crtc *crtc)
829c349dbc7Sjsg {
830c349dbc7Sjsg 	struct intel_crtc_state *old_crtc_state =
831c349dbc7Sjsg 		intel_atomic_get_old_crtc_state(state, crtc);
832c349dbc7Sjsg 	struct intel_crtc_state *new_crtc_state =
833c349dbc7Sjsg 		intel_atomic_get_new_crtc_state(state, crtc);
8341bb76ff1Sjsg 	struct skl_ddb_entry ddb[I915_MAX_PLANES];
8351bb76ff1Sjsg 	struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
836c349dbc7Sjsg 	u32 update_mask = new_crtc_state->update_planes;
837c349dbc7Sjsg 	struct intel_plane *plane;
838c349dbc7Sjsg 
8391bb76ff1Sjsg 	memcpy(ddb, old_crtc_state->wm.skl.plane_ddb,
8401bb76ff1Sjsg 	       sizeof(old_crtc_state->wm.skl.plane_ddb));
8411bb76ff1Sjsg 	memcpy(ddb_y, old_crtc_state->wm.skl.plane_ddb_y,
842c349dbc7Sjsg 	       sizeof(old_crtc_state->wm.skl.plane_ddb_y));
843c349dbc7Sjsg 
8441bb76ff1Sjsg 	while ((plane = skl_next_plane_to_commit(state, crtc, ddb, ddb_y, &update_mask))) {
845c349dbc7Sjsg 		struct intel_plane_state *new_plane_state =
846c349dbc7Sjsg 			intel_atomic_get_new_plane_state(state, plane);
847c349dbc7Sjsg 
8481bb76ff1Sjsg 		/*
8491bb76ff1Sjsg 		 * TODO: for mailbox updates intel_plane_update_noarm()
8501bb76ff1Sjsg 		 * would have to be called here as well.
8511bb76ff1Sjsg 		 */
852c349dbc7Sjsg 		if (new_plane_state->uapi.visible ||
8531bb76ff1Sjsg 		    new_plane_state->planar_slave)
8541bb76ff1Sjsg 			intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
8551bb76ff1Sjsg 		else
8561bb76ff1Sjsg 			intel_plane_disable_arm(plane, new_crtc_state);
857c349dbc7Sjsg 	}
858c349dbc7Sjsg }
859c349dbc7Sjsg 
i9xx_crtc_planes_update_arm(struct intel_atomic_state * state,struct intel_crtc * crtc)8601bb76ff1Sjsg static void i9xx_crtc_planes_update_arm(struct intel_atomic_state *state,
861c349dbc7Sjsg 					struct intel_crtc *crtc)
862c349dbc7Sjsg {
863c349dbc7Sjsg 	struct intel_crtc_state *new_crtc_state =
864c349dbc7Sjsg 		intel_atomic_get_new_crtc_state(state, crtc);
865c349dbc7Sjsg 	u32 update_mask = new_crtc_state->update_planes;
866c349dbc7Sjsg 	struct intel_plane_state *new_plane_state;
867c349dbc7Sjsg 	struct intel_plane *plane;
868c349dbc7Sjsg 	int i;
869c349dbc7Sjsg 
870c349dbc7Sjsg 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
871c349dbc7Sjsg 		if (crtc->pipe != plane->pipe ||
872c349dbc7Sjsg 		    !(update_mask & BIT(plane->id)))
873c349dbc7Sjsg 			continue;
874c349dbc7Sjsg 
8751bb76ff1Sjsg 		/*
8761bb76ff1Sjsg 		 * TODO: for mailbox updates intel_plane_update_noarm()
8771bb76ff1Sjsg 		 * would have to be called here as well.
8781bb76ff1Sjsg 		 */
879c349dbc7Sjsg 		if (new_plane_state->uapi.visible)
8801bb76ff1Sjsg 			intel_plane_update_arm(plane, new_crtc_state, new_plane_state);
881c349dbc7Sjsg 		else
8821bb76ff1Sjsg 			intel_plane_disable_arm(plane, new_crtc_state);
883c349dbc7Sjsg 	}
884c349dbc7Sjsg }
885c349dbc7Sjsg 
intel_crtc_planes_update_arm(struct intel_atomic_state * state,struct intel_crtc * crtc)8861bb76ff1Sjsg void intel_crtc_planes_update_arm(struct intel_atomic_state *state,
8871bb76ff1Sjsg 				  struct intel_crtc *crtc)
8881bb76ff1Sjsg {
8891bb76ff1Sjsg 	struct drm_i915_private *i915 = to_i915(state->base.dev);
8901bb76ff1Sjsg 
8911bb76ff1Sjsg 	if (DISPLAY_VER(i915) >= 9)
8921bb76ff1Sjsg 		skl_crtc_planes_update_arm(state, crtc);
8931bb76ff1Sjsg 	else
8941bb76ff1Sjsg 		i9xx_crtc_planes_update_arm(state, crtc);
8951bb76ff1Sjsg }
8961bb76ff1Sjsg 
intel_atomic_plane_check_clipping(struct intel_plane_state * plane_state,struct intel_crtc_state * crtc_state,int min_scale,int max_scale,bool can_position)8975ca02815Sjsg int intel_atomic_plane_check_clipping(struct intel_plane_state *plane_state,
8985ca02815Sjsg 				      struct intel_crtc_state *crtc_state,
8995ca02815Sjsg 				      int min_scale, int max_scale,
9005ca02815Sjsg 				      bool can_position)
9015ca02815Sjsg {
9021bb76ff1Sjsg 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
9035ca02815Sjsg 	struct drm_framebuffer *fb = plane_state->hw.fb;
9045ca02815Sjsg 	struct drm_rect *src = &plane_state->uapi.src;
9055ca02815Sjsg 	struct drm_rect *dst = &plane_state->uapi.dst;
9061bb76ff1Sjsg 	const struct drm_rect *clip = &crtc_state->pipe_src;
9075ca02815Sjsg 	unsigned int rotation = plane_state->hw.rotation;
9085ca02815Sjsg 	int hscale, vscale;
9095ca02815Sjsg 
9105ca02815Sjsg 	if (!fb) {
9115ca02815Sjsg 		plane_state->uapi.visible = false;
9125ca02815Sjsg 		return 0;
9135ca02815Sjsg 	}
9145ca02815Sjsg 
9155ca02815Sjsg 	drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
9165ca02815Sjsg 
9175ca02815Sjsg 	/* Check scaling */
9185ca02815Sjsg 	hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
9195ca02815Sjsg 	vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
9205ca02815Sjsg 	if (hscale < 0 || vscale < 0) {
9211bb76ff1Sjsg 		drm_dbg_kms(&i915->drm, "Invalid scaling of plane\n");
9225ca02815Sjsg 		drm_rect_debug_print("src: ", src, true);
9235ca02815Sjsg 		drm_rect_debug_print("dst: ", dst, false);
9245ca02815Sjsg 		return -ERANGE;
9255ca02815Sjsg 	}
9265ca02815Sjsg 
9275ca02815Sjsg 	/*
9285ca02815Sjsg 	 * FIXME: This might need further adjustment for seamless scaling
9295ca02815Sjsg 	 * with phase information, for the 2p2 and 2p1 scenarios.
9305ca02815Sjsg 	 */
9311bb76ff1Sjsg 	plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, clip);
9325ca02815Sjsg 
9335ca02815Sjsg 	drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
9345ca02815Sjsg 
9355ca02815Sjsg 	if (!can_position && plane_state->uapi.visible &&
9361bb76ff1Sjsg 	    !drm_rect_equals(dst, clip)) {
9371bb76ff1Sjsg 		drm_dbg_kms(&i915->drm, "Plane must cover entire CRTC\n");
9385ca02815Sjsg 		drm_rect_debug_print("dst: ", dst, false);
9391bb76ff1Sjsg 		drm_rect_debug_print("clip: ", clip, false);
9405ca02815Sjsg 		return -EINVAL;
9415ca02815Sjsg 	}
9425ca02815Sjsg 
9431bb76ff1Sjsg 	/* final plane coordinates will be relative to the plane's pipe */
9441bb76ff1Sjsg 	drm_rect_translate(dst, -clip->x1, -clip->y1);
9451bb76ff1Sjsg 
9465ca02815Sjsg 	return 0;
9475ca02815Sjsg }
9485ca02815Sjsg 
intel_plane_check_src_coordinates(struct intel_plane_state * plane_state)949*f005ef32Sjsg int intel_plane_check_src_coordinates(struct intel_plane_state *plane_state)
9501bb76ff1Sjsg {
951*f005ef32Sjsg 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
952*f005ef32Sjsg 	const struct drm_framebuffer *fb = plane_state->hw.fb;
953*f005ef32Sjsg 	struct drm_rect *src = &plane_state->uapi.src;
954*f005ef32Sjsg 	u32 src_x, src_y, src_w, src_h, hsub, vsub;
955*f005ef32Sjsg 	bool rotated = drm_rotation_90_or_270(plane_state->hw.rotation);
9561bb76ff1Sjsg 
9571bb76ff1Sjsg 	/*
958*f005ef32Sjsg 	 * FIXME hsub/vsub vs. block size is a mess. Pre-tgl CCS
959*f005ef32Sjsg 	 * abuses hsub/vsub so we can't use them here. But as they
960*f005ef32Sjsg 	 * are limited to 32bpp RGB formats we don't actually need
961*f005ef32Sjsg 	 * to check anything.
9621bb76ff1Sjsg 	 */
963*f005ef32Sjsg 	if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
964*f005ef32Sjsg 	    fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS)
965*f005ef32Sjsg 		return 0;
9661bb76ff1Sjsg 
967*f005ef32Sjsg 	/*
968*f005ef32Sjsg 	 * Hardware doesn't handle subpixel coordinates.
969*f005ef32Sjsg 	 * Adjust to (macro)pixel boundary, but be careful not to
970*f005ef32Sjsg 	 * increase the source viewport size, because that could
971*f005ef32Sjsg 	 * push the downscaling factor out of bounds.
972*f005ef32Sjsg 	 */
973*f005ef32Sjsg 	src_x = src->x1 >> 16;
974*f005ef32Sjsg 	src_w = drm_rect_width(src) >> 16;
975*f005ef32Sjsg 	src_y = src->y1 >> 16;
976*f005ef32Sjsg 	src_h = drm_rect_height(src) >> 16;
9771bb76ff1Sjsg 
978*f005ef32Sjsg 	drm_rect_init(src, src_x << 16, src_y << 16,
979*f005ef32Sjsg 		      src_w << 16, src_h << 16);
980*f005ef32Sjsg 
981*f005ef32Sjsg 	if (fb->format->format == DRM_FORMAT_RGB565 && rotated) {
982*f005ef32Sjsg 		hsub = 2;
983*f005ef32Sjsg 		vsub = 2;
984*f005ef32Sjsg 	} else {
985*f005ef32Sjsg 		hsub = fb->format->hsub;
986*f005ef32Sjsg 		vsub = fb->format->vsub;
9871bb76ff1Sjsg 	}
9881bb76ff1Sjsg 
989*f005ef32Sjsg 	if (rotated)
990*f005ef32Sjsg 		hsub = vsub = max(hsub, vsub);
9911bb76ff1Sjsg 
992*f005ef32Sjsg 	if (src_x % hsub || src_w % hsub) {
993*f005ef32Sjsg 		drm_dbg_kms(&i915->drm, "src x/w (%u, %u) must be a multiple of %u (rotated: %s)\n",
994*f005ef32Sjsg 			    src_x, src_w, hsub, str_yes_no(rotated));
995*f005ef32Sjsg 		return -EINVAL;
9961bb76ff1Sjsg 	}
9971bb76ff1Sjsg 
998*f005ef32Sjsg 	if (src_y % vsub || src_h % vsub) {
999*f005ef32Sjsg 		drm_dbg_kms(&i915->drm, "src y/h (%u, %u) must be a multiple of %u (rotated: %s)\n",
1000*f005ef32Sjsg 			    src_y, src_h, vsub, str_yes_no(rotated));
1001*f005ef32Sjsg 		return -EINVAL;
1002*f005ef32Sjsg 	}
10031bb76ff1Sjsg 
1004*f005ef32Sjsg 	return 0;
10051bb76ff1Sjsg }
10061bb76ff1Sjsg 
10071bb76ff1Sjsg /**
10081bb76ff1Sjsg  * intel_prepare_plane_fb - Prepare fb for usage on plane
10091bb76ff1Sjsg  * @_plane: drm plane to prepare for
10101bb76ff1Sjsg  * @_new_plane_state: the plane state being prepared
10111bb76ff1Sjsg  *
10121bb76ff1Sjsg  * Prepares a framebuffer for usage on a display plane.  Generally this
10131bb76ff1Sjsg  * involves pinning the underlying object and updating the frontbuffer tracking
10141bb76ff1Sjsg  * bits.  Some older platforms need special physical address handling for
10151bb76ff1Sjsg  * cursor planes.
10161bb76ff1Sjsg  *
10171bb76ff1Sjsg  * Returns 0 on success, negative error code on failure.
10181bb76ff1Sjsg  */
10191bb76ff1Sjsg static int
intel_prepare_plane_fb(struct drm_plane * _plane,struct drm_plane_state * _new_plane_state)10201bb76ff1Sjsg intel_prepare_plane_fb(struct drm_plane *_plane,
10211bb76ff1Sjsg 		       struct drm_plane_state *_new_plane_state)
10221bb76ff1Sjsg {
10231bb76ff1Sjsg 	struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY };
10241bb76ff1Sjsg 	struct intel_plane *plane = to_intel_plane(_plane);
10251bb76ff1Sjsg 	struct intel_plane_state *new_plane_state =
10261bb76ff1Sjsg 		to_intel_plane_state(_new_plane_state);
10271bb76ff1Sjsg 	struct intel_atomic_state *state =
10281bb76ff1Sjsg 		to_intel_atomic_state(new_plane_state->uapi.state);
10291bb76ff1Sjsg 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10301bb76ff1Sjsg 	const struct intel_plane_state *old_plane_state =
10311bb76ff1Sjsg 		intel_atomic_get_old_plane_state(state, plane);
10321bb76ff1Sjsg 	struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
10331bb76ff1Sjsg 	struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
10341bb76ff1Sjsg 	int ret;
10351bb76ff1Sjsg 
10361bb76ff1Sjsg 	if (old_obj) {
10377e2e87fcSjsg 		const struct intel_crtc_state *new_crtc_state =
10381bb76ff1Sjsg 			intel_atomic_get_new_crtc_state(state,
10391bb76ff1Sjsg 							to_intel_crtc(old_plane_state->hw.crtc));
10401bb76ff1Sjsg 
10411bb76ff1Sjsg 		/* Big Hammer, we also need to ensure that any pending
10421bb76ff1Sjsg 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
10431bb76ff1Sjsg 		 * current scanout is retired before unpinning the old
10441bb76ff1Sjsg 		 * framebuffer. Note that we rely on userspace rendering
10451bb76ff1Sjsg 		 * into the buffer attached to the pipe they are waiting
10461bb76ff1Sjsg 		 * on. If not, userspace generates a GPU hang with IPEHR
10471bb76ff1Sjsg 		 * point to the MI_WAIT_FOR_EVENT.
10481bb76ff1Sjsg 		 *
10491bb76ff1Sjsg 		 * This should only fail upon a hung GPU, in which case we
10501bb76ff1Sjsg 		 * can safely continue.
10511bb76ff1Sjsg 		 */
10527e2e87fcSjsg 		if (new_crtc_state && intel_crtc_needs_modeset(new_crtc_state)) {
10531bb76ff1Sjsg 			ret = i915_sw_fence_await_reservation(&state->commit_ready,
1054*f005ef32Sjsg 							      old_obj->base.resv,
10551bb76ff1Sjsg 							      false, 0,
10561bb76ff1Sjsg 							      GFP_KERNEL);
10571bb76ff1Sjsg 			if (ret < 0)
10581bb76ff1Sjsg 				return ret;
10591bb76ff1Sjsg 		}
10601bb76ff1Sjsg 	}
10611bb76ff1Sjsg 
10621bb76ff1Sjsg 	if (new_plane_state->uapi.fence) { /* explicit fencing */
10631bb76ff1Sjsg 		i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
10641bb76ff1Sjsg 					     &attr);
10651bb76ff1Sjsg 		ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
10661bb76ff1Sjsg 						    new_plane_state->uapi.fence,
10671bb76ff1Sjsg 						    i915_fence_timeout(dev_priv),
10681bb76ff1Sjsg 						    GFP_KERNEL);
10691bb76ff1Sjsg 		if (ret < 0)
10701bb76ff1Sjsg 			return ret;
10711bb76ff1Sjsg 	}
10721bb76ff1Sjsg 
10731bb76ff1Sjsg 	if (!obj)
10741bb76ff1Sjsg 		return 0;
10751bb76ff1Sjsg 
10761bb76ff1Sjsg 
10771bb76ff1Sjsg 	ret = intel_plane_pin_fb(new_plane_state);
10781bb76ff1Sjsg 	if (ret)
10791bb76ff1Sjsg 		return ret;
10801bb76ff1Sjsg 
10811bb76ff1Sjsg 	i915_gem_object_wait_priority(obj, 0, &attr);
10821bb76ff1Sjsg 
10831bb76ff1Sjsg 	if (!new_plane_state->uapi.fence) { /* implicit fencing */
10841bb76ff1Sjsg 		struct dma_resv_iter cursor;
10851bb76ff1Sjsg 		struct dma_fence *fence;
10861bb76ff1Sjsg 
10871bb76ff1Sjsg 		ret = i915_sw_fence_await_reservation(&state->commit_ready,
1088*f005ef32Sjsg 						      obj->base.resv, false,
10891bb76ff1Sjsg 						      i915_fence_timeout(dev_priv),
10901bb76ff1Sjsg 						      GFP_KERNEL);
10911bb76ff1Sjsg 		if (ret < 0)
10921bb76ff1Sjsg 			goto unpin_fb;
10931bb76ff1Sjsg 
10941bb76ff1Sjsg 		dma_resv_iter_begin(&cursor, obj->base.resv,
10951bb76ff1Sjsg 				    DMA_RESV_USAGE_WRITE);
10961bb76ff1Sjsg 		dma_resv_for_each_fence_unlocked(&cursor, fence) {
1097*f005ef32Sjsg 			intel_display_rps_boost_after_vblank(new_plane_state->hw.crtc,
10981bb76ff1Sjsg 							     fence);
10991bb76ff1Sjsg 		}
11001bb76ff1Sjsg 		dma_resv_iter_end(&cursor);
11011bb76ff1Sjsg 	} else {
1102*f005ef32Sjsg 		intel_display_rps_boost_after_vblank(new_plane_state->hw.crtc,
11031bb76ff1Sjsg 						     new_plane_state->uapi.fence);
11041bb76ff1Sjsg 	}
11051bb76ff1Sjsg 
11061bb76ff1Sjsg 	/*
11071bb76ff1Sjsg 	 * We declare pageflips to be interactive and so merit a small bias
11081bb76ff1Sjsg 	 * towards upclocking to deliver the frame on time. By only changing
11091bb76ff1Sjsg 	 * the RPS thresholds to sample more regularly and aim for higher
11101bb76ff1Sjsg 	 * clocks we can hopefully deliver low power workloads (like kodi)
11111bb76ff1Sjsg 	 * that are not quite steady state without resorting to forcing
11121bb76ff1Sjsg 	 * maximum clocks following a vblank miss (see do_rps_boost()).
11131bb76ff1Sjsg 	 */
1114*f005ef32Sjsg 	intel_display_rps_mark_interactive(dev_priv, state, true);
11151bb76ff1Sjsg 
11161bb76ff1Sjsg 	return 0;
11171bb76ff1Sjsg 
11181bb76ff1Sjsg unpin_fb:
11191bb76ff1Sjsg 	intel_plane_unpin_fb(new_plane_state);
11201bb76ff1Sjsg 
11211bb76ff1Sjsg 	return ret;
11221bb76ff1Sjsg }
11231bb76ff1Sjsg 
11241bb76ff1Sjsg /**
11251bb76ff1Sjsg  * intel_cleanup_plane_fb - Cleans up an fb after plane use
11261bb76ff1Sjsg  * @plane: drm plane to clean up for
11271bb76ff1Sjsg  * @_old_plane_state: the state from the previous modeset
11281bb76ff1Sjsg  *
11291bb76ff1Sjsg  * Cleans up a framebuffer that has just been removed from a plane.
11301bb76ff1Sjsg  */
11311bb76ff1Sjsg static void
intel_cleanup_plane_fb(struct drm_plane * plane,struct drm_plane_state * _old_plane_state)11321bb76ff1Sjsg intel_cleanup_plane_fb(struct drm_plane *plane,
11331bb76ff1Sjsg 		       struct drm_plane_state *_old_plane_state)
11341bb76ff1Sjsg {
11351bb76ff1Sjsg 	struct intel_plane_state *old_plane_state =
11361bb76ff1Sjsg 		to_intel_plane_state(_old_plane_state);
11371bb76ff1Sjsg 	struct intel_atomic_state *state =
11381bb76ff1Sjsg 		to_intel_atomic_state(old_plane_state->uapi.state);
11391bb76ff1Sjsg 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
11401bb76ff1Sjsg 	struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
11411bb76ff1Sjsg 
11421bb76ff1Sjsg 	if (!obj)
11431bb76ff1Sjsg 		return;
11441bb76ff1Sjsg 
1145*f005ef32Sjsg 	intel_display_rps_mark_interactive(dev_priv, state, false);
11461bb76ff1Sjsg 
11471bb76ff1Sjsg 	/* Should only be called after a successful intel_prepare_plane_fb()! */
11481bb76ff1Sjsg 	intel_plane_unpin_fb(old_plane_state);
11491bb76ff1Sjsg }
11501bb76ff1Sjsg 
11515ca02815Sjsg static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1152c349dbc7Sjsg 	.prepare_fb = intel_prepare_plane_fb,
1153c349dbc7Sjsg 	.cleanup_fb = intel_cleanup_plane_fb,
1154c349dbc7Sjsg };
11555ca02815Sjsg 
intel_plane_helper_add(struct intel_plane * plane)11565ca02815Sjsg void intel_plane_helper_add(struct intel_plane *plane)
11575ca02815Sjsg {
11585ca02815Sjsg 	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
11595ca02815Sjsg }
1160