blob: 24c44c24dabe6de55830838109dc65b534411b14 [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
Thierry Reding4cd4df82014-12-03 16:44:34 +0100300 if (funcs->atomic_check) {
301 ret = funcs->atomic_check(encoder, crtc_state,
302 conn_state);
303 if (ret) {
304 DRM_DEBUG_KMS("[ENCODER:%d:%s] check failed\n",
305 encoder->base.id, encoder->name);
306 return ret;
307 }
308 } else {
309 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
310 &crtc_state->adjusted_mode);
311 if (!ret) {
312 DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
313 encoder->base.id, encoder->name);
314 return -EINVAL;
315 }
Daniel Vetter623369e2014-09-16 17:50:47 +0200316 }
317 }
318
319 for (i = 0; i < ncrtcs; i++) {
320 struct drm_crtc_helper_funcs *funcs;
321 struct drm_crtc *crtc;
322
323 crtc_state = state->crtc_states[i];
324 crtc = state->crtcs[i];
325
326 if (!crtc_state || !crtc_state->mode_changed)
327 continue;
328
329 funcs = crtc->helper_private;
330 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
331 &crtc_state->adjusted_mode);
332 if (!ret) {
333 DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
334 crtc->base.id);
335 return -EINVAL;
336 }
337 }
338
339 return 0;
340}
341
Daniel Vetterd9b13622014-11-26 16:57:41 +0100342/**
343 * drm_atomic_helper_check - validate state object for modeset changes
344 * @dev: DRM device
345 * @state: the driver state object
346 *
347 * Check the state object to see if the requested state is physically possible.
348 * This does all the crtc and connector related computations for an atomic
349 * update. It computes and updates crtc_state->mode_changed, adds any additional
350 * connectors needed for full modesets and calls down into ->mode_fixup
351 * functions of the driver backend.
352 *
353 * IMPORTANT:
354 *
355 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
356 * plane update can't be done without a full modeset) _must_ call this function
357 * afterwards after that change. It is permitted to call this function multiple
358 * times for the same update, e.g. when the ->atomic_check functions depend upon
359 * the adjusted dotclock for fifo space allocation and watermark computation.
360 *
361 * RETURNS
362 * Zero for success or -errno
363 */
364int
Rob Clark934ce1c2014-11-19 16:41:33 -0500365drm_atomic_helper_check_modeset(struct drm_device *dev,
Daniel Vetter623369e2014-09-16 17:50:47 +0200366 struct drm_atomic_state *state)
367{
368 int ncrtcs = dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200369 struct drm_crtc *crtc;
370 struct drm_crtc_state *crtc_state;
371 int i, ret;
372
373 for (i = 0; i < ncrtcs; i++) {
374 crtc = state->crtcs[i];
375 crtc_state = state->crtc_states[i];
376
377 if (!crtc)
378 continue;
379
380 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
381 DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
382 crtc->base.id);
383 crtc_state->mode_changed = true;
384 }
385
386 if (crtc->state->enable != crtc_state->enable) {
387 DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
388 crtc->base.id);
389 crtc_state->mode_changed = true;
390 }
391 }
392
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100393 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200394 /*
395 * This only sets crtc->mode_changed for routing changes,
396 * drivers must set crtc->mode_changed themselves when connector
397 * properties need to be updated.
398 */
399 ret = update_connector_routing(state, i);
400 if (ret)
401 return ret;
402 }
403
404 /*
405 * After all the routing has been prepared we need to add in any
406 * connector which is itself unchanged, but who's crtc changes it's
407 * configuration. This must be done before calling mode_fixup in case a
408 * crtc only changed its mode but has the same set of connectors.
409 */
410 for (i = 0; i < ncrtcs; i++) {
411 int num_connectors;
412
413 crtc = state->crtcs[i];
414 crtc_state = state->crtc_states[i];
415
416 if (!crtc || !crtc_state->mode_changed)
417 continue;
418
419 DRM_DEBUG_KMS("[CRTC:%d] needs full modeset, enable: %c\n",
420 crtc->base.id,
421 crtc_state->enable ? 'y' : 'n');
422
423 ret = drm_atomic_add_affected_connectors(state, crtc);
424 if (ret != 0)
425 return ret;
426
427 num_connectors = drm_atomic_connectors_for_crtc(state,
428 crtc);
429
430 if (crtc_state->enable != !!num_connectors) {
431 DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
432 crtc->base.id);
433
434 return -EINVAL;
435 }
436 }
437
438 return mode_fixup(state);
439}
Daniel Vetterd9b13622014-11-26 16:57:41 +0100440EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
Daniel Vetter623369e2014-09-16 17:50:47 +0200441
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100442/**
Daniel Vetterd9b13622014-11-26 16:57:41 +0100443 * drm_atomic_helper_check - validate state object for modeset changes
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100444 * @dev: DRM device
445 * @state: the driver state object
446 *
447 * Check the state object to see if the requested state is physically possible.
Daniel Vetterd9b13622014-11-26 16:57:41 +0100448 * This does all the plane update related checks using by calling into the
449 * ->atomic_check hooks provided by the driver.
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100450 *
451 * RETURNS
452 * Zero for success or -errno
453 */
Daniel Vetterd9b13622014-11-26 16:57:41 +0100454int
455drm_atomic_helper_check_planes(struct drm_device *dev,
456 struct drm_atomic_state *state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100457{
458 int nplanes = dev->mode_config.num_total_plane;
459 int ncrtcs = dev->mode_config.num_crtc;
460 int i, ret = 0;
461
462 for (i = 0; i < nplanes; i++) {
463 struct drm_plane_helper_funcs *funcs;
464 struct drm_plane *plane = state->planes[i];
465 struct drm_plane_state *plane_state = state->plane_states[i];
466
467 if (!plane)
468 continue;
469
470 funcs = plane->helper_private;
471
472 drm_atomic_helper_plane_changed(state, plane_state, plane);
473
474 if (!funcs || !funcs->atomic_check)
475 continue;
476
477 ret = funcs->atomic_check(plane, plane_state);
478 if (ret) {
Rob Clark5e743732014-12-18 16:01:51 -0500479 DRM_DEBUG_KMS("[PLANE:%d] atomic driver check failed\n",
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100480 plane->base.id);
481 return ret;
482 }
483 }
484
485 for (i = 0; i < ncrtcs; i++) {
486 struct drm_crtc_helper_funcs *funcs;
487 struct drm_crtc *crtc = state->crtcs[i];
488
489 if (!crtc)
490 continue;
491
492 funcs = crtc->helper_private;
493
494 if (!funcs || !funcs->atomic_check)
495 continue;
496
497 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
498 if (ret) {
Rob Clark5e743732014-12-18 16:01:51 -0500499 DRM_DEBUG_KMS("[CRTC:%d] atomic driver check failed\n",
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100500 crtc->base.id);
501 return ret;
502 }
503 }
504
Daniel Vetterd9b13622014-11-26 16:57:41 +0100505 return ret;
506}
507EXPORT_SYMBOL(drm_atomic_helper_check_planes);
508
509/**
510 * drm_atomic_helper_check - validate state object
511 * @dev: DRM device
512 * @state: the driver state object
513 *
514 * Check the state object to see if the requested state is physically possible.
515 * Only crtcs and planes have check callbacks, so for any additional (global)
516 * checking that a driver needs it can simply wrap that around this function.
517 * Drivers without such needs can directly use this as their ->atomic_check()
518 * callback.
519 *
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100520 * This just wraps the two parts of the state checking for planes and modeset
521 * state in the default order: First it calls drm_atomic_helper_check_modeset()
522 * and then drm_atomic_helper_check_planes(). The assumption is that the
523 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
524 * e.g. properly compute watermarks.
525 *
Daniel Vetterd9b13622014-11-26 16:57:41 +0100526 * RETURNS
527 * Zero for success or -errno
528 */
529int drm_atomic_helper_check(struct drm_device *dev,
530 struct drm_atomic_state *state)
531{
532 int ret;
533
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100534 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vetterd9b13622014-11-26 16:57:41 +0100535 if (ret)
536 return ret;
537
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100538 ret = drm_atomic_helper_check_planes(dev, state);
Rob Clark934ce1c2014-11-19 16:41:33 -0500539 if (ret)
540 return ret;
541
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100542 return ret;
543}
544EXPORT_SYMBOL(drm_atomic_helper_check);
545
Daniel Vetter623369e2014-09-16 17:50:47 +0200546static void
547disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
548{
549 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200550 int i;
551
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100552 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200553 struct drm_connector_state *old_conn_state;
554 struct drm_connector *connector;
555 struct drm_encoder_helper_funcs *funcs;
556 struct drm_encoder *encoder;
557
558 old_conn_state = old_state->connector_states[i];
559 connector = old_state->connectors[i];
560
561 /* Shut down everything that's in the changeset and currently
562 * still on. So need to check the old, saved state. */
563 if (!old_conn_state || !old_conn_state->crtc)
564 continue;
565
Rob Clark46df9ad2014-11-20 15:40:36 -0500566 encoder = old_conn_state->best_encoder;
Daniel Vetter623369e2014-09-16 17:50:47 +0200567
Rob Clark46df9ad2014-11-20 15:40:36 -0500568 /* We shouldn't get this far if we didn't previously have
569 * an encoder.. but WARN_ON() rather than explode.
570 */
571 if (WARN_ON(!encoder))
Daniel Vetter623369e2014-09-16 17:50:47 +0200572 continue;
573
574 funcs = encoder->helper_private;
575
576 /*
577 * Each encoder has at most one connector (since we always steal
578 * it away), so we won't call call disable hooks twice.
579 */
580 if (encoder->bridge)
581 encoder->bridge->funcs->disable(encoder->bridge);
582
583 /* Right function depends upon target state. */
584 if (connector->state->crtc)
585 funcs->prepare(encoder);
586 else if (funcs->disable)
587 funcs->disable(encoder);
588 else
589 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
590
591 if (encoder->bridge)
592 encoder->bridge->funcs->post_disable(encoder->bridge);
593 }
594
595 for (i = 0; i < ncrtcs; i++) {
596 struct drm_crtc_helper_funcs *funcs;
597 struct drm_crtc *crtc;
598
599 crtc = old_state->crtcs[i];
600
601 /* Shut down everything that needs a full modeset. */
602 if (!crtc || !crtc->state->mode_changed)
603 continue;
604
605 funcs = crtc->helper_private;
606
607 /* Right function depends upon target state. */
608 if (crtc->state->enable)
609 funcs->prepare(crtc);
610 else if (funcs->disable)
611 funcs->disable(crtc);
612 else
613 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
614 }
615}
616
617static void
618set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
619{
Daniel Vetter623369e2014-09-16 17:50:47 +0200620 int ncrtcs = old_state->dev->mode_config.num_crtc;
621 int i;
622
623 /* clear out existing links */
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100624 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200625 struct drm_connector *connector;
626
627 connector = old_state->connectors[i];
628
629 if (!connector || !connector->encoder)
630 continue;
631
632 WARN_ON(!connector->encoder->crtc);
633
634 connector->encoder->crtc = NULL;
635 connector->encoder = NULL;
636 }
637
638 /* set new links */
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100639 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200640 struct drm_connector *connector;
641
642 connector = old_state->connectors[i];
643
644 if (!connector || !connector->state->crtc)
645 continue;
646
647 if (WARN_ON(!connector->state->best_encoder))
648 continue;
649
650 connector->encoder = connector->state->best_encoder;
651 connector->encoder->crtc = connector->state->crtc;
652 }
653
654 /* set legacy state in the crtc structure */
655 for (i = 0; i < ncrtcs; i++) {
656 struct drm_crtc *crtc;
657
658 crtc = old_state->crtcs[i];
659
660 if (!crtc)
661 continue;
662
663 crtc->mode = crtc->state->mode;
664 crtc->enabled = crtc->state->enable;
665 crtc->x = crtc->primary->state->src_x >> 16;
666 crtc->y = crtc->primary->state->src_y >> 16;
667 }
668}
669
670static void
671crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
672{
673 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200674 int i;
675
676 for (i = 0; i < ncrtcs; i++) {
677 struct drm_crtc_helper_funcs *funcs;
678 struct drm_crtc *crtc;
679
680 crtc = old_state->crtcs[i];
681
682 if (!crtc || !crtc->state->mode_changed)
683 continue;
684
685 funcs = crtc->helper_private;
686
687 if (crtc->state->enable)
688 funcs->mode_set_nofb(crtc);
689 }
690
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100691 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200692 struct drm_connector *connector;
693 struct drm_crtc_state *new_crtc_state;
694 struct drm_encoder_helper_funcs *funcs;
695 struct drm_encoder *encoder;
696 struct drm_display_mode *mode, *adjusted_mode;
697
698 connector = old_state->connectors[i];
699
700 if (!connector || !connector->state->best_encoder)
701 continue;
702
703 encoder = connector->state->best_encoder;
704 funcs = encoder->helper_private;
705 new_crtc_state = connector->state->crtc->state;
706 mode = &new_crtc_state->mode;
707 adjusted_mode = &new_crtc_state->adjusted_mode;
708
709 /*
710 * Each encoder has at most one connector (since we always steal
711 * it away), so we won't call call mode_set hooks twice.
712 */
713 funcs->mode_set(encoder, mode, adjusted_mode);
714
715 if (encoder->bridge && encoder->bridge->funcs->mode_set)
716 encoder->bridge->funcs->mode_set(encoder->bridge,
717 mode, adjusted_mode);
718 }
719}
720
721/**
722 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
723 * @dev: DRM device
724 * @state: atomic state
725 *
726 * This function commits the modeset changes that need to be committed before
727 * updating planes. It shuts down all the outputs that need to be shut down and
728 * prepares them (if required) with the new mode.
729 */
730void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
731 struct drm_atomic_state *state)
732{
733 disable_outputs(dev, state);
734 set_routing_links(dev, state);
735 crtc_set_mode(dev, state);
736}
737EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
738
739/**
740 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
741 * @dev: DRM device
742 * @old_state: atomic state object with old state structures
743 *
744 * This function commits the modeset changes that need to be committed after
745 * updating planes: It enables all the outputs with the new configuration which
746 * had to be turned off for the update.
747 */
748void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
749 struct drm_atomic_state *old_state)
750{
751 int ncrtcs = old_state->dev->mode_config.num_crtc;
Daniel Vetter623369e2014-09-16 17:50:47 +0200752 int i;
753
754 for (i = 0; i < ncrtcs; i++) {
755 struct drm_crtc_helper_funcs *funcs;
756 struct drm_crtc *crtc;
757
758 crtc = old_state->crtcs[i];
759
760 /* Need to filter out CRTCs where only planes change. */
761 if (!crtc || !crtc->state->mode_changed)
762 continue;
763
764 funcs = crtc->helper_private;
765
766 if (crtc->state->enable)
767 funcs->commit(crtc);
768 }
769
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100770 for (i = 0; i < old_state->num_connector; i++) {
Daniel Vetter623369e2014-09-16 17:50:47 +0200771 struct drm_connector *connector;
772 struct drm_encoder_helper_funcs *funcs;
773 struct drm_encoder *encoder;
774
775 connector = old_state->connectors[i];
776
777 if (!connector || !connector->state->best_encoder)
778 continue;
779
780 encoder = connector->state->best_encoder;
781 funcs = encoder->helper_private;
782
783 /*
784 * Each encoder has at most one connector (since we always steal
785 * it away), so we won't call call enable hooks twice.
786 */
787 if (encoder->bridge)
788 encoder->bridge->funcs->pre_enable(encoder->bridge);
789
790 funcs->commit(encoder);
791
792 if (encoder->bridge)
793 encoder->bridge->funcs->enable(encoder->bridge);
794 }
795}
796EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
797
Daniel Vettere2330f02014-10-29 11:34:56 +0100798static void wait_for_fences(struct drm_device *dev,
799 struct drm_atomic_state *state)
800{
801 int nplanes = dev->mode_config.num_total_plane;
802 int i;
803
804 for (i = 0; i < nplanes; i++) {
805 struct drm_plane *plane = state->planes[i];
806
807 if (!plane || !plane->state->fence)
808 continue;
809
810 WARN_ON(!plane->state->fb);
811
812 fence_wait(plane->state->fence, false);
813 fence_put(plane->state->fence);
814 plane->state->fence = NULL;
815 }
816}
817
Daniel Vetterab58e332014-11-24 20:42:42 +0100818static bool framebuffer_changed(struct drm_device *dev,
819 struct drm_atomic_state *old_state,
820 struct drm_crtc *crtc)
821{
822 struct drm_plane *plane;
823 struct drm_plane_state *old_plane_state;
824 int nplanes = old_state->dev->mode_config.num_total_plane;
825 int i;
826
827 for (i = 0; i < nplanes; i++) {
828 plane = old_state->planes[i];
829 old_plane_state = old_state->plane_states[i];
830
831 if (!plane)
832 continue;
833
834 if (plane->state->crtc != crtc &&
835 old_plane_state->crtc != crtc)
836 continue;
837
838 if (plane->state->fb != old_plane_state->fb)
839 return true;
840 }
841
842 return false;
843}
844
Rob Clark5ee32292014-11-11 19:38:59 -0500845/**
846 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
847 * @dev: DRM device
848 * @old_state: atomic state object with old state structures
849 *
850 * Helper to, after atomic commit, wait for vblanks on all effected
851 * crtcs (ie. before cleaning up old framebuffers using
Daniel Vetterab58e332014-11-24 20:42:42 +0100852 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
853 * framebuffers have actually changed to optimize for the legacy cursor and
854 * plane update use-case.
Rob Clark5ee32292014-11-11 19:38:59 -0500855 */
856void
857drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
858 struct drm_atomic_state *old_state)
Daniel Vetter623369e2014-09-16 17:50:47 +0200859{
860 struct drm_crtc *crtc;
861 struct drm_crtc_state *old_crtc_state;
862 int ncrtcs = old_state->dev->mode_config.num_crtc;
863 int i, ret;
864
865 for (i = 0; i < ncrtcs; i++) {
866 crtc = old_state->crtcs[i];
867 old_crtc_state = old_state->crtc_states[i];
868
869 if (!crtc)
870 continue;
871
872 /* No one cares about the old state, so abuse it for tracking
873 * and store whether we hold a vblank reference (and should do a
874 * vblank wait) in the ->enable boolean. */
875 old_crtc_state->enable = false;
876
877 if (!crtc->state->enable)
878 continue;
879
Daniel Vetterab58e332014-11-24 20:42:42 +0100880 if (!framebuffer_changed(dev, old_state, crtc))
881 continue;
882
Daniel Vetter623369e2014-09-16 17:50:47 +0200883 ret = drm_crtc_vblank_get(crtc);
884 if (ret != 0)
885 continue;
886
887 old_crtc_state->enable = true;
888 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
889 }
890
891 for (i = 0; i < ncrtcs; i++) {
892 crtc = old_state->crtcs[i];
893 old_crtc_state = old_state->crtc_states[i];
894
895 if (!crtc || !old_crtc_state->enable)
896 continue;
897
898 ret = wait_event_timeout(dev->vblank[i].queue,
899 old_crtc_state->last_vblank_count !=
900 drm_vblank_count(dev, i),
901 msecs_to_jiffies(50));
902
903 drm_crtc_vblank_put(crtc);
904 }
905}
Rob Clark5ee32292014-11-11 19:38:59 -0500906EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
Daniel Vetter623369e2014-09-16 17:50:47 +0200907
908/**
909 * drm_atomic_helper_commit - commit validated state object
910 * @dev: DRM device
911 * @state: the driver state object
912 * @async: asynchronous commit
913 *
914 * This function commits a with drm_atomic_helper_check() pre-validated state
915 * object. This can still fail when e.g. the framebuffer reservation fails. For
916 * now this doesn't implement asynchronous commits.
917 *
918 * RETURNS
919 * Zero for success or -errno.
920 */
921int drm_atomic_helper_commit(struct drm_device *dev,
922 struct drm_atomic_state *state,
923 bool async)
924{
925 int ret;
926
927 if (async)
928 return -EBUSY;
929
930 ret = drm_atomic_helper_prepare_planes(dev, state);
931 if (ret)
932 return ret;
933
934 /*
935 * This is the point of no return - everything below never fails except
936 * when the hw goes bonghits. Which means we can commit the new state on
937 * the software side now.
938 */
939
940 drm_atomic_helper_swap_state(dev, state);
941
942 /*
943 * Everything below can be run asynchronously without the need to grab
944 * any modeset locks at all under one conditions: It must be guaranteed
945 * that the asynchronous work has either been cancelled (if the driver
946 * supports it, which at least requires that the framebuffers get
947 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
948 * before the new state gets committed on the software side with
949 * drm_atomic_helper_swap_state().
950 *
951 * This scheme allows new atomic state updates to be prepared and
952 * checked in parallel to the asynchronous completion of the previous
953 * update. Which is important since compositors need to figure out the
954 * composition of the next frame right after having submitted the
955 * current layout.
956 */
957
Daniel Vettere2330f02014-10-29 11:34:56 +0100958 wait_for_fences(dev, state);
959
Daniel Vetter623369e2014-09-16 17:50:47 +0200960 drm_atomic_helper_commit_pre_planes(dev, state);
961
962 drm_atomic_helper_commit_planes(dev, state);
963
964 drm_atomic_helper_commit_post_planes(dev, state);
965
Rob Clark5ee32292014-11-11 19:38:59 -0500966 drm_atomic_helper_wait_for_vblanks(dev, state);
Daniel Vetter623369e2014-09-16 17:50:47 +0200967
968 drm_atomic_helper_cleanup_planes(dev, state);
969
970 drm_atomic_state_free(state);
971
972 return 0;
973}
974EXPORT_SYMBOL(drm_atomic_helper_commit);
975
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100976/**
Daniel Vettere8c833a2014-07-27 18:30:19 +0200977 * DOC: implementing async commit
978 *
979 * For now the atomic helpers don't support async commit directly. If there is
980 * real need it could be added though, using the dma-buf fence infrastructure
981 * for generic synchronization with outstanding rendering.
982 *
983 * For now drivers have to implement async commit themselves, with the following
984 * sequence being the recommended one:
985 *
986 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
987 * which commit needs to call which can fail, so we want to run it first and
988 * synchronously.
989 *
990 * 2. Synchronize with any outstanding asynchronous commit worker threads which
991 * might be affected the new state update. This can be done by either cancelling
992 * or flushing the work items, depending upon whether the driver can deal with
993 * cancelled updates. Note that it is important to ensure that the framebuffer
994 * cleanup is still done when cancelling.
995 *
996 * For sufficient parallelism it is recommended to have a work item per crtc
997 * (for updates which don't touch global state) and a global one. Then we only
998 * need to synchronize with the crtc work items for changed crtcs and the global
999 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1000 *
1001 * 3. The software state is updated synchronously with
1002 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
1003 * locks means concurrent callers never see inconsistent state. And doing this
1004 * while it's guaranteed that no relevant async worker runs means that async
1005 * workers do not need grab any locks. Actually they must not grab locks, for
1006 * otherwise the work flushing will deadlock.
1007 *
1008 * 4. Schedule a work item to do all subsequent steps, using the split-out
1009 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1010 * then cleaning up the framebuffers after the old framebuffer is no longer
1011 * being displayed.
1012 */
1013
1014/**
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001015 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
1016 * @dev: DRM device
1017 * @state: atomic state object with old state structures
1018 *
1019 * This function prepares plane state, specifically framebuffers, for the new
1020 * configuration. If any failure is encountered this function will call
1021 * ->cleanup_fb on any already successfully prepared framebuffer.
1022 *
1023 * Returns:
1024 * 0 on success, negative error code on failure.
1025 */
1026int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1027 struct drm_atomic_state *state)
1028{
1029 int nplanes = dev->mode_config.num_total_plane;
1030 int ret, i;
1031
1032 for (i = 0; i < nplanes; i++) {
1033 struct drm_plane_helper_funcs *funcs;
1034 struct drm_plane *plane = state->planes[i];
1035 struct drm_framebuffer *fb;
1036
1037 if (!plane)
1038 continue;
1039
1040 funcs = plane->helper_private;
1041
1042 fb = state->plane_states[i]->fb;
1043
1044 if (fb && funcs->prepare_fb) {
1045 ret = funcs->prepare_fb(plane, fb);
1046 if (ret)
1047 goto fail;
1048 }
1049 }
1050
1051 return 0;
1052
1053fail:
1054 for (i--; i >= 0; i--) {
1055 struct drm_plane_helper_funcs *funcs;
1056 struct drm_plane *plane = state->planes[i];
1057 struct drm_framebuffer *fb;
1058
1059 if (!plane)
1060 continue;
1061
1062 funcs = plane->helper_private;
1063
1064 fb = state->plane_states[i]->fb;
1065
1066 if (fb && funcs->cleanup_fb)
1067 funcs->cleanup_fb(plane, fb);
1068
1069 }
1070
1071 return ret;
1072}
1073EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1074
1075/**
1076 * drm_atomic_helper_commit_planes - commit plane state
1077 * @dev: DRM device
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001078 * @old_state: atomic state object with old state structures
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001079 *
1080 * This function commits the new plane state using the plane and atomic helper
1081 * functions for planes and crtcs. It assumes that the atomic state has already
1082 * been pushed into the relevant object state pointers, since this step can no
1083 * longer fail.
1084 *
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001085 * It still requires the global state object @old_state to know which planes and
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001086 * crtcs need to be updated though.
1087 */
1088void drm_atomic_helper_commit_planes(struct drm_device *dev,
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001089 struct drm_atomic_state *old_state)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001090{
1091 int nplanes = dev->mode_config.num_total_plane;
1092 int ncrtcs = dev->mode_config.num_crtc;
1093 int i;
1094
1095 for (i = 0; i < ncrtcs; i++) {
1096 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001097 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001098
1099 if (!crtc)
1100 continue;
1101
1102 funcs = crtc->helper_private;
1103
1104 if (!funcs || !funcs->atomic_begin)
1105 continue;
1106
1107 funcs->atomic_begin(crtc);
1108 }
1109
1110 for (i = 0; i < nplanes; i++) {
1111 struct drm_plane_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001112 struct drm_plane *plane = old_state->planes[i];
Thierry Redingf1c37e12014-11-25 12:09:44 +01001113 struct drm_plane_state *old_plane_state;
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001114
1115 if (!plane)
1116 continue;
1117
1118 funcs = plane->helper_private;
1119
Thierry Reding3cad4b62014-11-25 13:05:12 +01001120 if (!funcs)
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001121 continue;
1122
Thierry Redingf1c37e12014-11-25 12:09:44 +01001123 old_plane_state = old_state->plane_states[i];
1124
Thierry Reding407b8bd2014-11-20 12:05:50 +01001125 /*
1126 * Special-case disabling the plane if drivers support it.
1127 */
1128 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1129 funcs->atomic_disable)
1130 funcs->atomic_disable(plane, old_plane_state);
1131 else
1132 funcs->atomic_update(plane, old_plane_state);
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001133 }
1134
1135 for (i = 0; i < ncrtcs; i++) {
1136 struct drm_crtc_helper_funcs *funcs;
Daniel Vetterb0fcfc82014-11-19 18:38:11 +01001137 struct drm_crtc *crtc = old_state->crtcs[i];
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001138
1139 if (!crtc)
1140 continue;
1141
1142 funcs = crtc->helper_private;
1143
1144 if (!funcs || !funcs->atomic_flush)
1145 continue;
1146
1147 funcs->atomic_flush(crtc);
1148 }
1149}
1150EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1151
1152/**
1153 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1154 * @dev: DRM device
1155 * @old_state: atomic state object with old state structures
1156 *
1157 * This function cleans up plane state, specifically framebuffers, from the old
1158 * configuration. Hence the old configuration must be perserved in @old_state to
1159 * be able to call this function.
1160 *
1161 * This function must also be called on the new state when the atomic update
1162 * fails at any point after calling drm_atomic_helper_prepare_planes().
1163 */
1164void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1165 struct drm_atomic_state *old_state)
1166{
1167 int nplanes = dev->mode_config.num_total_plane;
1168 int i;
1169
1170 for (i = 0; i < nplanes; i++) {
1171 struct drm_plane_helper_funcs *funcs;
1172 struct drm_plane *plane = old_state->planes[i];
1173 struct drm_framebuffer *old_fb;
1174
1175 if (!plane)
1176 continue;
1177
1178 funcs = plane->helper_private;
1179
1180 old_fb = old_state->plane_states[i]->fb;
1181
1182 if (old_fb && funcs->cleanup_fb)
1183 funcs->cleanup_fb(plane, old_fb);
1184 }
1185}
1186EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1187
1188/**
1189 * drm_atomic_helper_swap_state - store atomic state into current sw state
1190 * @dev: DRM device
1191 * @state: atomic state
1192 *
1193 * This function stores the atomic state into the current state pointers in all
1194 * driver objects. It should be called after all failing steps have been done
1195 * and succeeded, but before the actual hardware state is committed.
1196 *
1197 * For cleanup and error recovery the current state for all changed objects will
1198 * be swaped into @state.
1199 *
1200 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1201 *
1202 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1203 *
1204 * 2. Do any other steps that might fail.
1205 *
1206 * 3. Put the staged state into the current state pointers with this function.
1207 *
1208 * 4. Actually commit the hardware state.
1209 *
1210 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1211 * contains the old state. Also do any other cleanup required with that state.
1212 */
1213void drm_atomic_helper_swap_state(struct drm_device *dev,
1214 struct drm_atomic_state *state)
1215{
1216 int i;
1217
1218 for (i = 0; i < dev->mode_config.num_connector; i++) {
1219 struct drm_connector *connector = state->connectors[i];
1220
1221 if (!connector)
1222 continue;
1223
1224 connector->state->state = state;
1225 swap(state->connector_states[i], connector->state);
1226 connector->state->state = NULL;
1227 }
1228
1229 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1230 struct drm_crtc *crtc = state->crtcs[i];
1231
1232 if (!crtc)
1233 continue;
1234
1235 crtc->state->state = state;
1236 swap(state->crtc_states[i], crtc->state);
1237 crtc->state->state = NULL;
1238 }
1239
1240 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1241 struct drm_plane *plane = state->planes[i];
1242
1243 if (!plane)
1244 continue;
1245
1246 plane->state->state = state;
1247 swap(state->plane_states[i], plane->state);
1248 plane->state->state = NULL;
1249 }
1250}
1251EXPORT_SYMBOL(drm_atomic_helper_swap_state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001252
1253/**
1254 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1255 * @plane: plane object to update
1256 * @crtc: owning CRTC of owning plane
1257 * @fb: framebuffer to flip onto plane
1258 * @crtc_x: x offset of primary plane on crtc
1259 * @crtc_y: y offset of primary plane on crtc
1260 * @crtc_w: width of primary plane rectangle on crtc
1261 * @crtc_h: height of primary plane rectangle on crtc
1262 * @src_x: x offset of @fb for panning
1263 * @src_y: y offset of @fb for panning
1264 * @src_w: width of source rectangle in @fb
1265 * @src_h: height of source rectangle in @fb
1266 *
1267 * Provides a default plane update handler using the atomic driver interface.
1268 *
1269 * RETURNS:
1270 * Zero on success, error code on failure
1271 */
1272int drm_atomic_helper_update_plane(struct drm_plane *plane,
1273 struct drm_crtc *crtc,
1274 struct drm_framebuffer *fb,
1275 int crtc_x, int crtc_y,
1276 unsigned int crtc_w, unsigned int crtc_h,
1277 uint32_t src_x, uint32_t src_y,
1278 uint32_t src_w, uint32_t src_h)
1279{
1280 struct drm_atomic_state *state;
1281 struct drm_plane_state *plane_state;
1282 int ret = 0;
1283
1284 state = drm_atomic_state_alloc(plane->dev);
1285 if (!state)
1286 return -ENOMEM;
1287
1288 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1289retry:
1290 plane_state = drm_atomic_get_plane_state(state, plane);
1291 if (IS_ERR(plane_state)) {
1292 ret = PTR_ERR(plane_state);
1293 goto fail;
1294 }
1295
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001296 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001297 if (ret != 0)
1298 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001299 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001300 plane_state->crtc_x = crtc_x;
1301 plane_state->crtc_y = crtc_y;
1302 plane_state->crtc_h = crtc_h;
1303 plane_state->crtc_w = crtc_w;
1304 plane_state->src_x = src_x;
1305 plane_state->src_y = src_y;
1306 plane_state->src_h = src_h;
1307 plane_state->src_w = src_w;
1308
1309 ret = drm_atomic_commit(state);
1310 if (ret != 0)
1311 goto fail;
1312
1313 /* Driver takes ownership of state on successful commit. */
1314 return 0;
1315fail:
1316 if (ret == -EDEADLK)
1317 goto backoff;
1318
1319 drm_atomic_state_free(state);
1320
1321 return ret;
1322backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001323 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001324 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001325
1326 /*
1327 * Someone might have exchanged the framebuffer while we dropped locks
1328 * in the backoff code. We need to fix up the fb refcount tracking the
1329 * core does for us.
1330 */
1331 plane->old_fb = plane->fb;
1332
1333 goto retry;
1334}
1335EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1336
1337/**
1338 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1339 * @plane: plane to disable
1340 *
1341 * Provides a default plane disable handler using the atomic driver interface.
1342 *
1343 * RETURNS:
1344 * Zero on success, error code on failure
1345 */
1346int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1347{
1348 struct drm_atomic_state *state;
1349 struct drm_plane_state *plane_state;
1350 int ret = 0;
1351
Jasper St. Pierreaa54e2e2014-11-20 19:59:15 -08001352 /*
1353 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1354 * acquire context. The real fix will be to wire the acquire ctx through
1355 * everywhere we need it, but meanwhile prevent chaos by just skipping
1356 * this noop. The critical case is the cursor ioctls which a) only grab
1357 * crtc/cursor-plane locks (so we need the crtc to get at the right
1358 * acquire context) and b) can try to disable the plane multiple times.
1359 */
1360 if (!plane->crtc)
1361 return 0;
1362
Daniel Vetter042652e2014-07-27 13:46:52 +02001363 state = drm_atomic_state_alloc(plane->dev);
1364 if (!state)
1365 return -ENOMEM;
1366
1367 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1368retry:
1369 plane_state = drm_atomic_get_plane_state(state, plane);
1370 if (IS_ERR(plane_state)) {
1371 ret = PTR_ERR(plane_state);
1372 goto fail;
1373 }
1374
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001375 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001376 if (ret != 0)
1377 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001378 drm_atomic_set_fb_for_plane(plane_state, NULL);
Daniel Vetter042652e2014-07-27 13:46:52 +02001379 plane_state->crtc_x = 0;
1380 plane_state->crtc_y = 0;
1381 plane_state->crtc_h = 0;
1382 plane_state->crtc_w = 0;
1383 plane_state->src_x = 0;
1384 plane_state->src_y = 0;
1385 plane_state->src_h = 0;
1386 plane_state->src_w = 0;
1387
1388 ret = drm_atomic_commit(state);
1389 if (ret != 0)
1390 goto fail;
1391
1392 /* Driver takes ownership of state on successful commit. */
1393 return 0;
1394fail:
1395 if (ret == -EDEADLK)
1396 goto backoff;
1397
1398 drm_atomic_state_free(state);
1399
1400 return ret;
1401backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001402 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001403 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001404
1405 /*
1406 * Someone might have exchanged the framebuffer while we dropped locks
1407 * in the backoff code. We need to fix up the fb refcount tracking the
1408 * core does for us.
1409 */
1410 plane->old_fb = plane->fb;
1411
1412 goto retry;
1413}
1414EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1415
1416static int update_output_state(struct drm_atomic_state *state,
1417 struct drm_mode_set *set)
1418{
1419 struct drm_device *dev = set->crtc->dev;
1420 struct drm_connector_state *conn_state;
Daniel Vetter042652e2014-07-27 13:46:52 +02001421 int ncrtcs = state->dev->mode_config.num_crtc;
1422 int ret, i, j;
1423
1424 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1425 state->acquire_ctx);
1426 if (ret)
1427 return ret;
1428
1429 /* First grab all affected connector/crtc states. */
1430 for (i = 0; i < set->num_connectors; i++) {
1431 conn_state = drm_atomic_get_connector_state(state,
1432 set->connectors[i]);
1433 if (IS_ERR(conn_state))
1434 return PTR_ERR(conn_state);
1435 }
1436
1437 for (i = 0; i < ncrtcs; i++) {
1438 struct drm_crtc *crtc = state->crtcs[i];
1439
1440 if (!crtc)
1441 continue;
1442
1443 ret = drm_atomic_add_affected_connectors(state, crtc);
1444 if (ret)
1445 return ret;
1446 }
1447
1448 /* Then recompute connector->crtc links and crtc enabling state. */
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001449 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter042652e2014-07-27 13:46:52 +02001450 struct drm_connector *connector;
1451
1452 connector = state->connectors[i];
1453 conn_state = state->connector_states[i];
1454
1455 if (!connector)
1456 continue;
1457
1458 if (conn_state->crtc == set->crtc) {
1459 ret = drm_atomic_set_crtc_for_connector(conn_state,
1460 NULL);
1461 if (ret)
1462 return ret;
1463 }
1464
1465 for (j = 0; j < set->num_connectors; j++) {
1466 if (set->connectors[j] == connector) {
1467 ret = drm_atomic_set_crtc_for_connector(conn_state,
1468 set->crtc);
1469 if (ret)
1470 return ret;
1471 break;
1472 }
1473 }
1474 }
1475
1476 for (i = 0; i < ncrtcs; i++) {
1477 struct drm_crtc *crtc = state->crtcs[i];
1478 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1479
1480 if (!crtc)
1481 continue;
1482
1483 /* Don't update ->enable for the CRTC in the set_config request,
1484 * since a mismatch would indicate a bug in the upper layers.
1485 * The actual modeset code later on will catch any
1486 * inconsistencies here. */
1487 if (crtc == set->crtc)
1488 continue;
1489
1490 crtc_state->enable =
1491 drm_atomic_connectors_for_crtc(state, crtc);
1492 }
1493
1494 return 0;
1495}
1496
1497/**
1498 * drm_atomic_helper_set_config - set a new config from userspace
1499 * @set: mode set configuration
1500 *
1501 * Provides a default crtc set_config handler using the atomic driver interface.
1502 *
1503 * Returns:
1504 * Returns 0 on success, negative errno numbers on failure.
1505 */
1506int drm_atomic_helper_set_config(struct drm_mode_set *set)
1507{
1508 struct drm_atomic_state *state;
1509 struct drm_crtc *crtc = set->crtc;
1510 struct drm_crtc_state *crtc_state;
1511 struct drm_plane_state *primary_state;
1512 int ret = 0;
1513
1514 state = drm_atomic_state_alloc(crtc->dev);
1515 if (!state)
1516 return -ENOMEM;
1517
1518 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1519retry:
1520 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1521 if (IS_ERR(crtc_state)) {
1522 ret = PTR_ERR(crtc_state);
1523 goto fail;
1524 }
1525
Rob Clarke5b53412014-11-26 18:58:04 -05001526 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1527 if (IS_ERR(primary_state)) {
1528 ret = PTR_ERR(primary_state);
1529 goto fail;
1530 }
1531
Daniel Vetter042652e2014-07-27 13:46:52 +02001532 if (!set->mode) {
1533 WARN_ON(set->fb);
1534 WARN_ON(set->num_connectors);
1535
1536 crtc_state->enable = false;
Rob Clarke5b53412014-11-26 18:58:04 -05001537
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001538 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
Rob Clarke5b53412014-11-26 18:58:04 -05001539 if (ret != 0)
1540 goto fail;
1541
1542 drm_atomic_set_fb_for_plane(primary_state, NULL);
1543
Daniel Vetter042652e2014-07-27 13:46:52 +02001544 goto commit;
1545 }
1546
1547 WARN_ON(!set->fb);
1548 WARN_ON(!set->num_connectors);
1549
1550 crtc_state->enable = true;
1551 drm_mode_copy(&crtc_state->mode, set->mode);
1552
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001553 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
Daniel Vetter042652e2014-07-27 13:46:52 +02001554 if (ret != 0)
1555 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001556 drm_atomic_set_fb_for_plane(primary_state, set->fb);
Daniel Vetter042652e2014-07-27 13:46:52 +02001557 primary_state->crtc_x = 0;
1558 primary_state->crtc_y = 0;
1559 primary_state->crtc_h = set->mode->vdisplay;
1560 primary_state->crtc_w = set->mode->hdisplay;
1561 primary_state->src_x = set->x << 16;
1562 primary_state->src_y = set->y << 16;
1563 primary_state->src_h = set->mode->vdisplay << 16;
1564 primary_state->src_w = set->mode->hdisplay << 16;
1565
1566commit:
1567 ret = update_output_state(state, set);
1568 if (ret)
1569 goto fail;
1570
1571 ret = drm_atomic_commit(state);
1572 if (ret != 0)
1573 goto fail;
1574
1575 /* Driver takes ownership of state on successful commit. */
1576 return 0;
1577fail:
1578 if (ret == -EDEADLK)
1579 goto backoff;
1580
1581 drm_atomic_state_free(state);
1582
1583 return ret;
1584backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001585 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001586 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001587
1588 /*
1589 * Someone might have exchanged the framebuffer while we dropped locks
1590 * in the backoff code. We need to fix up the fb refcount tracking the
1591 * core does for us.
1592 */
1593 crtc->primary->old_fb = crtc->primary->fb;
1594
1595 goto retry;
1596}
1597EXPORT_SYMBOL(drm_atomic_helper_set_config);
1598
1599/**
1600 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1601 * @crtc: DRM crtc
1602 * @property: DRM property
1603 * @val: value of property
1604 *
1605 * Provides a default plane disablle handler using the atomic driver interface.
1606 *
1607 * RETURNS:
1608 * Zero on success, error code on failure
1609 */
1610int
1611drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1612 struct drm_property *property,
1613 uint64_t val)
1614{
1615 struct drm_atomic_state *state;
1616 struct drm_crtc_state *crtc_state;
1617 int ret = 0;
1618
1619 state = drm_atomic_state_alloc(crtc->dev);
1620 if (!state)
1621 return -ENOMEM;
1622
1623 /* ->set_property is always called with all locks held. */
1624 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1625retry:
1626 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1627 if (IS_ERR(crtc_state)) {
1628 ret = PTR_ERR(crtc_state);
1629 goto fail;
1630 }
1631
Rob Clark40ecc692014-12-18 16:01:46 -05001632 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1633 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001634 if (ret)
1635 goto fail;
1636
1637 ret = drm_atomic_commit(state);
1638 if (ret != 0)
1639 goto fail;
1640
1641 /* Driver takes ownership of state on successful commit. */
1642 return 0;
1643fail:
1644 if (ret == -EDEADLK)
1645 goto backoff;
1646
1647 drm_atomic_state_free(state);
1648
1649 return ret;
1650backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001651 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001652 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001653
1654 goto retry;
1655}
1656EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1657
1658/**
1659 * drm_atomic_helper_plane_set_property - helper for plane prorties
1660 * @plane: DRM plane
1661 * @property: DRM property
1662 * @val: value of property
1663 *
1664 * Provides a default plane disable handler using the atomic driver interface.
1665 *
1666 * RETURNS:
1667 * Zero on success, error code on failure
1668 */
1669int
1670drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1671 struct drm_property *property,
1672 uint64_t val)
1673{
1674 struct drm_atomic_state *state;
1675 struct drm_plane_state *plane_state;
1676 int ret = 0;
1677
1678 state = drm_atomic_state_alloc(plane->dev);
1679 if (!state)
1680 return -ENOMEM;
1681
1682 /* ->set_property is always called with all locks held. */
1683 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1684retry:
1685 plane_state = drm_atomic_get_plane_state(state, plane);
1686 if (IS_ERR(plane_state)) {
1687 ret = PTR_ERR(plane_state);
1688 goto fail;
1689 }
1690
Rob Clark40ecc692014-12-18 16:01:46 -05001691 ret = drm_atomic_plane_set_property(plane, plane_state,
1692 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001693 if (ret)
1694 goto fail;
1695
1696 ret = drm_atomic_commit(state);
1697 if (ret != 0)
1698 goto fail;
1699
1700 /* Driver takes ownership of state on successful commit. */
1701 return 0;
1702fail:
1703 if (ret == -EDEADLK)
1704 goto backoff;
1705
1706 drm_atomic_state_free(state);
1707
1708 return ret;
1709backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001710 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001711 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001712
1713 goto retry;
1714}
1715EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1716
1717/**
1718 * drm_atomic_helper_connector_set_property - helper for connector prorties
1719 * @connector: DRM connector
1720 * @property: DRM property
1721 * @val: value of property
1722 *
1723 * Provides a default plane disablle handler using the atomic driver interface.
1724 *
1725 * RETURNS:
1726 * Zero on success, error code on failure
1727 */
1728int
1729drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1730 struct drm_property *property,
1731 uint64_t val)
1732{
1733 struct drm_atomic_state *state;
1734 struct drm_connector_state *connector_state;
1735 int ret = 0;
1736
1737 state = drm_atomic_state_alloc(connector->dev);
1738 if (!state)
1739 return -ENOMEM;
1740
1741 /* ->set_property is always called with all locks held. */
1742 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1743retry:
1744 connector_state = drm_atomic_get_connector_state(state, connector);
1745 if (IS_ERR(connector_state)) {
1746 ret = PTR_ERR(connector_state);
1747 goto fail;
1748 }
1749
Rob Clark40ecc692014-12-18 16:01:46 -05001750 ret = drm_atomic_connector_set_property(connector, connector_state,
1751 property, val);
Daniel Vetter042652e2014-07-27 13:46:52 +02001752 if (ret)
1753 goto fail;
1754
1755 ret = drm_atomic_commit(state);
1756 if (ret != 0)
1757 goto fail;
1758
1759 /* Driver takes ownership of state on successful commit. */
1760 return 0;
1761fail:
1762 if (ret == -EDEADLK)
1763 goto backoff;
1764
1765 drm_atomic_state_free(state);
1766
1767 return ret;
1768backoff:
Daniel Vetter042652e2014-07-27 13:46:52 +02001769 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001770 drm_atomic_legacy_backoff(state);
Daniel Vetter042652e2014-07-27 13:46:52 +02001771
1772 goto retry;
1773}
1774EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001775
1776/**
1777 * drm_atomic_helper_page_flip - execute a legacy page flip
1778 * @crtc: DRM crtc
1779 * @fb: DRM framebuffer
1780 * @event: optional DRM event to signal upon completion
1781 * @flags: flip flags for non-vblank sync'ed updates
1782 *
1783 * Provides a default page flip implementation using the atomic driver interface.
1784 *
1785 * Note that for now so called async page flips (i.e. updates which are not
1786 * synchronized to vblank) are not supported, since the atomic interfaces have
1787 * no provisions for this yet.
1788 *
1789 * Returns:
1790 * Returns 0 on success, negative errno numbers on failure.
1791 */
1792int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1793 struct drm_framebuffer *fb,
1794 struct drm_pending_vblank_event *event,
1795 uint32_t flags)
1796{
1797 struct drm_plane *plane = crtc->primary;
1798 struct drm_atomic_state *state;
1799 struct drm_plane_state *plane_state;
1800 struct drm_crtc_state *crtc_state;
1801 int ret = 0;
1802
1803 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1804 return -EINVAL;
1805
1806 state = drm_atomic_state_alloc(plane->dev);
1807 if (!state)
1808 return -ENOMEM;
1809
1810 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1811retry:
1812 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1813 if (IS_ERR(crtc_state)) {
1814 ret = PTR_ERR(crtc_state);
1815 goto fail;
1816 }
1817 crtc_state->event = event;
1818
1819 plane_state = drm_atomic_get_plane_state(state, plane);
1820 if (IS_ERR(plane_state)) {
1821 ret = PTR_ERR(plane_state);
1822 goto fail;
1823 }
1824
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001825 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001826 if (ret != 0)
1827 goto fail;
Daniel Vetter321ebf02014-11-04 22:57:27 +01001828 drm_atomic_set_fb_for_plane(plane_state, fb);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001829
1830 ret = drm_atomic_async_commit(state);
1831 if (ret != 0)
1832 goto fail;
1833
1834 /* TODO: ->page_flip is the only driver callback where the core
1835 * doesn't update plane->fb. For now patch it up here. */
1836 plane->fb = plane->state->fb;
1837
1838 /* Driver takes ownership of state on successful async commit. */
1839 return 0;
1840fail:
1841 if (ret == -EDEADLK)
1842 goto backoff;
1843
1844 drm_atomic_state_free(state);
1845
1846 return ret;
1847backoff:
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001848 drm_atomic_state_clear(state);
Daniel Vetter6f75cea2014-11-19 18:38:07 +01001849 drm_atomic_legacy_backoff(state);
Daniel Vetter8bc0f312014-07-27 18:42:37 +02001850
1851 /*
1852 * Someone might have exchanged the framebuffer while we dropped locks
1853 * in the backoff code. We need to fix up the fb refcount tracking the
1854 * core does for us.
1855 */
1856 plane->old_fb = plane->fb;
1857
1858 goto retry;
1859}
1860EXPORT_SYMBOL(drm_atomic_helper_page_flip);
Daniel Vetterd4617012014-11-03 15:56:43 +01001861
1862/**
Daniel Vetter3150c7d2014-11-06 20:53:29 +01001863 * DOC: atomic state reset and initialization
1864 *
1865 * Both the drm core and the atomic helpers assume that there is always the full
1866 * and correct atomic software state for all connectors, CRTCs and planes
1867 * available. Which is a bit a problem on driver load and also after system
1868 * suspend. One way to solve this is to have a hardware state read-out
1869 * infrastructure which reconstructs the full software state (e.g. the i915
1870 * driver).
1871 *
1872 * The simpler solution is to just reset the software state to everything off,
1873 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
1874 * the atomic helpers provide default reset implementations for all hooks.
1875 */
1876
1877/**
Daniel Vetterd4617012014-11-03 15:56:43 +01001878 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
1879 * @crtc: drm CRTC
1880 *
1881 * Resets the atomic state for @crtc by freeing the state pointer (which might
1882 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1883 */
1884void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
1885{
1886 kfree(crtc->state);
1887 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001888
1889 if (crtc->state)
1890 crtc->state->crtc = crtc;
Daniel Vetterd4617012014-11-03 15:56:43 +01001891}
1892EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
1893
1894/**
1895 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
1896 * @crtc: drm CRTC
1897 *
1898 * Default CRTC state duplicate hook for drivers which don't have their own
1899 * subclassed CRTC state structure.
1900 */
1901struct drm_crtc_state *
1902drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
1903{
1904 struct drm_crtc_state *state;
1905
1906 if (WARN_ON(!crtc->state))
1907 return NULL;
1908
1909 state = kmemdup(crtc->state, sizeof(*crtc->state), GFP_KERNEL);
1910
1911 if (state) {
1912 state->mode_changed = false;
1913 state->planes_changed = false;
1914 state->event = NULL;
1915 }
1916
1917 return state;
1918}
1919EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
1920
1921/**
1922 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
1923 * @crtc: drm CRTC
1924 * @state: CRTC state object to release
1925 *
1926 * Default CRTC state destroy hook for drivers which don't have their own
1927 * subclassed CRTC state structure.
1928 */
1929void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
1930 struct drm_crtc_state *state)
1931{
1932 kfree(state);
1933}
1934EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
1935
1936/**
1937 * drm_atomic_helper_plane_reset - default ->reset hook for planes
1938 * @plane: drm plane
1939 *
1940 * Resets the atomic state for @plane by freeing the state pointer (which might
1941 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1942 */
1943void drm_atomic_helper_plane_reset(struct drm_plane *plane)
1944{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001945 if (plane->state && plane->state->fb)
1946 drm_framebuffer_unreference(plane->state->fb);
1947
Daniel Vetterd4617012014-11-03 15:56:43 +01001948 kfree(plane->state);
1949 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001950
1951 if (plane->state)
1952 plane->state->plane = plane;
Daniel Vetterd4617012014-11-03 15:56:43 +01001953}
1954EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
1955
1956/**
1957 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
1958 * @plane: drm plane
1959 *
1960 * Default plane state duplicate hook for drivers which don't have their own
1961 * subclassed plane state structure.
1962 */
1963struct drm_plane_state *
1964drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
1965{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001966 struct drm_plane_state *state;
1967
Daniel Vetterd4617012014-11-03 15:56:43 +01001968 if (WARN_ON(!plane->state))
1969 return NULL;
1970
Daniel Vetter321ebf02014-11-04 22:57:27 +01001971 state = kmemdup(plane->state, sizeof(*plane->state), GFP_KERNEL);
1972
1973 if (state && state->fb)
1974 drm_framebuffer_reference(state->fb);
1975
1976 return state;
Daniel Vetterd4617012014-11-03 15:56:43 +01001977}
1978EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
1979
1980/**
1981 * drm_atomic_helper_plane_destroy_state - default state destroy hook
1982 * @plane: drm plane
1983 * @state: plane state object to release
1984 *
1985 * Default plane state destroy hook for drivers which don't have their own
1986 * subclassed plane state structure.
1987 */
1988void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
Daniel Vetter321ebf02014-11-04 22:57:27 +01001989 struct drm_plane_state *state)
Daniel Vetterd4617012014-11-03 15:56:43 +01001990{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001991 if (state->fb)
1992 drm_framebuffer_unreference(state->fb);
1993
Daniel Vetterd4617012014-11-03 15:56:43 +01001994 kfree(state);
1995}
1996EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
1997
1998/**
1999 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2000 * @connector: drm connector
2001 *
2002 * Resets the atomic state for @connector by freeing the state pointer (which
2003 * might be NULL, e.g. at driver load time) and allocating a new empty state
2004 * object.
2005 */
2006void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2007{
2008 kfree(connector->state);
2009 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01002010
2011 if (connector->state)
2012 connector->state->connector = connector;
Daniel Vetterd4617012014-11-03 15:56:43 +01002013}
2014EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2015
2016/**
2017 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2018 * @connector: drm connector
2019 *
2020 * Default connector state duplicate hook for drivers which don't have their own
2021 * subclassed connector state structure.
2022 */
2023struct drm_connector_state *
2024drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2025{
2026 if (WARN_ON(!connector->state))
2027 return NULL;
2028
2029 return kmemdup(connector->state, sizeof(*connector->state), GFP_KERNEL);
2030}
2031EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2032
2033/**
2034 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2035 * @connector: drm connector
2036 * @state: connector state object to release
2037 *
2038 * Default connector state destroy hook for drivers which don't have their own
2039 * subclassed connector state structure.
2040 */
2041void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2042 struct drm_connector_state *state)
2043{
2044 kfree(state);
2045}
2046EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);