at commit afb1ecfe

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