ttm_object.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2009-2013 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. *
  30. * While no substantial code is shared, the prime code is inspired by
  31. * drm_prime.c, with
  32. * Authors:
  33. * Dave Airlie <airlied@redhat.com>
  34. * Rob Clark <rob.clark@linaro.org>
  35. */
  36. /** @file ttm_ref_object.c
  37. *
  38. * Base- and reference object implementation for the various
  39. * ttm objects. Implements reference counting, minimal security checks
  40. * and release on file close.
  41. */
  42. /**
  43. * struct ttm_object_file
  44. *
  45. * @tdev: Pointer to the ttm_object_device.
  46. *
  47. * @lock: Lock that protects the ref_list list and the
  48. * ref_hash hash tables.
  49. *
  50. * @ref_list: List of ttm_ref_objects to be destroyed at
  51. * file release.
  52. *
  53. * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
  54. * for fast lookup of ref objects given a base object.
  55. */
  56. #define pr_fmt(fmt) "[TTM] " fmt
  57. #include <drm/ttm/ttm_object.h>
  58. #include <drm/ttm/ttm_module.h>
  59. #include <linux/list.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/slab.h>
  62. #include <linux/module.h>
  63. #include <linux/atomic.h>
  64. struct ttm_object_file {
  65. struct ttm_object_device *tdev;
  66. rwlock_t lock;
  67. struct list_head ref_list;
  68. struct drm_open_hash ref_hash[TTM_REF_NUM];
  69. struct kref refcount;
  70. };
  71. /**
  72. * struct ttm_object_device
  73. *
  74. * @object_lock: lock that protects the object_hash hash table.
  75. *
  76. * @object_hash: hash table for fast lookup of object global names.
  77. *
  78. * @object_count: Per device object count.
  79. *
  80. * This is the per-device data structure needed for ttm object management.
  81. */
  82. struct ttm_object_device {
  83. spinlock_t object_lock;
  84. struct drm_open_hash object_hash;
  85. atomic_t object_count;
  86. struct ttm_mem_global *mem_glob;
  87. struct dma_buf_ops ops;
  88. void (*dmabuf_release)(struct dma_buf *dma_buf);
  89. size_t dma_buf_size;
  90. };
  91. /**
  92. * struct ttm_ref_object
  93. *
  94. * @hash: Hash entry for the per-file object reference hash.
  95. *
  96. * @head: List entry for the per-file list of ref-objects.
  97. *
  98. * @kref: Ref count.
  99. *
  100. * @obj: Base object this ref object is referencing.
  101. *
  102. * @ref_type: Type of ref object.
  103. *
  104. * This is similar to an idr object, but it also has a hash table entry
  105. * that allows lookup with a pointer to the referenced object as a key. In
  106. * that way, one can easily detect whether a base object is referenced by
  107. * a particular ttm_object_file. It also carries a ref count to avoid creating
  108. * multiple ref objects if a ttm_object_file references the same base
  109. * object more than once.
  110. */
  111. struct ttm_ref_object {
  112. struct drm_hash_item hash;
  113. struct list_head head;
  114. struct kref kref;
  115. enum ttm_ref_type ref_type;
  116. struct ttm_base_object *obj;
  117. struct ttm_object_file *tfile;
  118. };
  119. static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf);
  120. static inline struct ttm_object_file *
  121. ttm_object_file_ref(struct ttm_object_file *tfile)
  122. {
  123. kref_get(&tfile->refcount);
  124. return tfile;
  125. }
  126. static void ttm_object_file_destroy(struct kref *kref)
  127. {
  128. struct ttm_object_file *tfile =
  129. container_of(kref, struct ttm_object_file, refcount);
  130. kfree(tfile);
  131. }
  132. static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
  133. {
  134. struct ttm_object_file *tfile = *p_tfile;
  135. *p_tfile = NULL;
  136. kref_put(&tfile->refcount, ttm_object_file_destroy);
  137. }
  138. int ttm_base_object_init(struct ttm_object_file *tfile,
  139. struct ttm_base_object *base,
  140. bool shareable,
  141. enum ttm_object_type object_type,
  142. void (*refcount_release) (struct ttm_base_object **),
  143. void (*ref_obj_release) (struct ttm_base_object *,
  144. enum ttm_ref_type ref_type))
  145. {
  146. struct ttm_object_device *tdev = tfile->tdev;
  147. int ret;
  148. base->shareable = shareable;
  149. base->tfile = ttm_object_file_ref(tfile);
  150. base->refcount_release = refcount_release;
  151. base->ref_obj_release = ref_obj_release;
  152. base->object_type = object_type;
  153. kref_init(&base->refcount);
  154. spin_lock(&tdev->object_lock);
  155. ret = drm_ht_just_insert_please_rcu(&tdev->object_hash,
  156. &base->hash,
  157. (unsigned long)base, 31, 0, 0);
  158. spin_unlock(&tdev->object_lock);
  159. if (unlikely(ret != 0))
  160. goto out_err0;
  161. ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
  162. if (unlikely(ret != 0))
  163. goto out_err1;
  164. ttm_base_object_unref(&base);
  165. return 0;
  166. out_err1:
  167. spin_lock(&tdev->object_lock);
  168. (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
  169. spin_unlock(&tdev->object_lock);
  170. out_err0:
  171. return ret;
  172. }
  173. EXPORT_SYMBOL(ttm_base_object_init);
  174. static void ttm_release_base(struct kref *kref)
  175. {
  176. struct ttm_base_object *base =
  177. container_of(kref, struct ttm_base_object, refcount);
  178. struct ttm_object_device *tdev = base->tfile->tdev;
  179. spin_lock(&tdev->object_lock);
  180. (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
  181. spin_unlock(&tdev->object_lock);
  182. /*
  183. * Note: We don't use synchronize_rcu() here because it's far
  184. * too slow. It's up to the user to free the object using
  185. * call_rcu() or ttm_base_object_kfree().
  186. */
  187. if (base->refcount_release) {
  188. ttm_object_file_unref(&base->tfile);
  189. base->refcount_release(&base);
  190. }
  191. }
  192. void ttm_base_object_unref(struct ttm_base_object **p_base)
  193. {
  194. struct ttm_base_object *base = *p_base;
  195. *p_base = NULL;
  196. kref_put(&base->refcount, ttm_release_base);
  197. }
  198. EXPORT_SYMBOL(ttm_base_object_unref);
  199. struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
  200. uint32_t key)
  201. {
  202. struct ttm_object_device *tdev = tfile->tdev;
  203. struct ttm_base_object *uninitialized_var(base);
  204. struct drm_hash_item *hash;
  205. int ret;
  206. rcu_read_lock();
  207. ret = drm_ht_find_item_rcu(&tdev->object_hash, key, &hash);
  208. if (likely(ret == 0)) {
  209. base = drm_hash_entry(hash, struct ttm_base_object, hash);
  210. ret = kref_get_unless_zero(&base->refcount) ? 0 : -EINVAL;
  211. }
  212. rcu_read_unlock();
  213. if (unlikely(ret != 0))
  214. return NULL;
  215. if (tfile != base->tfile && !base->shareable) {
  216. pr_err("Attempted access of non-shareable object\n");
  217. ttm_base_object_unref(&base);
  218. return NULL;
  219. }
  220. return base;
  221. }
  222. EXPORT_SYMBOL(ttm_base_object_lookup);
  223. int ttm_ref_object_add(struct ttm_object_file *tfile,
  224. struct ttm_base_object *base,
  225. enum ttm_ref_type ref_type, bool *existed)
  226. {
  227. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  228. struct ttm_ref_object *ref;
  229. struct drm_hash_item *hash;
  230. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  231. int ret = -EINVAL;
  232. if (existed != NULL)
  233. *existed = true;
  234. while (ret == -EINVAL) {
  235. read_lock(&tfile->lock);
  236. ret = drm_ht_find_item(ht, base->hash.key, &hash);
  237. if (ret == 0) {
  238. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  239. kref_get(&ref->kref);
  240. read_unlock(&tfile->lock);
  241. break;
  242. }
  243. read_unlock(&tfile->lock);
  244. ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
  245. false, false);
  246. if (unlikely(ret != 0))
  247. return ret;
  248. ref = kmalloc(sizeof(*ref), GFP_KERNEL);
  249. if (unlikely(ref == NULL)) {
  250. ttm_mem_global_free(mem_glob, sizeof(*ref));
  251. return -ENOMEM;
  252. }
  253. ref->hash.key = base->hash.key;
  254. ref->obj = base;
  255. ref->tfile = tfile;
  256. ref->ref_type = ref_type;
  257. kref_init(&ref->kref);
  258. write_lock(&tfile->lock);
  259. ret = drm_ht_insert_item(ht, &ref->hash);
  260. if (likely(ret == 0)) {
  261. list_add_tail(&ref->head, &tfile->ref_list);
  262. kref_get(&base->refcount);
  263. write_unlock(&tfile->lock);
  264. if (existed != NULL)
  265. *existed = false;
  266. break;
  267. }
  268. write_unlock(&tfile->lock);
  269. BUG_ON(ret != -EINVAL);
  270. ttm_mem_global_free(mem_glob, sizeof(*ref));
  271. kfree(ref);
  272. }
  273. return ret;
  274. }
  275. EXPORT_SYMBOL(ttm_ref_object_add);
  276. static void ttm_ref_object_release(struct kref *kref)
  277. {
  278. struct ttm_ref_object *ref =
  279. container_of(kref, struct ttm_ref_object, kref);
  280. struct ttm_base_object *base = ref->obj;
  281. struct ttm_object_file *tfile = ref->tfile;
  282. struct drm_open_hash *ht;
  283. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  284. ht = &tfile->ref_hash[ref->ref_type];
  285. (void)drm_ht_remove_item(ht, &ref->hash);
  286. list_del(&ref->head);
  287. write_unlock(&tfile->lock);
  288. if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
  289. base->ref_obj_release(base, ref->ref_type);
  290. ttm_base_object_unref(&ref->obj);
  291. ttm_mem_global_free(mem_glob, sizeof(*ref));
  292. kfree(ref);
  293. write_lock(&tfile->lock);
  294. }
  295. int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
  296. unsigned long key, enum ttm_ref_type ref_type)
  297. {
  298. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  299. struct ttm_ref_object *ref;
  300. struct drm_hash_item *hash;
  301. int ret;
  302. write_lock(&tfile->lock);
  303. ret = drm_ht_find_item(ht, key, &hash);
  304. if (unlikely(ret != 0)) {
  305. write_unlock(&tfile->lock);
  306. return -EINVAL;
  307. }
  308. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  309. kref_put(&ref->kref, ttm_ref_object_release);
  310. write_unlock(&tfile->lock);
  311. return 0;
  312. }
  313. EXPORT_SYMBOL(ttm_ref_object_base_unref);
  314. void ttm_object_file_release(struct ttm_object_file **p_tfile)
  315. {
  316. struct ttm_ref_object *ref;
  317. struct list_head *list;
  318. unsigned int i;
  319. struct ttm_object_file *tfile = *p_tfile;
  320. *p_tfile = NULL;
  321. write_lock(&tfile->lock);
  322. /*
  323. * Since we release the lock within the loop, we have to
  324. * restart it from the beginning each time.
  325. */
  326. while (!list_empty(&tfile->ref_list)) {
  327. list = tfile->ref_list.next;
  328. ref = list_entry(list, struct ttm_ref_object, head);
  329. ttm_ref_object_release(&ref->kref);
  330. }
  331. for (i = 0; i < TTM_REF_NUM; ++i)
  332. drm_ht_remove(&tfile->ref_hash[i]);
  333. write_unlock(&tfile->lock);
  334. ttm_object_file_unref(&tfile);
  335. }
  336. EXPORT_SYMBOL(ttm_object_file_release);
  337. struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
  338. unsigned int hash_order)
  339. {
  340. struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  341. unsigned int i;
  342. unsigned int j = 0;
  343. int ret;
  344. if (unlikely(tfile == NULL))
  345. return NULL;
  346. rwlock_init(&tfile->lock);
  347. tfile->tdev = tdev;
  348. kref_init(&tfile->refcount);
  349. INIT_LIST_HEAD(&tfile->ref_list);
  350. for (i = 0; i < TTM_REF_NUM; ++i) {
  351. ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
  352. if (ret) {
  353. j = i;
  354. goto out_err;
  355. }
  356. }
  357. return tfile;
  358. out_err:
  359. for (i = 0; i < j; ++i)
  360. drm_ht_remove(&tfile->ref_hash[i]);
  361. kfree(tfile);
  362. return NULL;
  363. }
  364. EXPORT_SYMBOL(ttm_object_file_init);
  365. struct ttm_object_device *
  366. ttm_object_device_init(struct ttm_mem_global *mem_glob,
  367. unsigned int hash_order,
  368. const struct dma_buf_ops *ops)
  369. {
  370. struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
  371. int ret;
  372. if (unlikely(tdev == NULL))
  373. return NULL;
  374. tdev->mem_glob = mem_glob;
  375. spin_lock_init(&tdev->object_lock);
  376. atomic_set(&tdev->object_count, 0);
  377. ret = drm_ht_create(&tdev->object_hash, hash_order);
  378. if (ret != 0)
  379. goto out_no_object_hash;
  380. tdev->ops = *ops;
  381. tdev->dmabuf_release = tdev->ops.release;
  382. tdev->ops.release = ttm_prime_dmabuf_release;
  383. tdev->dma_buf_size = ttm_round_pot(sizeof(struct dma_buf)) +
  384. ttm_round_pot(sizeof(struct file));
  385. return tdev;
  386. out_no_object_hash:
  387. kfree(tdev);
  388. return NULL;
  389. }
  390. EXPORT_SYMBOL(ttm_object_device_init);
  391. void ttm_object_device_release(struct ttm_object_device **p_tdev)
  392. {
  393. struct ttm_object_device *tdev = *p_tdev;
  394. *p_tdev = NULL;
  395. spin_lock(&tdev->object_lock);
  396. drm_ht_remove(&tdev->object_hash);
  397. spin_unlock(&tdev->object_lock);
  398. kfree(tdev);
  399. }
  400. EXPORT_SYMBOL(ttm_object_device_release);
  401. /**
  402. * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
  403. *
  404. * @dma_buf: Non-refcounted pointer to a struct dma-buf.
  405. *
  406. * Obtain a file reference from a lookup structure that doesn't refcount
  407. * the file, but synchronizes with its release method to make sure it has
  408. * not been freed yet. See for example kref_get_unless_zero documentation.
  409. * Returns true if refcounting succeeds, false otherwise.
  410. *
  411. * Nobody really wants this as a public API yet, so let it mature here
  412. * for some time...
  413. */
  414. static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
  415. {
  416. return atomic_long_inc_not_zero(&dmabuf->file->f_count) != 0L;
  417. }
  418. /**
  419. * ttm_prime_refcount_release - refcount release method for a prime object.
  420. *
  421. * @p_base: Pointer to ttm_base_object pointer.
  422. *
  423. * This is a wrapper that calls the refcount_release founction of the
  424. * underlying object. At the same time it cleans up the prime object.
  425. * This function is called when all references to the base object we
  426. * derive from are gone.
  427. */
  428. static void ttm_prime_refcount_release(struct ttm_base_object **p_base)
  429. {
  430. struct ttm_base_object *base = *p_base;
  431. struct ttm_prime_object *prime;
  432. *p_base = NULL;
  433. prime = container_of(base, struct ttm_prime_object, base);
  434. BUG_ON(prime->dma_buf != NULL);
  435. mutex_destroy(&prime->mutex);
  436. if (prime->refcount_release)
  437. prime->refcount_release(&base);
  438. }
  439. /**
  440. * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
  441. *
  442. * @dma_buf:
  443. *
  444. * This function first calls the dma_buf release method the driver
  445. * provides. Then it cleans up our dma_buf pointer used for lookup,
  446. * and finally releases the reference the dma_buf has on our base
  447. * object.
  448. */
  449. static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf)
  450. {
  451. struct ttm_prime_object *prime =
  452. (struct ttm_prime_object *) dma_buf->priv;
  453. struct ttm_base_object *base = &prime->base;
  454. struct ttm_object_device *tdev = base->tfile->tdev;
  455. if (tdev->dmabuf_release)
  456. tdev->dmabuf_release(dma_buf);
  457. mutex_lock(&prime->mutex);
  458. if (prime->dma_buf == dma_buf)
  459. prime->dma_buf = NULL;
  460. mutex_unlock(&prime->mutex);
  461. ttm_mem_global_free(tdev->mem_glob, tdev->dma_buf_size);
  462. ttm_base_object_unref(&base);
  463. }
  464. /**
  465. * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
  466. *
  467. * @tfile: A struct ttm_object_file identifying the caller.
  468. * @fd: The prime / dmabuf fd.
  469. * @handle: The returned handle.
  470. *
  471. * This function returns a handle to an object that previously exported
  472. * a dma-buf. Note that we don't handle imports yet, because we simply
  473. * have no consumers of that implementation.
  474. */
  475. int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
  476. int fd, u32 *handle)
  477. {
  478. struct ttm_object_device *tdev = tfile->tdev;
  479. struct dma_buf *dma_buf;
  480. struct ttm_prime_object *prime;
  481. struct ttm_base_object *base;
  482. int ret;
  483. dma_buf = dma_buf_get(fd);
  484. if (IS_ERR(dma_buf))
  485. return PTR_ERR(dma_buf);
  486. if (dma_buf->ops != &tdev->ops)
  487. return -ENOSYS;
  488. prime = (struct ttm_prime_object *) dma_buf->priv;
  489. base = &prime->base;
  490. *handle = base->hash.key;
  491. ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
  492. dma_buf_put(dma_buf);
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(ttm_prime_fd_to_handle);
  496. /**
  497. * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
  498. *
  499. * @tfile: Struct ttm_object_file identifying the caller.
  500. * @handle: Handle to the object we're exporting from.
  501. * @flags: flags for dma-buf creation. We just pass them on.
  502. * @prime_fd: The returned file descriptor.
  503. *
  504. */
  505. int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
  506. uint32_t handle, uint32_t flags,
  507. int *prime_fd)
  508. {
  509. struct ttm_object_device *tdev = tfile->tdev;
  510. struct ttm_base_object *base;
  511. struct dma_buf *dma_buf;
  512. struct ttm_prime_object *prime;
  513. int ret;
  514. base = ttm_base_object_lookup(tfile, handle);
  515. if (unlikely(base == NULL ||
  516. base->object_type != ttm_prime_type)) {
  517. ret = -ENOENT;
  518. goto out_unref;
  519. }
  520. prime = container_of(base, struct ttm_prime_object, base);
  521. if (unlikely(!base->shareable)) {
  522. ret = -EPERM;
  523. goto out_unref;
  524. }
  525. ret = mutex_lock_interruptible(&prime->mutex);
  526. if (unlikely(ret != 0)) {
  527. ret = -ERESTARTSYS;
  528. goto out_unref;
  529. }
  530. dma_buf = prime->dma_buf;
  531. if (!dma_buf || !get_dma_buf_unless_doomed(dma_buf)) {
  532. /*
  533. * Need to create a new dma_buf, with memory accounting.
  534. */
  535. ret = ttm_mem_global_alloc(tdev->mem_glob, tdev->dma_buf_size,
  536. false, true);
  537. if (unlikely(ret != 0)) {
  538. mutex_unlock(&prime->mutex);
  539. goto out_unref;
  540. }
  541. dma_buf = dma_buf_export(prime, &tdev->ops,
  542. prime->size, flags);
  543. if (IS_ERR(dma_buf)) {
  544. ret = PTR_ERR(dma_buf);
  545. ttm_mem_global_free(tdev->mem_glob,
  546. tdev->dma_buf_size);
  547. mutex_unlock(&prime->mutex);
  548. goto out_unref;
  549. }
  550. /*
  551. * dma_buf has taken the base object reference
  552. */
  553. base = NULL;
  554. prime->dma_buf = dma_buf;
  555. }
  556. mutex_unlock(&prime->mutex);
  557. ret = dma_buf_fd(dma_buf, flags);
  558. if (ret >= 0) {
  559. *prime_fd = ret;
  560. ret = 0;
  561. } else
  562. dma_buf_put(dma_buf);
  563. out_unref:
  564. if (base)
  565. ttm_base_object_unref(&base);
  566. return ret;
  567. }
  568. EXPORT_SYMBOL_GPL(ttm_prime_handle_to_fd);
  569. /**
  570. * ttm_prime_object_init - Initialize a ttm_prime_object
  571. *
  572. * @tfile: struct ttm_object_file identifying the caller
  573. * @size: The size of the dma_bufs we export.
  574. * @prime: The object to be initialized.
  575. * @shareable: See ttm_base_object_init
  576. * @type: See ttm_base_object_init
  577. * @refcount_release: See ttm_base_object_init
  578. * @ref_obj_release: See ttm_base_object_init
  579. *
  580. * Initializes an object which is compatible with the drm_prime model
  581. * for data sharing between processes and devices.
  582. */
  583. int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size,
  584. struct ttm_prime_object *prime, bool shareable,
  585. enum ttm_object_type type,
  586. void (*refcount_release) (struct ttm_base_object **),
  587. void (*ref_obj_release) (struct ttm_base_object *,
  588. enum ttm_ref_type ref_type))
  589. {
  590. mutex_init(&prime->mutex);
  591. prime->size = PAGE_ALIGN(size);
  592. prime->real_type = type;
  593. prime->dma_buf = NULL;
  594. prime->refcount_release = refcount_release;
  595. return ttm_base_object_init(tfile, &prime->base, shareable,
  596. ttm_prime_type,
  597. ttm_prime_refcount_release,
  598. ref_obj_release);
  599. }
  600. EXPORT_SYMBOL(ttm_prime_object_init);