1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2013 Red Hat 4 * Author: Rob Clark <robdclark@gmail.com> 5 */ 6 7 #include <linux/dma-map-ops.h> 8 #include <linux/vmalloc.h> 9 #include <linux/spinlock.h> 10 #include <linux/shmem_fs.h> 11 #include <linux/dma-buf.h> 12 #include <linux/pfn_t.h> 13 14 #include <drm/drm_prime.h> 15 16 #include "msm_drv.h" 17 #include "msm_fence.h" 18 #include "msm_gem.h" 19 #include "msm_gpu.h" 20 #include "msm_mmu.h" 21 22 static dma_addr_t physaddr(struct drm_gem_object *obj) 23 { 24 struct msm_gem_object *msm_obj = to_msm_bo(obj); 25 struct msm_drm_private *priv = obj->dev->dev_private; 26 return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) + 27 priv->vram.paddr; 28 } 29 30 static bool use_pages(struct drm_gem_object *obj) 31 { 32 struct msm_gem_object *msm_obj = to_msm_bo(obj); 33 return !msm_obj->vram_node; 34 } 35 36 /* 37 * Cache sync.. this is a bit over-complicated, to fit dma-mapping 38 * API. Really GPU cache is out of scope here (handled on cmdstream) 39 * and all we need to do is invalidate newly allocated pages before 40 * mapping to CPU as uncached/writecombine. 41 * 42 * On top of this, we have the added headache, that depending on 43 * display generation, the display's iommu may be wired up to either 44 * the toplevel drm device (mdss), or to the mdp sub-node, meaning 45 * that here we either have dma-direct or iommu ops. 46 * 47 * Let this be a cautionary tail of abstraction gone wrong. 48 */ 49 50 static void sync_for_device(struct msm_gem_object *msm_obj) 51 { 52 struct device *dev = msm_obj->base.dev->dev; 53 54 dma_map_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0); 55 } 56 57 static void sync_for_cpu(struct msm_gem_object *msm_obj) 58 { 59 struct device *dev = msm_obj->base.dev->dev; 60 61 dma_unmap_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0); 62 } 63 64 static void update_lru_active(struct drm_gem_object *obj) 65 { 66 struct msm_drm_private *priv = obj->dev->dev_private; 67 struct msm_gem_object *msm_obj = to_msm_bo(obj); 68 69 GEM_WARN_ON(!msm_obj->pages); 70 71 if (msm_obj->pin_count) { 72 drm_gem_lru_move_tail_locked(&priv->lru.pinned, obj); 73 } else if (msm_obj->madv == MSM_MADV_WILLNEED) { 74 drm_gem_lru_move_tail_locked(&priv->lru.willneed, obj); 75 } else { 76 GEM_WARN_ON(msm_obj->madv != MSM_MADV_DONTNEED); 77 78 drm_gem_lru_move_tail_locked(&priv->lru.dontneed, obj); 79 } 80 } 81 82 static void update_lru_locked(struct drm_gem_object *obj) 83 { 84 struct msm_drm_private *priv = obj->dev->dev_private; 85 struct msm_gem_object *msm_obj = to_msm_bo(obj); 86 87 msm_gem_assert_locked(&msm_obj->base); 88 89 if (!msm_obj->pages) { 90 GEM_WARN_ON(msm_obj->pin_count); 91 92 drm_gem_lru_move_tail_locked(&priv->lru.unbacked, obj); 93 } else { 94 update_lru_active(obj); 95 } 96 } 97 98 static void update_lru(struct drm_gem_object *obj) 99 { 100 struct msm_drm_private *priv = obj->dev->dev_private; 101 102 mutex_lock(&priv->lru.lock); 103 update_lru_locked(obj); 104 mutex_unlock(&priv->lru.lock); 105 } 106 107 /* allocate pages from VRAM carveout, used when no IOMMU: */ 108 static struct page **get_pages_vram(struct drm_gem_object *obj, int npages) 109 { 110 struct msm_gem_object *msm_obj = to_msm_bo(obj); 111 struct msm_drm_private *priv = obj->dev->dev_private; 112 dma_addr_t paddr; 113 struct page **p; 114 int ret, i; 115 116 p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 117 if (!p) 118 return ERR_PTR(-ENOMEM); 119 120 spin_lock(&priv->vram.lock); 121 ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages); 122 spin_unlock(&priv->vram.lock); 123 if (ret) { 124 kvfree(p); 125 return ERR_PTR(ret); 126 } 127 128 paddr = physaddr(obj); 129 for (i = 0; i < npages; i++) { 130 p[i] = pfn_to_page(__phys_to_pfn(paddr)); 131 paddr += PAGE_SIZE; 132 } 133 134 return p; 135 } 136 137 static struct page **get_pages(struct drm_gem_object *obj) 138 { 139 struct msm_gem_object *msm_obj = to_msm_bo(obj); 140 141 msm_gem_assert_locked(obj); 142 143 if (!msm_obj->pages) { 144 struct drm_device *dev = obj->dev; 145 struct page **p; 146 int npages = obj->size >> PAGE_SHIFT; 147 148 if (use_pages(obj)) 149 p = drm_gem_get_pages(obj); 150 else 151 p = get_pages_vram(obj, npages); 152 153 if (IS_ERR(p)) { 154 DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n", 155 PTR_ERR(p)); 156 return p; 157 } 158 159 msm_obj->pages = p; 160 161 msm_obj->sgt = drm_prime_pages_to_sg(obj->dev, p, npages); 162 if (IS_ERR(msm_obj->sgt)) { 163 void *ptr = ERR_CAST(msm_obj->sgt); 164 165 DRM_DEV_ERROR(dev->dev, "failed to allocate sgt\n"); 166 msm_obj->sgt = NULL; 167 return ptr; 168 } 169 170 /* For non-cached buffers, ensure the new pages are clean 171 * because display controller, GPU, etc. are not coherent: 172 */ 173 if (msm_obj->flags & MSM_BO_WC) 174 sync_for_device(msm_obj); 175 176 update_lru(obj); 177 } 178 179 return msm_obj->pages; 180 } 181 182 static void put_pages_vram(struct drm_gem_object *obj) 183 { 184 struct msm_gem_object *msm_obj = to_msm_bo(obj); 185 struct msm_drm_private *priv = obj->dev->dev_private; 186 187 spin_lock(&priv->vram.lock); 188 drm_mm_remove_node(msm_obj->vram_node); 189 spin_unlock(&priv->vram.lock); 190 191 kvfree(msm_obj->pages); 192 } 193 194 static void put_pages(struct drm_gem_object *obj) 195 { 196 struct msm_gem_object *msm_obj = to_msm_bo(obj); 197 198 if (msm_obj->pages) { 199 if (msm_obj->sgt) { 200 /* For non-cached buffers, ensure the new 201 * pages are clean because display controller, 202 * GPU, etc. are not coherent: 203 */ 204 if (msm_obj->flags & MSM_BO_WC) 205 sync_for_cpu(msm_obj); 206 207 sg_free_table(msm_obj->sgt); 208 kfree(msm_obj->sgt); 209 msm_obj->sgt = NULL; 210 } 211 212 if (use_pages(obj)) 213 drm_gem_put_pages(obj, msm_obj->pages, true, false); 214 else 215 put_pages_vram(obj); 216 217 msm_obj->pages = NULL; 218 update_lru(obj); 219 } 220 } 221 222 static struct page **msm_gem_pin_pages_locked(struct drm_gem_object *obj, 223 unsigned madv) 224 { 225 struct msm_drm_private *priv = obj->dev->dev_private; 226 struct msm_gem_object *msm_obj = to_msm_bo(obj); 227 struct page **p; 228 229 msm_gem_assert_locked(obj); 230 231 if (GEM_WARN_ON(msm_obj->madv > madv)) { 232 DRM_DEV_ERROR(obj->dev->dev, "Invalid madv state: %u vs %u\n", 233 msm_obj->madv, madv); 234 return ERR_PTR(-EBUSY); 235 } 236 237 p = get_pages(obj); 238 if (IS_ERR(p)) 239 return p; 240 241 mutex_lock(&priv->lru.lock); 242 msm_obj->pin_count++; 243 update_lru_locked(obj); 244 mutex_unlock(&priv->lru.lock); 245 246 return p; 247 } 248 249 struct page **msm_gem_pin_pages(struct drm_gem_object *obj) 250 { 251 struct page **p; 252 253 msm_gem_lock(obj); 254 p = msm_gem_pin_pages_locked(obj, MSM_MADV_WILLNEED); 255 msm_gem_unlock(obj); 256 257 return p; 258 } 259 260 void msm_gem_unpin_pages(struct drm_gem_object *obj) 261 { 262 msm_gem_lock(obj); 263 msm_gem_unpin_locked(obj); 264 msm_gem_unlock(obj); 265 } 266 267 static pgprot_t msm_gem_pgprot(struct msm_gem_object *msm_obj, pgprot_t prot) 268 { 269 if (msm_obj->flags & MSM_BO_WC) 270 return pgprot_writecombine(prot); 271 return prot; 272 } 273 274 static vm_fault_t msm_gem_fault(struct vm_fault *vmf) 275 { 276 struct vm_area_struct *vma = vmf->vma; 277 struct drm_gem_object *obj = vma->vm_private_data; 278 struct msm_gem_object *msm_obj = to_msm_bo(obj); 279 struct page **pages; 280 unsigned long pfn; 281 pgoff_t pgoff; 282 int err; 283 vm_fault_t ret; 284 285 /* 286 * vm_ops.open/drm_gem_mmap_obj and close get and put 287 * a reference on obj. So, we dont need to hold one here. 288 */ 289 err = msm_gem_lock_interruptible(obj); 290 if (err) { 291 ret = VM_FAULT_NOPAGE; 292 goto out; 293 } 294 295 if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) { 296 msm_gem_unlock(obj); 297 return VM_FAULT_SIGBUS; 298 } 299 300 /* make sure we have pages attached now */ 301 pages = get_pages(obj); 302 if (IS_ERR(pages)) { 303 ret = vmf_error(PTR_ERR(pages)); 304 goto out_unlock; 305 } 306 307 /* We don't use vmf->pgoff since that has the fake offset: */ 308 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT; 309 310 pfn = page_to_pfn(pages[pgoff]); 311 312 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address, 313 pfn, pfn << PAGE_SHIFT); 314 315 ret = vmf_insert_pfn(vma, vmf->address, pfn); 316 317 out_unlock: 318 msm_gem_unlock(obj); 319 out: 320 return ret; 321 } 322 323 /** get mmap offset */ 324 static uint64_t mmap_offset(struct drm_gem_object *obj) 325 { 326 struct drm_device *dev = obj->dev; 327 int ret; 328 329 msm_gem_assert_locked(obj); 330 331 /* Make it mmapable */ 332 ret = drm_gem_create_mmap_offset(obj); 333 334 if (ret) { 335 DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n"); 336 return 0; 337 } 338 339 return drm_vma_node_offset_addr(&obj->vma_node); 340 } 341 342 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj) 343 { 344 uint64_t offset; 345 346 msm_gem_lock(obj); 347 offset = mmap_offset(obj); 348 msm_gem_unlock(obj); 349 return offset; 350 } 351 352 static struct msm_gem_vma *add_vma(struct drm_gem_object *obj, 353 struct msm_gem_address_space *aspace) 354 { 355 struct msm_gem_object *msm_obj = to_msm_bo(obj); 356 struct msm_gem_vma *vma; 357 358 msm_gem_assert_locked(obj); 359 360 vma = msm_gem_vma_new(aspace); 361 if (!vma) 362 return ERR_PTR(-ENOMEM); 363 364 list_add_tail(&vma->list, &msm_obj->vmas); 365 366 return vma; 367 } 368 369 static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj, 370 struct msm_gem_address_space *aspace) 371 { 372 struct msm_gem_object *msm_obj = to_msm_bo(obj); 373 struct msm_gem_vma *vma; 374 375 msm_gem_assert_locked(obj); 376 377 list_for_each_entry(vma, &msm_obj->vmas, list) { 378 if (vma->aspace == aspace) 379 return vma; 380 } 381 382 return NULL; 383 } 384 385 static void del_vma(struct msm_gem_vma *vma) 386 { 387 if (!vma) 388 return; 389 390 list_del(&vma->list); 391 kfree(vma); 392 } 393 394 /* 395 * If close is true, this also closes the VMA (releasing the allocated 396 * iova range) in addition to removing the iommu mapping. In the eviction 397 * case (!close), we keep the iova allocated, but only remove the iommu 398 * mapping. 399 */ 400 static void 401 put_iova_spaces(struct drm_gem_object *obj, bool close) 402 { 403 struct msm_gem_object *msm_obj = to_msm_bo(obj); 404 struct msm_gem_vma *vma; 405 406 msm_gem_assert_locked(obj); 407 408 list_for_each_entry(vma, &msm_obj->vmas, list) { 409 if (vma->aspace) { 410 msm_gem_vma_purge(vma); 411 if (close) 412 msm_gem_vma_close(vma); 413 } 414 } 415 } 416 417 /* Called with msm_obj locked */ 418 static void 419 put_iova_vmas(struct drm_gem_object *obj) 420 { 421 struct msm_gem_object *msm_obj = to_msm_bo(obj); 422 struct msm_gem_vma *vma, *tmp; 423 424 msm_gem_assert_locked(obj); 425 426 list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) { 427 del_vma(vma); 428 } 429 } 430 431 static struct msm_gem_vma *get_vma_locked(struct drm_gem_object *obj, 432 struct msm_gem_address_space *aspace, 433 u64 range_start, u64 range_end) 434 { 435 struct msm_gem_vma *vma; 436 437 msm_gem_assert_locked(obj); 438 439 vma = lookup_vma(obj, aspace); 440 441 if (!vma) { 442 int ret; 443 444 vma = add_vma(obj, aspace); 445 if (IS_ERR(vma)) 446 return vma; 447 448 ret = msm_gem_vma_init(vma, obj->size, 449 range_start, range_end); 450 if (ret) { 451 del_vma(vma); 452 return ERR_PTR(ret); 453 } 454 } else { 455 GEM_WARN_ON(vma->iova < range_start); 456 GEM_WARN_ON((vma->iova + obj->size) > range_end); 457 } 458 459 return vma; 460 } 461 462 int msm_gem_pin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma) 463 { 464 struct msm_gem_object *msm_obj = to_msm_bo(obj); 465 struct page **pages; 466 int ret, prot = IOMMU_READ; 467 468 if (!(msm_obj->flags & MSM_BO_GPU_READONLY)) 469 prot |= IOMMU_WRITE; 470 471 if (msm_obj->flags & MSM_BO_MAP_PRIV) 472 prot |= IOMMU_PRIV; 473 474 if (msm_obj->flags & MSM_BO_CACHED_COHERENT) 475 prot |= IOMMU_CACHE; 476 477 msm_gem_assert_locked(obj); 478 479 pages = msm_gem_pin_pages_locked(obj, MSM_MADV_WILLNEED); 480 if (IS_ERR(pages)) 481 return PTR_ERR(pages); 482 483 ret = msm_gem_vma_map(vma, prot, msm_obj->sgt, obj->size); 484 if (ret) 485 msm_gem_unpin_locked(obj); 486 487 return ret; 488 } 489 490 void msm_gem_unpin_locked(struct drm_gem_object *obj) 491 { 492 struct msm_drm_private *priv = obj->dev->dev_private; 493 struct msm_gem_object *msm_obj = to_msm_bo(obj); 494 495 msm_gem_assert_locked(obj); 496 497 mutex_lock(&priv->lru.lock); 498 msm_obj->pin_count--; 499 GEM_WARN_ON(msm_obj->pin_count < 0); 500 update_lru_locked(obj); 501 mutex_unlock(&priv->lru.lock); 502 } 503 504 /* Special unpin path for use in fence-signaling path, avoiding the need 505 * to hold the obj lock by only depending on things that a protected by 506 * the LRU lock. In particular we know that that we already have backing 507 * and and that the object's dma_resv has the fence for the current 508 * submit/job which will prevent us racing against page eviction. 509 */ 510 void msm_gem_unpin_active(struct drm_gem_object *obj) 511 { 512 struct msm_drm_private *priv = obj->dev->dev_private; 513 struct msm_gem_object *msm_obj = to_msm_bo(obj); 514 515 mutex_lock(&priv->lru.lock); 516 msm_obj->pin_count--; 517 GEM_WARN_ON(msm_obj->pin_count < 0); 518 update_lru_active(obj); 519 mutex_unlock(&priv->lru.lock); 520 } 521 522 struct msm_gem_vma *msm_gem_get_vma_locked(struct drm_gem_object *obj, 523 struct msm_gem_address_space *aspace) 524 { 525 return get_vma_locked(obj, aspace, 0, U64_MAX); 526 } 527 528 static int get_and_pin_iova_range_locked(struct drm_gem_object *obj, 529 struct msm_gem_address_space *aspace, uint64_t *iova, 530 u64 range_start, u64 range_end) 531 { 532 struct msm_gem_vma *vma; 533 int ret; 534 535 msm_gem_assert_locked(obj); 536 537 vma = get_vma_locked(obj, aspace, range_start, range_end); 538 if (IS_ERR(vma)) 539 return PTR_ERR(vma); 540 541 ret = msm_gem_pin_vma_locked(obj, vma); 542 if (!ret) 543 *iova = vma->iova; 544 545 return ret; 546 } 547 548 /* 549 * get iova and pin it. Should have a matching put 550 * limits iova to specified range (in pages) 551 */ 552 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj, 553 struct msm_gem_address_space *aspace, uint64_t *iova, 554 u64 range_start, u64 range_end) 555 { 556 int ret; 557 558 msm_gem_lock(obj); 559 ret = get_and_pin_iova_range_locked(obj, aspace, iova, range_start, range_end); 560 msm_gem_unlock(obj); 561 562 return ret; 563 } 564 565 /* get iova and pin it. Should have a matching put */ 566 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj, 567 struct msm_gem_address_space *aspace, uint64_t *iova) 568 { 569 return msm_gem_get_and_pin_iova_range(obj, aspace, iova, 0, U64_MAX); 570 } 571 572 /* 573 * Get an iova but don't pin it. Doesn't need a put because iovas are currently 574 * valid for the life of the object 575 */ 576 int msm_gem_get_iova(struct drm_gem_object *obj, 577 struct msm_gem_address_space *aspace, uint64_t *iova) 578 { 579 struct msm_gem_vma *vma; 580 int ret = 0; 581 582 msm_gem_lock(obj); 583 vma = get_vma_locked(obj, aspace, 0, U64_MAX); 584 if (IS_ERR(vma)) { 585 ret = PTR_ERR(vma); 586 } else { 587 *iova = vma->iova; 588 } 589 msm_gem_unlock(obj); 590 591 return ret; 592 } 593 594 static int clear_iova(struct drm_gem_object *obj, 595 struct msm_gem_address_space *aspace) 596 { 597 struct msm_gem_vma *vma = lookup_vma(obj, aspace); 598 599 if (!vma) 600 return 0; 601 602 if (msm_gem_vma_inuse(vma)) 603 return -EBUSY; 604 605 msm_gem_vma_purge(vma); 606 msm_gem_vma_close(vma); 607 del_vma(vma); 608 609 return 0; 610 } 611 612 /* 613 * Get the requested iova but don't pin it. Fails if the requested iova is 614 * not available. Doesn't need a put because iovas are currently valid for 615 * the life of the object. 616 * 617 * Setting an iova of zero will clear the vma. 618 */ 619 int msm_gem_set_iova(struct drm_gem_object *obj, 620 struct msm_gem_address_space *aspace, uint64_t iova) 621 { 622 int ret = 0; 623 624 msm_gem_lock(obj); 625 if (!iova) { 626 ret = clear_iova(obj, aspace); 627 } else { 628 struct msm_gem_vma *vma; 629 vma = get_vma_locked(obj, aspace, iova, iova + obj->size); 630 if (IS_ERR(vma)) { 631 ret = PTR_ERR(vma); 632 } else if (GEM_WARN_ON(vma->iova != iova)) { 633 clear_iova(obj, aspace); 634 ret = -EBUSY; 635 } 636 } 637 msm_gem_unlock(obj); 638 639 return ret; 640 } 641 642 /* 643 * Unpin a iova by updating the reference counts. The memory isn't actually 644 * purged until something else (shrinker, mm_notifier, destroy, etc) decides 645 * to get rid of it 646 */ 647 void msm_gem_unpin_iova(struct drm_gem_object *obj, 648 struct msm_gem_address_space *aspace) 649 { 650 struct msm_gem_vma *vma; 651 652 msm_gem_lock(obj); 653 vma = lookup_vma(obj, aspace); 654 if (!GEM_WARN_ON(!vma)) { 655 msm_gem_vma_unpin(vma); 656 msm_gem_unpin_locked(obj); 657 } 658 msm_gem_unlock(obj); 659 } 660 661 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev, 662 struct drm_mode_create_dumb *args) 663 { 664 args->pitch = align_pitch(args->width, args->bpp); 665 args->size = PAGE_ALIGN(args->pitch * args->height); 666 return msm_gem_new_handle(dev, file, args->size, 667 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle, "dumb"); 668 } 669 670 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, 671 uint32_t handle, uint64_t *offset) 672 { 673 struct drm_gem_object *obj; 674 int ret = 0; 675 676 /* GEM does all our handle to object mapping */ 677 obj = drm_gem_object_lookup(file, handle); 678 if (obj == NULL) { 679 ret = -ENOENT; 680 goto fail; 681 } 682 683 *offset = msm_gem_mmap_offset(obj); 684 685 drm_gem_object_put(obj); 686 687 fail: 688 return ret; 689 } 690 691 static void *get_vaddr(struct drm_gem_object *obj, unsigned madv) 692 { 693 struct msm_gem_object *msm_obj = to_msm_bo(obj); 694 struct page **pages; 695 int ret = 0; 696 697 msm_gem_assert_locked(obj); 698 699 if (obj->import_attach) 700 return ERR_PTR(-ENODEV); 701 702 pages = msm_gem_pin_pages_locked(obj, madv); 703 if (IS_ERR(pages)) 704 return ERR_CAST(pages); 705 706 /* increment vmap_count *before* vmap() call, so shrinker can 707 * check vmap_count (is_vunmapable()) outside of msm_obj lock. 708 * This guarantees that we won't try to msm_gem_vunmap() this 709 * same object from within the vmap() call (while we already 710 * hold msm_obj lock) 711 */ 712 msm_obj->vmap_count++; 713 714 if (!msm_obj->vaddr) { 715 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT, 716 VM_MAP, msm_gem_pgprot(msm_obj, PAGE_KERNEL)); 717 if (msm_obj->vaddr == NULL) { 718 ret = -ENOMEM; 719 goto fail; 720 } 721 } 722 723 return msm_obj->vaddr; 724 725 fail: 726 msm_obj->vmap_count--; 727 msm_gem_unpin_locked(obj); 728 return ERR_PTR(ret); 729 } 730 731 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj) 732 { 733 return get_vaddr(obj, MSM_MADV_WILLNEED); 734 } 735 736 void *msm_gem_get_vaddr(struct drm_gem_object *obj) 737 { 738 void *ret; 739 740 msm_gem_lock(obj); 741 ret = msm_gem_get_vaddr_locked(obj); 742 msm_gem_unlock(obj); 743 744 return ret; 745 } 746 747 /* 748 * Don't use this! It is for the very special case of dumping 749 * submits from GPU hangs or faults, were the bo may already 750 * be MSM_MADV_DONTNEED, but we know the buffer is still on the 751 * active list. 752 */ 753 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj) 754 { 755 return get_vaddr(obj, __MSM_MADV_PURGED); 756 } 757 758 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj) 759 { 760 struct msm_gem_object *msm_obj = to_msm_bo(obj); 761 762 msm_gem_assert_locked(obj); 763 GEM_WARN_ON(msm_obj->vmap_count < 1); 764 765 msm_obj->vmap_count--; 766 msm_gem_unpin_locked(obj); 767 } 768 769 void msm_gem_put_vaddr(struct drm_gem_object *obj) 770 { 771 msm_gem_lock(obj); 772 msm_gem_put_vaddr_locked(obj); 773 msm_gem_unlock(obj); 774 } 775 776 /* Update madvise status, returns true if not purged, else 777 * false or -errno. 778 */ 779 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv) 780 { 781 struct msm_drm_private *priv = obj->dev->dev_private; 782 struct msm_gem_object *msm_obj = to_msm_bo(obj); 783 784 msm_gem_lock(obj); 785 786 mutex_lock(&priv->lru.lock); 787 788 if (msm_obj->madv != __MSM_MADV_PURGED) 789 msm_obj->madv = madv; 790 791 madv = msm_obj->madv; 792 793 /* If the obj is inactive, we might need to move it 794 * between inactive lists 795 */ 796 update_lru_locked(obj); 797 798 mutex_unlock(&priv->lru.lock); 799 800 msm_gem_unlock(obj); 801 802 return (madv != __MSM_MADV_PURGED); 803 } 804 805 void msm_gem_purge(struct drm_gem_object *obj) 806 { 807 struct drm_device *dev = obj->dev; 808 struct msm_drm_private *priv = obj->dev->dev_private; 809 struct msm_gem_object *msm_obj = to_msm_bo(obj); 810 811 msm_gem_assert_locked(obj); 812 GEM_WARN_ON(!is_purgeable(msm_obj)); 813 814 /* Get rid of any iommu mapping(s): */ 815 put_iova_spaces(obj, true); 816 817 msm_gem_vunmap(obj); 818 819 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping); 820 821 put_pages(obj); 822 823 put_iova_vmas(obj); 824 825 mutex_lock(&priv->lru.lock); 826 /* A one-way transition: */ 827 msm_obj->madv = __MSM_MADV_PURGED; 828 mutex_unlock(&priv->lru.lock); 829 830 drm_gem_free_mmap_offset(obj); 831 832 /* Our goal here is to return as much of the memory as 833 * is possible back to the system as we are called from OOM. 834 * To do this we must instruct the shmfs to drop all of its 835 * backing pages, *now*. 836 */ 837 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1); 838 839 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping, 840 0, (loff_t)-1); 841 } 842 843 /* 844 * Unpin the backing pages and make them available to be swapped out. 845 */ 846 void msm_gem_evict(struct drm_gem_object *obj) 847 { 848 struct drm_device *dev = obj->dev; 849 struct msm_gem_object *msm_obj = to_msm_bo(obj); 850 851 msm_gem_assert_locked(obj); 852 GEM_WARN_ON(is_unevictable(msm_obj)); 853 854 /* Get rid of any iommu mapping(s): */ 855 put_iova_spaces(obj, false); 856 857 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping); 858 859 put_pages(obj); 860 } 861 862 void msm_gem_vunmap(struct drm_gem_object *obj) 863 { 864 struct msm_gem_object *msm_obj = to_msm_bo(obj); 865 866 msm_gem_assert_locked(obj); 867 868 if (!msm_obj->vaddr || GEM_WARN_ON(!is_vunmapable(msm_obj))) 869 return; 870 871 vunmap(msm_obj->vaddr); 872 msm_obj->vaddr = NULL; 873 } 874 875 bool msm_gem_active(struct drm_gem_object *obj) 876 { 877 msm_gem_assert_locked(obj); 878 879 if (to_msm_bo(obj)->pin_count) 880 return true; 881 882 return !dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true)); 883 } 884 885 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout) 886 { 887 bool write = !!(op & MSM_PREP_WRITE); 888 unsigned long remain = 889 op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout); 890 long ret; 891 892 if (op & MSM_PREP_BOOST) { 893 dma_resv_set_deadline(obj->resv, dma_resv_usage_rw(write), 894 ktime_get()); 895 } 896 897 ret = dma_resv_wait_timeout(obj->resv, dma_resv_usage_rw(write), 898 true, remain); 899 if (ret == 0) 900 return remain == 0 ? -EBUSY : -ETIMEDOUT; 901 else if (ret < 0) 902 return ret; 903 904 /* TODO cache maintenance */ 905 906 return 0; 907 } 908 909 int msm_gem_cpu_fini(struct drm_gem_object *obj) 910 { 911 /* TODO cache maintenance */ 912 return 0; 913 } 914 915 #ifdef CONFIG_DEBUG_FS 916 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m, 917 struct msm_gem_stats *stats) 918 { 919 struct msm_gem_object *msm_obj = to_msm_bo(obj); 920 struct dma_resv *robj = obj->resv; 921 struct msm_gem_vma *vma; 922 uint64_t off = drm_vma_node_start(&obj->vma_node); 923 const char *madv; 924 925 msm_gem_lock(obj); 926 927 stats->all.count++; 928 stats->all.size += obj->size; 929 930 if (msm_gem_active(obj)) { 931 stats->active.count++; 932 stats->active.size += obj->size; 933 } 934 935 if (msm_obj->pages) { 936 stats->resident.count++; 937 stats->resident.size += obj->size; 938 } 939 940 switch (msm_obj->madv) { 941 case __MSM_MADV_PURGED: 942 stats->purged.count++; 943 stats->purged.size += obj->size; 944 madv = " purged"; 945 break; 946 case MSM_MADV_DONTNEED: 947 stats->purgeable.count++; 948 stats->purgeable.size += obj->size; 949 madv = " purgeable"; 950 break; 951 case MSM_MADV_WILLNEED: 952 default: 953 madv = ""; 954 break; 955 } 956 957 seq_printf(m, "%08x: %c %2d (%2d) %08llx %p", 958 msm_obj->flags, msm_gem_active(obj) ? 'A' : 'I', 959 obj->name, kref_read(&obj->refcount), 960 off, msm_obj->vaddr); 961 962 seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name); 963 964 if (!list_empty(&msm_obj->vmas)) { 965 966 seq_puts(m, " vmas:"); 967 968 list_for_each_entry(vma, &msm_obj->vmas, list) { 969 const char *name, *comm; 970 if (vma->aspace) { 971 struct msm_gem_address_space *aspace = vma->aspace; 972 struct task_struct *task = 973 get_pid_task(aspace->pid, PIDTYPE_PID); 974 if (task) { 975 comm = kstrdup(task->comm, GFP_KERNEL); 976 put_task_struct(task); 977 } else { 978 comm = NULL; 979 } 980 name = aspace->name; 981 } else { 982 name = comm = NULL; 983 } 984 seq_printf(m, " [%s%s%s: aspace=%p, %08llx,%s,inuse=%d]", 985 name, comm ? ":" : "", comm ? comm : "", 986 vma->aspace, vma->iova, 987 vma->mapped ? "mapped" : "unmapped", 988 msm_gem_vma_inuse(vma)); 989 kfree(comm); 990 } 991 992 seq_puts(m, "\n"); 993 } 994 995 dma_resv_describe(robj, m); 996 msm_gem_unlock(obj); 997 } 998 999 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m) 1000 { 1001 struct msm_gem_stats stats = {}; 1002 struct msm_gem_object *msm_obj; 1003 1004 seq_puts(m, " flags id ref offset kaddr size madv name\n"); 1005 list_for_each_entry(msm_obj, list, node) { 1006 struct drm_gem_object *obj = &msm_obj->base; 1007 seq_puts(m, " "); 1008 msm_gem_describe(obj, m, &stats); 1009 } 1010 1011 seq_printf(m, "Total: %4d objects, %9zu bytes\n", 1012 stats.all.count, stats.all.size); 1013 seq_printf(m, "Active: %4d objects, %9zu bytes\n", 1014 stats.active.count, stats.active.size); 1015 seq_printf(m, "Resident: %4d objects, %9zu bytes\n", 1016 stats.resident.count, stats.resident.size); 1017 seq_printf(m, "Purgeable: %4d objects, %9zu bytes\n", 1018 stats.purgeable.count, stats.purgeable.size); 1019 seq_printf(m, "Purged: %4d objects, %9zu bytes\n", 1020 stats.purged.count, stats.purged.size); 1021 } 1022 #endif 1023 1024 /* don't call directly! Use drm_gem_object_put() */ 1025 static void msm_gem_free_object(struct drm_gem_object *obj) 1026 { 1027 struct msm_gem_object *msm_obj = to_msm_bo(obj); 1028 struct drm_device *dev = obj->dev; 1029 struct msm_drm_private *priv = dev->dev_private; 1030 1031 mutex_lock(&priv->obj_lock); 1032 list_del(&msm_obj->node); 1033 mutex_unlock(&priv->obj_lock); 1034 1035 put_iova_spaces(obj, true); 1036 1037 if (obj->import_attach) { 1038 GEM_WARN_ON(msm_obj->vaddr); 1039 1040 /* Don't drop the pages for imported dmabuf, as they are not 1041 * ours, just free the array we allocated: 1042 */ 1043 kvfree(msm_obj->pages); 1044 1045 put_iova_vmas(obj); 1046 1047 drm_prime_gem_destroy(obj, msm_obj->sgt); 1048 } else { 1049 msm_gem_vunmap(obj); 1050 put_pages(obj); 1051 put_iova_vmas(obj); 1052 } 1053 1054 drm_gem_object_release(obj); 1055 1056 kfree(msm_obj); 1057 } 1058 1059 static int msm_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 1060 { 1061 struct msm_gem_object *msm_obj = to_msm_bo(obj); 1062 1063 vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 1064 vma->vm_page_prot = msm_gem_pgprot(msm_obj, vm_get_page_prot(vma->vm_flags)); 1065 1066 return 0; 1067 } 1068 1069 /* convenience method to construct a GEM buffer object, and userspace handle */ 1070 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, 1071 uint32_t size, uint32_t flags, uint32_t *handle, 1072 char *name) 1073 { 1074 struct drm_gem_object *obj; 1075 int ret; 1076 1077 obj = msm_gem_new(dev, size, flags); 1078 1079 if (IS_ERR(obj)) 1080 return PTR_ERR(obj); 1081 1082 if (name) 1083 msm_gem_object_set_name(obj, "%s", name); 1084 1085 ret = drm_gem_handle_create(file, obj, handle); 1086 1087 /* drop reference from allocate - handle holds it now */ 1088 drm_gem_object_put(obj); 1089 1090 return ret; 1091 } 1092 1093 static const struct vm_operations_struct vm_ops = { 1094 .fault = msm_gem_fault, 1095 .open = drm_gem_vm_open, 1096 .close = drm_gem_vm_close, 1097 }; 1098 1099 static const struct drm_gem_object_funcs msm_gem_object_funcs = { 1100 .free = msm_gem_free_object, 1101 .pin = msm_gem_prime_pin, 1102 .unpin = msm_gem_prime_unpin, 1103 .get_sg_table = msm_gem_prime_get_sg_table, 1104 .vmap = msm_gem_prime_vmap, 1105 .vunmap = msm_gem_prime_vunmap, 1106 .mmap = msm_gem_object_mmap, 1107 .vm_ops = &vm_ops, 1108 }; 1109 1110 static int msm_gem_new_impl(struct drm_device *dev, 1111 uint32_t size, uint32_t flags, 1112 struct drm_gem_object **obj) 1113 { 1114 struct msm_drm_private *priv = dev->dev_private; 1115 struct msm_gem_object *msm_obj; 1116 1117 switch (flags & MSM_BO_CACHE_MASK) { 1118 case MSM_BO_CACHED: 1119 case MSM_BO_WC: 1120 break; 1121 case MSM_BO_CACHED_COHERENT: 1122 if (priv->has_cached_coherent) 1123 break; 1124 fallthrough; 1125 default: 1126 DRM_DEV_DEBUG(dev->dev, "invalid cache flag: %x\n", 1127 (flags & MSM_BO_CACHE_MASK)); 1128 return -EINVAL; 1129 } 1130 1131 msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL); 1132 if (!msm_obj) 1133 return -ENOMEM; 1134 1135 msm_obj->flags = flags; 1136 msm_obj->madv = MSM_MADV_WILLNEED; 1137 1138 INIT_LIST_HEAD(&msm_obj->node); 1139 INIT_LIST_HEAD(&msm_obj->vmas); 1140 1141 *obj = &msm_obj->base; 1142 (*obj)->funcs = &msm_gem_object_funcs; 1143 1144 return 0; 1145 } 1146 1147 struct drm_gem_object *msm_gem_new(struct drm_device *dev, uint32_t size, uint32_t flags) 1148 { 1149 struct msm_drm_private *priv = dev->dev_private; 1150 struct msm_gem_object *msm_obj; 1151 struct drm_gem_object *obj = NULL; 1152 bool use_vram = false; 1153 int ret; 1154 1155 size = PAGE_ALIGN(size); 1156 1157 if (!msm_use_mmu(dev)) 1158 use_vram = true; 1159 else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size) 1160 use_vram = true; 1161 1162 if (GEM_WARN_ON(use_vram && !priv->vram.size)) 1163 return ERR_PTR(-EINVAL); 1164 1165 /* Disallow zero sized objects as they make the underlying 1166 * infrastructure grumpy 1167 */ 1168 if (size == 0) 1169 return ERR_PTR(-EINVAL); 1170 1171 ret = msm_gem_new_impl(dev, size, flags, &obj); 1172 if (ret) 1173 return ERR_PTR(ret); 1174 1175 msm_obj = to_msm_bo(obj); 1176 1177 if (use_vram) { 1178 struct msm_gem_vma *vma; 1179 struct page **pages; 1180 1181 drm_gem_private_object_init(dev, obj, size); 1182 1183 msm_gem_lock(obj); 1184 1185 vma = add_vma(obj, NULL); 1186 msm_gem_unlock(obj); 1187 if (IS_ERR(vma)) { 1188 ret = PTR_ERR(vma); 1189 goto fail; 1190 } 1191 1192 to_msm_bo(obj)->vram_node = &vma->node; 1193 1194 msm_gem_lock(obj); 1195 pages = get_pages(obj); 1196 msm_gem_unlock(obj); 1197 if (IS_ERR(pages)) { 1198 ret = PTR_ERR(pages); 1199 goto fail; 1200 } 1201 1202 vma->iova = physaddr(obj); 1203 } else { 1204 ret = drm_gem_object_init(dev, obj, size); 1205 if (ret) 1206 goto fail; 1207 /* 1208 * Our buffers are kept pinned, so allocating them from the 1209 * MOVABLE zone is a really bad idea, and conflicts with CMA. 1210 * See comments above new_inode() why this is required _and_ 1211 * expected if you're going to pin these pages. 1212 */ 1213 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER); 1214 } 1215 1216 drm_gem_lru_move_tail(&priv->lru.unbacked, obj); 1217 1218 mutex_lock(&priv->obj_lock); 1219 list_add_tail(&msm_obj->node, &priv->objects); 1220 mutex_unlock(&priv->obj_lock); 1221 1222 return obj; 1223 1224 fail: 1225 drm_gem_object_put(obj); 1226 return ERR_PTR(ret); 1227 } 1228 1229 struct drm_gem_object *msm_gem_import(struct drm_device *dev, 1230 struct dma_buf *dmabuf, struct sg_table *sgt) 1231 { 1232 struct msm_drm_private *priv = dev->dev_private; 1233 struct msm_gem_object *msm_obj; 1234 struct drm_gem_object *obj; 1235 uint32_t size; 1236 int ret, npages; 1237 1238 /* if we don't have IOMMU, don't bother pretending we can import: */ 1239 if (!msm_use_mmu(dev)) { 1240 DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n"); 1241 return ERR_PTR(-EINVAL); 1242 } 1243 1244 size = PAGE_ALIGN(dmabuf->size); 1245 1246 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj); 1247 if (ret) 1248 return ERR_PTR(ret); 1249 1250 drm_gem_private_object_init(dev, obj, size); 1251 1252 npages = size / PAGE_SIZE; 1253 1254 msm_obj = to_msm_bo(obj); 1255 msm_gem_lock(obj); 1256 msm_obj->sgt = sgt; 1257 msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 1258 if (!msm_obj->pages) { 1259 msm_gem_unlock(obj); 1260 ret = -ENOMEM; 1261 goto fail; 1262 } 1263 1264 ret = drm_prime_sg_to_page_array(sgt, msm_obj->pages, npages); 1265 if (ret) { 1266 msm_gem_unlock(obj); 1267 goto fail; 1268 } 1269 1270 msm_gem_unlock(obj); 1271 1272 drm_gem_lru_move_tail(&priv->lru.pinned, obj); 1273 1274 mutex_lock(&priv->obj_lock); 1275 list_add_tail(&msm_obj->node, &priv->objects); 1276 mutex_unlock(&priv->obj_lock); 1277 1278 return obj; 1279 1280 fail: 1281 drm_gem_object_put(obj); 1282 return ERR_PTR(ret); 1283 } 1284 1285 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size, 1286 uint32_t flags, struct msm_gem_address_space *aspace, 1287 struct drm_gem_object **bo, uint64_t *iova) 1288 { 1289 void *vaddr; 1290 struct drm_gem_object *obj = msm_gem_new(dev, size, flags); 1291 int ret; 1292 1293 if (IS_ERR(obj)) 1294 return ERR_CAST(obj); 1295 1296 if (iova) { 1297 ret = msm_gem_get_and_pin_iova(obj, aspace, iova); 1298 if (ret) 1299 goto err; 1300 } 1301 1302 vaddr = msm_gem_get_vaddr(obj); 1303 if (IS_ERR(vaddr)) { 1304 msm_gem_unpin_iova(obj, aspace); 1305 ret = PTR_ERR(vaddr); 1306 goto err; 1307 } 1308 1309 if (bo) 1310 *bo = obj; 1311 1312 return vaddr; 1313 err: 1314 drm_gem_object_put(obj); 1315 1316 return ERR_PTR(ret); 1317 1318 } 1319 1320 void msm_gem_kernel_put(struct drm_gem_object *bo, 1321 struct msm_gem_address_space *aspace) 1322 { 1323 if (IS_ERR_OR_NULL(bo)) 1324 return; 1325 1326 msm_gem_put_vaddr(bo); 1327 msm_gem_unpin_iova(bo, aspace); 1328 drm_gem_object_put(bo); 1329 } 1330 1331 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...) 1332 { 1333 struct msm_gem_object *msm_obj = to_msm_bo(bo); 1334 va_list ap; 1335 1336 if (!fmt) 1337 return; 1338 1339 va_start(ap, fmt); 1340 vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap); 1341 va_end(ap); 1342 } 1343