summaryrefslogtreecommitdiff
path: root/tests/kms_force_connector_basic.c
blob: e3b9034db6afcae86d79c4989a3200a7aeb94209 (plain)
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
/*
 * Copyright © 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

/**
 * TEST: kms force connector basic
 * Category: Display
 * Description: Check the debugfs force connector/edid features work correctly.
 */

#include "igt.h"

IGT_TEST_DESCRIPTION("Check the debugfs force connector/edid features work"
		     " correctly.");

#define CHECK_MODE(m, h, w, r) \
	igt_assert_eq(m.hdisplay, h); igt_assert_eq(m.vdisplay, w); \
	igt_assert_eq(m.vrefresh, r);

static void reset_connectors(void)
{
	drmModeRes *res;
	drmModeConnector *connector = NULL;
	int drm_fd;
	char *param;

	drm_fd = drm_open_driver_master(DRIVER_ANY);
	res = drmModeGetResources(drm_fd);

	for (int i = 0; i < res->count_connectors; i++) {

		connector = drmModeGetConnectorCurrent(drm_fd,
						       res->connectors[i]);

		kmstest_force_connector(drm_fd, connector,
					FORCE_CONNECTOR_UNSPECIFIED);

		kmstest_force_edid(drm_fd, connector, NULL);

		drmModeFreeConnector(connector);
	}

	param = __igt_params_get(drm_fd, "load_detect_test");
	if (param) {
		igt_set_module_param_int(drm_fd, "load_detect_test", 0);
		free(param);
	}

	drm_close_driver(drm_fd);
}

/**
 * SUBTEST: force-load-detect
 * Description: Test to detect forced load.
 * Driver requirement: i915
 * Functionality: force_connector, vga
 * Mega feature: General Display Features
 * Test category: functionality test
 */
static void force_load_detect(int drm_fd, drmModeConnectorPtr connector, drmModeRes *res)
{
	int i, j, w = 64, h = 64;
	drmModePlaneRes *plane_resources;
	drmModeConnector *temp;
	struct igt_fb xrgb_fb, argb_fb;

	/* No load detect on HDMI. */
	igt_require(connector->connector_type == DRM_MODE_CONNECTOR_VGA);

	igt_create_fb(drm_fd, w, h, DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR, &xrgb_fb);
	igt_create_fb(drm_fd, w, h, DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_LINEAR, &argb_fb);
	igt_assert(drmSetClientCap(drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) == 0);

	/*
	 * Disable all outputs to make sure we have a
	 * free crtc available for load detect.
	 */
	kmstest_unset_all_crtcs(drm_fd, res);

	igt_set_module_param_int(drm_fd, "load_detect_test", 1);

	plane_resources = drmModeGetPlaneResources(drm_fd);
	igt_assert(plane_resources);

	for (i = 0; i < plane_resources->count_planes; i++) {
		drmModePlane *drm_plane;
		bool found = false;
		uint32_t plane_id = plane_resources->planes[i];

		drm_plane = drmModeGetPlane(drm_fd, plane_id);
		igt_assert(drm_plane);

		for (j = 0; j < drm_plane->count_formats; j++) {
			uint32_t format = drm_plane->formats[j];
			uint32_t crtc = ffs(drm_plane->possible_crtcs) - 1;
			uint32_t crtc_id = res->crtcs[crtc];

			if (format == DRM_FORMAT_XRGB8888)
				do_or_die(drmModeSetPlane(drm_fd, plane_id, crtc_id,
						xrgb_fb.fb_id,
						0, 0, 0, w, h,
						0, 0, IGT_FIXED(w, 0), IGT_FIXED(h, 0)));
			else if (format == DRM_FORMAT_ARGB8888)
				do_or_die(drmModeSetPlane(drm_fd, plane_id, crtc_id,
						argb_fb.fb_id,
						0, 0, 0, w, h,
						0, 0, IGT_FIXED(w, 0), IGT_FIXED(h, 0)));
			else
				continue;

			found = true;
			break;
		}
		drmModeFreePlane(drm_plane);
		igt_assert(found);
	}

	/*
	 * This can't use drmModeGetConnectorCurrent
	 * because connector probing is the point of this test.
	 */
	temp = drmModeGetConnector(drm_fd, connector->connector_id);

	igt_set_module_param_int(drm_fd, "load_detect_test", 0);

	igt_assert(temp->connection != DRM_MODE_UNKNOWNCONNECTION);

	drmModeFreeConnector(temp);

	/* Look if planes are unmodified. */
	for (i = 0; i < plane_resources->count_planes; i++) {
		drmModePlane *drm_plane;

		drm_plane = drmModeGetPlane(drm_fd,
					    plane_resources->planes[i]);
		igt_assert(drm_plane);

		igt_assert(drm_plane->crtc_id);
		igt_assert(drm_plane->fb_id);

		if (drm_plane->fb_id != xrgb_fb.fb_id)
			igt_assert_eq(drm_plane->fb_id, argb_fb.fb_id);

		drmModeFreePlane(drm_plane);
	}
}

/**
 * SUBTEST: force-connector-state
 * Description: Test to check the forced connector state
 * Driver requirement: i915, xe
 * Test category: functionality test
 * Functionality: force_connector
 * Mega feature: General Display Features
 */
static void force_connector_state(int drm_fd, drmModeConnectorPtr connector)
{
	igt_display_t display;
	drmModeConnector *temp;

	igt_display_require(&display, drm_fd);

	/* Reset display before attempt to use it. */
	igt_display_reset(&display);
	igt_display_commit(&display);

	/* Force the connector on and check the reported values. */
	kmstest_force_connector(drm_fd, connector, FORCE_CONNECTOR_ON);
	temp = drmModeGetConnectorCurrent(drm_fd,
					  connector->connector_id);
	igt_assert_eq(temp->connection, DRM_MODE_CONNECTED);
	igt_assert_lt(0, temp->count_modes);
	drmModeFreeConnector(temp);

	/* Force the connector off. */
	kmstest_force_connector(drm_fd, connector,
				FORCE_CONNECTOR_OFF);
	temp = drmModeGetConnectorCurrent(drm_fd,
					  connector->connector_id);
	igt_assert_eq(temp->connection, DRM_MODE_DISCONNECTED);
	igt_assert_eq(0, temp->count_modes);
	drmModeFreeConnector(temp);

	/* Check that the previous state is restored. */
	kmstest_force_connector(drm_fd, connector,
				FORCE_CONNECTOR_UNSPECIFIED);
	temp = drmModeGetConnectorCurrent(drm_fd,
					  connector->connector_id);
	igt_assert_eq(temp->connection, connector->connection);
	drmModeFreeConnector(temp);

	igt_display_fini(&display);
}

/**
 * SUBTEST: force-edid
 * Description: Test to check the values after forcing edid
 * Driver requirement: i915, xe
 * Test category: functionality test
 * Functionality: force_connector
 * Mega feature: General Display Features
 */
static void force_edid(int drm_fd, drmModeConnectorPtr connector)
{
	drmModeConnector *temp;

	kmstest_force_connector(drm_fd, connector,
				FORCE_CONNECTOR_ON);
	temp = drmModeGetConnectorCurrent(drm_fd,
					  connector->connector_id);
	drmModeFreeConnector(temp);

	/* Test edid forcing. */
	kmstest_force_edid(drm_fd, connector,
			   igt_kms_get_base_edid());
	temp = drmModeGetConnectorCurrent(drm_fd,
					  connector->connector_id);

	igt_debug("num_conn %i\n", temp->count_modes);

	CHECK_MODE(temp->modes[0], 1920, 1080, 60);
	/*
	 * Don't check non-preferred modes to avoid to tight coupling
	 * with the in-kernel EDID parser.
	 */

	drmModeFreeConnector(temp);

	/* Remove edid. */
	kmstest_force_edid(drm_fd, connector, NULL);
	kmstest_force_connector(drm_fd, connector,
				FORCE_CONNECTOR_UNSPECIFIED);
	temp = drmModeGetConnectorCurrent(drm_fd,
					  connector->connector_id);
	/*
	 * The connector should now have the same number of modes that
	 * it started with.
	 */
	igt_assert_eq(temp->count_modes, connector->count_modes);
	drmModeFreeConnector(temp);

}

/**
 * SUBTEST: prune-stale-modes
 * Description: Tests pruning of stale modes
 * Driver requirement: i915, xe
 * Test category: functionality test
 * Functionality: force_connector
 * Mega feature: General Display Features
 */
static void prune_stale_modes(int drm_fd, drmModeConnectorPtr connector)
{
	int i;
	drmModeConnector *temp;

	kmstest_force_connector(drm_fd, connector,
				FORCE_CONNECTOR_ON);

	/* Test pruning of stale modes. */
	kmstest_force_edid(drm_fd, connector,
			   igt_kms_get_alt_edid());
	temp = drmModeGetConnectorCurrent(drm_fd,
					  connector->connector_id);

	for (i = 0; i < temp->count_modes; i++) {
		if (temp->modes[i].hdisplay == 1400 &&
		    temp->modes[i].vdisplay == 1050)
			break;
	}
	igt_require_f(i != temp->count_modes, "1400x1050 not on mode list\n");
	drmModeFreeConnector(temp);

	kmstest_force_edid(drm_fd, connector,
			   igt_kms_get_base_edid());
	temp = drmModeGetConnectorCurrent(drm_fd,
					  connector->connector_id);

	for (i = 0; i < temp->count_modes; i++) {
		if (temp->modes[i].hdisplay == 1400 &&
		    temp->modes[i].vdisplay == 1050)
			break;
	}
	igt_assert_f(i == temp->count_modes, "1400x1050 not pruned from mode list\n");

	drmModeFreeConnector(temp);

	kmstest_force_edid(drm_fd, connector, NULL);
	kmstest_force_connector(drm_fd, connector,
				FORCE_CONNECTOR_UNSPECIFIED);
}

static int opt_handler(int opt, int opt_index, void *data)
{
	switch (opt) {
	case 'r':
		reset_connectors();
		exit(0);
		break;
	}

	return IGT_OPT_HANDLER_SUCCESS;
}

struct option long_opts[] = {
	{"reset", 0, 0, 'r'},
	{0, 0, 0, 0}
};
const char *help_str =
	"  --reset\t\tReset all connector force states and edid.\n";

igt_main_args("", long_opts, help_str, opt_handler, NULL)
{
	/* Force the VGA output and test that it worked. */
	int i, drm_fd = 0;
	drmModeRes *res;
	drmModeConnector *connector = NULL;
	struct {
		const char *name;
		const char *desc;
		void (*func)(int, drmModeConnector *);
	} tests[] = {
		{ "force-connector-state",
		  "Test to check the forced connector state.",
		  force_connector_state },
		{ "force-edid",
		  "Test to check the values after forcing edid.",
		  force_edid },
		{ "prune-stale-modes",
		  "Tests pruning of stale modes.",
		  prune_stale_modes }
	};

	igt_fixture {
		unsigned connector_id = 0;

		drm_fd = drm_open_driver_master(DRIVER_ANY);
		kmstest_set_vt_graphics_mode();

		res = drmModeGetResources(drm_fd);
		igt_require(res);

		/* Find a vga connector. */
		for (i = 0; i < res->count_connectors; i++) {
			connector = drmModeGetConnectorCurrent(drm_fd,
							       res->connectors[i]);

			if (connector->connector_type == DRM_MODE_CONNECTOR_VGA) {
				/* Ensure that no override was left in place. */
				kmstest_force_connector(drm_fd,
							connector,
							FORCE_CONNECTOR_UNSPECIFIED);

				/* Only use the first VGA connector. */
				if (!connector_id)
					connector_id = res->connectors[i];
			}

			drmModeFreeConnector(connector);
		}

		if (connector_id)
			goto end;

		/* Find a hdmi connector if we didn't find vga. */
		for (i = 0; i < res->count_connectors; i++) {
			connector = drmModeGetConnectorCurrent(drm_fd,
							       res->connectors[i]);

			if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
			    connector->connector_type == DRM_MODE_CONNECTOR_HDMIB) {
				/* Ensure that no override was left in place. */
				kmstest_force_connector(drm_fd,
							connector,
							FORCE_CONNECTOR_UNSPECIFIED);

				/* Use the the first HDMI connector. */
				if (!connector_id)
					connector_id = res->connectors[i];
			}

			drmModeFreeConnector(connector);
		}

		igt_require(connector_id);

end:
		/* Reacquire status after clearing any previous overrides. */
		connector = drmModeGetConnector(drm_fd, connector_id);
	}

	igt_describe("Test to detect forced load.");
	igt_subtest("force-load-detect") {
		igt_require_i915(drm_fd);

		force_load_detect(drm_fd, connector, res);
	}

	for (i = 0; i < ARRAY_SIZE(tests); i++) {
		igt_describe_f("%s", tests[i].desc);
		igt_subtest_f("%s", tests[i].name)
			tests[i].func(drm_fd, connector);
	}

	igt_fixture {
		drmModeFreeConnector(connector);
		drm_close_driver(drm_fd);

		reset_connectors();
	}
}