blob: 71c70a031a0437e7eaf92118d6b8aab17f369dfd [file] [log] [blame]
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28#include <drm/drmP.h>
29#include <drm/drm_atomic.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020032#include <drm/drm_atomic_helper.h>
Brian Starkey935774c2017-03-29 17:42:32 +010033#include <drm/drm_writeback.h>
Chris Wilsonf54d1862016-10-25 13:00:45 +010034#include <linux/dma-fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010035
Jose Abreufaf94a02017-05-25 15:19:16 +010036#include "drm_crtc_helper_internal.h"
Marek Szyprowski44d1240d2016-06-13 11:11:26 +020037#include "drm_crtc_internal.h"
38
Daniel Vetter3150c7d2014-11-06 20:53:29 +010039/**
40 * DOC: overview
41 *
42 * This helper library provides implementations of check and commit functions on
43 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
44 * also provides convenience implementations for the atomic state handling
45 * callbacks for drivers which don't need to subclass the drm core structures to
46 * add their own additional internal state.
47 *
48 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020049 * drm_atomic_helper_check() and for the commit callback with
50 * drm_atomic_helper_commit(). But the individual stages and callbacks are
51 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010052 * together with a driver private modeset implementation.
53 *
54 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020055 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
56 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010057 * various functions to implement set_property callbacks. New drivers must not
58 * implement these functions themselves but must use the provided helpers.
Daniel Vetter092d01d2015-12-04 09:45:44 +010059 *
60 * The atomic helper uses the same function table structures as all other
Daniel Vetterea0dd852016-12-29 21:48:26 +010061 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
62 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
63 * also shares the &struct drm_plane_helper_funcs function table with the plane
Daniel Vetter092d01d2015-12-04 09:45:44 +010064 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010065 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010066static void
67drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010068 struct drm_plane_state *old_plane_state,
Daniel Vetterc2fcd272014-11-05 00:14:14 +010069 struct drm_plane_state *plane_state,
70 struct drm_plane *plane)
71{
72 struct drm_crtc_state *crtc_state;
73
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010074 if (old_plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010075 crtc_state = drm_atomic_get_new_crtc_state(state,
76 old_plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010077
78 if (WARN_ON(!crtc_state))
79 return;
80
81 crtc_state->planes_changed = true;
82 }
83
84 if (plane_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +010085 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
Daniel Vetterc2fcd272014-11-05 00:14:14 +010086
87 if (WARN_ON(!crtc_state))
88 return;
89
90 crtc_state->planes_changed = true;
91 }
92}
93
Maarten Lankhorst8248b652016-03-03 10:17:40 +010094static int handle_conflicting_encoders(struct drm_atomic_state *state,
95 bool disable_conflicting_encoders)
Daniel Vetter97ac3202015-12-03 10:49:14 +010096{
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +010097 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +020098 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +010099 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100100 struct drm_encoder *encoder;
101 unsigned encoder_mask = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100102 int i, ret = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200103
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100104 /*
105 * First loop, find all newly assigned encoders from the connectors
106 * part of the state. If the same encoder is assigned to multiple
107 * connectors bail out.
108 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100109 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100110 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
111 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200112
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100113 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200114 continue;
115
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100116 if (funcs->atomic_best_encoder)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100117 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200118 else if (funcs->best_encoder)
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100119 new_encoder = funcs->best_encoder(connector);
Boris Brezillona0909cc2016-06-01 18:03:37 +0200120 else
121 new_encoder = drm_atomic_helper_best_encoder(connector);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100122
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100123 if (new_encoder) {
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300124 if (encoder_mask & drm_encoder_mask(new_encoder)) {
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100125 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
126 new_encoder->base.id, new_encoder->name,
127 connector->base.id, connector->name);
128
129 return -EINVAL;
130 }
131
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300132 encoder_mask |= drm_encoder_mask(new_encoder);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100133 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200134 }
135
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100136 if (!encoder_mask)
137 return 0;
138
139 /*
140 * Second loop, iterate over all connectors not part of the state.
141 *
142 * If a conflicting encoder is found and disable_conflicting_encoders
143 * is not set, an error is returned. Userspace can provide a solution
144 * through the atomic ioctl.
145 *
146 * If the flag is set conflicting connectors are removed from the crtc
147 * and the crtc is disabled if no encoder is left. This preserves
148 * compatibility with the legacy set_config behavior.
149 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100150 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100151 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100152 struct drm_crtc_state *crtc_state;
153
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100154 if (drm_atomic_get_new_connector_state(state, connector))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100155 continue;
156
157 encoder = connector->state->best_encoder;
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300158 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100159 continue;
160
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100161 if (!disable_conflicting_encoders) {
162 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
163 encoder->base.id, encoder->name,
164 connector->state->crtc->base.id,
165 connector->state->crtc->name,
166 connector->base.id, connector->name);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100167 ret = -EINVAL;
168 goto out;
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100169 }
170
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100171 new_conn_state = drm_atomic_get_connector_state(state, connector);
172 if (IS_ERR(new_conn_state)) {
173 ret = PTR_ERR(new_conn_state);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100174 goto out;
175 }
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100176
177 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
178 encoder->base.id, encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100179 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100180 connector->base.id, connector->name);
181
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100182 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100183
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100184 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100185 if (ret)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100186 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100187
188 if (!crtc_state->connector_mask) {
189 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
190 NULL);
191 if (ret < 0)
Daniel Vetterc36a3252016-12-15 16:58:43 +0100192 goto out;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100193
194 crtc_state->active = false;
195 }
196 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100197out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100198 drm_connector_list_iter_end(&conn_iter);
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100199
Daniel Vetterc36a3252016-12-15 16:58:43 +0100200 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200201}
202
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100203static void
204set_best_encoder(struct drm_atomic_state *state,
205 struct drm_connector_state *conn_state,
206 struct drm_encoder *encoder)
207{
208 struct drm_crtc_state *crtc_state;
209 struct drm_crtc *crtc;
210
211 if (conn_state->best_encoder) {
212 /* Unset the encoder_mask in the old crtc state. */
213 crtc = conn_state->connector->state->crtc;
214
215 /* A NULL crtc is an error here because we should have
216 * duplicated a NULL best_encoder when crtc was NULL.
217 * As an exception restoring duplicated atomic state
218 * during resume is allowed, so don't warn when
219 * best_encoder is equal to encoder we intend to set.
220 */
221 WARN_ON(!crtc && encoder != conn_state->best_encoder);
222 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100223 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100224
225 crtc_state->encoder_mask &=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300226 ~drm_encoder_mask(conn_state->best_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100227 }
228 }
229
230 if (encoder) {
231 crtc = conn_state->crtc;
232 WARN_ON(!crtc);
233 if (crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100234 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100235
236 crtc_state->encoder_mask |=
Ville Syrjälä6f3be032018-06-26 22:47:09 +0300237 drm_encoder_mask(encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100238 }
239 }
240
241 conn_state->best_encoder = encoder;
242}
243
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100244static void
Daniel Vetter623369e2014-09-16 17:50:47 +0200245steal_encoder(struct drm_atomic_state *state,
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100246 struct drm_encoder *encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200247{
Daniel Vetter623369e2014-09-16 17:50:47 +0200248 struct drm_crtc_state *crtc_state;
249 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100250 struct drm_connector_state *old_connector_state, *new_connector_state;
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100251 int i;
Daniel Vetter623369e2014-09-16 17:50:47 +0200252
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100253 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100254 struct drm_crtc *encoder_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200255
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100256 if (new_connector_state->best_encoder != encoder)
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100257 continue;
258
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100259 encoder_crtc = old_connector_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200260
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100261 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
262 encoder->base.id, encoder->name,
263 encoder_crtc->base.id, encoder_crtc->name);
264
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100265 set_best_encoder(state, new_connector_state, NULL);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100266
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100267 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
Maarten Lankhorstff19b782016-03-03 10:17:38 +0100268 crtc_state->connectors_changed = true;
269
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100270 return;
Daniel Vetter623369e2014-09-16 17:50:47 +0200271 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200272}
273
274static int
Maarten Lankhorst94595452016-02-24 09:37:29 +0100275update_connector_routing(struct drm_atomic_state *state,
276 struct drm_connector *connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100277 struct drm_connector_state *old_connector_state,
278 struct drm_connector_state *new_connector_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200279{
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200280 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200281 struct drm_encoder *new_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200282 struct drm_crtc_state *crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200283
Daniel Vetter17a38d92015-02-22 12:24:16 +0100284 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
285 connector->base.id,
286 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200287
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100288 if (old_connector_state->crtc != new_connector_state->crtc) {
289 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100290 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200291 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200292 }
293
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100294 if (new_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100295 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200296 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200297 }
298 }
299
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100300 if (!new_connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100301 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200302 connector->base.id,
303 connector->name);
304
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100305 set_best_encoder(state, new_connector_state, NULL);
Daniel Vetter623369e2014-09-16 17:50:47 +0200306
307 return 0;
308 }
309
Lyude Paule352d8e2018-10-08 19:24:30 -0400310 crtc_state = drm_atomic_get_new_crtc_state(state,
311 new_connector_state->crtc);
312 /*
313 * For compatibility with legacy users, we want to make sure that
314 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
315 * which would result in anything else must be considered invalid, to
316 * avoid turning on new displays on dead connectors.
317 *
318 * Since the connector can be unregistered at any point during an
319 * atomic check or commit, this is racy. But that's OK: all we care
320 * about is ensuring that userspace can't do anything but shut off the
321 * display on a connector that was destroyed after its been notified,
322 * not before.
323 */
324 if (!READ_ONCE(connector->registered) && crtc_state->active) {
325 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
326 connector->base.id, connector->name);
327 return -EINVAL;
328 }
329
Daniel Vetter623369e2014-09-16 17:50:47 +0200330 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200331
332 if (funcs->atomic_best_encoder)
333 new_encoder = funcs->atomic_best_encoder(connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100334 new_connector_state);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200335 else if (funcs->best_encoder)
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200336 new_encoder = funcs->best_encoder(connector);
Boris Brezillonc61b93f2016-06-07 13:47:56 +0200337 else
338 new_encoder = drm_atomic_helper_best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200339
340 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100341 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
342 connector->base.id,
343 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200344 return -EINVAL;
345 }
346
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100347 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
Russell King6ac7c542017-02-13 12:27:03 +0000348 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100349 new_encoder->base.id,
350 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100351 new_connector_state->crtc->base.id,
352 new_connector_state->crtc->name);
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100353 return -EINVAL;
354 }
355
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100356 if (new_encoder == new_connector_state->best_encoder) {
357 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100358
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200359 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100360 connector->base.id,
361 connector->name,
362 new_encoder->base.id,
363 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100364 new_connector_state->crtc->base.id,
365 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200366
367 return 0;
368 }
369
Maarten Lankhorstec5aaa52016-03-03 10:17:41 +0100370 steal_encoder(state, new_encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200371
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100372 set_best_encoder(state, new_connector_state, new_encoder);
Maarten Lankhorste87a52b2016-01-28 15:04:58 +0100373
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200374 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200375
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200376 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100377 connector->base.id,
378 connector->name,
379 new_encoder->base.id,
380 new_encoder->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100381 new_connector_state->crtc->base.id,
382 new_connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200383
384 return 0;
385}
386
387static int
388mode_fixup(struct drm_atomic_state *state)
389{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300390 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100391 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300392 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100393 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200394 int i;
Dan Carpenterf9ad86e2017-02-08 02:46:01 +0300395 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200396
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100397 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
398 if (!new_crtc_state->mode_changed &&
399 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200400 continue;
401
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100402 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200403 }
404
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100405 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200406 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200407 struct drm_encoder *encoder;
408
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100409 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200410
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100411 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200412 continue;
413
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100414 new_crtc_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100415 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200416
417 /*
418 * Each encoder has at most one connector (since we always steal
419 * it away), so we won't call ->mode_fixup twice.
420 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100421 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200422 funcs = encoder->helper_private;
423
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100424 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
425 &new_crtc_state->adjusted_mode);
Archit Taneja862e6862015-05-21 11:03:16 +0530426 if (!ret) {
427 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
428 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200429 }
430
Noralf Trønnes28276352016-05-11 18:09:20 +0200431 if (funcs && funcs->atomic_check) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100432 ret = funcs->atomic_check(encoder, new_crtc_state,
433 new_conn_state);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100434 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100435 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
436 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100437 return ret;
438 }
Noralf Trønnes28276352016-05-11 18:09:20 +0200439 } else if (funcs && funcs->mode_fixup) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100440 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
441 &new_crtc_state->adjusted_mode);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100442 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100443 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
444 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100445 return -EINVAL;
446 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200447 }
448 }
449
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100450 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200451 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200452
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100453 if (!new_crtc_state->enable)
Liu Yingf55f1702016-05-27 17:35:54 +0800454 continue;
455
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100456 if (!new_crtc_state->mode_changed &&
457 !new_crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200458 continue;
459
460 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300461 if (!funcs->mode_fixup)
462 continue;
463
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100464 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
465 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200466 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200467 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
468 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200469 return -EINVAL;
470 }
471 }
472
473 return 0;
474}
475
Jose Abreufaf94a02017-05-25 15:19:16 +0100476static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
477 struct drm_encoder *encoder,
478 struct drm_crtc *crtc,
479 struct drm_display_mode *mode)
480{
481 enum drm_mode_status ret;
482
483 ret = drm_encoder_mode_valid(encoder, mode);
484 if (ret != MODE_OK) {
485 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
486 encoder->base.id, encoder->name);
487 return ret;
488 }
489
490 ret = drm_bridge_mode_valid(encoder->bridge, mode);
491 if (ret != MODE_OK) {
492 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
493 return ret;
494 }
495
496 ret = drm_crtc_mode_valid(crtc, mode);
497 if (ret != MODE_OK) {
498 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
499 crtc->base.id, crtc->name);
500 return ret;
501 }
502
503 return ret;
504}
505
506static int
507mode_valid(struct drm_atomic_state *state)
508{
509 struct drm_connector_state *conn_state;
510 struct drm_connector *connector;
511 int i;
512
513 for_each_new_connector_in_state(state, connector, conn_state, i) {
514 struct drm_encoder *encoder = conn_state->best_encoder;
515 struct drm_crtc *crtc = conn_state->crtc;
516 struct drm_crtc_state *crtc_state;
517 enum drm_mode_status mode_status;
518 struct drm_display_mode *mode;
519
520 if (!crtc || !encoder)
521 continue;
522
523 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
524 if (!crtc_state)
525 continue;
526 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
527 continue;
528
529 mode = &crtc_state->mode;
530
531 mode_status = mode_valid_path(connector, encoder, crtc, mode);
532 if (mode_status != MODE_OK)
533 return -EINVAL;
534 }
535
536 return 0;
537}
538
Daniel Vetterd9b13622014-11-26 16:57:41 +0100539/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800540 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100541 * @dev: DRM device
542 * @state: the driver state object
543 *
544 * Check the state object to see if the requested state is physically possible.
545 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200546 * update and adds any additional connectors needed for full modesets. It calls
547 * the various per-object callbacks in the follow order:
548 *
549 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
550 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
551 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
552 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
Jose Abreufaf94a02017-05-25 15:19:16 +0100553 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
554 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
555 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
556 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200557 * This function is only called when the encoder will be part of a configured crtc,
558 * it must not be used for implementing connector property validation.
559 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
560 * instead.
Jose Abreufaf94a02017-05-25 15:19:16 +0100561 * 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 +0200562 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100563 * &drm_crtc_state.mode_changed is set when the input mode is changed.
564 * &drm_crtc_state.connectors_changed is set when a connector is added or
565 * removed from the crtc. &drm_crtc_state.active_changed is set when
566 * &drm_crtc_state.active changes, which is used for DPMS.
Brian Starkeyd807ed12016-10-13 10:47:08 +0100567 * See also: drm_atomic_crtc_needs_modeset()
Daniel Vetterd9b13622014-11-26 16:57:41 +0100568 *
569 * IMPORTANT:
570 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100571 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
572 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
573 * without a full modeset) _must_ call this function afterwards after that
574 * change. It is permitted to call this function multiple times for the same
575 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
576 * upon the adjusted dotclock for fifo space allocation and watermark
577 * computation.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100578 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200579 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100580 * Zero for success or -errno
581 */
582int
Rob Clark934ce1c2014-11-19 16:41:33 -0500583drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200584 struct drm_atomic_state *state)
585{
Daniel Vetter623369e2014-09-16 17:50:47 +0200586 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100587 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300588 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100589 struct drm_connector_state *old_connector_state, *new_connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200590 int i, ret;
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200591 unsigned connectors_mask = 0;
Daniel Vetter623369e2014-09-16 17:50:47 +0200592
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100593 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200594 bool has_connectors =
595 !!new_crtc_state->connector_mask;
596
Daniel Vetter869e1882017-05-31 10:38:13 +0200597 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
598
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100599 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200600 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
601 crtc->base.id, crtc->name);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100602 new_crtc_state->mode_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200603 }
604
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100605 if (old_crtc_state->enable != new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200606 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
607 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200608
609 /*
610 * For clarity this assignment is done here, but
611 * enable == 0 is only true when there are no
612 * connectors and a NULL mode.
613 *
614 * The other way around is true as well. enable != 0
615 * iff connectors are attached and a mode is set.
616 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100617 new_crtc_state->mode_changed = true;
618 new_crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200619 }
Maarten Lankhorst24d66522017-04-06 13:19:01 +0200620
621 if (old_crtc_state->active != new_crtc_state->active) {
622 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
623 crtc->base.id, crtc->name);
624 new_crtc_state->active_changed = true;
625 }
Maarten Lankhorst970ece82017-04-06 13:19:02 +0200626
627 if (new_crtc_state->enable != has_connectors) {
628 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
629 crtc->base.id, crtc->name);
630
631 return -EINVAL;
632 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200633 }
634
Maarten Lankhorst44596b82017-04-06 13:19:00 +0200635 ret = handle_conflicting_encoders(state, false);
Maarten Lankhorst8248b652016-03-03 10:17:40 +0100636 if (ret)
637 return ret;
Maarten Lankhorst40616a22016-03-03 10:17:39 +0100638
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100639 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200640 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
641
Daniel Vetter869e1882017-05-31 10:38:13 +0200642 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
643
Daniel Vetter623369e2014-09-16 17:50:47 +0200644 /*
Brian Starkeyd807ed12016-10-13 10:47:08 +0100645 * This only sets crtc->connectors_changed for routing changes,
646 * drivers must set crtc->connectors_changed themselves when
647 * connector properties need to be updated.
Daniel Vetter623369e2014-09-16 17:50:47 +0200648 */
Maarten Lankhorst94595452016-02-24 09:37:29 +0100649 ret = update_connector_routing(state, connector,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100650 old_connector_state,
651 new_connector_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200652 if (ret)
653 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100654 if (old_connector_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100655 new_crtc_state = drm_atomic_get_new_crtc_state(state,
656 old_connector_state->crtc);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100657 if (old_connector_state->link_status !=
658 new_connector_state->link_status)
659 new_crtc_state->connectors_changed = true;
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200660 }
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200661
662 if (funcs->atomic_check)
663 ret = funcs->atomic_check(connector, new_connector_state);
664 if (ret)
665 return ret;
666
Ville Syrjäläca52bea2018-06-15 20:07:34 +0300667 connectors_mask |= BIT(i);
Daniel Vetter623369e2014-09-16 17:50:47 +0200668 }
669
670 /*
671 * After all the routing has been prepared we need to add in any
672 * connector which is itself unchanged, but who's crtc changes it's
673 * configuration. This must be done before calling mode_fixup in case a
674 * crtc only changed its mode but has the same set of connectors.
675 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100676 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100677 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100678 continue;
679
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200680 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
681 crtc->base.id, crtc->name,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100682 new_crtc_state->enable ? 'y' : 'n',
683 new_crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200684
685 ret = drm_atomic_add_affected_connectors(state, crtc);
686 if (ret != 0)
687 return ret;
688
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200689 ret = drm_atomic_add_affected_planes(state, crtc);
690 if (ret != 0)
691 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200692 }
693
Maarten Lankhorstce09d762017-04-06 13:19:03 +0200694 /*
695 * Iterate over all connectors again, to make sure atomic_check()
696 * has been called on them when a modeset is forced.
697 */
698 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
699 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
700
701 if (connectors_mask & BIT(i))
702 continue;
703
704 if (funcs->atomic_check)
705 ret = funcs->atomic_check(connector, new_connector_state);
706 if (ret)
707 return ret;
708 }
709
Jose Abreufaf94a02017-05-25 15:19:16 +0100710 ret = mode_valid(state);
711 if (ret)
712 return ret;
713
Daniel Vetter623369e2014-09-16 17:50:47 +0200714 return mode_fixup(state);
715}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100716EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200717
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100718/**
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200719 * drm_atomic_helper_check_plane_state() - Check plane state for validity
720 * @plane_state: plane state to check
721 * @crtc_state: crtc state to check
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200722 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
723 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
724 * @can_position: is it legal to position the plane such that it
725 * doesn't cover the entire crtc? This will generally
726 * only be false for primary planes.
727 * @can_update_disabled: can the plane be updated while the crtc
728 * is disabled?
729 *
730 * Checks that a desired plane update is valid, and updates various
731 * bits of derived state (clipped coordinates etc.). Drivers that provide
732 * their own plane handling rather than helper-provided implementations may
733 * still wish to call this function to avoid duplication of error checking
734 * code.
735 *
736 * RETURNS:
737 * Zero if update appears valid, error code on failure
738 */
739int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
740 const struct drm_crtc_state *crtc_state,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200741 int min_scale,
742 int max_scale,
743 bool can_position,
744 bool can_update_disabled)
745{
746 struct drm_framebuffer *fb = plane_state->fb;
747 struct drm_rect *src = &plane_state->src;
748 struct drm_rect *dst = &plane_state->dst;
749 unsigned int rotation = plane_state->rotation;
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200750 struct drm_rect clip = {};
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200751 int hscale, vscale;
752
753 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
754
755 *src = drm_plane_state_src(plane_state);
756 *dst = drm_plane_state_dest(plane_state);
757
758 if (!fb) {
759 plane_state->visible = false;
760 return 0;
761 }
762
763 /* crtc should only be NULL when disabling (i.e., !fb) */
764 if (WARN_ON(!plane_state->crtc)) {
765 plane_state->visible = false;
766 return 0;
767 }
768
769 if (!crtc_state->enable && !can_update_disabled) {
770 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
771 return -EINVAL;
772 }
773
774 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
775
776 /* Check scaling */
777 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
778 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
779 if (hscale < 0 || vscale < 0) {
780 DRM_DEBUG_KMS("Invalid scaling of plane\n");
781 drm_rect_debug_print("src: ", &plane_state->src, true);
782 drm_rect_debug_print("dst: ", &plane_state->dst, false);
783 return -ERANGE;
784 }
785
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200786 if (crtc_state->enable)
787 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
788
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +0200789 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200790
791 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
792
793 if (!plane_state->visible)
794 /*
795 * Plane isn't visible; some drivers can handle this
796 * so we just return success here. Drivers that can't
797 * (including those that use the primary plane helper's
798 * update function) will return an error from their
799 * update_plane handler.
800 */
801 return 0;
802
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200803 if (!can_position && !drm_rect_equals(dst, &clip)) {
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200804 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
805 drm_rect_debug_print("dst: ", dst, false);
Ville Syrjälä81af63a2018-01-23 19:08:57 +0200806 drm_rect_debug_print("clip: ", &clip, false);
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200807 return -EINVAL;
808 }
809
810 return 0;
811}
812EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
813
814/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800815 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100816 * @dev: DRM device
817 * @state: the driver state object
818 *
819 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100820 * This does all the plane update related checks using by calling into the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100821 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
822 * hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100823 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100824 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200825 * updated planes.
826 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200827 * RETURNS:
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100828 * Zero for success or -errno
829 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100830int
831drm_atomic_helper_check_planes(struct drm_device *dev,
832 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100833{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300834 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100835 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300836 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100837 struct drm_plane_state *new_plane_state, *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100838 int i, ret = 0;
839
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100840 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200841 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100842
Daniel Vetter869e1882017-05-31 10:38:13 +0200843 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
844
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100845 funcs = plane->helper_private;
846
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100847 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100848
849 if (!funcs || !funcs->atomic_check)
850 continue;
851
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100852 ret = funcs->atomic_check(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100853 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200854 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
855 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100856 return ret;
857 }
858 }
859
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100860 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200861 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100862
863 funcs = crtc->helper_private;
864
865 if (!funcs || !funcs->atomic_check)
866 continue;
867
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100868 ret = funcs->atomic_check(crtc, new_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100869 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200870 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
871 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100872 return ret;
873 }
874 }
875
Daniel Vetterd9b13622014-11-26 16:57:41 +0100876 return ret;
877}
878EXPORT_SYMBOL(drm_atomic_helper_check_planes);
879
880/**
881 * drm_atomic_helper_check - validate state object
882 * @dev: DRM device
883 * @state: the driver state object
884 *
885 * Check the state object to see if the requested state is physically possible.
886 * Only crtcs and planes have check callbacks, so for any additional (global)
887 * checking that a driver needs it can simply wrap that around this function.
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100888 * Drivers without such needs can directly use this as their
889 * &drm_mode_config_funcs.atomic_check callback.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100890 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100891 * This just wraps the two parts of the state checking for planes and modeset
892 * state in the default order: First it calls drm_atomic_helper_check_modeset()
893 * and then drm_atomic_helper_check_planes(). The assumption is that the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100894 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
895 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
896 * watermarks.
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100897 *
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200898 * Note that zpos normalization will add all enable planes to the state which
899 * might not desired for some drivers.
900 * For example enable/disable of a cursor plane which have fixed zpos value
901 * would trigger all other enabled planes to be forced to the state change.
902 *
Daniel Vetterc39032a2016-06-08 14:19:19 +0200903 * RETURNS:
Daniel Vetterd9b13622014-11-26 16:57:41 +0100904 * Zero for success or -errno
905 */
906int drm_atomic_helper_check(struct drm_device *dev,
907 struct drm_atomic_state *state)
908{
909 int ret;
910
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100911 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100912 if (ret)
913 return ret;
914
Peter Ujfalusi49efffc2018-03-21 12:20:24 +0200915 if (dev->mode_config.normalize_zpos) {
916 ret = drm_atomic_normalize_zpos(dev, state);
917 if (ret)
918 return ret;
919 }
920
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100921 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500922 if (ret)
923 return ret;
924
Gustavo Padovanfef9df82017-06-30 15:03:17 -0300925 if (state->legacy_cursor_update)
926 state->async_update = !drm_atomic_helper_async_check(dev, state);
927
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100928 return ret;
929}
930EXPORT_SYMBOL(drm_atomic_helper_check);
931
Daniel Vetter623369e2014-09-16 17:50:47 +0200932static void
933disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
934{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300935 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100936 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300937 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100938 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200939 int i;
940
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100941 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200942 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200943 struct drm_encoder *encoder;
944
Daniel Vetter623369e2014-09-16 17:50:47 +0200945 /* Shut down everything that's in the changeset and currently
946 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300947 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200948 continue;
949
Maarten Lankhorstb4d93672017-03-01 10:22:10 +0100950 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100951
Daniel Vetter4218a322015-03-26 22:18:40 +0100952 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200953 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100954 continue;
955
Rob Clark46df9ad2014-11-20 15:40:36 -0500956 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200957
Rob Clark46df9ad2014-11-20 15:40:36 -0500958 /* We shouldn't get this far if we didn't previously have
959 * an encoder.. but WARN_ON() rather than explode.
960 */
961 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200962 continue;
963
964 funcs = encoder->helper_private;
965
Daniel Vetter17a38d92015-02-22 12:24:16 +0100966 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
967 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100968
Daniel Vetter623369e2014-09-16 17:50:47 +0200969 /*
970 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800971 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200972 */
Archit Taneja862e6862015-05-21 11:03:16 +0530973 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200974
975 /* Right function depends upon target state. */
Noralf Trønnes28276352016-05-11 18:09:20 +0200976 if (funcs) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100977 if (new_conn_state->crtc && funcs->prepare)
Noralf Trønnes28276352016-05-11 18:09:20 +0200978 funcs->prepare(encoder);
979 else if (funcs->disable)
980 funcs->disable(encoder);
981 else if (funcs->dpms)
982 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
983 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200984
Archit Taneja862e6862015-05-21 11:03:16 +0530985 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200986 }
987
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100988 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +0200989 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter84014b02017-10-17 17:27:14 +0200990 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200991
992 /* Shut down everything that needs a full modeset. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +0100993 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100994 continue;
995
996 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200997 continue;
998
999 funcs = crtc->helper_private;
1000
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001001 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1002 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001003
1004
Daniel Vetter623369e2014-09-16 17:50:47 +02001005 /* Right function depends upon target state. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001006 if (new_crtc_state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +02001007 funcs->prepare(crtc);
Liu Yingc9ac8b42016-08-26 15:30:38 +08001008 else if (funcs->atomic_disable)
1009 funcs->atomic_disable(crtc, old_crtc_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001010 else if (funcs->disable)
1011 funcs->disable(crtc);
1012 else
1013 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Daniel Vetter84014b02017-10-17 17:27:14 +02001014
1015 if (!(dev->irq_enabled && dev->num_crtcs))
1016 continue;
1017
1018 ret = drm_crtc_vblank_get(crtc);
1019 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1020 if (ret == 0)
1021 drm_crtc_vblank_put(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001022 }
1023}
1024
Daniel Vetter4c18d302015-05-12 15:27:37 +02001025/**
1026 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1027 * @dev: DRM device
1028 * @old_state: atomic state object with old state structures
1029 *
1030 * This function updates all the various legacy modeset state pointers in
1031 * connectors, encoders and crtcs. It also updates the timestamping constants
1032 * used for precise vblank timestamps by calling
1033 * drm_calc_timestamping_constants().
1034 *
1035 * Drivers can use this for building their own atomic commit if they don't have
1036 * a pure helper-based modeset implementation.
Daniel Vetter2e2b96e2017-11-08 21:30:07 +01001037 *
1038 * Since these updates are not synchronized with lockings, only code paths
1039 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1040 * legacy state filled out by this helper. Defacto this means this helper and
1041 * the legacy state pointers are only really useful for transitioning an
1042 * existing driver to the atomic world.
Daniel Vetter4c18d302015-05-12 15:27:37 +02001043 */
1044void
1045drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1046 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001047{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001048 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001049 struct drm_connector_state *old_conn_state, *new_conn_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001050 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001051 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001052 int i;
1053
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001054 /* clear out existing links and update dpms */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001055 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001056 if (connector->encoder) {
1057 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001058
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001059 connector->encoder->crtc = NULL;
1060 connector->encoder = NULL;
1061 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001062
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001063 crtc = new_conn_state->crtc;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001064 if ((!crtc && old_conn_state->crtc) ||
1065 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001066 int mode = DRM_MODE_DPMS_OFF;
1067
1068 if (crtc && crtc->state->active)
1069 mode = DRM_MODE_DPMS_ON;
1070
1071 connector->dpms = mode;
Maarten Lankhorst8c103422015-07-27 13:24:29 +02001072 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001073 }
1074
1075 /* set new links */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001076 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1077 if (!new_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +02001078 continue;
1079
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001080 if (WARN_ON(!new_conn_state->best_encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +02001081 continue;
1082
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001083 connector->encoder = new_conn_state->best_encoder;
1084 connector->encoder->crtc = new_conn_state->crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +02001085 }
1086
1087 /* set legacy state in the crtc structure */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001088 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +02001089 struct drm_plane *primary = crtc->primary;
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001090 struct drm_plane_state *new_plane_state;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001091
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001092 crtc->mode = new_crtc_state->mode;
1093 crtc->enabled = new_crtc_state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001094
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001095 new_plane_state =
1096 drm_atomic_get_new_plane_state(old_state, primary);
1097
1098 if (new_plane_state && new_plane_state->crtc == crtc) {
1099 crtc->x = new_plane_state->src_x >> 16;
1100 crtc->y = new_plane_state->src_y >> 16;
Maarten Lankhorst26608012015-07-16 15:51:01 +02001101 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +02001102
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001103 if (new_crtc_state->enable)
Daniel Vetter3d51d2d2015-05-12 15:21:06 +02001104 drm_calc_timestamping_constants(crtc,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001105 &new_crtc_state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001106 }
1107}
Daniel Vetter4c18d302015-05-12 15:27:37 +02001108EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001109
1110static void
1111crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1112{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001113 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001114 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001115 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001116 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001117 int i;
1118
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001119 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001120 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001121
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001122 if (!new_crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +02001123 continue;
1124
1125 funcs = crtc->helper_private;
1126
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001127 if (new_crtc_state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001128 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1129 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001130
Daniel Vetter623369e2014-09-16 17:50:47 +02001131 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001132 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001133 }
1134
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001135 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001136 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001137 struct drm_encoder *encoder;
1138 struct drm_display_mode *mode, *adjusted_mode;
1139
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001140 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001141 continue;
1142
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001143 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001144 funcs = encoder->helper_private;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001145 new_crtc_state = new_conn_state->crtc->state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001146 mode = &new_crtc_state->mode;
1147 adjusted_mode = &new_crtc_state->adjusted_mode;
1148
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001149 if (!new_crtc_state->mode_changed)
1150 continue;
1151
Daniel Vetter17a38d92015-02-22 12:24:16 +01001152 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1153 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001154
Daniel Vetter623369e2014-09-16 17:50:47 +02001155 /*
1156 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001157 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001158 */
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001159 if (funcs && funcs->atomic_mode_set) {
1160 funcs->atomic_mode_set(encoder, new_crtc_state,
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001161 new_conn_state);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001162 } else if (funcs && funcs->mode_set) {
Daniel Vetterc982bd92015-02-22 12:24:20 +01001163 funcs->mode_set(encoder, mode, adjusted_mode);
Philipp Zabelfe4a11c2016-07-22 12:20:47 +02001164 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001165
Archit Taneja862e6862015-05-21 11:03:16 +05301166 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +02001167 }
1168}
1169
1170/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001171 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001172 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +01001173 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001174 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001175 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +02001176 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +01001177 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +03001178 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +01001179 * drm_atomic_helper_commit_planes(), which is what the default commit function
1180 * does. But drivers with different needs can group the modeset commits together
1181 * and do the plane commits at the end. This is useful for drivers doing runtime
1182 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001183 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001184void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1185 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001186{
Laurent Pincharta072f802015-02-22 12:24:18 +01001187 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +02001188
1189 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1190
Laurent Pincharta072f802015-02-22 12:24:18 +01001191 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001192}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001193EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001194
Brian Starkey935774c2017-03-29 17:42:32 +01001195static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1196 struct drm_atomic_state *old_state)
1197{
1198 struct drm_connector *connector;
1199 struct drm_connector_state *new_conn_state;
1200 int i;
1201
1202 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1203 const struct drm_connector_helper_funcs *funcs;
1204
1205 funcs = connector->helper_private;
Boris Brezillon814bde92018-07-03 09:50:17 +02001206 if (!funcs->atomic_commit)
1207 continue;
Brian Starkey935774c2017-03-29 17:42:32 +01001208
1209 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1210 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
Boris Brezillon425132f2018-07-03 09:50:16 +02001211 funcs->atomic_commit(connector, new_conn_state);
Brian Starkey935774c2017-03-29 17:42:32 +01001212 }
1213 }
1214}
1215
Daniel Vetter623369e2014-09-16 17:50:47 +02001216/**
Daniel Vetter1af434a2015-02-22 12:24:19 +01001217 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +02001218 * @dev: DRM device
1219 * @old_state: atomic state object with old state structures
1220 *
Daniel Vetter1af434a2015-02-22 12:24:19 +01001221 * This function enables all the outputs with the new configuration which had to
1222 * be turned off for the update.
1223 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +03001224 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +01001225 * drm_atomic_helper_commit_planes(), which is what the default commit function
1226 * does. But drivers with different needs can group the modeset commits together
1227 * and do the plane commits at the end. This is useful for drivers doing runtime
1228 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +02001229 */
Daniel Vetter1af434a2015-02-22 12:24:19 +01001230void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1231 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001232{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001233 struct drm_crtc *crtc;
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001234 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001235 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001236 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001237 struct drm_connector_state *new_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001238 int i;
1239
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001240 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001241 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001242
1243 /* Need to filter out CRTCs where only planes change. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001244 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001245 continue;
1246
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001247 if (!new_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +02001248 continue;
1249
1250 funcs = crtc->helper_private;
1251
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001252 if (new_crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001253 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1254 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001255
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001256 if (funcs->atomic_enable)
1257 funcs->atomic_enable(crtc, old_crtc_state);
Daniel Vetteree0a89c2015-01-22 16:36:24 +01001258 else
1259 funcs->commit(crtc);
1260 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001261 }
1262
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001263 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02001264 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +02001265 struct drm_encoder *encoder;
1266
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001267 if (!new_conn_state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +02001268 continue;
1269
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001270 if (!new_conn_state->crtc->state->active ||
1271 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001272 continue;
1273
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001274 encoder = new_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +02001275 funcs = encoder->helper_private;
1276
Daniel Vetter17a38d92015-02-22 12:24:16 +01001277 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1278 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +01001279
Daniel Vetter623369e2014-09-16 17:50:47 +02001280 /*
1281 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +08001282 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +02001283 */
Archit Taneja862e6862015-05-21 11:03:16 +05301284 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001285
Noralf Trønnes28276352016-05-11 18:09:20 +02001286 if (funcs) {
1287 if (funcs->enable)
1288 funcs->enable(encoder);
1289 else if (funcs->commit)
1290 funcs->commit(encoder);
1291 }
Daniel Vetter623369e2014-09-16 17:50:47 +02001292
Archit Taneja862e6862015-05-21 11:03:16 +05301293 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +02001294 }
Brian Starkey935774c2017-03-29 17:42:32 +01001295
1296 drm_atomic_helper_commit_writebacks(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001297}
Daniel Vetter1af434a2015-02-22 12:24:19 +01001298EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +02001299
Rob Clark4c5b7f32016-03-18 19:14:55 -04001300/**
1301 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1302 * @dev: DRM device
1303 * @state: atomic state object with old state structures
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001304 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1305 * Otherwise @state is the old state.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001306 *
1307 * For implicit sync, driver should fish the exclusive fence out from the
1308 * incoming fb's and stash it in the drm_plane_state. This is called after
1309 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1310 * just uses the atomic state to find the changed planes)
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001311 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001312 * Note that @pre_swap is needed since the point where we block for fences moves
1313 * around depending upon whether an atomic commit is blocking or
Gustavo Padovan42590372017-04-27 11:35:06 -03001314 * non-blocking. For non-blocking commit all waiting needs to happen after
1315 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001316 * to wait **before** we do anything that can't be easily rolled back. That is
1317 * before we call drm_atomic_helper_swap_state().
1318 *
Chris Wilsonf54d1862016-10-25 13:00:45 +01001319 * Returns zero if success or < 0 if dma_fence_wait() fails.
Rob Clark4c5b7f32016-03-18 19:14:55 -04001320 */
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001321int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1322 struct drm_atomic_state *state,
1323 bool pre_swap)
Daniel Vettere2330f02014-10-29 11:34:56 +01001324{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001325 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001326 struct drm_plane_state *new_plane_state;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001327 int i, ret;
Daniel Vettere2330f02014-10-29 11:34:56 +01001328
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001329 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1330 if (!new_plane_state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +01001331 continue;
1332
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001333 WARN_ON(!new_plane_state->fb);
Daniel Vettere2330f02014-10-29 11:34:56 +01001334
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001335 /*
1336 * If waiting for fences pre-swap (ie: nonblock), userspace can
1337 * still interrupt the operation. Instead of blocking until the
1338 * timer expires, make the wait interruptible.
1339 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001340 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001341 if (ret)
1342 return ret;
1343
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001344 dma_fence_put(new_plane_state->fence);
1345 new_plane_state->fence = NULL;
Daniel Vettere2330f02014-10-29 11:34:56 +01001346 }
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001347
1348 return 0;
Daniel Vettere2330f02014-10-29 11:34:56 +01001349}
Rob Clark4c5b7f32016-03-18 19:14:55 -04001350EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
Daniel Vettere2330f02014-10-29 11:34:56 +01001351
John Keepingc2409062016-01-19 10:46:58 +00001352/**
Rob Clark5ee32292014-11-11 19:38:59 -05001353 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1354 * @dev: DRM device
1355 * @old_state: atomic state object with old state structures
1356 *
1357 * Helper to, after atomic commit, wait for vblanks on all effected
1358 * crtcs (ie. before cleaning up old framebuffers using
Boris Brezillon01086482017-06-02 10:32:06 +02001359 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
Daniel Vetterab58e332014-11-24 20:42:42 +01001360 * framebuffers have actually changed to optimize for the legacy cursor and
1361 * plane update use-case.
Boris Brezillon01086482017-06-02 10:32:06 +02001362 *
1363 * Drivers using the nonblocking commit tracking support initialized by calling
1364 * drm_atomic_helper_setup_commit() should look at
1365 * drm_atomic_helper_wait_for_flip_done() as an alternative.
Rob Clark5ee32292014-11-11 19:38:59 -05001366 */
1367void
1368drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1369 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +02001370{
1371 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001372 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +02001373 int i, ret;
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001374 unsigned crtc_mask = 0;
1375
1376 /*
1377 * Legacy cursor ioctls are completely unsynced, and userspace
1378 * relies on that (by doing tons of cursor updates).
1379 */
1380 if (old_state->legacy_cursor_update)
1381 return;
Daniel Vetter623369e2014-09-16 17:50:47 +02001382
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001383 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Lucas Stacha76dfe32017-11-29 12:04:31 +01001384 if (!new_crtc_state->active)
Daniel Vetterab58e332014-11-24 20:42:42 +01001385 continue;
1386
Daniel Vetter623369e2014-09-16 17:50:47 +02001387 ret = drm_crtc_vblank_get(crtc);
1388 if (ret != 0)
1389 continue;
1390
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001391 crtc_mask |= drm_crtc_mask(crtc);
1392 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001393 }
1394
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001395 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001396 if (!(crtc_mask & drm_crtc_mask(crtc)))
Daniel Vetter623369e2014-09-16 17:50:47 +02001397 continue;
1398
1399 ret = wait_event_timeout(dev->vblank[i].queue,
Maarten Lankhorstbdc57142016-12-15 12:51:42 +01001400 old_state->crtcs[i].last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001401 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001402 msecs_to_jiffies(50));
1403
Russell King6ac7c542017-02-13 12:27:03 +00001404 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1405 crtc->base.id, crtc->name);
Ville Syrjälä8d4d0d72016-04-18 14:29:33 +03001406
Daniel Vetter623369e2014-09-16 17:50:47 +02001407 drm_crtc_vblank_put(crtc);
1408 }
1409}
Rob Clark5ee32292014-11-11 19:38:59 -05001410EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001411
1412/**
Boris Brezillon01086482017-06-02 10:32:06 +02001413 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1414 * @dev: DRM device
1415 * @old_state: atomic state object with old state structures
1416 *
1417 * Helper to, after atomic commit, wait for page flips on all effected
1418 * crtcs (ie. before cleaning up old framebuffers using
1419 * drm_atomic_helper_cleanup_planes()). Compared to
1420 * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1421 * CRTCs, assuming that cursors-only updates are signalling their completion
1422 * immediately (or using a different path).
1423 *
1424 * This requires that drivers use the nonblocking commit tracking support
1425 * initialized using drm_atomic_helper_setup_commit().
1426 */
1427void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1428 struct drm_atomic_state *old_state)
1429{
Boris Brezillon01086482017-06-02 10:32:06 +02001430 struct drm_crtc *crtc;
1431 int i;
1432
Leo Li4364bcb2018-10-15 09:46:40 -04001433 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1434 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
Boris Brezillon01086482017-06-02 10:32:06 +02001435 int ret;
1436
Leo Li4364bcb2018-10-15 09:46:40 -04001437 crtc = old_state->crtcs[i].ptr;
1438
1439 if (!crtc || !commit)
Boris Brezillon01086482017-06-02 10:32:06 +02001440 continue;
1441
1442 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1443 if (ret == 0)
1444 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1445 crtc->base.id, crtc->name);
1446 }
Ville Syrjälä6ebffc52018-11-22 16:34:11 +02001447
1448 if (old_state->fake_commit)
1449 complete_all(&old_state->fake_commit->flip_done);
Boris Brezillon01086482017-06-02 10:32:06 +02001450}
1451EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1452
1453/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001454 * drm_atomic_helper_commit_tail - commit atomic update to hardware
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001455 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +02001456 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001457 * This is the default implementation for the
Maxime Ripard81a099a2017-07-20 15:01:16 +02001458 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1459 * that do not support runtime_pm or do not need the CRTC to be
1460 * enabled to perform a commit. Otherwise, see
1461 * drm_atomic_helper_commit_tail_rpm().
Daniel Vetter623369e2014-09-16 17:50:47 +02001462 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001463 * Note that the default ordering of how the various stages are called is to
Maxime Ripard81a099a2017-07-20 15:01:16 +02001464 * match the legacy modeset helper library closest.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001465 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001466void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001467{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001468 struct drm_device *dev = old_state->dev;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001469
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001470 drm_atomic_helper_commit_modeset_disables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001471
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001472 drm_atomic_helper_commit_planes(dev, old_state, 0);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001473
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001474 drm_atomic_helper_commit_modeset_enables(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001475
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001476 drm_atomic_helper_fake_vblank(old_state);
1477
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001478 drm_atomic_helper_commit_hw_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001479
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001480 drm_atomic_helper_wait_for_vblanks(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001481
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001482 drm_atomic_helper_cleanup_planes(dev, old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001483}
1484EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1485
Maxime Ripard81a099a2017-07-20 15:01:16 +02001486/**
1487 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1488 * @old_state: new modeset state to be committed
1489 *
1490 * This is an alternative implementation for the
1491 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1492 * that support runtime_pm or need the CRTC to be enabled to perform a
1493 * commit. Otherwise, one should use the default implementation
1494 * drm_atomic_helper_commit_tail().
1495 */
1496void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1497{
1498 struct drm_device *dev = old_state->dev;
1499
1500 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1501
1502 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1503
1504 drm_atomic_helper_commit_planes(dev, old_state,
1505 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1506
Boris Brezillon6fb42b62018-07-03 09:50:20 +02001507 drm_atomic_helper_fake_vblank(old_state);
1508
Maxime Ripard81a099a2017-07-20 15:01:16 +02001509 drm_atomic_helper_commit_hw_done(old_state);
1510
1511 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1512
1513 drm_atomic_helper_cleanup_planes(dev, old_state);
1514}
1515EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1516
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001517static void commit_tail(struct drm_atomic_state *old_state)
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001518{
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001519 struct drm_device *dev = old_state->dev;
Laurent Pincharta4b10cc2017-01-02 11:16:13 +02001520 const struct drm_mode_config_helper_funcs *funcs;
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001521
1522 funcs = dev->mode_config.helper_private;
1523
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001524 drm_atomic_helper_wait_for_fences(dev, old_state, false);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001525
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001526 drm_atomic_helper_wait_for_dependencies(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001527
1528 if (funcs && funcs->atomic_commit_tail)
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001529 funcs->atomic_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001530 else
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001531 drm_atomic_helper_commit_tail(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001532
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001533 drm_atomic_helper_commit_cleanup_done(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001534
Daniel Vetter1ea0c022016-11-21 18:18:02 +01001535 drm_atomic_state_put(old_state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001536}
1537
1538static void commit_work(struct work_struct *work)
1539{
1540 struct drm_atomic_state *state = container_of(work,
1541 struct drm_atomic_state,
1542 commit_work);
1543 commit_tail(state);
1544}
1545
1546/**
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001547 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1548 * @dev: DRM device
1549 * @state: the driver state object
1550 *
1551 * This helper will check if it is possible to commit the state asynchronously.
1552 * Async commits are not supposed to swap the states like normal sync commits
1553 * but just do in-place changes on the current state.
1554 *
1555 * It will return 0 if the commit can happen in an asynchronous fashion or error
1556 * if not. Note that error just mean it can't be commited asynchronously, if it
1557 * fails the commit should be treated like a normal synchronous commit.
1558 */
1559int drm_atomic_helper_async_check(struct drm_device *dev,
1560 struct drm_atomic_state *state)
1561{
1562 struct drm_crtc *crtc;
1563 struct drm_crtc_state *crtc_state;
Boris Brezillonde2d8db2018-07-24 15:33:00 +02001564 struct drm_plane *plane = NULL;
1565 struct drm_plane_state *old_plane_state = NULL;
1566 struct drm_plane_state *new_plane_state = NULL;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001567 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001568 int i, n_planes = 0;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001569
1570 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1571 if (drm_atomic_crtc_needs_modeset(crtc_state))
1572 return -EINVAL;
1573 }
1574
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001575 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001576 n_planes++;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001577
1578 /* FIXME: we support only single plane updates for now */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001579 if (n_planes != 1)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001580 return -EINVAL;
1581
Boris Brezillon603ba2d2018-07-24 15:32:15 +02001582 if (!new_plane_state->crtc ||
1583 old_plane_state->crtc != new_plane_state->crtc)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001584 return -EINVAL;
1585
Nicholas Kazlauskasf0233ca2019-01-07 12:41:46 -05001586 /*
1587 * FIXME: Since prepare_fb and cleanup_fb are always called on
1588 * the new_plane_state for async updates we need to block framebuffer
1589 * changes. This prevents use of a fb that's been cleaned up and
1590 * double cleanups from occuring.
1591 */
1592 if (old_plane_state->fb != new_plane_state->fb)
1593 return -EINVAL;
1594
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001595 funcs = plane->helper_private;
1596 if (!funcs->atomic_async_update)
1597 return -EINVAL;
1598
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001599 if (new_plane_state->fence)
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001600 return -EINVAL;
1601
1602 /*
1603 * Don't do an async update if there is an outstanding commit modifying
1604 * the plane. This prevents our async update's changes from getting
1605 * overridden by a previous synchronous update's state.
1606 */
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001607 if (old_plane_state->commit &&
1608 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1609 return -EBUSY;
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001610
Maarten Lankhorst669c9212017-09-04 12:48:38 +02001611 return funcs->atomic_async_check(plane, new_plane_state);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001612}
1613EXPORT_SYMBOL(drm_atomic_helper_async_check);
1614
1615/**
1616 * drm_atomic_helper_async_commit - commit state asynchronously
1617 * @dev: DRM device
1618 * @state: the driver state object
1619 *
1620 * This function commits a state asynchronously, i.e., not vblank
1621 * synchronized. It should be used on a state only when
1622 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1623 * the states like normal sync commits, but just do in-place changes on the
1624 * current state.
Helen Koikefbb7e112019-06-03 13:56:10 -03001625 *
1626 * TODO: Implement full swap instead of doing in-place changes.
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001627 */
1628void drm_atomic_helper_async_commit(struct drm_device *dev,
1629 struct drm_atomic_state *state)
1630{
1631 struct drm_plane *plane;
1632 struct drm_plane_state *plane_state;
1633 const struct drm_plane_helper_funcs *funcs;
1634 int i;
1635
1636 for_each_new_plane_in_state(state, plane, plane_state, i) {
Helen Koikefbb7e112019-06-03 13:56:10 -03001637 struct drm_framebuffer *new_fb = plane_state->fb;
1638 struct drm_framebuffer *old_fb = plane->state->fb;
1639
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001640 funcs = plane->helper_private;
1641 funcs->atomic_async_update(plane, plane_state);
Boris Brezillon02edfd92018-03-30 16:55:18 +02001642
1643 /*
1644 * ->atomic_async_update() is supposed to update the
1645 * plane->state in-place, make sure at least common
1646 * properties have been properly updated.
1647 */
Helen Koikefbb7e112019-06-03 13:56:10 -03001648 WARN_ON_ONCE(plane->state->fb != new_fb);
Boris Brezillon02edfd92018-03-30 16:55:18 +02001649 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1650 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1651 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1652 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
Helen Koikefbb7e112019-06-03 13:56:10 -03001653
1654 /*
1655 * Make sure the FBs have been swapped so that cleanups in the
1656 * new_state performs a cleanup in the old FB.
1657 */
1658 WARN_ON_ONCE(plane_state->fb != old_fb);
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001659 }
1660}
1661EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1662
1663/**
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001664 * drm_atomic_helper_commit - commit validated state object
1665 * @dev: DRM device
1666 * @state: the driver state object
1667 * @nonblock: whether nonblocking behavior is requested.
1668 *
1669 * This function commits a with drm_atomic_helper_check() pre-validated state
1670 * object. This can still fail when e.g. the framebuffer reservation fails. This
1671 * function implements nonblocking commits, using
1672 * drm_atomic_helper_setup_commit() and related functions.
1673 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001674 * Committing the actual hardware state is done through the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001675 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1676 * implementation drm_atomic_helper_commit_tail().
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001677 *
Daniel Vetterc39032a2016-06-08 14:19:19 +02001678 * RETURNS:
Daniel Vetter623369e2014-09-16 17:50:47 +02001679 * Zero for success or -errno.
1680 */
1681int drm_atomic_helper_commit(struct drm_device *dev,
1682 struct drm_atomic_state *state,
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001683 bool nonblock)
Daniel Vetter623369e2014-09-16 17:50:47 +02001684{
1685 int ret;
1686
Gustavo Padovanfef9df82017-06-30 15:03:17 -03001687 if (state->async_update) {
1688 ret = drm_atomic_helper_prepare_planes(dev, state);
1689 if (ret)
1690 return ret;
1691
1692 drm_atomic_helper_async_commit(dev, state);
1693 drm_atomic_helper_cleanup_planes(dev, state);
1694
1695 return 0;
1696 }
1697
Daniel Vettera095caa2016-06-08 17:15:36 +02001698 ret = drm_atomic_helper_setup_commit(state, nonblock);
1699 if (ret)
1700 return ret;
1701
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001702 INIT_WORK(&state->commit_work, commit_work);
1703
Daniel Vetter623369e2014-09-16 17:50:47 +02001704 ret = drm_atomic_helper_prepare_planes(dev, state);
1705 if (ret)
1706 return ret;
1707
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001708 if (!nonblock) {
1709 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001710 if (ret)
1711 goto err;
Gustavo Padovanf6ce4102016-09-12 16:08:11 -03001712 }
1713
Daniel Vetter623369e2014-09-16 17:50:47 +02001714 /*
1715 * This is the point of no return - everything below never fails except
1716 * when the hw goes bonghits. Which means we can commit the new state on
1717 * the software side now.
1718 */
1719
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001720 ret = drm_atomic_helper_swap_state(state, true);
1721 if (ret)
1722 goto err;
Daniel Vetter623369e2014-09-16 17:50:47 +02001723
1724 /*
1725 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001726 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001727 * that the asynchronous work has either been cancelled (if the driver
1728 * supports it, which at least requires that the framebuffers get
1729 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1730 * before the new state gets committed on the software side with
1731 * drm_atomic_helper_swap_state().
1732 *
1733 * This scheme allows new atomic state updates to be prepared and
1734 * checked in parallel to the asynchronous completion of the previous
1735 * update. Which is important since compositors need to figure out the
1736 * composition of the next frame right after having submitted the
1737 * current layout.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001738 *
1739 * NOTE: Commit work has multiple phases, first hardware commit, then
1740 * cleanup. We want them to overlap, hence need system_unbound_wq to
1741 * make sure work items don't artifically stall on each another.
Daniel Vetter623369e2014-09-16 17:50:47 +02001742 */
1743
Chris Wilson08536952016-10-14 13:18:18 +01001744 drm_atomic_state_get(state);
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001745 if (nonblock)
1746 queue_work(system_unbound_wq, &state->commit_work);
1747 else
1748 commit_tail(state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001749
1750 return 0;
Maarten Lankhorstc066d232017-07-11 16:33:04 +02001751
1752err:
1753 drm_atomic_helper_cleanup_planes(dev, state);
1754 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +02001755}
1756EXPORT_SYMBOL(drm_atomic_helper_commit);
1757
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001758/**
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001759 * DOC: implementing nonblocking commit
Daniel Vettere8c833a2014-07-27 18:30:19 +02001760 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001761 * Nonblocking atomic commits have to be implemented in the following sequence:
Daniel Vettere8c833a2014-07-27 18:30:19 +02001762 *
1763 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1764 * which commit needs to call which can fail, so we want to run it first and
1765 * synchronously.
1766 *
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001767 * 2. Synchronize with any outstanding nonblocking commit worker threads which
Daniel Vettere8c833a2014-07-27 18:30:19 +02001768 * might be affected the new state update. This can be done by either cancelling
1769 * or flushing the work items, depending upon whether the driver can deal with
1770 * cancelled updates. Note that it is important to ensure that the framebuffer
1771 * cleanup is still done when cancelling.
1772 *
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001773 * Asynchronous workers need to have sufficient parallelism to be able to run
1774 * different atomic commits on different CRTCs in parallel. The simplest way to
1775 * achive this is by running them on the &system_unbound_wq work queue. Note
1776 * that drivers are not required to split up atomic commits and run an
1777 * individual commit in parallel - userspace is supposed to do that if it cares.
1778 * But it might be beneficial to do that for modesets, since those necessarily
1779 * must be done as one global operation, and enabling or disabling a CRTC can
1780 * take a long time. But even that is not required.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001781 *
1782 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001783 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001784 * locks means concurrent callers never see inconsistent state. And doing this
Maarten Lankhorst286dbb82016-04-26 16:11:34 +02001785 * while it's guaranteed that no relevant nonblocking worker runs means that
1786 * nonblocking workers do not need grab any locks. Actually they must not grab
1787 * locks, for otherwise the work flushing will deadlock.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001788 *
1789 * 4. Schedule a work item to do all subsequent steps, using the split-out
1790 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1791 * then cleaning up the framebuffers after the old framebuffer is no longer
1792 * being displayed.
Daniel Vetter9f2a7952016-06-08 14:19:02 +02001793 *
1794 * The above scheme is implemented in the atomic helper libraries in
1795 * drm_atomic_helper_commit() using a bunch of helper functions. See
1796 * drm_atomic_helper_setup_commit() for a starting point.
Daniel Vettere8c833a2014-07-27 18:30:19 +02001797 */
1798
Daniel Vettera095caa2016-06-08 17:15:36 +02001799static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1800{
1801 struct drm_crtc_commit *commit, *stall_commit = NULL;
1802 bool completed = true;
1803 int i;
1804 long ret = 0;
1805
1806 spin_lock(&crtc->commit_lock);
1807 i = 0;
1808 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1809 if (i == 0) {
1810 completed = try_wait_for_completion(&commit->flip_done);
1811 /* Userspace is not allowed to get ahead of the previous
1812 * commit with nonblocking ones. */
1813 if (!completed && nonblock) {
1814 spin_unlock(&crtc->commit_lock);
1815 return -EBUSY;
1816 }
1817 } else if (i == 1) {
Maarten Lankhorstf46640b2017-09-04 12:48:36 +02001818 stall_commit = drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001819 break;
Daniel Vetter723c3e52016-06-14 19:50:58 +02001820 }
Daniel Vettera095caa2016-06-08 17:15:36 +02001821
1822 i++;
1823 }
1824 spin_unlock(&crtc->commit_lock);
1825
1826 if (!stall_commit)
1827 return 0;
1828
1829 /* We don't want to let commits get ahead of cleanup work too much,
1830 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1831 */
Daniel Vetter723c3e52016-06-14 19:50:58 +02001832 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
Daniel Vettera095caa2016-06-08 17:15:36 +02001833 10*HZ);
1834 if (ret == 0)
1835 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1836 crtc->base.id, crtc->name);
1837
1838 drm_crtc_commit_put(stall_commit);
1839
1840 return ret < 0 ? ret : 0;
1841}
1842
Wei Yongjun899cc5f2017-01-12 14:21:57 +00001843static void release_crtc_commit(struct completion *completion)
Daniel Vetter24835e42016-12-21 11:23:30 +01001844{
1845 struct drm_crtc_commit *commit = container_of(completion,
1846 typeof(*commit),
1847 flip_done);
1848
1849 drm_crtc_commit_put(commit);
1850}
1851
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001852static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1853{
1854 init_completion(&commit->flip_done);
1855 init_completion(&commit->hw_done);
1856 init_completion(&commit->cleanup_done);
1857 INIT_LIST_HEAD(&commit->commit_entry);
1858 kref_init(&commit->ref);
1859 commit->crtc = crtc;
1860}
1861
1862static struct drm_crtc_commit *
1863crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1864{
1865 if (crtc) {
1866 struct drm_crtc_state *new_crtc_state;
1867
1868 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1869
1870 return new_crtc_state->commit;
1871 }
1872
1873 if (!state->fake_commit) {
1874 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1875 if (!state->fake_commit)
1876 return NULL;
1877
1878 init_commit(state->fake_commit, NULL);
1879 }
1880
1881 return state->fake_commit;
1882}
1883
Daniel Vettera095caa2016-06-08 17:15:36 +02001884/**
1885 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1886 * @state: new modeset state to be committed
1887 * @nonblock: whether nonblocking behavior is requested.
1888 *
1889 * This function prepares @state to be used by the atomic helper's support for
1890 * nonblocking commits. Drivers using the nonblocking commit infrastructure
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001891 * should always call this function from their
1892 * &drm_mode_config_funcs.atomic_commit hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001893 *
1894 * To be able to use this support drivers need to use a few more helper
1895 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1896 * actually committing the hardware state, and for nonblocking commits this call
1897 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1898 * and it's stall parameter, for when a driver's commit hooks look at the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001899 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
Daniel Vettera095caa2016-06-08 17:15:36 +02001900 *
1901 * Completion of the hardware commit step must be signalled using
1902 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1903 * to read or change any permanent software or hardware modeset state. The only
1904 * exception is state protected by other means than &drm_modeset_lock locks.
1905 * Only the free standing @state with pointers to the old state structures can
1906 * be inspected, e.g. to clean up old buffers using
1907 * drm_atomic_helper_cleanup_planes().
1908 *
1909 * At the very end, before cleaning up @state drivers must call
1910 * drm_atomic_helper_commit_cleanup_done().
1911 *
1912 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
Thierry Reding9ac07812017-10-12 16:06:16 +02001913 * complete and easy-to-use default implementation of the atomic_commit() hook.
Daniel Vettera095caa2016-06-08 17:15:36 +02001914 *
1915 * The tracking of asynchronously executed and still pending commits is done
1916 * using the core structure &drm_crtc_commit.
1917 *
1918 * By default there's no need to clean up resources allocated by this function
1919 * explicitly: drm_atomic_state_default_clear() will take care of that
1920 * automatically.
1921 *
1922 * Returns:
1923 *
1924 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1925 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1926 */
1927int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1928 bool nonblock)
1929{
1930 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001931 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001932 struct drm_connector *conn;
1933 struct drm_connector_state *old_conn_state, *new_conn_state;
1934 struct drm_plane *plane;
1935 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02001936 struct drm_crtc_commit *commit;
1937 int i, ret;
1938
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001939 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001940 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1941 if (!commit)
1942 return -ENOMEM;
1943
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001944 init_commit(commit, crtc);
Daniel Vettera095caa2016-06-08 17:15:36 +02001945
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02001946 new_crtc_state->commit = commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02001947
1948 ret = stall_checks(crtc, nonblock);
1949 if (ret)
1950 return ret;
1951
1952 /* Drivers only send out events when at least either current or
1953 * new CRTC state is active. Complete right away if everything
1954 * stays off. */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001955 if (!old_crtc_state->active && !new_crtc_state->active) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001956 complete_all(&commit->flip_done);
1957 continue;
1958 }
1959
1960 /* Legacy cursor updates are fully unsynced. */
1961 if (state->legacy_cursor_update) {
1962 complete_all(&commit->flip_done);
1963 continue;
1964 }
1965
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001966 if (!new_crtc_state->event) {
Daniel Vettera095caa2016-06-08 17:15:36 +02001967 commit->event = kzalloc(sizeof(*commit->event),
1968 GFP_KERNEL);
1969 if (!commit->event)
1970 return -ENOMEM;
1971
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001972 new_crtc_state->event = commit->event;
Daniel Vettera095caa2016-06-08 17:15:36 +02001973 }
1974
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01001975 new_crtc_state->event->base.completion = &commit->flip_done;
1976 new_crtc_state->event->base.completion_release = release_crtc_commit;
Daniel Vetter24835e42016-12-21 11:23:30 +01001977 drm_crtc_commit_get(commit);
Leo (Sunpeng) Li1c6ceee2018-01-17 12:51:08 +01001978
1979 commit->abort_completion = true;
Leo Li4364bcb2018-10-15 09:46:40 -04001980
1981 state->crtcs[i].commit = commit;
1982 drm_crtc_commit_get(commit);
Daniel Vettera095caa2016-06-08 17:15:36 +02001983 }
1984
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001985 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001986 /* Userspace is not allowed to get ahead of the previous
1987 * commit with nonblocking ones. */
1988 if (nonblock && old_conn_state->commit &&
1989 !try_wait_for_completion(&old_conn_state->commit->flip_done))
1990 return -EBUSY;
1991
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01001992 /* Always track connectors explicitly for e.g. link retraining. */
1993 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02001994 if (!commit)
1995 return -ENOMEM;
1996
1997 new_conn_state->commit = drm_crtc_commit_get(commit);
1998 }
1999
2000 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002001 /* Userspace is not allowed to get ahead of the previous
2002 * commit with nonblocking ones. */
2003 if (nonblock && old_plane_state->commit &&
2004 !try_wait_for_completion(&old_plane_state->commit->flip_done))
2005 return -EBUSY;
2006
Daniel Vetter1f2d9bd2017-11-10 11:53:12 +01002007 /* Always track planes explicitly for async pageflip support. */
Maarten Lankhorst4edd6082017-10-17 07:20:47 +02002008 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002009 if (!commit)
2010 return -ENOMEM;
2011
2012 new_plane_state->commit = drm_crtc_commit_get(commit);
2013 }
2014
Daniel Vettera095caa2016-06-08 17:15:36 +02002015 return 0;
2016}
2017EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2018
Daniel Vettera095caa2016-06-08 17:15:36 +02002019/**
2020 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002021 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002022 *
2023 * This function waits for all preceeding commits that touch the same CRTC as
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002024 * @old_state to both be committed to the hardware (as signalled by
Daniel Vettera095caa2016-06-08 17:15:36 +02002025 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
Thierry Reding277b09c2017-10-12 16:08:57 +02002026 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
Daniel Vettera095caa2016-06-08 17:15:36 +02002027 *
2028 * This is part of the atomic helper support for nonblocking commits, see
2029 * drm_atomic_helper_setup_commit() for an overview.
2030 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002031void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002032{
2033 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002034 struct drm_crtc_state *old_crtc_state;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002035 struct drm_plane *plane;
2036 struct drm_plane_state *old_plane_state;
2037 struct drm_connector *conn;
2038 struct drm_connector_state *old_conn_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002039 struct drm_crtc_commit *commit;
2040 int i;
2041 long ret;
2042
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002043 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2044 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002045
2046 if (!commit)
2047 continue;
2048
2049 ret = wait_for_completion_timeout(&commit->hw_done,
2050 10*HZ);
2051 if (ret == 0)
2052 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2053 crtc->base.id, crtc->name);
2054
2055 /* Currently no support for overwriting flips, hence
2056 * stall for previous one to execute completely. */
2057 ret = wait_for_completion_timeout(&commit->flip_done,
2058 10*HZ);
2059 if (ret == 0)
2060 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2061 crtc->base.id, crtc->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002062 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002063
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002064 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2065 commit = old_conn_state->commit;
2066
2067 if (!commit)
2068 continue;
2069
2070 ret = wait_for_completion_timeout(&commit->hw_done,
2071 10*HZ);
2072 if (ret == 0)
2073 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2074 conn->base.id, conn->name);
2075
2076 /* Currently no support for overwriting flips, hence
2077 * stall for previous one to execute completely. */
2078 ret = wait_for_completion_timeout(&commit->flip_done,
2079 10*HZ);
2080 if (ret == 0)
2081 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2082 conn->base.id, conn->name);
2083 }
2084
2085 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2086 commit = old_plane_state->commit;
2087
2088 if (!commit)
2089 continue;
2090
2091 ret = wait_for_completion_timeout(&commit->hw_done,
2092 10*HZ);
2093 if (ret == 0)
2094 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2095 plane->base.id, plane->name);
2096
2097 /* Currently no support for overwriting flips, hence
2098 * stall for previous one to execute completely. */
2099 ret = wait_for_completion_timeout(&commit->flip_done,
2100 10*HZ);
2101 if (ret == 0)
2102 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2103 plane->base.id, plane->name);
Daniel Vettera095caa2016-06-08 17:15:36 +02002104 }
2105}
2106EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2107
2108/**
Boris Brezillonb25c60a2018-07-03 09:50:19 +02002109 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2110 * @old_state: atomic state object with old state structures
2111 *
2112 * This function walks all CRTCs and fake VBLANK events on those with
2113 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2114 * The primary use of this function is writeback connectors working in oneshot
2115 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2116 * when a job is queued, and any change to the pipeline that does not touch the
2117 * connector is leading to timeouts when calling
2118 * drm_atomic_helper_wait_for_vblanks() or
2119 * drm_atomic_helper_wait_for_flip_done().
2120 *
2121 * This is part of the atomic helper support for nonblocking commits, see
2122 * drm_atomic_helper_setup_commit() for an overview.
2123 */
2124void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2125{
2126 struct drm_crtc_state *new_crtc_state;
2127 struct drm_crtc *crtc;
2128 int i;
2129
2130 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2131 unsigned long flags;
2132
2133 if (!new_crtc_state->no_vblank)
2134 continue;
2135
2136 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2137 if (new_crtc_state->event) {
2138 drm_crtc_send_vblank_event(crtc,
2139 new_crtc_state->event);
2140 new_crtc_state->event = NULL;
2141 }
2142 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2143 }
2144}
2145EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2146
2147/**
Daniel Vettera095caa2016-06-08 17:15:36 +02002148 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002149 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002150 *
2151 * This function is used to signal completion of the hardware commit step. After
2152 * this step the driver is not allowed to read or change any permanent software
2153 * or hardware modeset state. The only exception is state protected by other
2154 * means than &drm_modeset_lock locks.
2155 *
2156 * Drivers should try to postpone any expensive or delayed cleanup work after
2157 * this function is called.
2158 *
2159 * This is part of the atomic helper support for nonblocking commits, see
2160 * drm_atomic_helper_setup_commit() for an overview.
2161 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002162void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002163{
2164 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002165 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002166 struct drm_crtc_commit *commit;
2167 int i;
2168
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002169 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2170 commit = new_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002171 if (!commit)
2172 continue;
2173
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002174 /*
2175 * copy new_crtc_state->commit to old_crtc_state->commit,
2176 * it's unsafe to touch new_crtc_state after hw_done,
2177 * but we still need to do so in cleanup_done().
2178 */
2179 if (old_crtc_state->commit)
2180 drm_crtc_commit_put(old_crtc_state->commit);
2181
2182 old_crtc_state->commit = drm_crtc_commit_get(commit);
2183
Daniel Vettera095caa2016-06-08 17:15:36 +02002184 /* backend must have consumed any event by now */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002185 WARN_ON(new_crtc_state->event);
Daniel Vettera095caa2016-06-08 17:15:36 +02002186 complete_all(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002187 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002188
2189 if (old_state->fake_commit) {
2190 complete_all(&old_state->fake_commit->hw_done);
2191 complete_all(&old_state->fake_commit->flip_done);
2192 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002193}
2194EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2195
2196/**
2197 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002198 * @old_state: atomic state object with old state structures
Daniel Vettera095caa2016-06-08 17:15:36 +02002199 *
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002200 * This signals completion of the atomic update @old_state, including any
2201 * cleanup work. If used, it must be called right before calling
Chris Wilson08536952016-10-14 13:18:18 +01002202 * drm_atomic_state_put().
Daniel Vettera095caa2016-06-08 17:15:36 +02002203 *
2204 * This is part of the atomic helper support for nonblocking commits, see
2205 * drm_atomic_helper_setup_commit() for an overview.
2206 */
Daniel Vetter1ea0c022016-11-21 18:18:02 +01002207void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
Daniel Vettera095caa2016-06-08 17:15:36 +02002208{
2209 struct drm_crtc *crtc;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002210 struct drm_crtc_state *old_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002211 struct drm_crtc_commit *commit;
2212 int i;
Daniel Vettera095caa2016-06-08 17:15:36 +02002213
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002214 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2215 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002216 if (WARN_ON(!commit))
2217 continue;
2218
Daniel Vettera095caa2016-06-08 17:15:36 +02002219 complete_all(&commit->cleanup_done);
2220 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2221
Daniel Vetter7141fd32017-06-21 11:16:27 +02002222 spin_lock(&crtc->commit_lock);
Daniel Vettera095caa2016-06-08 17:15:36 +02002223 list_del(&commit->commit_entry);
2224 spin_unlock(&crtc->commit_lock);
2225 }
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002226
2227 if (old_state->fake_commit)
2228 complete_all(&old_state->fake_commit->cleanup_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002229}
2230EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2231
Daniel Vettere8c833a2014-07-27 18:30:19 +02002232/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002233 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002234 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01002235 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002236 *
2237 * This function prepares plane state, specifically framebuffers, for the new
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002238 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2239 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2240 * any already successfully prepared framebuffer.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002241 *
2242 * Returns:
2243 * 0 on success, negative error code on failure.
2244 */
2245int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2246 struct drm_atomic_state *state)
2247{
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002248 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002249 struct drm_plane_state *new_plane_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002250 int ret, i, j;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002251
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002252 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02002253 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002254
2255 funcs = plane->helper_private;
2256
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002257 if (funcs->prepare_fb) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002258 ret = funcs->prepare_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002259 if (ret)
2260 goto fail;
2261 }
2262 }
2263
2264 return 0;
2265
2266fail:
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002267 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02002268 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002269
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002270 if (j >= i)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002271 continue;
2272
2273 funcs = plane->helper_private;
2274
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002275 if (funcs->cleanup_fb)
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002276 funcs->cleanup_fb(plane, new_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002277 }
2278
2279 return ret;
2280}
2281EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2282
Ville Syrjälä7135ac52016-09-19 16:33:42 +03002283static bool plane_crtc_active(const struct drm_plane_state *state)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002284{
2285 return state->crtc && state->crtc->state->active;
2286}
2287
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002288/**
2289 * drm_atomic_helper_commit_planes - commit plane state
2290 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002291 * @old_state: atomic state object with old state structures
Liu Ying2b58e982016-08-29 17:12:03 +08002292 * @flags: flags for committing plane state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002293 *
2294 * This function commits the new plane state using the plane and atomic helper
2295 * functions for planes and crtcs. It assumes that the atomic state has already
2296 * been pushed into the relevant object state pointers, since this step can no
2297 * longer fail.
2298 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01002299 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002300 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002301 *
2302 * Note that this function does all plane updates across all CRTCs in one step.
2303 * If the hardware can't support this approach look at
2304 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002305 *
2306 * Plane parameters can be updated by applications while the associated CRTC is
2307 * disabled. The DRM/KMS core will store the parameters in the plane state,
2308 * which will be available to the driver when the CRTC is turned on. As a result
2309 * most drivers don't need to be immediately notified of plane updates for a
2310 * disabled CRTC.
2311 *
Liu Ying2b58e982016-08-29 17:12:03 +08002312 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2313 * @flags in order not to receive plane update notifications related to a
2314 * disabled CRTC. This avoids the need to manually ignore plane updates in
Daniel Vetter6e48ae32015-09-08 13:52:45 +02002315 * driver code when the driver and/or hardware can't or just don't need to deal
2316 * with updates on disabled CRTCs, for example when supporting runtime PM.
2317 *
Liu Ying2b58e982016-08-29 17:12:03 +08002318 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2319 * display controllers require to disable a CRTC's planes when the CRTC is
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002320 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2321 * call for a plane if the CRTC of the old plane state needs a modesetting
2322 * operation. Of course, the drivers need to disable the planes in their CRTC
2323 * disable callbacks since no one else would do that.
Liu Ying2b58e982016-08-29 17:12:03 +08002324 *
2325 * The drm_atomic_helper_commit() default implementation doesn't set the
2326 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2327 * This should not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002328 */
2329void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002330 struct drm_atomic_state *old_state,
Liu Ying2b58e982016-08-29 17:12:03 +08002331 uint32_t flags)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002332{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002333 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002334 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002335 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002336 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002337 int i;
Liu Ying2b58e982016-08-29 17:12:03 +08002338 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2339 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002340
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002341 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02002342 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002343
2344 funcs = crtc->helper_private;
2345
2346 if (!funcs || !funcs->atomic_begin)
2347 continue;
2348
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002349 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002350 continue;
2351
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002352 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002353 }
2354
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002355 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02002356 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002357 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002358
2359 funcs = plane->helper_private;
2360
Thierry Reding3cad4b62014-11-25 13:05:12 +01002361 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002362 continue;
2363
Maarten Lankhorst51ffa122017-02-16 15:47:07 +01002364 disabling = drm_atomic_plane_disabling(old_plane_state,
2365 new_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002366
2367 if (active_only) {
2368 /*
2369 * Skip planes related to inactive CRTCs. If the plane
2370 * is enabled use the state of the current CRTC. If the
2371 * plane is being disabled use the state of the old
2372 * CRTC to avoid skipping planes being disabled on an
2373 * active CRTC.
2374 */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002375 if (!disabling && !plane_crtc_active(new_plane_state))
Laurent Pinchart216c59d2015-09-11 00:07:19 +03002376 continue;
2377 if (disabling && !plane_crtc_active(old_plane_state))
2378 continue;
2379 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002380
Thierry Reding407b8bd2014-11-20 12:05:50 +01002381 /*
2382 * Special-case disabling the plane if drivers support it.
2383 */
Liu Ying2b58e982016-08-29 17:12:03 +08002384 if (disabling && funcs->atomic_disable) {
2385 struct drm_crtc_state *crtc_state;
2386
2387 crtc_state = old_plane_state->crtc->state;
2388
2389 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2390 no_disable)
2391 continue;
2392
Thierry Reding407b8bd2014-11-20 12:05:50 +01002393 funcs->atomic_disable(plane, old_plane_state);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002394 } else if (new_plane_state->crtc || disabling) {
Thierry Reding407b8bd2014-11-20 12:05:50 +01002395 funcs->atomic_update(plane, old_plane_state);
Liu Ying2b58e982016-08-29 17:12:03 +08002396 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002397 }
2398
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002399 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02002400 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002401
2402 funcs = crtc->helper_private;
2403
2404 if (!funcs || !funcs->atomic_flush)
2405 continue;
2406
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002407 if (active_only && !new_crtc_state->active)
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02002408 continue;
2409
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002410 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002411 }
2412}
2413EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2414
2415/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002416 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2417 * @old_crtc_state: atomic state object with the old crtc state
2418 *
2419 * This function commits the new plane state using the plane and atomic helper
2420 * functions for planes on the specific crtc. It assumes that the atomic state
2421 * has already been pushed into the relevant object state pointers, since this
2422 * step can no longer fail.
2423 *
2424 * This function is useful when plane updates should be done crtc-by-crtc
2425 * instead of one global step like drm_atomic_helper_commit_planes() does.
2426 *
2427 * This function can only be savely used when planes are not allowed to move
2428 * between different CRTCs because this function doesn't handle inter-CRTC
2429 * depencies. Callers need to ensure that either no such depencies exist,
2430 * resolve them through ordering of commit calls or through some other means.
2431 */
2432void
2433drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2434{
2435 const struct drm_crtc_helper_funcs *crtc_funcs;
2436 struct drm_crtc *crtc = old_crtc_state->crtc;
2437 struct drm_atomic_state *old_state = old_crtc_state->state;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002438 struct drm_crtc_state *new_crtc_state =
2439 drm_atomic_get_new_crtc_state(old_state, crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002440 struct drm_plane *plane;
2441 unsigned plane_mask;
2442
2443 plane_mask = old_crtc_state->plane_mask;
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002444 plane_mask |= new_crtc_state->plane_mask;
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002445
2446 crtc_funcs = crtc->helper_private;
2447 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002448 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002449
2450 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2451 struct drm_plane_state *old_plane_state =
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01002452 drm_atomic_get_old_plane_state(old_state, plane);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002453 struct drm_plane_state *new_plane_state =
2454 drm_atomic_get_new_plane_state(old_state, plane);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002455 const struct drm_plane_helper_funcs *plane_funcs;
2456
2457 plane_funcs = plane->helper_private;
2458
2459 if (!old_plane_state || !plane_funcs)
2460 continue;
2461
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002462 WARN_ON(new_plane_state->crtc &&
2463 new_plane_state->crtc != crtc);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002464
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002465 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002466 plane_funcs->atomic_disable)
2467 plane_funcs->atomic_disable(plane, old_plane_state);
Ville Syrjäläe35a2f92018-06-26 23:41:44 +03002468 else if (new_plane_state->crtc ||
2469 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002470 plane_funcs->atomic_update(plane, old_plane_state);
2471 }
2472
2473 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02002474 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02002475}
2476EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2477
2478/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02002479 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
Liu Ying28500292016-08-26 15:30:39 +08002480 * @old_crtc_state: atomic state object with the old CRTC state
Jyri Sarha6753ba92015-11-27 16:14:01 +02002481 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2482 *
2483 * Disables all planes associated with the given CRTC. This can be
Liu Ying28500292016-08-26 15:30:39 +08002484 * used for instance in the CRTC helper atomic_disable callback to disable
2485 * all planes.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002486 *
2487 * If the atomic-parameter is set the function calls the CRTC's
2488 * atomic_begin hook before and atomic_flush hook after disabling the
2489 * planes.
2490 *
2491 * It is a bug to call this function without having implemented the
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002492 * &drm_plane_helper_funcs.atomic_disable plane hook.
Jyri Sarha6753ba92015-11-27 16:14:01 +02002493 */
Liu Ying28500292016-08-26 15:30:39 +08002494void
2495drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2496 bool atomic)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002497{
Liu Ying28500292016-08-26 15:30:39 +08002498 struct drm_crtc *crtc = old_crtc_state->crtc;
Jyri Sarha6753ba92015-11-27 16:14:01 +02002499 const struct drm_crtc_helper_funcs *crtc_funcs =
2500 crtc->helper_private;
2501 struct drm_plane *plane;
2502
2503 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2504 crtc_funcs->atomic_begin(crtc, NULL);
2505
Liu Ying28500292016-08-26 15:30:39 +08002506 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
Jyri Sarha6753ba92015-11-27 16:14:01 +02002507 const struct drm_plane_helper_funcs *plane_funcs =
2508 plane->helper_private;
2509
Liu Ying28500292016-08-26 15:30:39 +08002510 if (!plane_funcs)
Jyri Sarha6753ba92015-11-27 16:14:01 +02002511 continue;
2512
2513 WARN_ON(!plane_funcs->atomic_disable);
2514 if (plane_funcs->atomic_disable)
2515 plane_funcs->atomic_disable(plane, NULL);
2516 }
2517
2518 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2519 crtc_funcs->atomic_flush(crtc, NULL);
2520}
2521EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2522
2523/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002524 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2525 * @dev: DRM device
2526 * @old_state: atomic state object with old state structures
2527 *
2528 * This function cleans up plane state, specifically framebuffers, from the old
2529 * configuration. Hence the old configuration must be perserved in @old_state to
2530 * be able to call this function.
2531 *
2532 * This function must also be called on the new state when the atomic update
2533 * fails at any point after calling drm_atomic_helper_prepare_planes().
2534 */
2535void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2536 struct drm_atomic_state *old_state)
2537{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002538 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002539 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002540 int i;
2541
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002542 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
Ville Syrjäläb5ceff22015-03-10 14:35:20 +02002543 const struct drm_plane_helper_funcs *funcs;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002544 struct drm_plane_state *plane_state;
2545
2546 /*
2547 * This might be called before swapping when commit is aborted,
2548 * in which case we have to cleanup the new state.
2549 */
2550 if (old_plane_state == plane->state)
2551 plane_state = new_plane_state;
2552 else
2553 plane_state = old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002554
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002555 funcs = plane->helper_private;
2556
Maarten Lankhorst844f9112015-09-02 10:42:40 +02002557 if (funcs->cleanup_fb)
2558 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002559 }
2560}
2561EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2562
2563/**
2564 * drm_atomic_helper_swap_state - store atomic state into current sw state
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002565 * @state: atomic state
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002566 * @stall: stall for preceeding commits
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002567 *
2568 * This function stores the atomic state into the current state pointers in all
2569 * driver objects. It should be called after all failing steps have been done
2570 * and succeeded, but before the actual hardware state is committed.
2571 *
2572 * For cleanup and error recovery the current state for all changed objects will
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002573 * be swapped into @state.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002574 *
2575 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2576 *
2577 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2578 *
2579 * 2. Do any other steps that might fail.
2580 *
2581 * 3. Put the staged state into the current state pointers with this function.
2582 *
2583 * 4. Actually commit the hardware state.
2584 *
Daniel Vetter26196f72015-08-25 16:26:03 +02002585 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002586 * contains the old state. Also do any other cleanup required with that state.
Daniel Vettera095caa2016-06-08 17:15:36 +02002587 *
2588 * @stall must be set when nonblocking commits for this driver directly access
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002589 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2590 * the current atomic helpers this is almost always the case, since the helpers
Daniel Vettera095caa2016-06-08 17:15:36 +02002591 * don't pass the right state structures to the callbacks.
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002592 *
2593 * Returns:
2594 *
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002595 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2596 * waiting for the previous commits has been interrupted.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002597 */
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002598int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
Daniel Vetter5e84c262016-06-10 00:06:32 +02002599 bool stall)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002600{
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002601 int i, ret;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002602 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002603 struct drm_connector_state *old_conn_state, *new_conn_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002604 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002605 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Daniel Vetterbe9174a2016-06-02 00:06:24 +02002606 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002607 struct drm_plane_state *old_plane_state, *new_plane_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002608 struct drm_crtc_commit *commit;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002609 struct drm_private_obj *obj;
2610 struct drm_private_state *old_obj_state, *new_obj_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002611
2612 if (stall) {
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002613 /*
2614 * We have to stall for hw_done here before
2615 * drm_atomic_helper_wait_for_dependencies() because flip
2616 * depth > 1 is not yet supported by all drivers. As long as
2617 * obj->state is directly dereferenced anywhere in the drivers
2618 * atomic_commit_tail function, then it's unsafe to swap state
2619 * before drm_atomic_helper_commit_hw_done() is called.
2620 */
2621
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002622 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2623 commit = old_crtc_state->commit;
Daniel Vettera095caa2016-06-08 17:15:36 +02002624
2625 if (!commit)
2626 continue;
2627
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002628 ret = wait_for_completion_interruptible(&commit->hw_done);
Maarten Lankhorstc4bbb732017-07-11 16:33:14 +02002629 if (ret)
2630 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002631 }
Daniel Vettera095caa2016-06-08 17:15:36 +02002632
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02002633 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2634 commit = old_conn_state->commit;
2635
2636 if (!commit)
2637 continue;
2638
2639 ret = wait_for_completion_interruptible(&commit->hw_done);
2640 if (ret)
2641 return ret;
2642 }
2643
2644 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2645 commit = old_plane_state->commit;
2646
2647 if (!commit)
2648 continue;
2649
2650 ret = wait_for_completion_interruptible(&commit->hw_done);
Daniel Vettera095caa2016-06-08 17:15:36 +02002651 if (ret)
2652 return ret;
Daniel Vettera095caa2016-06-08 17:15:36 +02002653 }
2654 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002655
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002656 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002657 WARN_ON(connector->state != old_conn_state);
2658
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002659 old_conn_state->state = state;
2660 new_conn_state->state = NULL;
2661
2662 state->connectors[i].state = old_conn_state;
2663 connector->state = new_conn_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002664 }
2665
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002666 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002667 WARN_ON(crtc->state != old_crtc_state);
2668
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002669 old_crtc_state->state = state;
2670 new_crtc_state->state = NULL;
2671
2672 state->crtcs[i].state = old_crtc_state;
2673 crtc->state = new_crtc_state;
Daniel Vettera095caa2016-06-08 17:15:36 +02002674
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002675 if (new_crtc_state->commit) {
Daniel Vettera095caa2016-06-08 17:15:36 +02002676 spin_lock(&crtc->commit_lock);
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002677 list_add(&new_crtc_state->commit->commit_entry,
Daniel Vettera095caa2016-06-08 17:15:36 +02002678 &crtc->commit_list);
2679 spin_unlock(&crtc->commit_lock);
2680
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02002681 new_crtc_state->commit->event = NULL;
Daniel Vettera095caa2016-06-08 17:15:36 +02002682 }
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002683 }
2684
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002685 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01002686 WARN_ON(plane->state != old_plane_state);
2687
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002688 old_plane_state->state = state;
2689 new_plane_state->state = NULL;
2690
2691 state->planes[i].state = old_plane_state;
2692 plane->state = new_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002693 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -07002694
Ville Syrjäläa4370c72017-07-12 18:51:02 +03002695 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2696 WARN_ON(obj->state != old_obj_state);
2697
2698 old_obj_state->state = state;
2699 new_obj_state->state = NULL;
2700
2701 state->private_objs[i].state = old_obj_state;
2702 obj->state = new_obj_state;
2703 }
Maarten Lankhorstc066d232017-07-11 16:33:04 +02002704
2705 return 0;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01002706}
2707EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002708
2709/**
2710 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2711 * @plane: plane object to update
2712 * @crtc: owning CRTC of owning plane
2713 * @fb: framebuffer to flip onto plane
2714 * @crtc_x: x offset of primary plane on crtc
2715 * @crtc_y: y offset of primary plane on crtc
2716 * @crtc_w: width of primary plane rectangle on crtc
2717 * @crtc_h: height of primary plane rectangle on crtc
2718 * @src_x: x offset of @fb for panning
2719 * @src_y: y offset of @fb for panning
2720 * @src_w: width of source rectangle in @fb
2721 * @src_h: height of source rectangle in @fb
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002722 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002723 *
2724 * Provides a default plane update handler using the atomic driver interface.
2725 *
2726 * RETURNS:
2727 * Zero on success, error code on failure
2728 */
2729int drm_atomic_helper_update_plane(struct drm_plane *plane,
2730 struct drm_crtc *crtc,
2731 struct drm_framebuffer *fb,
2732 int crtc_x, int crtc_y,
2733 unsigned int crtc_w, unsigned int crtc_h,
2734 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +01002735 uint32_t src_w, uint32_t src_h,
2736 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002737{
2738 struct drm_atomic_state *state;
2739 struct drm_plane_state *plane_state;
2740 int ret = 0;
2741
2742 state = drm_atomic_state_alloc(plane->dev);
2743 if (!state)
2744 return -ENOMEM;
2745
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002746 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002747 plane_state = drm_atomic_get_plane_state(state, plane);
2748 if (IS_ERR(plane_state)) {
2749 ret = PTR_ERR(plane_state);
2750 goto fail;
2751 }
2752
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002753 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02002754 if (ret != 0)
2755 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002756 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02002757 plane_state->crtc_x = crtc_x;
2758 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002759 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002760 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002761 plane_state->src_x = src_x;
2762 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02002763 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002764 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02002765
Daniel Vetter3671c582015-05-04 15:40:52 +02002766 if (plane == crtc->cursor)
2767 state->legacy_cursor_update = true;
2768
Daniel Vetter042652e2014-07-27 13:46:52 +02002769 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002770fail:
Chris Wilson08536952016-10-14 13:18:18 +01002771 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002772 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002773}
2774EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2775
2776/**
2777 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2778 * @plane: plane to disable
Daniel Vetter19315292017-03-22 22:50:43 +01002779 * @ctx: lock acquire context
Daniel Vetter042652e2014-07-27 13:46:52 +02002780 *
2781 * Provides a default plane disable handler using the atomic driver interface.
2782 *
2783 * RETURNS:
2784 * Zero on success, error code on failure
2785 */
Daniel Vetter19315292017-03-22 22:50:43 +01002786int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2787 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002788{
2789 struct drm_atomic_state *state;
2790 struct drm_plane_state *plane_state;
2791 int ret = 0;
2792
2793 state = drm_atomic_state_alloc(plane->dev);
2794 if (!state)
2795 return -ENOMEM;
2796
Daniel Vetterd26f96c2017-03-22 22:50:44 +01002797 state->acquire_ctx = ctx;
Daniel Vetter042652e2014-07-27 13:46:52 +02002798 plane_state = drm_atomic_get_plane_state(state, plane);
2799 if (IS_ERR(plane_state)) {
2800 ret = PTR_ERR(plane_state);
2801 goto fail;
2802 }
2803
Ville Syrjäläa36c0272018-03-22 17:22:58 +02002804 if (plane_state->crtc && plane_state->crtc->cursor == plane)
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01002805 plane_state->state->legacy_cursor_update = true;
2806
Rob Clarkbbb1e522015-08-25 15:35:58 -04002807 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002808 if (ret != 0)
2809 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01002810
Daniel Vetter042652e2014-07-27 13:46:52 +02002811 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002812fail:
Chris Wilson08536952016-10-14 13:18:18 +01002813 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002814 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002815}
2816EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2817
Rob Clarkbbb1e522015-08-25 15:35:58 -04002818/* just used from fb-helper and atomic-helper: */
2819int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2820 struct drm_plane_state *plane_state)
2821{
2822 int ret;
2823
2824 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2825 if (ret != 0)
2826 return ret;
2827
2828 drm_atomic_set_fb_for_plane(plane_state, NULL);
2829 plane_state->crtc_x = 0;
2830 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002831 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002832 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002833 plane_state->src_x = 0;
2834 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002835 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02002836 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002837
Rob Clarkbbb1e522015-08-25 15:35:58 -04002838 return 0;
2839}
2840
Daniel Vetter042652e2014-07-27 13:46:52 +02002841static int update_output_state(struct drm_atomic_state *state,
2842 struct drm_mode_set *set)
2843{
2844 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002845 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002846 struct drm_crtc_state *new_crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03002847 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002848 struct drm_connector_state *new_conn_state;
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002849 int ret, i;
Daniel Vetter042652e2014-07-27 13:46:52 +02002850
2851 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2852 state->acquire_ctx);
2853 if (ret)
2854 return ret;
2855
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002856 /* First disable all connectors on the target crtc. */
2857 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2858 if (ret)
2859 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002860
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002861 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2862 if (new_conn_state->crtc == set->crtc) {
2863 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Daniel Vetter042652e2014-07-27 13:46:52 +02002864 NULL);
2865 if (ret)
2866 return ret;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002867
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002868 /* Make sure legacy setCrtc always re-trains */
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002869 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
Daniel Vetter042652e2014-07-27 13:46:52 +02002870 }
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002871 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002872
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002873 /* Then set all connectors from set->connectors on the target crtc */
2874 for (i = 0; i < set->num_connectors; i++) {
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002875 new_conn_state = drm_atomic_get_connector_state(state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002876 set->connectors[i]);
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002877 if (IS_ERR(new_conn_state))
2878 return PTR_ERR(new_conn_state);
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002879
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002880 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
Maarten Lankhorst6ab520a2016-02-24 09:37:28 +01002881 set->crtc);
2882 if (ret)
2883 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002884 }
2885
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002886 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02002887 /* Don't update ->enable for the CRTC in the set_config request,
2888 * since a mismatch would indicate a bug in the upper layers.
2889 * The actual modeset code later on will catch any
2890 * inconsistencies here. */
2891 if (crtc == set->crtc)
2892 continue;
2893
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002894 if (!new_crtc_state->connector_mask) {
2895 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002896 NULL);
2897 if (ret < 0)
2898 return ret;
2899
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01002900 new_crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03002901 }
Daniel Vetter042652e2014-07-27 13:46:52 +02002902 }
2903
2904 return 0;
2905}
2906
2907/**
2908 * drm_atomic_helper_set_config - set a new config from userspace
2909 * @set: mode set configuration
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002910 * @ctx: lock acquisition context
Daniel Vetter042652e2014-07-27 13:46:52 +02002911 *
2912 * Provides a default crtc set_config handler using the atomic driver interface.
2913 *
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002914 * NOTE: For backwards compatibility with old userspace this automatically
2915 * resets the "link-status" property to GOOD, to force any link
2916 * re-training. The SETCRTC ioctl does not define whether an update does
2917 * need a full modeset or just a plane update, hence we're allowed to do
Daniel Vetter97e14fb2018-07-09 10:40:08 +02002918 * that. See also drm_connector_set_link_status_property().
Manasi Navare40ee6fb2016-12-16 12:29:06 +02002919 *
Daniel Vetter042652e2014-07-27 13:46:52 +02002920 * Returns:
2921 * Returns 0 on success, negative errno numbers on failure.
2922 */
Daniel Vettera4eff9a2017-03-22 22:50:57 +01002923int drm_atomic_helper_set_config(struct drm_mode_set *set,
2924 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter042652e2014-07-27 13:46:52 +02002925{
2926 struct drm_atomic_state *state;
2927 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02002928 int ret = 0;
2929
2930 state = drm_atomic_state_alloc(crtc->dev);
2931 if (!state)
2932 return -ENOMEM;
2933
Daniel Vetter38b64412017-03-22 22:50:58 +01002934 state->acquire_ctx = ctx;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002935 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01002936 if (ret != 0)
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002937 goto fail;
Daniel Stone819364d2015-05-26 14:36:48 +01002938
Maarten Lankhorst44596b82017-04-06 13:19:00 +02002939 ret = handle_conflicting_encoders(state, true);
2940 if (ret)
2941 return ret;
2942
Daniel Vetter042652e2014-07-27 13:46:52 +02002943 ret = drm_atomic_commit(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002944
Daniel Vetter1fa4da02017-03-29 19:41:36 +02002945fail:
Chris Wilson08536952016-10-14 13:18:18 +01002946 drm_atomic_state_put(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002947 return ret;
Daniel Vetter042652e2014-07-27 13:46:52 +02002948}
2949EXPORT_SYMBOL(drm_atomic_helper_set_config);
2950
Rob Clarkbbb1e522015-08-25 15:35:58 -04002951/* just used from fb-helper and atomic-helper: */
2952int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2953 struct drm_atomic_state *state)
2954{
2955 struct drm_crtc_state *crtc_state;
2956 struct drm_plane_state *primary_state;
2957 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02002958 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04002959 int ret;
2960
2961 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2962 if (IS_ERR(crtc_state))
2963 return PTR_ERR(crtc_state);
2964
2965 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2966 if (IS_ERR(primary_state))
2967 return PTR_ERR(primary_state);
2968
2969 if (!set->mode) {
2970 WARN_ON(set->fb);
2971 WARN_ON(set->num_connectors);
2972
2973 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2974 if (ret != 0)
2975 return ret;
2976
2977 crtc_state->active = false;
2978
2979 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2980 if (ret != 0)
2981 return ret;
2982
2983 drm_atomic_set_fb_for_plane(primary_state, NULL);
2984
2985 goto commit;
2986 }
2987
2988 WARN_ON(!set->fb);
2989 WARN_ON(!set->num_connectors);
2990
2991 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2992 if (ret != 0)
2993 return ret;
2994
2995 crtc_state->active = true;
2996
2997 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2998 if (ret != 0)
2999 return ret;
3000
Daniel Vetter196cd5d2017-01-25 07:26:56 +01003001 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Ville Syrjälä83926112015-11-16 17:02:34 +02003002
Rob Clarkbbb1e522015-08-25 15:35:58 -04003003 drm_atomic_set_fb_for_plane(primary_state, set->fb);
3004 primary_state->crtc_x = 0;
3005 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02003006 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02003007 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04003008 primary_state->src_x = set->x << 16;
3009 primary_state->src_y = set->y << 16;
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003010 if (drm_rotation_90_or_270(primary_state->rotation)) {
Ville Syrjälä83926112015-11-16 17:02:34 +02003011 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02003012 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03003013 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02003014 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02003015 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03003016 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04003017
3018commit:
3019 ret = update_output_state(state, set);
3020 if (ret)
3021 return ret;
3022
3023 return 0;
3024}
3025
Ville Syrjälä5e9cfeb2018-03-22 17:22:51 +02003026static int __drm_atomic_helper_disable_all(struct drm_device *dev,
3027 struct drm_modeset_acquire_ctx *ctx,
3028 bool clean_old_fbs)
Thierry Reding14942762015-12-02 17:50:04 +01003029{
3030 struct drm_atomic_state *state;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003031 struct drm_connector_state *conn_state;
Thierry Reding14942762015-12-02 17:50:04 +01003032 struct drm_connector *conn;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003033 struct drm_plane_state *plane_state;
3034 struct drm_plane *plane;
3035 struct drm_crtc_state *crtc_state;
3036 struct drm_crtc *crtc;
3037 int ret, i;
Thierry Reding14942762015-12-02 17:50:04 +01003038
3039 state = drm_atomic_state_alloc(dev);
3040 if (!state)
3041 return -ENOMEM;
3042
3043 state->acquire_ctx = ctx;
3044
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003045 drm_for_each_crtc(crtc, dev) {
Thierry Reding14942762015-12-02 17:50:04 +01003046 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3047 if (IS_ERR(crtc_state)) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003048 ret = PTR_ERR(crtc_state);
Thierry Reding14942762015-12-02 17:50:04 +01003049 goto free;
3050 }
3051
3052 crtc_state->active = false;
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003053
3054 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3055 if (ret < 0)
3056 goto free;
3057
3058 ret = drm_atomic_add_affected_planes(state, crtc);
3059 if (ret < 0)
3060 goto free;
3061
3062 ret = drm_atomic_add_affected_connectors(state, crtc);
3063 if (ret < 0)
3064 goto free;
Thierry Reding14942762015-12-02 17:50:04 +01003065 }
3066
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02003067 for_each_new_connector_in_state(state, conn, conn_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003068 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3069 if (ret < 0)
3070 goto free;
3071 }
3072
Maarten Lankhorstdfb8bb32017-07-12 10:13:31 +02003073 for_each_new_plane_in_state(state, plane, plane_state, i) {
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003074 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3075 if (ret < 0)
3076 goto free;
3077
3078 drm_atomic_set_fb_for_plane(plane_state, NULL);
3079 }
3080
3081 ret = drm_atomic_commit(state);
Thierry Reding14942762015-12-02 17:50:04 +01003082free:
Chris Wilson08536952016-10-14 13:18:18 +01003083 drm_atomic_state_put(state);
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003084 return ret;
Thierry Reding14942762015-12-02 17:50:04 +01003085}
Maarten Lankhorst9b2104f2017-02-21 14:51:40 +01003086
Ville Syrjälä5e9cfeb2018-03-22 17:22:51 +02003087/**
3088 * drm_atomic_helper_disable_all - disable all currently active outputs
3089 * @dev: DRM device
3090 * @ctx: lock acquisition context
3091 *
3092 * Loops through all connectors, finding those that aren't turned off and then
3093 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3094 * that they are connected to.
3095 *
3096 * This is used for example in suspend/resume to disable all currently active
3097 * functions when suspending. If you just want to shut down everything at e.g.
3098 * driver unload, look at drm_atomic_helper_shutdown().
3099 *
3100 * Note that if callers haven't already acquired all modeset locks this might
3101 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3102 *
3103 * Returns:
3104 * 0 on success or a negative error code on failure.
3105 *
3106 * See also:
3107 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3108 * drm_atomic_helper_shutdown().
3109 */
3110int drm_atomic_helper_disable_all(struct drm_device *dev,
3111 struct drm_modeset_acquire_ctx *ctx)
3112{
3113 return __drm_atomic_helper_disable_all(dev, ctx, false);
3114}
Thierry Reding14942762015-12-02 17:50:04 +01003115EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3116
3117/**
Daniel Vetter18dddad2017-03-21 17:41:49 +01003118 * drm_atomic_helper_shutdown - shutdown all CRTC
3119 * @dev: DRM device
3120 *
3121 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3122 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3123 * that also takes a snapshot of the modeset state to be restored on resume.
3124 *
3125 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3126 * and it is the atomic version of drm_crtc_force_disable_all().
3127 */
3128void drm_atomic_helper_shutdown(struct drm_device *dev)
3129{
3130 struct drm_modeset_acquire_ctx ctx;
3131 int ret;
3132
3133 drm_modeset_acquire_init(&ctx, 0);
3134 while (1) {
3135 ret = drm_modeset_lock_all_ctx(dev, &ctx);
3136 if (!ret)
Ville Syrjälä5e9cfeb2018-03-22 17:22:51 +02003137 ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
Daniel Vetter18dddad2017-03-21 17:41:49 +01003138
3139 if (ret != -EDEADLK)
3140 break;
3141
3142 drm_modeset_backoff(&ctx);
3143 }
3144
3145 if (ret)
3146 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3147
3148 drm_modeset_drop_locks(&ctx);
3149 drm_modeset_acquire_fini(&ctx);
3150}
3151EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3152
3153/**
Thierry Reding14942762015-12-02 17:50:04 +01003154 * drm_atomic_helper_suspend - subsystem-level suspend helper
3155 * @dev: DRM device
3156 *
3157 * Duplicates the current atomic state, disables all active outputs and then
3158 * returns a pointer to the original atomic state to the caller. Drivers can
3159 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3160 * restore the output configuration that was active at the time the system
3161 * entered suspend.
3162 *
3163 * Note that it is potentially unsafe to use this. The atomic state object
3164 * returned by this function is assumed to be persistent. Drivers must ensure
3165 * that this holds true. Before calling this function, drivers must make sure
3166 * to suspend fbdev emulation so that nothing can be using the device.
3167 *
3168 * Returns:
3169 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3170 * encoded error code on failure. Drivers should store the returned atomic
3171 * state object and pass it to the drm_atomic_helper_resume() helper upon
3172 * resume.
3173 *
3174 * See also:
3175 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003176 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
Thierry Reding14942762015-12-02 17:50:04 +01003177 */
3178struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3179{
3180 struct drm_modeset_acquire_ctx ctx;
3181 struct drm_atomic_state *state;
3182 int err;
3183
3184 drm_modeset_acquire_init(&ctx, 0);
3185
3186retry:
3187 err = drm_modeset_lock_all_ctx(dev, &ctx);
3188 if (err < 0) {
3189 state = ERR_PTR(err);
3190 goto unlock;
3191 }
3192
3193 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3194 if (IS_ERR(state))
3195 goto unlock;
3196
3197 err = drm_atomic_helper_disable_all(dev, &ctx);
3198 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003199 drm_atomic_state_put(state);
Thierry Reding14942762015-12-02 17:50:04 +01003200 state = ERR_PTR(err);
3201 goto unlock;
3202 }
3203
3204unlock:
3205 if (PTR_ERR(state) == -EDEADLK) {
3206 drm_modeset_backoff(&ctx);
3207 goto retry;
3208 }
3209
3210 drm_modeset_drop_locks(&ctx);
3211 drm_modeset_acquire_fini(&ctx);
3212 return state;
3213}
3214EXPORT_SYMBOL(drm_atomic_helper_suspend);
3215
3216/**
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003217 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3218 * @state: duplicated atomic state to commit
3219 * @ctx: pointer to acquire_ctx to use for commit.
3220 *
3221 * The state returned by drm_atomic_helper_duplicate_state() and
3222 * drm_atomic_helper_suspend() is partially invalid, and needs to
3223 * be fixed up before commit.
3224 *
3225 * Returns:
3226 * 0 on success or a negative error code on failure.
3227 *
3228 * See also:
3229 * drm_atomic_helper_suspend()
3230 */
3231int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3232 struct drm_modeset_acquire_ctx *ctx)
3233{
Sean Paulaf6db7e2018-11-29 10:04:14 -05003234 int i, ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003235 struct drm_plane *plane;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003236 struct drm_plane_state *new_plane_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003237 struct drm_connector *connector;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003238 struct drm_connector_state *new_conn_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003239 struct drm_crtc *crtc;
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003240 struct drm_crtc_state *new_crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003241
3242 state->acquire_ctx = ctx;
3243
Ville Syrjäläe00fb852018-05-25 21:50:45 +03003244 for_each_new_plane_in_state(state, plane, new_plane_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003245 state->planes[i].old_state = plane->state;
3246
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003247 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003248 state->crtcs[i].old_state = crtc->state;
3249
Maarten Lankhorst415c3ac2017-03-01 10:21:26 +01003250 for_each_new_connector_in_state(state, connector, new_conn_state, i)
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003251 state->connectors[i].old_state = connector->state;
3252
Sean Paulaf6db7e2018-11-29 10:04:14 -05003253 ret = drm_atomic_commit(state);
3254
3255 state->acquire_ctx = NULL;
3256
3257 return ret;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003258}
3259EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3260
3261/**
Thierry Reding14942762015-12-02 17:50:04 +01003262 * drm_atomic_helper_resume - subsystem-level resume helper
3263 * @dev: DRM device
3264 * @state: atomic state to resume to
3265 *
3266 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3267 * grabs all modeset locks and commits the atomic state object. This can be
3268 * used in conjunction with the drm_atomic_helper_suspend() helper to
3269 * implement suspend/resume for drivers that support atomic mode-setting.
3270 *
3271 * Returns:
3272 * 0 on success or a negative error code on failure.
3273 *
3274 * See also:
3275 * drm_atomic_helper_suspend()
3276 */
3277int drm_atomic_helper_resume(struct drm_device *dev,
3278 struct drm_atomic_state *state)
3279{
Daniel Vettera5b84442017-04-03 10:32:53 +02003280 struct drm_modeset_acquire_ctx ctx;
Thierry Reding14942762015-12-02 17:50:04 +01003281 int err;
3282
3283 drm_mode_config_reset(dev);
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01003284
Daniel Vettera5b84442017-04-03 10:32:53 +02003285 drm_modeset_acquire_init(&ctx, 0);
3286 while (1) {
Daniel Vetter869e1882017-05-31 10:38:13 +02003287 err = drm_modeset_lock_all_ctx(dev, &ctx);
3288 if (err)
3289 goto out;
3290
Daniel Vettera5b84442017-04-03 10:32:53 +02003291 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
Daniel Vetter869e1882017-05-31 10:38:13 +02003292out:
Daniel Vettera5b84442017-04-03 10:32:53 +02003293 if (err != -EDEADLK)
3294 break;
3295
3296 drm_modeset_backoff(&ctx);
3297 }
3298
Jeffy Chen6d281b12017-10-09 14:46:41 +08003299 drm_atomic_state_put(state);
Daniel Vettera5b84442017-04-03 10:32:53 +02003300 drm_modeset_drop_locks(&ctx);
3301 drm_modeset_acquire_fini(&ctx);
Thierry Reding14942762015-12-02 17:50:04 +01003302
3303 return err;
3304}
3305EXPORT_SYMBOL(drm_atomic_helper_resume);
3306
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003307static int page_flip_common(struct drm_atomic_state *state,
3308 struct drm_crtc *crtc,
3309 struct drm_framebuffer *fb,
3310 struct drm_pending_vblank_event *event,
3311 uint32_t flags)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003312{
3313 struct drm_plane *plane = crtc->primary;
3314 struct drm_plane_state *plane_state;
3315 struct drm_crtc_state *crtc_state;
3316 int ret = 0;
3317
3318 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3319 if (IS_ERR(crtc_state))
3320 return PTR_ERR(crtc_state);
3321
3322 crtc_state->event = event;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003323 crtc_state->pageflip_flags = flags;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003324
3325 plane_state = drm_atomic_get_plane_state(state, plane);
3326 if (IS_ERR(plane_state))
3327 return PTR_ERR(plane_state);
3328
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003329 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3330 if (ret != 0)
3331 return ret;
3332 drm_atomic_set_fb_for_plane(plane_state, fb);
3333
3334 /* Make sure we don't accidentally do a full modeset. */
3335 state->allow_modeset = false;
3336 if (!crtc_state->active) {
Russell King6ac7c542017-02-13 12:27:03 +00003337 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3338 crtc->base.id, crtc->name);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003339 return -EINVAL;
3340 }
3341
3342 return ret;
3343}
3344
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003345/**
3346 * drm_atomic_helper_page_flip - execute a legacy page flip
3347 * @crtc: DRM crtc
3348 * @fb: DRM framebuffer
3349 * @event: optional DRM event to signal upon completion
3350 * @flags: flip flags for non-vblank sync'ed updates
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003351 * @ctx: lock acquisition context
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003352 *
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003353 * Provides a default &drm_crtc_funcs.page_flip implementation
3354 * using the atomic driver interface.
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003355 *
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003356 * Returns:
3357 * Returns 0 on success, negative errno numbers on failure.
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003358 *
3359 * See also:
3360 * drm_atomic_helper_page_flip_target()
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003361 */
3362int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3363 struct drm_framebuffer *fb,
3364 struct drm_pending_vblank_event *event,
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003365 uint32_t flags,
3366 struct drm_modeset_acquire_ctx *ctx)
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003367{
3368 struct drm_plane *plane = crtc->primary;
3369 struct drm_atomic_state *state;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003370 int ret = 0;
3371
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003372 state = drm_atomic_state_alloc(plane->dev);
3373 if (!state)
3374 return -ENOMEM;
3375
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003376 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003377
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003378 ret = page_flip_common(state, crtc, fb, event, flags);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003379 if (ret != 0)
3380 goto fail;
Daniel Vetter4cba6852015-12-08 09:49:20 +01003381
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02003382 ret = drm_atomic_nonblocking_commit(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003383fail:
Chris Wilson08536952016-10-14 13:18:18 +01003384 drm_atomic_state_put(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003385 return ret;
Daniel Vetter8bc0f312014-07-27 18:42:37 +02003386}
3387EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01003388
3389/**
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003390 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3391 * @crtc: DRM crtc
3392 * @fb: DRM framebuffer
3393 * @event: optional DRM event to signal upon completion
3394 * @flags: flip flags for non-vblank sync'ed updates
3395 * @target: specifying the target vblank period when the flip to take effect
Daniel Vetter41292b1f2017-03-22 22:50:50 +01003396 * @ctx: lock acquisition context
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003397 *
3398 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3399 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3400 * target vblank period to flip.
3401 *
3402 * Returns:
3403 * Returns 0 on success, negative errno numbers on failure.
3404 */
Daniel Vetter8c3a8182017-06-27 16:59:36 +02003405int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3406 struct drm_framebuffer *fb,
3407 struct drm_pending_vblank_event *event,
3408 uint32_t flags,
3409 uint32_t target,
3410 struct drm_modeset_acquire_ctx *ctx)
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003411{
3412 struct drm_plane *plane = crtc->primary;
3413 struct drm_atomic_state *state;
3414 struct drm_crtc_state *crtc_state;
3415 int ret = 0;
3416
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003417 state = drm_atomic_state_alloc(plane->dev);
3418 if (!state)
3419 return -ENOMEM;
3420
Daniel Vetter043e7fb2017-03-22 22:50:51 +01003421 state->acquire_ctx = ctx;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003422
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003423 ret = page_flip_common(state, crtc, fb, event, flags);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003424 if (ret != 0)
3425 goto fail;
3426
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01003427 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003428 if (WARN_ON(!crtc_state)) {
3429 ret = -EINVAL;
3430 goto fail;
3431 }
3432 crtc_state->target_vblank = target;
3433
3434 ret = drm_atomic_nonblocking_commit(state);
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003435fail:
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003436 drm_atomic_state_put(state);
3437 return ret;
Andrey Grodzovskyf869a6e2017-01-06 15:39:40 -05003438}
3439EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3440
3441/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003442 * drm_atomic_helper_best_encoder - Helper for
3443 * &drm_connector_helper_funcs.best_encoder callback
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003444 * @connector: Connector control structure
3445 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003446 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003447 * connectors that support exactly 1 encoder, statically determined at driver
3448 * init time.
3449 */
3450struct drm_encoder *
3451drm_atomic_helper_best_encoder(struct drm_connector *connector)
3452{
3453 WARN_ON(connector->encoder_ids[1]);
Keith Packard418da172017-03-14 23:25:07 -07003454 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
Noralf Trønnes9ecb5492016-05-11 18:09:21 +02003455}
3456EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3457
3458/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003459 * DOC: atomic state reset and initialization
3460 *
3461 * Both the drm core and the atomic helpers assume that there is always the full
3462 * and correct atomic software state for all connectors, CRTCs and planes
3463 * available. Which is a bit a problem on driver load and also after system
3464 * suspend. One way to solve this is to have a hardware state read-out
3465 * infrastructure which reconstructs the full software state (e.g. the i915
3466 * driver).
3467 *
3468 * The simpler solution is to just reset the software state to everything off,
3469 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3470 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01003471 *
3472 * On the upside the precise state tracking of atomic simplifies system suspend
3473 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3474 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3475 * For other drivers the building blocks are split out, see the documentation
3476 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01003477 */
3478
3479/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003480 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
Daniel Vetterd4617012014-11-03 15:56:43 +01003481 * @crtc: drm CRTC
3482 *
3483 * Resets the atomic state for @crtc by freeing the state pointer (which might
3484 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3485 */
3486void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3487{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003488 if (crtc->state)
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003489 __drm_atomic_helper_crtc_destroy_state(crtc->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003490
Daniel Vetterd4617012014-11-03 15:56:43 +01003491 kfree(crtc->state);
3492 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003493
3494 if (crtc->state)
3495 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01003496}
3497EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3498
3499/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003500 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3501 * @crtc: CRTC object
3502 * @state: atomic CRTC state
3503 *
3504 * Copies atomic state from a CRTC's current state and resets inferred values.
3505 * This is useful for drivers that subclass the CRTC state.
3506 */
3507void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3508 struct drm_crtc_state *state)
3509{
3510 memcpy(state, crtc->state, sizeof(*state));
3511
Daniel Stone99cf4a22015-05-25 19:11:51 +01003512 if (state->mode_blob)
Thierry Reding6472e502017-02-28 15:46:42 +01003513 drm_property_blob_get(state->mode_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003514 if (state->degamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003515 drm_property_blob_get(state->degamma_lut);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003516 if (state->ctm)
Thierry Reding6472e502017-02-28 15:46:42 +01003517 drm_property_blob_get(state->ctm);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003518 if (state->gamma_lut)
Thierry Reding6472e502017-02-28 15:46:42 +01003519 drm_property_blob_get(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003520 state->mode_changed = false;
3521 state->active_changed = false;
3522 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02003523 state->connectors_changed = false;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003524 state->color_mgmt_changed = false;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +02003525 state->zpos_changed = false;
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02003526 state->commit = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003527 state->event = NULL;
Andrey Grodzovsky6cbe5c42017-02-02 16:56:29 -05003528 state->pageflip_flags = 0;
Thierry Redingf5e78402015-01-28 14:54:32 +01003529}
3530EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3531
3532/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003533 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3534 * @crtc: drm CRTC
3535 *
3536 * Default CRTC state duplicate hook for drivers which don't have their own
3537 * subclassed CRTC state structure.
3538 */
3539struct drm_crtc_state *
3540drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3541{
3542 struct drm_crtc_state *state;
3543
3544 if (WARN_ON(!crtc->state))
3545 return NULL;
3546
Thierry Redingf5e78402015-01-28 14:54:32 +01003547 state = kmalloc(sizeof(*state), GFP_KERNEL);
3548 if (state)
3549 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003550
3551 return state;
3552}
3553EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3554
3555/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003556 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
Thierry Redingf5e78402015-01-28 14:54:32 +01003557 * @state: CRTC state object to release
3558 *
3559 * Releases all resources stored in the CRTC state without actually freeing
3560 * the memory of the CRTC state. This is useful for drivers that subclass the
3561 * CRTC state.
3562 */
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003563void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003564{
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02003565 if (state->commit) {
Leo (Sunpeng) Li1c6ceee2018-01-17 12:51:08 +01003566 /*
3567 * In the event that a non-blocking commit returns
3568 * -ERESTARTSYS before the commit_tail work is queued, we will
3569 * have an extra reference to the commit object. Release it, if
3570 * the event has not been consumed by the worker.
3571 *
3572 * state->event may be freed, so we can't directly look at
3573 * state->event->base.completion.
3574 */
3575 if (state->event && state->commit->abort_completion)
3576 drm_crtc_commit_put(state->commit);
3577
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02003578 kfree(state->commit->event);
3579 state->commit->event = NULL;
Leo (Sunpeng) Li1c6ceee2018-01-17 12:51:08 +01003580
Maarten Lankhorst163bcc22017-09-04 17:04:56 +02003581 drm_crtc_commit_put(state->commit);
3582 }
3583
Thierry Reding6472e502017-02-28 15:46:42 +01003584 drm_property_blob_put(state->mode_blob);
3585 drm_property_blob_put(state->degamma_lut);
3586 drm_property_blob_put(state->ctm);
3587 drm_property_blob_put(state->gamma_lut);
Thierry Redingf5e78402015-01-28 14:54:32 +01003588}
3589EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3590
3591/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003592 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3593 * @crtc: drm CRTC
3594 * @state: CRTC state object to release
3595 *
3596 * Default CRTC state destroy hook for drivers which don't have their own
3597 * subclassed CRTC state structure.
3598 */
3599void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3600 struct drm_crtc_state *state)
3601{
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02003602 __drm_atomic_helper_crtc_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003603 kfree(state);
3604}
3605EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3606
3607/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003608 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
Daniel Vetterd4617012014-11-03 15:56:43 +01003609 * @plane: drm plane
3610 *
3611 * Resets the atomic state for @plane by freeing the state pointer (which might
3612 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3613 */
3614void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3615{
Daniel Vetterb0b55112016-04-22 22:10:29 +02003616 if (plane->state)
Daniel Vetter2f701692016-05-09 16:34:10 +02003617 __drm_atomic_helper_plane_destroy_state(plane->state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003618
Daniel Vetterd4617012014-11-03 15:56:43 +01003619 kfree(plane->state);
3620 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003621
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003622 if (plane->state) {
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003623 plane->state->plane = plane;
Robert Fossc2c446a2017-05-19 16:50:17 -04003624 plane->state->rotation = DRM_MODE_ROTATE_0;
Maxime Ripardae0e2822018-04-11 09:39:25 +02003625
3626 /* Reset the alpha value to fully opaque if it matters */
3627 if (plane->alpha_property)
3628 plane->state->alpha = plane->alpha_property->values[1];
Marek Szyprowski25aaa3a2016-01-19 09:26:48 +01003629 }
Daniel Vetterd4617012014-11-03 15:56:43 +01003630}
3631EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3632
3633/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003634 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3635 * @plane: plane object
3636 * @state: atomic plane state
3637 *
3638 * Copies atomic state from a plane's current state. This is useful for
3639 * drivers that subclass the plane state.
3640 */
3641void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3642 struct drm_plane_state *state)
3643{
3644 memcpy(state, plane->state, sizeof(*state));
3645
3646 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003647 drm_framebuffer_get(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003648
3649 state->fence = NULL;
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02003650 state->commit = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003651}
3652EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3653
3654/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003655 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3656 * @plane: drm plane
3657 *
3658 * Default plane state duplicate hook for drivers which don't have their own
3659 * subclassed plane state structure.
3660 */
3661struct drm_plane_state *
3662drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3663{
Daniel Vetter321ebf02014-11-04 22:57:27 +01003664 struct drm_plane_state *state;
3665
Daniel Vetterd4617012014-11-03 15:56:43 +01003666 if (WARN_ON(!plane->state))
3667 return NULL;
3668
Thierry Redingf5e78402015-01-28 14:54:32 +01003669 state = kmalloc(sizeof(*state), GFP_KERNEL);
3670 if (state)
3671 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01003672
3673 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003674}
3675EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3676
3677/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003678 * __drm_atomic_helper_plane_destroy_state - release plane state
Thierry Redingf5e78402015-01-28 14:54:32 +01003679 * @state: plane state object to release
3680 *
3681 * Releases all resources stored in the plane state without actually freeing
3682 * the memory of the plane state. This is useful for drivers that subclass the
3683 * plane state.
3684 */
Daniel Vetter2f701692016-05-09 16:34:10 +02003685void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003686{
3687 if (state->fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01003688 drm_framebuffer_put(state->fb);
Gustavo Padovan96260142016-11-15 22:06:39 +09003689
3690 if (state->fence)
3691 dma_fence_put(state->fence);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02003692
3693 if (state->commit)
3694 drm_crtc_commit_put(state->commit);
Thierry Redingf5e78402015-01-28 14:54:32 +01003695}
3696EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3697
3698/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003699 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3700 * @plane: drm plane
3701 * @state: plane state object to release
3702 *
3703 * Default plane state destroy hook for drivers which don't have their own
3704 * subclassed plane state structure.
3705 */
3706void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01003707 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01003708{
Daniel Vetter2f701692016-05-09 16:34:10 +02003709 __drm_atomic_helper_plane_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003710 kfree(state);
3711}
3712EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3713
3714/**
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003715 * __drm_atomic_helper_connector_reset - reset state on connector
3716 * @connector: drm connector
3717 * @conn_state: connector state to assign
3718 *
3719 * Initializes the newly allocated @conn_state and assigns it to
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003720 * the &drm_conector->state pointer of @connector, usually required when
3721 * initializing the drivers or when called from the &drm_connector_funcs.reset
3722 * hook.
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003723 *
3724 * This is useful for drivers that subclass the connector state.
3725 */
3726void
3727__drm_atomic_helper_connector_reset(struct drm_connector *connector,
3728 struct drm_connector_state *conn_state)
3729{
3730 if (conn_state)
3731 conn_state->connector = connector;
3732
3733 connector->state = conn_state;
3734}
3735EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3736
3737/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01003738 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
Daniel Vetterd4617012014-11-03 15:56:43 +01003739 * @connector: drm connector
3740 *
3741 * Resets the atomic state for @connector by freeing the state pointer (which
3742 * might be NULL, e.g. at driver load time) and allocating a new empty state
3743 * object.
3744 */
3745void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3746{
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003747 struct drm_connector_state *conn_state =
3748 kzalloc(sizeof(*conn_state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01003749
Daniel Vetterb0b55112016-04-22 22:10:29 +02003750 if (connector->state)
Daniel Vetterfabd9102016-05-09 16:34:11 +02003751 __drm_atomic_helper_connector_destroy_state(connector->state);
Daniel Vetterb0b55112016-04-22 22:10:29 +02003752
Maarten Lankhorst4cd39912016-01-04 12:53:16 +01003753 kfree(connector->state);
3754 __drm_atomic_helper_connector_reset(connector, conn_state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003755}
3756EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3757
3758/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003759 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3760 * @connector: connector object
3761 * @state: atomic connector state
3762 *
3763 * Copies atomic state from a connector's current state. This is useful for
3764 * drivers that subclass the connector state.
3765 */
3766void
3767__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3768 struct drm_connector_state *state)
3769{
3770 memcpy(state, connector->state, sizeof(*state));
Dave Airlied2307de2016-04-27 11:27:39 +10003771 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003772 drm_connector_get(connector);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02003773 state->commit = NULL;
Brian Starkey935774c2017-03-29 17:42:32 +01003774
3775 /* Don't copy over a writeback job, they are used only once */
3776 state->writeback_job = NULL;
Thierry Redingf5e78402015-01-28 14:54:32 +01003777}
3778EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3779
3780/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003781 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3782 * @connector: drm connector
3783 *
3784 * Default connector state duplicate hook for drivers which don't have their own
3785 * subclassed connector state structure.
3786 */
3787struct drm_connector_state *
3788drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3789{
Thierry Redingf5e78402015-01-28 14:54:32 +01003790 struct drm_connector_state *state;
3791
Daniel Vetterd4617012014-11-03 15:56:43 +01003792 if (WARN_ON(!connector->state))
3793 return NULL;
3794
Thierry Redingf5e78402015-01-28 14:54:32 +01003795 state = kmalloc(sizeof(*state), GFP_KERNEL);
3796 if (state)
3797 __drm_atomic_helper_connector_duplicate_state(connector, state);
3798
3799 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01003800}
3801EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3802
3803/**
Thierry Reding397fd772015-09-08 15:00:45 +02003804 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3805 * @dev: DRM device
3806 * @ctx: lock acquisition context
3807 *
3808 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01003809 * duplicating their respective states. This is used for example by suspend/
3810 * resume support code to save the state prior to suspend such that it can
3811 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02003812 *
3813 * Note that this treats atomic state as persistent between save and restore.
3814 * Drivers must make sure that this is possible and won't result in confusion
3815 * or erroneous behaviour.
3816 *
3817 * Note that if callers haven't already acquired all modeset locks this might
3818 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3819 *
3820 * Returns:
3821 * A pointer to the copy of the atomic state object on success or an
3822 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01003823 *
3824 * See also:
3825 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02003826 */
3827struct drm_atomic_state *
3828drm_atomic_helper_duplicate_state(struct drm_device *dev,
3829 struct drm_modeset_acquire_ctx *ctx)
3830{
3831 struct drm_atomic_state *state;
3832 struct drm_connector *conn;
Daniel Vetterc36a3252016-12-15 16:58:43 +01003833 struct drm_connector_list_iter conn_iter;
Thierry Reding397fd772015-09-08 15:00:45 +02003834 struct drm_plane *plane;
3835 struct drm_crtc *crtc;
3836 int err = 0;
3837
3838 state = drm_atomic_state_alloc(dev);
3839 if (!state)
3840 return ERR_PTR(-ENOMEM);
3841
3842 state->acquire_ctx = ctx;
3843
3844 drm_for_each_crtc(crtc, dev) {
3845 struct drm_crtc_state *crtc_state;
3846
3847 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3848 if (IS_ERR(crtc_state)) {
3849 err = PTR_ERR(crtc_state);
3850 goto free;
3851 }
3852 }
3853
3854 drm_for_each_plane(plane, dev) {
3855 struct drm_plane_state *plane_state;
3856
3857 plane_state = drm_atomic_get_plane_state(state, plane);
3858 if (IS_ERR(plane_state)) {
3859 err = PTR_ERR(plane_state);
3860 goto free;
3861 }
3862 }
3863
Thierry Redingb982dab2017-02-28 15:46:43 +01003864 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +01003865 drm_for_each_connector_iter(conn, &conn_iter) {
Thierry Reding397fd772015-09-08 15:00:45 +02003866 struct drm_connector_state *conn_state;
3867
3868 conn_state = drm_atomic_get_connector_state(state, conn);
3869 if (IS_ERR(conn_state)) {
3870 err = PTR_ERR(conn_state);
Thierry Redingb982dab2017-02-28 15:46:43 +01003871 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003872 goto free;
3873 }
3874 }
Thierry Redingb982dab2017-02-28 15:46:43 +01003875 drm_connector_list_iter_end(&conn_iter);
Thierry Reding397fd772015-09-08 15:00:45 +02003876
3877 /* clear the acquire context so that it isn't accidentally reused */
3878 state->acquire_ctx = NULL;
3879
3880free:
3881 if (err < 0) {
Chris Wilson08536952016-10-14 13:18:18 +01003882 drm_atomic_state_put(state);
Thierry Reding397fd772015-09-08 15:00:45 +02003883 state = ERR_PTR(err);
3884 }
3885
3886 return state;
3887}
3888EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3889
3890/**
Thierry Redingf5e78402015-01-28 14:54:32 +01003891 * __drm_atomic_helper_connector_destroy_state - release connector state
Thierry Redingf5e78402015-01-28 14:54:32 +01003892 * @state: connector state object to release
3893 *
3894 * Releases all resources stored in the connector state without actually
3895 * freeing the memory of the connector state. This is useful for drivers that
3896 * subclass the connector state.
3897 */
3898void
Daniel Vetterfabd9102016-05-09 16:34:11 +02003899__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
Thierry Redingf5e78402015-01-28 14:54:32 +01003900{
Dave Airlied2307de2016-04-27 11:27:39 +10003901 if (state->crtc)
Thierry Redingad093602017-02-28 15:46:39 +01003902 drm_connector_put(state->connector);
Maarten Lankhorst21a01ab2017-09-04 12:48:37 +02003903
3904 if (state->commit)
3905 drm_crtc_commit_put(state->commit);
Thierry Redingf5e78402015-01-28 14:54:32 +01003906}
3907EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3908
3909/**
Daniel Vetterd4617012014-11-03 15:56:43 +01003910 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3911 * @connector: drm connector
3912 * @state: connector state object to release
3913 *
3914 * Default connector state destroy hook for drivers which don't have their own
3915 * subclassed connector state structure.
3916 */
3917void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3918 struct drm_connector_state *state)
3919{
Daniel Vetterfabd9102016-05-09 16:34:11 +02003920 __drm_atomic_helper_connector_destroy_state(state);
Daniel Vetterd4617012014-11-03 15:56:43 +01003921 kfree(state);
3922}
3923EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003924
3925/**
3926 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3927 * @crtc: CRTC object
3928 * @red: red correction table
3929 * @green: green correction table
3930 * @blue: green correction table
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003931 * @size: size of the tables
Daniel Vetter6d124ff2017-04-03 10:33:01 +02003932 * @ctx: lock acquire context
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003933 *
3934 * Implements support for legacy gamma correction table for drivers
3935 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
Daniel Vetter2e381782017-04-12 17:20:06 +02003936 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3937 * how the atomic color management and gamma tables work.
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003938 */
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003939int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3940 u16 *red, u16 *green, u16 *blue,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02003941 uint32_t size,
3942 struct drm_modeset_acquire_ctx *ctx)
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003943{
3944 struct drm_device *dev = crtc->dev;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003945 struct drm_atomic_state *state;
3946 struct drm_crtc_state *crtc_state;
3947 struct drm_property_blob *blob = NULL;
3948 struct drm_color_lut *blob_data;
3949 int i, ret = 0;
Peter Rosine2b9dd32017-07-13 18:25:26 +02003950 bool replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003951
3952 state = drm_atomic_state_alloc(crtc->dev);
3953 if (!state)
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003954 return -ENOMEM;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003955
3956 blob = drm_property_create_blob(dev,
3957 sizeof(struct drm_color_lut) * size,
3958 NULL);
Lionel Landwerlin562c5b42016-03-10 12:04:21 +00003959 if (IS_ERR(blob)) {
3960 ret = PTR_ERR(blob);
Lionel Landwerlinc1f415c2016-03-11 12:17:26 +00003961 blob = NULL;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003962 goto fail;
3963 }
3964
3965 /* Prepare GAMMA_LUT with the legacy values. */
Ville Syrjälä11b83e32018-02-23 21:25:02 +02003966 blob_data = blob->data;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003967 for (i = 0; i < size; i++) {
3968 blob_data[i].red = red[i];
3969 blob_data[i].green = green[i];
3970 blob_data[i].blue = blue[i];
3971 }
3972
Daniel Vetter3a09f732017-04-03 10:33:02 +02003973 state->acquire_ctx = ctx;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003974 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3975 if (IS_ERR(crtc_state)) {
3976 ret = PTR_ERR(crtc_state);
3977 goto fail;
3978 }
3979
3980 /* Reset DEGAMMA_LUT and CTM properties. */
Peter Rosine2b9dd32017-07-13 18:25:26 +02003981 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3982 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3983 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3984 crtc_state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003985
3986 ret = drm_atomic_commit(state);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003987
Daniel Vetter3a09f732017-04-03 10:33:02 +02003988fail:
Chris Wilson08536952016-10-14 13:18:18 +01003989 drm_atomic_state_put(state);
Thierry Reding6472e502017-02-28 15:46:42 +01003990 drm_property_blob_put(blob);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02003991 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00003992}
3993EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003994
3995/**
3996 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
3997 * @obj: CRTC object
3998 * @state: new private object state
3999 *
4000 * Copies atomic state from a private objects's current state and resets inferred values.
4001 * This is useful for drivers that subclass the private state.
4002 */
4003void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
4004 struct drm_private_state *state)
4005{
4006 memcpy(state, obj->state, sizeof(*state));
4007}
4008EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);