blob: d0c3611402da4200fa335337db7d941f7f181b04 [file] [log] [blame]
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28#include <drm/drmP.h>
29#include <drm/drm_atomic.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020032#include <drm/drm_atomic_helper.h>
Daniel Vettere2330f02014-10-29 11:34:56 +010033#include <linux/fence.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010034
Daniel Vetter3150c7d2014-11-06 20:53:29 +010035/**
36 * DOC: overview
37 *
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
43 *
44 * This library also provides default implementations for the check callback in
45 * drm_atomic_helper_check and for the commit callback with
46 * drm_atomic_helper_commit. But the individual stages and callbacks are expose
47 * to allow drivers to mix and match and e.g. use the plane helpers only
48 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
51 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config,
52 * drm_atomic_helper_disable_plane, drm_atomic_helper_disable_plane and the
53 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
55 */
Daniel Vetterc2fcd272014-11-05 00:14:14 +010056static void
57drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
58 struct drm_plane_state *plane_state,
59 struct drm_plane *plane)
60{
61 struct drm_crtc_state *crtc_state;
62
63 if (plane->state->crtc) {
Rob Clark4b08eae2014-12-08 10:45:43 -050064 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
Daniel Vetterc2fcd272014-11-05 00:14:14 +010065
66 if (WARN_ON(!crtc_state))
67 return;
68
69 crtc_state->planes_changed = true;
70 }
71
72 if (plane_state->crtc) {
73 crtc_state =
74 state->crtc_states[drm_crtc_index(plane_state->crtc)];
75
76 if (WARN_ON(!crtc_state))
77 return;
78
79 crtc_state->planes_changed = true;
80 }
81}
82
Daniel Vetter623369e2014-09-16 17:50:47 +020083static struct drm_crtc *
84get_current_crtc_for_encoder(struct drm_device *dev,
85 struct drm_encoder *encoder)
86{
87 struct drm_mode_config *config = &dev->mode_config;
88 struct drm_connector *connector;
89
90 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
91
92 list_for_each_entry(connector, &config->connector_list, head) {
93 if (connector->state->best_encoder != encoder)
94 continue;
95
96 return connector->state->crtc;
97 }
98
99 return NULL;
100}
101
102static int
103steal_encoder(struct drm_atomic_state *state,
104 struct drm_encoder *encoder,
105 struct drm_crtc *encoder_crtc)
106{
107 struct drm_mode_config *config = &state->dev->mode_config;
108 struct drm_crtc_state *crtc_state;
109 struct drm_connector *connector;
110 struct drm_connector_state *connector_state;
Daniel Vetter042652e2014-07-27 13:46:52 +0200111 int ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200112
113 /*
114 * We can only steal an encoder coming from a connector, which means we
115 * must already hold the connection_mutex.
116 */
117 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118
119 DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
120 encoder->base.id, encoder->name,
121 encoder_crtc->base.id);
122
123 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
124 if (IS_ERR(crtc_state))
125 return PTR_ERR(crtc_state);
126
127 crtc_state->mode_changed = true;
128
129 list_for_each_entry(connector, &config->connector_list, head) {
130 if (connector->state->best_encoder != encoder)
131 continue;
132
133 DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
134 connector->base.id,
135 connector->name);
136
137 connector_state = drm_atomic_get_connector_state(state,
138 connector);
139 if (IS_ERR(connector_state))
140 return PTR_ERR(connector_state);
141
Daniel Vetter042652e2014-07-27 13:46:52 +0200142 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
143 if (ret)
144 return ret;
Daniel Vetter623369e2014-09-16 17:50:47 +0200145 connector_state->best_encoder = NULL;
146 }
147
148 return 0;
149}
150
151static int
152update_connector_routing(struct drm_atomic_state *state, int conn_idx)
153{
154 struct drm_connector_helper_funcs *funcs;
155 struct drm_encoder *new_encoder;
156 struct drm_crtc *encoder_crtc;
157 struct drm_connector *connector;
158 struct drm_connector_state *connector_state;
159 struct drm_crtc_state *crtc_state;
160 int idx, ret;
161
162 connector = state->connectors[conn_idx];
163 connector_state = state->connector_states[conn_idx];
164
165 if (!connector)
166 return 0;
167
168 DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
169 connector->base.id,
170 connector->name);
171
172 if (connector->state->crtc != connector_state->crtc) {
173 if (connector->state->crtc) {
174 idx = drm_crtc_index(connector->state->crtc);
175
176 crtc_state = state->crtc_states[idx];
177 crtc_state->mode_changed = true;
178 }
179
180 if (connector_state->crtc) {
181 idx = drm_crtc_index(connector_state->crtc);
182
183 crtc_state = state->crtc_states[idx];
184 crtc_state->mode_changed = true;
185 }
186 }
187
188 if (!connector_state->crtc) {
189 DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
190 connector->base.id,
191 connector->name);
192
193 connector_state->best_encoder = NULL;
194
195 return 0;
196 }
197
198 funcs = connector->helper_private;
199 new_encoder = funcs->best_encoder(connector);
200
201 if (!new_encoder) {
202 DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
203 connector->base.id,
204 connector->name);
205 return -EINVAL;
206 }
207
208 if (new_encoder == connector_state->best_encoder) {
209 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
210 connector->base.id,
211 connector->name,
212 new_encoder->base.id,
213 new_encoder->name,
214 connector_state->crtc->base.id);
215
216 return 0;
217 }
218
219 encoder_crtc = get_current_crtc_for_encoder(state->dev,
220 new_encoder);
221
222 if (encoder_crtc) {
223 ret = steal_encoder(state, new_encoder, encoder_crtc);
224 if (ret) {
225 DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
226 connector->base.id,
227 connector->name);
228 return ret;
229 }
230 }
231
232 connector_state->best_encoder = new_encoder;
233 idx = drm_crtc_index(connector_state->crtc);
234
235 crtc_state = state->crtc_states[idx];
236 crtc_state->mode_changed = true;
237
238 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
239 connector->base.id,
240 connector->name,
241 new_encoder->base.id,
242 new_encoder->name,
243 connector_state->crtc->base.id);
244
245 return 0;
246}
247
248static int
249mode_fixup(struct drm_atomic_state *state)
250{
251 int ncrtcs = state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200252 struct drm_crtc_state *crtc_state;
253 struct drm_connector_state *conn_state;
254 int i;
255 bool ret;
256
257 for (i = 0; i < ncrtcs; i++) {
258 crtc_state = state->crtc_states[i];
259
260 if (!crtc_state || !crtc_state->mode_changed)
261 continue;
262
263 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
264 }
265
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100266 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200267 struct drm_encoder_helper_funcs *funcs;
268 struct drm_encoder *encoder;
269
270 conn_state = state->connector_states[i];
271
272 if (!conn_state)
273 continue;
274
275 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
276
277 if (!conn_state->crtc || !conn_state->best_encoder)
278 continue;
279
280 crtc_state =
281 state->crtc_states[drm_crtc_index(conn_state->crtc)];
282
283 /*
284 * Each encoder has at most one connector (since we always steal
285 * it away), so we won't call ->mode_fixup twice.
286 */
287 encoder = conn_state->best_encoder;
288 funcs = encoder->helper_private;
289
290 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
291 ret = encoder->bridge->funcs->mode_fixup(
292 encoder->bridge, &crtc_state->mode,
293 &crtc_state->adjusted_mode);
294 if (!ret) {
295 DRM_DEBUG_KMS("Bridge fixup failed\n");
296 return -EINVAL;
297 }
298 }
299
300
301 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
302 &crtc_state->adjusted_mode);
303 if (!ret) {
304 DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
305 encoder->base.id, encoder->name);
306 return -EINVAL;
307 }
308 }
309
310 for (i = 0; i < ncrtcs; i++) {
311 struct drm_crtc_helper_funcs *funcs;
312 struct drm_crtc *crtc;
313
314 crtc_state = state->crtc_states[i];
315 crtc = state->crtcs[i];
316
317 if (!crtc_state || !crtc_state->mode_changed)
318 continue;
319
320 funcs = crtc->helper_private;
321 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
322 &crtc_state->adjusted_mode);
323 if (!ret) {
324 DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
325 crtc->base.id);
326 return -EINVAL;
327 }
328 }
329
330 return 0;
331}
332
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100333static bool
334needs_modeset(struct drm_crtc_state *state)
335{
336 return state->mode_changed || state->active_changed;
337}
338
Daniel Vetterd9b13622014-11-26 16:57:41 +0100339/**
340 * drm_atomic_helper_check - validate state object for modeset changes
341 * @dev: DRM device
342 * @state: the driver state object
343 *
344 * Check the state object to see if the requested state is physically possible.
345 * This does all the crtc and connector related computations for an atomic
346 * update. It computes and updates crtc_state->mode_changed, adds any additional
347 * connectors needed for full modesets and calls down into ->mode_fixup
348 * functions of the driver backend.
349 *
350 * IMPORTANT:
351 *
352 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
353 * plane update can't be done without a full modeset) _must_ call this function
354 * afterwards after that change. It is permitted to call this function multiple
355 * times for the same update, e.g. when the ->atomic_check functions depend upon
356 * the adjusted dotclock for fifo space allocation and watermark computation.
357 *
358 * RETURNS
359 * Zero for success or -errno
360 */
361int
Rob Clark934ce1c2014-11-19 16:41:33 -0500362drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200363 struct drm_atomic_state *state)
364{
365 int ncrtcs = dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200366 struct drm_crtc *crtc;
367 struct drm_crtc_state *crtc_state;
368 int i, ret;
369
370 for (i = 0; i < ncrtcs; i++) {
371 crtc = state->crtcs[i];
372 crtc_state = state->crtc_states[i];
373
374 if (!crtc)
375 continue;
376
377 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
378 DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
379 crtc->base.id);
380 crtc_state->mode_changed = true;
381 }
382
383 if (crtc->state->enable != crtc_state->enable) {
384 DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
385 crtc->base.id);
386 crtc_state->mode_changed = true;
387 }
388 }
389
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100390 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200391 /*
392 * This only sets crtc->mode_changed for routing changes,
393 * drivers must set crtc->mode_changed themselves when connector
394 * properties need to be updated.
395 */
396 ret = update_connector_routing(state, i);
397 if (ret)
398 return ret;
399 }
400
401 /*
402 * After all the routing has been prepared we need to add in any
403 * connector which is itself unchanged, but who's crtc changes it's
404 * configuration. This must be done before calling mode_fixup in case a
405 * crtc only changed its mode but has the same set of connectors.
406 */
407 for (i = 0; i < ncrtcs; i++) {
408 int num_connectors;
409
410 crtc = state->crtcs[i];
411 crtc_state = state->crtc_states[i];
412
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100413 if (!crtc)
Daniel Vetter623369e2014-09-16 17:50:47 +0200414 continue;
415
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100416 /*
417 * We must set ->active_changed after walking connectors for
418 * otherwise an update that only changes active would result in
419 * a full modeset because update_connector_routing force that.
420 */
421 if (crtc->state->active != crtc_state->active) {
422 DRM_DEBUG_KMS("[CRTC:%d] active changed\n",
423 crtc->base.id);
424 crtc_state->active_changed = true;
425 }
426
427 if (!needs_modeset(crtc_state))
428 continue;
429
430 DRM_DEBUG_KMS("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
Daniel Vetter623369e2014-09-16 17:50:47 +0200431 crtc->base.id,
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100432 crtc_state->enable ? 'y' : 'n',
433 crtc_state->active ? 'y' : 'n');
Daniel Vetter623369e2014-09-16 17:50:47 +0200434
435 ret = drm_atomic_add_affected_connectors(state, crtc);
436 if (ret != 0)
437 return ret;
438
439 num_connectors = drm_atomic_connectors_for_crtc(state,
440 crtc);
441
442 if (crtc_state->enable != !!num_connectors) {
443 DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
444 crtc->base.id);
445
446 return -EINVAL;
447 }
448 }
449
450 return mode_fixup(state);
451}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100452EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200453
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100454/**
Daniel Vetterd9b13622014-11-26 16:57:41 +0100455 * drm_atomic_helper_check - validate state object for modeset changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100456 * @dev: DRM device
457 * @state: the driver state object
458 *
459 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100460 * This does all the plane update related checks using by calling into the
461 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100462 *
463 * RETURNS
464 * Zero for success or -errno
465 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100466int
467drm_atomic_helper_check_planes(struct drm_device *dev,
468 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100469{
470 int nplanes = dev->mode_config.num_total_plane;
471 int ncrtcs = dev->mode_config.num_crtc;
472 int i, ret = 0;
473
474 for (i = 0; i < nplanes; i++) {
475 struct drm_plane_helper_funcs *funcs;
476 struct drm_plane *plane = state->planes[i];
477 struct drm_plane_state *plane_state = state->plane_states[i];
478
479 if (!plane)
480 continue;
481
482 funcs = plane->helper_private;
483
484 drm_atomic_helper_plane_changed(state, plane_state, plane);
485
486 if (!funcs || !funcs->atomic_check)
487 continue;
488
489 ret = funcs->atomic_check(plane, plane_state);
490 if (ret) {
Rob Clark5e743732014-12-18 16:01:51 -0500491 DRM_DEBUG_KMS("[PLANE:%d] atomic driver check failed\n",
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100492 plane->base.id);
493 return ret;
494 }
495 }
496
497 for (i = 0; i < ncrtcs; i++) {
498 struct drm_crtc_helper_funcs *funcs;
499 struct drm_crtc *crtc = state->crtcs[i];
500
501 if (!crtc)
502 continue;
503
504 funcs = crtc->helper_private;
505
506 if (!funcs || !funcs->atomic_check)
507 continue;
508
509 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
510 if (ret) {
Rob Clark5e743732014-12-18 16:01:51 -0500511 DRM_DEBUG_KMS("[CRTC:%d] atomic driver check failed\n",
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100512 crtc->base.id);
513 return ret;
514 }
515 }
516
Daniel Vetterd9b13622014-11-26 16:57:41 +0100517 return ret;
518}
519EXPORT_SYMBOL(drm_atomic_helper_check_planes);
520
521/**
522 * drm_atomic_helper_check - validate state object
523 * @dev: DRM device
524 * @state: the driver state object
525 *
526 * Check the state object to see if the requested state is physically possible.
527 * Only crtcs and planes have check callbacks, so for any additional (global)
528 * checking that a driver needs it can simply wrap that around this function.
529 * Drivers without such needs can directly use this as their ->atomic_check()
530 * callback.
531 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100532 * This just wraps the two parts of the state checking for planes and modeset
533 * state in the default order: First it calls drm_atomic_helper_check_modeset()
534 * and then drm_atomic_helper_check_planes(). The assumption is that the
535 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
536 * e.g. properly compute watermarks.
537 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100538 * RETURNS
539 * Zero for success or -errno
540 */
541int drm_atomic_helper_check(struct drm_device *dev,
542 struct drm_atomic_state *state)
543{
544 int ret;
545
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100546 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100547 if (ret)
548 return ret;
549
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100550 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500551 if (ret)
552 return ret;
553
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100554 return ret;
555}
556EXPORT_SYMBOL(drm_atomic_helper_check);
557
Daniel Vetter623369e2014-09-16 17:50:47 +0200558static void
559disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
560{
561 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200562 int i;
563
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100564 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200565 struct drm_connector_state *old_conn_state;
566 struct drm_connector *connector;
567 struct drm_encoder_helper_funcs *funcs;
568 struct drm_encoder *encoder;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100569 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200570
571 old_conn_state = old_state->connector_states[i];
572 connector = old_state->connectors[i];
573
574 /* Shut down everything that's in the changeset and currently
575 * still on. So need to check the old, saved state. */
576 if (!old_conn_state || !old_conn_state->crtc)
577 continue;
578
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100579 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
580
581 if (!old_crtc_state->active)
582 continue;
583
Rob Clark46df9ad2014-11-20 15:40:36 -0500584 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200585
Rob Clark46df9ad2014-11-20 15:40:36 -0500586 /* We shouldn't get this far if we didn't previously have
587 * an encoder.. but WARN_ON() rather than explode.
588 */
589 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200590 continue;
591
592 funcs = encoder->helper_private;
593
594 /*
595 * Each encoder has at most one connector (since we always steal
596 * it away), so we won't call call disable hooks twice.
597 */
598 if (encoder->bridge)
599 encoder->bridge->funcs->disable(encoder->bridge);
600
601 /* Right function depends upon target state. */
602 if (connector->state->crtc)
603 funcs->prepare(encoder);
604 else if (funcs->disable)
605 funcs->disable(encoder);
606 else
607 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
608
609 if (encoder->bridge)
610 encoder->bridge->funcs->post_disable(encoder->bridge);
611 }
612
613 for (i = 0; i < ncrtcs; i++) {
614 struct drm_crtc_helper_funcs *funcs;
615 struct drm_crtc *crtc;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100616 struct drm_crtc_state *old_crtc_state;
Daniel Vetter623369e2014-09-16 17:50:47 +0200617
618 crtc = old_state->crtcs[i];
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100619 old_crtc_state = old_state->crtc_states[i];
Daniel Vetter623369e2014-09-16 17:50:47 +0200620
621 /* Shut down everything that needs a full modeset. */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100622 if (!crtc || !needs_modeset(crtc->state))
623 continue;
624
625 if (!old_crtc_state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200626 continue;
627
628 funcs = crtc->helper_private;
629
630 /* Right function depends upon target state. */
631 if (crtc->state->enable)
632 funcs->prepare(crtc);
633 else if (funcs->disable)
634 funcs->disable(crtc);
635 else
636 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
637 }
638}
639
640static void
641set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
642{
Daniel Vetter623369e2014-09-16 17:50:47 +0200643 int ncrtcs = old_state->dev->mode_config.num_crtc;
644 int i;
645
646 /* clear out existing links */
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100647 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200648 struct drm_connector *connector;
649
650 connector = old_state->connectors[i];
651
652 if (!connector || !connector->encoder)
653 continue;
654
655 WARN_ON(!connector->encoder->crtc);
656
657 connector->encoder->crtc = NULL;
658 connector->encoder = NULL;
659 }
660
661 /* set new links */
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100662 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200663 struct drm_connector *connector;
664
665 connector = old_state->connectors[i];
666
667 if (!connector || !connector->state->crtc)
668 continue;
669
670 if (WARN_ON(!connector->state->best_encoder))
671 continue;
672
673 connector->encoder = connector->state->best_encoder;
674 connector->encoder->crtc = connector->state->crtc;
675 }
676
677 /* set legacy state in the crtc structure */
678 for (i = 0; i < ncrtcs; i++) {
679 struct drm_crtc *crtc;
680
681 crtc = old_state->crtcs[i];
682
683 if (!crtc)
684 continue;
685
686 crtc->mode = crtc->state->mode;
687 crtc->enabled = crtc->state->enable;
688 crtc->x = crtc->primary->state->src_x >> 16;
689 crtc->y = crtc->primary->state->src_y >> 16;
690 }
691}
692
693static void
694crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
695{
696 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200697 int i;
698
699 for (i = 0; i < ncrtcs; i++) {
700 struct drm_crtc_helper_funcs *funcs;
701 struct drm_crtc *crtc;
702
703 crtc = old_state->crtcs[i];
704
705 if (!crtc || !crtc->state->mode_changed)
706 continue;
707
708 funcs = crtc->helper_private;
709
710 if (crtc->state->enable)
711 funcs->mode_set_nofb(crtc);
712 }
713
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100714 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200715 struct drm_connector *connector;
716 struct drm_crtc_state *new_crtc_state;
717 struct drm_encoder_helper_funcs *funcs;
718 struct drm_encoder *encoder;
719 struct drm_display_mode *mode, *adjusted_mode;
720
721 connector = old_state->connectors[i];
722
723 if (!connector || !connector->state->best_encoder)
724 continue;
725
726 encoder = connector->state->best_encoder;
727 funcs = encoder->helper_private;
728 new_crtc_state = connector->state->crtc->state;
729 mode = &new_crtc_state->mode;
730 adjusted_mode = &new_crtc_state->adjusted_mode;
731
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100732 if (!new_crtc_state->mode_changed)
733 continue;
734
Daniel Vetter623369e2014-09-16 17:50:47 +0200735 /*
736 * Each encoder has at most one connector (since we always steal
737 * it away), so we won't call call mode_set hooks twice.
738 */
739 funcs->mode_set(encoder, mode, adjusted_mode);
740
741 if (encoder->bridge && encoder->bridge->funcs->mode_set)
742 encoder->bridge->funcs->mode_set(encoder->bridge,
743 mode, adjusted_mode);
744 }
745}
746
747/**
748 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
749 * @dev: DRM device
750 * @state: atomic state
751 *
752 * This function commits the modeset changes that need to be committed before
753 * updating planes. It shuts down all the outputs that need to be shut down and
754 * prepares them (if required) with the new mode.
755 */
756void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
757 struct drm_atomic_state *state)
758{
759 disable_outputs(dev, state);
760 set_routing_links(dev, state);
761 crtc_set_mode(dev, state);
762}
763EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
764
765/**
766 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
767 * @dev: DRM device
768 * @old_state: atomic state object with old state structures
769 *
770 * This function commits the modeset changes that need to be committed after
771 * updating planes: It enables all the outputs with the new configuration which
772 * had to be turned off for the update.
773 */
774void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
775 struct drm_atomic_state *old_state)
776{
777 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200778 int i;
779
780 for (i = 0; i < ncrtcs; i++) {
781 struct drm_crtc_helper_funcs *funcs;
782 struct drm_crtc *crtc;
783
784 crtc = old_state->crtcs[i];
785
786 /* Need to filter out CRTCs where only planes change. */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100787 if (!crtc || !needs_modeset(crtc->state))
788 continue;
789
790 if (!crtc->state->active)
Daniel Vetter623369e2014-09-16 17:50:47 +0200791 continue;
792
793 funcs = crtc->helper_private;
794
795 if (crtc->state->enable)
796 funcs->commit(crtc);
797 }
798
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100799 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200800 struct drm_connector *connector;
801 struct drm_encoder_helper_funcs *funcs;
802 struct drm_encoder *encoder;
803
804 connector = old_state->connectors[i];
805
806 if (!connector || !connector->state->best_encoder)
807 continue;
808
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100809 if (!connector->state->crtc->state->active)
810 continue;
811
Daniel Vetter623369e2014-09-16 17:50:47 +0200812 encoder = connector->state->best_encoder;
813 funcs = encoder->helper_private;
814
815 /*
816 * Each encoder has at most one connector (since we always steal
817 * it away), so we won't call call enable hooks twice.
818 */
819 if (encoder->bridge)
820 encoder->bridge->funcs->pre_enable(encoder->bridge);
821
822 funcs->commit(encoder);
823
824 if (encoder->bridge)
825 encoder->bridge->funcs->enable(encoder->bridge);
826 }
827}
828EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
829
Daniel Vettere2330f02014-10-29 11:34:56 +0100830static void wait_for_fences(struct drm_device *dev,
831 struct drm_atomic_state *state)
832{
833 int nplanes = dev->mode_config.num_total_plane;
834 int i;
835
836 for (i = 0; i < nplanes; i++) {
837 struct drm_plane *plane = state->planes[i];
838
839 if (!plane || !plane->state->fence)
840 continue;
841
842 WARN_ON(!plane->state->fb);
843
844 fence_wait(plane->state->fence, false);
845 fence_put(plane->state->fence);
846 plane->state->fence = NULL;
847 }
848}
849
Daniel Vetterab58e332014-11-24 20:42:42 +0100850static bool framebuffer_changed(struct drm_device *dev,
851 struct drm_atomic_state *old_state,
852 struct drm_crtc *crtc)
853{
854 struct drm_plane *plane;
855 struct drm_plane_state *old_plane_state;
856 int nplanes = old_state->dev->mode_config.num_total_plane;
857 int i;
858
859 for (i = 0; i < nplanes; i++) {
860 plane = old_state->planes[i];
861 old_plane_state = old_state->plane_states[i];
862
863 if (!plane)
864 continue;
865
866 if (plane->state->crtc != crtc &&
867 old_plane_state->crtc != crtc)
868 continue;
869
870 if (plane->state->fb != old_plane_state->fb)
871 return true;
872 }
873
874 return false;
875}
876
Rob Clark5ee32292014-11-11 19:38:59 -0500877/**
878 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
879 * @dev: DRM device
880 * @old_state: atomic state object with old state structures
881 *
882 * Helper to, after atomic commit, wait for vblanks on all effected
883 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100884 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
885 * framebuffers have actually changed to optimize for the legacy cursor and
886 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500887 */
888void
889drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
890 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200891{
892 struct drm_crtc *crtc;
893 struct drm_crtc_state *old_crtc_state;
894 int ncrtcs = old_state->dev->mode_config.num_crtc;
895 int i, ret;
896
897 for (i = 0; i < ncrtcs; i++) {
898 crtc = old_state->crtcs[i];
899 old_crtc_state = old_state->crtc_states[i];
900
901 if (!crtc)
902 continue;
903
904 /* No one cares about the old state, so abuse it for tracking
905 * and store whether we hold a vblank reference (and should do a
906 * vblank wait) in the ->enable boolean. */
907 old_crtc_state->enable = false;
908
909 if (!crtc->state->enable)
910 continue;
911
Daniel Vetterf02ad902015-01-22 16:36:23 +0100912 /* Legacy cursor ioctls are completely unsynced, and userspace
913 * relies on that (by doing tons of cursor updates). */
914 if (old_state->legacy_cursor_update)
915 continue;
916
Daniel Vetterab58e332014-11-24 20:42:42 +0100917 if (!framebuffer_changed(dev, old_state, crtc))
918 continue;
919
Daniel Vetter623369e2014-09-16 17:50:47 +0200920 ret = drm_crtc_vblank_get(crtc);
921 if (ret != 0)
922 continue;
923
924 old_crtc_state->enable = true;
925 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
926 }
927
928 for (i = 0; i < ncrtcs; i++) {
929 crtc = old_state->crtcs[i];
930 old_crtc_state = old_state->crtc_states[i];
931
932 if (!crtc || !old_crtc_state->enable)
933 continue;
934
935 ret = wait_event_timeout(dev->vblank[i].queue,
936 old_crtc_state->last_vblank_count !=
937 drm_vblank_count(dev, i),
938 msecs_to_jiffies(50));
939
940 drm_crtc_vblank_put(crtc);
941 }
942}
Rob Clark5ee32292014-11-11 19:38:59 -0500943EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +0200944
945/**
946 * drm_atomic_helper_commit - commit validated state object
947 * @dev: DRM device
948 * @state: the driver state object
949 * @async: asynchronous commit
950 *
951 * This function commits a with drm_atomic_helper_check() pre-validated state
952 * object. This can still fail when e.g. the framebuffer reservation fails. For
953 * now this doesn't implement asynchronous commits.
954 *
955 * RETURNS
956 * Zero for success or -errno.
957 */
958int drm_atomic_helper_commit(struct drm_device *dev,
959 struct drm_atomic_state *state,
960 bool async)
961{
962 int ret;
963
964 if (async)
965 return -EBUSY;
966
967 ret = drm_atomic_helper_prepare_planes(dev, state);
968 if (ret)
969 return ret;
970
971 /*
972 * This is the point of no return - everything below never fails except
973 * when the hw goes bonghits. Which means we can commit the new state on
974 * the software side now.
975 */
976
977 drm_atomic_helper_swap_state(dev, state);
978
979 /*
980 * Everything below can be run asynchronously without the need to grab
981 * any modeset locks at all under one conditions: It must be guaranteed
982 * that the asynchronous work has either been cancelled (if the driver
983 * supports it, which at least requires that the framebuffers get
984 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
985 * before the new state gets committed on the software side with
986 * drm_atomic_helper_swap_state().
987 *
988 * This scheme allows new atomic state updates to be prepared and
989 * checked in parallel to the asynchronous completion of the previous
990 * update. Which is important since compositors need to figure out the
991 * composition of the next frame right after having submitted the
992 * current layout.
993 */
994
Daniel Vettere2330f02014-10-29 11:34:56 +0100995 wait_for_fences(dev, state);
996
Daniel Vetter623369e2014-09-16 17:50:47 +0200997 drm_atomic_helper_commit_pre_planes(dev, state);
998
999 drm_atomic_helper_commit_planes(dev, state);
1000
1001 drm_atomic_helper_commit_post_planes(dev, state);
1002
Rob Clark5ee32292014-11-11 19:38:59 -05001003 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +02001004
1005 drm_atomic_helper_cleanup_planes(dev, state);
1006
1007 drm_atomic_state_free(state);
1008
1009 return 0;
1010}
1011EXPORT_SYMBOL(drm_atomic_helper_commit);
1012
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001013/**
Daniel Vettere8c833a2014-07-27 18:30:19 +02001014 * DOC: implementing async commit
1015 *
1016 * For now the atomic helpers don't support async commit directly. If there is
1017 * real need it could be added though, using the dma-buf fence infrastructure
1018 * for generic synchronization with outstanding rendering.
1019 *
1020 * For now drivers have to implement async commit themselves, with the following
1021 * sequence being the recommended one:
1022 *
1023 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1024 * which commit needs to call which can fail, so we want to run it first and
1025 * synchronously.
1026 *
1027 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1028 * might be affected the new state update. This can be done by either cancelling
1029 * or flushing the work items, depending upon whether the driver can deal with
1030 * cancelled updates. Note that it is important to ensure that the framebuffer
1031 * cleanup is still done when cancelling.
1032 *
1033 * For sufficient parallelism it is recommended to have a work item per crtc
1034 * (for updates which don't touch global state) and a global one. Then we only
1035 * need to synchronize with the crtc work items for changed crtcs and the global
1036 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1037 *
1038 * 3. The software state is updated synchronously with
1039 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
1040 * locks means concurrent callers never see inconsistent state. And doing this
1041 * while it's guaranteed that no relevant async worker runs means that async
1042 * workers do not need grab any locks. Actually they must not grab locks, for
1043 * otherwise the work flushing will deadlock.
1044 *
1045 * 4. Schedule a work item to do all subsequent steps, using the split-out
1046 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1047 * then cleaning up the framebuffers after the old framebuffer is no longer
1048 * being displayed.
1049 */
1050
1051/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001052 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
1053 * @dev: DRM device
1054 * @state: atomic state object with old state structures
1055 *
1056 * This function prepares plane state, specifically framebuffers, for the new
1057 * configuration. If any failure is encountered this function will call
1058 * ->cleanup_fb on any already successfully prepared framebuffer.
1059 *
1060 * Returns:
1061 * 0 on success, negative error code on failure.
1062 */
1063int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1064 struct drm_atomic_state *state)
1065{
1066 int nplanes = dev->mode_config.num_total_plane;
1067 int ret, i;
1068
1069 for (i = 0; i < nplanes; i++) {
1070 struct drm_plane_helper_funcs *funcs;
1071 struct drm_plane *plane = state->planes[i];
1072 struct drm_framebuffer *fb;
1073
1074 if (!plane)
1075 continue;
1076
1077 funcs = plane->helper_private;
1078
1079 fb = state->plane_states[i]->fb;
1080
1081 if (fb && funcs->prepare_fb) {
1082 ret = funcs->prepare_fb(plane, fb);
1083 if (ret)
1084 goto fail;
1085 }
1086 }
1087
1088 return 0;
1089
1090fail:
1091 for (i--; i >= 0; i--) {
1092 struct drm_plane_helper_funcs *funcs;
1093 struct drm_plane *plane = state->planes[i];
1094 struct drm_framebuffer *fb;
1095
1096 if (!plane)
1097 continue;
1098
1099 funcs = plane->helper_private;
1100
1101 fb = state->plane_states[i]->fb;
1102
1103 if (fb && funcs->cleanup_fb)
1104 funcs->cleanup_fb(plane, fb);
1105
1106 }
1107
1108 return ret;
1109}
1110EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1111
1112/**
1113 * drm_atomic_helper_commit_planes - commit plane state
1114 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001115 * @old_state: atomic state object with old state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001116 *
1117 * This function commits the new plane state using the plane and atomic helper
1118 * functions for planes and crtcs. It assumes that the atomic state has already
1119 * been pushed into the relevant object state pointers, since this step can no
1120 * longer fail.
1121 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001122 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001123 * crtcs need to be updated though.
1124 */
1125void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001126 struct drm_atomic_state *old_state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001127{
1128 int nplanes = dev->mode_config.num_total_plane;
1129 int ncrtcs = dev->mode_config.num_crtc;
1130 int i;
1131
1132 for (i = 0; i < ncrtcs; i++) {
1133 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001134 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001135
1136 if (!crtc)
1137 continue;
1138
1139 funcs = crtc->helper_private;
1140
1141 if (!funcs || !funcs->atomic_begin)
1142 continue;
1143
1144 funcs->atomic_begin(crtc);
1145 }
1146
1147 for (i = 0; i < nplanes; i++) {
1148 struct drm_plane_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001149 struct drm_plane *plane = old_state->planes[i];
Thierry Redingf1c37e12014-11-25 12:09:44 +01001150 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001151
1152 if (!plane)
1153 continue;
1154
1155 funcs = plane->helper_private;
1156
1157 if (!funcs || !funcs->atomic_update)
1158 continue;
1159
Thierry Redingf1c37e12014-11-25 12:09:44 +01001160 old_plane_state = old_state->plane_states[i];
1161
1162 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001163 }
1164
1165 for (i = 0; i < ncrtcs; i++) {
1166 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001167 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001168
1169 if (!crtc)
1170 continue;
1171
1172 funcs = crtc->helper_private;
1173
1174 if (!funcs || !funcs->atomic_flush)
1175 continue;
1176
1177 funcs->atomic_flush(crtc);
1178 }
1179}
1180EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1181
1182/**
1183 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1184 * @dev: DRM device
1185 * @old_state: atomic state object with old state structures
1186 *
1187 * This function cleans up plane state, specifically framebuffers, from the old
1188 * configuration. Hence the old configuration must be perserved in @old_state to
1189 * be able to call this function.
1190 *
1191 * This function must also be called on the new state when the atomic update
1192 * fails at any point after calling drm_atomic_helper_prepare_planes().
1193 */
1194void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1195 struct drm_atomic_state *old_state)
1196{
1197 int nplanes = dev->mode_config.num_total_plane;
1198 int i;
1199
1200 for (i = 0; i < nplanes; i++) {
1201 struct drm_plane_helper_funcs *funcs;
1202 struct drm_plane *plane = old_state->planes[i];
1203 struct drm_framebuffer *old_fb;
1204
1205 if (!plane)
1206 continue;
1207
1208 funcs = plane->helper_private;
1209
1210 old_fb = old_state->plane_states[i]->fb;
1211
1212 if (old_fb && funcs->cleanup_fb)
1213 funcs->cleanup_fb(plane, old_fb);
1214 }
1215}
1216EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1217
1218/**
1219 * drm_atomic_helper_swap_state - store atomic state into current sw state
1220 * @dev: DRM device
1221 * @state: atomic state
1222 *
1223 * This function stores the atomic state into the current state pointers in all
1224 * driver objects. It should be called after all failing steps have been done
1225 * and succeeded, but before the actual hardware state is committed.
1226 *
1227 * For cleanup and error recovery the current state for all changed objects will
1228 * be swaped into @state.
1229 *
1230 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1231 *
1232 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1233 *
1234 * 2. Do any other steps that might fail.
1235 *
1236 * 3. Put the staged state into the current state pointers with this function.
1237 *
1238 * 4. Actually commit the hardware state.
1239 *
1240 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1241 * contains the old state. Also do any other cleanup required with that state.
1242 */
1243void drm_atomic_helper_swap_state(struct drm_device *dev,
1244 struct drm_atomic_state *state)
1245{
1246 int i;
1247
1248 for (i = 0; i < dev->mode_config.num_connector; i++) {
1249 struct drm_connector *connector = state->connectors[i];
1250
1251 if (!connector)
1252 continue;
1253
1254 connector->state->state = state;
1255 swap(state->connector_states[i], connector->state);
1256 connector->state->state = NULL;
1257 }
1258
1259 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1260 struct drm_crtc *crtc = state->crtcs[i];
1261
1262 if (!crtc)
1263 continue;
1264
1265 crtc->state->state = state;
1266 swap(state->crtc_states[i], crtc->state);
1267 crtc->state->state = NULL;
1268 }
1269
1270 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1271 struct drm_plane *plane = state->planes[i];
1272
1273 if (!plane)
1274 continue;
1275
1276 plane->state->state = state;
1277 swap(state->plane_states[i], plane->state);
1278 plane->state->state = NULL;
1279 }
1280}
1281EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001282
1283/**
1284 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1285 * @plane: plane object to update
1286 * @crtc: owning CRTC of owning plane
1287 * @fb: framebuffer to flip onto plane
1288 * @crtc_x: x offset of primary plane on crtc
1289 * @crtc_y: y offset of primary plane on crtc
1290 * @crtc_w: width of primary plane rectangle on crtc
1291 * @crtc_h: height of primary plane rectangle on crtc
1292 * @src_x: x offset of @fb for panning
1293 * @src_y: y offset of @fb for panning
1294 * @src_w: width of source rectangle in @fb
1295 * @src_h: height of source rectangle in @fb
1296 *
1297 * Provides a default plane update handler using the atomic driver interface.
1298 *
1299 * RETURNS:
1300 * Zero on success, error code on failure
1301 */
1302int drm_atomic_helper_update_plane(struct drm_plane *plane,
1303 struct drm_crtc *crtc,
1304 struct drm_framebuffer *fb,
1305 int crtc_x, int crtc_y,
1306 unsigned int crtc_w, unsigned int crtc_h,
1307 uint32_t src_x, uint32_t src_y,
1308 uint32_t src_w, uint32_t src_h)
1309{
1310 struct drm_atomic_state *state;
1311 struct drm_plane_state *plane_state;
1312 int ret = 0;
1313
1314 state = drm_atomic_state_alloc(plane->dev);
1315 if (!state)
1316 return -ENOMEM;
1317
1318 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1319retry:
1320 plane_state = drm_atomic_get_plane_state(state, plane);
1321 if (IS_ERR(plane_state)) {
1322 ret = PTR_ERR(plane_state);
1323 goto fail;
1324 }
1325
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001326 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001327 if (ret != 0)
1328 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001329 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001330 plane_state->crtc_x = crtc_x;
1331 plane_state->crtc_y = crtc_y;
1332 plane_state->crtc_h = crtc_h;
1333 plane_state->crtc_w = crtc_w;
1334 plane_state->src_x = src_x;
1335 plane_state->src_y = src_y;
1336 plane_state->src_h = src_h;
1337 plane_state->src_w = src_w;
1338
1339 ret = drm_atomic_commit(state);
1340 if (ret != 0)
1341 goto fail;
1342
Daniel Vetterf02ad902015-01-22 16:36:23 +01001343 if (plane == crtc->cursor)
1344 state->legacy_cursor_update = true;
1345
Daniel Vetter042652e2014-07-27 13:46:52 +02001346 /* Driver takes ownership of state on successful commit. */
1347 return 0;
1348fail:
1349 if (ret == -EDEADLK)
1350 goto backoff;
1351
1352 drm_atomic_state_free(state);
1353
1354 return ret;
1355backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001356 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001357 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001358
1359 /*
1360 * Someone might have exchanged the framebuffer while we dropped locks
1361 * in the backoff code. We need to fix up the fb refcount tracking the
1362 * core does for us.
1363 */
1364 plane->old_fb = plane->fb;
1365
1366 goto retry;
1367}
1368EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1369
1370/**
1371 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1372 * @plane: plane to disable
1373 *
1374 * Provides a default plane disable handler using the atomic driver interface.
1375 *
1376 * RETURNS:
1377 * Zero on success, error code on failure
1378 */
1379int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1380{
1381 struct drm_atomic_state *state;
1382 struct drm_plane_state *plane_state;
1383 int ret = 0;
1384
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001385 /*
1386 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1387 * acquire context. The real fix will be to wire the acquire ctx through
1388 * everywhere we need it, but meanwhile prevent chaos by just skipping
1389 * this noop. The critical case is the cursor ioctls which a) only grab
1390 * crtc/cursor-plane locks (so we need the crtc to get at the right
1391 * acquire context) and b) can try to disable the plane multiple times.
1392 */
1393 if (!plane->crtc)
1394 return 0;
1395
Daniel Vetter042652e2014-07-27 13:46:52 +02001396 state = drm_atomic_state_alloc(plane->dev);
1397 if (!state)
1398 return -ENOMEM;
1399
1400 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1401retry:
1402 plane_state = drm_atomic_get_plane_state(state, plane);
1403 if (IS_ERR(plane_state)) {
1404 ret = PTR_ERR(plane_state);
1405 goto fail;
1406 }
1407
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001408 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001409 if (ret != 0)
1410 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001411 drm_atomic_set_fb_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001412 plane_state->crtc_x = 0;
1413 plane_state->crtc_y = 0;
1414 plane_state->crtc_h = 0;
1415 plane_state->crtc_w = 0;
1416 plane_state->src_x = 0;
1417 plane_state->src_y = 0;
1418 plane_state->src_h = 0;
1419 plane_state->src_w = 0;
1420
Daniel Vetterf02ad902015-01-22 16:36:23 +01001421 if (plane == plane->crtc->cursor)
1422 state->legacy_cursor_update = true;
1423
Daniel Vetter042652e2014-07-27 13:46:52 +02001424 ret = drm_atomic_commit(state);
1425 if (ret != 0)
1426 goto fail;
1427
1428 /* Driver takes ownership of state on successful commit. */
1429 return 0;
1430fail:
1431 if (ret == -EDEADLK)
1432 goto backoff;
1433
1434 drm_atomic_state_free(state);
1435
1436 return ret;
1437backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001438 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001439 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001440
1441 /*
1442 * Someone might have exchanged the framebuffer while we dropped locks
1443 * in the backoff code. We need to fix up the fb refcount tracking the
1444 * core does for us.
1445 */
1446 plane->old_fb = plane->fb;
1447
1448 goto retry;
1449}
1450EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1451
1452static int update_output_state(struct drm_atomic_state *state,
1453 struct drm_mode_set *set)
1454{
1455 struct drm_device *dev = set->crtc->dev;
1456 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001457 int ncrtcs = state->dev->mode_config.num_crtc;
1458 int ret, i, j;
1459
1460 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1461 state->acquire_ctx);
1462 if (ret)
1463 return ret;
1464
1465 /* First grab all affected connector/crtc states. */
1466 for (i = 0; i < set->num_connectors; i++) {
1467 conn_state = drm_atomic_get_connector_state(state,
1468 set->connectors[i]);
1469 if (IS_ERR(conn_state))
1470 return PTR_ERR(conn_state);
1471 }
1472
1473 for (i = 0; i < ncrtcs; i++) {
1474 struct drm_crtc *crtc = state->crtcs[i];
1475
1476 if (!crtc)
1477 continue;
1478
1479 ret = drm_atomic_add_affected_connectors(state, crtc);
1480 if (ret)
1481 return ret;
1482 }
1483
1484 /* Then recompute connector->crtc links and crtc enabling state. */
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001485 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001486 struct drm_connector *connector;
1487
1488 connector = state->connectors[i];
1489 conn_state = state->connector_states[i];
1490
1491 if (!connector)
1492 continue;
1493
1494 if (conn_state->crtc == set->crtc) {
1495 ret = drm_atomic_set_crtc_for_connector(conn_state,
1496 NULL);
1497 if (ret)
1498 return ret;
1499 }
1500
1501 for (j = 0; j < set->num_connectors; j++) {
1502 if (set->connectors[j] == connector) {
1503 ret = drm_atomic_set_crtc_for_connector(conn_state,
1504 set->crtc);
1505 if (ret)
1506 return ret;
1507 break;
1508 }
1509 }
1510 }
1511
1512 for (i = 0; i < ncrtcs; i++) {
1513 struct drm_crtc *crtc = state->crtcs[i];
1514 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1515
1516 if (!crtc)
1517 continue;
1518
1519 /* Don't update ->enable for the CRTC in the set_config request,
1520 * since a mismatch would indicate a bug in the upper layers.
1521 * The actual modeset code later on will catch any
1522 * inconsistencies here. */
1523 if (crtc == set->crtc)
1524 continue;
1525
1526 crtc_state->enable =
1527 drm_atomic_connectors_for_crtc(state, crtc);
1528 }
1529
1530 return 0;
1531}
1532
1533/**
1534 * drm_atomic_helper_set_config - set a new config from userspace
1535 * @set: mode set configuration
1536 *
1537 * Provides a default crtc set_config handler using the atomic driver interface.
1538 *
1539 * Returns:
1540 * Returns 0 on success, negative errno numbers on failure.
1541 */
1542int drm_atomic_helper_set_config(struct drm_mode_set *set)
1543{
1544 struct drm_atomic_state *state;
1545 struct drm_crtc *crtc = set->crtc;
1546 struct drm_crtc_state *crtc_state;
1547 struct drm_plane_state *primary_state;
1548 int ret = 0;
1549
1550 state = drm_atomic_state_alloc(crtc->dev);
1551 if (!state)
1552 return -ENOMEM;
1553
1554 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1555retry:
1556 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1557 if (IS_ERR(crtc_state)) {
1558 ret = PTR_ERR(crtc_state);
1559 goto fail;
1560 }
1561
Rob Clarke5b53412014-11-26 18:58:04 -05001562 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1563 if (IS_ERR(primary_state)) {
1564 ret = PTR_ERR(primary_state);
1565 goto fail;
1566 }
1567
Daniel Vetter042652e2014-07-27 13:46:52 +02001568 if (!set->mode) {
1569 WARN_ON(set->fb);
1570 WARN_ON(set->num_connectors);
1571
1572 crtc_state->enable = false;
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001573 crtc_state->active = false;
Rob Clarke5b53412014-11-26 18:58:04 -05001574
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001575 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
Rob Clarke5b53412014-11-26 18:58:04 -05001576 if (ret != 0)
1577 goto fail;
1578
1579 drm_atomic_set_fb_for_plane(primary_state, NULL);
1580
Daniel Vetter042652e2014-07-27 13:46:52 +02001581 goto commit;
1582 }
1583
1584 WARN_ON(!set->fb);
1585 WARN_ON(!set->num_connectors);
1586
1587 crtc_state->enable = true;
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001588 crtc_state->active = true;
Daniel Vetter042652e2014-07-27 13:46:52 +02001589 drm_mode_copy(&crtc_state->mode, set->mode);
1590
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001591 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001592 if (ret != 0)
1593 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001594 drm_atomic_set_fb_for_plane(primary_state, set->fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001595 primary_state->crtc_x = 0;
1596 primary_state->crtc_y = 0;
1597 primary_state->crtc_h = set->mode->vdisplay;
1598 primary_state->crtc_w = set->mode->hdisplay;
1599 primary_state->src_x = set->x << 16;
1600 primary_state->src_y = set->y << 16;
1601 primary_state->src_h = set->mode->vdisplay << 16;
1602 primary_state->src_w = set->mode->hdisplay << 16;
1603
1604commit:
1605 ret = update_output_state(state, set);
1606 if (ret)
1607 goto fail;
1608
1609 ret = drm_atomic_commit(state);
1610 if (ret != 0)
1611 goto fail;
1612
1613 /* Driver takes ownership of state on successful commit. */
1614 return 0;
1615fail:
1616 if (ret == -EDEADLK)
1617 goto backoff;
1618
1619 drm_atomic_state_free(state);
1620
1621 return ret;
1622backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001623 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001624 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001625
1626 /*
1627 * Someone might have exchanged the framebuffer while we dropped locks
1628 * in the backoff code. We need to fix up the fb refcount tracking the
1629 * core does for us.
1630 */
1631 crtc->primary->old_fb = crtc->primary->fb;
1632
1633 goto retry;
1634}
1635EXPORT_SYMBOL(drm_atomic_helper_set_config);
1636
1637/**
1638 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1639 * @crtc: DRM crtc
1640 * @property: DRM property
1641 * @val: value of property
1642 *
1643 * Provides a default plane disablle handler using the atomic driver interface.
1644 *
1645 * RETURNS:
1646 * Zero on success, error code on failure
1647 */
1648int
1649drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1650 struct drm_property *property,
1651 uint64_t val)
1652{
1653 struct drm_atomic_state *state;
1654 struct drm_crtc_state *crtc_state;
1655 int ret = 0;
1656
1657 state = drm_atomic_state_alloc(crtc->dev);
1658 if (!state)
1659 return -ENOMEM;
1660
1661 /* ->set_property is always called with all locks held. */
1662 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1663retry:
1664 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1665 if (IS_ERR(crtc_state)) {
1666 ret = PTR_ERR(crtc_state);
1667 goto fail;
1668 }
1669
Rob Clark40ecc692014-12-18 16:01:46 -05001670 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1671 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001672 if (ret)
1673 goto fail;
1674
1675 ret = drm_atomic_commit(state);
1676 if (ret != 0)
1677 goto fail;
1678
1679 /* Driver takes ownership of state on successful commit. */
1680 return 0;
1681fail:
1682 if (ret == -EDEADLK)
1683 goto backoff;
1684
1685 drm_atomic_state_free(state);
1686
1687 return ret;
1688backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001689 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001690 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001691
1692 goto retry;
1693}
1694EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1695
1696/**
1697 * drm_atomic_helper_plane_set_property - helper for plane prorties
1698 * @plane: DRM plane
1699 * @property: DRM property
1700 * @val: value of property
1701 *
1702 * Provides a default plane disable handler using the atomic driver interface.
1703 *
1704 * RETURNS:
1705 * Zero on success, error code on failure
1706 */
1707int
1708drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1709 struct drm_property *property,
1710 uint64_t val)
1711{
1712 struct drm_atomic_state *state;
1713 struct drm_plane_state *plane_state;
1714 int ret = 0;
1715
1716 state = drm_atomic_state_alloc(plane->dev);
1717 if (!state)
1718 return -ENOMEM;
1719
1720 /* ->set_property is always called with all locks held. */
1721 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1722retry:
1723 plane_state = drm_atomic_get_plane_state(state, plane);
1724 if (IS_ERR(plane_state)) {
1725 ret = PTR_ERR(plane_state);
1726 goto fail;
1727 }
1728
Rob Clark40ecc692014-12-18 16:01:46 -05001729 ret = drm_atomic_plane_set_property(plane, plane_state,
1730 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001731 if (ret)
1732 goto fail;
1733
1734 ret = drm_atomic_commit(state);
1735 if (ret != 0)
1736 goto fail;
1737
1738 /* Driver takes ownership of state on successful commit. */
1739 return 0;
1740fail:
1741 if (ret == -EDEADLK)
1742 goto backoff;
1743
1744 drm_atomic_state_free(state);
1745
1746 return ret;
1747backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001748 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001749 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001750
1751 goto retry;
1752}
1753EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1754
1755/**
1756 * drm_atomic_helper_connector_set_property - helper for connector prorties
1757 * @connector: DRM connector
1758 * @property: DRM property
1759 * @val: value of property
1760 *
1761 * Provides a default plane disablle handler using the atomic driver interface.
1762 *
1763 * RETURNS:
1764 * Zero on success, error code on failure
1765 */
1766int
1767drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1768 struct drm_property *property,
1769 uint64_t val)
1770{
1771 struct drm_atomic_state *state;
1772 struct drm_connector_state *connector_state;
1773 int ret = 0;
1774
1775 state = drm_atomic_state_alloc(connector->dev);
1776 if (!state)
1777 return -ENOMEM;
1778
1779 /* ->set_property is always called with all locks held. */
1780 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1781retry:
1782 connector_state = drm_atomic_get_connector_state(state, connector);
1783 if (IS_ERR(connector_state)) {
1784 ret = PTR_ERR(connector_state);
1785 goto fail;
1786 }
1787
Rob Clark40ecc692014-12-18 16:01:46 -05001788 ret = drm_atomic_connector_set_property(connector, connector_state,
1789 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001790 if (ret)
1791 goto fail;
1792
1793 ret = drm_atomic_commit(state);
1794 if (ret != 0)
1795 goto fail;
1796
1797 /* Driver takes ownership of state on successful commit. */
1798 return 0;
1799fail:
1800 if (ret == -EDEADLK)
1801 goto backoff;
1802
1803 drm_atomic_state_free(state);
1804
1805 return ret;
1806backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001807 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001808 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001809
1810 goto retry;
1811}
1812EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001813
1814/**
1815 * drm_atomic_helper_page_flip - execute a legacy page flip
1816 * @crtc: DRM crtc
1817 * @fb: DRM framebuffer
1818 * @event: optional DRM event to signal upon completion
1819 * @flags: flip flags for non-vblank sync'ed updates
1820 *
1821 * Provides a default page flip implementation using the atomic driver interface.
1822 *
1823 * Note that for now so called async page flips (i.e. updates which are not
1824 * synchronized to vblank) are not supported, since the atomic interfaces have
1825 * no provisions for this yet.
1826 *
1827 * Returns:
1828 * Returns 0 on success, negative errno numbers on failure.
1829 */
1830int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1831 struct drm_framebuffer *fb,
1832 struct drm_pending_vblank_event *event,
1833 uint32_t flags)
1834{
1835 struct drm_plane *plane = crtc->primary;
1836 struct drm_atomic_state *state;
1837 struct drm_plane_state *plane_state;
1838 struct drm_crtc_state *crtc_state;
1839 int ret = 0;
1840
1841 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1842 return -EINVAL;
1843
1844 state = drm_atomic_state_alloc(plane->dev);
1845 if (!state)
1846 return -ENOMEM;
1847
1848 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1849retry:
1850 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1851 if (IS_ERR(crtc_state)) {
1852 ret = PTR_ERR(crtc_state);
1853 goto fail;
1854 }
1855 crtc_state->event = event;
1856
1857 plane_state = drm_atomic_get_plane_state(state, plane);
1858 if (IS_ERR(plane_state)) {
1859 ret = PTR_ERR(plane_state);
1860 goto fail;
1861 }
1862
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001863 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001864 if (ret != 0)
1865 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001866 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001867
1868 ret = drm_atomic_async_commit(state);
1869 if (ret != 0)
1870 goto fail;
1871
1872 /* TODO: ->page_flip is the only driver callback where the core
1873 * doesn't update plane->fb. For now patch it up here. */
1874 plane->fb = plane->state->fb;
1875
1876 /* Driver takes ownership of state on successful async commit. */
1877 return 0;
1878fail:
1879 if (ret == -EDEADLK)
1880 goto backoff;
1881
1882 drm_atomic_state_free(state);
1883
1884 return ret;
1885backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001886 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001887 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001888
1889 /*
1890 * Someone might have exchanged the framebuffer while we dropped locks
1891 * in the backoff code. We need to fix up the fb refcount tracking the
1892 * core does for us.
1893 */
1894 plane->old_fb = plane->fb;
1895
1896 goto retry;
1897}
1898EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01001899
1900/**
Daniel Vetterb486e0e2015-01-22 18:53:05 +01001901 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
1902 * @connector: affected connector
1903 * @mode: DPMS mode
1904 *
1905 * This is the main helper function provided by the atomic helper framework for
1906 * implementing the legacy DPMS connector interface. It computes the new desired
1907 * ->active state for the corresponding CRTC (if the connector is enabled) and
1908 * updates it.
1909 */
1910void drm_atomic_helper_connector_dpms(struct drm_connector *connector,
1911 int mode)
1912{
1913 struct drm_mode_config *config = &connector->dev->mode_config;
1914 struct drm_atomic_state *state;
1915 struct drm_crtc_state *crtc_state;
1916 struct drm_crtc *crtc;
1917 struct drm_connector *tmp_connector;
1918 int ret;
1919 bool active = false;
1920
1921 if (mode != DRM_MODE_DPMS_ON)
1922 mode = DRM_MODE_DPMS_OFF;
1923
1924 connector->dpms = mode;
1925 crtc = connector->state->crtc;
1926
1927 if (!crtc)
1928 return;
1929
1930 /* FIXME: ->dpms has no return value so can't forward the -ENOMEM. */
1931 state = drm_atomic_state_alloc(connector->dev);
1932 if (!state)
1933 return;
1934
1935 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1936retry:
1937 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1938 if (IS_ERR(crtc_state))
1939 return;
1940
1941 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
1942
1943 list_for_each_entry(tmp_connector, &config->connector_list, head) {
1944 if (connector->state->crtc != crtc)
1945 continue;
1946
1947 if (connector->dpms == DRM_MODE_DPMS_ON) {
1948 active = true;
1949 break;
1950 }
1951 }
1952 crtc_state->active = active;
1953
1954 ret = drm_atomic_commit(state);
1955 if (ret != 0)
1956 goto fail;
1957
1958 /* Driver takes ownership of state on successful async commit. */
1959 return;
1960fail:
1961 if (ret == -EDEADLK)
1962 goto backoff;
1963
1964 drm_atomic_state_free(state);
1965
1966 WARN(1, "Driver bug: Changing ->active failed with ret=%i\n", ret);
1967
1968 return;
1969backoff:
1970 drm_atomic_state_clear(state);
1971 drm_atomic_legacy_backoff(state);
1972
1973 goto retry;
1974}
1975EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
1976
1977/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01001978 * DOC: atomic state reset and initialization
1979 *
1980 * Both the drm core and the atomic helpers assume that there is always the full
1981 * and correct atomic software state for all connectors, CRTCs and planes
1982 * available. Which is a bit a problem on driver load and also after system
1983 * suspend. One way to solve this is to have a hardware state read-out
1984 * infrastructure which reconstructs the full software state (e.g. the i915
1985 * driver).
1986 *
1987 * The simpler solution is to just reset the software state to everything off,
1988 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
1989 * the atomic helpers provide default reset implementations for all hooks.
1990 */
1991
1992/**
Daniel Vetterd4617012014-11-03 15:56:43 +01001993 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
1994 * @crtc: drm CRTC
1995 *
1996 * Resets the atomic state for @crtc by freeing the state pointer (which might
1997 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1998 */
1999void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2000{
2001 kfree(crtc->state);
2002 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002003
2004 if (crtc->state)
2005 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01002006}
2007EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2008
2009/**
2010 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2011 * @crtc: drm CRTC
2012 *
2013 * Default CRTC state duplicate hook for drivers which don't have their own
2014 * subclassed CRTC state structure.
2015 */
2016struct drm_crtc_state *
2017drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2018{
2019 struct drm_crtc_state *state;
2020
2021 if (WARN_ON(!crtc->state))
2022 return NULL;
2023
2024 state = kmemdup(crtc->state, sizeof(*crtc->state), GFP_KERNEL);
2025
2026 if (state) {
2027 state->mode_changed = false;
Daniel Vettereab3bbe2015-01-22 16:36:21 +01002028 state->active_changed = false;
Daniel Vetterd4617012014-11-03 15:56:43 +01002029 state->planes_changed = false;
2030 state->event = NULL;
2031 }
2032
2033 return state;
2034}
2035EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2036
2037/**
2038 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2039 * @crtc: drm CRTC
2040 * @state: CRTC state object to release
2041 *
2042 * Default CRTC state destroy hook for drivers which don't have their own
2043 * subclassed CRTC state structure.
2044 */
2045void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2046 struct drm_crtc_state *state)
2047{
2048 kfree(state);
2049}
2050EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2051
2052/**
2053 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2054 * @plane: drm plane
2055 *
2056 * Resets the atomic state for @plane by freeing the state pointer (which might
2057 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2058 */
2059void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2060{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002061 if (plane->state && plane->state->fb)
2062 drm_framebuffer_unreference(plane->state->fb);
2063
Daniel Vetterd4617012014-11-03 15:56:43 +01002064 kfree(plane->state);
2065 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002066
2067 if (plane->state)
2068 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01002069}
2070EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2071
2072/**
2073 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2074 * @plane: drm plane
2075 *
2076 * Default plane state duplicate hook for drivers which don't have their own
2077 * subclassed plane state structure.
2078 */
2079struct drm_plane_state *
2080drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2081{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002082 struct drm_plane_state *state;
2083
Daniel Vetterd4617012014-11-03 15:56:43 +01002084 if (WARN_ON(!plane->state))
2085 return NULL;
2086
Daniel Vetter321ebf02014-11-04 22:57:27 +01002087 state = kmemdup(plane->state, sizeof(*plane->state), GFP_KERNEL);
2088
2089 if (state && state->fb)
2090 drm_framebuffer_reference(state->fb);
2091
2092 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01002093}
2094EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2095
2096/**
2097 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2098 * @plane: drm plane
2099 * @state: plane state object to release
2100 *
2101 * Default plane state destroy hook for drivers which don't have their own
2102 * subclassed plane state structure.
2103 */
2104void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01002105 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01002106{
Daniel Vetter321ebf02014-11-04 22:57:27 +01002107 if (state->fb)
2108 drm_framebuffer_unreference(state->fb);
2109
Daniel Vetterd4617012014-11-03 15:56:43 +01002110 kfree(state);
2111}
2112EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2113
2114/**
2115 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2116 * @connector: drm connector
2117 *
2118 * Resets the atomic state for @connector by freeing the state pointer (which
2119 * might be NULL, e.g. at driver load time) and allocating a new empty state
2120 * object.
2121 */
2122void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2123{
2124 kfree(connector->state);
2125 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002126
2127 if (connector->state)
2128 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002129}
2130EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2131
2132/**
2133 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2134 * @connector: drm connector
2135 *
2136 * Default connector state duplicate hook for drivers which don't have their own
2137 * subclassed connector state structure.
2138 */
2139struct drm_connector_state *
2140drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2141{
2142 if (WARN_ON(!connector->state))
2143 return NULL;
2144
2145 return kmemdup(connector->state, sizeof(*connector->state), GFP_KERNEL);
2146}
2147EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2148
2149/**
2150 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2151 * @connector: drm connector
2152 * @state: connector state object to release
2153 *
2154 * Default connector state destroy hook for drivers which don't have their own
2155 * subclassed connector state structure.
2156 */
2157void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2158 struct drm_connector_state *state)
2159{
2160 kfree(state);
2161}
2162EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);