drm_gem.c 18 KB

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