8c5b6531fdff33ced4935d0e65c9f74886fd0987
[openwrt/staging/pepe2k.git] /
1 From cb8e04462f60d3f0659ef3adbba06cdf474f7fe3 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Mon, 21 Nov 2022 11:11:52 +0100
4 Subject: [PATCH] drm/tests: helpers: Create the device in another
5 function
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 We'll need in some tests to control when the device needs to be added
11 and removed, so let's split the device creation from the DRM device
12 creation function.
13
14 Reviewed-by: MaĆ­ra Canal <mcanal@igalia.com>
15 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
16 ---
17 drivers/gpu/drm/tests/drm_kunit_helpers.c | 56 +++++++++++++----------
18 include/drm/drm_kunit_helpers.h | 5 +-
19 2 files changed, 37 insertions(+), 24 deletions(-)
20
21 --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
22 +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
23 @@ -17,36 +17,51 @@ struct kunit_dev {
24 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
25 };
26
27 -static int dev_init(struct kunit_resource *res, void *ptr)
28 +/**
29 + * drm_kunit_helper_alloc_device - Allocate a mock device for a KUnit test
30 + * @test: The test context object
31 + *
32 + * This allocates a fake struct &device to create a mock for a KUnit
33 + * test.
34 + *
35 + * Callers need to make sure drm_kunit_helper_free_device() on the
36 + * device when done.
37 + *
38 + * Returns:
39 + * A pointer to the new device, or an ERR_PTR() otherwise.
40 + */
41 +struct device *drm_kunit_helper_alloc_device(struct kunit *test)
42 {
43 - char *name = ptr;
44 - struct device *dev;
45 -
46 - dev = root_device_register(name);
47 - if (IS_ERR(dev))
48 - return PTR_ERR(dev);
49 -
50 - res->data = dev;
51 - return 0;
52 + return root_device_register(KUNIT_DEVICE_NAME);
53 }
54 +EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device);
55
56 -static void dev_free(struct kunit_resource *res)
57 +/**
58 + * drm_kunit_helper_free_device - Frees a mock device
59 + * @test: The test context object
60 + * @dev: The device to free
61 + *
62 + * Frees a device allocated with drm_kunit_helper_alloc_device().
63 + */
64 +void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
65 {
66 - struct device *dev = res->data;
67 -
68 root_device_unregister(dev);
69 }
70 +EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
71
72 /**
73 * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
74 * @test: The test context object
75 + * @dev: The parent device object
76 * @features: Mocked DRM device driver features
77 *
78 - * This function allocates a new struct &device, creates a struct
79 - * &drm_driver and will create a struct &drm_device using both.
80 + * This function creates a struct &drm_driver and will create a struct
81 + * &drm_device from @dev and that driver.
82 *
83 - * The device and driver are tied to the @test context and will get
84 - * cleaned at the end of the test. The drm_device is allocated through
85 + * @dev should be allocated using drm_kunit_helper_alloc_device().
86 + *
87 + * The driver is tied to the @test context and will get cleaned at the
88 + * end of the test. The drm_device is allocated through
89 * devm_drm_dev_alloc() and will thus be freed through a device-managed
90 * resource.
91 *
92 @@ -54,19 +69,14 @@ static void dev_free(struct kunit_resour
93 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
94 */
95 struct drm_device *
96 -drm_kunit_helper_alloc_drm_device(struct kunit *test,
97 +drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
98 u32 features)
99 {
100 struct kunit_dev *kdev;
101 struct drm_device *drm;
102 struct drm_driver *driver;
103 - struct device *dev;
104 int ret;
105
106 - dev = kunit_alloc_resource(test, dev_init, dev_free, GFP_KERNEL, KUNIT_DEVICE_NAME);
107 - if (!dev)
108 - return ERR_PTR(-ENOMEM);
109 -
110 driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
111 if (!driver)
112 return ERR_PTR(-ENOMEM);
113 --- a/include/drm/drm_kunit_helpers.h
114 +++ b/include/drm/drm_kunit_helpers.h
115 @@ -6,8 +6,11 @@
116 struct drm_device;
117 struct kunit;
118
119 +struct device *drm_kunit_helper_alloc_device(struct kunit *test);
120 +void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
121 +
122 struct drm_device *
123 -drm_kunit_helper_alloc_drm_device(struct kunit *test,
124 +drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
125 u32 features);
126
127 #endif // DRM_KUNIT_HELPERS_H_