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