1 // SPDX-License-Identifier: MIT
3 * Copyright 2022 Advanced Micro Devices, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
26 #include <drm/drm_vblank.h>
27 #include <drm/drm_atomic_helper.h>
31 #include "amdgpu_dm_psr.h"
32 #include "amdgpu_dm_crtc.h"
33 #include "amdgpu_dm_plane.h"
34 #include "amdgpu_dm_trace.h"
35 #include "amdgpu_dm_debugfs.h"
37 void amdgpu_dm_crtc_handle_vblank(struct amdgpu_crtc *acrtc)
39 struct drm_crtc *crtc = &acrtc->base;
40 struct drm_device *dev = crtc->dev;
43 drm_crtc_handle_vblank(crtc);
45 spin_lock_irqsave(&dev->event_lock, flags);
47 /* Send completion event for cursor-only commits */
48 if (acrtc->event && acrtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
49 drm_crtc_send_vblank_event(crtc, acrtc->event);
50 drm_crtc_vblank_put(crtc);
54 spin_unlock_irqrestore(&dev->event_lock, flags);
57 bool amdgpu_dm_crtc_modeset_required(struct drm_crtc_state *crtc_state,
58 struct dc_stream_state *new_stream,
59 struct dc_stream_state *old_stream)
61 return crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
64 bool amdgpu_dm_crtc_vrr_active_irq(struct amdgpu_crtc *acrtc)
67 return acrtc->dm_irq_params.freesync_config.state ==
68 VRR_STATE_ACTIVE_VARIABLE ||
69 acrtc->dm_irq_params.freesync_config.state ==
70 VRR_STATE_ACTIVE_FIXED;
73 int amdgpu_dm_crtc_set_vupdate_irq(struct drm_crtc *crtc, bool enable)
75 enum dc_irq_source irq_source;
76 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
77 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
80 if (acrtc->otg_inst == -1)
83 irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst;
85 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
87 DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n",
88 acrtc->crtc_id, enable ? "en" : "dis", rc);
92 bool amdgpu_dm_crtc_vrr_active(struct dm_crtc_state *dm_state)
94 return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE ||
95 dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
98 static void vblank_control_worker(struct work_struct *work)
100 struct vblank_control_work *vblank_work =
101 container_of(work, struct vblank_control_work, work);
102 struct amdgpu_display_manager *dm = vblank_work->dm;
104 mutex_lock(&dm->dc_lock);
106 if (vblank_work->enable)
107 dm->active_vblank_irq_count++;
108 else if (dm->active_vblank_irq_count)
109 dm->active_vblank_irq_count--;
111 dc_allow_idle_optimizations(dm->dc, dm->active_vblank_irq_count == 0);
113 DRM_DEBUG_KMS("Allow idle optimizations (MALL): %d\n", dm->active_vblank_irq_count == 0);
116 * Control PSR based on vblank requirements from OS
118 * If panel supports PSR SU, there's no need to disable PSR when OS is
119 * submitting fast atomic commits (we infer this by whether the OS
120 * requests vblank events). Fast atomic commits will simply trigger a
121 * full-frame-update (FFU); a specific case of selective-update (SU)
122 * where the SU region is the full hactive*vactive region. See
123 * fill_dc_dirty_rects().
125 if (vblank_work->stream && vblank_work->stream->link) {
126 if (vblank_work->enable) {
127 if (vblank_work->stream->link->psr_settings.psr_version < DC_PSR_VERSION_SU_1 &&
128 vblank_work->stream->link->psr_settings.psr_allow_active)
129 amdgpu_dm_psr_disable(vblank_work->stream);
130 } else if (vblank_work->stream->link->psr_settings.psr_feature_enabled &&
131 !vblank_work->stream->link->psr_settings.psr_allow_active &&
132 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
133 !amdgpu_dm_crc_window_is_activated(&vblank_work->acrtc->base) &&
135 vblank_work->acrtc->dm_irq_params.allow_psr_entry) {
136 amdgpu_dm_psr_enable(vblank_work->stream);
140 mutex_unlock(&dm->dc_lock);
142 dc_stream_release(vblank_work->stream);
147 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
149 enum dc_irq_source irq_source;
150 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
151 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
152 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
153 struct amdgpu_display_manager *dm = &adev->dm;
154 struct vblank_control_work *work;
157 if (acrtc->otg_inst == -1)
161 /* vblank irq on -> Only need vupdate irq in vrr mode */
162 if (amdgpu_dm_crtc_vrr_active(acrtc_state))
163 rc = amdgpu_dm_crtc_set_vupdate_irq(crtc, true);
165 /* vblank irq off -> vupdate irq off */
166 rc = amdgpu_dm_crtc_set_vupdate_irq(crtc, false);
172 if (amdgpu_in_reset(adev)) {
173 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
174 /* During gpu-reset we disable and then enable vblank irq, so
175 * don't use amdgpu_irq_get/put() to avoid refcount change.
177 if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
181 ? amdgpu_irq_get(adev, &adev->crtc_irq, acrtc->crtc_id)
182 : amdgpu_irq_put(adev, &adev->crtc_irq, acrtc->crtc_id);
189 if (amdgpu_in_reset(adev))
192 if (dm->vblank_control_workqueue) {
193 work = kzalloc(sizeof(*work), GFP_ATOMIC);
197 INIT_WORK(&work->work, vblank_control_worker);
200 work->enable = enable;
202 if (acrtc_state->stream) {
203 dc_stream_retain(acrtc_state->stream);
204 work->stream = acrtc_state->stream;
207 queue_work(dm->vblank_control_workqueue, &work->work);
213 int amdgpu_dm_crtc_enable_vblank(struct drm_crtc *crtc)
215 return dm_set_vblank(crtc, true);
218 void amdgpu_dm_crtc_disable_vblank(struct drm_crtc *crtc)
220 dm_set_vblank(crtc, false);
223 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
224 struct drm_crtc_state *state)
226 struct dm_crtc_state *cur = to_dm_crtc_state(state);
228 /* TODO Destroy dc_stream objects are stream object is flattened */
230 dc_stream_release(cur->stream);
233 __drm_atomic_helper_crtc_destroy_state(state);
239 static struct drm_crtc_state *dm_crtc_duplicate_state(struct drm_crtc *crtc)
241 struct dm_crtc_state *state, *cur;
243 cur = to_dm_crtc_state(crtc->state);
245 if (WARN_ON(!crtc->state))
248 state = kzalloc(sizeof(*state), GFP_KERNEL);
252 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
255 state->stream = cur->stream;
256 dc_stream_retain(state->stream);
259 state->active_planes = cur->active_planes;
260 state->vrr_infopacket = cur->vrr_infopacket;
261 state->abm_level = cur->abm_level;
262 state->vrr_supported = cur->vrr_supported;
263 state->freesync_config = cur->freesync_config;
264 state->cm_has_degamma = cur->cm_has_degamma;
265 state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb;
266 state->crc_skip_count = cur->crc_skip_count;
267 state->mpo_requested = cur->mpo_requested;
268 /* TODO Duplicate dc_stream after objects are stream object is flattened */
273 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
275 drm_crtc_cleanup(crtc);
279 static void dm_crtc_reset_state(struct drm_crtc *crtc)
281 struct dm_crtc_state *state;
284 dm_crtc_destroy_state(crtc, crtc->state);
286 state = kzalloc(sizeof(*state), GFP_KERNEL);
290 __drm_atomic_helper_crtc_reset(crtc, &state->base);
293 #ifdef CONFIG_DEBUG_FS
294 static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc)
296 crtc_debugfs_init(crtc);
302 /* Implemented only the options currently available for the driver */
303 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
304 .reset = dm_crtc_reset_state,
305 .destroy = amdgpu_dm_crtc_destroy,
306 .set_config = drm_atomic_helper_set_config,
307 .page_flip = drm_atomic_helper_page_flip,
308 .atomic_duplicate_state = dm_crtc_duplicate_state,
309 .atomic_destroy_state = dm_crtc_destroy_state,
310 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
311 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
312 .get_crc_sources = amdgpu_dm_crtc_get_crc_sources,
313 .get_vblank_counter = amdgpu_get_vblank_counter_kms,
314 .enable_vblank = amdgpu_dm_crtc_enable_vblank,
315 .disable_vblank = amdgpu_dm_crtc_disable_vblank,
316 .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
317 #if defined(CONFIG_DEBUG_FS)
318 .late_register = amdgpu_dm_crtc_late_register,
322 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
326 static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state)
328 struct drm_atomic_state *state = new_crtc_state->state;
329 struct drm_plane *plane;
332 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) {
333 struct drm_plane_state *new_plane_state;
335 /* Cursor planes are "fake". */
336 if (plane->type == DRM_PLANE_TYPE_CURSOR)
339 new_plane_state = drm_atomic_get_new_plane_state(state, plane);
341 if (!new_plane_state) {
343 * The plane is enable on the CRTC and hasn't changed
344 * state. This means that it previously passed
345 * validation and is therefore enabled.
351 /* We need a framebuffer to be considered enabled. */
352 num_active += (new_plane_state->fb != NULL);
358 static void dm_update_crtc_active_planes(struct drm_crtc *crtc,
359 struct drm_crtc_state *new_crtc_state)
361 struct dm_crtc_state *dm_new_crtc_state =
362 to_dm_crtc_state(new_crtc_state);
364 dm_new_crtc_state->active_planes = 0;
366 if (!dm_new_crtc_state->stream)
369 dm_new_crtc_state->active_planes =
370 count_crtc_active_planes(new_crtc_state);
373 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
374 const struct drm_display_mode *mode,
375 struct drm_display_mode *adjusted_mode)
380 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
381 struct drm_atomic_state *state)
383 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
385 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
386 struct dc *dc = adev->dm.dc;
387 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
390 trace_amdgpu_dm_crtc_atomic_check(crtc_state);
392 dm_update_crtc_active_planes(crtc, crtc_state);
394 if (WARN_ON(unlikely(!dm_crtc_state->stream &&
395 amdgpu_dm_crtc_modeset_required(crtc_state, NULL, dm_crtc_state->stream)))) {
400 * We require the primary plane to be enabled whenever the CRTC is, otherwise
401 * drm_mode_cursor_universal may end up trying to enable the cursor plane while all other
402 * planes are disabled, which is not supported by the hardware. And there is legacy
403 * userspace which stops using the HW cursor altogether in response to the resulting EINVAL.
405 if (crtc_state->enable &&
406 !(crtc_state->plane_mask & drm_plane_mask(crtc->primary))) {
407 DRM_DEBUG_ATOMIC("Can't enable a CRTC without enabling the primary plane\n");
411 /* In some use cases, like reset, no stream is attached */
412 if (!dm_crtc_state->stream)
415 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
418 DRM_DEBUG_ATOMIC("Failed DC stream validation\n");
422 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
423 .disable = dm_crtc_helper_disable,
424 .atomic_check = dm_crtc_helper_atomic_check,
425 .mode_fixup = dm_crtc_helper_mode_fixup,
426 .get_scanout_position = amdgpu_crtc_get_scanout_position,
429 int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
430 struct drm_plane *plane,
433 struct amdgpu_crtc *acrtc = NULL;
434 struct drm_plane *cursor_plane;
438 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
442 cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
443 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL);
445 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
449 res = drm_crtc_init_with_planes(
454 &amdgpu_dm_crtc_funcs, NULL);
459 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
461 /* Create (reset) the plane state */
462 if (acrtc->base.funcs->reset)
463 acrtc->base.funcs->reset(&acrtc->base);
465 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
466 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
468 acrtc->crtc_id = crtc_index;
469 acrtc->base.enabled = false;
470 acrtc->otg_inst = -1;
472 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
474 /* Don't enable DRM CRTC degamma property for DCE since it doesn't
475 * support programmable degamma anywhere.
477 is_dcn = dm->adev->dm.dc->caps.color.dpp.dcn_arch;
478 drm_crtc_enable_color_mgmt(&acrtc->base, is_dcn ? MAX_COLOR_LUT_ENTRIES : 0,
479 true, MAX_COLOR_LUT_ENTRIES);
481 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);