blob: 268d37f26960f681266790d41d104c4a353c11b6 [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.
Daniel Vetter092d01d2015-12-04 09:45:44 +010055 *
56 * The atomic helper uses the same function table structures as all other
57 * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58 * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59 * also shares the struct &drm_plane_helper_funcs function table with the plane
60 * helpers.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010061 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010062static void
63drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 struct drm_plane_state *plane_state,
65 struct drm_plane *plane)
66{
67 struct drm_crtc_state *crtc_state;
68
69 if (plane->state->crtc) {
Rob Clark4b08eae2014-12-08 10:45:43 -050070 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
Daniel Vetterc2fcd272014-11-05 00:14:14 +010071
72 if (WARN_ON(!crtc_state))
73 return;
74
75 crtc_state->planes_changed = true;
76 }
77
78 if (plane_state->crtc) {
79 crtc_state =
80 state->crtc_states[drm_crtc_index(plane_state->crtc)];
81
82 if (WARN_ON(!crtc_state))
83 return;
84
85 crtc_state->planes_changed = true;
86 }
87}
88
Daniel Vetter97ac3202015-12-03 10:49:14 +010089static bool
90check_pending_encoder_assignment(struct drm_atomic_state *state,
Daniel Vetter07bad492015-12-08 09:49:18 +010091 struct drm_encoder *new_encoder)
Daniel Vetter97ac3202015-12-03 10:49:14 +010092{
93 struct drm_connector *connector;
94 struct drm_connector_state *conn_state;
95 int i;
96
97 for_each_connector_in_state(state, connector, conn_state, i) {
98 if (conn_state->best_encoder != new_encoder)
99 continue;
100
101 /* encoder already assigned and we're trying to re-steal it! */
102 if (connector->state->best_encoder != conn_state->best_encoder)
103 return false;
104 }
105
106 return true;
107}
108
Daniel Vetter623369e2014-09-16 17:50:47 +0200109static struct drm_crtc *
110get_current_crtc_for_encoder(struct drm_device *dev,
111 struct drm_encoder *encoder)
112{
113 struct drm_mode_config *config = &dev->mode_config;
114 struct drm_connector *connector;
115
116 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
117
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +0200118 drm_for_each_connector(connector, dev) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200119 if (connector->state->best_encoder != encoder)
120 continue;
121
122 return connector->state->crtc;
123 }
124
125 return NULL;
126}
127
128static int
129steal_encoder(struct drm_atomic_state *state,
130 struct drm_encoder *encoder,
131 struct drm_crtc *encoder_crtc)
132{
133 struct drm_mode_config *config = &state->dev->mode_config;
134 struct drm_crtc_state *crtc_state;
135 struct drm_connector *connector;
136 struct drm_connector_state *connector_state;
Daniel Vetter042652e2014-07-27 13:46:52 +0200137 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200138
139 /*
140 * We can only steal an encoder coming from a connector, which means we
141 * must already hold the connection_mutex.
142 */
143 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
144
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200145 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100146 encoder->base.id, encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200147 encoder_crtc->base.id, encoder_crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200148
149 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
150 if (IS_ERR(crtc_state))
151 return PTR_ERR(crtc_state);
152
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200153 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200154
155 list_for_each_entry(connector, &config->connector_list, head) {
156 if (connector->state->best_encoder != encoder)
157 continue;
158
Daniel Vetter17a38d92015-02-22 12:24:16 +0100159 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
160 connector->base.id,
161 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200162
163 connector_state = drm_atomic_get_connector_state(state,
164 connector);
165 if (IS_ERR(connector_state))
166 return PTR_ERR(connector_state);
167
Daniel Vetter042652e2014-07-27 13:46:52 +0200168 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
169 if (ret)
170 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200171 connector_state->best_encoder = NULL;
172 }
173
174 return 0;
175}
176
177static int
178update_connector_routing(struct drm_atomic_state *state, int conn_idx)
179{
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200180 const struct drm_connector_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200181 struct drm_encoder *new_encoder;
182 struct drm_crtc *encoder_crtc;
183 struct drm_connector *connector;
184 struct drm_connector_state *connector_state;
185 struct drm_crtc_state *crtc_state;
186 int idx, ret;
187
188 connector = state->connectors[conn_idx];
189 connector_state = state->connector_states[conn_idx];
190
191 if (!connector)
192 return 0;
193
Daniel Vetter17a38d92015-02-22 12:24:16 +0100194 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
195 connector->base.id,
196 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200197
198 if (connector->state->crtc != connector_state->crtc) {
199 if (connector->state->crtc) {
200 idx = drm_crtc_index(connector->state->crtc);
201
202 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200203 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200204 }
205
206 if (connector_state->crtc) {
207 idx = drm_crtc_index(connector_state->crtc);
208
209 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200210 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200211 }
212 }
213
214 if (!connector_state->crtc) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100215 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200216 connector->base.id,
217 connector->name);
218
219 connector_state->best_encoder = NULL;
220
221 return 0;
222 }
223
224 funcs = connector->helper_private;
Daniel Vetter3b8a6842015-08-03 17:24:08 +0200225
226 if (funcs->atomic_best_encoder)
227 new_encoder = funcs->atomic_best_encoder(connector,
228 connector_state);
229 else
230 new_encoder = funcs->best_encoder(connector);
Daniel Vetter623369e2014-09-16 17:50:47 +0200231
232 if (!new_encoder) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100233 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
234 connector->base.id,
235 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200236 return -EINVAL;
237 }
238
Daniel Vetter5481c8f2015-11-18 18:46:48 +0100239 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
240 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
241 new_encoder->base.id,
242 new_encoder->name,
243 connector_state->crtc->base.id);
244 return -EINVAL;
245 }
246
Daniel Vetter623369e2014-09-16 17:50:47 +0200247 if (new_encoder == connector_state->best_encoder) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200248 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100249 connector->base.id,
250 connector->name,
251 new_encoder->base.id,
252 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200253 connector_state->crtc->base.id,
254 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200255
256 return 0;
257 }
258
Daniel Vetter07bad492015-12-08 09:49:18 +0100259 if (!check_pending_encoder_assignment(state, new_encoder)) {
Daniel Vetter97ac3202015-12-03 10:49:14 +0100260 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
261 connector->base.id,
262 connector->name);
263 return -EINVAL;
264 }
265
Daniel Vetter623369e2014-09-16 17:50:47 +0200266 encoder_crtc = get_current_crtc_for_encoder(state->dev,
267 new_encoder);
268
269 if (encoder_crtc) {
270 ret = steal_encoder(state, new_encoder, encoder_crtc);
271 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100272 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
273 connector->base.id,
274 connector->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200275 return ret;
276 }
277 }
278
Daniel Vetter6ea76f3c2015-08-03 17:24:11 +0200279 if (WARN_ON(!connector_state->crtc))
280 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200281
Daniel Vetter623369e2014-09-16 17:50:47 +0200282 connector_state->best_encoder = new_encoder;
283 idx = drm_crtc_index(connector_state->crtc);
284
285 crtc_state = state->crtc_states[idx];
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200286 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200287
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200288 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
Daniel Vetter17a38d92015-02-22 12:24:16 +0100289 connector->base.id,
290 connector->name,
291 new_encoder->base.id,
292 new_encoder->name,
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200293 connector_state->crtc->base.id,
294 connector_state->crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200295
296 return 0;
297}
298
299static int
300mode_fixup(struct drm_atomic_state *state)
301{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300302 struct drm_crtc *crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200303 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300304 struct drm_connector *connector;
Daniel Vetter623369e2014-09-16 17:50:47 +0200305 struct drm_connector_state *conn_state;
306 int i;
307 bool ret;
308
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300309 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200310 if (!crtc_state->mode_changed &&
311 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200312 continue;
313
314 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
315 }
316
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300317 for_each_connector_in_state(state, connector, conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200318 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200319 struct drm_encoder *encoder;
320
Daniel Vetter623369e2014-09-16 17:50:47 +0200321 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
322
323 if (!conn_state->crtc || !conn_state->best_encoder)
324 continue;
325
326 crtc_state =
327 state->crtc_states[drm_crtc_index(conn_state->crtc)];
328
329 /*
330 * Each encoder has at most one connector (since we always steal
331 * it away), so we won't call ->mode_fixup twice.
332 */
333 encoder = conn_state->best_encoder;
334 funcs = encoder->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300335 if (!funcs)
336 continue;
Daniel Vetter623369e2014-09-16 17:50:47 +0200337
Archit Taneja862e6862015-05-21 11:03:16 +0530338 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
339 &crtc_state->adjusted_mode);
340 if (!ret) {
341 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
342 return -EINVAL;
Daniel Vetter623369e2014-09-16 17:50:47 +0200343 }
344
Thierry Reding4cd4df82014-12-03 16:44:34 +0100345 if (funcs->atomic_check) {
346 ret = funcs->atomic_check(encoder, crtc_state,
347 conn_state);
348 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100349 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
350 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100351 return ret;
352 }
Inki Dae84524912015-08-11 21:23:49 +0900353 } else if (funcs->mode_fixup) {
Thierry Reding4cd4df82014-12-03 16:44:34 +0100354 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
355 &crtc_state->adjusted_mode);
356 if (!ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100357 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
358 encoder->base.id, encoder->name);
Thierry Reding4cd4df82014-12-03 16:44:34 +0100359 return -EINVAL;
360 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200361 }
362 }
363
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300364 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200365 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200366
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200367 if (!crtc_state->mode_changed &&
368 !crtc_state->connectors_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200369 continue;
370
371 funcs = crtc->helper_private;
Ander Conselvan de Oliveira840bfe92015-04-21 17:13:18 +0300372 if (!funcs->mode_fixup)
373 continue;
374
Daniel Vetter623369e2014-09-16 17:50:47 +0200375 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
376 &crtc_state->adjusted_mode);
377 if (!ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200378 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
379 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200380 return -EINVAL;
381 }
382 }
383
384 return 0;
385}
386
Daniel Vetterd9b13622014-11-26 16:57:41 +0100387/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800388 * drm_atomic_helper_check_modeset - validate state object for modeset changes
Daniel Vetterd9b13622014-11-26 16:57:41 +0100389 * @dev: DRM device
390 * @state: the driver state object
391 *
392 * Check the state object to see if the requested state is physically possible.
393 * This does all the crtc and connector related computations for an atomic
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200394 * update and adds any additional connectors needed for full modesets and calls
395 * down into ->mode_fixup functions of the driver backend.
396 *
397 * crtc_state->mode_changed is set when the input mode is changed.
398 * crtc_state->connectors_changed is set when a connector is added or
399 * removed from the crtc.
400 * crtc_state->active_changed is set when crtc_state->active changes,
401 * which is used for dpms.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100402 *
403 * IMPORTANT:
404 *
405 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
406 * plane update can't be done without a full modeset) _must_ call this function
407 * afterwards after that change. It is permitted to call this function multiple
408 * times for the same update, e.g. when the ->atomic_check functions depend upon
409 * the adjusted dotclock for fifo space allocation and watermark computation.
410 *
411 * RETURNS
412 * Zero for success or -errno
413 */
414int
Rob Clark934ce1c2014-11-19 16:41:33 -0500415drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200416 struct drm_atomic_state *state)
417{
Daniel Vetter623369e2014-09-16 17:50:47 +0200418 struct drm_crtc *crtc;
419 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300420 struct drm_connector *connector;
421 struct drm_connector_state *connector_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200422 int i, ret;
423
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300424 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200425 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200426 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
427 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200428 crtc_state->mode_changed = true;
429 }
430
431 if (crtc->state->enable != crtc_state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200432 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
433 crtc->base.id, crtc->name);
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200434
435 /*
436 * For clarity this assignment is done here, but
437 * enable == 0 is only true when there are no
438 * connectors and a NULL mode.
439 *
440 * The other way around is true as well. enable != 0
441 * iff connectors are attached and a mode is set.
442 */
Daniel Vetter623369e2014-09-16 17:50:47 +0200443 crtc_state->mode_changed = true;
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200444 crtc_state->connectors_changed = true;
Daniel Vetter623369e2014-09-16 17:50:47 +0200445 }
446 }
447
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300448 for_each_connector_in_state(state, connector, connector_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200449 /*
450 * This only sets crtc->mode_changed for routing changes,
451 * drivers must set crtc->mode_changed themselves when connector
452 * properties need to be updated.
453 */
454 ret = update_connector_routing(state, i);
455 if (ret)
456 return ret;
457 }
458
459 /*
460 * After all the routing has been prepared we need to add in any
461 * connector which is itself unchanged, but who's crtc changes it's
462 * configuration. This must be done before calling mode_fixup in case a
463 * crtc only changed its mode but has the same set of connectors.
464 */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300465 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200466 int num_connectors;
467
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100468 /*
469 * We must set ->active_changed after walking connectors for
470 * otherwise an update that only changes active would result in
471 * a full modeset because update_connector_routing force that.
472 */
473 if (crtc->state->active != crtc_state->active) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200474 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
475 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100476 crtc_state->active_changed = true;
477 }
478
Daniel Vetter2465ff62015-06-18 09:58:55 +0200479 if (!drm_atomic_crtc_needs_modeset(crtc_state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100480 continue;
481
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200482 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
483 crtc->base.id, crtc->name,
Daniel Vetter17a38d92015-02-22 12:24:16 +0100484 crtc_state->enable ? 'y' : 'n',
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200485 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200486
487 ret = drm_atomic_add_affected_connectors(state, crtc);
488 if (ret != 0)
489 return ret;
490
Maarten Lankhorst57744aa2015-05-19 16:41:03 +0200491 ret = drm_atomic_add_affected_planes(state, crtc);
492 if (ret != 0)
493 return ret;
494
Daniel Vetter623369e2014-09-16 17:50:47 +0200495 num_connectors = drm_atomic_connectors_for_crtc(state,
496 crtc);
497
498 if (crtc_state->enable != !!num_connectors) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200499 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
500 crtc->base.id, crtc->name);
Daniel Vetter623369e2014-09-16 17:50:47 +0200501
502 return -EINVAL;
503 }
504 }
505
506 return mode_fixup(state);
507}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100508EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200509
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100510/**
John Hunterf98bd3e2015-03-17 15:30:26 +0800511 * drm_atomic_helper_check_planes - validate state object for planes changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100512 * @dev: DRM device
513 * @state: the driver state object
514 *
515 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100516 * This does all the plane update related checks using by calling into the
517 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100518 *
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200519 * It also sets crtc_state->planes_changed to indicate that a crtc has
520 * updated planes.
521 *
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100522 * RETURNS
523 * Zero for success or -errno
524 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100525int
526drm_atomic_helper_check_planes(struct drm_device *dev,
527 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100528{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300529 struct drm_crtc *crtc;
530 struct drm_crtc_state *crtc_state;
531 struct drm_plane *plane;
532 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100533 int i, ret = 0;
534
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300535 for_each_plane_in_state(state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200536 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100537
538 funcs = plane->helper_private;
539
540 drm_atomic_helper_plane_changed(state, plane_state, plane);
541
542 if (!funcs || !funcs->atomic_check)
543 continue;
544
545 ret = funcs->atomic_check(plane, plane_state);
546 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200547 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
548 plane->base.id, plane->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100549 return ret;
550 }
551 }
552
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300553 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200554 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100555
556 funcs = crtc->helper_private;
557
558 if (!funcs || !funcs->atomic_check)
559 continue;
560
561 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
562 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200563 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
564 crtc->base.id, crtc->name);
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100565 return ret;
566 }
567 }
568
Daniel Vetterd9b13622014-11-26 16:57:41 +0100569 return ret;
570}
571EXPORT_SYMBOL(drm_atomic_helper_check_planes);
572
573/**
574 * drm_atomic_helper_check - validate state object
575 * @dev: DRM device
576 * @state: the driver state object
577 *
578 * Check the state object to see if the requested state is physically possible.
579 * Only crtcs and planes have check callbacks, so for any additional (global)
580 * checking that a driver needs it can simply wrap that around this function.
581 * Drivers without such needs can directly use this as their ->atomic_check()
582 * callback.
583 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100584 * This just wraps the two parts of the state checking for planes and modeset
585 * state in the default order: First it calls drm_atomic_helper_check_modeset()
586 * and then drm_atomic_helper_check_planes(). The assumption is that the
587 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
588 * e.g. properly compute watermarks.
589 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100590 * RETURNS
591 * Zero for success or -errno
592 */
593int drm_atomic_helper_check(struct drm_device *dev,
594 struct drm_atomic_state *state)
595{
596 int ret;
597
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100598 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100599 if (ret)
600 return ret;
601
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100602 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500603 if (ret)
604 return ret;
605
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100606 return ret;
607}
608EXPORT_SYMBOL(drm_atomic_helper_check);
609
Daniel Vetter623369e2014-09-16 17:50:47 +0200610static void
611disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
612{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300613 struct drm_connector *connector;
614 struct drm_connector_state *old_conn_state;
615 struct drm_crtc *crtc;
616 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200617 int i;
618
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300619 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200620 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200621 struct drm_encoder *encoder;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100622 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200623
Daniel Vetter623369e2014-09-16 17:50:47 +0200624 /* Shut down everything that's in the changeset and currently
625 * still on. So need to check the old, saved state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300626 if (!old_conn_state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200627 continue;
628
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100629 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
630
Daniel Vetter4218a322015-03-26 22:18:40 +0100631 if (!old_crtc_state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200632 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100633 continue;
634
Rob Clark46df9ad2014-11-20 15:40:36 -0500635 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200636
Rob Clark46df9ad2014-11-20 15:40:36 -0500637 /* We shouldn't get this far if we didn't previously have
638 * an encoder.. but WARN_ON() rather than explode.
639 */
640 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200641 continue;
642
643 funcs = encoder->helper_private;
644
Daniel Vetter17a38d92015-02-22 12:24:16 +0100645 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
646 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100647
Daniel Vetter623369e2014-09-16 17:50:47 +0200648 /*
649 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800650 * it away), so we won't call disable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200651 */
Archit Taneja862e6862015-05-21 11:03:16 +0530652 drm_bridge_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200653
654 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100655 if (connector->state->crtc && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200656 funcs->prepare(encoder);
657 else if (funcs->disable)
658 funcs->disable(encoder);
659 else
660 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
661
Archit Taneja862e6862015-05-21 11:03:16 +0530662 drm_bridge_post_disable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200663 }
664
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300665 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200666 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200667
668 /* Shut down everything that needs a full modeset. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200669 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100670 continue;
671
672 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200673 continue;
674
675 funcs = crtc->helper_private;
676
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200677 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
678 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100679
680
Daniel Vetter623369e2014-09-16 17:50:47 +0200681 /* Right function depends upon target state. */
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100682 if (crtc->state->enable && funcs->prepare)
Daniel Vetter623369e2014-09-16 17:50:47 +0200683 funcs->prepare(crtc);
684 else if (funcs->disable)
685 funcs->disable(crtc);
686 else
687 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
688 }
689}
690
Daniel Vetter4c18d302015-05-12 15:27:37 +0200691/**
692 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
693 * @dev: DRM device
694 * @old_state: atomic state object with old state structures
695 *
696 * This function updates all the various legacy modeset state pointers in
697 * connectors, encoders and crtcs. It also updates the timestamping constants
698 * used for precise vblank timestamps by calling
699 * drm_calc_timestamping_constants().
700 *
701 * Drivers can use this for building their own atomic commit if they don't have
702 * a pure helper-based modeset implementation.
703 */
704void
705drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
706 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200707{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300708 struct drm_connector *connector;
709 struct drm_connector_state *old_conn_state;
710 struct drm_crtc *crtc;
711 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200712 int i;
713
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200714 /* clear out existing links and update dpms */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300715 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200716 if (connector->encoder) {
717 WARN_ON(!connector->encoder->crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +0200718
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200719 connector->encoder->crtc = NULL;
720 connector->encoder = NULL;
721 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200722
Maarten Lankhorst8c103422015-07-27 13:24:29 +0200723 crtc = connector->state->crtc;
724 if ((!crtc && old_conn_state->crtc) ||
725 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
726 struct drm_property *dpms_prop =
727 dev->mode_config.dpms_property;
728 int mode = DRM_MODE_DPMS_OFF;
729
730 if (crtc && crtc->state->active)
731 mode = DRM_MODE_DPMS_ON;
732
733 connector->dpms = mode;
734 drm_object_property_set_value(&connector->base,
735 dpms_prop, mode);
736 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200737 }
738
739 /* set new links */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300740 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
741 if (!connector->state->crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200742 continue;
743
744 if (WARN_ON(!connector->state->best_encoder))
745 continue;
746
747 connector->encoder = connector->state->best_encoder;
748 connector->encoder->crtc = connector->state->crtc;
749 }
750
751 /* set legacy state in the crtc structure */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300752 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Maarten Lankhorst26608012015-07-16 15:51:01 +0200753 struct drm_plane *primary = crtc->primary;
754
Daniel Vetter623369e2014-09-16 17:50:47 +0200755 crtc->mode = crtc->state->mode;
756 crtc->enabled = crtc->state->enable;
Maarten Lankhorst26608012015-07-16 15:51:01 +0200757
758 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
759 primary->state->crtc == crtc) {
760 crtc->x = primary->state->src_x >> 16;
761 crtc->y = primary->state->src_y >> 16;
762 }
Daniel Vetter3d51d2d2015-05-12 15:21:06 +0200763
764 if (crtc->state->enable)
765 drm_calc_timestamping_constants(crtc,
766 &crtc->state->adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200767 }
768}
Daniel Vetter4c18d302015-05-12 15:27:37 +0200769EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200770
771static void
772crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
773{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300774 struct drm_crtc *crtc;
775 struct drm_crtc_state *old_crtc_state;
776 struct drm_connector *connector;
777 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200778 int i;
779
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300780 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200781 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200782
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300783 if (!crtc->state->mode_changed)
Daniel Vetter623369e2014-09-16 17:50:47 +0200784 continue;
785
786 funcs = crtc->helper_private;
787
Daniel Vetterc982bd92015-02-22 12:24:20 +0100788 if (crtc->state->enable && funcs->mode_set_nofb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200789 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
790 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100791
Daniel Vetter623369e2014-09-16 17:50:47 +0200792 funcs->mode_set_nofb(crtc);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100793 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200794 }
795
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300796 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200797 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200798 struct drm_crtc_state *new_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200799 struct drm_encoder *encoder;
800 struct drm_display_mode *mode, *adjusted_mode;
801
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300802 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200803 continue;
804
805 encoder = connector->state->best_encoder;
806 funcs = encoder->helper_private;
807 new_crtc_state = connector->state->crtc->state;
808 mode = &new_crtc_state->mode;
809 adjusted_mode = &new_crtc_state->adjusted_mode;
810
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100811 if (!new_crtc_state->mode_changed)
812 continue;
813
Daniel Vetter17a38d92015-02-22 12:24:16 +0100814 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
815 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100816
Daniel Vetter623369e2014-09-16 17:50:47 +0200817 /*
818 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800819 * it away), so we won't call mode_set hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200820 */
Daniel Vetterc982bd92015-02-22 12:24:20 +0100821 if (funcs->mode_set)
822 funcs->mode_set(encoder, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200823
Archit Taneja862e6862015-05-21 11:03:16 +0530824 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Daniel Vetter623369e2014-09-16 17:50:47 +0200825 }
826}
827
828/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100829 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200830 * @dev: DRM device
Laurent Pincharta072f802015-02-22 12:24:18 +0100831 * @old_state: atomic state object with old state structures
Daniel Vetter623369e2014-09-16 17:50:47 +0200832 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100833 * This function shuts down all the outputs that need to be shut down and
Daniel Vetter623369e2014-09-16 17:50:47 +0200834 * prepares them (if required) with the new mode.
Daniel Vetter1af434a2015-02-22 12:24:19 +0100835 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300836 * For compatibility with legacy crtc helpers this should be called before
Daniel Vetter1af434a2015-02-22 12:24:19 +0100837 * drm_atomic_helper_commit_planes(), which is what the default commit function
838 * does. But drivers with different needs can group the modeset commits together
839 * and do the plane commits at the end. This is useful for drivers doing runtime
840 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200841 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100842void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
843 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200844{
Laurent Pincharta072f802015-02-22 12:24:18 +0100845 disable_outputs(dev, old_state);
Daniel Vetter4c18d302015-05-12 15:27:37 +0200846
847 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
848
Laurent Pincharta072f802015-02-22 12:24:18 +0100849 crtc_set_mode(dev, old_state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200850}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100851EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200852
853/**
Daniel Vetter1af434a2015-02-22 12:24:19 +0100854 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
Daniel Vetter623369e2014-09-16 17:50:47 +0200855 * @dev: DRM device
856 * @old_state: atomic state object with old state structures
857 *
Daniel Vetter1af434a2015-02-22 12:24:19 +0100858 * This function enables all the outputs with the new configuration which had to
859 * be turned off for the update.
860 *
Laurent Pinchart60acc4e2015-05-27 15:05:42 +0300861 * For compatibility with legacy crtc helpers this should be called after
Daniel Vetter1af434a2015-02-22 12:24:19 +0100862 * drm_atomic_helper_commit_planes(), which is what the default commit function
863 * does. But drivers with different needs can group the modeset commits together
864 * and do the plane commits at the end. This is useful for drivers doing runtime
865 * PM since planes updates then only happen when the CRTC is actually enabled.
Daniel Vetter623369e2014-09-16 17:50:47 +0200866 */
Daniel Vetter1af434a2015-02-22 12:24:19 +0100867void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
868 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200869{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300870 struct drm_crtc *crtc;
871 struct drm_crtc_state *old_crtc_state;
872 struct drm_connector *connector;
873 struct drm_connector_state *old_conn_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200874 int i;
875
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300876 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200877 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200878
879 /* Need to filter out CRTCs where only planes change. */
Daniel Vetter2465ff62015-06-18 09:58:55 +0200880 if (!drm_atomic_crtc_needs_modeset(crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100881 continue;
882
883 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200884 continue;
885
886 funcs = crtc->helper_private;
887
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100888 if (crtc->state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200889 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
890 crtc->base.id, crtc->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100891
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100892 if (funcs->enable)
893 funcs->enable(crtc);
894 else
895 funcs->commit(crtc);
896 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200897 }
898
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300899 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +0200900 const struct drm_encoder_helper_funcs *funcs;
Daniel Vetter623369e2014-09-16 17:50:47 +0200901 struct drm_encoder *encoder;
902
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300903 if (!connector->state->best_encoder)
Daniel Vetter623369e2014-09-16 17:50:47 +0200904 continue;
905
Daniel Vetter4218a322015-03-26 22:18:40 +0100906 if (!connector->state->crtc->state->active ||
Daniel Vetter2465ff62015-06-18 09:58:55 +0200907 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100908 continue;
909
Daniel Vetter623369e2014-09-16 17:50:47 +0200910 encoder = connector->state->best_encoder;
911 funcs = encoder->helper_private;
912
Daniel Vetter17a38d92015-02-22 12:24:16 +0100913 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
914 encoder->base.id, encoder->name);
Daniel Vetter95d6eb32015-01-22 16:36:25 +0100915
Daniel Vetter623369e2014-09-16 17:50:47 +0200916 /*
917 * Each encoder has at most one connector (since we always steal
John Hunterf98bd3e2015-03-17 15:30:26 +0800918 * it away), so we won't call enable hooks twice.
Daniel Vetter623369e2014-09-16 17:50:47 +0200919 */
Archit Taneja862e6862015-05-21 11:03:16 +0530920 drm_bridge_pre_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200921
Daniel Vetteree0a89c2015-01-22 16:36:24 +0100922 if (funcs->enable)
923 funcs->enable(encoder);
924 else
925 funcs->commit(encoder);
Daniel Vetter623369e2014-09-16 17:50:47 +0200926
Archit Taneja862e6862015-05-21 11:03:16 +0530927 drm_bridge_enable(encoder->bridge);
Daniel Vetter623369e2014-09-16 17:50:47 +0200928 }
929}
Daniel Vetter1af434a2015-02-22 12:24:19 +0100930EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
Daniel Vetter623369e2014-09-16 17:50:47 +0200931
Daniel Vettere2330f02014-10-29 11:34:56 +0100932static void wait_for_fences(struct drm_device *dev,
933 struct drm_atomic_state *state)
934{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300935 struct drm_plane *plane;
936 struct drm_plane_state *plane_state;
Daniel Vettere2330f02014-10-29 11:34:56 +0100937 int i;
938
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300939 for_each_plane_in_state(state, plane, plane_state, i) {
940 if (!plane->state->fence)
Daniel Vettere2330f02014-10-29 11:34:56 +0100941 continue;
942
943 WARN_ON(!plane->state->fb);
944
945 fence_wait(plane->state->fence, false);
946 fence_put(plane->state->fence);
947 plane->state->fence = NULL;
948 }
949}
950
Daniel Vetterab58e332014-11-24 20:42:42 +0100951static bool framebuffer_changed(struct drm_device *dev,
952 struct drm_atomic_state *old_state,
953 struct drm_crtc *crtc)
954{
955 struct drm_plane *plane;
956 struct drm_plane_state *old_plane_state;
Daniel Vetterab58e332014-11-24 20:42:42 +0100957 int i;
958
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300959 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Daniel Vetterab58e332014-11-24 20:42:42 +0100960 if (plane->state->crtc != crtc &&
961 old_plane_state->crtc != crtc)
962 continue;
963
964 if (plane->state->fb != old_plane_state->fb)
965 return true;
966 }
967
968 return false;
969}
970
Rob Clark5ee32292014-11-11 19:38:59 -0500971/**
972 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
973 * @dev: DRM device
974 * @old_state: atomic state object with old state structures
975 *
976 * Helper to, after atomic commit, wait for vblanks on all effected
977 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100978 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
979 * framebuffers have actually changed to optimize for the legacy cursor and
980 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500981 */
982void
983drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
984 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200985{
986 struct drm_crtc *crtc;
987 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200988 int i, ret;
989
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +0300990 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200991 /* No one cares about the old state, so abuse it for tracking
992 * and store whether we hold a vblank reference (and should do a
993 * vblank wait) in the ->enable boolean. */
994 old_crtc_state->enable = false;
995
996 if (!crtc->state->enable)
997 continue;
998
Daniel Vetterf02ad902015-01-22 16:36:23 +0100999 /* Legacy cursor ioctls are completely unsynced, and userspace
1000 * relies on that (by doing tons of cursor updates). */
1001 if (old_state->legacy_cursor_update)
1002 continue;
1003
Daniel Vetterab58e332014-11-24 20:42:42 +01001004 if (!framebuffer_changed(dev, old_state, crtc))
1005 continue;
1006
Daniel Vetter623369e2014-09-16 17:50:47 +02001007 ret = drm_crtc_vblank_get(crtc);
1008 if (ret != 0)
1009 continue;
1010
1011 old_crtc_state->enable = true;
Thierry Redingd4853632015-08-12 17:00:35 +02001012 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
Daniel Vetter623369e2014-09-16 17:50:47 +02001013 }
1014
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001015 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1016 if (!old_crtc_state->enable)
Daniel Vetter623369e2014-09-16 17:50:47 +02001017 continue;
1018
1019 ret = wait_event_timeout(dev->vblank[i].queue,
1020 old_crtc_state->last_vblank_count !=
Thierry Redingd4853632015-08-12 17:00:35 +02001021 drm_crtc_vblank_count(crtc),
Daniel Vetter623369e2014-09-16 17:50:47 +02001022 msecs_to_jiffies(50));
1023
1024 drm_crtc_vblank_put(crtc);
1025 }
1026}
Rob Clark5ee32292014-11-11 19:38:59 -05001027EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +02001028
1029/**
1030 * drm_atomic_helper_commit - commit validated state object
1031 * @dev: DRM device
1032 * @state: the driver state object
1033 * @async: asynchronous commit
1034 *
1035 * This function commits a with drm_atomic_helper_check() pre-validated state
1036 * object. This can still fail when e.g. the framebuffer reservation fails. For
1037 * now this doesn't implement asynchronous commits.
1038 *
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001039 * Note that right now this function does not support async commits, and hence
1040 * driver writers must implement their own version for now. Also note that the
1041 * default ordering of how the various stages are called is to match the legacy
1042 * modeset helper library closest. One peculiarity of that is that it doesn't
1043 * mesh well with runtime PM at all.
1044 *
1045 * For drivers supporting runtime PM the recommended sequence is
1046 *
1047 * drm_atomic_helper_commit_modeset_disables(dev, state);
1048 *
1049 * drm_atomic_helper_commit_modeset_enables(dev, state);
1050 *
1051 * drm_atomic_helper_commit_planes(dev, state, true);
1052 *
1053 * See the kerneldoc entries for these three functions for more details.
1054 *
Daniel Vetter623369e2014-09-16 17:50:47 +02001055 * RETURNS
1056 * Zero for success or -errno.
1057 */
1058int drm_atomic_helper_commit(struct drm_device *dev,
1059 struct drm_atomic_state *state,
1060 bool async)
1061{
1062 int ret;
1063
1064 if (async)
1065 return -EBUSY;
1066
1067 ret = drm_atomic_helper_prepare_planes(dev, state);
1068 if (ret)
1069 return ret;
1070
1071 /*
1072 * This is the point of no return - everything below never fails except
1073 * when the hw goes bonghits. Which means we can commit the new state on
1074 * the software side now.
1075 */
1076
1077 drm_atomic_helper_swap_state(dev, state);
1078
1079 /*
1080 * Everything below can be run asynchronously without the need to grab
John Hunterf98bd3e2015-03-17 15:30:26 +08001081 * any modeset locks at all under one condition: It must be guaranteed
Daniel Vetter623369e2014-09-16 17:50:47 +02001082 * that the asynchronous work has either been cancelled (if the driver
1083 * supports it, which at least requires that the framebuffers get
1084 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1085 * before the new state gets committed on the software side with
1086 * drm_atomic_helper_swap_state().
1087 *
1088 * This scheme allows new atomic state updates to be prepared and
1089 * checked in parallel to the asynchronous completion of the previous
1090 * update. Which is important since compositors need to figure out the
1091 * composition of the next frame right after having submitted the
1092 * current layout.
1093 */
1094
Daniel Vettere2330f02014-10-29 11:34:56 +01001095 wait_for_fences(dev, state);
1096
Daniel Vetter1af434a2015-02-22 12:24:19 +01001097 drm_atomic_helper_commit_modeset_disables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001098
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001099 drm_atomic_helper_commit_planes(dev, state, false);
Daniel Vetter623369e2014-09-16 17:50:47 +02001100
Daniel Vetter1af434a2015-02-22 12:24:19 +01001101 drm_atomic_helper_commit_modeset_enables(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001102
Rob Clark5ee32292014-11-11 19:38:59 -05001103 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001104
1105 drm_atomic_helper_cleanup_planes(dev, state);
1106
1107 drm_atomic_state_free(state);
1108
1109 return 0;
1110}
1111EXPORT_SYMBOL(drm_atomic_helper_commit);
1112
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001113/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001114 * DOC: implementing async commit
1115 *
1116 * For now the atomic helpers don't support async commit directly. If there is
1117 * real need it could be added though, using the dma-buf fence infrastructure
1118 * for generic synchronization with outstanding rendering.
1119 *
1120 * For now drivers have to implement async commit themselves, with the following
1121 * sequence being the recommended one:
1122 *
1123 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1124 * which commit needs to call which can fail, so we want to run it first and
1125 * synchronously.
1126 *
1127 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1128 * might be affected the new state update. This can be done by either cancelling
1129 * or flushing the work items, depending upon whether the driver can deal with
1130 * cancelled updates. Note that it is important to ensure that the framebuffer
1131 * cleanup is still done when cancelling.
1132 *
1133 * For sufficient parallelism it is recommended to have a work item per crtc
1134 * (for updates which don't touch global state) and a global one. Then we only
1135 * need to synchronize with the crtc work items for changed crtcs and the global
1136 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1137 *
1138 * 3. The software state is updated synchronously with
Daniel Vetter26196f72015-08-25 16:26:03 +02001139 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
Daniel Vettere8c833a2014-07-27 18:30:19 +02001140 * locks means concurrent callers never see inconsistent state. And doing this
1141 * while it's guaranteed that no relevant async worker runs means that async
1142 * workers do not need grab any locks. Actually they must not grab locks, for
1143 * otherwise the work flushing will deadlock.
1144 *
1145 * 4. Schedule a work item to do all subsequent steps, using the split-out
1146 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1147 * then cleaning up the framebuffers after the old framebuffer is no longer
1148 * being displayed.
1149 */
1150
1151/**
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001152 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001153 * @dev: DRM device
Daniel Vetter2e3afd42015-02-26 14:17:38 +01001154 * @state: atomic state object with new state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001155 *
1156 * This function prepares plane state, specifically framebuffers, for the new
1157 * configuration. If any failure is encountered this function will call
1158 * ->cleanup_fb on any already successfully prepared framebuffer.
1159 *
1160 * Returns:
1161 * 0 on success, negative error code on failure.
1162 */
1163int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1164 struct drm_atomic_state *state)
1165{
1166 int nplanes = dev->mode_config.num_total_plane;
1167 int ret, i;
1168
1169 for (i = 0; i < nplanes; i++) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001170 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001171 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001172 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001173
1174 if (!plane)
1175 continue;
1176
1177 funcs = plane->helper_private;
1178
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001179 if (funcs->prepare_fb) {
1180 ret = funcs->prepare_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001181 if (ret)
1182 goto fail;
1183 }
1184 }
1185
1186 return 0;
1187
1188fail:
1189 for (i--; i >= 0; i--) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001190 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001191 struct drm_plane *plane = state->planes[i];
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +00001192 struct drm_plane_state *plane_state = state->plane_states[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001193
1194 if (!plane)
1195 continue;
1196
1197 funcs = plane->helper_private;
1198
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001199 if (funcs->cleanup_fb)
1200 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001201
1202 }
1203
1204 return ret;
1205}
1206EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1207
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001208bool plane_crtc_active(struct drm_plane_state *state)
1209{
1210 return state->crtc && state->crtc->state->active;
1211}
1212
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001213/**
1214 * drm_atomic_helper_commit_planes - commit plane state
1215 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001216 * @old_state: atomic state object with old state structures
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001217 * @active_only: Only commit on active CRTC if set
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001218 *
1219 * This function commits the new plane state using the plane and atomic helper
1220 * functions for planes and crtcs. It assumes that the atomic state has already
1221 * been pushed into the relevant object state pointers, since this step can no
1222 * longer fail.
1223 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001224 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001225 * crtcs need to be updated though.
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001226 *
1227 * Note that this function does all plane updates across all CRTCs in one step.
1228 * If the hardware can't support this approach look at
1229 * drm_atomic_helper_commit_planes_on_crtc() instead.
Daniel Vetter6e48ae32015-09-08 13:52:45 +02001230 *
1231 * Plane parameters can be updated by applications while the associated CRTC is
1232 * disabled. The DRM/KMS core will store the parameters in the plane state,
1233 * which will be available to the driver when the CRTC is turned on. As a result
1234 * most drivers don't need to be immediately notified of plane updates for a
1235 * disabled CRTC.
1236 *
1237 * Unless otherwise needed, drivers are advised to set the @active_only
1238 * parameters to true in order not to receive plane update notifications related
1239 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1240 * driver code when the driver and/or hardware can't or just don't need to deal
1241 * with updates on disabled CRTCs, for example when supporting runtime PM.
1242 *
1243 * The drm_atomic_helper_commit() default implementation only sets @active_only
1244 * to false to most closely match the behaviour of the legacy helpers. This should
1245 * not be copied blindly by drivers.
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001246 */
1247void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001248 struct drm_atomic_state *old_state,
1249 bool active_only)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001250{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001251 struct drm_crtc *crtc;
1252 struct drm_crtc_state *old_crtc_state;
1253 struct drm_plane *plane;
1254 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001255 int i;
1256
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001257 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001258 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001259
1260 funcs = crtc->helper_private;
1261
1262 if (!funcs || !funcs->atomic_begin)
1263 continue;
1264
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001265 if (active_only && !crtc->state->active)
1266 continue;
1267
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001268 funcs->atomic_begin(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001269 }
1270
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001271 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001272 const struct drm_plane_helper_funcs *funcs;
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001273 bool disabling;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001274
1275 funcs = plane->helper_private;
1276
Thierry Reding3cad4b62014-11-25 13:05:12 +01001277 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001278 continue;
1279
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001280 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1281
1282 if (active_only) {
1283 /*
1284 * Skip planes related to inactive CRTCs. If the plane
1285 * is enabled use the state of the current CRTC. If the
1286 * plane is being disabled use the state of the old
1287 * CRTC to avoid skipping planes being disabled on an
1288 * active CRTC.
1289 */
1290 if (!disabling && !plane_crtc_active(plane->state))
1291 continue;
1292 if (disabling && !plane_crtc_active(old_plane_state))
1293 continue;
1294 }
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001295
Thierry Reding407b8bd2014-11-20 12:05:50 +01001296 /*
1297 * Special-case disabling the plane if drivers support it.
1298 */
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001299 if (disabling && funcs->atomic_disable)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001300 funcs->atomic_disable(plane, old_plane_state);
Laurent Pinchart216c59d2015-09-11 00:07:19 +03001301 else if (plane->state->crtc || disabling)
Thierry Reding407b8bd2014-11-20 12:05:50 +01001302 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001303 }
1304
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001305 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001306 const struct drm_crtc_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001307
1308 funcs = crtc->helper_private;
1309
1310 if (!funcs || !funcs->atomic_flush)
1311 continue;
1312
Daniel Vetteraef9dbb2015-09-08 12:02:07 +02001313 if (active_only && !crtc->state->active)
1314 continue;
1315
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001316 funcs->atomic_flush(crtc, old_crtc_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001317 }
1318}
1319EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1320
1321/**
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001322 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1323 * @old_crtc_state: atomic state object with the old crtc state
1324 *
1325 * This function commits the new plane state using the plane and atomic helper
1326 * functions for planes on the specific crtc. It assumes that the atomic state
1327 * has already been pushed into the relevant object state pointers, since this
1328 * step can no longer fail.
1329 *
1330 * This function is useful when plane updates should be done crtc-by-crtc
1331 * instead of one global step like drm_atomic_helper_commit_planes() does.
1332 *
1333 * This function can only be savely used when planes are not allowed to move
1334 * between different CRTCs because this function doesn't handle inter-CRTC
1335 * depencies. Callers need to ensure that either no such depencies exist,
1336 * resolve them through ordering of commit calls or through some other means.
1337 */
1338void
1339drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1340{
1341 const struct drm_crtc_helper_funcs *crtc_funcs;
1342 struct drm_crtc *crtc = old_crtc_state->crtc;
1343 struct drm_atomic_state *old_state = old_crtc_state->state;
1344 struct drm_plane *plane;
1345 unsigned plane_mask;
1346
1347 plane_mask = old_crtc_state->plane_mask;
1348 plane_mask |= crtc->state->plane_mask;
1349
1350 crtc_funcs = crtc->helper_private;
1351 if (crtc_funcs && crtc_funcs->atomic_begin)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001352 crtc_funcs->atomic_begin(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001353
1354 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1355 struct drm_plane_state *old_plane_state =
1356 drm_atomic_get_existing_plane_state(old_state, plane);
1357 const struct drm_plane_helper_funcs *plane_funcs;
1358
1359 plane_funcs = plane->helper_private;
1360
1361 if (!old_plane_state || !plane_funcs)
1362 continue;
1363
1364 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1365
1366 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1367 plane_funcs->atomic_disable)
1368 plane_funcs->atomic_disable(plane, old_plane_state);
1369 else if (plane->state->crtc ||
1370 drm_atomic_plane_disabling(plane, old_plane_state))
1371 plane_funcs->atomic_update(plane, old_plane_state);
1372 }
1373
1374 if (crtc_funcs && crtc_funcs->atomic_flush)
Maarten Lankhorst613d2b22015-07-21 13:28:58 +02001375 crtc_funcs->atomic_flush(crtc, old_crtc_state);
Maarten Lankhorstde28d022015-05-19 16:41:01 +02001376}
1377EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1378
1379/**
Jyri Sarha6753ba92015-11-27 16:14:01 +02001380 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1381 * @crtc: CRTC
1382 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1383 *
1384 * Disables all planes associated with the given CRTC. This can be
1385 * used for instance in the CRTC helper disable callback to disable
1386 * all planes before shutting down the display pipeline.
1387 *
1388 * If the atomic-parameter is set the function calls the CRTC's
1389 * atomic_begin hook before and atomic_flush hook after disabling the
1390 * planes.
1391 *
1392 * It is a bug to call this function without having implemented the
1393 * ->atomic_disable() plane hook.
1394 */
1395void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1396 bool atomic)
1397{
1398 const struct drm_crtc_helper_funcs *crtc_funcs =
1399 crtc->helper_private;
1400 struct drm_plane *plane;
1401
1402 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1403 crtc_funcs->atomic_begin(crtc, NULL);
1404
1405 drm_for_each_plane(plane, crtc->dev) {
1406 const struct drm_plane_helper_funcs *plane_funcs =
1407 plane->helper_private;
1408
1409 if (plane->state->crtc != crtc || !plane_funcs)
1410 continue;
1411
1412 WARN_ON(!plane_funcs->atomic_disable);
1413 if (plane_funcs->atomic_disable)
1414 plane_funcs->atomic_disable(plane, NULL);
1415 }
1416
1417 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1418 crtc_funcs->atomic_flush(crtc, NULL);
1419}
1420EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1421
1422/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001423 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1424 * @dev: DRM device
1425 * @old_state: atomic state object with old state structures
1426 *
1427 * This function cleans up plane state, specifically framebuffers, from the old
1428 * configuration. Hence the old configuration must be perserved in @old_state to
1429 * be able to call this function.
1430 *
1431 * This function must also be called on the new state when the atomic update
1432 * fails at any point after calling drm_atomic_helper_prepare_planes().
1433 */
1434void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1435 struct drm_atomic_state *old_state)
1436{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001437 struct drm_plane *plane;
1438 struct drm_plane_state *plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001439 int i;
1440
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001441 for_each_plane_in_state(old_state, plane, plane_state, i) {
Ville Syrjäläb5ceff202015-03-10 14:35:20 +02001442 const struct drm_plane_helper_funcs *funcs;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001443
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001444 funcs = plane->helper_private;
1445
Maarten Lankhorst844f9112015-09-02 10:42:40 +02001446 if (funcs->cleanup_fb)
1447 funcs->cleanup_fb(plane, plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001448 }
1449}
1450EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1451
1452/**
1453 * drm_atomic_helper_swap_state - store atomic state into current sw state
1454 * @dev: DRM device
1455 * @state: atomic state
1456 *
1457 * This function stores the atomic state into the current state pointers in all
1458 * driver objects. It should be called after all failing steps have been done
1459 * and succeeded, but before the actual hardware state is committed.
1460 *
1461 * For cleanup and error recovery the current state for all changed objects will
1462 * be swaped into @state.
1463 *
1464 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1465 *
1466 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1467 *
1468 * 2. Do any other steps that might fail.
1469 *
1470 * 3. Put the staged state into the current state pointers with this function.
1471 *
1472 * 4. Actually commit the hardware state.
1473 *
Daniel Vetter26196f72015-08-25 16:26:03 +02001474 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001475 * contains the old state. Also do any other cleanup required with that state.
1476 */
1477void drm_atomic_helper_swap_state(struct drm_device *dev,
1478 struct drm_atomic_state *state)
1479{
1480 int i;
1481
1482 for (i = 0; i < dev->mode_config.num_connector; i++) {
1483 struct drm_connector *connector = state->connectors[i];
1484
1485 if (!connector)
1486 continue;
1487
1488 connector->state->state = state;
1489 swap(state->connector_states[i], connector->state);
1490 connector->state->state = NULL;
1491 }
1492
1493 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1494 struct drm_crtc *crtc = state->crtcs[i];
1495
1496 if (!crtc)
1497 continue;
1498
1499 crtc->state->state = state;
1500 swap(state->crtc_states[i], crtc->state);
1501 crtc->state->state = NULL;
1502 }
1503
1504 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1505 struct drm_plane *plane = state->planes[i];
1506
1507 if (!plane)
1508 continue;
1509
1510 plane->state->state = state;
1511 swap(state->plane_states[i], plane->state);
1512 plane->state->state = NULL;
1513 }
1514}
1515EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001516
1517/**
1518 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1519 * @plane: plane object to update
1520 * @crtc: owning CRTC of owning plane
1521 * @fb: framebuffer to flip onto plane
1522 * @crtc_x: x offset of primary plane on crtc
1523 * @crtc_y: y offset of primary plane on crtc
1524 * @crtc_w: width of primary plane rectangle on crtc
1525 * @crtc_h: height of primary plane rectangle on crtc
1526 * @src_x: x offset of @fb for panning
1527 * @src_y: y offset of @fb for panning
1528 * @src_w: width of source rectangle in @fb
1529 * @src_h: height of source rectangle in @fb
1530 *
1531 * Provides a default plane update handler using the atomic driver interface.
1532 *
1533 * RETURNS:
1534 * Zero on success, error code on failure
1535 */
1536int drm_atomic_helper_update_plane(struct drm_plane *plane,
1537 struct drm_crtc *crtc,
1538 struct drm_framebuffer *fb,
1539 int crtc_x, int crtc_y,
1540 unsigned int crtc_w, unsigned int crtc_h,
1541 uint32_t src_x, uint32_t src_y,
1542 uint32_t src_w, uint32_t src_h)
1543{
1544 struct drm_atomic_state *state;
1545 struct drm_plane_state *plane_state;
1546 int ret = 0;
1547
1548 state = drm_atomic_state_alloc(plane->dev);
1549 if (!state)
1550 return -ENOMEM;
1551
1552 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1553retry:
1554 plane_state = drm_atomic_get_plane_state(state, plane);
1555 if (IS_ERR(plane_state)) {
1556 ret = PTR_ERR(plane_state);
1557 goto fail;
1558 }
1559
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001560 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001561 if (ret != 0)
1562 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001563 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001564 plane_state->crtc_x = crtc_x;
1565 plane_state->crtc_y = crtc_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001566 plane_state->crtc_w = crtc_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001567 plane_state->crtc_h = crtc_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001568 plane_state->src_x = src_x;
1569 plane_state->src_y = src_y;
Daniel Vetter042652e2014-07-27 13:46:52 +02001570 plane_state->src_w = src_w;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001571 plane_state->src_h = src_h;
Daniel Vetter042652e2014-07-27 13:46:52 +02001572
Daniel Vetter3671c582015-05-04 15:40:52 +02001573 if (plane == crtc->cursor)
1574 state->legacy_cursor_update = true;
1575
Daniel Vetter042652e2014-07-27 13:46:52 +02001576 ret = drm_atomic_commit(state);
1577 if (ret != 0)
1578 goto fail;
1579
1580 /* Driver takes ownership of state on successful commit. */
1581 return 0;
1582fail:
1583 if (ret == -EDEADLK)
1584 goto backoff;
1585
1586 drm_atomic_state_free(state);
1587
1588 return ret;
1589backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001590 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001591 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001592
1593 /*
1594 * Someone might have exchanged the framebuffer while we dropped locks
1595 * in the backoff code. We need to fix up the fb refcount tracking the
1596 * core does for us.
1597 */
1598 plane->old_fb = plane->fb;
1599
1600 goto retry;
1601}
1602EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1603
1604/**
1605 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1606 * @plane: plane to disable
1607 *
1608 * Provides a default plane disable handler using the atomic driver interface.
1609 *
1610 * RETURNS:
1611 * Zero on success, error code on failure
1612 */
1613int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1614{
1615 struct drm_atomic_state *state;
1616 struct drm_plane_state *plane_state;
1617 int ret = 0;
1618
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001619 /*
1620 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1621 * acquire context. The real fix will be to wire the acquire ctx through
1622 * everywhere we need it, but meanwhile prevent chaos by just skipping
1623 * this noop. The critical case is the cursor ioctls which a) only grab
1624 * crtc/cursor-plane locks (so we need the crtc to get at the right
1625 * acquire context) and b) can try to disable the plane multiple times.
1626 */
1627 if (!plane->crtc)
1628 return 0;
1629
Daniel Vetter042652e2014-07-27 13:46:52 +02001630 state = drm_atomic_state_alloc(plane->dev);
1631 if (!state)
1632 return -ENOMEM;
1633
1634 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1635retry:
1636 plane_state = drm_atomic_get_plane_state(state, plane);
1637 if (IS_ERR(plane_state)) {
1638 ret = PTR_ERR(plane_state);
1639 goto fail;
1640 }
1641
Maarten Lankhorst24e79d02015-11-11 11:29:07 +01001642 if (plane_state->crtc && (plane == plane->crtc->cursor))
1643 plane_state->state->legacy_cursor_update = true;
1644
Rob Clarkbbb1e522015-08-25 15:35:58 -04001645 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001646 if (ret != 0)
1647 goto fail;
Daniel Vetterf02ad902015-01-22 16:36:23 +01001648
Daniel Vetter042652e2014-07-27 13:46:52 +02001649 ret = drm_atomic_commit(state);
1650 if (ret != 0)
1651 goto fail;
1652
1653 /* Driver takes ownership of state on successful commit. */
1654 return 0;
1655fail:
1656 if (ret == -EDEADLK)
1657 goto backoff;
1658
1659 drm_atomic_state_free(state);
1660
1661 return ret;
1662backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001663 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001664 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001665
1666 /*
1667 * Someone might have exchanged the framebuffer while we dropped locks
1668 * in the backoff code. We need to fix up the fb refcount tracking the
1669 * core does for us.
1670 */
1671 plane->old_fb = plane->fb;
1672
1673 goto retry;
1674}
1675EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1676
Rob Clarkbbb1e522015-08-25 15:35:58 -04001677/* just used from fb-helper and atomic-helper: */
1678int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1679 struct drm_plane_state *plane_state)
1680{
1681 int ret;
1682
1683 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1684 if (ret != 0)
1685 return ret;
1686
1687 drm_atomic_set_fb_for_plane(plane_state, NULL);
1688 plane_state->crtc_x = 0;
1689 plane_state->crtc_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001690 plane_state->crtc_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001691 plane_state->crtc_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001692 plane_state->src_x = 0;
1693 plane_state->src_y = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001694 plane_state->src_w = 0;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001695 plane_state->src_h = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001696
Rob Clarkbbb1e522015-08-25 15:35:58 -04001697 return 0;
1698}
1699
Daniel Vetter042652e2014-07-27 13:46:52 +02001700static int update_output_state(struct drm_atomic_state *state,
1701 struct drm_mode_set *set)
1702{
1703 struct drm_device *dev = set->crtc->dev;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001704 struct drm_crtc *crtc;
1705 struct drm_crtc_state *crtc_state;
1706 struct drm_connector *connector;
Daniel Vetter042652e2014-07-27 13:46:52 +02001707 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001708 int ret, i, j;
1709
1710 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1711 state->acquire_ctx);
1712 if (ret)
1713 return ret;
1714
1715 /* First grab all affected connector/crtc states. */
1716 for (i = 0; i < set->num_connectors; i++) {
1717 conn_state = drm_atomic_get_connector_state(state,
1718 set->connectors[i]);
1719 if (IS_ERR(conn_state))
1720 return PTR_ERR(conn_state);
1721 }
1722
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001723 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001724 ret = drm_atomic_add_affected_connectors(state, crtc);
1725 if (ret)
1726 return ret;
1727 }
1728
1729 /* Then recompute connector->crtc links and crtc enabling state. */
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001730 for_each_connector_in_state(state, connector, conn_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001731 if (conn_state->crtc == set->crtc) {
1732 ret = drm_atomic_set_crtc_for_connector(conn_state,
1733 NULL);
1734 if (ret)
1735 return ret;
1736 }
1737
1738 for (j = 0; j < set->num_connectors; j++) {
1739 if (set->connectors[j] == connector) {
1740 ret = drm_atomic_set_crtc_for_connector(conn_state,
1741 set->crtc);
1742 if (ret)
1743 return ret;
1744 break;
1745 }
1746 }
1747 }
1748
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001749 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001750 /* Don't update ->enable for the CRTC in the set_config request,
1751 * since a mismatch would indicate a bug in the upper layers.
1752 * The actual modeset code later on will catch any
1753 * inconsistencies here. */
1754 if (crtc == set->crtc)
1755 continue;
1756
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001757 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1758 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1759 NULL);
1760 if (ret < 0)
1761 return ret;
1762
Maarten Lankhorst9b5edbf2015-06-01 08:59:53 +02001763 crtc_state->active = false;
Laurent Pinchartc30f55a2015-06-22 13:37:46 +03001764 }
Daniel Vetter042652e2014-07-27 13:46:52 +02001765 }
1766
1767 return 0;
1768}
1769
1770/**
1771 * drm_atomic_helper_set_config - set a new config from userspace
1772 * @set: mode set configuration
1773 *
1774 * Provides a default crtc set_config handler using the atomic driver interface.
1775 *
1776 * Returns:
1777 * Returns 0 on success, negative errno numbers on failure.
1778 */
1779int drm_atomic_helper_set_config(struct drm_mode_set *set)
1780{
1781 struct drm_atomic_state *state;
1782 struct drm_crtc *crtc = set->crtc;
Daniel Vetter042652e2014-07-27 13:46:52 +02001783 int ret = 0;
1784
1785 state = drm_atomic_state_alloc(crtc->dev);
1786 if (!state)
1787 return -ENOMEM;
1788
1789 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1790retry:
Rob Clarkbbb1e522015-08-25 15:35:58 -04001791 ret = __drm_atomic_helper_set_config(set, state);
Daniel Stone819364d2015-05-26 14:36:48 +01001792 if (ret != 0)
1793 goto fail;
1794
Daniel Vetter042652e2014-07-27 13:46:52 +02001795 ret = drm_atomic_commit(state);
1796 if (ret != 0)
1797 goto fail;
1798
1799 /* Driver takes ownership of state on successful commit. */
1800 return 0;
1801fail:
1802 if (ret == -EDEADLK)
1803 goto backoff;
1804
1805 drm_atomic_state_free(state);
1806
1807 return ret;
1808backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001809 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001810 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001811
1812 /*
1813 * Someone might have exchanged the framebuffer while we dropped locks
1814 * in the backoff code. We need to fix up the fb refcount tracking the
1815 * core does for us.
1816 */
1817 crtc->primary->old_fb = crtc->primary->fb;
1818
1819 goto retry;
1820}
1821EXPORT_SYMBOL(drm_atomic_helper_set_config);
1822
Rob Clarkbbb1e522015-08-25 15:35:58 -04001823/* just used from fb-helper and atomic-helper: */
1824int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1825 struct drm_atomic_state *state)
1826{
1827 struct drm_crtc_state *crtc_state;
1828 struct drm_plane_state *primary_state;
1829 struct drm_crtc *crtc = set->crtc;
Ville Syrjälä83926112015-11-16 17:02:34 +02001830 int hdisplay, vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001831 int ret;
1832
1833 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1834 if (IS_ERR(crtc_state))
1835 return PTR_ERR(crtc_state);
1836
1837 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1838 if (IS_ERR(primary_state))
1839 return PTR_ERR(primary_state);
1840
1841 if (!set->mode) {
1842 WARN_ON(set->fb);
1843 WARN_ON(set->num_connectors);
1844
1845 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1846 if (ret != 0)
1847 return ret;
1848
1849 crtc_state->active = false;
1850
1851 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1852 if (ret != 0)
1853 return ret;
1854
1855 drm_atomic_set_fb_for_plane(primary_state, NULL);
1856
1857 goto commit;
1858 }
1859
1860 WARN_ON(!set->fb);
1861 WARN_ON(!set->num_connectors);
1862
1863 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1864 if (ret != 0)
1865 return ret;
1866
1867 crtc_state->active = true;
1868
1869 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1870 if (ret != 0)
1871 return ret;
1872
Ville Syrjälä83926112015-11-16 17:02:34 +02001873 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1874
Rob Clarkbbb1e522015-08-25 15:35:58 -04001875 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1876 primary_state->crtc_x = 0;
1877 primary_state->crtc_y = 0;
Ville Syrjälä83926112015-11-16 17:02:34 +02001878 primary_state->crtc_w = hdisplay;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001879 primary_state->crtc_h = vdisplay;
Rob Clarkbbb1e522015-08-25 15:35:58 -04001880 primary_state->src_x = set->x << 16;
1881 primary_state->src_y = set->y << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001882 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
Ville Syrjälä83926112015-11-16 17:02:34 +02001883 primary_state->src_w = vdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001884 primary_state->src_h = hdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001885 } else {
Ville Syrjälä83926112015-11-16 17:02:34 +02001886 primary_state->src_w = hdisplay << 16;
Ville Syrjälä02e6f372015-11-16 17:02:35 +02001887 primary_state->src_h = vdisplay << 16;
Ville Syrjälä41121242015-10-15 20:39:59 +03001888 }
Rob Clarkbbb1e522015-08-25 15:35:58 -04001889
1890commit:
1891 ret = update_output_state(state, set);
1892 if (ret)
1893 return ret;
1894
1895 return 0;
1896}
1897
Daniel Vetter042652e2014-07-27 13:46:52 +02001898/**
Thierry Reding14942762015-12-02 17:50:04 +01001899 * drm_atomic_helper_disable_all - disable all currently active outputs
1900 * @dev: DRM device
1901 * @ctx: lock acquisition context
1902 *
1903 * Loops through all connectors, finding those that aren't turned off and then
1904 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1905 * that they are connected to.
1906 *
1907 * This is used for example in suspend/resume to disable all currently active
1908 * functions when suspending.
1909 *
1910 * Note that if callers haven't already acquired all modeset locks this might
1911 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1912 *
1913 * Returns:
1914 * 0 on success or a negative error code on failure.
1915 *
1916 * See also:
1917 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1918 */
1919int drm_atomic_helper_disable_all(struct drm_device *dev,
1920 struct drm_modeset_acquire_ctx *ctx)
1921{
1922 struct drm_atomic_state *state;
1923 struct drm_connector *conn;
1924 int err;
1925
1926 state = drm_atomic_state_alloc(dev);
1927 if (!state)
1928 return -ENOMEM;
1929
1930 state->acquire_ctx = ctx;
1931
1932 drm_for_each_connector(conn, dev) {
1933 struct drm_crtc *crtc = conn->state->crtc;
1934 struct drm_crtc_state *crtc_state;
1935
1936 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1937 continue;
1938
1939 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1940 if (IS_ERR(crtc_state)) {
1941 err = PTR_ERR(crtc_state);
1942 goto free;
1943 }
1944
1945 crtc_state->active = false;
1946 }
1947
1948 err = drm_atomic_commit(state);
1949
1950free:
1951 if (err < 0)
1952 drm_atomic_state_free(state);
1953
1954 return err;
1955}
1956EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1957
1958/**
1959 * drm_atomic_helper_suspend - subsystem-level suspend helper
1960 * @dev: DRM device
1961 *
1962 * Duplicates the current atomic state, disables all active outputs and then
1963 * returns a pointer to the original atomic state to the caller. Drivers can
1964 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1965 * restore the output configuration that was active at the time the system
1966 * entered suspend.
1967 *
1968 * Note that it is potentially unsafe to use this. The atomic state object
1969 * returned by this function is assumed to be persistent. Drivers must ensure
1970 * that this holds true. Before calling this function, drivers must make sure
1971 * to suspend fbdev emulation so that nothing can be using the device.
1972 *
1973 * Returns:
1974 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1975 * encoded error code on failure. Drivers should store the returned atomic
1976 * state object and pass it to the drm_atomic_helper_resume() helper upon
1977 * resume.
1978 *
1979 * See also:
1980 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1981 * drm_atomic_helper_resume()
1982 */
1983struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1984{
1985 struct drm_modeset_acquire_ctx ctx;
1986 struct drm_atomic_state *state;
1987 int err;
1988
1989 drm_modeset_acquire_init(&ctx, 0);
1990
1991retry:
1992 err = drm_modeset_lock_all_ctx(dev, &ctx);
1993 if (err < 0) {
1994 state = ERR_PTR(err);
1995 goto unlock;
1996 }
1997
1998 state = drm_atomic_helper_duplicate_state(dev, &ctx);
1999 if (IS_ERR(state))
2000 goto unlock;
2001
2002 err = drm_atomic_helper_disable_all(dev, &ctx);
2003 if (err < 0) {
2004 drm_atomic_state_free(state);
2005 state = ERR_PTR(err);
2006 goto unlock;
2007 }
2008
2009unlock:
2010 if (PTR_ERR(state) == -EDEADLK) {
2011 drm_modeset_backoff(&ctx);
2012 goto retry;
2013 }
2014
2015 drm_modeset_drop_locks(&ctx);
2016 drm_modeset_acquire_fini(&ctx);
2017 return state;
2018}
2019EXPORT_SYMBOL(drm_atomic_helper_suspend);
2020
2021/**
2022 * drm_atomic_helper_resume - subsystem-level resume helper
2023 * @dev: DRM device
2024 * @state: atomic state to resume to
2025 *
2026 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2027 * grabs all modeset locks and commits the atomic state object. This can be
2028 * used in conjunction with the drm_atomic_helper_suspend() helper to
2029 * implement suspend/resume for drivers that support atomic mode-setting.
2030 *
2031 * Returns:
2032 * 0 on success or a negative error code on failure.
2033 *
2034 * See also:
2035 * drm_atomic_helper_suspend()
2036 */
2037int drm_atomic_helper_resume(struct drm_device *dev,
2038 struct drm_atomic_state *state)
2039{
2040 struct drm_mode_config *config = &dev->mode_config;
2041 int err;
2042
2043 drm_mode_config_reset(dev);
2044 drm_modeset_lock_all(dev);
2045 state->acquire_ctx = config->acquire_ctx;
2046 err = drm_atomic_commit(state);
2047 drm_modeset_unlock_all(dev);
2048
2049 return err;
2050}
2051EXPORT_SYMBOL(drm_atomic_helper_resume);
2052
2053/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002054 * drm_atomic_helper_crtc_set_property - helper for crtc properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002055 * @crtc: DRM crtc
2056 * @property: DRM property
2057 * @val: value of property
2058 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002059 * Provides a default crtc set_property handler using the atomic driver
2060 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002061 *
2062 * RETURNS:
2063 * Zero on success, error code on failure
2064 */
2065int
2066drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2067 struct drm_property *property,
2068 uint64_t val)
2069{
2070 struct drm_atomic_state *state;
2071 struct drm_crtc_state *crtc_state;
2072 int ret = 0;
2073
2074 state = drm_atomic_state_alloc(crtc->dev);
2075 if (!state)
2076 return -ENOMEM;
2077
2078 /* ->set_property is always called with all locks held. */
2079 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2080retry:
2081 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2082 if (IS_ERR(crtc_state)) {
2083 ret = PTR_ERR(crtc_state);
2084 goto fail;
2085 }
2086
Rob Clark40ecc692014-12-18 16:01:46 -05002087 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2088 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002089 if (ret)
2090 goto fail;
2091
2092 ret = drm_atomic_commit(state);
2093 if (ret != 0)
2094 goto fail;
2095
2096 /* Driver takes ownership of state on successful commit. */
2097 return 0;
2098fail:
2099 if (ret == -EDEADLK)
2100 goto backoff;
2101
2102 drm_atomic_state_free(state);
2103
2104 return ret;
2105backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002106 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002107 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002108
2109 goto retry;
2110}
2111EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2112
2113/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002114 * drm_atomic_helper_plane_set_property - helper for plane properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002115 * @plane: DRM plane
2116 * @property: DRM property
2117 * @val: value of property
2118 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002119 * Provides a default plane set_property handler using the atomic driver
2120 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002121 *
2122 * RETURNS:
2123 * Zero on success, error code on failure
2124 */
2125int
2126drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2127 struct drm_property *property,
2128 uint64_t val)
2129{
2130 struct drm_atomic_state *state;
2131 struct drm_plane_state *plane_state;
2132 int ret = 0;
2133
2134 state = drm_atomic_state_alloc(plane->dev);
2135 if (!state)
2136 return -ENOMEM;
2137
2138 /* ->set_property is always called with all locks held. */
2139 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2140retry:
2141 plane_state = drm_atomic_get_plane_state(state, plane);
2142 if (IS_ERR(plane_state)) {
2143 ret = PTR_ERR(plane_state);
2144 goto fail;
2145 }
2146
Rob Clark40ecc692014-12-18 16:01:46 -05002147 ret = drm_atomic_plane_set_property(plane, plane_state,
2148 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002149 if (ret)
2150 goto fail;
2151
2152 ret = drm_atomic_commit(state);
2153 if (ret != 0)
2154 goto fail;
2155
2156 /* Driver takes ownership of state on successful commit. */
2157 return 0;
2158fail:
2159 if (ret == -EDEADLK)
2160 goto backoff;
2161
2162 drm_atomic_state_free(state);
2163
2164 return ret;
2165backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002166 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002167 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002168
2169 goto retry;
2170}
2171EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2172
2173/**
Laurent Pinchart7f500022015-02-23 02:50:23 +02002174 * drm_atomic_helper_connector_set_property - helper for connector properties
Daniel Vetter042652e2014-07-27 13:46:52 +02002175 * @connector: DRM connector
2176 * @property: DRM property
2177 * @val: value of property
2178 *
Laurent Pinchart7f500022015-02-23 02:50:23 +02002179 * Provides a default connector set_property handler using the atomic driver
2180 * interface.
Daniel Vetter042652e2014-07-27 13:46:52 +02002181 *
2182 * RETURNS:
2183 * Zero on success, error code on failure
2184 */
2185int
2186drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2187 struct drm_property *property,
2188 uint64_t val)
2189{
2190 struct drm_atomic_state *state;
2191 struct drm_connector_state *connector_state;
2192 int ret = 0;
2193
2194 state = drm_atomic_state_alloc(connector->dev);
2195 if (!state)
2196 return -ENOMEM;
2197
2198 /* ->set_property is always called with all locks held. */
2199 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2200retry:
2201 connector_state = drm_atomic_get_connector_state(state, connector);
2202 if (IS_ERR(connector_state)) {
2203 ret = PTR_ERR(connector_state);
2204 goto fail;
2205 }
2206
Rob Clark40ecc692014-12-18 16:01:46 -05002207 ret = drm_atomic_connector_set_property(connector, connector_state,
2208 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02002209 if (ret)
2210 goto fail;
2211
2212 ret = drm_atomic_commit(state);
2213 if (ret != 0)
2214 goto fail;
2215
2216 /* Driver takes ownership of state on successful commit. */
2217 return 0;
2218fail:
2219 if (ret == -EDEADLK)
2220 goto backoff;
2221
2222 drm_atomic_state_free(state);
2223
2224 return ret;
2225backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02002226 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002227 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02002228
2229 goto retry;
2230}
2231EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002232
2233/**
2234 * drm_atomic_helper_page_flip - execute a legacy page flip
2235 * @crtc: DRM crtc
2236 * @fb: DRM framebuffer
2237 * @event: optional DRM event to signal upon completion
2238 * @flags: flip flags for non-vblank sync'ed updates
2239 *
2240 * Provides a default page flip implementation using the atomic driver interface.
2241 *
2242 * Note that for now so called async page flips (i.e. updates which are not
2243 * synchronized to vblank) are not supported, since the atomic interfaces have
2244 * no provisions for this yet.
2245 *
2246 * Returns:
2247 * Returns 0 on success, negative errno numbers on failure.
2248 */
2249int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2250 struct drm_framebuffer *fb,
2251 struct drm_pending_vblank_event *event,
2252 uint32_t flags)
2253{
2254 struct drm_plane *plane = crtc->primary;
2255 struct drm_atomic_state *state;
2256 struct drm_plane_state *plane_state;
2257 struct drm_crtc_state *crtc_state;
2258 int ret = 0;
2259
2260 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2261 return -EINVAL;
2262
2263 state = drm_atomic_state_alloc(plane->dev);
2264 if (!state)
2265 return -ENOMEM;
2266
2267 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2268retry:
2269 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2270 if (IS_ERR(crtc_state)) {
2271 ret = PTR_ERR(crtc_state);
2272 goto fail;
2273 }
2274 crtc_state->event = event;
2275
2276 plane_state = drm_atomic_get_plane_state(state, plane);
2277 if (IS_ERR(plane_state)) {
2278 ret = PTR_ERR(plane_state);
2279 goto fail;
2280 }
2281
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002282 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002283 if (ret != 0)
2284 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01002285 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002286
2287 ret = drm_atomic_async_commit(state);
2288 if (ret != 0)
2289 goto fail;
2290
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002291 /* Driver takes ownership of state on successful async commit. */
2292 return 0;
2293fail:
2294 if (ret == -EDEADLK)
2295 goto backoff;
2296
2297 drm_atomic_state_free(state);
2298
2299 return ret;
2300backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002301 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01002302 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02002303
2304 /*
2305 * Someone might have exchanged the framebuffer while we dropped locks
2306 * in the backoff code. We need to fix up the fb refcount tracking the
2307 * core does for us.
2308 */
2309 plane->old_fb = plane->fb;
2310
2311 goto retry;
2312}
2313EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01002314
2315/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002316 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2317 * @connector: affected connector
2318 * @mode: DPMS mode
2319 *
2320 * This is the main helper function provided by the atomic helper framework for
2321 * implementing the legacy DPMS connector interface. It computes the new desired
2322 * ->active state for the corresponding CRTC (if the connector is enabled) and
2323 * updates it.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002324 *
2325 * Returns:
2326 * Returns 0 on success, negative errno numbers on failure.
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002327 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002328int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2329 int mode)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002330{
2331 struct drm_mode_config *config = &connector->dev->mode_config;
2332 struct drm_atomic_state *state;
2333 struct drm_crtc_state *crtc_state;
2334 struct drm_crtc *crtc;
2335 struct drm_connector *tmp_connector;
2336 int ret;
2337 bool active = false;
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002338 int old_mode = connector->dpms;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002339
2340 if (mode != DRM_MODE_DPMS_ON)
2341 mode = DRM_MODE_DPMS_OFF;
2342
2343 connector->dpms = mode;
2344 crtc = connector->state->crtc;
2345
2346 if (!crtc)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002347 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002348
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002349 state = drm_atomic_state_alloc(connector->dev);
2350 if (!state)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002351 return -ENOMEM;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002352
2353 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2354retry:
2355 crtc_state = drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002356 if (IS_ERR(crtc_state)) {
2357 ret = PTR_ERR(crtc_state);
2358 goto fail;
2359 }
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002360
2361 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2362
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02002363 drm_for_each_connector(tmp_connector, connector->dev) {
John Hunter0388df02015-03-17 15:30:28 +08002364 if (tmp_connector->state->crtc != crtc)
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002365 continue;
2366
John Hunter0388df02015-03-17 15:30:28 +08002367 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002368 active = true;
2369 break;
2370 }
2371 }
2372 crtc_state->active = active;
2373
2374 ret = drm_atomic_commit(state);
2375 if (ret != 0)
2376 goto fail;
2377
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002378 /* Driver takes ownership of state on successful commit. */
2379 return 0;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002380fail:
2381 if (ret == -EDEADLK)
2382 goto backoff;
2383
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002384 connector->dpms = old_mode;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002385 drm_atomic_state_free(state);
2386
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002387 return ret;
Daniel Vetterb486e0e2015-01-22 18:53:05 +01002388backoff:
2389 drm_atomic_state_clear(state);
2390 drm_atomic_legacy_backoff(state);
2391
2392 goto retry;
2393}
2394EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2395
2396/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002397 * DOC: atomic state reset and initialization
2398 *
2399 * Both the drm core and the atomic helpers assume that there is always the full
2400 * and correct atomic software state for all connectors, CRTCs and planes
2401 * available. Which is a bit a problem on driver load and also after system
2402 * suspend. One way to solve this is to have a hardware state read-out
2403 * infrastructure which reconstructs the full software state (e.g. the i915
2404 * driver).
2405 *
2406 * The simpler solution is to just reset the software state to everything off,
2407 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2408 * the atomic helpers provide default reset implementations for all hooks.
Daniel Vetter7f8ee3e2015-12-04 09:46:06 +01002409 *
2410 * On the upside the precise state tracking of atomic simplifies system suspend
2411 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2412 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2413 * For other drivers the building blocks are split out, see the documentation
2414 * for these functions.
Daniel Vetter3150c7d2014-11-06 20:53:29 +01002415 */
2416
2417/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002418 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2419 * @crtc: drm CRTC
2420 *
2421 * Resets the atomic state for @crtc by freeing the state pointer (which might
2422 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2423 */
2424void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2425{
Markus Elfring5f911902015-11-06 12:03:46 +01002426 if (crtc->state)
Daniel Stone99cf4a22015-05-25 19:11:51 +01002427 drm_property_unreference_blob(crtc->state->mode_blob);
Daniel Vetterd4617012014-11-03 15:56:43 +01002428 kfree(crtc->state);
2429 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002430
2431 if (crtc->state)
2432 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002433}
2434EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2435
2436/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002437 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2438 * @crtc: CRTC object
2439 * @state: atomic CRTC state
2440 *
2441 * Copies atomic state from a CRTC's current state and resets inferred values.
2442 * This is useful for drivers that subclass the CRTC state.
2443 */
2444void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2445 struct drm_crtc_state *state)
2446{
2447 memcpy(state, crtc->state, sizeof(*state));
2448
Daniel Stone99cf4a22015-05-25 19:11:51 +01002449 if (state->mode_blob)
2450 drm_property_reference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002451 state->mode_changed = false;
2452 state->active_changed = false;
2453 state->planes_changed = false;
Maarten Lankhorstfc596662015-07-21 13:28:57 +02002454 state->connectors_changed = false;
Thierry Redingf5e78402015-01-28 14:54:32 +01002455 state->event = NULL;
2456}
2457EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2458
2459/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002460 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2461 * @crtc: drm CRTC
2462 *
2463 * Default CRTC state duplicate hook for drivers which don't have their own
2464 * subclassed CRTC state structure.
2465 */
2466struct drm_crtc_state *
2467drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2468{
2469 struct drm_crtc_state *state;
2470
2471 if (WARN_ON(!crtc->state))
2472 return NULL;
2473
Thierry Redingf5e78402015-01-28 14:54:32 +01002474 state = kmalloc(sizeof(*state), GFP_KERNEL);
2475 if (state)
2476 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002477
2478 return state;
2479}
2480EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2481
2482/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002483 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2484 * @crtc: CRTC object
2485 * @state: CRTC state object to release
2486 *
2487 * Releases all resources stored in the CRTC state without actually freeing
2488 * the memory of the CRTC state. This is useful for drivers that subclass the
2489 * CRTC state.
2490 */
2491void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2492 struct drm_crtc_state *state)
2493{
Markus Elfring5f911902015-11-06 12:03:46 +01002494 drm_property_unreference_blob(state->mode_blob);
Thierry Redingf5e78402015-01-28 14:54:32 +01002495}
2496EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2497
2498/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002499 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2500 * @crtc: drm CRTC
2501 * @state: CRTC state object to release
2502 *
2503 * Default CRTC state destroy hook for drivers which don't have their own
2504 * subclassed CRTC state structure.
2505 */
2506void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2507 struct drm_crtc_state *state)
2508{
Thierry Redingf5e78402015-01-28 14:54:32 +01002509 __drm_atomic_helper_crtc_destroy_state(crtc, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002510 kfree(state);
2511}
2512EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2513
2514/**
2515 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2516 * @plane: drm plane
2517 *
2518 * Resets the atomic state for @plane by freeing the state pointer (which might
2519 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2520 */
2521void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2522{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002523 if (plane->state && plane->state->fb)
2524 drm_framebuffer_unreference(plane->state->fb);
2525
Daniel Vetterd4617012014-11-03 15:56:43 +01002526 kfree(plane->state);
2527 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002528
2529 if (plane->state)
2530 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01002531}
2532EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2533
2534/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002535 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2536 * @plane: plane object
2537 * @state: atomic plane state
2538 *
2539 * Copies atomic state from a plane's current state. This is useful for
2540 * drivers that subclass the plane state.
2541 */
2542void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2543 struct drm_plane_state *state)
2544{
2545 memcpy(state, plane->state, sizeof(*state));
2546
2547 if (state->fb)
2548 drm_framebuffer_reference(state->fb);
2549}
2550EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2551
2552/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002553 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2554 * @plane: drm plane
2555 *
2556 * Default plane state duplicate hook for drivers which don't have their own
2557 * subclassed plane state structure.
2558 */
2559struct drm_plane_state *
2560drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2561{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002562 struct drm_plane_state *state;
2563
Daniel Vetterd4617012014-11-03 15:56:43 +01002564 if (WARN_ON(!plane->state))
2565 return NULL;
2566
Thierry Redingf5e78402015-01-28 14:54:32 +01002567 state = kmalloc(sizeof(*state), GFP_KERNEL);
2568 if (state)
2569 __drm_atomic_helper_plane_duplicate_state(plane, state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01002570
2571 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002572}
2573EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2574
2575/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002576 * __drm_atomic_helper_plane_destroy_state - release plane state
2577 * @plane: plane object
2578 * @state: plane state object to release
2579 *
2580 * Releases all resources stored in the plane state without actually freeing
2581 * the memory of the plane state. This is useful for drivers that subclass the
2582 * plane state.
2583 */
2584void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2585 struct drm_plane_state *state)
2586{
2587 if (state->fb)
2588 drm_framebuffer_unreference(state->fb);
2589}
2590EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2591
2592/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002593 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2594 * @plane: drm plane
2595 * @state: plane state object to release
2596 *
2597 * Default plane state destroy hook for drivers which don't have their own
2598 * subclassed plane state structure.
2599 */
2600void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002601 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002602{
Thierry Redingf5e78402015-01-28 14:54:32 +01002603 __drm_atomic_helper_plane_destroy_state(plane, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002604 kfree(state);
2605}
2606EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2607
2608/**
2609 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2610 * @connector: drm connector
2611 *
2612 * Resets the atomic state for @connector by freeing the state pointer (which
2613 * might be NULL, e.g. at driver load time) and allocating a new empty state
2614 * object.
2615 */
2616void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2617{
2618 kfree(connector->state);
2619 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002620
2621 if (connector->state)
2622 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002623}
2624EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2625
2626/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002627 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2628 * @connector: connector object
2629 * @state: atomic connector state
2630 *
2631 * Copies atomic state from a connector's current state. This is useful for
2632 * drivers that subclass the connector state.
2633 */
2634void
2635__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2636 struct drm_connector_state *state)
2637{
2638 memcpy(state, connector->state, sizeof(*state));
2639}
2640EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2641
2642/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002643 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2644 * @connector: drm connector
2645 *
2646 * Default connector state duplicate hook for drivers which don't have their own
2647 * subclassed connector state structure.
2648 */
2649struct drm_connector_state *
2650drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2651{
Thierry Redingf5e78402015-01-28 14:54:32 +01002652 struct drm_connector_state *state;
2653
Daniel Vetterd4617012014-11-03 15:56:43 +01002654 if (WARN_ON(!connector->state))
2655 return NULL;
2656
Thierry Redingf5e78402015-01-28 14:54:32 +01002657 state = kmalloc(sizeof(*state), GFP_KERNEL);
2658 if (state)
2659 __drm_atomic_helper_connector_duplicate_state(connector, state);
2660
2661 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002662}
2663EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2664
2665/**
Thierry Reding397fd772015-09-08 15:00:45 +02002666 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2667 * @dev: DRM device
2668 * @ctx: lock acquisition context
2669 *
2670 * Makes a copy of the current atomic state by looping over all objects and
Thierry Reding14942762015-12-02 17:50:04 +01002671 * duplicating their respective states. This is used for example by suspend/
2672 * resume support code to save the state prior to suspend such that it can
2673 * be restored upon resume.
Thierry Reding397fd772015-09-08 15:00:45 +02002674 *
2675 * Note that this treats atomic state as persistent between save and restore.
2676 * Drivers must make sure that this is possible and won't result in confusion
2677 * or erroneous behaviour.
2678 *
2679 * Note that if callers haven't already acquired all modeset locks this might
2680 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2681 *
2682 * Returns:
2683 * A pointer to the copy of the atomic state object on success or an
2684 * ERR_PTR()-encoded error code on failure.
Thierry Reding14942762015-12-02 17:50:04 +01002685 *
2686 * See also:
2687 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Thierry Reding397fd772015-09-08 15:00:45 +02002688 */
2689struct drm_atomic_state *
2690drm_atomic_helper_duplicate_state(struct drm_device *dev,
2691 struct drm_modeset_acquire_ctx *ctx)
2692{
2693 struct drm_atomic_state *state;
2694 struct drm_connector *conn;
2695 struct drm_plane *plane;
2696 struct drm_crtc *crtc;
2697 int err = 0;
2698
2699 state = drm_atomic_state_alloc(dev);
2700 if (!state)
2701 return ERR_PTR(-ENOMEM);
2702
2703 state->acquire_ctx = ctx;
2704
2705 drm_for_each_crtc(crtc, dev) {
2706 struct drm_crtc_state *crtc_state;
2707
2708 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2709 if (IS_ERR(crtc_state)) {
2710 err = PTR_ERR(crtc_state);
2711 goto free;
2712 }
2713 }
2714
2715 drm_for_each_plane(plane, dev) {
2716 struct drm_plane_state *plane_state;
2717
2718 plane_state = drm_atomic_get_plane_state(state, plane);
2719 if (IS_ERR(plane_state)) {
2720 err = PTR_ERR(plane_state);
2721 goto free;
2722 }
2723 }
2724
2725 drm_for_each_connector(conn, dev) {
2726 struct drm_connector_state *conn_state;
2727
2728 conn_state = drm_atomic_get_connector_state(state, conn);
2729 if (IS_ERR(conn_state)) {
2730 err = PTR_ERR(conn_state);
2731 goto free;
2732 }
2733 }
2734
2735 /* clear the acquire context so that it isn't accidentally reused */
2736 state->acquire_ctx = NULL;
2737
2738free:
2739 if (err < 0) {
2740 drm_atomic_state_free(state);
2741 state = ERR_PTR(err);
2742 }
2743
2744 return state;
2745}
2746EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2747
2748/**
Thierry Redingf5e78402015-01-28 14:54:32 +01002749 * __drm_atomic_helper_connector_destroy_state - release connector state
2750 * @connector: connector object
2751 * @state: connector state object to release
2752 *
2753 * Releases all resources stored in the connector state without actually
2754 * freeing the memory of the connector state. This is useful for drivers that
2755 * subclass the connector state.
2756 */
2757void
2758__drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2759 struct drm_connector_state *state)
2760{
2761 /*
2762 * This is currently a placeholder so that drivers that subclass the
2763 * state will automatically do the right thing if code is ever added
2764 * to this function.
2765 */
2766}
2767EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2768
2769/**
Daniel Vetterd4617012014-11-03 15:56:43 +01002770 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2771 * @connector: drm connector
2772 * @state: connector state object to release
2773 *
2774 * Default connector state destroy hook for drivers which don't have their own
2775 * subclassed connector state structure.
2776 */
2777void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2778 struct drm_connector_state *state)
2779{
Thierry Redingf5e78402015-01-28 14:54:32 +01002780 __drm_atomic_helper_connector_destroy_state(connector, state);
Daniel Vetterd4617012014-11-03 15:56:43 +01002781 kfree(state);
2782}
2783EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);