blob: b26e3419027ecd4a0ccea61cd3e45bd92b636321 [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
461 * down into ->mode_fixup functions of the driver backend.
462 *
463 * crtc_state->mode_changed is set when the input mode is changed.
464 * crtc_state->connectors_changed is set when a connector is added or
465 * removed from the crtc.
466 * crtc_state->active_changed is set when crtc_state->active changes,
467 * which is used for dpms.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100468 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100469 *
470 * IMPORTANT:
471 *
Brian Starkeyd807ed12016-10-13 10:47:08 +0100472 * Drivers which set ->mode_changed (e.g. in their ->atomic_check hooks if a
Daniel Vetterd9b13622014-11-26 16:57:41 +0100473 * plane update can't be done without a full modeset) _must_ call this function
474 * afterwards after that change. It is permitted to call this function multiple
475 * times for the same update, e.g. when the ->atomic_check functions depend upon
476 * the adjusted dotclock for fifo space allocation and watermark computation.
477 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200478 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100479 * Zero for success or -errno
480 */
481int
Rob Clark934ce1c2014-11-19 16:41:33 -0500482drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200483 struct drm_atomic_state *state)
484{
Daniel Vetter623369e2014-09-16 17:50:47 +0200485 struct drm_crtc *crtc;
486 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300487 struct drm_connector *connector;
488 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200489 int i, ret;
490
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300491 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200492 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200493 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
494 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200495 crtc_state->mode_changed = true;
496 }
497
498 if (crtc->state->enable != crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200499 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
500 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200501
502 /*
503 * For clarity this assignment is done here, but
504 * enable == 0 is only true when there are no
505 * connectors and a NULL mode.
506 *
507 * The other way around is true as well. enable != 0
508 * iff connectors are attached and a mode is set.
509 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200510 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200511 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200512 }
513 }
514
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100515 ret = handle_conflicting_encoders(state, state->legacy_set_config);
516 if (ret)
517 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100518
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300519 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200520 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100521 * This only sets crtc->connectors_changed for routing changes,
522 * drivers must set crtc->connectors_changed themselves when
523 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200524 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100525 ret = update_connector_routing(state, connector,
526 connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200527 if (ret)
528 return ret;
529 }
530
531 /*
532 * After all the routing has been prepared we need to add in any
533 * connector which is itself unchanged, but who's crtc changes it's
534 * configuration. This must be done before calling mode_fixup in case a
535 * crtc only changed its mode but has the same set of connectors.
536 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300537 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100538 bool has_connectors =
539 !!crtc_state->connector_mask;
Daniel Vetter623369e2014-09-16 17:50:47 +0200540
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100541 /*
542 * We must set ->active_changed after walking connectors for
543 * otherwise an update that only changes active would result in
544 * a full modeset because update_connector_routing force that.
545 */
546 if (crtc->state->active != crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200547 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
548 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100549 crtc_state->active_changed = true;
550 }
551
Daniel Vetter2465ff62015-06-18 09:58:55 +0200552 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100553 continue;
554
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200555 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
556 crtc->base.id, crtc->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100557 crtc_state->enable ? 'y' : 'n',
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200558 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200559
560 ret = drm_atomic_add_affected_connectors(state, crtc);
561 if (ret != 0)
562 return ret;
563
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200564 ret = drm_atomic_add_affected_planes(state, crtc);
565 if (ret != 0)
566 return ret;
567
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100568 if (crtc_state->enable != has_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200569 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
570 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200571
572 return -EINVAL;
573 }
574 }
575
576 return mode_fixup(state);
577}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100578EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200579
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100580/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800581 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100582 * @dev: DRM device
583 * @state: the driver state object
584 *
585 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100586 * This does all the plane update related checks using by calling into the
587 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100588 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200589 * It also sets crtc_state->planes_changed to indicate that a crtc has
590 * updated planes.
591 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200592 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100593 * Zero for success or -errno
594 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100595int
596drm_atomic_helper_check_planes(struct drm_device *dev,
597 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100598{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300599 struct drm_crtc *crtc;
600 struct drm_crtc_state *crtc_state;
601 struct drm_plane *plane;
602 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100603 int i, ret = 0;
604
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300605 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200606 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100607
608 funcs = plane->helper_private;
609
610 drm_atomic_helper_plane_changed(state, plane_state, plane);
611
612 if (!funcs || !funcs->atomic_check)
613 continue;
614
615 ret = funcs->atomic_check(plane, plane_state);
616 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200617 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
618 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100619 return ret;
620 }
621 }
622
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300623 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200624 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100625
626 funcs = crtc->helper_private;
627
628 if (!funcs || !funcs->atomic_check)
629 continue;
630
Daniel Vetterbe9174a2016-06-02 00:06:24 +0200631 ret = funcs->atomic_check(crtc, crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100632 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200633 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
634 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100635 return ret;
636 }
637 }
638
Daniel Vetterd9b13622014-11-26 16:57:41 +0100639 return ret;
640}
641EXPORT_SYMBOL(drm_atomic_helper_check_planes);
642
643/**
644 * drm_atomic_helper_check - validate state object
645 * @dev: DRM device
646 * @state: the driver state object
647 *
648 * Check the state object to see if the requested state is physically possible.
649 * Only crtcs and planes have check callbacks, so for any additional (global)
650 * checking that a driver needs it can simply wrap that around this function.
651 * Drivers without such needs can directly use this as their ->atomic_check()
652 * callback.
653 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100654 * This just wraps the two parts of the state checking for planes and modeset
655 * state in the default order: First it calls drm_atomic_helper_check_modeset()
656 * and then drm_atomic_helper_check_planes(). The assumption is that the
657 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
658 * e.g. properly compute watermarks.
659 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200660 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100661 * Zero for success or -errno
662 */
663int drm_atomic_helper_check(struct drm_device *dev,
664 struct drm_atomic_state *state)
665{
666 int ret;
667
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100668 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100669 if (ret)
670 return ret;
671
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100672 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500673 if (ret)
674 return ret;
675
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100676 return ret;
677}
678EXPORT_SYMBOL(drm_atomic_helper_check);
679
Daniel Vetter623369e2014-09-16 17:50:47 +0200680static void
681disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
682{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300683 struct drm_connector *connector;
684 struct drm_connector_state *old_conn_state;
685 struct drm_crtc *crtc;
686 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200687 int i;
688
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300689 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200690 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200691 struct drm_encoder *encoder;
692
Daniel Vetter623369e2014-09-16 17:50:47 +0200693 /* Shut down everything that's in the changeset and currently
694 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300695 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200696 continue;
697
Andrzej Hajda2943ef32016-03-15 13:46:00 +0100698 old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
699 old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100700
Daniel Vetter4218a322015-03-26 22:18:40 +0100701 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200702 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100703 continue;
704
Rob Clark46df9ad2014-11-20 15:40:36 -0500705 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200706
Rob Clark46df9ad2014-11-20 15:40:36 -0500707 /* We shouldn't get this far if we didn't previously have
708 * an encoder.. but WARN_ON() rather than explode.
709 */
710 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200711 continue;
712
713 funcs = encoder->helper_private;
714
Daniel Vetter17a38d92015-02-22 12:24:16 +0100715 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
716 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100717
Daniel Vetter623369e2014-09-16 17:50:47 +0200718 /*
719 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800720 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200721 */
Archit Taneja862e6862015-05-21 11:03:16 +0530722 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200723
724 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200725 if (funcs) {
726 if (connector->state->crtc && funcs->prepare)
727 funcs->prepare(encoder);
728 else if (funcs->disable)
729 funcs->disable(encoder);
730 else if (funcs->dpms)
731 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
732 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200733
Archit Taneja862e6862015-05-21 11:03:16 +0530734 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200735 }
736
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300737 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200738 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200739
740 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200741 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100742 continue;
743
744 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200745 continue;
746
747 funcs = crtc->helper_private;
748
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200749 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
750 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100751
752
Daniel Vetter623369e2014-09-16 17:50:47 +0200753 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100754 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200755 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +0800756 else if (funcs->atomic_disable)
757 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200758 else if (funcs->disable)
759 funcs->disable(crtc);
760 else
761 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
762 }
763}
764
Daniel Vetter4c18d302015-05-12 15:27:37 +0200765/**
766 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
767 * @dev: DRM device
768 * @old_state: atomic state object with old state structures
769 *
770 * This function updates all the various legacy modeset state pointers in
771 * connectors, encoders and crtcs. It also updates the timestamping constants
772 * used for precise vblank timestamps by calling
773 * drm_calc_timestamping_constants().
774 *
775 * Drivers can use this for building their own atomic commit if they don't have
776 * a pure helper-based modeset implementation.
777 */
778void
779drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
780 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200781{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300782 struct drm_connector *connector;
783 struct drm_connector_state *old_conn_state;
784 struct drm_crtc *crtc;
785 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200786 int i;
787
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200788 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300789 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200790 if (connector->encoder) {
791 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200792
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200793 connector->encoder->crtc = NULL;
794 connector->encoder = NULL;
795 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200796
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200797 crtc = connector->state->crtc;
798 if ((!crtc && old_conn_state->crtc) ||
799 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
800 struct drm_property *dpms_prop =
801 dev->mode_config.dpms_property;
802 int mode = DRM_MODE_DPMS_OFF;
803
804 if (crtc && crtc->state->active)
805 mode = DRM_MODE_DPMS_ON;
806
807 connector->dpms = mode;
808 drm_object_property_set_value(&connector->base,
809 dpms_prop, mode);
810 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200811 }
812
813 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300814 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
815 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200816 continue;
817
818 if (WARN_ON(!connector->state->best_encoder))
819 continue;
820
821 connector->encoder = connector->state->best_encoder;
822 connector->encoder->crtc = connector->state->crtc;
823 }
824
825 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300826 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200827 struct drm_plane *primary = crtc->primary;
828
Daniel Vetter623369e2014-09-16 17:50:47 +0200829 crtc->mode = crtc->state->mode;
830 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200831
832 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
833 primary->state->crtc == crtc) {
834 crtc->x = primary->state->src_x >> 16;
835 crtc->y = primary->state->src_y >> 16;
836 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200837
838 if (crtc->state->enable)
839 drm_calc_timestamping_constants(crtc,
840 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200841 }
842}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200843EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200844
845static void
846crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
847{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300848 struct drm_crtc *crtc;
849 struct drm_crtc_state *old_crtc_state;
850 struct drm_connector *connector;
851 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200852 int i;
853
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300854 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200855 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200856
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300857 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200858 continue;
859
860 funcs = crtc->helper_private;
861
Daniel Vetterc982bd92015-02-22 12:24:20 +0100862 if (crtc->state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200863 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
864 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100865
Daniel Vetter623369e2014-09-16 17:50:47 +0200866 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100867 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200868 }
869
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300870 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200871 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200872 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200873 struct drm_encoder *encoder;
874 struct drm_display_mode *mode, *adjusted_mode;
875
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300876 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200877 continue;
878
879 encoder = connector->state->best_encoder;
880 funcs = encoder->helper_private;
881 new_crtc_state = connector->state->crtc->state;
882 mode = &new_crtc_state->mode;
883 adjusted_mode = &new_crtc_state->adjusted_mode;
884
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100885 if (!new_crtc_state->mode_changed)
886 continue;
887
Daniel Vetter17a38d92015-02-22 12:24:16 +0100888 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
889 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100890
Daniel Vetter623369e2014-09-16 17:50:47 +0200891 /*
892 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800893 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200894 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200895 if (funcs && funcs->atomic_mode_set) {
896 funcs->atomic_mode_set(encoder, new_crtc_state,
897 connector->state);
898 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +0100899 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +0200900 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200901
Archit Taneja862e6862015-05-21 11:03:16 +0530902 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200903 }
904}
905
906/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100907 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200908 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100909 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200910 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100911 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200912 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100913 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300914 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100915 * drm_atomic_helper_commit_planes(), which is what the default commit function
916 * does. But drivers with different needs can group the modeset commits together
917 * and do the plane commits at the end. This is useful for drivers doing runtime
918 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200919 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100920void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
921 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200922{
Laurent Pincharta072f802015-02-22 12:24:18 +0100923 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200924
925 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
926
Laurent Pincharta072f802015-02-22 12:24:18 +0100927 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200928}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100929EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200930
931/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100932 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200933 * @dev: DRM device
934 * @old_state: atomic state object with old state structures
935 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100936 * This function enables all the outputs with the new configuration which had to
937 * be turned off for the update.
938 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300939 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100940 * drm_atomic_helper_commit_planes(), which is what the default commit function
941 * does. But drivers with different needs can group the modeset commits together
942 * and do the plane commits at the end. This is useful for drivers doing runtime
943 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200944 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100945void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
946 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200947{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300948 struct drm_crtc *crtc;
949 struct drm_crtc_state *old_crtc_state;
950 struct drm_connector *connector;
951 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200952 int i;
953
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300954 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200955 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200956
957 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200958 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100959 continue;
960
961 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200962 continue;
963
964 funcs = crtc->helper_private;
965
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100966 if (crtc->state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200967 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
968 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100969
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100970 if (funcs->enable)
971 funcs->enable(crtc);
972 else
973 funcs->commit(crtc);
974 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200975 }
976
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300977 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200978 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200979 struct drm_encoder *encoder;
980
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300981 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200982 continue;
983
Daniel Vetter4218a322015-03-26 22:18:40 +0100984 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200985 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100986 continue;
987
Daniel Vetter623369e2014-09-16 17:50:47 +0200988 encoder = connector->state->best_encoder;
989 funcs = encoder->helper_private;
990
Daniel Vetter17a38d92015-02-22 12:24:16 +0100991 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
992 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100993
Daniel Vetter623369e2014-09-16 17:50:47 +0200994 /*
995 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800996 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200997 */
Archit Taneja862e6862015-05-21 11:03:16 +0530998 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200999
Noralf Trønnes28276352016-05-11 18:09:20 +02001000 if (funcs) {
1001 if (funcs->enable)
1002 funcs->enable(encoder);
1003 else if (funcs->commit)
1004 funcs->commit(encoder);
1005 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001006
Archit Taneja862e6862015-05-21 11:03:16 +05301007 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001008 }
1009}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001010EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001011
Rob Clark4c5b7f32016-03-18 19:14:55 -04001012/**
1013 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1014 * @dev: DRM device
1015 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001016 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1017 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001018 *
1019 * For implicit sync, driver should fish the exclusive fence out from the
1020 * incoming fb's and stash it in the drm_plane_state. This is called after
1021 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1022 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001023 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001024 * Note that @pre_swap is needed since the point where we block for fences moves
1025 * around depending upon whether an atomic commit is blocking or
1026 * non-blocking. For async commit all waiting needs to happen after
1027 * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1028 * to wait **before** we do anything that can't be easily rolled back. That is
1029 * before we call drm_atomic_helper_swap_state().
1030 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001031 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001032 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001033int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1034 struct drm_atomic_state *state,
1035 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001036{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001037 struct drm_plane *plane;
1038 struct drm_plane_state *plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001039 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001040
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001041 for_each_plane_in_state(state, plane, plane_state, i) {
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001042 if (!pre_swap)
1043 plane_state = plane->state;
1044
1045 if (!plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001046 continue;
1047
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001048 WARN_ON(!plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001049
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001050 /*
1051 * If waiting for fences pre-swap (ie: nonblock), userspace can
1052 * still interrupt the operation. Instead of blocking until the
1053 * timer expires, make the wait interruptible.
1054 */
Chris Wilsonf54d1862016-10-25 13:00:45 +01001055 ret = dma_fence_wait(plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001056 if (ret)
1057 return ret;
1058
Chris Wilsonf54d1862016-10-25 13:00:45 +01001059 dma_fence_put(plane_state->fence);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001060 plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001061 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001062
1063 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001064}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001065EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001066
John Keepingc2409062016-01-19 10:46:58 +00001067/**
Rob Clark5ee32292014-11-11 19:38:59 -05001068 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1069 * @dev: DRM device
1070 * @old_state: atomic state object with old state structures
1071 *
1072 * Helper to, after atomic commit, wait for vblanks on all effected
1073 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001074 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1075 * framebuffers have actually changed to optimize for the legacy cursor and
1076 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001077 */
1078void
1079drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1080 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001081{
1082 struct drm_crtc *crtc;
1083 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001084 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001085 unsigned crtc_mask = 0;
1086
1087 /*
1088 * Legacy cursor ioctls are completely unsynced, and userspace
1089 * relies on that (by doing tons of cursor updates).
1090 */
1091 if (old_state->legacy_cursor_update)
1092 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001093
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001094 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001095 struct drm_crtc_state *new_crtc_state = crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001096
Maarten Lankhorsta3fbb532016-12-08 14:45:27 +01001097 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
Daniel Vetterab58e332014-11-24 20:42:42 +01001098 continue;
1099
Daniel Vetter623369e2014-09-16 17:50:47 +02001100 ret = drm_crtc_vblank_get(crtc);
1101 if (ret != 0)
1102 continue;
1103
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001104 crtc_mask |= drm_crtc_mask(crtc);
1105 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001106 }
1107
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001108 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001109 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001110 continue;
1111
1112 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001113 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001114 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001115 msecs_to_jiffies(50));
1116
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001117 WARN(!ret, "[CRTC:%d] vblank wait timed out\n", crtc->base.id);
1118
Daniel Vetter623369e2014-09-16 17:50:47 +02001119 drm_crtc_vblank_put(crtc);
1120 }
1121}
Rob Clark5ee32292014-11-11 19:38:59 -05001122EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001123
1124/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001125 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001126 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001127 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001128 * This is the default implemenation for the ->atomic_commit_tail() hook of the
1129 * &drm_mode_config_helper_funcs vtable.
Daniel Vetter623369e2014-09-16 17:50:47 +02001130 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001131 * Note that the default ordering of how the various stages are called is to
1132 * match the legacy modeset helper library closest. One peculiarity of that is
1133 * that it doesn't mesh well with runtime PM at all.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001134 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001135 * For drivers supporting runtime PM the recommended sequence is instead ::
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001136 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001137 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001138 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001139 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001140 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001141 * drm_atomic_helper_commit_planes(dev, old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001142 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001143 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001144 * for committing the atomic update to hardware. See the kerneldoc entries for
1145 * these three functions for more details.
1146 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001147void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001148{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001149 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001150
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001151 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001152
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001153 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001154
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001155 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001156
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001157 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001158
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001159 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001160
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001161 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001162}
1163EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1164
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001165static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001166{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001167 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001168 struct drm_mode_config_helper_funcs *funcs;
1169
1170 funcs = dev->mode_config.helper_private;
1171
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001172 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001173
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001174 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001175
1176 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001177 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001178 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001179 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001180
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001181 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001182
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001183 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001184}
1185
1186static void commit_work(struct work_struct *work)
1187{
1188 struct drm_atomic_state *state = container_of(work,
1189 struct drm_atomic_state,
1190 commit_work);
1191 commit_tail(state);
1192}
1193
1194/**
1195 * drm_atomic_helper_commit - commit validated state object
1196 * @dev: DRM device
1197 * @state: the driver state object
1198 * @nonblock: whether nonblocking behavior is requested.
1199 *
1200 * This function commits a with drm_atomic_helper_check() pre-validated state
1201 * object. This can still fail when e.g. the framebuffer reservation fails. This
1202 * function implements nonblocking commits, using
1203 * drm_atomic_helper_setup_commit() and related functions.
1204 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001205 * Committing the actual hardware state is done through the
1206 * ->atomic_commit_tail() callback of the &drm_mode_config_helper_funcs vtable,
1207 * or it's default implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001208 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001209 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001210 * Zero for success or -errno.
1211 */
1212int drm_atomic_helper_commit(struct drm_device *dev,
1213 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001214 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001215{
1216 int ret;
1217
Daniel Vettera095caa2016-06-08 17:15:36 +02001218 ret = drm_atomic_helper_setup_commit(state, nonblock);
1219 if (ret)
1220 return ret;
1221
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001222 INIT_WORK(&state->commit_work, commit_work);
1223
Daniel Vetter623369e2014-09-16 17:50:47 +02001224 ret = drm_atomic_helper_prepare_planes(dev, state);
1225 if (ret)
1226 return ret;
1227
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001228 if (!nonblock) {
1229 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001230 if (ret) {
1231 drm_atomic_helper_cleanup_planes(dev, state);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001232 return ret;
Laurent Pinchartaebe55c2017-01-03 01:14:27 +02001233 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001234 }
1235
Daniel Vetter623369e2014-09-16 17:50:47 +02001236 /*
1237 * This is the point of no return - everything below never fails except
1238 * when the hw goes bonghits. Which means we can commit the new state on
1239 * the software side now.
1240 */
1241
Daniel Vetter5e84c262016-06-10 00:06:32 +02001242 drm_atomic_helper_swap_state(state, true);
Daniel Vetter623369e2014-09-16 17:50:47 +02001243
1244 /*
1245 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001246 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001247 * that the asynchronous work has either been cancelled (if the driver
1248 * supports it, which at least requires that the framebuffers get
1249 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1250 * before the new state gets committed on the software side with
1251 * drm_atomic_helper_swap_state().
1252 *
1253 * This scheme allows new atomic state updates to be prepared and
1254 * checked in parallel to the asynchronous completion of the previous
1255 * update. Which is important since compositors need to figure out the
1256 * composition of the next frame right after having submitted the
1257 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001258 *
1259 * NOTE: Commit work has multiple phases, first hardware commit, then
1260 * cleanup. We want them to overlap, hence need system_unbound_wq to
1261 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001262 */
1263
Chris Wilson08536952016-10-14 13:18:18 +01001264 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001265 if (nonblock)
1266 queue_work(system_unbound_wq, &state->commit_work);
1267 else
1268 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001269
1270 return 0;
1271}
1272EXPORT_SYMBOL(drm_atomic_helper_commit);
1273
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001274/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001275 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001276 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001277 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001278 *
1279 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1280 * which commit needs to call which can fail, so we want to run it first and
1281 * synchronously.
1282 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001283 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001284 * might be affected the new state update. This can be done by either cancelling
1285 * or flushing the work items, depending upon whether the driver can deal with
1286 * cancelled updates. Note that it is important to ensure that the framebuffer
1287 * cleanup is still done when cancelling.
1288 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001289 * Asynchronous workers need to have sufficient parallelism to be able to run
1290 * different atomic commits on different CRTCs in parallel. The simplest way to
1291 * achive this is by running them on the &system_unbound_wq work queue. Note
1292 * that drivers are not required to split up atomic commits and run an
1293 * individual commit in parallel - userspace is supposed to do that if it cares.
1294 * But it might be beneficial to do that for modesets, since those necessarily
1295 * must be done as one global operation, and enabling or disabling a CRTC can
1296 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001297 *
1298 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001299 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001300 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001301 * while it's guaranteed that no relevant nonblocking worker runs means that
1302 * nonblocking workers do not need grab any locks. Actually they must not grab
1303 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001304 *
1305 * 4. Schedule a work item to do all subsequent steps, using the split-out
1306 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1307 * then cleaning up the framebuffers after the old framebuffer is no longer
1308 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001309 *
1310 * The above scheme is implemented in the atomic helper libraries in
1311 * drm_atomic_helper_commit() using a bunch of helper functions. See
1312 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001313 */
1314
Daniel Vettera095caa2016-06-08 17:15:36 +02001315static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1316{
1317 struct drm_crtc_commit *commit, *stall_commit = NULL;
1318 bool completed = true;
1319 int i;
1320 long ret = 0;
1321
1322 spin_lock(&crtc->commit_lock);
1323 i = 0;
1324 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1325 if (i == 0) {
1326 completed = try_wait_for_completion(&commit->flip_done);
1327 /* Userspace is not allowed to get ahead of the previous
1328 * commit with nonblocking ones. */
1329 if (!completed && nonblock) {
1330 spin_unlock(&crtc->commit_lock);
1331 return -EBUSY;
1332 }
1333 } else if (i == 1) {
1334 stall_commit = commit;
1335 drm_crtc_commit_get(stall_commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001336 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001337 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001338
1339 i++;
1340 }
1341 spin_unlock(&crtc->commit_lock);
1342
1343 if (!stall_commit)
1344 return 0;
1345
1346 /* We don't want to let commits get ahead of cleanup work too much,
1347 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1348 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001349 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001350 10*HZ);
1351 if (ret == 0)
1352 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1353 crtc->base.id, crtc->name);
1354
1355 drm_crtc_commit_put(stall_commit);
1356
1357 return ret < 0 ? ret : 0;
1358}
1359
Daniel Vetter24835e42016-12-21 11:23:30 +01001360void release_crtc_commit(struct completion *completion)
1361{
1362 struct drm_crtc_commit *commit = container_of(completion,
1363 typeof(*commit),
1364 flip_done);
1365
1366 drm_crtc_commit_put(commit);
1367}
1368
Daniel Vettera095caa2016-06-08 17:15:36 +02001369/**
1370 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1371 * @state: new modeset state to be committed
1372 * @nonblock: whether nonblocking behavior is requested.
1373 *
1374 * This function prepares @state to be used by the atomic helper's support for
1375 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1376 * should always call this function from their ->atomic_commit hook.
1377 *
1378 * To be able to use this support drivers need to use a few more helper
1379 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1380 * actually committing the hardware state, and for nonblocking commits this call
1381 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1382 * and it's stall parameter, for when a driver's commit hooks look at the
Daniel Vetterea0dd852016-12-29 21:48:26 +01001383 * ->state pointers of &struct drm_crtc, &drm_plane or &drm_connector directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001384 *
1385 * Completion of the hardware commit step must be signalled using
1386 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1387 * to read or change any permanent software or hardware modeset state. The only
1388 * exception is state protected by other means than &drm_modeset_lock locks.
1389 * Only the free standing @state with pointers to the old state structures can
1390 * be inspected, e.g. to clean up old buffers using
1391 * drm_atomic_helper_cleanup_planes().
1392 *
1393 * At the very end, before cleaning up @state drivers must call
1394 * drm_atomic_helper_commit_cleanup_done().
1395 *
1396 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1397 * complete and esay-to-use default implementation of the atomic_commit() hook.
1398 *
1399 * The tracking of asynchronously executed and still pending commits is done
1400 * using the core structure &drm_crtc_commit.
1401 *
1402 * By default there's no need to clean up resources allocated by this function
1403 * explicitly: drm_atomic_state_default_clear() will take care of that
1404 * automatically.
1405 *
1406 * Returns:
1407 *
1408 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1409 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1410 */
1411int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1412 bool nonblock)
1413{
1414 struct drm_crtc *crtc;
1415 struct drm_crtc_state *crtc_state;
1416 struct drm_crtc_commit *commit;
1417 int i, ret;
1418
1419 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1420 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1421 if (!commit)
1422 return -ENOMEM;
1423
1424 init_completion(&commit->flip_done);
1425 init_completion(&commit->hw_done);
1426 init_completion(&commit->cleanup_done);
1427 INIT_LIST_HEAD(&commit->commit_entry);
1428 kref_init(&commit->ref);
1429 commit->crtc = crtc;
1430
1431 state->crtcs[i].commit = commit;
1432
1433 ret = stall_checks(crtc, nonblock);
1434 if (ret)
1435 return ret;
1436
1437 /* Drivers only send out events when at least either current or
1438 * new CRTC state is active. Complete right away if everything
1439 * stays off. */
1440 if (!crtc->state->active && !crtc_state->active) {
1441 complete_all(&commit->flip_done);
1442 continue;
1443 }
1444
1445 /* Legacy cursor updates are fully unsynced. */
1446 if (state->legacy_cursor_update) {
1447 complete_all(&commit->flip_done);
1448 continue;
1449 }
1450
1451 if (!crtc_state->event) {
1452 commit->event = kzalloc(sizeof(*commit->event),
1453 GFP_KERNEL);
1454 if (!commit->event)
1455 return -ENOMEM;
1456
1457 crtc_state->event = commit->event;
1458 }
1459
1460 crtc_state->event->base.completion = &commit->flip_done;
Daniel Vetter24835e42016-12-21 11:23:30 +01001461 crtc_state->event->base.completion_release = release_crtc_commit;
1462 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001463 }
1464
1465 return 0;
1466}
1467EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1468
1469
1470static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1471{
1472 struct drm_crtc_commit *commit;
1473 int i = 0;
1474
1475 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1476 /* skip the first entry, that's the current commit */
1477 if (i == 1)
1478 return commit;
1479 i++;
1480 }
1481
1482 return NULL;
1483}
1484
1485/**
1486 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001487 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001488 *
1489 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001490 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02001491 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1492 * by calling drm_crtc_vblank_send_event on the event member of
1493 * &drm_crtc_state).
1494 *
1495 * This is part of the atomic helper support for nonblocking commits, see
1496 * drm_atomic_helper_setup_commit() for an overview.
1497 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001498void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001499{
1500 struct drm_crtc *crtc;
1501 struct drm_crtc_state *crtc_state;
1502 struct drm_crtc_commit *commit;
1503 int i;
1504 long ret;
1505
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001506 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001507 spin_lock(&crtc->commit_lock);
1508 commit = preceeding_commit(crtc);
1509 if (commit)
1510 drm_crtc_commit_get(commit);
1511 spin_unlock(&crtc->commit_lock);
1512
1513 if (!commit)
1514 continue;
1515
1516 ret = wait_for_completion_timeout(&commit->hw_done,
1517 10*HZ);
1518 if (ret == 0)
1519 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1520 crtc->base.id, crtc->name);
1521
1522 /* Currently no support for overwriting flips, hence
1523 * stall for previous one to execute completely. */
1524 ret = wait_for_completion_timeout(&commit->flip_done,
1525 10*HZ);
1526 if (ret == 0)
1527 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1528 crtc->base.id, crtc->name);
1529
1530 drm_crtc_commit_put(commit);
1531 }
1532}
1533EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1534
1535/**
1536 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001537 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001538 *
1539 * This function is used to signal completion of the hardware commit step. After
1540 * this step the driver is not allowed to read or change any permanent software
1541 * or hardware modeset state. The only exception is state protected by other
1542 * means than &drm_modeset_lock locks.
1543 *
1544 * Drivers should try to postpone any expensive or delayed cleanup work after
1545 * this function is called.
1546 *
1547 * This is part of the atomic helper support for nonblocking commits, see
1548 * drm_atomic_helper_setup_commit() for an overview.
1549 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001550void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001551{
1552 struct drm_crtc *crtc;
1553 struct drm_crtc_state *crtc_state;
1554 struct drm_crtc_commit *commit;
1555 int i;
1556
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001557 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1558 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001559 if (!commit)
1560 continue;
1561
1562 /* backend must have consumed any event by now */
1563 WARN_ON(crtc->state->event);
1564 spin_lock(&crtc->commit_lock);
1565 complete_all(&commit->hw_done);
1566 spin_unlock(&crtc->commit_lock);
1567 }
1568}
1569EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1570
1571/**
1572 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001573 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02001574 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001575 * This signals completion of the atomic update @old_state, including any
1576 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01001577 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02001578 *
1579 * This is part of the atomic helper support for nonblocking commits, see
1580 * drm_atomic_helper_setup_commit() for an overview.
1581 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001582void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02001583{
1584 struct drm_crtc *crtc;
1585 struct drm_crtc_state *crtc_state;
1586 struct drm_crtc_commit *commit;
1587 int i;
1588 long ret;
1589
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001590 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1591 commit = old_state->crtcs[i].commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001592 if (WARN_ON(!commit))
1593 continue;
1594
1595 spin_lock(&crtc->commit_lock);
1596 complete_all(&commit->cleanup_done);
1597 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1598
1599 /* commit_list borrows our reference, need to remove before we
1600 * clean up our drm_atomic_state. But only after it actually
1601 * completed, otherwise subsequent commits won't stall properly. */
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001602 if (try_wait_for_completion(&commit->flip_done))
1603 goto del_commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001604
1605 spin_unlock(&crtc->commit_lock);
1606
1607 /* We must wait for the vblank event to signal our completion
1608 * before releasing our reference, since the vblank work does
1609 * not hold a reference of its own. */
1610 ret = wait_for_completion_timeout(&commit->flip_done,
1611 10*HZ);
1612 if (ret == 0)
1613 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1614 crtc->base.id, crtc->name);
1615
1616 spin_lock(&crtc->commit_lock);
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001617del_commit:
Daniel Vettera095caa2016-06-08 17:15:36 +02001618 list_del(&commit->commit_entry);
1619 spin_unlock(&crtc->commit_lock);
1620 }
1621}
1622EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1623
Daniel Vettere8c833a2014-07-27 18:30:19 +02001624/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001625 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001626 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001627 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001628 *
1629 * This function prepares plane state, specifically framebuffers, for the new
1630 * configuration. If any failure is encountered this function will call
1631 * ->cleanup_fb on any already successfully prepared framebuffer.
1632 *
1633 * Returns:
1634 * 0 on success, negative error code on failure.
1635 */
1636int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1637 struct drm_atomic_state *state)
1638{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001639 struct drm_plane *plane;
1640 struct drm_plane_state *plane_state;
1641 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001642
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001643 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001644 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001645
1646 funcs = plane->helper_private;
1647
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001648 if (funcs->prepare_fb) {
1649 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001650 if (ret)
1651 goto fail;
1652 }
1653 }
1654
1655 return 0;
1656
1657fail:
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001658 for_each_plane_in_state(state, plane, plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001659 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001660
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001661 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001662 continue;
1663
1664 funcs = plane->helper_private;
1665
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001666 if (funcs->cleanup_fb)
1667 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001668 }
1669
1670 return ret;
1671}
1672EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1673
Ville Syrjälä7135ac52016-09-19 16:33:42 +03001674static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001675{
1676 return state->crtc && state->crtc->state->active;
1677}
1678
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001679/**
1680 * drm_atomic_helper_commit_planes - commit plane state
1681 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001682 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08001683 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001684 *
1685 * This function commits the new plane state using the plane and atomic helper
1686 * functions for planes and crtcs. It assumes that the atomic state has already
1687 * been pushed into the relevant object state pointers, since this step can no
1688 * longer fail.
1689 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001690 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001691 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001692 *
1693 * Note that this function does all plane updates across all CRTCs in one step.
1694 * If the hardware can't support this approach look at
1695 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001696 *
1697 * Plane parameters can be updated by applications while the associated CRTC is
1698 * disabled. The DRM/KMS core will store the parameters in the plane state,
1699 * which will be available to the driver when the CRTC is turned on. As a result
1700 * most drivers don't need to be immediately notified of plane updates for a
1701 * disabled CRTC.
1702 *
Liu Ying2b58e982016-08-29 17:12:03 +08001703 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1704 * @flags in order not to receive plane update notifications related to a
1705 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001706 * driver code when the driver and/or hardware can't or just don't need to deal
1707 * with updates on disabled CRTCs, for example when supporting runtime PM.
1708 *
Liu Ying2b58e982016-08-29 17:12:03 +08001709 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1710 * display controllers require to disable a CRTC's planes when the CRTC is
1711 * disabled. This function would skip the ->atomic_disable call for a plane if
1712 * the CRTC of the old plane state needs a modesetting operation. Of course,
1713 * the drivers need to disable the planes in their CRTC disable callbacks
1714 * since no one else would do that.
1715 *
1716 * The drm_atomic_helper_commit() default implementation doesn't set the
1717 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1718 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001719 */
1720void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001721 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08001722 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001723{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001724 struct drm_crtc *crtc;
1725 struct drm_crtc_state *old_crtc_state;
1726 struct drm_plane *plane;
1727 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001728 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08001729 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1730 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001731
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001732 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001733 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001734
1735 funcs = crtc->helper_private;
1736
1737 if (!funcs || !funcs->atomic_begin)
1738 continue;
1739
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001740 if (active_only && !crtc->state->active)
1741 continue;
1742
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001743 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001744 }
1745
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001746 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001747 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001748 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001749
1750 funcs = plane->helper_private;
1751
Thierry Reding3cad4b62014-11-25 13:05:12 +01001752 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001753 continue;
1754
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001755 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1756
1757 if (active_only) {
1758 /*
1759 * Skip planes related to inactive CRTCs. If the plane
1760 * is enabled use the state of the current CRTC. If the
1761 * plane is being disabled use the state of the old
1762 * CRTC to avoid skipping planes being disabled on an
1763 * active CRTC.
1764 */
1765 if (!disabling && !plane_crtc_active(plane->state))
1766 continue;
1767 if (disabling && !plane_crtc_active(old_plane_state))
1768 continue;
1769 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001770
Thierry Reding407b8bd2014-11-20 12:05:50 +01001771 /*
1772 * Special-case disabling the plane if drivers support it.
1773 */
Liu Ying2b58e982016-08-29 17:12:03 +08001774 if (disabling && funcs->atomic_disable) {
1775 struct drm_crtc_state *crtc_state;
1776
1777 crtc_state = old_plane_state->crtc->state;
1778
1779 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1780 no_disable)
1781 continue;
1782
Thierry Reding407b8bd2014-11-20 12:05:50 +01001783 funcs->atomic_disable(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08001784 } else if (plane->state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01001785 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08001786 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001787 }
1788
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001789 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001790 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001791
1792 funcs = crtc->helper_private;
1793
1794 if (!funcs || !funcs->atomic_flush)
1795 continue;
1796
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001797 if (active_only && !crtc->state->active)
1798 continue;
1799
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001800 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001801 }
1802}
1803EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1804
1805/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001806 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1807 * @old_crtc_state: atomic state object with the old crtc state
1808 *
1809 * This function commits the new plane state using the plane and atomic helper
1810 * functions for planes on the specific crtc. It assumes that the atomic state
1811 * has already been pushed into the relevant object state pointers, since this
1812 * step can no longer fail.
1813 *
1814 * This function is useful when plane updates should be done crtc-by-crtc
1815 * instead of one global step like drm_atomic_helper_commit_planes() does.
1816 *
1817 * This function can only be savely used when planes are not allowed to move
1818 * between different CRTCs because this function doesn't handle inter-CRTC
1819 * depencies. Callers need to ensure that either no such depencies exist,
1820 * resolve them through ordering of commit calls or through some other means.
1821 */
1822void
1823drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1824{
1825 const struct drm_crtc_helper_funcs *crtc_funcs;
1826 struct drm_crtc *crtc = old_crtc_state->crtc;
1827 struct drm_atomic_state *old_state = old_crtc_state->state;
1828 struct drm_plane *plane;
1829 unsigned plane_mask;
1830
1831 plane_mask = old_crtc_state->plane_mask;
1832 plane_mask |= crtc->state->plane_mask;
1833
1834 crtc_funcs = crtc->helper_private;
1835 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001836 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001837
1838 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1839 struct drm_plane_state *old_plane_state =
1840 drm_atomic_get_existing_plane_state(old_state, plane);
1841 const struct drm_plane_helper_funcs *plane_funcs;
1842
1843 plane_funcs = plane->helper_private;
1844
1845 if (!old_plane_state || !plane_funcs)
1846 continue;
1847
1848 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1849
1850 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1851 plane_funcs->atomic_disable)
1852 plane_funcs->atomic_disable(plane, old_plane_state);
1853 else if (plane->state->crtc ||
1854 drm_atomic_plane_disabling(plane, old_plane_state))
1855 plane_funcs->atomic_update(plane, old_plane_state);
1856 }
1857
1858 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001859 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001860}
1861EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1862
1863/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001864 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08001865 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02001866 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1867 *
1868 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08001869 * used for instance in the CRTC helper atomic_disable callback to disable
1870 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02001871 *
1872 * If the atomic-parameter is set the function calls the CRTC's
1873 * atomic_begin hook before and atomic_flush hook after disabling the
1874 * planes.
1875 *
1876 * It is a bug to call this function without having implemented the
1877 * ->atomic_disable() plane hook.
1878 */
Liu Ying28500292016-08-26 15:30:39 +08001879void
1880drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1881 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001882{
Liu Ying28500292016-08-26 15:30:39 +08001883 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02001884 const struct drm_crtc_helper_funcs *crtc_funcs =
1885 crtc->helper_private;
1886 struct drm_plane *plane;
1887
1888 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1889 crtc_funcs->atomic_begin(crtc, NULL);
1890
Liu Ying28500292016-08-26 15:30:39 +08001891 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02001892 const struct drm_plane_helper_funcs *plane_funcs =
1893 plane->helper_private;
1894
Liu Ying28500292016-08-26 15:30:39 +08001895 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02001896 continue;
1897
1898 WARN_ON(!plane_funcs->atomic_disable);
1899 if (plane_funcs->atomic_disable)
1900 plane_funcs->atomic_disable(plane, NULL);
1901 }
1902
1903 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1904 crtc_funcs->atomic_flush(crtc, NULL);
1905}
1906EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1907
1908/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001909 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1910 * @dev: DRM device
1911 * @old_state: atomic state object with old state structures
1912 *
1913 * This function cleans up plane state, specifically framebuffers, from the old
1914 * configuration. Hence the old configuration must be perserved in @old_state to
1915 * be able to call this function.
1916 *
1917 * This function must also be called on the new state when the atomic update
1918 * fails at any point after calling drm_atomic_helper_prepare_planes().
1919 */
1920void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1921 struct drm_atomic_state *old_state)
1922{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001923 struct drm_plane *plane;
1924 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001925 int i;
1926
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001927 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001928 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001929
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001930 funcs = plane->helper_private;
1931
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001932 if (funcs->cleanup_fb)
1933 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001934 }
1935}
1936EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1937
1938/**
1939 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001940 * @state: atomic state
Daniel Vetter5e84c262016-06-10 00:06:32 +02001941 * @stall: stall for proceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001942 *
1943 * This function stores the atomic state into the current state pointers in all
1944 * driver objects. It should be called after all failing steps have been done
1945 * and succeeded, but before the actual hardware state is committed.
1946 *
1947 * For cleanup and error recovery the current state for all changed objects will
1948 * be swaped into @state.
1949 *
1950 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1951 *
1952 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1953 *
1954 * 2. Do any other steps that might fail.
1955 *
1956 * 3. Put the staged state into the current state pointers with this function.
1957 *
1958 * 4. Actually commit the hardware state.
1959 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001960 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001961 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02001962 *
1963 * @stall must be set when nonblocking commits for this driver directly access
1964 * the ->state pointer of &drm_plane, &drm_crtc or &drm_connector. With the
1965 * current atomic helpers this is almost always the case, since the helpers
1966 * don't pass the right state structures to the callbacks.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001967 */
Daniel Vetter5e84c262016-06-10 00:06:32 +02001968void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1969 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001970{
1971 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02001972 long ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001973 struct drm_connector *connector;
1974 struct drm_connector_state *conn_state;
1975 struct drm_crtc *crtc;
1976 struct drm_crtc_state *crtc_state;
1977 struct drm_plane *plane;
1978 struct drm_plane_state *plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001979 struct drm_crtc_commit *commit;
1980
1981 if (stall) {
1982 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1983 spin_lock(&crtc->commit_lock);
1984 commit = list_first_entry_or_null(&crtc->commit_list,
1985 struct drm_crtc_commit, commit_entry);
1986 if (commit)
1987 drm_crtc_commit_get(commit);
1988 spin_unlock(&crtc->commit_lock);
1989
1990 if (!commit)
1991 continue;
1992
1993 ret = wait_for_completion_timeout(&commit->hw_done,
1994 10*HZ);
1995 if (ret == 0)
1996 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1997 crtc->base.id, crtc->name);
1998 drm_crtc_commit_put(commit);
1999 }
2000 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002001
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002002 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002003 connector->state->state = state;
Daniel Vetter63e83c12016-06-02 00:06:32 +02002004 swap(state->connectors[i].state, connector->state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002005 connector->state->state = NULL;
2006 }
2007
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002008 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002009 crtc->state->state = state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +02002010 swap(state->crtcs[i].state, crtc->state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002011 crtc->state->state = NULL;
Daniel Vettera095caa2016-06-08 17:15:36 +02002012
2013 if (state->crtcs[i].commit) {
2014 spin_lock(&crtc->commit_lock);
2015 list_add(&state->crtcs[i].commit->commit_entry,
2016 &crtc->commit_list);
2017 spin_unlock(&crtc->commit_lock);
2018
2019 state->crtcs[i].commit->event = NULL;
2020 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002021 }
2022
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002023 for_each_plane_in_state(state, plane, plane_state, i) {
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002024 plane->state->state = state;
Daniel Vetterb8b53422016-06-02 00:06:33 +02002025 swap(state->planes[i].state, plane->state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002026 plane->state->state = NULL;
2027 }
2028}
2029EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002030
2031/**
2032 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2033 * @plane: plane object to update
2034 * @crtc: owning CRTC of owning plane
2035 * @fb: framebuffer to flip onto plane
2036 * @crtc_x: x offset of primary plane on crtc
2037 * @crtc_y: y offset of primary plane on crtc
2038 * @crtc_w: width of primary plane rectangle on crtc
2039 * @crtc_h: height of primary plane rectangle on crtc
2040 * @src_x: x offset of @fb for panning
2041 * @src_y: y offset of @fb for panning
2042 * @src_w: width of source rectangle in @fb
2043 * @src_h: height of source rectangle in @fb
2044 *
2045 * Provides a default plane update handler using the atomic driver interface.
2046 *
2047 * RETURNS:
2048 * Zero on success, error code on failure
2049 */
2050int drm_atomic_helper_update_plane(struct drm_plane *plane,
2051 struct drm_crtc *crtc,
2052 struct drm_framebuffer *fb,
2053 int crtc_x, int crtc_y,
2054 unsigned int crtc_w, unsigned int crtc_h,
2055 uint32_t src_x, uint32_t src_y,
2056 uint32_t src_w, uint32_t src_h)
2057{
2058 struct drm_atomic_state *state;
2059 struct drm_plane_state *plane_state;
2060 int ret = 0;
2061
2062 state = drm_atomic_state_alloc(plane->dev);
2063 if (!state)
2064 return -ENOMEM;
2065
2066 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2067retry:
2068 plane_state = drm_atomic_get_plane_state(state, plane);
2069 if (IS_ERR(plane_state)) {
2070 ret = PTR_ERR(plane_state);
2071 goto fail;
2072 }
2073
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002074 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002075 if (ret != 0)
2076 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002077 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002078 plane_state->crtc_x = crtc_x;
2079 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002080 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002081 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002082 plane_state->src_x = src_x;
2083 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002084 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002085 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002086
Daniel Vetter3671c582015-05-04 15:40:52 +02002087 if (plane == crtc->cursor)
2088 state->legacy_cursor_update = true;
2089
Daniel Vetter042652e2014-07-27 13:46:52 +02002090 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002091fail:
2092 if (ret == -EDEADLK)
2093 goto backoff;
2094
Chris Wilson08536952016-10-14 13:18:18 +01002095 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002096 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002097
Daniel Vetter042652e2014-07-27 13:46:52 +02002098backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002099 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002100 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002101
2102 /*
2103 * Someone might have exchanged the framebuffer while we dropped locks
2104 * in the backoff code. We need to fix up the fb refcount tracking the
2105 * core does for us.
2106 */
2107 plane->old_fb = plane->fb;
2108
2109 goto retry;
2110}
2111EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2112
2113/**
2114 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2115 * @plane: plane to disable
2116 *
2117 * Provides a default plane disable handler using the atomic driver interface.
2118 *
2119 * RETURNS:
2120 * Zero on success, error code on failure
2121 */
2122int drm_atomic_helper_disable_plane(struct drm_plane *plane)
2123{
2124 struct drm_atomic_state *state;
2125 struct drm_plane_state *plane_state;
2126 int ret = 0;
2127
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08002128 /*
2129 * FIXME: Without plane->crtc set we can't get at the implicit legacy
2130 * acquire context. The real fix will be to wire the acquire ctx through
2131 * everywhere we need it, but meanwhile prevent chaos by just skipping
2132 * this noop. The critical case is the cursor ioctls which a) only grab
2133 * crtc/cursor-plane locks (so we need the crtc to get at the right
2134 * acquire context) and b) can try to disable the plane multiple times.
2135 */
2136 if (!plane->crtc)
2137 return 0;
2138
Daniel Vetter042652e2014-07-27 13:46:52 +02002139 state = drm_atomic_state_alloc(plane->dev);
2140 if (!state)
2141 return -ENOMEM;
2142
2143 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
2144retry:
2145 plane_state = drm_atomic_get_plane_state(state, plane);
2146 if (IS_ERR(plane_state)) {
2147 ret = PTR_ERR(plane_state);
2148 goto fail;
2149 }
2150
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002151 if (plane_state->crtc && (plane == plane->crtc->cursor))
2152 plane_state->state->legacy_cursor_update = true;
2153
Rob Clarkbbb1e522015-08-25 15:35:58 -04002154 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002155 if (ret != 0)
2156 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002157
Daniel Vetter042652e2014-07-27 13:46:52 +02002158 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002159fail:
2160 if (ret == -EDEADLK)
2161 goto backoff;
2162
Chris Wilson08536952016-10-14 13:18:18 +01002163 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002164 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002165
Daniel Vetter042652e2014-07-27 13:46:52 +02002166backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002167 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002168 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002169
2170 /*
2171 * Someone might have exchanged the framebuffer while we dropped locks
2172 * in the backoff code. We need to fix up the fb refcount tracking the
2173 * core does for us.
2174 */
2175 plane->old_fb = plane->fb;
2176
2177 goto retry;
2178}
2179EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2180
Rob Clarkbbb1e522015-08-25 15:35:58 -04002181/* just used from fb-helper and atomic-helper: */
2182int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2183 struct drm_plane_state *plane_state)
2184{
2185 int ret;
2186
2187 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2188 if (ret != 0)
2189 return ret;
2190
2191 drm_atomic_set_fb_for_plane(plane_state, NULL);
2192 plane_state->crtc_x = 0;
2193 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002194 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002195 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002196 plane_state->src_x = 0;
2197 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002198 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002199 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002200
Rob Clarkbbb1e522015-08-25 15:35:58 -04002201 return 0;
2202}
2203
Daniel Vetter042652e2014-07-27 13:46:52 +02002204static int update_output_state(struct drm_atomic_state *state,
2205 struct drm_mode_set *set)
2206{
2207 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002208 struct drm_crtc *crtc;
2209 struct drm_crtc_state *crtc_state;
2210 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02002211 struct drm_connector_state *conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002212 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002213
2214 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2215 state->acquire_ctx);
2216 if (ret)
2217 return ret;
2218
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002219 /* First disable all connectors on the target crtc. */
2220 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2221 if (ret)
2222 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002223
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002224 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002225 if (conn_state->crtc == set->crtc) {
2226 ret = drm_atomic_set_crtc_for_connector(conn_state,
2227 NULL);
2228 if (ret)
2229 return ret;
2230 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002231 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002232
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002233 /* Then set all connectors from set->connectors on the target crtc */
2234 for (i = 0; i < set->num_connectors; i++) {
2235 conn_state = drm_atomic_get_connector_state(state,
2236 set->connectors[i]);
2237 if (IS_ERR(conn_state))
2238 return PTR_ERR(conn_state);
2239
2240 ret = drm_atomic_set_crtc_for_connector(conn_state,
2241 set->crtc);
2242 if (ret)
2243 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002244 }
2245
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002246 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002247 /* Don't update ->enable for the CRTC in the set_config request,
2248 * since a mismatch would indicate a bug in the upper layers.
2249 * The actual modeset code later on will catch any
2250 * inconsistencies here. */
2251 if (crtc == set->crtc)
2252 continue;
2253
Maarten Lankhorst14de6c42016-01-04 12:53:20 +01002254 if (!crtc_state->connector_mask) {
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002255 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
2256 NULL);
2257 if (ret < 0)
2258 return ret;
2259
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02002260 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002261 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002262 }
2263
2264 return 0;
2265}
2266
2267/**
2268 * drm_atomic_helper_set_config - set a new config from userspace
2269 * @set: mode set configuration
2270 *
2271 * Provides a default crtc set_config handler using the atomic driver interface.
2272 *
2273 * Returns:
2274 * Returns 0 on success, negative errno numbers on failure.
2275 */
2276int drm_atomic_helper_set_config(struct drm_mode_set *set)
2277{
2278 struct drm_atomic_state *state;
2279 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002280 int ret = 0;
2281
2282 state = drm_atomic_state_alloc(crtc->dev);
2283 if (!state)
2284 return -ENOMEM;
2285
Maarten Lankhorst40616a22016-03-03 10:17:39 +01002286 state->legacy_set_config = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02002287 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2288retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04002289 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002290 if (ret != 0)
2291 goto fail;
2292
Daniel Vetter042652e2014-07-27 13:46:52 +02002293 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002294fail:
2295 if (ret == -EDEADLK)
2296 goto backoff;
2297
Chris Wilson08536952016-10-14 13:18:18 +01002298 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002299 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002300
Daniel Vetter042652e2014-07-27 13:46:52 +02002301backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002302 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002303 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002304
2305 /*
2306 * Someone might have exchanged the framebuffer while we dropped locks
2307 * in the backoff code. We need to fix up the fb refcount tracking the
2308 * core does for us.
2309 */
2310 crtc->primary->old_fb = crtc->primary->fb;
2311
2312 goto retry;
2313}
2314EXPORT_SYMBOL(drm_atomic_helper_set_config);
2315
Rob Clarkbbb1e522015-08-25 15:35:58 -04002316/* just used from fb-helper and atomic-helper: */
2317int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2318 struct drm_atomic_state *state)
2319{
2320 struct drm_crtc_state *crtc_state;
2321 struct drm_plane_state *primary_state;
2322 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002323 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002324 int ret;
2325
2326 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2327 if (IS_ERR(crtc_state))
2328 return PTR_ERR(crtc_state);
2329
2330 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2331 if (IS_ERR(primary_state))
2332 return PTR_ERR(primary_state);
2333
2334 if (!set->mode) {
2335 WARN_ON(set->fb);
2336 WARN_ON(set->num_connectors);
2337
2338 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2339 if (ret != 0)
2340 return ret;
2341
2342 crtc_state->active = false;
2343
2344 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2345 if (ret != 0)
2346 return ret;
2347
2348 drm_atomic_set_fb_for_plane(primary_state, NULL);
2349
2350 goto commit;
2351 }
2352
2353 WARN_ON(!set->fb);
2354 WARN_ON(!set->num_connectors);
2355
2356 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2357 if (ret != 0)
2358 return ret;
2359
2360 crtc_state->active = true;
2361
2362 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2363 if (ret != 0)
2364 return ret;
2365
Ville Syrjälä83926112015-11-16 17:02:34 +02002366 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2367
Rob Clarkbbb1e522015-08-25 15:35:58 -04002368 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2369 primary_state->crtc_x = 0;
2370 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02002371 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002372 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002373 primary_state->src_x = set->x << 16;
2374 primary_state->src_y = set->y << 16;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03002375 if (drm_rotation_90_or_270(primary_state->rotation)) {
Ville Syrjälä83926112015-11-16 17:02:34 +02002376 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002377 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002378 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02002379 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002380 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002381 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04002382
2383commit:
2384 ret = update_output_state(state, set);
2385 if (ret)
2386 return ret;
2387
2388 return 0;
2389}
2390
Daniel Vetter042652e2014-07-27 13:46:52 +02002391/**
Thierry Reding14942762015-12-02 17:50:04 +01002392 * drm_atomic_helper_disable_all - disable all currently active outputs
2393 * @dev: DRM device
2394 * @ctx: lock acquisition context
2395 *
2396 * Loops through all connectors, finding those that aren't turned off and then
2397 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2398 * that they are connected to.
2399 *
2400 * This is used for example in suspend/resume to disable all currently active
2401 * functions when suspending.
2402 *
2403 * Note that if callers haven't already acquired all modeset locks this might
2404 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2405 *
2406 * Returns:
2407 * 0 on success or a negative error code on failure.
2408 *
2409 * See also:
2410 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2411 */
2412int drm_atomic_helper_disable_all(struct drm_device *dev,
2413 struct drm_modeset_acquire_ctx *ctx)
2414{
2415 struct drm_atomic_state *state;
2416 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01002417 struct drm_connector_list_iter conn_iter;
Thierry Reding14942762015-12-02 17:50:04 +01002418 int err;
2419
2420 state = drm_atomic_state_alloc(dev);
2421 if (!state)
2422 return -ENOMEM;
2423
2424 state->acquire_ctx = ctx;
2425
Daniel Vetterc36a3252016-12-15 16:58:43 +01002426 drm_connector_list_iter_get(dev, &conn_iter);
2427 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding14942762015-12-02 17:50:04 +01002428 struct drm_crtc *crtc = conn->state->crtc;
2429 struct drm_crtc_state *crtc_state;
2430
2431 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
2432 continue;
2433
2434 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2435 if (IS_ERR(crtc_state)) {
2436 err = PTR_ERR(crtc_state);
2437 goto free;
2438 }
2439
2440 crtc_state->active = false;
2441 }
2442
2443 err = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01002444free:
Daniel Vetterc36a3252016-12-15 16:58:43 +01002445 drm_connector_list_iter_put(&conn_iter);
Chris Wilson08536952016-10-14 13:18:18 +01002446 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01002447 return err;
2448}
2449EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2450
2451/**
2452 * drm_atomic_helper_suspend - subsystem-level suspend helper
2453 * @dev: DRM device
2454 *
2455 * Duplicates the current atomic state, disables all active outputs and then
2456 * returns a pointer to the original atomic state to the caller. Drivers can
2457 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2458 * restore the output configuration that was active at the time the system
2459 * entered suspend.
2460 *
2461 * Note that it is potentially unsafe to use this. The atomic state object
2462 * returned by this function is assumed to be persistent. Drivers must ensure
2463 * that this holds true. Before calling this function, drivers must make sure
2464 * to suspend fbdev emulation so that nothing can be using the device.
2465 *
2466 * Returns:
2467 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2468 * encoded error code on failure. Drivers should store the returned atomic
2469 * state object and pass it to the drm_atomic_helper_resume() helper upon
2470 * resume.
2471 *
2472 * See also:
2473 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2474 * drm_atomic_helper_resume()
2475 */
2476struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2477{
2478 struct drm_modeset_acquire_ctx ctx;
2479 struct drm_atomic_state *state;
2480 int err;
2481
2482 drm_modeset_acquire_init(&ctx, 0);
2483
2484retry:
2485 err = drm_modeset_lock_all_ctx(dev, &ctx);
2486 if (err < 0) {
2487 state = ERR_PTR(err);
2488 goto unlock;
2489 }
2490
2491 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2492 if (IS_ERR(state))
2493 goto unlock;
2494
2495 err = drm_atomic_helper_disable_all(dev, &ctx);
2496 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01002497 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01002498 state = ERR_PTR(err);
2499 goto unlock;
2500 }
2501
2502unlock:
2503 if (PTR_ERR(state) == -EDEADLK) {
2504 drm_modeset_backoff(&ctx);
2505 goto retry;
2506 }
2507
2508 drm_modeset_drop_locks(&ctx);
2509 drm_modeset_acquire_fini(&ctx);
2510 return state;
2511}
2512EXPORT_SYMBOL(drm_atomic_helper_suspend);
2513
2514/**
2515 * drm_atomic_helper_resume - subsystem-level resume helper
2516 * @dev: DRM device
2517 * @state: atomic state to resume to
2518 *
2519 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2520 * grabs all modeset locks and commits the atomic state object. This can be
2521 * used in conjunction with the drm_atomic_helper_suspend() helper to
2522 * implement suspend/resume for drivers that support atomic mode-setting.
2523 *
2524 * Returns:
2525 * 0 on success or a negative error code on failure.
2526 *
2527 * See also:
2528 * drm_atomic_helper_suspend()
2529 */
2530int drm_atomic_helper_resume(struct drm_device *dev,
2531 struct drm_atomic_state *state)
2532{
2533 struct drm_mode_config *config = &dev->mode_config;
2534 int err;
2535
2536 drm_mode_config_reset(dev);
2537 drm_modeset_lock_all(dev);
2538 state->acquire_ctx = config->acquire_ctx;
2539 err = drm_atomic_commit(state);
2540 drm_modeset_unlock_all(dev);
2541
2542 return err;
2543}
2544EXPORT_SYMBOL(drm_atomic_helper_resume);
2545
2546/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002547 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002548 * @crtc: DRM crtc
2549 * @property: DRM property
2550 * @val: value of property
2551 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002552 * Provides a default crtc set_property handler using the atomic driver
2553 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002554 *
2555 * RETURNS:
2556 * Zero on success, error code on failure
2557 */
2558int
2559drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2560 struct drm_property *property,
2561 uint64_t val)
2562{
2563 struct drm_atomic_state *state;
2564 struct drm_crtc_state *crtc_state;
2565 int ret = 0;
2566
2567 state = drm_atomic_state_alloc(crtc->dev);
2568 if (!state)
2569 return -ENOMEM;
2570
2571 /* ->set_property is always called with all locks held. */
2572 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2573retry:
2574 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2575 if (IS_ERR(crtc_state)) {
2576 ret = PTR_ERR(crtc_state);
2577 goto fail;
2578 }
2579
Rob Clark40ecc692014-12-18 16:01:46 -05002580 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2581 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002582 if (ret)
2583 goto fail;
2584
2585 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002586fail:
2587 if (ret == -EDEADLK)
2588 goto backoff;
2589
Chris Wilson08536952016-10-14 13:18:18 +01002590 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002591 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002592
Daniel Vetter042652e2014-07-27 13:46:52 +02002593backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002594 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002595 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002596
2597 goto retry;
2598}
2599EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2600
2601/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002602 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002603 * @plane: DRM plane
2604 * @property: DRM property
2605 * @val: value of property
2606 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002607 * Provides a default plane set_property handler using the atomic driver
2608 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002609 *
2610 * RETURNS:
2611 * Zero on success, error code on failure
2612 */
2613int
2614drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2615 struct drm_property *property,
2616 uint64_t val)
2617{
2618 struct drm_atomic_state *state;
2619 struct drm_plane_state *plane_state;
2620 int ret = 0;
2621
2622 state = drm_atomic_state_alloc(plane->dev);
2623 if (!state)
2624 return -ENOMEM;
2625
2626 /* ->set_property is always called with all locks held. */
2627 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2628retry:
2629 plane_state = drm_atomic_get_plane_state(state, plane);
2630 if (IS_ERR(plane_state)) {
2631 ret = PTR_ERR(plane_state);
2632 goto fail;
2633 }
2634
Rob Clark40ecc692014-12-18 16:01:46 -05002635 ret = drm_atomic_plane_set_property(plane, plane_state,
2636 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002637 if (ret)
2638 goto fail;
2639
2640 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002641fail:
2642 if (ret == -EDEADLK)
2643 goto backoff;
2644
Chris Wilson08536952016-10-14 13:18:18 +01002645 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002646 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002647
Daniel Vetter042652e2014-07-27 13:46:52 +02002648backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002649 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002650 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002651
2652 goto retry;
2653}
2654EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2655
2656/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002657 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002658 * @connector: DRM connector
2659 * @property: DRM property
2660 * @val: value of property
2661 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002662 * Provides a default connector set_property handler using the atomic driver
2663 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002664 *
2665 * RETURNS:
2666 * Zero on success, error code on failure
2667 */
2668int
2669drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2670 struct drm_property *property,
2671 uint64_t val)
2672{
2673 struct drm_atomic_state *state;
2674 struct drm_connector_state *connector_state;
2675 int ret = 0;
2676
2677 state = drm_atomic_state_alloc(connector->dev);
2678 if (!state)
2679 return -ENOMEM;
2680
2681 /* ->set_property is always called with all locks held. */
2682 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2683retry:
2684 connector_state = drm_atomic_get_connector_state(state, connector);
2685 if (IS_ERR(connector_state)) {
2686 ret = PTR_ERR(connector_state);
2687 goto fail;
2688 }
2689
Rob Clark40ecc692014-12-18 16:01:46 -05002690 ret = drm_atomic_connector_set_property(connector, connector_state,
2691 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002692 if (ret)
2693 goto fail;
2694
2695 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002696fail:
2697 if (ret == -EDEADLK)
2698 goto backoff;
2699
Chris Wilson08536952016-10-14 13:18:18 +01002700 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002701 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002702
Daniel Vetter042652e2014-07-27 13:46:52 +02002703backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002704 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002705 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002706
2707 goto retry;
2708}
2709EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002710
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002711static int page_flip_common(
2712 struct drm_atomic_state *state,
2713 struct drm_crtc *crtc,
2714 struct drm_framebuffer *fb,
2715 struct drm_pending_vblank_event *event)
2716{
2717 struct drm_plane *plane = crtc->primary;
2718 struct drm_plane_state *plane_state;
2719 struct drm_crtc_state *crtc_state;
2720 int ret = 0;
2721
2722 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2723 if (IS_ERR(crtc_state))
2724 return PTR_ERR(crtc_state);
2725
2726 crtc_state->event = event;
2727
2728 plane_state = drm_atomic_get_plane_state(state, plane);
2729 if (IS_ERR(plane_state))
2730 return PTR_ERR(plane_state);
2731
2732
2733 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2734 if (ret != 0)
2735 return ret;
2736 drm_atomic_set_fb_for_plane(plane_state, fb);
2737
2738 /* Make sure we don't accidentally do a full modeset. */
2739 state->allow_modeset = false;
2740 if (!crtc_state->active) {
2741 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2742 crtc->base.id);
2743 return -EINVAL;
2744 }
2745
2746 return ret;
2747}
2748
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002749/**
2750 * drm_atomic_helper_page_flip - execute a legacy page flip
2751 * @crtc: DRM crtc
2752 * @fb: DRM framebuffer
2753 * @event: optional DRM event to signal upon completion
2754 * @flags: flip flags for non-vblank sync'ed updates
2755 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002756 * Provides a default &drm_crtc_funcs.page_flip implementation
2757 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002758 *
2759 * Note that for now so called async page flips (i.e. updates which are not
2760 * synchronized to vblank) are not supported, since the atomic interfaces have
2761 * no provisions for this yet.
2762 *
2763 * Returns:
2764 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002765 *
2766 * See also:
2767 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002768 */
2769int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2770 struct drm_framebuffer *fb,
2771 struct drm_pending_vblank_event *event,
2772 uint32_t flags)
2773{
2774 struct drm_plane *plane = crtc->primary;
2775 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002776 int ret = 0;
2777
2778 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2779 return -EINVAL;
2780
2781 state = drm_atomic_state_alloc(plane->dev);
2782 if (!state)
2783 return -ENOMEM;
2784
2785 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002786
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002787retry:
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002788 ret = page_flip_common(state, crtc, fb, event);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002789 if (ret != 0)
2790 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01002791
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002792 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002793
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002794fail:
2795 if (ret == -EDEADLK)
2796 goto backoff;
2797
Chris Wilson08536952016-10-14 13:18:18 +01002798 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002799 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002800
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002801backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002802 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002803 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002804
2805 /*
2806 * Someone might have exchanged the framebuffer while we dropped locks
2807 * in the backoff code. We need to fix up the fb refcount tracking the
2808 * core does for us.
2809 */
2810 plane->old_fb = plane->fb;
2811
2812 goto retry;
2813}
2814EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002815
2816/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05002817 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2818 * @crtc: DRM crtc
2819 * @fb: DRM framebuffer
2820 * @event: optional DRM event to signal upon completion
2821 * @flags: flip flags for non-vblank sync'ed updates
2822 * @target: specifying the target vblank period when the flip to take effect
2823 *
2824 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2825 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2826 * target vblank period to flip.
2827 *
2828 * Returns:
2829 * Returns 0 on success, negative errno numbers on failure.
2830 */
2831int drm_atomic_helper_page_flip_target(
2832 struct drm_crtc *crtc,
2833 struct drm_framebuffer *fb,
2834 struct drm_pending_vblank_event *event,
2835 uint32_t flags,
2836 uint32_t target)
2837{
2838 struct drm_plane *plane = crtc->primary;
2839 struct drm_atomic_state *state;
2840 struct drm_crtc_state *crtc_state;
2841 int ret = 0;
2842
2843 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2844 return -EINVAL;
2845
2846 state = drm_atomic_state_alloc(plane->dev);
2847 if (!state)
2848 return -ENOMEM;
2849
2850 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2851
2852retry:
2853 ret = page_flip_common(state, crtc, fb, event);
2854 if (ret != 0)
2855 goto fail;
2856
2857 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
2858 if (WARN_ON(!crtc_state)) {
2859 ret = -EINVAL;
2860 goto fail;
2861 }
2862 crtc_state->target_vblank = target;
2863
2864 ret = drm_atomic_nonblocking_commit(state);
2865
2866fail:
2867 if (ret == -EDEADLK)
2868 goto backoff;
2869
2870 drm_atomic_state_put(state);
2871 return ret;
2872
2873backoff:
2874 drm_atomic_state_clear(state);
2875 drm_atomic_legacy_backoff(state);
2876
2877 /*
2878 * Someone might have exchanged the framebuffer while we dropped locks
2879 * in the backoff code. We need to fix up the fb refcount tracking the
2880 * core does for us.
2881 */
2882 plane->old_fb = plane->fb;
2883
2884 goto retry;
2885}
2886EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2887
2888/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002889 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2890 * @connector: affected connector
2891 * @mode: DPMS mode
2892 *
2893 * This is the main helper function provided by the atomic helper framework for
2894 * implementing the legacy DPMS connector interface. It computes the new desired
2895 * ->active state for the corresponding CRTC (if the connector is enabled) and
Daniel Vetter2e7a5702016-06-01 23:40:36 +02002896 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002897 *
2898 * Returns:
2899 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002900 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002901int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2902 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002903{
2904 struct drm_mode_config *config = &connector->dev->mode_config;
2905 struct drm_atomic_state *state;
2906 struct drm_crtc_state *crtc_state;
2907 struct drm_crtc *crtc;
2908 struct drm_connector *tmp_connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +01002909 struct drm_connector_list_iter conn_iter;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002910 int ret;
2911 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002912 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002913
2914 if (mode != DRM_MODE_DPMS_ON)
2915 mode = DRM_MODE_DPMS_OFF;
2916
2917 connector->dpms = mode;
2918 crtc = connector->state->crtc;
2919
2920 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002921 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002922
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002923 state = drm_atomic_state_alloc(connector->dev);
2924 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002925 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002926
2927 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2928retry:
2929 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002930 if (IS_ERR(crtc_state)) {
2931 ret = PTR_ERR(crtc_state);
2932 goto fail;
2933 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002934
2935 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2936
Daniel Vetterc36a3252016-12-15 16:58:43 +01002937 drm_connector_list_iter_get(connector->dev, &conn_iter);
2938 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
John Hunter0388df02015-03-17 15:30:28 +08002939 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002940 continue;
2941
John Hunter0388df02015-03-17 15:30:28 +08002942 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002943 active = true;
2944 break;
2945 }
2946 }
Daniel Vetterc36a3252016-12-15 16:58:43 +01002947 drm_connector_list_iter_put(&conn_iter);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002948 crtc_state->active = active;
2949
2950 ret = drm_atomic_commit(state);
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002951fail:
2952 if (ret == -EDEADLK)
2953 goto backoff;
Marta Lofstedt8f5040e2016-12-05 14:04:08 +02002954 if (ret != 0)
2955 connector->dpms = old_mode;
Chris Wilson08536952016-10-14 13:18:18 +01002956 drm_atomic_state_put(state);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002957 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01002958
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002959backoff:
2960 drm_atomic_state_clear(state);
2961 drm_atomic_legacy_backoff(state);
2962
2963 goto retry;
2964}
2965EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2966
2967/**
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02002968 * drm_atomic_helper_best_encoder - Helper for &drm_connector_helper_funcs
2969 * ->best_encoder callback
2970 * @connector: Connector control structure
2971 *
2972 * This is a &drm_connector_helper_funcs ->best_encoder callback helper for
2973 * connectors that support exactly 1 encoder, statically determined at driver
2974 * init time.
2975 */
2976struct drm_encoder *
2977drm_atomic_helper_best_encoder(struct drm_connector *connector)
2978{
2979 WARN_ON(connector->encoder_ids[1]);
2980 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
2981}
2982EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
2983
2984/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002985 * DOC: atomic state reset and initialization
2986 *
2987 * Both the drm core and the atomic helpers assume that there is always the full
2988 * and correct atomic software state for all connectors, CRTCs and planes
2989 * available. Which is a bit a problem on driver load and also after system
2990 * suspend. One way to solve this is to have a hardware state read-out
2991 * infrastructure which reconstructs the full software state (e.g. the i915
2992 * driver).
2993 *
2994 * The simpler solution is to just reset the software state to everything off,
2995 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2996 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01002997 *
2998 * On the upside the precise state tracking of atomic simplifies system suspend
2999 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3000 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3001 * For other drivers the building blocks are split out, see the documentation
3002 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003003 */
3004
3005/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003006 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
3007 * @crtc: drm CRTC
3008 *
3009 * Resets the atomic state for @crtc by freeing the state pointer (which might
3010 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3011 */
3012void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3013{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003014 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003015 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003016
Daniel Vetterd4617012014-11-03 15:56:43 +01003017 kfree(crtc->state);
3018 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003019
3020 if (crtc->state)
3021 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01003022}
3023EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3024
3025/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003026 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3027 * @crtc: CRTC object
3028 * @state: atomic CRTC state
3029 *
3030 * Copies atomic state from a CRTC's current state and resets inferred values.
3031 * This is useful for drivers that subclass the CRTC state.
3032 */
3033void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3034 struct drm_crtc_state *state)
3035{
3036 memcpy(state, crtc->state, sizeof(*state));
3037
Daniel Stone99cf4a22015-05-25 19:11:51 +01003038 if (state->mode_blob)
3039 drm_property_reference_blob(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003040 if (state->degamma_lut)
3041 drm_property_reference_blob(state->degamma_lut);
3042 if (state->ctm)
3043 drm_property_reference_blob(state->ctm);
3044 if (state->gamma_lut)
3045 drm_property_reference_blob(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003046 state->mode_changed = false;
3047 state->active_changed = false;
3048 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02003049 state->connectors_changed = false;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003050 state->color_mgmt_changed = false;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02003051 state->zpos_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01003052 state->event = NULL;
3053}
3054EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3055
3056/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003057 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3058 * @crtc: drm CRTC
3059 *
3060 * Default CRTC state duplicate hook for drivers which don't have their own
3061 * subclassed CRTC state structure.
3062 */
3063struct drm_crtc_state *
3064drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3065{
3066 struct drm_crtc_state *state;
3067
3068 if (WARN_ON(!crtc->state))
3069 return NULL;
3070
Thierry Redingf5e78402015-01-28 14:54:32 +01003071 state = kmalloc(sizeof(*state), GFP_KERNEL);
3072 if (state)
3073 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003074
3075 return state;
3076}
3077EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3078
3079/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003080 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
Thierry Redingf5e78402015-01-28 14:54:32 +01003081 * @state: CRTC state object to release
3082 *
3083 * Releases all resources stored in the CRTC state without actually freeing
3084 * the memory of the CRTC state. This is useful for drivers that subclass the
3085 * CRTC state.
3086 */
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003087void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003088{
Markus Elfring5f911902015-11-06 12:03:46 +01003089 drm_property_unreference_blob(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003090 drm_property_unreference_blob(state->degamma_lut);
3091 drm_property_unreference_blob(state->ctm);
3092 drm_property_unreference_blob(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003093}
3094EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3095
3096/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003097 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3098 * @crtc: drm CRTC
3099 * @state: CRTC state object to release
3100 *
3101 * Default CRTC state destroy hook for drivers which don't have their own
3102 * subclassed CRTC state structure.
3103 */
3104void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3105 struct drm_crtc_state *state)
3106{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003107 __drm_atomic_helper_crtc_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003108 kfree(state);
3109}
3110EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3111
3112/**
3113 * drm_atomic_helper_plane_reset - default ->reset hook for planes
3114 * @plane: drm plane
3115 *
3116 * Resets the atomic state for @plane by freeing the state pointer (which might
3117 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3118 */
3119void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3120{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003121 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +02003122 __drm_atomic_helper_plane_destroy_state(plane->state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003123
Daniel Vetterd4617012014-11-03 15:56:43 +01003124 kfree(plane->state);
3125 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003126
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003127 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003128 plane->state->plane = plane;
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03003129 plane->state->rotation = DRM_ROTATE_0;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003130 }
Daniel Vetterd4617012014-11-03 15:56:43 +01003131}
3132EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3133
3134/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003135 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3136 * @plane: plane object
3137 * @state: atomic plane state
3138 *
3139 * Copies atomic state from a plane's current state. This is useful for
3140 * drivers that subclass the plane state.
3141 */
3142void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3143 struct drm_plane_state *state)
3144{
3145 memcpy(state, plane->state, sizeof(*state));
3146
3147 if (state->fb)
3148 drm_framebuffer_reference(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003149
3150 state->fence = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003151}
3152EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3153
3154/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003155 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3156 * @plane: drm plane
3157 *
3158 * Default plane state duplicate hook for drivers which don't have their own
3159 * subclassed plane state structure.
3160 */
3161struct drm_plane_state *
3162drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3163{
Daniel Vetter321ebf02014-11-04 22:57:27 +01003164 struct drm_plane_state *state;
3165
Daniel Vetterd4617012014-11-03 15:56:43 +01003166 if (WARN_ON(!plane->state))
3167 return NULL;
3168
Thierry Redingf5e78402015-01-28 14:54:32 +01003169 state = kmalloc(sizeof(*state), GFP_KERNEL);
3170 if (state)
3171 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003172
3173 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003174}
3175EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3176
3177/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003178 * __drm_atomic_helper_plane_destroy_state - release plane state
Thierry Redingf5e78402015-01-28 14:54:32 +01003179 * @state: plane state object to release
3180 *
3181 * Releases all resources stored in the plane state without actually freeing
3182 * the memory of the plane state. This is useful for drivers that subclass the
3183 * plane state.
3184 */
Daniel Vetter2f701692016-05-09 16:34:10 +02003185void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003186{
3187 if (state->fb)
3188 drm_framebuffer_unreference(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003189
3190 if (state->fence)
3191 dma_fence_put(state->fence);
Thierry Redingf5e78402015-01-28 14:54:32 +01003192}
3193EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3194
3195/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003196 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3197 * @plane: drm plane
3198 * @state: plane state object to release
3199 *
3200 * Default plane state destroy hook for drivers which don't have their own
3201 * subclassed plane state structure.
3202 */
3203void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01003204 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01003205{
Daniel Vetter2f701692016-05-09 16:34:10 +02003206 __drm_atomic_helper_plane_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003207 kfree(state);
3208}
3209EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3210
3211/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003212 * __drm_atomic_helper_connector_reset - reset state on connector
3213 * @connector: drm connector
3214 * @conn_state: connector state to assign
3215 *
3216 * Initializes the newly allocated @conn_state and assigns it to
3217 * #connector ->state, usually required when initializing the drivers
3218 * or when called from the ->reset hook.
3219 *
3220 * This is useful for drivers that subclass the connector state.
3221 */
3222void
3223__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3224 struct drm_connector_state *conn_state)
3225{
3226 if (conn_state)
3227 conn_state->connector = connector;
3228
3229 connector->state = conn_state;
3230}
3231EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3232
3233/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003234 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
3235 * @connector: drm connector
3236 *
3237 * Resets the atomic state for @connector by freeing the state pointer (which
3238 * might be NULL, e.g. at driver load time) and allocating a new empty state
3239 * object.
3240 */
3241void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3242{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003243 struct drm_connector_state *conn_state =
3244 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003245
Daniel Vetterb0b55112016-04-22 22:10:29 +02003246 if (connector->state)
Daniel Vetterfabd9102016-05-09 16:34:11 +02003247 __drm_atomic_helper_connector_destroy_state(connector->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003248
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003249 kfree(connector->state);
3250 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003251}
3252EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3253
3254/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003255 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3256 * @connector: connector object
3257 * @state: atomic connector state
3258 *
3259 * Copies atomic state from a connector's current state. This is useful for
3260 * drivers that subclass the connector state.
3261 */
3262void
3263__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3264 struct drm_connector_state *state)
3265{
3266 memcpy(state, connector->state, sizeof(*state));
Dave Airlied2307de2016-04-27 11:27:39 +10003267 if (state->crtc)
3268 drm_connector_reference(connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003269}
3270EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3271
3272/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003273 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3274 * @connector: drm connector
3275 *
3276 * Default connector state duplicate hook for drivers which don't have their own
3277 * subclassed connector state structure.
3278 */
3279struct drm_connector_state *
3280drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3281{
Thierry Redingf5e78402015-01-28 14:54:32 +01003282 struct drm_connector_state *state;
3283
Daniel Vetterd4617012014-11-03 15:56:43 +01003284 if (WARN_ON(!connector->state))
3285 return NULL;
3286
Thierry Redingf5e78402015-01-28 14:54:32 +01003287 state = kmalloc(sizeof(*state), GFP_KERNEL);
3288 if (state)
3289 __drm_atomic_helper_connector_duplicate_state(connector, state);
3290
3291 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003292}
3293EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3294
3295/**
Thierry Reding397fd772015-09-08 15:00:45 +02003296 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3297 * @dev: DRM device
3298 * @ctx: lock acquisition context
3299 *
3300 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01003301 * duplicating their respective states. This is used for example by suspend/
3302 * resume support code to save the state prior to suspend such that it can
3303 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02003304 *
3305 * Note that this treats atomic state as persistent between save and restore.
3306 * Drivers must make sure that this is possible and won't result in confusion
3307 * or erroneous behaviour.
3308 *
3309 * Note that if callers haven't already acquired all modeset locks this might
3310 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3311 *
3312 * Returns:
3313 * A pointer to the copy of the atomic state object on success or an
3314 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01003315 *
3316 * See also:
3317 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02003318 */
3319struct drm_atomic_state *
3320drm_atomic_helper_duplicate_state(struct drm_device *dev,
3321 struct drm_modeset_acquire_ctx *ctx)
3322{
3323 struct drm_atomic_state *state;
3324 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003325 struct drm_connector_list_iter conn_iter;
Thierry Reding397fd772015-09-08 15:00:45 +02003326 struct drm_plane *plane;
3327 struct drm_crtc *crtc;
3328 int err = 0;
3329
3330 state = drm_atomic_state_alloc(dev);
3331 if (!state)
3332 return ERR_PTR(-ENOMEM);
3333
3334 state->acquire_ctx = ctx;
3335
3336 drm_for_each_crtc(crtc, dev) {
3337 struct drm_crtc_state *crtc_state;
3338
3339 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3340 if (IS_ERR(crtc_state)) {
3341 err = PTR_ERR(crtc_state);
3342 goto free;
3343 }
3344 }
3345
3346 drm_for_each_plane(plane, dev) {
3347 struct drm_plane_state *plane_state;
3348
3349 plane_state = drm_atomic_get_plane_state(state, plane);
3350 if (IS_ERR(plane_state)) {
3351 err = PTR_ERR(plane_state);
3352 goto free;
3353 }
3354 }
3355
Daniel Vetterc36a3252016-12-15 16:58:43 +01003356 drm_connector_list_iter_get(dev, &conn_iter);
3357 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding397fd772015-09-08 15:00:45 +02003358 struct drm_connector_state *conn_state;
3359
3360 conn_state = drm_atomic_get_connector_state(state, conn);
3361 if (IS_ERR(conn_state)) {
3362 err = PTR_ERR(conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003363 drm_connector_list_iter_put(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003364 goto free;
3365 }
3366 }
Daniel Vetterc36a3252016-12-15 16:58:43 +01003367 drm_connector_list_iter_put(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003368
3369 /* clear the acquire context so that it isn't accidentally reused */
3370 state->acquire_ctx = NULL;
3371
3372free:
3373 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003374 drm_atomic_state_put(state);
Thierry Reding397fd772015-09-08 15:00:45 +02003375 state = ERR_PTR(err);
3376 }
3377
3378 return state;
3379}
3380EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3381
3382/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003383 * __drm_atomic_helper_connector_destroy_state - release connector state
Thierry Redingf5e78402015-01-28 14:54:32 +01003384 * @state: connector state object to release
3385 *
3386 * Releases all resources stored in the connector state without actually
3387 * freeing the memory of the connector state. This is useful for drivers that
3388 * subclass the connector state.
3389 */
3390void
Daniel Vetterfabd9102016-05-09 16:34:11 +02003391__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003392{
Dave Airlied2307de2016-04-27 11:27:39 +10003393 if (state->crtc)
3394 drm_connector_unreference(state->connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003395}
3396EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3397
3398/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003399 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3400 * @connector: drm connector
3401 * @state: connector state object to release
3402 *
3403 * Default connector state destroy hook for drivers which don't have their own
3404 * subclassed connector state structure.
3405 */
3406void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3407 struct drm_connector_state *state)
3408{
Daniel Vetterfabd9102016-05-09 16:34:11 +02003409 __drm_atomic_helper_connector_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003410 kfree(state);
3411}
3412EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003413
3414/**
3415 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3416 * @crtc: CRTC object
3417 * @red: red correction table
3418 * @green: green correction table
3419 * @blue: green correction table
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003420 * @size: size of the tables
3421 *
3422 * Implements support for legacy gamma correction table for drivers
3423 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3424 * properties.
3425 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003426int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3427 u16 *red, u16 *green, u16 *blue,
3428 uint32_t size)
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003429{
3430 struct drm_device *dev = crtc->dev;
3431 struct drm_mode_config *config = &dev->mode_config;
3432 struct drm_atomic_state *state;
3433 struct drm_crtc_state *crtc_state;
3434 struct drm_property_blob *blob = NULL;
3435 struct drm_color_lut *blob_data;
3436 int i, ret = 0;
3437
3438 state = drm_atomic_state_alloc(crtc->dev);
3439 if (!state)
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003440 return -ENOMEM;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003441
3442 blob = drm_property_create_blob(dev,
3443 sizeof(struct drm_color_lut) * size,
3444 NULL);
Lionel Landwerlin562c5b42016-03-10 12:04:21 +00003445 if (IS_ERR(blob)) {
3446 ret = PTR_ERR(blob);
Lionel Landwerlinc1f415c2016-03-11 12:17:26 +00003447 blob = NULL;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003448 goto fail;
3449 }
3450
3451 /* Prepare GAMMA_LUT with the legacy values. */
3452 blob_data = (struct drm_color_lut *) blob->data;
3453 for (i = 0; i < size; i++) {
3454 blob_data[i].red = red[i];
3455 blob_data[i].green = green[i];
3456 blob_data[i].blue = blue[i];
3457 }
3458
3459 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3460retry:
3461 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3462 if (IS_ERR(crtc_state)) {
3463 ret = PTR_ERR(crtc_state);
3464 goto fail;
3465 }
3466
3467 /* Reset DEGAMMA_LUT and CTM properties. */
3468 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3469 config->degamma_lut_property, 0);
3470 if (ret)
3471 goto fail;
3472
3473 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3474 config->ctm_property, 0);
3475 if (ret)
3476 goto fail;
3477
3478 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3479 config->gamma_lut_property, blob->base.id);
3480 if (ret)
3481 goto fail;
3482
3483 ret = drm_atomic_commit(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003484fail:
3485 if (ret == -EDEADLK)
3486 goto backoff;
3487
Chris Wilson08536952016-10-14 13:18:18 +01003488 drm_atomic_state_put(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003489 drm_property_unreference_blob(blob);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003490 return ret;
Chris Wilson08536952016-10-14 13:18:18 +01003491
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003492backoff:
3493 drm_atomic_state_clear(state);
3494 drm_atomic_legacy_backoff(state);
3495
3496 goto retry;
3497}
3498EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);