1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <drm/drm_drv.h> 4 #include <drm/drm_kunit_helpers.h> 5 #include <drm/drm_managed.h> 6 7 #include <kunit/resource.h> 8 9 #include <linux/device.h> 10 11 /* Ought to be enough for anybody */ 12 #define TEST_TIMEOUT_MS 100 13 14 struct managed_test_priv { 15 struct drm_device *drm; 16 bool action_done; 17 wait_queue_head_t action_wq; 18 }; 19 20 static void drm_action(struct drm_device *drm, void *ptr) 21 { 22 struct managed_test_priv *priv = ptr; 23 24 priv->action_done = true; 25 wake_up_interruptible(&priv->action_wq); 26 } 27 28 /* 29 * The test verifies that the release action is called when 30 * drmm_release_action is called. 31 */ 32 static void drm_test_managed_release_action(struct kunit *test) 33 { 34 struct managed_test_priv *priv = test->priv; 35 int ret; 36 37 ret = drmm_add_action_or_reset(priv->drm, drm_action, priv); 38 KUNIT_EXPECT_EQ(test, ret, 0); 39 40 ret = drm_dev_register(priv->drm, 0); 41 KUNIT_ASSERT_EQ(test, ret, 0); 42 43 drmm_release_action(priv->drm, drm_action, priv); 44 ret = wait_event_interruptible_timeout(priv->action_wq, priv->action_done, 45 msecs_to_jiffies(TEST_TIMEOUT_MS)); 46 KUNIT_EXPECT_GT(test, ret, 0); 47 48 drm_dev_unregister(priv->drm); 49 drm_kunit_helper_free_device(test, priv->drm->dev); 50 } 51 52 /* 53 * The test verifies that the release action is called automatically when the 54 * device is released. 55 */ 56 static void drm_test_managed_run_action(struct kunit *test) 57 { 58 struct managed_test_priv *priv = test->priv; 59 int ret; 60 61 ret = drmm_add_action_or_reset(priv->drm, drm_action, priv); 62 KUNIT_EXPECT_EQ(test, ret, 0); 63 64 ret = drm_dev_register(priv->drm, 0); 65 KUNIT_ASSERT_EQ(test, ret, 0); 66 67 drm_dev_unregister(priv->drm); 68 drm_kunit_helper_free_device(test, priv->drm->dev); 69 70 ret = wait_event_interruptible_timeout(priv->action_wq, priv->action_done, 71 msecs_to_jiffies(TEST_TIMEOUT_MS)); 72 KUNIT_EXPECT_GT(test, ret, 0); 73 } 74 75 static int drm_managed_test_init(struct kunit *test) 76 { 77 struct managed_test_priv *priv; 78 struct device *dev; 79 80 priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL); 81 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv); 82 init_waitqueue_head(&priv->action_wq); 83 84 dev = drm_kunit_helper_alloc_device(test); 85 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); 86 87 /* 88 * DRM device can't be embedded in priv, since priv->action_done needs 89 * to remain allocated beyond both parent device and drm_device 90 * lifetime. 91 */ 92 priv->drm = __drm_kunit_helper_alloc_drm_device(test, dev, sizeof(*priv->drm), 0, 93 DRIVER_MODESET); 94 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->drm); 95 96 test->priv = priv; 97 98 return 0; 99 } 100 101 static struct kunit_case drm_managed_tests[] = { 102 KUNIT_CASE(drm_test_managed_release_action), 103 KUNIT_CASE(drm_test_managed_run_action), 104 {} 105 }; 106 107 static struct kunit_suite drm_managed_test_suite = { 108 .name = "drm_managed", 109 .init = drm_managed_test_init, 110 .test_cases = drm_managed_tests 111 }; 112 113 kunit_test_suite(drm_managed_test_suite); 114 115 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>"); 116 MODULE_DESCRIPTION("KUnit DRM managed test suite"); 117 MODULE_LICENSE("GPL"); 118