blob: d9053ebf49032ab4e2eda47a8fbc5af93cedd3d2 [file] [log] [blame]
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28#include <drm/drmP.h>
29#include <drm/drm_atomic.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020032#include <drm/drm_atomic_helper.h>
Daniel Vettere2330f02014-10-29 11:34:56 +010033#include <linux/fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Daniel Vetter3150c7d2014-11-06 20:53:29 +010035/**
36 * DOC: overview
37 *
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
43 *
44 * This library also provides default implementations for the check callback in
Daniel Vetter26196f72015-08-25 16:26:03 +020045 * drm_atomic_helper_check() and for the commit callback with
46 * drm_atomic_helper_commit(). But the individual stages and callbacks are
47 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
Daniel Vetter3150c7d2014-11-06 20:53:29 +010048 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
Daniel Vetter26196f72015-08-25 16:26:03 +020051 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
52 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
Daniel Vetter3150c7d2014-11-06 20:53:29 +010053 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
55 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010056static void
57drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
58 struct drm_plane_state *plane_state,
59 struct drm_plane *plane)
60{
61 struct drm_crtc_state *crtc_state;
62
63 if (plane->state->crtc) {
Rob Clark4b08eae2014-12-08 10:45:43 -050064 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
Daniel Vetterc2fcd272014-11-05 00:14:14 +010065
66 if (WARN_ON(!crtc_state))
67 return;
68
69 crtc_state->planes_changed = true;
70 }
71
72 if (plane_state->crtc) {
73 crtc_state =
74 state->crtc_states[drm_crtc_index(plane_state->crtc)];
75
76 if (WARN_ON(!crtc_state))
77 return;
78
79 crtc_state->planes_changed = true;
80 }
81}
82
Daniel 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
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +020092 drm_for_each_connector(connector, dev) {
Daniel Vetter623369e2014-09-16 17:50:47 +020093 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
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200127 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200128
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];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200177 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200178 }
179
180 if (connector_state->crtc) {
181 idx = drm_crtc_index(connector_state->crtc);
182
183 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200184 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200185 }
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
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100213 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
214 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
215 new_encoder->base.id,
216 new_encoder->name,
217 connector_state->crtc->base.id);
218 return -EINVAL;
219 }
220
Daniel Vetter623369e2014-09-16 17:50:47 +0200221 if (new_encoder == connector_state->best_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100222 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
223 connector->base.id,
224 connector->name,
225 new_encoder->base.id,
226 new_encoder->name,
227 connector_state->crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200228
229 return 0;
230 }
231
232 encoder_crtc = get_current_crtc_for_encoder(state->dev,
233 new_encoder);
234
235 if (encoder_crtc) {
236 ret = steal_encoder(state, new_encoder, encoder_crtc);
237 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100238 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
239 connector->base.id,
240 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200241 return ret;
242 }
243 }
244
Daniel Vetter6ea76f3c2015-08-03 17:24:11 +0200245 if (WARN_ON(!connector_state->crtc))
246 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200247
Daniel Vetter623369e2014-09-16 17:50:47 +0200248 connector_state->best_encoder = new_encoder;
249 idx = drm_crtc_index(connector_state->crtc);
250
251 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200252 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200253
Daniel Vetter17a38d92015-02-22 12:24:16 +0100254 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
255 connector->base.id,
256 connector->name,
257 new_encoder->base.id,
258 new_encoder->name,
259 connector_state->crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200260
261 return 0;
262}
263
264static int
265mode_fixup(struct drm_atomic_state *state)
266{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300267 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200268 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300269 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200270 struct drm_connector_state *conn_state;
271 int i;
272 bool ret;
273
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300274 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200275 if (!crtc_state->mode_changed &&
276 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200277 continue;
278
279 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
280 }
281
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300282 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200283 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200284 struct drm_encoder *encoder;
285
Daniel Vetter623369e2014-09-16 17:50:47 +0200286 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
287
288 if (!conn_state->crtc || !conn_state->best_encoder)
289 continue;
290
291 crtc_state =
292 state->crtc_states[drm_crtc_index(conn_state->crtc)];
293
294 /*
295 * Each encoder has at most one connector (since we always steal
296 * it away), so we won't call ->mode_fixup twice.
297 */
298 encoder = conn_state->best_encoder;
299 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300300 if (!funcs)
301 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200302
Archit Taneja862e6862015-05-21 11:03:16 +0530303 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
304 &crtc_state->adjusted_mode);
305 if (!ret) {
306 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
307 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200308 }
309
Thierry Reding4cd4df82014-12-03 16:44:34 +0100310 if (funcs->atomic_check) {
311 ret = funcs->atomic_check(encoder, crtc_state,
312 conn_state);
313 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100314 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
315 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100316 return ret;
317 }
Inki Dae84524912015-08-11 21:23:49 +0900318 } else if (funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100319 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
320 &crtc_state->adjusted_mode);
321 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100322 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
323 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100324 return -EINVAL;
325 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200326 }
327 }
328
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300329 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200330 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200331
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200332 if (!crtc_state->mode_changed &&
333 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200334 continue;
335
336 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300337 if (!funcs->mode_fixup)
338 continue;
339
Daniel Vetter623369e2014-09-16 17:50:47 +0200340 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
341 &crtc_state->adjusted_mode);
342 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100343 DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
344 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200345 return -EINVAL;
346 }
347 }
348
349 return 0;
350}
351
Daniel Vetterd9b13622014-11-26 16:57:41 +0100352/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800353 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100354 * @dev: DRM device
355 * @state: the driver state object
356 *
357 * Check the state object to see if the requested state is physically possible.
358 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200359 * update and adds any additional connectors needed for full modesets and calls
360 * down into ->mode_fixup functions of the driver backend.
361 *
362 * crtc_state->mode_changed is set when the input mode is changed.
363 * crtc_state->connectors_changed is set when a connector is added or
364 * removed from the crtc.
365 * crtc_state->active_changed is set when crtc_state->active changes,
366 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100367 *
368 * IMPORTANT:
369 *
370 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
371 * plane update can't be done without a full modeset) _must_ call this function
372 * afterwards after that change. It is permitted to call this function multiple
373 * times for the same update, e.g. when the ->atomic_check functions depend upon
374 * the adjusted dotclock for fifo space allocation and watermark computation.
375 *
376 * RETURNS
377 * Zero for success or -errno
378 */
379int
Rob Clark934ce1c2014-11-19 16:41:33 -0500380drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200381 struct drm_atomic_state *state)
382{
Daniel Vetter623369e2014-09-16 17:50:47 +0200383 struct drm_crtc *crtc;
384 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300385 struct drm_connector *connector;
386 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200387 int i, ret;
388
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300389 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200390 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100391 DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
392 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200393 crtc_state->mode_changed = true;
394 }
395
396 if (crtc->state->enable != crtc_state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100397 DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
398 crtc->base.id);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200399
400 /*
401 * For clarity this assignment is done here, but
402 * enable == 0 is only true when there are no
403 * connectors and a NULL mode.
404 *
405 * The other way around is true as well. enable != 0
406 * iff connectors are attached and a mode is set.
407 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200408 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200409 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200410 }
411 }
412
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300413 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200414 /*
415 * This only sets crtc->mode_changed for routing changes,
416 * drivers must set crtc->mode_changed themselves when connector
417 * properties need to be updated.
418 */
419 ret = update_connector_routing(state, i);
420 if (ret)
421 return ret;
422 }
423
424 /*
425 * After all the routing has been prepared we need to add in any
426 * connector which is itself unchanged, but who's crtc changes it's
427 * configuration. This must be done before calling mode_fixup in case a
428 * crtc only changed its mode but has the same set of connectors.
429 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300430 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200431 int num_connectors;
432
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100433 /*
434 * We must set ->active_changed after walking connectors for
435 * otherwise an update that only changes active would result in
436 * a full modeset because update_connector_routing force that.
437 */
438 if (crtc->state->active != crtc_state->active) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100439 DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
440 crtc->base.id);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100441 crtc_state->active_changed = true;
442 }
443
Daniel Vetter2465ff62015-06-18 09:58:55 +0200444 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100445 continue;
446
Daniel Vetter17a38d92015-02-22 12:24:16 +0100447 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
448 crtc->base.id,
449 crtc_state->enable ? 'y' : 'n',
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100450 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200451
452 ret = drm_atomic_add_affected_connectors(state, crtc);
453 if (ret != 0)
454 return ret;
455
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200456 ret = drm_atomic_add_affected_planes(state, crtc);
457 if (ret != 0)
458 return ret;
459
Daniel Vetter623369e2014-09-16 17:50:47 +0200460 num_connectors = drm_atomic_connectors_for_crtc(state,
461 crtc);
462
463 if (crtc_state->enable != !!num_connectors) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100464 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
465 crtc->base.id);
Daniel Vetter623369e2014-09-16 17:50:47 +0200466
467 return -EINVAL;
468 }
469 }
470
471 return mode_fixup(state);
472}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100473EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200474
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100475/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800476 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100477 * @dev: DRM device
478 * @state: the driver state object
479 *
480 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100481 * This does all the plane update related checks using by calling into the
482 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100483 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200484 * It also sets crtc_state->planes_changed to indicate that a crtc has
485 * updated planes.
486 *
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100487 * RETURNS
488 * Zero for success or -errno
489 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100490int
491drm_atomic_helper_check_planes(struct drm_device *dev,
492 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100493{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300494 struct drm_crtc *crtc;
495 struct drm_crtc_state *crtc_state;
496 struct drm_plane *plane;
497 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100498 int i, ret = 0;
499
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300500 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200501 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100502
503 funcs = plane->helper_private;
504
505 drm_atomic_helper_plane_changed(state, plane_state, plane);
506
507 if (!funcs || !funcs->atomic_check)
508 continue;
509
510 ret = funcs->atomic_check(plane, plane_state);
511 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100512 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
513 plane->base.id);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100514 return ret;
515 }
516 }
517
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300518 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200519 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100520
521 funcs = crtc->helper_private;
522
523 if (!funcs || !funcs->atomic_check)
524 continue;
525
526 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
527 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100528 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
529 crtc->base.id);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100530 return ret;
531 }
532 }
533
Daniel Vetterd9b13622014-11-26 16:57:41 +0100534 return ret;
535}
536EXPORT_SYMBOL(drm_atomic_helper_check_planes);
537
538/**
539 * drm_atomic_helper_check - validate state object
540 * @dev: DRM device
541 * @state: the driver state object
542 *
543 * Check the state object to see if the requested state is physically possible.
544 * Only crtcs and planes have check callbacks, so for any additional (global)
545 * checking that a driver needs it can simply wrap that around this function.
546 * Drivers without such needs can directly use this as their ->atomic_check()
547 * callback.
548 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100549 * This just wraps the two parts of the state checking for planes and modeset
550 * state in the default order: First it calls drm_atomic_helper_check_modeset()
551 * and then drm_atomic_helper_check_planes(). The assumption is that the
552 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
553 * e.g. properly compute watermarks.
554 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100555 * RETURNS
556 * Zero for success or -errno
557 */
558int drm_atomic_helper_check(struct drm_device *dev,
559 struct drm_atomic_state *state)
560{
561 int ret;
562
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100563 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100564 if (ret)
565 return ret;
566
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100567 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500568 if (ret)
569 return ret;
570
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100571 return ret;
572}
573EXPORT_SYMBOL(drm_atomic_helper_check);
574
Daniel Vetter623369e2014-09-16 17:50:47 +0200575static void
576disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
577{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300578 struct drm_connector *connector;
579 struct drm_connector_state *old_conn_state;
580 struct drm_crtc *crtc;
581 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200582 int i;
583
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300584 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200585 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200586 struct drm_encoder *encoder;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100587 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200588
Daniel Vetter623369e2014-09-16 17:50:47 +0200589 /* Shut down everything that's in the changeset and currently
590 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300591 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200592 continue;
593
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100594 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
595
Daniel Vetter4218a322015-03-26 22:18:40 +0100596 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200597 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100598 continue;
599
Rob Clark46df9ad2014-11-20 15:40:36 -0500600 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200601
Rob Clark46df9ad2014-11-20 15:40:36 -0500602 /* We shouldn't get this far if we didn't previously have
603 * an encoder.. but WARN_ON() rather than explode.
604 */
605 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200606 continue;
607
608 funcs = encoder->helper_private;
609
Daniel Vetter17a38d92015-02-22 12:24:16 +0100610 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
611 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100612
Daniel Vetter623369e2014-09-16 17:50:47 +0200613 /*
614 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800615 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200616 */
Archit Taneja862e6862015-05-21 11:03:16 +0530617 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200618
619 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100620 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200621 funcs->prepare(encoder);
622 else if (funcs->disable)
623 funcs->disable(encoder);
624 else
625 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
626
Archit Taneja862e6862015-05-21 11:03:16 +0530627 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200628 }
629
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300630 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200631 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200632
633 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200634 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100635 continue;
636
637 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200638 continue;
639
640 funcs = crtc->helper_private;
641
Daniel Vetter17a38d92015-02-22 12:24:16 +0100642 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
643 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100644
645
Daniel Vetter623369e2014-09-16 17:50:47 +0200646 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100647 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200648 funcs->prepare(crtc);
649 else if (funcs->disable)
650 funcs->disable(crtc);
651 else
652 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
653 }
654}
655
Daniel Vetter4c18d302015-05-12 15:27:37 +0200656/**
657 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
658 * @dev: DRM device
659 * @old_state: atomic state object with old state structures
660 *
661 * This function updates all the various legacy modeset state pointers in
662 * connectors, encoders and crtcs. It also updates the timestamping constants
663 * used for precise vblank timestamps by calling
664 * drm_calc_timestamping_constants().
665 *
666 * Drivers can use this for building their own atomic commit if they don't have
667 * a pure helper-based modeset implementation.
668 */
669void
670drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
671 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200672{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300673 struct drm_connector *connector;
674 struct drm_connector_state *old_conn_state;
675 struct drm_crtc *crtc;
676 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200677 int i;
678
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200679 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300680 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200681 if (connector->encoder) {
682 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200683
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200684 connector->encoder->crtc = NULL;
685 connector->encoder = NULL;
686 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200687
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200688 crtc = connector->state->crtc;
689 if ((!crtc && old_conn_state->crtc) ||
690 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
691 struct drm_property *dpms_prop =
692 dev->mode_config.dpms_property;
693 int mode = DRM_MODE_DPMS_OFF;
694
695 if (crtc && crtc->state->active)
696 mode = DRM_MODE_DPMS_ON;
697
698 connector->dpms = mode;
699 drm_object_property_set_value(&connector->base,
700 dpms_prop, mode);
701 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200702 }
703
704 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300705 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
706 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200707 continue;
708
709 if (WARN_ON(!connector->state->best_encoder))
710 continue;
711
712 connector->encoder = connector->state->best_encoder;
713 connector->encoder->crtc = connector->state->crtc;
714 }
715
716 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300717 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200718 struct drm_plane *primary = crtc->primary;
719
Daniel Vetter623369e2014-09-16 17:50:47 +0200720 crtc->mode = crtc->state->mode;
721 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200722
723 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
724 primary->state->crtc == crtc) {
725 crtc->x = primary->state->src_x >> 16;
726 crtc->y = primary->state->src_y >> 16;
727 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200728
729 if (crtc->state->enable)
730 drm_calc_timestamping_constants(crtc,
731 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200732 }
733}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200734EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200735
736static void
737crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
738{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300739 struct drm_crtc *crtc;
740 struct drm_crtc_state *old_crtc_state;
741 struct drm_connector *connector;
742 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200743 int i;
744
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300745 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200746 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200747
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300748 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200749 continue;
750
751 funcs = crtc->helper_private;
752
Daniel Vetterc982bd92015-02-22 12:24:20 +0100753 if (crtc->state->enable && funcs->mode_set_nofb) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100754 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
755 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100756
Daniel Vetter623369e2014-09-16 17:50:47 +0200757 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100758 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200759 }
760
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300761 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200762 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200763 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200764 struct drm_encoder *encoder;
765 struct drm_display_mode *mode, *adjusted_mode;
766
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300767 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200768 continue;
769
770 encoder = connector->state->best_encoder;
771 funcs = encoder->helper_private;
772 new_crtc_state = connector->state->crtc->state;
773 mode = &new_crtc_state->mode;
774 adjusted_mode = &new_crtc_state->adjusted_mode;
775
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100776 if (!new_crtc_state->mode_changed)
777 continue;
778
Daniel Vetter17a38d92015-02-22 12:24:16 +0100779 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
780 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100781
Daniel Vetter623369e2014-09-16 17:50:47 +0200782 /*
783 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800784 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200785 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100786 if (funcs->mode_set)
787 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200788
Archit Taneja862e6862015-05-21 11:03:16 +0530789 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200790 }
791}
792
793/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100794 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200795 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100796 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200797 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100798 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200799 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100800 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300801 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100802 * drm_atomic_helper_commit_planes(), which is what the default commit function
803 * does. But drivers with different needs can group the modeset commits together
804 * and do the plane commits at the end. This is useful for drivers doing runtime
805 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200806 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100807void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
808 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200809{
Laurent Pincharta072f802015-02-22 12:24:18 +0100810 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200811
812 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
813
Laurent Pincharta072f802015-02-22 12:24:18 +0100814 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200815}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100816EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200817
818/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100819 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200820 * @dev: DRM device
821 * @old_state: atomic state object with old state structures
822 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100823 * This function enables all the outputs with the new configuration which had to
824 * be turned off for the update.
825 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300826 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100827 * drm_atomic_helper_commit_planes(), which is what the default commit function
828 * does. But drivers with different needs can group the modeset commits together
829 * and do the plane commits at the end. This is useful for drivers doing runtime
830 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200831 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100832void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
833 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200834{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300835 struct drm_crtc *crtc;
836 struct drm_crtc_state *old_crtc_state;
837 struct drm_connector *connector;
838 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200839 int i;
840
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300841 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200842 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200843
844 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200845 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100846 continue;
847
848 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200849 continue;
850
851 funcs = crtc->helper_private;
852
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100853 if (crtc->state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100854 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
855 crtc->base.id);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100856
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100857 if (funcs->enable)
858 funcs->enable(crtc);
859 else
860 funcs->commit(crtc);
861 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200862 }
863
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300864 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200865 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200866 struct drm_encoder *encoder;
867
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300868 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200869 continue;
870
Daniel Vetter4218a322015-03-26 22:18:40 +0100871 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200872 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100873 continue;
874
Daniel Vetter623369e2014-09-16 17:50:47 +0200875 encoder = connector->state->best_encoder;
876 funcs = encoder->helper_private;
877
Daniel Vetter17a38d92015-02-22 12:24:16 +0100878 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
879 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100880
Daniel Vetter623369e2014-09-16 17:50:47 +0200881 /*
882 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800883 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200884 */
Archit Taneja862e6862015-05-21 11:03:16 +0530885 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200886
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100887 if (funcs->enable)
888 funcs->enable(encoder);
889 else
890 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200891
Archit Taneja862e6862015-05-21 11:03:16 +0530892 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200893 }
894}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100895EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200896
Daniel Vettere2330f02014-10-29 11:34:56 +0100897static void wait_for_fences(struct drm_device *dev,
898 struct drm_atomic_state *state)
899{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300900 struct drm_plane *plane;
901 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +0100902 int i;
903
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300904 for_each_plane_in_state(state, plane, plane_state, i) {
905 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +0100906 continue;
907
908 WARN_ON(!plane->state->fb);
909
910 fence_wait(plane->state->fence, false);
911 fence_put(plane->state->fence);
912 plane->state->fence = NULL;
913 }
914}
915
Daniel Vetterab58e332014-11-24 20:42:42 +0100916static bool framebuffer_changed(struct drm_device *dev,
917 struct drm_atomic_state *old_state,
918 struct drm_crtc *crtc)
919{
920 struct drm_plane *plane;
921 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +0100922 int i;
923
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300924 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +0100925 if (plane->state->crtc != crtc &&
926 old_plane_state->crtc != crtc)
927 continue;
928
929 if (plane->state->fb != old_plane_state->fb)
930 return true;
931 }
932
933 return false;
934}
935
Rob Clark5ee32292014-11-11 19:38:59 -0500936/**
937 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
938 * @dev: DRM device
939 * @old_state: atomic state object with old state structures
940 *
941 * Helper to, after atomic commit, wait for vblanks on all effected
942 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100943 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
944 * framebuffers have actually changed to optimize for the legacy cursor and
945 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500946 */
947void
948drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
949 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200950{
951 struct drm_crtc *crtc;
952 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200953 int i, ret;
954
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300955 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200956 /* No one cares about the old state, so abuse it for tracking
957 * and store whether we hold a vblank reference (and should do a
958 * vblank wait) in the ->enable boolean. */
959 old_crtc_state->enable = false;
960
961 if (!crtc->state->enable)
962 continue;
963
Daniel Vetterf02ad902015-01-22 16:36:23 +0100964 /* Legacy cursor ioctls are completely unsynced, and userspace
965 * relies on that (by doing tons of cursor updates). */
966 if (old_state->legacy_cursor_update)
967 continue;
968
Daniel Vetterab58e332014-11-24 20:42:42 +0100969 if (!framebuffer_changed(dev, old_state, crtc))
970 continue;
971
Daniel Vetter623369e2014-09-16 17:50:47 +0200972 ret = drm_crtc_vblank_get(crtc);
973 if (ret != 0)
974 continue;
975
976 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +0200977 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200978 }
979
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300980 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
981 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +0200982 continue;
983
984 ret = wait_event_timeout(dev->vblank[i].queue,
985 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +0200986 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +0200987 msecs_to_jiffies(50));
988
989 drm_crtc_vblank_put(crtc);
990 }
991}
Rob Clark5ee32292014-11-11 19:38:59 -0500992EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +0200993
994/**
995 * drm_atomic_helper_commit - commit validated state object
996 * @dev: DRM device
997 * @state: the driver state object
998 * @async: asynchronous commit
999 *
1000 * This function commits a with drm_atomic_helper_check() pre-validated state
1001 * object. This can still fail when e.g. the framebuffer reservation fails. For
1002 * now this doesn't implement asynchronous commits.
1003 *
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001004 * Note that right now this function does not support async commits, and hence
1005 * driver writers must implement their own version for now. Also note that the
1006 * default ordering of how the various stages are called is to match the legacy
1007 * modeset helper library closest. One peculiarity of that is that it doesn't
1008 * mesh well with runtime PM at all.
1009 *
1010 * For drivers supporting runtime PM the recommended sequence is
1011 *
1012 * drm_atomic_helper_commit_modeset_disables(dev, state);
1013 *
1014 * drm_atomic_helper_commit_modeset_enables(dev, state);
1015 *
1016 * drm_atomic_helper_commit_planes(dev, state, true);
1017 *
1018 * See the kerneldoc entries for these three functions for more details.
1019 *
Daniel Vetter623369e2014-09-16 17:50:47 +02001020 * RETURNS
1021 * Zero for success or -errno.
1022 */
1023int drm_atomic_helper_commit(struct drm_device *dev,
1024 struct drm_atomic_state *state,
1025 bool async)
1026{
1027 int ret;
1028
1029 if (async)
1030 return -EBUSY;
1031
1032 ret = drm_atomic_helper_prepare_planes(dev, state);
1033 if (ret)
1034 return ret;
1035
1036 /*
1037 * This is the point of no return - everything below never fails except
1038 * when the hw goes bonghits. Which means we can commit the new state on
1039 * the software side now.
1040 */
1041
1042 drm_atomic_helper_swap_state(dev, state);
1043
1044 /*
1045 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001046 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001047 * that the asynchronous work has either been cancelled (if the driver
1048 * supports it, which at least requires that the framebuffers get
1049 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1050 * before the new state gets committed on the software side with
1051 * drm_atomic_helper_swap_state().
1052 *
1053 * This scheme allows new atomic state updates to be prepared and
1054 * checked in parallel to the asynchronous completion of the previous
1055 * update. Which is important since compositors need to figure out the
1056 * composition of the next frame right after having submitted the
1057 * current layout.
1058 */
1059
Daniel Vettere2330f02014-10-29 11:34:56 +01001060 wait_for_fences(dev, state);
1061
Daniel Vetter1af434a2015-02-22 12:24:19 +01001062 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001063
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001064 drm_atomic_helper_commit_planes(dev, state, false);
Daniel Vetter623369e2014-09-16 17:50:47 +02001065
Daniel Vetter1af434a2015-02-22 12:24:19 +01001066 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001067
Rob Clark5ee32292014-11-11 19:38:59 -05001068 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001069
1070 drm_atomic_helper_cleanup_planes(dev, state);
1071
1072 drm_atomic_state_free(state);
1073
1074 return 0;
1075}
1076EXPORT_SYMBOL(drm_atomic_helper_commit);
1077
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001078/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001079 * DOC: implementing async commit
1080 *
1081 * For now the atomic helpers don't support async commit directly. If there is
1082 * real need it could be added though, using the dma-buf fence infrastructure
1083 * for generic synchronization with outstanding rendering.
1084 *
1085 * For now drivers have to implement async commit themselves, with the following
1086 * sequence being the recommended one:
1087 *
1088 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1089 * which commit needs to call which can fail, so we want to run it first and
1090 * synchronously.
1091 *
1092 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1093 * might be affected the new state update. This can be done by either cancelling
1094 * or flushing the work items, depending upon whether the driver can deal with
1095 * cancelled updates. Note that it is important to ensure that the framebuffer
1096 * cleanup is still done when cancelling.
1097 *
1098 * For sufficient parallelism it is recommended to have a work item per crtc
1099 * (for updates which don't touch global state) and a global one. Then we only
1100 * need to synchronize with the crtc work items for changed crtcs and the global
1101 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1102 *
1103 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001104 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001105 * locks means concurrent callers never see inconsistent state. And doing this
1106 * while it's guaranteed that no relevant async worker runs means that async
1107 * workers do not need grab any locks. Actually they must not grab locks, for
1108 * otherwise the work flushing will deadlock.
1109 *
1110 * 4. Schedule a work item to do all subsequent steps, using the split-out
1111 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1112 * then cleaning up the framebuffers after the old framebuffer is no longer
1113 * being displayed.
1114 */
1115
1116/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001117 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001118 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001119 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001120 *
1121 * This function prepares plane state, specifically framebuffers, for the new
1122 * configuration. If any failure is encountered this function will call
1123 * ->cleanup_fb on any already successfully prepared framebuffer.
1124 *
1125 * Returns:
1126 * 0 on success, negative error code on failure.
1127 */
1128int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1129 struct drm_atomic_state *state)
1130{
1131 int nplanes = dev->mode_config.num_total_plane;
1132 int ret, i;
1133
1134 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001135 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001136 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001137 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001138
1139 if (!plane)
1140 continue;
1141
1142 funcs = plane->helper_private;
1143
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001144 if (funcs->prepare_fb) {
1145 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001146 if (ret)
1147 goto fail;
1148 }
1149 }
1150
1151 return 0;
1152
1153fail:
1154 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001155 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001156 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001157 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001158
1159 if (!plane)
1160 continue;
1161
1162 funcs = plane->helper_private;
1163
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001164 if (funcs->cleanup_fb)
1165 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001166
1167 }
1168
1169 return ret;
1170}
1171EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1172
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001173bool plane_crtc_active(struct drm_plane_state *state)
1174{
1175 return state->crtc && state->crtc->state->active;
1176}
1177
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001178/**
1179 * drm_atomic_helper_commit_planes - commit plane state
1180 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001181 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001182 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001183 *
1184 * This function commits the new plane state using the plane and atomic helper
1185 * functions for planes and crtcs. It assumes that the atomic state has already
1186 * been pushed into the relevant object state pointers, since this step can no
1187 * longer fail.
1188 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001189 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001190 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001191 *
1192 * Note that this function does all plane updates across all CRTCs in one step.
1193 * If the hardware can't support this approach look at
1194 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001195 *
1196 * Plane parameters can be updated by applications while the associated CRTC is
1197 * disabled. The DRM/KMS core will store the parameters in the plane state,
1198 * which will be available to the driver when the CRTC is turned on. As a result
1199 * most drivers don't need to be immediately notified of plane updates for a
1200 * disabled CRTC.
1201 *
1202 * Unless otherwise needed, drivers are advised to set the @active_only
1203 * parameters to true in order not to receive plane update notifications related
1204 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1205 * driver code when the driver and/or hardware can't or just don't need to deal
1206 * with updates on disabled CRTCs, for example when supporting runtime PM.
1207 *
1208 * The drm_atomic_helper_commit() default implementation only sets @active_only
1209 * to false to most closely match the behaviour of the legacy helpers. This should
1210 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001211 */
1212void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001213 struct drm_atomic_state *old_state,
1214 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001215{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001216 struct drm_crtc *crtc;
1217 struct drm_crtc_state *old_crtc_state;
1218 struct drm_plane *plane;
1219 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001220 int i;
1221
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001222 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001223 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001224
1225 funcs = crtc->helper_private;
1226
1227 if (!funcs || !funcs->atomic_begin)
1228 continue;
1229
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001230 if (active_only && !crtc->state->active)
1231 continue;
1232
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001233 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001234 }
1235
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001236 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001237 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001238 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001239
1240 funcs = plane->helper_private;
1241
Thierry Reding3cad4b62014-11-25 13:05:12 +01001242 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001243 continue;
1244
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001245 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1246
1247 if (active_only) {
1248 /*
1249 * Skip planes related to inactive CRTCs. If the plane
1250 * is enabled use the state of the current CRTC. If the
1251 * plane is being disabled use the state of the old
1252 * CRTC to avoid skipping planes being disabled on an
1253 * active CRTC.
1254 */
1255 if (!disabling && !plane_crtc_active(plane->state))
1256 continue;
1257 if (disabling && !plane_crtc_active(old_plane_state))
1258 continue;
1259 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001260
Thierry Reding407b8bd2014-11-20 12:05:50 +01001261 /*
1262 * Special-case disabling the plane if drivers support it.
1263 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001264 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001265 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001266 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001267 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001268 }
1269
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001270 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001271 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001272
1273 funcs = crtc->helper_private;
1274
1275 if (!funcs || !funcs->atomic_flush)
1276 continue;
1277
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001278 if (active_only && !crtc->state->active)
1279 continue;
1280
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001281 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001282 }
1283}
1284EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1285
1286/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001287 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1288 * @old_crtc_state: atomic state object with the old crtc state
1289 *
1290 * This function commits the new plane state using the plane and atomic helper
1291 * functions for planes on the specific crtc. It assumes that the atomic state
1292 * has already been pushed into the relevant object state pointers, since this
1293 * step can no longer fail.
1294 *
1295 * This function is useful when plane updates should be done crtc-by-crtc
1296 * instead of one global step like drm_atomic_helper_commit_planes() does.
1297 *
1298 * This function can only be savely used when planes are not allowed to move
1299 * between different CRTCs because this function doesn't handle inter-CRTC
1300 * depencies. Callers need to ensure that either no such depencies exist,
1301 * resolve them through ordering of commit calls or through some other means.
1302 */
1303void
1304drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1305{
1306 const struct drm_crtc_helper_funcs *crtc_funcs;
1307 struct drm_crtc *crtc = old_crtc_state->crtc;
1308 struct drm_atomic_state *old_state = old_crtc_state->state;
1309 struct drm_plane *plane;
1310 unsigned plane_mask;
1311
1312 plane_mask = old_crtc_state->plane_mask;
1313 plane_mask |= crtc->state->plane_mask;
1314
1315 crtc_funcs = crtc->helper_private;
1316 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001317 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001318
1319 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1320 struct drm_plane_state *old_plane_state =
1321 drm_atomic_get_existing_plane_state(old_state, plane);
1322 const struct drm_plane_helper_funcs *plane_funcs;
1323
1324 plane_funcs = plane->helper_private;
1325
1326 if (!old_plane_state || !plane_funcs)
1327 continue;
1328
1329 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1330
1331 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1332 plane_funcs->atomic_disable)
1333 plane_funcs->atomic_disable(plane, old_plane_state);
1334 else if (plane->state->crtc ||
1335 drm_atomic_plane_disabling(plane, old_plane_state))
1336 plane_funcs->atomic_update(plane, old_plane_state);
1337 }
1338
1339 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001340 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001341}
1342EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1343
1344/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001345 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1346 * @crtc: CRTC
1347 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1348 *
1349 * Disables all planes associated with the given CRTC. This can be
1350 * used for instance in the CRTC helper disable callback to disable
1351 * all planes before shutting down the display pipeline.
1352 *
1353 * If the atomic-parameter is set the function calls the CRTC's
1354 * atomic_begin hook before and atomic_flush hook after disabling the
1355 * planes.
1356 *
1357 * It is a bug to call this function without having implemented the
1358 * ->atomic_disable() plane hook.
1359 */
1360void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1361 bool atomic)
1362{
1363 const struct drm_crtc_helper_funcs *crtc_funcs =
1364 crtc->helper_private;
1365 struct drm_plane *plane;
1366
1367 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1368 crtc_funcs->atomic_begin(crtc, NULL);
1369
1370 drm_for_each_plane(plane, crtc->dev) {
1371 const struct drm_plane_helper_funcs *plane_funcs =
1372 plane->helper_private;
1373
1374 if (plane->state->crtc != crtc || !plane_funcs)
1375 continue;
1376
1377 WARN_ON(!plane_funcs->atomic_disable);
1378 if (plane_funcs->atomic_disable)
1379 plane_funcs->atomic_disable(plane, NULL);
1380 }
1381
1382 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1383 crtc_funcs->atomic_flush(crtc, NULL);
1384}
1385EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1386
1387/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001388 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1389 * @dev: DRM device
1390 * @old_state: atomic state object with old state structures
1391 *
1392 * This function cleans up plane state, specifically framebuffers, from the old
1393 * configuration. Hence the old configuration must be perserved in @old_state to
1394 * be able to call this function.
1395 *
1396 * This function must also be called on the new state when the atomic update
1397 * fails at any point after calling drm_atomic_helper_prepare_planes().
1398 */
1399void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1400 struct drm_atomic_state *old_state)
1401{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001402 struct drm_plane *plane;
1403 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001404 int i;
1405
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001406 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001407 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001408
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001409 funcs = plane->helper_private;
1410
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001411 if (funcs->cleanup_fb)
1412 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001413 }
1414}
1415EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1416
1417/**
1418 * drm_atomic_helper_swap_state - store atomic state into current sw state
1419 * @dev: DRM device
1420 * @state: atomic state
1421 *
1422 * This function stores the atomic state into the current state pointers in all
1423 * driver objects. It should be called after all failing steps have been done
1424 * and succeeded, but before the actual hardware state is committed.
1425 *
1426 * For cleanup and error recovery the current state for all changed objects will
1427 * be swaped into @state.
1428 *
1429 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1430 *
1431 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1432 *
1433 * 2. Do any other steps that might fail.
1434 *
1435 * 3. Put the staged state into the current state pointers with this function.
1436 *
1437 * 4. Actually commit the hardware state.
1438 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001439 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001440 * contains the old state. Also do any other cleanup required with that state.
1441 */
1442void drm_atomic_helper_swap_state(struct drm_device *dev,
1443 struct drm_atomic_state *state)
1444{
1445 int i;
1446
1447 for (i = 0; i < dev->mode_config.num_connector; i++) {
1448 struct drm_connector *connector = state->connectors[i];
1449
1450 if (!connector)
1451 continue;
1452
1453 connector->state->state = state;
1454 swap(state->connector_states[i], connector->state);
1455 connector->state->state = NULL;
1456 }
1457
1458 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1459 struct drm_crtc *crtc = state->crtcs[i];
1460
1461 if (!crtc)
1462 continue;
1463
1464 crtc->state->state = state;
1465 swap(state->crtc_states[i], crtc->state);
1466 crtc->state->state = NULL;
1467 }
1468
1469 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1470 struct drm_plane *plane = state->planes[i];
1471
1472 if (!plane)
1473 continue;
1474
1475 plane->state->state = state;
1476 swap(state->plane_states[i], plane->state);
1477 plane->state->state = NULL;
1478 }
1479}
1480EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001481
1482/**
1483 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1484 * @plane: plane object to update
1485 * @crtc: owning CRTC of owning plane
1486 * @fb: framebuffer to flip onto plane
1487 * @crtc_x: x offset of primary plane on crtc
1488 * @crtc_y: y offset of primary plane on crtc
1489 * @crtc_w: width of primary plane rectangle on crtc
1490 * @crtc_h: height of primary plane rectangle on crtc
1491 * @src_x: x offset of @fb for panning
1492 * @src_y: y offset of @fb for panning
1493 * @src_w: width of source rectangle in @fb
1494 * @src_h: height of source rectangle in @fb
1495 *
1496 * Provides a default plane update handler using the atomic driver interface.
1497 *
1498 * RETURNS:
1499 * Zero on success, error code on failure
1500 */
1501int drm_atomic_helper_update_plane(struct drm_plane *plane,
1502 struct drm_crtc *crtc,
1503 struct drm_framebuffer *fb,
1504 int crtc_x, int crtc_y,
1505 unsigned int crtc_w, unsigned int crtc_h,
1506 uint32_t src_x, uint32_t src_y,
1507 uint32_t src_w, uint32_t src_h)
1508{
1509 struct drm_atomic_state *state;
1510 struct drm_plane_state *plane_state;
1511 int ret = 0;
1512
1513 state = drm_atomic_state_alloc(plane->dev);
1514 if (!state)
1515 return -ENOMEM;
1516
1517 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1518retry:
1519 plane_state = drm_atomic_get_plane_state(state, plane);
1520 if (IS_ERR(plane_state)) {
1521 ret = PTR_ERR(plane_state);
1522 goto fail;
1523 }
1524
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001525 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001526 if (ret != 0)
1527 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001528 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001529 plane_state->crtc_x = crtc_x;
1530 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001531 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001532 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001533 plane_state->src_x = src_x;
1534 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001535 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001536 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001537
Daniel Vetter3671c582015-05-04 15:40:52 +02001538 if (plane == crtc->cursor)
1539 state->legacy_cursor_update = true;
1540
Daniel Vetter042652e2014-07-27 13:46:52 +02001541 ret = drm_atomic_commit(state);
1542 if (ret != 0)
1543 goto fail;
1544
1545 /* Driver takes ownership of state on successful commit. */
1546 return 0;
1547fail:
1548 if (ret == -EDEADLK)
1549 goto backoff;
1550
1551 drm_atomic_state_free(state);
1552
1553 return ret;
1554backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001555 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001556 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001557
1558 /*
1559 * Someone might have exchanged the framebuffer while we dropped locks
1560 * in the backoff code. We need to fix up the fb refcount tracking the
1561 * core does for us.
1562 */
1563 plane->old_fb = plane->fb;
1564
1565 goto retry;
1566}
1567EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1568
1569/**
1570 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1571 * @plane: plane to disable
1572 *
1573 * Provides a default plane disable handler using the atomic driver interface.
1574 *
1575 * RETURNS:
1576 * Zero on success, error code on failure
1577 */
1578int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1579{
1580 struct drm_atomic_state *state;
1581 struct drm_plane_state *plane_state;
1582 int ret = 0;
1583
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001584 /*
1585 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1586 * acquire context. The real fix will be to wire the acquire ctx through
1587 * everywhere we need it, but meanwhile prevent chaos by just skipping
1588 * this noop. The critical case is the cursor ioctls which a) only grab
1589 * crtc/cursor-plane locks (so we need the crtc to get at the right
1590 * acquire context) and b) can try to disable the plane multiple times.
1591 */
1592 if (!plane->crtc)
1593 return 0;
1594
Daniel Vetter042652e2014-07-27 13:46:52 +02001595 state = drm_atomic_state_alloc(plane->dev);
1596 if (!state)
1597 return -ENOMEM;
1598
1599 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1600retry:
1601 plane_state = drm_atomic_get_plane_state(state, plane);
1602 if (IS_ERR(plane_state)) {
1603 ret = PTR_ERR(plane_state);
1604 goto fail;
1605 }
1606
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01001607 if (plane_state->crtc && (plane == plane->crtc->cursor))
1608 plane_state->state->legacy_cursor_update = true;
1609
Rob Clarkbbb1e522015-08-25 15:35:58 -04001610 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001611 if (ret != 0)
1612 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01001613
Daniel Vetter042652e2014-07-27 13:46:52 +02001614 ret = drm_atomic_commit(state);
1615 if (ret != 0)
1616 goto fail;
1617
1618 /* Driver takes ownership of state on successful commit. */
1619 return 0;
1620fail:
1621 if (ret == -EDEADLK)
1622 goto backoff;
1623
1624 drm_atomic_state_free(state);
1625
1626 return ret;
1627backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001628 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001629 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001630
1631 /*
1632 * Someone might have exchanged the framebuffer while we dropped locks
1633 * in the backoff code. We need to fix up the fb refcount tracking the
1634 * core does for us.
1635 */
1636 plane->old_fb = plane->fb;
1637
1638 goto retry;
1639}
1640EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1641
Rob Clarkbbb1e522015-08-25 15:35:58 -04001642/* just used from fb-helper and atomic-helper: */
1643int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1644 struct drm_plane_state *plane_state)
1645{
1646 int ret;
1647
1648 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1649 if (ret != 0)
1650 return ret;
1651
1652 drm_atomic_set_fb_for_plane(plane_state, NULL);
1653 plane_state->crtc_x = 0;
1654 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001655 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001656 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001657 plane_state->src_x = 0;
1658 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001659 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001660 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001661
Rob Clarkbbb1e522015-08-25 15:35:58 -04001662 return 0;
1663}
1664
Daniel Vetter042652e2014-07-27 13:46:52 +02001665static int update_output_state(struct drm_atomic_state *state,
1666 struct drm_mode_set *set)
1667{
1668 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001669 struct drm_crtc *crtc;
1670 struct drm_crtc_state *crtc_state;
1671 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001672 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001673 int ret, i, j;
1674
1675 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1676 state->acquire_ctx);
1677 if (ret)
1678 return ret;
1679
1680 /* First grab all affected connector/crtc states. */
1681 for (i = 0; i < set->num_connectors; i++) {
1682 conn_state = drm_atomic_get_connector_state(state,
1683 set->connectors[i]);
1684 if (IS_ERR(conn_state))
1685 return PTR_ERR(conn_state);
1686 }
1687
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001688 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001689 ret = drm_atomic_add_affected_connectors(state, crtc);
1690 if (ret)
1691 return ret;
1692 }
1693
1694 /* Then recompute connector->crtc links and crtc enabling state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001695 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001696 if (conn_state->crtc == set->crtc) {
1697 ret = drm_atomic_set_crtc_for_connector(conn_state,
1698 NULL);
1699 if (ret)
1700 return ret;
1701 }
1702
1703 for (j = 0; j < set->num_connectors; j++) {
1704 if (set->connectors[j] == connector) {
1705 ret = drm_atomic_set_crtc_for_connector(conn_state,
1706 set->crtc);
1707 if (ret)
1708 return ret;
1709 break;
1710 }
1711 }
1712 }
1713
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001714 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001715 /* Don't update ->enable for the CRTC in the set_config request,
1716 * since a mismatch would indicate a bug in the upper layers.
1717 * The actual modeset code later on will catch any
1718 * inconsistencies here. */
1719 if (crtc == set->crtc)
1720 continue;
1721
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001722 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1723 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1724 NULL);
1725 if (ret < 0)
1726 return ret;
1727
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001728 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001729 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001730 }
1731
1732 return 0;
1733}
1734
1735/**
1736 * drm_atomic_helper_set_config - set a new config from userspace
1737 * @set: mode set configuration
1738 *
1739 * Provides a default crtc set_config handler using the atomic driver interface.
1740 *
1741 * Returns:
1742 * Returns 0 on success, negative errno numbers on failure.
1743 */
1744int drm_atomic_helper_set_config(struct drm_mode_set *set)
1745{
1746 struct drm_atomic_state *state;
1747 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02001748 int ret = 0;
1749
1750 state = drm_atomic_state_alloc(crtc->dev);
1751 if (!state)
1752 return -ENOMEM;
1753
1754 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1755retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04001756 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01001757 if (ret != 0)
1758 goto fail;
1759
Daniel Vetter042652e2014-07-27 13:46:52 +02001760 ret = drm_atomic_commit(state);
1761 if (ret != 0)
1762 goto fail;
1763
1764 /* Driver takes ownership of state on successful commit. */
1765 return 0;
1766fail:
1767 if (ret == -EDEADLK)
1768 goto backoff;
1769
1770 drm_atomic_state_free(state);
1771
1772 return ret;
1773backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001774 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001775 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001776
1777 /*
1778 * Someone might have exchanged the framebuffer while we dropped locks
1779 * in the backoff code. We need to fix up the fb refcount tracking the
1780 * core does for us.
1781 */
1782 crtc->primary->old_fb = crtc->primary->fb;
1783
1784 goto retry;
1785}
1786EXPORT_SYMBOL(drm_atomic_helper_set_config);
1787
Rob Clarkbbb1e522015-08-25 15:35:58 -04001788/* just used from fb-helper and atomic-helper: */
1789int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1790 struct drm_atomic_state *state)
1791{
1792 struct drm_crtc_state *crtc_state;
1793 struct drm_plane_state *primary_state;
1794 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02001795 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001796 int ret;
1797
1798 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1799 if (IS_ERR(crtc_state))
1800 return PTR_ERR(crtc_state);
1801
1802 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1803 if (IS_ERR(primary_state))
1804 return PTR_ERR(primary_state);
1805
1806 if (!set->mode) {
1807 WARN_ON(set->fb);
1808 WARN_ON(set->num_connectors);
1809
1810 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1811 if (ret != 0)
1812 return ret;
1813
1814 crtc_state->active = false;
1815
1816 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1817 if (ret != 0)
1818 return ret;
1819
1820 drm_atomic_set_fb_for_plane(primary_state, NULL);
1821
1822 goto commit;
1823 }
1824
1825 WARN_ON(!set->fb);
1826 WARN_ON(!set->num_connectors);
1827
1828 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1829 if (ret != 0)
1830 return ret;
1831
1832 crtc_state->active = true;
1833
1834 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1835 if (ret != 0)
1836 return ret;
1837
Ville Syrjälä83926112015-11-16 17:02:34 +02001838 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1839
Rob Clarkbbb1e522015-08-25 15:35:58 -04001840 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1841 primary_state->crtc_x = 0;
1842 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02001843 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001844 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001845 primary_state->src_x = set->x << 16;
1846 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001847 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02001848 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001849 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001850 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02001851 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001852 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001853 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04001854
1855commit:
1856 ret = update_output_state(state, set);
1857 if (ret)
1858 return ret;
1859
1860 return 0;
1861}
1862
Daniel Vetter042652e2014-07-27 13:46:52 +02001863/**
Thierry Reding14942762015-12-02 17:50:04 +01001864 * drm_atomic_helper_disable_all - disable all currently active outputs
1865 * @dev: DRM device
1866 * @ctx: lock acquisition context
1867 *
1868 * Loops through all connectors, finding those that aren't turned off and then
1869 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1870 * that they are connected to.
1871 *
1872 * This is used for example in suspend/resume to disable all currently active
1873 * functions when suspending.
1874 *
1875 * Note that if callers haven't already acquired all modeset locks this might
1876 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1877 *
1878 * Returns:
1879 * 0 on success or a negative error code on failure.
1880 *
1881 * See also:
1882 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1883 */
1884int drm_atomic_helper_disable_all(struct drm_device *dev,
1885 struct drm_modeset_acquire_ctx *ctx)
1886{
1887 struct drm_atomic_state *state;
1888 struct drm_connector *conn;
1889 int err;
1890
1891 state = drm_atomic_state_alloc(dev);
1892 if (!state)
1893 return -ENOMEM;
1894
1895 state->acquire_ctx = ctx;
1896
1897 drm_for_each_connector(conn, dev) {
1898 struct drm_crtc *crtc = conn->state->crtc;
1899 struct drm_crtc_state *crtc_state;
1900
1901 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1902 continue;
1903
1904 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1905 if (IS_ERR(crtc_state)) {
1906 err = PTR_ERR(crtc_state);
1907 goto free;
1908 }
1909
1910 crtc_state->active = false;
1911 }
1912
1913 err = drm_atomic_commit(state);
1914
1915free:
1916 if (err < 0)
1917 drm_atomic_state_free(state);
1918
1919 return err;
1920}
1921EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1922
1923/**
1924 * drm_atomic_helper_suspend - subsystem-level suspend helper
1925 * @dev: DRM device
1926 *
1927 * Duplicates the current atomic state, disables all active outputs and then
1928 * returns a pointer to the original atomic state to the caller. Drivers can
1929 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1930 * restore the output configuration that was active at the time the system
1931 * entered suspend.
1932 *
1933 * Note that it is potentially unsafe to use this. The atomic state object
1934 * returned by this function is assumed to be persistent. Drivers must ensure
1935 * that this holds true. Before calling this function, drivers must make sure
1936 * to suspend fbdev emulation so that nothing can be using the device.
1937 *
1938 * Returns:
1939 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1940 * encoded error code on failure. Drivers should store the returned atomic
1941 * state object and pass it to the drm_atomic_helper_resume() helper upon
1942 * resume.
1943 *
1944 * See also:
1945 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1946 * drm_atomic_helper_resume()
1947 */
1948struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1949{
1950 struct drm_modeset_acquire_ctx ctx;
1951 struct drm_atomic_state *state;
1952 int err;
1953
1954 drm_modeset_acquire_init(&ctx, 0);
1955
1956retry:
1957 err = drm_modeset_lock_all_ctx(dev, &ctx);
1958 if (err < 0) {
1959 state = ERR_PTR(err);
1960 goto unlock;
1961 }
1962
1963 state = drm_atomic_helper_duplicate_state(dev, &ctx);
1964 if (IS_ERR(state))
1965 goto unlock;
1966
1967 err = drm_atomic_helper_disable_all(dev, &ctx);
1968 if (err < 0) {
1969 drm_atomic_state_free(state);
1970 state = ERR_PTR(err);
1971 goto unlock;
1972 }
1973
1974unlock:
1975 if (PTR_ERR(state) == -EDEADLK) {
1976 drm_modeset_backoff(&ctx);
1977 goto retry;
1978 }
1979
1980 drm_modeset_drop_locks(&ctx);
1981 drm_modeset_acquire_fini(&ctx);
1982 return state;
1983}
1984EXPORT_SYMBOL(drm_atomic_helper_suspend);
1985
1986/**
1987 * drm_atomic_helper_resume - subsystem-level resume helper
1988 * @dev: DRM device
1989 * @state: atomic state to resume to
1990 *
1991 * Calls drm_mode_config_reset() to synchronize hardware and software states,
1992 * grabs all modeset locks and commits the atomic state object. This can be
1993 * used in conjunction with the drm_atomic_helper_suspend() helper to
1994 * implement suspend/resume for drivers that support atomic mode-setting.
1995 *
1996 * Returns:
1997 * 0 on success or a negative error code on failure.
1998 *
1999 * See also:
2000 * drm_atomic_helper_suspend()
2001 */
2002int drm_atomic_helper_resume(struct drm_device *dev,
2003 struct drm_atomic_state *state)
2004{
2005 struct drm_mode_config *config = &dev->mode_config;
2006 int err;
2007
2008 drm_mode_config_reset(dev);
2009 drm_modeset_lock_all(dev);
2010 state->acquire_ctx = config->acquire_ctx;
2011 err = drm_atomic_commit(state);
2012 drm_modeset_unlock_all(dev);
2013
2014 return err;
2015}
2016EXPORT_SYMBOL(drm_atomic_helper_resume);
2017
2018/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002019 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002020 * @crtc: DRM crtc
2021 * @property: DRM property
2022 * @val: value of property
2023 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002024 * Provides a default crtc set_property handler using the atomic driver
2025 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002026 *
2027 * RETURNS:
2028 * Zero on success, error code on failure
2029 */
2030int
2031drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2032 struct drm_property *property,
2033 uint64_t val)
2034{
2035 struct drm_atomic_state *state;
2036 struct drm_crtc_state *crtc_state;
2037 int ret = 0;
2038
2039 state = drm_atomic_state_alloc(crtc->dev);
2040 if (!state)
2041 return -ENOMEM;
2042
2043 /* ->set_property is always called with all locks held. */
2044 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2045retry:
2046 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2047 if (IS_ERR(crtc_state)) {
2048 ret = PTR_ERR(crtc_state);
2049 goto fail;
2050 }
2051
Rob Clark40ecc692014-12-18 16:01:46 -05002052 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2053 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002054 if (ret)
2055 goto fail;
2056
2057 ret = drm_atomic_commit(state);
2058 if (ret != 0)
2059 goto fail;
2060
2061 /* Driver takes ownership of state on successful commit. */
2062 return 0;
2063fail:
2064 if (ret == -EDEADLK)
2065 goto backoff;
2066
2067 drm_atomic_state_free(state);
2068
2069 return ret;
2070backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002071 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002072 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002073
2074 goto retry;
2075}
2076EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2077
2078/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002079 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002080 * @plane: DRM plane
2081 * @property: DRM property
2082 * @val: value of property
2083 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002084 * Provides a default plane set_property handler using the atomic driver
2085 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002086 *
2087 * RETURNS:
2088 * Zero on success, error code on failure
2089 */
2090int
2091drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2092 struct drm_property *property,
2093 uint64_t val)
2094{
2095 struct drm_atomic_state *state;
2096 struct drm_plane_state *plane_state;
2097 int ret = 0;
2098
2099 state = drm_atomic_state_alloc(plane->dev);
2100 if (!state)
2101 return -ENOMEM;
2102
2103 /* ->set_property is always called with all locks held. */
2104 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2105retry:
2106 plane_state = drm_atomic_get_plane_state(state, plane);
2107 if (IS_ERR(plane_state)) {
2108 ret = PTR_ERR(plane_state);
2109 goto fail;
2110 }
2111
Rob Clark40ecc692014-12-18 16:01:46 -05002112 ret = drm_atomic_plane_set_property(plane, plane_state,
2113 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002114 if (ret)
2115 goto fail;
2116
2117 ret = drm_atomic_commit(state);
2118 if (ret != 0)
2119 goto fail;
2120
2121 /* Driver takes ownership of state on successful commit. */
2122 return 0;
2123fail:
2124 if (ret == -EDEADLK)
2125 goto backoff;
2126
2127 drm_atomic_state_free(state);
2128
2129 return ret;
2130backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002131 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002132 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002133
2134 goto retry;
2135}
2136EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2137
2138/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002139 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002140 * @connector: DRM connector
2141 * @property: DRM property
2142 * @val: value of property
2143 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002144 * Provides a default connector set_property handler using the atomic driver
2145 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002146 *
2147 * RETURNS:
2148 * Zero on success, error code on failure
2149 */
2150int
2151drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2152 struct drm_property *property,
2153 uint64_t val)
2154{
2155 struct drm_atomic_state *state;
2156 struct drm_connector_state *connector_state;
2157 int ret = 0;
2158
2159 state = drm_atomic_state_alloc(connector->dev);
2160 if (!state)
2161 return -ENOMEM;
2162
2163 /* ->set_property is always called with all locks held. */
2164 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2165retry:
2166 connector_state = drm_atomic_get_connector_state(state, connector);
2167 if (IS_ERR(connector_state)) {
2168 ret = PTR_ERR(connector_state);
2169 goto fail;
2170 }
2171
Rob Clark40ecc692014-12-18 16:01:46 -05002172 ret = drm_atomic_connector_set_property(connector, connector_state,
2173 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002174 if (ret)
2175 goto fail;
2176
2177 ret = drm_atomic_commit(state);
2178 if (ret != 0)
2179 goto fail;
2180
2181 /* Driver takes ownership of state on successful commit. */
2182 return 0;
2183fail:
2184 if (ret == -EDEADLK)
2185 goto backoff;
2186
2187 drm_atomic_state_free(state);
2188
2189 return ret;
2190backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002191 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002192 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002193
2194 goto retry;
2195}
2196EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002197
2198/**
2199 * drm_atomic_helper_page_flip - execute a legacy page flip
2200 * @crtc: DRM crtc
2201 * @fb: DRM framebuffer
2202 * @event: optional DRM event to signal upon completion
2203 * @flags: flip flags for non-vblank sync'ed updates
2204 *
2205 * Provides a default page flip implementation using the atomic driver interface.
2206 *
2207 * Note that for now so called async page flips (i.e. updates which are not
2208 * synchronized to vblank) are not supported, since the atomic interfaces have
2209 * no provisions for this yet.
2210 *
2211 * Returns:
2212 * Returns 0 on success, negative errno numbers on failure.
2213 */
2214int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2215 struct drm_framebuffer *fb,
2216 struct drm_pending_vblank_event *event,
2217 uint32_t flags)
2218{
2219 struct drm_plane *plane = crtc->primary;
2220 struct drm_atomic_state *state;
2221 struct drm_plane_state *plane_state;
2222 struct drm_crtc_state *crtc_state;
2223 int ret = 0;
2224
2225 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2226 return -EINVAL;
2227
2228 state = drm_atomic_state_alloc(plane->dev);
2229 if (!state)
2230 return -ENOMEM;
2231
2232 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2233retry:
2234 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2235 if (IS_ERR(crtc_state)) {
2236 ret = PTR_ERR(crtc_state);
2237 goto fail;
2238 }
2239 crtc_state->event = event;
2240
2241 plane_state = drm_atomic_get_plane_state(state, plane);
2242 if (IS_ERR(plane_state)) {
2243 ret = PTR_ERR(plane_state);
2244 goto fail;
2245 }
2246
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002247 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002248 if (ret != 0)
2249 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002250 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002251
2252 ret = drm_atomic_async_commit(state);
2253 if (ret != 0)
2254 goto fail;
2255
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002256 /* Driver takes ownership of state on successful async commit. */
2257 return 0;
2258fail:
2259 if (ret == -EDEADLK)
2260 goto backoff;
2261
2262 drm_atomic_state_free(state);
2263
2264 return ret;
2265backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002266 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002267 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002268
2269 /*
2270 * Someone might have exchanged the framebuffer while we dropped locks
2271 * in the backoff code. We need to fix up the fb refcount tracking the
2272 * core does for us.
2273 */
2274 plane->old_fb = plane->fb;
2275
2276 goto retry;
2277}
2278EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002279
2280/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002281 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2282 * @connector: affected connector
2283 * @mode: DPMS mode
2284 *
2285 * This is the main helper function provided by the atomic helper framework for
2286 * implementing the legacy DPMS connector interface. It computes the new desired
2287 * ->active state for the corresponding CRTC (if the connector is enabled) and
2288 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002289 *
2290 * Returns:
2291 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002292 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002293int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2294 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002295{
2296 struct drm_mode_config *config = &connector->dev->mode_config;
2297 struct drm_atomic_state *state;
2298 struct drm_crtc_state *crtc_state;
2299 struct drm_crtc *crtc;
2300 struct drm_connector *tmp_connector;
2301 int ret;
2302 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002303 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002304
2305 if (mode != DRM_MODE_DPMS_ON)
2306 mode = DRM_MODE_DPMS_OFF;
2307
2308 connector->dpms = mode;
2309 crtc = connector->state->crtc;
2310
2311 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002312 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002313
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002314 state = drm_atomic_state_alloc(connector->dev);
2315 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002316 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002317
2318 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2319retry:
2320 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002321 if (IS_ERR(crtc_state)) {
2322 ret = PTR_ERR(crtc_state);
2323 goto fail;
2324 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002325
2326 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2327
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002328 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002329 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002330 continue;
2331
John Hunter0388df02015-03-17 15:30:28 +08002332 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002333 active = true;
2334 break;
2335 }
2336 }
2337 crtc_state->active = active;
2338
2339 ret = drm_atomic_commit(state);
2340 if (ret != 0)
2341 goto fail;
2342
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002343 /* Driver takes ownership of state on successful commit. */
2344 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002345fail:
2346 if (ret == -EDEADLK)
2347 goto backoff;
2348
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002349 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002350 drm_atomic_state_free(state);
2351
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002352 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002353backoff:
2354 drm_atomic_state_clear(state);
2355 drm_atomic_legacy_backoff(state);
2356
2357 goto retry;
2358}
2359EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2360
2361/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002362 * DOC: atomic state reset and initialization
2363 *
2364 * Both the drm core and the atomic helpers assume that there is always the full
2365 * and correct atomic software state for all connectors, CRTCs and planes
2366 * available. Which is a bit a problem on driver load and also after system
2367 * suspend. One way to solve this is to have a hardware state read-out
2368 * infrastructure which reconstructs the full software state (e.g. the i915
2369 * driver).
2370 *
2371 * The simpler solution is to just reset the software state to everything off,
2372 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2373 * the atomic helpers provide default reset implementations for all hooks.
2374 */
2375
2376/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002377 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2378 * @crtc: drm CRTC
2379 *
2380 * Resets the atomic state for @crtc by freeing the state pointer (which might
2381 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2382 */
2383void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2384{
Markus Elfring5f911902015-11-06 12:03:46 +01002385 if (crtc->state)
Daniel Stone99cf4a22015-05-25 19:11:51 +01002386 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002387 kfree(crtc->state);
2388 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002389
2390 if (crtc->state)
2391 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002392}
2393EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2394
2395/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002396 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2397 * @crtc: CRTC object
2398 * @state: atomic CRTC state
2399 *
2400 * Copies atomic state from a CRTC's current state and resets inferred values.
2401 * This is useful for drivers that subclass the CRTC state.
2402 */
2403void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2404 struct drm_crtc_state *state)
2405{
2406 memcpy(state, crtc->state, sizeof(*state));
2407
Daniel Stone99cf4a22015-05-25 19:11:51 +01002408 if (state->mode_blob)
2409 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002410 state->mode_changed = false;
2411 state->active_changed = false;
2412 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002413 state->connectors_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002414 state->event = NULL;
2415}
2416EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2417
2418/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002419 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2420 * @crtc: drm CRTC
2421 *
2422 * Default CRTC state duplicate hook for drivers which don't have their own
2423 * subclassed CRTC state structure.
2424 */
2425struct drm_crtc_state *
2426drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2427{
2428 struct drm_crtc_state *state;
2429
2430 if (WARN_ON(!crtc->state))
2431 return NULL;
2432
Thierry Redingf5e78402015-01-28 14:54:32 +01002433 state = kmalloc(sizeof(*state), GFP_KERNEL);
2434 if (state)
2435 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002436
2437 return state;
2438}
2439EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2440
2441/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002442 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2443 * @crtc: CRTC object
2444 * @state: CRTC state object to release
2445 *
2446 * Releases all resources stored in the CRTC state without actually freeing
2447 * the memory of the CRTC state. This is useful for drivers that subclass the
2448 * CRTC state.
2449 */
2450void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2451 struct drm_crtc_state *state)
2452{
Markus Elfring5f911902015-11-06 12:03:46 +01002453 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002454}
2455EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2456
2457/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002458 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2459 * @crtc: drm CRTC
2460 * @state: CRTC state object to release
2461 *
2462 * Default CRTC state destroy hook for drivers which don't have their own
2463 * subclassed CRTC state structure.
2464 */
2465void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2466 struct drm_crtc_state *state)
2467{
Thierry Redingf5e78402015-01-28 14:54:32 +01002468 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002469 kfree(state);
2470}
2471EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2472
2473/**
2474 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2475 * @plane: drm plane
2476 *
2477 * Resets the atomic state for @plane by freeing the state pointer (which might
2478 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2479 */
2480void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2481{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002482 if (plane->state && plane->state->fb)
2483 drm_framebuffer_unreference(plane->state->fb);
2484
Daniel Vetterd4617012014-11-03 15:56:43 +01002485 kfree(plane->state);
2486 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002487
2488 if (plane->state)
2489 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01002490}
2491EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2492
2493/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002494 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2495 * @plane: plane object
2496 * @state: atomic plane state
2497 *
2498 * Copies atomic state from a plane's current state. This is useful for
2499 * drivers that subclass the plane state.
2500 */
2501void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2502 struct drm_plane_state *state)
2503{
2504 memcpy(state, plane->state, sizeof(*state));
2505
2506 if (state->fb)
2507 drm_framebuffer_reference(state->fb);
2508}
2509EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2510
2511/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002512 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2513 * @plane: drm plane
2514 *
2515 * Default plane state duplicate hook for drivers which don't have their own
2516 * subclassed plane state structure.
2517 */
2518struct drm_plane_state *
2519drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2520{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002521 struct drm_plane_state *state;
2522
Daniel Vetterd4617012014-11-03 15:56:43 +01002523 if (WARN_ON(!plane->state))
2524 return NULL;
2525
Thierry Redingf5e78402015-01-28 14:54:32 +01002526 state = kmalloc(sizeof(*state), GFP_KERNEL);
2527 if (state)
2528 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002529
2530 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002531}
2532EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2533
2534/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002535 * __drm_atomic_helper_plane_destroy_state - release plane state
2536 * @plane: plane object
2537 * @state: plane state object to release
2538 *
2539 * Releases all resources stored in the plane state without actually freeing
2540 * the memory of the plane state. This is useful for drivers that subclass the
2541 * plane state.
2542 */
2543void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2544 struct drm_plane_state *state)
2545{
2546 if (state->fb)
2547 drm_framebuffer_unreference(state->fb);
2548}
2549EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2550
2551/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002552 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2553 * @plane: drm plane
2554 * @state: plane state object to release
2555 *
2556 * Default plane state destroy hook for drivers which don't have their own
2557 * subclassed plane state structure.
2558 */
2559void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002560 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002561{
Thierry Redingf5e78402015-01-28 14:54:32 +01002562 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002563 kfree(state);
2564}
2565EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2566
2567/**
2568 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2569 * @connector: drm connector
2570 *
2571 * Resets the atomic state for @connector by freeing the state pointer (which
2572 * might be NULL, e.g. at driver load time) and allocating a new empty state
2573 * object.
2574 */
2575void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2576{
2577 kfree(connector->state);
2578 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002579
2580 if (connector->state)
2581 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002582}
2583EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2584
2585/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002586 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2587 * @connector: connector object
2588 * @state: atomic connector state
2589 *
2590 * Copies atomic state from a connector's current state. This is useful for
2591 * drivers that subclass the connector state.
2592 */
2593void
2594__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2595 struct drm_connector_state *state)
2596{
2597 memcpy(state, connector->state, sizeof(*state));
2598}
2599EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2600
2601/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002602 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2603 * @connector: drm connector
2604 *
2605 * Default connector state duplicate hook for drivers which don't have their own
2606 * subclassed connector state structure.
2607 */
2608struct drm_connector_state *
2609drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2610{
Thierry Redingf5e78402015-01-28 14:54:32 +01002611 struct drm_connector_state *state;
2612
Daniel Vetterd4617012014-11-03 15:56:43 +01002613 if (WARN_ON(!connector->state))
2614 return NULL;
2615
Thierry Redingf5e78402015-01-28 14:54:32 +01002616 state = kmalloc(sizeof(*state), GFP_KERNEL);
2617 if (state)
2618 __drm_atomic_helper_connector_duplicate_state(connector, state);
2619
2620 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002621}
2622EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2623
2624/**
Thierry Reding397fd772015-09-08 15:00:45 +02002625 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2626 * @dev: DRM device
2627 * @ctx: lock acquisition context
2628 *
2629 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01002630 * duplicating their respective states. This is used for example by suspend/
2631 * resume support code to save the state prior to suspend such that it can
2632 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02002633 *
2634 * Note that this treats atomic state as persistent between save and restore.
2635 * Drivers must make sure that this is possible and won't result in confusion
2636 * or erroneous behaviour.
2637 *
2638 * Note that if callers haven't already acquired all modeset locks this might
2639 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2640 *
2641 * Returns:
2642 * A pointer to the copy of the atomic state object on success or an
2643 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01002644 *
2645 * See also:
2646 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02002647 */
2648struct drm_atomic_state *
2649drm_atomic_helper_duplicate_state(struct drm_device *dev,
2650 struct drm_modeset_acquire_ctx *ctx)
2651{
2652 struct drm_atomic_state *state;
2653 struct drm_connector *conn;
2654 struct drm_plane *plane;
2655 struct drm_crtc *crtc;
2656 int err = 0;
2657
2658 state = drm_atomic_state_alloc(dev);
2659 if (!state)
2660 return ERR_PTR(-ENOMEM);
2661
2662 state->acquire_ctx = ctx;
2663
2664 drm_for_each_crtc(crtc, dev) {
2665 struct drm_crtc_state *crtc_state;
2666
2667 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2668 if (IS_ERR(crtc_state)) {
2669 err = PTR_ERR(crtc_state);
2670 goto free;
2671 }
2672 }
2673
2674 drm_for_each_plane(plane, dev) {
2675 struct drm_plane_state *plane_state;
2676
2677 plane_state = drm_atomic_get_plane_state(state, plane);
2678 if (IS_ERR(plane_state)) {
2679 err = PTR_ERR(plane_state);
2680 goto free;
2681 }
2682 }
2683
2684 drm_for_each_connector(conn, dev) {
2685 struct drm_connector_state *conn_state;
2686
2687 conn_state = drm_atomic_get_connector_state(state, conn);
2688 if (IS_ERR(conn_state)) {
2689 err = PTR_ERR(conn_state);
2690 goto free;
2691 }
2692 }
2693
2694 /* clear the acquire context so that it isn't accidentally reused */
2695 state->acquire_ctx = NULL;
2696
2697free:
2698 if (err < 0) {
2699 drm_atomic_state_free(state);
2700 state = ERR_PTR(err);
2701 }
2702
2703 return state;
2704}
2705EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2706
2707/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002708 * __drm_atomic_helper_connector_destroy_state - release connector state
2709 * @connector: connector object
2710 * @state: connector state object to release
2711 *
2712 * Releases all resources stored in the connector state without actually
2713 * freeing the memory of the connector state. This is useful for drivers that
2714 * subclass the connector state.
2715 */
2716void
2717__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2718 struct drm_connector_state *state)
2719{
2720 /*
2721 * This is currently a placeholder so that drivers that subclass the
2722 * state will automatically do the right thing if code is ever added
2723 * to this function.
2724 */
2725}
2726EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2727
2728/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002729 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2730 * @connector: drm connector
2731 * @state: connector state object to release
2732 *
2733 * Default connector state destroy hook for drivers which don't have their own
2734 * subclassed connector state structure.
2735 */
2736void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2737 struct drm_connector_state *state)
2738{
Thierry Redingf5e78402015-01-28 14:54:32 +01002739 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002740 kfree(state);
2741}
2742EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);