drm/atomic: Use new atomic iterator macros.
[sfrench/cifs-2.6.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_mode.h>
32 #include <drm/drm_plane_helper.h>
33 #include <drm/drm_print.h>
34 #include <linux/sync_file.h>
35
36 #include "drm_crtc_internal.h"
37
38 void __drm_crtc_commit_free(struct kref *kref)
39 {
40         struct drm_crtc_commit *commit =
41                 container_of(kref, struct drm_crtc_commit, ref);
42
43         kfree(commit);
44 }
45 EXPORT_SYMBOL(__drm_crtc_commit_free);
46
47 /**
48  * drm_atomic_state_default_release -
49  * release memory initialized by drm_atomic_state_init
50  * @state: atomic state
51  *
52  * Free all the memory allocated by drm_atomic_state_init.
53  * This is useful for drivers that subclass the atomic state.
54  */
55 void drm_atomic_state_default_release(struct drm_atomic_state *state)
56 {
57         kfree(state->connectors);
58         kfree(state->crtcs);
59         kfree(state->planes);
60 }
61 EXPORT_SYMBOL(drm_atomic_state_default_release);
62
63 /**
64  * drm_atomic_state_init - init new atomic state
65  * @dev: DRM device
66  * @state: atomic state
67  *
68  * Default implementation for filling in a new atomic state.
69  * This is useful for drivers that subclass the atomic state.
70  */
71 int
72 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
73 {
74         kref_init(&state->ref);
75
76         /* TODO legacy paths should maybe do a better job about
77          * setting this appropriately?
78          */
79         state->allow_modeset = true;
80
81         state->crtcs = kcalloc(dev->mode_config.num_crtc,
82                                sizeof(*state->crtcs), GFP_KERNEL);
83         if (!state->crtcs)
84                 goto fail;
85         state->planes = kcalloc(dev->mode_config.num_total_plane,
86                                 sizeof(*state->planes), GFP_KERNEL);
87         if (!state->planes)
88                 goto fail;
89
90         state->dev = dev;
91
92         DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
93
94         return 0;
95 fail:
96         drm_atomic_state_default_release(state);
97         return -ENOMEM;
98 }
99 EXPORT_SYMBOL(drm_atomic_state_init);
100
101 /**
102  * drm_atomic_state_alloc - allocate atomic state
103  * @dev: DRM device
104  *
105  * This allocates an empty atomic state to track updates.
106  */
107 struct drm_atomic_state *
108 drm_atomic_state_alloc(struct drm_device *dev)
109 {
110         struct drm_mode_config *config = &dev->mode_config;
111         struct drm_atomic_state *state;
112
113         if (!config->funcs->atomic_state_alloc) {
114                 state = kzalloc(sizeof(*state), GFP_KERNEL);
115                 if (!state)
116                         return NULL;
117                 if (drm_atomic_state_init(dev, state) < 0) {
118                         kfree(state);
119                         return NULL;
120                 }
121                 return state;
122         }
123
124         return config->funcs->atomic_state_alloc(dev);
125 }
126 EXPORT_SYMBOL(drm_atomic_state_alloc);
127
128 /**
129  * drm_atomic_state_default_clear - clear base atomic state
130  * @state: atomic state
131  *
132  * Default implementation for clearing atomic state.
133  * This is useful for drivers that subclass the atomic state.
134  */
135 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
136 {
137         struct drm_device *dev = state->dev;
138         struct drm_mode_config *config = &dev->mode_config;
139         int i;
140
141         DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
142
143         for (i = 0; i < state->num_connector; i++) {
144                 struct drm_connector *connector = state->connectors[i].ptr;
145
146                 if (!connector)
147                         continue;
148
149                 connector->funcs->atomic_destroy_state(connector,
150                                                        state->connectors[i].state);
151                 state->connectors[i].ptr = NULL;
152                 state->connectors[i].state = NULL;
153                 drm_connector_unreference(connector);
154         }
155
156         for (i = 0; i < config->num_crtc; i++) {
157                 struct drm_crtc *crtc = state->crtcs[i].ptr;
158
159                 if (!crtc)
160                         continue;
161
162                 crtc->funcs->atomic_destroy_state(crtc,
163                                                   state->crtcs[i].state);
164
165                 if (state->crtcs[i].commit) {
166                         kfree(state->crtcs[i].commit->event);
167                         state->crtcs[i].commit->event = NULL;
168                         drm_crtc_commit_put(state->crtcs[i].commit);
169                 }
170
171                 state->crtcs[i].commit = NULL;
172                 state->crtcs[i].ptr = NULL;
173                 state->crtcs[i].state = NULL;
174         }
175
176         for (i = 0; i < config->num_total_plane; i++) {
177                 struct drm_plane *plane = state->planes[i].ptr;
178
179                 if (!plane)
180                         continue;
181
182                 plane->funcs->atomic_destroy_state(plane,
183                                                    state->planes[i].state);
184                 state->planes[i].ptr = NULL;
185                 state->planes[i].state = NULL;
186         }
187 }
188 EXPORT_SYMBOL(drm_atomic_state_default_clear);
189
190 /**
191  * drm_atomic_state_clear - clear state object
192  * @state: atomic state
193  *
194  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
195  * all locks. So someone else could sneak in and change the current modeset
196  * configuration. Which means that all the state assembled in @state is no
197  * longer an atomic update to the current state, but to some arbitrary earlier
198  * state. Which could break assumptions the driver's
199  * &drm_mode_config_funcs.atomic_check likely relies on.
200  *
201  * Hence we must clear all cached state and completely start over, using this
202  * function.
203  */
204 void drm_atomic_state_clear(struct drm_atomic_state *state)
205 {
206         struct drm_device *dev = state->dev;
207         struct drm_mode_config *config = &dev->mode_config;
208
209         if (config->funcs->atomic_state_clear)
210                 config->funcs->atomic_state_clear(state);
211         else
212                 drm_atomic_state_default_clear(state);
213 }
214 EXPORT_SYMBOL(drm_atomic_state_clear);
215
216 /**
217  * __drm_atomic_state_free - free all memory for an atomic state
218  * @ref: This atomic state to deallocate
219  *
220  * This frees all memory associated with an atomic state, including all the
221  * per-object state for planes, crtcs and connectors.
222  */
223 void __drm_atomic_state_free(struct kref *ref)
224 {
225         struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
226         struct drm_mode_config *config = &state->dev->mode_config;
227
228         drm_atomic_state_clear(state);
229
230         DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
231
232         if (config->funcs->atomic_state_free) {
233                 config->funcs->atomic_state_free(state);
234         } else {
235                 drm_atomic_state_default_release(state);
236                 kfree(state);
237         }
238 }
239 EXPORT_SYMBOL(__drm_atomic_state_free);
240
241 /**
242  * drm_atomic_get_crtc_state - get crtc state
243  * @state: global atomic state object
244  * @crtc: crtc to get state object for
245  *
246  * This function returns the crtc state for the given crtc, allocating it if
247  * needed. It will also grab the relevant crtc lock to make sure that the state
248  * is consistent.
249  *
250  * Returns:
251  *
252  * Either the allocated state or the error code encoded into the pointer. When
253  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
254  * entire atomic sequence must be restarted. All other errors are fatal.
255  */
256 struct drm_crtc_state *
257 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
258                           struct drm_crtc *crtc)
259 {
260         int ret, index = drm_crtc_index(crtc);
261         struct drm_crtc_state *crtc_state;
262
263         WARN_ON(!state->acquire_ctx);
264
265         crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
266         if (crtc_state)
267                 return crtc_state;
268
269         ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
270         if (ret)
271                 return ERR_PTR(ret);
272
273         crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
274         if (!crtc_state)
275                 return ERR_PTR(-ENOMEM);
276
277         state->crtcs[index].state = crtc_state;
278         state->crtcs[index].old_state = crtc->state;
279         state->crtcs[index].new_state = crtc_state;
280         state->crtcs[index].ptr = crtc;
281         crtc_state->state = state;
282
283         DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
284                          crtc->base.id, crtc->name, crtc_state, state);
285
286         return crtc_state;
287 }
288 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
289
290 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
291                                    struct drm_crtc *crtc, s64 __user *fence_ptr)
292 {
293         state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
294 }
295
296 static s64 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
297                                           struct drm_crtc *crtc)
298 {
299         s64 __user *fence_ptr;
300
301         fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
302         state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
303
304         return fence_ptr;
305 }
306
307 /**
308  * drm_atomic_set_mode_for_crtc - set mode for CRTC
309  * @state: the CRTC whose incoming state to update
310  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
311  *
312  * Set a mode (originating from the kernel) on the desired CRTC state and update
313  * the enable property.
314  *
315  * RETURNS:
316  * Zero on success, error code on failure. Cannot return -EDEADLK.
317  */
318 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
319                                  struct drm_display_mode *mode)
320 {
321         struct drm_mode_modeinfo umode;
322
323         /* Early return for no change. */
324         if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
325                 return 0;
326
327         drm_property_unreference_blob(state->mode_blob);
328         state->mode_blob = NULL;
329
330         if (mode) {
331                 drm_mode_convert_to_umode(&umode, mode);
332                 state->mode_blob =
333                         drm_property_create_blob(state->crtc->dev,
334                                                  sizeof(umode),
335                                                  &umode);
336                 if (IS_ERR(state->mode_blob))
337                         return PTR_ERR(state->mode_blob);
338
339                 drm_mode_copy(&state->mode, mode);
340                 state->enable = true;
341                 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
342                                  mode->name, state);
343         } else {
344                 memset(&state->mode, 0, sizeof(state->mode));
345                 state->enable = false;
346                 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
347                                  state);
348         }
349
350         return 0;
351 }
352 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
353
354 /**
355  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
356  * @state: the CRTC whose incoming state to update
357  * @blob: pointer to blob property to use for mode
358  *
359  * Set a mode (originating from a blob property) on the desired CRTC state.
360  * This function will take a reference on the blob property for the CRTC state,
361  * and release the reference held on the state's existing mode property, if any
362  * was set.
363  *
364  * RETURNS:
365  * Zero on success, error code on failure. Cannot return -EDEADLK.
366  */
367 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
368                                       struct drm_property_blob *blob)
369 {
370         if (blob == state->mode_blob)
371                 return 0;
372
373         drm_property_unreference_blob(state->mode_blob);
374         state->mode_blob = NULL;
375
376         memset(&state->mode, 0, sizeof(state->mode));
377
378         if (blob) {
379                 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
380                     drm_mode_convert_umode(&state->mode,
381                                            (const struct drm_mode_modeinfo *)
382                                             blob->data))
383                         return -EINVAL;
384
385                 state->mode_blob = drm_property_reference_blob(blob);
386                 state->enable = true;
387                 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
388                                  state->mode.name, state);
389         } else {
390                 state->enable = false;
391                 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
392                                  state);
393         }
394
395         return 0;
396 }
397 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
398
399 /**
400  * drm_atomic_replace_property_blob - replace a blob property
401  * @blob: a pointer to the member blob to be replaced
402  * @new_blob: the new blob to replace with
403  * @replaced: whether the blob has been replaced
404  *
405  * RETURNS:
406  * Zero on success, error code on failure
407  */
408 static void
409 drm_atomic_replace_property_blob(struct drm_property_blob **blob,
410                                  struct drm_property_blob *new_blob,
411                                  bool *replaced)
412 {
413         struct drm_property_blob *old_blob = *blob;
414
415         if (old_blob == new_blob)
416                 return;
417
418         drm_property_unreference_blob(old_blob);
419         if (new_blob)
420                 drm_property_reference_blob(new_blob);
421         *blob = new_blob;
422         *replaced = true;
423
424         return;
425 }
426
427 static int
428 drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
429                                          struct drm_property_blob **blob,
430                                          uint64_t blob_id,
431                                          ssize_t expected_size,
432                                          bool *replaced)
433 {
434         struct drm_property_blob *new_blob = NULL;
435
436         if (blob_id != 0) {
437                 new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
438                 if (new_blob == NULL)
439                         return -EINVAL;
440
441                 if (expected_size > 0 && expected_size != new_blob->length) {
442                         drm_property_unreference_blob(new_blob);
443                         return -EINVAL;
444                 }
445         }
446
447         drm_atomic_replace_property_blob(blob, new_blob, replaced);
448         drm_property_unreference_blob(new_blob);
449
450         return 0;
451 }
452
453 /**
454  * drm_atomic_crtc_set_property - set property on CRTC
455  * @crtc: the drm CRTC to set a property on
456  * @state: the state object to update with the new property value
457  * @property: the property to set
458  * @val: the new property value
459  *
460  * This function handles generic/core properties and calls out to driver's
461  * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
462  * consistent behavior you must call this function rather than the driver hook
463  * directly.
464  *
465  * RETURNS:
466  * Zero on success, error code on failure
467  */
468 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
469                 struct drm_crtc_state *state, struct drm_property *property,
470                 uint64_t val)
471 {
472         struct drm_device *dev = crtc->dev;
473         struct drm_mode_config *config = &dev->mode_config;
474         bool replaced = false;
475         int ret;
476
477         if (property == config->prop_active)
478                 state->active = val;
479         else if (property == config->prop_mode_id) {
480                 struct drm_property_blob *mode =
481                         drm_property_lookup_blob(dev, val);
482                 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
483                 drm_property_unreference_blob(mode);
484                 return ret;
485         } else if (property == config->degamma_lut_property) {
486                 ret = drm_atomic_replace_property_blob_from_id(crtc,
487                                         &state->degamma_lut,
488                                         val,
489                                         -1,
490                                         &replaced);
491                 state->color_mgmt_changed |= replaced;
492                 return ret;
493         } else if (property == config->ctm_property) {
494                 ret = drm_atomic_replace_property_blob_from_id(crtc,
495                                         &state->ctm,
496                                         val,
497                                         sizeof(struct drm_color_ctm),
498                                         &replaced);
499                 state->color_mgmt_changed |= replaced;
500                 return ret;
501         } else if (property == config->gamma_lut_property) {
502                 ret = drm_atomic_replace_property_blob_from_id(crtc,
503                                         &state->gamma_lut,
504                                         val,
505                                         -1,
506                                         &replaced);
507                 state->color_mgmt_changed |= replaced;
508                 return ret;
509         } else if (property == config->prop_out_fence_ptr) {
510                 s64 __user *fence_ptr = u64_to_user_ptr(val);
511
512                 if (!fence_ptr)
513                         return 0;
514
515                 if (put_user(-1, fence_ptr))
516                         return -EFAULT;
517
518                 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
519         } else if (crtc->funcs->atomic_set_property)
520                 return crtc->funcs->atomic_set_property(crtc, state, property, val);
521         else
522                 return -EINVAL;
523
524         return 0;
525 }
526 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
527
528 /**
529  * drm_atomic_crtc_get_property - get property value from CRTC state
530  * @crtc: the drm CRTC to set a property on
531  * @state: the state object to get the property value from
532  * @property: the property to set
533  * @val: return location for the property value
534  *
535  * This function handles generic/core properties and calls out to driver's
536  * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
537  * consistent behavior you must call this function rather than the driver hook
538  * directly.
539  *
540  * RETURNS:
541  * Zero on success, error code on failure
542  */
543 static int
544 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
545                 const struct drm_crtc_state *state,
546                 struct drm_property *property, uint64_t *val)
547 {
548         struct drm_device *dev = crtc->dev;
549         struct drm_mode_config *config = &dev->mode_config;
550
551         if (property == config->prop_active)
552                 *val = state->active;
553         else if (property == config->prop_mode_id)
554                 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
555         else if (property == config->degamma_lut_property)
556                 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
557         else if (property == config->ctm_property)
558                 *val = (state->ctm) ? state->ctm->base.id : 0;
559         else if (property == config->gamma_lut_property)
560                 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
561         else if (property == config->prop_out_fence_ptr)
562                 *val = 0;
563         else if (crtc->funcs->atomic_get_property)
564                 return crtc->funcs->atomic_get_property(crtc, state, property, val);
565         else
566                 return -EINVAL;
567
568         return 0;
569 }
570
571 /**
572  * drm_atomic_crtc_check - check crtc state
573  * @crtc: crtc to check
574  * @state: crtc state to check
575  *
576  * Provides core sanity checks for crtc state.
577  *
578  * RETURNS:
579  * Zero on success, error code on failure
580  */
581 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
582                 struct drm_crtc_state *state)
583 {
584         /* NOTE: we explicitly don't enforce constraints such as primary
585          * layer covering entire screen, since that is something we want
586          * to allow (on hw that supports it).  For hw that does not, it
587          * should be checked in driver's crtc->atomic_check() vfunc.
588          *
589          * TODO: Add generic modeset state checks once we support those.
590          */
591
592         if (state->active && !state->enable) {
593                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
594                                  crtc->base.id, crtc->name);
595                 return -EINVAL;
596         }
597
598         /* The state->enable vs. state->mode_blob checks can be WARN_ON,
599          * as this is a kernel-internal detail that userspace should never
600          * be able to trigger. */
601         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
602             WARN_ON(state->enable && !state->mode_blob)) {
603                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
604                                  crtc->base.id, crtc->name);
605                 return -EINVAL;
606         }
607
608         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
609             WARN_ON(!state->enable && state->mode_blob)) {
610                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
611                                  crtc->base.id, crtc->name);
612                 return -EINVAL;
613         }
614
615         /*
616          * Reject event generation for when a CRTC is off and stays off.
617          * It wouldn't be hard to implement this, but userspace has a track
618          * record of happily burning through 100% cpu (or worse, crash) when the
619          * display pipe is suspended. To avoid all that fun just reject updates
620          * that ask for events since likely that indicates a bug in the
621          * compositor's drawing loop. This is consistent with the vblank IOCTL
622          * and legacy page_flip IOCTL which also reject service on a disabled
623          * pipe.
624          */
625         if (state->event && !state->active && !crtc->state->active) {
626                 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
627                                  crtc->base.id);
628                 return -EINVAL;
629         }
630
631         return 0;
632 }
633
634 static void drm_atomic_crtc_print_state(struct drm_printer *p,
635                 const struct drm_crtc_state *state)
636 {
637         struct drm_crtc *crtc = state->crtc;
638
639         drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
640         drm_printf(p, "\tenable=%d\n", state->enable);
641         drm_printf(p, "\tactive=%d\n", state->active);
642         drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
643         drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
644         drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
645         drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
646         drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
647         drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
648         drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
649         drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
650         drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
651
652         if (crtc->funcs->atomic_print_state)
653                 crtc->funcs->atomic_print_state(p, state);
654 }
655
656 /**
657  * drm_atomic_get_plane_state - get plane state
658  * @state: global atomic state object
659  * @plane: plane to get state object for
660  *
661  * This function returns the plane state for the given plane, allocating it if
662  * needed. It will also grab the relevant plane lock to make sure that the state
663  * is consistent.
664  *
665  * Returns:
666  *
667  * Either the allocated state or the error code encoded into the pointer. When
668  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
669  * entire atomic sequence must be restarted. All other errors are fatal.
670  */
671 struct drm_plane_state *
672 drm_atomic_get_plane_state(struct drm_atomic_state *state,
673                           struct drm_plane *plane)
674 {
675         int ret, index = drm_plane_index(plane);
676         struct drm_plane_state *plane_state;
677
678         WARN_ON(!state->acquire_ctx);
679
680         plane_state = drm_atomic_get_existing_plane_state(state, plane);
681         if (plane_state)
682                 return plane_state;
683
684         ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
685         if (ret)
686                 return ERR_PTR(ret);
687
688         plane_state = plane->funcs->atomic_duplicate_state(plane);
689         if (!plane_state)
690                 return ERR_PTR(-ENOMEM);
691
692         state->planes[index].state = plane_state;
693         state->planes[index].ptr = plane;
694         state->planes[index].old_state = plane->state;
695         state->planes[index].new_state = plane_state;
696         plane_state->state = state;
697
698         DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
699                          plane->base.id, plane->name, plane_state, state);
700
701         if (plane_state->crtc) {
702                 struct drm_crtc_state *crtc_state;
703
704                 crtc_state = drm_atomic_get_crtc_state(state,
705                                                        plane_state->crtc);
706                 if (IS_ERR(crtc_state))
707                         return ERR_CAST(crtc_state);
708         }
709
710         return plane_state;
711 }
712 EXPORT_SYMBOL(drm_atomic_get_plane_state);
713
714 /**
715  * drm_atomic_plane_set_property - set property on plane
716  * @plane: the drm plane to set a property on
717  * @state: the state object to update with the new property value
718  * @property: the property to set
719  * @val: the new property value
720  *
721  * This function handles generic/core properties and calls out to driver's
722  * &drm_plane_funcs.atomic_set_property for driver properties.  To ensure
723  * consistent behavior you must call this function rather than the driver hook
724  * directly.
725  *
726  * RETURNS:
727  * Zero on success, error code on failure
728  */
729 int drm_atomic_plane_set_property(struct drm_plane *plane,
730                 struct drm_plane_state *state, struct drm_property *property,
731                 uint64_t val)
732 {
733         struct drm_device *dev = plane->dev;
734         struct drm_mode_config *config = &dev->mode_config;
735
736         if (property == config->prop_fb_id) {
737                 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
738                 drm_atomic_set_fb_for_plane(state, fb);
739                 if (fb)
740                         drm_framebuffer_unreference(fb);
741         } else if (property == config->prop_in_fence_fd) {
742                 if (state->fence)
743                         return -EINVAL;
744
745                 if (U642I64(val) == -1)
746                         return 0;
747
748                 state->fence = sync_file_get_fence(val);
749                 if (!state->fence)
750                         return -EINVAL;
751
752         } else if (property == config->prop_crtc_id) {
753                 struct drm_crtc *crtc = drm_crtc_find(dev, val);
754                 return drm_atomic_set_crtc_for_plane(state, crtc);
755         } else if (property == config->prop_crtc_x) {
756                 state->crtc_x = U642I64(val);
757         } else if (property == config->prop_crtc_y) {
758                 state->crtc_y = U642I64(val);
759         } else if (property == config->prop_crtc_w) {
760                 state->crtc_w = val;
761         } else if (property == config->prop_crtc_h) {
762                 state->crtc_h = val;
763         } else if (property == config->prop_src_x) {
764                 state->src_x = val;
765         } else if (property == config->prop_src_y) {
766                 state->src_y = val;
767         } else if (property == config->prop_src_w) {
768                 state->src_w = val;
769         } else if (property == config->prop_src_h) {
770                 state->src_h = val;
771         } else if (property == plane->rotation_property) {
772                 if (!is_power_of_2(val & DRM_ROTATE_MASK))
773                         return -EINVAL;
774                 state->rotation = val;
775         } else if (property == plane->zpos_property) {
776                 state->zpos = val;
777         } else if (plane->funcs->atomic_set_property) {
778                 return plane->funcs->atomic_set_property(plane, state,
779                                 property, val);
780         } else {
781                 return -EINVAL;
782         }
783
784         return 0;
785 }
786 EXPORT_SYMBOL(drm_atomic_plane_set_property);
787
788 /**
789  * drm_atomic_plane_get_property - get property value from plane state
790  * @plane: the drm plane to set a property on
791  * @state: the state object to get the property value from
792  * @property: the property to set
793  * @val: return location for the property value
794  *
795  * This function handles generic/core properties and calls out to driver's
796  * &drm_plane_funcs.atomic_get_property for driver properties.  To ensure
797  * consistent behavior you must call this function rather than the driver hook
798  * directly.
799  *
800  * RETURNS:
801  * Zero on success, error code on failure
802  */
803 static int
804 drm_atomic_plane_get_property(struct drm_plane *plane,
805                 const struct drm_plane_state *state,
806                 struct drm_property *property, uint64_t *val)
807 {
808         struct drm_device *dev = plane->dev;
809         struct drm_mode_config *config = &dev->mode_config;
810
811         if (property == config->prop_fb_id) {
812                 *val = (state->fb) ? state->fb->base.id : 0;
813         } else if (property == config->prop_in_fence_fd) {
814                 *val = -1;
815         } else if (property == config->prop_crtc_id) {
816                 *val = (state->crtc) ? state->crtc->base.id : 0;
817         } else if (property == config->prop_crtc_x) {
818                 *val = I642U64(state->crtc_x);
819         } else if (property == config->prop_crtc_y) {
820                 *val = I642U64(state->crtc_y);
821         } else if (property == config->prop_crtc_w) {
822                 *val = state->crtc_w;
823         } else if (property == config->prop_crtc_h) {
824                 *val = state->crtc_h;
825         } else if (property == config->prop_src_x) {
826                 *val = state->src_x;
827         } else if (property == config->prop_src_y) {
828                 *val = state->src_y;
829         } else if (property == config->prop_src_w) {
830                 *val = state->src_w;
831         } else if (property == config->prop_src_h) {
832                 *val = state->src_h;
833         } else if (property == plane->rotation_property) {
834                 *val = state->rotation;
835         } else if (property == plane->zpos_property) {
836                 *val = state->zpos;
837         } else if (plane->funcs->atomic_get_property) {
838                 return plane->funcs->atomic_get_property(plane, state, property, val);
839         } else {
840                 return -EINVAL;
841         }
842
843         return 0;
844 }
845
846 static bool
847 plane_switching_crtc(struct drm_atomic_state *state,
848                      struct drm_plane *plane,
849                      struct drm_plane_state *plane_state)
850 {
851         if (!plane->state->crtc || !plane_state->crtc)
852                 return false;
853
854         if (plane->state->crtc == plane_state->crtc)
855                 return false;
856
857         /* This could be refined, but currently there's no helper or driver code
858          * to implement direct switching of active planes nor userspace to take
859          * advantage of more direct plane switching without the intermediate
860          * full OFF state.
861          */
862         return true;
863 }
864
865 /**
866  * drm_atomic_plane_check - check plane state
867  * @plane: plane to check
868  * @state: plane state to check
869  *
870  * Provides core sanity checks for plane state.
871  *
872  * RETURNS:
873  * Zero on success, error code on failure
874  */
875 static int drm_atomic_plane_check(struct drm_plane *plane,
876                 struct drm_plane_state *state)
877 {
878         unsigned int fb_width, fb_height;
879         int ret;
880
881         /* either *both* CRTC and FB must be set, or neither */
882         if (WARN_ON(state->crtc && !state->fb)) {
883                 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
884                 return -EINVAL;
885         } else if (WARN_ON(state->fb && !state->crtc)) {
886                 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
887                 return -EINVAL;
888         }
889
890         /* if disabled, we don't care about the rest of the state: */
891         if (!state->crtc)
892                 return 0;
893
894         /* Check whether this plane is usable on this CRTC */
895         if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
896                 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
897                 return -EINVAL;
898         }
899
900         /* Check whether this plane supports the fb pixel format. */
901         ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
902         if (ret) {
903                 struct drm_format_name_buf format_name;
904                 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
905                                  drm_get_format_name(state->fb->format->format,
906                                                      &format_name));
907                 return ret;
908         }
909
910         /* Give drivers some help against integer overflows */
911         if (state->crtc_w > INT_MAX ||
912             state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
913             state->crtc_h > INT_MAX ||
914             state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
915                 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
916                                  state->crtc_w, state->crtc_h,
917                                  state->crtc_x, state->crtc_y);
918                 return -ERANGE;
919         }
920
921         fb_width = state->fb->width << 16;
922         fb_height = state->fb->height << 16;
923
924         /* Make sure source coordinates are inside the fb. */
925         if (state->src_w > fb_width ||
926             state->src_x > fb_width - state->src_w ||
927             state->src_h > fb_height ||
928             state->src_y > fb_height - state->src_h) {
929                 DRM_DEBUG_ATOMIC("Invalid source coordinates "
930                                  "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
931                                  state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
932                                  state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
933                                  state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
934                                  state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
935                 return -ENOSPC;
936         }
937
938         if (plane_switching_crtc(state->state, plane, state)) {
939                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
940                                  plane->base.id, plane->name);
941                 return -EINVAL;
942         }
943
944         return 0;
945 }
946
947 static void drm_atomic_plane_print_state(struct drm_printer *p,
948                 const struct drm_plane_state *state)
949 {
950         struct drm_plane *plane = state->plane;
951         struct drm_rect src  = drm_plane_state_src(state);
952         struct drm_rect dest = drm_plane_state_dest(state);
953
954         drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
955         drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
956         drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
957         if (state->fb) {
958                 struct drm_framebuffer *fb = state->fb;
959                 int i, n = fb->format->num_planes;
960                 struct drm_format_name_buf format_name;
961
962                 drm_printf(p, "\t\tformat=%s\n",
963                               drm_get_format_name(fb->format->format, &format_name));
964                 drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
965                 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
966                 drm_printf(p, "\t\tlayers:\n");
967                 for (i = 0; i < n; i++) {
968                         drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
969                         drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
970                 }
971         }
972         drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
973         drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
974         drm_printf(p, "\trotation=%x\n", state->rotation);
975
976         if (plane->funcs->atomic_print_state)
977                 plane->funcs->atomic_print_state(p, state);
978 }
979
980 /**
981  * drm_atomic_get_connector_state - get connector state
982  * @state: global atomic state object
983  * @connector: connector to get state object for
984  *
985  * This function returns the connector state for the given connector,
986  * allocating it if needed. It will also grab the relevant connector lock to
987  * make sure that the state is consistent.
988  *
989  * Returns:
990  *
991  * Either the allocated state or the error code encoded into the pointer. When
992  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
993  * entire atomic sequence must be restarted. All other errors are fatal.
994  */
995 struct drm_connector_state *
996 drm_atomic_get_connector_state(struct drm_atomic_state *state,
997                           struct drm_connector *connector)
998 {
999         int ret, index;
1000         struct drm_mode_config *config = &connector->dev->mode_config;
1001         struct drm_connector_state *connector_state;
1002
1003         WARN_ON(!state->acquire_ctx);
1004
1005         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1006         if (ret)
1007                 return ERR_PTR(ret);
1008
1009         index = drm_connector_index(connector);
1010
1011         if (index >= state->num_connector) {
1012                 struct __drm_connnectors_state *c;
1013                 int alloc = max(index + 1, config->num_connector);
1014
1015                 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1016                 if (!c)
1017                         return ERR_PTR(-ENOMEM);
1018
1019                 state->connectors = c;
1020                 memset(&state->connectors[state->num_connector], 0,
1021                        sizeof(*state->connectors) * (alloc - state->num_connector));
1022
1023                 state->num_connector = alloc;
1024         }
1025
1026         if (state->connectors[index].state)
1027                 return state->connectors[index].state;
1028
1029         connector_state = connector->funcs->atomic_duplicate_state(connector);
1030         if (!connector_state)
1031                 return ERR_PTR(-ENOMEM);
1032
1033         drm_connector_reference(connector);
1034         state->connectors[index].state = connector_state;
1035         state->connectors[index].old_state = connector->state;
1036         state->connectors[index].new_state = connector_state;
1037         state->connectors[index].ptr = connector;
1038         connector_state->state = state;
1039
1040         DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
1041                          connector->base.id, connector_state, state);
1042
1043         if (connector_state->crtc) {
1044                 struct drm_crtc_state *crtc_state;
1045
1046                 crtc_state = drm_atomic_get_crtc_state(state,
1047                                                        connector_state->crtc);
1048                 if (IS_ERR(crtc_state))
1049                         return ERR_CAST(crtc_state);
1050         }
1051
1052         return connector_state;
1053 }
1054 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1055
1056 /**
1057  * drm_atomic_connector_set_property - set property on connector.
1058  * @connector: the drm connector to set a property on
1059  * @state: the state object to update with the new property value
1060  * @property: the property to set
1061  * @val: the new property value
1062  *
1063  * This function handles generic/core properties and calls out to driver's
1064  * &drm_connector_funcs.atomic_set_property for driver properties.  To ensure
1065  * consistent behavior you must call this function rather than the driver hook
1066  * directly.
1067  *
1068  * RETURNS:
1069  * Zero on success, error code on failure
1070  */
1071 int drm_atomic_connector_set_property(struct drm_connector *connector,
1072                 struct drm_connector_state *state, struct drm_property *property,
1073                 uint64_t val)
1074 {
1075         struct drm_device *dev = connector->dev;
1076         struct drm_mode_config *config = &dev->mode_config;
1077
1078         if (property == config->prop_crtc_id) {
1079                 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1080                 return drm_atomic_set_crtc_for_connector(state, crtc);
1081         } else if (property == config->dpms_property) {
1082                 /* setting DPMS property requires special handling, which
1083                  * is done in legacy setprop path for us.  Disallow (for
1084                  * now?) atomic writes to DPMS property:
1085                  */
1086                 return -EINVAL;
1087         } else if (property == config->tv_select_subconnector_property) {
1088                 state->tv.subconnector = val;
1089         } else if (property == config->tv_left_margin_property) {
1090                 state->tv.margins.left = val;
1091         } else if (property == config->tv_right_margin_property) {
1092                 state->tv.margins.right = val;
1093         } else if (property == config->tv_top_margin_property) {
1094                 state->tv.margins.top = val;
1095         } else if (property == config->tv_bottom_margin_property) {
1096                 state->tv.margins.bottom = val;
1097         } else if (property == config->tv_mode_property) {
1098                 state->tv.mode = val;
1099         } else if (property == config->tv_brightness_property) {
1100                 state->tv.brightness = val;
1101         } else if (property == config->tv_contrast_property) {
1102                 state->tv.contrast = val;
1103         } else if (property == config->tv_flicker_reduction_property) {
1104                 state->tv.flicker_reduction = val;
1105         } else if (property == config->tv_overscan_property) {
1106                 state->tv.overscan = val;
1107         } else if (property == config->tv_saturation_property) {
1108                 state->tv.saturation = val;
1109         } else if (property == config->tv_hue_property) {
1110                 state->tv.hue = val;
1111         } else if (connector->funcs->atomic_set_property) {
1112                 return connector->funcs->atomic_set_property(connector,
1113                                 state, property, val);
1114         } else {
1115                 return -EINVAL;
1116         }
1117
1118         return 0;
1119 }
1120 EXPORT_SYMBOL(drm_atomic_connector_set_property);
1121
1122 static void drm_atomic_connector_print_state(struct drm_printer *p,
1123                 const struct drm_connector_state *state)
1124 {
1125         struct drm_connector *connector = state->connector;
1126
1127         drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1128         drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1129
1130         if (connector->funcs->atomic_print_state)
1131                 connector->funcs->atomic_print_state(p, state);
1132 }
1133
1134 /**
1135  * drm_atomic_connector_get_property - get property value from connector state
1136  * @connector: the drm connector to set a property on
1137  * @state: the state object to get the property value from
1138  * @property: the property to set
1139  * @val: return location for the property value
1140  *
1141  * This function handles generic/core properties and calls out to driver's
1142  * &drm_connector_funcs.atomic_get_property for driver properties.  To ensure
1143  * consistent behavior you must call this function rather than the driver hook
1144  * directly.
1145  *
1146  * RETURNS:
1147  * Zero on success, error code on failure
1148  */
1149 static int
1150 drm_atomic_connector_get_property(struct drm_connector *connector,
1151                 const struct drm_connector_state *state,
1152                 struct drm_property *property, uint64_t *val)
1153 {
1154         struct drm_device *dev = connector->dev;
1155         struct drm_mode_config *config = &dev->mode_config;
1156
1157         if (property == config->prop_crtc_id) {
1158                 *val = (state->crtc) ? state->crtc->base.id : 0;
1159         } else if (property == config->dpms_property) {
1160                 *val = connector->dpms;
1161         } else if (property == config->tv_select_subconnector_property) {
1162                 *val = state->tv.subconnector;
1163         } else if (property == config->tv_left_margin_property) {
1164                 *val = state->tv.margins.left;
1165         } else if (property == config->tv_right_margin_property) {
1166                 *val = state->tv.margins.right;
1167         } else if (property == config->tv_top_margin_property) {
1168                 *val = state->tv.margins.top;
1169         } else if (property == config->tv_bottom_margin_property) {
1170                 *val = state->tv.margins.bottom;
1171         } else if (property == config->tv_mode_property) {
1172                 *val = state->tv.mode;
1173         } else if (property == config->tv_brightness_property) {
1174                 *val = state->tv.brightness;
1175         } else if (property == config->tv_contrast_property) {
1176                 *val = state->tv.contrast;
1177         } else if (property == config->tv_flicker_reduction_property) {
1178                 *val = state->tv.flicker_reduction;
1179         } else if (property == config->tv_overscan_property) {
1180                 *val = state->tv.overscan;
1181         } else if (property == config->tv_saturation_property) {
1182                 *val = state->tv.saturation;
1183         } else if (property == config->tv_hue_property) {
1184                 *val = state->tv.hue;
1185         } else if (connector->funcs->atomic_get_property) {
1186                 return connector->funcs->atomic_get_property(connector,
1187                                 state, property, val);
1188         } else {
1189                 return -EINVAL;
1190         }
1191
1192         return 0;
1193 }
1194
1195 int drm_atomic_get_property(struct drm_mode_object *obj,
1196                 struct drm_property *property, uint64_t *val)
1197 {
1198         struct drm_device *dev = property->dev;
1199         int ret;
1200
1201         switch (obj->type) {
1202         case DRM_MODE_OBJECT_CONNECTOR: {
1203                 struct drm_connector *connector = obj_to_connector(obj);
1204                 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1205                 ret = drm_atomic_connector_get_property(connector,
1206                                 connector->state, property, val);
1207                 break;
1208         }
1209         case DRM_MODE_OBJECT_CRTC: {
1210                 struct drm_crtc *crtc = obj_to_crtc(obj);
1211                 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1212                 ret = drm_atomic_crtc_get_property(crtc,
1213                                 crtc->state, property, val);
1214                 break;
1215         }
1216         case DRM_MODE_OBJECT_PLANE: {
1217                 struct drm_plane *plane = obj_to_plane(obj);
1218                 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1219                 ret = drm_atomic_plane_get_property(plane,
1220                                 plane->state, property, val);
1221                 break;
1222         }
1223         default:
1224                 ret = -EINVAL;
1225                 break;
1226         }
1227
1228         return ret;
1229 }
1230
1231 /**
1232  * drm_atomic_set_crtc_for_plane - set crtc for plane
1233  * @plane_state: the plane whose incoming state to update
1234  * @crtc: crtc to use for the plane
1235  *
1236  * Changing the assigned crtc for a plane requires us to grab the lock and state
1237  * for the new crtc, as needed. This function takes care of all these details
1238  * besides updating the pointer in the state object itself.
1239  *
1240  * Returns:
1241  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1242  * then the w/w mutex code has detected a deadlock and the entire atomic
1243  * sequence must be restarted. All other errors are fatal.
1244  */
1245 int
1246 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1247                               struct drm_crtc *crtc)
1248 {
1249         struct drm_plane *plane = plane_state->plane;
1250         struct drm_crtc_state *crtc_state;
1251
1252         if (plane_state->crtc) {
1253                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1254                                                        plane_state->crtc);
1255                 if (WARN_ON(IS_ERR(crtc_state)))
1256                         return PTR_ERR(crtc_state);
1257
1258                 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1259         }
1260
1261         plane_state->crtc = crtc;
1262
1263         if (crtc) {
1264                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1265                                                        crtc);
1266                 if (IS_ERR(crtc_state))
1267                         return PTR_ERR(crtc_state);
1268                 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1269         }
1270
1271         if (crtc)
1272                 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1273                                  plane_state, crtc->base.id, crtc->name);
1274         else
1275                 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1276                                  plane_state);
1277
1278         return 0;
1279 }
1280 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1281
1282 /**
1283  * drm_atomic_set_fb_for_plane - set framebuffer for plane
1284  * @plane_state: atomic state object for the plane
1285  * @fb: fb to use for the plane
1286  *
1287  * Changing the assigned framebuffer for a plane requires us to grab a reference
1288  * to the new fb and drop the reference to the old fb, if there is one. This
1289  * function takes care of all these details besides updating the pointer in the
1290  * state object itself.
1291  */
1292 void
1293 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1294                             struct drm_framebuffer *fb)
1295 {
1296         if (fb)
1297                 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1298                                  fb->base.id, plane_state);
1299         else
1300                 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1301                                  plane_state);
1302
1303         drm_framebuffer_assign(&plane_state->fb, fb);
1304 }
1305 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1306
1307 /**
1308  * drm_atomic_set_fence_for_plane - set fence for plane
1309  * @plane_state: atomic state object for the plane
1310  * @fence: dma_fence to use for the plane
1311  *
1312  * Helper to setup the plane_state fence in case it is not set yet.
1313  * By using this drivers doesn't need to worry if the user choose
1314  * implicit or explicit fencing.
1315  *
1316  * This function will not set the fence to the state if it was set
1317  * via explicit fencing interfaces on the atomic ioctl. In that case it will
1318  * drop the reference to the fence as we are not storing it anywhere.
1319  * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1320  * with the received implicit fence. In both cases this function consumes a
1321  * reference for @fence.
1322  */
1323 void
1324 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1325                                struct dma_fence *fence)
1326 {
1327         if (plane_state->fence) {
1328                 dma_fence_put(fence);
1329                 return;
1330         }
1331
1332         plane_state->fence = fence;
1333 }
1334 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1335
1336 /**
1337  * drm_atomic_set_crtc_for_connector - set crtc for connector
1338  * @conn_state: atomic state object for the connector
1339  * @crtc: crtc to use for the connector
1340  *
1341  * Changing the assigned crtc for a connector requires us to grab the lock and
1342  * state for the new crtc, as needed. This function takes care of all these
1343  * details besides updating the pointer in the state object itself.
1344  *
1345  * Returns:
1346  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1347  * then the w/w mutex code has detected a deadlock and the entire atomic
1348  * sequence must be restarted. All other errors are fatal.
1349  */
1350 int
1351 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1352                                   struct drm_crtc *crtc)
1353 {
1354         struct drm_crtc_state *crtc_state;
1355
1356         if (conn_state->crtc == crtc)
1357                 return 0;
1358
1359         if (conn_state->crtc) {
1360                 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1361                                                                 conn_state->crtc);
1362
1363                 crtc_state->connector_mask &=
1364                         ~(1 << drm_connector_index(conn_state->connector));
1365
1366                 drm_connector_unreference(conn_state->connector);
1367                 conn_state->crtc = NULL;
1368         }
1369
1370         if (crtc) {
1371                 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1372                 if (IS_ERR(crtc_state))
1373                         return PTR_ERR(crtc_state);
1374
1375                 crtc_state->connector_mask |=
1376                         1 << drm_connector_index(conn_state->connector);
1377
1378                 drm_connector_reference(conn_state->connector);
1379                 conn_state->crtc = crtc;
1380
1381                 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1382                                  conn_state, crtc->base.id, crtc->name);
1383         } else {
1384                 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1385                                  conn_state);
1386         }
1387
1388         return 0;
1389 }
1390 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1391
1392 /**
1393  * drm_atomic_add_affected_connectors - add connectors for crtc
1394  * @state: atomic state
1395  * @crtc: DRM crtc
1396  *
1397  * This function walks the current configuration and adds all connectors
1398  * currently using @crtc to the atomic configuration @state. Note that this
1399  * function must acquire the connection mutex. This can potentially cause
1400  * unneeded seralization if the update is just for the planes on one crtc. Hence
1401  * drivers and helpers should only call this when really needed (e.g. when a
1402  * full modeset needs to happen due to some change).
1403  *
1404  * Returns:
1405  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1406  * then the w/w mutex code has detected a deadlock and the entire atomic
1407  * sequence must be restarted. All other errors are fatal.
1408  */
1409 int
1410 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1411                                    struct drm_crtc *crtc)
1412 {
1413         struct drm_mode_config *config = &state->dev->mode_config;
1414         struct drm_connector *connector;
1415         struct drm_connector_state *conn_state;
1416         struct drm_connector_list_iter conn_iter;
1417         struct drm_crtc_state *crtc_state;
1418         int ret;
1419
1420         crtc_state = drm_atomic_get_crtc_state(state, crtc);
1421         if (IS_ERR(crtc_state))
1422                 return PTR_ERR(crtc_state);
1423
1424         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1425         if (ret)
1426                 return ret;
1427
1428         DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1429                          crtc->base.id, crtc->name, state);
1430
1431         /*
1432          * Changed connectors are already in @state, so only need to look
1433          * at the connector_mask in crtc_state.
1434          */
1435         drm_connector_list_iter_get(state->dev, &conn_iter);
1436         drm_for_each_connector_iter(connector, &conn_iter) {
1437                 if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
1438                         continue;
1439
1440                 conn_state = drm_atomic_get_connector_state(state, connector);
1441                 if (IS_ERR(conn_state)) {
1442                         drm_connector_list_iter_put(&conn_iter);
1443                         return PTR_ERR(conn_state);
1444                 }
1445         }
1446         drm_connector_list_iter_put(&conn_iter);
1447
1448         return 0;
1449 }
1450 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1451
1452 /**
1453  * drm_atomic_add_affected_planes - add planes for crtc
1454  * @state: atomic state
1455  * @crtc: DRM crtc
1456  *
1457  * This function walks the current configuration and adds all planes
1458  * currently used by @crtc to the atomic configuration @state. This is useful
1459  * when an atomic commit also needs to check all currently enabled plane on
1460  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1461  * to avoid special code to force-enable all planes.
1462  *
1463  * Since acquiring a plane state will always also acquire the w/w mutex of the
1464  * current CRTC for that plane (if there is any) adding all the plane states for
1465  * a CRTC will not reduce parallism of atomic updates.
1466  *
1467  * Returns:
1468  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1469  * then the w/w mutex code has detected a deadlock and the entire atomic
1470  * sequence must be restarted. All other errors are fatal.
1471  */
1472 int
1473 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1474                                struct drm_crtc *crtc)
1475 {
1476         struct drm_plane *plane;
1477
1478         WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1479
1480         drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1481                 struct drm_plane_state *plane_state =
1482                         drm_atomic_get_plane_state(state, plane);
1483
1484                 if (IS_ERR(plane_state))
1485                         return PTR_ERR(plane_state);
1486         }
1487         return 0;
1488 }
1489 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1490
1491 /**
1492  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1493  * @state: atomic state
1494  *
1495  * This function should be used by legacy entry points which don't understand
1496  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1497  * the slowpath completed.
1498  */
1499 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1500 {
1501         struct drm_device *dev = state->dev;
1502         unsigned crtc_mask = 0;
1503         struct drm_crtc *crtc;
1504         int ret;
1505         bool global = false;
1506
1507         drm_for_each_crtc(crtc, dev) {
1508                 if (crtc->acquire_ctx != state->acquire_ctx)
1509                         continue;
1510
1511                 crtc_mask |= drm_crtc_mask(crtc);
1512                 crtc->acquire_ctx = NULL;
1513         }
1514
1515         if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1516                 global = true;
1517
1518                 dev->mode_config.acquire_ctx = NULL;
1519         }
1520
1521 retry:
1522         drm_modeset_backoff(state->acquire_ctx);
1523
1524         ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
1525         if (ret)
1526                 goto retry;
1527
1528         drm_for_each_crtc(crtc, dev)
1529                 if (drm_crtc_mask(crtc) & crtc_mask)
1530                         crtc->acquire_ctx = state->acquire_ctx;
1531
1532         if (global)
1533                 dev->mode_config.acquire_ctx = state->acquire_ctx;
1534 }
1535 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1536
1537 /**
1538  * drm_atomic_check_only - check whether a given config would work
1539  * @state: atomic configuration to check
1540  *
1541  * Note that this function can return -EDEADLK if the driver needed to acquire
1542  * more locks but encountered a deadlock. The caller must then do the usual w/w
1543  * backoff dance and restart. All other errors are fatal.
1544  *
1545  * Returns:
1546  * 0 on success, negative error code on failure.
1547  */
1548 int drm_atomic_check_only(struct drm_atomic_state *state)
1549 {
1550         struct drm_device *dev = state->dev;
1551         struct drm_mode_config *config = &dev->mode_config;
1552         struct drm_plane *plane;
1553         struct drm_plane_state *plane_state;
1554         struct drm_crtc *crtc;
1555         struct drm_crtc_state *crtc_state;
1556         int i, ret = 0;
1557
1558         DRM_DEBUG_ATOMIC("checking %p\n", state);
1559
1560         for_each_new_plane_in_state(state, plane, plane_state, i) {
1561                 ret = drm_atomic_plane_check(plane, plane_state);
1562                 if (ret) {
1563                         DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1564                                          plane->base.id, plane->name);
1565                         return ret;
1566                 }
1567         }
1568
1569         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1570                 ret = drm_atomic_crtc_check(crtc, crtc_state);
1571                 if (ret) {
1572                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1573                                          crtc->base.id, crtc->name);
1574                         return ret;
1575                 }
1576         }
1577
1578         if (config->funcs->atomic_check)
1579                 ret = config->funcs->atomic_check(state->dev, state);
1580
1581         if (!state->allow_modeset) {
1582                 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1583                         if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1584                                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1585                                                  crtc->base.id, crtc->name);
1586                                 return -EINVAL;
1587                         }
1588                 }
1589         }
1590
1591         return ret;
1592 }
1593 EXPORT_SYMBOL(drm_atomic_check_only);
1594
1595 /**
1596  * drm_atomic_commit - commit configuration atomically
1597  * @state: atomic configuration to check
1598  *
1599  * Note that this function can return -EDEADLK if the driver needed to acquire
1600  * more locks but encountered a deadlock. The caller must then do the usual w/w
1601  * backoff dance and restart. All other errors are fatal.
1602  *
1603  * This function will take its own reference on @state.
1604  * Callers should always release their reference with drm_atomic_state_put().
1605  *
1606  * Returns:
1607  * 0 on success, negative error code on failure.
1608  */
1609 int drm_atomic_commit(struct drm_atomic_state *state)
1610 {
1611         struct drm_mode_config *config = &state->dev->mode_config;
1612         int ret;
1613
1614         ret = drm_atomic_check_only(state);
1615         if (ret)
1616                 return ret;
1617
1618         DRM_DEBUG_ATOMIC("commiting %p\n", state);
1619
1620         return config->funcs->atomic_commit(state->dev, state, false);
1621 }
1622 EXPORT_SYMBOL(drm_atomic_commit);
1623
1624 /**
1625  * drm_atomic_nonblocking_commit - atomic nonblocking commit
1626  * @state: atomic configuration to check
1627  *
1628  * Note that this function can return -EDEADLK if the driver needed to acquire
1629  * more locks but encountered a deadlock. The caller must then do the usual w/w
1630  * backoff dance and restart. All other errors are fatal.
1631  *
1632  * This function will take its own reference on @state.
1633  * Callers should always release their reference with drm_atomic_state_put().
1634  *
1635  * Returns:
1636  * 0 on success, negative error code on failure.
1637  */
1638 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1639 {
1640         struct drm_mode_config *config = &state->dev->mode_config;
1641         int ret;
1642
1643         ret = drm_atomic_check_only(state);
1644         if (ret)
1645                 return ret;
1646
1647         DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
1648
1649         return config->funcs->atomic_commit(state->dev, state, true);
1650 }
1651 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1652
1653 static void drm_atomic_print_state(const struct drm_atomic_state *state)
1654 {
1655         struct drm_printer p = drm_info_printer(state->dev->dev);
1656         struct drm_plane *plane;
1657         struct drm_plane_state *plane_state;
1658         struct drm_crtc *crtc;
1659         struct drm_crtc_state *crtc_state;
1660         struct drm_connector *connector;
1661         struct drm_connector_state *connector_state;
1662         int i;
1663
1664         DRM_DEBUG_ATOMIC("checking %p\n", state);
1665
1666         for_each_new_plane_in_state(state, plane, plane_state, i)
1667                 drm_atomic_plane_print_state(&p, plane_state);
1668
1669         for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1670                 drm_atomic_crtc_print_state(&p, crtc_state);
1671
1672         for_each_new_connector_in_state(state, connector, connector_state, i)
1673                 drm_atomic_connector_print_state(&p, connector_state);
1674 }
1675
1676 /**
1677  * drm_state_dump - dump entire device atomic state
1678  * @dev: the drm device
1679  * @p: where to print the state to
1680  *
1681  * Just for debugging.  Drivers might want an option to dump state
1682  * to dmesg in case of error irq's.  (Hint, you probably want to
1683  * ratelimit this!)
1684  *
1685  * The caller must drm_modeset_lock_all(), or if this is called
1686  * from error irq handler, it should not be enabled by default.
1687  * (Ie. if you are debugging errors you might not care that this
1688  * is racey.  But calling this without all modeset locks held is
1689  * not inherently safe.)
1690  */
1691 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1692 {
1693         struct drm_mode_config *config = &dev->mode_config;
1694         struct drm_plane *plane;
1695         struct drm_crtc *crtc;
1696         struct drm_connector *connector;
1697         struct drm_connector_list_iter conn_iter;
1698
1699         if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1700                 return;
1701
1702         list_for_each_entry(plane, &config->plane_list, head)
1703                 drm_atomic_plane_print_state(p, plane->state);
1704
1705         list_for_each_entry(crtc, &config->crtc_list, head)
1706                 drm_atomic_crtc_print_state(p, crtc->state);
1707
1708         drm_connector_list_iter_get(dev, &conn_iter);
1709         drm_for_each_connector_iter(connector, &conn_iter)
1710                 drm_atomic_connector_print_state(p, connector->state);
1711         drm_connector_list_iter_put(&conn_iter);
1712 }
1713 EXPORT_SYMBOL(drm_state_dump);
1714
1715 #ifdef CONFIG_DEBUG_FS
1716 static int drm_state_info(struct seq_file *m, void *data)
1717 {
1718         struct drm_info_node *node = (struct drm_info_node *) m->private;
1719         struct drm_device *dev = node->minor->dev;
1720         struct drm_printer p = drm_seq_file_printer(m);
1721
1722         drm_modeset_lock_all(dev);
1723         drm_state_dump(dev, &p);
1724         drm_modeset_unlock_all(dev);
1725
1726         return 0;
1727 }
1728
1729 /* any use in debugfs files to dump individual planes/crtc/etc? */
1730 static const struct drm_info_list drm_atomic_debugfs_list[] = {
1731         {"state", drm_state_info, 0},
1732 };
1733
1734 int drm_atomic_debugfs_init(struct drm_minor *minor)
1735 {
1736         return drm_debugfs_create_files(drm_atomic_debugfs_list,
1737                         ARRAY_SIZE(drm_atomic_debugfs_list),
1738                         minor->debugfs_root, minor);
1739 }
1740 #endif
1741
1742 /*
1743  * The big monstor ioctl
1744  */
1745
1746 static struct drm_pending_vblank_event *create_vblank_event(
1747                 struct drm_device *dev, uint64_t user_data)
1748 {
1749         struct drm_pending_vblank_event *e = NULL;
1750
1751         e = kzalloc(sizeof *e, GFP_KERNEL);
1752         if (!e)
1753                 return NULL;
1754
1755         e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1756         e->event.base.length = sizeof(e->event);
1757         e->event.user_data = user_data;
1758
1759         return e;
1760 }
1761
1762 static int atomic_set_prop(struct drm_atomic_state *state,
1763                 struct drm_mode_object *obj, struct drm_property *prop,
1764                 uint64_t prop_value)
1765 {
1766         struct drm_mode_object *ref;
1767         int ret;
1768
1769         if (!drm_property_change_valid_get(prop, prop_value, &ref))
1770                 return -EINVAL;
1771
1772         switch (obj->type) {
1773         case DRM_MODE_OBJECT_CONNECTOR: {
1774                 struct drm_connector *connector = obj_to_connector(obj);
1775                 struct drm_connector_state *connector_state;
1776
1777                 connector_state = drm_atomic_get_connector_state(state, connector);
1778                 if (IS_ERR(connector_state)) {
1779                         ret = PTR_ERR(connector_state);
1780                         break;
1781                 }
1782
1783                 ret = drm_atomic_connector_set_property(connector,
1784                                 connector_state, prop, prop_value);
1785                 break;
1786         }
1787         case DRM_MODE_OBJECT_CRTC: {
1788                 struct drm_crtc *crtc = obj_to_crtc(obj);
1789                 struct drm_crtc_state *crtc_state;
1790
1791                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1792                 if (IS_ERR(crtc_state)) {
1793                         ret = PTR_ERR(crtc_state);
1794                         break;
1795                 }
1796
1797                 ret = drm_atomic_crtc_set_property(crtc,
1798                                 crtc_state, prop, prop_value);
1799                 break;
1800         }
1801         case DRM_MODE_OBJECT_PLANE: {
1802                 struct drm_plane *plane = obj_to_plane(obj);
1803                 struct drm_plane_state *plane_state;
1804
1805                 plane_state = drm_atomic_get_plane_state(state, plane);
1806                 if (IS_ERR(plane_state)) {
1807                         ret = PTR_ERR(plane_state);
1808                         break;
1809                 }
1810
1811                 ret = drm_atomic_plane_set_property(plane,
1812                                 plane_state, prop, prop_value);
1813                 break;
1814         }
1815         default:
1816                 ret = -EINVAL;
1817                 break;
1818         }
1819
1820         drm_property_change_valid_put(prop, ref);
1821         return ret;
1822 }
1823
1824 /**
1825  * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1826  *
1827  * @dev: drm device to check.
1828  * @plane_mask: plane mask for planes that were updated.
1829  * @ret: return value, can be -EDEADLK for a retry.
1830  *
1831  * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1832  * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1833  * is a common operation for each atomic update, so this call is split off as a
1834  * helper.
1835  */
1836 void drm_atomic_clean_old_fb(struct drm_device *dev,
1837                              unsigned plane_mask,
1838                              int ret)
1839 {
1840         struct drm_plane *plane;
1841
1842         /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1843          * locks (ie. while it is still safe to deref plane->state).  We
1844          * need to do this here because the driver entry points cannot
1845          * distinguish between legacy and atomic ioctls.
1846          */
1847         drm_for_each_plane_mask(plane, dev, plane_mask) {
1848                 if (ret == 0) {
1849                         struct drm_framebuffer *new_fb = plane->state->fb;
1850                         if (new_fb)
1851                                 drm_framebuffer_reference(new_fb);
1852                         plane->fb = new_fb;
1853                         plane->crtc = plane->state->crtc;
1854
1855                         if (plane->old_fb)
1856                                 drm_framebuffer_unreference(plane->old_fb);
1857                 }
1858                 plane->old_fb = NULL;
1859         }
1860 }
1861 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1862
1863 /**
1864  * DOC: explicit fencing properties
1865  *
1866  * Explicit fencing allows userspace to control the buffer synchronization
1867  * between devices. A Fence or a group of fences are transfered to/from
1868  * userspace using Sync File fds and there are two DRM properties for that.
1869  * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1870  * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1871  *
1872  * As a contrast, with implicit fencing the kernel keeps track of any
1873  * ongoing rendering, and automatically ensures that the atomic update waits
1874  * for any pending rendering to complete. For shared buffers represented with
1875  * a &struct dma_buf this is tracked in &struct reservation_object.
1876  * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1877  * whereas explicit fencing is what Android wants.
1878  *
1879  * "IN_FENCE_FD”:
1880  *      Use this property to pass a fence that DRM should wait on before
1881  *      proceeding with the Atomic Commit request and show the framebuffer for
1882  *      the plane on the screen. The fence can be either a normal fence or a
1883  *      merged one, the sync_file framework will handle both cases and use a
1884  *      fence_array if a merged fence is received. Passing -1 here means no
1885  *      fences to wait on.
1886  *
1887  *      If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1888  *      it will only check if the Sync File is a valid one.
1889  *
1890  *      On the driver side the fence is stored on the @fence parameter of
1891  *      &struct drm_plane_state. Drivers which also support implicit fencing
1892  *      should set the implicit fence using drm_atomic_set_fence_for_plane(),
1893  *      to make sure there's consistent behaviour between drivers in precedence
1894  *      of implicit vs. explicit fencing.
1895  *
1896  * "OUT_FENCE_PTR”:
1897  *      Use this property to pass a file descriptor pointer to DRM. Once the
1898  *      Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1899  *      the file descriptor number of a Sync File. This Sync File contains the
1900  *      CRTC fence that will be signaled when all framebuffers present on the
1901  *      Atomic Commit * request for that given CRTC are scanned out on the
1902  *      screen.
1903  *
1904  *      The Atomic Commit request fails if a invalid pointer is passed. If the
1905  *      Atomic Commit request fails for any other reason the out fence fd
1906  *      returned will be -1. On a Atomic Commit with the
1907  *      DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1908  *
1909  *      Note that out-fences don't have a special interface to drivers and are
1910  *      internally represented by a &struct drm_pending_vblank_event in struct
1911  *      &drm_crtc_state, which is also used by the nonblocking atomic commit
1912  *      helpers and for the DRM event handling for existing userspace.
1913  */
1914
1915 struct drm_out_fence_state {
1916         s64 __user *out_fence_ptr;
1917         struct sync_file *sync_file;
1918         int fd;
1919 };
1920
1921 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1922                            struct dma_fence *fence)
1923 {
1924         fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1925         if (fence_state->fd < 0)
1926                 return fence_state->fd;
1927
1928         if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1929                 return -EFAULT;
1930
1931         fence_state->sync_file = sync_file_create(fence);
1932         if (!fence_state->sync_file)
1933                 return -ENOMEM;
1934
1935         return 0;
1936 }
1937
1938 static int prepare_crtc_signaling(struct drm_device *dev,
1939                                   struct drm_atomic_state *state,
1940                                   struct drm_mode_atomic *arg,
1941                                   struct drm_file *file_priv,
1942                                   struct drm_out_fence_state **fence_state,
1943                                   unsigned int *num_fences)
1944 {
1945         struct drm_crtc *crtc;
1946         struct drm_crtc_state *crtc_state;
1947         int i, ret;
1948
1949         if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1950                 return 0;
1951
1952         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1953                 u64 __user *fence_ptr;
1954
1955                 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1956
1957                 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1958                         struct drm_pending_vblank_event *e;
1959
1960                         e = create_vblank_event(dev, arg->user_data);
1961                         if (!e)
1962                                 return -ENOMEM;
1963
1964                         crtc_state->event = e;
1965                 }
1966
1967                 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1968                         struct drm_pending_vblank_event *e = crtc_state->event;
1969
1970                         if (!file_priv)
1971                                 continue;
1972
1973                         ret = drm_event_reserve_init(dev, file_priv, &e->base,
1974                                                      &e->event.base);
1975                         if (ret) {
1976                                 kfree(e);
1977                                 crtc_state->event = NULL;
1978                                 return ret;
1979                         }
1980                 }
1981
1982                 if (fence_ptr) {
1983                         struct dma_fence *fence;
1984                         struct drm_out_fence_state *f;
1985
1986                         f = krealloc(*fence_state, sizeof(**fence_state) *
1987                                      (*num_fences + 1), GFP_KERNEL);
1988                         if (!f)
1989                                 return -ENOMEM;
1990
1991                         memset(&f[*num_fences], 0, sizeof(*f));
1992
1993                         f[*num_fences].out_fence_ptr = fence_ptr;
1994                         *fence_state = f;
1995
1996                         fence = drm_crtc_create_fence(crtc);
1997                         if (!fence)
1998                                 return -ENOMEM;
1999
2000                         ret = setup_out_fence(&f[(*num_fences)++], fence);
2001                         if (ret) {
2002                                 dma_fence_put(fence);
2003                                 return ret;
2004                         }
2005
2006                         crtc_state->event->base.fence = fence;
2007                 }
2008         }
2009
2010         return 0;
2011 }
2012
2013 static void complete_crtc_signaling(struct drm_device *dev,
2014                                     struct drm_atomic_state *state,
2015                                     struct drm_out_fence_state *fence_state,
2016                                     unsigned int num_fences,
2017                                     bool install_fds)
2018 {
2019         struct drm_crtc *crtc;
2020         struct drm_crtc_state *crtc_state;
2021         int i;
2022
2023         if (install_fds) {
2024                 for (i = 0; i < num_fences; i++)
2025                         fd_install(fence_state[i].fd,
2026                                    fence_state[i].sync_file->file);
2027
2028                 kfree(fence_state);
2029                 return;
2030         }
2031
2032         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2033                 /*
2034                  * TEST_ONLY and PAGE_FLIP_EVENT are mutually
2035                  * exclusive, if they weren't, this code should be
2036                  * called on success for TEST_ONLY too.
2037                  */
2038                 if (crtc_state->event)
2039                         drm_event_cancel_free(dev, &crtc_state->event->base);
2040         }
2041
2042         if (!fence_state)
2043                 return;
2044
2045         for (i = 0; i < num_fences; i++) {
2046                 if (fence_state[i].sync_file)
2047                         fput(fence_state[i].sync_file->file);
2048                 if (fence_state[i].fd >= 0)
2049                         put_unused_fd(fence_state[i].fd);
2050
2051                 /* If this fails log error to the user */
2052                 if (fence_state[i].out_fence_ptr &&
2053                     put_user(-1, fence_state[i].out_fence_ptr))
2054                         DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2055         }
2056
2057         kfree(fence_state);
2058 }
2059
2060 int drm_mode_atomic_ioctl(struct drm_device *dev,
2061                           void *data, struct drm_file *file_priv)
2062 {
2063         struct drm_mode_atomic *arg = data;
2064         uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2065         uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2066         uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2067         uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2068         unsigned int copied_objs, copied_props;
2069         struct drm_atomic_state *state;
2070         struct drm_modeset_acquire_ctx ctx;
2071         struct drm_plane *plane;
2072         struct drm_out_fence_state *fence_state = NULL;
2073         unsigned plane_mask;
2074         int ret = 0;
2075         unsigned int i, j, num_fences = 0;
2076
2077         /* disallow for drivers not supporting atomic: */
2078         if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2079                 return -EINVAL;
2080
2081         /* disallow for userspace that has not enabled atomic cap (even
2082          * though this may be a bit overkill, since legacy userspace
2083          * wouldn't know how to call this ioctl)
2084          */
2085         if (!file_priv->atomic)
2086                 return -EINVAL;
2087
2088         if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2089                 return -EINVAL;
2090
2091         if (arg->reserved)
2092                 return -EINVAL;
2093
2094         if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2095                         !dev->mode_config.async_page_flip)
2096                 return -EINVAL;
2097
2098         /* can't test and expect an event at the same time. */
2099         if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2100                         (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2101                 return -EINVAL;
2102
2103         drm_modeset_acquire_init(&ctx, 0);
2104
2105         state = drm_atomic_state_alloc(dev);
2106         if (!state)
2107                 return -ENOMEM;
2108
2109         state->acquire_ctx = &ctx;
2110         state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2111
2112 retry:
2113         plane_mask = 0;
2114         copied_objs = 0;
2115         copied_props = 0;
2116
2117         for (i = 0; i < arg->count_objs; i++) {
2118                 uint32_t obj_id, count_props;
2119                 struct drm_mode_object *obj;
2120
2121                 if (get_user(obj_id, objs_ptr + copied_objs)) {
2122                         ret = -EFAULT;
2123                         goto out;
2124                 }
2125
2126                 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
2127                 if (!obj) {
2128                         ret = -ENOENT;
2129                         goto out;
2130                 }
2131
2132                 if (!obj->properties) {
2133                         drm_mode_object_unreference(obj);
2134                         ret = -ENOENT;
2135                         goto out;
2136                 }
2137
2138                 if (get_user(count_props, count_props_ptr + copied_objs)) {
2139                         drm_mode_object_unreference(obj);
2140                         ret = -EFAULT;
2141                         goto out;
2142                 }
2143
2144                 copied_objs++;
2145
2146                 for (j = 0; j < count_props; j++) {
2147                         uint32_t prop_id;
2148                         uint64_t prop_value;
2149                         struct drm_property *prop;
2150
2151                         if (get_user(prop_id, props_ptr + copied_props)) {
2152                                 drm_mode_object_unreference(obj);
2153                                 ret = -EFAULT;
2154                                 goto out;
2155                         }
2156
2157                         prop = drm_mode_obj_find_prop_id(obj, prop_id);
2158                         if (!prop) {
2159                                 drm_mode_object_unreference(obj);
2160                                 ret = -ENOENT;
2161                                 goto out;
2162                         }
2163
2164                         if (copy_from_user(&prop_value,
2165                                            prop_values_ptr + copied_props,
2166                                            sizeof(prop_value))) {
2167                                 drm_mode_object_unreference(obj);
2168                                 ret = -EFAULT;
2169                                 goto out;
2170                         }
2171
2172                         ret = atomic_set_prop(state, obj, prop, prop_value);
2173                         if (ret) {
2174                                 drm_mode_object_unreference(obj);
2175                                 goto out;
2176                         }
2177
2178                         copied_props++;
2179                 }
2180
2181                 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2182                     !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
2183                         plane = obj_to_plane(obj);
2184                         plane_mask |= (1 << drm_plane_index(plane));
2185                         plane->old_fb = plane->fb;
2186                 }
2187                 drm_mode_object_unreference(obj);
2188         }
2189
2190         ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2191                                      &num_fences);
2192         if (ret)
2193                 goto out;
2194
2195         if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2196                 ret = drm_atomic_check_only(state);
2197         } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
2198                 ret = drm_atomic_nonblocking_commit(state);
2199         } else {
2200                 if (unlikely(drm_debug & DRM_UT_STATE))
2201                         drm_atomic_print_state(state);
2202
2203                 ret = drm_atomic_commit(state);
2204         }
2205
2206 out:
2207         drm_atomic_clean_old_fb(dev, plane_mask, ret);
2208
2209         complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
2210
2211         if (ret == -EDEADLK) {
2212                 drm_atomic_state_clear(state);
2213                 drm_modeset_backoff(&ctx);
2214                 goto retry;
2215         }
2216
2217         drm_atomic_state_put(state);
2218
2219         drm_modeset_drop_locks(&ctx);
2220         drm_modeset_acquire_fini(&ctx);
2221
2222         return ret;
2223 }