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