ttm_object.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. spinlock_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 rcu_head rcu_head;
  113. struct drm_hash_item hash;
  114. struct list_head head;
  115. struct kref kref;
  116. enum ttm_ref_type ref_type;
  117. struct ttm_base_object *obj;
  118. struct ttm_object_file *tfile;
  119. };
  120. static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf);
  121. static inline struct ttm_object_file *
  122. ttm_object_file_ref(struct ttm_object_file *tfile)
  123. {
  124. kref_get(&tfile->refcount);
  125. return tfile;
  126. }
  127. static void ttm_object_file_destroy(struct kref *kref)
  128. {
  129. struct ttm_object_file *tfile =
  130. container_of(kref, struct ttm_object_file, refcount);
  131. kfree(tfile);
  132. }
  133. static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
  134. {
  135. struct ttm_object_file *tfile = *p_tfile;
  136. *p_tfile = NULL;
  137. kref_put(&tfile->refcount, ttm_object_file_destroy);
  138. }
  139. int ttm_base_object_init(struct ttm_object_file *tfile,
  140. struct ttm_base_object *base,
  141. bool shareable,
  142. enum ttm_object_type object_type,
  143. void (*refcount_release) (struct ttm_base_object **),
  144. void (*ref_obj_release) (struct ttm_base_object *,
  145. enum ttm_ref_type ref_type))
  146. {
  147. struct ttm_object_device *tdev = tfile->tdev;
  148. int ret;
  149. base->shareable = shareable;
  150. base->tfile = ttm_object_file_ref(tfile);
  151. base->refcount_release = refcount_release;
  152. base->ref_obj_release = ref_obj_release;
  153. base->object_type = object_type;
  154. kref_init(&base->refcount);
  155. spin_lock(&tdev->object_lock);
  156. ret = drm_ht_just_insert_please_rcu(&tdev->object_hash,
  157. &base->hash,
  158. (unsigned long)base, 31, 0, 0);
  159. spin_unlock(&tdev->object_lock);
  160. if (unlikely(ret != 0))
  161. goto out_err0;
  162. ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
  163. if (unlikely(ret != 0))
  164. goto out_err1;
  165. ttm_base_object_unref(&base);
  166. return 0;
  167. out_err1:
  168. spin_lock(&tdev->object_lock);
  169. (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
  170. spin_unlock(&tdev->object_lock);
  171. out_err0:
  172. return ret;
  173. }
  174. EXPORT_SYMBOL(ttm_base_object_init);
  175. static void ttm_release_base(struct kref *kref)
  176. {
  177. struct ttm_base_object *base =
  178. container_of(kref, struct ttm_base_object, refcount);
  179. struct ttm_object_device *tdev = base->tfile->tdev;
  180. spin_lock(&tdev->object_lock);
  181. (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
  182. spin_unlock(&tdev->object_lock);
  183. /*
  184. * Note: We don't use synchronize_rcu() here because it's far
  185. * too slow. It's up to the user to free the object using
  186. * call_rcu() or ttm_base_object_kfree().
  187. */
  188. ttm_object_file_unref(&base->tfile);
  189. if (base->refcount_release)
  190. base->refcount_release(&base);
  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_base_object *base = NULL;
  203. struct drm_hash_item *hash;
  204. struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
  205. int ret;
  206. rcu_read_lock();
  207. ret = drm_ht_find_item_rcu(ht, key, &hash);
  208. if (likely(ret == 0)) {
  209. base = drm_hash_entry(hash, struct ttm_ref_object, hash)->obj;
  210. if (!kref_get_unless_zero(&base->refcount))
  211. base = NULL;
  212. }
  213. rcu_read_unlock();
  214. return base;
  215. }
  216. EXPORT_SYMBOL(ttm_base_object_lookup);
  217. struct ttm_base_object *
  218. ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key)
  219. {
  220. struct ttm_base_object *base = NULL;
  221. struct drm_hash_item *hash;
  222. struct drm_open_hash *ht = &tdev->object_hash;
  223. int ret;
  224. rcu_read_lock();
  225. ret = drm_ht_find_item_rcu(ht, key, &hash);
  226. if (likely(ret == 0)) {
  227. base = drm_hash_entry(hash, struct ttm_base_object, hash);
  228. if (!kref_get_unless_zero(&base->refcount))
  229. base = NULL;
  230. }
  231. rcu_read_unlock();
  232. return base;
  233. }
  234. EXPORT_SYMBOL(ttm_base_object_lookup_for_ref);
  235. /**
  236. * ttm_ref_object_exists - Check whether a caller has a valid ref object
  237. * (has opened) a base object.
  238. *
  239. * @tfile: Pointer to a struct ttm_object_file identifying the caller.
  240. * @base: Pointer to a struct base object.
  241. *
  242. * Checks wether the caller identified by @tfile has put a valid USAGE
  243. * reference object on the base object identified by @base.
  244. */
  245. bool ttm_ref_object_exists(struct ttm_object_file *tfile,
  246. struct ttm_base_object *base)
  247. {
  248. struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
  249. struct drm_hash_item *hash;
  250. struct ttm_ref_object *ref;
  251. rcu_read_lock();
  252. if (unlikely(drm_ht_find_item_rcu(ht, base->hash.key, &hash) != 0))
  253. goto out_false;
  254. /*
  255. * Verify that the ref object is really pointing to our base object.
  256. * Our base object could actually be dead, and the ref object pointing
  257. * to another base object with the same handle.
  258. */
  259. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  260. if (unlikely(base != ref->obj))
  261. goto out_false;
  262. /*
  263. * Verify that the ref->obj pointer was actually valid!
  264. */
  265. rmb();
  266. if (unlikely(atomic_read(&ref->kref.refcount) == 0))
  267. goto out_false;
  268. rcu_read_unlock();
  269. return true;
  270. out_false:
  271. rcu_read_unlock();
  272. return false;
  273. }
  274. EXPORT_SYMBOL(ttm_ref_object_exists);
  275. int ttm_ref_object_add(struct ttm_object_file *tfile,
  276. struct ttm_base_object *base,
  277. enum ttm_ref_type ref_type, bool *existed,
  278. bool require_existed)
  279. {
  280. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  281. struct ttm_ref_object *ref;
  282. struct drm_hash_item *hash;
  283. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  284. int ret = -EINVAL;
  285. if (base->tfile != tfile && !base->shareable)
  286. return -EPERM;
  287. if (existed != NULL)
  288. *existed = true;
  289. while (ret == -EINVAL) {
  290. rcu_read_lock();
  291. ret = drm_ht_find_item_rcu(ht, base->hash.key, &hash);
  292. if (ret == 0) {
  293. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  294. if (kref_get_unless_zero(&ref->kref)) {
  295. rcu_read_unlock();
  296. break;
  297. }
  298. }
  299. rcu_read_unlock();
  300. if (require_existed)
  301. return -EPERM;
  302. ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
  303. false, false);
  304. if (unlikely(ret != 0))
  305. return ret;
  306. ref = kmalloc(sizeof(*ref), GFP_KERNEL);
  307. if (unlikely(ref == NULL)) {
  308. ttm_mem_global_free(mem_glob, sizeof(*ref));
  309. return -ENOMEM;
  310. }
  311. ref->hash.key = base->hash.key;
  312. ref->obj = base;
  313. ref->tfile = tfile;
  314. ref->ref_type = ref_type;
  315. kref_init(&ref->kref);
  316. spin_lock(&tfile->lock);
  317. ret = drm_ht_insert_item_rcu(ht, &ref->hash);
  318. if (likely(ret == 0)) {
  319. list_add_tail(&ref->head, &tfile->ref_list);
  320. kref_get(&base->refcount);
  321. spin_unlock(&tfile->lock);
  322. if (existed != NULL)
  323. *existed = false;
  324. break;
  325. }
  326. spin_unlock(&tfile->lock);
  327. BUG_ON(ret != -EINVAL);
  328. ttm_mem_global_free(mem_glob, sizeof(*ref));
  329. kfree(ref);
  330. }
  331. return ret;
  332. }
  333. EXPORT_SYMBOL(ttm_ref_object_add);
  334. static void ttm_ref_object_release(struct kref *kref)
  335. {
  336. struct ttm_ref_object *ref =
  337. container_of(kref, struct ttm_ref_object, kref);
  338. struct ttm_base_object *base = ref->obj;
  339. struct ttm_object_file *tfile = ref->tfile;
  340. struct drm_open_hash *ht;
  341. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  342. ht = &tfile->ref_hash[ref->ref_type];
  343. (void)drm_ht_remove_item_rcu(ht, &ref->hash);
  344. list_del(&ref->head);
  345. spin_unlock(&tfile->lock);
  346. if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
  347. base->ref_obj_release(base, ref->ref_type);
  348. ttm_base_object_unref(&ref->obj);
  349. ttm_mem_global_free(mem_glob, sizeof(*ref));
  350. kfree_rcu(ref, rcu_head);
  351. spin_lock(&tfile->lock);
  352. }
  353. int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
  354. unsigned long key, enum ttm_ref_type ref_type)
  355. {
  356. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  357. struct ttm_ref_object *ref;
  358. struct drm_hash_item *hash;
  359. int ret;
  360. spin_lock(&tfile->lock);
  361. ret = drm_ht_find_item(ht, key, &hash);
  362. if (unlikely(ret != 0)) {
  363. spin_unlock(&tfile->lock);
  364. return -EINVAL;
  365. }
  366. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  367. kref_put(&ref->kref, ttm_ref_object_release);
  368. spin_unlock(&tfile->lock);
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(ttm_ref_object_base_unref);
  372. void ttm_object_file_release(struct ttm_object_file **p_tfile)
  373. {
  374. struct ttm_ref_object *ref;
  375. struct list_head *list;
  376. unsigned int i;
  377. struct ttm_object_file *tfile = *p_tfile;
  378. *p_tfile = NULL;
  379. spin_lock(&tfile->lock);
  380. /*
  381. * Since we release the lock within the loop, we have to
  382. * restart it from the beginning each time.
  383. */
  384. while (!list_empty(&tfile->ref_list)) {
  385. list = tfile->ref_list.next;
  386. ref = list_entry(list, struct ttm_ref_object, head);
  387. ttm_ref_object_release(&ref->kref);
  388. }
  389. for (i = 0; i < TTM_REF_NUM; ++i)
  390. drm_ht_remove(&tfile->ref_hash[i]);
  391. spin_unlock(&tfile->lock);
  392. ttm_object_file_unref(&tfile);
  393. }
  394. EXPORT_SYMBOL(ttm_object_file_release);
  395. struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
  396. unsigned int hash_order)
  397. {
  398. struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  399. unsigned int i;
  400. unsigned int j = 0;
  401. int ret;
  402. if (unlikely(tfile == NULL))
  403. return NULL;
  404. spin_lock_init(&tfile->lock);
  405. tfile->tdev = tdev;
  406. kref_init(&tfile->refcount);
  407. INIT_LIST_HEAD(&tfile->ref_list);
  408. for (i = 0; i < TTM_REF_NUM; ++i) {
  409. ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
  410. if (ret) {
  411. j = i;
  412. goto out_err;
  413. }
  414. }
  415. return tfile;
  416. out_err:
  417. for (i = 0; i < j; ++i)
  418. drm_ht_remove(&tfile->ref_hash[i]);
  419. kfree(tfile);
  420. return NULL;
  421. }
  422. EXPORT_SYMBOL(ttm_object_file_init);
  423. struct ttm_object_device *
  424. ttm_object_device_init(struct ttm_mem_global *mem_glob,
  425. unsigned int hash_order,
  426. const struct dma_buf_ops *ops)
  427. {
  428. struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
  429. int ret;
  430. if (unlikely(tdev == NULL))
  431. return NULL;
  432. tdev->mem_glob = mem_glob;
  433. spin_lock_init(&tdev->object_lock);
  434. atomic_set(&tdev->object_count, 0);
  435. ret = drm_ht_create(&tdev->object_hash, hash_order);
  436. if (ret != 0)
  437. goto out_no_object_hash;
  438. tdev->ops = *ops;
  439. tdev->dmabuf_release = tdev->ops.release;
  440. tdev->ops.release = ttm_prime_dmabuf_release;
  441. tdev->dma_buf_size = ttm_round_pot(sizeof(struct dma_buf)) +
  442. ttm_round_pot(sizeof(struct file));
  443. return tdev;
  444. out_no_object_hash:
  445. kfree(tdev);
  446. return NULL;
  447. }
  448. EXPORT_SYMBOL(ttm_object_device_init);
  449. void ttm_object_device_release(struct ttm_object_device **p_tdev)
  450. {
  451. struct ttm_object_device *tdev = *p_tdev;
  452. *p_tdev = NULL;
  453. spin_lock(&tdev->object_lock);
  454. drm_ht_remove(&tdev->object_hash);
  455. spin_unlock(&tdev->object_lock);
  456. kfree(tdev);
  457. }
  458. EXPORT_SYMBOL(ttm_object_device_release);
  459. /**
  460. * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
  461. *
  462. * @dma_buf: Non-refcounted pointer to a struct dma-buf.
  463. *
  464. * Obtain a file reference from a lookup structure that doesn't refcount
  465. * the file, but synchronizes with its release method to make sure it has
  466. * not been freed yet. See for example kref_get_unless_zero documentation.
  467. * Returns true if refcounting succeeds, false otherwise.
  468. *
  469. * Nobody really wants this as a public API yet, so let it mature here
  470. * for some time...
  471. */
  472. static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
  473. {
  474. return atomic_long_inc_not_zero(&dmabuf->file->f_count) != 0L;
  475. }
  476. /**
  477. * ttm_prime_refcount_release - refcount release method for a prime object.
  478. *
  479. * @p_base: Pointer to ttm_base_object pointer.
  480. *
  481. * This is a wrapper that calls the refcount_release founction of the
  482. * underlying object. At the same time it cleans up the prime object.
  483. * This function is called when all references to the base object we
  484. * derive from are gone.
  485. */
  486. static void ttm_prime_refcount_release(struct ttm_base_object **p_base)
  487. {
  488. struct ttm_base_object *base = *p_base;
  489. struct ttm_prime_object *prime;
  490. *p_base = NULL;
  491. prime = container_of(base, struct ttm_prime_object, base);
  492. BUG_ON(prime->dma_buf != NULL);
  493. mutex_destroy(&prime->mutex);
  494. if (prime->refcount_release)
  495. prime->refcount_release(&base);
  496. }
  497. /**
  498. * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
  499. *
  500. * @dma_buf:
  501. *
  502. * This function first calls the dma_buf release method the driver
  503. * provides. Then it cleans up our dma_buf pointer used for lookup,
  504. * and finally releases the reference the dma_buf has on our base
  505. * object.
  506. */
  507. static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf)
  508. {
  509. struct ttm_prime_object *prime =
  510. (struct ttm_prime_object *) dma_buf->priv;
  511. struct ttm_base_object *base = &prime->base;
  512. struct ttm_object_device *tdev = base->tfile->tdev;
  513. if (tdev->dmabuf_release)
  514. tdev->dmabuf_release(dma_buf);
  515. mutex_lock(&prime->mutex);
  516. if (prime->dma_buf == dma_buf)
  517. prime->dma_buf = NULL;
  518. mutex_unlock(&prime->mutex);
  519. ttm_mem_global_free(tdev->mem_glob, tdev->dma_buf_size);
  520. ttm_base_object_unref(&base);
  521. }
  522. /**
  523. * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
  524. *
  525. * @tfile: A struct ttm_object_file identifying the caller.
  526. * @fd: The prime / dmabuf fd.
  527. * @handle: The returned handle.
  528. *
  529. * This function returns a handle to an object that previously exported
  530. * a dma-buf. Note that we don't handle imports yet, because we simply
  531. * have no consumers of that implementation.
  532. */
  533. int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
  534. int fd, u32 *handle)
  535. {
  536. struct ttm_object_device *tdev = tfile->tdev;
  537. struct dma_buf *dma_buf;
  538. struct ttm_prime_object *prime;
  539. struct ttm_base_object *base;
  540. int ret;
  541. dma_buf = dma_buf_get(fd);
  542. if (IS_ERR(dma_buf))
  543. return PTR_ERR(dma_buf);
  544. if (dma_buf->ops != &tdev->ops)
  545. return -ENOSYS;
  546. prime = (struct ttm_prime_object *) dma_buf->priv;
  547. base = &prime->base;
  548. *handle = base->hash.key;
  549. ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
  550. dma_buf_put(dma_buf);
  551. return ret;
  552. }
  553. EXPORT_SYMBOL_GPL(ttm_prime_fd_to_handle);
  554. /**
  555. * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
  556. *
  557. * @tfile: Struct ttm_object_file identifying the caller.
  558. * @handle: Handle to the object we're exporting from.
  559. * @flags: flags for dma-buf creation. We just pass them on.
  560. * @prime_fd: The returned file descriptor.
  561. *
  562. */
  563. int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
  564. uint32_t handle, uint32_t flags,
  565. int *prime_fd)
  566. {
  567. struct ttm_object_device *tdev = tfile->tdev;
  568. struct ttm_base_object *base;
  569. struct dma_buf *dma_buf;
  570. struct ttm_prime_object *prime;
  571. int ret;
  572. base = ttm_base_object_lookup(tfile, handle);
  573. if (unlikely(base == NULL ||
  574. base->object_type != ttm_prime_type)) {
  575. ret = -ENOENT;
  576. goto out_unref;
  577. }
  578. prime = container_of(base, struct ttm_prime_object, base);
  579. if (unlikely(!base->shareable)) {
  580. ret = -EPERM;
  581. goto out_unref;
  582. }
  583. ret = mutex_lock_interruptible(&prime->mutex);
  584. if (unlikely(ret != 0)) {
  585. ret = -ERESTARTSYS;
  586. goto out_unref;
  587. }
  588. dma_buf = prime->dma_buf;
  589. if (!dma_buf || !get_dma_buf_unless_doomed(dma_buf)) {
  590. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  591. exp_info.ops = &tdev->ops;
  592. exp_info.size = prime->size;
  593. exp_info.flags = flags;
  594. exp_info.priv = prime;
  595. /*
  596. * Need to create a new dma_buf, with memory accounting.
  597. */
  598. ret = ttm_mem_global_alloc(tdev->mem_glob, tdev->dma_buf_size,
  599. false, true);
  600. if (unlikely(ret != 0)) {
  601. mutex_unlock(&prime->mutex);
  602. goto out_unref;
  603. }
  604. dma_buf = dma_buf_export(&exp_info);
  605. if (IS_ERR(dma_buf)) {
  606. ret = PTR_ERR(dma_buf);
  607. ttm_mem_global_free(tdev->mem_glob,
  608. tdev->dma_buf_size);
  609. mutex_unlock(&prime->mutex);
  610. goto out_unref;
  611. }
  612. /*
  613. * dma_buf has taken the base object reference
  614. */
  615. base = NULL;
  616. prime->dma_buf = dma_buf;
  617. }
  618. mutex_unlock(&prime->mutex);
  619. ret = dma_buf_fd(dma_buf, flags);
  620. if (ret >= 0) {
  621. *prime_fd = ret;
  622. ret = 0;
  623. } else
  624. dma_buf_put(dma_buf);
  625. out_unref:
  626. if (base)
  627. ttm_base_object_unref(&base);
  628. return ret;
  629. }
  630. EXPORT_SYMBOL_GPL(ttm_prime_handle_to_fd);
  631. /**
  632. * ttm_prime_object_init - Initialize a ttm_prime_object
  633. *
  634. * @tfile: struct ttm_object_file identifying the caller
  635. * @size: The size of the dma_bufs we export.
  636. * @prime: The object to be initialized.
  637. * @shareable: See ttm_base_object_init
  638. * @type: See ttm_base_object_init
  639. * @refcount_release: See ttm_base_object_init
  640. * @ref_obj_release: See ttm_base_object_init
  641. *
  642. * Initializes an object which is compatible with the drm_prime model
  643. * for data sharing between processes and devices.
  644. */
  645. int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size,
  646. struct ttm_prime_object *prime, bool shareable,
  647. enum ttm_object_type type,
  648. void (*refcount_release) (struct ttm_base_object **),
  649. void (*ref_obj_release) (struct ttm_base_object *,
  650. enum ttm_ref_type ref_type))
  651. {
  652. mutex_init(&prime->mutex);
  653. prime->size = PAGE_ALIGN(size);
  654. prime->real_type = type;
  655. prime->dma_buf = NULL;
  656. prime->refcount_release = refcount_release;
  657. return ttm_base_object_init(tfile, &prime->base, shareable,
  658. ttm_prime_type,
  659. ttm_prime_refcount_release,
  660. ref_obj_release);
  661. }
  662. EXPORT_SYMBOL(ttm_prime_object_init);