1 /* 2 * Copyright (C) 2013 Red Hat 3 * Author: Rob Clark <robdclark@gmail.com> 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * You should have received a copy of the GNU General Public License along with 15 * this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include <linux/spinlock.h> 19 #include <linux/shmem_fs.h> 20 #include <linux/dma-buf.h> 21 #include <linux/pfn_t.h> 22 23 #include "msm_drv.h" 24 #include "msm_fence.h" 25 #include "msm_gem.h" 26 #include "msm_gpu.h" 27 #include "msm_mmu.h" 28 29 static dma_addr_t physaddr(struct drm_gem_object *obj) 30 { 31 struct msm_gem_object *msm_obj = to_msm_bo(obj); 32 struct msm_drm_private *priv = obj->dev->dev_private; 33 return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) + 34 priv->vram.paddr; 35 } 36 37 static bool use_pages(struct drm_gem_object *obj) 38 { 39 struct msm_gem_object *msm_obj = to_msm_bo(obj); 40 return !msm_obj->vram_node; 41 } 42 43 /* allocate pages from VRAM carveout, used when no IOMMU: */ 44 static struct page **get_pages_vram(struct drm_gem_object *obj, 45 int npages) 46 { 47 struct msm_gem_object *msm_obj = to_msm_bo(obj); 48 struct msm_drm_private *priv = obj->dev->dev_private; 49 dma_addr_t paddr; 50 struct page **p; 51 int ret, i; 52 53 p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 54 if (!p) 55 return ERR_PTR(-ENOMEM); 56 57 ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages); 58 if (ret) { 59 kvfree(p); 60 return ERR_PTR(ret); 61 } 62 63 paddr = physaddr(obj); 64 for (i = 0; i < npages; i++) { 65 p[i] = phys_to_page(paddr); 66 paddr += PAGE_SIZE; 67 } 68 69 return p; 70 } 71 72 /* called with dev->struct_mutex held */ 73 static struct page **get_pages(struct drm_gem_object *obj) 74 { 75 struct msm_gem_object *msm_obj = to_msm_bo(obj); 76 77 if (!msm_obj->pages) { 78 struct drm_device *dev = obj->dev; 79 struct page **p; 80 int npages = obj->size >> PAGE_SHIFT; 81 82 if (use_pages(obj)) 83 p = drm_gem_get_pages(obj); 84 else 85 p = get_pages_vram(obj, npages); 86 87 if (IS_ERR(p)) { 88 dev_err(dev->dev, "could not get pages: %ld\n", 89 PTR_ERR(p)); 90 return p; 91 } 92 93 msm_obj->sgt = drm_prime_pages_to_sg(p, npages); 94 if (IS_ERR(msm_obj->sgt)) { 95 dev_err(dev->dev, "failed to allocate sgt\n"); 96 return ERR_CAST(msm_obj->sgt); 97 } 98 99 msm_obj->pages = p; 100 101 /* For non-cached buffers, ensure the new pages are clean 102 * because display controller, GPU, etc. are not coherent: 103 */ 104 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) 105 dma_map_sg(dev->dev, msm_obj->sgt->sgl, 106 msm_obj->sgt->nents, DMA_BIDIRECTIONAL); 107 } 108 109 return msm_obj->pages; 110 } 111 112 static void put_pages(struct drm_gem_object *obj) 113 { 114 struct msm_gem_object *msm_obj = to_msm_bo(obj); 115 116 if (msm_obj->pages) { 117 /* For non-cached buffers, ensure the new pages are clean 118 * because display controller, GPU, etc. are not coherent: 119 */ 120 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) 121 dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl, 122 msm_obj->sgt->nents, DMA_BIDIRECTIONAL); 123 sg_free_table(msm_obj->sgt); 124 kfree(msm_obj->sgt); 125 126 if (use_pages(obj)) 127 drm_gem_put_pages(obj, msm_obj->pages, true, false); 128 else { 129 drm_mm_remove_node(msm_obj->vram_node); 130 kvfree(msm_obj->pages); 131 } 132 133 msm_obj->pages = NULL; 134 } 135 } 136 137 struct page **msm_gem_get_pages(struct drm_gem_object *obj) 138 { 139 struct drm_device *dev = obj->dev; 140 struct page **p; 141 mutex_lock(&dev->struct_mutex); 142 p = get_pages(obj); 143 mutex_unlock(&dev->struct_mutex); 144 return p; 145 } 146 147 void msm_gem_put_pages(struct drm_gem_object *obj) 148 { 149 /* when we start tracking the pin count, then do something here */ 150 } 151 152 int msm_gem_mmap_obj(struct drm_gem_object *obj, 153 struct vm_area_struct *vma) 154 { 155 struct msm_gem_object *msm_obj = to_msm_bo(obj); 156 157 vma->vm_flags &= ~VM_PFNMAP; 158 vma->vm_flags |= VM_MIXEDMAP; 159 160 if (msm_obj->flags & MSM_BO_WC) { 161 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 162 } else if (msm_obj->flags & MSM_BO_UNCACHED) { 163 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags)); 164 } else { 165 /* 166 * Shunt off cached objs to shmem file so they have their own 167 * address_space (so unmap_mapping_range does what we want, 168 * in particular in the case of mmap'd dmabufs) 169 */ 170 fput(vma->vm_file); 171 get_file(obj->filp); 172 vma->vm_pgoff = 0; 173 vma->vm_file = obj->filp; 174 175 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 176 } 177 178 return 0; 179 } 180 181 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 182 { 183 int ret; 184 185 ret = drm_gem_mmap(filp, vma); 186 if (ret) { 187 DBG("mmap failed: %d", ret); 188 return ret; 189 } 190 191 return msm_gem_mmap_obj(vma->vm_private_data, vma); 192 } 193 194 int msm_gem_fault(struct vm_fault *vmf) 195 { 196 struct vm_area_struct *vma = vmf->vma; 197 struct drm_gem_object *obj = vma->vm_private_data; 198 struct drm_device *dev = obj->dev; 199 struct msm_drm_private *priv = dev->dev_private; 200 struct page **pages; 201 unsigned long pfn; 202 pgoff_t pgoff; 203 int ret; 204 205 /* This should only happen if userspace tries to pass a mmap'd 206 * but unfaulted gem bo vaddr into submit ioctl, triggering 207 * a page fault while struct_mutex is already held. This is 208 * not a valid use-case so just bail. 209 */ 210 if (priv->struct_mutex_task == current) 211 return VM_FAULT_SIGBUS; 212 213 /* Make sure we don't parallel update on a fault, nor move or remove 214 * something from beneath our feet 215 */ 216 ret = mutex_lock_interruptible(&dev->struct_mutex); 217 if (ret) 218 goto out; 219 220 /* make sure we have pages attached now */ 221 pages = get_pages(obj); 222 if (IS_ERR(pages)) { 223 ret = PTR_ERR(pages); 224 goto out_unlock; 225 } 226 227 /* We don't use vmf->pgoff since that has the fake offset: */ 228 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT; 229 230 pfn = page_to_pfn(pages[pgoff]); 231 232 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address, 233 pfn, pfn << PAGE_SHIFT); 234 235 ret = vm_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV)); 236 237 out_unlock: 238 mutex_unlock(&dev->struct_mutex); 239 out: 240 switch (ret) { 241 case -EAGAIN: 242 case 0: 243 case -ERESTARTSYS: 244 case -EINTR: 245 case -EBUSY: 246 /* 247 * EBUSY is ok: this just means that another thread 248 * already did the job. 249 */ 250 return VM_FAULT_NOPAGE; 251 case -ENOMEM: 252 return VM_FAULT_OOM; 253 default: 254 return VM_FAULT_SIGBUS; 255 } 256 } 257 258 /** get mmap offset */ 259 static uint64_t mmap_offset(struct drm_gem_object *obj) 260 { 261 struct drm_device *dev = obj->dev; 262 int ret; 263 264 WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 265 266 /* Make it mmapable */ 267 ret = drm_gem_create_mmap_offset(obj); 268 269 if (ret) { 270 dev_err(dev->dev, "could not allocate mmap offset\n"); 271 return 0; 272 } 273 274 return drm_vma_node_offset_addr(&obj->vma_node); 275 } 276 277 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj) 278 { 279 uint64_t offset; 280 mutex_lock(&obj->dev->struct_mutex); 281 offset = mmap_offset(obj); 282 mutex_unlock(&obj->dev->struct_mutex); 283 return offset; 284 } 285 286 static void 287 put_iova(struct drm_gem_object *obj) 288 { 289 struct drm_device *dev = obj->dev; 290 struct msm_drm_private *priv = obj->dev->dev_private; 291 struct msm_gem_object *msm_obj = to_msm_bo(obj); 292 int id; 293 294 WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 295 296 for (id = 0; id < ARRAY_SIZE(msm_obj->domain); id++) { 297 if (!priv->aspace[id]) 298 continue; 299 msm_gem_unmap_vma(priv->aspace[id], 300 &msm_obj->domain[id], msm_obj->sgt); 301 } 302 } 303 304 /* should be called under struct_mutex.. although it can be called 305 * from atomic context without struct_mutex to acquire an extra 306 * iova ref if you know one is already held. 307 * 308 * That means when I do eventually need to add support for unpinning 309 * the refcnt counter needs to be atomic_t. 310 */ 311 int msm_gem_get_iova_locked(struct drm_gem_object *obj, int id, 312 uint64_t *iova) 313 { 314 struct msm_gem_object *msm_obj = to_msm_bo(obj); 315 int ret = 0; 316 317 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); 318 319 if (!msm_obj->domain[id].iova) { 320 struct msm_drm_private *priv = obj->dev->dev_private; 321 struct page **pages = get_pages(obj); 322 323 if (IS_ERR(pages)) 324 return PTR_ERR(pages); 325 326 if (iommu_present(&platform_bus_type)) { 327 ret = msm_gem_map_vma(priv->aspace[id], &msm_obj->domain[id], 328 msm_obj->sgt, obj->size >> PAGE_SHIFT); 329 } else { 330 msm_obj->domain[id].iova = physaddr(obj); 331 } 332 } 333 334 if (!ret) 335 *iova = msm_obj->domain[id].iova; 336 337 return ret; 338 } 339 340 /* get iova, taking a reference. Should have a matching put */ 341 int msm_gem_get_iova(struct drm_gem_object *obj, int id, uint64_t *iova) 342 { 343 struct msm_gem_object *msm_obj = to_msm_bo(obj); 344 int ret; 345 346 /* this is safe right now because we don't unmap until the 347 * bo is deleted: 348 */ 349 if (msm_obj->domain[id].iova) { 350 might_lock(&obj->dev->struct_mutex); 351 *iova = msm_obj->domain[id].iova; 352 return 0; 353 } 354 355 mutex_lock(&obj->dev->struct_mutex); 356 ret = msm_gem_get_iova_locked(obj, id, iova); 357 mutex_unlock(&obj->dev->struct_mutex); 358 return ret; 359 } 360 361 /* get iova without taking a reference, used in places where you have 362 * already done a 'msm_gem_get_iova()'. 363 */ 364 uint64_t msm_gem_iova(struct drm_gem_object *obj, int id) 365 { 366 struct msm_gem_object *msm_obj = to_msm_bo(obj); 367 WARN_ON(!msm_obj->domain[id].iova); 368 return msm_obj->domain[id].iova; 369 } 370 371 void msm_gem_put_iova(struct drm_gem_object *obj, int id) 372 { 373 // XXX TODO .. 374 // NOTE: probably don't need a _locked() version.. we wouldn't 375 // normally unmap here, but instead just mark that it could be 376 // unmapped (if the iova refcnt drops to zero), but then later 377 // if another _get_iova_locked() fails we can start unmapping 378 // things that are no longer needed.. 379 } 380 381 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev, 382 struct drm_mode_create_dumb *args) 383 { 384 args->pitch = align_pitch(args->width, args->bpp); 385 args->size = PAGE_ALIGN(args->pitch * args->height); 386 return msm_gem_new_handle(dev, file, args->size, 387 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle); 388 } 389 390 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, 391 uint32_t handle, uint64_t *offset) 392 { 393 struct drm_gem_object *obj; 394 int ret = 0; 395 396 /* GEM does all our handle to object mapping */ 397 obj = drm_gem_object_lookup(file, handle); 398 if (obj == NULL) { 399 ret = -ENOENT; 400 goto fail; 401 } 402 403 *offset = msm_gem_mmap_offset(obj); 404 405 drm_gem_object_unreference_unlocked(obj); 406 407 fail: 408 return ret; 409 } 410 411 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj) 412 { 413 struct msm_gem_object *msm_obj = to_msm_bo(obj); 414 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); 415 if (!msm_obj->vaddr) { 416 struct page **pages = get_pages(obj); 417 if (IS_ERR(pages)) 418 return ERR_CAST(pages); 419 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT, 420 VM_MAP, pgprot_writecombine(PAGE_KERNEL)); 421 if (msm_obj->vaddr == NULL) 422 return ERR_PTR(-ENOMEM); 423 } 424 msm_obj->vmap_count++; 425 return msm_obj->vaddr; 426 } 427 428 void *msm_gem_get_vaddr(struct drm_gem_object *obj) 429 { 430 void *ret; 431 mutex_lock(&obj->dev->struct_mutex); 432 ret = msm_gem_get_vaddr_locked(obj); 433 mutex_unlock(&obj->dev->struct_mutex); 434 return ret; 435 } 436 437 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj) 438 { 439 struct msm_gem_object *msm_obj = to_msm_bo(obj); 440 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); 441 WARN_ON(msm_obj->vmap_count < 1); 442 msm_obj->vmap_count--; 443 } 444 445 void msm_gem_put_vaddr(struct drm_gem_object *obj) 446 { 447 mutex_lock(&obj->dev->struct_mutex); 448 msm_gem_put_vaddr_locked(obj); 449 mutex_unlock(&obj->dev->struct_mutex); 450 } 451 452 /* Update madvise status, returns true if not purged, else 453 * false or -errno. 454 */ 455 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv) 456 { 457 struct msm_gem_object *msm_obj = to_msm_bo(obj); 458 459 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); 460 461 if (msm_obj->madv != __MSM_MADV_PURGED) 462 msm_obj->madv = madv; 463 464 return (msm_obj->madv != __MSM_MADV_PURGED); 465 } 466 467 void msm_gem_purge(struct drm_gem_object *obj) 468 { 469 struct drm_device *dev = obj->dev; 470 struct msm_gem_object *msm_obj = to_msm_bo(obj); 471 472 WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 473 WARN_ON(!is_purgeable(msm_obj)); 474 WARN_ON(obj->import_attach); 475 476 put_iova(obj); 477 478 msm_gem_vunmap(obj); 479 480 put_pages(obj); 481 482 msm_obj->madv = __MSM_MADV_PURGED; 483 484 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping); 485 drm_gem_free_mmap_offset(obj); 486 487 /* Our goal here is to return as much of the memory as 488 * is possible back to the system as we are called from OOM. 489 * To do this we must instruct the shmfs to drop all of its 490 * backing pages, *now*. 491 */ 492 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1); 493 494 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping, 495 0, (loff_t)-1); 496 } 497 498 void msm_gem_vunmap(struct drm_gem_object *obj) 499 { 500 struct msm_gem_object *msm_obj = to_msm_bo(obj); 501 502 if (!msm_obj->vaddr || WARN_ON(!is_vunmapable(msm_obj))) 503 return; 504 505 vunmap(msm_obj->vaddr); 506 msm_obj->vaddr = NULL; 507 } 508 509 /* must be called before _move_to_active().. */ 510 int msm_gem_sync_object(struct drm_gem_object *obj, 511 struct msm_fence_context *fctx, bool exclusive) 512 { 513 struct msm_gem_object *msm_obj = to_msm_bo(obj); 514 struct reservation_object_list *fobj; 515 struct dma_fence *fence; 516 int i, ret; 517 518 if (!exclusive) { 519 /* NOTE: _reserve_shared() must happen before _add_shared_fence(), 520 * which makes this a slightly strange place to call it. OTOH this 521 * is a convenient can-fail point to hook it in. (And similar to 522 * how etnaviv and nouveau handle this.) 523 */ 524 ret = reservation_object_reserve_shared(msm_obj->resv); 525 if (ret) 526 return ret; 527 } 528 529 fobj = reservation_object_get_list(msm_obj->resv); 530 if (!fobj || (fobj->shared_count == 0)) { 531 fence = reservation_object_get_excl(msm_obj->resv); 532 /* don't need to wait on our own fences, since ring is fifo */ 533 if (fence && (fence->context != fctx->context)) { 534 ret = dma_fence_wait(fence, true); 535 if (ret) 536 return ret; 537 } 538 } 539 540 if (!exclusive || !fobj) 541 return 0; 542 543 for (i = 0; i < fobj->shared_count; i++) { 544 fence = rcu_dereference_protected(fobj->shared[i], 545 reservation_object_held(msm_obj->resv)); 546 if (fence->context != fctx->context) { 547 ret = dma_fence_wait(fence, true); 548 if (ret) 549 return ret; 550 } 551 } 552 553 return 0; 554 } 555 556 void msm_gem_move_to_active(struct drm_gem_object *obj, 557 struct msm_gpu *gpu, bool exclusive, struct dma_fence *fence) 558 { 559 struct msm_gem_object *msm_obj = to_msm_bo(obj); 560 WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED); 561 msm_obj->gpu = gpu; 562 if (exclusive) 563 reservation_object_add_excl_fence(msm_obj->resv, fence); 564 else 565 reservation_object_add_shared_fence(msm_obj->resv, fence); 566 list_del_init(&msm_obj->mm_list); 567 list_add_tail(&msm_obj->mm_list, &gpu->active_list); 568 } 569 570 void msm_gem_move_to_inactive(struct drm_gem_object *obj) 571 { 572 struct drm_device *dev = obj->dev; 573 struct msm_drm_private *priv = dev->dev_private; 574 struct msm_gem_object *msm_obj = to_msm_bo(obj); 575 576 WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 577 578 msm_obj->gpu = NULL; 579 list_del_init(&msm_obj->mm_list); 580 list_add_tail(&msm_obj->mm_list, &priv->inactive_list); 581 } 582 583 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout) 584 { 585 struct msm_gem_object *msm_obj = to_msm_bo(obj); 586 bool write = !!(op & MSM_PREP_WRITE); 587 unsigned long remain = 588 op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout); 589 long ret; 590 591 ret = reservation_object_wait_timeout_rcu(msm_obj->resv, write, 592 true, remain); 593 if (ret == 0) 594 return remain == 0 ? -EBUSY : -ETIMEDOUT; 595 else if (ret < 0) 596 return ret; 597 598 /* TODO cache maintenance */ 599 600 return 0; 601 } 602 603 int msm_gem_cpu_fini(struct drm_gem_object *obj) 604 { 605 /* TODO cache maintenance */ 606 return 0; 607 } 608 609 #ifdef CONFIG_DEBUG_FS 610 static void describe_fence(struct dma_fence *fence, const char *type, 611 struct seq_file *m) 612 { 613 if (!dma_fence_is_signaled(fence)) 614 seq_printf(m, "\t%9s: %s %s seq %u\n", type, 615 fence->ops->get_driver_name(fence), 616 fence->ops->get_timeline_name(fence), 617 fence->seqno); 618 } 619 620 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m) 621 { 622 struct msm_gem_object *msm_obj = to_msm_bo(obj); 623 struct reservation_object *robj = msm_obj->resv; 624 struct reservation_object_list *fobj; 625 struct msm_drm_private *priv = obj->dev->dev_private; 626 struct dma_fence *fence; 627 uint64_t off = drm_vma_node_start(&obj->vma_node); 628 const char *madv; 629 unsigned id; 630 631 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); 632 633 switch (msm_obj->madv) { 634 case __MSM_MADV_PURGED: 635 madv = " purged"; 636 break; 637 case MSM_MADV_DONTNEED: 638 madv = " purgeable"; 639 break; 640 case MSM_MADV_WILLNEED: 641 default: 642 madv = ""; 643 break; 644 } 645 646 seq_printf(m, "%08x: %c %2d (%2d) %08llx %p\t", 647 msm_obj->flags, is_active(msm_obj) ? 'A' : 'I', 648 obj->name, kref_read(&obj->refcount), 649 off, msm_obj->vaddr); 650 651 for (id = 0; id < priv->num_aspaces; id++) 652 seq_printf(m, " %08llx", msm_obj->domain[id].iova); 653 654 seq_printf(m, " %zu%s\n", obj->size, madv); 655 656 rcu_read_lock(); 657 fobj = rcu_dereference(robj->fence); 658 if (fobj) { 659 unsigned int i, shared_count = fobj->shared_count; 660 661 for (i = 0; i < shared_count; i++) { 662 fence = rcu_dereference(fobj->shared[i]); 663 describe_fence(fence, "Shared", m); 664 } 665 } 666 667 fence = rcu_dereference(robj->fence_excl); 668 if (fence) 669 describe_fence(fence, "Exclusive", m); 670 rcu_read_unlock(); 671 } 672 673 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m) 674 { 675 struct msm_gem_object *msm_obj; 676 int count = 0; 677 size_t size = 0; 678 679 list_for_each_entry(msm_obj, list, mm_list) { 680 struct drm_gem_object *obj = &msm_obj->base; 681 seq_printf(m, " "); 682 msm_gem_describe(obj, m); 683 count++; 684 size += obj->size; 685 } 686 687 seq_printf(m, "Total %d objects, %zu bytes\n", count, size); 688 } 689 #endif 690 691 void msm_gem_free_object(struct drm_gem_object *obj) 692 { 693 struct drm_device *dev = obj->dev; 694 struct msm_gem_object *msm_obj = to_msm_bo(obj); 695 696 WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 697 698 /* object should not be on active list: */ 699 WARN_ON(is_active(msm_obj)); 700 701 list_del(&msm_obj->mm_list); 702 703 put_iova(obj); 704 705 if (obj->import_attach) { 706 if (msm_obj->vaddr) 707 dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr); 708 709 /* Don't drop the pages for imported dmabuf, as they are not 710 * ours, just free the array we allocated: 711 */ 712 if (msm_obj->pages) 713 kvfree(msm_obj->pages); 714 715 drm_prime_gem_destroy(obj, msm_obj->sgt); 716 } else { 717 msm_gem_vunmap(obj); 718 put_pages(obj); 719 } 720 721 if (msm_obj->resv == &msm_obj->_resv) 722 reservation_object_fini(msm_obj->resv); 723 724 drm_gem_object_release(obj); 725 726 kfree(msm_obj); 727 } 728 729 /* convenience method to construct a GEM buffer object, and userspace handle */ 730 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, 731 uint32_t size, uint32_t flags, uint32_t *handle) 732 { 733 struct drm_gem_object *obj; 734 int ret; 735 736 ret = mutex_lock_interruptible(&dev->struct_mutex); 737 if (ret) 738 return ret; 739 740 obj = msm_gem_new(dev, size, flags); 741 742 mutex_unlock(&dev->struct_mutex); 743 744 if (IS_ERR(obj)) 745 return PTR_ERR(obj); 746 747 ret = drm_gem_handle_create(file, obj, handle); 748 749 /* drop reference from allocate - handle holds it now */ 750 drm_gem_object_unreference_unlocked(obj); 751 752 return ret; 753 } 754 755 static int msm_gem_new_impl(struct drm_device *dev, 756 uint32_t size, uint32_t flags, 757 struct reservation_object *resv, 758 struct drm_gem_object **obj) 759 { 760 struct msm_drm_private *priv = dev->dev_private; 761 struct msm_gem_object *msm_obj; 762 bool use_vram = false; 763 764 WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 765 766 switch (flags & MSM_BO_CACHE_MASK) { 767 case MSM_BO_UNCACHED: 768 case MSM_BO_CACHED: 769 case MSM_BO_WC: 770 break; 771 default: 772 dev_err(dev->dev, "invalid cache flag: %x\n", 773 (flags & MSM_BO_CACHE_MASK)); 774 return -EINVAL; 775 } 776 777 if (!iommu_present(&platform_bus_type)) 778 use_vram = true; 779 else if ((flags & MSM_BO_STOLEN) && priv->vram.size) 780 use_vram = true; 781 782 if (WARN_ON(use_vram && !priv->vram.size)) 783 return -EINVAL; 784 785 msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL); 786 if (!msm_obj) 787 return -ENOMEM; 788 789 if (use_vram) 790 msm_obj->vram_node = &msm_obj->domain[0].node; 791 792 msm_obj->flags = flags; 793 msm_obj->madv = MSM_MADV_WILLNEED; 794 795 if (resv) { 796 msm_obj->resv = resv; 797 } else { 798 msm_obj->resv = &msm_obj->_resv; 799 reservation_object_init(msm_obj->resv); 800 } 801 802 INIT_LIST_HEAD(&msm_obj->submit_entry); 803 list_add_tail(&msm_obj->mm_list, &priv->inactive_list); 804 805 *obj = &msm_obj->base; 806 807 return 0; 808 } 809 810 struct drm_gem_object *msm_gem_new(struct drm_device *dev, 811 uint32_t size, uint32_t flags) 812 { 813 struct drm_gem_object *obj = NULL; 814 int ret; 815 816 WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 817 818 size = PAGE_ALIGN(size); 819 820 /* Disallow zero sized objects as they make the underlying 821 * infrastructure grumpy 822 */ 823 if (size == 0) 824 return ERR_PTR(-EINVAL); 825 826 ret = msm_gem_new_impl(dev, size, flags, NULL, &obj); 827 if (ret) 828 goto fail; 829 830 if (use_pages(obj)) { 831 ret = drm_gem_object_init(dev, obj, size); 832 if (ret) 833 goto fail; 834 } else { 835 drm_gem_private_object_init(dev, obj, size); 836 } 837 838 return obj; 839 840 fail: 841 drm_gem_object_unreference(obj); 842 return ERR_PTR(ret); 843 } 844 845 struct drm_gem_object *msm_gem_import(struct drm_device *dev, 846 struct dma_buf *dmabuf, struct sg_table *sgt) 847 { 848 struct msm_gem_object *msm_obj; 849 struct drm_gem_object *obj; 850 uint32_t size; 851 int ret, npages; 852 853 /* if we don't have IOMMU, don't bother pretending we can import: */ 854 if (!iommu_present(&platform_bus_type)) { 855 dev_err(dev->dev, "cannot import without IOMMU\n"); 856 return ERR_PTR(-EINVAL); 857 } 858 859 size = PAGE_ALIGN(dmabuf->size); 860 861 /* Take mutex so we can modify the inactive list in msm_gem_new_impl */ 862 mutex_lock(&dev->struct_mutex); 863 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, dmabuf->resv, &obj); 864 mutex_unlock(&dev->struct_mutex); 865 866 if (ret) 867 goto fail; 868 869 drm_gem_private_object_init(dev, obj, size); 870 871 npages = size / PAGE_SIZE; 872 873 msm_obj = to_msm_bo(obj); 874 msm_obj->sgt = sgt; 875 msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 876 if (!msm_obj->pages) { 877 ret = -ENOMEM; 878 goto fail; 879 } 880 881 ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages); 882 if (ret) 883 goto fail; 884 885 return obj; 886 887 fail: 888 drm_gem_object_unreference_unlocked(obj); 889 return ERR_PTR(ret); 890 } 891