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 	bool action_done;
16 	wait_queue_head_t action_wq;
17 };
18 
19 static void drm_action(struct drm_device *drm, void *ptr)
20 {
21 	struct managed_test_priv *priv = ptr;
22 
23 	priv->action_done = true;
24 	wake_up_interruptible(&priv->action_wq);
25 }
26 
27 static void drm_test_managed_run_action(struct kunit *test)
28 {
29 	struct managed_test_priv *priv;
30 	struct drm_device *drm;
31 	struct device *dev;
32 	int ret;
33 
34 	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
35 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
36 	init_waitqueue_head(&priv->action_wq);
37 
38 	dev = drm_kunit_helper_alloc_device(test);
39 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
40 
41 	drm = __drm_kunit_helper_alloc_drm_device(test, dev, sizeof(*drm), 0, DRIVER_MODESET);
42 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, drm);
43 
44 	ret = drmm_add_action_or_reset(drm, drm_action, priv);
45 	KUNIT_EXPECT_EQ(test, ret, 0);
46 
47 	ret = drm_dev_register(drm, 0);
48 	KUNIT_ASSERT_EQ(test, ret, 0);
49 
50 	drm_dev_unregister(drm);
51 	drm_kunit_helper_free_device(test, dev);
52 
53 	ret = wait_event_interruptible_timeout(priv->action_wq, priv->action_done,
54 					       msecs_to_jiffies(TEST_TIMEOUT_MS));
55 	KUNIT_EXPECT_GT(test, ret, 0);
56 }
57 
58 static struct kunit_case drm_managed_tests[] = {
59 	KUNIT_CASE(drm_test_managed_run_action),
60 	{}
61 };
62 
63 static struct kunit_suite drm_managed_test_suite = {
64 	.name = "drm-test-managed",
65 	.test_cases = drm_managed_tests
66 };
67 
68 kunit_test_suite(drm_managed_test_suite);
69 
70 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
71 MODULE_LICENSE("GPL");
72