1 /* 2 * Copyright (C) 2015 Etnaviv Project 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 as published by 6 * the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #include <linux/spinlock.h> 18 #include <linux/shmem_fs.h> 19 #include <linux/sched/mm.h> 20 21 #include "etnaviv_drv.h" 22 #include "etnaviv_gem.h" 23 #include "etnaviv_gpu.h" 24 #include "etnaviv_mmu.h" 25 26 static void etnaviv_gem_scatter_map(struct etnaviv_gem_object *etnaviv_obj) 27 { 28 struct drm_device *dev = etnaviv_obj->base.dev; 29 struct sg_table *sgt = etnaviv_obj->sgt; 30 31 /* 32 * For non-cached buffers, ensure the new pages are clean 33 * because display controller, GPU, etc. are not coherent. 34 */ 35 if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK) 36 dma_map_sg(dev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL); 37 } 38 39 static void etnaviv_gem_scatterlist_unmap(struct etnaviv_gem_object *etnaviv_obj) 40 { 41 struct drm_device *dev = etnaviv_obj->base.dev; 42 struct sg_table *sgt = etnaviv_obj->sgt; 43 44 /* 45 * For non-cached buffers, ensure the new pages are clean 46 * because display controller, GPU, etc. are not coherent: 47 * 48 * WARNING: The DMA API does not support concurrent CPU 49 * and device access to the memory area. With BIDIRECTIONAL, 50 * we will clean the cache lines which overlap the region, 51 * and invalidate all cache lines (partially) contained in 52 * the region. 53 * 54 * If you have dirty data in the overlapping cache lines, 55 * that will corrupt the GPU-written data. If you have 56 * written into the remainder of the region, this can 57 * discard those writes. 58 */ 59 if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK) 60 dma_unmap_sg(dev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL); 61 } 62 63 /* called with etnaviv_obj->lock held */ 64 static int etnaviv_gem_shmem_get_pages(struct etnaviv_gem_object *etnaviv_obj) 65 { 66 struct drm_device *dev = etnaviv_obj->base.dev; 67 struct page **p = drm_gem_get_pages(&etnaviv_obj->base); 68 69 if (IS_ERR(p)) { 70 dev_err(dev->dev, "could not get pages: %ld\n", PTR_ERR(p)); 71 return PTR_ERR(p); 72 } 73 74 etnaviv_obj->pages = p; 75 76 return 0; 77 } 78 79 static void put_pages(struct etnaviv_gem_object *etnaviv_obj) 80 { 81 if (etnaviv_obj->sgt) { 82 etnaviv_gem_scatterlist_unmap(etnaviv_obj); 83 sg_free_table(etnaviv_obj->sgt); 84 kfree(etnaviv_obj->sgt); 85 etnaviv_obj->sgt = NULL; 86 } 87 if (etnaviv_obj->pages) { 88 drm_gem_put_pages(&etnaviv_obj->base, etnaviv_obj->pages, 89 true, false); 90 91 etnaviv_obj->pages = NULL; 92 } 93 } 94 95 struct page **etnaviv_gem_get_pages(struct etnaviv_gem_object *etnaviv_obj) 96 { 97 int ret; 98 99 lockdep_assert_held(&etnaviv_obj->lock); 100 101 if (!etnaviv_obj->pages) { 102 ret = etnaviv_obj->ops->get_pages(etnaviv_obj); 103 if (ret < 0) 104 return ERR_PTR(ret); 105 } 106 107 if (!etnaviv_obj->sgt) { 108 struct drm_device *dev = etnaviv_obj->base.dev; 109 int npages = etnaviv_obj->base.size >> PAGE_SHIFT; 110 struct sg_table *sgt; 111 112 sgt = drm_prime_pages_to_sg(etnaviv_obj->pages, npages); 113 if (IS_ERR(sgt)) { 114 dev_err(dev->dev, "failed to allocate sgt: %ld\n", 115 PTR_ERR(sgt)); 116 return ERR_CAST(sgt); 117 } 118 119 etnaviv_obj->sgt = sgt; 120 121 etnaviv_gem_scatter_map(etnaviv_obj); 122 } 123 124 return etnaviv_obj->pages; 125 } 126 127 void etnaviv_gem_put_pages(struct etnaviv_gem_object *etnaviv_obj) 128 { 129 lockdep_assert_held(&etnaviv_obj->lock); 130 /* when we start tracking the pin count, then do something here */ 131 } 132 133 static int etnaviv_gem_mmap_obj(struct etnaviv_gem_object *etnaviv_obj, 134 struct vm_area_struct *vma) 135 { 136 pgprot_t vm_page_prot; 137 138 vma->vm_flags &= ~VM_PFNMAP; 139 vma->vm_flags |= VM_MIXEDMAP; 140 141 vm_page_prot = vm_get_page_prot(vma->vm_flags); 142 143 if (etnaviv_obj->flags & ETNA_BO_WC) { 144 vma->vm_page_prot = pgprot_writecombine(vm_page_prot); 145 } else if (etnaviv_obj->flags & ETNA_BO_UNCACHED) { 146 vma->vm_page_prot = pgprot_noncached(vm_page_prot); 147 } else { 148 /* 149 * Shunt off cached objs to shmem file so they have their own 150 * address_space (so unmap_mapping_range does what we want, 151 * in particular in the case of mmap'd dmabufs) 152 */ 153 fput(vma->vm_file); 154 get_file(etnaviv_obj->base.filp); 155 vma->vm_pgoff = 0; 156 vma->vm_file = etnaviv_obj->base.filp; 157 158 vma->vm_page_prot = vm_page_prot; 159 } 160 161 return 0; 162 } 163 164 int etnaviv_gem_mmap(struct file *filp, struct vm_area_struct *vma) 165 { 166 struct etnaviv_gem_object *obj; 167 int ret; 168 169 ret = drm_gem_mmap(filp, vma); 170 if (ret) { 171 DBG("mmap failed: %d", ret); 172 return ret; 173 } 174 175 obj = to_etnaviv_bo(vma->vm_private_data); 176 return obj->ops->mmap(obj, vma); 177 } 178 179 int etnaviv_gem_fault(struct vm_fault *vmf) 180 { 181 struct vm_area_struct *vma = vmf->vma; 182 struct drm_gem_object *obj = vma->vm_private_data; 183 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 184 struct page **pages, *page; 185 pgoff_t pgoff; 186 int ret; 187 188 /* 189 * Make sure we don't parallel update on a fault, nor move or remove 190 * something from beneath our feet. Note that vm_insert_page() is 191 * specifically coded to take care of this, so we don't have to. 192 */ 193 ret = mutex_lock_interruptible(&etnaviv_obj->lock); 194 if (ret) 195 goto out; 196 197 /* make sure we have pages attached now */ 198 pages = etnaviv_gem_get_pages(etnaviv_obj); 199 mutex_unlock(&etnaviv_obj->lock); 200 201 if (IS_ERR(pages)) { 202 ret = PTR_ERR(pages); 203 goto out; 204 } 205 206 /* We don't use vmf->pgoff since that has the fake offset: */ 207 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT; 208 209 page = pages[pgoff]; 210 211 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address, 212 page_to_pfn(page), page_to_pfn(page) << PAGE_SHIFT); 213 214 ret = vm_insert_page(vma, vmf->address, page); 215 216 out: 217 switch (ret) { 218 case -EAGAIN: 219 case 0: 220 case -ERESTARTSYS: 221 case -EINTR: 222 case -EBUSY: 223 /* 224 * EBUSY is ok: this just means that another thread 225 * already did the job. 226 */ 227 return VM_FAULT_NOPAGE; 228 case -ENOMEM: 229 return VM_FAULT_OOM; 230 default: 231 return VM_FAULT_SIGBUS; 232 } 233 } 234 235 int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset) 236 { 237 int ret; 238 239 /* Make it mmapable */ 240 ret = drm_gem_create_mmap_offset(obj); 241 if (ret) 242 dev_err(obj->dev->dev, "could not allocate mmap offset\n"); 243 else 244 *offset = drm_vma_node_offset_addr(&obj->vma_node); 245 246 return ret; 247 } 248 249 static struct etnaviv_vram_mapping * 250 etnaviv_gem_get_vram_mapping(struct etnaviv_gem_object *obj, 251 struct etnaviv_iommu *mmu) 252 { 253 struct etnaviv_vram_mapping *mapping; 254 255 list_for_each_entry(mapping, &obj->vram_list, obj_node) { 256 if (mapping->mmu == mmu) 257 return mapping; 258 } 259 260 return NULL; 261 } 262 263 void etnaviv_gem_mapping_reference(struct etnaviv_vram_mapping *mapping) 264 { 265 struct etnaviv_gem_object *etnaviv_obj = mapping->object; 266 267 drm_gem_object_reference(&etnaviv_obj->base); 268 269 mutex_lock(&etnaviv_obj->lock); 270 WARN_ON(mapping->use == 0); 271 mapping->use += 1; 272 mutex_unlock(&etnaviv_obj->lock); 273 } 274 275 void etnaviv_gem_mapping_unreference(struct etnaviv_vram_mapping *mapping) 276 { 277 struct etnaviv_gem_object *etnaviv_obj = mapping->object; 278 279 mutex_lock(&etnaviv_obj->lock); 280 WARN_ON(mapping->use == 0); 281 mapping->use -= 1; 282 mutex_unlock(&etnaviv_obj->lock); 283 284 drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 285 } 286 287 struct etnaviv_vram_mapping *etnaviv_gem_mapping_get( 288 struct drm_gem_object *obj, struct etnaviv_gpu *gpu) 289 { 290 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 291 struct etnaviv_vram_mapping *mapping; 292 struct page **pages; 293 int ret = 0; 294 295 mutex_lock(&etnaviv_obj->lock); 296 mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, gpu->mmu); 297 if (mapping) { 298 /* 299 * Holding the object lock prevents the use count changing 300 * beneath us. If the use count is zero, the MMU might be 301 * reaping this object, so take the lock and re-check that 302 * the MMU owns this mapping to close this race. 303 */ 304 if (mapping->use == 0) { 305 mutex_lock(&gpu->mmu->lock); 306 if (mapping->mmu == gpu->mmu) 307 mapping->use += 1; 308 else 309 mapping = NULL; 310 mutex_unlock(&gpu->mmu->lock); 311 if (mapping) 312 goto out; 313 } else { 314 mapping->use += 1; 315 goto out; 316 } 317 } 318 319 pages = etnaviv_gem_get_pages(etnaviv_obj); 320 if (IS_ERR(pages)) { 321 ret = PTR_ERR(pages); 322 goto out; 323 } 324 325 /* 326 * See if we have a reaped vram mapping we can re-use before 327 * allocating a fresh mapping. 328 */ 329 mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, NULL); 330 if (!mapping) { 331 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 332 if (!mapping) { 333 ret = -ENOMEM; 334 goto out; 335 } 336 337 INIT_LIST_HEAD(&mapping->scan_node); 338 mapping->object = etnaviv_obj; 339 } else { 340 list_del(&mapping->obj_node); 341 } 342 343 mapping->mmu = gpu->mmu; 344 mapping->use = 1; 345 346 ret = etnaviv_iommu_map_gem(gpu->mmu, etnaviv_obj, gpu->memory_base, 347 mapping); 348 if (ret < 0) 349 kfree(mapping); 350 else 351 list_add_tail(&mapping->obj_node, &etnaviv_obj->vram_list); 352 353 out: 354 mutex_unlock(&etnaviv_obj->lock); 355 356 if (ret) 357 return ERR_PTR(ret); 358 359 /* Take a reference on the object */ 360 drm_gem_object_reference(obj); 361 return mapping; 362 } 363 364 void *etnaviv_gem_vmap(struct drm_gem_object *obj) 365 { 366 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 367 368 if (etnaviv_obj->vaddr) 369 return etnaviv_obj->vaddr; 370 371 mutex_lock(&etnaviv_obj->lock); 372 /* 373 * Need to check again, as we might have raced with another thread 374 * while waiting for the mutex. 375 */ 376 if (!etnaviv_obj->vaddr) 377 etnaviv_obj->vaddr = etnaviv_obj->ops->vmap(etnaviv_obj); 378 mutex_unlock(&etnaviv_obj->lock); 379 380 return etnaviv_obj->vaddr; 381 } 382 383 static void *etnaviv_gem_vmap_impl(struct etnaviv_gem_object *obj) 384 { 385 struct page **pages; 386 387 lockdep_assert_held(&obj->lock); 388 389 pages = etnaviv_gem_get_pages(obj); 390 if (IS_ERR(pages)) 391 return NULL; 392 393 return vmap(pages, obj->base.size >> PAGE_SHIFT, 394 VM_MAP, pgprot_writecombine(PAGE_KERNEL)); 395 } 396 397 static inline enum dma_data_direction etnaviv_op_to_dma_dir(u32 op) 398 { 399 if (op & ETNA_PREP_READ) 400 return DMA_FROM_DEVICE; 401 else if (op & ETNA_PREP_WRITE) 402 return DMA_TO_DEVICE; 403 else 404 return DMA_BIDIRECTIONAL; 405 } 406 407 int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op, 408 struct timespec *timeout) 409 { 410 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 411 struct drm_device *dev = obj->dev; 412 bool write = !!(op & ETNA_PREP_WRITE); 413 unsigned long remain = 414 op & ETNA_PREP_NOSYNC ? 0 : etnaviv_timeout_to_jiffies(timeout); 415 long lret; 416 417 lret = reservation_object_wait_timeout_rcu(etnaviv_obj->resv, 418 write, true, remain); 419 if (lret < 0) 420 return lret; 421 else if (lret == 0) 422 return remain == 0 ? -EBUSY : -ETIMEDOUT; 423 424 if (etnaviv_obj->flags & ETNA_BO_CACHED) { 425 if (!etnaviv_obj->sgt) { 426 void *ret; 427 428 mutex_lock(&etnaviv_obj->lock); 429 ret = etnaviv_gem_get_pages(etnaviv_obj); 430 mutex_unlock(&etnaviv_obj->lock); 431 if (IS_ERR(ret)) 432 return PTR_ERR(ret); 433 } 434 435 dma_sync_sg_for_cpu(dev->dev, etnaviv_obj->sgt->sgl, 436 etnaviv_obj->sgt->nents, 437 etnaviv_op_to_dma_dir(op)); 438 etnaviv_obj->last_cpu_prep_op = op; 439 } 440 441 return 0; 442 } 443 444 int etnaviv_gem_cpu_fini(struct drm_gem_object *obj) 445 { 446 struct drm_device *dev = obj->dev; 447 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 448 449 if (etnaviv_obj->flags & ETNA_BO_CACHED) { 450 /* fini without a prep is almost certainly a userspace error */ 451 WARN_ON(etnaviv_obj->last_cpu_prep_op == 0); 452 dma_sync_sg_for_device(dev->dev, etnaviv_obj->sgt->sgl, 453 etnaviv_obj->sgt->nents, 454 etnaviv_op_to_dma_dir(etnaviv_obj->last_cpu_prep_op)); 455 etnaviv_obj->last_cpu_prep_op = 0; 456 } 457 458 return 0; 459 } 460 461 int etnaviv_gem_wait_bo(struct etnaviv_gpu *gpu, struct drm_gem_object *obj, 462 struct timespec *timeout) 463 { 464 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 465 466 return etnaviv_gpu_wait_obj_inactive(gpu, etnaviv_obj, timeout); 467 } 468 469 #ifdef CONFIG_DEBUG_FS 470 static void etnaviv_gem_describe_fence(struct dma_fence *fence, 471 const char *type, struct seq_file *m) 472 { 473 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 474 seq_printf(m, "\t%9s: %s %s seq %u\n", 475 type, 476 fence->ops->get_driver_name(fence), 477 fence->ops->get_timeline_name(fence), 478 fence->seqno); 479 } 480 481 static void etnaviv_gem_describe(struct drm_gem_object *obj, struct seq_file *m) 482 { 483 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 484 struct reservation_object *robj = etnaviv_obj->resv; 485 struct reservation_object_list *fobj; 486 struct dma_fence *fence; 487 unsigned long off = drm_vma_node_start(&obj->vma_node); 488 489 seq_printf(m, "%08x: %c %2d (%2d) %08lx %p %zd\n", 490 etnaviv_obj->flags, is_active(etnaviv_obj) ? 'A' : 'I', 491 obj->name, kref_read(&obj->refcount), 492 off, etnaviv_obj->vaddr, obj->size); 493 494 rcu_read_lock(); 495 fobj = rcu_dereference(robj->fence); 496 if (fobj) { 497 unsigned int i, shared_count = fobj->shared_count; 498 499 for (i = 0; i < shared_count; i++) { 500 fence = rcu_dereference(fobj->shared[i]); 501 etnaviv_gem_describe_fence(fence, "Shared", m); 502 } 503 } 504 505 fence = rcu_dereference(robj->fence_excl); 506 if (fence) 507 etnaviv_gem_describe_fence(fence, "Exclusive", m); 508 rcu_read_unlock(); 509 } 510 511 void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv, 512 struct seq_file *m) 513 { 514 struct etnaviv_gem_object *etnaviv_obj; 515 int count = 0; 516 size_t size = 0; 517 518 mutex_lock(&priv->gem_lock); 519 list_for_each_entry(etnaviv_obj, &priv->gem_list, gem_node) { 520 struct drm_gem_object *obj = &etnaviv_obj->base; 521 522 seq_puts(m, " "); 523 etnaviv_gem_describe(obj, m); 524 count++; 525 size += obj->size; 526 } 527 mutex_unlock(&priv->gem_lock); 528 529 seq_printf(m, "Total %d objects, %zu bytes\n", count, size); 530 } 531 #endif 532 533 static void etnaviv_gem_shmem_release(struct etnaviv_gem_object *etnaviv_obj) 534 { 535 vunmap(etnaviv_obj->vaddr); 536 put_pages(etnaviv_obj); 537 } 538 539 static const struct etnaviv_gem_ops etnaviv_gem_shmem_ops = { 540 .get_pages = etnaviv_gem_shmem_get_pages, 541 .release = etnaviv_gem_shmem_release, 542 .vmap = etnaviv_gem_vmap_impl, 543 .mmap = etnaviv_gem_mmap_obj, 544 }; 545 546 void etnaviv_gem_free_object(struct drm_gem_object *obj) 547 { 548 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 549 struct etnaviv_vram_mapping *mapping, *tmp; 550 551 /* object should not be active */ 552 WARN_ON(is_active(etnaviv_obj)); 553 554 list_del(&etnaviv_obj->gem_node); 555 556 list_for_each_entry_safe(mapping, tmp, &etnaviv_obj->vram_list, 557 obj_node) { 558 struct etnaviv_iommu *mmu = mapping->mmu; 559 560 WARN_ON(mapping->use); 561 562 if (mmu) 563 etnaviv_iommu_unmap_gem(mmu, mapping); 564 565 list_del(&mapping->obj_node); 566 kfree(mapping); 567 } 568 569 drm_gem_free_mmap_offset(obj); 570 etnaviv_obj->ops->release(etnaviv_obj); 571 if (etnaviv_obj->resv == &etnaviv_obj->_resv) 572 reservation_object_fini(&etnaviv_obj->_resv); 573 drm_gem_object_release(obj); 574 575 kfree(etnaviv_obj); 576 } 577 578 int etnaviv_gem_obj_add(struct drm_device *dev, struct drm_gem_object *obj) 579 { 580 struct etnaviv_drm_private *priv = dev->dev_private; 581 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 582 583 mutex_lock(&priv->gem_lock); 584 list_add_tail(&etnaviv_obj->gem_node, &priv->gem_list); 585 mutex_unlock(&priv->gem_lock); 586 587 return 0; 588 } 589 590 static int etnaviv_gem_new_impl(struct drm_device *dev, u32 size, u32 flags, 591 struct reservation_object *robj, const struct etnaviv_gem_ops *ops, 592 struct drm_gem_object **obj) 593 { 594 struct etnaviv_gem_object *etnaviv_obj; 595 unsigned sz = sizeof(*etnaviv_obj); 596 bool valid = true; 597 598 /* validate flags */ 599 switch (flags & ETNA_BO_CACHE_MASK) { 600 case ETNA_BO_UNCACHED: 601 case ETNA_BO_CACHED: 602 case ETNA_BO_WC: 603 break; 604 default: 605 valid = false; 606 } 607 608 if (!valid) { 609 dev_err(dev->dev, "invalid cache flag: %x\n", 610 (flags & ETNA_BO_CACHE_MASK)); 611 return -EINVAL; 612 } 613 614 etnaviv_obj = kzalloc(sz, GFP_KERNEL); 615 if (!etnaviv_obj) 616 return -ENOMEM; 617 618 etnaviv_obj->flags = flags; 619 etnaviv_obj->ops = ops; 620 if (robj) { 621 etnaviv_obj->resv = robj; 622 } else { 623 etnaviv_obj->resv = &etnaviv_obj->_resv; 624 reservation_object_init(&etnaviv_obj->_resv); 625 } 626 627 mutex_init(&etnaviv_obj->lock); 628 INIT_LIST_HEAD(&etnaviv_obj->vram_list); 629 630 *obj = &etnaviv_obj->base; 631 632 return 0; 633 } 634 635 static struct drm_gem_object *__etnaviv_gem_new(struct drm_device *dev, 636 u32 size, u32 flags) 637 { 638 struct drm_gem_object *obj = NULL; 639 int ret; 640 641 size = PAGE_ALIGN(size); 642 643 ret = etnaviv_gem_new_impl(dev, size, flags, NULL, 644 &etnaviv_gem_shmem_ops, &obj); 645 if (ret) 646 goto fail; 647 648 ret = drm_gem_object_init(dev, obj, size); 649 if (ret == 0) { 650 struct address_space *mapping; 651 652 /* 653 * Our buffers are kept pinned, so allocating them 654 * from the MOVABLE zone is a really bad idea, and 655 * conflicts with CMA. See coments above new_inode() 656 * why this is required _and_ expected if you're 657 * going to pin these pages. 658 */ 659 mapping = obj->filp->f_mapping; 660 mapping_set_gfp_mask(mapping, GFP_HIGHUSER); 661 } 662 663 if (ret) 664 goto fail; 665 666 return obj; 667 668 fail: 669 drm_gem_object_unreference_unlocked(obj); 670 return ERR_PTR(ret); 671 } 672 673 /* convenience method to construct a GEM buffer object, and userspace handle */ 674 int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file, 675 u32 size, u32 flags, u32 *handle) 676 { 677 struct drm_gem_object *obj; 678 int ret; 679 680 obj = __etnaviv_gem_new(dev, size, flags); 681 if (IS_ERR(obj)) 682 return PTR_ERR(obj); 683 684 ret = etnaviv_gem_obj_add(dev, obj); 685 if (ret < 0) { 686 drm_gem_object_unreference_unlocked(obj); 687 return ret; 688 } 689 690 ret = drm_gem_handle_create(file, obj, handle); 691 692 /* drop reference from allocate - handle holds it now */ 693 drm_gem_object_unreference_unlocked(obj); 694 695 return ret; 696 } 697 698 struct drm_gem_object *etnaviv_gem_new(struct drm_device *dev, 699 u32 size, u32 flags) 700 { 701 struct drm_gem_object *obj; 702 int ret; 703 704 obj = __etnaviv_gem_new(dev, size, flags); 705 if (IS_ERR(obj)) 706 return obj; 707 708 ret = etnaviv_gem_obj_add(dev, obj); 709 if (ret < 0) { 710 drm_gem_object_unreference_unlocked(obj); 711 return ERR_PTR(ret); 712 } 713 714 return obj; 715 } 716 717 int etnaviv_gem_new_private(struct drm_device *dev, size_t size, u32 flags, 718 struct reservation_object *robj, const struct etnaviv_gem_ops *ops, 719 struct etnaviv_gem_object **res) 720 { 721 struct drm_gem_object *obj; 722 int ret; 723 724 ret = etnaviv_gem_new_impl(dev, size, flags, robj, ops, &obj); 725 if (ret) 726 return ret; 727 728 drm_gem_private_object_init(dev, obj, size); 729 730 *res = to_etnaviv_bo(obj); 731 732 return 0; 733 } 734 735 struct get_pages_work { 736 struct work_struct work; 737 struct mm_struct *mm; 738 struct task_struct *task; 739 struct etnaviv_gem_object *etnaviv_obj; 740 }; 741 742 static struct page **etnaviv_gem_userptr_do_get_pages( 743 struct etnaviv_gem_object *etnaviv_obj, struct mm_struct *mm, struct task_struct *task) 744 { 745 int ret = 0, pinned, npages = etnaviv_obj->base.size >> PAGE_SHIFT; 746 struct page **pvec; 747 uintptr_t ptr; 748 unsigned int flags = 0; 749 750 pvec = drm_malloc_ab(npages, sizeof(struct page *)); 751 if (!pvec) 752 return ERR_PTR(-ENOMEM); 753 754 if (!etnaviv_obj->userptr.ro) 755 flags |= FOLL_WRITE; 756 757 pinned = 0; 758 ptr = etnaviv_obj->userptr.ptr; 759 760 down_read(&mm->mmap_sem); 761 while (pinned < npages) { 762 ret = get_user_pages_remote(task, mm, ptr, npages - pinned, 763 flags, pvec + pinned, NULL, NULL); 764 if (ret < 0) 765 break; 766 767 ptr += ret * PAGE_SIZE; 768 pinned += ret; 769 } 770 up_read(&mm->mmap_sem); 771 772 if (ret < 0) { 773 release_pages(pvec, pinned, 0); 774 drm_free_large(pvec); 775 return ERR_PTR(ret); 776 } 777 778 return pvec; 779 } 780 781 static void __etnaviv_gem_userptr_get_pages(struct work_struct *_work) 782 { 783 struct get_pages_work *work = container_of(_work, typeof(*work), work); 784 struct etnaviv_gem_object *etnaviv_obj = work->etnaviv_obj; 785 struct page **pvec; 786 787 pvec = etnaviv_gem_userptr_do_get_pages(etnaviv_obj, work->mm, work->task); 788 789 mutex_lock(&etnaviv_obj->lock); 790 if (IS_ERR(pvec)) { 791 etnaviv_obj->userptr.work = ERR_CAST(pvec); 792 } else { 793 etnaviv_obj->userptr.work = NULL; 794 etnaviv_obj->pages = pvec; 795 } 796 797 mutex_unlock(&etnaviv_obj->lock); 798 drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 799 800 mmput(work->mm); 801 put_task_struct(work->task); 802 kfree(work); 803 } 804 805 static int etnaviv_gem_userptr_get_pages(struct etnaviv_gem_object *etnaviv_obj) 806 { 807 struct page **pvec = NULL; 808 struct get_pages_work *work; 809 struct mm_struct *mm; 810 int ret, pinned, npages = etnaviv_obj->base.size >> PAGE_SHIFT; 811 812 if (etnaviv_obj->userptr.work) { 813 if (IS_ERR(etnaviv_obj->userptr.work)) { 814 ret = PTR_ERR(etnaviv_obj->userptr.work); 815 etnaviv_obj->userptr.work = NULL; 816 } else { 817 ret = -EAGAIN; 818 } 819 return ret; 820 } 821 822 mm = get_task_mm(etnaviv_obj->userptr.task); 823 pinned = 0; 824 if (mm == current->mm) { 825 pvec = drm_malloc_ab(npages, sizeof(struct page *)); 826 if (!pvec) { 827 mmput(mm); 828 return -ENOMEM; 829 } 830 831 pinned = __get_user_pages_fast(etnaviv_obj->userptr.ptr, npages, 832 !etnaviv_obj->userptr.ro, pvec); 833 if (pinned < 0) { 834 drm_free_large(pvec); 835 mmput(mm); 836 return pinned; 837 } 838 839 if (pinned == npages) { 840 etnaviv_obj->pages = pvec; 841 mmput(mm); 842 return 0; 843 } 844 } 845 846 release_pages(pvec, pinned, 0); 847 drm_free_large(pvec); 848 849 work = kmalloc(sizeof(*work), GFP_KERNEL); 850 if (!work) { 851 mmput(mm); 852 return -ENOMEM; 853 } 854 855 get_task_struct(current); 856 drm_gem_object_reference(&etnaviv_obj->base); 857 858 work->mm = mm; 859 work->task = current; 860 work->etnaviv_obj = etnaviv_obj; 861 862 etnaviv_obj->userptr.work = &work->work; 863 INIT_WORK(&work->work, __etnaviv_gem_userptr_get_pages); 864 865 etnaviv_queue_work(etnaviv_obj->base.dev, &work->work); 866 867 return -EAGAIN; 868 } 869 870 static void etnaviv_gem_userptr_release(struct etnaviv_gem_object *etnaviv_obj) 871 { 872 if (etnaviv_obj->sgt) { 873 etnaviv_gem_scatterlist_unmap(etnaviv_obj); 874 sg_free_table(etnaviv_obj->sgt); 875 kfree(etnaviv_obj->sgt); 876 } 877 if (etnaviv_obj->pages) { 878 int npages = etnaviv_obj->base.size >> PAGE_SHIFT; 879 880 release_pages(etnaviv_obj->pages, npages, 0); 881 drm_free_large(etnaviv_obj->pages); 882 } 883 put_task_struct(etnaviv_obj->userptr.task); 884 } 885 886 static int etnaviv_gem_userptr_mmap_obj(struct etnaviv_gem_object *etnaviv_obj, 887 struct vm_area_struct *vma) 888 { 889 return -EINVAL; 890 } 891 892 static const struct etnaviv_gem_ops etnaviv_gem_userptr_ops = { 893 .get_pages = etnaviv_gem_userptr_get_pages, 894 .release = etnaviv_gem_userptr_release, 895 .vmap = etnaviv_gem_vmap_impl, 896 .mmap = etnaviv_gem_userptr_mmap_obj, 897 }; 898 899 int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file, 900 uintptr_t ptr, u32 size, u32 flags, u32 *handle) 901 { 902 struct etnaviv_gem_object *etnaviv_obj; 903 int ret; 904 905 ret = etnaviv_gem_new_private(dev, size, ETNA_BO_CACHED, NULL, 906 &etnaviv_gem_userptr_ops, &etnaviv_obj); 907 if (ret) 908 return ret; 909 910 etnaviv_obj->userptr.ptr = ptr; 911 etnaviv_obj->userptr.task = current; 912 etnaviv_obj->userptr.ro = !(flags & ETNA_USERPTR_WRITE); 913 get_task_struct(current); 914 915 ret = etnaviv_gem_obj_add(dev, &etnaviv_obj->base); 916 if (ret) 917 goto unreference; 918 919 ret = drm_gem_handle_create(file, &etnaviv_obj->base, handle); 920 unreference: 921 /* drop reference from allocate - handle holds it now */ 922 drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 923 return ret; 924 } 925