1 /*
2  * Copyright © 2019 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 "igt_sysfs.h"
27 #include "igt_psr.h"
28 #include <errno.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/timerfd.h>
33 #include "intel_bufmgr.h"
34 
35 IGT_TEST_DESCRIPTION("Test PSR2 selective update");
36 
37 #define SQUARE_SIZE 100
38 /* each selective update block is 4 lines tall */
39 #define EXPECTED_NUM_SU_BLOCKS ((SQUARE_SIZE / 4) + (SQUARE_SIZE % 4 ? 1 : 0))
40 
41 /*
42  * Minimum is 15 as the number of frames to active PSR2 could be configured
43  * to 15 frames plus a few more in case we miss a selective update between
44  * debugfs reads.
45  */
46 #define MAX_SCREEN_CHANGES 20
47 
48 enum operations {
49 	PAGE_FLIP,
50 	FRONTBUFFER,
51 	LAST
52 };
53 
op_str(enum operations op)54 static const char *op_str(enum operations op)
55 {
56 	static const char * const name[] = {
57 		[PAGE_FLIP] = "page_flip",
58 		[FRONTBUFFER] = "frontbuffer"
59 	};
60 
61 	return name[op];
62 }
63 
64 typedef struct {
65 	int drm_fd;
66 	int debugfs_fd;
67 	igt_display_t display;
68 	drm_intel_bufmgr *bufmgr;
69 	drmModeModeInfo *mode;
70 	igt_output_t *output;
71 	struct igt_fb fb[2];
72 	enum operations op;
73 	cairo_t *cr;
74 	int change_screen_timerfd;
75 	uint32_t screen_changes;
76 } data_t;
77 
setup_output(data_t * data)78 static void setup_output(data_t *data)
79 {
80 	igt_display_t *display = &data->display;
81 	igt_output_t *output;
82 	enum pipe pipe;
83 
84 	for_each_pipe_with_valid_output(display, pipe, output) {
85 		drmModeConnectorPtr c = output->config.connector;
86 
87 		if (c->connector_type != DRM_MODE_CONNECTOR_eDP)
88 			continue;
89 
90 		igt_output_set_pipe(output, pipe);
91 		data->output = output;
92 		data->mode = igt_output_get_mode(output);
93 
94 		return;
95 	}
96 }
97 
display_init(data_t * data)98 static void display_init(data_t *data)
99 {
100 	igt_display_require(&data->display, data->drm_fd);
101 	setup_output(data);
102 }
103 
display_fini(data_t * data)104 static void display_fini(data_t *data)
105 {
106 	igt_display_fini(&data->display);
107 }
108 
prepare(data_t * data)109 static void prepare(data_t *data)
110 {
111 	igt_plane_t *primary;
112 
113 	/* all green frame */
114 	igt_create_color_fb(data->drm_fd,
115 			    data->mode->hdisplay, data->mode->vdisplay,
116 			    DRM_FORMAT_XRGB8888,
117 			    LOCAL_DRM_FORMAT_MOD_NONE,
118 			    0.0, 1.0, 0.0,
119 			    &data->fb[0]);
120 
121 	if (data->op == PAGE_FLIP) {
122 		cairo_t *cr;
123 
124 		igt_create_color_fb(data->drm_fd,
125 				    data->mode->hdisplay, data->mode->vdisplay,
126 				    DRM_FORMAT_XRGB8888,
127 				    LOCAL_DRM_FORMAT_MOD_NONE,
128 				    0.0, 1.0, 0.0,
129 				    &data->fb[1]);
130 
131 		cr = igt_get_cairo_ctx(data->drm_fd, &data->fb[1]);
132 		/* paint a white square */
133 		igt_paint_color_alpha(cr, 0, 0, SQUARE_SIZE, SQUARE_SIZE,
134 				      1.0, 1.0, 1.0, 1.0);
135 		igt_put_cairo_ctx(data->drm_fd,  &data->fb[1], cr);
136 	} else if (data->op == FRONTBUFFER) {
137 		data->cr = igt_get_cairo_ctx(data->drm_fd, &data->fb[0]);
138 	}
139 
140 	primary = igt_output_get_plane_type(data->output,
141 					    DRM_PLANE_TYPE_PRIMARY);
142 
143 	igt_plane_set_fb(primary, &data->fb[0]);
144 	igt_display_commit2(&data->display, COMMIT_ATOMIC);
145 }
146 
update_screen_and_test(data_t * data)147 static bool update_screen_and_test(data_t *data)
148 {
149 	uint16_t su_blocks;
150 	bool ret = false;
151 
152 	switch (data->op) {
153 	case PAGE_FLIP: {
154 		igt_plane_t *primary;
155 
156 		primary = igt_output_get_plane_type(data->output,
157 						    DRM_PLANE_TYPE_PRIMARY);
158 
159 		igt_plane_set_fb(primary, &data->fb[data->screen_changes & 1]);
160 		igt_display_commit2(&data->display, COMMIT_ATOMIC);
161 		break;
162 	}
163 	case FRONTBUFFER: {
164 		drmModeClip clip;
165 
166 		clip.x1 = clip.y1 = 0;
167 		clip.x2 = clip.y2 = SQUARE_SIZE;
168 
169 		if (data->screen_changes & 1) {
170 			/* go back to all green frame with a square */
171 			igt_paint_color_alpha(data->cr, 0, 0, SQUARE_SIZE,
172 					      SQUARE_SIZE, 1.0, 1.0, 1.0, 1.0);
173 		} else {
174 			/* go back to all green frame */
175 			igt_paint_color_alpha(data->cr, 0, 0, SQUARE_SIZE,
176 					      SQUARE_SIZE, 0, 1.0, 0, 1.0);
177 		}
178 
179 		drmModeDirtyFB(data->drm_fd, data->fb[0].fb_id, &clip, 1);
180 		break;
181 	}
182 	default:
183 		igt_assert_f(data->op, "Operation not handled\n");
184 	}
185 
186 	if (psr2_wait_su(data->debugfs_fd, &su_blocks))
187 		ret = su_blocks == EXPECTED_NUM_SU_BLOCKS;
188 
189 	return ret;
190 }
191 
run(data_t * data)192 static void run(data_t *data)
193 {
194 	bool result = false;
195 
196 	igt_assert(psr_wait_entry(data->debugfs_fd, PSR_MODE_2));
197 
198 	for (data->screen_changes = 0;
199 	     data->screen_changes < MAX_SCREEN_CHANGES && !result;
200 	     data->screen_changes++) {
201 		uint64_t exp;
202 		int r;
203 
204 		r = read(data->change_screen_timerfd, &exp, sizeof(exp));
205 		if (r == sizeof(uint64_t) && exp)
206 			result = update_screen_and_test(data);
207 	}
208 
209 	igt_debug("Screen changes: %u\n", data->screen_changes);
210 	igt_assert_f(result,
211 		     "No matching selective update blocks read from debugfs\n");
212 }
213 
cleanup(data_t * data)214 static void cleanup(data_t *data)
215 {
216 	igt_plane_t *primary;
217 
218 	primary = igt_output_get_plane_type(data->output,
219 					    DRM_PLANE_TYPE_PRIMARY);
220 	igt_plane_set_fb(primary, NULL);
221 	igt_display_commit2(&data->display, COMMIT_ATOMIC);
222 
223 	if (data->op == PAGE_FLIP)
224 		igt_remove_fb(data->drm_fd, &data->fb[1]);
225 	else if (data->op == FRONTBUFFER)
226 		igt_put_cairo_ctx(data->drm_fd, &data->fb[0], data->cr);
227 
228 	igt_remove_fb(data->drm_fd, &data->fb[0]);
229 }
230 
231 igt_main
232 {
233 	data_t data = {};
234 
235 	igt_skip_on_simulation();
236 
237 	igt_fixture {
238 		struct itimerspec interval;
239 		int r;
240 
241 		data.drm_fd = drm_open_driver_master(DRIVER_INTEL);
242 		data.debugfs_fd = igt_debugfs_dir(data.drm_fd);
243 		kmstest_set_vt_graphics_mode();
244 
245 		igt_require_f(psr_sink_support(data.debugfs_fd, PSR_MODE_2),
246 			      "Sink does not support PSR2\n");
247 
248 		data.bufmgr = drm_intel_bufmgr_gem_init(data.drm_fd, 4096);
249 		igt_assert(data.bufmgr);
250 		drm_intel_bufmgr_gem_enable_reuse(data.bufmgr);
251 
252 		display_init(&data);
253 
254 		/* Test if PSR2 can be enabled */
255 		igt_require_f(psr_enable(data.debugfs_fd, PSR_MODE_2),
256 			      "Error enabling PSR2\n");
257 		data.op = FRONTBUFFER;
258 		prepare(&data);
259 		r = psr_wait_entry(data.debugfs_fd, PSR_MODE_2);
260 		cleanup(&data);
261 		igt_require_f(r, "PSR2 can not be enabled\n");
262 
263 		/* blocking timerfd */
264 		data.change_screen_timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
265 		igt_require(data.change_screen_timerfd != -1);
266 		/* Changing screen at 30hz to support 30hz panels */
267 		interval.it_value.tv_nsec = NSEC_PER_SEC / 30;
268 		interval.it_value.tv_sec = 0;
269 		interval.it_interval.tv_nsec = interval.it_value.tv_nsec;
270 		interval.it_interval.tv_sec = interval.it_value.tv_sec;
271 		r = timerfd_settime(data.change_screen_timerfd, 0, &interval, NULL);
272 		igt_require_f(r != -1, "Error setting timerfd\n");
273 	}
274 
275 	for (data.op = PAGE_FLIP; data.op < LAST; data.op++) {
276 		igt_subtest_f("%s", op_str(data.op)) {
277 			prepare(&data);
278 			run(&data);
279 			cleanup(&data);
280 		}
281 	}
282 
283 	igt_fixture {
284 		close(data.debugfs_fd);
285 		drm_intel_bufmgr_destroy(data.bufmgr);
286 		display_fini(&data);
287 	}
288 }
289