Merge drm/drm-next into drm-intel-next-queued
[linux-2.6-block.git] / drivers / gpu / drm / drm_atomic.c
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  * Rob Clark <robdclark@gmail.com>
25  * Daniel Vetter <daniel.vetter@ffwll.ch>
26  */
27
28
29 #include <drm/drmP.h>
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>
36
37 #include "drm_crtc_internal.h"
38 #include "drm_internal.h"
39
40 void __drm_crtc_commit_free(struct kref *kref)
41 {
42         struct drm_crtc_commit *commit =
43                 container_of(kref, struct drm_crtc_commit, ref);
44
45         kfree(commit);
46 }
47 EXPORT_SYMBOL(__drm_crtc_commit_free);
48
49 /**
50  * drm_atomic_state_default_release -
51  * release memory initialized by drm_atomic_state_init
52  * @state: atomic state
53  *
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.
57  */
58 void drm_atomic_state_default_release(struct drm_atomic_state *state)
59 {
60         kfree(state->connectors);
61         kfree(state->crtcs);
62         kfree(state->planes);
63         kfree(state->private_objs);
64 }
65 EXPORT_SYMBOL(drm_atomic_state_default_release);
66
67 /**
68  * drm_atomic_state_init - init new atomic state
69  * @dev: DRM device
70  * @state: atomic state
71  *
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.
75  */
76 int
77 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
78 {
79         kref_init(&state->ref);
80
81         /* TODO legacy paths should maybe do a better job about
82          * setting this appropriately?
83          */
84         state->allow_modeset = true;
85
86         state->crtcs = kcalloc(dev->mode_config.num_crtc,
87                                sizeof(*state->crtcs), GFP_KERNEL);
88         if (!state->crtcs)
89                 goto fail;
90         state->planes = kcalloc(dev->mode_config.num_total_plane,
91                                 sizeof(*state->planes), GFP_KERNEL);
92         if (!state->planes)
93                 goto fail;
94
95         state->dev = dev;
96
97         DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
98
99         return 0;
100 fail:
101         drm_atomic_state_default_release(state);
102         return -ENOMEM;
103 }
104 EXPORT_SYMBOL(drm_atomic_state_init);
105
106 /**
107  * drm_atomic_state_alloc - allocate atomic state
108  * @dev: DRM device
109  *
110  * This allocates an empty atomic state to track updates.
111  */
112 struct drm_atomic_state *
113 drm_atomic_state_alloc(struct drm_device *dev)
114 {
115         struct drm_mode_config *config = &dev->mode_config;
116
117         if (!config->funcs->atomic_state_alloc) {
118                 struct drm_atomic_state *state;
119
120                 state = kzalloc(sizeof(*state), GFP_KERNEL);
121                 if (!state)
122                         return NULL;
123                 if (drm_atomic_state_init(dev, state) < 0) {
124                         kfree(state);
125                         return NULL;
126                 }
127                 return state;
128         }
129
130         return config->funcs->atomic_state_alloc(dev);
131 }
132 EXPORT_SYMBOL(drm_atomic_state_alloc);
133
134 /**
135  * drm_atomic_state_default_clear - clear base atomic state
136  * @state: atomic state
137  *
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.
141  */
142 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
143 {
144         struct drm_device *dev = state->dev;
145         struct drm_mode_config *config = &dev->mode_config;
146         int i;
147
148         DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
149
150         for (i = 0; i < state->num_connector; i++) {
151                 struct drm_connector *connector = state->connectors[i].ptr;
152
153                 if (!connector)
154                         continue;
155
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);
163         }
164
165         for (i = 0; i < config->num_crtc; i++) {
166                 struct drm_crtc *crtc = state->crtcs[i].ptr;
167
168                 if (!crtc)
169                         continue;
170
171                 crtc->funcs->atomic_destroy_state(crtc,
172                                                   state->crtcs[i].state);
173
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;
178
179                 if (state->crtcs[i].commit) {
180                         drm_crtc_commit_put(state->crtcs[i].commit);
181                         state->crtcs[i].commit = NULL;
182                 }
183         }
184
185         for (i = 0; i < config->num_total_plane; i++) {
186                 struct drm_plane *plane = state->planes[i].ptr;
187
188                 if (!plane)
189                         continue;
190
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;
197         }
198
199         for (i = 0; i < state->num_private_objs; i++) {
200                 struct drm_private_obj *obj = state->private_objs[i].ptr;
201
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;
208         }
209         state->num_private_objs = 0;
210
211         if (state->fake_commit) {
212                 drm_crtc_commit_put(state->fake_commit);
213                 state->fake_commit = NULL;
214         }
215 }
216 EXPORT_SYMBOL(drm_atomic_state_default_clear);
217
218 /**
219  * drm_atomic_state_clear - clear state object
220  * @state: atomic state
221  *
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.
228  *
229  * Hence we must clear all cached state and completely start over, using this
230  * function.
231  */
232 void drm_atomic_state_clear(struct drm_atomic_state *state)
233 {
234         struct drm_device *dev = state->dev;
235         struct drm_mode_config *config = &dev->mode_config;
236
237         if (config->funcs->atomic_state_clear)
238                 config->funcs->atomic_state_clear(state);
239         else
240                 drm_atomic_state_default_clear(state);
241 }
242 EXPORT_SYMBOL(drm_atomic_state_clear);
243
244 /**
245  * __drm_atomic_state_free - free all memory for an atomic state
246  * @ref: This atomic state to deallocate
247  *
248  * This frees all memory associated with an atomic state, including all the
249  * per-object state for planes, crtcs and connectors.
250  */
251 void __drm_atomic_state_free(struct kref *ref)
252 {
253         struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
254         struct drm_mode_config *config = &state->dev->mode_config;
255
256         drm_atomic_state_clear(state);
257
258         DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
259
260         if (config->funcs->atomic_state_free) {
261                 config->funcs->atomic_state_free(state);
262         } else {
263                 drm_atomic_state_default_release(state);
264                 kfree(state);
265         }
266 }
267 EXPORT_SYMBOL(__drm_atomic_state_free);
268
269 /**
270  * drm_atomic_get_crtc_state - get crtc state
271  * @state: global atomic state object
272  * @crtc: crtc to get state object for
273  *
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
276  * is consistent.
277  *
278  * Returns:
279  *
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.
283  */
284 struct drm_crtc_state *
285 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
286                           struct drm_crtc *crtc)
287 {
288         int ret, index = drm_crtc_index(crtc);
289         struct drm_crtc_state *crtc_state;
290
291         WARN_ON(!state->acquire_ctx);
292
293         crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
294         if (crtc_state)
295                 return crtc_state;
296
297         ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
298         if (ret)
299                 return ERR_PTR(ret);
300
301         crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
302         if (!crtc_state)
303                 return ERR_PTR(-ENOMEM);
304
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;
310
311         DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
312                          crtc->base.id, crtc->name, crtc_state, state);
313
314         return crtc_state;
315 }
316 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
317
318 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
319                 struct drm_crtc_state *state)
320 {
321         /* NOTE: we explicitly don't enforce constraints such as primary
322          * layer covering entire screen, since that is something we want
323          * to allow (on hw that supports it).  For hw that does not, it
324          * should be checked in driver's crtc->atomic_check() vfunc.
325          *
326          * TODO: Add generic modeset state checks once we support those.
327          */
328
329         if (state->active && !state->enable) {
330                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
331                                  crtc->base.id, crtc->name);
332                 return -EINVAL;
333         }
334
335         /* The state->enable vs. state->mode_blob checks can be WARN_ON,
336          * as this is a kernel-internal detail that userspace should never
337          * be able to trigger. */
338         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
339             WARN_ON(state->enable && !state->mode_blob)) {
340                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
341                                  crtc->base.id, crtc->name);
342                 return -EINVAL;
343         }
344
345         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
346             WARN_ON(!state->enable && state->mode_blob)) {
347                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
348                                  crtc->base.id, crtc->name);
349                 return -EINVAL;
350         }
351
352         /*
353          * Reject event generation for when a CRTC is off and stays off.
354          * It wouldn't be hard to implement this, but userspace has a track
355          * record of happily burning through 100% cpu (or worse, crash) when the
356          * display pipe is suspended. To avoid all that fun just reject updates
357          * that ask for events since likely that indicates a bug in the
358          * compositor's drawing loop. This is consistent with the vblank IOCTL
359          * and legacy page_flip IOCTL which also reject service on a disabled
360          * pipe.
361          */
362         if (state->event && !state->active && !crtc->state->active) {
363                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
364                                  crtc->base.id, crtc->name);
365                 return -EINVAL;
366         }
367
368         return 0;
369 }
370
371 static void drm_atomic_crtc_print_state(struct drm_printer *p,
372                 const struct drm_crtc_state *state)
373 {
374         struct drm_crtc *crtc = state->crtc;
375
376         drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
377         drm_printf(p, "\tenable=%d\n", state->enable);
378         drm_printf(p, "\tactive=%d\n", state->active);
379         drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
380         drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
381         drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
382         drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
383         drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
384         drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
385         drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
386         drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
387         drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
388
389         if (crtc->funcs->atomic_print_state)
390                 crtc->funcs->atomic_print_state(p, state);
391 }
392
393 static int drm_atomic_connector_check(struct drm_connector *connector,
394                 struct drm_connector_state *state)
395 {
396         struct drm_crtc_state *crtc_state;
397         struct drm_writeback_job *writeback_job = state->writeback_job;
398         const struct drm_display_info *info = &connector->display_info;
399
400         state->max_bpc = info->bpc ? info->bpc : 8;
401         if (connector->max_bpc_property)
402                 state->max_bpc = min(state->max_bpc, state->max_requested_bpc);
403
404         if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
405                 return 0;
406
407         if (writeback_job->fb && !state->crtc) {
408                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
409                                  connector->base.id, connector->name);
410                 return -EINVAL;
411         }
412
413         if (state->crtc)
414                 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
415                                                                 state->crtc);
416
417         if (writeback_job->fb && !crtc_state->active) {
418                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
419                                  connector->base.id, connector->name,
420                                  state->crtc->base.id);
421                 return -EINVAL;
422         }
423
424         if (writeback_job->out_fence && !writeback_job->fb) {
425                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
426                                  connector->base.id, connector->name);
427                 return -EINVAL;
428         }
429
430         return 0;
431 }
432
433 /**
434  * drm_atomic_get_plane_state - get plane state
435  * @state: global atomic state object
436  * @plane: plane to get state object for
437  *
438  * This function returns the plane state for the given plane, allocating it if
439  * needed. It will also grab the relevant plane lock to make sure that the state
440  * is consistent.
441  *
442  * Returns:
443  *
444  * Either the allocated state or the error code encoded into the pointer. When
445  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
446  * entire atomic sequence must be restarted. All other errors are fatal.
447  */
448 struct drm_plane_state *
449 drm_atomic_get_plane_state(struct drm_atomic_state *state,
450                           struct drm_plane *plane)
451 {
452         int ret, index = drm_plane_index(plane);
453         struct drm_plane_state *plane_state;
454
455         WARN_ON(!state->acquire_ctx);
456
457         /* the legacy pointers should never be set */
458         WARN_ON(plane->fb);
459         WARN_ON(plane->old_fb);
460         WARN_ON(plane->crtc);
461
462         plane_state = drm_atomic_get_existing_plane_state(state, plane);
463         if (plane_state)
464                 return plane_state;
465
466         ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
467         if (ret)
468                 return ERR_PTR(ret);
469
470         plane_state = plane->funcs->atomic_duplicate_state(plane);
471         if (!plane_state)
472                 return ERR_PTR(-ENOMEM);
473
474         state->planes[index].state = plane_state;
475         state->planes[index].ptr = plane;
476         state->planes[index].old_state = plane->state;
477         state->planes[index].new_state = plane_state;
478         plane_state->state = state;
479
480         DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
481                          plane->base.id, plane->name, plane_state, state);
482
483         if (plane_state->crtc) {
484                 struct drm_crtc_state *crtc_state;
485
486                 crtc_state = drm_atomic_get_crtc_state(state,
487                                                        plane_state->crtc);
488                 if (IS_ERR(crtc_state))
489                         return ERR_CAST(crtc_state);
490         }
491
492         return plane_state;
493 }
494 EXPORT_SYMBOL(drm_atomic_get_plane_state);
495
496 static bool
497 plane_switching_crtc(struct drm_atomic_state *state,
498                      struct drm_plane *plane,
499                      struct drm_plane_state *plane_state)
500 {
501         if (!plane->state->crtc || !plane_state->crtc)
502                 return false;
503
504         if (plane->state->crtc == plane_state->crtc)
505                 return false;
506
507         /* This could be refined, but currently there's no helper or driver code
508          * to implement direct switching of active planes nor userspace to take
509          * advantage of more direct plane switching without the intermediate
510          * full OFF state.
511          */
512         return true;
513 }
514
515 /**
516  * drm_atomic_plane_check - check plane state
517  * @plane: plane to check
518  * @state: plane state to check
519  *
520  * Provides core sanity checks for plane state.
521  *
522  * RETURNS:
523  * Zero on success, error code on failure
524  */
525 static int drm_atomic_plane_check(struct drm_plane *plane,
526                 struct drm_plane_state *state)
527 {
528         unsigned int fb_width, fb_height;
529         int ret;
530
531         /* either *both* CRTC and FB must be set, or neither */
532         if (state->crtc && !state->fb) {
533                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
534                                  plane->base.id, plane->name);
535                 return -EINVAL;
536         } else if (state->fb && !state->crtc) {
537                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
538                                  plane->base.id, plane->name);
539                 return -EINVAL;
540         }
541
542         /* if disabled, we don't care about the rest of the state: */
543         if (!state->crtc)
544                 return 0;
545
546         /* Check whether this plane is usable on this CRTC */
547         if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
548                 DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
549                                  state->crtc->base.id, state->crtc->name,
550                                  plane->base.id, plane->name);
551                 return -EINVAL;
552         }
553
554         /* Check whether this plane supports the fb pixel format. */
555         ret = drm_plane_check_pixel_format(plane, state->fb->format->format,
556                                            state->fb->modifier);
557         if (ret) {
558                 struct drm_format_name_buf format_name;
559                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n",
560                                  plane->base.id, plane->name,
561                                  drm_get_format_name(state->fb->format->format,
562                                                      &format_name),
563                                  state->fb->modifier);
564                 return ret;
565         }
566
567         /* Give drivers some help against integer overflows */
568         if (state->crtc_w > INT_MAX ||
569             state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
570             state->crtc_h > INT_MAX ||
571             state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
572                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
573                                  plane->base.id, plane->name,
574                                  state->crtc_w, state->crtc_h,
575                                  state->crtc_x, state->crtc_y);
576                 return -ERANGE;
577         }
578
579         fb_width = state->fb->width << 16;
580         fb_height = state->fb->height << 16;
581
582         /* Make sure source coordinates are inside the fb. */
583         if (state->src_w > fb_width ||
584             state->src_x > fb_width - state->src_w ||
585             state->src_h > fb_height ||
586             state->src_y > fb_height - state->src_h) {
587                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
588                                  "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
589                                  plane->base.id, plane->name,
590                                  state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
591                                  state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
592                                  state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
593                                  state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10,
594                                  state->fb->width, state->fb->height);
595                 return -ENOSPC;
596         }
597
598         if (plane_switching_crtc(state->state, plane, state)) {
599                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
600                                  plane->base.id, plane->name);
601                 return -EINVAL;
602         }
603
604         return 0;
605 }
606
607 static void drm_atomic_plane_print_state(struct drm_printer *p,
608                 const struct drm_plane_state *state)
609 {
610         struct drm_plane *plane = state->plane;
611         struct drm_rect src  = drm_plane_state_src(state);
612         struct drm_rect dest = drm_plane_state_dest(state);
613
614         drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
615         drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
616         drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
617         if (state->fb)
618                 drm_framebuffer_print_info(p, 2, state->fb);
619         drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
620         drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
621         drm_printf(p, "\trotation=%x\n", state->rotation);
622         drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
623         drm_printf(p, "\tcolor-encoding=%s\n",
624                    drm_get_color_encoding_name(state->color_encoding));
625         drm_printf(p, "\tcolor-range=%s\n",
626                    drm_get_color_range_name(state->color_range));
627
628         if (plane->funcs->atomic_print_state)
629                 plane->funcs->atomic_print_state(p, state);
630 }
631
632 /**
633  * DOC: handling driver private state
634  *
635  * Very often the DRM objects exposed to userspace in the atomic modeset api
636  * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
637  * underlying hardware. Especially for any kind of shared resources (e.g. shared
638  * clocks, scaler units, bandwidth and fifo limits shared among a group of
639  * planes or CRTCs, and so on) it makes sense to model these as independent
640  * objects. Drivers then need to do similar state tracking and commit ordering for
641  * such private (since not exposed to userpace) objects as the atomic core and
642  * helpers already provide for connectors, planes and CRTCs.
643  *
644  * To make this easier on drivers the atomic core provides some support to track
645  * driver private state objects using struct &drm_private_obj, with the
646  * associated state struct &drm_private_state.
647  *
648  * Similar to userspace-exposed objects, private state structures can be
649  * acquired by calling drm_atomic_get_private_obj_state(). Since this function
650  * does not take care of locking, drivers should wrap it for each type of
651  * private state object they have with the required call to drm_modeset_lock()
652  * for the corresponding &drm_modeset_lock.
653  *
654  * All private state structures contained in a &drm_atomic_state update can be
655  * iterated using for_each_oldnew_private_obj_in_state(),
656  * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
657  * Drivers are recommended to wrap these for each type of driver private state
658  * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
659  * least if they want to iterate over all objects of a given type.
660  *
661  * An earlier way to handle driver private state was by subclassing struct
662  * &drm_atomic_state. But since that encourages non-standard ways to implement
663  * the check/commit split atomic requires (by using e.g. "check and rollback or
664  * commit instead" of "duplicate state, check, then either commit or release
665  * duplicated state) it is deprecated in favour of using &drm_private_state.
666  */
667
668 /**
669  * drm_atomic_private_obj_init - initialize private object
670  * @obj: private object
671  * @state: initial private object state
672  * @funcs: pointer to the struct of function pointers that identify the object
673  * type
674  *
675  * Initialize the private object, which can be embedded into any
676  * driver private object that needs its own atomic state.
677  */
678 void
679 drm_atomic_private_obj_init(struct drm_private_obj *obj,
680                             struct drm_private_state *state,
681                             const struct drm_private_state_funcs *funcs)
682 {
683         memset(obj, 0, sizeof(*obj));
684
685         obj->state = state;
686         obj->funcs = funcs;
687 }
688 EXPORT_SYMBOL(drm_atomic_private_obj_init);
689
690 /**
691  * drm_atomic_private_obj_fini - finalize private object
692  * @obj: private object
693  *
694  * Finalize the private object.
695  */
696 void
697 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
698 {
699         obj->funcs->atomic_destroy_state(obj, obj->state);
700 }
701 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
702
703 /**
704  * drm_atomic_get_private_obj_state - get private object state
705  * @state: global atomic state
706  * @obj: private object to get the state for
707  *
708  * This function returns the private object state for the given private object,
709  * allocating the state if needed. It does not grab any locks as the caller is
710  * expected to care of any required locking.
711  *
712  * RETURNS:
713  *
714  * Either the allocated state or the error code encoded into a pointer.
715  */
716 struct drm_private_state *
717 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
718                                  struct drm_private_obj *obj)
719 {
720         int index, num_objs, i;
721         size_t size;
722         struct __drm_private_objs_state *arr;
723         struct drm_private_state *obj_state;
724
725         for (i = 0; i < state->num_private_objs; i++)
726                 if (obj == state->private_objs[i].ptr)
727                         return state->private_objs[i].state;
728
729         num_objs = state->num_private_objs + 1;
730         size = sizeof(*state->private_objs) * num_objs;
731         arr = krealloc(state->private_objs, size, GFP_KERNEL);
732         if (!arr)
733                 return ERR_PTR(-ENOMEM);
734
735         state->private_objs = arr;
736         index = state->num_private_objs;
737         memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
738
739         obj_state = obj->funcs->atomic_duplicate_state(obj);
740         if (!obj_state)
741                 return ERR_PTR(-ENOMEM);
742
743         state->private_objs[index].state = obj_state;
744         state->private_objs[index].old_state = obj->state;
745         state->private_objs[index].new_state = obj_state;
746         state->private_objs[index].ptr = obj;
747         obj_state->state = state;
748
749         state->num_private_objs = num_objs;
750
751         DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
752                          obj, obj_state, state);
753
754         return obj_state;
755 }
756 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
757
758 /**
759  * drm_atomic_get_connector_state - get connector state
760  * @state: global atomic state object
761  * @connector: connector to get state object for
762  *
763  * This function returns the connector state for the given connector,
764  * allocating it if needed. It will also grab the relevant connector lock to
765  * make sure that the state is consistent.
766  *
767  * Returns:
768  *
769  * Either the allocated state or the error code encoded into the pointer. When
770  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
771  * entire atomic sequence must be restarted. All other errors are fatal.
772  */
773 struct drm_connector_state *
774 drm_atomic_get_connector_state(struct drm_atomic_state *state,
775                           struct drm_connector *connector)
776 {
777         int ret, index;
778         struct drm_mode_config *config = &connector->dev->mode_config;
779         struct drm_connector_state *connector_state;
780
781         WARN_ON(!state->acquire_ctx);
782
783         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
784         if (ret)
785                 return ERR_PTR(ret);
786
787         index = drm_connector_index(connector);
788
789         if (index >= state->num_connector) {
790                 struct __drm_connnectors_state *c;
791                 int alloc = max(index + 1, config->num_connector);
792
793                 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
794                 if (!c)
795                         return ERR_PTR(-ENOMEM);
796
797                 state->connectors = c;
798                 memset(&state->connectors[state->num_connector], 0,
799                        sizeof(*state->connectors) * (alloc - state->num_connector));
800
801                 state->num_connector = alloc;
802         }
803
804         if (state->connectors[index].state)
805                 return state->connectors[index].state;
806
807         connector_state = connector->funcs->atomic_duplicate_state(connector);
808         if (!connector_state)
809                 return ERR_PTR(-ENOMEM);
810
811         drm_connector_get(connector);
812         state->connectors[index].state = connector_state;
813         state->connectors[index].old_state = connector->state;
814         state->connectors[index].new_state = connector_state;
815         state->connectors[index].ptr = connector;
816         connector_state->state = state;
817
818         DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
819                          connector->base.id, connector->name,
820                          connector_state, state);
821
822         if (connector_state->crtc) {
823                 struct drm_crtc_state *crtc_state;
824
825                 crtc_state = drm_atomic_get_crtc_state(state,
826                                                        connector_state->crtc);
827                 if (IS_ERR(crtc_state))
828                         return ERR_CAST(crtc_state);
829         }
830
831         return connector_state;
832 }
833 EXPORT_SYMBOL(drm_atomic_get_connector_state);
834
835 static void drm_atomic_connector_print_state(struct drm_printer *p,
836                 const struct drm_connector_state *state)
837 {
838         struct drm_connector *connector = state->connector;
839
840         drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
841         drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
842
843         if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
844                 if (state->writeback_job && state->writeback_job->fb)
845                         drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
846
847         if (connector->funcs->atomic_print_state)
848                 connector->funcs->atomic_print_state(p, state);
849 }
850
851 /**
852  * drm_atomic_add_affected_connectors - add connectors for crtc
853  * @state: atomic state
854  * @crtc: DRM crtc
855  *
856  * This function walks the current configuration and adds all connectors
857  * currently using @crtc to the atomic configuration @state. Note that this
858  * function must acquire the connection mutex. This can potentially cause
859  * unneeded seralization if the update is just for the planes on one crtc. Hence
860  * drivers and helpers should only call this when really needed (e.g. when a
861  * full modeset needs to happen due to some change).
862  *
863  * Returns:
864  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
865  * then the w/w mutex code has detected a deadlock and the entire atomic
866  * sequence must be restarted. All other errors are fatal.
867  */
868 int
869 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
870                                    struct drm_crtc *crtc)
871 {
872         struct drm_mode_config *config = &state->dev->mode_config;
873         struct drm_connector *connector;
874         struct drm_connector_state *conn_state;
875         struct drm_connector_list_iter conn_iter;
876         struct drm_crtc_state *crtc_state;
877         int ret;
878
879         crtc_state = drm_atomic_get_crtc_state(state, crtc);
880         if (IS_ERR(crtc_state))
881                 return PTR_ERR(crtc_state);
882
883         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
884         if (ret)
885                 return ret;
886
887         DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
888                          crtc->base.id, crtc->name, state);
889
890         /*
891          * Changed connectors are already in @state, so only need to look
892          * at the connector_mask in crtc_state.
893          */
894         drm_connector_list_iter_begin(state->dev, &conn_iter);
895         drm_for_each_connector_iter(connector, &conn_iter) {
896                 if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
897                         continue;
898
899                 conn_state = drm_atomic_get_connector_state(state, connector);
900                 if (IS_ERR(conn_state)) {
901                         drm_connector_list_iter_end(&conn_iter);
902                         return PTR_ERR(conn_state);
903                 }
904         }
905         drm_connector_list_iter_end(&conn_iter);
906
907         return 0;
908 }
909 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
910
911 /**
912  * drm_atomic_add_affected_planes - add planes for crtc
913  * @state: atomic state
914  * @crtc: DRM crtc
915  *
916  * This function walks the current configuration and adds all planes
917  * currently used by @crtc to the atomic configuration @state. This is useful
918  * when an atomic commit also needs to check all currently enabled plane on
919  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
920  * to avoid special code to force-enable all planes.
921  *
922  * Since acquiring a plane state will always also acquire the w/w mutex of the
923  * current CRTC for that plane (if there is any) adding all the plane states for
924  * a CRTC will not reduce parallism of atomic updates.
925  *
926  * Returns:
927  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
928  * then the w/w mutex code has detected a deadlock and the entire atomic
929  * sequence must be restarted. All other errors are fatal.
930  */
931 int
932 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
933                                struct drm_crtc *crtc)
934 {
935         struct drm_plane *plane;
936
937         WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
938
939         DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
940                          crtc->base.id, crtc->name, state);
941
942         drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
943                 struct drm_plane_state *plane_state =
944                         drm_atomic_get_plane_state(state, plane);
945
946                 if (IS_ERR(plane_state))
947                         return PTR_ERR(plane_state);
948         }
949         return 0;
950 }
951 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
952
953 /**
954  * drm_atomic_check_only - check whether a given config would work
955  * @state: atomic configuration to check
956  *
957  * Note that this function can return -EDEADLK if the driver needed to acquire
958  * more locks but encountered a deadlock. The caller must then do the usual w/w
959  * backoff dance and restart. All other errors are fatal.
960  *
961  * Returns:
962  * 0 on success, negative error code on failure.
963  */
964 int drm_atomic_check_only(struct drm_atomic_state *state)
965 {
966         struct drm_device *dev = state->dev;
967         struct drm_mode_config *config = &dev->mode_config;
968         struct drm_plane *plane;
969         struct drm_plane_state *plane_state;
970         struct drm_crtc *crtc;
971         struct drm_crtc_state *crtc_state;
972         struct drm_connector *conn;
973         struct drm_connector_state *conn_state;
974         int i, ret = 0;
975
976         DRM_DEBUG_ATOMIC("checking %p\n", state);
977
978         for_each_new_plane_in_state(state, plane, plane_state, i) {
979                 ret = drm_atomic_plane_check(plane, plane_state);
980                 if (ret) {
981                         DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
982                                          plane->base.id, plane->name);
983                         return ret;
984                 }
985         }
986
987         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
988                 ret = drm_atomic_crtc_check(crtc, crtc_state);
989                 if (ret) {
990                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
991                                          crtc->base.id, crtc->name);
992                         return ret;
993                 }
994         }
995
996         for_each_new_connector_in_state(state, conn, conn_state, i) {
997                 ret = drm_atomic_connector_check(conn, conn_state);
998                 if (ret) {
999                         DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
1000                                          conn->base.id, conn->name);
1001                         return ret;
1002                 }
1003         }
1004
1005         if (config->funcs->atomic_check) {
1006                 ret = config->funcs->atomic_check(state->dev, state);
1007
1008                 if (ret) {
1009                         DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
1010                                          state, ret);
1011                         return ret;
1012                 }
1013         }
1014
1015         if (!state->allow_modeset) {
1016                 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1017                         if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1018                                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1019                                                  crtc->base.id, crtc->name);
1020                                 return -EINVAL;
1021                         }
1022                 }
1023         }
1024
1025         return 0;
1026 }
1027 EXPORT_SYMBOL(drm_atomic_check_only);
1028
1029 /**
1030  * drm_atomic_commit - commit configuration atomically
1031  * @state: atomic configuration to check
1032  *
1033  * Note that this function can return -EDEADLK if the driver needed to acquire
1034  * more locks but encountered a deadlock. The caller must then do the usual w/w
1035  * backoff dance and restart. All other errors are fatal.
1036  *
1037  * This function will take its own reference on @state.
1038  * Callers should always release their reference with drm_atomic_state_put().
1039  *
1040  * Returns:
1041  * 0 on success, negative error code on failure.
1042  */
1043 int drm_atomic_commit(struct drm_atomic_state *state)
1044 {
1045         struct drm_mode_config *config = &state->dev->mode_config;
1046         int ret;
1047
1048         ret = drm_atomic_check_only(state);
1049         if (ret)
1050                 return ret;
1051
1052         DRM_DEBUG_ATOMIC("committing %p\n", state);
1053
1054         return config->funcs->atomic_commit(state->dev, state, false);
1055 }
1056 EXPORT_SYMBOL(drm_atomic_commit);
1057
1058 /**
1059  * drm_atomic_nonblocking_commit - atomic nonblocking commit
1060  * @state: atomic configuration to check
1061  *
1062  * Note that this function can return -EDEADLK if the driver needed to acquire
1063  * more locks but encountered a deadlock. The caller must then do the usual w/w
1064  * backoff dance and restart. All other errors are fatal.
1065  *
1066  * This function will take its own reference on @state.
1067  * Callers should always release their reference with drm_atomic_state_put().
1068  *
1069  * Returns:
1070  * 0 on success, negative error code on failure.
1071  */
1072 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1073 {
1074         struct drm_mode_config *config = &state->dev->mode_config;
1075         int ret;
1076
1077         ret = drm_atomic_check_only(state);
1078         if (ret)
1079                 return ret;
1080
1081         DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
1082
1083         return config->funcs->atomic_commit(state->dev, state, true);
1084 }
1085 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1086
1087 void drm_atomic_print_state(const struct drm_atomic_state *state)
1088 {
1089         struct drm_printer p = drm_info_printer(state->dev->dev);
1090         struct drm_plane *plane;
1091         struct drm_plane_state *plane_state;
1092         struct drm_crtc *crtc;
1093         struct drm_crtc_state *crtc_state;
1094         struct drm_connector *connector;
1095         struct drm_connector_state *connector_state;
1096         int i;
1097
1098         DRM_DEBUG_ATOMIC("checking %p\n", state);
1099
1100         for_each_new_plane_in_state(state, plane, plane_state, i)
1101                 drm_atomic_plane_print_state(&p, plane_state);
1102
1103         for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1104                 drm_atomic_crtc_print_state(&p, crtc_state);
1105
1106         for_each_new_connector_in_state(state, connector, connector_state, i)
1107                 drm_atomic_connector_print_state(&p, connector_state);
1108 }
1109
1110 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1111                              bool take_locks)
1112 {
1113         struct drm_mode_config *config = &dev->mode_config;
1114         struct drm_plane *plane;
1115         struct drm_crtc *crtc;
1116         struct drm_connector *connector;
1117         struct drm_connector_list_iter conn_iter;
1118
1119         if (!drm_drv_uses_atomic_modeset(dev))
1120                 return;
1121
1122         list_for_each_entry(plane, &config->plane_list, head) {
1123                 if (take_locks)
1124                         drm_modeset_lock(&plane->mutex, NULL);
1125                 drm_atomic_plane_print_state(p, plane->state);
1126                 if (take_locks)
1127                         drm_modeset_unlock(&plane->mutex);
1128         }
1129
1130         list_for_each_entry(crtc, &config->crtc_list, head) {
1131                 if (take_locks)
1132                         drm_modeset_lock(&crtc->mutex, NULL);
1133                 drm_atomic_crtc_print_state(p, crtc->state);
1134                 if (take_locks)
1135                         drm_modeset_unlock(&crtc->mutex);
1136         }
1137
1138         drm_connector_list_iter_begin(dev, &conn_iter);
1139         if (take_locks)
1140                 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1141         drm_for_each_connector_iter(connector, &conn_iter)
1142                 drm_atomic_connector_print_state(p, connector->state);
1143         if (take_locks)
1144                 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1145         drm_connector_list_iter_end(&conn_iter);
1146 }
1147
1148 /**
1149  * drm_state_dump - dump entire device atomic state
1150  * @dev: the drm device
1151  * @p: where to print the state to
1152  *
1153  * Just for debugging.  Drivers might want an option to dump state
1154  * to dmesg in case of error irq's.  (Hint, you probably want to
1155  * ratelimit this!)
1156  *
1157  * The caller must drm_modeset_lock_all(), or if this is called
1158  * from error irq handler, it should not be enabled by default.
1159  * (Ie. if you are debugging errors you might not care that this
1160  * is racey.  But calling this without all modeset locks held is
1161  * not inherently safe.)
1162  */
1163 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1164 {
1165         __drm_state_dump(dev, p, false);
1166 }
1167 EXPORT_SYMBOL(drm_state_dump);
1168
1169 #ifdef CONFIG_DEBUG_FS
1170 static int drm_state_info(struct seq_file *m, void *data)
1171 {
1172         struct drm_info_node *node = (struct drm_info_node *) m->private;
1173         struct drm_device *dev = node->minor->dev;
1174         struct drm_printer p = drm_seq_file_printer(m);
1175
1176         __drm_state_dump(dev, &p, true);
1177
1178         return 0;
1179 }
1180
1181 /* any use in debugfs files to dump individual planes/crtc/etc? */
1182 static const struct drm_info_list drm_atomic_debugfs_list[] = {
1183         {"state", drm_state_info, 0},
1184 };
1185
1186 int drm_atomic_debugfs_init(struct drm_minor *minor)
1187 {
1188         return drm_debugfs_create_files(drm_atomic_debugfs_list,
1189                         ARRAY_SIZE(drm_atomic_debugfs_list),
1190                         minor->debugfs_root, minor);
1191 }
1192 #endif
1193