drm_gem.c 22 KB

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