drm/fence: add fence to drm_pending_event
[cascardo/linux.git] / drivers / gpu / drm / drm_atomic.c
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  * Rob Clark <robdclark@gmail.com>
25  * Daniel Vetter <daniel.vetter@ffwll.ch>
26  */
27
28
29 #include <drm/drmP.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_mode.h>
32 #include <drm/drm_plane_helper.h>
33
34 #include "drm_crtc_internal.h"
35
36 /**
37  * drm_atomic_state_default_release -
38  * release memory initialized by drm_atomic_state_init
39  * @state: atomic state
40  *
41  * Free all the memory allocated by drm_atomic_state_init.
42  * This is useful for drivers that subclass the atomic state.
43  */
44 void drm_atomic_state_default_release(struct drm_atomic_state *state)
45 {
46         kfree(state->connectors);
47         kfree(state->crtcs);
48         kfree(state->planes);
49 }
50 EXPORT_SYMBOL(drm_atomic_state_default_release);
51
52 /**
53  * drm_atomic_state_init - init new atomic state
54  * @dev: DRM device
55  * @state: atomic state
56  *
57  * Default implementation for filling in a new atomic state.
58  * This is useful for drivers that subclass the atomic state.
59  */
60 int
61 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
62 {
63         /* TODO legacy paths should maybe do a better job about
64          * setting this appropriately?
65          */
66         state->allow_modeset = true;
67
68         state->crtcs = kcalloc(dev->mode_config.num_crtc,
69                                sizeof(*state->crtcs), GFP_KERNEL);
70         if (!state->crtcs)
71                 goto fail;
72         state->planes = kcalloc(dev->mode_config.num_total_plane,
73                                 sizeof(*state->planes), GFP_KERNEL);
74         if (!state->planes)
75                 goto fail;
76
77         state->dev = dev;
78
79         DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
80
81         return 0;
82 fail:
83         drm_atomic_state_default_release(state);
84         return -ENOMEM;
85 }
86 EXPORT_SYMBOL(drm_atomic_state_init);
87
88 /**
89  * drm_atomic_state_alloc - allocate atomic state
90  * @dev: DRM device
91  *
92  * This allocates an empty atomic state to track updates.
93  */
94 struct drm_atomic_state *
95 drm_atomic_state_alloc(struct drm_device *dev)
96 {
97         struct drm_mode_config *config = &dev->mode_config;
98         struct drm_atomic_state *state;
99
100         if (!config->funcs->atomic_state_alloc) {
101                 state = kzalloc(sizeof(*state), GFP_KERNEL);
102                 if (!state)
103                         return NULL;
104                 if (drm_atomic_state_init(dev, state) < 0) {
105                         kfree(state);
106                         return NULL;
107                 }
108                 return state;
109         }
110
111         return config->funcs->atomic_state_alloc(dev);
112 }
113 EXPORT_SYMBOL(drm_atomic_state_alloc);
114
115 /**
116  * drm_atomic_state_default_clear - clear base atomic state
117  * @state: atomic state
118  *
119  * Default implementation for clearing atomic state.
120  * This is useful for drivers that subclass the atomic state.
121  */
122 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
123 {
124         struct drm_device *dev = state->dev;
125         struct drm_mode_config *config = &dev->mode_config;
126         int i;
127
128         DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
129
130         for (i = 0; i < state->num_connector; i++) {
131                 struct drm_connector *connector = state->connectors[i].ptr;
132
133                 if (!connector)
134                         continue;
135
136                 connector->funcs->atomic_destroy_state(connector,
137                                                        state->connectors[i].state);
138                 state->connectors[i].ptr = NULL;
139                 state->connectors[i].state = NULL;
140                 drm_connector_unreference(connector);
141         }
142
143         for (i = 0; i < config->num_crtc; i++) {
144                 struct drm_crtc *crtc = state->crtcs[i].ptr;
145
146                 if (!crtc)
147                         continue;
148
149                 crtc->funcs->atomic_destroy_state(crtc,
150                                                   state->crtcs[i].state);
151                 state->crtcs[i].ptr = NULL;
152                 state->crtcs[i].state = NULL;
153         }
154
155         for (i = 0; i < config->num_total_plane; i++) {
156                 struct drm_plane *plane = state->planes[i].ptr;
157
158                 if (!plane)
159                         continue;
160
161                 plane->funcs->atomic_destroy_state(plane,
162                                                    state->planes[i].state);
163                 state->planes[i].ptr = NULL;
164                 state->planes[i].state = NULL;
165         }
166 }
167 EXPORT_SYMBOL(drm_atomic_state_default_clear);
168
169 /**
170  * drm_atomic_state_clear - clear state object
171  * @state: atomic state
172  *
173  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
174  * all locks. So someone else could sneak in and change the current modeset
175  * configuration. Which means that all the state assembled in @state is no
176  * longer an atomic update to the current state, but to some arbitrary earlier
177  * state. Which could break assumptions the driver's ->atomic_check likely
178  * relies on.
179  *
180  * Hence we must clear all cached state and completely start over, using this
181  * function.
182  */
183 void drm_atomic_state_clear(struct drm_atomic_state *state)
184 {
185         struct drm_device *dev = state->dev;
186         struct drm_mode_config *config = &dev->mode_config;
187
188         if (config->funcs->atomic_state_clear)
189                 config->funcs->atomic_state_clear(state);
190         else
191                 drm_atomic_state_default_clear(state);
192 }
193 EXPORT_SYMBOL(drm_atomic_state_clear);
194
195 /**
196  * drm_atomic_state_free - free all memory for an atomic state
197  * @state: atomic state to deallocate
198  *
199  * This frees all memory associated with an atomic state, including all the
200  * per-object state for planes, crtcs and connectors.
201  */
202 void drm_atomic_state_free(struct drm_atomic_state *state)
203 {
204         struct drm_device *dev;
205         struct drm_mode_config *config;
206
207         if (!state)
208                 return;
209
210         dev = state->dev;
211         config = &dev->mode_config;
212
213         drm_atomic_state_clear(state);
214
215         DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
216
217         if (config->funcs->atomic_state_free) {
218                 config->funcs->atomic_state_free(state);
219         } else {
220                 drm_atomic_state_default_release(state);
221                 kfree(state);
222         }
223 }
224 EXPORT_SYMBOL(drm_atomic_state_free);
225
226 /**
227  * drm_atomic_get_crtc_state - get crtc state
228  * @state: global atomic state object
229  * @crtc: crtc to get state object for
230  *
231  * This function returns the crtc state for the given crtc, allocating it if
232  * needed. It will also grab the relevant crtc lock to make sure that the state
233  * is consistent.
234  *
235  * Returns:
236  *
237  * Either the allocated state or the error code encoded into the pointer. When
238  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
239  * entire atomic sequence must be restarted. All other errors are fatal.
240  */
241 struct drm_crtc_state *
242 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
243                           struct drm_crtc *crtc)
244 {
245         int ret, index = drm_crtc_index(crtc);
246         struct drm_crtc_state *crtc_state;
247
248         WARN_ON(!state->acquire_ctx);
249
250         crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
251         if (crtc_state)
252                 return crtc_state;
253
254         ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
255         if (ret)
256                 return ERR_PTR(ret);
257
258         crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
259         if (!crtc_state)
260                 return ERR_PTR(-ENOMEM);
261
262         state->crtcs[index].state = crtc_state;
263         state->crtcs[index].ptr = crtc;
264         crtc_state->state = state;
265
266         DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
267                          crtc->base.id, crtc->name, crtc_state, state);
268
269         return crtc_state;
270 }
271 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
272
273 /**
274  * drm_atomic_set_mode_for_crtc - set mode for CRTC
275  * @state: the CRTC whose incoming state to update
276  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
277  *
278  * Set a mode (originating from the kernel) on the desired CRTC state. Does
279  * not change any other state properties, including enable, active, or
280  * mode_changed.
281  *
282  * RETURNS:
283  * Zero on success, error code on failure. Cannot return -EDEADLK.
284  */
285 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
286                                  struct drm_display_mode *mode)
287 {
288         struct drm_mode_modeinfo umode;
289
290         /* Early return for no change. */
291         if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
292                 return 0;
293
294         drm_property_unreference_blob(state->mode_blob);
295         state->mode_blob = NULL;
296
297         if (mode) {
298                 drm_mode_convert_to_umode(&umode, mode);
299                 state->mode_blob =
300                         drm_property_create_blob(state->crtc->dev,
301                                                  sizeof(umode),
302                                                  &umode);
303                 if (IS_ERR(state->mode_blob))
304                         return PTR_ERR(state->mode_blob);
305
306                 drm_mode_copy(&state->mode, mode);
307                 state->enable = true;
308                 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
309                                  mode->name, state);
310         } else {
311                 memset(&state->mode, 0, sizeof(state->mode));
312                 state->enable = false;
313                 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
314                                  state);
315         }
316
317         return 0;
318 }
319 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
320
321 /**
322  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
323  * @state: the CRTC whose incoming state to update
324  * @blob: pointer to blob property to use for mode
325  *
326  * Set a mode (originating from a blob property) on the desired CRTC state.
327  * This function will take a reference on the blob property for the CRTC state,
328  * and release the reference held on the state's existing mode property, if any
329  * was set.
330  *
331  * RETURNS:
332  * Zero on success, error code on failure. Cannot return -EDEADLK.
333  */
334 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
335                                       struct drm_property_blob *blob)
336 {
337         if (blob == state->mode_blob)
338                 return 0;
339
340         drm_property_unreference_blob(state->mode_blob);
341         state->mode_blob = NULL;
342
343         if (blob) {
344                 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
345                     drm_mode_convert_umode(&state->mode,
346                                            (const struct drm_mode_modeinfo *)
347                                             blob->data))
348                         return -EINVAL;
349
350                 state->mode_blob = drm_property_reference_blob(blob);
351                 state->enable = true;
352                 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
353                                  state->mode.name, state);
354         } else {
355                 memset(&state->mode, 0, sizeof(state->mode));
356                 state->enable = false;
357                 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
358                                  state);
359         }
360
361         return 0;
362 }
363 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
364
365 /**
366  * drm_atomic_replace_property_blob - replace a blob property
367  * @blob: a pointer to the member blob to be replaced
368  * @new_blob: the new blob to replace with
369  * @replaced: whether the blob has been replaced
370  *
371  * RETURNS:
372  * Zero on success, error code on failure
373  */
374 static void
375 drm_atomic_replace_property_blob(struct drm_property_blob **blob,
376                                  struct drm_property_blob *new_blob,
377                                  bool *replaced)
378 {
379         struct drm_property_blob *old_blob = *blob;
380
381         if (old_blob == new_blob)
382                 return;
383
384         if (old_blob)
385                 drm_property_unreference_blob(old_blob);
386         if (new_blob)
387                 drm_property_reference_blob(new_blob);
388         *blob = new_blob;
389         *replaced = true;
390
391         return;
392 }
393
394 static int
395 drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
396                                          struct drm_property_blob **blob,
397                                          uint64_t blob_id,
398                                          ssize_t expected_size,
399                                          bool *replaced)
400 {
401         struct drm_device *dev = crtc->dev;
402         struct drm_property_blob *new_blob = NULL;
403
404         if (blob_id != 0) {
405                 new_blob = drm_property_lookup_blob(dev, blob_id);
406                 if (new_blob == NULL)
407                         return -EINVAL;
408                 if (expected_size > 0 && expected_size != new_blob->length)
409                         return -EINVAL;
410         }
411
412         drm_atomic_replace_property_blob(blob, new_blob, replaced);
413
414         return 0;
415 }
416
417 /**
418  * drm_atomic_crtc_set_property - set property on CRTC
419  * @crtc: the drm CRTC to set a property on
420  * @state: the state object to update with the new property value
421  * @property: the property to set
422  * @val: the new property value
423  *
424  * Use this instead of calling crtc->atomic_set_property directly.
425  * This function handles generic/core properties and calls out to
426  * driver's ->atomic_set_property() for driver properties.  To ensure
427  * consistent behavior you must call this function rather than the
428  * driver hook directly.
429  *
430  * RETURNS:
431  * Zero on success, error code on failure
432  */
433 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
434                 struct drm_crtc_state *state, struct drm_property *property,
435                 uint64_t val)
436 {
437         struct drm_device *dev = crtc->dev;
438         struct drm_mode_config *config = &dev->mode_config;
439         bool replaced = false;
440         int ret;
441
442         if (property == config->prop_active)
443                 state->active = val;
444         else if (property == config->prop_mode_id) {
445                 struct drm_property_blob *mode =
446                         drm_property_lookup_blob(dev, val);
447                 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
448                 drm_property_unreference_blob(mode);
449                 return ret;
450         } else if (property == config->degamma_lut_property) {
451                 ret = drm_atomic_replace_property_blob_from_id(crtc,
452                                         &state->degamma_lut,
453                                         val,
454                                         -1,
455                                         &replaced);
456                 state->color_mgmt_changed = replaced;
457                 return ret;
458         } else if (property == config->ctm_property) {
459                 ret = drm_atomic_replace_property_blob_from_id(crtc,
460                                         &state->ctm,
461                                         val,
462                                         sizeof(struct drm_color_ctm),
463                                         &replaced);
464                 state->color_mgmt_changed = replaced;
465                 return ret;
466         } else if (property == config->gamma_lut_property) {
467                 ret = drm_atomic_replace_property_blob_from_id(crtc,
468                                         &state->gamma_lut,
469                                         val,
470                                         -1,
471                                         &replaced);
472                 state->color_mgmt_changed = replaced;
473                 return ret;
474         } else if (crtc->funcs->atomic_set_property)
475                 return crtc->funcs->atomic_set_property(crtc, state, property, val);
476         else
477                 return -EINVAL;
478
479         return 0;
480 }
481 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
482
483 /**
484  * drm_atomic_crtc_get_property - get property value from CRTC state
485  * @crtc: the drm CRTC to set a property on
486  * @state: the state object to get the property value from
487  * @property: the property to set
488  * @val: return location for the property value
489  *
490  * This function handles generic/core properties and calls out to
491  * driver's ->atomic_get_property() for driver properties.  To ensure
492  * consistent behavior you must call this function rather than the
493  * driver hook directly.
494  *
495  * RETURNS:
496  * Zero on success, error code on failure
497  */
498 static int
499 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
500                 const struct drm_crtc_state *state,
501                 struct drm_property *property, uint64_t *val)
502 {
503         struct drm_device *dev = crtc->dev;
504         struct drm_mode_config *config = &dev->mode_config;
505
506         if (property == config->prop_active)
507                 *val = state->active;
508         else if (property == config->prop_mode_id)
509                 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
510         else if (property == config->degamma_lut_property)
511                 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
512         else if (property == config->ctm_property)
513                 *val = (state->ctm) ? state->ctm->base.id : 0;
514         else if (property == config->gamma_lut_property)
515                 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
516         else if (crtc->funcs->atomic_get_property)
517                 return crtc->funcs->atomic_get_property(crtc, state, property, val);
518         else
519                 return -EINVAL;
520
521         return 0;
522 }
523
524 /**
525  * drm_atomic_crtc_check - check crtc state
526  * @crtc: crtc to check
527  * @state: crtc state to check
528  *
529  * Provides core sanity checks for crtc state.
530  *
531  * RETURNS:
532  * Zero on success, error code on failure
533  */
534 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
535                 struct drm_crtc_state *state)
536 {
537         /* NOTE: we explicitly don't enforce constraints such as primary
538          * layer covering entire screen, since that is something we want
539          * to allow (on hw that supports it).  For hw that does not, it
540          * should be checked in driver's crtc->atomic_check() vfunc.
541          *
542          * TODO: Add generic modeset state checks once we support those.
543          */
544
545         if (state->active && !state->enable) {
546                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
547                                  crtc->base.id, crtc->name);
548                 return -EINVAL;
549         }
550
551         /* The state->enable vs. state->mode_blob checks can be WARN_ON,
552          * as this is a kernel-internal detail that userspace should never
553          * be able to trigger. */
554         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
555             WARN_ON(state->enable && !state->mode_blob)) {
556                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
557                                  crtc->base.id, crtc->name);
558                 return -EINVAL;
559         }
560
561         if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
562             WARN_ON(!state->enable && state->mode_blob)) {
563                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
564                                  crtc->base.id, crtc->name);
565                 return -EINVAL;
566         }
567
568         /*
569          * Reject event generation for when a CRTC is off and stays off.
570          * It wouldn't be hard to implement this, but userspace has a track
571          * record of happily burning through 100% cpu (or worse, crash) when the
572          * display pipe is suspended. To avoid all that fun just reject updates
573          * that ask for events since likely that indicates a bug in the
574          * compositor's drawing loop. This is consistent with the vblank IOCTL
575          * and legacy page_flip IOCTL which also reject service on a disabled
576          * pipe.
577          */
578         if (state->event && !state->active && !crtc->state->active) {
579                 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
580                                  crtc->base.id);
581                 return -EINVAL;
582         }
583
584         return 0;
585 }
586
587 /**
588  * drm_atomic_get_plane_state - get plane state
589  * @state: global atomic state object
590  * @plane: plane to get state object for
591  *
592  * This function returns the plane state for the given plane, allocating it if
593  * needed. It will also grab the relevant plane lock to make sure that the state
594  * is consistent.
595  *
596  * Returns:
597  *
598  * Either the allocated state or the error code encoded into the pointer. When
599  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
600  * entire atomic sequence must be restarted. All other errors are fatal.
601  */
602 struct drm_plane_state *
603 drm_atomic_get_plane_state(struct drm_atomic_state *state,
604                           struct drm_plane *plane)
605 {
606         int ret, index = drm_plane_index(plane);
607         struct drm_plane_state *plane_state;
608
609         WARN_ON(!state->acquire_ctx);
610
611         plane_state = drm_atomic_get_existing_plane_state(state, plane);
612         if (plane_state)
613                 return plane_state;
614
615         ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
616         if (ret)
617                 return ERR_PTR(ret);
618
619         plane_state = plane->funcs->atomic_duplicate_state(plane);
620         if (!plane_state)
621                 return ERR_PTR(-ENOMEM);
622
623         state->planes[index].state = plane_state;
624         state->planes[index].ptr = plane;
625         plane_state->state = state;
626
627         DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
628                          plane->base.id, plane->name, plane_state, state);
629
630         if (plane_state->crtc) {
631                 struct drm_crtc_state *crtc_state;
632
633                 crtc_state = drm_atomic_get_crtc_state(state,
634                                                        plane_state->crtc);
635                 if (IS_ERR(crtc_state))
636                         return ERR_CAST(crtc_state);
637         }
638
639         return plane_state;
640 }
641 EXPORT_SYMBOL(drm_atomic_get_plane_state);
642
643 /**
644  * drm_atomic_plane_set_property - set property on plane
645  * @plane: the drm plane to set a property on
646  * @state: the state object to update with the new property value
647  * @property: the property to set
648  * @val: the new property value
649  *
650  * Use this instead of calling plane->atomic_set_property directly.
651  * This function handles generic/core properties and calls out to
652  * driver's ->atomic_set_property() for driver properties.  To ensure
653  * consistent behavior you must call this function rather than the
654  * driver hook directly.
655  *
656  * RETURNS:
657  * Zero on success, error code on failure
658  */
659 int drm_atomic_plane_set_property(struct drm_plane *plane,
660                 struct drm_plane_state *state, struct drm_property *property,
661                 uint64_t val)
662 {
663         struct drm_device *dev = plane->dev;
664         struct drm_mode_config *config = &dev->mode_config;
665
666         if (property == config->prop_fb_id) {
667                 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
668                 drm_atomic_set_fb_for_plane(state, fb);
669                 if (fb)
670                         drm_framebuffer_unreference(fb);
671         } else if (property == config->prop_crtc_id) {
672                 struct drm_crtc *crtc = drm_crtc_find(dev, val);
673                 return drm_atomic_set_crtc_for_plane(state, crtc);
674         } else if (property == config->prop_crtc_x) {
675                 state->crtc_x = U642I64(val);
676         } else if (property == config->prop_crtc_y) {
677                 state->crtc_y = U642I64(val);
678         } else if (property == config->prop_crtc_w) {
679                 state->crtc_w = val;
680         } else if (property == config->prop_crtc_h) {
681                 state->crtc_h = val;
682         } else if (property == config->prop_src_x) {
683                 state->src_x = val;
684         } else if (property == config->prop_src_y) {
685                 state->src_y = val;
686         } else if (property == config->prop_src_w) {
687                 state->src_w = val;
688         } else if (property == config->prop_src_h) {
689                 state->src_h = val;
690         } else if (property == config->rotation_property) {
691                 state->rotation = val;
692         } else if (plane->funcs->atomic_set_property) {
693                 return plane->funcs->atomic_set_property(plane, state,
694                                 property, val);
695         } else {
696                 return -EINVAL;
697         }
698
699         return 0;
700 }
701 EXPORT_SYMBOL(drm_atomic_plane_set_property);
702
703 /**
704  * drm_atomic_plane_get_property - get property value from plane state
705  * @plane: the drm plane to set a property on
706  * @state: the state object to get the property value from
707  * @property: the property to set
708  * @val: return location for the property value
709  *
710  * This function handles generic/core properties and calls out to
711  * driver's ->atomic_get_property() for driver properties.  To ensure
712  * consistent behavior you must call this function rather than the
713  * driver hook directly.
714  *
715  * RETURNS:
716  * Zero on success, error code on failure
717  */
718 static int
719 drm_atomic_plane_get_property(struct drm_plane *plane,
720                 const struct drm_plane_state *state,
721                 struct drm_property *property, uint64_t *val)
722 {
723         struct drm_device *dev = plane->dev;
724         struct drm_mode_config *config = &dev->mode_config;
725
726         if (property == config->prop_fb_id) {
727                 *val = (state->fb) ? state->fb->base.id : 0;
728         } else if (property == config->prop_crtc_id) {
729                 *val = (state->crtc) ? state->crtc->base.id : 0;
730         } else if (property == config->prop_crtc_x) {
731                 *val = I642U64(state->crtc_x);
732         } else if (property == config->prop_crtc_y) {
733                 *val = I642U64(state->crtc_y);
734         } else if (property == config->prop_crtc_w) {
735                 *val = state->crtc_w;
736         } else if (property == config->prop_crtc_h) {
737                 *val = state->crtc_h;
738         } else if (property == config->prop_src_x) {
739                 *val = state->src_x;
740         } else if (property == config->prop_src_y) {
741                 *val = state->src_y;
742         } else if (property == config->prop_src_w) {
743                 *val = state->src_w;
744         } else if (property == config->prop_src_h) {
745                 *val = state->src_h;
746         } else if (property == config->rotation_property) {
747                 *val = state->rotation;
748         } else if (plane->funcs->atomic_get_property) {
749                 return plane->funcs->atomic_get_property(plane, state, property, val);
750         } else {
751                 return -EINVAL;
752         }
753
754         return 0;
755 }
756
757 static bool
758 plane_switching_crtc(struct drm_atomic_state *state,
759                      struct drm_plane *plane,
760                      struct drm_plane_state *plane_state)
761 {
762         if (!plane->state->crtc || !plane_state->crtc)
763                 return false;
764
765         if (plane->state->crtc == plane_state->crtc)
766                 return false;
767
768         /* This could be refined, but currently there's no helper or driver code
769          * to implement direct switching of active planes nor userspace to take
770          * advantage of more direct plane switching without the intermediate
771          * full OFF state.
772          */
773         return true;
774 }
775
776 /**
777  * drm_atomic_plane_check - check plane state
778  * @plane: plane to check
779  * @state: plane state to check
780  *
781  * Provides core sanity checks for plane state.
782  *
783  * RETURNS:
784  * Zero on success, error code on failure
785  */
786 static int drm_atomic_plane_check(struct drm_plane *plane,
787                 struct drm_plane_state *state)
788 {
789         unsigned int fb_width, fb_height;
790         int ret;
791
792         /* either *both* CRTC and FB must be set, or neither */
793         if (WARN_ON(state->crtc && !state->fb)) {
794                 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
795                 return -EINVAL;
796         } else if (WARN_ON(state->fb && !state->crtc)) {
797                 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
798                 return -EINVAL;
799         }
800
801         /* if disabled, we don't care about the rest of the state: */
802         if (!state->crtc)
803                 return 0;
804
805         /* Check whether this plane is usable on this CRTC */
806         if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
807                 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
808                 return -EINVAL;
809         }
810
811         /* Check whether this plane supports the fb pixel format. */
812         ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
813         if (ret) {
814                 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
815                                  drm_get_format_name(state->fb->pixel_format));
816                 return ret;
817         }
818
819         /* Give drivers some help against integer overflows */
820         if (state->crtc_w > INT_MAX ||
821             state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
822             state->crtc_h > INT_MAX ||
823             state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
824                 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
825                                  state->crtc_w, state->crtc_h,
826                                  state->crtc_x, state->crtc_y);
827                 return -ERANGE;
828         }
829
830         fb_width = state->fb->width << 16;
831         fb_height = state->fb->height << 16;
832
833         /* Make sure source coordinates are inside the fb. */
834         if (state->src_w > fb_width ||
835             state->src_x > fb_width - state->src_w ||
836             state->src_h > fb_height ||
837             state->src_y > fb_height - state->src_h) {
838                 DRM_DEBUG_ATOMIC("Invalid source coordinates "
839                                  "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
840                                  state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
841                                  state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
842                                  state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
843                                  state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
844                 return -ENOSPC;
845         }
846
847         if (plane_switching_crtc(state->state, plane, state)) {
848                 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
849                                  plane->base.id, plane->name);
850                 return -EINVAL;
851         }
852
853         return 0;
854 }
855
856 /**
857  * drm_atomic_get_connector_state - get connector state
858  * @state: global atomic state object
859  * @connector: connector to get state object for
860  *
861  * This function returns the connector state for the given connector,
862  * allocating it if needed. It will also grab the relevant connector lock to
863  * make sure that the state is consistent.
864  *
865  * Returns:
866  *
867  * Either the allocated state or the error code encoded into the pointer. When
868  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
869  * entire atomic sequence must be restarted. All other errors are fatal.
870  */
871 struct drm_connector_state *
872 drm_atomic_get_connector_state(struct drm_atomic_state *state,
873                           struct drm_connector *connector)
874 {
875         int ret, index;
876         struct drm_mode_config *config = &connector->dev->mode_config;
877         struct drm_connector_state *connector_state;
878
879         WARN_ON(!state->acquire_ctx);
880
881         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
882         if (ret)
883                 return ERR_PTR(ret);
884
885         index = drm_connector_index(connector);
886
887         if (index >= state->num_connector) {
888                 struct __drm_connnectors_state *c;
889                 int alloc = max(index + 1, config->num_connector);
890
891                 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
892                 if (!c)
893                         return ERR_PTR(-ENOMEM);
894
895                 state->connectors = c;
896                 memset(&state->connectors[state->num_connector], 0,
897                        sizeof(*state->connectors) * (alloc - state->num_connector));
898
899                 state->num_connector = alloc;
900         }
901
902         if (state->connectors[index].state)
903                 return state->connectors[index].state;
904
905         connector_state = connector->funcs->atomic_duplicate_state(connector);
906         if (!connector_state)
907                 return ERR_PTR(-ENOMEM);
908
909         drm_connector_reference(connector);
910         state->connectors[index].state = connector_state;
911         state->connectors[index].ptr = connector;
912         connector_state->state = state;
913
914         DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
915                          connector->base.id, connector_state, state);
916
917         if (connector_state->crtc) {
918                 struct drm_crtc_state *crtc_state;
919
920                 crtc_state = drm_atomic_get_crtc_state(state,
921                                                        connector_state->crtc);
922                 if (IS_ERR(crtc_state))
923                         return ERR_CAST(crtc_state);
924         }
925
926         return connector_state;
927 }
928 EXPORT_SYMBOL(drm_atomic_get_connector_state);
929
930 /**
931  * drm_atomic_connector_set_property - set property on connector.
932  * @connector: the drm connector to set a property on
933  * @state: the state object to update with the new property value
934  * @property: the property to set
935  * @val: the new property value
936  *
937  * Use this instead of calling connector->atomic_set_property directly.
938  * This function handles generic/core properties and calls out to
939  * driver's ->atomic_set_property() for driver properties.  To ensure
940  * consistent behavior you must call this function rather than the
941  * driver hook directly.
942  *
943  * RETURNS:
944  * Zero on success, error code on failure
945  */
946 int drm_atomic_connector_set_property(struct drm_connector *connector,
947                 struct drm_connector_state *state, struct drm_property *property,
948                 uint64_t val)
949 {
950         struct drm_device *dev = connector->dev;
951         struct drm_mode_config *config = &dev->mode_config;
952
953         if (property == config->prop_crtc_id) {
954                 struct drm_crtc *crtc = drm_crtc_find(dev, val);
955                 return drm_atomic_set_crtc_for_connector(state, crtc);
956         } else if (property == config->dpms_property) {
957                 /* setting DPMS property requires special handling, which
958                  * is done in legacy setprop path for us.  Disallow (for
959                  * now?) atomic writes to DPMS property:
960                  */
961                 return -EINVAL;
962         } else if (connector->funcs->atomic_set_property) {
963                 return connector->funcs->atomic_set_property(connector,
964                                 state, property, val);
965         } else {
966                 return -EINVAL;
967         }
968 }
969 EXPORT_SYMBOL(drm_atomic_connector_set_property);
970
971 /**
972  * drm_atomic_connector_get_property - get property value from connector state
973  * @connector: the drm connector to set a property on
974  * @state: the state object to get the property value from
975  * @property: the property to set
976  * @val: return location for the property value
977  *
978  * This function handles generic/core properties and calls out to
979  * driver's ->atomic_get_property() for driver properties.  To ensure
980  * consistent behavior you must call this function rather than the
981  * driver hook directly.
982  *
983  * RETURNS:
984  * Zero on success, error code on failure
985  */
986 static int
987 drm_atomic_connector_get_property(struct drm_connector *connector,
988                 const struct drm_connector_state *state,
989                 struct drm_property *property, uint64_t *val)
990 {
991         struct drm_device *dev = connector->dev;
992         struct drm_mode_config *config = &dev->mode_config;
993
994         if (property == config->prop_crtc_id) {
995                 *val = (state->crtc) ? state->crtc->base.id : 0;
996         } else if (property == config->dpms_property) {
997                 *val = connector->dpms;
998         } else if (connector->funcs->atomic_get_property) {
999                 return connector->funcs->atomic_get_property(connector,
1000                                 state, property, val);
1001         } else {
1002                 return -EINVAL;
1003         }
1004
1005         return 0;
1006 }
1007
1008 int drm_atomic_get_property(struct drm_mode_object *obj,
1009                 struct drm_property *property, uint64_t *val)
1010 {
1011         struct drm_device *dev = property->dev;
1012         int ret;
1013
1014         switch (obj->type) {
1015         case DRM_MODE_OBJECT_CONNECTOR: {
1016                 struct drm_connector *connector = obj_to_connector(obj);
1017                 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1018                 ret = drm_atomic_connector_get_property(connector,
1019                                 connector->state, property, val);
1020                 break;
1021         }
1022         case DRM_MODE_OBJECT_CRTC: {
1023                 struct drm_crtc *crtc = obj_to_crtc(obj);
1024                 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1025                 ret = drm_atomic_crtc_get_property(crtc,
1026                                 crtc->state, property, val);
1027                 break;
1028         }
1029         case DRM_MODE_OBJECT_PLANE: {
1030                 struct drm_plane *plane = obj_to_plane(obj);
1031                 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1032                 ret = drm_atomic_plane_get_property(plane,
1033                                 plane->state, property, val);
1034                 break;
1035         }
1036         default:
1037                 ret = -EINVAL;
1038                 break;
1039         }
1040
1041         return ret;
1042 }
1043
1044 /**
1045  * drm_atomic_set_crtc_for_plane - set crtc for plane
1046  * @plane_state: the plane whose incoming state to update
1047  * @crtc: crtc to use for the plane
1048  *
1049  * Changing the assigned crtc for a plane requires us to grab the lock and state
1050  * for the new crtc, as needed. This function takes care of all these details
1051  * besides updating the pointer in the state object itself.
1052  *
1053  * Returns:
1054  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1055  * then the w/w mutex code has detected a deadlock and the entire atomic
1056  * sequence must be restarted. All other errors are fatal.
1057  */
1058 int
1059 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1060                               struct drm_crtc *crtc)
1061 {
1062         struct drm_plane *plane = plane_state->plane;
1063         struct drm_crtc_state *crtc_state;
1064
1065         if (plane_state->crtc) {
1066                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1067                                                        plane_state->crtc);
1068                 if (WARN_ON(IS_ERR(crtc_state)))
1069                         return PTR_ERR(crtc_state);
1070
1071                 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1072         }
1073
1074         plane_state->crtc = crtc;
1075
1076         if (crtc) {
1077                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1078                                                        crtc);
1079                 if (IS_ERR(crtc_state))
1080                         return PTR_ERR(crtc_state);
1081                 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1082         }
1083
1084         if (crtc)
1085                 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1086                                  plane_state, crtc->base.id, crtc->name);
1087         else
1088                 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1089                                  plane_state);
1090
1091         return 0;
1092 }
1093 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1094
1095 /**
1096  * drm_atomic_set_fb_for_plane - set framebuffer for plane
1097  * @plane_state: atomic state object for the plane
1098  * @fb: fb to use for the plane
1099  *
1100  * Changing the assigned framebuffer for a plane requires us to grab a reference
1101  * to the new fb and drop the reference to the old fb, if there is one. This
1102  * function takes care of all these details besides updating the pointer in the
1103  * state object itself.
1104  */
1105 void
1106 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1107                             struct drm_framebuffer *fb)
1108 {
1109         if (plane_state->fb)
1110                 drm_framebuffer_unreference(plane_state->fb);
1111         if (fb)
1112                 drm_framebuffer_reference(fb);
1113         plane_state->fb = fb;
1114
1115         if (fb)
1116                 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1117                                  fb->base.id, plane_state);
1118         else
1119                 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1120                                  plane_state);
1121 }
1122 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1123
1124 /**
1125  * drm_atomic_set_crtc_for_connector - set crtc for connector
1126  * @conn_state: atomic state object for the connector
1127  * @crtc: crtc to use for the connector
1128  *
1129  * Changing the assigned crtc for a connector requires us to grab the lock and
1130  * state for the new crtc, as needed. This function takes care of all these
1131  * details besides updating the pointer in the state object itself.
1132  *
1133  * Returns:
1134  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1135  * then the w/w mutex code has detected a deadlock and the entire atomic
1136  * sequence must be restarted. All other errors are fatal.
1137  */
1138 int
1139 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1140                                   struct drm_crtc *crtc)
1141 {
1142         struct drm_crtc_state *crtc_state;
1143
1144         if (conn_state->crtc == crtc)
1145                 return 0;
1146
1147         if (conn_state->crtc) {
1148                 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1149                                                                 conn_state->crtc);
1150
1151                 crtc_state->connector_mask &=
1152                         ~(1 << drm_connector_index(conn_state->connector));
1153
1154                 drm_connector_unreference(conn_state->connector);
1155                 conn_state->crtc = NULL;
1156         }
1157
1158         if (crtc) {
1159                 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1160                 if (IS_ERR(crtc_state))
1161                         return PTR_ERR(crtc_state);
1162
1163                 crtc_state->connector_mask |=
1164                         1 << drm_connector_index(conn_state->connector);
1165
1166                 drm_connector_reference(conn_state->connector);
1167                 conn_state->crtc = crtc;
1168
1169                 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1170                                  conn_state, crtc->base.id, crtc->name);
1171         } else {
1172                 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1173                                  conn_state);
1174         }
1175
1176         return 0;
1177 }
1178 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1179
1180 /**
1181  * drm_atomic_add_affected_connectors - add connectors for crtc
1182  * @state: atomic state
1183  * @crtc: DRM crtc
1184  *
1185  * This function walks the current configuration and adds all connectors
1186  * currently using @crtc to the atomic configuration @state. Note that this
1187  * function must acquire the connection mutex. This can potentially cause
1188  * unneeded seralization if the update is just for the planes on one crtc. Hence
1189  * drivers and helpers should only call this when really needed (e.g. when a
1190  * full modeset needs to happen due to some change).
1191  *
1192  * Returns:
1193  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1194  * then the w/w mutex code has detected a deadlock and the entire atomic
1195  * sequence must be restarted. All other errors are fatal.
1196  */
1197 int
1198 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1199                                    struct drm_crtc *crtc)
1200 {
1201         struct drm_mode_config *config = &state->dev->mode_config;
1202         struct drm_connector *connector;
1203         struct drm_connector_state *conn_state;
1204         int ret;
1205
1206         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1207         if (ret)
1208                 return ret;
1209
1210         DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1211                          crtc->base.id, crtc->name, state);
1212
1213         /*
1214          * Changed connectors are already in @state, so only need to look at the
1215          * current configuration.
1216          */
1217         drm_for_each_connector(connector, state->dev) {
1218                 if (connector->state->crtc != crtc)
1219                         continue;
1220
1221                 conn_state = drm_atomic_get_connector_state(state, connector);
1222                 if (IS_ERR(conn_state))
1223                         return PTR_ERR(conn_state);
1224         }
1225
1226         return 0;
1227 }
1228 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1229
1230 /**
1231  * drm_atomic_add_affected_planes - add planes for crtc
1232  * @state: atomic state
1233  * @crtc: DRM crtc
1234  *
1235  * This function walks the current configuration and adds all planes
1236  * currently used by @crtc to the atomic configuration @state. This is useful
1237  * when an atomic commit also needs to check all currently enabled plane on
1238  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1239  * to avoid special code to force-enable all planes.
1240  *
1241  * Since acquiring a plane state will always also acquire the w/w mutex of the
1242  * current CRTC for that plane (if there is any) adding all the plane states for
1243  * a CRTC will not reduce parallism of atomic updates.
1244  *
1245  * Returns:
1246  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1247  * then the w/w mutex code has detected a deadlock and the entire atomic
1248  * sequence must be restarted. All other errors are fatal.
1249  */
1250 int
1251 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1252                                struct drm_crtc *crtc)
1253 {
1254         struct drm_plane *plane;
1255
1256         WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1257
1258         drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1259                 struct drm_plane_state *plane_state =
1260                         drm_atomic_get_plane_state(state, plane);
1261
1262                 if (IS_ERR(plane_state))
1263                         return PTR_ERR(plane_state);
1264         }
1265         return 0;
1266 }
1267 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1268
1269 /**
1270  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1271  * @state: atomic state
1272  *
1273  * This function should be used by legacy entry points which don't understand
1274  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1275  * the slowpath completed.
1276  */
1277 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1278 {
1279         int ret;
1280
1281 retry:
1282         drm_modeset_backoff(state->acquire_ctx);
1283
1284         ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx);
1285         if (ret)
1286                 goto retry;
1287 }
1288 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1289
1290 /**
1291  * drm_atomic_check_only - check whether a given config would work
1292  * @state: atomic configuration to check
1293  *
1294  * Note that this function can return -EDEADLK if the driver needed to acquire
1295  * more locks but encountered a deadlock. The caller must then do the usual w/w
1296  * backoff dance and restart. All other errors are fatal.
1297  *
1298  * Returns:
1299  * 0 on success, negative error code on failure.
1300  */
1301 int drm_atomic_check_only(struct drm_atomic_state *state)
1302 {
1303         struct drm_device *dev = state->dev;
1304         struct drm_mode_config *config = &dev->mode_config;
1305         struct drm_plane *plane;
1306         struct drm_plane_state *plane_state;
1307         struct drm_crtc *crtc;
1308         struct drm_crtc_state *crtc_state;
1309         int i, ret = 0;
1310
1311         DRM_DEBUG_ATOMIC("checking %p\n", state);
1312
1313         for_each_plane_in_state(state, plane, plane_state, i) {
1314                 ret = drm_atomic_plane_check(plane, plane_state);
1315                 if (ret) {
1316                         DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1317                                          plane->base.id, plane->name);
1318                         return ret;
1319                 }
1320         }
1321
1322         for_each_crtc_in_state(state, crtc, crtc_state, i) {
1323                 ret = drm_atomic_crtc_check(crtc, crtc_state);
1324                 if (ret) {
1325                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1326                                          crtc->base.id, crtc->name);
1327                         return ret;
1328                 }
1329         }
1330
1331         if (config->funcs->atomic_check)
1332                 ret = config->funcs->atomic_check(state->dev, state);
1333
1334         if (!state->allow_modeset) {
1335                 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1336                         if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1337                                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1338                                                  crtc->base.id, crtc->name);
1339                                 return -EINVAL;
1340                         }
1341                 }
1342         }
1343
1344         return ret;
1345 }
1346 EXPORT_SYMBOL(drm_atomic_check_only);
1347
1348 /**
1349  * drm_atomic_commit - commit configuration atomically
1350  * @state: atomic configuration to check
1351  *
1352  * Note that this function can return -EDEADLK if the driver needed to acquire
1353  * more locks but encountered a deadlock. The caller must then do the usual w/w
1354  * backoff dance and restart. All other errors are fatal.
1355  *
1356  * Also note that on successful execution ownership of @state is transferred
1357  * from the caller of this function to the function itself. The caller must not
1358  * free or in any other way access @state. If the function fails then the caller
1359  * must clean up @state itself.
1360  *
1361  * Returns:
1362  * 0 on success, negative error code on failure.
1363  */
1364 int drm_atomic_commit(struct drm_atomic_state *state)
1365 {
1366         struct drm_mode_config *config = &state->dev->mode_config;
1367         int ret;
1368
1369         ret = drm_atomic_check_only(state);
1370         if (ret)
1371                 return ret;
1372
1373         DRM_DEBUG_ATOMIC("commiting %p\n", state);
1374
1375         return config->funcs->atomic_commit(state->dev, state, false);
1376 }
1377 EXPORT_SYMBOL(drm_atomic_commit);
1378
1379 /**
1380  * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
1381  * @state: atomic configuration to check
1382  *
1383  * Note that this function can return -EDEADLK if the driver needed to acquire
1384  * more locks but encountered a deadlock. The caller must then do the usual w/w
1385  * backoff dance and restart. All other errors are fatal.
1386  *
1387  * Also note that on successful execution ownership of @state is transferred
1388  * from the caller of this function to the function itself. The caller must not
1389  * free or in any other way access @state. If the function fails then the caller
1390  * must clean up @state itself.
1391  *
1392  * Returns:
1393  * 0 on success, negative error code on failure.
1394  */
1395 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1396 {
1397         struct drm_mode_config *config = &state->dev->mode_config;
1398         int ret;
1399
1400         ret = drm_atomic_check_only(state);
1401         if (ret)
1402                 return ret;
1403
1404         DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
1405
1406         return config->funcs->atomic_commit(state->dev, state, true);
1407 }
1408 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1409
1410 /*
1411  * The big monstor ioctl
1412  */
1413
1414 static struct drm_pending_vblank_event *create_vblank_event(
1415                 struct drm_device *dev, struct drm_file *file_priv,
1416                 struct fence *fence, uint64_t user_data)
1417 {
1418         struct drm_pending_vblank_event *e = NULL;
1419         int ret;
1420
1421         e = kzalloc(sizeof *e, GFP_KERNEL);
1422         if (!e)
1423                 return NULL;
1424
1425         e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1426         e->event.base.length = sizeof(e->event);
1427         e->event.user_data = user_data;
1428
1429         if (file_priv) {
1430                 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1431                                              &e->event.base);
1432                 if (ret) {
1433                         kfree(e);
1434                         return NULL;
1435                 }
1436         }
1437
1438         e->base.fence = fence;
1439
1440         return e;
1441 }
1442
1443 static int atomic_set_prop(struct drm_atomic_state *state,
1444                 struct drm_mode_object *obj, struct drm_property *prop,
1445                 uint64_t prop_value)
1446 {
1447         struct drm_mode_object *ref;
1448         int ret;
1449
1450         if (!drm_property_change_valid_get(prop, prop_value, &ref))
1451                 return -EINVAL;
1452
1453         switch (obj->type) {
1454         case DRM_MODE_OBJECT_CONNECTOR: {
1455                 struct drm_connector *connector = obj_to_connector(obj);
1456                 struct drm_connector_state *connector_state;
1457
1458                 connector_state = drm_atomic_get_connector_state(state, connector);
1459                 if (IS_ERR(connector_state)) {
1460                         ret = PTR_ERR(connector_state);
1461                         break;
1462                 }
1463
1464                 ret = drm_atomic_connector_set_property(connector,
1465                                 connector_state, prop, prop_value);
1466                 break;
1467         }
1468         case DRM_MODE_OBJECT_CRTC: {
1469                 struct drm_crtc *crtc = obj_to_crtc(obj);
1470                 struct drm_crtc_state *crtc_state;
1471
1472                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1473                 if (IS_ERR(crtc_state)) {
1474                         ret = PTR_ERR(crtc_state);
1475                         break;
1476                 }
1477
1478                 ret = drm_atomic_crtc_set_property(crtc,
1479                                 crtc_state, prop, prop_value);
1480                 break;
1481         }
1482         case DRM_MODE_OBJECT_PLANE: {
1483                 struct drm_plane *plane = obj_to_plane(obj);
1484                 struct drm_plane_state *plane_state;
1485
1486                 plane_state = drm_atomic_get_plane_state(state, plane);
1487                 if (IS_ERR(plane_state)) {
1488                         ret = PTR_ERR(plane_state);
1489                         break;
1490                 }
1491
1492                 ret = drm_atomic_plane_set_property(plane,
1493                                 plane_state, prop, prop_value);
1494                 break;
1495         }
1496         default:
1497                 ret = -EINVAL;
1498                 break;
1499         }
1500
1501         drm_property_change_valid_put(prop, ref);
1502         return ret;
1503 }
1504
1505 /**
1506  * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1507  *
1508  * @dev: drm device to check.
1509  * @plane_mask: plane mask for planes that were updated.
1510  * @ret: return value, can be -EDEADLK for a retry.
1511  *
1512  * Before doing an update plane->old_fb is set to plane->fb,
1513  * but before dropping the locks old_fb needs to be set to NULL
1514  * and plane->fb updated. This is a common operation for each
1515  * atomic update, so this call is split off as a helper.
1516  */
1517 void drm_atomic_clean_old_fb(struct drm_device *dev,
1518                              unsigned plane_mask,
1519                              int ret)
1520 {
1521         struct drm_plane *plane;
1522
1523         /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1524          * locks (ie. while it is still safe to deref plane->state).  We
1525          * need to do this here because the driver entry points cannot
1526          * distinguish between legacy and atomic ioctls.
1527          */
1528         drm_for_each_plane_mask(plane, dev, plane_mask) {
1529                 if (ret == 0) {
1530                         struct drm_framebuffer *new_fb = plane->state->fb;
1531                         if (new_fb)
1532                                 drm_framebuffer_reference(new_fb);
1533                         plane->fb = new_fb;
1534                         plane->crtc = plane->state->crtc;
1535
1536                         if (plane->old_fb)
1537                                 drm_framebuffer_unreference(plane->old_fb);
1538                 }
1539                 plane->old_fb = NULL;
1540         }
1541 }
1542 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1543
1544 int drm_mode_atomic_ioctl(struct drm_device *dev,
1545                           void *data, struct drm_file *file_priv)
1546 {
1547         struct drm_mode_atomic *arg = data;
1548         uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1549         uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1550         uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1551         uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1552         unsigned int copied_objs, copied_props;
1553         struct drm_atomic_state *state;
1554         struct drm_modeset_acquire_ctx ctx;
1555         struct drm_plane *plane;
1556         struct drm_crtc *crtc;
1557         struct drm_crtc_state *crtc_state;
1558         unsigned plane_mask;
1559         int ret = 0;
1560         unsigned int i, j;
1561
1562         /* disallow for drivers not supporting atomic: */
1563         if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1564                 return -EINVAL;
1565
1566         /* disallow for userspace that has not enabled atomic cap (even
1567          * though this may be a bit overkill, since legacy userspace
1568          * wouldn't know how to call this ioctl)
1569          */
1570         if (!file_priv->atomic)
1571                 return -EINVAL;
1572
1573         if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1574                 return -EINVAL;
1575
1576         if (arg->reserved)
1577                 return -EINVAL;
1578
1579         if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1580                         !dev->mode_config.async_page_flip)
1581                 return -EINVAL;
1582
1583         /* can't test and expect an event at the same time. */
1584         if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1585                         (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1586                 return -EINVAL;
1587
1588         drm_modeset_acquire_init(&ctx, 0);
1589
1590         state = drm_atomic_state_alloc(dev);
1591         if (!state)
1592                 return -ENOMEM;
1593
1594         state->acquire_ctx = &ctx;
1595         state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1596
1597 retry:
1598         plane_mask = 0;
1599         copied_objs = 0;
1600         copied_props = 0;
1601
1602         for (i = 0; i < arg->count_objs; i++) {
1603                 uint32_t obj_id, count_props;
1604                 struct drm_mode_object *obj;
1605
1606                 if (get_user(obj_id, objs_ptr + copied_objs)) {
1607                         ret = -EFAULT;
1608                         goto out;
1609                 }
1610
1611                 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1612                 if (!obj) {
1613                         ret = -ENOENT;
1614                         goto out;
1615                 }
1616
1617                 if (!obj->properties) {
1618                         drm_mode_object_unreference(obj);
1619                         ret = -ENOENT;
1620                         goto out;
1621                 }
1622
1623                 if (get_user(count_props, count_props_ptr + copied_objs)) {
1624                         drm_mode_object_unreference(obj);
1625                         ret = -EFAULT;
1626                         goto out;
1627                 }
1628
1629                 copied_objs++;
1630
1631                 for (j = 0; j < count_props; j++) {
1632                         uint32_t prop_id;
1633                         uint64_t prop_value;
1634                         struct drm_property *prop;
1635
1636                         if (get_user(prop_id, props_ptr + copied_props)) {
1637                                 drm_mode_object_unreference(obj);
1638                                 ret = -EFAULT;
1639                                 goto out;
1640                         }
1641
1642                         prop = drm_property_find(dev, prop_id);
1643                         if (!prop) {
1644                                 drm_mode_object_unreference(obj);
1645                                 ret = -ENOENT;
1646                                 goto out;
1647                         }
1648
1649                         if (copy_from_user(&prop_value,
1650                                            prop_values_ptr + copied_props,
1651                                            sizeof(prop_value))) {
1652                                 drm_mode_object_unreference(obj);
1653                                 ret = -EFAULT;
1654                                 goto out;
1655                         }
1656
1657                         ret = atomic_set_prop(state, obj, prop, prop_value);
1658                         if (ret) {
1659                                 drm_mode_object_unreference(obj);
1660                                 goto out;
1661                         }
1662
1663                         copied_props++;
1664                 }
1665
1666                 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1667                     !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
1668                         plane = obj_to_plane(obj);
1669                         plane_mask |= (1 << drm_plane_index(plane));
1670                         plane->old_fb = plane->fb;
1671                 }
1672                 drm_mode_object_unreference(obj);
1673         }
1674
1675         if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1676                 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1677                         struct drm_pending_vblank_event *e;
1678
1679                         e = create_vblank_event(dev, file_priv, NULL,
1680                                                 arg->user_data);
1681                         if (!e) {
1682                                 ret = -ENOMEM;
1683                                 goto out;
1684                         }
1685
1686                         crtc_state->event = e;
1687                 }
1688         }
1689
1690         if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1691                 /*
1692                  * Unlike commit, check_only does not clean up state.
1693                  * Below we call drm_atomic_state_free for it.
1694                  */
1695                 ret = drm_atomic_check_only(state);
1696         } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1697                 ret = drm_atomic_nonblocking_commit(state);
1698         } else {
1699                 ret = drm_atomic_commit(state);
1700         }
1701
1702 out:
1703         drm_atomic_clean_old_fb(dev, plane_mask, ret);
1704
1705         if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1706                 /*
1707                  * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1708                  * if they weren't, this code should be called on success
1709                  * for TEST_ONLY too.
1710                  */
1711
1712                 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1713                         if (!crtc_state->event)
1714                                 continue;
1715
1716                         drm_event_cancel_free(dev, &crtc_state->event->base);
1717                 }
1718         }
1719
1720         if (ret == -EDEADLK) {
1721                 drm_atomic_state_clear(state);
1722                 drm_modeset_backoff(&ctx);
1723                 goto retry;
1724         }
1725
1726         if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1727                 drm_atomic_state_free(state);
1728
1729         drm_modeset_drop_locks(&ctx);
1730         drm_modeset_acquire_fini(&ctx);
1731
1732         return ret;
1733 }