drm_gem.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
  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. kfree(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_ht_remove(&mm->offset_hash);
  98. kfree(mm);
  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. kfree(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 = kzalloc(sizeof(*obj), GFP_KERNEL);
  121. if (!obj)
  122. goto free;
  123. obj->dev = dev;
  124. obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
  125. if (IS_ERR(obj->filp))
  126. goto free;
  127. /* Basically we want to disable the OOM killer and handle ENOMEM
  128. * ourselves by sacrificing pages from cached buffers.
  129. * XXX shmem_file_[gs]et_gfp_mask()
  130. */
  131. mapping_set_gfp_mask(obj->filp->f_path.dentry->d_inode->i_mapping,
  132. GFP_HIGHUSER |
  133. __GFP_COLD |
  134. __GFP_FS |
  135. __GFP_RECLAIMABLE |
  136. __GFP_NORETRY |
  137. __GFP_NOWARN |
  138. __GFP_NOMEMALLOC);
  139. kref_init(&obj->refcount);
  140. kref_init(&obj->handlecount);
  141. obj->size = size;
  142. if (dev->driver->gem_init_object != NULL &&
  143. dev->driver->gem_init_object(obj) != 0) {
  144. goto fput;
  145. }
  146. atomic_inc(&dev->object_count);
  147. atomic_add(obj->size, &dev->object_memory);
  148. return obj;
  149. fput:
  150. fput(obj->filp);
  151. free:
  152. kfree(obj);
  153. return NULL;
  154. }
  155. EXPORT_SYMBOL(drm_gem_object_alloc);
  156. /**
  157. * Removes the mapping from handle to filp for this object.
  158. */
  159. static int
  160. drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  161. {
  162. struct drm_device *dev;
  163. struct drm_gem_object *obj;
  164. /* This is gross. The idr system doesn't let us try a delete and
  165. * return an error code. It just spews if you fail at deleting.
  166. * So, we have to grab a lock around finding the object and then
  167. * doing the delete on it and dropping the refcount, or the user
  168. * could race us to double-decrement the refcount and cause a
  169. * use-after-free later. Given the frequency of our handle lookups,
  170. * we may want to use ida for number allocation and a hash table
  171. * for the pointers, anyway.
  172. */
  173. spin_lock(&filp->table_lock);
  174. /* Check if we currently have a reference on the object */
  175. obj = idr_find(&filp->object_idr, handle);
  176. if (obj == NULL) {
  177. spin_unlock(&filp->table_lock);
  178. return -EINVAL;
  179. }
  180. dev = obj->dev;
  181. /* Release reference and decrement refcount. */
  182. idr_remove(&filp->object_idr, handle);
  183. spin_unlock(&filp->table_lock);
  184. mutex_lock(&dev->struct_mutex);
  185. drm_gem_object_handle_unreference(obj);
  186. mutex_unlock(&dev->struct_mutex);
  187. return 0;
  188. }
  189. /**
  190. * Create a handle for this object. This adds a handle reference
  191. * to the object, which includes a regular reference count. Callers
  192. * will likely want to dereference the object afterwards.
  193. */
  194. int
  195. drm_gem_handle_create(struct drm_file *file_priv,
  196. struct drm_gem_object *obj,
  197. u32 *handlep)
  198. {
  199. int ret;
  200. /*
  201. * Get the user-visible handle using idr.
  202. */
  203. again:
  204. /* ensure there is space available to allocate a handle */
  205. if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
  206. return -ENOMEM;
  207. /* do the allocation under our spinlock */
  208. spin_lock(&file_priv->table_lock);
  209. ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
  210. spin_unlock(&file_priv->table_lock);
  211. if (ret == -EAGAIN)
  212. goto again;
  213. if (ret != 0)
  214. return ret;
  215. drm_gem_object_handle_reference(obj);
  216. return 0;
  217. }
  218. EXPORT_SYMBOL(drm_gem_handle_create);
  219. /** Returns a reference to the object named by the handle. */
  220. struct drm_gem_object *
  221. drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
  222. u32 handle)
  223. {
  224. struct drm_gem_object *obj;
  225. spin_lock(&filp->table_lock);
  226. /* Check if we currently have a reference on the object */
  227. obj = idr_find(&filp->object_idr, handle);
  228. if (obj == NULL) {
  229. spin_unlock(&filp->table_lock);
  230. return NULL;
  231. }
  232. drm_gem_object_reference(obj);
  233. spin_unlock(&filp->table_lock);
  234. return obj;
  235. }
  236. EXPORT_SYMBOL(drm_gem_object_lookup);
  237. /**
  238. * Releases the handle to an mm object.
  239. */
  240. int
  241. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  242. struct drm_file *file_priv)
  243. {
  244. struct drm_gem_close *args = data;
  245. int ret;
  246. if (!(dev->driver->driver_features & DRIVER_GEM))
  247. return -ENODEV;
  248. ret = drm_gem_handle_delete(file_priv, args->handle);
  249. return ret;
  250. }
  251. /**
  252. * Create a global name for an object, returning the name.
  253. *
  254. * Note that the name does not hold a reference; when the object
  255. * is freed, the name goes away.
  256. */
  257. int
  258. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  259. struct drm_file *file_priv)
  260. {
  261. struct drm_gem_flink *args = data;
  262. struct drm_gem_object *obj;
  263. int ret;
  264. if (!(dev->driver->driver_features & DRIVER_GEM))
  265. return -ENODEV;
  266. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  267. if (obj == NULL)
  268. return -EBADF;
  269. again:
  270. if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
  271. ret = -ENOMEM;
  272. goto err;
  273. }
  274. spin_lock(&dev->object_name_lock);
  275. if (!obj->name) {
  276. ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
  277. &obj->name);
  278. args->name = (uint64_t) obj->name;
  279. spin_unlock(&dev->object_name_lock);
  280. if (ret == -EAGAIN)
  281. goto again;
  282. if (ret != 0)
  283. goto err;
  284. /* Allocate a reference for the name table. */
  285. drm_gem_object_reference(obj);
  286. } else {
  287. args->name = (uint64_t) obj->name;
  288. spin_unlock(&dev->object_name_lock);
  289. ret = 0;
  290. }
  291. err:
  292. mutex_lock(&dev->struct_mutex);
  293. drm_gem_object_unreference(obj);
  294. mutex_unlock(&dev->struct_mutex);
  295. return ret;
  296. }
  297. /**
  298. * Open an object using the global name, returning a handle and the size.
  299. *
  300. * This handle (of course) holds a reference to the object, so the object
  301. * will not go away until the handle is deleted.
  302. */
  303. int
  304. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  305. struct drm_file *file_priv)
  306. {
  307. struct drm_gem_open *args = data;
  308. struct drm_gem_object *obj;
  309. int ret;
  310. u32 handle;
  311. if (!(dev->driver->driver_features & DRIVER_GEM))
  312. return -ENODEV;
  313. spin_lock(&dev->object_name_lock);
  314. obj = idr_find(&dev->object_name_idr, (int) args->name);
  315. if (obj)
  316. drm_gem_object_reference(obj);
  317. spin_unlock(&dev->object_name_lock);
  318. if (!obj)
  319. return -ENOENT;
  320. ret = drm_gem_handle_create(file_priv, obj, &handle);
  321. mutex_lock(&dev->struct_mutex);
  322. drm_gem_object_unreference(obj);
  323. mutex_unlock(&dev->struct_mutex);
  324. if (ret)
  325. return ret;
  326. args->handle = handle;
  327. args->size = obj->size;
  328. return 0;
  329. }
  330. /**
  331. * Called at device open time, sets up the structure for handling refcounting
  332. * of mm objects.
  333. */
  334. void
  335. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  336. {
  337. idr_init(&file_private->object_idr);
  338. spin_lock_init(&file_private->table_lock);
  339. }
  340. /**
  341. * Called at device close to release the file's
  342. * handle references on objects.
  343. */
  344. static int
  345. drm_gem_object_release_handle(int id, void *ptr, void *data)
  346. {
  347. struct drm_gem_object *obj = ptr;
  348. drm_gem_object_handle_unreference(obj);
  349. return 0;
  350. }
  351. /**
  352. * Called at close time when the filp is going away.
  353. *
  354. * Releases any remaining references on objects by this filp.
  355. */
  356. void
  357. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  358. {
  359. mutex_lock(&dev->struct_mutex);
  360. idr_for_each(&file_private->object_idr,
  361. &drm_gem_object_release_handle, NULL);
  362. idr_destroy(&file_private->object_idr);
  363. mutex_unlock(&dev->struct_mutex);
  364. }
  365. /**
  366. * Called after the last reference to the object has been lost.
  367. *
  368. * Frees the object
  369. */
  370. void
  371. drm_gem_object_free(struct kref *kref)
  372. {
  373. struct drm_gem_object *obj = (struct drm_gem_object *) kref;
  374. struct drm_device *dev = obj->dev;
  375. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  376. if (dev->driver->gem_free_object != NULL)
  377. dev->driver->gem_free_object(obj);
  378. fput(obj->filp);
  379. atomic_dec(&dev->object_count);
  380. atomic_sub(obj->size, &dev->object_memory);
  381. kfree(obj);
  382. }
  383. EXPORT_SYMBOL(drm_gem_object_free);
  384. /**
  385. * Called after the last handle to the object has been closed
  386. *
  387. * Removes any name for the object. Note that this must be
  388. * called before drm_gem_object_free or we'll be touching
  389. * freed memory
  390. */
  391. void
  392. drm_gem_object_handle_free(struct kref *kref)
  393. {
  394. struct drm_gem_object *obj = container_of(kref,
  395. struct drm_gem_object,
  396. handlecount);
  397. struct drm_device *dev = obj->dev;
  398. /* Remove any name for this object */
  399. spin_lock(&dev->object_name_lock);
  400. if (obj->name) {
  401. idr_remove(&dev->object_name_idr, obj->name);
  402. obj->name = 0;
  403. spin_unlock(&dev->object_name_lock);
  404. /*
  405. * The object name held a reference to this object, drop
  406. * that now.
  407. */
  408. drm_gem_object_unreference(obj);
  409. } else
  410. spin_unlock(&dev->object_name_lock);
  411. }
  412. EXPORT_SYMBOL(drm_gem_object_handle_free);
  413. void drm_gem_vm_open(struct vm_area_struct *vma)
  414. {
  415. struct drm_gem_object *obj = vma->vm_private_data;
  416. drm_gem_object_reference(obj);
  417. }
  418. EXPORT_SYMBOL(drm_gem_vm_open);
  419. void drm_gem_vm_close(struct vm_area_struct *vma)
  420. {
  421. struct drm_gem_object *obj = vma->vm_private_data;
  422. struct drm_device *dev = obj->dev;
  423. mutex_lock(&dev->struct_mutex);
  424. drm_gem_object_unreference(obj);
  425. mutex_unlock(&dev->struct_mutex);
  426. }
  427. EXPORT_SYMBOL(drm_gem_vm_close);
  428. /**
  429. * drm_gem_mmap - memory map routine for GEM objects
  430. * @filp: DRM file pointer
  431. * @vma: VMA for the area to be mapped
  432. *
  433. * If a driver supports GEM object mapping, mmap calls on the DRM file
  434. * descriptor will end up here.
  435. *
  436. * If we find the object based on the offset passed in (vma->vm_pgoff will
  437. * contain the fake offset we created when the GTT map ioctl was called on
  438. * the object), we set up the driver fault handler so that any accesses
  439. * to the object can be trapped, to perform migration, GTT binding, surface
  440. * register allocation, or performance monitoring.
  441. */
  442. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  443. {
  444. struct drm_file *priv = filp->private_data;
  445. struct drm_device *dev = priv->minor->dev;
  446. struct drm_gem_mm *mm = dev->mm_private;
  447. struct drm_local_map *map = NULL;
  448. struct drm_gem_object *obj;
  449. struct drm_hash_item *hash;
  450. int ret = 0;
  451. mutex_lock(&dev->struct_mutex);
  452. if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
  453. mutex_unlock(&dev->struct_mutex);
  454. return drm_mmap(filp, vma);
  455. }
  456. map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
  457. if (!map ||
  458. ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
  459. ret = -EPERM;
  460. goto out_unlock;
  461. }
  462. /* Check for valid size. */
  463. if (map->size < vma->vm_end - vma->vm_start) {
  464. ret = -EINVAL;
  465. goto out_unlock;
  466. }
  467. obj = map->handle;
  468. if (!obj->dev->driver->gem_vm_ops) {
  469. ret = -EINVAL;
  470. goto out_unlock;
  471. }
  472. vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
  473. vma->vm_ops = obj->dev->driver->gem_vm_ops;
  474. vma->vm_private_data = map->handle;
  475. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  476. /* Take a ref for this mapping of the object, so that the fault
  477. * handler can dereference the mmap offset's pointer to the object.
  478. * This reference is cleaned up by the corresponding vm_close
  479. * (which should happen whether the vma was created by this call, or
  480. * by a vm_open due to mremap or partial unmap or whatever).
  481. */
  482. drm_gem_object_reference(obj);
  483. vma->vm_file = filp; /* Needed for drm_vm_open() */
  484. drm_vm_open_locked(vma);
  485. out_unlock:
  486. mutex_unlock(&dev->struct_mutex);
  487. return ret;
  488. }
  489. EXPORT_SYMBOL(drm_gem_mmap);