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