drm_gem.c 29 KB

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