blob: 6096233a740cd56e252f02834f320880c2a405e0 [file] [log] [blame]
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28#include <drm/drmP.h>
29#include <drm/drm_atomic.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020032#include <drm/drm_atomic_helper.h>
Chris Wilsonf54d1862016-10-25 13:00:45 +010033#include <linux/dma-fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020035#include "drm_crtc_internal.h"
36
Daniel Vetter3150c7d2014-11-06 20:53:29 +010037/**
38 * DOC: overview
39 *
40 * This helper library provides implementations of check and commit functions on
41 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
42 * also provides convenience implementations for the atomic state handling
43 * callbacks for drivers which don't need to subclass the drm core structures to
44 * add their own additional internal state.
45 *
46 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020047 * drm_atomic_helper_check() and for the commit callback with
48 * drm_atomic_helper_commit(). But the individual stages and callbacks are
49 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010050 * together with a driver private modeset implementation.
51 *
52 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020053 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
54 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010055 * various functions to implement set_property callbacks. New drivers must not
56 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010057 *
58 * The atomic helper uses the same function table structures as all other
Daniel Vetterea0dd852016-12-29 21:48:26 +010059 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
60 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
61 * also shares the &struct drm_plane_helper_funcs function table with the plane
Daniel Vetter092d01d2015-12-04 09:45:44 +010062 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010063 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010064static void
65drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010066 struct drm_plane_state *old_plane_state,
Daniel Vetterc2fcd272014-11-05 00:14:14 +010067 struct drm_plane_state *plane_state,
68 struct drm_plane *plane)
69{
70 struct drm_crtc_state *crtc_state;
71
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010072 if (old_plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010073 crtc_state = drm_atomic_get_new_crtc_state(state,
74 old_plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010075
76 if (WARN_ON(!crtc_state))
77 return;
78
79 crtc_state->planes_changed = true;
80 }
81
82 if (plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010083 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010084
85 if (WARN_ON(!crtc_state))
86 return;
87
88 crtc_state->planes_changed = true;
89 }
90}
91
Maarten Lankhorst8248b652016-03-03 10:17:40 +010092static int handle_conflicting_encoders(struct drm_atomic_state *state,
93 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +010094{
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010095 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +020096 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +010097 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst40616a22016-03-03 10:17:39 +010098 struct drm_encoder *encoder;
99 unsigned encoder_mask = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100100 int i, ret = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200101
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100102 /*
103 * First loop, find all newly assigned encoders from the connectors
104 * part of the state. If the same encoder is assigned to multiple
105 * connectors bail out.
106 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100107 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100108 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
109 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200110
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100111 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200112 continue;
113
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100114 if (funcs->atomic_best_encoder)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100115 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200116 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100117 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200118 else
119 new_encoder = drm_atomic_helper_best_encoder(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100120
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100121 if (new_encoder) {
122 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
123 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
124 new_encoder->base.id, new_encoder->name,
125 connector->base.id, connector->name);
126
127 return -EINVAL;
128 }
129
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100130 encoder_mask |= 1 << drm_encoder_index(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100131 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200132 }
133
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100134 if (!encoder_mask)
135 return 0;
136
137 /*
138 * Second loop, iterate over all connectors not part of the state.
139 *
140 * If a conflicting encoder is found and disable_conflicting_encoders
141 * is not set, an error is returned. Userspace can provide a solution
142 * through the atomic ioctl.
143 *
144 * If the flag is set conflicting connectors are removed from the crtc
145 * and the crtc is disabled if no encoder is left. This preserves
146 * compatibility with the legacy set_config behavior.
147 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100148 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100149 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100150 struct drm_crtc_state *crtc_state;
151
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100152 if (drm_atomic_get_new_connector_state(state, connector))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100153 continue;
154
155 encoder = connector->state->best_encoder;
156 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
157 continue;
158
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100159 if (!disable_conflicting_encoders) {
160 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
161 encoder->base.id, encoder->name,
162 connector->state->crtc->base.id,
163 connector->state->crtc->name,
164 connector->base.id, connector->name);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100165 ret = -EINVAL;
166 goto out;
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100167 }
168
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100169 new_conn_state = drm_atomic_get_connector_state(state, connector);
170 if (IS_ERR(new_conn_state)) {
171 ret = PTR_ERR(new_conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100172 goto out;
173 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100174
175 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
176 encoder->base.id, encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100177 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100178 connector->base.id, connector->name);
179
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100180 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100181
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100182 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100183 if (ret)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100184 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100185
186 if (!crtc_state->connector_mask) {
187 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
188 NULL);
189 if (ret < 0)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100190 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100191
192 crtc_state->active = false;
193 }
194 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100195out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100196 drm_connector_list_iter_end(&conn_iter);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100197
Daniel Vetterc36a3252016-12-15 16:58:43 +0100198 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200199}
200
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100201static void
202set_best_encoder(struct drm_atomic_state *state,
203 struct drm_connector_state *conn_state,
204 struct drm_encoder *encoder)
205{
206 struct drm_crtc_state *crtc_state;
207 struct drm_crtc *crtc;
208
209 if (conn_state->best_encoder) {
210 /* Unset the encoder_mask in the old crtc state. */
211 crtc = conn_state->connector->state->crtc;
212
213 /* A NULL crtc is an error here because we should have
214 * duplicated a NULL best_encoder when crtc was NULL.
215 * As an exception restoring duplicated atomic state
216 * during resume is allowed, so don't warn when
217 * best_encoder is equal to encoder we intend to set.
218 */
219 WARN_ON(!crtc && encoder != conn_state->best_encoder);
220 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100221 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100222
223 crtc_state->encoder_mask &=
224 ~(1 << drm_encoder_index(conn_state->best_encoder));
225 }
226 }
227
228 if (encoder) {
229 crtc = conn_state->crtc;
230 WARN_ON(!crtc);
231 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100232 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100233
234 crtc_state->encoder_mask |=
235 1 << drm_encoder_index(encoder);
236 }
237 }
238
239 conn_state->best_encoder = encoder;
240}
241
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100242static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200243steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100244 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200245{
Daniel Vetter623369e2014-09-16 17:50:47 +0200246 struct drm_crtc_state *crtc_state;
247 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100248 struct drm_connector_state *old_connector_state, *new_connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100249 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200250
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100251 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100252 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200253
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100254 if (new_connector_state->best_encoder != encoder)
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100255 continue;
256
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100257 encoder_crtc = old_connector_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200258
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100259 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
260 encoder->base.id, encoder->name,
261 encoder_crtc->base.id, encoder_crtc->name);
262
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100263 set_best_encoder(state, new_connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100264
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100265 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100266 crtc_state->connectors_changed = true;
267
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100268 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200269 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200270}
271
272static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100273update_connector_routing(struct drm_atomic_state *state,
274 struct drm_connector *connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100275 struct drm_connector_state *old_connector_state,
276 struct drm_connector_state *new_connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200277{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200278 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200279 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200280 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200281
Daniel Vetter17a38d92015-02-22 12:24:16 +0100282 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
283 connector->base.id,
284 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200285
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100286 if (old_connector_state->crtc != new_connector_state->crtc) {
287 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100288 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200289 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200290 }
291
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100292 if (new_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100293 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200294 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200295 }
296 }
297
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100298 if (!new_connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100299 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200300 connector->base.id,
301 connector->name);
302
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100303 set_best_encoder(state, new_connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200304
305 return 0;
306 }
307
308 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200309
310 if (funcs->atomic_best_encoder)
311 new_encoder = funcs->atomic_best_encoder(connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100312 new_connector_state);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200313 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200314 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200315 else
316 new_encoder = drm_atomic_helper_best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200317
318 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100319 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
320 connector->base.id,
321 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200322 return -EINVAL;
323 }
324
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100325 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
Russell King6ac7c542017-02-13 12:27:03 +0000326 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100327 new_encoder->base.id,
328 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100329 new_connector_state->crtc->base.id,
330 new_connector_state->crtc->name);
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100331 return -EINVAL;
332 }
333
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100334 if (new_encoder == new_connector_state->best_encoder) {
335 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100336
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200337 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100338 connector->base.id,
339 connector->name,
340 new_encoder->base.id,
341 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100342 new_connector_state->crtc->base.id,
343 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200344
345 return 0;
346 }
347
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100348 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200349
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100350 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100351
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100352 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200353 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200354
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200355 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100356 connector->base.id,
357 connector->name,
358 new_encoder->base.id,
359 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100360 new_connector_state->crtc->base.id,
361 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200362
363 return 0;
364}
365
366static int
367mode_fixup(struct drm_atomic_state *state)
368{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300369 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100370 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300371 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100372 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200373 int i;
Dan Carpenterf9ad86e2017-02-08 02:46:01 +0300374 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200375
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100376 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
377 if (!new_crtc_state->mode_changed &&
378 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200379 continue;
380
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100381 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200382 }
383
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100384 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200385 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200386 struct drm_encoder *encoder;
387
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100388 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200389
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100390 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200391 continue;
392
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100393 new_crtc_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100394 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200395
396 /*
397 * Each encoder has at most one connector (since we always steal
398 * it away), so we won't call ->mode_fixup twice.
399 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100400 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200401 funcs = encoder->helper_private;
402
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100403 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
404 &new_crtc_state->adjusted_mode);
Archit Taneja862e6862015-05-21 11:03:16 +0530405 if (!ret) {
406 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
407 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200408 }
409
Noralf Trønnes28276352016-05-11 18:09:20 +0200410 if (funcs && funcs->atomic_check) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100411 ret = funcs->atomic_check(encoder, new_crtc_state,
412 new_conn_state);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100413 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100414 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
415 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100416 return ret;
417 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200418 } else if (funcs && funcs->mode_fixup) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100419 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
420 &new_crtc_state->adjusted_mode);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100421 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100422 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
423 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100424 return -EINVAL;
425 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200426 }
427 }
428
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100429 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200430 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200431
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100432 if (!new_crtc_state->enable)
Liu Yingf55f1702016-05-27 17:35:54 +0800433 continue;
434
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100435 if (!new_crtc_state->mode_changed &&
436 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200437 continue;
438
439 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300440 if (!funcs->mode_fixup)
441 continue;
442
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100443 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
444 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200445 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200446 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
447 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200448 return -EINVAL;
449 }
450 }
451
452 return 0;
453}
454
Daniel Vetterd9b13622014-11-26 16:57:41 +0100455/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800456 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100457 * @dev: DRM device
458 * @state: the driver state object
459 *
460 * Check the state object to see if the requested state is physically possible.
461 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200462 * update and adds any additional connectors needed for full modesets and calls
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100463 * down into &drm_crtc_helper_funcs.mode_fixup and
464 * &drm_encoder_helper_funcs.mode_fixup or
465 * &drm_encoder_helper_funcs.atomic_check functions of the driver backend.
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200466 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100467 * &drm_crtc_state.mode_changed is set when the input mode is changed.
468 * &drm_crtc_state.connectors_changed is set when a connector is added or
469 * removed from the crtc. &drm_crtc_state.active_changed is set when
470 * &drm_crtc_state.active changes, which is used for DPMS.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100471 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100472 *
473 * IMPORTANT:
474 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100475 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
476 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
477 * without a full modeset) _must_ call this function afterwards after that
478 * change. It is permitted to call this function multiple times for the same
479 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
480 * upon the adjusted dotclock for fifo space allocation and watermark
481 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100482 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200483 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100484 * Zero for success or -errno
485 */
486int
Rob Clark934ce1c2014-11-19 16:41:33 -0500487drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200488 struct drm_atomic_state *state)
489{
Daniel Vetter623369e2014-09-16 17:50:47 +0200490 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100491 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300492 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100493 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200494 int i, ret;
495
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100496 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
497 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200498 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
499 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100500 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200501 }
502
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100503 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200504 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
505 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200506
507 /*
508 * For clarity this assignment is done here, but
509 * enable == 0 is only true when there are no
510 * connectors and a NULL mode.
511 *
512 * The other way around is true as well. enable != 0
513 * iff connectors are attached and a mode is set.
514 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100515 new_crtc_state->mode_changed = true;
516 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200517 }
518 }
519
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100520 ret = handle_conflicting_encoders(state, state->legacy_set_config);
521 if (ret)
522 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100523
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100524 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200525 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100526 * This only sets crtc->connectors_changed for routing changes,
527 * drivers must set crtc->connectors_changed themselves when
528 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200529 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100530 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100531 old_connector_state,
532 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200533 if (ret)
534 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100535 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100536 new_crtc_state = drm_atomic_get_new_crtc_state(state,
537 old_connector_state->crtc);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100538 if (old_connector_state->link_status !=
539 new_connector_state->link_status)
540 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200541 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200542 }
543
544 /*
545 * After all the routing has been prepared we need to add in any
546 * connector which is itself unchanged, but who's crtc changes it's
547 * configuration. This must be done before calling mode_fixup in case a
548 * crtc only changed its mode but has the same set of connectors.
549 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100550 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100551 bool has_connectors =
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100552 !!new_crtc_state->connector_mask;
Daniel Vetter623369e2014-09-16 17:50:47 +0200553
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100554 /*
555 * We must set ->active_changed after walking connectors for
556 * otherwise an update that only changes active would result in
557 * a full modeset because update_connector_routing force that.
558 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100559 if (old_crtc_state->active != new_crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200560 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
561 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100562 new_crtc_state->active_changed = true;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100563 }
564
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100565 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100566 continue;
567
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200568 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
569 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100570 new_crtc_state->enable ? 'y' : 'n',
571 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200572
573 ret = drm_atomic_add_affected_connectors(state, crtc);
574 if (ret != 0)
575 return ret;
576
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200577 ret = drm_atomic_add_affected_planes(state, crtc);
578 if (ret != 0)
579 return ret;
580
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100581 if (new_crtc_state->enable != has_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200582 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
583 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200584
585 return -EINVAL;
586 }
587 }
588
589 return mode_fixup(state);
590}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100591EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200592
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100593/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800594 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100595 * @dev: DRM device
596 * @state: the driver state object
597 *
598 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100599 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100600 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
601 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100602 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100603 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200604 * updated planes.
605 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200606 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100607 * Zero for success or -errno
608 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100609int
610drm_atomic_helper_check_planes(struct drm_device *dev,
611 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100612{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300613 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100614 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300615 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100616 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100617 int i, ret = 0;
618
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100619 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200620 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100621
622 funcs = plane->helper_private;
623
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100624 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100625
626 if (!funcs || !funcs->atomic_check)
627 continue;
628
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100629 ret = funcs->atomic_check(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100630 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200631 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
632 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100633 return ret;
634 }
635 }
636
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100637 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200638 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100639
640 funcs = crtc->helper_private;
641
642 if (!funcs || !funcs->atomic_check)
643 continue;
644
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100645 ret = funcs->atomic_check(crtc, new_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100646 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200647 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
648 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100649 return ret;
650 }
651 }
652
Daniel Vetterd9b13622014-11-26 16:57:41 +0100653 return ret;
654}
655EXPORT_SYMBOL(drm_atomic_helper_check_planes);
656
657/**
658 * drm_atomic_helper_check - validate state object
659 * @dev: DRM device
660 * @state: the driver state object
661 *
662 * Check the state object to see if the requested state is physically possible.
663 * Only crtcs and planes have check callbacks, so for any additional (global)
664 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100665 * Drivers without such needs can directly use this as their
666 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100667 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100668 * This just wraps the two parts of the state checking for planes and modeset
669 * state in the default order: First it calls drm_atomic_helper_check_modeset()
670 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100671 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
672 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
673 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100674 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200675 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100676 * Zero for success or -errno
677 */
678int drm_atomic_helper_check(struct drm_device *dev,
679 struct drm_atomic_state *state)
680{
681 int ret;
682
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100683 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100684 if (ret)
685 return ret;
686
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100687 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500688 if (ret)
689 return ret;
690
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100691 return ret;
692}
693EXPORT_SYMBOL(drm_atomic_helper_check);
694
Daniel Vetter623369e2014-09-16 17:50:47 +0200695static void
696disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
697{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300698 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100699 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300700 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100701 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200702 int i;
703
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100704 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200705 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200706 struct drm_encoder *encoder;
707
Daniel Vetter623369e2014-09-16 17:50:47 +0200708 /* Shut down everything that's in the changeset and currently
709 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300710 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200711 continue;
712
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100713 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100714
Daniel Vetter4218a322015-03-26 22:18:40 +0100715 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200716 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100717 continue;
718
Rob Clark46df9ad2014-11-20 15:40:36 -0500719 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200720
Rob Clark46df9ad2014-11-20 15:40:36 -0500721 /* We shouldn't get this far if we didn't previously have
722 * an encoder.. but WARN_ON() rather than explode.
723 */
724 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200725 continue;
726
727 funcs = encoder->helper_private;
728
Daniel Vetter17a38d92015-02-22 12:24:16 +0100729 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
730 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100731
Daniel Vetter623369e2014-09-16 17:50:47 +0200732 /*
733 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800734 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200735 */
Archit Taneja862e6862015-05-21 11:03:16 +0530736 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200737
738 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200739 if (funcs) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100740 if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +0200741 funcs->prepare(encoder);
742 else if (funcs->disable)
743 funcs->disable(encoder);
744 else if (funcs->dpms)
745 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
746 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200747
Archit Taneja862e6862015-05-21 11:03:16 +0530748 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200749 }
750
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100751 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200752 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200753
754 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100755 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100756 continue;
757
758 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200759 continue;
760
761 funcs = crtc->helper_private;
762
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200763 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
764 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100765
766
Daniel Vetter623369e2014-09-16 17:50:47 +0200767 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100768 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200769 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +0800770 else if (funcs->atomic_disable)
771 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200772 else if (funcs->disable)
773 funcs->disable(crtc);
774 else
775 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
776 }
777}
778
Daniel Vetter4c18d302015-05-12 15:27:37 +0200779/**
780 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
781 * @dev: DRM device
782 * @old_state: atomic state object with old state structures
783 *
784 * This function updates all the various legacy modeset state pointers in
785 * connectors, encoders and crtcs. It also updates the timestamping constants
786 * used for precise vblank timestamps by calling
787 * drm_calc_timestamping_constants().
788 *
789 * Drivers can use this for building their own atomic commit if they don't have
790 * a pure helper-based modeset implementation.
791 */
792void
793drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
794 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200795{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300796 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100797 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300798 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100799 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200800 int i;
801
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200802 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100803 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200804 if (connector->encoder) {
805 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200806
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200807 connector->encoder->crtc = NULL;
808 connector->encoder = NULL;
809 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200810
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100811 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200812 if ((!crtc && old_conn_state->crtc) ||
813 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
814 struct drm_property *dpms_prop =
815 dev->mode_config.dpms_property;
816 int mode = DRM_MODE_DPMS_OFF;
817
818 if (crtc && crtc->state->active)
819 mode = DRM_MODE_DPMS_ON;
820
821 connector->dpms = mode;
822 drm_object_property_set_value(&connector->base,
823 dpms_prop, mode);
824 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200825 }
826
827 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100828 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
829 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200830 continue;
831
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100832 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200833 continue;
834
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100835 connector->encoder = new_conn_state->best_encoder;
836 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200837 }
838
839 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100840 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200841 struct drm_plane *primary = crtc->primary;
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100842 struct drm_plane_state *new_plane_state;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200843
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100844 crtc->mode = new_crtc_state->mode;
845 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200846
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100847 new_plane_state =
848 drm_atomic_get_new_plane_state(old_state, primary);
849
850 if (new_plane_state && new_plane_state->crtc == crtc) {
851 crtc->x = new_plane_state->src_x >> 16;
852 crtc->y = new_plane_state->src_y >> 16;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200853 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200854
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100855 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200856 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100857 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200858 }
859}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200860EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200861
862static void
863crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
864{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300865 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100866 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300867 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100868 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200869 int i;
870
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100871 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200872 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200873
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100874 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200875 continue;
876
877 funcs = crtc->helper_private;
878
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100879 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200880 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
881 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100882
Daniel Vetter623369e2014-09-16 17:50:47 +0200883 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100884 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200885 }
886
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100887 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200888 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200889 struct drm_encoder *encoder;
890 struct drm_display_mode *mode, *adjusted_mode;
891
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100892 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200893 continue;
894
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100895 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200896 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100897 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200898 mode = &new_crtc_state->mode;
899 adjusted_mode = &new_crtc_state->adjusted_mode;
900
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100901 if (!new_crtc_state->mode_changed)
902 continue;
903
Daniel Vetter17a38d92015-02-22 12:24:16 +0100904 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
905 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100906
Daniel Vetter623369e2014-09-16 17:50:47 +0200907 /*
908 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800909 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200910 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200911 if (funcs && funcs->atomic_mode_set) {
912 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100913 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200914 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +0100915 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200916 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200917
Archit Taneja862e6862015-05-21 11:03:16 +0530918 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200919 }
920}
921
922/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100923 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200924 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100925 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200926 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100927 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200928 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100929 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300930 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100931 * drm_atomic_helper_commit_planes(), which is what the default commit function
932 * does. But drivers with different needs can group the modeset commits together
933 * and do the plane commits at the end. This is useful for drivers doing runtime
934 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200935 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100936void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
937 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200938{
Laurent Pincharta072f802015-02-22 12:24:18 +0100939 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200940
941 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
942
Laurent Pincharta072f802015-02-22 12:24:18 +0100943 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200944}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100945EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200946
947/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100948 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200949 * @dev: DRM device
950 * @old_state: atomic state object with old state structures
951 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100952 * This function enables all the outputs with the new configuration which had to
953 * be turned off for the update.
954 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300955 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100956 * drm_atomic_helper_commit_planes(), which is what the default commit function
957 * does. But drivers with different needs can group the modeset commits together
958 * and do the plane commits at the end. This is useful for drivers doing runtime
959 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200960 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100961void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
962 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200963{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300964 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100965 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300966 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100967 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200968 int i;
969
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100970 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200971 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200972
973 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100974 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100975 continue;
976
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100977 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200978 continue;
979
980 funcs = crtc->helper_private;
981
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100982 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200983 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
984 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100985
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100986 if (funcs->enable)
987 funcs->enable(crtc);
988 else
989 funcs->commit(crtc);
990 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200991 }
992
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100993 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200994 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200995 struct drm_encoder *encoder;
996
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100997 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200998 continue;
999
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001000 if (!new_conn_state->crtc->state->active ||
1001 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001002 continue;
1003
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001004 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001005 funcs = encoder->helper_private;
1006
Daniel Vetter17a38d92015-02-22 12:24:16 +01001007 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1008 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001009
Daniel Vetter623369e2014-09-16 17:50:47 +02001010 /*
1011 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001012 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001013 */
Archit Taneja862e6862015-05-21 11:03:16 +05301014 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001015
Noralf Trønnes28276352016-05-11 18:09:20 +02001016 if (funcs) {
1017 if (funcs->enable)
1018 funcs->enable(encoder);
1019 else if (funcs->commit)
1020 funcs->commit(encoder);
1021 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001022
Archit Taneja862e6862015-05-21 11:03:16 +05301023 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001024 }
1025}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001026EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001027
Rob Clark4c5b7f32016-03-18 19:14:55 -04001028/**
1029 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1030 * @dev: DRM device
1031 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001032 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1033 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001034 *
1035 * For implicit sync, driver should fish the exclusive fence out from the
1036 * incoming fb's and stash it in the drm_plane_state. This is called after
1037 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1038 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001039 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001040 * Note that @pre_swap is needed since the point where we block for fences moves
1041 * around depending upon whether an atomic commit is blocking or
1042 * non-blocking. For async commit all waiting needs to happen after
1043 * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1044 * to wait **before** we do anything that can't be easily rolled back. That is
1045 * before we call drm_atomic_helper_swap_state().
1046 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001047 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001048 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001049int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1050 struct drm_atomic_state *state,
1051 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001052{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001053 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001054 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001055 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001056
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001057 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1058 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001059 continue;
1060
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001061 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001062
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001063 /*
1064 * If waiting for fences pre-swap (ie: nonblock), userspace can
1065 * still interrupt the operation. Instead of blocking until the
1066 * timer expires, make the wait interruptible.
1067 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001068 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001069 if (ret)
1070 return ret;
1071
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001072 dma_fence_put(new_plane_state->fence);
1073 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001074 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001075
1076 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001077}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001078EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001079
John Keepingc2409062016-01-19 10:46:58 +00001080/**
Rob Clark5ee32292014-11-11 19:38:59 -05001081 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1082 * @dev: DRM device
1083 * @old_state: atomic state object with old state structures
1084 *
1085 * Helper to, after atomic commit, wait for vblanks on all effected
1086 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001087 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1088 * framebuffers have actually changed to optimize for the legacy cursor and
1089 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001090 */
1091void
1092drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1093 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001094{
1095 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001096 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001097 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001098 unsigned crtc_mask = 0;
1099
1100 /*
1101 * Legacy cursor ioctls are completely unsynced, and userspace
1102 * relies on that (by doing tons of cursor updates).
1103 */
1104 if (old_state->legacy_cursor_update)
1105 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001106
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001107 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorsta3fbb532016-12-08 14:45:27 +01001108 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
Daniel Vetterab58e332014-11-24 20:42:42 +01001109 continue;
1110
Daniel Vetter623369e2014-09-16 17:50:47 +02001111 ret = drm_crtc_vblank_get(crtc);
1112 if (ret != 0)
1113 continue;
1114
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001115 crtc_mask |= drm_crtc_mask(crtc);
1116 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001117 }
1118
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001119 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001120 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001121 continue;
1122
1123 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001124 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001125 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001126 msecs_to_jiffies(50));
1127
Russell King6ac7c542017-02-13 12:27:03 +00001128 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1129 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001130
Daniel Vetter623369e2014-09-16 17:50:47 +02001131 drm_crtc_vblank_put(crtc);
1132 }
1133}
Rob Clark5ee32292014-11-11 19:38:59 -05001134EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001135
1136/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001137 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001138 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001139 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001140 * This is the default implementation for the
1141 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
Daniel Vetter623369e2014-09-16 17:50:47 +02001142 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001143 * Note that the default ordering of how the various stages are called is to
1144 * match the legacy modeset helper library closest. One peculiarity of that is
1145 * that it doesn't mesh well with runtime PM at all.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001146 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001147 * For drivers supporting runtime PM the recommended sequence is instead ::
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001148 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001149 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001150 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001151 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001152 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001153 * drm_atomic_helper_commit_planes(dev, old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001154 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001155 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001156 * for committing the atomic update to hardware. See the kerneldoc entries for
1157 * these three functions for more details.
1158 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001159void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001160{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001161 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001162
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001163 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001164
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001165 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001166
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001167 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001168
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001169 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001170
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001171 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001172
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001173 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001174}
1175EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1176
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001177static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001178{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001179 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001180 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001181
1182 funcs = dev->mode_config.helper_private;
1183
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001184 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001185
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001186 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001187
1188 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001189 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001190 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001191 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001192
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001193 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001194
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001195 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001196}
1197
1198static void commit_work(struct work_struct *work)
1199{
1200 struct drm_atomic_state *state = container_of(work,
1201 struct drm_atomic_state,
1202 commit_work);
1203 commit_tail(state);
1204}
1205
1206/**
1207 * drm_atomic_helper_commit - commit validated state object
1208 * @dev: DRM device
1209 * @state: the driver state object
1210 * @nonblock: whether nonblocking behavior is requested.
1211 *
1212 * This function commits a with drm_atomic_helper_check() pre-validated state
1213 * object. This can still fail when e.g. the framebuffer reservation fails. This
1214 * function implements nonblocking commits, using
1215 * drm_atomic_helper_setup_commit() and related functions.
1216 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001217 * Committing the actual hardware state is done through the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001218 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1219 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001220 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001221 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001222 * Zero for success or -errno.
1223 */
1224int drm_atomic_helper_commit(struct drm_device *dev,
1225 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001226 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001227{
1228 int ret;
1229
Daniel Vettera095caa2016-06-08 17:15:36 +02001230 ret = drm_atomic_helper_setup_commit(state, nonblock);
1231 if (ret)
1232 return ret;
1233
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001234 INIT_WORK(&state->commit_work, commit_work);
1235
Daniel Vetter623369e2014-09-16 17:50:47 +02001236 ret = drm_atomic_helper_prepare_planes(dev, state);
1237 if (ret)
1238 return ret;
1239
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001240 if (!nonblock) {
1241 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001242 if (ret) {
1243 drm_atomic_helper_cleanup_planes(dev, state);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001244 return ret;
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001245 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001246 }
1247
Daniel Vetter623369e2014-09-16 17:50:47 +02001248 /*
1249 * This is the point of no return - everything below never fails except
1250 * when the hw goes bonghits. Which means we can commit the new state on
1251 * the software side now.
1252 */
1253
Daniel Vetter5e84c262016-06-10 00:06:32 +02001254 drm_atomic_helper_swap_state(state, true);
Daniel Vetter623369e2014-09-16 17:50:47 +02001255
1256 /*
1257 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001258 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001259 * that the asynchronous work has either been cancelled (if the driver
1260 * supports it, which at least requires that the framebuffers get
1261 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1262 * before the new state gets committed on the software side with
1263 * drm_atomic_helper_swap_state().
1264 *
1265 * This scheme allows new atomic state updates to be prepared and
1266 * checked in parallel to the asynchronous completion of the previous
1267 * update. Which is important since compositors need to figure out the
1268 * composition of the next frame right after having submitted the
1269 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001270 *
1271 * NOTE: Commit work has multiple phases, first hardware commit, then
1272 * cleanup. We want them to overlap, hence need system_unbound_wq to
1273 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001274 */
1275
Chris Wilson08536952016-10-14 13:18:18 +01001276 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001277 if (nonblock)
1278 queue_work(system_unbound_wq, &state->commit_work);
1279 else
1280 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001281
1282 return 0;
1283}
1284EXPORT_SYMBOL(drm_atomic_helper_commit);
1285
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001286/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001287 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001288 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001289 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001290 *
1291 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1292 * which commit needs to call which can fail, so we want to run it first and
1293 * synchronously.
1294 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001295 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001296 * might be affected the new state update. This can be done by either cancelling
1297 * or flushing the work items, depending upon whether the driver can deal with
1298 * cancelled updates. Note that it is important to ensure that the framebuffer
1299 * cleanup is still done when cancelling.
1300 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001301 * Asynchronous workers need to have sufficient parallelism to be able to run
1302 * different atomic commits on different CRTCs in parallel. The simplest way to
1303 * achive this is by running them on the &system_unbound_wq work queue. Note
1304 * that drivers are not required to split up atomic commits and run an
1305 * individual commit in parallel - userspace is supposed to do that if it cares.
1306 * But it might be beneficial to do that for modesets, since those necessarily
1307 * must be done as one global operation, and enabling or disabling a CRTC can
1308 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001309 *
1310 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001311 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001312 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001313 * while it's guaranteed that no relevant nonblocking worker runs means that
1314 * nonblocking workers do not need grab any locks. Actually they must not grab
1315 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001316 *
1317 * 4. Schedule a work item to do all subsequent steps, using the split-out
1318 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1319 * then cleaning up the framebuffers after the old framebuffer is no longer
1320 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001321 *
1322 * The above scheme is implemented in the atomic helper libraries in
1323 * drm_atomic_helper_commit() using a bunch of helper functions. See
1324 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001325 */
1326
Daniel Vettera095caa2016-06-08 17:15:36 +02001327static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1328{
1329 struct drm_crtc_commit *commit, *stall_commit = NULL;
1330 bool completed = true;
1331 int i;
1332 long ret = 0;
1333
1334 spin_lock(&crtc->commit_lock);
1335 i = 0;
1336 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1337 if (i == 0) {
1338 completed = try_wait_for_completion(&commit->flip_done);
1339 /* Userspace is not allowed to get ahead of the previous
1340 * commit with nonblocking ones. */
1341 if (!completed && nonblock) {
1342 spin_unlock(&crtc->commit_lock);
1343 return -EBUSY;
1344 }
1345 } else if (i == 1) {
1346 stall_commit = commit;
1347 drm_crtc_commit_get(stall_commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001348 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001349 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001350
1351 i++;
1352 }
1353 spin_unlock(&crtc->commit_lock);
1354
1355 if (!stall_commit)
1356 return 0;
1357
1358 /* We don't want to let commits get ahead of cleanup work too much,
1359 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1360 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001361 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001362 10*HZ);
1363 if (ret == 0)
1364 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1365 crtc->base.id, crtc->name);
1366
1367 drm_crtc_commit_put(stall_commit);
1368
1369 return ret < 0 ? ret : 0;
1370}
1371
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001372static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001373{
1374 struct drm_crtc_commit *commit = container_of(completion,
1375 typeof(*commit),
1376 flip_done);
1377
1378 drm_crtc_commit_put(commit);
1379}
1380
Daniel Vettera095caa2016-06-08 17:15:36 +02001381/**
1382 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1383 * @state: new modeset state to be committed
1384 * @nonblock: whether nonblocking behavior is requested.
1385 *
1386 * This function prepares @state to be used by the atomic helper's support for
1387 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001388 * should always call this function from their
1389 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001390 *
1391 * To be able to use this support drivers need to use a few more helper
1392 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1393 * actually committing the hardware state, and for nonblocking commits this call
1394 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1395 * and it's stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001396 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001397 *
1398 * Completion of the hardware commit step must be signalled using
1399 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1400 * to read or change any permanent software or hardware modeset state. The only
1401 * exception is state protected by other means than &drm_modeset_lock locks.
1402 * Only the free standing @state with pointers to the old state structures can
1403 * be inspected, e.g. to clean up old buffers using
1404 * drm_atomic_helper_cleanup_planes().
1405 *
1406 * At the very end, before cleaning up @state drivers must call
1407 * drm_atomic_helper_commit_cleanup_done().
1408 *
1409 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1410 * complete and esay-to-use default implementation of the atomic_commit() hook.
1411 *
1412 * The tracking of asynchronously executed and still pending commits is done
1413 * using the core structure &drm_crtc_commit.
1414 *
1415 * By default there's no need to clean up resources allocated by this function
1416 * explicitly: drm_atomic_state_default_clear() will take care of that
1417 * automatically.
1418 *
1419 * Returns:
1420 *
1421 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1422 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1423 */
1424int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1425 bool nonblock)
1426{
1427 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001428 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001429 struct drm_crtc_commit *commit;
1430 int i, ret;
1431
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001432 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001433 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1434 if (!commit)
1435 return -ENOMEM;
1436
1437 init_completion(&commit->flip_done);
1438 init_completion(&commit->hw_done);
1439 init_completion(&commit->cleanup_done);
1440 INIT_LIST_HEAD(&commit->commit_entry);
1441 kref_init(&commit->ref);
1442 commit->crtc = crtc;
1443
1444 state->crtcs[i].commit = commit;
1445
1446 ret = stall_checks(crtc, nonblock);
1447 if (ret)
1448 return ret;
1449
1450 /* Drivers only send out events when at least either current or
1451 * new CRTC state is active. Complete right away if everything
1452 * stays off. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001453 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001454 complete_all(&commit->flip_done);
1455 continue;
1456 }
1457
1458 /* Legacy cursor updates are fully unsynced. */
1459 if (state->legacy_cursor_update) {
1460 complete_all(&commit->flip_done);
1461 continue;
1462 }
1463
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001464 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001465 commit->event = kzalloc(sizeof(*commit->event),
1466 GFP_KERNEL);
1467 if (!commit->event)
1468 return -ENOMEM;
1469
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001470 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02001471 }
1472
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001473 new_crtc_state->event->base.completion = &commit->flip_done;
1474 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01001475 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001476 }
1477
1478 return 0;
1479}
1480EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1481
1482
1483static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1484{
1485 struct drm_crtc_commit *commit;
1486 int i = 0;
1487
1488 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1489 /* skip the first entry, that's the current commit */
1490 if (i == 1)
1491 return commit;
1492 i++;
1493 }
1494
1495 return NULL;
1496}
1497
1498/**
1499 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001500 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001501 *
1502 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001503 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02001504 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001505 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02001506 *
1507 * This is part of the atomic helper support for nonblocking commits, see
1508 * drm_atomic_helper_setup_commit() for an overview.
1509 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001510void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001511{
1512 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001513 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001514 struct drm_crtc_commit *commit;
1515 int i;
1516 long ret;
1517
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001518 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001519 spin_lock(&crtc->commit_lock);
1520 commit = preceeding_commit(crtc);
1521 if (commit)
1522 drm_crtc_commit_get(commit);
1523 spin_unlock(&crtc->commit_lock);
1524
1525 if (!commit)
1526 continue;
1527
1528 ret = wait_for_completion_timeout(&commit->hw_done,
1529 10*HZ);
1530 if (ret == 0)
1531 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1532 crtc->base.id, crtc->name);
1533
1534 /* Currently no support for overwriting flips, hence
1535 * stall for previous one to execute completely. */
1536 ret = wait_for_completion_timeout(&commit->flip_done,
1537 10*HZ);
1538 if (ret == 0)
1539 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1540 crtc->base.id, crtc->name);
1541
1542 drm_crtc_commit_put(commit);
1543 }
1544}
1545EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1546
1547/**
1548 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001549 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001550 *
1551 * This function is used to signal completion of the hardware commit step. After
1552 * this step the driver is not allowed to read or change any permanent software
1553 * or hardware modeset state. The only exception is state protected by other
1554 * means than &drm_modeset_lock locks.
1555 *
1556 * Drivers should try to postpone any expensive or delayed cleanup work after
1557 * this function is called.
1558 *
1559 * This is part of the atomic helper support for nonblocking commits, see
1560 * drm_atomic_helper_setup_commit() for an overview.
1561 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001562void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001563{
1564 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001565 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001566 struct drm_crtc_commit *commit;
1567 int i;
1568
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001569 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001570 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001571 if (!commit)
1572 continue;
1573
1574 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001575 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02001576 spin_lock(&crtc->commit_lock);
1577 complete_all(&commit->hw_done);
1578 spin_unlock(&crtc->commit_lock);
1579 }
1580}
1581EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1582
1583/**
1584 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001585 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001586 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001587 * This signals completion of the atomic update @old_state, including any
1588 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01001589 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02001590 *
1591 * This is part of the atomic helper support for nonblocking commits, see
1592 * drm_atomic_helper_setup_commit() for an overview.
1593 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001594void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001595{
1596 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001597 struct drm_crtc_state *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001598 struct drm_crtc_commit *commit;
1599 int i;
1600 long ret;
1601
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001602 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001603 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001604 if (WARN_ON(!commit))
1605 continue;
1606
1607 spin_lock(&crtc->commit_lock);
1608 complete_all(&commit->cleanup_done);
1609 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1610
1611 /* commit_list borrows our reference, need to remove before we
1612 * clean up our drm_atomic_state. But only after it actually
1613 * completed, otherwise subsequent commits won't stall properly. */
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001614 if (try_wait_for_completion(&commit->flip_done))
1615 goto del_commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001616
1617 spin_unlock(&crtc->commit_lock);
1618
1619 /* We must wait for the vblank event to signal our completion
1620 * before releasing our reference, since the vblank work does
1621 * not hold a reference of its own. */
1622 ret = wait_for_completion_timeout(&commit->flip_done,
1623 10*HZ);
1624 if (ret == 0)
1625 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1626 crtc->base.id, crtc->name);
1627
1628 spin_lock(&crtc->commit_lock);
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001629del_commit:
Daniel Vettera095caa2016-06-08 17:15:36 +02001630 list_del(&commit->commit_entry);
1631 spin_unlock(&crtc->commit_lock);
1632 }
1633}
1634EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1635
Daniel Vettere8c833a2014-07-27 18:30:19 +02001636/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001637 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001638 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001639 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001640 *
1641 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001642 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1643 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1644 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001645 *
1646 * Returns:
1647 * 0 on success, negative error code on failure.
1648 */
1649int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1650 struct drm_atomic_state *state)
1651{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001652 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001653 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001654 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001655
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001656 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001657 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001658
1659 funcs = plane->helper_private;
1660
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001661 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001662 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001663 if (ret)
1664 goto fail;
1665 }
1666 }
1667
1668 return 0;
1669
1670fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001671 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001672 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001673
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001674 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001675 continue;
1676
1677 funcs = plane->helper_private;
1678
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001679 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001680 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001681 }
1682
1683 return ret;
1684}
1685EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1686
Ville Syrjälä7135ac52016-09-19 16:33:42 +03001687static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001688{
1689 return state->crtc && state->crtc->state->active;
1690}
1691
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001692/**
1693 * drm_atomic_helper_commit_planes - commit plane state
1694 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001695 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08001696 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001697 *
1698 * This function commits the new plane state using the plane and atomic helper
1699 * functions for planes and crtcs. It assumes that the atomic state has already
1700 * been pushed into the relevant object state pointers, since this step can no
1701 * longer fail.
1702 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001703 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001704 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001705 *
1706 * Note that this function does all plane updates across all CRTCs in one step.
1707 * If the hardware can't support this approach look at
1708 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001709 *
1710 * Plane parameters can be updated by applications while the associated CRTC is
1711 * disabled. The DRM/KMS core will store the parameters in the plane state,
1712 * which will be available to the driver when the CRTC is turned on. As a result
1713 * most drivers don't need to be immediately notified of plane updates for a
1714 * disabled CRTC.
1715 *
Liu Ying2b58e982016-08-29 17:12:03 +08001716 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1717 * @flags in order not to receive plane update notifications related to a
1718 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001719 * driver code when the driver and/or hardware can't or just don't need to deal
1720 * with updates on disabled CRTCs, for example when supporting runtime PM.
1721 *
Liu Ying2b58e982016-08-29 17:12:03 +08001722 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1723 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001724 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1725 * call for a plane if the CRTC of the old plane state needs a modesetting
1726 * operation. Of course, the drivers need to disable the planes in their CRTC
1727 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08001728 *
1729 * The drm_atomic_helper_commit() default implementation doesn't set the
1730 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1731 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001732 */
1733void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001734 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001735 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001736{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001737 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001738 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001739 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001740 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001741 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08001742 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1743 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001744
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001745 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001746 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001747
1748 funcs = crtc->helper_private;
1749
1750 if (!funcs || !funcs->atomic_begin)
1751 continue;
1752
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001753 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001754 continue;
1755
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001756 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001757 }
1758
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001759 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001760 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001761 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001762
1763 funcs = plane->helper_private;
1764
Thierry Reding3cad4b62014-11-25 13:05:12 +01001765 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001766 continue;
1767
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001768 disabling = drm_atomic_plane_disabling(old_plane_state,
1769 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001770
1771 if (active_only) {
1772 /*
1773 * Skip planes related to inactive CRTCs. If the plane
1774 * is enabled use the state of the current CRTC. If the
1775 * plane is being disabled use the state of the old
1776 * CRTC to avoid skipping planes being disabled on an
1777 * active CRTC.
1778 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001779 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001780 continue;
1781 if (disabling && !plane_crtc_active(old_plane_state))
1782 continue;
1783 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001784
Thierry Reding407b8bd2014-11-20 12:05:50 +01001785 /*
1786 * Special-case disabling the plane if drivers support it.
1787 */
Liu Ying2b58e982016-08-29 17:12:03 +08001788 if (disabling && funcs->atomic_disable) {
1789 struct drm_crtc_state *crtc_state;
1790
1791 crtc_state = old_plane_state->crtc->state;
1792
1793 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1794 no_disable)
1795 continue;
1796
Thierry Reding407b8bd2014-11-20 12:05:50 +01001797 funcs->atomic_disable(plane, old_plane_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001798 } else if (new_plane_state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01001799 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08001800 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001801 }
1802
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001803 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001804 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001805
1806 funcs = crtc->helper_private;
1807
1808 if (!funcs || !funcs->atomic_flush)
1809 continue;
1810
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001811 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001812 continue;
1813
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001814 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001815 }
1816}
1817EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1818
1819/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001820 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1821 * @old_crtc_state: atomic state object with the old crtc state
1822 *
1823 * This function commits the new plane state using the plane and atomic helper
1824 * functions for planes on the specific crtc. It assumes that the atomic state
1825 * has already been pushed into the relevant object state pointers, since this
1826 * step can no longer fail.
1827 *
1828 * This function is useful when plane updates should be done crtc-by-crtc
1829 * instead of one global step like drm_atomic_helper_commit_planes() does.
1830 *
1831 * This function can only be savely used when planes are not allowed to move
1832 * between different CRTCs because this function doesn't handle inter-CRTC
1833 * depencies. Callers need to ensure that either no such depencies exist,
1834 * resolve them through ordering of commit calls or through some other means.
1835 */
1836void
1837drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1838{
1839 const struct drm_crtc_helper_funcs *crtc_funcs;
1840 struct drm_crtc *crtc = old_crtc_state->crtc;
1841 struct drm_atomic_state *old_state = old_crtc_state->state;
1842 struct drm_plane *plane;
1843 unsigned plane_mask;
1844
1845 plane_mask = old_crtc_state->plane_mask;
1846 plane_mask |= crtc->state->plane_mask;
1847
1848 crtc_funcs = crtc->helper_private;
1849 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001850 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001851
1852 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1853 struct drm_plane_state *old_plane_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001854 drm_atomic_get_old_plane_state(old_state, plane);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001855 const struct drm_plane_helper_funcs *plane_funcs;
1856
1857 plane_funcs = plane->helper_private;
1858
1859 if (!old_plane_state || !plane_funcs)
1860 continue;
1861
1862 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1863
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001864 if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001865 plane_funcs->atomic_disable)
1866 plane_funcs->atomic_disable(plane, old_plane_state);
1867 else if (plane->state->crtc ||
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01001868 drm_atomic_plane_disabling(old_plane_state, plane->state))
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001869 plane_funcs->atomic_update(plane, old_plane_state);
1870 }
1871
1872 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001873 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001874}
1875EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1876
1877/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001878 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08001879 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02001880 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1881 *
1882 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08001883 * used for instance in the CRTC helper atomic_disable callback to disable
1884 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001885 *
1886 * If the atomic-parameter is set the function calls the CRTC's
1887 * atomic_begin hook before and atomic_flush hook after disabling the
1888 * planes.
1889 *
1890 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001891 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001892 */
Liu Ying28500292016-08-26 15:30:39 +08001893void
1894drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1895 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001896{
Liu Ying28500292016-08-26 15:30:39 +08001897 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02001898 const struct drm_crtc_helper_funcs *crtc_funcs =
1899 crtc->helper_private;
1900 struct drm_plane *plane;
1901
1902 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1903 crtc_funcs->atomic_begin(crtc, NULL);
1904
Liu Ying28500292016-08-26 15:30:39 +08001905 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02001906 const struct drm_plane_helper_funcs *plane_funcs =
1907 plane->helper_private;
1908
Liu Ying28500292016-08-26 15:30:39 +08001909 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001910 continue;
1911
1912 WARN_ON(!plane_funcs->atomic_disable);
1913 if (plane_funcs->atomic_disable)
1914 plane_funcs->atomic_disable(plane, NULL);
1915 }
1916
1917 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1918 crtc_funcs->atomic_flush(crtc, NULL);
1919}
1920EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1921
1922/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001923 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1924 * @dev: DRM device
1925 * @old_state: atomic state object with old state structures
1926 *
1927 * This function cleans up plane state, specifically framebuffers, from the old
1928 * configuration. Hence the old configuration must be perserved in @old_state to
1929 * be able to call this function.
1930 *
1931 * This function must also be called on the new state when the atomic update
1932 * fails at any point after calling drm_atomic_helper_prepare_planes().
1933 */
1934void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1935 struct drm_atomic_state *old_state)
1936{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001937 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001938 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001939 int i;
1940
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001941 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001942 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001943 struct drm_plane_state *plane_state;
1944
1945 /*
1946 * This might be called before swapping when commit is aborted,
1947 * in which case we have to cleanup the new state.
1948 */
1949 if (old_plane_state == plane->state)
1950 plane_state = new_plane_state;
1951 else
1952 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001953
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001954 funcs = plane->helper_private;
1955
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001956 if (funcs->cleanup_fb)
1957 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001958 }
1959}
1960EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1961
1962/**
1963 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001964 * @state: atomic state
Daniel Vetter5e84c262016-06-10 00:06:32 +02001965 * @stall: stall for proceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001966 *
1967 * This function stores the atomic state into the current state pointers in all
1968 * driver objects. It should be called after all failing steps have been done
1969 * and succeeded, but before the actual hardware state is committed.
1970 *
1971 * For cleanup and error recovery the current state for all changed objects will
1972 * be swaped into @state.
1973 *
1974 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1975 *
1976 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1977 *
1978 * 2. Do any other steps that might fail.
1979 *
1980 * 3. Put the staged state into the current state pointers with this function.
1981 *
1982 * 4. Actually commit the hardware state.
1983 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001984 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001985 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02001986 *
1987 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001988 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
1989 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02001990 * don't pass the right state structures to the callbacks.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001991 */
Daniel Vetter5e84c262016-06-10 00:06:32 +02001992void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1993 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001994{
1995 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02001996 long ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001997 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001998 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001999 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002000 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002001 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002002 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002003 struct drm_crtc_commit *commit;
2004
2005 if (stall) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002006 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002007 spin_lock(&crtc->commit_lock);
2008 commit = list_first_entry_or_null(&crtc->commit_list,
2009 struct drm_crtc_commit, commit_entry);
2010 if (commit)
2011 drm_crtc_commit_get(commit);
2012 spin_unlock(&crtc->commit_lock);
2013
2014 if (!commit)
2015 continue;
2016
2017 ret = wait_for_completion_timeout(&commit->hw_done,
2018 10*HZ);
2019 if (ret == 0)
2020 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2021 crtc->base.id, crtc->name);
2022 drm_crtc_commit_put(commit);
2023 }
2024 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002025
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002026 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002027 WARN_ON(connector->state != old_conn_state);
2028
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002029 old_conn_state->state = state;
2030 new_conn_state->state = NULL;
2031
2032 state->connectors[i].state = old_conn_state;
2033 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002034 }
2035
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002036 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002037 WARN_ON(crtc->state != old_crtc_state);
2038
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002039 old_crtc_state->state = state;
2040 new_crtc_state->state = NULL;
2041
2042 state->crtcs[i].state = old_crtc_state;
2043 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002044
2045 if (state->crtcs[i].commit) {
2046 spin_lock(&crtc->commit_lock);
2047 list_add(&state->crtcs[i].commit->commit_entry,
2048 &crtc->commit_list);
2049 spin_unlock(&crtc->commit_lock);
2050
2051 state->crtcs[i].commit->event = NULL;
2052 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002053 }
2054
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002055 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002056 WARN_ON(plane->state != old_plane_state);
2057
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002058 old_plane_state->state = state;
2059 new_plane_state->state = NULL;
2060
2061 state->planes[i].state = old_plane_state;
2062 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002063 }
2064}
2065EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002066
2067/**
2068 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2069 * @plane: plane object to update
2070 * @crtc: owning CRTC of owning plane
2071 * @fb: framebuffer to flip onto plane
2072 * @crtc_x: x offset of primary plane on crtc
2073 * @crtc_y: y offset of primary plane on crtc
2074 * @crtc_w: width of primary plane rectangle on crtc
2075 * @crtc_h: height of primary plane rectangle on crtc
2076 * @src_x: x offset of @fb for panning
2077 * @src_y: y offset of @fb for panning
2078 * @src_w: width of source rectangle in @fb
2079 * @src_h: height of source rectangle in @fb
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002080 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002081 *
2082 * Provides a default plane update handler using the atomic driver interface.
2083 *
2084 * RETURNS:
2085 * Zero on success, error code on failure
2086 */
2087int drm_atomic_helper_update_plane(struct drm_plane *plane,
2088 struct drm_crtc *crtc,
2089 struct drm_framebuffer *fb,
2090 int crtc_x, int crtc_y,
2091 unsigned int crtc_w, unsigned int crtc_h,
2092 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002093 uint32_t src_w, uint32_t src_h,
2094 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002095{
2096 struct drm_atomic_state *state;
2097 struct drm_plane_state *plane_state;
2098 int ret = 0;
2099
2100 state = drm_atomic_state_alloc(plane->dev);
2101 if (!state)
2102 return -ENOMEM;
2103
2104 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2105retry:
2106 plane_state = drm_atomic_get_plane_state(state, plane);
2107 if (IS_ERR(plane_state)) {
2108 ret = PTR_ERR(plane_state);
2109 goto fail;
2110 }
2111
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002112 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002113 if (ret != 0)
2114 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002115 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002116 plane_state->crtc_x = crtc_x;
2117 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002118 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002119 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002120 plane_state->src_x = src_x;
2121 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002122 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002123 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002124
Daniel Vetter3671c582015-05-04 15:40:52 +02002125 if (plane == crtc->cursor)
2126 state->legacy_cursor_update = true;
2127
Daniel Vetter042652e2014-07-27 13:46:52 +02002128 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002129fail:
2130 if (ret == -EDEADLK)
2131 goto backoff;
2132
Chris Wilson08536952016-10-14 13:18:18 +01002133 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002134 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002135
Daniel Vetter042652e2014-07-27 13:46:52 +02002136backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002137 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002138 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002139
2140 /*
2141 * Someone might have exchanged the framebuffer while we dropped locks
2142 * in the backoff code. We need to fix up the fb refcount tracking the
2143 * core does for us.
2144 */
2145 plane->old_fb = plane->fb;
2146
2147 goto retry;
2148}
2149EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2150
2151/**
2152 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2153 * @plane: plane to disable
Daniel Vetter19315292017-03-22 22:50:43 +01002154 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002155 *
2156 * Provides a default plane disable handler using the atomic driver interface.
2157 *
2158 * RETURNS:
2159 * Zero on success, error code on failure
2160 */
Daniel Vetter19315292017-03-22 22:50:43 +01002161int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2162 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002163{
2164 struct drm_atomic_state *state;
2165 struct drm_plane_state *plane_state;
2166 int ret = 0;
2167
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08002168 /*
2169 * FIXME: Without plane->crtc set we can't get at the implicit legacy
2170 * acquire context. The real fix will be to wire the acquire ctx through
2171 * everywhere we need it, but meanwhile prevent chaos by just skipping
2172 * this noop. The critical case is the cursor ioctls which a) only grab
2173 * crtc/cursor-plane locks (so we need the crtc to get at the right
2174 * acquire context) and b) can try to disable the plane multiple times.
2175 */
2176 if (!plane->crtc)
2177 return 0;
2178
Daniel Vetter042652e2014-07-27 13:46:52 +02002179 state = drm_atomic_state_alloc(plane->dev);
2180 if (!state)
2181 return -ENOMEM;
2182
2183 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
2184retry:
2185 plane_state = drm_atomic_get_plane_state(state, plane);
2186 if (IS_ERR(plane_state)) {
2187 ret = PTR_ERR(plane_state);
2188 goto fail;
2189 }
2190
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002191 if (plane_state->crtc && (plane == plane->crtc->cursor))
2192 plane_state->state->legacy_cursor_update = true;
2193
Rob Clarkbbb1e522015-08-25 15:35:58 -04002194 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002195 if (ret != 0)
2196 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002197
Daniel Vetter042652e2014-07-27 13:46:52 +02002198 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002199fail:
2200 if (ret == -EDEADLK)
2201 goto backoff;
2202
Chris Wilson08536952016-10-14 13:18:18 +01002203 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002204 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002205
Daniel Vetter042652e2014-07-27 13:46:52 +02002206backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002207 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002208 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002209
2210 /*
2211 * Someone might have exchanged the framebuffer while we dropped locks
2212 * in the backoff code. We need to fix up the fb refcount tracking the
2213 * core does for us.
2214 */
2215 plane->old_fb = plane->fb;
2216
2217 goto retry;
2218}
2219EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2220
Rob Clarkbbb1e522015-08-25 15:35:58 -04002221/* just used from fb-helper and atomic-helper: */
2222int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2223 struct drm_plane_state *plane_state)
2224{
2225 int ret;
2226
2227 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2228 if (ret != 0)
2229 return ret;
2230
2231 drm_atomic_set_fb_for_plane(plane_state, NULL);
2232 plane_state->crtc_x = 0;
2233 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002234 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002235 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002236 plane_state->src_x = 0;
2237 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002238 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002239 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002240
Rob Clarkbbb1e522015-08-25 15:35:58 -04002241 return 0;
2242}
2243
Daniel Vetter042652e2014-07-27 13:46:52 +02002244static int update_output_state(struct drm_atomic_state *state,
2245 struct drm_mode_set *set)
2246{
2247 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002248 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002249 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002250 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002251 struct drm_connector_state *new_conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002252 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002253
2254 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2255 state->acquire_ctx);
2256 if (ret)
2257 return ret;
2258
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002259 /* First disable all connectors on the target crtc. */
2260 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2261 if (ret)
2262 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002263
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002264 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2265 if (new_conn_state->crtc == set->crtc) {
2266 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Daniel Vetter042652e2014-07-27 13:46:52 +02002267 NULL);
2268 if (ret)
2269 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002270
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002271 /* Make sure legacy setCrtc always re-trains */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002272 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
Daniel Vetter042652e2014-07-27 13:46:52 +02002273 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002274 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002275
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002276 /* Then set all connectors from set->connectors on the target crtc */
2277 for (i = 0; i < set->num_connectors; i++) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002278 new_conn_state = drm_atomic_get_connector_state(state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002279 set->connectors[i]);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002280 if (IS_ERR(new_conn_state))
2281 return PTR_ERR(new_conn_state);
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002282
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002283 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002284 set->crtc);
2285 if (ret)
2286 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002287 }
2288
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002289 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002290 /* Don't update ->enable for the CRTC in the set_config request,
2291 * since a mismatch would indicate a bug in the upper layers.
2292 * The actual modeset code later on will catch any
2293 * inconsistencies here. */
2294 if (crtc == set->crtc)
2295 continue;
2296
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002297 if (!new_crtc_state->connector_mask) {
2298 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002299 NULL);
2300 if (ret < 0)
2301 return ret;
2302
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002303 new_crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002304 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002305 }
2306
2307 return 0;
2308}
2309
2310/**
2311 * drm_atomic_helper_set_config - set a new config from userspace
2312 * @set: mode set configuration
2313 *
2314 * Provides a default crtc set_config handler using the atomic driver interface.
2315 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002316 * NOTE: For backwards compatibility with old userspace this automatically
2317 * resets the "link-status" property to GOOD, to force any link
2318 * re-training. The SETCRTC ioctl does not define whether an update does
2319 * need a full modeset or just a plane update, hence we're allowed to do
2320 * that. See also drm_mode_connector_set_link_status_property().
2321 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002322 * Returns:
2323 * Returns 0 on success, negative errno numbers on failure.
2324 */
2325int drm_atomic_helper_set_config(struct drm_mode_set *set)
2326{
2327 struct drm_atomic_state *state;
2328 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002329 int ret = 0;
2330
2331 state = drm_atomic_state_alloc(crtc->dev);
2332 if (!state)
2333 return -ENOMEM;
2334
Maarten Lankhorst40616a22016-03-03 10:17:39 +01002335 state->legacy_set_config = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02002336 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2337retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04002338 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002339 if (ret != 0)
2340 goto fail;
2341
Daniel Vetter042652e2014-07-27 13:46:52 +02002342 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002343fail:
2344 if (ret == -EDEADLK)
2345 goto backoff;
2346
Chris Wilson08536952016-10-14 13:18:18 +01002347 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002348 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002349
Daniel Vetter042652e2014-07-27 13:46:52 +02002350backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002351 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002352 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002353
2354 /*
2355 * Someone might have exchanged the framebuffer while we dropped locks
2356 * in the backoff code. We need to fix up the fb refcount tracking the
2357 * core does for us.
2358 */
2359 crtc->primary->old_fb = crtc->primary->fb;
2360
2361 goto retry;
2362}
2363EXPORT_SYMBOL(drm_atomic_helper_set_config);
2364
Rob Clarkbbb1e522015-08-25 15:35:58 -04002365/* just used from fb-helper and atomic-helper: */
2366int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2367 struct drm_atomic_state *state)
2368{
2369 struct drm_crtc_state *crtc_state;
2370 struct drm_plane_state *primary_state;
2371 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002372 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002373 int ret;
2374
2375 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2376 if (IS_ERR(crtc_state))
2377 return PTR_ERR(crtc_state);
2378
2379 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2380 if (IS_ERR(primary_state))
2381 return PTR_ERR(primary_state);
2382
2383 if (!set->mode) {
2384 WARN_ON(set->fb);
2385 WARN_ON(set->num_connectors);
2386
2387 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2388 if (ret != 0)
2389 return ret;
2390
2391 crtc_state->active = false;
2392
2393 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2394 if (ret != 0)
2395 return ret;
2396
2397 drm_atomic_set_fb_for_plane(primary_state, NULL);
2398
2399 goto commit;
2400 }
2401
2402 WARN_ON(!set->fb);
2403 WARN_ON(!set->num_connectors);
2404
2405 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2406 if (ret != 0)
2407 return ret;
2408
2409 crtc_state->active = true;
2410
2411 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2412 if (ret != 0)
2413 return ret;
2414
Daniel Vetter196cd5d2017-01-25 07:26:56 +01002415 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Ville Syrjälä83926112015-11-16 17:02:34 +02002416
Rob Clarkbbb1e522015-08-25 15:35:58 -04002417 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2418 primary_state->crtc_x = 0;
2419 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02002420 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002421 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002422 primary_state->src_x = set->x << 16;
2423 primary_state->src_y = set->y << 16;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002424 if (drm_rotation_90_or_270(primary_state->rotation)) {
Ville Syrjälä83926112015-11-16 17:02:34 +02002425 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002426 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002427 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02002428 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002429 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002430 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04002431
2432commit:
2433 ret = update_output_state(state, set);
2434 if (ret)
2435 return ret;
2436
2437 return 0;
2438}
2439
Daniel Vetter042652e2014-07-27 13:46:52 +02002440/**
Thierry Reding14942762015-12-02 17:50:04 +01002441 * drm_atomic_helper_disable_all - disable all currently active outputs
2442 * @dev: DRM device
2443 * @ctx: lock acquisition context
2444 *
2445 * Loops through all connectors, finding those that aren't turned off and then
2446 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2447 * that they are connected to.
2448 *
2449 * This is used for example in suspend/resume to disable all currently active
Daniel Vetter18dddad2017-03-21 17:41:49 +01002450 * functions when suspending. If you just want to shut down everything at e.g.
2451 * driver unload, look at drm_atomic_helper_shutdown().
Thierry Reding14942762015-12-02 17:50:04 +01002452 *
2453 * Note that if callers haven't already acquired all modeset locks this might
2454 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2455 *
2456 * Returns:
2457 * 0 on success or a negative error code on failure.
2458 *
2459 * See also:
Daniel Vetter18dddad2017-03-21 17:41:49 +01002460 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
2461 * drm_atomic_helper_shutdown().
Thierry Reding14942762015-12-02 17:50:04 +01002462 */
2463int drm_atomic_helper_disable_all(struct drm_device *dev,
2464 struct drm_modeset_acquire_ctx *ctx)
2465{
2466 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002467 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01002468 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002469 struct drm_plane_state *plane_state;
2470 struct drm_plane *plane;
2471 struct drm_crtc_state *crtc_state;
2472 struct drm_crtc *crtc;
2473 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01002474
2475 state = drm_atomic_state_alloc(dev);
2476 if (!state)
2477 return -ENOMEM;
2478
2479 state->acquire_ctx = ctx;
2480
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002481 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01002482 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2483 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002484 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01002485 goto free;
2486 }
2487
2488 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002489
2490 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2491 if (ret < 0)
2492 goto free;
2493
2494 ret = drm_atomic_add_affected_planes(state, crtc);
2495 if (ret < 0)
2496 goto free;
2497
2498 ret = drm_atomic_add_affected_connectors(state, crtc);
2499 if (ret < 0)
2500 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01002501 }
2502
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002503 for_each_connector_in_state(state, conn, conn_state, i) {
2504 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2505 if (ret < 0)
2506 goto free;
2507 }
2508
2509 for_each_plane_in_state(state, plane, plane_state, i) {
2510 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2511 if (ret < 0)
2512 goto free;
2513
2514 drm_atomic_set_fb_for_plane(plane_state, NULL);
2515 }
2516
2517 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01002518free:
Chris Wilson08536952016-10-14 13:18:18 +01002519 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002520 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01002521}
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002522
Thierry Reding14942762015-12-02 17:50:04 +01002523EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2524
2525/**
Daniel Vetter18dddad2017-03-21 17:41:49 +01002526 * drm_atomic_helper_shutdown - shutdown all CRTC
2527 * @dev: DRM device
2528 *
2529 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
2530 * suspend should instead be handled with drm_atomic_helper_suspend(), since
2531 * that also takes a snapshot of the modeset state to be restored on resume.
2532 *
2533 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
2534 * and it is the atomic version of drm_crtc_force_disable_all().
2535 */
2536void drm_atomic_helper_shutdown(struct drm_device *dev)
2537{
2538 struct drm_modeset_acquire_ctx ctx;
2539 int ret;
2540
2541 drm_modeset_acquire_init(&ctx, 0);
2542 while (1) {
2543 ret = drm_modeset_lock_all_ctx(dev, &ctx);
2544 if (!ret)
2545 ret = drm_atomic_helper_disable_all(dev, &ctx);
2546
2547 if (ret != -EDEADLK)
2548 break;
2549
2550 drm_modeset_backoff(&ctx);
2551 }
2552
2553 if (ret)
2554 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
2555
2556 drm_modeset_drop_locks(&ctx);
2557 drm_modeset_acquire_fini(&ctx);
2558}
2559EXPORT_SYMBOL(drm_atomic_helper_shutdown);
2560
2561/**
Thierry Reding14942762015-12-02 17:50:04 +01002562 * drm_atomic_helper_suspend - subsystem-level suspend helper
2563 * @dev: DRM device
2564 *
2565 * Duplicates the current atomic state, disables all active outputs and then
2566 * returns a pointer to the original atomic state to the caller. Drivers can
2567 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2568 * restore the output configuration that was active at the time the system
2569 * entered suspend.
2570 *
2571 * Note that it is potentially unsafe to use this. The atomic state object
2572 * returned by this function is assumed to be persistent. Drivers must ensure
2573 * that this holds true. Before calling this function, drivers must make sure
2574 * to suspend fbdev emulation so that nothing can be using the device.
2575 *
2576 * Returns:
2577 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2578 * encoded error code on failure. Drivers should store the returned atomic
2579 * state object and pass it to the drm_atomic_helper_resume() helper upon
2580 * resume.
2581 *
2582 * See also:
2583 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002584 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01002585 */
2586struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2587{
2588 struct drm_modeset_acquire_ctx ctx;
2589 struct drm_atomic_state *state;
2590 int err;
2591
2592 drm_modeset_acquire_init(&ctx, 0);
2593
2594retry:
2595 err = drm_modeset_lock_all_ctx(dev, &ctx);
2596 if (err < 0) {
2597 state = ERR_PTR(err);
2598 goto unlock;
2599 }
2600
2601 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2602 if (IS_ERR(state))
2603 goto unlock;
2604
2605 err = drm_atomic_helper_disable_all(dev, &ctx);
2606 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01002607 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01002608 state = ERR_PTR(err);
2609 goto unlock;
2610 }
2611
2612unlock:
2613 if (PTR_ERR(state) == -EDEADLK) {
2614 drm_modeset_backoff(&ctx);
2615 goto retry;
2616 }
2617
2618 drm_modeset_drop_locks(&ctx);
2619 drm_modeset_acquire_fini(&ctx);
2620 return state;
2621}
2622EXPORT_SYMBOL(drm_atomic_helper_suspend);
2623
2624/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002625 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2626 * @state: duplicated atomic state to commit
2627 * @ctx: pointer to acquire_ctx to use for commit.
2628 *
2629 * The state returned by drm_atomic_helper_duplicate_state() and
2630 * drm_atomic_helper_suspend() is partially invalid, and needs to
2631 * be fixed up before commit.
2632 *
2633 * Returns:
2634 * 0 on success or a negative error code on failure.
2635 *
2636 * See also:
2637 * drm_atomic_helper_suspend()
2638 */
2639int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2640 struct drm_modeset_acquire_ctx *ctx)
2641{
2642 int i;
2643 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002644 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002645 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002646 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002647 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002648 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002649
2650 state->acquire_ctx = ctx;
2651
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002652 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002653 state->planes[i].old_state = plane->state;
2654
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002655 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002656 state->crtcs[i].old_state = crtc->state;
2657
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002658 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002659 state->connectors[i].old_state = connector->state;
2660
2661 return drm_atomic_commit(state);
2662}
2663EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2664
2665/**
Thierry Reding14942762015-12-02 17:50:04 +01002666 * drm_atomic_helper_resume - subsystem-level resume helper
2667 * @dev: DRM device
2668 * @state: atomic state to resume to
2669 *
2670 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2671 * grabs all modeset locks and commits the atomic state object. This can be
2672 * used in conjunction with the drm_atomic_helper_suspend() helper to
2673 * implement suspend/resume for drivers that support atomic mode-setting.
2674 *
2675 * Returns:
2676 * 0 on success or a negative error code on failure.
2677 *
2678 * See also:
2679 * drm_atomic_helper_suspend()
2680 */
2681int drm_atomic_helper_resume(struct drm_device *dev,
2682 struct drm_atomic_state *state)
2683{
2684 struct drm_mode_config *config = &dev->mode_config;
2685 int err;
2686
2687 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002688
Thierry Reding14942762015-12-02 17:50:04 +01002689 drm_modeset_lock_all(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002690 err = drm_atomic_helper_commit_duplicated_state(state, config->acquire_ctx);
Thierry Reding14942762015-12-02 17:50:04 +01002691 drm_modeset_unlock_all(dev);
2692
2693 return err;
2694}
2695EXPORT_SYMBOL(drm_atomic_helper_resume);
2696
2697/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002698 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002699 * @crtc: DRM crtc
2700 * @property: DRM property
2701 * @val: value of property
2702 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002703 * Provides a default crtc set_property handler using the atomic driver
2704 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002705 *
2706 * RETURNS:
2707 * Zero on success, error code on failure
2708 */
2709int
2710drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2711 struct drm_property *property,
2712 uint64_t val)
2713{
2714 struct drm_atomic_state *state;
2715 struct drm_crtc_state *crtc_state;
2716 int ret = 0;
2717
2718 state = drm_atomic_state_alloc(crtc->dev);
2719 if (!state)
2720 return -ENOMEM;
2721
2722 /* ->set_property is always called with all locks held. */
2723 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2724retry:
2725 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2726 if (IS_ERR(crtc_state)) {
2727 ret = PTR_ERR(crtc_state);
2728 goto fail;
2729 }
2730
Rob Clark40ecc692014-12-18 16:01:46 -05002731 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2732 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002733 if (ret)
2734 goto fail;
2735
2736 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002737fail:
2738 if (ret == -EDEADLK)
2739 goto backoff;
2740
Chris Wilson08536952016-10-14 13:18:18 +01002741 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002742 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002743
Daniel Vetter042652e2014-07-27 13:46:52 +02002744backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002745 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002746 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002747
2748 goto retry;
2749}
2750EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2751
2752/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002753 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002754 * @plane: DRM plane
2755 * @property: DRM property
2756 * @val: value of property
2757 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002758 * Provides a default plane set_property handler using the atomic driver
2759 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002760 *
2761 * RETURNS:
2762 * Zero on success, error code on failure
2763 */
2764int
2765drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2766 struct drm_property *property,
2767 uint64_t val)
2768{
2769 struct drm_atomic_state *state;
2770 struct drm_plane_state *plane_state;
2771 int ret = 0;
2772
2773 state = drm_atomic_state_alloc(plane->dev);
2774 if (!state)
2775 return -ENOMEM;
2776
2777 /* ->set_property is always called with all locks held. */
2778 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2779retry:
2780 plane_state = drm_atomic_get_plane_state(state, plane);
2781 if (IS_ERR(plane_state)) {
2782 ret = PTR_ERR(plane_state);
2783 goto fail;
2784 }
2785
Rob Clark40ecc692014-12-18 16:01:46 -05002786 ret = drm_atomic_plane_set_property(plane, plane_state,
2787 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002788 if (ret)
2789 goto fail;
2790
2791 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002792fail:
2793 if (ret == -EDEADLK)
2794 goto backoff;
2795
Chris Wilson08536952016-10-14 13:18:18 +01002796 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002797 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002798
Daniel Vetter042652e2014-07-27 13:46:52 +02002799backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002800 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002801 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002802
2803 goto retry;
2804}
2805EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2806
2807/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002808 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002809 * @connector: DRM connector
2810 * @property: DRM property
2811 * @val: value of property
2812 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002813 * Provides a default connector set_property handler using the atomic driver
2814 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002815 *
2816 * RETURNS:
2817 * Zero on success, error code on failure
2818 */
2819int
2820drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2821 struct drm_property *property,
2822 uint64_t val)
2823{
2824 struct drm_atomic_state *state;
2825 struct drm_connector_state *connector_state;
2826 int ret = 0;
2827
2828 state = drm_atomic_state_alloc(connector->dev);
2829 if (!state)
2830 return -ENOMEM;
2831
2832 /* ->set_property is always called with all locks held. */
2833 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2834retry:
2835 connector_state = drm_atomic_get_connector_state(state, connector);
2836 if (IS_ERR(connector_state)) {
2837 ret = PTR_ERR(connector_state);
2838 goto fail;
2839 }
2840
Rob Clark40ecc692014-12-18 16:01:46 -05002841 ret = drm_atomic_connector_set_property(connector, connector_state,
2842 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002843 if (ret)
2844 goto fail;
2845
2846 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002847fail:
2848 if (ret == -EDEADLK)
2849 goto backoff;
2850
Chris Wilson08536952016-10-14 13:18:18 +01002851 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002852 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002853
Daniel Vetter042652e2014-07-27 13:46:52 +02002854backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002855 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002856 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002857
2858 goto retry;
2859}
2860EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002861
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002862static int page_flip_common(
2863 struct drm_atomic_state *state,
2864 struct drm_crtc *crtc,
2865 struct drm_framebuffer *fb,
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002866 struct drm_pending_vblank_event *event,
2867 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002868{
2869 struct drm_plane *plane = crtc->primary;
2870 struct drm_plane_state *plane_state;
2871 struct drm_crtc_state *crtc_state;
2872 int ret = 0;
2873
2874 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2875 if (IS_ERR(crtc_state))
2876 return PTR_ERR(crtc_state);
2877
2878 crtc_state->event = event;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002879 crtc_state->pageflip_flags = flags;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002880
2881 plane_state = drm_atomic_get_plane_state(state, plane);
2882 if (IS_ERR(plane_state))
2883 return PTR_ERR(plane_state);
2884
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002885 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2886 if (ret != 0)
2887 return ret;
2888 drm_atomic_set_fb_for_plane(plane_state, fb);
2889
2890 /* Make sure we don't accidentally do a full modeset. */
2891 state->allow_modeset = false;
2892 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00002893 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
2894 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002895 return -EINVAL;
2896 }
2897
2898 return ret;
2899}
2900
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002901/**
2902 * drm_atomic_helper_page_flip - execute a legacy page flip
2903 * @crtc: DRM crtc
2904 * @fb: DRM framebuffer
2905 * @event: optional DRM event to signal upon completion
2906 * @flags: flip flags for non-vblank sync'ed updates
2907 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002908 * Provides a default &drm_crtc_funcs.page_flip implementation
2909 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002910 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002911 * Returns:
2912 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002913 *
2914 * See also:
2915 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002916 */
2917int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2918 struct drm_framebuffer *fb,
2919 struct drm_pending_vblank_event *event,
2920 uint32_t flags)
2921{
2922 struct drm_plane *plane = crtc->primary;
2923 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002924 int ret = 0;
2925
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002926 state = drm_atomic_state_alloc(plane->dev);
2927 if (!state)
2928 return -ENOMEM;
2929
2930 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002931
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002932retry:
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002933 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002934 if (ret != 0)
2935 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01002936
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002937 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002938
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002939fail:
2940 if (ret == -EDEADLK)
2941 goto backoff;
2942
Chris Wilson08536952016-10-14 13:18:18 +01002943 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002944 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002945
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002946backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002947 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002948 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002949
2950 /*
2951 * Someone might have exchanged the framebuffer while we dropped locks
2952 * in the backoff code. We need to fix up the fb refcount tracking the
2953 * core does for us.
2954 */
2955 plane->old_fb = plane->fb;
2956
2957 goto retry;
2958}
2959EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002960
2961/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002962 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2963 * @crtc: DRM crtc
2964 * @fb: DRM framebuffer
2965 * @event: optional DRM event to signal upon completion
2966 * @flags: flip flags for non-vblank sync'ed updates
2967 * @target: specifying the target vblank period when the flip to take effect
2968 *
2969 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2970 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2971 * target vblank period to flip.
2972 *
2973 * Returns:
2974 * Returns 0 on success, negative errno numbers on failure.
2975 */
2976int drm_atomic_helper_page_flip_target(
2977 struct drm_crtc *crtc,
2978 struct drm_framebuffer *fb,
2979 struct drm_pending_vblank_event *event,
2980 uint32_t flags,
2981 uint32_t target)
2982{
2983 struct drm_plane *plane = crtc->primary;
2984 struct drm_atomic_state *state;
2985 struct drm_crtc_state *crtc_state;
2986 int ret = 0;
2987
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002988 state = drm_atomic_state_alloc(plane->dev);
2989 if (!state)
2990 return -ENOMEM;
2991
2992 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2993
2994retry:
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002995 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002996 if (ret != 0)
2997 goto fail;
2998
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01002999 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003000 if (WARN_ON(!crtc_state)) {
3001 ret = -EINVAL;
3002 goto fail;
3003 }
3004 crtc_state->target_vblank = target;
3005
3006 ret = drm_atomic_nonblocking_commit(state);
3007
3008fail:
3009 if (ret == -EDEADLK)
3010 goto backoff;
3011
3012 drm_atomic_state_put(state);
3013 return ret;
3014
3015backoff:
3016 drm_atomic_state_clear(state);
3017 drm_atomic_legacy_backoff(state);
3018
3019 /*
3020 * Someone might have exchanged the framebuffer while we dropped locks
3021 * in the backoff code. We need to fix up the fb refcount tracking the
3022 * core does for us.
3023 */
3024 plane->old_fb = plane->fb;
3025
3026 goto retry;
3027}
3028EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3029
3030/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003031 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
3032 * @connector: affected connector
3033 * @mode: DPMS mode
3034 *
3035 * This is the main helper function provided by the atomic helper framework for
3036 * implementing the legacy DPMS connector interface. It computes the new desired
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003037 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
3038 * enabled) and updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003039 *
3040 * Returns:
3041 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003042 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003043int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
3044 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003045{
3046 struct drm_mode_config *config = &connector->dev->mode_config;
3047 struct drm_atomic_state *state;
3048 struct drm_crtc_state *crtc_state;
3049 struct drm_crtc *crtc;
3050 struct drm_connector *tmp_connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003051 struct drm_connector_list_iter conn_iter;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003052 int ret;
3053 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003054 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003055
3056 if (mode != DRM_MODE_DPMS_ON)
3057 mode = DRM_MODE_DPMS_OFF;
3058
3059 connector->dpms = mode;
3060 crtc = connector->state->crtc;
3061
3062 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003063 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003064
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003065 state = drm_atomic_state_alloc(connector->dev);
3066 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003067 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003068
3069 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
3070retry:
3071 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003072 if (IS_ERR(crtc_state)) {
3073 ret = PTR_ERR(crtc_state);
3074 goto fail;
3075 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003076
3077 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
3078
Thierry Redingb982dab2017-02-28 15:46:43 +01003079 drm_connector_list_iter_begin(connector->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003080 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
John Hunter0388df02015-03-17 15:30:28 +08003081 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003082 continue;
3083
John Hunter0388df02015-03-17 15:30:28 +08003084 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003085 active = true;
3086 break;
3087 }
3088 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003089 drm_connector_list_iter_end(&conn_iter);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003090 crtc_state->active = active;
3091
3092 ret = drm_atomic_commit(state);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003093fail:
3094 if (ret == -EDEADLK)
3095 goto backoff;
Marta Lofstedt8f5040e2016-12-05 14:04:08 +02003096 if (ret != 0)
3097 connector->dpms = old_mode;
Chris Wilson08536952016-10-14 13:18:18 +01003098 drm_atomic_state_put(state);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003099 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003100
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003101backoff:
3102 drm_atomic_state_clear(state);
3103 drm_atomic_legacy_backoff(state);
3104
3105 goto retry;
3106}
3107EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3108
3109/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003110 * drm_atomic_helper_best_encoder - Helper for
3111 * &drm_connector_helper_funcs.best_encoder callback
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003112 * @connector: Connector control structure
3113 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003114 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003115 * connectors that support exactly 1 encoder, statically determined at driver
3116 * init time.
3117 */
3118struct drm_encoder *
3119drm_atomic_helper_best_encoder(struct drm_connector *connector)
3120{
3121 WARN_ON(connector->encoder_ids[1]);
3122 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3123}
3124EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3125
3126/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003127 * DOC: atomic state reset and initialization
3128 *
3129 * Both the drm core and the atomic helpers assume that there is always the full
3130 * and correct atomic software state for all connectors, CRTCs and planes
3131 * available. Which is a bit a problem on driver load and also after system
3132 * suspend. One way to solve this is to have a hardware state read-out
3133 * infrastructure which reconstructs the full software state (e.g. the i915
3134 * driver).
3135 *
3136 * The simpler solution is to just reset the software state to everything off,
3137 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3138 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01003139 *
3140 * On the upside the precise state tracking of atomic simplifies system suspend
3141 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3142 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3143 * For other drivers the building blocks are split out, see the documentation
3144 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003145 */
3146
3147/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003148 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
Daniel Vetterd4617012014-11-03 15:56:43 +01003149 * @crtc: drm CRTC
3150 *
3151 * Resets the atomic state for @crtc by freeing the state pointer (which might
3152 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3153 */
3154void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3155{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003156 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003157 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003158
Daniel Vetterd4617012014-11-03 15:56:43 +01003159 kfree(crtc->state);
3160 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003161
3162 if (crtc->state)
3163 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01003164}
3165EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3166
3167/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003168 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3169 * @crtc: CRTC object
3170 * @state: atomic CRTC state
3171 *
3172 * Copies atomic state from a CRTC's current state and resets inferred values.
3173 * This is useful for drivers that subclass the CRTC state.
3174 */
3175void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3176 struct drm_crtc_state *state)
3177{
3178 memcpy(state, crtc->state, sizeof(*state));
3179
Daniel Stone99cf4a22015-05-25 19:11:51 +01003180 if (state->mode_blob)
Thierry Reding6472e502017-02-28 15:46:42 +01003181 drm_property_blob_get(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003182 if (state->degamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003183 drm_property_blob_get(state->degamma_lut);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003184 if (state->ctm)
Thierry Reding6472e502017-02-28 15:46:42 +01003185 drm_property_blob_get(state->ctm);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003186 if (state->gamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003187 drm_property_blob_get(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003188 state->mode_changed = false;
3189 state->active_changed = false;
3190 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02003191 state->connectors_changed = false;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003192 state->color_mgmt_changed = false;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02003193 state->zpos_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01003194 state->event = NULL;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003195 state->pageflip_flags = 0;
Thierry Redingf5e78402015-01-28 14:54:32 +01003196}
3197EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3198
3199/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003200 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3201 * @crtc: drm CRTC
3202 *
3203 * Default CRTC state duplicate hook for drivers which don't have their own
3204 * subclassed CRTC state structure.
3205 */
3206struct drm_crtc_state *
3207drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3208{
3209 struct drm_crtc_state *state;
3210
3211 if (WARN_ON(!crtc->state))
3212 return NULL;
3213
Thierry Redingf5e78402015-01-28 14:54:32 +01003214 state = kmalloc(sizeof(*state), GFP_KERNEL);
3215 if (state)
3216 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003217
3218 return state;
3219}
3220EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3221
3222/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003223 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
Thierry Redingf5e78402015-01-28 14:54:32 +01003224 * @state: CRTC state object to release
3225 *
3226 * Releases all resources stored in the CRTC state without actually freeing
3227 * the memory of the CRTC state. This is useful for drivers that subclass the
3228 * CRTC state.
3229 */
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003230void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003231{
Thierry Reding6472e502017-02-28 15:46:42 +01003232 drm_property_blob_put(state->mode_blob);
3233 drm_property_blob_put(state->degamma_lut);
3234 drm_property_blob_put(state->ctm);
3235 drm_property_blob_put(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003236}
3237EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3238
3239/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003240 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3241 * @crtc: drm CRTC
3242 * @state: CRTC state object to release
3243 *
3244 * Default CRTC state destroy hook for drivers which don't have their own
3245 * subclassed CRTC state structure.
3246 */
3247void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3248 struct drm_crtc_state *state)
3249{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003250 __drm_atomic_helper_crtc_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003251 kfree(state);
3252}
3253EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3254
3255/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003256 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
Daniel Vetterd4617012014-11-03 15:56:43 +01003257 * @plane: drm plane
3258 *
3259 * Resets the atomic state for @plane by freeing the state pointer (which might
3260 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3261 */
3262void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3263{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003264 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +02003265 __drm_atomic_helper_plane_destroy_state(plane->state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003266
Daniel Vetterd4617012014-11-03 15:56:43 +01003267 kfree(plane->state);
3268 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003269
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003270 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003271 plane->state->plane = plane;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003272 plane->state->rotation = DRM_ROTATE_0;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003273 }
Daniel Vetterd4617012014-11-03 15:56:43 +01003274}
3275EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3276
3277/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003278 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3279 * @plane: plane object
3280 * @state: atomic plane state
3281 *
3282 * Copies atomic state from a plane's current state. This is useful for
3283 * drivers that subclass the plane state.
3284 */
3285void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3286 struct drm_plane_state *state)
3287{
3288 memcpy(state, plane->state, sizeof(*state));
3289
3290 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003291 drm_framebuffer_get(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003292
3293 state->fence = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003294}
3295EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3296
3297/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003298 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3299 * @plane: drm plane
3300 *
3301 * Default plane state duplicate hook for drivers which don't have their own
3302 * subclassed plane state structure.
3303 */
3304struct drm_plane_state *
3305drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3306{
Daniel Vetter321ebf02014-11-04 22:57:27 +01003307 struct drm_plane_state *state;
3308
Daniel Vetterd4617012014-11-03 15:56:43 +01003309 if (WARN_ON(!plane->state))
3310 return NULL;
3311
Thierry Redingf5e78402015-01-28 14:54:32 +01003312 state = kmalloc(sizeof(*state), GFP_KERNEL);
3313 if (state)
3314 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003315
3316 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003317}
3318EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3319
3320/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003321 * __drm_atomic_helper_plane_destroy_state - release plane state
Thierry Redingf5e78402015-01-28 14:54:32 +01003322 * @state: plane state object to release
3323 *
3324 * Releases all resources stored in the plane state without actually freeing
3325 * the memory of the plane state. This is useful for drivers that subclass the
3326 * plane state.
3327 */
Daniel Vetter2f701692016-05-09 16:34:10 +02003328void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003329{
3330 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003331 drm_framebuffer_put(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003332
3333 if (state->fence)
3334 dma_fence_put(state->fence);
Thierry Redingf5e78402015-01-28 14:54:32 +01003335}
3336EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3337
3338/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003339 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3340 * @plane: drm plane
3341 * @state: plane state object to release
3342 *
3343 * Default plane state destroy hook for drivers which don't have their own
3344 * subclassed plane state structure.
3345 */
3346void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01003347 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01003348{
Daniel Vetter2f701692016-05-09 16:34:10 +02003349 __drm_atomic_helper_plane_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003350 kfree(state);
3351}
3352EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3353
3354/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003355 * __drm_atomic_helper_connector_reset - reset state on connector
3356 * @connector: drm connector
3357 * @conn_state: connector state to assign
3358 *
3359 * Initializes the newly allocated @conn_state and assigns it to
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003360 * the &drm_conector->state pointer of @connector, usually required when
3361 * initializing the drivers or when called from the &drm_connector_funcs.reset
3362 * hook.
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003363 *
3364 * This is useful for drivers that subclass the connector state.
3365 */
3366void
3367__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3368 struct drm_connector_state *conn_state)
3369{
3370 if (conn_state)
3371 conn_state->connector = connector;
3372
3373 connector->state = conn_state;
3374}
3375EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3376
3377/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003378 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
Daniel Vetterd4617012014-11-03 15:56:43 +01003379 * @connector: drm connector
3380 *
3381 * Resets the atomic state for @connector by freeing the state pointer (which
3382 * might be NULL, e.g. at driver load time) and allocating a new empty state
3383 * object.
3384 */
3385void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3386{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003387 struct drm_connector_state *conn_state =
3388 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003389
Daniel Vetterb0b55112016-04-22 22:10:29 +02003390 if (connector->state)
Daniel Vetterfabd9102016-05-09 16:34:11 +02003391 __drm_atomic_helper_connector_destroy_state(connector->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003392
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003393 kfree(connector->state);
3394 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003395}
3396EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3397
3398/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003399 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3400 * @connector: connector object
3401 * @state: atomic connector state
3402 *
3403 * Copies atomic state from a connector's current state. This is useful for
3404 * drivers that subclass the connector state.
3405 */
3406void
3407__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3408 struct drm_connector_state *state)
3409{
3410 memcpy(state, connector->state, sizeof(*state));
Dave Airlied2307de2016-04-27 11:27:39 +10003411 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003412 drm_connector_get(connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003413}
3414EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3415
3416/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003417 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3418 * @connector: drm connector
3419 *
3420 * Default connector state duplicate hook for drivers which don't have their own
3421 * subclassed connector state structure.
3422 */
3423struct drm_connector_state *
3424drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3425{
Thierry Redingf5e78402015-01-28 14:54:32 +01003426 struct drm_connector_state *state;
3427
Daniel Vetterd4617012014-11-03 15:56:43 +01003428 if (WARN_ON(!connector->state))
3429 return NULL;
3430
Thierry Redingf5e78402015-01-28 14:54:32 +01003431 state = kmalloc(sizeof(*state), GFP_KERNEL);
3432 if (state)
3433 __drm_atomic_helper_connector_duplicate_state(connector, state);
3434
3435 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003436}
3437EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3438
3439/**
Thierry Reding397fd772015-09-08 15:00:45 +02003440 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3441 * @dev: DRM device
3442 * @ctx: lock acquisition context
3443 *
3444 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01003445 * duplicating their respective states. This is used for example by suspend/
3446 * resume support code to save the state prior to suspend such that it can
3447 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02003448 *
3449 * Note that this treats atomic state as persistent between save and restore.
3450 * Drivers must make sure that this is possible and won't result in confusion
3451 * or erroneous behaviour.
3452 *
3453 * Note that if callers haven't already acquired all modeset locks this might
3454 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3455 *
3456 * Returns:
3457 * A pointer to the copy of the atomic state object on success or an
3458 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01003459 *
3460 * See also:
3461 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02003462 */
3463struct drm_atomic_state *
3464drm_atomic_helper_duplicate_state(struct drm_device *dev,
3465 struct drm_modeset_acquire_ctx *ctx)
3466{
3467 struct drm_atomic_state *state;
3468 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003469 struct drm_connector_list_iter conn_iter;
Thierry Reding397fd772015-09-08 15:00:45 +02003470 struct drm_plane *plane;
3471 struct drm_crtc *crtc;
3472 int err = 0;
3473
3474 state = drm_atomic_state_alloc(dev);
3475 if (!state)
3476 return ERR_PTR(-ENOMEM);
3477
3478 state->acquire_ctx = ctx;
3479
3480 drm_for_each_crtc(crtc, dev) {
3481 struct drm_crtc_state *crtc_state;
3482
3483 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3484 if (IS_ERR(crtc_state)) {
3485 err = PTR_ERR(crtc_state);
3486 goto free;
3487 }
3488 }
3489
3490 drm_for_each_plane(plane, dev) {
3491 struct drm_plane_state *plane_state;
3492
3493 plane_state = drm_atomic_get_plane_state(state, plane);
3494 if (IS_ERR(plane_state)) {
3495 err = PTR_ERR(plane_state);
3496 goto free;
3497 }
3498 }
3499
Thierry Redingb982dab2017-02-28 15:46:43 +01003500 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003501 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding397fd772015-09-08 15:00:45 +02003502 struct drm_connector_state *conn_state;
3503
3504 conn_state = drm_atomic_get_connector_state(state, conn);
3505 if (IS_ERR(conn_state)) {
3506 err = PTR_ERR(conn_state);
Thierry Redingb982dab2017-02-28 15:46:43 +01003507 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003508 goto free;
3509 }
3510 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003511 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003512
3513 /* clear the acquire context so that it isn't accidentally reused */
3514 state->acquire_ctx = NULL;
3515
3516free:
3517 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003518 drm_atomic_state_put(state);
Thierry Reding397fd772015-09-08 15:00:45 +02003519 state = ERR_PTR(err);
3520 }
3521
3522 return state;
3523}
3524EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3525
3526/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003527 * __drm_atomic_helper_connector_destroy_state - release connector state
Thierry Redingf5e78402015-01-28 14:54:32 +01003528 * @state: connector state object to release
3529 *
3530 * Releases all resources stored in the connector state without actually
3531 * freeing the memory of the connector state. This is useful for drivers that
3532 * subclass the connector state.
3533 */
3534void
Daniel Vetterfabd9102016-05-09 16:34:11 +02003535__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003536{
Dave Airlied2307de2016-04-27 11:27:39 +10003537 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003538 drm_connector_put(state->connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003539}
3540EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3541
3542/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003543 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3544 * @connector: drm connector
3545 * @state: connector state object to release
3546 *
3547 * Default connector state destroy hook for drivers which don't have their own
3548 * subclassed connector state structure.
3549 */
3550void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3551 struct drm_connector_state *state)
3552{
Daniel Vetterfabd9102016-05-09 16:34:11 +02003553 __drm_atomic_helper_connector_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003554 kfree(state);
3555}
3556EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003557
3558/**
3559 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3560 * @crtc: CRTC object
3561 * @red: red correction table
3562 * @green: green correction table
3563 * @blue: green correction table
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003564 * @size: size of the tables
3565 *
3566 * Implements support for legacy gamma correction table for drivers
3567 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3568 * properties.
3569 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003570int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3571 u16 *red, u16 *green, u16 *blue,
3572 uint32_t size)
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003573{
3574 struct drm_device *dev = crtc->dev;
3575 struct drm_mode_config *config = &dev->mode_config;
3576 struct drm_atomic_state *state;
3577 struct drm_crtc_state *crtc_state;
3578 struct drm_property_blob *blob = NULL;
3579 struct drm_color_lut *blob_data;
3580 int i, ret = 0;
3581
3582 state = drm_atomic_state_alloc(crtc->dev);
3583 if (!state)
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003584 return -ENOMEM;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003585
3586 blob = drm_property_create_blob(dev,
3587 sizeof(struct drm_color_lut) * size,
3588 NULL);
Lionel Landwerlin562c5b42016-03-10 12:04:21 +00003589 if (IS_ERR(blob)) {
3590 ret = PTR_ERR(blob);
Lionel Landwerlinc1f415c2016-03-11 12:17:26 +00003591 blob = NULL;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003592 goto fail;
3593 }
3594
3595 /* Prepare GAMMA_LUT with the legacy values. */
3596 blob_data = (struct drm_color_lut *) blob->data;
3597 for (i = 0; i < size; i++) {
3598 blob_data[i].red = red[i];
3599 blob_data[i].green = green[i];
3600 blob_data[i].blue = blue[i];
3601 }
3602
3603 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3604retry:
3605 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3606 if (IS_ERR(crtc_state)) {
3607 ret = PTR_ERR(crtc_state);
3608 goto fail;
3609 }
3610
3611 /* Reset DEGAMMA_LUT and CTM properties. */
3612 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3613 config->degamma_lut_property, 0);
3614 if (ret)
3615 goto fail;
3616
3617 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3618 config->ctm_property, 0);
3619 if (ret)
3620 goto fail;
3621
3622 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3623 config->gamma_lut_property, blob->base.id);
3624 if (ret)
3625 goto fail;
3626
3627 ret = drm_atomic_commit(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003628fail:
3629 if (ret == -EDEADLK)
3630 goto backoff;
3631
Chris Wilson08536952016-10-14 13:18:18 +01003632 drm_atomic_state_put(state);
Thierry Reding6472e502017-02-28 15:46:42 +01003633 drm_property_blob_put(blob);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003634 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003635
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003636backoff:
3637 drm_atomic_state_clear(state);
3638 drm_atomic_legacy_backoff(state);
3639
3640 goto retry;
3641}
3642EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);