drm_gem.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * Copyright © 2008 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. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/mm.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/fs.h>
  32. #include <linux/file.h>
  33. #include <linux/module.h>
  34. #include <linux/mman.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/shmem_fs.h>
  37. #include <linux/dma-buf.h>
  38. #include <drm/drmP.h>
  39. #include <drm/drm_vma_manager.h>
  40. /** @file drm_gem.c
  41. *
  42. * This file provides some of the base ioctls and library routines for
  43. * the graphics memory manager implemented by each device driver.
  44. *
  45. * Because various devices have different requirements in terms of
  46. * synchronization and migration strategies, implementing that is left up to
  47. * the driver, and all that the general API provides should be generic --
  48. * allocating objects, reading/writing data with the cpu, freeing objects.
  49. * Even there, platform-dependent optimizations for reading/writing data with
  50. * the CPU mean we'll likely hook those out to driver-specific calls. However,
  51. * the DRI2 implementation wants to have at least allocate/mmap be generic.
  52. *
  53. * The goal was to have swap-backed object allocation managed through
  54. * struct file. However, file descriptors as handles to a struct file have
  55. * two major failings:
  56. * - Process limits prevent more than 1024 or so being used at a time by
  57. * default.
  58. * - Inability to allocate high fds will aggravate the X Server's select()
  59. * handling, and likely that of many GL client applications as well.
  60. *
  61. * This led to a plan of using our own integer IDs (called handles, following
  62. * DRM terminology) to mimic fds, and implement the fd syscalls we need as
  63. * ioctls. The objects themselves will still include the struct file so
  64. * that we can transition to fds if the required kernel infrastructure shows
  65. * up at a later date, and as our interface with shmfs for memory allocation.
  66. */
  67. /*
  68. * We make up offsets for buffer objects so we can recognize them at
  69. * mmap time.
  70. */
  71. /* pgoff in mmap is an unsigned long, so we need to make sure that
  72. * the faked up offset will fit
  73. */
  74. #if BITS_PER_LONG == 64
  75. #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
  76. #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
  77. #else
  78. #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
  79. #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
  80. #endif
  81. /**
  82. * Initialize the GEM device fields
  83. */
  84. int
  85. drm_gem_init(struct drm_device *dev)
  86. {
  87. struct drm_gem_mm *mm;
  88. spin_lock_init(&dev->object_name_lock);
  89. idr_init(&dev->object_name_idr);
  90. mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
  91. if (!mm) {
  92. DRM_ERROR("out of memory\n");
  93. return -ENOMEM;
  94. }
  95. dev->mm_private = mm;
  96. drm_vma_offset_manager_init(&mm->vma_manager,
  97. DRM_FILE_PAGE_OFFSET_START,
  98. DRM_FILE_PAGE_OFFSET_SIZE);
  99. return 0;
  100. }
  101. void
  102. drm_gem_destroy(struct drm_device *dev)
  103. {
  104. struct drm_gem_mm *mm = dev->mm_private;
  105. drm_vma_offset_manager_destroy(&mm->vma_manager);
  106. kfree(mm);
  107. dev->mm_private = NULL;
  108. }
  109. /**
  110. * Initialize an already allocated GEM object of the specified size with
  111. * shmfs backing store.
  112. */
  113. int drm_gem_object_init(struct drm_device *dev,
  114. struct drm_gem_object *obj, size_t size)
  115. {
  116. struct file *filp;
  117. filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
  118. if (IS_ERR(filp))
  119. return PTR_ERR(filp);
  120. drm_gem_private_object_init(dev, obj, size);
  121. obj->filp = filp;
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(drm_gem_object_init);
  125. /**
  126. * Initialize an already allocated GEM object of the specified size with
  127. * no GEM provided backing store. Instead the caller is responsible for
  128. * backing the object and handling it.
  129. */
  130. void drm_gem_private_object_init(struct drm_device *dev,
  131. struct drm_gem_object *obj, size_t size)
  132. {
  133. BUG_ON((size & (PAGE_SIZE - 1)) != 0);
  134. obj->dev = dev;
  135. obj->filp = NULL;
  136. kref_init(&obj->refcount);
  137. atomic_set(&obj->handle_count, 0);
  138. obj->size = size;
  139. }
  140. EXPORT_SYMBOL(drm_gem_private_object_init);
  141. /**
  142. * Allocate a GEM object of the specified size with shmfs backing store
  143. */
  144. struct drm_gem_object *
  145. drm_gem_object_alloc(struct drm_device *dev, size_t size)
  146. {
  147. struct drm_gem_object *obj;
  148. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  149. if (!obj)
  150. goto free;
  151. if (drm_gem_object_init(dev, obj, size) != 0)
  152. goto free;
  153. if (dev->driver->gem_init_object != NULL &&
  154. dev->driver->gem_init_object(obj) != 0) {
  155. goto fput;
  156. }
  157. return obj;
  158. fput:
  159. /* Object_init mangles the global counters - readjust them. */
  160. fput(obj->filp);
  161. free:
  162. kfree(obj);
  163. return NULL;
  164. }
  165. EXPORT_SYMBOL(drm_gem_object_alloc);
  166. static void
  167. drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
  168. {
  169. if (obj->import_attach) {
  170. drm_prime_remove_buf_handle(&filp->prime,
  171. obj->import_attach->dmabuf);
  172. }
  173. if (obj->export_dma_buf) {
  174. drm_prime_remove_buf_handle(&filp->prime,
  175. obj->export_dma_buf);
  176. }
  177. }
  178. /**
  179. * Removes the mapping from handle to filp for this object.
  180. */
  181. int
  182. drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  183. {
  184. struct drm_device *dev;
  185. struct drm_gem_object *obj;
  186. /* This is gross. The idr system doesn't let us try a delete and
  187. * return an error code. It just spews if you fail at deleting.
  188. * So, we have to grab a lock around finding the object and then
  189. * doing the delete on it and dropping the refcount, or the user
  190. * could race us to double-decrement the refcount and cause a
  191. * use-after-free later. Given the frequency of our handle lookups,
  192. * we may want to use ida for number allocation and a hash table
  193. * for the pointers, anyway.
  194. */
  195. spin_lock(&filp->table_lock);
  196. /* Check if we currently have a reference on the object */
  197. obj = idr_find(&filp->object_idr, handle);
  198. if (obj == NULL) {
  199. spin_unlock(&filp->table_lock);
  200. return -EINVAL;
  201. }
  202. dev = obj->dev;
  203. /* Release reference and decrement refcount. */
  204. idr_remove(&filp->object_idr, handle);
  205. spin_unlock(&filp->table_lock);
  206. drm_gem_remove_prime_handles(obj, filp);
  207. if (dev->driver->gem_close_object)
  208. dev->driver->gem_close_object(obj, filp);
  209. drm_gem_object_handle_unreference_unlocked(obj);
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(drm_gem_handle_delete);
  213. /**
  214. * Create a handle for this object. This adds a handle reference
  215. * to the object, which includes a regular reference count. Callers
  216. * will likely want to dereference the object afterwards.
  217. */
  218. int
  219. drm_gem_handle_create(struct drm_file *file_priv,
  220. struct drm_gem_object *obj,
  221. u32 *handlep)
  222. {
  223. struct drm_device *dev = obj->dev;
  224. int ret;
  225. /*
  226. * Get the user-visible handle using idr. Preload and perform
  227. * allocation under our spinlock.
  228. */
  229. idr_preload(GFP_KERNEL);
  230. spin_lock(&file_priv->table_lock);
  231. ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
  232. spin_unlock(&file_priv->table_lock);
  233. idr_preload_end();
  234. if (ret < 0)
  235. return ret;
  236. *handlep = ret;
  237. drm_gem_object_handle_reference(obj);
  238. if (dev->driver->gem_open_object) {
  239. ret = dev->driver->gem_open_object(obj, file_priv);
  240. if (ret) {
  241. drm_gem_handle_delete(file_priv, *handlep);
  242. return ret;
  243. }
  244. }
  245. return 0;
  246. }
  247. EXPORT_SYMBOL(drm_gem_handle_create);
  248. /**
  249. * drm_gem_free_mmap_offset - release a fake mmap offset for an object
  250. * @obj: obj in question
  251. *
  252. * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
  253. */
  254. void
  255. drm_gem_free_mmap_offset(struct drm_gem_object *obj)
  256. {
  257. struct drm_device *dev = obj->dev;
  258. struct drm_gem_mm *mm = dev->mm_private;
  259. drm_vma_offset_remove(&mm->vma_manager, &obj->vma_node);
  260. }
  261. EXPORT_SYMBOL(drm_gem_free_mmap_offset);
  262. /**
  263. * drm_gem_create_mmap_offset - create a fake mmap offset for an object
  264. * @obj: obj in question
  265. *
  266. * GEM memory mapping works by handing back to userspace a fake mmap offset
  267. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  268. * up the object based on the offset and sets up the various memory mapping
  269. * structures.
  270. *
  271. * This routine allocates and attaches a fake offset for @obj.
  272. */
  273. int
  274. drm_gem_create_mmap_offset(struct drm_gem_object *obj)
  275. {
  276. struct drm_device *dev = obj->dev;
  277. struct drm_gem_mm *mm = dev->mm_private;
  278. return drm_vma_offset_add(&mm->vma_manager, &obj->vma_node,
  279. obj->size / PAGE_SIZE);
  280. }
  281. EXPORT_SYMBOL(drm_gem_create_mmap_offset);
  282. /** Returns a reference to the object named by the handle. */
  283. struct drm_gem_object *
  284. drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
  285. u32 handle)
  286. {
  287. struct drm_gem_object *obj;
  288. spin_lock(&filp->table_lock);
  289. /* Check if we currently have a reference on the object */
  290. obj = idr_find(&filp->object_idr, handle);
  291. if (obj == NULL) {
  292. spin_unlock(&filp->table_lock);
  293. return NULL;
  294. }
  295. drm_gem_object_reference(obj);
  296. spin_unlock(&filp->table_lock);
  297. return obj;
  298. }
  299. EXPORT_SYMBOL(drm_gem_object_lookup);
  300. /**
  301. * Releases the handle to an mm object.
  302. */
  303. int
  304. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  305. struct drm_file *file_priv)
  306. {
  307. struct drm_gem_close *args = data;
  308. int ret;
  309. if (!(dev->driver->driver_features & DRIVER_GEM))
  310. return -ENODEV;
  311. ret = drm_gem_handle_delete(file_priv, args->handle);
  312. return ret;
  313. }
  314. /**
  315. * Create a global name for an object, returning the name.
  316. *
  317. * Note that the name does not hold a reference; when the object
  318. * is freed, the name goes away.
  319. */
  320. int
  321. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  322. struct drm_file *file_priv)
  323. {
  324. struct drm_gem_flink *args = data;
  325. struct drm_gem_object *obj;
  326. int ret;
  327. if (!(dev->driver->driver_features & DRIVER_GEM))
  328. return -ENODEV;
  329. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  330. if (obj == NULL)
  331. return -ENOENT;
  332. idr_preload(GFP_KERNEL);
  333. spin_lock(&dev->object_name_lock);
  334. if (!obj->name) {
  335. ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT);
  336. if (ret < 0)
  337. goto err;
  338. obj->name = ret;
  339. /* Allocate a reference for the name table. */
  340. drm_gem_object_reference(obj);
  341. }
  342. args->name = (uint64_t) obj->name;
  343. ret = 0;
  344. err:
  345. spin_unlock(&dev->object_name_lock);
  346. idr_preload_end();
  347. drm_gem_object_unreference_unlocked(obj);
  348. return ret;
  349. }
  350. /**
  351. * Open an object using the global name, returning a handle and the size.
  352. *
  353. * This handle (of course) holds a reference to the object, so the object
  354. * will not go away until the handle is deleted.
  355. */
  356. int
  357. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  358. struct drm_file *file_priv)
  359. {
  360. struct drm_gem_open *args = data;
  361. struct drm_gem_object *obj;
  362. int ret;
  363. u32 handle;
  364. if (!(dev->driver->driver_features & DRIVER_GEM))
  365. return -ENODEV;
  366. spin_lock(&dev->object_name_lock);
  367. obj = idr_find(&dev->object_name_idr, (int) args->name);
  368. if (obj)
  369. drm_gem_object_reference(obj);
  370. spin_unlock(&dev->object_name_lock);
  371. if (!obj)
  372. return -ENOENT;
  373. ret = drm_gem_handle_create(file_priv, obj, &handle);
  374. drm_gem_object_unreference_unlocked(obj);
  375. if (ret)
  376. return ret;
  377. args->handle = handle;
  378. args->size = obj->size;
  379. return 0;
  380. }
  381. /**
  382. * Called at device open time, sets up the structure for handling refcounting
  383. * of mm objects.
  384. */
  385. void
  386. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  387. {
  388. idr_init(&file_private->object_idr);
  389. spin_lock_init(&file_private->table_lock);
  390. }
  391. /**
  392. * Called at device close to release the file's
  393. * handle references on objects.
  394. */
  395. static int
  396. drm_gem_object_release_handle(int id, void *ptr, void *data)
  397. {
  398. struct drm_file *file_priv = data;
  399. struct drm_gem_object *obj = ptr;
  400. struct drm_device *dev = obj->dev;
  401. drm_gem_remove_prime_handles(obj, file_priv);
  402. if (dev->driver->gem_close_object)
  403. dev->driver->gem_close_object(obj, file_priv);
  404. drm_gem_object_handle_unreference_unlocked(obj);
  405. return 0;
  406. }
  407. /**
  408. * Called at close time when the filp is going away.
  409. *
  410. * Releases any remaining references on objects by this filp.
  411. */
  412. void
  413. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  414. {
  415. idr_for_each(&file_private->object_idr,
  416. &drm_gem_object_release_handle, file_private);
  417. idr_destroy(&file_private->object_idr);
  418. }
  419. void
  420. drm_gem_object_release(struct drm_gem_object *obj)
  421. {
  422. if (obj->filp)
  423. fput(obj->filp);
  424. }
  425. EXPORT_SYMBOL(drm_gem_object_release);
  426. /**
  427. * Called after the last reference to the object has been lost.
  428. * Must be called holding struct_ mutex
  429. *
  430. * Frees the object
  431. */
  432. void
  433. drm_gem_object_free(struct kref *kref)
  434. {
  435. struct drm_gem_object *obj = (struct drm_gem_object *) kref;
  436. struct drm_device *dev = obj->dev;
  437. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  438. if (dev->driver->gem_free_object != NULL)
  439. dev->driver->gem_free_object(obj);
  440. }
  441. EXPORT_SYMBOL(drm_gem_object_free);
  442. static void drm_gem_object_ref_bug(struct kref *list_kref)
  443. {
  444. BUG();
  445. }
  446. /**
  447. * Called after the last handle to the object has been closed
  448. *
  449. * Removes any name for the object. Note that this must be
  450. * called before drm_gem_object_free or we'll be touching
  451. * freed memory
  452. */
  453. void drm_gem_object_handle_free(struct drm_gem_object *obj)
  454. {
  455. struct drm_device *dev = obj->dev;
  456. /* Remove any name for this object */
  457. spin_lock(&dev->object_name_lock);
  458. if (obj->name) {
  459. idr_remove(&dev->object_name_idr, obj->name);
  460. obj->name = 0;
  461. spin_unlock(&dev->object_name_lock);
  462. /*
  463. * The object name held a reference to this object, drop
  464. * that now.
  465. *
  466. * This cannot be the last reference, since the handle holds one too.
  467. */
  468. kref_put(&obj->refcount, drm_gem_object_ref_bug);
  469. } else
  470. spin_unlock(&dev->object_name_lock);
  471. }
  472. EXPORT_SYMBOL(drm_gem_object_handle_free);
  473. void drm_gem_vm_open(struct vm_area_struct *vma)
  474. {
  475. struct drm_gem_object *obj = vma->vm_private_data;
  476. drm_gem_object_reference(obj);
  477. mutex_lock(&obj->dev->struct_mutex);
  478. drm_vm_open_locked(obj->dev, vma);
  479. mutex_unlock(&obj->dev->struct_mutex);
  480. }
  481. EXPORT_SYMBOL(drm_gem_vm_open);
  482. void drm_gem_vm_close(struct vm_area_struct *vma)
  483. {
  484. struct drm_gem_object *obj = vma->vm_private_data;
  485. struct drm_device *dev = obj->dev;
  486. mutex_lock(&dev->struct_mutex);
  487. drm_vm_close_locked(obj->dev, vma);
  488. drm_gem_object_unreference(obj);
  489. mutex_unlock(&dev->struct_mutex);
  490. }
  491. EXPORT_SYMBOL(drm_gem_vm_close);
  492. /**
  493. * drm_gem_mmap_obj - memory map a GEM object
  494. * @obj: the GEM object to map
  495. * @obj_size: the object size to be mapped, in bytes
  496. * @vma: VMA for the area to be mapped
  497. *
  498. * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
  499. * provided by the driver. Depending on their requirements, drivers can either
  500. * provide a fault handler in their gem_vm_ops (in which case any accesses to
  501. * the object will be trapped, to perform migration, GTT binding, surface
  502. * register allocation, or performance monitoring), or mmap the buffer memory
  503. * synchronously after calling drm_gem_mmap_obj.
  504. *
  505. * This function is mainly intended to implement the DMABUF mmap operation, when
  506. * the GEM object is not looked up based on its fake offset. To implement the
  507. * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  508. *
  509. * NOTE: This function has to be protected with dev->struct_mutex
  510. *
  511. * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  512. * size, or if no gem_vm_ops are provided.
  513. */
  514. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  515. struct vm_area_struct *vma)
  516. {
  517. struct drm_device *dev = obj->dev;
  518. lockdep_assert_held(&dev->struct_mutex);
  519. /* Check for valid size. */
  520. if (obj_size < vma->vm_end - vma->vm_start)
  521. return -EINVAL;
  522. if (!dev->driver->gem_vm_ops)
  523. return -EINVAL;
  524. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  525. vma->vm_ops = dev->driver->gem_vm_ops;
  526. vma->vm_private_data = obj;
  527. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  528. /* Take a ref for this mapping of the object, so that the fault
  529. * handler can dereference the mmap offset's pointer to the object.
  530. * This reference is cleaned up by the corresponding vm_close
  531. * (which should happen whether the vma was created by this call, or
  532. * by a vm_open due to mremap or partial unmap or whatever).
  533. */
  534. drm_gem_object_reference(obj);
  535. drm_vm_open_locked(dev, vma);
  536. return 0;
  537. }
  538. EXPORT_SYMBOL(drm_gem_mmap_obj);
  539. /**
  540. * drm_gem_mmap - memory map routine for GEM objects
  541. * @filp: DRM file pointer
  542. * @vma: VMA for the area to be mapped
  543. *
  544. * If a driver supports GEM object mapping, mmap calls on the DRM file
  545. * descriptor will end up here.
  546. *
  547. * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
  548. * contain the fake offset we created when the GTT map ioctl was called on
  549. * the object) and map it with a call to drm_gem_mmap_obj().
  550. */
  551. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  552. {
  553. struct drm_file *priv = filp->private_data;
  554. struct drm_device *dev = priv->minor->dev;
  555. struct drm_gem_mm *mm = dev->mm_private;
  556. struct drm_gem_object *obj;
  557. struct drm_vma_offset_node *node;
  558. int ret = 0;
  559. if (drm_device_is_unplugged(dev))
  560. return -ENODEV;
  561. mutex_lock(&dev->struct_mutex);
  562. node = drm_vma_offset_exact_lookup(&mm->vma_manager, vma->vm_pgoff,
  563. vma_pages(vma));
  564. if (!node) {
  565. mutex_unlock(&dev->struct_mutex);
  566. return drm_mmap(filp, vma);
  567. }
  568. obj = container_of(node, struct drm_gem_object, vma_node);
  569. ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, vma);
  570. mutex_unlock(&dev->struct_mutex);
  571. return ret;
  572. }
  573. EXPORT_SYMBOL(drm_gem_mmap);