drm_gem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 "drmP.h"
  37. /** @file drm_gem.c
  38. *
  39. * This file provides some of the base ioctls and library routines for
  40. * the graphics memory manager implemented by each device driver.
  41. *
  42. * Because various devices have different requirements in terms of
  43. * synchronization and migration strategies, implementing that is left up to
  44. * the driver, and all that the general API provides should be generic --
  45. * allocating objects, reading/writing data with the cpu, freeing objects.
  46. * Even there, platform-dependent optimizations for reading/writing data with
  47. * the CPU mean we'll likely hook those out to driver-specific calls. However,
  48. * the DRI2 implementation wants to have at least allocate/mmap be generic.
  49. *
  50. * The goal was to have swap-backed object allocation managed through
  51. * struct file. However, file descriptors as handles to a struct file have
  52. * two major failings:
  53. * - Process limits prevent more than 1024 or so being used at a time by
  54. * default.
  55. * - Inability to allocate high fds will aggravate the X Server's select()
  56. * handling, and likely that of many GL client applications as well.
  57. *
  58. * This led to a plan of using our own integer IDs (called handles, following
  59. * DRM terminology) to mimic fds, and implement the fd syscalls we need as
  60. * ioctls. The objects themselves will still include the struct file so
  61. * that we can transition to fds if the required kernel infrastructure shows
  62. * up at a later date, and as our interface with shmfs for memory allocation.
  63. */
  64. /**
  65. * Initialize the GEM device fields
  66. */
  67. int
  68. drm_gem_init(struct drm_device *dev)
  69. {
  70. spin_lock_init(&dev->object_name_lock);
  71. idr_init(&dev->object_name_idr);
  72. atomic_set(&dev->object_count, 0);
  73. atomic_set(&dev->object_memory, 0);
  74. atomic_set(&dev->pin_count, 0);
  75. atomic_set(&dev->pin_memory, 0);
  76. atomic_set(&dev->gtt_count, 0);
  77. atomic_set(&dev->gtt_memory, 0);
  78. return 0;
  79. }
  80. /**
  81. * Allocate a GEM object of the specified size with shmfs backing store
  82. */
  83. struct drm_gem_object *
  84. drm_gem_object_alloc(struct drm_device *dev, size_t size)
  85. {
  86. struct drm_gem_object *obj;
  87. BUG_ON((size & (PAGE_SIZE - 1)) != 0);
  88. obj = kcalloc(1, sizeof(*obj), GFP_KERNEL);
  89. obj->dev = dev;
  90. obj->filp = shmem_file_setup("drm mm object", size, 0);
  91. if (IS_ERR(obj->filp)) {
  92. kfree(obj);
  93. return NULL;
  94. }
  95. kref_init(&obj->refcount);
  96. kref_init(&obj->handlecount);
  97. obj->size = size;
  98. if (dev->driver->gem_init_object != NULL &&
  99. dev->driver->gem_init_object(obj) != 0) {
  100. fput(obj->filp);
  101. kfree(obj);
  102. return NULL;
  103. }
  104. atomic_inc(&dev->object_count);
  105. atomic_add(obj->size, &dev->object_memory);
  106. return obj;
  107. }
  108. EXPORT_SYMBOL(drm_gem_object_alloc);
  109. /**
  110. * Removes the mapping from handle to filp for this object.
  111. */
  112. static int
  113. drm_gem_handle_delete(struct drm_file *filp, int handle)
  114. {
  115. struct drm_device *dev;
  116. struct drm_gem_object *obj;
  117. /* This is gross. The idr system doesn't let us try a delete and
  118. * return an error code. It just spews if you fail at deleting.
  119. * So, we have to grab a lock around finding the object and then
  120. * doing the delete on it and dropping the refcount, or the user
  121. * could race us to double-decrement the refcount and cause a
  122. * use-after-free later. Given the frequency of our handle lookups,
  123. * we may want to use ida for number allocation and a hash table
  124. * for the pointers, anyway.
  125. */
  126. spin_lock(&filp->table_lock);
  127. /* Check if we currently have a reference on the object */
  128. obj = idr_find(&filp->object_idr, handle);
  129. if (obj == NULL) {
  130. spin_unlock(&filp->table_lock);
  131. return -EINVAL;
  132. }
  133. dev = obj->dev;
  134. /* Release reference and decrement refcount. */
  135. idr_remove(&filp->object_idr, handle);
  136. spin_unlock(&filp->table_lock);
  137. mutex_lock(&dev->struct_mutex);
  138. drm_gem_object_handle_unreference(obj);
  139. mutex_unlock(&dev->struct_mutex);
  140. return 0;
  141. }
  142. /**
  143. * Create a handle for this object. This adds a handle reference
  144. * to the object, which includes a regular reference count. Callers
  145. * will likely want to dereference the object afterwards.
  146. */
  147. int
  148. drm_gem_handle_create(struct drm_file *file_priv,
  149. struct drm_gem_object *obj,
  150. int *handlep)
  151. {
  152. int ret;
  153. /*
  154. * Get the user-visible handle using idr.
  155. */
  156. again:
  157. /* ensure there is space available to allocate a handle */
  158. if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
  159. return -ENOMEM;
  160. /* do the allocation under our spinlock */
  161. spin_lock(&file_priv->table_lock);
  162. ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep);
  163. spin_unlock(&file_priv->table_lock);
  164. if (ret == -EAGAIN)
  165. goto again;
  166. if (ret != 0)
  167. return ret;
  168. drm_gem_object_handle_reference(obj);
  169. return 0;
  170. }
  171. EXPORT_SYMBOL(drm_gem_handle_create);
  172. /** Returns a reference to the object named by the handle. */
  173. struct drm_gem_object *
  174. drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
  175. int handle)
  176. {
  177. struct drm_gem_object *obj;
  178. spin_lock(&filp->table_lock);
  179. /* Check if we currently have a reference on the object */
  180. obj = idr_find(&filp->object_idr, handle);
  181. if (obj == NULL) {
  182. spin_unlock(&filp->table_lock);
  183. return NULL;
  184. }
  185. drm_gem_object_reference(obj);
  186. spin_unlock(&filp->table_lock);
  187. return obj;
  188. }
  189. EXPORT_SYMBOL(drm_gem_object_lookup);
  190. /**
  191. * Releases the handle to an mm object.
  192. */
  193. int
  194. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  195. struct drm_file *file_priv)
  196. {
  197. struct drm_gem_close *args = data;
  198. int ret;
  199. if (!(dev->driver->driver_features & DRIVER_GEM))
  200. return -ENODEV;
  201. ret = drm_gem_handle_delete(file_priv, args->handle);
  202. return ret;
  203. }
  204. /**
  205. * Create a global name for an object, returning the name.
  206. *
  207. * Note that the name does not hold a reference; when the object
  208. * is freed, the name goes away.
  209. */
  210. int
  211. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  212. struct drm_file *file_priv)
  213. {
  214. struct drm_gem_flink *args = data;
  215. struct drm_gem_object *obj;
  216. int ret;
  217. if (!(dev->driver->driver_features & DRIVER_GEM))
  218. return -ENODEV;
  219. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  220. if (obj == NULL)
  221. return -EBADF;
  222. again:
  223. if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0)
  224. return -ENOMEM;
  225. spin_lock(&dev->object_name_lock);
  226. if (obj->name) {
  227. args->name = obj->name;
  228. spin_unlock(&dev->object_name_lock);
  229. return 0;
  230. }
  231. ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
  232. &obj->name);
  233. spin_unlock(&dev->object_name_lock);
  234. if (ret == -EAGAIN)
  235. goto again;
  236. if (ret != 0) {
  237. mutex_lock(&dev->struct_mutex);
  238. drm_gem_object_unreference(obj);
  239. mutex_unlock(&dev->struct_mutex);
  240. return ret;
  241. }
  242. /*
  243. * Leave the reference from the lookup around as the
  244. * name table now holds one
  245. */
  246. args->name = (uint64_t) obj->name;
  247. return 0;
  248. }
  249. /**
  250. * Open an object using the global name, returning a handle and the size.
  251. *
  252. * This handle (of course) holds a reference to the object, so the object
  253. * will not go away until the handle is deleted.
  254. */
  255. int
  256. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  257. struct drm_file *file_priv)
  258. {
  259. struct drm_gem_open *args = data;
  260. struct drm_gem_object *obj;
  261. int ret;
  262. int handle;
  263. if (!(dev->driver->driver_features & DRIVER_GEM))
  264. return -ENODEV;
  265. spin_lock(&dev->object_name_lock);
  266. obj = idr_find(&dev->object_name_idr, (int) args->name);
  267. if (obj)
  268. drm_gem_object_reference(obj);
  269. spin_unlock(&dev->object_name_lock);
  270. if (!obj)
  271. return -ENOENT;
  272. ret = drm_gem_handle_create(file_priv, obj, &handle);
  273. mutex_lock(&dev->struct_mutex);
  274. drm_gem_object_unreference(obj);
  275. mutex_unlock(&dev->struct_mutex);
  276. if (ret)
  277. return ret;
  278. args->handle = handle;
  279. args->size = obj->size;
  280. return 0;
  281. }
  282. /**
  283. * Called at device open time, sets up the structure for handling refcounting
  284. * of mm objects.
  285. */
  286. void
  287. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  288. {
  289. idr_init(&file_private->object_idr);
  290. spin_lock_init(&file_private->table_lock);
  291. }
  292. /**
  293. * Called at device close to release the file's
  294. * handle references on objects.
  295. */
  296. static int
  297. drm_gem_object_release_handle(int id, void *ptr, void *data)
  298. {
  299. struct drm_gem_object *obj = ptr;
  300. drm_gem_object_handle_unreference(obj);
  301. return 0;
  302. }
  303. /**
  304. * Called at close time when the filp is going away.
  305. *
  306. * Releases any remaining references on objects by this filp.
  307. */
  308. void
  309. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  310. {
  311. mutex_lock(&dev->struct_mutex);
  312. idr_for_each(&file_private->object_idr,
  313. &drm_gem_object_release_handle, NULL);
  314. idr_destroy(&file_private->object_idr);
  315. mutex_unlock(&dev->struct_mutex);
  316. }
  317. /**
  318. * Called after the last reference to the object has been lost.
  319. *
  320. * Frees the object
  321. */
  322. void
  323. drm_gem_object_free(struct kref *kref)
  324. {
  325. struct drm_gem_object *obj = (struct drm_gem_object *) kref;
  326. struct drm_device *dev = obj->dev;
  327. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  328. if (dev->driver->gem_free_object != NULL)
  329. dev->driver->gem_free_object(obj);
  330. fput(obj->filp);
  331. atomic_dec(&dev->object_count);
  332. atomic_sub(obj->size, &dev->object_memory);
  333. kfree(obj);
  334. }
  335. EXPORT_SYMBOL(drm_gem_object_free);
  336. /**
  337. * Called after the last handle to the object has been closed
  338. *
  339. * Removes any name for the object. Note that this must be
  340. * called before drm_gem_object_free or we'll be touching
  341. * freed memory
  342. */
  343. void
  344. drm_gem_object_handle_free(struct kref *kref)
  345. {
  346. struct drm_gem_object *obj = container_of(kref,
  347. struct drm_gem_object,
  348. handlecount);
  349. struct drm_device *dev = obj->dev;
  350. /* Remove any name for this object */
  351. spin_lock(&dev->object_name_lock);
  352. if (obj->name) {
  353. idr_remove(&dev->object_name_idr, obj->name);
  354. spin_unlock(&dev->object_name_lock);
  355. /*
  356. * The object name held a reference to this object, drop
  357. * that now.
  358. */
  359. drm_gem_object_unreference(obj);
  360. } else
  361. spin_unlock(&dev->object_name_lock);
  362. }
  363. EXPORT_SYMBOL(drm_gem_object_handle_free);