4e88976d9d3624a77e5ba14b52bb47d47fb259b2
[openwrt/staging/pepe2k.git] /
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
5 to be allocated
6
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.
10
11 Let's use a similar pattern to the other allocation helpers by providing
12 the structure size and offset as arguments.
13
14 Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
15 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
16 ---
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(-)
20
21 --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
22 +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
23 @@ -11,10 +11,6 @@
24
25 #define KUNIT_DEVICE_NAME "drm-kunit-mock-device"
26
27 -struct kunit_dev {
28 - struct drm_device base;
29 -};
30 -
31 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
32 };
33
34 @@ -85,32 +81,14 @@ void drm_kunit_helper_free_device(struct
35 }
36 EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
37
38 -/**
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
43 - *
44 - * This function creates a struct &drm_driver and will create a struct
45 - * &drm_device from @dev and that driver.
46 - *
47 - * @dev should be allocated using drm_kunit_helper_alloc_device().
48 - *
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
52 - * resource.
53 - *
54 - * Returns:
55 - * A pointer to the new drm_device, or an ERR_PTR() otherwise.
56 - */
57 struct drm_device *
58 -drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
59 - u32 features)
60 +__drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
61 + size_t size, size_t offset,
62 + u32 features)
63 {
64 - struct kunit_dev *kdev;
65 struct drm_device *drm;
66 struct drm_driver *driver;
67 + void *container;
68 int ret;
69
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);
73
74 driver->driver_features = features;
75 - kdev = devm_drm_dev_alloc(dev, driver, struct kunit_dev, base);
76 - if (IS_ERR(kdev))
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);
81
82 - drm = &kdev->base;
83 + drm = container + offset;
84 drm->mode_config.funcs = &drm_mode_config_funcs;
85
86 ret = drmm_mode_config_init(drm);
87 @@ -131,7 +109,7 @@ drm_kunit_helper_alloc_drm_device(struct
88
89 return drm;
90 }
91 -EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_drm_device);
92 +EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device);
93
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);
100
101 struct drm_device *
102 -drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
103 - u32 features);
104 +__drm_kunit_helper_alloc_drm_device(struct kunit *test, struct device *dev,
105 + size_t size, size_t offset,
106 + u32 features);
107 +
108 +/**
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
115 + *
116 + * This function creates a struct &drm_driver and will create a struct
117 + * &drm_device from @_dev and that driver.
118 + *
119 + * @_dev should be allocated using drm_kunit_helper_alloc_device().
120 + *
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
124 + * resource.
125 + *
126 + * Returns:
127 + * A pointer to the new drm_device, or an ERR_PTR() otherwise.
128 + */
129 +#define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat) \
130 + ((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev, \
131 + sizeof(_type), \
132 + offsetof(_type, _member), \
133 + _feat))
134
135 #endif // DRM_KUNIT_HELPERS_H_