Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / gpu / drm / msm / msm_gem.c
blob795660e29b2ceff71358894d2139c6eb9065eef7
1 /*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
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.
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.
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/>.
18 #include <linux/spinlock.h>
19 #include <linux/shmem_fs.h>
20 #include <linux/dma-buf.h>
21 #include <linux/pfn_t.h>
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"
29 static dma_addr_t physaddr(struct drm_gem_object *obj)
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;
37 static bool use_pages(struct drm_gem_object *obj)
39 struct msm_gem_object *msm_obj = to_msm_bo(obj);
40 return !msm_obj->vram_node;
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)
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;
53 p = drm_malloc_ab(npages, sizeof(struct page *));
54 if (!p)
55 return ERR_PTR(-ENOMEM);
57 ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node,
58 npages, 0, DRM_MM_SEARCH_DEFAULT);
59 if (ret) {
60 drm_free_large(p);
61 return ERR_PTR(ret);
64 paddr = physaddr(obj);
65 for (i = 0; i < npages; i++) {
66 p[i] = phys_to_page(paddr);
67 paddr += PAGE_SIZE;
70 return p;
73 /* called with dev->struct_mutex held */
74 static struct page **get_pages(struct drm_gem_object *obj)
76 struct msm_gem_object *msm_obj = to_msm_bo(obj);
78 if (!msm_obj->pages) {
79 struct drm_device *dev = obj->dev;
80 struct page **p;
81 int npages = obj->size >> PAGE_SHIFT;
83 if (use_pages(obj))
84 p = drm_gem_get_pages(obj);
85 else
86 p = get_pages_vram(obj, npages);
88 if (IS_ERR(p)) {
89 dev_err(dev->dev, "could not get pages: %ld\n",
90 PTR_ERR(p));
91 return p;
94 msm_obj->pages = p;
96 msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
97 if (IS_ERR(msm_obj->sgt)) {
98 void *ptr = ERR_CAST(msm_obj->sgt);
100 dev_err(dev->dev, "failed to allocate sgt\n");
101 msm_obj->sgt = NULL;
102 return ptr;
105 /* For non-cached buffers, ensure the new pages are clean
106 * because display controller, GPU, etc. are not coherent:
108 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
109 dma_map_sg(dev->dev, msm_obj->sgt->sgl,
110 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
113 return msm_obj->pages;
116 static void put_pages(struct drm_gem_object *obj)
118 struct msm_gem_object *msm_obj = to_msm_bo(obj);
120 if (msm_obj->pages) {
121 if (msm_obj->sgt) {
122 /* For non-cached buffers, ensure the new
123 * pages are clean because display controller,
124 * GPU, etc. are not coherent:
126 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
127 dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl,
128 msm_obj->sgt->nents,
129 DMA_BIDIRECTIONAL);
131 sg_free_table(msm_obj->sgt);
132 kfree(msm_obj->sgt);
135 if (use_pages(obj))
136 drm_gem_put_pages(obj, msm_obj->pages, true, false);
137 else {
138 drm_mm_remove_node(msm_obj->vram_node);
139 drm_free_large(msm_obj->pages);
142 msm_obj->pages = NULL;
146 struct page **msm_gem_get_pages(struct drm_gem_object *obj)
148 struct drm_device *dev = obj->dev;
149 struct page **p;
150 mutex_lock(&dev->struct_mutex);
151 p = get_pages(obj);
152 mutex_unlock(&dev->struct_mutex);
153 return p;
156 void msm_gem_put_pages(struct drm_gem_object *obj)
158 /* when we start tracking the pin count, then do something here */
161 int msm_gem_mmap_obj(struct drm_gem_object *obj,
162 struct vm_area_struct *vma)
164 struct msm_gem_object *msm_obj = to_msm_bo(obj);
166 vma->vm_flags &= ~VM_PFNMAP;
167 vma->vm_flags |= VM_MIXEDMAP;
169 if (msm_obj->flags & MSM_BO_WC) {
170 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
171 } else if (msm_obj->flags & MSM_BO_UNCACHED) {
172 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
173 } else {
175 * Shunt off cached objs to shmem file so they have their own
176 * address_space (so unmap_mapping_range does what we want,
177 * in particular in the case of mmap'd dmabufs)
179 fput(vma->vm_file);
180 get_file(obj->filp);
181 vma->vm_pgoff = 0;
182 vma->vm_file = obj->filp;
184 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
187 return 0;
190 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
192 int ret;
194 ret = drm_gem_mmap(filp, vma);
195 if (ret) {
196 DBG("mmap failed: %d", ret);
197 return ret;
200 return msm_gem_mmap_obj(vma->vm_private_data, vma);
203 int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
205 struct drm_gem_object *obj = vma->vm_private_data;
206 struct drm_device *dev = obj->dev;
207 struct msm_drm_private *priv = dev->dev_private;
208 struct page **pages;
209 unsigned long pfn;
210 pgoff_t pgoff;
211 int ret;
213 /* This should only happen if userspace tries to pass a mmap'd
214 * but unfaulted gem bo vaddr into submit ioctl, triggering
215 * a page fault while struct_mutex is already held. This is
216 * not a valid use-case so just bail.
218 if (priv->struct_mutex_task == current)
219 return VM_FAULT_SIGBUS;
221 /* Make sure we don't parallel update on a fault, nor move or remove
222 * something from beneath our feet
224 ret = mutex_lock_interruptible(&dev->struct_mutex);
225 if (ret)
226 goto out;
228 /* make sure we have pages attached now */
229 pages = get_pages(obj);
230 if (IS_ERR(pages)) {
231 ret = PTR_ERR(pages);
232 goto out_unlock;
235 /* We don't use vmf->pgoff since that has the fake offset: */
236 pgoff = ((unsigned long)vmf->virtual_address -
237 vma->vm_start) >> PAGE_SHIFT;
239 pfn = page_to_pfn(pages[pgoff]);
241 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
242 pfn, pfn << PAGE_SHIFT);
244 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
245 __pfn_to_pfn_t(pfn, PFN_DEV));
247 out_unlock:
248 mutex_unlock(&dev->struct_mutex);
249 out:
250 switch (ret) {
251 case -EAGAIN:
252 case 0:
253 case -ERESTARTSYS:
254 case -EINTR:
255 case -EBUSY:
257 * EBUSY is ok: this just means that another thread
258 * already did the job.
260 return VM_FAULT_NOPAGE;
261 case -ENOMEM:
262 return VM_FAULT_OOM;
263 default:
264 return VM_FAULT_SIGBUS;
268 /** get mmap offset */
269 static uint64_t mmap_offset(struct drm_gem_object *obj)
271 struct drm_device *dev = obj->dev;
272 int ret;
274 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
276 /* Make it mmapable */
277 ret = drm_gem_create_mmap_offset(obj);
279 if (ret) {
280 dev_err(dev->dev, "could not allocate mmap offset\n");
281 return 0;
284 return drm_vma_node_offset_addr(&obj->vma_node);
287 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
289 uint64_t offset;
290 mutex_lock(&obj->dev->struct_mutex);
291 offset = mmap_offset(obj);
292 mutex_unlock(&obj->dev->struct_mutex);
293 return offset;
296 static void
297 put_iova(struct drm_gem_object *obj)
299 struct drm_device *dev = obj->dev;
300 struct msm_drm_private *priv = obj->dev->dev_private;
301 struct msm_gem_object *msm_obj = to_msm_bo(obj);
302 int id;
304 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
306 for (id = 0; id < ARRAY_SIZE(msm_obj->domain); id++) {
307 struct msm_mmu *mmu = priv->mmus[id];
308 if (mmu && msm_obj->domain[id].iova) {
309 uint32_t offset = msm_obj->domain[id].iova;
310 mmu->funcs->unmap(mmu, offset, msm_obj->sgt, obj->size);
311 msm_obj->domain[id].iova = 0;
316 /* should be called under struct_mutex.. although it can be called
317 * from atomic context without struct_mutex to acquire an extra
318 * iova ref if you know one is already held.
320 * That means when I do eventually need to add support for unpinning
321 * the refcnt counter needs to be atomic_t.
323 int msm_gem_get_iova_locked(struct drm_gem_object *obj, int id,
324 uint32_t *iova)
326 struct msm_gem_object *msm_obj = to_msm_bo(obj);
327 int ret = 0;
329 if (!msm_obj->domain[id].iova) {
330 struct msm_drm_private *priv = obj->dev->dev_private;
331 struct page **pages = get_pages(obj);
333 if (IS_ERR(pages))
334 return PTR_ERR(pages);
336 if (iommu_present(&platform_bus_type)) {
337 struct msm_mmu *mmu = priv->mmus[id];
338 uint32_t offset;
340 if (WARN_ON(!mmu))
341 return -EINVAL;
343 offset = (uint32_t)mmap_offset(obj);
344 ret = mmu->funcs->map(mmu, offset, msm_obj->sgt,
345 obj->size, IOMMU_READ | IOMMU_WRITE);
346 msm_obj->domain[id].iova = offset;
347 } else {
348 msm_obj->domain[id].iova = physaddr(obj);
352 if (!ret)
353 *iova = msm_obj->domain[id].iova;
355 return ret;
358 /* get iova, taking a reference. Should have a matching put */
359 int msm_gem_get_iova(struct drm_gem_object *obj, int id, uint32_t *iova)
361 struct msm_gem_object *msm_obj = to_msm_bo(obj);
362 int ret;
364 /* this is safe right now because we don't unmap until the
365 * bo is deleted:
367 if (msm_obj->domain[id].iova) {
368 *iova = msm_obj->domain[id].iova;
369 return 0;
372 mutex_lock(&obj->dev->struct_mutex);
373 ret = msm_gem_get_iova_locked(obj, id, iova);
374 mutex_unlock(&obj->dev->struct_mutex);
375 return ret;
378 /* get iova without taking a reference, used in places where you have
379 * already done a 'msm_gem_get_iova()'.
381 uint32_t msm_gem_iova(struct drm_gem_object *obj, int id)
383 struct msm_gem_object *msm_obj = to_msm_bo(obj);
384 WARN_ON(!msm_obj->domain[id].iova);
385 return msm_obj->domain[id].iova;
388 void msm_gem_put_iova(struct drm_gem_object *obj, int id)
390 // XXX TODO ..
391 // NOTE: probably don't need a _locked() version.. we wouldn't
392 // normally unmap here, but instead just mark that it could be
393 // unmapped (if the iova refcnt drops to zero), but then later
394 // if another _get_iova_locked() fails we can start unmapping
395 // things that are no longer needed..
398 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
399 struct drm_mode_create_dumb *args)
401 args->pitch = align_pitch(args->width, args->bpp);
402 args->size = PAGE_ALIGN(args->pitch * args->height);
403 return msm_gem_new_handle(dev, file, args->size,
404 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle);
407 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
408 uint32_t handle, uint64_t *offset)
410 struct drm_gem_object *obj;
411 int ret = 0;
413 /* GEM does all our handle to object mapping */
414 obj = drm_gem_object_lookup(file, handle);
415 if (obj == NULL) {
416 ret = -ENOENT;
417 goto fail;
420 *offset = msm_gem_mmap_offset(obj);
422 drm_gem_object_unreference_unlocked(obj);
424 fail:
425 return ret;
428 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj)
430 struct msm_gem_object *msm_obj = to_msm_bo(obj);
431 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
432 if (!msm_obj->vaddr) {
433 struct page **pages = get_pages(obj);
434 if (IS_ERR(pages))
435 return ERR_CAST(pages);
436 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
437 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
438 if (msm_obj->vaddr == NULL)
439 return ERR_PTR(-ENOMEM);
441 msm_obj->vmap_count++;
442 return msm_obj->vaddr;
445 void *msm_gem_get_vaddr(struct drm_gem_object *obj)
447 void *ret;
448 mutex_lock(&obj->dev->struct_mutex);
449 ret = msm_gem_get_vaddr_locked(obj);
450 mutex_unlock(&obj->dev->struct_mutex);
451 return ret;
454 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj)
456 struct msm_gem_object *msm_obj = to_msm_bo(obj);
457 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
458 WARN_ON(msm_obj->vmap_count < 1);
459 msm_obj->vmap_count--;
462 void msm_gem_put_vaddr(struct drm_gem_object *obj)
464 mutex_lock(&obj->dev->struct_mutex);
465 msm_gem_put_vaddr_locked(obj);
466 mutex_unlock(&obj->dev->struct_mutex);
469 /* Update madvise status, returns true if not purged, else
470 * false or -errno.
472 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
474 struct msm_gem_object *msm_obj = to_msm_bo(obj);
476 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
478 if (msm_obj->madv != __MSM_MADV_PURGED)
479 msm_obj->madv = madv;
481 return (msm_obj->madv != __MSM_MADV_PURGED);
484 void msm_gem_purge(struct drm_gem_object *obj)
486 struct drm_device *dev = obj->dev;
487 struct msm_gem_object *msm_obj = to_msm_bo(obj);
489 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
490 WARN_ON(!is_purgeable(msm_obj));
491 WARN_ON(obj->import_attach);
493 put_iova(obj);
495 msm_gem_vunmap(obj);
497 put_pages(obj);
499 msm_obj->madv = __MSM_MADV_PURGED;
501 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
502 drm_gem_free_mmap_offset(obj);
504 /* Our goal here is to return as much of the memory as
505 * is possible back to the system as we are called from OOM.
506 * To do this we must instruct the shmfs to drop all of its
507 * backing pages, *now*.
509 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
511 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
512 0, (loff_t)-1);
515 void msm_gem_vunmap(struct drm_gem_object *obj)
517 struct msm_gem_object *msm_obj = to_msm_bo(obj);
519 if (!msm_obj->vaddr || WARN_ON(!is_vunmapable(msm_obj)))
520 return;
522 vunmap(msm_obj->vaddr);
523 msm_obj->vaddr = NULL;
526 /* must be called before _move_to_active().. */
527 int msm_gem_sync_object(struct drm_gem_object *obj,
528 struct msm_fence_context *fctx, bool exclusive)
530 struct msm_gem_object *msm_obj = to_msm_bo(obj);
531 struct reservation_object_list *fobj;
532 struct fence *fence;
533 int i, ret;
535 if (!exclusive) {
536 /* NOTE: _reserve_shared() must happen before _add_shared_fence(),
537 * which makes this a slightly strange place to call it. OTOH this
538 * is a convenient can-fail point to hook it in. (And similar to
539 * how etnaviv and nouveau handle this.)
541 ret = reservation_object_reserve_shared(msm_obj->resv);
542 if (ret)
543 return ret;
546 fobj = reservation_object_get_list(msm_obj->resv);
547 if (!fobj || (fobj->shared_count == 0)) {
548 fence = reservation_object_get_excl(msm_obj->resv);
549 /* don't need to wait on our own fences, since ring is fifo */
550 if (fence && (fence->context != fctx->context)) {
551 ret = fence_wait(fence, true);
552 if (ret)
553 return ret;
557 if (!exclusive || !fobj)
558 return 0;
560 for (i = 0; i < fobj->shared_count; i++) {
561 fence = rcu_dereference_protected(fobj->shared[i],
562 reservation_object_held(msm_obj->resv));
563 if (fence->context != fctx->context) {
564 ret = fence_wait(fence, true);
565 if (ret)
566 return ret;
570 return 0;
573 void msm_gem_move_to_active(struct drm_gem_object *obj,
574 struct msm_gpu *gpu, bool exclusive, struct fence *fence)
576 struct msm_gem_object *msm_obj = to_msm_bo(obj);
577 WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
578 msm_obj->gpu = gpu;
579 if (exclusive)
580 reservation_object_add_excl_fence(msm_obj->resv, fence);
581 else
582 reservation_object_add_shared_fence(msm_obj->resv, fence);
583 list_del_init(&msm_obj->mm_list);
584 list_add_tail(&msm_obj->mm_list, &gpu->active_list);
587 void msm_gem_move_to_inactive(struct drm_gem_object *obj)
589 struct drm_device *dev = obj->dev;
590 struct msm_drm_private *priv = dev->dev_private;
591 struct msm_gem_object *msm_obj = to_msm_bo(obj);
593 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
595 msm_obj->gpu = NULL;
596 list_del_init(&msm_obj->mm_list);
597 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
600 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
602 struct msm_gem_object *msm_obj = to_msm_bo(obj);
603 bool write = !!(op & MSM_PREP_WRITE);
604 unsigned long remain =
605 op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
606 long ret;
608 ret = reservation_object_wait_timeout_rcu(msm_obj->resv, write,
609 true, remain);
610 if (ret == 0)
611 return remain == 0 ? -EBUSY : -ETIMEDOUT;
612 else if (ret < 0)
613 return ret;
615 /* TODO cache maintenance */
617 return 0;
620 int msm_gem_cpu_fini(struct drm_gem_object *obj)
622 /* TODO cache maintenance */
623 return 0;
626 #ifdef CONFIG_DEBUG_FS
627 static void describe_fence(struct fence *fence, const char *type,
628 struct seq_file *m)
630 if (!fence_is_signaled(fence))
631 seq_printf(m, "\t%9s: %s %s seq %u\n", type,
632 fence->ops->get_driver_name(fence),
633 fence->ops->get_timeline_name(fence),
634 fence->seqno);
637 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
639 struct msm_gem_object *msm_obj = to_msm_bo(obj);
640 struct reservation_object *robj = msm_obj->resv;
641 struct reservation_object_list *fobj;
642 struct fence *fence;
643 uint64_t off = drm_vma_node_start(&obj->vma_node);
644 const char *madv;
646 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
648 switch (msm_obj->madv) {
649 case __MSM_MADV_PURGED:
650 madv = " purged";
651 break;
652 case MSM_MADV_DONTNEED:
653 madv = " purgeable";
654 break;
655 case MSM_MADV_WILLNEED:
656 default:
657 madv = "";
658 break;
661 seq_printf(m, "%08x: %c %2d (%2d) %08llx %p %zu%s\n",
662 msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
663 obj->name, obj->refcount.refcount.counter,
664 off, msm_obj->vaddr, obj->size, madv);
666 rcu_read_lock();
667 fobj = rcu_dereference(robj->fence);
668 if (fobj) {
669 unsigned int i, shared_count = fobj->shared_count;
671 for (i = 0; i < shared_count; i++) {
672 fence = rcu_dereference(fobj->shared[i]);
673 describe_fence(fence, "Shared", m);
677 fence = rcu_dereference(robj->fence_excl);
678 if (fence)
679 describe_fence(fence, "Exclusive", m);
680 rcu_read_unlock();
683 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
685 struct msm_gem_object *msm_obj;
686 int count = 0;
687 size_t size = 0;
689 list_for_each_entry(msm_obj, list, mm_list) {
690 struct drm_gem_object *obj = &msm_obj->base;
691 seq_printf(m, " ");
692 msm_gem_describe(obj, m);
693 count++;
694 size += obj->size;
697 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
699 #endif
701 void msm_gem_free_object(struct drm_gem_object *obj)
703 struct drm_device *dev = obj->dev;
704 struct msm_gem_object *msm_obj = to_msm_bo(obj);
706 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
708 /* object should not be on active list: */
709 WARN_ON(is_active(msm_obj));
711 list_del(&msm_obj->mm_list);
713 put_iova(obj);
715 if (obj->import_attach) {
716 if (msm_obj->vaddr)
717 dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr);
719 /* Don't drop the pages for imported dmabuf, as they are not
720 * ours, just free the array we allocated:
722 if (msm_obj->pages)
723 drm_free_large(msm_obj->pages);
725 drm_prime_gem_destroy(obj, msm_obj->sgt);
726 } else {
727 msm_gem_vunmap(obj);
728 put_pages(obj);
731 if (msm_obj->resv == &msm_obj->_resv)
732 reservation_object_fini(msm_obj->resv);
734 drm_gem_object_release(obj);
736 kfree(msm_obj);
739 /* convenience method to construct a GEM buffer object, and userspace handle */
740 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
741 uint32_t size, uint32_t flags, uint32_t *handle)
743 struct drm_gem_object *obj;
744 int ret;
746 ret = mutex_lock_interruptible(&dev->struct_mutex);
747 if (ret)
748 return ret;
750 obj = msm_gem_new(dev, size, flags);
752 mutex_unlock(&dev->struct_mutex);
754 if (IS_ERR(obj))
755 return PTR_ERR(obj);
757 ret = drm_gem_handle_create(file, obj, handle);
759 /* drop reference from allocate - handle holds it now */
760 drm_gem_object_unreference_unlocked(obj);
762 return ret;
765 static int msm_gem_new_impl(struct drm_device *dev,
766 uint32_t size, uint32_t flags,
767 struct reservation_object *resv,
768 struct drm_gem_object **obj)
770 struct msm_drm_private *priv = dev->dev_private;
771 struct msm_gem_object *msm_obj;
772 unsigned sz;
773 bool use_vram = false;
775 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
777 switch (flags & MSM_BO_CACHE_MASK) {
778 case MSM_BO_UNCACHED:
779 case MSM_BO_CACHED:
780 case MSM_BO_WC:
781 break;
782 default:
783 dev_err(dev->dev, "invalid cache flag: %x\n",
784 (flags & MSM_BO_CACHE_MASK));
785 return -EINVAL;
788 if (!iommu_present(&platform_bus_type))
789 use_vram = true;
790 else if ((flags & MSM_BO_STOLEN) && priv->vram.size)
791 use_vram = true;
793 if (WARN_ON(use_vram && !priv->vram.size))
794 return -EINVAL;
796 sz = sizeof(*msm_obj);
797 if (use_vram)
798 sz += sizeof(struct drm_mm_node);
800 msm_obj = kzalloc(sz, GFP_KERNEL);
801 if (!msm_obj)
802 return -ENOMEM;
804 if (use_vram)
805 msm_obj->vram_node = (void *)&msm_obj[1];
807 msm_obj->flags = flags;
808 msm_obj->madv = MSM_MADV_WILLNEED;
810 if (resv) {
811 msm_obj->resv = resv;
812 } else {
813 msm_obj->resv = &msm_obj->_resv;
814 reservation_object_init(msm_obj->resv);
817 INIT_LIST_HEAD(&msm_obj->submit_entry);
818 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
820 *obj = &msm_obj->base;
822 return 0;
825 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
826 uint32_t size, uint32_t flags)
828 struct drm_gem_object *obj = NULL;
829 int ret;
831 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
833 size = PAGE_ALIGN(size);
835 ret = msm_gem_new_impl(dev, size, flags, NULL, &obj);
836 if (ret)
837 goto fail;
839 if (use_pages(obj)) {
840 ret = drm_gem_object_init(dev, obj, size);
841 if (ret)
842 goto fail;
843 } else {
844 drm_gem_private_object_init(dev, obj, size);
847 return obj;
849 fail:
850 drm_gem_object_unreference(obj);
851 return ERR_PTR(ret);
854 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
855 struct dma_buf *dmabuf, struct sg_table *sgt)
857 struct msm_gem_object *msm_obj;
858 struct drm_gem_object *obj;
859 uint32_t size;
860 int ret, npages;
862 /* if we don't have IOMMU, don't bother pretending we can import: */
863 if (!iommu_present(&platform_bus_type)) {
864 dev_err(dev->dev, "cannot import without IOMMU\n");
865 return ERR_PTR(-EINVAL);
868 size = PAGE_ALIGN(dmabuf->size);
870 /* Take mutex so we can modify the inactive list in msm_gem_new_impl */
871 mutex_lock(&dev->struct_mutex);
872 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, dmabuf->resv, &obj);
873 mutex_unlock(&dev->struct_mutex);
875 if (ret)
876 goto fail;
878 drm_gem_private_object_init(dev, obj, size);
880 npages = size / PAGE_SIZE;
882 msm_obj = to_msm_bo(obj);
883 msm_obj->sgt = sgt;
884 msm_obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
885 if (!msm_obj->pages) {
886 ret = -ENOMEM;
887 goto fail;
890 ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
891 if (ret)
892 goto fail;
894 return obj;
896 fail:
897 drm_gem_object_unreference_unlocked(obj);
898 return ERR_PTR(ret);