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