drm_gem.c 29 KB

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