blob: 3ba996069d69edae05e5013075a67f9615617a64 [file] [log] [blame]
Daniel Vetter9ef8a9d2018-10-04 22:24:28 +02001/*
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_state_helper.h>
28#include <drm/drm_crtc.h>
29#include <drm/drm_plane.h>
30#include <drm/drm_connector.h>
31#include <drm/drm_atomic.h>
32#include <drm/drm_device.h>
33
34#include <linux/slab.h>
35#include <linux/dma-fence.h>
36
37/**
38 * DOC: atomic state reset and initialization
39 *
40 * Both the drm core and the atomic helpers assume that there is always the full
41 * and correct atomic software state for all connectors, CRTCs and planes
42 * available. Which is a bit a problem on driver load and also after system
43 * suspend. One way to solve this is to have a hardware state read-out
44 * infrastructure which reconstructs the full software state (e.g. the i915
45 * driver).
46 *
47 * The simpler solution is to just reset the software state to everything off,
48 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
49 * the atomic helpers provide default reset implementations for all hooks.
50 *
51 * On the upside the precise state tracking of atomic simplifies system suspend
52 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
53 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
54 * For other drivers the building blocks are split out, see the documentation
55 * for these functions.
56 */
57
58/**
59 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
60 * @crtc: drm CRTC
61 *
62 * Resets the atomic state for @crtc by freeing the state pointer (which might
63 * be NULL, e.g. at driver load time) and allocating a new empty state object.
64 */
65void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
66{
67 if (crtc->state)
68 __drm_atomic_helper_crtc_destroy_state(crtc->state);
69
70 kfree(crtc->state);
71 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
72
73 if (crtc->state)
74 crtc->state->crtc = crtc;
75}
76EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
77
78/**
79 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
80 * @crtc: CRTC object
81 * @state: atomic CRTC state
82 *
83 * Copies atomic state from a CRTC's current state and resets inferred values.
84 * This is useful for drivers that subclass the CRTC state.
85 */
86void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
87 struct drm_crtc_state *state)
88{
89 memcpy(state, crtc->state, sizeof(*state));
90
91 if (state->mode_blob)
92 drm_property_blob_get(state->mode_blob);
93 if (state->degamma_lut)
94 drm_property_blob_get(state->degamma_lut);
95 if (state->ctm)
96 drm_property_blob_get(state->ctm);
97 if (state->gamma_lut)
98 drm_property_blob_get(state->gamma_lut);
99 state->mode_changed = false;
100 state->active_changed = false;
101 state->planes_changed = false;
102 state->connectors_changed = false;
103 state->color_mgmt_changed = false;
104 state->zpos_changed = false;
105 state->commit = NULL;
106 state->event = NULL;
107 state->pageflip_flags = 0;
108}
109EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
110
111/**
112 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
113 * @crtc: drm CRTC
114 *
115 * Default CRTC state duplicate hook for drivers which don't have their own
116 * subclassed CRTC state structure.
117 */
118struct drm_crtc_state *
119drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
120{
121 struct drm_crtc_state *state;
122
123 if (WARN_ON(!crtc->state))
124 return NULL;
125
126 state = kmalloc(sizeof(*state), GFP_KERNEL);
127 if (state)
128 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
129
130 return state;
131}
132EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
133
134/**
135 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
136 * @state: CRTC state object to release
137 *
138 * Releases all resources stored in the CRTC state without actually freeing
139 * the memory of the CRTC state. This is useful for drivers that subclass the
140 * CRTC state.
141 */
142void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
143{
144 if (state->commit) {
145 /*
146 * In the event that a non-blocking commit returns
147 * -ERESTARTSYS before the commit_tail work is queued, we will
148 * have an extra reference to the commit object. Release it, if
149 * the event has not been consumed by the worker.
150 *
151 * state->event may be freed, so we can't directly look at
152 * state->event->base.completion.
153 */
154 if (state->event && state->commit->abort_completion)
155 drm_crtc_commit_put(state->commit);
156
157 kfree(state->commit->event);
158 state->commit->event = NULL;
159
160 drm_crtc_commit_put(state->commit);
161 }
162
163 drm_property_blob_put(state->mode_blob);
164 drm_property_blob_put(state->degamma_lut);
165 drm_property_blob_put(state->ctm);
166 drm_property_blob_put(state->gamma_lut);
167}
168EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
169
170/**
171 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
172 * @crtc: drm CRTC
173 * @state: CRTC state object to release
174 *
175 * Default CRTC state destroy hook for drivers which don't have their own
176 * subclassed CRTC state structure.
177 */
178void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
179 struct drm_crtc_state *state)
180{
181 __drm_atomic_helper_crtc_destroy_state(state);
182 kfree(state);
183}
184EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
185
186/**
187 * __drm_atomic_helper_plane_reset - resets planes state to default values
188 * @plane: plane object, must not be NULL
189 * @state: atomic plane state, must not be NULL
190 *
191 * Initializes plane state to default. This is useful for drivers that subclass
192 * the plane state.
193 */
194void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
195 struct drm_plane_state *state)
196{
197 state->plane = plane;
198 state->rotation = DRM_MODE_ROTATE_0;
199
200 state->alpha = DRM_BLEND_ALPHA_OPAQUE;
201 state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
202
203 plane->state = state;
204}
205EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
206
207/**
208 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
209 * @plane: drm plane
210 *
211 * Resets the atomic state for @plane by freeing the state pointer (which might
212 * be NULL, e.g. at driver load time) and allocating a new empty state object.
213 */
214void drm_atomic_helper_plane_reset(struct drm_plane *plane)
215{
216 if (plane->state)
217 __drm_atomic_helper_plane_destroy_state(plane->state);
218
219 kfree(plane->state);
220 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
221 if (plane->state)
222 __drm_atomic_helper_plane_reset(plane, plane->state);
223}
224EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
225
226/**
227 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
228 * @plane: plane object
229 * @state: atomic plane state
230 *
231 * Copies atomic state from a plane's current state. This is useful for
232 * drivers that subclass the plane state.
233 */
234void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
235 struct drm_plane_state *state)
236{
237 memcpy(state, plane->state, sizeof(*state));
238
239 if (state->fb)
240 drm_framebuffer_get(state->fb);
241
242 state->fence = NULL;
243 state->commit = NULL;
244}
245EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
246
247/**
248 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
249 * @plane: drm plane
250 *
251 * Default plane state duplicate hook for drivers which don't have their own
252 * subclassed plane state structure.
253 */
254struct drm_plane_state *
255drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
256{
257 struct drm_plane_state *state;
258
259 if (WARN_ON(!plane->state))
260 return NULL;
261
262 state = kmalloc(sizeof(*state), GFP_KERNEL);
263 if (state)
264 __drm_atomic_helper_plane_duplicate_state(plane, state);
265
266 return state;
267}
268EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
269
270/**
271 * __drm_atomic_helper_plane_destroy_state - release plane state
272 * @state: plane state object to release
273 *
274 * Releases all resources stored in the plane state without actually freeing
275 * the memory of the plane state. This is useful for drivers that subclass the
276 * plane state.
277 */
278void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
279{
280 if (state->fb)
281 drm_framebuffer_put(state->fb);
282
283 if (state->fence)
284 dma_fence_put(state->fence);
285
286 if (state->commit)
287 drm_crtc_commit_put(state->commit);
288}
289EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
290
291/**
292 * drm_atomic_helper_plane_destroy_state - default state destroy hook
293 * @plane: drm plane
294 * @state: plane state object to release
295 *
296 * Default plane state destroy hook for drivers which don't have their own
297 * subclassed plane state structure.
298 */
299void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
300 struct drm_plane_state *state)
301{
302 __drm_atomic_helper_plane_destroy_state(state);
303 kfree(state);
304}
305EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
306
307/**
308 * __drm_atomic_helper_connector_reset - reset state on connector
309 * @connector: drm connector
310 * @conn_state: connector state to assign
311 *
312 * Initializes the newly allocated @conn_state and assigns it to
313 * the &drm_conector->state pointer of @connector, usually required when
314 * initializing the drivers or when called from the &drm_connector_funcs.reset
315 * hook.
316 *
317 * This is useful for drivers that subclass the connector state.
318 */
319void
320__drm_atomic_helper_connector_reset(struct drm_connector *connector,
321 struct drm_connector_state *conn_state)
322{
323 if (conn_state)
324 conn_state->connector = connector;
325
326 connector->state = conn_state;
327}
328EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
329
330/**
331 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
332 * @connector: drm connector
333 *
334 * Resets the atomic state for @connector by freeing the state pointer (which
335 * might be NULL, e.g. at driver load time) and allocating a new empty state
336 * object.
337 */
338void drm_atomic_helper_connector_reset(struct drm_connector *connector)
339{
340 struct drm_connector_state *conn_state =
341 kzalloc(sizeof(*conn_state), GFP_KERNEL);
342
343 if (connector->state)
344 __drm_atomic_helper_connector_destroy_state(connector->state);
345
346 kfree(connector->state);
347 __drm_atomic_helper_connector_reset(connector, conn_state);
348}
349EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
350
351/**
352 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
353 * @connector: connector object
354 * @state: atomic connector state
355 *
356 * Copies atomic state from a connector's current state. This is useful for
357 * drivers that subclass the connector state.
358 */
359void
360__drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
361 struct drm_connector_state *state)
362{
363 memcpy(state, connector->state, sizeof(*state));
364 if (state->crtc)
365 drm_connector_get(connector);
366 state->commit = NULL;
367
368 /* Don't copy over a writeback job, they are used only once */
369 state->writeback_job = NULL;
370}
371EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
372
373/**
374 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
375 * @connector: drm connector
376 *
377 * Default connector state duplicate hook for drivers which don't have their own
378 * subclassed connector state structure.
379 */
380struct drm_connector_state *
381drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
382{
383 struct drm_connector_state *state;
384
385 if (WARN_ON(!connector->state))
386 return NULL;
387
388 state = kmalloc(sizeof(*state), GFP_KERNEL);
389 if (state)
390 __drm_atomic_helper_connector_duplicate_state(connector, state);
391
392 return state;
393}
394EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
395
396/**
397 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
398 * @dev: DRM device
399 * @ctx: lock acquisition context
400 *
401 * Makes a copy of the current atomic state by looping over all objects and
402 * duplicating their respective states. This is used for example by suspend/
403 * resume support code to save the state prior to suspend such that it can
404 * be restored upon resume.
405 *
406 * Note that this treats atomic state as persistent between save and restore.
407 * Drivers must make sure that this is possible and won't result in confusion
408 * or erroneous behaviour.
409 *
410 * Note that if callers haven't already acquired all modeset locks this might
411 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
412 *
413 * Returns:
414 * A pointer to the copy of the atomic state object on success or an
415 * ERR_PTR()-encoded error code on failure.
416 *
417 * See also:
418 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
419 */
420struct drm_atomic_state *
421drm_atomic_helper_duplicate_state(struct drm_device *dev,
422 struct drm_modeset_acquire_ctx *ctx)
423{
424 struct drm_atomic_state *state;
425 struct drm_connector *conn;
426 struct drm_connector_list_iter conn_iter;
427 struct drm_plane *plane;
428 struct drm_crtc *crtc;
429 int err = 0;
430
431 state = drm_atomic_state_alloc(dev);
432 if (!state)
433 return ERR_PTR(-ENOMEM);
434
435 state->acquire_ctx = ctx;
436
437 drm_for_each_crtc(crtc, dev) {
438 struct drm_crtc_state *crtc_state;
439
440 crtc_state = drm_atomic_get_crtc_state(state, crtc);
441 if (IS_ERR(crtc_state)) {
442 err = PTR_ERR(crtc_state);
443 goto free;
444 }
445 }
446
447 drm_for_each_plane(plane, dev) {
448 struct drm_plane_state *plane_state;
449
450 plane_state = drm_atomic_get_plane_state(state, plane);
451 if (IS_ERR(plane_state)) {
452 err = PTR_ERR(plane_state);
453 goto free;
454 }
455 }
456
457 drm_connector_list_iter_begin(dev, &conn_iter);
458 drm_for_each_connector_iter(conn, &conn_iter) {
459 struct drm_connector_state *conn_state;
460
461 conn_state = drm_atomic_get_connector_state(state, conn);
462 if (IS_ERR(conn_state)) {
463 err = PTR_ERR(conn_state);
464 drm_connector_list_iter_end(&conn_iter);
465 goto free;
466 }
467 }
468 drm_connector_list_iter_end(&conn_iter);
469
470 /* clear the acquire context so that it isn't accidentally reused */
471 state->acquire_ctx = NULL;
472
473free:
474 if (err < 0) {
475 drm_atomic_state_put(state);
476 state = ERR_PTR(err);
477 }
478
479 return state;
480}
481EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
482
483/**
484 * __drm_atomic_helper_connector_destroy_state - release connector state
485 * @state: connector state object to release
486 *
487 * Releases all resources stored in the connector state without actually
488 * freeing the memory of the connector state. This is useful for drivers that
489 * subclass the connector state.
490 */
491void
492__drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
493{
494 if (state->crtc)
495 drm_connector_put(state->connector);
496
497 if (state->commit)
498 drm_crtc_commit_put(state->commit);
499}
500EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
501
502/**
503 * drm_atomic_helper_connector_destroy_state - default state destroy hook
504 * @connector: drm connector
505 * @state: connector state object to release
506 *
507 * Default connector state destroy hook for drivers which don't have their own
508 * subclassed connector state structure.
509 */
510void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
511 struct drm_connector_state *state)
512{
513 __drm_atomic_helper_connector_destroy_state(state);
514 kfree(state);
515}
516EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
517
518/**
519 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
520 * @crtc: CRTC object
521 * @red: red correction table
522 * @green: green correction table
523 * @blue: green correction table
524 * @size: size of the tables
525 * @ctx: lock acquire context
526 *
527 * Implements support for legacy gamma correction table for drivers
528 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
529 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
530 * how the atomic color management and gamma tables work.
531 */
532int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
533 u16 *red, u16 *green, u16 *blue,
534 uint32_t size,
535 struct drm_modeset_acquire_ctx *ctx)
536{
537 struct drm_device *dev = crtc->dev;
538 struct drm_atomic_state *state;
539 struct drm_crtc_state *crtc_state;
540 struct drm_property_blob *blob = NULL;
541 struct drm_color_lut *blob_data;
542 int i, ret = 0;
543 bool replaced;
544
545 state = drm_atomic_state_alloc(crtc->dev);
546 if (!state)
547 return -ENOMEM;
548
549 blob = drm_property_create_blob(dev,
550 sizeof(struct drm_color_lut) * size,
551 NULL);
552 if (IS_ERR(blob)) {
553 ret = PTR_ERR(blob);
554 blob = NULL;
555 goto fail;
556 }
557
558 /* Prepare GAMMA_LUT with the legacy values. */
559 blob_data = blob->data;
560 for (i = 0; i < size; i++) {
561 blob_data[i].red = red[i];
562 blob_data[i].green = green[i];
563 blob_data[i].blue = blue[i];
564 }
565
566 state->acquire_ctx = ctx;
567 crtc_state = drm_atomic_get_crtc_state(state, crtc);
568 if (IS_ERR(crtc_state)) {
569 ret = PTR_ERR(crtc_state);
570 goto fail;
571 }
572
573 /* Reset DEGAMMA_LUT and CTM properties. */
574 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
575 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
576 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
577 crtc_state->color_mgmt_changed |= replaced;
578
579 ret = drm_atomic_commit(state);
580
581fail:
582 drm_atomic_state_put(state);
583 drm_property_blob_put(blob);
584 return ret;
585}
586EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);
587
588/**
589 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
590 * @obj: CRTC object
591 * @state: new private object state
592 *
593 * Copies atomic state from a private objects's current state and resets inferred values.
594 * This is useful for drivers that subclass the private state.
595 */
596void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
597 struct drm_private_state *state)
598{
599 memcpy(state, obj->state, sizeof(*state));
600}
601EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);