1 /*
2  * Copyright © 2016 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 #include "igt.h"
25 #include "drmtest.h"
26 #include <errno.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <time.h>
31 
32 #ifndef DRM_CAP_CURSOR_WIDTH
33 #define DRM_CAP_CURSOR_WIDTH 0x8
34 #endif
35 #ifndef DRM_CAP_CURSOR_HEIGHT
36 #define DRM_CAP_CURSOR_HEIGHT 0x9
37 #endif
38 
39 struct rmfb_data {
40 	int drm_fd;
41 	igt_display_t display;
42 };
43 
44 /*
45  * 1. Set primary plane to a known fb.
46  * 2. Make sure getcrtc returns the correct fb id.
47  * 3. Call rmfb on the fb.
48  * 4. Make sure getcrtc returns 0 fb id.
49  *
50  * RMFB is supposed to free the framebuffers from any and all planes,
51  * so test this and make sure it works.
52  */
53 static void
test_rmfb(struct rmfb_data * data,igt_output_t * output,enum pipe pipe,bool reopen)54 test_rmfb(struct rmfb_data *data, igt_output_t *output, enum pipe pipe, bool reopen)
55 {
56 	struct igt_fb fb, argb_fb;
57 	drmModeModeInfo *mode;
58 	igt_plane_t *plane;
59 	drmModeCrtc *crtc;
60 	uint64_t cursor_width, cursor_height;
61 	int num_active_planes = 0;
62 
63 	igt_output_set_pipe(output, pipe);
64 
65 	mode = igt_output_get_mode(output);
66 
67 	igt_create_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
68 		      DRM_FORMAT_XRGB8888, LOCAL_DRM_FORMAT_MOD_NONE, &fb);
69 
70 	do_or_die(drmGetCap(data->drm_fd, DRM_CAP_CURSOR_WIDTH, &cursor_width));
71 	do_or_die(drmGetCap(data->drm_fd, DRM_CAP_CURSOR_HEIGHT, &cursor_height));
72 
73 	igt_create_fb(data->drm_fd, cursor_width, cursor_height,
74 		      DRM_FORMAT_ARGB8888, LOCAL_DRM_FORMAT_MOD_NONE, &argb_fb);
75 
76 	/*
77 	 * Make sure these buffers are suited for display use
78 	 * because most of the modeset operations must be fast
79 	 * later on.
80 	 */
81 	for_each_plane_on_pipe(&data->display, pipe, plane) {
82 		if (plane->type == DRM_PLANE_TYPE_CURSOR) {
83 			igt_plane_set_fb(plane, &argb_fb);
84 			igt_fb_set_size(&argb_fb, plane, cursor_width, cursor_height);
85 			igt_plane_set_size(plane, cursor_width, cursor_height);
86 		} else {
87 			igt_plane_set_fb(plane, &fb);
88 		}
89 
90 		if (igt_display_try_commit2(&data->display, data->display.is_atomic ?
91 					    COMMIT_ATOMIC : COMMIT_LEGACY)) {
92 			/*
93 			 * Disable any plane that fails (presumably
94 			 * due to exceeding some hardware limit).
95 			 */
96 			igt_plane_set_fb(plane, NULL);
97 		} else {
98 			num_active_planes++;
99 		}
100 	}
101 
102 	/*
103 	 * Make sure we were able to enable at least one
104 	 * plane so that we actually test something.
105 	 */
106 	igt_assert_lt(0, num_active_planes);
107 
108 	igt_display_commit2(&data->display, data->display.is_atomic ? COMMIT_ATOMIC : COMMIT_LEGACY);
109 
110 	crtc = drmModeGetCrtc(data->drm_fd, output->config.crtc->crtc_id);
111 
112 	igt_assert_eq(crtc->buffer_id, fb.fb_id);
113 
114 	drmModeFreeCrtc(crtc);
115 
116 	if (reopen) {
117 		close(data->drm_fd);
118 
119 		data->drm_fd = drm_open_driver_master(DRIVER_ANY);
120 		drmSetClientCap(data->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
121 		drmSetClientCap(data->drm_fd, DRM_CLIENT_CAP_ATOMIC, 1);
122 
123 		igt_pipe_refresh(&data->display, pipe, true);
124 	} else {
125 		igt_remove_fb(data->drm_fd, &fb);
126 		igt_remove_fb(data->drm_fd, &argb_fb);
127 	}
128 
129 	crtc = drmModeGetCrtc(data->drm_fd, output->config.crtc->crtc_id);
130 
131 	igt_assert_eq(crtc->buffer_id, 0);
132 
133 	drmModeFreeCrtc(crtc);
134 
135 	for_each_plane_on_pipe(&data->display, pipe, plane) {
136 		drmModePlanePtr planeres = drmModeGetPlane(data->drm_fd, plane->drm_plane->plane_id);
137 
138 		igt_assert_eq(planeres->fb_id, 0);
139 
140 		drmModeFreePlane(planeres);
141 	}
142 
143 	igt_output_set_pipe(output, PIPE_ANY);
144 }
145 
146 static void
run_rmfb_test(struct rmfb_data * data,bool reopen)147 run_rmfb_test(struct rmfb_data *data, bool reopen)
148 {
149 	igt_output_t *output;
150 	enum pipe pipe;
151 
152 	for_each_pipe_with_single_output(&data->display, pipe, output)
153 		test_rmfb(data, output, pipe, reopen);
154 }
155 
156 igt_main
157 {
158 	struct rmfb_data data = {};
159 
160 	igt_skip_on_simulation();
161 
162 	igt_fixture {
163 		data.drm_fd = drm_open_driver_master(DRIVER_ANY);
164 
165 		kmstest_set_vt_graphics_mode();
166 
167 		igt_display_require(&data.display, data.drm_fd);
168 		igt_display_require_output(&data.display);
169 	}
170 
171 	igt_subtest_f("rmfb-ioctl")
172 		run_rmfb_test(&data, false);
173 
174 	igt_subtest_f("close-fd")
175 		run_rmfb_test(&data, true);
176 
177 	igt_fixture {
178 		igt_display_fini(&data.display);
179 	}
180 }
181