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