xref: /linux/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_cursor.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 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.h>
28 #include <drm/drm_atomic_helper.h>
29 #include <drm/drm_blend.h>
30 #include <drm/drm_fourcc.h>
31 #include <drm/drm_framebuffer.h>
32 #include <drm/drm_plane.h>
33 #include <drm/drm_colorop.h>
34 
35 #include "dc.h"
36 #include "dal_asic_id.h"
37 #include "amdgpu.h"
38 #include "amdgpu_display.h"
39 #include "amdgpu_dm.h"
40 #include "amdgpu_dm_plane.h"
41 #include "amdgpu_dm_cursor.h"
42 #include "dm_helpers.h"
43 
44 static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
45 			      struct drm_plane_state *new_plane_state,
46 			      struct drm_framebuffer *fb)
47 {
48 	struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
49 	struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
50 	unsigned int pitch;
51 	bool linear;
52 
53 	if (fb->width > new_acrtc->max_cursor_width ||
54 	    fb->height > new_acrtc->max_cursor_height) {
55 		drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB size %dx%d\n",
56 				 new_plane_state->fb->width,
57 				 new_plane_state->fb->height);
58 		return -EINVAL;
59 	}
60 	if (new_plane_state->src_w != fb->width << 16 ||
61 	    new_plane_state->src_h != fb->height << 16) {
62 		drm_dbg_atomic(adev_to_drm(adev), "Cropping not supported for cursor plane\n");
63 		return -EINVAL;
64 	}
65 
66 	/* Pitch in pixels */
67 	pitch = fb->pitches[0] / fb->format->cpp[0];
68 
69 	if (fb->width != pitch) {
70 		drm_dbg_atomic(adev_to_drm(adev), "Cursor FB width %d doesn't match pitch %d",
71 				 fb->width, pitch);
72 		return -EINVAL;
73 	}
74 
75 	switch (pitch) {
76 	case 64:
77 	case 128:
78 	case 256:
79 		/* FB pitch is supported by cursor plane */
80 		break;
81 	default:
82 		drm_dbg_atomic(adev_to_drm(adev), "Bad cursor FB pitch %d px\n", pitch);
83 		return -EINVAL;
84 	}
85 
86 	/* Core DRM takes care of checking FB modifiers, so we only need to
87 	 * check tiling flags when the FB doesn't have a modifier.
88 	 */
89 	if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
90 #if defined(CONFIG_DRM_AMD_DC_DCN6_0) || defined(CONFIG_DRM_AMD_DC_DCN5_0)
91 		if (adev->family == AMDGPU_FAMILY_GC_12_0_0
92 		    || adev->family == AMDGPU_FAMILY_GC_13_0_1) {
93 #else
94 		if (adev->family == AMDGPU_FAMILY_GC_12_0_0) {
95 #endif
96 			linear = AMDGPU_TILING_GET(afb->tiling_flags, GFX12_SWIZZLE_MODE) == 0;
97 		} else if (adev->family >= AMDGPU_FAMILY_AI) {
98 			linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
99 		} else {
100 			linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
101 				 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
102 				 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
103 		}
104 		if (!linear) {
105 			drm_dbg_atomic(adev_to_drm(adev), "Cursor FB not linear");
106 			return -EINVAL;
107 		}
108 	}
109 
110 	return 0;
111 }
112 
113 /*
114  * Helper function for checking the cursor in native mode
115  */
116 int amdgpu_dm_check_native_cursor_state(struct drm_crtc *new_plane_crtc,
117 					struct drm_plane *plane,
118 					struct drm_plane_state *new_plane_state,
119 					bool enable)
120 {
121 
122 	struct amdgpu_crtc *new_acrtc;
123 	int ret;
124 
125 	if (!enable || !new_plane_crtc ||
126 	    drm_atomic_plane_disabling(plane->state, new_plane_state))
127 		return 0;
128 
129 	new_acrtc = to_amdgpu_crtc(new_plane_crtc);
130 
131 	if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
132 		drm_dbg_atomic(new_plane_crtc->dev, "Cropping not supported for cursor plane\n");
133 		return -EINVAL;
134 	}
135 
136 	if (new_plane_state->fb) {
137 		ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
138 						new_plane_state->fb);
139 		if (ret)
140 			return ret;
141 	}
142 
143 	return 0;
144 }
145 
146 bool amdgpu_dm_should_update_native_cursor(struct drm_atomic_commit *state,
147 					   struct drm_crtc *old_plane_crtc,
148 					   struct drm_crtc *new_plane_crtc,
149 					   bool enable)
150 {
151 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
152 	struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
153 
154 	if (!enable) {
155 		if (old_plane_crtc == NULL)
156 			return true;
157 
158 		old_crtc_state = drm_atomic_get_old_crtc_state(
159 			state, old_plane_crtc);
160 		dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
161 
162 		return dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
163 	}
164 
165 	if (new_plane_crtc == NULL)
166 		return true;
167 
168 	new_crtc_state = drm_atomic_get_new_crtc_state(
169 		state, new_plane_crtc);
170 	dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
171 
172 	return dm_new_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE;
173 }
174 EXPORT_IF_KUNIT(amdgpu_dm_should_update_native_cursor);
175 
176 STATIC_IFN_KUNIT void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
177 					 int *src_w, int *src_h)
178 {
179 	switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
180 	case DRM_MODE_ROTATE_90:
181 	case DRM_MODE_ROTATE_270:
182 		*src_w = plane_state->src_h >> 16;
183 		*src_h = plane_state->src_w >> 16;
184 		break;
185 	case DRM_MODE_ROTATE_0:
186 	case DRM_MODE_ROTATE_180:
187 	default:
188 		*src_w = plane_state->src_w >> 16;
189 		*src_h = plane_state->src_h >> 16;
190 		break;
191 	}
192 }
193 EXPORT_IF_KUNIT(dm_get_oriented_plane_size);
194 
195 STATIC_IFN_KUNIT void
196 dm_get_plane_scale(struct drm_plane_state *plane_state,
197 		   int *out_plane_scale_w, int *out_plane_scale_h)
198 {
199 	int plane_src_w, plane_src_h;
200 
201 	dm_get_oriented_plane_size(plane_state, &plane_src_w, &plane_src_h);
202 	*out_plane_scale_w = plane_src_w ? plane_state->crtc_w * 1000 / plane_src_w : 0;
203 	*out_plane_scale_h = plane_src_h ? plane_state->crtc_h * 1000 / plane_src_h : 0;
204 }
205 EXPORT_IF_KUNIT(dm_get_plane_scale);
206 
207 /**
208  * DOC: Cursor Modes - Native vs Overlay
209  *
210  * In native mode, the cursor uses a integrated cursor pipe within each DCN hw
211  * plane. It does not require a dedicated hw plane to enable, but it is
212  * subjected to the same z-order and scaling as the hw plane. It also has format
213  * restrictions, a RGB cursor in native mode cannot be enabled within a non-RGB
214  * hw plane.
215  *
216  * In overlay mode, the cursor uses a separate DCN hw plane, and thus has its
217  * own scaling and z-pos. It also has no blending restrictions. It lends to a
218  * cursor behavior more akin to a DRM client's expectations. However, it does
219  * occupy an extra DCN plane, and therefore will only be used if a DCN plane is
220  * available.
221  */
222 
223 /**
224  * dm_plane_color_pipeline_active() - Check if a plane's color pipeline active.
225  * @state: DRM atomic state
226  * @plane: DRM plane to check
227  * @use_old: if true, inspect the old colorop states; otherwise the new ones
228  *
229  * A color pipeline may be selected (color_pipeline != NULL) but still is
230  * inactive if every colorop in the chain is bypassed.  Only return
231  * true when at least one colorop has bypass == false, meaning the cursor
232  * would be subjected to the transformation in native mode.
233  *
234  * Return: true if the pipeline modifies pixels, false otherwise.
235  */
236 static bool dm_plane_color_pipeline_active(struct drm_atomic_commit *state,
237 					   struct drm_plane *plane,
238 					   bool use_old)
239 {
240 	struct drm_colorop *colorop;
241 	struct drm_colorop_state *old_colorop_state, *new_colorop_state;
242 	int i;
243 
244 	for_each_oldnew_colorop_in_state(state, colorop, old_colorop_state, new_colorop_state, i) {
245 		struct drm_colorop_state *cstate = use_old ? old_colorop_state : new_colorop_state;
246 
247 		if (cstate->colorop->plane != plane)
248 			continue;
249 		if (!cstate->bypass)
250 			return true;
251 	}
252 	return false;
253 }
254 
255 /**
256  * amdgpu_dm_crtc_get_cursor_mode() - Determine the required cursor mode on crtc
257  * @adev: amdgpu device
258  * @state: DRM atomic state
259  * @dm_crtc_state: amdgpu state for the CRTC containing the cursor
260  * @cursor_mode: Returns the required cursor mode on dm_crtc_state
261  *
262  * Get whether the cursor should be enabled in native mode, or overlay mode, on
263  * the dm_crtc_state.
264  *
265  * The cursor should be enabled in overlay mode if there exists an underlying
266  * plane - on which the cursor may be blended - that is either YUV formatted,
267  * scaled differently from the cursor, or has a color pipeline active.
268  *
269  * Since zpos info is required, drm_atomic_normalize_zpos must be called before
270  * calling this function.
271  *
272  * Return: 0 on success, or an error code if getting the cursor plane state
273  * failed.
274  */
275 int amdgpu_dm_crtc_get_cursor_mode(struct amdgpu_device *adev,
276 				   struct drm_atomic_commit *state,
277 				   struct dm_crtc_state *dm_crtc_state,
278 				   enum amdgpu_dm_cursor_mode *cursor_mode)
279 {
280 	struct drm_plane_state *old_plane_state, *plane_state, *cursor_state;
281 	struct drm_crtc_state *crtc_state = &dm_crtc_state->base;
282 	struct drm_plane *plane;
283 	bool consider_mode_change = false;
284 	bool entire_crtc_covered = false;
285 	bool cursor_changed = false;
286 	int underlying_scale_w, underlying_scale_h;
287 	int cursor_scale_w, cursor_scale_h;
288 	int i;
289 
290 	/* Overlay cursor not supported on HW before DCN
291 	 * DCN401/420 does not have the cursor-on-scaled-plane or cursor-on-yuv-plane restrictions
292 	 * as previous DCN generations, so enable native mode on DCN401/420
293 	 *
294 	 * Always set native cursor mode when the CRTC is disabled,
295 	 * to make sure it doesn't cause atomic commits to fail when
296 	 * they are trying to disable the CRTC.
297 	 */
298 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 0, 1) ||
299 	    amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 0) ||
300 #if defined(CONFIG_DRM_AMD_DC_DCN6_0)
301 	    amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 1) ||
302 	    amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(6, 0, 0) ||
303 #else
304 	    amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(4, 2, 1) ||
305 #endif
306 	    !dm_crtc_state->base.enable) {
307 		*cursor_mode = DM_CURSOR_NATIVE_MODE;
308 		return 0;
309 	}
310 
311 	/* Init cursor_mode to be the same as current */
312 	*cursor_mode = dm_crtc_state->cursor_mode;
313 
314 	/*
315 	 * Cursor mode can change if a plane's format changes, scale changes, is
316 	 * enabled/disabled, z-order changes, or color management properties change.
317 	 */
318 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, plane_state, i) {
319 		int new_scale_w, new_scale_h, old_scale_w, old_scale_h;
320 
321 		/* Only care about planes on this CRTC */
322 		if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0)
323 			continue;
324 
325 		if (plane->type == DRM_PLANE_TYPE_CURSOR)
326 			cursor_changed = true;
327 
328 		if (drm_atomic_plane_enabling(old_plane_state, plane_state) ||
329 		    drm_atomic_plane_disabling(old_plane_state, plane_state) ||
330 		    old_plane_state->fb->format != plane_state->fb->format) {
331 			consider_mode_change = true;
332 			break;
333 		}
334 
335 		dm_get_plane_scale(plane_state, &new_scale_w, &new_scale_h);
336 		dm_get_plane_scale(old_plane_state, &old_scale_w, &old_scale_h);
337 		if (new_scale_w != old_scale_w || new_scale_h != old_scale_h) {
338 			consider_mode_change = true;
339 			break;
340 		}
341 
342 		/*
343 		 * A non-cursor plane moving or resizing (without a scale change)
344 		 * changes how much of the CRTC it covers. This can create or
345 		 * remove a hole under the cursor and thus flip the required
346 		 * cursor mode (native vs overlay), so its destination rect must
347 		 * be re-evaluated too.
348 		 *
349 		 * The cursor plane itself is deliberately excluded: the cursor
350 		 * mode depends on the underlying planes' coverage, not on the
351 		 * cursor's position (see the entire_crtc_covered logic below).
352 		 * Triggering on cursor movement would force every legacy cursor
353 		 * update off its fast path, and in a cursor-only commit - where
354 		 * the underlying planes are not part of the state - the coverage
355 		 * loop would see no covering plane and misevaluate the mode as
356 		 * overlay, regressing flip-vs-cursor-legacy.
357 		 */
358 		if (plane->type != DRM_PLANE_TYPE_CURSOR &&
359 		    (old_plane_state->crtc_x != plane_state->crtc_x ||
360 		     old_plane_state->crtc_y != plane_state->crtc_y ||
361 		     old_plane_state->crtc_w != plane_state->crtc_w ||
362 		     old_plane_state->crtc_h != plane_state->crtc_h)) {
363 			consider_mode_change = true;
364 			break;
365 		}
366 
367 		if (dm_plane_color_pipeline_active(state, plane, true) !=
368 		    dm_plane_color_pipeline_active(state, plane, false)) {
369 			consider_mode_change = true;
370 			break;
371 		}
372 	}
373 
374 	if (!consider_mode_change && !crtc_state->zpos_changed)
375 		return 0;
376 
377 	/*
378 	 * If no cursor change on this CRTC, and not enabled on this CRTC, then
379 	 * no need to set cursor mode. This avoids needlessly locking the cursor
380 	 * state.
381 	 */
382 	if (!cursor_changed &&
383 	    !(drm_plane_mask(crtc_state->crtc->cursor) & crtc_state->plane_mask)) {
384 		return 0;
385 	}
386 
387 	cursor_state = drm_atomic_get_plane_state(state,
388 						  crtc_state->crtc->cursor);
389 	if (IS_ERR(cursor_state))
390 		return PTR_ERR(cursor_state);
391 
392 	/* Cursor is disabled */
393 	if (!cursor_state->fb)
394 		return 0;
395 
396 	/* For all planes in descending z-order (all of which are below cursor
397 	 * as per zpos definitions), check their scaling and format
398 	 */
399 	for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, plane_state) {
400 
401 		/* Only care about non-cursor planes on this CRTC */
402 		if ((drm_plane_mask(plane) & crtc_state->plane_mask) == 0 ||
403 		    plane->type == DRM_PLANE_TYPE_CURSOR)
404 			continue;
405 
406 		/* Underlying plane is YUV format - use overlay cursor */
407 		if (amdgpu_dm_plane_is_video_format(plane_state->fb->format->format)) {
408 			*cursor_mode = DM_CURSOR_OVERLAY_MODE;
409 			return 0;
410 		}
411 
412 		/* Underlying plane has an active color pipeline - cursor would be transformed */
413 		if (dm_plane_color_pipeline_active(state, plane, false)) {
414 			*cursor_mode = DM_CURSOR_OVERLAY_MODE;
415 			return 0;
416 		}
417 
418 		dm_get_plane_scale(plane_state,
419 				   &underlying_scale_w, &underlying_scale_h);
420 		dm_get_plane_scale(cursor_state,
421 				   &cursor_scale_w, &cursor_scale_h);
422 
423 		/* Underlying plane has different scale - use overlay cursor */
424 		if (cursor_scale_w != underlying_scale_w &&
425 		    cursor_scale_h != underlying_scale_h) {
426 			*cursor_mode = DM_CURSOR_OVERLAY_MODE;
427 			return 0;
428 		}
429 
430 		/* If this plane covers the whole CRTC, no need to check planes underneath */
431 		if (plane_state->crtc_x <= 0 && plane_state->crtc_y <= 0 &&
432 		    plane_state->crtc_x + plane_state->crtc_w >= crtc_state->mode.hdisplay &&
433 		    plane_state->crtc_y + plane_state->crtc_h >= crtc_state->mode.vdisplay) {
434 			entire_crtc_covered = true;
435 			break;
436 		}
437 	}
438 
439 	/* If planes do not cover the entire CRTC, use overlay mode to enable
440 	 * cursor over holes
441 	 */
442 	if (entire_crtc_covered)
443 		*cursor_mode = DM_CURSOR_NATIVE_MODE;
444 	else
445 		*cursor_mode = DM_CURSOR_OVERLAY_MODE;
446 
447 	return 0;
448 }
449