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