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