drm_gem.c 26 KB

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