xref: /openbmc/linux/drivers/gpu/drm/msm/msm_atomic.c (revision 347b90b406ff6b6f3c9f666a527eb46e0fcd7aaf)
1cf3a7e4cSRob Clark /*
2cf3a7e4cSRob Clark  * Copyright (C) 2014 Red Hat
3cf3a7e4cSRob Clark  * Author: Rob Clark <robdclark@gmail.com>
4cf3a7e4cSRob Clark  *
5cf3a7e4cSRob Clark  * This program is free software; you can redistribute it and/or modify it
6cf3a7e4cSRob Clark  * under the terms of the GNU General Public License version 2 as published by
7cf3a7e4cSRob Clark  * the Free Software Foundation.
8cf3a7e4cSRob Clark  *
9cf3a7e4cSRob Clark  * This program is distributed in the hope that it will be useful, but WITHOUT
10cf3a7e4cSRob Clark  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11cf3a7e4cSRob Clark  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12cf3a7e4cSRob Clark  * more details.
13cf3a7e4cSRob Clark  *
14cf3a7e4cSRob Clark  * You should have received a copy of the GNU General Public License along with
15cf3a7e4cSRob Clark  * this program.  If not, see <http://www.gnu.org/licenses/>.
16cf3a7e4cSRob Clark  */
17cf3a7e4cSRob Clark 
18cf3a7e4cSRob Clark #include "msm_drv.h"
19cf3a7e4cSRob Clark #include "msm_kms.h"
20cf3a7e4cSRob Clark #include "msm_gem.h"
21fde5de6cSRob Clark #include "msm_fence.h"
22cf3a7e4cSRob Clark 
23cf3a7e4cSRob Clark struct msm_commit {
240b776d45SRob Clark 	struct drm_device *dev;
25cf3a7e4cSRob Clark 	struct drm_atomic_state *state;
26ba00c3f2SRob Clark 	struct work_struct work;
27f86afecfSRob Clark 	uint32_t crtc_mask;
28cf3a7e4cSRob Clark };
29cf3a7e4cSRob Clark 
30ba00c3f2SRob Clark static void commit_worker(struct work_struct *work);
31cf3a7e4cSRob Clark 
32f86afecfSRob Clark /* block until specified crtcs are no longer pending update, and
33f86afecfSRob Clark  * atomically mark them as pending update
34f86afecfSRob Clark  */
35f86afecfSRob Clark static int start_atomic(struct msm_drm_private *priv, uint32_t crtc_mask)
36f86afecfSRob Clark {
37f86afecfSRob Clark 	int ret;
38f86afecfSRob Clark 
39f86afecfSRob Clark 	spin_lock(&priv->pending_crtcs_event.lock);
40f86afecfSRob Clark 	ret = wait_event_interruptible_locked(priv->pending_crtcs_event,
41f86afecfSRob Clark 			!(priv->pending_crtcs & crtc_mask));
42f86afecfSRob Clark 	if (ret == 0) {
43f86afecfSRob Clark 		DBG("start: %08x", crtc_mask);
44f86afecfSRob Clark 		priv->pending_crtcs |= crtc_mask;
45f86afecfSRob Clark 	}
46f86afecfSRob Clark 	spin_unlock(&priv->pending_crtcs_event.lock);
47f86afecfSRob Clark 
48f86afecfSRob Clark 	return ret;
49f86afecfSRob Clark }
50f86afecfSRob Clark 
51f86afecfSRob Clark /* clear specified crtcs (no longer pending update)
52f86afecfSRob Clark  */
53f86afecfSRob Clark static void end_atomic(struct msm_drm_private *priv, uint32_t crtc_mask)
54f86afecfSRob Clark {
55f86afecfSRob Clark 	spin_lock(&priv->pending_crtcs_event.lock);
56f86afecfSRob Clark 	DBG("end: %08x", crtc_mask);
57f86afecfSRob Clark 	priv->pending_crtcs &= ~crtc_mask;
58f86afecfSRob Clark 	wake_up_all_locked(&priv->pending_crtcs_event);
59f86afecfSRob Clark 	spin_unlock(&priv->pending_crtcs_event.lock);
60f86afecfSRob Clark }
61f86afecfSRob Clark 
620b776d45SRob Clark static struct msm_commit *commit_init(struct drm_atomic_state *state)
63cf3a7e4cSRob Clark {
64cf3a7e4cSRob Clark 	struct msm_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
65cf3a7e4cSRob Clark 
66cf3a7e4cSRob Clark 	if (!c)
67cf3a7e4cSRob Clark 		return NULL;
68cf3a7e4cSRob Clark 
690b776d45SRob Clark 	c->dev = state->dev;
70cf3a7e4cSRob Clark 	c->state = state;
710b776d45SRob Clark 
72ba00c3f2SRob Clark 	INIT_WORK(&c->work, commit_worker);
73cf3a7e4cSRob Clark 
74cf3a7e4cSRob Clark 	return c;
75cf3a7e4cSRob Clark }
76cf3a7e4cSRob Clark 
770b776d45SRob Clark static void commit_destroy(struct msm_commit *c)
780b776d45SRob Clark {
790b776d45SRob Clark 	end_atomic(c->dev->dev_private, c->crtc_mask);
800b776d45SRob Clark 	kfree(c);
810b776d45SRob Clark }
820b776d45SRob Clark 
830a5c9aadSHai Li static void msm_atomic_wait_for_commit_done(struct drm_device *dev,
840a5c9aadSHai Li 		struct drm_atomic_state *old_state)
850a5c9aadSHai Li {
860a5c9aadSHai Li 	struct drm_crtc *crtc;
87d7429669SMaarten Lankhorst 	struct drm_crtc_state *new_crtc_state;
880a5c9aadSHai Li 	struct msm_drm_private *priv = old_state->dev->dev_private;
890a5c9aadSHai Li 	struct msm_kms *kms = priv->kms;
900a5c9aadSHai Li 	int i;
910a5c9aadSHai Li 
92d7429669SMaarten Lankhorst 	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
93d7429669SMaarten Lankhorst 		if (!new_crtc_state->active)
940a5c9aadSHai Li 			continue;
950a5c9aadSHai Li 
960a5c9aadSHai Li 		kms->funcs->wait_for_crtc_commit_done(kms, crtc);
970a5c9aadSHai Li 	}
980a5c9aadSHai Li }
990a5c9aadSHai Li 
100*347b90b4SSean Paul static void msm_atomic_commit_tail(struct drm_atomic_state *state)
101cf3a7e4cSRob Clark {
102cf3a7e4cSRob Clark 	struct drm_device *dev = state->dev;
1030b776d45SRob Clark 	struct msm_drm_private *priv = dev->dev_private;
1040b776d45SRob Clark 	struct msm_kms *kms = priv->kms;
1050b776d45SRob Clark 
1060b776d45SRob Clark 	kms->funcs->prepare_commit(kms, state);
107cf3a7e4cSRob Clark 
1081af434a9SDaniel Vetter 	drm_atomic_helper_commit_modeset_disables(dev, state);
109cf3a7e4cSRob Clark 
1102b58e98dSLiu Ying 	drm_atomic_helper_commit_planes(dev, state, 0);
111cf3a7e4cSRob Clark 
1121af434a9SDaniel Vetter 	drm_atomic_helper_commit_modeset_enables(dev, state);
113cf3a7e4cSRob Clark 
114f86afecfSRob Clark 	/* NOTE: _wait_for_vblanks() only waits for vblank on
115f86afecfSRob Clark 	 * enabled CRTCs.  So we end up faulting when disabling
116f86afecfSRob Clark 	 * due to (potentially) unref'ing the outgoing fb's
117f86afecfSRob Clark 	 * before the vblank when the disable has latched.
118f86afecfSRob Clark 	 *
119f86afecfSRob Clark 	 * But if it did wait on disabled (or newly disabled)
120f86afecfSRob Clark 	 * CRTCs, that would be racy (ie. we could have missed
121f86afecfSRob Clark 	 * the irq.  We need some way to poll for pipe shut
122f86afecfSRob Clark 	 * down.  Or just live with occasionally hitting the
123f86afecfSRob Clark 	 * timeout in the CRTC disable path (which really should
124f86afecfSRob Clark 	 * not be critical path)
125f86afecfSRob Clark 	 */
126f86afecfSRob Clark 
1270a5c9aadSHai Li 	msm_atomic_wait_for_commit_done(dev, state);
128cf3a7e4cSRob Clark 
129cf3a7e4cSRob Clark 	drm_atomic_helper_cleanup_planes(dev, state);
130cf3a7e4cSRob Clark 
1310b776d45SRob Clark 	kms->funcs->complete_commit(kms, state);
132*347b90b4SSean Paul }
133*347b90b4SSean Paul 
134*347b90b4SSean Paul /* The (potentially) asynchronous part of the commit.  At this point
135*347b90b4SSean Paul  * nothing can fail short of armageddon.
136*347b90b4SSean Paul  */
137*347b90b4SSean Paul static void complete_commit(struct msm_commit *c)
138*347b90b4SSean Paul {
139*347b90b4SSean Paul 	struct drm_atomic_state *state = c->state;
140*347b90b4SSean Paul 	struct drm_device *dev = state->dev;
141*347b90b4SSean Paul 
142*347b90b4SSean Paul 	drm_atomic_helper_wait_for_fences(dev, state, false);
143*347b90b4SSean Paul 
144*347b90b4SSean Paul 	msm_atomic_commit_tail(state);
1450b776d45SRob Clark 
1460853695cSChris Wilson 	drm_atomic_state_put(state);
147cf3a7e4cSRob Clark 
1480b776d45SRob Clark 	commit_destroy(c);
149cf3a7e4cSRob Clark }
150cf3a7e4cSRob Clark 
151ba00c3f2SRob Clark static void commit_worker(struct work_struct *work)
152cf3a7e4cSRob Clark {
153*347b90b4SSean Paul 	complete_commit(container_of(work, struct msm_commit, work));
154cf3a7e4cSRob Clark }
155cf3a7e4cSRob Clark 
156cf3a7e4cSRob Clark /**
157cf3a7e4cSRob Clark  * drm_atomic_helper_commit - commit validated state object
158cf3a7e4cSRob Clark  * @dev: DRM device
159cf3a7e4cSRob Clark  * @state: the driver state object
160a3ccfb9fSMaarten Lankhorst  * @nonblock: nonblocking commit
161cf3a7e4cSRob Clark  *
162cf3a7e4cSRob Clark  * This function commits a with drm_atomic_helper_check() pre-validated state
163a3ccfb9fSMaarten Lankhorst  * object. This can still fail when e.g. the framebuffer reservation fails.
164cf3a7e4cSRob Clark  *
165cf3a7e4cSRob Clark  * RETURNS
166cf3a7e4cSRob Clark  * Zero for success or -errno.
167cf3a7e4cSRob Clark  */
168cf3a7e4cSRob Clark int msm_atomic_commit(struct drm_device *dev,
169a3ccfb9fSMaarten Lankhorst 		struct drm_atomic_state *state, bool nonblock)
170cf3a7e4cSRob Clark {
171ca762a8aSRob Clark 	struct msm_drm_private *priv = dev->dev_private;
172f86afecfSRob Clark 	struct msm_commit *c;
1738d76b79fSDaniel Vetter 	struct drm_crtc *crtc;
1748d76b79fSDaniel Vetter 	struct drm_crtc_state *crtc_state;
1758d76b79fSDaniel Vetter 	struct drm_plane *plane;
176d7429669SMaarten Lankhorst 	struct drm_plane_state *old_plane_state, *new_plane_state;
177cf3a7e4cSRob Clark 	int i, ret;
178cf3a7e4cSRob Clark 
179cf3a7e4cSRob Clark 	ret = drm_atomic_helper_prepare_planes(dev, state);
180cf3a7e4cSRob Clark 	if (ret)
181cf3a7e4cSRob Clark 		return ret;
182cf3a7e4cSRob Clark 
183224a4c97SGustavo Padovan 	/*
184224a4c97SGustavo Padovan 	 * Note that plane->atomic_async_check() should fail if we need
185224a4c97SGustavo Padovan 	 * to re-assign hwpipe or anything that touches global atomic
186224a4c97SGustavo Padovan 	 * state, so we'll never go down the async update path in those
187224a4c97SGustavo Padovan 	 * cases.
188224a4c97SGustavo Padovan 	 */
189224a4c97SGustavo Padovan 	if (state->async_update) {
190224a4c97SGustavo Padovan 		drm_atomic_helper_async_commit(dev, state);
191224a4c97SGustavo Padovan 		drm_atomic_helper_cleanup_planes(dev, state);
192224a4c97SGustavo Padovan 		return 0;
193224a4c97SGustavo Padovan 	}
194224a4c97SGustavo Padovan 
1950b776d45SRob Clark 	c = commit_init(state);
196f65c18c0SLaurent Pinchart 	if (!c) {
197f65c18c0SLaurent Pinchart 		ret = -ENOMEM;
198f65c18c0SLaurent Pinchart 		goto error;
199f65c18c0SLaurent Pinchart 	}
200f86afecfSRob Clark 
201f86afecfSRob Clark 	/*
202f86afecfSRob Clark 	 * Figure out what crtcs we have:
203f86afecfSRob Clark 	 */
204d7429669SMaarten Lankhorst 	for_each_new_crtc_in_state(state, crtc, crtc_state, i)
2058d76b79fSDaniel Vetter 		c->crtc_mask |= drm_crtc_mask(crtc);
206cf3a7e4cSRob Clark 
207cf3a7e4cSRob Clark 	/*
208b6295f9aSRob Clark 	 * Figure out what fence to wait for:
209b6295f9aSRob Clark 	 */
210d7429669SMaarten Lankhorst 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
211d7429669SMaarten Lankhorst 		if ((new_plane_state->fb != old_plane_state->fb) && new_plane_state->fb) {
212d7429669SMaarten Lankhorst 			struct drm_gem_object *obj = msm_framebuffer_bo(new_plane_state->fb, 0);
213b6295f9aSRob Clark 			struct msm_gem_object *msm_obj = to_msm_bo(obj);
2145d586983SGustavo Padovan 			struct dma_fence *fence = reservation_object_get_excl_rcu(msm_obj->resv);
215b6295f9aSRob Clark 
216d7429669SMaarten Lankhorst 			drm_atomic_set_fence_for_plane(new_plane_state, fence);
217b6295f9aSRob Clark 		}
218b6295f9aSRob Clark 	}
219b6295f9aSRob Clark 
220b6295f9aSRob Clark 	/*
221f86afecfSRob Clark 	 * Wait for pending updates on any of the same crtc's and then
222f86afecfSRob Clark 	 * mark our set of crtc's as busy:
223f86afecfSRob Clark 	 */
224f86afecfSRob Clark 	ret = start_atomic(dev->dev_private, c->crtc_mask);
225771f14c5SMaarten Lankhorst 	if (ret)
226771f14c5SMaarten Lankhorst 		goto err_free;
227771f14c5SMaarten Lankhorst 
228771f14c5SMaarten Lankhorst 	BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
229f86afecfSRob Clark 
230f86afecfSRob Clark 	/*
231cf3a7e4cSRob Clark 	 * This is the point of no return - everything below never fails except
232cf3a7e4cSRob Clark 	 * when the hw goes bonghits. Which means we can commit the new state on
233cf3a7e4cSRob Clark 	 * the software side now.
234cf3a7e4cSRob Clark 	 */
235870d738aSRob Clark 
236cf3a7e4cSRob Clark 	/*
237cf3a7e4cSRob Clark 	 * Everything below can be run asynchronously without the need to grab
238cf3a7e4cSRob Clark 	 * any modeset locks at all under one conditions: It must be guaranteed
239cf3a7e4cSRob Clark 	 * that the asynchronous work has either been cancelled (if the driver
240cf3a7e4cSRob Clark 	 * supports it, which at least requires that the framebuffers get
241cf3a7e4cSRob Clark 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
242cf3a7e4cSRob Clark 	 * before the new state gets committed on the software side with
243cf3a7e4cSRob Clark 	 * drm_atomic_helper_swap_state().
244cf3a7e4cSRob Clark 	 *
245cf3a7e4cSRob Clark 	 * This scheme allows new atomic state updates to be prepared and
246cf3a7e4cSRob Clark 	 * checked in parallel to the asynchronous completion of the previous
247cf3a7e4cSRob Clark 	 * update. Which is important since compositors need to figure out the
248cf3a7e4cSRob Clark 	 * composition of the next frame right after having submitted the
249cf3a7e4cSRob Clark 	 * current layout.
250cf3a7e4cSRob Clark 	 */
251cf3a7e4cSRob Clark 
2520853695cSChris Wilson 	drm_atomic_state_get(state);
253ba00c3f2SRob Clark 	if (nonblock) {
254ba00c3f2SRob Clark 		queue_work(priv->atomic_wq, &c->work);
255cf3a7e4cSRob Clark 		return 0;
256cf3a7e4cSRob Clark 	}
257cf3a7e4cSRob Clark 
258*347b90b4SSean Paul 	complete_commit(c);
259cf3a7e4cSRob Clark 
260cf3a7e4cSRob Clark 	return 0;
261f65c18c0SLaurent Pinchart 
262771f14c5SMaarten Lankhorst err_free:
263771f14c5SMaarten Lankhorst 	kfree(c);
264f65c18c0SLaurent Pinchart error:
265f65c18c0SLaurent Pinchart 	drm_atomic_helper_cleanup_planes(dev, state);
266f65c18c0SLaurent Pinchart 	return ret;
267cf3a7e4cSRob Clark }
268