xref: /linux/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c (revision bc0400e6f41cbc43a5eaa5ed1c7749da7af7eb1d)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2022 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 #include <drm/drm_vblank.h>
27 #include <drm/drm_atomic_helper.h>
28 
29 #include "dc.h"
30 #include "amdgpu.h"
31 #include "amdgpu_dm_psr.h"
32 #include "amdgpu_dm_replay.h"
33 #include "amdgpu_dm_crtc.h"
34 #include "amdgpu_dm_plane.h"
35 #include "amdgpu_dm_trace.h"
36 #include "amdgpu_dm_debugfs.h"
37 #include "modules/inc/mod_power.h"
38 
39 #define HPD_DETECTION_PERIOD_uS 2000000
40 #define HPD_DETECTION_TIME_uS 100000
41 
42 void amdgpu_dm_crtc_handle_vblank(struct amdgpu_crtc *acrtc)
43 {
44 	struct drm_crtc *crtc = &acrtc->base;
45 	struct drm_device *dev = crtc->dev;
46 	unsigned long flags;
47 
48 	drm_crtc_handle_vblank(crtc);
49 
50 	spin_lock_irqsave(&dev->event_lock, flags);
51 
52 	/* Send completion event for cursor-only commits */
53 	if (acrtc->event && acrtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
54 		drm_crtc_send_vblank_event(crtc, acrtc->event);
55 		drm_crtc_vblank_put(crtc);
56 		acrtc->event = NULL;
57 	}
58 
59 	spin_unlock_irqrestore(&dev->event_lock, flags);
60 }
61 
62 bool amdgpu_dm_crtc_modeset_required(struct drm_crtc_state *crtc_state,
63 			     struct dc_stream_state *new_stream,
64 			     struct dc_stream_state *old_stream)
65 {
66 	return crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
67 }
68 
69 bool amdgpu_dm_crtc_vrr_active_irq(struct amdgpu_crtc *acrtc)
70 
71 {
72 	return acrtc->dm_irq_params.freesync_config.state ==
73 		       VRR_STATE_ACTIVE_VARIABLE ||
74 	       acrtc->dm_irq_params.freesync_config.state ==
75 		       VRR_STATE_ACTIVE_FIXED;
76 }
77 
78 int amdgpu_dm_crtc_set_vupdate_irq(struct drm_crtc *crtc, bool enable)
79 {
80 	enum dc_irq_source irq_source;
81 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
82 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
83 	int rc;
84 
85 	if (acrtc->otg_inst == -1)
86 		return 0;
87 
88 	irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst;
89 
90 	rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
91 
92 	DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n",
93 		      acrtc->crtc_id, enable ? "en" : "dis", rc);
94 	return rc;
95 }
96 
97 bool amdgpu_dm_crtc_vrr_active(const struct dm_crtc_state *dm_state)
98 {
99 	return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE ||
100 	       dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
101 }
102 
103 /**
104  * amdgpu_dm_crtc_set_static_screen_optimze() - Toggle static screen optimizations.
105  *
106  * @dm: display manager
107  * @stream: DC stream state
108  * @sso_enable: desired static screen optimization state
109  * @allow_sr_entry: whether entry into self-refresh mode is allowed
110  *
111  * This function uses the static-screen optimization state as the trigger to
112  * set/clear the Replay and PSR vsync-related events.
113  */
114 void amdgpu_dm_crtc_set_static_screen_optimze(
115 	struct amdgpu_display_manager *dm,
116 	struct dc_stream_state *stream,
117 	bool sso_enable, bool allow_sr_entry)
118 {
119 	struct dc_link *link = stream->link;
120 	bool set_vsync_event = !sso_enable;
121 
122 	if (!allow_sr_entry)
123 		return;
124 
125 	amdgpu_dm_replay_set_event(dm, stream,
126 		set_vsync_event, replay_event_vsync, set_vsync_event);
127 
128 	if (link->psr_settings.psr_version < DC_PSR_VERSION_SU_1)
129 		amdgpu_dm_psr_set_event(dm, stream,
130 			set_vsync_event, psr_event_vsync, set_vsync_event);
131 }
132 
133 bool amdgpu_dm_is_headless(struct amdgpu_device *adev)
134 {
135 	struct drm_connector *connector;
136 	struct drm_connector_list_iter iter;
137 	struct drm_device *dev;
138 	bool is_headless = true;
139 
140 	if (adev == NULL)
141 		return true;
142 
143 	dev = adev->dm.ddev;
144 
145 	drm_connector_list_iter_begin(dev, &iter);
146 	drm_for_each_connector_iter(connector, &iter) {
147 
148 		if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
149 			continue;
150 
151 		if (connector->status == connector_status_connected) {
152 			is_headless = false;
153 			break;
154 		}
155 	}
156 	drm_connector_list_iter_end(&iter);
157 	return is_headless;
158 }
159 
160 static void amdgpu_dm_idle_worker(struct work_struct *work)
161 {
162 	struct idle_workqueue *idle_work;
163 
164 	idle_work = container_of(work, struct idle_workqueue, work);
165 	idle_work->dm->idle_workqueue->running = true;
166 
167 	while (idle_work->enable) {
168 		fsleep(HPD_DETECTION_PERIOD_uS);
169 		mutex_lock(&idle_work->dm->dc_lock);
170 		if (!idle_work->dm->dc->idle_optimizations_allowed) {
171 			mutex_unlock(&idle_work->dm->dc_lock);
172 			break;
173 		}
174 		dc_allow_idle_optimizations(idle_work->dm->dc, false);
175 
176 		mutex_unlock(&idle_work->dm->dc_lock);
177 		fsleep(HPD_DETECTION_TIME_uS);
178 		mutex_lock(&idle_work->dm->dc_lock);
179 
180 		if (!amdgpu_dm_is_headless(idle_work->dm->adev) &&
181 		    !amdgpu_dm_psr_is_active_allowed(idle_work->dm)) {
182 			mutex_unlock(&idle_work->dm->dc_lock);
183 			break;
184 		}
185 
186 		if (idle_work->enable) {
187 			dc_post_update_surfaces_to_stream(idle_work->dm->dc);
188 			dc_allow_idle_optimizations(idle_work->dm->dc, true);
189 		}
190 		mutex_unlock(&idle_work->dm->dc_lock);
191 	}
192 	idle_work->dm->idle_workqueue->running = false;
193 }
194 
195 struct idle_workqueue *idle_create_workqueue(struct amdgpu_device *adev)
196 {
197 	struct idle_workqueue *idle_work;
198 
199 	idle_work = kzalloc_obj(*idle_work);
200 	if (ZERO_OR_NULL_PTR(idle_work))
201 		return NULL;
202 
203 	idle_work->dm = &adev->dm;
204 	idle_work->enable = false;
205 	idle_work->running = false;
206 	INIT_WORK(&idle_work->work, amdgpu_dm_idle_worker);
207 
208 	return idle_work;
209 }
210 
211 static void amdgpu_dm_crtc_vblank_control_worker(struct work_struct *work)
212 {
213 	struct vblank_control_work *vblank_work =
214 		container_of(work, struct vblank_control_work, work);
215 	struct amdgpu_display_manager *dm = vblank_work->dm;
216 
217 	mutex_lock(&dm->dc_lock);
218 
219 	if (vblank_work->enable) {
220 		dm->active_vblank_irq_count++;
221 		amdgpu_dm_ism_commit_event(&vblank_work->acrtc->ism,
222 				DM_ISM_EVENT_EXIT_IDLE_REQUESTED);
223 	} else {
224 		if (dm->active_vblank_irq_count > 0)
225 			dm->active_vblank_irq_count--;
226 		amdgpu_dm_ism_commit_event(&vblank_work->acrtc->ism,
227 				DM_ISM_EVENT_ENTER_IDLE_REQUESTED);
228 	}
229 
230 	mutex_unlock(&dm->dc_lock);
231 
232 	dc_stream_release(vblank_work->stream);
233 
234 	kfree(vblank_work);
235 }
236 
237 static inline int amdgpu_dm_crtc_set_vblank(struct drm_crtc *crtc, bool enable)
238 {
239 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
240 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
241 	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
242 	struct amdgpu_display_manager *dm = &adev->dm;
243 	struct vblank_control_work *work;
244 	int irq_type;
245 	int rc = 0;
246 
247 	if (enable && !acrtc->base.enabled) {
248 		drm_dbg_vbl(crtc->dev,
249 				"Reject vblank enable on unconfigured CRTC %d (enabled=%d)\n",
250 				acrtc->crtc_id, acrtc->base.enabled);
251 		return -EINVAL;
252 	}
253 
254 	irq_type = amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
255 
256 	if (enable) {
257 		struct dc *dc = adev->dm.dc;
258 		struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
259 		struct psr_settings *psr = &acrtc_state->stream->link->psr_settings;
260 		struct replay_settings *pr = &acrtc_state->stream->link->replay_settings;
261 		bool sr_supported = (psr->psr_version != DC_PSR_VERSION_UNSUPPORTED) ||
262 								pr->config.replay_supported;
263 
264 		/*
265 		 * IPS & self-refresh feature can cause vblank counter resets between
266 		 * vblank disable and enable.
267 		 * It may cause system stuck due to waiting for the vblank counter.
268 		 * Call this function to estimate missed vblanks by using timestamps and
269 		 * update the vblank counter in DRM.
270 		 */
271 		if (dc->caps.ips_support &&
272 			dc->config.disable_ips != DMUB_IPS_DISABLE_ALL &&
273 			sr_supported && vblank->config.disable_immediate)
274 			drm_crtc_vblank_restore(crtc);
275 	}
276 
277 	if (dc_supports_vrr(dm->dc->ctx->dce_version)) {
278 		if (enable) {
279 			/* vblank irq on -> Only need vupdate irq in vrr mode */
280 			if (amdgpu_dm_crtc_vrr_active(acrtc_state))
281 				rc = amdgpu_dm_crtc_set_vupdate_irq(crtc, true);
282 		} else {
283 			/* vblank irq off -> vupdate irq off */
284 			rc = amdgpu_dm_crtc_set_vupdate_irq(crtc, false);
285 		}
286 	}
287 
288 	if (rc)
289 		return rc;
290 
291 	/* crtc vblank or vstartup interrupt */
292 	if (enable) {
293 		rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
294 		drm_dbg_vbl(crtc->dev, "Get crtc_irq ret=%d\n", rc);
295 	} else {
296 		rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);
297 		drm_dbg_vbl(crtc->dev, "Put crtc_irq ret=%d\n", rc);
298 	}
299 
300 	if (rc)
301 		return rc;
302 
303 	/*
304 	 * hubp surface flip interrupt
305 	 *
306 	 * We have no guarantee that the frontend index maps to the same
307 	 * backend index - some even map to more than one.
308 	 *
309 	 * TODO: Use a different interrupt or check DC itself for the mapping.
310 	 */
311 	if (enable) {
312 		rc = amdgpu_irq_get(adev, &adev->pageflip_irq, irq_type);
313 		drm_dbg_vbl(crtc->dev, "Get pageflip_irq ret=%d\n", rc);
314 	} else {
315 		rc = amdgpu_irq_put(adev, &adev->pageflip_irq, irq_type);
316 		drm_dbg_vbl(crtc->dev, "Put pageflip_irq ret=%d\n", rc);
317 	}
318 
319 	if (rc)
320 		return rc;
321 
322 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
323 	/* crtc vline0 interrupt, only available on DCN+ */
324 	if (amdgpu_ip_version(adev, DCE_HWIP, 0) != 0) {
325 		if (enable) {
326 			rc = amdgpu_irq_get(adev, &adev->vline0_irq, irq_type);
327 			drm_dbg_vbl(crtc->dev, "Get vline0_irq ret=%d\n", rc);
328 		} else {
329 			rc = amdgpu_irq_put(adev, &adev->vline0_irq, irq_type);
330 			drm_dbg_vbl(crtc->dev, "Put vline0_irq ret=%d\n", rc);
331 		}
332 
333 		if (rc)
334 			return rc;
335 	}
336 #endif
337 
338 	if (amdgpu_in_reset(adev))
339 		return 0;
340 
341 	if (dm->vblank_control_workqueue) {
342 		work = kzalloc_obj(*work, GFP_ATOMIC);
343 		if (!work)
344 			return -ENOMEM;
345 
346 		INIT_WORK(&work->work, amdgpu_dm_crtc_vblank_control_worker);
347 		work->dm = dm;
348 		work->acrtc = acrtc;
349 		work->enable = enable;
350 
351 		if (acrtc_state->stream) {
352 			dc_stream_retain(acrtc_state->stream);
353 			work->stream = acrtc_state->stream;
354 		}
355 
356 		queue_work(dm->vblank_control_workqueue, &work->work);
357 	}
358 
359 	return 0;
360 }
361 
362 int amdgpu_dm_crtc_enable_vblank(struct drm_crtc *crtc)
363 {
364 	return amdgpu_dm_crtc_set_vblank(crtc, true);
365 }
366 
367 void amdgpu_dm_crtc_disable_vblank(struct drm_crtc *crtc)
368 {
369 	amdgpu_dm_crtc_set_vblank(crtc, false);
370 }
371 
372 static void amdgpu_dm_crtc_destroy_state(struct drm_crtc *crtc,
373 				  struct drm_crtc_state *state)
374 {
375 	struct dm_crtc_state *cur = to_dm_crtc_state(state);
376 
377 	/* TODO Destroy dc_stream objects are stream object is flattened */
378 	if (cur->stream)
379 		dc_stream_release(cur->stream);
380 
381 
382 	__drm_atomic_helper_crtc_destroy_state(state);
383 
384 
385 	kfree(state);
386 }
387 
388 static struct drm_crtc_state *amdgpu_dm_crtc_duplicate_state(struct drm_crtc *crtc)
389 {
390 	struct dm_crtc_state *state, *cur;
391 
392 	cur = to_dm_crtc_state(crtc->state);
393 
394 	if (WARN_ON(!crtc->state))
395 		return NULL;
396 
397 	state = kzalloc_obj(*state);
398 	if (!state)
399 		return NULL;
400 
401 	__drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
402 
403 	if (cur->stream) {
404 		state->stream = cur->stream;
405 		dc_stream_retain(state->stream);
406 	}
407 
408 	state->active_planes = cur->active_planes;
409 	state->vrr_infopacket = cur->vrr_infopacket;
410 	state->abm_level = cur->abm_level;
411 	state->vrr_supported = cur->vrr_supported;
412 	state->freesync_config = cur->freesync_config;
413 	state->cm_has_degamma = cur->cm_has_degamma;
414 	state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb;
415 	state->regamma_tf = cur->regamma_tf;
416 	state->crc_skip_count = cur->crc_skip_count;
417 	state->mpo_requested = cur->mpo_requested;
418 	state->cursor_mode = cur->cursor_mode;
419 	/* TODO Duplicate dc_stream after objects are stream object is flattened */
420 
421 	return &state->base;
422 }
423 
424 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
425 {
426 	/*
427 	 * amdgpu_dm_ism_fini() is intentionally called in amdgpu_dm_fini().
428 	 * It must be called before dc_destroy() in amdgpu_dm_fini()
429 	 * to avoid ISM accessing an invalid dc handle once dc is released.
430 	 */
431 
432 	drm_crtc_cleanup(crtc);
433 	kfree(crtc);
434 }
435 
436 static void amdgpu_dm_crtc_reset_state(struct drm_crtc *crtc)
437 {
438 	struct dm_crtc_state *state;
439 
440 	if (crtc->state)
441 		amdgpu_dm_crtc_destroy_state(crtc, crtc->state);
442 
443 	state = kzalloc_obj(*state);
444 	if (WARN_ON(!state))
445 		return;
446 
447 	__drm_atomic_helper_crtc_reset(crtc, &state->base);
448 }
449 
450 #ifdef CONFIG_DEBUG_FS
451 static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc)
452 {
453 	crtc_debugfs_init(crtc);
454 
455 	return 0;
456 }
457 #endif
458 
459 #ifdef AMD_PRIVATE_COLOR
460 /**
461  * dm_crtc_additional_color_mgmt - enable additional color properties
462  * @crtc: DRM CRTC
463  *
464  * This function lets the driver enable post-blending CRTC regamma transfer
465  * function property in addition to DRM CRTC gamma LUT. Default value means
466  * linear transfer function, which is the default CRTC gamma LUT behaviour
467  * without this property.
468  */
469 static void
470 dm_crtc_additional_color_mgmt(struct drm_crtc *crtc)
471 {
472 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
473 
474 	if (adev->dm.dc->caps.color.mpc.ogam_ram)
475 		drm_object_attach_property(&crtc->base,
476 					   adev->mode_info.regamma_tf_property,
477 					   AMDGPU_TRANSFER_FUNCTION_DEFAULT);
478 }
479 
480 static int
481 amdgpu_dm_atomic_crtc_set_property(struct drm_crtc *crtc,
482 				   struct drm_crtc_state *state,
483 				   struct drm_property *property,
484 				   uint64_t val)
485 {
486 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
487 	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(state);
488 
489 	if (property == adev->mode_info.regamma_tf_property) {
490 		if (acrtc_state->regamma_tf != val) {
491 			acrtc_state->regamma_tf = val;
492 			acrtc_state->base.color_mgmt_changed |= 1;
493 		}
494 	} else {
495 		drm_dbg_atomic(crtc->dev,
496 			       "[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
497 			       crtc->base.id, crtc->name,
498 			       property->base.id, property->name);
499 		return -EINVAL;
500 	}
501 
502 	return 0;
503 }
504 
505 static int
506 amdgpu_dm_atomic_crtc_get_property(struct drm_crtc *crtc,
507 				   const struct drm_crtc_state *state,
508 				   struct drm_property *property,
509 				   uint64_t *val)
510 {
511 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
512 	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(state);
513 
514 	if (property == adev->mode_info.regamma_tf_property)
515 		*val = acrtc_state->regamma_tf;
516 	else
517 		return -EINVAL;
518 
519 	return 0;
520 }
521 #endif
522 
523 /* Implemented only the options currently available for the driver */
524 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
525 	.reset = amdgpu_dm_crtc_reset_state,
526 	.destroy = amdgpu_dm_crtc_destroy,
527 	.set_config = drm_atomic_helper_set_config,
528 	.page_flip = drm_atomic_helper_page_flip,
529 	.atomic_duplicate_state = amdgpu_dm_crtc_duplicate_state,
530 	.atomic_destroy_state = amdgpu_dm_crtc_destroy_state,
531 	.set_crc_source = amdgpu_dm_crtc_set_crc_source,
532 	.verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
533 	.get_crc_sources = amdgpu_dm_crtc_get_crc_sources,
534 	.get_vblank_counter = amdgpu_get_vblank_counter_kms,
535 	.enable_vblank = amdgpu_dm_crtc_enable_vblank,
536 	.disable_vblank = amdgpu_dm_crtc_disable_vblank,
537 	.get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
538 #if defined(CONFIG_DEBUG_FS)
539 	.late_register = amdgpu_dm_crtc_late_register,
540 #endif
541 #ifdef AMD_PRIVATE_COLOR
542 	.atomic_set_property = amdgpu_dm_atomic_crtc_set_property,
543 	.atomic_get_property = amdgpu_dm_atomic_crtc_get_property,
544 #endif
545 };
546 
547 static void amdgpu_dm_crtc_helper_disable(struct drm_crtc *crtc)
548 {
549 }
550 
551 static int amdgpu_dm_crtc_count_crtc_active_planes(struct drm_crtc_state *new_crtc_state)
552 {
553 	struct drm_atomic_commit *state = new_crtc_state->state;
554 	struct drm_plane *plane;
555 	int num_active = 0;
556 
557 	drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) {
558 		struct drm_plane_state *new_plane_state;
559 
560 		/* Cursor planes are "fake". */
561 		if (plane->type == DRM_PLANE_TYPE_CURSOR)
562 			continue;
563 
564 		new_plane_state = drm_atomic_get_new_plane_state(state, plane);
565 
566 		if (!new_plane_state) {
567 			/*
568 			 * The plane is enable on the CRTC and hasn't changed
569 			 * state. This means that it previously passed
570 			 * validation and is therefore enabled.
571 			 */
572 			num_active += 1;
573 			continue;
574 		}
575 
576 		/* We need a framebuffer to be considered enabled. */
577 		num_active += (new_plane_state->fb != NULL);
578 	}
579 
580 	return num_active;
581 }
582 
583 static void amdgpu_dm_crtc_update_crtc_active_planes(struct drm_crtc *crtc,
584 						     struct drm_crtc_state *new_crtc_state)
585 {
586 	struct dm_crtc_state *dm_new_crtc_state =
587 		to_dm_crtc_state(new_crtc_state);
588 
589 	dm_new_crtc_state->active_planes = 0;
590 
591 	if (!dm_new_crtc_state->stream)
592 		return;
593 
594 	dm_new_crtc_state->active_planes =
595 		amdgpu_dm_crtc_count_crtc_active_planes(new_crtc_state);
596 }
597 
598 static bool amdgpu_dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
599 				      const struct drm_display_mode *mode,
600 				      struct drm_display_mode *adjusted_mode)
601 {
602 	return true;
603 }
604 
605 static int amdgpu_dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
606 					      struct drm_atomic_commit *state)
607 {
608 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
609 										crtc);
610 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
611 	struct dc *dc = adev->dm.dc;
612 	struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
613 	int ret = -EINVAL;
614 
615 	trace_amdgpu_dm_crtc_atomic_check(crtc_state);
616 
617 	amdgpu_dm_crtc_update_crtc_active_planes(crtc, crtc_state);
618 
619 	if (WARN_ON(unlikely(!dm_crtc_state->stream &&
620 			amdgpu_dm_crtc_modeset_required(crtc_state, NULL, dm_crtc_state->stream)))) {
621 		return ret;
622 	}
623 
624 	/*
625 	 * We require the primary plane to be enabled whenever the CRTC is, otherwise
626 	 * drm_mode_cursor_universal may end up trying to enable the cursor plane while all other
627 	 * planes are disabled, which is not supported by the hardware. And there is legacy
628 	 * userspace which stops using the HW cursor altogether in response to the resulting EINVAL.
629 	 */
630 	if (crtc_state->enable &&
631 		!(crtc_state->plane_mask & drm_plane_mask(crtc->primary))) {
632 		DRM_DEBUG_ATOMIC("Can't enable a CRTC without enabling the primary plane\n");
633 		return -EINVAL;
634 	}
635 
636 	/*
637 	 * Only allow async flips for fast updates that don't change the FB
638 	 * pitch, the DCC state, rotation, etc.
639 	 */
640 	if (crtc_state->async_flip &&
641 	    dm_crtc_state->update_type != UPDATE_TYPE_FAST) {
642 		drm_dbg_atomic(crtc->dev,
643 			       "[CRTC:%d:%s] async flips are only supported for fast updates\n",
644 			       crtc->base.id, crtc->name);
645 		return -EINVAL;
646 	}
647 
648 	if (!state->legacy_cursor_update && amdgpu_dm_crtc_vrr_active(dm_crtc_state)) {
649 		struct drm_plane_state *primary_state;
650 
651 		/* Pull in primary plane for correct VRR handling */
652 		primary_state = drm_atomic_get_plane_state(state, crtc->primary);
653 		if (IS_ERR(primary_state))
654 			return PTR_ERR(primary_state);
655 	}
656 
657 	/* In some use cases, like reset, no stream is attached */
658 	if (!dm_crtc_state->stream)
659 		return 0;
660 
661 	if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
662 		return 0;
663 
664 	DRM_DEBUG_ATOMIC("Failed DC stream validation\n");
665 	return ret;
666 }
667 
668 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
669 	.disable = amdgpu_dm_crtc_helper_disable,
670 	.atomic_check = amdgpu_dm_crtc_helper_atomic_check,
671 	.mode_fixup = amdgpu_dm_crtc_helper_mode_fixup,
672 	.get_scanout_position = amdgpu_crtc_get_scanout_position,
673 };
674 
675 /*
676  * This hysteresis filter as configured will:
677  *
678  * * Search through the latest 8[filter_history_size] entries in history,
679  *   skipping entries that are older than [filter_old_history_threshold] frames
680  *   (0 means ignore age)
681  * * Searches for short-idle-periods that lasted shorter than
682  *   4[filter_num_frames] frames-times
683  * * If there is at least 1[filter_entry_count] short-idle-period, then a delay
684  *   of 4[activation_num_delay_frames] will applied before allowing idle
685  *   optimizations again.
686  * * An additional delay of 11[sso_num_frames] is applied before enabling
687  *   panel-specific optimizations.
688  *
689  * The values were determined empirically on another OS, optimizing for Z8
690  * residency on APUs when running a productivity + web browsing test.
691  *
692  * TODO: Run similar tests to determine if these values are also optimal for
693  * Linux, and if each APU generation benefits differently.
694  */
695 static struct amdgpu_dm_ism_config default_ism_config = {
696 	.filter_num_frames = 4,
697 	.filter_history_size = 8,
698 	.filter_entry_count = 1,
699 	.activation_num_delay_frames = 4,
700 	.filter_old_history_threshold = 0,
701 	.sso_num_frames = 11,
702 };
703 
704 int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
705 			       struct drm_plane *plane,
706 			       uint32_t crtc_index)
707 {
708 	struct amdgpu_crtc *acrtc = NULL;
709 	struct drm_plane *cursor_plane;
710 	bool has_degamma;
711 	int res = -ENOMEM;
712 
713 	cursor_plane = kzalloc_obj(*cursor_plane);
714 	if (!cursor_plane)
715 		goto fail;
716 
717 	cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
718 	res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL);
719 
720 	acrtc = kzalloc_obj(struct amdgpu_crtc);
721 	if (!acrtc)
722 		goto fail;
723 
724 	res = drm_crtc_init_with_planes(
725 			dm->ddev,
726 			&acrtc->base,
727 			plane,
728 			cursor_plane,
729 			&amdgpu_dm_crtc_funcs, NULL);
730 
731 	if (res)
732 		goto fail;
733 
734 	amdgpu_dm_ism_init(&acrtc->ism, &default_ism_config);
735 
736 	drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
737 
738 	/* Create (reset) the plane state */
739 	if (acrtc->base.funcs->reset)
740 		acrtc->base.funcs->reset(&acrtc->base);
741 
742 	acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
743 	acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
744 
745 	acrtc->crtc_id = crtc_index;
746 	acrtc->base.enabled = false;
747 	acrtc->otg_inst = -1;
748 
749 	dm->adev->mode_info.crtcs[crtc_index] = acrtc;
750 
751 	/* Don't enable DRM CRTC degamma property for
752 	 * 1. DCE since it doesn't support programmable degamma anywhere.
753 	 * 2. DCN401 since pre-blending degamma LUT doesn't apply to cursor.
754 	 * Note: DEGAMMA properties are created even if the primary plane has the
755 	 * COLOR_PIPELINE property. User space can use either the DEGAMMA properties
756 	 * or the COLOR_PIPELINE property. An atomic commit which attempts to enable
757 	 * both is rejected.
758 	 */
759 	has_degamma = dm->adev->dm.dc->caps.color.dpp.dcn_arch &&
760 		      dm->adev->dm.dc->ctx->dce_version != DCN_VERSION_4_01;
761 
762 	drm_crtc_enable_color_mgmt(&acrtc->base, has_degamma ? MAX_COLOR_LUT_ENTRIES : 0,
763 				   true, MAX_COLOR_LUT_ENTRIES);
764 
765 	drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
766 
767 #ifdef AMD_PRIVATE_COLOR
768 	dm_crtc_additional_color_mgmt(&acrtc->base);
769 #endif
770 	return 0;
771 
772 fail:
773 	kfree(acrtc);
774 	kfree(cursor_plane);
775 	return res;
776 }
777 
778