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@xxxxxxxxxx> --- drivers/gpu/drm/tests/drm_kunit_helpers.c | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) 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 * -- 2.38.1-b4-0.11.0-dev-d416f