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
|
/*
* Copyright © 2013 Intel Corporation
* Copyright © 2018 Collabora, Ltd.
*
* 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.
*
* Authors:
* Daniel Vetter <daniel.vetter@ffwll.ch>
* Daniel Stone <daniels@collabora.com>
*
*/
/**
* TEST: kms getfb
* Category: Display
* Description: Tests GETFB and GETFB2 ioctls.
* Driver requirement: i915, xe
* Mega feature: General Display Features
*/
#include "igt.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include "drm.h"
#include "drm_fourcc.h"
#include "i915/gem_create.h"
#include "igt_device.h"
#include "xe/xe_query.h"
#include "xe/xe_ioctl.h"
/**
* SUBTEST: getfb-handle-%s
* Description: Tests error handling %arg[1]
*
* arg[1]:
*
* @closed: when passing a handle that has been closed.
* @not-fb: when passing an invalid handle.
* @valid: when passing an valid handle.
* @zero: for a zero'd input.
*/
/**
* SUBTEST: getfb-reject-ccs
* Description: Tests error handling while requesting CCS buffers it should
* refuse because getfb supports returning a single buffer handle.
*
* SUBTEST: getfb-%s-different-handles
* Description: Tests error handling while requesting for two different handles
* from %arg[1].
*
* arg[1]:
*
* @addfb: same fd
* @repeated: different fd
*/
/**
* SUBTEST: getfb2-accept-ccs
* Description: Tests outputs are correct when retrieving a CCS framebuffer.
*
* SUBTEST: getfb2-into-addfb2
* Description: Output check by passing the output of GETFB2 into ADDFB2.
*
* SUBTEST: getfb2-handle-%s
* Description: Tests error handling %arg[1].
*
* arg[1]:
*
* @closed: when passing a handle that has been closed
* @not-fb: when passing an invalid handle
* @zero: for a zero'd input
*/
/**
* SUBTEST: %s-handle-protection
* Description: Make sure %arg[1] return handles if caller is non-root or non-master.
*
* arg[1]:
*
* @getfb: GETFB ioctl
* @getfb2: GETFB2 ioctl
*/
IGT_TEST_DESCRIPTION("Tests GETFB and GETFB2 ioctls.");
static bool has_getfb_iface(int fd)
{
struct drm_mode_fb_cmd arg = { };
int err;
err = 0;
if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &arg))
err = -errno;
switch (err) {
case -ENOTTY: /* ioctl unrecognised (kernel too old) */
case -ENOTSUP: /* driver doesn't support KMS */
return false;
default:
return true;
}
}
static bool has_addfb2_iface(int fd)
{
struct drm_mode_fb_cmd2 arg = { };
int err;
err = 0;
if (drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &arg))
err = -errno;
switch (err) {
case -ENOTTY: /* ioctl unrecognised (kernel too old) */
case -ENOTSUP: /* driver doesn't support KMS */
return false;
default:
return true;
}
}
static void get_ccs_fb(int fd, struct drm_mode_fb_cmd2 *ret)
{
struct drm_mode_fb_cmd2 add = {
.width = 1024,
.height = 1024,
.pixel_format = DRM_FORMAT_XRGB8888,
.flags = DRM_MODE_FB_MODIFIERS,
};
int size;
uint32_t devid;
igt_require(has_addfb2_iface(fd));
devid = intel_get_drm_devid(fd);
igt_require_f(intel_graphics_ver(devid) < IP_VER(20, 0),
"No ccs modifiers on Xe2\n");
if (HAS_FLATCCS(devid)) {
add.modifier[0] = I915_FORMAT_MOD_4_TILED_DG2_RC_CCS;
add.pitches[0] = ALIGN(add.width * 4, 4 * 512);
size = add.pitches[0] * ALIGN(add.height, 8);
size = ALIGN(size, 4096);
} else if ((intel_display_ver(devid)) >= 12) {
add.modifier[0] = IS_METEORLAKE(devid) ?
I915_FORMAT_MOD_4_TILED_MTL_RC_CCS :
I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS;
add.modifier[1] = IS_METEORLAKE(devid) ?
I915_FORMAT_MOD_4_TILED_MTL_RC_CCS :
I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS;
/* The main surface for Gen12+ is 4x4 tiles aligned
* For 32bpp the pitch is 4*4*32 bytes i.e. 512 bytes
*/
add.pitches[0] = ALIGN(add.width * 4, 4 * 128);
/* The main surface height is 4 tile rows aligned */
add.offsets[1] = add.pitches[0] * ALIGN(add.height, 128);
/* CCS surface pitch is 64 bytes aligned which corresponds to
* 4 tiles on the main surface
*/
add.pitches[1] = DIV_ROUND_UP(add.width, 128) * 64;
size = add.offsets[1];
/* CCS surface height is 4 tile rows aligned */
size += add.pitches[1] * DIV_ROUND_UP(add.height, 128) * 4;
/* GEM object is page aligned */
size = ALIGN(size, 4096);
} else {
add.modifier[0] = I915_FORMAT_MOD_Y_TILED_CCS;
add.modifier[1] = I915_FORMAT_MOD_Y_TILED_CCS;
/* An explanation of the magic numbers can be found in kms_ccs.c. */
add.pitches[0] = ALIGN(add.width * 4, 128);
add.offsets[1] = add.pitches[0] * ALIGN(add.height, 32);
add.pitches[1] = ALIGN(ALIGN(add.width * 4, 32) / 32, 128);
size = add.offsets[1];
size += add.pitches[1] * ALIGN(ALIGN(add.height, 16) / 16, 32);
}
if (is_i915_device(fd))
add.handles[0] = gem_buffer_create_fb_obj(fd, size);
else
add.handles[0] = xe_bo_create(fd, 0, size, vram_if_possible(fd, 0), 0);
igt_require(add.handles[0] != 0);
if (!HAS_FLATCCS(devid))
add.handles[1] = add.handles[0];
if (drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &add) == 0)
*ret = add;
else
gem_close(fd, add.handles[0]);
}
/**
* Find and return an arbitrary valid property ID.
*/
static uint32_t get_any_prop_id(int fd)
{
igt_display_t display;
igt_display_require(&display, fd);
for (int i = 0; i < display.n_outputs; i++) {
igt_output_t *output = &display.outputs[i];
if (output->props[IGT_CONNECTOR_DPMS] != 0)
return output->props[IGT_CONNECTOR_DPMS];
}
return 0;
}
static void test_handle_input(int fd)
{
struct drm_mode_fb_cmd2 add = {};
igt_fixture {
add.width = 1024;
add.height = 1024;
add.pixel_format = DRM_FORMAT_XRGB8888;
add.pitches[0] = 1024*4;
add.handles[0] = igt_create_bo_with_dimensions(fd, 1024, 1024,
DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR, 0, NULL, NULL, NULL);
igt_require(add.handles[0] != 0);
do_ioctl(fd, DRM_IOCTL_MODE_ADDFB2, &add);
}
igt_describe("Tests error handling for a zero'd input.");
igt_subtest("getfb-handle-zero") {
struct drm_mode_fb_cmd get = { .fb_id = 0 };
do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB, &get, ENOENT);
}
igt_describe("Tests error handling when passing an valid "
"handle.");
igt_subtest("getfb-handle-valid") {
struct drm_mode_fb_cmd get = { .fb_id = add.fb_id };
do_ioctl(fd, DRM_IOCTL_MODE_GETFB, &get);
igt_assert_neq_u32(get.handle, 0);
igt_assert_eq_u32(get.width, add.width);
igt_assert_eq_u32(get.height, add.height);
igt_assert_eq_u32(get.pitch, add.pitches[0]);
igt_assert_eq_u32(get.depth, 24);
igt_assert_eq_u32(get.bpp, 32);
gem_close(fd, get.handle);
}
igt_describe("Tests error handling when passing a handle that "
"has been closed.");
igt_subtest("getfb-handle-closed") {
struct drm_mode_fb_cmd get = { .fb_id = add.fb_id };
do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &add.fb_id);
do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB, &get, ENOENT);
}
igt_describe("Tests error handling when passing an invalid "
"handle.");
igt_subtest("getfb-handle-not-fb") {
struct drm_mode_fb_cmd get = { .fb_id = get_any_prop_id(fd) };
igt_require(get.fb_id > 0);
do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB, &get, ENOENT);
}
igt_fixture
gem_close(fd, add.handles[0]);
}
static void test_duplicate_handles(int fd)
{
struct drm_mode_fb_cmd2 add = {};
igt_fixture {
add.width = 1024;
add.height = 1024;
add.pixel_format = DRM_FORMAT_XRGB8888;
add.pitches[0] = 1024*4;
add.handles[0] = igt_create_bo_with_dimensions(fd, 1024, 1024,
DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR, 0, NULL, NULL, NULL);
igt_assert(add.handles[0]);
do_ioctl(fd, DRM_IOCTL_MODE_ADDFB2, &add);
}
igt_describe("Tests error handling while requesting for two different "
"handles from same fd.");
igt_subtest("getfb-addfb-different-handles") {
struct drm_mode_fb_cmd get = { .fb_id = add.fb_id };
do_ioctl(fd, DRM_IOCTL_MODE_GETFB, &get);
igt_assert_neq_u32(get.handle, add.handles[0]);
gem_close(fd, get.handle);
}
igt_describe("Tests error handling while requesting for two different "
"handles from different fd.");
igt_subtest("getfb-repeated-different-handles") {
struct drm_mode_fb_cmd get1 = { .fb_id = add.fb_id };
struct drm_mode_fb_cmd get2 = { .fb_id = add.fb_id };
do_ioctl(fd, DRM_IOCTL_MODE_GETFB, &get1);
do_ioctl(fd, DRM_IOCTL_MODE_GETFB, &get2);
igt_assert_neq_u32(get1.handle, get2.handle);
gem_close(fd, get1.handle);
gem_close(fd, get2.handle);
}
igt_describe("Tests error handling while requesting CCS buffers "
"it should refuse because getfb supports returning "
"a single buffer handle.");
igt_subtest("getfb-reject-ccs") {
struct drm_mode_fb_cmd2 add_ccs = { };
struct drm_mode_fb_cmd get = { };
igt_require_intel(fd);
igt_require_f(!HAS_FLATCCS(intel_get_drm_devid(fd)),
"skip because flat ccs has only one buffer.\n");
get_ccs_fb(fd, &add_ccs);
igt_require(add_ccs.handles[0] != 0);
get.fb_id = add_ccs.fb_id;
do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB, &get, EINVAL);
do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &add_ccs.fb_id);
gem_close(fd, add_ccs.handles[0]);
}
igt_fixture {
do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &add.fb_id);
gem_close(fd, add.handles[0]);
}
}
static void test_getfb2(int fd)
{
struct drm_mode_fb_cmd2 add_basic = {};
igt_fixture {
struct drm_mode_fb_cmd2 get = {};
add_basic.width = 1024;
add_basic.height = 1024;
add_basic.pixel_format = DRM_FORMAT_XRGB8888;
add_basic.pitches[0] = 1024*4;
add_basic.handles[0] = igt_create_bo_with_dimensions(fd, 1024, 1024,
DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR, 0, NULL, NULL, NULL);
igt_assert(add_basic.handles[0]);
do_ioctl(fd, DRM_IOCTL_MODE_ADDFB2, &add_basic);
get.fb_id = add_basic.fb_id;
do_ioctl(fd, DRM_IOCTL_MODE_GETFB2, &get);
igt_assert_neq_u32(get.handles[0], 0);
gem_close(fd, get.handles[0]);
}
igt_describe("Tests error handling for a zero'd input.");
igt_subtest("getfb2-handle-zero") {
struct drm_mode_fb_cmd2 get = {};
do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB2, &get, ENOENT);
}
igt_describe("Tests error handling when passing a handle that "
"has been closed.");
igt_subtest("getfb2-handle-closed") {
struct drm_mode_fb_cmd2 add = add_basic;
struct drm_mode_fb_cmd2 get = { };
do_ioctl(fd, DRM_IOCTL_MODE_ADDFB2, &add);
do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &add.fb_id);
get.fb_id = add.fb_id;
do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB2, &get, ENOENT);
}
igt_describe("Tests error handling when passing an invalid "
"handle.");
igt_subtest("getfb2-handle-not-fb") {
struct drm_mode_fb_cmd2 get = { .fb_id = get_any_prop_id(fd) };
igt_require(get.fb_id > 0);
do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB2, &get, ENOENT);
}
igt_describe("Tests outputs are correct when retrieving a "
"CCS framebuffer.");
igt_subtest("getfb2-accept-ccs") {
struct drm_mode_fb_cmd2 add_ccs = { };
struct drm_mode_fb_cmd2 get = { };
int i;
igt_require_intel(fd);
get_ccs_fb(fd, &add_ccs);
igt_require(add_ccs.fb_id != 0);
get.fb_id = add_ccs.fb_id;
do_ioctl(fd, DRM_IOCTL_MODE_GETFB2, &get);
igt_assert_eq_u32(get.width, add_ccs.width);
igt_assert_eq_u32(get.height, add_ccs.height);
igt_assert(get.flags & DRM_MODE_FB_MODIFIERS);
for (i = 0; i < ARRAY_SIZE(get.handles); i++) {
igt_assert_eq_u32(get.pitches[i], add_ccs.pitches[i]);
igt_assert_eq_u32(get.offsets[i], add_ccs.offsets[i]);
if (add_ccs.handles[i] != 0) {
igt_assert_neq_u32(get.handles[i], 0);
igt_assert_neq_u32(get.handles[i],
add_ccs.handles[i]);
igt_assert_eq_u64(get.modifier[i],
add_ccs.modifier[i]);
} else {
igt_assert_eq_u32(get.handles[i], 0);
igt_assert_eq_u64(get.modifier[i], 0);
}
}
if (!HAS_FLATCCS(intel_get_drm_devid(fd)))
igt_assert_eq_u32(get.handles[0], get.handles[1]);
do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &get.fb_id);
gem_close(fd, add_ccs.handles[0]);
gem_close(fd, get.handles[0]);
}
igt_describe("Output check by passing the output of GETFB2 "
"into ADDFB2.");
igt_subtest("getfb2-into-addfb2") {
struct drm_mode_fb_cmd2 cmd = { };
cmd.fb_id = add_basic.fb_id;
do_ioctl(fd, DRM_IOCTL_MODE_GETFB2, &cmd);
do_ioctl(fd, DRM_IOCTL_MODE_ADDFB2, &cmd);
do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &cmd.fb_id);
gem_close(fd, cmd.handles[0]);
}
igt_fixture {
do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &add_basic.fb_id);
gem_close(fd, add_basic.handles[0]);
}
}
static void test_handle_protection(void) {
int non_master_fd;
struct drm_mode_fb_cmd2 non_master_add = {};
igt_fixture {
non_master_fd = drm_open_driver(DRIVER_ANY);
non_master_add.width = 1024;
non_master_add.height = 1024;
non_master_add.pixel_format = DRM_FORMAT_XRGB8888;
non_master_add.pitches[0] = 1024*4;
non_master_add.handles[0] = igt_create_bo_with_dimensions(non_master_fd, 1024, 1024,
DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR, 0, NULL, NULL, NULL);
igt_require(non_master_add.handles[0] != 0);
do_ioctl(non_master_fd, DRM_IOCTL_MODE_ADDFB2, &non_master_add);
}
igt_describe("Make sure GETFB doesn't return handles if caller "
"is non-root or non-master.");
igt_subtest("getfb-handle-protection") {
struct drm_mode_fb_cmd get = { .fb_id = non_master_add.fb_id};
igt_fork(child, 1) {
igt_drop_root();
do_ioctl(non_master_fd, DRM_IOCTL_MODE_GETFB, &get);
/* ioctl succeeds but handle should be 0 */
igt_assert_eq_u32(get.handle, 0);
}
igt_waitchildren();
}
igt_describe("Make sure GETFB2 doesn't return handles if caller "
"is non-root or non-master.");
igt_subtest("getfb2-handle-protection") {
struct drm_mode_fb_cmd2 get = { .fb_id = non_master_add.fb_id};
int i;
igt_fork(child, 1) {
igt_drop_root();
do_ioctl(non_master_fd, DRM_IOCTL_MODE_GETFB2, &get);
/* ioctl succeeds but handles should be 0 */
for (i = 0; i < ARRAY_SIZE(get.handles); i++) {
igt_assert_eq_u32(get.handles[i], 0);
}
}
igt_waitchildren();
}
igt_fixture {
do_ioctl(non_master_fd, DRM_IOCTL_MODE_RMFB, &non_master_add.fb_id);
gem_close(non_master_fd, non_master_add.handles[0]);
drm_close_driver(non_master_fd);
}
}
igt_main
{
int fd;
igt_fixture {
fd = drm_open_driver_master(DRIVER_ANY);
igt_require(has_getfb_iface(fd));
}
igt_subtest_group
test_handle_input(fd);
igt_subtest_group
test_duplicate_handles(fd);
igt_subtest_group
test_getfb2(fd);
igt_subtest_group
test_handle_protection();
igt_fixture
drm_close_driver(fd);
}
|