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