drm_gem.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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_handle_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. struct drm_device *dev = obj->dev;
  199. bool final = false;
  200. if (WARN_ON(obj->handle_count == 0))
  201. return;
  202. /*
  203. * Must bump handle count first as this may be the last
  204. * ref, in which case the object would disappear before we
  205. * checked for a name
  206. */
  207. mutex_lock(&dev->object_name_lock);
  208. if (--obj->handle_count == 0) {
  209. drm_gem_object_handle_free(obj);
  210. drm_gem_object_exported_dma_buf_free(obj);
  211. final = true;
  212. }
  213. mutex_unlock(&dev->object_name_lock);
  214. if (final)
  215. drm_gem_object_unreference_unlocked(obj);
  216. }
  217. /*
  218. * Called at device or object close to release the file's
  219. * handle references on objects.
  220. */
  221. static int
  222. drm_gem_object_release_handle(int id, void *ptr, void *data)
  223. {
  224. struct drm_file *file_priv = data;
  225. struct drm_gem_object *obj = ptr;
  226. struct drm_device *dev = obj->dev;
  227. if (drm_core_check_feature(dev, DRIVER_PRIME))
  228. drm_gem_remove_prime_handles(obj, file_priv);
  229. drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
  230. if (dev->driver->gem_close_object)
  231. dev->driver->gem_close_object(obj, file_priv);
  232. drm_gem_object_handle_unreference_unlocked(obj);
  233. return 0;
  234. }
  235. /**
  236. * drm_gem_handle_delete - deletes the given file-private handle
  237. * @filp: drm file-private structure to use for the handle look up
  238. * @handle: userspace handle to delete
  239. *
  240. * Removes the GEM handle from the @filp lookup table which has been added with
  241. * drm_gem_handle_create(). If this is the last handle also cleans up linked
  242. * resources like GEM names.
  243. */
  244. int
  245. drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  246. {
  247. struct drm_gem_object *obj;
  248. /* This is gross. The idr system doesn't let us try a delete and
  249. * return an error code. It just spews if you fail at deleting.
  250. * So, we have to grab a lock around finding the object and then
  251. * doing the delete on it and dropping the refcount, or the user
  252. * could race us to double-decrement the refcount and cause a
  253. * use-after-free later. Given the frequency of our handle lookups,
  254. * we may want to use ida for number allocation and a hash table
  255. * for the pointers, anyway.
  256. */
  257. spin_lock(&filp->table_lock);
  258. /* Check if we currently have a reference on the object */
  259. obj = idr_replace(&filp->object_idr, NULL, handle);
  260. spin_unlock(&filp->table_lock);
  261. if (IS_ERR_OR_NULL(obj))
  262. return -EINVAL;
  263. /* Release driver's reference and decrement refcount. */
  264. drm_gem_object_release_handle(handle, obj, filp);
  265. /* And finally make the handle available for future allocations. */
  266. spin_lock(&filp->table_lock);
  267. idr_remove(&filp->object_idr, handle);
  268. spin_unlock(&filp->table_lock);
  269. return 0;
  270. }
  271. EXPORT_SYMBOL(drm_gem_handle_delete);
  272. /**
  273. * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
  274. * @file: drm file-private structure to remove the dumb handle from
  275. * @dev: corresponding drm_device
  276. * @handle: the dumb handle to remove
  277. *
  278. * This implements the ->dumb_destroy kms driver callback for drivers which use
  279. * gem to manage their backing storage.
  280. */
  281. int drm_gem_dumb_destroy(struct drm_file *file,
  282. struct drm_device *dev,
  283. uint32_t handle)
  284. {
  285. return drm_gem_handle_delete(file, handle);
  286. }
  287. EXPORT_SYMBOL(drm_gem_dumb_destroy);
  288. /**
  289. * drm_gem_handle_create_tail - internal functions to create a handle
  290. * @file_priv: drm file-private structure to register the handle for
  291. * @obj: object to register
  292. * @handlep: pointer to return the created handle to the caller
  293. *
  294. * This expects the dev->object_name_lock to be held already and will drop it
  295. * before returning. Used to avoid races in establishing new handles when
  296. * importing an object from either an flink name or a dma-buf.
  297. *
  298. * Handles must be release again through drm_gem_handle_delete(). This is done
  299. * when userspace closes @file_priv for all attached handles, or through the
  300. * GEM_CLOSE ioctl for individual handles.
  301. */
  302. int
  303. drm_gem_handle_create_tail(struct drm_file *file_priv,
  304. struct drm_gem_object *obj,
  305. u32 *handlep)
  306. {
  307. struct drm_device *dev = obj->dev;
  308. u32 handle;
  309. int ret;
  310. WARN_ON(!mutex_is_locked(&dev->object_name_lock));
  311. if (obj->handle_count++ == 0)
  312. drm_gem_object_reference(obj);
  313. /*
  314. * Get the user-visible handle using idr. Preload and perform
  315. * allocation under our spinlock.
  316. */
  317. idr_preload(GFP_KERNEL);
  318. spin_lock(&file_priv->table_lock);
  319. ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
  320. spin_unlock(&file_priv->table_lock);
  321. idr_preload_end();
  322. mutex_unlock(&dev->object_name_lock);
  323. if (ret < 0)
  324. goto err_unref;
  325. handle = ret;
  326. ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
  327. if (ret)
  328. goto err_remove;
  329. if (dev->driver->gem_open_object) {
  330. ret = dev->driver->gem_open_object(obj, file_priv);
  331. if (ret)
  332. goto err_revoke;
  333. }
  334. *handlep = handle;
  335. return 0;
  336. err_revoke:
  337. drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
  338. err_remove:
  339. spin_lock(&file_priv->table_lock);
  340. idr_remove(&file_priv->object_idr, handle);
  341. spin_unlock(&file_priv->table_lock);
  342. err_unref:
  343. drm_gem_object_handle_unreference_unlocked(obj);
  344. return ret;
  345. }
  346. /**
  347. * drm_gem_handle_create - create a gem handle for an object
  348. * @file_priv: drm file-private structure to register the handle for
  349. * @obj: object to register
  350. * @handlep: pionter to return the created handle to the caller
  351. *
  352. * Create a handle for this object. This adds a handle reference
  353. * to the object, which includes a regular reference count. Callers
  354. * will likely want to dereference the object afterwards.
  355. */
  356. int drm_gem_handle_create(struct drm_file *file_priv,
  357. struct drm_gem_object *obj,
  358. u32 *handlep)
  359. {
  360. mutex_lock(&obj->dev->object_name_lock);
  361. return drm_gem_handle_create_tail(file_priv, obj, handlep);
  362. }
  363. EXPORT_SYMBOL(drm_gem_handle_create);
  364. /**
  365. * drm_gem_free_mmap_offset - release a fake mmap offset for an object
  366. * @obj: obj in question
  367. *
  368. * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
  369. *
  370. * Note that drm_gem_object_release() already calls this function, so drivers
  371. * don't have to take care of releasing the mmap offset themselves when freeing
  372. * the GEM object.
  373. */
  374. void
  375. drm_gem_free_mmap_offset(struct drm_gem_object *obj)
  376. {
  377. struct drm_device *dev = obj->dev;
  378. drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
  379. }
  380. EXPORT_SYMBOL(drm_gem_free_mmap_offset);
  381. /**
  382. * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
  383. * @obj: obj in question
  384. * @size: the virtual size
  385. *
  386. * GEM memory mapping works by handing back to userspace a fake mmap offset
  387. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  388. * up the object based on the offset and sets up the various memory mapping
  389. * structures.
  390. *
  391. * This routine allocates and attaches a fake offset for @obj, in cases where
  392. * the virtual size differs from the physical size (ie. obj->size). Otherwise
  393. * just use drm_gem_create_mmap_offset().
  394. *
  395. * This function is idempotent and handles an already allocated mmap offset
  396. * transparently. Drivers do not need to check for this case.
  397. */
  398. int
  399. drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
  400. {
  401. struct drm_device *dev = obj->dev;
  402. return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
  403. size / PAGE_SIZE);
  404. }
  405. EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
  406. /**
  407. * drm_gem_create_mmap_offset - create a fake mmap offset for an object
  408. * @obj: obj in question
  409. *
  410. * GEM memory mapping works by handing back to userspace a fake mmap offset
  411. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  412. * up the object based on the offset and sets up the various memory mapping
  413. * structures.
  414. *
  415. * This routine allocates and attaches a fake offset for @obj.
  416. *
  417. * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
  418. * the fake offset again.
  419. */
  420. int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
  421. {
  422. return drm_gem_create_mmap_offset_size(obj, obj->size);
  423. }
  424. EXPORT_SYMBOL(drm_gem_create_mmap_offset);
  425. /**
  426. * drm_gem_get_pages - helper to allocate backing pages for a GEM object
  427. * from shmem
  428. * @obj: obj in question
  429. *
  430. * This reads the page-array of the shmem-backing storage of the given gem
  431. * object. An array of pages is returned. If a page is not allocated or
  432. * swapped-out, this will allocate/swap-in the required pages. Note that the
  433. * whole object is covered by the page-array and pinned in memory.
  434. *
  435. * Use drm_gem_put_pages() to release the array and unpin all pages.
  436. *
  437. * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
  438. * If you require other GFP-masks, you have to do those allocations yourself.
  439. *
  440. * Note that you are not allowed to change gfp-zones during runtime. That is,
  441. * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
  442. * set during initialization. If you have special zone constraints, set them
  443. * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care
  444. * to keep pages in the required zone during swap-in.
  445. */
  446. struct page **drm_gem_get_pages(struct drm_gem_object *obj)
  447. {
  448. struct address_space *mapping;
  449. struct page *p, **pages;
  450. int i, npages;
  451. /* This is the shared memory object that backs the GEM resource */
  452. mapping = file_inode(obj->filp)->i_mapping;
  453. /* We already BUG_ON() for non-page-aligned sizes in
  454. * drm_gem_object_init(), so we should never hit this unless
  455. * driver author is doing something really wrong:
  456. */
  457. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  458. npages = obj->size >> PAGE_SHIFT;
  459. pages = drm_malloc_ab(npages, sizeof(struct page *));
  460. if (pages == NULL)
  461. return ERR_PTR(-ENOMEM);
  462. for (i = 0; i < npages; i++) {
  463. p = shmem_read_mapping_page(mapping, i);
  464. if (IS_ERR(p))
  465. goto fail;
  466. pages[i] = p;
  467. /* Make sure shmem keeps __GFP_DMA32 allocated pages in the
  468. * correct region during swapin. Note that this requires
  469. * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
  470. * so shmem can relocate pages during swapin if required.
  471. */
  472. BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
  473. (page_to_pfn(p) >= 0x00100000UL));
  474. }
  475. return pages;
  476. fail:
  477. while (i--)
  478. put_page(pages[i]);
  479. drm_free_large(pages);
  480. return ERR_CAST(p);
  481. }
  482. EXPORT_SYMBOL(drm_gem_get_pages);
  483. /**
  484. * drm_gem_put_pages - helper to free backing pages for a GEM object
  485. * @obj: obj in question
  486. * @pages: pages to free
  487. * @dirty: if true, pages will be marked as dirty
  488. * @accessed: if true, the pages will be marked as accessed
  489. */
  490. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  491. bool dirty, bool accessed)
  492. {
  493. int i, npages;
  494. /* We already BUG_ON() for non-page-aligned sizes in
  495. * drm_gem_object_init(), so we should never hit this unless
  496. * driver author is doing something really wrong:
  497. */
  498. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  499. npages = obj->size >> PAGE_SHIFT;
  500. for (i = 0; i < npages; i++) {
  501. if (dirty)
  502. set_page_dirty(pages[i]);
  503. if (accessed)
  504. mark_page_accessed(pages[i]);
  505. /* Undo the reference we took when populating the table */
  506. put_page(pages[i]);
  507. }
  508. drm_free_large(pages);
  509. }
  510. EXPORT_SYMBOL(drm_gem_put_pages);
  511. /**
  512. * drm_gem_object_lookup - look up a GEM object from it's handle
  513. * @dev: DRM device
  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_device *dev, struct drm_file *filp,
  524. u32 handle)
  525. {
  526. struct drm_gem_object *obj;
  527. spin_lock(&filp->table_lock);
  528. /* Check if we currently have a reference on the object */
  529. obj = idr_find(&filp->object_idr, handle);
  530. if (obj == NULL) {
  531. spin_unlock(&filp->table_lock);
  532. return NULL;
  533. }
  534. drm_gem_object_reference(obj);
  535. spin_unlock(&filp->table_lock);
  536. return obj;
  537. }
  538. EXPORT_SYMBOL(drm_gem_object_lookup);
  539. /**
  540. * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
  541. * @dev: drm_device
  542. * @data: ioctl data
  543. * @file_priv: drm file-private structure
  544. *
  545. * Releases the handle to an mm object.
  546. */
  547. int
  548. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  549. struct drm_file *file_priv)
  550. {
  551. struct drm_gem_close *args = data;
  552. int ret;
  553. if (!drm_core_check_feature(dev, DRIVER_GEM))
  554. return -ENODEV;
  555. ret = drm_gem_handle_delete(file_priv, args->handle);
  556. return ret;
  557. }
  558. /**
  559. * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
  560. * @dev: drm_device
  561. * @data: ioctl data
  562. * @file_priv: drm file-private structure
  563. *
  564. * Create a global name for an object, returning the name.
  565. *
  566. * Note that the name does not hold a reference; when the object
  567. * is freed, the name goes away.
  568. */
  569. int
  570. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  571. struct drm_file *file_priv)
  572. {
  573. struct drm_gem_flink *args = data;
  574. struct drm_gem_object *obj;
  575. int ret;
  576. if (!drm_core_check_feature(dev, DRIVER_GEM))
  577. return -ENODEV;
  578. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  579. if (obj == NULL)
  580. return -ENOENT;
  581. mutex_lock(&dev->object_name_lock);
  582. /* prevent races with concurrent gem_close. */
  583. if (obj->handle_count == 0) {
  584. ret = -ENOENT;
  585. goto err;
  586. }
  587. if (!obj->name) {
  588. ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
  589. if (ret < 0)
  590. goto err;
  591. obj->name = ret;
  592. }
  593. args->name = (uint64_t) obj->name;
  594. ret = 0;
  595. err:
  596. mutex_unlock(&dev->object_name_lock);
  597. drm_gem_object_unreference_unlocked(obj);
  598. return ret;
  599. }
  600. /**
  601. * drm_gem_open - implementation of the GEM_OPEN ioctl
  602. * @dev: drm_device
  603. * @data: ioctl data
  604. * @file_priv: drm file-private structure
  605. *
  606. * Open an object using the global name, returning a handle and the size.
  607. *
  608. * This handle (of course) holds a reference to the object, so the object
  609. * will not go away until the handle is deleted.
  610. */
  611. int
  612. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  613. struct drm_file *file_priv)
  614. {
  615. struct drm_gem_open *args = data;
  616. struct drm_gem_object *obj;
  617. int ret;
  618. u32 handle;
  619. if (!drm_core_check_feature(dev, DRIVER_GEM))
  620. return -ENODEV;
  621. mutex_lock(&dev->object_name_lock);
  622. obj = idr_find(&dev->object_name_idr, (int) args->name);
  623. if (obj) {
  624. drm_gem_object_reference(obj);
  625. } else {
  626. mutex_unlock(&dev->object_name_lock);
  627. return -ENOENT;
  628. }
  629. /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
  630. ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
  631. drm_gem_object_unreference_unlocked(obj);
  632. if (ret)
  633. return ret;
  634. args->handle = handle;
  635. args->size = obj->size;
  636. return 0;
  637. }
  638. /**
  639. * gem_gem_open - initalizes GEM file-private structures at devnode open time
  640. * @dev: drm_device which is being opened by userspace
  641. * @file_private: drm file-private structure to set up
  642. *
  643. * Called at device open time, sets up the structure for handling refcounting
  644. * of mm objects.
  645. */
  646. void
  647. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  648. {
  649. idr_init(&file_private->object_idr);
  650. spin_lock_init(&file_private->table_lock);
  651. }
  652. /**
  653. * drm_gem_release - release file-private GEM resources
  654. * @dev: drm_device which is being closed by userspace
  655. * @file_private: drm file-private structure to clean up
  656. *
  657. * Called at close time when the filp is going away.
  658. *
  659. * Releases any remaining references on objects by this filp.
  660. */
  661. void
  662. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  663. {
  664. idr_for_each(&file_private->object_idr,
  665. &drm_gem_object_release_handle, file_private);
  666. idr_destroy(&file_private->object_idr);
  667. }
  668. /**
  669. * drm_gem_object_release - release GEM buffer object resources
  670. * @obj: GEM buffer object
  671. *
  672. * This releases any structures and resources used by @obj and is the invers of
  673. * drm_gem_object_init().
  674. */
  675. void
  676. drm_gem_object_release(struct drm_gem_object *obj)
  677. {
  678. WARN_ON(obj->dma_buf);
  679. if (obj->filp)
  680. fput(obj->filp);
  681. drm_gem_free_mmap_offset(obj);
  682. }
  683. EXPORT_SYMBOL(drm_gem_object_release);
  684. /**
  685. * drm_gem_object_free - free a GEM object
  686. * @kref: kref of the object to free
  687. *
  688. * Called after the last reference to the object has been lost.
  689. * Must be called holding struct_ mutex
  690. *
  691. * Frees the object
  692. */
  693. void
  694. drm_gem_object_free(struct kref *kref)
  695. {
  696. struct drm_gem_object *obj =
  697. container_of(kref, struct drm_gem_object, refcount);
  698. struct drm_device *dev = obj->dev;
  699. if (dev->driver->gem_free_object_unlocked) {
  700. dev->driver->gem_free_object_unlocked(obj);
  701. } else if (dev->driver->gem_free_object) {
  702. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  703. dev->driver->gem_free_object(obj);
  704. }
  705. }
  706. EXPORT_SYMBOL(drm_gem_object_free);
  707. /**
  708. * drm_gem_object_unreference_unlocked - release a GEM BO reference
  709. * @obj: GEM buffer object
  710. *
  711. * This releases a reference to @obj. Callers must not hold the
  712. * dev->struct_mutex lock when calling this function.
  713. *
  714. * See also __drm_gem_object_unreference().
  715. */
  716. void
  717. drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
  718. {
  719. struct drm_device *dev;
  720. if (!obj)
  721. return;
  722. dev = obj->dev;
  723. might_lock(&dev->struct_mutex);
  724. if (dev->driver->gem_free_object_unlocked)
  725. kref_put(&obj->refcount, drm_gem_object_free);
  726. else if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
  727. &dev->struct_mutex))
  728. mutex_unlock(&dev->struct_mutex);
  729. }
  730. EXPORT_SYMBOL(drm_gem_object_unreference_unlocked);
  731. /**
  732. * drm_gem_object_unreference - release a GEM BO reference
  733. * @obj: GEM buffer object
  734. *
  735. * This releases a reference to @obj. Callers must hold the dev->struct_mutex
  736. * lock when calling this function, even when the driver doesn't use
  737. * dev->struct_mutex for anything.
  738. *
  739. * For drivers not encumbered with legacy locking use
  740. * drm_gem_object_unreference_unlocked() instead.
  741. */
  742. void
  743. drm_gem_object_unreference(struct drm_gem_object *obj)
  744. {
  745. if (obj) {
  746. WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
  747. kref_put(&obj->refcount, drm_gem_object_free);
  748. }
  749. }
  750. EXPORT_SYMBOL(drm_gem_object_unreference);
  751. /**
  752. * drm_gem_vm_open - vma->ops->open implementation for GEM
  753. * @vma: VM area structure
  754. *
  755. * This function implements the #vm_operations_struct open() callback for GEM
  756. * drivers. This must be used together with drm_gem_vm_close().
  757. */
  758. void drm_gem_vm_open(struct vm_area_struct *vma)
  759. {
  760. struct drm_gem_object *obj = vma->vm_private_data;
  761. drm_gem_object_reference(obj);
  762. }
  763. EXPORT_SYMBOL(drm_gem_vm_open);
  764. /**
  765. * drm_gem_vm_close - vma->ops->close implementation for GEM
  766. * @vma: VM area structure
  767. *
  768. * This function implements the #vm_operations_struct close() callback for GEM
  769. * drivers. This must be used together with drm_gem_vm_open().
  770. */
  771. void drm_gem_vm_close(struct vm_area_struct *vma)
  772. {
  773. struct drm_gem_object *obj = vma->vm_private_data;
  774. drm_gem_object_unreference_unlocked(obj);
  775. }
  776. EXPORT_SYMBOL(drm_gem_vm_close);
  777. /**
  778. * drm_gem_mmap_obj - memory map a GEM object
  779. * @obj: the GEM object to map
  780. * @obj_size: the object size to be mapped, in bytes
  781. * @vma: VMA for the area to be mapped
  782. *
  783. * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
  784. * provided by the driver. Depending on their requirements, drivers can either
  785. * provide a fault handler in their gem_vm_ops (in which case any accesses to
  786. * the object will be trapped, to perform migration, GTT binding, surface
  787. * register allocation, or performance monitoring), or mmap the buffer memory
  788. * synchronously after calling drm_gem_mmap_obj.
  789. *
  790. * This function is mainly intended to implement the DMABUF mmap operation, when
  791. * the GEM object is not looked up based on its fake offset. To implement the
  792. * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  793. *
  794. * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
  795. * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
  796. * callers must verify access restrictions before calling this helper.
  797. *
  798. * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  799. * size, or if no gem_vm_ops are provided.
  800. */
  801. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  802. struct vm_area_struct *vma)
  803. {
  804. struct drm_device *dev = obj->dev;
  805. /* Check for valid size. */
  806. if (obj_size < vma->vm_end - vma->vm_start)
  807. return -EINVAL;
  808. if (!dev->driver->gem_vm_ops)
  809. return -EINVAL;
  810. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  811. vma->vm_ops = dev->driver->gem_vm_ops;
  812. vma->vm_private_data = obj;
  813. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  814. /* Take a ref for this mapping of the object, so that the fault
  815. * handler can dereference the mmap offset's pointer to the object.
  816. * This reference is cleaned up by the corresponding vm_close
  817. * (which should happen whether the vma was created by this call, or
  818. * by a vm_open due to mremap or partial unmap or whatever).
  819. */
  820. drm_gem_object_reference(obj);
  821. return 0;
  822. }
  823. EXPORT_SYMBOL(drm_gem_mmap_obj);
  824. /**
  825. * drm_gem_mmap - memory map routine for GEM objects
  826. * @filp: DRM file pointer
  827. * @vma: VMA for the area to be mapped
  828. *
  829. * If a driver supports GEM object mapping, mmap calls on the DRM file
  830. * descriptor will end up here.
  831. *
  832. * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
  833. * contain the fake offset we created when the GTT map ioctl was called on
  834. * the object) and map it with a call to drm_gem_mmap_obj().
  835. *
  836. * If the caller is not granted access to the buffer object, the mmap will fail
  837. * with EACCES. Please see the vma manager for more information.
  838. */
  839. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  840. {
  841. struct drm_file *priv = filp->private_data;
  842. struct drm_device *dev = priv->minor->dev;
  843. struct drm_gem_object *obj = NULL;
  844. struct drm_vma_offset_node *node;
  845. int ret;
  846. if (drm_device_is_unplugged(dev))
  847. return -ENODEV;
  848. drm_vma_offset_lock_lookup(dev->vma_offset_manager);
  849. node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
  850. vma->vm_pgoff,
  851. vma_pages(vma));
  852. if (likely(node)) {
  853. obj = container_of(node, struct drm_gem_object, vma_node);
  854. /*
  855. * When the object is being freed, after it hits 0-refcnt it
  856. * proceeds to tear down the object. In the process it will
  857. * attempt to remove the VMA offset and so acquire this
  858. * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
  859. * that matches our range, we know it is in the process of being
  860. * destroyed and will be freed as soon as we release the lock -
  861. * so we have to check for the 0-refcnted object and treat it as
  862. * invalid.
  863. */
  864. if (!kref_get_unless_zero(&obj->refcount))
  865. obj = NULL;
  866. }
  867. drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
  868. if (!obj)
  869. return -EINVAL;
  870. if (!drm_vma_node_is_allowed(node, filp)) {
  871. drm_gem_object_unreference_unlocked(obj);
  872. return -EACCES;
  873. }
  874. ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
  875. vma);
  876. drm_gem_object_unreference_unlocked(obj);
  877. return ret;
  878. }
  879. EXPORT_SYMBOL(drm_gem_mmap);