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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 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 585 586 587 588 589 590 591 592 593 594
|
// SPDX-License-Identifier: MIT
/*
* Copyright © 2013 Intel Corporation
*/
#include <inttypes.h>
#include <sys/stat.h>
#include <sys/mount.h>
#ifdef __linux__
#include <sys/sysmacros.h>
#endif
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include "drmtest.h"
#include "igt_aux.h"
#include "igt_kms.h"
#include "igt_debugfs.h"
#include "igt_pipe_crc.h"
/**
* SECTION:igt_pipe_crc
* @short_description: Pipe CRC support
* @title: pipe_crc
* @include: igt_pipe_crc.h
*
* This library wraps up the kernel's support for capturing pipe CRCs into a
* neat and tidy package. For the detailed usage see all the functions which
* work on #igt_pipe_crc_t. This is supported on all platforms and outputs.
*
* Actually using pipe CRCs to write modeset tests is a bit tricky though, so
* there is no way to directly check a CRC: Both the details of the plane
* blending, color correction and other hardware and how exactly the CRC is
* computed at each tap point vary by hardware generation and are not disclosed.
*
* The only way to use #igt_crc_t CRCs therefore is to compare CRCs among each
* another either for equality or difference. Otherwise CRCs must be treated as
* completely opaque values. Note that not even CRCs from different pipes or tap
* points on the same platform can be compared. Hence only use
* igt_assert_crc_equal() to inspect CRC values captured by the same
* #igt_pipe_crc_t object.
*/
/**
* igt_find_crc_mismatch:
* @a: first pipe CRC value
* @b: second pipe CRC value
* @index: index of the first value that mismatched
*
* Check if CRC a and CRC b mismatch.
*
* Returns true if CRC values mismatch, false otherwise;
*/
bool igt_find_crc_mismatch(const igt_crc_t *a, const igt_crc_t *b, int *index)
{
int nwords = min(a->n_words, b->n_words);
int i;
for (i = 0; i < nwords; i++) {
if (a->crc[i] != b->crc[i]) {
if (index)
*index = i;
return true;
}
}
if (a->n_words != b->n_words) {
if (index)
*index = i;
return true;
}
return false;
}
/**
* igt_assert_crc_equal:
* @a: first pipe CRC value
* @b: second pipe CRC value
*
* Compares two CRC values and fails the testcase if they don't match with
* igt_fail(). Note that due to CRC collisions CRC based testcase can only
* assert that CRCs match, never that they are different. Otherwise there might
* be random testcase failures when different screen contents end up with the
* same CRC by chance.
*
* Passing --skip-crc-compare on the command line will force this function
* to always pass, which can be useful in interactive debugging where you
* might know the test will fail, but still want the test to keep going as if
* it had succeeded so that you can see the on-screen behavior.
*
*/
void igt_assert_crc_equal(const igt_crc_t *a, const igt_crc_t *b)
{
int index;
bool mismatch;
mismatch = igt_find_crc_mismatch(a, b, &index);
if (mismatch)
igt_debug("CRC mismatch%s at index %d: 0x%x != 0x%x\n",
igt_skip_crc_compare ? " (ignored)" : "",
index, a->crc[index], b->crc[index]);
igt_assert(!mismatch || igt_skip_crc_compare);
}
/**
* igt_check_crc_equal:
* @a: first pipe CRC value
* @b: second pipe CRC value
*
* Compares two CRC values and return whether they match.
*
* Returns: A boolean indicating whether the CRC values match
*/
bool igt_check_crc_equal(const igt_crc_t *a, const igt_crc_t *b)
{
int index;
bool mismatch;
mismatch = igt_find_crc_mismatch(a, b, &index);
if (mismatch)
igt_debug("CRC mismatch at index %d: 0x%x != 0x%x\n", index,
a->crc[index], b->crc[index]);
return !mismatch;
}
/**
* igt_crc_to_string_extended:
* @crc: pipe CRC value to print
* @delimiter: The delimiter to use between crc words
* @crc_size: the number of bytes to print per crc word (between 1 and 4)
*
* This function allocates a string and formats @crc into it, depending on
* @delimiter and @crc_size.
* The caller is responsible for freeing the string.
*
* This should only ever be used for diagnostic debug output.
*/
char *igt_crc_to_string_extended(igt_crc_t *crc, char delimiter, int crc_size)
{
int i;
int len = 0;
int field_width = 2 * crc_size; /* Two chars per byte. */
char *buf = malloc((field_width+1) * crc->n_words);
if (!buf)
return NULL;
for (i = 0; i < crc->n_words - 1; i++)
len += sprintf(buf + len, "%0*x%c", field_width,
crc->crc[i], delimiter);
sprintf(buf + len, "%0*x", field_width, crc->crc[i]);
return buf;
}
/**
* igt_crc_to_string:
* @crc: pipe CRC value to print
*
* This function allocates a string and formats @crc into it.
* The caller is responsible for freeing the string.
*
* This should only ever be used for diagnostic debug output.
*/
char *igt_crc_to_string(igt_crc_t *crc)
{
return igt_crc_to_string_extended(crc, ' ', 4);
}
#define MAX_CRC_ENTRIES 10
#define MAX_LINE_LEN (10 + 11 * MAX_CRC_ENTRIES + 1)
struct _igt_pipe_crc {
int fd;
int dir;
int ctl_fd;
int crc_fd;
int flags;
enum pipe pipe;
char *source;
};
/**
* igt_require_pipe_crc:
* @fd: fd of the device
*
* Convenience helper to check whether pipe CRC capturing is supported by the
* kernel. Uses igt_skip to automatically skip the test/subtest if this isn't
* the case.
*/
void igt_require_pipe_crc(int fd)
{
int dir;
struct stat stat;
dir = igt_debugfs_dir(fd);
igt_require_f(dir >= 0, "Could not open debugfs directory\n");
igt_require_f(fstatat(dir, "crtc-0/crc/control", &stat, 0) == 0,
"CRCs not supported on this platform\n");
close(dir);
}
static igt_pipe_crc_t *
pipe_crc_new(int fd, enum pipe pipe, const char *source, int flags)
{
igt_pipe_crc_t *pipe_crc;
char buf[128];
int debugfs;
const char *env_source;
igt_assert(source);
env_source = getenv("IGT_CRC_SOURCE");
if (!env_source)
env_source = source;
debugfs = igt_debugfs_dir(fd);
igt_assert(debugfs != -1);
pipe_crc = calloc(1, sizeof(struct _igt_pipe_crc));
igt_assert(pipe_crc);
sprintf(buf, "crtc-%d/crc/control", pipe);
pipe_crc->ctl_fd = openat(debugfs, buf, O_WRONLY);
igt_assert(pipe_crc->ctl_fd != -1);
pipe_crc->crc_fd = -1;
pipe_crc->fd = fd;
pipe_crc->dir = debugfs;
pipe_crc->pipe = pipe;
pipe_crc->source = strdup(env_source);
igt_assert(pipe_crc->source);
pipe_crc->flags = flags;
return pipe_crc;
}
/**
* igt_pipe_crc_new:
* @fd: fd of the device
* @pipe: display pipe to use as source
* @source: CRC tap point to use as source
*
* This sets up a new pipe CRC capture object for the given @pipe and @source
* in blocking mode.
*
* Returns: A pipe CRC object for the given @pipe and @source. The library
* assumes that the source is always available since recent kernels support at
* least IGT_PIPE_CRC_SOURCE_AUTO everywhere.
*/
igt_pipe_crc_t *
igt_pipe_crc_new(int fd, enum pipe pipe, const char *source)
{
return pipe_crc_new(fd, pipe, source, O_RDONLY);
}
/**
* igt_pipe_crc_new_nonblock:
* @fd: fd of the device
* @pipe: display pipe to use as source
* @source: CRC tap point to use as source
*
* This sets up a new pipe CRC capture object for the given @pipe and @source
* in nonblocking mode.
*
* Returns: A pipe CRC object for the given @pipe and @source. The library
* assumes that the source is always available since recent kernels support at
* least IGT_PIPE_CRC_SOURCE_AUTO everywhere.
*/
igt_pipe_crc_t *
igt_pipe_crc_new_nonblock(int fd, enum pipe pipe, const char *source)
{
return pipe_crc_new(fd, pipe, source, O_RDONLY | O_NONBLOCK);
}
/**
* igt_pipe_crc_free:
* @pipe_crc: pipe CRC object
*
* Frees all resources associated with @pipe_crc.
*/
void igt_pipe_crc_free(igt_pipe_crc_t *pipe_crc)
{
if (!pipe_crc)
return;
close(pipe_crc->ctl_fd);
close(pipe_crc->crc_fd);
close(pipe_crc->dir);
free(pipe_crc->source);
free(pipe_crc);
}
static bool pipe_crc_init_from_string(igt_pipe_crc_t *pipe_crc, igt_crc_t *crc,
const char *line)
{
int i;
const char *buf;
if (strncmp(line, "XXXXXXXXXX", 10) == 0)
crc->has_valid_frame = false;
else {
crc->has_valid_frame = true;
crc->frame = strtoul(line, NULL, 16);
}
buf = line + 10;
for (i = 0; *buf != '\n'; i++, buf += 11)
crc->crc[i] = strtoul(buf, NULL, 16);
crc->n_words = i;
return true;
}
static int read_crc(igt_pipe_crc_t *pipe_crc, igt_crc_t *out)
{
ssize_t bytes_read;
char buf[MAX_LINE_LEN + 1];
igt_set_timeout(5, "CRC reading");
bytes_read = read(pipe_crc->crc_fd, &buf, MAX_LINE_LEN);
igt_reset_timeout();
if (bytes_read < 0)
bytes_read = -errno;
else
buf[bytes_read] = '\0';
if (bytes_read > 0 && !pipe_crc_init_from_string(pipe_crc, out, buf))
return -EINVAL;
return bytes_read;
}
static void read_one_crc(igt_pipe_crc_t *pipe_crc, igt_crc_t *out)
{
int ret;
fcntl(pipe_crc->crc_fd, F_SETFL, pipe_crc->flags & ~O_NONBLOCK);
do {
ret = read_crc(pipe_crc, out);
} while (ret == -EINTR);
fcntl(pipe_crc->crc_fd, F_SETFL, pipe_crc->flags);
}
/**
* igt_pipe_crc_start:
* @pipe_crc: pipe CRC object
*
* Starts the CRC capture process on @pipe_crc.
*/
void igt_pipe_crc_start(igt_pipe_crc_t *pipe_crc)
{
const char *src = pipe_crc->source;
struct pollfd pfd;
char buf[32];
/* Stop first just to make sure we don't have lingering state left. */
igt_pipe_crc_stop(pipe_crc);
igt_reset_fifo_underrun_reporting(pipe_crc->fd);
igt_assert_eq(write(pipe_crc->ctl_fd, src, strlen(src)), strlen(src));
sprintf(buf, "crtc-%d/crc/data", pipe_crc->pipe);
igt_set_timeout(10, "Opening crc fd, and poll for first CRC.");
pipe_crc->crc_fd = openat(pipe_crc->dir, buf, pipe_crc->flags);
igt_assert(pipe_crc->crc_fd != -1);
pfd.fd = pipe_crc->crc_fd;
pfd.events = POLLIN;
poll(&pfd, 1, -1);
igt_reset_timeout();
errno = 0;
}
/**
* igt_pipe_crc_stop:
* @pipe_crc: pipe CRC object
*
* Stops the CRC capture process on @pipe_crc.
*/
void igt_pipe_crc_stop(igt_pipe_crc_t *pipe_crc)
{
close(pipe_crc->crc_fd);
pipe_crc->crc_fd = -1;
}
/**
* igt_pipe_crc_get_crcs:
* @pipe_crc: pipe CRC object
* @n_crcs: number of CRCs to capture
* @out_crcs: buffer pointer for the captured CRC values
*
* Read up to @n_crcs from @pipe_crc. This function does not block, and will
* return early if not enough CRCs can be captured, if @pipe_crc has been
* opened using igt_pipe_crc_new_nonblock(). It will block until @n_crcs are
* retrieved if @pipe_crc has been opened using igt_pipe_crc_new(). @out_crcs is
* alloced by this function and must be released with free() by the caller.
*
* Callers must start and stop the capturing themselves by calling
* igt_pipe_crc_start() and igt_pipe_crc_stop(). For one-shot CRC collecting
* look at igt_pipe_crc_collect_crc().
*
* Returns:
* The number of CRCs captured. Should be equal to @n_crcs in blocking mode, but
* can be less (even zero) in non-blocking mode.
*/
int
igt_pipe_crc_get_crcs(igt_pipe_crc_t *pipe_crc, int n_crcs,
igt_crc_t **out_crcs)
{
igt_crc_t *crcs;
int n = 0;
crcs = calloc(n_crcs, sizeof(igt_crc_t));
do {
igt_crc_t *crc = &crcs[n];
int ret;
ret = read_crc(pipe_crc, crc);
if (ret == -EAGAIN)
break;
if (ret < 0)
continue;
n++;
} while (n < n_crcs);
*out_crcs = crcs;
return n;
}
static void crc_sanity_checks(igt_pipe_crc_t *pipe_crc, igt_crc_t *crc)
{
int i;
bool all_zero = true;
/* Any CRC value can be considered valid on amdgpu hardware. */
if (is_amdgpu_device(pipe_crc->fd))
return;
for (i = 0; i < crc->n_words; i++) {
igt_warn_on_f(crc->crc[i] == 0xffffffff,
"Suspicious CRC: it looks like the CRC "
"read back was from a register in a powered "
"down well\n");
if (crc->crc[i])
all_zero = false;
}
igt_warn_on_f(all_zero, "Suspicious CRC: All values are 0.\n");
}
/**
* igt_pipe_crc_drain:
* @pipe_crc: pipe CRC object
*
* Discards all currently queued CRC values from @pipe_crc. This function does
* not block, and is useful to flush @pipe_crc. Afterwards you can get a fresh
* CRC with igt_pipe_crc_get_single().
*/
void igt_pipe_crc_drain(igt_pipe_crc_t *pipe_crc)
{
int ret;
igt_crc_t crc;
fcntl(pipe_crc->crc_fd, F_SETFL, pipe_crc->flags | O_NONBLOCK);
do {
ret = read_crc(pipe_crc, &crc);
} while (ret > 0 || ret == -EINVAL);
fcntl(pipe_crc->crc_fd, F_SETFL, pipe_crc->flags);
}
/**
* igt_pipe_crc_get_single:
* @pipe_crc: pipe CRC object
* @crc: buffer pointer for the captured CRC value
*
* Read a single @crc from @pipe_crc. This function blocks even
* when nonblocking CRC is requested.
*
* Callers must start and stop the capturing themselves by calling
* igt_pipe_crc_start() and igt_pipe_crc_stop(). For one-shot CRC collecting
* look at igt_pipe_crc_collect_crc().
*
* If capturing has been going on for a while and a fresh crc is required,
* you should use igt_pipe_crc_get_current() instead.
*/
void igt_pipe_crc_get_single(igt_pipe_crc_t *pipe_crc, igt_crc_t *crc)
{
read_one_crc(pipe_crc, crc);
crc_sanity_checks(pipe_crc, crc);
}
/**
* igt_pipe_crc_get_current:
* @drm_fd: drm device fd for vblank counter
* @pipe_crc: pipe CRC object
* @vblank: frame counter value we're looking for
* @crc: buffer pointer for the captured CRC value
*
* Same as igt_pipe_crc_get_single(), but will wait until a CRC has been captured
* for frame @vblank.
*/
void
igt_pipe_crc_get_for_frame(int drm_fd, igt_pipe_crc_t *pipe_crc,
unsigned int vblank, igt_crc_t *crc)
{
do {
read_one_crc(pipe_crc, crc);
/* Only works with valid frame counter */
if (!crc->has_valid_frame) {
igt_pipe_crc_drain(pipe_crc);
igt_pipe_crc_get_single(pipe_crc, crc);
return;
}
} while (igt_vblank_before(crc->frame, vblank));
crc_sanity_checks(pipe_crc, crc);
}
/**
* igt_pipe_crc_get_current:
* @drm_fd: Pointer to drm fd for vblank counter
* @pipe_crc: pipe CRC object
* @crc: buffer pointer for the captured CRC value
*
* Same as igt_pipe_crc_get_single(), but will wait until a new CRC can be captured.
* This is useful for retrieving the current CRC in a more race free way than
* igt_pipe_crc_drain() + igt_pipe_crc_get_single().
*/
void
igt_pipe_crc_get_current(int drm_fd, igt_pipe_crc_t *pipe_crc, igt_crc_t *crc)
{
unsigned vblank = kmstest_get_vblank(drm_fd, pipe_crc->pipe, 0) + 1;
return igt_pipe_crc_get_for_frame(drm_fd, pipe_crc, vblank, crc);
}
/**
* igt_pipe_crc_collect_crc:
* @pipe_crc: pipe CRC object
* @out_crc: buffer for the captured CRC values
*
* Read a single CRC from @pipe_crc. This function blocks until the CRC is
* retrieved, irrespective of whether @pipe_crc has been opened with
* igt_pipe_crc_new() or igt_pipe_crc_new_nonblock(). @out_crc must be
* allocated by the caller.
*
* This function takes care of the pipe_crc book-keeping, it will start/stop
* the collection of the CRC.
*
* This function also calls the interactive debug with the "crc" domain, so you
* can make use of this feature to actually see the screen that is being CRC'd.
*
* For continuous CRC collection look at igt_pipe_crc_start(),
* igt_pipe_crc_get_crcs() and igt_pipe_crc_stop().
*/
void igt_pipe_crc_collect_crc(igt_pipe_crc_t *pipe_crc, igt_crc_t *out_crc)
{
igt_debug_wait_for_keypress("crc");
igt_pipe_crc_start(pipe_crc);
igt_pipe_crc_get_single(pipe_crc, out_crc);
igt_pipe_crc_stop(pipe_crc);
}
|