1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
6 #include <linux/dma-fence.h>
7 #include <linux/dma-resv.h>
9 #include <drm/drm_blend.h>
10 #include <drm/drm_gem.h>
11 #include <drm/drm_modeset_helper.h>
14 #include "intel_atomic_plane.h"
16 #include "intel_display.h"
17 #include "intel_display_types.h"
18 #include "intel_dpt.h"
20 #include "intel_fb_bo.h"
21 #include "intel_frontbuffer.h"
23 #define check_array_bounds(i915, a, i) drm_WARN_ON(&(i915)->drm, (i) >= ARRAY_SIZE(a))
26 * From the Sky Lake PRM:
27 * "The Color Control Surface (CCS) contains the compression status of
28 * the cache-line pairs. The compression state of the cache-line pair
29 * is specified by 2 bits in the CCS. Each CCS cache-line represents
30 * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
31 * cache-line-pairs. CCS is always Y tiled."
33 * Since cache line pairs refers to horizontally adjacent cache lines,
34 * each cache line in the CCS corresponds to an area of 32x16 cache
35 * lines on the main surface. Since each pixel is 4 bytes, this gives
36 * us a ratio of one byte in the CCS for each 8x16 pixels in the
39 static const struct drm_format_info skl_ccs_formats[] = {
40 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
41 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
42 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
43 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
44 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
45 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
46 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
47 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
48 { .format = DRM_FORMAT_XRGB2101010, .depth = 30, .num_planes = 2,
49 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
50 { .format = DRM_FORMAT_XBGR2101010, .depth = 30, .num_planes = 2,
51 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
52 { .format = DRM_FORMAT_ARGB2101010, .depth = 30, .num_planes = 2,
53 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
54 { .format = DRM_FORMAT_ABGR2101010, .depth = 30, .num_planes = 2,
55 .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
59 * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
60 * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
61 * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
62 * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
65 static const struct drm_format_info gen12_ccs_formats[] = {
66 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
67 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
68 .hsub = 1, .vsub = 1, },
69 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
70 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
71 .hsub = 1, .vsub = 1, },
72 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
73 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
74 .hsub = 1, .vsub = 1, .has_alpha = true },
75 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
76 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
77 .hsub = 1, .vsub = 1, .has_alpha = true },
78 { .format = DRM_FORMAT_XRGB2101010, .depth = 30, .num_planes = 2,
79 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
80 .hsub = 1, .vsub = 1, },
81 { .format = DRM_FORMAT_XBGR2101010, .depth = 30, .num_planes = 2,
82 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
83 .hsub = 1, .vsub = 1, },
84 { .format = DRM_FORMAT_ARGB2101010, .depth = 30, .num_planes = 2,
85 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
86 .hsub = 1, .vsub = 1, .has_alpha = true },
87 { .format = DRM_FORMAT_ABGR2101010, .depth = 30, .num_planes = 2,
88 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
89 .hsub = 1, .vsub = 1, .has_alpha = true },
90 { .format = DRM_FORMAT_XRGB16161616F, .depth = 0, .num_planes = 2,
91 .char_per_block = { 8, 1 }, .block_w = { 1, 1 }, .block_h = { 1, 1 },
92 .hsub = 1, .vsub = 1, },
93 { .format = DRM_FORMAT_XBGR16161616F, .depth = 0, .num_planes = 2,
94 .char_per_block = { 8, 1 }, .block_w = { 1, 1 }, .block_h = { 1, 1 },
95 .hsub = 1, .vsub = 1, },
96 { .format = DRM_FORMAT_ARGB16161616F, .depth = 0, .num_planes = 2,
97 .char_per_block = { 8, 1 }, .block_w = { 1, 1 }, .block_h = { 1, 1 },
98 .hsub = 1, .vsub = 1, .has_alpha = true },
99 { .format = DRM_FORMAT_ABGR16161616F, .depth = 0, .num_planes = 2,
100 .char_per_block = { 8, 1 }, .block_w = { 1, 1 }, .block_h = { 1, 1 },
101 .hsub = 1, .vsub = 1, .has_alpha = true },
102 { .format = DRM_FORMAT_YUYV, .num_planes = 2,
103 .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
104 .hsub = 2, .vsub = 1, .is_yuv = true },
105 { .format = DRM_FORMAT_YVYU, .num_planes = 2,
106 .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
107 .hsub = 2, .vsub = 1, .is_yuv = true },
108 { .format = DRM_FORMAT_UYVY, .num_planes = 2,
109 .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
110 .hsub = 2, .vsub = 1, .is_yuv = true },
111 { .format = DRM_FORMAT_VYUY, .num_planes = 2,
112 .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
113 .hsub = 2, .vsub = 1, .is_yuv = true },
114 { .format = DRM_FORMAT_XYUV8888, .num_planes = 2,
115 .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
116 .hsub = 1, .vsub = 1, .is_yuv = true },
117 { .format = DRM_FORMAT_NV12, .num_planes = 4,
118 .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
119 .hsub = 2, .vsub = 2, .is_yuv = true },
120 { .format = DRM_FORMAT_P010, .num_planes = 4,
121 .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
122 .hsub = 2, .vsub = 2, .is_yuv = true },
123 { .format = DRM_FORMAT_P012, .num_planes = 4,
124 .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
125 .hsub = 2, .vsub = 2, .is_yuv = true },
126 { .format = DRM_FORMAT_P016, .num_planes = 4,
127 .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
128 .hsub = 2, .vsub = 2, .is_yuv = true },
132 * Same as gen12_ccs_formats[] above, but with additional surface used
133 * to pass Clear Color information in plane 2 with 64 bits of data.
135 static const struct drm_format_info gen12_ccs_cc_formats[] = {
136 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 3,
137 .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 0 }, .block_h = { 1, 1, 0 },
138 .hsub = 1, .vsub = 1, },
139 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 3,
140 .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 0 }, .block_h = { 1, 1, 0 },
141 .hsub = 1, .vsub = 1, },
142 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 3,
143 .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 0 }, .block_h = { 1, 1, 0 },
144 .hsub = 1, .vsub = 1, .has_alpha = true },
145 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 3,
146 .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 0 }, .block_h = { 1, 1, 0 },
147 .hsub = 1, .vsub = 1, .has_alpha = true },
148 { .format = DRM_FORMAT_XRGB2101010, .depth = 30, .num_planes = 3,
149 .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 0 }, .block_h = { 1, 1, 0 },
150 .hsub = 1, .vsub = 1, },
151 { .format = DRM_FORMAT_XBGR2101010, .depth = 30, .num_planes = 3,
152 .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 0 }, .block_h = { 1, 1, 0 },
153 .hsub = 1, .vsub = 1, },
154 { .format = DRM_FORMAT_ARGB2101010, .depth = 30, .num_planes = 3,
155 .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 0 }, .block_h = { 1, 1, 0 },
156 .hsub = 1, .vsub = 1, .has_alpha = true },
157 { .format = DRM_FORMAT_ABGR2101010, .depth = 30, .num_planes = 3,
158 .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 0 }, .block_h = { 1, 1, 0 },
159 .hsub = 1, .vsub = 1, .has_alpha = true },
160 { .format = DRM_FORMAT_XRGB16161616F, .depth = 0, .num_planes = 3,
161 .char_per_block = { 8, 1, 0 }, .block_w = { 1, 1, 0 }, .block_h = { 1, 1, 0 },
162 .hsub = 1, .vsub = 1, },
163 { .format = DRM_FORMAT_XBGR16161616F, .depth = 0, .num_planes = 3,
164 .char_per_block = { 8, 1, 0 }, .block_w = { 1, 1, 0 }, .block_h = { 1, 1, 0 },
165 .hsub = 1, .vsub = 1, },
166 { .format = DRM_FORMAT_ARGB16161616F, .depth = 0, .num_planes = 3,
167 .char_per_block = { 8, 1, 0 }, .block_w = { 1, 1, 0 }, .block_h = { 1, 1, 0 },
168 .hsub = 1, .vsub = 1, .has_alpha = true },
169 { .format = DRM_FORMAT_ABGR16161616F, .depth = 0, .num_planes = 3,
170 .char_per_block = { 8, 1, 0 }, .block_w = { 1, 1, 0 }, .block_h = { 1, 1, 0 },
171 .hsub = 1, .vsub = 1, .has_alpha = true },
174 static const struct drm_format_info gen12_flat_ccs_cc_formats[] = {
175 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
176 .char_per_block = { 4, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
177 .hsub = 1, .vsub = 1, },
178 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
179 .char_per_block = { 4, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
180 .hsub = 1, .vsub = 1, },
181 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
182 .char_per_block = { 4, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
183 .hsub = 1, .vsub = 1, .has_alpha = true },
184 { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
185 .char_per_block = { 4, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
186 .hsub = 1, .vsub = 1, .has_alpha = true },
187 { .format = DRM_FORMAT_XRGB2101010, .depth = 30, .num_planes = 2,
188 .char_per_block = { 4, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
189 .hsub = 1, .vsub = 1, },
190 { .format = DRM_FORMAT_XBGR2101010, .depth = 30, .num_planes = 2,
191 .char_per_block = { 4, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
192 .hsub = 1, .vsub = 1, },
193 { .format = DRM_FORMAT_ARGB2101010, .depth = 30, .num_planes = 2,
194 .char_per_block = { 4, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
195 .hsub = 1, .vsub = 1, .has_alpha = true },
196 { .format = DRM_FORMAT_ABGR2101010, .depth = 30, .num_planes = 2,
197 .char_per_block = { 4, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
198 .hsub = 1, .vsub = 1, .has_alpha = true },
199 { .format = DRM_FORMAT_XRGB16161616F, .depth = 0, .num_planes = 2,
200 .char_per_block = { 8, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
201 .hsub = 1, .vsub = 1, },
202 { .format = DRM_FORMAT_XBGR16161616F, .depth = 0, .num_planes = 2,
203 .char_per_block = { 8, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
204 .hsub = 1, .vsub = 1, },
205 { .format = DRM_FORMAT_ARGB16161616F, .depth = 0, .num_planes = 2,
206 .char_per_block = { 8, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
207 .hsub = 1, .vsub = 1, .has_alpha = true },
208 { .format = DRM_FORMAT_ABGR16161616F, .depth = 0, .num_planes = 2,
209 .char_per_block = { 8, 0 }, .block_w = { 1, 0 }, .block_h = { 1, 0 },
210 .hsub = 1, .vsub = 1, .has_alpha = true },
213 struct intel_modifier_desc {
219 #define DISPLAY_VER_ALL { 0, -1 }
221 const struct drm_format_info *formats;
223 #define FORMAT_OVERRIDE(format_list) \
224 .formats = format_list, \
225 .format_count = ARRAY_SIZE(format_list)
231 u8 packed_aux_planes:4;
232 u8 planar_aux_planes:4;
236 #define INTEL_PLANE_CAP_CCS_MASK (INTEL_PLANE_CAP_CCS_RC | \
237 INTEL_PLANE_CAP_CCS_RC_CC | \
238 INTEL_PLANE_CAP_CCS_MC)
239 #define INTEL_PLANE_CAP_TILING_MASK (INTEL_PLANE_CAP_TILING_X | \
240 INTEL_PLANE_CAP_TILING_Y | \
241 INTEL_PLANE_CAP_TILING_Yf | \
242 INTEL_PLANE_CAP_TILING_4)
243 #define INTEL_PLANE_CAP_TILING_NONE 0
245 static const struct intel_modifier_desc intel_modifiers[] = {
247 .modifier = I915_FORMAT_MOD_4_TILED_LNL_CCS,
248 .display_ver = { 20, -1 },
249 .plane_caps = INTEL_PLANE_CAP_TILING_4,
251 .modifier = I915_FORMAT_MOD_4_TILED_BMG_CCS,
252 .display_ver = { 14, -1 },
253 .plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_NEED64K_PHYS,
255 .modifier = I915_FORMAT_MOD_4_TILED_MTL_MC_CCS,
256 .display_ver = { 14, 14 },
257 .plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_MC,
259 .ccs.packed_aux_planes = BIT(1),
260 .ccs.planar_aux_planes = BIT(2) | BIT(3),
262 FORMAT_OVERRIDE(gen12_ccs_formats),
264 .modifier = I915_FORMAT_MOD_4_TILED_MTL_RC_CCS,
265 .display_ver = { 14, 14 },
266 .plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_RC,
268 .ccs.packed_aux_planes = BIT(1),
270 FORMAT_OVERRIDE(gen12_ccs_formats),
272 .modifier = I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC,
273 .display_ver = { 14, 14 },
274 .plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_RC_CC,
276 .ccs.cc_planes = BIT(2),
277 .ccs.packed_aux_planes = BIT(1),
279 FORMAT_OVERRIDE(gen12_ccs_cc_formats),
281 .modifier = I915_FORMAT_MOD_4_TILED_DG2_MC_CCS,
282 .display_ver = { 13, 13 },
283 .plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_MC,
285 .modifier = I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC,
286 .display_ver = { 13, 13 },
287 .plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_RC_CC,
289 .ccs.cc_planes = BIT(1),
291 FORMAT_OVERRIDE(gen12_flat_ccs_cc_formats),
293 .modifier = I915_FORMAT_MOD_4_TILED_DG2_RC_CCS,
294 .display_ver = { 13, 13 },
295 .plane_caps = INTEL_PLANE_CAP_TILING_4 | INTEL_PLANE_CAP_CCS_RC,
297 .modifier = I915_FORMAT_MOD_4_TILED,
298 .display_ver = { 13, -1 },
299 .plane_caps = INTEL_PLANE_CAP_TILING_4,
301 .modifier = I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS,
302 .display_ver = { 12, 13 },
303 .plane_caps = INTEL_PLANE_CAP_TILING_Y | INTEL_PLANE_CAP_CCS_MC,
305 .ccs.packed_aux_planes = BIT(1),
306 .ccs.planar_aux_planes = BIT(2) | BIT(3),
308 FORMAT_OVERRIDE(gen12_ccs_formats),
310 .modifier = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,
311 .display_ver = { 12, 13 },
312 .plane_caps = INTEL_PLANE_CAP_TILING_Y | INTEL_PLANE_CAP_CCS_RC,
314 .ccs.packed_aux_planes = BIT(1),
316 FORMAT_OVERRIDE(gen12_ccs_formats),
318 .modifier = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC,
319 .display_ver = { 12, 13 },
320 .plane_caps = INTEL_PLANE_CAP_TILING_Y | INTEL_PLANE_CAP_CCS_RC_CC,
322 .ccs.cc_planes = BIT(2),
323 .ccs.packed_aux_planes = BIT(1),
325 FORMAT_OVERRIDE(gen12_ccs_cc_formats),
327 .modifier = I915_FORMAT_MOD_Yf_TILED_CCS,
328 .display_ver = { 9, 11 },
329 .plane_caps = INTEL_PLANE_CAP_TILING_Yf | INTEL_PLANE_CAP_CCS_RC,
331 .ccs.packed_aux_planes = BIT(1),
333 FORMAT_OVERRIDE(skl_ccs_formats),
335 .modifier = I915_FORMAT_MOD_Y_TILED_CCS,
336 .display_ver = { 9, 11 },
337 .plane_caps = INTEL_PLANE_CAP_TILING_Y | INTEL_PLANE_CAP_CCS_RC,
339 .ccs.packed_aux_planes = BIT(1),
341 FORMAT_OVERRIDE(skl_ccs_formats),
343 .modifier = I915_FORMAT_MOD_Yf_TILED,
344 .display_ver = { 9, 11 },
345 .plane_caps = INTEL_PLANE_CAP_TILING_Yf,
347 .modifier = I915_FORMAT_MOD_Y_TILED,
348 .display_ver = { 9, 13 },
349 .plane_caps = INTEL_PLANE_CAP_TILING_Y,
351 .modifier = I915_FORMAT_MOD_X_TILED,
352 .display_ver = { 0, 29 },
353 .plane_caps = INTEL_PLANE_CAP_TILING_X,
355 .modifier = DRM_FORMAT_MOD_LINEAR,
356 .display_ver = DISPLAY_VER_ALL,
360 static const struct intel_modifier_desc *lookup_modifier_or_null(u64 modifier)
364 for (i = 0; i < ARRAY_SIZE(intel_modifiers); i++)
365 if (intel_modifiers[i].modifier == modifier)
366 return &intel_modifiers[i];
371 static const struct intel_modifier_desc *lookup_modifier(u64 modifier)
373 const struct intel_modifier_desc *md = lookup_modifier_or_null(modifier);
376 return &intel_modifiers[0];
381 static const struct drm_format_info *
382 lookup_format_info(const struct drm_format_info formats[],
383 int num_formats, u32 format)
387 for (i = 0; i < num_formats; i++) {
388 if (formats[i].format == format)
395 unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
397 const struct intel_modifier_desc *md;
400 md = lookup_modifier_or_null(fb_modifier);
402 return I915_TILING_NONE;
404 tiling_caps = lookup_modifier_or_null(fb_modifier)->plane_caps &
405 INTEL_PLANE_CAP_TILING_MASK;
407 switch (tiling_caps) {
408 case INTEL_PLANE_CAP_TILING_Y:
409 return I915_TILING_Y;
410 case INTEL_PLANE_CAP_TILING_X:
411 return I915_TILING_X;
412 case INTEL_PLANE_CAP_TILING_4:
413 case INTEL_PLANE_CAP_TILING_Yf:
414 case INTEL_PLANE_CAP_TILING_NONE:
415 return I915_TILING_NONE;
417 MISSING_CASE(tiling_caps);
418 return I915_TILING_NONE;
423 * intel_fb_get_format_info: Get a modifier specific format information
424 * @cmd: FB add command structure
427 * Returns the format information for @cmd->pixel_format specific to @cmd->modifier[0],
428 * or %NULL if the modifier doesn't override the format.
430 const struct drm_format_info *
431 intel_fb_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
433 const struct intel_modifier_desc *md = lookup_modifier_or_null(cmd->modifier[0]);
435 if (!md || !md->formats)
438 return lookup_format_info(md->formats, md->format_count, cmd->pixel_format);
441 static bool plane_caps_contain_any(u8 caps, u8 mask)
446 static bool plane_caps_contain_all(u8 caps, u8 mask)
448 return (caps & mask) == mask;
452 * intel_fb_is_tiled_modifier: Check if a modifier is a tiled modifier type
453 * @modifier: Modifier to check
456 * Returns %true if @modifier is a tiled modifier.
458 bool intel_fb_is_tiled_modifier(u64 modifier)
460 return plane_caps_contain_any(lookup_modifier(modifier)->plane_caps,
461 INTEL_PLANE_CAP_TILING_MASK);
465 * intel_fb_is_ccs_modifier: Check if a modifier is a CCS modifier type
466 * @modifier: Modifier to check
469 * Returns %true if @modifier is a render, render with color clear or
470 * media compression modifier.
472 bool intel_fb_is_ccs_modifier(u64 modifier)
474 return plane_caps_contain_any(lookup_modifier(modifier)->plane_caps,
475 INTEL_PLANE_CAP_CCS_MASK);
479 * intel_fb_is_rc_ccs_cc_modifier: Check if a modifier is an RC CCS CC modifier type
480 * @modifier: Modifier to check
483 * Returns %true if @modifier is a render with color clear modifier.
485 bool intel_fb_is_rc_ccs_cc_modifier(u64 modifier)
487 return plane_caps_contain_any(lookup_modifier(modifier)->plane_caps,
488 INTEL_PLANE_CAP_CCS_RC_CC);
492 * intel_fb_is_mc_ccs_modifier: Check if a modifier is an MC CCS modifier type
493 * @modifier: Modifier to check
496 * Returns %true if @modifier is a media compression modifier.
498 bool intel_fb_is_mc_ccs_modifier(u64 modifier)
500 return plane_caps_contain_any(lookup_modifier(modifier)->plane_caps,
501 INTEL_PLANE_CAP_CCS_MC);
505 * intel_fb_needs_64k_phys: Check if modifier requires 64k physical placement.
506 * @modifier: Modifier to check
509 * Returns %true if @modifier requires 64k aligned physical pages.
511 bool intel_fb_needs_64k_phys(u64 modifier)
513 const struct intel_modifier_desc *md = lookup_modifier_or_null(modifier);
518 return plane_caps_contain_any(md->plane_caps,
519 INTEL_PLANE_CAP_NEED64K_PHYS);
523 * intel_fb_is_tile4_modifier: Check if a modifier is a tile4 modifier type
524 * @modifier: Modifier to check
527 * Returns %true if @modifier is a tile4 modifier.
529 bool intel_fb_is_tile4_modifier(u64 modifier)
531 return plane_caps_contain_any(lookup_modifier(modifier)->plane_caps,
532 INTEL_PLANE_CAP_TILING_4);
535 static bool check_modifier_display_ver_range(const struct intel_modifier_desc *md,
536 u8 display_ver_from, u8 display_ver_until)
538 return md->display_ver.from <= display_ver_until &&
539 display_ver_from <= md->display_ver.until;
542 static bool plane_has_modifier(struct drm_i915_private *i915,
544 const struct intel_modifier_desc *md)
546 if (!IS_DISPLAY_VER(i915, md->display_ver.from, md->display_ver.until))
549 if (!plane_caps_contain_all(plane_caps, md->plane_caps))
553 * Separate AuxCCS and Flat CCS modifiers to be run only on platforms
556 if (intel_fb_is_ccs_modifier(md->modifier) &&
557 HAS_FLAT_CCS(i915) != !md->ccs.packed_aux_planes)
560 if (md->modifier == I915_FORMAT_MOD_4_TILED_BMG_CCS &&
561 (GRAPHICS_VER(i915) < 20 || !IS_DGFX(i915)))
564 if (md->modifier == I915_FORMAT_MOD_4_TILED_LNL_CCS &&
565 (GRAPHICS_VER(i915) < 20 || IS_DGFX(i915)))
572 * intel_fb_plane_get_modifiers: Get the modifiers for the given platform and plane capabilities
573 * @i915: i915 device instance
574 * @plane_caps: capabilities for the plane the modifiers are queried for
577 * Returns the list of modifiers allowed by the @i915 platform and @plane_caps.
578 * The caller must free the returned buffer.
580 u64 *intel_fb_plane_get_modifiers(struct drm_i915_private *i915,
584 int count = 1; /* +1 for invalid modifier terminator */
587 for (i = 0; i < ARRAY_SIZE(intel_modifiers); i++) {
588 if (plane_has_modifier(i915, plane_caps, &intel_modifiers[i]))
592 list = kmalloc_array(count, sizeof(*list), GFP_KERNEL);
593 if (drm_WARN_ON(&i915->drm, !list))
597 for (i = 0; i < ARRAY_SIZE(intel_modifiers); i++) {
598 if (plane_has_modifier(i915, plane_caps, &intel_modifiers[i]))
599 *p++ = intel_modifiers[i].modifier;
601 *p++ = DRM_FORMAT_MOD_INVALID;
607 * intel_fb_plane_supports_modifier: Determine if a modifier is supported by the given plane
608 * @plane: Plane to check the modifier support for
609 * @modifier: The modifier to check the support for
612 * %true if the @modifier is supported on @plane.
614 bool intel_fb_plane_supports_modifier(struct intel_plane *plane, u64 modifier)
618 for (i = 0; i < plane->base.modifier_count; i++)
619 if (plane->base.modifiers[i] == modifier)
625 static bool format_is_yuv_semiplanar(const struct intel_modifier_desc *md,
626 const struct drm_format_info *info)
631 if (hweight8(md->ccs.planar_aux_planes) == 2)
632 return info->num_planes == 4;
634 return info->num_planes == 2;
638 * intel_format_info_is_yuv_semiplanar: Check if the given format is YUV semiplanar
639 * @info: format to check
640 * @modifier: modifier used with the format
643 * %true if @info / @modifier is YUV semiplanar.
645 bool intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
648 return format_is_yuv_semiplanar(lookup_modifier(modifier), info);
651 static u8 ccs_aux_plane_mask(const struct intel_modifier_desc *md,
652 const struct drm_format_info *format)
654 if (format_is_yuv_semiplanar(md, format))
655 return md->ccs.planar_aux_planes;
657 return md->ccs.packed_aux_planes;
661 * intel_fb_is_ccs_aux_plane: Check if a framebuffer color plane is a CCS AUX plane
663 * @color_plane: color plane index to check
666 * Returns %true if @fb's color plane at index @color_plane is a CCS AUX plane.
668 bool intel_fb_is_ccs_aux_plane(const struct drm_framebuffer *fb, int color_plane)
670 const struct intel_modifier_desc *md = lookup_modifier(fb->modifier);
672 return ccs_aux_plane_mask(md, fb->format) & BIT(color_plane);
676 * intel_fb_is_gen12_ccs_aux_plane: Check if a framebuffer color plane is a GEN12 CCS AUX plane
678 * @color_plane: color plane index to check
681 * Returns %true if @fb's color plane at index @color_plane is a GEN12 CCS AUX plane.
683 static bool intel_fb_is_gen12_ccs_aux_plane(const struct drm_framebuffer *fb, int color_plane)
685 const struct intel_modifier_desc *md = lookup_modifier(fb->modifier);
687 return check_modifier_display_ver_range(md, 12, 14) &&
688 ccs_aux_plane_mask(md, fb->format) & BIT(color_plane);
692 * intel_fb_rc_ccs_cc_plane: Get the CCS CC color plane index for a framebuffer
696 * Returns the index of the color clear plane for @fb, or -1 if @fb is not a
697 * framebuffer using a render compression/color clear modifier.
699 int intel_fb_rc_ccs_cc_plane(const struct drm_framebuffer *fb)
701 const struct intel_modifier_desc *md = lookup_modifier(fb->modifier);
703 if (!md->ccs.cc_planes)
706 drm_WARN_ON_ONCE(fb->dev, hweight8(md->ccs.cc_planes) > 1);
708 return ilog2((int)md->ccs.cc_planes);
711 static bool is_gen12_ccs_cc_plane(const struct drm_framebuffer *fb, int color_plane)
713 return intel_fb_rc_ccs_cc_plane(fb) == color_plane;
716 bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
718 return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
719 intel_fb_is_gen12_ccs_aux_plane(fb, color_plane) ||
720 is_gen12_ccs_cc_plane(fb, color_plane);
723 int main_to_ccs_plane(const struct drm_framebuffer *fb, int main_plane)
725 drm_WARN_ON(fb->dev, !intel_fb_is_ccs_modifier(fb->modifier) ||
726 (main_plane && main_plane >= fb->format->num_planes / 2));
728 return fb->format->num_planes / 2 + main_plane;
731 int skl_ccs_to_main_plane(const struct drm_framebuffer *fb, int ccs_plane)
733 drm_WARN_ON(fb->dev, !intel_fb_is_ccs_modifier(fb->modifier) ||
734 ccs_plane < fb->format->num_planes / 2);
736 if (is_gen12_ccs_cc_plane(fb, ccs_plane))
739 return ccs_plane - fb->format->num_planes / 2;
742 static unsigned int gen12_ccs_aux_stride(struct intel_framebuffer *fb, int ccs_plane)
744 int main_plane = skl_ccs_to_main_plane(&fb->base, ccs_plane);
745 unsigned int main_stride = fb->base.pitches[main_plane];
746 unsigned int main_tile_width = intel_tile_width_bytes(&fb->base, main_plane);
748 return DIV_ROUND_UP(main_stride, 4 * main_tile_width) * 64;
751 int skl_main_to_aux_plane(const struct drm_framebuffer *fb, int main_plane)
753 const struct intel_modifier_desc *md = lookup_modifier(fb->modifier);
754 struct drm_i915_private *i915 = to_i915(fb->dev);
756 if (md->ccs.packed_aux_planes | md->ccs.planar_aux_planes)
757 return main_to_ccs_plane(fb, main_plane);
758 else if (DISPLAY_VER(i915) < 11 &&
759 format_is_yuv_semiplanar(md, fb->format))
765 unsigned int intel_tile_size(const struct drm_i915_private *i915)
767 return DISPLAY_VER(i915) == 2 ? 2048 : 4096;
771 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
773 struct drm_i915_private *dev_priv = to_i915(fb->dev);
774 unsigned int cpp = fb->format->cpp[color_plane];
776 switch (fb->modifier) {
777 case DRM_FORMAT_MOD_LINEAR:
778 return intel_tile_size(dev_priv);
779 case I915_FORMAT_MOD_X_TILED:
780 if (DISPLAY_VER(dev_priv) == 2)
784 case I915_FORMAT_MOD_4_TILED_BMG_CCS:
785 case I915_FORMAT_MOD_4_TILED_LNL_CCS:
786 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
787 case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
788 case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
789 case I915_FORMAT_MOD_4_TILED:
791 * Each 4K tile consists of 64B(8*8) subtiles, with
792 * same shape as Y Tile(i.e 4*16B OWords)
795 case I915_FORMAT_MOD_Y_TILED_CCS:
796 if (intel_fb_is_ccs_aux_plane(fb, color_plane))
799 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS:
800 case I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC:
801 case I915_FORMAT_MOD_4_TILED_MTL_MC_CCS:
802 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
803 case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
804 case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
805 if (intel_fb_is_ccs_aux_plane(fb, color_plane) ||
806 is_gen12_ccs_cc_plane(fb, color_plane))
809 case I915_FORMAT_MOD_Y_TILED:
810 if (DISPLAY_VER(dev_priv) == 2 || HAS_128_BYTE_Y_TILING(dev_priv))
814 case I915_FORMAT_MOD_Yf_TILED_CCS:
815 if (intel_fb_is_ccs_aux_plane(fb, color_plane))
818 case I915_FORMAT_MOD_Yf_TILED:
834 MISSING_CASE(fb->modifier);
839 unsigned int intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
841 return intel_tile_size(to_i915(fb->dev)) /
842 intel_tile_width_bytes(fb, color_plane);
846 * Return the tile dimensions in pixel units, based on the (2 or 4 kbyte) GTT
849 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
850 unsigned int *tile_width,
851 unsigned int *tile_height)
853 unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
854 unsigned int cpp = fb->format->cpp[color_plane];
856 *tile_width = tile_width_bytes / cpp;
857 *tile_height = intel_tile_height(fb, color_plane);
861 * Return the tile dimensions in pixel units, based on the tile block size.
862 * The block covers the full GTT page sized tile on all tiled surfaces and
863 * it's a 64 byte portion of the tile on TGL+ CCS surfaces.
865 static void intel_tile_block_dims(const struct drm_framebuffer *fb, int color_plane,
866 unsigned int *tile_width,
867 unsigned int *tile_height)
869 intel_tile_dims(fb, color_plane, tile_width, tile_height);
871 if (intel_fb_is_gen12_ccs_aux_plane(fb, color_plane))
875 unsigned int intel_tile_row_size(const struct drm_framebuffer *fb, int color_plane)
877 unsigned int tile_width, tile_height;
879 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
881 return fb->pitches[color_plane] * tile_height;
885 intel_fb_align_height(const struct drm_framebuffer *fb,
886 int color_plane, unsigned int height)
888 unsigned int tile_height = intel_tile_height(fb, color_plane);
890 return ALIGN(height, tile_height);
893 bool intel_fb_modifier_uses_dpt(struct drm_i915_private *i915, u64 modifier)
895 return HAS_DPT(i915) && modifier != DRM_FORMAT_MOD_LINEAR;
898 bool intel_fb_uses_dpt(const struct drm_framebuffer *fb)
900 return to_i915(fb->dev)->display.params.enable_dpt &&
901 intel_fb_modifier_uses_dpt(to_i915(fb->dev), fb->modifier);
904 void intel_fb_plane_get_subsampling(int *hsub, int *vsub,
905 const struct drm_framebuffer *fb,
910 if (color_plane == 0) {
918 * TODO: Deduct the subsampling from the char block for all CCS
919 * formats and planes.
921 if (!intel_fb_is_gen12_ccs_aux_plane(fb, color_plane)) {
922 *hsub = fb->format->hsub;
923 *vsub = fb->format->vsub;
928 main_plane = skl_ccs_to_main_plane(fb, color_plane);
929 *hsub = drm_format_info_block_width(fb->format, color_plane) /
930 drm_format_info_block_width(fb->format, main_plane);
933 * The min stride check in the core framebuffer_check() function
934 * assumes that format->hsub applies to every plane except for the
935 * first plane. That's incorrect for the CCS AUX plane of the first
936 * plane, but for the above check to pass we must define the block
937 * width with that subsampling applied to it. Adjust the width here
938 * accordingly, so we can calculate the actual subsampling factor.
941 *hsub *= fb->format->hsub;
946 static void intel_fb_plane_dims(const struct intel_framebuffer *fb, int color_plane, int *w, int *h)
948 int main_plane = intel_fb_is_ccs_aux_plane(&fb->base, color_plane) ?
949 skl_ccs_to_main_plane(&fb->base, color_plane) : 0;
950 unsigned int main_width = fb->base.width;
951 unsigned int main_height = fb->base.height;
952 int main_hsub, main_vsub;
955 intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, &fb->base, main_plane);
956 intel_fb_plane_get_subsampling(&hsub, &vsub, &fb->base, color_plane);
958 *w = DIV_ROUND_UP(main_width, main_hsub * hsub);
959 *h = DIV_ROUND_UP(main_height, main_vsub * vsub);
962 static u32 intel_adjust_tile_offset(int *x, int *y,
963 unsigned int tile_width,
964 unsigned int tile_height,
965 unsigned int tile_size,
966 unsigned int pitch_tiles,
970 unsigned int pitch_pixels = pitch_tiles * tile_width;
973 WARN_ON(old_offset & (tile_size - 1));
974 WARN_ON(new_offset & (tile_size - 1));
975 WARN_ON(new_offset > old_offset);
977 tiles = (old_offset - new_offset) / tile_size;
979 *y += tiles / pitch_tiles * tile_height;
980 *x += tiles % pitch_tiles * tile_width;
982 /* minimize x in case it got needlessly big */
983 *y += *x / pitch_pixels * tile_height;
989 static u32 intel_adjust_linear_offset(int *x, int *y,
995 old_offset += *y * pitch + *x * cpp;
997 *y = (old_offset - new_offset) / pitch;
998 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
1003 static u32 intel_adjust_aligned_offset(int *x, int *y,
1004 const struct drm_framebuffer *fb,
1006 unsigned int rotation,
1008 u32 old_offset, u32 new_offset)
1010 struct drm_i915_private *i915 = to_i915(fb->dev);
1011 unsigned int cpp = fb->format->cpp[color_plane];
1013 drm_WARN_ON(&i915->drm, new_offset > old_offset);
1015 if (!is_surface_linear(fb, color_plane)) {
1016 unsigned int tile_size, tile_width, tile_height;
1017 unsigned int pitch_tiles;
1019 tile_size = intel_tile_size(i915);
1020 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
1022 if (drm_rotation_90_or_270(rotation)) {
1023 pitch_tiles = pitch / tile_height;
1024 swap(tile_width, tile_height);
1026 pitch_tiles = pitch / (tile_width * cpp);
1029 intel_adjust_tile_offset(x, y, tile_width, tile_height,
1030 tile_size, pitch_tiles,
1031 old_offset, new_offset);
1033 intel_adjust_linear_offset(x, y, cpp, pitch,
1034 old_offset, new_offset);
1041 * Adjust the tile offset by moving the difference into
1044 u32 intel_plane_adjust_aligned_offset(int *x, int *y,
1045 const struct intel_plane_state *state,
1047 u32 old_offset, u32 new_offset)
1049 return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
1051 state->view.color_plane[color_plane].mapping_stride,
1052 old_offset, new_offset);
1056 * Computes the aligned offset to the base tile and adjusts
1057 * x, y. bytes per pixel is assumed to be a power-of-two.
1059 * In the 90/270 rotated case, x and y are assumed
1060 * to be already rotated to match the rotated GTT view, and
1061 * pitch is the tile_height aligned framebuffer height.
1063 * This function is used when computing the derived information
1064 * under intel_framebuffer, so using any of that information
1065 * here is not allowed. Anything under drm_framebuffer can be
1066 * used. This is why the user has to pass in the pitch since it
1067 * is specified in the rotated orientation.
1069 static u32 intel_compute_aligned_offset(struct drm_i915_private *i915,
1071 const struct drm_framebuffer *fb,
1074 unsigned int rotation,
1075 unsigned int alignment)
1077 unsigned int cpp = fb->format->cpp[color_plane];
1078 u32 offset, offset_aligned;
1080 if (!is_surface_linear(fb, color_plane)) {
1081 unsigned int tile_size, tile_width, tile_height;
1082 unsigned int tile_rows, tiles, pitch_tiles;
1084 tile_size = intel_tile_size(i915);
1085 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
1087 if (drm_rotation_90_or_270(rotation)) {
1088 pitch_tiles = pitch / tile_height;
1089 swap(tile_width, tile_height);
1091 pitch_tiles = pitch / (tile_width * cpp);
1094 tile_rows = *y / tile_height;
1097 tiles = *x / tile_width;
1100 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
1102 offset_aligned = offset;
1104 offset_aligned = rounddown(offset_aligned, alignment);
1106 intel_adjust_tile_offset(x, y, tile_width, tile_height,
1107 tile_size, pitch_tiles,
1108 offset, offset_aligned);
1110 offset = *y * pitch + *x * cpp;
1111 offset_aligned = offset;
1113 offset_aligned = rounddown(offset_aligned, alignment);
1114 *y = (offset % alignment) / pitch;
1115 *x = ((offset % alignment) - *y * pitch) / cpp;
1121 return offset_aligned;
1124 u32 intel_plane_compute_aligned_offset(int *x, int *y,
1125 const struct intel_plane_state *state,
1128 struct intel_plane *plane = to_intel_plane(state->uapi.plane);
1129 struct drm_i915_private *i915 = to_i915(plane->base.dev);
1130 const struct drm_framebuffer *fb = state->hw.fb;
1131 unsigned int rotation = state->hw.rotation;
1132 unsigned int pitch = state->view.color_plane[color_plane].mapping_stride;
1133 unsigned int alignment = plane->min_alignment(plane, fb, color_plane);
1135 return intel_compute_aligned_offset(i915, x, y, fb, color_plane,
1136 pitch, rotation, alignment);
1139 /* Convert the fb->offset[] into x/y offsets */
1140 static int intel_fb_offset_to_xy(int *x, int *y,
1141 const struct drm_framebuffer *fb,
1144 struct drm_i915_private *i915 = to_i915(fb->dev);
1145 unsigned int height, alignment, unused;
1147 if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
1148 alignment = intel_tile_size(i915);
1152 if (alignment != 0 && fb->offsets[color_plane] % alignment) {
1153 drm_dbg_kms(&i915->drm,
1154 "Misaligned offset 0x%08x for color plane %d\n",
1155 fb->offsets[color_plane], color_plane);
1159 height = drm_format_info_plane_height(fb->format, fb->height, color_plane);
1160 height = ALIGN(height, intel_tile_height(fb, color_plane));
1162 /* Catch potential overflows early */
1163 if (check_add_overflow(mul_u32_u32(height, fb->pitches[color_plane]),
1164 fb->offsets[color_plane], &unused)) {
1165 drm_dbg_kms(&i915->drm,
1166 "Bad offset 0x%08x or pitch %d for color plane %d\n",
1167 fb->offsets[color_plane], fb->pitches[color_plane],
1175 intel_adjust_aligned_offset(x, y,
1176 fb, color_plane, DRM_MODE_ROTATE_0,
1177 fb->pitches[color_plane],
1178 fb->offsets[color_plane], 0);
1183 static int intel_fb_check_ccs_xy(const struct drm_framebuffer *fb, int ccs_plane, int x, int y)
1185 struct drm_i915_private *i915 = to_i915(fb->dev);
1186 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1189 int tile_width, tile_height;
1193 if (!intel_fb_is_ccs_aux_plane(fb, ccs_plane))
1197 * While all the tile dimensions are based on a 2k or 4k GTT page size
1198 * here the main and CCS coordinates must match only within a (64 byte
1199 * on TGL+) block inside the tile.
1201 intel_tile_block_dims(fb, ccs_plane, &tile_width, &tile_height);
1202 intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
1205 tile_height *= vsub;
1207 ccs_x = (x * hsub) % tile_width;
1208 ccs_y = (y * vsub) % tile_height;
1210 main_plane = skl_ccs_to_main_plane(fb, ccs_plane);
1211 main_x = intel_fb->normal_view.color_plane[main_plane].x % tile_width;
1212 main_y = intel_fb->normal_view.color_plane[main_plane].y % tile_height;
1215 * CCS doesn't have its own x/y offset register, so the intra CCS tile
1216 * x/y offsets must match between CCS and the main surface.
1218 if (main_x != ccs_x || main_y != ccs_y) {
1219 drm_dbg_kms(&i915->drm,
1220 "Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
1223 intel_fb->normal_view.color_plane[main_plane].x,
1224 intel_fb->normal_view.color_plane[main_plane].y,
1232 static bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
1234 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1235 struct drm_i915_private *i915 = to_i915(plane->base.dev);
1236 const struct drm_framebuffer *fb = plane_state->hw.fb;
1239 /* We don't want to deal with remapping with cursors */
1240 if (plane->id == PLANE_CURSOR)
1244 * The display engine limits already match/exceed the
1245 * render engine limits, so not much point in remapping.
1246 * Would also need to deal with the fence POT alignment
1247 * and gen2 2KiB GTT tile size.
1249 if (DISPLAY_VER(i915) < 4)
1253 * The new CCS hash mode isn't compatible with remapping as
1254 * the virtual address of the pages affects the compressed data.
1256 if (intel_fb_is_ccs_modifier(fb->modifier))
1259 /* Linear needs a page aligned stride for remapping */
1260 if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
1261 unsigned int alignment = intel_tile_size(i915) - 1;
1263 for (i = 0; i < fb->format->num_planes; i++) {
1264 if (fb->pitches[i] & alignment)
1272 bool intel_fb_needs_pot_stride_remap(const struct intel_framebuffer *fb)
1274 struct drm_i915_private *i915 = to_i915(fb->base.dev);
1276 return (IS_ALDERLAKE_P(i915) || DISPLAY_VER(i915) >= 14) &&
1277 intel_fb_uses_dpt(&fb->base);
1280 static int intel_fb_pitch(const struct intel_framebuffer *fb, int color_plane, unsigned int rotation)
1282 if (drm_rotation_90_or_270(rotation))
1283 return fb->rotated_view.color_plane[color_plane].mapping_stride;
1284 else if (intel_fb_needs_pot_stride_remap(fb))
1285 return fb->remapped_view.color_plane[color_plane].mapping_stride;
1287 return fb->normal_view.color_plane[color_plane].mapping_stride;
1290 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
1292 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1293 const struct intel_framebuffer *fb = to_intel_framebuffer(plane_state->hw.fb);
1294 unsigned int rotation = plane_state->hw.rotation;
1295 u32 stride, max_stride;
1298 * No remapping for invisible planes since we don't have
1299 * an actual source viewport to remap.
1301 if (!plane_state->uapi.visible)
1304 if (!intel_plane_can_remap(plane_state))
1308 * FIXME: aux plane limits on gen9+ are
1309 * unclear in Bspec, for now no checking.
1311 stride = intel_fb_pitch(fb, 0, rotation);
1312 max_stride = plane->max_stride(plane, fb->base.format->format,
1313 fb->base.modifier, rotation);
1315 return stride > max_stride;
1318 static int convert_plane_offset_to_xy(const struct intel_framebuffer *fb, int color_plane,
1319 int plane_width, int *x, int *y)
1321 struct drm_gem_object *obj = intel_fb_bo(&fb->base);
1324 ret = intel_fb_offset_to_xy(x, y, &fb->base, color_plane);
1326 drm_dbg_kms(fb->base.dev,
1327 "bad fb plane %d offset: 0x%x\n",
1328 color_plane, fb->base.offsets[color_plane]);
1332 ret = intel_fb_check_ccs_xy(&fb->base, color_plane, *x, *y);
1337 * The fence (if used) is aligned to the start of the object
1338 * so having the framebuffer wrap around across the edge of the
1339 * fenced region doesn't really work. We have no API to configure
1340 * the fence start offset within the object (nor could we probably
1341 * on gen2/3). So it's just easier if we just require that the
1342 * fb layout agrees with the fence layout. We already check that the
1343 * fb stride matches the fence stride elsewhere.
1345 if (color_plane == 0 && intel_bo_is_tiled(obj) &&
1346 (*x + plane_width) * fb->base.format->cpp[color_plane] > fb->base.pitches[color_plane]) {
1347 drm_dbg_kms(fb->base.dev,
1348 "bad fb plane %d offset: 0x%x\n",
1349 color_plane, fb->base.offsets[color_plane]);
1356 static u32 calc_plane_aligned_offset(const struct intel_framebuffer *fb, int color_plane, int *x, int *y)
1358 struct drm_i915_private *i915 = to_i915(fb->base.dev);
1359 unsigned int tile_size = intel_tile_size(i915);
1362 offset = intel_compute_aligned_offset(i915, x, y, &fb->base, color_plane,
1363 fb->base.pitches[color_plane],
1367 return offset / tile_size;
1370 struct fb_plane_view_dims {
1371 unsigned int width, height;
1372 unsigned int tile_width, tile_height;
1375 static void init_plane_view_dims(const struct intel_framebuffer *fb, int color_plane,
1376 unsigned int width, unsigned int height,
1377 struct fb_plane_view_dims *dims)
1379 dims->width = width;
1380 dims->height = height;
1382 intel_tile_dims(&fb->base, color_plane, &dims->tile_width, &dims->tile_height);
1386 plane_view_src_stride_tiles(const struct intel_framebuffer *fb, int color_plane,
1387 const struct fb_plane_view_dims *dims)
1389 return DIV_ROUND_UP(fb->base.pitches[color_plane],
1390 dims->tile_width * fb->base.format->cpp[color_plane]);
1394 plane_view_dst_stride_tiles(const struct intel_framebuffer *fb, int color_plane,
1395 unsigned int pitch_tiles)
1397 if (intel_fb_needs_pot_stride_remap(fb)) {
1399 * ADL_P, the only platform needing a POT stride has a minimum
1400 * of 8 main surface tiles.
1402 return roundup_pow_of_two(max(pitch_tiles, 8u));
1409 plane_view_scanout_stride(const struct intel_framebuffer *fb, int color_plane,
1410 unsigned int tile_width,
1411 unsigned int src_stride_tiles, unsigned int dst_stride_tiles)
1413 struct drm_i915_private *i915 = to_i915(fb->base.dev);
1414 unsigned int stride_tiles;
1416 if ((IS_ALDERLAKE_P(i915) || DISPLAY_VER(i915) >= 14) &&
1417 src_stride_tiles < dst_stride_tiles)
1418 stride_tiles = src_stride_tiles;
1420 stride_tiles = dst_stride_tiles;
1422 return stride_tiles * tile_width * fb->base.format->cpp[color_plane];
1426 plane_view_width_tiles(const struct intel_framebuffer *fb, int color_plane,
1427 const struct fb_plane_view_dims *dims,
1430 return DIV_ROUND_UP(x + dims->width, dims->tile_width);
1434 plane_view_height_tiles(const struct intel_framebuffer *fb, int color_plane,
1435 const struct fb_plane_view_dims *dims,
1438 return DIV_ROUND_UP(y + dims->height, dims->tile_height);
1442 plane_view_linear_tiles(const struct intel_framebuffer *fb, int color_plane,
1443 const struct fb_plane_view_dims *dims,
1446 struct drm_i915_private *i915 = to_i915(fb->base.dev);
1449 size = (y + dims->height) * fb->base.pitches[color_plane] +
1450 x * fb->base.format->cpp[color_plane];
1452 return DIV_ROUND_UP(size, intel_tile_size(i915));
1455 #define assign_chk_ovf(i915, var, val) ({ \
1456 drm_WARN_ON(&(i915)->drm, overflows_type(val, var)); \
1460 #define assign_bfld_chk_ovf(i915, var, val) ({ \
1462 drm_WARN_ON(&(i915)->drm, (var) != (val)); \
1466 static u32 calc_plane_remap_info(const struct intel_framebuffer *fb, int color_plane,
1467 const struct fb_plane_view_dims *dims,
1468 u32 obj_offset, u32 gtt_offset, int x, int y,
1469 struct intel_fb_view *view)
1471 struct drm_i915_private *i915 = to_i915(fb->base.dev);
1472 struct intel_remapped_plane_info *remap_info = &view->gtt.remapped.plane[color_plane];
1473 struct i915_color_plane_view *color_plane_info = &view->color_plane[color_plane];
1474 unsigned int tile_width = dims->tile_width;
1475 unsigned int tile_height = dims->tile_height;
1476 unsigned int tile_size = intel_tile_size(i915);
1480 assign_bfld_chk_ovf(i915, remap_info->offset, obj_offset);
1482 if (intel_fb_is_gen12_ccs_aux_plane(&fb->base, color_plane)) {
1483 remap_info->linear = 1;
1485 assign_chk_ovf(i915, remap_info->size,
1486 plane_view_linear_tiles(fb, color_plane, dims, x, y));
1488 remap_info->linear = 0;
1490 assign_chk_ovf(i915, remap_info->src_stride,
1491 plane_view_src_stride_tiles(fb, color_plane, dims));
1492 assign_chk_ovf(i915, remap_info->width,
1493 plane_view_width_tiles(fb, color_plane, dims, x));
1494 assign_chk_ovf(i915, remap_info->height,
1495 plane_view_height_tiles(fb, color_plane, dims, y));
1498 if (view->gtt.type == I915_GTT_VIEW_ROTATED) {
1499 drm_WARN_ON(&i915->drm, remap_info->linear);
1500 check_array_bounds(i915, view->gtt.rotated.plane, color_plane);
1502 assign_chk_ovf(i915, remap_info->dst_stride,
1503 plane_view_dst_stride_tiles(fb, color_plane, remap_info->height));
1505 /* rotate the x/y offsets to match the GTT view */
1506 drm_rect_init(&r, x, y, dims->width, dims->height);
1508 remap_info->width * tile_width,
1509 remap_info->height * tile_height,
1510 DRM_MODE_ROTATE_270);
1512 color_plane_info->x = r.x1;
1513 color_plane_info->y = r.y1;
1515 color_plane_info->mapping_stride = remap_info->dst_stride * tile_height;
1516 color_plane_info->scanout_stride = color_plane_info->mapping_stride;
1518 size += remap_info->dst_stride * remap_info->width;
1520 /* rotate the tile dimensions to match the GTT view */
1521 swap(tile_width, tile_height);
1523 drm_WARN_ON(&i915->drm, view->gtt.type != I915_GTT_VIEW_REMAPPED);
1525 check_array_bounds(i915, view->gtt.remapped.plane, color_plane);
1527 if (view->gtt.remapped.plane_alignment) {
1528 u32 aligned_offset = ALIGN(gtt_offset,
1529 view->gtt.remapped.plane_alignment);
1531 size += aligned_offset - gtt_offset;
1532 gtt_offset = aligned_offset;
1535 color_plane_info->x = x;
1536 color_plane_info->y = y;
1538 if (remap_info->linear) {
1539 color_plane_info->mapping_stride = fb->base.pitches[color_plane];
1540 color_plane_info->scanout_stride = color_plane_info->mapping_stride;
1542 size += remap_info->size;
1544 unsigned int dst_stride;
1547 * The hardware automagically calculates the CCS AUX surface
1548 * stride from the main surface stride so can't really remap a
1549 * smaller subset (unless we'd remap in whole AUX page units).
1551 if (intel_fb_needs_pot_stride_remap(fb) &&
1552 intel_fb_is_ccs_modifier(fb->base.modifier))
1553 dst_stride = remap_info->src_stride;
1555 dst_stride = remap_info->width;
1557 dst_stride = plane_view_dst_stride_tiles(fb, color_plane, dst_stride);
1559 assign_chk_ovf(i915, remap_info->dst_stride, dst_stride);
1560 color_plane_info->mapping_stride = dst_stride *
1562 fb->base.format->cpp[color_plane];
1563 color_plane_info->scanout_stride =
1564 plane_view_scanout_stride(fb, color_plane, tile_width,
1565 remap_info->src_stride,
1568 size += dst_stride * remap_info->height;
1573 * We only keep the x/y offsets, so push all of the gtt offset into
1574 * the x/y offsets. x,y will hold the first pixel of the framebuffer
1575 * plane from the start of the remapped/rotated gtt mapping.
1577 if (remap_info->linear)
1578 intel_adjust_linear_offset(&color_plane_info->x, &color_plane_info->y,
1579 fb->base.format->cpp[color_plane],
1580 color_plane_info->mapping_stride,
1581 gtt_offset * tile_size, 0);
1583 intel_adjust_tile_offset(&color_plane_info->x, &color_plane_info->y,
1584 tile_width, tile_height,
1585 tile_size, remap_info->dst_stride,
1586 gtt_offset * tile_size, 0);
1591 #undef assign_chk_ovf
1593 /* Return number of tiles @color_plane needs. */
1595 calc_plane_normal_size(const struct intel_framebuffer *fb, int color_plane,
1596 const struct fb_plane_view_dims *dims,
1601 if (is_surface_linear(&fb->base, color_plane)) {
1602 tiles = plane_view_linear_tiles(fb, color_plane, dims, x, y);
1604 tiles = plane_view_src_stride_tiles(fb, color_plane, dims) *
1605 plane_view_height_tiles(fb, color_plane, dims, y);
1607 * If the plane isn't horizontally tile aligned,
1608 * we need one more tile.
1617 static void intel_fb_view_init(struct drm_i915_private *i915, struct intel_fb_view *view,
1618 enum i915_gtt_view_type view_type)
1620 memset(view, 0, sizeof(*view));
1621 view->gtt.type = view_type;
1623 if (view_type == I915_GTT_VIEW_REMAPPED &&
1624 (IS_ALDERLAKE_P(i915) || DISPLAY_VER(i915) >= 14))
1625 view->gtt.remapped.plane_alignment = SZ_2M / PAGE_SIZE;
1628 bool intel_fb_supports_90_270_rotation(const struct intel_framebuffer *fb)
1630 if (DISPLAY_VER(to_i915(fb->base.dev)) >= 13)
1633 return fb->base.modifier == I915_FORMAT_MOD_Y_TILED ||
1634 fb->base.modifier == I915_FORMAT_MOD_Yf_TILED;
1637 static unsigned int intel_fb_min_alignment(const struct drm_framebuffer *fb)
1639 struct drm_i915_private *i915 = to_i915(fb->dev);
1640 struct intel_plane *plane;
1641 unsigned int min_alignment = 0;
1643 for_each_intel_plane(&i915->drm, plane) {
1644 unsigned int plane_min_alignment;
1646 if (!drm_plane_has_format(&plane->base, fb->format->format, fb->modifier))
1649 plane_min_alignment = plane->min_alignment(plane, fb, 0);
1651 drm_WARN_ON(&i915->drm, plane_min_alignment &&
1652 !is_power_of_2(plane_min_alignment));
1654 if (intel_plane_needs_physical(plane))
1657 min_alignment = max(min_alignment, plane_min_alignment);
1660 return min_alignment;
1663 int intel_fill_fb_info(struct drm_i915_private *i915, struct intel_framebuffer *fb)
1665 struct drm_gem_object *obj = intel_fb_bo(&fb->base);
1666 u32 gtt_offset_rotated = 0;
1667 u32 gtt_offset_remapped = 0;
1668 unsigned int max_size = 0;
1669 int i, num_planes = fb->base.format->num_planes;
1670 unsigned int tile_size = intel_tile_size(i915);
1672 intel_fb_view_init(i915, &fb->normal_view, I915_GTT_VIEW_NORMAL);
1674 drm_WARN_ON(&i915->drm,
1675 intel_fb_supports_90_270_rotation(fb) &&
1676 intel_fb_needs_pot_stride_remap(fb));
1678 if (intel_fb_supports_90_270_rotation(fb))
1679 intel_fb_view_init(i915, &fb->rotated_view, I915_GTT_VIEW_ROTATED);
1680 if (intel_fb_needs_pot_stride_remap(fb))
1681 intel_fb_view_init(i915, &fb->remapped_view, I915_GTT_VIEW_REMAPPED);
1683 for (i = 0; i < num_planes; i++) {
1684 struct fb_plane_view_dims view_dims;
1685 unsigned int width, height;
1692 * Plane 2 of Render Compression with Clear Color fb modifier
1693 * is consumed by the driver and not passed to DE. Skip the
1694 * arithmetic related to alignment and offset calculation.
1696 if (is_gen12_ccs_cc_plane(&fb->base, i)) {
1697 if (IS_ALIGNED(fb->base.offsets[i], PAGE_SIZE))
1703 intel_fb_plane_dims(fb, i, &width, &height);
1705 ret = convert_plane_offset_to_xy(fb, i, width, &x, &y);
1709 init_plane_view_dims(fb, i, width, height, &view_dims);
1712 * First pixel of the framebuffer from
1713 * the start of the normal gtt mapping.
1715 fb->normal_view.color_plane[i].x = x;
1716 fb->normal_view.color_plane[i].y = y;
1717 fb->normal_view.color_plane[i].mapping_stride = fb->base.pitches[i];
1718 fb->normal_view.color_plane[i].scanout_stride =
1719 fb->normal_view.color_plane[i].mapping_stride;
1721 offset = calc_plane_aligned_offset(fb, i, &x, &y);
1723 if (intel_fb_supports_90_270_rotation(fb))
1724 gtt_offset_rotated += calc_plane_remap_info(fb, i, &view_dims,
1725 offset, gtt_offset_rotated, x, y,
1728 if (intel_fb_needs_pot_stride_remap(fb))
1729 gtt_offset_remapped += calc_plane_remap_info(fb, i, &view_dims,
1730 offset, gtt_offset_remapped, x, y,
1731 &fb->remapped_view);
1733 size = calc_plane_normal_size(fb, i, &view_dims, x, y);
1734 /* how many tiles in total needed in the bo */
1735 max_size = max(max_size, offset + size);
1738 if (mul_u32_u32(max_size, tile_size) > obj->size) {
1739 drm_dbg_kms(&i915->drm,
1740 "fb too big for bo (need %llu bytes, have %zu bytes)\n",
1741 mul_u32_u32(max_size, tile_size), obj->size);
1745 fb->min_alignment = intel_fb_min_alignment(&fb->base);
1750 static void intel_plane_remap_gtt(struct intel_plane_state *plane_state)
1752 struct drm_i915_private *i915 =
1753 to_i915(plane_state->uapi.plane->dev);
1754 struct drm_framebuffer *fb = plane_state->hw.fb;
1755 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1756 unsigned int rotation = plane_state->hw.rotation;
1757 int i, num_planes = fb->format->num_planes;
1758 unsigned int src_x, src_y;
1759 unsigned int src_w, src_h;
1762 intel_fb_view_init(i915, &plane_state->view,
1763 drm_rotation_90_or_270(rotation) ? I915_GTT_VIEW_ROTATED :
1764 I915_GTT_VIEW_REMAPPED);
1766 src_x = plane_state->uapi.src.x1 >> 16;
1767 src_y = plane_state->uapi.src.y1 >> 16;
1768 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
1769 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
1771 drm_WARN_ON(&i915->drm, intel_fb_is_ccs_modifier(fb->modifier));
1773 /* Make src coordinates relative to the viewport */
1774 drm_rect_translate(&plane_state->uapi.src,
1775 -(src_x << 16), -(src_y << 16));
1777 /* Rotate src coordinates to match rotated GTT view */
1778 if (drm_rotation_90_or_270(rotation))
1779 drm_rect_rotate(&plane_state->uapi.src,
1780 src_w << 16, src_h << 16,
1781 DRM_MODE_ROTATE_270);
1783 for (i = 0; i < num_planes; i++) {
1784 unsigned int hsub = i ? fb->format->hsub : 1;
1785 unsigned int vsub = i ? fb->format->vsub : 1;
1786 struct fb_plane_view_dims view_dims;
1787 unsigned int width, height;
1793 width = src_w / hsub;
1794 height = src_h / vsub;
1796 init_plane_view_dims(intel_fb, i, width, height, &view_dims);
1799 * First pixel of the src viewport from the
1800 * start of the normal gtt mapping.
1802 x += intel_fb->normal_view.color_plane[i].x;
1803 y += intel_fb->normal_view.color_plane[i].y;
1805 offset = calc_plane_aligned_offset(intel_fb, i, &x, &y);
1807 gtt_offset += calc_plane_remap_info(intel_fb, i, &view_dims,
1808 offset, gtt_offset, x, y,
1809 &plane_state->view);
1813 void intel_fb_fill_view(const struct intel_framebuffer *fb, unsigned int rotation,
1814 struct intel_fb_view *view)
1816 if (drm_rotation_90_or_270(rotation))
1817 *view = fb->rotated_view;
1818 else if (intel_fb_needs_pot_stride_remap(fb))
1819 *view = fb->remapped_view;
1821 *view = fb->normal_view;
1825 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
1826 u32 pixel_format, u64 modifier)
1829 * Arbitrary limit for gen4+ chosen to match the
1830 * render engine max stride.
1832 * The new CCS hash mode makes remapping impossible
1834 if (DISPLAY_VER(dev_priv) < 4 || intel_fb_is_ccs_modifier(modifier) ||
1835 intel_fb_modifier_uses_dpt(dev_priv, modifier))
1836 return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
1837 else if (DISPLAY_VER(dev_priv) >= 7)
1844 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
1846 struct drm_i915_private *dev_priv = to_i915(fb->dev);
1847 unsigned int tile_width;
1849 if (is_surface_linear(fb, color_plane)) {
1850 unsigned int max_stride = intel_plane_fb_max_stride(dev_priv,
1855 * To make remapping with linear generally feasible
1856 * we need the stride to be page aligned.
1858 if (fb->pitches[color_plane] > max_stride &&
1859 !intel_fb_is_ccs_modifier(fb->modifier))
1860 return intel_tile_size(dev_priv);
1865 tile_width = intel_tile_width_bytes(fb, color_plane);
1866 if (intel_fb_is_ccs_modifier(fb->modifier)) {
1868 * On TGL the surface stride must be 4 tile aligned, mapped by
1869 * one 64 byte cacheline on the CCS AUX surface.
1871 if (DISPLAY_VER(dev_priv) >= 12)
1874 * Display WA #0531: skl,bxt,kbl,glk
1876 * Render decompression and plane width > 3840
1877 * combined with horizontal panning requires the
1878 * plane stride to be a multiple of 4. We'll just
1879 * require the entire fb to accommodate that to avoid
1880 * potential runtime errors at plane configuration time.
1882 else if ((DISPLAY_VER(dev_priv) == 9 || IS_GEMINILAKE(dev_priv)) &&
1883 color_plane == 0 && fb->width > 3840)
1889 static int intel_plane_check_stride(const struct intel_plane_state *plane_state)
1891 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1892 const struct drm_framebuffer *fb = plane_state->hw.fb;
1893 unsigned int rotation = plane_state->hw.rotation;
1894 u32 stride, max_stride;
1897 * We ignore stride for all invisible planes that
1898 * can be remapped. Otherwise we could end up
1899 * with a false positive when the remapping didn't
1900 * kick in due the plane being invisible.
1902 if (intel_plane_can_remap(plane_state) &&
1903 !plane_state->uapi.visible)
1906 /* FIXME other color planes? */
1907 stride = plane_state->view.color_plane[0].mapping_stride;
1908 max_stride = plane->max_stride(plane, fb->format->format,
1909 fb->modifier, rotation);
1911 if (stride > max_stride) {
1912 drm_dbg_kms(plane->base.dev,
1913 "[FB:%d] stride (%d) exceeds [PLANE:%d:%s] max stride (%d)\n",
1914 fb->base.id, stride,
1915 plane->base.base.id, plane->base.name, max_stride);
1922 int intel_plane_compute_gtt(struct intel_plane_state *plane_state)
1924 const struct intel_framebuffer *fb =
1925 to_intel_framebuffer(plane_state->hw.fb);
1926 unsigned int rotation = plane_state->hw.rotation;
1931 if (intel_plane_needs_remap(plane_state)) {
1932 intel_plane_remap_gtt(plane_state);
1935 * Sometimes even remapping can't overcome
1936 * the stride limitations :( Can happen with
1937 * big plane sizes and suitably misaligned
1940 return intel_plane_check_stride(plane_state);
1943 intel_fb_fill_view(fb, rotation, &plane_state->view);
1945 /* Rotate src coordinates to match rotated GTT view */
1946 if (drm_rotation_90_or_270(rotation))
1947 drm_rect_rotate(&plane_state->uapi.src,
1948 fb->base.width << 16, fb->base.height << 16,
1949 DRM_MODE_ROTATE_270);
1951 return intel_plane_check_stride(plane_state);
1954 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
1956 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1958 drm_framebuffer_cleanup(fb);
1960 if (intel_fb_uses_dpt(fb))
1961 intel_dpt_destroy(intel_fb->dpt_vm);
1963 intel_frontbuffer_put(intel_fb->frontbuffer);
1965 intel_fb_bo_framebuffer_fini(intel_fb_bo(fb));
1970 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
1971 struct drm_file *file,
1972 unsigned int *handle)
1974 struct drm_gem_object *obj = intel_fb_bo(fb);
1975 struct intel_display *display = to_intel_display(obj->dev);
1977 if (intel_bo_is_userptr(obj)) {
1978 drm_dbg(display->drm,
1979 "attempting to use a userptr for a framebuffer, denied\n");
1983 return drm_gem_handle_create(file, obj, handle);
1986 struct frontbuffer_fence_cb {
1987 struct dma_fence_cb base;
1988 struct intel_frontbuffer *front;
1991 static void intel_user_framebuffer_fence_wake(struct dma_fence *dma,
1992 struct dma_fence_cb *data)
1994 struct frontbuffer_fence_cb *cb = container_of(data, typeof(*cb), base);
1996 intel_frontbuffer_queue_flush(cb->front);
2001 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
2002 struct drm_file *file,
2003 unsigned int flags, unsigned int color,
2004 struct drm_clip_rect *clips,
2005 unsigned int num_clips)
2007 struct drm_gem_object *obj = intel_fb_bo(fb);
2008 struct intel_frontbuffer *front = to_intel_frontbuffer(fb);
2009 struct dma_fence *fence;
2010 struct frontbuffer_fence_cb *cb;
2013 if (!atomic_read(&front->bits))
2016 if (dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(false)))
2019 ret = dma_resv_get_singleton(obj->resv, dma_resv_usage_rw(false),
2024 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
2026 dma_fence_put(fence);
2033 intel_frontbuffer_invalidate(front, ORIGIN_DIRTYFB);
2035 ret = dma_fence_add_callback(fence, &cb->base,
2036 intel_user_framebuffer_fence_wake);
2038 intel_user_framebuffer_fence_wake(fence, &cb->base);
2046 intel_bo_flush_if_display(obj);
2047 intel_frontbuffer_flush(front, ORIGIN_DIRTYFB);
2051 static const struct drm_framebuffer_funcs intel_fb_funcs = {
2052 .destroy = intel_user_framebuffer_destroy,
2053 .create_handle = intel_user_framebuffer_create_handle,
2054 .dirty = intel_user_framebuffer_dirty,
2057 int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
2058 struct drm_gem_object *obj,
2059 struct drm_mode_fb_cmd2 *mode_cmd)
2061 struct drm_i915_private *dev_priv = to_i915(obj->dev);
2062 struct drm_framebuffer *fb = &intel_fb->base;
2067 ret = intel_fb_bo_framebuffer_init(intel_fb, obj, mode_cmd);
2071 intel_fb->frontbuffer = intel_frontbuffer_get(obj);
2072 if (!intel_fb->frontbuffer) {
2078 if (!drm_any_plane_has_format(&dev_priv->drm,
2079 mode_cmd->pixel_format,
2080 mode_cmd->modifier[0])) {
2081 drm_dbg_kms(&dev_priv->drm,
2082 "unsupported pixel format %p4cc / modifier 0x%llx\n",
2083 &mode_cmd->pixel_format, mode_cmd->modifier[0]);
2084 goto err_frontbuffer_put;
2087 max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
2088 mode_cmd->modifier[0]);
2089 if (mode_cmd->pitches[0] > max_stride) {
2090 drm_dbg_kms(&dev_priv->drm,
2091 "%s pitch (%u) must be at most %d\n",
2092 mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
2094 mode_cmd->pitches[0], max_stride);
2095 goto err_frontbuffer_put;
2098 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
2099 if (mode_cmd->offsets[0] != 0) {
2100 drm_dbg_kms(&dev_priv->drm,
2101 "plane 0 offset (0x%08x) must be 0\n",
2102 mode_cmd->offsets[0]);
2103 goto err_frontbuffer_put;
2106 drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
2108 for (i = 0; i < fb->format->num_planes; i++) {
2109 unsigned int stride_alignment;
2111 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
2112 drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
2114 goto err_frontbuffer_put;
2117 stride_alignment = intel_fb_stride_alignment(fb, i);
2118 if (fb->pitches[i] & (stride_alignment - 1)) {
2119 drm_dbg_kms(&dev_priv->drm,
2120 "plane %d pitch (%d) must be at least %u byte aligned\n",
2121 i, fb->pitches[i], stride_alignment);
2122 goto err_frontbuffer_put;
2125 if (intel_fb_is_gen12_ccs_aux_plane(fb, i)) {
2126 unsigned int ccs_aux_stride = gen12_ccs_aux_stride(intel_fb, i);
2128 if (fb->pitches[i] != ccs_aux_stride) {
2129 drm_dbg_kms(&dev_priv->drm,
2130 "ccs aux plane %d pitch (%d) must be %d\n",
2132 fb->pitches[i], ccs_aux_stride);
2133 goto err_frontbuffer_put;
2140 ret = intel_fill_fb_info(dev_priv, intel_fb);
2142 goto err_frontbuffer_put;
2144 if (intel_fb_uses_dpt(fb)) {
2145 struct i915_address_space *vm;
2147 vm = intel_dpt_create(intel_fb);
2149 drm_dbg_kms(&dev_priv->drm, "failed to create DPT\n");
2151 goto err_frontbuffer_put;
2154 intel_fb->dpt_vm = vm;
2157 ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
2159 drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
2166 if (intel_fb_uses_dpt(fb))
2167 intel_dpt_destroy(intel_fb->dpt_vm);
2168 err_frontbuffer_put:
2169 intel_frontbuffer_put(intel_fb->frontbuffer);
2171 intel_fb_bo_framebuffer_fini(obj);
2175 struct drm_framebuffer *
2176 intel_user_framebuffer_create(struct drm_device *dev,
2177 struct drm_file *filp,
2178 const struct drm_mode_fb_cmd2 *user_mode_cmd)
2180 struct drm_framebuffer *fb;
2181 struct drm_gem_object *obj;
2182 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
2183 struct drm_i915_private *i915 = to_i915(dev);
2185 obj = intel_fb_bo_lookup_valid_bo(i915, filp, &mode_cmd);
2187 return ERR_CAST(obj);
2189 fb = intel_framebuffer_create(obj, &mode_cmd);
2190 drm_gem_object_put(obj);
2195 struct drm_framebuffer *
2196 intel_framebuffer_create(struct drm_gem_object *obj,
2197 struct drm_mode_fb_cmd2 *mode_cmd)
2199 struct intel_framebuffer *intel_fb;
2202 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
2204 return ERR_PTR(-ENOMEM);
2206 ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
2210 return &intel_fb->base;
2214 return ERR_PTR(ret);
2217 struct drm_gem_object *intel_fb_bo(const struct drm_framebuffer *fb)
2219 return fb ? fb->obj[0] : NULL;