blob: 6426339427a43a00a818faaa395ab5a385d34e87 [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äb5ceff22015-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 Brezillonc61b93f2016-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 Brezillonc61b93f2016-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äb5ceff22015-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äb5ceff22015-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 Lankhorstce09d762017-04-06 13:19:03 +0200462 * update and adds any additional connectors needed for full modesets. It calls
463 * the various per-object callbacks in the follow order:
464 *
465 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
466 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
467 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
468 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
469 * 4. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
470 * 5. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
471 * This function is only called when the encoder will be part of a configured crtc,
472 * it must not be used for implementing connector property validation.
473 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
474 * instead.
475 * 6. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200476 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100477 * &drm_crtc_state.mode_changed is set when the input mode is changed.
478 * &drm_crtc_state.connectors_changed is set when a connector is added or
479 * removed from the crtc. &drm_crtc_state.active_changed is set when
480 * &drm_crtc_state.active changes, which is used for DPMS.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100481 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100482 *
483 * IMPORTANT:
484 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100485 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
486 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
487 * without a full modeset) _must_ call this function afterwards after that
488 * change. It is permitted to call this function multiple times for the same
489 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
490 * upon the adjusted dotclock for fifo space allocation and watermark
491 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100492 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200493 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100494 * Zero for success or -errno
495 */
496int
Rob Clark934ce1c2014-11-19 16:41:33 -0500497drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200498 struct drm_atomic_state *state)
499{
Daniel Vetter623369e2014-09-16 17:50:47 +0200500 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100501 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300502 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100503 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200504 int i, ret;
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200505 unsigned connectors_mask = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200506
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100507 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200508 bool has_connectors =
509 !!new_crtc_state->connector_mask;
510
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100511 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200512 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
513 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100514 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200515 }
516
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100517 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200518 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
519 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200520
521 /*
522 * For clarity this assignment is done here, but
523 * enable == 0 is only true when there are no
524 * connectors and a NULL mode.
525 *
526 * The other way around is true as well. enable != 0
527 * iff connectors are attached and a mode is set.
528 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100529 new_crtc_state->mode_changed = true;
530 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200531 }
Maarten Lankhorst24d66522017-04-06 13:19:01 +0200532
533 if (old_crtc_state->active != new_crtc_state->active) {
534 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
535 crtc->base.id, crtc->name);
536 new_crtc_state->active_changed = true;
537 }
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200538
539 if (new_crtc_state->enable != has_connectors) {
540 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
541 crtc->base.id, crtc->name);
542
543 return -EINVAL;
544 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200545 }
546
Maarten Lankhorst44596b82017-04-06 13:19:00 +0200547 ret = handle_conflicting_encoders(state, false);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100548 if (ret)
549 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100550
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100551 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200552 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
553
Daniel Vetter623369e2014-09-16 17:50:47 +0200554 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100555 * This only sets crtc->connectors_changed for routing changes,
556 * drivers must set crtc->connectors_changed themselves when
557 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200558 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100559 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100560 old_connector_state,
561 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200562 if (ret)
563 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100564 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100565 new_crtc_state = drm_atomic_get_new_crtc_state(state,
566 old_connector_state->crtc);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100567 if (old_connector_state->link_status !=
568 new_connector_state->link_status)
569 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200570 }
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200571
572 if (funcs->atomic_check)
573 ret = funcs->atomic_check(connector, new_connector_state);
574 if (ret)
575 return ret;
576
577 connectors_mask += BIT(i);
Daniel Vetter623369e2014-09-16 17:50:47 +0200578 }
579
580 /*
581 * After all the routing has been prepared we need to add in any
582 * connector which is itself unchanged, but who's crtc changes it's
583 * configuration. This must be done before calling mode_fixup in case a
584 * crtc only changed its mode but has the same set of connectors.
585 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100586 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100587 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100588 continue;
589
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200590 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
591 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100592 new_crtc_state->enable ? 'y' : 'n',
593 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200594
595 ret = drm_atomic_add_affected_connectors(state, crtc);
596 if (ret != 0)
597 return ret;
598
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200599 ret = drm_atomic_add_affected_planes(state, crtc);
600 if (ret != 0)
601 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200602 }
603
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200604 /*
605 * Iterate over all connectors again, to make sure atomic_check()
606 * has been called on them when a modeset is forced.
607 */
608 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
609 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
610
611 if (connectors_mask & BIT(i))
612 continue;
613
614 if (funcs->atomic_check)
615 ret = funcs->atomic_check(connector, new_connector_state);
616 if (ret)
617 return ret;
618 }
619
Daniel Vetter623369e2014-09-16 17:50:47 +0200620 return mode_fixup(state);
621}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100622EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200623
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100624/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800625 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100626 * @dev: DRM device
627 * @state: the driver state object
628 *
629 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100630 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100631 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
632 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100633 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100634 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200635 * updated planes.
636 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200637 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100638 * Zero for success or -errno
639 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100640int
641drm_atomic_helper_check_planes(struct drm_device *dev,
642 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100643{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300644 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100645 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300646 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100647 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100648 int i, ret = 0;
649
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100650 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200651 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100652
653 funcs = plane->helper_private;
654
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100655 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100656
657 if (!funcs || !funcs->atomic_check)
658 continue;
659
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100660 ret = funcs->atomic_check(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100661 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200662 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
663 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100664 return ret;
665 }
666 }
667
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100668 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200669 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100670
671 funcs = crtc->helper_private;
672
673 if (!funcs || !funcs->atomic_check)
674 continue;
675
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100676 ret = funcs->atomic_check(crtc, new_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100677 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200678 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
679 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100680 return ret;
681 }
682 }
683
Daniel Vetterd9b13622014-11-26 16:57:41 +0100684 return ret;
685}
686EXPORT_SYMBOL(drm_atomic_helper_check_planes);
687
688/**
689 * drm_atomic_helper_check - validate state object
690 * @dev: DRM device
691 * @state: the driver state object
692 *
693 * Check the state object to see if the requested state is physically possible.
694 * Only crtcs and planes have check callbacks, so for any additional (global)
695 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100696 * Drivers without such needs can directly use this as their
697 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100698 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100699 * This just wraps the two parts of the state checking for planes and modeset
700 * state in the default order: First it calls drm_atomic_helper_check_modeset()
701 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100702 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
703 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
704 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100705 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200706 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100707 * Zero for success or -errno
708 */
709int drm_atomic_helper_check(struct drm_device *dev,
710 struct drm_atomic_state *state)
711{
712 int ret;
713
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100714 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100715 if (ret)
716 return ret;
717
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100718 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500719 if (ret)
720 return ret;
721
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100722 return ret;
723}
724EXPORT_SYMBOL(drm_atomic_helper_check);
725
Daniel Vetter623369e2014-09-16 17:50:47 +0200726static void
727disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
728{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300729 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100730 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300731 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100732 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200733 int i;
734
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100735 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200736 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200737 struct drm_encoder *encoder;
738
Daniel Vetter623369e2014-09-16 17:50:47 +0200739 /* Shut down everything that's in the changeset and currently
740 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300741 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200742 continue;
743
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100744 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100745
Daniel Vetter4218a322015-03-26 22:18:40 +0100746 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200747 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100748 continue;
749
Rob Clark46df9ad2014-11-20 15:40:36 -0500750 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200751
Rob Clark46df9ad2014-11-20 15:40:36 -0500752 /* We shouldn't get this far if we didn't previously have
753 * an encoder.. but WARN_ON() rather than explode.
754 */
755 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200756 continue;
757
758 funcs = encoder->helper_private;
759
Daniel Vetter17a38d92015-02-22 12:24:16 +0100760 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
761 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100762
Daniel Vetter623369e2014-09-16 17:50:47 +0200763 /*
764 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800765 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200766 */
Archit Taneja862e6862015-05-21 11:03:16 +0530767 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200768
769 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200770 if (funcs) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100771 if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +0200772 funcs->prepare(encoder);
773 else if (funcs->disable)
774 funcs->disable(encoder);
775 else if (funcs->dpms)
776 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
777 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200778
Archit Taneja862e6862015-05-21 11:03:16 +0530779 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200780 }
781
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100782 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200783 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200784
785 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100786 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100787 continue;
788
789 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200790 continue;
791
792 funcs = crtc->helper_private;
793
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200794 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
795 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100796
797
Daniel Vetter623369e2014-09-16 17:50:47 +0200798 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100799 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200800 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +0800801 else if (funcs->atomic_disable)
802 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200803 else if (funcs->disable)
804 funcs->disable(crtc);
805 else
806 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
807 }
808}
809
Daniel Vetter4c18d302015-05-12 15:27:37 +0200810/**
811 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
812 * @dev: DRM device
813 * @old_state: atomic state object with old state structures
814 *
815 * This function updates all the various legacy modeset state pointers in
816 * connectors, encoders and crtcs. It also updates the timestamping constants
817 * used for precise vblank timestamps by calling
818 * drm_calc_timestamping_constants().
819 *
820 * Drivers can use this for building their own atomic commit if they don't have
821 * a pure helper-based modeset implementation.
822 */
823void
824drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
825 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200826{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300827 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100828 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300829 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100830 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200831 int i;
832
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200833 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100834 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200835 if (connector->encoder) {
836 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200837
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200838 connector->encoder->crtc = NULL;
839 connector->encoder = NULL;
840 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200841
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100842 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200843 if ((!crtc && old_conn_state->crtc) ||
844 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
845 struct drm_property *dpms_prop =
846 dev->mode_config.dpms_property;
847 int mode = DRM_MODE_DPMS_OFF;
848
849 if (crtc && crtc->state->active)
850 mode = DRM_MODE_DPMS_ON;
851
852 connector->dpms = mode;
853 drm_object_property_set_value(&connector->base,
854 dpms_prop, mode);
855 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200856 }
857
858 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100859 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
860 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200861 continue;
862
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100863 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200864 continue;
865
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100866 connector->encoder = new_conn_state->best_encoder;
867 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200868 }
869
870 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100871 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200872 struct drm_plane *primary = crtc->primary;
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100873 struct drm_plane_state *new_plane_state;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200874
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100875 crtc->mode = new_crtc_state->mode;
876 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200877
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100878 new_plane_state =
879 drm_atomic_get_new_plane_state(old_state, primary);
880
881 if (new_plane_state && new_plane_state->crtc == crtc) {
882 crtc->x = new_plane_state->src_x >> 16;
883 crtc->y = new_plane_state->src_y >> 16;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200884 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200885
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100886 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200887 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100888 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200889 }
890}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200891EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200892
893static void
894crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
895{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300896 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100897 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300898 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100899 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200900 int i;
901
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100902 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200903 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200904
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100905 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200906 continue;
907
908 funcs = crtc->helper_private;
909
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100910 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200911 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
912 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100913
Daniel Vetter623369e2014-09-16 17:50:47 +0200914 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100915 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200916 }
917
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100918 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200919 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200920 struct drm_encoder *encoder;
921 struct drm_display_mode *mode, *adjusted_mode;
922
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100923 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200924 continue;
925
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100926 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200927 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100928 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200929 mode = &new_crtc_state->mode;
930 adjusted_mode = &new_crtc_state->adjusted_mode;
931
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100932 if (!new_crtc_state->mode_changed)
933 continue;
934
Daniel Vetter17a38d92015-02-22 12:24:16 +0100935 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
936 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100937
Daniel Vetter623369e2014-09-16 17:50:47 +0200938 /*
939 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800940 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200941 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200942 if (funcs && funcs->atomic_mode_set) {
943 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100944 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200945 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +0100946 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200947 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200948
Archit Taneja862e6862015-05-21 11:03:16 +0530949 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200950 }
951}
952
953/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100954 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200955 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100956 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200957 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100958 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200959 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100960 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300961 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100962 * drm_atomic_helper_commit_planes(), which is what the default commit function
963 * does. But drivers with different needs can group the modeset commits together
964 * and do the plane commits at the end. This is useful for drivers doing runtime
965 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200966 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100967void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
968 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200969{
Laurent Pincharta072f802015-02-22 12:24:18 +0100970 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200971
972 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
973
Laurent Pincharta072f802015-02-22 12:24:18 +0100974 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200975}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100976EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200977
978/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100979 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200980 * @dev: DRM device
981 * @old_state: atomic state object with old state structures
982 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100983 * This function enables all the outputs with the new configuration which had to
984 * be turned off for the update.
985 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300986 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100987 * drm_atomic_helper_commit_planes(), which is what the default commit function
988 * does. But drivers with different needs can group the modeset commits together
989 * and do the plane commits at the end. This is useful for drivers doing runtime
990 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200991 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100992void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
993 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200994{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300995 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100996 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300997 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100998 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200999 int i;
1000
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001001 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001002 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001003
1004 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001005 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001006 continue;
1007
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001008 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +02001009 continue;
1010
1011 funcs = crtc->helper_private;
1012
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001013 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001014 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1015 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001016
Daniel Vetteree0a89c2015-01-22 16:36:24 +01001017 if (funcs->enable)
1018 funcs->enable(crtc);
1019 else
1020 funcs->commit(crtc);
1021 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001022 }
1023
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001024 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001025 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001026 struct drm_encoder *encoder;
1027
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001028 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001029 continue;
1030
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001031 if (!new_conn_state->crtc->state->active ||
1032 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001033 continue;
1034
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001035 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001036 funcs = encoder->helper_private;
1037
Daniel Vetter17a38d92015-02-22 12:24:16 +01001038 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1039 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001040
Daniel Vetter623369e2014-09-16 17:50:47 +02001041 /*
1042 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001043 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001044 */
Archit Taneja862e6862015-05-21 11:03:16 +05301045 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001046
Noralf Trønnes28276352016-05-11 18:09:20 +02001047 if (funcs) {
1048 if (funcs->enable)
1049 funcs->enable(encoder);
1050 else if (funcs->commit)
1051 funcs->commit(encoder);
1052 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001053
Archit Taneja862e6862015-05-21 11:03:16 +05301054 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001055 }
1056}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001057EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001058
Rob Clark4c5b7f32016-03-18 19:14:55 -04001059/**
1060 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1061 * @dev: DRM device
1062 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001063 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1064 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001065 *
1066 * For implicit sync, driver should fish the exclusive fence out from the
1067 * incoming fb's and stash it in the drm_plane_state. This is called after
1068 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1069 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001070 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001071 * Note that @pre_swap is needed since the point where we block for fences moves
1072 * around depending upon whether an atomic commit is blocking or
Gustavo Padovan42590372017-04-27 11:35:06 -03001073 * non-blocking. For non-blocking commit all waiting needs to happen after
1074 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001075 * to wait **before** we do anything that can't be easily rolled back. That is
1076 * before we call drm_atomic_helper_swap_state().
1077 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001078 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001079 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001080int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1081 struct drm_atomic_state *state,
1082 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001083{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001084 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001085 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001086 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001087
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001088 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1089 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001090 continue;
1091
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001092 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001093
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001094 /*
1095 * If waiting for fences pre-swap (ie: nonblock), userspace can
1096 * still interrupt the operation. Instead of blocking until the
1097 * timer expires, make the wait interruptible.
1098 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001099 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001100 if (ret)
1101 return ret;
1102
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001103 dma_fence_put(new_plane_state->fence);
1104 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001105 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001106
1107 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001108}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001109EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001110
John Keepingc2409062016-01-19 10:46:58 +00001111/**
Rob Clark5ee32292014-11-11 19:38:59 -05001112 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1113 * @dev: DRM device
1114 * @old_state: atomic state object with old state structures
1115 *
1116 * Helper to, after atomic commit, wait for vblanks on all effected
1117 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001118 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1119 * framebuffers have actually changed to optimize for the legacy cursor and
1120 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001121 */
1122void
1123drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1124 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001125{
1126 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001127 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001128 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001129 unsigned crtc_mask = 0;
1130
1131 /*
1132 * Legacy cursor ioctls are completely unsynced, and userspace
1133 * relies on that (by doing tons of cursor updates).
1134 */
1135 if (old_state->legacy_cursor_update)
1136 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001137
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001138 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta3fbb532016-12-08 14:45:27 +01001139 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
Daniel Vetterab58e332014-11-24 20:42:42 +01001140 continue;
1141
Daniel Vetter623369e2014-09-16 17:50:47 +02001142 ret = drm_crtc_vblank_get(crtc);
1143 if (ret != 0)
1144 continue;
1145
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001146 crtc_mask |= drm_crtc_mask(crtc);
1147 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001148 }
1149
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001150 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001151 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001152 continue;
1153
1154 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001155 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001156 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001157 msecs_to_jiffies(50));
1158
Russell King6ac7c542017-02-13 12:27:03 +00001159 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1160 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001161
Daniel Vetter623369e2014-09-16 17:50:47 +02001162 drm_crtc_vblank_put(crtc);
1163 }
1164}
Rob Clark5ee32292014-11-11 19:38:59 -05001165EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001166
1167/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001168 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001169 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001170 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001171 * This is the default implementation for the
1172 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
Daniel Vetter623369e2014-09-16 17:50:47 +02001173 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001174 * Note that the default ordering of how the various stages are called is to
1175 * match the legacy modeset helper library closest. One peculiarity of that is
1176 * that it doesn't mesh well with runtime PM at all.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001177 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001178 * For drivers supporting runtime PM the recommended sequence is instead ::
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001179 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001180 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001181 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001182 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001183 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001184 * drm_atomic_helper_commit_planes(dev, old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001185 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001186 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001187 * for committing the atomic update to hardware. See the kerneldoc entries for
1188 * these three functions for more details.
1189 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001190void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001191{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001192 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001193
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001194 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001195
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001196 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001197
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001198 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001199
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001200 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001201
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001202 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001203
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001204 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001205}
1206EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1207
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001208static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001209{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001210 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001211 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001212
1213 funcs = dev->mode_config.helper_private;
1214
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001215 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001216
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001217 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001218
1219 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001220 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001221 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001222 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001223
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001224 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001225
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001226 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001227}
1228
1229static void commit_work(struct work_struct *work)
1230{
1231 struct drm_atomic_state *state = container_of(work,
1232 struct drm_atomic_state,
1233 commit_work);
1234 commit_tail(state);
1235}
1236
1237/**
1238 * drm_atomic_helper_commit - commit validated state object
1239 * @dev: DRM device
1240 * @state: the driver state object
1241 * @nonblock: whether nonblocking behavior is requested.
1242 *
1243 * This function commits a with drm_atomic_helper_check() pre-validated state
1244 * object. This can still fail when e.g. the framebuffer reservation fails. This
1245 * function implements nonblocking commits, using
1246 * drm_atomic_helper_setup_commit() and related functions.
1247 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001248 * Committing the actual hardware state is done through the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001249 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1250 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001251 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001252 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001253 * Zero for success or -errno.
1254 */
1255int drm_atomic_helper_commit(struct drm_device *dev,
1256 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001257 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001258{
1259 int ret;
1260
Daniel Vettera095caa2016-06-08 17:15:36 +02001261 ret = drm_atomic_helper_setup_commit(state, nonblock);
1262 if (ret)
1263 return ret;
1264
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001265 INIT_WORK(&state->commit_work, commit_work);
1266
Daniel Vetter623369e2014-09-16 17:50:47 +02001267 ret = drm_atomic_helper_prepare_planes(dev, state);
1268 if (ret)
1269 return ret;
1270
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001271 if (!nonblock) {
1272 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001273 if (ret) {
1274 drm_atomic_helper_cleanup_planes(dev, state);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001275 return ret;
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001276 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001277 }
1278
Daniel Vetter623369e2014-09-16 17:50:47 +02001279 /*
1280 * This is the point of no return - everything below never fails except
1281 * when the hw goes bonghits. Which means we can commit the new state on
1282 * the software side now.
1283 */
1284
Daniel Vetter5e84c262016-06-10 00:06:32 +02001285 drm_atomic_helper_swap_state(state, true);
Daniel Vetter623369e2014-09-16 17:50:47 +02001286
1287 /*
1288 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001289 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001290 * that the asynchronous work has either been cancelled (if the driver
1291 * supports it, which at least requires that the framebuffers get
1292 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1293 * before the new state gets committed on the software side with
1294 * drm_atomic_helper_swap_state().
1295 *
1296 * This scheme allows new atomic state updates to be prepared and
1297 * checked in parallel to the asynchronous completion of the previous
1298 * update. Which is important since compositors need to figure out the
1299 * composition of the next frame right after having submitted the
1300 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001301 *
1302 * NOTE: Commit work has multiple phases, first hardware commit, then
1303 * cleanup. We want them to overlap, hence need system_unbound_wq to
1304 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001305 */
1306
Chris Wilson08536952016-10-14 13:18:18 +01001307 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001308 if (nonblock)
1309 queue_work(system_unbound_wq, &state->commit_work);
1310 else
1311 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001312
1313 return 0;
1314}
1315EXPORT_SYMBOL(drm_atomic_helper_commit);
1316
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001317/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001318 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001319 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001320 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001321 *
1322 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1323 * which commit needs to call which can fail, so we want to run it first and
1324 * synchronously.
1325 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001326 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001327 * might be affected the new state update. This can be done by either cancelling
1328 * or flushing the work items, depending upon whether the driver can deal with
1329 * cancelled updates. Note that it is important to ensure that the framebuffer
1330 * cleanup is still done when cancelling.
1331 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001332 * Asynchronous workers need to have sufficient parallelism to be able to run
1333 * different atomic commits on different CRTCs in parallel. The simplest way to
1334 * achive this is by running them on the &system_unbound_wq work queue. Note
1335 * that drivers are not required to split up atomic commits and run an
1336 * individual commit in parallel - userspace is supposed to do that if it cares.
1337 * But it might be beneficial to do that for modesets, since those necessarily
1338 * must be done as one global operation, and enabling or disabling a CRTC can
1339 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001340 *
1341 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001342 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001343 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001344 * while it's guaranteed that no relevant nonblocking worker runs means that
1345 * nonblocking workers do not need grab any locks. Actually they must not grab
1346 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001347 *
1348 * 4. Schedule a work item to do all subsequent steps, using the split-out
1349 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1350 * then cleaning up the framebuffers after the old framebuffer is no longer
1351 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001352 *
1353 * The above scheme is implemented in the atomic helper libraries in
1354 * drm_atomic_helper_commit() using a bunch of helper functions. See
1355 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001356 */
1357
Daniel Vettera095caa2016-06-08 17:15:36 +02001358static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1359{
1360 struct drm_crtc_commit *commit, *stall_commit = NULL;
1361 bool completed = true;
1362 int i;
1363 long ret = 0;
1364
1365 spin_lock(&crtc->commit_lock);
1366 i = 0;
1367 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1368 if (i == 0) {
1369 completed = try_wait_for_completion(&commit->flip_done);
1370 /* Userspace is not allowed to get ahead of the previous
1371 * commit with nonblocking ones. */
1372 if (!completed && nonblock) {
1373 spin_unlock(&crtc->commit_lock);
1374 return -EBUSY;
1375 }
1376 } else if (i == 1) {
1377 stall_commit = commit;
1378 drm_crtc_commit_get(stall_commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001379 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001380 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001381
1382 i++;
1383 }
1384 spin_unlock(&crtc->commit_lock);
1385
1386 if (!stall_commit)
1387 return 0;
1388
1389 /* We don't want to let commits get ahead of cleanup work too much,
1390 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1391 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001392 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001393 10*HZ);
1394 if (ret == 0)
1395 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1396 crtc->base.id, crtc->name);
1397
1398 drm_crtc_commit_put(stall_commit);
1399
1400 return ret < 0 ? ret : 0;
1401}
1402
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001403static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001404{
1405 struct drm_crtc_commit *commit = container_of(completion,
1406 typeof(*commit),
1407 flip_done);
1408
1409 drm_crtc_commit_put(commit);
1410}
1411
Daniel Vettera095caa2016-06-08 17:15:36 +02001412/**
1413 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1414 * @state: new modeset state to be committed
1415 * @nonblock: whether nonblocking behavior is requested.
1416 *
1417 * This function prepares @state to be used by the atomic helper's support for
1418 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001419 * should always call this function from their
1420 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001421 *
1422 * To be able to use this support drivers need to use a few more helper
1423 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1424 * actually committing the hardware state, and for nonblocking commits this call
1425 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1426 * and it's stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001427 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001428 *
1429 * Completion of the hardware commit step must be signalled using
1430 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1431 * to read or change any permanent software or hardware modeset state. The only
1432 * exception is state protected by other means than &drm_modeset_lock locks.
1433 * Only the free standing @state with pointers to the old state structures can
1434 * be inspected, e.g. to clean up old buffers using
1435 * drm_atomic_helper_cleanup_planes().
1436 *
1437 * At the very end, before cleaning up @state drivers must call
1438 * drm_atomic_helper_commit_cleanup_done().
1439 *
1440 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1441 * complete and esay-to-use default implementation of the atomic_commit() hook.
1442 *
1443 * The tracking of asynchronously executed and still pending commits is done
1444 * using the core structure &drm_crtc_commit.
1445 *
1446 * By default there's no need to clean up resources allocated by this function
1447 * explicitly: drm_atomic_state_default_clear() will take care of that
1448 * automatically.
1449 *
1450 * Returns:
1451 *
1452 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1453 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1454 */
1455int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1456 bool nonblock)
1457{
1458 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001459 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001460 struct drm_crtc_commit *commit;
1461 int i, ret;
1462
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001463 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001464 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1465 if (!commit)
1466 return -ENOMEM;
1467
1468 init_completion(&commit->flip_done);
1469 init_completion(&commit->hw_done);
1470 init_completion(&commit->cleanup_done);
1471 INIT_LIST_HEAD(&commit->commit_entry);
1472 kref_init(&commit->ref);
1473 commit->crtc = crtc;
1474
1475 state->crtcs[i].commit = commit;
1476
1477 ret = stall_checks(crtc, nonblock);
1478 if (ret)
1479 return ret;
1480
1481 /* Drivers only send out events when at least either current or
1482 * new CRTC state is active. Complete right away if everything
1483 * stays off. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001484 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001485 complete_all(&commit->flip_done);
1486 continue;
1487 }
1488
1489 /* Legacy cursor updates are fully unsynced. */
1490 if (state->legacy_cursor_update) {
1491 complete_all(&commit->flip_done);
1492 continue;
1493 }
1494
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001495 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001496 commit->event = kzalloc(sizeof(*commit->event),
1497 GFP_KERNEL);
1498 if (!commit->event)
1499 return -ENOMEM;
1500
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001501 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02001502 }
1503
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001504 new_crtc_state->event->base.completion = &commit->flip_done;
1505 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01001506 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001507 }
1508
1509 return 0;
1510}
1511EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1512
1513
1514static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1515{
1516 struct drm_crtc_commit *commit;
1517 int i = 0;
1518
1519 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1520 /* skip the first entry, that's the current commit */
1521 if (i == 1)
1522 return commit;
1523 i++;
1524 }
1525
1526 return NULL;
1527}
1528
1529/**
1530 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001531 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001532 *
1533 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001534 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02001535 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001536 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02001537 *
1538 * This is part of the atomic helper support for nonblocking commits, see
1539 * drm_atomic_helper_setup_commit() for an overview.
1540 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001541void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001542{
1543 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001544 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001545 struct drm_crtc_commit *commit;
1546 int i;
1547 long ret;
1548
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001549 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001550 spin_lock(&crtc->commit_lock);
1551 commit = preceeding_commit(crtc);
1552 if (commit)
1553 drm_crtc_commit_get(commit);
1554 spin_unlock(&crtc->commit_lock);
1555
1556 if (!commit)
1557 continue;
1558
1559 ret = wait_for_completion_timeout(&commit->hw_done,
1560 10*HZ);
1561 if (ret == 0)
1562 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1563 crtc->base.id, crtc->name);
1564
1565 /* Currently no support for overwriting flips, hence
1566 * stall for previous one to execute completely. */
1567 ret = wait_for_completion_timeout(&commit->flip_done,
1568 10*HZ);
1569 if (ret == 0)
1570 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1571 crtc->base.id, crtc->name);
1572
1573 drm_crtc_commit_put(commit);
1574 }
1575}
1576EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1577
1578/**
1579 * drm_atomic_helper_commit_hw_done - setup possible nonblocking 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 *
1582 * This function is used to signal completion of the hardware commit step. After
1583 * this step the driver is not allowed to read or change any permanent software
1584 * or hardware modeset state. The only exception is state protected by other
1585 * means than &drm_modeset_lock locks.
1586 *
1587 * Drivers should try to postpone any expensive or delayed cleanup work after
1588 * this function is called.
1589 *
1590 * This is part of the atomic helper support for nonblocking commits, see
1591 * drm_atomic_helper_setup_commit() for an overview.
1592 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001593void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001594{
1595 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001596 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001597 struct drm_crtc_commit *commit;
1598 int i;
1599
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001600 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001601 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001602 if (!commit)
1603 continue;
1604
1605 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001606 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02001607 spin_lock(&crtc->commit_lock);
1608 complete_all(&commit->hw_done);
1609 spin_unlock(&crtc->commit_lock);
1610 }
1611}
1612EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1613
1614/**
1615 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001616 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001617 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001618 * This signals completion of the atomic update @old_state, including any
1619 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01001620 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02001621 *
1622 * This is part of the atomic helper support for nonblocking commits, see
1623 * drm_atomic_helper_setup_commit() for an overview.
1624 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001625void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001626{
1627 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001628 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001629 struct drm_crtc_commit *commit;
1630 int i;
1631 long ret;
1632
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001633 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001634 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001635 if (WARN_ON(!commit))
1636 continue;
1637
1638 spin_lock(&crtc->commit_lock);
1639 complete_all(&commit->cleanup_done);
1640 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1641
1642 /* commit_list borrows our reference, need to remove before we
1643 * clean up our drm_atomic_state. But only after it actually
1644 * completed, otherwise subsequent commits won't stall properly. */
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001645 if (try_wait_for_completion(&commit->flip_done))
1646 goto del_commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001647
1648 spin_unlock(&crtc->commit_lock);
1649
1650 /* We must wait for the vblank event to signal our completion
1651 * before releasing our reference, since the vblank work does
1652 * not hold a reference of its own. */
1653 ret = wait_for_completion_timeout(&commit->flip_done,
1654 10*HZ);
1655 if (ret == 0)
1656 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1657 crtc->base.id, crtc->name);
1658
1659 spin_lock(&crtc->commit_lock);
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001660del_commit:
Daniel Vettera095caa2016-06-08 17:15:36 +02001661 list_del(&commit->commit_entry);
1662 spin_unlock(&crtc->commit_lock);
1663 }
1664}
1665EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1666
Daniel Vettere8c833a2014-07-27 18:30:19 +02001667/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001668 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001669 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001670 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001671 *
1672 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001673 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1674 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1675 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001676 *
1677 * Returns:
1678 * 0 on success, negative error code on failure.
1679 */
1680int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1681 struct drm_atomic_state *state)
1682{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001683 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001684 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001685 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001686
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001687 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001688 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001689
1690 funcs = plane->helper_private;
1691
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001692 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001693 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001694 if (ret)
1695 goto fail;
1696 }
1697 }
1698
1699 return 0;
1700
1701fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001702 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001703 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001704
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001705 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001706 continue;
1707
1708 funcs = plane->helper_private;
1709
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001710 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001711 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001712 }
1713
1714 return ret;
1715}
1716EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1717
Ville Syrjälä7135ac52016-09-19 16:33:42 +03001718static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001719{
1720 return state->crtc && state->crtc->state->active;
1721}
1722
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001723/**
1724 * drm_atomic_helper_commit_planes - commit plane state
1725 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001726 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08001727 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001728 *
1729 * This function commits the new plane state using the plane and atomic helper
1730 * functions for planes and crtcs. It assumes that the atomic state has already
1731 * been pushed into the relevant object state pointers, since this step can no
1732 * longer fail.
1733 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001734 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001735 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001736 *
1737 * Note that this function does all plane updates across all CRTCs in one step.
1738 * If the hardware can't support this approach look at
1739 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001740 *
1741 * Plane parameters can be updated by applications while the associated CRTC is
1742 * disabled. The DRM/KMS core will store the parameters in the plane state,
1743 * which will be available to the driver when the CRTC is turned on. As a result
1744 * most drivers don't need to be immediately notified of plane updates for a
1745 * disabled CRTC.
1746 *
Liu Ying2b58e982016-08-29 17:12:03 +08001747 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1748 * @flags in order not to receive plane update notifications related to a
1749 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001750 * driver code when the driver and/or hardware can't or just don't need to deal
1751 * with updates on disabled CRTCs, for example when supporting runtime PM.
1752 *
Liu Ying2b58e982016-08-29 17:12:03 +08001753 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1754 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001755 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1756 * call for a plane if the CRTC of the old plane state needs a modesetting
1757 * operation. Of course, the drivers need to disable the planes in their CRTC
1758 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08001759 *
1760 * The drm_atomic_helper_commit() default implementation doesn't set the
1761 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1762 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001763 */
1764void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001765 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001766 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001767{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001768 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001769 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001770 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001771 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001772 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08001773 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1774 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001775
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001776 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001777 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001778
1779 funcs = crtc->helper_private;
1780
1781 if (!funcs || !funcs->atomic_begin)
1782 continue;
1783
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001784 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001785 continue;
1786
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001787 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001788 }
1789
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001790 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001791 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001792 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001793
1794 funcs = plane->helper_private;
1795
Thierry Reding3cad4b62014-11-25 13:05:12 +01001796 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001797 continue;
1798
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001799 disabling = drm_atomic_plane_disabling(old_plane_state,
1800 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001801
1802 if (active_only) {
1803 /*
1804 * Skip planes related to inactive CRTCs. If the plane
1805 * is enabled use the state of the current CRTC. If the
1806 * plane is being disabled use the state of the old
1807 * CRTC to avoid skipping planes being disabled on an
1808 * active CRTC.
1809 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001810 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001811 continue;
1812 if (disabling && !plane_crtc_active(old_plane_state))
1813 continue;
1814 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001815
Thierry Reding407b8bd2014-11-20 12:05:50 +01001816 /*
1817 * Special-case disabling the plane if drivers support it.
1818 */
Liu Ying2b58e982016-08-29 17:12:03 +08001819 if (disabling && funcs->atomic_disable) {
1820 struct drm_crtc_state *crtc_state;
1821
1822 crtc_state = old_plane_state->crtc->state;
1823
1824 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1825 no_disable)
1826 continue;
1827
Thierry Reding407b8bd2014-11-20 12:05:50 +01001828 funcs->atomic_disable(plane, old_plane_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001829 } else if (new_plane_state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01001830 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08001831 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001832 }
1833
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001834 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001835 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001836
1837 funcs = crtc->helper_private;
1838
1839 if (!funcs || !funcs->atomic_flush)
1840 continue;
1841
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001842 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001843 continue;
1844
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001845 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001846 }
1847}
1848EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1849
1850/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001851 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1852 * @old_crtc_state: atomic state object with the old crtc state
1853 *
1854 * This function commits the new plane state using the plane and atomic helper
1855 * functions for planes on the specific crtc. It assumes that the atomic state
1856 * has already been pushed into the relevant object state pointers, since this
1857 * step can no longer fail.
1858 *
1859 * This function is useful when plane updates should be done crtc-by-crtc
1860 * instead of one global step like drm_atomic_helper_commit_planes() does.
1861 *
1862 * This function can only be savely used when planes are not allowed to move
1863 * between different CRTCs because this function doesn't handle inter-CRTC
1864 * depencies. Callers need to ensure that either no such depencies exist,
1865 * resolve them through ordering of commit calls or through some other means.
1866 */
1867void
1868drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1869{
1870 const struct drm_crtc_helper_funcs *crtc_funcs;
1871 struct drm_crtc *crtc = old_crtc_state->crtc;
1872 struct drm_atomic_state *old_state = old_crtc_state->state;
1873 struct drm_plane *plane;
1874 unsigned plane_mask;
1875
1876 plane_mask = old_crtc_state->plane_mask;
1877 plane_mask |= crtc->state->plane_mask;
1878
1879 crtc_funcs = crtc->helper_private;
1880 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001881 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001882
1883 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1884 struct drm_plane_state *old_plane_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001885 drm_atomic_get_old_plane_state(old_state, plane);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001886 const struct drm_plane_helper_funcs *plane_funcs;
1887
1888 plane_funcs = plane->helper_private;
1889
1890 if (!old_plane_state || !plane_funcs)
1891 continue;
1892
1893 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1894
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001895 if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001896 plane_funcs->atomic_disable)
1897 plane_funcs->atomic_disable(plane, old_plane_state);
1898 else if (plane->state->crtc ||
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001899 drm_atomic_plane_disabling(old_plane_state, plane->state))
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001900 plane_funcs->atomic_update(plane, old_plane_state);
1901 }
1902
1903 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001904 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001905}
1906EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1907
1908/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001909 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08001910 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02001911 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1912 *
1913 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08001914 * used for instance in the CRTC helper atomic_disable callback to disable
1915 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001916 *
1917 * If the atomic-parameter is set the function calls the CRTC's
1918 * atomic_begin hook before and atomic_flush hook after disabling the
1919 * planes.
1920 *
1921 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001922 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001923 */
Liu Ying28500292016-08-26 15:30:39 +08001924void
1925drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1926 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001927{
Liu Ying28500292016-08-26 15:30:39 +08001928 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02001929 const struct drm_crtc_helper_funcs *crtc_funcs =
1930 crtc->helper_private;
1931 struct drm_plane *plane;
1932
1933 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1934 crtc_funcs->atomic_begin(crtc, NULL);
1935
Liu Ying28500292016-08-26 15:30:39 +08001936 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02001937 const struct drm_plane_helper_funcs *plane_funcs =
1938 plane->helper_private;
1939
Liu Ying28500292016-08-26 15:30:39 +08001940 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001941 continue;
1942
1943 WARN_ON(!plane_funcs->atomic_disable);
1944 if (plane_funcs->atomic_disable)
1945 plane_funcs->atomic_disable(plane, NULL);
1946 }
1947
1948 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1949 crtc_funcs->atomic_flush(crtc, NULL);
1950}
1951EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1952
1953/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001954 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1955 * @dev: DRM device
1956 * @old_state: atomic state object with old state structures
1957 *
1958 * This function cleans up plane state, specifically framebuffers, from the old
1959 * configuration. Hence the old configuration must be perserved in @old_state to
1960 * be able to call this function.
1961 *
1962 * This function must also be called on the new state when the atomic update
1963 * fails at any point after calling drm_atomic_helper_prepare_planes().
1964 */
1965void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1966 struct drm_atomic_state *old_state)
1967{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001968 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001969 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001970 int i;
1971
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001972 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001973 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001974 struct drm_plane_state *plane_state;
1975
1976 /*
1977 * This might be called before swapping when commit is aborted,
1978 * in which case we have to cleanup the new state.
1979 */
1980 if (old_plane_state == plane->state)
1981 plane_state = new_plane_state;
1982 else
1983 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001984
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001985 funcs = plane->helper_private;
1986
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001987 if (funcs->cleanup_fb)
1988 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001989 }
1990}
1991EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1992
1993/**
1994 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001995 * @state: atomic state
Daniel Vetter5e84c262016-06-10 00:06:32 +02001996 * @stall: stall for proceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001997 *
1998 * This function stores the atomic state into the current state pointers in all
1999 * driver objects. It should be called after all failing steps have been done
2000 * and succeeded, but before the actual hardware state is committed.
2001 *
2002 * For cleanup and error recovery the current state for all changed objects will
2003 * be swaped into @state.
2004 *
2005 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2006 *
2007 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2008 *
2009 * 2. Do any other steps that might fail.
2010 *
2011 * 3. Put the staged state into the current state pointers with this function.
2012 *
2013 * 4. Actually commit the hardware state.
2014 *
Daniel Vetter26196f72015-08-25 16:26:03 +02002015 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002016 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02002017 *
2018 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002019 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2020 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02002021 * don't pass the right state structures to the callbacks.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002022 */
Daniel Vetter5e84c262016-06-10 00:06:32 +02002023void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2024 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002025{
2026 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02002027 long ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002028 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002029 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002030 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002031 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002032 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002033 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002034 struct drm_crtc_commit *commit;
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07002035 void *obj, *obj_state;
2036 const struct drm_private_state_funcs *funcs;
Daniel Vettera095caa2016-06-08 17:15:36 +02002037
2038 if (stall) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002039 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002040 spin_lock(&crtc->commit_lock);
2041 commit = list_first_entry_or_null(&crtc->commit_list,
2042 struct drm_crtc_commit, commit_entry);
2043 if (commit)
2044 drm_crtc_commit_get(commit);
2045 spin_unlock(&crtc->commit_lock);
2046
2047 if (!commit)
2048 continue;
2049
2050 ret = wait_for_completion_timeout(&commit->hw_done,
2051 10*HZ);
2052 if (ret == 0)
2053 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2054 crtc->base.id, crtc->name);
2055 drm_crtc_commit_put(commit);
2056 }
2057 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002058
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002059 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002060 WARN_ON(connector->state != old_conn_state);
2061
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002062 old_conn_state->state = state;
2063 new_conn_state->state = NULL;
2064
2065 state->connectors[i].state = old_conn_state;
2066 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002067 }
2068
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002069 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002070 WARN_ON(crtc->state != old_crtc_state);
2071
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002072 old_crtc_state->state = state;
2073 new_crtc_state->state = NULL;
2074
2075 state->crtcs[i].state = old_crtc_state;
2076 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002077
2078 if (state->crtcs[i].commit) {
2079 spin_lock(&crtc->commit_lock);
2080 list_add(&state->crtcs[i].commit->commit_entry,
2081 &crtc->commit_list);
2082 spin_unlock(&crtc->commit_lock);
2083
2084 state->crtcs[i].commit->event = NULL;
2085 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002086 }
2087
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002088 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002089 WARN_ON(plane->state != old_plane_state);
2090
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002091 old_plane_state->state = state;
2092 new_plane_state->state = NULL;
2093
2094 state->planes[i].state = old_plane_state;
2095 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002096 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07002097
2098 __for_each_private_obj(state, obj, obj_state, i, funcs)
2099 funcs->swap_state(obj, &state->private_objs[i].obj_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002100}
2101EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002102
2103/**
2104 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2105 * @plane: plane object to update
2106 * @crtc: owning CRTC of owning plane
2107 * @fb: framebuffer to flip onto plane
2108 * @crtc_x: x offset of primary plane on crtc
2109 * @crtc_y: y offset of primary plane on crtc
2110 * @crtc_w: width of primary plane rectangle on crtc
2111 * @crtc_h: height of primary plane rectangle on crtc
2112 * @src_x: x offset of @fb for panning
2113 * @src_y: y offset of @fb for panning
2114 * @src_w: width of source rectangle in @fb
2115 * @src_h: height of source rectangle in @fb
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002116 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002117 *
2118 * Provides a default plane update handler using the atomic driver interface.
2119 *
2120 * RETURNS:
2121 * Zero on success, error code on failure
2122 */
2123int drm_atomic_helper_update_plane(struct drm_plane *plane,
2124 struct drm_crtc *crtc,
2125 struct drm_framebuffer *fb,
2126 int crtc_x, int crtc_y,
2127 unsigned int crtc_w, unsigned int crtc_h,
2128 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002129 uint32_t src_w, uint32_t src_h,
2130 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002131{
2132 struct drm_atomic_state *state;
2133 struct drm_plane_state *plane_state;
2134 int ret = 0;
2135
2136 state = drm_atomic_state_alloc(plane->dev);
2137 if (!state)
2138 return -ENOMEM;
2139
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002140 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002141 plane_state = drm_atomic_get_plane_state(state, plane);
2142 if (IS_ERR(plane_state)) {
2143 ret = PTR_ERR(plane_state);
2144 goto fail;
2145 }
2146
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002147 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002148 if (ret != 0)
2149 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002150 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002151 plane_state->crtc_x = crtc_x;
2152 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002153 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002154 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002155 plane_state->src_x = src_x;
2156 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002157 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002158 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002159
Daniel Vetter3671c582015-05-04 15:40:52 +02002160 if (plane == crtc->cursor)
2161 state->legacy_cursor_update = true;
2162
Daniel Vetter042652e2014-07-27 13:46:52 +02002163 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002164fail:
Chris Wilson08536952016-10-14 13:18:18 +01002165 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002166 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002167}
2168EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2169
2170/**
2171 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2172 * @plane: plane to disable
Daniel Vetter19315292017-03-22 22:50:43 +01002173 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002174 *
2175 * Provides a default plane disable handler using the atomic driver interface.
2176 *
2177 * RETURNS:
2178 * Zero on success, error code on failure
2179 */
Daniel Vetter19315292017-03-22 22:50:43 +01002180int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2181 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002182{
2183 struct drm_atomic_state *state;
2184 struct drm_plane_state *plane_state;
2185 int ret = 0;
2186
2187 state = drm_atomic_state_alloc(plane->dev);
2188 if (!state)
2189 return -ENOMEM;
2190
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002191 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002192 plane_state = drm_atomic_get_plane_state(state, plane);
2193 if (IS_ERR(plane_state)) {
2194 ret = PTR_ERR(plane_state);
2195 goto fail;
2196 }
2197
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002198 if (plane_state->crtc && (plane == plane->crtc->cursor))
2199 plane_state->state->legacy_cursor_update = true;
2200
Rob Clarkbbb1e522015-08-25 15:35:58 -04002201 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002202 if (ret != 0)
2203 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002204
Daniel Vetter042652e2014-07-27 13:46:52 +02002205 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002206fail:
Chris Wilson08536952016-10-14 13:18:18 +01002207 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002208 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002209}
2210EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2211
Rob Clarkbbb1e522015-08-25 15:35:58 -04002212/* just used from fb-helper and atomic-helper: */
2213int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2214 struct drm_plane_state *plane_state)
2215{
2216 int ret;
2217
2218 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2219 if (ret != 0)
2220 return ret;
2221
2222 drm_atomic_set_fb_for_plane(plane_state, NULL);
2223 plane_state->crtc_x = 0;
2224 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002225 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002226 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002227 plane_state->src_x = 0;
2228 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002229 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002230 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002231
Rob Clarkbbb1e522015-08-25 15:35:58 -04002232 return 0;
2233}
2234
Daniel Vetter042652e2014-07-27 13:46:52 +02002235static int update_output_state(struct drm_atomic_state *state,
2236 struct drm_mode_set *set)
2237{
2238 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002239 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002240 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002241 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002242 struct drm_connector_state *new_conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002243 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002244
2245 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2246 state->acquire_ctx);
2247 if (ret)
2248 return ret;
2249
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002250 /* First disable all connectors on the target crtc. */
2251 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2252 if (ret)
2253 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002254
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002255 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2256 if (new_conn_state->crtc == set->crtc) {
2257 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Daniel Vetter042652e2014-07-27 13:46:52 +02002258 NULL);
2259 if (ret)
2260 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002261
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002262 /* Make sure legacy setCrtc always re-trains */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002263 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
Daniel Vetter042652e2014-07-27 13:46:52 +02002264 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002265 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002266
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002267 /* Then set all connectors from set->connectors on the target crtc */
2268 for (i = 0; i < set->num_connectors; i++) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002269 new_conn_state = drm_atomic_get_connector_state(state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002270 set->connectors[i]);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002271 if (IS_ERR(new_conn_state))
2272 return PTR_ERR(new_conn_state);
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002273
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002274 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002275 set->crtc);
2276 if (ret)
2277 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002278 }
2279
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002280 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002281 /* Don't update ->enable for the CRTC in the set_config request,
2282 * since a mismatch would indicate a bug in the upper layers.
2283 * The actual modeset code later on will catch any
2284 * inconsistencies here. */
2285 if (crtc == set->crtc)
2286 continue;
2287
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002288 if (!new_crtc_state->connector_mask) {
2289 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002290 NULL);
2291 if (ret < 0)
2292 return ret;
2293
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002294 new_crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002295 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002296 }
2297
2298 return 0;
2299}
2300
2301/**
2302 * drm_atomic_helper_set_config - set a new config from userspace
2303 * @set: mode set configuration
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002304 * @ctx: lock acquisition context
Daniel Vetter042652e2014-07-27 13:46:52 +02002305 *
2306 * Provides a default crtc set_config handler using the atomic driver interface.
2307 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002308 * NOTE: For backwards compatibility with old userspace this automatically
2309 * resets the "link-status" property to GOOD, to force any link
2310 * re-training. The SETCRTC ioctl does not define whether an update does
2311 * need a full modeset or just a plane update, hence we're allowed to do
2312 * that. See also drm_mode_connector_set_link_status_property().
2313 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002314 * Returns:
2315 * Returns 0 on success, negative errno numbers on failure.
2316 */
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002317int drm_atomic_helper_set_config(struct drm_mode_set *set,
2318 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002319{
2320 struct drm_atomic_state *state;
2321 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002322 int ret = 0;
2323
2324 state = drm_atomic_state_alloc(crtc->dev);
2325 if (!state)
2326 return -ENOMEM;
2327
Daniel Vetter38b64412017-03-22 22:50:58 +01002328 state->acquire_ctx = ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002329 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002330 if (ret != 0)
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002331 goto fail;
Daniel Stone819364d2015-05-26 14:36:48 +01002332
Maarten Lankhorst44596b82017-04-06 13:19:00 +02002333 ret = handle_conflicting_encoders(state, true);
2334 if (ret)
2335 return ret;
2336
Daniel Vetter042652e2014-07-27 13:46:52 +02002337 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002338
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002339fail:
Chris Wilson08536952016-10-14 13:18:18 +01002340 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002341 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002342}
2343EXPORT_SYMBOL(drm_atomic_helper_set_config);
2344
Rob Clarkbbb1e522015-08-25 15:35:58 -04002345/* just used from fb-helper and atomic-helper: */
2346int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2347 struct drm_atomic_state *state)
2348{
2349 struct drm_crtc_state *crtc_state;
2350 struct drm_plane_state *primary_state;
2351 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002352 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002353 int ret;
2354
2355 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2356 if (IS_ERR(crtc_state))
2357 return PTR_ERR(crtc_state);
2358
2359 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2360 if (IS_ERR(primary_state))
2361 return PTR_ERR(primary_state);
2362
2363 if (!set->mode) {
2364 WARN_ON(set->fb);
2365 WARN_ON(set->num_connectors);
2366
2367 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2368 if (ret != 0)
2369 return ret;
2370
2371 crtc_state->active = false;
2372
2373 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2374 if (ret != 0)
2375 return ret;
2376
2377 drm_atomic_set_fb_for_plane(primary_state, NULL);
2378
2379 goto commit;
2380 }
2381
2382 WARN_ON(!set->fb);
2383 WARN_ON(!set->num_connectors);
2384
2385 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2386 if (ret != 0)
2387 return ret;
2388
2389 crtc_state->active = true;
2390
2391 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2392 if (ret != 0)
2393 return ret;
2394
Daniel Vetter196cd5d2017-01-25 07:26:56 +01002395 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Ville Syrjälä83926112015-11-16 17:02:34 +02002396
Rob Clarkbbb1e522015-08-25 15:35:58 -04002397 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2398 primary_state->crtc_x = 0;
2399 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02002400 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002401 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002402 primary_state->src_x = set->x << 16;
2403 primary_state->src_y = set->y << 16;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002404 if (drm_rotation_90_or_270(primary_state->rotation)) {
Ville Syrjälä83926112015-11-16 17:02:34 +02002405 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002406 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002407 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02002408 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002409 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002410 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04002411
2412commit:
2413 ret = update_output_state(state, set);
2414 if (ret)
2415 return ret;
2416
2417 return 0;
2418}
2419
Daniel Vetter042652e2014-07-27 13:46:52 +02002420/**
Thierry Reding14942762015-12-02 17:50:04 +01002421 * drm_atomic_helper_disable_all - disable all currently active outputs
2422 * @dev: DRM device
2423 * @ctx: lock acquisition context
2424 *
2425 * Loops through all connectors, finding those that aren't turned off and then
2426 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2427 * that they are connected to.
2428 *
2429 * This is used for example in suspend/resume to disable all currently active
Daniel Vetter18dddad2017-03-21 17:41:49 +01002430 * functions when suspending. If you just want to shut down everything at e.g.
2431 * driver unload, look at drm_atomic_helper_shutdown().
Thierry Reding14942762015-12-02 17:50:04 +01002432 *
2433 * Note that if callers haven't already acquired all modeset locks this might
2434 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2435 *
2436 * Returns:
2437 * 0 on success or a negative error code on failure.
2438 *
2439 * See also:
Daniel Vetter18dddad2017-03-21 17:41:49 +01002440 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
2441 * drm_atomic_helper_shutdown().
Thierry Reding14942762015-12-02 17:50:04 +01002442 */
2443int drm_atomic_helper_disable_all(struct drm_device *dev,
2444 struct drm_modeset_acquire_ctx *ctx)
2445{
2446 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002447 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01002448 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002449 struct drm_plane_state *plane_state;
2450 struct drm_plane *plane;
2451 struct drm_crtc_state *crtc_state;
2452 struct drm_crtc *crtc;
2453 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01002454
2455 state = drm_atomic_state_alloc(dev);
2456 if (!state)
2457 return -ENOMEM;
2458
2459 state->acquire_ctx = ctx;
2460
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002461 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01002462 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2463 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002464 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01002465 goto free;
2466 }
2467
2468 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002469
2470 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2471 if (ret < 0)
2472 goto free;
2473
2474 ret = drm_atomic_add_affected_planes(state, crtc);
2475 if (ret < 0)
2476 goto free;
2477
2478 ret = drm_atomic_add_affected_connectors(state, crtc);
2479 if (ret < 0)
2480 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01002481 }
2482
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002483 for_each_connector_in_state(state, conn, conn_state, i) {
2484 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2485 if (ret < 0)
2486 goto free;
2487 }
2488
2489 for_each_plane_in_state(state, plane, plane_state, i) {
2490 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2491 if (ret < 0)
2492 goto free;
2493
2494 drm_atomic_set_fb_for_plane(plane_state, NULL);
2495 }
2496
2497 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01002498free:
Chris Wilson08536952016-10-14 13:18:18 +01002499 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002500 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01002501}
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002502
Thierry Reding14942762015-12-02 17:50:04 +01002503EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2504
2505/**
Daniel Vetter18dddad2017-03-21 17:41:49 +01002506 * drm_atomic_helper_shutdown - shutdown all CRTC
2507 * @dev: DRM device
2508 *
2509 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
2510 * suspend should instead be handled with drm_atomic_helper_suspend(), since
2511 * that also takes a snapshot of the modeset state to be restored on resume.
2512 *
2513 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
2514 * and it is the atomic version of drm_crtc_force_disable_all().
2515 */
2516void drm_atomic_helper_shutdown(struct drm_device *dev)
2517{
2518 struct drm_modeset_acquire_ctx ctx;
2519 int ret;
2520
2521 drm_modeset_acquire_init(&ctx, 0);
2522 while (1) {
2523 ret = drm_modeset_lock_all_ctx(dev, &ctx);
2524 if (!ret)
2525 ret = drm_atomic_helper_disable_all(dev, &ctx);
2526
2527 if (ret != -EDEADLK)
2528 break;
2529
2530 drm_modeset_backoff(&ctx);
2531 }
2532
2533 if (ret)
2534 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
2535
2536 drm_modeset_drop_locks(&ctx);
2537 drm_modeset_acquire_fini(&ctx);
2538}
2539EXPORT_SYMBOL(drm_atomic_helper_shutdown);
2540
2541/**
Thierry Reding14942762015-12-02 17:50:04 +01002542 * drm_atomic_helper_suspend - subsystem-level suspend helper
2543 * @dev: DRM device
2544 *
2545 * Duplicates the current atomic state, disables all active outputs and then
2546 * returns a pointer to the original atomic state to the caller. Drivers can
2547 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2548 * restore the output configuration that was active at the time the system
2549 * entered suspend.
2550 *
2551 * Note that it is potentially unsafe to use this. The atomic state object
2552 * returned by this function is assumed to be persistent. Drivers must ensure
2553 * that this holds true. Before calling this function, drivers must make sure
2554 * to suspend fbdev emulation so that nothing can be using the device.
2555 *
2556 * Returns:
2557 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2558 * encoded error code on failure. Drivers should store the returned atomic
2559 * state object and pass it to the drm_atomic_helper_resume() helper upon
2560 * resume.
2561 *
2562 * See also:
2563 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002564 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01002565 */
2566struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2567{
2568 struct drm_modeset_acquire_ctx ctx;
2569 struct drm_atomic_state *state;
2570 int err;
2571
2572 drm_modeset_acquire_init(&ctx, 0);
2573
2574retry:
2575 err = drm_modeset_lock_all_ctx(dev, &ctx);
2576 if (err < 0) {
2577 state = ERR_PTR(err);
2578 goto unlock;
2579 }
2580
2581 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2582 if (IS_ERR(state))
2583 goto unlock;
2584
2585 err = drm_atomic_helper_disable_all(dev, &ctx);
2586 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01002587 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01002588 state = ERR_PTR(err);
2589 goto unlock;
2590 }
2591
2592unlock:
2593 if (PTR_ERR(state) == -EDEADLK) {
2594 drm_modeset_backoff(&ctx);
2595 goto retry;
2596 }
2597
2598 drm_modeset_drop_locks(&ctx);
2599 drm_modeset_acquire_fini(&ctx);
2600 return state;
2601}
2602EXPORT_SYMBOL(drm_atomic_helper_suspend);
2603
2604/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002605 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2606 * @state: duplicated atomic state to commit
2607 * @ctx: pointer to acquire_ctx to use for commit.
2608 *
2609 * The state returned by drm_atomic_helper_duplicate_state() and
2610 * drm_atomic_helper_suspend() is partially invalid, and needs to
2611 * be fixed up before commit.
2612 *
2613 * Returns:
2614 * 0 on success or a negative error code on failure.
2615 *
2616 * See also:
2617 * drm_atomic_helper_suspend()
2618 */
2619int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2620 struct drm_modeset_acquire_ctx *ctx)
2621{
2622 int i;
2623 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002624 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002625 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002626 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002627 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002628 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002629
2630 state->acquire_ctx = ctx;
2631
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002632 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002633 state->planes[i].old_state = plane->state;
2634
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002635 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002636 state->crtcs[i].old_state = crtc->state;
2637
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002638 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002639 state->connectors[i].old_state = connector->state;
2640
2641 return drm_atomic_commit(state);
2642}
2643EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2644
2645/**
Thierry Reding14942762015-12-02 17:50:04 +01002646 * drm_atomic_helper_resume - subsystem-level resume helper
2647 * @dev: DRM device
2648 * @state: atomic state to resume to
2649 *
2650 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2651 * grabs all modeset locks and commits the atomic state object. This can be
2652 * used in conjunction with the drm_atomic_helper_suspend() helper to
2653 * implement suspend/resume for drivers that support atomic mode-setting.
2654 *
2655 * Returns:
2656 * 0 on success or a negative error code on failure.
2657 *
2658 * See also:
2659 * drm_atomic_helper_suspend()
2660 */
2661int drm_atomic_helper_resume(struct drm_device *dev,
2662 struct drm_atomic_state *state)
2663{
Daniel Vettera5b84442017-04-03 10:32:53 +02002664 struct drm_modeset_acquire_ctx ctx;
Thierry Reding14942762015-12-02 17:50:04 +01002665 int err;
2666
2667 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002668
Daniel Vettera5b84442017-04-03 10:32:53 +02002669 drm_modeset_acquire_init(&ctx, 0);
2670 while (1) {
2671 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
2672 if (err != -EDEADLK)
2673 break;
2674
2675 drm_modeset_backoff(&ctx);
2676 }
2677
2678 drm_modeset_drop_locks(&ctx);
2679 drm_modeset_acquire_fini(&ctx);
Thierry Reding14942762015-12-02 17:50:04 +01002680
2681 return err;
2682}
2683EXPORT_SYMBOL(drm_atomic_helper_resume);
2684
2685/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002686 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002687 * @crtc: DRM crtc
2688 * @property: DRM property
2689 * @val: value of property
2690 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002691 * Provides a default crtc set_property handler using the atomic driver
2692 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002693 *
2694 * RETURNS:
2695 * Zero on success, error code on failure
2696 */
2697int
2698drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2699 struct drm_property *property,
2700 uint64_t val)
2701{
2702 struct drm_atomic_state *state;
2703 struct drm_crtc_state *crtc_state;
2704 int ret = 0;
2705
2706 state = drm_atomic_state_alloc(crtc->dev);
2707 if (!state)
2708 return -ENOMEM;
2709
2710 /* ->set_property is always called with all locks held. */
2711 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2712retry:
2713 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2714 if (IS_ERR(crtc_state)) {
2715 ret = PTR_ERR(crtc_state);
2716 goto fail;
2717 }
2718
Rob Clark40ecc692014-12-18 16:01:46 -05002719 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2720 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002721 if (ret)
2722 goto fail;
2723
2724 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002725fail:
2726 if (ret == -EDEADLK)
2727 goto backoff;
2728
Chris Wilson08536952016-10-14 13:18:18 +01002729 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002730 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002731
Daniel Vetter042652e2014-07-27 13:46:52 +02002732backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002733 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002734 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002735
2736 goto retry;
2737}
2738EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2739
2740/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002741 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002742 * @plane: DRM plane
2743 * @property: DRM property
2744 * @val: value of property
2745 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002746 * Provides a default plane set_property handler using the atomic driver
2747 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002748 *
2749 * RETURNS:
2750 * Zero on success, error code on failure
2751 */
2752int
2753drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2754 struct drm_property *property,
2755 uint64_t val)
2756{
2757 struct drm_atomic_state *state;
2758 struct drm_plane_state *plane_state;
2759 int ret = 0;
2760
2761 state = drm_atomic_state_alloc(plane->dev);
2762 if (!state)
2763 return -ENOMEM;
2764
2765 /* ->set_property is always called with all locks held. */
2766 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2767retry:
2768 plane_state = drm_atomic_get_plane_state(state, plane);
2769 if (IS_ERR(plane_state)) {
2770 ret = PTR_ERR(plane_state);
2771 goto fail;
2772 }
2773
Rob Clark40ecc692014-12-18 16:01:46 -05002774 ret = drm_atomic_plane_set_property(plane, plane_state,
2775 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002776 if (ret)
2777 goto fail;
2778
2779 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002780fail:
2781 if (ret == -EDEADLK)
2782 goto backoff;
2783
Chris Wilson08536952016-10-14 13:18:18 +01002784 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002785 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002786
Daniel Vetter042652e2014-07-27 13:46:52 +02002787backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002788 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002789 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002790
2791 goto retry;
2792}
2793EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2794
2795/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002796 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002797 * @connector: DRM connector
2798 * @property: DRM property
2799 * @val: value of property
2800 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002801 * Provides a default connector set_property handler using the atomic driver
2802 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002803 *
2804 * RETURNS:
2805 * Zero on success, error code on failure
2806 */
2807int
2808drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2809 struct drm_property *property,
2810 uint64_t val)
2811{
2812 struct drm_atomic_state *state;
2813 struct drm_connector_state *connector_state;
2814 int ret = 0;
2815
2816 state = drm_atomic_state_alloc(connector->dev);
2817 if (!state)
2818 return -ENOMEM;
2819
2820 /* ->set_property is always called with all locks held. */
2821 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2822retry:
2823 connector_state = drm_atomic_get_connector_state(state, connector);
2824 if (IS_ERR(connector_state)) {
2825 ret = PTR_ERR(connector_state);
2826 goto fail;
2827 }
2828
Rob Clark40ecc692014-12-18 16:01:46 -05002829 ret = drm_atomic_connector_set_property(connector, connector_state,
2830 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002831 if (ret)
2832 goto fail;
2833
2834 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002835fail:
2836 if (ret == -EDEADLK)
2837 goto backoff;
2838
Chris Wilson08536952016-10-14 13:18:18 +01002839 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002840 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002841
Daniel Vetter042652e2014-07-27 13:46:52 +02002842backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002843 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002844 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002845
2846 goto retry;
2847}
2848EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002849
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002850static int page_flip_common(
2851 struct drm_atomic_state *state,
2852 struct drm_crtc *crtc,
2853 struct drm_framebuffer *fb,
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002854 struct drm_pending_vblank_event *event,
2855 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002856{
2857 struct drm_plane *plane = crtc->primary;
2858 struct drm_plane_state *plane_state;
2859 struct drm_crtc_state *crtc_state;
2860 int ret = 0;
2861
2862 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2863 if (IS_ERR(crtc_state))
2864 return PTR_ERR(crtc_state);
2865
2866 crtc_state->event = event;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002867 crtc_state->pageflip_flags = flags;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002868
2869 plane_state = drm_atomic_get_plane_state(state, plane);
2870 if (IS_ERR(plane_state))
2871 return PTR_ERR(plane_state);
2872
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002873 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2874 if (ret != 0)
2875 return ret;
2876 drm_atomic_set_fb_for_plane(plane_state, fb);
2877
2878 /* Make sure we don't accidentally do a full modeset. */
2879 state->allow_modeset = false;
2880 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00002881 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
2882 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002883 return -EINVAL;
2884 }
2885
2886 return ret;
2887}
2888
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002889/**
2890 * drm_atomic_helper_page_flip - execute a legacy page flip
2891 * @crtc: DRM crtc
2892 * @fb: DRM framebuffer
2893 * @event: optional DRM event to signal upon completion
2894 * @flags: flip flags for non-vblank sync'ed updates
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002895 * @ctx: lock acquisition context
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002896 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002897 * Provides a default &drm_crtc_funcs.page_flip implementation
2898 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002899 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002900 * Returns:
2901 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002902 *
2903 * See also:
2904 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002905 */
2906int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2907 struct drm_framebuffer *fb,
2908 struct drm_pending_vblank_event *event,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002909 uint32_t flags,
2910 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002911{
2912 struct drm_plane *plane = crtc->primary;
2913 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002914 int ret = 0;
2915
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002916 state = drm_atomic_state_alloc(plane->dev);
2917 if (!state)
2918 return -ENOMEM;
2919
Daniel Vetter043e7fb2017-03-22 22:50:51 +01002920 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002921
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002922 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002923 if (ret != 0)
2924 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01002925
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002926 ret = drm_atomic_nonblocking_commit(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002927fail:
Chris Wilson08536952016-10-14 13:18:18 +01002928 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002929 return ret;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002930}
2931EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002932
2933/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002934 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2935 * @crtc: DRM crtc
2936 * @fb: DRM framebuffer
2937 * @event: optional DRM event to signal upon completion
2938 * @flags: flip flags for non-vblank sync'ed updates
2939 * @target: specifying the target vblank period when the flip to take effect
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002940 * @ctx: lock acquisition context
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002941 *
2942 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2943 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2944 * target vblank period to flip.
2945 *
2946 * Returns:
2947 * Returns 0 on success, negative errno numbers on failure.
2948 */
2949int drm_atomic_helper_page_flip_target(
2950 struct drm_crtc *crtc,
2951 struct drm_framebuffer *fb,
2952 struct drm_pending_vblank_event *event,
2953 uint32_t flags,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01002954 uint32_t target,
2955 struct drm_modeset_acquire_ctx *ctx)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002956{
2957 struct drm_plane *plane = crtc->primary;
2958 struct drm_atomic_state *state;
2959 struct drm_crtc_state *crtc_state;
2960 int ret = 0;
2961
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002962 state = drm_atomic_state_alloc(plane->dev);
2963 if (!state)
2964 return -ENOMEM;
2965
Daniel Vetter043e7fb2017-03-22 22:50:51 +01002966 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002967
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002968 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002969 if (ret != 0)
2970 goto fail;
2971
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01002972 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002973 if (WARN_ON(!crtc_state)) {
2974 ret = -EINVAL;
2975 goto fail;
2976 }
2977 crtc_state->target_vblank = target;
2978
2979 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002980fail:
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002981 drm_atomic_state_put(state);
2982 return ret;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002983}
2984EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2985
2986/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002987 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2988 * @connector: affected connector
2989 * @mode: DPMS mode
2990 *
2991 * This is the main helper function provided by the atomic helper framework for
2992 * implementing the legacy DPMS connector interface. It computes the new desired
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002993 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
2994 * enabled) and updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002995 *
2996 * Returns:
2997 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002998 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002999int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
3000 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003001{
3002 struct drm_mode_config *config = &connector->dev->mode_config;
3003 struct drm_atomic_state *state;
3004 struct drm_crtc_state *crtc_state;
3005 struct drm_crtc *crtc;
3006 struct drm_connector *tmp_connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003007 struct drm_connector_list_iter conn_iter;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003008 int ret;
3009 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003010 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003011
3012 if (mode != DRM_MODE_DPMS_ON)
3013 mode = DRM_MODE_DPMS_OFF;
3014
3015 connector->dpms = mode;
3016 crtc = connector->state->crtc;
3017
3018 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003019 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003020
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003021 state = drm_atomic_state_alloc(connector->dev);
3022 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003023 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003024
Daniel Vetterb260ac32017-04-03 10:32:52 +02003025 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003026retry:
3027 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003028 if (IS_ERR(crtc_state)) {
3029 ret = PTR_ERR(crtc_state);
3030 goto fail;
3031 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003032
3033 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
3034
Thierry Redingb982dab2017-02-28 15:46:43 +01003035 drm_connector_list_iter_begin(connector->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003036 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
John Hunter0388df02015-03-17 15:30:28 +08003037 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003038 continue;
3039
John Hunter0388df02015-03-17 15:30:28 +08003040 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003041 active = true;
3042 break;
3043 }
3044 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003045 drm_connector_list_iter_end(&conn_iter);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003046 crtc_state->active = active;
3047
3048 ret = drm_atomic_commit(state);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003049fail:
3050 if (ret == -EDEADLK)
3051 goto backoff;
Marta Lofstedt8f5040e2016-12-05 14:04:08 +02003052 if (ret != 0)
3053 connector->dpms = old_mode;
Chris Wilson08536952016-10-14 13:18:18 +01003054 drm_atomic_state_put(state);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003055 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003056
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003057backoff:
3058 drm_atomic_state_clear(state);
3059 drm_atomic_legacy_backoff(state);
3060
3061 goto retry;
3062}
3063EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3064
3065/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003066 * drm_atomic_helper_best_encoder - Helper for
3067 * &drm_connector_helper_funcs.best_encoder callback
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003068 * @connector: Connector control structure
3069 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003070 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003071 * connectors that support exactly 1 encoder, statically determined at driver
3072 * init time.
3073 */
3074struct drm_encoder *
3075drm_atomic_helper_best_encoder(struct drm_connector *connector)
3076{
3077 WARN_ON(connector->encoder_ids[1]);
3078 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3079}
3080EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3081
3082/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003083 * DOC: atomic state reset and initialization
3084 *
3085 * Both the drm core and the atomic helpers assume that there is always the full
3086 * and correct atomic software state for all connectors, CRTCs and planes
3087 * available. Which is a bit a problem on driver load and also after system
3088 * suspend. One way to solve this is to have a hardware state read-out
3089 * infrastructure which reconstructs the full software state (e.g. the i915
3090 * driver).
3091 *
3092 * The simpler solution is to just reset the software state to everything off,
3093 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3094 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01003095 *
3096 * On the upside the precise state tracking of atomic simplifies system suspend
3097 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3098 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3099 * For other drivers the building blocks are split out, see the documentation
3100 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003101 */
3102
3103/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003104 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
Daniel Vetterd4617012014-11-03 15:56:43 +01003105 * @crtc: drm CRTC
3106 *
3107 * Resets the atomic state for @crtc by freeing the state pointer (which might
3108 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3109 */
3110void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3111{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003112 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003113 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003114
Daniel Vetterd4617012014-11-03 15:56:43 +01003115 kfree(crtc->state);
3116 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003117
3118 if (crtc->state)
3119 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01003120}
3121EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3122
3123/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003124 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3125 * @crtc: CRTC object
3126 * @state: atomic CRTC state
3127 *
3128 * Copies atomic state from a CRTC's current state and resets inferred values.
3129 * This is useful for drivers that subclass the CRTC state.
3130 */
3131void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3132 struct drm_crtc_state *state)
3133{
3134 memcpy(state, crtc->state, sizeof(*state));
3135
Daniel Stone99cf4a22015-05-25 19:11:51 +01003136 if (state->mode_blob)
Thierry Reding6472e502017-02-28 15:46:42 +01003137 drm_property_blob_get(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003138 if (state->degamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003139 drm_property_blob_get(state->degamma_lut);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003140 if (state->ctm)
Thierry Reding6472e502017-02-28 15:46:42 +01003141 drm_property_blob_get(state->ctm);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003142 if (state->gamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003143 drm_property_blob_get(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003144 state->mode_changed = false;
3145 state->active_changed = false;
3146 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02003147 state->connectors_changed = false;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003148 state->color_mgmt_changed = false;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02003149 state->zpos_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01003150 state->event = NULL;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003151 state->pageflip_flags = 0;
Thierry Redingf5e78402015-01-28 14:54:32 +01003152}
3153EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3154
3155/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003156 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3157 * @crtc: drm CRTC
3158 *
3159 * Default CRTC state duplicate hook for drivers which don't have their own
3160 * subclassed CRTC state structure.
3161 */
3162struct drm_crtc_state *
3163drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3164{
3165 struct drm_crtc_state *state;
3166
3167 if (WARN_ON(!crtc->state))
3168 return NULL;
3169
Thierry Redingf5e78402015-01-28 14:54:32 +01003170 state = kmalloc(sizeof(*state), GFP_KERNEL);
3171 if (state)
3172 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003173
3174 return state;
3175}
3176EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3177
3178/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003179 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
Thierry Redingf5e78402015-01-28 14:54:32 +01003180 * @state: CRTC state object to release
3181 *
3182 * Releases all resources stored in the CRTC state without actually freeing
3183 * the memory of the CRTC state. This is useful for drivers that subclass the
3184 * CRTC state.
3185 */
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003186void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003187{
Thierry Reding6472e502017-02-28 15:46:42 +01003188 drm_property_blob_put(state->mode_blob);
3189 drm_property_blob_put(state->degamma_lut);
3190 drm_property_blob_put(state->ctm);
3191 drm_property_blob_put(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003192}
3193EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3194
3195/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003196 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3197 * @crtc: drm CRTC
3198 * @state: CRTC state object to release
3199 *
3200 * Default CRTC state destroy hook for drivers which don't have their own
3201 * subclassed CRTC state structure.
3202 */
3203void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3204 struct drm_crtc_state *state)
3205{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003206 __drm_atomic_helper_crtc_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003207 kfree(state);
3208}
3209EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3210
3211/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003212 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
Daniel Vetterd4617012014-11-03 15:56:43 +01003213 * @plane: drm plane
3214 *
3215 * Resets the atomic state for @plane by freeing the state pointer (which might
3216 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3217 */
3218void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3219{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003220 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +02003221 __drm_atomic_helper_plane_destroy_state(plane->state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003222
Daniel Vetterd4617012014-11-03 15:56:43 +01003223 kfree(plane->state);
3224 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003225
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003226 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003227 plane->state->plane = plane;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003228 plane->state->rotation = DRM_ROTATE_0;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003229 }
Daniel Vetterd4617012014-11-03 15:56:43 +01003230}
3231EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3232
3233/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003234 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3235 * @plane: plane object
3236 * @state: atomic plane state
3237 *
3238 * Copies atomic state from a plane's current state. This is useful for
3239 * drivers that subclass the plane state.
3240 */
3241void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3242 struct drm_plane_state *state)
3243{
3244 memcpy(state, plane->state, sizeof(*state));
3245
3246 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003247 drm_framebuffer_get(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003248
3249 state->fence = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003250}
3251EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3252
3253/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003254 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3255 * @plane: drm plane
3256 *
3257 * Default plane state duplicate hook for drivers which don't have their own
3258 * subclassed plane state structure.
3259 */
3260struct drm_plane_state *
3261drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3262{
Daniel Vetter321ebf02014-11-04 22:57:27 +01003263 struct drm_plane_state *state;
3264
Daniel Vetterd4617012014-11-03 15:56:43 +01003265 if (WARN_ON(!plane->state))
3266 return NULL;
3267
Thierry Redingf5e78402015-01-28 14:54:32 +01003268 state = kmalloc(sizeof(*state), GFP_KERNEL);
3269 if (state)
3270 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003271
3272 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003273}
3274EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3275
3276/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003277 * __drm_atomic_helper_plane_destroy_state - release plane state
Thierry Redingf5e78402015-01-28 14:54:32 +01003278 * @state: plane state object to release
3279 *
3280 * Releases all resources stored in the plane state without actually freeing
3281 * the memory of the plane state. This is useful for drivers that subclass the
3282 * plane state.
3283 */
Daniel Vetter2f701692016-05-09 16:34:10 +02003284void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003285{
3286 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003287 drm_framebuffer_put(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003288
3289 if (state->fence)
3290 dma_fence_put(state->fence);
Thierry Redingf5e78402015-01-28 14:54:32 +01003291}
3292EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3293
3294/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003295 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3296 * @plane: drm plane
3297 * @state: plane state object to release
3298 *
3299 * Default plane state destroy hook for drivers which don't have their own
3300 * subclassed plane state structure.
3301 */
3302void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01003303 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01003304{
Daniel Vetter2f701692016-05-09 16:34:10 +02003305 __drm_atomic_helper_plane_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003306 kfree(state);
3307}
3308EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3309
3310/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003311 * __drm_atomic_helper_connector_reset - reset state on connector
3312 * @connector: drm connector
3313 * @conn_state: connector state to assign
3314 *
3315 * Initializes the newly allocated @conn_state and assigns it to
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003316 * the &drm_conector->state pointer of @connector, usually required when
3317 * initializing the drivers or when called from the &drm_connector_funcs.reset
3318 * hook.
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003319 *
3320 * This is useful for drivers that subclass the connector state.
3321 */
3322void
3323__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3324 struct drm_connector_state *conn_state)
3325{
3326 if (conn_state)
3327 conn_state->connector = connector;
3328
3329 connector->state = conn_state;
3330}
3331EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3332
3333/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003334 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
Daniel Vetterd4617012014-11-03 15:56:43 +01003335 * @connector: drm connector
3336 *
3337 * Resets the atomic state for @connector by freeing the state pointer (which
3338 * might be NULL, e.g. at driver load time) and allocating a new empty state
3339 * object.
3340 */
3341void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3342{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003343 struct drm_connector_state *conn_state =
3344 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003345
Daniel Vetterb0b55112016-04-22 22:10:29 +02003346 if (connector->state)
Daniel Vetterfabd9102016-05-09 16:34:11 +02003347 __drm_atomic_helper_connector_destroy_state(connector->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003348
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003349 kfree(connector->state);
3350 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003351}
3352EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3353
3354/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003355 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3356 * @connector: connector object
3357 * @state: atomic connector state
3358 *
3359 * Copies atomic state from a connector's current state. This is useful for
3360 * drivers that subclass the connector state.
3361 */
3362void
3363__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3364 struct drm_connector_state *state)
3365{
3366 memcpy(state, connector->state, sizeof(*state));
Dave Airlied2307de2016-04-27 11:27:39 +10003367 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003368 drm_connector_get(connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003369}
3370EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3371
3372/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003373 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3374 * @connector: drm connector
3375 *
3376 * Default connector state duplicate hook for drivers which don't have their own
3377 * subclassed connector state structure.
3378 */
3379struct drm_connector_state *
3380drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3381{
Thierry Redingf5e78402015-01-28 14:54:32 +01003382 struct drm_connector_state *state;
3383
Daniel Vetterd4617012014-11-03 15:56:43 +01003384 if (WARN_ON(!connector->state))
3385 return NULL;
3386
Thierry Redingf5e78402015-01-28 14:54:32 +01003387 state = kmalloc(sizeof(*state), GFP_KERNEL);
3388 if (state)
3389 __drm_atomic_helper_connector_duplicate_state(connector, state);
3390
3391 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003392}
3393EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3394
3395/**
Thierry Reding397fd772015-09-08 15:00:45 +02003396 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3397 * @dev: DRM device
3398 * @ctx: lock acquisition context
3399 *
3400 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01003401 * duplicating their respective states. This is used for example by suspend/
3402 * resume support code to save the state prior to suspend such that it can
3403 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02003404 *
3405 * Note that this treats atomic state as persistent between save and restore.
3406 * Drivers must make sure that this is possible and won't result in confusion
3407 * or erroneous behaviour.
3408 *
3409 * Note that if callers haven't already acquired all modeset locks this might
3410 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3411 *
3412 * Returns:
3413 * A pointer to the copy of the atomic state object on success or an
3414 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01003415 *
3416 * See also:
3417 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02003418 */
3419struct drm_atomic_state *
3420drm_atomic_helper_duplicate_state(struct drm_device *dev,
3421 struct drm_modeset_acquire_ctx *ctx)
3422{
3423 struct drm_atomic_state *state;
3424 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003425 struct drm_connector_list_iter conn_iter;
Thierry Reding397fd772015-09-08 15:00:45 +02003426 struct drm_plane *plane;
3427 struct drm_crtc *crtc;
3428 int err = 0;
3429
3430 state = drm_atomic_state_alloc(dev);
3431 if (!state)
3432 return ERR_PTR(-ENOMEM);
3433
3434 state->acquire_ctx = ctx;
3435
3436 drm_for_each_crtc(crtc, dev) {
3437 struct drm_crtc_state *crtc_state;
3438
3439 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3440 if (IS_ERR(crtc_state)) {
3441 err = PTR_ERR(crtc_state);
3442 goto free;
3443 }
3444 }
3445
3446 drm_for_each_plane(plane, dev) {
3447 struct drm_plane_state *plane_state;
3448
3449 plane_state = drm_atomic_get_plane_state(state, plane);
3450 if (IS_ERR(plane_state)) {
3451 err = PTR_ERR(plane_state);
3452 goto free;
3453 }
3454 }
3455
Thierry Redingb982dab2017-02-28 15:46:43 +01003456 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003457 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding397fd772015-09-08 15:00:45 +02003458 struct drm_connector_state *conn_state;
3459
3460 conn_state = drm_atomic_get_connector_state(state, conn);
3461 if (IS_ERR(conn_state)) {
3462 err = PTR_ERR(conn_state);
Thierry Redingb982dab2017-02-28 15:46:43 +01003463 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003464 goto free;
3465 }
3466 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003467 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003468
3469 /* clear the acquire context so that it isn't accidentally reused */
3470 state->acquire_ctx = NULL;
3471
3472free:
3473 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003474 drm_atomic_state_put(state);
Thierry Reding397fd772015-09-08 15:00:45 +02003475 state = ERR_PTR(err);
3476 }
3477
3478 return state;
3479}
3480EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3481
3482/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003483 * __drm_atomic_helper_connector_destroy_state - release connector state
Thierry Redingf5e78402015-01-28 14:54:32 +01003484 * @state: connector state object to release
3485 *
3486 * Releases all resources stored in the connector state without actually
3487 * freeing the memory of the connector state. This is useful for drivers that
3488 * subclass the connector state.
3489 */
3490void
Daniel Vetterfabd9102016-05-09 16:34:11 +02003491__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003492{
Dave Airlied2307de2016-04-27 11:27:39 +10003493 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003494 drm_connector_put(state->connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003495}
3496EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3497
3498/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003499 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3500 * @connector: drm connector
3501 * @state: connector state object to release
3502 *
3503 * Default connector state destroy hook for drivers which don't have their own
3504 * subclassed connector state structure.
3505 */
3506void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3507 struct drm_connector_state *state)
3508{
Daniel Vetterfabd9102016-05-09 16:34:11 +02003509 __drm_atomic_helper_connector_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003510 kfree(state);
3511}
3512EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003513
3514/**
3515 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3516 * @crtc: CRTC object
3517 * @red: red correction table
3518 * @green: green correction table
3519 * @blue: green correction table
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003520 * @size: size of the tables
Daniel Vetter6d124ff2017-04-03 10:33:01 +02003521 * @ctx: lock acquire context
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003522 *
3523 * Implements support for legacy gamma correction table for drivers
3524 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
Daniel Vetter2e381782017-04-12 17:20:06 +02003525 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3526 * how the atomic color management and gamma tables work.
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003527 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003528int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3529 u16 *red, u16 *green, u16 *blue,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02003530 uint32_t size,
3531 struct drm_modeset_acquire_ctx *ctx)
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003532{
3533 struct drm_device *dev = crtc->dev;
3534 struct drm_mode_config *config = &dev->mode_config;
3535 struct drm_atomic_state *state;
3536 struct drm_crtc_state *crtc_state;
3537 struct drm_property_blob *blob = NULL;
3538 struct drm_color_lut *blob_data;
3539 int i, ret = 0;
3540
3541 state = drm_atomic_state_alloc(crtc->dev);
3542 if (!state)
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003543 return -ENOMEM;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003544
3545 blob = drm_property_create_blob(dev,
3546 sizeof(struct drm_color_lut) * size,
3547 NULL);
Lionel Landwerlin562c5b42016-03-10 12:04:21 +00003548 if (IS_ERR(blob)) {
3549 ret = PTR_ERR(blob);
Lionel Landwerlinc1f415c2016-03-11 12:17:26 +00003550 blob = NULL;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003551 goto fail;
3552 }
3553
3554 /* Prepare GAMMA_LUT with the legacy values. */
3555 blob_data = (struct drm_color_lut *) blob->data;
3556 for (i = 0; i < size; i++) {
3557 blob_data[i].red = red[i];
3558 blob_data[i].green = green[i];
3559 blob_data[i].blue = blue[i];
3560 }
3561
Daniel Vetter3a09f732017-04-03 10:33:02 +02003562 state->acquire_ctx = ctx;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003563 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3564 if (IS_ERR(crtc_state)) {
3565 ret = PTR_ERR(crtc_state);
3566 goto fail;
3567 }
3568
3569 /* Reset DEGAMMA_LUT and CTM properties. */
3570 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3571 config->degamma_lut_property, 0);
3572 if (ret)
3573 goto fail;
3574
3575 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3576 config->ctm_property, 0);
3577 if (ret)
3578 goto fail;
3579
3580 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3581 config->gamma_lut_property, blob->base.id);
3582 if (ret)
3583 goto fail;
3584
3585 ret = drm_atomic_commit(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003586
Daniel Vetter3a09f732017-04-03 10:33:02 +02003587fail:
Chris Wilson08536952016-10-14 13:18:18 +01003588 drm_atomic_state_put(state);
Thierry Reding6472e502017-02-28 15:46:42 +01003589 drm_property_blob_put(blob);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003590 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003591}
3592EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);