blob: 32f6f57fc66ea79c718eea093f8f31c5b804ff2b [file] [log] [blame]
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28#include <drm/drmP.h>
29#include <drm/drm_atomic.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020032#include <drm/drm_atomic_helper.h>
Daniel Vettere2330f02014-10-29 11:34:56 +010033#include <linux/fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Daniel Vetter3150c7d2014-11-06 20:53:29 +010035/**
36 * DOC: overview
37 *
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
43 *
44 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020045 * drm_atomic_helper_check() and for the commit callback with
46 * drm_atomic_helper_commit(). But the individual stages and callbacks are
47 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010048 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020051 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
52 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010053 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010055 *
56 * The atomic helper uses the same function table structures as all other
57 * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58 * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59 * also shares the struct &drm_plane_helper_funcs function table with the plane
60 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010061 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010062static void
63drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 struct drm_plane_state *plane_state,
65 struct drm_plane *plane)
66{
67 struct drm_crtc_state *crtc_state;
68
69 if (plane->state->crtc) {
Rob Clark4b08eae2014-12-08 10:45:43 -050070 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
Daniel Vetterc2fcd272014-11-05 00:14:14 +010071
72 if (WARN_ON(!crtc_state))
73 return;
74
75 crtc_state->planes_changed = true;
76 }
77
78 if (plane_state->crtc) {
79 crtc_state =
80 state->crtc_states[drm_crtc_index(plane_state->crtc)];
81
82 if (WARN_ON(!crtc_state))
83 return;
84
85 crtc_state->planes_changed = true;
86 }
87}
88
Daniel Vetter97ac3202015-12-03 10:49:14 +010089static bool
90check_pending_encoder_assignment(struct drm_atomic_state *state,
Daniel Vetter07bad492015-12-08 09:49:18 +010091 struct drm_encoder *new_encoder)
Daniel Vetter97ac3202015-12-03 10:49:14 +010092{
93 struct drm_connector *connector;
94 struct drm_connector_state *conn_state;
95 int i;
96
97 for_each_connector_in_state(state, connector, conn_state, i) {
98 if (conn_state->best_encoder != new_encoder)
99 continue;
100
101 /* encoder already assigned and we're trying to re-steal it! */
102 if (connector->state->best_encoder != conn_state->best_encoder)
103 return false;
104 }
105
106 return true;
107}
108
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100109static void
110set_best_encoder(struct drm_atomic_state *state,
111 struct drm_connector_state *conn_state,
112 struct drm_encoder *encoder)
113{
114 struct drm_crtc_state *crtc_state;
115 struct drm_crtc *crtc;
116
117 if (conn_state->best_encoder) {
118 /* Unset the encoder_mask in the old crtc state. */
119 crtc = conn_state->connector->state->crtc;
120
121 /* A NULL crtc is an error here because we should have
122 * duplicated a NULL best_encoder when crtc was NULL.
123 * As an exception restoring duplicated atomic state
124 * during resume is allowed, so don't warn when
125 * best_encoder is equal to encoder we intend to set.
126 */
127 WARN_ON(!crtc && encoder != conn_state->best_encoder);
128 if (crtc) {
129 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
130
131 crtc_state->encoder_mask &=
132 ~(1 << drm_encoder_index(conn_state->best_encoder));
133 }
134 }
135
136 if (encoder) {
137 crtc = conn_state->crtc;
138 WARN_ON(!crtc);
139 if (crtc) {
140 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
141
142 crtc_state->encoder_mask |=
143 1 << drm_encoder_index(encoder);
144 }
145 }
146
147 conn_state->best_encoder = encoder;
148}
149
Daniel Vetter623369e2014-09-16 17:50:47 +0200150static int
151steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100152 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200153{
Daniel Vetter623369e2014-09-16 17:50:47 +0200154 struct drm_crtc_state *crtc_state;
155 struct drm_connector *connector;
156 struct drm_connector_state *connector_state;
157
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100158 drm_for_each_connector(connector, state->dev) {
159 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200160
Daniel Vetter623369e2014-09-16 17:50:47 +0200161 if (connector->state->best_encoder != encoder)
162 continue;
163
Daniel Vetter623369e2014-09-16 17:50:47 +0200164 connector_state = drm_atomic_get_connector_state(state,
165 connector);
166 if (IS_ERR(connector_state))
167 return PTR_ERR(connector_state);
168
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100169 if (connector_state->best_encoder != encoder)
170 continue;
171
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100172 encoder_crtc = connector->state->crtc;
173
174 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
175 encoder->base.id, encoder->name,
176 encoder_crtc->base.id, encoder_crtc->name);
177
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100178 set_best_encoder(state, connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100179
180 crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
181 crtc_state->connectors_changed = true;
182
183 return 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200184 }
185
186 return 0;
187}
188
189static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100190update_connector_routing(struct drm_atomic_state *state,
191 struct drm_connector *connector,
192 struct drm_connector_state *connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200193{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200194 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200195 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200196 struct drm_crtc_state *crtc_state;
197 int idx, ret;
198
Daniel Vetter17a38d92015-02-22 12:24:16 +0100199 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
200 connector->base.id,
201 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200202
203 if (connector->state->crtc != connector_state->crtc) {
204 if (connector->state->crtc) {
205 idx = drm_crtc_index(connector->state->crtc);
206
207 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200208 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200209 }
210
211 if (connector_state->crtc) {
212 idx = drm_crtc_index(connector_state->crtc);
213
214 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200215 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200216 }
217 }
218
219 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100220 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200221 connector->base.id,
222 connector->name);
223
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100224 set_best_encoder(state, connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200225
226 return 0;
227 }
228
229 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200230
231 if (funcs->atomic_best_encoder)
232 new_encoder = funcs->atomic_best_encoder(connector,
233 connector_state);
234 else
235 new_encoder = funcs->best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200236
237 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100238 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
239 connector->base.id,
240 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200241 return -EINVAL;
242 }
243
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100244 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
245 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
246 new_encoder->base.id,
247 new_encoder->name,
248 connector_state->crtc->base.id);
249 return -EINVAL;
250 }
251
Daniel Vetter623369e2014-09-16 17:50:47 +0200252 if (new_encoder == connector_state->best_encoder) {
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100253 set_best_encoder(state, connector_state, new_encoder);
254
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200255 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100256 connector->base.id,
257 connector->name,
258 new_encoder->base.id,
259 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200260 connector_state->crtc->base.id,
261 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200262
263 return 0;
264 }
265
Daniel Vetter07bad492015-12-08 09:49:18 +0100266 if (!check_pending_encoder_assignment(state, new_encoder)) {
Daniel Vetter97ac3202015-12-03 10:49:14 +0100267 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
268 connector->base.id,
269 connector->name);
270 return -EINVAL;
271 }
272
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100273 ret = steal_encoder(state, new_encoder);
274 if (ret) {
275 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
276 connector->base.id,
277 connector->name);
278 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200279 }
280
Daniel Vetter6ea76f3c2015-08-03 17:24:11 +0200281 if (WARN_ON(!connector_state->crtc))
282 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200283
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100284 set_best_encoder(state, connector_state, new_encoder);
285
Daniel Vetter623369e2014-09-16 17:50:47 +0200286 idx = drm_crtc_index(connector_state->crtc);
287
288 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200289 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200290
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200291 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100292 connector->base.id,
293 connector->name,
294 new_encoder->base.id,
295 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200296 connector_state->crtc->base.id,
297 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200298
299 return 0;
300}
301
302static int
303mode_fixup(struct drm_atomic_state *state)
304{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300305 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200306 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300307 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200308 struct drm_connector_state *conn_state;
309 int i;
310 bool ret;
311
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300312 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200313 if (!crtc_state->mode_changed &&
314 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200315 continue;
316
317 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
318 }
319
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300320 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200321 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200322 struct drm_encoder *encoder;
323
Daniel Vetter623369e2014-09-16 17:50:47 +0200324 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
325
326 if (!conn_state->crtc || !conn_state->best_encoder)
327 continue;
328
329 crtc_state =
330 state->crtc_states[drm_crtc_index(conn_state->crtc)];
331
332 /*
333 * Each encoder has at most one connector (since we always steal
334 * it away), so we won't call ->mode_fixup twice.
335 */
336 encoder = conn_state->best_encoder;
337 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300338 if (!funcs)
339 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200340
Archit Taneja862e6862015-05-21 11:03:16 +0530341 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
342 &crtc_state->adjusted_mode);
343 if (!ret) {
344 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
345 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200346 }
347
Thierry Reding4cd4df82014-12-03 16:44:34 +0100348 if (funcs->atomic_check) {
349 ret = funcs->atomic_check(encoder, crtc_state,
350 conn_state);
351 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100352 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
353 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100354 return ret;
355 }
Inki Dae84524912015-08-11 21:23:49 +0900356 } else if (funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100357 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
358 &crtc_state->adjusted_mode);
359 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100360 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
361 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100362 return -EINVAL;
363 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200364 }
365 }
366
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300367 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200368 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200369
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200370 if (!crtc_state->mode_changed &&
371 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200372 continue;
373
374 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300375 if (!funcs->mode_fixup)
376 continue;
377
Daniel Vetter623369e2014-09-16 17:50:47 +0200378 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
379 &crtc_state->adjusted_mode);
380 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200381 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
382 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200383 return -EINVAL;
384 }
385 }
386
387 return 0;
388}
389
Daniel Vetterd9b13622014-11-26 16:57:41 +0100390/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800391 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100392 * @dev: DRM device
393 * @state: the driver state object
394 *
395 * Check the state object to see if the requested state is physically possible.
396 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200397 * update and adds any additional connectors needed for full modesets and calls
398 * down into ->mode_fixup functions of the driver backend.
399 *
400 * crtc_state->mode_changed is set when the input mode is changed.
401 * crtc_state->connectors_changed is set when a connector is added or
402 * removed from the crtc.
403 * crtc_state->active_changed is set when crtc_state->active changes,
404 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100405 *
406 * IMPORTANT:
407 *
408 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
409 * plane update can't be done without a full modeset) _must_ call this function
410 * afterwards after that change. It is permitted to call this function multiple
411 * times for the same update, e.g. when the ->atomic_check functions depend upon
412 * the adjusted dotclock for fifo space allocation and watermark computation.
413 *
414 * RETURNS
415 * Zero for success or -errno
416 */
417int
Rob Clark934ce1c2014-11-19 16:41:33 -0500418drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200419 struct drm_atomic_state *state)
420{
Daniel Vetter623369e2014-09-16 17:50:47 +0200421 struct drm_crtc *crtc;
422 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300423 struct drm_connector *connector;
424 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200425 int i, ret;
426
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300427 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200428 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200429 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
430 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200431 crtc_state->mode_changed = true;
432 }
433
434 if (crtc->state->enable != crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200435 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
436 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200437
438 /*
439 * For clarity this assignment is done here, but
440 * enable == 0 is only true when there are no
441 * connectors and a NULL mode.
442 *
443 * The other way around is true as well. enable != 0
444 * iff connectors are attached and a mode is set.
445 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200446 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200447 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200448 }
449 }
450
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300451 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200452 /*
453 * This only sets crtc->mode_changed for routing changes,
454 * drivers must set crtc->mode_changed themselves when connector
455 * properties need to be updated.
456 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100457 ret = update_connector_routing(state, connector,
458 connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200459 if (ret)
460 return ret;
461 }
462
463 /*
464 * After all the routing has been prepared we need to add in any
465 * connector which is itself unchanged, but who's crtc changes it's
466 * configuration. This must be done before calling mode_fixup in case a
467 * crtc only changed its mode but has the same set of connectors.
468 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300469 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100470 bool has_connectors =
471 !!crtc_state->connector_mask;
Daniel Vetter623369e2014-09-16 17:50:47 +0200472
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100473 /*
474 * We must set ->active_changed after walking connectors for
475 * otherwise an update that only changes active would result in
476 * a full modeset because update_connector_routing force that.
477 */
478 if (crtc->state->active != crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200479 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
480 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100481 crtc_state->active_changed = true;
482 }
483
Daniel Vetter2465ff62015-06-18 09:58:55 +0200484 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100485 continue;
486
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200487 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
488 crtc->base.id, crtc->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100489 crtc_state->enable ? 'y' : 'n',
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200490 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200491
492 ret = drm_atomic_add_affected_connectors(state, crtc);
493 if (ret != 0)
494 return ret;
495
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200496 ret = drm_atomic_add_affected_planes(state, crtc);
497 if (ret != 0)
498 return ret;
499
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100500 if (crtc_state->enable != has_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200501 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
502 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200503
504 return -EINVAL;
505 }
506 }
507
508 return mode_fixup(state);
509}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100510EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200511
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100512/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800513 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100514 * @dev: DRM device
515 * @state: the driver state object
516 *
517 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100518 * This does all the plane update related checks using by calling into the
519 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100520 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200521 * It also sets crtc_state->planes_changed to indicate that a crtc has
522 * updated planes.
523 *
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100524 * RETURNS
525 * Zero for success or -errno
526 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100527int
528drm_atomic_helper_check_planes(struct drm_device *dev,
529 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100530{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300531 struct drm_crtc *crtc;
532 struct drm_crtc_state *crtc_state;
533 struct drm_plane *plane;
534 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100535 int i, ret = 0;
536
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300537 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200538 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100539
540 funcs = plane->helper_private;
541
542 drm_atomic_helper_plane_changed(state, plane_state, plane);
543
544 if (!funcs || !funcs->atomic_check)
545 continue;
546
547 ret = funcs->atomic_check(plane, plane_state);
548 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200549 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
550 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100551 return ret;
552 }
553 }
554
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300555 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200556 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100557
558 funcs = crtc->helper_private;
559
560 if (!funcs || !funcs->atomic_check)
561 continue;
562
563 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
564 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200565 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
566 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100567 return ret;
568 }
569 }
570
Daniel Vetterd9b13622014-11-26 16:57:41 +0100571 return ret;
572}
573EXPORT_SYMBOL(drm_atomic_helper_check_planes);
574
575/**
576 * drm_atomic_helper_check - validate state object
577 * @dev: DRM device
578 * @state: the driver state object
579 *
580 * Check the state object to see if the requested state is physically possible.
581 * Only crtcs and planes have check callbacks, so for any additional (global)
582 * checking that a driver needs it can simply wrap that around this function.
583 * Drivers without such needs can directly use this as their ->atomic_check()
584 * callback.
585 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100586 * This just wraps the two parts of the state checking for planes and modeset
587 * state in the default order: First it calls drm_atomic_helper_check_modeset()
588 * and then drm_atomic_helper_check_planes(). The assumption is that the
589 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
590 * e.g. properly compute watermarks.
591 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100592 * RETURNS
593 * Zero for success or -errno
594 */
595int drm_atomic_helper_check(struct drm_device *dev,
596 struct drm_atomic_state *state)
597{
598 int ret;
599
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100600 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100601 if (ret)
602 return ret;
603
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100604 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500605 if (ret)
606 return ret;
607
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100608 return ret;
609}
610EXPORT_SYMBOL(drm_atomic_helper_check);
611
Daniel Vetter623369e2014-09-16 17:50:47 +0200612static void
613disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
614{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300615 struct drm_connector *connector;
616 struct drm_connector_state *old_conn_state;
617 struct drm_crtc *crtc;
618 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200619 int i;
620
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300621 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200622 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200623 struct drm_encoder *encoder;
624
Daniel Vetter623369e2014-09-16 17:50:47 +0200625 /* Shut down everything that's in the changeset and currently
626 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300627 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200628 continue;
629
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100630 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
631
Daniel Vetter4218a322015-03-26 22:18:40 +0100632 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200633 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100634 continue;
635
Rob Clark46df9ad2014-11-20 15:40:36 -0500636 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200637
Rob Clark46df9ad2014-11-20 15:40:36 -0500638 /* We shouldn't get this far if we didn't previously have
639 * an encoder.. but WARN_ON() rather than explode.
640 */
641 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200642 continue;
643
644 funcs = encoder->helper_private;
645
Daniel Vetter17a38d92015-02-22 12:24:16 +0100646 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
647 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100648
Daniel Vetter623369e2014-09-16 17:50:47 +0200649 /*
650 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800651 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200652 */
Archit Taneja862e6862015-05-21 11:03:16 +0530653 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200654
655 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100656 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200657 funcs->prepare(encoder);
658 else if (funcs->disable)
659 funcs->disable(encoder);
660 else
661 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
662
Archit Taneja862e6862015-05-21 11:03:16 +0530663 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200664 }
665
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300666 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200667 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200668
669 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200670 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100671 continue;
672
673 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200674 continue;
675
676 funcs = crtc->helper_private;
677
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200678 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
679 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100680
681
Daniel Vetter623369e2014-09-16 17:50:47 +0200682 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100683 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200684 funcs->prepare(crtc);
685 else if (funcs->disable)
686 funcs->disable(crtc);
687 else
688 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
689 }
690}
691
Daniel Vetter4c18d302015-05-12 15:27:37 +0200692/**
693 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
694 * @dev: DRM device
695 * @old_state: atomic state object with old state structures
696 *
697 * This function updates all the various legacy modeset state pointers in
698 * connectors, encoders and crtcs. It also updates the timestamping constants
699 * used for precise vblank timestamps by calling
700 * drm_calc_timestamping_constants().
701 *
702 * Drivers can use this for building their own atomic commit if they don't have
703 * a pure helper-based modeset implementation.
704 */
705void
706drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
707 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200708{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300709 struct drm_connector *connector;
710 struct drm_connector_state *old_conn_state;
711 struct drm_crtc *crtc;
712 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200713 int i;
714
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200715 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300716 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200717 if (connector->encoder) {
718 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200719
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200720 connector->encoder->crtc = NULL;
721 connector->encoder = NULL;
722 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200723
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200724 crtc = connector->state->crtc;
725 if ((!crtc && old_conn_state->crtc) ||
726 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
727 struct drm_property *dpms_prop =
728 dev->mode_config.dpms_property;
729 int mode = DRM_MODE_DPMS_OFF;
730
731 if (crtc && crtc->state->active)
732 mode = DRM_MODE_DPMS_ON;
733
734 connector->dpms = mode;
735 drm_object_property_set_value(&connector->base,
736 dpms_prop, mode);
737 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200738 }
739
740 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300741 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
742 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200743 continue;
744
745 if (WARN_ON(!connector->state->best_encoder))
746 continue;
747
748 connector->encoder = connector->state->best_encoder;
749 connector->encoder->crtc = connector->state->crtc;
750 }
751
752 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300753 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200754 struct drm_plane *primary = crtc->primary;
755
Daniel Vetter623369e2014-09-16 17:50:47 +0200756 crtc->mode = crtc->state->mode;
757 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200758
759 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
760 primary->state->crtc == crtc) {
761 crtc->x = primary->state->src_x >> 16;
762 crtc->y = primary->state->src_y >> 16;
763 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200764
765 if (crtc->state->enable)
766 drm_calc_timestamping_constants(crtc,
767 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200768 }
769}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200770EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200771
772static void
773crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
774{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300775 struct drm_crtc *crtc;
776 struct drm_crtc_state *old_crtc_state;
777 struct drm_connector *connector;
778 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200779 int i;
780
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300781 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200782 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200783
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300784 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200785 continue;
786
787 funcs = crtc->helper_private;
788
Daniel Vetterc982bd92015-02-22 12:24:20 +0100789 if (crtc->state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200790 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
791 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100792
Daniel Vetter623369e2014-09-16 17:50:47 +0200793 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100794 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200795 }
796
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300797 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200798 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200799 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200800 struct drm_encoder *encoder;
801 struct drm_display_mode *mode, *adjusted_mode;
802
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300803 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200804 continue;
805
806 encoder = connector->state->best_encoder;
807 funcs = encoder->helper_private;
808 new_crtc_state = connector->state->crtc->state;
809 mode = &new_crtc_state->mode;
810 adjusted_mode = &new_crtc_state->adjusted_mode;
811
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100812 if (!new_crtc_state->mode_changed)
813 continue;
814
Daniel Vetter17a38d92015-02-22 12:24:16 +0100815 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
816 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100817
Daniel Vetter623369e2014-09-16 17:50:47 +0200818 /*
819 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800820 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200821 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100822 if (funcs->mode_set)
823 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200824
Archit Taneja862e6862015-05-21 11:03:16 +0530825 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200826 }
827}
828
829/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100830 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200831 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100832 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200833 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100834 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200835 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100836 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300837 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100838 * drm_atomic_helper_commit_planes(), which is what the default commit function
839 * does. But drivers with different needs can group the modeset commits together
840 * and do the plane commits at the end. This is useful for drivers doing runtime
841 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200842 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100843void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
844 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200845{
Laurent Pincharta072f802015-02-22 12:24:18 +0100846 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200847
848 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
849
Laurent Pincharta072f802015-02-22 12:24:18 +0100850 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200851}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100852EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200853
854/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100855 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200856 * @dev: DRM device
857 * @old_state: atomic state object with old state structures
858 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100859 * This function enables all the outputs with the new configuration which had to
860 * be turned off for the update.
861 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300862 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100863 * drm_atomic_helper_commit_planes(), which is what the default commit function
864 * does. But drivers with different needs can group the modeset commits together
865 * and do the plane commits at the end. This is useful for drivers doing runtime
866 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200867 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100868void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
869 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200870{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300871 struct drm_crtc *crtc;
872 struct drm_crtc_state *old_crtc_state;
873 struct drm_connector *connector;
874 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200875 int i;
876
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300877 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200878 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200879
880 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200881 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100882 continue;
883
884 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200885 continue;
886
887 funcs = crtc->helper_private;
888
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100889 if (crtc->state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200890 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
891 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100892
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100893 if (funcs->enable)
894 funcs->enable(crtc);
895 else
896 funcs->commit(crtc);
897 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200898 }
899
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300900 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200901 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200902 struct drm_encoder *encoder;
903
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300904 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200905 continue;
906
Daniel Vetter4218a322015-03-26 22:18:40 +0100907 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200908 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100909 continue;
910
Daniel Vetter623369e2014-09-16 17:50:47 +0200911 encoder = connector->state->best_encoder;
912 funcs = encoder->helper_private;
913
Daniel Vetter17a38d92015-02-22 12:24:16 +0100914 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
915 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100916
Daniel Vetter623369e2014-09-16 17:50:47 +0200917 /*
918 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800919 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200920 */
Archit Taneja862e6862015-05-21 11:03:16 +0530921 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200922
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100923 if (funcs->enable)
924 funcs->enable(encoder);
925 else
926 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200927
Archit Taneja862e6862015-05-21 11:03:16 +0530928 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200929 }
930}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100931EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200932
Daniel Vettere2330f02014-10-29 11:34:56 +0100933static void wait_for_fences(struct drm_device *dev,
934 struct drm_atomic_state *state)
935{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300936 struct drm_plane *plane;
937 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +0100938 int i;
939
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300940 for_each_plane_in_state(state, plane, plane_state, i) {
941 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +0100942 continue;
943
944 WARN_ON(!plane->state->fb);
945
946 fence_wait(plane->state->fence, false);
947 fence_put(plane->state->fence);
948 plane->state->fence = NULL;
949 }
950}
951
John Keepingc2409062016-01-19 10:46:58 +0000952/**
953 * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
954 * @dev: DRM device
955 * @old_state: atomic state object with old state structures
956 * @crtc: DRM crtc
957 *
958 * Checks whether the framebuffer used for this CRTC changes as a result of
959 * the atomic update. This is useful for drivers which cannot use
960 * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
961 * functionality.
962 *
963 * Returns:
964 * true if the framebuffer changed.
965 */
966bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
967 struct drm_atomic_state *old_state,
968 struct drm_crtc *crtc)
Daniel Vetterab58e332014-11-24 20:42:42 +0100969{
970 struct drm_plane *plane;
971 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +0100972 int i;
973
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300974 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +0100975 if (plane->state->crtc != crtc &&
976 old_plane_state->crtc != crtc)
977 continue;
978
979 if (plane->state->fb != old_plane_state->fb)
980 return true;
981 }
982
983 return false;
984}
John Keepingc2409062016-01-19 10:46:58 +0000985EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
Daniel Vetterab58e332014-11-24 20:42:42 +0100986
Rob Clark5ee32292014-11-11 19:38:59 -0500987/**
988 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
989 * @dev: DRM device
990 * @old_state: atomic state object with old state structures
991 *
992 * Helper to, after atomic commit, wait for vblanks on all effected
993 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100994 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
995 * framebuffers have actually changed to optimize for the legacy cursor and
996 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500997 */
998void
999drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1000 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001001{
1002 struct drm_crtc *crtc;
1003 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001004 int i, ret;
1005
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001006 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +02001007 /* No one cares about the old state, so abuse it for tracking
1008 * and store whether we hold a vblank reference (and should do a
1009 * vblank wait) in the ->enable boolean. */
1010 old_crtc_state->enable = false;
1011
1012 if (!crtc->state->enable)
1013 continue;
1014
Daniel Vetterf02ad902015-01-22 16:36:23 +01001015 /* Legacy cursor ioctls are completely unsynced, and userspace
1016 * relies on that (by doing tons of cursor updates). */
1017 if (old_state->legacy_cursor_update)
1018 continue;
1019
John Keepingc2409062016-01-19 10:46:58 +00001020 if (!drm_atomic_helper_framebuffer_changed(dev,
1021 old_state, crtc))
Daniel Vetterab58e332014-11-24 20:42:42 +01001022 continue;
1023
Daniel Vetter623369e2014-09-16 17:50:47 +02001024 ret = drm_crtc_vblank_get(crtc);
1025 if (ret != 0)
1026 continue;
1027
1028 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +02001029 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001030 }
1031
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001032 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1033 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +02001034 continue;
1035
1036 ret = wait_event_timeout(dev->vblank[i].queue,
1037 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001038 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001039 msecs_to_jiffies(50));
1040
1041 drm_crtc_vblank_put(crtc);
1042 }
1043}
Rob Clark5ee32292014-11-11 19:38:59 -05001044EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001045
1046/**
1047 * drm_atomic_helper_commit - commit validated state object
1048 * @dev: DRM device
1049 * @state: the driver state object
1050 * @async: asynchronous commit
1051 *
1052 * This function commits a with drm_atomic_helper_check() pre-validated state
1053 * object. This can still fail when e.g. the framebuffer reservation fails. For
1054 * now this doesn't implement asynchronous commits.
1055 *
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001056 * Note that right now this function does not support async commits, and hence
1057 * driver writers must implement their own version for now. Also note that the
1058 * default ordering of how the various stages are called is to match the legacy
1059 * modeset helper library closest. One peculiarity of that is that it doesn't
1060 * mesh well with runtime PM at all.
1061 *
1062 * For drivers supporting runtime PM the recommended sequence is
1063 *
1064 * drm_atomic_helper_commit_modeset_disables(dev, state);
1065 *
1066 * drm_atomic_helper_commit_modeset_enables(dev, state);
1067 *
1068 * drm_atomic_helper_commit_planes(dev, state, true);
1069 *
1070 * See the kerneldoc entries for these three functions for more details.
1071 *
Daniel Vetter623369e2014-09-16 17:50:47 +02001072 * RETURNS
1073 * Zero for success or -errno.
1074 */
1075int drm_atomic_helper_commit(struct drm_device *dev,
1076 struct drm_atomic_state *state,
1077 bool async)
1078{
1079 int ret;
1080
1081 if (async)
1082 return -EBUSY;
1083
1084 ret = drm_atomic_helper_prepare_planes(dev, state);
1085 if (ret)
1086 return ret;
1087
1088 /*
1089 * This is the point of no return - everything below never fails except
1090 * when the hw goes bonghits. Which means we can commit the new state on
1091 * the software side now.
1092 */
1093
1094 drm_atomic_helper_swap_state(dev, state);
1095
1096 /*
1097 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001098 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001099 * that the asynchronous work has either been cancelled (if the driver
1100 * supports it, which at least requires that the framebuffers get
1101 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1102 * before the new state gets committed on the software side with
1103 * drm_atomic_helper_swap_state().
1104 *
1105 * This scheme allows new atomic state updates to be prepared and
1106 * checked in parallel to the asynchronous completion of the previous
1107 * update. Which is important since compositors need to figure out the
1108 * composition of the next frame right after having submitted the
1109 * current layout.
1110 */
1111
Daniel Vettere2330f02014-10-29 11:34:56 +01001112 wait_for_fences(dev, state);
1113
Daniel Vetter1af434a2015-02-22 12:24:19 +01001114 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001115
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001116 drm_atomic_helper_commit_planes(dev, state, false);
Daniel Vetter623369e2014-09-16 17:50:47 +02001117
Daniel Vetter1af434a2015-02-22 12:24:19 +01001118 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001119
Rob Clark5ee32292014-11-11 19:38:59 -05001120 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001121
1122 drm_atomic_helper_cleanup_planes(dev, state);
1123
1124 drm_atomic_state_free(state);
1125
1126 return 0;
1127}
1128EXPORT_SYMBOL(drm_atomic_helper_commit);
1129
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001130/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001131 * DOC: implementing async commit
1132 *
1133 * For now the atomic helpers don't support async commit directly. If there is
1134 * real need it could be added though, using the dma-buf fence infrastructure
1135 * for generic synchronization with outstanding rendering.
1136 *
1137 * For now drivers have to implement async commit themselves, with the following
1138 * sequence being the recommended one:
1139 *
1140 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1141 * which commit needs to call which can fail, so we want to run it first and
1142 * synchronously.
1143 *
1144 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1145 * might be affected the new state update. This can be done by either cancelling
1146 * or flushing the work items, depending upon whether the driver can deal with
1147 * cancelled updates. Note that it is important to ensure that the framebuffer
1148 * cleanup is still done when cancelling.
1149 *
1150 * For sufficient parallelism it is recommended to have a work item per crtc
1151 * (for updates which don't touch global state) and a global one. Then we only
1152 * need to synchronize with the crtc work items for changed crtcs and the global
1153 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1154 *
1155 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001156 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001157 * locks means concurrent callers never see inconsistent state. And doing this
1158 * while it's guaranteed that no relevant async worker runs means that async
1159 * workers do not need grab any locks. Actually they must not grab locks, for
1160 * otherwise the work flushing will deadlock.
1161 *
1162 * 4. Schedule a work item to do all subsequent steps, using the split-out
1163 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1164 * then cleaning up the framebuffers after the old framebuffer is no longer
1165 * being displayed.
1166 */
1167
1168/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001169 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001170 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001171 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001172 *
1173 * This function prepares plane state, specifically framebuffers, for the new
1174 * configuration. If any failure is encountered this function will call
1175 * ->cleanup_fb on any already successfully prepared framebuffer.
1176 *
1177 * Returns:
1178 * 0 on success, negative error code on failure.
1179 */
1180int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1181 struct drm_atomic_state *state)
1182{
1183 int nplanes = dev->mode_config.num_total_plane;
1184 int ret, i;
1185
1186 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001187 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001188 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001189 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001190
1191 if (!plane)
1192 continue;
1193
1194 funcs = plane->helper_private;
1195
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001196 if (funcs->prepare_fb) {
1197 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001198 if (ret)
1199 goto fail;
1200 }
1201 }
1202
1203 return 0;
1204
1205fail:
1206 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001207 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001208 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001209 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001210
1211 if (!plane)
1212 continue;
1213
1214 funcs = plane->helper_private;
1215
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001216 if (funcs->cleanup_fb)
1217 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001218
1219 }
1220
1221 return ret;
1222}
1223EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1224
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001225bool plane_crtc_active(struct drm_plane_state *state)
1226{
1227 return state->crtc && state->crtc->state->active;
1228}
1229
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001230/**
1231 * drm_atomic_helper_commit_planes - commit plane state
1232 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001233 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001234 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001235 *
1236 * This function commits the new plane state using the plane and atomic helper
1237 * functions for planes and crtcs. It assumes that the atomic state has already
1238 * been pushed into the relevant object state pointers, since this step can no
1239 * longer fail.
1240 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001241 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001242 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001243 *
1244 * Note that this function does all plane updates across all CRTCs in one step.
1245 * If the hardware can't support this approach look at
1246 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001247 *
1248 * Plane parameters can be updated by applications while the associated CRTC is
1249 * disabled. The DRM/KMS core will store the parameters in the plane state,
1250 * which will be available to the driver when the CRTC is turned on. As a result
1251 * most drivers don't need to be immediately notified of plane updates for a
1252 * disabled CRTC.
1253 *
1254 * Unless otherwise needed, drivers are advised to set the @active_only
1255 * parameters to true in order not to receive plane update notifications related
1256 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1257 * driver code when the driver and/or hardware can't or just don't need to deal
1258 * with updates on disabled CRTCs, for example when supporting runtime PM.
1259 *
1260 * The drm_atomic_helper_commit() default implementation only sets @active_only
1261 * to false to most closely match the behaviour of the legacy helpers. This should
1262 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001263 */
1264void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001265 struct drm_atomic_state *old_state,
1266 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001267{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001268 struct drm_crtc *crtc;
1269 struct drm_crtc_state *old_crtc_state;
1270 struct drm_plane *plane;
1271 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001272 int i;
1273
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001274 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001275 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001276
1277 funcs = crtc->helper_private;
1278
1279 if (!funcs || !funcs->atomic_begin)
1280 continue;
1281
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001282 if (active_only && !crtc->state->active)
1283 continue;
1284
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001285 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001286 }
1287
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001288 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001289 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001290 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001291
1292 funcs = plane->helper_private;
1293
Thierry Reding3cad4b62014-11-25 13:05:12 +01001294 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001295 continue;
1296
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001297 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1298
1299 if (active_only) {
1300 /*
1301 * Skip planes related to inactive CRTCs. If the plane
1302 * is enabled use the state of the current CRTC. If the
1303 * plane is being disabled use the state of the old
1304 * CRTC to avoid skipping planes being disabled on an
1305 * active CRTC.
1306 */
1307 if (!disabling && !plane_crtc_active(plane->state))
1308 continue;
1309 if (disabling && !plane_crtc_active(old_plane_state))
1310 continue;
1311 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001312
Thierry Reding407b8bd2014-11-20 12:05:50 +01001313 /*
1314 * Special-case disabling the plane if drivers support it.
1315 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001316 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001317 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001318 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001319 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001320 }
1321
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001322 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001323 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001324
1325 funcs = crtc->helper_private;
1326
1327 if (!funcs || !funcs->atomic_flush)
1328 continue;
1329
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001330 if (active_only && !crtc->state->active)
1331 continue;
1332
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001333 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001334 }
1335}
1336EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1337
1338/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001339 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1340 * @old_crtc_state: atomic state object with the old crtc state
1341 *
1342 * This function commits the new plane state using the plane and atomic helper
1343 * functions for planes on the specific crtc. It assumes that the atomic state
1344 * has already been pushed into the relevant object state pointers, since this
1345 * step can no longer fail.
1346 *
1347 * This function is useful when plane updates should be done crtc-by-crtc
1348 * instead of one global step like drm_atomic_helper_commit_planes() does.
1349 *
1350 * This function can only be savely used when planes are not allowed to move
1351 * between different CRTCs because this function doesn't handle inter-CRTC
1352 * depencies. Callers need to ensure that either no such depencies exist,
1353 * resolve them through ordering of commit calls or through some other means.
1354 */
1355void
1356drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1357{
1358 const struct drm_crtc_helper_funcs *crtc_funcs;
1359 struct drm_crtc *crtc = old_crtc_state->crtc;
1360 struct drm_atomic_state *old_state = old_crtc_state->state;
1361 struct drm_plane *plane;
1362 unsigned plane_mask;
1363
1364 plane_mask = old_crtc_state->plane_mask;
1365 plane_mask |= crtc->state->plane_mask;
1366
1367 crtc_funcs = crtc->helper_private;
1368 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001369 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001370
1371 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1372 struct drm_plane_state *old_plane_state =
1373 drm_atomic_get_existing_plane_state(old_state, plane);
1374 const struct drm_plane_helper_funcs *plane_funcs;
1375
1376 plane_funcs = plane->helper_private;
1377
1378 if (!old_plane_state || !plane_funcs)
1379 continue;
1380
1381 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1382
1383 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1384 plane_funcs->atomic_disable)
1385 plane_funcs->atomic_disable(plane, old_plane_state);
1386 else if (plane->state->crtc ||
1387 drm_atomic_plane_disabling(plane, old_plane_state))
1388 plane_funcs->atomic_update(plane, old_plane_state);
1389 }
1390
1391 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001392 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001393}
1394EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1395
1396/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001397 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1398 * @crtc: CRTC
1399 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1400 *
1401 * Disables all planes associated with the given CRTC. This can be
1402 * used for instance in the CRTC helper disable callback to disable
1403 * all planes before shutting down the display pipeline.
1404 *
1405 * If the atomic-parameter is set the function calls the CRTC's
1406 * atomic_begin hook before and atomic_flush hook after disabling the
1407 * planes.
1408 *
1409 * It is a bug to call this function without having implemented the
1410 * ->atomic_disable() plane hook.
1411 */
1412void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1413 bool atomic)
1414{
1415 const struct drm_crtc_helper_funcs *crtc_funcs =
1416 crtc->helper_private;
1417 struct drm_plane *plane;
1418
1419 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1420 crtc_funcs->atomic_begin(crtc, NULL);
1421
1422 drm_for_each_plane(plane, crtc->dev) {
1423 const struct drm_plane_helper_funcs *plane_funcs =
1424 plane->helper_private;
1425
1426 if (plane->state->crtc != crtc || !plane_funcs)
1427 continue;
1428
1429 WARN_ON(!plane_funcs->atomic_disable);
1430 if (plane_funcs->atomic_disable)
1431 plane_funcs->atomic_disable(plane, NULL);
1432 }
1433
1434 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1435 crtc_funcs->atomic_flush(crtc, NULL);
1436}
1437EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1438
1439/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001440 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1441 * @dev: DRM device
1442 * @old_state: atomic state object with old state structures
1443 *
1444 * This function cleans up plane state, specifically framebuffers, from the old
1445 * configuration. Hence the old configuration must be perserved in @old_state to
1446 * be able to call this function.
1447 *
1448 * This function must also be called on the new state when the atomic update
1449 * fails at any point after calling drm_atomic_helper_prepare_planes().
1450 */
1451void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1452 struct drm_atomic_state *old_state)
1453{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001454 struct drm_plane *plane;
1455 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001456 int i;
1457
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001458 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001459 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001460
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001461 funcs = plane->helper_private;
1462
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001463 if (funcs->cleanup_fb)
1464 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001465 }
1466}
1467EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1468
1469/**
1470 * drm_atomic_helper_swap_state - store atomic state into current sw state
1471 * @dev: DRM device
1472 * @state: atomic state
1473 *
1474 * This function stores the atomic state into the current state pointers in all
1475 * driver objects. It should be called after all failing steps have been done
1476 * and succeeded, but before the actual hardware state is committed.
1477 *
1478 * For cleanup and error recovery the current state for all changed objects will
1479 * be swaped into @state.
1480 *
1481 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1482 *
1483 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1484 *
1485 * 2. Do any other steps that might fail.
1486 *
1487 * 3. Put the staged state into the current state pointers with this function.
1488 *
1489 * 4. Actually commit the hardware state.
1490 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001491 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001492 * contains the old state. Also do any other cleanup required with that state.
1493 */
1494void drm_atomic_helper_swap_state(struct drm_device *dev,
1495 struct drm_atomic_state *state)
1496{
1497 int i;
1498
1499 for (i = 0; i < dev->mode_config.num_connector; i++) {
1500 struct drm_connector *connector = state->connectors[i];
1501
1502 if (!connector)
1503 continue;
1504
1505 connector->state->state = state;
1506 swap(state->connector_states[i], connector->state);
1507 connector->state->state = NULL;
1508 }
1509
1510 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1511 struct drm_crtc *crtc = state->crtcs[i];
1512
1513 if (!crtc)
1514 continue;
1515
1516 crtc->state->state = state;
1517 swap(state->crtc_states[i], crtc->state);
1518 crtc->state->state = NULL;
1519 }
1520
1521 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1522 struct drm_plane *plane = state->planes[i];
1523
1524 if (!plane)
1525 continue;
1526
1527 plane->state->state = state;
1528 swap(state->plane_states[i], plane->state);
1529 plane->state->state = NULL;
1530 }
1531}
1532EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001533
1534/**
1535 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1536 * @plane: plane object to update
1537 * @crtc: owning CRTC of owning plane
1538 * @fb: framebuffer to flip onto plane
1539 * @crtc_x: x offset of primary plane on crtc
1540 * @crtc_y: y offset of primary plane on crtc
1541 * @crtc_w: width of primary plane rectangle on crtc
1542 * @crtc_h: height of primary plane rectangle on crtc
1543 * @src_x: x offset of @fb for panning
1544 * @src_y: y offset of @fb for panning
1545 * @src_w: width of source rectangle in @fb
1546 * @src_h: height of source rectangle in @fb
1547 *
1548 * Provides a default plane update handler using the atomic driver interface.
1549 *
1550 * RETURNS:
1551 * Zero on success, error code on failure
1552 */
1553int drm_atomic_helper_update_plane(struct drm_plane *plane,
1554 struct drm_crtc *crtc,
1555 struct drm_framebuffer *fb,
1556 int crtc_x, int crtc_y,
1557 unsigned int crtc_w, unsigned int crtc_h,
1558 uint32_t src_x, uint32_t src_y,
1559 uint32_t src_w, uint32_t src_h)
1560{
1561 struct drm_atomic_state *state;
1562 struct drm_plane_state *plane_state;
1563 int ret = 0;
1564
1565 state = drm_atomic_state_alloc(plane->dev);
1566 if (!state)
1567 return -ENOMEM;
1568
1569 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1570retry:
1571 plane_state = drm_atomic_get_plane_state(state, plane);
1572 if (IS_ERR(plane_state)) {
1573 ret = PTR_ERR(plane_state);
1574 goto fail;
1575 }
1576
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001577 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001578 if (ret != 0)
1579 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001580 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001581 plane_state->crtc_x = crtc_x;
1582 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001583 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001584 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001585 plane_state->src_x = src_x;
1586 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001587 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001588 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001589
Daniel Vetter3671c582015-05-04 15:40:52 +02001590 if (plane == crtc->cursor)
1591 state->legacy_cursor_update = true;
1592
Daniel Vetter042652e2014-07-27 13:46:52 +02001593 ret = drm_atomic_commit(state);
1594 if (ret != 0)
1595 goto fail;
1596
1597 /* Driver takes ownership of state on successful commit. */
1598 return 0;
1599fail:
1600 if (ret == -EDEADLK)
1601 goto backoff;
1602
1603 drm_atomic_state_free(state);
1604
1605 return ret;
1606backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001607 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001608 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001609
1610 /*
1611 * Someone might have exchanged the framebuffer while we dropped locks
1612 * in the backoff code. We need to fix up the fb refcount tracking the
1613 * core does for us.
1614 */
1615 plane->old_fb = plane->fb;
1616
1617 goto retry;
1618}
1619EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1620
1621/**
1622 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1623 * @plane: plane to disable
1624 *
1625 * Provides a default plane disable handler using the atomic driver interface.
1626 *
1627 * RETURNS:
1628 * Zero on success, error code on failure
1629 */
1630int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1631{
1632 struct drm_atomic_state *state;
1633 struct drm_plane_state *plane_state;
1634 int ret = 0;
1635
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001636 /*
1637 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1638 * acquire context. The real fix will be to wire the acquire ctx through
1639 * everywhere we need it, but meanwhile prevent chaos by just skipping
1640 * this noop. The critical case is the cursor ioctls which a) only grab
1641 * crtc/cursor-plane locks (so we need the crtc to get at the right
1642 * acquire context) and b) can try to disable the plane multiple times.
1643 */
1644 if (!plane->crtc)
1645 return 0;
1646
Daniel Vetter042652e2014-07-27 13:46:52 +02001647 state = drm_atomic_state_alloc(plane->dev);
1648 if (!state)
1649 return -ENOMEM;
1650
1651 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1652retry:
1653 plane_state = drm_atomic_get_plane_state(state, plane);
1654 if (IS_ERR(plane_state)) {
1655 ret = PTR_ERR(plane_state);
1656 goto fail;
1657 }
1658
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01001659 if (plane_state->crtc && (plane == plane->crtc->cursor))
1660 plane_state->state->legacy_cursor_update = true;
1661
Rob Clarkbbb1e522015-08-25 15:35:58 -04001662 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001663 if (ret != 0)
1664 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01001665
Daniel Vetter042652e2014-07-27 13:46:52 +02001666 ret = drm_atomic_commit(state);
1667 if (ret != 0)
1668 goto fail;
1669
1670 /* Driver takes ownership of state on successful commit. */
1671 return 0;
1672fail:
1673 if (ret == -EDEADLK)
1674 goto backoff;
1675
1676 drm_atomic_state_free(state);
1677
1678 return ret;
1679backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001680 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001681 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001682
1683 /*
1684 * Someone might have exchanged the framebuffer while we dropped locks
1685 * in the backoff code. We need to fix up the fb refcount tracking the
1686 * core does for us.
1687 */
1688 plane->old_fb = plane->fb;
1689
1690 goto retry;
1691}
1692EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1693
Rob Clarkbbb1e522015-08-25 15:35:58 -04001694/* just used from fb-helper and atomic-helper: */
1695int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1696 struct drm_plane_state *plane_state)
1697{
1698 int ret;
1699
1700 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1701 if (ret != 0)
1702 return ret;
1703
1704 drm_atomic_set_fb_for_plane(plane_state, NULL);
1705 plane_state->crtc_x = 0;
1706 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001707 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001708 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001709 plane_state->src_x = 0;
1710 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001711 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001712 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001713
Rob Clarkbbb1e522015-08-25 15:35:58 -04001714 return 0;
1715}
1716
Daniel Vetter042652e2014-07-27 13:46:52 +02001717static int update_output_state(struct drm_atomic_state *state,
1718 struct drm_mode_set *set)
1719{
1720 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001721 struct drm_crtc *crtc;
1722 struct drm_crtc_state *crtc_state;
1723 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001724 struct drm_connector_state *conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001725 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02001726
1727 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1728 state->acquire_ctx);
1729 if (ret)
1730 return ret;
1731
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001732 /* First disable all connectors on the target crtc. */
1733 ret = drm_atomic_add_affected_connectors(state, set->crtc);
1734 if (ret)
1735 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02001736
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001737 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001738 if (conn_state->crtc == set->crtc) {
1739 ret = drm_atomic_set_crtc_for_connector(conn_state,
1740 NULL);
1741 if (ret)
1742 return ret;
1743 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001744 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001745
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01001746 /* Then set all connectors from set->connectors on the target crtc */
1747 for (i = 0; i < set->num_connectors; i++) {
1748 conn_state = drm_atomic_get_connector_state(state,
1749 set->connectors[i]);
1750 if (IS_ERR(conn_state))
1751 return PTR_ERR(conn_state);
1752
1753 ret = drm_atomic_set_crtc_for_connector(conn_state,
1754 set->crtc);
1755 if (ret)
1756 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02001757 }
1758
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001759 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001760 /* Don't update ->enable for the CRTC in the set_config request,
1761 * since a mismatch would indicate a bug in the upper layers.
1762 * The actual modeset code later on will catch any
1763 * inconsistencies here. */
1764 if (crtc == set->crtc)
1765 continue;
1766
Maarten Lankhorst14de6c42016-01-04 12:53:20 +01001767 if (!crtc_state->connector_mask) {
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001768 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1769 NULL);
1770 if (ret < 0)
1771 return ret;
1772
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001773 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001774 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001775 }
1776
1777 return 0;
1778}
1779
1780/**
1781 * drm_atomic_helper_set_config - set a new config from userspace
1782 * @set: mode set configuration
1783 *
1784 * Provides a default crtc set_config handler using the atomic driver interface.
1785 *
1786 * Returns:
1787 * Returns 0 on success, negative errno numbers on failure.
1788 */
1789int drm_atomic_helper_set_config(struct drm_mode_set *set)
1790{
1791 struct drm_atomic_state *state;
1792 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02001793 int ret = 0;
1794
1795 state = drm_atomic_state_alloc(crtc->dev);
1796 if (!state)
1797 return -ENOMEM;
1798
1799 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1800retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04001801 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01001802 if (ret != 0)
1803 goto fail;
1804
Daniel Vetter042652e2014-07-27 13:46:52 +02001805 ret = drm_atomic_commit(state);
1806 if (ret != 0)
1807 goto fail;
1808
1809 /* Driver takes ownership of state on successful commit. */
1810 return 0;
1811fail:
1812 if (ret == -EDEADLK)
1813 goto backoff;
1814
1815 drm_atomic_state_free(state);
1816
1817 return ret;
1818backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001819 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001820 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001821
1822 /*
1823 * Someone might have exchanged the framebuffer while we dropped locks
1824 * in the backoff code. We need to fix up the fb refcount tracking the
1825 * core does for us.
1826 */
1827 crtc->primary->old_fb = crtc->primary->fb;
1828
1829 goto retry;
1830}
1831EXPORT_SYMBOL(drm_atomic_helper_set_config);
1832
Rob Clarkbbb1e522015-08-25 15:35:58 -04001833/* just used from fb-helper and atomic-helper: */
1834int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1835 struct drm_atomic_state *state)
1836{
1837 struct drm_crtc_state *crtc_state;
1838 struct drm_plane_state *primary_state;
1839 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02001840 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001841 int ret;
1842
1843 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1844 if (IS_ERR(crtc_state))
1845 return PTR_ERR(crtc_state);
1846
1847 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1848 if (IS_ERR(primary_state))
1849 return PTR_ERR(primary_state);
1850
1851 if (!set->mode) {
1852 WARN_ON(set->fb);
1853 WARN_ON(set->num_connectors);
1854
1855 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1856 if (ret != 0)
1857 return ret;
1858
1859 crtc_state->active = false;
1860
1861 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1862 if (ret != 0)
1863 return ret;
1864
1865 drm_atomic_set_fb_for_plane(primary_state, NULL);
1866
1867 goto commit;
1868 }
1869
1870 WARN_ON(!set->fb);
1871 WARN_ON(!set->num_connectors);
1872
1873 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1874 if (ret != 0)
1875 return ret;
1876
1877 crtc_state->active = true;
1878
1879 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1880 if (ret != 0)
1881 return ret;
1882
Ville Syrjälä83926112015-11-16 17:02:34 +02001883 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1884
Rob Clarkbbb1e522015-08-25 15:35:58 -04001885 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1886 primary_state->crtc_x = 0;
1887 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02001888 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001889 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001890 primary_state->src_x = set->x << 16;
1891 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001892 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02001893 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001894 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001895 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02001896 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001897 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001898 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04001899
1900commit:
1901 ret = update_output_state(state, set);
1902 if (ret)
1903 return ret;
1904
1905 return 0;
1906}
1907
Daniel Vetter042652e2014-07-27 13:46:52 +02001908/**
Thierry Reding14942762015-12-02 17:50:04 +01001909 * drm_atomic_helper_disable_all - disable all currently active outputs
1910 * @dev: DRM device
1911 * @ctx: lock acquisition context
1912 *
1913 * Loops through all connectors, finding those that aren't turned off and then
1914 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1915 * that they are connected to.
1916 *
1917 * This is used for example in suspend/resume to disable all currently active
1918 * functions when suspending.
1919 *
1920 * Note that if callers haven't already acquired all modeset locks this might
1921 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1922 *
1923 * Returns:
1924 * 0 on success or a negative error code on failure.
1925 *
1926 * See also:
1927 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1928 */
1929int drm_atomic_helper_disable_all(struct drm_device *dev,
1930 struct drm_modeset_acquire_ctx *ctx)
1931{
1932 struct drm_atomic_state *state;
1933 struct drm_connector *conn;
1934 int err;
1935
1936 state = drm_atomic_state_alloc(dev);
1937 if (!state)
1938 return -ENOMEM;
1939
1940 state->acquire_ctx = ctx;
1941
1942 drm_for_each_connector(conn, dev) {
1943 struct drm_crtc *crtc = conn->state->crtc;
1944 struct drm_crtc_state *crtc_state;
1945
1946 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1947 continue;
1948
1949 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1950 if (IS_ERR(crtc_state)) {
1951 err = PTR_ERR(crtc_state);
1952 goto free;
1953 }
1954
1955 crtc_state->active = false;
1956 }
1957
1958 err = drm_atomic_commit(state);
1959
1960free:
1961 if (err < 0)
1962 drm_atomic_state_free(state);
1963
1964 return err;
1965}
1966EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1967
1968/**
1969 * drm_atomic_helper_suspend - subsystem-level suspend helper
1970 * @dev: DRM device
1971 *
1972 * Duplicates the current atomic state, disables all active outputs and then
1973 * returns a pointer to the original atomic state to the caller. Drivers can
1974 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1975 * restore the output configuration that was active at the time the system
1976 * entered suspend.
1977 *
1978 * Note that it is potentially unsafe to use this. The atomic state object
1979 * returned by this function is assumed to be persistent. Drivers must ensure
1980 * that this holds true. Before calling this function, drivers must make sure
1981 * to suspend fbdev emulation so that nothing can be using the device.
1982 *
1983 * Returns:
1984 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1985 * encoded error code on failure. Drivers should store the returned atomic
1986 * state object and pass it to the drm_atomic_helper_resume() helper upon
1987 * resume.
1988 *
1989 * See also:
1990 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1991 * drm_atomic_helper_resume()
1992 */
1993struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1994{
1995 struct drm_modeset_acquire_ctx ctx;
1996 struct drm_atomic_state *state;
1997 int err;
1998
1999 drm_modeset_acquire_init(&ctx, 0);
2000
2001retry:
2002 err = drm_modeset_lock_all_ctx(dev, &ctx);
2003 if (err < 0) {
2004 state = ERR_PTR(err);
2005 goto unlock;
2006 }
2007
2008 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2009 if (IS_ERR(state))
2010 goto unlock;
2011
2012 err = drm_atomic_helper_disable_all(dev, &ctx);
2013 if (err < 0) {
2014 drm_atomic_state_free(state);
2015 state = ERR_PTR(err);
2016 goto unlock;
2017 }
2018
2019unlock:
2020 if (PTR_ERR(state) == -EDEADLK) {
2021 drm_modeset_backoff(&ctx);
2022 goto retry;
2023 }
2024
2025 drm_modeset_drop_locks(&ctx);
2026 drm_modeset_acquire_fini(&ctx);
2027 return state;
2028}
2029EXPORT_SYMBOL(drm_atomic_helper_suspend);
2030
2031/**
2032 * drm_atomic_helper_resume - subsystem-level resume helper
2033 * @dev: DRM device
2034 * @state: atomic state to resume to
2035 *
2036 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2037 * grabs all modeset locks and commits the atomic state object. This can be
2038 * used in conjunction with the drm_atomic_helper_suspend() helper to
2039 * implement suspend/resume for drivers that support atomic mode-setting.
2040 *
2041 * Returns:
2042 * 0 on success or a negative error code on failure.
2043 *
2044 * See also:
2045 * drm_atomic_helper_suspend()
2046 */
2047int drm_atomic_helper_resume(struct drm_device *dev,
2048 struct drm_atomic_state *state)
2049{
2050 struct drm_mode_config *config = &dev->mode_config;
2051 int err;
2052
2053 drm_mode_config_reset(dev);
2054 drm_modeset_lock_all(dev);
2055 state->acquire_ctx = config->acquire_ctx;
2056 err = drm_atomic_commit(state);
2057 drm_modeset_unlock_all(dev);
2058
2059 return err;
2060}
2061EXPORT_SYMBOL(drm_atomic_helper_resume);
2062
2063/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002064 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002065 * @crtc: DRM crtc
2066 * @property: DRM property
2067 * @val: value of property
2068 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002069 * Provides a default crtc set_property handler using the atomic driver
2070 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002071 *
2072 * RETURNS:
2073 * Zero on success, error code on failure
2074 */
2075int
2076drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2077 struct drm_property *property,
2078 uint64_t val)
2079{
2080 struct drm_atomic_state *state;
2081 struct drm_crtc_state *crtc_state;
2082 int ret = 0;
2083
2084 state = drm_atomic_state_alloc(crtc->dev);
2085 if (!state)
2086 return -ENOMEM;
2087
2088 /* ->set_property is always called with all locks held. */
2089 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2090retry:
2091 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2092 if (IS_ERR(crtc_state)) {
2093 ret = PTR_ERR(crtc_state);
2094 goto fail;
2095 }
2096
Rob Clark40ecc692014-12-18 16:01:46 -05002097 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2098 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002099 if (ret)
2100 goto fail;
2101
2102 ret = drm_atomic_commit(state);
2103 if (ret != 0)
2104 goto fail;
2105
2106 /* Driver takes ownership of state on successful commit. */
2107 return 0;
2108fail:
2109 if (ret == -EDEADLK)
2110 goto backoff;
2111
2112 drm_atomic_state_free(state);
2113
2114 return ret;
2115backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002116 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002117 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002118
2119 goto retry;
2120}
2121EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2122
2123/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002124 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002125 * @plane: DRM plane
2126 * @property: DRM property
2127 * @val: value of property
2128 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002129 * Provides a default plane set_property handler using the atomic driver
2130 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002131 *
2132 * RETURNS:
2133 * Zero on success, error code on failure
2134 */
2135int
2136drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2137 struct drm_property *property,
2138 uint64_t val)
2139{
2140 struct drm_atomic_state *state;
2141 struct drm_plane_state *plane_state;
2142 int ret = 0;
2143
2144 state = drm_atomic_state_alloc(plane->dev);
2145 if (!state)
2146 return -ENOMEM;
2147
2148 /* ->set_property is always called with all locks held. */
2149 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2150retry:
2151 plane_state = drm_atomic_get_plane_state(state, plane);
2152 if (IS_ERR(plane_state)) {
2153 ret = PTR_ERR(plane_state);
2154 goto fail;
2155 }
2156
Rob Clark40ecc692014-12-18 16:01:46 -05002157 ret = drm_atomic_plane_set_property(plane, plane_state,
2158 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002159 if (ret)
2160 goto fail;
2161
2162 ret = drm_atomic_commit(state);
2163 if (ret != 0)
2164 goto fail;
2165
2166 /* Driver takes ownership of state on successful commit. */
2167 return 0;
2168fail:
2169 if (ret == -EDEADLK)
2170 goto backoff;
2171
2172 drm_atomic_state_free(state);
2173
2174 return ret;
2175backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002176 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002177 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002178
2179 goto retry;
2180}
2181EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2182
2183/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002184 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002185 * @connector: DRM connector
2186 * @property: DRM property
2187 * @val: value of property
2188 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002189 * Provides a default connector set_property handler using the atomic driver
2190 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002191 *
2192 * RETURNS:
2193 * Zero on success, error code on failure
2194 */
2195int
2196drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2197 struct drm_property *property,
2198 uint64_t val)
2199{
2200 struct drm_atomic_state *state;
2201 struct drm_connector_state *connector_state;
2202 int ret = 0;
2203
2204 state = drm_atomic_state_alloc(connector->dev);
2205 if (!state)
2206 return -ENOMEM;
2207
2208 /* ->set_property is always called with all locks held. */
2209 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2210retry:
2211 connector_state = drm_atomic_get_connector_state(state, connector);
2212 if (IS_ERR(connector_state)) {
2213 ret = PTR_ERR(connector_state);
2214 goto fail;
2215 }
2216
Rob Clark40ecc692014-12-18 16:01:46 -05002217 ret = drm_atomic_connector_set_property(connector, connector_state,
2218 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002219 if (ret)
2220 goto fail;
2221
2222 ret = drm_atomic_commit(state);
2223 if (ret != 0)
2224 goto fail;
2225
2226 /* Driver takes ownership of state on successful commit. */
2227 return 0;
2228fail:
2229 if (ret == -EDEADLK)
2230 goto backoff;
2231
2232 drm_atomic_state_free(state);
2233
2234 return ret;
2235backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002236 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002237 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002238
2239 goto retry;
2240}
2241EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002242
2243/**
2244 * drm_atomic_helper_page_flip - execute a legacy page flip
2245 * @crtc: DRM crtc
2246 * @fb: DRM framebuffer
2247 * @event: optional DRM event to signal upon completion
2248 * @flags: flip flags for non-vblank sync'ed updates
2249 *
2250 * Provides a default page flip implementation using the atomic driver interface.
2251 *
2252 * Note that for now so called async page flips (i.e. updates which are not
2253 * synchronized to vblank) are not supported, since the atomic interfaces have
2254 * no provisions for this yet.
2255 *
2256 * Returns:
2257 * Returns 0 on success, negative errno numbers on failure.
2258 */
2259int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2260 struct drm_framebuffer *fb,
2261 struct drm_pending_vblank_event *event,
2262 uint32_t flags)
2263{
2264 struct drm_plane *plane = crtc->primary;
2265 struct drm_atomic_state *state;
2266 struct drm_plane_state *plane_state;
2267 struct drm_crtc_state *crtc_state;
2268 int ret = 0;
2269
2270 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2271 return -EINVAL;
2272
2273 state = drm_atomic_state_alloc(plane->dev);
2274 if (!state)
2275 return -ENOMEM;
2276
2277 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2278retry:
2279 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2280 if (IS_ERR(crtc_state)) {
2281 ret = PTR_ERR(crtc_state);
2282 goto fail;
2283 }
2284 crtc_state->event = event;
2285
2286 plane_state = drm_atomic_get_plane_state(state, plane);
2287 if (IS_ERR(plane_state)) {
2288 ret = PTR_ERR(plane_state);
2289 goto fail;
2290 }
2291
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002292 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002293 if (ret != 0)
2294 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002295 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002296
Daniel Vetter4cba6852015-12-08 09:49:20 +01002297 /* Make sure we don't accidentally do a full modeset. */
2298 state->allow_modeset = false;
2299 if (!crtc_state->active) {
2300 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2301 crtc->base.id);
2302 ret = -EINVAL;
2303 goto fail;
2304 }
2305
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002306 ret = drm_atomic_async_commit(state);
2307 if (ret != 0)
2308 goto fail;
2309
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002310 /* Driver takes ownership of state on successful async commit. */
2311 return 0;
2312fail:
2313 if (ret == -EDEADLK)
2314 goto backoff;
2315
2316 drm_atomic_state_free(state);
2317
2318 return ret;
2319backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002320 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002321 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002322
2323 /*
2324 * Someone might have exchanged the framebuffer while we dropped locks
2325 * in the backoff code. We need to fix up the fb refcount tracking the
2326 * core does for us.
2327 */
2328 plane->old_fb = plane->fb;
2329
2330 goto retry;
2331}
2332EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002333
2334/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002335 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2336 * @connector: affected connector
2337 * @mode: DPMS mode
2338 *
2339 * This is the main helper function provided by the atomic helper framework for
2340 * implementing the legacy DPMS connector interface. It computes the new desired
2341 * ->active state for the corresponding CRTC (if the connector is enabled) and
2342 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002343 *
2344 * Returns:
2345 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002346 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002347int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2348 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002349{
2350 struct drm_mode_config *config = &connector->dev->mode_config;
2351 struct drm_atomic_state *state;
2352 struct drm_crtc_state *crtc_state;
2353 struct drm_crtc *crtc;
2354 struct drm_connector *tmp_connector;
2355 int ret;
2356 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002357 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002358
2359 if (mode != DRM_MODE_DPMS_ON)
2360 mode = DRM_MODE_DPMS_OFF;
2361
2362 connector->dpms = mode;
2363 crtc = connector->state->crtc;
2364
2365 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002366 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002367
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002368 state = drm_atomic_state_alloc(connector->dev);
2369 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002370 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002371
2372 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2373retry:
2374 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002375 if (IS_ERR(crtc_state)) {
2376 ret = PTR_ERR(crtc_state);
2377 goto fail;
2378 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002379
2380 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2381
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002382 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002383 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002384 continue;
2385
John Hunter0388df02015-03-17 15:30:28 +08002386 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002387 active = true;
2388 break;
2389 }
2390 }
2391 crtc_state->active = active;
2392
2393 ret = drm_atomic_commit(state);
2394 if (ret != 0)
2395 goto fail;
2396
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002397 /* Driver takes ownership of state on successful commit. */
2398 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002399fail:
2400 if (ret == -EDEADLK)
2401 goto backoff;
2402
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002403 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002404 drm_atomic_state_free(state);
2405
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002406 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002407backoff:
2408 drm_atomic_state_clear(state);
2409 drm_atomic_legacy_backoff(state);
2410
2411 goto retry;
2412}
2413EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2414
2415/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002416 * DOC: atomic state reset and initialization
2417 *
2418 * Both the drm core and the atomic helpers assume that there is always the full
2419 * and correct atomic software state for all connectors, CRTCs and planes
2420 * available. Which is a bit a problem on driver load and also after system
2421 * suspend. One way to solve this is to have a hardware state read-out
2422 * infrastructure which reconstructs the full software state (e.g. the i915
2423 * driver).
2424 *
2425 * The simpler solution is to just reset the software state to everything off,
2426 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2427 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01002428 *
2429 * On the upside the precise state tracking of atomic simplifies system suspend
2430 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2431 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2432 * For other drivers the building blocks are split out, see the documentation
2433 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002434 */
2435
2436/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002437 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2438 * @crtc: drm CRTC
2439 *
2440 * Resets the atomic state for @crtc by freeing the state pointer (which might
2441 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2442 */
2443void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2444{
Markus Elfring5f911902015-11-06 12:03:46 +01002445 if (crtc->state)
Daniel Stone99cf4a22015-05-25 19:11:51 +01002446 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002447 kfree(crtc->state);
2448 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002449
2450 if (crtc->state)
2451 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002452}
2453EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2454
2455/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002456 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2457 * @crtc: CRTC object
2458 * @state: atomic CRTC state
2459 *
2460 * Copies atomic state from a CRTC's current state and resets inferred values.
2461 * This is useful for drivers that subclass the CRTC state.
2462 */
2463void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2464 struct drm_crtc_state *state)
2465{
2466 memcpy(state, crtc->state, sizeof(*state));
2467
Daniel Stone99cf4a22015-05-25 19:11:51 +01002468 if (state->mode_blob)
2469 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002470 state->mode_changed = false;
2471 state->active_changed = false;
2472 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002473 state->connectors_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002474 state->event = NULL;
2475}
2476EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2477
2478/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002479 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2480 * @crtc: drm CRTC
2481 *
2482 * Default CRTC state duplicate hook for drivers which don't have their own
2483 * subclassed CRTC state structure.
2484 */
2485struct drm_crtc_state *
2486drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2487{
2488 struct drm_crtc_state *state;
2489
2490 if (WARN_ON(!crtc->state))
2491 return NULL;
2492
Thierry Redingf5e78402015-01-28 14:54:32 +01002493 state = kmalloc(sizeof(*state), GFP_KERNEL);
2494 if (state)
2495 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002496
2497 return state;
2498}
2499EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2500
2501/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002502 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2503 * @crtc: CRTC object
2504 * @state: CRTC state object to release
2505 *
2506 * Releases all resources stored in the CRTC state without actually freeing
2507 * the memory of the CRTC state. This is useful for drivers that subclass the
2508 * CRTC state.
2509 */
2510void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2511 struct drm_crtc_state *state)
2512{
Markus Elfring5f911902015-11-06 12:03:46 +01002513 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002514}
2515EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2516
2517/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002518 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2519 * @crtc: drm CRTC
2520 * @state: CRTC state object to release
2521 *
2522 * Default CRTC state destroy hook for drivers which don't have their own
2523 * subclassed CRTC state structure.
2524 */
2525void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2526 struct drm_crtc_state *state)
2527{
Thierry Redingf5e78402015-01-28 14:54:32 +01002528 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002529 kfree(state);
2530}
2531EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2532
2533/**
2534 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2535 * @plane: drm plane
2536 *
2537 * Resets the atomic state for @plane by freeing the state pointer (which might
2538 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2539 */
2540void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2541{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002542 if (plane->state && plane->state->fb)
2543 drm_framebuffer_unreference(plane->state->fb);
2544
Daniel Vetterd4617012014-11-03 15:56:43 +01002545 kfree(plane->state);
2546 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002547
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01002548 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002549 plane->state->plane = plane;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01002550 plane->state->rotation = BIT(DRM_ROTATE_0);
2551 }
Daniel Vetterd4617012014-11-03 15:56:43 +01002552}
2553EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2554
2555/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002556 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2557 * @plane: plane object
2558 * @state: atomic plane state
2559 *
2560 * Copies atomic state from a plane's current state. This is useful for
2561 * drivers that subclass the plane state.
2562 */
2563void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2564 struct drm_plane_state *state)
2565{
2566 memcpy(state, plane->state, sizeof(*state));
2567
2568 if (state->fb)
2569 drm_framebuffer_reference(state->fb);
2570}
2571EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2572
2573/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002574 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2575 * @plane: drm plane
2576 *
2577 * Default plane state duplicate hook for drivers which don't have their own
2578 * subclassed plane state structure.
2579 */
2580struct drm_plane_state *
2581drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2582{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002583 struct drm_plane_state *state;
2584
Daniel Vetterd4617012014-11-03 15:56:43 +01002585 if (WARN_ON(!plane->state))
2586 return NULL;
2587
Thierry Redingf5e78402015-01-28 14:54:32 +01002588 state = kmalloc(sizeof(*state), GFP_KERNEL);
2589 if (state)
2590 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002591
2592 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002593}
2594EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2595
2596/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002597 * __drm_atomic_helper_plane_destroy_state - release plane state
2598 * @plane: plane object
2599 * @state: plane state object to release
2600 *
2601 * Releases all resources stored in the plane state without actually freeing
2602 * the memory of the plane state. This is useful for drivers that subclass the
2603 * plane state.
2604 */
2605void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2606 struct drm_plane_state *state)
2607{
2608 if (state->fb)
2609 drm_framebuffer_unreference(state->fb);
2610}
2611EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2612
2613/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002614 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2615 * @plane: drm plane
2616 * @state: plane state object to release
2617 *
2618 * Default plane state destroy hook for drivers which don't have their own
2619 * subclassed plane state structure.
2620 */
2621void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002622 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002623{
Thierry Redingf5e78402015-01-28 14:54:32 +01002624 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002625 kfree(state);
2626}
2627EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2628
2629/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002630 * __drm_atomic_helper_connector_reset - reset state on connector
2631 * @connector: drm connector
2632 * @conn_state: connector state to assign
2633 *
2634 * Initializes the newly allocated @conn_state and assigns it to
2635 * #connector ->state, usually required when initializing the drivers
2636 * or when called from the ->reset hook.
2637 *
2638 * This is useful for drivers that subclass the connector state.
2639 */
2640void
2641__drm_atomic_helper_connector_reset(struct drm_connector *connector,
2642 struct drm_connector_state *conn_state)
2643{
2644 if (conn_state)
2645 conn_state->connector = connector;
2646
2647 connector->state = conn_state;
2648}
2649EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
2650
2651/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002652 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2653 * @connector: drm connector
2654 *
2655 * Resets the atomic state for @connector by freeing the state pointer (which
2656 * might be NULL, e.g. at driver load time) and allocating a new empty state
2657 * object.
2658 */
2659void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2660{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002661 struct drm_connector_state *conn_state =
2662 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002663
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01002664 kfree(connector->state);
2665 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002666}
2667EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2668
2669/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002670 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2671 * @connector: connector object
2672 * @state: atomic connector state
2673 *
2674 * Copies atomic state from a connector's current state. This is useful for
2675 * drivers that subclass the connector state.
2676 */
2677void
2678__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2679 struct drm_connector_state *state)
2680{
2681 memcpy(state, connector->state, sizeof(*state));
2682}
2683EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2684
2685/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002686 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2687 * @connector: drm connector
2688 *
2689 * Default connector state duplicate hook for drivers which don't have their own
2690 * subclassed connector state structure.
2691 */
2692struct drm_connector_state *
2693drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2694{
Thierry Redingf5e78402015-01-28 14:54:32 +01002695 struct drm_connector_state *state;
2696
Daniel Vetterd4617012014-11-03 15:56:43 +01002697 if (WARN_ON(!connector->state))
2698 return NULL;
2699
Thierry Redingf5e78402015-01-28 14:54:32 +01002700 state = kmalloc(sizeof(*state), GFP_KERNEL);
2701 if (state)
2702 __drm_atomic_helper_connector_duplicate_state(connector, state);
2703
2704 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002705}
2706EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2707
2708/**
Thierry Reding397fd772015-09-08 15:00:45 +02002709 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2710 * @dev: DRM device
2711 * @ctx: lock acquisition context
2712 *
2713 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01002714 * duplicating their respective states. This is used for example by suspend/
2715 * resume support code to save the state prior to suspend such that it can
2716 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02002717 *
2718 * Note that this treats atomic state as persistent between save and restore.
2719 * Drivers must make sure that this is possible and won't result in confusion
2720 * or erroneous behaviour.
2721 *
2722 * Note that if callers haven't already acquired all modeset locks this might
2723 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2724 *
2725 * Returns:
2726 * A pointer to the copy of the atomic state object on success or an
2727 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01002728 *
2729 * See also:
2730 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02002731 */
2732struct drm_atomic_state *
2733drm_atomic_helper_duplicate_state(struct drm_device *dev,
2734 struct drm_modeset_acquire_ctx *ctx)
2735{
2736 struct drm_atomic_state *state;
2737 struct drm_connector *conn;
2738 struct drm_plane *plane;
2739 struct drm_crtc *crtc;
2740 int err = 0;
2741
2742 state = drm_atomic_state_alloc(dev);
2743 if (!state)
2744 return ERR_PTR(-ENOMEM);
2745
2746 state->acquire_ctx = ctx;
2747
2748 drm_for_each_crtc(crtc, dev) {
2749 struct drm_crtc_state *crtc_state;
2750
2751 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2752 if (IS_ERR(crtc_state)) {
2753 err = PTR_ERR(crtc_state);
2754 goto free;
2755 }
2756 }
2757
2758 drm_for_each_plane(plane, dev) {
2759 struct drm_plane_state *plane_state;
2760
2761 plane_state = drm_atomic_get_plane_state(state, plane);
2762 if (IS_ERR(plane_state)) {
2763 err = PTR_ERR(plane_state);
2764 goto free;
2765 }
2766 }
2767
2768 drm_for_each_connector(conn, dev) {
2769 struct drm_connector_state *conn_state;
2770
2771 conn_state = drm_atomic_get_connector_state(state, conn);
2772 if (IS_ERR(conn_state)) {
2773 err = PTR_ERR(conn_state);
2774 goto free;
2775 }
2776 }
2777
2778 /* clear the acquire context so that it isn't accidentally reused */
2779 state->acquire_ctx = NULL;
2780
2781free:
2782 if (err < 0) {
2783 drm_atomic_state_free(state);
2784 state = ERR_PTR(err);
2785 }
2786
2787 return state;
2788}
2789EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2790
2791/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002792 * __drm_atomic_helper_connector_destroy_state - release connector state
2793 * @connector: connector object
2794 * @state: connector state object to release
2795 *
2796 * Releases all resources stored in the connector state without actually
2797 * freeing the memory of the connector state. This is useful for drivers that
2798 * subclass the connector state.
2799 */
2800void
2801__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2802 struct drm_connector_state *state)
2803{
2804 /*
2805 * This is currently a placeholder so that drivers that subclass the
2806 * state will automatically do the right thing if code is ever added
2807 * to this function.
2808 */
2809}
2810EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2811
2812/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002813 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2814 * @connector: drm connector
2815 * @state: connector state object to release
2816 *
2817 * Default connector state destroy hook for drivers which don't have their own
2818 * subclassed connector state structure.
2819 */
2820void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2821 struct drm_connector_state *state)
2822{
Thierry Redingf5e78402015-01-28 14:54:32 +01002823 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002824 kfree(state);
2825}
2826EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);