1 /*
2  * Copyright © 2014 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 
33 IGT_TEST_DESCRIPTION(
34    "Use the display CRC support to validate pwrite to an already uncached future scanout buffer.");
35 
36 typedef struct {
37 	int drm_fd;
38 	igt_display_t display;
39 	struct igt_fb fb[2];
40 	igt_output_t *output;
41 	igt_plane_t *primary;
42 	enum pipe pipe;
43 	igt_crc_t ref_crc;
44 	igt_pipe_crc_t *pipe_crc;
45 	uint32_t devid;
46 } data_t;
47 
test(data_t * data)48 static void test(data_t *data)
49 {
50 	igt_output_t *output = data->output;
51 	struct igt_fb *fb = &data->fb[1];
52 	drmModeModeInfo *mode;
53 	uint32_t caching;
54 	void *buf;
55 	igt_crc_t crc;
56 
57 	mode = igt_output_get_mode(output);
58 
59 	/* create a non-white fb where we can pwrite later */
60 	igt_create_pattern_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
61 			      DRM_FORMAT_XRGB8888, LOCAL_DRM_FORMAT_MOD_NONE, fb);
62 
63 	/* flip to it to make it UC/WC and fully flushed */
64 	drmModeSetPlane(data->drm_fd,
65 			data->primary->drm_plane->plane_id,
66 			output->config.crtc->crtc_id,
67 			fb->fb_id, 0,
68 			0, 0, fb->width, fb->height,
69 			0, 0, fb->width << 16, fb->height << 16);
70 
71 	/* flip back the original white buffer */
72 	drmModeSetPlane(data->drm_fd,
73 			data->primary->drm_plane->plane_id,
74 			output->config.crtc->crtc_id,
75 			data->fb[0].fb_id, 0,
76 			0, 0, fb->width, fb->height,
77 			0, 0, fb->width << 16, fb->height << 16);
78 
79 	/* make sure caching mode has become UC/WT */
80 	caching = gem_get_caching(data->drm_fd, fb->gem_handle);
81 	igt_assert(caching == I915_CACHING_NONE || caching == I915_CACHING_DISPLAY);
82 
83 	/* use pwrite to make the other fb all white too */
84 	buf = malloc(fb->size);
85 	igt_assert(buf != NULL);
86 	memset(buf, 0xff, fb->size);
87 	gem_write(data->drm_fd, fb->gem_handle, 0, buf, fb->size);
88 	free(buf);
89 
90 	/* and flip to it */
91 	drmModeSetPlane(data->drm_fd,
92 			data->primary->drm_plane->plane_id,
93 			output->config.crtc->crtc_id,
94 			fb->fb_id, 0,
95 			0, 0, fb->width, fb->height,
96 			0, 0, fb->width << 16, fb->height << 16);
97 
98 	/* check that the crc is as expected, which requires that caches got flushed */
99 	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
100 	igt_assert_crc_equal(&crc, &data->ref_crc);
101 }
102 
prepare_crtc(data_t * data)103 static void prepare_crtc(data_t *data)
104 {
105 	igt_display_t *display = &data->display;
106 	igt_output_t *output = data->output;
107 	drmModeModeInfo *mode;
108 
109 	/* select the pipe we want to use */
110 	igt_output_set_pipe(output, data->pipe);
111 
112 	mode = igt_output_get_mode(output);
113 
114 	/* create a white reference fb and flip to it */
115 	igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
116 			    DRM_FORMAT_XRGB8888, LOCAL_DRM_FORMAT_MOD_NONE,
117 			    1.0, 1.0, 1.0, &data->fb[0]);
118 
119 	data->primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
120 
121 	igt_plane_set_fb(data->primary, &data->fb[0]);
122 	igt_display_commit(display);
123 
124 	if (data->pipe_crc)
125 		igt_pipe_crc_free(data->pipe_crc);
126 
127 	data->pipe_crc = igt_pipe_crc_new(data->drm_fd, data->pipe,
128 					  INTEL_PIPE_CRC_SOURCE_AUTO);
129 
130 	/* get reference crc for the white fb */
131 	igt_pipe_crc_collect_crc(data->pipe_crc, &data->ref_crc);
132 }
133 
cleanup_crtc(data_t * data)134 static void cleanup_crtc(data_t *data)
135 {
136 	igt_display_t *display = &data->display;
137 	igt_output_t *output = data->output;
138 
139 	igt_pipe_crc_free(data->pipe_crc);
140 	data->pipe_crc = NULL;
141 
142 	igt_plane_set_fb(data->primary, NULL);
143 
144 	igt_output_set_pipe(output, PIPE_ANY);
145 	igt_display_commit(display);
146 
147 	igt_remove_fb(data->drm_fd, &data->fb[0]);
148 	igt_remove_fb(data->drm_fd, &data->fb[1]);
149 
150 }
151 
run_test(data_t * data)152 static void run_test(data_t *data)
153 {
154 	igt_display_t *display = &data->display;
155 	igt_output_t *output;
156 	enum pipe pipe;
157 
158 	for_each_pipe_with_valid_output(display, pipe, output) {
159 		data->output = output;
160 		data->pipe = pipe;
161 
162 		prepare_crtc(data);
163 		test(data);
164 		cleanup_crtc(data);
165 
166 		/* once is enough */
167 		return;
168 	}
169 
170 	igt_skip("no valid crtc/connector combinations found\n");
171 }
172 
173 static data_t data;
174 
175 igt_simple_main
176 {
177 	igt_skip_on_simulation();
178 
179 	data.drm_fd = drm_open_driver_master(DRIVER_INTEL);
180 
181 	data.devid = intel_get_drm_devid(data.drm_fd);
182 
183 	kmstest_set_vt_graphics_mode();
184 
185 	igt_require_pipe_crc(data.drm_fd);
186 
187 	igt_display_require(&data.display, data.drm_fd);
188 
189 	run_test(&data);
190 
191 	igt_display_fini(&data.display);
192 }
193