blob: de7fddce3cefac5a00de3fb95b94578585b9ce8d [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>
Daniel Vettere2330f02014-10-29 11:34:56 +010033#include <linux/fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Daniel Vetter3150c7d2014-11-06 20:53:29 +010035/**
36 * DOC: overview
37 *
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
43 *
44 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020045 * drm_atomic_helper_check() and for the commit callback with
46 * drm_atomic_helper_commit(). But the individual stages and callbacks are
47 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010048 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020051 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
52 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010053 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010055 *
56 * The atomic helper uses the same function table structures as all other
57 * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58 * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59 * also shares the struct &drm_plane_helper_funcs function table with the plane
60 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010061 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010062static void
63drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 struct drm_plane_state *plane_state,
65 struct drm_plane *plane)
66{
67 struct drm_crtc_state *crtc_state;
68
69 if (plane->state->crtc) {
Andrzej Hajda2943ef32016-03-15 13:46:00 +010070 crtc_state = drm_atomic_get_existing_crtc_state(state,
71 plane->state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010072
73 if (WARN_ON(!crtc_state))
74 return;
75
76 crtc_state->planes_changed = true;
77 }
78
79 if (plane_state->crtc) {
Andrzej Hajda2943ef32016-03-15 13:46:00 +010080 crtc_state = drm_atomic_get_existing_crtc_state(state,
81 plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010082
83 if (WARN_ON(!crtc_state))
84 return;
85
86 crtc_state->planes_changed = true;
87 }
88}
89
Maarten Lankhorst8248b652016-03-03 10:17:40 +010090static int handle_conflicting_encoders(struct drm_atomic_state *state,
91 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +010092{
Daniel Vetter97ac3202015-12-03 10:49:14 +010093 struct drm_connector_state *conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +020094 struct drm_connector *connector;
Maarten Lankhorst40616a22016-03-03 10:17:39 +010095 struct drm_encoder *encoder;
96 unsigned encoder_mask = 0;
97 int i, ret;
Daniel Vetter623369e2014-09-16 17:50:47 +020098
Maarten Lankhorst8248b652016-03-03 10:17:40 +010099 /*
100 * First loop, find all newly assigned encoders from the connectors
101 * part of the state. If the same encoder is assigned to multiple
102 * connectors bail out.
103 */
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100104 for_each_connector_in_state(state, connector, conn_state, i) {
105 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
106 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200107
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100108 if (!conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200109 continue;
110
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100111 if (funcs->atomic_best_encoder)
112 new_encoder = funcs->atomic_best_encoder(connector, conn_state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200113 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100114 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200115 else
116 new_encoder = drm_atomic_helper_best_encoder(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100117
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100118 if (new_encoder) {
119 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
120 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
121 new_encoder->base.id, new_encoder->name,
122 connector->base.id, connector->name);
123
124 return -EINVAL;
125 }
126
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100127 encoder_mask |= 1 << drm_encoder_index(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100128 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200129 }
130
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100131 if (!encoder_mask)
132 return 0;
133
134 /*
135 * Second loop, iterate over all connectors not part of the state.
136 *
137 * If a conflicting encoder is found and disable_conflicting_encoders
138 * is not set, an error is returned. Userspace can provide a solution
139 * through the atomic ioctl.
140 *
141 * If the flag is set conflicting connectors are removed from the crtc
142 * and the crtc is disabled if no encoder is left. This preserves
143 * compatibility with the legacy set_config behavior.
144 */
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100145 drm_for_each_connector(connector, state->dev) {
146 struct drm_crtc_state *crtc_state;
147
148 if (drm_atomic_get_existing_connector_state(state, connector))
149 continue;
150
151 encoder = connector->state->best_encoder;
152 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
153 continue;
154
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100155 if (!disable_conflicting_encoders) {
156 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
157 encoder->base.id, encoder->name,
158 connector->state->crtc->base.id,
159 connector->state->crtc->name,
160 connector->base.id, connector->name);
161 return -EINVAL;
162 }
163
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100164 conn_state = drm_atomic_get_connector_state(state, connector);
165 if (IS_ERR(conn_state))
166 return PTR_ERR(conn_state);
167
168 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
169 encoder->base.id, encoder->name,
170 conn_state->crtc->base.id, conn_state->crtc->name,
171 connector->base.id, connector->name);
172
173 crtc_state = drm_atomic_get_existing_crtc_state(state, conn_state->crtc);
174
175 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
176 if (ret)
177 return ret;
178
179 if (!crtc_state->connector_mask) {
180 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
181 NULL);
182 if (ret < 0)
183 return ret;
184
185 crtc_state->active = false;
186 }
187 }
188
189 return 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200190}
191
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100192static void
193set_best_encoder(struct drm_atomic_state *state,
194 struct drm_connector_state *conn_state,
195 struct drm_encoder *encoder)
196{
197 struct drm_crtc_state *crtc_state;
198 struct drm_crtc *crtc;
199
200 if (conn_state->best_encoder) {
201 /* Unset the encoder_mask in the old crtc state. */
202 crtc = conn_state->connector->state->crtc;
203
204 /* A NULL crtc is an error here because we should have
205 * duplicated a NULL best_encoder when crtc was NULL.
206 * As an exception restoring duplicated atomic state
207 * during resume is allowed, so don't warn when
208 * best_encoder is equal to encoder we intend to set.
209 */
210 WARN_ON(!crtc && encoder != conn_state->best_encoder);
211 if (crtc) {
212 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
213
214 crtc_state->encoder_mask &=
215 ~(1 << drm_encoder_index(conn_state->best_encoder));
216 }
217 }
218
219 if (encoder) {
220 crtc = conn_state->crtc;
221 WARN_ON(!crtc);
222 if (crtc) {
223 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
224
225 crtc_state->encoder_mask |=
226 1 << drm_encoder_index(encoder);
227 }
228 }
229
230 conn_state->best_encoder = encoder;
231}
232
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100233static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200234steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100235 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200236{
Daniel Vetter623369e2014-09-16 17:50:47 +0200237 struct drm_crtc_state *crtc_state;
238 struct drm_connector *connector;
239 struct drm_connector_state *connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100240 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200241
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100242 for_each_connector_in_state(state, connector, connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100243 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200244
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100245 if (connector_state->best_encoder != encoder)
246 continue;
247
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100248 encoder_crtc = connector->state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200249
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100250 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
251 encoder->base.id, encoder->name,
252 encoder_crtc->base.id, encoder_crtc->name);
253
Daniel Vetter623369e2014-09-16 17:50:47 +0200254 set_best_encoder(state, connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100255
256 crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
257 crtc_state->connectors_changed = true;
258
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100259 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200260 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200261}
262
263static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100264update_connector_routing(struct drm_atomic_state *state,
265 struct drm_connector *connector,
266 struct drm_connector_state *connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200267{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200268 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200269 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200270 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200271
Daniel Vetter17a38d92015-02-22 12:24:16 +0100272 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
273 connector->base.id,
274 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200275
276 if (connector->state->crtc != connector_state->crtc) {
277 if (connector->state->crtc) {
Maarten Lankhorst9b8d1e52016-03-03 10:17:42 +0100278 crtc_state = drm_atomic_get_existing_crtc_state(state, connector->state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200279 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200280 }
281
282 if (connector_state->crtc) {
Maarten Lankhorst9b8d1e52016-03-03 10:17:42 +0100283 crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200284 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200285 }
286 }
287
288 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100289 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200290 connector->base.id,
291 connector->name);
292
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100293 set_best_encoder(state, connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200294
295 return 0;
296 }
297
298 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200299
300 if (funcs->atomic_best_encoder)
301 new_encoder = funcs->atomic_best_encoder(connector,
302 connector_state);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200303 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200304 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93fe2016-06-07 13:47:56 +0200305 else
306 new_encoder = drm_atomic_helper_best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200307
308 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100309 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
310 connector->base.id,
311 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200312 return -EINVAL;
313 }
314
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100315 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
316 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
317 new_encoder->base.id,
318 new_encoder->name,
319 connector_state->crtc->base.id);
320 return -EINVAL;
321 }
322
Daniel Vetter623369e2014-09-16 17:50:47 +0200323 if (new_encoder == connector_state->best_encoder) {
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100324 set_best_encoder(state, connector_state, new_encoder);
325
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200326 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100327 connector->base.id,
328 connector->name,
329 new_encoder->base.id,
330 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200331 connector_state->crtc->base.id,
332 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200333
334 return 0;
335 }
336
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100337 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200338
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100339 set_best_encoder(state, connector_state, new_encoder);
340
Maarten Lankhorst9b8d1e52016-03-03 10:17:42 +0100341 crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200342 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200343
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200344 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100345 connector->base.id,
346 connector->name,
347 new_encoder->base.id,
348 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200349 connector_state->crtc->base.id,
350 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200351
352 return 0;
353}
354
355static int
356mode_fixup(struct drm_atomic_state *state)
357{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300358 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200359 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300360 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200361 struct drm_connector_state *conn_state;
362 int i;
363 bool ret;
364
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300365 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200366 if (!crtc_state->mode_changed &&
367 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200368 continue;
369
370 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
371 }
372
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300373 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200374 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200375 struct drm_encoder *encoder;
376
Daniel Vetter623369e2014-09-16 17:50:47 +0200377 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
378
379 if (!conn_state->crtc || !conn_state->best_encoder)
380 continue;
381
Andrzej Hajda2943ef32016-03-15 13:46:00 +0100382 crtc_state = drm_atomic_get_existing_crtc_state(state,
383 conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200384
385 /*
386 * Each encoder has at most one connector (since we always steal
387 * it away), so we won't call ->mode_fixup twice.
388 */
389 encoder = conn_state->best_encoder;
390 funcs = encoder->helper_private;
391
Archit Taneja862e6862015-05-21 11:03:16 +0530392 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
393 &crtc_state->adjusted_mode);
394 if (!ret) {
395 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
396 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200397 }
398
Noralf Trønnes28276352016-05-11 18:09:20 +0200399 if (funcs && funcs->atomic_check) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100400 ret = funcs->atomic_check(encoder, crtc_state,
401 conn_state);
402 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100403 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
404 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100405 return ret;
406 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200407 } else if (funcs && funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100408 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
409 &crtc_state->adjusted_mode);
410 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100411 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
412 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100413 return -EINVAL;
414 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200415 }
416 }
417
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300418 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200419 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200420
Liu Yingf55f1702016-05-27 17:35:54 +0800421 if (!crtc_state->enable)
422 continue;
423
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200424 if (!crtc_state->mode_changed &&
425 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200426 continue;
427
428 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300429 if (!funcs->mode_fixup)
430 continue;
431
Daniel Vetter623369e2014-09-16 17:50:47 +0200432 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
433 &crtc_state->adjusted_mode);
434 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200435 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
436 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200437 return -EINVAL;
438 }
439 }
440
441 return 0;
442}
443
Daniel Vetterd9b13622014-11-26 16:57:41 +0100444/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800445 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100446 * @dev: DRM device
447 * @state: the driver state object
448 *
449 * Check the state object to see if the requested state is physically possible.
450 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200451 * update and adds any additional connectors needed for full modesets and calls
452 * down into ->mode_fixup functions of the driver backend.
453 *
454 * crtc_state->mode_changed is set when the input mode is changed.
455 * crtc_state->connectors_changed is set when a connector is added or
456 * removed from the crtc.
457 * crtc_state->active_changed is set when crtc_state->active changes,
458 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100459 *
460 * IMPORTANT:
461 *
462 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
463 * plane update can't be done without a full modeset) _must_ call this function
464 * afterwards after that change. It is permitted to call this function multiple
465 * times for the same update, e.g. when the ->atomic_check functions depend upon
466 * the adjusted dotclock for fifo space allocation and watermark computation.
467 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200468 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100469 * Zero for success or -errno
470 */
471int
Rob Clark934ce1c2014-11-19 16:41:33 -0500472drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200473 struct drm_atomic_state *state)
474{
Daniel Vetter623369e2014-09-16 17:50:47 +0200475 struct drm_crtc *crtc;
476 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300477 struct drm_connector *connector;
478 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200479 int i, ret;
480
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300481 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200482 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200483 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
484 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200485 crtc_state->mode_changed = true;
486 }
487
488 if (crtc->state->enable != crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200489 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
490 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200491
492 /*
493 * For clarity this assignment is done here, but
494 * enable == 0 is only true when there are no
495 * connectors and a NULL mode.
496 *
497 * The other way around is true as well. enable != 0
498 * iff connectors are attached and a mode is set.
499 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200500 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200501 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200502 }
503 }
504
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100505 ret = handle_conflicting_encoders(state, state->legacy_set_config);
506 if (ret)
507 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100508
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300509 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200510 /*
511 * This only sets crtc->mode_changed for routing changes,
512 * drivers must set crtc->mode_changed themselves when connector
513 * properties need to be updated.
514 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100515 ret = update_connector_routing(state, connector,
516 connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200517 if (ret)
518 return ret;
519 }
520
521 /*
522 * After all the routing has been prepared we need to add in any
523 * connector which is itself unchanged, but who's crtc changes it's
524 * configuration. This must be done before calling mode_fixup in case a
525 * crtc only changed its mode but has the same set of connectors.
526 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300527 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100528 bool has_connectors =
529 !!crtc_state->connector_mask;
Daniel Vetter623369e2014-09-16 17:50:47 +0200530
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100531 /*
532 * We must set ->active_changed after walking connectors for
533 * otherwise an update that only changes active would result in
534 * a full modeset because update_connector_routing force that.
535 */
536 if (crtc->state->active != crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200537 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
538 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100539 crtc_state->active_changed = true;
540 }
541
Daniel Vetter2465ff62015-06-18 09:58:55 +0200542 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100543 continue;
544
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200545 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
546 crtc->base.id, crtc->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100547 crtc_state->enable ? 'y' : 'n',
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200548 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200549
550 ret = drm_atomic_add_affected_connectors(state, crtc);
551 if (ret != 0)
552 return ret;
553
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200554 ret = drm_atomic_add_affected_planes(state, crtc);
555 if (ret != 0)
556 return ret;
557
Maarten Lankhorst14de6c42016-01-04 12:53:20 +0100558 if (crtc_state->enable != has_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200559 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
560 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200561
562 return -EINVAL;
563 }
564 }
565
566 return mode_fixup(state);
567}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100568EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200569
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100570/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800571 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100572 * @dev: DRM device
573 * @state: the driver state object
574 *
575 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100576 * This does all the plane update related checks using by calling into the
577 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100578 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200579 * It also sets crtc_state->planes_changed to indicate that a crtc has
580 * updated planes.
581 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200582 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100583 * Zero for success or -errno
584 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100585int
586drm_atomic_helper_check_planes(struct drm_device *dev,
587 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100588{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300589 struct drm_crtc *crtc;
590 struct drm_crtc_state *crtc_state;
591 struct drm_plane *plane;
592 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100593 int i, ret = 0;
594
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300595 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200596 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100597
598 funcs = plane->helper_private;
599
600 drm_atomic_helper_plane_changed(state, plane_state, plane);
601
602 if (!funcs || !funcs->atomic_check)
603 continue;
604
605 ret = funcs->atomic_check(plane, plane_state);
606 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200607 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
608 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100609 return ret;
610 }
611 }
612
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300613 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200614 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100615
616 funcs = crtc->helper_private;
617
618 if (!funcs || !funcs->atomic_check)
619 continue;
620
Daniel Vetterbe9174a2016-06-02 00:06:24 +0200621 ret = funcs->atomic_check(crtc, crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100622 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200623 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
624 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100625 return ret;
626 }
627 }
628
Daniel Vetterd9b13622014-11-26 16:57:41 +0100629 return ret;
630}
631EXPORT_SYMBOL(drm_atomic_helper_check_planes);
632
633/**
634 * drm_atomic_helper_check - validate state object
635 * @dev: DRM device
636 * @state: the driver state object
637 *
638 * Check the state object to see if the requested state is physically possible.
639 * Only crtcs and planes have check callbacks, so for any additional (global)
640 * checking that a driver needs it can simply wrap that around this function.
641 * Drivers without such needs can directly use this as their ->atomic_check()
642 * callback.
643 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100644 * This just wraps the two parts of the state checking for planes and modeset
645 * state in the default order: First it calls drm_atomic_helper_check_modeset()
646 * and then drm_atomic_helper_check_planes(). The assumption is that the
647 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
648 * e.g. properly compute watermarks.
649 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200650 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100651 * Zero for success or -errno
652 */
653int drm_atomic_helper_check(struct drm_device *dev,
654 struct drm_atomic_state *state)
655{
656 int ret;
657
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100658 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100659 if (ret)
660 return ret;
661
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100662 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500663 if (ret)
664 return ret;
665
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100666 return ret;
667}
668EXPORT_SYMBOL(drm_atomic_helper_check);
669
Daniel Vetter623369e2014-09-16 17:50:47 +0200670static void
671disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
672{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300673 struct drm_connector *connector;
674 struct drm_connector_state *old_conn_state;
675 struct drm_crtc *crtc;
676 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200677 int i;
678
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300679 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200680 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200681 struct drm_encoder *encoder;
682
Daniel Vetter623369e2014-09-16 17:50:47 +0200683 /* Shut down everything that's in the changeset and currently
684 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300685 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200686 continue;
687
Andrzej Hajda2943ef32016-03-15 13:46:00 +0100688 old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
689 old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100690
Daniel Vetter4218a322015-03-26 22:18:40 +0100691 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200692 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100693 continue;
694
Rob Clark46df9ad2014-11-20 15:40:36 -0500695 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200696
Rob Clark46df9ad2014-11-20 15:40:36 -0500697 /* We shouldn't get this far if we didn't previously have
698 * an encoder.. but WARN_ON() rather than explode.
699 */
700 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200701 continue;
702
703 funcs = encoder->helper_private;
704
Daniel Vetter17a38d92015-02-22 12:24:16 +0100705 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
706 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100707
Daniel Vetter623369e2014-09-16 17:50:47 +0200708 /*
709 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800710 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200711 */
Archit Taneja862e6862015-05-21 11:03:16 +0530712 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200713
714 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200715 if (funcs) {
716 if (connector->state->crtc && funcs->prepare)
717 funcs->prepare(encoder);
718 else if (funcs->disable)
719 funcs->disable(encoder);
720 else if (funcs->dpms)
721 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
722 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200723
Archit Taneja862e6862015-05-21 11:03:16 +0530724 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200725 }
726
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300727 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200728 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200729
730 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200731 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100732 continue;
733
734 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200735 continue;
736
737 funcs = crtc->helper_private;
738
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200739 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
740 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100741
742
Daniel Vetter623369e2014-09-16 17:50:47 +0200743 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100744 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200745 funcs->prepare(crtc);
746 else if (funcs->disable)
747 funcs->disable(crtc);
748 else
749 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
750 }
751}
752
Daniel Vetter4c18d302015-05-12 15:27:37 +0200753/**
754 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
755 * @dev: DRM device
756 * @old_state: atomic state object with old state structures
757 *
758 * This function updates all the various legacy modeset state pointers in
759 * connectors, encoders and crtcs. It also updates the timestamping constants
760 * used for precise vblank timestamps by calling
761 * drm_calc_timestamping_constants().
762 *
763 * Drivers can use this for building their own atomic commit if they don't have
764 * a pure helper-based modeset implementation.
765 */
766void
767drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
768 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200769{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300770 struct drm_connector *connector;
771 struct drm_connector_state *old_conn_state;
772 struct drm_crtc *crtc;
773 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200774 int i;
775
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200776 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300777 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200778 if (connector->encoder) {
779 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200780
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200781 connector->encoder->crtc = NULL;
782 connector->encoder = NULL;
783 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200784
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200785 crtc = connector->state->crtc;
786 if ((!crtc && old_conn_state->crtc) ||
787 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
788 struct drm_property *dpms_prop =
789 dev->mode_config.dpms_property;
790 int mode = DRM_MODE_DPMS_OFF;
791
792 if (crtc && crtc->state->active)
793 mode = DRM_MODE_DPMS_ON;
794
795 connector->dpms = mode;
796 drm_object_property_set_value(&connector->base,
797 dpms_prop, mode);
798 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200799 }
800
801 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300802 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
803 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200804 continue;
805
806 if (WARN_ON(!connector->state->best_encoder))
807 continue;
808
809 connector->encoder = connector->state->best_encoder;
810 connector->encoder->crtc = connector->state->crtc;
811 }
812
813 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300814 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200815 struct drm_plane *primary = crtc->primary;
816
Daniel Vetter623369e2014-09-16 17:50:47 +0200817 crtc->mode = crtc->state->mode;
818 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200819
820 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
821 primary->state->crtc == crtc) {
822 crtc->x = primary->state->src_x >> 16;
823 crtc->y = primary->state->src_y >> 16;
824 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200825
826 if (crtc->state->enable)
827 drm_calc_timestamping_constants(crtc,
828 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200829 }
830}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200831EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200832
833static void
834crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
835{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300836 struct drm_crtc *crtc;
837 struct drm_crtc_state *old_crtc_state;
838 struct drm_connector *connector;
839 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200840 int i;
841
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300842 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200843 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200844
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300845 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200846 continue;
847
848 funcs = crtc->helper_private;
849
Daniel Vetterc982bd92015-02-22 12:24:20 +0100850 if (crtc->state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200851 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
852 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100853
Daniel Vetter623369e2014-09-16 17:50:47 +0200854 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100855 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200856 }
857
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300858 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200859 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200860 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200861 struct drm_encoder *encoder;
862 struct drm_display_mode *mode, *adjusted_mode;
863
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300864 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200865 continue;
866
867 encoder = connector->state->best_encoder;
868 funcs = encoder->helper_private;
869 new_crtc_state = connector->state->crtc->state;
870 mode = &new_crtc_state->mode;
871 adjusted_mode = &new_crtc_state->adjusted_mode;
872
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100873 if (!new_crtc_state->mode_changed)
874 continue;
875
Daniel Vetter17a38d92015-02-22 12:24:16 +0100876 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
877 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100878
Daniel Vetter623369e2014-09-16 17:50:47 +0200879 /*
880 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800881 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200882 */
Noralf Trønnes28276352016-05-11 18:09:20 +0200883 if (funcs && funcs->mode_set)
Daniel Vetterc982bd92015-02-22 12:24:20 +0100884 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200885
Archit Taneja862e6862015-05-21 11:03:16 +0530886 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200887 }
888}
889
890/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100891 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200892 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100893 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200894 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100895 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200896 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100897 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300898 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100899 * drm_atomic_helper_commit_planes(), which is what the default commit function
900 * does. But drivers with different needs can group the modeset commits together
901 * and do the plane commits at the end. This is useful for drivers doing runtime
902 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200903 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100904void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
905 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200906{
Laurent Pincharta072f802015-02-22 12:24:18 +0100907 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200908
909 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
910
Laurent Pincharta072f802015-02-22 12:24:18 +0100911 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200912}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100913EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200914
915/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100916 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200917 * @dev: DRM device
918 * @old_state: atomic state object with old state structures
919 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100920 * This function enables all the outputs with the new configuration which had to
921 * be turned off for the update.
922 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300923 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100924 * drm_atomic_helper_commit_planes(), which is what the default commit function
925 * does. But drivers with different needs can group the modeset commits together
926 * and do the plane commits at the end. This is useful for drivers doing runtime
927 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200928 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100929void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
930 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200931{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300932 struct drm_crtc *crtc;
933 struct drm_crtc_state *old_crtc_state;
934 struct drm_connector *connector;
935 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200936 int i;
937
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300938 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200939 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200940
941 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200942 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100943 continue;
944
945 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200946 continue;
947
948 funcs = crtc->helper_private;
949
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100950 if (crtc->state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200951 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
952 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100953
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100954 if (funcs->enable)
955 funcs->enable(crtc);
956 else
957 funcs->commit(crtc);
958 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200959 }
960
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300961 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200962 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200963 struct drm_encoder *encoder;
964
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300965 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200966 continue;
967
Daniel Vetter4218a322015-03-26 22:18:40 +0100968 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200969 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100970 continue;
971
Daniel Vetter623369e2014-09-16 17:50:47 +0200972 encoder = connector->state->best_encoder;
973 funcs = encoder->helper_private;
974
Daniel Vetter17a38d92015-02-22 12:24:16 +0100975 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
976 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100977
Daniel Vetter623369e2014-09-16 17:50:47 +0200978 /*
979 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800980 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200981 */
Archit Taneja862e6862015-05-21 11:03:16 +0530982 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200983
Noralf Trønnes28276352016-05-11 18:09:20 +0200984 if (funcs) {
985 if (funcs->enable)
986 funcs->enable(encoder);
987 else if (funcs->commit)
988 funcs->commit(encoder);
989 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200990
Archit Taneja862e6862015-05-21 11:03:16 +0530991 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200992 }
993}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100994EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200995
Rob Clark4c5b7f32016-03-18 19:14:55 -0400996/**
997 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
998 * @dev: DRM device
999 * @state: atomic state object with old state structures
1000 *
1001 * For implicit sync, driver should fish the exclusive fence out from the
1002 * incoming fb's and stash it in the drm_plane_state. This is called after
1003 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1004 * just uses the atomic state to find the changed planes)
1005 */
1006void drm_atomic_helper_wait_for_fences(struct drm_device *dev,
Daniel Vettere2330f02014-10-29 11:34:56 +01001007 struct drm_atomic_state *state)
1008{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001009 struct drm_plane *plane;
1010 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +01001011 int i;
1012
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001013 for_each_plane_in_state(state, plane, plane_state, i) {
1014 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001015 continue;
1016
1017 WARN_ON(!plane->state->fb);
1018
1019 fence_wait(plane->state->fence, false);
1020 fence_put(plane->state->fence);
1021 plane->state->fence = NULL;
1022 }
1023}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001024EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001025
John Keepingc2409062016-01-19 10:46:58 +00001026/**
1027 * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
1028 * @dev: DRM device
1029 * @old_state: atomic state object with old state structures
1030 * @crtc: DRM crtc
1031 *
1032 * Checks whether the framebuffer used for this CRTC changes as a result of
1033 * the atomic update. This is useful for drivers which cannot use
1034 * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
1035 * functionality.
1036 *
1037 * Returns:
1038 * true if the framebuffer changed.
1039 */
1040bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
1041 struct drm_atomic_state *old_state,
1042 struct drm_crtc *crtc)
Daniel Vetterab58e332014-11-24 20:42:42 +01001043{
1044 struct drm_plane *plane;
1045 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +01001046 int i;
1047
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001048 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +01001049 if (plane->state->crtc != crtc &&
1050 old_plane_state->crtc != crtc)
1051 continue;
1052
1053 if (plane->state->fb != old_plane_state->fb)
1054 return true;
1055 }
1056
1057 return false;
1058}
John Keepingc2409062016-01-19 10:46:58 +00001059EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
Daniel Vetterab58e332014-11-24 20:42:42 +01001060
Rob Clark5ee32292014-11-11 19:38:59 -05001061/**
1062 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1063 * @dev: DRM device
1064 * @old_state: atomic state object with old state structures
1065 *
1066 * Helper to, after atomic commit, wait for vblanks on all effected
1067 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +01001068 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1069 * framebuffers have actually changed to optimize for the legacy cursor and
1070 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -05001071 */
1072void
1073drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1074 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001075{
1076 struct drm_crtc *crtc;
1077 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001078 int i, ret;
1079
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001080 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +02001081 /* No one cares about the old state, so abuse it for tracking
1082 * and store whether we hold a vblank reference (and should do a
1083 * vblank wait) in the ->enable boolean. */
1084 old_crtc_state->enable = false;
1085
1086 if (!crtc->state->enable)
1087 continue;
1088
Daniel Vetterf02ad902015-01-22 16:36:23 +01001089 /* Legacy cursor ioctls are completely unsynced, and userspace
1090 * relies on that (by doing tons of cursor updates). */
1091 if (old_state->legacy_cursor_update)
1092 continue;
1093
John Keepingc2409062016-01-19 10:46:58 +00001094 if (!drm_atomic_helper_framebuffer_changed(dev,
1095 old_state, crtc))
Daniel Vetterab58e332014-11-24 20:42:42 +01001096 continue;
1097
Daniel Vetter623369e2014-09-16 17:50:47 +02001098 ret = drm_crtc_vblank_get(crtc);
1099 if (ret != 0)
1100 continue;
1101
1102 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +02001103 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001104 }
1105
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001106 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1107 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +02001108 continue;
1109
1110 ret = wait_event_timeout(dev->vblank[i].queue,
1111 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001112 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001113 msecs_to_jiffies(50));
1114
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001115 WARN(!ret, "[CRTC:%d] vblank wait timed out\n", crtc->base.id);
1116
Daniel Vetter623369e2014-09-16 17:50:47 +02001117 drm_crtc_vblank_put(crtc);
1118 }
1119}
Rob Clark5ee32292014-11-11 19:38:59 -05001120EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001121
1122/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001123 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1124 * @state: new modeset state to be committed
Daniel Vetter623369e2014-09-16 17:50:47 +02001125 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001126 * This is the default implemenation for the ->atomic_commit_tail() hook of the
1127 * &drm_mode_config_helper_funcs vtable.
Daniel Vetter623369e2014-09-16 17:50:47 +02001128 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001129 * Note that the default ordering of how the various stages are called is to
1130 * match the legacy modeset helper library closest. One peculiarity of that is
1131 * that it doesn't mesh well with runtime PM at all.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001132 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001133 * For drivers supporting runtime PM the recommended sequence is instead ::
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001134 *
1135 * drm_atomic_helper_commit_modeset_disables(dev, state);
1136 *
1137 * drm_atomic_helper_commit_modeset_enables(dev, state);
1138 *
1139 * drm_atomic_helper_commit_planes(dev, state, true);
1140 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001141 * for committing the atomic update to hardware. See the kerneldoc entries for
1142 * these three functions for more details.
1143 */
1144void drm_atomic_helper_commit_tail(struct drm_atomic_state *state)
1145{
1146 struct drm_device *dev = state->dev;
1147
1148 drm_atomic_helper_commit_modeset_disables(dev, state);
1149
1150 drm_atomic_helper_commit_planes(dev, state, false);
1151
1152 drm_atomic_helper_commit_modeset_enables(dev, state);
1153
1154 drm_atomic_helper_commit_hw_done(state);
1155
1156 drm_atomic_helper_wait_for_vblanks(dev, state);
1157
1158 drm_atomic_helper_cleanup_planes(dev, state);
1159}
1160EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1161
1162static void commit_tail(struct drm_atomic_state *state)
1163{
1164 struct drm_device *dev = state->dev;
1165 struct drm_mode_config_helper_funcs *funcs;
1166
1167 funcs = dev->mode_config.helper_private;
1168
1169 drm_atomic_helper_wait_for_fences(dev, state);
1170
1171 drm_atomic_helper_wait_for_dependencies(state);
1172
1173 if (funcs && funcs->atomic_commit_tail)
1174 funcs->atomic_commit_tail(state);
1175 else
1176 drm_atomic_helper_commit_tail(state);
1177
1178 drm_atomic_helper_commit_cleanup_done(state);
1179
1180 drm_atomic_state_free(state);
1181}
1182
1183static void commit_work(struct work_struct *work)
1184{
1185 struct drm_atomic_state *state = container_of(work,
1186 struct drm_atomic_state,
1187 commit_work);
1188 commit_tail(state);
1189}
1190
1191/**
1192 * drm_atomic_helper_commit - commit validated state object
1193 * @dev: DRM device
1194 * @state: the driver state object
1195 * @nonblock: whether nonblocking behavior is requested.
1196 *
1197 * This function commits a with drm_atomic_helper_check() pre-validated state
1198 * object. This can still fail when e.g. the framebuffer reservation fails. This
1199 * function implements nonblocking commits, using
1200 * drm_atomic_helper_setup_commit() and related functions.
1201 *
1202 * Note that right now this function does not support nonblocking commits, hence
1203 * driver writers must implement their own version for now.
1204 *
1205 * 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
1228 /*
1229 * This is the point of no return - everything below never fails except
1230 * when the hw goes bonghits. Which means we can commit the new state on
1231 * the software side now.
1232 */
1233
Daniel Vetter5e84c262016-06-10 00:06:32 +02001234 drm_atomic_helper_swap_state(state, true);
Daniel Vetter623369e2014-09-16 17:50:47 +02001235
1236 /*
1237 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001238 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001239 * that the asynchronous work has either been cancelled (if the driver
1240 * supports it, which at least requires that the framebuffers get
1241 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1242 * before the new state gets committed on the software side with
1243 * drm_atomic_helper_swap_state().
1244 *
1245 * This scheme allows new atomic state updates to be prepared and
1246 * checked in parallel to the asynchronous completion of the previous
1247 * update. Which is important since compositors need to figure out the
1248 * composition of the next frame right after having submitted the
1249 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001250 *
1251 * NOTE: Commit work has multiple phases, first hardware commit, then
1252 * cleanup. We want them to overlap, hence need system_unbound_wq to
1253 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001254 */
1255
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001256 if (nonblock)
1257 queue_work(system_unbound_wq, &state->commit_work);
1258 else
1259 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001260
1261 return 0;
1262}
1263EXPORT_SYMBOL(drm_atomic_helper_commit);
1264
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001265/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001266 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001267 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001268 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001269 *
1270 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1271 * which commit needs to call which can fail, so we want to run it first and
1272 * synchronously.
1273 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001274 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001275 * might be affected the new state update. This can be done by either cancelling
1276 * or flushing the work items, depending upon whether the driver can deal with
1277 * cancelled updates. Note that it is important to ensure that the framebuffer
1278 * cleanup is still done when cancelling.
1279 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001280 * Asynchronous workers need to have sufficient parallelism to be able to run
1281 * different atomic commits on different CRTCs in parallel. The simplest way to
1282 * achive this is by running them on the &system_unbound_wq work queue. Note
1283 * that drivers are not required to split up atomic commits and run an
1284 * individual commit in parallel - userspace is supposed to do that if it cares.
1285 * But it might be beneficial to do that for modesets, since those necessarily
1286 * must be done as one global operation, and enabling or disabling a CRTC can
1287 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001288 *
1289 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001290 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001291 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001292 * while it's guaranteed that no relevant nonblocking worker runs means that
1293 * nonblocking workers do not need grab any locks. Actually they must not grab
1294 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001295 *
1296 * 4. Schedule a work item to do all subsequent steps, using the split-out
1297 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1298 * then cleaning up the framebuffers after the old framebuffer is no longer
1299 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001300 *
1301 * The above scheme is implemented in the atomic helper libraries in
1302 * drm_atomic_helper_commit() using a bunch of helper functions. See
1303 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001304 */
1305
Daniel Vettera095caa2016-06-08 17:15:36 +02001306static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1307{
1308 struct drm_crtc_commit *commit, *stall_commit = NULL;
1309 bool completed = true;
1310 int i;
1311 long ret = 0;
1312
1313 spin_lock(&crtc->commit_lock);
1314 i = 0;
1315 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1316 if (i == 0) {
1317 completed = try_wait_for_completion(&commit->flip_done);
1318 /* Userspace is not allowed to get ahead of the previous
1319 * commit with nonblocking ones. */
1320 if (!completed && nonblock) {
1321 spin_unlock(&crtc->commit_lock);
1322 return -EBUSY;
1323 }
1324 } else if (i == 1) {
1325 stall_commit = commit;
1326 drm_crtc_commit_get(stall_commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001327 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001328 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001329
1330 i++;
1331 }
1332 spin_unlock(&crtc->commit_lock);
1333
1334 if (!stall_commit)
1335 return 0;
1336
1337 /* We don't want to let commits get ahead of cleanup work too much,
1338 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1339 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001340 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001341 10*HZ);
1342 if (ret == 0)
1343 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1344 crtc->base.id, crtc->name);
1345
1346 drm_crtc_commit_put(stall_commit);
1347
1348 return ret < 0 ? ret : 0;
1349}
1350
1351/**
1352 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1353 * @state: new modeset state to be committed
1354 * @nonblock: whether nonblocking behavior is requested.
1355 *
1356 * This function prepares @state to be used by the atomic helper's support for
1357 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1358 * should always call this function from their ->atomic_commit hook.
1359 *
1360 * To be able to use this support drivers need to use a few more helper
1361 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1362 * actually committing the hardware state, and for nonblocking commits this call
1363 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1364 * and it's stall parameter, for when a driver's commit hooks look at the
1365 * ->state pointers of struct &drm_crtc, &drm_plane or &drm_connector directly.
1366 *
1367 * Completion of the hardware commit step must be signalled using
1368 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1369 * to read or change any permanent software or hardware modeset state. The only
1370 * exception is state protected by other means than &drm_modeset_lock locks.
1371 * Only the free standing @state with pointers to the old state structures can
1372 * be inspected, e.g. to clean up old buffers using
1373 * drm_atomic_helper_cleanup_planes().
1374 *
1375 * At the very end, before cleaning up @state drivers must call
1376 * drm_atomic_helper_commit_cleanup_done().
1377 *
1378 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1379 * complete and esay-to-use default implementation of the atomic_commit() hook.
1380 *
1381 * The tracking of asynchronously executed and still pending commits is done
1382 * using the core structure &drm_crtc_commit.
1383 *
1384 * By default there's no need to clean up resources allocated by this function
1385 * explicitly: drm_atomic_state_default_clear() will take care of that
1386 * automatically.
1387 *
1388 * Returns:
1389 *
1390 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1391 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1392 */
1393int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1394 bool nonblock)
1395{
1396 struct drm_crtc *crtc;
1397 struct drm_crtc_state *crtc_state;
1398 struct drm_crtc_commit *commit;
1399 int i, ret;
1400
1401 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1402 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1403 if (!commit)
1404 return -ENOMEM;
1405
1406 init_completion(&commit->flip_done);
1407 init_completion(&commit->hw_done);
1408 init_completion(&commit->cleanup_done);
1409 INIT_LIST_HEAD(&commit->commit_entry);
1410 kref_init(&commit->ref);
1411 commit->crtc = crtc;
1412
1413 state->crtcs[i].commit = commit;
1414
1415 ret = stall_checks(crtc, nonblock);
1416 if (ret)
1417 return ret;
1418
1419 /* Drivers only send out events when at least either current or
1420 * new CRTC state is active. Complete right away if everything
1421 * stays off. */
1422 if (!crtc->state->active && !crtc_state->active) {
1423 complete_all(&commit->flip_done);
1424 continue;
1425 }
1426
1427 /* Legacy cursor updates are fully unsynced. */
1428 if (state->legacy_cursor_update) {
1429 complete_all(&commit->flip_done);
1430 continue;
1431 }
1432
1433 if (!crtc_state->event) {
1434 commit->event = kzalloc(sizeof(*commit->event),
1435 GFP_KERNEL);
1436 if (!commit->event)
1437 return -ENOMEM;
1438
1439 crtc_state->event = commit->event;
1440 }
1441
1442 crtc_state->event->base.completion = &commit->flip_done;
1443 }
1444
1445 return 0;
1446}
1447EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1448
1449
1450static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1451{
1452 struct drm_crtc_commit *commit;
1453 int i = 0;
1454
1455 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1456 /* skip the first entry, that's the current commit */
1457 if (i == 1)
1458 return commit;
1459 i++;
1460 }
1461
1462 return NULL;
1463}
1464
1465/**
1466 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1467 * @state: new modeset state to be committed
1468 *
1469 * This function waits for all preceeding commits that touch the same CRTC as
1470 * @state to both be committed to the hardware (as signalled by
1471 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1472 * by calling drm_crtc_vblank_send_event on the event member of
1473 * &drm_crtc_state).
1474 *
1475 * This is part of the atomic helper support for nonblocking commits, see
1476 * drm_atomic_helper_setup_commit() for an overview.
1477 */
1478void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *state)
1479{
1480 struct drm_crtc *crtc;
1481 struct drm_crtc_state *crtc_state;
1482 struct drm_crtc_commit *commit;
1483 int i;
1484 long ret;
1485
1486 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1487 spin_lock(&crtc->commit_lock);
1488 commit = preceeding_commit(crtc);
1489 if (commit)
1490 drm_crtc_commit_get(commit);
1491 spin_unlock(&crtc->commit_lock);
1492
1493 if (!commit)
1494 continue;
1495
1496 ret = wait_for_completion_timeout(&commit->hw_done,
1497 10*HZ);
1498 if (ret == 0)
1499 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1500 crtc->base.id, crtc->name);
1501
1502 /* Currently no support for overwriting flips, hence
1503 * stall for previous one to execute completely. */
1504 ret = wait_for_completion_timeout(&commit->flip_done,
1505 10*HZ);
1506 if (ret == 0)
1507 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1508 crtc->base.id, crtc->name);
1509
1510 drm_crtc_commit_put(commit);
1511 }
1512}
1513EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1514
1515/**
1516 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1517 * @state: new modeset state to be committed
1518 *
1519 * This function is used to signal completion of the hardware commit step. After
1520 * this step the driver is not allowed to read or change any permanent software
1521 * or hardware modeset state. The only exception is state protected by other
1522 * means than &drm_modeset_lock locks.
1523 *
1524 * Drivers should try to postpone any expensive or delayed cleanup work after
1525 * this function is called.
1526 *
1527 * This is part of the atomic helper support for nonblocking commits, see
1528 * drm_atomic_helper_setup_commit() for an overview.
1529 */
1530void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *state)
1531{
1532 struct drm_crtc *crtc;
1533 struct drm_crtc_state *crtc_state;
1534 struct drm_crtc_commit *commit;
1535 int i;
1536
1537 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1538 commit = state->crtcs[i].commit;
1539 if (!commit)
1540 continue;
1541
1542 /* backend must have consumed any event by now */
1543 WARN_ON(crtc->state->event);
1544 spin_lock(&crtc->commit_lock);
1545 complete_all(&commit->hw_done);
1546 spin_unlock(&crtc->commit_lock);
1547 }
1548}
1549EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1550
1551/**
1552 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1553 * @state: new modeset state to be committed
1554 *
1555 * This signals completion of the atomic update @state, including any cleanup
1556 * work. If used, it must be called right before calling
1557 * drm_atomic_state_free().
1558 *
1559 * This is part of the atomic helper support for nonblocking commits, see
1560 * drm_atomic_helper_setup_commit() for an overview.
1561 */
1562void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *state)
1563{
1564 struct drm_crtc *crtc;
1565 struct drm_crtc_state *crtc_state;
1566 struct drm_crtc_commit *commit;
1567 int i;
1568 long ret;
1569
1570 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1571 commit = state->crtcs[i].commit;
1572 if (WARN_ON(!commit))
1573 continue;
1574
1575 spin_lock(&crtc->commit_lock);
1576 complete_all(&commit->cleanup_done);
1577 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1578
1579 /* commit_list borrows our reference, need to remove before we
1580 * clean up our drm_atomic_state. But only after it actually
1581 * completed, otherwise subsequent commits won't stall properly. */
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001582 if (try_wait_for_completion(&commit->flip_done))
1583 goto del_commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001584
1585 spin_unlock(&crtc->commit_lock);
1586
1587 /* We must wait for the vblank event to signal our completion
1588 * before releasing our reference, since the vblank work does
1589 * not hold a reference of its own. */
1590 ret = wait_for_completion_timeout(&commit->flip_done,
1591 10*HZ);
1592 if (ret == 0)
1593 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1594 crtc->base.id, crtc->name);
1595
1596 spin_lock(&crtc->commit_lock);
Daniel Vetter7deef7f12016-06-15 12:24:26 +02001597del_commit:
Daniel Vettera095caa2016-06-08 17:15:36 +02001598 list_del(&commit->commit_entry);
1599 spin_unlock(&crtc->commit_lock);
1600 }
1601}
1602EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1603
Daniel Vettere8c833a2014-07-27 18:30:19 +02001604/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001605 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001606 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001607 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001608 *
1609 * This function prepares plane state, specifically framebuffers, for the new
1610 * configuration. If any failure is encountered this function will call
1611 * ->cleanup_fb on any already successfully prepared framebuffer.
1612 *
1613 * Returns:
1614 * 0 on success, negative error code on failure.
1615 */
1616int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1617 struct drm_atomic_state *state)
1618{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001619 struct drm_plane *plane;
1620 struct drm_plane_state *plane_state;
1621 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001622
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001623 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001624 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001625
1626 funcs = plane->helper_private;
1627
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001628 if (funcs->prepare_fb) {
1629 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001630 if (ret)
1631 goto fail;
1632 }
1633 }
1634
1635 return 0;
1636
1637fail:
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001638 for_each_plane_in_state(state, plane, plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001639 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001640
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001641 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001642 continue;
1643
1644 funcs = plane->helper_private;
1645
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001646 if (funcs->cleanup_fb)
1647 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001648
1649 }
1650
1651 return ret;
1652}
1653EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1654
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001655bool plane_crtc_active(struct drm_plane_state *state)
1656{
1657 return state->crtc && state->crtc->state->active;
1658}
1659
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001660/**
1661 * drm_atomic_helper_commit_planes - commit plane state
1662 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001663 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001664 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001665 *
1666 * This function commits the new plane state using the plane and atomic helper
1667 * functions for planes and crtcs. It assumes that the atomic state has already
1668 * been pushed into the relevant object state pointers, since this step can no
1669 * longer fail.
1670 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001671 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001672 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001673 *
1674 * Note that this function does all plane updates across all CRTCs in one step.
1675 * If the hardware can't support this approach look at
1676 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001677 *
1678 * Plane parameters can be updated by applications while the associated CRTC is
1679 * disabled. The DRM/KMS core will store the parameters in the plane state,
1680 * which will be available to the driver when the CRTC is turned on. As a result
1681 * most drivers don't need to be immediately notified of plane updates for a
1682 * disabled CRTC.
1683 *
1684 * Unless otherwise needed, drivers are advised to set the @active_only
1685 * parameters to true in order not to receive plane update notifications related
1686 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1687 * driver code when the driver and/or hardware can't or just don't need to deal
1688 * with updates on disabled CRTCs, for example when supporting runtime PM.
1689 *
1690 * The drm_atomic_helper_commit() default implementation only sets @active_only
1691 * to false to most closely match the behaviour of the legacy helpers. This should
1692 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001693 */
1694void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001695 struct drm_atomic_state *old_state,
1696 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001697{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001698 struct drm_crtc *crtc;
1699 struct drm_crtc_state *old_crtc_state;
1700 struct drm_plane *plane;
1701 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001702 int i;
1703
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001704 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001705 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001706
1707 funcs = crtc->helper_private;
1708
1709 if (!funcs || !funcs->atomic_begin)
1710 continue;
1711
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001712 if (active_only && !crtc->state->active)
1713 continue;
1714
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001715 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001716 }
1717
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001718 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001719 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001720 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001721
1722 funcs = plane->helper_private;
1723
Thierry Reding3cad4b62014-11-25 13:05:12 +01001724 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001725 continue;
1726
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001727 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1728
1729 if (active_only) {
1730 /*
1731 * Skip planes related to inactive CRTCs. If the plane
1732 * is enabled use the state of the current CRTC. If the
1733 * plane is being disabled use the state of the old
1734 * CRTC to avoid skipping planes being disabled on an
1735 * active CRTC.
1736 */
1737 if (!disabling && !plane_crtc_active(plane->state))
1738 continue;
1739 if (disabling && !plane_crtc_active(old_plane_state))
1740 continue;
1741 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001742
Thierry Reding407b8bd2014-11-20 12:05:50 +01001743 /*
1744 * Special-case disabling the plane if drivers support it.
1745 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001746 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001747 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001748 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001749 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001750 }
1751
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001752 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001753 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001754
1755 funcs = crtc->helper_private;
1756
1757 if (!funcs || !funcs->atomic_flush)
1758 continue;
1759
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001760 if (active_only && !crtc->state->active)
1761 continue;
1762
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001763 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001764 }
1765}
1766EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1767
1768/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001769 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1770 * @old_crtc_state: atomic state object with the old crtc state
1771 *
1772 * This function commits the new plane state using the plane and atomic helper
1773 * functions for planes on the specific crtc. It assumes that the atomic state
1774 * has already been pushed into the relevant object state pointers, since this
1775 * step can no longer fail.
1776 *
1777 * This function is useful when plane updates should be done crtc-by-crtc
1778 * instead of one global step like drm_atomic_helper_commit_planes() does.
1779 *
1780 * This function can only be savely used when planes are not allowed to move
1781 * between different CRTCs because this function doesn't handle inter-CRTC
1782 * depencies. Callers need to ensure that either no such depencies exist,
1783 * resolve them through ordering of commit calls or through some other means.
1784 */
1785void
1786drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1787{
1788 const struct drm_crtc_helper_funcs *crtc_funcs;
1789 struct drm_crtc *crtc = old_crtc_state->crtc;
1790 struct drm_atomic_state *old_state = old_crtc_state->state;
1791 struct drm_plane *plane;
1792 unsigned plane_mask;
1793
1794 plane_mask = old_crtc_state->plane_mask;
1795 plane_mask |= crtc->state->plane_mask;
1796
1797 crtc_funcs = crtc->helper_private;
1798 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001799 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001800
1801 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1802 struct drm_plane_state *old_plane_state =
1803 drm_atomic_get_existing_plane_state(old_state, plane);
1804 const struct drm_plane_helper_funcs *plane_funcs;
1805
1806 plane_funcs = plane->helper_private;
1807
1808 if (!old_plane_state || !plane_funcs)
1809 continue;
1810
1811 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1812
1813 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1814 plane_funcs->atomic_disable)
1815 plane_funcs->atomic_disable(plane, old_plane_state);
1816 else if (plane->state->crtc ||
1817 drm_atomic_plane_disabling(plane, old_plane_state))
1818 plane_funcs->atomic_update(plane, old_plane_state);
1819 }
1820
1821 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001822 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001823}
1824EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1825
1826/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001827 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1828 * @crtc: CRTC
1829 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1830 *
1831 * Disables all planes associated with the given CRTC. This can be
1832 * used for instance in the CRTC helper disable callback to disable
1833 * all planes before shutting down the display pipeline.
1834 *
1835 * If the atomic-parameter is set the function calls the CRTC's
1836 * atomic_begin hook before and atomic_flush hook after disabling the
1837 * planes.
1838 *
1839 * It is a bug to call this function without having implemented the
1840 * ->atomic_disable() plane hook.
1841 */
1842void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1843 bool atomic)
1844{
1845 const struct drm_crtc_helper_funcs *crtc_funcs =
1846 crtc->helper_private;
1847 struct drm_plane *plane;
1848
1849 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1850 crtc_funcs->atomic_begin(crtc, NULL);
1851
1852 drm_for_each_plane(plane, crtc->dev) {
1853 const struct drm_plane_helper_funcs *plane_funcs =
1854 plane->helper_private;
1855
1856 if (plane->state->crtc != crtc || !plane_funcs)
1857 continue;
1858
1859 WARN_ON(!plane_funcs->atomic_disable);
1860 if (plane_funcs->atomic_disable)
1861 plane_funcs->atomic_disable(plane, NULL);
1862 }
1863
1864 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1865 crtc_funcs->atomic_flush(crtc, NULL);
1866}
1867EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1868
1869/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001870 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1871 * @dev: DRM device
1872 * @old_state: atomic state object with old state structures
1873 *
1874 * This function cleans up plane state, specifically framebuffers, from the old
1875 * configuration. Hence the old configuration must be perserved in @old_state to
1876 * be able to call this function.
1877 *
1878 * This function must also be called on the new state when the atomic update
1879 * fails at any point after calling drm_atomic_helper_prepare_planes().
1880 */
1881void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1882 struct drm_atomic_state *old_state)
1883{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001884 struct drm_plane *plane;
1885 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001886 int i;
1887
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001888 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001889 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001890
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001891 funcs = plane->helper_private;
1892
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001893 if (funcs->cleanup_fb)
1894 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001895 }
1896}
1897EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1898
1899/**
1900 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001901 * @state: atomic state
Daniel Vetter5e84c262016-06-10 00:06:32 +02001902 * @stall: stall for proceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001903 *
1904 * This function stores the atomic state into the current state pointers in all
1905 * driver objects. It should be called after all failing steps have been done
1906 * and succeeded, but before the actual hardware state is committed.
1907 *
1908 * For cleanup and error recovery the current state for all changed objects will
1909 * be swaped into @state.
1910 *
1911 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1912 *
1913 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1914 *
1915 * 2. Do any other steps that might fail.
1916 *
1917 * 3. Put the staged state into the current state pointers with this function.
1918 *
1919 * 4. Actually commit the hardware state.
1920 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001921 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001922 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02001923 *
1924 * @stall must be set when nonblocking commits for this driver directly access
1925 * the ->state pointer of &drm_plane, &drm_crtc or &drm_connector. With the
1926 * current atomic helpers this is almost always the case, since the helpers
1927 * don't pass the right state structures to the callbacks.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001928 */
Daniel Vetter5e84c262016-06-10 00:06:32 +02001929void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1930 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001931{
1932 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02001933 long ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001934 struct drm_connector *connector;
1935 struct drm_connector_state *conn_state;
1936 struct drm_crtc *crtc;
1937 struct drm_crtc_state *crtc_state;
1938 struct drm_plane *plane;
1939 struct drm_plane_state *plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001940 struct drm_crtc_commit *commit;
1941
1942 if (stall) {
1943 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1944 spin_lock(&crtc->commit_lock);
1945 commit = list_first_entry_or_null(&crtc->commit_list,
1946 struct drm_crtc_commit, commit_entry);
1947 if (commit)
1948 drm_crtc_commit_get(commit);
1949 spin_unlock(&crtc->commit_lock);
1950
1951 if (!commit)
1952 continue;
1953
1954 ret = wait_for_completion_timeout(&commit->hw_done,
1955 10*HZ);
1956 if (ret == 0)
1957 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1958 crtc->base.id, crtc->name);
1959 drm_crtc_commit_put(commit);
1960 }
1961 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001962
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001963 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001964 connector->state->state = state;
Daniel Vetter63e83c12016-06-02 00:06:32 +02001965 swap(state->connectors[i].state, connector->state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001966 connector->state->state = NULL;
1967 }
1968
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001969 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001970 crtc->state->state = state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +02001971 swap(state->crtcs[i].state, crtc->state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001972 crtc->state->state = NULL;
Daniel Vettera095caa2016-06-08 17:15:36 +02001973
1974 if (state->crtcs[i].commit) {
1975 spin_lock(&crtc->commit_lock);
1976 list_add(&state->crtcs[i].commit->commit_entry,
1977 &crtc->commit_list);
1978 spin_unlock(&crtc->commit_lock);
1979
1980 state->crtcs[i].commit->event = NULL;
1981 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001982 }
1983
Daniel Vetterbe9174a2016-06-02 00:06:24 +02001984 for_each_plane_in_state(state, plane, plane_state, i) {
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001985 plane->state->state = state;
Daniel Vetterb8b53422016-06-02 00:06:33 +02001986 swap(state->planes[i].state, plane->state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001987 plane->state->state = NULL;
1988 }
1989}
1990EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001991
1992/**
1993 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1994 * @plane: plane object to update
1995 * @crtc: owning CRTC of owning plane
1996 * @fb: framebuffer to flip onto plane
1997 * @crtc_x: x offset of primary plane on crtc
1998 * @crtc_y: y offset of primary plane on crtc
1999 * @crtc_w: width of primary plane rectangle on crtc
2000 * @crtc_h: height of primary plane rectangle on crtc
2001 * @src_x: x offset of @fb for panning
2002 * @src_y: y offset of @fb for panning
2003 * @src_w: width of source rectangle in @fb
2004 * @src_h: height of source rectangle in @fb
2005 *
2006 * Provides a default plane update handler using the atomic driver interface.
2007 *
2008 * RETURNS:
2009 * Zero on success, error code on failure
2010 */
2011int drm_atomic_helper_update_plane(struct drm_plane *plane,
2012 struct drm_crtc *crtc,
2013 struct drm_framebuffer *fb,
2014 int crtc_x, int crtc_y,
2015 unsigned int crtc_w, unsigned int crtc_h,
2016 uint32_t src_x, uint32_t src_y,
2017 uint32_t src_w, uint32_t src_h)
2018{
2019 struct drm_atomic_state *state;
2020 struct drm_plane_state *plane_state;
2021 int ret = 0;
2022
2023 state = drm_atomic_state_alloc(plane->dev);
2024 if (!state)
2025 return -ENOMEM;
2026
2027 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2028retry:
2029 plane_state = drm_atomic_get_plane_state(state, plane);
2030 if (IS_ERR(plane_state)) {
2031 ret = PTR_ERR(plane_state);
2032 goto fail;
2033 }
2034
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002035 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002036 if (ret != 0)
2037 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002038 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002039 plane_state->crtc_x = crtc_x;
2040 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002041 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002042 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002043 plane_state->src_x = src_x;
2044 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002045 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002046 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002047
Daniel Vetter3671c582015-05-04 15:40:52 +02002048 if (plane == crtc->cursor)
2049 state->legacy_cursor_update = true;
2050
Daniel Vetter042652e2014-07-27 13:46:52 +02002051 ret = drm_atomic_commit(state);
2052 if (ret != 0)
2053 goto fail;
2054
2055 /* Driver takes ownership of state on successful commit. */
2056 return 0;
2057fail:
2058 if (ret == -EDEADLK)
2059 goto backoff;
2060
2061 drm_atomic_state_free(state);
2062
2063 return ret;
2064backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002065 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002066 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002067
2068 /*
2069 * Someone might have exchanged the framebuffer while we dropped locks
2070 * in the backoff code. We need to fix up the fb refcount tracking the
2071 * core does for us.
2072 */
2073 plane->old_fb = plane->fb;
2074
2075 goto retry;
2076}
2077EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2078
2079/**
2080 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2081 * @plane: plane to disable
2082 *
2083 * Provides a default plane disable handler using the atomic driver interface.
2084 *
2085 * RETURNS:
2086 * Zero on success, error code on failure
2087 */
2088int drm_atomic_helper_disable_plane(struct drm_plane *plane)
2089{
2090 struct drm_atomic_state *state;
2091 struct drm_plane_state *plane_state;
2092 int ret = 0;
2093
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08002094 /*
2095 * FIXME: Without plane->crtc set we can't get at the implicit legacy
2096 * acquire context. The real fix will be to wire the acquire ctx through
2097 * everywhere we need it, but meanwhile prevent chaos by just skipping
2098 * this noop. The critical case is the cursor ioctls which a) only grab
2099 * crtc/cursor-plane locks (so we need the crtc to get at the right
2100 * acquire context) and b) can try to disable the plane multiple times.
2101 */
2102 if (!plane->crtc)
2103 return 0;
2104
Daniel Vetter042652e2014-07-27 13:46:52 +02002105 state = drm_atomic_state_alloc(plane->dev);
2106 if (!state)
2107 return -ENOMEM;
2108
2109 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
2110retry:
2111 plane_state = drm_atomic_get_plane_state(state, plane);
2112 if (IS_ERR(plane_state)) {
2113 ret = PTR_ERR(plane_state);
2114 goto fail;
2115 }
2116
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002117 if (plane_state->crtc && (plane == plane->crtc->cursor))
2118 plane_state->state->legacy_cursor_update = true;
2119
Rob Clarkbbb1e522015-08-25 15:35:58 -04002120 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002121 if (ret != 0)
2122 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002123
Daniel Vetter042652e2014-07-27 13:46:52 +02002124 ret = drm_atomic_commit(state);
2125 if (ret != 0)
2126 goto fail;
2127
2128 /* Driver takes ownership of state on successful commit. */
2129 return 0;
2130fail:
2131 if (ret == -EDEADLK)
2132 goto backoff;
2133
2134 drm_atomic_state_free(state);
2135
2136 return ret;
2137backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002138 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002139 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002140
2141 /*
2142 * Someone might have exchanged the framebuffer while we dropped locks
2143 * in the backoff code. We need to fix up the fb refcount tracking the
2144 * core does for us.
2145 */
2146 plane->old_fb = plane->fb;
2147
2148 goto retry;
2149}
2150EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2151
Rob Clarkbbb1e522015-08-25 15:35:58 -04002152/* just used from fb-helper and atomic-helper: */
2153int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2154 struct drm_plane_state *plane_state)
2155{
2156 int ret;
2157
2158 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2159 if (ret != 0)
2160 return ret;
2161
2162 drm_atomic_set_fb_for_plane(plane_state, NULL);
2163 plane_state->crtc_x = 0;
2164 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002165 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002166 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002167 plane_state->src_x = 0;
2168 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002169 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002170 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002171
Rob Clarkbbb1e522015-08-25 15:35:58 -04002172 return 0;
2173}
2174
Daniel Vetter042652e2014-07-27 13:46:52 +02002175static int update_output_state(struct drm_atomic_state *state,
2176 struct drm_mode_set *set)
2177{
2178 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002179 struct drm_crtc *crtc;
2180 struct drm_crtc_state *crtc_state;
2181 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02002182 struct drm_connector_state *conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002183 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002184
2185 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2186 state->acquire_ctx);
2187 if (ret)
2188 return ret;
2189
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002190 /* First disable all connectors on the target crtc. */
2191 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2192 if (ret)
2193 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002194
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002195 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002196 if (conn_state->crtc == set->crtc) {
2197 ret = drm_atomic_set_crtc_for_connector(conn_state,
2198 NULL);
2199 if (ret)
2200 return ret;
2201 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002202 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002203
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002204 /* Then set all connectors from set->connectors on the target crtc */
2205 for (i = 0; i < set->num_connectors; i++) {
2206 conn_state = drm_atomic_get_connector_state(state,
2207 set->connectors[i]);
2208 if (IS_ERR(conn_state))
2209 return PTR_ERR(conn_state);
2210
2211 ret = drm_atomic_set_crtc_for_connector(conn_state,
2212 set->crtc);
2213 if (ret)
2214 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002215 }
2216
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002217 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002218 /* Don't update ->enable for the CRTC in the set_config request,
2219 * since a mismatch would indicate a bug in the upper layers.
2220 * The actual modeset code later on will catch any
2221 * inconsistencies here. */
2222 if (crtc == set->crtc)
2223 continue;
2224
Maarten Lankhorst14de6c42016-01-04 12:53:20 +01002225 if (!crtc_state->connector_mask) {
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002226 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
2227 NULL);
2228 if (ret < 0)
2229 return ret;
2230
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02002231 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002232 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002233 }
2234
2235 return 0;
2236}
2237
2238/**
2239 * drm_atomic_helper_set_config - set a new config from userspace
2240 * @set: mode set configuration
2241 *
2242 * Provides a default crtc set_config handler using the atomic driver interface.
2243 *
2244 * Returns:
2245 * Returns 0 on success, negative errno numbers on failure.
2246 */
2247int drm_atomic_helper_set_config(struct drm_mode_set *set)
2248{
2249 struct drm_atomic_state *state;
2250 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002251 int ret = 0;
2252
2253 state = drm_atomic_state_alloc(crtc->dev);
2254 if (!state)
2255 return -ENOMEM;
2256
Maarten Lankhorst40616a22016-03-03 10:17:39 +01002257 state->legacy_set_config = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02002258 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2259retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04002260 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002261 if (ret != 0)
2262 goto fail;
2263
Daniel Vetter042652e2014-07-27 13:46:52 +02002264 ret = drm_atomic_commit(state);
2265 if (ret != 0)
2266 goto fail;
2267
2268 /* Driver takes ownership of state on successful commit. */
2269 return 0;
2270fail:
2271 if (ret == -EDEADLK)
2272 goto backoff;
2273
2274 drm_atomic_state_free(state);
2275
2276 return ret;
2277backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002278 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002279 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002280
2281 /*
2282 * Someone might have exchanged the framebuffer while we dropped locks
2283 * in the backoff code. We need to fix up the fb refcount tracking the
2284 * core does for us.
2285 */
2286 crtc->primary->old_fb = crtc->primary->fb;
2287
2288 goto retry;
2289}
2290EXPORT_SYMBOL(drm_atomic_helper_set_config);
2291
Rob Clarkbbb1e522015-08-25 15:35:58 -04002292/* just used from fb-helper and atomic-helper: */
2293int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2294 struct drm_atomic_state *state)
2295{
2296 struct drm_crtc_state *crtc_state;
2297 struct drm_plane_state *primary_state;
2298 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002299 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002300 int ret;
2301
2302 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2303 if (IS_ERR(crtc_state))
2304 return PTR_ERR(crtc_state);
2305
2306 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2307 if (IS_ERR(primary_state))
2308 return PTR_ERR(primary_state);
2309
2310 if (!set->mode) {
2311 WARN_ON(set->fb);
2312 WARN_ON(set->num_connectors);
2313
2314 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2315 if (ret != 0)
2316 return ret;
2317
2318 crtc_state->active = false;
2319
2320 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2321 if (ret != 0)
2322 return ret;
2323
2324 drm_atomic_set_fb_for_plane(primary_state, NULL);
2325
2326 goto commit;
2327 }
2328
2329 WARN_ON(!set->fb);
2330 WARN_ON(!set->num_connectors);
2331
2332 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2333 if (ret != 0)
2334 return ret;
2335
2336 crtc_state->active = true;
2337
2338 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2339 if (ret != 0)
2340 return ret;
2341
Ville Syrjälä83926112015-11-16 17:02:34 +02002342 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2343
Rob Clarkbbb1e522015-08-25 15:35:58 -04002344 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2345 primary_state->crtc_x = 0;
2346 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02002347 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002348 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002349 primary_state->src_x = set->x << 16;
2350 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002351 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02002352 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002353 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002354 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02002355 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002356 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03002357 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04002358
2359commit:
2360 ret = update_output_state(state, set);
2361 if (ret)
2362 return ret;
2363
2364 return 0;
2365}
2366
Daniel Vetter042652e2014-07-27 13:46:52 +02002367/**
Thierry Reding14942762015-12-02 17:50:04 +01002368 * drm_atomic_helper_disable_all - disable all currently active outputs
2369 * @dev: DRM device
2370 * @ctx: lock acquisition context
2371 *
2372 * Loops through all connectors, finding those that aren't turned off and then
2373 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2374 * that they are connected to.
2375 *
2376 * This is used for example in suspend/resume to disable all currently active
2377 * functions when suspending.
2378 *
2379 * Note that if callers haven't already acquired all modeset locks this might
2380 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2381 *
2382 * Returns:
2383 * 0 on success or a negative error code on failure.
2384 *
2385 * See also:
2386 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2387 */
2388int drm_atomic_helper_disable_all(struct drm_device *dev,
2389 struct drm_modeset_acquire_ctx *ctx)
2390{
2391 struct drm_atomic_state *state;
2392 struct drm_connector *conn;
2393 int err;
2394
2395 state = drm_atomic_state_alloc(dev);
2396 if (!state)
2397 return -ENOMEM;
2398
2399 state->acquire_ctx = ctx;
2400
2401 drm_for_each_connector(conn, dev) {
2402 struct drm_crtc *crtc = conn->state->crtc;
2403 struct drm_crtc_state *crtc_state;
2404
2405 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
2406 continue;
2407
2408 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2409 if (IS_ERR(crtc_state)) {
2410 err = PTR_ERR(crtc_state);
2411 goto free;
2412 }
2413
2414 crtc_state->active = false;
2415 }
2416
2417 err = drm_atomic_commit(state);
2418
2419free:
2420 if (err < 0)
2421 drm_atomic_state_free(state);
2422
2423 return err;
2424}
2425EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2426
2427/**
2428 * drm_atomic_helper_suspend - subsystem-level suspend helper
2429 * @dev: DRM device
2430 *
2431 * Duplicates the current atomic state, disables all active outputs and then
2432 * returns a pointer to the original atomic state to the caller. Drivers can
2433 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2434 * restore the output configuration that was active at the time the system
2435 * entered suspend.
2436 *
2437 * Note that it is potentially unsafe to use this. The atomic state object
2438 * returned by this function is assumed to be persistent. Drivers must ensure
2439 * that this holds true. Before calling this function, drivers must make sure
2440 * to suspend fbdev emulation so that nothing can be using the device.
2441 *
2442 * Returns:
2443 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2444 * encoded error code on failure. Drivers should store the returned atomic
2445 * state object and pass it to the drm_atomic_helper_resume() helper upon
2446 * resume.
2447 *
2448 * See also:
2449 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2450 * drm_atomic_helper_resume()
2451 */
2452struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2453{
2454 struct drm_modeset_acquire_ctx ctx;
2455 struct drm_atomic_state *state;
2456 int err;
2457
2458 drm_modeset_acquire_init(&ctx, 0);
2459
2460retry:
2461 err = drm_modeset_lock_all_ctx(dev, &ctx);
2462 if (err < 0) {
2463 state = ERR_PTR(err);
2464 goto unlock;
2465 }
2466
2467 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2468 if (IS_ERR(state))
2469 goto unlock;
2470
2471 err = drm_atomic_helper_disable_all(dev, &ctx);
2472 if (err < 0) {
2473 drm_atomic_state_free(state);
2474 state = ERR_PTR(err);
2475 goto unlock;
2476 }
2477
2478unlock:
2479 if (PTR_ERR(state) == -EDEADLK) {
2480 drm_modeset_backoff(&ctx);
2481 goto retry;
2482 }
2483
2484 drm_modeset_drop_locks(&ctx);
2485 drm_modeset_acquire_fini(&ctx);
2486 return state;
2487}
2488EXPORT_SYMBOL(drm_atomic_helper_suspend);
2489
2490/**
2491 * drm_atomic_helper_resume - subsystem-level resume helper
2492 * @dev: DRM device
2493 * @state: atomic state to resume to
2494 *
2495 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2496 * grabs all modeset locks and commits the atomic state object. This can be
2497 * used in conjunction with the drm_atomic_helper_suspend() helper to
2498 * implement suspend/resume for drivers that support atomic mode-setting.
2499 *
2500 * Returns:
2501 * 0 on success or a negative error code on failure.
2502 *
2503 * See also:
2504 * drm_atomic_helper_suspend()
2505 */
2506int drm_atomic_helper_resume(struct drm_device *dev,
2507 struct drm_atomic_state *state)
2508{
2509 struct drm_mode_config *config = &dev->mode_config;
2510 int err;
2511
2512 drm_mode_config_reset(dev);
2513 drm_modeset_lock_all(dev);
2514 state->acquire_ctx = config->acquire_ctx;
2515 err = drm_atomic_commit(state);
2516 drm_modeset_unlock_all(dev);
2517
2518 return err;
2519}
2520EXPORT_SYMBOL(drm_atomic_helper_resume);
2521
2522/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002523 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002524 * @crtc: DRM crtc
2525 * @property: DRM property
2526 * @val: value of property
2527 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002528 * Provides a default crtc set_property handler using the atomic driver
2529 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002530 *
2531 * RETURNS:
2532 * Zero on success, error code on failure
2533 */
2534int
2535drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2536 struct drm_property *property,
2537 uint64_t val)
2538{
2539 struct drm_atomic_state *state;
2540 struct drm_crtc_state *crtc_state;
2541 int ret = 0;
2542
2543 state = drm_atomic_state_alloc(crtc->dev);
2544 if (!state)
2545 return -ENOMEM;
2546
2547 /* ->set_property is always called with all locks held. */
2548 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2549retry:
2550 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2551 if (IS_ERR(crtc_state)) {
2552 ret = PTR_ERR(crtc_state);
2553 goto fail;
2554 }
2555
Rob Clark40ecc692014-12-18 16:01:46 -05002556 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2557 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002558 if (ret)
2559 goto fail;
2560
2561 ret = drm_atomic_commit(state);
2562 if (ret != 0)
2563 goto fail;
2564
2565 /* Driver takes ownership of state on successful commit. */
2566 return 0;
2567fail:
2568 if (ret == -EDEADLK)
2569 goto backoff;
2570
2571 drm_atomic_state_free(state);
2572
2573 return ret;
2574backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002575 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002576 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002577
2578 goto retry;
2579}
2580EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2581
2582/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002583 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002584 * @plane: DRM plane
2585 * @property: DRM property
2586 * @val: value of property
2587 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002588 * Provides a default plane set_property handler using the atomic driver
2589 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002590 *
2591 * RETURNS:
2592 * Zero on success, error code on failure
2593 */
2594int
2595drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2596 struct drm_property *property,
2597 uint64_t val)
2598{
2599 struct drm_atomic_state *state;
2600 struct drm_plane_state *plane_state;
2601 int ret = 0;
2602
2603 state = drm_atomic_state_alloc(plane->dev);
2604 if (!state)
2605 return -ENOMEM;
2606
2607 /* ->set_property is always called with all locks held. */
2608 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2609retry:
2610 plane_state = drm_atomic_get_plane_state(state, plane);
2611 if (IS_ERR(plane_state)) {
2612 ret = PTR_ERR(plane_state);
2613 goto fail;
2614 }
2615
Rob Clark40ecc692014-12-18 16:01:46 -05002616 ret = drm_atomic_plane_set_property(plane, plane_state,
2617 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002618 if (ret)
2619 goto fail;
2620
2621 ret = drm_atomic_commit(state);
2622 if (ret != 0)
2623 goto fail;
2624
2625 /* Driver takes ownership of state on successful commit. */
2626 return 0;
2627fail:
2628 if (ret == -EDEADLK)
2629 goto backoff;
2630
2631 drm_atomic_state_free(state);
2632
2633 return ret;
2634backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002635 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002636 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002637
2638 goto retry;
2639}
2640EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2641
2642/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002643 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002644 * @connector: DRM connector
2645 * @property: DRM property
2646 * @val: value of property
2647 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002648 * Provides a default connector set_property handler using the atomic driver
2649 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002650 *
2651 * RETURNS:
2652 * Zero on success, error code on failure
2653 */
2654int
2655drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2656 struct drm_property *property,
2657 uint64_t val)
2658{
2659 struct drm_atomic_state *state;
2660 struct drm_connector_state *connector_state;
2661 int ret = 0;
2662
2663 state = drm_atomic_state_alloc(connector->dev);
2664 if (!state)
2665 return -ENOMEM;
2666
2667 /* ->set_property is always called with all locks held. */
2668 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2669retry:
2670 connector_state = drm_atomic_get_connector_state(state, connector);
2671 if (IS_ERR(connector_state)) {
2672 ret = PTR_ERR(connector_state);
2673 goto fail;
2674 }
2675
Rob Clark40ecc692014-12-18 16:01:46 -05002676 ret = drm_atomic_connector_set_property(connector, connector_state,
2677 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002678 if (ret)
2679 goto fail;
2680
2681 ret = drm_atomic_commit(state);
2682 if (ret != 0)
2683 goto fail;
2684
2685 /* Driver takes ownership of state on successful commit. */
2686 return 0;
2687fail:
2688 if (ret == -EDEADLK)
2689 goto backoff;
2690
2691 drm_atomic_state_free(state);
2692
2693 return ret;
2694backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002695 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002696 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002697
2698 goto retry;
2699}
2700EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002701
2702/**
2703 * drm_atomic_helper_page_flip - execute a legacy page flip
2704 * @crtc: DRM crtc
2705 * @fb: DRM framebuffer
2706 * @event: optional DRM event to signal upon completion
2707 * @flags: flip flags for non-vblank sync'ed updates
2708 *
2709 * Provides a default page flip implementation using the atomic driver interface.
2710 *
2711 * Note that for now so called async page flips (i.e. updates which are not
2712 * synchronized to vblank) are not supported, since the atomic interfaces have
2713 * no provisions for this yet.
2714 *
2715 * Returns:
2716 * Returns 0 on success, negative errno numbers on failure.
2717 */
2718int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2719 struct drm_framebuffer *fb,
2720 struct drm_pending_vblank_event *event,
2721 uint32_t flags)
2722{
2723 struct drm_plane *plane = crtc->primary;
2724 struct drm_atomic_state *state;
2725 struct drm_plane_state *plane_state;
2726 struct drm_crtc_state *crtc_state;
2727 int ret = 0;
2728
2729 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2730 return -EINVAL;
2731
2732 state = drm_atomic_state_alloc(plane->dev);
2733 if (!state)
2734 return -ENOMEM;
2735
2736 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2737retry:
2738 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2739 if (IS_ERR(crtc_state)) {
2740 ret = PTR_ERR(crtc_state);
2741 goto fail;
2742 }
2743 crtc_state->event = event;
2744
2745 plane_state = drm_atomic_get_plane_state(state, plane);
2746 if (IS_ERR(plane_state)) {
2747 ret = PTR_ERR(plane_state);
2748 goto fail;
2749 }
2750
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002751 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002752 if (ret != 0)
2753 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002754 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002755
Daniel Vetter4cba6852015-12-08 09:49:20 +01002756 /* Make sure we don't accidentally do a full modeset. */
2757 state->allow_modeset = false;
2758 if (!crtc_state->active) {
2759 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2760 crtc->base.id);
2761 ret = -EINVAL;
2762 goto fail;
2763 }
2764
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002765 ret = drm_atomic_nonblocking_commit(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002766 if (ret != 0)
2767 goto fail;
2768
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002769 /* Driver takes ownership of state on successful commit. */
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002770 return 0;
2771fail:
2772 if (ret == -EDEADLK)
2773 goto backoff;
2774
2775 drm_atomic_state_free(state);
2776
2777 return ret;
2778backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002779 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002780 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002781
2782 /*
2783 * Someone might have exchanged the framebuffer while we dropped locks
2784 * in the backoff code. We need to fix up the fb refcount tracking the
2785 * core does for us.
2786 */
2787 plane->old_fb = plane->fb;
2788
2789 goto retry;
2790}
2791EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002792
2793/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002794 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2795 * @connector: affected connector
2796 * @mode: DPMS mode
2797 *
2798 * This is the main helper function provided by the atomic helper framework for
2799 * implementing the legacy DPMS connector interface. It computes the new desired
2800 * ->active state for the corresponding CRTC (if the connector is enabled) and
Daniel Vetter2e7a5702016-06-01 23:40:36 +02002801 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002802 *
2803 * Returns:
2804 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002805 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002806int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2807 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002808{
2809 struct drm_mode_config *config = &connector->dev->mode_config;
2810 struct drm_atomic_state *state;
2811 struct drm_crtc_state *crtc_state;
2812 struct drm_crtc *crtc;
2813 struct drm_connector *tmp_connector;
2814 int ret;
2815 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002816 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002817
2818 if (mode != DRM_MODE_DPMS_ON)
2819 mode = DRM_MODE_DPMS_OFF;
2820
2821 connector->dpms = mode;
2822 crtc = connector->state->crtc;
2823
2824 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002825 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002826
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002827 state = drm_atomic_state_alloc(connector->dev);
2828 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002829 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002830
2831 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2832retry:
2833 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002834 if (IS_ERR(crtc_state)) {
2835 ret = PTR_ERR(crtc_state);
2836 goto fail;
2837 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002838
2839 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2840
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002841 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002842 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002843 continue;
2844
John Hunter0388df02015-03-17 15:30:28 +08002845 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002846 active = true;
2847 break;
2848 }
2849 }
2850 crtc_state->active = active;
2851
2852 ret = drm_atomic_commit(state);
2853 if (ret != 0)
2854 goto fail;
2855
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002856 /* Driver takes ownership of state on successful commit. */
2857 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002858fail:
2859 if (ret == -EDEADLK)
2860 goto backoff;
2861
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002862 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002863 drm_atomic_state_free(state);
2864
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002865 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002866backoff:
2867 drm_atomic_state_clear(state);
2868 drm_atomic_legacy_backoff(state);
2869
2870 goto retry;
2871}
2872EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2873
2874/**
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02002875 * drm_atomic_helper_best_encoder - Helper for &drm_connector_helper_funcs
2876 * ->best_encoder callback
2877 * @connector: Connector control structure
2878 *
2879 * This is a &drm_connector_helper_funcs ->best_encoder callback helper for
2880 * connectors that support exactly 1 encoder, statically determined at driver
2881 * init time.
2882 */
2883struct drm_encoder *
2884drm_atomic_helper_best_encoder(struct drm_connector *connector)
2885{
2886 WARN_ON(connector->encoder_ids[1]);
2887 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
2888}
2889EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
2890
2891/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002892 * DOC: atomic state reset and initialization
2893 *
2894 * Both the drm core and the atomic helpers assume that there is always the full
2895 * and correct atomic software state for all connectors, CRTCs and planes
2896 * available. Which is a bit a problem on driver load and also after system
2897 * suspend. One way to solve this is to have a hardware state read-out
2898 * infrastructure which reconstructs the full software state (e.g. the i915
2899 * driver).
2900 *
2901 * The simpler solution is to just reset the software state to everything off,
2902 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2903 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01002904 *
2905 * On the upside the precise state tracking of atomic simplifies system suspend
2906 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2907 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2908 * For other drivers the building blocks are split out, see the documentation
2909 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002910 */
2911
2912/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002913 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2914 * @crtc: drm CRTC
2915 *
2916 * Resets the atomic state for @crtc by freeing the state pointer (which might
2917 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2918 */
2919void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2920{
Daniel Vetterb0b55112016-04-22 22:10:29 +02002921 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02002922 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02002923
Daniel Vetterd4617012014-11-03 15:56:43 +01002924 kfree(crtc->state);
2925 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002926
2927 if (crtc->state)
2928 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002929}
2930EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2931
2932/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002933 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2934 * @crtc: CRTC object
2935 * @state: atomic CRTC state
2936 *
2937 * Copies atomic state from a CRTC's current state and resets inferred values.
2938 * This is useful for drivers that subclass the CRTC state.
2939 */
2940void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2941 struct drm_crtc_state *state)
2942{
2943 memcpy(state, crtc->state, sizeof(*state));
2944
Daniel Stone99cf4a22015-05-25 19:11:51 +01002945 if (state->mode_blob)
2946 drm_property_reference_blob(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00002947 if (state->degamma_lut)
2948 drm_property_reference_blob(state->degamma_lut);
2949 if (state->ctm)
2950 drm_property_reference_blob(state->ctm);
2951 if (state->gamma_lut)
2952 drm_property_reference_blob(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01002953 state->mode_changed = false;
2954 state->active_changed = false;
2955 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002956 state->connectors_changed = false;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00002957 state->color_mgmt_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002958 state->event = NULL;
2959}
2960EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2961
2962/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002963 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2964 * @crtc: drm CRTC
2965 *
2966 * Default CRTC state duplicate hook for drivers which don't have their own
2967 * subclassed CRTC state structure.
2968 */
2969struct drm_crtc_state *
2970drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2971{
2972 struct drm_crtc_state *state;
2973
2974 if (WARN_ON(!crtc->state))
2975 return NULL;
2976
Thierry Redingf5e78402015-01-28 14:54:32 +01002977 state = kmalloc(sizeof(*state), GFP_KERNEL);
2978 if (state)
2979 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002980
2981 return state;
2982}
2983EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2984
2985/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002986 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
Thierry Redingf5e78402015-01-28 14:54:32 +01002987 * @state: CRTC state object to release
2988 *
2989 * Releases all resources stored in the CRTC state without actually freeing
2990 * the memory of the CRTC state. This is useful for drivers that subclass the
2991 * CRTC state.
2992 */
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02002993void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01002994{
Markus Elfring5f911902015-11-06 12:03:46 +01002995 drm_property_unreference_blob(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00002996 drm_property_unreference_blob(state->degamma_lut);
2997 drm_property_unreference_blob(state->ctm);
2998 drm_property_unreference_blob(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01002999}
3000EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3001
3002/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003003 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3004 * @crtc: drm CRTC
3005 * @state: CRTC state object to release
3006 *
3007 * Default CRTC state destroy hook for drivers which don't have their own
3008 * subclassed CRTC state structure.
3009 */
3010void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3011 struct drm_crtc_state *state)
3012{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003013 __drm_atomic_helper_crtc_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003014 kfree(state);
3015}
3016EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3017
3018/**
3019 * drm_atomic_helper_plane_reset - default ->reset hook for planes
3020 * @plane: drm plane
3021 *
3022 * Resets the atomic state for @plane by freeing the state pointer (which might
3023 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3024 */
3025void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3026{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003027 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +02003028 __drm_atomic_helper_plane_destroy_state(plane->state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003029
Daniel Vetterd4617012014-11-03 15:56:43 +01003030 kfree(plane->state);
3031 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003032
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003033 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003034 plane->state->plane = plane;
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003035 plane->state->rotation = BIT(DRM_ROTATE_0);
3036 }
Daniel Vetterd4617012014-11-03 15:56:43 +01003037}
3038EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3039
3040/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003041 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3042 * @plane: plane object
3043 * @state: atomic plane state
3044 *
3045 * Copies atomic state from a plane's current state. This is useful for
3046 * drivers that subclass the plane state.
3047 */
3048void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3049 struct drm_plane_state *state)
3050{
3051 memcpy(state, plane->state, sizeof(*state));
3052
3053 if (state->fb)
3054 drm_framebuffer_reference(state->fb);
3055}
3056EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3057
3058/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003059 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3060 * @plane: drm plane
3061 *
3062 * Default plane state duplicate hook for drivers which don't have their own
3063 * subclassed plane state structure.
3064 */
3065struct drm_plane_state *
3066drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3067{
Daniel Vetter321ebf02014-11-04 22:57:27 +01003068 struct drm_plane_state *state;
3069
Daniel Vetterd4617012014-11-03 15:56:43 +01003070 if (WARN_ON(!plane->state))
3071 return NULL;
3072
Thierry Redingf5e78402015-01-28 14:54:32 +01003073 state = kmalloc(sizeof(*state), GFP_KERNEL);
3074 if (state)
3075 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003076
3077 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003078}
3079EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3080
3081/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003082 * __drm_atomic_helper_plane_destroy_state - release plane state
Thierry Redingf5e78402015-01-28 14:54:32 +01003083 * @state: plane state object to release
3084 *
3085 * Releases all resources stored in the plane state without actually freeing
3086 * the memory of the plane state. This is useful for drivers that subclass the
3087 * plane state.
3088 */
Daniel Vetter2f701692016-05-09 16:34:10 +02003089void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003090{
3091 if (state->fb)
3092 drm_framebuffer_unreference(state->fb);
3093}
3094EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3095
3096/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003097 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3098 * @plane: drm plane
3099 * @state: plane state object to release
3100 *
3101 * Default plane state destroy hook for drivers which don't have their own
3102 * subclassed plane state structure.
3103 */
3104void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01003105 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01003106{
Daniel Vetter2f701692016-05-09 16:34:10 +02003107 __drm_atomic_helper_plane_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003108 kfree(state);
3109}
3110EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3111
3112/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003113 * __drm_atomic_helper_connector_reset - reset state on connector
3114 * @connector: drm connector
3115 * @conn_state: connector state to assign
3116 *
3117 * Initializes the newly allocated @conn_state and assigns it to
3118 * #connector ->state, usually required when initializing the drivers
3119 * or when called from the ->reset hook.
3120 *
3121 * This is useful for drivers that subclass the connector state.
3122 */
3123void
3124__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3125 struct drm_connector_state *conn_state)
3126{
3127 if (conn_state)
3128 conn_state->connector = connector;
3129
3130 connector->state = conn_state;
3131}
3132EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3133
3134/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003135 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
3136 * @connector: drm connector
3137 *
3138 * Resets the atomic state for @connector by freeing the state pointer (which
3139 * might be NULL, e.g. at driver load time) and allocating a new empty state
3140 * object.
3141 */
3142void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3143{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003144 struct drm_connector_state *conn_state =
3145 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003146
Daniel Vetterb0b55112016-04-22 22:10:29 +02003147 if (connector->state)
Daniel Vetterfabd9102016-05-09 16:34:11 +02003148 __drm_atomic_helper_connector_destroy_state(connector->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003149
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003150 kfree(connector->state);
3151 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003152}
3153EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3154
3155/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003156 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3157 * @connector: connector object
3158 * @state: atomic connector state
3159 *
3160 * Copies atomic state from a connector's current state. This is useful for
3161 * drivers that subclass the connector state.
3162 */
3163void
3164__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3165 struct drm_connector_state *state)
3166{
3167 memcpy(state, connector->state, sizeof(*state));
Dave Airlied2307de2016-04-27 11:27:39 +10003168 if (state->crtc)
3169 drm_connector_reference(connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003170}
3171EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3172
3173/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003174 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3175 * @connector: drm connector
3176 *
3177 * Default connector state duplicate hook for drivers which don't have their own
3178 * subclassed connector state structure.
3179 */
3180struct drm_connector_state *
3181drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3182{
Thierry Redingf5e78402015-01-28 14:54:32 +01003183 struct drm_connector_state *state;
3184
Daniel Vetterd4617012014-11-03 15:56:43 +01003185 if (WARN_ON(!connector->state))
3186 return NULL;
3187
Thierry Redingf5e78402015-01-28 14:54:32 +01003188 state = kmalloc(sizeof(*state), GFP_KERNEL);
3189 if (state)
3190 __drm_atomic_helper_connector_duplicate_state(connector, state);
3191
3192 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003193}
3194EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3195
3196/**
Thierry Reding397fd772015-09-08 15:00:45 +02003197 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3198 * @dev: DRM device
3199 * @ctx: lock acquisition context
3200 *
3201 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01003202 * duplicating their respective states. This is used for example by suspend/
3203 * resume support code to save the state prior to suspend such that it can
3204 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02003205 *
3206 * Note that this treats atomic state as persistent between save and restore.
3207 * Drivers must make sure that this is possible and won't result in confusion
3208 * or erroneous behaviour.
3209 *
3210 * Note that if callers haven't already acquired all modeset locks this might
3211 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3212 *
3213 * Returns:
3214 * A pointer to the copy of the atomic state object on success or an
3215 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01003216 *
3217 * See also:
3218 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02003219 */
3220struct drm_atomic_state *
3221drm_atomic_helper_duplicate_state(struct drm_device *dev,
3222 struct drm_modeset_acquire_ctx *ctx)
3223{
3224 struct drm_atomic_state *state;
3225 struct drm_connector *conn;
3226 struct drm_plane *plane;
3227 struct drm_crtc *crtc;
3228 int err = 0;
3229
3230 state = drm_atomic_state_alloc(dev);
3231 if (!state)
3232 return ERR_PTR(-ENOMEM);
3233
3234 state->acquire_ctx = ctx;
3235
3236 drm_for_each_crtc(crtc, dev) {
3237 struct drm_crtc_state *crtc_state;
3238
3239 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3240 if (IS_ERR(crtc_state)) {
3241 err = PTR_ERR(crtc_state);
3242 goto free;
3243 }
3244 }
3245
3246 drm_for_each_plane(plane, dev) {
3247 struct drm_plane_state *plane_state;
3248
3249 plane_state = drm_atomic_get_plane_state(state, plane);
3250 if (IS_ERR(plane_state)) {
3251 err = PTR_ERR(plane_state);
3252 goto free;
3253 }
3254 }
3255
3256 drm_for_each_connector(conn, dev) {
3257 struct drm_connector_state *conn_state;
3258
3259 conn_state = drm_atomic_get_connector_state(state, conn);
3260 if (IS_ERR(conn_state)) {
3261 err = PTR_ERR(conn_state);
3262 goto free;
3263 }
3264 }
3265
3266 /* clear the acquire context so that it isn't accidentally reused */
3267 state->acquire_ctx = NULL;
3268
3269free:
3270 if (err < 0) {
3271 drm_atomic_state_free(state);
3272 state = ERR_PTR(err);
3273 }
3274
3275 return state;
3276}
3277EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3278
3279/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003280 * __drm_atomic_helper_connector_destroy_state - release connector state
Thierry Redingf5e78402015-01-28 14:54:32 +01003281 * @state: connector state object to release
3282 *
3283 * Releases all resources stored in the connector state without actually
3284 * freeing the memory of the connector state. This is useful for drivers that
3285 * subclass the connector state.
3286 */
3287void
Daniel Vetterfabd9102016-05-09 16:34:11 +02003288__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003289{
3290 /*
3291 * This is currently a placeholder so that drivers that subclass the
3292 * state will automatically do the right thing if code is ever added
3293 * to this function.
3294 */
Dave Airlied2307de2016-04-27 11:27:39 +10003295 if (state->crtc)
3296 drm_connector_unreference(state->connector);
Thierry Redingf5e78402015-01-28 14:54:32 +01003297}
3298EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3299
3300/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003301 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3302 * @connector: drm connector
3303 * @state: connector state object to release
3304 *
3305 * Default connector state destroy hook for drivers which don't have their own
3306 * subclassed connector state structure.
3307 */
3308void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3309 struct drm_connector_state *state)
3310{
Daniel Vetterfabd9102016-05-09 16:34:11 +02003311 __drm_atomic_helper_connector_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003312 kfree(state);
3313}
3314EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003315
3316/**
3317 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3318 * @crtc: CRTC object
3319 * @red: red correction table
3320 * @green: green correction table
3321 * @blue: green correction table
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003322 * @size: size of the tables
3323 *
3324 * Implements support for legacy gamma correction table for drivers
3325 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3326 * properties.
3327 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003328int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3329 u16 *red, u16 *green, u16 *blue,
3330 uint32_t size)
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003331{
3332 struct drm_device *dev = crtc->dev;
3333 struct drm_mode_config *config = &dev->mode_config;
3334 struct drm_atomic_state *state;
3335 struct drm_crtc_state *crtc_state;
3336 struct drm_property_blob *blob = NULL;
3337 struct drm_color_lut *blob_data;
3338 int i, ret = 0;
3339
3340 state = drm_atomic_state_alloc(crtc->dev);
3341 if (!state)
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003342 return -ENOMEM;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003343
3344 blob = drm_property_create_blob(dev,
3345 sizeof(struct drm_color_lut) * size,
3346 NULL);
Lionel Landwerlin562c5b42016-03-10 12:04:21 +00003347 if (IS_ERR(blob)) {
3348 ret = PTR_ERR(blob);
Lionel Landwerlinc1f415c2016-03-11 12:17:26 +00003349 blob = NULL;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003350 goto fail;
3351 }
3352
3353 /* Prepare GAMMA_LUT with the legacy values. */
3354 blob_data = (struct drm_color_lut *) blob->data;
3355 for (i = 0; i < size; i++) {
3356 blob_data[i].red = red[i];
3357 blob_data[i].green = green[i];
3358 blob_data[i].blue = blue[i];
3359 }
3360
3361 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3362retry:
3363 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3364 if (IS_ERR(crtc_state)) {
3365 ret = PTR_ERR(crtc_state);
3366 goto fail;
3367 }
3368
3369 /* Reset DEGAMMA_LUT and CTM properties. */
3370 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3371 config->degamma_lut_property, 0);
3372 if (ret)
3373 goto fail;
3374
3375 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3376 config->ctm_property, 0);
3377 if (ret)
3378 goto fail;
3379
3380 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3381 config->gamma_lut_property, blob->base.id);
3382 if (ret)
3383 goto fail;
3384
3385 ret = drm_atomic_commit(state);
3386 if (ret)
3387 goto fail;
3388
3389 /* Driver takes ownership of state on successful commit. */
3390
3391 drm_property_unreference_blob(blob);
3392
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003393 return 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003394fail:
3395 if (ret == -EDEADLK)
3396 goto backoff;
3397
3398 drm_atomic_state_free(state);
3399 drm_property_unreference_blob(blob);
3400
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003401 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003402backoff:
3403 drm_atomic_state_clear(state);
3404 drm_atomic_legacy_backoff(state);
3405
3406 goto retry;
3407}
3408EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);