/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */ /* * Copyright (C) 2011 Texas Instruments, Inc * * 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: * Rob Clark */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include #include #include #include #include #include #include #include #include "omap_drm.h" #include "omap_drmif.h" #define __round_mask(x, y) ((__typeof__(x))((y)-1)) #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) #define PAGE_SIZE 4096 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER; static void * dev_table; struct omap_device { int fd; atomic_t refcnt; /* The handle_table is used to track GEM bo handles associated w/ * this fd. This is needed, in particular, when importing * dmabuf's because we don't want multiple 'struct omap_bo's * floating around with the same handle. Otherwise, when the * first one is omap_bo_del()'d the handle becomes no longer * valid, and the remaining 'struct omap_bo's are left pointing * to an invalid handle (and possible a GEM bo that is already * free'd). */ void *handle_table; }; /* a GEM buffer object allocated from the DRM device */ struct omap_bo { struct omap_device *dev; void *map; /* userspace mmap'ing (if there is one) */ uint32_t size; uint32_t handle; uint32_t name; /* flink global handle (DRI2 name) */ uint64_t offset; /* offset to mmap() */ int fd; /* dmabuf handle */ atomic_t refcnt; }; static struct omap_device * omap_device_new_impl(int fd) { struct omap_device *dev = calloc(sizeof(*dev), 1); if (!dev) return NULL; dev->fd = fd; atomic_set(&dev->refcnt, 1); dev->handle_table = drmHashCreate(); return dev; } struct omap_device * omap_device_new(int fd) { struct omap_device *dev = NULL; pthread_mutex_lock(&table_lock); if (!dev_table) dev_table = drmHashCreate(); if (drmHashLookup(dev_table, fd, (void **)&dev)) { /* not found, create new device */ dev = omap_device_new_impl(fd); drmHashInsert(dev_table, fd, dev); } else { /* found, just incr refcnt */ dev = omap_device_ref(dev); } pthread_mutex_unlock(&table_lock); return dev; } struct omap_device * omap_device_ref(struct omap_device *dev) { atomic_inc(&dev->refcnt); return dev; } void omap_device_del(struct omap_device *dev) { if (!atomic_dec_and_test(&dev->refcnt)) return; pthread_mutex_lock(&table_lock); drmHashDestroy(dev->handle_table); drmHashDelete(dev_table, dev->fd); pthread_mutex_unlock(&table_lock); free(dev); } int omap_get_param(struct omap_device *dev, uint64_t param, uint64_t *value) { struct drm_omap_param req = { .param = param, }; int ret; ret = drmCommandWriteRead(dev->fd, DRM_OMAP_GET_PARAM, &req, sizeof(req)); if (ret) { return ret; } *value = req.value; return 0; } int omap_set_param(struct omap_device *dev, uint64_t param, uint64_t value) { struct drm_omap_param req = { .param = param, .value = value, }; return drmCommandWrite(dev->fd, DRM_OMAP_SET_PARAM, &req, sizeof(req)); } /* lookup a buffer from it's handle, call w/ table_lock held: */ static struct omap_bo * lookup_bo(struct omap_device *dev, uint32_t handle) { struct omap_bo *bo = NULL; if (!drmHashLookup(dev->handle_table, handle, (void **)&bo)) { /* found, incr refcnt and return: */ bo = omap_bo_ref(bo); } return bo; } /* allocate a new buffer object, call w/ table_lock held */ static struct omap_bo * bo_from_handle(struct omap_device *dev, uint32_t handle) { struct omap_bo *bo = calloc(sizeof(*bo), 1); if (!bo) { struct drm_gem_close req = { .handle = handle, }; drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req); return NULL; } bo->dev = omap_device_ref(dev); bo->handle = handle; atomic_set(&bo->refcnt, 1); /* add ourselves to the handle table: */ drmHashInsert(dev->handle_table, handle, bo); return bo; } /* allocate a new buffer object */ static struct omap_bo * omap_bo_new_impl(struct omap_device *dev, union omap_gem_size size, uint32_t flags) { struct omap_bo *bo = NULL; struct drm_omap_gem_new req = { .size = size, .flags = flags, }; if (size.bytes == 0) { goto fail; } if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) { goto fail; } pthread_mutex_lock(&table_lock); bo = bo_from_handle(dev, req.handle); pthread_mutex_unlock(&table_lock); if (flags & OMAP_BO_TILED) { bo->size = round_up(size.tiled.width, PAGE_SIZE) * size.tiled.height; } else { bo->size = size.bytes; } return bo; fail: free(bo); return NULL; } /* allocate a new (un-tiled) buffer object */ struct omap_bo * omap_bo_new(struct omap_device *dev, uint32_t size, uint32_t flags) { union omap_gem_size gsize = { .bytes = size, }; if (flags & OMAP_BO_TILED) { return NULL; } return omap_bo_new_impl(dev, gsize, flags); } /* allocate a new buffer object */ struct omap_bo * omap_bo_new_tiled(struct omap_device *dev, uint32_t width, uint32_t height, uint32_t flags) { union omap_gem_size gsize = { .tiled = { .width = width, .height = height, }, }; if (!(flags & OMAP_BO_TILED)) { return NULL; } return omap_bo_new_impl(dev, gsize, flags); } struct omap_bo * omap_bo_ref(struct omap_bo *bo) { atomic_inc(&bo->refcnt); return bo; } /* get buffer info */ static int get_buffer_info(struct omap_bo *bo) { struct drm_omap_gem_info req = { .handle = bo->handle, }; int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO, &req, sizeof(req)); if (ret) { return ret; } /* really all we need for now is mmap offset */ bo->offset = req.offset; bo->size = req.size; return 0; } /* import a buffer object from DRI2 name */ struct omap_bo * omap_bo_from_name(struct omap_device *dev, uint32_t name) { struct omap_bo *bo = NULL; struct drm_gem_open req = { .name = name, }; pthread_mutex_lock(&table_lock); if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) { goto fail; } bo = lookup_bo(dev, req.handle); if (!bo) { bo = bo_from_handle(dev, req.handle); bo->name = name; } pthread_mutex_unlock(&table_lock); return bo; fail: pthread_mutex_unlock(&table_lock); free(bo); return NULL; } /* import a buffer from dmabuf fd, does not take ownership of the * fd so caller should close() the fd when it is otherwise done * with it (even if it is still using the 'struct omap_bo *') */ struct omap_bo * omap_bo_from_dmabuf(struct omap_device *dev, int fd) { struct omap_bo *bo = NULL; struct drm_prime_handle req = { .fd = fd, }; int ret; pthread_mutex_lock(&table_lock); ret = drmIoctl(dev->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &req); if (ret) { goto fail; } bo = lookup_bo(dev, req.handle); if (!bo) { bo = bo_from_handle(dev, req.handle); } pthread_mutex_unlock(&table_lock); return bo; fail: pthread_mutex_unlock(&table_lock); free(bo); return NULL; } /* destroy a buffer object */ void omap_bo_del(struct omap_bo *bo) { if (!bo) { return; } if (!atomic_dec_and_test(&bo->refcnt)) return; if (bo->map) { munmap(bo->map, bo->size); } if (bo->fd) { close(bo->fd); } if (bo->handle) { struct drm_gem_close req = { .handle = bo->handle, }; pthread_mutex_lock(&table_lock); drmHashDelete(bo->dev->handle_table, bo->handle); drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req); pthread_mutex_unlock(&table_lock); } omap_device_del(bo->dev); free(bo); } /* get the global flink/DRI2 buffer name */ int omap_bo_get_name(struct omap_bo *bo, uint32_t *name) { if (!bo->name) { struct drm_gem_flink req = { .handle = bo->handle, }; int ret; ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req); if (ret) { return ret; } bo->name = req.name; } *name = bo->name; return 0; } uint32_t omap_bo_handle(struct omap_bo *bo) { return bo->handle; } /* caller owns the dmabuf fd that is returned and is responsible * to close() it when done */ int omap_bo_dmabuf(struct omap_bo *bo) { if (!bo->fd) { struct drm_prime_handle req = { .handle = bo->handle, .flags = DRM_CLOEXEC, }; int ret; ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req); if (ret) { return ret; } bo->fd = req.fd; } return dup(bo->fd); } uint32_t omap_bo_size(struct omap_bo *bo) { if (!bo->size) { get_buffer_info(bo); } return bo->size; } void * omap_bo_map(struct omap_bo *bo) { if (!bo->map) { if (!bo->offset) { get_buffer_info(bo); } bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->dev->fd, bo->offset); if (bo->map == MAP_FAILED) { bo->map = NULL; } } return bo->map; } int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op) { struct drm_omap_gem_cpu_prep req = { .handle = bo->handle, .op = op, }; return drmCommandWrite(bo->dev->fd, DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req)); } int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op) { struct drm_omap_gem_cpu_fini req = { .handle = bo->handle, .op = op, .nregions = 0, }; return drmCommandWrite(bo->dev->fd, DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req)); } ='n222' href='#n222'>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 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829