blob: baa0fc652f43e76155e283bdf86162015195246d [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
Maarten Lankhorst8248b652016-03-03 10:17:40 +010089static int handle_conflicting_encoders(struct drm_atomic_state *state,
90 bool disable_conflicting_encoders)
Maarten Lankhorst40616a22016-03-03 10:17:39 +010091{
92 struct drm_connector_state *conn_state;
93 struct drm_connector *connector;
94 struct drm_encoder *encoder;
95 unsigned encoder_mask = 0;
96 int i, ret;
97
Maarten Lankhorst8248b652016-03-03 10:17:40 +010098 /*
99 * First loop, find all newly assigned encoders from the connectors
100 * part of the state. If the same encoder is assigned to multiple
101 * connectors bail out.
102 */
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100103 for_each_connector_in_state(state, connector, conn_state, i) {
104 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
105 struct drm_encoder *new_encoder;
106
107 if (!conn_state->crtc)
108 continue;
109
110 if (funcs->atomic_best_encoder)
111 new_encoder = funcs->atomic_best_encoder(connector, conn_state);
112 else
113 new_encoder = funcs->best_encoder(connector);
114
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100115 if (new_encoder) {
116 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
117 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
118 new_encoder->base.id, new_encoder->name,
119 connector->base.id, connector->name);
120
121 return -EINVAL;
122 }
123
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100124 encoder_mask |= 1 << drm_encoder_index(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100125 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100126 }
127
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100128 if (!encoder_mask)
129 return 0;
130
131 /*
132 * Second loop, iterate over all connectors not part of the state.
133 *
134 * If a conflicting encoder is found and disable_conflicting_encoders
135 * is not set, an error is returned. Userspace can provide a solution
136 * through the atomic ioctl.
137 *
138 * If the flag is set conflicting connectors are removed from the crtc
139 * and the crtc is disabled if no encoder is left. This preserves
140 * compatibility with the legacy set_config behavior.
141 */
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100142 drm_for_each_connector(connector, state->dev) {
143 struct drm_crtc_state *crtc_state;
144
145 if (drm_atomic_get_existing_connector_state(state, connector))
146 continue;
147
148 encoder = connector->state->best_encoder;
149 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
150 continue;
151
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100152 if (!disable_conflicting_encoders) {
153 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
154 encoder->base.id, encoder->name,
155 connector->state->crtc->base.id,
156 connector->state->crtc->name,
157 connector->base.id, connector->name);
158 return -EINVAL;
159 }
160
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100161 conn_state = drm_atomic_get_connector_state(state, connector);
162 if (IS_ERR(conn_state))
163 return PTR_ERR(conn_state);
164
165 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
166 encoder->base.id, encoder->name,
167 conn_state->crtc->base.id, conn_state->crtc->name,
168 connector->base.id, connector->name);
169
170 crtc_state = drm_atomic_get_existing_crtc_state(state, conn_state->crtc);
171
172 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
173 if (ret)
174 return ret;
175
176 if (!crtc_state->connector_mask) {
177 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
178 NULL);
179 if (ret < 0)
180 return ret;
181
182 crtc_state->active = false;
183 }
184 }
185
186 return 0;
187}
188
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100189static void
190set_best_encoder(struct drm_atomic_state *state,
191 struct drm_connector_state *conn_state,
192 struct drm_encoder *encoder)
193{
194 struct drm_crtc_state *crtc_state;
195 struct drm_crtc *crtc;
196
197 if (conn_state->best_encoder) {
198 /* Unset the encoder_mask in the old crtc state. */
199 crtc = conn_state->connector->state->crtc;
200
201 /* A NULL crtc is an error here because we should have
202 * duplicated a NULL best_encoder when crtc was NULL.
203 * As an exception restoring duplicated atomic state
204 * during resume is allowed, so don't warn when
205 * best_encoder is equal to encoder we intend to set.
206 */
207 WARN_ON(!crtc && encoder != conn_state->best_encoder);
208 if (crtc) {
209 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
210
211 crtc_state->encoder_mask &=
212 ~(1 << drm_encoder_index(conn_state->best_encoder));
213 }
214 }
215
216 if (encoder) {
217 crtc = conn_state->crtc;
218 WARN_ON(!crtc);
219 if (crtc) {
220 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
221
222 crtc_state->encoder_mask |=
223 1 << drm_encoder_index(encoder);
224 }
225 }
226
227 conn_state->best_encoder = encoder;
228}
229
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100230static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200231steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100232 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200233{
Daniel Vetter623369e2014-09-16 17:50:47 +0200234 struct drm_crtc_state *crtc_state;
235 struct drm_connector *connector;
236 struct drm_connector_state *connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100237 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200238
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100239 for_each_connector_in_state(state, connector, connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100240 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200241
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100242 if (connector_state->best_encoder != encoder)
243 continue;
244
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100245 encoder_crtc = connector->state->crtc;
246
247 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
248 encoder->base.id, encoder->name,
249 encoder_crtc->base.id, encoder_crtc->name);
250
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100251 set_best_encoder(state, connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100252
253 crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
254 crtc_state->connectors_changed = true;
255
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100256 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200257 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200258}
259
260static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100261update_connector_routing(struct drm_atomic_state *state,
262 struct drm_connector *connector,
263 struct drm_connector_state *connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200264{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200265 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200266 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200267 struct drm_crtc_state *crtc_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100268 int idx;
Daniel Vetter623369e2014-09-16 17:50:47 +0200269
Daniel Vetter17a38d92015-02-22 12:24:16 +0100270 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
271 connector->base.id,
272 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200273
274 if (connector->state->crtc != connector_state->crtc) {
275 if (connector->state->crtc) {
276 idx = drm_crtc_index(connector->state->crtc);
277
278 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200279 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200280 }
281
282 if (connector_state->crtc) {
283 idx = drm_crtc_index(connector_state->crtc);
284
285 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200286 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200287 }
288 }
289
290 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100291 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200292 connector->base.id,
293 connector->name);
294
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100295 set_best_encoder(state, connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200296
297 return 0;
298 }
299
300 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200301
302 if (funcs->atomic_best_encoder)
303 new_encoder = funcs->atomic_best_encoder(connector,
304 connector_state);
305 else
306 new_encoder = funcs->best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200307
308 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100309 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
310 connector->base.id,
311 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200312 return -EINVAL;
313 }
314
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100315 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
316 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
317 new_encoder->base.id,
318 new_encoder->name,
319 connector_state->crtc->base.id);
320 return -EINVAL;
321 }
322
Daniel Vetter623369e2014-09-16 17:50:47 +0200323 if (new_encoder == connector_state->best_encoder) {
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100324 set_best_encoder(state, connector_state, new_encoder);
325
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200326 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100327 connector->base.id,
328 connector->name,
329 new_encoder->base.id,
330 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200331 connector_state->crtc->base.id,
332 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200333
334 return 0;
335 }
336
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100337 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200338
Daniel Vetter6ea76f3c2015-08-03 17:24:11 +0200339 if (WARN_ON(!connector_state->crtc))
340 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200341
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100342 set_best_encoder(state, connector_state, new_encoder);
343
Daniel Vetter623369e2014-09-16 17:50:47 +0200344 idx = drm_crtc_index(connector_state->crtc);
345
346 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200347 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200348
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200349 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100350 connector->base.id,
351 connector->name,
352 new_encoder->base.id,
353 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200354 connector_state->crtc->base.id,
355 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200356
357 return 0;
358}
359
360static int
361mode_fixup(struct drm_atomic_state *state)
362{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300363 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200364 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300365 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200366 struct drm_connector_state *conn_state;
367 int i;
368 bool ret;
369
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300370 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200371 if (!crtc_state->mode_changed &&
372 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200373 continue;
374
375 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
376 }
377
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300378 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200379 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200380 struct drm_encoder *encoder;
381
Daniel Vetter623369e2014-09-16 17:50:47 +0200382 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
383
384 if (!conn_state->crtc || !conn_state->best_encoder)
385 continue;
386
387 crtc_state =
388 state->crtc_states[drm_crtc_index(conn_state->crtc)];
389
390 /*
391 * Each encoder has at most one connector (since we always steal
392 * it away), so we won't call ->mode_fixup twice.
393 */
394 encoder = conn_state->best_encoder;
395 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300396 if (!funcs)
397 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200398
Archit Taneja862e6862015-05-21 11:03:16 +0530399 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
400 &crtc_state->adjusted_mode);
401 if (!ret) {
402 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
403 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200404 }
405
Thierry Reding4cd4df82014-12-03 16:44:34 +0100406 if (funcs->atomic_check) {
407 ret = funcs->atomic_check(encoder, crtc_state,
408 conn_state);
409 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100410 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
411 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100412 return ret;
413 }
Inki Dae84524912015-08-11 21:23:49 +0900414 } else if (funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100415 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
416 &crtc_state->adjusted_mode);
417 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100418 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
419 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100420 return -EINVAL;
421 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200422 }
423 }
424
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300425 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200426 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200427
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200428 if (!crtc_state->mode_changed &&
429 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200430 continue;
431
432 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300433 if (!funcs->mode_fixup)
434 continue;
435
Daniel Vetter623369e2014-09-16 17:50:47 +0200436 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
437 &crtc_state->adjusted_mode);
438 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200439 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
440 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200441 return -EINVAL;
442 }
443 }
444
445 return 0;
446}
447
Daniel Vetterd9b13622014-11-26 16:57:41 +0100448/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800449 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100450 * @dev: DRM device
451 * @state: the driver state object
452 *
453 * Check the state object to see if the requested state is physically possible.
454 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200455 * update and adds any additional connectors needed for full modesets and calls
456 * down into ->mode_fixup functions of the driver backend.
457 *
458 * crtc_state->mode_changed is set when the input mode is changed.
459 * crtc_state->connectors_changed is set when a connector is added or
460 * removed from the crtc.
461 * crtc_state->active_changed is set when crtc_state->active changes,
462 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100463 *
464 * IMPORTANT:
465 *
466 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
467 * plane update can't be done without a full modeset) _must_ call this function
468 * afterwards after that change. It is permitted to call this function multiple
469 * times for the same update, e.g. when the ->atomic_check functions depend upon
470 * the adjusted dotclock for fifo space allocation and watermark computation.
471 *
472 * RETURNS
473 * Zero for success or -errno
474 */
475int
Rob Clark934ce1c2014-11-19 16:41:33 -0500476drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200477 struct drm_atomic_state *state)
478{
Daniel Vetter623369e2014-09-16 17:50:47 +0200479 struct drm_crtc *crtc;
480 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300481 struct drm_connector *connector;
482 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200483 int i, ret;
484
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300485 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200486 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200487 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
488 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200489 crtc_state->mode_changed = true;
490 }
491
492 if (crtc->state->enable != crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200493 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
494 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200495
496 /*
497 * For clarity this assignment is done here, but
498 * enable == 0 is only true when there are no
499 * connectors and a NULL mode.
500 *
501 * The other way around is true as well. enable != 0
502 * iff connectors are attached and a mode is set.
503 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200504 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200505 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200506 }
507 }
508
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100509 ret = handle_conflicting_encoders(state, state->legacy_set_config);
510 if (ret)
511 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100512
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300513 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200514 /*
515 * This only sets crtc->mode_changed for routing changes,
516 * drivers must set crtc->mode_changed themselves when connector
517 * properties need to be updated.
518 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100519 ret = update_connector_routing(state, connector,
520 connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200521 if (ret)
522 return ret;
523 }
524
525 /*
526 * After all the routing has been prepared we need to add in any
527 * connector which is itself unchanged, but who's crtc changes it's
528 * configuration. This must be done before calling mode_fixup in case a
529 * crtc only changed its mode but has the same set of connectors.
530 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300531 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100532 bool has_connectors =
533 !!crtc_state->connector_mask;
Daniel Vetter623369e2014-09-16 17:50:47 +0200534
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100535 /*
536 * We must set ->active_changed after walking connectors for
537 * otherwise an update that only changes active would result in
538 * a full modeset because update_connector_routing force that.
539 */
540 if (crtc->state->active != crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200541 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
542 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100543 crtc_state->active_changed = true;
544 }
545
Daniel Vetter2465ff62015-06-18 09:58:55 +0200546 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100547 continue;
548
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200549 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
550 crtc->base.id, crtc->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100551 crtc_state->enable ? 'y' : 'n',
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200552 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200553
554 ret = drm_atomic_add_affected_connectors(state, crtc);
555 if (ret != 0)
556 return ret;
557
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200558 ret = drm_atomic_add_affected_planes(state, crtc);
559 if (ret != 0)
560 return ret;
561
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100562 if (crtc_state->enable != has_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200563 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
564 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200565
566 return -EINVAL;
567 }
568 }
569
570 return mode_fixup(state);
571}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100572EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200573
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100574/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800575 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100576 * @dev: DRM device
577 * @state: the driver state object
578 *
579 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100580 * This does all the plane update related checks using by calling into the
581 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100582 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200583 * It also sets crtc_state->planes_changed to indicate that a crtc has
584 * updated planes.
585 *
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100586 * RETURNS
587 * Zero for success or -errno
588 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100589int
590drm_atomic_helper_check_planes(struct drm_device *dev,
591 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100592{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300593 struct drm_crtc *crtc;
594 struct drm_crtc_state *crtc_state;
595 struct drm_plane *plane;
596 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100597 int i, ret = 0;
598
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300599 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200600 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100601
602 funcs = plane->helper_private;
603
604 drm_atomic_helper_plane_changed(state, plane_state, plane);
605
606 if (!funcs || !funcs->atomic_check)
607 continue;
608
609 ret = funcs->atomic_check(plane, plane_state);
610 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200611 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
612 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100613 return ret;
614 }
615 }
616
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300617 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200618 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100619
620 funcs = crtc->helper_private;
621
622 if (!funcs || !funcs->atomic_check)
623 continue;
624
625 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
626 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200627 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
628 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100629 return ret;
630 }
631 }
632
Daniel Vetterd9b13622014-11-26 16:57:41 +0100633 return ret;
634}
635EXPORT_SYMBOL(drm_atomic_helper_check_planes);
636
637/**
638 * drm_atomic_helper_check - validate state object
639 * @dev: DRM device
640 * @state: the driver state object
641 *
642 * Check the state object to see if the requested state is physically possible.
643 * Only crtcs and planes have check callbacks, so for any additional (global)
644 * checking that a driver needs it can simply wrap that around this function.
645 * Drivers without such needs can directly use this as their ->atomic_check()
646 * callback.
647 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100648 * This just wraps the two parts of the state checking for planes and modeset
649 * state in the default order: First it calls drm_atomic_helper_check_modeset()
650 * and then drm_atomic_helper_check_planes(). The assumption is that the
651 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
652 * e.g. properly compute watermarks.
653 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100654 * RETURNS
655 * Zero for success or -errno
656 */
657int drm_atomic_helper_check(struct drm_device *dev,
658 struct drm_atomic_state *state)
659{
660 int ret;
661
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100662 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100663 if (ret)
664 return ret;
665
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100666 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500667 if (ret)
668 return ret;
669
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100670 return ret;
671}
672EXPORT_SYMBOL(drm_atomic_helper_check);
673
Daniel Vetter623369e2014-09-16 17:50:47 +0200674static void
675disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
676{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300677 struct drm_connector *connector;
678 struct drm_connector_state *old_conn_state;
679 struct drm_crtc *crtc;
680 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200681 int i;
682
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300683 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200684 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200685 struct drm_encoder *encoder;
686
Daniel Vetter623369e2014-09-16 17:50:47 +0200687 /* Shut down everything that's in the changeset and currently
688 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300689 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200690 continue;
691
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100692 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
693
Daniel Vetter4218a322015-03-26 22:18:40 +0100694 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200695 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100696 continue;
697
Rob Clark46df9ad2014-11-20 15:40:36 -0500698 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200699
Rob Clark46df9ad2014-11-20 15:40:36 -0500700 /* We shouldn't get this far if we didn't previously have
701 * an encoder.. but WARN_ON() rather than explode.
702 */
703 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200704 continue;
705
706 funcs = encoder->helper_private;
707
Daniel Vetter17a38d92015-02-22 12:24:16 +0100708 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
709 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100710
Daniel Vetter623369e2014-09-16 17:50:47 +0200711 /*
712 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800713 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200714 */
Archit Taneja862e6862015-05-21 11:03:16 +0530715 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200716
717 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100718 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200719 funcs->prepare(encoder);
720 else if (funcs->disable)
721 funcs->disable(encoder);
722 else
723 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
724
Archit Taneja862e6862015-05-21 11:03:16 +0530725 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200726 }
727
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300728 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200729 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200730
731 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200732 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100733 continue;
734
735 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200736 continue;
737
738 funcs = crtc->helper_private;
739
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200740 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
741 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100742
743
Daniel Vetter623369e2014-09-16 17:50:47 +0200744 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100745 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200746 funcs->prepare(crtc);
747 else if (funcs->disable)
748 funcs->disable(crtc);
749 else
750 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
751 }
752}
753
Daniel Vetter4c18d302015-05-12 15:27:37 +0200754/**
755 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
756 * @dev: DRM device
757 * @old_state: atomic state object with old state structures
758 *
759 * This function updates all the various legacy modeset state pointers in
760 * connectors, encoders and crtcs. It also updates the timestamping constants
761 * used for precise vblank timestamps by calling
762 * drm_calc_timestamping_constants().
763 *
764 * Drivers can use this for building their own atomic commit if they don't have
765 * a pure helper-based modeset implementation.
766 */
767void
768drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
769 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200770{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300771 struct drm_connector *connector;
772 struct drm_connector_state *old_conn_state;
773 struct drm_crtc *crtc;
774 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200775 int i;
776
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200777 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300778 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200779 if (connector->encoder) {
780 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200781
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200782 connector->encoder->crtc = NULL;
783 connector->encoder = NULL;
784 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200785
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200786 crtc = connector->state->crtc;
787 if ((!crtc && old_conn_state->crtc) ||
788 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
789 struct drm_property *dpms_prop =
790 dev->mode_config.dpms_property;
791 int mode = DRM_MODE_DPMS_OFF;
792
793 if (crtc && crtc->state->active)
794 mode = DRM_MODE_DPMS_ON;
795
796 connector->dpms = mode;
797 drm_object_property_set_value(&connector->base,
798 dpms_prop, mode);
799 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200800 }
801
802 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300803 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
804 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200805 continue;
806
807 if (WARN_ON(!connector->state->best_encoder))
808 continue;
809
810 connector->encoder = connector->state->best_encoder;
811 connector->encoder->crtc = connector->state->crtc;
812 }
813
814 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300815 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200816 struct drm_plane *primary = crtc->primary;
817
Daniel Vetter623369e2014-09-16 17:50:47 +0200818 crtc->mode = crtc->state->mode;
819 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200820
821 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
822 primary->state->crtc == crtc) {
823 crtc->x = primary->state->src_x >> 16;
824 crtc->y = primary->state->src_y >> 16;
825 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200826
827 if (crtc->state->enable)
828 drm_calc_timestamping_constants(crtc,
829 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200830 }
831}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200832EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200833
834static void
835crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
836{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300837 struct drm_crtc *crtc;
838 struct drm_crtc_state *old_crtc_state;
839 struct drm_connector *connector;
840 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200841 int i;
842
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300843 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200844 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200845
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300846 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200847 continue;
848
849 funcs = crtc->helper_private;
850
Daniel Vetterc982bd92015-02-22 12:24:20 +0100851 if (crtc->state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200852 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
853 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100854
Daniel Vetter623369e2014-09-16 17:50:47 +0200855 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100856 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200857 }
858
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300859 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200860 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200861 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200862 struct drm_encoder *encoder;
863 struct drm_display_mode *mode, *adjusted_mode;
864
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300865 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200866 continue;
867
868 encoder = connector->state->best_encoder;
869 funcs = encoder->helper_private;
870 new_crtc_state = connector->state->crtc->state;
871 mode = &new_crtc_state->mode;
872 adjusted_mode = &new_crtc_state->adjusted_mode;
873
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100874 if (!new_crtc_state->mode_changed)
875 continue;
876
Daniel Vetter17a38d92015-02-22 12:24:16 +0100877 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
878 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100879
Daniel Vetter623369e2014-09-16 17:50:47 +0200880 /*
881 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800882 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200883 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100884 if (funcs->mode_set)
885 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200886
Archit Taneja862e6862015-05-21 11:03:16 +0530887 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200888 }
889}
890
891/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100892 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200893 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100894 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200895 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100896 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200897 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100898 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300899 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100900 * drm_atomic_helper_commit_planes(), which is what the default commit function
901 * does. But drivers with different needs can group the modeset commits together
902 * and do the plane commits at the end. This is useful for drivers doing runtime
903 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200904 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100905void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
906 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200907{
Laurent Pincharta072f802015-02-22 12:24:18 +0100908 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200909
910 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
911
Laurent Pincharta072f802015-02-22 12:24:18 +0100912 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200913}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100914EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200915
916/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100917 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200918 * @dev: DRM device
919 * @old_state: atomic state object with old state structures
920 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100921 * This function enables all the outputs with the new configuration which had to
922 * be turned off for the update.
923 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300924 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100925 * drm_atomic_helper_commit_planes(), which is what the default commit function
926 * does. But drivers with different needs can group the modeset commits together
927 * and do the plane commits at the end. This is useful for drivers doing runtime
928 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200929 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100930void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
931 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200932{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300933 struct drm_crtc *crtc;
934 struct drm_crtc_state *old_crtc_state;
935 struct drm_connector *connector;
936 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200937 int i;
938
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300939 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200940 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200941
942 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200943 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100944 continue;
945
946 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200947 continue;
948
949 funcs = crtc->helper_private;
950
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100951 if (crtc->state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200952 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
953 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100954
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100955 if (funcs->enable)
956 funcs->enable(crtc);
957 else
958 funcs->commit(crtc);
959 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200960 }
961
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300962 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200963 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200964 struct drm_encoder *encoder;
965
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300966 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200967 continue;
968
Daniel Vetter4218a322015-03-26 22:18:40 +0100969 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200970 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100971 continue;
972
Daniel Vetter623369e2014-09-16 17:50:47 +0200973 encoder = connector->state->best_encoder;
974 funcs = encoder->helper_private;
975
Daniel Vetter17a38d92015-02-22 12:24:16 +0100976 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
977 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100978
Daniel Vetter623369e2014-09-16 17:50:47 +0200979 /*
980 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800981 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200982 */
Archit Taneja862e6862015-05-21 11:03:16 +0530983 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200984
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100985 if (funcs->enable)
986 funcs->enable(encoder);
987 else
988 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200989
Archit Taneja862e6862015-05-21 11:03:16 +0530990 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200991 }
992}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100993EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200994
Daniel Vettere2330f02014-10-29 11:34:56 +0100995static void wait_for_fences(struct drm_device *dev,
996 struct drm_atomic_state *state)
997{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300998 struct drm_plane *plane;
999 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +01001000 int i;
1001
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001002 for_each_plane_in_state(state, plane, plane_state, i) {
1003 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001004 continue;
1005
1006 WARN_ON(!plane->state->fb);
1007
1008 fence_wait(plane->state->fence, false);
1009 fence_put(plane->state->fence);
1010 plane->state->fence = NULL;
1011 }
1012}
1013
John Keepingc2409062016-01-19 10:46:58 +00001014/**
1015 * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
1016 * @dev: DRM device
1017 * @old_state: atomic state object with old state structures
1018 * @crtc: DRM crtc
1019 *
1020 * Checks whether the framebuffer used for this CRTC changes as a result of
1021 * the atomic update. This is useful for drivers which cannot use
1022 * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
1023 * functionality.
1024 *
1025 * Returns:
1026 * true if the framebuffer changed.
1027 */
1028bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
1029 struct drm_atomic_state *old_state,
1030 struct drm_crtc *crtc)
Daniel Vetterab58e332014-11-24 20:42:42 +01001031{
1032 struct drm_plane *plane;
1033 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +01001034 int i;
1035
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001036 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +01001037 if (plane->state->crtc != crtc &&
1038 old_plane_state->crtc != crtc)
1039 continue;
1040
1041 if (plane->state->fb != old_plane_state->fb)
1042 return true;
1043 }
1044
1045 return false;
1046}
John Keepingc2409062016-01-19 10:46:58 +00001047EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
Daniel Vetterab58e332014-11-24 20:42:42 +01001048
Rob Clark5ee32292014-11-11 19:38:59 -05001049/**
1050 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1051 * @dev: DRM device
1052 * @old_state: atomic state object with old state structures
1053 *
1054 * Helper to, after atomic commit, wait for vblanks on all effected
1055 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001056 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1057 * framebuffers have actually changed to optimize for the legacy cursor and
1058 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001059 */
1060void
1061drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1062 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001063{
1064 struct drm_crtc *crtc;
1065 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001066 int i, ret;
1067
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001068 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +02001069 /* No one cares about the old state, so abuse it for tracking
1070 * and store whether we hold a vblank reference (and should do a
1071 * vblank wait) in the ->enable boolean. */
1072 old_crtc_state->enable = false;
1073
1074 if (!crtc->state->enable)
1075 continue;
1076
Daniel Vetterf02ad902015-01-22 16:36:23 +01001077 /* Legacy cursor ioctls are completely unsynced, and userspace
1078 * relies on that (by doing tons of cursor updates). */
1079 if (old_state->legacy_cursor_update)
1080 continue;
1081
John Keepingc2409062016-01-19 10:46:58 +00001082 if (!drm_atomic_helper_framebuffer_changed(dev,
1083 old_state, crtc))
Daniel Vetterab58e332014-11-24 20:42:42 +01001084 continue;
1085
Daniel Vetter623369e2014-09-16 17:50:47 +02001086 ret = drm_crtc_vblank_get(crtc);
1087 if (ret != 0)
1088 continue;
1089
1090 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +02001091 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001092 }
1093
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001094 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1095 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +02001096 continue;
1097
1098 ret = wait_event_timeout(dev->vblank[i].queue,
1099 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001100 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001101 msecs_to_jiffies(50));
1102
1103 drm_crtc_vblank_put(crtc);
1104 }
1105}
Rob Clark5ee32292014-11-11 19:38:59 -05001106EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001107
1108/**
1109 * drm_atomic_helper_commit - commit validated state object
1110 * @dev: DRM device
1111 * @state: the driver state object
1112 * @async: asynchronous commit
1113 *
1114 * This function commits a with drm_atomic_helper_check() pre-validated state
1115 * object. This can still fail when e.g. the framebuffer reservation fails. For
1116 * now this doesn't implement asynchronous commits.
1117 *
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001118 * Note that right now this function does not support async commits, and hence
1119 * driver writers must implement their own version for now. Also note that the
1120 * default ordering of how the various stages are called is to match the legacy
1121 * modeset helper library closest. One peculiarity of that is that it doesn't
1122 * mesh well with runtime PM at all.
1123 *
1124 * For drivers supporting runtime PM the recommended sequence is
1125 *
1126 * drm_atomic_helper_commit_modeset_disables(dev, state);
1127 *
1128 * drm_atomic_helper_commit_modeset_enables(dev, state);
1129 *
1130 * drm_atomic_helper_commit_planes(dev, state, true);
1131 *
1132 * See the kerneldoc entries for these three functions for more details.
1133 *
Daniel Vetter623369e2014-09-16 17:50:47 +02001134 * RETURNS
1135 * Zero for success or -errno.
1136 */
1137int drm_atomic_helper_commit(struct drm_device *dev,
1138 struct drm_atomic_state *state,
1139 bool async)
1140{
1141 int ret;
1142
1143 if (async)
1144 return -EBUSY;
1145
1146 ret = drm_atomic_helper_prepare_planes(dev, state);
1147 if (ret)
1148 return ret;
1149
1150 /*
1151 * This is the point of no return - everything below never fails except
1152 * when the hw goes bonghits. Which means we can commit the new state on
1153 * the software side now.
1154 */
1155
1156 drm_atomic_helper_swap_state(dev, state);
1157
1158 /*
1159 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001160 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001161 * that the asynchronous work has either been cancelled (if the driver
1162 * supports it, which at least requires that the framebuffers get
1163 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1164 * before the new state gets committed on the software side with
1165 * drm_atomic_helper_swap_state().
1166 *
1167 * This scheme allows new atomic state updates to be prepared and
1168 * checked in parallel to the asynchronous completion of the previous
1169 * update. Which is important since compositors need to figure out the
1170 * composition of the next frame right after having submitted the
1171 * current layout.
1172 */
1173
Daniel Vettere2330f02014-10-29 11:34:56 +01001174 wait_for_fences(dev, state);
1175
Daniel Vetter1af434a2015-02-22 12:24:19 +01001176 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001177
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001178 drm_atomic_helper_commit_planes(dev, state, false);
Daniel Vetter623369e2014-09-16 17:50:47 +02001179
Daniel Vetter1af434a2015-02-22 12:24:19 +01001180 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001181
Rob Clark5ee32292014-11-11 19:38:59 -05001182 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001183
1184 drm_atomic_helper_cleanup_planes(dev, state);
1185
1186 drm_atomic_state_free(state);
1187
1188 return 0;
1189}
1190EXPORT_SYMBOL(drm_atomic_helper_commit);
1191
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001192/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001193 * DOC: implementing async commit
1194 *
1195 * For now the atomic helpers don't support async commit directly. If there is
1196 * real need it could be added though, using the dma-buf fence infrastructure
1197 * for generic synchronization with outstanding rendering.
1198 *
1199 * For now drivers have to implement async commit themselves, with the following
1200 * sequence being the recommended one:
1201 *
1202 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1203 * which commit needs to call which can fail, so we want to run it first and
1204 * synchronously.
1205 *
1206 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1207 * might be affected the new state update. This can be done by either cancelling
1208 * or flushing the work items, depending upon whether the driver can deal with
1209 * cancelled updates. Note that it is important to ensure that the framebuffer
1210 * cleanup is still done when cancelling.
1211 *
1212 * For sufficient parallelism it is recommended to have a work item per crtc
1213 * (for updates which don't touch global state) and a global one. Then we only
1214 * need to synchronize with the crtc work items for changed crtcs and the global
1215 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1216 *
1217 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001218 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001219 * locks means concurrent callers never see inconsistent state. And doing this
1220 * while it's guaranteed that no relevant async worker runs means that async
1221 * workers do not need grab any locks. Actually they must not grab locks, for
1222 * otherwise the work flushing will deadlock.
1223 *
1224 * 4. Schedule a work item to do all subsequent steps, using the split-out
1225 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1226 * then cleaning up the framebuffers after the old framebuffer is no longer
1227 * being displayed.
1228 */
1229
1230/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001231 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001232 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001233 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001234 *
1235 * This function prepares plane state, specifically framebuffers, for the new
1236 * configuration. If any failure is encountered this function will call
1237 * ->cleanup_fb on any already successfully prepared framebuffer.
1238 *
1239 * Returns:
1240 * 0 on success, negative error code on failure.
1241 */
1242int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1243 struct drm_atomic_state *state)
1244{
1245 int nplanes = dev->mode_config.num_total_plane;
1246 int ret, i;
1247
1248 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001249 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001250 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001251 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001252
1253 if (!plane)
1254 continue;
1255
1256 funcs = plane->helper_private;
1257
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001258 if (funcs->prepare_fb) {
1259 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001260 if (ret)
1261 goto fail;
1262 }
1263 }
1264
1265 return 0;
1266
1267fail:
1268 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001269 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001270 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001271 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001272
1273 if (!plane)
1274 continue;
1275
1276 funcs = plane->helper_private;
1277
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001278 if (funcs->cleanup_fb)
1279 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001280
1281 }
1282
1283 return ret;
1284}
1285EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1286
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001287bool plane_crtc_active(struct drm_plane_state *state)
1288{
1289 return state->crtc && state->crtc->state->active;
1290}
1291
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001292/**
1293 * drm_atomic_helper_commit_planes - commit plane state
1294 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001295 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001296 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001297 *
1298 * This function commits the new plane state using the plane and atomic helper
1299 * functions for planes and crtcs. It assumes that the atomic state has already
1300 * been pushed into the relevant object state pointers, since this step can no
1301 * longer fail.
1302 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001303 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001304 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001305 *
1306 * Note that this function does all plane updates across all CRTCs in one step.
1307 * If the hardware can't support this approach look at
1308 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001309 *
1310 * Plane parameters can be updated by applications while the associated CRTC is
1311 * disabled. The DRM/KMS core will store the parameters in the plane state,
1312 * which will be available to the driver when the CRTC is turned on. As a result
1313 * most drivers don't need to be immediately notified of plane updates for a
1314 * disabled CRTC.
1315 *
1316 * Unless otherwise needed, drivers are advised to set the @active_only
1317 * parameters to true in order not to receive plane update notifications related
1318 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1319 * driver code when the driver and/or hardware can't or just don't need to deal
1320 * with updates on disabled CRTCs, for example when supporting runtime PM.
1321 *
1322 * The drm_atomic_helper_commit() default implementation only sets @active_only
1323 * to false to most closely match the behaviour of the legacy helpers. This should
1324 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001325 */
1326void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001327 struct drm_atomic_state *old_state,
1328 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001329{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001330 struct drm_crtc *crtc;
1331 struct drm_crtc_state *old_crtc_state;
1332 struct drm_plane *plane;
1333 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001334 int i;
1335
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001336 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001337 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001338
1339 funcs = crtc->helper_private;
1340
1341 if (!funcs || !funcs->atomic_begin)
1342 continue;
1343
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001344 if (active_only && !crtc->state->active)
1345 continue;
1346
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001347 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001348 }
1349
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001350 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001351 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001352 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001353
1354 funcs = plane->helper_private;
1355
Thierry Reding3cad4b62014-11-25 13:05:12 +01001356 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001357 continue;
1358
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001359 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1360
1361 if (active_only) {
1362 /*
1363 * Skip planes related to inactive CRTCs. If the plane
1364 * is enabled use the state of the current CRTC. If the
1365 * plane is being disabled use the state of the old
1366 * CRTC to avoid skipping planes being disabled on an
1367 * active CRTC.
1368 */
1369 if (!disabling && !plane_crtc_active(plane->state))
1370 continue;
1371 if (disabling && !plane_crtc_active(old_plane_state))
1372 continue;
1373 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001374
Thierry Reding407b8bd2014-11-20 12:05:50 +01001375 /*
1376 * Special-case disabling the plane if drivers support it.
1377 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001378 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001379 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001380 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001381 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001382 }
1383
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001384 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001385 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001386
1387 funcs = crtc->helper_private;
1388
1389 if (!funcs || !funcs->atomic_flush)
1390 continue;
1391
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001392 if (active_only && !crtc->state->active)
1393 continue;
1394
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001395 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001396 }
1397}
1398EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1399
1400/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001401 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1402 * @old_crtc_state: atomic state object with the old crtc state
1403 *
1404 * This function commits the new plane state using the plane and atomic helper
1405 * functions for planes on the specific crtc. It assumes that the atomic state
1406 * has already been pushed into the relevant object state pointers, since this
1407 * step can no longer fail.
1408 *
1409 * This function is useful when plane updates should be done crtc-by-crtc
1410 * instead of one global step like drm_atomic_helper_commit_planes() does.
1411 *
1412 * This function can only be savely used when planes are not allowed to move
1413 * between different CRTCs because this function doesn't handle inter-CRTC
1414 * depencies. Callers need to ensure that either no such depencies exist,
1415 * resolve them through ordering of commit calls or through some other means.
1416 */
1417void
1418drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1419{
1420 const struct drm_crtc_helper_funcs *crtc_funcs;
1421 struct drm_crtc *crtc = old_crtc_state->crtc;
1422 struct drm_atomic_state *old_state = old_crtc_state->state;
1423 struct drm_plane *plane;
1424 unsigned plane_mask;
1425
1426 plane_mask = old_crtc_state->plane_mask;
1427 plane_mask |= crtc->state->plane_mask;
1428
1429 crtc_funcs = crtc->helper_private;
1430 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001431 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001432
1433 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1434 struct drm_plane_state *old_plane_state =
1435 drm_atomic_get_existing_plane_state(old_state, plane);
1436 const struct drm_plane_helper_funcs *plane_funcs;
1437
1438 plane_funcs = plane->helper_private;
1439
1440 if (!old_plane_state || !plane_funcs)
1441 continue;
1442
1443 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1444
1445 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1446 plane_funcs->atomic_disable)
1447 plane_funcs->atomic_disable(plane, old_plane_state);
1448 else if (plane->state->crtc ||
1449 drm_atomic_plane_disabling(plane, old_plane_state))
1450 plane_funcs->atomic_update(plane, old_plane_state);
1451 }
1452
1453 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001454 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001455}
1456EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1457
1458/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001459 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1460 * @crtc: CRTC
1461 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1462 *
1463 * Disables all planes associated with the given CRTC. This can be
1464 * used for instance in the CRTC helper disable callback to disable
1465 * all planes before shutting down the display pipeline.
1466 *
1467 * If the atomic-parameter is set the function calls the CRTC's
1468 * atomic_begin hook before and atomic_flush hook after disabling the
1469 * planes.
1470 *
1471 * It is a bug to call this function without having implemented the
1472 * ->atomic_disable() plane hook.
1473 */
1474void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1475 bool atomic)
1476{
1477 const struct drm_crtc_helper_funcs *crtc_funcs =
1478 crtc->helper_private;
1479 struct drm_plane *plane;
1480
1481 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1482 crtc_funcs->atomic_begin(crtc, NULL);
1483
1484 drm_for_each_plane(plane, crtc->dev) {
1485 const struct drm_plane_helper_funcs *plane_funcs =
1486 plane->helper_private;
1487
1488 if (plane->state->crtc != crtc || !plane_funcs)
1489 continue;
1490
1491 WARN_ON(!plane_funcs->atomic_disable);
1492 if (plane_funcs->atomic_disable)
1493 plane_funcs->atomic_disable(plane, NULL);
1494 }
1495
1496 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1497 crtc_funcs->atomic_flush(crtc, NULL);
1498}
1499EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1500
1501/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001502 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1503 * @dev: DRM device
1504 * @old_state: atomic state object with old state structures
1505 *
1506 * This function cleans up plane state, specifically framebuffers, from the old
1507 * configuration. Hence the old configuration must be perserved in @old_state to
1508 * be able to call this function.
1509 *
1510 * This function must also be called on the new state when the atomic update
1511 * fails at any point after calling drm_atomic_helper_prepare_planes().
1512 */
1513void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1514 struct drm_atomic_state *old_state)
1515{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001516 struct drm_plane *plane;
1517 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001518 int i;
1519
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001520 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001521 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001522
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001523 funcs = plane->helper_private;
1524
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001525 if (funcs->cleanup_fb)
1526 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001527 }
1528}
1529EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1530
1531/**
1532 * drm_atomic_helper_swap_state - store atomic state into current sw state
1533 * @dev: DRM device
1534 * @state: atomic state
1535 *
1536 * This function stores the atomic state into the current state pointers in all
1537 * driver objects. It should be called after all failing steps have been done
1538 * and succeeded, but before the actual hardware state is committed.
1539 *
1540 * For cleanup and error recovery the current state for all changed objects will
1541 * be swaped into @state.
1542 *
1543 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1544 *
1545 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1546 *
1547 * 2. Do any other steps that might fail.
1548 *
1549 * 3. Put the staged state into the current state pointers with this function.
1550 *
1551 * 4. Actually commit the hardware state.
1552 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001553 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001554 * contains the old state. Also do any other cleanup required with that state.
1555 */
1556void drm_atomic_helper_swap_state(struct drm_device *dev,
1557 struct drm_atomic_state *state)
1558{
1559 int i;
1560
1561 for (i = 0; i < dev->mode_config.num_connector; i++) {
1562 struct drm_connector *connector = state->connectors[i];
1563
1564 if (!connector)
1565 continue;
1566
1567 connector->state->state = state;
1568 swap(state->connector_states[i], connector->state);
1569 connector->state->state = NULL;
1570 }
1571
1572 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1573 struct drm_crtc *crtc = state->crtcs[i];
1574
1575 if (!crtc)
1576 continue;
1577
1578 crtc->state->state = state;
1579 swap(state->crtc_states[i], crtc->state);
1580 crtc->state->state = NULL;
1581 }
1582
1583 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1584 struct drm_plane *plane = state->planes[i];
1585
1586 if (!plane)
1587 continue;
1588
1589 plane->state->state = state;
1590 swap(state->plane_states[i], plane->state);
1591 plane->state->state = NULL;
1592 }
1593}
1594EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001595
1596/**
1597 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1598 * @plane: plane object to update
1599 * @crtc: owning CRTC of owning plane
1600 * @fb: framebuffer to flip onto plane
1601 * @crtc_x: x offset of primary plane on crtc
1602 * @crtc_y: y offset of primary plane on crtc
1603 * @crtc_w: width of primary plane rectangle on crtc
1604 * @crtc_h: height of primary plane rectangle on crtc
1605 * @src_x: x offset of @fb for panning
1606 * @src_y: y offset of @fb for panning
1607 * @src_w: width of source rectangle in @fb
1608 * @src_h: height of source rectangle in @fb
1609 *
1610 * Provides a default plane update handler using the atomic driver interface.
1611 *
1612 * RETURNS:
1613 * Zero on success, error code on failure
1614 */
1615int drm_atomic_helper_update_plane(struct drm_plane *plane,
1616 struct drm_crtc *crtc,
1617 struct drm_framebuffer *fb,
1618 int crtc_x, int crtc_y,
1619 unsigned int crtc_w, unsigned int crtc_h,
1620 uint32_t src_x, uint32_t src_y,
1621 uint32_t src_w, uint32_t src_h)
1622{
1623 struct drm_atomic_state *state;
1624 struct drm_plane_state *plane_state;
1625 int ret = 0;
1626
1627 state = drm_atomic_state_alloc(plane->dev);
1628 if (!state)
1629 return -ENOMEM;
1630
1631 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1632retry:
1633 plane_state = drm_atomic_get_plane_state(state, plane);
1634 if (IS_ERR(plane_state)) {
1635 ret = PTR_ERR(plane_state);
1636 goto fail;
1637 }
1638
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001639 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001640 if (ret != 0)
1641 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001642 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001643 plane_state->crtc_x = crtc_x;
1644 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001645 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001646 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001647 plane_state->src_x = src_x;
1648 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001649 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001650 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001651
Daniel Vetter3671c582015-05-04 15:40:52 +02001652 if (plane == crtc->cursor)
1653 state->legacy_cursor_update = true;
1654
Daniel Vetter042652e2014-07-27 13:46:52 +02001655 ret = drm_atomic_commit(state);
1656 if (ret != 0)
1657 goto fail;
1658
1659 /* Driver takes ownership of state on successful commit. */
1660 return 0;
1661fail:
1662 if (ret == -EDEADLK)
1663 goto backoff;
1664
1665 drm_atomic_state_free(state);
1666
1667 return ret;
1668backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001669 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001670 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001671
1672 /*
1673 * Someone might have exchanged the framebuffer while we dropped locks
1674 * in the backoff code. We need to fix up the fb refcount tracking the
1675 * core does for us.
1676 */
1677 plane->old_fb = plane->fb;
1678
1679 goto retry;
1680}
1681EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1682
1683/**
1684 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1685 * @plane: plane to disable
1686 *
1687 * Provides a default plane disable handler using the atomic driver interface.
1688 *
1689 * RETURNS:
1690 * Zero on success, error code on failure
1691 */
1692int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1693{
1694 struct drm_atomic_state *state;
1695 struct drm_plane_state *plane_state;
1696 int ret = 0;
1697
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001698 /*
1699 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1700 * acquire context. The real fix will be to wire the acquire ctx through
1701 * everywhere we need it, but meanwhile prevent chaos by just skipping
1702 * this noop. The critical case is the cursor ioctls which a) only grab
1703 * crtc/cursor-plane locks (so we need the crtc to get at the right
1704 * acquire context) and b) can try to disable the plane multiple times.
1705 */
1706 if (!plane->crtc)
1707 return 0;
1708
Daniel Vetter042652e2014-07-27 13:46:52 +02001709 state = drm_atomic_state_alloc(plane->dev);
1710 if (!state)
1711 return -ENOMEM;
1712
1713 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1714retry:
1715 plane_state = drm_atomic_get_plane_state(state, plane);
1716 if (IS_ERR(plane_state)) {
1717 ret = PTR_ERR(plane_state);
1718 goto fail;
1719 }
1720
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01001721 if (plane_state->crtc && (plane == plane->crtc->cursor))
1722 plane_state->state->legacy_cursor_update = true;
1723
Rob Clarkbbb1e522015-08-25 15:35:58 -04001724 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001725 if (ret != 0)
1726 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01001727
Daniel Vetter042652e2014-07-27 13:46:52 +02001728 ret = drm_atomic_commit(state);
1729 if (ret != 0)
1730 goto fail;
1731
1732 /* Driver takes ownership of state on successful commit. */
1733 return 0;
1734fail:
1735 if (ret == -EDEADLK)
1736 goto backoff;
1737
1738 drm_atomic_state_free(state);
1739
1740 return ret;
1741backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001742 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001743 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001744
1745 /*
1746 * Someone might have exchanged the framebuffer while we dropped locks
1747 * in the backoff code. We need to fix up the fb refcount tracking the
1748 * core does for us.
1749 */
1750 plane->old_fb = plane->fb;
1751
1752 goto retry;
1753}
1754EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1755
Rob Clarkbbb1e522015-08-25 15:35:58 -04001756/* just used from fb-helper and atomic-helper: */
1757int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1758 struct drm_plane_state *plane_state)
1759{
1760 int ret;
1761
1762 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1763 if (ret != 0)
1764 return ret;
1765
1766 drm_atomic_set_fb_for_plane(plane_state, NULL);
1767 plane_state->crtc_x = 0;
1768 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001769 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001770 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001771 plane_state->src_x = 0;
1772 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001773 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001774 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001775
Rob Clarkbbb1e522015-08-25 15:35:58 -04001776 return 0;
1777}
1778
Daniel Vetter042652e2014-07-27 13:46:52 +02001779static int update_output_state(struct drm_atomic_state *state,
1780 struct drm_mode_set *set)
1781{
1782 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001783 struct drm_crtc *crtc;
1784 struct drm_crtc_state *crtc_state;
1785 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001786 struct drm_connector_state *conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001787 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02001788
1789 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1790 state->acquire_ctx);
1791 if (ret)
1792 return ret;
1793
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001794 /* First disable all connectors on the target crtc. */
1795 ret = drm_atomic_add_affected_connectors(state, set->crtc);
1796 if (ret)
1797 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02001798
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001799 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001800 if (conn_state->crtc == set->crtc) {
1801 ret = drm_atomic_set_crtc_for_connector(conn_state,
1802 NULL);
1803 if (ret)
1804 return ret;
1805 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001806 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001807
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001808 /* Then set all connectors from set->connectors on the target crtc */
1809 for (i = 0; i < set->num_connectors; i++) {
1810 conn_state = drm_atomic_get_connector_state(state,
1811 set->connectors[i]);
1812 if (IS_ERR(conn_state))
1813 return PTR_ERR(conn_state);
1814
1815 ret = drm_atomic_set_crtc_for_connector(conn_state,
1816 set->crtc);
1817 if (ret)
1818 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02001819 }
1820
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001821 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001822 /* Don't update ->enable for the CRTC in the set_config request,
1823 * since a mismatch would indicate a bug in the upper layers.
1824 * The actual modeset code later on will catch any
1825 * inconsistencies here. */
1826 if (crtc == set->crtc)
1827 continue;
1828
Maarten Lankhorst14de6c42016-01-04 12:53:20 +01001829 if (!crtc_state->connector_mask) {
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001830 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1831 NULL);
1832 if (ret < 0)
1833 return ret;
1834
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001835 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001836 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001837 }
1838
1839 return 0;
1840}
1841
1842/**
1843 * drm_atomic_helper_set_config - set a new config from userspace
1844 * @set: mode set configuration
1845 *
1846 * Provides a default crtc set_config handler using the atomic driver interface.
1847 *
1848 * Returns:
1849 * Returns 0 on success, negative errno numbers on failure.
1850 */
1851int drm_atomic_helper_set_config(struct drm_mode_set *set)
1852{
1853 struct drm_atomic_state *state;
1854 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02001855 int ret = 0;
1856
1857 state = drm_atomic_state_alloc(crtc->dev);
1858 if (!state)
1859 return -ENOMEM;
1860
Maarten Lankhorst40616a22016-03-03 10:17:39 +01001861 state->legacy_set_config = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02001862 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1863retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04001864 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01001865 if (ret != 0)
1866 goto fail;
1867
Daniel Vetter042652e2014-07-27 13:46:52 +02001868 ret = drm_atomic_commit(state);
1869 if (ret != 0)
1870 goto fail;
1871
1872 /* Driver takes ownership of state on successful commit. */
1873 return 0;
1874fail:
1875 if (ret == -EDEADLK)
1876 goto backoff;
1877
1878 drm_atomic_state_free(state);
1879
1880 return ret;
1881backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001882 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001883 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001884
1885 /*
1886 * Someone might have exchanged the framebuffer while we dropped locks
1887 * in the backoff code. We need to fix up the fb refcount tracking the
1888 * core does for us.
1889 */
1890 crtc->primary->old_fb = crtc->primary->fb;
1891
1892 goto retry;
1893}
1894EXPORT_SYMBOL(drm_atomic_helper_set_config);
1895
Rob Clarkbbb1e522015-08-25 15:35:58 -04001896/* just used from fb-helper and atomic-helper: */
1897int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1898 struct drm_atomic_state *state)
1899{
1900 struct drm_crtc_state *crtc_state;
1901 struct drm_plane_state *primary_state;
1902 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02001903 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001904 int ret;
1905
1906 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1907 if (IS_ERR(crtc_state))
1908 return PTR_ERR(crtc_state);
1909
1910 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1911 if (IS_ERR(primary_state))
1912 return PTR_ERR(primary_state);
1913
1914 if (!set->mode) {
1915 WARN_ON(set->fb);
1916 WARN_ON(set->num_connectors);
1917
1918 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1919 if (ret != 0)
1920 return ret;
1921
1922 crtc_state->active = false;
1923
1924 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1925 if (ret != 0)
1926 return ret;
1927
1928 drm_atomic_set_fb_for_plane(primary_state, NULL);
1929
1930 goto commit;
1931 }
1932
1933 WARN_ON(!set->fb);
1934 WARN_ON(!set->num_connectors);
1935
1936 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1937 if (ret != 0)
1938 return ret;
1939
1940 crtc_state->active = true;
1941
1942 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1943 if (ret != 0)
1944 return ret;
1945
Ville Syrjälä83926112015-11-16 17:02:34 +02001946 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1947
Rob Clarkbbb1e522015-08-25 15:35:58 -04001948 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1949 primary_state->crtc_x = 0;
1950 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02001951 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001952 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001953 primary_state->src_x = set->x << 16;
1954 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001955 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02001956 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001957 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001958 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02001959 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001960 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001961 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04001962
1963commit:
1964 ret = update_output_state(state, set);
1965 if (ret)
1966 return ret;
1967
1968 return 0;
1969}
1970
Daniel Vetter042652e2014-07-27 13:46:52 +02001971/**
Thierry Reding14942762015-12-02 17:50:04 +01001972 * drm_atomic_helper_disable_all - disable all currently active outputs
1973 * @dev: DRM device
1974 * @ctx: lock acquisition context
1975 *
1976 * Loops through all connectors, finding those that aren't turned off and then
1977 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1978 * that they are connected to.
1979 *
1980 * This is used for example in suspend/resume to disable all currently active
1981 * functions when suspending.
1982 *
1983 * Note that if callers haven't already acquired all modeset locks this might
1984 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1985 *
1986 * Returns:
1987 * 0 on success or a negative error code on failure.
1988 *
1989 * See also:
1990 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1991 */
1992int drm_atomic_helper_disable_all(struct drm_device *dev,
1993 struct drm_modeset_acquire_ctx *ctx)
1994{
1995 struct drm_atomic_state *state;
1996 struct drm_connector *conn;
1997 int err;
1998
1999 state = drm_atomic_state_alloc(dev);
2000 if (!state)
2001 return -ENOMEM;
2002
2003 state->acquire_ctx = ctx;
2004
2005 drm_for_each_connector(conn, dev) {
2006 struct drm_crtc *crtc = conn->state->crtc;
2007 struct drm_crtc_state *crtc_state;
2008
2009 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
2010 continue;
2011
2012 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2013 if (IS_ERR(crtc_state)) {
2014 err = PTR_ERR(crtc_state);
2015 goto free;
2016 }
2017
2018 crtc_state->active = false;
2019 }
2020
2021 err = drm_atomic_commit(state);
2022
2023free:
2024 if (err < 0)
2025 drm_atomic_state_free(state);
2026
2027 return err;
2028}
2029EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2030
2031/**
2032 * drm_atomic_helper_suspend - subsystem-level suspend helper
2033 * @dev: DRM device
2034 *
2035 * Duplicates the current atomic state, disables all active outputs and then
2036 * returns a pointer to the original atomic state to the caller. Drivers can
2037 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2038 * restore the output configuration that was active at the time the system
2039 * entered suspend.
2040 *
2041 * Note that it is potentially unsafe to use this. The atomic state object
2042 * returned by this function is assumed to be persistent. Drivers must ensure
2043 * that this holds true. Before calling this function, drivers must make sure
2044 * to suspend fbdev emulation so that nothing can be using the device.
2045 *
2046 * Returns:
2047 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2048 * encoded error code on failure. Drivers should store the returned atomic
2049 * state object and pass it to the drm_atomic_helper_resume() helper upon
2050 * resume.
2051 *
2052 * See also:
2053 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2054 * drm_atomic_helper_resume()
2055 */
2056struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2057{
2058 struct drm_modeset_acquire_ctx ctx;
2059 struct drm_atomic_state *state;
2060 int err;
2061
2062 drm_modeset_acquire_init(&ctx, 0);
2063
2064retry:
2065 err = drm_modeset_lock_all_ctx(dev, &ctx);
2066 if (err < 0) {
2067 state = ERR_PTR(err);
2068 goto unlock;
2069 }
2070
2071 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2072 if (IS_ERR(state))
2073 goto unlock;
2074
2075 err = drm_atomic_helper_disable_all(dev, &ctx);
2076 if (err < 0) {
2077 drm_atomic_state_free(state);
2078 state = ERR_PTR(err);
2079 goto unlock;
2080 }
2081
2082unlock:
2083 if (PTR_ERR(state) == -EDEADLK) {
2084 drm_modeset_backoff(&ctx);
2085 goto retry;
2086 }
2087
2088 drm_modeset_drop_locks(&ctx);
2089 drm_modeset_acquire_fini(&ctx);
2090 return state;
2091}
2092EXPORT_SYMBOL(drm_atomic_helper_suspend);
2093
2094/**
2095 * drm_atomic_helper_resume - subsystem-level resume helper
2096 * @dev: DRM device
2097 * @state: atomic state to resume to
2098 *
2099 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2100 * grabs all modeset locks and commits the atomic state object. This can be
2101 * used in conjunction with the drm_atomic_helper_suspend() helper to
2102 * implement suspend/resume for drivers that support atomic mode-setting.
2103 *
2104 * Returns:
2105 * 0 on success or a negative error code on failure.
2106 *
2107 * See also:
2108 * drm_atomic_helper_suspend()
2109 */
2110int drm_atomic_helper_resume(struct drm_device *dev,
2111 struct drm_atomic_state *state)
2112{
2113 struct drm_mode_config *config = &dev->mode_config;
2114 int err;
2115
2116 drm_mode_config_reset(dev);
2117 drm_modeset_lock_all(dev);
2118 state->acquire_ctx = config->acquire_ctx;
2119 err = drm_atomic_commit(state);
2120 drm_modeset_unlock_all(dev);
2121
2122 return err;
2123}
2124EXPORT_SYMBOL(drm_atomic_helper_resume);
2125
2126/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002127 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002128 * @crtc: DRM crtc
2129 * @property: DRM property
2130 * @val: value of property
2131 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002132 * Provides a default crtc set_property handler using the atomic driver
2133 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002134 *
2135 * RETURNS:
2136 * Zero on success, error code on failure
2137 */
2138int
2139drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2140 struct drm_property *property,
2141 uint64_t val)
2142{
2143 struct drm_atomic_state *state;
2144 struct drm_crtc_state *crtc_state;
2145 int ret = 0;
2146
2147 state = drm_atomic_state_alloc(crtc->dev);
2148 if (!state)
2149 return -ENOMEM;
2150
2151 /* ->set_property is always called with all locks held. */
2152 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2153retry:
2154 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2155 if (IS_ERR(crtc_state)) {
2156 ret = PTR_ERR(crtc_state);
2157 goto fail;
2158 }
2159
Rob Clark40ecc692014-12-18 16:01:46 -05002160 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2161 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002162 if (ret)
2163 goto fail;
2164
2165 ret = drm_atomic_commit(state);
2166 if (ret != 0)
2167 goto fail;
2168
2169 /* Driver takes ownership of state on successful commit. */
2170 return 0;
2171fail:
2172 if (ret == -EDEADLK)
2173 goto backoff;
2174
2175 drm_atomic_state_free(state);
2176
2177 return ret;
2178backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002179 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002180 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002181
2182 goto retry;
2183}
2184EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2185
2186/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002187 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002188 * @plane: DRM plane
2189 * @property: DRM property
2190 * @val: value of property
2191 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002192 * Provides a default plane set_property handler using the atomic driver
2193 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002194 *
2195 * RETURNS:
2196 * Zero on success, error code on failure
2197 */
2198int
2199drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2200 struct drm_property *property,
2201 uint64_t val)
2202{
2203 struct drm_atomic_state *state;
2204 struct drm_plane_state *plane_state;
2205 int ret = 0;
2206
2207 state = drm_atomic_state_alloc(plane->dev);
2208 if (!state)
2209 return -ENOMEM;
2210
2211 /* ->set_property is always called with all locks held. */
2212 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2213retry:
2214 plane_state = drm_atomic_get_plane_state(state, plane);
2215 if (IS_ERR(plane_state)) {
2216 ret = PTR_ERR(plane_state);
2217 goto fail;
2218 }
2219
Rob Clark40ecc692014-12-18 16:01:46 -05002220 ret = drm_atomic_plane_set_property(plane, plane_state,
2221 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002222 if (ret)
2223 goto fail;
2224
2225 ret = drm_atomic_commit(state);
2226 if (ret != 0)
2227 goto fail;
2228
2229 /* Driver takes ownership of state on successful commit. */
2230 return 0;
2231fail:
2232 if (ret == -EDEADLK)
2233 goto backoff;
2234
2235 drm_atomic_state_free(state);
2236
2237 return ret;
2238backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002239 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002240 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002241
2242 goto retry;
2243}
2244EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2245
2246/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002247 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002248 * @connector: DRM connector
2249 * @property: DRM property
2250 * @val: value of property
2251 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002252 * Provides a default connector set_property handler using the atomic driver
2253 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002254 *
2255 * RETURNS:
2256 * Zero on success, error code on failure
2257 */
2258int
2259drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2260 struct drm_property *property,
2261 uint64_t val)
2262{
2263 struct drm_atomic_state *state;
2264 struct drm_connector_state *connector_state;
2265 int ret = 0;
2266
2267 state = drm_atomic_state_alloc(connector->dev);
2268 if (!state)
2269 return -ENOMEM;
2270
2271 /* ->set_property is always called with all locks held. */
2272 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2273retry:
2274 connector_state = drm_atomic_get_connector_state(state, connector);
2275 if (IS_ERR(connector_state)) {
2276 ret = PTR_ERR(connector_state);
2277 goto fail;
2278 }
2279
Rob Clark40ecc692014-12-18 16:01:46 -05002280 ret = drm_atomic_connector_set_property(connector, connector_state,
2281 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002282 if (ret)
2283 goto fail;
2284
2285 ret = drm_atomic_commit(state);
2286 if (ret != 0)
2287 goto fail;
2288
2289 /* Driver takes ownership of state on successful commit. */
2290 return 0;
2291fail:
2292 if (ret == -EDEADLK)
2293 goto backoff;
2294
2295 drm_atomic_state_free(state);
2296
2297 return ret;
2298backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002299 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002300 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002301
2302 goto retry;
2303}
2304EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002305
2306/**
2307 * drm_atomic_helper_page_flip - execute a legacy page flip
2308 * @crtc: DRM crtc
2309 * @fb: DRM framebuffer
2310 * @event: optional DRM event to signal upon completion
2311 * @flags: flip flags for non-vblank sync'ed updates
2312 *
2313 * Provides a default page flip implementation using the atomic driver interface.
2314 *
2315 * Note that for now so called async page flips (i.e. updates which are not
2316 * synchronized to vblank) are not supported, since the atomic interfaces have
2317 * no provisions for this yet.
2318 *
2319 * Returns:
2320 * Returns 0 on success, negative errno numbers on failure.
2321 */
2322int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2323 struct drm_framebuffer *fb,
2324 struct drm_pending_vblank_event *event,
2325 uint32_t flags)
2326{
2327 struct drm_plane *plane = crtc->primary;
2328 struct drm_atomic_state *state;
2329 struct drm_plane_state *plane_state;
2330 struct drm_crtc_state *crtc_state;
2331 int ret = 0;
2332
2333 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2334 return -EINVAL;
2335
2336 state = drm_atomic_state_alloc(plane->dev);
2337 if (!state)
2338 return -ENOMEM;
2339
2340 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2341retry:
2342 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2343 if (IS_ERR(crtc_state)) {
2344 ret = PTR_ERR(crtc_state);
2345 goto fail;
2346 }
2347 crtc_state->event = event;
2348
2349 plane_state = drm_atomic_get_plane_state(state, plane);
2350 if (IS_ERR(plane_state)) {
2351 ret = PTR_ERR(plane_state);
2352 goto fail;
2353 }
2354
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002355 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002356 if (ret != 0)
2357 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002358 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002359
Daniel Vetter4cba6852015-12-08 09:49:20 +01002360 /* Make sure we don't accidentally do a full modeset. */
2361 state->allow_modeset = false;
2362 if (!crtc_state->active) {
2363 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2364 crtc->base.id);
2365 ret = -EINVAL;
2366 goto fail;
2367 }
2368
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002369 ret = drm_atomic_async_commit(state);
2370 if (ret != 0)
2371 goto fail;
2372
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002373 /* Driver takes ownership of state on successful async commit. */
2374 return 0;
2375fail:
2376 if (ret == -EDEADLK)
2377 goto backoff;
2378
2379 drm_atomic_state_free(state);
2380
2381 return ret;
2382backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002383 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002384 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002385
2386 /*
2387 * Someone might have exchanged the framebuffer while we dropped locks
2388 * in the backoff code. We need to fix up the fb refcount tracking the
2389 * core does for us.
2390 */
2391 plane->old_fb = plane->fb;
2392
2393 goto retry;
2394}
2395EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002396
2397/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002398 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2399 * @connector: affected connector
2400 * @mode: DPMS mode
2401 *
2402 * This is the main helper function provided by the atomic helper framework for
2403 * implementing the legacy DPMS connector interface. It computes the new desired
2404 * ->active state for the corresponding CRTC (if the connector is enabled) and
2405 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002406 *
2407 * Returns:
2408 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002409 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002410int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2411 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002412{
2413 struct drm_mode_config *config = &connector->dev->mode_config;
2414 struct drm_atomic_state *state;
2415 struct drm_crtc_state *crtc_state;
2416 struct drm_crtc *crtc;
2417 struct drm_connector *tmp_connector;
2418 int ret;
2419 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002420 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002421
2422 if (mode != DRM_MODE_DPMS_ON)
2423 mode = DRM_MODE_DPMS_OFF;
2424
2425 connector->dpms = mode;
2426 crtc = connector->state->crtc;
2427
2428 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002429 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002430
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002431 state = drm_atomic_state_alloc(connector->dev);
2432 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002433 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002434
2435 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2436retry:
2437 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002438 if (IS_ERR(crtc_state)) {
2439 ret = PTR_ERR(crtc_state);
2440 goto fail;
2441 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002442
2443 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2444
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002445 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002446 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002447 continue;
2448
John Hunter0388df02015-03-17 15:30:28 +08002449 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002450 active = true;
2451 break;
2452 }
2453 }
2454 crtc_state->active = active;
2455
2456 ret = drm_atomic_commit(state);
2457 if (ret != 0)
2458 goto fail;
2459
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002460 /* Driver takes ownership of state on successful commit. */
2461 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002462fail:
2463 if (ret == -EDEADLK)
2464 goto backoff;
2465
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002466 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002467 drm_atomic_state_free(state);
2468
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002469 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002470backoff:
2471 drm_atomic_state_clear(state);
2472 drm_atomic_legacy_backoff(state);
2473
2474 goto retry;
2475}
2476EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2477
2478/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002479 * DOC: atomic state reset and initialization
2480 *
2481 * Both the drm core and the atomic helpers assume that there is always the full
2482 * and correct atomic software state for all connectors, CRTCs and planes
2483 * available. Which is a bit a problem on driver load and also after system
2484 * suspend. One way to solve this is to have a hardware state read-out
2485 * infrastructure which reconstructs the full software state (e.g. the i915
2486 * driver).
2487 *
2488 * The simpler solution is to just reset the software state to everything off,
2489 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2490 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01002491 *
2492 * On the upside the precise state tracking of atomic simplifies system suspend
2493 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2494 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2495 * For other drivers the building blocks are split out, see the documentation
2496 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002497 */
2498
2499/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002500 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2501 * @crtc: drm CRTC
2502 *
2503 * Resets the atomic state for @crtc by freeing the state pointer (which might
2504 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2505 */
2506void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2507{
Markus Elfring5f911902015-11-06 12:03:46 +01002508 if (crtc->state)
Daniel Stone99cf4a22015-05-25 19:11:51 +01002509 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002510 kfree(crtc->state);
2511 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002512
2513 if (crtc->state)
2514 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002515}
2516EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2517
2518/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002519 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2520 * @crtc: CRTC object
2521 * @state: atomic CRTC state
2522 *
2523 * Copies atomic state from a CRTC's current state and resets inferred values.
2524 * This is useful for drivers that subclass the CRTC state.
2525 */
2526void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2527 struct drm_crtc_state *state)
2528{
2529 memcpy(state, crtc->state, sizeof(*state));
2530
Daniel Stone99cf4a22015-05-25 19:11:51 +01002531 if (state->mode_blob)
2532 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002533 state->mode_changed = false;
2534 state->active_changed = false;
2535 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002536 state->connectors_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002537 state->event = NULL;
2538}
2539EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2540
2541/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002542 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2543 * @crtc: drm CRTC
2544 *
2545 * Default CRTC state duplicate hook for drivers which don't have their own
2546 * subclassed CRTC state structure.
2547 */
2548struct drm_crtc_state *
2549drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2550{
2551 struct drm_crtc_state *state;
2552
2553 if (WARN_ON(!crtc->state))
2554 return NULL;
2555
Thierry Redingf5e78402015-01-28 14:54:32 +01002556 state = kmalloc(sizeof(*state), GFP_KERNEL);
2557 if (state)
2558 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002559
2560 return state;
2561}
2562EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2563
2564/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002565 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2566 * @crtc: CRTC object
2567 * @state: CRTC state object to release
2568 *
2569 * Releases all resources stored in the CRTC state without actually freeing
2570 * the memory of the CRTC state. This is useful for drivers that subclass the
2571 * CRTC state.
2572 */
2573void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2574 struct drm_crtc_state *state)
2575{
Markus Elfring5f911902015-11-06 12:03:46 +01002576 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002577}
2578EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2579
2580/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002581 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2582 * @crtc: drm CRTC
2583 * @state: CRTC state object to release
2584 *
2585 * Default CRTC state destroy hook for drivers which don't have their own
2586 * subclassed CRTC state structure.
2587 */
2588void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2589 struct drm_crtc_state *state)
2590{
Thierry Redingf5e78402015-01-28 14:54:32 +01002591 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002592 kfree(state);
2593}
2594EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2595
2596/**
2597 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2598 * @plane: drm plane
2599 *
2600 * Resets the atomic state for @plane by freeing the state pointer (which might
2601 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2602 */
2603void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2604{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002605 if (plane->state && plane->state->fb)
2606 drm_framebuffer_unreference(plane->state->fb);
2607
Daniel Vetterd4617012014-11-03 15:56:43 +01002608 kfree(plane->state);
2609 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002610
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01002611 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002612 plane->state->plane = plane;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01002613 plane->state->rotation = BIT(DRM_ROTATE_0);
2614 }
Daniel Vetterd4617012014-11-03 15:56:43 +01002615}
2616EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2617
2618/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002619 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2620 * @plane: plane object
2621 * @state: atomic plane state
2622 *
2623 * Copies atomic state from a plane's current state. This is useful for
2624 * drivers that subclass the plane state.
2625 */
2626void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2627 struct drm_plane_state *state)
2628{
2629 memcpy(state, plane->state, sizeof(*state));
2630
2631 if (state->fb)
2632 drm_framebuffer_reference(state->fb);
2633}
2634EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2635
2636/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002637 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2638 * @plane: drm plane
2639 *
2640 * Default plane state duplicate hook for drivers which don't have their own
2641 * subclassed plane state structure.
2642 */
2643struct drm_plane_state *
2644drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2645{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002646 struct drm_plane_state *state;
2647
Daniel Vetterd4617012014-11-03 15:56:43 +01002648 if (WARN_ON(!plane->state))
2649 return NULL;
2650
Thierry Redingf5e78402015-01-28 14:54:32 +01002651 state = kmalloc(sizeof(*state), GFP_KERNEL);
2652 if (state)
2653 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002654
2655 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002656}
2657EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2658
2659/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002660 * __drm_atomic_helper_plane_destroy_state - release plane state
2661 * @plane: plane object
2662 * @state: plane state object to release
2663 *
2664 * Releases all resources stored in the plane state without actually freeing
2665 * the memory of the plane state. This is useful for drivers that subclass the
2666 * plane state.
2667 */
2668void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2669 struct drm_plane_state *state)
2670{
2671 if (state->fb)
2672 drm_framebuffer_unreference(state->fb);
2673}
2674EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2675
2676/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002677 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2678 * @plane: drm plane
2679 * @state: plane state object to release
2680 *
2681 * Default plane state destroy hook for drivers which don't have their own
2682 * subclassed plane state structure.
2683 */
2684void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002685 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002686{
Thierry Redingf5e78402015-01-28 14:54:32 +01002687 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002688 kfree(state);
2689}
2690EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2691
2692/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002693 * __drm_atomic_helper_connector_reset - reset state on connector
2694 * @connector: drm connector
2695 * @conn_state: connector state to assign
2696 *
2697 * Initializes the newly allocated @conn_state and assigns it to
2698 * #connector ->state, usually required when initializing the drivers
2699 * or when called from the ->reset hook.
2700 *
2701 * This is useful for drivers that subclass the connector state.
2702 */
2703void
2704__drm_atomic_helper_connector_reset(struct drm_connector *connector,
2705 struct drm_connector_state *conn_state)
2706{
2707 if (conn_state)
2708 conn_state->connector = connector;
2709
2710 connector->state = conn_state;
2711}
2712EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
2713
2714/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002715 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2716 * @connector: drm connector
2717 *
2718 * Resets the atomic state for @connector by freeing the state pointer (which
2719 * might be NULL, e.g. at driver load time) and allocating a new empty state
2720 * object.
2721 */
2722void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2723{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002724 struct drm_connector_state *conn_state =
2725 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002726
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002727 kfree(connector->state);
2728 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002729}
2730EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2731
2732/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002733 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2734 * @connector: connector object
2735 * @state: atomic connector state
2736 *
2737 * Copies atomic state from a connector's current state. This is useful for
2738 * drivers that subclass the connector state.
2739 */
2740void
2741__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2742 struct drm_connector_state *state)
2743{
2744 memcpy(state, connector->state, sizeof(*state));
2745}
2746EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2747
2748/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002749 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2750 * @connector: drm connector
2751 *
2752 * Default connector state duplicate hook for drivers which don't have their own
2753 * subclassed connector state structure.
2754 */
2755struct drm_connector_state *
2756drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2757{
Thierry Redingf5e78402015-01-28 14:54:32 +01002758 struct drm_connector_state *state;
2759
Daniel Vetterd4617012014-11-03 15:56:43 +01002760 if (WARN_ON(!connector->state))
2761 return NULL;
2762
Thierry Redingf5e78402015-01-28 14:54:32 +01002763 state = kmalloc(sizeof(*state), GFP_KERNEL);
2764 if (state)
2765 __drm_atomic_helper_connector_duplicate_state(connector, state);
2766
2767 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002768}
2769EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2770
2771/**
Thierry Reding397fd772015-09-08 15:00:45 +02002772 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2773 * @dev: DRM device
2774 * @ctx: lock acquisition context
2775 *
2776 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01002777 * duplicating their respective states. This is used for example by suspend/
2778 * resume support code to save the state prior to suspend such that it can
2779 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02002780 *
2781 * Note that this treats atomic state as persistent between save and restore.
2782 * Drivers must make sure that this is possible and won't result in confusion
2783 * or erroneous behaviour.
2784 *
2785 * Note that if callers haven't already acquired all modeset locks this might
2786 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2787 *
2788 * Returns:
2789 * A pointer to the copy of the atomic state object on success or an
2790 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01002791 *
2792 * See also:
2793 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02002794 */
2795struct drm_atomic_state *
2796drm_atomic_helper_duplicate_state(struct drm_device *dev,
2797 struct drm_modeset_acquire_ctx *ctx)
2798{
2799 struct drm_atomic_state *state;
2800 struct drm_connector *conn;
2801 struct drm_plane *plane;
2802 struct drm_crtc *crtc;
2803 int err = 0;
2804
2805 state = drm_atomic_state_alloc(dev);
2806 if (!state)
2807 return ERR_PTR(-ENOMEM);
2808
2809 state->acquire_ctx = ctx;
2810
2811 drm_for_each_crtc(crtc, dev) {
2812 struct drm_crtc_state *crtc_state;
2813
2814 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2815 if (IS_ERR(crtc_state)) {
2816 err = PTR_ERR(crtc_state);
2817 goto free;
2818 }
2819 }
2820
2821 drm_for_each_plane(plane, dev) {
2822 struct drm_plane_state *plane_state;
2823
2824 plane_state = drm_atomic_get_plane_state(state, plane);
2825 if (IS_ERR(plane_state)) {
2826 err = PTR_ERR(plane_state);
2827 goto free;
2828 }
2829 }
2830
2831 drm_for_each_connector(conn, dev) {
2832 struct drm_connector_state *conn_state;
2833
2834 conn_state = drm_atomic_get_connector_state(state, conn);
2835 if (IS_ERR(conn_state)) {
2836 err = PTR_ERR(conn_state);
2837 goto free;
2838 }
2839 }
2840
2841 /* clear the acquire context so that it isn't accidentally reused */
2842 state->acquire_ctx = NULL;
2843
2844free:
2845 if (err < 0) {
2846 drm_atomic_state_free(state);
2847 state = ERR_PTR(err);
2848 }
2849
2850 return state;
2851}
2852EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2853
2854/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002855 * __drm_atomic_helper_connector_destroy_state - release connector state
2856 * @connector: connector object
2857 * @state: connector state object to release
2858 *
2859 * Releases all resources stored in the connector state without actually
2860 * freeing the memory of the connector state. This is useful for drivers that
2861 * subclass the connector state.
2862 */
2863void
2864__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2865 struct drm_connector_state *state)
2866{
2867 /*
2868 * This is currently a placeholder so that drivers that subclass the
2869 * state will automatically do the right thing if code is ever added
2870 * to this function.
2871 */
2872}
2873EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2874
2875/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002876 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2877 * @connector: drm connector
2878 * @state: connector state object to release
2879 *
2880 * Default connector state destroy hook for drivers which don't have their own
2881 * subclassed connector state structure.
2882 */
2883void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2884 struct drm_connector_state *state)
2885{
Thierry Redingf5e78402015-01-28 14:54:32 +01002886 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002887 kfree(state);
2888}
2889EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);