blob: 62e29b5ebb6e3a25a540c500dbc5a8be3927d9e9 [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
Chris Wilsonf54d1862016-10-25 13:00:45 +010028#include <linux/dma-fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010029
Sam Ravnborg0500c042019-05-26 19:35:35 +020030#include <drm/drm_atomic.h>
31#include <drm/drm_atomic_helper.h>
32#include <drm/drm_atomic_uapi.h>
33#include <drm/drm_damage_helper.h>
34#include <drm/drm_device.h>
35#include <drm/drm_plane_helper.h>
36#include <drm/drm_print.h>
37#include <drm/drm_vblank.h>
38#include <drm/drm_writeback.h>
39
Jose Abreufaf94a02017-05-25 15:19:16 +010040#include "drm_crtc_helper_internal.h"
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020041#include "drm_crtc_internal.h"
42
Daniel Vetter3150c7d2014-11-06 20:53:29 +010043/**
44 * DOC: overview
45 *
46 * This helper library provides implementations of check and commit functions on
47 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
48 * also provides convenience implementations for the atomic state handling
49 * callbacks for drivers which don't need to subclass the drm core structures to
50 * add their own additional internal state.
51 *
52 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020053 * drm_atomic_helper_check() and for the commit callback with
54 * drm_atomic_helper_commit(). But the individual stages and callbacks are
55 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010056 * together with a driver private modeset implementation.
57 *
58 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020059 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
60 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010061 * various functions to implement set_property callbacks. New drivers must not
62 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010063 *
64 * The atomic helper uses the same function table structures as all other
Daniel Vetterea0dd852016-12-29 21:48:26 +010065 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
66 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
67 * also shares the &struct drm_plane_helper_funcs function table with the plane
Daniel Vetter092d01d2015-12-04 09:45:44 +010068 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010069 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010070static void
71drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010072 struct drm_plane_state *old_plane_state,
Daniel Vetterc2fcd272014-11-05 00:14:14 +010073 struct drm_plane_state *plane_state,
74 struct drm_plane *plane)
75{
76 struct drm_crtc_state *crtc_state;
77
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010078 if (old_plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010079 crtc_state = drm_atomic_get_new_crtc_state(state,
80 old_plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010081
82 if (WARN_ON(!crtc_state))
83 return;
84
85 crtc_state->planes_changed = true;
86 }
87
88 if (plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010089 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010090
91 if (WARN_ON(!crtc_state))
92 return;
93
94 crtc_state->planes_changed = true;
95 }
96}
97
Daniel Vetter297e30b2018-10-04 22:24:27 +020098/*
99 * For connectors that support multiple encoders, either the
100 * .atomic_best_encoder() or .best_encoder() operation must be implemented.
101 */
102static struct drm_encoder *
103pick_single_encoder_for_connector(struct drm_connector *connector)
104{
105 WARN_ON(connector->encoder_ids[1]);
106 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
107}
108
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100109static int handle_conflicting_encoders(struct drm_atomic_state *state,
110 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +0100111{
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100112 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200113 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100114 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100115 struct drm_encoder *encoder;
116 unsigned encoder_mask = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100117 int i, ret = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200118
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100119 /*
120 * First loop, find all newly assigned encoders from the connectors
121 * part of the state. If the same encoder is assigned to multiple
122 * connectors bail out.
123 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100124 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100125 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
126 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200127
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100128 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200129 continue;
130
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100131 if (funcs->atomic_best_encoder)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100132 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200133 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100134 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200135 else
Daniel Vetter297e30b2018-10-04 22:24:27 +0200136 new_encoder = pick_single_encoder_for_connector(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100137
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100138 if (new_encoder) {
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300139 if (encoder_mask & drm_encoder_mask(new_encoder)) {
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100140 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
141 new_encoder->base.id, new_encoder->name,
142 connector->base.id, connector->name);
143
144 return -EINVAL;
145 }
146
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300147 encoder_mask |= drm_encoder_mask(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100148 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200149 }
150
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100151 if (!encoder_mask)
152 return 0;
153
154 /*
155 * Second loop, iterate over all connectors not part of the state.
156 *
157 * If a conflicting encoder is found and disable_conflicting_encoders
158 * is not set, an error is returned. Userspace can provide a solution
159 * through the atomic ioctl.
160 *
161 * If the flag is set conflicting connectors are removed from the crtc
162 * and the crtc is disabled if no encoder is left. This preserves
163 * compatibility with the legacy set_config behavior.
164 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100165 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100166 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100167 struct drm_crtc_state *crtc_state;
168
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100169 if (drm_atomic_get_new_connector_state(state, connector))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100170 continue;
171
172 encoder = connector->state->best_encoder;
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300173 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100174 continue;
175
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100176 if (!disable_conflicting_encoders) {
177 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
178 encoder->base.id, encoder->name,
179 connector->state->crtc->base.id,
180 connector->state->crtc->name,
181 connector->base.id, connector->name);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100182 ret = -EINVAL;
183 goto out;
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100184 }
185
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100186 new_conn_state = drm_atomic_get_connector_state(state, connector);
187 if (IS_ERR(new_conn_state)) {
188 ret = PTR_ERR(new_conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100189 goto out;
190 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100191
192 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
193 encoder->base.id, encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100194 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100195 connector->base.id, connector->name);
196
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100197 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100198
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100199 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100200 if (ret)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100201 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100202
203 if (!crtc_state->connector_mask) {
204 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
205 NULL);
206 if (ret < 0)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100207 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100208
209 crtc_state->active = false;
210 }
211 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100212out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100213 drm_connector_list_iter_end(&conn_iter);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100214
Daniel Vetterc36a3252016-12-15 16:58:43 +0100215 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200216}
217
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100218static void
219set_best_encoder(struct drm_atomic_state *state,
220 struct drm_connector_state *conn_state,
221 struct drm_encoder *encoder)
222{
223 struct drm_crtc_state *crtc_state;
224 struct drm_crtc *crtc;
225
226 if (conn_state->best_encoder) {
227 /* Unset the encoder_mask in the old crtc state. */
228 crtc = conn_state->connector->state->crtc;
229
230 /* A NULL crtc is an error here because we should have
231 * duplicated a NULL best_encoder when crtc was NULL.
232 * As an exception restoring duplicated atomic state
233 * during resume is allowed, so don't warn when
234 * best_encoder is equal to encoder we intend to set.
235 */
236 WARN_ON(!crtc && encoder != conn_state->best_encoder);
237 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100238 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100239
240 crtc_state->encoder_mask &=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300241 ~drm_encoder_mask(conn_state->best_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100242 }
243 }
244
245 if (encoder) {
246 crtc = conn_state->crtc;
247 WARN_ON(!crtc);
248 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100249 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100250
251 crtc_state->encoder_mask |=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300252 drm_encoder_mask(encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100253 }
254 }
255
256 conn_state->best_encoder = encoder;
257}
258
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100259static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200260steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100261 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200262{
Daniel Vetter623369e2014-09-16 17:50:47 +0200263 struct drm_crtc_state *crtc_state;
264 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100265 struct drm_connector_state *old_connector_state, *new_connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100266 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200267
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100268 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100269 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200270
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100271 if (new_connector_state->best_encoder != encoder)
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100272 continue;
273
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100274 encoder_crtc = old_connector_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200275
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100276 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
277 encoder->base.id, encoder->name,
278 encoder_crtc->base.id, encoder_crtc->name);
279
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100280 set_best_encoder(state, new_connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100281
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100282 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100283 crtc_state->connectors_changed = true;
284
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100285 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200286 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200287}
288
289static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100290update_connector_routing(struct drm_atomic_state *state,
291 struct drm_connector *connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100292 struct drm_connector_state *old_connector_state,
293 struct drm_connector_state *new_connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200294{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200295 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200296 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200297 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200298
Daniel Vetter17a38d92015-02-22 12:24:16 +0100299 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
300 connector->base.id,
301 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200302
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100303 if (old_connector_state->crtc != new_connector_state->crtc) {
304 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100305 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200306 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200307 }
308
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100309 if (new_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100310 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200311 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200312 }
313 }
314
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100315 if (!new_connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100316 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200317 connector->base.id,
318 connector->name);
319
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100320 set_best_encoder(state, new_connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200321
322 return 0;
323 }
324
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400325 crtc_state = drm_atomic_get_new_crtc_state(state,
326 new_connector_state->crtc);
327 /*
328 * For compatibility with legacy users, we want to make sure that
329 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
330 * which would result in anything else must be considered invalid, to
331 * avoid turning on new displays on dead connectors.
332 *
333 * Since the connector can be unregistered at any point during an
334 * atomic check or commit, this is racy. But that's OK: all we care
335 * about is ensuring that userspace can't do anything but shut off the
Matt Roper1e55a532019-02-01 17:23:26 -0800336 * display on a connector that was destroyed after it's been notified,
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400337 * not before.
Lyude Paul022deba2019-02-01 19:20:03 -0500338 *
339 * Additionally, we also want to ignore connector registration when
340 * we're trying to restore an atomic state during system resume since
341 * there's a chance the connector may have been destroyed during the
342 * process, but it's better to ignore that then cause
343 * drm_atomic_helper_resume() to fail.
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400344 */
Lyude Paul022deba2019-02-01 19:20:03 -0500345 if (!state->duplicated && drm_connector_is_unregistered(connector) &&
346 crtc_state->active) {
Lyude Paulde9f8ee2018-10-16 16:39:46 -0400347 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
348 connector->base.id, connector->name);
349 return -EINVAL;
350 }
351
Daniel Vetter623369e2014-09-16 17:50:47 +0200352 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200353
354 if (funcs->atomic_best_encoder)
355 new_encoder = funcs->atomic_best_encoder(connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100356 new_connector_state);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200357 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200358 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200359 else
Daniel Vetter297e30b2018-10-04 22:24:27 +0200360 new_encoder = pick_single_encoder_for_connector(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200361
362 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100363 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
364 connector->base.id,
365 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200366 return -EINVAL;
367 }
368
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100369 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
Russell King6ac7c542017-02-13 12:27:03 +0000370 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100371 new_encoder->base.id,
372 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100373 new_connector_state->crtc->base.id,
374 new_connector_state->crtc->name);
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100375 return -EINVAL;
376 }
377
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100378 if (new_encoder == new_connector_state->best_encoder) {
379 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100380
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200381 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100382 connector->base.id,
383 connector->name,
384 new_encoder->base.id,
385 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100386 new_connector_state->crtc->base.id,
387 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200388
389 return 0;
390 }
391
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100392 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200393
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100394 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100395
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200396 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200397
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200398 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100399 connector->base.id,
400 connector->name,
401 new_encoder->base.id,
402 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100403 new_connector_state->crtc->base.id,
404 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200405
406 return 0;
407}
408
409static int
410mode_fixup(struct drm_atomic_state *state)
411{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300412 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100413 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300414 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100415 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200416 int i;
Dan Carpenterf9ad86e2017-02-08 02:46:01 +0300417 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200418
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100419 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
420 if (!new_crtc_state->mode_changed &&
421 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200422 continue;
423
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100424 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200425 }
426
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100427 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200428 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200429 struct drm_encoder *encoder;
430
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100431 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200432
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100433 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200434 continue;
435
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100436 new_crtc_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100437 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200438
439 /*
440 * Each encoder has at most one connector (since we always steal
441 * it away), so we won't call ->mode_fixup twice.
442 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100443 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200444 funcs = encoder->helper_private;
445
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100446 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
447 &new_crtc_state->adjusted_mode);
Archit Taneja862e6862015-05-21 11:03:16 +0530448 if (!ret) {
449 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
450 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200451 }
452
Noralf Trønnes28276352016-05-11 18:09:20 +0200453 if (funcs && funcs->atomic_check) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100454 ret = funcs->atomic_check(encoder, new_crtc_state,
455 new_conn_state);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100456 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100457 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
458 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100459 return ret;
460 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200461 } else if (funcs && funcs->mode_fixup) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100462 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
463 &new_crtc_state->adjusted_mode);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100464 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100465 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
466 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100467 return -EINVAL;
468 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200469 }
470 }
471
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100472 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200473 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200474
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100475 if (!new_crtc_state->enable)
Liu Yingf55f1702016-05-27 17:35:54 +0800476 continue;
477
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100478 if (!new_crtc_state->mode_changed &&
479 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200480 continue;
481
482 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300483 if (!funcs->mode_fixup)
484 continue;
485
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100486 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
487 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200488 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200489 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
490 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200491 return -EINVAL;
492 }
493 }
494
495 return 0;
496}
497
Jose Abreufaf94a02017-05-25 15:19:16 +0100498static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
499 struct drm_encoder *encoder,
500 struct drm_crtc *crtc,
Laurent Pinchart8518f052018-06-05 03:36:13 +0300501 const struct drm_display_mode *mode)
Jose Abreufaf94a02017-05-25 15:19:16 +0100502{
503 enum drm_mode_status ret;
504
505 ret = drm_encoder_mode_valid(encoder, mode);
506 if (ret != MODE_OK) {
507 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
508 encoder->base.id, encoder->name);
509 return ret;
510 }
511
512 ret = drm_bridge_mode_valid(encoder->bridge, mode);
513 if (ret != MODE_OK) {
514 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
515 return ret;
516 }
517
518 ret = drm_crtc_mode_valid(crtc, mode);
519 if (ret != MODE_OK) {
520 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
521 crtc->base.id, crtc->name);
522 return ret;
523 }
524
525 return ret;
526}
527
528static int
529mode_valid(struct drm_atomic_state *state)
530{
531 struct drm_connector_state *conn_state;
532 struct drm_connector *connector;
533 int i;
534
535 for_each_new_connector_in_state(state, connector, conn_state, i) {
536 struct drm_encoder *encoder = conn_state->best_encoder;
537 struct drm_crtc *crtc = conn_state->crtc;
538 struct drm_crtc_state *crtc_state;
539 enum drm_mode_status mode_status;
Laurent Pinchart8518f052018-06-05 03:36:13 +0300540 const struct drm_display_mode *mode;
Jose Abreufaf94a02017-05-25 15:19:16 +0100541
542 if (!crtc || !encoder)
543 continue;
544
545 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
546 if (!crtc_state)
547 continue;
548 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
549 continue;
550
551 mode = &crtc_state->mode;
552
553 mode_status = mode_valid_path(connector, encoder, crtc, mode);
554 if (mode_status != MODE_OK)
555 return -EINVAL;
556 }
557
558 return 0;
559}
560
Daniel Vetterd9b13622014-11-26 16:57:41 +0100561/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800562 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100563 * @dev: DRM device
564 * @state: the driver state object
565 *
566 * Check the state object to see if the requested state is physically possible.
567 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200568 * update and adds any additional connectors needed for full modesets. It calls
569 * the various per-object callbacks in the follow order:
570 *
571 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
572 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
573 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
574 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
Jose Abreufaf94a02017-05-25 15:19:16 +0100575 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
576 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
577 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
578 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200579 * This function is only called when the encoder will be part of a configured crtc,
580 * it must not be used for implementing connector property validation.
581 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
582 * instead.
Jose Abreufaf94a02017-05-25 15:19:16 +0100583 * 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 +0200584 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100585 * &drm_crtc_state.mode_changed is set when the input mode is changed.
586 * &drm_crtc_state.connectors_changed is set when a connector is added or
587 * removed from the crtc. &drm_crtc_state.active_changed is set when
588 * &drm_crtc_state.active changes, which is used for DPMS.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100589 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100590 *
591 * IMPORTANT:
592 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100593 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
594 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
595 * without a full modeset) _must_ call this function afterwards after that
596 * change. It is permitted to call this function multiple times for the same
597 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
598 * upon the adjusted dotclock for fifo space allocation and watermark
599 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100600 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200601 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100602 * Zero for success or -errno
603 */
604int
Rob Clark934ce1c2014-11-19 16:41:33 -0500605drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200606 struct drm_atomic_state *state)
607{
Daniel Vetter623369e2014-09-16 17:50:47 +0200608 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100609 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300610 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100611 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200612 int i, ret;
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200613 unsigned connectors_mask = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200614
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100615 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200616 bool has_connectors =
617 !!new_crtc_state->connector_mask;
618
Daniel Vetter869e1882017-05-31 10:38:13 +0200619 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
620
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100621 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200622 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
623 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100624 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200625 }
626
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100627 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200628 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
629 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200630
631 /*
632 * For clarity this assignment is done here, but
633 * enable == 0 is only true when there are no
634 * connectors and a NULL mode.
635 *
636 * The other way around is true as well. enable != 0
637 * iff connectors are attached and a mode is set.
638 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100639 new_crtc_state->mode_changed = true;
640 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200641 }
Maarten Lankhorst24d66522017-04-06 13:19:01 +0200642
643 if (old_crtc_state->active != new_crtc_state->active) {
644 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
645 crtc->base.id, crtc->name);
646 new_crtc_state->active_changed = true;
647 }
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200648
649 if (new_crtc_state->enable != has_connectors) {
650 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
651 crtc->base.id, crtc->name);
652
653 return -EINVAL;
654 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200655 }
656
Maarten Lankhorst44596b82017-04-06 13:19:00 +0200657 ret = handle_conflicting_encoders(state, false);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100658 if (ret)
659 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100660
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100661 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200662 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
663
Daniel Vetter869e1882017-05-31 10:38:13 +0200664 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
665
Daniel Vetter623369e2014-09-16 17:50:47 +0200666 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100667 * This only sets crtc->connectors_changed for routing changes,
668 * drivers must set crtc->connectors_changed themselves when
669 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200670 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100671 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100672 old_connector_state,
673 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200674 if (ret)
675 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100676 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100677 new_crtc_state = drm_atomic_get_new_crtc_state(state,
678 old_connector_state->crtc);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100679 if (old_connector_state->link_status !=
680 new_connector_state->link_status)
681 new_crtc_state->connectors_changed = true;
Radhakrishna Sripada47e22ff2018-10-12 11:42:32 -0700682
683 if (old_connector_state->max_requested_bpc !=
684 new_connector_state->max_requested_bpc)
685 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200686 }
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200687
688 if (funcs->atomic_check)
689 ret = funcs->atomic_check(connector, new_connector_state);
690 if (ret)
691 return ret;
692
Ville Syrjäläca52bea2018-06-15 20:07:34 +0300693 connectors_mask |= BIT(i);
Daniel Vetter623369e2014-09-16 17:50:47 +0200694 }
695
696 /*
697 * After all the routing has been prepared we need to add in any
Matt Roper1e55a532019-02-01 17:23:26 -0800698 * connector which is itself unchanged, but whose crtc changes its
Daniel Vetter623369e2014-09-16 17:50:47 +0200699 * configuration. This must be done before calling mode_fixup in case a
700 * crtc only changed its mode but has the same set of connectors.
701 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100702 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100703 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100704 continue;
705
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200706 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
707 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100708 new_crtc_state->enable ? 'y' : 'n',
709 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200710
711 ret = drm_atomic_add_affected_connectors(state, crtc);
712 if (ret != 0)
713 return ret;
714
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200715 ret = drm_atomic_add_affected_planes(state, crtc);
716 if (ret != 0)
717 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200718 }
719
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200720 /*
721 * Iterate over all connectors again, to make sure atomic_check()
722 * has been called on them when a modeset is forced.
723 */
724 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
725 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
726
727 if (connectors_mask & BIT(i))
728 continue;
729
730 if (funcs->atomic_check)
731 ret = funcs->atomic_check(connector, new_connector_state);
732 if (ret)
733 return ret;
734 }
735
Jose Abreufaf94a02017-05-25 15:19:16 +0100736 ret = mode_valid(state);
737 if (ret)
738 return ret;
739
Daniel Vetter623369e2014-09-16 17:50:47 +0200740 return mode_fixup(state);
741}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100742EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200743
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100744/**
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200745 * drm_atomic_helper_check_plane_state() - Check plane state for validity
746 * @plane_state: plane state to check
747 * @crtc_state: crtc state to check
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200748 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
749 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
750 * @can_position: is it legal to position the plane such that it
751 * doesn't cover the entire crtc? This will generally
752 * only be false for primary planes.
753 * @can_update_disabled: can the plane be updated while the crtc
754 * is disabled?
755 *
756 * Checks that a desired plane update is valid, and updates various
757 * bits of derived state (clipped coordinates etc.). Drivers that provide
758 * their own plane handling rather than helper-provided implementations may
759 * still wish to call this function to avoid duplication of error checking
760 * code.
761 *
762 * RETURNS:
763 * Zero if update appears valid, error code on failure
764 */
765int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
766 const struct drm_crtc_state *crtc_state,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200767 int min_scale,
768 int max_scale,
769 bool can_position,
770 bool can_update_disabled)
771{
772 struct drm_framebuffer *fb = plane_state->fb;
773 struct drm_rect *src = &plane_state->src;
774 struct drm_rect *dst = &plane_state->dst;
775 unsigned int rotation = plane_state->rotation;
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200776 struct drm_rect clip = {};
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200777 int hscale, vscale;
778
779 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
780
781 *src = drm_plane_state_src(plane_state);
782 *dst = drm_plane_state_dest(plane_state);
783
784 if (!fb) {
785 plane_state->visible = false;
786 return 0;
787 }
788
789 /* crtc should only be NULL when disabling (i.e., !fb) */
790 if (WARN_ON(!plane_state->crtc)) {
791 plane_state->visible = false;
792 return 0;
793 }
794
795 if (!crtc_state->enable && !can_update_disabled) {
796 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
797 return -EINVAL;
798 }
799
800 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
801
802 /* Check scaling */
803 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
804 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
805 if (hscale < 0 || vscale < 0) {
806 DRM_DEBUG_KMS("Invalid scaling of plane\n");
807 drm_rect_debug_print("src: ", &plane_state->src, true);
808 drm_rect_debug_print("dst: ", &plane_state->dst, false);
809 return -ERANGE;
810 }
811
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200812 if (crtc_state->enable)
813 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
814
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +0200815 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200816
817 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
818
819 if (!plane_state->visible)
820 /*
821 * Plane isn't visible; some drivers can handle this
822 * so we just return success here. Drivers that can't
823 * (including those that use the primary plane helper's
824 * update function) will return an error from their
825 * update_plane handler.
826 */
827 return 0;
828
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200829 if (!can_position && !drm_rect_equals(dst, &clip)) {
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200830 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
831 drm_rect_debug_print("dst: ", dst, false);
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200832 drm_rect_debug_print("clip: ", &clip, false);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200833 return -EINVAL;
834 }
835
836 return 0;
837}
838EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
839
840/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800841 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100842 * @dev: DRM device
843 * @state: the driver state object
844 *
845 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100846 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100847 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
848 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100849 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100850 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200851 * updated planes.
852 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200853 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100854 * Zero for success or -errno
855 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100856int
857drm_atomic_helper_check_planes(struct drm_device *dev,
858 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100859{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300860 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100861 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300862 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100863 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100864 int i, ret = 0;
865
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100866 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200867 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100868
Daniel Vetter869e1882017-05-31 10:38:13 +0200869 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
870
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100871 funcs = plane->helper_private;
872
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100873 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100874
Deepak Rawatd9778b42018-08-08 17:36:26 -0700875 drm_atomic_helper_check_plane_damage(state, new_plane_state);
876
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100877 if (!funcs || !funcs->atomic_check)
878 continue;
879
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100880 ret = funcs->atomic_check(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100881 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200882 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
883 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100884 return ret;
885 }
886 }
887
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100888 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200889 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100890
891 funcs = crtc->helper_private;
892
893 if (!funcs || !funcs->atomic_check)
894 continue;
895
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100896 ret = funcs->atomic_check(crtc, new_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100897 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200898 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
899 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100900 return ret;
901 }
902 }
903
Daniel Vetterd9b13622014-11-26 16:57:41 +0100904 return ret;
905}
906EXPORT_SYMBOL(drm_atomic_helper_check_planes);
907
908/**
909 * drm_atomic_helper_check - validate state object
910 * @dev: DRM device
911 * @state: the driver state object
912 *
913 * Check the state object to see if the requested state is physically possible.
914 * Only crtcs and planes have check callbacks, so for any additional (global)
915 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100916 * Drivers without such needs can directly use this as their
917 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100918 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100919 * This just wraps the two parts of the state checking for planes and modeset
920 * state in the default order: First it calls drm_atomic_helper_check_modeset()
921 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100922 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
923 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
924 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100925 *
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200926 * Note that zpos normalization will add all enable planes to the state which
927 * might not desired for some drivers.
928 * For example enable/disable of a cursor plane which have fixed zpos value
929 * would trigger all other enabled planes to be forced to the state change.
930 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200931 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100932 * Zero for success or -errno
933 */
934int drm_atomic_helper_check(struct drm_device *dev,
935 struct drm_atomic_state *state)
936{
937 int ret;
938
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100939 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100940 if (ret)
941 return ret;
942
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200943 if (dev->mode_config.normalize_zpos) {
944 ret = drm_atomic_normalize_zpos(dev, state);
945 if (ret)
946 return ret;
947 }
948
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100949 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500950 if (ret)
951 return ret;
952
Gustavo Padovanfef9df82017-06-30 15:03:17 -0300953 if (state->legacy_cursor_update)
954 state->async_update = !drm_atomic_helper_async_check(dev, state);
955
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100956 return ret;
957}
958EXPORT_SYMBOL(drm_atomic_helper_check);
959
Daniel Vetter623369e2014-09-16 17:50:47 +0200960static void
961disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
962{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300963 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100964 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300965 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100966 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200967 int i;
968
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100969 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 +0200970 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200971 struct drm_encoder *encoder;
972
Daniel Vetter623369e2014-09-16 17:50:47 +0200973 /* Shut down everything that's in the changeset and currently
974 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300975 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200976 continue;
977
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100978 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100979
Daniel Vetter4218a322015-03-26 22:18:40 +0100980 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200981 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100982 continue;
983
Rob Clark46df9ad2014-11-20 15:40:36 -0500984 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200985
Rob Clark46df9ad2014-11-20 15:40:36 -0500986 /* We shouldn't get this far if we didn't previously have
987 * an encoder.. but WARN_ON() rather than explode.
988 */
989 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200990 continue;
991
992 funcs = encoder->helper_private;
993
Daniel Vetter17a38d92015-02-22 12:24:16 +0100994 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
995 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100996
Daniel Vetter623369e2014-09-16 17:50:47 +0200997 /*
998 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800999 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001000 */
Archit Taneja862e6862015-05-21 11:03:16 +05301001 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001002
1003 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +02001004 if (funcs) {
Sean Paul43c76d72019-06-11 16:49:53 -04001005 if (funcs->atomic_disable)
1006 funcs->atomic_disable(encoder, old_state);
1007 else if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +02001008 funcs->prepare(encoder);
1009 else if (funcs->disable)
1010 funcs->disable(encoder);
1011 else if (funcs->dpms)
1012 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1013 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001014
Archit Taneja862e6862015-05-21 11:03:16 +05301015 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001016 }
1017
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001018 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 +02001019 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter84014b02017-10-17 17:27:14 +02001020 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +02001021
1022 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001023 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001024 continue;
1025
1026 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +02001027 continue;
1028
1029 funcs = crtc->helper_private;
1030
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001031 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1032 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001033
1034
Daniel Vetter623369e2014-09-16 17:50:47 +02001035 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001036 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +02001037 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +08001038 else if (funcs->atomic_disable)
1039 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001040 else if (funcs->disable)
1041 funcs->disable(crtc);
Rodrigo Siqueirafe616922019-03-14 15:48:45 -03001042 else if (funcs->dpms)
Daniel Vetter623369e2014-09-16 17:50:47 +02001043 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Daniel Vetter84014b02017-10-17 17:27:14 +02001044
1045 if (!(dev->irq_enabled && dev->num_crtcs))
1046 continue;
1047
1048 ret = drm_crtc_vblank_get(crtc);
1049 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1050 if (ret == 0)
1051 drm_crtc_vblank_put(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001052 }
1053}
1054
Daniel Vetter4c18d302015-05-12 15:27:37 +02001055/**
1056 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1057 * @dev: DRM device
1058 * @old_state: atomic state object with old state structures
1059 *
1060 * This function updates all the various legacy modeset state pointers in
1061 * connectors, encoders and crtcs. It also updates the timestamping constants
1062 * used for precise vblank timestamps by calling
1063 * drm_calc_timestamping_constants().
1064 *
1065 * Drivers can use this for building their own atomic commit if they don't have
1066 * a pure helper-based modeset implementation.
Daniel Vetter2e2b96e2017-11-08 21:30:07 +01001067 *
1068 * Since these updates are not synchronized with lockings, only code paths
1069 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1070 * legacy state filled out by this helper. Defacto this means this helper and
1071 * the legacy state pointers are only really useful for transitioning an
1072 * existing driver to the atomic world.
Daniel Vetter4c18d302015-05-12 15:27:37 +02001073 */
1074void
1075drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1076 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001077{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001078 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001079 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001080 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001081 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001082 int i;
1083
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001084 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001085 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001086 if (connector->encoder) {
1087 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001088
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001089 connector->encoder->crtc = NULL;
1090 connector->encoder = NULL;
1091 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001092
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001093 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001094 if ((!crtc && old_conn_state->crtc) ||
1095 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001096 int mode = DRM_MODE_DPMS_OFF;
1097
1098 if (crtc && crtc->state->active)
1099 mode = DRM_MODE_DPMS_ON;
1100
1101 connector->dpms = mode;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001102 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001103 }
1104
1105 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001106 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1107 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +02001108 continue;
1109
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001110 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +02001111 continue;
1112
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001113 connector->encoder = new_conn_state->best_encoder;
1114 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +02001115 }
1116
1117 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001118 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +02001119 struct drm_plane *primary = crtc->primary;
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001120 struct drm_plane_state *new_plane_state;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001121
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001122 crtc->mode = new_crtc_state->mode;
1123 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001124
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001125 new_plane_state =
1126 drm_atomic_get_new_plane_state(old_state, primary);
1127
1128 if (new_plane_state && new_plane_state->crtc == crtc) {
1129 crtc->x = new_plane_state->src_x >> 16;
1130 crtc->y = new_plane_state->src_y >> 16;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001131 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +02001132
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001133 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +02001134 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001135 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001136 }
1137}
Daniel Vetter4c18d302015-05-12 15:27:37 +02001138EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001139
1140static void
1141crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1142{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001143 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001144 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001145 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001146 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001147 int i;
1148
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001149 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001150 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001151
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001152 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +02001153 continue;
1154
1155 funcs = crtc->helper_private;
1156
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001157 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001158 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1159 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001160
Daniel Vetter623369e2014-09-16 17:50:47 +02001161 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001162 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001163 }
1164
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001165 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001166 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001167 struct drm_encoder *encoder;
1168 struct drm_display_mode *mode, *adjusted_mode;
1169
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001170 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001171 continue;
1172
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001173 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001174 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001175 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001176 mode = &new_crtc_state->mode;
1177 adjusted_mode = &new_crtc_state->adjusted_mode;
1178
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001179 if (!new_crtc_state->mode_changed)
1180 continue;
1181
Daniel Vetter17a38d92015-02-22 12:24:16 +01001182 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1183 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001184
Daniel Vetter623369e2014-09-16 17:50:47 +02001185 /*
1186 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001187 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001188 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001189 if (funcs && funcs->atomic_mode_set) {
1190 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001191 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001192 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +01001193 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001194 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001195
Archit Taneja862e6862015-05-21 11:03:16 +05301196 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001197 }
1198}
1199
1200/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001201 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001202 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +01001203 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001204 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001205 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +02001206 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +01001207 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +03001208 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +01001209 * drm_atomic_helper_commit_planes(), which is what the default commit function
1210 * does. But drivers with different needs can group the modeset commits together
1211 * and do the plane commits at the end. This is useful for drivers doing runtime
1212 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001213 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001214void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1215 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001216{
Laurent Pincharta072f802015-02-22 12:24:18 +01001217 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +02001218
1219 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1220
Laurent Pincharta072f802015-02-22 12:24:18 +01001221 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001222}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001223EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001224
Brian Starkey935774c2017-03-29 17:42:32 +01001225static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1226 struct drm_atomic_state *old_state)
1227{
1228 struct drm_connector *connector;
1229 struct drm_connector_state *new_conn_state;
1230 int i;
1231
1232 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1233 const struct drm_connector_helper_funcs *funcs;
1234
1235 funcs = connector->helper_private;
Boris Brezillon814bde92018-07-03 09:50:17 +02001236 if (!funcs->atomic_commit)
1237 continue;
Brian Starkey935774c2017-03-29 17:42:32 +01001238
1239 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1240 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
Boris Brezillon425132f2018-07-03 09:50:16 +02001241 funcs->atomic_commit(connector, new_conn_state);
Brian Starkey935774c2017-03-29 17:42:32 +01001242 }
1243 }
1244}
1245
Daniel Vetter623369e2014-09-16 17:50:47 +02001246/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001247 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001248 * @dev: DRM device
1249 * @old_state: atomic state object with old state structures
1250 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001251 * This function enables all the outputs with the new configuration which had to
1252 * be turned off for the update.
1253 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +03001254 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +01001255 * drm_atomic_helper_commit_planes(), which is what the default commit function
1256 * does. But drivers with different needs can group the modeset commits together
1257 * and do the plane commits at the end. This is useful for drivers doing runtime
1258 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001259 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001260void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1261 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001262{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001263 struct drm_crtc *crtc;
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001264 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001265 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001266 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001267 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001268 int i;
1269
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001270 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 +02001271 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001272
1273 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001274 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001275 continue;
1276
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001277 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +02001278 continue;
1279
1280 funcs = crtc->helper_private;
1281
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001282 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001283 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1284 crtc->base.id, crtc->name);
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001285 if (funcs->atomic_enable)
1286 funcs->atomic_enable(crtc, old_crtc_state);
Rodrigo Siqueirafe616922019-03-14 15:48:45 -03001287 else if (funcs->commit)
Daniel Vetteree0a89c2015-01-22 16:36:24 +01001288 funcs->commit(crtc);
1289 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001290 }
1291
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001292 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001293 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001294 struct drm_encoder *encoder;
1295
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001296 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001297 continue;
1298
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001299 if (!new_conn_state->crtc->state->active ||
1300 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001301 continue;
1302
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001303 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001304 funcs = encoder->helper_private;
1305
Daniel Vetter17a38d92015-02-22 12:24:16 +01001306 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1307 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001308
Daniel Vetter623369e2014-09-16 17:50:47 +02001309 /*
1310 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001311 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001312 */
Archit Taneja862e6862015-05-21 11:03:16 +05301313 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001314
Noralf Trønnes28276352016-05-11 18:09:20 +02001315 if (funcs) {
Sean Paul43c76d72019-06-11 16:49:53 -04001316 if (funcs->atomic_enable)
1317 funcs->atomic_enable(encoder, old_state);
1318 else if (funcs->enable)
Noralf Trønnes28276352016-05-11 18:09:20 +02001319 funcs->enable(encoder);
1320 else if (funcs->commit)
1321 funcs->commit(encoder);
1322 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001323
Archit Taneja862e6862015-05-21 11:03:16 +05301324 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001325 }
Brian Starkey935774c2017-03-29 17:42:32 +01001326
1327 drm_atomic_helper_commit_writebacks(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001328}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001329EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001330
Rob Clark4c5b7f32016-03-18 19:14:55 -04001331/**
1332 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1333 * @dev: DRM device
1334 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001335 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1336 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001337 *
1338 * For implicit sync, driver should fish the exclusive fence out from the
1339 * incoming fb's and stash it in the drm_plane_state. This is called after
1340 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1341 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001342 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001343 * Note that @pre_swap is needed since the point where we block for fences moves
1344 * around depending upon whether an atomic commit is blocking or
Gustavo Padovan42590372017-04-27 11:35:06 -03001345 * non-blocking. For non-blocking commit all waiting needs to happen after
1346 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001347 * to wait **before** we do anything that can't be easily rolled back. That is
1348 * before we call drm_atomic_helper_swap_state().
1349 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001350 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001351 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001352int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1353 struct drm_atomic_state *state,
1354 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001355{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001356 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001357 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001358 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001359
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001360 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1361 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001362 continue;
1363
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001364 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001365
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001366 /*
1367 * If waiting for fences pre-swap (ie: nonblock), userspace can
1368 * still interrupt the operation. Instead of blocking until the
1369 * timer expires, make the wait interruptible.
1370 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001371 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001372 if (ret)
1373 return ret;
1374
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001375 dma_fence_put(new_plane_state->fence);
1376 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001377 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001378
1379 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001380}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001381EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001382
John Keepingc2409062016-01-19 10:46:58 +00001383/**
Rob Clark5ee32292014-11-11 19:38:59 -05001384 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1385 * @dev: DRM device
1386 * @old_state: atomic state object with old state structures
1387 *
1388 * Helper to, after atomic commit, wait for vblanks on all effected
1389 * crtcs (ie. before cleaning up old framebuffers using
Boris Brezillon01086482017-06-02 10:32:06 +02001390 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
Daniel Vetterab58e332014-11-24 20:42:42 +01001391 * framebuffers have actually changed to optimize for the legacy cursor and
1392 * plane update use-case.
Boris Brezillon01086482017-06-02 10:32:06 +02001393 *
1394 * Drivers using the nonblocking commit tracking support initialized by calling
1395 * drm_atomic_helper_setup_commit() should look at
1396 * drm_atomic_helper_wait_for_flip_done() as an alternative.
Rob Clark5ee32292014-11-11 19:38:59 -05001397 */
1398void
1399drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1400 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001401{
1402 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001403 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001404 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001405 unsigned crtc_mask = 0;
1406
1407 /*
1408 * Legacy cursor ioctls are completely unsynced, and userspace
1409 * relies on that (by doing tons of cursor updates).
1410 */
1411 if (old_state->legacy_cursor_update)
1412 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001413
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001414 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Lucas Stacha76dfe32017-11-29 12:04:31 +01001415 if (!new_crtc_state->active)
Daniel Vetterab58e332014-11-24 20:42:42 +01001416 continue;
1417
Daniel Vetter623369e2014-09-16 17:50:47 +02001418 ret = drm_crtc_vblank_get(crtc);
1419 if (ret != 0)
1420 continue;
1421
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001422 crtc_mask |= drm_crtc_mask(crtc);
1423 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001424 }
1425
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001426 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001427 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001428 continue;
1429
1430 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001431 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001432 drm_crtc_vblank_count(crtc),
Linus Walleijb3198c32019-04-30 11:37:46 +02001433 msecs_to_jiffies(100));
Daniel Vetter623369e2014-09-16 17:50:47 +02001434
Russell King6ac7c542017-02-13 12:27:03 +00001435 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1436 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001437
Daniel Vetter623369e2014-09-16 17:50:47 +02001438 drm_crtc_vblank_put(crtc);
1439 }
1440}
Rob Clark5ee32292014-11-11 19:38:59 -05001441EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001442
1443/**
Boris Brezillon01086482017-06-02 10:32:06 +02001444 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1445 * @dev: DRM device
1446 * @old_state: atomic state object with old state structures
1447 *
1448 * Helper to, after atomic commit, wait for page flips on all effected
1449 * crtcs (ie. before cleaning up old framebuffers using
1450 * drm_atomic_helper_cleanup_planes()). Compared to
1451 * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1452 * CRTCs, assuming that cursors-only updates are signalling their completion
1453 * immediately (or using a different path).
1454 *
1455 * This requires that drivers use the nonblocking commit tracking support
1456 * initialized using drm_atomic_helper_setup_commit().
1457 */
1458void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1459 struct drm_atomic_state *old_state)
1460{
Boris Brezillon01086482017-06-02 10:32:06 +02001461 struct drm_crtc *crtc;
1462 int i;
1463
Leo Li4364bcb2018-10-15 09:46:40 -04001464 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1465 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
Boris Brezillon01086482017-06-02 10:32:06 +02001466 int ret;
1467
Leo Li4364bcb2018-10-15 09:46:40 -04001468 crtc = old_state->crtcs[i].ptr;
1469
1470 if (!crtc || !commit)
Boris Brezillon01086482017-06-02 10:32:06 +02001471 continue;
1472
1473 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1474 if (ret == 0)
1475 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1476 crtc->base.id, crtc->name);
1477 }
Ville Syrjälä2de42f72018-11-22 16:34:11 +02001478
1479 if (old_state->fake_commit)
1480 complete_all(&old_state->fake_commit->flip_done);
Boris Brezillon01086482017-06-02 10:32:06 +02001481}
1482EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1483
1484/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001485 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001486 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001487 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001488 * This is the default implementation for the
Maxime Ripard81a099a2017-07-20 15:01:16 +02001489 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1490 * that do not support runtime_pm or do not need the CRTC to be
1491 * enabled to perform a commit. Otherwise, see
1492 * drm_atomic_helper_commit_tail_rpm().
Daniel Vetter623369e2014-09-16 17:50:47 +02001493 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001494 * Note that the default ordering of how the various stages are called is to
Maxime Ripard81a099a2017-07-20 15:01:16 +02001495 * match the legacy modeset helper library closest.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001496 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001497void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001498{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001499 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001500
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001501 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001502
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001503 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001504
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001505 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001506
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001507 drm_atomic_helper_fake_vblank(old_state);
1508
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001509 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001510
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001511 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001512
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001513 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001514}
1515EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1516
Maxime Ripard81a099a2017-07-20 15:01:16 +02001517/**
1518 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1519 * @old_state: new modeset state to be committed
1520 *
1521 * This is an alternative implementation for the
1522 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1523 * that support runtime_pm or need the CRTC to be enabled to perform a
1524 * commit. Otherwise, one should use the default implementation
1525 * drm_atomic_helper_commit_tail().
1526 */
1527void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1528{
1529 struct drm_device *dev = old_state->dev;
1530
1531 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1532
1533 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1534
1535 drm_atomic_helper_commit_planes(dev, old_state,
1536 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1537
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001538 drm_atomic_helper_fake_vblank(old_state);
1539
Maxime Ripard81a099a2017-07-20 15:01:16 +02001540 drm_atomic_helper_commit_hw_done(old_state);
1541
1542 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1543
1544 drm_atomic_helper_cleanup_planes(dev, old_state);
1545}
1546EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1547
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001548static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001549{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001550 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001551 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001552
1553 funcs = dev->mode_config.helper_private;
1554
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001555 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001556
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001557 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001558
1559 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001560 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001561 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001562 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001563
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001564 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001565
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001566 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001567}
1568
1569static void commit_work(struct work_struct *work)
1570{
1571 struct drm_atomic_state *state = container_of(work,
1572 struct drm_atomic_state,
1573 commit_work);
1574 commit_tail(state);
1575}
1576
1577/**
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001578 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1579 * @dev: DRM device
1580 * @state: the driver state object
1581 *
1582 * This helper will check if it is possible to commit the state asynchronously.
1583 * Async commits are not supposed to swap the states like normal sync commits
1584 * but just do in-place changes on the current state.
1585 *
1586 * It will return 0 if the commit can happen in an asynchronous fashion or error
1587 * if not. Note that error just mean it can't be commited asynchronously, if it
1588 * fails the commit should be treated like a normal synchronous commit.
1589 */
1590int drm_atomic_helper_async_check(struct drm_device *dev,
1591 struct drm_atomic_state *state)
1592{
1593 struct drm_crtc *crtc;
1594 struct drm_crtc_state *crtc_state;
Boris Brezillonde2d8db2018-07-24 15:33:00 +02001595 struct drm_plane *plane = NULL;
1596 struct drm_plane_state *old_plane_state = NULL;
1597 struct drm_plane_state *new_plane_state = NULL;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001598 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001599 int i, n_planes = 0;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001600
1601 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1602 if (drm_atomic_crtc_needs_modeset(crtc_state))
1603 return -EINVAL;
1604 }
1605
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001606 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001607 n_planes++;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001608
1609 /* FIXME: we support only single plane updates for now */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001610 if (n_planes != 1)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001611 return -EINVAL;
1612
Boris Brezillon603ba2d2018-07-24 15:32:15 +02001613 if (!new_plane_state->crtc ||
1614 old_plane_state->crtc != new_plane_state->crtc)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001615 return -EINVAL;
1616
Nicholas Kazlauskas22163222019-01-07 12:41:46 -05001617 /*
1618 * FIXME: Since prepare_fb and cleanup_fb are always called on
1619 * the new_plane_state for async updates we need to block framebuffer
1620 * changes. This prevents use of a fb that's been cleaned up and
1621 * double cleanups from occuring.
1622 */
1623 if (old_plane_state->fb != new_plane_state->fb)
1624 return -EINVAL;
1625
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001626 funcs = plane->helper_private;
1627 if (!funcs->atomic_async_update)
1628 return -EINVAL;
1629
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001630 if (new_plane_state->fence)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001631 return -EINVAL;
1632
1633 /*
1634 * Don't do an async update if there is an outstanding commit modifying
1635 * the plane. This prevents our async update's changes from getting
1636 * overridden by a previous synchronous update's state.
1637 */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001638 if (old_plane_state->commit &&
1639 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1640 return -EBUSY;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001641
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001642 return funcs->atomic_async_check(plane, new_plane_state);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001643}
1644EXPORT_SYMBOL(drm_atomic_helper_async_check);
1645
1646/**
1647 * drm_atomic_helper_async_commit - commit state asynchronously
1648 * @dev: DRM device
1649 * @state: the driver state object
1650 *
1651 * This function commits a state asynchronously, i.e., not vblank
1652 * synchronized. It should be used on a state only when
1653 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1654 * the states like normal sync commits, but just do in-place changes on the
1655 * current state.
1656 */
1657void drm_atomic_helper_async_commit(struct drm_device *dev,
1658 struct drm_atomic_state *state)
1659{
1660 struct drm_plane *plane;
1661 struct drm_plane_state *plane_state;
1662 const struct drm_plane_helper_funcs *funcs;
1663 int i;
1664
1665 for_each_new_plane_in_state(state, plane, plane_state, i) {
1666 funcs = plane->helper_private;
1667 funcs->atomic_async_update(plane, plane_state);
Boris Brezillon02edfd92018-03-30 16:55:18 +02001668
1669 /*
1670 * ->atomic_async_update() is supposed to update the
1671 * plane->state in-place, make sure at least common
1672 * properties have been properly updated.
1673 */
1674 WARN_ON_ONCE(plane->state->fb != plane_state->fb);
1675 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1676 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1677 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1678 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001679 }
1680}
1681EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1682
1683/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001684 * drm_atomic_helper_commit - commit validated state object
1685 * @dev: DRM device
1686 * @state: the driver state object
1687 * @nonblock: whether nonblocking behavior is requested.
1688 *
1689 * This function commits a with drm_atomic_helper_check() pre-validated state
1690 * object. This can still fail when e.g. the framebuffer reservation fails. This
1691 * function implements nonblocking commits, using
1692 * drm_atomic_helper_setup_commit() and related functions.
1693 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001694 * Committing the actual hardware state is done through the
Matt Roper1e55a532019-02-01 17:23:26 -08001695 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or its default
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001696 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001697 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001698 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001699 * Zero for success or -errno.
1700 */
1701int drm_atomic_helper_commit(struct drm_device *dev,
1702 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001703 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001704{
1705 int ret;
1706
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001707 if (state->async_update) {
1708 ret = drm_atomic_helper_prepare_planes(dev, state);
1709 if (ret)
1710 return ret;
1711
1712 drm_atomic_helper_async_commit(dev, state);
1713 drm_atomic_helper_cleanup_planes(dev, state);
1714
1715 return 0;
1716 }
1717
Daniel Vettera095caa2016-06-08 17:15:36 +02001718 ret = drm_atomic_helper_setup_commit(state, nonblock);
1719 if (ret)
1720 return ret;
1721
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001722 INIT_WORK(&state->commit_work, commit_work);
1723
Daniel Vetter623369e2014-09-16 17:50:47 +02001724 ret = drm_atomic_helper_prepare_planes(dev, state);
1725 if (ret)
1726 return ret;
1727
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001728 if (!nonblock) {
1729 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001730 if (ret)
1731 goto err;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001732 }
1733
Daniel Vetter623369e2014-09-16 17:50:47 +02001734 /*
1735 * This is the point of no return - everything below never fails except
1736 * when the hw goes bonghits. Which means we can commit the new state on
1737 * the software side now.
1738 */
1739
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001740 ret = drm_atomic_helper_swap_state(state, true);
1741 if (ret)
1742 goto err;
Daniel Vetter623369e2014-09-16 17:50:47 +02001743
1744 /*
1745 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001746 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001747 * that the asynchronous work has either been cancelled (if the driver
1748 * supports it, which at least requires that the framebuffers get
1749 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1750 * before the new state gets committed on the software side with
1751 * drm_atomic_helper_swap_state().
1752 *
1753 * This scheme allows new atomic state updates to be prepared and
1754 * checked in parallel to the asynchronous completion of the previous
1755 * update. Which is important since compositors need to figure out the
1756 * composition of the next frame right after having submitted the
1757 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001758 *
1759 * NOTE: Commit work has multiple phases, first hardware commit, then
1760 * cleanup. We want them to overlap, hence need system_unbound_wq to
Kieran Binghama0689e32019-03-12 00:33:07 +00001761 * make sure work items don't artificially stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001762 */
1763
Chris Wilson08536952016-10-14 13:18:18 +01001764 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001765 if (nonblock)
1766 queue_work(system_unbound_wq, &state->commit_work);
1767 else
1768 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001769
1770 return 0;
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001771
1772err:
1773 drm_atomic_helper_cleanup_planes(dev, state);
1774 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +02001775}
1776EXPORT_SYMBOL(drm_atomic_helper_commit);
1777
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001778/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001779 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001780 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001781 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001782 *
1783 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1784 * which commit needs to call which can fail, so we want to run it first and
1785 * synchronously.
1786 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001787 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001788 * might be affected the new state update. This can be done by either cancelling
1789 * or flushing the work items, depending upon whether the driver can deal with
1790 * cancelled updates. Note that it is important to ensure that the framebuffer
1791 * cleanup is still done when cancelling.
1792 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001793 * Asynchronous workers need to have sufficient parallelism to be able to run
1794 * different atomic commits on different CRTCs in parallel. The simplest way to
Kieran Binghama0689e32019-03-12 00:33:07 +00001795 * achieve this is by running them on the &system_unbound_wq work queue. Note
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001796 * that drivers are not required to split up atomic commits and run an
1797 * individual commit in parallel - userspace is supposed to do that if it cares.
1798 * But it might be beneficial to do that for modesets, since those necessarily
1799 * must be done as one global operation, and enabling or disabling a CRTC can
1800 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001801 *
1802 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001803 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001804 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001805 * while it's guaranteed that no relevant nonblocking worker runs means that
1806 * nonblocking workers do not need grab any locks. Actually they must not grab
1807 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001808 *
1809 * 4. Schedule a work item to do all subsequent steps, using the split-out
1810 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1811 * then cleaning up the framebuffers after the old framebuffer is no longer
1812 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001813 *
1814 * The above scheme is implemented in the atomic helper libraries in
1815 * drm_atomic_helper_commit() using a bunch of helper functions. See
1816 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001817 */
1818
Daniel Vettera095caa2016-06-08 17:15:36 +02001819static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1820{
1821 struct drm_crtc_commit *commit, *stall_commit = NULL;
1822 bool completed = true;
1823 int i;
1824 long ret = 0;
1825
1826 spin_lock(&crtc->commit_lock);
1827 i = 0;
1828 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1829 if (i == 0) {
1830 completed = try_wait_for_completion(&commit->flip_done);
1831 /* Userspace is not allowed to get ahead of the previous
1832 * commit with nonblocking ones. */
1833 if (!completed && nonblock) {
1834 spin_unlock(&crtc->commit_lock);
1835 return -EBUSY;
1836 }
1837 } else if (i == 1) {
Maarten Lankhorstf46640b2017-09-04 12:48:36 +02001838 stall_commit = drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001839 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001840 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001841
1842 i++;
1843 }
1844 spin_unlock(&crtc->commit_lock);
1845
1846 if (!stall_commit)
1847 return 0;
1848
1849 /* We don't want to let commits get ahead of cleanup work too much,
1850 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1851 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001852 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001853 10*HZ);
1854 if (ret == 0)
1855 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1856 crtc->base.id, crtc->name);
1857
1858 drm_crtc_commit_put(stall_commit);
1859
1860 return ret < 0 ? ret : 0;
1861}
1862
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001863static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001864{
1865 struct drm_crtc_commit *commit = container_of(completion,
1866 typeof(*commit),
1867 flip_done);
1868
1869 drm_crtc_commit_put(commit);
1870}
1871
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001872static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1873{
1874 init_completion(&commit->flip_done);
1875 init_completion(&commit->hw_done);
1876 init_completion(&commit->cleanup_done);
1877 INIT_LIST_HEAD(&commit->commit_entry);
1878 kref_init(&commit->ref);
1879 commit->crtc = crtc;
1880}
1881
1882static struct drm_crtc_commit *
1883crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1884{
1885 if (crtc) {
1886 struct drm_crtc_state *new_crtc_state;
1887
1888 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1889
1890 return new_crtc_state->commit;
1891 }
1892
1893 if (!state->fake_commit) {
1894 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1895 if (!state->fake_commit)
1896 return NULL;
1897
1898 init_commit(state->fake_commit, NULL);
1899 }
1900
1901 return state->fake_commit;
1902}
1903
Daniel Vettera095caa2016-06-08 17:15:36 +02001904/**
1905 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1906 * @state: new modeset state to be committed
1907 * @nonblock: whether nonblocking behavior is requested.
1908 *
1909 * This function prepares @state to be used by the atomic helper's support for
1910 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001911 * should always call this function from their
1912 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001913 *
1914 * To be able to use this support drivers need to use a few more helper
1915 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1916 * actually committing the hardware state, and for nonblocking commits this call
1917 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
Matt Roper1e55a532019-02-01 17:23:26 -08001918 * and its stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001919 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001920 *
1921 * Completion of the hardware commit step must be signalled using
1922 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1923 * to read or change any permanent software or hardware modeset state. The only
1924 * exception is state protected by other means than &drm_modeset_lock locks.
1925 * Only the free standing @state with pointers to the old state structures can
1926 * be inspected, e.g. to clean up old buffers using
1927 * drm_atomic_helper_cleanup_planes().
1928 *
1929 * At the very end, before cleaning up @state drivers must call
1930 * drm_atomic_helper_commit_cleanup_done().
1931 *
1932 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
Thierry Reding9ac07812017-10-12 16:06:16 +02001933 * complete and easy-to-use default implementation of the atomic_commit() hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001934 *
1935 * The tracking of asynchronously executed and still pending commits is done
1936 * using the core structure &drm_crtc_commit.
1937 *
1938 * By default there's no need to clean up resources allocated by this function
1939 * explicitly: drm_atomic_state_default_clear() will take care of that
1940 * automatically.
1941 *
1942 * Returns:
1943 *
1944 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1945 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1946 */
1947int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1948 bool nonblock)
1949{
1950 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001951 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001952 struct drm_connector *conn;
1953 struct drm_connector_state *old_conn_state, *new_conn_state;
1954 struct drm_plane *plane;
1955 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001956 struct drm_crtc_commit *commit;
1957 int i, ret;
1958
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001959 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001960 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1961 if (!commit)
1962 return -ENOMEM;
1963
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001964 init_commit(commit, crtc);
Daniel Vettera095caa2016-06-08 17:15:36 +02001965
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02001966 new_crtc_state->commit = commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001967
1968 ret = stall_checks(crtc, nonblock);
1969 if (ret)
1970 return ret;
1971
1972 /* Drivers only send out events when at least either current or
1973 * new CRTC state is active. Complete right away if everything
1974 * stays off. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001975 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001976 complete_all(&commit->flip_done);
1977 continue;
1978 }
1979
1980 /* Legacy cursor updates are fully unsynced. */
1981 if (state->legacy_cursor_update) {
1982 complete_all(&commit->flip_done);
1983 continue;
1984 }
1985
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001986 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001987 commit->event = kzalloc(sizeof(*commit->event),
1988 GFP_KERNEL);
1989 if (!commit->event)
1990 return -ENOMEM;
1991
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001992 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02001993 }
1994
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001995 new_crtc_state->event->base.completion = &commit->flip_done;
1996 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01001997 drm_crtc_commit_get(commit);
Leo (Sunpeng) Li1c6ceee2018-01-17 12:51:08 +01001998
1999 commit->abort_completion = true;
Leo Li4364bcb2018-10-15 09:46:40 -04002000
2001 state->crtcs[i].commit = commit;
2002 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02002003 }
2004
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002005 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002006 /* Userspace is not allowed to get ahead of the previous
2007 * commit with nonblocking ones. */
2008 if (nonblock && old_conn_state->commit &&
2009 !try_wait_for_completion(&old_conn_state->commit->flip_done))
2010 return -EBUSY;
2011
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01002012 /* Always track connectors explicitly for e.g. link retraining. */
2013 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002014 if (!commit)
2015 return -ENOMEM;
2016
2017 new_conn_state->commit = drm_crtc_commit_get(commit);
2018 }
2019
2020 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002021 /* Userspace is not allowed to get ahead of the previous
2022 * commit with nonblocking ones. */
2023 if (nonblock && old_plane_state->commit &&
2024 !try_wait_for_completion(&old_plane_state->commit->flip_done))
2025 return -EBUSY;
2026
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01002027 /* Always track planes explicitly for async pageflip support. */
Maarten Lankhorst4edd6082017-10-17 07:20:47 +02002028 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002029 if (!commit)
2030 return -ENOMEM;
2031
2032 new_plane_state->commit = drm_crtc_commit_get(commit);
2033 }
2034
Daniel Vettera095caa2016-06-08 17:15:36 +02002035 return 0;
2036}
2037EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2038
Daniel Vettera095caa2016-06-08 17:15:36 +02002039/**
2040 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002041 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002042 *
2043 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002044 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02002045 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
Thierry Reding277b09c2017-10-12 16:08:57 +02002046 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02002047 *
2048 * This is part of the atomic helper support for nonblocking commits, see
2049 * drm_atomic_helper_setup_commit() for an overview.
2050 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002051void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002052{
2053 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002054 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002055 struct drm_plane *plane;
2056 struct drm_plane_state *old_plane_state;
2057 struct drm_connector *conn;
2058 struct drm_connector_state *old_conn_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002059 struct drm_crtc_commit *commit;
2060 int i;
2061 long ret;
2062
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002063 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2064 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002065
2066 if (!commit)
2067 continue;
2068
2069 ret = wait_for_completion_timeout(&commit->hw_done,
2070 10*HZ);
2071 if (ret == 0)
2072 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2073 crtc->base.id, crtc->name);
2074
2075 /* Currently no support for overwriting flips, hence
2076 * stall for previous one to execute completely. */
2077 ret = wait_for_completion_timeout(&commit->flip_done,
2078 10*HZ);
2079 if (ret == 0)
2080 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2081 crtc->base.id, crtc->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002082 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002083
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002084 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2085 commit = old_conn_state->commit;
2086
2087 if (!commit)
2088 continue;
2089
2090 ret = wait_for_completion_timeout(&commit->hw_done,
2091 10*HZ);
2092 if (ret == 0)
2093 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2094 conn->base.id, conn->name);
2095
2096 /* Currently no support for overwriting flips, hence
2097 * stall for previous one to execute completely. */
2098 ret = wait_for_completion_timeout(&commit->flip_done,
2099 10*HZ);
2100 if (ret == 0)
2101 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2102 conn->base.id, conn->name);
2103 }
2104
2105 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2106 commit = old_plane_state->commit;
2107
2108 if (!commit)
2109 continue;
2110
2111 ret = wait_for_completion_timeout(&commit->hw_done,
2112 10*HZ);
2113 if (ret == 0)
2114 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2115 plane->base.id, plane->name);
2116
2117 /* Currently no support for overwriting flips, hence
2118 * stall for previous one to execute completely. */
2119 ret = wait_for_completion_timeout(&commit->flip_done,
2120 10*HZ);
2121 if (ret == 0)
2122 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2123 plane->base.id, plane->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002124 }
2125}
2126EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2127
2128/**
Boris Brezillonb25c60a2018-07-03 09:50:19 +02002129 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2130 * @old_state: atomic state object with old state structures
2131 *
2132 * This function walks all CRTCs and fake VBLANK events on those with
2133 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2134 * The primary use of this function is writeback connectors working in oneshot
2135 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2136 * when a job is queued, and any change to the pipeline that does not touch the
2137 * connector is leading to timeouts when calling
2138 * drm_atomic_helper_wait_for_vblanks() or
2139 * drm_atomic_helper_wait_for_flip_done().
2140 *
2141 * This is part of the atomic helper support for nonblocking commits, see
2142 * drm_atomic_helper_setup_commit() for an overview.
2143 */
2144void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2145{
2146 struct drm_crtc_state *new_crtc_state;
2147 struct drm_crtc *crtc;
2148 int i;
2149
2150 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2151 unsigned long flags;
2152
2153 if (!new_crtc_state->no_vblank)
2154 continue;
2155
2156 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2157 if (new_crtc_state->event) {
2158 drm_crtc_send_vblank_event(crtc,
2159 new_crtc_state->event);
2160 new_crtc_state->event = NULL;
2161 }
2162 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2163 }
2164}
2165EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2166
2167/**
Daniel Vettera095caa2016-06-08 17:15:36 +02002168 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002169 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002170 *
2171 * This function is used to signal completion of the hardware commit step. After
2172 * this step the driver is not allowed to read or change any permanent software
2173 * or hardware modeset state. The only exception is state protected by other
2174 * means than &drm_modeset_lock locks.
2175 *
2176 * Drivers should try to postpone any expensive or delayed cleanup work after
2177 * this function is called.
2178 *
2179 * This is part of the atomic helper support for nonblocking commits, see
2180 * drm_atomic_helper_setup_commit() for an overview.
2181 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002182void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002183{
2184 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002185 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002186 struct drm_crtc_commit *commit;
2187 int i;
2188
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002189 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2190 commit = new_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002191 if (!commit)
2192 continue;
2193
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002194 /*
2195 * copy new_crtc_state->commit to old_crtc_state->commit,
2196 * it's unsafe to touch new_crtc_state after hw_done,
2197 * but we still need to do so in cleanup_done().
2198 */
2199 if (old_crtc_state->commit)
2200 drm_crtc_commit_put(old_crtc_state->commit);
2201
2202 old_crtc_state->commit = drm_crtc_commit_get(commit);
2203
Daniel Vettera095caa2016-06-08 17:15:36 +02002204 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002205 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02002206 complete_all(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002207 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002208
2209 if (old_state->fake_commit) {
2210 complete_all(&old_state->fake_commit->hw_done);
2211 complete_all(&old_state->fake_commit->flip_done);
2212 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002213}
2214EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2215
2216/**
2217 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002218 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002219 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002220 * This signals completion of the atomic update @old_state, including any
2221 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01002222 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02002223 *
2224 * This is part of the atomic helper support for nonblocking commits, see
2225 * drm_atomic_helper_setup_commit() for an overview.
2226 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002227void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002228{
2229 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002230 struct drm_crtc_state *old_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002231 struct drm_crtc_commit *commit;
2232 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02002233
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002234 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2235 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002236 if (WARN_ON(!commit))
2237 continue;
2238
Daniel Vettera095caa2016-06-08 17:15:36 +02002239 complete_all(&commit->cleanup_done);
2240 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2241
Daniel Vetter7141fd32017-06-21 11:16:27 +02002242 spin_lock(&crtc->commit_lock);
Daniel Vettera095caa2016-06-08 17:15:36 +02002243 list_del(&commit->commit_entry);
2244 spin_unlock(&crtc->commit_lock);
2245 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002246
Ville Syrjälä10a599f2018-11-22 16:34:12 +02002247 if (old_state->fake_commit) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002248 complete_all(&old_state->fake_commit->cleanup_done);
Ville Syrjälä10a599f2018-11-22 16:34:12 +02002249 WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2250 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002251}
2252EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2253
Daniel Vettere8c833a2014-07-27 18:30:19 +02002254/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002255 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002256 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002257 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002258 *
2259 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002260 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2261 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2262 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002263 *
2264 * Returns:
2265 * 0 on success, negative error code on failure.
2266 */
2267int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2268 struct drm_atomic_state *state)
2269{
Laurent Pinchart9d2230d2019-02-21 03:01:38 +02002270 struct drm_connector *connector;
2271 struct drm_connector_state *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002272 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002273 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002274 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002275
Laurent Pinchart9d2230d2019-02-21 03:01:38 +02002276 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2277 if (!new_conn_state->writeback_job)
2278 continue;
2279
2280 ret = drm_writeback_prepare_job(new_conn_state->writeback_job);
2281 if (ret < 0)
2282 return ret;
2283 }
2284
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002285 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002286 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002287
2288 funcs = plane->helper_private;
2289
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002290 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002291 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002292 if (ret)
2293 goto fail;
2294 }
2295 }
2296
2297 return 0;
2298
2299fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002300 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02002301 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002302
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002303 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002304 continue;
2305
2306 funcs = plane->helper_private;
2307
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002308 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002309 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002310 }
2311
2312 return ret;
2313}
2314EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2315
Ville Syrjälä7135ac52016-09-19 16:33:42 +03002316static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002317{
2318 return state->crtc && state->crtc->state->active;
2319}
2320
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002321/**
2322 * drm_atomic_helper_commit_planes - commit plane state
2323 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002324 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08002325 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002326 *
2327 * This function commits the new plane state using the plane and atomic helper
2328 * functions for planes and crtcs. It assumes that the atomic state has already
2329 * been pushed into the relevant object state pointers, since this step can no
2330 * longer fail.
2331 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002332 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002333 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002334 *
2335 * Note that this function does all plane updates across all CRTCs in one step.
2336 * If the hardware can't support this approach look at
2337 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002338 *
2339 * Plane parameters can be updated by applications while the associated CRTC is
2340 * disabled. The DRM/KMS core will store the parameters in the plane state,
2341 * which will be available to the driver when the CRTC is turned on. As a result
2342 * most drivers don't need to be immediately notified of plane updates for a
2343 * disabled CRTC.
2344 *
Liu Ying2b58e982016-08-29 17:12:03 +08002345 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2346 * @flags in order not to receive plane update notifications related to a
2347 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002348 * driver code when the driver and/or hardware can't or just don't need to deal
2349 * with updates on disabled CRTCs, for example when supporting runtime PM.
2350 *
Liu Ying2b58e982016-08-29 17:12:03 +08002351 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2352 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002353 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2354 * call for a plane if the CRTC of the old plane state needs a modesetting
2355 * operation. Of course, the drivers need to disable the planes in their CRTC
2356 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08002357 *
2358 * The drm_atomic_helper_commit() default implementation doesn't set the
2359 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2360 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002361 */
2362void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002363 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08002364 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002365{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002366 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002367 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002368 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002369 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002370 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08002371 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2372 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002373
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002374 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 +02002375 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002376
2377 funcs = crtc->helper_private;
2378
2379 if (!funcs || !funcs->atomic_begin)
2380 continue;
2381
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002382 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002383 continue;
2384
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002385 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002386 }
2387
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002388 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 +02002389 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002390 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002391
2392 funcs = plane->helper_private;
2393
Thierry Reding3cad4b62014-11-25 13:05:12 +01002394 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002395 continue;
2396
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01002397 disabling = drm_atomic_plane_disabling(old_plane_state,
2398 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002399
2400 if (active_only) {
2401 /*
2402 * Skip planes related to inactive CRTCs. If the plane
2403 * is enabled use the state of the current CRTC. If the
2404 * plane is being disabled use the state of the old
2405 * CRTC to avoid skipping planes being disabled on an
2406 * active CRTC.
2407 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002408 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002409 continue;
2410 if (disabling && !plane_crtc_active(old_plane_state))
2411 continue;
2412 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002413
Thierry Reding407b8bd2014-11-20 12:05:50 +01002414 /*
2415 * Special-case disabling the plane if drivers support it.
2416 */
Liu Ying2b58e982016-08-29 17:12:03 +08002417 if (disabling && funcs->atomic_disable) {
2418 struct drm_crtc_state *crtc_state;
2419
2420 crtc_state = old_plane_state->crtc->state;
2421
2422 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2423 no_disable)
2424 continue;
2425
Thierry Reding407b8bd2014-11-20 12:05:50 +01002426 funcs->atomic_disable(plane, old_plane_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002427 } else if (new_plane_state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01002428 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08002429 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002430 }
2431
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002432 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 +02002433 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002434
2435 funcs = crtc->helper_private;
2436
2437 if (!funcs || !funcs->atomic_flush)
2438 continue;
2439
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002440 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002441 continue;
2442
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002443 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002444 }
2445}
2446EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2447
2448/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002449 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2450 * @old_crtc_state: atomic state object with the old crtc state
2451 *
2452 * This function commits the new plane state using the plane and atomic helper
2453 * functions for planes on the specific crtc. It assumes that the atomic state
2454 * has already been pushed into the relevant object state pointers, since this
2455 * step can no longer fail.
2456 *
2457 * This function is useful when plane updates should be done crtc-by-crtc
2458 * instead of one global step like drm_atomic_helper_commit_planes() does.
2459 *
2460 * This function can only be savely used when planes are not allowed to move
2461 * between different CRTCs because this function doesn't handle inter-CRTC
2462 * depencies. Callers need to ensure that either no such depencies exist,
2463 * resolve them through ordering of commit calls or through some other means.
2464 */
2465void
2466drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2467{
2468 const struct drm_crtc_helper_funcs *crtc_funcs;
2469 struct drm_crtc *crtc = old_crtc_state->crtc;
2470 struct drm_atomic_state *old_state = old_crtc_state->state;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002471 struct drm_crtc_state *new_crtc_state =
2472 drm_atomic_get_new_crtc_state(old_state, crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002473 struct drm_plane *plane;
2474 unsigned plane_mask;
2475
2476 plane_mask = old_crtc_state->plane_mask;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002477 plane_mask |= new_crtc_state->plane_mask;
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002478
2479 crtc_funcs = crtc->helper_private;
2480 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002481 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002482
2483 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2484 struct drm_plane_state *old_plane_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01002485 drm_atomic_get_old_plane_state(old_state, plane);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002486 struct drm_plane_state *new_plane_state =
2487 drm_atomic_get_new_plane_state(old_state, plane);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002488 const struct drm_plane_helper_funcs *plane_funcs;
2489
2490 plane_funcs = plane->helper_private;
2491
2492 if (!old_plane_state || !plane_funcs)
2493 continue;
2494
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002495 WARN_ON(new_plane_state->crtc &&
2496 new_plane_state->crtc != crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002497
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002498 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002499 plane_funcs->atomic_disable)
2500 plane_funcs->atomic_disable(plane, old_plane_state);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002501 else if (new_plane_state->crtc ||
2502 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002503 plane_funcs->atomic_update(plane, old_plane_state);
2504 }
2505
2506 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002507 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002508}
2509EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2510
2511/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02002512 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08002513 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02002514 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2515 *
2516 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08002517 * used for instance in the CRTC helper atomic_disable callback to disable
2518 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002519 *
2520 * If the atomic-parameter is set the function calls the CRTC's
2521 * atomic_begin hook before and atomic_flush hook after disabling the
2522 * planes.
2523 *
2524 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002525 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002526 */
Liu Ying28500292016-08-26 15:30:39 +08002527void
2528drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2529 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002530{
Liu Ying28500292016-08-26 15:30:39 +08002531 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02002532 const struct drm_crtc_helper_funcs *crtc_funcs =
2533 crtc->helper_private;
2534 struct drm_plane *plane;
2535
2536 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2537 crtc_funcs->atomic_begin(crtc, NULL);
2538
Liu Ying28500292016-08-26 15:30:39 +08002539 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02002540 const struct drm_plane_helper_funcs *plane_funcs =
2541 plane->helper_private;
2542
Liu Ying28500292016-08-26 15:30:39 +08002543 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002544 continue;
2545
2546 WARN_ON(!plane_funcs->atomic_disable);
2547 if (plane_funcs->atomic_disable)
2548 plane_funcs->atomic_disable(plane, NULL);
2549 }
2550
2551 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2552 crtc_funcs->atomic_flush(crtc, NULL);
2553}
2554EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2555
2556/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002557 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2558 * @dev: DRM device
2559 * @old_state: atomic state object with old state structures
2560 *
2561 * This function cleans up plane state, specifically framebuffers, from the old
2562 * configuration. Hence the old configuration must be perserved in @old_state to
2563 * be able to call this function.
2564 *
2565 * This function must also be called on the new state when the atomic update
2566 * fails at any point after calling drm_atomic_helper_prepare_planes().
2567 */
2568void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2569 struct drm_atomic_state *old_state)
2570{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002571 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002572 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002573 int i;
2574
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002575 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 +02002576 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002577 struct drm_plane_state *plane_state;
2578
2579 /*
2580 * This might be called before swapping when commit is aborted,
2581 * in which case we have to cleanup the new state.
2582 */
2583 if (old_plane_state == plane->state)
2584 plane_state = new_plane_state;
2585 else
2586 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002587
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002588 funcs = plane->helper_private;
2589
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002590 if (funcs->cleanup_fb)
2591 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002592 }
2593}
2594EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2595
2596/**
2597 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002598 * @state: atomic state
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002599 * @stall: stall for preceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002600 *
2601 * This function stores the atomic state into the current state pointers in all
2602 * driver objects. It should be called after all failing steps have been done
2603 * and succeeded, but before the actual hardware state is committed.
2604 *
2605 * For cleanup and error recovery the current state for all changed objects will
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002606 * be swapped into @state.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002607 *
2608 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2609 *
2610 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2611 *
2612 * 2. Do any other steps that might fail.
2613 *
2614 * 3. Put the staged state into the current state pointers with this function.
2615 *
2616 * 4. Actually commit the hardware state.
2617 *
Daniel Vetter26196f72015-08-25 16:26:03 +02002618 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002619 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02002620 *
2621 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002622 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2623 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02002624 * don't pass the right state structures to the callbacks.
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002625 *
2626 * Returns:
2627 *
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002628 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2629 * waiting for the previous commits has been interrupted.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002630 */
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002631int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
Daniel Vetter5e84c262016-06-10 00:06:32 +02002632 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002633{
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002634 int i, ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002635 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002636 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002637 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002638 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002639 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002640 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002641 struct drm_crtc_commit *commit;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002642 struct drm_private_obj *obj;
2643 struct drm_private_state *old_obj_state, *new_obj_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002644
2645 if (stall) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002646 /*
2647 * We have to stall for hw_done here before
2648 * drm_atomic_helper_wait_for_dependencies() because flip
2649 * depth > 1 is not yet supported by all drivers. As long as
2650 * obj->state is directly dereferenced anywhere in the drivers
2651 * atomic_commit_tail function, then it's unsafe to swap state
2652 * before drm_atomic_helper_commit_hw_done() is called.
2653 */
2654
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002655 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2656 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002657
2658 if (!commit)
2659 continue;
2660
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002661 ret = wait_for_completion_interruptible(&commit->hw_done);
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002662 if (ret)
2663 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002664 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002665
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002666 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2667 commit = old_conn_state->commit;
2668
2669 if (!commit)
2670 continue;
2671
2672 ret = wait_for_completion_interruptible(&commit->hw_done);
2673 if (ret)
2674 return ret;
2675 }
2676
2677 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2678 commit = old_plane_state->commit;
2679
2680 if (!commit)
2681 continue;
2682
2683 ret = wait_for_completion_interruptible(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002684 if (ret)
2685 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002686 }
2687 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002688
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002689 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002690 WARN_ON(connector->state != old_conn_state);
2691
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002692 old_conn_state->state = state;
2693 new_conn_state->state = NULL;
2694
2695 state->connectors[i].state = old_conn_state;
2696 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002697 }
2698
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002699 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002700 WARN_ON(crtc->state != old_crtc_state);
2701
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002702 old_crtc_state->state = state;
2703 new_crtc_state->state = NULL;
2704
2705 state->crtcs[i].state = old_crtc_state;
2706 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002707
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002708 if (new_crtc_state->commit) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002709 spin_lock(&crtc->commit_lock);
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002710 list_add(&new_crtc_state->commit->commit_entry,
Daniel Vettera095caa2016-06-08 17:15:36 +02002711 &crtc->commit_list);
2712 spin_unlock(&crtc->commit_lock);
2713
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002714 new_crtc_state->commit->event = NULL;
Daniel Vettera095caa2016-06-08 17:15:36 +02002715 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002716 }
2717
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002718 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002719 WARN_ON(plane->state != old_plane_state);
2720
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002721 old_plane_state->state = state;
2722 new_plane_state->state = NULL;
2723
2724 state->planes[i].state = old_plane_state;
2725 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002726 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07002727
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002728 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2729 WARN_ON(obj->state != old_obj_state);
2730
2731 old_obj_state->state = state;
2732 new_obj_state->state = NULL;
2733
2734 state->private_objs[i].state = old_obj_state;
2735 obj->state = new_obj_state;
2736 }
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002737
2738 return 0;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002739}
2740EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002741
2742/**
2743 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2744 * @plane: plane object to update
2745 * @crtc: owning CRTC of owning plane
2746 * @fb: framebuffer to flip onto plane
2747 * @crtc_x: x offset of primary plane on crtc
2748 * @crtc_y: y offset of primary plane on crtc
2749 * @crtc_w: width of primary plane rectangle on crtc
2750 * @crtc_h: height of primary plane rectangle on crtc
2751 * @src_x: x offset of @fb for panning
2752 * @src_y: y offset of @fb for panning
2753 * @src_w: width of source rectangle in @fb
2754 * @src_h: height of source rectangle in @fb
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002755 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002756 *
2757 * Provides a default plane update handler using the atomic driver interface.
2758 *
2759 * RETURNS:
2760 * Zero on success, error code on failure
2761 */
2762int drm_atomic_helper_update_plane(struct drm_plane *plane,
2763 struct drm_crtc *crtc,
2764 struct drm_framebuffer *fb,
2765 int crtc_x, int crtc_y,
2766 unsigned int crtc_w, unsigned int crtc_h,
2767 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002768 uint32_t src_w, uint32_t src_h,
2769 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002770{
2771 struct drm_atomic_state *state;
2772 struct drm_plane_state *plane_state;
2773 int ret = 0;
2774
2775 state = drm_atomic_state_alloc(plane->dev);
2776 if (!state)
2777 return -ENOMEM;
2778
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002779 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002780 plane_state = drm_atomic_get_plane_state(state, plane);
2781 if (IS_ERR(plane_state)) {
2782 ret = PTR_ERR(plane_state);
2783 goto fail;
2784 }
2785
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002786 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002787 if (ret != 0)
2788 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002789 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002790 plane_state->crtc_x = crtc_x;
2791 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002792 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002793 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002794 plane_state->src_x = src_x;
2795 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002796 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002797 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002798
Daniel Vetter3671c582015-05-04 15:40:52 +02002799 if (plane == crtc->cursor)
2800 state->legacy_cursor_update = true;
2801
Daniel Vetter042652e2014-07-27 13:46:52 +02002802 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002803fail:
Chris Wilson08536952016-10-14 13:18:18 +01002804 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002805 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002806}
2807EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2808
2809/**
2810 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2811 * @plane: plane to disable
Daniel Vetter19315292017-03-22 22:50:43 +01002812 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002813 *
2814 * Provides a default plane disable handler using the atomic driver interface.
2815 *
2816 * RETURNS:
2817 * Zero on success, error code on failure
2818 */
Daniel Vetter19315292017-03-22 22:50:43 +01002819int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2820 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002821{
2822 struct drm_atomic_state *state;
2823 struct drm_plane_state *plane_state;
2824 int ret = 0;
2825
2826 state = drm_atomic_state_alloc(plane->dev);
2827 if (!state)
2828 return -ENOMEM;
2829
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002830 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002831 plane_state = drm_atomic_get_plane_state(state, plane);
2832 if (IS_ERR(plane_state)) {
2833 ret = PTR_ERR(plane_state);
2834 goto fail;
2835 }
2836
Ville Syrjäläa36c0272018-03-22 17:22:58 +02002837 if (plane_state->crtc && plane_state->crtc->cursor == plane)
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002838 plane_state->state->legacy_cursor_update = true;
2839
Rob Clarkbbb1e522015-08-25 15:35:58 -04002840 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002841 if (ret != 0)
2842 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002843
Daniel Vetter042652e2014-07-27 13:46:52 +02002844 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002845fail:
Chris Wilson08536952016-10-14 13:18:18 +01002846 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002847 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002848}
2849EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2850
Daniel Vetter042652e2014-07-27 13:46:52 +02002851/**
2852 * drm_atomic_helper_set_config - set a new config from userspace
2853 * @set: mode set configuration
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002854 * @ctx: lock acquisition context
Daniel Vetter042652e2014-07-27 13:46:52 +02002855 *
2856 * Provides a default crtc set_config handler using the atomic driver interface.
2857 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002858 * NOTE: For backwards compatibility with old userspace this automatically
2859 * resets the "link-status" property to GOOD, to force any link
2860 * re-training. The SETCRTC ioctl does not define whether an update does
2861 * need a full modeset or just a plane update, hence we're allowed to do
Daniel Vetter97e14fb2018-07-09 10:40:08 +02002862 * that. See also drm_connector_set_link_status_property().
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002863 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002864 * Returns:
2865 * Returns 0 on success, negative errno numbers on failure.
2866 */
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002867int drm_atomic_helper_set_config(struct drm_mode_set *set,
2868 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002869{
2870 struct drm_atomic_state *state;
2871 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002872 int ret = 0;
2873
2874 state = drm_atomic_state_alloc(crtc->dev);
2875 if (!state)
2876 return -ENOMEM;
2877
Daniel Vetter38b64412017-03-22 22:50:58 +01002878 state->acquire_ctx = ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002879 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002880 if (ret != 0)
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002881 goto fail;
Daniel Stone819364d2015-05-26 14:36:48 +01002882
Maarten Lankhorst44596b82017-04-06 13:19:00 +02002883 ret = handle_conflicting_encoders(state, true);
2884 if (ret)
2885 return ret;
2886
Daniel Vetter042652e2014-07-27 13:46:52 +02002887 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002888
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002889fail:
Chris Wilson08536952016-10-14 13:18:18 +01002890 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002891 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002892}
2893EXPORT_SYMBOL(drm_atomic_helper_set_config);
2894
Sean Paul37406a62019-02-12 12:32:41 -05002895/**
2896 * drm_atomic_helper_disable_all - disable all currently active outputs
2897 * @dev: DRM device
2898 * @ctx: lock acquisition context
2899 *
2900 * Loops through all connectors, finding those that aren't turned off and then
2901 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2902 * that they are connected to.
2903 *
2904 * This is used for example in suspend/resume to disable all currently active
2905 * functions when suspending. If you just want to shut down everything at e.g.
2906 * driver unload, look at drm_atomic_helper_shutdown().
2907 *
2908 * Note that if callers haven't already acquired all modeset locks this might
2909 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2910 *
2911 * Returns:
2912 * 0 on success or a negative error code on failure.
2913 *
2914 * See also:
2915 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
2916 * drm_atomic_helper_shutdown().
2917 */
2918int drm_atomic_helper_disable_all(struct drm_device *dev,
2919 struct drm_modeset_acquire_ctx *ctx)
Thierry Reding14942762015-12-02 17:50:04 +01002920{
2921 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002922 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01002923 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002924 struct drm_plane_state *plane_state;
2925 struct drm_plane *plane;
2926 struct drm_crtc_state *crtc_state;
2927 struct drm_crtc *crtc;
2928 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01002929
2930 state = drm_atomic_state_alloc(dev);
2931 if (!state)
2932 return -ENOMEM;
2933
2934 state->acquire_ctx = ctx;
2935
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002936 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01002937 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2938 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002939 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01002940 goto free;
2941 }
2942
2943 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002944
2945 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2946 if (ret < 0)
2947 goto free;
2948
2949 ret = drm_atomic_add_affected_planes(state, crtc);
2950 if (ret < 0)
2951 goto free;
2952
2953 ret = drm_atomic_add_affected_connectors(state, crtc);
2954 if (ret < 0)
2955 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01002956 }
2957
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02002958 for_each_new_connector_in_state(state, conn, conn_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002959 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2960 if (ret < 0)
2961 goto free;
2962 }
2963
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02002964 for_each_new_plane_in_state(state, plane, plane_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002965 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2966 if (ret < 0)
2967 goto free;
2968
2969 drm_atomic_set_fb_for_plane(plane_state, NULL);
2970 }
2971
2972 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01002973free:
Chris Wilson08536952016-10-14 13:18:18 +01002974 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01002975 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01002976}
2977EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2978
2979/**
Daniel Vetter18dddad2017-03-21 17:41:49 +01002980 * drm_atomic_helper_shutdown - shutdown all CRTC
2981 * @dev: DRM device
2982 *
2983 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
2984 * suspend should instead be handled with drm_atomic_helper_suspend(), since
2985 * that also takes a snapshot of the modeset state to be restored on resume.
2986 *
2987 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
2988 * and it is the atomic version of drm_crtc_force_disable_all().
2989 */
2990void drm_atomic_helper_shutdown(struct drm_device *dev)
2991{
2992 struct drm_modeset_acquire_ctx ctx;
2993 int ret;
2994
Sean Paulb7ea04d2018-11-29 10:04:17 -05002995 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
Daniel Vetter18dddad2017-03-21 17:41:49 +01002996
Sean Paul37406a62019-02-12 12:32:41 -05002997 ret = drm_atomic_helper_disable_all(dev, &ctx);
Daniel Vetter18dddad2017-03-21 17:41:49 +01002998 if (ret)
2999 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3000
Sean Paulb7ea04d2018-11-29 10:04:17 -05003001 DRM_MODESET_LOCK_ALL_END(ctx, ret);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003002}
3003EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3004
3005/**
Daniel Vetter1d8224e2018-11-28 11:07:28 +01003006 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3007 * @dev: DRM device
3008 * @ctx: lock acquisition context
3009 *
3010 * Makes a copy of the current atomic state by looping over all objects and
3011 * duplicating their respective states. This is used for example by suspend/
3012 * resume support code to save the state prior to suspend such that it can
3013 * be restored upon resume.
3014 *
3015 * Note that this treats atomic state as persistent between save and restore.
3016 * Drivers must make sure that this is possible and won't result in confusion
3017 * or erroneous behaviour.
3018 *
3019 * Note that if callers haven't already acquired all modeset locks this might
3020 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3021 *
3022 * Returns:
3023 * A pointer to the copy of the atomic state object on success or an
3024 * ERR_PTR()-encoded error code on failure.
3025 *
3026 * See also:
3027 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3028 */
3029struct drm_atomic_state *
3030drm_atomic_helper_duplicate_state(struct drm_device *dev,
3031 struct drm_modeset_acquire_ctx *ctx)
3032{
3033 struct drm_atomic_state *state;
3034 struct drm_connector *conn;
3035 struct drm_connector_list_iter conn_iter;
3036 struct drm_plane *plane;
3037 struct drm_crtc *crtc;
3038 int err = 0;
3039
3040 state = drm_atomic_state_alloc(dev);
3041 if (!state)
3042 return ERR_PTR(-ENOMEM);
3043
3044 state->acquire_ctx = ctx;
Lyude Paul022deba2019-02-01 19:20:03 -05003045 state->duplicated = true;
Daniel Vetter1d8224e2018-11-28 11:07:28 +01003046
3047 drm_for_each_crtc(crtc, dev) {
3048 struct drm_crtc_state *crtc_state;
3049
3050 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3051 if (IS_ERR(crtc_state)) {
3052 err = PTR_ERR(crtc_state);
3053 goto free;
3054 }
3055 }
3056
3057 drm_for_each_plane(plane, dev) {
3058 struct drm_plane_state *plane_state;
3059
3060 plane_state = drm_atomic_get_plane_state(state, plane);
3061 if (IS_ERR(plane_state)) {
3062 err = PTR_ERR(plane_state);
3063 goto free;
3064 }
3065 }
3066
3067 drm_connector_list_iter_begin(dev, &conn_iter);
3068 drm_for_each_connector_iter(conn, &conn_iter) {
3069 struct drm_connector_state *conn_state;
3070
3071 conn_state = drm_atomic_get_connector_state(state, conn);
3072 if (IS_ERR(conn_state)) {
3073 err = PTR_ERR(conn_state);
3074 drm_connector_list_iter_end(&conn_iter);
3075 goto free;
3076 }
3077 }
3078 drm_connector_list_iter_end(&conn_iter);
3079
3080 /* clear the acquire context so that it isn't accidentally reused */
3081 state->acquire_ctx = NULL;
3082
3083free:
3084 if (err < 0) {
3085 drm_atomic_state_put(state);
3086 state = ERR_PTR(err);
3087 }
3088
3089 return state;
3090}
3091EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3092
3093/**
Thierry Reding14942762015-12-02 17:50:04 +01003094 * drm_atomic_helper_suspend - subsystem-level suspend helper
3095 * @dev: DRM device
3096 *
3097 * Duplicates the current atomic state, disables all active outputs and then
3098 * returns a pointer to the original atomic state to the caller. Drivers can
3099 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3100 * restore the output configuration that was active at the time the system
3101 * entered suspend.
3102 *
3103 * Note that it is potentially unsafe to use this. The atomic state object
3104 * returned by this function is assumed to be persistent. Drivers must ensure
3105 * that this holds true. Before calling this function, drivers must make sure
3106 * to suspend fbdev emulation so that nothing can be using the device.
3107 *
3108 * Returns:
3109 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3110 * encoded error code on failure. Drivers should store the returned atomic
3111 * state object and pass it to the drm_atomic_helper_resume() helper upon
3112 * resume.
3113 *
3114 * See also:
3115 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003116 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01003117 */
3118struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3119{
3120 struct drm_modeset_acquire_ctx ctx;
3121 struct drm_atomic_state *state;
3122 int err;
3123
Sean Paul615aa3d2018-11-29 15:36:48 -05003124 /* This can never be returned, but it makes the compiler happy */
3125 state = ERR_PTR(-EINVAL);
Thierry Reding14942762015-12-02 17:50:04 +01003126
Sean Paulb7ea04d2018-11-29 10:04:17 -05003127 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
Thierry Reding14942762015-12-02 17:50:04 +01003128
3129 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3130 if (IS_ERR(state))
3131 goto unlock;
3132
3133 err = drm_atomic_helper_disable_all(dev, &ctx);
3134 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003135 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01003136 state = ERR_PTR(err);
3137 goto unlock;
3138 }
3139
3140unlock:
Sean Paulb7ea04d2018-11-29 10:04:17 -05003141 DRM_MODESET_LOCK_ALL_END(ctx, err);
3142 if (err)
3143 return ERR_PTR(err);
Thierry Reding14942762015-12-02 17:50:04 +01003144
Thierry Reding14942762015-12-02 17:50:04 +01003145 return state;
3146}
3147EXPORT_SYMBOL(drm_atomic_helper_suspend);
3148
3149/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003150 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3151 * @state: duplicated atomic state to commit
3152 * @ctx: pointer to acquire_ctx to use for commit.
3153 *
3154 * The state returned by drm_atomic_helper_duplicate_state() and
3155 * drm_atomic_helper_suspend() is partially invalid, and needs to
3156 * be fixed up before commit.
3157 *
3158 * Returns:
3159 * 0 on success or a negative error code on failure.
3160 *
3161 * See also:
3162 * drm_atomic_helper_suspend()
3163 */
3164int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3165 struct drm_modeset_acquire_ctx *ctx)
3166{
Sean Paulaa394b02018-11-29 10:04:14 -05003167 int i, ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003168 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003169 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003170 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003171 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003172 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003173 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003174
3175 state->acquire_ctx = ctx;
3176
Ville Syrjäläe00fb852018-05-25 21:50:45 +03003177 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003178 state->planes[i].old_state = plane->state;
3179
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003180 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003181 state->crtcs[i].old_state = crtc->state;
3182
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003183 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003184 state->connectors[i].old_state = connector->state;
3185
Sean Paulaa394b02018-11-29 10:04:14 -05003186 ret = drm_atomic_commit(state);
3187
3188 state->acquire_ctx = NULL;
3189
3190 return ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003191}
3192EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3193
3194/**
Thierry Reding14942762015-12-02 17:50:04 +01003195 * drm_atomic_helper_resume - subsystem-level resume helper
3196 * @dev: DRM device
3197 * @state: atomic state to resume to
3198 *
3199 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3200 * grabs all modeset locks and commits the atomic state object. This can be
3201 * used in conjunction with the drm_atomic_helper_suspend() helper to
3202 * implement suspend/resume for drivers that support atomic mode-setting.
3203 *
3204 * Returns:
3205 * 0 on success or a negative error code on failure.
3206 *
3207 * See also:
3208 * drm_atomic_helper_suspend()
3209 */
3210int drm_atomic_helper_resume(struct drm_device *dev,
3211 struct drm_atomic_state *state)
3212{
Daniel Vettera5b84442017-04-03 10:32:53 +02003213 struct drm_modeset_acquire_ctx ctx;
Thierry Reding14942762015-12-02 17:50:04 +01003214 int err;
3215
3216 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003217
Sean Paulb7ea04d2018-11-29 10:04:17 -05003218 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
Daniel Vetter869e1882017-05-31 10:38:13 +02003219
Sean Paulb7ea04d2018-11-29 10:04:17 -05003220 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
Daniel Vettera5b84442017-04-03 10:32:53 +02003221
Sean Paulb7ea04d2018-11-29 10:04:17 -05003222 DRM_MODESET_LOCK_ALL_END(ctx, err);
Jeffy Chen6d281b12017-10-09 14:46:41 +08003223 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01003224
3225 return err;
3226}
3227EXPORT_SYMBOL(drm_atomic_helper_resume);
3228
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003229static int page_flip_common(struct drm_atomic_state *state,
3230 struct drm_crtc *crtc,
3231 struct drm_framebuffer *fb,
3232 struct drm_pending_vblank_event *event,
3233 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003234{
3235 struct drm_plane *plane = crtc->primary;
3236 struct drm_plane_state *plane_state;
3237 struct drm_crtc_state *crtc_state;
3238 int ret = 0;
3239
3240 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3241 if (IS_ERR(crtc_state))
3242 return PTR_ERR(crtc_state);
3243
3244 crtc_state->event = event;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003245 crtc_state->pageflip_flags = flags;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003246
3247 plane_state = drm_atomic_get_plane_state(state, plane);
3248 if (IS_ERR(plane_state))
3249 return PTR_ERR(plane_state);
3250
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003251 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3252 if (ret != 0)
3253 return ret;
3254 drm_atomic_set_fb_for_plane(plane_state, fb);
3255
3256 /* Make sure we don't accidentally do a full modeset. */
3257 state->allow_modeset = false;
3258 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00003259 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3260 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003261 return -EINVAL;
3262 }
3263
3264 return ret;
3265}
3266
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003267/**
3268 * drm_atomic_helper_page_flip - execute a legacy page flip
3269 * @crtc: DRM crtc
3270 * @fb: DRM framebuffer
3271 * @event: optional DRM event to signal upon completion
3272 * @flags: flip flags for non-vblank sync'ed updates
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003273 * @ctx: lock acquisition context
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003274 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003275 * Provides a default &drm_crtc_funcs.page_flip implementation
3276 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003277 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003278 * Returns:
3279 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003280 *
3281 * See also:
3282 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003283 */
3284int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3285 struct drm_framebuffer *fb,
3286 struct drm_pending_vblank_event *event,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003287 uint32_t flags,
3288 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003289{
3290 struct drm_plane *plane = crtc->primary;
3291 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003292 int ret = 0;
3293
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003294 state = drm_atomic_state_alloc(plane->dev);
3295 if (!state)
3296 return -ENOMEM;
3297
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003298 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003299
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003300 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003301 if (ret != 0)
3302 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01003303
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02003304 ret = drm_atomic_nonblocking_commit(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003305fail:
Chris Wilson08536952016-10-14 13:18:18 +01003306 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003307 return ret;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003308}
3309EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01003310
3311/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003312 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3313 * @crtc: DRM crtc
3314 * @fb: DRM framebuffer
3315 * @event: optional DRM event to signal upon completion
3316 * @flags: flip flags for non-vblank sync'ed updates
3317 * @target: specifying the target vblank period when the flip to take effect
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003318 * @ctx: lock acquisition context
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003319 *
3320 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3321 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3322 * target vblank period to flip.
3323 *
3324 * Returns:
3325 * Returns 0 on success, negative errno numbers on failure.
3326 */
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003327int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3328 struct drm_framebuffer *fb,
3329 struct drm_pending_vblank_event *event,
3330 uint32_t flags,
3331 uint32_t target,
3332 struct drm_modeset_acquire_ctx *ctx)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003333{
3334 struct drm_plane *plane = crtc->primary;
3335 struct drm_atomic_state *state;
3336 struct drm_crtc_state *crtc_state;
3337 int ret = 0;
3338
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003339 state = drm_atomic_state_alloc(plane->dev);
3340 if (!state)
3341 return -ENOMEM;
3342
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003343 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003344
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003345 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003346 if (ret != 0)
3347 goto fail;
3348
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01003349 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003350 if (WARN_ON(!crtc_state)) {
3351 ret = -EINVAL;
3352 goto fail;
3353 }
3354 crtc_state->target_vblank = target;
3355
3356 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003357fail:
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003358 drm_atomic_state_put(state);
3359 return ret;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003360}
3361EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
Daniel Vetter1d8224e2018-11-28 11:07:28 +01003362
3363/**
3364 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3365 * @crtc: CRTC object
3366 * @red: red correction table
3367 * @green: green correction table
3368 * @blue: green correction table
3369 * @size: size of the tables
3370 * @ctx: lock acquire context
3371 *
3372 * Implements support for legacy gamma correction table for drivers
3373 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3374 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3375 * how the atomic color management and gamma tables work.
3376 */
3377int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3378 u16 *red, u16 *green, u16 *blue,
3379 uint32_t size,
3380 struct drm_modeset_acquire_ctx *ctx)
3381{
3382 struct drm_device *dev = crtc->dev;
3383 struct drm_atomic_state *state;
3384 struct drm_crtc_state *crtc_state;
3385 struct drm_property_blob *blob = NULL;
3386 struct drm_color_lut *blob_data;
3387 int i, ret = 0;
3388 bool replaced;
3389
3390 state = drm_atomic_state_alloc(crtc->dev);
3391 if (!state)
3392 return -ENOMEM;
3393
3394 blob = drm_property_create_blob(dev,
3395 sizeof(struct drm_color_lut) * size,
3396 NULL);
3397 if (IS_ERR(blob)) {
3398 ret = PTR_ERR(blob);
3399 blob = NULL;
3400 goto fail;
3401 }
3402
3403 /* Prepare GAMMA_LUT with the legacy values. */
3404 blob_data = blob->data;
3405 for (i = 0; i < size; i++) {
3406 blob_data[i].red = red[i];
3407 blob_data[i].green = green[i];
3408 blob_data[i].blue = blue[i];
3409 }
3410
3411 state->acquire_ctx = ctx;
3412 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3413 if (IS_ERR(crtc_state)) {
3414 ret = PTR_ERR(crtc_state);
3415 goto fail;
3416 }
3417
3418 /* Reset DEGAMMA_LUT and CTM properties. */
3419 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3420 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3421 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3422 crtc_state->color_mgmt_changed |= replaced;
3423
3424 ret = drm_atomic_commit(state);
3425
3426fail:
3427 drm_atomic_state_put(state);
3428 drm_property_blob_put(blob);
3429 return ret;
3430}
3431EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);