Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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_bridge.h>
30#include <drm/drm_connector.h>
31#include <drm/drm_crtc.h>
32#include <drm/drm_device.h>
33#include <drm/drm_plane.h>
34#include <drm/drm_print.h>
35#include <drm/drm_writeback.h>
36
37#include <linux/slab.h>
38#include <linux/dma-fence.h>
39
40/**
41 * DOC: atomic state reset and initialization
42 *
43 * Both the drm core and the atomic helpers assume that there is always the full
44 * and correct atomic software state for all connectors, CRTCs and planes
45 * available. Which is a bit a problem on driver load and also after system
46 * suspend. One way to solve this is to have a hardware state read-out
47 * infrastructure which reconstructs the full software state (e.g. the i915
48 * driver).
49 *
50 * The simpler solution is to just reset the software state to everything off,
51 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
52 * the atomic helpers provide default reset implementations for all hooks.
53 *
54 * On the upside the precise state tracking of atomic simplifies system suspend
55 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
56 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
57 * For other drivers the building blocks are split out, see the documentation
58 * for these functions.
59 */
60
61/**
62 * __drm_atomic_helper_crtc_state_reset - reset the CRTC state
63 * @crtc_state: atomic CRTC state, must not be NULL
64 * @crtc: CRTC object, must not be NULL
65 *
66 * Initializes the newly allocated @crtc_state with default
67 * values. This is useful for drivers that subclass the CRTC state.
68 */
69void
70__drm_atomic_helper_crtc_state_reset(struct drm_crtc_state *crtc_state,
71 struct drm_crtc *crtc)
72{
73 crtc_state->crtc = crtc;
74}
75EXPORT_SYMBOL(__drm_atomic_helper_crtc_state_reset);
76
77/**
78 * __drm_atomic_helper_crtc_reset - reset state on CRTC
79 * @crtc: drm CRTC
80 * @crtc_state: CRTC state to assign
81 *
82 * Initializes the newly allocated @crtc_state and assigns it to
83 * the &drm_crtc->state pointer of @crtc, usually required when
84 * initializing the drivers or when called from the &drm_crtc_funcs.reset
85 * hook.
86 *
87 * This is useful for drivers that subclass the CRTC state.
88 */
89void
90__drm_atomic_helper_crtc_reset(struct drm_crtc *crtc,
91 struct drm_crtc_state *crtc_state)
92{
93 if (crtc_state)
94 __drm_atomic_helper_crtc_state_reset(crtc_state, crtc);
95
96 crtc->state = crtc_state;
97}
98EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset);
99
100/**
101 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
102 * @crtc: drm CRTC
103 *
104 * Resets the atomic state for @crtc by freeing the state pointer (which might
105 * be NULL, e.g. at driver load time) and allocating a new empty state object.
106 */
107void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
108{
109 struct drm_crtc_state *crtc_state =
110 kzalloc(sizeof(*crtc->state), GFP_KERNEL);
111
112 if (crtc->state)
113 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
114
115 __drm_atomic_helper_crtc_reset(crtc, crtc_state);
116}
117EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
118
119/**
120 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
121 * @crtc: CRTC object
122 * @state: atomic CRTC state
123 *
124 * Copies atomic state from a CRTC's current state and resets inferred values.
125 * This is useful for drivers that subclass the CRTC state.
126 */
127void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
128 struct drm_crtc_state *state)
129{
130 memcpy(state, crtc->state, sizeof(*state));
131
132 if (state->mode_blob)
133 drm_property_blob_get(state->mode_blob);
134 if (state->degamma_lut)
135 drm_property_blob_get(state->degamma_lut);
136 if (state->ctm)
137 drm_property_blob_get(state->ctm);
138 if (state->gamma_lut)
139 drm_property_blob_get(state->gamma_lut);
140 state->mode_changed = false;
141 state->active_changed = false;
142 state->planes_changed = false;
143 state->connectors_changed = false;
144 state->color_mgmt_changed = false;
145 state->zpos_changed = false;
146 state->commit = NULL;
147 state->event = NULL;
148 state->async_flip = false;
149
150 /* Self refresh should be canceled when a new update is available */
151 state->active = drm_atomic_crtc_effectively_active(state);
152 state->self_refresh_active = false;
153}
154EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
155
156/**
157 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
158 * @crtc: drm CRTC
159 *
160 * Default CRTC state duplicate hook for drivers which don't have their own
161 * subclassed CRTC state structure.
162 */
163struct drm_crtc_state *
164drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
165{
166 struct drm_crtc_state *state;
167
168 if (WARN_ON(!crtc->state))
169 return NULL;
170
171 state = kmalloc(sizeof(*state), GFP_KERNEL);
172 if (state)
173 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
174
175 return state;
176}
177EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
178
179/**
180 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
181 * @state: CRTC state object to release
182 *
183 * Releases all resources stored in the CRTC state without actually freeing
184 * the memory of the CRTC state. This is useful for drivers that subclass the
185 * CRTC state.
186 */
187void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
188{
189 if (state->commit) {
190 /*
191 * In the event that a non-blocking commit returns
192 * -ERESTARTSYS before the commit_tail work is queued, we will
193 * have an extra reference to the commit object. Release it, if
194 * the event has not been consumed by the worker.
195 *
196 * state->event may be freed, so we can't directly look at
197 * state->event->base.completion.
198 */
199 if (state->event && state->commit->abort_completion)
200 drm_crtc_commit_put(state->commit);
201
202 kfree(state->commit->event);
203 state->commit->event = NULL;
204
205 drm_crtc_commit_put(state->commit);
206 }
207
208 drm_property_blob_put(state->mode_blob);
209 drm_property_blob_put(state->degamma_lut);
210 drm_property_blob_put(state->ctm);
211 drm_property_blob_put(state->gamma_lut);
212}
213EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
214
215/**
216 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
217 * @crtc: drm CRTC
218 * @state: CRTC state object to release
219 *
220 * Default CRTC state destroy hook for drivers which don't have their own
221 * subclassed CRTC state structure.
222 */
223void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
224 struct drm_crtc_state *state)
225{
226 __drm_atomic_helper_crtc_destroy_state(state);
227 kfree(state);
228}
229EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
230
231/**
232 * __drm_atomic_helper_plane_state_reset - resets plane state to default values
233 * @plane_state: atomic plane state, must not be NULL
234 * @plane: plane object, must not be NULL
235 *
236 * Initializes the newly allocated @plane_state with default
237 * values. This is useful for drivers that subclass the CRTC state.
238 */
239void __drm_atomic_helper_plane_state_reset(struct drm_plane_state *plane_state,
240 struct drm_plane *plane)
241{
242 plane_state->plane = plane;
243 plane_state->rotation = DRM_MODE_ROTATE_0;
244
245 plane_state->alpha = DRM_BLEND_ALPHA_OPAQUE;
246 plane_state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
247}
248EXPORT_SYMBOL(__drm_atomic_helper_plane_state_reset);
249
250/**
251 * __drm_atomic_helper_plane_reset - reset state on plane
252 * @plane: drm plane
253 * @plane_state: plane state to assign
254 *
255 * Initializes the newly allocated @plane_state and assigns it to
256 * the &drm_crtc->state pointer of @plane, usually required when
257 * initializing the drivers or when called from the &drm_plane_funcs.reset
258 * hook.
259 *
260 * This is useful for drivers that subclass the plane state.
261 */
262void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
263 struct drm_plane_state *plane_state)
264{
265 if (plane_state)
266 __drm_atomic_helper_plane_state_reset(plane_state, plane);
267
268 plane->state = plane_state;
269}
270EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
271
272/**
273 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
274 * @plane: drm plane
275 *
276 * Resets the atomic state for @plane by freeing the state pointer (which might
277 * be NULL, e.g. at driver load time) and allocating a new empty state object.
278 */
279void drm_atomic_helper_plane_reset(struct drm_plane *plane)
280{
281 if (plane->state)
282 __drm_atomic_helper_plane_destroy_state(plane->state);
283
284 kfree(plane->state);
285 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
286 if (plane->state)
287 __drm_atomic_helper_plane_reset(plane, plane->state);
288}
289EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
290
291/**
292 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
293 * @plane: plane object
294 * @state: atomic plane state
295 *
296 * Copies atomic state from a plane's current state. This is useful for
297 * drivers that subclass the plane state.
298 */
299void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
300 struct drm_plane_state *state)
301{
302 memcpy(state, plane->state, sizeof(*state));
303
304 if (state->fb)
305 drm_framebuffer_get(state->fb);
306
307 state->fence = NULL;
308 state->commit = NULL;
309 state->fb_damage_clips = NULL;
310}
311EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
312
313/**
314 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
315 * @plane: drm plane
316 *
317 * Default plane state duplicate hook for drivers which don't have their own
318 * subclassed plane state structure.
319 */
320struct drm_plane_state *
321drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
322{
323 struct drm_plane_state *state;
324
325 if (WARN_ON(!plane->state))
326 return NULL;
327
328 state = kmalloc(sizeof(*state), GFP_KERNEL);
329 if (state)
330 __drm_atomic_helper_plane_duplicate_state(plane, state);
331
332 return state;
333}
334EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
335
336/**
337 * __drm_atomic_helper_plane_destroy_state - release plane state
338 * @state: plane state object to release
339 *
340 * Releases all resources stored in the plane state without actually freeing
341 * the memory of the plane state. This is useful for drivers that subclass the
342 * plane state.
343 */
344void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
345{
346 if (state->fb)
347 drm_framebuffer_put(state->fb);
348
349 if (state->fence)
350 dma_fence_put(state->fence);
351
352 if (state->commit)
353 drm_crtc_commit_put(state->commit);
354
355 drm_property_blob_put(state->fb_damage_clips);
356}
357EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
358
359/**
360 * drm_atomic_helper_plane_destroy_state - default state destroy hook
361 * @plane: drm plane
362 * @state: plane state object to release
363 *
364 * Default plane state destroy hook for drivers which don't have their own
365 * subclassed plane state structure.
366 */
367void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
368 struct drm_plane_state *state)
369{
370 __drm_atomic_helper_plane_destroy_state(state);
371 kfree(state);
372}
373EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
374
375/**
376 * __drm_atomic_helper_connector_state_reset - reset the connector state
377 * @conn_state: atomic connector state, must not be NULL
378 * @connector: connectotr object, must not be NULL
379 *
380 * Initializes the newly allocated @conn_state with default
381 * values. This is useful for drivers that subclass the connector state.
382 */
383void
384__drm_atomic_helper_connector_state_reset(struct drm_connector_state *conn_state,
385 struct drm_connector *connector)
386{
387 conn_state->connector = connector;
388}
389EXPORT_SYMBOL(__drm_atomic_helper_connector_state_reset);
390
391/**
392 * __drm_atomic_helper_connector_reset - reset state on connector
393 * @connector: drm connector
394 * @conn_state: connector state to assign
395 *
396 * Initializes the newly allocated @conn_state and assigns it to
397 * the &drm_connector->state pointer of @connector, usually required when
398 * initializing the drivers or when called from the &drm_connector_funcs.reset
399 * hook.
400 *
401 * This is useful for drivers that subclass the connector state.
402 */
403void
404__drm_atomic_helper_connector_reset(struct drm_connector *connector,
405 struct drm_connector_state *conn_state)
406{
407 if (conn_state)
408 __drm_atomic_helper_connector_state_reset(conn_state, connector);
409
410 connector->state = conn_state;
411}
412EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
413
414/**
415 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
416 * @connector: drm connector
417 *
418 * Resets the atomic state for @connector by freeing the state pointer (which
419 * might be NULL, e.g. at driver load time) and allocating a new empty state
420 * object.
421 */
422void drm_atomic_helper_connector_reset(struct drm_connector *connector)
423{
424 struct drm_connector_state *conn_state =
425 kzalloc(sizeof(*conn_state), GFP_KERNEL);
426
427 if (connector->state)
428 __drm_atomic_helper_connector_destroy_state(connector->state);
429
430 kfree(connector->state);
431 __drm_atomic_helper_connector_reset(connector, conn_state);
432}
433EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
434
435/**
436 * drm_atomic_helper_connector_tv_reset - Resets TV connector properties
437 * @connector: DRM connector
438 *
439 * Resets the TV-related properties attached to a connector.
440 */
441void drm_atomic_helper_connector_tv_reset(struct drm_connector *connector)
442{
443 struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
444 struct drm_connector_state *state = connector->state;
445
446 state->tv.margins.left = cmdline->tv_margins.left;
447 state->tv.margins.right = cmdline->tv_margins.right;
448 state->tv.margins.top = cmdline->tv_margins.top;
449 state->tv.margins.bottom = cmdline->tv_margins.bottom;
450}
451EXPORT_SYMBOL(drm_atomic_helper_connector_tv_reset);
452
453/**
454 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
455 * @connector: connector object
456 * @state: atomic connector state
457 *
458 * Copies atomic state from a connector's current state. This is useful for
459 * drivers that subclass the connector state.
460 */
461void
462__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
463 struct drm_connector_state *state)
464{
465 memcpy(state, connector->state, sizeof(*state));
466 if (state->crtc)
467 drm_connector_get(connector);
468 state->commit = NULL;
469
470 if (state->hdr_output_metadata)
471 drm_property_blob_get(state->hdr_output_metadata);
472
473 /* Don't copy over a writeback job, they are used only once */
474 state->writeback_job = NULL;
475}
476EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
477
478/**
479 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
480 * @connector: drm connector
481 *
482 * Default connector state duplicate hook for drivers which don't have their own
483 * subclassed connector state structure.
484 */
485struct drm_connector_state *
486drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
487{
488 struct drm_connector_state *state;
489
490 if (WARN_ON(!connector->state))
491 return NULL;
492
493 state = kmalloc(sizeof(*state), GFP_KERNEL);
494 if (state)
495 __drm_atomic_helper_connector_duplicate_state(connector, state);
496
497 return state;
498}
499EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
500
501/**
502 * __drm_atomic_helper_connector_destroy_state - release connector state
503 * @state: connector state object to release
504 *
505 * Releases all resources stored in the connector state without actually
506 * freeing the memory of the connector state. This is useful for drivers that
507 * subclass the connector state.
508 */
509void
510__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
511{
512 if (state->crtc)
513 drm_connector_put(state->connector);
514
515 if (state->commit)
516 drm_crtc_commit_put(state->commit);
517
518 if (state->writeback_job)
519 drm_writeback_cleanup_job(state->writeback_job);
520
521 drm_property_blob_put(state->hdr_output_metadata);
522}
523EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
524
525/**
526 * drm_atomic_helper_connector_destroy_state - default state destroy hook
527 * @connector: drm connector
528 * @state: connector state object to release
529 *
530 * Default connector state destroy hook for drivers which don't have their own
531 * subclassed connector state structure.
532 */
533void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
534 struct drm_connector_state *state)
535{
536 __drm_atomic_helper_connector_destroy_state(state);
537 kfree(state);
538}
539EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
540
541/**
542 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
543 * @obj: CRTC object
544 * @state: new private object state
545 *
546 * Copies atomic state from a private objects's current state and resets inferred values.
547 * This is useful for drivers that subclass the private state.
548 */
549void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
550 struct drm_private_state *state)
551{
552 memcpy(state, obj->state, sizeof(*state));
553}
554EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);
555
556/**
557 * __drm_atomic_helper_bridge_duplicate_state() - Copy atomic bridge state
558 * @bridge: bridge object
559 * @state: atomic bridge state
560 *
561 * Copies atomic state from a bridge's current state and resets inferred values.
562 * This is useful for drivers that subclass the bridge state.
563 */
564void __drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge,
565 struct drm_bridge_state *state)
566{
567 __drm_atomic_helper_private_obj_duplicate_state(&bridge->base,
568 &state->base);
569 state->bridge = bridge;
570}
571EXPORT_SYMBOL(__drm_atomic_helper_bridge_duplicate_state);
572
573/**
574 * drm_atomic_helper_bridge_duplicate_state() - Duplicate a bridge state object
575 * @bridge: bridge object
576 *
577 * Allocates a new bridge state and initializes it with the current bridge
578 * state values. This helper is meant to be used as a bridge
579 * &drm_bridge_funcs.atomic_duplicate_state hook for bridges that don't
580 * subclass the bridge state.
581 */
582struct drm_bridge_state *
583drm_atomic_helper_bridge_duplicate_state(struct drm_bridge *bridge)
584{
585 struct drm_bridge_state *new;
586
587 if (WARN_ON(!bridge->base.state))
588 return NULL;
589
590 new = kzalloc(sizeof(*new), GFP_KERNEL);
591 if (new)
592 __drm_atomic_helper_bridge_duplicate_state(bridge, new);
593
594 return new;
595}
596EXPORT_SYMBOL(drm_atomic_helper_bridge_duplicate_state);
597
598/**
599 * drm_atomic_helper_bridge_destroy_state() - Destroy a bridge state object
600 * @bridge: the bridge this state refers to
601 * @state: bridge state to destroy
602 *
603 * Destroys a bridge state previously created by
604 * &drm_atomic_helper_bridge_reset() or
605 * &drm_atomic_helper_bridge_duplicate_state(). This helper is meant to be
606 * used as a bridge &drm_bridge_funcs.atomic_destroy_state hook for bridges
607 * that don't subclass the bridge state.
608 */
609void drm_atomic_helper_bridge_destroy_state(struct drm_bridge *bridge,
610 struct drm_bridge_state *state)
611{
612 kfree(state);
613}
614EXPORT_SYMBOL(drm_atomic_helper_bridge_destroy_state);
615
616/**
617 * __drm_atomic_helper_bridge_reset() - Initialize a bridge state to its
618 * default
619 * @bridge: the bridge this state refers to
620 * @state: bridge state to initialize
621 *
622 * Initializes the bridge state to default values. This is meant to be called
623 * by the bridge &drm_bridge_funcs.atomic_reset hook for bridges that subclass
624 * the bridge state.
625 */
626void __drm_atomic_helper_bridge_reset(struct drm_bridge *bridge,
627 struct drm_bridge_state *state)
628{
629 memset(state, 0, sizeof(*state));
630 state->bridge = bridge;
631}
632EXPORT_SYMBOL(__drm_atomic_helper_bridge_reset);
633
634/**
635 * drm_atomic_helper_bridge_reset() - Allocate and initialize a bridge state
636 * to its default
637 * @bridge: the bridge this state refers to
638 *
639 * Allocates the bridge state and initializes it to default values. This helper
640 * is meant to be used as a bridge &drm_bridge_funcs.atomic_reset hook for
641 * bridges that don't subclass the bridge state.
642 */
643struct drm_bridge_state *
644drm_atomic_helper_bridge_reset(struct drm_bridge *bridge)
645{
646 struct drm_bridge_state *bridge_state;
647
648 bridge_state = kzalloc(sizeof(*bridge_state), GFP_KERNEL);
649 if (!bridge_state)
650 return ERR_PTR(-ENOMEM);
651
652 __drm_atomic_helper_bridge_reset(bridge, bridge_state);
653 return bridge_state;
654}
655EXPORT_SYMBOL(drm_atomic_helper_bridge_reset);