2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_uapi.h>
32 #include <drm/drm_mode.h>
33 #include <drm/drm_print.h>
34 #include <drm/drm_writeback.h>
35 #include <linux/sync_file.h>
37 #include "drm_crtc_internal.h"
38 #include "drm_internal.h"
40 void __drm_crtc_commit_free(struct kref *kref)
42 struct drm_crtc_commit *commit =
43 container_of(kref, struct drm_crtc_commit, ref);
47 EXPORT_SYMBOL(__drm_crtc_commit_free);
50 * drm_atomic_state_default_release -
51 * release memory initialized by drm_atomic_state_init
52 * @state: atomic state
54 * Free all the memory allocated by drm_atomic_state_init.
55 * This should only be used by drivers which are still subclassing
56 * &drm_atomic_state and haven't switched to &drm_private_state yet.
58 void drm_atomic_state_default_release(struct drm_atomic_state *state)
60 kfree(state->connectors);
63 kfree(state->private_objs);
65 EXPORT_SYMBOL(drm_atomic_state_default_release);
68 * drm_atomic_state_init - init new atomic state
70 * @state: atomic state
72 * Default implementation for filling in a new atomic state.
73 * This should only be used by drivers which are still subclassing
74 * &drm_atomic_state and haven't switched to &drm_private_state yet.
77 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
79 kref_init(&state->ref);
81 /* TODO legacy paths should maybe do a better job about
82 * setting this appropriately?
84 state->allow_modeset = true;
86 state->crtcs = kcalloc(dev->mode_config.num_crtc,
87 sizeof(*state->crtcs), GFP_KERNEL);
90 state->planes = kcalloc(dev->mode_config.num_total_plane,
91 sizeof(*state->planes), GFP_KERNEL);
97 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
101 drm_atomic_state_default_release(state);
104 EXPORT_SYMBOL(drm_atomic_state_init);
107 * drm_atomic_state_alloc - allocate atomic state
110 * This allocates an empty atomic state to track updates.
112 struct drm_atomic_state *
113 drm_atomic_state_alloc(struct drm_device *dev)
115 struct drm_mode_config *config = &dev->mode_config;
117 if (!config->funcs->atomic_state_alloc) {
118 struct drm_atomic_state *state;
120 state = kzalloc(sizeof(*state), GFP_KERNEL);
123 if (drm_atomic_state_init(dev, state) < 0) {
130 return config->funcs->atomic_state_alloc(dev);
132 EXPORT_SYMBOL(drm_atomic_state_alloc);
135 * drm_atomic_state_default_clear - clear base atomic state
136 * @state: atomic state
138 * Default implementation for clearing atomic state.
139 * This should only be used by drivers which are still subclassing
140 * &drm_atomic_state and haven't switched to &drm_private_state yet.
142 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
144 struct drm_device *dev = state->dev;
145 struct drm_mode_config *config = &dev->mode_config;
148 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
150 for (i = 0; i < state->num_connector; i++) {
151 struct drm_connector *connector = state->connectors[i].ptr;
156 connector->funcs->atomic_destroy_state(connector,
157 state->connectors[i].state);
158 state->connectors[i].ptr = NULL;
159 state->connectors[i].state = NULL;
160 state->connectors[i].old_state = NULL;
161 state->connectors[i].new_state = NULL;
162 drm_connector_put(connector);
165 for (i = 0; i < config->num_crtc; i++) {
166 struct drm_crtc *crtc = state->crtcs[i].ptr;
171 crtc->funcs->atomic_destroy_state(crtc,
172 state->crtcs[i].state);
174 state->crtcs[i].ptr = NULL;
175 state->crtcs[i].state = NULL;
176 state->crtcs[i].old_state = NULL;
177 state->crtcs[i].new_state = NULL;
179 if (state->crtcs[i].commit) {
180 drm_crtc_commit_put(state->crtcs[i].commit);
181 state->crtcs[i].commit = NULL;
185 for (i = 0; i < config->num_total_plane; i++) {
186 struct drm_plane *plane = state->planes[i].ptr;
191 plane->funcs->atomic_destroy_state(plane,
192 state->planes[i].state);
193 state->planes[i].ptr = NULL;
194 state->planes[i].state = NULL;
195 state->planes[i].old_state = NULL;
196 state->planes[i].new_state = NULL;
199 for (i = 0; i < state->num_private_objs; i++) {
200 struct drm_private_obj *obj = state->private_objs[i].ptr;
202 obj->funcs->atomic_destroy_state(obj,
203 state->private_objs[i].state);
204 state->private_objs[i].ptr = NULL;
205 state->private_objs[i].state = NULL;
206 state->private_objs[i].old_state = NULL;
207 state->private_objs[i].new_state = NULL;
209 state->num_private_objs = 0;
211 if (state->fake_commit) {
212 drm_crtc_commit_put(state->fake_commit);
213 state->fake_commit = NULL;
216 EXPORT_SYMBOL(drm_atomic_state_default_clear);
219 * drm_atomic_state_clear - clear state object
220 * @state: atomic state
222 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
223 * all locks. So someone else could sneak in and change the current modeset
224 * configuration. Which means that all the state assembled in @state is no
225 * longer an atomic update to the current state, but to some arbitrary earlier
226 * state. Which could break assumptions the driver's
227 * &drm_mode_config_funcs.atomic_check likely relies on.
229 * Hence we must clear all cached state and completely start over, using this
232 void drm_atomic_state_clear(struct drm_atomic_state *state)
234 struct drm_device *dev = state->dev;
235 struct drm_mode_config *config = &dev->mode_config;
237 if (config->funcs->atomic_state_clear)
238 config->funcs->atomic_state_clear(state);
240 drm_atomic_state_default_clear(state);
242 EXPORT_SYMBOL(drm_atomic_state_clear);
245 * __drm_atomic_state_free - free all memory for an atomic state
246 * @ref: This atomic state to deallocate
248 * This frees all memory associated with an atomic state, including all the
249 * per-object state for planes, crtcs and connectors.
251 void __drm_atomic_state_free(struct kref *ref)
253 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
254 struct drm_mode_config *config = &state->dev->mode_config;
256 drm_atomic_state_clear(state);
258 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
260 if (config->funcs->atomic_state_free) {
261 config->funcs->atomic_state_free(state);
263 drm_atomic_state_default_release(state);
267 EXPORT_SYMBOL(__drm_atomic_state_free);
270 * drm_atomic_get_crtc_state - get crtc state
271 * @state: global atomic state object
272 * @crtc: crtc to get state object for
274 * This function returns the crtc state for the given crtc, allocating it if
275 * needed. It will also grab the relevant crtc lock to make sure that the state
280 * Either the allocated state or the error code encoded into the pointer. When
281 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
282 * entire atomic sequence must be restarted. All other errors are fatal.
284 struct drm_crtc_state *
285 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
286 struct drm_crtc *crtc)
288 int ret, index = drm_crtc_index(crtc);
289 struct drm_crtc_state *crtc_state;
291 WARN_ON(!state->acquire_ctx);
293 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
297 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
301 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
303 return ERR_PTR(-ENOMEM);
305 state->crtcs[index].state = crtc_state;
306 state->crtcs[index].old_state = crtc->state;
307 state->crtcs[index].new_state = crtc_state;
308 state->crtcs[index].ptr = crtc;
309 crtc_state->state = state;
311 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
312 crtc->base.id, crtc->name, crtc_state, state);
316 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
318 static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state,
319 const struct drm_crtc_state *new_crtc_state)
321 struct drm_crtc *crtc = new_crtc_state->crtc;
323 /* NOTE: we explicitly don't enforce constraints such as primary
324 * layer covering entire screen, since that is something we want
325 * to allow (on hw that supports it). For hw that does not, it
326 * should be checked in driver's crtc->atomic_check() vfunc.
328 * TODO: Add generic modeset state checks once we support those.
331 if (new_crtc_state->active && !new_crtc_state->enable) {
332 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
333 crtc->base.id, crtc->name);
337 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
338 * as this is a kernel-internal detail that userspace should never
339 * be able to trigger. */
340 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
341 WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) {
342 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
343 crtc->base.id, crtc->name);
347 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
348 WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) {
349 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
350 crtc->base.id, crtc->name);
355 * Reject event generation for when a CRTC is off and stays off.
356 * It wouldn't be hard to implement this, but userspace has a track
357 * record of happily burning through 100% cpu (or worse, crash) when the
358 * display pipe is suspended. To avoid all that fun just reject updates
359 * that ask for events since likely that indicates a bug in the
360 * compositor's drawing loop. This is consistent with the vblank IOCTL
361 * and legacy page_flip IOCTL which also reject service on a disabled
364 if (new_crtc_state->event &&
365 !new_crtc_state->active && !old_crtc_state->active) {
366 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
367 crtc->base.id, crtc->name);
374 static void drm_atomic_crtc_print_state(struct drm_printer *p,
375 const struct drm_crtc_state *state)
377 struct drm_crtc *crtc = state->crtc;
379 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
380 drm_printf(p, "\tenable=%d\n", state->enable);
381 drm_printf(p, "\tactive=%d\n", state->active);
382 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
383 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
384 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
385 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
386 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
387 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
388 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
389 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
390 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
392 if (crtc->funcs->atomic_print_state)
393 crtc->funcs->atomic_print_state(p, state);
396 static int drm_atomic_connector_check(struct drm_connector *connector,
397 struct drm_connector_state *state)
399 struct drm_crtc_state *crtc_state;
400 struct drm_writeback_job *writeback_job = state->writeback_job;
401 const struct drm_display_info *info = &connector->display_info;
403 state->max_bpc = info->bpc ? info->bpc : 8;
404 if (connector->max_bpc_property)
405 state->max_bpc = min(state->max_bpc, state->max_requested_bpc);
407 if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
410 if (writeback_job->fb && !state->crtc) {
411 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
412 connector->base.id, connector->name);
417 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
420 if (writeback_job->fb && !crtc_state->active) {
421 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
422 connector->base.id, connector->name,
423 state->crtc->base.id);
427 if (writeback_job->out_fence && !writeback_job->fb) {
428 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
429 connector->base.id, connector->name);
437 * drm_atomic_get_plane_state - get plane state
438 * @state: global atomic state object
439 * @plane: plane to get state object for
441 * This function returns the plane state for the given plane, allocating it if
442 * needed. It will also grab the relevant plane lock to make sure that the state
447 * Either the allocated state or the error code encoded into the pointer. When
448 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
449 * entire atomic sequence must be restarted. All other errors are fatal.
451 struct drm_plane_state *
452 drm_atomic_get_plane_state(struct drm_atomic_state *state,
453 struct drm_plane *plane)
455 int ret, index = drm_plane_index(plane);
456 struct drm_plane_state *plane_state;
458 WARN_ON(!state->acquire_ctx);
460 /* the legacy pointers should never be set */
462 WARN_ON(plane->old_fb);
463 WARN_ON(plane->crtc);
465 plane_state = drm_atomic_get_existing_plane_state(state, plane);
469 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
473 plane_state = plane->funcs->atomic_duplicate_state(plane);
475 return ERR_PTR(-ENOMEM);
477 state->planes[index].state = plane_state;
478 state->planes[index].ptr = plane;
479 state->planes[index].old_state = plane->state;
480 state->planes[index].new_state = plane_state;
481 plane_state->state = state;
483 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
484 plane->base.id, plane->name, plane_state, state);
486 if (plane_state->crtc) {
487 struct drm_crtc_state *crtc_state;
489 crtc_state = drm_atomic_get_crtc_state(state,
491 if (IS_ERR(crtc_state))
492 return ERR_CAST(crtc_state);
497 EXPORT_SYMBOL(drm_atomic_get_plane_state);
500 plane_switching_crtc(const struct drm_plane_state *old_plane_state,
501 const struct drm_plane_state *new_plane_state)
503 if (!old_plane_state->crtc || !new_plane_state->crtc)
506 if (old_plane_state->crtc == new_plane_state->crtc)
509 /* This could be refined, but currently there's no helper or driver code
510 * to implement direct switching of active planes nor userspace to take
511 * advantage of more direct plane switching without the intermediate
518 * drm_atomic_plane_check - check plane state
519 * @old_plane_state: old plane state to check
520 * @new_plane_state: new plane state to check
522 * Provides core sanity checks for plane state.
525 * Zero on success, error code on failure
527 static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state,
528 const struct drm_plane_state *new_plane_state)
530 struct drm_plane *plane = new_plane_state->plane;
531 struct drm_crtc *crtc = new_plane_state->crtc;
532 const struct drm_framebuffer *fb = new_plane_state->fb;
533 unsigned int fb_width, fb_height;
534 struct drm_mode_rect *clips;
538 /* either *both* CRTC and FB must be set, or neither */
540 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
541 plane->base.id, plane->name);
543 } else if (fb && !crtc) {
544 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
545 plane->base.id, plane->name);
549 /* if disabled, we don't care about the rest of the state: */
553 /* Check whether this plane is usable on this CRTC */
554 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
555 DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
556 crtc->base.id, crtc->name,
557 plane->base.id, plane->name);
561 /* Check whether this plane supports the fb pixel format. */
562 ret = drm_plane_check_pixel_format(plane, fb->format->format,
565 struct drm_format_name_buf format_name;
566 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n",
567 plane->base.id, plane->name,
568 drm_get_format_name(fb->format->format,
574 /* Give drivers some help against integer overflows */
575 if (new_plane_state->crtc_w > INT_MAX ||
576 new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w ||
577 new_plane_state->crtc_h > INT_MAX ||
578 new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) {
579 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
580 plane->base.id, plane->name,
581 new_plane_state->crtc_w, new_plane_state->crtc_h,
582 new_plane_state->crtc_x, new_plane_state->crtc_y);
586 fb_width = fb->width << 16;
587 fb_height = fb->height << 16;
589 /* Make sure source coordinates are inside the fb. */
590 if (new_plane_state->src_w > fb_width ||
591 new_plane_state->src_x > fb_width - new_plane_state->src_w ||
592 new_plane_state->src_h > fb_height ||
593 new_plane_state->src_y > fb_height - new_plane_state->src_h) {
594 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
595 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
596 plane->base.id, plane->name,
597 new_plane_state->src_w >> 16,
598 ((new_plane_state->src_w & 0xffff) * 15625) >> 10,
599 new_plane_state->src_h >> 16,
600 ((new_plane_state->src_h & 0xffff) * 15625) >> 10,
601 new_plane_state->src_x >> 16,
602 ((new_plane_state->src_x & 0xffff) * 15625) >> 10,
603 new_plane_state->src_y >> 16,
604 ((new_plane_state->src_y & 0xffff) * 15625) >> 10,
605 fb->width, fb->height);
609 clips = drm_plane_get_damage_clips(new_plane_state);
610 num_clips = drm_plane_get_damage_clips_count(new_plane_state);
612 /* Make sure damage clips are valid and inside the fb. */
613 while (num_clips > 0) {
614 if (clips->x1 >= clips->x2 ||
615 clips->y1 >= clips->y2 ||
618 clips->x2 > fb_width ||
619 clips->y2 > fb_height) {
620 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid damage clip %d %d %d %d\n",
621 plane->base.id, plane->name, clips->x1,
622 clips->y1, clips->x2, clips->y2);
629 if (plane_switching_crtc(old_plane_state, new_plane_state)) {
630 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
631 plane->base.id, plane->name);
638 static void drm_atomic_plane_print_state(struct drm_printer *p,
639 const struct drm_plane_state *state)
641 struct drm_plane *plane = state->plane;
642 struct drm_rect src = drm_plane_state_src(state);
643 struct drm_rect dest = drm_plane_state_dest(state);
645 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
646 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
647 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
649 drm_framebuffer_print_info(p, 2, state->fb);
650 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
651 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
652 drm_printf(p, "\trotation=%x\n", state->rotation);
653 drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
654 drm_printf(p, "\tcolor-encoding=%s\n",
655 drm_get_color_encoding_name(state->color_encoding));
656 drm_printf(p, "\tcolor-range=%s\n",
657 drm_get_color_range_name(state->color_range));
659 if (plane->funcs->atomic_print_state)
660 plane->funcs->atomic_print_state(p, state);
664 * DOC: handling driver private state
666 * Very often the DRM objects exposed to userspace in the atomic modeset api
667 * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
668 * underlying hardware. Especially for any kind of shared resources (e.g. shared
669 * clocks, scaler units, bandwidth and fifo limits shared among a group of
670 * planes or CRTCs, and so on) it makes sense to model these as independent
671 * objects. Drivers then need to do similar state tracking and commit ordering for
672 * such private (since not exposed to userpace) objects as the atomic core and
673 * helpers already provide for connectors, planes and CRTCs.
675 * To make this easier on drivers the atomic core provides some support to track
676 * driver private state objects using struct &drm_private_obj, with the
677 * associated state struct &drm_private_state.
679 * Similar to userspace-exposed objects, private state structures can be
680 * acquired by calling drm_atomic_get_private_obj_state(). Since this function
681 * does not take care of locking, drivers should wrap it for each type of
682 * private state object they have with the required call to drm_modeset_lock()
683 * for the corresponding &drm_modeset_lock.
685 * All private state structures contained in a &drm_atomic_state update can be
686 * iterated using for_each_oldnew_private_obj_in_state(),
687 * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
688 * Drivers are recommended to wrap these for each type of driver private state
689 * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
690 * least if they want to iterate over all objects of a given type.
692 * An earlier way to handle driver private state was by subclassing struct
693 * &drm_atomic_state. But since that encourages non-standard ways to implement
694 * the check/commit split atomic requires (by using e.g. "check and rollback or
695 * commit instead" of "duplicate state, check, then either commit or release
696 * duplicated state) it is deprecated in favour of using &drm_private_state.
700 * drm_atomic_private_obj_init - initialize private object
701 * @dev: DRM device this object will be attached to
702 * @obj: private object
703 * @state: initial private object state
704 * @funcs: pointer to the struct of function pointers that identify the object
707 * Initialize the private object, which can be embedded into any
708 * driver private object that needs its own atomic state.
711 drm_atomic_private_obj_init(struct drm_device *dev,
712 struct drm_private_obj *obj,
713 struct drm_private_state *state,
714 const struct drm_private_state_funcs *funcs)
716 memset(obj, 0, sizeof(*obj));
718 drm_modeset_lock_init(&obj->lock);
722 list_add_tail(&obj->head, &dev->mode_config.privobj_list);
724 EXPORT_SYMBOL(drm_atomic_private_obj_init);
727 * drm_atomic_private_obj_fini - finalize private object
728 * @obj: private object
730 * Finalize the private object.
733 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
735 list_del(&obj->head);
736 obj->funcs->atomic_destroy_state(obj, obj->state);
737 drm_modeset_lock_fini(&obj->lock);
739 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
742 * drm_atomic_get_private_obj_state - get private object state
743 * @state: global atomic state
744 * @obj: private object to get the state for
746 * This function returns the private object state for the given private object,
747 * allocating the state if needed. It will also grab the relevant private
748 * object lock to make sure that the state is consistent.
752 * Either the allocated state or the error code encoded into a pointer.
754 struct drm_private_state *
755 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
756 struct drm_private_obj *obj)
758 int index, num_objs, i, ret;
760 struct __drm_private_objs_state *arr;
761 struct drm_private_state *obj_state;
763 for (i = 0; i < state->num_private_objs; i++)
764 if (obj == state->private_objs[i].ptr)
765 return state->private_objs[i].state;
767 ret = drm_modeset_lock(&obj->lock, state->acquire_ctx);
771 num_objs = state->num_private_objs + 1;
772 size = sizeof(*state->private_objs) * num_objs;
773 arr = krealloc(state->private_objs, size, GFP_KERNEL);
775 return ERR_PTR(-ENOMEM);
777 state->private_objs = arr;
778 index = state->num_private_objs;
779 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
781 obj_state = obj->funcs->atomic_duplicate_state(obj);
783 return ERR_PTR(-ENOMEM);
785 state->private_objs[index].state = obj_state;
786 state->private_objs[index].old_state = obj->state;
787 state->private_objs[index].new_state = obj_state;
788 state->private_objs[index].ptr = obj;
789 obj_state->state = state;
791 state->num_private_objs = num_objs;
793 DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
794 obj, obj_state, state);
798 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
801 * drm_atomic_get_old_private_obj_state
802 * @state: global atomic state object
803 * @obj: private_obj to grab
805 * This function returns the old private object state for the given private_obj,
806 * or NULL if the private_obj is not part of the global atomic state.
808 struct drm_private_state *
809 drm_atomic_get_old_private_obj_state(struct drm_atomic_state *state,
810 struct drm_private_obj *obj)
814 for (i = 0; i < state->num_private_objs; i++)
815 if (obj == state->private_objs[i].ptr)
816 return state->private_objs[i].old_state;
820 EXPORT_SYMBOL(drm_atomic_get_old_private_obj_state);
823 * drm_atomic_get_new_private_obj_state
824 * @state: global atomic state object
825 * @obj: private_obj to grab
827 * This function returns the new private object state for the given private_obj,
828 * or NULL if the private_obj is not part of the global atomic state.
830 struct drm_private_state *
831 drm_atomic_get_new_private_obj_state(struct drm_atomic_state *state,
832 struct drm_private_obj *obj)
836 for (i = 0; i < state->num_private_objs; i++)
837 if (obj == state->private_objs[i].ptr)
838 return state->private_objs[i].new_state;
842 EXPORT_SYMBOL(drm_atomic_get_new_private_obj_state);
845 * drm_atomic_get_connector_state - get connector state
846 * @state: global atomic state object
847 * @connector: connector to get state object for
849 * This function returns the connector state for the given connector,
850 * allocating it if needed. It will also grab the relevant connector lock to
851 * make sure that the state is consistent.
855 * Either the allocated state or the error code encoded into the pointer. When
856 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
857 * entire atomic sequence must be restarted. All other errors are fatal.
859 struct drm_connector_state *
860 drm_atomic_get_connector_state(struct drm_atomic_state *state,
861 struct drm_connector *connector)
864 struct drm_mode_config *config = &connector->dev->mode_config;
865 struct drm_connector_state *connector_state;
867 WARN_ON(!state->acquire_ctx);
869 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
873 index = drm_connector_index(connector);
875 if (index >= state->num_connector) {
876 struct __drm_connnectors_state *c;
877 int alloc = max(index + 1, config->num_connector);
879 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
881 return ERR_PTR(-ENOMEM);
883 state->connectors = c;
884 memset(&state->connectors[state->num_connector], 0,
885 sizeof(*state->connectors) * (alloc - state->num_connector));
887 state->num_connector = alloc;
890 if (state->connectors[index].state)
891 return state->connectors[index].state;
893 connector_state = connector->funcs->atomic_duplicate_state(connector);
894 if (!connector_state)
895 return ERR_PTR(-ENOMEM);
897 drm_connector_get(connector);
898 state->connectors[index].state = connector_state;
899 state->connectors[index].old_state = connector->state;
900 state->connectors[index].new_state = connector_state;
901 state->connectors[index].ptr = connector;
902 connector_state->state = state;
904 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
905 connector->base.id, connector->name,
906 connector_state, state);
908 if (connector_state->crtc) {
909 struct drm_crtc_state *crtc_state;
911 crtc_state = drm_atomic_get_crtc_state(state,
912 connector_state->crtc);
913 if (IS_ERR(crtc_state))
914 return ERR_CAST(crtc_state);
917 return connector_state;
919 EXPORT_SYMBOL(drm_atomic_get_connector_state);
921 static void drm_atomic_connector_print_state(struct drm_printer *p,
922 const struct drm_connector_state *state)
924 struct drm_connector *connector = state->connector;
926 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
927 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
929 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
930 if (state->writeback_job && state->writeback_job->fb)
931 drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
933 if (connector->funcs->atomic_print_state)
934 connector->funcs->atomic_print_state(p, state);
938 * drm_atomic_add_affected_connectors - add connectors for crtc
939 * @state: atomic state
942 * This function walks the current configuration and adds all connectors
943 * currently using @crtc to the atomic configuration @state. Note that this
944 * function must acquire the connection mutex. This can potentially cause
945 * unneeded seralization if the update is just for the planes on one crtc. Hence
946 * drivers and helpers should only call this when really needed (e.g. when a
947 * full modeset needs to happen due to some change).
950 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
951 * then the w/w mutex code has detected a deadlock and the entire atomic
952 * sequence must be restarted. All other errors are fatal.
955 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
956 struct drm_crtc *crtc)
958 struct drm_mode_config *config = &state->dev->mode_config;
959 struct drm_connector *connector;
960 struct drm_connector_state *conn_state;
961 struct drm_connector_list_iter conn_iter;
962 struct drm_crtc_state *crtc_state;
965 crtc_state = drm_atomic_get_crtc_state(state, crtc);
966 if (IS_ERR(crtc_state))
967 return PTR_ERR(crtc_state);
969 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
973 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
974 crtc->base.id, crtc->name, state);
977 * Changed connectors are already in @state, so only need to look
978 * at the connector_mask in crtc_state.
980 drm_connector_list_iter_begin(state->dev, &conn_iter);
981 drm_for_each_connector_iter(connector, &conn_iter) {
982 if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
985 conn_state = drm_atomic_get_connector_state(state, connector);
986 if (IS_ERR(conn_state)) {
987 drm_connector_list_iter_end(&conn_iter);
988 return PTR_ERR(conn_state);
991 drm_connector_list_iter_end(&conn_iter);
995 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
998 * drm_atomic_add_affected_planes - add planes for crtc
999 * @state: atomic state
1002 * This function walks the current configuration and adds all planes
1003 * currently used by @crtc to the atomic configuration @state. This is useful
1004 * when an atomic commit also needs to check all currently enabled plane on
1005 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1006 * to avoid special code to force-enable all planes.
1008 * Since acquiring a plane state will always also acquire the w/w mutex of the
1009 * current CRTC for that plane (if there is any) adding all the plane states for
1010 * a CRTC will not reduce parallism of atomic updates.
1013 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1014 * then the w/w mutex code has detected a deadlock and the entire atomic
1015 * sequence must be restarted. All other errors are fatal.
1018 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1019 struct drm_crtc *crtc)
1021 const struct drm_crtc_state *old_crtc_state =
1022 drm_atomic_get_old_crtc_state(state, crtc);
1023 struct drm_plane *plane;
1025 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1027 DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
1028 crtc->base.id, crtc->name, state);
1030 drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) {
1031 struct drm_plane_state *plane_state =
1032 drm_atomic_get_plane_state(state, plane);
1034 if (IS_ERR(plane_state))
1035 return PTR_ERR(plane_state);
1039 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1042 * drm_atomic_check_only - check whether a given config would work
1043 * @state: atomic configuration to check
1045 * Note that this function can return -EDEADLK if the driver needed to acquire
1046 * more locks but encountered a deadlock. The caller must then do the usual w/w
1047 * backoff dance and restart. All other errors are fatal.
1050 * 0 on success, negative error code on failure.
1052 int drm_atomic_check_only(struct drm_atomic_state *state)
1054 struct drm_device *dev = state->dev;
1055 struct drm_mode_config *config = &dev->mode_config;
1056 struct drm_plane *plane;
1057 struct drm_plane_state *old_plane_state;
1058 struct drm_plane_state *new_plane_state;
1059 struct drm_crtc *crtc;
1060 struct drm_crtc_state *old_crtc_state;
1061 struct drm_crtc_state *new_crtc_state;
1062 struct drm_connector *conn;
1063 struct drm_connector_state *conn_state;
1066 DRM_DEBUG_ATOMIC("checking %p\n", state);
1068 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1069 ret = drm_atomic_plane_check(old_plane_state, new_plane_state);
1071 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1072 plane->base.id, plane->name);
1077 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1078 ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state);
1080 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1081 crtc->base.id, crtc->name);
1086 for_each_new_connector_in_state(state, conn, conn_state, i) {
1087 ret = drm_atomic_connector_check(conn, conn_state);
1089 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
1090 conn->base.id, conn->name);
1095 if (config->funcs->atomic_check) {
1096 ret = config->funcs->atomic_check(state->dev, state);
1099 DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
1105 if (!state->allow_modeset) {
1106 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1107 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
1108 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1109 crtc->base.id, crtc->name);
1117 EXPORT_SYMBOL(drm_atomic_check_only);
1120 * drm_atomic_commit - commit configuration atomically
1121 * @state: atomic configuration to check
1123 * Note that this function can return -EDEADLK if the driver needed to acquire
1124 * more locks but encountered a deadlock. The caller must then do the usual w/w
1125 * backoff dance and restart. All other errors are fatal.
1127 * This function will take its own reference on @state.
1128 * Callers should always release their reference with drm_atomic_state_put().
1131 * 0 on success, negative error code on failure.
1133 int drm_atomic_commit(struct drm_atomic_state *state)
1135 struct drm_mode_config *config = &state->dev->mode_config;
1138 ret = drm_atomic_check_only(state);
1142 DRM_DEBUG_ATOMIC("committing %p\n", state);
1144 return config->funcs->atomic_commit(state->dev, state, false);
1146 EXPORT_SYMBOL(drm_atomic_commit);
1149 * drm_atomic_nonblocking_commit - atomic nonblocking commit
1150 * @state: atomic configuration to check
1152 * Note that this function can return -EDEADLK if the driver needed to acquire
1153 * more locks but encountered a deadlock. The caller must then do the usual w/w
1154 * backoff dance and restart. All other errors are fatal.
1156 * This function will take its own reference on @state.
1157 * Callers should always release their reference with drm_atomic_state_put().
1160 * 0 on success, negative error code on failure.
1162 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1164 struct drm_mode_config *config = &state->dev->mode_config;
1167 ret = drm_atomic_check_only(state);
1171 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
1173 return config->funcs->atomic_commit(state->dev, state, true);
1175 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1177 void drm_atomic_print_state(const struct drm_atomic_state *state)
1179 struct drm_printer p = drm_info_printer(state->dev->dev);
1180 struct drm_plane *plane;
1181 struct drm_plane_state *plane_state;
1182 struct drm_crtc *crtc;
1183 struct drm_crtc_state *crtc_state;
1184 struct drm_connector *connector;
1185 struct drm_connector_state *connector_state;
1188 DRM_DEBUG_ATOMIC("checking %p\n", state);
1190 for_each_new_plane_in_state(state, plane, plane_state, i)
1191 drm_atomic_plane_print_state(&p, plane_state);
1193 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1194 drm_atomic_crtc_print_state(&p, crtc_state);
1196 for_each_new_connector_in_state(state, connector, connector_state, i)
1197 drm_atomic_connector_print_state(&p, connector_state);
1200 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1203 struct drm_mode_config *config = &dev->mode_config;
1204 struct drm_plane *plane;
1205 struct drm_crtc *crtc;
1206 struct drm_connector *connector;
1207 struct drm_connector_list_iter conn_iter;
1209 if (!drm_drv_uses_atomic_modeset(dev))
1212 list_for_each_entry(plane, &config->plane_list, head) {
1214 drm_modeset_lock(&plane->mutex, NULL);
1215 drm_atomic_plane_print_state(p, plane->state);
1217 drm_modeset_unlock(&plane->mutex);
1220 list_for_each_entry(crtc, &config->crtc_list, head) {
1222 drm_modeset_lock(&crtc->mutex, NULL);
1223 drm_atomic_crtc_print_state(p, crtc->state);
1225 drm_modeset_unlock(&crtc->mutex);
1228 drm_connector_list_iter_begin(dev, &conn_iter);
1230 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1231 drm_for_each_connector_iter(connector, &conn_iter)
1232 drm_atomic_connector_print_state(p, connector->state);
1234 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1235 drm_connector_list_iter_end(&conn_iter);
1239 * drm_state_dump - dump entire device atomic state
1240 * @dev: the drm device
1241 * @p: where to print the state to
1243 * Just for debugging. Drivers might want an option to dump state
1244 * to dmesg in case of error irq's. (Hint, you probably want to
1247 * The caller must drm_modeset_lock_all(), or if this is called
1248 * from error irq handler, it should not be enabled by default.
1249 * (Ie. if you are debugging errors you might not care that this
1250 * is racey. But calling this without all modeset locks held is
1251 * not inherently safe.)
1253 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1255 __drm_state_dump(dev, p, false);
1257 EXPORT_SYMBOL(drm_state_dump);
1259 #ifdef CONFIG_DEBUG_FS
1260 static int drm_state_info(struct seq_file *m, void *data)
1262 struct drm_info_node *node = (struct drm_info_node *) m->private;
1263 struct drm_device *dev = node->minor->dev;
1264 struct drm_printer p = drm_seq_file_printer(m);
1266 __drm_state_dump(dev, &p, true);
1271 /* any use in debugfs files to dump individual planes/crtc/etc? */
1272 static const struct drm_info_list drm_atomic_debugfs_list[] = {
1273 {"state", drm_state_info, 0},
1276 int drm_atomic_debugfs_init(struct drm_minor *minor)
1278 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1279 ARRAY_SIZE(drm_atomic_debugfs_list),
1280 minor->debugfs_root, minor);