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