drm_gem.c 30 KB

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