i915_gem_userptr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * Copyright © 2012-2014 Intel Corporation
  3. *
  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:
  10. *
  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.
  14. *
  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.
  22. *
  23. */
  24. #include "drmP.h"
  25. #include "i915_drm.h"
  26. #include "i915_drv.h"
  27. #include "i915_trace.h"
  28. #include "intel_drv.h"
  29. #include <linux/mmu_context.h>
  30. #include <linux/mmu_notifier.h>
  31. #include <linux/mempolicy.h>
  32. #include <linux/swap.h>
  33. #if defined(CONFIG_MMU_NOTIFIER)
  34. #include <linux/interval_tree.h>
  35. struct i915_mmu_notifier {
  36. spinlock_t lock;
  37. struct hlist_node node;
  38. struct mmu_notifier mn;
  39. struct rb_root objects;
  40. struct drm_device *dev;
  41. struct mm_struct *mm;
  42. struct work_struct work;
  43. unsigned long count;
  44. unsigned long serial;
  45. };
  46. struct i915_mmu_object {
  47. struct i915_mmu_notifier *mmu;
  48. struct interval_tree_node it;
  49. struct drm_i915_gem_object *obj;
  50. };
  51. static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
  52. struct mm_struct *mm,
  53. unsigned long start,
  54. unsigned long end)
  55. {
  56. struct i915_mmu_notifier *mn = container_of(_mn, struct i915_mmu_notifier, mn);
  57. struct interval_tree_node *it = NULL;
  58. unsigned long serial = 0;
  59. end--; /* interval ranges are inclusive, but invalidate range is exclusive */
  60. while (start < end) {
  61. struct drm_i915_gem_object *obj;
  62. obj = NULL;
  63. spin_lock(&mn->lock);
  64. if (serial == mn->serial)
  65. it = interval_tree_iter_next(it, start, end);
  66. else
  67. it = interval_tree_iter_first(&mn->objects, start, end);
  68. if (it != NULL) {
  69. obj = container_of(it, struct i915_mmu_object, it)->obj;
  70. drm_gem_object_reference(&obj->base);
  71. serial = mn->serial;
  72. }
  73. spin_unlock(&mn->lock);
  74. if (obj == NULL)
  75. return;
  76. mutex_lock(&mn->dev->struct_mutex);
  77. /* Cancel any active worker and force us to re-evaluate gup */
  78. obj->userptr.work = NULL;
  79. if (obj->pages != NULL) {
  80. struct drm_i915_private *dev_priv = to_i915(mn->dev);
  81. struct i915_vma *vma, *tmp;
  82. bool was_interruptible;
  83. was_interruptible = dev_priv->mm.interruptible;
  84. dev_priv->mm.interruptible = false;
  85. list_for_each_entry_safe(vma, tmp, &obj->vma_list, vma_link) {
  86. int ret = i915_vma_unbind(vma);
  87. WARN_ON(ret && ret != -EIO);
  88. }
  89. WARN_ON(i915_gem_object_put_pages(obj));
  90. dev_priv->mm.interruptible = was_interruptible;
  91. }
  92. start = obj->userptr.ptr + obj->base.size;
  93. drm_gem_object_unreference(&obj->base);
  94. mutex_unlock(&mn->dev->struct_mutex);
  95. }
  96. }
  97. static const struct mmu_notifier_ops i915_gem_userptr_notifier = {
  98. .invalidate_range_start = i915_gem_userptr_mn_invalidate_range_start,
  99. };
  100. static struct i915_mmu_notifier *
  101. __i915_mmu_notifier_lookup(struct drm_device *dev, struct mm_struct *mm)
  102. {
  103. struct drm_i915_private *dev_priv = to_i915(dev);
  104. struct i915_mmu_notifier *mmu;
  105. /* Protected by dev->struct_mutex */
  106. hash_for_each_possible(dev_priv->mmu_notifiers, mmu, node, (unsigned long)mm)
  107. if (mmu->mm == mm)
  108. return mmu;
  109. return NULL;
  110. }
  111. static struct i915_mmu_notifier *
  112. i915_mmu_notifier_get(struct drm_device *dev, struct mm_struct *mm)
  113. {
  114. struct drm_i915_private *dev_priv = to_i915(dev);
  115. struct i915_mmu_notifier *mmu;
  116. int ret;
  117. lockdep_assert_held(&dev->struct_mutex);
  118. mmu = __i915_mmu_notifier_lookup(dev, mm);
  119. if (mmu)
  120. return mmu;
  121. mmu = kmalloc(sizeof(*mmu), GFP_KERNEL);
  122. if (mmu == NULL)
  123. return ERR_PTR(-ENOMEM);
  124. spin_lock_init(&mmu->lock);
  125. mmu->dev = dev;
  126. mmu->mn.ops = &i915_gem_userptr_notifier;
  127. mmu->mm = mm;
  128. mmu->objects = RB_ROOT;
  129. mmu->count = 0;
  130. mmu->serial = 0;
  131. /* Protected by mmap_sem (write-lock) */
  132. ret = __mmu_notifier_register(&mmu->mn, mm);
  133. if (ret) {
  134. kfree(mmu);
  135. return ERR_PTR(ret);
  136. }
  137. /* Protected by dev->struct_mutex */
  138. hash_add(dev_priv->mmu_notifiers, &mmu->node, (unsigned long)mm);
  139. return mmu;
  140. }
  141. static void
  142. __i915_mmu_notifier_destroy_worker(struct work_struct *work)
  143. {
  144. struct i915_mmu_notifier *mmu = container_of(work, typeof(*mmu), work);
  145. mmu_notifier_unregister(&mmu->mn, mmu->mm);
  146. kfree(mmu);
  147. }
  148. static void
  149. __i915_mmu_notifier_destroy(struct i915_mmu_notifier *mmu)
  150. {
  151. lockdep_assert_held(&mmu->dev->struct_mutex);
  152. /* Protected by dev->struct_mutex */
  153. hash_del(&mmu->node);
  154. /* Our lock ordering is: mmap_sem, mmu_notifier_scru, struct_mutex.
  155. * We enter the function holding struct_mutex, therefore we need
  156. * to drop our mutex prior to calling mmu_notifier_unregister in
  157. * order to prevent lock inversion (and system-wide deadlock)
  158. * between the mmap_sem and struct-mutex. Hence we defer the
  159. * unregistration to a workqueue where we hold no locks.
  160. */
  161. INIT_WORK(&mmu->work, __i915_mmu_notifier_destroy_worker);
  162. schedule_work(&mmu->work);
  163. }
  164. static void __i915_mmu_notifier_update_serial(struct i915_mmu_notifier *mmu)
  165. {
  166. if (++mmu->serial == 0)
  167. mmu->serial = 1;
  168. }
  169. static void
  170. i915_mmu_notifier_del(struct i915_mmu_notifier *mmu,
  171. struct i915_mmu_object *mn)
  172. {
  173. lockdep_assert_held(&mmu->dev->struct_mutex);
  174. spin_lock(&mmu->lock);
  175. interval_tree_remove(&mn->it, &mmu->objects);
  176. __i915_mmu_notifier_update_serial(mmu);
  177. spin_unlock(&mmu->lock);
  178. /* Protected against _add() by dev->struct_mutex */
  179. if (--mmu->count == 0)
  180. __i915_mmu_notifier_destroy(mmu);
  181. }
  182. static int
  183. i915_mmu_notifier_add(struct i915_mmu_notifier *mmu,
  184. struct i915_mmu_object *mn)
  185. {
  186. struct interval_tree_node *it;
  187. int ret;
  188. ret = i915_mutex_lock_interruptible(mmu->dev);
  189. if (ret)
  190. return ret;
  191. /* Make sure we drop the final active reference (and thereby
  192. * remove the objects from the interval tree) before we do
  193. * the check for overlapping objects.
  194. */
  195. i915_gem_retire_requests(mmu->dev);
  196. /* Disallow overlapping userptr objects */
  197. spin_lock(&mmu->lock);
  198. it = interval_tree_iter_first(&mmu->objects,
  199. mn->it.start, mn->it.last);
  200. if (it) {
  201. struct drm_i915_gem_object *obj;
  202. /* We only need to check the first object in the range as it
  203. * either has cancelled gup work queued and we need to
  204. * return back to the user to give time for the gup-workers
  205. * to flush their object references upon which the object will
  206. * be removed from the interval-tree, or the the range is
  207. * still in use by another client and the overlap is invalid.
  208. */
  209. obj = container_of(it, struct i915_mmu_object, it)->obj;
  210. ret = obj->userptr.workers ? -EAGAIN : -EINVAL;
  211. } else {
  212. interval_tree_insert(&mn->it, &mmu->objects);
  213. __i915_mmu_notifier_update_serial(mmu);
  214. ret = 0;
  215. }
  216. spin_unlock(&mmu->lock);
  217. mutex_unlock(&mmu->dev->struct_mutex);
  218. return ret;
  219. }
  220. static void
  221. i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
  222. {
  223. struct i915_mmu_object *mn;
  224. mn = obj->userptr.mn;
  225. if (mn == NULL)
  226. return;
  227. i915_mmu_notifier_del(mn->mmu, mn);
  228. obj->userptr.mn = NULL;
  229. }
  230. static int
  231. i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
  232. unsigned flags)
  233. {
  234. struct i915_mmu_notifier *mmu;
  235. struct i915_mmu_object *mn;
  236. int ret;
  237. if (flags & I915_USERPTR_UNSYNCHRONIZED)
  238. return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
  239. down_write(&obj->userptr.mm->mmap_sem);
  240. ret = i915_mutex_lock_interruptible(obj->base.dev);
  241. if (ret == 0) {
  242. mmu = i915_mmu_notifier_get(obj->base.dev, obj->userptr.mm);
  243. if (!IS_ERR(mmu))
  244. mmu->count++; /* preemptive add to act as a refcount */
  245. else
  246. ret = PTR_ERR(mmu);
  247. mutex_unlock(&obj->base.dev->struct_mutex);
  248. }
  249. up_write(&obj->userptr.mm->mmap_sem);
  250. if (ret)
  251. return ret;
  252. mn = kzalloc(sizeof(*mn), GFP_KERNEL);
  253. if (mn == NULL) {
  254. ret = -ENOMEM;
  255. goto destroy_mmu;
  256. }
  257. mn->mmu = mmu;
  258. mn->it.start = obj->userptr.ptr;
  259. mn->it.last = mn->it.start + obj->base.size - 1;
  260. mn->obj = obj;
  261. ret = i915_mmu_notifier_add(mmu, mn);
  262. if (ret)
  263. goto free_mn;
  264. obj->userptr.mn = mn;
  265. return 0;
  266. free_mn:
  267. kfree(mn);
  268. destroy_mmu:
  269. mutex_lock(&obj->base.dev->struct_mutex);
  270. if (--mmu->count == 0)
  271. __i915_mmu_notifier_destroy(mmu);
  272. mutex_unlock(&obj->base.dev->struct_mutex);
  273. return ret;
  274. }
  275. #else
  276. static void
  277. i915_gem_userptr_release__mmu_notifier(struct drm_i915_gem_object *obj)
  278. {
  279. }
  280. static int
  281. i915_gem_userptr_init__mmu_notifier(struct drm_i915_gem_object *obj,
  282. unsigned flags)
  283. {
  284. if ((flags & I915_USERPTR_UNSYNCHRONIZED) == 0)
  285. return -ENODEV;
  286. if (!capable(CAP_SYS_ADMIN))
  287. return -EPERM;
  288. return 0;
  289. }
  290. #endif
  291. struct get_pages_work {
  292. struct work_struct work;
  293. struct drm_i915_gem_object *obj;
  294. struct task_struct *task;
  295. };
  296. #if IS_ENABLED(CONFIG_SWIOTLB)
  297. #define swiotlb_active() swiotlb_nr_tbl()
  298. #else
  299. #define swiotlb_active() 0
  300. #endif
  301. static int
  302. st_set_pages(struct sg_table **st, struct page **pvec, int num_pages)
  303. {
  304. struct scatterlist *sg;
  305. int ret, n;
  306. *st = kmalloc(sizeof(**st), GFP_KERNEL);
  307. if (*st == NULL)
  308. return -ENOMEM;
  309. if (swiotlb_active()) {
  310. ret = sg_alloc_table(*st, num_pages, GFP_KERNEL);
  311. if (ret)
  312. goto err;
  313. for_each_sg((*st)->sgl, sg, num_pages, n)
  314. sg_set_page(sg, pvec[n], PAGE_SIZE, 0);
  315. } else {
  316. ret = sg_alloc_table_from_pages(*st, pvec, num_pages,
  317. 0, num_pages << PAGE_SHIFT,
  318. GFP_KERNEL);
  319. if (ret)
  320. goto err;
  321. }
  322. return 0;
  323. err:
  324. kfree(*st);
  325. *st = NULL;
  326. return ret;
  327. }
  328. static void
  329. __i915_gem_userptr_get_pages_worker(struct work_struct *_work)
  330. {
  331. struct get_pages_work *work = container_of(_work, typeof(*work), work);
  332. struct drm_i915_gem_object *obj = work->obj;
  333. struct drm_device *dev = obj->base.dev;
  334. const int num_pages = obj->base.size >> PAGE_SHIFT;
  335. struct page **pvec;
  336. int pinned, ret;
  337. ret = -ENOMEM;
  338. pinned = 0;
  339. pvec = kmalloc(num_pages*sizeof(struct page *),
  340. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  341. if (pvec == NULL)
  342. pvec = drm_malloc_ab(num_pages, sizeof(struct page *));
  343. if (pvec != NULL) {
  344. struct mm_struct *mm = obj->userptr.mm;
  345. down_read(&mm->mmap_sem);
  346. while (pinned < num_pages) {
  347. ret = get_user_pages(work->task, mm,
  348. obj->userptr.ptr + pinned * PAGE_SIZE,
  349. num_pages - pinned,
  350. !obj->userptr.read_only, 0,
  351. pvec + pinned, NULL);
  352. if (ret < 0)
  353. break;
  354. pinned += ret;
  355. }
  356. up_read(&mm->mmap_sem);
  357. }
  358. mutex_lock(&dev->struct_mutex);
  359. if (obj->userptr.work != &work->work) {
  360. ret = 0;
  361. } else if (pinned == num_pages) {
  362. ret = st_set_pages(&obj->pages, pvec, num_pages);
  363. if (ret == 0) {
  364. list_add_tail(&obj->global_list, &to_i915(dev)->mm.unbound_list);
  365. pinned = 0;
  366. }
  367. }
  368. obj->userptr.work = ERR_PTR(ret);
  369. obj->userptr.workers--;
  370. drm_gem_object_unreference(&obj->base);
  371. mutex_unlock(&dev->struct_mutex);
  372. release_pages(pvec, pinned, 0);
  373. drm_free_large(pvec);
  374. put_task_struct(work->task);
  375. kfree(work);
  376. }
  377. static int
  378. i915_gem_userptr_get_pages(struct drm_i915_gem_object *obj)
  379. {
  380. const int num_pages = obj->base.size >> PAGE_SHIFT;
  381. struct page **pvec;
  382. int pinned, ret;
  383. /* If userspace should engineer that these pages are replaced in
  384. * the vma between us binding this page into the GTT and completion
  385. * of rendering... Their loss. If they change the mapping of their
  386. * pages they need to create a new bo to point to the new vma.
  387. *
  388. * However, that still leaves open the possibility of the vma
  389. * being copied upon fork. Which falls under the same userspace
  390. * synchronisation issue as a regular bo, except that this time
  391. * the process may not be expecting that a particular piece of
  392. * memory is tied to the GPU.
  393. *
  394. * Fortunately, we can hook into the mmu_notifier in order to
  395. * discard the page references prior to anything nasty happening
  396. * to the vma (discard or cloning) which should prevent the more
  397. * egregious cases from causing harm.
  398. */
  399. pvec = NULL;
  400. pinned = 0;
  401. if (obj->userptr.mm == current->mm) {
  402. pvec = kmalloc(num_pages*sizeof(struct page *),
  403. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  404. if (pvec == NULL) {
  405. pvec = drm_malloc_ab(num_pages, sizeof(struct page *));
  406. if (pvec == NULL)
  407. return -ENOMEM;
  408. }
  409. pinned = __get_user_pages_fast(obj->userptr.ptr, num_pages,
  410. !obj->userptr.read_only, pvec);
  411. }
  412. if (pinned < num_pages) {
  413. if (pinned < 0) {
  414. ret = pinned;
  415. pinned = 0;
  416. } else {
  417. /* Spawn a worker so that we can acquire the
  418. * user pages without holding our mutex. Access
  419. * to the user pages requires mmap_sem, and we have
  420. * a strict lock ordering of mmap_sem, struct_mutex -
  421. * we already hold struct_mutex here and so cannot
  422. * call gup without encountering a lock inversion.
  423. *
  424. * Userspace will keep on repeating the operation
  425. * (thanks to EAGAIN) until either we hit the fast
  426. * path or the worker completes. If the worker is
  427. * cancelled or superseded, the task is still run
  428. * but the results ignored. (This leads to
  429. * complications that we may have a stray object
  430. * refcount that we need to be wary of when
  431. * checking for existing objects during creation.)
  432. * If the worker encounters an error, it reports
  433. * that error back to this function through
  434. * obj->userptr.work = ERR_PTR.
  435. */
  436. ret = -EAGAIN;
  437. if (obj->userptr.work == NULL &&
  438. obj->userptr.workers < I915_GEM_USERPTR_MAX_WORKERS) {
  439. struct get_pages_work *work;
  440. work = kmalloc(sizeof(*work), GFP_KERNEL);
  441. if (work != NULL) {
  442. obj->userptr.work = &work->work;
  443. obj->userptr.workers++;
  444. work->obj = obj;
  445. drm_gem_object_reference(&obj->base);
  446. work->task = current;
  447. get_task_struct(work->task);
  448. INIT_WORK(&work->work, __i915_gem_userptr_get_pages_worker);
  449. schedule_work(&work->work);
  450. } else
  451. ret = -ENOMEM;
  452. } else {
  453. if (IS_ERR(obj->userptr.work)) {
  454. ret = PTR_ERR(obj->userptr.work);
  455. obj->userptr.work = NULL;
  456. }
  457. }
  458. }
  459. } else {
  460. ret = st_set_pages(&obj->pages, pvec, num_pages);
  461. if (ret == 0) {
  462. obj->userptr.work = NULL;
  463. pinned = 0;
  464. }
  465. }
  466. release_pages(pvec, pinned, 0);
  467. drm_free_large(pvec);
  468. return ret;
  469. }
  470. static void
  471. i915_gem_userptr_put_pages(struct drm_i915_gem_object *obj)
  472. {
  473. struct scatterlist *sg;
  474. int i;
  475. BUG_ON(obj->userptr.work != NULL);
  476. if (obj->madv != I915_MADV_WILLNEED)
  477. obj->dirty = 0;
  478. for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i) {
  479. struct page *page = sg_page(sg);
  480. if (obj->dirty)
  481. set_page_dirty(page);
  482. mark_page_accessed(page);
  483. page_cache_release(page);
  484. }
  485. obj->dirty = 0;
  486. sg_free_table(obj->pages);
  487. kfree(obj->pages);
  488. }
  489. static void
  490. i915_gem_userptr_release(struct drm_i915_gem_object *obj)
  491. {
  492. i915_gem_userptr_release__mmu_notifier(obj);
  493. if (obj->userptr.mm) {
  494. mmput(obj->userptr.mm);
  495. obj->userptr.mm = NULL;
  496. }
  497. }
  498. static int
  499. i915_gem_userptr_dmabuf_export(struct drm_i915_gem_object *obj)
  500. {
  501. if (obj->userptr.mn)
  502. return 0;
  503. return i915_gem_userptr_init__mmu_notifier(obj, 0);
  504. }
  505. static const struct drm_i915_gem_object_ops i915_gem_userptr_ops = {
  506. .dmabuf_export = i915_gem_userptr_dmabuf_export,
  507. .get_pages = i915_gem_userptr_get_pages,
  508. .put_pages = i915_gem_userptr_put_pages,
  509. .release = i915_gem_userptr_release,
  510. };
  511. /**
  512. * Creates a new mm object that wraps some normal memory from the process
  513. * context - user memory.
  514. *
  515. * We impose several restrictions upon the memory being mapped
  516. * into the GPU.
  517. * 1. It must be page aligned (both start/end addresses, i.e ptr and size).
  518. * 2. It cannot overlap any other userptr object in the same address space.
  519. * 3. It must be normal system memory, not a pointer into another map of IO
  520. * space (e.g. it must not be a GTT mmapping of another object).
  521. * 4. We only allow a bo as large as we could in theory map into the GTT,
  522. * that is we limit the size to the total size of the GTT.
  523. * 5. The bo is marked as being snoopable. The backing pages are left
  524. * accessible directly by the CPU, but reads and writes by the GPU may
  525. * incur the cost of a snoop (unless you have an LLC architecture).
  526. *
  527. * Synchronisation between multiple users and the GPU is left to userspace
  528. * through the normal set-domain-ioctl. The kernel will enforce that the
  529. * GPU relinquishes the VMA before it is returned back to the system
  530. * i.e. upon free(), munmap() or process termination. However, the userspace
  531. * malloc() library may not immediately relinquish the VMA after free() and
  532. * instead reuse it whilst the GPU is still reading and writing to the VMA.
  533. * Caveat emptor.
  534. *
  535. * Also note, that the object created here is not currently a "first class"
  536. * object, in that several ioctls are banned. These are the CPU access
  537. * ioctls: mmap(), pwrite and pread. In practice, you are expected to use
  538. * direct access via your pointer rather than use those ioctls.
  539. *
  540. * If you think this is a good interface to use to pass GPU memory between
  541. * drivers, please use dma-buf instead. In fact, wherever possible use
  542. * dma-buf instead.
  543. */
  544. int
  545. i915_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  546. {
  547. struct drm_i915_private *dev_priv = dev->dev_private;
  548. struct drm_i915_gem_userptr *args = data;
  549. struct drm_i915_gem_object *obj;
  550. int ret;
  551. u32 handle;
  552. if (args->flags & ~(I915_USERPTR_READ_ONLY |
  553. I915_USERPTR_UNSYNCHRONIZED))
  554. return -EINVAL;
  555. if (offset_in_page(args->user_ptr | args->user_size))
  556. return -EINVAL;
  557. if (args->user_size > dev_priv->gtt.base.total)
  558. return -E2BIG;
  559. if (!access_ok(args->flags & I915_USERPTR_READ_ONLY ? VERIFY_READ : VERIFY_WRITE,
  560. (char __user *)(unsigned long)args->user_ptr, args->user_size))
  561. return -EFAULT;
  562. if (args->flags & I915_USERPTR_READ_ONLY) {
  563. /* On almost all of the current hw, we cannot tell the GPU that a
  564. * page is readonly, so this is just a placeholder in the uAPI.
  565. */
  566. return -ENODEV;
  567. }
  568. /* Allocate the new object */
  569. obj = i915_gem_object_alloc(dev);
  570. if (obj == NULL)
  571. return -ENOMEM;
  572. drm_gem_private_object_init(dev, &obj->base, args->user_size);
  573. i915_gem_object_init(obj, &i915_gem_userptr_ops);
  574. obj->cache_level = I915_CACHE_LLC;
  575. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  576. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  577. obj->userptr.ptr = args->user_ptr;
  578. obj->userptr.read_only = !!(args->flags & I915_USERPTR_READ_ONLY);
  579. /* And keep a pointer to the current->mm for resolving the user pages
  580. * at binding. This means that we need to hook into the mmu_notifier
  581. * in order to detect if the mmu is destroyed.
  582. */
  583. ret = -ENOMEM;
  584. if ((obj->userptr.mm = get_task_mm(current)))
  585. ret = i915_gem_userptr_init__mmu_notifier(obj, args->flags);
  586. if (ret == 0)
  587. ret = drm_gem_handle_create(file, &obj->base, &handle);
  588. /* drop reference from allocate - handle holds it now */
  589. drm_gem_object_unreference_unlocked(&obj->base);
  590. if (ret)
  591. return ret;
  592. args->handle = handle;
  593. return 0;
  594. }
  595. int
  596. i915_gem_init_userptr(struct drm_device *dev)
  597. {
  598. #if defined(CONFIG_MMU_NOTIFIER)
  599. struct drm_i915_private *dev_priv = to_i915(dev);
  600. hash_init(dev_priv->mmu_notifiers);
  601. #endif
  602. return 0;
  603. }