drm_gem.c 26 KB

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