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) && intel_fb_uses_dpt(&fb->base);
1194 }
1195 
1196 static int intel_fb_pitch(const struct intel_framebuffer *fb, int color_plane, unsigned int rotation)
1197 {
1198 	if (drm_rotation_90_or_270(rotation))
1199 		return fb->rotated_view.color_plane[color_plane].mapping_stride;
1200 	else if (intel_fb_needs_pot_stride_remap(fb))
1201 		return fb->remapped_view.color_plane[color_plane].mapping_stride;
1202 	else
1203 		return fb->normal_view.color_plane[color_plane].mapping_stride;
1204 }
1205 
1206 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
1207 {
1208 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1209 	const struct intel_framebuffer *fb = to_intel_framebuffer(plane_state->hw.fb);
1210 	unsigned int rotation = plane_state->hw.rotation;
1211 	u32 stride, max_stride;
1212 
1213 	/*
1214 	 * No remapping for invisible planes since we don't have
1215 	 * an actual source viewport to remap.
1216 	 */
1217 	if (!plane_state->uapi.visible)
1218 		return false;
1219 
1220 	if (!intel_plane_can_remap(plane_state))
1221 		return false;
1222 
1223 	/*
1224 	 * FIXME: aux plane limits on gen9+ are
1225 	 * unclear in Bspec, for now no checking.
1226 	 */
1227 	stride = intel_fb_pitch(fb, 0, rotation);
1228 	max_stride = plane->max_stride(plane, fb->base.format->format,
1229 				       fb->base.modifier, rotation);
1230 
1231 	return stride > max_stride;
1232 }
1233 
1234 static int convert_plane_offset_to_xy(const struct intel_framebuffer *fb, int color_plane,
1235 				      int plane_width, int *x, int *y)
1236 {
1237 	struct drm_i915_gem_object *obj = intel_fb_obj(&fb->base);
1238 	int ret;
1239 
1240 	ret = intel_fb_offset_to_xy(x, y, &fb->base, color_plane);
1241 	if (ret) {
1242 		drm_dbg_kms(fb->base.dev,
1243 			    "bad fb plane %d offset: 0x%x\n",
1244 			    color_plane, fb->base.offsets[color_plane]);
1245 		return ret;
1246 	}
1247 
1248 	ret = intel_fb_check_ccs_xy(&fb->base, color_plane, *x, *y);
1249 	if (ret)
1250 		return ret;
1251 
1252 	/*
1253 	 * The fence (if used) is aligned to the start of the object
1254 	 * so having the framebuffer wrap around across the edge of the
1255 	 * fenced region doesn't really work. We have no API to configure
1256 	 * the fence start offset within the object (nor could we probably
1257 	 * on gen2/3). So it's just easier if we just require that the
1258 	 * fb layout agrees with the fence layout. We already check that the
1259 	 * fb stride matches the fence stride elsewhere.
1260 	 */
1261 	if (color_plane == 0 && i915_gem_object_is_tiled(obj) &&
1262 	    (*x + plane_width) * fb->base.format->cpp[color_plane] > fb->base.pitches[color_plane]) {
1263 		drm_dbg_kms(fb->base.dev,
1264 			    "bad fb plane %d offset: 0x%x\n",
1265 			    color_plane, fb->base.offsets[color_plane]);
1266 		return -EINVAL;
1267 	}
1268 
1269 	return 0;
1270 }
1271 
1272 static u32 calc_plane_aligned_offset(const struct intel_framebuffer *fb, int color_plane, int *x, int *y)
1273 {
1274 	struct drm_i915_private *i915 = to_i915(fb->base.dev);
1275 	unsigned int tile_size = intel_tile_size(i915);
1276 	u32 offset;
1277 
1278 	offset = intel_compute_aligned_offset(i915, x, y, &fb->base, color_plane,
1279 					      fb->base.pitches[color_plane],
1280 					      DRM_MODE_ROTATE_0,
1281 					      tile_size);
1282 
1283 	return offset / tile_size;
1284 }
1285 
1286 struct fb_plane_view_dims {
1287 	unsigned int width, height;
1288 	unsigned int tile_width, tile_height;
1289 };
1290 
1291 static void init_plane_view_dims(const struct intel_framebuffer *fb, int color_plane,
1292 				 unsigned int width, unsigned int height,
1293 				 struct fb_plane_view_dims *dims)
1294 {
1295 	dims->width = width;
1296 	dims->height = height;
1297 
1298 	intel_tile_dims(&fb->base, color_plane, &dims->tile_width, &dims->tile_height);
1299 }
1300 
1301 static unsigned int
1302 plane_view_src_stride_tiles(const struct intel_framebuffer *fb, int color_plane,
1303 			    const struct fb_plane_view_dims *dims)
1304 {
1305 	return DIV_ROUND_UP(fb->base.pitches[color_plane],
1306 			    dims->tile_width * fb->base.format->cpp[color_plane]);
1307 }
1308 
1309 static unsigned int
1310 plane_view_dst_stride_tiles(const struct intel_framebuffer *fb, int color_plane,
1311 			    unsigned int pitch_tiles)
1312 {
1313 	if (intel_fb_needs_pot_stride_remap(fb)) {
1314 		/*
1315 		 * ADL_P, the only platform needing a POT stride has a minimum
1316 		 * of 8 main surface tiles.
1317 		 */
1318 		return roundup_pow_of_two(max(pitch_tiles, 8u));
1319 	} else {
1320 		return pitch_tiles;
1321 	}
1322 }
1323 
1324 static unsigned int
1325 plane_view_scanout_stride(const struct intel_framebuffer *fb, int color_plane,
1326 			  unsigned int tile_width,
1327 			  unsigned int src_stride_tiles, unsigned int dst_stride_tiles)
1328 {
1329 	unsigned int stride_tiles;
1330 
1331 	if (IS_ALDERLAKE_P(to_i915(fb->base.dev)))
1332 		stride_tiles = src_stride_tiles;
1333 	else
1334 		stride_tiles = dst_stride_tiles;
1335 
1336 	return stride_tiles * tile_width * fb->base.format->cpp[color_plane];
1337 }
1338 
1339 static unsigned int
1340 plane_view_width_tiles(const struct intel_framebuffer *fb, int color_plane,
1341 		       const struct fb_plane_view_dims *dims,
1342 		       int x)
1343 {
1344 	return DIV_ROUND_UP(x + dims->width, dims->tile_width);
1345 }
1346 
1347 static unsigned int
1348 plane_view_height_tiles(const struct intel_framebuffer *fb, int color_plane,
1349 			const struct fb_plane_view_dims *dims,
1350 			int y)
1351 {
1352 	return DIV_ROUND_UP(y + dims->height, dims->tile_height);
1353 }
1354 
1355 static unsigned int
1356 plane_view_linear_tiles(const struct intel_framebuffer *fb, int color_plane,
1357 			const struct fb_plane_view_dims *dims,
1358 			int x, int y)
1359 {
1360 	struct drm_i915_private *i915 = to_i915(fb->base.dev);
1361 	unsigned int size;
1362 
1363 	size = (y + dims->height) * fb->base.pitches[color_plane] +
1364 		x * fb->base.format->cpp[color_plane];
1365 
1366 	return DIV_ROUND_UP(size, intel_tile_size(i915));
1367 }
1368 
1369 #define assign_chk_ovf(i915, var, val) ({ \
1370 	drm_WARN_ON(&(i915)->drm, overflows_type(val, var)); \
1371 	(var) = (val); \
1372 })
1373 
1374 #define assign_bfld_chk_ovf(i915, var, val) ({ \
1375 	(var) = (val); \
1376 	drm_WARN_ON(&(i915)->drm, (var) != (val)); \
1377 	(var); \
1378 })
1379 
1380 static u32 calc_plane_remap_info(const struct intel_framebuffer *fb, int color_plane,
1381 				 const struct fb_plane_view_dims *dims,
1382 				 u32 obj_offset, u32 gtt_offset, int x, int y,
1383 				 struct intel_fb_view *view)
1384 {
1385 	struct drm_i915_private *i915 = to_i915(fb->base.dev);
1386 	struct intel_remapped_plane_info *remap_info = &view->gtt.remapped.plane[color_plane];
1387 	struct i915_color_plane_view *color_plane_info = &view->color_plane[color_plane];
1388 	unsigned int tile_width = dims->tile_width;
1389 	unsigned int tile_height = dims->tile_height;
1390 	unsigned int tile_size = intel_tile_size(i915);
1391 	struct drm_rect r;
1392 	u32 size = 0;
1393 
1394 	assign_bfld_chk_ovf(i915, remap_info->offset, obj_offset);
1395 
1396 	if (intel_fb_is_gen12_ccs_aux_plane(&fb->base, color_plane)) {
1397 		remap_info->linear = 1;
1398 
1399 		assign_chk_ovf(i915, remap_info->size,
1400 			       plane_view_linear_tiles(fb, color_plane, dims, x, y));
1401 	} else {
1402 		remap_info->linear = 0;
1403 
1404 		assign_chk_ovf(i915, remap_info->src_stride,
1405 			       plane_view_src_stride_tiles(fb, color_plane, dims));
1406 		assign_chk_ovf(i915, remap_info->width,
1407 			       plane_view_width_tiles(fb, color_plane, dims, x));
1408 		assign_chk_ovf(i915, remap_info->height,
1409 			       plane_view_height_tiles(fb, color_plane, dims, y));
1410 	}
1411 
1412 	if (view->gtt.type == I915_GTT_VIEW_ROTATED) {
1413 		drm_WARN_ON(&i915->drm, remap_info->linear);
1414 		check_array_bounds(i915, view->gtt.rotated.plane, color_plane);
1415 
1416 		assign_chk_ovf(i915, remap_info->dst_stride,
1417 			       plane_view_dst_stride_tiles(fb, color_plane, remap_info->height));
1418 
1419 		/* rotate the x/y offsets to match the GTT view */
1420 		drm_rect_init(&r, x, y, dims->width, dims->height);
1421 		drm_rect_rotate(&r,
1422 				remap_info->width * tile_width,
1423 				remap_info->height * tile_height,
1424 				DRM_MODE_ROTATE_270);
1425 
1426 		color_plane_info->x = r.x1;
1427 		color_plane_info->y = r.y1;
1428 
1429 		color_plane_info->mapping_stride = remap_info->dst_stride * tile_height;
1430 		color_plane_info->scanout_stride = color_plane_info->mapping_stride;
1431 
1432 		size += remap_info->dst_stride * remap_info->width;
1433 
1434 		/* rotate the tile dimensions to match the GTT view */
1435 		swap(tile_width, tile_height);
1436 	} else {
1437 		drm_WARN_ON(&i915->drm, view->gtt.type != I915_GTT_VIEW_REMAPPED);
1438 
1439 		check_array_bounds(i915, view->gtt.remapped.plane, color_plane);
1440 
1441 		if (view->gtt.remapped.plane_alignment) {
1442 			unsigned int aligned_offset = ALIGN(gtt_offset,
1443 							    view->gtt.remapped.plane_alignment);
1444 
1445 			size += aligned_offset - gtt_offset;
1446 			gtt_offset = aligned_offset;
1447 		}
1448 
1449 		color_plane_info->x = x;
1450 		color_plane_info->y = y;
1451 
1452 		if (remap_info->linear) {
1453 			color_plane_info->mapping_stride = fb->base.pitches[color_plane];
1454 			color_plane_info->scanout_stride = color_plane_info->mapping_stride;
1455 
1456 			size += remap_info->size;
1457 		} else {
1458 			unsigned int dst_stride = plane_view_dst_stride_tiles(fb, color_plane,
1459 									      remap_info->width);
1460 
1461 			assign_chk_ovf(i915, remap_info->dst_stride, dst_stride);
1462 			color_plane_info->mapping_stride = dst_stride *
1463 							   tile_width *
1464 							   fb->base.format->cpp[color_plane];
1465 			color_plane_info->scanout_stride =
1466 				plane_view_scanout_stride(fb, color_plane, tile_width,
1467 							  remap_info->src_stride,
1468 							  dst_stride);
1469 
1470 			size += dst_stride * remap_info->height;
1471 		}
1472 	}
1473 
1474 	/*
1475 	 * We only keep the x/y offsets, so push all of the gtt offset into
1476 	 * the x/y offsets.  x,y will hold the first pixel of the framebuffer
1477 	 * plane from the start of the remapped/rotated gtt mapping.
1478 	 */
1479 	if (remap_info->linear)
1480 		intel_adjust_linear_offset(&color_plane_info->x, &color_plane_info->y,
1481 					   fb->base.format->cpp[color_plane],
1482 					   color_plane_info->mapping_stride,
1483 					   gtt_offset * tile_size, 0);
1484 	else
1485 		intel_adjust_tile_offset(&color_plane_info->x, &color_plane_info->y,
1486 					 tile_width, tile_height,
1487 					 tile_size, remap_info->dst_stride,
1488 					 gtt_offset * tile_size, 0);
1489 
1490 	return size;
1491 }
1492 
1493 #undef assign_chk_ovf
1494 
1495 /* Return number of tiles @color_plane needs. */
1496 static unsigned int
1497 calc_plane_normal_size(const struct intel_framebuffer *fb, int color_plane,
1498 		       const struct fb_plane_view_dims *dims,
1499 		       int x, int y)
1500 {
1501 	unsigned int tiles;
1502 
1503 	if (is_surface_linear(&fb->base, color_plane)) {
1504 		tiles = plane_view_linear_tiles(fb, color_plane, dims, x, y);
1505 	} else {
1506 		tiles = plane_view_src_stride_tiles(fb, color_plane, dims) *
1507 			plane_view_height_tiles(fb, color_plane, dims, y);
1508 		/*
1509 		 * If the plane isn't horizontally tile aligned,
1510 		 * we need one more tile.
1511 		 */
1512 		if (x != 0)
1513 			tiles++;
1514 	}
1515 
1516 	return tiles;
1517 }
1518 
1519 static void intel_fb_view_init(struct drm_i915_private *i915, struct intel_fb_view *view,
1520 			       enum i915_gtt_view_type view_type)
1521 {
1522 	memset(view, 0, sizeof(*view));
1523 	view->gtt.type = view_type;
1524 
1525 	if (view_type == I915_GTT_VIEW_REMAPPED && IS_ALDERLAKE_P(i915))
1526 		view->gtt.remapped.plane_alignment = SZ_2M / PAGE_SIZE;
1527 }
1528 
1529 bool intel_fb_supports_90_270_rotation(const struct intel_framebuffer *fb)
1530 {
1531 	if (DISPLAY_VER(to_i915(fb->base.dev)) >= 13)
1532 		return false;
1533 
1534 	return fb->base.modifier == I915_FORMAT_MOD_Y_TILED ||
1535 	       fb->base.modifier == I915_FORMAT_MOD_Yf_TILED;
1536 }
1537 
1538 int intel_fill_fb_info(struct drm_i915_private *i915, struct intel_framebuffer *fb)
1539 {
1540 	struct drm_i915_gem_object *obj = intel_fb_obj(&fb->base);
1541 	u32 gtt_offset_rotated = 0;
1542 	u32 gtt_offset_remapped = 0;
1543 	unsigned int max_size = 0;
1544 	int i, num_planes = fb->base.format->num_planes;
1545 	unsigned int tile_size = intel_tile_size(i915);
1546 
1547 	intel_fb_view_init(i915, &fb->normal_view, I915_GTT_VIEW_NORMAL);
1548 
1549 	drm_WARN_ON(&i915->drm,
1550 		    intel_fb_supports_90_270_rotation(fb) &&
1551 		    intel_fb_needs_pot_stride_remap(fb));
1552 
1553 	if (intel_fb_supports_90_270_rotation(fb))
1554 		intel_fb_view_init(i915, &fb->rotated_view, I915_GTT_VIEW_ROTATED);
1555 	if (intel_fb_needs_pot_stride_remap(fb))
1556 		intel_fb_view_init(i915, &fb->remapped_view, I915_GTT_VIEW_REMAPPED);
1557 
1558 	for (i = 0; i < num_planes; i++) {
1559 		struct fb_plane_view_dims view_dims;
1560 		unsigned int width, height;
1561 		unsigned int cpp, size;
1562 		u32 offset;
1563 		int x, y;
1564 		int ret;
1565 
1566 		/*
1567 		 * Plane 2 of Render Compression with Clear Color fb modifier
1568 		 * is consumed by the driver and not passed to DE. Skip the
1569 		 * arithmetic related to alignment and offset calculation.
1570 		 */
1571 		if (is_gen12_ccs_cc_plane(&fb->base, i)) {
1572 			if (IS_ALIGNED(fb->base.offsets[i], PAGE_SIZE))
1573 				continue;
1574 			else
1575 				return -EINVAL;
1576 		}
1577 
1578 		cpp = fb->base.format->cpp[i];
1579 		intel_fb_plane_dims(fb, i, &width, &height);
1580 
1581 		ret = convert_plane_offset_to_xy(fb, i, width, &x, &y);
1582 		if (ret)
1583 			return ret;
1584 
1585 		init_plane_view_dims(fb, i, width, height, &view_dims);
1586 
1587 		/*
1588 		 * First pixel of the framebuffer from
1589 		 * the start of the normal gtt mapping.
1590 		 */
1591 		fb->normal_view.color_plane[i].x = x;
1592 		fb->normal_view.color_plane[i].y = y;
1593 		fb->normal_view.color_plane[i].mapping_stride = fb->base.pitches[i];
1594 		fb->normal_view.color_plane[i].scanout_stride =
1595 			fb->normal_view.color_plane[i].mapping_stride;
1596 
1597 		offset = calc_plane_aligned_offset(fb, i, &x, &y);
1598 
1599 		if (intel_fb_supports_90_270_rotation(fb))
1600 			gtt_offset_rotated += calc_plane_remap_info(fb, i, &view_dims,
1601 								    offset, gtt_offset_rotated, x, y,
1602 								    &fb->rotated_view);
1603 
1604 		if (intel_fb_needs_pot_stride_remap(fb))
1605 			gtt_offset_remapped += calc_plane_remap_info(fb, i, &view_dims,
1606 								     offset, gtt_offset_remapped, x, y,
1607 								     &fb->remapped_view);
1608 
1609 		size = calc_plane_normal_size(fb, i, &view_dims, x, y);
1610 		/* how many tiles in total needed in the bo */
1611 		max_size = max(max_size, offset + size);
1612 	}
1613 
1614 	if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
1615 		drm_dbg_kms(&i915->drm,
1616 			    "fb too big for bo (need %llu bytes, have %zu bytes)\n",
1617 			    mul_u32_u32(max_size, tile_size), obj->base.size);
1618 		return -EINVAL;
1619 	}
1620 
1621 	return 0;
1622 }
1623 
1624 static void intel_plane_remap_gtt(struct intel_plane_state *plane_state)
1625 {
1626 	struct drm_i915_private *i915 =
1627 		to_i915(plane_state->uapi.plane->dev);
1628 	struct drm_framebuffer *fb = plane_state->hw.fb;
1629 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1630 	unsigned int rotation = plane_state->hw.rotation;
1631 	int i, num_planes = fb->format->num_planes;
1632 	unsigned int src_x, src_y;
1633 	unsigned int src_w, src_h;
1634 	u32 gtt_offset = 0;
1635 
1636 	intel_fb_view_init(i915, &plane_state->view,
1637 			   drm_rotation_90_or_270(rotation) ? I915_GTT_VIEW_ROTATED :
1638 							      I915_GTT_VIEW_REMAPPED);
1639 
1640 	src_x = plane_state->uapi.src.x1 >> 16;
1641 	src_y = plane_state->uapi.src.y1 >> 16;
1642 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
1643 	src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
1644 
1645 	drm_WARN_ON(&i915->drm, intel_fb_is_ccs_modifier(fb->modifier));
1646 
1647 	/* Make src coordinates relative to the viewport */
1648 	drm_rect_translate(&plane_state->uapi.src,
1649 			   -(src_x << 16), -(src_y << 16));
1650 
1651 	/* Rotate src coordinates to match rotated GTT view */
1652 	if (drm_rotation_90_or_270(rotation))
1653 		drm_rect_rotate(&plane_state->uapi.src,
1654 				src_w << 16, src_h << 16,
1655 				DRM_MODE_ROTATE_270);
1656 
1657 	for (i = 0; i < num_planes; i++) {
1658 		unsigned int hsub = i ? fb->format->hsub : 1;
1659 		unsigned int vsub = i ? fb->format->vsub : 1;
1660 		struct fb_plane_view_dims view_dims;
1661 		unsigned int width, height;
1662 		unsigned int x, y;
1663 		u32 offset;
1664 
1665 		x = src_x / hsub;
1666 		y = src_y / vsub;
1667 		width = src_w / hsub;
1668 		height = src_h / vsub;
1669 
1670 		init_plane_view_dims(intel_fb, i, width, height, &view_dims);
1671 
1672 		/*
1673 		 * First pixel of the src viewport from the
1674 		 * start of the normal gtt mapping.
1675 		 */
1676 		x += intel_fb->normal_view.color_plane[i].x;
1677 		y += intel_fb->normal_view.color_plane[i].y;
1678 
1679 		offset = calc_plane_aligned_offset(intel_fb, i, &x, &y);
1680 
1681 		gtt_offset += calc_plane_remap_info(intel_fb, i, &view_dims,
1682 						    offset, gtt_offset, x, y,
1683 						    &plane_state->view);
1684 	}
1685 }
1686 
1687 void intel_fb_fill_view(const struct intel_framebuffer *fb, unsigned int rotation,
1688 			struct intel_fb_view *view)
1689 {
1690 	if (drm_rotation_90_or_270(rotation))
1691 		*view = fb->rotated_view;
1692 	else if (intel_fb_needs_pot_stride_remap(fb))
1693 		*view = fb->remapped_view;
1694 	else
1695 		*view = fb->normal_view;
1696 }
1697 
1698 static
1699 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
1700 			u32 pixel_format, u64 modifier)
1701 {
1702 	/*
1703 	 * Arbitrary limit for gen4+ chosen to match the
1704 	 * render engine max stride.
1705 	 *
1706 	 * The new CCS hash mode makes remapping impossible
1707 	 */
1708 	if (DISPLAY_VER(dev_priv) < 4 || intel_fb_is_ccs_modifier(modifier) ||
1709 	    intel_fb_modifier_uses_dpt(dev_priv, modifier))
1710 		return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
1711 	else if (DISPLAY_VER(dev_priv) >= 7)
1712 		return 256 * 1024;
1713 	else
1714 		return 128 * 1024;
1715 }
1716 
1717 static u32
1718 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
1719 {
1720 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
1721 	u32 tile_width;
1722 
1723 	if (is_surface_linear(fb, color_plane)) {
1724 		u32 max_stride = intel_plane_fb_max_stride(dev_priv,
1725 							   fb->format->format,
1726 							   fb->modifier);
1727 
1728 		/*
1729 		 * To make remapping with linear generally feasible
1730 		 * we need the stride to be page aligned.
1731 		 */
1732 		if (fb->pitches[color_plane] > max_stride &&
1733 		    !intel_fb_is_ccs_modifier(fb->modifier))
1734 			return intel_tile_size(dev_priv);
1735 		else
1736 			return 64;
1737 	}
1738 
1739 	tile_width = intel_tile_width_bytes(fb, color_plane);
1740 	if (intel_fb_is_ccs_modifier(fb->modifier)) {
1741 		/*
1742 		 * On TGL the surface stride must be 4 tile aligned, mapped by
1743 		 * one 64 byte cacheline on the CCS AUX surface.
1744 		 */
1745 		if (DISPLAY_VER(dev_priv) >= 12)
1746 			tile_width *= 4;
1747 		/*
1748 		 * Display WA #0531: skl,bxt,kbl,glk
1749 		 *
1750 		 * Render decompression and plane width > 3840
1751 		 * combined with horizontal panning requires the
1752 		 * plane stride to be a multiple of 4. We'll just
1753 		 * require the entire fb to accommodate that to avoid
1754 		 * potential runtime errors at plane configuration time.
1755 		 */
1756 		else if ((DISPLAY_VER(dev_priv) == 9 || IS_GEMINILAKE(dev_priv)) &&
1757 			 color_plane == 0 && fb->width > 3840)
1758 			tile_width *= 4;
1759 	}
1760 	return tile_width;
1761 }
1762 
1763 static int intel_plane_check_stride(const struct intel_plane_state *plane_state)
1764 {
1765 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1766 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1767 	unsigned int rotation = plane_state->hw.rotation;
1768 	u32 stride, max_stride;
1769 
1770 	/*
1771 	 * We ignore stride for all invisible planes that
1772 	 * can be remapped. Otherwise we could end up
1773 	 * with a false positive when the remapping didn't
1774 	 * kick in due the plane being invisible.
1775 	 */
1776 	if (intel_plane_can_remap(plane_state) &&
1777 	    !plane_state->uapi.visible)
1778 		return 0;
1779 
1780 	/* FIXME other color planes? */
1781 	stride = plane_state->view.color_plane[0].mapping_stride;
1782 	max_stride = plane->max_stride(plane, fb->format->format,
1783 				       fb->modifier, rotation);
1784 
1785 	if (stride > max_stride) {
1786 		DRM_DEBUG_KMS("[FB:%d] stride (%d) exceeds [PLANE:%d:%s] max stride (%d)\n",
1787 			      fb->base.id, stride,
1788 			      plane->base.base.id, plane->base.name, max_stride);
1789 		return -EINVAL;
1790 	}
1791 
1792 	return 0;
1793 }
1794 
1795 int intel_plane_compute_gtt(struct intel_plane_state *plane_state)
1796 {
1797 	const struct intel_framebuffer *fb =
1798 		to_intel_framebuffer(plane_state->hw.fb);
1799 	unsigned int rotation = plane_state->hw.rotation;
1800 
1801 	if (!fb)
1802 		return 0;
1803 
1804 	if (intel_plane_needs_remap(plane_state)) {
1805 		intel_plane_remap_gtt(plane_state);
1806 
1807 		/*
1808 		 * Sometimes even remapping can't overcome
1809 		 * the stride limitations :( Can happen with
1810 		 * big plane sizes and suitably misaligned
1811 		 * offsets.
1812 		 */
1813 		return intel_plane_check_stride(plane_state);
1814 	}
1815 
1816 	intel_fb_fill_view(fb, rotation, &plane_state->view);
1817 
1818 	/* Rotate src coordinates to match rotated GTT view */
1819 	if (drm_rotation_90_or_270(rotation))
1820 		drm_rect_rotate(&plane_state->uapi.src,
1821 				fb->base.width << 16, fb->base.height << 16,
1822 				DRM_MODE_ROTATE_270);
1823 
1824 	return intel_plane_check_stride(plane_state);
1825 }
1826 
1827 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
1828 {
1829 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1830 
1831 	drm_framebuffer_cleanup(fb);
1832 
1833 	if (intel_fb_uses_dpt(fb))
1834 		intel_dpt_destroy(intel_fb->dpt_vm);
1835 
1836 	intel_frontbuffer_put(intel_fb->frontbuffer);
1837 
1838 	kfree(intel_fb);
1839 }
1840 
1841 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1842 						struct drm_file *file,
1843 						unsigned int *handle)
1844 {
1845 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1846 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
1847 
1848 	if (i915_gem_object_is_userptr(obj)) {
1849 		drm_dbg(&i915->drm,
1850 			"attempting to use a userptr for a framebuffer, denied\n");
1851 		return -EINVAL;
1852 	}
1853 
1854 	return drm_gem_handle_create(file, &obj->base, handle);
1855 }
1856 
1857 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
1858 					struct drm_file *file,
1859 					unsigned int flags, unsigned int color,
1860 					struct drm_clip_rect *clips,
1861 					unsigned int num_clips)
1862 {
1863 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1864 
1865 	i915_gem_object_flush_if_display(obj);
1866 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
1867 
1868 	return 0;
1869 }
1870 
1871 static const struct drm_framebuffer_funcs intel_fb_funcs = {
1872 	.destroy = intel_user_framebuffer_destroy,
1873 	.create_handle = intel_user_framebuffer_create_handle,
1874 	.dirty = intel_user_framebuffer_dirty,
1875 };
1876 
1877 int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
1878 			   struct drm_i915_gem_object *obj,
1879 			   struct drm_mode_fb_cmd2 *mode_cmd)
1880 {
1881 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
1882 	struct drm_framebuffer *fb = &intel_fb->base;
1883 	u32 max_stride;
1884 	unsigned int tiling, stride;
1885 	int ret = -EINVAL;
1886 	int i;
1887 
1888 	intel_fb->frontbuffer = intel_frontbuffer_get(obj);
1889 	if (!intel_fb->frontbuffer)
1890 		return -ENOMEM;
1891 
1892 	i915_gem_object_lock(obj, NULL);
1893 	tiling = i915_gem_object_get_tiling(obj);
1894 	stride = i915_gem_object_get_stride(obj);
1895 	i915_gem_object_unlock(obj);
1896 
1897 	if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
1898 		/*
1899 		 * If there's a fence, enforce that
1900 		 * the fb modifier and tiling mode match.
1901 		 */
1902 		if (tiling != I915_TILING_NONE &&
1903 		    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
1904 			drm_dbg_kms(&dev_priv->drm,
1905 				    "tiling_mode doesn't match fb modifier\n");
1906 			goto err;
1907 		}
1908 	} else {
1909 		if (tiling == I915_TILING_X) {
1910 			mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
1911 		} else if (tiling == I915_TILING_Y) {
1912 			drm_dbg_kms(&dev_priv->drm,
1913 				    "No Y tiling for legacy addfb\n");
1914 			goto err;
1915 		}
1916 	}
1917 
1918 	if (!drm_any_plane_has_format(&dev_priv->drm,
1919 				      mode_cmd->pixel_format,
1920 				      mode_cmd->modifier[0])) {
1921 		drm_dbg_kms(&dev_priv->drm,
1922 			    "unsupported pixel format %p4cc / modifier 0x%llx\n",
1923 			    &mode_cmd->pixel_format, mode_cmd->modifier[0]);
1924 		goto err;
1925 	}
1926 
1927 	/*
1928 	 * gen2/3 display engine uses the fence if present,
1929 	 * so the tiling mode must match the fb modifier exactly.
1930 	 */
1931 	if (DISPLAY_VER(dev_priv) < 4 &&
1932 	    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
1933 		drm_dbg_kms(&dev_priv->drm,
1934 			    "tiling_mode must match fb modifier exactly on gen2/3\n");
1935 		goto err;
1936 	}
1937 
1938 	max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
1939 					 mode_cmd->modifier[0]);
1940 	if (mode_cmd->pitches[0] > max_stride) {
1941 		drm_dbg_kms(&dev_priv->drm,
1942 			    "%s pitch (%u) must be at most %d\n",
1943 			    mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
1944 			    "tiled" : "linear",
1945 			    mode_cmd->pitches[0], max_stride);
1946 		goto err;
1947 	}
1948 
1949 	/*
1950 	 * If there's a fence, enforce that
1951 	 * the fb pitch and fence stride match.
1952 	 */
1953 	if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
1954 		drm_dbg_kms(&dev_priv->drm,
1955 			    "pitch (%d) must match tiling stride (%d)\n",
1956 			    mode_cmd->pitches[0], stride);
1957 		goto err;
1958 	}
1959 
1960 	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
1961 	if (mode_cmd->offsets[0] != 0) {
1962 		drm_dbg_kms(&dev_priv->drm,
1963 			    "plane 0 offset (0x%08x) must be 0\n",
1964 			    mode_cmd->offsets[0]);
1965 		goto err;
1966 	}
1967 
1968 	drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
1969 
1970 	for (i = 0; i < fb->format->num_planes; i++) {
1971 		u32 stride_alignment;
1972 
1973 		if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
1974 			drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
1975 				    i);
1976 			goto err;
1977 		}
1978 
1979 		stride_alignment = intel_fb_stride_alignment(fb, i);
1980 		if (fb->pitches[i] & (stride_alignment - 1)) {
1981 			drm_dbg_kms(&dev_priv->drm,
1982 				    "plane %d pitch (%d) must be at least %u byte aligned\n",
1983 				    i, fb->pitches[i], stride_alignment);
1984 			goto err;
1985 		}
1986 
1987 		if (intel_fb_is_gen12_ccs_aux_plane(fb, i)) {
1988 			int ccs_aux_stride = gen12_ccs_aux_stride(intel_fb, i);
1989 
1990 			if (fb->pitches[i] != ccs_aux_stride) {
1991 				drm_dbg_kms(&dev_priv->drm,
1992 					    "ccs aux plane %d pitch (%d) must be %d\n",
1993 					    i,
1994 					    fb->pitches[i], ccs_aux_stride);
1995 				goto err;
1996 			}
1997 		}
1998 
1999 		fb->obj[i] = &obj->base;
2000 	}
2001 
2002 	ret = intel_fill_fb_info(dev_priv, intel_fb);
2003 	if (ret)
2004 		goto err;
2005 
2006 	if (intel_fb_uses_dpt(fb)) {
2007 		struct i915_address_space *vm;
2008 
2009 		vm = intel_dpt_create(intel_fb);
2010 		if (IS_ERR(vm)) {
2011 			drm_dbg_kms(&dev_priv->drm, "failed to create DPT\n");
2012 			ret = PTR_ERR(vm);
2013 			goto err;
2014 		}
2015 
2016 		intel_fb->dpt_vm = vm;
2017 	}
2018 
2019 	ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
2020 	if (ret) {
2021 		drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
2022 		goto err_free_dpt;
2023 	}
2024 
2025 	return 0;
2026 
2027 err_free_dpt:
2028 	if (intel_fb_uses_dpt(fb))
2029 		intel_dpt_destroy(intel_fb->dpt_vm);
2030 err:
2031 	intel_frontbuffer_put(intel_fb->frontbuffer);
2032 	return ret;
2033 }
2034 
2035 struct drm_framebuffer *
2036 intel_user_framebuffer_create(struct drm_device *dev,
2037 			      struct drm_file *filp,
2038 			      const struct drm_mode_fb_cmd2 *user_mode_cmd)
2039 {
2040 	struct drm_framebuffer *fb;
2041 	struct drm_i915_gem_object *obj;
2042 	struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
2043 	struct drm_i915_private *i915;
2044 
2045 	obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
2046 	if (!obj)
2047 		return ERR_PTR(-ENOENT);
2048 
2049 	/* object is backed with LMEM for discrete */
2050 	i915 = to_i915(obj->base.dev);
2051 	if (HAS_LMEM(i915) && !i915_gem_object_can_migrate(obj, INTEL_REGION_LMEM_0)) {
2052 		/* object is "remote", not in local memory */
2053 		i915_gem_object_put(obj);
2054 		drm_dbg_kms(&i915->drm, "framebuffer must reside in local memory\n");
2055 		return ERR_PTR(-EREMOTE);
2056 	}
2057 
2058 	fb = intel_framebuffer_create(obj, &mode_cmd);
2059 	i915_gem_object_put(obj);
2060 
2061 	return fb;
2062 }
2063 
2064 struct drm_framebuffer *
2065 intel_framebuffer_create(struct drm_i915_gem_object *obj,
2066 			 struct drm_mode_fb_cmd2 *mode_cmd)
2067 {
2068 	struct intel_framebuffer *intel_fb;
2069 	int ret;
2070 
2071 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
2072 	if (!intel_fb)
2073 		return ERR_PTR(-ENOMEM);
2074 
2075 	ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
2076 	if (ret)
2077 		goto err;
2078 
2079 	return &intel_fb->base;
2080 
2081 err:
2082 	kfree(intel_fb);
2083 	return ERR_PTR(ret);
2084 }
2085