xref: /linux/drivers/gpu/drm/i915/display/intel_plane.c (revision 98910fa0a487622d767d0674de3ce8fe02bde0b0)
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * DOC: atomic plane helpers
26  *
27  * The functions here are used by the atomic plane helper functions to
28  * implement legacy plane updates (i.e., drm_plane->update_plane() and
29  * drm_plane->disable_plane()).  This allows plane updates to use the
30  * atomic state infrastructure and perform plane updates as separate
31  * prepare/check/commit/cleanup steps.
32  */
33 
34 #include <linux/dma-fence-chain.h>
35 #include <linux/dma-resv.h>
36 #include <linux/iosys-map.h>
37 
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_blend.h>
40 #include <drm/drm_cache.h>
41 #include <drm/drm_damage_helper.h>
42 #include <drm/drm_fourcc.h>
43 #include <drm/drm_gem.h>
44 #include <drm/drm_gem_atomic_helper.h>
45 #include <drm/drm_panic.h>
46 
47 #include "gem/i915_gem_object.h"
48 #include "i915_scheduler_types.h"
49 #include "i915_vma.h"
50 #include "i9xx_plane_regs.h"
51 #include "intel_bo.h"
52 #include "intel_cdclk.h"
53 #include "intel_cursor.h"
54 #include "intel_display_rps.h"
55 #include "intel_display_trace.h"
56 #include "intel_display_types.h"
57 #include "intel_fb.h"
58 #include "intel_fb_pin.h"
59 #include "intel_fbdev.h"
60 #include "intel_plane.h"
61 #include "intel_psr.h"
62 #include "skl_scaler.h"
63 #include "skl_universal_plane.h"
64 #include "skl_watermark.h"
65 
intel_plane_state_reset(struct intel_plane_state * plane_state,struct intel_plane * plane)66 static void intel_plane_state_reset(struct intel_plane_state *plane_state,
67 				    struct intel_plane *plane)
68 {
69 	memset(plane_state, 0, sizeof(*plane_state));
70 
71 	__drm_atomic_helper_plane_state_reset(&plane_state->uapi, &plane->base);
72 
73 	plane_state->scaler_id = -1;
74 }
75 
intel_plane_alloc(void)76 struct intel_plane *intel_plane_alloc(void)
77 {
78 	struct intel_plane_state *plane_state;
79 	struct intel_plane *plane;
80 
81 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
82 	if (!plane)
83 		return ERR_PTR(-ENOMEM);
84 
85 	plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
86 	if (!plane_state) {
87 		kfree(plane);
88 		return ERR_PTR(-ENOMEM);
89 	}
90 
91 	intel_plane_state_reset(plane_state, plane);
92 
93 	plane->base.state = &plane_state->uapi;
94 
95 	return plane;
96 }
97 
intel_plane_free(struct intel_plane * plane)98 void intel_plane_free(struct intel_plane *plane)
99 {
100 	intel_plane_destroy_state(&plane->base, plane->base.state);
101 	kfree(plane);
102 }
103 
104 /**
105  * intel_plane_destroy - destroy a plane
106  * @plane: plane to destroy
107  *
108  * Common destruction function for all types of planes (primary, cursor,
109  * sprite).
110  */
intel_plane_destroy(struct drm_plane * plane)111 void intel_plane_destroy(struct drm_plane *plane)
112 {
113 	drm_plane_cleanup(plane);
114 	kfree(to_intel_plane(plane));
115 }
116 
117 /**
118  * intel_plane_duplicate_state - duplicate plane state
119  * @plane: drm plane
120  *
121  * Allocates and returns a copy of the plane state (both common and
122  * Intel-specific) for the specified plane.
123  *
124  * Returns: The newly allocated plane state, or NULL on failure.
125  */
126 struct drm_plane_state *
intel_plane_duplicate_state(struct drm_plane * plane)127 intel_plane_duplicate_state(struct drm_plane *plane)
128 {
129 	struct intel_plane_state *intel_state;
130 
131 	intel_state = to_intel_plane_state(plane->state);
132 	intel_state = kmemdup(intel_state, sizeof(*intel_state), GFP_KERNEL);
133 
134 	if (!intel_state)
135 		return NULL;
136 
137 	__drm_atomic_helper_plane_duplicate_state(plane, &intel_state->uapi);
138 
139 	intel_state->ggtt_vma = NULL;
140 	intel_state->dpt_vma = NULL;
141 	intel_state->flags = 0;
142 	intel_state->damage = DRM_RECT_INIT(0, 0, 0, 0);
143 
144 	/* add reference to fb */
145 	if (intel_state->hw.fb)
146 		drm_framebuffer_get(intel_state->hw.fb);
147 
148 	return &intel_state->uapi;
149 }
150 
151 /**
152  * intel_plane_destroy_state - destroy plane state
153  * @plane: drm plane
154  * @state: state object to destroy
155  *
156  * Destroys the plane state (both common and Intel-specific) for the
157  * specified plane.
158  */
159 void
intel_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)160 intel_plane_destroy_state(struct drm_plane *plane,
161 			  struct drm_plane_state *state)
162 {
163 	struct intel_plane_state *plane_state = to_intel_plane_state(state);
164 
165 	drm_WARN_ON(plane->dev, plane_state->ggtt_vma);
166 	drm_WARN_ON(plane->dev, plane_state->dpt_vma);
167 
168 	__drm_atomic_helper_plane_destroy_state(&plane_state->uapi);
169 	if (plane_state->hw.fb)
170 		drm_framebuffer_put(plane_state->hw.fb);
171 	kfree(plane_state);
172 }
173 
intel_plane_needs_physical(struct intel_plane * plane)174 bool intel_plane_needs_physical(struct intel_plane *plane)
175 {
176 	struct intel_display *display = to_intel_display(plane);
177 
178 	return plane->id == PLANE_CURSOR &&
179 		DISPLAY_INFO(display)->cursor_needs_physical;
180 }
181 
intel_plane_can_async_flip(struct intel_plane * plane,u32 format,u64 modifier)182 bool intel_plane_can_async_flip(struct intel_plane *plane, u32 format,
183 				u64 modifier)
184 {
185 	if (intel_format_info_is_yuv_semiplanar(drm_format_info(format), modifier) ||
186 	    format == DRM_FORMAT_C8)
187 		return false;
188 
189 	return plane->can_async_flip && plane->can_async_flip(modifier);
190 }
191 
intel_plane_format_mod_supported_async(struct drm_plane * plane,u32 format,u64 modifier)192 bool intel_plane_format_mod_supported_async(struct drm_plane *plane,
193 					    u32 format,
194 					    u64 modifier)
195 {
196 	if (!plane->funcs->format_mod_supported(plane, format, modifier))
197 		return false;
198 
199 	return intel_plane_can_async_flip(to_intel_plane(plane),
200 					format, modifier);
201 }
202 
intel_adjusted_rate(const struct drm_rect * src,const struct drm_rect * dst,unsigned int rate)203 unsigned int intel_adjusted_rate(const struct drm_rect *src,
204 				 const struct drm_rect *dst,
205 				 unsigned int rate)
206 {
207 	unsigned int src_w, src_h, dst_w, dst_h;
208 
209 	src_w = drm_rect_width(src) >> 16;
210 	src_h = drm_rect_height(src) >> 16;
211 	dst_w = drm_rect_width(dst);
212 	dst_h = drm_rect_height(dst);
213 
214 	/* Downscaling limits the maximum pixel rate */
215 	dst_w = min(src_w, dst_w);
216 	dst_h = min(src_h, dst_h);
217 
218 	return DIV_ROUND_UP_ULL(mul_u32_u32(rate, src_w * src_h),
219 				dst_w * dst_h);
220 }
221 
intel_plane_pixel_rate(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)222 unsigned int intel_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
223 				    const struct intel_plane_state *plane_state)
224 {
225 	/*
226 	 * Note we don't check for plane visibility here as
227 	 * we want to use this when calculating the cursor
228 	 * watermarks even if the cursor is fully offscreen.
229 	 * That depends on the src/dst rectangles being
230 	 * correctly populated whenever the watermark code
231 	 * considers the cursor to be visible, whether or not
232 	 * it is actually visible.
233 	 *
234 	 * See: intel_wm_plane_visible() and intel_check_cursor()
235 	 */
236 
237 	return intel_adjusted_rate(&plane_state->uapi.src,
238 				   &plane_state->uapi.dst,
239 				   crtc_state->pixel_rate);
240 }
241 
intel_plane_data_rate(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,int color_plane)242 unsigned int intel_plane_data_rate(const struct intel_crtc_state *crtc_state,
243 				   const struct intel_plane_state *plane_state,
244 				   int color_plane)
245 {
246 	const struct drm_framebuffer *fb = plane_state->hw.fb;
247 
248 	if (!plane_state->uapi.visible)
249 		return 0;
250 
251 	return intel_plane_pixel_rate(crtc_state, plane_state) *
252 		fb->format->cpp[color_plane];
253 }
254 
255 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)256 intel_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
257 			       const struct intel_plane_state *plane_state,
258 			       int color_plane)
259 {
260 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
261 	const struct drm_framebuffer *fb = plane_state->hw.fb;
262 	unsigned int rel_data_rate;
263 	int width, height;
264 
265 	if (plane->id == PLANE_CURSOR)
266 		return 0;
267 
268 	if (!plane_state->uapi.visible)
269 		return 0;
270 
271 	/*
272 	 * Src coordinates are already rotated by 270 degrees for
273 	 * the 90/270 degree plane rotation cases (to match the
274 	 * GTT mapping), hence no need to account for rotation here.
275 	 */
276 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
277 	height = drm_rect_height(&plane_state->uapi.src) >> 16;
278 
279 	/* UV plane does 1/2 pixel sub-sampling */
280 	if (color_plane == 1) {
281 		width /= 2;
282 		height /= 2;
283 	}
284 
285 	rel_data_rate =
286 		skl_plane_relative_data_rate(crtc_state, plane, width, height,
287 					     fb->format->cpp[color_plane]);
288 	if (!rel_data_rate)
289 		return 0;
290 
291 	return intel_adjusted_rate(&plane_state->uapi.src,
292 				   &plane_state->uapi.dst,
293 				   rel_data_rate);
294 }
295 
intel_plane_calc_min_cdclk(struct intel_atomic_state * state,struct intel_plane * plane,bool * need_cdclk_calc)296 int intel_plane_calc_min_cdclk(struct intel_atomic_state *state,
297 			       struct intel_plane *plane,
298 			       bool *need_cdclk_calc)
299 {
300 	struct intel_display *display = to_intel_display(plane);
301 	const struct intel_plane_state *plane_state =
302 		intel_atomic_get_new_plane_state(state, plane);
303 	struct intel_crtc *crtc = to_intel_crtc(plane_state->hw.crtc);
304 	const struct intel_cdclk_state *cdclk_state;
305 	const struct intel_crtc_state *old_crtc_state;
306 	struct intel_crtc_state *new_crtc_state;
307 
308 	if (!plane_state->uapi.visible || !plane->min_cdclk)
309 		return 0;
310 
311 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
312 	new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
313 
314 	new_crtc_state->min_cdclk[plane->id] =
315 		plane->min_cdclk(new_crtc_state, plane_state);
316 
317 	/*
318 	 * No need to check against the cdclk state if
319 	 * the min cdclk for the plane doesn't increase.
320 	 *
321 	 * Ie. we only ever increase the cdclk due to plane
322 	 * requirements. This can reduce back and forth
323 	 * display blinking due to constant cdclk changes.
324 	 */
325 	if (new_crtc_state->min_cdclk[plane->id] <=
326 	    old_crtc_state->min_cdclk[plane->id])
327 		return 0;
328 
329 	cdclk_state = intel_atomic_get_cdclk_state(state);
330 	if (IS_ERR(cdclk_state))
331 		return PTR_ERR(cdclk_state);
332 
333 	/*
334 	 * No need to recalculate the cdclk state if
335 	 * the min cdclk for the pipe doesn't increase.
336 	 *
337 	 * Ie. we only ever increase the cdclk due to plane
338 	 * requirements. This can reduce back and forth
339 	 * display blinking due to constant cdclk changes.
340 	 */
341 	if (new_crtc_state->min_cdclk[plane->id] <=
342 	    intel_cdclk_min_cdclk(cdclk_state, crtc->pipe))
343 		return 0;
344 
345 	drm_dbg_kms(display->drm,
346 		    "[PLANE:%d:%s] min cdclk (%d kHz) > [CRTC:%d:%s] min cdclk (%d kHz)\n",
347 		    plane->base.base.id, plane->base.name,
348 		    new_crtc_state->min_cdclk[plane->id],
349 		    crtc->base.base.id, crtc->base.name,
350 		    intel_cdclk_min_cdclk(cdclk_state, crtc->pipe));
351 	*need_cdclk_calc = true;
352 
353 	return 0;
354 }
355 
intel_plane_clear_hw_state(struct intel_plane_state * plane_state)356 static void intel_plane_clear_hw_state(struct intel_plane_state *plane_state)
357 {
358 	if (plane_state->hw.fb)
359 		drm_framebuffer_put(plane_state->hw.fb);
360 
361 	memset(&plane_state->hw, 0, sizeof(plane_state->hw));
362 }
363 
364 static void
intel_plane_copy_uapi_plane_damage(struct intel_plane_state * new_plane_state,const struct intel_plane_state * old_uapi_plane_state,const struct intel_plane_state * new_uapi_plane_state)365 intel_plane_copy_uapi_plane_damage(struct intel_plane_state *new_plane_state,
366 				   const struct intel_plane_state *old_uapi_plane_state,
367 				   const struct intel_plane_state *new_uapi_plane_state)
368 {
369 	struct intel_display *display = to_intel_display(new_plane_state);
370 	struct drm_rect *damage = &new_plane_state->damage;
371 
372 	/* damage property tracking enabled from display version 12 onwards */
373 	if (DISPLAY_VER(display) < 12)
374 		return;
375 
376 	if (!drm_atomic_helper_damage_merged(&old_uapi_plane_state->uapi,
377 					     &new_uapi_plane_state->uapi,
378 					     damage))
379 		/* Incase helper fails, mark whole plane region as damage */
380 		*damage = drm_plane_state_src(&new_uapi_plane_state->uapi);
381 }
382 
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)383 void intel_plane_copy_uapi_to_hw_state(struct intel_plane_state *plane_state,
384 				       const struct intel_plane_state *from_plane_state,
385 				       struct intel_crtc *crtc)
386 {
387 	intel_plane_clear_hw_state(plane_state);
388 
389 	/*
390 	 * For the joiner secondary uapi.crtc will point at
391 	 * the primary crtc. So we explicitly assign the right
392 	 * secondary crtc to hw.crtc. uapi.crtc!=NULL simply
393 	 * indicates the plane is logically enabled on the uapi level.
394 	 */
395 	plane_state->hw.crtc = from_plane_state->uapi.crtc ? &crtc->base : NULL;
396 
397 	plane_state->hw.fb = from_plane_state->uapi.fb;
398 	if (plane_state->hw.fb)
399 		drm_framebuffer_get(plane_state->hw.fb);
400 
401 	plane_state->hw.alpha = from_plane_state->uapi.alpha;
402 	plane_state->hw.pixel_blend_mode =
403 		from_plane_state->uapi.pixel_blend_mode;
404 	plane_state->hw.rotation = from_plane_state->uapi.rotation;
405 	plane_state->hw.color_encoding = from_plane_state->uapi.color_encoding;
406 	plane_state->hw.color_range = from_plane_state->uapi.color_range;
407 	plane_state->hw.scaling_filter = from_plane_state->uapi.scaling_filter;
408 
409 	plane_state->uapi.src = drm_plane_state_src(&from_plane_state->uapi);
410 	plane_state->uapi.dst = drm_plane_state_dest(&from_plane_state->uapi);
411 }
412 
intel_plane_copy_hw_state(struct intel_plane_state * plane_state,const struct intel_plane_state * from_plane_state)413 void intel_plane_copy_hw_state(struct intel_plane_state *plane_state,
414 			       const struct intel_plane_state *from_plane_state)
415 {
416 	intel_plane_clear_hw_state(plane_state);
417 
418 	memcpy(&plane_state->hw, &from_plane_state->hw,
419 	       sizeof(plane_state->hw));
420 
421 	if (plane_state->hw.fb)
422 		drm_framebuffer_get(plane_state->hw.fb);
423 }
424 
intel_plane_set_invisible(struct intel_crtc_state * crtc_state,struct intel_plane_state * plane_state)425 void intel_plane_set_invisible(struct intel_crtc_state *crtc_state,
426 			       struct intel_plane_state *plane_state)
427 {
428 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
429 
430 	crtc_state->active_planes &= ~BIT(plane->id);
431 	crtc_state->scaled_planes &= ~BIT(plane->id);
432 	crtc_state->nv12_planes &= ~BIT(plane->id);
433 	crtc_state->c8_planes &= ~BIT(plane->id);
434 	crtc_state->async_flip_planes &= ~BIT(plane->id);
435 	crtc_state->data_rate[plane->id] = 0;
436 	crtc_state->data_rate_y[plane->id] = 0;
437 	crtc_state->rel_data_rate[plane->id] = 0;
438 	crtc_state->rel_data_rate_y[plane->id] = 0;
439 	crtc_state->min_cdclk[plane->id] = 0;
440 
441 	plane_state->uapi.visible = false;
442 }
443 
intel_plane_is_scaled(const struct intel_plane_state * plane_state)444 static bool intel_plane_is_scaled(const struct intel_plane_state *plane_state)
445 {
446 	int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
447 	int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
448 	int dst_w = drm_rect_width(&plane_state->uapi.dst);
449 	int dst_h = drm_rect_height(&plane_state->uapi.dst);
450 
451 	return src_w != dst_w || src_h != dst_h;
452 }
453 
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)454 static bool intel_plane_do_async_flip(struct intel_plane *plane,
455 				      const struct intel_crtc_state *old_crtc_state,
456 				      const struct intel_crtc_state *new_crtc_state)
457 {
458 	struct intel_display *display = to_intel_display(plane);
459 
460 	if (!plane->async_flip)
461 		return false;
462 
463 	if (!new_crtc_state->uapi.async_flip)
464 		return false;
465 
466 	/*
467 	 * In platforms after DISPLAY13, we might need to override
468 	 * first async flip in order to change watermark levels
469 	 * as part of optimization.
470 	 *
471 	 * And let's do this for all skl+ so that we can eg. change the
472 	 * modifier as well.
473 	 *
474 	 * TODO: For older platforms there is less reason to do this as
475 	 * only X-tile is supported with async flips, though we could
476 	 * extend this so other scanout parameters (stride/etc) could
477 	 * be changed as well...
478 	 */
479 	return DISPLAY_VER(display) < 9 || old_crtc_state->uapi.async_flip;
480 }
481 
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)482 static bool i9xx_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state,
483 				   const struct intel_plane_state *old_plane_state,
484 				   const struct intel_plane_state *new_plane_state)
485 {
486 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
487 	bool old_visible = old_plane_state->uapi.visible;
488 	bool new_visible = new_plane_state->uapi.visible;
489 	u32 old_ctl = old_plane_state->ctl;
490 	u32 new_ctl = new_plane_state->ctl;
491 	bool modeset, turn_on, turn_off;
492 
493 	if (plane->id == PLANE_CURSOR)
494 		return false;
495 
496 	modeset = intel_crtc_needs_modeset(new_crtc_state);
497 	turn_off = old_visible && (!new_visible || modeset);
498 	turn_on = new_visible && (!old_visible || modeset);
499 
500 	/* Must disable CxSR around plane enable/disable */
501 	if (turn_on || turn_off)
502 		return true;
503 
504 	if (!old_visible || !new_visible)
505 		return false;
506 
507 	/*
508 	 * Most plane control register updates are blocked while in CxSR.
509 	 *
510 	 * Tiling mode is one exception where the primary plane can
511 	 * apparently handle it, whereas the sprites can not (the
512 	 * sprite issue being only relevant on VLV/CHV where CxSR
513 	 * is actually possible with a sprite enabled).
514 	 */
515 	if (plane->id == PLANE_PRIMARY) {
516 		old_ctl &= ~DISP_TILED;
517 		new_ctl &= ~DISP_TILED;
518 	}
519 
520 	return old_ctl != new_ctl;
521 }
522 
ilk_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)523 static bool ilk_must_disable_cxsr(const struct intel_crtc_state *new_crtc_state,
524 				  const struct intel_plane_state *old_plane_state,
525 				  const struct intel_plane_state *new_plane_state)
526 {
527 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
528 	bool old_visible = old_plane_state->uapi.visible;
529 	bool new_visible = new_plane_state->uapi.visible;
530 	bool modeset, turn_on;
531 
532 	if (plane->id == PLANE_CURSOR)
533 		return false;
534 
535 	modeset = intel_crtc_needs_modeset(new_crtc_state);
536 	turn_on = new_visible && (!old_visible || modeset);
537 
538 	/*
539 	 * ILK/SNB DVSACNTR/Sprite Enable
540 	 * IVB SPR_CTL/Sprite Enable
541 	 * "When in Self Refresh Big FIFO mode, a write to enable the
542 	 *  plane will be internally buffered and delayed while Big FIFO
543 	 *  mode is exiting."
544 	 *
545 	 * Which means that enabling the sprite can take an extra frame
546 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
547 	 * down to LP0 and wait for vblank in order to make sure the
548 	 * sprite gets enabled on the next vblank after the register write.
549 	 * Doing otherwise would risk enabling the sprite one frame after
550 	 * we've already signalled flip completion. We can resume LP1+
551 	 * once the sprite has been enabled.
552 	 *
553 	 * With experimental results seems this is needed also for primary
554 	 * plane, not only sprite plane.
555 	 */
556 	if (turn_on)
557 		return true;
558 
559 	/*
560 	 * WaCxSRDisabledForSpriteScaling:ivb
561 	 * IVB SPR_SCALE/Scaling Enable
562 	 * "Low Power watermarks must be disabled for at least one
563 	 *  frame before enabling sprite scaling, and kept disabled
564 	 *  until sprite scaling is disabled."
565 	 *
566 	 * ILK/SNB DVSASCALE/Scaling Enable
567 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
568 	 *  masked off while Big FIFO mode is exiting."
569 	 *
570 	 * Despite the w/a only being listed for IVB we assume that
571 	 * the ILK/SNB note has similar ramifications, hence we apply
572 	 * the w/a on all three platforms.
573 	 */
574 	return !intel_plane_is_scaled(old_plane_state) &&
575 		intel_plane_is_scaled(new_plane_state);
576 }
577 
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)578 static int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
579 					   struct intel_crtc_state *new_crtc_state,
580 					   const struct intel_plane_state *old_plane_state,
581 					   struct intel_plane_state *new_plane_state)
582 {
583 	struct intel_display *display = to_intel_display(new_crtc_state);
584 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
585 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
586 	bool mode_changed = intel_crtc_needs_modeset(new_crtc_state);
587 	bool was_crtc_enabled = old_crtc_state->hw.active;
588 	bool is_crtc_enabled = new_crtc_state->hw.active;
589 	bool turn_off, turn_on, visible, was_visible;
590 	int ret;
591 
592 	if (DISPLAY_VER(display) >= 9 && plane->id != PLANE_CURSOR) {
593 		ret = skl_update_scaler_plane(new_crtc_state, new_plane_state);
594 		if (ret)
595 			return ret;
596 	}
597 
598 	was_visible = old_plane_state->uapi.visible;
599 	visible = new_plane_state->uapi.visible;
600 
601 	if (!was_crtc_enabled && drm_WARN_ON(display->drm, was_visible))
602 		was_visible = false;
603 
604 	/*
605 	 * Visibility is calculated as if the crtc was on, but
606 	 * after scaler setup everything depends on it being off
607 	 * when the crtc isn't active.
608 	 *
609 	 * FIXME this is wrong for watermarks. Watermarks should also
610 	 * be computed as if the pipe would be active. Perhaps move
611 	 * per-plane wm computation to the .check_plane() hook, and
612 	 * only combine the results from all planes in the current place?
613 	 */
614 	if (!is_crtc_enabled) {
615 		intel_plane_set_invisible(new_crtc_state, new_plane_state);
616 		visible = false;
617 	}
618 
619 	if (!was_visible && !visible)
620 		return 0;
621 
622 	turn_off = was_visible && (!visible || mode_changed);
623 	turn_on = visible && (!was_visible || mode_changed);
624 
625 	drm_dbg_atomic(display->drm,
626 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
627 		       crtc->base.base.id, crtc->base.name,
628 		       plane->base.base.id, plane->base.name,
629 		       was_visible, visible,
630 		       turn_off, turn_on, mode_changed);
631 
632 	if (visible || was_visible)
633 		new_crtc_state->fb_bits |= plane->frontbuffer_bit;
634 
635 	if (HAS_GMCH(display) &&
636 	    i9xx_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
637 		new_crtc_state->disable_cxsr = true;
638 
639 	if ((display->platform.ironlake || display->platform.sandybridge || display->platform.ivybridge) &&
640 	    ilk_must_disable_cxsr(new_crtc_state, old_plane_state, new_plane_state))
641 		new_crtc_state->disable_cxsr = true;
642 
643 	if (intel_plane_do_async_flip(plane, old_crtc_state, new_crtc_state)) {
644 		new_crtc_state->do_async_flip = true;
645 		new_crtc_state->async_flip_planes |= BIT(plane->id);
646 	} else if (plane->need_async_flip_toggle_wa &&
647 		   new_crtc_state->uapi.async_flip) {
648 		/*
649 		 * On platforms with double buffered async flip bit we
650 		 * set the bit already one frame early during the sync
651 		 * flip (see {i9xx,skl}_plane_update_arm()). The
652 		 * hardware will therefore be ready to perform a real
653 		 * async flip during the next commit, without having
654 		 * to wait yet another frame for the bit to latch.
655 		 */
656 		new_crtc_state->async_flip_planes |= BIT(plane->id);
657 	}
658 
659 	return 0;
660 }
661 
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)662 int intel_plane_atomic_check_with_state(const struct intel_crtc_state *old_crtc_state,
663 					struct intel_crtc_state *new_crtc_state,
664 					const struct intel_plane_state *old_plane_state,
665 					struct intel_plane_state *new_plane_state)
666 {
667 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
668 	const struct drm_framebuffer *fb = new_plane_state->hw.fb;
669 	int ret;
670 
671 	intel_plane_set_invisible(new_crtc_state, new_plane_state);
672 	new_crtc_state->enabled_planes &= ~BIT(plane->id);
673 
674 	if (!new_plane_state->hw.crtc && !old_plane_state->hw.crtc)
675 		return 0;
676 
677 	ret = plane->check_plane(new_crtc_state, new_plane_state);
678 	if (ret)
679 		return ret;
680 
681 	if (fb)
682 		new_crtc_state->enabled_planes |= BIT(plane->id);
683 
684 	/* FIXME pre-g4x don't work like this */
685 	if (new_plane_state->uapi.visible)
686 		new_crtc_state->active_planes |= BIT(plane->id);
687 
688 	if (new_plane_state->uapi.visible &&
689 	    intel_plane_is_scaled(new_plane_state))
690 		new_crtc_state->scaled_planes |= BIT(plane->id);
691 
692 	if (new_plane_state->uapi.visible &&
693 	    intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
694 		new_crtc_state->nv12_planes |= BIT(plane->id);
695 
696 	if (new_plane_state->uapi.visible &&
697 	    fb->format->format == DRM_FORMAT_C8)
698 		new_crtc_state->c8_planes |= BIT(plane->id);
699 
700 	if (new_plane_state->uapi.visible || old_plane_state->uapi.visible)
701 		new_crtc_state->update_planes |= BIT(plane->id);
702 
703 	if (new_plane_state->uapi.visible &&
704 	    intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
705 		new_crtc_state->data_rate_y[plane->id] =
706 			intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
707 		new_crtc_state->data_rate[plane->id] =
708 			intel_plane_data_rate(new_crtc_state, new_plane_state, 1);
709 
710 		new_crtc_state->rel_data_rate_y[plane->id] =
711 			intel_plane_relative_data_rate(new_crtc_state,
712 						       new_plane_state, 0);
713 		new_crtc_state->rel_data_rate[plane->id] =
714 			intel_plane_relative_data_rate(new_crtc_state,
715 						       new_plane_state, 1);
716 	} else if (new_plane_state->uapi.visible) {
717 		new_crtc_state->data_rate[plane->id] =
718 			intel_plane_data_rate(new_crtc_state, new_plane_state, 0);
719 
720 		new_crtc_state->rel_data_rate[plane->id] =
721 			intel_plane_relative_data_rate(new_crtc_state,
722 						       new_plane_state, 0);
723 	}
724 
725 	return intel_plane_atomic_calc_changes(old_crtc_state, new_crtc_state,
726 					       old_plane_state, new_plane_state);
727 }
728 
729 struct intel_plane *
intel_crtc_get_plane(struct intel_crtc * crtc,enum plane_id plane_id)730 intel_crtc_get_plane(struct intel_crtc *crtc, enum plane_id plane_id)
731 {
732 	struct intel_display *display = to_intel_display(crtc);
733 	struct intel_plane *plane;
734 
735 	for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
736 		if (plane->id == plane_id)
737 			return plane;
738 	}
739 
740 	return NULL;
741 }
742 
plane_atomic_check(struct intel_atomic_state * state,struct intel_plane * plane)743 static int plane_atomic_check(struct intel_atomic_state *state,
744 			      struct intel_plane *plane)
745 {
746 	struct intel_display *display = to_intel_display(state);
747 	struct intel_plane_state *new_plane_state =
748 		intel_atomic_get_new_plane_state(state, plane);
749 	const struct intel_plane_state *old_plane_state =
750 		intel_atomic_get_old_plane_state(state, plane);
751 	const struct intel_plane_state *new_primary_crtc_plane_state;
752 	const struct intel_plane_state *old_primary_crtc_plane_state;
753 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, plane->pipe);
754 	const struct intel_crtc_state *old_crtc_state =
755 		intel_atomic_get_old_crtc_state(state, crtc);
756 	struct intel_crtc_state *new_crtc_state =
757 		intel_atomic_get_new_crtc_state(state, crtc);
758 
759 	if (new_crtc_state && intel_crtc_is_joiner_secondary(new_crtc_state)) {
760 		struct intel_crtc *primary_crtc =
761 			intel_primary_crtc(new_crtc_state);
762 		struct intel_plane *primary_crtc_plane =
763 			intel_crtc_get_plane(primary_crtc, plane->id);
764 
765 		new_primary_crtc_plane_state =
766 			intel_atomic_get_new_plane_state(state, primary_crtc_plane);
767 		old_primary_crtc_plane_state =
768 			intel_atomic_get_old_plane_state(state, primary_crtc_plane);
769 	} else {
770 		new_primary_crtc_plane_state = new_plane_state;
771 		old_primary_crtc_plane_state = old_plane_state;
772 	}
773 
774 	intel_plane_copy_uapi_plane_damage(new_plane_state,
775 					   old_primary_crtc_plane_state,
776 					   new_primary_crtc_plane_state);
777 
778 	intel_plane_copy_uapi_to_hw_state(new_plane_state,
779 					  new_primary_crtc_plane_state,
780 					  crtc);
781 
782 	new_plane_state->uapi.visible = false;
783 	if (!new_crtc_state)
784 		return 0;
785 
786 	return intel_plane_atomic_check_with_state(old_crtc_state,
787 						   new_crtc_state,
788 						   old_plane_state,
789 						   new_plane_state);
790 }
791 
792 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)793 skl_next_plane_to_commit(struct intel_atomic_state *state,
794 			 struct intel_crtc *crtc,
795 			 struct skl_ddb_entry ddb[I915_MAX_PLANES],
796 			 struct skl_ddb_entry ddb_y[I915_MAX_PLANES],
797 			 unsigned int *update_mask)
798 {
799 	struct intel_crtc_state *crtc_state =
800 		intel_atomic_get_new_crtc_state(state, crtc);
801 	struct intel_plane_state __maybe_unused *plane_state;
802 	struct intel_plane *plane;
803 	int i;
804 
805 	if (*update_mask == 0)
806 		return NULL;
807 
808 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
809 		enum plane_id plane_id = plane->id;
810 
811 		if (crtc->pipe != plane->pipe ||
812 		    !(*update_mask & BIT(plane_id)))
813 			continue;
814 
815 		if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb[plane_id],
816 						ddb, I915_MAX_PLANES, plane_id) ||
817 		    skl_ddb_allocation_overlaps(&crtc_state->wm.skl.plane_ddb_y[plane_id],
818 						ddb_y, I915_MAX_PLANES, plane_id))
819 			continue;
820 
821 		*update_mask &= ~BIT(plane_id);
822 		ddb[plane_id] = crtc_state->wm.skl.plane_ddb[plane_id];
823 		ddb_y[plane_id] = crtc_state->wm.skl.plane_ddb_y[plane_id];
824 
825 		return plane;
826 	}
827 
828 	/* should never happen */
829 	drm_WARN_ON(state->base.dev, 1);
830 
831 	return NULL;
832 }
833 
intel_plane_update_noarm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)834 void intel_plane_update_noarm(struct intel_dsb *dsb,
835 			      struct intel_plane *plane,
836 			      const struct intel_crtc_state *crtc_state,
837 			      const struct intel_plane_state *plane_state)
838 {
839 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
840 
841 	trace_intel_plane_update_noarm(plane_state, crtc);
842 
843 	if (plane->fbc)
844 		intel_fbc_dirty_rect_update_noarm(dsb, plane);
845 
846 	if (plane->update_noarm)
847 		plane->update_noarm(dsb, plane, crtc_state, plane_state);
848 }
849 
intel_plane_async_flip(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,bool async_flip)850 void intel_plane_async_flip(struct intel_dsb *dsb,
851 			    struct intel_plane *plane,
852 			    const struct intel_crtc_state *crtc_state,
853 			    const struct intel_plane_state *plane_state,
854 			    bool async_flip)
855 {
856 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
857 
858 	trace_intel_plane_async_flip(plane, crtc, async_flip);
859 	plane->async_flip(dsb, plane, crtc_state, plane_state, async_flip);
860 }
861 
intel_plane_update_arm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)862 void intel_plane_update_arm(struct intel_dsb *dsb,
863 			    struct intel_plane *plane,
864 			    const struct intel_crtc_state *crtc_state,
865 			    const struct intel_plane_state *plane_state)
866 {
867 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
868 
869 	if (crtc_state->do_async_flip && plane->async_flip) {
870 		intel_plane_async_flip(dsb, plane, crtc_state, plane_state, true);
871 		return;
872 	}
873 
874 	trace_intel_plane_update_arm(plane_state, crtc);
875 	plane->update_arm(dsb, plane, crtc_state, plane_state);
876 }
877 
intel_plane_disable_arm(struct intel_dsb * dsb,struct intel_plane * plane,const struct intel_crtc_state * crtc_state)878 void intel_plane_disable_arm(struct intel_dsb *dsb,
879 			     struct intel_plane *plane,
880 			     const struct intel_crtc_state *crtc_state)
881 {
882 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
883 
884 	trace_intel_plane_disable_arm(plane, crtc);
885 	plane->disable_arm(dsb, plane, crtc_state);
886 }
887 
intel_crtc_planes_update_noarm(struct intel_dsb * dsb,struct intel_atomic_state * state,struct intel_crtc * crtc)888 void intel_crtc_planes_update_noarm(struct intel_dsb *dsb,
889 				    struct intel_atomic_state *state,
890 				    struct intel_crtc *crtc)
891 {
892 	struct intel_crtc_state *new_crtc_state =
893 		intel_atomic_get_new_crtc_state(state, crtc);
894 	u32 update_mask = new_crtc_state->update_planes;
895 	struct intel_plane_state *new_plane_state;
896 	struct intel_plane *plane;
897 	int i;
898 
899 	if (new_crtc_state->do_async_flip)
900 		return;
901 
902 	/*
903 	 * Since we only write non-arming registers here,
904 	 * the order does not matter even for skl+.
905 	 */
906 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
907 		if (crtc->pipe != plane->pipe ||
908 		    !(update_mask & BIT(plane->id)))
909 			continue;
910 
911 		/* TODO: for mailbox updates this should be skipped */
912 		if (new_plane_state->uapi.visible ||
913 		    new_plane_state->is_y_plane)
914 			intel_plane_update_noarm(dsb, plane,
915 						 new_crtc_state, new_plane_state);
916 	}
917 }
918 
skl_crtc_planes_update_arm(struct intel_dsb * dsb,struct intel_atomic_state * state,struct intel_crtc * crtc)919 static void skl_crtc_planes_update_arm(struct intel_dsb *dsb,
920 				       struct intel_atomic_state *state,
921 				       struct intel_crtc *crtc)
922 {
923 	struct intel_crtc_state *old_crtc_state =
924 		intel_atomic_get_old_crtc_state(state, crtc);
925 	struct intel_crtc_state *new_crtc_state =
926 		intel_atomic_get_new_crtc_state(state, crtc);
927 	struct skl_ddb_entry ddb[I915_MAX_PLANES];
928 	struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
929 	u32 update_mask = new_crtc_state->update_planes;
930 	struct intel_plane *plane;
931 
932 	memcpy(ddb, old_crtc_state->wm.skl.plane_ddb,
933 	       sizeof(old_crtc_state->wm.skl.plane_ddb));
934 	memcpy(ddb_y, old_crtc_state->wm.skl.plane_ddb_y,
935 	       sizeof(old_crtc_state->wm.skl.plane_ddb_y));
936 
937 	while ((plane = skl_next_plane_to_commit(state, crtc, ddb, ddb_y, &update_mask))) {
938 		struct intel_plane_state *new_plane_state =
939 			intel_atomic_get_new_plane_state(state, plane);
940 
941 		/*
942 		 * TODO: for mailbox updates intel_plane_update_noarm()
943 		 * would have to be called here as well.
944 		 */
945 		if (new_plane_state->uapi.visible ||
946 		    new_plane_state->is_y_plane)
947 			intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state);
948 		else
949 			intel_plane_disable_arm(dsb, plane, new_crtc_state);
950 	}
951 }
952 
i9xx_crtc_planes_update_arm(struct intel_dsb * dsb,struct intel_atomic_state * state,struct intel_crtc * crtc)953 static void i9xx_crtc_planes_update_arm(struct intel_dsb *dsb,
954 					struct intel_atomic_state *state,
955 					struct intel_crtc *crtc)
956 {
957 	struct intel_crtc_state *new_crtc_state =
958 		intel_atomic_get_new_crtc_state(state, crtc);
959 	u32 update_mask = new_crtc_state->update_planes;
960 	struct intel_plane_state *new_plane_state;
961 	struct intel_plane *plane;
962 	int i;
963 
964 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
965 		if (crtc->pipe != plane->pipe ||
966 		    !(update_mask & BIT(plane->id)))
967 			continue;
968 
969 		/*
970 		 * TODO: for mailbox updates intel_plane_update_noarm()
971 		 * would have to be called here as well.
972 		 */
973 		if (new_plane_state->uapi.visible)
974 			intel_plane_update_arm(dsb, plane, new_crtc_state, new_plane_state);
975 		else
976 			intel_plane_disable_arm(dsb, plane, new_crtc_state);
977 	}
978 }
979 
intel_crtc_planes_update_arm(struct intel_dsb * dsb,struct intel_atomic_state * state,struct intel_crtc * crtc)980 void intel_crtc_planes_update_arm(struct intel_dsb *dsb,
981 				  struct intel_atomic_state *state,
982 				  struct intel_crtc *crtc)
983 {
984 	struct intel_display *display = to_intel_display(state);
985 
986 	if (DISPLAY_VER(display) >= 9)
987 		skl_crtc_planes_update_arm(dsb, state, crtc);
988 	else
989 		i9xx_crtc_planes_update_arm(dsb, state, crtc);
990 }
991 
intel_plane_check_clipping(struct intel_plane_state * plane_state,struct intel_crtc_state * crtc_state,int min_scale,int max_scale,bool can_position)992 int intel_plane_check_clipping(struct intel_plane_state *plane_state,
993 			       struct intel_crtc_state *crtc_state,
994 			       int min_scale, int max_scale,
995 			       bool can_position)
996 {
997 	struct intel_display *display = to_intel_display(plane_state);
998 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
999 	struct drm_framebuffer *fb = plane_state->hw.fb;
1000 	struct drm_rect *src = &plane_state->uapi.src;
1001 	struct drm_rect *dst = &plane_state->uapi.dst;
1002 	const struct drm_rect *clip = &crtc_state->pipe_src;
1003 	unsigned int rotation = plane_state->hw.rotation;
1004 	int hscale, vscale;
1005 
1006 	if (!fb) {
1007 		plane_state->uapi.visible = false;
1008 		return 0;
1009 	}
1010 
1011 	drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
1012 
1013 	/* Check scaling */
1014 	hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
1015 	vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
1016 	if (hscale < 0 || vscale < 0) {
1017 		drm_dbg_kms(display->drm,
1018 			    "[PLANE:%d:%s] invalid scaling "DRM_RECT_FP_FMT " -> " DRM_RECT_FMT "\n",
1019 			    plane->base.base.id, plane->base.name,
1020 			    DRM_RECT_FP_ARG(src), DRM_RECT_ARG(dst));
1021 		return -ERANGE;
1022 	}
1023 
1024 	/*
1025 	 * FIXME: This might need further adjustment for seamless scaling
1026 	 * with phase information, for the 2p2 and 2p1 scenarios.
1027 	 */
1028 	plane_state->uapi.visible = drm_rect_clip_scaled(src, dst, clip);
1029 
1030 	drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
1031 
1032 	if (!can_position && plane_state->uapi.visible &&
1033 	    !drm_rect_equals(dst, clip)) {
1034 		drm_dbg_kms(display->drm,
1035 			    "[PLANE:%d:%s] plane (" DRM_RECT_FMT ") must cover entire CRTC (" DRM_RECT_FMT ")\n",
1036 			    plane->base.base.id, plane->base.name,
1037 			    DRM_RECT_ARG(dst), DRM_RECT_ARG(clip));
1038 		return -EINVAL;
1039 	}
1040 
1041 	/* final plane coordinates will be relative to the plane's pipe */
1042 	drm_rect_translate(dst, -clip->x1, -clip->y1);
1043 
1044 	return 0;
1045 }
1046 
intel_plane_check_src_coordinates(struct intel_plane_state * plane_state)1047 int intel_plane_check_src_coordinates(struct intel_plane_state *plane_state)
1048 {
1049 	struct intel_display *display = to_intel_display(plane_state);
1050 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1051 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1052 	struct drm_rect *src = &plane_state->uapi.src;
1053 	u32 src_x, src_y, src_w, src_h, hsub, vsub;
1054 	bool rotated = drm_rotation_90_or_270(plane_state->hw.rotation);
1055 
1056 	/*
1057 	 * FIXME hsub/vsub vs. block size is a mess. Pre-tgl CCS
1058 	 * abuses hsub/vsub so we can't use them here. But as they
1059 	 * are limited to 32bpp RGB formats we don't actually need
1060 	 * to check anything.
1061 	 */
1062 	if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
1063 	    fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS)
1064 		return 0;
1065 
1066 	/*
1067 	 * Hardware doesn't handle subpixel coordinates.
1068 	 * Adjust to (macro)pixel boundary, but be careful not to
1069 	 * increase the source viewport size, because that could
1070 	 * push the downscaling factor out of bounds.
1071 	 */
1072 	src_x = src->x1 >> 16;
1073 	src_w = drm_rect_width(src) >> 16;
1074 	src_y = src->y1 >> 16;
1075 	src_h = drm_rect_height(src) >> 16;
1076 
1077 	drm_rect_init(src, src_x << 16, src_y << 16,
1078 		      src_w << 16, src_h << 16);
1079 
1080 	if (fb->format->format == DRM_FORMAT_RGB565 && rotated) {
1081 		hsub = 2;
1082 		vsub = 2;
1083 	} else if (DISPLAY_VER(display) >= 20 &&
1084 		   intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier)) {
1085 		/*
1086 		 * This allows NV12 and P0xx formats to have odd size and/or odd
1087 		 * source coordinates on DISPLAY_VER(display) >= 20
1088 		 */
1089 		hsub = 1;
1090 		vsub = 1;
1091 
1092 		/* Wa_16023981245 */
1093 		if ((DISPLAY_VERx100(display) == 2000 ||
1094 		     DISPLAY_VERx100(display) == 3000 ||
1095 		     DISPLAY_VERx100(display) == 3002) &&
1096 		     src_x % 2 != 0)
1097 			hsub = 2;
1098 	} else {
1099 		hsub = fb->format->hsub;
1100 		vsub = fb->format->vsub;
1101 	}
1102 
1103 	if (rotated)
1104 		hsub = vsub = max(hsub, vsub);
1105 
1106 	if (src_x % hsub || src_w % hsub) {
1107 		drm_dbg_kms(display->drm,
1108 			    "[PLANE:%d:%s] src x/w (%u, %u) must be a multiple of %u (rotated: %s)\n",
1109 			    plane->base.base.id, plane->base.name,
1110 			    src_x, src_w, hsub, str_yes_no(rotated));
1111 		return -EINVAL;
1112 	}
1113 
1114 	if (src_y % vsub || src_h % vsub) {
1115 		drm_dbg_kms(display->drm,
1116 			    "[PLANE:%d:%s] src y/h (%u, %u) must be a multiple of %u (rotated: %s)\n",
1117 			    plane->base.base.id, plane->base.name,
1118 			    src_y, src_h, vsub, str_yes_no(rotated));
1119 		return -EINVAL;
1120 	}
1121 
1122 	return 0;
1123 }
1124 
add_dma_resv_fences(struct dma_resv * resv,struct drm_plane_state * new_plane_state)1125 static int add_dma_resv_fences(struct dma_resv *resv,
1126 			       struct drm_plane_state *new_plane_state)
1127 {
1128 	struct dma_fence *fence = dma_fence_get(new_plane_state->fence);
1129 	struct dma_fence *new;
1130 	int ret;
1131 
1132 	ret = dma_resv_get_singleton(resv, dma_resv_usage_rw(false), &new);
1133 	if (ret)
1134 		goto error;
1135 
1136 	if (new && fence) {
1137 		struct dma_fence_chain *chain = dma_fence_chain_alloc();
1138 
1139 		if (!chain) {
1140 			ret = -ENOMEM;
1141 			goto error;
1142 		}
1143 
1144 		dma_fence_chain_init(chain, fence, new, 1);
1145 		fence = &chain->base;
1146 
1147 	} else if (new) {
1148 		fence = new;
1149 	}
1150 
1151 	dma_fence_put(new_plane_state->fence);
1152 	new_plane_state->fence = fence;
1153 	return 0;
1154 
1155 error:
1156 	dma_fence_put(fence);
1157 	return ret;
1158 }
1159 
1160 /**
1161  * intel_prepare_plane_fb - Prepare fb for usage on plane
1162  * @_plane: drm plane to prepare for
1163  * @_new_plane_state: the plane state being prepared
1164  *
1165  * Prepares a framebuffer for usage on a display plane.  Generally this
1166  * involves pinning the underlying object and updating the frontbuffer tracking
1167  * bits.  Some older platforms need special physical address handling for
1168  * cursor planes.
1169  *
1170  * Returns 0 on success, negative error code on failure.
1171  */
1172 static int
intel_prepare_plane_fb(struct drm_plane * _plane,struct drm_plane_state * _new_plane_state)1173 intel_prepare_plane_fb(struct drm_plane *_plane,
1174 		       struct drm_plane_state *_new_plane_state)
1175 {
1176 	struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY };
1177 	struct intel_plane *plane = to_intel_plane(_plane);
1178 	struct intel_display *display = to_intel_display(plane);
1179 	struct intel_plane_state *new_plane_state =
1180 		to_intel_plane_state(_new_plane_state);
1181 	struct intel_atomic_state *state =
1182 		to_intel_atomic_state(new_plane_state->uapi.state);
1183 	struct intel_plane_state *old_plane_state =
1184 		intel_atomic_get_old_plane_state(state, plane);
1185 	struct drm_gem_object *obj = intel_fb_bo(new_plane_state->hw.fb);
1186 	struct drm_gem_object *old_obj = intel_fb_bo(old_plane_state->hw.fb);
1187 	int ret;
1188 
1189 	if (old_obj) {
1190 		const struct intel_crtc_state *new_crtc_state =
1191 			intel_atomic_get_new_crtc_state(state,
1192 							to_intel_crtc(old_plane_state->hw.crtc));
1193 
1194 		/* Big Hammer, we also need to ensure that any pending
1195 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1196 		 * current scanout is retired before unpinning the old
1197 		 * framebuffer. Note that we rely on userspace rendering
1198 		 * into the buffer attached to the pipe they are waiting
1199 		 * on. If not, userspace generates a GPU hang with IPEHR
1200 		 * point to the MI_WAIT_FOR_EVENT.
1201 		 *
1202 		 * This should only fail upon a hung GPU, in which case we
1203 		 * can safely continue.
1204 		 */
1205 		if (intel_crtc_needs_modeset(new_crtc_state)) {
1206 			ret = add_dma_resv_fences(old_obj->resv,
1207 						  &new_plane_state->uapi);
1208 			if (ret < 0)
1209 				return ret;
1210 		}
1211 	}
1212 
1213 	if (!obj)
1214 		return 0;
1215 
1216 	ret = intel_plane_pin_fb(new_plane_state, old_plane_state);
1217 	if (ret)
1218 		return ret;
1219 
1220 	ret = drm_gem_plane_helper_prepare_fb(&plane->base, &new_plane_state->uapi);
1221 	if (ret < 0)
1222 		goto unpin_fb;
1223 
1224 	if (new_plane_state->uapi.fence) {
1225 		i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
1226 					     &attr);
1227 
1228 		intel_display_rps_boost_after_vblank(new_plane_state->hw.crtc,
1229 						     new_plane_state->uapi.fence);
1230 	}
1231 
1232 	/*
1233 	 * We declare pageflips to be interactive and so merit a small bias
1234 	 * towards upclocking to deliver the frame on time. By only changing
1235 	 * the RPS thresholds to sample more regularly and aim for higher
1236 	 * clocks we can hopefully deliver low power workloads (like kodi)
1237 	 * that are not quite steady state without resorting to forcing
1238 	 * maximum clocks following a vblank miss (see do_rps_boost()).
1239 	 */
1240 	intel_display_rps_mark_interactive(display, state, true);
1241 
1242 	return 0;
1243 
1244 unpin_fb:
1245 	intel_plane_unpin_fb(new_plane_state);
1246 
1247 	return ret;
1248 }
1249 
1250 /**
1251  * intel_cleanup_plane_fb - Cleans up an fb after plane use
1252  * @plane: drm plane to clean up for
1253  * @_old_plane_state: the state from the previous modeset
1254  *
1255  * Cleans up a framebuffer that has just been removed from a plane.
1256  */
1257 static void
intel_cleanup_plane_fb(struct drm_plane * plane,struct drm_plane_state * _old_plane_state)1258 intel_cleanup_plane_fb(struct drm_plane *plane,
1259 		       struct drm_plane_state *_old_plane_state)
1260 {
1261 	struct intel_display *display = to_intel_display(plane->dev);
1262 	struct intel_plane_state *old_plane_state =
1263 		to_intel_plane_state(_old_plane_state);
1264 	struct intel_atomic_state *state =
1265 		to_intel_atomic_state(old_plane_state->uapi.state);
1266 	struct drm_gem_object *obj = intel_fb_bo(old_plane_state->hw.fb);
1267 
1268 	if (!obj)
1269 		return;
1270 
1271 	intel_display_rps_mark_interactive(display, state, false);
1272 
1273 	intel_plane_unpin_fb(old_plane_state);
1274 }
1275 
1276 /* Handle Y-tiling, only if DPT is enabled (otherwise disabling tiling is easier)
1277  * All DPT hardware have 128-bytes width tiling, so Y-tile dimension is 32x32
1278  * pixels for 32bits pixels.
1279  */
1280 #define YTILE_WIDTH	32
1281 #define YTILE_HEIGHT	32
1282 #define YTILE_SIZE (YTILE_WIDTH * YTILE_HEIGHT * 4)
1283 
intel_ytile_get_offset(unsigned int width,unsigned int x,unsigned int y)1284 static unsigned int intel_ytile_get_offset(unsigned int width, unsigned int x, unsigned int y)
1285 {
1286 	u32 offset;
1287 	unsigned int swizzle;
1288 	unsigned int width_in_blocks = DIV_ROUND_UP(width, 32);
1289 
1290 	/* Block offset */
1291 	offset = ((y / YTILE_HEIGHT) * width_in_blocks + (x / YTILE_WIDTH)) * YTILE_SIZE;
1292 
1293 	x = x % YTILE_WIDTH;
1294 	y = y % YTILE_HEIGHT;
1295 
1296 	/* bit order inside a block is x4 x3 x2 y4 y3 y2 y1 y0 x1 x0 */
1297 	swizzle = (x & 3) | ((y & 0x1f) << 2) | ((x & 0x1c) << 5);
1298 	offset += swizzle * 4;
1299 	return offset;
1300 }
1301 
intel_4tile_get_offset(unsigned int width,unsigned int x,unsigned int y)1302 static unsigned int intel_4tile_get_offset(unsigned int width, unsigned int x, unsigned int y)
1303 {
1304 	u32 offset;
1305 	unsigned int swizzle;
1306 	unsigned int width_in_blocks = DIV_ROUND_UP(width, 32);
1307 
1308 	/* Block offset */
1309 	offset = ((y / YTILE_HEIGHT) * width_in_blocks + (x / YTILE_WIDTH)) * YTILE_SIZE;
1310 
1311 	x = x % YTILE_WIDTH;
1312 	y = y % YTILE_HEIGHT;
1313 
1314 	/* bit order inside a block is y4 y3 x4 y2 x3 x2 y1 y0 x1 x0 */
1315 	swizzle = (x & 3) | ((y & 3) << 2) | ((x & 0xc) << 2) | (y & 4) << 4 |
1316 		  ((x & 0x10) << 3) | ((y & 0x18) << 5);
1317 	offset += swizzle * 4;
1318 	return offset;
1319 }
1320 
intel_panic_flush(struct drm_plane * plane)1321 static void intel_panic_flush(struct drm_plane *plane)
1322 {
1323 	struct intel_plane_state *plane_state = to_intel_plane_state(plane->state);
1324 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(plane->state->crtc->state);
1325 	struct intel_plane *iplane = to_intel_plane(plane);
1326 	struct intel_display *display = to_intel_display(iplane);
1327 	struct drm_framebuffer *fb = plane_state->hw.fb;
1328 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1329 
1330 	intel_bo_panic_finish(intel_fb);
1331 
1332 	if (crtc_state->enable_psr2_sel_fetch) {
1333 		/* Force a full update for psr2 */
1334 		intel_psr2_panic_force_full_update(display, crtc_state);
1335 	}
1336 
1337 	/* Flush the cache and don't disable tiling if it's the fbdev framebuffer.*/
1338 	if (intel_fb == intel_fbdev_framebuffer(display->fbdev.fbdev)) {
1339 		struct iosys_map map;
1340 
1341 		intel_fbdev_get_map(display->fbdev.fbdev, &map);
1342 		drm_clflush_virt_range(map.vaddr, fb->pitches[0] * fb->height);
1343 		return;
1344 	}
1345 
1346 	if (fb->modifier && iplane->disable_tiling)
1347 		iplane->disable_tiling(iplane);
1348 }
1349 
intel_get_tiling_func(u64 fb_modifier)1350 static unsigned int (*intel_get_tiling_func(u64 fb_modifier))(unsigned int width,
1351 							      unsigned int x,
1352 							      unsigned int y)
1353 {
1354 	switch (fb_modifier) {
1355 	case I915_FORMAT_MOD_Y_TILED:
1356 	case I915_FORMAT_MOD_Y_TILED_CCS:
1357 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1358 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1359 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1360 		return intel_ytile_get_offset;
1361 	case I915_FORMAT_MOD_4_TILED:
1362 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
1363 	case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
1364 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
1365 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
1366 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
1367 	case I915_FORMAT_MOD_4_TILED_MTL_MC_CCS:
1368 	case I915_FORMAT_MOD_4_TILED_BMG_CCS:
1369 	case I915_FORMAT_MOD_4_TILED_LNL_CCS:
1370 		return intel_4tile_get_offset;
1371 	case I915_FORMAT_MOD_X_TILED:
1372 	case I915_FORMAT_MOD_Yf_TILED:
1373 	case I915_FORMAT_MOD_Yf_TILED_CCS:
1374 	default:
1375 	/* Not supported yet */
1376 		return NULL;
1377 	}
1378 }
1379 
intel_get_scanout_buffer(struct drm_plane * plane,struct drm_scanout_buffer * sb)1380 static int intel_get_scanout_buffer(struct drm_plane *plane,
1381 				    struct drm_scanout_buffer *sb)
1382 {
1383 	struct intel_plane_state *plane_state;
1384 	struct drm_gem_object *obj;
1385 	struct drm_framebuffer *fb;
1386 	struct intel_framebuffer *intel_fb;
1387 	struct intel_display *display = to_intel_display(plane->dev);
1388 
1389 	if (!plane->state || !plane->state->fb || !plane->state->visible)
1390 		return -ENODEV;
1391 
1392 	plane_state = to_intel_plane_state(plane->state);
1393 	fb = plane_state->hw.fb;
1394 	intel_fb = to_intel_framebuffer(fb);
1395 
1396 	obj = intel_fb_bo(fb);
1397 	if (!obj)
1398 		return -ENODEV;
1399 
1400 	if (intel_fb == intel_fbdev_framebuffer(display->fbdev.fbdev)) {
1401 		intel_fbdev_get_map(display->fbdev.fbdev, &sb->map[0]);
1402 	} else {
1403 		int ret;
1404 		/* Can't disable tiling if DPT is in use */
1405 		if (intel_fb_uses_dpt(fb)) {
1406 			if (fb->format->cpp[0] != 4)
1407 				return -EOPNOTSUPP;
1408 			intel_fb->panic_tiling = intel_get_tiling_func(fb->modifier);
1409 			if (!intel_fb->panic_tiling)
1410 				return -EOPNOTSUPP;
1411 		}
1412 		sb->private = intel_fb;
1413 		ret = intel_bo_panic_setup(sb);
1414 		if (ret)
1415 			return ret;
1416 	}
1417 	sb->width = fb->width;
1418 	sb->height = fb->height;
1419 	/* Use the generic linear format, because tiling, RC, CCS, CC
1420 	 * will be disabled in disable_tiling()
1421 	 */
1422 	sb->format = drm_format_info(fb->format->format);
1423 	sb->pitch[0] = fb->pitches[0];
1424 
1425 	return 0;
1426 }
1427 
1428 static const struct drm_plane_helper_funcs intel_plane_helper_funcs = {
1429 	.prepare_fb = intel_prepare_plane_fb,
1430 	.cleanup_fb = intel_cleanup_plane_fb,
1431 };
1432 
1433 static const struct drm_plane_helper_funcs intel_primary_plane_helper_funcs = {
1434 	.prepare_fb = intel_prepare_plane_fb,
1435 	.cleanup_fb = intel_cleanup_plane_fb,
1436 	.get_scanout_buffer = intel_get_scanout_buffer,
1437 	.panic_flush = intel_panic_flush,
1438 };
1439 
intel_plane_helper_add(struct intel_plane * plane)1440 void intel_plane_helper_add(struct intel_plane *plane)
1441 {
1442 	if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1443 		drm_plane_helper_add(&plane->base, &intel_primary_plane_helper_funcs);
1444 	else
1445 		drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
1446 }
1447 
intel_plane_init_cursor_vblank_work(struct intel_plane_state * old_plane_state,struct intel_plane_state * new_plane_state)1448 void intel_plane_init_cursor_vblank_work(struct intel_plane_state *old_plane_state,
1449 					 struct intel_plane_state *new_plane_state)
1450 {
1451 	if (!old_plane_state->ggtt_vma ||
1452 	    old_plane_state->ggtt_vma == new_plane_state->ggtt_vma)
1453 		return;
1454 
1455 	drm_vblank_work_init(&old_plane_state->unpin_work, old_plane_state->uapi.crtc,
1456 			     intel_cursor_unpin_work);
1457 }
1458 
link_nv12_planes(struct intel_crtc_state * crtc_state,struct intel_plane_state * uv_plane_state,struct intel_plane_state * y_plane_state)1459 static void link_nv12_planes(struct intel_crtc_state *crtc_state,
1460 			     struct intel_plane_state *uv_plane_state,
1461 			     struct intel_plane_state *y_plane_state)
1462 {
1463 	struct intel_display *display = to_intel_display(uv_plane_state);
1464 	struct intel_plane *uv_plane = to_intel_plane(uv_plane_state->uapi.plane);
1465 	struct intel_plane *y_plane = to_intel_plane(y_plane_state->uapi.plane);
1466 
1467 	drm_dbg_kms(display->drm, "UV plane [PLANE:%d:%s] using Y plane [PLANE:%d:%s]\n",
1468 		    uv_plane->base.base.id, uv_plane->base.name,
1469 		    y_plane->base.base.id, y_plane->base.name);
1470 
1471 	uv_plane_state->planar_linked_plane = y_plane;
1472 
1473 	y_plane_state->is_y_plane = true;
1474 	y_plane_state->planar_linked_plane = uv_plane;
1475 
1476 	crtc_state->enabled_planes |= BIT(y_plane->id);
1477 	crtc_state->active_planes |= BIT(y_plane->id);
1478 	crtc_state->update_planes |= BIT(y_plane->id);
1479 
1480 	crtc_state->data_rate[y_plane->id] = crtc_state->data_rate_y[uv_plane->id];
1481 	crtc_state->rel_data_rate[y_plane->id] = crtc_state->rel_data_rate_y[uv_plane->id];
1482 
1483 	/* Copy parameters to Y plane */
1484 	intel_plane_copy_hw_state(y_plane_state, uv_plane_state);
1485 	y_plane_state->uapi.src = uv_plane_state->uapi.src;
1486 	y_plane_state->uapi.dst = uv_plane_state->uapi.dst;
1487 
1488 	y_plane_state->ctl = uv_plane_state->ctl;
1489 	y_plane_state->color_ctl = uv_plane_state->color_ctl;
1490 	y_plane_state->view = uv_plane_state->view;
1491 	y_plane_state->decrypt = uv_plane_state->decrypt;
1492 
1493 	icl_link_nv12_planes(uv_plane_state, y_plane_state);
1494 }
1495 
unlink_nv12_plane(struct intel_crtc_state * crtc_state,struct intel_plane_state * plane_state)1496 static void unlink_nv12_plane(struct intel_crtc_state *crtc_state,
1497 			      struct intel_plane_state *plane_state)
1498 {
1499 	struct intel_display *display = to_intel_display(plane_state);
1500 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1501 
1502 	plane_state->planar_linked_plane = NULL;
1503 
1504 	if (!plane_state->is_y_plane)
1505 		return;
1506 
1507 	drm_WARN_ON(display->drm, plane_state->uapi.visible);
1508 
1509 	plane_state->is_y_plane = false;
1510 
1511 	crtc_state->enabled_planes &= ~BIT(plane->id);
1512 	crtc_state->active_planes &= ~BIT(plane->id);
1513 	crtc_state->update_planes |= BIT(plane->id);
1514 	crtc_state->data_rate[plane->id] = 0;
1515 	crtc_state->rel_data_rate[plane->id] = 0;
1516 }
1517 
icl_check_nv12_planes(struct intel_atomic_state * state,struct intel_crtc * crtc)1518 static int icl_check_nv12_planes(struct intel_atomic_state *state,
1519 				 struct intel_crtc *crtc)
1520 {
1521 	struct intel_display *display = to_intel_display(state);
1522 	struct intel_crtc_state *crtc_state =
1523 		intel_atomic_get_new_crtc_state(state, crtc);
1524 	struct intel_plane_state *plane_state;
1525 	struct intel_plane *plane;
1526 	int i;
1527 
1528 	if (DISPLAY_VER(display) < 11)
1529 		return 0;
1530 
1531 	/*
1532 	 * Destroy all old plane links and make the Y plane invisible
1533 	 * in the crtc_state->active_planes mask.
1534 	 */
1535 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1536 		if (plane->pipe != crtc->pipe)
1537 			continue;
1538 
1539 		if (plane_state->planar_linked_plane)
1540 			unlink_nv12_plane(crtc_state, plane_state);
1541 	}
1542 
1543 	if (!crtc_state->nv12_planes)
1544 		return 0;
1545 
1546 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1547 		struct intel_plane_state *y_plane_state = NULL;
1548 		struct intel_plane *y_plane;
1549 
1550 		if (plane->pipe != crtc->pipe)
1551 			continue;
1552 
1553 		if ((crtc_state->nv12_planes & BIT(plane->id)) == 0)
1554 			continue;
1555 
1556 		for_each_intel_plane_on_crtc(display->drm, crtc, y_plane) {
1557 			if (!icl_is_nv12_y_plane(display, y_plane->id))
1558 				continue;
1559 
1560 			if (crtc_state->active_planes & BIT(y_plane->id))
1561 				continue;
1562 
1563 			y_plane_state = intel_atomic_get_plane_state(state, y_plane);
1564 			if (IS_ERR(y_plane_state))
1565 				return PTR_ERR(y_plane_state);
1566 
1567 			break;
1568 		}
1569 
1570 		if (!y_plane_state) {
1571 			drm_dbg_kms(display->drm,
1572 				    "[CRTC:%d:%s] need %d free Y planes for planar YUV\n",
1573 				    crtc->base.base.id, crtc->base.name,
1574 				    hweight8(crtc_state->nv12_planes));
1575 			return -EINVAL;
1576 		}
1577 
1578 		link_nv12_planes(crtc_state, plane_state, y_plane_state);
1579 	}
1580 
1581 	return 0;
1582 }
1583 
intel_crtc_add_planes_to_state(struct intel_atomic_state * state,struct intel_crtc * crtc,u8 plane_ids_mask)1584 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
1585 					  struct intel_crtc *crtc,
1586 					  u8 plane_ids_mask)
1587 {
1588 	struct intel_display *display = to_intel_display(state);
1589 	struct intel_plane *plane;
1590 
1591 	for_each_intel_plane_on_crtc(display->drm, crtc, plane) {
1592 		struct intel_plane_state *plane_state;
1593 
1594 		if ((plane_ids_mask & BIT(plane->id)) == 0)
1595 			continue;
1596 
1597 		plane_state = intel_atomic_get_plane_state(state, plane);
1598 		if (IS_ERR(plane_state))
1599 			return PTR_ERR(plane_state);
1600 	}
1601 
1602 	return 0;
1603 }
1604 
intel_plane_add_affected(struct intel_atomic_state * state,struct intel_crtc * crtc)1605 int intel_plane_add_affected(struct intel_atomic_state *state,
1606 			     struct intel_crtc *crtc)
1607 {
1608 	const struct intel_crtc_state *old_crtc_state =
1609 		intel_atomic_get_old_crtc_state(state, crtc);
1610 	const struct intel_crtc_state *new_crtc_state =
1611 		intel_atomic_get_new_crtc_state(state, crtc);
1612 
1613 	return intel_crtc_add_planes_to_state(state, crtc,
1614 					      old_crtc_state->enabled_planes |
1615 					      new_crtc_state->enabled_planes);
1616 }
1617 
active_planes_affects_min_cdclk(struct intel_display * display)1618 static bool active_planes_affects_min_cdclk(struct intel_display *display)
1619 {
1620 	/* See {hsw,vlv,ivb}_plane_ratio() */
1621 	return display->platform.broadwell || display->platform.haswell ||
1622 		display->platform.cherryview || display->platform.valleyview ||
1623 		display->platform.ivybridge;
1624 }
1625 
intel_joiner_affected_planes(struct intel_atomic_state * state,u8 joined_pipes)1626 static u8 intel_joiner_affected_planes(struct intel_atomic_state *state,
1627 				       u8 joined_pipes)
1628 {
1629 	const struct intel_plane_state *plane_state;
1630 	struct intel_plane *plane;
1631 	u8 affected_planes = 0;
1632 	int i;
1633 
1634 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1635 		struct intel_plane *linked = plane_state->planar_linked_plane;
1636 
1637 		if ((joined_pipes & BIT(plane->pipe)) == 0)
1638 			continue;
1639 
1640 		affected_planes |= BIT(plane->id);
1641 		if (linked)
1642 			affected_planes |= BIT(linked->id);
1643 	}
1644 
1645 	return affected_planes;
1646 }
1647 
intel_joiner_add_affected_planes(struct intel_atomic_state * state,u8 joined_pipes)1648 static int intel_joiner_add_affected_planes(struct intel_atomic_state *state,
1649 					    u8 joined_pipes)
1650 {
1651 	u8 prev_affected_planes, affected_planes = 0;
1652 
1653 	/*
1654 	 * We want all the joined pipes to have the same
1655 	 * set of planes in the atomic state, to make sure
1656 	 * state copying always works correctly, and the
1657 	 * UV<->Y plane linkage is always up to date.
1658 	 * Keep pulling planes in until we've determined
1659 	 * the full set of affected planes. A bit complicated
1660 	 * on account of each pipe being capable of selecting
1661 	 * their own Y planes independently of the other pipes,
1662 	 * and the selection being done from the set of
1663 	 * inactive planes.
1664 	 */
1665 	do {
1666 		struct intel_crtc *crtc;
1667 
1668 		for_each_intel_crtc_in_pipe_mask(state->base.dev, crtc, joined_pipes) {
1669 			int ret;
1670 
1671 			ret = intel_crtc_add_planes_to_state(state, crtc, affected_planes);
1672 			if (ret)
1673 				return ret;
1674 		}
1675 
1676 		prev_affected_planes = affected_planes;
1677 		affected_planes = intel_joiner_affected_planes(state, joined_pipes);
1678 	} while (affected_planes != prev_affected_planes);
1679 
1680 	return 0;
1681 }
1682 
intel_add_affected_planes(struct intel_atomic_state * state)1683 static int intel_add_affected_planes(struct intel_atomic_state *state)
1684 {
1685 	const struct intel_crtc_state *crtc_state;
1686 	struct intel_crtc *crtc;
1687 	int i;
1688 
1689 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1690 		int ret;
1691 
1692 		ret = intel_joiner_add_affected_planes(state, intel_crtc_joined_pipe_mask(crtc_state));
1693 		if (ret)
1694 			return ret;
1695 	}
1696 
1697 	return 0;
1698 }
1699 
intel_plane_atomic_check(struct intel_atomic_state * state)1700 int intel_plane_atomic_check(struct intel_atomic_state *state)
1701 {
1702 	struct intel_display *display = to_intel_display(state);
1703 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
1704 	struct intel_plane_state __maybe_unused *plane_state;
1705 	struct intel_plane *plane;
1706 	struct intel_crtc *crtc;
1707 	int i, ret;
1708 
1709 	ret = intel_add_affected_planes(state);
1710 	if (ret)
1711 		return ret;
1712 
1713 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1714 		ret = plane_atomic_check(state, plane);
1715 		if (ret) {
1716 			drm_dbg_atomic(display->drm,
1717 				       "[PLANE:%d:%s] atomic driver check failed\n",
1718 				       plane->base.base.id, plane->base.name);
1719 			return ret;
1720 		}
1721 	}
1722 
1723 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
1724 					    new_crtc_state, i) {
1725 		u8 old_active_planes, new_active_planes;
1726 
1727 		ret = icl_check_nv12_planes(state, crtc);
1728 		if (ret)
1729 			return ret;
1730 
1731 		/*
1732 		 * On some platforms the number of active planes affects
1733 		 * the planes' minimum cdclk calculation. Add such planes
1734 		 * to the state before we compute the minimum cdclk.
1735 		 */
1736 		if (!active_planes_affects_min_cdclk(display))
1737 			continue;
1738 
1739 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1740 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1741 
1742 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
1743 			continue;
1744 
1745 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
1746 		if (ret)
1747 			return ret;
1748 	}
1749 
1750 	return 0;
1751 }
1752 
intel_plane_ggtt_offset(const struct intel_plane_state * plane_state)1753 u32 intel_plane_ggtt_offset(const struct intel_plane_state *plane_state)
1754 {
1755 	return i915_ggtt_offset(plane_state->ggtt_vma);
1756 }
1757