drm_gem.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. * drm_gem_init - Initialize the GEM device fields
  83. * @dev: drm_devic structure to initialize
  84. */
  85. int
  86. drm_gem_init(struct drm_device *dev)
  87. {
  88. struct drm_vma_offset_manager *vma_offset_manager;
  89. mutex_init(&dev->object_name_lock);
  90. idr_init(&dev->object_name_idr);
  91. vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
  92. if (!vma_offset_manager) {
  93. DRM_ERROR("out of memory\n");
  94. return -ENOMEM;
  95. }
  96. dev->vma_offset_manager = vma_offset_manager;
  97. drm_vma_offset_manager_init(vma_offset_manager,
  98. DRM_FILE_PAGE_OFFSET_START,
  99. DRM_FILE_PAGE_OFFSET_SIZE);
  100. return 0;
  101. }
  102. void
  103. drm_gem_destroy(struct drm_device *dev)
  104. {
  105. drm_vma_offset_manager_destroy(dev->vma_offset_manager);
  106. kfree(dev->vma_offset_manager);
  107. dev->vma_offset_manager = NULL;
  108. }
  109. /**
  110. * drm_gem_object_init - initialize an allocated shmem-backed GEM object
  111. * @dev: drm_device the object should be initialized for
  112. * @obj: drm_gem_object to initialize
  113. * @size: object size
  114. *
  115. * Initialize an already allocated GEM object of the specified size with
  116. * shmfs backing store.
  117. */
  118. int drm_gem_object_init(struct drm_device *dev,
  119. struct drm_gem_object *obj, size_t size)
  120. {
  121. struct file *filp;
  122. drm_gem_private_object_init(dev, obj, size);
  123. filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
  124. if (IS_ERR(filp))
  125. return PTR_ERR(filp);
  126. obj->filp = filp;
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(drm_gem_object_init);
  130. /**
  131. * drm_gem_object_init - initialize an allocated private GEM object
  132. * @dev: drm_device the object should be initialized for
  133. * @obj: drm_gem_object to initialize
  134. * @size: object size
  135. *
  136. * Initialize an already allocated GEM object of the specified size with
  137. * no GEM provided backing store. Instead the caller is responsible for
  138. * backing the object and handling it.
  139. */
  140. void drm_gem_private_object_init(struct drm_device *dev,
  141. struct drm_gem_object *obj, size_t size)
  142. {
  143. BUG_ON((size & (PAGE_SIZE - 1)) != 0);
  144. obj->dev = dev;
  145. obj->filp = NULL;
  146. kref_init(&obj->refcount);
  147. obj->handle_count = 0;
  148. obj->size = size;
  149. drm_vma_node_reset(&obj->vma_node);
  150. }
  151. EXPORT_SYMBOL(drm_gem_private_object_init);
  152. static void
  153. drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
  154. {
  155. /*
  156. * Note: obj->dma_buf can't disappear as long as we still hold a
  157. * handle reference in obj->handle_count.
  158. */
  159. mutex_lock(&filp->prime.lock);
  160. if (obj->dma_buf) {
  161. drm_prime_remove_buf_handle_locked(&filp->prime,
  162. obj->dma_buf);
  163. }
  164. mutex_unlock(&filp->prime.lock);
  165. }
  166. /**
  167. * drm_gem_object_free - release resources bound to userspace handles
  168. * @obj: GEM object to clean up.
  169. *
  170. * Called after the last handle to the object has been closed
  171. *
  172. * Removes any name for the object. Note that this must be
  173. * called before drm_gem_object_free or we'll be touching
  174. * freed memory
  175. */
  176. static void drm_gem_object_handle_free(struct drm_gem_object *obj)
  177. {
  178. struct drm_device *dev = obj->dev;
  179. /* Remove any name for this object */
  180. if (obj->name) {
  181. idr_remove(&dev->object_name_idr, obj->name);
  182. obj->name = 0;
  183. }
  184. }
  185. static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
  186. {
  187. /* Unbreak the reference cycle if we have an exported dma_buf. */
  188. if (obj->dma_buf) {
  189. dma_buf_put(obj->dma_buf);
  190. obj->dma_buf = NULL;
  191. }
  192. }
  193. static void
  194. drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
  195. {
  196. if (WARN_ON(obj->handle_count == 0))
  197. return;
  198. /*
  199. * Must bump handle count first as this may be the last
  200. * ref, in which case the object would disappear before we
  201. * checked for a name
  202. */
  203. mutex_lock(&obj->dev->object_name_lock);
  204. if (--obj->handle_count == 0) {
  205. drm_gem_object_handle_free(obj);
  206. drm_gem_object_exported_dma_buf_free(obj);
  207. }
  208. mutex_unlock(&obj->dev->object_name_lock);
  209. drm_gem_object_unreference_unlocked(obj);
  210. }
  211. /**
  212. * drm_gem_handle_delete - deletes the given file-private handle
  213. * @filp: drm file-private structure to use for the handle look up
  214. * @handle: userspace handle to delete
  215. *
  216. * Removes the GEM handle from the @filp lookup table and if this is the last
  217. * handle also cleans up linked resources like GEM names.
  218. */
  219. int
  220. drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  221. {
  222. struct drm_device *dev;
  223. struct drm_gem_object *obj;
  224. /* This is gross. The idr system doesn't let us try a delete and
  225. * return an error code. It just spews if you fail at deleting.
  226. * So, we have to grab a lock around finding the object and then
  227. * doing the delete on it and dropping the refcount, or the user
  228. * could race us to double-decrement the refcount and cause a
  229. * use-after-free later. Given the frequency of our handle lookups,
  230. * we may want to use ida for number allocation and a hash table
  231. * for the pointers, anyway.
  232. */
  233. spin_lock(&filp->table_lock);
  234. /* Check if we currently have a reference on the object */
  235. obj = idr_find(&filp->object_idr, handle);
  236. if (obj == NULL) {
  237. spin_unlock(&filp->table_lock);
  238. return -EINVAL;
  239. }
  240. dev = obj->dev;
  241. /* Release reference and decrement refcount. */
  242. idr_remove(&filp->object_idr, handle);
  243. spin_unlock(&filp->table_lock);
  244. if (drm_core_check_feature(dev, DRIVER_PRIME))
  245. drm_gem_remove_prime_handles(obj, filp);
  246. drm_vma_node_revoke(&obj->vma_node, filp->filp);
  247. if (dev->driver->gem_close_object)
  248. dev->driver->gem_close_object(obj, filp);
  249. drm_gem_object_handle_unreference_unlocked(obj);
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(drm_gem_handle_delete);
  253. /**
  254. * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
  255. * @file: drm file-private structure to remove the dumb handle from
  256. * @dev: corresponding drm_device
  257. * @handle: the dumb handle to remove
  258. *
  259. * This implements the ->dumb_destroy kms driver callback for drivers which use
  260. * gem to manage their backing storage.
  261. */
  262. int drm_gem_dumb_destroy(struct drm_file *file,
  263. struct drm_device *dev,
  264. uint32_t handle)
  265. {
  266. return drm_gem_handle_delete(file, handle);
  267. }
  268. EXPORT_SYMBOL(drm_gem_dumb_destroy);
  269. /**
  270. * drm_gem_handle_create_tail - internal functions to create a handle
  271. * @file_priv: drm file-private structure to register the handle for
  272. * @obj: object to register
  273. * @handlep: pionter to return the created handle to the caller
  274. *
  275. * This expects the dev->object_name_lock to be held already and will drop it
  276. * before returning. Used to avoid races in establishing new handles when
  277. * importing an object from either an flink name or a dma-buf.
  278. */
  279. int
  280. drm_gem_handle_create_tail(struct drm_file *file_priv,
  281. struct drm_gem_object *obj,
  282. u32 *handlep)
  283. {
  284. struct drm_device *dev = obj->dev;
  285. int ret;
  286. WARN_ON(!mutex_is_locked(&dev->object_name_lock));
  287. /*
  288. * Get the user-visible handle using idr. Preload and perform
  289. * allocation under our spinlock.
  290. */
  291. idr_preload(GFP_KERNEL);
  292. spin_lock(&file_priv->table_lock);
  293. ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
  294. drm_gem_object_reference(obj);
  295. obj->handle_count++;
  296. spin_unlock(&file_priv->table_lock);
  297. idr_preload_end();
  298. mutex_unlock(&dev->object_name_lock);
  299. if (ret < 0) {
  300. drm_gem_object_handle_unreference_unlocked(obj);
  301. return ret;
  302. }
  303. *handlep = ret;
  304. ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
  305. if (ret) {
  306. drm_gem_handle_delete(file_priv, *handlep);
  307. return ret;
  308. }
  309. if (dev->driver->gem_open_object) {
  310. ret = dev->driver->gem_open_object(obj, file_priv);
  311. if (ret) {
  312. drm_gem_handle_delete(file_priv, *handlep);
  313. return ret;
  314. }
  315. }
  316. return 0;
  317. }
  318. /**
  319. * gem_handle_create - create a gem handle for an object
  320. * @file_priv: drm file-private structure to register the handle for
  321. * @obj: object to register
  322. * @handlep: pionter to return the created handle to the caller
  323. *
  324. * Create a handle for this object. This adds a handle reference
  325. * to the object, which includes a regular reference count. Callers
  326. * will likely want to dereference the object afterwards.
  327. */
  328. int
  329. drm_gem_handle_create(struct drm_file *file_priv,
  330. struct drm_gem_object *obj,
  331. u32 *handlep)
  332. {
  333. mutex_lock(&obj->dev->object_name_lock);
  334. return drm_gem_handle_create_tail(file_priv, obj, handlep);
  335. }
  336. EXPORT_SYMBOL(drm_gem_handle_create);
  337. /**
  338. * drm_gem_free_mmap_offset - release a fake mmap offset for an object
  339. * @obj: obj in question
  340. *
  341. * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
  342. */
  343. void
  344. drm_gem_free_mmap_offset(struct drm_gem_object *obj)
  345. {
  346. struct drm_device *dev = obj->dev;
  347. drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
  348. }
  349. EXPORT_SYMBOL(drm_gem_free_mmap_offset);
  350. /**
  351. * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
  352. * @obj: obj in question
  353. * @size: the virtual size
  354. *
  355. * GEM memory mapping works by handing back to userspace a fake mmap offset
  356. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  357. * up the object based on the offset and sets up the various memory mapping
  358. * structures.
  359. *
  360. * This routine allocates and attaches a fake offset for @obj, in cases where
  361. * the virtual size differs from the physical size (ie. obj->size). Otherwise
  362. * just use drm_gem_create_mmap_offset().
  363. */
  364. int
  365. drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
  366. {
  367. struct drm_device *dev = obj->dev;
  368. return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
  369. size / PAGE_SIZE);
  370. }
  371. EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
  372. /**
  373. * drm_gem_create_mmap_offset - create a fake mmap offset for an object
  374. * @obj: obj in question
  375. *
  376. * GEM memory mapping works by handing back to userspace a fake mmap offset
  377. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  378. * up the object based on the offset and sets up the various memory mapping
  379. * structures.
  380. *
  381. * This routine allocates and attaches a fake offset for @obj.
  382. */
  383. int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
  384. {
  385. return drm_gem_create_mmap_offset_size(obj, obj->size);
  386. }
  387. EXPORT_SYMBOL(drm_gem_create_mmap_offset);
  388. /**
  389. * drm_gem_get_pages - helper to allocate backing pages for a GEM object
  390. * from shmem
  391. * @obj: obj in question
  392. *
  393. * This reads the page-array of the shmem-backing storage of the given gem
  394. * object. An array of pages is returned. If a page is not allocated or
  395. * swapped-out, this will allocate/swap-in the required pages. Note that the
  396. * whole object is covered by the page-array and pinned in memory.
  397. *
  398. * Use drm_gem_put_pages() to release the array and unpin all pages.
  399. *
  400. * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
  401. * If you require other GFP-masks, you have to do those allocations yourself.
  402. *
  403. * Note that you are not allowed to change gfp-zones during runtime. That is,
  404. * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
  405. * set during initialization. If you have special zone constraints, set them
  406. * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care
  407. * to keep pages in the required zone during swap-in.
  408. */
  409. struct page **drm_gem_get_pages(struct drm_gem_object *obj)
  410. {
  411. struct address_space *mapping;
  412. struct page *p, **pages;
  413. int i, npages;
  414. /* This is the shared memory object that backs the GEM resource */
  415. mapping = file_inode(obj->filp)->i_mapping;
  416. /* We already BUG_ON() for non-page-aligned sizes in
  417. * drm_gem_object_init(), so we should never hit this unless
  418. * driver author is doing something really wrong:
  419. */
  420. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  421. npages = obj->size >> PAGE_SHIFT;
  422. pages = drm_malloc_ab(npages, sizeof(struct page *));
  423. if (pages == NULL)
  424. return ERR_PTR(-ENOMEM);
  425. for (i = 0; i < npages; i++) {
  426. p = shmem_read_mapping_page(mapping, i);
  427. if (IS_ERR(p))
  428. goto fail;
  429. pages[i] = p;
  430. /* Make sure shmem keeps __GFP_DMA32 allocated pages in the
  431. * correct region during swapin. Note that this requires
  432. * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
  433. * so shmem can relocate pages during swapin if required.
  434. */
  435. BUG_ON((mapping_gfp_mask(mapping) & __GFP_DMA32) &&
  436. (page_to_pfn(p) >= 0x00100000UL));
  437. }
  438. return pages;
  439. fail:
  440. while (i--)
  441. page_cache_release(pages[i]);
  442. drm_free_large(pages);
  443. return ERR_CAST(p);
  444. }
  445. EXPORT_SYMBOL(drm_gem_get_pages);
  446. /**
  447. * drm_gem_put_pages - helper to free backing pages for a GEM object
  448. * @obj: obj in question
  449. * @pages: pages to free
  450. * @dirty: if true, pages will be marked as dirty
  451. * @accessed: if true, the pages will be marked as accessed
  452. */
  453. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  454. bool dirty, bool accessed)
  455. {
  456. int i, npages;
  457. /* We already BUG_ON() for non-page-aligned sizes in
  458. * drm_gem_object_init(), so we should never hit this unless
  459. * driver author is doing something really wrong:
  460. */
  461. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  462. npages = obj->size >> PAGE_SHIFT;
  463. for (i = 0; i < npages; i++) {
  464. if (dirty)
  465. set_page_dirty(pages[i]);
  466. if (accessed)
  467. mark_page_accessed(pages[i]);
  468. /* Undo the reference we took when populating the table */
  469. page_cache_release(pages[i]);
  470. }
  471. drm_free_large(pages);
  472. }
  473. EXPORT_SYMBOL(drm_gem_put_pages);
  474. /** Returns a reference to the object named by the handle. */
  475. struct drm_gem_object *
  476. drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
  477. u32 handle)
  478. {
  479. struct drm_gem_object *obj;
  480. spin_lock(&filp->table_lock);
  481. /* Check if we currently have a reference on the object */
  482. obj = idr_find(&filp->object_idr, handle);
  483. if (obj == NULL) {
  484. spin_unlock(&filp->table_lock);
  485. return NULL;
  486. }
  487. drm_gem_object_reference(obj);
  488. spin_unlock(&filp->table_lock);
  489. return obj;
  490. }
  491. EXPORT_SYMBOL(drm_gem_object_lookup);
  492. /**
  493. * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
  494. * @dev: drm_device
  495. * @data: ioctl data
  496. * @file_priv: drm file-private structure
  497. *
  498. * Releases the handle to an mm object.
  499. */
  500. int
  501. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  502. struct drm_file *file_priv)
  503. {
  504. struct drm_gem_close *args = data;
  505. int ret;
  506. if (!(dev->driver->driver_features & DRIVER_GEM))
  507. return -ENODEV;
  508. ret = drm_gem_handle_delete(file_priv, args->handle);
  509. return ret;
  510. }
  511. /**
  512. * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
  513. * @dev: drm_device
  514. * @data: ioctl data
  515. * @file_priv: drm file-private structure
  516. *
  517. * Create a global name for an object, returning the name.
  518. *
  519. * Note that the name does not hold a reference; when the object
  520. * is freed, the name goes away.
  521. */
  522. int
  523. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  524. struct drm_file *file_priv)
  525. {
  526. struct drm_gem_flink *args = data;
  527. struct drm_gem_object *obj;
  528. int ret;
  529. if (!(dev->driver->driver_features & DRIVER_GEM))
  530. return -ENODEV;
  531. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  532. if (obj == NULL)
  533. return -ENOENT;
  534. mutex_lock(&dev->object_name_lock);
  535. idr_preload(GFP_KERNEL);
  536. /* prevent races with concurrent gem_close. */
  537. if (obj->handle_count == 0) {
  538. ret = -ENOENT;
  539. goto err;
  540. }
  541. if (!obj->name) {
  542. ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT);
  543. if (ret < 0)
  544. goto err;
  545. obj->name = ret;
  546. }
  547. args->name = (uint64_t) obj->name;
  548. ret = 0;
  549. err:
  550. idr_preload_end();
  551. mutex_unlock(&dev->object_name_lock);
  552. drm_gem_object_unreference_unlocked(obj);
  553. return ret;
  554. }
  555. /**
  556. * drm_gem_open - implementation of the GEM_OPEN ioctl
  557. * @dev: drm_device
  558. * @data: ioctl data
  559. * @file_priv: drm file-private structure
  560. *
  561. * Open an object using the global name, returning a handle and the size.
  562. *
  563. * This handle (of course) holds a reference to the object, so the object
  564. * will not go away until the handle is deleted.
  565. */
  566. int
  567. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  568. struct drm_file *file_priv)
  569. {
  570. struct drm_gem_open *args = data;
  571. struct drm_gem_object *obj;
  572. int ret;
  573. u32 handle;
  574. if (!(dev->driver->driver_features & DRIVER_GEM))
  575. return -ENODEV;
  576. mutex_lock(&dev->object_name_lock);
  577. obj = idr_find(&dev->object_name_idr, (int) args->name);
  578. if (obj) {
  579. drm_gem_object_reference(obj);
  580. } else {
  581. mutex_unlock(&dev->object_name_lock);
  582. return -ENOENT;
  583. }
  584. /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
  585. ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
  586. drm_gem_object_unreference_unlocked(obj);
  587. if (ret)
  588. return ret;
  589. args->handle = handle;
  590. args->size = obj->size;
  591. return 0;
  592. }
  593. /**
  594. * gem_gem_open - initalizes GEM file-private structures at devnode open time
  595. * @dev: drm_device which is being opened by userspace
  596. * @file_private: drm file-private structure to set up
  597. *
  598. * Called at device open time, sets up the structure for handling refcounting
  599. * of mm objects.
  600. */
  601. void
  602. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  603. {
  604. idr_init(&file_private->object_idr);
  605. spin_lock_init(&file_private->table_lock);
  606. }
  607. /*
  608. * Called at device close to release the file's
  609. * handle references on objects.
  610. */
  611. static int
  612. drm_gem_object_release_handle(int id, void *ptr, void *data)
  613. {
  614. struct drm_file *file_priv = data;
  615. struct drm_gem_object *obj = ptr;
  616. struct drm_device *dev = obj->dev;
  617. if (drm_core_check_feature(dev, DRIVER_PRIME))
  618. drm_gem_remove_prime_handles(obj, file_priv);
  619. drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
  620. if (dev->driver->gem_close_object)
  621. dev->driver->gem_close_object(obj, file_priv);
  622. drm_gem_object_handle_unreference_unlocked(obj);
  623. return 0;
  624. }
  625. /**
  626. * drm_gem_release - release file-private GEM resources
  627. * @dev: drm_device which is being closed by userspace
  628. * @file_private: drm file-private structure to clean up
  629. *
  630. * Called at close time when the filp is going away.
  631. *
  632. * Releases any remaining references on objects by this filp.
  633. */
  634. void
  635. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  636. {
  637. idr_for_each(&file_private->object_idr,
  638. &drm_gem_object_release_handle, file_private);
  639. idr_destroy(&file_private->object_idr);
  640. }
  641. void
  642. drm_gem_object_release(struct drm_gem_object *obj)
  643. {
  644. WARN_ON(obj->dma_buf);
  645. if (obj->filp)
  646. fput(obj->filp);
  647. drm_gem_free_mmap_offset(obj);
  648. }
  649. EXPORT_SYMBOL(drm_gem_object_release);
  650. /**
  651. * drm_gem_object_free - free a GEM object
  652. * @kref: kref of the object to free
  653. *
  654. * Called after the last reference to the object has been lost.
  655. * Must be called holding struct_ mutex
  656. *
  657. * Frees the object
  658. */
  659. void
  660. drm_gem_object_free(struct kref *kref)
  661. {
  662. struct drm_gem_object *obj = (struct drm_gem_object *) kref;
  663. struct drm_device *dev = obj->dev;
  664. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  665. if (dev->driver->gem_free_object != NULL)
  666. dev->driver->gem_free_object(obj);
  667. }
  668. EXPORT_SYMBOL(drm_gem_object_free);
  669. void drm_gem_vm_open(struct vm_area_struct *vma)
  670. {
  671. struct drm_gem_object *obj = vma->vm_private_data;
  672. drm_gem_object_reference(obj);
  673. mutex_lock(&obj->dev->struct_mutex);
  674. drm_vm_open_locked(obj->dev, vma);
  675. mutex_unlock(&obj->dev->struct_mutex);
  676. }
  677. EXPORT_SYMBOL(drm_gem_vm_open);
  678. void drm_gem_vm_close(struct vm_area_struct *vma)
  679. {
  680. struct drm_gem_object *obj = vma->vm_private_data;
  681. struct drm_device *dev = obj->dev;
  682. mutex_lock(&dev->struct_mutex);
  683. drm_vm_close_locked(obj->dev, vma);
  684. drm_gem_object_unreference(obj);
  685. mutex_unlock(&dev->struct_mutex);
  686. }
  687. EXPORT_SYMBOL(drm_gem_vm_close);
  688. /**
  689. * drm_gem_mmap_obj - memory map a GEM object
  690. * @obj: the GEM object to map
  691. * @obj_size: the object size to be mapped, in bytes
  692. * @vma: VMA for the area to be mapped
  693. *
  694. * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
  695. * provided by the driver. Depending on their requirements, drivers can either
  696. * provide a fault handler in their gem_vm_ops (in which case any accesses to
  697. * the object will be trapped, to perform migration, GTT binding, surface
  698. * register allocation, or performance monitoring), or mmap the buffer memory
  699. * synchronously after calling drm_gem_mmap_obj.
  700. *
  701. * This function is mainly intended to implement the DMABUF mmap operation, when
  702. * the GEM object is not looked up based on its fake offset. To implement the
  703. * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  704. *
  705. * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
  706. * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
  707. * callers must verify access restrictions before calling this helper.
  708. *
  709. * NOTE: This function has to be protected with dev->struct_mutex
  710. *
  711. * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  712. * size, or if no gem_vm_ops are provided.
  713. */
  714. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  715. struct vm_area_struct *vma)
  716. {
  717. struct drm_device *dev = obj->dev;
  718. lockdep_assert_held(&dev->struct_mutex);
  719. /* Check for valid size. */
  720. if (obj_size < vma->vm_end - vma->vm_start)
  721. return -EINVAL;
  722. if (!dev->driver->gem_vm_ops)
  723. return -EINVAL;
  724. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  725. vma->vm_ops = dev->driver->gem_vm_ops;
  726. vma->vm_private_data = obj;
  727. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  728. /* Take a ref for this mapping of the object, so that the fault
  729. * handler can dereference the mmap offset's pointer to the object.
  730. * This reference is cleaned up by the corresponding vm_close
  731. * (which should happen whether the vma was created by this call, or
  732. * by a vm_open due to mremap or partial unmap or whatever).
  733. */
  734. drm_gem_object_reference(obj);
  735. drm_vm_open_locked(dev, vma);
  736. return 0;
  737. }
  738. EXPORT_SYMBOL(drm_gem_mmap_obj);
  739. /**
  740. * drm_gem_mmap - memory map routine for GEM objects
  741. * @filp: DRM file pointer
  742. * @vma: VMA for the area to be mapped
  743. *
  744. * If a driver supports GEM object mapping, mmap calls on the DRM file
  745. * descriptor will end up here.
  746. *
  747. * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
  748. * contain the fake offset we created when the GTT map ioctl was called on
  749. * the object) and map it with a call to drm_gem_mmap_obj().
  750. *
  751. * If the caller is not granted access to the buffer object, the mmap will fail
  752. * with EACCES. Please see the vma manager for more information.
  753. */
  754. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  755. {
  756. struct drm_file *priv = filp->private_data;
  757. struct drm_device *dev = priv->minor->dev;
  758. struct drm_gem_object *obj;
  759. struct drm_vma_offset_node *node;
  760. int ret;
  761. if (drm_device_is_unplugged(dev))
  762. return -ENODEV;
  763. mutex_lock(&dev->struct_mutex);
  764. node = drm_vma_offset_exact_lookup(dev->vma_offset_manager,
  765. vma->vm_pgoff,
  766. vma_pages(vma));
  767. if (!node) {
  768. mutex_unlock(&dev->struct_mutex);
  769. return drm_mmap(filp, vma);
  770. } else if (!drm_vma_node_is_allowed(node, filp)) {
  771. mutex_unlock(&dev->struct_mutex);
  772. return -EACCES;
  773. }
  774. obj = container_of(node, struct drm_gem_object, vma_node);
  775. ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, vma);
  776. mutex_unlock(&dev->struct_mutex);
  777. return ret;
  778. }
  779. EXPORT_SYMBOL(drm_gem_mmap);