1 From 6f034f1e2e19a86ea85355efdf6dc55fa8a04e59 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Mon, 21 Nov 2022 14:28:44 +0100
4 Subject: [PATCH] drm/tests: helpers: Allow for a custom device struct
7 The current helper to allocate a DRM device doesn't allow for any
8 subclassing by drivers, which is going to be troublesome as we work on
9 getting some kunit testing on atomic modesetting code.
11 Let's use a similar pattern to the other allocation helpers by providing
12 the structure size and offset as arguments.
14 Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
15 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
17 drivers/gpu/drm/tests/drm_kunit_helpers.c | 40 +++++------------------
18 include/drm/drm_kunit_helpers.h | 32 ++++++++++++++++--
19 2 files changed, 39 insertions(+), 33 deletions(-)
21 --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
22 +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
25 #define KUNIT_DEVICE_NAME "drm-kunit-mock-device"
28 - struct drm_device base;
31 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
34 @@ -85,32 +81,14 @@ void drm_kunit_helper_free_device(struct
36 EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
39 - * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
40 - * @test: The test context object
41 - * @dev: The parent device object
42 - * @features: Mocked DRM device driver features
44 - * This function creates a struct &drm_driver and will create a struct
45 - * &drm_device from @dev and that driver.
47 - * @dev should be allocated using drm_kunit_helper_alloc_device().
49 - * The driver is tied to the @test context and will get cleaned at the
50 - * end of the test. The drm_device is allocated through
51 - * devm_drm_dev_alloc() and will thus be freed through a device-managed
55 - * A pointer to the new drm_device, or an ERR_PTR() otherwise.
58 -drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
60 +__drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
61 + size_t size, size_t offset,
64 - struct kunit_dev *kdev;
65 struct drm_device *drm;
66 struct drm_driver *driver;
70 driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
71 @@ -118,11 +96,11 @@ drm_kunit_helper_alloc_drm_device(struct
72 return ERR_PTR(-ENOMEM);
74 driver->driver_features = features;
75 - kdev = devm_drm_dev_alloc(dev, driver, struct kunit_dev, base);
77 - return ERR_CAST(kdev);
78 + container = __devm_drm_dev_alloc(dev, driver, size, offset);
79 + if (IS_ERR(container))
80 + return ERR_CAST(container);
83 + drm = container + offset;
84 drm->mode_config.funcs = &drm_mode_config_funcs;
86 ret = drmm_mode_config_init(drm);
87 @@ -131,7 +109,7 @@ drm_kunit_helper_alloc_drm_device(struct
91 -EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_drm_device);
92 +EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device);
94 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
95 MODULE_LICENSE("GPL");
96 --- a/include/drm/drm_kunit_helpers.h
97 +++ b/include/drm/drm_kunit_helpers.h
98 @@ -10,7 +10,35 @@ struct device *drm_kunit_helper_alloc_de
99 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
102 -drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
104 +__drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
105 + size_t size, size_t offset,
109 + * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
110 + * @_test: The test context object
111 + * @_dev: The parent device object
112 + * @_type: the type of the struct which contains struct &drm_device
113 + * @_member: the name of the &drm_device within @_type.
114 + * @_features: Mocked DRM device driver features
116 + * This function creates a struct &drm_driver and will create a struct
117 + * &drm_device from @_dev and that driver.
119 + * @_dev should be allocated using drm_kunit_helper_alloc_device().
121 + * The driver is tied to the @_test context and will get cleaned at the
122 + * end of the test. The drm_device is allocated through
123 + * devm_drm_dev_alloc() and will thus be freed through a device-managed
127 + * A pointer to the new drm_device, or an ERR_PTR() otherwise.
129 +#define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat) \
130 + ((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev, \
132 + offsetof(_type, _member), \
135 #endif // DRM_KUNIT_HELPERS_H_