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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
|
// SPDX-License-Identifier: GPL-2.0
/*
* Test cases for the drm_framebuffer functions
*
* Copyright (c) 2022 MaĆra Canal <mairacanal@riseup.net>
*/
#include <kunit/device.h>
#include <kunit/test.h>
#include <drm/drm_device.h>
#include <drm/drm_drv.h>
#include <drm/drm_mode.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_kunit_helpers.h>
#include <drm/drm_print.h>
#include "../drm_crtc_internal.h"
#define MIN_WIDTH 4
#define MAX_WIDTH 4096
#define MIN_HEIGHT 4
#define MAX_HEIGHT 4096
#define DRM_MODE_FB_INVALID BIT(2)
struct drm_framebuffer_test {
int buffer_created;
struct drm_mode_fb_cmd2 cmd;
const char *name;
};
static const struct drm_framebuffer_test drm_framebuffer_create_cases[] = {
{ .buffer_created = 1, .name = "ABGR8888 normal sizes",
.cmd = { .width = 600, .height = 600, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .pitches = { 4 * 600, 0, 0 },
}
},
{ .buffer_created = 1, .name = "ABGR8888 max sizes",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .pitches = { 4 * MAX_WIDTH, 0, 0 },
}
},
{ .buffer_created = 1, .name = "ABGR8888 pitch greater than min required",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .pitches = { 4 * MAX_WIDTH + 1, 0, 0 },
}
},
{ .buffer_created = 0, .name = "ABGR8888 pitch less than min required",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .pitches = { 4 * MAX_WIDTH - 1, 0, 0 },
}
},
{ .buffer_created = 0, .name = "ABGR8888 Invalid width",
.cmd = { .width = MAX_WIDTH + 1, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .pitches = { 4 * (MAX_WIDTH + 1), 0, 0 },
}
},
{ .buffer_created = 0, .name = "ABGR8888 Invalid buffer handle",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 0, 0, 0 }, .pitches = { 4 * MAX_WIDTH, 0, 0 },
}
},
{ .buffer_created = 0, .name = "No pixel format",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = 0,
.handles = { 1, 0, 0 }, .pitches = { 4 * MAX_WIDTH, 0, 0 },
}
},
{ .buffer_created = 0, .name = "ABGR8888 Width 0",
.cmd = { .width = 0, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .pitches = { 4 * MAX_WIDTH, 0, 0 },
}
},
{ .buffer_created = 0, .name = "ABGR8888 Height 0",
.cmd = { .width = MAX_WIDTH, .height = 0, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .pitches = { 4 * MAX_WIDTH, 0, 0 },
}
},
{ .buffer_created = 0, .name = "ABGR8888 Out of bound height * pitch combination",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .offsets = { UINT_MAX - 1, 0, 0 },
.pitches = { 4 * MAX_WIDTH, 0, 0 },
}
},
{ .buffer_created = 1, .name = "ABGR8888 Large buffer offset",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .offsets = { UINT_MAX / 2, 0, 0 },
.pitches = { 4 * MAX_WIDTH, 0, 0 },
}
},
/*
* All entries in members that represents per-plane values (@modifier, @handles,
* @pitches and @offsets) must be zero when unused.
*/
{ .buffer_created = 0, .name = "ABGR8888 Buffer offset for inexistent plane",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .offsets = { UINT_MAX / 2, UINT_MAX / 2, 0 },
.pitches = { 4 * MAX_WIDTH, 0, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
}
},
{ .buffer_created = 0, .name = "ABGR8888 Invalid flag",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .offsets = { UINT_MAX / 2, 0, 0 },
.pitches = { 4 * MAX_WIDTH, 0, 0 }, .flags = DRM_MODE_FB_INVALID,
}
},
{ .buffer_created = 1, .name = "ABGR8888 Set DRM_MODE_FB_MODIFIERS without modifiers",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .offsets = { UINT_MAX / 2, 0, 0 },
.pitches = { 4 * MAX_WIDTH, 0, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
}
},
{ .buffer_created = 1, .name = "ABGR8888 Valid buffer modifier",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .offsets = { UINT_MAX / 2, 0, 0 },
.pitches = { 4 * MAX_WIDTH, 0, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { AFBC_FORMAT_MOD_YTR, 0, 0 },
}
},
{ .buffer_created = 0,
.name = "ABGR8888 Invalid buffer modifier(DRM_FORMAT_MOD_SAMSUNG_64_32_TILE)",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .offsets = { UINT_MAX / 2, 0, 0 },
.pitches = { 4 * MAX_WIDTH, 0, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { DRM_FORMAT_MOD_SAMSUNG_64_32_TILE, 0, 0 },
}
},
{ .buffer_created = 1, .name = "ABGR8888 Extra pitches without DRM_MODE_FB_MODIFIERS",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .offsets = { UINT_MAX / 2, 0, 0 },
.pitches = { 4 * MAX_WIDTH, 4 * MAX_WIDTH, 0 },
}
},
{ .buffer_created = 0, .name = "ABGR8888 Extra pitches with DRM_MODE_FB_MODIFIERS",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_ABGR8888,
.handles = { 1, 0, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
.pitches = { 4 * MAX_WIDTH, 4 * MAX_WIDTH, 0 },
}
},
{ .buffer_created = 1, .name = "NV12 Normal sizes",
.cmd = { .width = 600, .height = 600, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 0 }, .pitches = { 600, 600, 0 },
}
},
{ .buffer_created = 1, .name = "NV12 Max sizes",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 0 }, .pitches = { MAX_WIDTH, MAX_WIDTH, 0 },
}
},
{ .buffer_created = 0, .name = "NV12 Invalid pitch",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 0 }, .pitches = { MAX_WIDTH, MAX_WIDTH - 1, 0 },
}
},
{ .buffer_created = 0, .name = "NV12 Invalid modifier/missing DRM_MODE_FB_MODIFIERS flag",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 0 }, .modifier = { DRM_FORMAT_MOD_SAMSUNG_64_32_TILE, 0, 0 },
.pitches = { MAX_WIDTH, MAX_WIDTH, 0 },
}
},
{ .buffer_created = 0, .name = "NV12 different modifier per-plane",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { DRM_FORMAT_MOD_SAMSUNG_64_32_TILE, 0, 0 },
.pitches = { MAX_WIDTH, MAX_WIDTH, 0 },
}
},
{ .buffer_created = 1, .name = "NV12 with DRM_FORMAT_MOD_SAMSUNG_64_32_TILE",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { DRM_FORMAT_MOD_SAMSUNG_64_32_TILE,
DRM_FORMAT_MOD_SAMSUNG_64_32_TILE, 0 },
.pitches = { MAX_WIDTH, MAX_WIDTH, 0 },
}
},
{ .buffer_created = 0, .name = "NV12 Valid modifiers without DRM_MODE_FB_MODIFIERS",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 0 }, .modifier = { DRM_FORMAT_MOD_SAMSUNG_64_32_TILE,
DRM_FORMAT_MOD_SAMSUNG_64_32_TILE, 0 },
.pitches = { MAX_WIDTH, MAX_WIDTH, 0 },
}
},
{ .buffer_created = 0, .name = "NV12 Modifier for inexistent plane",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { DRM_FORMAT_MOD_SAMSUNG_64_32_TILE, DRM_FORMAT_MOD_SAMSUNG_64_32_TILE,
DRM_FORMAT_MOD_SAMSUNG_64_32_TILE },
.pitches = { MAX_WIDTH, MAX_WIDTH, 0 },
}
},
{ .buffer_created = 0, .name = "NV12 Handle for inexistent plane",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 1 }, .flags = DRM_MODE_FB_MODIFIERS,
.pitches = { MAX_WIDTH, MAX_WIDTH, 0 },
}
},
{ .buffer_created = 1, .name = "NV12 Handle for inexistent plane without DRM_MODE_FB_MODIFIERS",
.cmd = { .width = 600, .height = 600, .pixel_format = DRM_FORMAT_NV12,
.handles = { 1, 1, 1 }, .pitches = { 600, 600, 600 },
}
},
{ .buffer_created = 1, .name = "YVU420 DRM_MODE_FB_MODIFIERS set without modifier",
.cmd = { .width = 600, .height = 600, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .flags = DRM_MODE_FB_MODIFIERS,
.pitches = { 600, 300, 300 },
}
},
{ .buffer_created = 1, .name = "YVU420 Normal sizes",
.cmd = { .width = 600, .height = 600, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .pitches = { 600, 300, 300 },
}
},
{ .buffer_created = 1, .name = "YVU420 Max sizes",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2),
DIV_ROUND_UP(MAX_WIDTH, 2) },
}
},
{ .buffer_created = 0, .name = "YVU420 Invalid pitch",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2) - 1,
DIV_ROUND_UP(MAX_WIDTH, 2) },
}
},
{ .buffer_created = 1, .name = "YVU420 Different pitches",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2) + 1,
DIV_ROUND_UP(MAX_WIDTH, 2) + 7 },
}
},
{ .buffer_created = 1, .name = "YVU420 Different buffer offsets/pitches",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .offsets = { MAX_WIDTH, MAX_WIDTH +
MAX_WIDTH * MAX_HEIGHT, MAX_WIDTH + 2 * MAX_WIDTH * MAX_HEIGHT },
.pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2) + 1,
DIV_ROUND_UP(MAX_WIDTH, 2) + 7 },
}
},
{ .buffer_created = 0,
.name = "YVU420 Modifier set just for plane 0, without DRM_MODE_FB_MODIFIERS",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .modifier = { AFBC_FORMAT_MOD_SPARSE, 0, 0 },
.pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2), DIV_ROUND_UP(MAX_WIDTH, 2) },
}
},
{ .buffer_created = 0,
.name = "YVU420 Modifier set just for planes 0, 1, without DRM_MODE_FB_MODIFIERS",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 },
.modifier = { AFBC_FORMAT_MOD_SPARSE, AFBC_FORMAT_MOD_SPARSE, 0 },
.pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2), DIV_ROUND_UP(MAX_WIDTH, 2) },
}
},
{ .buffer_created = 0,
.name = "YVU420 Modifier set just for plane 0, 1, with DRM_MODE_FB_MODIFIERS",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { AFBC_FORMAT_MOD_SPARSE, AFBC_FORMAT_MOD_SPARSE, 0 },
.pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2), DIV_ROUND_UP(MAX_WIDTH, 2) },
}
},
{ .buffer_created = 1, .name = "YVU420 Valid modifier",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { AFBC_FORMAT_MOD_SPARSE, AFBC_FORMAT_MOD_SPARSE,
AFBC_FORMAT_MOD_SPARSE },
.pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2), DIV_ROUND_UP(MAX_WIDTH, 2) },
}
},
{ .buffer_created = 0, .name = "YVU420 Different modifiers per plane",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { AFBC_FORMAT_MOD_SPARSE, AFBC_FORMAT_MOD_SPARSE | AFBC_FORMAT_MOD_YTR,
AFBC_FORMAT_MOD_SPARSE },
.pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2), DIV_ROUND_UP(MAX_WIDTH, 2) },
}
},
{ .buffer_created = 0, .name = "YVU420 Modifier for inexistent plane",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YVU420,
.handles = { 1, 1, 1 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { AFBC_FORMAT_MOD_SPARSE, AFBC_FORMAT_MOD_SPARSE,
AFBC_FORMAT_MOD_SPARSE, AFBC_FORMAT_MOD_SPARSE },
.pitches = { MAX_WIDTH, DIV_ROUND_UP(MAX_WIDTH, 2), DIV_ROUND_UP(MAX_WIDTH, 2) },
}
},
{ .buffer_created = 0, .name = "YUV420_10BIT Invalid modifier(DRM_FORMAT_MOD_LINEAR)",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_YUV420_10BIT,
.handles = { 1, 0, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
.modifier = { DRM_FORMAT_MOD_LINEAR, 0, 0 },
.pitches = { MAX_WIDTH, 0, 0 },
}
},
{ .buffer_created = 1, .name = "X0L2 Normal sizes",
.cmd = { .width = 600, .height = 600, .pixel_format = DRM_FORMAT_X0L2,
.handles = { 1, 0, 0 }, .pitches = { 1200, 0, 0 }
}
},
{ .buffer_created = 1, .name = "X0L2 Max sizes",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_X0L2,
.handles = { 1, 0, 0 }, .pitches = { 2 * MAX_WIDTH, 0, 0 }
}
},
{ .buffer_created = 0, .name = "X0L2 Invalid pitch",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_X0L2,
.handles = { 1, 0, 0 }, .pitches = { 2 * MAX_WIDTH - 1, 0, 0 }
}
},
{ .buffer_created = 1, .name = "X0L2 Pitch greater than minimum required",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_X0L2,
.handles = { 1, 0, 0 }, .pitches = { 2 * MAX_WIDTH + 1, 0, 0 }
}
},
{ .buffer_created = 0, .name = "X0L2 Handle for inexistent plane",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_X0L2,
.handles = { 1, 1, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
.pitches = { 2 * MAX_WIDTH + 1, 0, 0 }
}
},
{ .buffer_created = 1,
.name = "X0L2 Offset for inexistent plane, without DRM_MODE_FB_MODIFIERS set",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_X0L2,
.handles = { 1, 0, 0 }, .offsets = { 0, 0, 3 },
.pitches = { 2 * MAX_WIDTH + 1, 0, 0 }
}
},
{ .buffer_created = 0, .name = "X0L2 Modifier without DRM_MODE_FB_MODIFIERS set",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_X0L2,
.handles = { 1, 0, 0 }, .pitches = { 2 * MAX_WIDTH + 1, 0, 0 },
.modifier = { AFBC_FORMAT_MOD_SPARSE, 0, 0 },
}
},
{ .buffer_created = 1, .name = "X0L2 Valid modifier",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT, .pixel_format = DRM_FORMAT_X0L2,
.handles = { 1, 0, 0 }, .pitches = { 2 * MAX_WIDTH + 1, 0, 0 },
.modifier = { AFBC_FORMAT_MOD_SPARSE, 0, 0 }, .flags = DRM_MODE_FB_MODIFIERS,
}
},
{ .buffer_created = 0, .name = "X0L2 Modifier for inexistent plane",
.cmd = { .width = MAX_WIDTH, .height = MAX_HEIGHT,
.pixel_format = DRM_FORMAT_X0L2, .handles = { 1, 0, 0 },
.pitches = { 2 * MAX_WIDTH + 1, 0, 0 },
.modifier = { AFBC_FORMAT_MOD_SPARSE, AFBC_FORMAT_MOD_SPARSE, 0 },
.flags = DRM_MODE_FB_MODIFIERS,
}
},
};
/*
* This struct is intended to provide a way to mocked functions communicate
* with the outer test when it can't be achieved by using its return value. In
* this way, the functions that receive the mocked drm_device, for example, can
* grab a reference to this and actually return something to be used on some
* expectation.
*/
struct drm_framebuffer_test_priv {
struct drm_device dev;
bool buffer_created;
bool buffer_freed;
};
static struct drm_framebuffer *fb_create_mock(struct drm_device *dev,
struct drm_file *file_priv,
const struct drm_mode_fb_cmd2 *mode_cmd)
{
struct drm_framebuffer_test_priv *priv = container_of(dev, typeof(*priv), dev);
priv->buffer_created = true;
return ERR_PTR(-EINVAL);
}
static struct drm_mode_config_funcs mock_config_funcs = {
.fb_create = fb_create_mock,
};
static int drm_framebuffer_test_init(struct kunit *test)
{
struct device *parent;
struct drm_framebuffer_test_priv *priv;
struct drm_device *dev;
parent = drm_kunit_helper_alloc_device(test);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
priv = drm_kunit_helper_alloc_drm_device(test, parent, typeof(*priv),
dev, 0);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
dev = &priv->dev;
dev->mode_config.min_width = MIN_WIDTH;
dev->mode_config.max_width = MAX_WIDTH;
dev->mode_config.min_height = MIN_HEIGHT;
dev->mode_config.max_height = MAX_HEIGHT;
dev->mode_config.funcs = &mock_config_funcs;
test->priv = priv;
return 0;
}
static void drm_test_framebuffer_create(struct kunit *test)
{
const struct drm_framebuffer_test *params = test->param_value;
struct drm_framebuffer_test_priv *priv = test->priv;
struct drm_device *dev = &priv->dev;
priv->buffer_created = false;
drm_internal_framebuffer_create(dev, ¶ms->cmd, NULL);
KUNIT_EXPECT_EQ(test, params->buffer_created, priv->buffer_created);
}
static void drm_framebuffer_test_to_desc(const struct drm_framebuffer_test *t, char *desc)
{
strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
}
KUNIT_ARRAY_PARAM(drm_framebuffer_create, drm_framebuffer_create_cases,
drm_framebuffer_test_to_desc);
/* Tries to create a framebuffer with modifiers without drm_device supporting it */
static void drm_test_framebuffer_modifiers_not_supported(struct kunit *test)
{
struct drm_framebuffer_test_priv *priv = test->priv;
struct drm_device *dev = &priv->dev;
struct drm_framebuffer *fb;
/* A valid cmd with modifier */
struct drm_mode_fb_cmd2 cmd = {
.width = MAX_WIDTH, .height = MAX_HEIGHT,
.pixel_format = DRM_FORMAT_ABGR8888, .handles = { 1, 0, 0 },
.offsets = { UINT_MAX / 2, 0, 0 }, .pitches = { 4 * MAX_WIDTH, 0, 0 },
.flags = DRM_MODE_FB_MODIFIERS,
};
priv->buffer_created = false;
dev->mode_config.fb_modifiers_not_supported = 1;
fb = drm_internal_framebuffer_create(dev, &cmd, NULL);
KUNIT_EXPECT_EQ(test, priv->buffer_created, false);
KUNIT_EXPECT_EQ(test, PTR_ERR(fb), -EINVAL);
}
/* Parameters for testing drm_framebuffer_check_src_coords function */
struct drm_framebuffer_check_src_coords_case {
const char *name;
const int expect;
const unsigned int fb_size;
const uint32_t src_x;
const uint32_t src_y;
/* Deltas to be applied on source */
const uint32_t dsrc_w;
const uint32_t dsrc_h;
};
static const struct drm_framebuffer_check_src_coords_case
drm_framebuffer_check_src_coords_cases[] = {
{ .name = "Success: source fits into fb",
.expect = 0,
},
{ .name = "Fail: overflowing fb with x-axis coordinate",
.expect = -ENOSPC, .src_x = 1, .fb_size = UINT_MAX,
},
{ .name = "Fail: overflowing fb with y-axis coordinate",
.expect = -ENOSPC, .src_y = 1, .fb_size = UINT_MAX,
},
{ .name = "Fail: overflowing fb with source width",
.expect = -ENOSPC, .dsrc_w = 1, .fb_size = UINT_MAX - 1,
},
{ .name = "Fail: overflowing fb with source height",
.expect = -ENOSPC, .dsrc_h = 1, .fb_size = UINT_MAX - 1,
},
};
static void drm_test_framebuffer_check_src_coords(struct kunit *test)
{
const struct drm_framebuffer_check_src_coords_case *params = test->param_value;
const uint32_t src_x = params->src_x;
const uint32_t src_y = params->src_y;
const uint32_t src_w = (params->fb_size << 16) + params->dsrc_w;
const uint32_t src_h = (params->fb_size << 16) + params->dsrc_h;
const struct drm_framebuffer fb = {
.width = params->fb_size,
.height = params->fb_size
};
int ret;
ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, &fb);
KUNIT_EXPECT_EQ(test, ret, params->expect);
}
static void
check_src_coords_test_to_desc(const struct drm_framebuffer_check_src_coords_case *t,
char *desc)
{
strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
}
KUNIT_ARRAY_PARAM(check_src_coords, drm_framebuffer_check_src_coords_cases,
check_src_coords_test_to_desc);
/*
* Test if drm_framebuffer_cleanup() really pops out the framebuffer object
* from device's fb_list and decrement the number of framebuffers for that
* device, which is the only things it does.
*/
static void drm_test_framebuffer_cleanup(struct kunit *test)
{
struct drm_framebuffer_test_priv *priv = test->priv;
struct drm_device *dev = &priv->dev;
struct list_head *fb_list = &dev->mode_config.fb_list;
struct drm_format_info format = { };
struct drm_framebuffer fb1 = { .dev = dev, .format = &format };
struct drm_framebuffer fb2 = { .dev = dev, .format = &format };
/* This will result on [fb_list] -> fb2 -> fb1 */
drm_framebuffer_init(dev, &fb1, NULL);
drm_framebuffer_init(dev, &fb2, NULL);
drm_framebuffer_cleanup(&fb1);
/* Now fb2 is the only one element on fb_list */
KUNIT_ASSERT_TRUE(test, list_is_singular(&fb2.head));
KUNIT_ASSERT_EQ(test, dev->mode_config.num_fb, 1);
drm_framebuffer_cleanup(&fb2);
/* Now fb_list is empty */
KUNIT_ASSERT_TRUE(test, list_empty(fb_list));
KUNIT_ASSERT_EQ(test, dev->mode_config.num_fb, 0);
}
/*
* Initialize a framebuffer, lookup its id and test if the returned reference
* matches.
*/
static void drm_test_framebuffer_lookup(struct kunit *test)
{
struct drm_framebuffer_test_priv *priv = test->priv;
struct drm_device *dev = &priv->dev;
struct drm_format_info format = { };
struct drm_framebuffer expected_fb = { .dev = dev, .format = &format };
struct drm_framebuffer *returned_fb;
uint32_t id = 0;
int ret;
ret = drm_framebuffer_init(dev, &expected_fb, NULL);
KUNIT_ASSERT_EQ(test, ret, 0);
id = expected_fb.base.id;
/* Looking for expected_fb */
returned_fb = drm_framebuffer_lookup(dev, NULL, id);
KUNIT_EXPECT_PTR_EQ(test, returned_fb, &expected_fb);
drm_framebuffer_put(returned_fb);
drm_framebuffer_cleanup(&expected_fb);
}
/* Try to lookup an id that is not linked to a framebuffer */
static void drm_test_framebuffer_lookup_inexistent(struct kunit *test)
{
struct drm_framebuffer_test_priv *priv = test->priv;
struct drm_device *dev = &priv->dev;
struct drm_framebuffer *fb;
uint32_t id = 0;
/* Looking for an inexistent framebuffer */
fb = drm_framebuffer_lookup(dev, NULL, id);
KUNIT_EXPECT_NULL(test, fb);
}
/*
* Test if drm_framebuffer_init initializes the framebuffer successfully,
* asserting that its modeset object struct and its refcount are correctly
* set and that strictly one framebuffer is initialized.
*/
static void drm_test_framebuffer_init(struct kunit *test)
{
struct drm_framebuffer_test_priv *priv = test->priv;
struct drm_device *dev = &priv->dev;
struct drm_format_info format = { };
struct drm_framebuffer fb1 = { .dev = dev, .format = &format };
struct drm_framebuffer_funcs funcs = { };
int ret;
ret = drm_framebuffer_init(dev, &fb1, &funcs);
KUNIT_ASSERT_EQ(test, ret, 0);
/* Check if fb->funcs is actually set to the drm_framebuffer_funcs passed on */
KUNIT_EXPECT_PTR_EQ(test, fb1.funcs, &funcs);
/* The fb->comm must be set to the current running process */
KUNIT_EXPECT_STREQ(test, fb1.comm, current->comm);
/* The fb->base must be successfully initialized */
KUNIT_EXPECT_NE(test, fb1.base.id, 0);
KUNIT_EXPECT_EQ(test, fb1.base.type, DRM_MODE_OBJECT_FB);
KUNIT_EXPECT_EQ(test, kref_read(&fb1.base.refcount), 1);
KUNIT_EXPECT_PTR_EQ(test, fb1.base.free_cb, &drm_framebuffer_free);
/* There must be just that one fb initialized */
KUNIT_EXPECT_EQ(test, dev->mode_config.num_fb, 1);
KUNIT_EXPECT_PTR_EQ(test, dev->mode_config.fb_list.prev, &fb1.head);
KUNIT_EXPECT_PTR_EQ(test, dev->mode_config.fb_list.next, &fb1.head);
drm_framebuffer_cleanup(&fb1);
}
/* Try to init a framebuffer without setting its format */
static void drm_test_framebuffer_init_bad_format(struct kunit *test)
{
struct drm_framebuffer_test_priv *priv = test->priv;
struct drm_device *dev = &priv->dev;
struct drm_framebuffer fb1 = { .dev = dev, .format = NULL };
struct drm_framebuffer_funcs funcs = { };
int ret;
/* Fails if fb.format isn't set */
ret = drm_framebuffer_init(dev, &fb1, &funcs);
KUNIT_EXPECT_EQ(test, ret, -EINVAL);
}
/*
* Test calling drm_framebuffer_init() passing a framebuffer linked to a
* different drm_device parent from the one passed on the first argument, which
* must fail.
*/
static void drm_test_framebuffer_init_dev_mismatch(struct kunit *test)
{
struct drm_framebuffer_test_priv *priv = test->priv;
struct drm_device *right_dev = &priv->dev;
struct drm_device *wrong_dev;
struct device *wrong_dev_parent;
struct drm_format_info format = { };
struct drm_framebuffer fb1 = { .dev = right_dev, .format = &format };
struct drm_framebuffer_funcs funcs = { };
int ret;
wrong_dev_parent = kunit_device_register(test, "drm-kunit-wrong-device-mock");
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, wrong_dev_parent);
wrong_dev = __drm_kunit_helper_alloc_drm_device(test, wrong_dev_parent,
sizeof(struct drm_device),
0, 0);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, wrong_dev);
/* Fails if fb->dev doesn't point to the drm_device passed on first arg */
ret = drm_framebuffer_init(wrong_dev, &fb1, &funcs);
KUNIT_EXPECT_EQ(test, ret, -EINVAL);
}
static void destroy_free_mock(struct drm_framebuffer *fb)
{
struct drm_framebuffer_test_priv *priv = container_of(fb->dev, typeof(*priv), dev);
priv->buffer_freed = true;
}
static struct drm_framebuffer_funcs framebuffer_funcs_free_mock = {
.destroy = destroy_free_mock,
};
/*
* In summary, the drm_framebuffer_free() function must implicitly call
* fb->funcs->destroy() and garantee that the framebufer object is unregistered
* from the drm_device idr pool.
*/
static void drm_test_framebuffer_free(struct kunit *test)
{
struct drm_framebuffer_test_priv *priv = test->priv;
struct drm_device *dev = &priv->dev;
struct drm_mode_object *obj;
struct drm_framebuffer fb = {
.dev = dev,
.funcs = &framebuffer_funcs_free_mock,
};
int id, ret;
priv->buffer_freed = false;
/*
* Mock a framebuffer that was not unregistered at the moment of the
* drm_framebuffer_free() call.
*/
ret = drm_mode_object_add(dev, &fb.base, DRM_MODE_OBJECT_FB);
KUNIT_ASSERT_EQ(test, ret, 0);
id = fb.base.id;
drm_framebuffer_free(&fb.base.refcount);
/* The framebuffer object must be unregistered */
obj = drm_mode_object_find(dev, NULL, id, DRM_MODE_OBJECT_FB);
KUNIT_EXPECT_PTR_EQ(test, obj, NULL);
KUNIT_EXPECT_EQ(test, fb.base.id, 0);
/* Test if fb->funcs->destroy() was called */
KUNIT_EXPECT_EQ(test, priv->buffer_freed, true);
}
static struct kunit_case drm_framebuffer_tests[] = {
KUNIT_CASE_PARAM(drm_test_framebuffer_check_src_coords, check_src_coords_gen_params),
KUNIT_CASE(drm_test_framebuffer_cleanup),
KUNIT_CASE_PARAM(drm_test_framebuffer_create, drm_framebuffer_create_gen_params),
KUNIT_CASE(drm_test_framebuffer_free),
KUNIT_CASE(drm_test_framebuffer_init),
KUNIT_CASE(drm_test_framebuffer_init_bad_format),
KUNIT_CASE(drm_test_framebuffer_init_dev_mismatch),
KUNIT_CASE(drm_test_framebuffer_lookup),
KUNIT_CASE(drm_test_framebuffer_lookup_inexistent),
KUNIT_CASE(drm_test_framebuffer_modifiers_not_supported),
{ }
};
static struct kunit_suite drm_framebuffer_test_suite = {
.name = "drm_framebuffer",
.init = drm_framebuffer_test_init,
.test_cases = drm_framebuffer_tests,
};
kunit_test_suite(drm_framebuffer_test_suite);
MODULE_DESCRIPTION("Test cases for the drm_framebuffer functions");
MODULE_LICENSE("GPL");
|