blob: 0ee83efeb94a46da25b7b947704c70ea644bbf72 [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>
Daniel Vetter72fdb40c2018-09-05 15:57:11 +020030#include <drm/drm_atomic_uapi.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010031#include <drm/drm_plane_helper.h>
32#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020033#include <drm/drm_atomic_helper.h>
Brian Starkey935774c2017-03-29 17:42:32 +010034#include <drm/drm_writeback.h>
Chris Wilsonf54d1862016-10-25 13:00:45 +010035#include <linux/dma-fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010036
Jose Abreufaf94a02017-05-25 15:19:16 +010037#include "drm_crtc_helper_internal.h"
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020038#include "drm_crtc_internal.h"
39
Daniel Vetter3150c7d2014-11-06 20:53:29 +010040/**
41 * DOC: overview
42 *
43 * This helper library provides implementations of check and commit functions on
44 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
45 * also provides convenience implementations for the atomic state handling
46 * callbacks for drivers which don't need to subclass the drm core structures to
47 * add their own additional internal state.
48 *
49 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020050 * drm_atomic_helper_check() and for the commit callback with
51 * drm_atomic_helper_commit(). But the individual stages and callbacks are
52 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010053 * together with a driver private modeset implementation.
54 *
55 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020056 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
57 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010058 * various functions to implement set_property callbacks. New drivers must not
59 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010060 *
61 * The atomic helper uses the same function table structures as all other
Daniel Vetterea0dd852016-12-29 21:48:26 +010062 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
63 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
64 * also shares the &struct drm_plane_helper_funcs function table with the plane
Daniel Vetter092d01d2015-12-04 09:45:44 +010065 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010066 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010067static void
68drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010069 struct drm_plane_state *old_plane_state,
Daniel Vetterc2fcd272014-11-05 00:14:14 +010070 struct drm_plane_state *plane_state,
71 struct drm_plane *plane)
72{
73 struct drm_crtc_state *crtc_state;
74
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010075 if (old_plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010076 crtc_state = drm_atomic_get_new_crtc_state(state,
77 old_plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010078
79 if (WARN_ON(!crtc_state))
80 return;
81
82 crtc_state->planes_changed = true;
83 }
84
85 if (plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010086 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010087
88 if (WARN_ON(!crtc_state))
89 return;
90
91 crtc_state->planes_changed = true;
92 }
93}
94
Daniel Vetter297e30b2018-10-04 22:24:27 +020095/*
96 * For connectors that support multiple encoders, either the
97 * .atomic_best_encoder() or .best_encoder() operation must be implemented.
98 */
99static struct drm_encoder *
100pick_single_encoder_for_connector(struct drm_connector *connector)
101{
102 WARN_ON(connector->encoder_ids[1]);
103 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
104}
105
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100106static int handle_conflicting_encoders(struct drm_atomic_state *state,
107 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +0100108{
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100109 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200110 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100111 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100112 struct drm_encoder *encoder;
113 unsigned encoder_mask = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100114 int i, ret = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200115
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100116 /*
117 * First loop, find all newly assigned encoders from the connectors
118 * part of the state. If the same encoder is assigned to multiple
119 * connectors bail out.
120 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100121 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100122 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
123 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200124
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100125 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200126 continue;
127
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100128 if (funcs->atomic_best_encoder)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100129 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200130 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100131 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200132 else
Daniel Vetter297e30b2018-10-04 22:24:27 +0200133 new_encoder = pick_single_encoder_for_connector(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100134
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100135 if (new_encoder) {
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300136 if (encoder_mask & drm_encoder_mask(new_encoder)) {
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100137 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
138 new_encoder->base.id, new_encoder->name,
139 connector->base.id, connector->name);
140
141 return -EINVAL;
142 }
143
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300144 encoder_mask |= drm_encoder_mask(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100145 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200146 }
147
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100148 if (!encoder_mask)
149 return 0;
150
151 /*
152 * Second loop, iterate over all connectors not part of the state.
153 *
154 * If a conflicting encoder is found and disable_conflicting_encoders
155 * is not set, an error is returned. Userspace can provide a solution
156 * through the atomic ioctl.
157 *
158 * If the flag is set conflicting connectors are removed from the crtc
159 * and the crtc is disabled if no encoder is left. This preserves
160 * compatibility with the legacy set_config behavior.
161 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100162 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100163 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100164 struct drm_crtc_state *crtc_state;
165
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100166 if (drm_atomic_get_new_connector_state(state, connector))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100167 continue;
168
169 encoder = connector->state->best_encoder;
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300170 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100171 continue;
172
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100173 if (!disable_conflicting_encoders) {
174 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
175 encoder->base.id, encoder->name,
176 connector->state->crtc->base.id,
177 connector->state->crtc->name,
178 connector->base.id, connector->name);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100179 ret = -EINVAL;
180 goto out;
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100181 }
182
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100183 new_conn_state = drm_atomic_get_connector_state(state, connector);
184 if (IS_ERR(new_conn_state)) {
185 ret = PTR_ERR(new_conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100186 goto out;
187 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100188
189 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
190 encoder->base.id, encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100191 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100192 connector->base.id, connector->name);
193
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100194 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100195
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100196 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100197 if (ret)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100198 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100199
200 if (!crtc_state->connector_mask) {
201 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
202 NULL);
203 if (ret < 0)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100204 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100205
206 crtc_state->active = false;
207 }
208 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100209out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100210 drm_connector_list_iter_end(&conn_iter);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100211
Daniel Vetterc36a3252016-12-15 16:58:43 +0100212 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200213}
214
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100215static void
216set_best_encoder(struct drm_atomic_state *state,
217 struct drm_connector_state *conn_state,
218 struct drm_encoder *encoder)
219{
220 struct drm_crtc_state *crtc_state;
221 struct drm_crtc *crtc;
222
223 if (conn_state->best_encoder) {
224 /* Unset the encoder_mask in the old crtc state. */
225 crtc = conn_state->connector->state->crtc;
226
227 /* A NULL crtc is an error here because we should have
228 * duplicated a NULL best_encoder when crtc was NULL.
229 * As an exception restoring duplicated atomic state
230 * during resume is allowed, so don't warn when
231 * best_encoder is equal to encoder we intend to set.
232 */
233 WARN_ON(!crtc && encoder != conn_state->best_encoder);
234 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100235 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100236
237 crtc_state->encoder_mask &=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300238 ~drm_encoder_mask(conn_state->best_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100239 }
240 }
241
242 if (encoder) {
243 crtc = conn_state->crtc;
244 WARN_ON(!crtc);
245 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100246 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100247
248 crtc_state->encoder_mask |=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300249 drm_encoder_mask(encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100250 }
251 }
252
253 conn_state->best_encoder = encoder;
254}
255
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100256static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200257steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100258 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200259{
Daniel Vetter623369e2014-09-16 17:50:47 +0200260 struct drm_crtc_state *crtc_state;
261 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100262 struct drm_connector_state *old_connector_state, *new_connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100263 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200264
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100265 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100266 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200267
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100268 if (new_connector_state->best_encoder != encoder)
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100269 continue;
270
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100271 encoder_crtc = old_connector_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200272
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100273 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
274 encoder->base.id, encoder->name,
275 encoder_crtc->base.id, encoder_crtc->name);
276
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100277 set_best_encoder(state, new_connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100278
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100279 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100280 crtc_state->connectors_changed = true;
281
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100282 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200283 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200284}
285
286static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100287update_connector_routing(struct drm_atomic_state *state,
288 struct drm_connector *connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100289 struct drm_connector_state *old_connector_state,
290 struct drm_connector_state *new_connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200291{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200292 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200293 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200294 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200295
Daniel Vetter17a38d92015-02-22 12:24:16 +0100296 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
297 connector->base.id,
298 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200299
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100300 if (old_connector_state->crtc != new_connector_state->crtc) {
301 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100302 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200303 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200304 }
305
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100306 if (new_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100307 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200308 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200309 }
310 }
311
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100312 if (!new_connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100313 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200314 connector->base.id,
315 connector->name);
316
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100317 set_best_encoder(state, new_connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200318
319 return 0;
320 }
321
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400322 crtc_state = drm_atomic_get_new_crtc_state(state,
323 new_connector_state->crtc);
324 /*
325 * For compatibility with legacy users, we want to make sure that
326 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
327 * which would result in anything else must be considered invalid, to
328 * avoid turning on new displays on dead connectors.
329 *
330 * Since the connector can be unregistered at any point during an
331 * atomic check or commit, this is racy. But that's OK: all we care
332 * about is ensuring that userspace can't do anything but shut off the
333 * display on a connector that was destroyed after its been notified,
334 * not before.
335 */
336 if (drm_connector_is_unregistered(connector) && crtc_state->active) {
337 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
338 connector->base.id, connector->name);
339 return -EINVAL;
340 }
341
Daniel Vetter623369e2014-09-16 17:50:47 +0200342 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200343
344 if (funcs->atomic_best_encoder)
345 new_encoder = funcs->atomic_best_encoder(connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100346 new_connector_state);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200347 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200348 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200349 else
Daniel Vetter297e30b2018-10-04 22:24:27 +0200350 new_encoder = pick_single_encoder_for_connector(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200351
352 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100353 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
354 connector->base.id,
355 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200356 return -EINVAL;
357 }
358
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100359 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
Russell King6ac7c542017-02-13 12:27:03 +0000360 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100361 new_encoder->base.id,
362 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100363 new_connector_state->crtc->base.id,
364 new_connector_state->crtc->name);
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100365 return -EINVAL;
366 }
367
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100368 if (new_encoder == new_connector_state->best_encoder) {
369 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100370
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200371 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100372 connector->base.id,
373 connector->name,
374 new_encoder->base.id,
375 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100376 new_connector_state->crtc->base.id,
377 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200378
379 return 0;
380 }
381
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100382 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200383
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100384 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100385
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200386 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200387
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200388 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100389 connector->base.id,
390 connector->name,
391 new_encoder->base.id,
392 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100393 new_connector_state->crtc->base.id,
394 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200395
396 return 0;
397}
398
399static int
400mode_fixup(struct drm_atomic_state *state)
401{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300402 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100403 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300404 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100405 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200406 int i;
Dan Carpenterf9ad86e2017-02-08 02:46:01 +0300407 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200408
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100409 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
410 if (!new_crtc_state->mode_changed &&
411 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200412 continue;
413
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100414 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200415 }
416
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100417 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200418 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200419 struct drm_encoder *encoder;
420
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100421 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200422
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100423 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200424 continue;
425
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100426 new_crtc_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100427 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200428
429 /*
430 * Each encoder has at most one connector (since we always steal
431 * it away), so we won't call ->mode_fixup twice.
432 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100433 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200434 funcs = encoder->helper_private;
435
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100436 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
437 &new_crtc_state->adjusted_mode);
Archit Taneja862e6862015-05-21 11:03:16 +0530438 if (!ret) {
439 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
440 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200441 }
442
Noralf Trønnes28276352016-05-11 18:09:20 +0200443 if (funcs && funcs->atomic_check) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100444 ret = funcs->atomic_check(encoder, new_crtc_state,
445 new_conn_state);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100446 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100447 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
448 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100449 return ret;
450 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200451 } else if (funcs && funcs->mode_fixup) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100452 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
453 &new_crtc_state->adjusted_mode);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100454 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100455 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
456 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100457 return -EINVAL;
458 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200459 }
460 }
461
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100462 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200463 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200464
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100465 if (!new_crtc_state->enable)
Liu Yingf55f1702016-05-27 17:35:54 +0800466 continue;
467
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100468 if (!new_crtc_state->mode_changed &&
469 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200470 continue;
471
472 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300473 if (!funcs->mode_fixup)
474 continue;
475
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100476 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
477 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200478 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200479 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
480 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200481 return -EINVAL;
482 }
483 }
484
485 return 0;
486}
487
Jose Abreufaf94a02017-05-25 15:19:16 +0100488static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
489 struct drm_encoder *encoder,
490 struct drm_crtc *crtc,
491 struct drm_display_mode *mode)
492{
493 enum drm_mode_status ret;
494
495 ret = drm_encoder_mode_valid(encoder, mode);
496 if (ret != MODE_OK) {
497 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
498 encoder->base.id, encoder->name);
499 return ret;
500 }
501
502 ret = drm_bridge_mode_valid(encoder->bridge, mode);
503 if (ret != MODE_OK) {
504 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
505 return ret;
506 }
507
508 ret = drm_crtc_mode_valid(crtc, mode);
509 if (ret != MODE_OK) {
510 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
511 crtc->base.id, crtc->name);
512 return ret;
513 }
514
515 return ret;
516}
517
518static int
519mode_valid(struct drm_atomic_state *state)
520{
521 struct drm_connector_state *conn_state;
522 struct drm_connector *connector;
523 int i;
524
525 for_each_new_connector_in_state(state, connector, conn_state, i) {
526 struct drm_encoder *encoder = conn_state->best_encoder;
527 struct drm_crtc *crtc = conn_state->crtc;
528 struct drm_crtc_state *crtc_state;
529 enum drm_mode_status mode_status;
530 struct drm_display_mode *mode;
531
532 if (!crtc || !encoder)
533 continue;
534
535 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
536 if (!crtc_state)
537 continue;
538 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
539 continue;
540
541 mode = &crtc_state->mode;
542
543 mode_status = mode_valid_path(connector, encoder, crtc, mode);
544 if (mode_status != MODE_OK)
545 return -EINVAL;
546 }
547
548 return 0;
549}
550
Daniel Vetterd9b13622014-11-26 16:57:41 +0100551/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800552 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100553 * @dev: DRM device
554 * @state: the driver state object
555 *
556 * Check the state object to see if the requested state is physically possible.
557 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200558 * update and adds any additional connectors needed for full modesets. It calls
559 * the various per-object callbacks in the follow order:
560 *
561 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
562 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
563 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
564 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
Jose Abreufaf94a02017-05-25 15:19:16 +0100565 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
566 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
567 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
568 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200569 * This function is only called when the encoder will be part of a configured crtc,
570 * it must not be used for implementing connector property validation.
571 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
572 * instead.
Jose Abreufaf94a02017-05-25 15:19:16 +0100573 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200574 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100575 * &drm_crtc_state.mode_changed is set when the input mode is changed.
576 * &drm_crtc_state.connectors_changed is set when a connector is added or
577 * removed from the crtc. &drm_crtc_state.active_changed is set when
578 * &drm_crtc_state.active changes, which is used for DPMS.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100579 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100580 *
581 * IMPORTANT:
582 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100583 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
584 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
585 * without a full modeset) _must_ call this function afterwards after that
586 * change. It is permitted to call this function multiple times for the same
587 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
588 * upon the adjusted dotclock for fifo space allocation and watermark
589 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100590 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200591 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100592 * Zero for success or -errno
593 */
594int
Rob Clark934ce1c2014-11-19 16:41:33 -0500595drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200596 struct drm_atomic_state *state)
597{
Daniel Vetter623369e2014-09-16 17:50:47 +0200598 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100599 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300600 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100601 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200602 int i, ret;
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200603 unsigned connectors_mask = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200604
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100605 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200606 bool has_connectors =
607 !!new_crtc_state->connector_mask;
608
Daniel Vetter869e1882017-05-31 10:38:13 +0200609 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
610
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100611 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200612 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
613 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100614 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200615 }
616
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100617 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200618 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
619 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200620
621 /*
622 * For clarity this assignment is done here, but
623 * enable == 0 is only true when there are no
624 * connectors and a NULL mode.
625 *
626 * The other way around is true as well. enable != 0
627 * iff connectors are attached and a mode is set.
628 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100629 new_crtc_state->mode_changed = true;
630 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200631 }
Maarten Lankhorst24d66522017-04-06 13:19:01 +0200632
633 if (old_crtc_state->active != new_crtc_state->active) {
634 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
635 crtc->base.id, crtc->name);
636 new_crtc_state->active_changed = true;
637 }
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200638
639 if (new_crtc_state->enable != has_connectors) {
640 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
641 crtc->base.id, crtc->name);
642
643 return -EINVAL;
644 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200645 }
646
Maarten Lankhorst44596b82017-04-06 13:19:00 +0200647 ret = handle_conflicting_encoders(state, false);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100648 if (ret)
649 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100650
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100651 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200652 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
653
Daniel Vetter869e1882017-05-31 10:38:13 +0200654 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
655
Daniel Vetter623369e2014-09-16 17:50:47 +0200656 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100657 * This only sets crtc->connectors_changed for routing changes,
658 * drivers must set crtc->connectors_changed themselves when
659 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200660 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100661 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100662 old_connector_state,
663 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200664 if (ret)
665 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100666 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100667 new_crtc_state = drm_atomic_get_new_crtc_state(state,
668 old_connector_state->crtc);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100669 if (old_connector_state->link_status !=
670 new_connector_state->link_status)
671 new_crtc_state->connectors_changed = true;
Radhakrishna Sripada47e22ff2018-10-12 11:42:32 -0700672
673 if (old_connector_state->max_requested_bpc !=
674 new_connector_state->max_requested_bpc)
675 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200676 }
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200677
678 if (funcs->atomic_check)
679 ret = funcs->atomic_check(connector, new_connector_state);
680 if (ret)
681 return ret;
682
Ville Syrjäläca52bea2018-06-15 20:07:34 +0300683 connectors_mask |= BIT(i);
Daniel Vetter623369e2014-09-16 17:50:47 +0200684 }
685
686 /*
687 * After all the routing has been prepared we need to add in any
688 * connector which is itself unchanged, but who's crtc changes it's
689 * configuration. This must be done before calling mode_fixup in case a
690 * crtc only changed its mode but has the same set of connectors.
691 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100692 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100693 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100694 continue;
695
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200696 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
697 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100698 new_crtc_state->enable ? 'y' : 'n',
699 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200700
701 ret = drm_atomic_add_affected_connectors(state, crtc);
702 if (ret != 0)
703 return ret;
704
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200705 ret = drm_atomic_add_affected_planes(state, crtc);
706 if (ret != 0)
707 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200708 }
709
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200710 /*
711 * Iterate over all connectors again, to make sure atomic_check()
712 * has been called on them when a modeset is forced.
713 */
714 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
715 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
716
717 if (connectors_mask & BIT(i))
718 continue;
719
720 if (funcs->atomic_check)
721 ret = funcs->atomic_check(connector, new_connector_state);
722 if (ret)
723 return ret;
724 }
725
Jose Abreufaf94a02017-05-25 15:19:16 +0100726 ret = mode_valid(state);
727 if (ret)
728 return ret;
729
Daniel Vetter623369e2014-09-16 17:50:47 +0200730 return mode_fixup(state);
731}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100732EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200733
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100734/**
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200735 * drm_atomic_helper_check_plane_state() - Check plane state for validity
736 * @plane_state: plane state to check
737 * @crtc_state: crtc state to check
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200738 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
739 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
740 * @can_position: is it legal to position the plane such that it
741 * doesn't cover the entire crtc? This will generally
742 * only be false for primary planes.
743 * @can_update_disabled: can the plane be updated while the crtc
744 * is disabled?
745 *
746 * Checks that a desired plane update is valid, and updates various
747 * bits of derived state (clipped coordinates etc.). Drivers that provide
748 * their own plane handling rather than helper-provided implementations may
749 * still wish to call this function to avoid duplication of error checking
750 * code.
751 *
752 * RETURNS:
753 * Zero if update appears valid, error code on failure
754 */
755int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
756 const struct drm_crtc_state *crtc_state,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200757 int min_scale,
758 int max_scale,
759 bool can_position,
760 bool can_update_disabled)
761{
762 struct drm_framebuffer *fb = plane_state->fb;
763 struct drm_rect *src = &plane_state->src;
764 struct drm_rect *dst = &plane_state->dst;
765 unsigned int rotation = plane_state->rotation;
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200766 struct drm_rect clip = {};
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200767 int hscale, vscale;
768
769 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
770
771 *src = drm_plane_state_src(plane_state);
772 *dst = drm_plane_state_dest(plane_state);
773
774 if (!fb) {
775 plane_state->visible = false;
776 return 0;
777 }
778
779 /* crtc should only be NULL when disabling (i.e., !fb) */
780 if (WARN_ON(!plane_state->crtc)) {
781 plane_state->visible = false;
782 return 0;
783 }
784
785 if (!crtc_state->enable && !can_update_disabled) {
786 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
787 return -EINVAL;
788 }
789
790 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
791
792 /* Check scaling */
793 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
794 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
795 if (hscale < 0 || vscale < 0) {
796 DRM_DEBUG_KMS("Invalid scaling of plane\n");
797 drm_rect_debug_print("src: ", &plane_state->src, true);
798 drm_rect_debug_print("dst: ", &plane_state->dst, false);
799 return -ERANGE;
800 }
801
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200802 if (crtc_state->enable)
803 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
804
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +0200805 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200806
807 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
808
809 if (!plane_state->visible)
810 /*
811 * Plane isn't visible; some drivers can handle this
812 * so we just return success here. Drivers that can't
813 * (including those that use the primary plane helper's
814 * update function) will return an error from their
815 * update_plane handler.
816 */
817 return 0;
818
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200819 if (!can_position && !drm_rect_equals(dst, &clip)) {
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200820 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
821 drm_rect_debug_print("dst: ", dst, false);
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200822 drm_rect_debug_print("clip: ", &clip, false);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200823 return -EINVAL;
824 }
825
826 return 0;
827}
828EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
829
830/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800831 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100832 * @dev: DRM device
833 * @state: the driver state object
834 *
835 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100836 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100837 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
838 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100839 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100840 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200841 * updated planes.
842 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200843 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100844 * Zero for success or -errno
845 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100846int
847drm_atomic_helper_check_planes(struct drm_device *dev,
848 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100849{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300850 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100851 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300852 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100853 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100854 int i, ret = 0;
855
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100856 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200857 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100858
Daniel Vetter869e1882017-05-31 10:38:13 +0200859 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
860
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100861 funcs = plane->helper_private;
862
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100863 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100864
865 if (!funcs || !funcs->atomic_check)
866 continue;
867
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100868 ret = funcs->atomic_check(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100869 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200870 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
871 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100872 return ret;
873 }
874 }
875
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100876 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200877 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100878
879 funcs = crtc->helper_private;
880
881 if (!funcs || !funcs->atomic_check)
882 continue;
883
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100884 ret = funcs->atomic_check(crtc, new_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100885 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200886 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
887 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100888 return ret;
889 }
890 }
891
Daniel Vetterd9b13622014-11-26 16:57:41 +0100892 return ret;
893}
894EXPORT_SYMBOL(drm_atomic_helper_check_planes);
895
896/**
897 * drm_atomic_helper_check - validate state object
898 * @dev: DRM device
899 * @state: the driver state object
900 *
901 * Check the state object to see if the requested state is physically possible.
902 * Only crtcs and planes have check callbacks, so for any additional (global)
903 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100904 * Drivers without such needs can directly use this as their
905 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100906 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100907 * This just wraps the two parts of the state checking for planes and modeset
908 * state in the default order: First it calls drm_atomic_helper_check_modeset()
909 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100910 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
911 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
912 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100913 *
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200914 * Note that zpos normalization will add all enable planes to the state which
915 * might not desired for some drivers.
916 * For example enable/disable of a cursor plane which have fixed zpos value
917 * would trigger all other enabled planes to be forced to the state change.
918 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200919 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100920 * Zero for success or -errno
921 */
922int drm_atomic_helper_check(struct drm_device *dev,
923 struct drm_atomic_state *state)
924{
925 int ret;
926
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100927 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100928 if (ret)
929 return ret;
930
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200931 if (dev->mode_config.normalize_zpos) {
932 ret = drm_atomic_normalize_zpos(dev, state);
933 if (ret)
934 return ret;
935 }
936
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100937 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500938 if (ret)
939 return ret;
940
Gustavo Padovanfef9df82017-06-30 15:03:17 -0300941 if (state->legacy_cursor_update)
942 state->async_update = !drm_atomic_helper_async_check(dev, state);
943
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100944 return ret;
945}
946EXPORT_SYMBOL(drm_atomic_helper_check);
947
Daniel Vetter623369e2014-09-16 17:50:47 +0200948static void
949disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
950{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300951 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100952 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300953 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100954 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200955 int i;
956
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100957 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200958 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200959 struct drm_encoder *encoder;
960
Daniel Vetter623369e2014-09-16 17:50:47 +0200961 /* Shut down everything that's in the changeset and currently
962 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300963 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200964 continue;
965
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100966 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100967
Daniel Vetter4218a322015-03-26 22:18:40 +0100968 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200969 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100970 continue;
971
Rob Clark46df9ad2014-11-20 15:40:36 -0500972 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200973
Rob Clark46df9ad2014-11-20 15:40:36 -0500974 /* We shouldn't get this far if we didn't previously have
975 * an encoder.. but WARN_ON() rather than explode.
976 */
977 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200978 continue;
979
980 funcs = encoder->helper_private;
981
Daniel Vetter17a38d92015-02-22 12:24:16 +0100982 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
983 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100984
Daniel Vetter623369e2014-09-16 17:50:47 +0200985 /*
986 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800987 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200988 */
Archit Taneja862e6862015-05-21 11:03:16 +0530989 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200990
991 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200992 if (funcs) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100993 if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +0200994 funcs->prepare(encoder);
995 else if (funcs->disable)
996 funcs->disable(encoder);
997 else if (funcs->dpms)
998 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
999 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001000
Archit Taneja862e6862015-05-21 11:03:16 +05301001 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001002 }
1003
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001004 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001005 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter84014b02017-10-17 17:27:14 +02001006 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +02001007
1008 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001009 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001010 continue;
1011
1012 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +02001013 continue;
1014
1015 funcs = crtc->helper_private;
1016
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001017 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1018 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001019
1020
Daniel Vetter623369e2014-09-16 17:50:47 +02001021 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001022 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +02001023 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +08001024 else if (funcs->atomic_disable)
1025 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001026 else if (funcs->disable)
1027 funcs->disable(crtc);
1028 else
1029 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Daniel Vetter84014b02017-10-17 17:27:14 +02001030
1031 if (!(dev->irq_enabled && dev->num_crtcs))
1032 continue;
1033
1034 ret = drm_crtc_vblank_get(crtc);
1035 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1036 if (ret == 0)
1037 drm_crtc_vblank_put(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001038 }
1039}
1040
Daniel Vetter4c18d302015-05-12 15:27:37 +02001041/**
1042 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1043 * @dev: DRM device
1044 * @old_state: atomic state object with old state structures
1045 *
1046 * This function updates all the various legacy modeset state pointers in
1047 * connectors, encoders and crtcs. It also updates the timestamping constants
1048 * used for precise vblank timestamps by calling
1049 * drm_calc_timestamping_constants().
1050 *
1051 * Drivers can use this for building their own atomic commit if they don't have
1052 * a pure helper-based modeset implementation.
Daniel Vetter2e2b96e2017-11-08 21:30:07 +01001053 *
1054 * Since these updates are not synchronized with lockings, only code paths
1055 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1056 * legacy state filled out by this helper. Defacto this means this helper and
1057 * the legacy state pointers are only really useful for transitioning an
1058 * existing driver to the atomic world.
Daniel Vetter4c18d302015-05-12 15:27:37 +02001059 */
1060void
1061drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1062 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001063{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001064 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001065 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001066 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001067 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001068 int i;
1069
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001070 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001071 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001072 if (connector->encoder) {
1073 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001074
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001075 connector->encoder->crtc = NULL;
1076 connector->encoder = NULL;
1077 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001078
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001079 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001080 if ((!crtc && old_conn_state->crtc) ||
1081 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001082 int mode = DRM_MODE_DPMS_OFF;
1083
1084 if (crtc && crtc->state->active)
1085 mode = DRM_MODE_DPMS_ON;
1086
1087 connector->dpms = mode;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001088 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001089 }
1090
1091 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001092 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1093 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +02001094 continue;
1095
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001096 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +02001097 continue;
1098
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001099 connector->encoder = new_conn_state->best_encoder;
1100 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +02001101 }
1102
1103 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001104 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +02001105 struct drm_plane *primary = crtc->primary;
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001106 struct drm_plane_state *new_plane_state;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001107
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001108 crtc->mode = new_crtc_state->mode;
1109 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001110
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001111 new_plane_state =
1112 drm_atomic_get_new_plane_state(old_state, primary);
1113
1114 if (new_plane_state && new_plane_state->crtc == crtc) {
1115 crtc->x = new_plane_state->src_x >> 16;
1116 crtc->y = new_plane_state->src_y >> 16;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001117 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +02001118
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001119 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +02001120 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001121 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001122 }
1123}
Daniel Vetter4c18d302015-05-12 15:27:37 +02001124EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001125
1126static void
1127crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1128{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001129 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001130 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001131 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001132 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001133 int i;
1134
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001135 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001136 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001137
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001138 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +02001139 continue;
1140
1141 funcs = crtc->helper_private;
1142
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001143 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001144 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1145 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001146
Daniel Vetter623369e2014-09-16 17:50:47 +02001147 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001148 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001149 }
1150
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001151 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001152 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001153 struct drm_encoder *encoder;
1154 struct drm_display_mode *mode, *adjusted_mode;
1155
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001156 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001157 continue;
1158
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001159 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001160 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001161 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001162 mode = &new_crtc_state->mode;
1163 adjusted_mode = &new_crtc_state->adjusted_mode;
1164
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001165 if (!new_crtc_state->mode_changed)
1166 continue;
1167
Daniel Vetter17a38d92015-02-22 12:24:16 +01001168 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1169 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001170
Daniel Vetter623369e2014-09-16 17:50:47 +02001171 /*
1172 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001173 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001174 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001175 if (funcs && funcs->atomic_mode_set) {
1176 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001177 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001178 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +01001179 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001180 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001181
Archit Taneja862e6862015-05-21 11:03:16 +05301182 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001183 }
1184}
1185
1186/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001187 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001188 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +01001189 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001190 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001191 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +02001192 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +01001193 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +03001194 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +01001195 * drm_atomic_helper_commit_planes(), which is what the default commit function
1196 * does. But drivers with different needs can group the modeset commits together
1197 * and do the plane commits at the end. This is useful for drivers doing runtime
1198 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001199 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001200void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1201 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001202{
Laurent Pincharta072f802015-02-22 12:24:18 +01001203 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +02001204
1205 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1206
Laurent Pincharta072f802015-02-22 12:24:18 +01001207 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001208}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001209EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001210
Brian Starkey935774c2017-03-29 17:42:32 +01001211static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1212 struct drm_atomic_state *old_state)
1213{
1214 struct drm_connector *connector;
1215 struct drm_connector_state *new_conn_state;
1216 int i;
1217
1218 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1219 const struct drm_connector_helper_funcs *funcs;
1220
1221 funcs = connector->helper_private;
Boris Brezillon814bde92018-07-03 09:50:17 +02001222 if (!funcs->atomic_commit)
1223 continue;
Brian Starkey935774c2017-03-29 17:42:32 +01001224
1225 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1226 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
Boris Brezillon425132f2018-07-03 09:50:16 +02001227 funcs->atomic_commit(connector, new_conn_state);
Brian Starkey935774c2017-03-29 17:42:32 +01001228 }
1229 }
1230}
1231
Daniel Vetter623369e2014-09-16 17:50:47 +02001232/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001233 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001234 * @dev: DRM device
1235 * @old_state: atomic state object with old state structures
1236 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001237 * This function enables all the outputs with the new configuration which had to
1238 * be turned off for the update.
1239 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +03001240 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +01001241 * drm_atomic_helper_commit_planes(), which is what the default commit function
1242 * does. But drivers with different needs can group the modeset commits together
1243 * and do the plane commits at the end. This is useful for drivers doing runtime
1244 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001245 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001246void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1247 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001248{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001249 struct drm_crtc *crtc;
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001250 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001251 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001252 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001253 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001254 int i;
1255
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001256 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001257 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001258
1259 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001260 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001261 continue;
1262
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001263 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +02001264 continue;
1265
1266 funcs = crtc->helper_private;
1267
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001268 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001269 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1270 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001271
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001272 if (funcs->atomic_enable)
1273 funcs->atomic_enable(crtc, old_crtc_state);
Daniel Vetteree0a89c2015-01-22 16:36:24 +01001274 else
1275 funcs->commit(crtc);
1276 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001277 }
1278
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001279 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001280 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001281 struct drm_encoder *encoder;
1282
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001283 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001284 continue;
1285
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001286 if (!new_conn_state->crtc->state->active ||
1287 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001288 continue;
1289
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001290 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001291 funcs = encoder->helper_private;
1292
Daniel Vetter17a38d92015-02-22 12:24:16 +01001293 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1294 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001295
Daniel Vetter623369e2014-09-16 17:50:47 +02001296 /*
1297 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001298 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001299 */
Archit Taneja862e6862015-05-21 11:03:16 +05301300 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001301
Noralf Trønnes28276352016-05-11 18:09:20 +02001302 if (funcs) {
1303 if (funcs->enable)
1304 funcs->enable(encoder);
1305 else if (funcs->commit)
1306 funcs->commit(encoder);
1307 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001308
Archit Taneja862e6862015-05-21 11:03:16 +05301309 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001310 }
Brian Starkey935774c2017-03-29 17:42:32 +01001311
1312 drm_atomic_helper_commit_writebacks(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001313}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001314EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001315
Rob Clark4c5b7f32016-03-18 19:14:55 -04001316/**
1317 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1318 * @dev: DRM device
1319 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001320 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1321 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001322 *
1323 * For implicit sync, driver should fish the exclusive fence out from the
1324 * incoming fb's and stash it in the drm_plane_state. This is called after
1325 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1326 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001327 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001328 * Note that @pre_swap is needed since the point where we block for fences moves
1329 * around depending upon whether an atomic commit is blocking or
Gustavo Padovan42590372017-04-27 11:35:06 -03001330 * non-blocking. For non-blocking commit all waiting needs to happen after
1331 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001332 * to wait **before** we do anything that can't be easily rolled back. That is
1333 * before we call drm_atomic_helper_swap_state().
1334 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001335 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001336 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001337int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1338 struct drm_atomic_state *state,
1339 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001340{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001341 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001342 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001343 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001344
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001345 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1346 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001347 continue;
1348
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001349 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001350
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001351 /*
1352 * If waiting for fences pre-swap (ie: nonblock), userspace can
1353 * still interrupt the operation. Instead of blocking until the
1354 * timer expires, make the wait interruptible.
1355 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001356 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001357 if (ret)
1358 return ret;
1359
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001360 dma_fence_put(new_plane_state->fence);
1361 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001362 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001363
1364 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001365}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001366EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001367
John Keepingc2409062016-01-19 10:46:58 +00001368/**
Rob Clark5ee32292014-11-11 19:38:59 -05001369 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1370 * @dev: DRM device
1371 * @old_state: atomic state object with old state structures
1372 *
1373 * Helper to, after atomic commit, wait for vblanks on all effected
1374 * crtcs (ie. before cleaning up old framebuffers using
Boris Brezillon01086482017-06-02 10:32:06 +02001375 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
Daniel Vetterab58e332014-11-24 20:42:42 +01001376 * framebuffers have actually changed to optimize for the legacy cursor and
1377 * plane update use-case.
Boris Brezillon01086482017-06-02 10:32:06 +02001378 *
1379 * Drivers using the nonblocking commit tracking support initialized by calling
1380 * drm_atomic_helper_setup_commit() should look at
1381 * drm_atomic_helper_wait_for_flip_done() as an alternative.
Rob Clark5ee32292014-11-11 19:38:59 -05001382 */
1383void
1384drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1385 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001386{
1387 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001388 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001389 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001390 unsigned crtc_mask = 0;
1391
1392 /*
1393 * Legacy cursor ioctls are completely unsynced, and userspace
1394 * relies on that (by doing tons of cursor updates).
1395 */
1396 if (old_state->legacy_cursor_update)
1397 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001398
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001399 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Lucas Stacha76dfe32017-11-29 12:04:31 +01001400 if (!new_crtc_state->active)
Daniel Vetterab58e332014-11-24 20:42:42 +01001401 continue;
1402
Daniel Vetter623369e2014-09-16 17:50:47 +02001403 ret = drm_crtc_vblank_get(crtc);
1404 if (ret != 0)
1405 continue;
1406
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001407 crtc_mask |= drm_crtc_mask(crtc);
1408 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001409 }
1410
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001411 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001412 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001413 continue;
1414
1415 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001416 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001417 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001418 msecs_to_jiffies(50));
1419
Russell King6ac7c542017-02-13 12:27:03 +00001420 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1421 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001422
Daniel Vetter623369e2014-09-16 17:50:47 +02001423 drm_crtc_vblank_put(crtc);
1424 }
1425}
Rob Clark5ee32292014-11-11 19:38:59 -05001426EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001427
1428/**
Boris Brezillon01086482017-06-02 10:32:06 +02001429 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1430 * @dev: DRM device
1431 * @old_state: atomic state object with old state structures
1432 *
1433 * Helper to, after atomic commit, wait for page flips on all effected
1434 * crtcs (ie. before cleaning up old framebuffers using
1435 * drm_atomic_helper_cleanup_planes()). Compared to
1436 * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1437 * CRTCs, assuming that cursors-only updates are signalling their completion
1438 * immediately (or using a different path).
1439 *
1440 * This requires that drivers use the nonblocking commit tracking support
1441 * initialized using drm_atomic_helper_setup_commit().
1442 */
1443void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1444 struct drm_atomic_state *old_state)
1445{
Boris Brezillon01086482017-06-02 10:32:06 +02001446 struct drm_crtc *crtc;
1447 int i;
1448
Leo Li4364bcb2018-10-15 09:46:40 -04001449 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1450 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
Boris Brezillon01086482017-06-02 10:32:06 +02001451 int ret;
1452
Leo Li4364bcb2018-10-15 09:46:40 -04001453 crtc = old_state->crtcs[i].ptr;
1454
1455 if (!crtc || !commit)
Boris Brezillon01086482017-06-02 10:32:06 +02001456 continue;
1457
1458 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1459 if (ret == 0)
1460 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1461 crtc->base.id, crtc->name);
1462 }
Ville Syrjälä2de42f72018-11-22 16:34:11 +02001463
1464 if (old_state->fake_commit)
1465 complete_all(&old_state->fake_commit->flip_done);
Boris Brezillon01086482017-06-02 10:32:06 +02001466}
1467EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1468
1469/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001470 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001471 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001472 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001473 * This is the default implementation for the
Maxime Ripard81a099a2017-07-20 15:01:16 +02001474 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1475 * that do not support runtime_pm or do not need the CRTC to be
1476 * enabled to perform a commit. Otherwise, see
1477 * drm_atomic_helper_commit_tail_rpm().
Daniel Vetter623369e2014-09-16 17:50:47 +02001478 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001479 * Note that the default ordering of how the various stages are called is to
Maxime Ripard81a099a2017-07-20 15:01:16 +02001480 * match the legacy modeset helper library closest.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001481 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001482void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001483{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001484 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001485
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001486 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001487
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001488 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001489
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001490 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001491
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001492 drm_atomic_helper_fake_vblank(old_state);
1493
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001494 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001495
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001496 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001497
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001498 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001499}
1500EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1501
Maxime Ripard81a099a2017-07-20 15:01:16 +02001502/**
1503 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1504 * @old_state: new modeset state to be committed
1505 *
1506 * This is an alternative implementation for the
1507 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1508 * that support runtime_pm or need the CRTC to be enabled to perform a
1509 * commit. Otherwise, one should use the default implementation
1510 * drm_atomic_helper_commit_tail().
1511 */
1512void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1513{
1514 struct drm_device *dev = old_state->dev;
1515
1516 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1517
1518 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1519
1520 drm_atomic_helper_commit_planes(dev, old_state,
1521 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1522
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001523 drm_atomic_helper_fake_vblank(old_state);
1524
Maxime Ripard81a099a2017-07-20 15:01:16 +02001525 drm_atomic_helper_commit_hw_done(old_state);
1526
1527 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1528
1529 drm_atomic_helper_cleanup_planes(dev, old_state);
1530}
1531EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1532
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001533static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001534{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001535 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001536 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001537
1538 funcs = dev->mode_config.helper_private;
1539
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001540 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001541
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001542 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001543
1544 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001545 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001546 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001547 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001548
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001549 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001550
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001551 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001552}
1553
1554static void commit_work(struct work_struct *work)
1555{
1556 struct drm_atomic_state *state = container_of(work,
1557 struct drm_atomic_state,
1558 commit_work);
1559 commit_tail(state);
1560}
1561
1562/**
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001563 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1564 * @dev: DRM device
1565 * @state: the driver state object
1566 *
1567 * This helper will check if it is possible to commit the state asynchronously.
1568 * Async commits are not supposed to swap the states like normal sync commits
1569 * but just do in-place changes on the current state.
1570 *
1571 * It will return 0 if the commit can happen in an asynchronous fashion or error
1572 * if not. Note that error just mean it can't be commited asynchronously, if it
1573 * fails the commit should be treated like a normal synchronous commit.
1574 */
1575int drm_atomic_helper_async_check(struct drm_device *dev,
1576 struct drm_atomic_state *state)
1577{
1578 struct drm_crtc *crtc;
1579 struct drm_crtc_state *crtc_state;
Boris Brezillonde2d8db2018-07-24 15:33:00 +02001580 struct drm_plane *plane = NULL;
1581 struct drm_plane_state *old_plane_state = NULL;
1582 struct drm_plane_state *new_plane_state = NULL;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001583 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001584 int i, n_planes = 0;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001585
1586 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1587 if (drm_atomic_crtc_needs_modeset(crtc_state))
1588 return -EINVAL;
1589 }
1590
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001591 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001592 n_planes++;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001593
1594 /* FIXME: we support only single plane updates for now */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001595 if (n_planes != 1)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001596 return -EINVAL;
1597
Boris Brezillon603ba2d2018-07-24 15:32:15 +02001598 if (!new_plane_state->crtc ||
1599 old_plane_state->crtc != new_plane_state->crtc)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001600 return -EINVAL;
1601
1602 funcs = plane->helper_private;
1603 if (!funcs->atomic_async_update)
1604 return -EINVAL;
1605
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001606 if (new_plane_state->fence)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001607 return -EINVAL;
1608
1609 /*
1610 * Don't do an async update if there is an outstanding commit modifying
1611 * the plane. This prevents our async update's changes from getting
1612 * overridden by a previous synchronous update's state.
1613 */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001614 if (old_plane_state->commit &&
1615 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1616 return -EBUSY;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001617
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001618 return funcs->atomic_async_check(plane, new_plane_state);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001619}
1620EXPORT_SYMBOL(drm_atomic_helper_async_check);
1621
1622/**
1623 * drm_atomic_helper_async_commit - commit state asynchronously
1624 * @dev: DRM device
1625 * @state: the driver state object
1626 *
1627 * This function commits a state asynchronously, i.e., not vblank
1628 * synchronized. It should be used on a state only when
1629 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1630 * the states like normal sync commits, but just do in-place changes on the
1631 * current state.
1632 */
1633void drm_atomic_helper_async_commit(struct drm_device *dev,
1634 struct drm_atomic_state *state)
1635{
1636 struct drm_plane *plane;
1637 struct drm_plane_state *plane_state;
1638 const struct drm_plane_helper_funcs *funcs;
1639 int i;
1640
1641 for_each_new_plane_in_state(state, plane, plane_state, i) {
1642 funcs = plane->helper_private;
1643 funcs->atomic_async_update(plane, plane_state);
Boris Brezillon02edfd92018-03-30 16:55:18 +02001644
1645 /*
1646 * ->atomic_async_update() is supposed to update the
1647 * plane->state in-place, make sure at least common
1648 * properties have been properly updated.
1649 */
1650 WARN_ON_ONCE(plane->state->fb != plane_state->fb);
1651 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1652 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1653 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1654 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001655 }
1656}
1657EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1658
1659/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001660 * drm_atomic_helper_commit - commit validated state object
1661 * @dev: DRM device
1662 * @state: the driver state object
1663 * @nonblock: whether nonblocking behavior is requested.
1664 *
1665 * This function commits a with drm_atomic_helper_check() pre-validated state
1666 * object. This can still fail when e.g. the framebuffer reservation fails. This
1667 * function implements nonblocking commits, using
1668 * drm_atomic_helper_setup_commit() and related functions.
1669 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001670 * Committing the actual hardware state is done through the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001671 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1672 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001673 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001674 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001675 * Zero for success or -errno.
1676 */
1677int drm_atomic_helper_commit(struct drm_device *dev,
1678 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001679 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001680{
1681 int ret;
1682
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001683 if (state->async_update) {
1684 ret = drm_atomic_helper_prepare_planes(dev, state);
1685 if (ret)
1686 return ret;
1687
1688 drm_atomic_helper_async_commit(dev, state);
1689 drm_atomic_helper_cleanup_planes(dev, state);
1690
1691 return 0;
1692 }
1693
Daniel Vettera095caa2016-06-08 17:15:36 +02001694 ret = drm_atomic_helper_setup_commit(state, nonblock);
1695 if (ret)
1696 return ret;
1697
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001698 INIT_WORK(&state->commit_work, commit_work);
1699
Daniel Vetter623369e2014-09-16 17:50:47 +02001700 ret = drm_atomic_helper_prepare_planes(dev, state);
1701 if (ret)
1702 return ret;
1703
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001704 if (!nonblock) {
1705 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001706 if (ret)
1707 goto err;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001708 }
1709
Daniel Vetter623369e2014-09-16 17:50:47 +02001710 /*
1711 * This is the point of no return - everything below never fails except
1712 * when the hw goes bonghits. Which means we can commit the new state on
1713 * the software side now.
1714 */
1715
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001716 ret = drm_atomic_helper_swap_state(state, true);
1717 if (ret)
1718 goto err;
Daniel Vetter623369e2014-09-16 17:50:47 +02001719
1720 /*
1721 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001722 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001723 * that the asynchronous work has either been cancelled (if the driver
1724 * supports it, which at least requires that the framebuffers get
1725 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1726 * before the new state gets committed on the software side with
1727 * drm_atomic_helper_swap_state().
1728 *
1729 * This scheme allows new atomic state updates to be prepared and
1730 * checked in parallel to the asynchronous completion of the previous
1731 * update. Which is important since compositors need to figure out the
1732 * composition of the next frame right after having submitted the
1733 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001734 *
1735 * NOTE: Commit work has multiple phases, first hardware commit, then
1736 * cleanup. We want them to overlap, hence need system_unbound_wq to
1737 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001738 */
1739
Chris Wilson08536952016-10-14 13:18:18 +01001740 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001741 if (nonblock)
1742 queue_work(system_unbound_wq, &state->commit_work);
1743 else
1744 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001745
1746 return 0;
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001747
1748err:
1749 drm_atomic_helper_cleanup_planes(dev, state);
1750 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +02001751}
1752EXPORT_SYMBOL(drm_atomic_helper_commit);
1753
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001754/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001755 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001756 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001757 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001758 *
1759 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1760 * which commit needs to call which can fail, so we want to run it first and
1761 * synchronously.
1762 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001763 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001764 * might be affected the new state update. This can be done by either cancelling
1765 * or flushing the work items, depending upon whether the driver can deal with
1766 * cancelled updates. Note that it is important to ensure that the framebuffer
1767 * cleanup is still done when cancelling.
1768 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001769 * Asynchronous workers need to have sufficient parallelism to be able to run
1770 * different atomic commits on different CRTCs in parallel. The simplest way to
1771 * achive this is by running them on the &system_unbound_wq work queue. Note
1772 * that drivers are not required to split up atomic commits and run an
1773 * individual commit in parallel - userspace is supposed to do that if it cares.
1774 * But it might be beneficial to do that for modesets, since those necessarily
1775 * must be done as one global operation, and enabling or disabling a CRTC can
1776 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001777 *
1778 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001779 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001780 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001781 * while it's guaranteed that no relevant nonblocking worker runs means that
1782 * nonblocking workers do not need grab any locks. Actually they must not grab
1783 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001784 *
1785 * 4. Schedule a work item to do all subsequent steps, using the split-out
1786 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1787 * then cleaning up the framebuffers after the old framebuffer is no longer
1788 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001789 *
1790 * The above scheme is implemented in the atomic helper libraries in
1791 * drm_atomic_helper_commit() using a bunch of helper functions. See
1792 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001793 */
1794
Daniel Vettera095caa2016-06-08 17:15:36 +02001795static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1796{
1797 struct drm_crtc_commit *commit, *stall_commit = NULL;
1798 bool completed = true;
1799 int i;
1800 long ret = 0;
1801
1802 spin_lock(&crtc->commit_lock);
1803 i = 0;
1804 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1805 if (i == 0) {
1806 completed = try_wait_for_completion(&commit->flip_done);
1807 /* Userspace is not allowed to get ahead of the previous
1808 * commit with nonblocking ones. */
1809 if (!completed && nonblock) {
1810 spin_unlock(&crtc->commit_lock);
1811 return -EBUSY;
1812 }
1813 } else if (i == 1) {
Maarten Lankhorstf46640b2017-09-04 12:48:36 +02001814 stall_commit = drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001815 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001816 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001817
1818 i++;
1819 }
1820 spin_unlock(&crtc->commit_lock);
1821
1822 if (!stall_commit)
1823 return 0;
1824
1825 /* We don't want to let commits get ahead of cleanup work too much,
1826 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1827 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001828 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001829 10*HZ);
1830 if (ret == 0)
1831 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1832 crtc->base.id, crtc->name);
1833
1834 drm_crtc_commit_put(stall_commit);
1835
1836 return ret < 0 ? ret : 0;
1837}
1838
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001839static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001840{
1841 struct drm_crtc_commit *commit = container_of(completion,
1842 typeof(*commit),
1843 flip_done);
1844
1845 drm_crtc_commit_put(commit);
1846}
1847
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001848static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1849{
1850 init_completion(&commit->flip_done);
1851 init_completion(&commit->hw_done);
1852 init_completion(&commit->cleanup_done);
1853 INIT_LIST_HEAD(&commit->commit_entry);
1854 kref_init(&commit->ref);
1855 commit->crtc = crtc;
1856}
1857
1858static struct drm_crtc_commit *
1859crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1860{
1861 if (crtc) {
1862 struct drm_crtc_state *new_crtc_state;
1863
1864 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1865
1866 return new_crtc_state->commit;
1867 }
1868
1869 if (!state->fake_commit) {
1870 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1871 if (!state->fake_commit)
1872 return NULL;
1873
1874 init_commit(state->fake_commit, NULL);
1875 }
1876
1877 return state->fake_commit;
1878}
1879
Daniel Vettera095caa2016-06-08 17:15:36 +02001880/**
1881 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1882 * @state: new modeset state to be committed
1883 * @nonblock: whether nonblocking behavior is requested.
1884 *
1885 * This function prepares @state to be used by the atomic helper's support for
1886 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001887 * should always call this function from their
1888 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001889 *
1890 * To be able to use this support drivers need to use a few more helper
1891 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1892 * actually committing the hardware state, and for nonblocking commits this call
1893 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1894 * and it's stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001895 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001896 *
1897 * Completion of the hardware commit step must be signalled using
1898 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1899 * to read or change any permanent software or hardware modeset state. The only
1900 * exception is state protected by other means than &drm_modeset_lock locks.
1901 * Only the free standing @state with pointers to the old state structures can
1902 * be inspected, e.g. to clean up old buffers using
1903 * drm_atomic_helper_cleanup_planes().
1904 *
1905 * At the very end, before cleaning up @state drivers must call
1906 * drm_atomic_helper_commit_cleanup_done().
1907 *
1908 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
Thierry Reding9ac07812017-10-12 16:06:16 +02001909 * complete and easy-to-use default implementation of the atomic_commit() hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001910 *
1911 * The tracking of asynchronously executed and still pending commits is done
1912 * using the core structure &drm_crtc_commit.
1913 *
1914 * By default there's no need to clean up resources allocated by this function
1915 * explicitly: drm_atomic_state_default_clear() will take care of that
1916 * automatically.
1917 *
1918 * Returns:
1919 *
1920 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1921 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1922 */
1923int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1924 bool nonblock)
1925{
1926 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001927 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001928 struct drm_connector *conn;
1929 struct drm_connector_state *old_conn_state, *new_conn_state;
1930 struct drm_plane *plane;
1931 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001932 struct drm_crtc_commit *commit;
1933 int i, ret;
1934
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001935 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001936 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1937 if (!commit)
1938 return -ENOMEM;
1939
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001940 init_commit(commit, crtc);
Daniel Vettera095caa2016-06-08 17:15:36 +02001941
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02001942 new_crtc_state->commit = commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001943
1944 ret = stall_checks(crtc, nonblock);
1945 if (ret)
1946 return ret;
1947
1948 /* Drivers only send out events when at least either current or
1949 * new CRTC state is active. Complete right away if everything
1950 * stays off. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001951 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001952 complete_all(&commit->flip_done);
1953 continue;
1954 }
1955
1956 /* Legacy cursor updates are fully unsynced. */
1957 if (state->legacy_cursor_update) {
1958 complete_all(&commit->flip_done);
1959 continue;
1960 }
1961
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001962 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001963 commit->event = kzalloc(sizeof(*commit->event),
1964 GFP_KERNEL);
1965 if (!commit->event)
1966 return -ENOMEM;
1967
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001968 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02001969 }
1970
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001971 new_crtc_state->event->base.completion = &commit->flip_done;
1972 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01001973 drm_crtc_commit_get(commit);
Leo (Sunpeng) Li1c6ceee2018-01-17 12:51:08 +01001974
1975 commit->abort_completion = true;
Leo Li4364bcb2018-10-15 09:46:40 -04001976
1977 state->crtcs[i].commit = commit;
1978 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001979 }
1980
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001981 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001982 /* Userspace is not allowed to get ahead of the previous
1983 * commit with nonblocking ones. */
1984 if (nonblock && old_conn_state->commit &&
1985 !try_wait_for_completion(&old_conn_state->commit->flip_done))
1986 return -EBUSY;
1987
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01001988 /* Always track connectors explicitly for e.g. link retraining. */
1989 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001990 if (!commit)
1991 return -ENOMEM;
1992
1993 new_conn_state->commit = drm_crtc_commit_get(commit);
1994 }
1995
1996 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001997 /* Userspace is not allowed to get ahead of the previous
1998 * commit with nonblocking ones. */
1999 if (nonblock && old_plane_state->commit &&
2000 !try_wait_for_completion(&old_plane_state->commit->flip_done))
2001 return -EBUSY;
2002
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01002003 /* Always track planes explicitly for async pageflip support. */
Maarten Lankhorst4edd6082017-10-17 07:20:47 +02002004 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002005 if (!commit)
2006 return -ENOMEM;
2007
2008 new_plane_state->commit = drm_crtc_commit_get(commit);
2009 }
2010
Daniel Vettera095caa2016-06-08 17:15:36 +02002011 return 0;
2012}
2013EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2014
Daniel Vettera095caa2016-06-08 17:15:36 +02002015/**
2016 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002017 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002018 *
2019 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002020 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02002021 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
Thierry Reding277b09c2017-10-12 16:08:57 +02002022 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02002023 *
2024 * This is part of the atomic helper support for nonblocking commits, see
2025 * drm_atomic_helper_setup_commit() for an overview.
2026 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002027void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002028{
2029 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002030 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002031 struct drm_plane *plane;
2032 struct drm_plane_state *old_plane_state;
2033 struct drm_connector *conn;
2034 struct drm_connector_state *old_conn_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002035 struct drm_crtc_commit *commit;
2036 int i;
2037 long ret;
2038
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002039 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2040 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002041
2042 if (!commit)
2043 continue;
2044
2045 ret = wait_for_completion_timeout(&commit->hw_done,
2046 10*HZ);
2047 if (ret == 0)
2048 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2049 crtc->base.id, crtc->name);
2050
2051 /* Currently no support for overwriting flips, hence
2052 * stall for previous one to execute completely. */
2053 ret = wait_for_completion_timeout(&commit->flip_done,
2054 10*HZ);
2055 if (ret == 0)
2056 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2057 crtc->base.id, crtc->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002058 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002059
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002060 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2061 commit = old_conn_state->commit;
2062
2063 if (!commit)
2064 continue;
2065
2066 ret = wait_for_completion_timeout(&commit->hw_done,
2067 10*HZ);
2068 if (ret == 0)
2069 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2070 conn->base.id, conn->name);
2071
2072 /* Currently no support for overwriting flips, hence
2073 * stall for previous one to execute completely. */
2074 ret = wait_for_completion_timeout(&commit->flip_done,
2075 10*HZ);
2076 if (ret == 0)
2077 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2078 conn->base.id, conn->name);
2079 }
2080
2081 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2082 commit = old_plane_state->commit;
2083
2084 if (!commit)
2085 continue;
2086
2087 ret = wait_for_completion_timeout(&commit->hw_done,
2088 10*HZ);
2089 if (ret == 0)
2090 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2091 plane->base.id, plane->name);
2092
2093 /* Currently no support for overwriting flips, hence
2094 * stall for previous one to execute completely. */
2095 ret = wait_for_completion_timeout(&commit->flip_done,
2096 10*HZ);
2097 if (ret == 0)
2098 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2099 plane->base.id, plane->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002100 }
2101}
2102EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2103
2104/**
Boris Brezillonb25c60a2018-07-03 09:50:19 +02002105 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2106 * @old_state: atomic state object with old state structures
2107 *
2108 * This function walks all CRTCs and fake VBLANK events on those with
2109 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2110 * The primary use of this function is writeback connectors working in oneshot
2111 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2112 * when a job is queued, and any change to the pipeline that does not touch the
2113 * connector is leading to timeouts when calling
2114 * drm_atomic_helper_wait_for_vblanks() or
2115 * drm_atomic_helper_wait_for_flip_done().
2116 *
2117 * This is part of the atomic helper support for nonblocking commits, see
2118 * drm_atomic_helper_setup_commit() for an overview.
2119 */
2120void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2121{
2122 struct drm_crtc_state *new_crtc_state;
2123 struct drm_crtc *crtc;
2124 int i;
2125
2126 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2127 unsigned long flags;
2128
2129 if (!new_crtc_state->no_vblank)
2130 continue;
2131
2132 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2133 if (new_crtc_state->event) {
2134 drm_crtc_send_vblank_event(crtc,
2135 new_crtc_state->event);
2136 new_crtc_state->event = NULL;
2137 }
2138 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2139 }
2140}
2141EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2142
2143/**
Daniel Vettera095caa2016-06-08 17:15:36 +02002144 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002145 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002146 *
2147 * This function is used to signal completion of the hardware commit step. After
2148 * this step the driver is not allowed to read or change any permanent software
2149 * or hardware modeset state. The only exception is state protected by other
2150 * means than &drm_modeset_lock locks.
2151 *
2152 * Drivers should try to postpone any expensive or delayed cleanup work after
2153 * this function is called.
2154 *
2155 * This is part of the atomic helper support for nonblocking commits, see
2156 * drm_atomic_helper_setup_commit() for an overview.
2157 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002158void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002159{
2160 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002161 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002162 struct drm_crtc_commit *commit;
2163 int i;
2164
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002165 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2166 commit = new_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002167 if (!commit)
2168 continue;
2169
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002170 /*
2171 * copy new_crtc_state->commit to old_crtc_state->commit,
2172 * it's unsafe to touch new_crtc_state after hw_done,
2173 * but we still need to do so in cleanup_done().
2174 */
2175 if (old_crtc_state->commit)
2176 drm_crtc_commit_put(old_crtc_state->commit);
2177
2178 old_crtc_state->commit = drm_crtc_commit_get(commit);
2179
Daniel Vettera095caa2016-06-08 17:15:36 +02002180 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002181 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02002182 complete_all(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002183 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002184
2185 if (old_state->fake_commit) {
2186 complete_all(&old_state->fake_commit->hw_done);
2187 complete_all(&old_state->fake_commit->flip_done);
2188 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002189}
2190EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2191
2192/**
2193 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002194 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002195 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002196 * This signals completion of the atomic update @old_state, including any
2197 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01002198 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02002199 *
2200 * This is part of the atomic helper support for nonblocking commits, see
2201 * drm_atomic_helper_setup_commit() for an overview.
2202 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002203void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002204{
2205 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002206 struct drm_crtc_state *old_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002207 struct drm_crtc_commit *commit;
2208 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02002209
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002210 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2211 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002212 if (WARN_ON(!commit))
2213 continue;
2214
Daniel Vettera095caa2016-06-08 17:15:36 +02002215 complete_all(&commit->cleanup_done);
2216 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2217
Daniel Vetter7141fd32017-06-21 11:16:27 +02002218 spin_lock(&crtc->commit_lock);
Daniel Vettera095caa2016-06-08 17:15:36 +02002219 list_del(&commit->commit_entry);
2220 spin_unlock(&crtc->commit_lock);
2221 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002222
Ville Syrjälä10a599f2018-11-22 16:34:12 +02002223 if (old_state->fake_commit) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002224 complete_all(&old_state->fake_commit->cleanup_done);
Ville Syrjälä10a599f2018-11-22 16:34:12 +02002225 WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2226 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002227}
2228EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2229
Daniel Vettere8c833a2014-07-27 18:30:19 +02002230/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002231 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002232 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002233 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002234 *
2235 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002236 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2237 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2238 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002239 *
2240 * Returns:
2241 * 0 on success, negative error code on failure.
2242 */
2243int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2244 struct drm_atomic_state *state)
2245{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002246 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002247 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002248 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002249
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002250 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002251 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002252
2253 funcs = plane->helper_private;
2254
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002255 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002256 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002257 if (ret)
2258 goto fail;
2259 }
2260 }
2261
2262 return 0;
2263
2264fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002265 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002266 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002267
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002268 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002269 continue;
2270
2271 funcs = plane->helper_private;
2272
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002273 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002274 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002275 }
2276
2277 return ret;
2278}
2279EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2280
Ville Syrjälä7135ac52016-09-19 16:33:42 +03002281static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002282{
2283 return state->crtc && state->crtc->state->active;
2284}
2285
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002286/**
2287 * drm_atomic_helper_commit_planes - commit plane state
2288 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002289 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08002290 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002291 *
2292 * This function commits the new plane state using the plane and atomic helper
2293 * functions for planes and crtcs. It assumes that the atomic state has already
2294 * been pushed into the relevant object state pointers, since this step can no
2295 * longer fail.
2296 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002297 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002298 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002299 *
2300 * Note that this function does all plane updates across all CRTCs in one step.
2301 * If the hardware can't support this approach look at
2302 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002303 *
2304 * Plane parameters can be updated by applications while the associated CRTC is
2305 * disabled. The DRM/KMS core will store the parameters in the plane state,
2306 * which will be available to the driver when the CRTC is turned on. As a result
2307 * most drivers don't need to be immediately notified of plane updates for a
2308 * disabled CRTC.
2309 *
Liu Ying2b58e982016-08-29 17:12:03 +08002310 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2311 * @flags in order not to receive plane update notifications related to a
2312 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002313 * driver code when the driver and/or hardware can't or just don't need to deal
2314 * with updates on disabled CRTCs, for example when supporting runtime PM.
2315 *
Liu Ying2b58e982016-08-29 17:12:03 +08002316 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2317 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002318 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2319 * call for a plane if the CRTC of the old plane state needs a modesetting
2320 * operation. Of course, the drivers need to disable the planes in their CRTC
2321 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08002322 *
2323 * The drm_atomic_helper_commit() default implementation doesn't set the
2324 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2325 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002326 */
2327void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002328 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08002329 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002330{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002331 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002332 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002333 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002334 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002335 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08002336 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2337 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002338
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002339 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002340 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002341
2342 funcs = crtc->helper_private;
2343
2344 if (!funcs || !funcs->atomic_begin)
2345 continue;
2346
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002347 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002348 continue;
2349
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002350 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002351 }
2352
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002353 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002354 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002355 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002356
2357 funcs = plane->helper_private;
2358
Thierry Reding3cad4b62014-11-25 13:05:12 +01002359 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002360 continue;
2361
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01002362 disabling = drm_atomic_plane_disabling(old_plane_state,
2363 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002364
2365 if (active_only) {
2366 /*
2367 * Skip planes related to inactive CRTCs. If the plane
2368 * is enabled use the state of the current CRTC. If the
2369 * plane is being disabled use the state of the old
2370 * CRTC to avoid skipping planes being disabled on an
2371 * active CRTC.
2372 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002373 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002374 continue;
2375 if (disabling && !plane_crtc_active(old_plane_state))
2376 continue;
2377 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002378
Thierry Reding407b8bd2014-11-20 12:05:50 +01002379 /*
2380 * Special-case disabling the plane if drivers support it.
2381 */
Liu Ying2b58e982016-08-29 17:12:03 +08002382 if (disabling && funcs->atomic_disable) {
2383 struct drm_crtc_state *crtc_state;
2384
2385 crtc_state = old_plane_state->crtc->state;
2386
2387 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2388 no_disable)
2389 continue;
2390
Thierry Reding407b8bd2014-11-20 12:05:50 +01002391 funcs->atomic_disable(plane, old_plane_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002392 } else if (new_plane_state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01002393 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08002394 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002395 }
2396
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002397 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002398 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002399
2400 funcs = crtc->helper_private;
2401
2402 if (!funcs || !funcs->atomic_flush)
2403 continue;
2404
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002405 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002406 continue;
2407
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002408 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002409 }
2410}
2411EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2412
2413/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002414 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2415 * @old_crtc_state: atomic state object with the old crtc state
2416 *
2417 * This function commits the new plane state using the plane and atomic helper
2418 * functions for planes on the specific crtc. It assumes that the atomic state
2419 * has already been pushed into the relevant object state pointers, since this
2420 * step can no longer fail.
2421 *
2422 * This function is useful when plane updates should be done crtc-by-crtc
2423 * instead of one global step like drm_atomic_helper_commit_planes() does.
2424 *
2425 * This function can only be savely used when planes are not allowed to move
2426 * between different CRTCs because this function doesn't handle inter-CRTC
2427 * depencies. Callers need to ensure that either no such depencies exist,
2428 * resolve them through ordering of commit calls or through some other means.
2429 */
2430void
2431drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2432{
2433 const struct drm_crtc_helper_funcs *crtc_funcs;
2434 struct drm_crtc *crtc = old_crtc_state->crtc;
2435 struct drm_atomic_state *old_state = old_crtc_state->state;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002436 struct drm_crtc_state *new_crtc_state =
2437 drm_atomic_get_new_crtc_state(old_state, crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002438 struct drm_plane *plane;
2439 unsigned plane_mask;
2440
2441 plane_mask = old_crtc_state->plane_mask;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002442 plane_mask |= new_crtc_state->plane_mask;
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002443
2444 crtc_funcs = crtc->helper_private;
2445 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002446 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002447
2448 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2449 struct drm_plane_state *old_plane_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01002450 drm_atomic_get_old_plane_state(old_state, plane);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002451 struct drm_plane_state *new_plane_state =
2452 drm_atomic_get_new_plane_state(old_state, plane);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002453 const struct drm_plane_helper_funcs *plane_funcs;
2454
2455 plane_funcs = plane->helper_private;
2456
2457 if (!old_plane_state || !plane_funcs)
2458 continue;
2459
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002460 WARN_ON(new_plane_state->crtc &&
2461 new_plane_state->crtc != crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002462
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002463 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002464 plane_funcs->atomic_disable)
2465 plane_funcs->atomic_disable(plane, old_plane_state);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002466 else if (new_plane_state->crtc ||
2467 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002468 plane_funcs->atomic_update(plane, old_plane_state);
2469 }
2470
2471 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002472 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002473}
2474EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2475
2476/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02002477 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08002478 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02002479 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2480 *
2481 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08002482 * used for instance in the CRTC helper atomic_disable callback to disable
2483 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002484 *
2485 * If the atomic-parameter is set the function calls the CRTC's
2486 * atomic_begin hook before and atomic_flush hook after disabling the
2487 * planes.
2488 *
2489 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002490 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002491 */
Liu Ying28500292016-08-26 15:30:39 +08002492void
2493drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2494 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002495{
Liu Ying28500292016-08-26 15:30:39 +08002496 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02002497 const struct drm_crtc_helper_funcs *crtc_funcs =
2498 crtc->helper_private;
2499 struct drm_plane *plane;
2500
2501 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2502 crtc_funcs->atomic_begin(crtc, NULL);
2503
Liu Ying28500292016-08-26 15:30:39 +08002504 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02002505 const struct drm_plane_helper_funcs *plane_funcs =
2506 plane->helper_private;
2507
Liu Ying28500292016-08-26 15:30:39 +08002508 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002509 continue;
2510
2511 WARN_ON(!plane_funcs->atomic_disable);
2512 if (plane_funcs->atomic_disable)
2513 plane_funcs->atomic_disable(plane, NULL);
2514 }
2515
2516 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2517 crtc_funcs->atomic_flush(crtc, NULL);
2518}
2519EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2520
2521/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002522 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2523 * @dev: DRM device
2524 * @old_state: atomic state object with old state structures
2525 *
2526 * This function cleans up plane state, specifically framebuffers, from the old
2527 * configuration. Hence the old configuration must be perserved in @old_state to
2528 * be able to call this function.
2529 *
2530 * This function must also be called on the new state when the atomic update
2531 * fails at any point after calling drm_atomic_helper_prepare_planes().
2532 */
2533void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2534 struct drm_atomic_state *old_state)
2535{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002536 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002537 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002538 int i;
2539
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002540 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002541 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002542 struct drm_plane_state *plane_state;
2543
2544 /*
2545 * This might be called before swapping when commit is aborted,
2546 * in which case we have to cleanup the new state.
2547 */
2548 if (old_plane_state == plane->state)
2549 plane_state = new_plane_state;
2550 else
2551 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002552
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002553 funcs = plane->helper_private;
2554
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002555 if (funcs->cleanup_fb)
2556 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002557 }
2558}
2559EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2560
2561/**
2562 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002563 * @state: atomic state
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002564 * @stall: stall for preceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002565 *
2566 * This function stores the atomic state into the current state pointers in all
2567 * driver objects. It should be called after all failing steps have been done
2568 * and succeeded, but before the actual hardware state is committed.
2569 *
2570 * For cleanup and error recovery the current state for all changed objects will
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002571 * be swapped into @state.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002572 *
2573 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2574 *
2575 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2576 *
2577 * 2. Do any other steps that might fail.
2578 *
2579 * 3. Put the staged state into the current state pointers with this function.
2580 *
2581 * 4. Actually commit the hardware state.
2582 *
Daniel Vetter26196f72015-08-25 16:26:03 +02002583 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002584 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02002585 *
2586 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002587 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2588 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02002589 * don't pass the right state structures to the callbacks.
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002590 *
2591 * Returns:
2592 *
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002593 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2594 * waiting for the previous commits has been interrupted.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002595 */
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002596int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
Daniel Vetter5e84c262016-06-10 00:06:32 +02002597 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002598{
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002599 int i, ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002600 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002601 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002602 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002603 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002604 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002605 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002606 struct drm_crtc_commit *commit;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002607 struct drm_private_obj *obj;
2608 struct drm_private_state *old_obj_state, *new_obj_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002609
2610 if (stall) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002611 /*
2612 * We have to stall for hw_done here before
2613 * drm_atomic_helper_wait_for_dependencies() because flip
2614 * depth > 1 is not yet supported by all drivers. As long as
2615 * obj->state is directly dereferenced anywhere in the drivers
2616 * atomic_commit_tail function, then it's unsafe to swap state
2617 * before drm_atomic_helper_commit_hw_done() is called.
2618 */
2619
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002620 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2621 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002622
2623 if (!commit)
2624 continue;
2625
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002626 ret = wait_for_completion_interruptible(&commit->hw_done);
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002627 if (ret)
2628 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002629 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002630
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002631 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2632 commit = old_conn_state->commit;
2633
2634 if (!commit)
2635 continue;
2636
2637 ret = wait_for_completion_interruptible(&commit->hw_done);
2638 if (ret)
2639 return ret;
2640 }
2641
2642 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2643 commit = old_plane_state->commit;
2644
2645 if (!commit)
2646 continue;
2647
2648 ret = wait_for_completion_interruptible(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002649 if (ret)
2650 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002651 }
2652 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002653
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002654 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002655 WARN_ON(connector->state != old_conn_state);
2656
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002657 old_conn_state->state = state;
2658 new_conn_state->state = NULL;
2659
2660 state->connectors[i].state = old_conn_state;
2661 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002662 }
2663
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002664 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002665 WARN_ON(crtc->state != old_crtc_state);
2666
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002667 old_crtc_state->state = state;
2668 new_crtc_state->state = NULL;
2669
2670 state->crtcs[i].state = old_crtc_state;
2671 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002672
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002673 if (new_crtc_state->commit) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002674 spin_lock(&crtc->commit_lock);
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002675 list_add(&new_crtc_state->commit->commit_entry,
Daniel Vettera095caa2016-06-08 17:15:36 +02002676 &crtc->commit_list);
2677 spin_unlock(&crtc->commit_lock);
2678
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002679 new_crtc_state->commit->event = NULL;
Daniel Vettera095caa2016-06-08 17:15:36 +02002680 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002681 }
2682
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002683 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002684 WARN_ON(plane->state != old_plane_state);
2685
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002686 old_plane_state->state = state;
2687 new_plane_state->state = NULL;
2688
2689 state->planes[i].state = old_plane_state;
2690 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002691 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07002692
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002693 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2694 WARN_ON(obj->state != old_obj_state);
2695
2696 old_obj_state->state = state;
2697 new_obj_state->state = NULL;
2698
2699 state->private_objs[i].state = old_obj_state;
2700 obj->state = new_obj_state;
2701 }
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002702
2703 return 0;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002704}
2705EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002706
2707/**
2708 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2709 * @plane: plane object to update
2710 * @crtc: owning CRTC of owning plane
2711 * @fb: framebuffer to flip onto plane
2712 * @crtc_x: x offset of primary plane on crtc
2713 * @crtc_y: y offset of primary plane on crtc
2714 * @crtc_w: width of primary plane rectangle on crtc
2715 * @crtc_h: height of primary plane rectangle on crtc
2716 * @src_x: x offset of @fb for panning
2717 * @src_y: y offset of @fb for panning
2718 * @src_w: width of source rectangle in @fb
2719 * @src_h: height of source rectangle in @fb
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002720 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002721 *
2722 * Provides a default plane update handler using the atomic driver interface.
2723 *
2724 * RETURNS:
2725 * Zero on success, error code on failure
2726 */
2727int drm_atomic_helper_update_plane(struct drm_plane *plane,
2728 struct drm_crtc *crtc,
2729 struct drm_framebuffer *fb,
2730 int crtc_x, int crtc_y,
2731 unsigned int crtc_w, unsigned int crtc_h,
2732 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002733 uint32_t src_w, uint32_t src_h,
2734 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002735{
2736 struct drm_atomic_state *state;
2737 struct drm_plane_state *plane_state;
2738 int ret = 0;
2739
2740 state = drm_atomic_state_alloc(plane->dev);
2741 if (!state)
2742 return -ENOMEM;
2743
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002744 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002745 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 Vetter042652e2014-07-27 13:46:52 +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 Vetter042652e2014-07-27 13:46:52 +02002755 plane_state->crtc_x = crtc_x;
2756 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002757 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002758 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002759 plane_state->src_x = src_x;
2760 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002761 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002762 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002763
Daniel Vetter3671c582015-05-04 15:40:52 +02002764 if (plane == crtc->cursor)
2765 state->legacy_cursor_update = true;
2766
Daniel Vetter042652e2014-07-27 13:46:52 +02002767 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002768fail:
Chris Wilson08536952016-10-14 13:18:18 +01002769 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002770 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002771}
2772EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2773
2774/**
2775 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2776 * @plane: plane to disable
Daniel Vetter19315292017-03-22 22:50:43 +01002777 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002778 *
2779 * Provides a default plane disable handler using the atomic driver interface.
2780 *
2781 * RETURNS:
2782 * Zero on success, error code on failure
2783 */
Daniel Vetter19315292017-03-22 22:50:43 +01002784int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2785 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002786{
2787 struct drm_atomic_state *state;
2788 struct drm_plane_state *plane_state;
2789 int ret = 0;
2790
2791 state = drm_atomic_state_alloc(plane->dev);
2792 if (!state)
2793 return -ENOMEM;
2794
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002795 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002796 plane_state = drm_atomic_get_plane_state(state, plane);
2797 if (IS_ERR(plane_state)) {
2798 ret = PTR_ERR(plane_state);
2799 goto fail;
2800 }
2801
Ville Syrjäläa36c0272018-03-22 17:22:58 +02002802 if (plane_state->crtc && plane_state->crtc->cursor == plane)
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002803 plane_state->state->legacy_cursor_update = true;
2804
Rob Clarkbbb1e522015-08-25 15:35:58 -04002805 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002806 if (ret != 0)
2807 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002808
Daniel Vetter042652e2014-07-27 13:46:52 +02002809 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002810fail:
Chris Wilson08536952016-10-14 13:18:18 +01002811 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002812 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002813}
2814EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2815
Rob Clarkbbb1e522015-08-25 15:35:58 -04002816/* just used from fb-helper and atomic-helper: */
2817int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2818 struct drm_plane_state *plane_state)
2819{
2820 int ret;
2821
2822 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2823 if (ret != 0)
2824 return ret;
2825
2826 drm_atomic_set_fb_for_plane(plane_state, NULL);
2827 plane_state->crtc_x = 0;
2828 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002829 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002830 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002831 plane_state->src_x = 0;
2832 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002833 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002834 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002835
Rob Clarkbbb1e522015-08-25 15:35:58 -04002836 return 0;
2837}
2838
Daniel Vetter042652e2014-07-27 13:46:52 +02002839static int update_output_state(struct drm_atomic_state *state,
2840 struct drm_mode_set *set)
2841{
2842 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002843 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002844 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002845 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002846 struct drm_connector_state *new_conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002847 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002848
2849 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2850 state->acquire_ctx);
2851 if (ret)
2852 return ret;
2853
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002854 /* First disable all connectors on the target crtc. */
2855 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2856 if (ret)
2857 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002858
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002859 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2860 if (new_conn_state->crtc == set->crtc) {
2861 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Daniel Vetter042652e2014-07-27 13:46:52 +02002862 NULL);
2863 if (ret)
2864 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002865
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002866 /* Make sure legacy setCrtc always re-trains */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002867 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
Daniel Vetter042652e2014-07-27 13:46:52 +02002868 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002869 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002870
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002871 /* Then set all connectors from set->connectors on the target crtc */
2872 for (i = 0; i < set->num_connectors; i++) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002873 new_conn_state = drm_atomic_get_connector_state(state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002874 set->connectors[i]);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002875 if (IS_ERR(new_conn_state))
2876 return PTR_ERR(new_conn_state);
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002877
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002878 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002879 set->crtc);
2880 if (ret)
2881 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002882 }
2883
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002884 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002885 /* Don't update ->enable for the CRTC in the set_config request,
2886 * since a mismatch would indicate a bug in the upper layers.
2887 * The actual modeset code later on will catch any
2888 * inconsistencies here. */
2889 if (crtc == set->crtc)
2890 continue;
2891
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002892 if (!new_crtc_state->connector_mask) {
2893 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002894 NULL);
2895 if (ret < 0)
2896 return ret;
2897
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002898 new_crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002899 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002900 }
2901
2902 return 0;
2903}
2904
2905/**
2906 * drm_atomic_helper_set_config - set a new config from userspace
2907 * @set: mode set configuration
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002908 * @ctx: lock acquisition context
Daniel Vetter042652e2014-07-27 13:46:52 +02002909 *
2910 * Provides a default crtc set_config handler using the atomic driver interface.
2911 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002912 * NOTE: For backwards compatibility with old userspace this automatically
2913 * resets the "link-status" property to GOOD, to force any link
2914 * re-training. The SETCRTC ioctl does not define whether an update does
2915 * need a full modeset or just a plane update, hence we're allowed to do
Daniel Vetter97e14fb2018-07-09 10:40:08 +02002916 * that. See also drm_connector_set_link_status_property().
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002917 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002918 * Returns:
2919 * Returns 0 on success, negative errno numbers on failure.
2920 */
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002921int drm_atomic_helper_set_config(struct drm_mode_set *set,
2922 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002923{
2924 struct drm_atomic_state *state;
2925 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002926 int ret = 0;
2927
2928 state = drm_atomic_state_alloc(crtc->dev);
2929 if (!state)
2930 return -ENOMEM;
2931
Daniel Vetter38b64412017-03-22 22:50:58 +01002932 state->acquire_ctx = ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002933 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002934 if (ret != 0)
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002935 goto fail;
Daniel Stone819364d2015-05-26 14:36:48 +01002936
Maarten Lankhorst44596b82017-04-06 13:19:00 +02002937 ret = handle_conflicting_encoders(state, true);
2938 if (ret)
2939 return ret;
2940
Daniel Vetter042652e2014-07-27 13:46:52 +02002941 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002942
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002943fail:
Chris Wilson08536952016-10-14 13:18:18 +01002944 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002945 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002946}
2947EXPORT_SYMBOL(drm_atomic_helper_set_config);
2948
Rob Clarkbbb1e522015-08-25 15:35:58 -04002949/* just used from fb-helper and atomic-helper: */
2950int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2951 struct drm_atomic_state *state)
2952{
2953 struct drm_crtc_state *crtc_state;
2954 struct drm_plane_state *primary_state;
2955 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002956 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002957 int ret;
2958
2959 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2960 if (IS_ERR(crtc_state))
2961 return PTR_ERR(crtc_state);
2962
2963 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2964 if (IS_ERR(primary_state))
2965 return PTR_ERR(primary_state);
2966
2967 if (!set->mode) {
2968 WARN_ON(set->fb);
2969 WARN_ON(set->num_connectors);
2970
2971 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2972 if (ret != 0)
2973 return ret;
2974
2975 crtc_state->active = false;
2976
2977 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2978 if (ret != 0)
2979 return ret;
2980
2981 drm_atomic_set_fb_for_plane(primary_state, NULL);
2982
2983 goto commit;
2984 }
2985
2986 WARN_ON(!set->fb);
2987 WARN_ON(!set->num_connectors);
2988
2989 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2990 if (ret != 0)
2991 return ret;
2992
2993 crtc_state->active = true;
2994
2995 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2996 if (ret != 0)
2997 return ret;
2998
Daniel Vetter196cd5d2017-01-25 07:26:56 +01002999 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Ville Syrjälä83926112015-11-16 17:02:34 +02003000
Rob Clarkbbb1e522015-08-25 15:35:58 -04003001 drm_atomic_set_fb_for_plane(primary_state, set->fb);
3002 primary_state->crtc_x = 0;
3003 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02003004 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02003005 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04003006 primary_state->src_x = set->x << 16;
3007 primary_state->src_y = set->y << 16;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003008 if (drm_rotation_90_or_270(primary_state->rotation)) {
Ville Syrjälä83926112015-11-16 17:02:34 +02003009 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02003010 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03003011 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02003012 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02003013 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03003014 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04003015
3016commit:
3017 ret = update_output_state(state, set);
3018 if (ret)
3019 return ret;
3020
3021 return 0;
3022}
3023
Ville Syrjälä5e9cfeb2018-03-22 17:22:51 +02003024static int __drm_atomic_helper_disable_all(struct drm_device *dev,
3025 struct drm_modeset_acquire_ctx *ctx,
3026 bool clean_old_fbs)
Thierry Reding14942762015-12-02 17:50:04 +01003027{
3028 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003029 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01003030 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003031 struct drm_plane_state *plane_state;
3032 struct drm_plane *plane;
3033 struct drm_crtc_state *crtc_state;
3034 struct drm_crtc *crtc;
3035 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01003036
3037 state = drm_atomic_state_alloc(dev);
3038 if (!state)
3039 return -ENOMEM;
3040
3041 state->acquire_ctx = ctx;
3042
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003043 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01003044 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3045 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003046 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01003047 goto free;
3048 }
3049
3050 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003051
3052 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3053 if (ret < 0)
3054 goto free;
3055
3056 ret = drm_atomic_add_affected_planes(state, crtc);
3057 if (ret < 0)
3058 goto free;
3059
3060 ret = drm_atomic_add_affected_connectors(state, crtc);
3061 if (ret < 0)
3062 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01003063 }
3064
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02003065 for_each_new_connector_in_state(state, conn, conn_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003066 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3067 if (ret < 0)
3068 goto free;
3069 }
3070
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02003071 for_each_new_plane_in_state(state, plane, plane_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003072 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3073 if (ret < 0)
3074 goto free;
3075
3076 drm_atomic_set_fb_for_plane(plane_state, NULL);
3077 }
3078
3079 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01003080free:
Chris Wilson08536952016-10-14 13:18:18 +01003081 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003082 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01003083}
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003084
Ville Syrjälä5e9cfeb2018-03-22 17:22:51 +02003085/**
3086 * drm_atomic_helper_disable_all - disable all currently active outputs
3087 * @dev: DRM device
3088 * @ctx: lock acquisition context
3089 *
3090 * Loops through all connectors, finding those that aren't turned off and then
3091 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3092 * that they are connected to.
3093 *
3094 * This is used for example in suspend/resume to disable all currently active
3095 * functions when suspending. If you just want to shut down everything at e.g.
3096 * driver unload, look at drm_atomic_helper_shutdown().
3097 *
3098 * Note that if callers haven't already acquired all modeset locks this might
3099 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3100 *
3101 * Returns:
3102 * 0 on success or a negative error code on failure.
3103 *
3104 * See also:
3105 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3106 * drm_atomic_helper_shutdown().
3107 */
3108int drm_atomic_helper_disable_all(struct drm_device *dev,
3109 struct drm_modeset_acquire_ctx *ctx)
3110{
3111 return __drm_atomic_helper_disable_all(dev, ctx, false);
3112}
Thierry Reding14942762015-12-02 17:50:04 +01003113EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3114
3115/**
Daniel Vetter18dddad2017-03-21 17:41:49 +01003116 * drm_atomic_helper_shutdown - shutdown all CRTC
3117 * @dev: DRM device
3118 *
3119 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3120 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3121 * that also takes a snapshot of the modeset state to be restored on resume.
3122 *
3123 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3124 * and it is the atomic version of drm_crtc_force_disable_all().
3125 */
3126void drm_atomic_helper_shutdown(struct drm_device *dev)
3127{
3128 struct drm_modeset_acquire_ctx ctx;
3129 int ret;
3130
Sean Paulb7ea04d2018-11-29 10:04:17 -05003131 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003132
Sean Paulb7ea04d2018-11-29 10:04:17 -05003133 ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003134 if (ret)
3135 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3136
Sean Paulb7ea04d2018-11-29 10:04:17 -05003137 DRM_MODESET_LOCK_ALL_END(ctx, ret);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003138}
3139EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3140
3141/**
Thierry Reding14942762015-12-02 17:50:04 +01003142 * drm_atomic_helper_suspend - subsystem-level suspend helper
3143 * @dev: DRM device
3144 *
3145 * Duplicates the current atomic state, disables all active outputs and then
3146 * returns a pointer to the original atomic state to the caller. Drivers can
3147 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3148 * restore the output configuration that was active at the time the system
3149 * entered suspend.
3150 *
3151 * Note that it is potentially unsafe to use this. The atomic state object
3152 * returned by this function is assumed to be persistent. Drivers must ensure
3153 * that this holds true. Before calling this function, drivers must make sure
3154 * to suspend fbdev emulation so that nothing can be using the device.
3155 *
3156 * Returns:
3157 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3158 * encoded error code on failure. Drivers should store the returned atomic
3159 * state object and pass it to the drm_atomic_helper_resume() helper upon
3160 * resume.
3161 *
3162 * See also:
3163 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003164 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01003165 */
3166struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3167{
3168 struct drm_modeset_acquire_ctx ctx;
3169 struct drm_atomic_state *state;
3170 int err;
3171
Sean Paul615aa3d2018-11-29 15:36:48 -05003172 /* This can never be returned, but it makes the compiler happy */
3173 state = ERR_PTR(-EINVAL);
3174
Sean Paulb7ea04d2018-11-29 10:04:17 -05003175 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
Thierry Reding14942762015-12-02 17:50:04 +01003176
3177 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3178 if (IS_ERR(state))
3179 goto unlock;
3180
3181 err = drm_atomic_helper_disable_all(dev, &ctx);
3182 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003183 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01003184 state = ERR_PTR(err);
3185 goto unlock;
3186 }
3187
3188unlock:
Sean Paulb7ea04d2018-11-29 10:04:17 -05003189 DRM_MODESET_LOCK_ALL_END(ctx, err);
3190 if (err)
3191 return ERR_PTR(err);
Thierry Reding14942762015-12-02 17:50:04 +01003192
Thierry Reding14942762015-12-02 17:50:04 +01003193 return state;
3194}
3195EXPORT_SYMBOL(drm_atomic_helper_suspend);
3196
3197/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003198 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3199 * @state: duplicated atomic state to commit
3200 * @ctx: pointer to acquire_ctx to use for commit.
3201 *
3202 * The state returned by drm_atomic_helper_duplicate_state() and
3203 * drm_atomic_helper_suspend() is partially invalid, and needs to
3204 * be fixed up before commit.
3205 *
3206 * Returns:
3207 * 0 on success or a negative error code on failure.
3208 *
3209 * See also:
3210 * drm_atomic_helper_suspend()
3211 */
3212int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3213 struct drm_modeset_acquire_ctx *ctx)
3214{
Sean Paulaa394b02018-11-29 10:04:14 -05003215 int i, ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003216 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003217 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003218 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003219 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003220 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003221 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003222
3223 state->acquire_ctx = ctx;
3224
Ville Syrjäläe00fb852018-05-25 21:50:45 +03003225 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003226 state->planes[i].old_state = plane->state;
3227
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003228 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003229 state->crtcs[i].old_state = crtc->state;
3230
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003231 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003232 state->connectors[i].old_state = connector->state;
3233
Sean Paulaa394b02018-11-29 10:04:14 -05003234 ret = drm_atomic_commit(state);
3235
3236 state->acquire_ctx = NULL;
3237
3238 return ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003239}
3240EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3241
3242/**
Thierry Reding14942762015-12-02 17:50:04 +01003243 * drm_atomic_helper_resume - subsystem-level resume helper
3244 * @dev: DRM device
3245 * @state: atomic state to resume to
3246 *
3247 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3248 * grabs all modeset locks and commits the atomic state object. This can be
3249 * used in conjunction with the drm_atomic_helper_suspend() helper to
3250 * implement suspend/resume for drivers that support atomic mode-setting.
3251 *
3252 * Returns:
3253 * 0 on success or a negative error code on failure.
3254 *
3255 * See also:
3256 * drm_atomic_helper_suspend()
3257 */
3258int drm_atomic_helper_resume(struct drm_device *dev,
3259 struct drm_atomic_state *state)
3260{
Daniel Vettera5b84442017-04-03 10:32:53 +02003261 struct drm_modeset_acquire_ctx ctx;
Thierry Reding14942762015-12-02 17:50:04 +01003262 int err;
3263
3264 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003265
Sean Paulb7ea04d2018-11-29 10:04:17 -05003266 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
Daniel Vetter869e1882017-05-31 10:38:13 +02003267
Sean Paulb7ea04d2018-11-29 10:04:17 -05003268 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
Daniel Vettera5b84442017-04-03 10:32:53 +02003269
Sean Paulb7ea04d2018-11-29 10:04:17 -05003270 DRM_MODESET_LOCK_ALL_END(ctx, err);
Sean Paul2aa3eef2018-11-29 10:04:16 -05003271 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01003272
3273 return err;
3274}
3275EXPORT_SYMBOL(drm_atomic_helper_resume);
3276
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003277static int page_flip_common(struct drm_atomic_state *state,
3278 struct drm_crtc *crtc,
3279 struct drm_framebuffer *fb,
3280 struct drm_pending_vblank_event *event,
3281 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003282{
3283 struct drm_plane *plane = crtc->primary;
3284 struct drm_plane_state *plane_state;
3285 struct drm_crtc_state *crtc_state;
3286 int ret = 0;
3287
3288 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3289 if (IS_ERR(crtc_state))
3290 return PTR_ERR(crtc_state);
3291
3292 crtc_state->event = event;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003293 crtc_state->pageflip_flags = flags;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003294
3295 plane_state = drm_atomic_get_plane_state(state, plane);
3296 if (IS_ERR(plane_state))
3297 return PTR_ERR(plane_state);
3298
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003299 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3300 if (ret != 0)
3301 return ret;
3302 drm_atomic_set_fb_for_plane(plane_state, fb);
3303
3304 /* Make sure we don't accidentally do a full modeset. */
3305 state->allow_modeset = false;
3306 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00003307 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3308 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003309 return -EINVAL;
3310 }
3311
3312 return ret;
3313}
3314
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003315/**
3316 * drm_atomic_helper_page_flip - execute a legacy page flip
3317 * @crtc: DRM crtc
3318 * @fb: DRM framebuffer
3319 * @event: optional DRM event to signal upon completion
3320 * @flags: flip flags for non-vblank sync'ed updates
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003321 * @ctx: lock acquisition context
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003322 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003323 * Provides a default &drm_crtc_funcs.page_flip implementation
3324 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003325 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003326 * Returns:
3327 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003328 *
3329 * See also:
3330 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003331 */
3332int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3333 struct drm_framebuffer *fb,
3334 struct drm_pending_vblank_event *event,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003335 uint32_t flags,
3336 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003337{
3338 struct drm_plane *plane = crtc->primary;
3339 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003340 int ret = 0;
3341
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003342 state = drm_atomic_state_alloc(plane->dev);
3343 if (!state)
3344 return -ENOMEM;
3345
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003346 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003347
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003348 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003349 if (ret != 0)
3350 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01003351
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02003352 ret = drm_atomic_nonblocking_commit(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003353fail:
Chris Wilson08536952016-10-14 13:18:18 +01003354 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003355 return ret;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003356}
3357EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01003358
3359/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003360 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3361 * @crtc: DRM crtc
3362 * @fb: DRM framebuffer
3363 * @event: optional DRM event to signal upon completion
3364 * @flags: flip flags for non-vblank sync'ed updates
3365 * @target: specifying the target vblank period when the flip to take effect
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003366 * @ctx: lock acquisition context
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003367 *
3368 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3369 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3370 * target vblank period to flip.
3371 *
3372 * Returns:
3373 * Returns 0 on success, negative errno numbers on failure.
3374 */
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003375int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3376 struct drm_framebuffer *fb,
3377 struct drm_pending_vblank_event *event,
3378 uint32_t flags,
3379 uint32_t target,
3380 struct drm_modeset_acquire_ctx *ctx)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003381{
3382 struct drm_plane *plane = crtc->primary;
3383 struct drm_atomic_state *state;
3384 struct drm_crtc_state *crtc_state;
3385 int ret = 0;
3386
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003387 state = drm_atomic_state_alloc(plane->dev);
3388 if (!state)
3389 return -ENOMEM;
3390
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003391 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003392
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003393 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003394 if (ret != 0)
3395 goto fail;
3396
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01003397 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003398 if (WARN_ON(!crtc_state)) {
3399 ret = -EINVAL;
3400 goto fail;
3401 }
3402 crtc_state->target_vblank = target;
3403
3404 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003405fail:
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003406 drm_atomic_state_put(state);
3407 return ret;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003408}
3409EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);