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