1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include "igt.h"
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <string.h>
31 
32 IGT_TEST_DESCRIPTION("Exercise CHV pipe C cursor fail");
33 
34 #ifndef DRM_CAP_CURSOR_WIDTH
35 #define DRM_CAP_CURSOR_WIDTH 0x8
36 #endif
37 #ifndef DRM_CAP_CURSOR_HEIGHT
38 #define DRM_CAP_CURSOR_HEIGHT 0x9
39 #endif
40 
41 typedef struct {
42 	int drm_fd;
43 	igt_display_t display;
44 	struct igt_fb primary_fb;
45 	struct igt_fb fb;
46 	igt_output_t *output;
47 	enum pipe pipe;
48 	igt_crc_t ref_crc;
49 	int curw, curh; /* cursor size */
50 	igt_pipe_crc_t *pipe_crc;
51 	uint32_t devid;
52 	bool colored, jump, disable;
53 	int jump_x, jump_y;
54 } data_t;
55 
56 enum {
57 	EDGE_LEFT = 0x1,
58 	EDGE_RIGHT = 0x2,
59 	EDGE_TOP = 0x4,
60 	EDGE_BOTTOM = 0x8,
61 };
62 
create_cursor_fb(data_t * data,int cur_w,int cur_h)63 static void create_cursor_fb(data_t *data, int cur_w, int cur_h)
64 {
65 	cairo_t *cr;
66 	uint32_t fb_id;
67 
68 	fb_id = igt_create_fb(data->drm_fd, cur_w, cur_h,
69 			      DRM_FORMAT_ARGB8888,
70 			      LOCAL_DRM_FORMAT_MOD_NONE,
71 			      &data->fb);
72 	igt_assert(fb_id);
73 
74 	cr = igt_get_cairo_ctx(data->drm_fd, &data->fb);
75 	if (data->colored)
76 		igt_paint_color_alpha(cr, 0, 0, data->fb.width, data->fb.height,
77 				      1.0, 0.0, 0.0, 1.0);
78 	else
79 		igt_paint_color_alpha(cr, 0, 0, data->fb.width, data->fb.height,
80 				      0.0, 0.0, 0.0, 0.0);
81 	igt_put_cairo_ctx(data->drm_fd, &data->fb, cr);
82 }
83 
cursor_move(data_t * data,int x,int y,int i)84 static void cursor_move(data_t *data, int x, int y, int i)
85 {
86 	int crtc_id = data->output->config.crtc->crtc_id;
87 
88 	igt_debug("[%d] x=%d, y=%d\n", i, x, y);
89 
90 	/*
91 	 * The "fixed" kernel will refuse the ioctl when pipe C cursor
92 	 * would straddle the left screen edge (which is when the hw
93 	 * fails). So let's accept a failure from the ioctl in that case.
94 	 */
95 	igt_assert(drmModeMoveCursor(data->drm_fd, crtc_id, x, y) == 0 ||
96 		   (IS_CHERRYVIEW(data->devid) && data->pipe == PIPE_C &&
97 		    x < 0 && x > -data->curw));
98 	igt_wait_for_vblank(data->drm_fd, data->pipe);
99 }
100 
101 #define XSTEP 8
102 #define YSTEP 8
103 #define NCRC 128
104 
test_edge_pos(data_t * data,int sx,int ex,int y,bool swap_axis)105 static void test_edge_pos(data_t *data, int sx, int ex, int y, bool swap_axis)
106 {
107 	igt_crc_t *crc = NULL;
108 	int i, n, x, xdir, dx;
109 
110 	if (sx > ex)
111 		xdir = -1;
112 	else
113 		xdir = 1;
114 
115 	dx = (ex - sx)/XSTEP;
116 
117 	i = 0;
118 
119 	for (x = sx; xdir * (x - ex) <= 0; x += dx) {
120 		int xx, yy;
121 
122 		if (swap_axis) {
123 			xx = y;
124 			yy = x;
125 		} else {
126 			xx = x;
127 			yy = y;
128 		}
129 
130 		if (data->jump) {
131 			cursor_move(data, data->jump_x, data->jump_y, i++);
132 		}
133 		if (data->disable) {
134 			cursor_move(data, -data->curw, -data->curh, i++);
135 		}
136 		cursor_move(data, xx, yy, i++);
137 		if (data->jump) {
138 			cursor_move(data, data->jump_x, data->jump_y, i++);
139 		}
140 		if (data->disable) {
141 			cursor_move(data, -data->curw, -data->curh, i++);
142 		}
143 	}
144 
145 	n = igt_pipe_crc_get_crcs(data->pipe_crc, NCRC, &crc);
146 
147 	if (!data->colored) {
148 		igt_debug("Checking CRCs: ");
149 		for (i = 0; i < n; i++) {
150 			igt_debug("[%d] ", i);
151 			igt_assert_crc_equal(&data->ref_crc, &crc[i]);
152 		}
153 		igt_debug("\n");
154 	}
155 }
156 
test_edge(data_t * data,int sy,int ey,int sx,int ex,bool swap_axis)157 static void test_edge(data_t *data, int sy, int ey, int sx, int ex, bool swap_axis)
158 {
159 	int crtc_id = data->output->config.crtc->crtc_id;
160 	int y, ydir, dy;
161 
162 	if (sy > ey)
163 		ydir = -1;
164 	else
165 		ydir = 1;
166 
167 	dy = (ey - sy) / YSTEP;
168 
169 	igt_assert_eq(drmModeMoveCursor(data->drm_fd, crtc_id, -data->curw, -data->curh), 0);
170 	igt_assert_eq(drmModeSetCursor(data->drm_fd, crtc_id, data->fb.gem_handle, data->curw, data->curh), 0);
171 
172 	for (y = sy; ydir * (y - ey) <= 0; ) {
173 		test_edge_pos(data, sx, ex, y, swap_axis);
174 		y += dy;
175 		test_edge_pos(data, ex, sx, y, swap_axis);
176 		y += dy;
177 	}
178 
179 	igt_assert_eq(drmModeMoveCursor(data->drm_fd, crtc_id, -data->curw, -data->curh), 0);
180 	igt_assert_eq(drmModeSetCursor(data->drm_fd, crtc_id, 0, data->curw, data->curh), 0);
181 }
182 
test_edges(data_t * data,unsigned int edges)183 static void test_edges(data_t *data, unsigned int edges)
184 {
185 	drmModeModeInfo *mode = igt_output_get_mode(data->output);
186 
187 	if (edges & EDGE_LEFT) {
188 		test_edge(data, mode->vdisplay, -data->curh,
189 			  -data->curw, 0, false);
190 		test_edge(data, -data->curh, mode->vdisplay,
191 			  -data->curw, 0, false);
192 	}
193 
194 	if (edges & EDGE_RIGHT) {
195 		test_edge(data, mode->vdisplay, -data->curh,
196 			  mode->hdisplay - data->curw, mode->hdisplay, false);
197 		test_edge(data, -data->curh, mode->vdisplay,
198 			  mode->hdisplay - data->curw, mode->hdisplay, false);
199 	}
200 
201 	if (edges & EDGE_TOP) {
202 		test_edge(data, mode->hdisplay, -data->curw,
203 			  -data->curh, 0, true);
204 		test_edge(data, -data->curw, mode->hdisplay,
205 			  -data->curh, 0, true);
206 	}
207 
208 	if (edges & EDGE_BOTTOM) {
209 		test_edge(data, mode->hdisplay, -data->curw,
210 			  mode->vdisplay - data->curh, mode->vdisplay, true);
211 		test_edge(data, -data->curw, mode->hdisplay,
212 			  mode->vdisplay - data->curh, mode->vdisplay, true);
213 	}
214 }
215 
cleanup_crtc(data_t * data)216 static void cleanup_crtc(data_t *data)
217 {
218 	igt_display_t *display = &data->display;
219 
220 	igt_display_reset(display);
221 	igt_pipe_crc_free(data->pipe_crc);
222 	data->pipe_crc = NULL;
223 
224 	igt_remove_fb(data->drm_fd, &data->primary_fb);
225 	igt_remove_fb(data->drm_fd, &data->fb);
226 }
227 
prepare_crtc(data_t * data)228 static void prepare_crtc(data_t *data)
229 {
230 	drmModeModeInfo *mode;
231 	igt_display_t *display = &data->display;
232 	igt_plane_t *primary;
233 
234 	cleanup_crtc(data);
235 
236 	/* select the pipe we want to use */
237 	igt_output_set_pipe(data->output, data->pipe);
238 
239 	mode = igt_output_get_mode(data->output);
240 	igt_create_pattern_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
241 			      DRM_FORMAT_XRGB8888,
242 			      LOCAL_DRM_FORMAT_MOD_NONE,
243 			      &data->primary_fb);
244 
245 	primary = igt_output_get_plane_type(data->output, DRM_PLANE_TYPE_PRIMARY);
246 	igt_plane_set_fb(primary, &data->primary_fb);
247 
248 	igt_display_commit2(display, display->is_atomic ? COMMIT_ATOMIC : COMMIT_LEGACY);
249 
250 	data->jump_x = (mode->hdisplay - data->curw) / 2;
251 	data->jump_y = (mode->vdisplay - data->curh) / 2;
252 
253 	/* create the pipe_crc object for this pipe */
254 	data->pipe_crc = igt_pipe_crc_new_nonblock(data->drm_fd, data->pipe,
255 						   INTEL_PIPE_CRC_SOURCE_AUTO);
256 
257 	/* get reference crc w/o cursor */
258 	igt_pipe_crc_start(data->pipe_crc);
259 	igt_pipe_crc_get_single(data->pipe_crc, &data->ref_crc);
260 }
261 
test_crtc(data_t * data,unsigned int edges)262 static void test_crtc(data_t *data, unsigned int edges)
263 {
264 	prepare_crtc(data);
265 
266 	create_cursor_fb(data, data->curw, data->curh);
267 
268 	test_edges(data, edges);
269 }
270 
opt_handler(int opt,int opt_index,void * _data)271 static int opt_handler(int opt, int opt_index, void *_data)
272 {
273 	data_t *data = _data;
274 
275 	switch (opt) {
276 	case 'c':
277 		data->colored = true;
278 		break;
279 	case 'd':
280 		data->disable = true;
281 		break;
282 	case 'j':
283 		data->jump = true;
284 		break;
285 	default:
286 		return IGT_OPT_HANDLER_ERROR;
287 	}
288 
289 	return IGT_OPT_HANDLER_SUCCESS;
290 }
291 
292 static data_t data;
293 static uint64_t max_curw = 64, max_curh = 64;
294 static const struct option long_opts[] = {
295 	{ .name = "colored", .val = 'c' },
296 	{ .name = "disable", .val = 'd'},
297 	{ .name = "jump", .val = 'j' },
298 	{}
299 };
300 static const char *help_str =
301 	"  --colored\t\tUse a colored cursor (disables CRC checks)\n"
302 	"  --disable\t\tDisable the cursor between each step\n"
303 	"  --jump\t\tJump the cursor to middle of the screen between each step)\n";
304 
305 igt_main_args("", long_opts, help_str, opt_handler, &data)
306 {
307 	igt_skip_on_simulation();
308 
309 	igt_fixture {
310 		int ret;
311 
312 		data.drm_fd = drm_open_driver_master(DRIVER_INTEL);
313 
314 		data.devid = intel_get_drm_devid(data.drm_fd);
315 
316 		ret = drmGetCap(data.drm_fd, DRM_CAP_CURSOR_WIDTH, &max_curw);
317 		igt_assert(ret == 0 || errno == EINVAL);
318 		/* Not making use of cursor_height since it is same as width, still reading */
319 		ret = drmGetCap(data.drm_fd, DRM_CAP_CURSOR_HEIGHT, &max_curh);
320 		igt_assert(ret == 0 || errno == EINVAL);
321 
322 		kmstest_set_vt_graphics_mode();
323 
324 		igt_require_pipe_crc(data.drm_fd);
325 
326 		igt_display_require(&data.display, data.drm_fd);
327 	}
328 
329 	for_each_pipe_static(data.pipe) {
330 		igt_subtest_group {
331 			igt_fixture {
332 				igt_display_require_output_on_pipe(&data.display, data.pipe);
333 				data.output = igt_get_single_output_for_pipe(&data.display, data.pipe);
334 			}
335 
336 			for (data.curw = 64; data.curw <= 256; data.curw *= 2) {
337 				data.curh = data.curw;
338 
339 				igt_fixture
340 					igt_require(data.curw <= max_curw && data.curh <= max_curh);
341 
342 				igt_subtest_f("pipe-%s-%dx%d-left-edge",
343 					kmstest_pipe_name(data.pipe),
344 					data.curw, data.curh)
345 					test_crtc(&data, EDGE_LEFT);
346 
347 				igt_subtest_f("pipe-%s-%dx%d-right-edge",
348 					kmstest_pipe_name(data.pipe),
349 					data.curw, data.curh)
350 					test_crtc(&data, EDGE_RIGHT);
351 
352 				igt_subtest_f("pipe-%s-%dx%d-top-edge",
353 					kmstest_pipe_name(data.pipe),
354 					data.curw, data.curh)
355 					test_crtc(&data, EDGE_TOP);
356 
357 				igt_subtest_f("pipe-%s-%dx%d-bottom-edge",
358 					kmstest_pipe_name(data.pipe),
359 					data.curw, data.curh)
360 					test_crtc(&data, EDGE_BOTTOM);
361 			}
362 		}
363 	}
364 
365 	igt_fixture
366 		igt_display_fini(&data.display);
367 }
368