drm_gem.c 19 KB

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