summaryrefslogtreecommitdiff
path: root/tests/kms_invalid_mode.c
blob: 0287ee02c1c4c6b5e9e96e2a649c49e8b983ffa9 (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
/*
 * Copyright © 2016 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.
 *
 */

#include "igt.h"
#include <limits.h>
#include <stdbool.h>

IGT_TEST_DESCRIPTION("Make sure all modesets are rejected when the requested mode is invalid");

typedef struct _data data_t;

struct _data {
	int drm_fd;
	enum pipe pipe;
	igt_display_t display;
	igt_output_t *output;
	drmModeResPtr res;
	int max_dotclock;
	bool (*adjust_mode)(data_t *data, drmModeModeInfoPtr mode);
};

static bool has_scaling_mode_prop(data_t *data)
{
	return kmstest_get_property(data->drm_fd,
				    data->output->id,
				    DRM_MODE_OBJECT_CONNECTOR,
				    "scaling mode",
				    NULL, NULL, NULL);
}
static bool
can_bigjoiner(data_t *data)
{
	uint32_t devid = intel_get_drm_devid(data->drm_fd);

	/*
	 * GEN11 and GEN12 require DSC to support bigjoiner.
	 * XELPD and later GEN support uncompressed bigjoiner.
	 */
	if (intel_display_ver(devid) > 12) {
		igt_debug("Platform supports uncompressed bigjoiner\n");
		return true;
	} else if (intel_display_ver(devid) >= 11) {
		return igt_is_dsc_supported(data->drm_fd, data->output->name);
	}

	return false;
}

static bool
adjust_mode_clock_too_high(data_t *data, drmModeModeInfoPtr mode)
{
	int max_dotclock = data->max_dotclock;

	igt_require(max_dotclock != 0);

	/*
	 * FIXME When we have a fixed mode, the kernel will ignore
	 * the user timings apart from hdisplay/vdisplay. Should
	 * fix the kernel to at least make sure the requested
	 * refresh rate as specified by the user timings will
	 * roughly match the user will get. For now skip the
	 * test on  any connector with a fixed mode.
	 */
	if (has_scaling_mode_prop(data))
		return false;

	/*
	 * Newer platforms can support modes higher than the maximum dot clock
	 * by using pipe joiner, so set the mode clock twice that of maximum
	 * dot clock;
	 */
	if (can_bigjoiner(data)) {
		igt_info("Platform supports bigjoiner with %s\n",
			 data->output->name);
		max_dotclock *= 2;
	}

	mode->clock = max_dotclock + 1;

	return true;
}

static bool
adjust_mode_zero_clock(data_t *data, drmModeModeInfoPtr mode)
{
	mode->clock = 0;
	return true;
}

static bool
adjust_mode_int_max_clock(data_t *data, drmModeModeInfoPtr mode)
{
	mode->clock = INT_MAX;
	return true;
}

static bool
adjust_mode_uint_max_clock(data_t *data, drmModeModeInfoPtr mode)
{
	mode->clock = UINT_MAX;
	return true;
}

static bool
adjust_mode_zero_hdisplay(data_t *data, drmModeModeInfoPtr mode)
{
	mode->hdisplay = 0;
	return true;
}

static bool
adjust_mode_zero_vdisplay(data_t *data, drmModeModeInfoPtr mode)
{
	mode->vdisplay = 0;
	return true;
}

static bool
adjust_mode_bad_hsync_start(data_t *data, drmModeModeInfoPtr mode)
{
	mode->hsync_start = mode->hdisplay - 1;
	return true;
}

static bool
adjust_mode_bad_vsync_start(data_t *data, drmModeModeInfoPtr mode)
{
	mode->vsync_start = mode->vdisplay - 1;
	return true;
}

static bool
adjust_mode_bad_hsync_end(data_t *data, drmModeModeInfoPtr mode)
{
	mode->hsync_end = mode->hsync_start - 1;
	return true;
}

static bool
adjust_mode_bad_vsync_end(data_t *data, drmModeModeInfoPtr mode)
{
	mode->vsync_end = mode->vsync_start - 1;
	return true;
}

static bool
adjust_mode_bad_htotal(data_t *data, drmModeModeInfoPtr mode)
{
	mode->htotal = mode->hsync_end - 1;
	return true;
}

static bool
adjust_mode_bad_vtotal(data_t *data, drmModeModeInfoPtr mode)
{
	mode->vtotal = mode->vsync_end - 1;
	return true;
}

static void
test_output(data_t *data)
{
	igt_output_t *output = data->output;
	drmModeModeInfo mode;
	struct igt_fb fb;
	int ret;
	uint32_t crtc_id;

	/*
	 * FIXME test every mode we have to be more
	 * sure everything is really getting rejected?
	 */
	mode = *igt_output_get_mode(output);
	igt_require(data->adjust_mode(data, &mode));

	igt_create_fb(data->drm_fd,
		      max_t(uint16_t, mode.hdisplay, 64),
		      max_t(uint16_t, mode.vdisplay, 64),
		      DRM_FORMAT_XRGB8888,
		      DRM_FORMAT_MOD_LINEAR,
		      &fb);

	kmstest_unset_all_crtcs(data->drm_fd, data->res);

	crtc_id = data->display.pipes[data->pipe].crtc_id;

	ret = drmModeSetCrtc(data->drm_fd, crtc_id,
			     fb.fb_id, 0, 0,
			     &output->id, 1, &mode);
	igt_assert_lt(ret, 0);

	igt_remove_fb(data->drm_fd, &fb);
}

static int i915_max_dotclock(data_t *data)
{
	char buf[4096];
	char *s;
	int max_dotclock = 0;

	if (!is_i915_device(data->drm_fd))
		return 0;

	igt_debugfs_read(data->drm_fd, "i915_frequency_info", buf);
	s = strstr(buf, "Max pixel clock frequency:");
	igt_assert(s);
	igt_assert_eq(sscanf(s, "Max pixel clock frequency: %d kHz", &max_dotclock), 1);

	/* 100 Mhz to 5 GHz seem like reasonable values to expect */
	igt_assert_lt(max_dotclock, 5000000);
	igt_assert_lt(100000, max_dotclock);

	return max_dotclock;
}

static const struct {
	const char *name;
	bool (*adjust_mode)(data_t *data, drmModeModeInfoPtr mode);
} subtests[] = {
	{ .name = "clock-too-high",
	  .adjust_mode = adjust_mode_clock_too_high,
	},
	{ .name = "zero-clock",
	  .adjust_mode = adjust_mode_zero_clock,
	},
	{ .name = "int-max-clock",
	  .adjust_mode = adjust_mode_int_max_clock,
	},
	{ .name = "uint-max-clock",
	  .adjust_mode = adjust_mode_uint_max_clock,
	},
	{ .name = "zero-hdisplay",
	  .adjust_mode = adjust_mode_zero_hdisplay,
	},
	{ .name = "zero-vdisplay",
	  .adjust_mode = adjust_mode_zero_vdisplay,
	},
	{ .name = "bad-hsync-start",
	  .adjust_mode = adjust_mode_bad_hsync_start,
	},
	{ .name = "bad-vsync-start",
	  .adjust_mode = adjust_mode_bad_vsync_start,
	},
	{ .name = "bad-hsync-end",
	  .adjust_mode = adjust_mode_bad_hsync_end,
	},
	{ .name = "bad-vsync-end",
	  .adjust_mode = adjust_mode_bad_vsync_end,
	},
	{ .name = "bad-htotal",
	  .adjust_mode = adjust_mode_bad_htotal,
	},
	{ .name = "bad-vtotal",
	  .adjust_mode = adjust_mode_bad_vtotal,
	},
};

static data_t data;

igt_main
{

	enum pipe pipe;
	igt_output_t *output;

	igt_fixture {
		data.drm_fd = drm_open_driver_master(DRIVER_ANY);

		kmstest_set_vt_graphics_mode();

		igt_display_require(&data.display, data.drm_fd);
		data.res = drmModeGetResources(data.drm_fd);
		igt_assert(data.res);

		data.max_dotclock = i915_max_dotclock(&data);
		igt_info("Max dotclock: %d kHz\n", data.max_dotclock);
	}

	igt_describe("Make sure all modesets are rejected when the requested mode is invalid");
	for (int i = 0; i < ARRAY_SIZE(subtests); i++) {
		igt_subtest_with_dynamic(subtests[i].name) {
			for_each_pipe_with_valid_output(&data.display, pipe, output) {
				igt_dynamic_f("%s-pipe-%s", igt_output_name(output), kmstest_pipe_name(pipe)) {
					data.output = output;
					data.pipe = pipe;
					data.adjust_mode = subtests[i].adjust_mode;
					test_output(&data);
				}
			}
		}
	}

	igt_fixture {
		igt_display_fini(&data.display);
		igt_reset_connectors();
		drmModeFreeResources(data.res);
	}
}