Linux 4.2.1
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / i915_gem_userptr.c
blob8fd431bcdfd3a33ffb6afda7a1584b44e33d8296
1 /*
2 * Copyright © 2012-2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
25 #include <drm/drmP.h>
26 #include <drm/i915_drm.h>
27 #include "i915_drv.h"
28 #include "i915_trace.h"
29 #include "intel_drv.h"
30 #include <linux/mmu_context.h>
31 #include <linux/mmu_notifier.h>
32 #include <linux/mempolicy.h>
33 #include <linux/swap.h>
35 struct i915_mm_struct {
36 struct mm_struct *mm;
37 struct drm_device *dev;
38 struct i915_mmu_notifier *mn;
39 struct hlist_node node;
40 struct kref kref;
41 struct work_struct work;
44 #if defined(CONFIG_MMU_NOTIFIER)
45 #include <linux/interval_tree.h>
47 struct i915_mmu_notifier {
48 spinlock_t lock;
49 struct hlist_node node;
50 struct mmu_notifier mn;
51 struct rb_root objects;
52 struct list_head linear;
53 unsigned long serial;
54 bool has_linear;
57 struct i915_mmu_object {
58 struct i915_mmu_notifier *mn;
59 struct interval_tree_node it;
60 struct list_head link;
61 struct drm_i915_gem_object *obj;
62 bool is_linear;
65 static unsigned long cancel_userptr(struct drm_i915_gem_object *obj)
67 struct drm_device *dev = obj->base.dev;
68 unsigned long end;
70 mutex_lock(&dev->struct_mutex);
71 /* Cancel any active worker and force us to re-evaluate gup */
72 obj->userptr.work = NULL;
74 if (obj->pages != NULL) {
75 struct drm_i915_private *dev_priv = to_i915(dev);
76 struct i915_vma *vma, *tmp;
77 bool was_interruptible;
79 was_interruptible = dev_priv->mm.interruptible;
80 dev_priv->mm.interruptible = false;
82 list_for_each_entry_safe(vma, tmp, &obj->vma_list, vma_link) {
83 int ret = i915_vma_unbind(vma);
84 WARN_ON(ret && ret != -EIO);
86 WARN_ON(i915_gem_object_put_pages(obj));
88 dev_priv->mm.interruptible = was_interruptible;
91 end = obj->userptr.ptr + obj->base.size;
93 drm_gem_object_unreference(&obj->base);
94 mutex_unlock(&dev->struct_mutex);
96 return end;
99 static void *invalidate_range__linear(struct i915_mmu_notifier *mn,
100 struct mm_struct *mm,
101 unsigned long start,
102 unsigned long end)
104 struct i915_mmu_object *mo;
105 unsigned long serial;
107 restart:
108 serial = mn->serial;
109 list_for_each_entry(mo, &mn->linear, link) {
110 struct drm_i915_gem_object *obj;
112 if (mo->it.last < start || mo->it.start > end)
113 continue;
115 obj = mo->obj;
117 if (!kref_get_unless_zero(&obj->base.refcount))
118 continue;
120 spin_unlock(&mn->lock);
122 cancel_userptr(obj);
124 spin_lock(&mn->lock);
125 if (serial != mn->serial)
126 goto restart;
129 return NULL;
132 static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
133 struct mm_struct *mm,
134 unsigned long start,
135 unsigned long end)
137 struct i915_mmu_notifier *mn = container_of(_mn, struct i915_mmu_notifier, mn);
138 struct interval_tree_node *it = NULL;
139 unsigned long next = start;
140 unsigned long serial = 0;
142 end--; /* interval ranges are inclusive, but invalidate range is exclusive */
143 while (next < end) {
144 struct drm_i915_gem_object *obj = NULL;
146 spin_lock(&mn->lock);
147 if (mn->has_linear)
148 it = invalidate_range__linear(mn, mm, start, end);
149 else if (serial == mn->serial)
150 it = interval_tree_iter_next(it, next, end);
151 else
152 it = interval_tree_iter_first(&mn->objects, start, end);
153 if (it != NULL) {
154 obj = container_of(it, struct i915_mmu_object, it)->obj;
156 /* The mmu_object is released late when destroying the
157 * GEM object so it is entirely possible to gain a
158 * reference on an object in the process of being freed
159 * since our serialisation is via the spinlock and not
160 * the struct_mutex - and consequently use it after it
161 * is freed and then double free it.
163 if (!kref_get_unless_zero(&obj->base.refcount)) {
164 spin_unlock(&mn->lock);
165 serial = 0;
166 continue;
169 serial = mn->serial;
171 spin_unlock(&mn->lock);
172 if (obj == NULL)
173 return;
175 next = cancel_userptr(obj);
179 static const struct mmu_notifier_ops i915_gem_userptr_notifier = {
180 .invalidate_range_start = i915_gem_userptr_mn_invalidate_range_start,
183 static struct i915_mmu_notifier *
184 i915_mmu_notifier_create(struct mm_struct *mm)
186 struct i915_mmu_notifier *mn;
187 int ret;
189 mn = kmalloc(sizeof(*mn), GFP_KERNEL);
190 if (mn == NULL)
191 return ERR_PTR(-ENOMEM);
193 spin_lock_init(&mn->lock);
194 mn->mn.ops = &i915_gem_userptr_notifier;
195 mn->objects = RB_ROOT;
196 mn->serial = 1;
197 INIT_LIST_HEAD(&mn->linear);
198 mn->has_linear = false;
200 /* Protected by mmap_sem (write-lock) */
201 ret = __mmu_notifier_register(&mn->mn, mm);
202 if (ret) {
203 kfree(mn);
204 return ERR_PTR(ret);
207 return mn;
210 static void __i915_mmu_notifier_update_serial(struct i915_mmu_notifier *mn)
212 if (++mn->serial == 0)
213 mn->serial = 1;
216 static int
217 i915_mmu_notifier_add(struct drm_device *dev,
218 struct i915_mmu_notifier *mn,
219 struct i915_mmu_object *mo)
221 struct interval_tree_node *it;
222 int ret = 0;
224 /* By this point we have already done a lot of expensive setup that
225 * we do not want to repeat just because the caller (e.g. X) has a
226 * signal pending (and partly because of that expensive setup, X
227 * using an interrupt timer is likely to get stuck in an EINTR loop).
229 mutex_lock(&dev->struct_mutex);
231 /* Make sure we drop the final active reference (and thereby
232 * remove the objects from the interval tree) before we do
233 * the check for overlapping objects.
235 i915_gem_retire_requests(dev);
237 spin_lock(&mn->lock);
238 it = interval_tree_iter_first(&mn->objects,
239 mo->it.start, mo->it.last);
240 if (it) {
241 struct drm_i915_gem_object *obj;
243 /* We only need to check the first object in the range as it
244 * either has cancelled gup work queued and we need to
245 * return back to the user to give time for the gup-workers
246 * to flush their object references upon which the object will
247 * be removed from the interval-tree, or the the range is
248 * still in use by another client and the overlap is invalid.
250 * If we do have an overlap, we cannot use the interval tree
251 * for fast range invalidation.
254 obj = container_of(it, struct i915_mmu_object, it)->obj;
255 if (!obj->userptr.workers)
256 mn->has_linear = mo->is_linear = true;
257 else
258 ret = -EAGAIN;
259 } else
260 interval_tree_insert(&mo->it, &mn->objects);
262 if (ret == 0) {
263 list_add(&mo->link, &mn->linear);
264 __i915_mmu_notifier_update_serial(mn);
266 spin_unlock(&mn->lock);
267 mutex_unlock(&dev->struct_mutex);
269 return ret;
272 static bool i915_mmu_notifier_has_linear(struct i915_mmu_notifier *mn)
274 struct i915_mmu_object *mo;
276 list_for_each_entry(mo, &mn->linear, link)
277 if (mo->is_linear)
278 return true;
280 return false;
283 static void
284 i915_mmu_notifier_del(struct i915_mmu_notifier *mn,
285 struct i915_mmu_object *mo)
287 spin_lock(&mn->lock);
288 list_del(&mo->link);
289 if (mo->is_linear)
290 mn->has_linear = i915_mmu_notifier_has_linear(mn);
291 else
292 interval_tree_remove(&mo->it, &mn->objects);
293 __i915_mmu_notifier_update_serial(mn);
294 spin_unlock(&mn->lock);
297 static void
298 i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
300 struct i915_mmu_object *mo;
302 mo = obj->userptr.mmu_object;
303 if (mo == NULL)
304 return;
306 i915_mmu_notifier_del(mo->mn, mo);
307 kfree(mo);
309 obj->userptr.mmu_object = NULL;
312 static struct i915_mmu_notifier *
313 i915_mmu_notifier_find(struct i915_mm_struct *mm)
315 struct i915_mmu_notifier *mn = mm->mn;
317 mn = mm->mn;
318 if (mn)
319 return mn;
321 down_write(&mm->mm->mmap_sem);
322 mutex_lock(&to_i915(mm->dev)->mm_lock);
323 if ((mn = mm->mn) == NULL) {
324 mn = i915_mmu_notifier_create(mm->mm);
325 if (!IS_ERR(mn))
326 mm->mn = mn;
328 mutex_unlock(&to_i915(mm->dev)->mm_lock);
329 up_write(&mm->mm->mmap_sem);
331 return mn;
334 static int
335 i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
336 unsigned flags)
338 struct i915_mmu_notifier *mn;
339 struct i915_mmu_object *mo;
340 int ret;
342 if (flags & I915_USERPTR_UNSYNCHRONIZED)
343 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
345 if (WARN_ON(obj->userptr.mm == NULL))
346 return -EINVAL;
348 mn = i915_mmu_notifier_find(obj->userptr.mm);
349 if (IS_ERR(mn))
350 return PTR_ERR(mn);
352 mo = kzalloc(sizeof(*mo), GFP_KERNEL);
353 if (mo == NULL)
354 return -ENOMEM;
356 mo->mn = mn;
357 mo->it.start = obj->userptr.ptr;
358 mo->it.last = mo->it.start + obj->base.size - 1;
359 mo->obj = obj;
361 ret = i915_mmu_notifier_add(obj->base.dev, mn, mo);
362 if (ret) {
363 kfree(mo);
364 return ret;
367 obj->userptr.mmu_object = mo;
368 return 0;
371 static void
372 i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
373 struct mm_struct *mm)
375 if (mn == NULL)
376 return;
378 mmu_notifier_unregister(&mn->mn, mm);
379 kfree(mn);
382 #else
384 static void
385 i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
389 static int
390 i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
391 unsigned flags)
393 if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0)
394 return -ENODEV;
396 if (!capable(CAP_SYS_ADMIN))
397 return -EPERM;
399 return 0;
402 static void
403 i915_mmu_notifier_free(struct i915_mmu_notifier *mn,
404 struct mm_struct *mm)
408 #endif
410 static struct i915_mm_struct *
411 __i915_mm_struct_find(struct drm_i915_private *dev_priv, struct mm_struct *real)
413 struct i915_mm_struct *mm;
415 /* Protected by dev_priv->mm_lock */
416 hash_for_each_possible(dev_priv->mm_structs, mm, node, (unsigned long)real)
417 if (mm->mm == real)
418 return mm;
420 return NULL;
423 static int
424 i915_gem_userptr_init__mm_struct(struct drm_i915_gem_object *obj)
426 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
427 struct i915_mm_struct *mm;
428 int ret = 0;
430 /* During release of the GEM object we hold the struct_mutex. This
431 * precludes us from calling mmput() at that time as that may be
432 * the last reference and so call exit_mmap(). exit_mmap() will
433 * attempt to reap the vma, and if we were holding a GTT mmap
434 * would then call drm_gem_vm_close() and attempt to reacquire
435 * the struct mutex. So in order to avoid that recursion, we have
436 * to defer releasing the mm reference until after we drop the
437 * struct_mutex, i.e. we need to schedule a worker to do the clean
438 * up.
440 mutex_lock(&dev_priv->mm_lock);
441 mm = __i915_mm_struct_find(dev_priv, current->mm);
442 if (mm == NULL) {
443 mm = kmalloc(sizeof(*mm), GFP_KERNEL);
444 if (mm == NULL) {
445 ret = -ENOMEM;
446 goto out;
449 kref_init(&mm->kref);
450 mm->dev = obj->base.dev;
452 mm->mm = current->mm;
453 atomic_inc(&current->mm->mm_count);
455 mm->mn = NULL;
457 /* Protected by dev_priv->mm_lock */
458 hash_add(dev_priv->mm_structs,
459 &mm->node, (unsigned long)mm->mm);
460 } else
461 kref_get(&mm->kref);
463 obj->userptr.mm = mm;
464 out:
465 mutex_unlock(&dev_priv->mm_lock);
466 return ret;
469 static void
470 __i915_mm_struct_free__worker(struct work_struct *work)
472 struct i915_mm_struct *mm = container_of(work, typeof(*mm), work);
473 i915_mmu_notifier_free(mm->mn, mm->mm);
474 mmdrop(mm->mm);
475 kfree(mm);
478 static void
479 __i915_mm_struct_free(struct kref *kref)
481 struct i915_mm_struct *mm = container_of(kref, typeof(*mm), kref);
483 /* Protected by dev_priv->mm_lock */
484 hash_del(&mm->node);
485 mutex_unlock(&to_i915(mm->dev)->mm_lock);
487 INIT_WORK(&mm->work, __i915_mm_struct_free__worker);
488 schedule_work(&mm->work);
491 static void
492 i915_gem_userptr_release__mm_struct(struct drm_i915_gem_object *obj)
494 if (obj->userptr.mm == NULL)
495 return;
497 kref_put_mutex(&obj->userptr.mm->kref,
498 __i915_mm_struct_free,
499 &to_i915(obj->base.dev)->mm_lock);
500 obj->userptr.mm = NULL;
503 struct get_pages_work {
504 struct work_struct work;
505 struct drm_i915_gem_object *obj;
506 struct task_struct *task;
509 #if IS_ENABLED(CONFIG_SWIOTLB)
510 #define swiotlb_active() swiotlb_nr_tbl()
511 #else
512 #define swiotlb_active() 0
513 #endif
515 static int
516 st_set_pages(struct sg_table **st, struct page **pvec, int num_pages)
518 struct scatterlist *sg;
519 int ret, n;
521 *st = kmalloc(sizeof(**st), GFP_KERNEL);
522 if (*st == NULL)
523 return -ENOMEM;
525 if (swiotlb_active()) {
526 ret = sg_alloc_table(*st, num_pages, GFP_KERNEL);
527 if (ret)
528 goto err;
530 for_each_sg((*st)->sgl, sg, num_pages, n)
531 sg_set_page(sg, pvec[n], PAGE_SIZE, 0);
532 } else {
533 ret = sg_alloc_table_from_pages(*st, pvec, num_pages,
534 0, num_pages << PAGE_SHIFT,
535 GFP_KERNEL);
536 if (ret)
537 goto err;
540 return 0;
542 err:
543 kfree(*st);
544 *st = NULL;
545 return ret;
548 static int
549 __i915_gem_userptr_set_pages(struct drm_i915_gem_object *obj,
550 struct page **pvec, int num_pages)
552 int ret;
554 ret = st_set_pages(&obj->pages, pvec, num_pages);
555 if (ret)
556 return ret;
558 ret = i915_gem_gtt_prepare_object(obj);
559 if (ret) {
560 sg_free_table(obj->pages);
561 kfree(obj->pages);
562 obj->pages = NULL;
565 return ret;
568 static void
569 __i915_gem_userptr_get_pages_worker(struct work_struct *_work)
571 struct get_pages_work *work = container_of(_work, typeof(*work), work);
572 struct drm_i915_gem_object *obj = work->obj;
573 struct drm_device *dev = obj->base.dev;
574 const int num_pages = obj->base.size >> PAGE_SHIFT;
575 struct page **pvec;
576 int pinned, ret;
578 ret = -ENOMEM;
579 pinned = 0;
581 pvec = kmalloc(num_pages*sizeof(struct page *),
582 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
583 if (pvec == NULL)
584 pvec = drm_malloc_ab(num_pages, sizeof(struct page *));
585 if (pvec != NULL) {
586 struct mm_struct *mm = obj->userptr.mm->mm;
588 down_read(&mm->mmap_sem);
589 while (pinned < num_pages) {
590 ret = get_user_pages(work->task, mm,
591 obj->userptr.ptr + pinned * PAGE_SIZE,
592 num_pages - pinned,
593 !obj->userptr.read_only, 0,
594 pvec + pinned, NULL);
595 if (ret < 0)
596 break;
598 pinned += ret;
600 up_read(&mm->mmap_sem);
603 mutex_lock(&dev->struct_mutex);
604 if (obj->userptr.work != &work->work) {
605 ret = 0;
606 } else if (pinned == num_pages) {
607 ret = __i915_gem_userptr_set_pages(obj, pvec, num_pages);
608 if (ret == 0) {
609 list_add_tail(&obj->global_list, &to_i915(dev)->mm.unbound_list);
610 obj->get_page.sg = obj->pages->sgl;
611 obj->get_page.last = 0;
613 pinned = 0;
617 obj->userptr.work = ERR_PTR(ret);
618 obj->userptr.workers--;
619 drm_gem_object_unreference(&obj->base);
620 mutex_unlock(&dev->struct_mutex);
622 release_pages(pvec, pinned, 0);
623 drm_free_large(pvec);
625 put_task_struct(work->task);
626 kfree(work);
629 static int
630 i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
632 const int num_pages = obj->base.size >> PAGE_SHIFT;
633 struct page **pvec;
634 int pinned, ret;
636 /* If userspace should engineer that these pages are replaced in
637 * the vma between us binding this page into the GTT and completion
638 * of rendering... Their loss. If they change the mapping of their
639 * pages they need to create a new bo to point to the new vma.
641 * However, that still leaves open the possibility of the vma
642 * being copied upon fork. Which falls under the same userspace
643 * synchronisation issue as a regular bo, except that this time
644 * the process may not be expecting that a particular piece of
645 * memory is tied to the GPU.
647 * Fortunately, we can hook into the mmu_notifier in order to
648 * discard the page references prior to anything nasty happening
649 * to the vma (discard or cloning) which should prevent the more
650 * egregious cases from causing harm.
653 pvec = NULL;
654 pinned = 0;
655 if (obj->userptr.mm->mm == current->mm) {
656 pvec = kmalloc(num_pages*sizeof(struct page *),
657 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
658 if (pvec == NULL) {
659 pvec = drm_malloc_ab(num_pages, sizeof(struct page *));
660 if (pvec == NULL)
661 return -ENOMEM;
664 pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
665 !obj->userptr.read_only, pvec);
667 if (pinned < num_pages) {
668 if (pinned < 0) {
669 ret = pinned;
670 pinned = 0;
671 } else {
672 /* Spawn a worker so that we can acquire the
673 * user pages without holding our mutex. Access
674 * to the user pages requires mmap_sem, and we have
675 * a strict lock ordering of mmap_sem, struct_mutex -
676 * we already hold struct_mutex here and so cannot
677 * call gup without encountering a lock inversion.
679 * Userspace will keep on repeating the operation
680 * (thanks to EAGAIN) until either we hit the fast
681 * path or the worker completes. If the worker is
682 * cancelled or superseded, the task is still run
683 * but the results ignored. (This leads to
684 * complications that we may have a stray object
685 * refcount that we need to be wary of when
686 * checking for existing objects during creation.)
687 * If the worker encounters an error, it reports
688 * that error back to this function through
689 * obj->userptr.work = ERR_PTR.
691 ret = -EAGAIN;
692 if (obj->userptr.work == NULL &&
693 obj->userptr.workers < I915_GEM_USERPTR_MAX_WORKERS) {
694 struct get_pages_work *work;
696 work = kmalloc(sizeof(*work), GFP_KERNEL);
697 if (work != NULL) {
698 obj->userptr.work = &work->work;
699 obj->userptr.workers++;
701 work->obj = obj;
702 drm_gem_object_reference(&obj->base);
704 work->task = current;
705 get_task_struct(work->task);
707 INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
708 schedule_work(&work->work);
709 } else
710 ret = -ENOMEM;
711 } else {
712 if (IS_ERR(obj->userptr.work)) {
713 ret = PTR_ERR(obj->userptr.work);
714 obj->userptr.work = NULL;
718 } else {
719 ret = __i915_gem_userptr_set_pages(obj, pvec, num_pages);
720 if (ret == 0) {
721 obj->userptr.work = NULL;
722 pinned = 0;
726 release_pages(pvec, pinned, 0);
727 drm_free_large(pvec);
728 return ret;
731 static void
732 i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj)
734 struct sg_page_iter sg_iter;
736 BUG_ON(obj->userptr.work != NULL);
738 if (obj->madv != I915_MADV_WILLNEED)
739 obj->dirty = 0;
741 i915_gem_gtt_finish_object(obj);
743 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
744 struct page *page = sg_page_iter_page(&sg_iter);
746 if (obj->dirty)
747 set_page_dirty(page);
749 mark_page_accessed(page);
750 page_cache_release(page);
752 obj->dirty = 0;
754 sg_free_table(obj->pages);
755 kfree(obj->pages);
758 static void
759 i915_gem_userptr_release(struct drm_i915_gem_object *obj)
761 i915_gem_userptr_release__mmu_notifier(obj);
762 i915_gem_userptr_release__mm_struct(obj);
765 static int
766 i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
768 if (obj->userptr.mmu_object)
769 return 0;
771 return i915_gem_userptr_init__mmu_notifier(obj, 0);
774 static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
775 .dmabuf_export = i915_gem_userptr_dmabuf_export,
776 .get_pages = i915_gem_userptr_get_pages,
777 .put_pages = i915_gem_userptr_put_pages,
778 .release = i915_gem_userptr_release,
782 * Creates a new mm object that wraps some normal memory from the process
783 * context - user memory.
785 * We impose several restrictions upon the memory being mapped
786 * into the GPU.
787 * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
788 * 2. It must be normal system memory, not a pointer into another map of IO
789 * space (e.g. it must not be a GTT mmapping of another object).
790 * 3. We only allow a bo as large as we could in theory map into the GTT,
791 * that is we limit the size to the total size of the GTT.
792 * 4. The bo is marked as being snoopable. The backing pages are left
793 * accessible directly by the CPU, but reads and writes by the GPU may
794 * incur the cost of a snoop (unless you have an LLC architecture).
796 * Synchronisation between multiple users and the GPU is left to userspace
797 * through the normal set-domain-ioctl. The kernel will enforce that the
798 * GPU relinquishes the VMA before it is returned back to the system
799 * i.e. upon free(), munmap() or process termination. However, the userspace
800 * malloc() library may not immediately relinquish the VMA after free() and
801 * instead reuse it whilst the GPU is still reading and writing to the VMA.
802 * Caveat emptor.
804 * Also note, that the object created here is not currently a "first class"
805 * object, in that several ioctls are banned. These are the CPU access
806 * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
807 * direct access via your pointer rather than use those ioctls.
809 * If you think this is a good interface to use to pass GPU memory between
810 * drivers, please use dma-buf instead. In fact, wherever possible use
811 * dma-buf instead.
814 i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
816 struct drm_i915_private *dev_priv = dev->dev_private;
817 struct drm_i915_gem_userptr *args = data;
818 struct drm_i915_gem_object *obj;
819 int ret;
820 u32 handle;
822 if (args->flags & ~(I915_USERPTR_READ_ONLY |
823 I915_USERPTR_UNSYNCHRONIZED))
824 return -EINVAL;
826 if (offset_in_page(args->user_ptr | args->user_size))
827 return -EINVAL;
829 if (args->user_size > dev_priv->gtt.base.total)
830 return -E2BIG;
832 if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
833 (char __user *)(unsigned long)args->user_ptr, args->user_size))
834 return -EFAULT;
836 if (args->flags & I915_USERPTR_READ_ONLY) {
837 /* On almost all of the current hw, we cannot tell the GPU that a
838 * page is readonly, so this is just a placeholder in the uAPI.
840 return -ENODEV;
843 obj = i915_gem_object_alloc(dev);
844 if (obj == NULL)
845 return -ENOMEM;
847 drm_gem_private_object_init(dev, &obj->base, args->user_size);
848 i915_gem_object_init(obj, &i915_gem_userptr_ops);
849 obj->cache_level = I915_CACHE_LLC;
850 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
851 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
853 obj->userptr.ptr = args->user_ptr;
854 obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
856 /* And keep a pointer to the current->mm for resolving the user pages
857 * at binding. This means that we need to hook into the mmu_notifier
858 * in order to detect if the mmu is destroyed.
860 ret = i915_gem_userptr_init__mm_struct(obj);
861 if (ret == 0)
862 ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
863 if (ret == 0)
864 ret = drm_gem_handle_create(file, &obj->base, &handle);
866 /* drop reference from allocate - handle holds it now */
867 drm_gem_object_unreference_unlocked(&obj->base);
868 if (ret)
869 return ret;
871 args->handle = handle;
872 return 0;
876 i915_gem_init_userptr(struct drm_device *dev)
878 struct drm_i915_private *dev_priv = to_i915(dev);
879 mutex_init(&dev_priv->mm_lock);
880 hash_init(dev_priv->mm_structs);
881 return 0;