sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / gpu / drm / msm / msm_gem.c
blob690d7e7b6d1e8296891dde245d871850b0631715
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>
22 #include "msm_drv.h"
23 #include "msm_gem.h"
24 #include "msm_gpu.h"
25 #include "msm_mmu.h"
27 static dma_addr_t physaddr(struct drm_gem_object *obj)
29 struct msm_gem_object *msm_obj = to_msm_bo(obj);
30 struct msm_drm_private *priv = obj->dev->dev_private;
31 return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) +
32 priv->vram.paddr;
35 /* allocate pages from VRAM carveout, used when no IOMMU: */
36 static struct page **get_pages_vram(struct drm_gem_object *obj,
37 int npages)
39 struct msm_gem_object *msm_obj = to_msm_bo(obj);
40 struct msm_drm_private *priv = obj->dev->dev_private;
41 dma_addr_t paddr;
42 struct page **p;
43 int ret, i;
45 p = drm_malloc_ab(npages, sizeof(struct page *));
46 if (!p)
47 return ERR_PTR(-ENOMEM);
49 ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node,
50 npages, 0, DRM_MM_SEARCH_DEFAULT);
51 if (ret) {
52 drm_free_large(p);
53 return ERR_PTR(ret);
56 paddr = physaddr(obj);
57 for (i = 0; i < npages; i++) {
58 p[i] = phys_to_page(paddr);
59 paddr += PAGE_SIZE;
62 return p;
65 /* called with dev->struct_mutex held */
66 static struct page **get_pages(struct drm_gem_object *obj)
68 struct msm_gem_object *msm_obj = to_msm_bo(obj);
70 if (!msm_obj->pages) {
71 struct drm_device *dev = obj->dev;
72 struct page **p;
73 int npages = obj->size >> PAGE_SHIFT;
75 if (iommu_present(&platform_bus_type))
76 p = drm_gem_get_pages(obj, 0);
77 else
78 p = get_pages_vram(obj, npages);
80 if (IS_ERR(p)) {
81 dev_err(dev->dev, "could not get pages: %ld\n",
82 PTR_ERR(p));
83 return p;
86 msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
87 if (IS_ERR(msm_obj->sgt)) {
88 dev_err(dev->dev, "failed to allocate sgt\n");
89 return ERR_CAST(msm_obj->sgt);
92 msm_obj->pages = p;
94 /* For non-cached buffers, ensure the new pages are clean
95 * because display controller, GPU, etc. are not coherent:
97 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
98 dma_map_sg(dev->dev, msm_obj->sgt->sgl,
99 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
102 return msm_obj->pages;
105 static void put_pages(struct drm_gem_object *obj)
107 struct msm_gem_object *msm_obj = to_msm_bo(obj);
109 if (msm_obj->pages) {
110 /* For non-cached buffers, ensure the new pages are clean
111 * because display controller, GPU, etc. are not coherent:
113 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
114 dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl,
115 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
116 sg_free_table(msm_obj->sgt);
117 kfree(msm_obj->sgt);
119 if (iommu_present(&platform_bus_type))
120 drm_gem_put_pages(obj, msm_obj->pages, true, false);
121 else {
122 drm_mm_remove_node(msm_obj->vram_node);
123 drm_free_large(msm_obj->pages);
126 msm_obj->pages = NULL;
130 struct page **msm_gem_get_pages(struct drm_gem_object *obj)
132 struct drm_device *dev = obj->dev;
133 struct page **p;
134 mutex_lock(&dev->struct_mutex);
135 p = get_pages(obj);
136 mutex_unlock(&dev->struct_mutex);
137 return p;
140 void msm_gem_put_pages(struct drm_gem_object *obj)
142 /* when we start tracking the pin count, then do something here */
145 int msm_gem_mmap_obj(struct drm_gem_object *obj,
146 struct vm_area_struct *vma)
148 struct msm_gem_object *msm_obj = to_msm_bo(obj);
150 vma->vm_flags &= ~VM_PFNMAP;
151 vma->vm_flags |= VM_MIXEDMAP;
153 if (msm_obj->flags & MSM_BO_WC) {
154 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
155 } else if (msm_obj->flags & MSM_BO_UNCACHED) {
156 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
157 } else {
159 * Shunt off cached objs to shmem file so they have their own
160 * address_space (so unmap_mapping_range does what we want,
161 * in particular in the case of mmap'd dmabufs)
163 fput(vma->vm_file);
164 get_file(obj->filp);
165 vma->vm_pgoff = 0;
166 vma->vm_file = obj->filp;
168 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
171 return 0;
174 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
176 int ret;
178 ret = drm_gem_mmap(filp, vma);
179 if (ret) {
180 DBG("mmap failed: %d", ret);
181 return ret;
184 return msm_gem_mmap_obj(vma->vm_private_data, vma);
187 int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
189 struct drm_gem_object *obj = vma->vm_private_data;
190 struct drm_device *dev = obj->dev;
191 struct page **pages;
192 unsigned long pfn;
193 pgoff_t pgoff;
194 int ret;
196 /* Make sure we don't parallel update on a fault, nor move or remove
197 * something from beneath our feet
199 ret = mutex_lock_interruptible(&dev->struct_mutex);
200 if (ret)
201 goto out;
203 /* make sure we have pages attached now */
204 pages = get_pages(obj);
205 if (IS_ERR(pages)) {
206 ret = PTR_ERR(pages);
207 goto out_unlock;
210 /* We don't use vmf->pgoff since that has the fake offset: */
211 pgoff = ((unsigned long)vmf->virtual_address -
212 vma->vm_start) >> PAGE_SHIFT;
214 pfn = page_to_pfn(pages[pgoff]);
216 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
217 pfn, pfn << PAGE_SHIFT);
219 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
221 out_unlock:
222 mutex_unlock(&dev->struct_mutex);
223 out:
224 switch (ret) {
225 case -EAGAIN:
226 case 0:
227 case -ERESTARTSYS:
228 case -EINTR:
229 case -EBUSY:
231 * EBUSY is ok: this just means that another thread
232 * already did the job.
234 return VM_FAULT_NOPAGE;
235 case -ENOMEM:
236 return VM_FAULT_OOM;
237 default:
238 return VM_FAULT_SIGBUS;
242 /** get mmap offset */
243 static uint64_t mmap_offset(struct drm_gem_object *obj)
245 struct drm_device *dev = obj->dev;
246 int ret;
248 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
250 /* Make it mmapable */
251 ret = drm_gem_create_mmap_offset(obj);
253 if (ret) {
254 dev_err(dev->dev, "could not allocate mmap offset\n");
255 return 0;
258 return drm_vma_node_offset_addr(&obj->vma_node);
261 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
263 uint64_t offset;
264 mutex_lock(&obj->dev->struct_mutex);
265 offset = mmap_offset(obj);
266 mutex_unlock(&obj->dev->struct_mutex);
267 return offset;
270 /* should be called under struct_mutex.. although it can be called
271 * from atomic context without struct_mutex to acquire an extra
272 * iova ref if you know one is already held.
274 * That means when I do eventually need to add support for unpinning
275 * the refcnt counter needs to be atomic_t.
277 int msm_gem_get_iova_locked(struct drm_gem_object *obj, int id,
278 uint32_t *iova)
280 struct msm_gem_object *msm_obj = to_msm_bo(obj);
281 struct drm_device *dev = obj->dev;
282 int ret = 0;
284 if (!msm_obj->domain[id].iova) {
285 struct msm_drm_private *priv = obj->dev->dev_private;
286 struct msm_mmu *mmu = priv->mmus[id];
287 struct page **pages = get_pages(obj);
289 if (!mmu) {
290 dev_err(dev->dev, "null MMU pointer\n");
291 return -EINVAL;
294 if (IS_ERR(pages))
295 return PTR_ERR(pages);
297 if (iommu_present(&platform_bus_type)) {
298 uint32_t offset = (uint32_t)mmap_offset(obj);
299 ret = mmu->funcs->map(mmu, offset, msm_obj->sgt,
300 obj->size, IOMMU_READ | IOMMU_WRITE);
301 msm_obj->domain[id].iova = offset;
302 } else {
303 msm_obj->domain[id].iova = physaddr(obj);
307 if (!ret)
308 *iova = msm_obj->domain[id].iova;
310 return ret;
313 int msm_gem_get_iova(struct drm_gem_object *obj, int id, uint32_t *iova)
315 struct msm_gem_object *msm_obj = to_msm_bo(obj);
316 int ret;
318 /* this is safe right now because we don't unmap until the
319 * bo is deleted:
321 if (msm_obj->domain[id].iova) {
322 *iova = msm_obj->domain[id].iova;
323 return 0;
326 mutex_lock(&obj->dev->struct_mutex);
327 ret = msm_gem_get_iova_locked(obj, id, iova);
328 mutex_unlock(&obj->dev->struct_mutex);
329 return ret;
332 void msm_gem_put_iova(struct drm_gem_object *obj, int id)
334 // XXX TODO ..
335 // NOTE: probably don't need a _locked() version.. we wouldn't
336 // normally unmap here, but instead just mark that it could be
337 // unmapped (if the iova refcnt drops to zero), but then later
338 // if another _get_iova_locked() fails we can start unmapping
339 // things that are no longer needed..
342 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
343 struct drm_mode_create_dumb *args)
345 args->pitch = align_pitch(args->width, args->bpp);
346 args->size = PAGE_ALIGN(args->pitch * args->height);
347 return msm_gem_new_handle(dev, file, args->size,
348 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle);
351 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
352 uint32_t handle, uint64_t *offset)
354 struct drm_gem_object *obj;
355 int ret = 0;
357 /* GEM does all our handle to object mapping */
358 obj = drm_gem_object_lookup(dev, file, handle);
359 if (obj == NULL) {
360 ret = -ENOENT;
361 goto fail;
364 *offset = msm_gem_mmap_offset(obj);
366 drm_gem_object_unreference_unlocked(obj);
368 fail:
369 return ret;
372 void *msm_gem_vaddr_locked(struct drm_gem_object *obj)
374 struct msm_gem_object *msm_obj = to_msm_bo(obj);
375 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
376 if (!msm_obj->vaddr) {
377 struct page **pages = get_pages(obj);
378 if (IS_ERR(pages))
379 return ERR_CAST(pages);
380 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
381 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
383 return msm_obj->vaddr;
386 void *msm_gem_vaddr(struct drm_gem_object *obj)
388 void *ret;
389 mutex_lock(&obj->dev->struct_mutex);
390 ret = msm_gem_vaddr_locked(obj);
391 mutex_unlock(&obj->dev->struct_mutex);
392 return ret;
395 /* setup callback for when bo is no longer busy..
396 * TODO probably want to differentiate read vs write..
398 int msm_gem_queue_inactive_cb(struct drm_gem_object *obj,
399 struct msm_fence_cb *cb)
401 struct drm_device *dev = obj->dev;
402 struct msm_drm_private *priv = dev->dev_private;
403 struct msm_gem_object *msm_obj = to_msm_bo(obj);
404 int ret = 0;
406 mutex_lock(&dev->struct_mutex);
407 if (!list_empty(&cb->work.entry)) {
408 ret = -EINVAL;
409 } else if (is_active(msm_obj)) {
410 cb->fence = max(msm_obj->read_fence, msm_obj->write_fence);
411 list_add_tail(&cb->work.entry, &priv->fence_cbs);
412 } else {
413 queue_work(priv->wq, &cb->work);
415 mutex_unlock(&dev->struct_mutex);
417 return ret;
420 void msm_gem_move_to_active(struct drm_gem_object *obj,
421 struct msm_gpu *gpu, bool write, uint32_t fence)
423 struct msm_gem_object *msm_obj = to_msm_bo(obj);
424 msm_obj->gpu = gpu;
425 if (write)
426 msm_obj->write_fence = fence;
427 else
428 msm_obj->read_fence = fence;
429 list_del_init(&msm_obj->mm_list);
430 list_add_tail(&msm_obj->mm_list, &gpu->active_list);
433 void msm_gem_move_to_inactive(struct drm_gem_object *obj)
435 struct drm_device *dev = obj->dev;
436 struct msm_drm_private *priv = dev->dev_private;
437 struct msm_gem_object *msm_obj = to_msm_bo(obj);
439 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
441 msm_obj->gpu = NULL;
442 msm_obj->read_fence = 0;
443 msm_obj->write_fence = 0;
444 list_del_init(&msm_obj->mm_list);
445 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
448 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op,
449 struct timespec *timeout)
451 struct drm_device *dev = obj->dev;
452 struct msm_gem_object *msm_obj = to_msm_bo(obj);
453 int ret = 0;
455 if (is_active(msm_obj)) {
456 uint32_t fence = 0;
458 if (op & MSM_PREP_READ)
459 fence = msm_obj->write_fence;
460 if (op & MSM_PREP_WRITE)
461 fence = max(fence, msm_obj->read_fence);
462 if (op & MSM_PREP_NOSYNC)
463 timeout = NULL;
465 ret = msm_wait_fence_interruptable(dev, fence, timeout);
468 /* TODO cache maintenance */
470 return ret;
473 int msm_gem_cpu_fini(struct drm_gem_object *obj)
475 /* TODO cache maintenance */
476 return 0;
479 #ifdef CONFIG_DEBUG_FS
480 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
482 struct drm_device *dev = obj->dev;
483 struct msm_gem_object *msm_obj = to_msm_bo(obj);
484 uint64_t off = drm_vma_node_start(&obj->vma_node);
486 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
487 seq_printf(m, "%08x: %c(r=%u,w=%u) %2d (%2d) %08llx %p %d\n",
488 msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
489 msm_obj->read_fence, msm_obj->write_fence,
490 obj->name, obj->refcount.refcount.counter,
491 off, msm_obj->vaddr, obj->size);
494 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
496 struct msm_gem_object *msm_obj;
497 int count = 0;
498 size_t size = 0;
500 list_for_each_entry(msm_obj, list, mm_list) {
501 struct drm_gem_object *obj = &msm_obj->base;
502 seq_printf(m, " ");
503 msm_gem_describe(obj, m);
504 count++;
505 size += obj->size;
508 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
510 #endif
512 void msm_gem_free_object(struct drm_gem_object *obj)
514 struct drm_device *dev = obj->dev;
515 struct msm_drm_private *priv = obj->dev->dev_private;
516 struct msm_gem_object *msm_obj = to_msm_bo(obj);
517 int id;
519 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
521 /* object should not be on active list: */
522 WARN_ON(is_active(msm_obj));
524 list_del(&msm_obj->mm_list);
526 for (id = 0; id < ARRAY_SIZE(msm_obj->domain); id++) {
527 struct msm_mmu *mmu = priv->mmus[id];
528 if (mmu && msm_obj->domain[id].iova) {
529 uint32_t offset = (uint32_t)mmap_offset(obj);
530 mmu->funcs->unmap(mmu, offset, msm_obj->sgt, obj->size);
534 drm_gem_free_mmap_offset(obj);
536 if (obj->import_attach) {
537 if (msm_obj->vaddr)
538 dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr);
540 /* Don't drop the pages for imported dmabuf, as they are not
541 * ours, just free the array we allocated:
543 if (msm_obj->pages)
544 drm_free_large(msm_obj->pages);
546 } else {
547 if (msm_obj->vaddr)
548 vunmap(msm_obj->vaddr);
549 put_pages(obj);
552 if (msm_obj->resv == &msm_obj->_resv)
553 reservation_object_fini(msm_obj->resv);
555 drm_gem_object_release(obj);
557 kfree(msm_obj);
560 /* convenience method to construct a GEM buffer object, and userspace handle */
561 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
562 uint32_t size, uint32_t flags, uint32_t *handle)
564 struct drm_gem_object *obj;
565 int ret;
567 ret = mutex_lock_interruptible(&dev->struct_mutex);
568 if (ret)
569 return ret;
571 obj = msm_gem_new(dev, size, flags);
573 mutex_unlock(&dev->struct_mutex);
575 if (IS_ERR(obj))
576 return PTR_ERR(obj);
578 ret = drm_gem_handle_create(file, obj, handle);
580 /* drop reference from allocate - handle holds it now */
581 drm_gem_object_unreference_unlocked(obj);
583 return ret;
586 static int msm_gem_new_impl(struct drm_device *dev,
587 uint32_t size, uint32_t flags,
588 struct drm_gem_object **obj)
590 struct msm_drm_private *priv = dev->dev_private;
591 struct msm_gem_object *msm_obj;
592 unsigned sz;
594 switch (flags & MSM_BO_CACHE_MASK) {
595 case MSM_BO_UNCACHED:
596 case MSM_BO_CACHED:
597 case MSM_BO_WC:
598 break;
599 default:
600 dev_err(dev->dev, "invalid cache flag: %x\n",
601 (flags & MSM_BO_CACHE_MASK));
602 return -EINVAL;
605 sz = sizeof(*msm_obj);
606 if (!iommu_present(&platform_bus_type))
607 sz += sizeof(struct drm_mm_node);
609 msm_obj = kzalloc(sz, GFP_KERNEL);
610 if (!msm_obj)
611 return -ENOMEM;
613 if (!iommu_present(&platform_bus_type))
614 msm_obj->vram_node = (void *)&msm_obj[1];
616 msm_obj->flags = flags;
618 msm_obj->resv = &msm_obj->_resv;
619 reservation_object_init(msm_obj->resv);
621 INIT_LIST_HEAD(&msm_obj->submit_entry);
622 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
624 *obj = &msm_obj->base;
626 return 0;
629 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
630 uint32_t size, uint32_t flags)
632 struct drm_gem_object *obj = NULL;
633 int ret;
635 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
637 size = PAGE_ALIGN(size);
639 ret = msm_gem_new_impl(dev, size, flags, &obj);
640 if (ret)
641 goto fail;
643 if (iommu_present(&platform_bus_type)) {
644 ret = drm_gem_object_init(dev, obj, size);
645 if (ret)
646 goto fail;
647 } else {
648 drm_gem_private_object_init(dev, obj, size);
651 return obj;
653 fail:
654 if (obj)
655 drm_gem_object_unreference(obj);
657 return ERR_PTR(ret);
660 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
661 uint32_t size, struct sg_table *sgt)
663 struct msm_gem_object *msm_obj;
664 struct drm_gem_object *obj;
665 int ret, npages;
667 /* if we don't have IOMMU, don't bother pretending we can import: */
668 if (!iommu_present(&platform_bus_type)) {
669 dev_err(dev->dev, "cannot import without IOMMU\n");
670 return ERR_PTR(-EINVAL);
673 size = PAGE_ALIGN(size);
675 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
676 if (ret)
677 goto fail;
679 drm_gem_private_object_init(dev, obj, size);
681 npages = size / PAGE_SIZE;
683 msm_obj = to_msm_bo(obj);
684 msm_obj->sgt = sgt;
685 msm_obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
686 if (!msm_obj->pages) {
687 ret = -ENOMEM;
688 goto fail;
691 ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
692 if (ret)
693 goto fail;
695 return obj;
697 fail:
698 if (obj)
699 drm_gem_object_unreference_unlocked(obj);
701 return ERR_PTR(ret);