drm/atomic: integrate modeset lock with private objects
authorRob Clark <robdclark@gmail.com>
Mon, 22 Oct 2018 12:31:22 +0000 (14:31 +0200)
committerBoris Brezillon <boris.brezillon@bootlin.com>
Tue, 11 Dec 2018 14:24:30 +0000 (15:24 +0100)
Follow the same pattern of locking as with other state objects. This
avoids boilerplate in the driver.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: https://patchwork.freedesktop.org/patch/msgid/20181022123122.30468-1-boris.brezillon@bootlin.com
drivers/gpu/drm/drm_atomic.c
drivers/gpu/drm/drm_dp_mst_topology.c
drivers/gpu/drm/drm_mode_config.c
drivers/gpu/drm/drm_modeset_lock.c
drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c
drivers/gpu/drm/tegra/hub.c
drivers/gpu/drm/vc4/vc4_kms.c
include/drm/drm_atomic.h
include/drm/drm_mode_config.h

index 9ac26437051b478b8d9a93da40c93edbaa87a912..008224f376fe84162afbccb054b382749a008fec 100644 (file)
@@ -676,6 +676,7 @@ static void drm_atomic_plane_print_state(struct drm_printer *p,
 
 /**
  * drm_atomic_private_obj_init - initialize private object
+ * @dev: DRM device this object will be attached to
  * @obj: private object
  * @state: initial private object state
  * @funcs: pointer to the struct of function pointers that identify the object
@@ -685,14 +686,18 @@ static void drm_atomic_plane_print_state(struct drm_printer *p,
  * driver private object that needs its own atomic state.
  */
 void
-drm_atomic_private_obj_init(struct drm_private_obj *obj,
+drm_atomic_private_obj_init(struct drm_device *dev,
+                           struct drm_private_obj *obj,
                            struct drm_private_state *state,
                            const struct drm_private_state_funcs *funcs)
 {
        memset(obj, 0, sizeof(*obj));
 
+       drm_modeset_lock_init(&obj->lock);
+
        obj->state = state;
        obj->funcs = funcs;
+       list_add_tail(&obj->head, &dev->mode_config.privobj_list);
 }
 EXPORT_SYMBOL(drm_atomic_private_obj_init);
 
@@ -705,7 +710,9 @@ EXPORT_SYMBOL(drm_atomic_private_obj_init);
 void
 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
 {
+       list_del(&obj->head);
        obj->funcs->atomic_destroy_state(obj, obj->state);
+       drm_modeset_lock_fini(&obj->lock);
 }
 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
 
@@ -715,8 +722,8 @@ EXPORT_SYMBOL(drm_atomic_private_obj_fini);
  * @obj: private object to get the state for
  *
  * This function returns the private object state for the given private object,
- * allocating the state if needed. It does not grab any locks as the caller is
- * expected to care of any required locking.
+ * allocating the state if needed. It will also grab the relevant private
+ * object lock to make sure that the state is consistent.
  *
  * RETURNS:
  *
@@ -726,7 +733,7 @@ struct drm_private_state *
 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
                                 struct drm_private_obj *obj)
 {
-       int index, num_objs, i;
+       int index, num_objs, i, ret;
        size_t size;
        struct __drm_private_objs_state *arr;
        struct drm_private_state *obj_state;
@@ -735,6 +742,10 @@ drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
                if (obj == state->private_objs[i].ptr)
                        return state->private_objs[i].state;
 
+       ret = drm_modeset_lock(&obj->lock, state->acquire_ctx);
+       if (ret)
+               return ERR_PTR(ret);
+
        num_objs = state->num_private_objs + 1;
        size = sizeof(*state->private_objs) * num_objs;
        arr = krealloc(state->private_objs, size, GFP_KERNEL);
index f0cbd5fad8fc54c662b63dcd68bdbb87311eda92..5a73a0b1c9cd32f348b48693241477e8e485ef48 100644 (file)
@@ -3221,7 +3221,7 @@ int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
        /* max. time slots - one slot for MTP header */
        mst_state->avail_slots = 63;
 
-       drm_atomic_private_obj_init(&mgr->base,
+       drm_atomic_private_obj_init(dev, &mgr->base,
                                    &mst_state->base,
                                    &mst_state_funcs);
 
index ee80788f2c40deb56045fa363e710450f25ef63d..931523c5bc9d879002e57f62029c006233a4253e 100644 (file)
@@ -381,6 +381,7 @@ void drm_mode_config_init(struct drm_device *dev)
        INIT_LIST_HEAD(&dev->mode_config.property_list);
        INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
        INIT_LIST_HEAD(&dev->mode_config.plane_list);
+       INIT_LIST_HEAD(&dev->mode_config.privobj_list);
        idr_init(&dev->mode_config.crtc_idr);
        idr_init(&dev->mode_config.tile_idr);
        ida_init(&dev->mode_config.connector_ida);
index 51f534db91070772e59265d16b3115a4883f2afe..81dd11901ffdd3bc6135c1d54a1a53a23e93fae8 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #include <drm/drmP.h>
+#include <drm/drm_atomic.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_modeset_lock.h>
 
@@ -394,6 +395,7 @@ EXPORT_SYMBOL(drm_modeset_unlock);
 int drm_modeset_lock_all_ctx(struct drm_device *dev,
                             struct drm_modeset_acquire_ctx *ctx)
 {
+       struct drm_private_obj *privobj;
        struct drm_crtc *crtc;
        struct drm_plane *plane;
        int ret;
@@ -414,6 +416,12 @@ int drm_modeset_lock_all_ctx(struct drm_device *dev,
                        return ret;
        }
 
+       drm_for_each_privobj(privobj, dev) {
+               ret = drm_modeset_lock(&privobj->lock, ctx);
+               if (ret)
+                       return ret;
+       }
+
        return 0;
 }
 EXPORT_SYMBOL(drm_modeset_lock_all_ctx);
index bddd625ab91bd44a56824c3e30a8ea2edefe46ff..f71d8cf2261bbc82a29adac62c7c66742415cf2f 100644 (file)
@@ -144,7 +144,7 @@ static int mdp5_global_obj_init(struct mdp5_kms *mdp5_kms)
 
        state->mdp5_kms = mdp5_kms;
 
-       drm_atomic_private_obj_init(&mdp5_kms->glob_state,
+       drm_atomic_private_obj_init(mdp5_kms->dev, &mdp5_kms->glob_state,
                                    &state->base,
                                    &mdp5_global_state_funcs);
        return 0;
index 6112d90429799582098c01a3282fc571aa665517..3e26be5359cf2ec27009ec2855e429d8e800d82e 100644 (file)
@@ -716,7 +716,7 @@ static int tegra_display_hub_init(struct host1x_client *client)
        if (!state)
                return -ENOMEM;
 
-       drm_atomic_private_obj_init(&hub->base, &state->base,
+       drm_atomic_private_obj_init(drm, &hub->base, &state->base,
                                    &tegra_display_hub_state_funcs);
 
        tegra->hub = hub;
index 1f94b9affe4bbafddede75306c3109b1368bc172..0490edb192a193d893d6c99166a9af26aa3584fa 100644 (file)
@@ -432,7 +432,8 @@ int vc4_kms_load(struct drm_device *dev)
        ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
        if (!ctm_state)
                return -ENOMEM;
-       drm_atomic_private_obj_init(&vc4->ctm_manager, &ctm_state->base,
+
+       drm_atomic_private_obj_init(dev, &vc4->ctm_manager, &ctm_state->base,
                                    &vc4_ctm_state_funcs);
 
        drm_mode_config_reset(dev);
index f9b35834c45d0e544fe7c2944353577072818ac2..cac4a1b6b0e8633b4f3ab21de492abea372308c6 100644 (file)
@@ -228,8 +228,30 @@ struct drm_private_state_funcs {
  * Currently only tracks the state update functions and the opaque driver
  * private state itself, but in the future might also track which
  * &drm_modeset_lock is required to duplicate and update this object's state.
+ *
+ * All private objects must be initialized before the DRM device they are
+ * attached to is registered to the DRM subsystem (call to drm_dev_register())
+ * and should stay around until this DRM device is unregistered (call to
+ * drm_dev_unregister()). In other words, private objects lifetime is tied
+ * to the DRM device lifetime. This implies that:
+ *
+ * 1/ all calls to drm_atomic_private_obj_init() must be done before calling
+ *    drm_dev_register()
+ * 2/ all calls to drm_atomic_private_obj_fini() must be done after calling
+ *    drm_dev_unregister()
  */
 struct drm_private_obj {
+       /**
+        * @head: List entry used to attach a private object to a &drm_device
+        * (queued to &drm_mode_config.privobj_list).
+        */
+       struct list_head head;
+
+       /**
+        * @lock: Modeset lock to protect the state object.
+        */
+       struct drm_modeset_lock lock;
+
        /**
         * @state: Current atomic state for this driver private object.
         */
@@ -244,6 +266,18 @@ struct drm_private_obj {
        const struct drm_private_state_funcs *funcs;
 };
 
+/**
+ * drm_for_each_privobj() - private object iterator
+ *
+ * @privobj: pointer to the current private object. Updated after each
+ *          iteration
+ * @dev: the DRM device we want get private objects from
+ *
+ * Allows one to iterate over all private objects attached to @dev
+ */
+#define drm_for_each_privobj(privobj, dev) \
+       list_for_each_entry(privobj, &(dev)->mode_config.privobj_list, head)
+
 /**
  * struct drm_private_state - base struct for driver private object state
  * @state: backpointer to global drm_atomic_state
@@ -400,7 +434,8 @@ struct drm_connector_state * __must_check
 drm_atomic_get_connector_state(struct drm_atomic_state *state,
                               struct drm_connector *connector);
 
-void drm_atomic_private_obj_init(struct drm_private_obj *obj,
+void drm_atomic_private_obj_init(struct drm_device *dev,
+                                struct drm_private_obj *obj,
                                 struct drm_private_state *state,
                                 const struct drm_private_state_funcs *funcs);
 void drm_atomic_private_obj_fini(struct drm_private_obj *obj);
index 5dbeabdbaf91f774c53c35588132835753e66827..d1e27836e0cde1cdf8b2523e75401b88bb47815a 100644 (file)
@@ -512,6 +512,15 @@ struct drm_mode_config {
         */
        struct list_head property_list;
 
+       /**
+        * @privobj_list:
+        *
+        * List of private objects linked with &drm_private_obj.head. This is
+        * invariant over the lifetime of a device and hence doesn't need any
+        * locks.
+        */
+       struct list_head privobj_list;
+
        int min_width, min_height;
        int max_width, max_height;
        const struct drm_mode_config_funcs *funcs;