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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
// SPDX-License-Identifier: GPL-2.0
/*
* Kunit test for drm_hdmi_state_helper functions
*/
#include <drm/drm_atomic.h>
#include <drm/drm_atomic_state_helper.h>
#include <drm/drm_atomic_uapi.h>
#include <drm/drm_drv.h>
#include <drm/drm_edid.h>
#include <drm/drm_connector.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_kunit_helpers.h>
#include <drm/drm_managed.h>
#include <drm/drm_modeset_helper_vtables.h>
#include <drm/drm_print.h>
#include <drm/drm_probe_helper.h>
#include <drm/display/drm_hdmi_state_helper.h>
#include "../drm_crtc_internal.h"
#include <kunit/test.h>
#include "drm_kunit_edid.h"
struct drm_atomic_helper_connector_hdmi_priv {
struct drm_device drm;
struct drm_plane *plane;
struct drm_crtc *crtc;
struct drm_encoder encoder;
struct drm_connector connector;
const char *current_edid;
size_t current_edid_len;
};
#define connector_to_priv(c) \
container_of_const(c, struct drm_atomic_helper_connector_hdmi_priv, connector)
static struct drm_display_mode *find_preferred_mode(struct drm_connector *connector)
{
struct drm_device *drm = connector->dev;
struct drm_display_mode *mode, *preferred;
mutex_lock(&drm->mode_config.mutex);
preferred = list_first_entry(&connector->modes, struct drm_display_mode, head);
list_for_each_entry(mode, &connector->modes, head)
if (mode->type & DRM_MODE_TYPE_PREFERRED)
preferred = mode;
mutex_unlock(&drm->mode_config.mutex);
return preferred;
}
static int light_up_connector(struct kunit *test,
struct drm_device *drm,
struct drm_crtc *crtc,
struct drm_connector *connector,
struct drm_display_mode *mode,
struct drm_modeset_acquire_ctx *ctx)
{
struct drm_atomic_state *state;
struct drm_connector_state *conn_state;
struct drm_crtc_state *crtc_state;
int ret;
state = drm_kunit_helper_atomic_state_alloc(test, drm, ctx);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, state);
conn_state = drm_atomic_get_connector_state(state, connector);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, conn_state);
conn_state->hdmi.output_bpc = connector->max_bpc;
conn_state->hdmi.output_format = HDMI_COLORSPACE_RGB;
ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
KUNIT_EXPECT_EQ(test, ret, 0);
crtc_state = drm_atomic_get_crtc_state(state, crtc);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc_state);
ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
KUNIT_EXPECT_EQ(test, ret, 0);
crtc_state->enable = true;
crtc_state->active = true;
ret = drm_atomic_commit(state);
KUNIT_ASSERT_EQ(test, ret, 0);
return 0;
}
static int set_connector_edid(struct kunit *test, struct drm_connector *connector,
const char *edid, size_t edid_len)
{
struct drm_atomic_helper_connector_hdmi_priv *priv =
connector_to_priv(connector);
struct drm_device *drm = connector->dev;
int ret;
priv->current_edid = edid;
priv->current_edid_len = edid_len;
mutex_lock(&drm->mode_config.mutex);
ret = connector->funcs->fill_modes(connector, 4096, 4096);
mutex_unlock(&drm->mode_config.mutex);
KUNIT_ASSERT_GT(test, ret, 0);
return 0;
}
static int dummy_connector_get_modes(struct drm_connector *connector)
{
struct drm_atomic_helper_connector_hdmi_priv *priv =
connector_to_priv(connector);
const struct drm_edid *edid;
unsigned int num_modes;
edid = drm_edid_alloc(priv->current_edid, priv->current_edid_len);
if (!edid)
return -EINVAL;
drm_edid_connector_update(connector, edid);
num_modes = drm_edid_connector_add_modes(connector);
drm_edid_free(edid);
return num_modes;
}
static const struct drm_connector_helper_funcs dummy_connector_helper_funcs = {
.atomic_check = drm_atomic_helper_connector_hdmi_check,
.get_modes = dummy_connector_get_modes,
};
static void dummy_hdmi_connector_reset(struct drm_connector *connector)
{
drm_atomic_helper_connector_reset(connector);
__drm_atomic_helper_connector_hdmi_reset(connector, connector->state);
}
static const struct drm_connector_funcs dummy_connector_funcs = {
.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
.fill_modes = drm_helper_probe_single_connector_modes,
.reset = dummy_hdmi_connector_reset,
};
static
struct drm_atomic_helper_connector_hdmi_priv *
drm_atomic_helper_connector_hdmi_init(struct kunit *test,
unsigned int formats,
unsigned int max_bpc)
{
struct drm_atomic_helper_connector_hdmi_priv *priv;
struct drm_connector *conn;
struct drm_encoder *enc;
struct drm_device *drm;
struct device *dev;
int ret;
dev = drm_kunit_helper_alloc_device(test);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
priv = drm_kunit_helper_alloc_drm_device(test, dev,
struct drm_atomic_helper_connector_hdmi_priv, drm,
DRIVER_MODESET | DRIVER_ATOMIC);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
test->priv = priv;
drm = &priv->drm;
priv->plane = drm_kunit_helper_create_primary_plane(test, drm,
NULL,
NULL,
NULL, 0,
NULL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->plane);
priv->crtc = drm_kunit_helper_create_crtc(test, drm,
priv->plane, NULL,
NULL,
NULL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->crtc);
enc = &priv->encoder;
ret = drmm_encoder_init(drm, enc, NULL, DRM_MODE_ENCODER_TMDS, NULL);
KUNIT_ASSERT_EQ(test, ret, 0);
enc->possible_crtcs = drm_crtc_mask(priv->crtc);
conn = &priv->connector;
ret = drmm_connector_hdmi_init(drm, conn,
&dummy_connector_funcs,
DRM_MODE_CONNECTOR_HDMIA,
NULL,
formats,
max_bpc);
KUNIT_ASSERT_EQ(test, ret, 0);
drm_connector_helper_add(conn, &dummy_connector_helper_funcs);
drm_connector_attach_encoder(conn, enc);
drm_mode_config_reset(drm);
ret = set_connector_edid(test, conn,
test_edid_hdmi_1080p_rgb_max_200mhz,
ARRAY_SIZE(test_edid_hdmi_1080p_rgb_max_200mhz));
KUNIT_ASSERT_EQ(test, ret, 0);
return priv;
}
/*
* Test that if we change the maximum bpc property to a different value,
* we trigger a mode change on the connector's CRTC, which will in turn
* disable/enable the connector.
*/
static void drm_test_check_output_bpc_crtc_mode_changed(struct kunit *test)
{
struct drm_atomic_helper_connector_hdmi_priv *priv;
struct drm_modeset_acquire_ctx *ctx;
struct drm_connector_state *old_conn_state;
struct drm_connector_state *new_conn_state;
struct drm_crtc_state *crtc_state;
struct drm_atomic_state *state;
struct drm_display_mode *preferred;
struct drm_connector *conn;
struct drm_device *drm;
struct drm_crtc *crtc;
int ret;
priv = drm_atomic_helper_connector_hdmi_init(test,
BIT(HDMI_COLORSPACE_RGB),
10);
KUNIT_ASSERT_NOT_NULL(test, priv);
ctx = drm_kunit_helper_acquire_ctx_alloc(test);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
conn = &priv->connector;
preferred = find_preferred_mode(conn);
KUNIT_ASSERT_NOT_NULL(test, preferred);
drm = &priv->drm;
crtc = priv->crtc;
ret = light_up_connector(test, drm, crtc, conn, preferred, ctx);
KUNIT_ASSERT_EQ(test, ret, 0);
state = drm_kunit_helper_atomic_state_alloc(test, drm, ctx);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, state);
new_conn_state = drm_atomic_get_connector_state(state, conn);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, new_conn_state);
old_conn_state = drm_atomic_get_old_connector_state(state, conn);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, old_conn_state);
new_conn_state->hdmi.output_bpc = 8;
KUNIT_ASSERT_NE(test,
old_conn_state->hdmi.output_bpc,
new_conn_state->hdmi.output_bpc);
ret = drm_atomic_check_only(state);
KUNIT_ASSERT_EQ(test, ret, 0);
old_conn_state = drm_atomic_get_old_connector_state(state, conn);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, old_conn_state);
new_conn_state = drm_atomic_get_new_connector_state(state, conn);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, new_conn_state);
KUNIT_ASSERT_NE(test,
old_conn_state->hdmi.output_bpc,
new_conn_state->hdmi.output_bpc);
crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc_state);
KUNIT_EXPECT_TRUE(test, crtc_state->mode_changed);
}
/*
* Test that if we set the output bpc property to the same value, we
* don't trigger a mode change on the connector's CRTC and leave the
* connector unaffected.
*/
static void drm_test_check_output_bpc_crtc_mode_not_changed(struct kunit *test)
{
struct drm_atomic_helper_connector_hdmi_priv *priv;
struct drm_modeset_acquire_ctx *ctx;
struct drm_connector_state *old_conn_state;
struct drm_connector_state *new_conn_state;
struct drm_crtc_state *crtc_state;
struct drm_atomic_state *state;
struct drm_display_mode *preferred;
struct drm_connector *conn;
struct drm_device *drm;
struct drm_crtc *crtc;
int ret;
priv = drm_atomic_helper_connector_hdmi_init(test,
BIT(HDMI_COLORSPACE_RGB),
10);
KUNIT_ASSERT_NOT_NULL(test, priv);
ctx = drm_kunit_helper_acquire_ctx_alloc(test);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
conn = &priv->connector;
preferred = find_preferred_mode(conn);
KUNIT_ASSERT_NOT_NULL(test, preferred);
drm = &priv->drm;
crtc = priv->crtc;
ret = light_up_connector(test, drm, crtc, conn, preferred, ctx);
KUNIT_ASSERT_EQ(test, ret, 0);
state = drm_kunit_helper_atomic_state_alloc(test, drm, ctx);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, state);
new_conn_state = drm_atomic_get_connector_state(state, conn);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, new_conn_state);
old_conn_state = drm_atomic_get_old_connector_state(state, conn);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, old_conn_state);
KUNIT_ASSERT_EQ(test,
new_conn_state->hdmi.output_bpc,
old_conn_state->hdmi.output_bpc);
ret = drm_atomic_check_only(state);
KUNIT_ASSERT_EQ(test, ret, 0);
old_conn_state = drm_atomic_get_old_connector_state(state, conn);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, old_conn_state);
new_conn_state = drm_atomic_get_new_connector_state(state, conn);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, new_conn_state);
KUNIT_EXPECT_EQ(test,
old_conn_state->hdmi.output_bpc,
new_conn_state->hdmi.output_bpc);
crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc_state);
KUNIT_EXPECT_FALSE(test, crtc_state->mode_changed);
}
static struct kunit_case drm_atomic_helper_connector_hdmi_check_tests[] = {
KUNIT_CASE(drm_test_check_output_bpc_crtc_mode_changed),
KUNIT_CASE(drm_test_check_output_bpc_crtc_mode_not_changed),
/*
* TODO: We should have tests to check that a change in the
* format triggers a CRTC mode change just like we do for the
* RGB Quantization and BPC.
*
* However, we don't have any way to control which format gets
* picked up aside from changing the BPC or mode which would
* already trigger a mode change.
*/
{ }
};
static struct kunit_suite drm_atomic_helper_connector_hdmi_check_test_suite = {
.name = "drm_atomic_helper_connector_hdmi_check",
.test_cases = drm_atomic_helper_connector_hdmi_check_tests,
};
/*
* Test that if the connector was initialised with a maximum bpc of 8,
* the value of the max_bpc and max_requested_bpc properties out of
* reset are also set to 8, and output_bpc is set to 0 and will be
* filled at atomic_check time.
*/
static void drm_test_check_bpc_8_value(struct kunit *test)
{
struct drm_atomic_helper_connector_hdmi_priv *priv;
struct drm_connector_state *conn_state;
struct drm_connector *conn;
priv = drm_atomic_helper_connector_hdmi_init(test,
BIT(HDMI_COLORSPACE_RGB),
8);
KUNIT_ASSERT_NOT_NULL(test, priv);
conn = &priv->connector;
conn_state = conn->state;
KUNIT_EXPECT_EQ(test, conn_state->max_bpc, 8);
KUNIT_EXPECT_EQ(test, conn_state->max_requested_bpc, 8);
KUNIT_EXPECT_EQ(test, conn_state->hdmi.output_bpc, 0);
}
/*
* Test that if the connector was initialised with a maximum bpc of 10,
* the value of the max_bpc and max_requested_bpc properties out of
* reset are also set to 10, and output_bpc is set to 0 and will be
* filled at atomic_check time.
*/
static void drm_test_check_bpc_10_value(struct kunit *test)
{
struct drm_atomic_helper_connector_hdmi_priv *priv;
struct drm_connector_state *conn_state;
struct drm_connector *conn;
priv = drm_atomic_helper_connector_hdmi_init(test,
BIT(HDMI_COLORSPACE_RGB),
10);
KUNIT_ASSERT_NOT_NULL(test, priv);
conn = &priv->connector;
conn_state = conn->state;
KUNIT_EXPECT_EQ(test, conn_state->max_bpc, 10);
KUNIT_EXPECT_EQ(test, conn_state->max_requested_bpc, 10);
KUNIT_EXPECT_EQ(test, conn_state->hdmi.output_bpc, 0);
}
/*
* Test that if the connector was initialised with a maximum bpc of 12,
* the value of the max_bpc and max_requested_bpc properties out of
* reset are also set to 12, and output_bpc is set to 0 and will be
* filled at atomic_check time.
*/
static void drm_test_check_bpc_12_value(struct kunit *test)
{
struct drm_atomic_helper_connector_hdmi_priv *priv;
struct drm_connector_state *conn_state;
struct drm_connector *conn;
priv = drm_atomic_helper_connector_hdmi_init(test,
BIT(HDMI_COLORSPACE_RGB),
12);
KUNIT_ASSERT_NOT_NULL(test, priv);
conn = &priv->connector;
conn_state = conn->state;
KUNIT_EXPECT_EQ(test, conn_state->max_bpc, 12);
KUNIT_EXPECT_EQ(test, conn_state->max_requested_bpc, 12);
KUNIT_EXPECT_EQ(test, conn_state->hdmi.output_bpc, 0);
}
/*
* Test that the value of the output format property out of reset is set
* to RGB, even if the driver supports more than that.
*/
static void drm_test_check_format_value(struct kunit *test)
{
struct drm_atomic_helper_connector_hdmi_priv *priv;
struct drm_connector_state *conn_state;
struct drm_connector *conn;
priv = drm_atomic_helper_connector_hdmi_init(test,
BIT(HDMI_COLORSPACE_RGB) |
BIT(HDMI_COLORSPACE_YUV422) |
BIT(HDMI_COLORSPACE_YUV444),
8);
KUNIT_ASSERT_NOT_NULL(test, priv);
conn = &priv->connector;
conn_state = conn->state;
KUNIT_EXPECT_EQ(test, conn_state->hdmi.output_format, HDMI_COLORSPACE_RGB);
}
static struct kunit_case drm_atomic_helper_connector_hdmi_reset_tests[] = {
KUNIT_CASE(drm_test_check_bpc_8_value),
KUNIT_CASE(drm_test_check_bpc_10_value),
KUNIT_CASE(drm_test_check_bpc_12_value),
KUNIT_CASE(drm_test_check_format_value),
{ }
};
static struct kunit_suite drm_atomic_helper_connector_hdmi_reset_test_suite = {
.name = "drm_atomic_helper_connector_hdmi_reset",
.test_cases = drm_atomic_helper_connector_hdmi_reset_tests,
};
kunit_test_suites(
&drm_atomic_helper_connector_hdmi_check_test_suite,
&drm_atomic_helper_connector_hdmi_reset_test_suite,
);
MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");
MODULE_LICENSE("GPL");
|