1 /*
2  * Copyright © 2017 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 "config.h"
26 
27 #include <dirent.h>
28 
29 #include "igt.h"
30 #include "igt_edid.h"
31 #include "igt_eld.h"
32 
33 #define HDISPLAY_4K	3840
34 #define VDISPLAY_4K	2160
35 
36 IGT_TEST_DESCRIPTION("Test that in-kernel EDID parsing is producing "
37 		     "expected results by forcing a disconnected HDMI "
38 		     "connector with a known EDID and checking that the "
39 		     "metadata exposed to user space matches.");
40 
41 /**
42  * This collection of tests performs EDID and status injection tests. Injection
43  * forces a given EDID and status on a connector. The kernel will parse the
44  * forced EDID and we will check whether correct metadata is exposed to
45  * userspace.
46  *
47  * Currently, this can be used to test:
48  *
49  * - 4K modes exposed via KMS
50  * - Audio capabilities of the monitor exposed via ALSA. EDID-Like Data (ELD)
51  *   entries in /proc/asound are verified.
52  *
53  * Injection is performed on a disconnected connector.
54  */
55 
56 /** get_connector: get the first disconnected HDMI connector */
57 static drmModeConnector *
get_connector(int drm_fd,drmModeRes * res)58 get_connector(int drm_fd, drmModeRes *res)
59 {
60 	int i;
61 	drmModeConnector *connector;
62 
63 	for (i = 0; i < res->count_connectors; i++) {
64 
65 		connector =
66 			drmModeGetConnectorCurrent(drm_fd, res->connectors[i]);
67 
68 		if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA)
69 			break;
70 
71 		drmModeFreeConnector(connector);
72 		connector = NULL;
73 	}
74 
75 	return connector;
76 }
77 
78 static void
hdmi_inject_4k(int drm_fd,drmModeConnector * connector)79 hdmi_inject_4k(int drm_fd, drmModeConnector *connector)
80 {
81 	const struct edid *edid;
82 	struct kmstest_connector_config config;
83 	int ret, cid, i, crtc_mask = -1;
84 	int fb_id;
85 	struct igt_fb fb;
86 	uint8_t found_4k_mode = 0;
87 	uint32_t devid;
88 
89 	devid = intel_get_drm_devid(drm_fd);
90 
91 	/* 4K requires at least HSW */
92 	igt_require(IS_HASWELL(devid) || intel_gen(devid) >= 8);
93 
94 	edid = igt_kms_get_4k_edid();
95 	kmstest_force_edid(drm_fd, connector, edid);
96 
97 	if (!kmstest_force_connector(drm_fd, connector, FORCE_CONNECTOR_ON))
98 		igt_skip("Could not force connector on\n");
99 
100 	cid = connector->connector_id;
101 
102 	connector = drmModeGetConnectorCurrent(drm_fd, cid);
103 
104 	for (i = 0; i < connector->count_modes; i++) {
105 		if (connector->modes[i].hdisplay == HDISPLAY_4K &&
106 		    connector->modes[i].vdisplay == VDISPLAY_4K) {
107 			found_4k_mode++;
108 			break;
109 		}
110 	}
111 
112 	igt_assert(found_4k_mode);
113 
114 	/* create a configuration */
115 	ret = kmstest_get_connector_config(drm_fd, cid, crtc_mask, &config);
116 	igt_assert(ret);
117 
118 	igt_info("  ");
119 	kmstest_dump_mode(&connector->modes[i]);
120 
121 	/* create framebuffer */
122 	fb_id = igt_create_fb(drm_fd, connector->modes[i].hdisplay,
123 			      connector->modes[i].vdisplay,
124 			      DRM_FORMAT_XRGB8888,
125 			      LOCAL_DRM_FORMAT_MOD_NONE, &fb);
126 
127 	ret = drmModeSetCrtc(drm_fd, config.crtc->crtc_id, fb_id, 0, 0,
128 			     &connector->connector_id, 1,
129 			     &connector->modes[i]);
130 
131 	igt_assert(ret == 0);
132 
133 	igt_remove_fb(drm_fd, &fb);
134 
135 	kmstest_force_connector(drm_fd, connector, FORCE_CONNECTOR_UNSPECIFIED);
136 	kmstest_force_edid(drm_fd, connector, NULL);
137 }
138 
139 static void
hdmi_inject_audio(int drm_fd,drmModeConnector * connector)140 hdmi_inject_audio(int drm_fd, drmModeConnector *connector)
141 {
142 	const struct edid *edid;
143 	int fb_id, cid, ret, crtc_mask = -1;
144 	struct igt_fb fb;
145 	struct kmstest_connector_config config;
146 
147 	edid = igt_kms_get_hdmi_audio_edid();
148 	kmstest_force_edid(drm_fd, connector, edid);
149 
150 	if (!kmstest_force_connector(drm_fd, connector, FORCE_CONNECTOR_ON))
151 		igt_skip("Could not force connector on\n");
152 
153 	cid = connector->connector_id;
154 	connector = drmModeGetConnectorCurrent(drm_fd, cid);
155 
156 	/* create a configuration */
157 	ret = kmstest_get_connector_config(drm_fd, cid, crtc_mask, &config);
158 	igt_assert(ret);
159 
160 	/*
161 	 * Create a framebuffer as to allow the kernel to enable the pipe and
162 	 * enable the audio encoder.
163 	 */
164 	fb_id = igt_create_fb(drm_fd, connector->modes[0].hdisplay,
165 			      connector->modes[0].vdisplay,
166 			      DRM_FORMAT_XRGB8888,
167 			      LOCAL_DRM_FORMAT_MOD_NONE, &fb);
168 
169 	ret = drmModeSetCrtc(drm_fd, config.crtc->crtc_id, fb_id, 0, 0,
170 			     &connector->connector_id, 1,
171 			     &connector->modes[0]);
172 
173 
174 	igt_assert(ret == 0);
175 
176 	/*
177 	 * Test if we have /proc/asound/HDMI/eld#0.0 and is its contents are
178 	 * valid.
179 	 */
180 	igt_assert(eld_has_igt());
181 
182 	igt_remove_fb(drm_fd, &fb);
183 
184 	igt_info("  ");
185 	kmstest_dump_mode(&connector->modes[0]);
186 
187 	kmstest_force_connector(drm_fd, connector, FORCE_CONNECTOR_UNSPECIFIED);
188 	kmstest_force_edid(drm_fd, connector, NULL);
189 }
190 
191 igt_main
192 {
193 	int drm_fd;
194 	drmModeRes *res;
195 	drmModeConnector *connector;
196 
197 	igt_fixture {
198 		drm_fd = drm_open_driver_master(DRIVER_INTEL);
199 
200 		res = drmModeGetResources(drm_fd);
201 		igt_require(res);
202 
203 		connector = get_connector(drm_fd, res);
204 		igt_require(connector);
205 
206 		kmstest_unset_all_crtcs(drm_fd, res);
207 	}
208 
209 	igt_describe("Make sure that 4K modes exposed by DRM match the "
210                      "forced EDID and modesetting using it succeed.");
211 	igt_subtest("inject-4k")
212 		hdmi_inject_4k(drm_fd, connector);
213 
214 	igt_describe("Make sure that audio information exposed by ALSA "
215 		     "match the forced EDID.");
216 	igt_subtest("inject-audio")
217 		hdmi_inject_audio(drm_fd, connector);
218 
219 	igt_fixture {
220 		drmModeFreeConnector(connector);
221 	}
222 }
223