drm_gem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. * We make up offsets for buffer objects so we can recognize them at
  66. * mmap time.
  67. */
  68. #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
  69. #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
  70. /**
  71. * Initialize the GEM device fields
  72. */
  73. int
  74. drm_gem_init(struct drm_device *dev)
  75. {
  76. struct drm_gem_mm *mm;
  77. spin_lock_init(&dev->object_name_lock);
  78. idr_init(&dev->object_name_idr);
  79. atomic_set(&dev->object_count, 0);
  80. atomic_set(&dev->object_memory, 0);
  81. atomic_set(&dev->pin_count, 0);
  82. atomic_set(&dev->pin_memory, 0);
  83. atomic_set(&dev->gtt_count, 0);
  84. atomic_set(&dev->gtt_memory, 0);
  85. mm = drm_calloc(1, sizeof(struct drm_gem_mm), DRM_MEM_MM);
  86. if (!mm) {
  87. DRM_ERROR("out of memory\n");
  88. return -ENOMEM;
  89. }
  90. dev->mm_private = mm;
  91. if (drm_ht_create(&mm->offset_hash, 19)) {
  92. drm_free(mm, sizeof(struct drm_gem_mm), DRM_MEM_MM);
  93. return -ENOMEM;
  94. }
  95. if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
  96. DRM_FILE_PAGE_OFFSET_SIZE)) {
  97. drm_free(mm, sizeof(struct drm_gem_mm), DRM_MEM_MM);
  98. drm_ht_remove(&mm->offset_hash);
  99. return -ENOMEM;
  100. }
  101. return 0;
  102. }
  103. void
  104. drm_gem_destroy(struct drm_device *dev)
  105. {
  106. struct drm_gem_mm *mm = dev->mm_private;
  107. drm_mm_takedown(&mm->offset_manager);
  108. drm_ht_remove(&mm->offset_hash);
  109. drm_free(mm, sizeof(struct drm_gem_mm), DRM_MEM_MM);
  110. dev->mm_private = NULL;
  111. }
  112. /**
  113. * Allocate a GEM object of the specified size with shmfs backing store
  114. */
  115. struct drm_gem_object *
  116. drm_gem_object_alloc(struct drm_device *dev, size_t size)
  117. {
  118. struct drm_gem_object *obj;
  119. BUG_ON((size & (PAGE_SIZE - 1)) != 0);
  120. obj = kcalloc(1, sizeof(*obj), GFP_KERNEL);
  121. obj->dev = dev;
  122. obj->filp = shmem_file_setup("drm mm object", size, 0);
  123. if (IS_ERR(obj->filp)) {
  124. kfree(obj);
  125. return NULL;
  126. }
  127. kref_init(&obj->refcount);
  128. kref_init(&obj->handlecount);
  129. obj->size = size;
  130. if (dev->driver->gem_init_object != NULL &&
  131. dev->driver->gem_init_object(obj) != 0) {
  132. fput(obj->filp);
  133. kfree(obj);
  134. return NULL;
  135. }
  136. atomic_inc(&dev->object_count);
  137. atomic_add(obj->size, &dev->object_memory);
  138. return obj;
  139. }
  140. EXPORT_SYMBOL(drm_gem_object_alloc);
  141. /**
  142. * Removes the mapping from handle to filp for this object.
  143. */
  144. static int
  145. drm_gem_handle_delete(struct drm_file *filp, int handle)
  146. {
  147. struct drm_device *dev;
  148. struct drm_gem_object *obj;
  149. /* This is gross. The idr system doesn't let us try a delete and
  150. * return an error code. It just spews if you fail at deleting.
  151. * So, we have to grab a lock around finding the object and then
  152. * doing the delete on it and dropping the refcount, or the user
  153. * could race us to double-decrement the refcount and cause a
  154. * use-after-free later. Given the frequency of our handle lookups,
  155. * we may want to use ida for number allocation and a hash table
  156. * for the pointers, anyway.
  157. */
  158. spin_lock(&filp->table_lock);
  159. /* Check if we currently have a reference on the object */
  160. obj = idr_find(&filp->object_idr, handle);
  161. if (obj == NULL) {
  162. spin_unlock(&filp->table_lock);
  163. return -EINVAL;
  164. }
  165. dev = obj->dev;
  166. /* Release reference and decrement refcount. */
  167. idr_remove(&filp->object_idr, handle);
  168. spin_unlock(&filp->table_lock);
  169. mutex_lock(&dev->struct_mutex);
  170. drm_gem_object_handle_unreference(obj);
  171. mutex_unlock(&dev->struct_mutex);
  172. return 0;
  173. }
  174. /**
  175. * Create a handle for this object. This adds a handle reference
  176. * to the object, which includes a regular reference count. Callers
  177. * will likely want to dereference the object afterwards.
  178. */
  179. int
  180. drm_gem_handle_create(struct drm_file *file_priv,
  181. struct drm_gem_object *obj,
  182. int *handlep)
  183. {
  184. int ret;
  185. /*
  186. * Get the user-visible handle using idr.
  187. */
  188. again:
  189. /* ensure there is space available to allocate a handle */
  190. if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
  191. return -ENOMEM;
  192. /* do the allocation under our spinlock */
  193. spin_lock(&file_priv->table_lock);
  194. ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep);
  195. spin_unlock(&file_priv->table_lock);
  196. if (ret == -EAGAIN)
  197. goto again;
  198. if (ret != 0)
  199. return ret;
  200. drm_gem_object_handle_reference(obj);
  201. return 0;
  202. }
  203. EXPORT_SYMBOL(drm_gem_handle_create);
  204. /** Returns a reference to the object named by the handle. */
  205. struct drm_gem_object *
  206. drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
  207. int handle)
  208. {
  209. struct drm_gem_object *obj;
  210. spin_lock(&filp->table_lock);
  211. /* Check if we currently have a reference on the object */
  212. obj = idr_find(&filp->object_idr, handle);
  213. if (obj == NULL) {
  214. spin_unlock(&filp->table_lock);
  215. return NULL;
  216. }
  217. drm_gem_object_reference(obj);
  218. spin_unlock(&filp->table_lock);
  219. return obj;
  220. }
  221. EXPORT_SYMBOL(drm_gem_object_lookup);
  222. /**
  223. * Releases the handle to an mm object.
  224. */
  225. int
  226. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  227. struct drm_file *file_priv)
  228. {
  229. struct drm_gem_close *args = data;
  230. int ret;
  231. if (!(dev->driver->driver_features & DRIVER_GEM))
  232. return -ENODEV;
  233. ret = drm_gem_handle_delete(file_priv, args->handle);
  234. return ret;
  235. }
  236. /**
  237. * Create a global name for an object, returning the name.
  238. *
  239. * Note that the name does not hold a reference; when the object
  240. * is freed, the name goes away.
  241. */
  242. int
  243. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  244. struct drm_file *file_priv)
  245. {
  246. struct drm_gem_flink *args = data;
  247. struct drm_gem_object *obj;
  248. int ret;
  249. if (!(dev->driver->driver_features & DRIVER_GEM))
  250. return -ENODEV;
  251. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  252. if (obj == NULL)
  253. return -EBADF;
  254. again:
  255. if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0)
  256. return -ENOMEM;
  257. spin_lock(&dev->object_name_lock);
  258. if (obj->name) {
  259. args->name = obj->name;
  260. spin_unlock(&dev->object_name_lock);
  261. return 0;
  262. }
  263. ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
  264. &obj->name);
  265. spin_unlock(&dev->object_name_lock);
  266. if (ret == -EAGAIN)
  267. goto again;
  268. if (ret != 0) {
  269. mutex_lock(&dev->struct_mutex);
  270. drm_gem_object_unreference(obj);
  271. mutex_unlock(&dev->struct_mutex);
  272. return ret;
  273. }
  274. /*
  275. * Leave the reference from the lookup around as the
  276. * name table now holds one
  277. */
  278. args->name = (uint64_t) obj->name;
  279. return 0;
  280. }
  281. /**
  282. * Open an object using the global name, returning a handle and the size.
  283. *
  284. * This handle (of course) holds a reference to the object, so the object
  285. * will not go away until the handle is deleted.
  286. */
  287. int
  288. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  289. struct drm_file *file_priv)
  290. {
  291. struct drm_gem_open *args = data;
  292. struct drm_gem_object *obj;
  293. int ret;
  294. int handle;
  295. if (!(dev->driver->driver_features & DRIVER_GEM))
  296. return -ENODEV;
  297. spin_lock(&dev->object_name_lock);
  298. obj = idr_find(&dev->object_name_idr, (int) args->name);
  299. if (obj)
  300. drm_gem_object_reference(obj);
  301. spin_unlock(&dev->object_name_lock);
  302. if (!obj)
  303. return -ENOENT;
  304. ret = drm_gem_handle_create(file_priv, obj, &handle);
  305. mutex_lock(&dev->struct_mutex);
  306. drm_gem_object_unreference(obj);
  307. mutex_unlock(&dev->struct_mutex);
  308. if (ret)
  309. return ret;
  310. args->handle = handle;
  311. args->size = obj->size;
  312. return 0;
  313. }
  314. /**
  315. * Called at device open time, sets up the structure for handling refcounting
  316. * of mm objects.
  317. */
  318. void
  319. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  320. {
  321. idr_init(&file_private->object_idr);
  322. spin_lock_init(&file_private->table_lock);
  323. }
  324. /**
  325. * Called at device close to release the file's
  326. * handle references on objects.
  327. */
  328. static int
  329. drm_gem_object_release_handle(int id, void *ptr, void *data)
  330. {
  331. struct drm_gem_object *obj = ptr;
  332. drm_gem_object_handle_unreference(obj);
  333. return 0;
  334. }
  335. /**
  336. * Called at close time when the filp is going away.
  337. *
  338. * Releases any remaining references on objects by this filp.
  339. */
  340. void
  341. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  342. {
  343. mutex_lock(&dev->struct_mutex);
  344. idr_for_each(&file_private->object_idr,
  345. &drm_gem_object_release_handle, NULL);
  346. idr_destroy(&file_private->object_idr);
  347. mutex_unlock(&dev->struct_mutex);
  348. }
  349. /**
  350. * Called after the last reference to the object has been lost.
  351. *
  352. * Frees the object
  353. */
  354. void
  355. drm_gem_object_free(struct kref *kref)
  356. {
  357. struct drm_gem_object *obj = (struct drm_gem_object *) kref;
  358. struct drm_device *dev = obj->dev;
  359. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  360. if (dev->driver->gem_free_object != NULL)
  361. dev->driver->gem_free_object(obj);
  362. fput(obj->filp);
  363. atomic_dec(&dev->object_count);
  364. atomic_sub(obj->size, &dev->object_memory);
  365. kfree(obj);
  366. }
  367. EXPORT_SYMBOL(drm_gem_object_free);
  368. /**
  369. * Called after the last handle to the object has been closed
  370. *
  371. * Removes any name for the object. Note that this must be
  372. * called before drm_gem_object_free or we'll be touching
  373. * freed memory
  374. */
  375. void
  376. drm_gem_object_handle_free(struct kref *kref)
  377. {
  378. struct drm_gem_object *obj = container_of(kref,
  379. struct drm_gem_object,
  380. handlecount);
  381. struct drm_device *dev = obj->dev;
  382. /* Remove any name for this object */
  383. spin_lock(&dev->object_name_lock);
  384. if (obj->name) {
  385. idr_remove(&dev->object_name_idr, obj->name);
  386. spin_unlock(&dev->object_name_lock);
  387. /*
  388. * The object name held a reference to this object, drop
  389. * that now.
  390. */
  391. drm_gem_object_unreference(obj);
  392. } else
  393. spin_unlock(&dev->object_name_lock);
  394. }
  395. EXPORT_SYMBOL(drm_gem_object_handle_free);
  396. /**
  397. * drm_gem_mmap - memory map routine for GEM objects
  398. * @filp: DRM file pointer
  399. * @vma: VMA for the area to be mapped
  400. *
  401. * If a driver supports GEM object mapping, mmap calls on the DRM file
  402. * descriptor will end up here.
  403. *
  404. * If we find the object based on the offset passed in (vma->vm_pgoff will
  405. * contain the fake offset we created when the GTT map ioctl was called on
  406. * the object), we set up the driver fault handler so that any accesses
  407. * to the object can be trapped, to perform migration, GTT binding, surface
  408. * register allocation, or performance monitoring.
  409. */
  410. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  411. {
  412. struct drm_file *priv = filp->private_data;
  413. struct drm_device *dev = priv->minor->dev;
  414. struct drm_gem_mm *mm = dev->mm_private;
  415. struct drm_map *map = NULL;
  416. struct drm_gem_object *obj;
  417. struct drm_hash_item *hash;
  418. unsigned long prot;
  419. int ret = 0;
  420. mutex_lock(&dev->struct_mutex);
  421. if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
  422. mutex_unlock(&dev->struct_mutex);
  423. return drm_mmap(filp, vma);
  424. }
  425. map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
  426. if (!map ||
  427. ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
  428. ret = -EPERM;
  429. goto out_unlock;
  430. }
  431. /* Check for valid size. */
  432. if (map->size < vma->vm_end - vma->vm_start) {
  433. ret = -EINVAL;
  434. goto out_unlock;
  435. }
  436. obj = map->handle;
  437. if (!obj->dev->driver->gem_vm_ops) {
  438. ret = -EINVAL;
  439. goto out_unlock;
  440. }
  441. vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
  442. vma->vm_ops = obj->dev->driver->gem_vm_ops;
  443. vma->vm_private_data = map->handle;
  444. /* FIXME: use pgprot_writecombine when available */
  445. prot = pgprot_val(vma->vm_page_prot);
  446. #ifdef CONFIG_X86
  447. prot |= _PAGE_CACHE_WC;
  448. #endif
  449. vma->vm_page_prot = __pgprot(prot);
  450. vma->vm_file = filp; /* Needed for drm_vm_open() */
  451. drm_vm_open_locked(vma);
  452. out_unlock:
  453. mutex_unlock(&dev->struct_mutex);
  454. return ret;
  455. }
  456. EXPORT_SYMBOL(drm_gem_mmap);