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
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
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
14 Reviewed-by: MaĆra Canal <mcanal@igalia.com>
15 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
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(-)
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 = {
27 -static int dev_init(struct kunit_resource *res, void *ptr)
29 + * drm_kunit_helper_alloc_device - Allocate a mock device for a KUnit test
30 + * @test: The test context object
32 + * This allocates a fake struct &device to create a mock for a KUnit
35 + * Callers need to make sure drm_kunit_helper_free_device() on the
39 + * A pointer to the new device, or an ERR_PTR() otherwise.
41 +struct device *drm_kunit_helper_alloc_device(struct kunit *test)
46 - dev = root_device_register(name);
48 - return PTR_ERR(dev);
52 + return root_device_register(KUNIT_DEVICE_NAME);
54 +EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device);
56 -static void dev_free(struct kunit_resource *res)
58 + * drm_kunit_helper_free_device - Frees a mock device
59 + * @test: The test context object
60 + * @dev: The device to free
62 + * Frees a device allocated with drm_kunit_helper_alloc_device().
64 +void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
66 - struct device *dev = res->data;
68 root_device_unregister(dev);
70 +EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
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
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.
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().
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
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.
96 -drm_kunit_helper_alloc_drm_device(struct kunit *test,
97 +drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
100 struct kunit_dev *kdev;
101 struct drm_device *drm;
102 struct drm_driver *driver;
103 - struct device *dev;
106 - dev = kunit_alloc_resource(test, dev_init, dev_free, GFP_KERNEL, KUNIT_DEVICE_NAME);
108 - return ERR_PTR(-ENOMEM);
110 driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
112 return ERR_PTR(-ENOMEM);
113 --- a/include/drm/drm_kunit_helpers.h
114 +++ b/include/drm/drm_kunit_helpers.h
119 +struct device *drm_kunit_helper_alloc_device(struct kunit *test);
120 +void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
123 -drm_kunit_helper_alloc_drm_device(struct kunit *test,
124 +drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
127 #endif // DRM_KUNIT_HELPERS_H_