Loading...
1/*
2 * Copyright (C) 2018 Intel Corp.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors:
23 * Rob Clark <robdclark@gmail.com>
24 * Daniel Vetter <daniel.vetter@ffwll.ch>
25 */
26
27#include <drm/drm_atomic.h>
28#include <drm/drm_atomic_state_helper.h>
29#include <drm/drm_blend.h>
30#include <drm/drm_bridge.h>
31#include <drm/drm_connector.h>
32#include <drm/drm_crtc.h>
33#include <drm/drm_device.h>
34#include <drm/drm_framebuffer.h>
35#include <drm/drm_plane.h>
36#include <drm/drm_print.h>
37#include <drm/drm_vblank.h>
38#include <drm/drm_writeback.h>
39
40#include <linux/slab.h>
41#include <linux/dma-fence.h>
42
43/**
44 * DOC: atomic state reset and initialization
45 *
46 * Both the drm core and the atomic helpers assume that there is always the full
47 * and correct atomic software state for all connectors, CRTCs and planes
48 * available. Which is a bit a problem on driver load and also after system
49 * suspend. One way to solve this is to have a hardware state read-out
50 * infrastructure which reconstructs the full software state (e.g. the i915
51 * driver).
52 *
53 * The simpler solution is to just reset the software state to everything off,
54 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
55 * the atomic helpers provide default reset implementations for all hooks.
56 *
57 * On the upside the precise state tracking of atomic simplifies system suspend
58 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
59 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
60 * For other drivers the building blocks are split out, see the documentation
61 * for these functions.
62 */
63
64/**
65 * __drm_atomic_helper_crtc_state_reset - reset the CRTC state
66 * @crtc_state: atomic CRTC state, must not be NULL
67 * @crtc: CRTC object, must not be NULL
68 *
69 * Initializes the newly allocated @crtc_state with default
70 * values. This is useful for drivers that subclass the CRTC state.
71 */
72void
73__drm_atomic_helper_crtc_state_reset(struct drm_crtc_state *crtc_state,
74 struct drm_crtc *crtc)
75{
76 crtc_state->crtc = crtc;
77}
78EXPORT_SYMBOL(__drm_atomic_helper_crtc_state_reset);
79
80/**
81 * __drm_atomic_helper_crtc_reset - reset state on CRTC
82 * @crtc: drm CRTC
83 * @crtc_state: CRTC state to assign
84 *
85 * Initializes the newly allocated @crtc_state and assigns it to
86 * the &drm_crtc->state pointer of @crtc, usually required when
87 * initializing the drivers or when called from the &drm_crtc_funcs.reset
88 * hook.
89 *
90 * This is useful for drivers that subclass the CRTC state.
91 */
92void
93__drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
94 struct drm_crtc_state *crtc_state)
95{
96 if (crtc_state)
97 __drm_atomic_helper_crtc_state_reset(crtc_state, crtc);
98
99 if (drm_dev_has_vblank(crtc->dev))
100 drm_crtc_vblank_reset(crtc);
101
102 crtc->state = crtc_state;
103}
104EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset);
105
106/**
107 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
108 * @crtc: drm CRTC
109 *
110 * Resets the atomic state for @crtc by freeing the state pointer (which might
111 * be NULL, e.g. at driver load time) and allocating a new empty state object.
112 */
113void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
114{
115 struct drm_crtc_state *crtc_state =
116 kzalloc(sizeof(*crtc->state), GFP_KERNEL);
117
118 if (crtc->state)
119 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
120
121 __drm_atomic_helper_crtc_reset(crtc, crtc_state);
122}
123EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
124
125/**
126 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
127 * @crtc: CRTC object
128 * @state: atomic CRTC state
129 *
130 * Copies atomic state from a CRTC's current state and resets inferred values.
131 * This is useful for drivers that subclass the CRTC state.
132 */
133void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
134 struct drm_crtc_state *state)
135{
136 memcpy(state, crtc->state, sizeof(*state));
137
138 if (state->mode_blob)
139 drm_property_blob_get(state->mode_blob);
140 if (state->degamma_lut)
141 drm_property_blob_get(state->degamma_lut);
142 if (state->ctm)
143 drm_property_blob_get(state->ctm);
144 if (state->gamma_lut)
145 drm_property_blob_get(state->gamma_lut);
146 state->mode_changed = false;
147 state->active_changed = false;
148 state->planes_changed = false;
149 state->connectors_changed = false;
150 state->color_mgmt_changed = false;
151 state->zpos_changed = false;
152 state->commit = NULL;
153 state->event = NULL;
154 state->async_flip = false;
155
156 /* Self refresh should be canceled when a new update is available */
157 state->active = drm_atomic_crtc_effectively_active(state);
158 state->self_refresh_active = false;
159}
160EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
161
162/**
163 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
164 * @crtc: drm CRTC
165 *
166 * Default CRTC state duplicate hook for drivers which don't have their own
167 * subclassed CRTC state structure.
168 */
169struct drm_crtc_state *
170drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
171{
172 struct drm_crtc_state *state;
173
174 if (WARN_ON(!crtc->state))
175 return NULL;
176
177 state = kmalloc(sizeof(*state), GFP_KERNEL);
178 if (state)
179 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
180
181 return state;
182}
183EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
184
185/**
186 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
187 * @state: CRTC state object to release
188 *
189 * Releases all resources stored in the CRTC state without actually freeing
190 * the memory of the CRTC state. This is useful for drivers that subclass the
191 * CRTC state.
192 */
193void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
194{
195 if (state->commit) {
196 /*
197 * In the event that a non-blocking commit returns
198 * -ERESTARTSYS before the commit_tail work is queued, we will
199 * have an extra reference to the commit object. Release it, if
200 * the event has not been consumed by the worker.
201 *
202 * state->event may be freed, so we can't directly look at
203 * state->event->base.completion.
204 */
205 if (state->event && state->commit->abort_completion)
206 drm_crtc_commit_put(state->commit);
207
208 kfree(state->commit->event);
209 state->commit->event = NULL;
210
211 drm_crtc_commit_put(state->commit);
212 }
213
214 drm_property_blob_put(state->mode_blob);
215 drm_property_blob_put(state->degamma_lut);
216 drm_property_blob_put(state->ctm);
217 drm_property_blob_put(state->gamma_lut);
218}
219EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
220
221/**
222 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
223 * @crtc: drm CRTC
224 * @state: CRTC state object to release
225 *
226 * Default CRTC state destroy hook for drivers which don't have their own
227 * subclassed CRTC state structure.
228 */
229void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
230 struct drm_crtc_state *state)
231{
232 __drm_atomic_helper_crtc_destroy_state(state);
233 kfree(state);
234}
235EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
236
237/**
238 * __drm_atomic_helper_plane_state_reset - resets plane state to default values
239 * @plane_state: atomic plane state, must not be NULL
240 * @plane: plane object, must not be NULL
241 *
242 * Initializes the newly allocated @plane_state with default
243 * values. This is useful for drivers that subclass the CRTC state.
244 */
245void __drm_atomic_helper_plane_state_reset(struct drm_plane_state *plane_state,
246 struct drm_plane *plane)
247{
248 u64 val;
249
250 plane_state->plane = plane;
251 plane_state->rotation = DRM_MODE_ROTATE_0;
252
253 plane_state->alpha = DRM_BLEND_ALPHA_OPAQUE;
254 plane_state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
255
256 if (plane->color_encoding_property) {
257 if (!drm_object_property_get_default_value(&plane->base,
258 plane->color_encoding_property,
259 &val))
260 plane_state->color_encoding = val;
261 }
262
263 if (plane->color_range_property) {
264 if (!drm_object_property_get_default_value(&plane->base,
265 plane->color_range_property,
266 &val))
267 plane_state->color_range = val;
268 }
269
270 if (plane->zpos_property) {
271 if (!drm_object_property_get_default_value(&plane->base,
272 plane->zpos_property,
273 &val)) {
274 plane_state->zpos = val;
275 plane_state->normalized_zpos = val;
276 }
277 }
278
279 if (plane->hotspot_x_property) {
280 if (!drm_object_property_get_default_value(&plane->base,
281 plane->hotspot_x_property,
282 &val))
283 plane_state->hotspot_x = val;
284 }
285
286 if (plane->hotspot_y_property) {
287 if (!drm_object_property_get_default_value(&plane->base,
288 plane->hotspot_y_property,
289 &val))
290 plane_state->hotspot_y = val;
291 }
292}
293EXPORT_SYMBOL(__drm_atomic_helper_plane_state_reset);
294
295/**
296 * __drm_atomic_helper_plane_reset - reset state on plane
297 * @plane: drm plane
298 * @plane_state: plane state to assign
299 *
300 * Initializes the newly allocated @plane_state and assigns it to
301 * the &drm_crtc->state pointer of @plane, usually required when
302 * initializing the drivers or when called from the &drm_plane_funcs.reset
303 * hook.
304 *
305 * This is useful for drivers that subclass the plane state.
306 */
307void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
308 struct drm_plane_state *plane_state)
309{
310 if (plane_state)
311 __drm_atomic_helper_plane_state_reset(plane_state, plane);
312
313 plane->state = plane_state;
314}
315EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
316
317/**
318 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
319 * @plane: drm plane
320 *
321 * Resets the atomic state for @plane by freeing the state pointer (which might
322 * be NULL, e.g. at driver load time) and allocating a new empty state object.
323 */
324void drm_atomic_helper_plane_reset(struct drm_plane *plane)
325{
326 if (plane->state)
327 __drm_atomic_helper_plane_destroy_state(plane->state);
328
329 kfree(plane->state);
330 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
331 if (plane->state)
332 __drm_atomic_helper_plane_reset(plane, plane->state);
333}
334EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
335
336/**
337 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
338 * @plane: plane object
339 * @state: atomic plane state
340 *
341 * Copies atomic state from a plane's current state. This is useful for
342 * drivers that subclass the plane state.
343 */
344void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
345 struct drm_plane_state *state)
346{
347 memcpy(state, plane->state, sizeof(*state));
348
349 if (state->fb)
350 drm_framebuffer_get(state->fb);
351
352 state->fence = NULL;
353 state->commit = NULL;
354 state->fb_damage_clips = NULL;
355 state->color_mgmt_changed = false;
356}
357EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
358
359/**
360 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
361 * @plane: drm plane
362 *
363 * Default plane state duplicate hook for drivers which don't have their own
364 * subclassed plane state structure.
365 */
366struct drm_plane_state *
367drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
368{
369 struct drm_plane_state *state;
370
371 if (WARN_ON(!plane->state))
372 return NULL;
373
374 state = kmalloc(sizeof(*state), GFP_KERNEL);
375 if (state)
376 __drm_atomic_helper_plane_duplicate_state(plane, state);
377
378 return state;
379}
380EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
381
382/**
383 * __drm_atomic_helper_plane_destroy_state - release plane state
384 * @state: plane state object to release
385 *
386 * Releases all resources stored in the plane state without actually freeing
387 * the memory of the plane state. This is useful for drivers that subclass the
388 * plane state.
389 */
390void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
391{
392 if (state->fb)
393 drm_framebuffer_put(state->fb);
394
395 if (state->fence)
396 dma_fence_put(state->fence);
397
398 if (state->commit)
399 drm_crtc_commit_put(state->commit);
400
401 drm_property_blob_put(state->fb_damage_clips);
402}
403EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
404
405/**
406 * drm_atomic_helper_plane_destroy_state - default state destroy hook
407 * @plane: drm plane
408 * @state: plane state object to release
409 *
410 * Default plane state destroy hook for drivers which don't have their own
411 * subclassed plane state structure.
412 */
413void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
414 struct drm_plane_state *state)
415{
416 __drm_atomic_helper_plane_destroy_state(state);
417 kfree(state);
418}
419EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
420
421/**
422 * __drm_atomic_helper_connector_state_reset - reset the connector state
423 * @conn_state: atomic connector state, must not be NULL
424 * @connector: connectotr object, must not be NULL
425 *
426 * Initializes the newly allocated @conn_state with default
427 * values. This is useful for drivers that subclass the connector state.
428 */
429void
430__drm_atomic_helper_connector_state_reset(struct drm_connector_state *conn_state,
431 struct drm_connector *connector)
432{
433 conn_state->connector = connector;
434}
435EXPORT_SYMBOL(__drm_atomic_helper_connector_state_reset);
436
437/**
438 * __drm_atomic_helper_connector_reset - reset state on connector
439 * @connector: drm connector
440 * @conn_state: connector state to assign
441 *
442 * Initializes the newly allocated @conn_state and assigns it to
443 * the &drm_connector->state pointer of @connector, usually required when
444 * initializing the drivers or when called from the &drm_connector_funcs.reset
445 * hook.
446 *
447 * This is useful for drivers that subclass the connector state.
448 */
449void
450__drm_atomic_helper_connector_reset(struct drm_connector *connector,
451 struct drm_connector_state *conn_state)
452{
453 if (conn_state)
454 __drm_atomic_helper_connector_state_reset(conn_state, connector);
455
456 connector->state = conn_state;
457}
458EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
459
460/**
461 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
462 * @connector: drm connector
463 *
464 * Resets the atomic state for @connector by freeing the state pointer (which
465 * might be NULL, e.g. at driver load time) and allocating a new empty state
466 * object.
467 */
468void drm_atomic_helper_connector_reset(struct drm_connector *connector)
469{
470 struct drm_connector_state *conn_state =
471 kzalloc(sizeof(*conn_state), GFP_KERNEL);
472
473 if (connector->state)
474 __drm_atomic_helper_connector_destroy_state(connector->state);
475
476 kfree(connector->state);
477 __drm_atomic_helper_connector_reset(connector, conn_state);
478}
479EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
480
481/**
482 * drm_atomic_helper_connector_tv_margins_reset - Resets TV connector properties
483 * @connector: DRM connector
484 *
485 * Resets the TV-related properties attached to a connector.
486 */
487void drm_atomic_helper_connector_tv_margins_reset(struct drm_connector *connector)
488{
489 struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
490 struct drm_connector_state *state = connector->state;
491
492 state->tv.margins.left = cmdline->tv_margins.left;
493 state->tv.margins.right = cmdline->tv_margins.right;
494 state->tv.margins.top = cmdline->tv_margins.top;
495 state->tv.margins.bottom = cmdline->tv_margins.bottom;
496}
497EXPORT_SYMBOL(drm_atomic_helper_connector_tv_margins_reset);
498
499/**
500 * drm_atomic_helper_connector_tv_reset - Resets Analog TV connector properties
501 * @connector: DRM connector
502 *
503 * Resets the analog TV properties attached to a connector
504 */
505void drm_atomic_helper_connector_tv_reset(struct drm_connector *connector)
506{
507 struct drm_device *dev = connector->dev;
508 struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
509 struct drm_connector_state *state = connector->state;
510 struct drm_property *prop;
511 uint64_t val;
512
513 prop = dev->mode_config.tv_mode_property;
514 if (prop)
515 if (!drm_object_property_get_default_value(&connector->base,
516 prop, &val))
517 state->tv.mode = val;
518
519 if (cmdline->tv_mode_specified)
520 state->tv.mode = cmdline->tv_mode;
521
522 prop = dev->mode_config.tv_select_subconnector_property;
523 if (prop)
524 if (!drm_object_property_get_default_value(&connector->base,
525 prop, &val))
526 state->tv.select_subconnector = val;
527
528 prop = dev->mode_config.tv_subconnector_property;
529 if (prop)
530 if (!drm_object_property_get_default_value(&connector->base,
531 prop, &val))
532 state->tv.subconnector = val;
533
534 prop = dev->mode_config.tv_brightness_property;
535 if (prop)
536 if (!drm_object_property_get_default_value(&connector->base,
537 prop, &val))
538 state->tv.brightness = val;
539
540 prop = dev->mode_config.tv_contrast_property;
541 if (prop)
542 if (!drm_object_property_get_default_value(&connector->base,
543 prop, &val))
544 state->tv.contrast = val;
545
546 prop = dev->mode_config.tv_flicker_reduction_property;
547 if (prop)
548 if (!drm_object_property_get_default_value(&connector->base,
549 prop, &val))
550 state->tv.flicker_reduction = val;
551
552 prop = dev->mode_config.tv_overscan_property;
553 if (prop)
554 if (!drm_object_property_get_default_value(&connector->base,
555 prop, &val))
556 state->tv.overscan = val;
557
558 prop = dev->mode_config.tv_saturation_property;
559 if (prop)
560 if (!drm_object_property_get_default_value(&connector->base,
561 prop, &val))
562 state->tv.saturation = val;
563
564 prop = dev->mode_config.tv_hue_property;
565 if (prop)
566 if (!drm_object_property_get_default_value(&connector->base,
567 prop, &val))
568 state->tv.hue = val;
569
570 drm_atomic_helper_connector_tv_margins_reset(connector);
571}
572EXPORT_SYMBOL(drm_atomic_helper_connector_tv_reset);
573
574/**
575 * drm_atomic_helper_connector_tv_check - Validate an analog TV connector state
576 * @connector: DRM Connector
577 * @state: the DRM State object
578 *
579 * Checks the state object to see if the requested state is valid for an
580 * analog TV connector.
581 *
582 * Return:
583 * %0 for success, a negative error code on error.
584 */
585int drm_atomic_helper_connector_tv_check(struct drm_connector *connector,
586 struct drm_atomic_state *state)
587{
588 struct drm_connector_state *old_conn_state =
589 drm_atomic_get_old_connector_state(state, connector);
590 struct drm_connector_state *new_conn_state =
591 drm_atomic_get_new_connector_state(state, connector);
592 struct drm_crtc_state *crtc_state;
593 struct drm_crtc *crtc;
594
595 crtc = new_conn_state->crtc;
596 if (!crtc)
597 return 0;
598
599 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
600 if (!crtc_state)
601 return -EINVAL;
602
603 if (old_conn_state->tv.mode != new_conn_state->tv.mode)
604 crtc_state->mode_changed = true;
605
606 if (old_conn_state->tv.margins.left != new_conn_state->tv.margins.left ||
607 old_conn_state->tv.margins.right != new_conn_state->tv.margins.right ||
608 old_conn_state->tv.margins.top != new_conn_state->tv.margins.top ||
609 old_conn_state->tv.margins.bottom != new_conn_state->tv.margins.bottom ||
610 old_conn_state->tv.mode != new_conn_state->tv.mode ||
611 old_conn_state->tv.brightness != new_conn_state->tv.brightness ||
612 old_conn_state->tv.contrast != new_conn_state->tv.contrast ||
613 old_conn_state->tv.flicker_reduction != new_conn_state->tv.flicker_reduction ||
614 old_conn_state->tv.overscan != new_conn_state->tv.overscan ||
615 old_conn_state->tv.saturation != new_conn_state->tv.saturation ||
616 old_conn_state->tv.hue != new_conn_state->tv.hue)
617 crtc_state->connectors_changed = true;
618
619 return 0;
620}
621EXPORT_SYMBOL(drm_atomic_helper_connector_tv_check);
622
623/**
624 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
625 * @connector: connector object
626 * @state: atomic connector state
627 *
628 * Copies atomic state from a connector's current state. This is useful for
629 * drivers that subclass the connector state.
630 */
631void
632__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
633 struct drm_connector_state *state)
634{
635 memcpy(state, connector->state, sizeof(*state));
636 if (state->crtc)
637 drm_connector_get(connector);
638 state->commit = NULL;
639
640 if (state->hdr_output_metadata)
641 drm_property_blob_get(state->hdr_output_metadata);
642
643 /* Don't copy over a writeback job, they are used only once */
644 state->writeback_job = NULL;
645}
646EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
647
648/**
649 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
650 * @connector: drm connector
651 *
652 * Default connector state duplicate hook for drivers which don't have their own
653 * subclassed connector state structure.
654 */
655struct drm_connector_state *
656drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
657{
658 struct drm_connector_state *state;
659
660 if (WARN_ON(!connector->state))
661 return NULL;
662
663 state = kmalloc(sizeof(*state), GFP_KERNEL);
664 if (state)
665 __drm_atomic_helper_connector_duplicate_state(connector, state);
666
667 return state;
668}
669EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
670
671/**
672 * __drm_atomic_helper_connector_destroy_state - release connector state
673 * @state: connector state object to release
674 *
675 * Releases all resources stored in the connector state without actually
676 * freeing the memory of the connector state. This is useful for drivers that
677 * subclass the connector state.
678 */
679void
680__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
681{
682 if (state->crtc)
683 drm_connector_put(state->connector);
684
685 if (state->commit)
686 drm_crtc_commit_put(state->commit);
687
688 if (state->writeback_job)
689 drm_writeback_cleanup_job(state->writeback_job);
690
691 drm_property_blob_put(state->hdr_output_metadata);
692}
693EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
694
695/**
696 * drm_atomic_helper_connector_destroy_state - default state destroy hook
697 * @connector: drm connector
698 * @state: connector state object to release
699 *
700 * Default connector state destroy hook for drivers which don't have their own
701 * subclassed connector state structure.
702 */
703void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
704 struct drm_connector_state *state)
705{
706 __drm_atomic_helper_connector_destroy_state(state);
707 kfree(state);
708}
709EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
710
711/**
712 * __drm_atomic_helper_private_obj_duplicate_state - copy atomic private state
713 * @obj: CRTC object
714 * @state: new private object state
715 *
716 * Copies atomic state from a private objects's current state and resets inferred values.
717 * This is useful for drivers that subclass the private state.
718 */
719void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
720 struct drm_private_state *state)
721{
722 memcpy(state, obj->state, sizeof(*state));
723}
724EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);
725
726/**
727 * __drm_atomic_helper_bridge_duplicate_state() - Copy atomic bridge state
728 * @bridge: bridge object
729 * @state: atomic bridge state
730 *
731 * Copies atomic state from a bridge's current state and resets inferred values.
732 * This is useful for drivers that subclass the bridge state.
733 */
734void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge,
735 struct drm_bridge_state *state)
736{
737 __drm_atomic_helper_private_obj_duplicate_state(&bridge->base,
738 &state->base);
739 state->bridge = bridge;
740}
741EXPORT_SYMBOL(__drm_atomic_helper_bridge_duplicate_state);
742
743/**
744 * drm_atomic_helper_bridge_duplicate_state() - Duplicate a bridge state object
745 * @bridge: bridge object
746 *
747 * Allocates a new bridge state and initializes it with the current bridge
748 * state values. This helper is meant to be used as a bridge
749 * &drm_bridge_funcs.atomic_duplicate_state hook for bridges that don't
750 * subclass the bridge state.
751 */
752struct drm_bridge_state *
753drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge)
754{
755 struct drm_bridge_state *new;
756
757 if (WARN_ON(!bridge->base.state))
758 return NULL;
759
760 new = kzalloc(sizeof(*new), GFP_KERNEL);
761 if (new)
762 __drm_atomic_helper_bridge_duplicate_state(bridge, new);
763
764 return new;
765}
766EXPORT_SYMBOL(drm_atomic_helper_bridge_duplicate_state);
767
768/**
769 * drm_atomic_helper_bridge_destroy_state() - Destroy a bridge state object
770 * @bridge: the bridge this state refers to
771 * @state: bridge state to destroy
772 *
773 * Destroys a bridge state previously created by
774 * &drm_atomic_helper_bridge_reset() or
775 * &drm_atomic_helper_bridge_duplicate_state(). This helper is meant to be
776 * used as a bridge &drm_bridge_funcs.atomic_destroy_state hook for bridges
777 * that don't subclass the bridge state.
778 */
779void drm_atomic_helper_bridge_destroy_state(struct drm_bridge *bridge,
780 struct drm_bridge_state *state)
781{
782 kfree(state);
783}
784EXPORT_SYMBOL(drm_atomic_helper_bridge_destroy_state);
785
786/**
787 * __drm_atomic_helper_bridge_reset() - Initialize a bridge state to its
788 * default
789 * @bridge: the bridge this state refers to
790 * @state: bridge state to initialize
791 *
792 * Initializes the bridge state to default values. This is meant to be called
793 * by the bridge &drm_bridge_funcs.atomic_reset hook for bridges that subclass
794 * the bridge state.
795 */
796void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge,
797 struct drm_bridge_state *state)
798{
799 memset(state, 0, sizeof(*state));
800 state->bridge = bridge;
801}
802EXPORT_SYMBOL(__drm_atomic_helper_bridge_reset);
803
804/**
805 * drm_atomic_helper_bridge_reset() - Allocate and initialize a bridge state
806 * to its default
807 * @bridge: the bridge this state refers to
808 *
809 * Allocates the bridge state and initializes it to default values. This helper
810 * is meant to be used as a bridge &drm_bridge_funcs.atomic_reset hook for
811 * bridges that don't subclass the bridge state.
812 */
813struct drm_bridge_state *
814drm_atomic_helper_bridge_reset(struct drm_bridge *bridge)
815{
816 struct drm_bridge_state *bridge_state;
817
818 bridge_state = kzalloc(sizeof(*bridge_state), GFP_KERNEL);
819 if (!bridge_state)
820 return ERR_PTR(-ENOMEM);
821
822 __drm_atomic_helper_bridge_reset(bridge, bridge_state);
823 return bridge_state;
824}
825EXPORT_SYMBOL(drm_atomic_helper_bridge_reset);
1/*
2 * Copyright (C) 2018 Intel Corp.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors:
23 * Rob Clark <robdclark@gmail.com>
24 * Daniel Vetter <daniel.vetter@ffwll.ch>
25 */
26
27#include <drm/drm_atomic.h>
28#include <drm/drm_atomic_state_helper.h>
29#include <drm/drm_connector.h>
30#include <drm/drm_crtc.h>
31#include <drm/drm_device.h>
32#include <drm/drm_plane.h>
33#include <drm/drm_print.h>
34#include <drm/drm_writeback.h>
35
36#include <linux/slab.h>
37#include <linux/dma-fence.h>
38
39/**
40 * DOC: atomic state reset and initialization
41 *
42 * Both the drm core and the atomic helpers assume that there is always the full
43 * and correct atomic software state for all connectors, CRTCs and planes
44 * available. Which is a bit a problem on driver load and also after system
45 * suspend. One way to solve this is to have a hardware state read-out
46 * infrastructure which reconstructs the full software state (e.g. the i915
47 * driver).
48 *
49 * The simpler solution is to just reset the software state to everything off,
50 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
51 * the atomic helpers provide default reset implementations for all hooks.
52 *
53 * On the upside the precise state tracking of atomic simplifies system suspend
54 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
55 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
56 * For other drivers the building blocks are split out, see the documentation
57 * for these functions.
58 */
59
60/**
61 * __drm_atomic_helper_crtc_reset - reset state on CRTC
62 * @crtc: drm CRTC
63 * @crtc_state: CRTC state to assign
64 *
65 * Initializes the newly allocated @crtc_state and assigns it to
66 * the &drm_crtc->state pointer of @crtc, usually required when
67 * initializing the drivers or when called from the &drm_crtc_funcs.reset
68 * hook.
69 *
70 * This is useful for drivers that subclass the CRTC state.
71 */
72void
73__drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
74 struct drm_crtc_state *crtc_state)
75{
76 if (crtc_state)
77 crtc_state->crtc = crtc;
78
79 crtc->state = crtc_state;
80}
81EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset);
82
83/**
84 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
85 * @crtc: drm CRTC
86 *
87 * Resets the atomic state for @crtc by freeing the state pointer (which might
88 * be NULL, e.g. at driver load time) and allocating a new empty state object.
89 */
90void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
91{
92 struct drm_crtc_state *crtc_state =
93 kzalloc(sizeof(*crtc->state), GFP_KERNEL);
94
95 if (crtc->state)
96 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
97
98 __drm_atomic_helper_crtc_reset(crtc, crtc_state);
99}
100EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
101
102/**
103 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
104 * @crtc: CRTC object
105 * @state: atomic CRTC state
106 *
107 * Copies atomic state from a CRTC's current state and resets inferred values.
108 * This is useful for drivers that subclass the CRTC state.
109 */
110void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
111 struct drm_crtc_state *state)
112{
113 memcpy(state, crtc->state, sizeof(*state));
114
115 if (state->mode_blob)
116 drm_property_blob_get(state->mode_blob);
117 if (state->degamma_lut)
118 drm_property_blob_get(state->degamma_lut);
119 if (state->ctm)
120 drm_property_blob_get(state->ctm);
121 if (state->gamma_lut)
122 drm_property_blob_get(state->gamma_lut);
123 state->mode_changed = false;
124 state->active_changed = false;
125 state->planes_changed = false;
126 state->connectors_changed = false;
127 state->color_mgmt_changed = false;
128 state->zpos_changed = false;
129 state->commit = NULL;
130 state->event = NULL;
131 state->async_flip = false;
132
133 /* Self refresh should be canceled when a new update is available */
134 state->active = drm_atomic_crtc_effectively_active(state);
135 state->self_refresh_active = false;
136}
137EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
138
139/**
140 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
141 * @crtc: drm CRTC
142 *
143 * Default CRTC state duplicate hook for drivers which don't have their own
144 * subclassed CRTC state structure.
145 */
146struct drm_crtc_state *
147drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
148{
149 struct drm_crtc_state *state;
150
151 if (WARN_ON(!crtc->state))
152 return NULL;
153
154 state = kmalloc(sizeof(*state), GFP_KERNEL);
155 if (state)
156 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
157
158 return state;
159}
160EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
161
162/**
163 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
164 * @state: CRTC state object to release
165 *
166 * Releases all resources stored in the CRTC state without actually freeing
167 * the memory of the CRTC state. This is useful for drivers that subclass the
168 * CRTC state.
169 */
170void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
171{
172 if (state->commit) {
173 /*
174 * In the event that a non-blocking commit returns
175 * -ERESTARTSYS before the commit_tail work is queued, we will
176 * have an extra reference to the commit object. Release it, if
177 * the event has not been consumed by the worker.
178 *
179 * state->event may be freed, so we can't directly look at
180 * state->event->base.completion.
181 */
182 if (state->event && state->commit->abort_completion)
183 drm_crtc_commit_put(state->commit);
184
185 kfree(state->commit->event);
186 state->commit->event = NULL;
187
188 drm_crtc_commit_put(state->commit);
189 }
190
191 drm_property_blob_put(state->mode_blob);
192 drm_property_blob_put(state->degamma_lut);
193 drm_property_blob_put(state->ctm);
194 drm_property_blob_put(state->gamma_lut);
195}
196EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
197
198/**
199 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
200 * @crtc: drm CRTC
201 * @state: CRTC state object to release
202 *
203 * Default CRTC state destroy hook for drivers which don't have their own
204 * subclassed CRTC state structure.
205 */
206void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
207 struct drm_crtc_state *state)
208{
209 __drm_atomic_helper_crtc_destroy_state(state);
210 kfree(state);
211}
212EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
213
214/**
215 * __drm_atomic_helper_plane_reset - resets planes state to default values
216 * @plane: plane object, must not be NULL
217 * @state: atomic plane state, must not be NULL
218 *
219 * Initializes plane state to default. This is useful for drivers that subclass
220 * the plane state.
221 */
222void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
223 struct drm_plane_state *state)
224{
225 state->plane = plane;
226 state->rotation = DRM_MODE_ROTATE_0;
227
228 state->alpha = DRM_BLEND_ALPHA_OPAQUE;
229 state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
230
231 plane->state = state;
232}
233EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
234
235/**
236 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
237 * @plane: drm plane
238 *
239 * Resets the atomic state for @plane by freeing the state pointer (which might
240 * be NULL, e.g. at driver load time) and allocating a new empty state object.
241 */
242void drm_atomic_helper_plane_reset(struct drm_plane *plane)
243{
244 if (plane->state)
245 __drm_atomic_helper_plane_destroy_state(plane->state);
246
247 kfree(plane->state);
248 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
249 if (plane->state)
250 __drm_atomic_helper_plane_reset(plane, plane->state);
251}
252EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
253
254/**
255 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
256 * @plane: plane object
257 * @state: atomic plane state
258 *
259 * Copies atomic state from a plane's current state. This is useful for
260 * drivers that subclass the plane state.
261 */
262void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
263 struct drm_plane_state *state)
264{
265 memcpy(state, plane->state, sizeof(*state));
266
267 if (state->fb)
268 drm_framebuffer_get(state->fb);
269
270 state->fence = NULL;
271 state->commit = NULL;
272 state->fb_damage_clips = NULL;
273}
274EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
275
276/**
277 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
278 * @plane: drm plane
279 *
280 * Default plane state duplicate hook for drivers which don't have their own
281 * subclassed plane state structure.
282 */
283struct drm_plane_state *
284drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
285{
286 struct drm_plane_state *state;
287
288 if (WARN_ON(!plane->state))
289 return NULL;
290
291 state = kmalloc(sizeof(*state), GFP_KERNEL);
292 if (state)
293 __drm_atomic_helper_plane_duplicate_state(plane, state);
294
295 return state;
296}
297EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
298
299/**
300 * __drm_atomic_helper_plane_destroy_state - release plane state
301 * @state: plane state object to release
302 *
303 * Releases all resources stored in the plane state without actually freeing
304 * the memory of the plane state. This is useful for drivers that subclass the
305 * plane state.
306 */
307void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
308{
309 if (state->fb)
310 drm_framebuffer_put(state->fb);
311
312 if (state->fence)
313 dma_fence_put(state->fence);
314
315 if (state->commit)
316 drm_crtc_commit_put(state->commit);
317
318 drm_property_blob_put(state->fb_damage_clips);
319}
320EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
321
322/**
323 * drm_atomic_helper_plane_destroy_state - default state destroy hook
324 * @plane: drm plane
325 * @state: plane state object to release
326 *
327 * Default plane state destroy hook for drivers which don't have their own
328 * subclassed plane state structure.
329 */
330void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
331 struct drm_plane_state *state)
332{
333 __drm_atomic_helper_plane_destroy_state(state);
334 kfree(state);
335}
336EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
337
338/**
339 * __drm_atomic_helper_connector_reset - reset state on connector
340 * @connector: drm connector
341 * @conn_state: connector state to assign
342 *
343 * Initializes the newly allocated @conn_state and assigns it to
344 * the &drm_connector->state pointer of @connector, usually required when
345 * initializing the drivers or when called from the &drm_connector_funcs.reset
346 * hook.
347 *
348 * This is useful for drivers that subclass the connector state.
349 */
350void
351__drm_atomic_helper_connector_reset(struct drm_connector *connector,
352 struct drm_connector_state *conn_state)
353{
354 if (conn_state)
355 conn_state->connector = connector;
356
357 connector->state = conn_state;
358}
359EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
360
361/**
362 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
363 * @connector: drm connector
364 *
365 * Resets the atomic state for @connector by freeing the state pointer (which
366 * might be NULL, e.g. at driver load time) and allocating a new empty state
367 * object.
368 */
369void drm_atomic_helper_connector_reset(struct drm_connector *connector)
370{
371 struct drm_connector_state *conn_state =
372 kzalloc(sizeof(*conn_state), GFP_KERNEL);
373
374 if (connector->state)
375 __drm_atomic_helper_connector_destroy_state(connector->state);
376
377 kfree(connector->state);
378 __drm_atomic_helper_connector_reset(connector, conn_state);
379}
380EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
381
382/**
383 * drm_atomic_helper_connector_tv_reset - Resets TV connector properties
384 * @connector: DRM connector
385 *
386 * Resets the TV-related properties attached to a connector.
387 */
388void drm_atomic_helper_connector_tv_reset(struct drm_connector *connector)
389{
390 struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
391 struct drm_connector_state *state = connector->state;
392
393 state->tv.margins.left = cmdline->tv_margins.left;
394 state->tv.margins.right = cmdline->tv_margins.right;
395 state->tv.margins.top = cmdline->tv_margins.top;
396 state->tv.margins.bottom = cmdline->tv_margins.bottom;
397}
398EXPORT_SYMBOL(drm_atomic_helper_connector_tv_reset);
399
400/**
401 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
402 * @connector: connector object
403 * @state: atomic connector state
404 *
405 * Copies atomic state from a connector's current state. This is useful for
406 * drivers that subclass the connector state.
407 */
408void
409__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
410 struct drm_connector_state *state)
411{
412 memcpy(state, connector->state, sizeof(*state));
413 if (state->crtc)
414 drm_connector_get(connector);
415 state->commit = NULL;
416
417 if (state->hdr_output_metadata)
418 drm_property_blob_get(state->hdr_output_metadata);
419
420 /* Don't copy over a writeback job, they are used only once */
421 state->writeback_job = NULL;
422}
423EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
424
425/**
426 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
427 * @connector: drm connector
428 *
429 * Default connector state duplicate hook for drivers which don't have their own
430 * subclassed connector state structure.
431 */
432struct drm_connector_state *
433drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
434{
435 struct drm_connector_state *state;
436
437 if (WARN_ON(!connector->state))
438 return NULL;
439
440 state = kmalloc(sizeof(*state), GFP_KERNEL);
441 if (state)
442 __drm_atomic_helper_connector_duplicate_state(connector, state);
443
444 return state;
445}
446EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
447
448/**
449 * __drm_atomic_helper_connector_destroy_state - release connector state
450 * @state: connector state object to release
451 *
452 * Releases all resources stored in the connector state without actually
453 * freeing the memory of the connector state. This is useful for drivers that
454 * subclass the connector state.
455 */
456void
457__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
458{
459 if (state->crtc)
460 drm_connector_put(state->connector);
461
462 if (state->commit)
463 drm_crtc_commit_put(state->commit);
464
465 if (state->writeback_job)
466 drm_writeback_cleanup_job(state->writeback_job);
467
468 drm_property_blob_put(state->hdr_output_metadata);
469}
470EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
471
472/**
473 * drm_atomic_helper_connector_destroy_state - default state destroy hook
474 * @connector: drm connector
475 * @state: connector state object to release
476 *
477 * Default connector state destroy hook for drivers which don't have their own
478 * subclassed connector state structure.
479 */
480void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
481 struct drm_connector_state *state)
482{
483 __drm_atomic_helper_connector_destroy_state(state);
484 kfree(state);
485}
486EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
487
488/**
489 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
490 * @obj: CRTC object
491 * @state: new private object state
492 *
493 * Copies atomic state from a private objects's current state and resets inferred values.
494 * This is useful for drivers that subclass the private state.
495 */
496void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
497 struct drm_private_state *state)
498{
499 memcpy(state, obj->state, sizeof(*state));
500}
501EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);