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
amdgpu_dm_crtc_handle_vblank(struct amdgpu_crtc * acrtc)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
amdgpu_dm_crtc_modeset_required(struct drm_crtc_state * crtc_state,struct dc_stream_state * new_stream,struct dc_stream_state * old_stream)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
amdgpu_dm_crtc_vrr_active_irq(struct amdgpu_crtc * acrtc)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
amdgpu_dm_crtc_set_vupdate_irq(struct drm_crtc * crtc,bool enable)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
amdgpu_dm_crtc_vrr_active(const struct dm_crtc_state * dm_state)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 */
amdgpu_dm_crtc_set_panel_sr_feature(struct vblank_control_work * vblank_work,bool vblank_enabled,bool allow_sr_entry)127 static void amdgpu_dm_crtc_set_panel_sr_feature(
128 struct vblank_control_work *vblank_work,
129 bool vblank_enabled, bool allow_sr_entry)
130 {
131 struct dc_link *link = vblank_work->stream->link;
132 bool is_sr_active = (link->replay_settings.replay_allow_active ||
133 link->psr_settings.psr_allow_active);
134 bool is_crc_window_active = false;
135 bool vrr_active = amdgpu_dm_crtc_vrr_active_irq(vblank_work->acrtc);
136
137 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
138 is_crc_window_active =
139 amdgpu_dm_crc_window_is_activated(&vblank_work->acrtc->base);
140 #endif
141
142 if (link->replay_settings.replay_feature_enabled && !vrr_active &&
143 allow_sr_entry && !is_sr_active && !is_crc_window_active) {
144 amdgpu_dm_replay_enable(vblank_work->stream, true);
145 } else if (vblank_enabled) {
146 if (link->psr_settings.psr_version < DC_PSR_VERSION_SU_1 && is_sr_active)
147 amdgpu_dm_psr_disable(vblank_work->stream, false);
148 } else if (link->psr_settings.psr_feature_enabled && !vrr_active &&
149 allow_sr_entry && !is_sr_active && !is_crc_window_active) {
150
151 struct amdgpu_dm_connector *aconn =
152 (struct amdgpu_dm_connector *) vblank_work->stream->dm_stream_context;
153
154 if (!aconn->disallow_edp_enter_psr) {
155 struct amdgpu_display_manager *dm = vblank_work->dm;
156
157 amdgpu_dm_psr_enable(vblank_work->stream);
158 if (dm->idle_workqueue &&
159 dm->dc->idle_optimizations_allowed &&
160 dm->idle_workqueue->enable &&
161 !dm->idle_workqueue->running)
162 schedule_work(&dm->idle_workqueue->work);
163 }
164 }
165 }
166
amdgpu_dm_is_headless(struct amdgpu_device * adev)167 bool amdgpu_dm_is_headless(struct amdgpu_device *adev)
168 {
169 struct drm_connector *connector;
170 struct drm_connector_list_iter iter;
171 struct drm_device *dev;
172 bool is_headless = true;
173
174 if (adev == NULL)
175 return true;
176
177 dev = adev->dm.ddev;
178
179 drm_connector_list_iter_begin(dev, &iter);
180 drm_for_each_connector_iter(connector, &iter) {
181
182 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
183 continue;
184
185 if (connector->status == connector_status_connected) {
186 is_headless = false;
187 break;
188 }
189 }
190 drm_connector_list_iter_end(&iter);
191 return is_headless;
192 }
193
amdgpu_dm_idle_worker(struct work_struct * work)194 static void amdgpu_dm_idle_worker(struct work_struct *work)
195 {
196 struct idle_workqueue *idle_work;
197
198 idle_work = container_of(work, struct idle_workqueue, work);
199 idle_work->dm->idle_workqueue->running = true;
200
201 while (idle_work->enable) {
202 fsleep(HPD_DETECTION_PERIOD_uS);
203 mutex_lock(&idle_work->dm->dc_lock);
204 if (!idle_work->dm->dc->idle_optimizations_allowed) {
205 mutex_unlock(&idle_work->dm->dc_lock);
206 break;
207 }
208 dc_allow_idle_optimizations(idle_work->dm->dc, false);
209
210 mutex_unlock(&idle_work->dm->dc_lock);
211 fsleep(HPD_DETECTION_TIME_uS);
212 mutex_lock(&idle_work->dm->dc_lock);
213
214 if (!amdgpu_dm_is_headless(idle_work->dm->adev) &&
215 !amdgpu_dm_psr_is_active_allowed(idle_work->dm)) {
216 mutex_unlock(&idle_work->dm->dc_lock);
217 break;
218 }
219
220 if (idle_work->enable)
221 dc_allow_idle_optimizations(idle_work->dm->dc, true);
222 mutex_unlock(&idle_work->dm->dc_lock);
223 }
224 idle_work->dm->idle_workqueue->running = false;
225 }
226
idle_create_workqueue(struct amdgpu_device * adev)227 struct idle_workqueue *idle_create_workqueue(struct amdgpu_device *adev)
228 {
229 struct idle_workqueue *idle_work;
230
231 idle_work = kzalloc(sizeof(*idle_work), GFP_KERNEL);
232 if (ZERO_OR_NULL_PTR(idle_work))
233 return NULL;
234
235 idle_work->dm = &adev->dm;
236 idle_work->enable = false;
237 idle_work->running = false;
238 INIT_WORK(&idle_work->work, amdgpu_dm_idle_worker);
239
240 return idle_work;
241 }
242
amdgpu_dm_crtc_vblank_control_worker(struct work_struct * work)243 static void amdgpu_dm_crtc_vblank_control_worker(struct work_struct *work)
244 {
245 struct vblank_control_work *vblank_work =
246 container_of(work, struct vblank_control_work, work);
247 struct amdgpu_display_manager *dm = vblank_work->dm;
248
249 mutex_lock(&dm->dc_lock);
250
251 if (vblank_work->enable)
252 dm->active_vblank_irq_count++;
253 else if (dm->active_vblank_irq_count)
254 dm->active_vblank_irq_count--;
255
256 if (dm->active_vblank_irq_count > 0) {
257 DRM_DEBUG_KMS("Allow idle optimizations (MALL): false\n");
258 dc_allow_idle_optimizations(dm->dc, false);
259 }
260
261 /*
262 * Control PSR based on vblank requirements from OS
263 *
264 * If panel supports PSR SU, there's no need to disable PSR when OS is
265 * submitting fast atomic commits (we infer this by whether the OS
266 * requests vblank events). Fast atomic commits will simply trigger a
267 * full-frame-update (FFU); a specific case of selective-update (SU)
268 * where the SU region is the full hactive*vactive region. See
269 * fill_dc_dirty_rects().
270 */
271 if (vblank_work->stream && vblank_work->stream->link && vblank_work->acrtc) {
272 amdgpu_dm_crtc_set_panel_sr_feature(
273 vblank_work, vblank_work->enable,
274 vblank_work->acrtc->dm_irq_params.allow_sr_entry);
275 }
276
277 if (dm->active_vblank_irq_count == 0) {
278 DRM_DEBUG_KMS("Allow idle optimizations (MALL): true\n");
279 dc_allow_idle_optimizations(dm->dc, true);
280 }
281
282 mutex_unlock(&dm->dc_lock);
283
284 dc_stream_release(vblank_work->stream);
285
286 kfree(vblank_work);
287 }
288
amdgpu_dm_crtc_set_vblank(struct drm_crtc * crtc,bool enable)289 static inline int amdgpu_dm_crtc_set_vblank(struct drm_crtc *crtc, bool enable)
290 {
291 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
292 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
293 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
294 struct amdgpu_display_manager *dm = &adev->dm;
295 struct vblank_control_work *work;
296 int irq_type;
297 int rc = 0;
298
299 if (acrtc->otg_inst == -1)
300 goto skip;
301
302 irq_type = amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
303
304 if (enable) {
305 struct dc *dc = adev->dm.dc;
306 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
307 struct psr_settings *psr = &acrtc_state->stream->link->psr_settings;
308 struct replay_settings *pr = &acrtc_state->stream->link->replay_settings;
309 bool sr_supported = (psr->psr_version != DC_PSR_VERSION_UNSUPPORTED) ||
310 pr->config.replay_supported;
311
312 /*
313 * IPS & self-refresh feature can cause vblank counter resets between
314 * vblank disable and enable.
315 * It may cause system stuck due to waiting for the vblank counter.
316 * Call this function to estimate missed vblanks by using timestamps and
317 * update the vblank counter in DRM.
318 */
319 if (dc->caps.ips_support &&
320 dc->config.disable_ips != DMUB_IPS_DISABLE_ALL &&
321 sr_supported && vblank->config.disable_immediate)
322 drm_crtc_vblank_restore(crtc);
323
324 /* vblank irq on -> Only need vupdate irq in vrr mode */
325 if (amdgpu_dm_crtc_vrr_active(acrtc_state))
326 rc = amdgpu_dm_crtc_set_vupdate_irq(crtc, true);
327 } else {
328 /* vblank irq off -> vupdate irq off */
329 rc = amdgpu_dm_crtc_set_vupdate_irq(crtc, false);
330 }
331
332 if (rc)
333 return rc;
334
335 /* crtc vblank or vstartup interrupt */
336 if (enable) {
337 rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type);
338 drm_dbg_vbl(crtc->dev, "Get crtc_irq ret=%d\n", rc);
339 } else {
340 rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type);
341 drm_dbg_vbl(crtc->dev, "Put crtc_irq ret=%d\n", rc);
342 }
343
344 if (rc)
345 return rc;
346
347 /*
348 * hubp surface flip interrupt
349 *
350 * We have no guarantee that the frontend index maps to the same
351 * backend index - some even map to more than one.
352 *
353 * TODO: Use a different interrupt or check DC itself for the mapping.
354 */
355 if (enable) {
356 rc = amdgpu_irq_get(adev, &adev->pageflip_irq, irq_type);
357 drm_dbg_vbl(crtc->dev, "Get pageflip_irq ret=%d\n", rc);
358 } else {
359 rc = amdgpu_irq_put(adev, &adev->pageflip_irq, irq_type);
360 drm_dbg_vbl(crtc->dev, "Put pageflip_irq ret=%d\n", rc);
361 }
362
363 if (rc)
364 return rc;
365
366 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
367 /* crtc vline0 interrupt, only available on DCN+ */
368 if (amdgpu_ip_version(adev, DCE_HWIP, 0) != 0) {
369 if (enable) {
370 rc = amdgpu_irq_get(adev, &adev->vline0_irq, irq_type);
371 drm_dbg_vbl(crtc->dev, "Get vline0_irq ret=%d\n", rc);
372 } else {
373 rc = amdgpu_irq_put(adev, &adev->vline0_irq, irq_type);
374 drm_dbg_vbl(crtc->dev, "Put vline0_irq ret=%d\n", rc);
375 }
376
377 if (rc)
378 return rc;
379 }
380 #endif
381 skip:
382 if (amdgpu_in_reset(adev))
383 return 0;
384
385 if (dm->vblank_control_workqueue) {
386 work = kzalloc(sizeof(*work), GFP_ATOMIC);
387 if (!work)
388 return -ENOMEM;
389
390 INIT_WORK(&work->work, amdgpu_dm_crtc_vblank_control_worker);
391 work->dm = dm;
392 work->acrtc = acrtc;
393 work->enable = enable;
394
395 if (acrtc_state->stream) {
396 dc_stream_retain(acrtc_state->stream);
397 work->stream = acrtc_state->stream;
398 }
399
400 queue_work(dm->vblank_control_workqueue, &work->work);
401 }
402
403 return 0;
404 }
405
amdgpu_dm_crtc_enable_vblank(struct drm_crtc * crtc)406 int amdgpu_dm_crtc_enable_vblank(struct drm_crtc *crtc)
407 {
408 return amdgpu_dm_crtc_set_vblank(crtc, true);
409 }
410
amdgpu_dm_crtc_disable_vblank(struct drm_crtc * crtc)411 void amdgpu_dm_crtc_disable_vblank(struct drm_crtc *crtc)
412 {
413 amdgpu_dm_crtc_set_vblank(crtc, false);
414 }
415
amdgpu_dm_crtc_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)416 static void amdgpu_dm_crtc_destroy_state(struct drm_crtc *crtc,
417 struct drm_crtc_state *state)
418 {
419 struct dm_crtc_state *cur = to_dm_crtc_state(state);
420
421 /* TODO Destroy dc_stream objects are stream object is flattened */
422 if (cur->stream)
423 dc_stream_release(cur->stream);
424
425
426 __drm_atomic_helper_crtc_destroy_state(state);
427
428
429 kfree(state);
430 }
431
amdgpu_dm_crtc_duplicate_state(struct drm_crtc * crtc)432 static struct drm_crtc_state *amdgpu_dm_crtc_duplicate_state(struct drm_crtc *crtc)
433 {
434 struct dm_crtc_state *state, *cur;
435
436 cur = to_dm_crtc_state(crtc->state);
437
438 if (WARN_ON(!crtc->state))
439 return NULL;
440
441 state = kzalloc(sizeof(*state), GFP_KERNEL);
442 if (!state)
443 return NULL;
444
445 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
446
447 if (cur->stream) {
448 state->stream = cur->stream;
449 dc_stream_retain(state->stream);
450 }
451
452 state->active_planes = cur->active_planes;
453 state->vrr_infopacket = cur->vrr_infopacket;
454 state->abm_level = cur->abm_level;
455 state->vrr_supported = cur->vrr_supported;
456 state->freesync_config = cur->freesync_config;
457 state->cm_has_degamma = cur->cm_has_degamma;
458 state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb;
459 state->regamma_tf = cur->regamma_tf;
460 state->crc_skip_count = cur->crc_skip_count;
461 state->mpo_requested = cur->mpo_requested;
462 state->cursor_mode = cur->cursor_mode;
463 /* TODO Duplicate dc_stream after objects are stream object is flattened */
464
465 return &state->base;
466 }
467
amdgpu_dm_crtc_destroy(struct drm_crtc * crtc)468 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
469 {
470 drm_crtc_cleanup(crtc);
471 kfree(crtc);
472 }
473
amdgpu_dm_crtc_reset_state(struct drm_crtc * crtc)474 static void amdgpu_dm_crtc_reset_state(struct drm_crtc *crtc)
475 {
476 struct dm_crtc_state *state;
477
478 if (crtc->state)
479 amdgpu_dm_crtc_destroy_state(crtc, crtc->state);
480
481 state = kzalloc(sizeof(*state), GFP_KERNEL);
482 if (WARN_ON(!state))
483 return;
484
485 __drm_atomic_helper_crtc_reset(crtc, &state->base);
486 }
487
488 #ifdef CONFIG_DEBUG_FS
amdgpu_dm_crtc_late_register(struct drm_crtc * crtc)489 static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc)
490 {
491 crtc_debugfs_init(crtc);
492
493 return 0;
494 }
495 #endif
496
497 #ifdef AMD_PRIVATE_COLOR
498 /**
499 * dm_crtc_additional_color_mgmt - enable additional color properties
500 * @crtc: DRM CRTC
501 *
502 * This function lets the driver enable post-blending CRTC regamma transfer
503 * function property in addition to DRM CRTC gamma LUT. Default value means
504 * linear transfer function, which is the default CRTC gamma LUT behaviour
505 * without this property.
506 */
507 static void
dm_crtc_additional_color_mgmt(struct drm_crtc * crtc)508 dm_crtc_additional_color_mgmt(struct drm_crtc *crtc)
509 {
510 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
511
512 if (adev->dm.dc->caps.color.mpc.ogam_ram)
513 drm_object_attach_property(&crtc->base,
514 adev->mode_info.regamma_tf_property,
515 AMDGPU_TRANSFER_FUNCTION_DEFAULT);
516 }
517
518 static int
amdgpu_dm_atomic_crtc_set_property(struct drm_crtc * crtc,struct drm_crtc_state * state,struct drm_property * property,uint64_t val)519 amdgpu_dm_atomic_crtc_set_property(struct drm_crtc *crtc,
520 struct drm_crtc_state *state,
521 struct drm_property *property,
522 uint64_t val)
523 {
524 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
525 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(state);
526
527 if (property == adev->mode_info.regamma_tf_property) {
528 if (acrtc_state->regamma_tf != val) {
529 acrtc_state->regamma_tf = val;
530 acrtc_state->base.color_mgmt_changed |= 1;
531 }
532 } else {
533 drm_dbg_atomic(crtc->dev,
534 "[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
535 crtc->base.id, crtc->name,
536 property->base.id, property->name);
537 return -EINVAL;
538 }
539
540 return 0;
541 }
542
543 static int
amdgpu_dm_atomic_crtc_get_property(struct drm_crtc * crtc,const struct drm_crtc_state * state,struct drm_property * property,uint64_t * val)544 amdgpu_dm_atomic_crtc_get_property(struct drm_crtc *crtc,
545 const struct drm_crtc_state *state,
546 struct drm_property *property,
547 uint64_t *val)
548 {
549 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
550 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(state);
551
552 if (property == adev->mode_info.regamma_tf_property)
553 *val = acrtc_state->regamma_tf;
554 else
555 return -EINVAL;
556
557 return 0;
558 }
559 #endif
560
561 /* Implemented only the options currently available for the driver */
562 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
563 .reset = amdgpu_dm_crtc_reset_state,
564 .destroy = amdgpu_dm_crtc_destroy,
565 .set_config = drm_atomic_helper_set_config,
566 .page_flip = drm_atomic_helper_page_flip,
567 .atomic_duplicate_state = amdgpu_dm_crtc_duplicate_state,
568 .atomic_destroy_state = amdgpu_dm_crtc_destroy_state,
569 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
570 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
571 .get_crc_sources = amdgpu_dm_crtc_get_crc_sources,
572 .get_vblank_counter = amdgpu_get_vblank_counter_kms,
573 .enable_vblank = amdgpu_dm_crtc_enable_vblank,
574 .disable_vblank = amdgpu_dm_crtc_disable_vblank,
575 .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
576 #if defined(CONFIG_DEBUG_FS)
577 .late_register = amdgpu_dm_crtc_late_register,
578 #endif
579 #ifdef AMD_PRIVATE_COLOR
580 .atomic_set_property = amdgpu_dm_atomic_crtc_set_property,
581 .atomic_get_property = amdgpu_dm_atomic_crtc_get_property,
582 #endif
583 };
584
amdgpu_dm_crtc_helper_disable(struct drm_crtc * crtc)585 static void amdgpu_dm_crtc_helper_disable(struct drm_crtc *crtc)
586 {
587 }
588
amdgpu_dm_crtc_count_crtc_active_planes(struct drm_crtc_state * new_crtc_state)589 static int amdgpu_dm_crtc_count_crtc_active_planes(struct drm_crtc_state *new_crtc_state)
590 {
591 struct drm_atomic_state *state = new_crtc_state->state;
592 struct drm_plane *plane;
593 int num_active = 0;
594
595 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) {
596 struct drm_plane_state *new_plane_state;
597
598 /* Cursor planes are "fake". */
599 if (plane->type == DRM_PLANE_TYPE_CURSOR)
600 continue;
601
602 new_plane_state = drm_atomic_get_new_plane_state(state, plane);
603
604 if (!new_plane_state) {
605 /*
606 * The plane is enable on the CRTC and hasn't changed
607 * state. This means that it previously passed
608 * validation and is therefore enabled.
609 */
610 num_active += 1;
611 continue;
612 }
613
614 /* We need a framebuffer to be considered enabled. */
615 num_active += (new_plane_state->fb != NULL);
616 }
617
618 return num_active;
619 }
620
amdgpu_dm_crtc_update_crtc_active_planes(struct drm_crtc * crtc,struct drm_crtc_state * new_crtc_state)621 static void amdgpu_dm_crtc_update_crtc_active_planes(struct drm_crtc *crtc,
622 struct drm_crtc_state *new_crtc_state)
623 {
624 struct dm_crtc_state *dm_new_crtc_state =
625 to_dm_crtc_state(new_crtc_state);
626
627 dm_new_crtc_state->active_planes = 0;
628
629 if (!dm_new_crtc_state->stream)
630 return;
631
632 dm_new_crtc_state->active_planes =
633 amdgpu_dm_crtc_count_crtc_active_planes(new_crtc_state);
634 }
635
amdgpu_dm_crtc_helper_mode_fixup(struct drm_crtc * crtc,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)636 static bool amdgpu_dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
637 const struct drm_display_mode *mode,
638 struct drm_display_mode *adjusted_mode)
639 {
640 return true;
641 }
642
amdgpu_dm_crtc_helper_atomic_check(struct drm_crtc * crtc,struct drm_atomic_state * state)643 static int amdgpu_dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
644 struct drm_atomic_state *state)
645 {
646 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
647 crtc);
648 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
649 struct dc *dc = adev->dm.dc;
650 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
651 int ret = -EINVAL;
652
653 trace_amdgpu_dm_crtc_atomic_check(crtc_state);
654
655 amdgpu_dm_crtc_update_crtc_active_planes(crtc, crtc_state);
656
657 if (WARN_ON(unlikely(!dm_crtc_state->stream &&
658 amdgpu_dm_crtc_modeset_required(crtc_state, NULL, dm_crtc_state->stream)))) {
659 return ret;
660 }
661
662 /*
663 * We require the primary plane to be enabled whenever the CRTC is, otherwise
664 * drm_mode_cursor_universal may end up trying to enable the cursor plane while all other
665 * planes are disabled, which is not supported by the hardware. And there is legacy
666 * userspace which stops using the HW cursor altogether in response to the resulting EINVAL.
667 */
668 if (crtc_state->enable &&
669 !(crtc_state->plane_mask & drm_plane_mask(crtc->primary))) {
670 DRM_DEBUG_ATOMIC("Can't enable a CRTC without enabling the primary plane\n");
671 return -EINVAL;
672 }
673
674 /*
675 * Only allow async flips for fast updates that don't change the FB
676 * pitch, the DCC state, rotation, etc.
677 */
678 if (crtc_state->async_flip &&
679 dm_crtc_state->update_type != UPDATE_TYPE_FAST) {
680 drm_dbg_atomic(crtc->dev,
681 "[CRTC:%d:%s] async flips are only supported for fast updates\n",
682 crtc->base.id, crtc->name);
683 return -EINVAL;
684 }
685
686 if (!state->legacy_cursor_update && amdgpu_dm_crtc_vrr_active(dm_crtc_state)) {
687 struct drm_plane_state *primary_state;
688
689 /* Pull in primary plane for correct VRR handling */
690 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
691 if (IS_ERR(primary_state))
692 return PTR_ERR(primary_state);
693 }
694
695 /* In some use cases, like reset, no stream is attached */
696 if (!dm_crtc_state->stream)
697 return 0;
698
699 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
700 return 0;
701
702 DRM_DEBUG_ATOMIC("Failed DC stream validation\n");
703 return ret;
704 }
705
706 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
707 .disable = amdgpu_dm_crtc_helper_disable,
708 .atomic_check = amdgpu_dm_crtc_helper_atomic_check,
709 .mode_fixup = amdgpu_dm_crtc_helper_mode_fixup,
710 .get_scanout_position = amdgpu_crtc_get_scanout_position,
711 };
712
amdgpu_dm_crtc_init(struct amdgpu_display_manager * dm,struct drm_plane * plane,uint32_t crtc_index)713 int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
714 struct drm_plane *plane,
715 uint32_t crtc_index)
716 {
717 struct amdgpu_crtc *acrtc = NULL;
718 struct drm_plane *cursor_plane;
719 bool is_dcn;
720 int res = -ENOMEM;
721
722 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
723 if (!cursor_plane)
724 goto fail;
725
726 cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
727 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL);
728
729 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
730 if (!acrtc)
731 goto fail;
732
733 res = drm_crtc_init_with_planes(
734 dm->ddev,
735 &acrtc->base,
736 plane,
737 cursor_plane,
738 &amdgpu_dm_crtc_funcs, NULL);
739
740 if (res)
741 goto fail;
742
743 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
744
745 /* Create (reset) the plane state */
746 if (acrtc->base.funcs->reset)
747 acrtc->base.funcs->reset(&acrtc->base);
748
749 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
750 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
751
752 acrtc->crtc_id = crtc_index;
753 acrtc->base.enabled = false;
754 acrtc->otg_inst = -1;
755
756 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
757
758 /* Don't enable DRM CRTC degamma property for DCE since it doesn't
759 * support programmable degamma anywhere.
760 */
761 is_dcn = dm->adev->dm.dc->caps.color.dpp.dcn_arch;
762 /* Dont't enable DRM CRTC degamma property for DCN401 since the
763 * pre-blending degamma LUT doesn't apply to cursor, and therefore
764 * can't work similar to a post-blending degamma LUT as in other hw
765 * versions.
766 * TODO: revisit it once KMS plane color API is merged.
767 */
768 drm_crtc_enable_color_mgmt(&acrtc->base,
769 (is_dcn &&
770 dm->adev->dm.dc->ctx->dce_version != DCN_VERSION_4_01) ?
771 MAX_COLOR_LUT_ENTRIES : 0,
772 true, MAX_COLOR_LUT_ENTRIES);
773
774 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
775
776 #ifdef AMD_PRIVATE_COLOR
777 dm_crtc_additional_color_mgmt(&acrtc->base);
778 #endif
779 return 0;
780
781 fail:
782 kfree(acrtc);
783 kfree(cursor_plane);
784 return res;
785 }
786
787