drm_gem.c 30 KB

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