drm: Introduce drm_fb_helper_prepare()
authorThierry Reding <treding@nvidia.com>
Fri, 27 Jun 2014 15:19:24 +0000 (17:19 +0200)
committerDave Airlie <airlied@redhat.com>
Tue, 8 Jul 2014 01:31:28 +0000 (11:31 +1000)
To implement hotplug detection in a race-free manner, drivers must call
drm_kms_helper_poll_init() before hotplug events can be triggered. Such
events can be triggered right after any of the encoders or connectors
are initialized. At the same time, if the drm_fb_helper_hotplug_event()
helper is used by a driver, then the poll helper requires some parts of
the FB helper to be initialized to prevent a crash.

At the same time, drm_fb_helper_init() requires information that is not
necessarily available at such an early stage (number of CRTCs and
connectors), so it cannot be used yet.

Add a new helper, drm_fb_helper_prepare(), that initializes the bare
minimum needed to allow drm_kms_helper_poll_init() to execute and any
subsequent hotplug events to be processed properly.

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Thierry Reding <treding@nvidia.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
18 files changed:
drivers/gpu/drm/armada/armada_fbdev.c
drivers/gpu/drm/ast/ast_fb.c
drivers/gpu/drm/bochs/bochs_fbdev.c
drivers/gpu/drm/cirrus/cirrus_fbdev.c
drivers/gpu/drm/drm_fb_cma_helper.c
drivers/gpu/drm/drm_fb_helper.c
drivers/gpu/drm/exynos/exynos_drm_fbdev.c
drivers/gpu/drm/gma500/framebuffer.c
drivers/gpu/drm/i915/intel_fbdev.c
drivers/gpu/drm/mgag200/mgag200_fb.c
drivers/gpu/drm/msm/msm_fbdev.c
drivers/gpu/drm/nouveau/nouveau_fbcon.c
drivers/gpu/drm/omapdrm/omap_fbdev.c
drivers/gpu/drm/qxl/qxl_fb.c
drivers/gpu/drm/radeon/radeon_fb.c
drivers/gpu/drm/tegra/fb.c
drivers/gpu/drm/udl/udl_fb.c
include/drm/drm_fb_helper.h

index a7c947cd93868f6b772dddb356123c826aff39f4..7838e731b0de1be856af431aa3848013a3492e09 100644 (file)
@@ -149,7 +149,7 @@ int armada_fbdev_init(struct drm_device *dev)
 
        priv->fbdev = fbh;
 
-       fbh->funcs = &armada_fb_helper_funcs;
+       drm_fb_helper_prepare(dev, fbh, &armada_fb_helper_funcs);
 
        ret = drm_fb_helper_init(dev, fbh, 1, 1);
        if (ret) {
index 2113894e4ff82ce40ff7a40855c937e8d5ec0f02..cba45c77455279ff0aaaad8974e815b06e9b9f5c 100644 (file)
@@ -328,8 +328,10 @@ int ast_fbdev_init(struct drm_device *dev)
                return -ENOMEM;
 
        ast->fbdev = afbdev;
-       afbdev->helper.funcs = &ast_fb_helper_funcs;
        spin_lock_init(&afbdev->dirty_lock);
+
+       drm_fb_helper_prepare(dev, &afbdev->helper, &ast_fb_helper_funcs);
+
        ret = drm_fb_helper_init(dev, &afbdev->helper,
                                 1, 1);
        if (ret) {
index 17e5c17f27305ebf3431252212028b8e7121b8b6..19cf3e9413b631cf6f13d7ad962cf7eda7c89eb5 100644 (file)
@@ -189,7 +189,8 @@ int bochs_fbdev_init(struct bochs_device *bochs)
 {
        int ret;
 
-       bochs->fb.helper.funcs = &bochs_fb_helper_funcs;
+       drm_fb_helper_prepare(bochs->dev, &bochs->fb.helper,
+                             &bochs_fb_helper_funcs);
 
        ret = drm_fb_helper_init(bochs->dev, &bochs->fb.helper,
                                 1, 1);
index 2bd0291168e42cd4414670e182089757a86a08bc..2a135f253e2930dd4d06da29b1a6a0da166969a6 100644 (file)
@@ -306,9 +306,11 @@ int cirrus_fbdev_init(struct cirrus_device *cdev)
                return -ENOMEM;
 
        cdev->mode_info.gfbdev = gfbdev;
-       gfbdev->helper.funcs = &cirrus_fb_helper_funcs;
        spin_lock_init(&gfbdev->dirty_lock);
 
+       drm_fb_helper_prepare(cdev->dev, &gfbdev->helper,
+                             &cirrus_fb_helper_funcs);
+
        ret = drm_fb_helper_init(cdev->dev, &gfbdev->helper,
                                 cdev->num_crtc, CIRRUSFB_CONN_LIMIT);
        if (ret) {
index cb01e16063841c14cd6f7f309a3579304d93822a..cc0ae047ed3bd8fcc265e38a06d8ec86207deb55 100644 (file)
@@ -354,9 +354,10 @@ struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
                return ERR_PTR(-ENOMEM);
        }
 
-       fbdev_cma->fb_helper.funcs = &drm_fb_cma_helper_funcs;
        helper = &fbdev_cma->fb_helper;
 
+       drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
+
        ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count);
        if (ret < 0) {
                dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
index 6cd687a20cd59c7a568ec4976e8748ccf361a8d4..bdee6eb0b24a5f21404a8302443f9151760352e3 100644 (file)
@@ -49,10 +49,11 @@ static LIST_HEAD(kernel_fb_helper_list);
  * helper functions used by many drivers to implement the kernel mode setting
  * interfaces.
  *
- * Initialization is done as a three-step process with drm_fb_helper_init(),
- * drm_fb_helper_single_add_all_connectors() and drm_fb_helper_initial_config().
- * Drivers with fancier requirements than the default behaviour can override the
- * second step with their own code.  Teardown is done with drm_fb_helper_fini().
+ * Initialization is done as a four-step process with drm_fb_helper_prepare(),
+ * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
+ * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
+ * default behaviour can override the third step with their own code.
+ * Teardown is done with drm_fb_helper_fini().
  *
  * At runtime drivers should restore the fbdev console by calling
  * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
@@ -63,6 +64,19 @@ static LIST_HEAD(kernel_fb_helper_list);
  *
  * All other functions exported by the fb helper library can be used to
  * implement the fbdev driver interface by the driver.
+ *
+ * It is possible, though perhaps somewhat tricky, to implement race-free
+ * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
+ * helper must be called first to initialize the minimum required to make
+ * hotplug detection work. Drivers also need to make sure to properly set up
+ * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
+ * it is safe to enable interrupts and start processing hotplug events. At the
+ * same time, drivers should initialize all modeset objects such as CRTCs,
+ * encoders and connectors. To finish up the fbdev helper initialization, the
+ * drm_fb_helper_init() function is called. To probe for all attached displays
+ * and set up an initial configuration using the detected hardware, drivers
+ * should call drm_fb_helper_single_add_all_connectors() followed by
+ * drm_fb_helper_initial_config().
  */
 
 /**
@@ -527,6 +541,24 @@ static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
        kfree(helper->crtc_info);
 }
 
+/**
+ * drm_fb_helper_prepare - setup a drm_fb_helper structure
+ * @dev: DRM device
+ * @helper: driver-allocated fbdev helper structure to set up
+ * @funcs: pointer to structure of functions associate with this helper
+ *
+ * Sets up the bare minimum to make the framebuffer helper usable. This is
+ * useful to implement race-free initialization of the polling helpers.
+ */
+void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
+                          const struct drm_fb_helper_funcs *funcs)
+{
+       INIT_LIST_HEAD(&helper->kernel_fb_list);
+       helper->funcs = funcs;
+       helper->dev = dev;
+}
+EXPORT_SYMBOL(drm_fb_helper_prepare);
+
 /**
  * drm_fb_helper_init - initialize a drm_fb_helper structure
  * @dev: drm device
@@ -539,8 +571,7 @@ static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  * to allow driver writes more control over the exact init sequence.
  *
- * Drivers must set fb_helper->funcs before calling
- * drm_fb_helper_initial_config().
+ * Drivers must call drm_fb_helper_prepare() before calling this function.
  *
  * RETURNS:
  * Zero if everything went ok, nonzero otherwise.
@@ -555,10 +586,6 @@ int drm_fb_helper_init(struct drm_device *dev,
        if (!max_conn_count)
                return -EINVAL;
 
-       fb_helper->dev = dev;
-
-       INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
-
        fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
        if (!fb_helper->crtc_info)
                return -ENOMEM;
index fc25fe75aa775852a2642e5a7ae450c02b7dbf55..32e63f60e1d15a14deef0d11365201e2476e5d3c 100644 (file)
@@ -266,7 +266,8 @@ int exynos_drm_fbdev_init(struct drm_device *dev)
                return -ENOMEM;
 
        private->fb_helper = helper = &fbdev->drm_fb_helper;
-       helper->funcs = &exynos_drm_fb_helper_funcs;
+
+       drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
 
        num_crtc = dev->mode_config.num_crtc;
 
index 76e4d777d01d6cc3daf40dcd66a4b15ad07d10b2..d0dd3bea8aa5a1a2d98b2505139f07dc1db57715 100644 (file)
@@ -600,7 +600,8 @@ int psb_fbdev_init(struct drm_device *dev)
        }
 
        dev_priv->fbdev = fbdev;
-       fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs;
+
+       drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
 
        drm_fb_helper_init(dev, &fbdev->psb_fb_helper, dev_priv->ops->crtcs,
                                                        INTELFB_CONN_LIMIT);
index c942d13cd5cb9ac966e8baf8130d65269136b3b4..347d16220cd053dae98cca1b899525fe955b8dc1 100644 (file)
@@ -623,7 +623,8 @@ int intel_fbdev_init(struct drm_device *dev)
        if (ifbdev == NULL)
                return -ENOMEM;
 
-       ifbdev->helper.funcs = &intel_fb_helper_funcs;
+       drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
+
        if (!intel_fbdev_init_bios(dev, ifbdev))
                ifbdev->preferred_bpp = 32;
 
index a4319aba91802ee9b9d79f9a1e063f0221e206aa..5451dc58eff19ccb81d2eb4780c464e314ae6b11 100644 (file)
@@ -293,9 +293,10 @@ int mgag200_fbdev_init(struct mga_device *mdev)
                return -ENOMEM;
 
        mdev->mfbdev = mfbdev;
-       mfbdev->helper.funcs = &mga_fb_helper_funcs;
        spin_lock_init(&mfbdev->dirty_lock);
 
+       drm_fb_helper_prepare(mdev->dev, &mfbdev->helper, &mga_fb_helper_funcs);
+
        ret = drm_fb_helper_init(mdev->dev, &mfbdev->helper,
                                 mdev->num_crtc, MGAG200FB_CONN_LIMIT);
        if (ret)
index 7e706c4cf033f3d770e0aa7b6c030ad7460f29a2..c437065933e3cab804b6bd9569c698f4e7d321d3 100644 (file)
@@ -197,7 +197,7 @@ struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
 
        helper = &fbdev->base;
 
-       helper->funcs = &msm_fb_helper_funcs;
+       drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
 
        ret = drm_fb_helper_init(dev, helper,
                        priv->num_crtcs, priv->num_connectors);
index 8e9c07b7fc89493de1d67ce3347f3cc65bbb6255..afe706a20f97fded224ff1056419a4470ed82b2c 100644 (file)
@@ -464,7 +464,8 @@ nouveau_fbcon_init(struct drm_device *dev)
 
        fbcon->dev = dev;
        drm->fbcon = fbcon;
-       fbcon->helper.funcs = &nouveau_fbcon_helper_funcs;
+
+       drm_fb_helper_prepare(dev, &fbcon->helper, &nouveau_fbcon_helper_funcs);
 
        ret = drm_fb_helper_init(dev, &fbcon->helper,
                                 dev->mode_config.num_crtc, 4);
index 4cb12083eb128e1863acc4ac370c77aed321a01f..8436c6857cda76f2311af5d612f50315a58438ce 100644 (file)
@@ -325,7 +325,7 @@ struct drm_fb_helper *omap_fbdev_init(struct drm_device *dev)
 
        helper = &fbdev->base;
 
-       helper->funcs = &omap_fb_helper_funcs;
+       drm_fb_helper_prepare(dev, helper, &omap_fb_helper_funcs);
 
        ret = drm_fb_helper_init(dev, helper,
                        priv->num_crtcs, priv->num_connectors);
index cf89614c72be4f667e2b442cb64d1b444ceb45c8..df567888bb1e3e04ef73b3b3f38a8a9e75e1fc5d 100644 (file)
@@ -676,9 +676,12 @@ int qxl_fbdev_init(struct qxl_device *qdev)
 
        qfbdev->qdev = qdev;
        qdev->mode_info.qfbdev = qfbdev;
-       qfbdev->helper.funcs = &qxl_fb_helper_funcs;
        spin_lock_init(&qfbdev->delayed_ops_lock);
        INIT_LIST_HEAD(&qfbdev->delayed_ops);
+
+       drm_fb_helper_prepare(qdev->ddev, &qfbdev->helper,
+                             &qxl_fb_helper_funcs);
+
        ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
                                 qxl_num_crtc /* num_crtc - QXL supports just 1 */,
                                 QXLFB_CONN_LIMIT);
index ad97afdbc4c7d2eed18112f75a34f4ead48561c0..db598d712901ee317190c086f9e69329aa6a8471 100644 (file)
@@ -353,7 +353,9 @@ int radeon_fbdev_init(struct radeon_device *rdev)
 
        rfbdev->rdev = rdev;
        rdev->mode_info.rfbdev = rfbdev;
-       rfbdev->helper.funcs = &radeon_fb_helper_funcs;
+
+       drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
+                             &radeon_fb_helper_funcs);
 
        ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
                                 rdev->num_crtc,
index f7cf47bf0afb3edda72737e1d54e2216b916f24c..d5d53aa79ced7d0b394394fa53446bb0b121557c 100644 (file)
@@ -276,7 +276,6 @@ static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm,
                                              unsigned int num_crtc,
                                              unsigned int max_connectors)
 {
-       struct drm_fb_helper *helper;
        struct tegra_fbdev *fbdev;
        int err;
 
@@ -286,8 +285,7 @@ static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm,
                return ERR_PTR(-ENOMEM);
        }
 
-       fbdev->base.funcs = &tegra_fb_helper_funcs;
-       helper = &fbdev->base;
+       drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
 
        err = drm_fb_helper_init(drm, &fbdev->base, num_crtc, max_connectors);
        if (err < 0) {
index 0647c8cc368b283dc52364c3aa8ef9c422e1d3ba..d1da339843cafb263b21030bc546b5f3d285b394 100644 (file)
@@ -583,7 +583,8 @@ int udl_fbdev_init(struct drm_device *dev)
                return -ENOMEM;
 
        udl->fbdev = ufbdev;
-       ufbdev->helper.funcs = &udl_fb_helper_funcs;
+
+       drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs);
 
        ret = drm_fb_helper_init(dev, &ufbdev->helper,
                                 1, 1);
index a47df7ca38e8a9d1aa82bbcbb5272391cefe43f9..1cf587f1f9274050cdead3ffff84c064d2d0a156 100644 (file)
@@ -97,6 +97,8 @@ struct drm_fb_helper {
        bool delayed_hotplug;
 };
 
+void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
+                          const struct drm_fb_helper_funcs *funcs);
 int drm_fb_helper_init(struct drm_device *dev,
                       struct drm_fb_helper *helper, int crtc_count,
                       int max_conn);