blob: 8694ca9beee35de502dd35a17ec7858682294097 [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
45 * drm_atomic_helper_check and for the commit callback with
46 * drm_atomic_helper_commit. But the individual stages and callbacks are expose
47 * to allow drivers to mix and match and e.g. use the plane helpers only
48 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
51 * 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
53 * 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 Vetter623369e2014-09-16 17:50:47 +020083static struct drm_crtc *
84get_current_crtc_for_encoder(struct drm_device *dev,
85 struct drm_encoder *encoder)
86{
87 struct drm_mode_config *config = &dev->mode_config;
88 struct drm_connector *connector;
89
90 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
91
92 list_for_each_entry(connector, &config->connector_list, head) {
93 if (connector->state->best_encoder != encoder)
94 continue;
95
96 return connector->state->crtc;
97 }
98
99 return NULL;
100}
101
102static int
103steal_encoder(struct drm_atomic_state *state,
104 struct drm_encoder *encoder,
105 struct drm_crtc *encoder_crtc)
106{
107 struct drm_mode_config *config = &state->dev->mode_config;
108 struct drm_crtc_state *crtc_state;
109 struct drm_connector *connector;
110 struct drm_connector_state *connector_state;
Daniel Vetter042652e2014-07-27 13:46:52 +0200111 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200112
113 /*
114 * We can only steal an encoder coming from a connector, which means we
115 * must already hold the connection_mutex.
116 */
117 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118
Daniel Vetter17a38d92015-02-22 12:24:16 +0100119 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
120 encoder->base.id, encoder->name,
121 encoder_crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200122
123 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
124 if (IS_ERR(crtc_state))
125 return PTR_ERR(crtc_state);
126
127 crtc_state->mode_changed = true;
128
129 list_for_each_entry(connector, &config->connector_list, head) {
130 if (connector->state->best_encoder != encoder)
131 continue;
132
Daniel Vetter17a38d92015-02-22 12:24:16 +0100133 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
134 connector->base.id,
135 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200136
137 connector_state = drm_atomic_get_connector_state(state,
138 connector);
139 if (IS_ERR(connector_state))
140 return PTR_ERR(connector_state);
141
Daniel Vetter042652e2014-07-27 13:46:52 +0200142 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
143 if (ret)
144 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200145 connector_state->best_encoder = NULL;
146 }
147
148 return 0;
149}
150
151static int
152update_connector_routing(struct drm_atomic_state *state, int conn_idx)
153{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200154 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200155 struct drm_encoder *new_encoder;
156 struct drm_crtc *encoder_crtc;
157 struct drm_connector *connector;
158 struct drm_connector_state *connector_state;
159 struct drm_crtc_state *crtc_state;
160 int idx, ret;
161
162 connector = state->connectors[conn_idx];
163 connector_state = state->connector_states[conn_idx];
164
165 if (!connector)
166 return 0;
167
Daniel Vetter17a38d92015-02-22 12:24:16 +0100168 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
169 connector->base.id,
170 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200171
172 if (connector->state->crtc != connector_state->crtc) {
173 if (connector->state->crtc) {
174 idx = drm_crtc_index(connector->state->crtc);
175
176 crtc_state = state->crtc_states[idx];
177 crtc_state->mode_changed = true;
178 }
179
180 if (connector_state->crtc) {
181 idx = drm_crtc_index(connector_state->crtc);
182
183 crtc_state = state->crtc_states[idx];
184 crtc_state->mode_changed = true;
185 }
186 }
187
188 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100189 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200190 connector->base.id,
191 connector->name);
192
193 connector_state->best_encoder = NULL;
194
195 return 0;
196 }
197
198 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200199
200 if (funcs->atomic_best_encoder)
201 new_encoder = funcs->atomic_best_encoder(connector,
202 connector_state);
203 else
204 new_encoder = funcs->best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200205
206 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100207 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
208 connector->base.id,
209 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200210 return -EINVAL;
211 }
212
213 if (new_encoder == connector_state->best_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100214 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
215 connector->base.id,
216 connector->name,
217 new_encoder->base.id,
218 new_encoder->name,
219 connector_state->crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200220
221 return 0;
222 }
223
224 encoder_crtc = get_current_crtc_for_encoder(state->dev,
225 new_encoder);
226
227 if (encoder_crtc) {
228 ret = steal_encoder(state, new_encoder, encoder_crtc);
229 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100230 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
231 connector->base.id,
232 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200233 return ret;
234 }
235 }
236
237 connector_state->best_encoder = new_encoder;
Linus Torvalds27667f42015-07-29 22:18:16 -0700238 if (connector_state->crtc) {
239 idx = drm_crtc_index(connector_state->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200240
Linus Torvalds27667f42015-07-29 22:18:16 -0700241 crtc_state = state->crtc_states[idx];
242 crtc_state->mode_changed = true;
243 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200244
Daniel Vetter17a38d92015-02-22 12:24:16 +0100245 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
246 connector->base.id,
247 connector->name,
248 new_encoder->base.id,
249 new_encoder->name,
250 connector_state->crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200251
252 return 0;
253}
254
255static int
256mode_fixup(struct drm_atomic_state *state)
257{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300258 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200259 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300260 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200261 struct drm_connector_state *conn_state;
262 int i;
263 bool ret;
264
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300265 for_each_crtc_in_state(state, crtc, crtc_state, i) {
266 if (!crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200267 continue;
268
269 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
270 }
271
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300272 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200273 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200274 struct drm_encoder *encoder;
275
Daniel Vetter623369e2014-09-16 17:50:47 +0200276 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
277
278 if (!conn_state->crtc || !conn_state->best_encoder)
279 continue;
280
281 crtc_state =
282 state->crtc_states[drm_crtc_index(conn_state->crtc)];
283
284 /*
285 * Each encoder has at most one connector (since we always steal
286 * it away), so we won't call ->mode_fixup twice.
287 */
288 encoder = conn_state->best_encoder;
289 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300290 if (!funcs)
291 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200292
Archit Taneja862e6862015-05-21 11:03:16 +0530293 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
294 &crtc_state->adjusted_mode);
295 if (!ret) {
296 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
297 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200298 }
299
Thierry Reding4cd4df82014-12-03 16:44:34 +0100300 if (funcs->atomic_check) {
301 ret = funcs->atomic_check(encoder, crtc_state,
302 conn_state);
303 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100304 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
305 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100306 return ret;
307 }
308 } else {
309 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
310 &crtc_state->adjusted_mode);
311 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100312 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
313 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100314 return -EINVAL;
315 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200316 }
317 }
318
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300319 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200320 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200321
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300322 if (!crtc_state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200323 continue;
324
325 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300326 if (!funcs->mode_fixup)
327 continue;
328
Daniel Vetter623369e2014-09-16 17:50:47 +0200329 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
330 &crtc_state->adjusted_mode);
331 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100332 DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
333 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200334 return -EINVAL;
335 }
336 }
337
338 return 0;
339}
340
Daniel Vetterd9b13622014-11-26 16:57:41 +0100341/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800342 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100343 * @dev: DRM device
344 * @state: the driver state object
345 *
346 * Check the state object to see if the requested state is physically possible.
347 * This does all the crtc and connector related computations for an atomic
348 * update. It computes and updates crtc_state->mode_changed, adds any additional
349 * connectors needed for full modesets and calls down into ->mode_fixup
350 * functions of the driver backend.
351 *
352 * IMPORTANT:
353 *
354 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
355 * plane update can't be done without a full modeset) _must_ call this function
356 * afterwards after that change. It is permitted to call this function multiple
357 * times for the same update, e.g. when the ->atomic_check functions depend upon
358 * the adjusted dotclock for fifo space allocation and watermark computation.
359 *
360 * RETURNS
361 * Zero for success or -errno
362 */
363int
Rob Clark934ce1c2014-11-19 16:41:33 -0500364drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200365 struct drm_atomic_state *state)
366{
Daniel Vetter623369e2014-09-16 17:50:47 +0200367 struct drm_crtc *crtc;
368 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300369 struct drm_connector *connector;
370 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200371 int i, ret;
372
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300373 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200374 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100375 DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
376 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200377 crtc_state->mode_changed = true;
378 }
379
380 if (crtc->state->enable != crtc_state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100381 DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
382 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200383 crtc_state->mode_changed = true;
384 }
385 }
386
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300387 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200388 /*
389 * This only sets crtc->mode_changed for routing changes,
390 * drivers must set crtc->mode_changed themselves when connector
391 * properties need to be updated.
392 */
393 ret = update_connector_routing(state, i);
394 if (ret)
395 return ret;
396 }
397
398 /*
399 * After all the routing has been prepared we need to add in any
400 * connector which is itself unchanged, but who's crtc changes it's
401 * configuration. This must be done before calling mode_fixup in case a
402 * crtc only changed its mode but has the same set of connectors.
403 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300404 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200405 int num_connectors;
406
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100407 /*
408 * We must set ->active_changed after walking connectors for
409 * otherwise an update that only changes active would result in
410 * a full modeset because update_connector_routing force that.
411 */
412 if (crtc->state->active != crtc_state->active) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100413 DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
414 crtc->base.id);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100415 crtc_state->active_changed = true;
416 }
417
Daniel Vetter2465ff62015-06-18 09:58:55 +0200418 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100419 continue;
420
Daniel Vetter17a38d92015-02-22 12:24:16 +0100421 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
422 crtc->base.id,
423 crtc_state->enable ? 'y' : 'n',
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100424 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200425
426 ret = drm_atomic_add_affected_connectors(state, crtc);
427 if (ret != 0)
428 return ret;
429
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200430 ret = drm_atomic_add_affected_planes(state, crtc);
431 if (ret != 0)
432 return ret;
433
Daniel Vetter623369e2014-09-16 17:50:47 +0200434 num_connectors = drm_atomic_connectors_for_crtc(state,
435 crtc);
436
437 if (crtc_state->enable != !!num_connectors) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100438 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
439 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200440
441 return -EINVAL;
442 }
443 }
444
445 return mode_fixup(state);
446}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100447EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200448
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100449/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800450 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100451 * @dev: DRM device
452 * @state: the driver state object
453 *
454 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100455 * This does all the plane update related checks using by calling into the
456 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100457 *
458 * RETURNS
459 * Zero for success or -errno
460 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100461int
462drm_atomic_helper_check_planes(struct drm_device *dev,
463 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100464{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300465 struct drm_crtc *crtc;
466 struct drm_crtc_state *crtc_state;
467 struct drm_plane *plane;
468 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100469 int i, ret = 0;
470
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300471 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200472 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100473
474 funcs = plane->helper_private;
475
476 drm_atomic_helper_plane_changed(state, plane_state, plane);
477
478 if (!funcs || !funcs->atomic_check)
479 continue;
480
481 ret = funcs->atomic_check(plane, plane_state);
482 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100483 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
484 plane->base.id);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100485 return ret;
486 }
487 }
488
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300489 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200490 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100491
492 funcs = crtc->helper_private;
493
494 if (!funcs || !funcs->atomic_check)
495 continue;
496
497 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
498 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100499 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
500 crtc->base.id);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100501 return ret;
502 }
503 }
504
Daniel Vetterd9b13622014-11-26 16:57:41 +0100505 return ret;
506}
507EXPORT_SYMBOL(drm_atomic_helper_check_planes);
508
509/**
510 * drm_atomic_helper_check - validate state object
511 * @dev: DRM device
512 * @state: the driver state object
513 *
514 * Check the state object to see if the requested state is physically possible.
515 * Only crtcs and planes have check callbacks, so for any additional (global)
516 * checking that a driver needs it can simply wrap that around this function.
517 * Drivers without such needs can directly use this as their ->atomic_check()
518 * callback.
519 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100520 * This just wraps the two parts of the state checking for planes and modeset
521 * state in the default order: First it calls drm_atomic_helper_check_modeset()
522 * and then drm_atomic_helper_check_planes(). The assumption is that the
523 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
524 * e.g. properly compute watermarks.
525 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100526 * RETURNS
527 * Zero for success or -errno
528 */
529int drm_atomic_helper_check(struct drm_device *dev,
530 struct drm_atomic_state *state)
531{
532 int ret;
533
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100534 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100535 if (ret)
536 return ret;
537
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100538 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500539 if (ret)
540 return ret;
541
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100542 return ret;
543}
544EXPORT_SYMBOL(drm_atomic_helper_check);
545
Daniel Vetter623369e2014-09-16 17:50:47 +0200546static void
547disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
548{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300549 struct drm_connector *connector;
550 struct drm_connector_state *old_conn_state;
551 struct drm_crtc *crtc;
552 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200553 int i;
554
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300555 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200556 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200557 struct drm_encoder *encoder;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100558 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200559
Daniel Vetter623369e2014-09-16 17:50:47 +0200560 /* Shut down everything that's in the changeset and currently
561 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300562 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200563 continue;
564
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100565 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
566
Daniel Vetter4218a322015-03-26 22:18:40 +0100567 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200568 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100569 continue;
570
Rob Clark46df9ad2014-11-20 15:40:36 -0500571 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200572
Rob Clark46df9ad2014-11-20 15:40:36 -0500573 /* We shouldn't get this far if we didn't previously have
574 * an encoder.. but WARN_ON() rather than explode.
575 */
576 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200577 continue;
578
579 funcs = encoder->helper_private;
580
Daniel Vetter17a38d92015-02-22 12:24:16 +0100581 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
582 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100583
Daniel Vetter623369e2014-09-16 17:50:47 +0200584 /*
585 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800586 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200587 */
Archit Taneja862e6862015-05-21 11:03:16 +0530588 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200589
590 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100591 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200592 funcs->prepare(encoder);
593 else if (funcs->disable)
594 funcs->disable(encoder);
595 else
596 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
597
Archit Taneja862e6862015-05-21 11:03:16 +0530598 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200599 }
600
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300601 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200602 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200603
604 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200605 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100606 continue;
607
608 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200609 continue;
610
611 funcs = crtc->helper_private;
612
Daniel Vetter17a38d92015-02-22 12:24:16 +0100613 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
614 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100615
616
Daniel Vetter623369e2014-09-16 17:50:47 +0200617 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100618 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200619 funcs->prepare(crtc);
620 else if (funcs->disable)
621 funcs->disable(crtc);
622 else
623 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
624 }
625}
626
Daniel Vetter4c18d302015-05-12 15:27:37 +0200627/**
628 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
629 * @dev: DRM device
630 * @old_state: atomic state object with old state structures
631 *
632 * This function updates all the various legacy modeset state pointers in
633 * connectors, encoders and crtcs. It also updates the timestamping constants
634 * used for precise vblank timestamps by calling
635 * drm_calc_timestamping_constants().
636 *
637 * Drivers can use this for building their own atomic commit if they don't have
638 * a pure helper-based modeset implementation.
639 */
640void
641drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
642 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200643{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300644 struct drm_connector *connector;
645 struct drm_connector_state *old_conn_state;
646 struct drm_crtc *crtc;
647 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200648 int i;
649
650 /* clear out existing links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300651 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
652 if (!connector->encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200653 continue;
654
655 WARN_ON(!connector->encoder->crtc);
656
657 connector->encoder->crtc = NULL;
658 connector->encoder = NULL;
659 }
660
661 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300662 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
663 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200664 continue;
665
666 if (WARN_ON(!connector->state->best_encoder))
667 continue;
668
669 connector->encoder = connector->state->best_encoder;
670 connector->encoder->crtc = connector->state->crtc;
671 }
672
673 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300674 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200675 crtc->mode = crtc->state->mode;
676 crtc->enabled = crtc->state->enable;
677 crtc->x = crtc->primary->state->src_x >> 16;
678 crtc->y = crtc->primary->state->src_y >> 16;
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200679
680 if (crtc->state->enable)
681 drm_calc_timestamping_constants(crtc,
682 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200683 }
684}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200685EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200686
687static void
688crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
689{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300690 struct drm_crtc *crtc;
691 struct drm_crtc_state *old_crtc_state;
692 struct drm_connector *connector;
693 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200694 int i;
695
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300696 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200697 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200698
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300699 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200700 continue;
701
702 funcs = crtc->helper_private;
703
Daniel Vetterc982bd92015-02-22 12:24:20 +0100704 if (crtc->state->enable && funcs->mode_set_nofb) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100705 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
706 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100707
Daniel Vetter623369e2014-09-16 17:50:47 +0200708 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100709 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200710 }
711
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300712 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200713 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200714 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200715 struct drm_encoder *encoder;
716 struct drm_display_mode *mode, *adjusted_mode;
717
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300718 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200719 continue;
720
721 encoder = connector->state->best_encoder;
722 funcs = encoder->helper_private;
723 new_crtc_state = connector->state->crtc->state;
724 mode = &new_crtc_state->mode;
725 adjusted_mode = &new_crtc_state->adjusted_mode;
726
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100727 if (!new_crtc_state->mode_changed)
728 continue;
729
Daniel Vetter17a38d92015-02-22 12:24:16 +0100730 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
731 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100732
Daniel Vetter623369e2014-09-16 17:50:47 +0200733 /*
734 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800735 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200736 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100737 if (funcs->mode_set)
738 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200739
Archit Taneja862e6862015-05-21 11:03:16 +0530740 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200741 }
742}
743
744/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100745 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200746 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100747 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200748 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100749 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200750 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100751 *
752 * For compatability with legacy crtc helpers this should be called before
753 * drm_atomic_helper_commit_planes(), which is what the default commit function
754 * does. But drivers with different needs can group the modeset commits together
755 * and do the plane commits at the end. This is useful for drivers doing runtime
756 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200757 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100758void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
759 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200760{
Laurent Pincharta072f802015-02-22 12:24:18 +0100761 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200762
763 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
764
Laurent Pincharta072f802015-02-22 12:24:18 +0100765 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200766}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100767EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200768
769/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100770 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200771 * @dev: DRM device
772 * @old_state: atomic state object with old state structures
773 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100774 * This function enables all the outputs with the new configuration which had to
775 * be turned off for the update.
776 *
777 * For compatability with legacy crtc helpers this should be called after
778 * drm_atomic_helper_commit_planes(), which is what the default commit function
779 * does. But drivers with different needs can group the modeset commits together
780 * and do the plane commits at the end. This is useful for drivers doing runtime
781 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200782 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100783void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
784 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200785{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300786 struct drm_crtc *crtc;
787 struct drm_crtc_state *old_crtc_state;
788 struct drm_connector *connector;
789 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200790 int i;
791
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300792 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200793 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200794
795 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200796 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100797 continue;
798
799 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200800 continue;
801
802 funcs = crtc->helper_private;
803
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100804 if (crtc->state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100805 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
806 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100807
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100808 if (funcs->enable)
809 funcs->enable(crtc);
810 else
811 funcs->commit(crtc);
812 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200813 }
814
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300815 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200816 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200817 struct drm_encoder *encoder;
818
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300819 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200820 continue;
821
Daniel Vetter4218a322015-03-26 22:18:40 +0100822 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200823 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100824 continue;
825
Daniel Vetter623369e2014-09-16 17:50:47 +0200826 encoder = connector->state->best_encoder;
827 funcs = encoder->helper_private;
828
Daniel Vetter17a38d92015-02-22 12:24:16 +0100829 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
830 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100831
Daniel Vetter623369e2014-09-16 17:50:47 +0200832 /*
833 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800834 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200835 */
Archit Taneja862e6862015-05-21 11:03:16 +0530836 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200837
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100838 if (funcs->enable)
839 funcs->enable(encoder);
840 else
841 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200842
Archit Taneja862e6862015-05-21 11:03:16 +0530843 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200844 }
845}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100846EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200847
Daniel Vettere2330f02014-10-29 11:34:56 +0100848static void wait_for_fences(struct drm_device *dev,
849 struct drm_atomic_state *state)
850{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300851 struct drm_plane *plane;
852 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +0100853 int i;
854
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300855 for_each_plane_in_state(state, plane, plane_state, i) {
856 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +0100857 continue;
858
859 WARN_ON(!plane->state->fb);
860
861 fence_wait(plane->state->fence, false);
862 fence_put(plane->state->fence);
863 plane->state->fence = NULL;
864 }
865}
866
Daniel Vetterab58e332014-11-24 20:42:42 +0100867static bool framebuffer_changed(struct drm_device *dev,
868 struct drm_atomic_state *old_state,
869 struct drm_crtc *crtc)
870{
871 struct drm_plane *plane;
872 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +0100873 int i;
874
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300875 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +0100876 if (plane->state->crtc != crtc &&
877 old_plane_state->crtc != crtc)
878 continue;
879
880 if (plane->state->fb != old_plane_state->fb)
881 return true;
882 }
883
884 return false;
885}
886
Rob Clark5ee32292014-11-11 19:38:59 -0500887/**
888 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
889 * @dev: DRM device
890 * @old_state: atomic state object with old state structures
891 *
892 * Helper to, after atomic commit, wait for vblanks on all effected
893 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100894 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
895 * framebuffers have actually changed to optimize for the legacy cursor and
896 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500897 */
898void
899drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
900 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200901{
902 struct drm_crtc *crtc;
903 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200904 int i, ret;
905
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300906 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200907 /* No one cares about the old state, so abuse it for tracking
908 * and store whether we hold a vblank reference (and should do a
909 * vblank wait) in the ->enable boolean. */
910 old_crtc_state->enable = false;
911
912 if (!crtc->state->enable)
913 continue;
914
Daniel Vetterf02ad902015-01-22 16:36:23 +0100915 /* Legacy cursor ioctls are completely unsynced, and userspace
916 * relies on that (by doing tons of cursor updates). */
917 if (old_state->legacy_cursor_update)
918 continue;
919
Daniel Vetterab58e332014-11-24 20:42:42 +0100920 if (!framebuffer_changed(dev, old_state, crtc))
921 continue;
922
Daniel Vetter623369e2014-09-16 17:50:47 +0200923 ret = drm_crtc_vblank_get(crtc);
924 if (ret != 0)
925 continue;
926
927 old_crtc_state->enable = true;
928 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
929 }
930
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300931 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
932 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +0200933 continue;
934
935 ret = wait_event_timeout(dev->vblank[i].queue,
936 old_crtc_state->last_vblank_count !=
937 drm_vblank_count(dev, i),
938 msecs_to_jiffies(50));
939
940 drm_crtc_vblank_put(crtc);
941 }
942}
Rob Clark5ee32292014-11-11 19:38:59 -0500943EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +0200944
945/**
946 * drm_atomic_helper_commit - commit validated state object
947 * @dev: DRM device
948 * @state: the driver state object
949 * @async: asynchronous commit
950 *
951 * This function commits a with drm_atomic_helper_check() pre-validated state
952 * object. This can still fail when e.g. the framebuffer reservation fails. For
953 * now this doesn't implement asynchronous commits.
954 *
955 * RETURNS
956 * Zero for success or -errno.
957 */
958int drm_atomic_helper_commit(struct drm_device *dev,
959 struct drm_atomic_state *state,
960 bool async)
961{
962 int ret;
963
964 if (async)
965 return -EBUSY;
966
967 ret = drm_atomic_helper_prepare_planes(dev, state);
968 if (ret)
969 return ret;
970
971 /*
972 * This is the point of no return - everything below never fails except
973 * when the hw goes bonghits. Which means we can commit the new state on
974 * the software side now.
975 */
976
977 drm_atomic_helper_swap_state(dev, state);
978
979 /*
980 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +0800981 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +0200982 * that the asynchronous work has either been cancelled (if the driver
983 * supports it, which at least requires that the framebuffers get
984 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
985 * before the new state gets committed on the software side with
986 * drm_atomic_helper_swap_state().
987 *
988 * This scheme allows new atomic state updates to be prepared and
989 * checked in parallel to the asynchronous completion of the previous
990 * update. Which is important since compositors need to figure out the
991 * composition of the next frame right after having submitted the
992 * current layout.
993 */
994
Daniel Vettere2330f02014-10-29 11:34:56 +0100995 wait_for_fences(dev, state);
996
Daniel Vetter1af434a2015-02-22 12:24:19 +0100997 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200998
999 drm_atomic_helper_commit_planes(dev, state);
1000
Daniel Vetter1af434a2015-02-22 12:24:19 +01001001 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001002
Rob Clark5ee32292014-11-11 19:38:59 -05001003 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001004
1005 drm_atomic_helper_cleanup_planes(dev, state);
1006
1007 drm_atomic_state_free(state);
1008
1009 return 0;
1010}
1011EXPORT_SYMBOL(drm_atomic_helper_commit);
1012
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001013/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001014 * DOC: implementing async commit
1015 *
1016 * For now the atomic helpers don't support async commit directly. If there is
1017 * real need it could be added though, using the dma-buf fence infrastructure
1018 * for generic synchronization with outstanding rendering.
1019 *
1020 * For now drivers have to implement async commit themselves, with the following
1021 * sequence being the recommended one:
1022 *
1023 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1024 * which commit needs to call which can fail, so we want to run it first and
1025 * synchronously.
1026 *
1027 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1028 * might be affected the new state update. This can be done by either cancelling
1029 * or flushing the work items, depending upon whether the driver can deal with
1030 * cancelled updates. Note that it is important to ensure that the framebuffer
1031 * cleanup is still done when cancelling.
1032 *
1033 * For sufficient parallelism it is recommended to have a work item per crtc
1034 * (for updates which don't touch global state) and a global one. Then we only
1035 * need to synchronize with the crtc work items for changed crtcs and the global
1036 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1037 *
1038 * 3. The software state is updated synchronously with
1039 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
1040 * locks means concurrent callers never see inconsistent state. And doing this
1041 * while it's guaranteed that no relevant async worker runs means that async
1042 * workers do not need grab any locks. Actually they must not grab locks, for
1043 * otherwise the work flushing will deadlock.
1044 *
1045 * 4. Schedule a work item to do all subsequent steps, using the split-out
1046 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1047 * then cleaning up the framebuffers after the old framebuffer is no longer
1048 * being displayed.
1049 */
1050
1051/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001052 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001053 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001054 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001055 *
1056 * This function prepares plane state, specifically framebuffers, for the new
1057 * configuration. If any failure is encountered this function will call
1058 * ->cleanup_fb on any already successfully prepared framebuffer.
1059 *
1060 * Returns:
1061 * 0 on success, negative error code on failure.
1062 */
1063int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1064 struct drm_atomic_state *state)
1065{
1066 int nplanes = dev->mode_config.num_total_plane;
1067 int ret, i;
1068
1069 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001070 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001071 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001072 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001073 struct drm_framebuffer *fb;
1074
1075 if (!plane)
1076 continue;
1077
1078 funcs = plane->helper_private;
1079
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001080 fb = plane_state->fb;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001081
1082 if (fb && funcs->prepare_fb) {
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001083 ret = funcs->prepare_fb(plane, fb, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001084 if (ret)
1085 goto fail;
1086 }
1087 }
1088
1089 return 0;
1090
1091fail:
1092 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001093 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001094 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001095 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001096 struct drm_framebuffer *fb;
1097
1098 if (!plane)
1099 continue;
1100
1101 funcs = plane->helper_private;
1102
1103 fb = state->plane_states[i]->fb;
1104
1105 if (fb && funcs->cleanup_fb)
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001106 funcs->cleanup_fb(plane, fb, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001107
1108 }
1109
1110 return ret;
1111}
1112EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1113
1114/**
1115 * drm_atomic_helper_commit_planes - commit plane state
1116 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001117 * @old_state: atomic state object with old state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001118 *
1119 * This function commits the new plane state using the plane and atomic helper
1120 * functions for planes and crtcs. It assumes that the atomic state has already
1121 * been pushed into the relevant object state pointers, since this step can no
1122 * longer fail.
1123 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001124 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001125 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001126 *
1127 * Note that this function does all plane updates across all CRTCs in one step.
1128 * If the hardware can't support this approach look at
1129 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001130 */
1131void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001132 struct drm_atomic_state *old_state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001133{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001134 struct drm_crtc *crtc;
1135 struct drm_crtc_state *old_crtc_state;
1136 struct drm_plane *plane;
1137 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001138 int i;
1139
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001140 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001141 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001142
1143 funcs = crtc->helper_private;
1144
1145 if (!funcs || !funcs->atomic_begin)
1146 continue;
1147
1148 funcs->atomic_begin(crtc);
1149 }
1150
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001151 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001152 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001153
1154 funcs = plane->helper_private;
1155
Thierry Reding3cad4b62014-11-25 13:05:12 +01001156 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001157 continue;
1158
Thierry Reding407b8bd2014-11-20 12:05:50 +01001159 /*
1160 * Special-case disabling the plane if drivers support it.
1161 */
1162 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1163 funcs->atomic_disable)
1164 funcs->atomic_disable(plane, old_plane_state);
Daniel Vetter475d2312015-04-10 16:22:39 +02001165 else if (plane->state->crtc ||
1166 drm_atomic_plane_disabling(plane, old_plane_state))
Thierry Reding407b8bd2014-11-20 12:05:50 +01001167 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001168 }
1169
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001170 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001171 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001172
1173 funcs = crtc->helper_private;
1174
1175 if (!funcs || !funcs->atomic_flush)
1176 continue;
1177
1178 funcs->atomic_flush(crtc);
1179 }
1180}
1181EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1182
1183/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001184 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1185 * @old_crtc_state: atomic state object with the old crtc state
1186 *
1187 * This function commits the new plane state using the plane and atomic helper
1188 * functions for planes on the specific crtc. It assumes that the atomic state
1189 * has already been pushed into the relevant object state pointers, since this
1190 * step can no longer fail.
1191 *
1192 * This function is useful when plane updates should be done crtc-by-crtc
1193 * instead of one global step like drm_atomic_helper_commit_planes() does.
1194 *
1195 * This function can only be savely used when planes are not allowed to move
1196 * between different CRTCs because this function doesn't handle inter-CRTC
1197 * depencies. Callers need to ensure that either no such depencies exist,
1198 * resolve them through ordering of commit calls or through some other means.
1199 */
1200void
1201drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1202{
1203 const struct drm_crtc_helper_funcs *crtc_funcs;
1204 struct drm_crtc *crtc = old_crtc_state->crtc;
1205 struct drm_atomic_state *old_state = old_crtc_state->state;
1206 struct drm_plane *plane;
1207 unsigned plane_mask;
1208
1209 plane_mask = old_crtc_state->plane_mask;
1210 plane_mask |= crtc->state->plane_mask;
1211
1212 crtc_funcs = crtc->helper_private;
1213 if (crtc_funcs && crtc_funcs->atomic_begin)
1214 crtc_funcs->atomic_begin(crtc);
1215
1216 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1217 struct drm_plane_state *old_plane_state =
1218 drm_atomic_get_existing_plane_state(old_state, plane);
1219 const struct drm_plane_helper_funcs *plane_funcs;
1220
1221 plane_funcs = plane->helper_private;
1222
1223 if (!old_plane_state || !plane_funcs)
1224 continue;
1225
1226 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1227
1228 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1229 plane_funcs->atomic_disable)
1230 plane_funcs->atomic_disable(plane, old_plane_state);
1231 else if (plane->state->crtc ||
1232 drm_atomic_plane_disabling(plane, old_plane_state))
1233 plane_funcs->atomic_update(plane, old_plane_state);
1234 }
1235
1236 if (crtc_funcs && crtc_funcs->atomic_flush)
1237 crtc_funcs->atomic_flush(crtc);
1238}
1239EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1240
1241/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001242 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1243 * @dev: DRM device
1244 * @old_state: atomic state object with old state structures
1245 *
1246 * This function cleans up plane state, specifically framebuffers, from the old
1247 * configuration. Hence the old configuration must be perserved in @old_state to
1248 * be able to call this function.
1249 *
1250 * This function must also be called on the new state when the atomic update
1251 * fails at any point after calling drm_atomic_helper_prepare_planes().
1252 */
1253void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1254 struct drm_atomic_state *old_state)
1255{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001256 struct drm_plane *plane;
1257 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001258 int i;
1259
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001260 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001261 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001262 struct drm_framebuffer *old_fb;
1263
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001264 funcs = plane->helper_private;
1265
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001266 old_fb = plane_state->fb;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001267
1268 if (old_fb && funcs->cleanup_fb)
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001269 funcs->cleanup_fb(plane, old_fb, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001270 }
1271}
1272EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1273
1274/**
1275 * drm_atomic_helper_swap_state - store atomic state into current sw state
1276 * @dev: DRM device
1277 * @state: atomic state
1278 *
1279 * This function stores the atomic state into the current state pointers in all
1280 * driver objects. It should be called after all failing steps have been done
1281 * and succeeded, but before the actual hardware state is committed.
1282 *
1283 * For cleanup and error recovery the current state for all changed objects will
1284 * be swaped into @state.
1285 *
1286 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1287 *
1288 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1289 *
1290 * 2. Do any other steps that might fail.
1291 *
1292 * 3. Put the staged state into the current state pointers with this function.
1293 *
1294 * 4. Actually commit the hardware state.
1295 *
1296 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1297 * contains the old state. Also do any other cleanup required with that state.
1298 */
1299void drm_atomic_helper_swap_state(struct drm_device *dev,
1300 struct drm_atomic_state *state)
1301{
1302 int i;
1303
1304 for (i = 0; i < dev->mode_config.num_connector; i++) {
1305 struct drm_connector *connector = state->connectors[i];
1306
1307 if (!connector)
1308 continue;
1309
1310 connector->state->state = state;
1311 swap(state->connector_states[i], connector->state);
1312 connector->state->state = NULL;
1313 }
1314
1315 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1316 struct drm_crtc *crtc = state->crtcs[i];
1317
1318 if (!crtc)
1319 continue;
1320
1321 crtc->state->state = state;
1322 swap(state->crtc_states[i], crtc->state);
1323 crtc->state->state = NULL;
1324 }
1325
1326 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1327 struct drm_plane *plane = state->planes[i];
1328
1329 if (!plane)
1330 continue;
1331
1332 plane->state->state = state;
1333 swap(state->plane_states[i], plane->state);
1334 plane->state->state = NULL;
1335 }
1336}
1337EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001338
1339/**
1340 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1341 * @plane: plane object to update
1342 * @crtc: owning CRTC of owning plane
1343 * @fb: framebuffer to flip onto plane
1344 * @crtc_x: x offset of primary plane on crtc
1345 * @crtc_y: y offset of primary plane on crtc
1346 * @crtc_w: width of primary plane rectangle on crtc
1347 * @crtc_h: height of primary plane rectangle on crtc
1348 * @src_x: x offset of @fb for panning
1349 * @src_y: y offset of @fb for panning
1350 * @src_w: width of source rectangle in @fb
1351 * @src_h: height of source rectangle in @fb
1352 *
1353 * Provides a default plane update handler using the atomic driver interface.
1354 *
1355 * RETURNS:
1356 * Zero on success, error code on failure
1357 */
1358int drm_atomic_helper_update_plane(struct drm_plane *plane,
1359 struct drm_crtc *crtc,
1360 struct drm_framebuffer *fb,
1361 int crtc_x, int crtc_y,
1362 unsigned int crtc_w, unsigned int crtc_h,
1363 uint32_t src_x, uint32_t src_y,
1364 uint32_t src_w, uint32_t src_h)
1365{
1366 struct drm_atomic_state *state;
1367 struct drm_plane_state *plane_state;
1368 int ret = 0;
1369
1370 state = drm_atomic_state_alloc(plane->dev);
1371 if (!state)
1372 return -ENOMEM;
1373
1374 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1375retry:
1376 plane_state = drm_atomic_get_plane_state(state, plane);
1377 if (IS_ERR(plane_state)) {
1378 ret = PTR_ERR(plane_state);
1379 goto fail;
1380 }
1381
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001382 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001383 if (ret != 0)
1384 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001385 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001386 plane_state->crtc_x = crtc_x;
1387 plane_state->crtc_y = crtc_y;
1388 plane_state->crtc_h = crtc_h;
1389 plane_state->crtc_w = crtc_w;
1390 plane_state->src_x = src_x;
1391 plane_state->src_y = src_y;
1392 plane_state->src_h = src_h;
1393 plane_state->src_w = src_w;
1394
Daniel Vetter3671c582015-05-04 15:40:52 +02001395 if (plane == crtc->cursor)
1396 state->legacy_cursor_update = true;
1397
Daniel Vetter042652e2014-07-27 13:46:52 +02001398 ret = drm_atomic_commit(state);
1399 if (ret != 0)
1400 goto fail;
1401
1402 /* Driver takes ownership of state on successful commit. */
1403 return 0;
1404fail:
1405 if (ret == -EDEADLK)
1406 goto backoff;
1407
1408 drm_atomic_state_free(state);
1409
1410 return ret;
1411backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001412 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001413 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001414
1415 /*
1416 * Someone might have exchanged the framebuffer while we dropped locks
1417 * in the backoff code. We need to fix up the fb refcount tracking the
1418 * core does for us.
1419 */
1420 plane->old_fb = plane->fb;
1421
1422 goto retry;
1423}
1424EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1425
1426/**
1427 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1428 * @plane: plane to disable
1429 *
1430 * Provides a default plane disable handler using the atomic driver interface.
1431 *
1432 * RETURNS:
1433 * Zero on success, error code on failure
1434 */
1435int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1436{
1437 struct drm_atomic_state *state;
1438 struct drm_plane_state *plane_state;
1439 int ret = 0;
1440
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001441 /*
1442 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1443 * acquire context. The real fix will be to wire the acquire ctx through
1444 * everywhere we need it, but meanwhile prevent chaos by just skipping
1445 * this noop. The critical case is the cursor ioctls which a) only grab
1446 * crtc/cursor-plane locks (so we need the crtc to get at the right
1447 * acquire context) and b) can try to disable the plane multiple times.
1448 */
1449 if (!plane->crtc)
1450 return 0;
1451
Daniel Vetter042652e2014-07-27 13:46:52 +02001452 state = drm_atomic_state_alloc(plane->dev);
1453 if (!state)
1454 return -ENOMEM;
1455
1456 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1457retry:
1458 plane_state = drm_atomic_get_plane_state(state, plane);
1459 if (IS_ERR(plane_state)) {
1460 ret = PTR_ERR(plane_state);
1461 goto fail;
1462 }
1463
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001464 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001465 if (ret != 0)
1466 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001467 drm_atomic_set_fb_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001468 plane_state->crtc_x = 0;
1469 plane_state->crtc_y = 0;
1470 plane_state->crtc_h = 0;
1471 plane_state->crtc_w = 0;
1472 plane_state->src_x = 0;
1473 plane_state->src_y = 0;
1474 plane_state->src_h = 0;
1475 plane_state->src_w = 0;
1476
Daniel Vetterf02ad902015-01-22 16:36:23 +01001477 if (plane == plane->crtc->cursor)
1478 state->legacy_cursor_update = true;
1479
Daniel Vetter042652e2014-07-27 13:46:52 +02001480 ret = drm_atomic_commit(state);
1481 if (ret != 0)
1482 goto fail;
1483
1484 /* Driver takes ownership of state on successful commit. */
1485 return 0;
1486fail:
1487 if (ret == -EDEADLK)
1488 goto backoff;
1489
1490 drm_atomic_state_free(state);
1491
1492 return ret;
1493backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001494 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001495 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001496
1497 /*
1498 * Someone might have exchanged the framebuffer while we dropped locks
1499 * in the backoff code. We need to fix up the fb refcount tracking the
1500 * core does for us.
1501 */
1502 plane->old_fb = plane->fb;
1503
1504 goto retry;
1505}
1506EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1507
1508static int update_output_state(struct drm_atomic_state *state,
1509 struct drm_mode_set *set)
1510{
1511 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001512 struct drm_crtc *crtc;
1513 struct drm_crtc_state *crtc_state;
1514 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001515 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001516 int ret, i, j;
1517
1518 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1519 state->acquire_ctx);
1520 if (ret)
1521 return ret;
1522
1523 /* First grab all affected connector/crtc states. */
1524 for (i = 0; i < set->num_connectors; i++) {
1525 conn_state = drm_atomic_get_connector_state(state,
1526 set->connectors[i]);
1527 if (IS_ERR(conn_state))
1528 return PTR_ERR(conn_state);
1529 }
1530
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001531 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001532 ret = drm_atomic_add_affected_connectors(state, crtc);
1533 if (ret)
1534 return ret;
1535 }
1536
1537 /* Then recompute connector->crtc links and crtc enabling state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001538 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001539 if (conn_state->crtc == set->crtc) {
1540 ret = drm_atomic_set_crtc_for_connector(conn_state,
1541 NULL);
1542 if (ret)
1543 return ret;
1544 }
1545
1546 for (j = 0; j < set->num_connectors; j++) {
1547 if (set->connectors[j] == connector) {
1548 ret = drm_atomic_set_crtc_for_connector(conn_state,
1549 set->crtc);
1550 if (ret)
1551 return ret;
1552 break;
1553 }
1554 }
1555 }
1556
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001557 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001558 /* Don't update ->enable for the CRTC in the set_config request,
1559 * since a mismatch would indicate a bug in the upper layers.
1560 * The actual modeset code later on will catch any
1561 * inconsistencies here. */
1562 if (crtc == set->crtc)
1563 continue;
1564
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001565 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1566 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1567 NULL);
1568 if (ret < 0)
1569 return ret;
1570
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001571 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001572 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001573 }
1574
1575 return 0;
1576}
1577
1578/**
1579 * drm_atomic_helper_set_config - set a new config from userspace
1580 * @set: mode set configuration
1581 *
1582 * Provides a default crtc set_config handler using the atomic driver interface.
1583 *
1584 * Returns:
1585 * Returns 0 on success, negative errno numbers on failure.
1586 */
1587int drm_atomic_helper_set_config(struct drm_mode_set *set)
1588{
1589 struct drm_atomic_state *state;
1590 struct drm_crtc *crtc = set->crtc;
1591 struct drm_crtc_state *crtc_state;
1592 struct drm_plane_state *primary_state;
1593 int ret = 0;
1594
1595 state = drm_atomic_state_alloc(crtc->dev);
1596 if (!state)
1597 return -ENOMEM;
1598
1599 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1600retry:
1601 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1602 if (IS_ERR(crtc_state)) {
1603 ret = PTR_ERR(crtc_state);
1604 goto fail;
1605 }
1606
Rob Clarke5b53412014-11-26 18:58:04 -05001607 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1608 if (IS_ERR(primary_state)) {
1609 ret = PTR_ERR(primary_state);
1610 goto fail;
1611 }
1612
Daniel Vetter042652e2014-07-27 13:46:52 +02001613 if (!set->mode) {
1614 WARN_ON(set->fb);
1615 WARN_ON(set->num_connectors);
1616
Daniel Stone819364d2015-05-26 14:36:48 +01001617 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1618 if (ret != 0)
1619 goto fail;
1620
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001621 crtc_state->active = false;
Rob Clarke5b53412014-11-26 18:58:04 -05001622
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001623 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
Rob Clarke5b53412014-11-26 18:58:04 -05001624 if (ret != 0)
1625 goto fail;
1626
1627 drm_atomic_set_fb_for_plane(primary_state, NULL);
1628
Daniel Vetter042652e2014-07-27 13:46:52 +02001629 goto commit;
1630 }
1631
1632 WARN_ON(!set->fb);
1633 WARN_ON(!set->num_connectors);
1634
Daniel Stone819364d2015-05-26 14:36:48 +01001635 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1636 if (ret != 0)
1637 goto fail;
1638
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001639 crtc_state->active = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02001640
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001641 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001642 if (ret != 0)
1643 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001644 drm_atomic_set_fb_for_plane(primary_state, set->fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001645 primary_state->crtc_x = 0;
1646 primary_state->crtc_y = 0;
1647 primary_state->crtc_h = set->mode->vdisplay;
1648 primary_state->crtc_w = set->mode->hdisplay;
1649 primary_state->src_x = set->x << 16;
1650 primary_state->src_y = set->y << 16;
1651 primary_state->src_h = set->mode->vdisplay << 16;
1652 primary_state->src_w = set->mode->hdisplay << 16;
1653
1654commit:
1655 ret = update_output_state(state, set);
1656 if (ret)
1657 goto fail;
1658
1659 ret = drm_atomic_commit(state);
1660 if (ret != 0)
1661 goto fail;
1662
1663 /* Driver takes ownership of state on successful commit. */
1664 return 0;
1665fail:
1666 if (ret == -EDEADLK)
1667 goto backoff;
1668
1669 drm_atomic_state_free(state);
1670
1671 return ret;
1672backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001673 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001674 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001675
1676 /*
1677 * Someone might have exchanged the framebuffer while we dropped locks
1678 * in the backoff code. We need to fix up the fb refcount tracking the
1679 * core does for us.
1680 */
1681 crtc->primary->old_fb = crtc->primary->fb;
1682
1683 goto retry;
1684}
1685EXPORT_SYMBOL(drm_atomic_helper_set_config);
1686
1687/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02001688 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02001689 * @crtc: DRM crtc
1690 * @property: DRM property
1691 * @val: value of property
1692 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02001693 * Provides a default crtc set_property handler using the atomic driver
1694 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02001695 *
1696 * RETURNS:
1697 * Zero on success, error code on failure
1698 */
1699int
1700drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1701 struct drm_property *property,
1702 uint64_t val)
1703{
1704 struct drm_atomic_state *state;
1705 struct drm_crtc_state *crtc_state;
1706 int ret = 0;
1707
1708 state = drm_atomic_state_alloc(crtc->dev);
1709 if (!state)
1710 return -ENOMEM;
1711
1712 /* ->set_property is always called with all locks held. */
1713 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1714retry:
1715 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1716 if (IS_ERR(crtc_state)) {
1717 ret = PTR_ERR(crtc_state);
1718 goto fail;
1719 }
1720
Rob Clark40ecc692014-12-18 16:01:46 -05001721 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1722 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001723 if (ret)
1724 goto fail;
1725
1726 ret = drm_atomic_commit(state);
1727 if (ret != 0)
1728 goto fail;
1729
1730 /* Driver takes ownership of state on successful commit. */
1731 return 0;
1732fail:
1733 if (ret == -EDEADLK)
1734 goto backoff;
1735
1736 drm_atomic_state_free(state);
1737
1738 return ret;
1739backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001740 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001741 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001742
1743 goto retry;
1744}
1745EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1746
1747/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02001748 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02001749 * @plane: DRM plane
1750 * @property: DRM property
1751 * @val: value of property
1752 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02001753 * Provides a default plane set_property handler using the atomic driver
1754 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02001755 *
1756 * RETURNS:
1757 * Zero on success, error code on failure
1758 */
1759int
1760drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1761 struct drm_property *property,
1762 uint64_t val)
1763{
1764 struct drm_atomic_state *state;
1765 struct drm_plane_state *plane_state;
1766 int ret = 0;
1767
1768 state = drm_atomic_state_alloc(plane->dev);
1769 if (!state)
1770 return -ENOMEM;
1771
1772 /* ->set_property is always called with all locks held. */
1773 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1774retry:
1775 plane_state = drm_atomic_get_plane_state(state, plane);
1776 if (IS_ERR(plane_state)) {
1777 ret = PTR_ERR(plane_state);
1778 goto fail;
1779 }
1780
Rob Clark40ecc692014-12-18 16:01:46 -05001781 ret = drm_atomic_plane_set_property(plane, plane_state,
1782 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001783 if (ret)
1784 goto fail;
1785
1786 ret = drm_atomic_commit(state);
1787 if (ret != 0)
1788 goto fail;
1789
1790 /* Driver takes ownership of state on successful commit. */
1791 return 0;
1792fail:
1793 if (ret == -EDEADLK)
1794 goto backoff;
1795
1796 drm_atomic_state_free(state);
1797
1798 return ret;
1799backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001800 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001801 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001802
1803 goto retry;
1804}
1805EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1806
1807/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02001808 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02001809 * @connector: DRM connector
1810 * @property: DRM property
1811 * @val: value of property
1812 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02001813 * Provides a default connector set_property handler using the atomic driver
1814 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02001815 *
1816 * RETURNS:
1817 * Zero on success, error code on failure
1818 */
1819int
1820drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1821 struct drm_property *property,
1822 uint64_t val)
1823{
1824 struct drm_atomic_state *state;
1825 struct drm_connector_state *connector_state;
1826 int ret = 0;
1827
1828 state = drm_atomic_state_alloc(connector->dev);
1829 if (!state)
1830 return -ENOMEM;
1831
1832 /* ->set_property is always called with all locks held. */
1833 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1834retry:
1835 connector_state = drm_atomic_get_connector_state(state, connector);
1836 if (IS_ERR(connector_state)) {
1837 ret = PTR_ERR(connector_state);
1838 goto fail;
1839 }
1840
Rob Clark40ecc692014-12-18 16:01:46 -05001841 ret = drm_atomic_connector_set_property(connector, connector_state,
1842 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001843 if (ret)
1844 goto fail;
1845
1846 ret = drm_atomic_commit(state);
1847 if (ret != 0)
1848 goto fail;
1849
1850 /* Driver takes ownership of state on successful commit. */
1851 return 0;
1852fail:
1853 if (ret == -EDEADLK)
1854 goto backoff;
1855
1856 drm_atomic_state_free(state);
1857
1858 return ret;
1859backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001860 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001861 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001862
1863 goto retry;
1864}
1865EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001866
1867/**
1868 * drm_atomic_helper_page_flip - execute a legacy page flip
1869 * @crtc: DRM crtc
1870 * @fb: DRM framebuffer
1871 * @event: optional DRM event to signal upon completion
1872 * @flags: flip flags for non-vblank sync'ed updates
1873 *
1874 * Provides a default page flip implementation using the atomic driver interface.
1875 *
1876 * Note that for now so called async page flips (i.e. updates which are not
1877 * synchronized to vblank) are not supported, since the atomic interfaces have
1878 * no provisions for this yet.
1879 *
1880 * Returns:
1881 * Returns 0 on success, negative errno numbers on failure.
1882 */
1883int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1884 struct drm_framebuffer *fb,
1885 struct drm_pending_vblank_event *event,
1886 uint32_t flags)
1887{
1888 struct drm_plane *plane = crtc->primary;
1889 struct drm_atomic_state *state;
1890 struct drm_plane_state *plane_state;
1891 struct drm_crtc_state *crtc_state;
1892 int ret = 0;
1893
1894 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1895 return -EINVAL;
1896
1897 state = drm_atomic_state_alloc(plane->dev);
1898 if (!state)
1899 return -ENOMEM;
1900
1901 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1902retry:
1903 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1904 if (IS_ERR(crtc_state)) {
1905 ret = PTR_ERR(crtc_state);
1906 goto fail;
1907 }
1908 crtc_state->event = event;
1909
1910 plane_state = drm_atomic_get_plane_state(state, plane);
1911 if (IS_ERR(plane_state)) {
1912 ret = PTR_ERR(plane_state);
1913 goto fail;
1914 }
1915
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001916 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001917 if (ret != 0)
1918 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001919 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001920
1921 ret = drm_atomic_async_commit(state);
1922 if (ret != 0)
1923 goto fail;
1924
1925 /* TODO: ->page_flip is the only driver callback where the core
1926 * doesn't update plane->fb. For now patch it up here. */
1927 plane->fb = plane->state->fb;
1928
1929 /* Driver takes ownership of state on successful async commit. */
1930 return 0;
1931fail:
1932 if (ret == -EDEADLK)
1933 goto backoff;
1934
1935 drm_atomic_state_free(state);
1936
1937 return ret;
1938backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001939 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001940 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001941
1942 /*
1943 * Someone might have exchanged the framebuffer while we dropped locks
1944 * in the backoff code. We need to fix up the fb refcount tracking the
1945 * core does for us.
1946 */
1947 plane->old_fb = plane->fb;
1948
1949 goto retry;
1950}
1951EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01001952
1953/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01001954 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
1955 * @connector: affected connector
1956 * @mode: DPMS mode
1957 *
1958 * This is the main helper function provided by the atomic helper framework for
1959 * implementing the legacy DPMS connector interface. It computes the new desired
1960 * ->active state for the corresponding CRTC (if the connector is enabled) and
1961 * updates it.
1962 */
1963void drm_atomic_helper_connector_dpms(struct drm_connector *connector,
1964 int mode)
1965{
1966 struct drm_mode_config *config = &connector->dev->mode_config;
1967 struct drm_atomic_state *state;
1968 struct drm_crtc_state *crtc_state;
1969 struct drm_crtc *crtc;
1970 struct drm_connector *tmp_connector;
1971 int ret;
1972 bool active = false;
1973
1974 if (mode != DRM_MODE_DPMS_ON)
1975 mode = DRM_MODE_DPMS_OFF;
1976
1977 connector->dpms = mode;
1978 crtc = connector->state->crtc;
1979
1980 if (!crtc)
1981 return;
1982
1983 /* FIXME: ->dpms has no return value so can't forward the -ENOMEM. */
1984 state = drm_atomic_state_alloc(connector->dev);
1985 if (!state)
1986 return;
1987
1988 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1989retry:
1990 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1991 if (IS_ERR(crtc_state))
1992 return;
1993
1994 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
1995
1996 list_for_each_entry(tmp_connector, &config->connector_list, head) {
John Hunter0388df02015-03-17 15:30:28 +08001997 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01001998 continue;
1999
John Hunter0388df02015-03-17 15:30:28 +08002000 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002001 active = true;
2002 break;
2003 }
2004 }
2005 crtc_state->active = active;
2006
2007 ret = drm_atomic_commit(state);
2008 if (ret != 0)
2009 goto fail;
2010
2011 /* Driver takes ownership of state on successful async commit. */
2012 return;
2013fail:
2014 if (ret == -EDEADLK)
2015 goto backoff;
2016
2017 drm_atomic_state_free(state);
2018
2019 WARN(1, "Driver bug: Changing ->active failed with ret=%i\n", ret);
2020
2021 return;
2022backoff:
2023 drm_atomic_state_clear(state);
2024 drm_atomic_legacy_backoff(state);
2025
2026 goto retry;
2027}
2028EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2029
2030/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002031 * DOC: atomic state reset and initialization
2032 *
2033 * Both the drm core and the atomic helpers assume that there is always the full
2034 * and correct atomic software state for all connectors, CRTCs and planes
2035 * available. Which is a bit a problem on driver load and also after system
2036 * suspend. One way to solve this is to have a hardware state read-out
2037 * infrastructure which reconstructs the full software state (e.g. the i915
2038 * driver).
2039 *
2040 * The simpler solution is to just reset the software state to everything off,
2041 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2042 * the atomic helpers provide default reset implementations for all hooks.
2043 */
2044
2045/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002046 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2047 * @crtc: drm CRTC
2048 *
2049 * Resets the atomic state for @crtc by freeing the state pointer (which might
2050 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2051 */
2052void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2053{
Daniel Stone99cf4a22015-05-25 19:11:51 +01002054 if (crtc->state && crtc->state->mode_blob)
2055 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002056 kfree(crtc->state);
2057 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002058
2059 if (crtc->state)
2060 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002061}
2062EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2063
2064/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002065 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2066 * @crtc: CRTC object
2067 * @state: atomic CRTC state
2068 *
2069 * Copies atomic state from a CRTC's current state and resets inferred values.
2070 * This is useful for drivers that subclass the CRTC state.
2071 */
2072void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2073 struct drm_crtc_state *state)
2074{
2075 memcpy(state, crtc->state, sizeof(*state));
2076
Daniel Stone99cf4a22015-05-25 19:11:51 +01002077 if (state->mode_blob)
2078 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002079 state->mode_changed = false;
2080 state->active_changed = false;
2081 state->planes_changed = false;
2082 state->event = NULL;
2083}
2084EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2085
2086/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002087 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2088 * @crtc: drm CRTC
2089 *
2090 * Default CRTC state duplicate hook for drivers which don't have their own
2091 * subclassed CRTC state structure.
2092 */
2093struct drm_crtc_state *
2094drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2095{
2096 struct drm_crtc_state *state;
2097
2098 if (WARN_ON(!crtc->state))
2099 return NULL;
2100
Thierry Redingf5e78402015-01-28 14:54:32 +01002101 state = kmalloc(sizeof(*state), GFP_KERNEL);
2102 if (state)
2103 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002104
2105 return state;
2106}
2107EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2108
2109/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002110 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2111 * @crtc: CRTC object
2112 * @state: CRTC state object to release
2113 *
2114 * Releases all resources stored in the CRTC state without actually freeing
2115 * the memory of the CRTC state. This is useful for drivers that subclass the
2116 * CRTC state.
2117 */
2118void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2119 struct drm_crtc_state *state)
2120{
Daniel Stone99cf4a22015-05-25 19:11:51 +01002121 if (state->mode_blob)
2122 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002123}
2124EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2125
2126/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002127 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2128 * @crtc: drm CRTC
2129 * @state: CRTC state object to release
2130 *
2131 * Default CRTC state destroy hook for drivers which don't have their own
2132 * subclassed CRTC state structure.
2133 */
2134void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2135 struct drm_crtc_state *state)
2136{
Thierry Redingf5e78402015-01-28 14:54:32 +01002137 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002138 kfree(state);
2139}
2140EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2141
2142/**
2143 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2144 * @plane: drm plane
2145 *
2146 * Resets the atomic state for @plane by freeing the state pointer (which might
2147 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2148 */
2149void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2150{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002151 if (plane->state && plane->state->fb)
2152 drm_framebuffer_unreference(plane->state->fb);
2153
Daniel Vetterd4617012014-11-03 15:56:43 +01002154 kfree(plane->state);
2155 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002156
2157 if (plane->state)
2158 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01002159}
2160EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2161
2162/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002163 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2164 * @plane: plane object
2165 * @state: atomic plane state
2166 *
2167 * Copies atomic state from a plane's current state. This is useful for
2168 * drivers that subclass the plane state.
2169 */
2170void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2171 struct drm_plane_state *state)
2172{
2173 memcpy(state, plane->state, sizeof(*state));
2174
2175 if (state->fb)
2176 drm_framebuffer_reference(state->fb);
2177}
2178EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2179
2180/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002181 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2182 * @plane: drm plane
2183 *
2184 * Default plane state duplicate hook for drivers which don't have their own
2185 * subclassed plane state structure.
2186 */
2187struct drm_plane_state *
2188drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2189{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002190 struct drm_plane_state *state;
2191
Daniel Vetterd4617012014-11-03 15:56:43 +01002192 if (WARN_ON(!plane->state))
2193 return NULL;
2194
Thierry Redingf5e78402015-01-28 14:54:32 +01002195 state = kmalloc(sizeof(*state), GFP_KERNEL);
2196 if (state)
2197 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002198
2199 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002200}
2201EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2202
2203/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002204 * __drm_atomic_helper_plane_destroy_state - release plane state
2205 * @plane: plane object
2206 * @state: plane state object to release
2207 *
2208 * Releases all resources stored in the plane state without actually freeing
2209 * the memory of the plane state. This is useful for drivers that subclass the
2210 * plane state.
2211 */
2212void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2213 struct drm_plane_state *state)
2214{
2215 if (state->fb)
2216 drm_framebuffer_unreference(state->fb);
2217}
2218EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2219
2220/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002221 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2222 * @plane: drm plane
2223 * @state: plane state object to release
2224 *
2225 * Default plane state destroy hook for drivers which don't have their own
2226 * subclassed plane state structure.
2227 */
2228void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002229 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002230{
Thierry Redingf5e78402015-01-28 14:54:32 +01002231 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002232 kfree(state);
2233}
2234EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2235
2236/**
2237 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2238 * @connector: drm connector
2239 *
2240 * Resets the atomic state for @connector by freeing the state pointer (which
2241 * might be NULL, e.g. at driver load time) and allocating a new empty state
2242 * object.
2243 */
2244void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2245{
2246 kfree(connector->state);
2247 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002248
2249 if (connector->state)
2250 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002251}
2252EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2253
2254/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002255 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2256 * @connector: connector object
2257 * @state: atomic connector state
2258 *
2259 * Copies atomic state from a connector's current state. This is useful for
2260 * drivers that subclass the connector state.
2261 */
2262void
2263__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2264 struct drm_connector_state *state)
2265{
2266 memcpy(state, connector->state, sizeof(*state));
2267}
2268EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2269
2270/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002271 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2272 * @connector: drm connector
2273 *
2274 * Default connector state duplicate hook for drivers which don't have their own
2275 * subclassed connector state structure.
2276 */
2277struct drm_connector_state *
2278drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2279{
Thierry Redingf5e78402015-01-28 14:54:32 +01002280 struct drm_connector_state *state;
2281
Daniel Vetterd4617012014-11-03 15:56:43 +01002282 if (WARN_ON(!connector->state))
2283 return NULL;
2284
Thierry Redingf5e78402015-01-28 14:54:32 +01002285 state = kmalloc(sizeof(*state), GFP_KERNEL);
2286 if (state)
2287 __drm_atomic_helper_connector_duplicate_state(connector, state);
2288
2289 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002290}
2291EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2292
2293/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002294 * __drm_atomic_helper_connector_destroy_state - release connector state
2295 * @connector: connector object
2296 * @state: connector state object to release
2297 *
2298 * Releases all resources stored in the connector state without actually
2299 * freeing the memory of the connector state. This is useful for drivers that
2300 * subclass the connector state.
2301 */
2302void
2303__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2304 struct drm_connector_state *state)
2305{
2306 /*
2307 * This is currently a placeholder so that drivers that subclass the
2308 * state will automatically do the right thing if code is ever added
2309 * to this function.
2310 */
2311}
2312EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2313
2314/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002315 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2316 * @connector: drm connector
2317 * @state: connector state object to release
2318 *
2319 * Default connector state destroy hook for drivers which don't have their own
2320 * subclassed connector state structure.
2321 */
2322void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2323 struct drm_connector_state *state)
2324{
Thierry Redingf5e78402015-01-28 14:54:32 +01002325 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002326 kfree(state);
2327}
2328EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);