summaryrefslogtreecommitdiff
path: root/lib/igt_dsc.c
blob: 229cd3298169d9ca31721cebc548c3aba039cb1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* SPDX-License-Identifier: MIT */
/*
 * Copyright © 2023 Intel Corporation
 */

#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "igt_core.h"
#include "igt_dsc.h"
#include "igt_sysfs.h"

static bool check_dsc_debugfs(int drmfd, char *connector_name, const char *check_str)
{
	char file_name[128] = {0};
	char buf[512];

	sprintf(file_name, "%s/i915_dsc_fec_support", connector_name);

	igt_debugfs_read(drmfd, file_name, buf);

	return strstr(buf, check_str);
}

static int write_dsc_debugfs(int drmfd, char *connector_name, const char *file_name,
			     const char *write_buf)
{
	int debugfs_fd = igt_debugfs_dir(drmfd);
	int len = strlen(write_buf);
	int ret;
	char file_path[128] = {0};

	sprintf(file_path, "%s/%s", connector_name, file_name);

	ret = igt_sysfs_write(debugfs_fd, file_path, write_buf, len);

	close(debugfs_fd);

	if (ret > 0)
		return 0;

	return ret;
}

/**
 * igt_is_dsc_supported_by_source:
 * @drmfd: A drm file descriptor
 *
 * Returns: True if DSC is supported by source, false otherwise.
 */
bool igt_is_dsc_supported_by_source(int drmfd)
{
	char buf[4096];
	int dir, res;

	dir = igt_debugfs_dir(drmfd);
	igt_assert(dir >= 0);

	res = igt_debugfs_simple_read(dir, "i915_display_capabilities",
				      buf, sizeof(buf));
	close(dir);

	return res > 0 ? strstr(buf, "has_dsc: yes") : 0;
}

/**
 * igt_is_dsc_supported_by_sink:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: True if DSC is supported for the given connector/sink, false otherwise.
 */
bool igt_is_dsc_supported_by_sink(int drmfd, char *connector_name)
{
	return check_dsc_debugfs(drmfd, connector_name, "DSC_Sink_Support: yes");
}

/**
 * igt_is_fec_supported:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: True if FEC is supported for the given connector, false otherwise.
 */
bool igt_is_fec_supported(int drmfd, char *connector_name)
{
	return check_dsc_debugfs(drmfd, connector_name, "FEC_Sink_Support: yes");
}

/**
 * igt_is_dsc_enabled:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: True if DSC is enabled for the given connector, false otherwise.
 */
bool igt_is_dsc_enabled(int drmfd, char *connector_name)
{
	return check_dsc_debugfs(drmfd, connector_name, "DSC_Enabled: yes");
}

/**
 * igt_is_force_dsc_enabled:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: True if DSC is force enabled (via debugfs) for the given connector,
 * false otherwise.
 */
bool igt_is_force_dsc_enabled(int drmfd, char *connector_name)
{
	return check_dsc_debugfs(drmfd, connector_name, "Force_DSC_Enable: yes");
}

/**
 * igt_force_dsc_enable:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: 0 on success or negative error code, in case of failure.
 */
int igt_force_dsc_enable(int drmfd, char *connector_name)
{
	return write_dsc_debugfs(drmfd, connector_name, "i915_dsc_fec_support", "1");
}

/**
 * igt_force_dsc_enable_bpc:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 * @bpc: Input BPC
 *
 * Returns: 0 on success or negative error code, in case of failure.
 */
int igt_force_dsc_enable_bpc(int drmfd, char *connector_name, int bpc)
{
	char buf[20] = {0};

	sprintf(buf, "%d", bpc);

	return write_dsc_debugfs(drmfd, connector_name, "i915_dsc_bpc", buf);
}

/**
 * igt_get_dsc_debugfs_fd:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: fd of the DSC debugfs for the given connector, else returns -1.
 */
int igt_get_dsc_debugfs_fd(int drmfd, char *connector_name)
{
	char file_name[128] = {0};

	sprintf(file_name, "%s/i915_dsc_fec_support", connector_name);

	return openat(igt_debugfs_dir(drmfd), file_name, O_WRONLY);
}

/**
 * igt_is_dsc_output_format_supported_by_sink:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 * @output_format: Output format
 *
 * Returns: True if DSC output format is supported for the given connector,
 * false otherwise.
 */
bool igt_is_dsc_output_format_supported_by_sink(int drmfd, char *connector_name,
						enum dsc_output_format output_format)
{
	const char *check_str = "OUTPUTFORMATNOTFOUND";

	switch (output_format) {
	case DSC_FORMAT_RGB:
		check_str = "RGB: yes";
		break;
	case DSC_FORMAT_YCBCR420:
		check_str = "YCBCR420: yes";
		break;
	case DSC_FORMAT_YCBCR444:
		check_str = "YCBCR444: yes";
		break;
	default:
		break;
	}

	return check_dsc_debugfs(drmfd, connector_name, check_str);
}

/**
 * igt_force_dsc_output_format:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 * @output_format: Output format
 *
 * Returns: 0 on success or negative error code, in case of failure.
 */
int igt_force_dsc_output_format(int drmfd, char *connector_name,
				enum dsc_output_format output_format)
{
	char buf[20] = {0};

	sprintf(buf, "%d", output_format);

	return write_dsc_debugfs(drmfd, connector_name, "i915_dsc_output_format", buf);
}

/**
 * igt_get_dsc_fractional_bpp_supported:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: DSC BPP precision supported by the sink.
 * 	    Precision value of
 * 		16 => 1/16
 * 		8  => 1/8
 * 		1  => fractional bpp not supported
 */
int igt_get_dsc_fractional_bpp_supported(int drmfd, char *connector_name)
{
	char file_name[128] = {0};
	char buf[512];
	char *start_loc;
	int bpp_prec;

	sprintf(file_name, "%s/i915_dsc_fec_support", connector_name);
	igt_debugfs_read(drmfd, file_name, buf);

	igt_assert(start_loc = strstr(buf, "DSC_Sink_BPP_Precision: "));
	igt_assert_eq(sscanf(start_loc, "DSC_Sink_BPP_Precision: %d", &bpp_prec), 1);
	igt_assert(bpp_prec > 0);

	return bpp_prec;
}

/**
 * igt_is_dsc_fractional_bpp_supported_by_sink:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: True if DSC fractional bpp is supported for the given connector,
 * false otherwise.
 */
bool igt_is_dsc_fractional_bpp_supported_by_sink(int drmfd, char *connector_name)
{
	int bpp_prec;

	bpp_prec = igt_get_dsc_fractional_bpp_supported(drmfd, connector_name);

	if (bpp_prec == 1)
		return false;

	return true;
}

static bool check_dsc_fractional_bpp_debugfs(int drmfd, char *connector_name,
					     const char *check_str)
{
	char file_name[128] = {0};
	char buf[512];

	sprintf(file_name, "%s/i915_dsc_fractional_bpp", connector_name);

	igt_debugfs_read(drmfd, file_name, buf);

	return strstr(buf, check_str);
}

/**
 * igt_is_force_dsc_fractional_bpp_enabled:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: True if DSC Fractional BPP is force enabled (via debugfs) for the given connector,
 * false otherwise.
 */
bool igt_is_force_dsc_fractional_bpp_enabled(int drmfd, char *connector_name)
{
	return check_dsc_fractional_bpp_debugfs(drmfd, connector_name, "Force_DSC_Fractional_BPP_Enable: yes");
}

/**
 * igt_force_dsc_fractional_bpp_enable:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: 0 on success or negative error code, in case of failure.
 */
int igt_force_dsc_fractional_bpp_enable(int drmfd, char *connector_name)
{
	return write_dsc_debugfs(drmfd, connector_name, "i915_dsc_fractional_bpp", "1");
}

/**
 * igt_get_dsc_fractional_bpp_debugfs_fd:
 * @drmfd: A drm file descriptor
 * @connector_name: Name of the libdrm connector we're going to use
 *
 * Returns: fd of the DSC Fractional BPP debugfs for the given connector,
 * else returns -1.
 */
int igt_get_dsc_fractional_bpp_debugfs_fd(int drmfd, char *connector_name)
{
	char file_name[128] = {0};

	sprintf(file_name, "%s/i915_dsc_fractional_bpp", connector_name);

	return openat(igt_debugfs_dir(drmfd), file_name, O_WRONLY);
}