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