aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/vkms/tests/vkms_config_test.c
blob: 104120c91c39e7d9a293f67c06a0a896a3ac5a3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// SPDX-License-Identifier: GPL-2.0+

#include <kunit/test.h>

#include "../vkms_config.h"

MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");

static size_t vkms_config_get_num_planes(struct vkms_config *config)
{
	struct vkms_config_plane *plane_cfg;
	size_t count = 0;

	vkms_config_for_each_plane(config, plane_cfg)
		count++;

	return count;
}

static struct vkms_config_plane *get_first_plane(struct vkms_config *config)
{
	struct vkms_config_plane *plane_cfg;

	vkms_config_for_each_plane(config, plane_cfg)
		return plane_cfg;

	return NULL;
}

static struct vkms_config_crtc *get_first_crtc(struct vkms_config *config)
{
	struct vkms_config_crtc *crtc_cfg;

	vkms_config_for_each_crtc(config, crtc_cfg)
		return crtc_cfg;

	return NULL;
}

struct default_config_case {
	bool enable_cursor;
	bool enable_writeback;
	bool enable_overlay;
};

static void vkms_config_test_empty_config(struct kunit *test)
{
	struct vkms_config *config;
	const char *dev_name = "test";

	config = vkms_config_create(dev_name);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);

	/* The dev_name string and the config have different lifetimes */
	dev_name = NULL;
	KUNIT_EXPECT_STREQ(test, vkms_config_get_device_name(config), "test");

	KUNIT_EXPECT_EQ(test, vkms_config_get_num_planes(config), 0);
	KUNIT_EXPECT_EQ(test, vkms_config_get_num_crtcs(config), 0);

	KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));

	vkms_config_destroy(config);
}

static struct default_config_case default_config_cases[] = {
	{ false, false, false },
	{ true, false, false },
	{ true, true, false },
	{ true, false, true },
	{ false, true, false },
	{ false, true, true },
	{ false, false, true },
	{ true, true, true },
};

KUNIT_ARRAY_PARAM(default_config, default_config_cases, NULL);

static void vkms_config_test_default_config(struct kunit *test)
{
	const struct default_config_case *params = test->param_value;
	struct vkms_config *config;
	struct vkms_config_plane *plane_cfg;
	struct vkms_config_crtc *crtc_cfg;
	int n_primaries = 0;
	int n_cursors = 0;
	int n_overlays = 0;

	config = vkms_config_default_create(params->enable_cursor,
					    params->enable_writeback,
					    params->enable_overlay);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);

	/* Planes */
	vkms_config_for_each_plane(config, plane_cfg) {
		switch (vkms_config_plane_get_type(plane_cfg)) {
		case DRM_PLANE_TYPE_PRIMARY:
			n_primaries++;
			break;
		case DRM_PLANE_TYPE_CURSOR:
			n_cursors++;
			break;
		case DRM_PLANE_TYPE_OVERLAY:
			n_overlays++;
			break;
		default:
			KUNIT_FAIL_AND_ABORT(test, "Unknown plane type");
		}
	}
	KUNIT_EXPECT_EQ(test, n_primaries, 1);
	KUNIT_EXPECT_EQ(test, n_cursors, params->enable_cursor ? 1 : 0);
	KUNIT_EXPECT_EQ(test, n_overlays, params->enable_overlay ? 8 : 0);

	/* CRTCs */
	KUNIT_EXPECT_EQ(test, vkms_config_get_num_crtcs(config), 1);

	crtc_cfg = get_first_crtc(config);
	KUNIT_EXPECT_EQ(test, vkms_config_crtc_get_writeback(crtc_cfg),
			params->enable_writeback);

	KUNIT_EXPECT_TRUE(test, vkms_config_is_valid(config));

	vkms_config_destroy(config);
}

static void vkms_config_test_get_planes(struct kunit *test)
{
	struct vkms_config *config;
	struct vkms_config_plane *plane_cfg;
	struct vkms_config_plane *plane_cfg1, *plane_cfg2;
	int n_planes = 0;

	config = vkms_config_create("test");
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);

	vkms_config_for_each_plane(config, plane_cfg)
		n_planes++;
	KUNIT_ASSERT_EQ(test, n_planes, 0);

	plane_cfg1 = vkms_config_create_plane(config);
	vkms_config_for_each_plane(config, plane_cfg) {
		n_planes++;
		if (plane_cfg != plane_cfg1)
			KUNIT_FAIL(test, "Unexpected plane");
	}
	KUNIT_ASSERT_EQ(test, n_planes, 1);
	n_planes = 0;

	plane_cfg2 = vkms_config_create_plane(config);
	vkms_config_for_each_plane(config, plane_cfg) {
		n_planes++;
		if (plane_cfg != plane_cfg1 && plane_cfg != plane_cfg2)
			KUNIT_FAIL(test, "Unexpected plane");
	}
	KUNIT_ASSERT_EQ(test, n_planes, 2);
	n_planes = 0;

	vkms_config_destroy_plane(plane_cfg1);
	vkms_config_for_each_plane(config, plane_cfg) {
		n_planes++;
		if (plane_cfg != plane_cfg2)
			KUNIT_FAIL(test, "Unexpected plane");
	}
	KUNIT_ASSERT_EQ(test, n_planes, 1);

	vkms_config_destroy(config);
}

static void vkms_config_test_get_crtcs(struct kunit *test)
{
	struct vkms_config *config;
	struct vkms_config_crtc *crtc_cfg;
	struct vkms_config_crtc *crtc_cfg1, *crtc_cfg2;

	config = vkms_config_create("test");
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);

	KUNIT_ASSERT_EQ(test, vkms_config_get_num_crtcs(config), 0);
	vkms_config_for_each_crtc(config, crtc_cfg)
		KUNIT_FAIL(test, "Unexpected CRTC");

	crtc_cfg1 = vkms_config_create_crtc(config);
	KUNIT_ASSERT_EQ(test, vkms_config_get_num_crtcs(config), 1);
	vkms_config_for_each_crtc(config, crtc_cfg) {
		if (crtc_cfg != crtc_cfg1)
			KUNIT_FAIL(test, "Unexpected CRTC");
	}

	crtc_cfg2 = vkms_config_create_crtc(config);
	KUNIT_ASSERT_EQ(test, vkms_config_get_num_crtcs(config), 2);
	vkms_config_for_each_crtc(config, crtc_cfg) {
		if (crtc_cfg != crtc_cfg1 && crtc_cfg != crtc_cfg2)
			KUNIT_FAIL(test, "Unexpected CRTC");
	}

	vkms_config_destroy_crtc(config, crtc_cfg2);
	KUNIT_ASSERT_EQ(test, vkms_config_get_num_crtcs(config), 1);
	vkms_config_for_each_crtc(config, crtc_cfg) {
		if (crtc_cfg != crtc_cfg1)
			KUNIT_FAIL(test, "Unexpected CRTC");
	}

	vkms_config_destroy(config);
}

static void vkms_config_test_invalid_plane_number(struct kunit *test)
{
	struct vkms_config *config;
	struct vkms_config_plane *plane_cfg;
	int n;

	config = vkms_config_default_create(false, false, false);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);

	/* Invalid: No planes */
	plane_cfg = get_first_plane(config);
	vkms_config_destroy_plane(plane_cfg);
	KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));

	/* Invalid: Too many planes */
	for (n = 0; n <= 32; n++)
		vkms_config_create_plane(config);

	KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));

	vkms_config_destroy(config);
}

static void vkms_config_test_valid_plane_type(struct kunit *test)
{
	struct vkms_config *config;
	struct vkms_config_plane *plane_cfg;

	config = vkms_config_default_create(false, false, false);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);

	plane_cfg = get_first_plane(config);
	vkms_config_destroy_plane(plane_cfg);

	/* Invalid: No primary plane */
	plane_cfg = vkms_config_create_plane(config);
	vkms_config_plane_set_type(plane_cfg, DRM_PLANE_TYPE_OVERLAY);
	KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));

	/* Invalid: Multiple primary planes */
	plane_cfg = vkms_config_create_plane(config);
	vkms_config_plane_set_type(plane_cfg, DRM_PLANE_TYPE_PRIMARY);
	plane_cfg = vkms_config_create_plane(config);
	vkms_config_plane_set_type(plane_cfg, DRM_PLANE_TYPE_PRIMARY);
	KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));

	/* Valid: One primary plane */
	vkms_config_destroy_plane(plane_cfg);
	KUNIT_EXPECT_TRUE(test, vkms_config_is_valid(config));

	/* Invalid: Multiple cursor planes */
	plane_cfg = vkms_config_create_plane(config);
	vkms_config_plane_set_type(plane_cfg, DRM_PLANE_TYPE_CURSOR);
	plane_cfg = vkms_config_create_plane(config);
	vkms_config_plane_set_type(plane_cfg, DRM_PLANE_TYPE_CURSOR);
	KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));

	/* Valid: One primary and one cursor plane */
	vkms_config_destroy_plane(plane_cfg);
	KUNIT_EXPECT_TRUE(test, vkms_config_is_valid(config));

	vkms_config_destroy(config);
}

static void vkms_config_test_invalid_crtc_number(struct kunit *test)
{
	struct vkms_config *config;
	struct vkms_config_crtc *crtc_cfg;
	int n;

	config = vkms_config_default_create(false, false, false);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);

	/* Invalid: No CRTCs */
	crtc_cfg = get_first_crtc(config);
	vkms_config_destroy_crtc(config, crtc_cfg);
	KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));

	/* Invalid: Too many CRTCs */
	for (n = 0; n <= 32; n++)
		vkms_config_create_crtc(config);

	KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));

	vkms_config_destroy(config);
}

static struct kunit_case vkms_config_test_cases[] = {
	KUNIT_CASE(vkms_config_test_empty_config),
	KUNIT_CASE_PARAM(vkms_config_test_default_config,
			 default_config_gen_params),
	KUNIT_CASE(vkms_config_test_get_planes),
	KUNIT_CASE(vkms_config_test_get_crtcs),
	KUNIT_CASE(vkms_config_test_invalid_plane_number),
	KUNIT_CASE(vkms_config_test_valid_plane_type),
	KUNIT_CASE(vkms_config_test_invalid_crtc_number),
	{}
};

static struct kunit_suite vkms_config_test_suite = {
	.name = "vkms-config",
	.test_cases = vkms_config_test_cases,
};

kunit_test_suite(vkms_config_test_suite);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Kunit test for vkms config utility");