LCOV - code coverage report
Current view: top level - drivers/gpu/drm - drm_atomic_uapi.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 650 0.0 %
Date: 2023-04-06 08:38:28 Functions: 0 22 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2014 Red Hat
       3             :  * Copyright (C) 2014 Intel Corp.
       4             :  * Copyright (C) 2018 Intel Corp.
       5             :  * Copyright (c) 2020, The Linux Foundation. All rights reserved.
       6             :  *
       7             :  * Permission is hereby granted, free of charge, to any person obtaining a
       8             :  * copy of this software and associated documentation files (the "Software"),
       9             :  * to deal in the Software without restriction, including without limitation
      10             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      11             :  * and/or sell copies of the Software, and to permit persons to whom the
      12             :  * Software is furnished to do so, subject to the following conditions:
      13             :  *
      14             :  * The above copyright notice and this permission notice shall be included in
      15             :  * all copies or substantial portions of the Software.
      16             :  *
      17             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      18             :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      19             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
      20             :  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
      21             :  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      22             :  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
      23             :  * OTHER DEALINGS IN THE SOFTWARE.
      24             :  *
      25             :  * Authors:
      26             :  * Rob Clark <robdclark@gmail.com>
      27             :  * Daniel Vetter <daniel.vetter@ffwll.ch>
      28             :  */
      29             : 
      30             : #include <drm/drm_atomic_uapi.h>
      31             : #include <drm/drm_atomic.h>
      32             : #include <drm/drm_framebuffer.h>
      33             : #include <drm/drm_print.h>
      34             : #include <drm/drm_drv.h>
      35             : #include <drm/drm_writeback.h>
      36             : #include <drm/drm_vblank.h>
      37             : 
      38             : #include <linux/dma-fence.h>
      39             : #include <linux/uaccess.h>
      40             : #include <linux/sync_file.h>
      41             : #include <linux/file.h>
      42             : 
      43             : #include "drm_crtc_internal.h"
      44             : 
      45             : /**
      46             :  * DOC: overview
      47             :  *
      48             :  * This file contains the marshalling and demarshalling glue for the atomic UAPI
      49             :  * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and
      50             :  * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and
      51             :  * drivers which have special needs to construct their own atomic updates, e.g.
      52             :  * for load detect or similar.
      53             :  */
      54             : 
      55             : /**
      56             :  * drm_atomic_set_mode_for_crtc - set mode for CRTC
      57             :  * @state: the CRTC whose incoming state to update
      58             :  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
      59             :  *
      60             :  * Set a mode (originating from the kernel) on the desired CRTC state and update
      61             :  * the enable property.
      62             :  *
      63             :  * RETURNS:
      64             :  * Zero on success, error code on failure. Cannot return -EDEADLK.
      65             :  */
      66           0 : int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
      67             :                                  const struct drm_display_mode *mode)
      68             : {
      69           0 :         struct drm_crtc *crtc = state->crtc;
      70             :         struct drm_mode_modeinfo umode;
      71             : 
      72             :         /* Early return for no change. */
      73           0 :         if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
      74             :                 return 0;
      75             : 
      76           0 :         drm_property_blob_put(state->mode_blob);
      77           0 :         state->mode_blob = NULL;
      78             : 
      79           0 :         if (mode) {
      80             :                 struct drm_property_blob *blob;
      81             : 
      82           0 :                 drm_mode_convert_to_umode(&umode, mode);
      83           0 :                 blob = drm_property_create_blob(crtc->dev,
      84             :                                                 sizeof(umode), &umode);
      85           0 :                 if (IS_ERR(blob))
      86           0 :                         return PTR_ERR(blob);
      87             : 
      88           0 :                 drm_mode_copy(&state->mode, mode);
      89             : 
      90           0 :                 state->mode_blob = blob;
      91           0 :                 state->enable = true;
      92           0 :                 drm_dbg_atomic(crtc->dev,
      93             :                                "Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
      94             :                                mode->name, crtc->base.id, crtc->name, state);
      95             :         } else {
      96           0 :                 memset(&state->mode, 0, sizeof(state->mode));
      97           0 :                 state->enable = false;
      98           0 :                 drm_dbg_atomic(crtc->dev,
      99             :                                "Set [NOMODE] for [CRTC:%d:%s] state %p\n",
     100             :                                crtc->base.id, crtc->name, state);
     101             :         }
     102             : 
     103             :         return 0;
     104             : }
     105             : EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
     106             : 
     107             : /**
     108             :  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
     109             :  * @state: the CRTC whose incoming state to update
     110             :  * @blob: pointer to blob property to use for mode
     111             :  *
     112             :  * Set a mode (originating from a blob property) on the desired CRTC state.
     113             :  * This function will take a reference on the blob property for the CRTC state,
     114             :  * and release the reference held on the state's existing mode property, if any
     115             :  * was set.
     116             :  *
     117             :  * RETURNS:
     118             :  * Zero on success, error code on failure. Cannot return -EDEADLK.
     119             :  */
     120           0 : int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
     121             :                                       struct drm_property_blob *blob)
     122             : {
     123           0 :         struct drm_crtc *crtc = state->crtc;
     124             : 
     125           0 :         if (blob == state->mode_blob)
     126             :                 return 0;
     127             : 
     128           0 :         drm_property_blob_put(state->mode_blob);
     129           0 :         state->mode_blob = NULL;
     130             : 
     131           0 :         memset(&state->mode, 0, sizeof(state->mode));
     132             : 
     133           0 :         if (blob) {
     134             :                 int ret;
     135             : 
     136           0 :                 if (blob->length != sizeof(struct drm_mode_modeinfo)) {
     137           0 :                         drm_dbg_atomic(crtc->dev,
     138             :                                        "[CRTC:%d:%s] bad mode blob length: %zu\n",
     139             :                                        crtc->base.id, crtc->name,
     140             :                                        blob->length);
     141           0 :                         return -EINVAL;
     142             :                 }
     143             : 
     144           0 :                 ret = drm_mode_convert_umode(crtc->dev,
     145           0 :                                              &state->mode, blob->data);
     146           0 :                 if (ret) {
     147           0 :                         drm_dbg_atomic(crtc->dev,
     148             :                                        "[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
     149             :                                        crtc->base.id, crtc->name,
     150             :                                        ret, drm_get_mode_status_name(state->mode.status));
     151           0 :                         drm_mode_debug_printmodeline(&state->mode);
     152           0 :                         return -EINVAL;
     153             :                 }
     154             : 
     155           0 :                 state->mode_blob = drm_property_blob_get(blob);
     156           0 :                 state->enable = true;
     157           0 :                 drm_dbg_atomic(crtc->dev,
     158             :                                "Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
     159             :                                state->mode.name, crtc->base.id, crtc->name,
     160             :                                state);
     161             :         } else {
     162           0 :                 state->enable = false;
     163           0 :                 drm_dbg_atomic(crtc->dev,
     164             :                                "Set [NOMODE] for [CRTC:%d:%s] state %p\n",
     165             :                                crtc->base.id, crtc->name, state);
     166             :         }
     167             : 
     168             :         return 0;
     169             : }
     170             : EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
     171             : 
     172             : /**
     173             :  * drm_atomic_set_crtc_for_plane - set CRTC for plane
     174             :  * @plane_state: the plane whose incoming state to update
     175             :  * @crtc: CRTC to use for the plane
     176             :  *
     177             :  * Changing the assigned CRTC for a plane requires us to grab the lock and state
     178             :  * for the new CRTC, as needed. This function takes care of all these details
     179             :  * besides updating the pointer in the state object itself.
     180             :  *
     181             :  * Returns:
     182             :  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
     183             :  * then the w/w mutex code has detected a deadlock and the entire atomic
     184             :  * sequence must be restarted. All other errors are fatal.
     185             :  */
     186             : int
     187           0 : drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
     188             :                               struct drm_crtc *crtc)
     189             : {
     190           0 :         struct drm_plane *plane = plane_state->plane;
     191             :         struct drm_crtc_state *crtc_state;
     192             :         /* Nothing to do for same crtc*/
     193           0 :         if (plane_state->crtc == crtc)
     194             :                 return 0;
     195           0 :         if (plane_state->crtc) {
     196           0 :                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
     197             :                                                        plane_state->crtc);
     198           0 :                 if (WARN_ON(IS_ERR(crtc_state)))
     199           0 :                         return PTR_ERR(crtc_state);
     200             : 
     201           0 :                 crtc_state->plane_mask &= ~drm_plane_mask(plane);
     202             :         }
     203             : 
     204           0 :         plane_state->crtc = crtc;
     205             : 
     206           0 :         if (crtc) {
     207           0 :                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
     208             :                                                        crtc);
     209           0 :                 if (IS_ERR(crtc_state))
     210           0 :                         return PTR_ERR(crtc_state);
     211           0 :                 crtc_state->plane_mask |= drm_plane_mask(plane);
     212             :         }
     213             : 
     214           0 :         if (crtc)
     215           0 :                 drm_dbg_atomic(plane->dev,
     216             :                                "Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
     217             :                                plane->base.id, plane->name, plane_state,
     218             :                                crtc->base.id, crtc->name);
     219             :         else
     220           0 :                 drm_dbg_atomic(plane->dev,
     221             :                                "Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
     222             :                                plane->base.id, plane->name, plane_state);
     223             : 
     224             :         return 0;
     225             : }
     226             : EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
     227             : 
     228             : /**
     229             :  * drm_atomic_set_fb_for_plane - set framebuffer for plane
     230             :  * @plane_state: atomic state object for the plane
     231             :  * @fb: fb to use for the plane
     232             :  *
     233             :  * Changing the assigned framebuffer for a plane requires us to grab a reference
     234             :  * to the new fb and drop the reference to the old fb, if there is one. This
     235             :  * function takes care of all these details besides updating the pointer in the
     236             :  * state object itself.
     237             :  */
     238             : void
     239           0 : drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
     240             :                             struct drm_framebuffer *fb)
     241             : {
     242           0 :         struct drm_plane *plane = plane_state->plane;
     243             : 
     244           0 :         if (fb)
     245           0 :                 drm_dbg_atomic(plane->dev,
     246             :                                "Set [FB:%d] for [PLANE:%d:%s] state %p\n",
     247             :                                fb->base.id, plane->base.id, plane->name,
     248             :                                plane_state);
     249             :         else
     250           0 :                 drm_dbg_atomic(plane->dev,
     251             :                                "Set [NOFB] for [PLANE:%d:%s] state %p\n",
     252             :                                plane->base.id, plane->name, plane_state);
     253             : 
     254           0 :         drm_framebuffer_assign(&plane_state->fb, fb);
     255           0 : }
     256             : EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
     257             : 
     258             : /**
     259             :  * drm_atomic_set_crtc_for_connector - set CRTC for connector
     260             :  * @conn_state: atomic state object for the connector
     261             :  * @crtc: CRTC to use for the connector
     262             :  *
     263             :  * Changing the assigned CRTC for a connector requires us to grab the lock and
     264             :  * state for the new CRTC, as needed. This function takes care of all these
     265             :  * details besides updating the pointer in the state object itself.
     266             :  *
     267             :  * Returns:
     268             :  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
     269             :  * then the w/w mutex code has detected a deadlock and the entire atomic
     270             :  * sequence must be restarted. All other errors are fatal.
     271             :  */
     272             : int
     273           0 : drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
     274             :                                   struct drm_crtc *crtc)
     275             : {
     276           0 :         struct drm_connector *connector = conn_state->connector;
     277             :         struct drm_crtc_state *crtc_state;
     278             : 
     279           0 :         if (conn_state->crtc == crtc)
     280             :                 return 0;
     281             : 
     282           0 :         if (conn_state->crtc) {
     283           0 :                 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
     284             :                                                            conn_state->crtc);
     285             : 
     286           0 :                 crtc_state->connector_mask &=
     287           0 :                         ~drm_connector_mask(conn_state->connector);
     288             : 
     289           0 :                 drm_connector_put(conn_state->connector);
     290           0 :                 conn_state->crtc = NULL;
     291             :         }
     292             : 
     293           0 :         if (crtc) {
     294           0 :                 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
     295           0 :                 if (IS_ERR(crtc_state))
     296           0 :                         return PTR_ERR(crtc_state);
     297             : 
     298           0 :                 crtc_state->connector_mask |=
     299           0 :                         drm_connector_mask(conn_state->connector);
     300             : 
     301           0 :                 drm_connector_get(conn_state->connector);
     302           0 :                 conn_state->crtc = crtc;
     303             : 
     304           0 :                 drm_dbg_atomic(connector->dev,
     305             :                                "Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
     306             :                                connector->base.id, connector->name,
     307             :                                conn_state, crtc->base.id, crtc->name);
     308             :         } else {
     309           0 :                 drm_dbg_atomic(connector->dev,
     310             :                                "Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
     311             :                                connector->base.id, connector->name,
     312             :                                conn_state);
     313             :         }
     314             : 
     315             :         return 0;
     316             : }
     317             : EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
     318             : 
     319             : static void set_out_fence_for_crtc(struct drm_atomic_state *state,
     320             :                                    struct drm_crtc *crtc, s32 __user *fence_ptr)
     321             : {
     322           0 :         state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
     323             : }
     324             : 
     325             : static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
     326             :                                           struct drm_crtc *crtc)
     327             : {
     328             :         s32 __user *fence_ptr;
     329             : 
     330           0 :         fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
     331           0 :         state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
     332             : 
     333             :         return fence_ptr;
     334             : }
     335             : 
     336           0 : static int set_out_fence_for_connector(struct drm_atomic_state *state,
     337             :                                         struct drm_connector *connector,
     338             :                                         s32 __user *fence_ptr)
     339             : {
     340           0 :         unsigned int index = drm_connector_index(connector);
     341             : 
     342           0 :         if (!fence_ptr)
     343             :                 return 0;
     344             : 
     345           0 :         if (put_user(-1, fence_ptr))
     346             :                 return -EFAULT;
     347             : 
     348           0 :         state->connectors[index].out_fence_ptr = fence_ptr;
     349             : 
     350             :         return 0;
     351             : }
     352             : 
     353             : static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
     354             :                                                struct drm_connector *connector)
     355             : {
     356           0 :         unsigned int index = drm_connector_index(connector);
     357             :         s32 __user *fence_ptr;
     358             : 
     359           0 :         fence_ptr = state->connectors[index].out_fence_ptr;
     360           0 :         state->connectors[index].out_fence_ptr = NULL;
     361             : 
     362             :         return fence_ptr;
     363             : }
     364             : 
     365             : static int
     366           0 : drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
     367             :                                          struct drm_property_blob **blob,
     368             :                                          uint64_t blob_id,
     369             :                                          ssize_t expected_size,
     370             :                                          ssize_t expected_elem_size,
     371             :                                          bool *replaced)
     372             : {
     373           0 :         struct drm_property_blob *new_blob = NULL;
     374             : 
     375           0 :         if (blob_id != 0) {
     376           0 :                 new_blob = drm_property_lookup_blob(dev, blob_id);
     377           0 :                 if (new_blob == NULL)
     378             :                         return -EINVAL;
     379             : 
     380           0 :                 if (expected_size > 0 &&
     381           0 :                     new_blob->length != expected_size) {
     382           0 :                         drm_property_blob_put(new_blob);
     383           0 :                         return -EINVAL;
     384             :                 }
     385           0 :                 if (expected_elem_size > 0 &&
     386           0 :                     new_blob->length % expected_elem_size != 0) {
     387           0 :                         drm_property_blob_put(new_blob);
     388           0 :                         return -EINVAL;
     389             :                 }
     390             :         }
     391             : 
     392           0 :         *replaced |= drm_property_replace_blob(blob, new_blob);
     393           0 :         drm_property_blob_put(new_blob);
     394             : 
     395           0 :         return 0;
     396             : }
     397             : 
     398           0 : static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
     399             :                 struct drm_crtc_state *state, struct drm_property *property,
     400             :                 uint64_t val)
     401             : {
     402           0 :         struct drm_device *dev = crtc->dev;
     403           0 :         struct drm_mode_config *config = &dev->mode_config;
     404           0 :         bool replaced = false;
     405             :         int ret;
     406             : 
     407           0 :         if (property == config->prop_active)
     408           0 :                 state->active = val;
     409           0 :         else if (property == config->prop_mode_id) {
     410           0 :                 struct drm_property_blob *mode =
     411           0 :                         drm_property_lookup_blob(dev, val);
     412           0 :                 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
     413           0 :                 drm_property_blob_put(mode);
     414           0 :                 return ret;
     415           0 :         } else if (property == config->prop_vrr_enabled) {
     416           0 :                 state->vrr_enabled = val;
     417           0 :         } else if (property == config->degamma_lut_property) {
     418           0 :                 ret = drm_atomic_replace_property_blob_from_id(dev,
     419             :                                         &state->degamma_lut,
     420             :                                         val,
     421             :                                         -1, sizeof(struct drm_color_lut),
     422             :                                         &replaced);
     423           0 :                 state->color_mgmt_changed |= replaced;
     424           0 :                 return ret;
     425           0 :         } else if (property == config->ctm_property) {
     426           0 :                 ret = drm_atomic_replace_property_blob_from_id(dev,
     427             :                                         &state->ctm,
     428             :                                         val,
     429             :                                         sizeof(struct drm_color_ctm), -1,
     430             :                                         &replaced);
     431           0 :                 state->color_mgmt_changed |= replaced;
     432           0 :                 return ret;
     433           0 :         } else if (property == config->gamma_lut_property) {
     434           0 :                 ret = drm_atomic_replace_property_blob_from_id(dev,
     435             :                                         &state->gamma_lut,
     436             :                                         val,
     437             :                                         -1, sizeof(struct drm_color_lut),
     438             :                                         &replaced);
     439           0 :                 state->color_mgmt_changed |= replaced;
     440           0 :                 return ret;
     441           0 :         } else if (property == config->prop_out_fence_ptr) {
     442           0 :                 s32 __user *fence_ptr = u64_to_user_ptr(val);
     443             : 
     444           0 :                 if (!fence_ptr)
     445             :                         return 0;
     446             : 
     447           0 :                 if (put_user(-1, fence_ptr))
     448             :                         return -EFAULT;
     449             : 
     450           0 :                 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
     451           0 :         } else if (property == crtc->scaling_filter_property) {
     452           0 :                 state->scaling_filter = val;
     453           0 :         } else if (crtc->funcs->atomic_set_property) {
     454           0 :                 return crtc->funcs->atomic_set_property(crtc, state, property, val);
     455             :         } else {
     456           0 :                 drm_dbg_atomic(crtc->dev,
     457             :                                "[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
     458             :                                crtc->base.id, crtc->name,
     459             :                                property->base.id, property->name);
     460           0 :                 return -EINVAL;
     461             :         }
     462             : 
     463             :         return 0;
     464             : }
     465             : 
     466             : static int
     467           0 : drm_atomic_crtc_get_property(struct drm_crtc *crtc,
     468             :                 const struct drm_crtc_state *state,
     469             :                 struct drm_property *property, uint64_t *val)
     470             : {
     471           0 :         struct drm_device *dev = crtc->dev;
     472           0 :         struct drm_mode_config *config = &dev->mode_config;
     473             : 
     474           0 :         if (property == config->prop_active)
     475           0 :                 *val = drm_atomic_crtc_effectively_active(state);
     476           0 :         else if (property == config->prop_mode_id)
     477           0 :                 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
     478           0 :         else if (property == config->prop_vrr_enabled)
     479           0 :                 *val = state->vrr_enabled;
     480           0 :         else if (property == config->degamma_lut_property)
     481           0 :                 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
     482           0 :         else if (property == config->ctm_property)
     483           0 :                 *val = (state->ctm) ? state->ctm->base.id : 0;
     484           0 :         else if (property == config->gamma_lut_property)
     485           0 :                 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
     486           0 :         else if (property == config->prop_out_fence_ptr)
     487           0 :                 *val = 0;
     488           0 :         else if (property == crtc->scaling_filter_property)
     489           0 :                 *val = state->scaling_filter;
     490           0 :         else if (crtc->funcs->atomic_get_property)
     491           0 :                 return crtc->funcs->atomic_get_property(crtc, state, property, val);
     492             :         else
     493             :                 return -EINVAL;
     494             : 
     495             :         return 0;
     496             : }
     497             : 
     498           0 : static int drm_atomic_plane_set_property(struct drm_plane *plane,
     499             :                 struct drm_plane_state *state, struct drm_file *file_priv,
     500             :                 struct drm_property *property, uint64_t val)
     501             : {
     502           0 :         struct drm_device *dev = plane->dev;
     503           0 :         struct drm_mode_config *config = &dev->mode_config;
     504           0 :         bool replaced = false;
     505             :         int ret;
     506             : 
     507           0 :         if (property == config->prop_fb_id) {
     508             :                 struct drm_framebuffer *fb;
     509             : 
     510           0 :                 fb = drm_framebuffer_lookup(dev, file_priv, val);
     511           0 :                 drm_atomic_set_fb_for_plane(state, fb);
     512           0 :                 if (fb)
     513             :                         drm_framebuffer_put(fb);
     514           0 :         } else if (property == config->prop_in_fence_fd) {
     515           0 :                 if (state->fence)
     516             :                         return -EINVAL;
     517             : 
     518           0 :                 if (U642I64(val) == -1)
     519             :                         return 0;
     520             : 
     521           0 :                 state->fence = sync_file_get_fence(val);
     522           0 :                 if (!state->fence)
     523             :                         return -EINVAL;
     524             : 
     525           0 :         } else if (property == config->prop_crtc_id) {
     526           0 :                 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
     527             : 
     528           0 :                 if (val && !crtc)
     529             :                         return -EACCES;
     530           0 :                 return drm_atomic_set_crtc_for_plane(state, crtc);
     531           0 :         } else if (property == config->prop_crtc_x) {
     532           0 :                 state->crtc_x = U642I64(val);
     533           0 :         } else if (property == config->prop_crtc_y) {
     534           0 :                 state->crtc_y = U642I64(val);
     535           0 :         } else if (property == config->prop_crtc_w) {
     536           0 :                 state->crtc_w = val;
     537           0 :         } else if (property == config->prop_crtc_h) {
     538           0 :                 state->crtc_h = val;
     539           0 :         } else if (property == config->prop_src_x) {
     540           0 :                 state->src_x = val;
     541           0 :         } else if (property == config->prop_src_y) {
     542           0 :                 state->src_y = val;
     543           0 :         } else if (property == config->prop_src_w) {
     544           0 :                 state->src_w = val;
     545           0 :         } else if (property == config->prop_src_h) {
     546           0 :                 state->src_h = val;
     547           0 :         } else if (property == plane->alpha_property) {
     548           0 :                 state->alpha = val;
     549           0 :         } else if (property == plane->blend_mode_property) {
     550           0 :                 state->pixel_blend_mode = val;
     551           0 :         } else if (property == plane->rotation_property) {
     552           0 :                 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
     553           0 :                         drm_dbg_atomic(plane->dev,
     554             :                                        "[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
     555             :                                        plane->base.id, plane->name, val);
     556           0 :                         return -EINVAL;
     557             :                 }
     558           0 :                 state->rotation = val;
     559           0 :         } else if (property == plane->zpos_property) {
     560           0 :                 state->zpos = val;
     561           0 :         } else if (property == plane->color_encoding_property) {
     562           0 :                 state->color_encoding = val;
     563           0 :         } else if (property == plane->color_range_property) {
     564           0 :                 state->color_range = val;
     565           0 :         } else if (property == config->prop_fb_damage_clips) {
     566           0 :                 ret = drm_atomic_replace_property_blob_from_id(dev,
     567             :                                         &state->fb_damage_clips,
     568             :                                         val,
     569             :                                         -1,
     570             :                                         sizeof(struct drm_rect),
     571             :                                         &replaced);
     572           0 :                 return ret;
     573           0 :         } else if (property == plane->scaling_filter_property) {
     574           0 :                 state->scaling_filter = val;
     575           0 :         } else if (plane->funcs->atomic_set_property) {
     576           0 :                 return plane->funcs->atomic_set_property(plane, state,
     577             :                                 property, val);
     578             :         } else {
     579           0 :                 drm_dbg_atomic(plane->dev,
     580             :                                "[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
     581             :                                plane->base.id, plane->name,
     582             :                                property->base.id, property->name);
     583           0 :                 return -EINVAL;
     584             :         }
     585             : 
     586             :         return 0;
     587             : }
     588             : 
     589             : static int
     590           0 : drm_atomic_plane_get_property(struct drm_plane *plane,
     591             :                 const struct drm_plane_state *state,
     592             :                 struct drm_property *property, uint64_t *val)
     593             : {
     594           0 :         struct drm_device *dev = plane->dev;
     595           0 :         struct drm_mode_config *config = &dev->mode_config;
     596             : 
     597           0 :         if (property == config->prop_fb_id) {
     598           0 :                 *val = (state->fb) ? state->fb->base.id : 0;
     599           0 :         } else if (property == config->prop_in_fence_fd) {
     600           0 :                 *val = -1;
     601           0 :         } else if (property == config->prop_crtc_id) {
     602           0 :                 *val = (state->crtc) ? state->crtc->base.id : 0;
     603           0 :         } else if (property == config->prop_crtc_x) {
     604           0 :                 *val = I642U64(state->crtc_x);
     605           0 :         } else if (property == config->prop_crtc_y) {
     606           0 :                 *val = I642U64(state->crtc_y);
     607           0 :         } else if (property == config->prop_crtc_w) {
     608           0 :                 *val = state->crtc_w;
     609           0 :         } else if (property == config->prop_crtc_h) {
     610           0 :                 *val = state->crtc_h;
     611           0 :         } else if (property == config->prop_src_x) {
     612           0 :                 *val = state->src_x;
     613           0 :         } else if (property == config->prop_src_y) {
     614           0 :                 *val = state->src_y;
     615           0 :         } else if (property == config->prop_src_w) {
     616           0 :                 *val = state->src_w;
     617           0 :         } else if (property == config->prop_src_h) {
     618           0 :                 *val = state->src_h;
     619           0 :         } else if (property == plane->alpha_property) {
     620           0 :                 *val = state->alpha;
     621           0 :         } else if (property == plane->blend_mode_property) {
     622           0 :                 *val = state->pixel_blend_mode;
     623           0 :         } else if (property == plane->rotation_property) {
     624           0 :                 *val = state->rotation;
     625           0 :         } else if (property == plane->zpos_property) {
     626           0 :                 *val = state->zpos;
     627           0 :         } else if (property == plane->color_encoding_property) {
     628           0 :                 *val = state->color_encoding;
     629           0 :         } else if (property == plane->color_range_property) {
     630           0 :                 *val = state->color_range;
     631           0 :         } else if (property == config->prop_fb_damage_clips) {
     632           0 :                 *val = (state->fb_damage_clips) ?
     633           0 :                         state->fb_damage_clips->base.id : 0;
     634           0 :         } else if (property == plane->scaling_filter_property) {
     635           0 :                 *val = state->scaling_filter;
     636           0 :         } else if (plane->funcs->atomic_get_property) {
     637           0 :                 return plane->funcs->atomic_get_property(plane, state, property, val);
     638             :         } else {
     639             :                 return -EINVAL;
     640             :         }
     641             : 
     642             :         return 0;
     643             : }
     644             : 
     645           0 : static int drm_atomic_set_writeback_fb_for_connector(
     646             :                 struct drm_connector_state *conn_state,
     647             :                 struct drm_framebuffer *fb)
     648             : {
     649             :         int ret;
     650           0 :         struct drm_connector *conn = conn_state->connector;
     651             : 
     652           0 :         ret = drm_writeback_set_fb(conn_state, fb);
     653           0 :         if (ret < 0)
     654             :                 return ret;
     655             : 
     656           0 :         if (fb)
     657           0 :                 drm_dbg_atomic(conn->dev,
     658             :                                "Set [FB:%d] for connector state %p\n",
     659             :                                fb->base.id, conn_state);
     660             :         else
     661           0 :                 drm_dbg_atomic(conn->dev,
     662             :                                "Set [NOFB] for connector state %p\n",
     663             :                                conn_state);
     664             : 
     665             :         return 0;
     666             : }
     667             : 
     668           0 : static int drm_atomic_connector_set_property(struct drm_connector *connector,
     669             :                 struct drm_connector_state *state, struct drm_file *file_priv,
     670             :                 struct drm_property *property, uint64_t val)
     671             : {
     672           0 :         struct drm_device *dev = connector->dev;
     673           0 :         struct drm_mode_config *config = &dev->mode_config;
     674           0 :         bool replaced = false;
     675             :         int ret;
     676             : 
     677           0 :         if (property == config->prop_crtc_id) {
     678           0 :                 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
     679             : 
     680           0 :                 if (val && !crtc)
     681             :                         return -EACCES;
     682           0 :                 return drm_atomic_set_crtc_for_connector(state, crtc);
     683           0 :         } else if (property == config->dpms_property) {
     684             :                 /* setting DPMS property requires special handling, which
     685             :                  * is done in legacy setprop path for us.  Disallow (for
     686             :                  * now?) atomic writes to DPMS property:
     687             :                  */
     688             :                 return -EINVAL;
     689           0 :         } else if (property == config->tv_select_subconnector_property) {
     690           0 :                 state->tv.select_subconnector = val;
     691           0 :         } else if (property == config->tv_subconnector_property) {
     692           0 :                 state->tv.subconnector = val;
     693           0 :         } else if (property == config->tv_left_margin_property) {
     694           0 :                 state->tv.margins.left = val;
     695           0 :         } else if (property == config->tv_right_margin_property) {
     696           0 :                 state->tv.margins.right = val;
     697           0 :         } else if (property == config->tv_top_margin_property) {
     698           0 :                 state->tv.margins.top = val;
     699           0 :         } else if (property == config->tv_bottom_margin_property) {
     700           0 :                 state->tv.margins.bottom = val;
     701           0 :         } else if (property == config->legacy_tv_mode_property) {
     702           0 :                 state->tv.legacy_mode = val;
     703           0 :         } else if (property == config->tv_mode_property) {
     704           0 :                 state->tv.mode = val;
     705           0 :         } else if (property == config->tv_brightness_property) {
     706           0 :                 state->tv.brightness = val;
     707           0 :         } else if (property == config->tv_contrast_property) {
     708           0 :                 state->tv.contrast = val;
     709           0 :         } else if (property == config->tv_flicker_reduction_property) {
     710           0 :                 state->tv.flicker_reduction = val;
     711           0 :         } else if (property == config->tv_overscan_property) {
     712           0 :                 state->tv.overscan = val;
     713           0 :         } else if (property == config->tv_saturation_property) {
     714           0 :                 state->tv.saturation = val;
     715           0 :         } else if (property == config->tv_hue_property) {
     716           0 :                 state->tv.hue = val;
     717           0 :         } else if (property == config->link_status_property) {
     718             :                 /* Never downgrade from GOOD to BAD on userspace's request here,
     719             :                  * only hw issues can do that.
     720             :                  *
     721             :                  * For an atomic property the userspace doesn't need to be able
     722             :                  * to understand all the properties, but needs to be able to
     723             :                  * restore the state it wants on VT switch. So if the userspace
     724             :                  * tries to change the link_status from GOOD to BAD, driver
     725             :                  * silently rejects it and returns a 0. This prevents userspace
     726             :                  * from accidentally breaking  the display when it restores the
     727             :                  * state.
     728             :                  */
     729           0 :                 if (state->link_status != DRM_LINK_STATUS_GOOD)
     730           0 :                         state->link_status = val;
     731           0 :         } else if (property == config->hdr_output_metadata_property) {
     732           0 :                 ret = drm_atomic_replace_property_blob_from_id(dev,
     733             :                                 &state->hdr_output_metadata,
     734             :                                 val,
     735             :                                 sizeof(struct hdr_output_metadata), -1,
     736             :                                 &replaced);
     737           0 :                 return ret;
     738           0 :         } else if (property == config->aspect_ratio_property) {
     739           0 :                 state->picture_aspect_ratio = val;
     740           0 :         } else if (property == config->content_type_property) {
     741           0 :                 state->content_type = val;
     742           0 :         } else if (property == connector->scaling_mode_property) {
     743           0 :                 state->scaling_mode = val;
     744           0 :         } else if (property == config->content_protection_property) {
     745           0 :                 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
     746           0 :                         drm_dbg_kms(dev, "only drivers can set CP Enabled\n");
     747           0 :                         return -EINVAL;
     748             :                 }
     749           0 :                 state->content_protection = val;
     750           0 :         } else if (property == config->hdcp_content_type_property) {
     751           0 :                 state->hdcp_content_type = val;
     752           0 :         } else if (property == connector->colorspace_property) {
     753           0 :                 state->colorspace = val;
     754           0 :         } else if (property == config->writeback_fb_id_property) {
     755             :                 struct drm_framebuffer *fb;
     756             :                 int ret;
     757             : 
     758           0 :                 fb = drm_framebuffer_lookup(dev, file_priv, val);
     759           0 :                 ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
     760           0 :                 if (fb)
     761             :                         drm_framebuffer_put(fb);
     762             :                 return ret;
     763           0 :         } else if (property == config->writeback_out_fence_ptr_property) {
     764           0 :                 s32 __user *fence_ptr = u64_to_user_ptr(val);
     765             : 
     766           0 :                 return set_out_fence_for_connector(state->state, connector,
     767             :                                                    fence_ptr);
     768           0 :         } else if (property == connector->max_bpc_property) {
     769           0 :                 state->max_requested_bpc = val;
     770           0 :         } else if (property == connector->privacy_screen_sw_state_property) {
     771           0 :                 state->privacy_screen_sw_state = val;
     772           0 :         } else if (connector->funcs->atomic_set_property) {
     773           0 :                 return connector->funcs->atomic_set_property(connector,
     774             :                                 state, property, val);
     775             :         } else {
     776           0 :                 drm_dbg_atomic(connector->dev,
     777             :                                "[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
     778             :                                connector->base.id, connector->name,
     779             :                                property->base.id, property->name);
     780           0 :                 return -EINVAL;
     781             :         }
     782             : 
     783             :         return 0;
     784             : }
     785             : 
     786             : static int
     787           0 : drm_atomic_connector_get_property(struct drm_connector *connector,
     788             :                 const struct drm_connector_state *state,
     789             :                 struct drm_property *property, uint64_t *val)
     790             : {
     791           0 :         struct drm_device *dev = connector->dev;
     792           0 :         struct drm_mode_config *config = &dev->mode_config;
     793             : 
     794           0 :         if (property == config->prop_crtc_id) {
     795           0 :                 *val = (state->crtc) ? state->crtc->base.id : 0;
     796           0 :         } else if (property == config->dpms_property) {
     797           0 :                 if (state->crtc && state->crtc->state->self_refresh_active)
     798           0 :                         *val = DRM_MODE_DPMS_ON;
     799             :                 else
     800           0 :                         *val = connector->dpms;
     801           0 :         } else if (property == config->tv_select_subconnector_property) {
     802           0 :                 *val = state->tv.select_subconnector;
     803           0 :         } else if (property == config->tv_subconnector_property) {
     804           0 :                 *val = state->tv.subconnector;
     805           0 :         } else if (property == config->tv_left_margin_property) {
     806           0 :                 *val = state->tv.margins.left;
     807           0 :         } else if (property == config->tv_right_margin_property) {
     808           0 :                 *val = state->tv.margins.right;
     809           0 :         } else if (property == config->tv_top_margin_property) {
     810           0 :                 *val = state->tv.margins.top;
     811           0 :         } else if (property == config->tv_bottom_margin_property) {
     812           0 :                 *val = state->tv.margins.bottom;
     813           0 :         } else if (property == config->legacy_tv_mode_property) {
     814           0 :                 *val = state->tv.legacy_mode;
     815           0 :         } else if (property == config->tv_mode_property) {
     816           0 :                 *val = state->tv.mode;
     817           0 :         } else if (property == config->tv_brightness_property) {
     818           0 :                 *val = state->tv.brightness;
     819           0 :         } else if (property == config->tv_contrast_property) {
     820           0 :                 *val = state->tv.contrast;
     821           0 :         } else if (property == config->tv_flicker_reduction_property) {
     822           0 :                 *val = state->tv.flicker_reduction;
     823           0 :         } else if (property == config->tv_overscan_property) {
     824           0 :                 *val = state->tv.overscan;
     825           0 :         } else if (property == config->tv_saturation_property) {
     826           0 :                 *val = state->tv.saturation;
     827           0 :         } else if (property == config->tv_hue_property) {
     828           0 :                 *val = state->tv.hue;
     829           0 :         } else if (property == config->link_status_property) {
     830           0 :                 *val = state->link_status;
     831           0 :         } else if (property == config->aspect_ratio_property) {
     832           0 :                 *val = state->picture_aspect_ratio;
     833           0 :         } else if (property == config->content_type_property) {
     834           0 :                 *val = state->content_type;
     835           0 :         } else if (property == connector->colorspace_property) {
     836           0 :                 *val = state->colorspace;
     837           0 :         } else if (property == connector->scaling_mode_property) {
     838           0 :                 *val = state->scaling_mode;
     839           0 :         } else if (property == config->hdr_output_metadata_property) {
     840           0 :                 *val = state->hdr_output_metadata ?
     841           0 :                         state->hdr_output_metadata->base.id : 0;
     842           0 :         } else if (property == config->content_protection_property) {
     843           0 :                 *val = state->content_protection;
     844           0 :         } else if (property == config->hdcp_content_type_property) {
     845           0 :                 *val = state->hdcp_content_type;
     846           0 :         } else if (property == config->writeback_fb_id_property) {
     847             :                 /* Writeback framebuffer is one-shot, write and forget */
     848           0 :                 *val = 0;
     849           0 :         } else if (property == config->writeback_out_fence_ptr_property) {
     850           0 :                 *val = 0;
     851           0 :         } else if (property == connector->max_bpc_property) {
     852           0 :                 *val = state->max_requested_bpc;
     853           0 :         } else if (property == connector->privacy_screen_sw_state_property) {
     854           0 :                 *val = state->privacy_screen_sw_state;
     855           0 :         } else if (connector->funcs->atomic_get_property) {
     856           0 :                 return connector->funcs->atomic_get_property(connector,
     857             :                                 state, property, val);
     858             :         } else {
     859             :                 return -EINVAL;
     860             :         }
     861             : 
     862             :         return 0;
     863             : }
     864             : 
     865           0 : int drm_atomic_get_property(struct drm_mode_object *obj,
     866             :                 struct drm_property *property, uint64_t *val)
     867             : {
     868           0 :         struct drm_device *dev = property->dev;
     869             :         int ret;
     870             : 
     871           0 :         switch (obj->type) {
     872             :         case DRM_MODE_OBJECT_CONNECTOR: {
     873           0 :                 struct drm_connector *connector = obj_to_connector(obj);
     874             : 
     875           0 :                 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
     876           0 :                 ret = drm_atomic_connector_get_property(connector,
     877           0 :                                 connector->state, property, val);
     878           0 :                 break;
     879             :         }
     880             :         case DRM_MODE_OBJECT_CRTC: {
     881           0 :                 struct drm_crtc *crtc = obj_to_crtc(obj);
     882             : 
     883           0 :                 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
     884           0 :                 ret = drm_atomic_crtc_get_property(crtc,
     885           0 :                                 crtc->state, property, val);
     886           0 :                 break;
     887             :         }
     888             :         case DRM_MODE_OBJECT_PLANE: {
     889           0 :                 struct drm_plane *plane = obj_to_plane(obj);
     890             : 
     891           0 :                 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
     892           0 :                 ret = drm_atomic_plane_get_property(plane,
     893           0 :                                 plane->state, property, val);
     894           0 :                 break;
     895             :         }
     896             :         default:
     897             :                 ret = -EINVAL;
     898             :                 break;
     899             :         }
     900             : 
     901           0 :         return ret;
     902             : }
     903             : 
     904             : /*
     905             :  * The big monster ioctl
     906             :  */
     907             : 
     908           0 : static struct drm_pending_vblank_event *create_vblank_event(
     909             :                 struct drm_crtc *crtc, uint64_t user_data)
     910             : {
     911           0 :         struct drm_pending_vblank_event *e = NULL;
     912             : 
     913           0 :         e = kzalloc(sizeof *e, GFP_KERNEL);
     914           0 :         if (!e)
     915             :                 return NULL;
     916             : 
     917           0 :         e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
     918           0 :         e->event.base.length = sizeof(e->event);
     919           0 :         e->event.vbl.crtc_id = crtc->base.id;
     920           0 :         e->event.vbl.user_data = user_data;
     921             : 
     922             :         return e;
     923             : }
     924             : 
     925           0 : int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
     926             :                                      struct drm_connector *connector,
     927             :                                      int mode)
     928             : {
     929             :         struct drm_connector *tmp_connector;
     930             :         struct drm_connector_state *new_conn_state;
     931             :         struct drm_crtc *crtc;
     932             :         struct drm_crtc_state *crtc_state;
     933           0 :         int i, ret, old_mode = connector->dpms;
     934           0 :         bool active = false;
     935             : 
     936           0 :         ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
     937             :                                state->acquire_ctx);
     938           0 :         if (ret)
     939             :                 return ret;
     940             : 
     941           0 :         if (mode != DRM_MODE_DPMS_ON)
     942           0 :                 mode = DRM_MODE_DPMS_OFF;
     943           0 :         connector->dpms = mode;
     944             : 
     945           0 :         crtc = connector->state->crtc;
     946           0 :         if (!crtc)
     947             :                 goto out;
     948           0 :         ret = drm_atomic_add_affected_connectors(state, crtc);
     949           0 :         if (ret)
     950             :                 goto out;
     951             : 
     952           0 :         crtc_state = drm_atomic_get_crtc_state(state, crtc);
     953           0 :         if (IS_ERR(crtc_state)) {
     954           0 :                 ret = PTR_ERR(crtc_state);
     955           0 :                 goto out;
     956             :         }
     957             : 
     958           0 :         for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
     959           0 :                 if (new_conn_state->crtc != crtc)
     960           0 :                         continue;
     961           0 :                 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
     962             :                         active = true;
     963             :                         break;
     964             :                 }
     965             :         }
     966             : 
     967           0 :         crtc_state->active = active;
     968           0 :         ret = drm_atomic_commit(state);
     969             : out:
     970           0 :         if (ret != 0)
     971           0 :                 connector->dpms = old_mode;
     972             :         return ret;
     973             : }
     974             : 
     975           0 : int drm_atomic_set_property(struct drm_atomic_state *state,
     976             :                             struct drm_file *file_priv,
     977             :                             struct drm_mode_object *obj,
     978             :                             struct drm_property *prop,
     979             :                             uint64_t prop_value)
     980             : {
     981             :         struct drm_mode_object *ref;
     982             :         int ret;
     983             : 
     984           0 :         if (!drm_property_change_valid_get(prop, prop_value, &ref))
     985             :                 return -EINVAL;
     986             : 
     987           0 :         switch (obj->type) {
     988             :         case DRM_MODE_OBJECT_CONNECTOR: {
     989           0 :                 struct drm_connector *connector = obj_to_connector(obj);
     990             :                 struct drm_connector_state *connector_state;
     991             : 
     992           0 :                 connector_state = drm_atomic_get_connector_state(state, connector);
     993           0 :                 if (IS_ERR(connector_state)) {
     994           0 :                         ret = PTR_ERR(connector_state);
     995           0 :                         break;
     996             :                 }
     997             : 
     998           0 :                 ret = drm_atomic_connector_set_property(connector,
     999             :                                 connector_state, file_priv,
    1000             :                                 prop, prop_value);
    1001           0 :                 break;
    1002             :         }
    1003             :         case DRM_MODE_OBJECT_CRTC: {
    1004           0 :                 struct drm_crtc *crtc = obj_to_crtc(obj);
    1005             :                 struct drm_crtc_state *crtc_state;
    1006             : 
    1007           0 :                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
    1008           0 :                 if (IS_ERR(crtc_state)) {
    1009           0 :                         ret = PTR_ERR(crtc_state);
    1010           0 :                         break;
    1011             :                 }
    1012             : 
    1013           0 :                 ret = drm_atomic_crtc_set_property(crtc,
    1014             :                                 crtc_state, prop, prop_value);
    1015           0 :                 break;
    1016             :         }
    1017             :         case DRM_MODE_OBJECT_PLANE: {
    1018           0 :                 struct drm_plane *plane = obj_to_plane(obj);
    1019             :                 struct drm_plane_state *plane_state;
    1020             : 
    1021           0 :                 plane_state = drm_atomic_get_plane_state(state, plane);
    1022           0 :                 if (IS_ERR(plane_state)) {
    1023           0 :                         ret = PTR_ERR(plane_state);
    1024           0 :                         break;
    1025             :                 }
    1026             : 
    1027           0 :                 ret = drm_atomic_plane_set_property(plane,
    1028             :                                 plane_state, file_priv,
    1029             :                                 prop, prop_value);
    1030           0 :                 break;
    1031             :         }
    1032             :         default:
    1033             :                 ret = -EINVAL;
    1034             :                 break;
    1035             :         }
    1036             : 
    1037           0 :         drm_property_change_valid_put(prop, ref);
    1038           0 :         return ret;
    1039             : }
    1040             : 
    1041             : /**
    1042             :  * DOC: explicit fencing properties
    1043             :  *
    1044             :  * Explicit fencing allows userspace to control the buffer synchronization
    1045             :  * between devices. A Fence or a group of fences are transferred to/from
    1046             :  * userspace using Sync File fds and there are two DRM properties for that.
    1047             :  * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
    1048             :  * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
    1049             :  *
    1050             :  * As a contrast, with implicit fencing the kernel keeps track of any
    1051             :  * ongoing rendering, and automatically ensures that the atomic update waits
    1052             :  * for any pending rendering to complete. This is usually tracked in &struct
    1053             :  * dma_resv which can also contain mandatory kernel fences. Implicit syncing
    1054             :  * is how Linux traditionally worked (e.g. DRI2/3 on X.org), whereas explicit
    1055             :  * fencing is what Android wants.
    1056             :  *
    1057             :  * "IN_FENCE_FD”:
    1058             :  *      Use this property to pass a fence that DRM should wait on before
    1059             :  *      proceeding with the Atomic Commit request and show the framebuffer for
    1060             :  *      the plane on the screen. The fence can be either a normal fence or a
    1061             :  *      merged one, the sync_file framework will handle both cases and use a
    1062             :  *      fence_array if a merged fence is received. Passing -1 here means no
    1063             :  *      fences to wait on.
    1064             :  *
    1065             :  *      If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
    1066             :  *      it will only check if the Sync File is a valid one.
    1067             :  *
    1068             :  *      On the driver side the fence is stored on the @fence parameter of
    1069             :  *      &struct drm_plane_state. Drivers which also support implicit fencing
    1070             :  *      should extract the implicit fence using drm_gem_plane_helper_prepare_fb(),
    1071             :  *      to make sure there's consistent behaviour between drivers in precedence
    1072             :  *      of implicit vs. explicit fencing.
    1073             :  *
    1074             :  * "OUT_FENCE_PTR”:
    1075             :  *      Use this property to pass a file descriptor pointer to DRM. Once the
    1076             :  *      Atomic Commit request call returns OUT_FENCE_PTR will be filled with
    1077             :  *      the file descriptor number of a Sync File. This Sync File contains the
    1078             :  *      CRTC fence that will be signaled when all framebuffers present on the
    1079             :  *      Atomic Commit * request for that given CRTC are scanned out on the
    1080             :  *      screen.
    1081             :  *
    1082             :  *      The Atomic Commit request fails if a invalid pointer is passed. If the
    1083             :  *      Atomic Commit request fails for any other reason the out fence fd
    1084             :  *      returned will be -1. On a Atomic Commit with the
    1085             :  *      DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
    1086             :  *
    1087             :  *      Note that out-fences don't have a special interface to drivers and are
    1088             :  *      internally represented by a &struct drm_pending_vblank_event in struct
    1089             :  *      &drm_crtc_state, which is also used by the nonblocking atomic commit
    1090             :  *      helpers and for the DRM event handling for existing userspace.
    1091             :  */
    1092             : 
    1093             : struct drm_out_fence_state {
    1094             :         s32 __user *out_fence_ptr;
    1095             :         struct sync_file *sync_file;
    1096             :         int fd;
    1097             : };
    1098             : 
    1099           0 : static int setup_out_fence(struct drm_out_fence_state *fence_state,
    1100             :                            struct dma_fence *fence)
    1101             : {
    1102           0 :         fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
    1103           0 :         if (fence_state->fd < 0)
    1104             :                 return fence_state->fd;
    1105             : 
    1106           0 :         if (put_user(fence_state->fd, fence_state->out_fence_ptr))
    1107             :                 return -EFAULT;
    1108             : 
    1109           0 :         fence_state->sync_file = sync_file_create(fence);
    1110           0 :         if (!fence_state->sync_file)
    1111             :                 return -ENOMEM;
    1112             : 
    1113           0 :         return 0;
    1114             : }
    1115             : 
    1116           0 : static int prepare_signaling(struct drm_device *dev,
    1117             :                                   struct drm_atomic_state *state,
    1118             :                                   struct drm_mode_atomic *arg,
    1119             :                                   struct drm_file *file_priv,
    1120             :                                   struct drm_out_fence_state **fence_state,
    1121             :                                   unsigned int *num_fences)
    1122             : {
    1123             :         struct drm_crtc *crtc;
    1124             :         struct drm_crtc_state *crtc_state;
    1125             :         struct drm_connector *conn;
    1126             :         struct drm_connector_state *conn_state;
    1127           0 :         int i, c = 0, ret;
    1128             : 
    1129           0 :         if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
    1130             :                 return 0;
    1131             : 
    1132           0 :         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
    1133             :                 s32 __user *fence_ptr;
    1134             : 
    1135           0 :                 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
    1136             : 
    1137           0 :                 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
    1138             :                         struct drm_pending_vblank_event *e;
    1139             : 
    1140           0 :                         e = create_vblank_event(crtc, arg->user_data);
    1141           0 :                         if (!e)
    1142             :                                 return -ENOMEM;
    1143             : 
    1144           0 :                         crtc_state->event = e;
    1145             :                 }
    1146             : 
    1147           0 :                 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
    1148           0 :                         struct drm_pending_vblank_event *e = crtc_state->event;
    1149             : 
    1150           0 :                         if (!file_priv)
    1151           0 :                                 continue;
    1152             : 
    1153           0 :                         ret = drm_event_reserve_init(dev, file_priv, &e->base,
    1154             :                                                      &e->event.base);
    1155           0 :                         if (ret) {
    1156           0 :                                 kfree(e);
    1157           0 :                                 crtc_state->event = NULL;
    1158             :                                 return ret;
    1159             :                         }
    1160             :                 }
    1161             : 
    1162           0 :                 if (fence_ptr) {
    1163             :                         struct dma_fence *fence;
    1164             :                         struct drm_out_fence_state *f;
    1165             : 
    1166           0 :                         f = krealloc(*fence_state, sizeof(**fence_state) *
    1167           0 :                                      (*num_fences + 1), GFP_KERNEL);
    1168           0 :                         if (!f)
    1169             :                                 return -ENOMEM;
    1170             : 
    1171           0 :                         memset(&f[*num_fences], 0, sizeof(*f));
    1172             : 
    1173           0 :                         f[*num_fences].out_fence_ptr = fence_ptr;
    1174           0 :                         *fence_state = f;
    1175             : 
    1176           0 :                         fence = drm_crtc_create_fence(crtc);
    1177           0 :                         if (!fence)
    1178             :                                 return -ENOMEM;
    1179             : 
    1180           0 :                         ret = setup_out_fence(&f[(*num_fences)++], fence);
    1181           0 :                         if (ret) {
    1182           0 :                                 dma_fence_put(fence);
    1183             :                                 return ret;
    1184             :                         }
    1185             : 
    1186           0 :                         crtc_state->event->base.fence = fence;
    1187             :                 }
    1188             : 
    1189           0 :                 c++;
    1190             :         }
    1191             : 
    1192           0 :         for_each_new_connector_in_state(state, conn, conn_state, i) {
    1193             :                 struct drm_writeback_connector *wb_conn;
    1194             :                 struct drm_out_fence_state *f;
    1195             :                 struct dma_fence *fence;
    1196             :                 s32 __user *fence_ptr;
    1197             : 
    1198           0 :                 if (!conn_state->writeback_job)
    1199           0 :                         continue;
    1200             : 
    1201           0 :                 fence_ptr = get_out_fence_for_connector(state, conn);
    1202           0 :                 if (!fence_ptr)
    1203           0 :                         continue;
    1204             : 
    1205           0 :                 f = krealloc(*fence_state, sizeof(**fence_state) *
    1206           0 :                              (*num_fences + 1), GFP_KERNEL);
    1207           0 :                 if (!f)
    1208             :                         return -ENOMEM;
    1209             : 
    1210           0 :                 memset(&f[*num_fences], 0, sizeof(*f));
    1211             : 
    1212           0 :                 f[*num_fences].out_fence_ptr = fence_ptr;
    1213           0 :                 *fence_state = f;
    1214             : 
    1215           0 :                 wb_conn = drm_connector_to_writeback(conn);
    1216           0 :                 fence = drm_writeback_get_out_fence(wb_conn);
    1217           0 :                 if (!fence)
    1218             :                         return -ENOMEM;
    1219             : 
    1220           0 :                 ret = setup_out_fence(&f[(*num_fences)++], fence);
    1221           0 :                 if (ret) {
    1222           0 :                         dma_fence_put(fence);
    1223             :                         return ret;
    1224             :                 }
    1225             : 
    1226           0 :                 conn_state->writeback_job->out_fence = fence;
    1227             :         }
    1228             : 
    1229             :         /*
    1230             :          * Having this flag means user mode pends on event which will never
    1231             :          * reach due to lack of at least one CRTC for signaling
    1232             :          */
    1233           0 :         if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
    1234             :                 return -EINVAL;
    1235             : 
    1236             :         return 0;
    1237             : }
    1238             : 
    1239           0 : static void complete_signaling(struct drm_device *dev,
    1240             :                                struct drm_atomic_state *state,
    1241             :                                struct drm_out_fence_state *fence_state,
    1242             :                                unsigned int num_fences,
    1243             :                                bool install_fds)
    1244             : {
    1245             :         struct drm_crtc *crtc;
    1246             :         struct drm_crtc_state *crtc_state;
    1247             :         int i;
    1248             : 
    1249           0 :         if (install_fds) {
    1250           0 :                 for (i = 0; i < num_fences; i++)
    1251           0 :                         fd_install(fence_state[i].fd,
    1252           0 :                                    fence_state[i].sync_file->file);
    1253             : 
    1254           0 :                 kfree(fence_state);
    1255           0 :                 return;
    1256             :         }
    1257             : 
    1258           0 :         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
    1259           0 :                 struct drm_pending_vblank_event *event = crtc_state->event;
    1260             :                 /*
    1261             :                  * Free the allocated event. drm_atomic_helper_setup_commit
    1262             :                  * can allocate an event too, so only free it if it's ours
    1263             :                  * to prevent a double free in drm_atomic_state_clear.
    1264             :                  */
    1265           0 :                 if (event && (event->base.fence || event->base.file_priv)) {
    1266           0 :                         drm_event_cancel_free(dev, &event->base);
    1267           0 :                         crtc_state->event = NULL;
    1268             :                 }
    1269             :         }
    1270             : 
    1271           0 :         if (!fence_state)
    1272             :                 return;
    1273             : 
    1274           0 :         for (i = 0; i < num_fences; i++) {
    1275           0 :                 if (fence_state[i].sync_file)
    1276           0 :                         fput(fence_state[i].sync_file->file);
    1277           0 :                 if (fence_state[i].fd >= 0)
    1278           0 :                         put_unused_fd(fence_state[i].fd);
    1279             : 
    1280             :                 /* If this fails log error to the user */
    1281           0 :                 if (fence_state[i].out_fence_ptr &&
    1282           0 :                     put_user(-1, fence_state[i].out_fence_ptr))
    1283           0 :                         drm_dbg_atomic(dev, "Couldn't clear out_fence_ptr\n");
    1284             :         }
    1285             : 
    1286           0 :         kfree(fence_state);
    1287             : }
    1288             : 
    1289           0 : int drm_mode_atomic_ioctl(struct drm_device *dev,
    1290             :                           void *data, struct drm_file *file_priv)
    1291             : {
    1292           0 :         struct drm_mode_atomic *arg = data;
    1293           0 :         uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
    1294           0 :         uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
    1295           0 :         uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
    1296           0 :         uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
    1297             :         unsigned int copied_objs, copied_props;
    1298             :         struct drm_atomic_state *state;
    1299             :         struct drm_modeset_acquire_ctx ctx;
    1300             :         struct drm_out_fence_state *fence_state;
    1301           0 :         int ret = 0;
    1302             :         unsigned int i, j, num_fences;
    1303             : 
    1304             :         /* disallow for drivers not supporting atomic: */
    1305           0 :         if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
    1306             :                 return -EOPNOTSUPP;
    1307             : 
    1308             :         /* disallow for userspace that has not enabled atomic cap (even
    1309             :          * though this may be a bit overkill, since legacy userspace
    1310             :          * wouldn't know how to call this ioctl)
    1311             :          */
    1312           0 :         if (!file_priv->atomic) {
    1313           0 :                 drm_dbg_atomic(dev,
    1314             :                                "commit failed: atomic cap not enabled\n");
    1315           0 :                 return -EINVAL;
    1316             :         }
    1317             : 
    1318           0 :         if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) {
    1319           0 :                 drm_dbg_atomic(dev, "commit failed: invalid flag\n");
    1320           0 :                 return -EINVAL;
    1321             :         }
    1322             : 
    1323           0 :         if (arg->reserved) {
    1324           0 :                 drm_dbg_atomic(dev, "commit failed: reserved field set\n");
    1325           0 :                 return -EINVAL;
    1326             :         }
    1327             : 
    1328           0 :         if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) {
    1329           0 :                 drm_dbg_atomic(dev,
    1330             :                                "commit failed: invalid flag DRM_MODE_PAGE_FLIP_ASYNC\n");
    1331           0 :                 return -EINVAL;
    1332             :         }
    1333             : 
    1334             :         /* can't test and expect an event at the same time. */
    1335           0 :         if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
    1336             :                         (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
    1337           0 :                 drm_dbg_atomic(dev,
    1338             :                                "commit failed: page-flip event requested with test-only commit\n");
    1339           0 :                 return -EINVAL;
    1340             :         }
    1341             : 
    1342           0 :         state = drm_atomic_state_alloc(dev);
    1343           0 :         if (!state)
    1344             :                 return -ENOMEM;
    1345             : 
    1346           0 :         drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
    1347           0 :         state->acquire_ctx = &ctx;
    1348           0 :         state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
    1349             : 
    1350             : retry:
    1351           0 :         copied_objs = 0;
    1352           0 :         copied_props = 0;
    1353           0 :         fence_state = NULL;
    1354           0 :         num_fences = 0;
    1355             : 
    1356           0 :         for (i = 0; i < arg->count_objs; i++) {
    1357             :                 uint32_t obj_id, count_props;
    1358             :                 struct drm_mode_object *obj;
    1359             : 
    1360           0 :                 if (get_user(obj_id, objs_ptr + copied_objs)) {
    1361             :                         ret = -EFAULT;
    1362             :                         goto out;
    1363             :                 }
    1364             : 
    1365           0 :                 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
    1366           0 :                 if (!obj) {
    1367             :                         ret = -ENOENT;
    1368             :                         goto out;
    1369             :                 }
    1370             : 
    1371           0 :                 if (!obj->properties) {
    1372           0 :                         drm_mode_object_put(obj);
    1373           0 :                         ret = -ENOENT;
    1374           0 :                         goto out;
    1375             :                 }
    1376             : 
    1377           0 :                 if (get_user(count_props, count_props_ptr + copied_objs)) {
    1378           0 :                         drm_mode_object_put(obj);
    1379           0 :                         ret = -EFAULT;
    1380           0 :                         goto out;
    1381             :                 }
    1382             : 
    1383           0 :                 copied_objs++;
    1384             : 
    1385           0 :                 for (j = 0; j < count_props; j++) {
    1386             :                         uint32_t prop_id;
    1387             :                         uint64_t prop_value;
    1388             :                         struct drm_property *prop;
    1389             : 
    1390           0 :                         if (get_user(prop_id, props_ptr + copied_props)) {
    1391           0 :                                 drm_mode_object_put(obj);
    1392           0 :                                 ret = -EFAULT;
    1393           0 :                                 goto out;
    1394             :                         }
    1395             : 
    1396           0 :                         prop = drm_mode_obj_find_prop_id(obj, prop_id);
    1397           0 :                         if (!prop) {
    1398           0 :                                 drm_mode_object_put(obj);
    1399           0 :                                 ret = -ENOENT;
    1400           0 :                                 goto out;
    1401             :                         }
    1402             : 
    1403           0 :                         if (copy_from_user(&prop_value,
    1404           0 :                                            prop_values_ptr + copied_props,
    1405             :                                            sizeof(prop_value))) {
    1406           0 :                                 drm_mode_object_put(obj);
    1407           0 :                                 ret = -EFAULT;
    1408           0 :                                 goto out;
    1409             :                         }
    1410             : 
    1411           0 :                         ret = drm_atomic_set_property(state, file_priv,
    1412             :                                                       obj, prop, prop_value);
    1413           0 :                         if (ret) {
    1414           0 :                                 drm_mode_object_put(obj);
    1415           0 :                                 goto out;
    1416             :                         }
    1417             : 
    1418           0 :                         copied_props++;
    1419             :                 }
    1420             : 
    1421           0 :                 drm_mode_object_put(obj);
    1422             :         }
    1423             : 
    1424           0 :         ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
    1425             :                                 &num_fences);
    1426           0 :         if (ret)
    1427             :                 goto out;
    1428             : 
    1429           0 :         if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
    1430           0 :                 ret = drm_atomic_check_only(state);
    1431           0 :         } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
    1432           0 :                 ret = drm_atomic_nonblocking_commit(state);
    1433             :         } else {
    1434           0 :                 ret = drm_atomic_commit(state);
    1435             :         }
    1436             : 
    1437             : out:
    1438           0 :         complete_signaling(dev, state, fence_state, num_fences, !ret);
    1439             : 
    1440           0 :         if (ret == -EDEADLK) {
    1441           0 :                 drm_atomic_state_clear(state);
    1442           0 :                 ret = drm_modeset_backoff(&ctx);
    1443           0 :                 if (!ret)
    1444             :                         goto retry;
    1445             :         }
    1446             : 
    1447           0 :         drm_atomic_state_put(state);
    1448             : 
    1449           0 :         drm_modeset_drop_locks(&ctx);
    1450           0 :         drm_modeset_acquire_fini(&ctx);
    1451             : 
    1452           0 :         return ret;
    1453             : }

Generated by: LCOV version 1.14