diff mbox series

[05/24] drm/tests: helpers: Make sure the device is bound

Message ID 20221123-rpi-kunit-tests-v1-5-051a0bb60a16@cerno.tech (mailing list archive)
State New, archived
Headers show
Series drm: Introduce Kunit Tests to VC4 | expand

Commit Message

Maxime Ripard Nov. 23, 2022, 3:25 p.m. UTC
The device managed resources are freed when the device is detached, so
it has to be bound in the first place.

Let's create a fake driver that we will bind to our fake device to
benefit from the device managed cleanups in our tests.

Signed-off-by: Maxime Ripard <maxime@cerno.tech>
---
 drivers/gpu/drm/tests/drm_kunit_helpers.c | 60 +++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

Comments

Javier Martinez Canillas Nov. 25, 2022, 10:19 a.m. UTC | #1
On 11/23/22 16:25, Maxime Ripard wrote:
> The device managed resources are freed when the device is detached, so
> it has to be bound in the first place.
> 
> Let's create a fake driver that we will bind to our fake device to
> benefit from the device managed cleanups in our tests.
> 
> Signed-off-by: Maxime Ripard <maxime@cerno.tech>
> ---
>  drivers/gpu/drm/tests/drm_kunit_helpers.c | 60 +++++++++++++++++++++++++++++++
>  1 file changed, 60 insertions(+)
>

If I understood the platform core code correctly, the probe is always sync in
the case of platform drivers. Unless .probe_type = PROBE_PREFER_ASYNCHRONOUS is
set or a module is loaded using with the "async_probe=1" parameter. So I believe
the wait queue won't be needed. The only DRM driver that forces an async probe is
drivers/gpu/drm/hyperv/hyperv_drm_drv.c AFAICT.

So I would drop this patch from the set for now.
diff mbox series

Patch

diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
index 651aa93a98a8..8bd7c40d0e1a 100644
--- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
+++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
@@ -10,6 +10,7 @@ 
 
 #include "drm_kunit_helpers.h"
 
+#define WQ_TIMEOUT_MS		100
 #define FAKE_DEVICE_NAME	"drm-kunit-fake-device"
 
 struct kunit_dev {
@@ -19,28 +20,87 @@  struct kunit_dev {
 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
 };
 
+struct fake_pdev_sync_point {
+	bool done;
+	wait_queue_head_t wq;
+};
+
+struct fake_pdev_priv {
+	struct fake_pdev_sync_point probe;
+	struct fake_pdev_sync_point remove;
+};
+
+static int fake_probe(struct platform_device *pdev)
+{
+	struct fake_pdev_priv *priv = *(struct fake_pdev_priv **)dev_get_platdata(&pdev->dev);
+
+	priv->probe.done = true;
+	wake_up_interruptible(&priv->probe.wq);
+
+	return 0;
+}
+
+static int fake_remove(struct platform_device *pdev)
+{
+	struct fake_pdev_priv *priv = *(struct fake_pdev_priv **)dev_get_platdata(&pdev->dev);
+
+	priv->remove.done = true;
+	wake_up_interruptible(&priv->remove.wq);
+
+	return 0;
+}
+
+static struct platform_driver fake_platform_driver = {
+	.probe	= fake_probe,
+	.remove	= fake_remove,
+	.driver = {
+		.name	= FAKE_DEVICE_NAME,
+	},
+};
+
 struct device *drm_kunit_helper_alloc_device(struct kunit *test)
 {
+	struct fake_pdev_priv *priv;
 	struct platform_device *pdev;
 	int ret;
 
+	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, priv);
+	init_waitqueue_head(&priv->probe.wq);
+	init_waitqueue_head(&priv->remove.wq);
+
 	ret = platform_driver_register(&fake_platform_driver);
 	KUNIT_ASSERT_EQ(test, ret, 0);
 
 	pdev = platform_device_alloc(FAKE_DEVICE_NAME, PLATFORM_DEVID_NONE);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
 
+	ret = platform_device_add_data(pdev, &priv, sizeof(priv));
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
 	ret = platform_device_add(pdev);
 	KUNIT_ASSERT_EQ(test, ret, 0);
 
+	ret = wait_event_interruptible_timeout(priv->probe.wq, priv->probe.done,
+					       msecs_to_jiffies(WQ_TIMEOUT_MS));
+	KUNIT_ASSERT_GT(test, ret, 0);
+
 	return &pdev->dev;
 }
 
 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
 {
+	struct fake_pdev_priv *priv = *(struct fake_pdev_priv **)dev_get_platdata(dev);
 	struct platform_device *pdev = to_platform_device(dev);
+	int ret;
 
 	platform_device_unregister(pdev);
+
+	ret = wait_event_interruptible_timeout(priv->probe.wq, priv->remove.done,
+					       msecs_to_jiffies(WQ_TIMEOUT_MS));
+	KUNIT_ASSERT_GT(test, ret, 0);
+
+	platform_driver_unregister(&fake_platform_driver);
 }
 
 struct drm_device *