xref: /linux/drivers/gpu/drm/i915/display/intel_fb.c (revision 569d7db70e5dcf13fbf072f10e9096577ac1e565)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include <drm/drm_blend.h>
7 #include <drm/drm_modeset_helper.h>
8 
9 #include <linux/dma-fence.h>
10 #include <linux/dma-resv.h>
11 
12 #include "gem/i915_gem_object.h"
13 #include "i915_drv.h"
14 #include "intel_display.h"
15 #include "intel_display_types.h"
16 #include "intel_dpt.h"
17 #include "intel_fb.h"
18 #include "intel_fb_bo.h"
19 #include "intel_frontbuffer.h"
20 
21 #define check_array_bounds(i915, a, i) drm_WARN_ON(&(i915)->drm, (i) >= ARRAY_SIZE(a))
22 
23 /*
24  * From the Sky Lake PRM:
25  * "The Color Control Surface (CCS) contains the compression status of
26  *  the cache-line pairs. The compression state of the cache-line pair
27  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
28  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
29  *  cache-line-pairs. CCS is always Y tiled."
30  *
31  * Since cache line pairs refers to horizontally adjacent cache lines,
32  * each cache line in the CCS corresponds to an area of 32x16 cache
33  * lines on the main surface. Since each pixel is 4 bytes, this gives
34  * us a ratio of one byte in the CCS for each 8x16 pixels in the
35  * main surface.
36  */
37 static const struct drm_format_info skl_ccs_formats[] = {
38 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
39 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
40 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
41 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
42 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
43 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
44 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
45 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
46 };
47 
48 /*
49  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
50  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
51  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
52  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
53  * the main surface.
54  */
55 static const struct drm_format_info gen12_ccs_formats[] = {
56 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
57 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
58 	  .hsub = 1, .vsub = 1, },
59 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
60 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
61 	  .hsub = 1, .vsub = 1, },
62 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
63 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
64 	  .hsub = 1, .vsub = 1, .has_alpha = true },
65 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
66 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
67 	  .hsub = 1, .vsub = 1, .has_alpha = true },
68 	{ .format = DRM_FORMAT_YUYV, .num_planes = 2,
69 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
70 	  .hsub = 2, .vsub = 1, .is_yuv = true },
71 	{ .format = DRM_FORMAT_YVYU, .num_planes = 2,
72 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
73 	  .hsub = 2, .vsub = 1, .is_yuv = true },
74 	{ .format = DRM_FORMAT_UYVY, .num_planes = 2,
75 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
76 	  .hsub = 2, .vsub = 1, .is_yuv = true },
77 	{ .format = DRM_FORMAT_VYUY, .num_planes = 2,
78 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
79 	  .hsub = 2, .vsub = 1, .is_yuv = true },
80 	{ .format = DRM_FORMAT_XYUV8888, .num_planes = 2,
81 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
82 	  .hsub = 1, .vsub = 1, .is_yuv = true },
83 	{ .format = DRM_FORMAT_NV12, .num_planes = 4,
84 	  .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
85 	  .hsub = 2, .vsub = 2, .is_yuv = true },
86 	{ .format = DRM_FORMAT_P010, .num_planes = 4,
87 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
88 	  .hsub = 2, .vsub = 2, .is_yuv = true },
89 	{ .format = DRM_FORMAT_P012, .num_planes = 4,
90 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
91 	  .hsub = 2, .vsub = 2, .is_yuv = true },
92 	{ .format = DRM_FORMAT_P016, .num_planes = 4,
93 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
94 	  .hsub = 2, .vsub = 2, .is_yuv = true },
95 };
96 
97 /*
98  * Same as gen12_ccs_formats[] above, but with additional surface used
99  * to pass Clear Color information in plane 2 with 64 bits of data.
100  */
101 static const struct drm_format_info gen12_ccs_cc_formats[] = {
102 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 3,
103 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
104 	  .hsub = 1, .vsub = 1, },
105 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 3,
106 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
107 	  .hsub = 1, .vsub = 1, },
108 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 3,
109 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
110 	  .hsub = 1, .vsub = 1, .has_alpha = true },
111 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 3,
112 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
113 	  .hsub = 1, .vsub = 1, .has_alpha = true },
114 };
115 
116 static const struct drm_format_info gen12_flat_ccs_cc_formats[] = {
117 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
118 	  .char_per_block = { 4, 0 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
119 	  .hsub = 1, .vsub = 1, },
120 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
121 	  .char_per_block = { 4, 0 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
122 	  .hsub = 1, .vsub = 1, },
123 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
124 	  .char_per_block = { 4, 0 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
125 	  .hsub = 1, .vsub = 1, .has_alpha = true },
126 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
127 	  .char_per_block = { 4, 0 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
128 	  .hsub = 1, .vsub = 1, .has_alpha = true },
129 };
130 
131 struct intel_modifier_desc {
132 	u64 modifier;
133 	struct {
134 		u8 from;
135 		u8 until;
136 	} display_ver;
137 #define DISPLAY_VER_ALL		{ 0, -1 }
138 
139 	const struct drm_format_info *formats;
140 	int format_count;
141 #define FORMAT_OVERRIDE(format_list) \
142 	.formats = format_list, \
143 	.format_count = ARRAY_SIZE(format_list)
144 
145 	u8 plane_caps;
146 
147 	struct {
148 		u8 cc_planes:3;
149 		u8 packed_aux_planes:4;
150 		u8 planar_aux_planes:4;
151 	} ccs;
152 };
153 
154 #define INTEL_PLANE_CAP_CCS_MASK	(INTEL_PLANE_CAP_CCS_RC | \
155 					 INTEL_PLANE_CAP_CCS_RC_CC | \
156 					 INTEL_PLANE_CAP_CCS_MC)
157 #define INTEL_PLANE_CAP_TILING_MASK	(INTEL_PLANE_CAP_TILING_X | \
158 					 INTEL_PLANE_CAP_TILING_Y | \
159 					 INTEL_PLANE_CAP_TILING_Yf | \
160 					 INTEL_PLANE_CAP_TILING_4)
161 #define INTEL_PLANE_CAP_TILING_NONE	0
162 
163 static const struct intel_modifier_desc intel_modifiers[] = {
164 	{
165 		.modifier = I915_FORMAT_MOD_4_TILED_MTL_MC_CCS,
166 		.display_ver = { 14, 14 },
167 		.plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_MC,
168 
169 		.ccs.packed_aux_planes = BIT(1),
170 		.ccs.planar_aux_planes = BIT(2) | BIT(3),
171 
172 		FORMAT_OVERRIDE(gen12_ccs_formats),
173 	}, {
174 		.modifier = I915_FORMAT_MOD_4_TILED_MTL_RC_CCS,
175 		.display_ver = { 14, 14 },
176 		.plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_RC,
177 
178 		.ccs.packed_aux_planes = BIT(1),
179 
180 		FORMAT_OVERRIDE(gen12_ccs_formats),
181 	}, {
182 		.modifier = I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC,
183 		.display_ver = { 14, 14 },
184 		.plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_RC_CC,
185 
186 		.ccs.cc_planes = BIT(2),
187 		.ccs.packed_aux_planes = BIT(1),
188 
189 		FORMAT_OVERRIDE(gen12_ccs_cc_formats),
190 	}, {
191 		.modifier = I915_FORMAT_MOD_4_TILED_DG2_MC_CCS,
192 		.display_ver = { 13, 13 },
193 		.plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_MC,
194 	}, {
195 		.modifier = I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC,
196 		.display_ver = { 13, 13 },
197 		.plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_RC_CC,
198 
199 		.ccs.cc_planes = BIT(1),
200 
201 		FORMAT_OVERRIDE(gen12_flat_ccs_cc_formats),
202 	}, {
203 		.modifier = I915_FORMAT_MOD_4_TILED_DG2_RC_CCS,
204 		.display_ver = { 13, 13 },
205 		.plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_RC,
206 	}, {
207 		.modifier = I915_FORMAT_MOD_4_TILED,
208 		.display_ver = { 13, -1 },
209 		.plane_caps = INTEL_PLANE_CAP_TILING_4,
210 	}, {
211 		.modifier = I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS,
212 		.display_ver = { 12, 13 },
213 		.plane_caps = INTEL_PLANE_CAP_TILING_Y | INTEL_PLANE_CAP_CCS_MC,
214 
215 		.ccs.packed_aux_planes = BIT(1),
216 		.ccs.planar_aux_planes = BIT(2) | BIT(3),
217 
218 		FORMAT_OVERRIDE(gen12_ccs_formats),
219 	}, {
220 		.modifier = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,
221 		.display_ver = { 12, 13 },
222 		.plane_caps = INTEL_PLANE_CAP_TILING_Y | INTEL_PLANE_CAP_CCS_RC,
223 
224 		.ccs.packed_aux_planes = BIT(1),
225 
226 		FORMAT_OVERRIDE(gen12_ccs_formats),
227 	}, {
228 		.modifier = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC,
229 		.display_ver = { 12, 13 },
230 		.plane_caps = INTEL_PLANE_CAP_TILING_Y | INTEL_PLANE_CAP_CCS_RC_CC,
231 
232 		.ccs.cc_planes = BIT(2),
233 		.ccs.packed_aux_planes = BIT(1),
234 
235 		FORMAT_OVERRIDE(gen12_ccs_cc_formats),
236 	}, {
237 		.modifier = I915_FORMAT_MOD_Yf_TILED_CCS,
238 		.display_ver = { 9, 11 },
239 		.plane_caps = INTEL_PLANE_CAP_TILING_Yf | INTEL_PLANE_CAP_CCS_RC,
240 
241 		.ccs.packed_aux_planes = BIT(1),
242 
243 		FORMAT_OVERRIDE(skl_ccs_formats),
244 	}, {
245 		.modifier = I915_FORMAT_MOD_Y_TILED_CCS,
246 		.display_ver = { 9, 11 },
247 		.plane_caps = INTEL_PLANE_CAP_TILING_Y | INTEL_PLANE_CAP_CCS_RC,
248 
249 		.ccs.packed_aux_planes = BIT(1),
250 
251 		FORMAT_OVERRIDE(skl_ccs_formats),
252 	}, {
253 		.modifier = I915_FORMAT_MOD_Yf_TILED,
254 		.display_ver = { 9, 11 },
255 		.plane_caps = INTEL_PLANE_CAP_TILING_Yf,
256 	}, {
257 		.modifier = I915_FORMAT_MOD_Y_TILED,
258 		.display_ver = { 9, 13 },
259 		.plane_caps = INTEL_PLANE_CAP_TILING_Y,
260 	}, {
261 		.modifier = I915_FORMAT_MOD_X_TILED,
262 		.display_ver = DISPLAY_VER_ALL,
263 		.plane_caps = INTEL_PLANE_CAP_TILING_X,
264 	}, {
265 		.modifier = DRM_FORMAT_MOD_LINEAR,
266 		.display_ver = DISPLAY_VER_ALL,
267 	},
268 };
269 
270 static const struct intel_modifier_desc *lookup_modifier_or_null(u64 modifier)
271 {
272 	int i;
273 
274 	for (i = 0; i < ARRAY_SIZE(intel_modifiers); i++)
275 		if (intel_modifiers[i].modifier == modifier)
276 			return &intel_modifiers[i];
277 
278 	return NULL;
279 }
280 
281 static const struct intel_modifier_desc *lookup_modifier(u64 modifier)
282 {
283 	const struct intel_modifier_desc *md = lookup_modifier_or_null(modifier);
284 
285 	if (WARN_ON(!md))
286 		return &intel_modifiers[0];
287 
288 	return md;
289 }
290 
291 static const struct drm_format_info *
292 lookup_format_info(const struct drm_format_info formats[],
293 		   int num_formats, u32 format)
294 {
295 	int i;
296 
297 	for (i = 0; i < num_formats; i++) {
298 		if (formats[i].format == format)
299 			return &formats[i];
300 	}
301 
302 	return NULL;
303 }
304 
305 unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
306 {
307 	const struct intel_modifier_desc *md;
308 	u8 tiling_caps;
309 
310 	md = lookup_modifier_or_null(fb_modifier);
311 	if (!md)
312 		return I915_TILING_NONE;
313 
314 	tiling_caps = lookup_modifier_or_null(fb_modifier)->plane_caps &
315 			 INTEL_PLANE_CAP_TILING_MASK;
316 
317 	switch (tiling_caps) {
318 	case INTEL_PLANE_CAP_TILING_Y:
319 		return I915_TILING_Y;
320 	case INTEL_PLANE_CAP_TILING_X:
321 		return I915_TILING_X;
322 	case INTEL_PLANE_CAP_TILING_4:
323 	case INTEL_PLANE_CAP_TILING_Yf:
324 	case INTEL_PLANE_CAP_TILING_NONE:
325 		return I915_TILING_NONE;
326 	default:
327 		MISSING_CASE(tiling_caps);
328 		return I915_TILING_NONE;
329 	}
330 }
331 
332 /**
333  * intel_fb_get_format_info: Get a modifier specific format information
334  * @cmd: FB add command structure
335  *
336  * Returns:
337  * Returns the format information for @cmd->pixel_format specific to @cmd->modifier[0],
338  * or %NULL if the modifier doesn't override the format.
339  */
340 const struct drm_format_info *
341 intel_fb_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
342 {
343 	const struct intel_modifier_desc *md = lookup_modifier_or_null(cmd->modifier[0]);
344 
345 	if (!md || !md->formats)
346 		return NULL;
347 
348 	return lookup_format_info(md->formats, md->format_count, cmd->pixel_format);
349 }
350 
351 static bool plane_caps_contain_any(u8 caps, u8 mask)
352 {
353 	return caps & mask;
354 }
355 
356 static bool plane_caps_contain_all(u8 caps, u8 mask)
357 {
358 	return (caps & mask) == mask;
359 }
360 
361 /**
362  * intel_fb_is_tiled_modifier: Check if a modifier is a tiled modifier type
363  * @modifier: Modifier to check
364  *
365  * Returns:
366  * Returns %true if @modifier is a tiled modifier.
367  */
368 bool intel_fb_is_tiled_modifier(u64 modifier)
369 {
370 	return plane_caps_contain_any(lookup_modifier(modifier)->plane_caps,
371 				      INTEL_PLANE_CAP_TILING_MASK);
372 }
373 
374 /**
375  * intel_fb_is_ccs_modifier: Check if a modifier is a CCS modifier type
376  * @modifier: Modifier to check
377  *
378  * Returns:
379  * Returns %true if @modifier is a render, render with color clear or
380  * media compression modifier.
381  */
382 bool intel_fb_is_ccs_modifier(u64 modifier)
383 {
384 	return plane_caps_contain_any(lookup_modifier(modifier)->plane_caps,
385 				      INTEL_PLANE_CAP_CCS_MASK);
386 }
387 
388 /**
389  * intel_fb_is_rc_ccs_cc_modifier: Check if a modifier is an RC CCS CC modifier type
390  * @modifier: Modifier to check
391  *
392  * Returns:
393  * Returns %true if @modifier is a render with color clear modifier.
394  */
395 bool intel_fb_is_rc_ccs_cc_modifier(u64 modifier)
396 {
397 	return plane_caps_contain_any(lookup_modifier(modifier)->plane_caps,
398 				      INTEL_PLANE_CAP_CCS_RC_CC);
399 }
400 
401 /**
402  * intel_fb_is_mc_ccs_modifier: Check if a modifier is an MC CCS modifier type
403  * @modifier: Modifier to check
404  *
405  * Returns:
406  * Returns %true if @modifier is a media compression modifier.
407  */
408 bool intel_fb_is_mc_ccs_modifier(u64 modifier)
409 {
410 	return plane_caps_contain_any(lookup_modifier(modifier)->plane_caps,
411 				      INTEL_PLANE_CAP_CCS_MC);
412 }
413 
414 static bool check_modifier_display_ver_range(const struct intel_modifier_desc *md,
415 					     u8 display_ver_from, u8 display_ver_until)
416 {
417 	return md->display_ver.from <= display_ver_until &&
418 		display_ver_from <= md->display_ver.until;
419 }
420 
421 static bool plane_has_modifier(struct drm_i915_private *i915,
422 			       u8 plane_caps,
423 			       const struct intel_modifier_desc *md)
424 {
425 	if (!IS_DISPLAY_VER(i915, md->display_ver.from, md->display_ver.until))
426 		return false;
427 
428 	if (!plane_caps_contain_all(plane_caps, md->plane_caps))
429 		return false;
430 
431 	/*
432 	 * Separate AuxCCS and Flat CCS modifiers to be run only on platforms
433 	 * where supported.
434 	 */
435 	if (intel_fb_is_ccs_modifier(md->modifier) &&
436 	    HAS_FLAT_CCS(i915) != !md->ccs.packed_aux_planes)
437 		return false;
438 
439 	return true;
440 }
441 
442 /**
443  * intel_fb_plane_get_modifiers: Get the modifiers for the given platform and plane capabilities
444  * @i915: i915 device instance
445  * @plane_caps: capabilities for the plane the modifiers are queried for
446  *
447  * Returns:
448  * Returns the list of modifiers allowed by the @i915 platform and @plane_caps.
449  * The caller must free the returned buffer.
450  */
451 u64 *intel_fb_plane_get_modifiers(struct drm_i915_private *i915,
452 				  u8 plane_caps)
453 {
454 	u64 *list, *p;
455 	int count = 1;		/* +1 for invalid modifier terminator */
456 	int i;
457 
458 	for (i = 0; i < ARRAY_SIZE(intel_modifiers); i++) {
459 		if (plane_has_modifier(i915, plane_caps, &intel_modifiers[i]))
460 			count++;
461 	}
462 
463 	list = kmalloc_array(count, sizeof(*list), GFP_KERNEL);
464 	if (drm_WARN_ON(&i915->drm, !list))
465 		return NULL;
466 
467 	p = list;
468 	for (i = 0; i < ARRAY_SIZE(intel_modifiers); i++) {
469 		if (plane_has_modifier(i915, plane_caps, &intel_modifiers[i]))
470 			*p++ = intel_modifiers[i].modifier;
471 	}
472 	*p++ = DRM_FORMAT_MOD_INVALID;
473 
474 	return list;
475 }
476 
477 /**
478  * intel_fb_plane_supports_modifier: Determine if a modifier is supported by the given plane
479  * @plane: Plane to check the modifier support for
480  * @modifier: The modifier to check the support for
481  *
482  * Returns:
483  * %true if the @modifier is supported on @plane.
484  */
485 bool intel_fb_plane_supports_modifier(struct intel_plane *plane, u64 modifier)
486 {
487 	int i;
488 
489 	for (i = 0; i < plane->base.modifier_count; i++)
490 		if (plane->base.modifiers[i] == modifier)
491 			return true;
492 
493 	return false;
494 }
495 
496 static bool format_is_yuv_semiplanar(const struct intel_modifier_desc *md,
497 				     const struct drm_format_info *info)
498 {
499 	if (!info->is_yuv)
500 		return false;
501 
502 	if (hweight8(md->ccs.planar_aux_planes) == 2)
503 		return info->num_planes == 4;
504 	else
505 		return info->num_planes == 2;
506 }
507 
508 /**
509  * intel_format_info_is_yuv_semiplanar: Check if the given format is YUV semiplanar
510  * @info: format to check
511  * @modifier: modifier used with the format
512  *
513  * Returns:
514  * %true if @info / @modifier is YUV semiplanar.
515  */
516 bool intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
517 					 u64 modifier)
518 {
519 	return format_is_yuv_semiplanar(lookup_modifier(modifier), info);
520 }
521 
522 static u8 ccs_aux_plane_mask(const struct intel_modifier_desc *md,
523 			     const struct drm_format_info *format)
524 {
525 	if (format_is_yuv_semiplanar(md, format))
526 		return md->ccs.planar_aux_planes;
527 	else
528 		return md->ccs.packed_aux_planes;
529 }
530 
531 /**
532  * intel_fb_is_ccs_aux_plane: Check if a framebuffer color plane is a CCS AUX plane
533  * @fb: Framebuffer
534  * @color_plane: color plane index to check
535  *
536  * Returns:
537  * Returns %true if @fb's color plane at index @color_plane is a CCS AUX plane.
538  */
539 bool intel_fb_is_ccs_aux_plane(const struct drm_framebuffer *fb, int color_plane)
540 {
541 	const struct intel_modifier_desc *md = lookup_modifier(fb->modifier);
542 
543 	return ccs_aux_plane_mask(md, fb->format) & BIT(color_plane);
544 }
545 
546 /**
547  * intel_fb_is_gen12_ccs_aux_plane: Check if a framebuffer color plane is a GEN12 CCS AUX plane
548  * @fb: Framebuffer
549  * @color_plane: color plane index to check
550  *
551  * Returns:
552  * Returns %true if @fb's color plane at index @color_plane is a GEN12 CCS AUX plane.
553  */
554 static bool intel_fb_is_gen12_ccs_aux_plane(const struct drm_framebuffer *fb, int color_plane)
555 {
556 	const struct intel_modifier_desc *md = lookup_modifier(fb->modifier);
557 
558 	return check_modifier_display_ver_range(md, 12, 14) &&
559 	       ccs_aux_plane_mask(md, fb->format) & BIT(color_plane);
560 }
561 
562 /**
563  * intel_fb_rc_ccs_cc_plane: Get the CCS CC color plane index for a framebuffer
564  * @fb: Framebuffer
565  *
566  * Returns:
567  * Returns the index of the color clear plane for @fb, or -1 if @fb is not a
568  * framebuffer using a render compression/color clear modifier.
569  */
570 int intel_fb_rc_ccs_cc_plane(const struct drm_framebuffer *fb)
571 {
572 	const struct intel_modifier_desc *md = lookup_modifier(fb->modifier);
573 
574 	if (!md->ccs.cc_planes)
575 		return -1;
576 
577 	drm_WARN_ON_ONCE(fb->dev, hweight8(md->ccs.cc_planes) > 1);
578 
579 	return ilog2((int)md->ccs.cc_planes);
580 }
581 
582 static bool is_gen12_ccs_cc_plane(const struct drm_framebuffer *fb, int color_plane)
583 {
584 	return intel_fb_rc_ccs_cc_plane(fb) == color_plane;
585 }
586 
587 static bool is_semiplanar_uv_plane(const struct drm_framebuffer *fb, int color_plane)
588 {
589 	return intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier) &&
590 		color_plane == 1;
591 }
592 
593 bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
594 {
595 	return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
596 	       intel_fb_is_gen12_ccs_aux_plane(fb, color_plane) ||
597 	       is_gen12_ccs_cc_plane(fb, color_plane);
598 }
599 
600 int main_to_ccs_plane(const struct drm_framebuffer *fb, int main_plane)
601 {
602 	drm_WARN_ON(fb->dev, !intel_fb_is_ccs_modifier(fb->modifier) ||
603 		    (main_plane && main_plane >= fb->format->num_planes / 2));
604 
605 	return fb->format->num_planes / 2 + main_plane;
606 }
607 
608 int skl_ccs_to_main_plane(const struct drm_framebuffer *fb, int ccs_plane)
609 {
610 	drm_WARN_ON(fb->dev, !intel_fb_is_ccs_modifier(fb->modifier) ||
611 		    ccs_plane < fb->format->num_planes / 2);
612 
613 	if (is_gen12_ccs_cc_plane(fb, ccs_plane))
614 		return 0;
615 
616 	return ccs_plane - fb->format->num_planes / 2;
617 }
618 
619 static unsigned int gen12_ccs_aux_stride(struct intel_framebuffer *fb, int ccs_plane)
620 {
621 	int main_plane = skl_ccs_to_main_plane(&fb->base, ccs_plane);
622 	unsigned int main_stride = fb->base.pitches[main_plane];
623 	unsigned int main_tile_width = intel_tile_width_bytes(&fb->base, main_plane);
624 
625 	return DIV_ROUND_UP(main_stride, 4 * main_tile_width) * 64;
626 }
627 
628 int skl_main_to_aux_plane(const struct drm_framebuffer *fb, int main_plane)
629 {
630 	const struct intel_modifier_desc *md = lookup_modifier(fb->modifier);
631 	struct drm_i915_private *i915 = to_i915(fb->dev);
632 
633 	if (md->ccs.packed_aux_planes | md->ccs.planar_aux_planes)
634 		return main_to_ccs_plane(fb, main_plane);
635 	else if (DISPLAY_VER(i915) < 11 &&
636 		 format_is_yuv_semiplanar(md, fb->format))
637 		return 1;
638 	else
639 		return 0;
640 }
641 
642 unsigned int intel_tile_size(const struct drm_i915_private *i915)
643 {
644 	return DISPLAY_VER(i915) == 2 ? 2048 : 4096;
645 }
646 
647 unsigned int
648 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
649 {
650 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
651 	unsigned int cpp = fb->format->cpp[color_plane];
652 
653 	switch (fb->modifier) {
654 	case DRM_FORMAT_MOD_LINEAR:
655 		return intel_tile_size(dev_priv);
656 	case I915_FORMAT_MOD_X_TILED:
657 		if (DISPLAY_VER(dev_priv) == 2)
658 			return 128;
659 		else
660 			return 512;
661 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
662 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
663 	case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
664 	case I915_FORMAT_MOD_4_TILED:
665 		/*
666 		 * Each 4K tile consists of 64B(8*8) subtiles, with
667 		 * same shape as Y Tile(i.e 4*16B OWords)
668 		 */
669 		return 128;
670 	case I915_FORMAT_MOD_Y_TILED_CCS:
671 		if (intel_fb_is_ccs_aux_plane(fb, color_plane))
672 			return 128;
673 		fallthrough;
674 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
675 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
676 	case I915_FORMAT_MOD_4_TILED_MTL_MC_CCS:
677 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
678 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
679 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
680 		if (intel_fb_is_ccs_aux_plane(fb, color_plane) ||
681 		    is_gen12_ccs_cc_plane(fb, color_plane))
682 			return 64;
683 		fallthrough;
684 	case I915_FORMAT_MOD_Y_TILED:
685 		if (DISPLAY_VER(dev_priv) == 2 || HAS_128_BYTE_Y_TILING(dev_priv))
686 			return 128;
687 		else
688 			return 512;
689 	case I915_FORMAT_MOD_Yf_TILED_CCS:
690 		if (intel_fb_is_ccs_aux_plane(fb, color_plane))
691 			return 128;
692 		fallthrough;
693 	case I915_FORMAT_MOD_Yf_TILED:
694 		switch (cpp) {
695 		case 1:
696 			return 64;
697 		case 2:
698 		case 4:
699 			return 128;
700 		case 8:
701 		case 16:
702 			return 256;
703 		default:
704 			MISSING_CASE(cpp);
705 			return cpp;
706 		}
707 		break;
708 	default:
709 		MISSING_CASE(fb->modifier);
710 		return cpp;
711 	}
712 }
713 
714 unsigned int intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
715 {
716 	return intel_tile_size(to_i915(fb->dev)) /
717 		intel_tile_width_bytes(fb, color_plane);
718 }
719 
720 /*
721  * Return the tile dimensions in pixel units, based on the (2 or 4 kbyte) GTT
722  * page tile size.
723  */
724 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
725 			    unsigned int *tile_width,
726 			    unsigned int *tile_height)
727 {
728 	unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
729 	unsigned int cpp = fb->format->cpp[color_plane];
730 
731 	*tile_width = tile_width_bytes / cpp;
732 	*tile_height = intel_tile_height(fb, color_plane);
733 }
734 
735 /*
736  * Return the tile dimensions in pixel units, based on the tile block size.
737  * The block covers the full GTT page sized tile on all tiled surfaces and
738  * it's a 64 byte portion of the tile on TGL+ CCS surfaces.
739  */
740 static void intel_tile_block_dims(const struct drm_framebuffer *fb, int color_plane,
741 				  unsigned int *tile_width,
742 				  unsigned int *tile_height)
743 {
744 	intel_tile_dims(fb, color_plane, tile_width, tile_height);
745 
746 	if (intel_fb_is_gen12_ccs_aux_plane(fb, color_plane))
747 		*tile_height = 1;
748 }
749 
750 unsigned int intel_tile_row_size(const struct drm_framebuffer *fb, int color_plane)
751 {
752 	unsigned int tile_width, tile_height;
753 
754 	intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
755 
756 	return fb->pitches[color_plane] * tile_height;
757 }
758 
759 unsigned int
760 intel_fb_align_height(const struct drm_framebuffer *fb,
761 		      int color_plane, unsigned int height)
762 {
763 	unsigned int tile_height = intel_tile_height(fb, color_plane);
764 
765 	return ALIGN(height, tile_height);
766 }
767 
768 bool intel_fb_modifier_uses_dpt(struct drm_i915_private *i915, u64 modifier)
769 {
770 	return HAS_DPT(i915) && modifier != DRM_FORMAT_MOD_LINEAR;
771 }
772 
773 bool intel_fb_uses_dpt(const struct drm_framebuffer *fb)
774 {
775 	return to_i915(fb->dev)->display.params.enable_dpt &&
776 		intel_fb_modifier_uses_dpt(to_i915(fb->dev), fb->modifier);
777 }
778 
779 unsigned int intel_cursor_alignment(const struct drm_i915_private *i915)
780 {
781 	if (IS_I830(i915))
782 		return 16 * 1024;
783 	else if (IS_I85X(i915))
784 		return 256;
785 	else if (IS_I845G(i915) || IS_I865G(i915))
786 		return 32;
787 	else
788 		return 4 * 1024;
789 }
790 
791 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
792 {
793 	if (DISPLAY_VER(dev_priv) >= 9)
794 		return 256 * 1024;
795 	else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
796 		 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
797 		return 128 * 1024;
798 	else if (DISPLAY_VER(dev_priv) >= 4)
799 		return 4 * 1024;
800 	else
801 		return 0;
802 }
803 
804 unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
805 				  int color_plane)
806 {
807 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
808 
809 	if (intel_fb_uses_dpt(fb)) {
810 		/* AUX_DIST needs only 4K alignment */
811 		if (intel_fb_is_ccs_aux_plane(fb, color_plane))
812 			return 512 * 4096;
813 
814 		/*
815 		 * FIXME ADL sees GGTT/DMAR faults with async
816 		 * flips unless we align to 16k at least.
817 		 * Figure out what's going on here...
818 		 */
819 		if (IS_ALDERLAKE_P(dev_priv) &&
820 		    !intel_fb_is_ccs_modifier(fb->modifier) &&
821 		    HAS_ASYNC_FLIPS(dev_priv))
822 			return 512 * 16 * 1024;
823 
824 		return 512 * 4096;
825 	}
826 
827 	/* AUX_DIST needs only 4K alignment */
828 	if (intel_fb_is_ccs_aux_plane(fb, color_plane))
829 		return 4096;
830 
831 	if (is_semiplanar_uv_plane(fb, color_plane)) {
832 		/*
833 		 * TODO: cross-check wrt. the bspec stride in bytes * 64 bytes
834 		 * alignment for linear UV planes on all platforms.
835 		 */
836 		if (DISPLAY_VER(dev_priv) >= 12) {
837 			if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
838 				return intel_linear_alignment(dev_priv);
839 
840 			return intel_tile_row_size(fb, color_plane);
841 		}
842 
843 		return 4096;
844 	}
845 
846 	drm_WARN_ON(&dev_priv->drm, color_plane != 0);
847 
848 	switch (fb->modifier) {
849 	case DRM_FORMAT_MOD_LINEAR:
850 		return intel_linear_alignment(dev_priv);
851 	case I915_FORMAT_MOD_X_TILED:
852 		if (HAS_ASYNC_FLIPS(dev_priv))
853 			return 256 * 1024;
854 		return 0;
855 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
856 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
857 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
858 	case I915_FORMAT_MOD_4_TILED_MTL_MC_CCS:
859 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
860 	case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
861 		return 16 * 1024;
862 	case I915_FORMAT_MOD_Y_TILED_CCS:
863 	case I915_FORMAT_MOD_Yf_TILED_CCS:
864 	case I915_FORMAT_MOD_Y_TILED:
865 	case I915_FORMAT_MOD_4_TILED:
866 	case I915_FORMAT_MOD_Yf_TILED:
867 		return 1 * 1024 * 1024;
868 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
869 	case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
870 	case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
871 		return 16 * 1024;
872 	default:
873 		MISSING_CASE(fb->modifier);
874 		return 0;
875 	}
876 }
877 
878 void intel_fb_plane_get_subsampling(int *hsub, int *vsub,
879 				    const struct drm_framebuffer *fb,
880 				    int color_plane)
881 {
882 	int main_plane;
883 
884 	if (color_plane == 0) {
885 		*hsub = 1;
886 		*vsub = 1;
887 
888 		return;
889 	}
890 
891 	/*
892 	 * TODO: Deduct the subsampling from the char block for all CCS
893 	 * formats and planes.
894 	 */
895 	if (!intel_fb_is_gen12_ccs_aux_plane(fb, color_plane)) {
896 		*hsub = fb->format->hsub;
897 		*vsub = fb->format->vsub;
898 
899 		return;
900 	}
901 
902 	main_plane = skl_ccs_to_main_plane(fb, color_plane);
903 	*hsub = drm_format_info_block_width(fb->format, color_plane) /
904 		drm_format_info_block_width(fb->format, main_plane);
905 
906 	/*
907 	 * The min stride check in the core framebuffer_check() function
908 	 * assumes that format->hsub applies to every plane except for the
909 	 * first plane. That's incorrect for the CCS AUX plane of the first
910 	 * plane, but for the above check to pass we must define the block
911 	 * width with that subsampling applied to it. Adjust the width here
912 	 * accordingly, so we can calculate the actual subsampling factor.
913 	 */
914 	if (main_plane == 0)
915 		*hsub *= fb->format->hsub;
916 
917 	*vsub = 32;
918 }
919 
920 static void intel_fb_plane_dims(const struct intel_framebuffer *fb, int color_plane, int *w, int *h)
921 {
922 	int main_plane = intel_fb_is_ccs_aux_plane(&fb->base, color_plane) ?
923 			 skl_ccs_to_main_plane(&fb->base, color_plane) : 0;
924 	unsigned int main_width = fb->base.width;
925 	unsigned int main_height = fb->base.height;
926 	int main_hsub, main_vsub;
927 	int hsub, vsub;
928 
929 	intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, &fb->base, main_plane);
930 	intel_fb_plane_get_subsampling(&hsub, &vsub, &fb->base, color_plane);
931 
932 	*w = DIV_ROUND_UP(main_width, main_hsub * hsub);
933 	*h = DIV_ROUND_UP(main_height, main_vsub * vsub);
934 }
935 
936 static u32 intel_adjust_tile_offset(int *x, int *y,
937 				    unsigned int tile_width,
938 				    unsigned int tile_height,
939 				    unsigned int tile_size,
940 				    unsigned int pitch_tiles,
941 				    u32 old_offset,
942 				    u32 new_offset)
943 {
944 	unsigned int pitch_pixels = pitch_tiles * tile_width;
945 	unsigned int tiles;
946 
947 	WARN_ON(old_offset & (tile_size - 1));
948 	WARN_ON(new_offset & (tile_size - 1));
949 	WARN_ON(new_offset > old_offset);
950 
951 	tiles = (old_offset - new_offset) / tile_size;
952 
953 	*y += tiles / pitch_tiles * tile_height;
954 	*x += tiles % pitch_tiles * tile_width;
955 
956 	/* minimize x in case it got needlessly big */
957 	*y += *x / pitch_pixels * tile_height;
958 	*x %= pitch_pixels;
959 
960 	return new_offset;
961 }
962 
963 static u32 intel_adjust_linear_offset(int *x, int *y,
964 				      unsigned int cpp,
965 				      unsigned int pitch,
966 				      u32 old_offset,
967 				      u32 new_offset)
968 {
969 	old_offset += *y * pitch + *x * cpp;
970 
971 	*y = (old_offset - new_offset) / pitch;
972 	*x = ((old_offset - new_offset) - *y * pitch) / cpp;
973 
974 	return new_offset;
975 }
976 
977 static u32 intel_adjust_aligned_offset(int *x, int *y,
978 				       const struct drm_framebuffer *fb,
979 				       int color_plane,
980 				       unsigned int rotation,
981 				       unsigned int pitch,
982 				       u32 old_offset, u32 new_offset)
983 {
984 	struct drm_i915_private *i915 = to_i915(fb->dev);
985 	unsigned int cpp = fb->format->cpp[color_plane];
986 
987 	drm_WARN_ON(&i915->drm, new_offset > old_offset);
988 
989 	if (!is_surface_linear(fb, color_plane)) {
990 		unsigned int tile_size, tile_width, tile_height;
991 		unsigned int pitch_tiles;
992 
993 		tile_size = intel_tile_size(i915);
994 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
995 
996 		if (drm_rotation_90_or_270(rotation)) {
997 			pitch_tiles = pitch / tile_height;
998 			swap(tile_width, tile_height);
999 		} else {
1000 			pitch_tiles = pitch / (tile_width * cpp);
1001 		}
1002 
1003 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
1004 					 tile_size, pitch_tiles,
1005 					 old_offset, new_offset);
1006 	} else {
1007 		intel_adjust_linear_offset(x, y, cpp, pitch,
1008 					   old_offset, new_offset);
1009 	}
1010 
1011 	return new_offset;
1012 }
1013 
1014 /*
1015  * Adjust the tile offset by moving the difference into
1016  * the x/y offsets.
1017  */
1018 u32 intel_plane_adjust_aligned_offset(int *x, int *y,
1019 				      const struct intel_plane_state *state,
1020 				      int color_plane,
1021 				      u32 old_offset, u32 new_offset)
1022 {
1023 	return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
1024 					   state->hw.rotation,
1025 					   state->view.color_plane[color_plane].mapping_stride,
1026 					   old_offset, new_offset);
1027 }
1028 
1029 /*
1030  * Computes the aligned offset to the base tile and adjusts
1031  * x, y. bytes per pixel is assumed to be a power-of-two.
1032  *
1033  * In the 90/270 rotated case, x and y are assumed
1034  * to be already rotated to match the rotated GTT view, and
1035  * pitch is the tile_height aligned framebuffer height.
1036  *
1037  * This function is used when computing the derived information
1038  * under intel_framebuffer, so using any of that information
1039  * here is not allowed. Anything under drm_framebuffer can be
1040  * used. This is why the user has to pass in the pitch since it
1041  * is specified in the rotated orientation.
1042  */
1043 static u32 intel_compute_aligned_offset(struct drm_i915_private *i915,
1044 					int *x, int *y,
1045 					const struct drm_framebuffer *fb,
1046 					int color_plane,
1047 					unsigned int pitch,
1048 					unsigned int rotation,
1049 					unsigned int alignment)
1050 {
1051 	unsigned int cpp = fb->format->cpp[color_plane];
1052 	u32 offset, offset_aligned;
1053 
1054 	if (!is_surface_linear(fb, color_plane)) {
1055 		unsigned int tile_size, tile_width, tile_height;
1056 		unsigned int tile_rows, tiles, pitch_tiles;
1057 
1058 		tile_size = intel_tile_size(i915);
1059 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
1060 
1061 		if (drm_rotation_90_or_270(rotation)) {
1062 			pitch_tiles = pitch / tile_height;
1063 			swap(tile_width, tile_height);
1064 		} else {
1065 			pitch_tiles = pitch / (tile_width * cpp);
1066 		}
1067 
1068 		tile_rows = *y / tile_height;
1069 		*y %= tile_height;
1070 
1071 		tiles = *x / tile_width;
1072 		*x %= tile_width;
1073 
1074 		offset = (tile_rows * pitch_tiles + tiles) * tile_size;
1075 
1076 		offset_aligned = offset;
1077 		if (alignment)
1078 			offset_aligned = rounddown(offset_aligned, alignment);
1079 
1080 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
1081 					 tile_size, pitch_tiles,
1082 					 offset, offset_aligned);
1083 	} else {
1084 		offset = *y * pitch + *x * cpp;
1085 		offset_aligned = offset;
1086 		if (alignment) {
1087 			offset_aligned = rounddown(offset_aligned, alignment);
1088 			*y = (offset % alignment) / pitch;
1089 			*x = ((offset % alignment) - *y * pitch) / cpp;
1090 		} else {
1091 			*y = *x = 0;
1092 		}
1093 	}
1094 
1095 	return offset_aligned;
1096 }
1097 
1098 u32 intel_plane_compute_aligned_offset(int *x, int *y,
1099 				       const struct intel_plane_state *state,
1100 				       int color_plane)
1101 {
1102 	struct intel_plane *intel_plane = to_intel_plane(state->uapi.plane);
1103 	struct drm_i915_private *i915 = to_i915(intel_plane->base.dev);
1104 	const struct drm_framebuffer *fb = state->hw.fb;
1105 	unsigned int rotation = state->hw.rotation;
1106 	unsigned int pitch = state->view.color_plane[color_plane].mapping_stride;
1107 	unsigned int alignment;
1108 
1109 	if (intel_plane->id == PLANE_CURSOR)
1110 		alignment = intel_cursor_alignment(i915);
1111 	else
1112 		alignment = intel_surf_alignment(fb, color_plane);
1113 
1114 	return intel_compute_aligned_offset(i915, x, y, fb, color_plane,
1115 					    pitch, rotation, alignment);
1116 }
1117 
1118 /* Convert the fb->offset[] into x/y offsets */
1119 static int intel_fb_offset_to_xy(int *x, int *y,
1120 				 const struct drm_framebuffer *fb,
1121 				 int color_plane)
1122 {
1123 	struct drm_i915_private *i915 = to_i915(fb->dev);
1124 	unsigned int height, alignment, unused;
1125 
1126 	if (DISPLAY_VER(i915) >= 12 &&
1127 	    !intel_fb_needs_pot_stride_remap(to_intel_framebuffer(fb)) &&
1128 	    is_semiplanar_uv_plane(fb, color_plane))
1129 		alignment = intel_tile_row_size(fb, color_plane);
1130 	else if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
1131 		alignment = intel_tile_size(i915);
1132 	else
1133 		alignment = 0;
1134 
1135 	if (alignment != 0 && fb->offsets[color_plane] % alignment) {
1136 		drm_dbg_kms(&i915->drm,
1137 			    "Misaligned offset 0x%08x for color plane %d\n",
1138 			    fb->offsets[color_plane], color_plane);
1139 		return -EINVAL;
1140 	}
1141 
1142 	height = drm_format_info_plane_height(fb->format, fb->height, color_plane);
1143 	height = ALIGN(height, intel_tile_height(fb, color_plane));
1144 
1145 	/* Catch potential overflows early */
1146 	if (check_add_overflow(mul_u32_u32(height, fb->pitches[color_plane]),
1147 			       fb->offsets[color_plane], &unused)) {
1148 		drm_dbg_kms(&i915->drm,
1149 			    "Bad offset 0x%08x or pitch %d for color plane %d\n",
1150 			    fb->offsets[color_plane], fb->pitches[color_plane],
1151 			    color_plane);
1152 		return -ERANGE;
1153 	}
1154 
1155 	*x = 0;
1156 	*y = 0;
1157 
1158 	intel_adjust_aligned_offset(x, y,
1159 				    fb, color_plane, DRM_MODE_ROTATE_0,
1160 				    fb->pitches[color_plane],
1161 				    fb->offsets[color_plane], 0);
1162 
1163 	return 0;
1164 }
1165 
1166 static int intel_fb_check_ccs_xy(const struct drm_framebuffer *fb, int ccs_plane, int x, int y)
1167 {
1168 	struct drm_i915_private *i915 = to_i915(fb->dev);
1169 	const struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1170 	int main_plane;
1171 	int hsub, vsub;
1172 	int tile_width, tile_height;
1173 	int ccs_x, ccs_y;
1174 	int main_x, main_y;
1175 
1176 	if (!intel_fb_is_ccs_aux_plane(fb, ccs_plane))
1177 		return 0;
1178 
1179 	/*
1180 	 * While all the tile dimensions are based on a 2k or 4k GTT page size
1181 	 * here the main and CCS coordinates must match only within a (64 byte
1182 	 * on TGL+) block inside the tile.
1183 	 */
1184 	intel_tile_block_dims(fb, ccs_plane, &tile_width, &tile_height);
1185 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
1186 
1187 	tile_width *= hsub;
1188 	tile_height *= vsub;
1189 
1190 	ccs_x = (x * hsub) % tile_width;
1191 	ccs_y = (y * vsub) % tile_height;
1192 
1193 	main_plane = skl_ccs_to_main_plane(fb, ccs_plane);
1194 	main_x = intel_fb->normal_view.color_plane[main_plane].x % tile_width;
1195 	main_y = intel_fb->normal_view.color_plane[main_plane].y % tile_height;
1196 
1197 	/*
1198 	 * CCS doesn't have its own x/y offset register, so the intra CCS tile
1199 	 * x/y offsets must match between CCS and the main surface.
1200 	 */
1201 	if (main_x != ccs_x || main_y != ccs_y) {
1202 		drm_dbg_kms(&i915->drm,
1203 			      "Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
1204 			      main_x, main_y,
1205 			      ccs_x, ccs_y,
1206 			      intel_fb->normal_view.color_plane[main_plane].x,
1207 			      intel_fb->normal_view.color_plane[main_plane].y,
1208 			      x, y);
1209 		return -EINVAL;
1210 	}
1211 
1212 	return 0;
1213 }
1214 
1215 static bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
1216 {
1217 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1218 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
1219 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1220 	int i;
1221 
1222 	/* We don't want to deal with remapping with cursors */
1223 	if (plane->id == PLANE_CURSOR)
1224 		return false;
1225 
1226 	/*
1227 	 * The display engine limits already match/exceed the
1228 	 * render engine limits, so not much point in remapping.
1229 	 * Would also need to deal with the fence POT alignment
1230 	 * and gen2 2KiB GTT tile size.
1231 	 */
1232 	if (DISPLAY_VER(i915) < 4)
1233 		return false;
1234 
1235 	/*
1236 	 * The new CCS hash mode isn't compatible with remapping as
1237 	 * the virtual address of the pages affects the compressed data.
1238 	 */
1239 	if (intel_fb_is_ccs_modifier(fb->modifier))
1240 		return false;
1241 
1242 	/* Linear needs a page aligned stride for remapping */
1243 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
1244 		unsigned int alignment = intel_tile_size(i915) - 1;
1245 
1246 		for (i = 0; i < fb->format->num_planes; i++) {
1247 			if (fb->pitches[i] & alignment)
1248 				return false;
1249 		}
1250 	}
1251 
1252 	return true;
1253 }
1254 
1255 bool intel_fb_needs_pot_stride_remap(const struct intel_framebuffer *fb)
1256 {
1257 	struct drm_i915_private *i915 = to_i915(fb->base.dev);
1258 
1259 	return (IS_ALDERLAKE_P(i915) || DISPLAY_VER(i915) >= 14) &&
1260 		intel_fb_uses_dpt(&fb->base);
1261 }
1262 
1263 static int intel_fb_pitch(const struct intel_framebuffer *fb, int color_plane, unsigned int rotation)
1264 {
1265 	if (drm_rotation_90_or_270(rotation))
1266 		return fb->rotated_view.color_plane[color_plane].mapping_stride;
1267 	else if (intel_fb_needs_pot_stride_remap(fb))
1268 		return fb->remapped_view.color_plane[color_plane].mapping_stride;
1269 	else
1270 		return fb->normal_view.color_plane[color_plane].mapping_stride;
1271 }
1272 
1273 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
1274 {
1275 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1276 	const struct intel_framebuffer *fb = to_intel_framebuffer(plane_state->hw.fb);
1277 	unsigned int rotation = plane_state->hw.rotation;
1278 	u32 stride, max_stride;
1279 
1280 	/*
1281 	 * No remapping for invisible planes since we don't have
1282 	 * an actual source viewport to remap.
1283 	 */
1284 	if (!plane_state->uapi.visible)
1285 		return false;
1286 
1287 	if (!intel_plane_can_remap(plane_state))
1288 		return false;
1289 
1290 	/*
1291 	 * FIXME: aux plane limits on gen9+ are
1292 	 * unclear in Bspec, for now no checking.
1293 	 */
1294 	stride = intel_fb_pitch(fb, 0, rotation);
1295 	max_stride = plane->max_stride(plane, fb->base.format->format,
1296 				       fb->base.modifier, rotation);
1297 
1298 	return stride > max_stride;
1299 }
1300 
1301 static int convert_plane_offset_to_xy(const struct intel_framebuffer *fb, int color_plane,
1302 				      int plane_width, int *x, int *y)
1303 {
1304 	struct drm_i915_gem_object *obj = intel_fb_obj(&fb->base);
1305 	int ret;
1306 
1307 	ret = intel_fb_offset_to_xy(x, y, &fb->base, color_plane);
1308 	if (ret) {
1309 		drm_dbg_kms(fb->base.dev,
1310 			    "bad fb plane %d offset: 0x%x\n",
1311 			    color_plane, fb->base.offsets[color_plane]);
1312 		return ret;
1313 	}
1314 
1315 	ret = intel_fb_check_ccs_xy(&fb->base, color_plane, *x, *y);
1316 	if (ret)
1317 		return ret;
1318 
1319 	/*
1320 	 * The fence (if used) is aligned to the start of the object
1321 	 * so having the framebuffer wrap around across the edge of the
1322 	 * fenced region doesn't really work. We have no API to configure
1323 	 * the fence start offset within the object (nor could we probably
1324 	 * on gen2/3). So it's just easier if we just require that the
1325 	 * fb layout agrees with the fence layout. We already check that the
1326 	 * fb stride matches the fence stride elsewhere.
1327 	 */
1328 	if (color_plane == 0 && i915_gem_object_is_tiled(obj) &&
1329 	    (*x + plane_width) * fb->base.format->cpp[color_plane] > fb->base.pitches[color_plane]) {
1330 		drm_dbg_kms(fb->base.dev,
1331 			    "bad fb plane %d offset: 0x%x\n",
1332 			    color_plane, fb->base.offsets[color_plane]);
1333 		return -EINVAL;
1334 	}
1335 
1336 	return 0;
1337 }
1338 
1339 static u32 calc_plane_aligned_offset(const struct intel_framebuffer *fb, int color_plane, int *x, int *y)
1340 {
1341 	struct drm_i915_private *i915 = to_i915(fb->base.dev);
1342 	unsigned int tile_size = intel_tile_size(i915);
1343 	u32 offset;
1344 
1345 	offset = intel_compute_aligned_offset(i915, x, y, &fb->base, color_plane,
1346 					      fb->base.pitches[color_plane],
1347 					      DRM_MODE_ROTATE_0,
1348 					      tile_size);
1349 
1350 	return offset / tile_size;
1351 }
1352 
1353 struct fb_plane_view_dims {
1354 	unsigned int width, height;
1355 	unsigned int tile_width, tile_height;
1356 };
1357 
1358 static void init_plane_view_dims(const struct intel_framebuffer *fb, int color_plane,
1359 				 unsigned int width, unsigned int height,
1360 				 struct fb_plane_view_dims *dims)
1361 {
1362 	dims->width = width;
1363 	dims->height = height;
1364 
1365 	intel_tile_dims(&fb->base, color_plane, &dims->tile_width, &dims->tile_height);
1366 }
1367 
1368 static unsigned int
1369 plane_view_src_stride_tiles(const struct intel_framebuffer *fb, int color_plane,
1370 			    const struct fb_plane_view_dims *dims)
1371 {
1372 	return DIV_ROUND_UP(fb->base.pitches[color_plane],
1373 			    dims->tile_width * fb->base.format->cpp[color_plane]);
1374 }
1375 
1376 static unsigned int
1377 plane_view_dst_stride_tiles(const struct intel_framebuffer *fb, int color_plane,
1378 			    unsigned int pitch_tiles)
1379 {
1380 	if (intel_fb_needs_pot_stride_remap(fb)) {
1381 		/*
1382 		 * ADL_P, the only platform needing a POT stride has a minimum
1383 		 * of 8 main surface tiles.
1384 		 */
1385 		return roundup_pow_of_two(max(pitch_tiles, 8u));
1386 	} else {
1387 		return pitch_tiles;
1388 	}
1389 }
1390 
1391 static unsigned int
1392 plane_view_scanout_stride(const struct intel_framebuffer *fb, int color_plane,
1393 			  unsigned int tile_width,
1394 			  unsigned int src_stride_tiles, unsigned int dst_stride_tiles)
1395 {
1396 	struct drm_i915_private *i915 = to_i915(fb->base.dev);
1397 	unsigned int stride_tiles;
1398 
1399 	if ((IS_ALDERLAKE_P(i915) || DISPLAY_VER(i915) >= 14) &&
1400 	    src_stride_tiles < dst_stride_tiles)
1401 		stride_tiles = src_stride_tiles;
1402 	else
1403 		stride_tiles = dst_stride_tiles;
1404 
1405 	return stride_tiles * tile_width * fb->base.format->cpp[color_plane];
1406 }
1407 
1408 static unsigned int
1409 plane_view_width_tiles(const struct intel_framebuffer *fb, int color_plane,
1410 		       const struct fb_plane_view_dims *dims,
1411 		       int x)
1412 {
1413 	return DIV_ROUND_UP(x + dims->width, dims->tile_width);
1414 }
1415 
1416 static unsigned int
1417 plane_view_height_tiles(const struct intel_framebuffer *fb, int color_plane,
1418 			const struct fb_plane_view_dims *dims,
1419 			int y)
1420 {
1421 	return DIV_ROUND_UP(y + dims->height, dims->tile_height);
1422 }
1423 
1424 static unsigned int
1425 plane_view_linear_tiles(const struct intel_framebuffer *fb, int color_plane,
1426 			const struct fb_plane_view_dims *dims,
1427 			int x, int y)
1428 {
1429 	struct drm_i915_private *i915 = to_i915(fb->base.dev);
1430 	unsigned int size;
1431 
1432 	size = (y + dims->height) * fb->base.pitches[color_plane] +
1433 		x * fb->base.format->cpp[color_plane];
1434 
1435 	return DIV_ROUND_UP(size, intel_tile_size(i915));
1436 }
1437 
1438 #define assign_chk_ovf(i915, var, val) ({ \
1439 	drm_WARN_ON(&(i915)->drm, overflows_type(val, var)); \
1440 	(var) = (val); \
1441 })
1442 
1443 #define assign_bfld_chk_ovf(i915, var, val) ({ \
1444 	(var) = (val); \
1445 	drm_WARN_ON(&(i915)->drm, (var) != (val)); \
1446 	(var); \
1447 })
1448 
1449 static u32 calc_plane_remap_info(const struct intel_framebuffer *fb, int color_plane,
1450 				 const struct fb_plane_view_dims *dims,
1451 				 u32 obj_offset, u32 gtt_offset, int x, int y,
1452 				 struct intel_fb_view *view)
1453 {
1454 	struct drm_i915_private *i915 = to_i915(fb->base.dev);
1455 	struct intel_remapped_plane_info *remap_info = &view->gtt.remapped.plane[color_plane];
1456 	struct i915_color_plane_view *color_plane_info = &view->color_plane[color_plane];
1457 	unsigned int tile_width = dims->tile_width;
1458 	unsigned int tile_height = dims->tile_height;
1459 	unsigned int tile_size = intel_tile_size(i915);
1460 	struct drm_rect r;
1461 	u32 size = 0;
1462 
1463 	assign_bfld_chk_ovf(i915, remap_info->offset, obj_offset);
1464 
1465 	if (intel_fb_is_gen12_ccs_aux_plane(&fb->base, color_plane)) {
1466 		remap_info->linear = 1;
1467 
1468 		assign_chk_ovf(i915, remap_info->size,
1469 			       plane_view_linear_tiles(fb, color_plane, dims, x, y));
1470 	} else {
1471 		remap_info->linear = 0;
1472 
1473 		assign_chk_ovf(i915, remap_info->src_stride,
1474 			       plane_view_src_stride_tiles(fb, color_plane, dims));
1475 		assign_chk_ovf(i915, remap_info->width,
1476 			       plane_view_width_tiles(fb, color_plane, dims, x));
1477 		assign_chk_ovf(i915, remap_info->height,
1478 			       plane_view_height_tiles(fb, color_plane, dims, y));
1479 	}
1480 
1481 	if (view->gtt.type == I915_GTT_VIEW_ROTATED) {
1482 		drm_WARN_ON(&i915->drm, remap_info->linear);
1483 		check_array_bounds(i915, view->gtt.rotated.plane, color_plane);
1484 
1485 		assign_chk_ovf(i915, remap_info->dst_stride,
1486 			       plane_view_dst_stride_tiles(fb, color_plane, remap_info->height));
1487 
1488 		/* rotate the x/y offsets to match the GTT view */
1489 		drm_rect_init(&r, x, y, dims->width, dims->height);
1490 		drm_rect_rotate(&r,
1491 				remap_info->width * tile_width,
1492 				remap_info->height * tile_height,
1493 				DRM_MODE_ROTATE_270);
1494 
1495 		color_plane_info->x = r.x1;
1496 		color_plane_info->y = r.y1;
1497 
1498 		color_plane_info->mapping_stride = remap_info->dst_stride * tile_height;
1499 		color_plane_info->scanout_stride = color_plane_info->mapping_stride;
1500 
1501 		size += remap_info->dst_stride * remap_info->width;
1502 
1503 		/* rotate the tile dimensions to match the GTT view */
1504 		swap(tile_width, tile_height);
1505 	} else {
1506 		drm_WARN_ON(&i915->drm, view->gtt.type != I915_GTT_VIEW_REMAPPED);
1507 
1508 		check_array_bounds(i915, view->gtt.remapped.plane, color_plane);
1509 
1510 		if (view->gtt.remapped.plane_alignment) {
1511 			u32 aligned_offset = ALIGN(gtt_offset,
1512 						   view->gtt.remapped.plane_alignment);
1513 
1514 			size += aligned_offset - gtt_offset;
1515 			gtt_offset = aligned_offset;
1516 		}
1517 
1518 		color_plane_info->x = x;
1519 		color_plane_info->y = y;
1520 
1521 		if (remap_info->linear) {
1522 			color_plane_info->mapping_stride = fb->base.pitches[color_plane];
1523 			color_plane_info->scanout_stride = color_plane_info->mapping_stride;
1524 
1525 			size += remap_info->size;
1526 		} else {
1527 			unsigned int dst_stride;
1528 
1529 			/*
1530 			 * The hardware automagically calculates the CCS AUX surface
1531 			 * stride from the main surface stride so can't really remap a
1532 			 * smaller subset (unless we'd remap in whole AUX page units).
1533 			 */
1534 			if (intel_fb_needs_pot_stride_remap(fb) &&
1535 			    intel_fb_is_ccs_modifier(fb->base.modifier))
1536 				dst_stride = remap_info->src_stride;
1537 			else
1538 				dst_stride = remap_info->width;
1539 
1540 			dst_stride = plane_view_dst_stride_tiles(fb, color_plane, dst_stride);
1541 
1542 			assign_chk_ovf(i915, remap_info->dst_stride, dst_stride);
1543 			color_plane_info->mapping_stride = dst_stride *
1544 							   tile_width *
1545 							   fb->base.format->cpp[color_plane];
1546 			color_plane_info->scanout_stride =
1547 				plane_view_scanout_stride(fb, color_plane, tile_width,
1548 							  remap_info->src_stride,
1549 							  dst_stride);
1550 
1551 			size += dst_stride * remap_info->height;
1552 		}
1553 	}
1554 
1555 	/*
1556 	 * We only keep the x/y offsets, so push all of the gtt offset into
1557 	 * the x/y offsets.  x,y will hold the first pixel of the framebuffer
1558 	 * plane from the start of the remapped/rotated gtt mapping.
1559 	 */
1560 	if (remap_info->linear)
1561 		intel_adjust_linear_offset(&color_plane_info->x, &color_plane_info->y,
1562 					   fb->base.format->cpp[color_plane],
1563 					   color_plane_info->mapping_stride,
1564 					   gtt_offset * tile_size, 0);
1565 	else
1566 		intel_adjust_tile_offset(&color_plane_info->x, &color_plane_info->y,
1567 					 tile_width, tile_height,
1568 					 tile_size, remap_info->dst_stride,
1569 					 gtt_offset * tile_size, 0);
1570 
1571 	return size;
1572 }
1573 
1574 #undef assign_chk_ovf
1575 
1576 /* Return number of tiles @color_plane needs. */
1577 static unsigned int
1578 calc_plane_normal_size(const struct intel_framebuffer *fb, int color_plane,
1579 		       const struct fb_plane_view_dims *dims,
1580 		       int x, int y)
1581 {
1582 	unsigned int tiles;
1583 
1584 	if (is_surface_linear(&fb->base, color_plane)) {
1585 		tiles = plane_view_linear_tiles(fb, color_plane, dims, x, y);
1586 	} else {
1587 		tiles = plane_view_src_stride_tiles(fb, color_plane, dims) *
1588 			plane_view_height_tiles(fb, color_plane, dims, y);
1589 		/*
1590 		 * If the plane isn't horizontally tile aligned,
1591 		 * we need one more tile.
1592 		 */
1593 		if (x != 0)
1594 			tiles++;
1595 	}
1596 
1597 	return tiles;
1598 }
1599 
1600 static void intel_fb_view_init(struct drm_i915_private *i915, struct intel_fb_view *view,
1601 			       enum i915_gtt_view_type view_type)
1602 {
1603 	memset(view, 0, sizeof(*view));
1604 	view->gtt.type = view_type;
1605 
1606 	if (view_type == I915_GTT_VIEW_REMAPPED &&
1607 	    (IS_ALDERLAKE_P(i915) || DISPLAY_VER(i915) >= 14))
1608 		view->gtt.remapped.plane_alignment = SZ_2M / PAGE_SIZE;
1609 }
1610 
1611 bool intel_fb_supports_90_270_rotation(const struct intel_framebuffer *fb)
1612 {
1613 	if (DISPLAY_VER(to_i915(fb->base.dev)) >= 13)
1614 		return false;
1615 
1616 	return fb->base.modifier == I915_FORMAT_MOD_Y_TILED ||
1617 	       fb->base.modifier == I915_FORMAT_MOD_Yf_TILED;
1618 }
1619 
1620 int intel_fill_fb_info(struct drm_i915_private *i915, struct intel_framebuffer *fb)
1621 {
1622 	struct drm_i915_gem_object *obj = intel_fb_obj(&fb->base);
1623 	u32 gtt_offset_rotated = 0;
1624 	u32 gtt_offset_remapped = 0;
1625 	unsigned int max_size = 0;
1626 	int i, num_planes = fb->base.format->num_planes;
1627 	unsigned int tile_size = intel_tile_size(i915);
1628 
1629 	intel_fb_view_init(i915, &fb->normal_view, I915_GTT_VIEW_NORMAL);
1630 
1631 	drm_WARN_ON(&i915->drm,
1632 		    intel_fb_supports_90_270_rotation(fb) &&
1633 		    intel_fb_needs_pot_stride_remap(fb));
1634 
1635 	if (intel_fb_supports_90_270_rotation(fb))
1636 		intel_fb_view_init(i915, &fb->rotated_view, I915_GTT_VIEW_ROTATED);
1637 	if (intel_fb_needs_pot_stride_remap(fb))
1638 		intel_fb_view_init(i915, &fb->remapped_view, I915_GTT_VIEW_REMAPPED);
1639 
1640 	for (i = 0; i < num_planes; i++) {
1641 		struct fb_plane_view_dims view_dims;
1642 		unsigned int width, height;
1643 		unsigned int size;
1644 		u32 offset;
1645 		int x, y;
1646 		int ret;
1647 
1648 		/*
1649 		 * Plane 2 of Render Compression with Clear Color fb modifier
1650 		 * is consumed by the driver and not passed to DE. Skip the
1651 		 * arithmetic related to alignment and offset calculation.
1652 		 */
1653 		if (is_gen12_ccs_cc_plane(&fb->base, i)) {
1654 			if (IS_ALIGNED(fb->base.offsets[i], PAGE_SIZE))
1655 				continue;
1656 			else
1657 				return -EINVAL;
1658 		}
1659 
1660 		intel_fb_plane_dims(fb, i, &width, &height);
1661 
1662 		ret = convert_plane_offset_to_xy(fb, i, width, &x, &y);
1663 		if (ret)
1664 			return ret;
1665 
1666 		init_plane_view_dims(fb, i, width, height, &view_dims);
1667 
1668 		/*
1669 		 * First pixel of the framebuffer from
1670 		 * the start of the normal gtt mapping.
1671 		 */
1672 		fb->normal_view.color_plane[i].x = x;
1673 		fb->normal_view.color_plane[i].y = y;
1674 		fb->normal_view.color_plane[i].mapping_stride = fb->base.pitches[i];
1675 		fb->normal_view.color_plane[i].scanout_stride =
1676 			fb->normal_view.color_plane[i].mapping_stride;
1677 
1678 		offset = calc_plane_aligned_offset(fb, i, &x, &y);
1679 
1680 		if (intel_fb_supports_90_270_rotation(fb))
1681 			gtt_offset_rotated += calc_plane_remap_info(fb, i, &view_dims,
1682 								    offset, gtt_offset_rotated, x, y,
1683 								    &fb->rotated_view);
1684 
1685 		if (intel_fb_needs_pot_stride_remap(fb))
1686 			gtt_offset_remapped += calc_plane_remap_info(fb, i, &view_dims,
1687 								     offset, gtt_offset_remapped, x, y,
1688 								     &fb->remapped_view);
1689 
1690 		size = calc_plane_normal_size(fb, i, &view_dims, x, y);
1691 		/* how many tiles in total needed in the bo */
1692 		max_size = max(max_size, offset + size);
1693 	}
1694 
1695 	if (mul_u32_u32(max_size, tile_size) > intel_bo_to_drm_bo(obj)->size) {
1696 		drm_dbg_kms(&i915->drm,
1697 			    "fb too big for bo (need %llu bytes, have %zu bytes)\n",
1698 			    mul_u32_u32(max_size, tile_size), intel_bo_to_drm_bo(obj)->size);
1699 		return -EINVAL;
1700 	}
1701 
1702 	return 0;
1703 }
1704 
1705 static void intel_plane_remap_gtt(struct intel_plane_state *plane_state)
1706 {
1707 	struct drm_i915_private *i915 =
1708 		to_i915(plane_state->uapi.plane->dev);
1709 	struct drm_framebuffer *fb = plane_state->hw.fb;
1710 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1711 	unsigned int rotation = plane_state->hw.rotation;
1712 	int i, num_planes = fb->format->num_planes;
1713 	unsigned int src_x, src_y;
1714 	unsigned int src_w, src_h;
1715 	u32 gtt_offset = 0;
1716 
1717 	intel_fb_view_init(i915, &plane_state->view,
1718 			   drm_rotation_90_or_270(rotation) ? I915_GTT_VIEW_ROTATED :
1719 							      I915_GTT_VIEW_REMAPPED);
1720 
1721 	src_x = plane_state->uapi.src.x1 >> 16;
1722 	src_y = plane_state->uapi.src.y1 >> 16;
1723 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
1724 	src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
1725 
1726 	drm_WARN_ON(&i915->drm, intel_fb_is_ccs_modifier(fb->modifier));
1727 
1728 	/* Make src coordinates relative to the viewport */
1729 	drm_rect_translate(&plane_state->uapi.src,
1730 			   -(src_x << 16), -(src_y << 16));
1731 
1732 	/* Rotate src coordinates to match rotated GTT view */
1733 	if (drm_rotation_90_or_270(rotation))
1734 		drm_rect_rotate(&plane_state->uapi.src,
1735 				src_w << 16, src_h << 16,
1736 				DRM_MODE_ROTATE_270);
1737 
1738 	for (i = 0; i < num_planes; i++) {
1739 		unsigned int hsub = i ? fb->format->hsub : 1;
1740 		unsigned int vsub = i ? fb->format->vsub : 1;
1741 		struct fb_plane_view_dims view_dims;
1742 		unsigned int width, height;
1743 		unsigned int x, y;
1744 		u32 offset;
1745 
1746 		x = src_x / hsub;
1747 		y = src_y / vsub;
1748 		width = src_w / hsub;
1749 		height = src_h / vsub;
1750 
1751 		init_plane_view_dims(intel_fb, i, width, height, &view_dims);
1752 
1753 		/*
1754 		 * First pixel of the src viewport from the
1755 		 * start of the normal gtt mapping.
1756 		 */
1757 		x += intel_fb->normal_view.color_plane[i].x;
1758 		y += intel_fb->normal_view.color_plane[i].y;
1759 
1760 		offset = calc_plane_aligned_offset(intel_fb, i, &x, &y);
1761 
1762 		gtt_offset += calc_plane_remap_info(intel_fb, i, &view_dims,
1763 						    offset, gtt_offset, x, y,
1764 						    &plane_state->view);
1765 	}
1766 }
1767 
1768 void intel_fb_fill_view(const struct intel_framebuffer *fb, unsigned int rotation,
1769 			struct intel_fb_view *view)
1770 {
1771 	if (drm_rotation_90_or_270(rotation))
1772 		*view = fb->rotated_view;
1773 	else if (intel_fb_needs_pot_stride_remap(fb))
1774 		*view = fb->remapped_view;
1775 	else
1776 		*view = fb->normal_view;
1777 }
1778 
1779 static
1780 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
1781 			u32 pixel_format, u64 modifier)
1782 {
1783 	/*
1784 	 * Arbitrary limit for gen4+ chosen to match the
1785 	 * render engine max stride.
1786 	 *
1787 	 * The new CCS hash mode makes remapping impossible
1788 	 */
1789 	if (DISPLAY_VER(dev_priv) < 4 || intel_fb_is_ccs_modifier(modifier) ||
1790 	    intel_fb_modifier_uses_dpt(dev_priv, modifier))
1791 		return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
1792 	else if (DISPLAY_VER(dev_priv) >= 7)
1793 		return 256 * 1024;
1794 	else
1795 		return 128 * 1024;
1796 }
1797 
1798 static unsigned int
1799 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
1800 {
1801 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
1802 	unsigned int tile_width;
1803 
1804 	if (is_surface_linear(fb, color_plane)) {
1805 		unsigned int max_stride = intel_plane_fb_max_stride(dev_priv,
1806 								    fb->format->format,
1807 								    fb->modifier);
1808 
1809 		/*
1810 		 * To make remapping with linear generally feasible
1811 		 * we need the stride to be page aligned.
1812 		 */
1813 		if (fb->pitches[color_plane] > max_stride &&
1814 		    !intel_fb_is_ccs_modifier(fb->modifier))
1815 			return intel_tile_size(dev_priv);
1816 		else
1817 			return 64;
1818 	}
1819 
1820 	tile_width = intel_tile_width_bytes(fb, color_plane);
1821 	if (intel_fb_is_ccs_modifier(fb->modifier)) {
1822 		/*
1823 		 * On TGL the surface stride must be 4 tile aligned, mapped by
1824 		 * one 64 byte cacheline on the CCS AUX surface.
1825 		 */
1826 		if (DISPLAY_VER(dev_priv) >= 12)
1827 			tile_width *= 4;
1828 		/*
1829 		 * Display WA #0531: skl,bxt,kbl,glk
1830 		 *
1831 		 * Render decompression and plane width > 3840
1832 		 * combined with horizontal panning requires the
1833 		 * plane stride to be a multiple of 4. We'll just
1834 		 * require the entire fb to accommodate that to avoid
1835 		 * potential runtime errors at plane configuration time.
1836 		 */
1837 		else if ((DISPLAY_VER(dev_priv) == 9 || IS_GEMINILAKE(dev_priv)) &&
1838 			 color_plane == 0 && fb->width > 3840)
1839 			tile_width *= 4;
1840 	}
1841 	return tile_width;
1842 }
1843 
1844 static int intel_plane_check_stride(const struct intel_plane_state *plane_state)
1845 {
1846 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1847 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1848 	unsigned int rotation = plane_state->hw.rotation;
1849 	u32 stride, max_stride;
1850 
1851 	/*
1852 	 * We ignore stride for all invisible planes that
1853 	 * can be remapped. Otherwise we could end up
1854 	 * with a false positive when the remapping didn't
1855 	 * kick in due the plane being invisible.
1856 	 */
1857 	if (intel_plane_can_remap(plane_state) &&
1858 	    !plane_state->uapi.visible)
1859 		return 0;
1860 
1861 	/* FIXME other color planes? */
1862 	stride = plane_state->view.color_plane[0].mapping_stride;
1863 	max_stride = plane->max_stride(plane, fb->format->format,
1864 				       fb->modifier, rotation);
1865 
1866 	if (stride > max_stride) {
1867 		drm_dbg_kms(plane->base.dev,
1868 			    "[FB:%d] stride (%d) exceeds [PLANE:%d:%s] max stride (%d)\n",
1869 			    fb->base.id, stride,
1870 			    plane->base.base.id, plane->base.name, max_stride);
1871 		return -EINVAL;
1872 	}
1873 
1874 	return 0;
1875 }
1876 
1877 int intel_plane_compute_gtt(struct intel_plane_state *plane_state)
1878 {
1879 	const struct intel_framebuffer *fb =
1880 		to_intel_framebuffer(plane_state->hw.fb);
1881 	unsigned int rotation = plane_state->hw.rotation;
1882 
1883 	if (!fb)
1884 		return 0;
1885 
1886 	if (intel_plane_needs_remap(plane_state)) {
1887 		intel_plane_remap_gtt(plane_state);
1888 
1889 		/*
1890 		 * Sometimes even remapping can't overcome
1891 		 * the stride limitations :( Can happen with
1892 		 * big plane sizes and suitably misaligned
1893 		 * offsets.
1894 		 */
1895 		return intel_plane_check_stride(plane_state);
1896 	}
1897 
1898 	intel_fb_fill_view(fb, rotation, &plane_state->view);
1899 
1900 	/* Rotate src coordinates to match rotated GTT view */
1901 	if (drm_rotation_90_or_270(rotation))
1902 		drm_rect_rotate(&plane_state->uapi.src,
1903 				fb->base.width << 16, fb->base.height << 16,
1904 				DRM_MODE_ROTATE_270);
1905 
1906 	return intel_plane_check_stride(plane_state);
1907 }
1908 
1909 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
1910 {
1911 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1912 
1913 	drm_framebuffer_cleanup(fb);
1914 
1915 	if (intel_fb_uses_dpt(fb))
1916 		intel_dpt_destroy(intel_fb->dpt_vm);
1917 
1918 	intel_frontbuffer_put(intel_fb->frontbuffer);
1919 
1920 	intel_fb_bo_framebuffer_fini(intel_fb_obj(fb));
1921 
1922 	kfree(intel_fb);
1923 }
1924 
1925 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1926 						struct drm_file *file,
1927 						unsigned int *handle)
1928 {
1929 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1930 	struct drm_i915_private *i915 = to_i915(intel_bo_to_drm_bo(obj)->dev);
1931 
1932 	if (i915_gem_object_is_userptr(obj)) {
1933 		drm_dbg(&i915->drm,
1934 			"attempting to use a userptr for a framebuffer, denied\n");
1935 		return -EINVAL;
1936 	}
1937 
1938 	return drm_gem_handle_create(file, intel_bo_to_drm_bo(obj), handle);
1939 }
1940 
1941 struct frontbuffer_fence_cb {
1942 	struct dma_fence_cb base;
1943 	struct intel_frontbuffer *front;
1944 };
1945 
1946 static void intel_user_framebuffer_fence_wake(struct dma_fence *dma,
1947 					      struct dma_fence_cb *data)
1948 {
1949 	struct frontbuffer_fence_cb *cb = container_of(data, typeof(*cb), base);
1950 
1951 	intel_frontbuffer_queue_flush(cb->front);
1952 	kfree(cb);
1953 	dma_fence_put(dma);
1954 }
1955 
1956 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
1957 					struct drm_file *file,
1958 					unsigned int flags, unsigned int color,
1959 					struct drm_clip_rect *clips,
1960 					unsigned int num_clips)
1961 {
1962 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1963 	struct intel_frontbuffer *front = to_intel_frontbuffer(fb);
1964 	struct dma_fence *fence;
1965 	struct frontbuffer_fence_cb *cb;
1966 	int ret = 0;
1967 
1968 	if (!atomic_read(&front->bits))
1969 		return 0;
1970 
1971 	if (dma_resv_test_signaled(intel_bo_to_drm_bo(obj)->resv, dma_resv_usage_rw(false)))
1972 		goto flush;
1973 
1974 	ret = dma_resv_get_singleton(intel_bo_to_drm_bo(obj)->resv, dma_resv_usage_rw(false),
1975 				     &fence);
1976 	if (ret || !fence)
1977 		goto flush;
1978 
1979 	cb = kmalloc(sizeof(*cb), GFP_KERNEL);
1980 	if (!cb) {
1981 		dma_fence_put(fence);
1982 		ret = -ENOMEM;
1983 		goto flush;
1984 	}
1985 
1986 	cb->front = front;
1987 
1988 	intel_frontbuffer_invalidate(front, ORIGIN_DIRTYFB);
1989 
1990 	ret = dma_fence_add_callback(fence, &cb->base,
1991 				     intel_user_framebuffer_fence_wake);
1992 	if (ret) {
1993 		intel_user_framebuffer_fence_wake(fence, &cb->base);
1994 		if (ret == -ENOENT)
1995 			ret = 0;
1996 	}
1997 
1998 	return ret;
1999 
2000 flush:
2001 	i915_gem_object_flush_if_display(obj);
2002 	intel_frontbuffer_flush(front, ORIGIN_DIRTYFB);
2003 	return ret;
2004 }
2005 
2006 static const struct drm_framebuffer_funcs intel_fb_funcs = {
2007 	.destroy = intel_user_framebuffer_destroy,
2008 	.create_handle = intel_user_framebuffer_create_handle,
2009 	.dirty = intel_user_framebuffer_dirty,
2010 };
2011 
2012 int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
2013 			   struct drm_i915_gem_object *obj,
2014 			   struct drm_mode_fb_cmd2 *mode_cmd)
2015 {
2016 	struct drm_i915_private *dev_priv = to_i915(intel_bo_to_drm_bo(obj)->dev);
2017 	struct drm_framebuffer *fb = &intel_fb->base;
2018 	u32 max_stride;
2019 	int ret = -EINVAL;
2020 	int i;
2021 
2022 	ret = intel_fb_bo_framebuffer_init(intel_fb, obj, mode_cmd);
2023 	if (ret)
2024 		return ret;
2025 
2026 	intel_fb->frontbuffer = intel_frontbuffer_get(obj);
2027 	if (!intel_fb->frontbuffer) {
2028 		ret = -ENOMEM;
2029 		goto err;
2030 	}
2031 
2032 	ret = -EINVAL;
2033 	if (!drm_any_plane_has_format(&dev_priv->drm,
2034 				      mode_cmd->pixel_format,
2035 				      mode_cmd->modifier[0])) {
2036 		drm_dbg_kms(&dev_priv->drm,
2037 			    "unsupported pixel format %p4cc / modifier 0x%llx\n",
2038 			    &mode_cmd->pixel_format, mode_cmd->modifier[0]);
2039 		goto err_frontbuffer_put;
2040 	}
2041 
2042 	max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
2043 					 mode_cmd->modifier[0]);
2044 	if (mode_cmd->pitches[0] > max_stride) {
2045 		drm_dbg_kms(&dev_priv->drm,
2046 			    "%s pitch (%u) must be at most %d\n",
2047 			    mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
2048 			    "tiled" : "linear",
2049 			    mode_cmd->pitches[0], max_stride);
2050 		goto err_frontbuffer_put;
2051 	}
2052 
2053 	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
2054 	if (mode_cmd->offsets[0] != 0) {
2055 		drm_dbg_kms(&dev_priv->drm,
2056 			    "plane 0 offset (0x%08x) must be 0\n",
2057 			    mode_cmd->offsets[0]);
2058 		goto err_frontbuffer_put;
2059 	}
2060 
2061 	drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
2062 
2063 	for (i = 0; i < fb->format->num_planes; i++) {
2064 		unsigned int stride_alignment;
2065 
2066 		if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
2067 			drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
2068 				    i);
2069 			goto err_frontbuffer_put;
2070 		}
2071 
2072 		stride_alignment = intel_fb_stride_alignment(fb, i);
2073 		if (fb->pitches[i] & (stride_alignment - 1)) {
2074 			drm_dbg_kms(&dev_priv->drm,
2075 				    "plane %d pitch (%d) must be at least %u byte aligned\n",
2076 				    i, fb->pitches[i], stride_alignment);
2077 			goto err_frontbuffer_put;
2078 		}
2079 
2080 		if (intel_fb_is_gen12_ccs_aux_plane(fb, i)) {
2081 			unsigned int ccs_aux_stride = gen12_ccs_aux_stride(intel_fb, i);
2082 
2083 			if (fb->pitches[i] != ccs_aux_stride) {
2084 				drm_dbg_kms(&dev_priv->drm,
2085 					    "ccs aux plane %d pitch (%d) must be %d\n",
2086 					    i,
2087 					    fb->pitches[i], ccs_aux_stride);
2088 				goto err_frontbuffer_put;
2089 			}
2090 		}
2091 
2092 		fb->obj[i] = intel_bo_to_drm_bo(obj);
2093 	}
2094 
2095 	ret = intel_fill_fb_info(dev_priv, intel_fb);
2096 	if (ret)
2097 		goto err_frontbuffer_put;
2098 
2099 	if (intel_fb_uses_dpt(fb)) {
2100 		struct i915_address_space *vm;
2101 
2102 		vm = intel_dpt_create(intel_fb);
2103 		if (IS_ERR(vm)) {
2104 			drm_dbg_kms(&dev_priv->drm, "failed to create DPT\n");
2105 			ret = PTR_ERR(vm);
2106 			goto err_frontbuffer_put;
2107 		}
2108 
2109 		intel_fb->dpt_vm = vm;
2110 	}
2111 
2112 	ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
2113 	if (ret) {
2114 		drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
2115 		goto err_free_dpt;
2116 	}
2117 
2118 	return 0;
2119 
2120 err_free_dpt:
2121 	if (intel_fb_uses_dpt(fb))
2122 		intel_dpt_destroy(intel_fb->dpt_vm);
2123 err_frontbuffer_put:
2124 	intel_frontbuffer_put(intel_fb->frontbuffer);
2125 err:
2126 	intel_fb_bo_framebuffer_fini(obj);
2127 	return ret;
2128 }
2129 
2130 struct drm_framebuffer *
2131 intel_user_framebuffer_create(struct drm_device *dev,
2132 			      struct drm_file *filp,
2133 			      const struct drm_mode_fb_cmd2 *user_mode_cmd)
2134 {
2135 	struct drm_framebuffer *fb;
2136 	struct drm_i915_gem_object *obj;
2137 	struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
2138 	struct drm_i915_private *i915 = to_i915(dev);
2139 
2140 	obj = intel_fb_bo_lookup_valid_bo(i915, filp, &mode_cmd);
2141 	if (IS_ERR(obj))
2142 		return ERR_CAST(obj);
2143 
2144 	fb = intel_framebuffer_create(obj, &mode_cmd);
2145 	drm_gem_object_put(intel_bo_to_drm_bo(obj));
2146 
2147 	return fb;
2148 }
2149 
2150 struct drm_framebuffer *
2151 intel_framebuffer_create(struct drm_i915_gem_object *obj,
2152 			 struct drm_mode_fb_cmd2 *mode_cmd)
2153 {
2154 	struct intel_framebuffer *intel_fb;
2155 	int ret;
2156 
2157 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
2158 	if (!intel_fb)
2159 		return ERR_PTR(-ENOMEM);
2160 
2161 	ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
2162 	if (ret)
2163 		goto err;
2164 
2165 	return &intel_fb->base;
2166 
2167 err:
2168 	kfree(intel_fb);
2169 	return ERR_PTR(ret);
2170 }
2171