blob: 74a5fc4deef688ced04b1e388d1f44dbae017ccb [file] [log] [blame]
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28#include <drm/drmP.h>
29#include <drm/drm_atomic.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020032#include <drm/drm_atomic_helper.h>
Daniel Vettere2330f02014-10-29 11:34:56 +010033#include <linux/fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Daniel Vetter3150c7d2014-11-06 20:53:29 +010035/**
36 * DOC: overview
37 *
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
43 *
44 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020045 * drm_atomic_helper_check() and for the commit callback with
46 * drm_atomic_helper_commit(). But the individual stages and callbacks are
47 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010048 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020051 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
52 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010053 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
55 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010056static void
57drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
58 struct drm_plane_state *plane_state,
59 struct drm_plane *plane)
60{
61 struct drm_crtc_state *crtc_state;
62
63 if (plane->state->crtc) {
Rob Clark4b08eae2014-12-08 10:45:43 -050064 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
Daniel Vetterc2fcd272014-11-05 00:14:14 +010065
66 if (WARN_ON(!crtc_state))
67 return;
68
69 crtc_state->planes_changed = true;
70 }
71
72 if (plane_state->crtc) {
73 crtc_state =
74 state->crtc_states[drm_crtc_index(plane_state->crtc)];
75
76 if (WARN_ON(!crtc_state))
77 return;
78
79 crtc_state->planes_changed = true;
80 }
81}
82
Daniel Vetter97ac3202015-12-03 10:49:14 +010083static bool
84check_pending_encoder_assignment(struct drm_atomic_state *state,
85 struct drm_encoder *new_encoder,
86 struct drm_connector *new_connector)
87{
88 struct drm_connector *connector;
89 struct drm_connector_state *conn_state;
90 int i;
91
92 for_each_connector_in_state(state, connector, conn_state, i) {
93 if (conn_state->best_encoder != new_encoder)
94 continue;
95
96 /* encoder already assigned and we're trying to re-steal it! */
97 if (connector->state->best_encoder != conn_state->best_encoder)
98 return false;
99 }
100
101 return true;
102}
103
Daniel Vetter623369e2014-09-16 17:50:47 +0200104static struct drm_crtc *
105get_current_crtc_for_encoder(struct drm_device *dev,
106 struct drm_encoder *encoder)
107{
108 struct drm_mode_config *config = &dev->mode_config;
109 struct drm_connector *connector;
110
111 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
112
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +0200113 drm_for_each_connector(connector, dev) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200114 if (connector->state->best_encoder != encoder)
115 continue;
116
117 return connector->state->crtc;
118 }
119
120 return NULL;
121}
122
123static int
124steal_encoder(struct drm_atomic_state *state,
125 struct drm_encoder *encoder,
126 struct drm_crtc *encoder_crtc)
127{
128 struct drm_mode_config *config = &state->dev->mode_config;
129 struct drm_crtc_state *crtc_state;
130 struct drm_connector *connector;
131 struct drm_connector_state *connector_state;
Daniel Vetter042652e2014-07-27 13:46:52 +0200132 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200133
134 /*
135 * We can only steal an encoder coming from a connector, which means we
136 * must already hold the connection_mutex.
137 */
138 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
139
Daniel Vetter17a38d92015-02-22 12:24:16 +0100140 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
141 encoder->base.id, encoder->name,
142 encoder_crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200143
144 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
145 if (IS_ERR(crtc_state))
146 return PTR_ERR(crtc_state);
147
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200148 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200149
150 list_for_each_entry(connector, &config->connector_list, head) {
151 if (connector->state->best_encoder != encoder)
152 continue;
153
Daniel Vetter17a38d92015-02-22 12:24:16 +0100154 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
155 connector->base.id,
156 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200157
158 connector_state = drm_atomic_get_connector_state(state,
159 connector);
160 if (IS_ERR(connector_state))
161 return PTR_ERR(connector_state);
162
Daniel Vetter042652e2014-07-27 13:46:52 +0200163 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
164 if (ret)
165 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200166 connector_state->best_encoder = NULL;
167 }
168
169 return 0;
170}
171
172static int
173update_connector_routing(struct drm_atomic_state *state, int conn_idx)
174{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200175 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200176 struct drm_encoder *new_encoder;
177 struct drm_crtc *encoder_crtc;
178 struct drm_connector *connector;
179 struct drm_connector_state *connector_state;
180 struct drm_crtc_state *crtc_state;
181 int idx, ret;
182
183 connector = state->connectors[conn_idx];
184 connector_state = state->connector_states[conn_idx];
185
186 if (!connector)
187 return 0;
188
Daniel Vetter17a38d92015-02-22 12:24:16 +0100189 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
190 connector->base.id,
191 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200192
193 if (connector->state->crtc != connector_state->crtc) {
194 if (connector->state->crtc) {
195 idx = drm_crtc_index(connector->state->crtc);
196
197 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200198 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200199 }
200
201 if (connector_state->crtc) {
202 idx = drm_crtc_index(connector_state->crtc);
203
204 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200205 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200206 }
207 }
208
209 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100210 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200211 connector->base.id,
212 connector->name);
213
214 connector_state->best_encoder = NULL;
215
216 return 0;
217 }
218
219 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200220
221 if (funcs->atomic_best_encoder)
222 new_encoder = funcs->atomic_best_encoder(connector,
223 connector_state);
224 else
225 new_encoder = funcs->best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200226
227 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100228 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
229 connector->base.id,
230 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200231 return -EINVAL;
232 }
233
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100234 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
235 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
236 new_encoder->base.id,
237 new_encoder->name,
238 connector_state->crtc->base.id);
239 return -EINVAL;
240 }
241
Daniel Vetter623369e2014-09-16 17:50:47 +0200242 if (new_encoder == connector_state->best_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100243 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
244 connector->base.id,
245 connector->name,
246 new_encoder->base.id,
247 new_encoder->name,
248 connector_state->crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200249
250 return 0;
251 }
252
Daniel Vetter97ac3202015-12-03 10:49:14 +0100253 if (!check_pending_encoder_assignment(state, new_encoder, connector)) {
254 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
255 connector->base.id,
256 connector->name);
257 return -EINVAL;
258 }
259
Daniel Vetter623369e2014-09-16 17:50:47 +0200260 encoder_crtc = get_current_crtc_for_encoder(state->dev,
261 new_encoder);
262
263 if (encoder_crtc) {
264 ret = steal_encoder(state, new_encoder, encoder_crtc);
265 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100266 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
267 connector->base.id,
268 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200269 return ret;
270 }
271 }
272
Daniel Vetter6ea76f3c2015-08-03 17:24:11 +0200273 if (WARN_ON(!connector_state->crtc))
274 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200275
Daniel Vetter623369e2014-09-16 17:50:47 +0200276 connector_state->best_encoder = new_encoder;
277 idx = drm_crtc_index(connector_state->crtc);
278
279 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200280 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200281
Daniel Vetter17a38d92015-02-22 12:24:16 +0100282 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
283 connector->base.id,
284 connector->name,
285 new_encoder->base.id,
286 new_encoder->name,
287 connector_state->crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200288
289 return 0;
290}
291
292static int
293mode_fixup(struct drm_atomic_state *state)
294{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300295 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200296 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300297 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200298 struct drm_connector_state *conn_state;
299 int i;
300 bool ret;
301
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300302 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200303 if (!crtc_state->mode_changed &&
304 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200305 continue;
306
307 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
308 }
309
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300310 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200311 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200312 struct drm_encoder *encoder;
313
Daniel Vetter623369e2014-09-16 17:50:47 +0200314 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
315
316 if (!conn_state->crtc || !conn_state->best_encoder)
317 continue;
318
319 crtc_state =
320 state->crtc_states[drm_crtc_index(conn_state->crtc)];
321
322 /*
323 * Each encoder has at most one connector (since we always steal
324 * it away), so we won't call ->mode_fixup twice.
325 */
326 encoder = conn_state->best_encoder;
327 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300328 if (!funcs)
329 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200330
Archit Taneja862e6862015-05-21 11:03:16 +0530331 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
332 &crtc_state->adjusted_mode);
333 if (!ret) {
334 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
335 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200336 }
337
Thierry Reding4cd4df82014-12-03 16:44:34 +0100338 if (funcs->atomic_check) {
339 ret = funcs->atomic_check(encoder, crtc_state,
340 conn_state);
341 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100342 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
343 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100344 return ret;
345 }
Inki Dae84524912015-08-11 21:23:49 +0900346 } else if (funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100347 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
348 &crtc_state->adjusted_mode);
349 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100350 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
351 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100352 return -EINVAL;
353 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200354 }
355 }
356
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300357 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200358 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200359
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200360 if (!crtc_state->mode_changed &&
361 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200362 continue;
363
364 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300365 if (!funcs->mode_fixup)
366 continue;
367
Daniel Vetter623369e2014-09-16 17:50:47 +0200368 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
369 &crtc_state->adjusted_mode);
370 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100371 DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
372 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200373 return -EINVAL;
374 }
375 }
376
377 return 0;
378}
379
Daniel Vetterd9b13622014-11-26 16:57:41 +0100380/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800381 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100382 * @dev: DRM device
383 * @state: the driver state object
384 *
385 * Check the state object to see if the requested state is physically possible.
386 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200387 * update and adds any additional connectors needed for full modesets and calls
388 * down into ->mode_fixup functions of the driver backend.
389 *
390 * crtc_state->mode_changed is set when the input mode is changed.
391 * crtc_state->connectors_changed is set when a connector is added or
392 * removed from the crtc.
393 * crtc_state->active_changed is set when crtc_state->active changes,
394 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100395 *
396 * IMPORTANT:
397 *
398 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
399 * plane update can't be done without a full modeset) _must_ call this function
400 * afterwards after that change. It is permitted to call this function multiple
401 * times for the same update, e.g. when the ->atomic_check functions depend upon
402 * the adjusted dotclock for fifo space allocation and watermark computation.
403 *
404 * RETURNS
405 * Zero for success or -errno
406 */
407int
Rob Clark934ce1c2014-11-19 16:41:33 -0500408drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200409 struct drm_atomic_state *state)
410{
Daniel Vetter623369e2014-09-16 17:50:47 +0200411 struct drm_crtc *crtc;
412 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300413 struct drm_connector *connector;
414 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200415 int i, ret;
416
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300417 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200418 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100419 DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
420 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200421 crtc_state->mode_changed = true;
422 }
423
424 if (crtc->state->enable != crtc_state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100425 DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
426 crtc->base.id);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200427
428 /*
429 * For clarity this assignment is done here, but
430 * enable == 0 is only true when there are no
431 * connectors and a NULL mode.
432 *
433 * The other way around is true as well. enable != 0
434 * iff connectors are attached and a mode is set.
435 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200436 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200437 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200438 }
439 }
440
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300441 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200442 /*
443 * This only sets crtc->mode_changed for routing changes,
444 * drivers must set crtc->mode_changed themselves when connector
445 * properties need to be updated.
446 */
447 ret = update_connector_routing(state, i);
448 if (ret)
449 return ret;
450 }
451
452 /*
453 * After all the routing has been prepared we need to add in any
454 * connector which is itself unchanged, but who's crtc changes it's
455 * configuration. This must be done before calling mode_fixup in case a
456 * crtc only changed its mode but has the same set of connectors.
457 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300458 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200459 int num_connectors;
460
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100461 /*
462 * We must set ->active_changed after walking connectors for
463 * otherwise an update that only changes active would result in
464 * a full modeset because update_connector_routing force that.
465 */
466 if (crtc->state->active != crtc_state->active) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100467 DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
468 crtc->base.id);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100469 crtc_state->active_changed = true;
470 }
471
Daniel Vetter2465ff62015-06-18 09:58:55 +0200472 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100473 continue;
474
Daniel Vetter17a38d92015-02-22 12:24:16 +0100475 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
476 crtc->base.id,
477 crtc_state->enable ? 'y' : 'n',
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100478 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200479
480 ret = drm_atomic_add_affected_connectors(state, crtc);
481 if (ret != 0)
482 return ret;
483
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200484 ret = drm_atomic_add_affected_planes(state, crtc);
485 if (ret != 0)
486 return ret;
487
Daniel Vetter623369e2014-09-16 17:50:47 +0200488 num_connectors = drm_atomic_connectors_for_crtc(state,
489 crtc);
490
491 if (crtc_state->enable != !!num_connectors) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100492 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
493 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200494
495 return -EINVAL;
496 }
497 }
498
499 return mode_fixup(state);
500}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100501EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200502
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100503/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800504 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100505 * @dev: DRM device
506 * @state: the driver state object
507 *
508 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100509 * This does all the plane update related checks using by calling into the
510 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100511 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200512 * It also sets crtc_state->planes_changed to indicate that a crtc has
513 * updated planes.
514 *
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100515 * RETURNS
516 * Zero for success or -errno
517 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100518int
519drm_atomic_helper_check_planes(struct drm_device *dev,
520 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100521{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300522 struct drm_crtc *crtc;
523 struct drm_crtc_state *crtc_state;
524 struct drm_plane *plane;
525 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100526 int i, ret = 0;
527
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300528 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200529 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100530
531 funcs = plane->helper_private;
532
533 drm_atomic_helper_plane_changed(state, plane_state, plane);
534
535 if (!funcs || !funcs->atomic_check)
536 continue;
537
538 ret = funcs->atomic_check(plane, plane_state);
539 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100540 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
541 plane->base.id);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100542 return ret;
543 }
544 }
545
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300546 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200547 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100548
549 funcs = crtc->helper_private;
550
551 if (!funcs || !funcs->atomic_check)
552 continue;
553
554 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
555 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100556 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
557 crtc->base.id);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100558 return ret;
559 }
560 }
561
Daniel Vetterd9b13622014-11-26 16:57:41 +0100562 return ret;
563}
564EXPORT_SYMBOL(drm_atomic_helper_check_planes);
565
566/**
567 * drm_atomic_helper_check - validate state object
568 * @dev: DRM device
569 * @state: the driver state object
570 *
571 * Check the state object to see if the requested state is physically possible.
572 * Only crtcs and planes have check callbacks, so for any additional (global)
573 * checking that a driver needs it can simply wrap that around this function.
574 * Drivers without such needs can directly use this as their ->atomic_check()
575 * callback.
576 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100577 * This just wraps the two parts of the state checking for planes and modeset
578 * state in the default order: First it calls drm_atomic_helper_check_modeset()
579 * and then drm_atomic_helper_check_planes(). The assumption is that the
580 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
581 * e.g. properly compute watermarks.
582 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100583 * RETURNS
584 * Zero for success or -errno
585 */
586int drm_atomic_helper_check(struct drm_device *dev,
587 struct drm_atomic_state *state)
588{
589 int ret;
590
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100591 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100592 if (ret)
593 return ret;
594
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100595 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500596 if (ret)
597 return ret;
598
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100599 return ret;
600}
601EXPORT_SYMBOL(drm_atomic_helper_check);
602
Daniel Vetter623369e2014-09-16 17:50:47 +0200603static void
604disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
605{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300606 struct drm_connector *connector;
607 struct drm_connector_state *old_conn_state;
608 struct drm_crtc *crtc;
609 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200610 int i;
611
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300612 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200613 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200614 struct drm_encoder *encoder;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100615 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200616
Daniel Vetter623369e2014-09-16 17:50:47 +0200617 /* Shut down everything that's in the changeset and currently
618 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300619 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200620 continue;
621
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100622 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
623
Daniel Vetter4218a322015-03-26 22:18:40 +0100624 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200625 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100626 continue;
627
Rob Clark46df9ad2014-11-20 15:40:36 -0500628 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200629
Rob Clark46df9ad2014-11-20 15:40:36 -0500630 /* We shouldn't get this far if we didn't previously have
631 * an encoder.. but WARN_ON() rather than explode.
632 */
633 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200634 continue;
635
636 funcs = encoder->helper_private;
637
Daniel Vetter17a38d92015-02-22 12:24:16 +0100638 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
639 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100640
Daniel Vetter623369e2014-09-16 17:50:47 +0200641 /*
642 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800643 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200644 */
Archit Taneja862e6862015-05-21 11:03:16 +0530645 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200646
647 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100648 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200649 funcs->prepare(encoder);
650 else if (funcs->disable)
651 funcs->disable(encoder);
652 else
653 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
654
Archit Taneja862e6862015-05-21 11:03:16 +0530655 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200656 }
657
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300658 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200659 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200660
661 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200662 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100663 continue;
664
665 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200666 continue;
667
668 funcs = crtc->helper_private;
669
Daniel Vetter17a38d92015-02-22 12:24:16 +0100670 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
671 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100672
673
Daniel Vetter623369e2014-09-16 17:50:47 +0200674 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100675 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200676 funcs->prepare(crtc);
677 else if (funcs->disable)
678 funcs->disable(crtc);
679 else
680 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
681 }
682}
683
Daniel Vetter4c18d302015-05-12 15:27:37 +0200684/**
685 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
686 * @dev: DRM device
687 * @old_state: atomic state object with old state structures
688 *
689 * This function updates all the various legacy modeset state pointers in
690 * connectors, encoders and crtcs. It also updates the timestamping constants
691 * used for precise vblank timestamps by calling
692 * drm_calc_timestamping_constants().
693 *
694 * Drivers can use this for building their own atomic commit if they don't have
695 * a pure helper-based modeset implementation.
696 */
697void
698drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
699 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200700{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300701 struct drm_connector *connector;
702 struct drm_connector_state *old_conn_state;
703 struct drm_crtc *crtc;
704 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200705 int i;
706
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200707 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300708 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200709 if (connector->encoder) {
710 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200711
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200712 connector->encoder->crtc = NULL;
713 connector->encoder = NULL;
714 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200715
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200716 crtc = connector->state->crtc;
717 if ((!crtc && old_conn_state->crtc) ||
718 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
719 struct drm_property *dpms_prop =
720 dev->mode_config.dpms_property;
721 int mode = DRM_MODE_DPMS_OFF;
722
723 if (crtc && crtc->state->active)
724 mode = DRM_MODE_DPMS_ON;
725
726 connector->dpms = mode;
727 drm_object_property_set_value(&connector->base,
728 dpms_prop, mode);
729 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200730 }
731
732 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300733 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
734 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200735 continue;
736
737 if (WARN_ON(!connector->state->best_encoder))
738 continue;
739
740 connector->encoder = connector->state->best_encoder;
741 connector->encoder->crtc = connector->state->crtc;
742 }
743
744 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300745 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200746 struct drm_plane *primary = crtc->primary;
747
Daniel Vetter623369e2014-09-16 17:50:47 +0200748 crtc->mode = crtc->state->mode;
749 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200750
751 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
752 primary->state->crtc == crtc) {
753 crtc->x = primary->state->src_x >> 16;
754 crtc->y = primary->state->src_y >> 16;
755 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200756
757 if (crtc->state->enable)
758 drm_calc_timestamping_constants(crtc,
759 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200760 }
761}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200762EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200763
764static void
765crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
766{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300767 struct drm_crtc *crtc;
768 struct drm_crtc_state *old_crtc_state;
769 struct drm_connector *connector;
770 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200771 int i;
772
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300773 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200774 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200775
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300776 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200777 continue;
778
779 funcs = crtc->helper_private;
780
Daniel Vetterc982bd92015-02-22 12:24:20 +0100781 if (crtc->state->enable && funcs->mode_set_nofb) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100782 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
783 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100784
Daniel Vetter623369e2014-09-16 17:50:47 +0200785 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100786 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200787 }
788
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300789 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200790 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200791 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200792 struct drm_encoder *encoder;
793 struct drm_display_mode *mode, *adjusted_mode;
794
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300795 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200796 continue;
797
798 encoder = connector->state->best_encoder;
799 funcs = encoder->helper_private;
800 new_crtc_state = connector->state->crtc->state;
801 mode = &new_crtc_state->mode;
802 adjusted_mode = &new_crtc_state->adjusted_mode;
803
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100804 if (!new_crtc_state->mode_changed)
805 continue;
806
Daniel Vetter17a38d92015-02-22 12:24:16 +0100807 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
808 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100809
Daniel Vetter623369e2014-09-16 17:50:47 +0200810 /*
811 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800812 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200813 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100814 if (funcs->mode_set)
815 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200816
Archit Taneja862e6862015-05-21 11:03:16 +0530817 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200818 }
819}
820
821/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100822 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200823 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100824 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200825 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100826 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200827 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100828 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300829 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100830 * drm_atomic_helper_commit_planes(), which is what the default commit function
831 * does. But drivers with different needs can group the modeset commits together
832 * and do the plane commits at the end. This is useful for drivers doing runtime
833 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200834 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100835void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
836 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200837{
Laurent Pincharta072f802015-02-22 12:24:18 +0100838 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200839
840 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
841
Laurent Pincharta072f802015-02-22 12:24:18 +0100842 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200843}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100844EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200845
846/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100847 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200848 * @dev: DRM device
849 * @old_state: atomic state object with old state structures
850 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100851 * This function enables all the outputs with the new configuration which had to
852 * be turned off for the update.
853 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300854 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100855 * drm_atomic_helper_commit_planes(), which is what the default commit function
856 * does. But drivers with different needs can group the modeset commits together
857 * and do the plane commits at the end. This is useful for drivers doing runtime
858 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200859 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100860void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
861 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200862{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300863 struct drm_crtc *crtc;
864 struct drm_crtc_state *old_crtc_state;
865 struct drm_connector *connector;
866 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200867 int i;
868
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300869 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200870 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200871
872 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200873 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100874 continue;
875
876 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200877 continue;
878
879 funcs = crtc->helper_private;
880
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100881 if (crtc->state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100882 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
883 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100884
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100885 if (funcs->enable)
886 funcs->enable(crtc);
887 else
888 funcs->commit(crtc);
889 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200890 }
891
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300892 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200893 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200894 struct drm_encoder *encoder;
895
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300896 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200897 continue;
898
Daniel Vetter4218a322015-03-26 22:18:40 +0100899 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200900 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100901 continue;
902
Daniel Vetter623369e2014-09-16 17:50:47 +0200903 encoder = connector->state->best_encoder;
904 funcs = encoder->helper_private;
905
Daniel Vetter17a38d92015-02-22 12:24:16 +0100906 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
907 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100908
Daniel Vetter623369e2014-09-16 17:50:47 +0200909 /*
910 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800911 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200912 */
Archit Taneja862e6862015-05-21 11:03:16 +0530913 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200914
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100915 if (funcs->enable)
916 funcs->enable(encoder);
917 else
918 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200919
Archit Taneja862e6862015-05-21 11:03:16 +0530920 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200921 }
922}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100923EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200924
Daniel Vettere2330f02014-10-29 11:34:56 +0100925static void wait_for_fences(struct drm_device *dev,
926 struct drm_atomic_state *state)
927{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300928 struct drm_plane *plane;
929 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +0100930 int i;
931
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300932 for_each_plane_in_state(state, plane, plane_state, i) {
933 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +0100934 continue;
935
936 WARN_ON(!plane->state->fb);
937
938 fence_wait(plane->state->fence, false);
939 fence_put(plane->state->fence);
940 plane->state->fence = NULL;
941 }
942}
943
Daniel Vetterab58e332014-11-24 20:42:42 +0100944static bool framebuffer_changed(struct drm_device *dev,
945 struct drm_atomic_state *old_state,
946 struct drm_crtc *crtc)
947{
948 struct drm_plane *plane;
949 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +0100950 int i;
951
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300952 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +0100953 if (plane->state->crtc != crtc &&
954 old_plane_state->crtc != crtc)
955 continue;
956
957 if (plane->state->fb != old_plane_state->fb)
958 return true;
959 }
960
961 return false;
962}
963
Rob Clark5ee32292014-11-11 19:38:59 -0500964/**
965 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
966 * @dev: DRM device
967 * @old_state: atomic state object with old state structures
968 *
969 * Helper to, after atomic commit, wait for vblanks on all effected
970 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100971 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
972 * framebuffers have actually changed to optimize for the legacy cursor and
973 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500974 */
975void
976drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
977 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200978{
979 struct drm_crtc *crtc;
980 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200981 int i, ret;
982
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300983 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200984 /* No one cares about the old state, so abuse it for tracking
985 * and store whether we hold a vblank reference (and should do a
986 * vblank wait) in the ->enable boolean. */
987 old_crtc_state->enable = false;
988
989 if (!crtc->state->enable)
990 continue;
991
Daniel Vetterf02ad902015-01-22 16:36:23 +0100992 /* Legacy cursor ioctls are completely unsynced, and userspace
993 * relies on that (by doing tons of cursor updates). */
994 if (old_state->legacy_cursor_update)
995 continue;
996
Daniel Vetterab58e332014-11-24 20:42:42 +0100997 if (!framebuffer_changed(dev, old_state, crtc))
998 continue;
999
Daniel Vetter623369e2014-09-16 17:50:47 +02001000 ret = drm_crtc_vblank_get(crtc);
1001 if (ret != 0)
1002 continue;
1003
1004 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +02001005 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001006 }
1007
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001008 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1009 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +02001010 continue;
1011
1012 ret = wait_event_timeout(dev->vblank[i].queue,
1013 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001014 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001015 msecs_to_jiffies(50));
1016
1017 drm_crtc_vblank_put(crtc);
1018 }
1019}
Rob Clark5ee32292014-11-11 19:38:59 -05001020EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001021
1022/**
1023 * drm_atomic_helper_commit - commit validated state object
1024 * @dev: DRM device
1025 * @state: the driver state object
1026 * @async: asynchronous commit
1027 *
1028 * This function commits a with drm_atomic_helper_check() pre-validated state
1029 * object. This can still fail when e.g. the framebuffer reservation fails. For
1030 * now this doesn't implement asynchronous commits.
1031 *
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001032 * Note that right now this function does not support async commits, and hence
1033 * driver writers must implement their own version for now. Also note that the
1034 * default ordering of how the various stages are called is to match the legacy
1035 * modeset helper library closest. One peculiarity of that is that it doesn't
1036 * mesh well with runtime PM at all.
1037 *
1038 * For drivers supporting runtime PM the recommended sequence is
1039 *
1040 * drm_atomic_helper_commit_modeset_disables(dev, state);
1041 *
1042 * drm_atomic_helper_commit_modeset_enables(dev, state);
1043 *
1044 * drm_atomic_helper_commit_planes(dev, state, true);
1045 *
1046 * See the kerneldoc entries for these three functions for more details.
1047 *
Daniel Vetter623369e2014-09-16 17:50:47 +02001048 * RETURNS
1049 * Zero for success or -errno.
1050 */
1051int drm_atomic_helper_commit(struct drm_device *dev,
1052 struct drm_atomic_state *state,
1053 bool async)
1054{
1055 int ret;
1056
1057 if (async)
1058 return -EBUSY;
1059
1060 ret = drm_atomic_helper_prepare_planes(dev, state);
1061 if (ret)
1062 return ret;
1063
1064 /*
1065 * This is the point of no return - everything below never fails except
1066 * when the hw goes bonghits. Which means we can commit the new state on
1067 * the software side now.
1068 */
1069
1070 drm_atomic_helper_swap_state(dev, state);
1071
1072 /*
1073 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001074 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001075 * that the asynchronous work has either been cancelled (if the driver
1076 * supports it, which at least requires that the framebuffers get
1077 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1078 * before the new state gets committed on the software side with
1079 * drm_atomic_helper_swap_state().
1080 *
1081 * This scheme allows new atomic state updates to be prepared and
1082 * checked in parallel to the asynchronous completion of the previous
1083 * update. Which is important since compositors need to figure out the
1084 * composition of the next frame right after having submitted the
1085 * current layout.
1086 */
1087
Daniel Vettere2330f02014-10-29 11:34:56 +01001088 wait_for_fences(dev, state);
1089
Daniel Vetter1af434a2015-02-22 12:24:19 +01001090 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001091
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001092 drm_atomic_helper_commit_planes(dev, state, false);
Daniel Vetter623369e2014-09-16 17:50:47 +02001093
Daniel Vetter1af434a2015-02-22 12:24:19 +01001094 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001095
Rob Clark5ee32292014-11-11 19:38:59 -05001096 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001097
1098 drm_atomic_helper_cleanup_planes(dev, state);
1099
1100 drm_atomic_state_free(state);
1101
1102 return 0;
1103}
1104EXPORT_SYMBOL(drm_atomic_helper_commit);
1105
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001106/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001107 * DOC: implementing async commit
1108 *
1109 * For now the atomic helpers don't support async commit directly. If there is
1110 * real need it could be added though, using the dma-buf fence infrastructure
1111 * for generic synchronization with outstanding rendering.
1112 *
1113 * For now drivers have to implement async commit themselves, with the following
1114 * sequence being the recommended one:
1115 *
1116 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1117 * which commit needs to call which can fail, so we want to run it first and
1118 * synchronously.
1119 *
1120 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1121 * might be affected the new state update. This can be done by either cancelling
1122 * or flushing the work items, depending upon whether the driver can deal with
1123 * cancelled updates. Note that it is important to ensure that the framebuffer
1124 * cleanup is still done when cancelling.
1125 *
1126 * For sufficient parallelism it is recommended to have a work item per crtc
1127 * (for updates which don't touch global state) and a global one. Then we only
1128 * need to synchronize with the crtc work items for changed crtcs and the global
1129 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1130 *
1131 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001132 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001133 * locks means concurrent callers never see inconsistent state. And doing this
1134 * while it's guaranteed that no relevant async worker runs means that async
1135 * workers do not need grab any locks. Actually they must not grab locks, for
1136 * otherwise the work flushing will deadlock.
1137 *
1138 * 4. Schedule a work item to do all subsequent steps, using the split-out
1139 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1140 * then cleaning up the framebuffers after the old framebuffer is no longer
1141 * being displayed.
1142 */
1143
1144/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001145 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001146 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001147 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001148 *
1149 * This function prepares plane state, specifically framebuffers, for the new
1150 * configuration. If any failure is encountered this function will call
1151 * ->cleanup_fb on any already successfully prepared framebuffer.
1152 *
1153 * Returns:
1154 * 0 on success, negative error code on failure.
1155 */
1156int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1157 struct drm_atomic_state *state)
1158{
1159 int nplanes = dev->mode_config.num_total_plane;
1160 int ret, i;
1161
1162 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001163 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001164 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001165 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001166
1167 if (!plane)
1168 continue;
1169
1170 funcs = plane->helper_private;
1171
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001172 if (funcs->prepare_fb) {
1173 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001174 if (ret)
1175 goto fail;
1176 }
1177 }
1178
1179 return 0;
1180
1181fail:
1182 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001183 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001184 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001185 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001186
1187 if (!plane)
1188 continue;
1189
1190 funcs = plane->helper_private;
1191
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001192 if (funcs->cleanup_fb)
1193 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001194
1195 }
1196
1197 return ret;
1198}
1199EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1200
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001201bool plane_crtc_active(struct drm_plane_state *state)
1202{
1203 return state->crtc && state->crtc->state->active;
1204}
1205
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001206/**
1207 * drm_atomic_helper_commit_planes - commit plane state
1208 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001209 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001210 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001211 *
1212 * This function commits the new plane state using the plane and atomic helper
1213 * functions for planes and crtcs. It assumes that the atomic state has already
1214 * been pushed into the relevant object state pointers, since this step can no
1215 * longer fail.
1216 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001217 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001218 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001219 *
1220 * Note that this function does all plane updates across all CRTCs in one step.
1221 * If the hardware can't support this approach look at
1222 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001223 *
1224 * Plane parameters can be updated by applications while the associated CRTC is
1225 * disabled. The DRM/KMS core will store the parameters in the plane state,
1226 * which will be available to the driver when the CRTC is turned on. As a result
1227 * most drivers don't need to be immediately notified of plane updates for a
1228 * disabled CRTC.
1229 *
1230 * Unless otherwise needed, drivers are advised to set the @active_only
1231 * parameters to true in order not to receive plane update notifications related
1232 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1233 * driver code when the driver and/or hardware can't or just don't need to deal
1234 * with updates on disabled CRTCs, for example when supporting runtime PM.
1235 *
1236 * The drm_atomic_helper_commit() default implementation only sets @active_only
1237 * to false to most closely match the behaviour of the legacy helpers. This should
1238 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001239 */
1240void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001241 struct drm_atomic_state *old_state,
1242 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001243{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001244 struct drm_crtc *crtc;
1245 struct drm_crtc_state *old_crtc_state;
1246 struct drm_plane *plane;
1247 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001248 int i;
1249
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001250 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001251 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001252
1253 funcs = crtc->helper_private;
1254
1255 if (!funcs || !funcs->atomic_begin)
1256 continue;
1257
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001258 if (active_only && !crtc->state->active)
1259 continue;
1260
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001261 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001262 }
1263
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001264 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001265 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001266 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001267
1268 funcs = plane->helper_private;
1269
Thierry Reding3cad4b62014-11-25 13:05:12 +01001270 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001271 continue;
1272
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001273 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1274
1275 if (active_only) {
1276 /*
1277 * Skip planes related to inactive CRTCs. If the plane
1278 * is enabled use the state of the current CRTC. If the
1279 * plane is being disabled use the state of the old
1280 * CRTC to avoid skipping planes being disabled on an
1281 * active CRTC.
1282 */
1283 if (!disabling && !plane_crtc_active(plane->state))
1284 continue;
1285 if (disabling && !plane_crtc_active(old_plane_state))
1286 continue;
1287 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001288
Thierry Reding407b8bd2014-11-20 12:05:50 +01001289 /*
1290 * Special-case disabling the plane if drivers support it.
1291 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001292 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001293 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001294 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001295 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001296 }
1297
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001298 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001299 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001300
1301 funcs = crtc->helper_private;
1302
1303 if (!funcs || !funcs->atomic_flush)
1304 continue;
1305
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001306 if (active_only && !crtc->state->active)
1307 continue;
1308
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001309 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001310 }
1311}
1312EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1313
1314/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001315 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1316 * @old_crtc_state: atomic state object with the old crtc state
1317 *
1318 * This function commits the new plane state using the plane and atomic helper
1319 * functions for planes on the specific crtc. It assumes that the atomic state
1320 * has already been pushed into the relevant object state pointers, since this
1321 * step can no longer fail.
1322 *
1323 * This function is useful when plane updates should be done crtc-by-crtc
1324 * instead of one global step like drm_atomic_helper_commit_planes() does.
1325 *
1326 * This function can only be savely used when planes are not allowed to move
1327 * between different CRTCs because this function doesn't handle inter-CRTC
1328 * depencies. Callers need to ensure that either no such depencies exist,
1329 * resolve them through ordering of commit calls or through some other means.
1330 */
1331void
1332drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1333{
1334 const struct drm_crtc_helper_funcs *crtc_funcs;
1335 struct drm_crtc *crtc = old_crtc_state->crtc;
1336 struct drm_atomic_state *old_state = old_crtc_state->state;
1337 struct drm_plane *plane;
1338 unsigned plane_mask;
1339
1340 plane_mask = old_crtc_state->plane_mask;
1341 plane_mask |= crtc->state->plane_mask;
1342
1343 crtc_funcs = crtc->helper_private;
1344 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001345 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001346
1347 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1348 struct drm_plane_state *old_plane_state =
1349 drm_atomic_get_existing_plane_state(old_state, plane);
1350 const struct drm_plane_helper_funcs *plane_funcs;
1351
1352 plane_funcs = plane->helper_private;
1353
1354 if (!old_plane_state || !plane_funcs)
1355 continue;
1356
1357 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1358
1359 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1360 plane_funcs->atomic_disable)
1361 plane_funcs->atomic_disable(plane, old_plane_state);
1362 else if (plane->state->crtc ||
1363 drm_atomic_plane_disabling(plane, old_plane_state))
1364 plane_funcs->atomic_update(plane, old_plane_state);
1365 }
1366
1367 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001368 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001369}
1370EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1371
1372/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001373 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1374 * @crtc: CRTC
1375 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1376 *
1377 * Disables all planes associated with the given CRTC. This can be
1378 * used for instance in the CRTC helper disable callback to disable
1379 * all planes before shutting down the display pipeline.
1380 *
1381 * If the atomic-parameter is set the function calls the CRTC's
1382 * atomic_begin hook before and atomic_flush hook after disabling the
1383 * planes.
1384 *
1385 * It is a bug to call this function without having implemented the
1386 * ->atomic_disable() plane hook.
1387 */
1388void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1389 bool atomic)
1390{
1391 const struct drm_crtc_helper_funcs *crtc_funcs =
1392 crtc->helper_private;
1393 struct drm_plane *plane;
1394
1395 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1396 crtc_funcs->atomic_begin(crtc, NULL);
1397
1398 drm_for_each_plane(plane, crtc->dev) {
1399 const struct drm_plane_helper_funcs *plane_funcs =
1400 plane->helper_private;
1401
1402 if (plane->state->crtc != crtc || !plane_funcs)
1403 continue;
1404
1405 WARN_ON(!plane_funcs->atomic_disable);
1406 if (plane_funcs->atomic_disable)
1407 plane_funcs->atomic_disable(plane, NULL);
1408 }
1409
1410 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1411 crtc_funcs->atomic_flush(crtc, NULL);
1412}
1413EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1414
1415/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001416 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1417 * @dev: DRM device
1418 * @old_state: atomic state object with old state structures
1419 *
1420 * This function cleans up plane state, specifically framebuffers, from the old
1421 * configuration. Hence the old configuration must be perserved in @old_state to
1422 * be able to call this function.
1423 *
1424 * This function must also be called on the new state when the atomic update
1425 * fails at any point after calling drm_atomic_helper_prepare_planes().
1426 */
1427void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1428 struct drm_atomic_state *old_state)
1429{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001430 struct drm_plane *plane;
1431 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001432 int i;
1433
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001434 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001435 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001436
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001437 funcs = plane->helper_private;
1438
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001439 if (funcs->cleanup_fb)
1440 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001441 }
1442}
1443EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1444
1445/**
1446 * drm_atomic_helper_swap_state - store atomic state into current sw state
1447 * @dev: DRM device
1448 * @state: atomic state
1449 *
1450 * This function stores the atomic state into the current state pointers in all
1451 * driver objects. It should be called after all failing steps have been done
1452 * and succeeded, but before the actual hardware state is committed.
1453 *
1454 * For cleanup and error recovery the current state for all changed objects will
1455 * be swaped into @state.
1456 *
1457 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1458 *
1459 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1460 *
1461 * 2. Do any other steps that might fail.
1462 *
1463 * 3. Put the staged state into the current state pointers with this function.
1464 *
1465 * 4. Actually commit the hardware state.
1466 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001467 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001468 * contains the old state. Also do any other cleanup required with that state.
1469 */
1470void drm_atomic_helper_swap_state(struct drm_device *dev,
1471 struct drm_atomic_state *state)
1472{
1473 int i;
1474
1475 for (i = 0; i < dev->mode_config.num_connector; i++) {
1476 struct drm_connector *connector = state->connectors[i];
1477
1478 if (!connector)
1479 continue;
1480
1481 connector->state->state = state;
1482 swap(state->connector_states[i], connector->state);
1483 connector->state->state = NULL;
1484 }
1485
1486 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1487 struct drm_crtc *crtc = state->crtcs[i];
1488
1489 if (!crtc)
1490 continue;
1491
1492 crtc->state->state = state;
1493 swap(state->crtc_states[i], crtc->state);
1494 crtc->state->state = NULL;
1495 }
1496
1497 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1498 struct drm_plane *plane = state->planes[i];
1499
1500 if (!plane)
1501 continue;
1502
1503 plane->state->state = state;
1504 swap(state->plane_states[i], plane->state);
1505 plane->state->state = NULL;
1506 }
1507}
1508EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001509
1510/**
1511 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1512 * @plane: plane object to update
1513 * @crtc: owning CRTC of owning plane
1514 * @fb: framebuffer to flip onto plane
1515 * @crtc_x: x offset of primary plane on crtc
1516 * @crtc_y: y offset of primary plane on crtc
1517 * @crtc_w: width of primary plane rectangle on crtc
1518 * @crtc_h: height of primary plane rectangle on crtc
1519 * @src_x: x offset of @fb for panning
1520 * @src_y: y offset of @fb for panning
1521 * @src_w: width of source rectangle in @fb
1522 * @src_h: height of source rectangle in @fb
1523 *
1524 * Provides a default plane update handler using the atomic driver interface.
1525 *
1526 * RETURNS:
1527 * Zero on success, error code on failure
1528 */
1529int drm_atomic_helper_update_plane(struct drm_plane *plane,
1530 struct drm_crtc *crtc,
1531 struct drm_framebuffer *fb,
1532 int crtc_x, int crtc_y,
1533 unsigned int crtc_w, unsigned int crtc_h,
1534 uint32_t src_x, uint32_t src_y,
1535 uint32_t src_w, uint32_t src_h)
1536{
1537 struct drm_atomic_state *state;
1538 struct drm_plane_state *plane_state;
1539 int ret = 0;
1540
1541 state = drm_atomic_state_alloc(plane->dev);
1542 if (!state)
1543 return -ENOMEM;
1544
1545 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1546retry:
1547 plane_state = drm_atomic_get_plane_state(state, plane);
1548 if (IS_ERR(plane_state)) {
1549 ret = PTR_ERR(plane_state);
1550 goto fail;
1551 }
1552
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001553 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001554 if (ret != 0)
1555 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001556 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001557 plane_state->crtc_x = crtc_x;
1558 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001559 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001560 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001561 plane_state->src_x = src_x;
1562 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001563 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001564 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001565
Daniel Vetter3671c582015-05-04 15:40:52 +02001566 if (plane == crtc->cursor)
1567 state->legacy_cursor_update = true;
1568
Daniel Vetter042652e2014-07-27 13:46:52 +02001569 ret = drm_atomic_commit(state);
1570 if (ret != 0)
1571 goto fail;
1572
1573 /* Driver takes ownership of state on successful commit. */
1574 return 0;
1575fail:
1576 if (ret == -EDEADLK)
1577 goto backoff;
1578
1579 drm_atomic_state_free(state);
1580
1581 return ret;
1582backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001583 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001584 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001585
1586 /*
1587 * Someone might have exchanged the framebuffer while we dropped locks
1588 * in the backoff code. We need to fix up the fb refcount tracking the
1589 * core does for us.
1590 */
1591 plane->old_fb = plane->fb;
1592
1593 goto retry;
1594}
1595EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1596
1597/**
1598 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1599 * @plane: plane to disable
1600 *
1601 * Provides a default plane disable handler using the atomic driver interface.
1602 *
1603 * RETURNS:
1604 * Zero on success, error code on failure
1605 */
1606int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1607{
1608 struct drm_atomic_state *state;
1609 struct drm_plane_state *plane_state;
1610 int ret = 0;
1611
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001612 /*
1613 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1614 * acquire context. The real fix will be to wire the acquire ctx through
1615 * everywhere we need it, but meanwhile prevent chaos by just skipping
1616 * this noop. The critical case is the cursor ioctls which a) only grab
1617 * crtc/cursor-plane locks (so we need the crtc to get at the right
1618 * acquire context) and b) can try to disable the plane multiple times.
1619 */
1620 if (!plane->crtc)
1621 return 0;
1622
Daniel Vetter042652e2014-07-27 13:46:52 +02001623 state = drm_atomic_state_alloc(plane->dev);
1624 if (!state)
1625 return -ENOMEM;
1626
1627 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1628retry:
1629 plane_state = drm_atomic_get_plane_state(state, plane);
1630 if (IS_ERR(plane_state)) {
1631 ret = PTR_ERR(plane_state);
1632 goto fail;
1633 }
1634
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01001635 if (plane_state->crtc && (plane == plane->crtc->cursor))
1636 plane_state->state->legacy_cursor_update = true;
1637
Rob Clarkbbb1e522015-08-25 15:35:58 -04001638 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001639 if (ret != 0)
1640 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01001641
Daniel Vetter042652e2014-07-27 13:46:52 +02001642 ret = drm_atomic_commit(state);
1643 if (ret != 0)
1644 goto fail;
1645
1646 /* Driver takes ownership of state on successful commit. */
1647 return 0;
1648fail:
1649 if (ret == -EDEADLK)
1650 goto backoff;
1651
1652 drm_atomic_state_free(state);
1653
1654 return ret;
1655backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001656 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001657 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001658
1659 /*
1660 * Someone might have exchanged the framebuffer while we dropped locks
1661 * in the backoff code. We need to fix up the fb refcount tracking the
1662 * core does for us.
1663 */
1664 plane->old_fb = plane->fb;
1665
1666 goto retry;
1667}
1668EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1669
Rob Clarkbbb1e522015-08-25 15:35:58 -04001670/* just used from fb-helper and atomic-helper: */
1671int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1672 struct drm_plane_state *plane_state)
1673{
1674 int ret;
1675
1676 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1677 if (ret != 0)
1678 return ret;
1679
1680 drm_atomic_set_fb_for_plane(plane_state, NULL);
1681 plane_state->crtc_x = 0;
1682 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001683 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001684 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001685 plane_state->src_x = 0;
1686 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001687 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001688 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001689
Rob Clarkbbb1e522015-08-25 15:35:58 -04001690 return 0;
1691}
1692
Daniel Vetter042652e2014-07-27 13:46:52 +02001693static int update_output_state(struct drm_atomic_state *state,
1694 struct drm_mode_set *set)
1695{
1696 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001697 struct drm_crtc *crtc;
1698 struct drm_crtc_state *crtc_state;
1699 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001700 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001701 int ret, i, j;
1702
1703 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1704 state->acquire_ctx);
1705 if (ret)
1706 return ret;
1707
1708 /* First grab all affected connector/crtc states. */
1709 for (i = 0; i < set->num_connectors; i++) {
1710 conn_state = drm_atomic_get_connector_state(state,
1711 set->connectors[i]);
1712 if (IS_ERR(conn_state))
1713 return PTR_ERR(conn_state);
1714 }
1715
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001716 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001717 ret = drm_atomic_add_affected_connectors(state, crtc);
1718 if (ret)
1719 return ret;
1720 }
1721
1722 /* Then recompute connector->crtc links and crtc enabling state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001723 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001724 if (conn_state->crtc == set->crtc) {
1725 ret = drm_atomic_set_crtc_for_connector(conn_state,
1726 NULL);
1727 if (ret)
1728 return ret;
1729 }
1730
1731 for (j = 0; j < set->num_connectors; j++) {
1732 if (set->connectors[j] == connector) {
1733 ret = drm_atomic_set_crtc_for_connector(conn_state,
1734 set->crtc);
1735 if (ret)
1736 return ret;
1737 break;
1738 }
1739 }
1740 }
1741
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001742 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001743 /* Don't update ->enable for the CRTC in the set_config request,
1744 * since a mismatch would indicate a bug in the upper layers.
1745 * The actual modeset code later on will catch any
1746 * inconsistencies here. */
1747 if (crtc == set->crtc)
1748 continue;
1749
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001750 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1751 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1752 NULL);
1753 if (ret < 0)
1754 return ret;
1755
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001756 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001757 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001758 }
1759
1760 return 0;
1761}
1762
1763/**
1764 * drm_atomic_helper_set_config - set a new config from userspace
1765 * @set: mode set configuration
1766 *
1767 * Provides a default crtc set_config handler using the atomic driver interface.
1768 *
1769 * Returns:
1770 * Returns 0 on success, negative errno numbers on failure.
1771 */
1772int drm_atomic_helper_set_config(struct drm_mode_set *set)
1773{
1774 struct drm_atomic_state *state;
1775 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02001776 int ret = 0;
1777
1778 state = drm_atomic_state_alloc(crtc->dev);
1779 if (!state)
1780 return -ENOMEM;
1781
1782 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1783retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04001784 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01001785 if (ret != 0)
1786 goto fail;
1787
Daniel Vetter042652e2014-07-27 13:46:52 +02001788 ret = drm_atomic_commit(state);
1789 if (ret != 0)
1790 goto fail;
1791
1792 /* Driver takes ownership of state on successful commit. */
1793 return 0;
1794fail:
1795 if (ret == -EDEADLK)
1796 goto backoff;
1797
1798 drm_atomic_state_free(state);
1799
1800 return ret;
1801backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001802 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001803 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001804
1805 /*
1806 * Someone might have exchanged the framebuffer while we dropped locks
1807 * in the backoff code. We need to fix up the fb refcount tracking the
1808 * core does for us.
1809 */
1810 crtc->primary->old_fb = crtc->primary->fb;
1811
1812 goto retry;
1813}
1814EXPORT_SYMBOL(drm_atomic_helper_set_config);
1815
Rob Clarkbbb1e522015-08-25 15:35:58 -04001816/* just used from fb-helper and atomic-helper: */
1817int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1818 struct drm_atomic_state *state)
1819{
1820 struct drm_crtc_state *crtc_state;
1821 struct drm_plane_state *primary_state;
1822 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02001823 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001824 int ret;
1825
1826 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1827 if (IS_ERR(crtc_state))
1828 return PTR_ERR(crtc_state);
1829
1830 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1831 if (IS_ERR(primary_state))
1832 return PTR_ERR(primary_state);
1833
1834 if (!set->mode) {
1835 WARN_ON(set->fb);
1836 WARN_ON(set->num_connectors);
1837
1838 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1839 if (ret != 0)
1840 return ret;
1841
1842 crtc_state->active = false;
1843
1844 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1845 if (ret != 0)
1846 return ret;
1847
1848 drm_atomic_set_fb_for_plane(primary_state, NULL);
1849
1850 goto commit;
1851 }
1852
1853 WARN_ON(!set->fb);
1854 WARN_ON(!set->num_connectors);
1855
1856 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1857 if (ret != 0)
1858 return ret;
1859
1860 crtc_state->active = true;
1861
1862 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1863 if (ret != 0)
1864 return ret;
1865
Ville Syrjälä83926112015-11-16 17:02:34 +02001866 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1867
Rob Clarkbbb1e522015-08-25 15:35:58 -04001868 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1869 primary_state->crtc_x = 0;
1870 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02001871 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001872 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001873 primary_state->src_x = set->x << 16;
1874 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001875 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02001876 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001877 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001878 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02001879 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001880 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001881 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04001882
1883commit:
1884 ret = update_output_state(state, set);
1885 if (ret)
1886 return ret;
1887
1888 return 0;
1889}
1890
Daniel Vetter042652e2014-07-27 13:46:52 +02001891/**
Thierry Reding14942762015-12-02 17:50:04 +01001892 * drm_atomic_helper_disable_all - disable all currently active outputs
1893 * @dev: DRM device
1894 * @ctx: lock acquisition context
1895 *
1896 * Loops through all connectors, finding those that aren't turned off and then
1897 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1898 * that they are connected to.
1899 *
1900 * This is used for example in suspend/resume to disable all currently active
1901 * functions when suspending.
1902 *
1903 * Note that if callers haven't already acquired all modeset locks this might
1904 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1905 *
1906 * Returns:
1907 * 0 on success or a negative error code on failure.
1908 *
1909 * See also:
1910 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1911 */
1912int drm_atomic_helper_disable_all(struct drm_device *dev,
1913 struct drm_modeset_acquire_ctx *ctx)
1914{
1915 struct drm_atomic_state *state;
1916 struct drm_connector *conn;
1917 int err;
1918
1919 state = drm_atomic_state_alloc(dev);
1920 if (!state)
1921 return -ENOMEM;
1922
1923 state->acquire_ctx = ctx;
1924
1925 drm_for_each_connector(conn, dev) {
1926 struct drm_crtc *crtc = conn->state->crtc;
1927 struct drm_crtc_state *crtc_state;
1928
1929 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1930 continue;
1931
1932 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1933 if (IS_ERR(crtc_state)) {
1934 err = PTR_ERR(crtc_state);
1935 goto free;
1936 }
1937
1938 crtc_state->active = false;
1939 }
1940
1941 err = drm_atomic_commit(state);
1942
1943free:
1944 if (err < 0)
1945 drm_atomic_state_free(state);
1946
1947 return err;
1948}
1949EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1950
1951/**
1952 * drm_atomic_helper_suspend - subsystem-level suspend helper
1953 * @dev: DRM device
1954 *
1955 * Duplicates the current atomic state, disables all active outputs and then
1956 * returns a pointer to the original atomic state to the caller. Drivers can
1957 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1958 * restore the output configuration that was active at the time the system
1959 * entered suspend.
1960 *
1961 * Note that it is potentially unsafe to use this. The atomic state object
1962 * returned by this function is assumed to be persistent. Drivers must ensure
1963 * that this holds true. Before calling this function, drivers must make sure
1964 * to suspend fbdev emulation so that nothing can be using the device.
1965 *
1966 * Returns:
1967 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1968 * encoded error code on failure. Drivers should store the returned atomic
1969 * state object and pass it to the drm_atomic_helper_resume() helper upon
1970 * resume.
1971 *
1972 * See also:
1973 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1974 * drm_atomic_helper_resume()
1975 */
1976struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1977{
1978 struct drm_modeset_acquire_ctx ctx;
1979 struct drm_atomic_state *state;
1980 int err;
1981
1982 drm_modeset_acquire_init(&ctx, 0);
1983
1984retry:
1985 err = drm_modeset_lock_all_ctx(dev, &ctx);
1986 if (err < 0) {
1987 state = ERR_PTR(err);
1988 goto unlock;
1989 }
1990
1991 state = drm_atomic_helper_duplicate_state(dev, &ctx);
1992 if (IS_ERR(state))
1993 goto unlock;
1994
1995 err = drm_atomic_helper_disable_all(dev, &ctx);
1996 if (err < 0) {
1997 drm_atomic_state_free(state);
1998 state = ERR_PTR(err);
1999 goto unlock;
2000 }
2001
2002unlock:
2003 if (PTR_ERR(state) == -EDEADLK) {
2004 drm_modeset_backoff(&ctx);
2005 goto retry;
2006 }
2007
2008 drm_modeset_drop_locks(&ctx);
2009 drm_modeset_acquire_fini(&ctx);
2010 return state;
2011}
2012EXPORT_SYMBOL(drm_atomic_helper_suspend);
2013
2014/**
2015 * drm_atomic_helper_resume - subsystem-level resume helper
2016 * @dev: DRM device
2017 * @state: atomic state to resume to
2018 *
2019 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2020 * grabs all modeset locks and commits the atomic state object. This can be
2021 * used in conjunction with the drm_atomic_helper_suspend() helper to
2022 * implement suspend/resume for drivers that support atomic mode-setting.
2023 *
2024 * Returns:
2025 * 0 on success or a negative error code on failure.
2026 *
2027 * See also:
2028 * drm_atomic_helper_suspend()
2029 */
2030int drm_atomic_helper_resume(struct drm_device *dev,
2031 struct drm_atomic_state *state)
2032{
2033 struct drm_mode_config *config = &dev->mode_config;
2034 int err;
2035
2036 drm_mode_config_reset(dev);
2037 drm_modeset_lock_all(dev);
2038 state->acquire_ctx = config->acquire_ctx;
2039 err = drm_atomic_commit(state);
2040 drm_modeset_unlock_all(dev);
2041
2042 return err;
2043}
2044EXPORT_SYMBOL(drm_atomic_helper_resume);
2045
2046/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002047 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002048 * @crtc: DRM crtc
2049 * @property: DRM property
2050 * @val: value of property
2051 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002052 * Provides a default crtc set_property handler using the atomic driver
2053 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002054 *
2055 * RETURNS:
2056 * Zero on success, error code on failure
2057 */
2058int
2059drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2060 struct drm_property *property,
2061 uint64_t val)
2062{
2063 struct drm_atomic_state *state;
2064 struct drm_crtc_state *crtc_state;
2065 int ret = 0;
2066
2067 state = drm_atomic_state_alloc(crtc->dev);
2068 if (!state)
2069 return -ENOMEM;
2070
2071 /* ->set_property is always called with all locks held. */
2072 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2073retry:
2074 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2075 if (IS_ERR(crtc_state)) {
2076 ret = PTR_ERR(crtc_state);
2077 goto fail;
2078 }
2079
Rob Clark40ecc692014-12-18 16:01:46 -05002080 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2081 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002082 if (ret)
2083 goto fail;
2084
2085 ret = drm_atomic_commit(state);
2086 if (ret != 0)
2087 goto fail;
2088
2089 /* Driver takes ownership of state on successful commit. */
2090 return 0;
2091fail:
2092 if (ret == -EDEADLK)
2093 goto backoff;
2094
2095 drm_atomic_state_free(state);
2096
2097 return ret;
2098backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002099 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002100 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002101
2102 goto retry;
2103}
2104EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2105
2106/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002107 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002108 * @plane: DRM plane
2109 * @property: DRM property
2110 * @val: value of property
2111 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002112 * Provides a default plane set_property handler using the atomic driver
2113 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002114 *
2115 * RETURNS:
2116 * Zero on success, error code on failure
2117 */
2118int
2119drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2120 struct drm_property *property,
2121 uint64_t val)
2122{
2123 struct drm_atomic_state *state;
2124 struct drm_plane_state *plane_state;
2125 int ret = 0;
2126
2127 state = drm_atomic_state_alloc(plane->dev);
2128 if (!state)
2129 return -ENOMEM;
2130
2131 /* ->set_property is always called with all locks held. */
2132 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2133retry:
2134 plane_state = drm_atomic_get_plane_state(state, plane);
2135 if (IS_ERR(plane_state)) {
2136 ret = PTR_ERR(plane_state);
2137 goto fail;
2138 }
2139
Rob Clark40ecc692014-12-18 16:01:46 -05002140 ret = drm_atomic_plane_set_property(plane, plane_state,
2141 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002142 if (ret)
2143 goto fail;
2144
2145 ret = drm_atomic_commit(state);
2146 if (ret != 0)
2147 goto fail;
2148
2149 /* Driver takes ownership of state on successful commit. */
2150 return 0;
2151fail:
2152 if (ret == -EDEADLK)
2153 goto backoff;
2154
2155 drm_atomic_state_free(state);
2156
2157 return ret;
2158backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002159 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002160 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002161
2162 goto retry;
2163}
2164EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2165
2166/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002167 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002168 * @connector: DRM connector
2169 * @property: DRM property
2170 * @val: value of property
2171 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002172 * Provides a default connector set_property handler using the atomic driver
2173 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002174 *
2175 * RETURNS:
2176 * Zero on success, error code on failure
2177 */
2178int
2179drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2180 struct drm_property *property,
2181 uint64_t val)
2182{
2183 struct drm_atomic_state *state;
2184 struct drm_connector_state *connector_state;
2185 int ret = 0;
2186
2187 state = drm_atomic_state_alloc(connector->dev);
2188 if (!state)
2189 return -ENOMEM;
2190
2191 /* ->set_property is always called with all locks held. */
2192 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2193retry:
2194 connector_state = drm_atomic_get_connector_state(state, connector);
2195 if (IS_ERR(connector_state)) {
2196 ret = PTR_ERR(connector_state);
2197 goto fail;
2198 }
2199
Rob Clark40ecc692014-12-18 16:01:46 -05002200 ret = drm_atomic_connector_set_property(connector, connector_state,
2201 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002202 if (ret)
2203 goto fail;
2204
2205 ret = drm_atomic_commit(state);
2206 if (ret != 0)
2207 goto fail;
2208
2209 /* Driver takes ownership of state on successful commit. */
2210 return 0;
2211fail:
2212 if (ret == -EDEADLK)
2213 goto backoff;
2214
2215 drm_atomic_state_free(state);
2216
2217 return ret;
2218backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002219 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002220 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002221
2222 goto retry;
2223}
2224EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002225
2226/**
2227 * drm_atomic_helper_page_flip - execute a legacy page flip
2228 * @crtc: DRM crtc
2229 * @fb: DRM framebuffer
2230 * @event: optional DRM event to signal upon completion
2231 * @flags: flip flags for non-vblank sync'ed updates
2232 *
2233 * Provides a default page flip implementation using the atomic driver interface.
2234 *
2235 * Note that for now so called async page flips (i.e. updates which are not
2236 * synchronized to vblank) are not supported, since the atomic interfaces have
2237 * no provisions for this yet.
2238 *
2239 * Returns:
2240 * Returns 0 on success, negative errno numbers on failure.
2241 */
2242int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2243 struct drm_framebuffer *fb,
2244 struct drm_pending_vblank_event *event,
2245 uint32_t flags)
2246{
2247 struct drm_plane *plane = crtc->primary;
2248 struct drm_atomic_state *state;
2249 struct drm_plane_state *plane_state;
2250 struct drm_crtc_state *crtc_state;
2251 int ret = 0;
2252
2253 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2254 return -EINVAL;
2255
2256 state = drm_atomic_state_alloc(plane->dev);
2257 if (!state)
2258 return -ENOMEM;
2259
2260 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2261retry:
2262 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2263 if (IS_ERR(crtc_state)) {
2264 ret = PTR_ERR(crtc_state);
2265 goto fail;
2266 }
2267 crtc_state->event = event;
2268
2269 plane_state = drm_atomic_get_plane_state(state, plane);
2270 if (IS_ERR(plane_state)) {
2271 ret = PTR_ERR(plane_state);
2272 goto fail;
2273 }
2274
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002275 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002276 if (ret != 0)
2277 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002278 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002279
2280 ret = drm_atomic_async_commit(state);
2281 if (ret != 0)
2282 goto fail;
2283
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002284 /* Driver takes ownership of state on successful async commit. */
2285 return 0;
2286fail:
2287 if (ret == -EDEADLK)
2288 goto backoff;
2289
2290 drm_atomic_state_free(state);
2291
2292 return ret;
2293backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002294 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002295 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002296
2297 /*
2298 * Someone might have exchanged the framebuffer while we dropped locks
2299 * in the backoff code. We need to fix up the fb refcount tracking the
2300 * core does for us.
2301 */
2302 plane->old_fb = plane->fb;
2303
2304 goto retry;
2305}
2306EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002307
2308/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002309 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2310 * @connector: affected connector
2311 * @mode: DPMS mode
2312 *
2313 * This is the main helper function provided by the atomic helper framework for
2314 * implementing the legacy DPMS connector interface. It computes the new desired
2315 * ->active state for the corresponding CRTC (if the connector is enabled) and
2316 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002317 *
2318 * Returns:
2319 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002320 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002321int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2322 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002323{
2324 struct drm_mode_config *config = &connector->dev->mode_config;
2325 struct drm_atomic_state *state;
2326 struct drm_crtc_state *crtc_state;
2327 struct drm_crtc *crtc;
2328 struct drm_connector *tmp_connector;
2329 int ret;
2330 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002331 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002332
2333 if (mode != DRM_MODE_DPMS_ON)
2334 mode = DRM_MODE_DPMS_OFF;
2335
2336 connector->dpms = mode;
2337 crtc = connector->state->crtc;
2338
2339 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002340 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002341
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002342 state = drm_atomic_state_alloc(connector->dev);
2343 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002344 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002345
2346 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2347retry:
2348 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002349 if (IS_ERR(crtc_state)) {
2350 ret = PTR_ERR(crtc_state);
2351 goto fail;
2352 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002353
2354 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2355
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002356 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002357 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002358 continue;
2359
John Hunter0388df02015-03-17 15:30:28 +08002360 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002361 active = true;
2362 break;
2363 }
2364 }
2365 crtc_state->active = active;
2366
2367 ret = drm_atomic_commit(state);
2368 if (ret != 0)
2369 goto fail;
2370
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002371 /* Driver takes ownership of state on successful commit. */
2372 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002373fail:
2374 if (ret == -EDEADLK)
2375 goto backoff;
2376
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002377 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002378 drm_atomic_state_free(state);
2379
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002380 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002381backoff:
2382 drm_atomic_state_clear(state);
2383 drm_atomic_legacy_backoff(state);
2384
2385 goto retry;
2386}
2387EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2388
2389/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002390 * DOC: atomic state reset and initialization
2391 *
2392 * Both the drm core and the atomic helpers assume that there is always the full
2393 * and correct atomic software state for all connectors, CRTCs and planes
2394 * available. Which is a bit a problem on driver load and also after system
2395 * suspend. One way to solve this is to have a hardware state read-out
2396 * infrastructure which reconstructs the full software state (e.g. the i915
2397 * driver).
2398 *
2399 * The simpler solution is to just reset the software state to everything off,
2400 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2401 * the atomic helpers provide default reset implementations for all hooks.
2402 */
2403
2404/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002405 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2406 * @crtc: drm CRTC
2407 *
2408 * Resets the atomic state for @crtc by freeing the state pointer (which might
2409 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2410 */
2411void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2412{
Markus Elfring5f911902015-11-06 12:03:46 +01002413 if (crtc->state)
Daniel Stone99cf4a22015-05-25 19:11:51 +01002414 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002415 kfree(crtc->state);
2416 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002417
2418 if (crtc->state)
2419 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002420}
2421EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2422
2423/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002424 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2425 * @crtc: CRTC object
2426 * @state: atomic CRTC state
2427 *
2428 * Copies atomic state from a CRTC's current state and resets inferred values.
2429 * This is useful for drivers that subclass the CRTC state.
2430 */
2431void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2432 struct drm_crtc_state *state)
2433{
2434 memcpy(state, crtc->state, sizeof(*state));
2435
Daniel Stone99cf4a22015-05-25 19:11:51 +01002436 if (state->mode_blob)
2437 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002438 state->mode_changed = false;
2439 state->active_changed = false;
2440 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002441 state->connectors_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002442 state->event = NULL;
2443}
2444EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2445
2446/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002447 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2448 * @crtc: drm CRTC
2449 *
2450 * Default CRTC state duplicate hook for drivers which don't have their own
2451 * subclassed CRTC state structure.
2452 */
2453struct drm_crtc_state *
2454drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2455{
2456 struct drm_crtc_state *state;
2457
2458 if (WARN_ON(!crtc->state))
2459 return NULL;
2460
Thierry Redingf5e78402015-01-28 14:54:32 +01002461 state = kmalloc(sizeof(*state), GFP_KERNEL);
2462 if (state)
2463 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002464
2465 return state;
2466}
2467EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2468
2469/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002470 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2471 * @crtc: CRTC object
2472 * @state: CRTC state object to release
2473 *
2474 * Releases all resources stored in the CRTC state without actually freeing
2475 * the memory of the CRTC state. This is useful for drivers that subclass the
2476 * CRTC state.
2477 */
2478void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2479 struct drm_crtc_state *state)
2480{
Markus Elfring5f911902015-11-06 12:03:46 +01002481 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002482}
2483EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2484
2485/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002486 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2487 * @crtc: drm CRTC
2488 * @state: CRTC state object to release
2489 *
2490 * Default CRTC state destroy hook for drivers which don't have their own
2491 * subclassed CRTC state structure.
2492 */
2493void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2494 struct drm_crtc_state *state)
2495{
Thierry Redingf5e78402015-01-28 14:54:32 +01002496 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002497 kfree(state);
2498}
2499EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2500
2501/**
2502 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2503 * @plane: drm plane
2504 *
2505 * Resets the atomic state for @plane by freeing the state pointer (which might
2506 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2507 */
2508void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2509{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002510 if (plane->state && plane->state->fb)
2511 drm_framebuffer_unreference(plane->state->fb);
2512
Daniel Vetterd4617012014-11-03 15:56:43 +01002513 kfree(plane->state);
2514 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002515
2516 if (plane->state)
2517 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01002518}
2519EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2520
2521/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002522 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2523 * @plane: plane object
2524 * @state: atomic plane state
2525 *
2526 * Copies atomic state from a plane's current state. This is useful for
2527 * drivers that subclass the plane state.
2528 */
2529void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2530 struct drm_plane_state *state)
2531{
2532 memcpy(state, plane->state, sizeof(*state));
2533
2534 if (state->fb)
2535 drm_framebuffer_reference(state->fb);
2536}
2537EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2538
2539/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002540 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2541 * @plane: drm plane
2542 *
2543 * Default plane state duplicate hook for drivers which don't have their own
2544 * subclassed plane state structure.
2545 */
2546struct drm_plane_state *
2547drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2548{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002549 struct drm_plane_state *state;
2550
Daniel Vetterd4617012014-11-03 15:56:43 +01002551 if (WARN_ON(!plane->state))
2552 return NULL;
2553
Thierry Redingf5e78402015-01-28 14:54:32 +01002554 state = kmalloc(sizeof(*state), GFP_KERNEL);
2555 if (state)
2556 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002557
2558 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002559}
2560EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2561
2562/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002563 * __drm_atomic_helper_plane_destroy_state - release plane state
2564 * @plane: plane object
2565 * @state: plane state object to release
2566 *
2567 * Releases all resources stored in the plane state without actually freeing
2568 * the memory of the plane state. This is useful for drivers that subclass the
2569 * plane state.
2570 */
2571void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2572 struct drm_plane_state *state)
2573{
2574 if (state->fb)
2575 drm_framebuffer_unreference(state->fb);
2576}
2577EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2578
2579/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002580 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2581 * @plane: drm plane
2582 * @state: plane state object to release
2583 *
2584 * Default plane state destroy hook for drivers which don't have their own
2585 * subclassed plane state structure.
2586 */
2587void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002588 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002589{
Thierry Redingf5e78402015-01-28 14:54:32 +01002590 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002591 kfree(state);
2592}
2593EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2594
2595/**
2596 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2597 * @connector: drm connector
2598 *
2599 * Resets the atomic state for @connector by freeing the state pointer (which
2600 * might be NULL, e.g. at driver load time) and allocating a new empty state
2601 * object.
2602 */
2603void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2604{
2605 kfree(connector->state);
2606 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002607
2608 if (connector->state)
2609 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002610}
2611EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2612
2613/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002614 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2615 * @connector: connector object
2616 * @state: atomic connector state
2617 *
2618 * Copies atomic state from a connector's current state. This is useful for
2619 * drivers that subclass the connector state.
2620 */
2621void
2622__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2623 struct drm_connector_state *state)
2624{
2625 memcpy(state, connector->state, sizeof(*state));
2626}
2627EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2628
2629/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002630 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2631 * @connector: drm connector
2632 *
2633 * Default connector state duplicate hook for drivers which don't have their own
2634 * subclassed connector state structure.
2635 */
2636struct drm_connector_state *
2637drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2638{
Thierry Redingf5e78402015-01-28 14:54:32 +01002639 struct drm_connector_state *state;
2640
Daniel Vetterd4617012014-11-03 15:56:43 +01002641 if (WARN_ON(!connector->state))
2642 return NULL;
2643
Thierry Redingf5e78402015-01-28 14:54:32 +01002644 state = kmalloc(sizeof(*state), GFP_KERNEL);
2645 if (state)
2646 __drm_atomic_helper_connector_duplicate_state(connector, state);
2647
2648 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002649}
2650EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2651
2652/**
Thierry Reding397fd772015-09-08 15:00:45 +02002653 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2654 * @dev: DRM device
2655 * @ctx: lock acquisition context
2656 *
2657 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01002658 * duplicating their respective states. This is used for example by suspend/
2659 * resume support code to save the state prior to suspend such that it can
2660 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02002661 *
2662 * Note that this treats atomic state as persistent between save and restore.
2663 * Drivers must make sure that this is possible and won't result in confusion
2664 * or erroneous behaviour.
2665 *
2666 * Note that if callers haven't already acquired all modeset locks this might
2667 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2668 *
2669 * Returns:
2670 * A pointer to the copy of the atomic state object on success or an
2671 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01002672 *
2673 * See also:
2674 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02002675 */
2676struct drm_atomic_state *
2677drm_atomic_helper_duplicate_state(struct drm_device *dev,
2678 struct drm_modeset_acquire_ctx *ctx)
2679{
2680 struct drm_atomic_state *state;
2681 struct drm_connector *conn;
2682 struct drm_plane *plane;
2683 struct drm_crtc *crtc;
2684 int err = 0;
2685
2686 state = drm_atomic_state_alloc(dev);
2687 if (!state)
2688 return ERR_PTR(-ENOMEM);
2689
2690 state->acquire_ctx = ctx;
2691
2692 drm_for_each_crtc(crtc, dev) {
2693 struct drm_crtc_state *crtc_state;
2694
2695 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2696 if (IS_ERR(crtc_state)) {
2697 err = PTR_ERR(crtc_state);
2698 goto free;
2699 }
2700 }
2701
2702 drm_for_each_plane(plane, dev) {
2703 struct drm_plane_state *plane_state;
2704
2705 plane_state = drm_atomic_get_plane_state(state, plane);
2706 if (IS_ERR(plane_state)) {
2707 err = PTR_ERR(plane_state);
2708 goto free;
2709 }
2710 }
2711
2712 drm_for_each_connector(conn, dev) {
2713 struct drm_connector_state *conn_state;
2714
2715 conn_state = drm_atomic_get_connector_state(state, conn);
2716 if (IS_ERR(conn_state)) {
2717 err = PTR_ERR(conn_state);
2718 goto free;
2719 }
2720 }
2721
2722 /* clear the acquire context so that it isn't accidentally reused */
2723 state->acquire_ctx = NULL;
2724
2725free:
2726 if (err < 0) {
2727 drm_atomic_state_free(state);
2728 state = ERR_PTR(err);
2729 }
2730
2731 return state;
2732}
2733EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2734
2735/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002736 * __drm_atomic_helper_connector_destroy_state - release connector state
2737 * @connector: connector object
2738 * @state: connector state object to release
2739 *
2740 * Releases all resources stored in the connector state without actually
2741 * freeing the memory of the connector state. This is useful for drivers that
2742 * subclass the connector state.
2743 */
2744void
2745__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2746 struct drm_connector_state *state)
2747{
2748 /*
2749 * This is currently a placeholder so that drivers that subclass the
2750 * state will automatically do the right thing if code is ever added
2751 * to this function.
2752 */
2753}
2754EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2755
2756/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002757 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2758 * @connector: drm connector
2759 * @state: connector state object to release
2760 *
2761 * Default connector state destroy hook for drivers which don't have their own
2762 * subclassed connector state structure.
2763 */
2764void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2765 struct drm_connector_state *state)
2766{
Thierry Redingf5e78402015-01-28 14:54:32 +01002767 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002768 kfree(state);
2769}
2770EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);