drm_gem.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/mm.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/fs.h>
  32. #include <linux/file.h>
  33. #include <linux/module.h>
  34. #include <linux/mman.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/shmem_fs.h>
  37. #include <linux/dma-buf.h>
  38. #include <drm/drmP.h>
  39. #include <drm/drm_vma_manager.h>
  40. /** @file drm_gem.c
  41. *
  42. * This file provides some of the base ioctls and library routines for
  43. * the graphics memory manager implemented by each device driver.
  44. *
  45. * Because various devices have different requirements in terms of
  46. * synchronization and migration strategies, implementing that is left up to
  47. * the driver, and all that the general API provides should be generic --
  48. * allocating objects, reading/writing data with the cpu, freeing objects.
  49. * Even there, platform-dependent optimizations for reading/writing data with
  50. * the CPU mean we'll likely hook those out to driver-specific calls. However,
  51. * the DRI2 implementation wants to have at least allocate/mmap be generic.
  52. *
  53. * The goal was to have swap-backed object allocation managed through
  54. * struct file. However, file descriptors as handles to a struct file have
  55. * two major failings:
  56. * - Process limits prevent more than 1024 or so being used at a time by
  57. * default.
  58. * - Inability to allocate high fds will aggravate the X Server's select()
  59. * handling, and likely that of many GL client applications as well.
  60. *
  61. * This led to a plan of using our own integer IDs (called handles, following
  62. * DRM terminology) to mimic fds, and implement the fd syscalls we need as
  63. * ioctls. The objects themselves will still include the struct file so
  64. * that we can transition to fds if the required kernel infrastructure shows
  65. * up at a later date, and as our interface with shmfs for memory allocation.
  66. */
  67. /*
  68. * We make up offsets for buffer objects so we can recognize them at
  69. * mmap time.
  70. */
  71. /* pgoff in mmap is an unsigned long, so we need to make sure that
  72. * the faked up offset will fit
  73. */
  74. #if BITS_PER_LONG == 64
  75. #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
  76. #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
  77. #else
  78. #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
  79. #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
  80. #endif
  81. /**
  82. * drm_gem_init - Initialize the GEM device fields
  83. * @dev: drm_devic structure to initialize
  84. */
  85. int
  86. drm_gem_init(struct drm_device *dev)
  87. {
  88. struct drm_vma_offset_manager *vma_offset_manager;
  89. mutex_init(&dev->object_name_lock);
  90. idr_init(&dev->object_name_idr);
  91. vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
  92. if (!vma_offset_manager) {
  93. DRM_ERROR("out of memory\n");
  94. return -ENOMEM;
  95. }
  96. dev->vma_offset_manager = vma_offset_manager;
  97. drm_vma_offset_manager_init(vma_offset_manager,
  98. DRM_FILE_PAGE_OFFSET_START,
  99. DRM_FILE_PAGE_OFFSET_SIZE);
  100. return 0;
  101. }
  102. void
  103. drm_gem_destroy(struct drm_device *dev)
  104. {
  105. drm_vma_offset_manager_destroy(dev->vma_offset_manager);
  106. kfree(dev->vma_offset_manager);
  107. dev->vma_offset_manager = NULL;
  108. }
  109. /**
  110. * drm_gem_object_init - initialize an allocated shmem-backed GEM object
  111. * @dev: drm_device the object should be initialized for
  112. * @obj: drm_gem_object to initialize
  113. * @size: object size
  114. *
  115. * Initialize an already allocated GEM object of the specified size with
  116. * shmfs backing store.
  117. */
  118. int drm_gem_object_init(struct drm_device *dev,
  119. struct drm_gem_object *obj, size_t size)
  120. {
  121. struct file *filp;
  122. drm_gem_private_object_init(dev, obj, size);
  123. filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
  124. if (IS_ERR(filp))
  125. return PTR_ERR(filp);
  126. obj->filp = filp;
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(drm_gem_object_init);
  130. /**
  131. * drm_gem_object_init - initialize an allocated private GEM object
  132. * @dev: drm_device the object should be initialized for
  133. * @obj: drm_gem_object to initialize
  134. * @size: object size
  135. *
  136. * Initialize an already allocated GEM object of the specified size with
  137. * no GEM provided backing store. Instead the caller is responsible for
  138. * backing the object and handling it.
  139. */
  140. void drm_gem_private_object_init(struct drm_device *dev,
  141. struct drm_gem_object *obj, size_t size)
  142. {
  143. BUG_ON((size & (PAGE_SIZE - 1)) != 0);
  144. obj->dev = dev;
  145. obj->filp = NULL;
  146. kref_init(&obj->refcount);
  147. obj->handle_count = 0;
  148. obj->size = size;
  149. drm_vma_node_reset(&obj->vma_node);
  150. }
  151. EXPORT_SYMBOL(drm_gem_private_object_init);
  152. static void
  153. drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
  154. {
  155. /*
  156. * Note: obj->dma_buf can't disappear as long as we still hold a
  157. * handle reference in obj->handle_count.
  158. */
  159. mutex_lock(&filp->prime.lock);
  160. if (obj->dma_buf) {
  161. drm_prime_remove_buf_handle_locked(&filp->prime,
  162. obj->dma_buf);
  163. }
  164. mutex_unlock(&filp->prime.lock);
  165. }
  166. /**
  167. * drm_gem_object_free - release resources bound to userspace handles
  168. * @obj: GEM object to clean up.
  169. *
  170. * Called after the last handle to the object has been closed
  171. *
  172. * Removes any name for the object. Note that this must be
  173. * called before drm_gem_object_free or we'll be touching
  174. * freed memory
  175. */
  176. static void drm_gem_object_handle_free(struct drm_gem_object *obj)
  177. {
  178. struct drm_device *dev = obj->dev;
  179. /* Remove any name for this object */
  180. if (obj->name) {
  181. idr_remove(&dev->object_name_idr, obj->name);
  182. obj->name = 0;
  183. }
  184. }
  185. static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
  186. {
  187. /* Unbreak the reference cycle if we have an exported dma_buf. */
  188. if (obj->dma_buf) {
  189. dma_buf_put(obj->dma_buf);
  190. obj->dma_buf = NULL;
  191. }
  192. }
  193. static void
  194. drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
  195. {
  196. if (WARN_ON(obj->handle_count == 0))
  197. return;
  198. /*
  199. * Must bump handle count first as this may be the last
  200. * ref, in which case the object would disappear before we
  201. * checked for a name
  202. */
  203. mutex_lock(&obj->dev->object_name_lock);
  204. if (--obj->handle_count == 0) {
  205. drm_gem_object_handle_free(obj);
  206. drm_gem_object_exported_dma_buf_free(obj);
  207. }
  208. mutex_unlock(&obj->dev->object_name_lock);
  209. drm_gem_object_unreference_unlocked(obj);
  210. }
  211. /**
  212. * drm_gem_handle_delete - deletes the given file-private handle
  213. * @filp: drm file-private structure to use for the handle look up
  214. * @handle: userspace handle to delete
  215. *
  216. * Removes the GEM handle from the @filp lookup table and if this is the last
  217. * handle also cleans up linked resources like GEM names.
  218. */
  219. int
  220. drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  221. {
  222. struct drm_device *dev;
  223. struct drm_gem_object *obj;
  224. /* This is gross. The idr system doesn't let us try a delete and
  225. * return an error code. It just spews if you fail at deleting.
  226. * So, we have to grab a lock around finding the object and then
  227. * doing the delete on it and dropping the refcount, or the user
  228. * could race us to double-decrement the refcount and cause a
  229. * use-after-free later. Given the frequency of our handle lookups,
  230. * we may want to use ida for number allocation and a hash table
  231. * for the pointers, anyway.
  232. */
  233. spin_lock(&filp->table_lock);
  234. /* Check if we currently have a reference on the object */
  235. obj = idr_find(&filp->object_idr, handle);
  236. if (obj == NULL) {
  237. spin_unlock(&filp->table_lock);
  238. return -EINVAL;
  239. }
  240. dev = obj->dev;
  241. /* Release reference and decrement refcount. */
  242. idr_remove(&filp->object_idr, handle);
  243. spin_unlock(&filp->table_lock);
  244. if (drm_core_check_feature(dev, DRIVER_PRIME))
  245. drm_gem_remove_prime_handles(obj, filp);
  246. drm_vma_node_revoke(&obj->vma_node, filp->filp);
  247. if (dev->driver->gem_close_object)
  248. dev->driver->gem_close_object(obj, filp);
  249. drm_gem_object_handle_unreference_unlocked(obj);
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(drm_gem_handle_delete);
  253. /**
  254. * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
  255. * @file: drm file-private structure to remove the dumb handle from
  256. * @dev: corresponding drm_device
  257. * @handle: the dumb handle to remove
  258. *
  259. * This implements the ->dumb_destroy kms driver callback for drivers which use
  260. * gem to manage their backing storage.
  261. */
  262. int drm_gem_dumb_destroy(struct drm_file *file,
  263. struct drm_device *dev,
  264. uint32_t handle)
  265. {
  266. return drm_gem_handle_delete(file, handle);
  267. }
  268. EXPORT_SYMBOL(drm_gem_dumb_destroy);
  269. /**
  270. * drm_gem_handle_create_tail - internal functions to create a handle
  271. * @file_priv: drm file-private structure to register the handle for
  272. * @obj: object to register
  273. * @handlep: pionter to return the created handle to the caller
  274. *
  275. * This expects the dev->object_name_lock to be held already and will drop it
  276. * before returning. Used to avoid races in establishing new handles when
  277. * importing an object from either an flink name or a dma-buf.
  278. */
  279. int
  280. drm_gem_handle_create_tail(struct drm_file *file_priv,
  281. struct drm_gem_object *obj,
  282. u32 *handlep)
  283. {
  284. struct drm_device *dev = obj->dev;
  285. int ret;
  286. WARN_ON(!mutex_is_locked(&dev->object_name_lock));
  287. /*
  288. * Get the user-visible handle using idr. Preload and perform
  289. * allocation under our spinlock.
  290. */
  291. idr_preload(GFP_KERNEL);
  292. spin_lock(&file_priv->table_lock);
  293. ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
  294. drm_gem_object_reference(obj);
  295. obj->handle_count++;
  296. spin_unlock(&file_priv->table_lock);
  297. idr_preload_end();
  298. mutex_unlock(&dev->object_name_lock);
  299. if (ret < 0) {
  300. drm_gem_object_handle_unreference_unlocked(obj);
  301. return ret;
  302. }
  303. *handlep = ret;
  304. ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
  305. if (ret) {
  306. drm_gem_handle_delete(file_priv, *handlep);
  307. return ret;
  308. }
  309. if (dev->driver->gem_open_object) {
  310. ret = dev->driver->gem_open_object(obj, file_priv);
  311. if (ret) {
  312. drm_gem_handle_delete(file_priv, *handlep);
  313. return ret;
  314. }
  315. }
  316. return 0;
  317. }
  318. /**
  319. * gem_handle_create - create a gem handle for an object
  320. * @file_priv: drm file-private structure to register the handle for
  321. * @obj: object to register
  322. * @handlep: pionter to return the created handle to the caller
  323. *
  324. * Create a handle for this object. This adds a handle reference
  325. * to the object, which includes a regular reference count. Callers
  326. * will likely want to dereference the object afterwards.
  327. */
  328. int
  329. drm_gem_handle_create(struct drm_file *file_priv,
  330. struct drm_gem_object *obj,
  331. u32 *handlep)
  332. {
  333. mutex_lock(&obj->dev->object_name_lock);
  334. return drm_gem_handle_create_tail(file_priv, obj, handlep);
  335. }
  336. EXPORT_SYMBOL(drm_gem_handle_create);
  337. /**
  338. * drm_gem_free_mmap_offset - release a fake mmap offset for an object
  339. * @obj: obj in question
  340. *
  341. * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
  342. */
  343. void
  344. drm_gem_free_mmap_offset(struct drm_gem_object *obj)
  345. {
  346. struct drm_device *dev = obj->dev;
  347. drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
  348. }
  349. EXPORT_SYMBOL(drm_gem_free_mmap_offset);
  350. /**
  351. * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
  352. * @obj: obj in question
  353. * @size: the virtual size
  354. *
  355. * GEM memory mapping works by handing back to userspace a fake mmap offset
  356. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  357. * up the object based on the offset and sets up the various memory mapping
  358. * structures.
  359. *
  360. * This routine allocates and attaches a fake offset for @obj, in cases where
  361. * the virtual size differs from the physical size (ie. obj->size). Otherwise
  362. * just use drm_gem_create_mmap_offset().
  363. */
  364. int
  365. drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
  366. {
  367. struct drm_device *dev = obj->dev;
  368. return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
  369. size / PAGE_SIZE);
  370. }
  371. EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
  372. /**
  373. * drm_gem_create_mmap_offset - create a fake mmap offset for an object
  374. * @obj: obj in question
  375. *
  376. * GEM memory mapping works by handing back to userspace a fake mmap offset
  377. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  378. * up the object based on the offset and sets up the various memory mapping
  379. * structures.
  380. *
  381. * This routine allocates and attaches a fake offset for @obj.
  382. */
  383. int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
  384. {
  385. return drm_gem_create_mmap_offset_size(obj, obj->size);
  386. }
  387. EXPORT_SYMBOL(drm_gem_create_mmap_offset);
  388. /**
  389. * drm_gem_get_pages - helper to allocate backing pages for a GEM object
  390. * from shmem
  391. * @obj: obj in question
  392. * @gfpmask: gfp mask of requested pages
  393. */
  394. struct page **drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask)
  395. {
  396. struct inode *inode;
  397. struct address_space *mapping;
  398. struct page *p, **pages;
  399. int i, npages;
  400. /* This is the shared memory object that backs the GEM resource */
  401. inode = file_inode(obj->filp);
  402. mapping = inode->i_mapping;
  403. /* We already BUG_ON() for non-page-aligned sizes in
  404. * drm_gem_object_init(), so we should never hit this unless
  405. * driver author is doing something really wrong:
  406. */
  407. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  408. npages = obj->size >> PAGE_SHIFT;
  409. pages = drm_malloc_ab(npages, sizeof(struct page *));
  410. if (pages == NULL)
  411. return ERR_PTR(-ENOMEM);
  412. gfpmask |= mapping_gfp_mask(mapping);
  413. for (i = 0; i < npages; i++) {
  414. p = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
  415. if (IS_ERR(p))
  416. goto fail;
  417. pages[i] = p;
  418. /* Make sure shmem keeps __GFP_DMA32 allocated pages in the
  419. * correct region during swapin. Note that this requires
  420. * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
  421. * so shmem can relocate pages during swapin if required.
  422. */
  423. BUG_ON((gfpmask & __GFP_DMA32) &&
  424. (page_to_pfn(p) >= 0x00100000UL));
  425. }
  426. return pages;
  427. fail:
  428. while (i--)
  429. page_cache_release(pages[i]);
  430. drm_free_large(pages);
  431. return ERR_CAST(p);
  432. }
  433. EXPORT_SYMBOL(drm_gem_get_pages);
  434. /**
  435. * drm_gem_put_pages - helper to free backing pages for a GEM object
  436. * @obj: obj in question
  437. * @pages: pages to free
  438. * @dirty: if true, pages will be marked as dirty
  439. * @accessed: if true, the pages will be marked as accessed
  440. */
  441. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  442. bool dirty, bool accessed)
  443. {
  444. int i, npages;
  445. /* We already BUG_ON() for non-page-aligned sizes in
  446. * drm_gem_object_init(), so we should never hit this unless
  447. * driver author is doing something really wrong:
  448. */
  449. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  450. npages = obj->size >> PAGE_SHIFT;
  451. for (i = 0; i < npages; i++) {
  452. if (dirty)
  453. set_page_dirty(pages[i]);
  454. if (accessed)
  455. mark_page_accessed(pages[i]);
  456. /* Undo the reference we took when populating the table */
  457. page_cache_release(pages[i]);
  458. }
  459. drm_free_large(pages);
  460. }
  461. EXPORT_SYMBOL(drm_gem_put_pages);
  462. /** Returns a reference to the object named by the handle. */
  463. struct drm_gem_object *
  464. drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
  465. u32 handle)
  466. {
  467. struct drm_gem_object *obj;
  468. spin_lock(&filp->table_lock);
  469. /* Check if we currently have a reference on the object */
  470. obj = idr_find(&filp->object_idr, handle);
  471. if (obj == NULL) {
  472. spin_unlock(&filp->table_lock);
  473. return NULL;
  474. }
  475. drm_gem_object_reference(obj);
  476. spin_unlock(&filp->table_lock);
  477. return obj;
  478. }
  479. EXPORT_SYMBOL(drm_gem_object_lookup);
  480. /**
  481. * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
  482. * @dev: drm_device
  483. * @data: ioctl data
  484. * @file_priv: drm file-private structure
  485. *
  486. * Releases the handle to an mm object.
  487. */
  488. int
  489. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  490. struct drm_file *file_priv)
  491. {
  492. struct drm_gem_close *args = data;
  493. int ret;
  494. if (!(dev->driver->driver_features & DRIVER_GEM))
  495. return -ENODEV;
  496. ret = drm_gem_handle_delete(file_priv, args->handle);
  497. return ret;
  498. }
  499. /**
  500. * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
  501. * @dev: drm_device
  502. * @data: ioctl data
  503. * @file_priv: drm file-private structure
  504. *
  505. * Create a global name for an object, returning the name.
  506. *
  507. * Note that the name does not hold a reference; when the object
  508. * is freed, the name goes away.
  509. */
  510. int
  511. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  512. struct drm_file *file_priv)
  513. {
  514. struct drm_gem_flink *args = data;
  515. struct drm_gem_object *obj;
  516. int ret;
  517. if (!(dev->driver->driver_features & DRIVER_GEM))
  518. return -ENODEV;
  519. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  520. if (obj == NULL)
  521. return -ENOENT;
  522. mutex_lock(&dev->object_name_lock);
  523. idr_preload(GFP_KERNEL);
  524. /* prevent races with concurrent gem_close. */
  525. if (obj->handle_count == 0) {
  526. ret = -ENOENT;
  527. goto err;
  528. }
  529. if (!obj->name) {
  530. ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT);
  531. if (ret < 0)
  532. goto err;
  533. obj->name = ret;
  534. }
  535. args->name = (uint64_t) obj->name;
  536. ret = 0;
  537. err:
  538. idr_preload_end();
  539. mutex_unlock(&dev->object_name_lock);
  540. drm_gem_object_unreference_unlocked(obj);
  541. return ret;
  542. }
  543. /**
  544. * drm_gem_open - implementation of the GEM_OPEN ioctl
  545. * @dev: drm_device
  546. * @data: ioctl data
  547. * @file_priv: drm file-private structure
  548. *
  549. * Open an object using the global name, returning a handle and the size.
  550. *
  551. * This handle (of course) holds a reference to the object, so the object
  552. * will not go away until the handle is deleted.
  553. */
  554. int
  555. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  556. struct drm_file *file_priv)
  557. {
  558. struct drm_gem_open *args = data;
  559. struct drm_gem_object *obj;
  560. int ret;
  561. u32 handle;
  562. if (!(dev->driver->driver_features & DRIVER_GEM))
  563. return -ENODEV;
  564. mutex_lock(&dev->object_name_lock);
  565. obj = idr_find(&dev->object_name_idr, (int) args->name);
  566. if (obj) {
  567. drm_gem_object_reference(obj);
  568. } else {
  569. mutex_unlock(&dev->object_name_lock);
  570. return -ENOENT;
  571. }
  572. /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
  573. ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
  574. drm_gem_object_unreference_unlocked(obj);
  575. if (ret)
  576. return ret;
  577. args->handle = handle;
  578. args->size = obj->size;
  579. return 0;
  580. }
  581. /**
  582. * gem_gem_open - initalizes GEM file-private structures at devnode open time
  583. * @dev: drm_device which is being opened by userspace
  584. * @file_private: drm file-private structure to set up
  585. *
  586. * Called at device open time, sets up the structure for handling refcounting
  587. * of mm objects.
  588. */
  589. void
  590. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  591. {
  592. idr_init(&file_private->object_idr);
  593. spin_lock_init(&file_private->table_lock);
  594. }
  595. /*
  596. * Called at device close to release the file's
  597. * handle references on objects.
  598. */
  599. static int
  600. drm_gem_object_release_handle(int id, void *ptr, void *data)
  601. {
  602. struct drm_file *file_priv = data;
  603. struct drm_gem_object *obj = ptr;
  604. struct drm_device *dev = obj->dev;
  605. if (drm_core_check_feature(dev, DRIVER_PRIME))
  606. drm_gem_remove_prime_handles(obj, file_priv);
  607. drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
  608. if (dev->driver->gem_close_object)
  609. dev->driver->gem_close_object(obj, file_priv);
  610. drm_gem_object_handle_unreference_unlocked(obj);
  611. return 0;
  612. }
  613. /**
  614. * drm_gem_release - release file-private GEM resources
  615. * @dev: drm_device which is being closed by userspace
  616. * @file_private: drm file-private structure to clean up
  617. *
  618. * Called at close time when the filp is going away.
  619. *
  620. * Releases any remaining references on objects by this filp.
  621. */
  622. void
  623. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  624. {
  625. idr_for_each(&file_private->object_idr,
  626. &drm_gem_object_release_handle, file_private);
  627. idr_destroy(&file_private->object_idr);
  628. }
  629. void
  630. drm_gem_object_release(struct drm_gem_object *obj)
  631. {
  632. WARN_ON(obj->dma_buf);
  633. if (obj->filp)
  634. fput(obj->filp);
  635. drm_gem_free_mmap_offset(obj);
  636. }
  637. EXPORT_SYMBOL(drm_gem_object_release);
  638. /**
  639. * drm_gem_object_free - free a GEM object
  640. * @kref: kref of the object to free
  641. *
  642. * Called after the last reference to the object has been lost.
  643. * Must be called holding struct_ mutex
  644. *
  645. * Frees the object
  646. */
  647. void
  648. drm_gem_object_free(struct kref *kref)
  649. {
  650. struct drm_gem_object *obj = (struct drm_gem_object *) kref;
  651. struct drm_device *dev = obj->dev;
  652. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  653. if (dev->driver->gem_free_object != NULL)
  654. dev->driver->gem_free_object(obj);
  655. }
  656. EXPORT_SYMBOL(drm_gem_object_free);
  657. void drm_gem_vm_open(struct vm_area_struct *vma)
  658. {
  659. struct drm_gem_object *obj = vma->vm_private_data;
  660. drm_gem_object_reference(obj);
  661. mutex_lock(&obj->dev->struct_mutex);
  662. drm_vm_open_locked(obj->dev, vma);
  663. mutex_unlock(&obj->dev->struct_mutex);
  664. }
  665. EXPORT_SYMBOL(drm_gem_vm_open);
  666. void drm_gem_vm_close(struct vm_area_struct *vma)
  667. {
  668. struct drm_gem_object *obj = vma->vm_private_data;
  669. struct drm_device *dev = obj->dev;
  670. mutex_lock(&dev->struct_mutex);
  671. drm_vm_close_locked(obj->dev, vma);
  672. drm_gem_object_unreference(obj);
  673. mutex_unlock(&dev->struct_mutex);
  674. }
  675. EXPORT_SYMBOL(drm_gem_vm_close);
  676. /**
  677. * drm_gem_mmap_obj - memory map a GEM object
  678. * @obj: the GEM object to map
  679. * @obj_size: the object size to be mapped, in bytes
  680. * @vma: VMA for the area to be mapped
  681. *
  682. * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
  683. * provided by the driver. Depending on their requirements, drivers can either
  684. * provide a fault handler in their gem_vm_ops (in which case any accesses to
  685. * the object will be trapped, to perform migration, GTT binding, surface
  686. * register allocation, or performance monitoring), or mmap the buffer memory
  687. * synchronously after calling drm_gem_mmap_obj.
  688. *
  689. * This function is mainly intended to implement the DMABUF mmap operation, when
  690. * the GEM object is not looked up based on its fake offset. To implement the
  691. * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  692. *
  693. * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
  694. * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
  695. * callers must verify access restrictions before calling this helper.
  696. *
  697. * NOTE: This function has to be protected with dev->struct_mutex
  698. *
  699. * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  700. * size, or if no gem_vm_ops are provided.
  701. */
  702. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  703. struct vm_area_struct *vma)
  704. {
  705. struct drm_device *dev = obj->dev;
  706. lockdep_assert_held(&dev->struct_mutex);
  707. /* Check for valid size. */
  708. if (obj_size < vma->vm_end - vma->vm_start)
  709. return -EINVAL;
  710. if (!dev->driver->gem_vm_ops)
  711. return -EINVAL;
  712. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  713. vma->vm_ops = dev->driver->gem_vm_ops;
  714. vma->vm_private_data = obj;
  715. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  716. /* Take a ref for this mapping of the object, so that the fault
  717. * handler can dereference the mmap offset's pointer to the object.
  718. * This reference is cleaned up by the corresponding vm_close
  719. * (which should happen whether the vma was created by this call, or
  720. * by a vm_open due to mremap or partial unmap or whatever).
  721. */
  722. drm_gem_object_reference(obj);
  723. drm_vm_open_locked(dev, vma);
  724. return 0;
  725. }
  726. EXPORT_SYMBOL(drm_gem_mmap_obj);
  727. /**
  728. * drm_gem_mmap - memory map routine for GEM objects
  729. * @filp: DRM file pointer
  730. * @vma: VMA for the area to be mapped
  731. *
  732. * If a driver supports GEM object mapping, mmap calls on the DRM file
  733. * descriptor will end up here.
  734. *
  735. * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
  736. * contain the fake offset we created when the GTT map ioctl was called on
  737. * the object) and map it with a call to drm_gem_mmap_obj().
  738. *
  739. * If the caller is not granted access to the buffer object, the mmap will fail
  740. * with EACCES. Please see the vma manager for more information.
  741. */
  742. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  743. {
  744. struct drm_file *priv = filp->private_data;
  745. struct drm_device *dev = priv->minor->dev;
  746. struct drm_gem_object *obj;
  747. struct drm_vma_offset_node *node;
  748. int ret;
  749. if (drm_device_is_unplugged(dev))
  750. return -ENODEV;
  751. mutex_lock(&dev->struct_mutex);
  752. node = drm_vma_offset_exact_lookup(dev->vma_offset_manager,
  753. vma->vm_pgoff,
  754. vma_pages(vma));
  755. if (!node) {
  756. mutex_unlock(&dev->struct_mutex);
  757. return drm_mmap(filp, vma);
  758. } else if (!drm_vma_node_is_allowed(node, filp)) {
  759. mutex_unlock(&dev->struct_mutex);
  760. return -EACCES;
  761. }
  762. obj = container_of(node, struct drm_gem_object, vma_node);
  763. ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, vma);
  764. mutex_unlock(&dev->struct_mutex);
  765. return ret;
  766. }
  767. EXPORT_SYMBOL(drm_gem_mmap);