Skip to content

Commit d987803

Browse files
committed
drm/tests: helpers: Allow to pass a custom drm_driver
Some tests will need to provide their own drm_driver instead of relying on the dumb one in the helpers, so let's create a helper that allows to do so. Reviewed-by: Javier Martinez Canillas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Maxime Ripard <[email protected]>
1 parent a9143c5 commit d987803

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

drivers/gpu/drm/tests/drm_kunit_helpers.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,15 @@ void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
8282
EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
8383

8484
struct drm_device *
85-
__drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
86-
size_t size, size_t offset,
87-
u32 features)
85+
__drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
86+
struct device *dev,
87+
size_t size, size_t offset,
88+
const struct drm_driver *driver)
8889
{
8990
struct drm_device *drm;
90-
struct drm_driver *driver;
9191
void *container;
9292
int ret;
9393

94-
driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
95-
if (!driver)
96-
return ERR_PTR(-ENOMEM);
97-
98-
driver->driver_features = features;
9994
container = __devm_drm_dev_alloc(dev, driver, size, offset);
10095
if (IS_ERR(container))
10196
return ERR_CAST(container);
@@ -109,7 +104,7 @@ __drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
109104

110105
return drm;
111106
}
112-
EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device);
107+
EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver);
113108

114109
MODULE_AUTHOR("Maxime Ripard <[email protected]>");
115110
MODULE_LICENSE("GPL");

include/drm/drm_kunit_helpers.h

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,63 @@
33
#ifndef DRM_KUNIT_HELPERS_H_
44
#define DRM_KUNIT_HELPERS_H_
55

6+
#include <kunit/test.h>
7+
68
struct drm_device;
79
struct kunit;
810

911
struct device *drm_kunit_helper_alloc_device(struct kunit *test);
1012
void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
1113

1214
struct drm_device *
13-
__drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
15+
__drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
16+
struct device *dev,
17+
size_t size, size_t offset,
18+
const struct drm_driver *driver);
19+
20+
/**
21+
* drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
22+
* @_test: The test context object
23+
* @_dev: The parent device object
24+
* @_type: the type of the struct which contains struct &drm_device
25+
* @_member: the name of the &drm_device within @_type.
26+
* @_drv: Mocked DRM device driver features
27+
*
28+
* This function creates a struct &drm_device from @_dev and @_drv.
29+
*
30+
* @_dev should be allocated using drm_kunit_helper_alloc_device().
31+
*
32+
* The driver is tied to the @_test context and will get cleaned at the
33+
* end of the test. The drm_device is allocated through
34+
* devm_drm_dev_alloc() and will thus be freed through a device-managed
35+
* resource.
36+
*
37+
* Returns:
38+
* A pointer to the new drm_device, or an ERR_PTR() otherwise.
39+
*/
40+
#define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv) \
41+
((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, \
42+
sizeof(_type), \
43+
offsetof(_type, _member), \
44+
_drv))
45+
46+
static inline struct drm_device *
47+
__drm_kunit_helper_alloc_drm_device(struct kunit *test,
48+
struct device *dev,
1449
size_t size, size_t offset,
15-
u32 features);
50+
u32 features)
51+
{
52+
struct drm_driver *driver;
53+
54+
driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
55+
KUNIT_ASSERT_NOT_NULL(test, driver);
56+
57+
driver->driver_features = features;
58+
59+
return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
60+
size, offset,
61+
driver);
62+
}
1663

1764
/**
1865
* drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests

0 commit comments

Comments
 (0)