blob: cbd1332c20d10bef722b4d89cc5ca719d3d8cf5f [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,
66 struct drm_plane_state *plane_state,
67 struct drm_plane *plane)
68{
69 struct drm_crtc_state *crtc_state;
70
71 if (plane->state->crtc) {
Andrzej Hajda2943ef32016-03-15 13:46:00 +010072 crtc_state = drm_atomic_get_existing_crtc_state(state,
73 plane->state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010074
75 if (WARN_ON(!crtc_state))
76 return;
77
78 crtc_state->planes_changed = true;
79 }
80
81 if (plane_state->crtc) {
Andrzej Hajda2943ef32016-03-15 13:46:00 +010082 crtc_state = drm_atomic_get_existing_crtc_state(state,
83 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{
Daniel Vetter97ac3202015-12-03 10:49:14 +010095 struct drm_connector_state *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 Lankhorst40616a22016-03-03 10:17:39 +0100107 for_each_connector_in_state(state, connector, conn_state, i) {
108 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 Lankhorst40616a22016-03-03 10:17:39 +0100111 if (!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)
115 new_encoder = funcs->atomic_best_encoder(connector, 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 */
Daniel Vetterc36a3252016-12-15 16:58:43 +0100148 drm_connector_list_iter_get(state->dev, &conn_iter);
149 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100150 struct drm_crtc_state *crtc_state;
151
152 if (drm_atomic_get_existing_connector_state(state, connector))
153 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 Lankhorst40616a22016-03-03 10:17:39 +0100169 conn_state = drm_atomic_get_connector_state(state, connector);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100170 if (IS_ERR(conn_state)) {
171 ret = PTR_ERR(conn_state);
172 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,
177 conn_state->crtc->base.id, conn_state->crtc->name,
178 connector->base.id, connector->name);
179
180 crtc_state = drm_atomic_get_existing_crtc_state(state, conn_state->crtc);
181
182 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
183 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:
196 drm_connector_list_iter_put(&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) {
221 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
222
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) {
232 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
233
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;
248 struct drm_connector_state *connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100249 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200250
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100251 for_each_connector_in_state(state, connector, 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 Lankhorste87a52b2016-01-28 15:04:58 +0100254 if (connector_state->best_encoder != encoder)
255 continue;
256
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100257 encoder_crtc = 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
Daniel Vetter623369e2014-09-16 17:50:47 +0200263 set_best_encoder(state, connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100264
265 crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
266 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,
275 struct drm_connector_state *connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200276{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200277 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200278 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200279 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200280
Daniel Vetter17a38d92015-02-22 12:24:16 +0100281 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
282 connector->base.id,
283 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200284
285 if (connector->state->crtc != connector_state->crtc) {
286 if (connector->state->crtc) {
Maarten Lankhorst9b8d1e52016-03-03 10:17:42 +0100287 crtc_state = drm_atomic_get_existing_crtc_state(state, connector->state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200288 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200289 }
290
291 if (connector_state->crtc) {
Maarten Lankhorst9b8d1e52016-03-03 10:17:42 +0100292 crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200293 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200294 }
295 }
296
297 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100298 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200299 connector->base.id,
300 connector->name);
301
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100302 set_best_encoder(state, connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200303
304 return 0;
305 }
306
307 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200308
309 if (funcs->atomic_best_encoder)
310 new_encoder = funcs->atomic_best_encoder(connector,
311 connector_state);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200312 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200313 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200314 else
315 new_encoder = drm_atomic_helper_best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200316
317 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100318 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
319 connector->base.id,
320 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200321 return -EINVAL;
322 }
323
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100324 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
325 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
326 new_encoder->base.id,
327 new_encoder->name,
328 connector_state->crtc->base.id);
329 return -EINVAL;
330 }
331
Daniel Vetter623369e2014-09-16 17:50:47 +0200332 if (new_encoder == connector_state->best_encoder) {
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100333 set_best_encoder(state, connector_state, new_encoder);
334
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200335 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100336 connector->base.id,
337 connector->name,
338 new_encoder->base.id,
339 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200340 connector_state->crtc->base.id,
341 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200342
343 return 0;
344 }
345
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100346 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200347
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100348 set_best_encoder(state, connector_state, new_encoder);
349
Maarten Lankhorst9b8d1e52016-03-03 10:17:42 +0100350 crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200351 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200352
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200353 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100354 connector->base.id,
355 connector->name,
356 new_encoder->base.id,
357 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200358 connector_state->crtc->base.id,
359 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200360
361 return 0;
362}
363
364static int
365mode_fixup(struct drm_atomic_state *state)
366{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300367 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200368 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300369 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200370 struct drm_connector_state *conn_state;
371 int i;
372 bool ret;
373
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300374 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200375 if (!crtc_state->mode_changed &&
376 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200377 continue;
378
379 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
380 }
381
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300382 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200383 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200384 struct drm_encoder *encoder;
385
Daniel Vetter623369e2014-09-16 17:50:47 +0200386 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
387
388 if (!conn_state->crtc || !conn_state->best_encoder)
389 continue;
390
Andrzej Hajda2943ef32016-03-15 13:46:00 +0100391 crtc_state = drm_atomic_get_existing_crtc_state(state,
392 conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200393
394 /*
395 * Each encoder has at most one connector (since we always steal
396 * it away), so we won't call ->mode_fixup twice.
397 */
398 encoder = conn_state->best_encoder;
399 funcs = encoder->helper_private;
400
Archit Taneja862e6862015-05-21 11:03:16 +0530401 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
402 &crtc_state->adjusted_mode);
403 if (!ret) {
404 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
405 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200406 }
407
Noralf Trønnes28276352016-05-11 18:09:20 +0200408 if (funcs && funcs->atomic_check) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100409 ret = funcs->atomic_check(encoder, crtc_state,
410 conn_state);
411 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100412 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
413 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100414 return ret;
415 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200416 } else if (funcs && funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100417 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
418 &crtc_state->adjusted_mode);
419 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100420 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
421 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100422 return -EINVAL;
423 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200424 }
425 }
426
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300427 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200428 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200429
Liu Yingf55f1702016-05-27 17:35:54 +0800430 if (!crtc_state->enable)
431 continue;
432
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200433 if (!crtc_state->mode_changed &&
434 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200435 continue;
436
437 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300438 if (!funcs->mode_fixup)
439 continue;
440
Daniel Vetter623369e2014-09-16 17:50:47 +0200441 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
442 &crtc_state->adjusted_mode);
443 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200444 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
445 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200446 return -EINVAL;
447 }
448 }
449
450 return 0;
451}
452
Daniel Vetterd9b13622014-11-26 16:57:41 +0100453/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800454 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100455 * @dev: DRM device
456 * @state: the driver state object
457 *
458 * Check the state object to see if the requested state is physically possible.
459 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200460 * update and adds any additional connectors needed for full modesets and calls
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100461 * down into &drm_crtc_helper_funcs.mode_fixup and
462 * &drm_encoder_helper_funcs.mode_fixup or
463 * &drm_encoder_helper_funcs.atomic_check functions of the driver backend.
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200464 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100465 * &drm_crtc_state.mode_changed is set when the input mode is changed.
466 * &drm_crtc_state.connectors_changed is set when a connector is added or
467 * removed from the crtc. &drm_crtc_state.active_changed is set when
468 * &drm_crtc_state.active changes, which is used for DPMS.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100469 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100470 *
471 * IMPORTANT:
472 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100473 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
474 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
475 * without a full modeset) _must_ call this function afterwards after that
476 * change. It is permitted to call this function multiple times for the same
477 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
478 * upon the adjusted dotclock for fifo space allocation and watermark
479 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100480 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200481 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100482 * Zero for success or -errno
483 */
484int
Rob Clark934ce1c2014-11-19 16:41:33 -0500485drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200486 struct drm_atomic_state *state)
487{
Daniel Vetter623369e2014-09-16 17:50:47 +0200488 struct drm_crtc *crtc;
489 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300490 struct drm_connector *connector;
491 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200492 int i, ret;
493
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300494 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200495 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200496 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
497 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200498 crtc_state->mode_changed = true;
499 }
500
501 if (crtc->state->enable != crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200502 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
503 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200504
505 /*
506 * For clarity this assignment is done here, but
507 * enable == 0 is only true when there are no
508 * connectors and a NULL mode.
509 *
510 * The other way around is true as well. enable != 0
511 * iff connectors are attached and a mode is set.
512 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200513 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200514 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200515 }
516 }
517
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100518 ret = handle_conflicting_encoders(state, state->legacy_set_config);
519 if (ret)
520 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100521
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300522 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200523 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100524 * This only sets crtc->connectors_changed for routing changes,
525 * drivers must set crtc->connectors_changed themselves when
526 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200527 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100528 ret = update_connector_routing(state, connector,
529 connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200530 if (ret)
531 return ret;
532 }
533
534 /*
535 * After all the routing has been prepared we need to add in any
536 * connector which is itself unchanged, but who's crtc changes it's
537 * configuration. This must be done before calling mode_fixup in case a
538 * crtc only changed its mode but has the same set of connectors.
539 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300540 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100541 bool has_connectors =
542 !!crtc_state->connector_mask;
Daniel Vetter623369e2014-09-16 17:50:47 +0200543
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100544 /*
545 * We must set ->active_changed after walking connectors for
546 * otherwise an update that only changes active would result in
547 * a full modeset because update_connector_routing force that.
548 */
549 if (crtc->state->active != crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200550 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
551 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100552 crtc_state->active_changed = true;
553 }
554
Daniel Vetter2465ff62015-06-18 09:58:55 +0200555 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100556 continue;
557
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200558 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
559 crtc->base.id, crtc->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100560 crtc_state->enable ? 'y' : 'n',
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200561 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200562
563 ret = drm_atomic_add_affected_connectors(state, crtc);
564 if (ret != 0)
565 return ret;
566
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200567 ret = drm_atomic_add_affected_planes(state, crtc);
568 if (ret != 0)
569 return ret;
570
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100571 if (crtc_state->enable != has_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200572 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
573 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200574
575 return -EINVAL;
576 }
577 }
578
579 return mode_fixup(state);
580}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100581EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200582
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100583/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800584 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100585 * @dev: DRM device
586 * @state: the driver state object
587 *
588 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100589 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100590 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
591 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100592 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100593 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200594 * updated planes.
595 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200596 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100597 * Zero for success or -errno
598 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100599int
600drm_atomic_helper_check_planes(struct drm_device *dev,
601 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100602{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300603 struct drm_crtc *crtc;
604 struct drm_crtc_state *crtc_state;
605 struct drm_plane *plane;
606 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100607 int i, ret = 0;
608
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300609 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200610 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100611
612 funcs = plane->helper_private;
613
614 drm_atomic_helper_plane_changed(state, plane_state, plane);
615
616 if (!funcs || !funcs->atomic_check)
617 continue;
618
619 ret = funcs->atomic_check(plane, plane_state);
620 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200621 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
622 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100623 return ret;
624 }
625 }
626
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300627 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200628 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100629
630 funcs = crtc->helper_private;
631
632 if (!funcs || !funcs->atomic_check)
633 continue;
634
Daniel Vetterbe9174a2016-06-02 00:06:24 +0200635 ret = funcs->atomic_check(crtc, crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100636 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200637 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
638 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100639 return ret;
640 }
641 }
642
Daniel Vetterd9b13622014-11-26 16:57:41 +0100643 return ret;
644}
645EXPORT_SYMBOL(drm_atomic_helper_check_planes);
646
647/**
648 * drm_atomic_helper_check - validate state object
649 * @dev: DRM device
650 * @state: the driver state object
651 *
652 * Check the state object to see if the requested state is physically possible.
653 * Only crtcs and planes have check callbacks, so for any additional (global)
654 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100655 * Drivers without such needs can directly use this as their
656 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100657 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100658 * This just wraps the two parts of the state checking for planes and modeset
659 * state in the default order: First it calls drm_atomic_helper_check_modeset()
660 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100661 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
662 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
663 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100664 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200665 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100666 * Zero for success or -errno
667 */
668int drm_atomic_helper_check(struct drm_device *dev,
669 struct drm_atomic_state *state)
670{
671 int ret;
672
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100673 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100674 if (ret)
675 return ret;
676
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100677 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500678 if (ret)
679 return ret;
680
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100681 return ret;
682}
683EXPORT_SYMBOL(drm_atomic_helper_check);
684
Daniel Vetter623369e2014-09-16 17:50:47 +0200685static void
686disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
687{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300688 struct drm_connector *connector;
689 struct drm_connector_state *old_conn_state;
690 struct drm_crtc *crtc;
691 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200692 int i;
693
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300694 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200695 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200696 struct drm_encoder *encoder;
697
Daniel Vetter623369e2014-09-16 17:50:47 +0200698 /* Shut down everything that's in the changeset and currently
699 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300700 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200701 continue;
702
Andrzej Hajda2943ef32016-03-15 13:46:00 +0100703 old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
704 old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100705
Daniel Vetter4218a322015-03-26 22:18:40 +0100706 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200707 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100708 continue;
709
Rob Clark46df9ad2014-11-20 15:40:36 -0500710 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200711
Rob Clark46df9ad2014-11-20 15:40:36 -0500712 /* We shouldn't get this far if we didn't previously have
713 * an encoder.. but WARN_ON() rather than explode.
714 */
715 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200716 continue;
717
718 funcs = encoder->helper_private;
719
Daniel Vetter17a38d92015-02-22 12:24:16 +0100720 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
721 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100722
Daniel Vetter623369e2014-09-16 17:50:47 +0200723 /*
724 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800725 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200726 */
Archit Taneja862e6862015-05-21 11:03:16 +0530727 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200728
729 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200730 if (funcs) {
731 if (connector->state->crtc && funcs->prepare)
732 funcs->prepare(encoder);
733 else if (funcs->disable)
734 funcs->disable(encoder);
735 else if (funcs->dpms)
736 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
737 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200738
Archit Taneja862e6862015-05-21 11:03:16 +0530739 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200740 }
741
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300742 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200743 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200744
745 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200746 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100747 continue;
748
749 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200750 continue;
751
752 funcs = crtc->helper_private;
753
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200754 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
755 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100756
757
Daniel Vetter623369e2014-09-16 17:50:47 +0200758 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100759 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200760 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +0800761 else if (funcs->atomic_disable)
762 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200763 else if (funcs->disable)
764 funcs->disable(crtc);
765 else
766 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
767 }
768}
769
Daniel Vetter4c18d302015-05-12 15:27:37 +0200770/**
771 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
772 * @dev: DRM device
773 * @old_state: atomic state object with old state structures
774 *
775 * This function updates all the various legacy modeset state pointers in
776 * connectors, encoders and crtcs. It also updates the timestamping constants
777 * used for precise vblank timestamps by calling
778 * drm_calc_timestamping_constants().
779 *
780 * Drivers can use this for building their own atomic commit if they don't have
781 * a pure helper-based modeset implementation.
782 */
783void
784drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
785 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200786{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300787 struct drm_connector *connector;
788 struct drm_connector_state *old_conn_state;
789 struct drm_crtc *crtc;
790 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200791 int i;
792
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200793 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300794 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200795 if (connector->encoder) {
796 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200797
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200798 connector->encoder->crtc = NULL;
799 connector->encoder = NULL;
800 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200801
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200802 crtc = connector->state->crtc;
803 if ((!crtc && old_conn_state->crtc) ||
804 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
805 struct drm_property *dpms_prop =
806 dev->mode_config.dpms_property;
807 int mode = DRM_MODE_DPMS_OFF;
808
809 if (crtc && crtc->state->active)
810 mode = DRM_MODE_DPMS_ON;
811
812 connector->dpms = mode;
813 drm_object_property_set_value(&connector->base,
814 dpms_prop, mode);
815 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200816 }
817
818 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300819 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
820 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200821 continue;
822
823 if (WARN_ON(!connector->state->best_encoder))
824 continue;
825
826 connector->encoder = connector->state->best_encoder;
827 connector->encoder->crtc = connector->state->crtc;
828 }
829
830 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300831 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200832 struct drm_plane *primary = crtc->primary;
833
Daniel Vetter623369e2014-09-16 17:50:47 +0200834 crtc->mode = crtc->state->mode;
835 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200836
837 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
838 primary->state->crtc == crtc) {
839 crtc->x = primary->state->src_x >> 16;
840 crtc->y = primary->state->src_y >> 16;
841 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200842
843 if (crtc->state->enable)
844 drm_calc_timestamping_constants(crtc,
845 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200846 }
847}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200848EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200849
850static void
851crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
852{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300853 struct drm_crtc *crtc;
854 struct drm_crtc_state *old_crtc_state;
855 struct drm_connector *connector;
856 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200857 int i;
858
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300859 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200860 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200861
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300862 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200863 continue;
864
865 funcs = crtc->helper_private;
866
Daniel Vetterc982bd92015-02-22 12:24:20 +0100867 if (crtc->state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200868 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
869 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100870
Daniel Vetter623369e2014-09-16 17:50:47 +0200871 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100872 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200873 }
874
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300875 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200876 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200877 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200878 struct drm_encoder *encoder;
879 struct drm_display_mode *mode, *adjusted_mode;
880
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300881 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200882 continue;
883
884 encoder = connector->state->best_encoder;
885 funcs = encoder->helper_private;
886 new_crtc_state = connector->state->crtc->state;
887 mode = &new_crtc_state->mode;
888 adjusted_mode = &new_crtc_state->adjusted_mode;
889
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100890 if (!new_crtc_state->mode_changed)
891 continue;
892
Daniel Vetter17a38d92015-02-22 12:24:16 +0100893 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
894 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100895
Daniel Vetter623369e2014-09-16 17:50:47 +0200896 /*
897 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800898 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200899 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200900 if (funcs && funcs->atomic_mode_set) {
901 funcs->atomic_mode_set(encoder, new_crtc_state,
902 connector->state);
903 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +0100904 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200905 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200906
Archit Taneja862e6862015-05-21 11:03:16 +0530907 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200908 }
909}
910
911/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100912 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200913 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100914 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200915 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100916 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200917 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100918 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300919 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100920 * drm_atomic_helper_commit_planes(), which is what the default commit function
921 * does. But drivers with different needs can group the modeset commits together
922 * and do the plane commits at the end. This is useful for drivers doing runtime
923 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200924 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100925void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
926 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200927{
Laurent Pincharta072f802015-02-22 12:24:18 +0100928 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200929
930 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
931
Laurent Pincharta072f802015-02-22 12:24:18 +0100932 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200933}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100934EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200935
936/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100937 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200938 * @dev: DRM device
939 * @old_state: atomic state object with old state structures
940 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100941 * This function enables all the outputs with the new configuration which had to
942 * be turned off for the update.
943 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300944 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100945 * drm_atomic_helper_commit_planes(), which is what the default commit function
946 * does. But drivers with different needs can group the modeset commits together
947 * and do the plane commits at the end. This is useful for drivers doing runtime
948 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200949 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100950void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
951 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200952{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300953 struct drm_crtc *crtc;
954 struct drm_crtc_state *old_crtc_state;
955 struct drm_connector *connector;
956 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200957 int i;
958
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300959 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200960 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200961
962 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200963 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100964 continue;
965
966 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200967 continue;
968
969 funcs = crtc->helper_private;
970
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100971 if (crtc->state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200972 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
973 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100974
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100975 if (funcs->enable)
976 funcs->enable(crtc);
977 else
978 funcs->commit(crtc);
979 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200980 }
981
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300982 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200983 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200984 struct drm_encoder *encoder;
985
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300986 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200987 continue;
988
Daniel Vetter4218a322015-03-26 22:18:40 +0100989 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200990 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100991 continue;
992
Daniel Vetter623369e2014-09-16 17:50:47 +0200993 encoder = connector->state->best_encoder;
994 funcs = encoder->helper_private;
995
Daniel Vetter17a38d92015-02-22 12:24:16 +0100996 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
997 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100998
Daniel Vetter623369e2014-09-16 17:50:47 +0200999 /*
1000 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001001 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001002 */
Archit Taneja862e6862015-05-21 11:03:16 +05301003 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001004
Noralf Trønnes28276352016-05-11 18:09:20 +02001005 if (funcs) {
1006 if (funcs->enable)
1007 funcs->enable(encoder);
1008 else if (funcs->commit)
1009 funcs->commit(encoder);
1010 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001011
Archit Taneja862e6862015-05-21 11:03:16 +05301012 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001013 }
1014}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001015EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001016
Rob Clark4c5b7f32016-03-18 19:14:55 -04001017/**
1018 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1019 * @dev: DRM device
1020 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001021 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1022 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001023 *
1024 * For implicit sync, driver should fish the exclusive fence out from the
1025 * incoming fb's and stash it in the drm_plane_state. This is called after
1026 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1027 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001028 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001029 * Note that @pre_swap is needed since the point where we block for fences moves
1030 * around depending upon whether an atomic commit is blocking or
1031 * non-blocking. For async commit all waiting needs to happen after
1032 * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1033 * to wait **before** we do anything that can't be easily rolled back. That is
1034 * before we call drm_atomic_helper_swap_state().
1035 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001036 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001037 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001038int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1039 struct drm_atomic_state *state,
1040 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001041{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001042 struct drm_plane *plane;
1043 struct drm_plane_state *plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001044 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001045
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001046 for_each_plane_in_state(state, plane, plane_state, i) {
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001047 if (!pre_swap)
1048 plane_state = plane->state;
1049
1050 if (!plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001051 continue;
1052
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001053 WARN_ON(!plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001054
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001055 /*
1056 * If waiting for fences pre-swap (ie: nonblock), userspace can
1057 * still interrupt the operation. Instead of blocking until the
1058 * timer expires, make the wait interruptible.
1059 */
Chris Wilsonf54d1862016-10-25 13:00:45 +01001060 ret = dma_fence_wait(plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001061 if (ret)
1062 return ret;
1063
Chris Wilsonf54d1862016-10-25 13:00:45 +01001064 dma_fence_put(plane_state->fence);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001065 plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001066 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001067
1068 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001069}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001070EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001071
John Keepingc2409062016-01-19 10:46:58 +00001072/**
Rob Clark5ee32292014-11-11 19:38:59 -05001073 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1074 * @dev: DRM device
1075 * @old_state: atomic state object with old state structures
1076 *
1077 * Helper to, after atomic commit, wait for vblanks on all effected
1078 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001079 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1080 * framebuffers have actually changed to optimize for the legacy cursor and
1081 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001082 */
1083void
1084drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1085 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001086{
1087 struct drm_crtc *crtc;
1088 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001089 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001090 unsigned crtc_mask = 0;
1091
1092 /*
1093 * Legacy cursor ioctls are completely unsynced, and userspace
1094 * relies on that (by doing tons of cursor updates).
1095 */
1096 if (old_state->legacy_cursor_update)
1097 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001098
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001099 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001100 struct drm_crtc_state *new_crtc_state = crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001101
Maarten Lankhorsta3fbb532016-12-08 14:45:27 +01001102 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
Daniel Vetterab58e332014-11-24 20:42:42 +01001103 continue;
1104
Daniel Vetter623369e2014-09-16 17:50:47 +02001105 ret = drm_crtc_vblank_get(crtc);
1106 if (ret != 0)
1107 continue;
1108
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001109 crtc_mask |= drm_crtc_mask(crtc);
1110 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001111 }
1112
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001113 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001114 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001115 continue;
1116
1117 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001118 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001119 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001120 msecs_to_jiffies(50));
1121
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001122 WARN(!ret, "[CRTC:%d] vblank wait timed out\n", crtc->base.id);
1123
Daniel Vetter623369e2014-09-16 17:50:47 +02001124 drm_crtc_vblank_put(crtc);
1125 }
1126}
Rob Clark5ee32292014-11-11 19:38:59 -05001127EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001128
1129/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001130 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001131 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001132 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001133 * This is the default implementation for the
1134 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
Daniel Vetter623369e2014-09-16 17:50:47 +02001135 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001136 * Note that the default ordering of how the various stages are called is to
1137 * match the legacy modeset helper library closest. One peculiarity of that is
1138 * that it doesn't mesh well with runtime PM at all.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001139 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001140 * For drivers supporting runtime PM the recommended sequence is instead ::
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001141 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001142 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001143 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001144 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001145 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001146 * drm_atomic_helper_commit_planes(dev, old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001147 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001148 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001149 * for committing the atomic update to hardware. See the kerneldoc entries for
1150 * these three functions for more details.
1151 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001152void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001153{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001154 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001155
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001156 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001157
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001158 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001159
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001160 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001161
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001162 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001163
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001164 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001165
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001166 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001167}
1168EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1169
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001170static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001171{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001172 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001173 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001174
1175 funcs = dev->mode_config.helper_private;
1176
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001177 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001178
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001179 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001180
1181 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001182 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001183 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001184 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001185
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001186 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001187
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001188 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001189}
1190
1191static void commit_work(struct work_struct *work)
1192{
1193 struct drm_atomic_state *state = container_of(work,
1194 struct drm_atomic_state,
1195 commit_work);
1196 commit_tail(state);
1197}
1198
1199/**
1200 * drm_atomic_helper_commit - commit validated state object
1201 * @dev: DRM device
1202 * @state: the driver state object
1203 * @nonblock: whether nonblocking behavior is requested.
1204 *
1205 * This function commits a with drm_atomic_helper_check() pre-validated state
1206 * object. This can still fail when e.g. the framebuffer reservation fails. This
1207 * function implements nonblocking commits, using
1208 * drm_atomic_helper_setup_commit() and related functions.
1209 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001210 * Committing the actual hardware state is done through the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001211 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1212 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001213 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001214 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001215 * Zero for success or -errno.
1216 */
1217int drm_atomic_helper_commit(struct drm_device *dev,
1218 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001219 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001220{
1221 int ret;
1222
Daniel Vettera095caa2016-06-08 17:15:36 +02001223 ret = drm_atomic_helper_setup_commit(state, nonblock);
1224 if (ret)
1225 return ret;
1226
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001227 INIT_WORK(&state->commit_work, commit_work);
1228
Daniel Vetter623369e2014-09-16 17:50:47 +02001229 ret = drm_atomic_helper_prepare_planes(dev, state);
1230 if (ret)
1231 return ret;
1232
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001233 if (!nonblock) {
1234 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001235 if (ret) {
1236 drm_atomic_helper_cleanup_planes(dev, state);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001237 return ret;
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001238 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001239 }
1240
Daniel Vetter623369e2014-09-16 17:50:47 +02001241 /*
1242 * This is the point of no return - everything below never fails except
1243 * when the hw goes bonghits. Which means we can commit the new state on
1244 * the software side now.
1245 */
1246
Daniel Vetter5e84c262016-06-10 00:06:32 +02001247 drm_atomic_helper_swap_state(state, true);
Daniel Vetter623369e2014-09-16 17:50:47 +02001248
1249 /*
1250 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001251 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001252 * that the asynchronous work has either been cancelled (if the driver
1253 * supports it, which at least requires that the framebuffers get
1254 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1255 * before the new state gets committed on the software side with
1256 * drm_atomic_helper_swap_state().
1257 *
1258 * This scheme allows new atomic state updates to be prepared and
1259 * checked in parallel to the asynchronous completion of the previous
1260 * update. Which is important since compositors need to figure out the
1261 * composition of the next frame right after having submitted the
1262 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001263 *
1264 * NOTE: Commit work has multiple phases, first hardware commit, then
1265 * cleanup. We want them to overlap, hence need system_unbound_wq to
1266 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001267 */
1268
Chris Wilson08536952016-10-14 13:18:18 +01001269 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001270 if (nonblock)
1271 queue_work(system_unbound_wq, &state->commit_work);
1272 else
1273 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001274
1275 return 0;
1276}
1277EXPORT_SYMBOL(drm_atomic_helper_commit);
1278
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001279/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001280 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001281 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001282 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001283 *
1284 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1285 * which commit needs to call which can fail, so we want to run it first and
1286 * synchronously.
1287 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001288 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001289 * might be affected the new state update. This can be done by either cancelling
1290 * or flushing the work items, depending upon whether the driver can deal with
1291 * cancelled updates. Note that it is important to ensure that the framebuffer
1292 * cleanup is still done when cancelling.
1293 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001294 * Asynchronous workers need to have sufficient parallelism to be able to run
1295 * different atomic commits on different CRTCs in parallel. The simplest way to
1296 * achive this is by running them on the &system_unbound_wq work queue. Note
1297 * that drivers are not required to split up atomic commits and run an
1298 * individual commit in parallel - userspace is supposed to do that if it cares.
1299 * But it might be beneficial to do that for modesets, since those necessarily
1300 * must be done as one global operation, and enabling or disabling a CRTC can
1301 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001302 *
1303 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001304 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001305 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001306 * while it's guaranteed that no relevant nonblocking worker runs means that
1307 * nonblocking workers do not need grab any locks. Actually they must not grab
1308 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001309 *
1310 * 4. Schedule a work item to do all subsequent steps, using the split-out
1311 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1312 * then cleaning up the framebuffers after the old framebuffer is no longer
1313 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001314 *
1315 * The above scheme is implemented in the atomic helper libraries in
1316 * drm_atomic_helper_commit() using a bunch of helper functions. See
1317 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001318 */
1319
Daniel Vettera095caa2016-06-08 17:15:36 +02001320static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1321{
1322 struct drm_crtc_commit *commit, *stall_commit = NULL;
1323 bool completed = true;
1324 int i;
1325 long ret = 0;
1326
1327 spin_lock(&crtc->commit_lock);
1328 i = 0;
1329 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1330 if (i == 0) {
1331 completed = try_wait_for_completion(&commit->flip_done);
1332 /* Userspace is not allowed to get ahead of the previous
1333 * commit with nonblocking ones. */
1334 if (!completed && nonblock) {
1335 spin_unlock(&crtc->commit_lock);
1336 return -EBUSY;
1337 }
1338 } else if (i == 1) {
1339 stall_commit = commit;
1340 drm_crtc_commit_get(stall_commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001341 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001342 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001343
1344 i++;
1345 }
1346 spin_unlock(&crtc->commit_lock);
1347
1348 if (!stall_commit)
1349 return 0;
1350
1351 /* We don't want to let commits get ahead of cleanup work too much,
1352 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1353 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001354 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001355 10*HZ);
1356 if (ret == 0)
1357 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1358 crtc->base.id, crtc->name);
1359
1360 drm_crtc_commit_put(stall_commit);
1361
1362 return ret < 0 ? ret : 0;
1363}
1364
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001365static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001366{
1367 struct drm_crtc_commit *commit = container_of(completion,
1368 typeof(*commit),
1369 flip_done);
1370
1371 drm_crtc_commit_put(commit);
1372}
1373
Daniel Vettera095caa2016-06-08 17:15:36 +02001374/**
1375 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1376 * @state: new modeset state to be committed
1377 * @nonblock: whether nonblocking behavior is requested.
1378 *
1379 * This function prepares @state to be used by the atomic helper's support for
1380 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001381 * should always call this function from their
1382 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001383 *
1384 * To be able to use this support drivers need to use a few more helper
1385 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1386 * actually committing the hardware state, and for nonblocking commits this call
1387 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1388 * and it's stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001389 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001390 *
1391 * Completion of the hardware commit step must be signalled using
1392 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1393 * to read or change any permanent software or hardware modeset state. The only
1394 * exception is state protected by other means than &drm_modeset_lock locks.
1395 * Only the free standing @state with pointers to the old state structures can
1396 * be inspected, e.g. to clean up old buffers using
1397 * drm_atomic_helper_cleanup_planes().
1398 *
1399 * At the very end, before cleaning up @state drivers must call
1400 * drm_atomic_helper_commit_cleanup_done().
1401 *
1402 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1403 * complete and esay-to-use default implementation of the atomic_commit() hook.
1404 *
1405 * The tracking of asynchronously executed and still pending commits is done
1406 * using the core structure &drm_crtc_commit.
1407 *
1408 * By default there's no need to clean up resources allocated by this function
1409 * explicitly: drm_atomic_state_default_clear() will take care of that
1410 * automatically.
1411 *
1412 * Returns:
1413 *
1414 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1415 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1416 */
1417int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1418 bool nonblock)
1419{
1420 struct drm_crtc *crtc;
1421 struct drm_crtc_state *crtc_state;
1422 struct drm_crtc_commit *commit;
1423 int i, ret;
1424
1425 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1426 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1427 if (!commit)
1428 return -ENOMEM;
1429
1430 init_completion(&commit->flip_done);
1431 init_completion(&commit->hw_done);
1432 init_completion(&commit->cleanup_done);
1433 INIT_LIST_HEAD(&commit->commit_entry);
1434 kref_init(&commit->ref);
1435 commit->crtc = crtc;
1436
1437 state->crtcs[i].commit = commit;
1438
1439 ret = stall_checks(crtc, nonblock);
1440 if (ret)
1441 return ret;
1442
1443 /* Drivers only send out events when at least either current or
1444 * new CRTC state is active. Complete right away if everything
1445 * stays off. */
1446 if (!crtc->state->active && !crtc_state->active) {
1447 complete_all(&commit->flip_done);
1448 continue;
1449 }
1450
1451 /* Legacy cursor updates are fully unsynced. */
1452 if (state->legacy_cursor_update) {
1453 complete_all(&commit->flip_done);
1454 continue;
1455 }
1456
1457 if (!crtc_state->event) {
1458 commit->event = kzalloc(sizeof(*commit->event),
1459 GFP_KERNEL);
1460 if (!commit->event)
1461 return -ENOMEM;
1462
1463 crtc_state->event = commit->event;
1464 }
1465
1466 crtc_state->event->base.completion = &commit->flip_done;
Daniel Vetter24835e42016-12-21 11:23:30 +01001467 crtc_state->event->base.completion_release = release_crtc_commit;
1468 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001469 }
1470
1471 return 0;
1472}
1473EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1474
1475
1476static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1477{
1478 struct drm_crtc_commit *commit;
1479 int i = 0;
1480
1481 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1482 /* skip the first entry, that's the current commit */
1483 if (i == 1)
1484 return commit;
1485 i++;
1486 }
1487
1488 return NULL;
1489}
1490
1491/**
1492 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001493 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001494 *
1495 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001496 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02001497 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001498 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02001499 *
1500 * This is part of the atomic helper support for nonblocking commits, see
1501 * drm_atomic_helper_setup_commit() for an overview.
1502 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001503void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001504{
1505 struct drm_crtc *crtc;
1506 struct drm_crtc_state *crtc_state;
1507 struct drm_crtc_commit *commit;
1508 int i;
1509 long ret;
1510
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001511 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001512 spin_lock(&crtc->commit_lock);
1513 commit = preceeding_commit(crtc);
1514 if (commit)
1515 drm_crtc_commit_get(commit);
1516 spin_unlock(&crtc->commit_lock);
1517
1518 if (!commit)
1519 continue;
1520
1521 ret = wait_for_completion_timeout(&commit->hw_done,
1522 10*HZ);
1523 if (ret == 0)
1524 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1525 crtc->base.id, crtc->name);
1526
1527 /* Currently no support for overwriting flips, hence
1528 * stall for previous one to execute completely. */
1529 ret = wait_for_completion_timeout(&commit->flip_done,
1530 10*HZ);
1531 if (ret == 0)
1532 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1533 crtc->base.id, crtc->name);
1534
1535 drm_crtc_commit_put(commit);
1536 }
1537}
1538EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1539
1540/**
1541 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001542 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001543 *
1544 * This function is used to signal completion of the hardware commit step. After
1545 * this step the driver is not allowed to read or change any permanent software
1546 * or hardware modeset state. The only exception is state protected by other
1547 * means than &drm_modeset_lock locks.
1548 *
1549 * Drivers should try to postpone any expensive or delayed cleanup work after
1550 * this function is called.
1551 *
1552 * This is part of the atomic helper support for nonblocking commits, see
1553 * drm_atomic_helper_setup_commit() for an overview.
1554 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001555void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001556{
1557 struct drm_crtc *crtc;
1558 struct drm_crtc_state *crtc_state;
1559 struct drm_crtc_commit *commit;
1560 int i;
1561
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001562 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1563 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001564 if (!commit)
1565 continue;
1566
1567 /* backend must have consumed any event by now */
1568 WARN_ON(crtc->state->event);
1569 spin_lock(&crtc->commit_lock);
1570 complete_all(&commit->hw_done);
1571 spin_unlock(&crtc->commit_lock);
1572 }
1573}
1574EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1575
1576/**
1577 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001578 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001579 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001580 * This signals completion of the atomic update @old_state, including any
1581 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01001582 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02001583 *
1584 * This is part of the atomic helper support for nonblocking commits, see
1585 * drm_atomic_helper_setup_commit() for an overview.
1586 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001587void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001588{
1589 struct drm_crtc *crtc;
1590 struct drm_crtc_state *crtc_state;
1591 struct drm_crtc_commit *commit;
1592 int i;
1593 long ret;
1594
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001595 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1596 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001597 if (WARN_ON(!commit))
1598 continue;
1599
1600 spin_lock(&crtc->commit_lock);
1601 complete_all(&commit->cleanup_done);
1602 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1603
1604 /* commit_list borrows our reference, need to remove before we
1605 * clean up our drm_atomic_state. But only after it actually
1606 * completed, otherwise subsequent commits won't stall properly. */
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001607 if (try_wait_for_completion(&commit->flip_done))
1608 goto del_commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001609
1610 spin_unlock(&crtc->commit_lock);
1611
1612 /* We must wait for the vblank event to signal our completion
1613 * before releasing our reference, since the vblank work does
1614 * not hold a reference of its own. */
1615 ret = wait_for_completion_timeout(&commit->flip_done,
1616 10*HZ);
1617 if (ret == 0)
1618 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1619 crtc->base.id, crtc->name);
1620
1621 spin_lock(&crtc->commit_lock);
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001622del_commit:
Daniel Vettera095caa2016-06-08 17:15:36 +02001623 list_del(&commit->commit_entry);
1624 spin_unlock(&crtc->commit_lock);
1625 }
1626}
1627EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1628
Daniel Vettere8c833a2014-07-27 18:30:19 +02001629/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001630 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001631 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001632 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001633 *
1634 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001635 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1636 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1637 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001638 *
1639 * Returns:
1640 * 0 on success, negative error code on failure.
1641 */
1642int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1643 struct drm_atomic_state *state)
1644{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001645 struct drm_plane *plane;
1646 struct drm_plane_state *plane_state;
1647 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001648
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001649 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001650 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001651
1652 funcs = plane->helper_private;
1653
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001654 if (funcs->prepare_fb) {
1655 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001656 if (ret)
1657 goto fail;
1658 }
1659 }
1660
1661 return 0;
1662
1663fail:
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001664 for_each_plane_in_state(state, plane, plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001665 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001666
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001667 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001668 continue;
1669
1670 funcs = plane->helper_private;
1671
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001672 if (funcs->cleanup_fb)
1673 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001674 }
1675
1676 return ret;
1677}
1678EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1679
Ville Syrjälä7135ac52016-09-19 16:33:42 +03001680static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001681{
1682 return state->crtc && state->crtc->state->active;
1683}
1684
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001685/**
1686 * drm_atomic_helper_commit_planes - commit plane state
1687 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001688 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08001689 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001690 *
1691 * This function commits the new plane state using the plane and atomic helper
1692 * functions for planes and crtcs. It assumes that the atomic state has already
1693 * been pushed into the relevant object state pointers, since this step can no
1694 * longer fail.
1695 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001696 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001697 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001698 *
1699 * Note that this function does all plane updates across all CRTCs in one step.
1700 * If the hardware can't support this approach look at
1701 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001702 *
1703 * Plane parameters can be updated by applications while the associated CRTC is
1704 * disabled. The DRM/KMS core will store the parameters in the plane state,
1705 * which will be available to the driver when the CRTC is turned on. As a result
1706 * most drivers don't need to be immediately notified of plane updates for a
1707 * disabled CRTC.
1708 *
Liu Ying2b58e982016-08-29 17:12:03 +08001709 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1710 * @flags in order not to receive plane update notifications related to a
1711 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001712 * driver code when the driver and/or hardware can't or just don't need to deal
1713 * with updates on disabled CRTCs, for example when supporting runtime PM.
1714 *
Liu Ying2b58e982016-08-29 17:12:03 +08001715 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1716 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001717 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1718 * call for a plane if the CRTC of the old plane state needs a modesetting
1719 * operation. Of course, the drivers need to disable the planes in their CRTC
1720 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08001721 *
1722 * The drm_atomic_helper_commit() default implementation doesn't set the
1723 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1724 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001725 */
1726void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001727 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001728 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001729{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001730 struct drm_crtc *crtc;
1731 struct drm_crtc_state *old_crtc_state;
1732 struct drm_plane *plane;
1733 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001734 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08001735 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1736 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001737
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001738 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001739 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001740
1741 funcs = crtc->helper_private;
1742
1743 if (!funcs || !funcs->atomic_begin)
1744 continue;
1745
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001746 if (active_only && !crtc->state->active)
1747 continue;
1748
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001749 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001750 }
1751
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001752 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001753 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001754 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001755
1756 funcs = plane->helper_private;
1757
Thierry Reding3cad4b62014-11-25 13:05:12 +01001758 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001759 continue;
1760
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001761 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1762
1763 if (active_only) {
1764 /*
1765 * Skip planes related to inactive CRTCs. If the plane
1766 * is enabled use the state of the current CRTC. If the
1767 * plane is being disabled use the state of the old
1768 * CRTC to avoid skipping planes being disabled on an
1769 * active CRTC.
1770 */
1771 if (!disabling && !plane_crtc_active(plane->state))
1772 continue;
1773 if (disabling && !plane_crtc_active(old_plane_state))
1774 continue;
1775 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001776
Thierry Reding407b8bd2014-11-20 12:05:50 +01001777 /*
1778 * Special-case disabling the plane if drivers support it.
1779 */
Liu Ying2b58e982016-08-29 17:12:03 +08001780 if (disabling && funcs->atomic_disable) {
1781 struct drm_crtc_state *crtc_state;
1782
1783 crtc_state = old_plane_state->crtc->state;
1784
1785 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1786 no_disable)
1787 continue;
1788
Thierry Reding407b8bd2014-11-20 12:05:50 +01001789 funcs->atomic_disable(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08001790 } else if (plane->state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01001791 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08001792 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001793 }
1794
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001795 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001796 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001797
1798 funcs = crtc->helper_private;
1799
1800 if (!funcs || !funcs->atomic_flush)
1801 continue;
1802
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001803 if (active_only && !crtc->state->active)
1804 continue;
1805
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001806 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001807 }
1808}
1809EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1810
1811/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001812 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1813 * @old_crtc_state: atomic state object with the old crtc state
1814 *
1815 * This function commits the new plane state using the plane and atomic helper
1816 * functions for planes on the specific crtc. It assumes that the atomic state
1817 * has already been pushed into the relevant object state pointers, since this
1818 * step can no longer fail.
1819 *
1820 * This function is useful when plane updates should be done crtc-by-crtc
1821 * instead of one global step like drm_atomic_helper_commit_planes() does.
1822 *
1823 * This function can only be savely used when planes are not allowed to move
1824 * between different CRTCs because this function doesn't handle inter-CRTC
1825 * depencies. Callers need to ensure that either no such depencies exist,
1826 * resolve them through ordering of commit calls or through some other means.
1827 */
1828void
1829drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1830{
1831 const struct drm_crtc_helper_funcs *crtc_funcs;
1832 struct drm_crtc *crtc = old_crtc_state->crtc;
1833 struct drm_atomic_state *old_state = old_crtc_state->state;
1834 struct drm_plane *plane;
1835 unsigned plane_mask;
1836
1837 plane_mask = old_crtc_state->plane_mask;
1838 plane_mask |= crtc->state->plane_mask;
1839
1840 crtc_funcs = crtc->helper_private;
1841 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001842 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001843
1844 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1845 struct drm_plane_state *old_plane_state =
1846 drm_atomic_get_existing_plane_state(old_state, plane);
1847 const struct drm_plane_helper_funcs *plane_funcs;
1848
1849 plane_funcs = plane->helper_private;
1850
1851 if (!old_plane_state || !plane_funcs)
1852 continue;
1853
1854 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1855
1856 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1857 plane_funcs->atomic_disable)
1858 plane_funcs->atomic_disable(plane, old_plane_state);
1859 else if (plane->state->crtc ||
1860 drm_atomic_plane_disabling(plane, old_plane_state))
1861 plane_funcs->atomic_update(plane, old_plane_state);
1862 }
1863
1864 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001865 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001866}
1867EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1868
1869/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001870 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08001871 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02001872 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1873 *
1874 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08001875 * used for instance in the CRTC helper atomic_disable callback to disable
1876 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001877 *
1878 * If the atomic-parameter is set the function calls the CRTC's
1879 * atomic_begin hook before and atomic_flush hook after disabling the
1880 * planes.
1881 *
1882 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001883 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001884 */
Liu Ying28500292016-08-26 15:30:39 +08001885void
1886drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1887 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001888{
Liu Ying28500292016-08-26 15:30:39 +08001889 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02001890 const struct drm_crtc_helper_funcs *crtc_funcs =
1891 crtc->helper_private;
1892 struct drm_plane *plane;
1893
1894 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1895 crtc_funcs->atomic_begin(crtc, NULL);
1896
Liu Ying28500292016-08-26 15:30:39 +08001897 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02001898 const struct drm_plane_helper_funcs *plane_funcs =
1899 plane->helper_private;
1900
Liu Ying28500292016-08-26 15:30:39 +08001901 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001902 continue;
1903
1904 WARN_ON(!plane_funcs->atomic_disable);
1905 if (plane_funcs->atomic_disable)
1906 plane_funcs->atomic_disable(plane, NULL);
1907 }
1908
1909 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1910 crtc_funcs->atomic_flush(crtc, NULL);
1911}
1912EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1913
1914/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001915 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1916 * @dev: DRM device
1917 * @old_state: atomic state object with old state structures
1918 *
1919 * This function cleans up plane state, specifically framebuffers, from the old
1920 * configuration. Hence the old configuration must be perserved in @old_state to
1921 * be able to call this function.
1922 *
1923 * This function must also be called on the new state when the atomic update
1924 * fails at any point after calling drm_atomic_helper_prepare_planes().
1925 */
1926void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1927 struct drm_atomic_state *old_state)
1928{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001929 struct drm_plane *plane;
1930 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001931 int i;
1932
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001933 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001934 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001935
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001936 funcs = plane->helper_private;
1937
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001938 if (funcs->cleanup_fb)
1939 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001940 }
1941}
1942EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1943
1944/**
1945 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001946 * @state: atomic state
Daniel Vetter5e84c262016-06-10 00:06:32 +02001947 * @stall: stall for proceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001948 *
1949 * This function stores the atomic state into the current state pointers in all
1950 * driver objects. It should be called after all failing steps have been done
1951 * and succeeded, but before the actual hardware state is committed.
1952 *
1953 * For cleanup and error recovery the current state for all changed objects will
1954 * be swaped into @state.
1955 *
1956 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1957 *
1958 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1959 *
1960 * 2. Do any other steps that might fail.
1961 *
1962 * 3. Put the staged state into the current state pointers with this function.
1963 *
1964 * 4. Actually commit the hardware state.
1965 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001966 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001967 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02001968 *
1969 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001970 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
1971 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02001972 * don't pass the right state structures to the callbacks.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001973 */
Daniel Vetter5e84c262016-06-10 00:06:32 +02001974void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1975 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001976{
1977 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02001978 long ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001979 struct drm_connector *connector;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01001980 struct drm_connector_state *conn_state, *old_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001981 struct drm_crtc *crtc;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01001982 struct drm_crtc_state *crtc_state, *old_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001983 struct drm_plane *plane;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01001984 struct drm_plane_state *plane_state, *old_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001985 struct drm_crtc_commit *commit;
1986
1987 if (stall) {
1988 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1989 spin_lock(&crtc->commit_lock);
1990 commit = list_first_entry_or_null(&crtc->commit_list,
1991 struct drm_crtc_commit, commit_entry);
1992 if (commit)
1993 drm_crtc_commit_get(commit);
1994 spin_unlock(&crtc->commit_lock);
1995
1996 if (!commit)
1997 continue;
1998
1999 ret = wait_for_completion_timeout(&commit->hw_done,
2000 10*HZ);
2001 if (ret == 0)
2002 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2003 crtc->base.id, crtc->name);
2004 drm_crtc_commit_put(commit);
2005 }
2006 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002007
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002008 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) {
2009 WARN_ON(connector->state != old_conn_state);
2010
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002011 connector->state->state = state;
Daniel Vetter63e83c12016-06-02 00:06:32 +02002012 swap(state->connectors[i].state, connector->state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002013 connector->state->state = NULL;
2014 }
2015
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002016 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
2017 WARN_ON(crtc->state != old_crtc_state);
2018
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002019 crtc->state->state = state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +02002020 swap(state->crtcs[i].state, crtc->state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002021 crtc->state->state = NULL;
Daniel Vettera095caa2016-06-08 17:15:36 +02002022
2023 if (state->crtcs[i].commit) {
2024 spin_lock(&crtc->commit_lock);
2025 list_add(&state->crtcs[i].commit->commit_entry,
2026 &crtc->commit_list);
2027 spin_unlock(&crtc->commit_lock);
2028
2029 state->crtcs[i].commit->event = NULL;
2030 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002031 }
2032
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002033 for_each_oldnew_plane_in_state(state, plane, old_plane_state, plane_state, i) {
2034 WARN_ON(plane->state != old_plane_state);
2035
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002036 plane->state->state = state;
Daniel Vetterb8b53422016-06-02 00:06:33 +02002037 swap(state->planes[i].state, plane->state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002038 plane->state->state = NULL;
2039 }
2040}
2041EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002042
2043/**
2044 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2045 * @plane: plane object to update
2046 * @crtc: owning CRTC of owning plane
2047 * @fb: framebuffer to flip onto plane
2048 * @crtc_x: x offset of primary plane on crtc
2049 * @crtc_y: y offset of primary plane on crtc
2050 * @crtc_w: width of primary plane rectangle on crtc
2051 * @crtc_h: height of primary plane rectangle on crtc
2052 * @src_x: x offset of @fb for panning
2053 * @src_y: y offset of @fb for panning
2054 * @src_w: width of source rectangle in @fb
2055 * @src_h: height of source rectangle in @fb
2056 *
2057 * Provides a default plane update handler using the atomic driver interface.
2058 *
2059 * RETURNS:
2060 * Zero on success, error code on failure
2061 */
2062int drm_atomic_helper_update_plane(struct drm_plane *plane,
2063 struct drm_crtc *crtc,
2064 struct drm_framebuffer *fb,
2065 int crtc_x, int crtc_y,
2066 unsigned int crtc_w, unsigned int crtc_h,
2067 uint32_t src_x, uint32_t src_y,
2068 uint32_t src_w, uint32_t src_h)
2069{
2070 struct drm_atomic_state *state;
2071 struct drm_plane_state *plane_state;
2072 int ret = 0;
2073
2074 state = drm_atomic_state_alloc(plane->dev);
2075 if (!state)
2076 return -ENOMEM;
2077
2078 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2079retry:
2080 plane_state = drm_atomic_get_plane_state(state, plane);
2081 if (IS_ERR(plane_state)) {
2082 ret = PTR_ERR(plane_state);
2083 goto fail;
2084 }
2085
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002086 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002087 if (ret != 0)
2088 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002089 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002090 plane_state->crtc_x = crtc_x;
2091 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002092 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002093 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002094 plane_state->src_x = src_x;
2095 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002096 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002097 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002098
Daniel Vetter3671c582015-05-04 15:40:52 +02002099 if (plane == crtc->cursor)
2100 state->legacy_cursor_update = true;
2101
Daniel Vetter042652e2014-07-27 13:46:52 +02002102 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002103fail:
2104 if (ret == -EDEADLK)
2105 goto backoff;
2106
Chris Wilson08536952016-10-14 13:18:18 +01002107 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002108 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002109
Daniel Vetter042652e2014-07-27 13:46:52 +02002110backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002111 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002112 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002113
2114 /*
2115 * Someone might have exchanged the framebuffer while we dropped locks
2116 * in the backoff code. We need to fix up the fb refcount tracking the
2117 * core does for us.
2118 */
2119 plane->old_fb = plane->fb;
2120
2121 goto retry;
2122}
2123EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2124
2125/**
2126 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2127 * @plane: plane to disable
2128 *
2129 * Provides a default plane disable handler using the atomic driver interface.
2130 *
2131 * RETURNS:
2132 * Zero on success, error code on failure
2133 */
2134int drm_atomic_helper_disable_plane(struct drm_plane *plane)
2135{
2136 struct drm_atomic_state *state;
2137 struct drm_plane_state *plane_state;
2138 int ret = 0;
2139
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08002140 /*
2141 * FIXME: Without plane->crtc set we can't get at the implicit legacy
2142 * acquire context. The real fix will be to wire the acquire ctx through
2143 * everywhere we need it, but meanwhile prevent chaos by just skipping
2144 * this noop. The critical case is the cursor ioctls which a) only grab
2145 * crtc/cursor-plane locks (so we need the crtc to get at the right
2146 * acquire context) and b) can try to disable the plane multiple times.
2147 */
2148 if (!plane->crtc)
2149 return 0;
2150
Daniel Vetter042652e2014-07-27 13:46:52 +02002151 state = drm_atomic_state_alloc(plane->dev);
2152 if (!state)
2153 return -ENOMEM;
2154
2155 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
2156retry:
2157 plane_state = drm_atomic_get_plane_state(state, plane);
2158 if (IS_ERR(plane_state)) {
2159 ret = PTR_ERR(plane_state);
2160 goto fail;
2161 }
2162
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002163 if (plane_state->crtc && (plane == plane->crtc->cursor))
2164 plane_state->state->legacy_cursor_update = true;
2165
Rob Clarkbbb1e522015-08-25 15:35:58 -04002166 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002167 if (ret != 0)
2168 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002169
Daniel Vetter042652e2014-07-27 13:46:52 +02002170 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002171fail:
2172 if (ret == -EDEADLK)
2173 goto backoff;
2174
Chris Wilson08536952016-10-14 13:18:18 +01002175 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002176 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002177
Daniel Vetter042652e2014-07-27 13:46:52 +02002178backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002179 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002180 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002181
2182 /*
2183 * Someone might have exchanged the framebuffer while we dropped locks
2184 * in the backoff code. We need to fix up the fb refcount tracking the
2185 * core does for us.
2186 */
2187 plane->old_fb = plane->fb;
2188
2189 goto retry;
2190}
2191EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2192
Rob Clarkbbb1e522015-08-25 15:35:58 -04002193/* just used from fb-helper and atomic-helper: */
2194int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2195 struct drm_plane_state *plane_state)
2196{
2197 int ret;
2198
2199 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2200 if (ret != 0)
2201 return ret;
2202
2203 drm_atomic_set_fb_for_plane(plane_state, NULL);
2204 plane_state->crtc_x = 0;
2205 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002206 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002207 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002208 plane_state->src_x = 0;
2209 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002210 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002211 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002212
Rob Clarkbbb1e522015-08-25 15:35:58 -04002213 return 0;
2214}
2215
Daniel Vetter042652e2014-07-27 13:46:52 +02002216static int update_output_state(struct drm_atomic_state *state,
2217 struct drm_mode_set *set)
2218{
2219 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002220 struct drm_crtc *crtc;
2221 struct drm_crtc_state *crtc_state;
2222 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02002223 struct drm_connector_state *conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002224 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002225
2226 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2227 state->acquire_ctx);
2228 if (ret)
2229 return ret;
2230
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002231 /* First disable all connectors on the target crtc. */
2232 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2233 if (ret)
2234 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002235
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002236 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002237 if (conn_state->crtc == set->crtc) {
2238 ret = drm_atomic_set_crtc_for_connector(conn_state,
2239 NULL);
2240 if (ret)
2241 return ret;
2242 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002243 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002244
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002245 /* Then set all connectors from set->connectors on the target crtc */
2246 for (i = 0; i < set->num_connectors; i++) {
2247 conn_state = drm_atomic_get_connector_state(state,
2248 set->connectors[i]);
2249 if (IS_ERR(conn_state))
2250 return PTR_ERR(conn_state);
2251
2252 ret = drm_atomic_set_crtc_for_connector(conn_state,
2253 set->crtc);
2254 if (ret)
2255 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002256 }
2257
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002258 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002259 /* Don't update ->enable for the CRTC in the set_config request,
2260 * since a mismatch would indicate a bug in the upper layers.
2261 * The actual modeset code later on will catch any
2262 * inconsistencies here. */
2263 if (crtc == set->crtc)
2264 continue;
2265
Maarten Lankhorst14de6c42016-01-04 12:53:20 +01002266 if (!crtc_state->connector_mask) {
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002267 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
2268 NULL);
2269 if (ret < 0)
2270 return ret;
2271
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02002272 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002273 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002274 }
2275
2276 return 0;
2277}
2278
2279/**
2280 * drm_atomic_helper_set_config - set a new config from userspace
2281 * @set: mode set configuration
2282 *
2283 * Provides a default crtc set_config handler using the atomic driver interface.
2284 *
2285 * Returns:
2286 * Returns 0 on success, negative errno numbers on failure.
2287 */
2288int drm_atomic_helper_set_config(struct drm_mode_set *set)
2289{
2290 struct drm_atomic_state *state;
2291 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002292 int ret = 0;
2293
2294 state = drm_atomic_state_alloc(crtc->dev);
2295 if (!state)
2296 return -ENOMEM;
2297
Maarten Lankhorst40616a22016-03-03 10:17:39 +01002298 state->legacy_set_config = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02002299 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2300retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04002301 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002302 if (ret != 0)
2303 goto fail;
2304
Daniel Vetter042652e2014-07-27 13:46:52 +02002305 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002306fail:
2307 if (ret == -EDEADLK)
2308 goto backoff;
2309
Chris Wilson08536952016-10-14 13:18:18 +01002310 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002311 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002312
Daniel Vetter042652e2014-07-27 13:46:52 +02002313backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002314 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002315 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002316
2317 /*
2318 * Someone might have exchanged the framebuffer while we dropped locks
2319 * in the backoff code. We need to fix up the fb refcount tracking the
2320 * core does for us.
2321 */
2322 crtc->primary->old_fb = crtc->primary->fb;
2323
2324 goto retry;
2325}
2326EXPORT_SYMBOL(drm_atomic_helper_set_config);
2327
Rob Clarkbbb1e522015-08-25 15:35:58 -04002328/* just used from fb-helper and atomic-helper: */
2329int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2330 struct drm_atomic_state *state)
2331{
2332 struct drm_crtc_state *crtc_state;
2333 struct drm_plane_state *primary_state;
2334 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002335 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002336 int ret;
2337
2338 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2339 if (IS_ERR(crtc_state))
2340 return PTR_ERR(crtc_state);
2341
2342 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2343 if (IS_ERR(primary_state))
2344 return PTR_ERR(primary_state);
2345
2346 if (!set->mode) {
2347 WARN_ON(set->fb);
2348 WARN_ON(set->num_connectors);
2349
2350 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2351 if (ret != 0)
2352 return ret;
2353
2354 crtc_state->active = false;
2355
2356 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2357 if (ret != 0)
2358 return ret;
2359
2360 drm_atomic_set_fb_for_plane(primary_state, NULL);
2361
2362 goto commit;
2363 }
2364
2365 WARN_ON(!set->fb);
2366 WARN_ON(!set->num_connectors);
2367
2368 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2369 if (ret != 0)
2370 return ret;
2371
2372 crtc_state->active = true;
2373
2374 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2375 if (ret != 0)
2376 return ret;
2377
Daniel Vetter196cd5d2017-01-25 07:26:56 +01002378 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Ville Syrjälä83926112015-11-16 17:02:34 +02002379
Rob Clarkbbb1e522015-08-25 15:35:58 -04002380 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2381 primary_state->crtc_x = 0;
2382 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02002383 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002384 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002385 primary_state->src_x = set->x << 16;
2386 primary_state->src_y = set->y << 16;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002387 if (drm_rotation_90_or_270(primary_state->rotation)) {
Ville Syrjälä83926112015-11-16 17:02:34 +02002388 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002389 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002390 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02002391 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002392 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002393 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04002394
2395commit:
2396 ret = update_output_state(state, set);
2397 if (ret)
2398 return ret;
2399
2400 return 0;
2401}
2402
Daniel Vetter042652e2014-07-27 13:46:52 +02002403/**
Thierry Reding14942762015-12-02 17:50:04 +01002404 * drm_atomic_helper_disable_all - disable all currently active outputs
2405 * @dev: DRM device
2406 * @ctx: lock acquisition context
2407 *
2408 * Loops through all connectors, finding those that aren't turned off and then
2409 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2410 * that they are connected to.
2411 *
2412 * This is used for example in suspend/resume to disable all currently active
2413 * functions when suspending.
2414 *
2415 * Note that if callers haven't already acquired all modeset locks this might
2416 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2417 *
2418 * Returns:
2419 * 0 on success or a negative error code on failure.
2420 *
2421 * See also:
2422 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2423 */
2424int drm_atomic_helper_disable_all(struct drm_device *dev,
2425 struct drm_modeset_acquire_ctx *ctx)
2426{
2427 struct drm_atomic_state *state;
2428 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01002429 struct drm_connector_list_iter conn_iter;
Thierry Reding14942762015-12-02 17:50:04 +01002430 int err;
2431
2432 state = drm_atomic_state_alloc(dev);
2433 if (!state)
2434 return -ENOMEM;
2435
2436 state->acquire_ctx = ctx;
2437
Daniel Vetterc36a3252016-12-15 16:58:43 +01002438 drm_connector_list_iter_get(dev, &conn_iter);
2439 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding14942762015-12-02 17:50:04 +01002440 struct drm_crtc *crtc = conn->state->crtc;
2441 struct drm_crtc_state *crtc_state;
2442
2443 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
2444 continue;
2445
2446 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2447 if (IS_ERR(crtc_state)) {
2448 err = PTR_ERR(crtc_state);
2449 goto free;
2450 }
2451
2452 crtc_state->active = false;
2453 }
2454
2455 err = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01002456free:
Daniel Vetterc36a3252016-12-15 16:58:43 +01002457 drm_connector_list_iter_put(&conn_iter);
Chris Wilson08536952016-10-14 13:18:18 +01002458 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01002459 return err;
2460}
2461EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2462
2463/**
2464 * drm_atomic_helper_suspend - subsystem-level suspend helper
2465 * @dev: DRM device
2466 *
2467 * Duplicates the current atomic state, disables all active outputs and then
2468 * returns a pointer to the original atomic state to the caller. Drivers can
2469 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2470 * restore the output configuration that was active at the time the system
2471 * entered suspend.
2472 *
2473 * Note that it is potentially unsafe to use this. The atomic state object
2474 * returned by this function is assumed to be persistent. Drivers must ensure
2475 * that this holds true. Before calling this function, drivers must make sure
2476 * to suspend fbdev emulation so that nothing can be using the device.
2477 *
2478 * Returns:
2479 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2480 * encoded error code on failure. Drivers should store the returned atomic
2481 * state object and pass it to the drm_atomic_helper_resume() helper upon
2482 * resume.
2483 *
2484 * See also:
2485 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002486 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01002487 */
2488struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2489{
2490 struct drm_modeset_acquire_ctx ctx;
2491 struct drm_atomic_state *state;
2492 int err;
2493
2494 drm_modeset_acquire_init(&ctx, 0);
2495
2496retry:
2497 err = drm_modeset_lock_all_ctx(dev, &ctx);
2498 if (err < 0) {
2499 state = ERR_PTR(err);
2500 goto unlock;
2501 }
2502
2503 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2504 if (IS_ERR(state))
2505 goto unlock;
2506
2507 err = drm_atomic_helper_disable_all(dev, &ctx);
2508 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01002509 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01002510 state = ERR_PTR(err);
2511 goto unlock;
2512 }
2513
2514unlock:
2515 if (PTR_ERR(state) == -EDEADLK) {
2516 drm_modeset_backoff(&ctx);
2517 goto retry;
2518 }
2519
2520 drm_modeset_drop_locks(&ctx);
2521 drm_modeset_acquire_fini(&ctx);
2522 return state;
2523}
2524EXPORT_SYMBOL(drm_atomic_helper_suspend);
2525
2526/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002527 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2528 * @state: duplicated atomic state to commit
2529 * @ctx: pointer to acquire_ctx to use for commit.
2530 *
2531 * The state returned by drm_atomic_helper_duplicate_state() and
2532 * drm_atomic_helper_suspend() is partially invalid, and needs to
2533 * be fixed up before commit.
2534 *
2535 * Returns:
2536 * 0 on success or a negative error code on failure.
2537 *
2538 * See also:
2539 * drm_atomic_helper_suspend()
2540 */
2541int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2542 struct drm_modeset_acquire_ctx *ctx)
2543{
2544 int i;
2545 struct drm_plane *plane;
2546 struct drm_plane_state *plane_state;
2547 struct drm_connector *connector;
2548 struct drm_connector_state *conn_state;
2549 struct drm_crtc *crtc;
2550 struct drm_crtc_state *crtc_state;
2551
2552 state->acquire_ctx = ctx;
2553
2554 for_each_new_plane_in_state(state, plane, plane_state, i)
2555 state->planes[i].old_state = plane->state;
2556
2557 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
2558 state->crtcs[i].old_state = crtc->state;
2559
2560 for_each_new_connector_in_state(state, connector, conn_state, i)
2561 state->connectors[i].old_state = connector->state;
2562
2563 return drm_atomic_commit(state);
2564}
2565EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2566
2567/**
Thierry Reding14942762015-12-02 17:50:04 +01002568 * drm_atomic_helper_resume - subsystem-level resume helper
2569 * @dev: DRM device
2570 * @state: atomic state to resume to
2571 *
2572 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2573 * grabs all modeset locks and commits the atomic state object. This can be
2574 * used in conjunction with the drm_atomic_helper_suspend() helper to
2575 * implement suspend/resume for drivers that support atomic mode-setting.
2576 *
2577 * Returns:
2578 * 0 on success or a negative error code on failure.
2579 *
2580 * See also:
2581 * drm_atomic_helper_suspend()
2582 */
2583int drm_atomic_helper_resume(struct drm_device *dev,
2584 struct drm_atomic_state *state)
2585{
2586 struct drm_mode_config *config = &dev->mode_config;
2587 int err;
2588
2589 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002590
Thierry Reding14942762015-12-02 17:50:04 +01002591 drm_modeset_lock_all(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002592 err = drm_atomic_helper_commit_duplicated_state(state, config->acquire_ctx);
Thierry Reding14942762015-12-02 17:50:04 +01002593 drm_modeset_unlock_all(dev);
2594
2595 return err;
2596}
2597EXPORT_SYMBOL(drm_atomic_helper_resume);
2598
2599/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002600 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002601 * @crtc: DRM crtc
2602 * @property: DRM property
2603 * @val: value of property
2604 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002605 * Provides a default crtc set_property handler using the atomic driver
2606 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002607 *
2608 * RETURNS:
2609 * Zero on success, error code on failure
2610 */
2611int
2612drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2613 struct drm_property *property,
2614 uint64_t val)
2615{
2616 struct drm_atomic_state *state;
2617 struct drm_crtc_state *crtc_state;
2618 int ret = 0;
2619
2620 state = drm_atomic_state_alloc(crtc->dev);
2621 if (!state)
2622 return -ENOMEM;
2623
2624 /* ->set_property is always called with all locks held. */
2625 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2626retry:
2627 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2628 if (IS_ERR(crtc_state)) {
2629 ret = PTR_ERR(crtc_state);
2630 goto fail;
2631 }
2632
Rob Clark40ecc692014-12-18 16:01:46 -05002633 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2634 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002635 if (ret)
2636 goto fail;
2637
2638 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002639fail:
2640 if (ret == -EDEADLK)
2641 goto backoff;
2642
Chris Wilson08536952016-10-14 13:18:18 +01002643 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002644 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002645
Daniel Vetter042652e2014-07-27 13:46:52 +02002646backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002647 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002648 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002649
2650 goto retry;
2651}
2652EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2653
2654/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002655 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002656 * @plane: DRM plane
2657 * @property: DRM property
2658 * @val: value of property
2659 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002660 * Provides a default plane set_property handler using the atomic driver
2661 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002662 *
2663 * RETURNS:
2664 * Zero on success, error code on failure
2665 */
2666int
2667drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2668 struct drm_property *property,
2669 uint64_t val)
2670{
2671 struct drm_atomic_state *state;
2672 struct drm_plane_state *plane_state;
2673 int ret = 0;
2674
2675 state = drm_atomic_state_alloc(plane->dev);
2676 if (!state)
2677 return -ENOMEM;
2678
2679 /* ->set_property is always called with all locks held. */
2680 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2681retry:
2682 plane_state = drm_atomic_get_plane_state(state, plane);
2683 if (IS_ERR(plane_state)) {
2684 ret = PTR_ERR(plane_state);
2685 goto fail;
2686 }
2687
Rob Clark40ecc692014-12-18 16:01:46 -05002688 ret = drm_atomic_plane_set_property(plane, plane_state,
2689 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002690 if (ret)
2691 goto fail;
2692
2693 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002694fail:
2695 if (ret == -EDEADLK)
2696 goto backoff;
2697
Chris Wilson08536952016-10-14 13:18:18 +01002698 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002699 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002700
Daniel Vetter042652e2014-07-27 13:46:52 +02002701backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002702 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002703 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002704
2705 goto retry;
2706}
2707EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2708
2709/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002710 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002711 * @connector: DRM connector
2712 * @property: DRM property
2713 * @val: value of property
2714 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002715 * Provides a default connector set_property handler using the atomic driver
2716 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002717 *
2718 * RETURNS:
2719 * Zero on success, error code on failure
2720 */
2721int
2722drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2723 struct drm_property *property,
2724 uint64_t val)
2725{
2726 struct drm_atomic_state *state;
2727 struct drm_connector_state *connector_state;
2728 int ret = 0;
2729
2730 state = drm_atomic_state_alloc(connector->dev);
2731 if (!state)
2732 return -ENOMEM;
2733
2734 /* ->set_property is always called with all locks held. */
2735 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2736retry:
2737 connector_state = drm_atomic_get_connector_state(state, connector);
2738 if (IS_ERR(connector_state)) {
2739 ret = PTR_ERR(connector_state);
2740 goto fail;
2741 }
2742
Rob Clark40ecc692014-12-18 16:01:46 -05002743 ret = drm_atomic_connector_set_property(connector, connector_state,
2744 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002745 if (ret)
2746 goto fail;
2747
2748 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002749fail:
2750 if (ret == -EDEADLK)
2751 goto backoff;
2752
Chris Wilson08536952016-10-14 13:18:18 +01002753 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002754 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002755
Daniel Vetter042652e2014-07-27 13:46:52 +02002756backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002757 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002758 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002759
2760 goto retry;
2761}
2762EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002763
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002764static int page_flip_common(
2765 struct drm_atomic_state *state,
2766 struct drm_crtc *crtc,
2767 struct drm_framebuffer *fb,
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002768 struct drm_pending_vblank_event *event,
2769 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002770{
2771 struct drm_plane *plane = crtc->primary;
2772 struct drm_plane_state *plane_state;
2773 struct drm_crtc_state *crtc_state;
2774 int ret = 0;
2775
2776 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2777 if (IS_ERR(crtc_state))
2778 return PTR_ERR(crtc_state);
2779
2780 crtc_state->event = event;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002781 crtc_state->pageflip_flags = flags;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002782
2783 plane_state = drm_atomic_get_plane_state(state, plane);
2784 if (IS_ERR(plane_state))
2785 return PTR_ERR(plane_state);
2786
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002787 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2788 if (ret != 0)
2789 return ret;
2790 drm_atomic_set_fb_for_plane(plane_state, fb);
2791
2792 /* Make sure we don't accidentally do a full modeset. */
2793 state->allow_modeset = false;
2794 if (!crtc_state->active) {
2795 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2796 crtc->base.id);
2797 return -EINVAL;
2798 }
2799
2800 return ret;
2801}
2802
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002803/**
2804 * drm_atomic_helper_page_flip - execute a legacy page flip
2805 * @crtc: DRM crtc
2806 * @fb: DRM framebuffer
2807 * @event: optional DRM event to signal upon completion
2808 * @flags: flip flags for non-vblank sync'ed updates
2809 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002810 * Provides a default &drm_crtc_funcs.page_flip implementation
2811 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002812 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002813 * Returns:
2814 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002815 *
2816 * See also:
2817 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002818 */
2819int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2820 struct drm_framebuffer *fb,
2821 struct drm_pending_vblank_event *event,
2822 uint32_t flags)
2823{
2824 struct drm_plane *plane = crtc->primary;
2825 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002826 int ret = 0;
2827
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002828 state = drm_atomic_state_alloc(plane->dev);
2829 if (!state)
2830 return -ENOMEM;
2831
2832 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002833
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002834retry:
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002835 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002836 if (ret != 0)
2837 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01002838
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002839 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002840
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002841fail:
2842 if (ret == -EDEADLK)
2843 goto backoff;
2844
Chris Wilson08536952016-10-14 13:18:18 +01002845 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002846 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002847
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002848backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002849 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002850 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002851
2852 /*
2853 * Someone might have exchanged the framebuffer while we dropped locks
2854 * in the backoff code. We need to fix up the fb refcount tracking the
2855 * core does for us.
2856 */
2857 plane->old_fb = plane->fb;
2858
2859 goto retry;
2860}
2861EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002862
2863/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002864 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2865 * @crtc: DRM crtc
2866 * @fb: DRM framebuffer
2867 * @event: optional DRM event to signal upon completion
2868 * @flags: flip flags for non-vblank sync'ed updates
2869 * @target: specifying the target vblank period when the flip to take effect
2870 *
2871 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2872 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2873 * target vblank period to flip.
2874 *
2875 * Returns:
2876 * Returns 0 on success, negative errno numbers on failure.
2877 */
2878int drm_atomic_helper_page_flip_target(
2879 struct drm_crtc *crtc,
2880 struct drm_framebuffer *fb,
2881 struct drm_pending_vblank_event *event,
2882 uint32_t flags,
2883 uint32_t target)
2884{
2885 struct drm_plane *plane = crtc->primary;
2886 struct drm_atomic_state *state;
2887 struct drm_crtc_state *crtc_state;
2888 int ret = 0;
2889
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002890 state = drm_atomic_state_alloc(plane->dev);
2891 if (!state)
2892 return -ENOMEM;
2893
2894 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2895
2896retry:
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05002897 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002898 if (ret != 0)
2899 goto fail;
2900
2901 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
2902 if (WARN_ON(!crtc_state)) {
2903 ret = -EINVAL;
2904 goto fail;
2905 }
2906 crtc_state->target_vblank = target;
2907
2908 ret = drm_atomic_nonblocking_commit(state);
2909
2910fail:
2911 if (ret == -EDEADLK)
2912 goto backoff;
2913
2914 drm_atomic_state_put(state);
2915 return ret;
2916
2917backoff:
2918 drm_atomic_state_clear(state);
2919 drm_atomic_legacy_backoff(state);
2920
2921 /*
2922 * Someone might have exchanged the framebuffer while we dropped locks
2923 * in the backoff code. We need to fix up the fb refcount tracking the
2924 * core does for us.
2925 */
2926 plane->old_fb = plane->fb;
2927
2928 goto retry;
2929}
2930EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2931
2932/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002933 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2934 * @connector: affected connector
2935 * @mode: DPMS mode
2936 *
2937 * This is the main helper function provided by the atomic helper framework for
2938 * implementing the legacy DPMS connector interface. It computes the new desired
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002939 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
2940 * enabled) and updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002941 *
2942 * Returns:
2943 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002944 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002945int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2946 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002947{
2948 struct drm_mode_config *config = &connector->dev->mode_config;
2949 struct drm_atomic_state *state;
2950 struct drm_crtc_state *crtc_state;
2951 struct drm_crtc *crtc;
2952 struct drm_connector *tmp_connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +01002953 struct drm_connector_list_iter conn_iter;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002954 int ret;
2955 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002956 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002957
2958 if (mode != DRM_MODE_DPMS_ON)
2959 mode = DRM_MODE_DPMS_OFF;
2960
2961 connector->dpms = mode;
2962 crtc = connector->state->crtc;
2963
2964 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002965 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002966
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002967 state = drm_atomic_state_alloc(connector->dev);
2968 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002969 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002970
2971 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2972retry:
2973 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002974 if (IS_ERR(crtc_state)) {
2975 ret = PTR_ERR(crtc_state);
2976 goto fail;
2977 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002978
2979 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2980
Daniel Vetterc36a3252016-12-15 16:58:43 +01002981 drm_connector_list_iter_get(connector->dev, &conn_iter);
2982 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
John Hunter0388df02015-03-17 15:30:28 +08002983 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002984 continue;
2985
John Hunter0388df02015-03-17 15:30:28 +08002986 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002987 active = true;
2988 break;
2989 }
2990 }
Daniel Vetterc36a3252016-12-15 16:58:43 +01002991 drm_connector_list_iter_put(&conn_iter);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002992 crtc_state->active = active;
2993
2994 ret = drm_atomic_commit(state);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002995fail:
2996 if (ret == -EDEADLK)
2997 goto backoff;
Marta Lofstedt8f5040e2016-12-05 14:04:08 +02002998 if (ret != 0)
2999 connector->dpms = old_mode;
Chris Wilson08536952016-10-14 13:18:18 +01003000 drm_atomic_state_put(state);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02003001 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003002
Daniel Vetterb486e0e2015-01-22 18:53:05 +01003003backoff:
3004 drm_atomic_state_clear(state);
3005 drm_atomic_legacy_backoff(state);
3006
3007 goto retry;
3008}
3009EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3010
3011/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003012 * drm_atomic_helper_best_encoder - Helper for
3013 * &drm_connector_helper_funcs.best_encoder callback
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003014 * @connector: Connector control structure
3015 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003016 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003017 * connectors that support exactly 1 encoder, statically determined at driver
3018 * init time.
3019 */
3020struct drm_encoder *
3021drm_atomic_helper_best_encoder(struct drm_connector *connector)
3022{
3023 WARN_ON(connector->encoder_ids[1]);
3024 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3025}
3026EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3027
3028/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003029 * DOC: atomic state reset and initialization
3030 *
3031 * Both the drm core and the atomic helpers assume that there is always the full
3032 * and correct atomic software state for all connectors, CRTCs and planes
3033 * available. Which is a bit a problem on driver load and also after system
3034 * suspend. One way to solve this is to have a hardware state read-out
3035 * infrastructure which reconstructs the full software state (e.g. the i915
3036 * driver).
3037 *
3038 * The simpler solution is to just reset the software state to everything off,
3039 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3040 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01003041 *
3042 * On the upside the precise state tracking of atomic simplifies system suspend
3043 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3044 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3045 * For other drivers the building blocks are split out, see the documentation
3046 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003047 */
3048
3049/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003050 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
Daniel Vetterd4617012014-11-03 15:56:43 +01003051 * @crtc: drm CRTC
3052 *
3053 * Resets the atomic state for @crtc by freeing the state pointer (which might
3054 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3055 */
3056void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3057{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003058 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003059 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003060
Daniel Vetterd4617012014-11-03 15:56:43 +01003061 kfree(crtc->state);
3062 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003063
3064 if (crtc->state)
3065 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01003066}
3067EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3068
3069/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003070 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3071 * @crtc: CRTC object
3072 * @state: atomic CRTC state
3073 *
3074 * Copies atomic state from a CRTC's current state and resets inferred values.
3075 * This is useful for drivers that subclass the CRTC state.
3076 */
3077void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3078 struct drm_crtc_state *state)
3079{
3080 memcpy(state, crtc->state, sizeof(*state));
3081
Daniel Stone99cf4a22015-05-25 19:11:51 +01003082 if (state->mode_blob)
3083 drm_property_reference_blob(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003084 if (state->degamma_lut)
3085 drm_property_reference_blob(state->degamma_lut);
3086 if (state->ctm)
3087 drm_property_reference_blob(state->ctm);
3088 if (state->gamma_lut)
3089 drm_property_reference_blob(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003090 state->mode_changed = false;
3091 state->active_changed = false;
3092 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02003093 state->connectors_changed = false;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003094 state->color_mgmt_changed = false;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02003095 state->zpos_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01003096 state->event = NULL;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003097 state->pageflip_flags = 0;
Thierry Redingf5e78402015-01-28 14:54:32 +01003098}
3099EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3100
3101/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003102 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3103 * @crtc: drm CRTC
3104 *
3105 * Default CRTC state duplicate hook for drivers which don't have their own
3106 * subclassed CRTC state structure.
3107 */
3108struct drm_crtc_state *
3109drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3110{
3111 struct drm_crtc_state *state;
3112
3113 if (WARN_ON(!crtc->state))
3114 return NULL;
3115
Thierry Redingf5e78402015-01-28 14:54:32 +01003116 state = kmalloc(sizeof(*state), GFP_KERNEL);
3117 if (state)
3118 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003119
3120 return state;
3121}
3122EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3123
3124/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003125 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
Thierry Redingf5e78402015-01-28 14:54:32 +01003126 * @state: CRTC state object to release
3127 *
3128 * Releases all resources stored in the CRTC state without actually freeing
3129 * the memory of the CRTC state. This is useful for drivers that subclass the
3130 * CRTC state.
3131 */
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003132void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003133{
Markus Elfring5f911902015-11-06 12:03:46 +01003134 drm_property_unreference_blob(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003135 drm_property_unreference_blob(state->degamma_lut);
3136 drm_property_unreference_blob(state->ctm);
3137 drm_property_unreference_blob(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003138}
3139EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3140
3141/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003142 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3143 * @crtc: drm CRTC
3144 * @state: CRTC state object to release
3145 *
3146 * Default CRTC state destroy hook for drivers which don't have their own
3147 * subclassed CRTC state structure.
3148 */
3149void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3150 struct drm_crtc_state *state)
3151{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003152 __drm_atomic_helper_crtc_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003153 kfree(state);
3154}
3155EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3156
3157/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003158 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
Daniel Vetterd4617012014-11-03 15:56:43 +01003159 * @plane: drm plane
3160 *
3161 * Resets the atomic state for @plane by freeing the state pointer (which might
3162 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3163 */
3164void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3165{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003166 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +02003167 __drm_atomic_helper_plane_destroy_state(plane->state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003168
Daniel Vetterd4617012014-11-03 15:56:43 +01003169 kfree(plane->state);
3170 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003171
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003172 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003173 plane->state->plane = plane;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003174 plane->state->rotation = DRM_ROTATE_0;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003175 }
Daniel Vetterd4617012014-11-03 15:56:43 +01003176}
3177EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3178
3179/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003180 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3181 * @plane: plane object
3182 * @state: atomic plane state
3183 *
3184 * Copies atomic state from a plane's current state. This is useful for
3185 * drivers that subclass the plane state.
3186 */
3187void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3188 struct drm_plane_state *state)
3189{
3190 memcpy(state, plane->state, sizeof(*state));
3191
3192 if (state->fb)
3193 drm_framebuffer_reference(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003194
3195 state->fence = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003196}
3197EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3198
3199/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003200 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3201 * @plane: drm plane
3202 *
3203 * Default plane state duplicate hook for drivers which don't have their own
3204 * subclassed plane state structure.
3205 */
3206struct drm_plane_state *
3207drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3208{
Daniel Vetter321ebf02014-11-04 22:57:27 +01003209 struct drm_plane_state *state;
3210
Daniel Vetterd4617012014-11-03 15:56:43 +01003211 if (WARN_ON(!plane->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_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003217
3218 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003219}
3220EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3221
3222/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003223 * __drm_atomic_helper_plane_destroy_state - release plane state
Thierry Redingf5e78402015-01-28 14:54:32 +01003224 * @state: plane state object to release
3225 *
3226 * Releases all resources stored in the plane state without actually freeing
3227 * the memory of the plane state. This is useful for drivers that subclass the
3228 * plane state.
3229 */
Daniel Vetter2f701692016-05-09 16:34:10 +02003230void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003231{
3232 if (state->fb)
3233 drm_framebuffer_unreference(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003234
3235 if (state->fence)
3236 dma_fence_put(state->fence);
Thierry Redingf5e78402015-01-28 14:54:32 +01003237}
3238EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3239
3240/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003241 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3242 * @plane: drm plane
3243 * @state: plane state object to release
3244 *
3245 * Default plane state destroy hook for drivers which don't have their own
3246 * subclassed plane state structure.
3247 */
3248void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01003249 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01003250{
Daniel Vetter2f701692016-05-09 16:34:10 +02003251 __drm_atomic_helper_plane_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003252 kfree(state);
3253}
3254EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3255
3256/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003257 * __drm_atomic_helper_connector_reset - reset state on connector
3258 * @connector: drm connector
3259 * @conn_state: connector state to assign
3260 *
3261 * Initializes the newly allocated @conn_state and assigns it to
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003262 * the &drm_conector->state pointer of @connector, usually required when
3263 * initializing the drivers or when called from the &drm_connector_funcs.reset
3264 * hook.
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003265 *
3266 * This is useful for drivers that subclass the connector state.
3267 */
3268void
3269__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3270 struct drm_connector_state *conn_state)
3271{
3272 if (conn_state)
3273 conn_state->connector = connector;
3274
3275 connector->state = conn_state;
3276}
3277EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3278
3279/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003280 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
Daniel Vetterd4617012014-11-03 15:56:43 +01003281 * @connector: drm connector
3282 *
3283 * Resets the atomic state for @connector by freeing the state pointer (which
3284 * might be NULL, e.g. at driver load time) and allocating a new empty state
3285 * object.
3286 */
3287void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3288{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003289 struct drm_connector_state *conn_state =
3290 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003291
Daniel Vetterb0b55112016-04-22 22:10:29 +02003292 if (connector->state)
Daniel Vetterfabd9102016-05-09 16:34:11 +02003293 __drm_atomic_helper_connector_destroy_state(connector->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003294
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003295 kfree(connector->state);
3296 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003297}
3298EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3299
3300/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003301 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3302 * @connector: connector object
3303 * @state: atomic connector state
3304 *
3305 * Copies atomic state from a connector's current state. This is useful for
3306 * drivers that subclass the connector state.
3307 */
3308void
3309__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3310 struct drm_connector_state *state)
3311{
3312 memcpy(state, connector->state, sizeof(*state));
Dave Airlied2307de2016-04-27 11:27:39 +10003313 if (state->crtc)
3314 drm_connector_reference(connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003315}
3316EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3317
3318/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003319 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3320 * @connector: drm connector
3321 *
3322 * Default connector state duplicate hook for drivers which don't have their own
3323 * subclassed connector state structure.
3324 */
3325struct drm_connector_state *
3326drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3327{
Thierry Redingf5e78402015-01-28 14:54:32 +01003328 struct drm_connector_state *state;
3329
Daniel Vetterd4617012014-11-03 15:56:43 +01003330 if (WARN_ON(!connector->state))
3331 return NULL;
3332
Thierry Redingf5e78402015-01-28 14:54:32 +01003333 state = kmalloc(sizeof(*state), GFP_KERNEL);
3334 if (state)
3335 __drm_atomic_helper_connector_duplicate_state(connector, state);
3336
3337 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003338}
3339EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3340
3341/**
Thierry Reding397fd772015-09-08 15:00:45 +02003342 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3343 * @dev: DRM device
3344 * @ctx: lock acquisition context
3345 *
3346 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01003347 * duplicating their respective states. This is used for example by suspend/
3348 * resume support code to save the state prior to suspend such that it can
3349 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02003350 *
3351 * Note that this treats atomic state as persistent between save and restore.
3352 * Drivers must make sure that this is possible and won't result in confusion
3353 * or erroneous behaviour.
3354 *
3355 * Note that if callers haven't already acquired all modeset locks this might
3356 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3357 *
3358 * Returns:
3359 * A pointer to the copy of the atomic state object on success or an
3360 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01003361 *
3362 * See also:
3363 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02003364 */
3365struct drm_atomic_state *
3366drm_atomic_helper_duplicate_state(struct drm_device *dev,
3367 struct drm_modeset_acquire_ctx *ctx)
3368{
3369 struct drm_atomic_state *state;
3370 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003371 struct drm_connector_list_iter conn_iter;
Thierry Reding397fd772015-09-08 15:00:45 +02003372 struct drm_plane *plane;
3373 struct drm_crtc *crtc;
3374 int err = 0;
3375
3376 state = drm_atomic_state_alloc(dev);
3377 if (!state)
3378 return ERR_PTR(-ENOMEM);
3379
3380 state->acquire_ctx = ctx;
3381
3382 drm_for_each_crtc(crtc, dev) {
3383 struct drm_crtc_state *crtc_state;
3384
3385 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3386 if (IS_ERR(crtc_state)) {
3387 err = PTR_ERR(crtc_state);
3388 goto free;
3389 }
3390 }
3391
3392 drm_for_each_plane(plane, dev) {
3393 struct drm_plane_state *plane_state;
3394
3395 plane_state = drm_atomic_get_plane_state(state, plane);
3396 if (IS_ERR(plane_state)) {
3397 err = PTR_ERR(plane_state);
3398 goto free;
3399 }
3400 }
3401
Daniel Vetterc36a3252016-12-15 16:58:43 +01003402 drm_connector_list_iter_get(dev, &conn_iter);
3403 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding397fd772015-09-08 15:00:45 +02003404 struct drm_connector_state *conn_state;
3405
3406 conn_state = drm_atomic_get_connector_state(state, conn);
3407 if (IS_ERR(conn_state)) {
3408 err = PTR_ERR(conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003409 drm_connector_list_iter_put(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003410 goto free;
3411 }
3412 }
Daniel Vetterc36a3252016-12-15 16:58:43 +01003413 drm_connector_list_iter_put(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003414
3415 /* clear the acquire context so that it isn't accidentally reused */
3416 state->acquire_ctx = NULL;
3417
3418free:
3419 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003420 drm_atomic_state_put(state);
Thierry Reding397fd772015-09-08 15:00:45 +02003421 state = ERR_PTR(err);
3422 }
3423
3424 return state;
3425}
3426EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3427
3428/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003429 * __drm_atomic_helper_connector_destroy_state - release connector state
Thierry Redingf5e78402015-01-28 14:54:32 +01003430 * @state: connector state object to release
3431 *
3432 * Releases all resources stored in the connector state without actually
3433 * freeing the memory of the connector state. This is useful for drivers that
3434 * subclass the connector state.
3435 */
3436void
Daniel Vetterfabd9102016-05-09 16:34:11 +02003437__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003438{
Dave Airlied2307de2016-04-27 11:27:39 +10003439 if (state->crtc)
3440 drm_connector_unreference(state->connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003441}
3442EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3443
3444/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003445 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3446 * @connector: drm connector
3447 * @state: connector state object to release
3448 *
3449 * Default connector state destroy hook for drivers which don't have their own
3450 * subclassed connector state structure.
3451 */
3452void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3453 struct drm_connector_state *state)
3454{
Daniel Vetterfabd9102016-05-09 16:34:11 +02003455 __drm_atomic_helper_connector_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003456 kfree(state);
3457}
3458EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003459
3460/**
3461 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3462 * @crtc: CRTC object
3463 * @red: red correction table
3464 * @green: green correction table
3465 * @blue: green correction table
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003466 * @size: size of the tables
3467 *
3468 * Implements support for legacy gamma correction table for drivers
3469 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3470 * properties.
3471 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003472int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3473 u16 *red, u16 *green, u16 *blue,
3474 uint32_t size)
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003475{
3476 struct drm_device *dev = crtc->dev;
3477 struct drm_mode_config *config = &dev->mode_config;
3478 struct drm_atomic_state *state;
3479 struct drm_crtc_state *crtc_state;
3480 struct drm_property_blob *blob = NULL;
3481 struct drm_color_lut *blob_data;
3482 int i, ret = 0;
3483
3484 state = drm_atomic_state_alloc(crtc->dev);
3485 if (!state)
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003486 return -ENOMEM;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003487
3488 blob = drm_property_create_blob(dev,
3489 sizeof(struct drm_color_lut) * size,
3490 NULL);
Lionel Landwerlin562c5b42016-03-10 12:04:21 +00003491 if (IS_ERR(blob)) {
3492 ret = PTR_ERR(blob);
Lionel Landwerlinc1f415c2016-03-11 12:17:26 +00003493 blob = NULL;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003494 goto fail;
3495 }
3496
3497 /* Prepare GAMMA_LUT with the legacy values. */
3498 blob_data = (struct drm_color_lut *) blob->data;
3499 for (i = 0; i < size; i++) {
3500 blob_data[i].red = red[i];
3501 blob_data[i].green = green[i];
3502 blob_data[i].blue = blue[i];
3503 }
3504
3505 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3506retry:
3507 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3508 if (IS_ERR(crtc_state)) {
3509 ret = PTR_ERR(crtc_state);
3510 goto fail;
3511 }
3512
3513 /* Reset DEGAMMA_LUT and CTM properties. */
3514 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3515 config->degamma_lut_property, 0);
3516 if (ret)
3517 goto fail;
3518
3519 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3520 config->ctm_property, 0);
3521 if (ret)
3522 goto fail;
3523
3524 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3525 config->gamma_lut_property, blob->base.id);
3526 if (ret)
3527 goto fail;
3528
3529 ret = drm_atomic_commit(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003530fail:
3531 if (ret == -EDEADLK)
3532 goto backoff;
3533
Chris Wilson08536952016-10-14 13:18:18 +01003534 drm_atomic_state_put(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003535 drm_property_unreference_blob(blob);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003536 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003537
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003538backoff:
3539 drm_atomic_state_clear(state);
3540 drm_atomic_legacy_backoff(state);
3541
3542 goto retry;
3543}
3544EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);