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