blob: ddcfc8efe59e9368fba74cfc0df2a9bccba4da61 [file] [log] [blame]
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
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:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28#include <drm/drmP.h>
29#include <drm/drm_atomic.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020032#include <drm/drm_atomic_helper.h>
Daniel Vettere2330f02014-10-29 11:34:56 +010033#include <linux/fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Daniel Vetter3150c7d2014-11-06 20:53:29 +010035/**
36 * DOC: overview
37 *
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
43 *
44 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020045 * drm_atomic_helper_check() and for the commit callback with
46 * drm_atomic_helper_commit(). But the individual stages and callbacks are
47 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010048 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020051 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
52 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010053 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010055 *
56 * The atomic helper uses the same function table structures as all other
57 * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58 * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59 * also shares the struct &drm_plane_helper_funcs function table with the plane
60 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010061 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010062static void
63drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 struct drm_plane_state *plane_state,
65 struct drm_plane *plane)
66{
67 struct drm_crtc_state *crtc_state;
68
69 if (plane->state->crtc) {
Rob Clark4b08eae2014-12-08 10:45:43 -050070 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
Daniel Vetterc2fcd272014-11-05 00:14:14 +010071
72 if (WARN_ON(!crtc_state))
73 return;
74
75 crtc_state->planes_changed = true;
76 }
77
78 if (plane_state->crtc) {
79 crtc_state =
80 state->crtc_states[drm_crtc_index(plane_state->crtc)];
81
82 if (WARN_ON(!crtc_state))
83 return;
84
85 crtc_state->planes_changed = true;
86 }
87}
88
Daniel Vetter97ac3202015-12-03 10:49:14 +010089static bool
90check_pending_encoder_assignment(struct drm_atomic_state *state,
Daniel Vetter07bad492015-12-08 09:49:18 +010091 struct drm_encoder *new_encoder)
Daniel Vetter97ac3202015-12-03 10:49:14 +010092{
93 struct drm_connector *connector;
94 struct drm_connector_state *conn_state;
95 int i;
96
97 for_each_connector_in_state(state, connector, conn_state, i) {
98 if (conn_state->best_encoder != new_encoder)
99 continue;
100
101 /* encoder already assigned and we're trying to re-steal it! */
102 if (connector->state->best_encoder != conn_state->best_encoder)
103 return false;
104 }
105
106 return true;
107}
108
Daniel Vetter623369e2014-09-16 17:50:47 +0200109static struct drm_crtc *
110get_current_crtc_for_encoder(struct drm_device *dev,
111 struct drm_encoder *encoder)
112{
113 struct drm_mode_config *config = &dev->mode_config;
114 struct drm_connector *connector;
115
116 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
117
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +0200118 drm_for_each_connector(connector, dev) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200119 if (connector->state->best_encoder != encoder)
120 continue;
121
122 return connector->state->crtc;
123 }
124
125 return NULL;
126}
127
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100128static void
129set_best_encoder(struct drm_atomic_state *state,
130 struct drm_connector_state *conn_state,
131 struct drm_encoder *encoder)
132{
133 struct drm_crtc_state *crtc_state;
134 struct drm_crtc *crtc;
135
136 if (conn_state->best_encoder) {
137 /* Unset the encoder_mask in the old crtc state. */
138 crtc = conn_state->connector->state->crtc;
139
140 /* A NULL crtc is an error here because we should have
141 * duplicated a NULL best_encoder when crtc was NULL.
142 * As an exception restoring duplicated atomic state
143 * during resume is allowed, so don't warn when
144 * best_encoder is equal to encoder we intend to set.
145 */
146 WARN_ON(!crtc && encoder != conn_state->best_encoder);
147 if (crtc) {
148 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
149
150 crtc_state->encoder_mask &=
151 ~(1 << drm_encoder_index(conn_state->best_encoder));
152 }
153 }
154
155 if (encoder) {
156 crtc = conn_state->crtc;
157 WARN_ON(!crtc);
158 if (crtc) {
159 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
160
161 crtc_state->encoder_mask |=
162 1 << drm_encoder_index(encoder);
163 }
164 }
165
166 conn_state->best_encoder = encoder;
167}
168
Daniel Vetter623369e2014-09-16 17:50:47 +0200169static int
170steal_encoder(struct drm_atomic_state *state,
171 struct drm_encoder *encoder,
172 struct drm_crtc *encoder_crtc)
173{
174 struct drm_mode_config *config = &state->dev->mode_config;
175 struct drm_crtc_state *crtc_state;
176 struct drm_connector *connector;
177 struct drm_connector_state *connector_state;
178
179 /*
180 * We can only steal an encoder coming from a connector, which means we
181 * must already hold the connection_mutex.
182 */
183 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
184
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200185 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100186 encoder->base.id, encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200187 encoder_crtc->base.id, encoder_crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200188
189 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
190 if (IS_ERR(crtc_state))
191 return PTR_ERR(crtc_state);
192
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200193 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200194
195 list_for_each_entry(connector, &config->connector_list, head) {
196 if (connector->state->best_encoder != encoder)
197 continue;
198
Daniel Vetter17a38d92015-02-22 12:24:16 +0100199 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
200 connector->base.id,
201 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200202
203 connector_state = drm_atomic_get_connector_state(state,
204 connector);
205 if (IS_ERR(connector_state))
206 return PTR_ERR(connector_state);
207
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100208 if (connector_state->best_encoder != encoder)
209 continue;
210
211 set_best_encoder(state, connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200212 }
213
214 return 0;
215}
216
217static int
218update_connector_routing(struct drm_atomic_state *state, int conn_idx)
219{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200220 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200221 struct drm_encoder *new_encoder;
222 struct drm_crtc *encoder_crtc;
223 struct drm_connector *connector;
224 struct drm_connector_state *connector_state;
225 struct drm_crtc_state *crtc_state;
226 int idx, ret;
227
228 connector = state->connectors[conn_idx];
229 connector_state = state->connector_states[conn_idx];
230
231 if (!connector)
232 return 0;
233
Daniel Vetter17a38d92015-02-22 12:24:16 +0100234 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
235 connector->base.id,
236 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200237
238 if (connector->state->crtc != connector_state->crtc) {
239 if (connector->state->crtc) {
240 idx = drm_crtc_index(connector->state->crtc);
241
242 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200243 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200244 }
245
246 if (connector_state->crtc) {
247 idx = drm_crtc_index(connector_state->crtc);
248
249 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200250 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200251 }
252 }
253
254 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100255 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200256 connector->base.id,
257 connector->name);
258
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100259 set_best_encoder(state, connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200260
261 return 0;
262 }
263
264 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200265
266 if (funcs->atomic_best_encoder)
267 new_encoder = funcs->atomic_best_encoder(connector,
268 connector_state);
269 else
270 new_encoder = funcs->best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200271
272 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100273 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
274 connector->base.id,
275 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200276 return -EINVAL;
277 }
278
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100279 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
280 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
281 new_encoder->base.id,
282 new_encoder->name,
283 connector_state->crtc->base.id);
284 return -EINVAL;
285 }
286
Daniel Vetter623369e2014-09-16 17:50:47 +0200287 if (new_encoder == connector_state->best_encoder) {
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100288 set_best_encoder(state, connector_state, new_encoder);
289
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200290 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100291 connector->base.id,
292 connector->name,
293 new_encoder->base.id,
294 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200295 connector_state->crtc->base.id,
296 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200297
298 return 0;
299 }
300
Daniel Vetter07bad492015-12-08 09:49:18 +0100301 if (!check_pending_encoder_assignment(state, new_encoder)) {
Daniel Vetter97ac3202015-12-03 10:49:14 +0100302 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
303 connector->base.id,
304 connector->name);
305 return -EINVAL;
306 }
307
Daniel Vetter623369e2014-09-16 17:50:47 +0200308 encoder_crtc = get_current_crtc_for_encoder(state->dev,
309 new_encoder);
310
311 if (encoder_crtc) {
312 ret = steal_encoder(state, new_encoder, encoder_crtc);
313 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100314 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
315 connector->base.id,
316 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200317 return ret;
318 }
319 }
320
Daniel Vetter6ea76f3c2015-08-03 17:24:11 +0200321 if (WARN_ON(!connector_state->crtc))
322 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200323
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100324 set_best_encoder(state, connector_state, new_encoder);
325
Daniel Vetter623369e2014-09-16 17:50:47 +0200326 idx = drm_crtc_index(connector_state->crtc);
327
328 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200329 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200330
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200331 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100332 connector->base.id,
333 connector->name,
334 new_encoder->base.id,
335 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200336 connector_state->crtc->base.id,
337 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200338
339 return 0;
340}
341
342static int
343mode_fixup(struct drm_atomic_state *state)
344{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300345 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200346 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300347 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200348 struct drm_connector_state *conn_state;
349 int i;
350 bool ret;
351
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300352 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200353 if (!crtc_state->mode_changed &&
354 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200355 continue;
356
357 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
358 }
359
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300360 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200361 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200362 struct drm_encoder *encoder;
363
Daniel Vetter623369e2014-09-16 17:50:47 +0200364 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
365
366 if (!conn_state->crtc || !conn_state->best_encoder)
367 continue;
368
369 crtc_state =
370 state->crtc_states[drm_crtc_index(conn_state->crtc)];
371
372 /*
373 * Each encoder has at most one connector (since we always steal
374 * it away), so we won't call ->mode_fixup twice.
375 */
376 encoder = conn_state->best_encoder;
377 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300378 if (!funcs)
379 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200380
Archit Taneja862e6862015-05-21 11:03:16 +0530381 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
382 &crtc_state->adjusted_mode);
383 if (!ret) {
384 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
385 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200386 }
387
Thierry Reding4cd4df82014-12-03 16:44:34 +0100388 if (funcs->atomic_check) {
389 ret = funcs->atomic_check(encoder, crtc_state,
390 conn_state);
391 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100392 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
393 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100394 return ret;
395 }
Inki Dae84524912015-08-11 21:23:49 +0900396 } else if (funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100397 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
398 &crtc_state->adjusted_mode);
399 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100400 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
401 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100402 return -EINVAL;
403 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200404 }
405 }
406
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300407 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200408 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200409
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200410 if (!crtc_state->mode_changed &&
411 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200412 continue;
413
414 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300415 if (!funcs->mode_fixup)
416 continue;
417
Daniel Vetter623369e2014-09-16 17:50:47 +0200418 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
419 &crtc_state->adjusted_mode);
420 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200421 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
422 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200423 return -EINVAL;
424 }
425 }
426
427 return 0;
428}
429
Daniel Vetterd9b13622014-11-26 16:57:41 +0100430/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800431 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100432 * @dev: DRM device
433 * @state: the driver state object
434 *
435 * Check the state object to see if the requested state is physically possible.
436 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200437 * update and adds any additional connectors needed for full modesets and calls
438 * down into ->mode_fixup functions of the driver backend.
439 *
440 * crtc_state->mode_changed is set when the input mode is changed.
441 * crtc_state->connectors_changed is set when a connector is added or
442 * removed from the crtc.
443 * crtc_state->active_changed is set when crtc_state->active changes,
444 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100445 *
446 * IMPORTANT:
447 *
448 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
449 * plane update can't be done without a full modeset) _must_ call this function
450 * afterwards after that change. It is permitted to call this function multiple
451 * times for the same update, e.g. when the ->atomic_check functions depend upon
452 * the adjusted dotclock for fifo space allocation and watermark computation.
453 *
454 * RETURNS
455 * Zero for success or -errno
456 */
457int
Rob Clark934ce1c2014-11-19 16:41:33 -0500458drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200459 struct drm_atomic_state *state)
460{
Daniel Vetter623369e2014-09-16 17:50:47 +0200461 struct drm_crtc *crtc;
462 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300463 struct drm_connector *connector;
464 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200465 int i, ret;
466
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300467 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200468 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200469 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
470 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200471 crtc_state->mode_changed = true;
472 }
473
474 if (crtc->state->enable != crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200475 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
476 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200477
478 /*
479 * For clarity this assignment is done here, but
480 * enable == 0 is only true when there are no
481 * connectors and a NULL mode.
482 *
483 * The other way around is true as well. enable != 0
484 * iff connectors are attached and a mode is set.
485 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200486 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200487 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200488 }
489 }
490
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300491 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200492 /*
493 * This only sets crtc->mode_changed for routing changes,
494 * drivers must set crtc->mode_changed themselves when connector
495 * properties need to be updated.
496 */
497 ret = update_connector_routing(state, i);
498 if (ret)
499 return ret;
500 }
501
502 /*
503 * After all the routing has been prepared we need to add in any
504 * connector which is itself unchanged, but who's crtc changes it's
505 * configuration. This must be done before calling mode_fixup in case a
506 * crtc only changed its mode but has the same set of connectors.
507 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300508 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100509 bool has_connectors =
510 !!crtc_state->connector_mask;
Daniel Vetter623369e2014-09-16 17:50:47 +0200511
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100512 /*
513 * We must set ->active_changed after walking connectors for
514 * otherwise an update that only changes active would result in
515 * a full modeset because update_connector_routing force that.
516 */
517 if (crtc->state->active != crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200518 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
519 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100520 crtc_state->active_changed = true;
521 }
522
Daniel Vetter2465ff62015-06-18 09:58:55 +0200523 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100524 continue;
525
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200526 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
527 crtc->base.id, crtc->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100528 crtc_state->enable ? 'y' : 'n',
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200529 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200530
531 ret = drm_atomic_add_affected_connectors(state, crtc);
532 if (ret != 0)
533 return ret;
534
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200535 ret = drm_atomic_add_affected_planes(state, crtc);
536 if (ret != 0)
537 return ret;
538
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100539 if (crtc_state->enable != has_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200540 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
541 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200542
543 return -EINVAL;
544 }
545 }
546
547 return mode_fixup(state);
548}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100549EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200550
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100551/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800552 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100553 * @dev: DRM device
554 * @state: the driver state object
555 *
556 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100557 * This does all the plane update related checks using by calling into the
558 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100559 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200560 * It also sets crtc_state->planes_changed to indicate that a crtc has
561 * updated planes.
562 *
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100563 * RETURNS
564 * Zero for success or -errno
565 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100566int
567drm_atomic_helper_check_planes(struct drm_device *dev,
568 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100569{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300570 struct drm_crtc *crtc;
571 struct drm_crtc_state *crtc_state;
572 struct drm_plane *plane;
573 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100574 int i, ret = 0;
575
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300576 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200577 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100578
579 funcs = plane->helper_private;
580
581 drm_atomic_helper_plane_changed(state, plane_state, plane);
582
583 if (!funcs || !funcs->atomic_check)
584 continue;
585
586 ret = funcs->atomic_check(plane, plane_state);
587 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200588 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
589 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100590 return ret;
591 }
592 }
593
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300594 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200595 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100596
597 funcs = crtc->helper_private;
598
599 if (!funcs || !funcs->atomic_check)
600 continue;
601
602 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
603 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200604 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
605 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100606 return ret;
607 }
608 }
609
Daniel Vetterd9b13622014-11-26 16:57:41 +0100610 return ret;
611}
612EXPORT_SYMBOL(drm_atomic_helper_check_planes);
613
614/**
615 * drm_atomic_helper_check - validate state object
616 * @dev: DRM device
617 * @state: the driver state object
618 *
619 * Check the state object to see if the requested state is physically possible.
620 * Only crtcs and planes have check callbacks, so for any additional (global)
621 * checking that a driver needs it can simply wrap that around this function.
622 * Drivers without such needs can directly use this as their ->atomic_check()
623 * callback.
624 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100625 * This just wraps the two parts of the state checking for planes and modeset
626 * state in the default order: First it calls drm_atomic_helper_check_modeset()
627 * and then drm_atomic_helper_check_planes(). The assumption is that the
628 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
629 * e.g. properly compute watermarks.
630 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100631 * RETURNS
632 * Zero for success or -errno
633 */
634int drm_atomic_helper_check(struct drm_device *dev,
635 struct drm_atomic_state *state)
636{
637 int ret;
638
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100639 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100640 if (ret)
641 return ret;
642
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100643 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500644 if (ret)
645 return ret;
646
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100647 return ret;
648}
649EXPORT_SYMBOL(drm_atomic_helper_check);
650
Daniel Vetter623369e2014-09-16 17:50:47 +0200651static void
652disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
653{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300654 struct drm_connector *connector;
655 struct drm_connector_state *old_conn_state;
656 struct drm_crtc *crtc;
657 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200658 int i;
659
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300660 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200661 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200662 struct drm_encoder *encoder;
663
Daniel Vetter623369e2014-09-16 17:50:47 +0200664 /* Shut down everything that's in the changeset and currently
665 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300666 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200667 continue;
668
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100669 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
670
Daniel Vetter4218a322015-03-26 22:18:40 +0100671 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200672 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100673 continue;
674
Rob Clark46df9ad2014-11-20 15:40:36 -0500675 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200676
Rob Clark46df9ad2014-11-20 15:40:36 -0500677 /* We shouldn't get this far if we didn't previously have
678 * an encoder.. but WARN_ON() rather than explode.
679 */
680 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200681 continue;
682
683 funcs = encoder->helper_private;
684
Daniel Vetter17a38d92015-02-22 12:24:16 +0100685 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
686 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100687
Daniel Vetter623369e2014-09-16 17:50:47 +0200688 /*
689 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800690 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200691 */
Archit Taneja862e6862015-05-21 11:03:16 +0530692 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200693
694 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100695 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200696 funcs->prepare(encoder);
697 else if (funcs->disable)
698 funcs->disable(encoder);
699 else
700 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
701
Archit Taneja862e6862015-05-21 11:03:16 +0530702 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200703 }
704
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300705 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200706 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200707
708 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200709 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100710 continue;
711
712 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200713 continue;
714
715 funcs = crtc->helper_private;
716
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200717 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
718 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100719
720
Daniel Vetter623369e2014-09-16 17:50:47 +0200721 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100722 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200723 funcs->prepare(crtc);
724 else if (funcs->disable)
725 funcs->disable(crtc);
726 else
727 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
728 }
729}
730
Daniel Vetter4c18d302015-05-12 15:27:37 +0200731/**
732 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
733 * @dev: DRM device
734 * @old_state: atomic state object with old state structures
735 *
736 * This function updates all the various legacy modeset state pointers in
737 * connectors, encoders and crtcs. It also updates the timestamping constants
738 * used for precise vblank timestamps by calling
739 * drm_calc_timestamping_constants().
740 *
741 * Drivers can use this for building their own atomic commit if they don't have
742 * a pure helper-based modeset implementation.
743 */
744void
745drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
746 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200747{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300748 struct drm_connector *connector;
749 struct drm_connector_state *old_conn_state;
750 struct drm_crtc *crtc;
751 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200752 int i;
753
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200754 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300755 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200756 if (connector->encoder) {
757 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200758
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200759 connector->encoder->crtc = NULL;
760 connector->encoder = NULL;
761 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200762
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200763 crtc = connector->state->crtc;
764 if ((!crtc && old_conn_state->crtc) ||
765 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
766 struct drm_property *dpms_prop =
767 dev->mode_config.dpms_property;
768 int mode = DRM_MODE_DPMS_OFF;
769
770 if (crtc && crtc->state->active)
771 mode = DRM_MODE_DPMS_ON;
772
773 connector->dpms = mode;
774 drm_object_property_set_value(&connector->base,
775 dpms_prop, mode);
776 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200777 }
778
779 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300780 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
781 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200782 continue;
783
784 if (WARN_ON(!connector->state->best_encoder))
785 continue;
786
787 connector->encoder = connector->state->best_encoder;
788 connector->encoder->crtc = connector->state->crtc;
789 }
790
791 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300792 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200793 struct drm_plane *primary = crtc->primary;
794
Daniel Vetter623369e2014-09-16 17:50:47 +0200795 crtc->mode = crtc->state->mode;
796 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200797
798 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
799 primary->state->crtc == crtc) {
800 crtc->x = primary->state->src_x >> 16;
801 crtc->y = primary->state->src_y >> 16;
802 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200803
804 if (crtc->state->enable)
805 drm_calc_timestamping_constants(crtc,
806 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200807 }
808}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200809EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200810
811static void
812crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
813{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300814 struct drm_crtc *crtc;
815 struct drm_crtc_state *old_crtc_state;
816 struct drm_connector *connector;
817 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200818 int i;
819
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300820 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200821 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200822
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300823 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200824 continue;
825
826 funcs = crtc->helper_private;
827
Daniel Vetterc982bd92015-02-22 12:24:20 +0100828 if (crtc->state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200829 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
830 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100831
Daniel Vetter623369e2014-09-16 17:50:47 +0200832 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100833 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200834 }
835
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300836 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200837 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200838 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200839 struct drm_encoder *encoder;
840 struct drm_display_mode *mode, *adjusted_mode;
841
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300842 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200843 continue;
844
845 encoder = connector->state->best_encoder;
846 funcs = encoder->helper_private;
847 new_crtc_state = connector->state->crtc->state;
848 mode = &new_crtc_state->mode;
849 adjusted_mode = &new_crtc_state->adjusted_mode;
850
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100851 if (!new_crtc_state->mode_changed)
852 continue;
853
Daniel Vetter17a38d92015-02-22 12:24:16 +0100854 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
855 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100856
Daniel Vetter623369e2014-09-16 17:50:47 +0200857 /*
858 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800859 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200860 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100861 if (funcs->mode_set)
862 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200863
Archit Taneja862e6862015-05-21 11:03:16 +0530864 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200865 }
866}
867
868/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100869 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200870 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100871 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200872 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100873 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200874 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100875 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300876 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100877 * drm_atomic_helper_commit_planes(), which is what the default commit function
878 * does. But drivers with different needs can group the modeset commits together
879 * and do the plane commits at the end. This is useful for drivers doing runtime
880 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200881 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100882void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
883 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200884{
Laurent Pincharta072f802015-02-22 12:24:18 +0100885 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200886
887 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
888
Laurent Pincharta072f802015-02-22 12:24:18 +0100889 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200890}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100891EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200892
893/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100894 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200895 * @dev: DRM device
896 * @old_state: atomic state object with old state structures
897 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100898 * This function enables all the outputs with the new configuration which had to
899 * be turned off for the update.
900 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300901 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100902 * drm_atomic_helper_commit_planes(), which is what the default commit function
903 * does. But drivers with different needs can group the modeset commits together
904 * and do the plane commits at the end. This is useful for drivers doing runtime
905 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200906 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100907void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
908 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200909{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300910 struct drm_crtc *crtc;
911 struct drm_crtc_state *old_crtc_state;
912 struct drm_connector *connector;
913 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200914 int i;
915
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300916 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200917 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200918
919 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200920 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100921 continue;
922
923 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200924 continue;
925
926 funcs = crtc->helper_private;
927
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100928 if (crtc->state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200929 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
930 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100931
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100932 if (funcs->enable)
933 funcs->enable(crtc);
934 else
935 funcs->commit(crtc);
936 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200937 }
938
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300939 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200940 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200941 struct drm_encoder *encoder;
942
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300943 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200944 continue;
945
Daniel Vetter4218a322015-03-26 22:18:40 +0100946 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200947 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100948 continue;
949
Daniel Vetter623369e2014-09-16 17:50:47 +0200950 encoder = connector->state->best_encoder;
951 funcs = encoder->helper_private;
952
Daniel Vetter17a38d92015-02-22 12:24:16 +0100953 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
954 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100955
Daniel Vetter623369e2014-09-16 17:50:47 +0200956 /*
957 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800958 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200959 */
Archit Taneja862e6862015-05-21 11:03:16 +0530960 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200961
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100962 if (funcs->enable)
963 funcs->enable(encoder);
964 else
965 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200966
Archit Taneja862e6862015-05-21 11:03:16 +0530967 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200968 }
969}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100970EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200971
Daniel Vettere2330f02014-10-29 11:34:56 +0100972static void wait_for_fences(struct drm_device *dev,
973 struct drm_atomic_state *state)
974{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300975 struct drm_plane *plane;
976 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +0100977 int i;
978
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300979 for_each_plane_in_state(state, plane, plane_state, i) {
980 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +0100981 continue;
982
983 WARN_ON(!plane->state->fb);
984
985 fence_wait(plane->state->fence, false);
986 fence_put(plane->state->fence);
987 plane->state->fence = NULL;
988 }
989}
990
John Keepingc2409062016-01-19 10:46:58 +0000991/**
992 * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
993 * @dev: DRM device
994 * @old_state: atomic state object with old state structures
995 * @crtc: DRM crtc
996 *
997 * Checks whether the framebuffer used for this CRTC changes as a result of
998 * the atomic update. This is useful for drivers which cannot use
999 * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
1000 * functionality.
1001 *
1002 * Returns:
1003 * true if the framebuffer changed.
1004 */
1005bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
1006 struct drm_atomic_state *old_state,
1007 struct drm_crtc *crtc)
Daniel Vetterab58e332014-11-24 20:42:42 +01001008{
1009 struct drm_plane *plane;
1010 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +01001011 int i;
1012
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001013 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +01001014 if (plane->state->crtc != crtc &&
1015 old_plane_state->crtc != crtc)
1016 continue;
1017
1018 if (plane->state->fb != old_plane_state->fb)
1019 return true;
1020 }
1021
1022 return false;
1023}
John Keepingc2409062016-01-19 10:46:58 +00001024EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
Daniel Vetterab58e332014-11-24 20:42:42 +01001025
Rob Clark5ee32292014-11-11 19:38:59 -05001026/**
1027 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1028 * @dev: DRM device
1029 * @old_state: atomic state object with old state structures
1030 *
1031 * Helper to, after atomic commit, wait for vblanks on all effected
1032 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001033 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1034 * framebuffers have actually changed to optimize for the legacy cursor and
1035 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001036 */
1037void
1038drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1039 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001040{
1041 struct drm_crtc *crtc;
1042 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001043 int i, ret;
1044
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001045 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +02001046 /* No one cares about the old state, so abuse it for tracking
1047 * and store whether we hold a vblank reference (and should do a
1048 * vblank wait) in the ->enable boolean. */
1049 old_crtc_state->enable = false;
1050
1051 if (!crtc->state->enable)
1052 continue;
1053
Daniel Vetterf02ad902015-01-22 16:36:23 +01001054 /* Legacy cursor ioctls are completely unsynced, and userspace
1055 * relies on that (by doing tons of cursor updates). */
1056 if (old_state->legacy_cursor_update)
1057 continue;
1058
John Keepingc2409062016-01-19 10:46:58 +00001059 if (!drm_atomic_helper_framebuffer_changed(dev,
1060 old_state, crtc))
Daniel Vetterab58e332014-11-24 20:42:42 +01001061 continue;
1062
Daniel Vetter623369e2014-09-16 17:50:47 +02001063 ret = drm_crtc_vblank_get(crtc);
1064 if (ret != 0)
1065 continue;
1066
1067 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +02001068 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001069 }
1070
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001071 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1072 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +02001073 continue;
1074
1075 ret = wait_event_timeout(dev->vblank[i].queue,
1076 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001077 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001078 msecs_to_jiffies(50));
1079
1080 drm_crtc_vblank_put(crtc);
1081 }
1082}
Rob Clark5ee32292014-11-11 19:38:59 -05001083EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001084
1085/**
1086 * drm_atomic_helper_commit - commit validated state object
1087 * @dev: DRM device
1088 * @state: the driver state object
1089 * @async: asynchronous commit
1090 *
1091 * This function commits a with drm_atomic_helper_check() pre-validated state
1092 * object. This can still fail when e.g. the framebuffer reservation fails. For
1093 * now this doesn't implement asynchronous commits.
1094 *
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001095 * Note that right now this function does not support async commits, and hence
1096 * driver writers must implement their own version for now. Also note that the
1097 * default ordering of how the various stages are called is to match the legacy
1098 * modeset helper library closest. One peculiarity of that is that it doesn't
1099 * mesh well with runtime PM at all.
1100 *
1101 * For drivers supporting runtime PM the recommended sequence is
1102 *
1103 * drm_atomic_helper_commit_modeset_disables(dev, state);
1104 *
1105 * drm_atomic_helper_commit_modeset_enables(dev, state);
1106 *
1107 * drm_atomic_helper_commit_planes(dev, state, true);
1108 *
1109 * See the kerneldoc entries for these three functions for more details.
1110 *
Daniel Vetter623369e2014-09-16 17:50:47 +02001111 * RETURNS
1112 * Zero for success or -errno.
1113 */
1114int drm_atomic_helper_commit(struct drm_device *dev,
1115 struct drm_atomic_state *state,
1116 bool async)
1117{
1118 int ret;
1119
1120 if (async)
1121 return -EBUSY;
1122
1123 ret = drm_atomic_helper_prepare_planes(dev, state);
1124 if (ret)
1125 return ret;
1126
1127 /*
1128 * This is the point of no return - everything below never fails except
1129 * when the hw goes bonghits. Which means we can commit the new state on
1130 * the software side now.
1131 */
1132
1133 drm_atomic_helper_swap_state(dev, state);
1134
1135 /*
1136 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001137 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001138 * that the asynchronous work has either been cancelled (if the driver
1139 * supports it, which at least requires that the framebuffers get
1140 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1141 * before the new state gets committed on the software side with
1142 * drm_atomic_helper_swap_state().
1143 *
1144 * This scheme allows new atomic state updates to be prepared and
1145 * checked in parallel to the asynchronous completion of the previous
1146 * update. Which is important since compositors need to figure out the
1147 * composition of the next frame right after having submitted the
1148 * current layout.
1149 */
1150
Daniel Vettere2330f02014-10-29 11:34:56 +01001151 wait_for_fences(dev, state);
1152
Daniel Vetter1af434a2015-02-22 12:24:19 +01001153 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001154
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001155 drm_atomic_helper_commit_planes(dev, state, false);
Daniel Vetter623369e2014-09-16 17:50:47 +02001156
Daniel Vetter1af434a2015-02-22 12:24:19 +01001157 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001158
Rob Clark5ee32292014-11-11 19:38:59 -05001159 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001160
1161 drm_atomic_helper_cleanup_planes(dev, state);
1162
1163 drm_atomic_state_free(state);
1164
1165 return 0;
1166}
1167EXPORT_SYMBOL(drm_atomic_helper_commit);
1168
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001169/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001170 * DOC: implementing async commit
1171 *
1172 * For now the atomic helpers don't support async commit directly. If there is
1173 * real need it could be added though, using the dma-buf fence infrastructure
1174 * for generic synchronization with outstanding rendering.
1175 *
1176 * For now drivers have to implement async commit themselves, with the following
1177 * sequence being the recommended one:
1178 *
1179 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1180 * which commit needs to call which can fail, so we want to run it first and
1181 * synchronously.
1182 *
1183 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1184 * might be affected the new state update. This can be done by either cancelling
1185 * or flushing the work items, depending upon whether the driver can deal with
1186 * cancelled updates. Note that it is important to ensure that the framebuffer
1187 * cleanup is still done when cancelling.
1188 *
1189 * For sufficient parallelism it is recommended to have a work item per crtc
1190 * (for updates which don't touch global state) and a global one. Then we only
1191 * need to synchronize with the crtc work items for changed crtcs and the global
1192 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1193 *
1194 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001195 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001196 * locks means concurrent callers never see inconsistent state. And doing this
1197 * while it's guaranteed that no relevant async worker runs means that async
1198 * workers do not need grab any locks. Actually they must not grab locks, for
1199 * otherwise the work flushing will deadlock.
1200 *
1201 * 4. Schedule a work item to do all subsequent steps, using the split-out
1202 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1203 * then cleaning up the framebuffers after the old framebuffer is no longer
1204 * being displayed.
1205 */
1206
1207/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001208 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001209 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001210 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001211 *
1212 * This function prepares plane state, specifically framebuffers, for the new
1213 * configuration. If any failure is encountered this function will call
1214 * ->cleanup_fb on any already successfully prepared framebuffer.
1215 *
1216 * Returns:
1217 * 0 on success, negative error code on failure.
1218 */
1219int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1220 struct drm_atomic_state *state)
1221{
1222 int nplanes = dev->mode_config.num_total_plane;
1223 int ret, i;
1224
1225 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001226 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001227 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001228 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001229
1230 if (!plane)
1231 continue;
1232
1233 funcs = plane->helper_private;
1234
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001235 if (funcs->prepare_fb) {
1236 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001237 if (ret)
1238 goto fail;
1239 }
1240 }
1241
1242 return 0;
1243
1244fail:
1245 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001246 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001247 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001248 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001249
1250 if (!plane)
1251 continue;
1252
1253 funcs = plane->helper_private;
1254
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001255 if (funcs->cleanup_fb)
1256 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001257
1258 }
1259
1260 return ret;
1261}
1262EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1263
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001264bool plane_crtc_active(struct drm_plane_state *state)
1265{
1266 return state->crtc && state->crtc->state->active;
1267}
1268
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001269/**
1270 * drm_atomic_helper_commit_planes - commit plane state
1271 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001272 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001273 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001274 *
1275 * This function commits the new plane state using the plane and atomic helper
1276 * functions for planes and crtcs. It assumes that the atomic state has already
1277 * been pushed into the relevant object state pointers, since this step can no
1278 * longer fail.
1279 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001280 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001281 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001282 *
1283 * Note that this function does all plane updates across all CRTCs in one step.
1284 * If the hardware can't support this approach look at
1285 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001286 *
1287 * Plane parameters can be updated by applications while the associated CRTC is
1288 * disabled. The DRM/KMS core will store the parameters in the plane state,
1289 * which will be available to the driver when the CRTC is turned on. As a result
1290 * most drivers don't need to be immediately notified of plane updates for a
1291 * disabled CRTC.
1292 *
1293 * Unless otherwise needed, drivers are advised to set the @active_only
1294 * parameters to true in order not to receive plane update notifications related
1295 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1296 * driver code when the driver and/or hardware can't or just don't need to deal
1297 * with updates on disabled CRTCs, for example when supporting runtime PM.
1298 *
1299 * The drm_atomic_helper_commit() default implementation only sets @active_only
1300 * to false to most closely match the behaviour of the legacy helpers. This should
1301 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001302 */
1303void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001304 struct drm_atomic_state *old_state,
1305 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001306{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001307 struct drm_crtc *crtc;
1308 struct drm_crtc_state *old_crtc_state;
1309 struct drm_plane *plane;
1310 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001311 int i;
1312
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001313 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001314 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001315
1316 funcs = crtc->helper_private;
1317
1318 if (!funcs || !funcs->atomic_begin)
1319 continue;
1320
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001321 if (active_only && !crtc->state->active)
1322 continue;
1323
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001324 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001325 }
1326
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001327 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001328 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001329 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001330
1331 funcs = plane->helper_private;
1332
Thierry Reding3cad4b62014-11-25 13:05:12 +01001333 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001334 continue;
1335
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001336 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1337
1338 if (active_only) {
1339 /*
1340 * Skip planes related to inactive CRTCs. If the plane
1341 * is enabled use the state of the current CRTC. If the
1342 * plane is being disabled use the state of the old
1343 * CRTC to avoid skipping planes being disabled on an
1344 * active CRTC.
1345 */
1346 if (!disabling && !plane_crtc_active(plane->state))
1347 continue;
1348 if (disabling && !plane_crtc_active(old_plane_state))
1349 continue;
1350 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001351
Thierry Reding407b8bd2014-11-20 12:05:50 +01001352 /*
1353 * Special-case disabling the plane if drivers support it.
1354 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001355 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001356 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001357 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001358 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001359 }
1360
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001361 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001362 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001363
1364 funcs = crtc->helper_private;
1365
1366 if (!funcs || !funcs->atomic_flush)
1367 continue;
1368
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001369 if (active_only && !crtc->state->active)
1370 continue;
1371
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001372 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001373 }
1374}
1375EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1376
1377/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001378 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1379 * @old_crtc_state: atomic state object with the old crtc state
1380 *
1381 * This function commits the new plane state using the plane and atomic helper
1382 * functions for planes on the specific crtc. It assumes that the atomic state
1383 * has already been pushed into the relevant object state pointers, since this
1384 * step can no longer fail.
1385 *
1386 * This function is useful when plane updates should be done crtc-by-crtc
1387 * instead of one global step like drm_atomic_helper_commit_planes() does.
1388 *
1389 * This function can only be savely used when planes are not allowed to move
1390 * between different CRTCs because this function doesn't handle inter-CRTC
1391 * depencies. Callers need to ensure that either no such depencies exist,
1392 * resolve them through ordering of commit calls or through some other means.
1393 */
1394void
1395drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1396{
1397 const struct drm_crtc_helper_funcs *crtc_funcs;
1398 struct drm_crtc *crtc = old_crtc_state->crtc;
1399 struct drm_atomic_state *old_state = old_crtc_state->state;
1400 struct drm_plane *plane;
1401 unsigned plane_mask;
1402
1403 plane_mask = old_crtc_state->plane_mask;
1404 plane_mask |= crtc->state->plane_mask;
1405
1406 crtc_funcs = crtc->helper_private;
1407 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001408 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001409
1410 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1411 struct drm_plane_state *old_plane_state =
1412 drm_atomic_get_existing_plane_state(old_state, plane);
1413 const struct drm_plane_helper_funcs *plane_funcs;
1414
1415 plane_funcs = plane->helper_private;
1416
1417 if (!old_plane_state || !plane_funcs)
1418 continue;
1419
1420 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1421
1422 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1423 plane_funcs->atomic_disable)
1424 plane_funcs->atomic_disable(plane, old_plane_state);
1425 else if (plane->state->crtc ||
1426 drm_atomic_plane_disabling(plane, old_plane_state))
1427 plane_funcs->atomic_update(plane, old_plane_state);
1428 }
1429
1430 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001431 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001432}
1433EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1434
1435/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001436 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1437 * @crtc: CRTC
1438 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1439 *
1440 * Disables all planes associated with the given CRTC. This can be
1441 * used for instance in the CRTC helper disable callback to disable
1442 * all planes before shutting down the display pipeline.
1443 *
1444 * If the atomic-parameter is set the function calls the CRTC's
1445 * atomic_begin hook before and atomic_flush hook after disabling the
1446 * planes.
1447 *
1448 * It is a bug to call this function without having implemented the
1449 * ->atomic_disable() plane hook.
1450 */
1451void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1452 bool atomic)
1453{
1454 const struct drm_crtc_helper_funcs *crtc_funcs =
1455 crtc->helper_private;
1456 struct drm_plane *plane;
1457
1458 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1459 crtc_funcs->atomic_begin(crtc, NULL);
1460
1461 drm_for_each_plane(plane, crtc->dev) {
1462 const struct drm_plane_helper_funcs *plane_funcs =
1463 plane->helper_private;
1464
1465 if (plane->state->crtc != crtc || !plane_funcs)
1466 continue;
1467
1468 WARN_ON(!plane_funcs->atomic_disable);
1469 if (plane_funcs->atomic_disable)
1470 plane_funcs->atomic_disable(plane, NULL);
1471 }
1472
1473 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1474 crtc_funcs->atomic_flush(crtc, NULL);
1475}
1476EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1477
1478/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001479 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1480 * @dev: DRM device
1481 * @old_state: atomic state object with old state structures
1482 *
1483 * This function cleans up plane state, specifically framebuffers, from the old
1484 * configuration. Hence the old configuration must be perserved in @old_state to
1485 * be able to call this function.
1486 *
1487 * This function must also be called on the new state when the atomic update
1488 * fails at any point after calling drm_atomic_helper_prepare_planes().
1489 */
1490void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1491 struct drm_atomic_state *old_state)
1492{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001493 struct drm_plane *plane;
1494 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001495 int i;
1496
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001497 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001498 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001499
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001500 funcs = plane->helper_private;
1501
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001502 if (funcs->cleanup_fb)
1503 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001504 }
1505}
1506EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1507
1508/**
1509 * drm_atomic_helper_swap_state - store atomic state into current sw state
1510 * @dev: DRM device
1511 * @state: atomic state
1512 *
1513 * This function stores the atomic state into the current state pointers in all
1514 * driver objects. It should be called after all failing steps have been done
1515 * and succeeded, but before the actual hardware state is committed.
1516 *
1517 * For cleanup and error recovery the current state for all changed objects will
1518 * be swaped into @state.
1519 *
1520 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1521 *
1522 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1523 *
1524 * 2. Do any other steps that might fail.
1525 *
1526 * 3. Put the staged state into the current state pointers with this function.
1527 *
1528 * 4. Actually commit the hardware state.
1529 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001530 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001531 * contains the old state. Also do any other cleanup required with that state.
1532 */
1533void drm_atomic_helper_swap_state(struct drm_device *dev,
1534 struct drm_atomic_state *state)
1535{
1536 int i;
1537
1538 for (i = 0; i < dev->mode_config.num_connector; i++) {
1539 struct drm_connector *connector = state->connectors[i];
1540
1541 if (!connector)
1542 continue;
1543
1544 connector->state->state = state;
1545 swap(state->connector_states[i], connector->state);
1546 connector->state->state = NULL;
1547 }
1548
1549 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1550 struct drm_crtc *crtc = state->crtcs[i];
1551
1552 if (!crtc)
1553 continue;
1554
1555 crtc->state->state = state;
1556 swap(state->crtc_states[i], crtc->state);
1557 crtc->state->state = NULL;
1558 }
1559
1560 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1561 struct drm_plane *plane = state->planes[i];
1562
1563 if (!plane)
1564 continue;
1565
1566 plane->state->state = state;
1567 swap(state->plane_states[i], plane->state);
1568 plane->state->state = NULL;
1569 }
1570}
1571EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001572
1573/**
1574 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1575 * @plane: plane object to update
1576 * @crtc: owning CRTC of owning plane
1577 * @fb: framebuffer to flip onto plane
1578 * @crtc_x: x offset of primary plane on crtc
1579 * @crtc_y: y offset of primary plane on crtc
1580 * @crtc_w: width of primary plane rectangle on crtc
1581 * @crtc_h: height of primary plane rectangle on crtc
1582 * @src_x: x offset of @fb for panning
1583 * @src_y: y offset of @fb for panning
1584 * @src_w: width of source rectangle in @fb
1585 * @src_h: height of source rectangle in @fb
1586 *
1587 * Provides a default plane update handler using the atomic driver interface.
1588 *
1589 * RETURNS:
1590 * Zero on success, error code on failure
1591 */
1592int drm_atomic_helper_update_plane(struct drm_plane *plane,
1593 struct drm_crtc *crtc,
1594 struct drm_framebuffer *fb,
1595 int crtc_x, int crtc_y,
1596 unsigned int crtc_w, unsigned int crtc_h,
1597 uint32_t src_x, uint32_t src_y,
1598 uint32_t src_w, uint32_t src_h)
1599{
1600 struct drm_atomic_state *state;
1601 struct drm_plane_state *plane_state;
1602 int ret = 0;
1603
1604 state = drm_atomic_state_alloc(plane->dev);
1605 if (!state)
1606 return -ENOMEM;
1607
1608 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1609retry:
1610 plane_state = drm_atomic_get_plane_state(state, plane);
1611 if (IS_ERR(plane_state)) {
1612 ret = PTR_ERR(plane_state);
1613 goto fail;
1614 }
1615
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001616 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001617 if (ret != 0)
1618 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001619 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001620 plane_state->crtc_x = crtc_x;
1621 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001622 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001623 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001624 plane_state->src_x = src_x;
1625 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001626 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001627 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001628
Daniel Vetter3671c582015-05-04 15:40:52 +02001629 if (plane == crtc->cursor)
1630 state->legacy_cursor_update = true;
1631
Daniel Vetter042652e2014-07-27 13:46:52 +02001632 ret = drm_atomic_commit(state);
1633 if (ret != 0)
1634 goto fail;
1635
1636 /* Driver takes ownership of state on successful commit. */
1637 return 0;
1638fail:
1639 if (ret == -EDEADLK)
1640 goto backoff;
1641
1642 drm_atomic_state_free(state);
1643
1644 return ret;
1645backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001646 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001647 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001648
1649 /*
1650 * Someone might have exchanged the framebuffer while we dropped locks
1651 * in the backoff code. We need to fix up the fb refcount tracking the
1652 * core does for us.
1653 */
1654 plane->old_fb = plane->fb;
1655
1656 goto retry;
1657}
1658EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1659
1660/**
1661 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1662 * @plane: plane to disable
1663 *
1664 * Provides a default plane disable handler using the atomic driver interface.
1665 *
1666 * RETURNS:
1667 * Zero on success, error code on failure
1668 */
1669int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1670{
1671 struct drm_atomic_state *state;
1672 struct drm_plane_state *plane_state;
1673 int ret = 0;
1674
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001675 /*
1676 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1677 * acquire context. The real fix will be to wire the acquire ctx through
1678 * everywhere we need it, but meanwhile prevent chaos by just skipping
1679 * this noop. The critical case is the cursor ioctls which a) only grab
1680 * crtc/cursor-plane locks (so we need the crtc to get at the right
1681 * acquire context) and b) can try to disable the plane multiple times.
1682 */
1683 if (!plane->crtc)
1684 return 0;
1685
Daniel Vetter042652e2014-07-27 13:46:52 +02001686 state = drm_atomic_state_alloc(plane->dev);
1687 if (!state)
1688 return -ENOMEM;
1689
1690 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1691retry:
1692 plane_state = drm_atomic_get_plane_state(state, plane);
1693 if (IS_ERR(plane_state)) {
1694 ret = PTR_ERR(plane_state);
1695 goto fail;
1696 }
1697
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01001698 if (plane_state->crtc && (plane == plane->crtc->cursor))
1699 plane_state->state->legacy_cursor_update = true;
1700
Rob Clarkbbb1e522015-08-25 15:35:58 -04001701 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001702 if (ret != 0)
1703 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01001704
Daniel Vetter042652e2014-07-27 13:46:52 +02001705 ret = drm_atomic_commit(state);
1706 if (ret != 0)
1707 goto fail;
1708
1709 /* Driver takes ownership of state on successful commit. */
1710 return 0;
1711fail:
1712 if (ret == -EDEADLK)
1713 goto backoff;
1714
1715 drm_atomic_state_free(state);
1716
1717 return ret;
1718backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001719 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001720 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001721
1722 /*
1723 * Someone might have exchanged the framebuffer while we dropped locks
1724 * in the backoff code. We need to fix up the fb refcount tracking the
1725 * core does for us.
1726 */
1727 plane->old_fb = plane->fb;
1728
1729 goto retry;
1730}
1731EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1732
Rob Clarkbbb1e522015-08-25 15:35:58 -04001733/* just used from fb-helper and atomic-helper: */
1734int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1735 struct drm_plane_state *plane_state)
1736{
1737 int ret;
1738
1739 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1740 if (ret != 0)
1741 return ret;
1742
1743 drm_atomic_set_fb_for_plane(plane_state, NULL);
1744 plane_state->crtc_x = 0;
1745 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001746 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001747 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001748 plane_state->src_x = 0;
1749 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001750 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001751 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001752
Rob Clarkbbb1e522015-08-25 15:35:58 -04001753 return 0;
1754}
1755
Daniel Vetter042652e2014-07-27 13:46:52 +02001756static int update_output_state(struct drm_atomic_state *state,
1757 struct drm_mode_set *set)
1758{
1759 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001760 struct drm_crtc *crtc;
1761 struct drm_crtc_state *crtc_state;
1762 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001763 struct drm_connector_state *conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001764 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02001765
1766 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1767 state->acquire_ctx);
1768 if (ret)
1769 return ret;
1770
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001771 /* First disable all connectors on the target crtc. */
1772 ret = drm_atomic_add_affected_connectors(state, set->crtc);
1773 if (ret)
1774 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02001775
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001776 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001777 if (conn_state->crtc == set->crtc) {
1778 ret = drm_atomic_set_crtc_for_connector(conn_state,
1779 NULL);
1780 if (ret)
1781 return ret;
1782 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001783 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001784
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001785 /* Then set all connectors from set->connectors on the target crtc */
1786 for (i = 0; i < set->num_connectors; i++) {
1787 conn_state = drm_atomic_get_connector_state(state,
1788 set->connectors[i]);
1789 if (IS_ERR(conn_state))
1790 return PTR_ERR(conn_state);
1791
1792 ret = drm_atomic_set_crtc_for_connector(conn_state,
1793 set->crtc);
1794 if (ret)
1795 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02001796 }
1797
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001798 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001799 /* Don't update ->enable for the CRTC in the set_config request,
1800 * since a mismatch would indicate a bug in the upper layers.
1801 * The actual modeset code later on will catch any
1802 * inconsistencies here. */
1803 if (crtc == set->crtc)
1804 continue;
1805
Maarten Lankhorst14de6c42016-01-04 12:53:20 +01001806 if (!crtc_state->connector_mask) {
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001807 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1808 NULL);
1809 if (ret < 0)
1810 return ret;
1811
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001812 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001813 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001814 }
1815
1816 return 0;
1817}
1818
1819/**
1820 * drm_atomic_helper_set_config - set a new config from userspace
1821 * @set: mode set configuration
1822 *
1823 * Provides a default crtc set_config handler using the atomic driver interface.
1824 *
1825 * Returns:
1826 * Returns 0 on success, negative errno numbers on failure.
1827 */
1828int drm_atomic_helper_set_config(struct drm_mode_set *set)
1829{
1830 struct drm_atomic_state *state;
1831 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02001832 int ret = 0;
1833
1834 state = drm_atomic_state_alloc(crtc->dev);
1835 if (!state)
1836 return -ENOMEM;
1837
1838 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1839retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04001840 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01001841 if (ret != 0)
1842 goto fail;
1843
Daniel Vetter042652e2014-07-27 13:46:52 +02001844 ret = drm_atomic_commit(state);
1845 if (ret != 0)
1846 goto fail;
1847
1848 /* Driver takes ownership of state on successful commit. */
1849 return 0;
1850fail:
1851 if (ret == -EDEADLK)
1852 goto backoff;
1853
1854 drm_atomic_state_free(state);
1855
1856 return ret;
1857backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001858 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001859 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001860
1861 /*
1862 * Someone might have exchanged the framebuffer while we dropped locks
1863 * in the backoff code. We need to fix up the fb refcount tracking the
1864 * core does for us.
1865 */
1866 crtc->primary->old_fb = crtc->primary->fb;
1867
1868 goto retry;
1869}
1870EXPORT_SYMBOL(drm_atomic_helper_set_config);
1871
Rob Clarkbbb1e522015-08-25 15:35:58 -04001872/* just used from fb-helper and atomic-helper: */
1873int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1874 struct drm_atomic_state *state)
1875{
1876 struct drm_crtc_state *crtc_state;
1877 struct drm_plane_state *primary_state;
1878 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02001879 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001880 int ret;
1881
1882 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1883 if (IS_ERR(crtc_state))
1884 return PTR_ERR(crtc_state);
1885
1886 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1887 if (IS_ERR(primary_state))
1888 return PTR_ERR(primary_state);
1889
1890 if (!set->mode) {
1891 WARN_ON(set->fb);
1892 WARN_ON(set->num_connectors);
1893
1894 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1895 if (ret != 0)
1896 return ret;
1897
1898 crtc_state->active = false;
1899
1900 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1901 if (ret != 0)
1902 return ret;
1903
1904 drm_atomic_set_fb_for_plane(primary_state, NULL);
1905
1906 goto commit;
1907 }
1908
1909 WARN_ON(!set->fb);
1910 WARN_ON(!set->num_connectors);
1911
1912 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1913 if (ret != 0)
1914 return ret;
1915
1916 crtc_state->active = true;
1917
1918 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1919 if (ret != 0)
1920 return ret;
1921
Ville Syrjälä83926112015-11-16 17:02:34 +02001922 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1923
Rob Clarkbbb1e522015-08-25 15:35:58 -04001924 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1925 primary_state->crtc_x = 0;
1926 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02001927 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001928 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001929 primary_state->src_x = set->x << 16;
1930 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001931 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02001932 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001933 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001934 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02001935 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001936 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001937 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04001938
1939commit:
1940 ret = update_output_state(state, set);
1941 if (ret)
1942 return ret;
1943
1944 return 0;
1945}
1946
Daniel Vetter042652e2014-07-27 13:46:52 +02001947/**
Thierry Reding14942762015-12-02 17:50:04 +01001948 * drm_atomic_helper_disable_all - disable all currently active outputs
1949 * @dev: DRM device
1950 * @ctx: lock acquisition context
1951 *
1952 * Loops through all connectors, finding those that aren't turned off and then
1953 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1954 * that they are connected to.
1955 *
1956 * This is used for example in suspend/resume to disable all currently active
1957 * functions when suspending.
1958 *
1959 * Note that if callers haven't already acquired all modeset locks this might
1960 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1961 *
1962 * Returns:
1963 * 0 on success or a negative error code on failure.
1964 *
1965 * See also:
1966 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1967 */
1968int drm_atomic_helper_disable_all(struct drm_device *dev,
1969 struct drm_modeset_acquire_ctx *ctx)
1970{
1971 struct drm_atomic_state *state;
1972 struct drm_connector *conn;
1973 int err;
1974
1975 state = drm_atomic_state_alloc(dev);
1976 if (!state)
1977 return -ENOMEM;
1978
1979 state->acquire_ctx = ctx;
1980
1981 drm_for_each_connector(conn, dev) {
1982 struct drm_crtc *crtc = conn->state->crtc;
1983 struct drm_crtc_state *crtc_state;
1984
1985 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1986 continue;
1987
1988 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1989 if (IS_ERR(crtc_state)) {
1990 err = PTR_ERR(crtc_state);
1991 goto free;
1992 }
1993
1994 crtc_state->active = false;
1995 }
1996
1997 err = drm_atomic_commit(state);
1998
1999free:
2000 if (err < 0)
2001 drm_atomic_state_free(state);
2002
2003 return err;
2004}
2005EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2006
2007/**
2008 * drm_atomic_helper_suspend - subsystem-level suspend helper
2009 * @dev: DRM device
2010 *
2011 * Duplicates the current atomic state, disables all active outputs and then
2012 * returns a pointer to the original atomic state to the caller. Drivers can
2013 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2014 * restore the output configuration that was active at the time the system
2015 * entered suspend.
2016 *
2017 * Note that it is potentially unsafe to use this. The atomic state object
2018 * returned by this function is assumed to be persistent. Drivers must ensure
2019 * that this holds true. Before calling this function, drivers must make sure
2020 * to suspend fbdev emulation so that nothing can be using the device.
2021 *
2022 * Returns:
2023 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2024 * encoded error code on failure. Drivers should store the returned atomic
2025 * state object and pass it to the drm_atomic_helper_resume() helper upon
2026 * resume.
2027 *
2028 * See also:
2029 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2030 * drm_atomic_helper_resume()
2031 */
2032struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2033{
2034 struct drm_modeset_acquire_ctx ctx;
2035 struct drm_atomic_state *state;
2036 int err;
2037
2038 drm_modeset_acquire_init(&ctx, 0);
2039
2040retry:
2041 err = drm_modeset_lock_all_ctx(dev, &ctx);
2042 if (err < 0) {
2043 state = ERR_PTR(err);
2044 goto unlock;
2045 }
2046
2047 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2048 if (IS_ERR(state))
2049 goto unlock;
2050
2051 err = drm_atomic_helper_disable_all(dev, &ctx);
2052 if (err < 0) {
2053 drm_atomic_state_free(state);
2054 state = ERR_PTR(err);
2055 goto unlock;
2056 }
2057
2058unlock:
2059 if (PTR_ERR(state) == -EDEADLK) {
2060 drm_modeset_backoff(&ctx);
2061 goto retry;
2062 }
2063
2064 drm_modeset_drop_locks(&ctx);
2065 drm_modeset_acquire_fini(&ctx);
2066 return state;
2067}
2068EXPORT_SYMBOL(drm_atomic_helper_suspend);
2069
2070/**
2071 * drm_atomic_helper_resume - subsystem-level resume helper
2072 * @dev: DRM device
2073 * @state: atomic state to resume to
2074 *
2075 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2076 * grabs all modeset locks and commits the atomic state object. This can be
2077 * used in conjunction with the drm_atomic_helper_suspend() helper to
2078 * implement suspend/resume for drivers that support atomic mode-setting.
2079 *
2080 * Returns:
2081 * 0 on success or a negative error code on failure.
2082 *
2083 * See also:
2084 * drm_atomic_helper_suspend()
2085 */
2086int drm_atomic_helper_resume(struct drm_device *dev,
2087 struct drm_atomic_state *state)
2088{
2089 struct drm_mode_config *config = &dev->mode_config;
2090 int err;
2091
2092 drm_mode_config_reset(dev);
2093 drm_modeset_lock_all(dev);
2094 state->acquire_ctx = config->acquire_ctx;
2095 err = drm_atomic_commit(state);
2096 drm_modeset_unlock_all(dev);
2097
2098 return err;
2099}
2100EXPORT_SYMBOL(drm_atomic_helper_resume);
2101
2102/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002103 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002104 * @crtc: DRM crtc
2105 * @property: DRM property
2106 * @val: value of property
2107 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002108 * Provides a default crtc set_property handler using the atomic driver
2109 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002110 *
2111 * RETURNS:
2112 * Zero on success, error code on failure
2113 */
2114int
2115drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2116 struct drm_property *property,
2117 uint64_t val)
2118{
2119 struct drm_atomic_state *state;
2120 struct drm_crtc_state *crtc_state;
2121 int ret = 0;
2122
2123 state = drm_atomic_state_alloc(crtc->dev);
2124 if (!state)
2125 return -ENOMEM;
2126
2127 /* ->set_property is always called with all locks held. */
2128 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2129retry:
2130 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2131 if (IS_ERR(crtc_state)) {
2132 ret = PTR_ERR(crtc_state);
2133 goto fail;
2134 }
2135
Rob Clark40ecc692014-12-18 16:01:46 -05002136 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2137 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002138 if (ret)
2139 goto fail;
2140
2141 ret = drm_atomic_commit(state);
2142 if (ret != 0)
2143 goto fail;
2144
2145 /* Driver takes ownership of state on successful commit. */
2146 return 0;
2147fail:
2148 if (ret == -EDEADLK)
2149 goto backoff;
2150
2151 drm_atomic_state_free(state);
2152
2153 return ret;
2154backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002155 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002156 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002157
2158 goto retry;
2159}
2160EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2161
2162/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002163 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002164 * @plane: DRM plane
2165 * @property: DRM property
2166 * @val: value of property
2167 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002168 * Provides a default plane set_property handler using the atomic driver
2169 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002170 *
2171 * RETURNS:
2172 * Zero on success, error code on failure
2173 */
2174int
2175drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2176 struct drm_property *property,
2177 uint64_t val)
2178{
2179 struct drm_atomic_state *state;
2180 struct drm_plane_state *plane_state;
2181 int ret = 0;
2182
2183 state = drm_atomic_state_alloc(plane->dev);
2184 if (!state)
2185 return -ENOMEM;
2186
2187 /* ->set_property is always called with all locks held. */
2188 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2189retry:
2190 plane_state = drm_atomic_get_plane_state(state, plane);
2191 if (IS_ERR(plane_state)) {
2192 ret = PTR_ERR(plane_state);
2193 goto fail;
2194 }
2195
Rob Clark40ecc692014-12-18 16:01:46 -05002196 ret = drm_atomic_plane_set_property(plane, plane_state,
2197 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002198 if (ret)
2199 goto fail;
2200
2201 ret = drm_atomic_commit(state);
2202 if (ret != 0)
2203 goto fail;
2204
2205 /* Driver takes ownership of state on successful commit. */
2206 return 0;
2207fail:
2208 if (ret == -EDEADLK)
2209 goto backoff;
2210
2211 drm_atomic_state_free(state);
2212
2213 return ret;
2214backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002215 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002216 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002217
2218 goto retry;
2219}
2220EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2221
2222/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002223 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002224 * @connector: DRM connector
2225 * @property: DRM property
2226 * @val: value of property
2227 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002228 * Provides a default connector set_property handler using the atomic driver
2229 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002230 *
2231 * RETURNS:
2232 * Zero on success, error code on failure
2233 */
2234int
2235drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2236 struct drm_property *property,
2237 uint64_t val)
2238{
2239 struct drm_atomic_state *state;
2240 struct drm_connector_state *connector_state;
2241 int ret = 0;
2242
2243 state = drm_atomic_state_alloc(connector->dev);
2244 if (!state)
2245 return -ENOMEM;
2246
2247 /* ->set_property is always called with all locks held. */
2248 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2249retry:
2250 connector_state = drm_atomic_get_connector_state(state, connector);
2251 if (IS_ERR(connector_state)) {
2252 ret = PTR_ERR(connector_state);
2253 goto fail;
2254 }
2255
Rob Clark40ecc692014-12-18 16:01:46 -05002256 ret = drm_atomic_connector_set_property(connector, connector_state,
2257 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002258 if (ret)
2259 goto fail;
2260
2261 ret = drm_atomic_commit(state);
2262 if (ret != 0)
2263 goto fail;
2264
2265 /* Driver takes ownership of state on successful commit. */
2266 return 0;
2267fail:
2268 if (ret == -EDEADLK)
2269 goto backoff;
2270
2271 drm_atomic_state_free(state);
2272
2273 return ret;
2274backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002275 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002276 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002277
2278 goto retry;
2279}
2280EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002281
2282/**
2283 * drm_atomic_helper_page_flip - execute a legacy page flip
2284 * @crtc: DRM crtc
2285 * @fb: DRM framebuffer
2286 * @event: optional DRM event to signal upon completion
2287 * @flags: flip flags for non-vblank sync'ed updates
2288 *
2289 * Provides a default page flip implementation using the atomic driver interface.
2290 *
2291 * Note that for now so called async page flips (i.e. updates which are not
2292 * synchronized to vblank) are not supported, since the atomic interfaces have
2293 * no provisions for this yet.
2294 *
2295 * Returns:
2296 * Returns 0 on success, negative errno numbers on failure.
2297 */
2298int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2299 struct drm_framebuffer *fb,
2300 struct drm_pending_vblank_event *event,
2301 uint32_t flags)
2302{
2303 struct drm_plane *plane = crtc->primary;
2304 struct drm_atomic_state *state;
2305 struct drm_plane_state *plane_state;
2306 struct drm_crtc_state *crtc_state;
2307 int ret = 0;
2308
2309 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2310 return -EINVAL;
2311
2312 state = drm_atomic_state_alloc(plane->dev);
2313 if (!state)
2314 return -ENOMEM;
2315
2316 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2317retry:
2318 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2319 if (IS_ERR(crtc_state)) {
2320 ret = PTR_ERR(crtc_state);
2321 goto fail;
2322 }
2323 crtc_state->event = event;
2324
2325 plane_state = drm_atomic_get_plane_state(state, plane);
2326 if (IS_ERR(plane_state)) {
2327 ret = PTR_ERR(plane_state);
2328 goto fail;
2329 }
2330
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002331 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002332 if (ret != 0)
2333 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002334 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002335
Daniel Vetter4cba6852015-12-08 09:49:20 +01002336 /* Make sure we don't accidentally do a full modeset. */
2337 state->allow_modeset = false;
2338 if (!crtc_state->active) {
2339 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2340 crtc->base.id);
2341 ret = -EINVAL;
2342 goto fail;
2343 }
2344
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002345 ret = drm_atomic_async_commit(state);
2346 if (ret != 0)
2347 goto fail;
2348
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002349 /* Driver takes ownership of state on successful async commit. */
2350 return 0;
2351fail:
2352 if (ret == -EDEADLK)
2353 goto backoff;
2354
2355 drm_atomic_state_free(state);
2356
2357 return ret;
2358backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002359 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002360 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002361
2362 /*
2363 * Someone might have exchanged the framebuffer while we dropped locks
2364 * in the backoff code. We need to fix up the fb refcount tracking the
2365 * core does for us.
2366 */
2367 plane->old_fb = plane->fb;
2368
2369 goto retry;
2370}
2371EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002372
2373/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002374 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2375 * @connector: affected connector
2376 * @mode: DPMS mode
2377 *
2378 * This is the main helper function provided by the atomic helper framework for
2379 * implementing the legacy DPMS connector interface. It computes the new desired
2380 * ->active state for the corresponding CRTC (if the connector is enabled) and
2381 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002382 *
2383 * Returns:
2384 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002385 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002386int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2387 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002388{
2389 struct drm_mode_config *config = &connector->dev->mode_config;
2390 struct drm_atomic_state *state;
2391 struct drm_crtc_state *crtc_state;
2392 struct drm_crtc *crtc;
2393 struct drm_connector *tmp_connector;
2394 int ret;
2395 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002396 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002397
2398 if (mode != DRM_MODE_DPMS_ON)
2399 mode = DRM_MODE_DPMS_OFF;
2400
2401 connector->dpms = mode;
2402 crtc = connector->state->crtc;
2403
2404 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002405 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002406
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002407 state = drm_atomic_state_alloc(connector->dev);
2408 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002409 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002410
2411 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2412retry:
2413 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002414 if (IS_ERR(crtc_state)) {
2415 ret = PTR_ERR(crtc_state);
2416 goto fail;
2417 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002418
2419 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2420
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002421 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002422 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002423 continue;
2424
John Hunter0388df02015-03-17 15:30:28 +08002425 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002426 active = true;
2427 break;
2428 }
2429 }
2430 crtc_state->active = active;
2431
2432 ret = drm_atomic_commit(state);
2433 if (ret != 0)
2434 goto fail;
2435
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002436 /* Driver takes ownership of state on successful commit. */
2437 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002438fail:
2439 if (ret == -EDEADLK)
2440 goto backoff;
2441
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002442 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002443 drm_atomic_state_free(state);
2444
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002445 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002446backoff:
2447 drm_atomic_state_clear(state);
2448 drm_atomic_legacy_backoff(state);
2449
2450 goto retry;
2451}
2452EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2453
2454/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002455 * DOC: atomic state reset and initialization
2456 *
2457 * Both the drm core and the atomic helpers assume that there is always the full
2458 * and correct atomic software state for all connectors, CRTCs and planes
2459 * available. Which is a bit a problem on driver load and also after system
2460 * suspend. One way to solve this is to have a hardware state read-out
2461 * infrastructure which reconstructs the full software state (e.g. the i915
2462 * driver).
2463 *
2464 * The simpler solution is to just reset the software state to everything off,
2465 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2466 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01002467 *
2468 * On the upside the precise state tracking of atomic simplifies system suspend
2469 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2470 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2471 * For other drivers the building blocks are split out, see the documentation
2472 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002473 */
2474
2475/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002476 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2477 * @crtc: drm CRTC
2478 *
2479 * Resets the atomic state for @crtc by freeing the state pointer (which might
2480 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2481 */
2482void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2483{
Markus Elfring5f911902015-11-06 12:03:46 +01002484 if (crtc->state)
Daniel Stone99cf4a22015-05-25 19:11:51 +01002485 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002486 kfree(crtc->state);
2487 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002488
2489 if (crtc->state)
2490 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002491}
2492EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2493
2494/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002495 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2496 * @crtc: CRTC object
2497 * @state: atomic CRTC state
2498 *
2499 * Copies atomic state from a CRTC's current state and resets inferred values.
2500 * This is useful for drivers that subclass the CRTC state.
2501 */
2502void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2503 struct drm_crtc_state *state)
2504{
2505 memcpy(state, crtc->state, sizeof(*state));
2506
Daniel Stone99cf4a22015-05-25 19:11:51 +01002507 if (state->mode_blob)
2508 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002509 state->mode_changed = false;
2510 state->active_changed = false;
2511 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002512 state->connectors_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002513 state->event = NULL;
2514}
2515EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2516
2517/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002518 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2519 * @crtc: drm CRTC
2520 *
2521 * Default CRTC state duplicate hook for drivers which don't have their own
2522 * subclassed CRTC state structure.
2523 */
2524struct drm_crtc_state *
2525drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2526{
2527 struct drm_crtc_state *state;
2528
2529 if (WARN_ON(!crtc->state))
2530 return NULL;
2531
Thierry Redingf5e78402015-01-28 14:54:32 +01002532 state = kmalloc(sizeof(*state), GFP_KERNEL);
2533 if (state)
2534 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002535
2536 return state;
2537}
2538EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2539
2540/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002541 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2542 * @crtc: CRTC object
2543 * @state: CRTC state object to release
2544 *
2545 * Releases all resources stored in the CRTC state without actually freeing
2546 * the memory of the CRTC state. This is useful for drivers that subclass the
2547 * CRTC state.
2548 */
2549void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2550 struct drm_crtc_state *state)
2551{
Markus Elfring5f911902015-11-06 12:03:46 +01002552 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002553}
2554EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2555
2556/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002557 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2558 * @crtc: drm CRTC
2559 * @state: CRTC state object to release
2560 *
2561 * Default CRTC state destroy hook for drivers which don't have their own
2562 * subclassed CRTC state structure.
2563 */
2564void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2565 struct drm_crtc_state *state)
2566{
Thierry Redingf5e78402015-01-28 14:54:32 +01002567 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002568 kfree(state);
2569}
2570EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2571
2572/**
2573 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2574 * @plane: drm plane
2575 *
2576 * Resets the atomic state for @plane by freeing the state pointer (which might
2577 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2578 */
2579void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2580{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002581 if (plane->state && plane->state->fb)
2582 drm_framebuffer_unreference(plane->state->fb);
2583
Daniel Vetterd4617012014-11-03 15:56:43 +01002584 kfree(plane->state);
2585 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002586
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01002587 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002588 plane->state->plane = plane;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01002589 plane->state->rotation = BIT(DRM_ROTATE_0);
2590 }
Daniel Vetterd4617012014-11-03 15:56:43 +01002591}
2592EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2593
2594/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002595 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2596 * @plane: plane object
2597 * @state: atomic plane state
2598 *
2599 * Copies atomic state from a plane's current state. This is useful for
2600 * drivers that subclass the plane state.
2601 */
2602void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2603 struct drm_plane_state *state)
2604{
2605 memcpy(state, plane->state, sizeof(*state));
2606
2607 if (state->fb)
2608 drm_framebuffer_reference(state->fb);
2609}
2610EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2611
2612/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002613 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2614 * @plane: drm plane
2615 *
2616 * Default plane state duplicate hook for drivers which don't have their own
2617 * subclassed plane state structure.
2618 */
2619struct drm_plane_state *
2620drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2621{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002622 struct drm_plane_state *state;
2623
Daniel Vetterd4617012014-11-03 15:56:43 +01002624 if (WARN_ON(!plane->state))
2625 return NULL;
2626
Thierry Redingf5e78402015-01-28 14:54:32 +01002627 state = kmalloc(sizeof(*state), GFP_KERNEL);
2628 if (state)
2629 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002630
2631 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002632}
2633EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2634
2635/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002636 * __drm_atomic_helper_plane_destroy_state - release plane state
2637 * @plane: plane object
2638 * @state: plane state object to release
2639 *
2640 * Releases all resources stored in the plane state without actually freeing
2641 * the memory of the plane state. This is useful for drivers that subclass the
2642 * plane state.
2643 */
2644void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2645 struct drm_plane_state *state)
2646{
2647 if (state->fb)
2648 drm_framebuffer_unreference(state->fb);
2649}
2650EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2651
2652/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002653 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2654 * @plane: drm plane
2655 * @state: plane state object to release
2656 *
2657 * Default plane state destroy hook for drivers which don't have their own
2658 * subclassed plane state structure.
2659 */
2660void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002661 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002662{
Thierry Redingf5e78402015-01-28 14:54:32 +01002663 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002664 kfree(state);
2665}
2666EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2667
2668/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002669 * __drm_atomic_helper_connector_reset - reset state on connector
2670 * @connector: drm connector
2671 * @conn_state: connector state to assign
2672 *
2673 * Initializes the newly allocated @conn_state and assigns it to
2674 * #connector ->state, usually required when initializing the drivers
2675 * or when called from the ->reset hook.
2676 *
2677 * This is useful for drivers that subclass the connector state.
2678 */
2679void
2680__drm_atomic_helper_connector_reset(struct drm_connector *connector,
2681 struct drm_connector_state *conn_state)
2682{
2683 if (conn_state)
2684 conn_state->connector = connector;
2685
2686 connector->state = conn_state;
2687}
2688EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
2689
2690/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002691 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2692 * @connector: drm connector
2693 *
2694 * Resets the atomic state for @connector by freeing the state pointer (which
2695 * might be NULL, e.g. at driver load time) and allocating a new empty state
2696 * object.
2697 */
2698void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2699{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002700 struct drm_connector_state *conn_state =
2701 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002702
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002703 kfree(connector->state);
2704 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002705}
2706EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2707
2708/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002709 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2710 * @connector: connector object
2711 * @state: atomic connector state
2712 *
2713 * Copies atomic state from a connector's current state. This is useful for
2714 * drivers that subclass the connector state.
2715 */
2716void
2717__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2718 struct drm_connector_state *state)
2719{
2720 memcpy(state, connector->state, sizeof(*state));
2721}
2722EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2723
2724/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002725 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2726 * @connector: drm connector
2727 *
2728 * Default connector state duplicate hook for drivers which don't have their own
2729 * subclassed connector state structure.
2730 */
2731struct drm_connector_state *
2732drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2733{
Thierry Redingf5e78402015-01-28 14:54:32 +01002734 struct drm_connector_state *state;
2735
Daniel Vetterd4617012014-11-03 15:56:43 +01002736 if (WARN_ON(!connector->state))
2737 return NULL;
2738
Thierry Redingf5e78402015-01-28 14:54:32 +01002739 state = kmalloc(sizeof(*state), GFP_KERNEL);
2740 if (state)
2741 __drm_atomic_helper_connector_duplicate_state(connector, state);
2742
2743 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002744}
2745EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2746
2747/**
Thierry Reding397fd772015-09-08 15:00:45 +02002748 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2749 * @dev: DRM device
2750 * @ctx: lock acquisition context
2751 *
2752 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01002753 * duplicating their respective states. This is used for example by suspend/
2754 * resume support code to save the state prior to suspend such that it can
2755 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02002756 *
2757 * Note that this treats atomic state as persistent between save and restore.
2758 * Drivers must make sure that this is possible and won't result in confusion
2759 * or erroneous behaviour.
2760 *
2761 * Note that if callers haven't already acquired all modeset locks this might
2762 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2763 *
2764 * Returns:
2765 * A pointer to the copy of the atomic state object on success or an
2766 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01002767 *
2768 * See also:
2769 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02002770 */
2771struct drm_atomic_state *
2772drm_atomic_helper_duplicate_state(struct drm_device *dev,
2773 struct drm_modeset_acquire_ctx *ctx)
2774{
2775 struct drm_atomic_state *state;
2776 struct drm_connector *conn;
2777 struct drm_plane *plane;
2778 struct drm_crtc *crtc;
2779 int err = 0;
2780
2781 state = drm_atomic_state_alloc(dev);
2782 if (!state)
2783 return ERR_PTR(-ENOMEM);
2784
2785 state->acquire_ctx = ctx;
2786
2787 drm_for_each_crtc(crtc, dev) {
2788 struct drm_crtc_state *crtc_state;
2789
2790 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2791 if (IS_ERR(crtc_state)) {
2792 err = PTR_ERR(crtc_state);
2793 goto free;
2794 }
2795 }
2796
2797 drm_for_each_plane(plane, dev) {
2798 struct drm_plane_state *plane_state;
2799
2800 plane_state = drm_atomic_get_plane_state(state, plane);
2801 if (IS_ERR(plane_state)) {
2802 err = PTR_ERR(plane_state);
2803 goto free;
2804 }
2805 }
2806
2807 drm_for_each_connector(conn, dev) {
2808 struct drm_connector_state *conn_state;
2809
2810 conn_state = drm_atomic_get_connector_state(state, conn);
2811 if (IS_ERR(conn_state)) {
2812 err = PTR_ERR(conn_state);
2813 goto free;
2814 }
2815 }
2816
2817 /* clear the acquire context so that it isn't accidentally reused */
2818 state->acquire_ctx = NULL;
2819
2820free:
2821 if (err < 0) {
2822 drm_atomic_state_free(state);
2823 state = ERR_PTR(err);
2824 }
2825
2826 return state;
2827}
2828EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2829
2830/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002831 * __drm_atomic_helper_connector_destroy_state - release connector state
2832 * @connector: connector object
2833 * @state: connector state object to release
2834 *
2835 * Releases all resources stored in the connector state without actually
2836 * freeing the memory of the connector state. This is useful for drivers that
2837 * subclass the connector state.
2838 */
2839void
2840__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2841 struct drm_connector_state *state)
2842{
2843 /*
2844 * This is currently a placeholder so that drivers that subclass the
2845 * state will automatically do the right thing if code is ever added
2846 * to this function.
2847 */
2848}
2849EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2850
2851/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002852 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2853 * @connector: drm connector
2854 * @state: connector state object to release
2855 *
2856 * Default connector state destroy hook for drivers which don't have their own
2857 * subclassed connector state structure.
2858 */
2859void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2860 struct drm_connector_state *state)
2861{
Thierry Redingf5e78402015-01-28 14:54:32 +01002862 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002863 kfree(state);
2864}
2865EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);