drm_gem.c 15 KB

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