1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2cf3a7e4cSRob Clark /*
3cf3a7e4cSRob Clark * Copyright (C) 2014 Red Hat
4cf3a7e4cSRob Clark * Author: Rob Clark <robdclark@gmail.com>
5cf3a7e4cSRob Clark */
6cf3a7e4cSRob Clark
772fdb40cSDaniel Vetter #include <drm/drm_atomic_uapi.h>
8feea39a8SSam Ravnborg #include <drm/drm_vblank.h>
972fdb40cSDaniel Vetter
10d934a712SRob Clark #include "msm_atomic_trace.h"
11cf3a7e4cSRob Clark #include "msm_drv.h"
12db8f4d5dSSean Paul #include "msm_gem.h"
13cf3a7e4cSRob Clark #include "msm_kms.h"
14cf3a7e4cSRob Clark
1543906812SRob Clark /*
1643906812SRob Clark * Helpers to control vblanks while we flush.. basically just to ensure
1743906812SRob Clark * that vblank accounting is switched on, so we get valid seqn/timestamp
1843906812SRob Clark * on pageflip events (if requested)
1943906812SRob Clark */
2043906812SRob Clark
vblank_get(struct msm_kms * kms,unsigned crtc_mask)2143906812SRob Clark static void vblank_get(struct msm_kms *kms, unsigned crtc_mask)
2243906812SRob Clark {
2343906812SRob Clark struct drm_crtc *crtc;
2443906812SRob Clark
2543906812SRob Clark for_each_crtc_mask(kms->dev, crtc, crtc_mask) {
2643906812SRob Clark if (!crtc->state->active)
2743906812SRob Clark continue;
2843906812SRob Clark drm_crtc_vblank_get(crtc);
2943906812SRob Clark }
3043906812SRob Clark }
3143906812SRob Clark
vblank_put(struct msm_kms * kms,unsigned crtc_mask)3243906812SRob Clark static void vblank_put(struct msm_kms *kms, unsigned crtc_mask)
3343906812SRob Clark {
3443906812SRob Clark struct drm_crtc *crtc;
3543906812SRob Clark
3643906812SRob Clark for_each_crtc_mask(kms->dev, crtc, crtc_mask) {
3743906812SRob Clark if (!crtc->state->active)
3843906812SRob Clark continue;
3943906812SRob Clark drm_crtc_vblank_put(crtc);
4043906812SRob Clark }
4143906812SRob Clark }
4243906812SRob Clark
lock_crtcs(struct msm_kms * kms,unsigned int crtc_mask)43b3d91800SKrishna Manikandan static void lock_crtcs(struct msm_kms *kms, unsigned int crtc_mask)
44b3d91800SKrishna Manikandan {
45743c97caSStephen Boyd int crtc_index;
46b3d91800SKrishna Manikandan struct drm_crtc *crtc;
47b3d91800SKrishna Manikandan
48743c97caSStephen Boyd for_each_crtc_mask(kms->dev, crtc, crtc_mask) {
49743c97caSStephen Boyd crtc_index = drm_crtc_index(crtc);
50743c97caSStephen Boyd mutex_lock_nested(&kms->commit_lock[crtc_index], crtc_index);
51743c97caSStephen Boyd }
52b3d91800SKrishna Manikandan }
53b3d91800SKrishna Manikandan
unlock_crtcs(struct msm_kms * kms,unsigned int crtc_mask)54b3d91800SKrishna Manikandan static void unlock_crtcs(struct msm_kms *kms, unsigned int crtc_mask)
55b3d91800SKrishna Manikandan {
56b3d91800SKrishna Manikandan struct drm_crtc *crtc;
57b3d91800SKrishna Manikandan
58cb21f3f8SRob Clark for_each_crtc_mask_reverse(kms->dev, crtc, crtc_mask)
59b3d91800SKrishna Manikandan mutex_unlock(&kms->commit_lock[drm_crtc_index(crtc)]);
60b3d91800SKrishna Manikandan }
61b3d91800SKrishna Manikandan
msm_atomic_async_commit(struct msm_kms * kms,int crtc_idx)622d99ced7SRob Clark static void msm_atomic_async_commit(struct msm_kms *kms, int crtc_idx)
632d99ced7SRob Clark {
642d99ced7SRob Clark unsigned crtc_mask = BIT(crtc_idx);
652d99ced7SRob Clark
66d934a712SRob Clark trace_msm_atomic_async_commit_start(crtc_mask);
67d934a712SRob Clark
68b3d91800SKrishna Manikandan lock_crtcs(kms, crtc_mask);
692d99ced7SRob Clark
702d99ced7SRob Clark if (!(kms->pending_crtc_mask & crtc_mask)) {
71b3d91800SKrishna Manikandan unlock_crtcs(kms, crtc_mask);
72d934a712SRob Clark goto out;
732d99ced7SRob Clark }
742d99ced7SRob Clark
752d99ced7SRob Clark kms->pending_crtc_mask &= ~crtc_mask;
762d99ced7SRob Clark
772d99ced7SRob Clark kms->funcs->enable_commit(kms);
782d99ced7SRob Clark
7943906812SRob Clark vblank_get(kms, crtc_mask);
8043906812SRob Clark
812d99ced7SRob Clark /*
822d99ced7SRob Clark * Flush hardware updates:
832d99ced7SRob Clark */
84d934a712SRob Clark trace_msm_atomic_flush_commit(crtc_mask);
852d99ced7SRob Clark kms->funcs->flush_commit(kms, crtc_mask);
862d99ced7SRob Clark
872d99ced7SRob Clark /*
882d99ced7SRob Clark * Wait for flush to complete:
892d99ced7SRob Clark */
90d934a712SRob Clark trace_msm_atomic_wait_flush_start(crtc_mask);
912d99ced7SRob Clark kms->funcs->wait_flush(kms, crtc_mask);
92d934a712SRob Clark trace_msm_atomic_wait_flush_finish(crtc_mask);
932d99ced7SRob Clark
9443906812SRob Clark vblank_put(kms, crtc_mask);
9543906812SRob Clark
962d99ced7SRob Clark kms->funcs->complete_commit(kms, crtc_mask);
97b3d91800SKrishna Manikandan unlock_crtcs(kms, crtc_mask);
982d99ced7SRob Clark kms->funcs->disable_commit(kms);
99d934a712SRob Clark
100d934a712SRob Clark out:
101d934a712SRob Clark trace_msm_atomic_async_commit_finish(crtc_mask);
1022d99ced7SRob Clark }
1032d99ced7SRob Clark
msm_atomic_pending_work(struct kthread_work * work)104363bcec9SRob Clark static void msm_atomic_pending_work(struct kthread_work *work)
1052d99ced7SRob Clark {
1062d99ced7SRob Clark struct msm_pending_timer *timer = container_of(work,
107ddb6e37aSRob Clark struct msm_pending_timer, work.work);
1082d99ced7SRob Clark
1092d99ced7SRob Clark msm_atomic_async_commit(timer->kms, timer->crtc_idx);
1102d99ced7SRob Clark }
1112d99ced7SRob Clark
msm_atomic_init_pending_timer(struct msm_pending_timer * timer,struct msm_kms * kms,int crtc_idx)112363bcec9SRob Clark int msm_atomic_init_pending_timer(struct msm_pending_timer *timer,
1132d99ced7SRob Clark struct msm_kms *kms, int crtc_idx)
1142d99ced7SRob Clark {
1152d99ced7SRob Clark timer->kms = kms;
1162d99ced7SRob Clark timer->crtc_idx = crtc_idx;
117363bcec9SRob Clark
118363bcec9SRob Clark timer->worker = kthread_create_worker(0, "atomic-worker-%d", crtc_idx);
119363bcec9SRob Clark if (IS_ERR(timer->worker)) {
120363bcec9SRob Clark int ret = PTR_ERR(timer->worker);
121363bcec9SRob Clark timer->worker = NULL;
122363bcec9SRob Clark return ret;
123363bcec9SRob Clark }
124363bcec9SRob Clark sched_set_fifo(timer->worker->task);
125ddb6e37aSRob Clark
126ddb6e37aSRob Clark msm_hrtimer_work_init(&timer->work, timer->worker,
127ddb6e37aSRob Clark msm_atomic_pending_work,
128ddb6e37aSRob Clark CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
129363bcec9SRob Clark
130363bcec9SRob Clark return 0;
131363bcec9SRob Clark }
132363bcec9SRob Clark
msm_atomic_destroy_pending_timer(struct msm_pending_timer * timer)133363bcec9SRob Clark void msm_atomic_destroy_pending_timer(struct msm_pending_timer *timer)
134363bcec9SRob Clark {
135363bcec9SRob Clark if (timer->worker)
136363bcec9SRob Clark kthread_destroy_worker(timer->worker);
1372d99ced7SRob Clark }
1382d99ced7SRob Clark
can_do_async(struct drm_atomic_state * state,struct drm_crtc ** async_crtc)1392d99ced7SRob Clark static bool can_do_async(struct drm_atomic_state *state,
1402d99ced7SRob Clark struct drm_crtc **async_crtc)
1412d99ced7SRob Clark {
1422d99ced7SRob Clark struct drm_connector_state *connector_state;
1432d99ced7SRob Clark struct drm_connector *connector;
1442d99ced7SRob Clark struct drm_crtc_state *crtc_state;
1452d99ced7SRob Clark struct drm_crtc *crtc;
1462d99ced7SRob Clark int i, num_crtcs = 0;
1472d99ced7SRob Clark
1482d99ced7SRob Clark if (!(state->legacy_cursor_update || state->async_update))
1492d99ced7SRob Clark return false;
1502d99ced7SRob Clark
1512d99ced7SRob Clark /* any connector change, means slow path: */
1522d99ced7SRob Clark for_each_new_connector_in_state(state, connector, connector_state, i)
1532d99ced7SRob Clark return false;
1542d99ced7SRob Clark
1552d99ced7SRob Clark for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1562d99ced7SRob Clark if (drm_atomic_crtc_needs_modeset(crtc_state))
1572d99ced7SRob Clark return false;
158*ce902336SRob Clark if (!crtc_state->active)
159*ce902336SRob Clark return false;
1602d99ced7SRob Clark if (++num_crtcs > 1)
1612d99ced7SRob Clark return false;
1622d99ced7SRob Clark *async_crtc = crtc;
1632d99ced7SRob Clark }
1642d99ced7SRob Clark
1652d99ced7SRob Clark return true;
1662d99ced7SRob Clark }
1672d99ced7SRob Clark
168d4d2c604SRob Clark /* Get bitmask of crtcs that will need to be flushed. The bitmask
169d4d2c604SRob Clark * can be used with for_each_crtc_mask() iterator, to iterate
170d4d2c604SRob Clark * effected crtcs without needing to preserve the atomic state.
171d4d2c604SRob Clark */
get_crtc_mask(struct drm_atomic_state * state)172d4d2c604SRob Clark static unsigned get_crtc_mask(struct drm_atomic_state *state)
173d4d2c604SRob Clark {
174d4d2c604SRob Clark struct drm_crtc_state *crtc_state;
175d4d2c604SRob Clark struct drm_crtc *crtc;
176d4d2c604SRob Clark unsigned i, mask = 0;
177d4d2c604SRob Clark
178d4d2c604SRob Clark for_each_new_crtc_in_state(state, crtc, crtc_state, i)
179d4d2c604SRob Clark mask |= drm_crtc_mask(crtc);
180d4d2c604SRob Clark
181d4d2c604SRob Clark return mask;
182d4d2c604SRob Clark }
183d4d2c604SRob Clark
msm_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)18482836692SKalyan Thota int msm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
18582836692SKalyan Thota {
18682836692SKalyan Thota struct drm_crtc_state *old_crtc_state, *new_crtc_state;
18782836692SKalyan Thota struct drm_crtc *crtc;
18882836692SKalyan Thota int i;
18982836692SKalyan Thota
19082836692SKalyan Thota for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
19182836692SKalyan Thota new_crtc_state, i) {
19282836692SKalyan Thota if ((old_crtc_state->ctm && !new_crtc_state->ctm) ||
19382836692SKalyan Thota (!old_crtc_state->ctm && new_crtc_state->ctm)) {
19482836692SKalyan Thota new_crtc_state->mode_changed = true;
19582836692SKalyan Thota state->allow_modeset = true;
19682836692SKalyan Thota }
19782836692SKalyan Thota }
19882836692SKalyan Thota
19982836692SKalyan Thota return drm_atomic_helper_check(dev, state);
20082836692SKalyan Thota }
20182836692SKalyan Thota
msm_atomic_commit_tail(struct drm_atomic_state * state)202d14659f5SSean Paul void msm_atomic_commit_tail(struct drm_atomic_state *state)
203cf3a7e4cSRob Clark {
204cf3a7e4cSRob Clark struct drm_device *dev = state->dev;
2050b776d45SRob Clark struct msm_drm_private *priv = dev->dev_private;
2060b776d45SRob Clark struct msm_kms *kms = priv->kms;
2072d99ced7SRob Clark struct drm_crtc *async_crtc = NULL;
208d4d2c604SRob Clark unsigned crtc_mask = get_crtc_mask(state);
20952ff0d30SRob Clark bool async = can_do_async(state, &async_crtc);
2100b776d45SRob Clark
211d934a712SRob Clark trace_msm_atomic_commit_tail_start(async, crtc_mask);
212d934a712SRob Clark
213e35a29d5SRob Clark kms->funcs->enable_commit(kms);
2142d99ced7SRob Clark
2152d99ced7SRob Clark /*
2162d99ced7SRob Clark * Ensure any previous (potentially async) commit has
2172d99ced7SRob Clark * completed:
2182d99ced7SRob Clark */
219b3d91800SKrishna Manikandan lock_crtcs(kms, crtc_mask);
220d934a712SRob Clark trace_msm_atomic_wait_flush_start(crtc_mask);
2212d99ced7SRob Clark kms->funcs->wait_flush(kms, crtc_mask);
222d934a712SRob Clark trace_msm_atomic_wait_flush_finish(crtc_mask);
2232d99ced7SRob Clark
2242d99ced7SRob Clark /*
2252d99ced7SRob Clark * Now that there is no in-progress flush, prepare the
2262d99ced7SRob Clark * current update:
2272d99ced7SRob Clark */
228078f6ec8SJessica Zhang if (kms->funcs->prepare_commit)
2290b776d45SRob Clark kms->funcs->prepare_commit(kms, state);
230cf3a7e4cSRob Clark
2319f6b6564SRob Clark /*
2329f6b6564SRob Clark * Push atomic updates down to hardware:
2339f6b6564SRob Clark */
2341af434a9SDaniel Vetter drm_atomic_helper_commit_modeset_disables(dev, state);
2352b58e98dSLiu Ying drm_atomic_helper_commit_planes(dev, state, 0);
2361af434a9SDaniel Vetter drm_atomic_helper_commit_modeset_enables(dev, state);
237cf3a7e4cSRob Clark
2382d99ced7SRob Clark if (async) {
2392d99ced7SRob Clark struct msm_pending_timer *timer =
2402d99ced7SRob Clark &kms->pending_timers[drm_crtc_index(async_crtc)];
2412d99ced7SRob Clark
2422d99ced7SRob Clark /* async updates are limited to single-crtc updates: */
2432d99ced7SRob Clark WARN_ON(crtc_mask != drm_crtc_mask(async_crtc));
2442d99ced7SRob Clark
2452d99ced7SRob Clark /*
2462d99ced7SRob Clark * Start timer if we don't already have an update pending
2472d99ced7SRob Clark * on this crtc:
2482d99ced7SRob Clark */
2492d99ced7SRob Clark if (!(kms->pending_crtc_mask & crtc_mask)) {
2502d99ced7SRob Clark ktime_t vsync_time, wakeup_time;
2512d99ced7SRob Clark
2522d99ced7SRob Clark kms->pending_crtc_mask |= crtc_mask;
2532d99ced7SRob Clark
25452ff0d30SRob Clark if (drm_crtc_next_vblank_start(async_crtc, &vsync_time))
25552ff0d30SRob Clark goto fallback;
25652ff0d30SRob Clark
2572d99ced7SRob Clark wakeup_time = ktime_sub(vsync_time, ms_to_ktime(1));
2582d99ced7SRob Clark
259ddb6e37aSRob Clark msm_hrtimer_queue_work(&timer->work, wakeup_time,
2602d99ced7SRob Clark HRTIMER_MODE_ABS);
2612d99ced7SRob Clark }
2622d99ced7SRob Clark
2632d99ced7SRob Clark kms->funcs->disable_commit(kms);
264b3d91800SKrishna Manikandan unlock_crtcs(kms, crtc_mask);
2652d99ced7SRob Clark /*
2662d99ced7SRob Clark * At this point, from drm core's perspective, we
2672d99ced7SRob Clark * are done with the atomic update, so we can just
2682d99ced7SRob Clark * go ahead and signal that it is done:
2692d99ced7SRob Clark */
2702d99ced7SRob Clark drm_atomic_helper_commit_hw_done(state);
2712d99ced7SRob Clark drm_atomic_helper_cleanup_planes(dev, state);
2722d99ced7SRob Clark
273d934a712SRob Clark trace_msm_atomic_commit_tail_finish(async, crtc_mask);
274d934a712SRob Clark
2752d99ced7SRob Clark return;
2762d99ced7SRob Clark }
2772d99ced7SRob Clark
27852ff0d30SRob Clark fallback:
2792d99ced7SRob Clark /*
2802d99ced7SRob Clark * If there is any async flush pending on updated crtcs, fold
2812d99ced7SRob Clark * them into the current flush.
2822d99ced7SRob Clark */
2832d99ced7SRob Clark kms->pending_crtc_mask &= ~crtc_mask;
2842d99ced7SRob Clark
28543906812SRob Clark vblank_get(kms, crtc_mask);
28643906812SRob Clark
2879f6b6564SRob Clark /*
2889f6b6564SRob Clark * Flush hardware updates:
2899f6b6564SRob Clark */
290d934a712SRob Clark trace_msm_atomic_flush_commit(crtc_mask);
2919f6b6564SRob Clark kms->funcs->flush_commit(kms, crtc_mask);
292b3d91800SKrishna Manikandan unlock_crtcs(kms, crtc_mask);
2932d99ced7SRob Clark /*
2942d99ced7SRob Clark * Wait for flush to complete:
2952d99ced7SRob Clark */
296d934a712SRob Clark trace_msm_atomic_wait_flush_start(crtc_mask);
297d4d2c604SRob Clark kms->funcs->wait_flush(kms, crtc_mask);
298d934a712SRob Clark trace_msm_atomic_wait_flush_finish(crtc_mask);
2992d99ced7SRob Clark
30043906812SRob Clark vblank_put(kms, crtc_mask);
30143906812SRob Clark
302b3d91800SKrishna Manikandan lock_crtcs(kms, crtc_mask);
30380b4b4a7SRob Clark kms->funcs->complete_commit(kms, crtc_mask);
304b3d91800SKrishna Manikandan unlock_crtcs(kms, crtc_mask);
305e35a29d5SRob Clark kms->funcs->disable_commit(kms);
30670db18dcSSean Paul
30770db18dcSSean Paul drm_atomic_helper_commit_hw_done(state);
30870db18dcSSean Paul drm_atomic_helper_cleanup_planes(dev, state);
309d934a712SRob Clark
310d934a712SRob Clark trace_msm_atomic_commit_tail_finish(async, crtc_mask);
311347b90b4SSean Paul }
312