igt_kms.c 114 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
Daniel Vetter's avatar
Daniel Vetter committed
23 24 25
 * Authors:
 * 	Daniel Vetter <daniel.vetter@ffwll.ch>
 * 	Damien Lespiau <damien.lespiau@intel.com>
26 27
 */

28
#include "config.h"
29
#include <inttypes.h>
30
#include <unistd.h>
31
#include <stdio.h>
32
#include <stdarg.h>
33 34 35
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
36
#include <stdlib.h>
37
#ifdef HAVE_LINUX_KD_H
38
#include <linux/kd.h>
39 40 41
#elif HAVE_SYS_KD_H
#include <sys/kd.h>
#endif
42

43
#include <libudev.h>
44
#include <poll.h>
45
#include <errno.h>
Thomas Wood's avatar
Thomas Wood committed
46
#include <time.h>
47

Daniel Vetter's avatar
Daniel Vetter committed
48 49
#include <i915_drm.h>

50 51
#include "drmtest.h"
#include "igt_kms.h"
Daniel Vetter's avatar
Daniel Vetter committed
52
#include "igt_aux.h"
53
#include "intel_chipset.h"
54
#include "igt_debugfs.h"
55
#include "igt_sysfs.h"
56
#include "sw_sync.h"
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/**
 * SECTION:igt_kms
 * @short_description: Kernel modesetting support library
 * @title: KMS
 * @include: igt.h
 *
 * This library provides support to enumerate and set modeset configurations.
 *
 * There are two parts in this library: First the low level helper function
 * which directly build on top of raw ioctls or the interfaces provided by
 * libdrm. Those functions all have a kmstest_ prefix.
 *
 * The second part is a high-level library to manage modeset configurations
 * which abstracts away some of the low-level details like the difference
 * between legacy and universal plane support for setting cursors or in the
 * future the difference between legacy and atomic commit. These high-level
 * functions have all igt_ prefixes. This part is still very much work in
 * progress and so also lacks a bit documentation for the individual functions.
 *
77
 * Note that this library's header pulls in the [i-g-t framebuffer](igt-gpu-tools-i-g-t-framebuffer.html)
78 79 80
 * library as a dependency.
 */

81 82 83
/* list of connectors that need resetting on exit */
#define MAX_CONNECTORS 32
static char *forced_connectors[MAX_CONNECTORS + 1];
84
static int forced_connectors_device[MAX_CONNECTORS + 1];
85

86
static void update_edid_csum(unsigned char *edid, int cea_pos)
87 88 89 90 91 92 93 94 95 96 97 98
{
	int i, sum = 0;
	struct tm *tm;
	time_t t;

	/* year of manufacture */
	t = time(NULL);
	tm = localtime(&t);
	edid[17] = tm->tm_year - 90;

	/* calculate checksum */
	for (i = 0; i < 127; i++) {
99
		sum = sum + edid[cea_pos + i];
100
	}
101
	edid[cea_pos + 127] = 256 - sum;
102
}
103

Thomas Wood's avatar
Thomas Wood committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117
#define VFREQ 60
#define CLOCK 148500
#define HACTIVE 1920
#define HBLANK 280
#define VACTIVE 1080
#define VBLANK 45
#define HOFFSET 88
#define HPULSE 44
#define VOFFSET 4
#define VPULSE 5

#define HSIZE 52
#define VSIZE 30

118 119
#define EDID_NAME base_edid
#include "igt_edid_template.h"
Thomas Wood's avatar
Thomas Wood committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

/**
 * igt_kms_get_base_edid:
 *
 * Get the base edid block, which includes the following modes:
 *
 *  - 1920x1080 60Hz
 *  - 1280x720 60Hz
 *  - 1024x768 60Hz
 *  - 800x600 60Hz
 *  - 640x480 60Hz
 *
 * This can be extended with further features using functions such as
 * #kmstest_edid_add_3d.
 *
 * Returns: a basic edid block
 */
const unsigned char* igt_kms_get_base_edid(void)
{
139
	update_edid_csum(base_edid, 0);
Thomas Wood's avatar
Thomas Wood committed
140 141 142 143

	return base_edid;
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#define VFREQ 60
#define CLOCK 101000
#define HACTIVE 1400
#define HBLANK 160
#define VACTIVE 1050
#define VBLANK 30
#define HOFFSET 48
#define HPULSE 32
#define VOFFSET 3
#define VPULSE 4

#define HSIZE 52
#define VSIZE 30

#define EDID_NAME alt_edid
#include "igt_edid_template.h"

161
const char * const igt_plane_prop_names[IGT_NUM_PLANE_PROPS] = {
162 163 164 165 166 167 168 169 170 171 172 173 174 175
	[IGT_PLANE_SRC_X] = "SRC_X",
	[IGT_PLANE_SRC_Y] = "SRC_Y",
	[IGT_PLANE_SRC_W] = "SRC_W",
	[IGT_PLANE_SRC_H] = "SRC_H",
	[IGT_PLANE_CRTC_X] = "CRTC_X",
	[IGT_PLANE_CRTC_Y] = "CRTC_Y",
	[IGT_PLANE_CRTC_W] = "CRTC_W",
	[IGT_PLANE_CRTC_H] = "CRTC_H",
	[IGT_PLANE_FB_ID] = "FB_ID",
	[IGT_PLANE_CRTC_ID] = "CRTC_ID",
	[IGT_PLANE_IN_FENCE_FD] = "IN_FENCE_FD",
	[IGT_PLANE_TYPE] = "type",
	[IGT_PLANE_ROTATION] = "rotation",
	[IGT_PLANE_IN_FORMATS] = "IN_FORMATS",
176 177
	[IGT_PLANE_COLOR_ENCODING] = "COLOR_ENCODING",
	[IGT_PLANE_COLOR_RANGE] = "COLOR_RANGE",
178 179
	[IGT_PLANE_PIXEL_BLEND_MODE] = "pixel blend mode",
	[IGT_PLANE_ALPHA] = "alpha",
180 181
};

182
const char * const igt_crtc_prop_names[IGT_NUM_CRTC_PROPS] = {
183 184 185 186 187 188 189 190 191
	[IGT_CRTC_BACKGROUND] = "background_color",
	[IGT_CRTC_CTM] = "CTM",
	[IGT_CRTC_GAMMA_LUT] = "GAMMA_LUT",
	[IGT_CRTC_GAMMA_LUT_SIZE] = "GAMMA_LUT_SIZE",
	[IGT_CRTC_DEGAMMA_LUT] = "DEGAMMA_LUT",
	[IGT_CRTC_DEGAMMA_LUT_SIZE] = "DEGAMMA_LUT_SIZE",
	[IGT_CRTC_MODE_ID] = "MODE_ID",
	[IGT_CRTC_ACTIVE] = "ACTIVE",
	[IGT_CRTC_OUT_FENCE_PTR] = "OUT_FENCE_PTR",
192
	[IGT_CRTC_VRR_ENABLED] = "VRR_ENABLED",
193 194
};

195
const char * const igt_connector_prop_names[IGT_NUM_CONNECTOR_PROPS] = {
196 197 198 199
	[IGT_CONNECTOR_SCALING_MODE] = "scaling mode",
	[IGT_CONNECTOR_CRTC_ID] = "CRTC_ID",
	[IGT_CONNECTOR_DPMS] = "DPMS",
	[IGT_CONNECTOR_BROADCAST_RGB] = "Broadcast RGB",
200
	[IGT_CONNECTOR_CONTENT_PROTECTION] = "Content Protection",
201
	[IGT_CONNECTOR_VRR_CAPABLE] = "vrr_capable",
202 203 204 205
};

/*
 * Retrieve all the properies specified in props_name and store them into
206
 * plane->props.
207 208
 */
static void
209
igt_fill_plane_props(igt_display_t *display, igt_plane_t *plane,
210
		     int num_props, const char * const prop_names[])
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
{
	drmModeObjectPropertiesPtr props;
	int i, j, fd;

	fd = display->drm_fd;

	props = drmModeObjectGetProperties(fd, plane->drm_plane->plane_id, DRM_MODE_OBJECT_PLANE);
	igt_assert(props);

	for (i = 0; i < props->count_props; i++) {
		drmModePropertyPtr prop =
			drmModeGetProperty(fd, props->props[i]);

		for (j = 0; j < num_props; j++) {
			if (strcmp(prop->name, prop_names[j]) != 0)
				continue;

228
			plane->props[j] = props->props[i];
229 230 231 232 233 234 235 236 237 238 239
			break;
		}

		drmModeFreeProperty(prop);
	}

	drmModeFreeObjectProperties(props);
}

/*
 * Retrieve all the properies specified in props_name and store them into
240
 * config->atomic_props_crtc and config->atomic_props_connector.
241 242
 */
static void
243
igt_atomic_fill_connector_props(igt_display_t *display, igt_output_t *output,
244
			int num_connector_props, const char * const conn_prop_names[])
245 246 247 248 249 250
{
	drmModeObjectPropertiesPtr props;
	int i, j, fd;

	fd = display->drm_fd;

251
	props = drmModeObjectGetProperties(fd, output->config.connector->connector_id, DRM_MODE_OBJECT_CONNECTOR);
252 253 254 255 256 257
	igt_assert(props);

	for (i = 0; i < props->count_props; i++) {
		drmModePropertyPtr prop =
			drmModeGetProperty(fd, props->props[i]);

258 259
		for (j = 0; j < num_connector_props; j++) {
			if (strcmp(prop->name, conn_prop_names[j]) != 0)
260 261
				continue;

262
			output->props[j] = props->props[i];
263 264 265 266 267 268 269
			break;
		}

		drmModeFreeProperty(prop);
	}

	drmModeFreeObjectProperties(props);
270 271 272
}

static void
273
igt_fill_pipe_props(igt_display_t *display, igt_pipe_t *pipe,
274
		    int num_crtc_props, const char * const crtc_prop_names[])
275 276 277 278 279 280 281
{
	drmModeObjectPropertiesPtr props;
	int i, j, fd;

	fd = display->drm_fd;

	props = drmModeObjectGetProperties(fd, pipe->crtc_id, DRM_MODE_OBJECT_CRTC);
282 283 284 285 286 287
	igt_assert(props);

	for (i = 0; i < props->count_props; i++) {
		drmModePropertyPtr prop =
			drmModeGetProperty(fd, props->props[i]);

288 289
		for (j = 0; j < num_crtc_props; j++) {
			if (strcmp(prop->name, crtc_prop_names[j]) != 0)
290 291
				continue;

292
			pipe->props[j] = props->props[i];
293 294 295 296 297 298
			break;
		}

		drmModeFreeProperty(prop);
	}

299
	drmModeFreeObjectProperties(props);
300 301
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
/**
 * igt_kms_get_alt_edid:
 *
 * Get an alternate edid block, which includes the following modes:
 *
 *  - 1400x1050 60Hz
 *  - 1920x1080 60Hz
 *  - 1280x720 60Hz
 *  - 1024x768 60Hz
 *  - 800x600 60Hz
 *  - 640x480 60Hz
 *
 * This can be extended with further features using functions such as
 * #kmstest_edid_add_3d.
 *
 * Returns: an alternate edid block
 */
319 320
const unsigned char* igt_kms_get_alt_edid(void)
{
321
	update_edid_csum(alt_edid, 0);
322 323 324

	return alt_edid;
}
Thomas Wood's avatar
Thomas Wood committed
325

326 327 328 329
/**
 * kmstest_pipe_name:
 * @pipe: display pipe
 *
Robert Foss's avatar
Robert Foss committed
330
 * Returns: String representing @pipe, e.g. "A".
331 332
 */
const char *kmstest_pipe_name(enum pipe pipe)
333
{
334 335 336 337
	static const char str[] = "A\0B\0C\0D\0E\0F";

	_Static_assert(sizeof(str) == IGT_MAX_PIPES * 2,
		       "Missing pipe name");
338

339 340 341
	if (pipe == PIPE_NONE)
		return "None";

342
	if (pipe >= IGT_MAX_PIPES)
343 344
		return "invalid";

345
	return str + (pipe * 2);
346 347
}

348 349 350 351 352 353 354 355
/**
 * kmstest_pipe_to_index:
 *@pipe: display pipe in string format
 *
 * Returns: index to corresponding pipe
 */
int kmstest_pipe_to_index(char pipe)
{
356 357 358
	int r = pipe - 'A';

	if (r < 0 || r >= IGT_MAX_PIPES)
359
		return -EINVAL;
360 361

	return r;
362 363
}

364 365
/**
 * kmstest_plane_type_name:
366
 * @plane_type: display plane type
367
 *
368
 * Returns: String representing @plane_type, e.g. "overlay".
369 370 371
 */
const char *kmstest_plane_type_name(int plane_type)
{
372
	static const char * const names[] = {
373 374 375 376 377 378 379 380 381 382
		[DRM_PLANE_TYPE_OVERLAY] = "overlay",
		[DRM_PLANE_TYPE_PRIMARY] = "primary",
		[DRM_PLANE_TYPE_CURSOR] = "cursor",
	};

	igt_assert(plane_type < ARRAY_SIZE(names) && names[plane_type]);

	return names[plane_type];
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
struct type_name {
	int type;
	const char *name;
};

static const char *find_type_name(const struct type_name *names, int type)
{
	for (; names->name; names++) {
		if (names->type == type)
			return names->name;
	}

	return "(invalid)";
}

static const struct type_name encoder_type_names[] = {
	{ DRM_MODE_ENCODER_NONE, "none" },
	{ DRM_MODE_ENCODER_DAC, "DAC" },
	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
	{ DRM_MODE_ENCODER_TVDAC, "TVDAC" },
	{ DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
	{ DRM_MODE_ENCODER_DSI, "DSI" },
	{ DRM_MODE_ENCODER_DPMST, "DP MST" },
	{}
};

/**
 * kmstest_encoder_type_str:
 * @type: DRM_MODE_ENCODER_* enumeration value
 *
 * Returns: A string representing the drm encoder @type.
 */
const char *kmstest_encoder_type_str(int type)
{
	return find_type_name(encoder_type_names, type);
}

static const struct type_name connector_status_names[] = {
	{ DRM_MODE_CONNECTED, "connected" },
	{ DRM_MODE_DISCONNECTED, "disconnected" },
	{ DRM_MODE_UNKNOWNCONNECTION, "unknown" },
	{}
};

/**
 * kmstest_connector_status_str:
 * @status: DRM_MODE_* connector status value
 *
 * Returns: A string representing the drm connector status @status.
 */
const char *kmstest_connector_status_str(int status)
{
	return find_type_name(connector_status_names, status);
}

static const struct type_name connector_type_names[] = {
	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
	{ DRM_MODE_CONNECTOR_Component, "Component" },
	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
	{ DRM_MODE_CONNECTOR_TV, "TV" },
	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
	{}
};

/**
 * kmstest_connector_type_str:
 * @type: DRM_MODE_CONNECTOR_* enumeration value
 *
 * Returns: A string representing the drm connector @type.
 */
const char *kmstest_connector_type_str(int type)
{
	return find_type_name(connector_type_names, type);
}

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
static const char *mode_stereo_name(const drmModeModeInfo *mode)
{
	switch (mode->flags & DRM_MODE_FLAG_3D_MASK) {
	case DRM_MODE_FLAG_3D_FRAME_PACKING:
		return "FP";
	case DRM_MODE_FLAG_3D_FIELD_ALTERNATIVE:
		return "FA";
	case DRM_MODE_FLAG_3D_LINE_ALTERNATIVE:
		return "LA";
	case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_FULL:
		return "SBSF";
	case DRM_MODE_FLAG_3D_L_DEPTH:
		return "LD";
	case DRM_MODE_FLAG_3D_L_DEPTH_GFX_GFX_DEPTH:
		return "LDGFX";
	case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
		return "TB";
	case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
		return "SBSH";
	default:
		return NULL;
	}
}

496 497 498 499
/**
 * kmstest_dump_mode:
 * @mode: libdrm mode structure
 *
500
 * Prints @mode to stdout in a human-readable form.
501
 */
502 503 504 505
void kmstest_dump_mode(drmModeModeInfo *mode)
{
	const char *stereo = mode_stereo_name(mode);

506 507 508 509 510 511 512 513 514
	igt_info("  %s %d %d %d %d %d %d %d %d %d 0x%x 0x%x %d%s%s%s\n",
		 mode->name, mode->vrefresh,
		 mode->hdisplay, mode->hsync_start,
		 mode->hsync_end, mode->htotal,
		 mode->vdisplay, mode->vsync_start,
		 mode->vsync_end, mode->vtotal,
		 mode->flags, mode->type, mode->clock,
		 stereo ? " (3D:" : "",
		 stereo ? stereo : "", stereo ? ")" : "");
515 516
}

517 518 519 520 521
/**
 * kmstest_get_pipe_from_crtc_id:
 * @fd: DRM fd
 * @crtc_id: DRM CRTC id
 *
522 523 524 525
 * Returns: The crtc index for the given DRM CRTC ID @crtc_id. The crtc index
 * is the equivalent of the pipe id.  This value maps directly to an enum pipe
 * value used in other helper functions.  Returns 0 if the index could not be
 * determined.
526
 */
527

528 529
int kmstest_get_pipe_from_crtc_id(int fd, int crtc_id)
{
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	drmModeRes *res;
	drmModeCrtc *crtc;
	int i, cur_id;

	res = drmModeGetResources(fd);
	igt_assert(res);

	for (i = 0; i < res->count_crtcs; i++) {
		crtc = drmModeGetCrtc(fd, res->crtcs[i]);
		igt_assert(crtc);
		cur_id = crtc->crtc_id;
		drmModeFreeCrtc(crtc);
		if (cur_id == crtc_id)
			break;
	}

	igt_assert(i < res->count_crtcs);
547

548 549
	drmModeFreeResources(res);

550
	return i;
551 552
}

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
/**
 * kmstest_find_crtc_for_connector:
 * @fd: DRM fd
 * @res: libdrm resources pointer
 * @connector: libdrm connector pointer
 * @crtc_blacklist_idx_mask: a mask of CRTC indexes that we can't return
 *
 * Returns: the CRTC ID for a CRTC that fits the connector, otherwise it asserts
 * false and never returns. The blacklist mask can be used in case you have
 * CRTCs that are already in use by other connectors.
 */
uint32_t kmstest_find_crtc_for_connector(int fd, drmModeRes *res,
					 drmModeConnector *connector,
					 uint32_t crtc_blacklist_idx_mask)
{
	drmModeEncoder *e;
	uint32_t possible_crtcs;
	int i, j;

	for (i = 0; i < connector->count_encoders; i++) {
		e = drmModeGetEncoder(fd, connector->encoders[i]);
		possible_crtcs = e->possible_crtcs & ~crtc_blacklist_idx_mask;
		drmModeFreeEncoder(e);

		for (j = 0; possible_crtcs >> j; j++)
			if (possible_crtcs & (1 << j))
				return res->crtcs[j];
	}

	igt_assert(false);
}

585 586 587 588 589 590
/**
 * kmstest_dumb_create:
 * @fd: open drm file descriptor
 * @width: width of the buffer in pixels
 * @height: height of the buffer in pixels
 * @bpp: bytes per pixel of the buffer
591 592
 * @stride: Pointer which receives the dumb bo's stride, can be NULL.
 * @size: Pointer which receives the dumb bo's size, can be NULL.
593 594 595 596 597 598 599
 *
 * This wraps the CREATE_DUMB ioctl, which allocates a new dumb buffer object
 * for the specified dimensions.
 *
 * Returns: The file-private handle of the created buffer object
 */
uint32_t kmstest_dumb_create(int fd, int width, int height, int bpp,
600
			     unsigned *stride, uint64_t *size)
601 602 603 604 605 606 607 608 609 610 611
{
	struct drm_mode_create_dumb create;

	memset(&create, 0, sizeof(create));
	create.width = width;
	create.height = height;
	create.bpp = bpp;

	create.handle = 0;
	do_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
	igt_assert(create.handle);
612
	igt_assert(create.size >= (uint64_t) width * height * bpp / 8);
613 614 615 616 617 618 619 620 621 622

	if (stride)
		*stride = create.pitch;

	if (size)
		*size = create.size;

	return create.handle;
}

623 624 625 626 627 628 629 630
/**
 * kmstest_dumb_map_buffer:
 * @fd: Opened drm file descriptor
 * @handle: Offset in the file referred to by fd
 * @size: Length of the mapping, must be greater than 0
 * @prot: Describes the memory protection of the mapping
 * Returns: A pointer representing the start of the virtual mapping
 */
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
void *kmstest_dumb_map_buffer(int fd, uint32_t handle, uint64_t size,
			      unsigned prot)
{
	struct drm_mode_map_dumb arg = {};
	void *ptr;

	arg.handle = handle;

	do_ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);

	ptr = mmap(NULL, size, prot, MAP_SHARED, fd, arg.offset);
	igt_assert(ptr != MAP_FAILED);

	return ptr;
}

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
static int __kmstest_dumb_destroy(int fd, uint32_t handle)
{
	struct drm_mode_destroy_dumb arg = { handle };
	int err = 0;

	if (drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg))
		err = -errno;

	errno = 0;
	return err;
}

/**
 * kmstest_dumb_destroy:
 * @fd: Opened drm file descriptor
 * @handle: Offset in the file referred to by fd
 */
void kmstest_dumb_destroy(int fd, uint32_t handle)
{
	igt_assert_eq(__kmstest_dumb_destroy(fd, handle), 0);
}

669 670 671 672
/*
 * Returns: the previous mode, or KD_GRAPHICS if no /dev/tty0 was
 * found and nothing was done.
 */
673 674 675 676
static signed long set_vt_mode(unsigned long mode)
{
	int fd;
	unsigned long prev_mode;
677 678 679 680 681 682 683 684 685 686
	static const char TTY0[] = "/dev/tty0";

	if (access(TTY0, F_OK)) {
		/* errno message should be "No such file". Do not
		   hardcode but ask strerror() in the very unlikely
		   case something else happened. */
		igt_debug("VT: %s: %s, cannot change its mode\n",
			  TTY0, strerror(errno));
		return KD_GRAPHICS;
	}
687

688
	fd = open(TTY0, O_RDONLY);
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
	if (fd < 0)
		return -errno;

	prev_mode = 0;
	if (drmIoctl(fd, KDGETMODE, &prev_mode))
		goto err;
	if (drmIoctl(fd, KDSETMODE, (void *)mode))
		goto err;

	close(fd);

	return prev_mode;
err:
	close(fd);

	return -errno;
}

static unsigned long orig_vt_mode = -1UL;

Thomas Wood's avatar
Thomas Wood committed
709 710 711 712 713 714
/**
 * kmstest_restore_vt_mode:
 *
 * Restore the VT mode in use before #kmstest_set_vt_graphics_mode was called.
 */
void kmstest_restore_vt_mode(void)
715
{
Thomas Wood's avatar
Thomas Wood committed
716 717 718 719 720 721
	long ret;

	if (orig_vt_mode != -1UL) {
		ret = set_vt_mode(orig_vt_mode);

		igt_assert(ret >= 0);
722 723
		igt_debug("VT: original mode 0x%lx restored\n", orig_vt_mode);
		orig_vt_mode = -1UL;
Thomas Wood's avatar
Thomas Wood committed
724
	}
725 726
}

727 728 729
/**
 * kmstest_set_vt_graphics_mode:
 *
Thomas Wood's avatar
Thomas Wood committed
730 731 732
 * Sets the controlling VT (if available) into graphics/raw mode and installs
 * an igt exit handler to set the VT back to text mode on exit. Use
 * #kmstest_restore_vt_mode to restore the previous VT mode manually.
733 734 735
 *
 * All kms tests must call this function to make sure that the fbcon doesn't
 * interfere by e.g. blanking the screen.
736
 */
737
void kmstest_set_vt_graphics_mode(void)
738
{
739 740
	long ret;

Thomas Wood's avatar
Thomas Wood committed
741
	igt_install_exit_handler((igt_exit_handler_t) kmstest_restore_vt_mode);
742

743
	ret = set_vt_mode(KD_GRAPHICS);
744

745 746
	igt_assert(ret >= 0);
	orig_vt_mode = ret;
Thomas Wood's avatar
Thomas Wood committed
747

748
	igt_debug("VT: graphics mode set (mode was 0x%lx)\n", ret);
749 750
}

751 752 753 754 755 756

static void reset_connectors_at_exit(int sig)
{
	igt_reset_connectors();
}

757 758 759 760 761 762 763 764 765 766 767 768 769
/**
 * kmstest_force_connector:
 * @fd: drm file descriptor
 * @connector: connector
 * @state: state to force on @connector
 *
 * Force the specified state on the specified connector.
 *
 * Returns: true on success
 */
bool kmstest_force_connector(int drm_fd, drmModeConnector *connector,
			     enum kmstest_force_connector_state state)
{
770
	char *path, **tmp;
771
	const char *value;
772
	drmModeConnector *temp;
773
	uint32_t devid;
774
	int len, dir, idx;
775

776 777 778 779 780 781 782 783 784 785 786 787 788 789
	if (is_i915_device(drm_fd)) {
		devid = intel_get_drm_devid(drm_fd);

		/*
		 * forcing hdmi or dp connectors on HSW and BDW doesn't
		 * currently work, so fail early to allow the test to skip if
		 * required
		 */
		if ((connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
		     connector->connector_type == DRM_MODE_CONNECTOR_HDMIB ||
		     connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)
		    && (IS_HASWELL(devid) || IS_BROADWELL(devid)))
			return false;
	}
790 791 792 793 794 795

	switch (state) {
	case FORCE_CONNECTOR_ON:
		value = "on";
		break;
	case FORCE_CONNECTOR_DIGITAL:
796
		value = "on-digital";
797 798 799 800 801 802 803
		break;
	case FORCE_CONNECTOR_OFF:
		value = "off";
		break;

	default:
	case FORCE_CONNECTOR_UNSPECIFIED:
804
		value = "detect";
805 806 807
		break;
	}

808 809 810
	dir = igt_sysfs_open(drm_fd, &idx);
	if (dir < 0)
		return false;
811

812 813 814 815 816
	if (asprintf(&path, "card%d-%s-%d/status",
		     idx,
		     kmstest_connector_type_str(connector->connector_type),
		     connector->connector_type_id) < 0) {
		close(dir);
817 818 819
		return false;
	}

820
	if (!igt_sysfs_set(dir, path, value)) {
821 822 823
		close(dir);
		return false;
	}
824

825 826 827 828 829 830 831 832 833
	for (len = 0, tmp = forced_connectors; *tmp; tmp++) {
		/* check the connector is not already present */
		if (strcmp(*tmp, path) == 0) {
			len = -1;
			break;
		}
		len++;
	}

834
	if (len != -1 && len < MAX_CONNECTORS) {
835
		forced_connectors[len] = path;
836 837
		forced_connectors_device[len] = dir;
	}
838 839 840 841 842 843 844 845 846 847 848 849 850

	if (len >= MAX_CONNECTORS)
		igt_warn("Connector limit reached, %s will not be reset\n",
			 path);

	igt_debug("Connector %s is now forced %s\n", path, value);
	igt_debug("Current forced connectors:\n");
	tmp = forced_connectors;
	while (*tmp) {
		igt_debug("\t%s\n", *tmp);
		tmp++;
	}

851
	igt_install_exit_handler(reset_connectors_at_exit);
852

853 854 855 856 857
	/* To allow callers to always use GetConnectorCurrent we need to force a
	 * redetection here. */
	temp = drmModeGetConnector(drm_fd, connector->connector_id);
	drmModeFreeConnector(temp);

858
	return true;
859 860 861 862 863 864 865 866 867 868
}

/**
 * kmstest_force_edid:
 * @drm_fd: drm file descriptor
 * @connector: connector to set @edid on
 * @edid: An EDID data block
 * @length: length of the EDID data. #EDID_LENGTH defines the standard EDID
 * length
 *
Thomas Wood's avatar
Thomas Wood committed
869
 * Set the EDID data on @connector to @edid. See also #igt_kms_get_base_edid.
870 871 872 873 874 875 876 877
 *
 * If @length is zero, the forced EDID will be removed.
 */
void kmstest_force_edid(int drm_fd, drmModeConnector *connector,
			const unsigned char *edid, size_t length)
{
	char *path;
	int debugfs_fd, ret;
878
	drmModeConnector *temp;
879

880 881
	igt_assert_neq(asprintf(&path, "%s-%d/edid_override", kmstest_connector_type_str(connector->connector_type), connector->connector_type_id),
		       -1);
882
	debugfs_fd = igt_debugfs_open(drm_fd, path, O_WRONLY | O_TRUNC);
883 884
	free(path);

885
	igt_require(debugfs_fd != -1);
886 887 888 889 890 891 892

	if (length == 0)
		ret = write(debugfs_fd, "reset", 5);
	else
		ret = write(debugfs_fd, edid, length);
	close(debugfs_fd);

893 894 895 896 897
	/* To allow callers to always use GetConnectorCurrent we need to force a
	 * redetection here. */
	temp = drmModeGetConnector(drm_fd, connector->connector_id);
	drmModeFreeConnector(temp);

898 899 900
	igt_assert(ret != -1);
}

901 902 903 904 905 906 907 908 909 910
/**
 * kmstest_get_connector_default_mode:
 * @drm_fd: DRM fd
 * @connector: libdrm connector
 * @mode: libdrm mode
 *
 * Retrieves the default mode for @connector and stores it in @mode.
 *
 * Returns: true on success, false on failure
 */
911 912
bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
					drmModeModeInfo *mode)
913 914 915 916
{
	int i;

	if (!connector->count_modes) {
917 918
		igt_warn("no modes for connector %d\n",
			 connector->connector_id);
919
		return false;
920 921 922 923 924 925 926 927 928 929 930
	}

	for (i = 0; i < connector->count_modes; i++) {
		if (i == 0 ||
		    connector->modes[i].type & DRM_MODE_TYPE_PREFERRED) {
			*mode = connector->modes[i];
			if (mode->type & DRM_MODE_TYPE_PREFERRED)
				break;
		}
	}

931
	return true;
932 933
}

934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
static void
_kmstest_connector_config_crtc_mask(int drm_fd,
				    drmModeConnector *connector,
				    struct kmstest_connector_config *config)
{
	int i;

	config->valid_crtc_idx_mask = 0;

	/* Now get a compatible encoder */
	for (i = 0; i < connector->count_encoders; i++) {
		drmModeEncoder *encoder = drmModeGetEncoder(drm_fd,
							    connector->encoders[i]);

		if (!encoder) {
			igt_warn("could not get encoder %d: %s\n",
				 connector->encoders[i],
				 strerror(errno));

			continue;
		}

		config->valid_crtc_idx_mask |= encoder->possible_crtcs;
		drmModeFreeEncoder(encoder);
	}
}

static drmModeEncoder *
_kmstest_connector_config_find_encoder(int drm_fd, drmModeConnector *connector, enum pipe pipe)
{
	int i;

	for (i = 0; i < connector->count_encoders; i++) {
		drmModeEncoder *encoder = drmModeGetEncoder(drm_fd, connector->encoders[i]);

		if (!encoder) {
			igt_warn("could not get encoder %d: %s\n",
				 connector->encoders[i],
				 strerror(errno));

			continue;
		}

		if (encoder->possible_crtcs & (1 << pipe))
			return encoder;

		drmModeFreeEncoder(encoder);
	}

	igt_assert(false);
	return NULL;
}

987
/**
988
 * _kmstest_connector_config:
989 990 991 992
 * @drm_fd: DRM fd
 * @connector_id: DRM connector id
 * @crtc_idx_mask: mask of allowed DRM CRTC indices
 * @config: structure filled with the possible configuration
993
 * @probe: whether to fully re-probe mode list or not
994 995 996 997
 *
 * This tries to find a suitable configuration for the given connector and CRTC
 * constraint and fills it into @config.
 */
998 999 1000 1001
static bool _kmstest_connector_config(int drm_fd, uint32_t connector_id,
				      unsigned long crtc_idx_mask,
				      struct kmstest_connector_config *config,
				      bool probe)
1002 1003 1004 1005
{
	drmModeRes *resources;
	drmModeConnector *connector;

1006 1007
	config->pipe = PIPE_NONE;

1008 1009
	resources = drmModeGetResources(drm_fd);
	if (!resources) {
1010
		igt_warn("drmModeGetResources failed");
1011 1012 1013 1014
		goto err1;
	}

	/* First, find the connector & mode */
1015 1016 1017 1018 1019
	if (probe)
		connector = drmModeGetConnector(drm_fd, connector_id);
	else
		connector = drmModeGetConnectorCurrent(drm_fd, connector_id);

1020 1021 1022 1023
	if (!connector)
		goto err2;

	if (connector->connector_id != connector_id) {
1024 1025
		igt_warn("connector id doesn't match (%d != %d)\n",
			 connector->connector_id, connector_id);
1026 1027 1028 1029 1030 1031 1032 1033
		goto err3;
	}

	/*
	 * Find given CRTC if crtc_id != 0 or else the first CRTC not in use.
	 * In both cases find the first compatible encoder and skip the CRTC
	 * if there is non such.
	 */
1034
	_kmstest_connector_config_crtc_mask(drm_fd, connector, config);
1035

1036 1037 1038 1039
	if (!connector->count_modes)
		memset(&config->default_mode, 0, sizeof(config->default_mode));
	else if (!kmstest_get_connector_default_mode(drm_fd, connector,
						     &config->default_mode))
1040 1041 1042 1043
		goto err3;

	config->connector = connector;

1044 1045
	crtc_idx_mask &= config->valid_crtc_idx_mask;
	if (!crtc_idx_mask)
1046 1047
		/* Keep config->connector */
		goto err2;
1048

1049 1050 1051 1052
	config->pipe = ffs(crtc_idx_mask) - 1;

	config->encoder = _kmstest_connector_config_find_encoder(drm_fd, connector, config->pipe);
	config->crtc = drmModeGetCrtc(drm_fd, resources->crtcs[config->pipe]);
1053

1054 1055 1056 1057
	if (connector->connection != DRM_MODE_CONNECTED)
		goto err2;

	if (!connector->count_modes) {
1058 1059 1060 1061
		if (probe)
			igt_warn("connector %d/%s-%d has no modes\n", connector_id,
				kmstest_connector_type_str(connector->connector_type),
				connector->connector_type_id);
1062 1063
		goto err2;
	}
1064

1065
	drmModeFreeResources(resources);
1066
	return true;
1067
err3:
1068
	drmModeFreeConnector(connector);
1069 1070 1071
err2:
	drmModeFreeResources(resources);
err1:
1072
	return false;
1073 1074
}

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
/**
 * kmstest_get_connector_config:
 * @drm_fd: DRM fd
 * @connector_id: DRM connector id
 * @crtc_idx_mask: mask of allowed DRM CRTC indices
 * @config: structure filled with the possible configuration
 *
 * This tries to find a suitable configuration for the given connector and CRTC
 * constraint and fills it into @config.
 */
bool kmstest_get_connector_config(int drm_fd, uint32_t connector_id,
				  unsigned long crtc_idx_mask,
				  struct kmstest_connector_config *config)
{
	return _kmstest_connector_config(drm_fd, connector_id, crtc_idx_mask,
					 config, 0);
}

/**
 * kmstest_probe_connector_config:
 * @drm_fd: DRM fd
 * @connector_id: DRM connector id
 * @crtc_idx_mask: mask of allowed DRM CRTC indices
 * @config: structure filled with the possible configuration
 *
 * This tries to find a suitable configuration for the given connector and CRTC
 * constraint and fills it into @config, fully probing the connector in the
 * process.
 */
bool kmstest_probe_connector_config(int drm_fd, uint32_t connector_id,
				    unsigned long crtc_idx_mask,
				    struct kmstest_connector_config *config)
{
	return _kmstest_connector_config(drm_fd, connector_id, crtc_idx_mask,
					 config, 1);
}

1112 1113 1114 1115 1116 1117
/**
 * kmstest_free_connector_config:
 * @config: connector configuration structure
 *
 * Free any resources in @config allocated in kmstest_get_connector_config().
 */
1118
void kmstest_free_connector_config(struct kmstest_connector_config *config)
1119
{
1120
	drmModeFreeCrtc(config->crtc);
1121 1122
	config->crtc = NULL;

1123
	drmModeFreeEncoder(config->encoder);
1124 1125
	config->encoder = NULL;

1126
	drmModeFreeConnector(config->connector);
1127
	config->connector = NULL;
1128 1129
}

1130 1131 1132 1133 1134 1135 1136 1137
/**
 * kmstest_set_connector_dpms:
 * @fd: DRM fd
 * @connector: libdrm connector
 * @mode: DRM DPMS value
 *
 * This function sets the DPMS setting of @connector to @mode.
 */
1138
void kmstest_set_connector_dpms(int fd, drmModeConnector *connector, int mode)
1139
{
1140 1141
	int i, dpms = 0;
	bool found_it = false;
1142

1143
	for (i = 0; i < connector->count_props; i++) {
1144 1145 1146
		struct drm_mode_get_property prop = {
			.prop_id = connector->props[i],
		};
1147

1148 1149
		if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
			continue;
1150

1151 1152
		if (strcmp(prop.name, "DPMS"))
			continue;
1153

1154 1155
		dpms = prop.prop_id;
		found_it = true;
1156 1157
		break;
	}
1158 1159
	igt_assert_f(found_it, "DPMS property not found on %d\n",
		     connector->connector_id);
1160

1161 1162
	igt_assert(drmModeConnectorSetProperty(fd, connector->connector_id,
					       dpms, mode) == 0);
1163 1164
}

1165
/**
1166
 * kmstest_get_property:
1167
 * @drm_fd: drm file descriptor
1168 1169 1170 1171 1172 1173 1174
 * @object_id: object whose properties we're going to get
 * @object_type: type of obj_id (DRM_MODE_OBJECT_*)
 * @name: name of the property we're going to get
 * @prop_id: if not NULL, returns the property id
 * @value: if not NULL, returns the property value
 * @prop: if not NULL, returns the property, and the caller will have to free
 *        it manually.
1175
 *
1176
 * Finds a property with the given name on the given object.
1177
 *
1178
 * Returns: true in case we found something.
1179
 */
1180 1181 1182 1183 1184
bool
kmstest_get_property(int drm_fd, uint32_t object_id, uint32_t object_type,
		     const char *name, uint32_t *prop_id /* out */,
		     uint64_t *value /* out */,
		     drmModePropertyPtr *prop /* out */)
1185
{
1186 1187 1188 1189
	drmModeObjectPropertiesPtr proplist;
	drmModePropertyPtr _prop;
	bool found = false;
	int i;
1190

1191 1192 1193 1194 1195
	proplist = drmModeObjectGetProperties(drm_fd, object_id, object_type);
	for (i = 0; i < proplist->count_props; i++) {
		_prop = drmModeGetProperty(drm_fd, proplist->props[i]);
		if (!_prop)
			continue;
1196

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
		if (strcmp(_prop->name, name) == 0) {
			found = true;
			if (prop_id)
				*prop_id = proplist->props[i];
			if (value)
				*value = proplist->prop_values[i];
			if (prop)
				*prop = _prop;
			else
				drmModeFreeProperty(_prop);
1207

1208 1209 1210 1211
			break;
		}
		drmModeFreeProperty(_prop);
	}
1212

1213 1214
	drmModeFreeObjectProperties(proplist);
	return found;
1215 1216
}

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
struct edid_block {
    int pos;
    unsigned char *data;
};

#define DTD_SUPPORTS_AUDIO 1<<6

static struct edid_block
init_cea_block(const unsigned char *edid, size_t length,
	       unsigned char *new_edid_ptr[], size_t *new_length,
	       char extra_extensions_length,
	       uint32_t dtd_support)
{
	struct edid_block new_edid;
	int n_extensions;
	int pos;
	static const char cea_header_len = 4, video_block_len = 6;

	igt_assert(new_edid_ptr != NULL && new_length != NULL);

	*new_length = length + 128;

1239
	new_edid.data = calloc(*new_length, sizeof(*new_edid.data));
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	igt_assert_f(new_edid.data, "Failed to allocate %zu bytes for edid\n", sizeof(new_length));
	memcpy(new_edid.data, edid, length);
	*new_edid_ptr = new_edid.data;

	n_extensions = new_edid.data[126];
	n_extensions++;
	new_edid.data[126] = n_extensions;

	update_edid_csum(new_edid.data, 0);

	/* add a cea-861 extension block */
	pos = length;
	new_edid.data[pos++] = 0x2;
	new_edid.data[pos++] = 0x3;
	new_edid.data[pos++] = cea_header_len + video_block_len +
		extra_extensions_length;
	new_edid.data[pos++] = dtd_support;

	/* video block (id | length) */
	new_edid.data[pos++] = 2 << 5 | (video_block_len - 1);
	new_edid.data[pos++] = 32 | 0x80; /* 1080p @ 24Hz | (native)*/
	new_edid.data[pos++] = 5;         /* 1080i @ 60Hz */
	new_edid.data[pos++] = 20;        /* 1080i @ 50Hz */
	new_edid.data[pos++] = 4;         /* 720p @ 60Hz*/
	new_edid.data[pos++] = 19;        /* 720p @ 50Hz*/
	new_edid.pos = pos;

	return new_edid;
}

Thomas Wood's avatar
Thomas Wood committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
/**
 * kmstest_edid_add_3d:
 * @edid: an existing valid edid block
 * @length: length of @edid
 * @new_edid_ptr: pointer to where the new edid will be placed
 * @new_length: pointer to the size of the new edid
 *
 * Makes a copy of an existing edid block and adds an extension indicating
 * stereo 3D capabilities.
 */
void kmstest_edid_add_3d(const unsigned char *edid, size_t length,
			 unsigned char *new_edid_ptr[], size_t *new_length)
{
1283 1284 1285 1286 1287
	char vsdb_block_len = 11;
	struct edid_block new_edid = init_cea_block(edid, length, new_edid_ptr,
						    new_length, vsdb_block_len,
						    0);
	int pos = new_edid.pos;
Thomas Wood's avatar
Thomas Wood committed
1288

1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
	/* vsdb block ( id | length ) */
	new_edid.data[pos++] = 3 << 5 | (vsdb_block_len - 1);
	/* registration id */
	new_edid.data[pos++] = 0x3;
	new_edid.data[pos++] = 0xc;
	new_edid.data[pos++] = 0x0;
	/* source physical address */
	new_edid.data[pos++] = 0x10;
	new_edid.data[pos++] = 0x00;
	/* Supports_AI ... etc */
	new_edid.data[pos++] = 0x00;
	/* Max TMDS Clock */
	new_edid.data[pos++] = 0x00;
	/* Latency present, HDMI Video Present */
	new_edid.data[pos++] = 0x20;
	/* HDMI Video */
	new_edid.data[pos++] = 0x80;
	new_edid.data[pos++] = 0x00;
Thomas Wood's avatar
Thomas Wood committed
1307

1308 1309
	update_edid_csum(new_edid.data, length);
}
Thomas Wood's avatar
Thomas Wood committed
1310

1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
/**
 * kmstest_edid_add_4k:
 * @edid: an existing valid edid block
 * @length: length of @edid
 * @new_edid_ptr: pointer to where the new edid will be placed
 * @new_length: pointer to the size of the new edid
 *
 * Makes a copy of an existing edid block and adds an extension indicating
 * a HDMI 4K mode in vsdb.
 */
void kmstest_edid_add_4k(const unsigned char *edid, size_t length,
			 unsigned char *new_edid_ptr[], size_t *new_length)
{
	char vsdb_block_len = 12;
	struct edid_block new_edid = init_cea_block(edid, length, new_edid_ptr,
						    new_length, vsdb_block_len,
						    0);
	int pos = new_edid.pos;
Thomas Wood's avatar
Thomas Wood committed
1329

1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
	/* vsdb block ( id | length ) */
	new_edid.data[pos++] = 3 << 5 | (vsdb_block_len - 1);
	/* registration id */
	new_edid.data[pos++] = 0x3;
	new_edid.data[pos++] = 0xc;
	new_edid.data[pos++] = 0x0;
	/* source physical address */
	new_edid.data[pos++] = 0x10;
	new_edid.data[pos++] = 0x00;
	/* Supports_AI ... etc */
	new_edid.data[pos++] = 0x00;
	/* Max TMDS Clock */
	new_edid.data[pos++] = 0x00;
	/* Latency present, HDMI Video Present */
	new_edid.data[pos++] = 0x20;
	/* HDMI Video */
	new_edid.data[pos++] = 0x00; /* 3D present */
Thomas Wood's avatar
Thomas Wood committed
1347

1348 1349 1350 1351
	/* HDMI MODE LEN -- how many entries */
	new_edid.data[pos++] = 0x20;
	/* 2160p, specified as short descriptor */
	new_edid.data[pos++] = 0x01;
Thomas Wood's avatar
Thomas Wood committed
1352

1353 1354
	update_edid_csum(new_edid.data, length);
}
Thomas Wood's avatar
Thomas Wood committed
1355

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
/**
 * kmstest_edid_add_audio:
 * @edid: an existing valid edid block
 * @length: length of @edid
 * @new_edid_ptr: pointer to where the new edid will be placed
 * @new_length: pointer to the size of the new edid
 *
 * Makes a copy of an existing edid block and adds an extension indicating
 * basic audio support and speaker data block.
 *
 */
void kmstest_edid_add_audio(const unsigned char *edid, size_t length,
			    unsigned char *new_edid_ptr[], size_t *new_length)
{
	char vsdb_block_len = 10, audio_block_len = 4, spkr_block_len = 4;
	struct edid_block new_edid = init_cea_block(edid, length, new_edid_ptr,
						    new_length,
						    vsdb_block_len +
						    audio_block_len +
						    spkr_block_len,
						    DTD_SUPPORTS_AUDIO);
	int pos = new_edid.pos;

	/* audio block, short audio block descriptors  */
	new_edid.data[pos++] = (1 << 5) | (audio_block_len - 1);
	new_edid.data[pos++] = 0x09; /* Audio Format, PCM */
	new_edid.data[pos++] = 0x07; /* Frequency, 32, 44.1, 48kHz  */
	new_edid.data[pos++] = 0x07; /* Bit Rate 16, 20, 24 bit */


	/* vsdb block ( id | length ) -- need vsdb as well
	 * otherwise the kernel will fallback to lower clock modes */
	new_edid.data[pos++] = 3 << 5 | (vsdb_block_len - 1);
Thomas Wood's avatar
Thomas Wood committed
1389
	/* registration id */
1390 1391 1392
	new_edid.data[pos++] = 0x3;
	new_edid.data[pos++] = 0xc;
	new_edid.data[pos++] = 0x0;
Thomas Wood's avatar
Thomas Wood committed
1393
	/* source physical address */
1394 1395
	new_edid.data[pos++] = 0x10;
	new_edid.data[pos++] = 0x00;
Thomas Wood's avatar
Thomas Wood committed
1396
	/* Supports_AI ... etc */
1397
	new_edid.data[pos++] = 0x00;
Thomas Wood's avatar
Thomas Wood committed
1398
	/* Max TMDS Clock */
1399
	new_edid.data[pos++] = 0x00;
Thomas Wood's avatar
Thomas Wood committed
1400
	/* Latency present, HDMI Video Present */
1401
	new_edid.data[pos++] = 0x20;
Thomas Wood's avatar
Thomas Wood committed
1402
	/* HDMI Video */
1403
	new_edid.data[pos++] = 0x00; /* 3D present */
Thomas Wood's avatar
Thomas Wood committed
1404

1405 1406 1407 1408 1409 1410 1411
	/* speaker data block */
	new_edid.data[pos++] = (4 << 5) | (spkr_block_len - 1);
	new_edid.data[pos++] = (1 << 5);
	new_edid.data[pos++] = 0x00;
	new_edid.data[pos++] = 0x00;

	update_edid_csum(new_edid.data, length);
Thomas Wood's avatar
Thomas Wood committed
1412 1413
}

1414 1415 1416 1417 1418 1419 1420 1421
/**
 * kmstest_unset_all_crtcs:
 * @drm_fd: the DRM fd
 * @resources: libdrm resources pointer
 *
 * Disables all the screens.
 */
void kmstest_unset_all_crtcs(int drm_fd, drmModeResPtr resources)
1422
{
1423 1424 1425
	int i, rc;

	for (i = 0; i < resources->count_crtcs; i++) {
1426
		rc = drmModeSetCrtc(drm_fd, resources->crtcs[i], 0, 0, 0, NULL,
1427 1428 1429
				    0, NULL);
		igt_assert(rc == 0);
	}
1430 1431
}

1432
/**
Daniel Vetter's avatar
Daniel Vetter committed
1433
 * kmstest_get_crtc_idx:
1434 1435 1436
 * @res: the libdrm resources
 * @crtc_id: the CRTC id
 *
Daniel Vetter's avatar
Daniel Vetter committed
1437 1438
 * Get the CRTC index based on its ID. This is useful since a few places of
 * libdrm deal with CRTC masks.
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
 */
int kmstest_get_crtc_idx(drmModeRes *res, uint32_t crtc_id)
{
	int i;

	for (i = 0; i < res->count_crtcs; i++)
		if (res->crtcs[i] == crtc_id)
			return i;

	igt_assert(false);
}

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
static inline uint32_t pipe_select(int pipe)
{
	if (pipe > 1)
		return pipe << DRM_VBLANK_HIGH_CRTC_SHIFT;
	else if (pipe > 0)
		return DRM_VBLANK_SECONDARY;
	else
		return 0;
}

1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
/**
 * kmstest_get_vblank:
 * @fd: Opened drm file descriptor
 * @pipe: Display pipe
 * @flags: Flags passed to drm_ioctl_wait_vblank
 *
 * Blocks or request a signal when a specified vblank event occurs
 *
 * Returns 0 on success or non-zero unsigned integer otherwise
 */
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
unsigned int kmstest_get_vblank(int fd, int pipe, unsigned int flags)
{
	union drm_wait_vblank vbl;

	memset(&vbl, 0, sizeof(vbl));
	vbl.request.type = DRM_VBLANK_RELATIVE | pipe_select(pipe) | flags;
	if (drmIoctl(fd, DRM_IOCTL_WAIT_VBLANK, &vbl))
		return 0;

	return vbl.reply.sequence;
}

1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
/**
 * kmstest_wait_for_pageflip:
 * @fd: Opened drm file descriptor
 *
 * Blocks until pageflip is completed
 *
 */
void kmstest_wait_for_pageflip(int fd)
{
	drmEventContext evctx = { .version = 2 };
	struct timeval timeout = { .tv_sec = 0, .tv_usec = 50000 };
	fd_set fds;
	int ret;

	/* Wait for pageflip completion, then consume event on fd */
	FD_ZERO(&fds);
	FD_SET(fd, &fds);
	do {
		ret = select(fd + 1, &fds, NULL, NULL, &timeout);
	} while (ret < 0 && errno == EINTR);
	igt_assert_eq(ret, 1);
	igt_assert(drmHandleEvent(fd, &evctx) == 0);
}

1507 1508 1509 1510 1511
static void get_plane(char *str, int type, struct kmstest_plane *plane)
{
	int ret;
	char buf[256];

1512
	plane->type = type;
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
	ret = sscanf(str + 12, "%d%*c %*s %[^n]s",
		     &plane->id,
		     buf);
	igt_assert_eq(ret, 2);

	ret = sscanf(buf + 9, "%4d%*c%4d%*c", &plane->pos_x, &plane->pos_y);
	igt_assert_eq(ret, 2);

	ret = sscanf(buf + 30, "%4d%*c%4d%*c", &plane->width, &plane->height);
	igt_assert_eq(ret, 2);
}

1525
static int parse_planes(FILE *fid, struct kmstest_plane *planes)
1526 1527
{
	char tmp[256];
1528
	int n_planes;
1529

1530
	n_planes = 0;
1531 1532
	while (fgets(tmp, 256, fid) != NULL) {
		if (strstr(tmp, "type=PRI") != NULL) {
1533 1534 1535 1536 1537
			if (planes) {
				get_plane(tmp, DRM_PLANE_TYPE_PRIMARY, &planes[n_planes]);
				planes[n_planes].index = n_planes;
			}
			n_planes++;
1538
		} else if (strstr(tmp, "type=OVL") != NULL) {
1539 1540 1541 1542 1543
			if (planes) {
				get_plane(tmp, DRM_PLANE_TYPE_OVERLAY, &planes[n_planes]);
				planes[n_planes].index = n_planes;
			}
			n_planes++;
1544
		} else if (strstr(tmp, "type=CUR") != NULL) {
1545 1546 1547 1548 1549
			if (planes) {
				get_plane(tmp, DRM_PLANE_TYPE_CURSOR, &planes[n_planes]);
				planes[n_planes].index = n_planes;
			}
			n_planes++;
1550 1551 1552 1553
			break;
		}
	}

1554
	return n_planes;
1555 1556 1557 1558 1559