drm_gem.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. * Initialize the GEM device fields
  83. */
  84. int
  85. drm_gem_init(struct drm_device *dev)
  86. {
  87. struct drm_gem_mm *mm;
  88. mutex_init(&dev->object_name_lock);
  89. idr_init(&dev->object_name_idr);
  90. mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
  91. if (!mm) {
  92. DRM_ERROR("out of memory\n");
  93. return -ENOMEM;
  94. }
  95. dev->mm_private = mm;
  96. drm_vma_offset_manager_init(&mm->vma_manager,
  97. DRM_FILE_PAGE_OFFSET_START,
  98. DRM_FILE_PAGE_OFFSET_SIZE);
  99. return 0;
  100. }
  101. void
  102. drm_gem_destroy(struct drm_device *dev)
  103. {
  104. struct drm_gem_mm *mm = dev->mm_private;
  105. drm_vma_offset_manager_destroy(&mm->vma_manager);
  106. kfree(mm);
  107. dev->mm_private = NULL;
  108. }
  109. /**
  110. * Initialize an already allocated GEM object of the specified size with
  111. * shmfs backing store.
  112. */
  113. int drm_gem_object_init(struct drm_device *dev,
  114. struct drm_gem_object *obj, size_t size)
  115. {
  116. struct file *filp;
  117. filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
  118. if (IS_ERR(filp))
  119. return PTR_ERR(filp);
  120. drm_gem_private_object_init(dev, obj, size);
  121. obj->filp = filp;
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(drm_gem_object_init);
  125. /**
  126. * Initialize an already allocated GEM object of the specified size with
  127. * no GEM provided backing store. Instead the caller is responsible for
  128. * backing the object and handling it.
  129. */
  130. void drm_gem_private_object_init(struct drm_device *dev,
  131. struct drm_gem_object *obj, size_t size)
  132. {
  133. BUG_ON((size & (PAGE_SIZE - 1)) != 0);
  134. obj->dev = dev;
  135. obj->filp = NULL;
  136. kref_init(&obj->refcount);
  137. obj->handle_count = 0;
  138. obj->size = size;
  139. drm_vma_node_reset(&obj->vma_node);
  140. }
  141. EXPORT_SYMBOL(drm_gem_private_object_init);
  142. /**
  143. * Allocate a GEM object of the specified size with shmfs backing store
  144. */
  145. struct drm_gem_object *
  146. drm_gem_object_alloc(struct drm_device *dev, size_t size)
  147. {
  148. struct drm_gem_object *obj;
  149. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  150. if (!obj)
  151. goto free;
  152. if (drm_gem_object_init(dev, obj, size) != 0)
  153. goto free;
  154. if (dev->driver->gem_init_object != NULL &&
  155. dev->driver->gem_init_object(obj) != 0) {
  156. goto fput;
  157. }
  158. return obj;
  159. fput:
  160. /* Object_init mangles the global counters - readjust them. */
  161. fput(obj->filp);
  162. free:
  163. kfree(obj);
  164. return NULL;
  165. }
  166. EXPORT_SYMBOL(drm_gem_object_alloc);
  167. static void
  168. drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
  169. {
  170. /*
  171. * Note: obj->dma_buf can't disappear as long as we still hold a
  172. * handle reference in obj->handle_count.
  173. */
  174. mutex_lock(&filp->prime.lock);
  175. if (obj->dma_buf) {
  176. drm_prime_remove_buf_handle_locked(&filp->prime,
  177. obj->dma_buf);
  178. }
  179. mutex_unlock(&filp->prime.lock);
  180. }
  181. static void drm_gem_object_ref_bug(struct kref *list_kref)
  182. {
  183. BUG();
  184. }
  185. /**
  186. * Called after the last handle to the object has been closed
  187. *
  188. * Removes any name for the object. Note that this must be
  189. * called before drm_gem_object_free or we'll be touching
  190. * freed memory
  191. */
  192. static void drm_gem_object_handle_free(struct drm_gem_object *obj)
  193. {
  194. struct drm_device *dev = obj->dev;
  195. /* Remove any name for this object */
  196. if (obj->name) {
  197. idr_remove(&dev->object_name_idr, obj->name);
  198. obj->name = 0;
  199. /*
  200. * The object name held a reference to this object, drop
  201. * that now.
  202. *
  203. * This cannot be the last reference, since the handle holds one too.
  204. */
  205. kref_put(&obj->refcount, drm_gem_object_ref_bug);
  206. }
  207. }
  208. static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
  209. {
  210. /* Unbreak the reference cycle if we have an exported dma_buf. */
  211. if (obj->dma_buf) {
  212. dma_buf_put(obj->dma_buf);
  213. obj->dma_buf = NULL;
  214. }
  215. }
  216. static void
  217. drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
  218. {
  219. if (WARN_ON(obj->handle_count == 0))
  220. return;
  221. /*
  222. * Must bump handle count first as this may be the last
  223. * ref, in which case the object would disappear before we
  224. * checked for a name
  225. */
  226. mutex_lock(&obj->dev->object_name_lock);
  227. if (--obj->handle_count == 0) {
  228. drm_gem_object_handle_free(obj);
  229. drm_gem_object_exported_dma_buf_free(obj);
  230. }
  231. mutex_unlock(&obj->dev->object_name_lock);
  232. drm_gem_object_unreference_unlocked(obj);
  233. }
  234. /**
  235. * Removes the mapping from handle to filp for this object.
  236. */
  237. int
  238. drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  239. {
  240. struct drm_device *dev;
  241. struct drm_gem_object *obj;
  242. /* This is gross. The idr system doesn't let us try a delete and
  243. * return an error code. It just spews if you fail at deleting.
  244. * So, we have to grab a lock around finding the object and then
  245. * doing the delete on it and dropping the refcount, or the user
  246. * could race us to double-decrement the refcount and cause a
  247. * use-after-free later. Given the frequency of our handle lookups,
  248. * we may want to use ida for number allocation and a hash table
  249. * for the pointers, anyway.
  250. */
  251. spin_lock(&filp->table_lock);
  252. /* Check if we currently have a reference on the object */
  253. obj = idr_find(&filp->object_idr, handle);
  254. if (obj == NULL) {
  255. spin_unlock(&filp->table_lock);
  256. return -EINVAL;
  257. }
  258. dev = obj->dev;
  259. /* Release reference and decrement refcount. */
  260. idr_remove(&filp->object_idr, handle);
  261. spin_unlock(&filp->table_lock);
  262. drm_gem_remove_prime_handles(obj, filp);
  263. drm_vma_node_revoke(&obj->vma_node, filp->filp);
  264. if (dev->driver->gem_close_object)
  265. dev->driver->gem_close_object(obj, filp);
  266. drm_gem_object_handle_unreference_unlocked(obj);
  267. return 0;
  268. }
  269. EXPORT_SYMBOL(drm_gem_handle_delete);
  270. /**
  271. * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
  272. *
  273. * This implements the ->dumb_destroy kms driver callback for drivers which use
  274. * gem to manage their backing storage.
  275. */
  276. int drm_gem_dumb_destroy(struct drm_file *file,
  277. struct drm_device *dev,
  278. uint32_t handle)
  279. {
  280. return drm_gem_handle_delete(file, handle);
  281. }
  282. EXPORT_SYMBOL(drm_gem_dumb_destroy);
  283. /**
  284. * drm_gem_handle_create_tail - internal functions to create a handle
  285. *
  286. * This expects the dev->object_name_lock to be held already and will drop it
  287. * before returning. Used to avoid races in establishing new handles when
  288. * importing an object from either an flink name or a dma-buf.
  289. */
  290. int
  291. drm_gem_handle_create_tail(struct drm_file *file_priv,
  292. struct drm_gem_object *obj,
  293. u32 *handlep)
  294. {
  295. struct drm_device *dev = obj->dev;
  296. int ret;
  297. WARN_ON(!mutex_is_locked(&dev->object_name_lock));
  298. /*
  299. * Get the user-visible handle using idr. Preload and perform
  300. * allocation under our spinlock.
  301. */
  302. idr_preload(GFP_KERNEL);
  303. spin_lock(&file_priv->table_lock);
  304. ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
  305. drm_gem_object_reference(obj);
  306. obj->handle_count++;
  307. spin_unlock(&file_priv->table_lock);
  308. idr_preload_end();
  309. mutex_unlock(&dev->object_name_lock);
  310. if (ret < 0) {
  311. drm_gem_object_handle_unreference_unlocked(obj);
  312. return ret;
  313. }
  314. *handlep = ret;
  315. ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
  316. if (ret) {
  317. drm_gem_handle_delete(file_priv, *handlep);
  318. return ret;
  319. }
  320. if (dev->driver->gem_open_object) {
  321. ret = dev->driver->gem_open_object(obj, file_priv);
  322. if (ret) {
  323. drm_gem_handle_delete(file_priv, *handlep);
  324. return ret;
  325. }
  326. }
  327. return 0;
  328. }
  329. /**
  330. * Create a handle for this object. This adds a handle reference
  331. * to the object, which includes a regular reference count. Callers
  332. * will likely want to dereference the object afterwards.
  333. */
  334. int
  335. drm_gem_handle_create(struct drm_file *file_priv,
  336. struct drm_gem_object *obj,
  337. u32 *handlep)
  338. {
  339. mutex_lock(&obj->dev->object_name_lock);
  340. return drm_gem_handle_create_tail(file_priv, obj, handlep);
  341. }
  342. EXPORT_SYMBOL(drm_gem_handle_create);
  343. /**
  344. * drm_gem_free_mmap_offset - release a fake mmap offset for an object
  345. * @obj: obj in question
  346. *
  347. * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
  348. */
  349. void
  350. drm_gem_free_mmap_offset(struct drm_gem_object *obj)
  351. {
  352. struct drm_device *dev = obj->dev;
  353. struct drm_gem_mm *mm = dev->mm_private;
  354. drm_vma_offset_remove(&mm->vma_manager, &obj->vma_node);
  355. }
  356. EXPORT_SYMBOL(drm_gem_free_mmap_offset);
  357. /**
  358. * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
  359. * @obj: obj in question
  360. * @size: the virtual size
  361. *
  362. * GEM memory mapping works by handing back to userspace a fake mmap offset
  363. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  364. * up the object based on the offset and sets up the various memory mapping
  365. * structures.
  366. *
  367. * This routine allocates and attaches a fake offset for @obj, in cases where
  368. * the virtual size differs from the physical size (ie. obj->size). Otherwise
  369. * just use drm_gem_create_mmap_offset().
  370. */
  371. int
  372. drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
  373. {
  374. struct drm_device *dev = obj->dev;
  375. struct drm_gem_mm *mm = dev->mm_private;
  376. return drm_vma_offset_add(&mm->vma_manager, &obj->vma_node,
  377. size / PAGE_SIZE);
  378. }
  379. EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
  380. /**
  381. * drm_gem_create_mmap_offset - create a fake mmap offset for an object
  382. * @obj: obj in question
  383. *
  384. * GEM memory mapping works by handing back to userspace a fake mmap offset
  385. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  386. * up the object based on the offset and sets up the various memory mapping
  387. * structures.
  388. *
  389. * This routine allocates and attaches a fake offset for @obj.
  390. */
  391. int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
  392. {
  393. return drm_gem_create_mmap_offset_size(obj, obj->size);
  394. }
  395. EXPORT_SYMBOL(drm_gem_create_mmap_offset);
  396. /**
  397. * drm_gem_get_pages - helper to allocate backing pages for a GEM object
  398. * from shmem
  399. * @obj: obj in question
  400. * @gfpmask: gfp mask of requested pages
  401. */
  402. struct page **drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask)
  403. {
  404. struct inode *inode;
  405. struct address_space *mapping;
  406. struct page *p, **pages;
  407. int i, npages;
  408. /* This is the shared memory object that backs the GEM resource */
  409. inode = file_inode(obj->filp);
  410. mapping = inode->i_mapping;
  411. /* We already BUG_ON() for non-page-aligned sizes in
  412. * drm_gem_object_init(), so we should never hit this unless
  413. * driver author is doing something really wrong:
  414. */
  415. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  416. npages = obj->size >> PAGE_SHIFT;
  417. pages = drm_malloc_ab(npages, sizeof(struct page *));
  418. if (pages == NULL)
  419. return ERR_PTR(-ENOMEM);
  420. gfpmask |= mapping_gfp_mask(mapping);
  421. for (i = 0; i < npages; i++) {
  422. p = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
  423. if (IS_ERR(p))
  424. goto fail;
  425. pages[i] = p;
  426. /* There is a hypothetical issue w/ drivers that require
  427. * buffer memory in the low 4GB.. if the pages are un-
  428. * pinned, and swapped out, they can end up swapped back
  429. * in above 4GB. If pages are already in memory, then
  430. * shmem_read_mapping_page_gfp will ignore the gfpmask,
  431. * even if the already in-memory page disobeys the mask.
  432. *
  433. * It is only a theoretical issue today, because none of
  434. * the devices with this limitation can be populated with
  435. * enough memory to trigger the issue. But this BUG_ON()
  436. * is here as a reminder in case the problem with
  437. * shmem_read_mapping_page_gfp() isn't solved by the time
  438. * it does become a real issue.
  439. *
  440. * See this thread: http://lkml.org/lkml/2011/7/11/238
  441. */
  442. BUG_ON((gfpmask & __GFP_DMA32) &&
  443. (page_to_pfn(p) >= 0x00100000UL));
  444. }
  445. return pages;
  446. fail:
  447. while (i--)
  448. page_cache_release(pages[i]);
  449. drm_free_large(pages);
  450. return ERR_CAST(p);
  451. }
  452. EXPORT_SYMBOL(drm_gem_get_pages);
  453. /**
  454. * drm_gem_put_pages - helper to free backing pages for a GEM object
  455. * @obj: obj in question
  456. * @pages: pages to free
  457. * @dirty: if true, pages will be marked as dirty
  458. * @accessed: if true, the pages will be marked as accessed
  459. */
  460. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  461. bool dirty, bool accessed)
  462. {
  463. int i, npages;
  464. /* We already BUG_ON() for non-page-aligned sizes in
  465. * drm_gem_object_init(), so we should never hit this unless
  466. * driver author is doing something really wrong:
  467. */
  468. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  469. npages = obj->size >> PAGE_SHIFT;
  470. for (i = 0; i < npages; i++) {
  471. if (dirty)
  472. set_page_dirty(pages[i]);
  473. if (accessed)
  474. mark_page_accessed(pages[i]);
  475. /* Undo the reference we took when populating the table */
  476. page_cache_release(pages[i]);
  477. }
  478. drm_free_large(pages);
  479. }
  480. EXPORT_SYMBOL(drm_gem_put_pages);
  481. /** Returns a reference to the object named by the handle. */
  482. struct drm_gem_object *
  483. drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
  484. u32 handle)
  485. {
  486. struct drm_gem_object *obj;
  487. spin_lock(&filp->table_lock);
  488. /* Check if we currently have a reference on the object */
  489. obj = idr_find(&filp->object_idr, handle);
  490. if (obj == NULL) {
  491. spin_unlock(&filp->table_lock);
  492. return NULL;
  493. }
  494. drm_gem_object_reference(obj);
  495. spin_unlock(&filp->table_lock);
  496. return obj;
  497. }
  498. EXPORT_SYMBOL(drm_gem_object_lookup);
  499. /**
  500. * Releases the handle to an mm object.
  501. */
  502. int
  503. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  504. struct drm_file *file_priv)
  505. {
  506. struct drm_gem_close *args = data;
  507. int ret;
  508. if (!(dev->driver->driver_features & DRIVER_GEM))
  509. return -ENODEV;
  510. ret = drm_gem_handle_delete(file_priv, args->handle);
  511. return ret;
  512. }
  513. /**
  514. * Create a global name for an object, returning the name.
  515. *
  516. * Note that the name does not hold a reference; when the object
  517. * is freed, the name goes away.
  518. */
  519. int
  520. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  521. struct drm_file *file_priv)
  522. {
  523. struct drm_gem_flink *args = data;
  524. struct drm_gem_object *obj;
  525. int ret;
  526. if (!(dev->driver->driver_features & DRIVER_GEM))
  527. return -ENODEV;
  528. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  529. if (obj == NULL)
  530. return -ENOENT;
  531. mutex_lock(&dev->object_name_lock);
  532. idr_preload(GFP_KERNEL);
  533. /* prevent races with concurrent gem_close. */
  534. if (obj->handle_count == 0) {
  535. ret = -ENOENT;
  536. goto err;
  537. }
  538. if (!obj->name) {
  539. ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT);
  540. if (ret < 0)
  541. goto err;
  542. obj->name = ret;
  543. /* Allocate a reference for the name table. */
  544. drm_gem_object_reference(obj);
  545. }
  546. args->name = (uint64_t) obj->name;
  547. ret = 0;
  548. err:
  549. idr_preload_end();
  550. mutex_unlock(&dev->object_name_lock);
  551. drm_gem_object_unreference_unlocked(obj);
  552. return ret;
  553. }
  554. /**
  555. * Open an object using the global name, returning a handle and the size.
  556. *
  557. * This handle (of course) holds a reference to the object, so the object
  558. * will not go away until the handle is deleted.
  559. */
  560. int
  561. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  562. struct drm_file *file_priv)
  563. {
  564. struct drm_gem_open *args = data;
  565. struct drm_gem_object *obj;
  566. int ret;
  567. u32 handle;
  568. if (!(dev->driver->driver_features & DRIVER_GEM))
  569. return -ENODEV;
  570. mutex_lock(&dev->object_name_lock);
  571. obj = idr_find(&dev->object_name_idr, (int) args->name);
  572. if (obj) {
  573. drm_gem_object_reference(obj);
  574. } else {
  575. mutex_unlock(&dev->object_name_lock);
  576. return -ENOENT;
  577. }
  578. /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
  579. ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
  580. drm_gem_object_unreference_unlocked(obj);
  581. if (ret)
  582. return ret;
  583. args->handle = handle;
  584. args->size = obj->size;
  585. return 0;
  586. }
  587. /**
  588. * Called at device open time, sets up the structure for handling refcounting
  589. * of mm objects.
  590. */
  591. void
  592. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  593. {
  594. idr_init(&file_private->object_idr);
  595. spin_lock_init(&file_private->table_lock);
  596. }
  597. /**
  598. * Called at device close to release the file's
  599. * handle references on objects.
  600. */
  601. static int
  602. drm_gem_object_release_handle(int id, void *ptr, void *data)
  603. {
  604. struct drm_file *file_priv = data;
  605. struct drm_gem_object *obj = ptr;
  606. struct drm_device *dev = obj->dev;
  607. drm_gem_remove_prime_handles(obj, file_priv);
  608. drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
  609. if (dev->driver->gem_close_object)
  610. dev->driver->gem_close_object(obj, file_priv);
  611. drm_gem_object_handle_unreference_unlocked(obj);
  612. return 0;
  613. }
  614. /**
  615. * Called at close time when the filp is going away.
  616. *
  617. * Releases any remaining references on objects by this filp.
  618. */
  619. void
  620. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  621. {
  622. idr_for_each(&file_private->object_idr,
  623. &drm_gem_object_release_handle, file_private);
  624. idr_destroy(&file_private->object_idr);
  625. }
  626. void
  627. drm_gem_object_release(struct drm_gem_object *obj)
  628. {
  629. WARN_ON(obj->dma_buf);
  630. if (obj->filp)
  631. fput(obj->filp);
  632. }
  633. EXPORT_SYMBOL(drm_gem_object_release);
  634. /**
  635. * Called after the last reference to the object has been lost.
  636. * Must be called holding struct_ mutex
  637. *
  638. * Frees the object
  639. */
  640. void
  641. drm_gem_object_free(struct kref *kref)
  642. {
  643. struct drm_gem_object *obj = (struct drm_gem_object *) kref;
  644. struct drm_device *dev = obj->dev;
  645. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  646. if (dev->driver->gem_free_object != NULL)
  647. dev->driver->gem_free_object(obj);
  648. }
  649. EXPORT_SYMBOL(drm_gem_object_free);
  650. void drm_gem_vm_open(struct vm_area_struct *vma)
  651. {
  652. struct drm_gem_object *obj = vma->vm_private_data;
  653. drm_gem_object_reference(obj);
  654. mutex_lock(&obj->dev->struct_mutex);
  655. drm_vm_open_locked(obj->dev, vma);
  656. mutex_unlock(&obj->dev->struct_mutex);
  657. }
  658. EXPORT_SYMBOL(drm_gem_vm_open);
  659. void drm_gem_vm_close(struct vm_area_struct *vma)
  660. {
  661. struct drm_gem_object *obj = vma->vm_private_data;
  662. struct drm_device *dev = obj->dev;
  663. mutex_lock(&dev->struct_mutex);
  664. drm_vm_close_locked(obj->dev, vma);
  665. drm_gem_object_unreference(obj);
  666. mutex_unlock(&dev->struct_mutex);
  667. }
  668. EXPORT_SYMBOL(drm_gem_vm_close);
  669. /**
  670. * drm_gem_mmap_obj - memory map a GEM object
  671. * @obj: the GEM object to map
  672. * @obj_size: the object size to be mapped, in bytes
  673. * @vma: VMA for the area to be mapped
  674. *
  675. * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
  676. * provided by the driver. Depending on their requirements, drivers can either
  677. * provide a fault handler in their gem_vm_ops (in which case any accesses to
  678. * the object will be trapped, to perform migration, GTT binding, surface
  679. * register allocation, or performance monitoring), or mmap the buffer memory
  680. * synchronously after calling drm_gem_mmap_obj.
  681. *
  682. * This function is mainly intended to implement the DMABUF mmap operation, when
  683. * the GEM object is not looked up based on its fake offset. To implement the
  684. * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  685. *
  686. * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
  687. * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
  688. * callers must verify access restrictions before calling this helper.
  689. *
  690. * NOTE: This function has to be protected with dev->struct_mutex
  691. *
  692. * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  693. * size, or if no gem_vm_ops are provided.
  694. */
  695. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  696. struct vm_area_struct *vma)
  697. {
  698. struct drm_device *dev = obj->dev;
  699. lockdep_assert_held(&dev->struct_mutex);
  700. /* Check for valid size. */
  701. if (obj_size < vma->vm_end - vma->vm_start)
  702. return -EINVAL;
  703. if (!dev->driver->gem_vm_ops)
  704. return -EINVAL;
  705. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  706. vma->vm_ops = dev->driver->gem_vm_ops;
  707. vma->vm_private_data = obj;
  708. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  709. /* Take a ref for this mapping of the object, so that the fault
  710. * handler can dereference the mmap offset's pointer to the object.
  711. * This reference is cleaned up by the corresponding vm_close
  712. * (which should happen whether the vma was created by this call, or
  713. * by a vm_open due to mremap or partial unmap or whatever).
  714. */
  715. drm_gem_object_reference(obj);
  716. drm_vm_open_locked(dev, vma);
  717. return 0;
  718. }
  719. EXPORT_SYMBOL(drm_gem_mmap_obj);
  720. /**
  721. * drm_gem_mmap - memory map routine for GEM objects
  722. * @filp: DRM file pointer
  723. * @vma: VMA for the area to be mapped
  724. *
  725. * If a driver supports GEM object mapping, mmap calls on the DRM file
  726. * descriptor will end up here.
  727. *
  728. * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
  729. * contain the fake offset we created when the GTT map ioctl was called on
  730. * the object) and map it with a call to drm_gem_mmap_obj().
  731. *
  732. * If the caller is not granted access to the buffer object, the mmap will fail
  733. * with EACCES. Please see the vma manager for more information.
  734. */
  735. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  736. {
  737. struct drm_file *priv = filp->private_data;
  738. struct drm_device *dev = priv->minor->dev;
  739. struct drm_gem_mm *mm = dev->mm_private;
  740. struct drm_gem_object *obj;
  741. struct drm_vma_offset_node *node;
  742. int ret = 0;
  743. if (drm_device_is_unplugged(dev))
  744. return -ENODEV;
  745. mutex_lock(&dev->struct_mutex);
  746. node = drm_vma_offset_exact_lookup(&mm->vma_manager, vma->vm_pgoff,
  747. vma_pages(vma));
  748. if (!node) {
  749. mutex_unlock(&dev->struct_mutex);
  750. return drm_mmap(filp, vma);
  751. } else if (!drm_vma_node_is_allowed(node, filp)) {
  752. mutex_unlock(&dev->struct_mutex);
  753. return -EACCES;
  754. }
  755. obj = container_of(node, struct drm_gem_object, vma_node);
  756. ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, vma);
  757. mutex_unlock(&dev->struct_mutex);
  758. return ret;
  759. }
  760. EXPORT_SYMBOL(drm_gem_mmap);