blob: ff3c6eb5b6bd92061c0d89ca8bea147f6b8b2dca [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) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010073 crtc_state = drm_atomic_get_new_crtc_state(state,
74 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) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010083 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010084
85 if (WARN_ON(!crtc_state))
86 return;
87
88 crtc_state->planes_changed = true;
89 }
90}
91
Maarten Lankhorst8248b652016-03-03 10:17:40 +010092static int handle_conflicting_encoders(struct drm_atomic_state *state,
93 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +010094{
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010095 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +020096 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +010097 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst40616a22016-03-03 10:17:39 +010098 struct drm_encoder *encoder;
99 unsigned encoder_mask = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100100 int i, ret = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200101
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100102 /*
103 * First loop, find all newly assigned encoders from the connectors
104 * part of the state. If the same encoder is assigned to multiple
105 * connectors bail out.
106 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100107 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100108 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
109 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200110
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100111 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200112 continue;
113
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100114 if (funcs->atomic_best_encoder)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100115 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200116 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100117 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200118 else
119 new_encoder = drm_atomic_helper_best_encoder(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100120
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100121 if (new_encoder) {
122 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
123 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
124 new_encoder->base.id, new_encoder->name,
125 connector->base.id, connector->name);
126
127 return -EINVAL;
128 }
129
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100130 encoder_mask |= 1 << drm_encoder_index(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100131 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200132 }
133
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100134 if (!encoder_mask)
135 return 0;
136
137 /*
138 * Second loop, iterate over all connectors not part of the state.
139 *
140 * If a conflicting encoder is found and disable_conflicting_encoders
141 * is not set, an error is returned. Userspace can provide a solution
142 * through the atomic ioctl.
143 *
144 * If the flag is set conflicting connectors are removed from the crtc
145 * and the crtc is disabled if no encoder is left. This preserves
146 * compatibility with the legacy set_config behavior.
147 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100148 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100149 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100150 struct drm_crtc_state *crtc_state;
151
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100152 if (drm_atomic_get_new_connector_state(state, connector))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100153 continue;
154
155 encoder = connector->state->best_encoder;
156 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
157 continue;
158
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100159 if (!disable_conflicting_encoders) {
160 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
161 encoder->base.id, encoder->name,
162 connector->state->crtc->base.id,
163 connector->state->crtc->name,
164 connector->base.id, connector->name);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100165 ret = -EINVAL;
166 goto out;
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100167 }
168
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100169 new_conn_state = drm_atomic_get_connector_state(state, connector);
170 if (IS_ERR(new_conn_state)) {
171 ret = PTR_ERR(new_conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100172 goto out;
173 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100174
175 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
176 encoder->base.id, encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100177 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100178 connector->base.id, connector->name);
179
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100180 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100181
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100182 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100183 if (ret)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100184 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100185
186 if (!crtc_state->connector_mask) {
187 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
188 NULL);
189 if (ret < 0)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100190 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100191
192 crtc_state->active = false;
193 }
194 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100195out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100196 drm_connector_list_iter_end(&conn_iter);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100197
Daniel Vetterc36a3252016-12-15 16:58:43 +0100198 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200199}
200
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100201static void
202set_best_encoder(struct drm_atomic_state *state,
203 struct drm_connector_state *conn_state,
204 struct drm_encoder *encoder)
205{
206 struct drm_crtc_state *crtc_state;
207 struct drm_crtc *crtc;
208
209 if (conn_state->best_encoder) {
210 /* Unset the encoder_mask in the old crtc state. */
211 crtc = conn_state->connector->state->crtc;
212
213 /* A NULL crtc is an error here because we should have
214 * duplicated a NULL best_encoder when crtc was NULL.
215 * As an exception restoring duplicated atomic state
216 * during resume is allowed, so don't warn when
217 * best_encoder is equal to encoder we intend to set.
218 */
219 WARN_ON(!crtc && encoder != conn_state->best_encoder);
220 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100221 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100222
223 crtc_state->encoder_mask &=
224 ~(1 << drm_encoder_index(conn_state->best_encoder));
225 }
226 }
227
228 if (encoder) {
229 crtc = conn_state->crtc;
230 WARN_ON(!crtc);
231 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100232 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100233
234 crtc_state->encoder_mask |=
235 1 << drm_encoder_index(encoder);
236 }
237 }
238
239 conn_state->best_encoder = encoder;
240}
241
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100242static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200243steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100244 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200245{
Daniel Vetter623369e2014-09-16 17:50:47 +0200246 struct drm_crtc_state *crtc_state;
247 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100248 struct drm_connector_state *old_connector_state, *new_connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100249 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200250
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100251 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100252 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200253
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100254 if (new_connector_state->best_encoder != encoder)
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100255 continue;
256
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100257 encoder_crtc = old_connector_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200258
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100259 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
260 encoder->base.id, encoder->name,
261 encoder_crtc->base.id, encoder_crtc->name);
262
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100263 set_best_encoder(state, new_connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100264
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100265 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100266 crtc_state->connectors_changed = true;
267
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100268 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200269 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200270}
271
272static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100273update_connector_routing(struct drm_atomic_state *state,
274 struct drm_connector *connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100275 struct drm_connector_state *old_connector_state,
276 struct drm_connector_state *new_connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200277{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200278 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200279 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200280 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200281
Daniel Vetter17a38d92015-02-22 12:24:16 +0100282 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
283 connector->base.id,
284 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200285
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100286 if (old_connector_state->crtc != new_connector_state->crtc) {
287 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100288 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200289 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200290 }
291
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100292 if (new_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100293 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200294 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200295 }
296 }
297
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100298 if (!new_connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100299 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200300 connector->base.id,
301 connector->name);
302
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100303 set_best_encoder(state, new_connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200304
305 return 0;
306 }
307
308 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200309
310 if (funcs->atomic_best_encoder)
311 new_encoder = funcs->atomic_best_encoder(connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100312 new_connector_state);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200313 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200314 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200315 else
316 new_encoder = drm_atomic_helper_best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200317
318 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100319 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
320 connector->base.id,
321 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200322 return -EINVAL;
323 }
324
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100325 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
Russell King6ac7c542017-02-13 12:27:03 +0000326 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100327 new_encoder->base.id,
328 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100329 new_connector_state->crtc->base.id,
330 new_connector_state->crtc->name);
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100331 return -EINVAL;
332 }
333
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100334 if (new_encoder == new_connector_state->best_encoder) {
335 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100336
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200337 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100338 connector->base.id,
339 connector->name,
340 new_encoder->base.id,
341 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100342 new_connector_state->crtc->base.id,
343 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200344
345 return 0;
346 }
347
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100348 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200349
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100350 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100351
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100352 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200353 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200354
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200355 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100356 connector->base.id,
357 connector->name,
358 new_encoder->base.id,
359 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100360 new_connector_state->crtc->base.id,
361 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200362
363 return 0;
364}
365
366static int
367mode_fixup(struct drm_atomic_state *state)
368{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300369 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100370 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300371 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100372 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200373 int i;
Dan Carpenterf9ad86e2017-02-08 02:46:01 +0300374 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200375
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100376 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
377 if (!new_crtc_state->mode_changed &&
378 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200379 continue;
380
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100381 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200382 }
383
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100384 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200385 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200386 struct drm_encoder *encoder;
387
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100388 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200389
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100390 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200391 continue;
392
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100393 new_crtc_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100394 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200395
396 /*
397 * Each encoder has at most one connector (since we always steal
398 * it away), so we won't call ->mode_fixup twice.
399 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100400 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200401 funcs = encoder->helper_private;
402
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100403 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
404 &new_crtc_state->adjusted_mode);
Archit Taneja862e6862015-05-21 11:03:16 +0530405 if (!ret) {
406 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
407 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200408 }
409
Noralf Trønnes28276352016-05-11 18:09:20 +0200410 if (funcs && funcs->atomic_check) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100411 ret = funcs->atomic_check(encoder, new_crtc_state,
412 new_conn_state);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100413 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100414 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
415 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100416 return ret;
417 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200418 } else if (funcs && funcs->mode_fixup) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100419 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
420 &new_crtc_state->adjusted_mode);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100421 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100422 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
423 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100424 return -EINVAL;
425 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200426 }
427 }
428
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100429 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200430 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200431
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100432 if (!new_crtc_state->enable)
Liu Yingf55f1702016-05-27 17:35:54 +0800433 continue;
434
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100435 if (!new_crtc_state->mode_changed &&
436 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200437 continue;
438
439 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300440 if (!funcs->mode_fixup)
441 continue;
442
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100443 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
444 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200445 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200446 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
447 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200448 return -EINVAL;
449 }
450 }
451
452 return 0;
453}
454
Daniel Vetterd9b13622014-11-26 16:57:41 +0100455/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800456 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100457 * @dev: DRM device
458 * @state: the driver state object
459 *
460 * Check the state object to see if the requested state is physically possible.
461 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200462 * update and adds any additional connectors needed for full modesets and calls
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100463 * down into &drm_crtc_helper_funcs.mode_fixup and
464 * &drm_encoder_helper_funcs.mode_fixup or
465 * &drm_encoder_helper_funcs.atomic_check functions of the driver backend.
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200466 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100467 * &drm_crtc_state.mode_changed is set when the input mode is changed.
468 * &drm_crtc_state.connectors_changed is set when a connector is added or
469 * removed from the crtc. &drm_crtc_state.active_changed is set when
470 * &drm_crtc_state.active changes, which is used for DPMS.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100471 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100472 *
473 * IMPORTANT:
474 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100475 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
476 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
477 * without a full modeset) _must_ call this function afterwards after that
478 * change. It is permitted to call this function multiple times for the same
479 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
480 * upon the adjusted dotclock for fifo space allocation and watermark
481 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100482 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200483 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100484 * Zero for success or -errno
485 */
486int
Rob Clark934ce1c2014-11-19 16:41:33 -0500487drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200488 struct drm_atomic_state *state)
489{
Daniel Vetter623369e2014-09-16 17:50:47 +0200490 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100491 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300492 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100493 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200494 int i, ret;
495
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100496 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200497 bool has_connectors =
498 !!new_crtc_state->connector_mask;
499
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100500 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200501 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
502 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100503 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200504 }
505
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100506 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200507 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
508 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200509
510 /*
511 * For clarity this assignment is done here, but
512 * enable == 0 is only true when there are no
513 * connectors and a NULL mode.
514 *
515 * The other way around is true as well. enable != 0
516 * iff connectors are attached and a mode is set.
517 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100518 new_crtc_state->mode_changed = true;
519 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200520 }
Maarten Lankhorst24d66522017-04-06 13:19:01 +0200521
522 if (old_crtc_state->active != new_crtc_state->active) {
523 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
524 crtc->base.id, crtc->name);
525 new_crtc_state->active_changed = true;
526 }
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200527
528 if (new_crtc_state->enable != has_connectors) {
529 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
530 crtc->base.id, crtc->name);
531
532 return -EINVAL;
533 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200534 }
535
Maarten Lankhorst44596b82017-04-06 13:19:00 +0200536 ret = handle_conflicting_encoders(state, false);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100537 if (ret)
538 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100539
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100540 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200541 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100542 * This only sets crtc->connectors_changed for routing changes,
543 * drivers must set crtc->connectors_changed themselves when
544 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200545 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100546 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100547 old_connector_state,
548 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200549 if (ret)
550 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100551 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100552 new_crtc_state = drm_atomic_get_new_crtc_state(state,
553 old_connector_state->crtc);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100554 if (old_connector_state->link_status !=
555 new_connector_state->link_status)
556 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200557 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200558 }
559
560 /*
561 * After all the routing has been prepared we need to add in any
562 * connector which is itself unchanged, but who's crtc changes it's
563 * configuration. This must be done before calling mode_fixup in case a
564 * crtc only changed its mode but has the same set of connectors.
565 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100566 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100567 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100568 continue;
569
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200570 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
571 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100572 new_crtc_state->enable ? 'y' : 'n',
573 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200574
575 ret = drm_atomic_add_affected_connectors(state, crtc);
576 if (ret != 0)
577 return ret;
578
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200579 ret = drm_atomic_add_affected_planes(state, crtc);
580 if (ret != 0)
581 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200582 }
583
584 return mode_fixup(state);
585}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100586EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200587
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100588/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800589 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100590 * @dev: DRM device
591 * @state: the driver state object
592 *
593 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100594 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100595 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
596 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100597 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100598 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200599 * updated planes.
600 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200601 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100602 * Zero for success or -errno
603 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100604int
605drm_atomic_helper_check_planes(struct drm_device *dev,
606 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100607{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300608 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100609 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300610 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100611 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100612 int i, ret = 0;
613
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100614 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200615 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100616
617 funcs = plane->helper_private;
618
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100619 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100620
621 if (!funcs || !funcs->atomic_check)
622 continue;
623
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100624 ret = funcs->atomic_check(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100625 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200626 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
627 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100628 return ret;
629 }
630 }
631
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100632 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200633 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100634
635 funcs = crtc->helper_private;
636
637 if (!funcs || !funcs->atomic_check)
638 continue;
639
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100640 ret = funcs->atomic_check(crtc, new_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100641 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200642 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
643 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100644 return ret;
645 }
646 }
647
Daniel Vetterd9b13622014-11-26 16:57:41 +0100648 return ret;
649}
650EXPORT_SYMBOL(drm_atomic_helper_check_planes);
651
652/**
653 * drm_atomic_helper_check - validate state object
654 * @dev: DRM device
655 * @state: the driver state object
656 *
657 * Check the state object to see if the requested state is physically possible.
658 * Only crtcs and planes have check callbacks, so for any additional (global)
659 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100660 * Drivers without such needs can directly use this as their
661 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100662 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100663 * This just wraps the two parts of the state checking for planes and modeset
664 * state in the default order: First it calls drm_atomic_helper_check_modeset()
665 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100666 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
667 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
668 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100669 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200670 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100671 * Zero for success or -errno
672 */
673int drm_atomic_helper_check(struct drm_device *dev,
674 struct drm_atomic_state *state)
675{
676 int ret;
677
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100678 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100679 if (ret)
680 return ret;
681
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100682 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500683 if (ret)
684 return ret;
685
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100686 return ret;
687}
688EXPORT_SYMBOL(drm_atomic_helper_check);
689
Daniel Vetter623369e2014-09-16 17:50:47 +0200690static void
691disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
692{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300693 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100694 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300695 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100696 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200697 int i;
698
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100699 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 +0200700 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200701 struct drm_encoder *encoder;
702
Daniel Vetter623369e2014-09-16 17:50:47 +0200703 /* Shut down everything that's in the changeset and currently
704 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300705 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200706 continue;
707
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100708 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100709
Daniel Vetter4218a322015-03-26 22:18:40 +0100710 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200711 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100712 continue;
713
Rob Clark46df9ad2014-11-20 15:40:36 -0500714 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200715
Rob Clark46df9ad2014-11-20 15:40:36 -0500716 /* We shouldn't get this far if we didn't previously have
717 * an encoder.. but WARN_ON() rather than explode.
718 */
719 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200720 continue;
721
722 funcs = encoder->helper_private;
723
Daniel Vetter17a38d92015-02-22 12:24:16 +0100724 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
725 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100726
Daniel Vetter623369e2014-09-16 17:50:47 +0200727 /*
728 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800729 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200730 */
Archit Taneja862e6862015-05-21 11:03:16 +0530731 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200732
733 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200734 if (funcs) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100735 if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +0200736 funcs->prepare(encoder);
737 else if (funcs->disable)
738 funcs->disable(encoder);
739 else if (funcs->dpms)
740 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
741 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200742
Archit Taneja862e6862015-05-21 11:03:16 +0530743 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200744 }
745
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100746 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 +0200747 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200748
749 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100750 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100751 continue;
752
753 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200754 continue;
755
756 funcs = crtc->helper_private;
757
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200758 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
759 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100760
761
Daniel Vetter623369e2014-09-16 17:50:47 +0200762 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100763 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200764 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +0800765 else if (funcs->atomic_disable)
766 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200767 else if (funcs->disable)
768 funcs->disable(crtc);
769 else
770 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
771 }
772}
773
Daniel Vetter4c18d302015-05-12 15:27:37 +0200774/**
775 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
776 * @dev: DRM device
777 * @old_state: atomic state object with old state structures
778 *
779 * This function updates all the various legacy modeset state pointers in
780 * connectors, encoders and crtcs. It also updates the timestamping constants
781 * used for precise vblank timestamps by calling
782 * drm_calc_timestamping_constants().
783 *
784 * Drivers can use this for building their own atomic commit if they don't have
785 * a pure helper-based modeset implementation.
786 */
787void
788drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
789 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200790{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300791 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100792 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300793 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100794 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200795 int i;
796
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200797 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100798 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200799 if (connector->encoder) {
800 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200801
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200802 connector->encoder->crtc = NULL;
803 connector->encoder = NULL;
804 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200805
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100806 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200807 if ((!crtc && old_conn_state->crtc) ||
808 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
809 struct drm_property *dpms_prop =
810 dev->mode_config.dpms_property;
811 int mode = DRM_MODE_DPMS_OFF;
812
813 if (crtc && crtc->state->active)
814 mode = DRM_MODE_DPMS_ON;
815
816 connector->dpms = mode;
817 drm_object_property_set_value(&connector->base,
818 dpms_prop, mode);
819 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200820 }
821
822 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100823 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
824 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200825 continue;
826
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100827 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200828 continue;
829
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100830 connector->encoder = new_conn_state->best_encoder;
831 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200832 }
833
834 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100835 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200836 struct drm_plane *primary = crtc->primary;
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100837 struct drm_plane_state *new_plane_state;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200838
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100839 crtc->mode = new_crtc_state->mode;
840 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200841
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100842 new_plane_state =
843 drm_atomic_get_new_plane_state(old_state, primary);
844
845 if (new_plane_state && new_plane_state->crtc == crtc) {
846 crtc->x = new_plane_state->src_x >> 16;
847 crtc->y = new_plane_state->src_y >> 16;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200848 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200849
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100850 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200851 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100852 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200853 }
854}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200855EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200856
857static void
858crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
859{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300860 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100861 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300862 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100863 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200864 int i;
865
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100866 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200867 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200868
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100869 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200870 continue;
871
872 funcs = crtc->helper_private;
873
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100874 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200875 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
876 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100877
Daniel Vetter623369e2014-09-16 17:50:47 +0200878 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100879 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200880 }
881
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100882 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200883 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200884 struct drm_encoder *encoder;
885 struct drm_display_mode *mode, *adjusted_mode;
886
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100887 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200888 continue;
889
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100890 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200891 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100892 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200893 mode = &new_crtc_state->mode;
894 adjusted_mode = &new_crtc_state->adjusted_mode;
895
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100896 if (!new_crtc_state->mode_changed)
897 continue;
898
Daniel Vetter17a38d92015-02-22 12:24:16 +0100899 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
900 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100901
Daniel Vetter623369e2014-09-16 17:50:47 +0200902 /*
903 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800904 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200905 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200906 if (funcs && funcs->atomic_mode_set) {
907 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100908 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200909 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +0100910 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200911 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200912
Archit Taneja862e6862015-05-21 11:03:16 +0530913 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200914 }
915}
916
917/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100918 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200919 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100920 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200921 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100922 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200923 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100924 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300925 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100926 * drm_atomic_helper_commit_planes(), which is what the default commit function
927 * does. But drivers with different needs can group the modeset commits together
928 * and do the plane commits at the end. This is useful for drivers doing runtime
929 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200930 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100931void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
932 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200933{
Laurent Pincharta072f802015-02-22 12:24:18 +0100934 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200935
936 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
937
Laurent Pincharta072f802015-02-22 12:24:18 +0100938 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200939}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100940EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200941
942/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100943 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200944 * @dev: DRM device
945 * @old_state: atomic state object with old state structures
946 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100947 * This function enables all the outputs with the new configuration which had to
948 * be turned off for the update.
949 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300950 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100951 * drm_atomic_helper_commit_planes(), which is what the default commit function
952 * does. But drivers with different needs can group the modeset commits together
953 * and do the plane commits at the end. This is useful for drivers doing runtime
954 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200955 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100956void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
957 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200958{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300959 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100960 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300961 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100962 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200963 int i;
964
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100965 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200966 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200967
968 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100969 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100970 continue;
971
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100972 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200973 continue;
974
975 funcs = crtc->helper_private;
976
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100977 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200978 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
979 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100980
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100981 if (funcs->enable)
982 funcs->enable(crtc);
983 else
984 funcs->commit(crtc);
985 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200986 }
987
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100988 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200989 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200990 struct drm_encoder *encoder;
991
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100992 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200993 continue;
994
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100995 if (!new_conn_state->crtc->state->active ||
996 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100997 continue;
998
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100999 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001000 funcs = encoder->helper_private;
1001
Daniel Vetter17a38d92015-02-22 12:24:16 +01001002 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1003 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001004
Daniel Vetter623369e2014-09-16 17:50:47 +02001005 /*
1006 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001007 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001008 */
Archit Taneja862e6862015-05-21 11:03:16 +05301009 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001010
Noralf Trønnes28276352016-05-11 18:09:20 +02001011 if (funcs) {
1012 if (funcs->enable)
1013 funcs->enable(encoder);
1014 else if (funcs->commit)
1015 funcs->commit(encoder);
1016 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001017
Archit Taneja862e6862015-05-21 11:03:16 +05301018 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001019 }
1020}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001021EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001022
Rob Clark4c5b7f32016-03-18 19:14:55 -04001023/**
1024 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1025 * @dev: DRM device
1026 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001027 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1028 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001029 *
1030 * For implicit sync, driver should fish the exclusive fence out from the
1031 * incoming fb's and stash it in the drm_plane_state. This is called after
1032 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1033 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001034 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001035 * Note that @pre_swap is needed since the point where we block for fences moves
1036 * around depending upon whether an atomic commit is blocking or
1037 * non-blocking. For async commit all waiting needs to happen after
1038 * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1039 * to wait **before** we do anything that can't be easily rolled back. That is
1040 * before we call drm_atomic_helper_swap_state().
1041 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001042 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001043 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001044int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1045 struct drm_atomic_state *state,
1046 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001047{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001048 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001049 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001050 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001051
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001052 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1053 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001054 continue;
1055
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001056 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001057
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001058 /*
1059 * If waiting for fences pre-swap (ie: nonblock), userspace can
1060 * still interrupt the operation. Instead of blocking until the
1061 * timer expires, make the wait interruptible.
1062 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001063 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001064 if (ret)
1065 return ret;
1066
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001067 dma_fence_put(new_plane_state->fence);
1068 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001069 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001070
1071 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001072}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001073EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001074
John Keepingc2409062016-01-19 10:46:58 +00001075/**
Rob Clark5ee32292014-11-11 19:38:59 -05001076 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1077 * @dev: DRM device
1078 * @old_state: atomic state object with old state structures
1079 *
1080 * Helper to, after atomic commit, wait for vblanks on all effected
1081 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001082 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1083 * framebuffers have actually changed to optimize for the legacy cursor and
1084 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001085 */
1086void
1087drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1088 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001089{
1090 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001091 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001092 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001093 unsigned crtc_mask = 0;
1094
1095 /*
1096 * Legacy cursor ioctls are completely unsynced, and userspace
1097 * relies on that (by doing tons of cursor updates).
1098 */
1099 if (old_state->legacy_cursor_update)
1100 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001101
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001102 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta3fbb532016-12-08 14:45:27 +01001103 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
Daniel Vetterab58e332014-11-24 20:42:42 +01001104 continue;
1105
Daniel Vetter623369e2014-09-16 17:50:47 +02001106 ret = drm_crtc_vblank_get(crtc);
1107 if (ret != 0)
1108 continue;
1109
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001110 crtc_mask |= drm_crtc_mask(crtc);
1111 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001112 }
1113
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001114 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001115 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001116 continue;
1117
1118 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001119 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001120 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001121 msecs_to_jiffies(50));
1122
Russell King6ac7c542017-02-13 12:27:03 +00001123 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1124 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001125
Daniel Vetter623369e2014-09-16 17:50:47 +02001126 drm_crtc_vblank_put(crtc);
1127 }
1128}
Rob Clark5ee32292014-11-11 19:38:59 -05001129EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001130
1131/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001132 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001133 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001134 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001135 * This is the default implementation for the
1136 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
Daniel Vetter623369e2014-09-16 17:50:47 +02001137 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001138 * Note that the default ordering of how the various stages are called is to
1139 * match the legacy modeset helper library closest. One peculiarity of that is
1140 * that it doesn't mesh well with runtime PM at all.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001141 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001142 * For drivers supporting runtime PM the recommended sequence is instead ::
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001143 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001144 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001145 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001146 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001147 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001148 * drm_atomic_helper_commit_planes(dev, old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001149 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001150 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001151 * for committing the atomic update to hardware. See the kerneldoc entries for
1152 * these three functions for more details.
1153 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001154void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001155{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001156 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001157
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001158 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001159
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001160 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001161
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001162 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001163
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001164 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001165
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001166 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001167
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001168 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001169}
1170EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1171
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001172static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001173{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001174 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001175 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001176
1177 funcs = dev->mode_config.helper_private;
1178
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001179 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001180
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001181 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001182
1183 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001184 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001185 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001186 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001187
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001188 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001189
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001190 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001191}
1192
1193static void commit_work(struct work_struct *work)
1194{
1195 struct drm_atomic_state *state = container_of(work,
1196 struct drm_atomic_state,
1197 commit_work);
1198 commit_tail(state);
1199}
1200
1201/**
1202 * drm_atomic_helper_commit - commit validated state object
1203 * @dev: DRM device
1204 * @state: the driver state object
1205 * @nonblock: whether nonblocking behavior is requested.
1206 *
1207 * This function commits a with drm_atomic_helper_check() pre-validated state
1208 * object. This can still fail when e.g. the framebuffer reservation fails. This
1209 * function implements nonblocking commits, using
1210 * drm_atomic_helper_setup_commit() and related functions.
1211 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001212 * Committing the actual hardware state is done through the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001213 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1214 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001215 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001216 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001217 * Zero for success or -errno.
1218 */
1219int drm_atomic_helper_commit(struct drm_device *dev,
1220 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001221 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001222{
1223 int ret;
1224
Daniel Vettera095caa2016-06-08 17:15:36 +02001225 ret = drm_atomic_helper_setup_commit(state, nonblock);
1226 if (ret)
1227 return ret;
1228
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001229 INIT_WORK(&state->commit_work, commit_work);
1230
Daniel Vetter623369e2014-09-16 17:50:47 +02001231 ret = drm_atomic_helper_prepare_planes(dev, state);
1232 if (ret)
1233 return ret;
1234
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001235 if (!nonblock) {
1236 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001237 if (ret) {
1238 drm_atomic_helper_cleanup_planes(dev, state);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001239 return ret;
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001240 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001241 }
1242
Daniel Vetter623369e2014-09-16 17:50:47 +02001243 /*
1244 * This is the point of no return - everything below never fails except
1245 * when the hw goes bonghits. Which means we can commit the new state on
1246 * the software side now.
1247 */
1248
Daniel Vetter5e84c262016-06-10 00:06:32 +02001249 drm_atomic_helper_swap_state(state, true);
Daniel Vetter623369e2014-09-16 17:50:47 +02001250
1251 /*
1252 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001253 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001254 * that the asynchronous work has either been cancelled (if the driver
1255 * supports it, which at least requires that the framebuffers get
1256 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1257 * before the new state gets committed on the software side with
1258 * drm_atomic_helper_swap_state().
1259 *
1260 * This scheme allows new atomic state updates to be prepared and
1261 * checked in parallel to the asynchronous completion of the previous
1262 * update. Which is important since compositors need to figure out the
1263 * composition of the next frame right after having submitted the
1264 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001265 *
1266 * NOTE: Commit work has multiple phases, first hardware commit, then
1267 * cleanup. We want them to overlap, hence need system_unbound_wq to
1268 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001269 */
1270
Chris Wilson08536952016-10-14 13:18:18 +01001271 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001272 if (nonblock)
1273 queue_work(system_unbound_wq, &state->commit_work);
1274 else
1275 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001276
1277 return 0;
1278}
1279EXPORT_SYMBOL(drm_atomic_helper_commit);
1280
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001281/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001282 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001283 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001284 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001285 *
1286 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1287 * which commit needs to call which can fail, so we want to run it first and
1288 * synchronously.
1289 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001290 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001291 * might be affected the new state update. This can be done by either cancelling
1292 * or flushing the work items, depending upon whether the driver can deal with
1293 * cancelled updates. Note that it is important to ensure that the framebuffer
1294 * cleanup is still done when cancelling.
1295 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001296 * Asynchronous workers need to have sufficient parallelism to be able to run
1297 * different atomic commits on different CRTCs in parallel. The simplest way to
1298 * achive this is by running them on the &system_unbound_wq work queue. Note
1299 * that drivers are not required to split up atomic commits and run an
1300 * individual commit in parallel - userspace is supposed to do that if it cares.
1301 * But it might be beneficial to do that for modesets, since those necessarily
1302 * must be done as one global operation, and enabling or disabling a CRTC can
1303 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001304 *
1305 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001306 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001307 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001308 * while it's guaranteed that no relevant nonblocking worker runs means that
1309 * nonblocking workers do not need grab any locks. Actually they must not grab
1310 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001311 *
1312 * 4. Schedule a work item to do all subsequent steps, using the split-out
1313 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1314 * then cleaning up the framebuffers after the old framebuffer is no longer
1315 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001316 *
1317 * The above scheme is implemented in the atomic helper libraries in
1318 * drm_atomic_helper_commit() using a bunch of helper functions. See
1319 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001320 */
1321
Daniel Vettera095caa2016-06-08 17:15:36 +02001322static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1323{
1324 struct drm_crtc_commit *commit, *stall_commit = NULL;
1325 bool completed = true;
1326 int i;
1327 long ret = 0;
1328
1329 spin_lock(&crtc->commit_lock);
1330 i = 0;
1331 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1332 if (i == 0) {
1333 completed = try_wait_for_completion(&commit->flip_done);
1334 /* Userspace is not allowed to get ahead of the previous
1335 * commit with nonblocking ones. */
1336 if (!completed && nonblock) {
1337 spin_unlock(&crtc->commit_lock);
1338 return -EBUSY;
1339 }
1340 } else if (i == 1) {
1341 stall_commit = commit;
1342 drm_crtc_commit_get(stall_commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001343 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001344 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001345
1346 i++;
1347 }
1348 spin_unlock(&crtc->commit_lock);
1349
1350 if (!stall_commit)
1351 return 0;
1352
1353 /* We don't want to let commits get ahead of cleanup work too much,
1354 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1355 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001356 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001357 10*HZ);
1358 if (ret == 0)
1359 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1360 crtc->base.id, crtc->name);
1361
1362 drm_crtc_commit_put(stall_commit);
1363
1364 return ret < 0 ? ret : 0;
1365}
1366
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001367static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001368{
1369 struct drm_crtc_commit *commit = container_of(completion,
1370 typeof(*commit),
1371 flip_done);
1372
1373 drm_crtc_commit_put(commit);
1374}
1375
Daniel Vettera095caa2016-06-08 17:15:36 +02001376/**
1377 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1378 * @state: new modeset state to be committed
1379 * @nonblock: whether nonblocking behavior is requested.
1380 *
1381 * This function prepares @state to be used by the atomic helper's support for
1382 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001383 * should always call this function from their
1384 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001385 *
1386 * To be able to use this support drivers need to use a few more helper
1387 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1388 * actually committing the hardware state, and for nonblocking commits this call
1389 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1390 * and it's stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001391 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001392 *
1393 * Completion of the hardware commit step must be signalled using
1394 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1395 * to read or change any permanent software or hardware modeset state. The only
1396 * exception is state protected by other means than &drm_modeset_lock locks.
1397 * Only the free standing @state with pointers to the old state structures can
1398 * be inspected, e.g. to clean up old buffers using
1399 * drm_atomic_helper_cleanup_planes().
1400 *
1401 * At the very end, before cleaning up @state drivers must call
1402 * drm_atomic_helper_commit_cleanup_done().
1403 *
1404 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1405 * complete and esay-to-use default implementation of the atomic_commit() hook.
1406 *
1407 * The tracking of asynchronously executed and still pending commits is done
1408 * using the core structure &drm_crtc_commit.
1409 *
1410 * By default there's no need to clean up resources allocated by this function
1411 * explicitly: drm_atomic_state_default_clear() will take care of that
1412 * automatically.
1413 *
1414 * Returns:
1415 *
1416 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1417 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1418 */
1419int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1420 bool nonblock)
1421{
1422 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001423 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001424 struct drm_crtc_commit *commit;
1425 int i, ret;
1426
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001427 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001428 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1429 if (!commit)
1430 return -ENOMEM;
1431
1432 init_completion(&commit->flip_done);
1433 init_completion(&commit->hw_done);
1434 init_completion(&commit->cleanup_done);
1435 INIT_LIST_HEAD(&commit->commit_entry);
1436 kref_init(&commit->ref);
1437 commit->crtc = crtc;
1438
1439 state->crtcs[i].commit = commit;
1440
1441 ret = stall_checks(crtc, nonblock);
1442 if (ret)
1443 return ret;
1444
1445 /* Drivers only send out events when at least either current or
1446 * new CRTC state is active. Complete right away if everything
1447 * stays off. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001448 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001449 complete_all(&commit->flip_done);
1450 continue;
1451 }
1452
1453 /* Legacy cursor updates are fully unsynced. */
1454 if (state->legacy_cursor_update) {
1455 complete_all(&commit->flip_done);
1456 continue;
1457 }
1458
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001459 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001460 commit->event = kzalloc(sizeof(*commit->event),
1461 GFP_KERNEL);
1462 if (!commit->event)
1463 return -ENOMEM;
1464
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001465 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02001466 }
1467
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001468 new_crtc_state->event->base.completion = &commit->flip_done;
1469 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01001470 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001471 }
1472
1473 return 0;
1474}
1475EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1476
1477
1478static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1479{
1480 struct drm_crtc_commit *commit;
1481 int i = 0;
1482
1483 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1484 /* skip the first entry, that's the current commit */
1485 if (i == 1)
1486 return commit;
1487 i++;
1488 }
1489
1490 return NULL;
1491}
1492
1493/**
1494 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001495 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001496 *
1497 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001498 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02001499 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001500 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02001501 *
1502 * This is part of the atomic helper support for nonblocking commits, see
1503 * drm_atomic_helper_setup_commit() for an overview.
1504 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001505void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001506{
1507 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001508 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001509 struct drm_crtc_commit *commit;
1510 int i;
1511 long ret;
1512
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001513 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001514 spin_lock(&crtc->commit_lock);
1515 commit = preceeding_commit(crtc);
1516 if (commit)
1517 drm_crtc_commit_get(commit);
1518 spin_unlock(&crtc->commit_lock);
1519
1520 if (!commit)
1521 continue;
1522
1523 ret = wait_for_completion_timeout(&commit->hw_done,
1524 10*HZ);
1525 if (ret == 0)
1526 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1527 crtc->base.id, crtc->name);
1528
1529 /* Currently no support for overwriting flips, hence
1530 * stall for previous one to execute completely. */
1531 ret = wait_for_completion_timeout(&commit->flip_done,
1532 10*HZ);
1533 if (ret == 0)
1534 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1535 crtc->base.id, crtc->name);
1536
1537 drm_crtc_commit_put(commit);
1538 }
1539}
1540EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1541
1542/**
1543 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001544 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001545 *
1546 * This function is used to signal completion of the hardware commit step. After
1547 * this step the driver is not allowed to read or change any permanent software
1548 * or hardware modeset state. The only exception is state protected by other
1549 * means than &drm_modeset_lock locks.
1550 *
1551 * Drivers should try to postpone any expensive or delayed cleanup work after
1552 * this function is called.
1553 *
1554 * This is part of the atomic helper support for nonblocking commits, see
1555 * drm_atomic_helper_setup_commit() for an overview.
1556 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001557void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001558{
1559 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001560 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001561 struct drm_crtc_commit *commit;
1562 int i;
1563
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001564 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001565 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001566 if (!commit)
1567 continue;
1568
1569 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001570 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02001571 spin_lock(&crtc->commit_lock);
1572 complete_all(&commit->hw_done);
1573 spin_unlock(&crtc->commit_lock);
1574 }
1575}
1576EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1577
1578/**
1579 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001580 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001581 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001582 * This signals completion of the atomic update @old_state, including any
1583 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01001584 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02001585 *
1586 * This is part of the atomic helper support for nonblocking commits, see
1587 * drm_atomic_helper_setup_commit() for an overview.
1588 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001589void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001590{
1591 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001592 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001593 struct drm_crtc_commit *commit;
1594 int i;
1595 long ret;
1596
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001597 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001598 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001599 if (WARN_ON(!commit))
1600 continue;
1601
1602 spin_lock(&crtc->commit_lock);
1603 complete_all(&commit->cleanup_done);
1604 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1605
1606 /* commit_list borrows our reference, need to remove before we
1607 * clean up our drm_atomic_state. But only after it actually
1608 * completed, otherwise subsequent commits won't stall properly. */
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001609 if (try_wait_for_completion(&commit->flip_done))
1610 goto del_commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001611
1612 spin_unlock(&crtc->commit_lock);
1613
1614 /* We must wait for the vblank event to signal our completion
1615 * before releasing our reference, since the vblank work does
1616 * not hold a reference of its own. */
1617 ret = wait_for_completion_timeout(&commit->flip_done,
1618 10*HZ);
1619 if (ret == 0)
1620 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1621 crtc->base.id, crtc->name);
1622
1623 spin_lock(&crtc->commit_lock);
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001624del_commit:
Daniel Vettera095caa2016-06-08 17:15:36 +02001625 list_del(&commit->commit_entry);
1626 spin_unlock(&crtc->commit_lock);
1627 }
1628}
1629EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1630
Daniel Vettere8c833a2014-07-27 18:30:19 +02001631/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001632 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001633 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001634 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001635 *
1636 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001637 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1638 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1639 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001640 *
1641 * Returns:
1642 * 0 on success, negative error code on failure.
1643 */
1644int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1645 struct drm_atomic_state *state)
1646{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001647 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001648 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001649 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001650
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001651 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001652 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001653
1654 funcs = plane->helper_private;
1655
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001656 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001657 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001658 if (ret)
1659 goto fail;
1660 }
1661 }
1662
1663 return 0;
1664
1665fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001666 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001667 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001668
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001669 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001670 continue;
1671
1672 funcs = plane->helper_private;
1673
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001674 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001675 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001676 }
1677
1678 return ret;
1679}
1680EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1681
Ville Syrjälä7135ac52016-09-19 16:33:42 +03001682static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001683{
1684 return state->crtc && state->crtc->state->active;
1685}
1686
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001687/**
1688 * drm_atomic_helper_commit_planes - commit plane state
1689 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001690 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08001691 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001692 *
1693 * This function commits the new plane state using the plane and atomic helper
1694 * functions for planes and crtcs. It assumes that the atomic state has already
1695 * been pushed into the relevant object state pointers, since this step can no
1696 * longer fail.
1697 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001698 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001699 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001700 *
1701 * Note that this function does all plane updates across all CRTCs in one step.
1702 * If the hardware can't support this approach look at
1703 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001704 *
1705 * Plane parameters can be updated by applications while the associated CRTC is
1706 * disabled. The DRM/KMS core will store the parameters in the plane state,
1707 * which will be available to the driver when the CRTC is turned on. As a result
1708 * most drivers don't need to be immediately notified of plane updates for a
1709 * disabled CRTC.
1710 *
Liu Ying2b58e982016-08-29 17:12:03 +08001711 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1712 * @flags in order not to receive plane update notifications related to a
1713 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001714 * driver code when the driver and/or hardware can't or just don't need to deal
1715 * with updates on disabled CRTCs, for example when supporting runtime PM.
1716 *
Liu Ying2b58e982016-08-29 17:12:03 +08001717 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1718 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001719 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1720 * call for a plane if the CRTC of the old plane state needs a modesetting
1721 * operation. Of course, the drivers need to disable the planes in their CRTC
1722 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08001723 *
1724 * The drm_atomic_helper_commit() default implementation doesn't set the
1725 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1726 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001727 */
1728void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001729 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001730 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001731{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001732 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001733 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001734 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001735 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001736 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08001737 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1738 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001739
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001740 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 +02001741 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001742
1743 funcs = crtc->helper_private;
1744
1745 if (!funcs || !funcs->atomic_begin)
1746 continue;
1747
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001748 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001749 continue;
1750
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001751 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001752 }
1753
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001754 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 +02001755 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001756 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001757
1758 funcs = plane->helper_private;
1759
Thierry Reding3cad4b62014-11-25 13:05:12 +01001760 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001761 continue;
1762
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001763 disabling = drm_atomic_plane_disabling(old_plane_state,
1764 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001765
1766 if (active_only) {
1767 /*
1768 * Skip planes related to inactive CRTCs. If the plane
1769 * is enabled use the state of the current CRTC. If the
1770 * plane is being disabled use the state of the old
1771 * CRTC to avoid skipping planes being disabled on an
1772 * active CRTC.
1773 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001774 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001775 continue;
1776 if (disabling && !plane_crtc_active(old_plane_state))
1777 continue;
1778 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001779
Thierry Reding407b8bd2014-11-20 12:05:50 +01001780 /*
1781 * Special-case disabling the plane if drivers support it.
1782 */
Liu Ying2b58e982016-08-29 17:12:03 +08001783 if (disabling && funcs->atomic_disable) {
1784 struct drm_crtc_state *crtc_state;
1785
1786 crtc_state = old_plane_state->crtc->state;
1787
1788 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1789 no_disable)
1790 continue;
1791
Thierry Reding407b8bd2014-11-20 12:05:50 +01001792 funcs->atomic_disable(plane, old_plane_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001793 } else if (new_plane_state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01001794 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08001795 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001796 }
1797
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001798 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 +02001799 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001800
1801 funcs = crtc->helper_private;
1802
1803 if (!funcs || !funcs->atomic_flush)
1804 continue;
1805
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001806 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001807 continue;
1808
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001809 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001810 }
1811}
1812EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1813
1814/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001815 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1816 * @old_crtc_state: atomic state object with the old crtc state
1817 *
1818 * This function commits the new plane state using the plane and atomic helper
1819 * functions for planes on the specific crtc. It assumes that the atomic state
1820 * has already been pushed into the relevant object state pointers, since this
1821 * step can no longer fail.
1822 *
1823 * This function is useful when plane updates should be done crtc-by-crtc
1824 * instead of one global step like drm_atomic_helper_commit_planes() does.
1825 *
1826 * This function can only be savely used when planes are not allowed to move
1827 * between different CRTCs because this function doesn't handle inter-CRTC
1828 * depencies. Callers need to ensure that either no such depencies exist,
1829 * resolve them through ordering of commit calls or through some other means.
1830 */
1831void
1832drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1833{
1834 const struct drm_crtc_helper_funcs *crtc_funcs;
1835 struct drm_crtc *crtc = old_crtc_state->crtc;
1836 struct drm_atomic_state *old_state = old_crtc_state->state;
1837 struct drm_plane *plane;
1838 unsigned plane_mask;
1839
1840 plane_mask = old_crtc_state->plane_mask;
1841 plane_mask |= crtc->state->plane_mask;
1842
1843 crtc_funcs = crtc->helper_private;
1844 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001845 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001846
1847 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1848 struct drm_plane_state *old_plane_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001849 drm_atomic_get_old_plane_state(old_state, plane);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001850 const struct drm_plane_helper_funcs *plane_funcs;
1851
1852 plane_funcs = plane->helper_private;
1853
1854 if (!old_plane_state || !plane_funcs)
1855 continue;
1856
1857 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1858
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001859 if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001860 plane_funcs->atomic_disable)
1861 plane_funcs->atomic_disable(plane, old_plane_state);
1862 else if (plane->state->crtc ||
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001863 drm_atomic_plane_disabling(old_plane_state, plane->state))
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001864 plane_funcs->atomic_update(plane, old_plane_state);
1865 }
1866
1867 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001868 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001869}
1870EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1871
1872/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001873 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08001874 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02001875 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1876 *
1877 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08001878 * used for instance in the CRTC helper atomic_disable callback to disable
1879 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001880 *
1881 * If the atomic-parameter is set the function calls the CRTC's
1882 * atomic_begin hook before and atomic_flush hook after disabling the
1883 * planes.
1884 *
1885 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001886 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001887 */
Liu Ying28500292016-08-26 15:30:39 +08001888void
1889drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1890 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001891{
Liu Ying28500292016-08-26 15:30:39 +08001892 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02001893 const struct drm_crtc_helper_funcs *crtc_funcs =
1894 crtc->helper_private;
1895 struct drm_plane *plane;
1896
1897 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1898 crtc_funcs->atomic_begin(crtc, NULL);
1899
Liu Ying28500292016-08-26 15:30:39 +08001900 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02001901 const struct drm_plane_helper_funcs *plane_funcs =
1902 plane->helper_private;
1903
Liu Ying28500292016-08-26 15:30:39 +08001904 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001905 continue;
1906
1907 WARN_ON(!plane_funcs->atomic_disable);
1908 if (plane_funcs->atomic_disable)
1909 plane_funcs->atomic_disable(plane, NULL);
1910 }
1911
1912 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1913 crtc_funcs->atomic_flush(crtc, NULL);
1914}
1915EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1916
1917/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001918 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1919 * @dev: DRM device
1920 * @old_state: atomic state object with old state structures
1921 *
1922 * This function cleans up plane state, specifically framebuffers, from the old
1923 * configuration. Hence the old configuration must be perserved in @old_state to
1924 * be able to call this function.
1925 *
1926 * This function must also be called on the new state when the atomic update
1927 * fails at any point after calling drm_atomic_helper_prepare_planes().
1928 */
1929void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1930 struct drm_atomic_state *old_state)
1931{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001932 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001933 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001934 int i;
1935
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001936 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 +02001937 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001938 struct drm_plane_state *plane_state;
1939
1940 /*
1941 * This might be called before swapping when commit is aborted,
1942 * in which case we have to cleanup the new state.
1943 */
1944 if (old_plane_state == plane->state)
1945 plane_state = new_plane_state;
1946 else
1947 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001948
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001949 funcs = plane->helper_private;
1950
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001951 if (funcs->cleanup_fb)
1952 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001953 }
1954}
1955EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1956
1957/**
1958 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001959 * @state: atomic state
Daniel Vetter5e84c262016-06-10 00:06:32 +02001960 * @stall: stall for proceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001961 *
1962 * This function stores the atomic state into the current state pointers in all
1963 * driver objects. It should be called after all failing steps have been done
1964 * and succeeded, but before the actual hardware state is committed.
1965 *
1966 * For cleanup and error recovery the current state for all changed objects will
1967 * be swaped into @state.
1968 *
1969 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1970 *
1971 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1972 *
1973 * 2. Do any other steps that might fail.
1974 *
1975 * 3. Put the staged state into the current state pointers with this function.
1976 *
1977 * 4. Actually commit the hardware state.
1978 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001979 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001980 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02001981 *
1982 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001983 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
1984 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02001985 * don't pass the right state structures to the callbacks.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001986 */
Daniel Vetter5e84c262016-06-10 00:06:32 +02001987void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1988 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001989{
1990 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02001991 long ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001992 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001993 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001994 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001995 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001996 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001997 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001998 struct drm_crtc_commit *commit;
1999
2000 if (stall) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002001 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002002 spin_lock(&crtc->commit_lock);
2003 commit = list_first_entry_or_null(&crtc->commit_list,
2004 struct drm_crtc_commit, commit_entry);
2005 if (commit)
2006 drm_crtc_commit_get(commit);
2007 spin_unlock(&crtc->commit_lock);
2008
2009 if (!commit)
2010 continue;
2011
2012 ret = wait_for_completion_timeout(&commit->hw_done,
2013 10*HZ);
2014 if (ret == 0)
2015 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2016 crtc->base.id, crtc->name);
2017 drm_crtc_commit_put(commit);
2018 }
2019 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002020
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002021 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002022 WARN_ON(connector->state != old_conn_state);
2023
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002024 old_conn_state->state = state;
2025 new_conn_state->state = NULL;
2026
2027 state->connectors[i].state = old_conn_state;
2028 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002029 }
2030
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002031 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002032 WARN_ON(crtc->state != old_crtc_state);
2033
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002034 old_crtc_state->state = state;
2035 new_crtc_state->state = NULL;
2036
2037 state->crtcs[i].state = old_crtc_state;
2038 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002039
2040 if (state->crtcs[i].commit) {
2041 spin_lock(&crtc->commit_lock);
2042 list_add(&state->crtcs[i].commit->commit_entry,
2043 &crtc->commit_list);
2044 spin_unlock(&crtc->commit_lock);
2045
2046 state->crtcs[i].commit->event = NULL;
2047 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002048 }
2049
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002050 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002051 WARN_ON(plane->state != old_plane_state);
2052
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002053 old_plane_state->state = state;
2054 new_plane_state->state = NULL;
2055
2056 state->planes[i].state = old_plane_state;
2057 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002058 }
2059}
2060EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002061
2062/**
2063 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2064 * @plane: plane object to update
2065 * @crtc: owning CRTC of owning plane
2066 * @fb: framebuffer to flip onto plane
2067 * @crtc_x: x offset of primary plane on crtc
2068 * @crtc_y: y offset of primary plane on crtc
2069 * @crtc_w: width of primary plane rectangle on crtc
2070 * @crtc_h: height of primary plane rectangle on crtc
2071 * @src_x: x offset of @fb for panning
2072 * @src_y: y offset of @fb for panning
2073 * @src_w: width of source rectangle in @fb
2074 * @src_h: height of source rectangle in @fb
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002075 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002076 *
2077 * Provides a default plane update handler using the atomic driver interface.
2078 *
2079 * RETURNS:
2080 * Zero on success, error code on failure
2081 */
2082int drm_atomic_helper_update_plane(struct drm_plane *plane,
2083 struct drm_crtc *crtc,
2084 struct drm_framebuffer *fb,
2085 int crtc_x, int crtc_y,
2086 unsigned int crtc_w, unsigned int crtc_h,
2087 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002088 uint32_t src_w, uint32_t src_h,
2089 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002090{
2091 struct drm_atomic_state *state;
2092 struct drm_plane_state *plane_state;
2093 int ret = 0;
2094
2095 state = drm_atomic_state_alloc(plane->dev);
2096 if (!state)
2097 return -ENOMEM;
2098
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002099 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002100 plane_state = drm_atomic_get_plane_state(state, plane);
2101 if (IS_ERR(plane_state)) {
2102 ret = PTR_ERR(plane_state);
2103 goto fail;
2104 }
2105
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002106 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002107 if (ret != 0)
2108 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002109 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002110 plane_state->crtc_x = crtc_x;
2111 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002112 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002113 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002114 plane_state->src_x = src_x;
2115 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002116 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002117 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002118
Daniel Vetter3671c582015-05-04 15:40:52 +02002119 if (plane == crtc->cursor)
2120 state->legacy_cursor_update = true;
2121
Daniel Vetter042652e2014-07-27 13:46:52 +02002122 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002123fail:
Chris Wilson08536952016-10-14 13:18:18 +01002124 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002125 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002126}
2127EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2128
2129/**
2130 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2131 * @plane: plane to disable
Daniel Vetter19315292017-03-22 22:50:43 +01002132 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002133 *
2134 * Provides a default plane disable handler using the atomic driver interface.
2135 *
2136 * RETURNS:
2137 * Zero on success, error code on failure
2138 */
Daniel Vetter19315292017-03-22 22:50:43 +01002139int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2140 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002141{
2142 struct drm_atomic_state *state;
2143 struct drm_plane_state *plane_state;
2144 int ret = 0;
2145
2146 state = drm_atomic_state_alloc(plane->dev);
2147 if (!state)
2148 return -ENOMEM;
2149
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002150 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002151 plane_state = drm_atomic_get_plane_state(state, plane);
2152 if (IS_ERR(plane_state)) {
2153 ret = PTR_ERR(plane_state);
2154 goto fail;
2155 }
2156
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002157 if (plane_state->crtc && (plane == plane->crtc->cursor))
2158 plane_state->state->legacy_cursor_update = true;
2159
Rob Clarkbbb1e522015-08-25 15:35:58 -04002160 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002161 if (ret != 0)
2162 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002163
Daniel Vetter042652e2014-07-27 13:46:52 +02002164 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002165fail:
Chris Wilson08536952016-10-14 13:18:18 +01002166 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002167 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002168}
2169EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2170
Rob Clarkbbb1e522015-08-25 15:35:58 -04002171/* just used from fb-helper and atomic-helper: */
2172int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2173 struct drm_plane_state *plane_state)
2174{
2175 int ret;
2176
2177 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2178 if (ret != 0)
2179 return ret;
2180
2181 drm_atomic_set_fb_for_plane(plane_state, NULL);
2182 plane_state->crtc_x = 0;
2183 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002184 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002185 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002186 plane_state->src_x = 0;
2187 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002188 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002189 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002190
Rob Clarkbbb1e522015-08-25 15:35:58 -04002191 return 0;
2192}
2193
Daniel Vetter042652e2014-07-27 13:46:52 +02002194static int update_output_state(struct drm_atomic_state *state,
2195 struct drm_mode_set *set)
2196{
2197 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002198 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002199 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002200 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002201 struct drm_connector_state *new_conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002202 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002203
2204 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2205 state->acquire_ctx);
2206 if (ret)
2207 return ret;
2208
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002209 /* First disable all connectors on the target crtc. */
2210 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2211 if (ret)
2212 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002213
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002214 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2215 if (new_conn_state->crtc == set->crtc) {
2216 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Daniel Vetter042652e2014-07-27 13:46:52 +02002217 NULL);
2218 if (ret)
2219 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002220
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002221 /* Make sure legacy setCrtc always re-trains */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002222 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
Daniel Vetter042652e2014-07-27 13:46:52 +02002223 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002224 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002225
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002226 /* Then set all connectors from set->connectors on the target crtc */
2227 for (i = 0; i < set->num_connectors; i++) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002228 new_conn_state = drm_atomic_get_connector_state(state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002229 set->connectors[i]);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002230 if (IS_ERR(new_conn_state))
2231 return PTR_ERR(new_conn_state);
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002232
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002233 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002234 set->crtc);
2235 if (ret)
2236 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002237 }
2238
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002239 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002240 /* Don't update ->enable for the CRTC in the set_config request,
2241 * since a mismatch would indicate a bug in the upper layers.
2242 * The actual modeset code later on will catch any
2243 * inconsistencies here. */
2244 if (crtc == set->crtc)
2245 continue;
2246
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002247 if (!new_crtc_state->connector_mask) {
2248 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002249 NULL);
2250 if (ret < 0)
2251 return ret;
2252
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002253 new_crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002254 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002255 }
2256
2257 return 0;
2258}
2259
2260/**
2261 * drm_atomic_helper_set_config - set a new config from userspace
2262 * @set: mode set configuration
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002263 * @ctx: lock acquisition context
Daniel Vetter042652e2014-07-27 13:46:52 +02002264 *
2265 * Provides a default crtc set_config handler using the atomic driver interface.
2266 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002267 * NOTE: For backwards compatibility with old userspace this automatically
2268 * resets the "link-status" property to GOOD, to force any link
2269 * re-training. The SETCRTC ioctl does not define whether an update does
2270 * need a full modeset or just a plane update, hence we're allowed to do
2271 * that. See also drm_mode_connector_set_link_status_property().
2272 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002273 * Returns:
2274 * Returns 0 on success, negative errno numbers on failure.
2275 */
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002276int drm_atomic_helper_set_config(struct drm_mode_set *set,
2277 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002278{
2279 struct drm_atomic_state *state;
2280 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002281 int ret = 0;
2282
2283 state = drm_atomic_state_alloc(crtc->dev);
2284 if (!state)
2285 return -ENOMEM;
2286
Daniel Vetter38b64412017-03-22 22:50:58 +01002287 state->acquire_ctx = ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002288 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002289 if (ret != 0)
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002290 goto fail;
Daniel Stone819364d2015-05-26 14:36:48 +01002291
Maarten Lankhorst44596b82017-04-06 13:19:00 +02002292 ret = handle_conflicting_encoders(state, true);
2293 if (ret)
2294 return ret;
2295
Daniel Vetter042652e2014-07-27 13:46:52 +02002296 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002297
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002298fail:
Chris Wilson08536952016-10-14 13:18:18 +01002299 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002300 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002301}
2302EXPORT_SYMBOL(drm_atomic_helper_set_config);
2303
Rob Clarkbbb1e522015-08-25 15:35:58 -04002304/* just used from fb-helper and atomic-helper: */
2305int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2306 struct drm_atomic_state *state)
2307{
2308 struct drm_crtc_state *crtc_state;
2309 struct drm_plane_state *primary_state;
2310 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002311 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002312 int ret;
2313
2314 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2315 if (IS_ERR(crtc_state))
2316 return PTR_ERR(crtc_state);
2317
2318 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2319 if (IS_ERR(primary_state))
2320 return PTR_ERR(primary_state);
2321
2322 if (!set->mode) {
2323 WARN_ON(set->fb);
2324 WARN_ON(set->num_connectors);
2325
2326 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2327 if (ret != 0)
2328 return ret;
2329
2330 crtc_state->active = false;
2331
2332 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2333 if (ret != 0)
2334 return ret;
2335
2336 drm_atomic_set_fb_for_plane(primary_state, NULL);
2337
2338 goto commit;
2339 }
2340
2341 WARN_ON(!set->fb);
2342 WARN_ON(!set->num_connectors);
2343
2344 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2345 if (ret != 0)
2346 return ret;
2347
2348 crtc_state->active = true;
2349
2350 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2351 if (ret != 0)
2352 return ret;
2353
Daniel Vetter196cd5d2017-01-25 07:26:56 +01002354 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Ville Syrjälä83926112015-11-16 17:02:34 +02002355
Rob Clarkbbb1e522015-08-25 15:35:58 -04002356 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2357 primary_state->crtc_x = 0;
2358 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02002359 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002360 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002361 primary_state->src_x = set->x << 16;
2362 primary_state->src_y = set->y << 16;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002363 if (drm_rotation_90_or_270(primary_state->rotation)) {
Ville Syrjälä83926112015-11-16 17:02:34 +02002364 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002365 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002366 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02002367 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002368 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002369 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04002370
2371commit:
2372 ret = update_output_state(state, set);
2373 if (ret)
2374 return ret;
2375
2376 return 0;
2377}
2378
Daniel Vetter042652e2014-07-27 13:46:52 +02002379/**
Thierry Reding14942762015-12-02 17:50:04 +01002380 * drm_atomic_helper_disable_all - disable all currently active outputs
2381 * @dev: DRM device
2382 * @ctx: lock acquisition context
2383 *
2384 * Loops through all connectors, finding those that aren't turned off and then
2385 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2386 * that they are connected to.
2387 *
2388 * This is used for example in suspend/resume to disable all currently active
Daniel Vetter18dddad2017-03-21 17:41:49 +01002389 * functions when suspending. If you just want to shut down everything at e.g.
2390 * driver unload, look at drm_atomic_helper_shutdown().
Thierry Reding14942762015-12-02 17:50:04 +01002391 *
2392 * Note that if callers haven't already acquired all modeset locks this might
2393 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2394 *
2395 * Returns:
2396 * 0 on success or a negative error code on failure.
2397 *
2398 * See also:
Daniel Vetter18dddad2017-03-21 17:41:49 +01002399 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
2400 * drm_atomic_helper_shutdown().
Thierry Reding14942762015-12-02 17:50:04 +01002401 */
2402int drm_atomic_helper_disable_all(struct drm_device *dev,
2403 struct drm_modeset_acquire_ctx *ctx)
2404{
2405 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002406 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01002407 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002408 struct drm_plane_state *plane_state;
2409 struct drm_plane *plane;
2410 struct drm_crtc_state *crtc_state;
2411 struct drm_crtc *crtc;
2412 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01002413
2414 state = drm_atomic_state_alloc(dev);
2415 if (!state)
2416 return -ENOMEM;
2417
2418 state->acquire_ctx = ctx;
2419
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002420 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01002421 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2422 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002423 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01002424 goto free;
2425 }
2426
2427 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002428
2429 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2430 if (ret < 0)
2431 goto free;
2432
2433 ret = drm_atomic_add_affected_planes(state, crtc);
2434 if (ret < 0)
2435 goto free;
2436
2437 ret = drm_atomic_add_affected_connectors(state, crtc);
2438 if (ret < 0)
2439 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01002440 }
2441
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002442 for_each_connector_in_state(state, conn, conn_state, i) {
2443 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2444 if (ret < 0)
2445 goto free;
2446 }
2447
2448 for_each_plane_in_state(state, plane, plane_state, i) {
2449 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2450 if (ret < 0)
2451 goto free;
2452
2453 drm_atomic_set_fb_for_plane(plane_state, NULL);
2454 }
2455
2456 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01002457free:
Chris Wilson08536952016-10-14 13:18:18 +01002458 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002459 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01002460}
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002461
Thierry Reding14942762015-12-02 17:50:04 +01002462EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2463
2464/**
Daniel Vetter18dddad2017-03-21 17:41:49 +01002465 * drm_atomic_helper_shutdown - shutdown all CRTC
2466 * @dev: DRM device
2467 *
2468 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
2469 * suspend should instead be handled with drm_atomic_helper_suspend(), since
2470 * that also takes a snapshot of the modeset state to be restored on resume.
2471 *
2472 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
2473 * and it is the atomic version of drm_crtc_force_disable_all().
2474 */
2475void drm_atomic_helper_shutdown(struct drm_device *dev)
2476{
2477 struct drm_modeset_acquire_ctx ctx;
2478 int ret;
2479
2480 drm_modeset_acquire_init(&ctx, 0);
2481 while (1) {
2482 ret = drm_modeset_lock_all_ctx(dev, &ctx);
2483 if (!ret)
2484 ret = drm_atomic_helper_disable_all(dev, &ctx);
2485
2486 if (ret != -EDEADLK)
2487 break;
2488
2489 drm_modeset_backoff(&ctx);
2490 }
2491
2492 if (ret)
2493 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
2494
2495 drm_modeset_drop_locks(&ctx);
2496 drm_modeset_acquire_fini(&ctx);
2497}
2498EXPORT_SYMBOL(drm_atomic_helper_shutdown);
2499
2500/**
Thierry Reding14942762015-12-02 17:50:04 +01002501 * drm_atomic_helper_suspend - subsystem-level suspend helper
2502 * @dev: DRM device
2503 *
2504 * Duplicates the current atomic state, disables all active outputs and then
2505 * returns a pointer to the original atomic state to the caller. Drivers can
2506 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2507 * restore the output configuration that was active at the time the system
2508 * entered suspend.
2509 *
2510 * Note that it is potentially unsafe to use this. The atomic state object
2511 * returned by this function is assumed to be persistent. Drivers must ensure
2512 * that this holds true. Before calling this function, drivers must make sure
2513 * to suspend fbdev emulation so that nothing can be using the device.
2514 *
2515 * Returns:
2516 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2517 * encoded error code on failure. Drivers should store the returned atomic
2518 * state object and pass it to the drm_atomic_helper_resume() helper upon
2519 * resume.
2520 *
2521 * See also:
2522 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002523 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01002524 */
2525struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2526{
2527 struct drm_modeset_acquire_ctx ctx;
2528 struct drm_atomic_state *state;
2529 int err;
2530
2531 drm_modeset_acquire_init(&ctx, 0);
2532
2533retry:
2534 err = drm_modeset_lock_all_ctx(dev, &ctx);
2535 if (err < 0) {
2536 state = ERR_PTR(err);
2537 goto unlock;
2538 }
2539
2540 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2541 if (IS_ERR(state))
2542 goto unlock;
2543
2544 err = drm_atomic_helper_disable_all(dev, &ctx);
2545 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01002546 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01002547 state = ERR_PTR(err);
2548 goto unlock;
2549 }
2550
2551unlock:
2552 if (PTR_ERR(state) == -EDEADLK) {
2553 drm_modeset_backoff(&ctx);
2554 goto retry;
2555 }
2556
2557 drm_modeset_drop_locks(&ctx);
2558 drm_modeset_acquire_fini(&ctx);
2559 return state;
2560}
2561EXPORT_SYMBOL(drm_atomic_helper_suspend);
2562
2563/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002564 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2565 * @state: duplicated atomic state to commit
2566 * @ctx: pointer to acquire_ctx to use for commit.
2567 *
2568 * The state returned by drm_atomic_helper_duplicate_state() and
2569 * drm_atomic_helper_suspend() is partially invalid, and needs to
2570 * be fixed up before commit.
2571 *
2572 * Returns:
2573 * 0 on success or a negative error code on failure.
2574 *
2575 * See also:
2576 * drm_atomic_helper_suspend()
2577 */
2578int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2579 struct drm_modeset_acquire_ctx *ctx)
2580{
2581 int i;
2582 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002583 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002584 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002585 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002586 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002587 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002588
2589 state->acquire_ctx = ctx;
2590
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002591 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002592 state->planes[i].old_state = plane->state;
2593
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002594 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002595 state->crtcs[i].old_state = crtc->state;
2596
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002597 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002598 state->connectors[i].old_state = connector->state;
2599
2600 return drm_atomic_commit(state);
2601}
2602EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2603
2604/**
Thierry Reding14942762015-12-02 17:50:04 +01002605 * drm_atomic_helper_resume - subsystem-level resume helper
2606 * @dev: DRM device
2607 * @state: atomic state to resume to
2608 *
2609 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2610 * grabs all modeset locks and commits the atomic state object. This can be
2611 * used in conjunction with the drm_atomic_helper_suspend() helper to
2612 * implement suspend/resume for drivers that support atomic mode-setting.
2613 *
2614 * Returns:
2615 * 0 on success or a negative error code on failure.
2616 *
2617 * See also:
2618 * drm_atomic_helper_suspend()
2619 */
2620int drm_atomic_helper_resume(struct drm_device *dev,
2621 struct drm_atomic_state *state)
2622{
Daniel Vettera5b84442017-04-03 10:32:53 +02002623 struct drm_modeset_acquire_ctx ctx;
Thierry Reding14942762015-12-02 17:50:04 +01002624 int err;
2625
2626 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002627
Daniel Vettera5b84442017-04-03 10:32:53 +02002628 drm_modeset_acquire_init(&ctx, 0);
2629 while (1) {
2630 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
2631 if (err != -EDEADLK)
2632 break;
2633
2634 drm_modeset_backoff(&ctx);
2635 }
2636
2637 drm_modeset_drop_locks(&ctx);
2638 drm_modeset_acquire_fini(&ctx);
Thierry Reding14942762015-12-02 17:50:04 +01002639
2640 return err;
2641}
2642EXPORT_SYMBOL(drm_atomic_helper_resume);
2643
2644/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002645 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002646 * @crtc: DRM crtc
2647 * @property: DRM property
2648 * @val: value of property
2649 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002650 * Provides a default crtc set_property handler using the atomic driver
2651 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002652 *
2653 * RETURNS:
2654 * Zero on success, error code on failure
2655 */
2656int
2657drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2658 struct drm_property *property,
2659 uint64_t val)
2660{
2661 struct drm_atomic_state *state;
2662 struct drm_crtc_state *crtc_state;
2663 int ret = 0;
2664
2665 state = drm_atomic_state_alloc(crtc->dev);
2666 if (!state)
2667 return -ENOMEM;
2668
2669 /* ->set_property is always called with all locks held. */
2670 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2671retry:
2672 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2673 if (IS_ERR(crtc_state)) {
2674 ret = PTR_ERR(crtc_state);
2675 goto fail;
2676 }
2677
Rob Clark40ecc692014-12-18 16:01:46 -05002678 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2679 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002680 if (ret)
2681 goto fail;
2682
2683 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002684fail:
2685 if (ret == -EDEADLK)
2686 goto backoff;
2687
Chris Wilson08536952016-10-14 13:18:18 +01002688 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002689 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002690
Daniel Vetter042652e2014-07-27 13:46:52 +02002691backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002692 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002693 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002694
2695 goto retry;
2696}
2697EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2698
2699/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002700 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002701 * @plane: DRM plane
2702 * @property: DRM property
2703 * @val: value of property
2704 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002705 * Provides a default plane set_property handler using the atomic driver
2706 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002707 *
2708 * RETURNS:
2709 * Zero on success, error code on failure
2710 */
2711int
2712drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2713 struct drm_property *property,
2714 uint64_t val)
2715{
2716 struct drm_atomic_state *state;
2717 struct drm_plane_state *plane_state;
2718 int ret = 0;
2719
2720 state = drm_atomic_state_alloc(plane->dev);
2721 if (!state)
2722 return -ENOMEM;
2723
2724 /* ->set_property is always called with all locks held. */
2725 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2726retry:
2727 plane_state = drm_atomic_get_plane_state(state, plane);
2728 if (IS_ERR(plane_state)) {
2729 ret = PTR_ERR(plane_state);
2730 goto fail;
2731 }
2732
Rob Clark40ecc692014-12-18 16:01:46 -05002733 ret = drm_atomic_plane_set_property(plane, plane_state,
2734 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002735 if (ret)
2736 goto fail;
2737
2738 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002739fail:
2740 if (ret == -EDEADLK)
2741 goto backoff;
2742
Chris Wilson08536952016-10-14 13:18:18 +01002743 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002744 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002745
Daniel Vetter042652e2014-07-27 13:46:52 +02002746backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002747 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002748 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002749
2750 goto retry;
2751}
2752EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2753
2754/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002755 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002756 * @connector: DRM connector
2757 * @property: DRM property
2758 * @val: value of property
2759 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002760 * Provides a default connector set_property handler using the atomic driver
2761 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002762 *
2763 * RETURNS:
2764 * Zero on success, error code on failure
2765 */
2766int
2767drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2768 struct drm_property *property,
2769 uint64_t val)
2770{
2771 struct drm_atomic_state *state;
2772 struct drm_connector_state *connector_state;
2773 int ret = 0;
2774
2775 state = drm_atomic_state_alloc(connector->dev);
2776 if (!state)
2777 return -ENOMEM;
2778
2779 /* ->set_property is always called with all locks held. */
2780 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2781retry:
2782 connector_state = drm_atomic_get_connector_state(state, connector);
2783 if (IS_ERR(connector_state)) {
2784 ret = PTR_ERR(connector_state);
2785 goto fail;
2786 }
2787
Rob Clark40ecc692014-12-18 16:01:46 -05002788 ret = drm_atomic_connector_set_property(connector, connector_state,
2789 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002790 if (ret)
2791 goto fail;
2792
2793 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002794fail:
2795 if (ret == -EDEADLK)
2796 goto backoff;
2797
Chris Wilson08536952016-10-14 13:18:18 +01002798 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002799 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002800
Daniel Vetter042652e2014-07-27 13:46:52 +02002801backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002802 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002803 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002804
2805 goto retry;
2806}
2807EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002808
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002809static int page_flip_common(
2810 struct drm_atomic_state *state,
2811 struct drm_crtc *crtc,
2812 struct drm_framebuffer *fb,
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002813 struct drm_pending_vblank_event *event,
2814 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002815{
2816 struct drm_plane *plane = crtc->primary;
2817 struct drm_plane_state *plane_state;
2818 struct drm_crtc_state *crtc_state;
2819 int ret = 0;
2820
2821 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2822 if (IS_ERR(crtc_state))
2823 return PTR_ERR(crtc_state);
2824
2825 crtc_state->event = event;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002826 crtc_state->pageflip_flags = flags;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002827
2828 plane_state = drm_atomic_get_plane_state(state, plane);
2829 if (IS_ERR(plane_state))
2830 return PTR_ERR(plane_state);
2831
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002832 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2833 if (ret != 0)
2834 return ret;
2835 drm_atomic_set_fb_for_plane(plane_state, fb);
2836
2837 /* Make sure we don't accidentally do a full modeset. */
2838 state->allow_modeset = false;
2839 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00002840 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
2841 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002842 return -EINVAL;
2843 }
2844
2845 return ret;
2846}
2847
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002848/**
2849 * drm_atomic_helper_page_flip - execute a legacy page flip
2850 * @crtc: DRM crtc
2851 * @fb: DRM framebuffer
2852 * @event: optional DRM event to signal upon completion
2853 * @flags: flip flags for non-vblank sync'ed updates
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002854 * @ctx: lock acquisition context
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002855 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002856 * Provides a default &drm_crtc_funcs.page_flip implementation
2857 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002858 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002859 * Returns:
2860 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002861 *
2862 * See also:
2863 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002864 */
2865int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2866 struct drm_framebuffer *fb,
2867 struct drm_pending_vblank_event *event,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002868 uint32_t flags,
2869 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002870{
2871 struct drm_plane *plane = crtc->primary;
2872 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002873 int ret = 0;
2874
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002875 state = drm_atomic_state_alloc(plane->dev);
2876 if (!state)
2877 return -ENOMEM;
2878
Daniel Vetter043e7fb2017-03-22 22:50:51 +01002879 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002880
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002881 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002882 if (ret != 0)
2883 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01002884
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002885 ret = drm_atomic_nonblocking_commit(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002886fail:
Chris Wilson08536952016-10-14 13:18:18 +01002887 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002888 return ret;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002889}
2890EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002891
2892/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002893 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2894 * @crtc: DRM crtc
2895 * @fb: DRM framebuffer
2896 * @event: optional DRM event to signal upon completion
2897 * @flags: flip flags for non-vblank sync'ed updates
2898 * @target: specifying the target vblank period when the flip to take effect
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002899 * @ctx: lock acquisition context
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002900 *
2901 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2902 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2903 * target vblank period to flip.
2904 *
2905 * Returns:
2906 * Returns 0 on success, negative errno numbers on failure.
2907 */
2908int drm_atomic_helper_page_flip_target(
2909 struct drm_crtc *crtc,
2910 struct drm_framebuffer *fb,
2911 struct drm_pending_vblank_event *event,
2912 uint32_t flags,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002913 uint32_t target,
2914 struct drm_modeset_acquire_ctx *ctx)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002915{
2916 struct drm_plane *plane = crtc->primary;
2917 struct drm_atomic_state *state;
2918 struct drm_crtc_state *crtc_state;
2919 int ret = 0;
2920
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002921 state = drm_atomic_state_alloc(plane->dev);
2922 if (!state)
2923 return -ENOMEM;
2924
Daniel Vetter043e7fb2017-03-22 22:50:51 +01002925 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002926
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002927 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002928 if (ret != 0)
2929 goto fail;
2930
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01002931 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002932 if (WARN_ON(!crtc_state)) {
2933 ret = -EINVAL;
2934 goto fail;
2935 }
2936 crtc_state->target_vblank = target;
2937
2938 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002939fail:
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002940 drm_atomic_state_put(state);
2941 return ret;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002942}
2943EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2944
2945/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002946 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2947 * @connector: affected connector
2948 * @mode: DPMS mode
2949 *
2950 * This is the main helper function provided by the atomic helper framework for
2951 * implementing the legacy DPMS connector interface. It computes the new desired
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002952 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
2953 * enabled) and updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002954 *
2955 * Returns:
2956 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002957 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002958int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2959 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002960{
2961 struct drm_mode_config *config = &connector->dev->mode_config;
2962 struct drm_atomic_state *state;
2963 struct drm_crtc_state *crtc_state;
2964 struct drm_crtc *crtc;
2965 struct drm_connector *tmp_connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +01002966 struct drm_connector_list_iter conn_iter;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002967 int ret;
2968 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002969 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002970
2971 if (mode != DRM_MODE_DPMS_ON)
2972 mode = DRM_MODE_DPMS_OFF;
2973
2974 connector->dpms = mode;
2975 crtc = connector->state->crtc;
2976
2977 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002978 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002979
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002980 state = drm_atomic_state_alloc(connector->dev);
2981 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002982 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002983
Daniel Vetterb260ac32017-04-03 10:32:52 +02002984 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002985retry:
2986 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002987 if (IS_ERR(crtc_state)) {
2988 ret = PTR_ERR(crtc_state);
2989 goto fail;
2990 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002991
2992 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2993
Thierry Redingb982dab2017-02-28 15:46:43 +01002994 drm_connector_list_iter_begin(connector->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01002995 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
John Hunter0388df02015-03-17 15:30:28 +08002996 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002997 continue;
2998
John Hunter0388df02015-03-17 15:30:28 +08002999 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003000 active = true;
3001 break;
3002 }
3003 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003004 drm_connector_list_iter_end(&conn_iter);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003005 crtc_state->active = active;
3006
3007 ret = drm_atomic_commit(state);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003008fail:
3009 if (ret == -EDEADLK)
3010 goto backoff;
Marta Lofstedt8f5040e2016-12-05 14:04:08 +02003011 if (ret != 0)
3012 connector->dpms = old_mode;
Chris Wilson08536952016-10-14 13:18:18 +01003013 drm_atomic_state_put(state);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003014 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003015
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003016backoff:
3017 drm_atomic_state_clear(state);
3018 drm_atomic_legacy_backoff(state);
3019
3020 goto retry;
3021}
3022EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3023
3024/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003025 * drm_atomic_helper_best_encoder - Helper for
3026 * &drm_connector_helper_funcs.best_encoder callback
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003027 * @connector: Connector control structure
3028 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003029 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003030 * connectors that support exactly 1 encoder, statically determined at driver
3031 * init time.
3032 */
3033struct drm_encoder *
3034drm_atomic_helper_best_encoder(struct drm_connector *connector)
3035{
3036 WARN_ON(connector->encoder_ids[1]);
3037 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3038}
3039EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3040
3041/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003042 * DOC: atomic state reset and initialization
3043 *
3044 * Both the drm core and the atomic helpers assume that there is always the full
3045 * and correct atomic software state for all connectors, CRTCs and planes
3046 * available. Which is a bit a problem on driver load and also after system
3047 * suspend. One way to solve this is to have a hardware state read-out
3048 * infrastructure which reconstructs the full software state (e.g. the i915
3049 * driver).
3050 *
3051 * The simpler solution is to just reset the software state to everything off,
3052 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3053 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01003054 *
3055 * On the upside the precise state tracking of atomic simplifies system suspend
3056 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3057 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3058 * For other drivers the building blocks are split out, see the documentation
3059 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003060 */
3061
3062/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003063 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
Daniel Vetterd4617012014-11-03 15:56:43 +01003064 * @crtc: drm CRTC
3065 *
3066 * Resets the atomic state for @crtc by freeing the state pointer (which might
3067 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3068 */
3069void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3070{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003071 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003072 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003073
Daniel Vetterd4617012014-11-03 15:56:43 +01003074 kfree(crtc->state);
3075 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003076
3077 if (crtc->state)
3078 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01003079}
3080EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3081
3082/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003083 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3084 * @crtc: CRTC object
3085 * @state: atomic CRTC state
3086 *
3087 * Copies atomic state from a CRTC's current state and resets inferred values.
3088 * This is useful for drivers that subclass the CRTC state.
3089 */
3090void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3091 struct drm_crtc_state *state)
3092{
3093 memcpy(state, crtc->state, sizeof(*state));
3094
Daniel Stone99cf4a22015-05-25 19:11:51 +01003095 if (state->mode_blob)
Thierry Reding6472e502017-02-28 15:46:42 +01003096 drm_property_blob_get(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003097 if (state->degamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003098 drm_property_blob_get(state->degamma_lut);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003099 if (state->ctm)
Thierry Reding6472e502017-02-28 15:46:42 +01003100 drm_property_blob_get(state->ctm);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003101 if (state->gamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003102 drm_property_blob_get(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003103 state->mode_changed = false;
3104 state->active_changed = false;
3105 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02003106 state->connectors_changed = false;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003107 state->color_mgmt_changed = false;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02003108 state->zpos_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01003109 state->event = NULL;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003110 state->pageflip_flags = 0;
Thierry Redingf5e78402015-01-28 14:54:32 +01003111}
3112EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3113
3114/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003115 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3116 * @crtc: drm CRTC
3117 *
3118 * Default CRTC state duplicate hook for drivers which don't have their own
3119 * subclassed CRTC state structure.
3120 */
3121struct drm_crtc_state *
3122drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3123{
3124 struct drm_crtc_state *state;
3125
3126 if (WARN_ON(!crtc->state))
3127 return NULL;
3128
Thierry Redingf5e78402015-01-28 14:54:32 +01003129 state = kmalloc(sizeof(*state), GFP_KERNEL);
3130 if (state)
3131 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003132
3133 return state;
3134}
3135EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3136
3137/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003138 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
Thierry Redingf5e78402015-01-28 14:54:32 +01003139 * @state: CRTC state object to release
3140 *
3141 * Releases all resources stored in the CRTC state without actually freeing
3142 * the memory of the CRTC state. This is useful for drivers that subclass the
3143 * CRTC state.
3144 */
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003145void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003146{
Thierry Reding6472e502017-02-28 15:46:42 +01003147 drm_property_blob_put(state->mode_blob);
3148 drm_property_blob_put(state->degamma_lut);
3149 drm_property_blob_put(state->ctm);
3150 drm_property_blob_put(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003151}
3152EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3153
3154/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003155 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3156 * @crtc: drm CRTC
3157 * @state: CRTC state object to release
3158 *
3159 * Default CRTC state destroy hook for drivers which don't have their own
3160 * subclassed CRTC state structure.
3161 */
3162void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3163 struct drm_crtc_state *state)
3164{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003165 __drm_atomic_helper_crtc_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003166 kfree(state);
3167}
3168EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3169
3170/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003171 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
Daniel Vetterd4617012014-11-03 15:56:43 +01003172 * @plane: drm plane
3173 *
3174 * Resets the atomic state for @plane by freeing the state pointer (which might
3175 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3176 */
3177void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3178{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003179 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +02003180 __drm_atomic_helper_plane_destroy_state(plane->state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003181
Daniel Vetterd4617012014-11-03 15:56:43 +01003182 kfree(plane->state);
3183 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003184
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003185 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003186 plane->state->plane = plane;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003187 plane->state->rotation = DRM_ROTATE_0;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003188 }
Daniel Vetterd4617012014-11-03 15:56:43 +01003189}
3190EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3191
3192/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003193 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3194 * @plane: plane object
3195 * @state: atomic plane state
3196 *
3197 * Copies atomic state from a plane's current state. This is useful for
3198 * drivers that subclass the plane state.
3199 */
3200void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3201 struct drm_plane_state *state)
3202{
3203 memcpy(state, plane->state, sizeof(*state));
3204
3205 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003206 drm_framebuffer_get(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003207
3208 state->fence = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003209}
3210EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3211
3212/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003213 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3214 * @plane: drm plane
3215 *
3216 * Default plane state duplicate hook for drivers which don't have their own
3217 * subclassed plane state structure.
3218 */
3219struct drm_plane_state *
3220drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3221{
Daniel Vetter321ebf02014-11-04 22:57:27 +01003222 struct drm_plane_state *state;
3223
Daniel Vetterd4617012014-11-03 15:56:43 +01003224 if (WARN_ON(!plane->state))
3225 return NULL;
3226
Thierry Redingf5e78402015-01-28 14:54:32 +01003227 state = kmalloc(sizeof(*state), GFP_KERNEL);
3228 if (state)
3229 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003230
3231 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003232}
3233EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3234
3235/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003236 * __drm_atomic_helper_plane_destroy_state - release plane state
Thierry Redingf5e78402015-01-28 14:54:32 +01003237 * @state: plane state object to release
3238 *
3239 * Releases all resources stored in the plane state without actually freeing
3240 * the memory of the plane state. This is useful for drivers that subclass the
3241 * plane state.
3242 */
Daniel Vetter2f701692016-05-09 16:34:10 +02003243void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003244{
3245 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003246 drm_framebuffer_put(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003247
3248 if (state->fence)
3249 dma_fence_put(state->fence);
Thierry Redingf5e78402015-01-28 14:54:32 +01003250}
3251EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3252
3253/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003254 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3255 * @plane: drm plane
3256 * @state: plane state object to release
3257 *
3258 * Default plane state destroy hook for drivers which don't have their own
3259 * subclassed plane state structure.
3260 */
3261void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01003262 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01003263{
Daniel Vetter2f701692016-05-09 16:34:10 +02003264 __drm_atomic_helper_plane_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003265 kfree(state);
3266}
3267EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3268
3269/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003270 * __drm_atomic_helper_connector_reset - reset state on connector
3271 * @connector: drm connector
3272 * @conn_state: connector state to assign
3273 *
3274 * Initializes the newly allocated @conn_state and assigns it to
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003275 * the &drm_conector->state pointer of @connector, usually required when
3276 * initializing the drivers or when called from the &drm_connector_funcs.reset
3277 * hook.
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003278 *
3279 * This is useful for drivers that subclass the connector state.
3280 */
3281void
3282__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3283 struct drm_connector_state *conn_state)
3284{
3285 if (conn_state)
3286 conn_state->connector = connector;
3287
3288 connector->state = conn_state;
3289}
3290EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3291
3292/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003293 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
Daniel Vetterd4617012014-11-03 15:56:43 +01003294 * @connector: drm connector
3295 *
3296 * Resets the atomic state for @connector by freeing the state pointer (which
3297 * might be NULL, e.g. at driver load time) and allocating a new empty state
3298 * object.
3299 */
3300void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3301{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003302 struct drm_connector_state *conn_state =
3303 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003304
Daniel Vetterb0b55112016-04-22 22:10:29 +02003305 if (connector->state)
Daniel Vetterfabd9102016-05-09 16:34:11 +02003306 __drm_atomic_helper_connector_destroy_state(connector->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003307
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003308 kfree(connector->state);
3309 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003310}
3311EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3312
3313/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003314 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3315 * @connector: connector object
3316 * @state: atomic connector state
3317 *
3318 * Copies atomic state from a connector's current state. This is useful for
3319 * drivers that subclass the connector state.
3320 */
3321void
3322__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3323 struct drm_connector_state *state)
3324{
3325 memcpy(state, connector->state, sizeof(*state));
Dave Airlied2307de2016-04-27 11:27:39 +10003326 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003327 drm_connector_get(connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003328}
3329EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3330
3331/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003332 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3333 * @connector: drm connector
3334 *
3335 * Default connector state duplicate hook for drivers which don't have their own
3336 * subclassed connector state structure.
3337 */
3338struct drm_connector_state *
3339drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3340{
Thierry Redingf5e78402015-01-28 14:54:32 +01003341 struct drm_connector_state *state;
3342
Daniel Vetterd4617012014-11-03 15:56:43 +01003343 if (WARN_ON(!connector->state))
3344 return NULL;
3345
Thierry Redingf5e78402015-01-28 14:54:32 +01003346 state = kmalloc(sizeof(*state), GFP_KERNEL);
3347 if (state)
3348 __drm_atomic_helper_connector_duplicate_state(connector, state);
3349
3350 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003351}
3352EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3353
3354/**
Thierry Reding397fd772015-09-08 15:00:45 +02003355 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3356 * @dev: DRM device
3357 * @ctx: lock acquisition context
3358 *
3359 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01003360 * duplicating their respective states. This is used for example by suspend/
3361 * resume support code to save the state prior to suspend such that it can
3362 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02003363 *
3364 * Note that this treats atomic state as persistent between save and restore.
3365 * Drivers must make sure that this is possible and won't result in confusion
3366 * or erroneous behaviour.
3367 *
3368 * Note that if callers haven't already acquired all modeset locks this might
3369 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3370 *
3371 * Returns:
3372 * A pointer to the copy of the atomic state object on success or an
3373 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01003374 *
3375 * See also:
3376 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02003377 */
3378struct drm_atomic_state *
3379drm_atomic_helper_duplicate_state(struct drm_device *dev,
3380 struct drm_modeset_acquire_ctx *ctx)
3381{
3382 struct drm_atomic_state *state;
3383 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003384 struct drm_connector_list_iter conn_iter;
Thierry Reding397fd772015-09-08 15:00:45 +02003385 struct drm_plane *plane;
3386 struct drm_crtc *crtc;
3387 int err = 0;
3388
3389 state = drm_atomic_state_alloc(dev);
3390 if (!state)
3391 return ERR_PTR(-ENOMEM);
3392
3393 state->acquire_ctx = ctx;
3394
3395 drm_for_each_crtc(crtc, dev) {
3396 struct drm_crtc_state *crtc_state;
3397
3398 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3399 if (IS_ERR(crtc_state)) {
3400 err = PTR_ERR(crtc_state);
3401 goto free;
3402 }
3403 }
3404
3405 drm_for_each_plane(plane, dev) {
3406 struct drm_plane_state *plane_state;
3407
3408 plane_state = drm_atomic_get_plane_state(state, plane);
3409 if (IS_ERR(plane_state)) {
3410 err = PTR_ERR(plane_state);
3411 goto free;
3412 }
3413 }
3414
Thierry Redingb982dab2017-02-28 15:46:43 +01003415 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003416 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding397fd772015-09-08 15:00:45 +02003417 struct drm_connector_state *conn_state;
3418
3419 conn_state = drm_atomic_get_connector_state(state, conn);
3420 if (IS_ERR(conn_state)) {
3421 err = PTR_ERR(conn_state);
Thierry Redingb982dab2017-02-28 15:46:43 +01003422 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003423 goto free;
3424 }
3425 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003426 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003427
3428 /* clear the acquire context so that it isn't accidentally reused */
3429 state->acquire_ctx = NULL;
3430
3431free:
3432 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003433 drm_atomic_state_put(state);
Thierry Reding397fd772015-09-08 15:00:45 +02003434 state = ERR_PTR(err);
3435 }
3436
3437 return state;
3438}
3439EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3440
3441/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003442 * __drm_atomic_helper_connector_destroy_state - release connector state
Thierry Redingf5e78402015-01-28 14:54:32 +01003443 * @state: connector state object to release
3444 *
3445 * Releases all resources stored in the connector state without actually
3446 * freeing the memory of the connector state. This is useful for drivers that
3447 * subclass the connector state.
3448 */
3449void
Daniel Vetterfabd9102016-05-09 16:34:11 +02003450__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003451{
Dave Airlied2307de2016-04-27 11:27:39 +10003452 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003453 drm_connector_put(state->connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003454}
3455EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3456
3457/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003458 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3459 * @connector: drm connector
3460 * @state: connector state object to release
3461 *
3462 * Default connector state destroy hook for drivers which don't have their own
3463 * subclassed connector state structure.
3464 */
3465void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3466 struct drm_connector_state *state)
3467{
Daniel Vetterfabd9102016-05-09 16:34:11 +02003468 __drm_atomic_helper_connector_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003469 kfree(state);
3470}
3471EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003472
3473/**
3474 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3475 * @crtc: CRTC object
3476 * @red: red correction table
3477 * @green: green correction table
3478 * @blue: green correction table
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003479 * @size: size of the tables
Daniel Vetter6d124ff2017-04-03 10:33:01 +02003480 * @ctx: lock acquire context
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003481 *
3482 * Implements support for legacy gamma correction table for drivers
3483 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3484 * properties.
3485 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003486int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3487 u16 *red, u16 *green, u16 *blue,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02003488 uint32_t size,
3489 struct drm_modeset_acquire_ctx *ctx)
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003490{
3491 struct drm_device *dev = crtc->dev;
3492 struct drm_mode_config *config = &dev->mode_config;
3493 struct drm_atomic_state *state;
3494 struct drm_crtc_state *crtc_state;
3495 struct drm_property_blob *blob = NULL;
3496 struct drm_color_lut *blob_data;
3497 int i, ret = 0;
3498
3499 state = drm_atomic_state_alloc(crtc->dev);
3500 if (!state)
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003501 return -ENOMEM;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003502
3503 blob = drm_property_create_blob(dev,
3504 sizeof(struct drm_color_lut) * size,
3505 NULL);
Lionel Landwerlin562c5b42016-03-10 12:04:21 +00003506 if (IS_ERR(blob)) {
3507 ret = PTR_ERR(blob);
Lionel Landwerlinc1f415c2016-03-11 12:17:26 +00003508 blob = NULL;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003509 goto fail;
3510 }
3511
3512 /* Prepare GAMMA_LUT with the legacy values. */
3513 blob_data = (struct drm_color_lut *) blob->data;
3514 for (i = 0; i < size; i++) {
3515 blob_data[i].red = red[i];
3516 blob_data[i].green = green[i];
3517 blob_data[i].blue = blue[i];
3518 }
3519
Daniel Vetter3a09f732017-04-03 10:33:02 +02003520 state->acquire_ctx = ctx;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003521 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3522 if (IS_ERR(crtc_state)) {
3523 ret = PTR_ERR(crtc_state);
3524 goto fail;
3525 }
3526
3527 /* Reset DEGAMMA_LUT and CTM properties. */
3528 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3529 config->degamma_lut_property, 0);
3530 if (ret)
3531 goto fail;
3532
3533 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3534 config->ctm_property, 0);
3535 if (ret)
3536 goto fail;
3537
3538 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3539 config->gamma_lut_property, blob->base.id);
3540 if (ret)
3541 goto fail;
3542
3543 ret = drm_atomic_commit(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003544
Daniel Vetter3a09f732017-04-03 10:33:02 +02003545fail:
Chris Wilson08536952016-10-14 13:18:18 +01003546 drm_atomic_state_put(state);
Thierry Reding6472e502017-02-28 15:46:42 +01003547 drm_property_blob_put(blob);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003548 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003549}
3550EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);