drm_gem.c 30 KB

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