drm_gem.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 "drmP.h"
  38. /** @file drm_gem.c
  39. *
  40. * This file provides some of the base ioctls and library routines for
  41. * the graphics memory manager implemented by each device driver.
  42. *
  43. * Because various devices have different requirements in terms of
  44. * synchronization and migration strategies, implementing that is left up to
  45. * the driver, and all that the general API provides should be generic --
  46. * allocating objects, reading/writing data with the cpu, freeing objects.
  47. * Even there, platform-dependent optimizations for reading/writing data with
  48. * the CPU mean we'll likely hook those out to driver-specific calls. However,
  49. * the DRI2 implementation wants to have at least allocate/mmap be generic.
  50. *
  51. * The goal was to have swap-backed object allocation managed through
  52. * struct file. However, file descriptors as handles to a struct file have
  53. * two major failings:
  54. * - Process limits prevent more than 1024 or so being used at a time by
  55. * default.
  56. * - Inability to allocate high fds will aggravate the X Server's select()
  57. * handling, and likely that of many GL client applications as well.
  58. *
  59. * This led to a plan of using our own integer IDs (called handles, following
  60. * DRM terminology) to mimic fds, and implement the fd syscalls we need as
  61. * ioctls. The objects themselves will still include the struct file so
  62. * that we can transition to fds if the required kernel infrastructure shows
  63. * up at a later date, and as our interface with shmfs for memory allocation.
  64. */
  65. /*
  66. * We make up offsets for buffer objects so we can recognize them at
  67. * mmap time.
  68. */
  69. /* pgoff in mmap is an unsigned long, so we need to make sure that
  70. * the faked up offset will fit
  71. */
  72. #if BITS_PER_LONG == 64
  73. #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
  74. #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
  75. #else
  76. #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
  77. #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
  78. #endif
  79. /**
  80. * Initialize the GEM device fields
  81. */
  82. int
  83. drm_gem_init(struct drm_device *dev)
  84. {
  85. struct drm_gem_mm *mm;
  86. spin_lock_init(&dev->object_name_lock);
  87. idr_init(&dev->object_name_idr);
  88. mm = kzalloc(sizeof(struct drm_gem_mm), GFP_KERNEL);
  89. if (!mm) {
  90. DRM_ERROR("out of memory\n");
  91. return -ENOMEM;
  92. }
  93. dev->mm_private = mm;
  94. if (drm_ht_create(&mm->offset_hash, 12)) {
  95. kfree(mm);
  96. return -ENOMEM;
  97. }
  98. if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START,
  99. DRM_FILE_PAGE_OFFSET_SIZE)) {
  100. drm_ht_remove(&mm->offset_hash);
  101. kfree(mm);
  102. return -ENOMEM;
  103. }
  104. return 0;
  105. }
  106. void
  107. drm_gem_destroy(struct drm_device *dev)
  108. {
  109. struct drm_gem_mm *mm = dev->mm_private;
  110. drm_mm_takedown(&mm->offset_manager);
  111. drm_ht_remove(&mm->offset_hash);
  112. kfree(mm);
  113. dev->mm_private = NULL;
  114. }
  115. /**
  116. * Initialize an already allocate GEM object of the specified size with
  117. * shmfs backing store.
  118. */
  119. int drm_gem_object_init(struct drm_device *dev,
  120. struct drm_gem_object *obj, size_t size)
  121. {
  122. BUG_ON((size & (PAGE_SIZE - 1)) != 0);
  123. obj->dev = dev;
  124. obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
  125. if (IS_ERR(obj->filp))
  126. return -ENOMEM;
  127. kref_init(&obj->refcount);
  128. atomic_set(&obj->handle_count, 0);
  129. obj->size = size;
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(drm_gem_object_init);
  133. /**
  134. * Allocate a GEM object of the specified size with shmfs backing store
  135. */
  136. struct drm_gem_object *
  137. drm_gem_object_alloc(struct drm_device *dev, size_t size)
  138. {
  139. struct drm_gem_object *obj;
  140. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  141. if (!obj)
  142. goto free;
  143. if (drm_gem_object_init(dev, obj, size) != 0)
  144. goto free;
  145. if (dev->driver->gem_init_object != NULL &&
  146. dev->driver->gem_init_object(obj) != 0) {
  147. goto fput;
  148. }
  149. return obj;
  150. fput:
  151. /* Object_init mangles the global counters - readjust them. */
  152. fput(obj->filp);
  153. free:
  154. kfree(obj);
  155. return NULL;
  156. }
  157. EXPORT_SYMBOL(drm_gem_object_alloc);
  158. /**
  159. * Removes the mapping from handle to filp for this object.
  160. */
  161. int
  162. drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  163. {
  164. struct drm_device *dev;
  165. struct drm_gem_object *obj;
  166. /* This is gross. The idr system doesn't let us try a delete and
  167. * return an error code. It just spews if you fail at deleting.
  168. * So, we have to grab a lock around finding the object and then
  169. * doing the delete on it and dropping the refcount, or the user
  170. * could race us to double-decrement the refcount and cause a
  171. * use-after-free later. Given the frequency of our handle lookups,
  172. * we may want to use ida for number allocation and a hash table
  173. * for the pointers, anyway.
  174. */
  175. spin_lock(&filp->table_lock);
  176. /* Check if we currently have a reference on the object */
  177. obj = idr_find(&filp->object_idr, handle);
  178. if (obj == NULL) {
  179. spin_unlock(&filp->table_lock);
  180. return -EINVAL;
  181. }
  182. dev = obj->dev;
  183. /* Release reference and decrement refcount. */
  184. idr_remove(&filp->object_idr, handle);
  185. spin_unlock(&filp->table_lock);
  186. if (dev->driver->gem_close_object)
  187. dev->driver->gem_close_object(obj, filp);
  188. drm_gem_object_handle_unreference_unlocked(obj);
  189. return 0;
  190. }
  191. EXPORT_SYMBOL(drm_gem_handle_delete);
  192. /**
  193. * Create a handle for this object. This adds a handle reference
  194. * to the object, which includes a regular reference count. Callers
  195. * will likely want to dereference the object afterwards.
  196. */
  197. int
  198. drm_gem_handle_create(struct drm_file *file_priv,
  199. struct drm_gem_object *obj,
  200. u32 *handlep)
  201. {
  202. struct drm_device *dev = obj->dev;
  203. int ret;
  204. /*
  205. * Get the user-visible handle using idr.
  206. */
  207. again:
  208. /* ensure there is space available to allocate a handle */
  209. if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
  210. return -ENOMEM;
  211. /* do the allocation under our spinlock */
  212. spin_lock(&file_priv->table_lock);
  213. ret = idr_get_new_above(&file_priv->object_idr, obj, 1, (int *)handlep);
  214. spin_unlock(&file_priv->table_lock);
  215. if (ret == -EAGAIN)
  216. goto again;
  217. if (ret != 0)
  218. return ret;
  219. drm_gem_object_handle_reference(obj);
  220. if (dev->driver->gem_open_object) {
  221. ret = dev->driver->gem_open_object(obj, file_priv);
  222. if (ret) {
  223. drm_gem_handle_delete(file_priv, *handlep);
  224. return ret;
  225. }
  226. }
  227. return 0;
  228. }
  229. EXPORT_SYMBOL(drm_gem_handle_create);
  230. /** Returns a reference to the object named by the handle. */
  231. struct drm_gem_object *
  232. drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
  233. u32 handle)
  234. {
  235. struct drm_gem_object *obj;
  236. spin_lock(&filp->table_lock);
  237. /* Check if we currently have a reference on the object */
  238. obj = idr_find(&filp->object_idr, handle);
  239. if (obj == NULL) {
  240. spin_unlock(&filp->table_lock);
  241. return NULL;
  242. }
  243. drm_gem_object_reference(obj);
  244. spin_unlock(&filp->table_lock);
  245. return obj;
  246. }
  247. EXPORT_SYMBOL(drm_gem_object_lookup);
  248. /**
  249. * Releases the handle to an mm object.
  250. */
  251. int
  252. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  253. struct drm_file *file_priv)
  254. {
  255. struct drm_gem_close *args = data;
  256. int ret;
  257. if (!(dev->driver->driver_features & DRIVER_GEM))
  258. return -ENODEV;
  259. ret = drm_gem_handle_delete(file_priv, args->handle);
  260. return ret;
  261. }
  262. /**
  263. * Create a global name for an object, returning the name.
  264. *
  265. * Note that the name does not hold a reference; when the object
  266. * is freed, the name goes away.
  267. */
  268. int
  269. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  270. struct drm_file *file_priv)
  271. {
  272. struct drm_gem_flink *args = data;
  273. struct drm_gem_object *obj;
  274. int ret;
  275. if (!(dev->driver->driver_features & DRIVER_GEM))
  276. return -ENODEV;
  277. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  278. if (obj == NULL)
  279. return -ENOENT;
  280. again:
  281. if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) {
  282. ret = -ENOMEM;
  283. goto err;
  284. }
  285. spin_lock(&dev->object_name_lock);
  286. if (!obj->name) {
  287. ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
  288. &obj->name);
  289. args->name = (uint64_t) obj->name;
  290. spin_unlock(&dev->object_name_lock);
  291. if (ret == -EAGAIN)
  292. goto again;
  293. if (ret != 0)
  294. goto err;
  295. /* Allocate a reference for the name table. */
  296. drm_gem_object_reference(obj);
  297. } else {
  298. args->name = (uint64_t) obj->name;
  299. spin_unlock(&dev->object_name_lock);
  300. ret = 0;
  301. }
  302. err:
  303. drm_gem_object_unreference_unlocked(obj);
  304. return ret;
  305. }
  306. /**
  307. * Open an object using the global name, returning a handle and the size.
  308. *
  309. * This handle (of course) holds a reference to the object, so the object
  310. * will not go away until the handle is deleted.
  311. */
  312. int
  313. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  314. struct drm_file *file_priv)
  315. {
  316. struct drm_gem_open *args = data;
  317. struct drm_gem_object *obj;
  318. int ret;
  319. u32 handle;
  320. if (!(dev->driver->driver_features & DRIVER_GEM))
  321. return -ENODEV;
  322. spin_lock(&dev->object_name_lock);
  323. obj = idr_find(&dev->object_name_idr, (int) args->name);
  324. if (obj)
  325. drm_gem_object_reference(obj);
  326. spin_unlock(&dev->object_name_lock);
  327. if (!obj)
  328. return -ENOENT;
  329. ret = drm_gem_handle_create(file_priv, obj, &handle);
  330. drm_gem_object_unreference_unlocked(obj);
  331. if (ret)
  332. return ret;
  333. args->handle = handle;
  334. args->size = obj->size;
  335. return 0;
  336. }
  337. /**
  338. * Called at device open time, sets up the structure for handling refcounting
  339. * of mm objects.
  340. */
  341. void
  342. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  343. {
  344. idr_init(&file_private->object_idr);
  345. spin_lock_init(&file_private->table_lock);
  346. }
  347. /**
  348. * Called at device close to release the file's
  349. * handle references on objects.
  350. */
  351. static int
  352. drm_gem_object_release_handle(int id, void *ptr, void *data)
  353. {
  354. struct drm_file *file_priv = data;
  355. struct drm_gem_object *obj = ptr;
  356. struct drm_device *dev = obj->dev;
  357. if (dev->driver->gem_close_object)
  358. dev->driver->gem_close_object(obj, file_priv);
  359. drm_gem_object_handle_unreference_unlocked(obj);
  360. return 0;
  361. }
  362. /**
  363. * Called at close time when the filp is going away.
  364. *
  365. * Releases any remaining references on objects by this filp.
  366. */
  367. void
  368. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  369. {
  370. idr_for_each(&file_private->object_idr,
  371. &drm_gem_object_release_handle, file_private);
  372. idr_remove_all(&file_private->object_idr);
  373. idr_destroy(&file_private->object_idr);
  374. }
  375. void
  376. drm_gem_object_release(struct drm_gem_object *obj)
  377. {
  378. fput(obj->filp);
  379. }
  380. EXPORT_SYMBOL(drm_gem_object_release);
  381. /**
  382. * Called after the last reference to the object has been lost.
  383. * Must be called holding struct_ mutex
  384. *
  385. * Frees the object
  386. */
  387. void
  388. drm_gem_object_free(struct kref *kref)
  389. {
  390. struct drm_gem_object *obj = (struct drm_gem_object *) kref;
  391. struct drm_device *dev = obj->dev;
  392. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  393. if (dev->driver->gem_free_object != NULL)
  394. dev->driver->gem_free_object(obj);
  395. }
  396. EXPORT_SYMBOL(drm_gem_object_free);
  397. static void drm_gem_object_ref_bug(struct kref *list_kref)
  398. {
  399. BUG();
  400. }
  401. /**
  402. * Called after the last handle to the object has been closed
  403. *
  404. * Removes any name for the object. Note that this must be
  405. * called before drm_gem_object_free or we'll be touching
  406. * freed memory
  407. */
  408. void drm_gem_object_handle_free(struct drm_gem_object *obj)
  409. {
  410. struct drm_device *dev = obj->dev;
  411. /* Remove any name for this object */
  412. spin_lock(&dev->object_name_lock);
  413. if (obj->name) {
  414. idr_remove(&dev->object_name_idr, obj->name);
  415. obj->name = 0;
  416. spin_unlock(&dev->object_name_lock);
  417. /*
  418. * The object name held a reference to this object, drop
  419. * that now.
  420. *
  421. * This cannot be the last reference, since the handle holds one too.
  422. */
  423. kref_put(&obj->refcount, drm_gem_object_ref_bug);
  424. } else
  425. spin_unlock(&dev->object_name_lock);
  426. }
  427. EXPORT_SYMBOL(drm_gem_object_handle_free);
  428. void drm_gem_vm_open(struct vm_area_struct *vma)
  429. {
  430. struct drm_gem_object *obj = vma->vm_private_data;
  431. drm_gem_object_reference(obj);
  432. mutex_lock(&obj->dev->struct_mutex);
  433. drm_vm_open_locked(vma);
  434. mutex_unlock(&obj->dev->struct_mutex);
  435. }
  436. EXPORT_SYMBOL(drm_gem_vm_open);
  437. void drm_gem_vm_close(struct vm_area_struct *vma)
  438. {
  439. struct drm_gem_object *obj = vma->vm_private_data;
  440. struct drm_device *dev = obj->dev;
  441. mutex_lock(&dev->struct_mutex);
  442. drm_vm_close_locked(vma);
  443. drm_gem_object_unreference(obj);
  444. mutex_unlock(&dev->struct_mutex);
  445. }
  446. EXPORT_SYMBOL(drm_gem_vm_close);
  447. /**
  448. * drm_gem_mmap - memory map routine for GEM objects
  449. * @filp: DRM file pointer
  450. * @vma: VMA for the area to be mapped
  451. *
  452. * If a driver supports GEM object mapping, mmap calls on the DRM file
  453. * descriptor will end up here.
  454. *
  455. * If we find the object based on the offset passed in (vma->vm_pgoff will
  456. * contain the fake offset we created when the GTT map ioctl was called on
  457. * the object), we set up the driver fault handler so that any accesses
  458. * to the object can be trapped, to perform migration, GTT binding, surface
  459. * register allocation, or performance monitoring.
  460. */
  461. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  462. {
  463. struct drm_file *priv = filp->private_data;
  464. struct drm_device *dev = priv->minor->dev;
  465. struct drm_gem_mm *mm = dev->mm_private;
  466. struct drm_local_map *map = NULL;
  467. struct drm_gem_object *obj;
  468. struct drm_hash_item *hash;
  469. int ret = 0;
  470. mutex_lock(&dev->struct_mutex);
  471. if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) {
  472. mutex_unlock(&dev->struct_mutex);
  473. return drm_mmap(filp, vma);
  474. }
  475. map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
  476. if (!map ||
  477. ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
  478. ret = -EPERM;
  479. goto out_unlock;
  480. }
  481. /* Check for valid size. */
  482. if (map->size < vma->vm_end - vma->vm_start) {
  483. ret = -EINVAL;
  484. goto out_unlock;
  485. }
  486. obj = map->handle;
  487. if (!obj->dev->driver->gem_vm_ops) {
  488. ret = -EINVAL;
  489. goto out_unlock;
  490. }
  491. vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND;
  492. vma->vm_ops = obj->dev->driver->gem_vm_ops;
  493. vma->vm_private_data = map->handle;
  494. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  495. /* Take a ref for this mapping of the object, so that the fault
  496. * handler can dereference the mmap offset's pointer to the object.
  497. * This reference is cleaned up by the corresponding vm_close
  498. * (which should happen whether the vma was created by this call, or
  499. * by a vm_open due to mremap or partial unmap or whatever).
  500. */
  501. drm_gem_object_reference(obj);
  502. vma->vm_file = filp; /* Needed for drm_vm_open() */
  503. drm_vm_open_locked(vma);
  504. out_unlock:
  505. mutex_unlock(&dev->struct_mutex);
  506. return ret;
  507. }
  508. EXPORT_SYMBOL(drm_gem_mmap);