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