xref: /linux/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c (revision 9ecdac91ed43d836683d5daa467f4907405420e2)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2022-2026 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: AMD
24  *
25  */
26 
27 #include <drm/drm_atomic_helper.h>
28 #include <drm/drm_blend.h>
29 #include "drm/drm_framebuffer.h"
30 #include <drm/drm_gem_atomic_helper.h>
31 #include <drm/drm_panic_helper.h>
32 #include <drm/drm_plane_helper.h>
33 #include <drm/drm_gem_framebuffer_helper.h>
34 #include <drm/drm_fourcc.h>
35 
36 #include "amdgpu.h"
37 #include "dc.h"
38 #include "dal_asic_id.h"
39 #include "amdgpu_display.h"
40 #include "amdgpu_dm_trace.h"
41 #include "amdgpu_dm_plane.h"
42 #include "dm_helpers.h"
43 #include "amdgpu_dm_colorop.h"
44 #include "gc/gc_11_0_0_offset.h"
45 #include "gc/gc_11_0_0_sh_mask.h"
46 
47 /*
48  * TODO: these are currently initialized to rgb formats only.
49  * For future use cases we should either initialize them dynamically based on
50  * plane capabilities, or initialize this array to all formats, so internal drm
51  * check will succeed, and let DC implement proper check
52  */
53 static const uint32_t rgb_formats[] = {
54 	DRM_FORMAT_XRGB8888,
55 	DRM_FORMAT_ARGB8888,
56 	DRM_FORMAT_RGBA8888,
57 	DRM_FORMAT_XRGB2101010,
58 	DRM_FORMAT_XBGR2101010,
59 	DRM_FORMAT_ARGB2101010,
60 	DRM_FORMAT_ABGR2101010,
61 	DRM_FORMAT_XRGB16161616,
62 	DRM_FORMAT_XBGR16161616,
63 	DRM_FORMAT_ARGB16161616,
64 	DRM_FORMAT_ABGR16161616,
65 	DRM_FORMAT_XBGR8888,
66 	DRM_FORMAT_ABGR8888,
67 	DRM_FORMAT_RGB565,
68 };
69 
70 static const uint32_t overlay_formats[] = {
71 	DRM_FORMAT_XRGB8888,
72 	DRM_FORMAT_ARGB8888,
73 	DRM_FORMAT_RGBA8888,
74 	DRM_FORMAT_XBGR8888,
75 	DRM_FORMAT_ABGR8888,
76 	DRM_FORMAT_RGB565,
77 	DRM_FORMAT_NV21,
78 	DRM_FORMAT_NV12,
79 	DRM_FORMAT_P010
80 };
81 
82 static const uint32_t video_formats[] = {
83 	DRM_FORMAT_NV21,
84 	DRM_FORMAT_NV12,
85 	DRM_FORMAT_P010
86 };
87 
88 static const u32 cursor_formats[] = {
89 	DRM_FORMAT_ARGB8888
90 };
91 
92 enum dm_micro_swizzle {
93 	MICRO_SWIZZLE_Z = 0,
94 	MICRO_SWIZZLE_S = 1,
95 	MICRO_SWIZZLE_D = 2,
96 	MICRO_SWIZZLE_R = 3
97 };
98 
99 const struct drm_format_info *amdgpu_dm_plane_get_format_info(u32 pixel_format, u64 modifier)
100 {
101 	return amdgpu_lookup_format_info(pixel_format, modifier);
102 }
103 EXPORT_IF_KUNIT(amdgpu_dm_plane_get_format_info);
104 
105 void amdgpu_dm_plane_fill_blending_from_plane_state(const struct drm_plane_state *plane_state,
106 			       bool *per_pixel_alpha, bool *pre_multiplied_alpha,
107 			       bool *global_alpha, int *global_alpha_value)
108 {
109 	*per_pixel_alpha = false;
110 	*pre_multiplied_alpha = true;
111 	*global_alpha = false;
112 	*global_alpha_value = 0xff;
113 
114 
115 	if (plane_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI ||
116 		plane_state->pixel_blend_mode == DRM_MODE_BLEND_COVERAGE) {
117 		*per_pixel_alpha = plane_state->fb->format->has_alpha;
118 
119 		if (*per_pixel_alpha && plane_state->pixel_blend_mode == DRM_MODE_BLEND_COVERAGE)
120 			*pre_multiplied_alpha = false;
121 	}
122 
123 	if (plane_state->alpha < 0xffff) {
124 		struct amdgpu_device *adev = drm_to_adev(plane_state->plane->dev);
125 		*global_alpha = true;
126 		/*
127 		 * DCN 4.2 uses a 12-bit MPCC_GLOBAL_ALPHA field, while
128 		 * other ASICs use an 8-bit field. The DRM plane alpha is
129 		 * 16-bit, so scale it down to the width the hardware expects.
130 		 */
131 		if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 0)
132 		    || amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 1)
133 		    || amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(6, 0, 0))
134 			*global_alpha_value = plane_state->alpha >> 4;
135 		else
136 			*global_alpha_value = plane_state->alpha >> 8;
137 
138 	}
139 }
140 EXPORT_IF_KUNIT(amdgpu_dm_plane_fill_blending_from_plane_state);
141 
142 STATIC_IFN_KUNIT void amdgpu_dm_plane_add_modifier(uint64_t **mods, uint64_t *size,
143 						   uint64_t *cap, uint64_t mod)
144 {
145 	if (!*mods)
146 		return;
147 
148 	if (*cap - *size < 1) {
149 		uint64_t new_cap = *cap * 2;
150 		uint64_t *new_mods = kmalloc_array(new_cap, sizeof(uint64_t), GFP_KERNEL);
151 
152 		if (!new_mods) {
153 			kfree(*mods);
154 			*mods = NULL;
155 			return;
156 		}
157 
158 		memcpy(new_mods, *mods, sizeof(uint64_t) * *size);
159 		kfree(*mods);
160 		*mods = new_mods;
161 		*cap = new_cap;
162 	}
163 
164 	(*mods)[*size] = mod;
165 	*size += 1;
166 }
167 EXPORT_IF_KUNIT(amdgpu_dm_plane_add_modifier);
168 
169 STATIC_IFN_KUNIT void amdgpu_dm_plane_add_modifier_dedup(uint64_t **mods, uint64_t *size,
170 							 uint64_t *cap, uint64_t mod)
171 {
172 	uint64_t i;
173 
174 	if (!*mods)
175 		return;
176 
177 	for (i = 0; i < *size; ++i)
178 		if ((*mods)[i] == mod)
179 			return;
180 
181 	amdgpu_dm_plane_add_modifier(mods, size, cap, mod);
182 }
183 EXPORT_IF_KUNIT(amdgpu_dm_plane_add_modifier_dedup);
184 
185 STATIC_IFN_KUNIT bool amdgpu_dm_plane_modifier_has_dcc(uint64_t modifier)
186 {
187 	return IS_AMD_FMT_MOD(modifier) && AMD_FMT_MOD_GET(DCC, modifier);
188 }
189 EXPORT_IF_KUNIT(amdgpu_dm_plane_modifier_has_dcc);
190 
191 STATIC_IFN_KUNIT unsigned int amdgpu_dm_plane_modifier_gfx9_swizzle_mode(uint64_t modifier)
192 {
193 	if (modifier == DRM_FORMAT_MOD_LINEAR)
194 		return 0;
195 
196 	return AMD_FMT_MOD_GET(TILE, modifier);
197 }
198 EXPORT_IF_KUNIT(amdgpu_dm_plane_modifier_gfx9_swizzle_mode);
199 
200 STATIC_IFN_KUNIT int amdgpu_dm_plane_fill_gfx6_tiling_info_from_modifier(
201 						struct dc_tiling_info *tiling_info,
202 						uint64_t modifier)
203 {
204 	if (modifier == DRM_FORMAT_MOD_LINEAR) {
205 		tiling_info->gfx8.array_mode = DC_ARRAY_LINEAR_GENERAL;
206 		tiling_info->gfxversion = DcGfxVersion8;
207 
208 		return 0;
209 	}
210 
211 	if (!IS_AMD_FMT_MOD(modifier))
212 		return -EINVAL;
213 
214 	if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) != AMD_FMT_MOD_TILE_VER_GFX6)
215 		return -EINVAL;
216 
217 	tiling_info->gfx8.array_mode = AMD_FMT_MOD_GET(TILE, modifier);
218 	tiling_info->gfxversion = DcGfxVersion8;
219 	tiling_info->gfx8.tile_mode = AMD_FMT_MOD_GET(MICROTILE, modifier);
220 
221 	if (AMD_FMT_MOD_GET(TILE, modifier) < AMD_FMT_MOD_TILE_GFX6_2D_TILED_THIN1)
222 		return 0;
223 
224 	tiling_info->gfx8.pipe_config = AMD_FMT_MOD_GET(PIPE_CONFIG, modifier);
225 	tiling_info->gfx8.tile_split = AMD_FMT_MOD_GET(TILE_SPLIT, modifier);
226 	tiling_info->gfx8.bank_width = AMD_FMT_MOD_GET(BANK_WIDTH, modifier);
227 	tiling_info->gfx8.bank_height = AMD_FMT_MOD_GET(BANK_HEIGHT, modifier);
228 	tiling_info->gfx8.tile_aspect = AMD_FMT_MOD_GET(MACRO_TILE_ASPECT, modifier);
229 	tiling_info->gfx8.num_banks = AMD_FMT_MOD_GET(NUM_BANKS, modifier);
230 
231 	return 0;
232 }
233 EXPORT_IF_KUNIT(amdgpu_dm_plane_fill_gfx6_tiling_info_from_modifier);
234 
235 STATIC_IFN_KUNIT void amdgpu_dm_plane_fill_gfx9_tiling_info_from_device(const struct amdgpu_device *adev,
236 									struct dc_tiling_info *tiling_info)
237 {
238 	/* Fill GFX9 params */
239 	tiling_info->gfx9.num_pipes =
240 		adev->gfx.config.gb_addr_config_fields.num_pipes;
241 	tiling_info->gfx9.num_banks =
242 		adev->gfx.config.gb_addr_config_fields.num_banks;
243 	tiling_info->gfx9.pipe_interleave =
244 		adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
245 	tiling_info->gfx9.num_shader_engines =
246 		adev->gfx.config.gb_addr_config_fields.num_se;
247 	tiling_info->gfx9.max_compressed_frags =
248 		adev->gfx.config.gb_addr_config_fields.max_compress_frags;
249 	tiling_info->gfx9.num_rb_per_se =
250 		adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
251 	tiling_info->gfx9.shaderEnable = 1;
252 	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(10, 3, 0))
253 		tiling_info->gfx9.num_pkrs = adev->gfx.config.gb_addr_config_fields.num_pkrs;
254 }
255 EXPORT_IF_KUNIT(amdgpu_dm_plane_fill_gfx9_tiling_info_from_device);
256 
257 STATIC_IFN_KUNIT void amdgpu_dm_plane_fill_gfx9_tiling_info_from_modifier(const struct amdgpu_device *adev,
258 									  struct dc_tiling_info *tiling_info,
259 									  uint64_t modifier)
260 {
261 	unsigned int mod_bank_xor_bits = AMD_FMT_MOD_GET(BANK_XOR_BITS, modifier);
262 	unsigned int mod_pipe_xor_bits = AMD_FMT_MOD_GET(PIPE_XOR_BITS, modifier);
263 	unsigned int pkrs_log2 = AMD_FMT_MOD_GET(PACKERS, modifier);
264 	unsigned int pipes_log2;
265 
266 	pipes_log2 = min(5u, mod_pipe_xor_bits);
267 
268 	amdgpu_dm_plane_fill_gfx9_tiling_info_from_device(adev, tiling_info);
269 
270 	if (!IS_AMD_FMT_MOD(modifier))
271 		return;
272 
273 	tiling_info->gfx9.num_pipes = 1u << pipes_log2;
274 	tiling_info->gfx9.num_shader_engines = 1u << (mod_pipe_xor_bits - pipes_log2);
275 
276 	if (adev->family >= AMDGPU_FAMILY_NV) {
277 		tiling_info->gfx9.num_pkrs = 1u << pkrs_log2;
278 	} else {
279 		tiling_info->gfx9.num_banks = 1u << mod_bank_xor_bits;
280 
281 		/* for DCC we know it isn't rb aligned, so rb_per_se doesn't matter. */
282 	}
283 }
284 EXPORT_IF_KUNIT(amdgpu_dm_plane_fill_gfx9_tiling_info_from_modifier);
285 
286 STATIC_IFN_KUNIT int amdgpu_dm_plane_validate_dcc(struct amdgpu_device *adev,
287 						  const enum surface_pixel_format format,
288 						  const enum dc_rotation_angle rotation,
289 						  const struct dc_tiling_info *tiling_info,
290 						  const struct dc_plane_dcc_param *dcc,
291 						  const struct dc_plane_address *address,
292 						  const struct plane_size *plane_size)
293 {
294 	struct dc *dc = adev->dm.dc;
295 	struct dc_dcc_surface_param input;
296 	struct dc_surface_dcc_cap output;
297 
298 	memset(&input, 0, sizeof(input));
299 	memset(&output, 0, sizeof(output));
300 
301 	if (!dcc->enable)
302 		return 0;
303 
304 	if (adev->family != AMDGPU_FAMILY_GC_12_0_0 &&
305 	    adev->family != AMDGPU_FAMILY_GC_13_0_1 &&
306 	    format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
307 		return -EINVAL;
308 
309 	if (!dc->cap_funcs.get_dcc_compression_cap)
310 		return -EINVAL;
311 
312 	input.format = format;
313 	input.surface_size.width = plane_size->surface_size.width;
314 	input.surface_size.height = plane_size->surface_size.height;
315 	input.swizzle_mode = tiling_info->gfx9.swizzle;
316 
317 	if (rotation == ROTATION_ANGLE_0 || rotation == ROTATION_ANGLE_180)
318 		input.scan = SCAN_DIRECTION_HORIZONTAL;
319 	else if (rotation == ROTATION_ANGLE_90 || rotation == ROTATION_ANGLE_270)
320 		input.scan = SCAN_DIRECTION_VERTICAL;
321 
322 	if (!dc->cap_funcs.get_dcc_compression_cap(dc, &input, &output))
323 		return -EINVAL;
324 
325 	if (!output.capable)
326 		return -EINVAL;
327 
328 	if (dcc->independent_64b_blks == 0 &&
329 	    output.grph.rgb.independent_64b_blks != 0)
330 		return -EINVAL;
331 
332 	return 0;
333 }
334 EXPORT_IF_KUNIT(amdgpu_dm_plane_validate_dcc);
335 
336 STATIC_IFN_KUNIT int amdgpu_dm_plane_fill_gfx9_attrs_from_modifiers(struct amdgpu_device *adev,
337 									       const struct amdgpu_framebuffer *afb,
338 									       const enum surface_pixel_format format,
339 									       const enum dc_rotation_angle rotation,
340 									       const struct plane_size *plane_size,
341 									       struct dc_tiling_info *tiling_info,
342 									       struct dc_plane_dcc_param *dcc,
343 									       struct dc_plane_address *address)
344 {
345 	const uint64_t modifier = afb->base.modifier;
346 	int ret = 0;
347 
348 	amdgpu_dm_plane_fill_gfx9_tiling_info_from_modifier(adev, tiling_info, modifier);
349 	tiling_info->gfx9.swizzle = amdgpu_dm_plane_modifier_gfx9_swizzle_mode(modifier);
350 	tiling_info->gfxversion = DcGfxVersion9;
351 
352 	if (amdgpu_dm_plane_modifier_has_dcc(modifier)) {
353 		uint64_t dcc_address = afb->address + afb->base.offsets[1];
354 		bool independent_64b_blks = AMD_FMT_MOD_GET(DCC_INDEPENDENT_64B, modifier);
355 		bool independent_128b_blks = AMD_FMT_MOD_GET(DCC_INDEPENDENT_128B, modifier);
356 
357 		dcc->enable = 1;
358 		dcc->meta_pitch = afb->base.pitches[1];
359 		dcc->independent_64b_blks = independent_64b_blks;
360 		if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) >= AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) {
361 			if (independent_64b_blks && independent_128b_blks)
362 				dcc->dcc_ind_blk = hubp_ind_block_64b_no_128bcl;
363 			else if (independent_128b_blks)
364 				dcc->dcc_ind_blk = hubp_ind_block_128b;
365 			else if (independent_64b_blks && !independent_128b_blks)
366 				dcc->dcc_ind_blk = hubp_ind_block_64b;
367 			else
368 				dcc->dcc_ind_blk = hubp_ind_block_unconstrained;
369 		} else {
370 			if (independent_64b_blks)
371 				dcc->dcc_ind_blk = hubp_ind_block_64b;
372 			else
373 				dcc->dcc_ind_blk = hubp_ind_block_unconstrained;
374 		}
375 
376 		address->grph.meta_addr.low_part = lower_32_bits(dcc_address);
377 		address->grph.meta_addr.high_part = upper_32_bits(dcc_address);
378 	}
379 
380 	ret = amdgpu_dm_plane_validate_dcc(adev, format, rotation, tiling_info, dcc, address, plane_size);
381 	if (ret)
382 		drm_dbg_kms(adev_to_drm(adev), "amdgpu_dm_plane_validate_dcc: returned error: %pe\n", ERR_PTR(ret));
383 
384 	return ret;
385 }
386 EXPORT_IF_KUNIT(amdgpu_dm_plane_fill_gfx9_attrs_from_modifiers);
387 
388 STATIC_IFN_KUNIT int amdgpu_dm_plane_fill_gfx12_attrs_from_modifiers(struct amdgpu_device *adev,
389 										const struct amdgpu_framebuffer *afb,
390 										const enum surface_pixel_format format,
391 										const enum dc_rotation_angle rotation,
392 										const struct plane_size *plane_size,
393 										struct dc_tiling_info *tiling_info,
394 										struct dc_plane_dcc_param *dcc,
395 										struct dc_plane_address *address)
396 {
397 	const uint64_t modifier = afb->base.modifier;
398 	int ret = 0;
399 
400 	/* TODO: Most of this function shouldn't be needed on GFX12. */
401 	amdgpu_dm_plane_fill_gfx9_tiling_info_from_device(adev, tiling_info);
402 
403 	tiling_info->gfx9.swizzle = amdgpu_dm_plane_modifier_gfx9_swizzle_mode(modifier);
404 	tiling_info->gfxversion = DcGfxAddr3;
405 
406 	if (amdgpu_dm_plane_modifier_has_dcc(modifier)) {
407 		int max_compressed_block = AMD_FMT_MOD_GET(DCC_MAX_COMPRESSED_BLOCK, modifier);
408 
409 		dcc->enable = 1;
410 		dcc->independent_64b_blks = max_compressed_block == 0;
411 
412 		if (max_compressed_block == 0)
413 			dcc->dcc_ind_blk = hubp_ind_block_64b;
414 		else if (max_compressed_block == 1)
415 			dcc->dcc_ind_blk = hubp_ind_block_128b;
416 		else
417 			dcc->dcc_ind_blk = hubp_ind_block_unconstrained;
418 	}
419 
420 	/* TODO: This seems wrong because there is no DCC plane on GFX12. */
421 	ret = amdgpu_dm_plane_validate_dcc(adev, format, rotation, tiling_info, dcc, address, plane_size);
422 	if (ret)
423 		drm_dbg_kms(adev_to_drm(adev), "amdgpu_dm_plane_validate_dcc: returned: %pe\n", ERR_PTR(ret));
424 
425 	return ret;
426 }
427 EXPORT_IF_KUNIT(amdgpu_dm_plane_fill_gfx12_attrs_from_modifiers);
428 
429 static void amdgpu_dm_plane_add_gfx10_1_modifiers(const struct amdgpu_device *adev,
430 						  uint64_t **mods,
431 						  uint64_t *size,
432 						  uint64_t *capacity)
433 {
434 	int pipe_xor_bits = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
435 
436 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
437 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
438 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
439 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
440 				     AMD_FMT_MOD_SET(DCC, 1) |
441 				     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
442 				     AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
443 				     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
444 
445 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
446 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
447 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
448 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
449 				     AMD_FMT_MOD_SET(DCC, 1) |
450 				     AMD_FMT_MOD_SET(DCC_RETILE, 1) |
451 				     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
452 				     AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
453 				     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
454 
455 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
456 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
457 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
458 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits));
459 
460 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
461 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
462 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
463 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits));
464 
465 
466 	/* Only supported for 64bpp, will be filtered in amdgpu_dm_plane_format_mod_supported */
467 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
468 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D) |
469 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
470 
471 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
472 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S) |
473 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
474 }
475 
476 /**
477  * amdgpu_dm_plane_get_gfx6_tile_idx() - Get tile mode index on GFX6-8
478  *
479  * @adev: amdgpu_device pointer
480  * @bpp: bits per pixel in the image
481  * @arr: array mode (aka. tile mode) of the image layout
482  *
483  * Select which tiling mode from the table is suitable for the given bits per pixel
484  * using the given array mode, assuming the displayable micro tile mode is used.
485  *
486  * On GFX6-8, the GFX block can only use pre-programmed tiling modes from GB_TILE_MODEn
487  * registers which are programmed by the kernel according to the tiling mode table.
488  * Note that the tiling table is uAPI and userspace relies on specific modes being
489  * present at specific indices.
490  * See SiLib::HwlSetupTileInfo() and CiLib::HwlSetupTileInfo() in addrlib.
491  */
492 STATIC_IFN_KUNIT u32 amdgpu_dm_plane_get_gfx6_tile_idx(const struct amdgpu_device *adev,
493 						       const u32 bpp,
494 						       const enum array_mode_values arr)
495 {
496 	/* Assume that the microtile mode is DISPLAY. */
497 
498 	if (arr == DC_ARRAY_1D_TILED_THIN1)
499 		return 9;
500 
501 	ASSERT(arr == DC_ARRAY_2D_TILED_THIN1);
502 
503 	if (adev->family > AMDGPU_FAMILY_SI)
504 		return 10;
505 
506 	switch (bpp) {
507 	case 8:
508 		return 10;
509 	case 16:
510 		return 11;
511 	default:
512 		return 12;
513 	}
514 }
515 EXPORT_IF_KUNIT(amdgpu_dm_plane_get_gfx6_tile_idx);
516 
517 /**
518  * amdgpu_dm_plane_calc_gfx7_tile_split() - Calculate tile split on GFX7-8
519  *
520  * @adev: amdgpu_device pointer
521  * @bpp: bits per pixel in the image
522  * @gb_tile_mode: GB_TILE_MODEn register value for the current tiling mode
523  *
524  * Calculate the actual tile split value on GFX7-8, assuming 2D_TILED_THIN1 array mode
525  * with a non-depth micro tile mode.
526  *
527  * On GFX7-8, SAMPLE_SPLIT holds a factor from which the actual tile split bytes
528  * can be calculated. The TILE_SPLIT field is only used for the depth micro tile mode.
529  * See CiLib::HwlComputeMacroModeIndex() in addrlib.
530  */
531 STATIC_IFN_KUNIT u32 amdgpu_dm_plane_calc_gfx7_tile_split(const struct amdgpu_device *adev,
532 							  const u32 bpp,
533 							  const u32 gb_tile_mode)
534 {
535 	/* Assume 2D_TILED_THIN1 mode with non-DEPTH microtiles */
536 	const u32 sample_split = (gb_tile_mode >> 25) & 0x3;
537 	const u32 thickness = 1;
538 	const u32 tile_size_pixels = 8 * 8;
539 	const u32 tile_bytes_1x = tile_size_pixels * bpp * thickness / 8;
540 	const u32 sample_split_factor = 1 << sample_split;
541 
542 	return clamp(tile_bytes_1x * sample_split_factor,
543 		     256,
544 		     adev->gfx.config.mem_row_size_in_kb * 1024);
545 }
546 EXPORT_IF_KUNIT(amdgpu_dm_plane_calc_gfx7_tile_split);
547 
548 /**
549  * amdgpu_dm_plane_get_gfx7_macro_tile_idx() - Get macro tile mode index on GFX7-8
550  *
551  * @bpp: bits per pixel in the image
552  * @tile_split_bytes: actual tile split bytes, see amdgpu_dm_plane_calc_gfx7_tile_split()
553  *
554  * Select which macro tiling mode from the table is suitable for the given bits per pixel,
555  * assuming 2D_TILED_THIN1 array mode and DISPLAY micro tile mode (and no multisampling).
556  * Note that the tiling table is uAPI and userspace relies on specific modes being
557  * present at specific indices.
558  * See CiLib::HwlComputeMacroModeIndex() in addrlib.
559  */
560 STATIC_IFN_KUNIT u32 amdgpu_dm_plane_get_gfx7_macro_tile_idx(const u32 bpp,
561 							     const u32 tile_split_bytes)
562 {
563 	const u32 thickness = 1;
564 	const u32 tile_size_pixels = 8 * 8;
565 	const u32 tile_bytes_1x = thickness * tile_size_pixels * bpp / 8;
566 	const u32 tile_bytes = clamp(tile_bytes_1x, 64, tile_split_bytes);
567 	const u32 macro_tile_idx = ilog2(tile_bytes / 64);
568 
569 	WARN_ON(macro_tile_idx >= 16);
570 
571 	return macro_tile_idx;
572 }
573 EXPORT_IF_KUNIT(amdgpu_dm_plane_get_gfx7_macro_tile_idx);
574 
575 /**
576  * amdgpu_dm_plane_calc_gfx6_mod() - Calculate a DRM format modifier for GFX6-8
577  *
578  * @adev: amdgpu_device pointer
579  * @bpp: bits per pixel in the image
580  * @arr: array mode (aka. tile mode) of the image layout
581  *
582  * Select suitable micro and macro tile modes for the given bits per pixel,
583  * and calculate the corresponding DRM format modifier.
584  */
585 STATIC_IFN_KUNIT u64 amdgpu_dm_plane_calc_gfx6_mod(const struct amdgpu_device *adev,
586 						   const u32 bpp,
587 						   const enum array_mode_values arr)
588 {
589 	u32 array_mode, micro_tile_mode, tile_split_bytes;
590 	u32 gb_macrotile_mode, macrotile_idx;
591 	u32 gb_tile_mode, tile_idx;
592 
593 	u64 modifier_base =
594 		AMD_FMT_MOD |
595 		AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX6) |
596 		AMD_FMT_MOD_SET(TILE, arr) |
597 		AMD_FMT_MOD_SET(MICROTILE, AMD_FMT_MOD_MICROTILE_DISPLAY);
598 
599 	if (arr < DC_ARRAY_2D_TILED_THIN1)
600 		return modifier_base;
601 
602 	tile_idx = amdgpu_dm_plane_get_gfx6_tile_idx(adev, bpp, arr);
603 	gb_tile_mode = adev->gfx.config.tile_mode_array[tile_idx];
604 	array_mode = (gb_tile_mode >> 2) & 0xf;
605 
606 	if (adev->family == AMDGPU_FAMILY_SI) {
607 		micro_tile_mode = (gb_tile_mode >> 0) & 0x3;
608 		tile_split_bytes = 64 << ((gb_tile_mode >> 11) & 0x7);
609 	} else {
610 		micro_tile_mode = (gb_tile_mode >> 22) & 0x7;
611 		tile_split_bytes = amdgpu_dm_plane_calc_gfx7_tile_split(adev, bpp, gb_tile_mode);
612 	}
613 
614 	ASSERT(array_mode == arr);
615 	ASSERT(micro_tile_mode == AMD_FMT_MOD_MICROTILE_DISPLAY);
616 
617 	modifier_base |=
618 		AMD_FMT_MOD_SET(PIPE_CONFIG, (gb_tile_mode >> 6) & 0x1f) |
619 		AMD_FMT_MOD_SET(TILE_SPLIT, ilog2(tile_split_bytes / 64));
620 
621 	if (adev->family == AMDGPU_FAMILY_SI)
622 		return modifier_base |
623 			AMD_FMT_MOD_SET(BANK_WIDTH, (gb_tile_mode >> 14) & 0x3) |
624 			AMD_FMT_MOD_SET(BANK_HEIGHT, (gb_tile_mode >> 16) & 0x3) |
625 			AMD_FMT_MOD_SET(MACRO_TILE_ASPECT, (gb_tile_mode >> 18) & 0x3) |
626 			AMD_FMT_MOD_SET(NUM_BANKS, (gb_tile_mode >> 20) & 0x3);
627 
628 	macrotile_idx = amdgpu_dm_plane_get_gfx7_macro_tile_idx(bpp, tile_split_bytes);
629 	gb_macrotile_mode = adev->gfx.config.macrotile_mode_array[macrotile_idx];
630 
631 	return modifier_base |
632 		AMD_FMT_MOD_SET(BANK_WIDTH, (gb_macrotile_mode >> 0) & 0x3) |
633 		AMD_FMT_MOD_SET(BANK_HEIGHT, (gb_macrotile_mode >> 2) & 0x3) |
634 		AMD_FMT_MOD_SET(MACRO_TILE_ASPECT, (gb_macrotile_mode >> 4) & 0x3) |
635 		AMD_FMT_MOD_SET(NUM_BANKS, (gb_macrotile_mode >> 6) & 0x3);
636 }
637 EXPORT_IF_KUNIT(amdgpu_dm_plane_calc_gfx6_mod);
638 
639 /**
640  * amdgpu_dm_plane_gfx6_format_mod_supported() - Check if a modifier is supported on GFX6-8
641  *
642  * @adev: amdgpu_device pointer
643  * @bpp: bits per pixel in the image
644  * @modifier: the modifier whose support we check
645  *
646  * On GFX6-8, not all DRM format modifier can be used with all image formats.
647  * Check whether the specified modifier is supported with the given bits per pixel value.
648  */
649 STATIC_IFN_KUNIT bool amdgpu_dm_plane_gfx6_format_mod_supported(const struct amdgpu_device *adev,
650 								const u32 bpp,
651 								const u64 modifier)
652 {
653 	const u32 array_mode = AMD_FMT_MOD_GET(TILE, modifier);
654 	const u32 micro_tile_mode = AMD_FMT_MOD_GET(MICROTILE, modifier);
655 
656 	if (!IS_AMD_FMT_MOD(modifier))
657 		return false;
658 
659 	/* GFX9 and newer format modifiers are not supported on GFX6-8. */
660 	if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) != AMD_FMT_MOD_TILE_VER_GFX6)
661 		return false;
662 
663 	/* GFX8 and older format modifiers are not supported on GFX9+. */
664 	if (adev->family >= AMDGPU_FAMILY_AI)
665 		return false;
666 
667 	/* GFX6-7 doesn't have any DCC, GFX8 doesn't support displayable DCC. */
668 	if (AMD_FMT_MOD_GET(DCC, modifier))
669 		return false;
670 
671 	/*
672 	 * For now, only expose 1D and 2D tiles THIN array modes.
673 	 * Linear is already exposed through DRM_FORMAT_MOD_LINEAR.
674 	 */
675 	if (array_mode != AMD_FMT_MOD_TILE_GFX6_1D_TILED_THIN1 &&
676 	    array_mode != AMD_FMT_MOD_TILE_GFX6_2D_TILED_THIN1)
677 		return false;
678 
679 	/*
680 	 * For now, only allow DISPLAY micro tile mode.
681 	 * THIN, DEPTH and THICK modes are not displayable.
682 	 * ROTATED has never been supported by Linux or Mesa.
683 	 */
684 	if (micro_tile_mode != AMD_FMT_MOD_MICROTILE_DISPLAY)
685 		return false;
686 
687 	/* Verify that the modifier is the same that we'd expose for this bpp */
688 	return amdgpu_dm_plane_calc_gfx6_mod(adev, bpp, array_mode) == modifier;
689 }
690 EXPORT_IF_KUNIT(amdgpu_dm_plane_gfx6_format_mod_supported);
691 
692 /**
693  * amdgpu_dm_plane_add_gfx6_modifiers() - Expose modifiers for GFX6-8
694  *
695  * @adev: amdgpu_device pointer
696  * @mods: Pointer to array of format modifiers
697  * @size: Pointer to size of the array
698  * @capacity: Pointer to capacity of the array
699  *
700  * Calculate a DRM format modifier for macro tiled modes for each supported
701  * bits per pixel value. Use de-duplication because on some GPUs it may happen
702  * that different bpp results in the exact same macro tiling mode, depending
703  * on the tiling table.
704  *
705  * Also expose a micro tiled only mode. This is less optimal, but supported.
706  */
707 static void amdgpu_dm_plane_add_gfx6_modifiers(const struct amdgpu_device *adev,
708 					       u64 **mods,
709 					       u64 *size,
710 					       u64 *capacity)
711 {
712 	/* 2D tiled displayable */
713 	amdgpu_dm_plane_add_modifier(mods, size, capacity,
714 		amdgpu_dm_plane_calc_gfx6_mod(adev, 16, DC_ARRAY_2D_TILED_THIN1));
715 	amdgpu_dm_plane_add_modifier_dedup(mods, size, capacity,
716 		amdgpu_dm_plane_calc_gfx6_mod(adev, 32, DC_ARRAY_2D_TILED_THIN1));
717 	amdgpu_dm_plane_add_modifier_dedup(mods, size, capacity,
718 		amdgpu_dm_plane_calc_gfx6_mod(adev, 64, DC_ARRAY_2D_TILED_THIN1));
719 
720 	/* 1D tiled displayable */
721 	amdgpu_dm_plane_add_modifier(mods, size, capacity,
722 		amdgpu_dm_plane_calc_gfx6_mod(adev, 0, DC_ARRAY_1D_TILED_THIN1));
723 }
724 
725 static void amdgpu_dm_plane_add_gfx9_modifiers(const struct amdgpu_device *adev,
726 					       uint64_t **mods,
727 					       uint64_t *size,
728 					       uint64_t *capacity)
729 {
730 	int pipes = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
731 	int pipe_xor_bits = min(8, pipes +
732 				ilog2(adev->gfx.config.gb_addr_config_fields.num_se));
733 	int bank_xor_bits = min(8 - pipe_xor_bits,
734 				ilog2(adev->gfx.config.gb_addr_config_fields.num_banks));
735 	int rb = ilog2(adev->gfx.config.gb_addr_config_fields.num_se) +
736 		 ilog2(adev->gfx.config.gb_addr_config_fields.num_rb_per_se);
737 
738 
739 	if (adev->family == AMDGPU_FAMILY_RV) {
740 		/* Raven2 and later */
741 		bool has_constant_encode = adev->asic_type > CHIP_RAVEN || adev->external_rev_id >= 0x81;
742 
743 		/*
744 		 * No _D DCC swizzles yet because we only allow 32bpp, which
745 		 * doesn't support _D on DCN
746 		 */
747 
748 		if (has_constant_encode) {
749 			amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
750 						     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
751 						     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
752 						     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
753 						     AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
754 						     AMD_FMT_MOD_SET(DCC, 1) |
755 						     AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
756 						     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
757 						     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1));
758 		}
759 
760 		amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
761 					     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
762 					     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
763 					     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
764 					     AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
765 					     AMD_FMT_MOD_SET(DCC, 1) |
766 					     AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
767 					     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
768 					     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 0));
769 
770 		if (has_constant_encode) {
771 			amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
772 						     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
773 						     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
774 						     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
775 						     AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
776 						     AMD_FMT_MOD_SET(DCC, 1) |
777 						     AMD_FMT_MOD_SET(DCC_RETILE, 1) |
778 						     AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
779 						     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
780 						     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
781 						     AMD_FMT_MOD_SET(RB, rb) |
782 						     AMD_FMT_MOD_SET(PIPE, pipes));
783 		}
784 
785 		amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
786 					     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
787 					     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
788 					     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
789 					     AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
790 					     AMD_FMT_MOD_SET(DCC, 1) |
791 					     AMD_FMT_MOD_SET(DCC_RETILE, 1) |
792 					     AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
793 					     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
794 					     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 0) |
795 					     AMD_FMT_MOD_SET(RB, rb) |
796 					     AMD_FMT_MOD_SET(PIPE, pipes));
797 	}
798 
799 	/*
800 	 * Only supported for 64bpp on Raven, will be filtered on format in
801 	 * amdgpu_dm_plane_format_mod_supported.
802 	 */
803 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
804 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D_X) |
805 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
806 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
807 				     AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits));
808 
809 	if (adev->family == AMDGPU_FAMILY_RV) {
810 		amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
811 					     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
812 					     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
813 					     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
814 					     AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits));
815 	}
816 
817 	/*
818 	 * Only supported for 64bpp on Raven, will be filtered on format in
819 	 * amdgpu_dm_plane_format_mod_supported.
820 	 */
821 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
822 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D) |
823 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
824 
825 	if (adev->family == AMDGPU_FAMILY_RV) {
826 		amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
827 					     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S) |
828 					     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
829 	}
830 }
831 
832 static void amdgpu_dm_plane_add_gfx10_3_modifiers(const struct amdgpu_device *adev,
833 						  uint64_t **mods,
834 						  uint64_t *size,
835 						  uint64_t *capacity)
836 {
837 	int pipe_xor_bits = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
838 	int pkrs = ilog2(adev->gfx.config.gb_addr_config_fields.num_pkrs);
839 
840 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
841 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
842 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
843 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
844 				     AMD_FMT_MOD_SET(PACKERS, pkrs) |
845 				     AMD_FMT_MOD_SET(DCC, 1) |
846 				     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
847 				     AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
848 				     AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
849 				     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
850 
851 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
852 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
853 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
854 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
855 				     AMD_FMT_MOD_SET(PACKERS, pkrs) |
856 				     AMD_FMT_MOD_SET(DCC, 1) |
857 				     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
858 				     AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
859 				     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_128B));
860 
861 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
862 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
863 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
864 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
865 				     AMD_FMT_MOD_SET(PACKERS, pkrs) |
866 				     AMD_FMT_MOD_SET(DCC, 1) |
867 				     AMD_FMT_MOD_SET(DCC_RETILE, 1) |
868 				     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
869 				     AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
870 				     AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
871 				     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
872 
873 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
874 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
875 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
876 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
877 				     AMD_FMT_MOD_SET(PACKERS, pkrs) |
878 				     AMD_FMT_MOD_SET(DCC, 1) |
879 				     AMD_FMT_MOD_SET(DCC_RETILE, 1) |
880 				     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
881 				     AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
882 				     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_128B));
883 
884 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
885 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
886 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
887 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
888 				     AMD_FMT_MOD_SET(PACKERS, pkrs));
889 
890 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
891 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
892 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
893 				     AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
894 				     AMD_FMT_MOD_SET(PACKERS, pkrs));
895 
896 	/* Only supported for 64bpp, will be filtered in amdgpu_dm_plane_format_mod_supported */
897 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
898 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D) |
899 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
900 
901 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
902 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S) |
903 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
904 }
905 
906 static void amdgpu_dm_plane_add_gfx11_modifiers(struct amdgpu_device *adev,
907 		      uint64_t **mods, uint64_t *size, uint64_t *capacity)
908 {
909 	int num_pipes = 0;
910 	int pipe_xor_bits = 0;
911 	int num_pkrs = 0;
912 	int pkrs = 0;
913 	u32 gb_addr_config;
914 	u8 i = 0;
915 	unsigned int swizzle_r_x;
916 	uint64_t modifier_r_x;
917 	uint64_t modifier_dcc_best;
918 	uint64_t modifier_dcc_4k;
919 
920 	/* TODO: GFX11 IP HW init hasnt finish and we get zero if we read from
921 	 * adev->gfx.config.gb_addr_config_fields.num_{pkrs,pipes}
922 	 */
923 	gb_addr_config = RREG32_SOC15(GC, 0, regGB_ADDR_CONFIG);
924 	ASSERT(gb_addr_config != 0);
925 
926 	num_pkrs = 1 << REG_GET_FIELD(gb_addr_config, GB_ADDR_CONFIG, NUM_PKRS);
927 	pkrs = ilog2(num_pkrs);
928 	num_pipes = 1 << REG_GET_FIELD(gb_addr_config, GB_ADDR_CONFIG, NUM_PIPES);
929 	pipe_xor_bits = ilog2(num_pipes);
930 
931 	for (i = 0; i < 2; i++) {
932 		/* Insert the best one first. */
933 		/* R_X swizzle modes are the best for rendering and DCC requires them. */
934 		if (num_pipes > 16)
935 			swizzle_r_x = !i ? AMD_FMT_MOD_TILE_GFX11_256K_R_X : AMD_FMT_MOD_TILE_GFX9_64K_R_X;
936 		else
937 			swizzle_r_x = !i ? AMD_FMT_MOD_TILE_GFX9_64K_R_X : AMD_FMT_MOD_TILE_GFX11_256K_R_X;
938 
939 		modifier_r_x = AMD_FMT_MOD |
940 			       AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX11) |
941 			       AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
942 			       AMD_FMT_MOD_SET(TILE, swizzle_r_x) |
943 			       AMD_FMT_MOD_SET(PACKERS, pkrs);
944 
945 		/* DCC_CONSTANT_ENCODE is not set because it can't vary with gfx11 (it's implied to be 1). */
946 		modifier_dcc_best = modifier_r_x | AMD_FMT_MOD_SET(DCC, 1) |
947 				    AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 0) |
948 				    AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
949 				    AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_128B);
950 
951 		/* DCC settings for 4K and greater resolutions. (required by display hw) */
952 		modifier_dcc_4k = modifier_r_x | AMD_FMT_MOD_SET(DCC, 1) |
953 				  AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
954 				  AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
955 				  AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B);
956 
957 		amdgpu_dm_plane_add_modifier(mods, size, capacity, modifier_dcc_best);
958 		amdgpu_dm_plane_add_modifier(mods, size, capacity, modifier_dcc_4k);
959 
960 		amdgpu_dm_plane_add_modifier(mods, size, capacity, modifier_dcc_best | AMD_FMT_MOD_SET(DCC_RETILE, 1));
961 		amdgpu_dm_plane_add_modifier(mods, size, capacity, modifier_dcc_4k | AMD_FMT_MOD_SET(DCC_RETILE, 1));
962 
963 		amdgpu_dm_plane_add_modifier(mods, size, capacity, modifier_r_x);
964 	}
965 
966 	amdgpu_dm_plane_add_modifier(mods, size, capacity, AMD_FMT_MOD |
967 				     AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX11) |
968 				     AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D));
969 }
970 
971 static void amdgpu_dm_plane_add_gfx12_modifiers(struct amdgpu_device *adev,
972 		      uint64_t **mods, uint64_t *size, uint64_t *capacity)
973 {
974 	uint64_t ver = AMD_FMT_MOD | AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX12);
975 	uint64_t mod_256k = ver | AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX12_256K_2D);
976 	uint64_t mod_64k = ver | AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX12_64K_2D);
977 	uint64_t mod_4k = ver | AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX12_4K_2D);
978 	uint64_t mod_256b = ver | AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX12_256B_2D);
979 	uint64_t dcc = ver | AMD_FMT_MOD_SET(DCC, 1);
980 	uint8_t max_comp_block[] = {2, 1, 0};
981 	uint64_t max_comp_block_mod[ARRAY_SIZE(max_comp_block)] = {0};
982 	uint8_t i = 0, j = 0;
983 	/* Note, linear (no DCC) gets added to the modifier list for all chips by the caller. */
984 	uint64_t gfx12_modifiers[] = {mod_256k, mod_64k, mod_4k, mod_256b};
985 
986 	for (i = 0; i < ARRAY_SIZE(max_comp_block); i++)
987 		max_comp_block_mod[i] = AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, max_comp_block[i]);
988 
989 	/* With DCC: Best choice should be kept first. Hence, add all 256k modifiers of different
990 	 * max compressed blocks first and then move on to the next smaller sized layouts.
991 	 */
992 	for (j = 0; j < ARRAY_SIZE(gfx12_modifiers); j++)
993 		for (i = 0; i < ARRAY_SIZE(max_comp_block); i++)
994 			amdgpu_dm_plane_add_modifier(mods, size, capacity,
995 						     ver | dcc | max_comp_block_mod[i] | gfx12_modifiers[j]);
996 
997 	/* Without DCC. */
998 	for (i = 0; i < ARRAY_SIZE(gfx12_modifiers); i++)
999 		amdgpu_dm_plane_add_modifier(mods, size, capacity, gfx12_modifiers[i]);
1000 
1001 }
1002 
1003 STATIC_IFN_KUNIT int amdgpu_dm_plane_get_plane_modifiers(struct amdgpu_device *adev, unsigned int plane_type, uint64_t **mods)
1004 {
1005 	uint64_t size = 0, capacity = 128;
1006 	*mods = kmalloc_array(capacity, sizeof(uint64_t), GFP_KERNEL);
1007 
1008 	if (plane_type == DRM_PLANE_TYPE_CURSOR) {
1009 		amdgpu_dm_plane_add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_LINEAR);
1010 		amdgpu_dm_plane_add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_INVALID);
1011 		return *mods ? 0 : -ENOMEM;
1012 	}
1013 
1014 	switch (adev->family) {
1015 	case AMDGPU_FAMILY_SI:
1016 	case AMDGPU_FAMILY_CI:
1017 	case AMDGPU_FAMILY_KV:
1018 	case AMDGPU_FAMILY_VI:
1019 	case AMDGPU_FAMILY_CZ:
1020 		amdgpu_dm_plane_add_gfx6_modifiers(adev, mods, &size, &capacity);
1021 		break;
1022 	case AMDGPU_FAMILY_AI:
1023 	case AMDGPU_FAMILY_RV:
1024 		amdgpu_dm_plane_add_gfx9_modifiers(adev, mods, &size, &capacity);
1025 		break;
1026 	case AMDGPU_FAMILY_NV:
1027 	case AMDGPU_FAMILY_VGH:
1028 	case AMDGPU_FAMILY_YC:
1029 	case AMDGPU_FAMILY_GC_10_3_6:
1030 	case AMDGPU_FAMILY_GC_10_3_7:
1031 		if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(10, 3, 0))
1032 			amdgpu_dm_plane_add_gfx10_3_modifiers(adev, mods, &size, &capacity);
1033 		else
1034 			amdgpu_dm_plane_add_gfx10_1_modifiers(adev, mods, &size, &capacity);
1035 		break;
1036 	case AMDGPU_FAMILY_GC_11_0_0:
1037 	case AMDGPU_FAMILY_GC_11_0_1:
1038 	case AMDGPU_FAMILY_GC_11_5_0:
1039 	case AMDGPU_FAMILY_GC_11_5_4:
1040 		amdgpu_dm_plane_add_gfx11_modifiers(adev, mods, &size, &capacity);
1041 		break;
1042 	case AMDGPU_FAMILY_GC_12_0_0:
1043 	case AMDGPU_FAMILY_GC_13_0_1:
1044 		/* GFX13 (DCN6) shares the GFX12 tiling/DCC binary modifier format. */
1045 		amdgpu_dm_plane_add_gfx12_modifiers(adev, mods, &size, &capacity);
1046 		break;
1047 	}
1048 
1049 	amdgpu_dm_plane_add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_LINEAR);
1050 
1051 	/* INVALID marks the end of the list. */
1052 	amdgpu_dm_plane_add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_INVALID);
1053 
1054 	if (!*mods)
1055 		return -ENOMEM;
1056 
1057 	return 0;
1058 }
1059 EXPORT_IF_KUNIT(amdgpu_dm_plane_get_plane_modifiers);
1060 
1061 STATIC_IFN_KUNIT int amdgpu_dm_plane_get_plane_formats(const struct drm_plane *plane,
1062 						       const struct dc_plane_cap *plane_cap,
1063 						       uint32_t *formats, int max_formats)
1064 {
1065 	int i, num_formats = 0;
1066 
1067 	/*
1068 	 * TODO: Query support for each group of formats directly from
1069 	 * DC plane caps. This will require adding more formats to the
1070 	 * caps list.
1071 	 */
1072 
1073 	if (plane->type == DRM_PLANE_TYPE_PRIMARY ||
1074 		(plane_cap && plane_cap->type == DC_PLANE_TYPE_DCN_UNIVERSAL && plane->type != DRM_PLANE_TYPE_CURSOR)) {
1075 		for (i = 0; i < ARRAY_SIZE(rgb_formats); ++i) {
1076 			if (num_formats >= max_formats)
1077 				break;
1078 
1079 			formats[num_formats++] = rgb_formats[i];
1080 		}
1081 
1082 		if (plane_cap && plane_cap->pixel_format_support.nv12)
1083 			formats[num_formats++] = DRM_FORMAT_NV12;
1084 		if (plane_cap && plane_cap->pixel_format_support.p010)
1085 			formats[num_formats++] = DRM_FORMAT_P010;
1086 		if (plane_cap && plane_cap->pixel_format_support.fp16) {
1087 			formats[num_formats++] = DRM_FORMAT_XRGB16161616F;
1088 			formats[num_formats++] = DRM_FORMAT_ARGB16161616F;
1089 			formats[num_formats++] = DRM_FORMAT_XBGR16161616F;
1090 			formats[num_formats++] = DRM_FORMAT_ABGR16161616F;
1091 		}
1092 	} else {
1093 		switch (plane->type) {
1094 		case DRM_PLANE_TYPE_OVERLAY:
1095 			for (i = 0; i < ARRAY_SIZE(overlay_formats); ++i) {
1096 				if (num_formats >= max_formats)
1097 					break;
1098 
1099 				formats[num_formats++] = overlay_formats[i];
1100 			}
1101 			break;
1102 
1103 		case DRM_PLANE_TYPE_CURSOR:
1104 			for (i = 0; i < ARRAY_SIZE(cursor_formats); ++i) {
1105 				if (num_formats >= max_formats)
1106 					break;
1107 
1108 				formats[num_formats++] = cursor_formats[i];
1109 			}
1110 			break;
1111 
1112 		default:
1113 			break;
1114 		}
1115 	}
1116 
1117 	return num_formats;
1118 }
1119 EXPORT_IF_KUNIT(amdgpu_dm_plane_get_plane_formats);
1120 
1121 int amdgpu_dm_plane_fill_plane_buffer_attributes(struct amdgpu_device *adev,
1122 			     const struct amdgpu_framebuffer *afb,
1123 			     const enum surface_pixel_format format,
1124 			     const enum dc_rotation_angle rotation,
1125 			     struct dc_tiling_info *tiling_info,
1126 			     struct plane_size *plane_size,
1127 			     struct dc_plane_dcc_param *dcc,
1128 			     struct dc_plane_address *address,
1129 			     bool tmz_surface)
1130 {
1131 	const struct drm_framebuffer *fb = &afb->base;
1132 	int ret;
1133 
1134 	memset(tiling_info, 0, sizeof(*tiling_info));
1135 	memset(plane_size, 0, sizeof(*plane_size));
1136 	memset(dcc, 0, sizeof(*dcc));
1137 	memset(address, 0, sizeof(*address));
1138 
1139 	address->tmz_surface = tmz_surface;
1140 
1141 	if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
1142 		uint64_t addr = afb->address + fb->offsets[0];
1143 
1144 		plane_size->surface_size.x = 0;
1145 		plane_size->surface_size.y = 0;
1146 		plane_size->surface_size.width = fb->width;
1147 		plane_size->surface_size.height = fb->height;
1148 		plane_size->surface_pitch =
1149 			fb->pitches[0] / fb->format->cpp[0];
1150 
1151 		address->type = PLN_ADDR_TYPE_GRAPHICS;
1152 		address->grph.addr.low_part = lower_32_bits(addr);
1153 		address->grph.addr.high_part = upper_32_bits(addr);
1154 	} else if (format < SURFACE_PIXEL_FORMAT_INVALID) {
1155 		uint64_t luma_addr = afb->address + fb->offsets[0];
1156 		uint64_t chroma_addr = afb->address + fb->offsets[1];
1157 
1158 		plane_size->surface_size.x = 0;
1159 		plane_size->surface_size.y = 0;
1160 		plane_size->surface_size.width = fb->width;
1161 		plane_size->surface_size.height = fb->height;
1162 		plane_size->surface_pitch =
1163 			fb->pitches[0] / fb->format->cpp[0];
1164 
1165 		plane_size->chroma_size.x = 0;
1166 		plane_size->chroma_size.y = 0;
1167 		/* TODO: set these based on surface format */
1168 		plane_size->chroma_size.width = fb->width / 2;
1169 		plane_size->chroma_size.height = fb->height / 2;
1170 
1171 		plane_size->chroma_pitch =
1172 			fb->pitches[1] / fb->format->cpp[1];
1173 
1174 		address->type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
1175 		address->video_progressive.luma_addr.low_part =
1176 			lower_32_bits(luma_addr);
1177 		address->video_progressive.luma_addr.high_part =
1178 			upper_32_bits(luma_addr);
1179 		address->video_progressive.chroma_addr.low_part =
1180 			lower_32_bits(chroma_addr);
1181 		address->video_progressive.chroma_addr.high_part =
1182 			upper_32_bits(chroma_addr);
1183 	}
1184 
1185 	if (adev->family == AMDGPU_FAMILY_GC_12_0_0
1186 	    || adev->family == AMDGPU_FAMILY_GC_13_0_1) {
1187 		ret = amdgpu_dm_plane_fill_gfx12_attrs_from_modifiers(adev, afb, format,
1188 										 rotation, plane_size,
1189 										 tiling_info, dcc,
1190 										 address);
1191 		if (ret)
1192 			return ret;
1193 	} else if (adev->family >= AMDGPU_FAMILY_AI) {
1194 		ret = amdgpu_dm_plane_fill_gfx9_attrs_from_modifiers(adev, afb, format,
1195 										rotation, plane_size,
1196 										tiling_info, dcc,
1197 										address);
1198 		if (ret)
1199 			return ret;
1200 	} else {
1201 		ret = amdgpu_dm_plane_fill_gfx6_tiling_info_from_modifier(tiling_info,
1202 									  afb->base.modifier);
1203 		if (ret)
1204 			return ret;
1205 	}
1206 
1207 	return 0;
1208 }
1209 EXPORT_IF_KUNIT(amdgpu_dm_plane_fill_plane_buffer_attributes);
1210 
1211 STATIC_IFN_KUNIT int amdgpu_dm_plane_helper_prepare_fb(struct drm_plane *plane,
1212 						       struct drm_plane_state *new_state)
1213 {
1214 	struct amdgpu_framebuffer *afb;
1215 	struct drm_gem_object *obj;
1216 	struct amdgpu_device *adev;
1217 	struct amdgpu_bo *rbo;
1218 	struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
1219 	uint32_t domain;
1220 	int r;
1221 
1222 	if (!new_state->fb) {
1223 		DRM_DEBUG_KMS("No FB bound\n");
1224 		return 0;
1225 	}
1226 
1227 	afb = to_amdgpu_framebuffer(new_state->fb);
1228 	obj = drm_gem_fb_get_obj(new_state->fb, 0);
1229 	if (!obj) {
1230 		DRM_ERROR("Failed to get obj from framebuffer\n");
1231 		return -EINVAL;
1232 	}
1233 
1234 	rbo = gem_to_amdgpu_bo(obj);
1235 	adev = amdgpu_ttm_adev(rbo->tbo.bdev);
1236 	r = amdgpu_bo_reserve(rbo, true);
1237 	if (r) {
1238 		drm_err(adev_to_drm(adev), "fail to reserve bo: %pe\n", ERR_PTR(r));
1239 		return r;
1240 	}
1241 
1242 	r = dma_resv_reserve_fences(rbo->tbo.base.resv, TTM_NUM_MOVE_FENCES);
1243 	if (r) {
1244 		drm_err(adev_to_drm(adev), "reserving fence slot failed: %pe\n", ERR_PTR(r));
1245 		goto error_unlock;
1246 	}
1247 
1248 	if (plane->type != DRM_PLANE_TYPE_CURSOR)
1249 		domain = amdgpu_display_supported_domains(adev, rbo->flags);
1250 	else
1251 		domain = AMDGPU_GEM_DOMAIN_VRAM;
1252 
1253 	rbo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1254 	r = amdgpu_bo_pin(rbo, domain);
1255 	if (unlikely(r != 0)) {
1256 		if (r != -ERESTARTSYS)
1257 			DRM_ERROR("Failed to pin framebuffer: %pe\n", ERR_PTR(r));
1258 		goto error_unlock;
1259 	}
1260 
1261 	r = amdgpu_ttm_alloc_gart(&rbo->tbo);
1262 	if (unlikely(r != 0)) {
1263 		DRM_ERROR("%p bind failed: %pe\n", rbo, ERR_PTR(r));
1264 		goto error_unpin;
1265 	}
1266 
1267 	r = drm_gem_plane_helper_prepare_fb(plane, new_state);
1268 	if (unlikely(r != 0))
1269 		goto error_unpin;
1270 
1271 	amdgpu_bo_unreserve(rbo);
1272 
1273 	afb->address = amdgpu_bo_gpu_offset(rbo);
1274 
1275 	amdgpu_bo_ref(rbo);
1276 
1277 	/**
1278 	 * We don't do surface updates on planes that have been newly created,
1279 	 * but we also don't have the afb->address during atomic check.
1280 	 *
1281 	 * Fill in buffer attributes depending on the address here, but only on
1282 	 * newly created planes since they're not being used by DC yet and this
1283 	 * won't modify global state.
1284 	 */
1285 	dm_plane_state_old = to_dm_plane_state(plane->state);
1286 	dm_plane_state_new = to_dm_plane_state(new_state);
1287 
1288 	if (dm_plane_state_new->dc_state &&
1289 	    dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
1290 		struct dc_plane_state *plane_state =
1291 			dm_plane_state_new->dc_state;
1292 
1293 		amdgpu_dm_plane_fill_plane_buffer_attributes(
1294 			adev, afb, plane_state->format, plane_state->rotation,
1295 			&plane_state->tiling_info, &plane_state->plane_size,
1296 			&plane_state->dcc, &plane_state->address,
1297 			afb->tmz_surface);
1298 	}
1299 
1300 	return 0;
1301 
1302 error_unpin:
1303 	amdgpu_bo_unpin(rbo);
1304 
1305 error_unlock:
1306 	amdgpu_bo_unreserve(rbo);
1307 	return r;
1308 }
1309 EXPORT_IF_KUNIT(amdgpu_dm_plane_helper_prepare_fb);
1310 
1311 STATIC_IFN_KUNIT void amdgpu_dm_plane_helper_cleanup_fb(struct drm_plane *plane,
1312 							struct drm_plane_state *old_state)
1313 {
1314 	struct amdgpu_bo *rbo;
1315 	int r;
1316 
1317 	if (!old_state->fb)
1318 		return;
1319 
1320 	rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
1321 	r = amdgpu_bo_reserve(rbo, false);
1322 	if (unlikely(r)) {
1323 		DRM_ERROR("failed to reserve rbo before unpin: %pe\n", ERR_PTR(r));
1324 		return;
1325 	}
1326 
1327 	amdgpu_bo_unpin(rbo);
1328 	amdgpu_bo_unreserve(rbo);
1329 	amdgpu_bo_unref(&rbo);
1330 }
1331 EXPORT_IF_KUNIT(amdgpu_dm_plane_helper_cleanup_fb);
1332 
1333 STATIC_IFN_KUNIT void amdgpu_dm_plane_get_min_max_dc_plane_scaling(struct drm_device *dev,
1334 								   struct drm_framebuffer *fb,
1335 								   int *min_downscale, int *max_upscale)
1336 {
1337 	struct amdgpu_device *adev = drm_to_adev(dev);
1338 	struct dc *dc = adev->dm.dc;
1339 	/* Caps for all supported planes are the same on DCE and DCN 1 - 3 */
1340 	struct dc_plane_cap *plane_cap = &dc->caps.planes[0];
1341 
1342 	switch (fb->format->format) {
1343 	case DRM_FORMAT_P010:
1344 	case DRM_FORMAT_NV12:
1345 	case DRM_FORMAT_NV21:
1346 		*max_upscale = plane_cap->max_upscale_factor.nv12;
1347 		*min_downscale = plane_cap->max_downscale_factor.nv12;
1348 		break;
1349 
1350 	/* All 64 bpp formats have the same fp16 scaling limits */
1351 	case DRM_FORMAT_XRGB16161616F:
1352 	case DRM_FORMAT_ARGB16161616F:
1353 	case DRM_FORMAT_XBGR16161616F:
1354 	case DRM_FORMAT_ABGR16161616F:
1355 	case DRM_FORMAT_XRGB16161616:
1356 	case DRM_FORMAT_ARGB16161616:
1357 	case DRM_FORMAT_XBGR16161616:
1358 	case DRM_FORMAT_ABGR16161616:
1359 		*max_upscale = plane_cap->max_upscale_factor.fp16;
1360 		*min_downscale = plane_cap->max_downscale_factor.fp16;
1361 		break;
1362 
1363 	default:
1364 		*max_upscale = plane_cap->max_upscale_factor.argb8888;
1365 		*min_downscale = plane_cap->max_downscale_factor.argb8888;
1366 		break;
1367 	}
1368 
1369 	/*
1370 	 * A factor of 1 in the plane_cap means to not allow scaling, ie. use a
1371 	 * scaling factor of 1.0 == 1000 units.
1372 	 */
1373 	if (*max_upscale == 1)
1374 		*max_upscale = 1000;
1375 
1376 	if (*min_downscale == 1)
1377 		*min_downscale = 1000;
1378 }
1379 EXPORT_IF_KUNIT(amdgpu_dm_plane_get_min_max_dc_plane_scaling);
1380 
1381 int amdgpu_dm_plane_helper_check_state(struct drm_plane_state *state,
1382 				       struct drm_crtc_state *new_crtc_state)
1383 {
1384 	struct drm_framebuffer *fb = state->fb;
1385 	int min_downscale, max_upscale;
1386 	int min_scale = 0;
1387 	int max_scale = INT_MAX;
1388 
1389 	/* Plane enabled? Validate viewport and get scaling factors from plane caps. */
1390 	if (fb && state->crtc) {
1391 		/* Validate viewport to cover the case when only the position changes */
1392 		if (state->plane->type != DRM_PLANE_TYPE_CURSOR) {
1393 			int viewport_width = state->crtc_w;
1394 			int viewport_height = state->crtc_h;
1395 
1396 			if (state->crtc_x < 0)
1397 				viewport_width += state->crtc_x;
1398 			else if (state->crtc_x + state->crtc_w > new_crtc_state->mode.crtc_hdisplay)
1399 				viewport_width = new_crtc_state->mode.crtc_hdisplay - state->crtc_x;
1400 
1401 			if (state->crtc_y < 0)
1402 				viewport_height += state->crtc_y;
1403 			else if (state->crtc_y + state->crtc_h > new_crtc_state->mode.crtc_vdisplay)
1404 				viewport_height = new_crtc_state->mode.crtc_vdisplay - state->crtc_y;
1405 
1406 			if (viewport_width < 0 || viewport_height < 0) {
1407 				DRM_DEBUG_ATOMIC("Plane completely outside of screen\n");
1408 				return -EINVAL;
1409 			} else if (viewport_width < MIN_VIEWPORT_SIZE*2) { /* x2 for width is because of pipe-split. */
1410 				DRM_DEBUG_ATOMIC("Viewport width %d smaller than %d\n", viewport_width, MIN_VIEWPORT_SIZE*2);
1411 				return -EINVAL;
1412 			} else if (viewport_height < MIN_VIEWPORT_SIZE) {
1413 				DRM_DEBUG_ATOMIC("Viewport height %d smaller than %d\n", viewport_height, MIN_VIEWPORT_SIZE);
1414 				return -EINVAL;
1415 			}
1416 
1417 		}
1418 
1419 		/* Get min/max allowed scaling factors from plane caps. */
1420 		amdgpu_dm_plane_get_min_max_dc_plane_scaling(state->crtc->dev, fb,
1421 							     &min_downscale, &max_upscale);
1422 		/*
1423 		 * Convert to drm convention: 16.16 fixed point, instead of dc's
1424 		 * 1.0 == 1000. Also drm scaling is src/dst instead of dc's
1425 		 * dst/src, so min_scale = 1.0 / max_upscale, etc.
1426 		 */
1427 		min_scale = (1000 << 16) / max_upscale;
1428 		max_scale = (1000 << 16) / min_downscale;
1429 	}
1430 
1431 	return drm_atomic_helper_check_plane_state(
1432 		state, new_crtc_state, min_scale, max_scale, true, true);
1433 }
1434 EXPORT_IF_KUNIT(amdgpu_dm_plane_helper_check_state);
1435 
1436 int amdgpu_dm_plane_fill_dc_scaling_info(struct amdgpu_device *adev,
1437 				const struct drm_plane_state *state,
1438 				struct dc_scaling_info *scaling_info)
1439 {
1440 	int scale_w, scale_h, min_downscale, max_upscale;
1441 
1442 	memset(scaling_info, 0, sizeof(*scaling_info));
1443 
1444 	/* Source is fixed 16.16 but we ignore mantissa for now... */
1445 	scaling_info->src_rect.x = state->src_x >> 16;
1446 	scaling_info->src_rect.y = state->src_y >> 16;
1447 
1448 	/*
1449 	 * For reasons we don't (yet) fully understand a non-zero
1450 	 * src_y coordinate into an NV12 buffer can cause a
1451 	 * system hang on DCN1x.
1452 	 * To avoid hangs (and maybe be overly cautious)
1453 	 * let's reject both non-zero src_x and src_y.
1454 	 *
1455 	 * We currently know of only one use-case to reproduce a
1456 	 * scenario with non-zero src_x and src_y for NV12, which
1457 	 * is to gesture the YouTube Android app into full screen
1458 	 * on ChromeOS.
1459 	 */
1460 	if (((amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(1, 0, 0)) ||
1461 	    (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(1, 0, 1))) &&
1462 	    (state->fb && state->fb->format->format == DRM_FORMAT_NV12 &&
1463 	    (scaling_info->src_rect.x != 0 || scaling_info->src_rect.y != 0)))
1464 		return -EINVAL;
1465 
1466 	scaling_info->src_rect.width = state->src_w >> 16;
1467 	if (scaling_info->src_rect.width == 0)
1468 		return -EINVAL;
1469 
1470 	scaling_info->src_rect.height = state->src_h >> 16;
1471 	if (scaling_info->src_rect.height == 0)
1472 		return -EINVAL;
1473 
1474 	scaling_info->dst_rect.x = state->crtc_x;
1475 	scaling_info->dst_rect.y = state->crtc_y;
1476 
1477 	if (state->crtc_w == 0)
1478 		return -EINVAL;
1479 
1480 	scaling_info->dst_rect.width = state->crtc_w;
1481 
1482 	if (state->crtc_h == 0)
1483 		return -EINVAL;
1484 
1485 	scaling_info->dst_rect.height = state->crtc_h;
1486 
1487 	/* DRM doesn't specify clipping on destination output. */
1488 	scaling_info->clip_rect = scaling_info->dst_rect;
1489 
1490 	/* Validate scaling per-format with DC plane caps */
1491 	if (state->plane && state->plane->dev && state->fb) {
1492 		amdgpu_dm_plane_get_min_max_dc_plane_scaling(state->plane->dev, state->fb,
1493 							     &min_downscale, &max_upscale);
1494 	} else {
1495 		min_downscale = 250;
1496 		max_upscale = 16000;
1497 	}
1498 
1499 	scale_w = scaling_info->dst_rect.width * 1000 /
1500 		  scaling_info->src_rect.width;
1501 
1502 	if (scale_w < min_downscale || scale_w > max_upscale)
1503 		return -EINVAL;
1504 
1505 	scale_h = scaling_info->dst_rect.height * 1000 /
1506 		  scaling_info->src_rect.height;
1507 
1508 	if (scale_h < min_downscale || scale_h > max_upscale)
1509 		return -EINVAL;
1510 
1511 	/*
1512 	 * The "scaling_quality" can be ignored for now, quality = 0 has DC
1513 	 * assume reasonable defaults based on the format.
1514 	 */
1515 
1516 	return 0;
1517 }
1518 EXPORT_IF_KUNIT(amdgpu_dm_plane_fill_dc_scaling_info);
1519 
1520 STATIC_IFN_KUNIT int amdgpu_dm_plane_atomic_check(struct drm_plane *plane,
1521 						  struct drm_atomic_commit *state)
1522 {
1523 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
1524 										 plane);
1525 	struct amdgpu_device *adev = drm_to_adev(plane->dev);
1526 	struct dc *dc = adev->dm.dc;
1527 	struct dm_plane_state *dm_plane_state;
1528 	struct dc_scaling_info scaling_info;
1529 	struct drm_crtc_state *new_crtc_state;
1530 	int ret;
1531 
1532 	trace_amdgpu_dm_plane_atomic_check(new_plane_state);
1533 
1534 	dm_plane_state = to_dm_plane_state(new_plane_state);
1535 
1536 	if (!dm_plane_state->dc_state)
1537 		return 0;
1538 
1539 	new_crtc_state =
1540 		drm_atomic_get_new_crtc_state(state,
1541 					      new_plane_state->crtc);
1542 	if (!new_crtc_state)
1543 		return -EINVAL;
1544 
1545 	ret = amdgpu_dm_plane_helper_check_state(new_plane_state, new_crtc_state);
1546 	if (ret)
1547 		return ret;
1548 
1549 	/* Reject commits that attempt to use both COLOR_PIPELINE and CRTC DEGAMMA_LUT */
1550 	if (new_plane_state->color_pipeline && new_crtc_state->degamma_lut) {
1551 		drm_dbg_atomic(plane->dev,
1552 			       "[PLANE:%d:%s] COLOR_PIPELINE and CRTC DEGAMMA_LUT cannot be enabled simultaneously\n",
1553 			       plane->base.id, plane->name);
1554 		return -EINVAL;
1555 	}
1556 
1557 	ret = amdgpu_dm_plane_fill_dc_scaling_info(adev, new_plane_state, &scaling_info);
1558 	if (ret)
1559 		return ret;
1560 
1561 	if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
1562 		return 0;
1563 
1564 	return -EINVAL;
1565 }
1566 EXPORT_IF_KUNIT(amdgpu_dm_plane_atomic_check);
1567 
1568 STATIC_IFN_KUNIT int amdgpu_dm_plane_atomic_async_check(struct drm_plane *plane,
1569 							struct drm_atomic_commit *state, bool flip)
1570 {
1571 	struct drm_crtc_state *new_crtc_state;
1572 	struct drm_plane_state *new_plane_state;
1573 	struct dm_crtc_state *dm_new_crtc_state;
1574 
1575 	if (flip) {
1576 		if (plane->type != DRM_PLANE_TYPE_OVERLAY)
1577 			return -EINVAL;
1578 	} else if (plane->type != DRM_PLANE_TYPE_CURSOR) {
1579 		return -EINVAL;
1580 	}
1581 
1582 	new_plane_state = drm_atomic_get_new_plane_state(state, plane);
1583 	new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
1584 	dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
1585 	/* Reject overlay cursors for now*/
1586 	if (!flip && dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE)
1587 		return -EINVAL;
1588 
1589 	return 0;
1590 }
1591 EXPORT_IF_KUNIT(amdgpu_dm_plane_atomic_async_check);
1592 
1593 int amdgpu_dm_plane_get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
1594 					struct dc_cursor_position *position)
1595 {
1596 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1597 	struct amdgpu_device *adev = drm_to_adev(plane->dev);
1598 	int x, y;
1599 	int xorigin = 0, yorigin = 0;
1600 
1601 	if (!crtc || !plane->state->fb)
1602 		return 0;
1603 
1604 	if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
1605 	    (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
1606 		DRM_ERROR("%s: bad cursor width or height %d x %d\n",
1607 			  __func__,
1608 			  plane->state->crtc_w,
1609 			  plane->state->crtc_h);
1610 		return -EINVAL;
1611 	}
1612 
1613 	x = plane->state->crtc_x;
1614 	y = plane->state->crtc_y;
1615 
1616 	if (x <= -amdgpu_crtc->max_cursor_width ||
1617 	    y <= -amdgpu_crtc->max_cursor_height)
1618 		return 0;
1619 
1620 	if (x < 0) {
1621 		xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
1622 		x = 0;
1623 	}
1624 	if (y < 0) {
1625 		yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
1626 		y = 0;
1627 	}
1628 	position->enable = true;
1629 	position->x = x;
1630 	position->y = y;
1631 	position->x_hotspot = xorigin;
1632 	position->y_hotspot = yorigin;
1633 
1634 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(4, 0, 1))
1635 		position->translate_by_source = true;
1636 
1637 	return 0;
1638 }
1639 EXPORT_IF_KUNIT(amdgpu_dm_plane_get_cursor_position);
1640 
1641 void amdgpu_dm_plane_handle_cursor_update(struct drm_plane *plane,
1642 				 struct drm_plane_state *old_plane_state)
1643 {
1644 	struct amdgpu_device *adev = drm_to_adev(plane->dev);
1645 	struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
1646 	struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
1647 	struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
1648 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1649 	uint64_t address = afb ? afb->address : 0;
1650 	struct dc_cursor_position position = {0};
1651 	struct dc_cursor_attributes attributes;
1652 	int ret;
1653 
1654 	if (!plane->state->fb && !old_plane_state->fb)
1655 		return;
1656 
1657 	drm_dbg_atomic(plane->dev, "crtc_id=%d with size %d to %d\n",
1658 		       amdgpu_crtc->crtc_id, plane->state->crtc_w,
1659 		       plane->state->crtc_h);
1660 
1661 	ret = amdgpu_dm_plane_get_cursor_position(plane, crtc, &position);
1662 	if (ret)
1663 		return;
1664 
1665 	if (!position.enable) {
1666 		/* turn off cursor */
1667 		if (crtc_state && crtc_state->stream) {
1668 			mutex_lock(&adev->dm.dc_lock);
1669 			amdgpu_dm_ism_commit_event(
1670 				&amdgpu_crtc->ism,
1671 				DM_ISM_EVENT_BEGIN_CURSOR_UPDATE);
1672 
1673 			dc_stream_program_cursor_position(crtc_state->stream,
1674 						      &position);
1675 
1676 			amdgpu_dm_ism_commit_event(
1677 				&amdgpu_crtc->ism,
1678 				DM_ISM_EVENT_END_CURSOR_UPDATE);
1679 			mutex_unlock(&adev->dm.dc_lock);
1680 		}
1681 		return;
1682 	}
1683 
1684 	amdgpu_crtc->cursor_width = plane->state->crtc_w;
1685 	amdgpu_crtc->cursor_height = plane->state->crtc_h;
1686 
1687 	memset(&attributes, 0, sizeof(attributes));
1688 	attributes.address.high_part = upper_32_bits(address);
1689 	attributes.address.low_part  = lower_32_bits(address);
1690 	attributes.width             = plane->state->crtc_w;
1691 	attributes.height            = plane->state->crtc_h;
1692 	attributes.color_format      = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
1693 	attributes.rotation_angle    = 0;
1694 	attributes.attribute_flags.value = 0;
1695 
1696 	/* Enable cursor degamma ROM on DCN3+ for implicit sRGB degamma in DRM
1697 	 * legacy gamma setup.
1698 	 */
1699 	if (crtc_state->cm_is_degamma_srgb &&
1700 	    adev->dm.dc->caps.color.dpp.gamma_corr)
1701 		attributes.attribute_flags.bits.ENABLE_CURSOR_DEGAMMA = 1;
1702 
1703 	if (afb)
1704 		attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0];
1705 
1706 	if (crtc_state->stream) {
1707 		mutex_lock(&adev->dm.dc_lock);
1708 		amdgpu_dm_ism_commit_event(
1709 			&amdgpu_crtc->ism,
1710 			DM_ISM_EVENT_BEGIN_CURSOR_UPDATE);
1711 
1712 		if (!dc_stream_program_cursor_attributes(crtc_state->stream,
1713 							 &attributes))
1714 			DRM_ERROR("DC failed to set cursor attributes\n");
1715 
1716 		if (!dc_stream_program_cursor_position(crtc_state->stream,
1717 						   &position))
1718 			DRM_ERROR("DC failed to set cursor position\n");
1719 
1720 		amdgpu_dm_ism_commit_event(
1721 			&amdgpu_crtc->ism,
1722 			DM_ISM_EVENT_END_CURSOR_UPDATE);
1723 		mutex_unlock(&adev->dm.dc_lock);
1724 	}
1725 }
1726 EXPORT_IF_KUNIT(amdgpu_dm_plane_handle_cursor_update);
1727 
1728 STATIC_IFN_KUNIT void amdgpu_dm_plane_atomic_async_update(struct drm_plane *plane,
1729 							  struct drm_atomic_commit *state)
1730 {
1731 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
1732 									   plane);
1733 	struct drm_plane_state *old_state =
1734 		drm_atomic_get_old_plane_state(state, plane);
1735 
1736 	trace_amdgpu_dm_atomic_update_cursor(new_state);
1737 
1738 	swap(plane->state->fb, new_state->fb);
1739 
1740 	plane->state->src_x = new_state->src_x;
1741 	plane->state->src_y = new_state->src_y;
1742 	plane->state->src_w = new_state->src_w;
1743 	plane->state->src_h = new_state->src_h;
1744 	plane->state->crtc_x = new_state->crtc_x;
1745 	plane->state->crtc_y = new_state->crtc_y;
1746 	plane->state->crtc_w = new_state->crtc_w;
1747 	plane->state->crtc_h = new_state->crtc_h;
1748 
1749 	amdgpu_dm_plane_handle_cursor_update(plane, old_state);
1750 }
1751 EXPORT_IF_KUNIT(amdgpu_dm_plane_atomic_async_update);
1752 
1753 STATIC_IFN_KUNIT void amdgpu_dm_plane_panic_flush(struct drm_plane *plane)
1754 {
1755 	struct dm_plane_state *dm_plane_state = to_dm_plane_state(plane->state);
1756 	struct drm_framebuffer *fb = plane->state->fb;
1757 	struct dc_plane_state *dc_plane_state;
1758 
1759 	if (!dm_plane_state || !dm_plane_state->dc_state)
1760 		return;
1761 
1762 	dc_plane_state = dm_plane_state->dc_state;
1763 
1764 	dc_plane_force_dcc_and_tiling_disable(dc_plane_state, fb->modifier ? true : false);
1765 }
1766 EXPORT_IF_KUNIT(amdgpu_dm_plane_panic_flush);
1767 
1768 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
1769 	.prepare_fb = amdgpu_dm_plane_helper_prepare_fb,
1770 	.cleanup_fb = amdgpu_dm_plane_helper_cleanup_fb,
1771 	.atomic_check = amdgpu_dm_plane_atomic_check,
1772 	.atomic_async_check = amdgpu_dm_plane_atomic_async_check,
1773 	.atomic_async_update = amdgpu_dm_plane_atomic_async_update
1774 };
1775 
1776 static const struct drm_plane_helper_funcs dm_primary_plane_helper_funcs = {
1777 	.prepare_fb = amdgpu_dm_plane_helper_prepare_fb,
1778 	.cleanup_fb = amdgpu_dm_plane_helper_cleanup_fb,
1779 	.atomic_check = amdgpu_dm_plane_atomic_check,
1780 	.atomic_async_check = amdgpu_dm_plane_atomic_async_check,
1781 	.atomic_async_update = amdgpu_dm_plane_atomic_async_update,
1782 	.get_scanout_buffer = amdgpu_display_get_scanout_buffer,
1783 	.panic_flush = amdgpu_dm_plane_panic_flush,
1784 };
1785 
1786 STATIC_IFN_KUNIT struct drm_plane_state *amdgpu_dm_plane_drm_plane_create_state(struct drm_plane *plane)
1787 {
1788 	struct dm_plane_state *amdgpu_state;
1789 
1790 	amdgpu_state = kzalloc_obj(*amdgpu_state);
1791 	if (!amdgpu_state)
1792 		return ERR_PTR(-ENOMEM);
1793 
1794 	amdgpu_state->flip_addr = kzalloc_obj(*amdgpu_state->flip_addr);
1795 	amdgpu_state->scaling_info = kzalloc_obj(*amdgpu_state->scaling_info);
1796 	amdgpu_state->plane_info = kzalloc_obj(*amdgpu_state->plane_info);
1797 	if (!amdgpu_state->flip_addr || !amdgpu_state->scaling_info ||
1798 	    !amdgpu_state->plane_info) {
1799 		kfree(amdgpu_state->flip_addr);
1800 		kfree(amdgpu_state->scaling_info);
1801 		kfree(amdgpu_state->plane_info);
1802 		kfree(amdgpu_state);
1803 		return ERR_PTR(-ENOMEM);
1804 	}
1805 
1806 	if (plane->state)
1807 		plane->funcs->atomic_destroy_state(plane, plane->state);
1808 
1809 	__drm_atomic_helper_plane_state_init(&amdgpu_state->base, plane);
1810 	amdgpu_state->degamma_tf = AMDGPU_TRANSFER_FUNCTION_DEFAULT;
1811 	amdgpu_state->hdr_mult = AMDGPU_HDR_MULT_DEFAULT;
1812 	amdgpu_state->shaper_tf = AMDGPU_TRANSFER_FUNCTION_DEFAULT;
1813 	amdgpu_state->blend_tf = AMDGPU_TRANSFER_FUNCTION_DEFAULT;
1814 
1815 	return &amdgpu_state->base;
1816 }
1817 EXPORT_IF_KUNIT(amdgpu_dm_plane_drm_plane_create_state);
1818 
1819 STATIC_IFN_KUNIT struct drm_plane_state *
1820 amdgpu_dm_plane_drm_plane_duplicate_state(struct drm_plane *plane)
1821 {
1822 	struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
1823 
1824 	old_dm_plane_state = to_dm_plane_state(plane->state);
1825 	dm_plane_state = kzalloc_obj(*dm_plane_state);
1826 	if (!dm_plane_state)
1827 		return NULL;
1828 
1829 	dm_plane_state->flip_addr = kmemdup(old_dm_plane_state->flip_addr,
1830 		sizeof(*old_dm_plane_state->flip_addr), GFP_KERNEL);
1831 	dm_plane_state->scaling_info = kmemdup(old_dm_plane_state->scaling_info,
1832 		sizeof(*old_dm_plane_state->scaling_info), GFP_KERNEL);
1833 	dm_plane_state->plane_info = kmemdup(old_dm_plane_state->plane_info,
1834 		sizeof(*old_dm_plane_state->plane_info), GFP_KERNEL);
1835 	if (!dm_plane_state->flip_addr || !dm_plane_state->scaling_info ||
1836 	    !dm_plane_state->plane_info) {
1837 		kfree(dm_plane_state->flip_addr);
1838 		kfree(dm_plane_state->scaling_info);
1839 		kfree(dm_plane_state->plane_info);
1840 		kfree(dm_plane_state);
1841 		return NULL;
1842 	}
1843 
1844 	__drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
1845 
1846 	if (old_dm_plane_state->dc_state) {
1847 		dm_plane_state->dc_state = old_dm_plane_state->dc_state;
1848 		dc_plane_state_retain(dm_plane_state->dc_state);
1849 	}
1850 
1851 	if (old_dm_plane_state->degamma_lut)
1852 		dm_plane_state->degamma_lut =
1853 			drm_property_blob_get(old_dm_plane_state->degamma_lut);
1854 	if (old_dm_plane_state->ctm)
1855 		dm_plane_state->ctm =
1856 			drm_property_blob_get(old_dm_plane_state->ctm);
1857 	if (old_dm_plane_state->shaper_lut)
1858 		dm_plane_state->shaper_lut =
1859 			drm_property_blob_get(old_dm_plane_state->shaper_lut);
1860 	if (old_dm_plane_state->lut3d)
1861 		dm_plane_state->lut3d =
1862 			drm_property_blob_get(old_dm_plane_state->lut3d);
1863 	if (old_dm_plane_state->blend_lut)
1864 		dm_plane_state->blend_lut =
1865 			drm_property_blob_get(old_dm_plane_state->blend_lut);
1866 
1867 	dm_plane_state->degamma_tf = old_dm_plane_state->degamma_tf;
1868 	dm_plane_state->hdr_mult = old_dm_plane_state->hdr_mult;
1869 	dm_plane_state->shaper_tf = old_dm_plane_state->shaper_tf;
1870 	dm_plane_state->blend_tf = old_dm_plane_state->blend_tf;
1871 
1872 	return &dm_plane_state->base;
1873 }
1874 EXPORT_IF_KUNIT(amdgpu_dm_plane_drm_plane_duplicate_state);
1875 
1876 STATIC_IFN_KUNIT bool amdgpu_dm_plane_format_mod_supported(struct drm_plane *plane,
1877 							   uint32_t format,
1878 							   uint64_t modifier)
1879 {
1880 	struct amdgpu_device *adev = drm_to_adev(plane->dev);
1881 	const struct drm_format_info *info = drm_format_info(format);
1882 	const u32 bpp = drm_format_info_bpp(info, 0);
1883 	int i;
1884 
1885 	if (!info)
1886 		return false;
1887 
1888 	/*
1889 	 * We always have to allow these modifiers:
1890 	 * 1. Core DRM checks for LINEAR support if userspace does not provide modifiers.
1891 	 * 2. Not passing any modifiers is the same as explicitly passing INVALID.
1892 	 */
1893 	if (modifier == DRM_FORMAT_MOD_LINEAR ||
1894 	    modifier == DRM_FORMAT_MOD_INVALID) {
1895 		return true;
1896 	}
1897 
1898 	/* Check that the modifier is on the list of the plane's supported modifiers. */
1899 	for (i = 0; i < plane->modifier_count; i++) {
1900 		if (modifier == plane->modifiers[i])
1901 			break;
1902 	}
1903 	if (i == plane->modifier_count)
1904 		return false;
1905 
1906 	if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) == AMD_FMT_MOD_TILE_VER_GFX6)
1907 		return amdgpu_dm_plane_gfx6_format_mod_supported(adev, bpp, modifier);
1908 
1909 	/* GFX9+ modifers are not supported on GFX8 and older. */
1910 	if (adev->family < AMDGPU_FAMILY_AI)
1911 		return false;
1912 
1913 	/* GFX12 doesn't have these limitations. */
1914 	if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) <= AMD_FMT_MOD_TILE_VER_GFX11) {
1915 		enum dm_micro_swizzle microtile = amdgpu_dm_plane_modifier_gfx9_swizzle_mode(modifier) & 3;
1916 
1917 		/*
1918 		 * For D swizzle the canonical modifier depends on the bpp, so check
1919 		 * it here.
1920 		 */
1921 		if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) == AMD_FMT_MOD_TILE_VER_GFX9 &&
1922 		    adev->family >= AMDGPU_FAMILY_NV) {
1923 			if (microtile == MICRO_SWIZZLE_D && info->cpp[0] == 4)
1924 				return false;
1925 		}
1926 
1927 		if (adev->family >= AMDGPU_FAMILY_RV && microtile == MICRO_SWIZZLE_D &&
1928 		    info->cpp[0] < 8)
1929 			return false;
1930 
1931 		if (amdgpu_dm_plane_modifier_has_dcc(modifier)) {
1932 			/* Per radeonsi comments 16/64 bpp are more complicated. */
1933 			if (info->cpp[0] != 4)
1934 				return false;
1935 			/* We support multi-planar formats, but not when combined with
1936 			 * additional DCC metadata planes.
1937 			 */
1938 			if (info->num_planes > 1)
1939 				return false;
1940 		}
1941 	}
1942 
1943 	return true;
1944 }
1945 EXPORT_IF_KUNIT(amdgpu_dm_plane_format_mod_supported);
1946 
1947 STATIC_IFN_KUNIT void amdgpu_dm_plane_drm_plane_destroy_state(struct drm_plane *plane,
1948 							      struct drm_plane_state *state)
1949 {
1950 	struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
1951 
1952 	if (dm_plane_state->degamma_lut)
1953 		drm_property_blob_put(dm_plane_state->degamma_lut);
1954 	if (dm_plane_state->ctm)
1955 		drm_property_blob_put(dm_plane_state->ctm);
1956 	if (dm_plane_state->lut3d)
1957 		drm_property_blob_put(dm_plane_state->lut3d);
1958 	if (dm_plane_state->shaper_lut)
1959 		drm_property_blob_put(dm_plane_state->shaper_lut);
1960 	if (dm_plane_state->blend_lut)
1961 		drm_property_blob_put(dm_plane_state->blend_lut);
1962 
1963 	kfree(dm_plane_state->flip_addr);
1964 	kfree(dm_plane_state->scaling_info);
1965 	kfree(dm_plane_state->plane_info);
1966 
1967 	if (dm_plane_state->dc_state)
1968 		dc_plane_state_release(dm_plane_state->dc_state);
1969 
1970 	drm_atomic_helper_plane_destroy_state(plane, state);
1971 }
1972 EXPORT_IF_KUNIT(amdgpu_dm_plane_drm_plane_destroy_state);
1973 
1974 #ifdef AMD_PRIVATE_COLOR
1975 static void
1976 dm_atomic_plane_attach_color_mgmt_properties(struct amdgpu_display_manager *dm,
1977 					     struct drm_plane *plane)
1978 {
1979 	struct amdgpu_mode_info mode_info = dm->adev->mode_info;
1980 	struct dpp_color_caps dpp_color_caps = dm->dc->caps.color.dpp;
1981 
1982 	/* Check HW color pipeline capabilities on DPP block (pre-blending)
1983 	 * before exposing related properties.
1984 	 */
1985 	if (dpp_color_caps.dgam_ram || dpp_color_caps.gamma_corr) {
1986 		drm_object_attach_property(&plane->base,
1987 					   mode_info.plane_degamma_lut_property,
1988 					   0);
1989 		drm_object_attach_property(&plane->base,
1990 					   mode_info.plane_degamma_lut_size_property,
1991 					   MAX_COLOR_LUT_ENTRIES);
1992 		drm_object_attach_property(&plane->base,
1993 					   dm->adev->mode_info.plane_degamma_tf_property,
1994 					   AMDGPU_TRANSFER_FUNCTION_DEFAULT);
1995 	}
1996 	/* HDR MULT is always available */
1997 	drm_object_attach_property(&plane->base,
1998 				   dm->adev->mode_info.plane_hdr_mult_property,
1999 				   AMDGPU_HDR_MULT_DEFAULT);
2000 
2001 	/* Only enable plane CTM if both DPP and MPC gamut remap is available. */
2002 	if (dm->dc->caps.color.mpc.gamut_remap)
2003 		drm_object_attach_property(&plane->base,
2004 					   dm->adev->mode_info.plane_ctm_property, 0);
2005 
2006 	if (dpp_color_caps.hw_3d_lut || dm->dc->caps.color.mpc.preblend) {
2007 		drm_object_attach_property(&plane->base,
2008 					   mode_info.plane_shaper_lut_property, 0);
2009 		drm_object_attach_property(&plane->base,
2010 					   mode_info.plane_shaper_lut_size_property,
2011 					   MAX_COLOR_LUT_ENTRIES);
2012 		drm_object_attach_property(&plane->base,
2013 					   mode_info.plane_shaper_tf_property,
2014 					   AMDGPU_TRANSFER_FUNCTION_DEFAULT);
2015 		drm_object_attach_property(&plane->base,
2016 					   mode_info.plane_lut3d_property, 0);
2017 		drm_object_attach_property(&plane->base,
2018 					   mode_info.plane_lut3d_size_property,
2019 					   MAX_COLOR_3DLUT_SIZE);
2020 	}
2021 
2022 	if (dpp_color_caps.ogam_ram || dm->dc->caps.color.mpc.preblend) {
2023 		drm_object_attach_property(&plane->base,
2024 					   mode_info.plane_blend_lut_property, 0);
2025 		drm_object_attach_property(&plane->base,
2026 					   mode_info.plane_blend_lut_size_property,
2027 					   MAX_COLOR_LUT_ENTRIES);
2028 		drm_object_attach_property(&plane->base,
2029 					   mode_info.plane_blend_tf_property,
2030 					   AMDGPU_TRANSFER_FUNCTION_DEFAULT);
2031 	}
2032 }
2033 
2034 static int
2035 dm_atomic_plane_set_property(struct drm_plane *plane,
2036 			     struct drm_plane_state *state,
2037 			     struct drm_property *property,
2038 			     uint64_t val)
2039 {
2040 	struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
2041 	struct amdgpu_device *adev = drm_to_adev(plane->dev);
2042 	bool replaced = false;
2043 	int ret;
2044 
2045 	if (property == adev->mode_info.plane_degamma_lut_property) {
2046 		ret = drm_property_replace_blob_from_id(plane->dev,
2047 							&dm_plane_state->degamma_lut,
2048 							val,
2049 							-1, -1, sizeof(struct drm_color_lut),
2050 							&replaced);
2051 		dm_plane_state->base.color_mgmt_changed |= replaced;
2052 		return ret;
2053 	} else if (property == adev->mode_info.plane_degamma_tf_property) {
2054 		if (dm_plane_state->degamma_tf != val) {
2055 			dm_plane_state->degamma_tf = val;
2056 			dm_plane_state->base.color_mgmt_changed = 1;
2057 		}
2058 	} else if (property == adev->mode_info.plane_hdr_mult_property) {
2059 		if (dm_plane_state->hdr_mult != val) {
2060 			dm_plane_state->hdr_mult = val;
2061 			dm_plane_state->base.color_mgmt_changed = 1;
2062 		}
2063 	} else if (property == adev->mode_info.plane_ctm_property) {
2064 		ret = drm_property_replace_blob_from_id(plane->dev,
2065 							&dm_plane_state->ctm,
2066 							val,
2067 							-1, sizeof(struct drm_color_ctm_3x4), -1,
2068 							&replaced);
2069 		dm_plane_state->base.color_mgmt_changed |= replaced;
2070 		return ret;
2071 	} else if (property == adev->mode_info.plane_shaper_lut_property) {
2072 		ret = drm_property_replace_blob_from_id(plane->dev,
2073 							&dm_plane_state->shaper_lut,
2074 							val,
2075 							-1, -1, sizeof(struct drm_color_lut),
2076 							&replaced);
2077 		dm_plane_state->base.color_mgmt_changed |= replaced;
2078 		return ret;
2079 	} else if (property == adev->mode_info.plane_shaper_tf_property) {
2080 		if (dm_plane_state->shaper_tf != val) {
2081 			dm_plane_state->shaper_tf = val;
2082 			dm_plane_state->base.color_mgmt_changed = 1;
2083 		}
2084 	} else if (property == adev->mode_info.plane_lut3d_property) {
2085 		ret = drm_property_replace_blob_from_id(plane->dev,
2086 							&dm_plane_state->lut3d,
2087 							val,
2088 							-1, -1, sizeof(struct drm_color_lut),
2089 							&replaced);
2090 		dm_plane_state->base.color_mgmt_changed |= replaced;
2091 		return ret;
2092 	} else if (property == adev->mode_info.plane_blend_lut_property) {
2093 		ret = drm_property_replace_blob_from_id(plane->dev,
2094 							&dm_plane_state->blend_lut,
2095 							val,
2096 							-1, -1, sizeof(struct drm_color_lut),
2097 							&replaced);
2098 		dm_plane_state->base.color_mgmt_changed |= replaced;
2099 		return ret;
2100 	} else if (property == adev->mode_info.plane_blend_tf_property) {
2101 		if (dm_plane_state->blend_tf != val) {
2102 			dm_plane_state->blend_tf = val;
2103 			dm_plane_state->base.color_mgmt_changed = 1;
2104 		}
2105 	} else {
2106 		drm_dbg_atomic(plane->dev,
2107 			       "[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
2108 			       plane->base.id, plane->name,
2109 			       property->base.id, property->name);
2110 		return -EINVAL;
2111 	}
2112 
2113 	return 0;
2114 }
2115 
2116 static int
2117 dm_atomic_plane_get_property(struct drm_plane *plane,
2118 			     const struct drm_plane_state *state,
2119 			     struct drm_property *property,
2120 			     uint64_t *val)
2121 {
2122 	struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
2123 	struct amdgpu_device *adev = drm_to_adev(plane->dev);
2124 
2125 	if (property == adev->mode_info.plane_degamma_lut_property) {
2126 		*val = (dm_plane_state->degamma_lut) ?
2127 			dm_plane_state->degamma_lut->base.id : 0;
2128 	} else if (property == adev->mode_info.plane_degamma_tf_property) {
2129 		*val = dm_plane_state->degamma_tf;
2130 	} else if (property == adev->mode_info.plane_hdr_mult_property) {
2131 		*val = dm_plane_state->hdr_mult;
2132 	} else if (property == adev->mode_info.plane_ctm_property) {
2133 		*val = (dm_plane_state->ctm) ?
2134 			dm_plane_state->ctm->base.id : 0;
2135 	} else 	if (property == adev->mode_info.plane_shaper_lut_property) {
2136 		*val = (dm_plane_state->shaper_lut) ?
2137 			dm_plane_state->shaper_lut->base.id : 0;
2138 	} else if (property == adev->mode_info.plane_shaper_tf_property) {
2139 		*val = dm_plane_state->shaper_tf;
2140 	} else 	if (property == adev->mode_info.plane_lut3d_property) {
2141 		*val = (dm_plane_state->lut3d) ?
2142 			dm_plane_state->lut3d->base.id : 0;
2143 	} else 	if (property == adev->mode_info.plane_blend_lut_property) {
2144 		*val = (dm_plane_state->blend_lut) ?
2145 			dm_plane_state->blend_lut->base.id : 0;
2146 	} else if (property == adev->mode_info.plane_blend_tf_property) {
2147 		*val = dm_plane_state->blend_tf;
2148 
2149 	} else {
2150 		return -EINVAL;
2151 	}
2152 
2153 	return 0;
2154 }
2155 #else
2156 
2157 #define MAX_COLOR_PIPELINES 5
2158 
2159 static int
2160 dm_plane_init_colorops(struct drm_plane *plane)
2161 {
2162 	struct drm_prop_enum_list pipelines[MAX_COLOR_PIPELINES] = {};
2163 	struct drm_device *dev = plane->dev;
2164 	struct amdgpu_device *adev = drm_to_adev(dev);
2165 	struct dc *dc = adev->dm.dc;
2166 	int len = 0;
2167 	int ret = 0;
2168 	int i;
2169 
2170 	if (plane->type == DRM_PLANE_TYPE_CURSOR)
2171 		return 0;
2172 
2173 	/* initialize pipeline */
2174 	if (dc->ctx->dce_version >= DCN_VERSION_3_0) {
2175 		ret = amdgpu_dm_initialize_default_pipeline(plane, &pipelines[len]);
2176 		if (ret) {
2177 			drm_err(plane->dev, "Failed to create color pipeline for plane %d: %pe\n",
2178 				plane->base.id, ERR_PTR(ret));
2179 			goto out;
2180 		}
2181 		len++;
2182 
2183 		/* Create COLOR_PIPELINE property and attach */
2184 		drm_plane_create_color_pipeline_property(plane, pipelines, len);
2185 	}
2186 
2187 out:
2188 	for (i = 0; i < len; i++)
2189 		kfree(pipelines[i].name);
2190 
2191 	return ret;
2192 }
2193 #endif
2194 
2195 static const struct drm_plane_funcs dm_plane_funcs = {
2196 	.update_plane	= drm_atomic_helper_update_plane,
2197 	.disable_plane	= drm_atomic_helper_disable_plane,
2198 	.destroy	= drm_plane_helper_destroy,
2199 	.atomic_create_state = amdgpu_dm_plane_drm_plane_create_state,
2200 	.atomic_duplicate_state = amdgpu_dm_plane_drm_plane_duplicate_state,
2201 	.atomic_destroy_state = amdgpu_dm_plane_drm_plane_destroy_state,
2202 	.format_mod_supported = amdgpu_dm_plane_format_mod_supported,
2203 	.format_mod_supported_async = amdgpu_dm_plane_format_mod_supported,
2204 #ifdef AMD_PRIVATE_COLOR
2205 	.atomic_set_property = dm_atomic_plane_set_property,
2206 	.atomic_get_property = dm_atomic_plane_get_property,
2207 #endif
2208 	DRM_PANIC_PLANE_FUNCS,
2209 };
2210 
2211 int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
2212 				struct drm_plane *plane,
2213 				unsigned long possible_crtcs,
2214 				const struct dc_plane_cap *plane_cap)
2215 {
2216 	uint32_t formats[32];
2217 	int num_formats;
2218 	int res = -EPERM;
2219 	unsigned int supported_rotations;
2220 	uint64_t *modifiers = NULL;
2221 	unsigned int primary_zpos = dm->dc->caps.max_slave_planes;
2222 
2223 	num_formats = amdgpu_dm_plane_get_plane_formats(plane, plane_cap, formats,
2224 							ARRAY_SIZE(formats));
2225 
2226 	res = amdgpu_dm_plane_get_plane_modifiers(dm->adev, plane->type, &modifiers);
2227 	if (res)
2228 		return res;
2229 
2230 	if (modifiers == NULL)
2231 		adev_to_drm(dm->adev)->mode_config.fb_modifiers_not_supported = true;
2232 
2233 	res = drm_universal_plane_init(adev_to_drm(dm->adev), plane, possible_crtcs,
2234 				       &dm_plane_funcs, formats, num_formats,
2235 				       modifiers, plane->type, NULL);
2236 	kfree(modifiers);
2237 	if (res)
2238 		return res;
2239 
2240 	/* Blend mode support varies on DCE generations according to HW caps
2241 	 * and number of planes per CRTC. However, as current driver
2242 	 * implementation only creates one primary and one cursor plane per
2243 	 * CRTC for DCE (overlay is only created if
2244 	 * DC_PLANE_TYPE_DCN_UNIVERSAL), the primary plane blend mode is
2245 	 * ignored across DCE versions. Keep PREMULTI to avoid uAPI
2246 	 * regressions: it was the default/mandatory mode for many years and,
2247 	 * with no overlay plane, primary composes on top of a black
2248 	 * background, where PREMULTI and PIXEL_NONE are equivalent.
2249 	 */
2250 	if (plane_cap && plane_cap->type != DC_PLANE_TYPE_DCN_UNIVERSAL) {
2251 		unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
2252 					  BIT(DRM_MODE_BLEND_PREMULTI);
2253 
2254 		drm_plane_create_blend_mode_property(plane, blend_caps);
2255 	} else if ((plane->type == DRM_PLANE_TYPE_OVERLAY ||
2256 		    plane->type == DRM_PLANE_TYPE_PRIMARY) &&
2257 		   plane_cap && plane_cap->per_pixel_alpha) {
2258 		unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
2259 					  BIT(DRM_MODE_BLEND_PREMULTI) |
2260 					  BIT(DRM_MODE_BLEND_COVERAGE);
2261 
2262 		drm_plane_create_blend_mode_property(plane, blend_caps);
2263 
2264 		if (plane->type == DRM_PLANE_TYPE_OVERLAY)
2265 			drm_plane_create_alpha_property(plane);
2266 	}
2267 
2268 	/* Cursor color format is set to CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA
2269 	 * by default, so only advertise DRM_MODE_BLEND_PREMULTI blend mode for
2270 	 * this type of plane.
2271 	 */
2272 	if (plane->type == DRM_PLANE_TYPE_CURSOR)
2273 		drm_plane_create_blend_mode_property(plane, BIT(DRM_MODE_BLEND_PREMULTI));
2274 
2275 	if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
2276 		/*
2277 		 * Allow OVERLAY planes to be used as underlays by assigning an
2278 		 * immutable zpos = # of OVERLAY planes to the PRIMARY plane.
2279 		 */
2280 		drm_plane_create_zpos_immutable_property(plane, primary_zpos);
2281 	} else if (plane->type == DRM_PLANE_TYPE_OVERLAY) {
2282 		/*
2283 		 * OVERLAY planes can be below or above the PRIMARY, but cannot
2284 		 * be above the CURSOR plane.
2285 		 */
2286 		unsigned int zpos = primary_zpos + 1 + drm_plane_index(plane);
2287 
2288 		drm_plane_create_zpos_property(plane, zpos, 0, 254);
2289 	} else if (plane->type == DRM_PLANE_TYPE_CURSOR) {
2290 		drm_plane_create_zpos_immutable_property(plane, 255);
2291 	}
2292 
2293 	if ((plane->type == DRM_PLANE_TYPE_PRIMARY ||
2294 	     plane->type == DRM_PLANE_TYPE_OVERLAY) &&
2295 	    plane_cap &&
2296 	    (plane_cap->pixel_format_support.nv12 ||
2297 	     plane_cap->pixel_format_support.p010)) {
2298 		/* This only affects YUV formats. */
2299 		drm_plane_create_color_properties(
2300 			plane,
2301 			BIT(DRM_COLOR_YCBCR_BT601) |
2302 			BIT(DRM_COLOR_YCBCR_BT709) |
2303 			BIT(DRM_COLOR_YCBCR_BT2020),
2304 			BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
2305 			BIT(DRM_COLOR_YCBCR_FULL_RANGE),
2306 			DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE);
2307 	}
2308 
2309 	supported_rotations =
2310 		DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
2311 		DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
2312 
2313 	if (dm->adev->asic_type >= CHIP_BONAIRE &&
2314 	    plane->type != DRM_PLANE_TYPE_CURSOR)
2315 		drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0,
2316 						   supported_rotations);
2317 
2318 	if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) > IP_VERSION(3, 0, 1) &&
2319 	    plane->type != DRM_PLANE_TYPE_CURSOR)
2320 		drm_plane_enable_fb_damage_clips(plane);
2321 
2322 	if (plane->type == DRM_PLANE_TYPE_PRIMARY)
2323 		drm_plane_helper_add(plane, &dm_primary_plane_helper_funcs);
2324 	else
2325 		drm_plane_helper_add(plane, &dm_plane_helper_funcs);
2326 
2327 #ifdef AMD_PRIVATE_COLOR
2328 	dm_atomic_plane_attach_color_mgmt_properties(dm, plane);
2329 #else
2330 	res = dm_plane_init_colorops(plane);
2331 	if (res)
2332 		return res;
2333 #endif
2334 
2335 	/* Create the plane state */
2336 	if (plane->funcs->atomic_create_state) {
2337 		struct drm_plane_state *plane_state;
2338 
2339 		plane_state = plane->funcs->atomic_create_state(plane);
2340 		if (IS_ERR(plane_state))
2341 			return PTR_ERR(plane_state);
2342 
2343 		plane->state = plane_state;
2344 	}
2345 
2346 	return 0;
2347 }
2348 EXPORT_IF_KUNIT(amdgpu_dm_plane_init);
2349 
2350 bool amdgpu_dm_plane_is_video_format(uint32_t format)
2351 {
2352 	int i;
2353 
2354 	for (i = 0; i < ARRAY_SIZE(video_formats); i++)
2355 		if (format == video_formats[i])
2356 			return true;
2357 
2358 	return false;
2359 }
2360 EXPORT_IF_KUNIT(amdgpu_dm_plane_is_video_format);
2361 
2362