1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "msm_drv.h"
19#include "msm_kms.h"
20#include "msm_gem.h"
21#include "msm_fence.h"
22
23struct msm_commit {
24 struct drm_device *dev;
25 struct drm_atomic_state *state;
26 struct work_struct work;
27 uint32_t crtc_mask;
28};
29
30static void commit_worker(struct work_struct *work);
31
32
33
34
35static int start_atomic(struct msm_drm_private *priv, uint32_t crtc_mask)
36{
37 int ret;
38
39 spin_lock(&priv->pending_crtcs_event.lock);
40 ret = wait_event_interruptible_locked(priv->pending_crtcs_event,
41 !(priv->pending_crtcs & crtc_mask));
42 if (ret == 0) {
43 DBG("start: %08x", crtc_mask);
44 priv->pending_crtcs |= crtc_mask;
45 }
46 spin_unlock(&priv->pending_crtcs_event.lock);
47
48 return ret;
49}
50
51
52
53static void end_atomic(struct msm_drm_private *priv, uint32_t crtc_mask)
54{
55 spin_lock(&priv->pending_crtcs_event.lock);
56 DBG("end: %08x", crtc_mask);
57 priv->pending_crtcs &= ~crtc_mask;
58 wake_up_all_locked(&priv->pending_crtcs_event);
59 spin_unlock(&priv->pending_crtcs_event.lock);
60}
61
62static struct msm_commit *commit_init(struct drm_atomic_state *state)
63{
64 struct msm_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
65
66 if (!c)
67 return NULL;
68
69 c->dev = state->dev;
70 c->state = state;
71
72 INIT_WORK(&c->work, commit_worker);
73
74 return c;
75}
76
77static void commit_destroy(struct msm_commit *c)
78{
79 end_atomic(c->dev->dev_private, c->crtc_mask);
80 kfree(c);
81}
82
83static void msm_atomic_wait_for_commit_done(struct drm_device *dev,
84 struct drm_atomic_state *old_state)
85{
86 struct drm_crtc *crtc;
87 struct drm_crtc_state *new_crtc_state;
88 struct msm_drm_private *priv = old_state->dev->dev_private;
89 struct msm_kms *kms = priv->kms;
90 int i;
91
92 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
93 if (!new_crtc_state->active)
94 continue;
95
96 kms->funcs->wait_for_crtc_commit_done(kms, crtc);
97 }
98}
99
100
101
102
103static void complete_commit(struct msm_commit *c, bool async)
104{
105 struct drm_atomic_state *state = c->state;
106 struct drm_device *dev = state->dev;
107 struct msm_drm_private *priv = dev->dev_private;
108 struct msm_kms *kms = priv->kms;
109
110 drm_atomic_helper_wait_for_fences(dev, state, false);
111
112 kms->funcs->prepare_commit(kms, state);
113
114 drm_atomic_helper_commit_modeset_disables(dev, state);
115
116 drm_atomic_helper_commit_planes(dev, state, 0);
117
118 drm_atomic_helper_commit_modeset_enables(dev, state);
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 msm_atomic_wait_for_commit_done(dev, state);
134
135 drm_atomic_helper_cleanup_planes(dev, state);
136
137 kms->funcs->complete_commit(kms, state);
138
139 drm_atomic_state_put(state);
140
141 commit_destroy(c);
142}
143
144static void commit_worker(struct work_struct *work)
145{
146 complete_commit(container_of(work, struct msm_commit, work), true);
147}
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162int msm_atomic_check(struct drm_device *dev,
163 struct drm_atomic_state *state)
164{
165 int ret;
166
167 ret = drm_atomic_helper_check_modeset(dev, state);
168 if (ret)
169 return ret;
170
171 ret = drm_atomic_helper_check_planes(dev, state);
172 if (ret)
173 return ret;
174
175 return ret;
176}
177
178
179
180
181
182
183
184
185
186
187
188
189
190int msm_atomic_commit(struct drm_device *dev,
191 struct drm_atomic_state *state, bool nonblock)
192{
193 struct msm_drm_private *priv = dev->dev_private;
194 struct msm_commit *c;
195 struct drm_crtc *crtc;
196 struct drm_crtc_state *crtc_state;
197 struct drm_plane *plane;
198 struct drm_plane_state *old_plane_state, *new_plane_state;
199 int i, ret;
200
201 ret = drm_atomic_helper_prepare_planes(dev, state);
202 if (ret)
203 return ret;
204
205 c = commit_init(state);
206 if (!c) {
207 ret = -ENOMEM;
208 goto error;
209 }
210
211
212
213
214 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
215 c->crtc_mask |= drm_crtc_mask(crtc);
216
217
218
219
220 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
221 if ((new_plane_state->fb != old_plane_state->fb) && new_plane_state->fb) {
222 struct drm_gem_object *obj = msm_framebuffer_bo(new_plane_state->fb, 0);
223 struct msm_gem_object *msm_obj = to_msm_bo(obj);
224 struct dma_fence *fence = reservation_object_get_excl_rcu(msm_obj->resv);
225
226 drm_atomic_set_fence_for_plane(new_plane_state, fence);
227 }
228 }
229
230
231
232
233
234 ret = start_atomic(dev->dev_private, c->crtc_mask);
235 if (ret)
236 goto err_free;
237
238 BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
239
240
241
242
243
244
245
246
247 if (to_kms_state(state)->state)
248 priv->kms->funcs->swap_state(priv->kms, state);
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 drm_atomic_state_get(state);
267 if (nonblock) {
268 queue_work(priv->atomic_wq, &c->work);
269 return 0;
270 }
271
272 complete_commit(c, false);
273
274 return 0;
275
276err_free:
277 kfree(c);
278error:
279 drm_atomic_helper_cleanup_planes(dev, state);
280 return ret;
281}
282
283struct drm_atomic_state *msm_atomic_state_alloc(struct drm_device *dev)
284{
285 struct msm_kms_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
286
287 if (!state || drm_atomic_state_init(dev, &state->base) < 0) {
288 kfree(state);
289 return NULL;
290 }
291
292 return &state->base;
293}
294
295void msm_atomic_state_clear(struct drm_atomic_state *s)
296{
297 struct msm_kms_state *state = to_kms_state(s);
298 drm_atomic_state_default_clear(&state->base);
299 kfree(state->state);
300 state->state = NULL;
301}
302
303void msm_atomic_state_free(struct drm_atomic_state *state)
304{
305 kfree(to_kms_state(state)->state);
306 drm_atomic_state_default_release(state);
307 kfree(state);
308}
309