blob: bc96d31bd3880635d95464ff17122044e89302b2 [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>
Chris Wilsonf54d1862016-10-25 13:00:45 +010033#include <linux/dma-fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020035#include "drm_crtc_internal.h"
36
Daniel Vetter3150c7d2014-11-06 20:53:29 +010037/**
38 * DOC: overview
39 *
40 * This helper library provides implementations of check and commit functions on
41 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
42 * also provides convenience implementations for the atomic state handling
43 * callbacks for drivers which don't need to subclass the drm core structures to
44 * add their own additional internal state.
45 *
46 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020047 * drm_atomic_helper_check() and for the commit callback with
48 * drm_atomic_helper_commit(). But the individual stages and callbacks are
49 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010050 * together with a driver private modeset implementation.
51 *
52 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020053 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
54 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010055 * various functions to implement set_property callbacks. New drivers must not
56 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010057 *
58 * The atomic helper uses the same function table structures as all other
Daniel Vetterea0dd852016-12-29 21:48:26 +010059 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
60 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
61 * also shares the &struct drm_plane_helper_funcs function table with the plane
Daniel Vetter092d01d2015-12-04 09:45:44 +010062 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010063 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010064static void
65drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010066 struct drm_plane_state *old_plane_state,
Daniel Vetterc2fcd272014-11-05 00:14:14 +010067 struct drm_plane_state *plane_state,
68 struct drm_plane *plane)
69{
70 struct drm_crtc_state *crtc_state;
71
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010072 if (old_plane_state->crtc) {
Andrzej Hajda2943ef32016-03-15 13:46:00 +010073 crtc_state = drm_atomic_get_existing_crtc_state(state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010074 old_plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010075
76 if (WARN_ON(!crtc_state))
77 return;
78
79 crtc_state->planes_changed = true;
80 }
81
82 if (plane_state->crtc) {
Andrzej Hajda2943ef32016-03-15 13:46:00 +010083 crtc_state = drm_atomic_get_existing_crtc_state(state,
84 plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010085
86 if (WARN_ON(!crtc_state))
87 return;
88
89 crtc_state->planes_changed = true;
90 }
91}
92
Maarten Lankhorst8248b652016-03-03 10:17:40 +010093static int handle_conflicting_encoders(struct drm_atomic_state *state,
94 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +010095{
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010096 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +020097 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +010098 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst40616a22016-03-03 10:17:39 +010099 struct drm_encoder *encoder;
100 unsigned encoder_mask = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100101 int i, ret = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200102
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100103 /*
104 * First loop, find all newly assigned encoders from the connectors
105 * part of the state. If the same encoder is assigned to multiple
106 * connectors bail out.
107 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100108 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100109 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
110 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200111
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100112 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200113 continue;
114
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100115 if (funcs->atomic_best_encoder)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100116 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200117 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100118 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200119 else
120 new_encoder = drm_atomic_helper_best_encoder(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100121
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100122 if (new_encoder) {
123 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
124 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
125 new_encoder->base.id, new_encoder->name,
126 connector->base.id, connector->name);
127
128 return -EINVAL;
129 }
130
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100131 encoder_mask |= 1 << drm_encoder_index(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100132 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200133 }
134
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100135 if (!encoder_mask)
136 return 0;
137
138 /*
139 * Second loop, iterate over all connectors not part of the state.
140 *
141 * If a conflicting encoder is found and disable_conflicting_encoders
142 * is not set, an error is returned. Userspace can provide a solution
143 * through the atomic ioctl.
144 *
145 * If the flag is set conflicting connectors are removed from the crtc
146 * and the crtc is disabled if no encoder is left. This preserves
147 * compatibility with the legacy set_config behavior.
148 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100149 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100150 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100151 struct drm_crtc_state *crtc_state;
152
153 if (drm_atomic_get_existing_connector_state(state, connector))
154 continue;
155
156 encoder = connector->state->best_encoder;
157 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
158 continue;
159
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100160 if (!disable_conflicting_encoders) {
161 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
162 encoder->base.id, encoder->name,
163 connector->state->crtc->base.id,
164 connector->state->crtc->name,
165 connector->base.id, connector->name);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100166 ret = -EINVAL;
167 goto out;
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100168 }
169
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100170 new_conn_state = drm_atomic_get_connector_state(state, connector);
171 if (IS_ERR(new_conn_state)) {
172 ret = PTR_ERR(new_conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100173 goto out;
174 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100175
176 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
177 encoder->base.id, encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100178 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100179 connector->base.id, connector->name);
180
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100181 crtc_state = drm_atomic_get_existing_crtc_state(state, new_conn_state->crtc);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100182
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100183 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100184 if (ret)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100185 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100186
187 if (!crtc_state->connector_mask) {
188 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
189 NULL);
190 if (ret < 0)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100191 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100192
193 crtc_state->active = false;
194 }
195 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100196out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100197 drm_connector_list_iter_end(&conn_iter);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100198
Daniel Vetterc36a3252016-12-15 16:58:43 +0100199 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200200}
201
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100202static void
203set_best_encoder(struct drm_atomic_state *state,
204 struct drm_connector_state *conn_state,
205 struct drm_encoder *encoder)
206{
207 struct drm_crtc_state *crtc_state;
208 struct drm_crtc *crtc;
209
210 if (conn_state->best_encoder) {
211 /* Unset the encoder_mask in the old crtc state. */
212 crtc = conn_state->connector->state->crtc;
213
214 /* A NULL crtc is an error here because we should have
215 * duplicated a NULL best_encoder when crtc was NULL.
216 * As an exception restoring duplicated atomic state
217 * during resume is allowed, so don't warn when
218 * best_encoder is equal to encoder we intend to set.
219 */
220 WARN_ON(!crtc && encoder != conn_state->best_encoder);
221 if (crtc) {
222 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
223
224 crtc_state->encoder_mask &=
225 ~(1 << drm_encoder_index(conn_state->best_encoder));
226 }
227 }
228
229 if (encoder) {
230 crtc = conn_state->crtc;
231 WARN_ON(!crtc);
232 if (crtc) {
233 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
234
235 crtc_state->encoder_mask |=
236 1 << drm_encoder_index(encoder);
237 }
238 }
239
240 conn_state->best_encoder = encoder;
241}
242
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100243static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200244steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100245 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200246{
Daniel Vetter623369e2014-09-16 17:50:47 +0200247 struct drm_crtc_state *crtc_state;
248 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100249 struct drm_connector_state *old_connector_state, *new_connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100250 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200251
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100252 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100253 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200254
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100255 if (new_connector_state->best_encoder != encoder)
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100256 continue;
257
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100258 encoder_crtc = old_connector_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200259
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100260 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
261 encoder->base.id, encoder->name,
262 encoder_crtc->base.id, encoder_crtc->name);
263
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100264 set_best_encoder(state, new_connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100265
266 crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
267 crtc_state->connectors_changed = true;
268
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100269 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200270 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200271}
272
273static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100274update_connector_routing(struct drm_atomic_state *state,
275 struct drm_connector *connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100276 struct drm_connector_state *old_connector_state,
277 struct drm_connector_state *new_connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200278{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200279 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200280 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200281 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200282
Daniel Vetter17a38d92015-02-22 12:24:16 +0100283 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
284 connector->base.id,
285 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200286
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100287 if (old_connector_state->crtc != new_connector_state->crtc) {
288 if (old_connector_state->crtc) {
289 crtc_state = drm_atomic_get_existing_crtc_state(state, old_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200290 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200291 }
292
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100293 if (new_connector_state->crtc) {
294 crtc_state = drm_atomic_get_existing_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200295 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200296 }
297 }
298
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100299 if (!new_connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100300 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200301 connector->base.id,
302 connector->name);
303
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100304 set_best_encoder(state, new_connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200305
306 return 0;
307 }
308
309 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200310
311 if (funcs->atomic_best_encoder)
312 new_encoder = funcs->atomic_best_encoder(connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100313 new_connector_state);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200314 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200315 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200316 else
317 new_encoder = drm_atomic_helper_best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200318
319 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100320 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
321 connector->base.id,
322 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200323 return -EINVAL;
324 }
325
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100326 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
Russell King6ac7c542017-02-13 12:27:03 +0000327 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100328 new_encoder->base.id,
329 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100330 new_connector_state->crtc->base.id,
331 new_connector_state->crtc->name);
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100332 return -EINVAL;
333 }
334
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100335 if (new_encoder == new_connector_state->best_encoder) {
336 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100337
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200338 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100339 connector->base.id,
340 connector->name,
341 new_encoder->base.id,
342 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100343 new_connector_state->crtc->base.id,
344 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200345
346 return 0;
347 }
348
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100349 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200350
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100351 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100352
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100353 crtc_state = drm_atomic_get_existing_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200354 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200355
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200356 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100357 connector->base.id,
358 connector->name,
359 new_encoder->base.id,
360 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100361 new_connector_state->crtc->base.id,
362 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200363
364 return 0;
365}
366
367static int
368mode_fixup(struct drm_atomic_state *state)
369{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300370 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100371 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300372 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100373 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200374 int i;
Dan Carpenterf9ad86e2017-02-08 02:46:01 +0300375 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200376
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100377 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
378 if (!new_crtc_state->mode_changed &&
379 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200380 continue;
381
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100382 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200383 }
384
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100385 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200386 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200387 struct drm_encoder *encoder;
388
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100389 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200390
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100391 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200392 continue;
393
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100394 new_crtc_state =
395 drm_atomic_get_existing_crtc_state(state, new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200396
397 /*
398 * Each encoder has at most one connector (since we always steal
399 * it away), so we won't call ->mode_fixup twice.
400 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100401 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200402 funcs = encoder->helper_private;
403
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100404 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
405 &new_crtc_state->adjusted_mode);
Archit Taneja862e6862015-05-21 11:03:16 +0530406 if (!ret) {
407 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
408 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200409 }
410
Noralf Trønnes28276352016-05-11 18:09:20 +0200411 if (funcs && funcs->atomic_check) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100412 ret = funcs->atomic_check(encoder, new_crtc_state,
413 new_conn_state);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100414 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100415 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
416 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100417 return ret;
418 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200419 } else if (funcs && funcs->mode_fixup) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100420 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
421 &new_crtc_state->adjusted_mode);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100422 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100423 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
424 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100425 return -EINVAL;
426 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200427 }
428 }
429
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100430 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200431 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200432
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100433 if (!new_crtc_state->enable)
Liu Yingf55f1702016-05-27 17:35:54 +0800434 continue;
435
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100436 if (!new_crtc_state->mode_changed &&
437 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200438 continue;
439
440 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300441 if (!funcs->mode_fixup)
442 continue;
443
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100444 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
445 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200446 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200447 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
448 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200449 return -EINVAL;
450 }
451 }
452
453 return 0;
454}
455
Daniel Vetterd9b13622014-11-26 16:57:41 +0100456/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800457 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100458 * @dev: DRM device
459 * @state: the driver state object
460 *
461 * Check the state object to see if the requested state is physically possible.
462 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200463 * update and adds any additional connectors needed for full modesets and calls
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100464 * down into &drm_crtc_helper_funcs.mode_fixup and
465 * &drm_encoder_helper_funcs.mode_fixup or
466 * &drm_encoder_helper_funcs.atomic_check functions of the driver backend.
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200467 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100468 * &drm_crtc_state.mode_changed is set when the input mode is changed.
469 * &drm_crtc_state.connectors_changed is set when a connector is added or
470 * removed from the crtc. &drm_crtc_state.active_changed is set when
471 * &drm_crtc_state.active changes, which is used for DPMS.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100472 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100473 *
474 * IMPORTANT:
475 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100476 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
477 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
478 * without a full modeset) _must_ call this function afterwards after that
479 * change. It is permitted to call this function multiple times for the same
480 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
481 * upon the adjusted dotclock for fifo space allocation and watermark
482 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100483 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200484 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100485 * Zero for success or -errno
486 */
487int
Rob Clark934ce1c2014-11-19 16:41:33 -0500488drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200489 struct drm_atomic_state *state)
490{
Daniel Vetter623369e2014-09-16 17:50:47 +0200491 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100492 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300493 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100494 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200495 int i, ret;
496
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100497 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
498 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200499 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
500 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100501 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200502 }
503
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100504 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200505 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
506 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200507
508 /*
509 * For clarity this assignment is done here, but
510 * enable == 0 is only true when there are no
511 * connectors and a NULL mode.
512 *
513 * The other way around is true as well. enable != 0
514 * iff connectors are attached and a mode is set.
515 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100516 new_crtc_state->mode_changed = true;
517 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200518 }
519 }
520
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100521 ret = handle_conflicting_encoders(state, state->legacy_set_config);
522 if (ret)
523 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100524
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100525 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200526 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100527 * This only sets crtc->connectors_changed for routing changes,
528 * drivers must set crtc->connectors_changed themselves when
529 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200530 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100531 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100532 old_connector_state,
533 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200534 if (ret)
535 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100536 if (old_connector_state->crtc) {
537 new_crtc_state = drm_atomic_get_existing_crtc_state(state,
538 old_connector_state->crtc);
539 if (old_connector_state->link_status !=
540 new_connector_state->link_status)
541 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200542 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200543 }
544
545 /*
546 * After all the routing has been prepared we need to add in any
547 * connector which is itself unchanged, but who's crtc changes it's
548 * configuration. This must be done before calling mode_fixup in case a
549 * crtc only changed its mode but has the same set of connectors.
550 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100551 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100552 bool has_connectors =
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100553 !!new_crtc_state->connector_mask;
Daniel Vetter623369e2014-09-16 17:50:47 +0200554
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100555 /*
556 * We must set ->active_changed after walking connectors for
557 * otherwise an update that only changes active would result in
558 * a full modeset because update_connector_routing force that.
559 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100560 if (old_crtc_state->active != new_crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200561 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
562 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100563 new_crtc_state->active_changed = true;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100564 }
565
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100566 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100567 continue;
568
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200569 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
570 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100571 new_crtc_state->enable ? 'y' : 'n',
572 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200573
574 ret = drm_atomic_add_affected_connectors(state, crtc);
575 if (ret != 0)
576 return ret;
577
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200578 ret = drm_atomic_add_affected_planes(state, crtc);
579 if (ret != 0)
580 return ret;
581
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100582 if (new_crtc_state->enable != has_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200583 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
584 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200585
586 return -EINVAL;
587 }
588 }
589
590 return mode_fixup(state);
591}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100592EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200593
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100594/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800595 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100596 * @dev: DRM device
597 * @state: the driver state object
598 *
599 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100600 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100601 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
602 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100603 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100604 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200605 * updated planes.
606 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200607 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100608 * Zero for success or -errno
609 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100610int
611drm_atomic_helper_check_planes(struct drm_device *dev,
612 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100613{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300614 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100615 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300616 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100617 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100618 int i, ret = 0;
619
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100620 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200621 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100622
623 funcs = plane->helper_private;
624
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100625 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100626
627 if (!funcs || !funcs->atomic_check)
628 continue;
629
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100630 ret = funcs->atomic_check(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100631 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200632 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
633 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100634 return ret;
635 }
636 }
637
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100638 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200639 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100640
641 funcs = crtc->helper_private;
642
643 if (!funcs || !funcs->atomic_check)
644 continue;
645
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100646 ret = funcs->atomic_check(crtc, new_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100647 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200648 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
649 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100650 return ret;
651 }
652 }
653
Daniel Vetterd9b13622014-11-26 16:57:41 +0100654 return ret;
655}
656EXPORT_SYMBOL(drm_atomic_helper_check_planes);
657
658/**
659 * drm_atomic_helper_check - validate state object
660 * @dev: DRM device
661 * @state: the driver state object
662 *
663 * Check the state object to see if the requested state is physically possible.
664 * Only crtcs and planes have check callbacks, so for any additional (global)
665 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100666 * Drivers without such needs can directly use this as their
667 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100668 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100669 * This just wraps the two parts of the state checking for planes and modeset
670 * state in the default order: First it calls drm_atomic_helper_check_modeset()
671 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100672 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
673 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
674 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100675 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200676 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100677 * Zero for success or -errno
678 */
679int drm_atomic_helper_check(struct drm_device *dev,
680 struct drm_atomic_state *state)
681{
682 int ret;
683
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100684 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100685 if (ret)
686 return ret;
687
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100688 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500689 if (ret)
690 return ret;
691
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100692 return ret;
693}
694EXPORT_SYMBOL(drm_atomic_helper_check);
695
Daniel Vetter623369e2014-09-16 17:50:47 +0200696static void
697disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
698{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300699 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100700 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300701 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100702 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200703 int i;
704
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100705 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200706 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200707 struct drm_encoder *encoder;
708
Daniel Vetter623369e2014-09-16 17:50:47 +0200709 /* Shut down everything that's in the changeset and currently
710 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300711 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200712 continue;
713
Andrzej Hajda2943ef32016-03-15 13:46:00 +0100714 old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
715 old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100716
Daniel Vetter4218a322015-03-26 22:18:40 +0100717 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200718 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100719 continue;
720
Rob Clark46df9ad2014-11-20 15:40:36 -0500721 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200722
Rob Clark46df9ad2014-11-20 15:40:36 -0500723 /* We shouldn't get this far if we didn't previously have
724 * an encoder.. but WARN_ON() rather than explode.
725 */
726 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200727 continue;
728
729 funcs = encoder->helper_private;
730
Daniel Vetter17a38d92015-02-22 12:24:16 +0100731 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
732 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100733
Daniel Vetter623369e2014-09-16 17:50:47 +0200734 /*
735 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800736 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200737 */
Archit Taneja862e6862015-05-21 11:03:16 +0530738 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200739
740 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200741 if (funcs) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100742 if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +0200743 funcs->prepare(encoder);
744 else if (funcs->disable)
745 funcs->disable(encoder);
746 else if (funcs->dpms)
747 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
748 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200749
Archit Taneja862e6862015-05-21 11:03:16 +0530750 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200751 }
752
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100753 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200754 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200755
756 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100757 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100758 continue;
759
760 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200761 continue;
762
763 funcs = crtc->helper_private;
764
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200765 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
766 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100767
768
Daniel Vetter623369e2014-09-16 17:50:47 +0200769 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100770 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200771 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +0800772 else if (funcs->atomic_disable)
773 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200774 else if (funcs->disable)
775 funcs->disable(crtc);
776 else
777 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
778 }
779}
780
Daniel Vetter4c18d302015-05-12 15:27:37 +0200781/**
782 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
783 * @dev: DRM device
784 * @old_state: atomic state object with old state structures
785 *
786 * This function updates all the various legacy modeset state pointers in
787 * connectors, encoders and crtcs. It also updates the timestamping constants
788 * used for precise vblank timestamps by calling
789 * drm_calc_timestamping_constants().
790 *
791 * Drivers can use this for building their own atomic commit if they don't have
792 * a pure helper-based modeset implementation.
793 */
794void
795drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
796 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200797{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300798 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100799 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300800 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100801 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200802 int i;
803
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200804 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100805 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200806 if (connector->encoder) {
807 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200808
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200809 connector->encoder->crtc = NULL;
810 connector->encoder = NULL;
811 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200812
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100813 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200814 if ((!crtc && old_conn_state->crtc) ||
815 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
816 struct drm_property *dpms_prop =
817 dev->mode_config.dpms_property;
818 int mode = DRM_MODE_DPMS_OFF;
819
820 if (crtc && crtc->state->active)
821 mode = DRM_MODE_DPMS_ON;
822
823 connector->dpms = mode;
824 drm_object_property_set_value(&connector->base,
825 dpms_prop, mode);
826 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200827 }
828
829 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100830 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
831 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200832 continue;
833
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100834 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200835 continue;
836
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100837 connector->encoder = new_conn_state->best_encoder;
838 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200839 }
840
841 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100842 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200843 struct drm_plane *primary = crtc->primary;
844
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100845 crtc->mode = new_crtc_state->mode;
846 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200847
848 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
849 primary->state->crtc == crtc) {
850 crtc->x = primary->state->src_x >> 16;
851 crtc->y = primary->state->src_y >> 16;
852 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200853
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100854 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200855 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100856 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200857 }
858}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200859EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200860
861static void
862crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
863{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300864 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100865 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300866 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100867 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200868 int i;
869
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100870 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200871 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200872
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100873 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200874 continue;
875
876 funcs = crtc->helper_private;
877
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100878 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200879 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
880 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100881
Daniel Vetter623369e2014-09-16 17:50:47 +0200882 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100883 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200884 }
885
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100886 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200887 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200888 struct drm_encoder *encoder;
889 struct drm_display_mode *mode, *adjusted_mode;
890
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100891 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200892 continue;
893
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100894 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200895 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100896 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200897 mode = &new_crtc_state->mode;
898 adjusted_mode = &new_crtc_state->adjusted_mode;
899
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100900 if (!new_crtc_state->mode_changed)
901 continue;
902
Daniel Vetter17a38d92015-02-22 12:24:16 +0100903 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
904 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100905
Daniel Vetter623369e2014-09-16 17:50:47 +0200906 /*
907 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800908 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200909 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200910 if (funcs && funcs->atomic_mode_set) {
911 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100912 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200913 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +0100914 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200915 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200916
Archit Taneja862e6862015-05-21 11:03:16 +0530917 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200918 }
919}
920
921/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100922 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200923 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100924 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200925 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100926 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200927 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100928 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300929 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100930 * drm_atomic_helper_commit_planes(), which is what the default commit function
931 * does. But drivers with different needs can group the modeset commits together
932 * and do the plane commits at the end. This is useful for drivers doing runtime
933 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200934 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100935void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
936 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200937{
Laurent Pincharta072f802015-02-22 12:24:18 +0100938 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200939
940 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
941
Laurent Pincharta072f802015-02-22 12:24:18 +0100942 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200943}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100944EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200945
946/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100947 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200948 * @dev: DRM device
949 * @old_state: atomic state object with old state structures
950 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100951 * This function enables all the outputs with the new configuration which had to
952 * be turned off for the update.
953 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300954 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100955 * drm_atomic_helper_commit_planes(), which is what the default commit function
956 * does. But drivers with different needs can group the modeset commits together
957 * and do the plane commits at the end. This is useful for drivers doing runtime
958 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200959 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100960void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
961 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200962{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300963 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100964 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300965 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100966 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200967 int i;
968
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100969 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200970 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200971
972 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100973 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100974 continue;
975
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100976 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200977 continue;
978
979 funcs = crtc->helper_private;
980
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100981 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200982 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
983 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100984
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100985 if (funcs->enable)
986 funcs->enable(crtc);
987 else
988 funcs->commit(crtc);
989 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200990 }
991
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100992 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200993 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200994 struct drm_encoder *encoder;
995
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100996 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200997 continue;
998
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100999 if (!new_conn_state->crtc->state->active ||
1000 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001001 continue;
1002
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001003 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001004 funcs = encoder->helper_private;
1005
Daniel Vetter17a38d92015-02-22 12:24:16 +01001006 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1007 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001008
Daniel Vetter623369e2014-09-16 17:50:47 +02001009 /*
1010 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001011 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001012 */
Archit Taneja862e6862015-05-21 11:03:16 +05301013 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001014
Noralf Trønnes28276352016-05-11 18:09:20 +02001015 if (funcs) {
1016 if (funcs->enable)
1017 funcs->enable(encoder);
1018 else if (funcs->commit)
1019 funcs->commit(encoder);
1020 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001021
Archit Taneja862e6862015-05-21 11:03:16 +05301022 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001023 }
1024}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001025EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001026
Rob Clark4c5b7f32016-03-18 19:14:55 -04001027/**
1028 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1029 * @dev: DRM device
1030 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001031 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1032 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001033 *
1034 * For implicit sync, driver should fish the exclusive fence out from the
1035 * incoming fb's and stash it in the drm_plane_state. This is called after
1036 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1037 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001038 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001039 * Note that @pre_swap is needed since the point where we block for fences moves
1040 * around depending upon whether an atomic commit is blocking or
1041 * non-blocking. For async commit all waiting needs to happen after
1042 * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1043 * to wait **before** we do anything that can't be easily rolled back. That is
1044 * before we call drm_atomic_helper_swap_state().
1045 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001046 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001047 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001048int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1049 struct drm_atomic_state *state,
1050 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001051{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001052 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001053 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001054 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001055
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001056 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1057 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001058 continue;
1059
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001060 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001061
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001062 /*
1063 * If waiting for fences pre-swap (ie: nonblock), userspace can
1064 * still interrupt the operation. Instead of blocking until the
1065 * timer expires, make the wait interruptible.
1066 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001067 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001068 if (ret)
1069 return ret;
1070
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001071 dma_fence_put(new_plane_state->fence);
1072 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001073 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001074
1075 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001076}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001077EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001078
John Keepingc2409062016-01-19 10:46:58 +00001079/**
Rob Clark5ee32292014-11-11 19:38:59 -05001080 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1081 * @dev: DRM device
1082 * @old_state: atomic state object with old state structures
1083 *
1084 * Helper to, after atomic commit, wait for vblanks on all effected
1085 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001086 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1087 * framebuffers have actually changed to optimize for the legacy cursor and
1088 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001089 */
1090void
1091drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1092 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001093{
1094 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001095 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001096 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001097 unsigned crtc_mask = 0;
1098
1099 /*
1100 * Legacy cursor ioctls are completely unsynced, and userspace
1101 * relies on that (by doing tons of cursor updates).
1102 */
1103 if (old_state->legacy_cursor_update)
1104 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001105
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001106 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta3fbb532016-12-08 14:45:27 +01001107 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
Daniel Vetterab58e332014-11-24 20:42:42 +01001108 continue;
1109
Daniel Vetter623369e2014-09-16 17:50:47 +02001110 ret = drm_crtc_vblank_get(crtc);
1111 if (ret != 0)
1112 continue;
1113
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001114 crtc_mask |= drm_crtc_mask(crtc);
1115 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001116 }
1117
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001118 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001119 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001120 continue;
1121
1122 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001123 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001124 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001125 msecs_to_jiffies(50));
1126
Russell King6ac7c542017-02-13 12:27:03 +00001127 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1128 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001129
Daniel Vetter623369e2014-09-16 17:50:47 +02001130 drm_crtc_vblank_put(crtc);
1131 }
1132}
Rob Clark5ee32292014-11-11 19:38:59 -05001133EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001134
1135/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001136 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001137 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001138 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001139 * This is the default implementation for the
1140 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
Daniel Vetter623369e2014-09-16 17:50:47 +02001141 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001142 * Note that the default ordering of how the various stages are called is to
1143 * match the legacy modeset helper library closest. One peculiarity of that is
1144 * that it doesn't mesh well with runtime PM at all.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001145 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001146 * For drivers supporting runtime PM the recommended sequence is instead ::
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001147 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001148 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001149 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001150 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001151 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001152 * drm_atomic_helper_commit_planes(dev, old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001153 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001154 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001155 * for committing the atomic update to hardware. See the kerneldoc entries for
1156 * these three functions for more details.
1157 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001158void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001159{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001160 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001161
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001162 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001163
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001164 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001165
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001166 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001167
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001168 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001169
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001170 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001171
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001172 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001173}
1174EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1175
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001176static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001177{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001178 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001179 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001180
1181 funcs = dev->mode_config.helper_private;
1182
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001183 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001184
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001185 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001186
1187 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001188 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001189 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001190 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001191
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001192 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001193
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001194 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001195}
1196
1197static void commit_work(struct work_struct *work)
1198{
1199 struct drm_atomic_state *state = container_of(work,
1200 struct drm_atomic_state,
1201 commit_work);
1202 commit_tail(state);
1203}
1204
1205/**
1206 * drm_atomic_helper_commit - commit validated state object
1207 * @dev: DRM device
1208 * @state: the driver state object
1209 * @nonblock: whether nonblocking behavior is requested.
1210 *
1211 * This function commits a with drm_atomic_helper_check() pre-validated state
1212 * object. This can still fail when e.g. the framebuffer reservation fails. This
1213 * function implements nonblocking commits, using
1214 * drm_atomic_helper_setup_commit() and related functions.
1215 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001216 * Committing the actual hardware state is done through the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001217 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1218 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001219 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001220 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001221 * Zero for success or -errno.
1222 */
1223int drm_atomic_helper_commit(struct drm_device *dev,
1224 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001225 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001226{
1227 int ret;
1228
Daniel Vettera095caa2016-06-08 17:15:36 +02001229 ret = drm_atomic_helper_setup_commit(state, nonblock);
1230 if (ret)
1231 return ret;
1232
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001233 INIT_WORK(&state->commit_work, commit_work);
1234
Daniel Vetter623369e2014-09-16 17:50:47 +02001235 ret = drm_atomic_helper_prepare_planes(dev, state);
1236 if (ret)
1237 return ret;
1238
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001239 if (!nonblock) {
1240 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001241 if (ret) {
1242 drm_atomic_helper_cleanup_planes(dev, state);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001243 return ret;
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001244 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001245 }
1246
Daniel Vetter623369e2014-09-16 17:50:47 +02001247 /*
1248 * This is the point of no return - everything below never fails except
1249 * when the hw goes bonghits. Which means we can commit the new state on
1250 * the software side now.
1251 */
1252
Daniel Vetter5e84c262016-06-10 00:06:32 +02001253 drm_atomic_helper_swap_state(state, true);
Daniel Vetter623369e2014-09-16 17:50:47 +02001254
1255 /*
1256 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001257 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001258 * that the asynchronous work has either been cancelled (if the driver
1259 * supports it, which at least requires that the framebuffers get
1260 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1261 * before the new state gets committed on the software side with
1262 * drm_atomic_helper_swap_state().
1263 *
1264 * This scheme allows new atomic state updates to be prepared and
1265 * checked in parallel to the asynchronous completion of the previous
1266 * update. Which is important since compositors need to figure out the
1267 * composition of the next frame right after having submitted the
1268 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001269 *
1270 * NOTE: Commit work has multiple phases, first hardware commit, then
1271 * cleanup. We want them to overlap, hence need system_unbound_wq to
1272 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001273 */
1274
Chris Wilson08536952016-10-14 13:18:18 +01001275 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001276 if (nonblock)
1277 queue_work(system_unbound_wq, &state->commit_work);
1278 else
1279 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001280
1281 return 0;
1282}
1283EXPORT_SYMBOL(drm_atomic_helper_commit);
1284
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001285/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001286 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001287 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001288 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001289 *
1290 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1291 * which commit needs to call which can fail, so we want to run it first and
1292 * synchronously.
1293 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001294 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001295 * might be affected the new state update. This can be done by either cancelling
1296 * or flushing the work items, depending upon whether the driver can deal with
1297 * cancelled updates. Note that it is important to ensure that the framebuffer
1298 * cleanup is still done when cancelling.
1299 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001300 * Asynchronous workers need to have sufficient parallelism to be able to run
1301 * different atomic commits on different CRTCs in parallel. The simplest way to
1302 * achive this is by running them on the &system_unbound_wq work queue. Note
1303 * that drivers are not required to split up atomic commits and run an
1304 * individual commit in parallel - userspace is supposed to do that if it cares.
1305 * But it might be beneficial to do that for modesets, since those necessarily
1306 * must be done as one global operation, and enabling or disabling a CRTC can
1307 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001308 *
1309 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001310 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001311 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001312 * while it's guaranteed that no relevant nonblocking worker runs means that
1313 * nonblocking workers do not need grab any locks. Actually they must not grab
1314 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001315 *
1316 * 4. Schedule a work item to do all subsequent steps, using the split-out
1317 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1318 * then cleaning up the framebuffers after the old framebuffer is no longer
1319 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001320 *
1321 * The above scheme is implemented in the atomic helper libraries in
1322 * drm_atomic_helper_commit() using a bunch of helper functions. See
1323 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001324 */
1325
Daniel Vettera095caa2016-06-08 17:15:36 +02001326static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1327{
1328 struct drm_crtc_commit *commit, *stall_commit = NULL;
1329 bool completed = true;
1330 int i;
1331 long ret = 0;
1332
1333 spin_lock(&crtc->commit_lock);
1334 i = 0;
1335 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1336 if (i == 0) {
1337 completed = try_wait_for_completion(&commit->flip_done);
1338 /* Userspace is not allowed to get ahead of the previous
1339 * commit with nonblocking ones. */
1340 if (!completed && nonblock) {
1341 spin_unlock(&crtc->commit_lock);
1342 return -EBUSY;
1343 }
1344 } else if (i == 1) {
1345 stall_commit = commit;
1346 drm_crtc_commit_get(stall_commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001347 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001348 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001349
1350 i++;
1351 }
1352 spin_unlock(&crtc->commit_lock);
1353
1354 if (!stall_commit)
1355 return 0;
1356
1357 /* We don't want to let commits get ahead of cleanup work too much,
1358 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1359 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001360 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001361 10*HZ);
1362 if (ret == 0)
1363 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1364 crtc->base.id, crtc->name);
1365
1366 drm_crtc_commit_put(stall_commit);
1367
1368 return ret < 0 ? ret : 0;
1369}
1370
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001371static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001372{
1373 struct drm_crtc_commit *commit = container_of(completion,
1374 typeof(*commit),
1375 flip_done);
1376
1377 drm_crtc_commit_put(commit);
1378}
1379
Daniel Vettera095caa2016-06-08 17:15:36 +02001380/**
1381 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1382 * @state: new modeset state to be committed
1383 * @nonblock: whether nonblocking behavior is requested.
1384 *
1385 * This function prepares @state to be used by the atomic helper's support for
1386 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001387 * should always call this function from their
1388 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001389 *
1390 * To be able to use this support drivers need to use a few more helper
1391 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1392 * actually committing the hardware state, and for nonblocking commits this call
1393 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1394 * and it's stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001395 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001396 *
1397 * Completion of the hardware commit step must be signalled using
1398 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1399 * to read or change any permanent software or hardware modeset state. The only
1400 * exception is state protected by other means than &drm_modeset_lock locks.
1401 * Only the free standing @state with pointers to the old state structures can
1402 * be inspected, e.g. to clean up old buffers using
1403 * drm_atomic_helper_cleanup_planes().
1404 *
1405 * At the very end, before cleaning up @state drivers must call
1406 * drm_atomic_helper_commit_cleanup_done().
1407 *
1408 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1409 * complete and esay-to-use default implementation of the atomic_commit() hook.
1410 *
1411 * The tracking of asynchronously executed and still pending commits is done
1412 * using the core structure &drm_crtc_commit.
1413 *
1414 * By default there's no need to clean up resources allocated by this function
1415 * explicitly: drm_atomic_state_default_clear() will take care of that
1416 * automatically.
1417 *
1418 * Returns:
1419 *
1420 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1421 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1422 */
1423int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1424 bool nonblock)
1425{
1426 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001427 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001428 struct drm_crtc_commit *commit;
1429 int i, ret;
1430
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001431 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001432 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1433 if (!commit)
1434 return -ENOMEM;
1435
1436 init_completion(&commit->flip_done);
1437 init_completion(&commit->hw_done);
1438 init_completion(&commit->cleanup_done);
1439 INIT_LIST_HEAD(&commit->commit_entry);
1440 kref_init(&commit->ref);
1441 commit->crtc = crtc;
1442
1443 state->crtcs[i].commit = commit;
1444
1445 ret = stall_checks(crtc, nonblock);
1446 if (ret)
1447 return ret;
1448
1449 /* Drivers only send out events when at least either current or
1450 * new CRTC state is active. Complete right away if everything
1451 * stays off. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001452 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001453 complete_all(&commit->flip_done);
1454 continue;
1455 }
1456
1457 /* Legacy cursor updates are fully unsynced. */
1458 if (state->legacy_cursor_update) {
1459 complete_all(&commit->flip_done);
1460 continue;
1461 }
1462
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001463 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001464 commit->event = kzalloc(sizeof(*commit->event),
1465 GFP_KERNEL);
1466 if (!commit->event)
1467 return -ENOMEM;
1468
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001469 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02001470 }
1471
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001472 new_crtc_state->event->base.completion = &commit->flip_done;
1473 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01001474 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001475 }
1476
1477 return 0;
1478}
1479EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1480
1481
1482static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1483{
1484 struct drm_crtc_commit *commit;
1485 int i = 0;
1486
1487 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1488 /* skip the first entry, that's the current commit */
1489 if (i == 1)
1490 return commit;
1491 i++;
1492 }
1493
1494 return NULL;
1495}
1496
1497/**
1498 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001499 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001500 *
1501 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001502 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02001503 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001504 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02001505 *
1506 * This is part of the atomic helper support for nonblocking commits, see
1507 * drm_atomic_helper_setup_commit() for an overview.
1508 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001509void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001510{
1511 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001512 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001513 struct drm_crtc_commit *commit;
1514 int i;
1515 long ret;
1516
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001517 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001518 spin_lock(&crtc->commit_lock);
1519 commit = preceeding_commit(crtc);
1520 if (commit)
1521 drm_crtc_commit_get(commit);
1522 spin_unlock(&crtc->commit_lock);
1523
1524 if (!commit)
1525 continue;
1526
1527 ret = wait_for_completion_timeout(&commit->hw_done,
1528 10*HZ);
1529 if (ret == 0)
1530 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1531 crtc->base.id, crtc->name);
1532
1533 /* Currently no support for overwriting flips, hence
1534 * stall for previous one to execute completely. */
1535 ret = wait_for_completion_timeout(&commit->flip_done,
1536 10*HZ);
1537 if (ret == 0)
1538 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1539 crtc->base.id, crtc->name);
1540
1541 drm_crtc_commit_put(commit);
1542 }
1543}
1544EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1545
1546/**
1547 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001548 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001549 *
1550 * This function is used to signal completion of the hardware commit step. After
1551 * this step the driver is not allowed to read or change any permanent software
1552 * or hardware modeset state. The only exception is state protected by other
1553 * means than &drm_modeset_lock locks.
1554 *
1555 * Drivers should try to postpone any expensive or delayed cleanup work after
1556 * this function is called.
1557 *
1558 * This is part of the atomic helper support for nonblocking commits, see
1559 * drm_atomic_helper_setup_commit() for an overview.
1560 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001561void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001562{
1563 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001564 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001565 struct drm_crtc_commit *commit;
1566 int i;
1567
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001568 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001569 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001570 if (!commit)
1571 continue;
1572
1573 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001574 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02001575 spin_lock(&crtc->commit_lock);
1576 complete_all(&commit->hw_done);
1577 spin_unlock(&crtc->commit_lock);
1578 }
1579}
1580EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1581
1582/**
1583 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001584 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001585 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001586 * This signals completion of the atomic update @old_state, including any
1587 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01001588 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02001589 *
1590 * This is part of the atomic helper support for nonblocking commits, see
1591 * drm_atomic_helper_setup_commit() for an overview.
1592 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001593void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001594{
1595 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001596 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001597 struct drm_crtc_commit *commit;
1598 int i;
1599 long ret;
1600
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001601 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001602 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001603 if (WARN_ON(!commit))
1604 continue;
1605
1606 spin_lock(&crtc->commit_lock);
1607 complete_all(&commit->cleanup_done);
1608 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1609
1610 /* commit_list borrows our reference, need to remove before we
1611 * clean up our drm_atomic_state. But only after it actually
1612 * completed, otherwise subsequent commits won't stall properly. */
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001613 if (try_wait_for_completion(&commit->flip_done))
1614 goto del_commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001615
1616 spin_unlock(&crtc->commit_lock);
1617
1618 /* We must wait for the vblank event to signal our completion
1619 * before releasing our reference, since the vblank work does
1620 * not hold a reference of its own. */
1621 ret = wait_for_completion_timeout(&commit->flip_done,
1622 10*HZ);
1623 if (ret == 0)
1624 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1625 crtc->base.id, crtc->name);
1626
1627 spin_lock(&crtc->commit_lock);
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001628del_commit:
Daniel Vettera095caa2016-06-08 17:15:36 +02001629 list_del(&commit->commit_entry);
1630 spin_unlock(&crtc->commit_lock);
1631 }
1632}
1633EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1634
Daniel Vettere8c833a2014-07-27 18:30:19 +02001635/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001636 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001637 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001638 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001639 *
1640 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001641 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1642 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1643 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001644 *
1645 * Returns:
1646 * 0 on success, negative error code on failure.
1647 */
1648int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1649 struct drm_atomic_state *state)
1650{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001651 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001652 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001653 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001654
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001655 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001656 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001657
1658 funcs = plane->helper_private;
1659
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001660 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001661 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001662 if (ret)
1663 goto fail;
1664 }
1665 }
1666
1667 return 0;
1668
1669fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001670 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001671 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001672
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001673 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001674 continue;
1675
1676 funcs = plane->helper_private;
1677
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001678 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001679 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001680 }
1681
1682 return ret;
1683}
1684EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1685
Ville Syrjälä7135ac52016-09-19 16:33:42 +03001686static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001687{
1688 return state->crtc && state->crtc->state->active;
1689}
1690
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001691/**
1692 * drm_atomic_helper_commit_planes - commit plane state
1693 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001694 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08001695 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001696 *
1697 * This function commits the new plane state using the plane and atomic helper
1698 * functions for planes and crtcs. It assumes that the atomic state has already
1699 * been pushed into the relevant object state pointers, since this step can no
1700 * longer fail.
1701 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001702 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001703 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001704 *
1705 * Note that this function does all plane updates across all CRTCs in one step.
1706 * If the hardware can't support this approach look at
1707 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001708 *
1709 * Plane parameters can be updated by applications while the associated CRTC is
1710 * disabled. The DRM/KMS core will store the parameters in the plane state,
1711 * which will be available to the driver when the CRTC is turned on. As a result
1712 * most drivers don't need to be immediately notified of plane updates for a
1713 * disabled CRTC.
1714 *
Liu Ying2b58e982016-08-29 17:12:03 +08001715 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1716 * @flags in order not to receive plane update notifications related to a
1717 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001718 * driver code when the driver and/or hardware can't or just don't need to deal
1719 * with updates on disabled CRTCs, for example when supporting runtime PM.
1720 *
Liu Ying2b58e982016-08-29 17:12:03 +08001721 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1722 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001723 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1724 * call for a plane if the CRTC of the old plane state needs a modesetting
1725 * operation. Of course, the drivers need to disable the planes in their CRTC
1726 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08001727 *
1728 * The drm_atomic_helper_commit() default implementation doesn't set the
1729 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1730 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001731 */
1732void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001733 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001734 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001735{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001736 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001737 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001738 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001739 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001740 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08001741 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1742 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001743
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001744 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001745 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001746
1747 funcs = crtc->helper_private;
1748
1749 if (!funcs || !funcs->atomic_begin)
1750 continue;
1751
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001752 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001753 continue;
1754
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001755 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001756 }
1757
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001758 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001759 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001760 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001761
1762 funcs = plane->helper_private;
1763
Thierry Reding3cad4b62014-11-25 13:05:12 +01001764 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001765 continue;
1766
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001767 disabling = drm_atomic_plane_disabling(old_plane_state,
1768 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001769
1770 if (active_only) {
1771 /*
1772 * Skip planes related to inactive CRTCs. If the plane
1773 * is enabled use the state of the current CRTC. If the
1774 * plane is being disabled use the state of the old
1775 * CRTC to avoid skipping planes being disabled on an
1776 * active CRTC.
1777 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001778 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001779 continue;
1780 if (disabling && !plane_crtc_active(old_plane_state))
1781 continue;
1782 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001783
Thierry Reding407b8bd2014-11-20 12:05:50 +01001784 /*
1785 * Special-case disabling the plane if drivers support it.
1786 */
Liu Ying2b58e982016-08-29 17:12:03 +08001787 if (disabling && funcs->atomic_disable) {
1788 struct drm_crtc_state *crtc_state;
1789
1790 crtc_state = old_plane_state->crtc->state;
1791
1792 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1793 no_disable)
1794 continue;
1795
Thierry Reding407b8bd2014-11-20 12:05:50 +01001796 funcs->atomic_disable(plane, old_plane_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001797 } else if (new_plane_state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01001798 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08001799 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001800 }
1801
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001802 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001803 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001804
1805 funcs = crtc->helper_private;
1806
1807 if (!funcs || !funcs->atomic_flush)
1808 continue;
1809
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001810 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001811 continue;
1812
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001813 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001814 }
1815}
1816EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1817
1818/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001819 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1820 * @old_crtc_state: atomic state object with the old crtc state
1821 *
1822 * This function commits the new plane state using the plane and atomic helper
1823 * functions for planes on the specific crtc. It assumes that the atomic state
1824 * has already been pushed into the relevant object state pointers, since this
1825 * step can no longer fail.
1826 *
1827 * This function is useful when plane updates should be done crtc-by-crtc
1828 * instead of one global step like drm_atomic_helper_commit_planes() does.
1829 *
1830 * This function can only be savely used when planes are not allowed to move
1831 * between different CRTCs because this function doesn't handle inter-CRTC
1832 * depencies. Callers need to ensure that either no such depencies exist,
1833 * resolve them through ordering of commit calls or through some other means.
1834 */
1835void
1836drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1837{
1838 const struct drm_crtc_helper_funcs *crtc_funcs;
1839 struct drm_crtc *crtc = old_crtc_state->crtc;
1840 struct drm_atomic_state *old_state = old_crtc_state->state;
1841 struct drm_plane *plane;
1842 unsigned plane_mask;
1843
1844 plane_mask = old_crtc_state->plane_mask;
1845 plane_mask |= crtc->state->plane_mask;
1846
1847 crtc_funcs = crtc->helper_private;
1848 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001849 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001850
1851 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1852 struct drm_plane_state *old_plane_state =
1853 drm_atomic_get_existing_plane_state(old_state, plane);
1854 const struct drm_plane_helper_funcs *plane_funcs;
1855
1856 plane_funcs = plane->helper_private;
1857
1858 if (!old_plane_state || !plane_funcs)
1859 continue;
1860
1861 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1862
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001863 if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001864 plane_funcs->atomic_disable)
1865 plane_funcs->atomic_disable(plane, old_plane_state);
1866 else if (plane->state->crtc ||
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001867 drm_atomic_plane_disabling(old_plane_state, plane->state))
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001868 plane_funcs->atomic_update(plane, old_plane_state);
1869 }
1870
1871 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001872 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001873}
1874EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1875
1876/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001877 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08001878 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02001879 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1880 *
1881 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08001882 * used for instance in the CRTC helper atomic_disable callback to disable
1883 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001884 *
1885 * If the atomic-parameter is set the function calls the CRTC's
1886 * atomic_begin hook before and atomic_flush hook after disabling the
1887 * planes.
1888 *
1889 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001890 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001891 */
Liu Ying28500292016-08-26 15:30:39 +08001892void
1893drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1894 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001895{
Liu Ying28500292016-08-26 15:30:39 +08001896 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02001897 const struct drm_crtc_helper_funcs *crtc_funcs =
1898 crtc->helper_private;
1899 struct drm_plane *plane;
1900
1901 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1902 crtc_funcs->atomic_begin(crtc, NULL);
1903
Liu Ying28500292016-08-26 15:30:39 +08001904 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02001905 const struct drm_plane_helper_funcs *plane_funcs =
1906 plane->helper_private;
1907
Liu Ying28500292016-08-26 15:30:39 +08001908 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001909 continue;
1910
1911 WARN_ON(!plane_funcs->atomic_disable);
1912 if (plane_funcs->atomic_disable)
1913 plane_funcs->atomic_disable(plane, NULL);
1914 }
1915
1916 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1917 crtc_funcs->atomic_flush(crtc, NULL);
1918}
1919EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1920
1921/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001922 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1923 * @dev: DRM device
1924 * @old_state: atomic state object with old state structures
1925 *
1926 * This function cleans up plane state, specifically framebuffers, from the old
1927 * configuration. Hence the old configuration must be perserved in @old_state to
1928 * be able to call this function.
1929 *
1930 * This function must also be called on the new state when the atomic update
1931 * fails at any point after calling drm_atomic_helper_prepare_planes().
1932 */
1933void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1934 struct drm_atomic_state *old_state)
1935{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001936 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001937 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001938 int i;
1939
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001940 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001941 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001942 struct drm_plane_state *plane_state;
1943
1944 /*
1945 * This might be called before swapping when commit is aborted,
1946 * in which case we have to cleanup the new state.
1947 */
1948 if (old_plane_state == plane->state)
1949 plane_state = new_plane_state;
1950 else
1951 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001952
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001953 funcs = plane->helper_private;
1954
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001955 if (funcs->cleanup_fb)
1956 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001957 }
1958}
1959EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1960
1961/**
1962 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001963 * @state: atomic state
Daniel Vetter5e84c262016-06-10 00:06:32 +02001964 * @stall: stall for proceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001965 *
1966 * This function stores the atomic state into the current state pointers in all
1967 * driver objects. It should be called after all failing steps have been done
1968 * and succeeded, but before the actual hardware state is committed.
1969 *
1970 * For cleanup and error recovery the current state for all changed objects will
1971 * be swaped into @state.
1972 *
1973 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1974 *
1975 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1976 *
1977 * 2. Do any other steps that might fail.
1978 *
1979 * 3. Put the staged state into the current state pointers with this function.
1980 *
1981 * 4. Actually commit the hardware state.
1982 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001983 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001984 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02001985 *
1986 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001987 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
1988 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02001989 * don't pass the right state structures to the callbacks.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001990 */
Daniel Vetter5e84c262016-06-10 00:06:32 +02001991void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1992 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001993{
1994 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02001995 long ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001996 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001997 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001998 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001999 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002000 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002001 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002002 struct drm_crtc_commit *commit;
2003
2004 if (stall) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002005 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002006 spin_lock(&crtc->commit_lock);
2007 commit = list_first_entry_or_null(&crtc->commit_list,
2008 struct drm_crtc_commit, commit_entry);
2009 if (commit)
2010 drm_crtc_commit_get(commit);
2011 spin_unlock(&crtc->commit_lock);
2012
2013 if (!commit)
2014 continue;
2015
2016 ret = wait_for_completion_timeout(&commit->hw_done,
2017 10*HZ);
2018 if (ret == 0)
2019 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2020 crtc->base.id, crtc->name);
2021 drm_crtc_commit_put(commit);
2022 }
2023 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002024
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002025 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002026 WARN_ON(connector->state != old_conn_state);
2027
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002028 old_conn_state->state = state;
2029 new_conn_state->state = NULL;
2030
2031 state->connectors[i].state = old_conn_state;
2032 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002033 }
2034
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002035 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002036 WARN_ON(crtc->state != old_crtc_state);
2037
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002038 old_crtc_state->state = state;
2039 new_crtc_state->state = NULL;
2040
2041 state->crtcs[i].state = old_crtc_state;
2042 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002043
2044 if (state->crtcs[i].commit) {
2045 spin_lock(&crtc->commit_lock);
2046 list_add(&state->crtcs[i].commit->commit_entry,
2047 &crtc->commit_list);
2048 spin_unlock(&crtc->commit_lock);
2049
2050 state->crtcs[i].commit->event = NULL;
2051 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002052 }
2053
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002054 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002055 WARN_ON(plane->state != old_plane_state);
2056
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002057 old_plane_state->state = state;
2058 new_plane_state->state = NULL;
2059
2060 state->planes[i].state = old_plane_state;
2061 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002062 }
2063}
2064EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002065
2066/**
2067 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2068 * @plane: plane object to update
2069 * @crtc: owning CRTC of owning plane
2070 * @fb: framebuffer to flip onto plane
2071 * @crtc_x: x offset of primary plane on crtc
2072 * @crtc_y: y offset of primary plane on crtc
2073 * @crtc_w: width of primary plane rectangle on crtc
2074 * @crtc_h: height of primary plane rectangle on crtc
2075 * @src_x: x offset of @fb for panning
2076 * @src_y: y offset of @fb for panning
2077 * @src_w: width of source rectangle in @fb
2078 * @src_h: height of source rectangle in @fb
2079 *
2080 * Provides a default plane update handler using the atomic driver interface.
2081 *
2082 * RETURNS:
2083 * Zero on success, error code on failure
2084 */
2085int drm_atomic_helper_update_plane(struct drm_plane *plane,
2086 struct drm_crtc *crtc,
2087 struct drm_framebuffer *fb,
2088 int crtc_x, int crtc_y,
2089 unsigned int crtc_w, unsigned int crtc_h,
2090 uint32_t src_x, uint32_t src_y,
2091 uint32_t src_w, uint32_t src_h)
2092{
2093 struct drm_atomic_state *state;
2094 struct drm_plane_state *plane_state;
2095 int ret = 0;
2096
2097 state = drm_atomic_state_alloc(plane->dev);
2098 if (!state)
2099 return -ENOMEM;
2100
2101 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2102retry:
2103 plane_state = drm_atomic_get_plane_state(state, plane);
2104 if (IS_ERR(plane_state)) {
2105 ret = PTR_ERR(plane_state);
2106 goto fail;
2107 }
2108
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002109 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002110 if (ret != 0)
2111 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002112 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002113 plane_state->crtc_x = crtc_x;
2114 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002115 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002116 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002117 plane_state->src_x = src_x;
2118 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002119 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002120 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002121
Daniel Vetter3671c582015-05-04 15:40:52 +02002122 if (plane == crtc->cursor)
2123 state->legacy_cursor_update = true;
2124
Daniel Vetter042652e2014-07-27 13:46:52 +02002125 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002126fail:
2127 if (ret == -EDEADLK)
2128 goto backoff;
2129
Chris Wilson08536952016-10-14 13:18:18 +01002130 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002131 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002132
Daniel Vetter042652e2014-07-27 13:46:52 +02002133backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002134 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002135 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002136
2137 /*
2138 * Someone might have exchanged the framebuffer while we dropped locks
2139 * in the backoff code. We need to fix up the fb refcount tracking the
2140 * core does for us.
2141 */
2142 plane->old_fb = plane->fb;
2143
2144 goto retry;
2145}
2146EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2147
2148/**
2149 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2150 * @plane: plane to disable
2151 *
2152 * Provides a default plane disable handler using the atomic driver interface.
2153 *
2154 * RETURNS:
2155 * Zero on success, error code on failure
2156 */
2157int drm_atomic_helper_disable_plane(struct drm_plane *plane)
2158{
2159 struct drm_atomic_state *state;
2160 struct drm_plane_state *plane_state;
2161 int ret = 0;
2162
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08002163 /*
2164 * FIXME: Without plane->crtc set we can't get at the implicit legacy
2165 * acquire context. The real fix will be to wire the acquire ctx through
2166 * everywhere we need it, but meanwhile prevent chaos by just skipping
2167 * this noop. The critical case is the cursor ioctls which a) only grab
2168 * crtc/cursor-plane locks (so we need the crtc to get at the right
2169 * acquire context) and b) can try to disable the plane multiple times.
2170 */
2171 if (!plane->crtc)
2172 return 0;
2173
Daniel Vetter042652e2014-07-27 13:46:52 +02002174 state = drm_atomic_state_alloc(plane->dev);
2175 if (!state)
2176 return -ENOMEM;
2177
2178 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
2179retry:
2180 plane_state = drm_atomic_get_plane_state(state, plane);
2181 if (IS_ERR(plane_state)) {
2182 ret = PTR_ERR(plane_state);
2183 goto fail;
2184 }
2185
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002186 if (plane_state->crtc && (plane == plane->crtc->cursor))
2187 plane_state->state->legacy_cursor_update = true;
2188
Rob Clarkbbb1e522015-08-25 15:35:58 -04002189 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002190 if (ret != 0)
2191 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002192
Daniel Vetter042652e2014-07-27 13:46:52 +02002193 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002194fail:
2195 if (ret == -EDEADLK)
2196 goto backoff;
2197
Chris Wilson08536952016-10-14 13:18:18 +01002198 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002199 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002200
Daniel Vetter042652e2014-07-27 13:46:52 +02002201backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002202 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002203 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002204
2205 /*
2206 * Someone might have exchanged the framebuffer while we dropped locks
2207 * in the backoff code. We need to fix up the fb refcount tracking the
2208 * core does for us.
2209 */
2210 plane->old_fb = plane->fb;
2211
2212 goto retry;
2213}
2214EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2215
Rob Clarkbbb1e522015-08-25 15:35:58 -04002216/* just used from fb-helper and atomic-helper: */
2217int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2218 struct drm_plane_state *plane_state)
2219{
2220 int ret;
2221
2222 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2223 if (ret != 0)
2224 return ret;
2225
2226 drm_atomic_set_fb_for_plane(plane_state, NULL);
2227 plane_state->crtc_x = 0;
2228 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002229 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002230 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002231 plane_state->src_x = 0;
2232 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002233 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002234 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002235
Rob Clarkbbb1e522015-08-25 15:35:58 -04002236 return 0;
2237}
2238
Daniel Vetter042652e2014-07-27 13:46:52 +02002239static int update_output_state(struct drm_atomic_state *state,
2240 struct drm_mode_set *set)
2241{
2242 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002243 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002244 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002245 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002246 struct drm_connector_state *new_conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002247 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002248
2249 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2250 state->acquire_ctx);
2251 if (ret)
2252 return ret;
2253
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002254 /* First disable all connectors on the target crtc. */
2255 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2256 if (ret)
2257 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002258
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002259 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2260 if (new_conn_state->crtc == set->crtc) {
2261 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Daniel Vetter042652e2014-07-27 13:46:52 +02002262 NULL);
2263 if (ret)
2264 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002265
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002266 /* Make sure legacy setCrtc always re-trains */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002267 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
Daniel Vetter042652e2014-07-27 13:46:52 +02002268 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002269 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002270
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002271 /* Then set all connectors from set->connectors on the target crtc */
2272 for (i = 0; i < set->num_connectors; i++) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002273 new_conn_state = drm_atomic_get_connector_state(state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002274 set->connectors[i]);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002275 if (IS_ERR(new_conn_state))
2276 return PTR_ERR(new_conn_state);
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002277
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002278 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002279 set->crtc);
2280 if (ret)
2281 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002282 }
2283
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002284 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002285 /* Don't update ->enable for the CRTC in the set_config request,
2286 * since a mismatch would indicate a bug in the upper layers.
2287 * The actual modeset code later on will catch any
2288 * inconsistencies here. */
2289 if (crtc == set->crtc)
2290 continue;
2291
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002292 if (!new_crtc_state->connector_mask) {
2293 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002294 NULL);
2295 if (ret < 0)
2296 return ret;
2297
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002298 new_crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002299 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002300 }
2301
2302 return 0;
2303}
2304
2305/**
2306 * drm_atomic_helper_set_config - set a new config from userspace
2307 * @set: mode set configuration
2308 *
2309 * Provides a default crtc set_config handler using the atomic driver interface.
2310 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002311 * NOTE: For backwards compatibility with old userspace this automatically
2312 * resets the "link-status" property to GOOD, to force any link
2313 * re-training. The SETCRTC ioctl does not define whether an update does
2314 * need a full modeset or just a plane update, hence we're allowed to do
2315 * that. See also drm_mode_connector_set_link_status_property().
2316 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002317 * Returns:
2318 * Returns 0 on success, negative errno numbers on failure.
2319 */
2320int drm_atomic_helper_set_config(struct drm_mode_set *set)
2321{
2322 struct drm_atomic_state *state;
2323 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002324 int ret = 0;
2325
2326 state = drm_atomic_state_alloc(crtc->dev);
2327 if (!state)
2328 return -ENOMEM;
2329
Maarten Lankhorst40616a22016-03-03 10:17:39 +01002330 state->legacy_set_config = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02002331 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2332retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04002333 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002334 if (ret != 0)
2335 goto fail;
2336
Daniel Vetter042652e2014-07-27 13:46:52 +02002337 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002338fail:
2339 if (ret == -EDEADLK)
2340 goto backoff;
2341
Chris Wilson08536952016-10-14 13:18:18 +01002342 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002343 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002344
Daniel Vetter042652e2014-07-27 13:46:52 +02002345backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002346 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002347 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002348
2349 /*
2350 * Someone might have exchanged the framebuffer while we dropped locks
2351 * in the backoff code. We need to fix up the fb refcount tracking the
2352 * core does for us.
2353 */
2354 crtc->primary->old_fb = crtc->primary->fb;
2355
2356 goto retry;
2357}
2358EXPORT_SYMBOL(drm_atomic_helper_set_config);
2359
Rob Clarkbbb1e522015-08-25 15:35:58 -04002360/* just used from fb-helper and atomic-helper: */
2361int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2362 struct drm_atomic_state *state)
2363{
2364 struct drm_crtc_state *crtc_state;
2365 struct drm_plane_state *primary_state;
2366 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002367 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002368 int ret;
2369
2370 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2371 if (IS_ERR(crtc_state))
2372 return PTR_ERR(crtc_state);
2373
2374 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2375 if (IS_ERR(primary_state))
2376 return PTR_ERR(primary_state);
2377
2378 if (!set->mode) {
2379 WARN_ON(set->fb);
2380 WARN_ON(set->num_connectors);
2381
2382 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2383 if (ret != 0)
2384 return ret;
2385
2386 crtc_state->active = false;
2387
2388 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2389 if (ret != 0)
2390 return ret;
2391
2392 drm_atomic_set_fb_for_plane(primary_state, NULL);
2393
2394 goto commit;
2395 }
2396
2397 WARN_ON(!set->fb);
2398 WARN_ON(!set->num_connectors);
2399
2400 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2401 if (ret != 0)
2402 return ret;
2403
2404 crtc_state->active = true;
2405
2406 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2407 if (ret != 0)
2408 return ret;
2409
Daniel Vetter196cd5d2017-01-25 07:26:56 +01002410 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Ville Syrjälä83926112015-11-16 17:02:34 +02002411
Rob Clarkbbb1e522015-08-25 15:35:58 -04002412 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2413 primary_state->crtc_x = 0;
2414 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02002415 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002416 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002417 primary_state->src_x = set->x << 16;
2418 primary_state->src_y = set->y << 16;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002419 if (drm_rotation_90_or_270(primary_state->rotation)) {
Ville Syrjälä83926112015-11-16 17:02:34 +02002420 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002421 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002422 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02002423 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002424 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002425 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04002426
2427commit:
2428 ret = update_output_state(state, set);
2429 if (ret)
2430 return ret;
2431
2432 return 0;
2433}
2434
Daniel Vetter042652e2014-07-27 13:46:52 +02002435/**
Thierry Reding14942762015-12-02 17:50:04 +01002436 * drm_atomic_helper_disable_all - disable all currently active outputs
2437 * @dev: DRM device
2438 * @ctx: lock acquisition context
2439 *
2440 * Loops through all connectors, finding those that aren't turned off and then
2441 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2442 * that they are connected to.
2443 *
2444 * This is used for example in suspend/resume to disable all currently active
2445 * functions when suspending.
2446 *
2447 * Note that if callers haven't already acquired all modeset locks this might
2448 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2449 *
2450 * Returns:
2451 * 0 on success or a negative error code on failure.
2452 *
2453 * See also:
2454 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2455 */
2456int drm_atomic_helper_disable_all(struct drm_device *dev,
2457 struct drm_modeset_acquire_ctx *ctx)
2458{
2459 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002460 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01002461 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002462 struct drm_plane_state *plane_state;
2463 struct drm_plane *plane;
2464 struct drm_crtc_state *crtc_state;
2465 struct drm_crtc *crtc;
2466 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01002467
2468 state = drm_atomic_state_alloc(dev);
2469 if (!state)
2470 return -ENOMEM;
2471
2472 state->acquire_ctx = ctx;
2473
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002474 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01002475 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2476 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002477 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01002478 goto free;
2479 }
2480
2481 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002482
2483 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2484 if (ret < 0)
2485 goto free;
2486
2487 ret = drm_atomic_add_affected_planes(state, crtc);
2488 if (ret < 0)
2489 goto free;
2490
2491 ret = drm_atomic_add_affected_connectors(state, crtc);
2492 if (ret < 0)
2493 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01002494 }
2495
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002496 for_each_connector_in_state(state, conn, conn_state, i) {
2497 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2498 if (ret < 0)
2499 goto free;
2500 }
2501
2502 for_each_plane_in_state(state, plane, plane_state, i) {
2503 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2504 if (ret < 0)
2505 goto free;
2506
2507 drm_atomic_set_fb_for_plane(plane_state, NULL);
2508 }
2509
2510 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01002511free:
Chris Wilson08536952016-10-14 13:18:18 +01002512 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002513 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01002514}
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002515
Thierry Reding14942762015-12-02 17:50:04 +01002516EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2517
2518/**
2519 * drm_atomic_helper_suspend - subsystem-level suspend helper
2520 * @dev: DRM device
2521 *
2522 * Duplicates the current atomic state, disables all active outputs and then
2523 * returns a pointer to the original atomic state to the caller. Drivers can
2524 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2525 * restore the output configuration that was active at the time the system
2526 * entered suspend.
2527 *
2528 * Note that it is potentially unsafe to use this. The atomic state object
2529 * returned by this function is assumed to be persistent. Drivers must ensure
2530 * that this holds true. Before calling this function, drivers must make sure
2531 * to suspend fbdev emulation so that nothing can be using the device.
2532 *
2533 * Returns:
2534 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2535 * encoded error code on failure. Drivers should store the returned atomic
2536 * state object and pass it to the drm_atomic_helper_resume() helper upon
2537 * resume.
2538 *
2539 * See also:
2540 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002541 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01002542 */
2543struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2544{
2545 struct drm_modeset_acquire_ctx ctx;
2546 struct drm_atomic_state *state;
2547 int err;
2548
2549 drm_modeset_acquire_init(&ctx, 0);
2550
2551retry:
2552 err = drm_modeset_lock_all_ctx(dev, &ctx);
2553 if (err < 0) {
2554 state = ERR_PTR(err);
2555 goto unlock;
2556 }
2557
2558 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2559 if (IS_ERR(state))
2560 goto unlock;
2561
2562 err = drm_atomic_helper_disable_all(dev, &ctx);
2563 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01002564 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01002565 state = ERR_PTR(err);
2566 goto unlock;
2567 }
2568
2569unlock:
2570 if (PTR_ERR(state) == -EDEADLK) {
2571 drm_modeset_backoff(&ctx);
2572 goto retry;
2573 }
2574
2575 drm_modeset_drop_locks(&ctx);
2576 drm_modeset_acquire_fini(&ctx);
2577 return state;
2578}
2579EXPORT_SYMBOL(drm_atomic_helper_suspend);
2580
2581/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002582 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2583 * @state: duplicated atomic state to commit
2584 * @ctx: pointer to acquire_ctx to use for commit.
2585 *
2586 * The state returned by drm_atomic_helper_duplicate_state() and
2587 * drm_atomic_helper_suspend() is partially invalid, and needs to
2588 * be fixed up before commit.
2589 *
2590 * Returns:
2591 * 0 on success or a negative error code on failure.
2592 *
2593 * See also:
2594 * drm_atomic_helper_suspend()
2595 */
2596int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2597 struct drm_modeset_acquire_ctx *ctx)
2598{
2599 int i;
2600 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002601 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002602 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002603 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002604 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002605 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002606
2607 state->acquire_ctx = ctx;
2608
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002609 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002610 state->planes[i].old_state = plane->state;
2611
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002612 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002613 state->crtcs[i].old_state = crtc->state;
2614
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002615 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002616 state->connectors[i].old_state = connector->state;
2617
2618 return drm_atomic_commit(state);
2619}
2620EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2621
2622/**
Thierry Reding14942762015-12-02 17:50:04 +01002623 * drm_atomic_helper_resume - subsystem-level resume helper
2624 * @dev: DRM device
2625 * @state: atomic state to resume to
2626 *
2627 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2628 * grabs all modeset locks and commits the atomic state object. This can be
2629 * used in conjunction with the drm_atomic_helper_suspend() helper to
2630 * implement suspend/resume for drivers that support atomic mode-setting.
2631 *
2632 * Returns:
2633 * 0 on success or a negative error code on failure.
2634 *
2635 * See also:
2636 * drm_atomic_helper_suspend()
2637 */
2638int drm_atomic_helper_resume(struct drm_device *dev,
2639 struct drm_atomic_state *state)
2640{
2641 struct drm_mode_config *config = &dev->mode_config;
2642 int err;
2643
2644 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002645
Thierry Reding14942762015-12-02 17:50:04 +01002646 drm_modeset_lock_all(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002647 err = drm_atomic_helper_commit_duplicated_state(state, config->acquire_ctx);
Thierry Reding14942762015-12-02 17:50:04 +01002648 drm_modeset_unlock_all(dev);
2649
2650 return err;
2651}
2652EXPORT_SYMBOL(drm_atomic_helper_resume);
2653
2654/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002655 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002656 * @crtc: DRM crtc
2657 * @property: DRM property
2658 * @val: value of property
2659 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002660 * Provides a default crtc set_property handler using the atomic driver
2661 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002662 *
2663 * RETURNS:
2664 * Zero on success, error code on failure
2665 */
2666int
2667drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2668 struct drm_property *property,
2669 uint64_t val)
2670{
2671 struct drm_atomic_state *state;
2672 struct drm_crtc_state *crtc_state;
2673 int ret = 0;
2674
2675 state = drm_atomic_state_alloc(crtc->dev);
2676 if (!state)
2677 return -ENOMEM;
2678
2679 /* ->set_property is always called with all locks held. */
2680 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2681retry:
2682 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2683 if (IS_ERR(crtc_state)) {
2684 ret = PTR_ERR(crtc_state);
2685 goto fail;
2686 }
2687
Rob Clark40ecc692014-12-18 16:01:46 -05002688 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2689 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002690 if (ret)
2691 goto fail;
2692
2693 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002694fail:
2695 if (ret == -EDEADLK)
2696 goto backoff;
2697
Chris Wilson08536952016-10-14 13:18:18 +01002698 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002699 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002700
Daniel Vetter042652e2014-07-27 13:46:52 +02002701backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002702 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002703 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002704
2705 goto retry;
2706}
2707EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2708
2709/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002710 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002711 * @plane: DRM plane
2712 * @property: DRM property
2713 * @val: value of property
2714 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002715 * Provides a default plane set_property handler using the atomic driver
2716 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002717 *
2718 * RETURNS:
2719 * Zero on success, error code on failure
2720 */
2721int
2722drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2723 struct drm_property *property,
2724 uint64_t val)
2725{
2726 struct drm_atomic_state *state;
2727 struct drm_plane_state *plane_state;
2728 int ret = 0;
2729
2730 state = drm_atomic_state_alloc(plane->dev);
2731 if (!state)
2732 return -ENOMEM;
2733
2734 /* ->set_property is always called with all locks held. */
2735 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2736retry:
2737 plane_state = drm_atomic_get_plane_state(state, plane);
2738 if (IS_ERR(plane_state)) {
2739 ret = PTR_ERR(plane_state);
2740 goto fail;
2741 }
2742
Rob Clark40ecc692014-12-18 16:01:46 -05002743 ret = drm_atomic_plane_set_property(plane, plane_state,
2744 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002745 if (ret)
2746 goto fail;
2747
2748 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002749fail:
2750 if (ret == -EDEADLK)
2751 goto backoff;
2752
Chris Wilson08536952016-10-14 13:18:18 +01002753 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002754 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002755
Daniel Vetter042652e2014-07-27 13:46:52 +02002756backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002757 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002758 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002759
2760 goto retry;
2761}
2762EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2763
2764/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002765 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002766 * @connector: DRM connector
2767 * @property: DRM property
2768 * @val: value of property
2769 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002770 * Provides a default connector set_property handler using the atomic driver
2771 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002772 *
2773 * RETURNS:
2774 * Zero on success, error code on failure
2775 */
2776int
2777drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2778 struct drm_property *property,
2779 uint64_t val)
2780{
2781 struct drm_atomic_state *state;
2782 struct drm_connector_state *connector_state;
2783 int ret = 0;
2784
2785 state = drm_atomic_state_alloc(connector->dev);
2786 if (!state)
2787 return -ENOMEM;
2788
2789 /* ->set_property is always called with all locks held. */
2790 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2791retry:
2792 connector_state = drm_atomic_get_connector_state(state, connector);
2793 if (IS_ERR(connector_state)) {
2794 ret = PTR_ERR(connector_state);
2795 goto fail;
2796 }
2797
Rob Clark40ecc692014-12-18 16:01:46 -05002798 ret = drm_atomic_connector_set_property(connector, connector_state,
2799 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002800 if (ret)
2801 goto fail;
2802
2803 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002804fail:
2805 if (ret == -EDEADLK)
2806 goto backoff;
2807
Chris Wilson08536952016-10-14 13:18:18 +01002808 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002809 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002810
Daniel Vetter042652e2014-07-27 13:46:52 +02002811backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002812 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002813 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002814
2815 goto retry;
2816}
2817EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002818
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002819static int page_flip_common(
2820 struct drm_atomic_state *state,
2821 struct drm_crtc *crtc,
2822 struct drm_framebuffer *fb,
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002823 struct drm_pending_vblank_event *event,
2824 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002825{
2826 struct drm_plane *plane = crtc->primary;
2827 struct drm_plane_state *plane_state;
2828 struct drm_crtc_state *crtc_state;
2829 int ret = 0;
2830
2831 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2832 if (IS_ERR(crtc_state))
2833 return PTR_ERR(crtc_state);
2834
2835 crtc_state->event = event;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002836 crtc_state->pageflip_flags = flags;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002837
2838 plane_state = drm_atomic_get_plane_state(state, plane);
2839 if (IS_ERR(plane_state))
2840 return PTR_ERR(plane_state);
2841
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002842 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2843 if (ret != 0)
2844 return ret;
2845 drm_atomic_set_fb_for_plane(plane_state, fb);
2846
2847 /* Make sure we don't accidentally do a full modeset. */
2848 state->allow_modeset = false;
2849 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00002850 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
2851 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002852 return -EINVAL;
2853 }
2854
2855 return ret;
2856}
2857
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002858/**
2859 * drm_atomic_helper_page_flip - execute a legacy page flip
2860 * @crtc: DRM crtc
2861 * @fb: DRM framebuffer
2862 * @event: optional DRM event to signal upon completion
2863 * @flags: flip flags for non-vblank sync'ed updates
2864 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002865 * Provides a default &drm_crtc_funcs.page_flip implementation
2866 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002867 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002868 * Returns:
2869 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002870 *
2871 * See also:
2872 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002873 */
2874int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2875 struct drm_framebuffer *fb,
2876 struct drm_pending_vblank_event *event,
2877 uint32_t flags)
2878{
2879 struct drm_plane *plane = crtc->primary;
2880 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002881 int ret = 0;
2882
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002883 state = drm_atomic_state_alloc(plane->dev);
2884 if (!state)
2885 return -ENOMEM;
2886
2887 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002888
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002889retry:
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002890 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002891 if (ret != 0)
2892 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01002893
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002894 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002895
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002896fail:
2897 if (ret == -EDEADLK)
2898 goto backoff;
2899
Chris Wilson08536952016-10-14 13:18:18 +01002900 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002901 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002902
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002903backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002904 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002905 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002906
2907 /*
2908 * Someone might have exchanged the framebuffer while we dropped locks
2909 * in the backoff code. We need to fix up the fb refcount tracking the
2910 * core does for us.
2911 */
2912 plane->old_fb = plane->fb;
2913
2914 goto retry;
2915}
2916EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002917
2918/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002919 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2920 * @crtc: DRM crtc
2921 * @fb: DRM framebuffer
2922 * @event: optional DRM event to signal upon completion
2923 * @flags: flip flags for non-vblank sync'ed updates
2924 * @target: specifying the target vblank period when the flip to take effect
2925 *
2926 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2927 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2928 * target vblank period to flip.
2929 *
2930 * Returns:
2931 * Returns 0 on success, negative errno numbers on failure.
2932 */
2933int drm_atomic_helper_page_flip_target(
2934 struct drm_crtc *crtc,
2935 struct drm_framebuffer *fb,
2936 struct drm_pending_vblank_event *event,
2937 uint32_t flags,
2938 uint32_t target)
2939{
2940 struct drm_plane *plane = crtc->primary;
2941 struct drm_atomic_state *state;
2942 struct drm_crtc_state *crtc_state;
2943 int ret = 0;
2944
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002945 state = drm_atomic_state_alloc(plane->dev);
2946 if (!state)
2947 return -ENOMEM;
2948
2949 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2950
2951retry:
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002952 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002953 if (ret != 0)
2954 goto fail;
2955
2956 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
2957 if (WARN_ON(!crtc_state)) {
2958 ret = -EINVAL;
2959 goto fail;
2960 }
2961 crtc_state->target_vblank = target;
2962
2963 ret = drm_atomic_nonblocking_commit(state);
2964
2965fail:
2966 if (ret == -EDEADLK)
2967 goto backoff;
2968
2969 drm_atomic_state_put(state);
2970 return ret;
2971
2972backoff:
2973 drm_atomic_state_clear(state);
2974 drm_atomic_legacy_backoff(state);
2975
2976 /*
2977 * Someone might have exchanged the framebuffer while we dropped locks
2978 * in the backoff code. We need to fix up the fb refcount tracking the
2979 * core does for us.
2980 */
2981 plane->old_fb = plane->fb;
2982
2983 goto retry;
2984}
2985EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2986
2987/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002988 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2989 * @connector: affected connector
2990 * @mode: DPMS mode
2991 *
2992 * This is the main helper function provided by the atomic helper framework for
2993 * implementing the legacy DPMS connector interface. It computes the new desired
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002994 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
2995 * enabled) and updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002996 *
2997 * Returns:
2998 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002999 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003000int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
3001 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003002{
3003 struct drm_mode_config *config = &connector->dev->mode_config;
3004 struct drm_atomic_state *state;
3005 struct drm_crtc_state *crtc_state;
3006 struct drm_crtc *crtc;
3007 struct drm_connector *tmp_connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003008 struct drm_connector_list_iter conn_iter;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003009 int ret;
3010 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003011 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003012
3013 if (mode != DRM_MODE_DPMS_ON)
3014 mode = DRM_MODE_DPMS_OFF;
3015
3016 connector->dpms = mode;
3017 crtc = connector->state->crtc;
3018
3019 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003020 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003021
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003022 state = drm_atomic_state_alloc(connector->dev);
3023 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003024 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003025
3026 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
3027retry:
3028 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003029 if (IS_ERR(crtc_state)) {
3030 ret = PTR_ERR(crtc_state);
3031 goto fail;
3032 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003033
3034 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
3035
Thierry Redingb982dab2017-02-28 15:46:43 +01003036 drm_connector_list_iter_begin(connector->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003037 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
John Hunter0388df02015-03-17 15:30:28 +08003038 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003039 continue;
3040
John Hunter0388df02015-03-17 15:30:28 +08003041 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003042 active = true;
3043 break;
3044 }
3045 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003046 drm_connector_list_iter_end(&conn_iter);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003047 crtc_state->active = active;
3048
3049 ret = drm_atomic_commit(state);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003050fail:
3051 if (ret == -EDEADLK)
3052 goto backoff;
Marta Lofstedt8f5040e2016-12-05 14:04:08 +02003053 if (ret != 0)
3054 connector->dpms = old_mode;
Chris Wilson08536952016-10-14 13:18:18 +01003055 drm_atomic_state_put(state);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003056 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003057
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003058backoff:
3059 drm_atomic_state_clear(state);
3060 drm_atomic_legacy_backoff(state);
3061
3062 goto retry;
3063}
3064EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3065
3066/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003067 * drm_atomic_helper_best_encoder - Helper for
3068 * &drm_connector_helper_funcs.best_encoder callback
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003069 * @connector: Connector control structure
3070 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003071 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003072 * connectors that support exactly 1 encoder, statically determined at driver
3073 * init time.
3074 */
3075struct drm_encoder *
3076drm_atomic_helper_best_encoder(struct drm_connector *connector)
3077{
3078 WARN_ON(connector->encoder_ids[1]);
3079 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3080}
3081EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3082
3083/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003084 * DOC: atomic state reset and initialization
3085 *
3086 * Both the drm core and the atomic helpers assume that there is always the full
3087 * and correct atomic software state for all connectors, CRTCs and planes
3088 * available. Which is a bit a problem on driver load and also after system
3089 * suspend. One way to solve this is to have a hardware state read-out
3090 * infrastructure which reconstructs the full software state (e.g. the i915
3091 * driver).
3092 *
3093 * The simpler solution is to just reset the software state to everything off,
3094 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3095 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01003096 *
3097 * On the upside the precise state tracking of atomic simplifies system suspend
3098 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3099 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3100 * For other drivers the building blocks are split out, see the documentation
3101 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003102 */
3103
3104/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003105 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
Daniel Vetterd4617012014-11-03 15:56:43 +01003106 * @crtc: drm CRTC
3107 *
3108 * Resets the atomic state for @crtc by freeing the state pointer (which might
3109 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3110 */
3111void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3112{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003113 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003114 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003115
Daniel Vetterd4617012014-11-03 15:56:43 +01003116 kfree(crtc->state);
3117 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003118
3119 if (crtc->state)
3120 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01003121}
3122EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3123
3124/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003125 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3126 * @crtc: CRTC object
3127 * @state: atomic CRTC state
3128 *
3129 * Copies atomic state from a CRTC's current state and resets inferred values.
3130 * This is useful for drivers that subclass the CRTC state.
3131 */
3132void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3133 struct drm_crtc_state *state)
3134{
3135 memcpy(state, crtc->state, sizeof(*state));
3136
Daniel Stone99cf4a22015-05-25 19:11:51 +01003137 if (state->mode_blob)
Thierry Reding6472e502017-02-28 15:46:42 +01003138 drm_property_blob_get(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003139 if (state->degamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003140 drm_property_blob_get(state->degamma_lut);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003141 if (state->ctm)
Thierry Reding6472e502017-02-28 15:46:42 +01003142 drm_property_blob_get(state->ctm);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003143 if (state->gamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003144 drm_property_blob_get(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003145 state->mode_changed = false;
3146 state->active_changed = false;
3147 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02003148 state->connectors_changed = false;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003149 state->color_mgmt_changed = false;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02003150 state->zpos_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01003151 state->event = NULL;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003152 state->pageflip_flags = 0;
Thierry Redingf5e78402015-01-28 14:54:32 +01003153}
3154EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3155
3156/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003157 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3158 * @crtc: drm CRTC
3159 *
3160 * Default CRTC state duplicate hook for drivers which don't have their own
3161 * subclassed CRTC state structure.
3162 */
3163struct drm_crtc_state *
3164drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3165{
3166 struct drm_crtc_state *state;
3167
3168 if (WARN_ON(!crtc->state))
3169 return NULL;
3170
Thierry Redingf5e78402015-01-28 14:54:32 +01003171 state = kmalloc(sizeof(*state), GFP_KERNEL);
3172 if (state)
3173 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003174
3175 return state;
3176}
3177EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3178
3179/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003180 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
Thierry Redingf5e78402015-01-28 14:54:32 +01003181 * @state: CRTC state object to release
3182 *
3183 * Releases all resources stored in the CRTC state without actually freeing
3184 * the memory of the CRTC state. This is useful for drivers that subclass the
3185 * CRTC state.
3186 */
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003187void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003188{
Thierry Reding6472e502017-02-28 15:46:42 +01003189 drm_property_blob_put(state->mode_blob);
3190 drm_property_blob_put(state->degamma_lut);
3191 drm_property_blob_put(state->ctm);
3192 drm_property_blob_put(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003193}
3194EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3195
3196/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003197 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3198 * @crtc: drm CRTC
3199 * @state: CRTC state object to release
3200 *
3201 * Default CRTC state destroy hook for drivers which don't have their own
3202 * subclassed CRTC state structure.
3203 */
3204void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3205 struct drm_crtc_state *state)
3206{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003207 __drm_atomic_helper_crtc_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003208 kfree(state);
3209}
3210EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3211
3212/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003213 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
Daniel Vetterd4617012014-11-03 15:56:43 +01003214 * @plane: drm plane
3215 *
3216 * Resets the atomic state for @plane by freeing the state pointer (which might
3217 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3218 */
3219void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3220{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003221 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +02003222 __drm_atomic_helper_plane_destroy_state(plane->state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003223
Daniel Vetterd4617012014-11-03 15:56:43 +01003224 kfree(plane->state);
3225 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003226
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003227 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003228 plane->state->plane = plane;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003229 plane->state->rotation = DRM_ROTATE_0;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003230 }
Daniel Vetterd4617012014-11-03 15:56:43 +01003231}
3232EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3233
3234/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003235 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3236 * @plane: plane object
3237 * @state: atomic plane state
3238 *
3239 * Copies atomic state from a plane's current state. This is useful for
3240 * drivers that subclass the plane state.
3241 */
3242void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3243 struct drm_plane_state *state)
3244{
3245 memcpy(state, plane->state, sizeof(*state));
3246
3247 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003248 drm_framebuffer_get(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003249
3250 state->fence = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003251}
3252EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3253
3254/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003255 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3256 * @plane: drm plane
3257 *
3258 * Default plane state duplicate hook for drivers which don't have their own
3259 * subclassed plane state structure.
3260 */
3261struct drm_plane_state *
3262drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3263{
Daniel Vetter321ebf02014-11-04 22:57:27 +01003264 struct drm_plane_state *state;
3265
Daniel Vetterd4617012014-11-03 15:56:43 +01003266 if (WARN_ON(!plane->state))
3267 return NULL;
3268
Thierry Redingf5e78402015-01-28 14:54:32 +01003269 state = kmalloc(sizeof(*state), GFP_KERNEL);
3270 if (state)
3271 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003272
3273 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003274}
3275EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3276
3277/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003278 * __drm_atomic_helper_plane_destroy_state - release plane state
Thierry Redingf5e78402015-01-28 14:54:32 +01003279 * @state: plane state object to release
3280 *
3281 * Releases all resources stored in the plane state without actually freeing
3282 * the memory of the plane state. This is useful for drivers that subclass the
3283 * plane state.
3284 */
Daniel Vetter2f701692016-05-09 16:34:10 +02003285void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003286{
3287 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003288 drm_framebuffer_put(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003289
3290 if (state->fence)
3291 dma_fence_put(state->fence);
Thierry Redingf5e78402015-01-28 14:54:32 +01003292}
3293EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3294
3295/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003296 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3297 * @plane: drm plane
3298 * @state: plane state object to release
3299 *
3300 * Default plane state destroy hook for drivers which don't have their own
3301 * subclassed plane state structure.
3302 */
3303void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01003304 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01003305{
Daniel Vetter2f701692016-05-09 16:34:10 +02003306 __drm_atomic_helper_plane_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003307 kfree(state);
3308}
3309EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3310
3311/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003312 * __drm_atomic_helper_connector_reset - reset state on connector
3313 * @connector: drm connector
3314 * @conn_state: connector state to assign
3315 *
3316 * Initializes the newly allocated @conn_state and assigns it to
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003317 * the &drm_conector->state pointer of @connector, usually required when
3318 * initializing the drivers or when called from the &drm_connector_funcs.reset
3319 * hook.
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003320 *
3321 * This is useful for drivers that subclass the connector state.
3322 */
3323void
3324__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3325 struct drm_connector_state *conn_state)
3326{
3327 if (conn_state)
3328 conn_state->connector = connector;
3329
3330 connector->state = conn_state;
3331}
3332EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3333
3334/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003335 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
Daniel Vetterd4617012014-11-03 15:56:43 +01003336 * @connector: drm connector
3337 *
3338 * Resets the atomic state for @connector by freeing the state pointer (which
3339 * might be NULL, e.g. at driver load time) and allocating a new empty state
3340 * object.
3341 */
3342void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3343{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003344 struct drm_connector_state *conn_state =
3345 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003346
Daniel Vetterb0b55112016-04-22 22:10:29 +02003347 if (connector->state)
Daniel Vetterfabd9102016-05-09 16:34:11 +02003348 __drm_atomic_helper_connector_destroy_state(connector->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003349
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003350 kfree(connector->state);
3351 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003352}
3353EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3354
3355/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003356 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3357 * @connector: connector object
3358 * @state: atomic connector state
3359 *
3360 * Copies atomic state from a connector's current state. This is useful for
3361 * drivers that subclass the connector state.
3362 */
3363void
3364__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3365 struct drm_connector_state *state)
3366{
3367 memcpy(state, connector->state, sizeof(*state));
Dave Airlied2307de2016-04-27 11:27:39 +10003368 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003369 drm_connector_get(connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003370}
3371EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3372
3373/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003374 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3375 * @connector: drm connector
3376 *
3377 * Default connector state duplicate hook for drivers which don't have their own
3378 * subclassed connector state structure.
3379 */
3380struct drm_connector_state *
3381drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3382{
Thierry Redingf5e78402015-01-28 14:54:32 +01003383 struct drm_connector_state *state;
3384
Daniel Vetterd4617012014-11-03 15:56:43 +01003385 if (WARN_ON(!connector->state))
3386 return NULL;
3387
Thierry Redingf5e78402015-01-28 14:54:32 +01003388 state = kmalloc(sizeof(*state), GFP_KERNEL);
3389 if (state)
3390 __drm_atomic_helper_connector_duplicate_state(connector, state);
3391
3392 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003393}
3394EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3395
3396/**
Thierry Reding397fd772015-09-08 15:00:45 +02003397 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3398 * @dev: DRM device
3399 * @ctx: lock acquisition context
3400 *
3401 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01003402 * duplicating their respective states. This is used for example by suspend/
3403 * resume support code to save the state prior to suspend such that it can
3404 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02003405 *
3406 * Note that this treats atomic state as persistent between save and restore.
3407 * Drivers must make sure that this is possible and won't result in confusion
3408 * or erroneous behaviour.
3409 *
3410 * Note that if callers haven't already acquired all modeset locks this might
3411 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3412 *
3413 * Returns:
3414 * A pointer to the copy of the atomic state object on success or an
3415 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01003416 *
3417 * See also:
3418 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02003419 */
3420struct drm_atomic_state *
3421drm_atomic_helper_duplicate_state(struct drm_device *dev,
3422 struct drm_modeset_acquire_ctx *ctx)
3423{
3424 struct drm_atomic_state *state;
3425 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003426 struct drm_connector_list_iter conn_iter;
Thierry Reding397fd772015-09-08 15:00:45 +02003427 struct drm_plane *plane;
3428 struct drm_crtc *crtc;
3429 int err = 0;
3430
3431 state = drm_atomic_state_alloc(dev);
3432 if (!state)
3433 return ERR_PTR(-ENOMEM);
3434
3435 state->acquire_ctx = ctx;
3436
3437 drm_for_each_crtc(crtc, dev) {
3438 struct drm_crtc_state *crtc_state;
3439
3440 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3441 if (IS_ERR(crtc_state)) {
3442 err = PTR_ERR(crtc_state);
3443 goto free;
3444 }
3445 }
3446
3447 drm_for_each_plane(plane, dev) {
3448 struct drm_plane_state *plane_state;
3449
3450 plane_state = drm_atomic_get_plane_state(state, plane);
3451 if (IS_ERR(plane_state)) {
3452 err = PTR_ERR(plane_state);
3453 goto free;
3454 }
3455 }
3456
Thierry Redingb982dab2017-02-28 15:46:43 +01003457 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003458 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding397fd772015-09-08 15:00:45 +02003459 struct drm_connector_state *conn_state;
3460
3461 conn_state = drm_atomic_get_connector_state(state, conn);
3462 if (IS_ERR(conn_state)) {
3463 err = PTR_ERR(conn_state);
Thierry Redingb982dab2017-02-28 15:46:43 +01003464 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003465 goto free;
3466 }
3467 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003468 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003469
3470 /* clear the acquire context so that it isn't accidentally reused */
3471 state->acquire_ctx = NULL;
3472
3473free:
3474 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003475 drm_atomic_state_put(state);
Thierry Reding397fd772015-09-08 15:00:45 +02003476 state = ERR_PTR(err);
3477 }
3478
3479 return state;
3480}
3481EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3482
3483/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003484 * __drm_atomic_helper_connector_destroy_state - release connector state
Thierry Redingf5e78402015-01-28 14:54:32 +01003485 * @state: connector state object to release
3486 *
3487 * Releases all resources stored in the connector state without actually
3488 * freeing the memory of the connector state. This is useful for drivers that
3489 * subclass the connector state.
3490 */
3491void
Daniel Vetterfabd9102016-05-09 16:34:11 +02003492__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003493{
Dave Airlied2307de2016-04-27 11:27:39 +10003494 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003495 drm_connector_put(state->connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003496}
3497EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3498
3499/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003500 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3501 * @connector: drm connector
3502 * @state: connector state object to release
3503 *
3504 * Default connector state destroy hook for drivers which don't have their own
3505 * subclassed connector state structure.
3506 */
3507void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3508 struct drm_connector_state *state)
3509{
Daniel Vetterfabd9102016-05-09 16:34:11 +02003510 __drm_atomic_helper_connector_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003511 kfree(state);
3512}
3513EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003514
3515/**
3516 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3517 * @crtc: CRTC object
3518 * @red: red correction table
3519 * @green: green correction table
3520 * @blue: green correction table
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003521 * @size: size of the tables
3522 *
3523 * Implements support for legacy gamma correction table for drivers
3524 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3525 * properties.
3526 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003527int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3528 u16 *red, u16 *green, u16 *blue,
3529 uint32_t size)
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003530{
3531 struct drm_device *dev = crtc->dev;
3532 struct drm_mode_config *config = &dev->mode_config;
3533 struct drm_atomic_state *state;
3534 struct drm_crtc_state *crtc_state;
3535 struct drm_property_blob *blob = NULL;
3536 struct drm_color_lut *blob_data;
3537 int i, ret = 0;
3538
3539 state = drm_atomic_state_alloc(crtc->dev);
3540 if (!state)
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003541 return -ENOMEM;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003542
3543 blob = drm_property_create_blob(dev,
3544 sizeof(struct drm_color_lut) * size,
3545 NULL);
Lionel Landwerlin562c5b42016-03-10 12:04:21 +00003546 if (IS_ERR(blob)) {
3547 ret = PTR_ERR(blob);
Lionel Landwerlinc1f415c2016-03-11 12:17:26 +00003548 blob = NULL;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003549 goto fail;
3550 }
3551
3552 /* Prepare GAMMA_LUT with the legacy values. */
3553 blob_data = (struct drm_color_lut *) blob->data;
3554 for (i = 0; i < size; i++) {
3555 blob_data[i].red = red[i];
3556 blob_data[i].green = green[i];
3557 blob_data[i].blue = blue[i];
3558 }
3559
3560 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3561retry:
3562 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3563 if (IS_ERR(crtc_state)) {
3564 ret = PTR_ERR(crtc_state);
3565 goto fail;
3566 }
3567
3568 /* Reset DEGAMMA_LUT and CTM properties. */
3569 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3570 config->degamma_lut_property, 0);
3571 if (ret)
3572 goto fail;
3573
3574 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3575 config->ctm_property, 0);
3576 if (ret)
3577 goto fail;
3578
3579 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3580 config->gamma_lut_property, blob->base.id);
3581 if (ret)
3582 goto fail;
3583
3584 ret = drm_atomic_commit(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003585fail:
3586 if (ret == -EDEADLK)
3587 goto backoff;
3588
Chris Wilson08536952016-10-14 13:18:18 +01003589 drm_atomic_state_put(state);
Thierry Reding6472e502017-02-28 15:46:42 +01003590 drm_property_blob_put(blob);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003591 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003592
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003593backoff:
3594 drm_atomic_state_clear(state);
3595 drm_atomic_legacy_backoff(state);
3596
3597 goto retry;
3598}
3599EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);