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 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m, 870 struct msm_gem_stats *stats) 871 { 872 struct msm_gem_object *msm_obj = to_msm_bo(obj); 873 struct dma_resv *robj = obj->resv; 874 struct msm_gem_vma *vma; 875 uint64_t off = drm_vma_node_start(&obj->vma_node); 876 const char *madv; 877 878 msm_gem_lock(obj); 879 880 stats->all.count++; 881 stats->all.size += obj->size; 882 883 if (is_active(msm_obj)) { 884 stats->active.count++; 885 stats->active.size += obj->size; 886 } 887 888 if (msm_obj->pages) { 889 stats->resident.count++; 890 stats->resident.size += obj->size; 891 } 892 893 switch (msm_obj->madv) { 894 case __MSM_MADV_PURGED: 895 stats->purged.count++; 896 stats->purged.size += obj->size; 897 madv = " purged"; 898 break; 899 case MSM_MADV_DONTNEED: 900 stats->purgeable.count++; 901 stats->purgeable.size += obj->size; 902 madv = " purgeable"; 903 break; 904 case MSM_MADV_WILLNEED: 905 default: 906 madv = ""; 907 break; 908 } 909 910 seq_printf(m, "%08x: %c %2d (%2d) %08llx %p", 911 msm_obj->flags, is_active(msm_obj) ? 'A' : 'I', 912 obj->name, kref_read(&obj->refcount), 913 off, msm_obj->vaddr); 914 915 seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name); 916 917 if (!list_empty(&msm_obj->vmas)) { 918 919 seq_puts(m, " vmas:"); 920 921 list_for_each_entry(vma, &msm_obj->vmas, list) { 922 const char *name, *comm; 923 if (vma->aspace) { 924 struct msm_gem_address_space *aspace = vma->aspace; 925 struct task_struct *task = 926 get_pid_task(aspace->pid, PIDTYPE_PID); 927 if (task) { 928 comm = kstrdup(task->comm, GFP_KERNEL); 929 put_task_struct(task); 930 } else { 931 comm = NULL; 932 } 933 name = aspace->name; 934 } else { 935 name = comm = NULL; 936 } 937 seq_printf(m, " [%s%s%s: aspace=%p, %08llx,%s,inuse=%d]", 938 name, comm ? ":" : "", comm ? comm : "", 939 vma->aspace, vma->iova, 940 vma->mapped ? "mapped" : "unmapped", 941 vma->inuse); 942 kfree(comm); 943 } 944 945 seq_puts(m, "\n"); 946 } 947 948 dma_resv_describe(robj, m); 949 msm_gem_unlock(obj); 950 } 951 952 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m) 953 { 954 struct msm_gem_stats stats = {}; 955 struct msm_gem_object *msm_obj; 956 957 seq_puts(m, " flags id ref offset kaddr size madv name\n"); 958 list_for_each_entry(msm_obj, list, node) { 959 struct drm_gem_object *obj = &msm_obj->base; 960 seq_puts(m, " "); 961 msm_gem_describe(obj, m, &stats); 962 } 963 964 seq_printf(m, "Total: %4d objects, %9zu bytes\n", 965 stats.all.count, stats.all.size); 966 seq_printf(m, "Active: %4d objects, %9zu bytes\n", 967 stats.active.count, stats.active.size); 968 seq_printf(m, "Resident: %4d objects, %9zu bytes\n", 969 stats.resident.count, stats.resident.size); 970 seq_printf(m, "Purgeable: %4d objects, %9zu bytes\n", 971 stats.purgeable.count, stats.purgeable.size); 972 seq_printf(m, "Purged: %4d objects, %9zu bytes\n", 973 stats.purged.count, stats.purged.size); 974 } 975 #endif 976 977 /* don't call directly! Use drm_gem_object_put() */ 978 void msm_gem_free_object(struct drm_gem_object *obj) 979 { 980 struct msm_gem_object *msm_obj = to_msm_bo(obj); 981 struct drm_device *dev = obj->dev; 982 struct msm_drm_private *priv = dev->dev_private; 983 984 mutex_lock(&priv->obj_lock); 985 list_del(&msm_obj->node); 986 mutex_unlock(&priv->obj_lock); 987 988 mutex_lock(&priv->mm_lock); 989 if (msm_obj->dontneed) 990 mark_unpurgeable(msm_obj); 991 list_del(&msm_obj->mm_list); 992 mutex_unlock(&priv->mm_lock); 993 994 msm_gem_lock(obj); 995 996 /* object should not be on active list: */ 997 GEM_WARN_ON(is_active(msm_obj)); 998 999 put_iova_spaces(obj, true); 1000 1001 if (obj->import_attach) { 1002 GEM_WARN_ON(msm_obj->vaddr); 1003 1004 /* Don't drop the pages for imported dmabuf, as they are not 1005 * ours, just free the array we allocated: 1006 */ 1007 kvfree(msm_obj->pages); 1008 1009 put_iova_vmas(obj); 1010 1011 /* dma_buf_detach() grabs resv lock, so we need to unlock 1012 * prior to drm_prime_gem_destroy 1013 */ 1014 msm_gem_unlock(obj); 1015 1016 drm_prime_gem_destroy(obj, msm_obj->sgt); 1017 } else { 1018 msm_gem_vunmap(obj); 1019 put_pages(obj); 1020 put_iova_vmas(obj); 1021 msm_gem_unlock(obj); 1022 } 1023 1024 drm_gem_object_release(obj); 1025 1026 kfree(msm_obj); 1027 } 1028 1029 static int msm_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 1030 { 1031 struct msm_gem_object *msm_obj = to_msm_bo(obj); 1032 1033 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP; 1034 vma->vm_page_prot = msm_gem_pgprot(msm_obj, vm_get_page_prot(vma->vm_flags)); 1035 1036 return 0; 1037 } 1038 1039 /* convenience method to construct a GEM buffer object, and userspace handle */ 1040 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, 1041 uint32_t size, uint32_t flags, uint32_t *handle, 1042 char *name) 1043 { 1044 struct drm_gem_object *obj; 1045 int ret; 1046 1047 obj = msm_gem_new(dev, size, flags); 1048 1049 if (IS_ERR(obj)) 1050 return PTR_ERR(obj); 1051 1052 if (name) 1053 msm_gem_object_set_name(obj, "%s", name); 1054 1055 ret = drm_gem_handle_create(file, obj, handle); 1056 1057 /* drop reference from allocate - handle holds it now */ 1058 drm_gem_object_put(obj); 1059 1060 return ret; 1061 } 1062 1063 static const struct vm_operations_struct vm_ops = { 1064 .fault = msm_gem_fault, 1065 .open = drm_gem_vm_open, 1066 .close = drm_gem_vm_close, 1067 }; 1068 1069 static const struct drm_gem_object_funcs msm_gem_object_funcs = { 1070 .free = msm_gem_free_object, 1071 .pin = msm_gem_prime_pin, 1072 .unpin = msm_gem_prime_unpin, 1073 .get_sg_table = msm_gem_prime_get_sg_table, 1074 .vmap = msm_gem_prime_vmap, 1075 .vunmap = msm_gem_prime_vunmap, 1076 .mmap = msm_gem_object_mmap, 1077 .vm_ops = &vm_ops, 1078 }; 1079 1080 static int msm_gem_new_impl(struct drm_device *dev, 1081 uint32_t size, uint32_t flags, 1082 struct drm_gem_object **obj) 1083 { 1084 struct msm_drm_private *priv = dev->dev_private; 1085 struct msm_gem_object *msm_obj; 1086 1087 switch (flags & MSM_BO_CACHE_MASK) { 1088 case MSM_BO_UNCACHED: 1089 case MSM_BO_CACHED: 1090 case MSM_BO_WC: 1091 break; 1092 case MSM_BO_CACHED_COHERENT: 1093 if (priv->has_cached_coherent) 1094 break; 1095 fallthrough; 1096 default: 1097 DRM_DEV_DEBUG(dev->dev, "invalid cache flag: %x\n", 1098 (flags & MSM_BO_CACHE_MASK)); 1099 return -EINVAL; 1100 } 1101 1102 msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL); 1103 if (!msm_obj) 1104 return -ENOMEM; 1105 1106 msm_obj->flags = flags; 1107 msm_obj->madv = MSM_MADV_WILLNEED; 1108 1109 INIT_LIST_HEAD(&msm_obj->node); 1110 INIT_LIST_HEAD(&msm_obj->vmas); 1111 1112 *obj = &msm_obj->base; 1113 (*obj)->funcs = &msm_gem_object_funcs; 1114 1115 return 0; 1116 } 1117 1118 struct drm_gem_object *msm_gem_new(struct drm_device *dev, uint32_t size, uint32_t flags) 1119 { 1120 struct msm_drm_private *priv = dev->dev_private; 1121 struct msm_gem_object *msm_obj; 1122 struct drm_gem_object *obj = NULL; 1123 bool use_vram = false; 1124 int ret; 1125 1126 size = PAGE_ALIGN(size); 1127 1128 if (!msm_use_mmu(dev)) 1129 use_vram = true; 1130 else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size) 1131 use_vram = true; 1132 1133 if (GEM_WARN_ON(use_vram && !priv->vram.size)) 1134 return ERR_PTR(-EINVAL); 1135 1136 /* Disallow zero sized objects as they make the underlying 1137 * infrastructure grumpy 1138 */ 1139 if (size == 0) 1140 return ERR_PTR(-EINVAL); 1141 1142 ret = msm_gem_new_impl(dev, size, flags, &obj); 1143 if (ret) 1144 return ERR_PTR(ret); 1145 1146 msm_obj = to_msm_bo(obj); 1147 1148 if (use_vram) { 1149 struct msm_gem_vma *vma; 1150 struct page **pages; 1151 1152 drm_gem_private_object_init(dev, obj, size); 1153 1154 msm_gem_lock(obj); 1155 1156 vma = add_vma(obj, NULL); 1157 msm_gem_unlock(obj); 1158 if (IS_ERR(vma)) { 1159 ret = PTR_ERR(vma); 1160 goto fail; 1161 } 1162 1163 to_msm_bo(obj)->vram_node = &vma->node; 1164 1165 /* Call chain get_pages() -> update_inactive() tries to 1166 * access msm_obj->mm_list, but it is not initialized yet. 1167 * To avoid NULL pointer dereference error, initialize 1168 * mm_list to be empty. 1169 */ 1170 INIT_LIST_HEAD(&msm_obj->mm_list); 1171 1172 msm_gem_lock(obj); 1173 pages = get_pages(obj); 1174 msm_gem_unlock(obj); 1175 if (IS_ERR(pages)) { 1176 ret = PTR_ERR(pages); 1177 goto fail; 1178 } 1179 1180 vma->iova = physaddr(obj); 1181 } else { 1182 ret = drm_gem_object_init(dev, obj, size); 1183 if (ret) 1184 goto fail; 1185 /* 1186 * Our buffers are kept pinned, so allocating them from the 1187 * MOVABLE zone is a really bad idea, and conflicts with CMA. 1188 * See comments above new_inode() why this is required _and_ 1189 * expected if you're going to pin these pages. 1190 */ 1191 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER); 1192 } 1193 1194 mutex_lock(&priv->mm_lock); 1195 list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned); 1196 mutex_unlock(&priv->mm_lock); 1197 1198 mutex_lock(&priv->obj_lock); 1199 list_add_tail(&msm_obj->node, &priv->objects); 1200 mutex_unlock(&priv->obj_lock); 1201 1202 return obj; 1203 1204 fail: 1205 drm_gem_object_put(obj); 1206 return ERR_PTR(ret); 1207 } 1208 1209 struct drm_gem_object *msm_gem_import(struct drm_device *dev, 1210 struct dma_buf *dmabuf, struct sg_table *sgt) 1211 { 1212 struct msm_drm_private *priv = dev->dev_private; 1213 struct msm_gem_object *msm_obj; 1214 struct drm_gem_object *obj; 1215 uint32_t size; 1216 int ret, npages; 1217 1218 /* if we don't have IOMMU, don't bother pretending we can import: */ 1219 if (!msm_use_mmu(dev)) { 1220 DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n"); 1221 return ERR_PTR(-EINVAL); 1222 } 1223 1224 size = PAGE_ALIGN(dmabuf->size); 1225 1226 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj); 1227 if (ret) 1228 return ERR_PTR(ret); 1229 1230 drm_gem_private_object_init(dev, obj, size); 1231 1232 npages = size / PAGE_SIZE; 1233 1234 msm_obj = to_msm_bo(obj); 1235 msm_gem_lock(obj); 1236 msm_obj->sgt = sgt; 1237 msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 1238 if (!msm_obj->pages) { 1239 msm_gem_unlock(obj); 1240 ret = -ENOMEM; 1241 goto fail; 1242 } 1243 1244 ret = drm_prime_sg_to_page_array(sgt, msm_obj->pages, npages); 1245 if (ret) { 1246 msm_gem_unlock(obj); 1247 goto fail; 1248 } 1249 1250 msm_gem_unlock(obj); 1251 1252 mutex_lock(&priv->mm_lock); 1253 list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned); 1254 mutex_unlock(&priv->mm_lock); 1255 1256 mutex_lock(&priv->obj_lock); 1257 list_add_tail(&msm_obj->node, &priv->objects); 1258 mutex_unlock(&priv->obj_lock); 1259 1260 return obj; 1261 1262 fail: 1263 drm_gem_object_put(obj); 1264 return ERR_PTR(ret); 1265 } 1266 1267 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size, 1268 uint32_t flags, struct msm_gem_address_space *aspace, 1269 struct drm_gem_object **bo, uint64_t *iova) 1270 { 1271 void *vaddr; 1272 struct drm_gem_object *obj = msm_gem_new(dev, size, flags); 1273 int ret; 1274 1275 if (IS_ERR(obj)) 1276 return ERR_CAST(obj); 1277 1278 if (iova) { 1279 ret = msm_gem_get_and_pin_iova(obj, aspace, iova); 1280 if (ret) 1281 goto err; 1282 } 1283 1284 vaddr = msm_gem_get_vaddr(obj); 1285 if (IS_ERR(vaddr)) { 1286 msm_gem_unpin_iova(obj, aspace); 1287 ret = PTR_ERR(vaddr); 1288 goto err; 1289 } 1290 1291 if (bo) 1292 *bo = obj; 1293 1294 return vaddr; 1295 err: 1296 drm_gem_object_put(obj); 1297 1298 return ERR_PTR(ret); 1299 1300 } 1301 1302 void msm_gem_kernel_put(struct drm_gem_object *bo, 1303 struct msm_gem_address_space *aspace) 1304 { 1305 if (IS_ERR_OR_NULL(bo)) 1306 return; 1307 1308 msm_gem_put_vaddr(bo); 1309 msm_gem_unpin_iova(bo, aspace); 1310 drm_gem_object_put(bo); 1311 } 1312 1313 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...) 1314 { 1315 struct msm_gem_object *msm_obj = to_msm_bo(bo); 1316 va_list ap; 1317 1318 if (!fmt) 1319 return; 1320 1321 va_start(ap, fmt); 1322 vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap); 1323 va_end(ap); 1324 } 1325