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