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