drm_gem.c 19 KB

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