summaryrefslogtreecommitdiff
path: root/tests/fbdev.c
blob: 794882a6a6f6f87c56d1ff3838dc32db6422d5d7 (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * Copyright © 2019 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 "config.h"

#include "igt.h"

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <linux/fb.h>

#include "igt.h"

#define PANSTEP(panstep_) \
	((panstep_) ? (panstep_) : 1)

static unsigned int __panoffset(unsigned int offset, unsigned int panstep)
{
	return offset - (offset % PANSTEP(panstep));
}

#define XOFFSET(offset_) \
	__panoffset(offset_, fix_info.xpanstep)

#define YOFFSET(offset_) \
	__panoffset(offset_, fix_info.ypanstep)

static void pan_test(int fd, const struct fb_var_screeninfo *var, int expected_ret)
{
	struct fb_var_screeninfo pan_var, new_var;
	int ret;

	memcpy(&pan_var, var, sizeof(pan_var));

	ret = ioctl(fd, FBIOPAN_DISPLAY, &pan_var);
	igt_assert_f(ret == expected_ret,
		     "ioctl(FBIOPAN_DISPLAY) returned ret=%d, expected %d\n", ret, expected_ret);

	if (ret)
		return; /* panning failed; skip additional tests */

	ret = ioctl(fd, FBIOGET_VSCREENINFO, &new_var);
	igt_assert_f(ret == 0, "ioctl(FBIOGET_VSCREENINFO) failed, ret=%d\n", ret);
	igt_assert_f(pan_var.xoffset == new_var.xoffset && pan_var.yoffset == new_var.yoffset,
		     "panning to (%u, %u) moved to (%u, %u)\n",
		     pan_var.xoffset, pan_var.yoffset, new_var.xoffset, new_var.yoffset);
}

static void mode_tests(int fd)
{
	struct fb_var_screeninfo var_info;
	struct fb_fix_screeninfo fix_info;

	igt_fixture {
		igt_require(ioctl(fd, FBIOGET_VSCREENINFO, &var_info) == 0);
		igt_require(ioctl(fd, FBIOGET_FSCREENINFO, &fix_info) == 0);
	}

	igt_describe("Check if screeninfo is valid");
	igt_subtest("info") {
		unsigned long nbits, nlines;

		/* video memory configuration */
		igt_assert_f(fix_info.line_length, "line length not set\n");
		igt_assert_f(fix_info.smem_len, "size of video memory not set\n");
		igt_assert_f(fix_info.line_length <= fix_info.smem_len,
			     "line length (%u) exceeds available video memory (%u)\n",
			     fix_info.line_length, fix_info.smem_len);

		/* color format */
		igt_assert_f(var_info.bits_per_pixel, "bits-per-pixel not set\n");

		/* horizontal resolution */
		igt_assert_f(var_info.xres, "horizontal resolution not set\n");
		igt_assert_f(var_info.xres_virtual, "horizontal virtual resolution not set\n");
		igt_assert_f(var_info.xres <= var_info.xres_virtual,
			     "horizontal virtual resolution (%u) less than horizontal resolution (%u)\n",
			     var_info.xres_virtual, var_info.xres);
		igt_assert_f(var_info.xoffset <= (var_info.xres_virtual - var_info.xres),
			     "screen horizontal offset (%u) overflow\n",
			     var_info.xoffset);
		nbits = fix_info.line_length * CHAR_BIT;
		igt_assert_f((var_info.xres_virtual * var_info.bits_per_pixel) <= nbits,
			     "vertical virtual resolution (%u) with bpp %u exceeds line length %u\n",
			     var_info.yres_virtual, var_info.bits_per_pixel, fix_info.line_length);

		/* vertical resolution */
		igt_assert_f(var_info.yres, "vertical resolution not set\n");
		igt_assert_f(var_info.yres_virtual, "vertical virtual resolution not set\n");
		igt_assert_f(var_info.yres <= var_info.yres_virtual,
			     "vertical virtual resolution (%u) less than vertical resolution (%u)\n",
			     var_info.yres_virtual, var_info.yres);
		igt_assert_f((var_info.vmode & FB_VMODE_YWRAP) ||
			     (var_info.yoffset <= (var_info.yres_virtual - var_info.yres)),
			     "screen vertical offset (%u) overflow\n",
			     var_info.yoffset);
		nlines = fix_info.smem_len / fix_info.line_length;
		igt_assert_f(var_info.yres_virtual <= nlines,
			     "vertical virtual resolution (%u) with line length %u exceeds available video memory\n",
			     var_info.yres_virtual, fix_info.line_length);
	}

	igt_describe("Check panning / page flipping");
	igt_subtest("pan") {
		struct fb_var_screeninfo pan_var;
		int expected_ret;

		memset(&pan_var, 0, sizeof(pan_var));

		/*
		 * Tests that are expected to succeed.
		 */

		igt_debug("Jump to opposite end of virtual screen\n");
		pan_var.xoffset = XOFFSET(var_info.xres_virtual - var_info.xres - var_info.xoffset);
		pan_var.yoffset = YOFFSET(var_info.yres_virtual - var_info.yres - var_info.yoffset);
		pan_test(fd, &pan_var, 0);
		igt_debug("Jump to (0, 0)\n");
		pan_var.xoffset = XOFFSET(0);
		pan_var.yoffset = YOFFSET(0);
		pan_test(fd, &pan_var, 0);
		igt_debug("Jump to maximum extend\n");
		pan_var.xoffset = XOFFSET(var_info.xres_virtual - var_info.xres);
		pan_var.yoffset = YOFFSET(var_info.yres_virtual - var_info.yres);
		pan_test(fd, &pan_var, 0);

		/*
		 * Tests that are expected to fail.
		 */

		igt_debug("Jump beyond maximum horizontal extend\n");
		pan_var.xoffset = XOFFSET(var_info.xres_virtual - var_info.xres + PANSTEP(fix_info.xpanstep));
		pan_var.yoffset = YOFFSET(0);
		pan_test(fd, &pan_var, -1);
		igt_debug("Jump beyond horizontal virtual resolution\n");
		pan_var.xoffset = XOFFSET(var_info.xres_virtual);
		pan_var.yoffset = YOFFSET(0);
		pan_test(fd, &pan_var, -1);

		/*
		 * The FB_VMODE_YWRAP flag is configurable as part of ioctl(FBIOPAN_DISPLAY),
		 * but it's hard to know which drivers support it and which don't. Testing for
		 * FBINFO_HWACCEL_YWRAP does not produce meaningful results. So we got with the
		 * device's current setting.
		 *
		 * With FB_VMODE_YWRAP set, the display is expected to wrap around when
		 * reaching the limits of the vertical resolution. Otherwise, this should
		 * fail.
		 *
		 */

		if (var_info.vmode & FB_VMODE_YWRAP) {
			pan_var.vmode |= FB_VMODE_YWRAP;
			expected_ret = 0;
		} else {
			expected_ret = -1;
		}

		igt_debug("Jump beyond maximum vertical extend\n");
		pan_var.xoffset = XOFFSET(0);
		pan_var.yoffset = YOFFSET(var_info.yres_virtual - var_info.yres + PANSTEP(fix_info.ypanstep));
		pan_test(fd, &pan_var, expected_ret);
		igt_debug("Jump beyond vertical virtual resolution\n");
		pan_var.xoffset = XOFFSET(0);
		pan_var.yoffset = YOFFSET(var_info.yres_virtual);
		pan_test(fd, &pan_var, expected_ret);

		pan_var.vmode &= ~FB_VMODE_YWRAP;
	}

	igt_fixture {
		/* restore original panning offsets */
		ioctl(fd, FBIOPAN_DISPLAY, &var_info);
	}
}

static void framebuffer_tests(int fd)
{
	const int values[] = { 0, 0x55, 0xaa, 0xff };
	struct fb_fix_screeninfo fix_info;
	unsigned char * volatile map;
	unsigned char * volatile buf;
	volatile size_t pagesize;

	igt_fixture {
		long ret;

		igt_require(ioctl(fd, FBIOGET_FSCREENINFO, &fix_info) == 0);
		igt_assert(fix_info.smem_len);

		map = mmap(NULL, fix_info.smem_len,
			   PROT_WRITE, MAP_SHARED, fd, 0);
		igt_assert(map != MAP_FAILED);

		/* allocate two additional bytes for eof test */
		buf = malloc(fix_info.smem_len + 2);
		igt_require(buf);

		ret = sysconf(_SC_PAGESIZE);
		igt_require(ret != -1);
		pagesize = ret;
	}

	igt_describe("Check read operations on framebuffer memory");
	igt_subtest("read") {
		ssize_t ret;

		/* fill framebuffer and compare */
		for (int i = 0; i < ARRAY_SIZE(values); i++) {
			memset(map, values[i], fix_info.smem_len);
			ret = pread(fd, buf, fix_info.smem_len, 0);
			igt_assert_f(ret == (ssize_t)fix_info.smem_len,
				     "pread failed, ret=%zd\n", ret);
			igt_assert_f(!memcmp(map, buf, fix_info.smem_len),
				     "read differs from mapped framebuffer for %x\n",
				     values[i]);
		}
	}

	igt_describe("Check read operations on unaligned locations in framebuffer memory");
	igt_subtest("unaligned-read") {
		const unsigned char *pos;
		ssize_t ret;
		size_t len;
		off_t off;

		off = pagesize + (pagesize >> 2); /* 1.25 * pagesize */
		len = (pagesize << 2) + (pagesize >> 1); /* 4.5 * pagesize */
		igt_require_f(off + len < fix_info.smem_len,
			      "framebuffer too small to test\n");

		/* read at unaligned location and compare */
		memset(map, 0, fix_info.smem_len);
		memset(&map[off], 0x55, len);
		memset(buf, 0xff, fix_info.smem_len);

		ret = pread(fd, &buf[off], len, off);
		igt_assert_f(ret == (ssize_t)len,
			     "pread failed, ret=%zd\n", ret);

		pos = memchr(buf, 0x55, fix_info.smem_len);
		igt_assert_f(pos, "0x55 not found within read buffer\n");
		igt_assert_f(pos == &buf[off],
			     "0x55 found at pos %zu, expected %lld\n",
			     pos - buf, (long long)off);

		pos = memchr(&buf[off], 0xff, fix_info.smem_len - off);
		igt_assert_f(pos, "0xff not found within read buffer\n");
		igt_assert_f(pos == &buf[off + len],
			     "0xff found at pos %zu, expected %lld\n",
			     pos - buf, (long long)(off + len));

		pos = memchr(&buf[off + len],
			     0x55,
			     fix_info.smem_len - (off + len));
		igt_assert_f(!pos,
			     "found 0x55 at pos %zu, none expected\n",
			     pos - buf);
	}

	igt_describe("Check write operations on framebuffer memory");
	igt_subtest("write") {
		ssize_t ret;

		/* write to framebuffer and compare */
		for (int i = 0; i < ARRAY_SIZE(values); i++) {
			memset(buf, values[i], fix_info.smem_len);
			ret = pwrite(fd, buf, fix_info.smem_len, 0);
			igt_assert_f(ret == (ssize_t)fix_info.smem_len,
				     "pwrite failed, ret=%zd\n", ret);
			igt_assert_f(!memcmp(map, buf, fix_info.smem_len),
				     "write differs from mapped framebuffer for %x\n",
				     values[i]);
		}
	}

	igt_describe("Check write operations on unaligned locations in framebuffer memory");
	igt_subtest("unaligned-write") {
		const unsigned char *pos;
		ssize_t ret;
		size_t len;
		off_t off;

		off = pagesize + (pagesize >> 2); /* 1.25 * pagesize */
		len = (pagesize << 2) + (pagesize >> 1); /* 4.5 * pagesize */
		igt_require_f(off + len < fix_info.smem_len,
			      "framebuffer too small to test\n");

		/* read at unaligned location and compare */
		memset(map, 0xff, fix_info.smem_len);
		memset(buf, 0, fix_info.smem_len);
		memset(&buf[off], 0x55, len);

		ret = pwrite(fd, &buf[off], len, off);
		igt_assert_f(ret == (ssize_t)len,
			     "pwrite failed, ret=%zd\n", ret);

		pos = memchr(map, 0x55, fix_info.smem_len);
		igt_assert_f(pos, "0x55 not found within framebuffer\n");
		igt_assert_f(pos == &map[off],
			     "0x55 found at pos %zu, expected %lld\n",
			     pos - map, (long long)off);

		pos = memchr(&map[off], 0xff, fix_info.smem_len - off);
		igt_assert_f(pos, "0xff not found within framebuffer\n");
		igt_assert_f(pos == &map[off + len],
			     "0xff found at pos %zu, expected %lld\n",
			     pos - map, (long long)(off + len));

		pos = memchr(&map[off + len],
			     0x55,
			     fix_info.smem_len - (off + len));
		igt_assert_f(!pos,
			     "found 0x55 at pos %zu, none expected\n",
			     pos - map);
	}

	igt_describe("Check framebuffer access near EOF");
	igt_subtest("eof") {
		unsigned long lastindex = fix_info.smem_len - 1;
		unsigned char * const maplast = map + lastindex;
		unsigned char * const buflast = buf + lastindex;
		ssize_t ret;

		*buflast = 0x55;

		/* write across EOF; set remaining bytes */
		ret = pwrite(fd, buflast, 2, lastindex);
		igt_assert_f(ret == 1, "write crossed EOF, ret=%zd\n", ret);
		igt_assert_f(*maplast == *buflast,
			     "write buffer differs from mapped framebuffer at final byte, "
			     "maplast=%u buflast=%u\n", *maplast, *buflast);

		/* write at EOF; get ENOSPC */
		ret = pwrite(fd, &buflast[1], 1, lastindex + 1);
		igt_assert_f(ret == -1 && errno == ENOSPC,
			     "write at EOF, ret=%zd\n", ret);

		*maplast = 0;

		/* write final byte */
		ret = pwrite(fd, buflast, 1, lastindex);
		igt_assert_f(ret == 1, "write before EOF, ret=%zd\n", ret);
		igt_assert_f(*maplast == *buflast,
			     "write buffer differs from mapped framebuffer at final byte, "
			     "maplast=%u buflast=%u\n", *maplast, *buflast);

		/* write after EOF; get EFBIG */
		ret = pwrite(fd, &buflast[2], 1, lastindex + 2);
		igt_assert_f(ret == -1 && errno == EFBIG,
			     "write after EOF, ret=%zd\n", ret);

		*maplast = 0;

		/* read across the EOF; get remaining bytes */
		ret = pread(fd, buflast, 2, lastindex);
		igt_assert_f(ret == 1, "read before EOF, ret=%zd\n", ret);
		igt_assert_f(*maplast == *buflast,
			     "read buffer differs from mapped framebuffer at final byte, "
			     "maplast=%u buflast=%u\n", *maplast, *buflast);

		/* read after EOF; get 0 */
		ret = pread(fd, &buflast[1], 1, lastindex + 1);
		igt_assert_f(ret == 0, "read at EOF, ret=%zd\n", ret);
	}

	igt_describe("Check framebuffer access with NULL");
	igt_subtest("nullptr") {
		ssize_t ret;

		ret = pread(fd, NULL, fix_info.smem_len, 0);
		igt_assert_f(ret == -1 && errno == EFAULT,
			     "reading into NULL did not return EFAULT, ret=%zd\n",
			     ret);

		ret = pwrite(fd, NULL, fix_info.smem_len, 0);
		igt_assert_f(ret == -1 && errno == EFAULT,
			     "writing from NULL did not return EFAULT, ret=%zd\n",
			     ret);
	}

	igt_fixture {
		free(buf);
		/* don't leave garbage on the screen */
		memset(map, 0, fix_info.smem_len);
		munmap(map, fix_info.smem_len);
	}
}

igt_main
{
	volatile int fd = -1;

	/*
	 * Should this test focus on the fbdev independent of any drm driver,
	 * or should it look for fbdev of a particular device?
	 */
	igt_fixture {
		fd = open("/dev/fb0", O_RDWR);
		if (fd < 0) {
			drm_load_module(DRIVER_ANY);
			fd = open("/dev/fb0", O_RDWR);
		}
		igt_require_f(fd != -1, "/dev/fb0\n");
	}

	igt_describe("Check modesetting");
	igt_subtest_group {
		mode_tests(fd);
	}

	igt_describe("Check framebuffer access");
	igt_subtest_group {
		framebuffer_tests(fd);
	}

	igt_fixture {
		close(fd);
	}
}