ttm_object.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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);
  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(kref_read(&ref->kref) == 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. {
  279. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  280. struct ttm_ref_object *ref;
  281. struct drm_hash_item *hash;
  282. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  283. int ret = -EINVAL;
  284. if (base->tfile != tfile && !base->shareable)
  285. return -EPERM;
  286. if (existed != NULL)
  287. *existed = true;
  288. while (ret == -EINVAL) {
  289. rcu_read_lock();
  290. ret = drm_ht_find_item_rcu(ht, base->hash.key, &hash);
  291. if (ret == 0) {
  292. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  293. if (kref_get_unless_zero(&ref->kref)) {
  294. rcu_read_unlock();
  295. break;
  296. }
  297. }
  298. rcu_read_unlock();
  299. ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
  300. false, false);
  301. if (unlikely(ret != 0))
  302. return ret;
  303. ref = kmalloc(sizeof(*ref), GFP_KERNEL);
  304. if (unlikely(ref == NULL)) {
  305. ttm_mem_global_free(mem_glob, sizeof(*ref));
  306. return -ENOMEM;
  307. }
  308. ref->hash.key = base->hash.key;
  309. ref->obj = base;
  310. ref->tfile = tfile;
  311. ref->ref_type = ref_type;
  312. kref_init(&ref->kref);
  313. spin_lock(&tfile->lock);
  314. ret = drm_ht_insert_item_rcu(ht, &ref->hash);
  315. if (likely(ret == 0)) {
  316. list_add_tail(&ref->head, &tfile->ref_list);
  317. kref_get(&base->refcount);
  318. spin_unlock(&tfile->lock);
  319. if (existed != NULL)
  320. *existed = false;
  321. break;
  322. }
  323. spin_unlock(&tfile->lock);
  324. BUG_ON(ret != -EINVAL);
  325. ttm_mem_global_free(mem_glob, sizeof(*ref));
  326. kfree(ref);
  327. }
  328. return ret;
  329. }
  330. EXPORT_SYMBOL(ttm_ref_object_add);
  331. static void ttm_ref_object_release(struct kref *kref)
  332. {
  333. struct ttm_ref_object *ref =
  334. container_of(kref, struct ttm_ref_object, kref);
  335. struct ttm_base_object *base = ref->obj;
  336. struct ttm_object_file *tfile = ref->tfile;
  337. struct drm_open_hash *ht;
  338. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  339. ht = &tfile->ref_hash[ref->ref_type];
  340. (void)drm_ht_remove_item_rcu(ht, &ref->hash);
  341. list_del(&ref->head);
  342. spin_unlock(&tfile->lock);
  343. if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
  344. base->ref_obj_release(base, ref->ref_type);
  345. ttm_base_object_unref(&ref->obj);
  346. ttm_mem_global_free(mem_glob, sizeof(*ref));
  347. kfree_rcu(ref, rcu_head);
  348. spin_lock(&tfile->lock);
  349. }
  350. int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
  351. unsigned long key, enum ttm_ref_type ref_type)
  352. {
  353. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  354. struct ttm_ref_object *ref;
  355. struct drm_hash_item *hash;
  356. int ret;
  357. spin_lock(&tfile->lock);
  358. ret = drm_ht_find_item(ht, key, &hash);
  359. if (unlikely(ret != 0)) {
  360. spin_unlock(&tfile->lock);
  361. return -EINVAL;
  362. }
  363. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  364. kref_put(&ref->kref, ttm_ref_object_release);
  365. spin_unlock(&tfile->lock);
  366. return 0;
  367. }
  368. EXPORT_SYMBOL(ttm_ref_object_base_unref);
  369. void ttm_object_file_release(struct ttm_object_file **p_tfile)
  370. {
  371. struct ttm_ref_object *ref;
  372. struct list_head *list;
  373. unsigned int i;
  374. struct ttm_object_file *tfile = *p_tfile;
  375. *p_tfile = NULL;
  376. spin_lock(&tfile->lock);
  377. /*
  378. * Since we release the lock within the loop, we have to
  379. * restart it from the beginning each time.
  380. */
  381. while (!list_empty(&tfile->ref_list)) {
  382. list = tfile->ref_list.next;
  383. ref = list_entry(list, struct ttm_ref_object, head);
  384. ttm_ref_object_release(&ref->kref);
  385. }
  386. for (i = 0; i < TTM_REF_NUM; ++i)
  387. drm_ht_remove(&tfile->ref_hash[i]);
  388. spin_unlock(&tfile->lock);
  389. ttm_object_file_unref(&tfile);
  390. }
  391. EXPORT_SYMBOL(ttm_object_file_release);
  392. struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
  393. unsigned int hash_order)
  394. {
  395. struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  396. unsigned int i;
  397. unsigned int j = 0;
  398. int ret;
  399. if (unlikely(tfile == NULL))
  400. return NULL;
  401. spin_lock_init(&tfile->lock);
  402. tfile->tdev = tdev;
  403. kref_init(&tfile->refcount);
  404. INIT_LIST_HEAD(&tfile->ref_list);
  405. for (i = 0; i < TTM_REF_NUM; ++i) {
  406. ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
  407. if (ret) {
  408. j = i;
  409. goto out_err;
  410. }
  411. }
  412. return tfile;
  413. out_err:
  414. for (i = 0; i < j; ++i)
  415. drm_ht_remove(&tfile->ref_hash[i]);
  416. kfree(tfile);
  417. return NULL;
  418. }
  419. EXPORT_SYMBOL(ttm_object_file_init);
  420. struct ttm_object_device *
  421. ttm_object_device_init(struct ttm_mem_global *mem_glob,
  422. unsigned int hash_order,
  423. const struct dma_buf_ops *ops)
  424. {
  425. struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
  426. int ret;
  427. if (unlikely(tdev == NULL))
  428. return NULL;
  429. tdev->mem_glob = mem_glob;
  430. spin_lock_init(&tdev->object_lock);
  431. atomic_set(&tdev->object_count, 0);
  432. ret = drm_ht_create(&tdev->object_hash, hash_order);
  433. if (ret != 0)
  434. goto out_no_object_hash;
  435. tdev->ops = *ops;
  436. tdev->dmabuf_release = tdev->ops.release;
  437. tdev->ops.release = ttm_prime_dmabuf_release;
  438. tdev->dma_buf_size = ttm_round_pot(sizeof(struct dma_buf)) +
  439. ttm_round_pot(sizeof(struct file));
  440. return tdev;
  441. out_no_object_hash:
  442. kfree(tdev);
  443. return NULL;
  444. }
  445. EXPORT_SYMBOL(ttm_object_device_init);
  446. void ttm_object_device_release(struct ttm_object_device **p_tdev)
  447. {
  448. struct ttm_object_device *tdev = *p_tdev;
  449. *p_tdev = NULL;
  450. spin_lock(&tdev->object_lock);
  451. drm_ht_remove(&tdev->object_hash);
  452. spin_unlock(&tdev->object_lock);
  453. kfree(tdev);
  454. }
  455. EXPORT_SYMBOL(ttm_object_device_release);
  456. /**
  457. * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
  458. *
  459. * @dma_buf: Non-refcounted pointer to a struct dma-buf.
  460. *
  461. * Obtain a file reference from a lookup structure that doesn't refcount
  462. * the file, but synchronizes with its release method to make sure it has
  463. * not been freed yet. See for example kref_get_unless_zero documentation.
  464. * Returns true if refcounting succeeds, false otherwise.
  465. *
  466. * Nobody really wants this as a public API yet, so let it mature here
  467. * for some time...
  468. */
  469. static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
  470. {
  471. return atomic_long_inc_not_zero(&dmabuf->file->f_count) != 0L;
  472. }
  473. /**
  474. * ttm_prime_refcount_release - refcount release method for a prime object.
  475. *
  476. * @p_base: Pointer to ttm_base_object pointer.
  477. *
  478. * This is a wrapper that calls the refcount_release founction of the
  479. * underlying object. At the same time it cleans up the prime object.
  480. * This function is called when all references to the base object we
  481. * derive from are gone.
  482. */
  483. static void ttm_prime_refcount_release(struct ttm_base_object **p_base)
  484. {
  485. struct ttm_base_object *base = *p_base;
  486. struct ttm_prime_object *prime;
  487. *p_base = NULL;
  488. prime = container_of(base, struct ttm_prime_object, base);
  489. BUG_ON(prime->dma_buf != NULL);
  490. mutex_destroy(&prime->mutex);
  491. if (prime->refcount_release)
  492. prime->refcount_release(&base);
  493. }
  494. /**
  495. * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
  496. *
  497. * @dma_buf:
  498. *
  499. * This function first calls the dma_buf release method the driver
  500. * provides. Then it cleans up our dma_buf pointer used for lookup,
  501. * and finally releases the reference the dma_buf has on our base
  502. * object.
  503. */
  504. static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf)
  505. {
  506. struct ttm_prime_object *prime =
  507. (struct ttm_prime_object *) dma_buf->priv;
  508. struct ttm_base_object *base = &prime->base;
  509. struct ttm_object_device *tdev = base->tfile->tdev;
  510. if (tdev->dmabuf_release)
  511. tdev->dmabuf_release(dma_buf);
  512. mutex_lock(&prime->mutex);
  513. if (prime->dma_buf == dma_buf)
  514. prime->dma_buf = NULL;
  515. mutex_unlock(&prime->mutex);
  516. ttm_mem_global_free(tdev->mem_glob, tdev->dma_buf_size);
  517. ttm_base_object_unref(&base);
  518. }
  519. /**
  520. * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
  521. *
  522. * @tfile: A struct ttm_object_file identifying the caller.
  523. * @fd: The prime / dmabuf fd.
  524. * @handle: The returned handle.
  525. *
  526. * This function returns a handle to an object that previously exported
  527. * a dma-buf. Note that we don't handle imports yet, because we simply
  528. * have no consumers of that implementation.
  529. */
  530. int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
  531. int fd, u32 *handle)
  532. {
  533. struct ttm_object_device *tdev = tfile->tdev;
  534. struct dma_buf *dma_buf;
  535. struct ttm_prime_object *prime;
  536. struct ttm_base_object *base;
  537. int ret;
  538. dma_buf = dma_buf_get(fd);
  539. if (IS_ERR(dma_buf))
  540. return PTR_ERR(dma_buf);
  541. if (dma_buf->ops != &tdev->ops)
  542. return -ENOSYS;
  543. prime = (struct ttm_prime_object *) dma_buf->priv;
  544. base = &prime->base;
  545. *handle = base->hash.key;
  546. ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
  547. dma_buf_put(dma_buf);
  548. return ret;
  549. }
  550. EXPORT_SYMBOL_GPL(ttm_prime_fd_to_handle);
  551. /**
  552. * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
  553. *
  554. * @tfile: Struct ttm_object_file identifying the caller.
  555. * @handle: Handle to the object we're exporting from.
  556. * @flags: flags for dma-buf creation. We just pass them on.
  557. * @prime_fd: The returned file descriptor.
  558. *
  559. */
  560. int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
  561. uint32_t handle, uint32_t flags,
  562. int *prime_fd)
  563. {
  564. struct ttm_object_device *tdev = tfile->tdev;
  565. struct ttm_base_object *base;
  566. struct dma_buf *dma_buf;
  567. struct ttm_prime_object *prime;
  568. int ret;
  569. base = ttm_base_object_lookup(tfile, handle);
  570. if (unlikely(base == NULL ||
  571. base->object_type != ttm_prime_type)) {
  572. ret = -ENOENT;
  573. goto out_unref;
  574. }
  575. prime = container_of(base, struct ttm_prime_object, base);
  576. if (unlikely(!base->shareable)) {
  577. ret = -EPERM;
  578. goto out_unref;
  579. }
  580. ret = mutex_lock_interruptible(&prime->mutex);
  581. if (unlikely(ret != 0)) {
  582. ret = -ERESTARTSYS;
  583. goto out_unref;
  584. }
  585. dma_buf = prime->dma_buf;
  586. if (!dma_buf || !get_dma_buf_unless_doomed(dma_buf)) {
  587. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  588. exp_info.ops = &tdev->ops;
  589. exp_info.size = prime->size;
  590. exp_info.flags = flags;
  591. exp_info.priv = prime;
  592. /*
  593. * Need to create a new dma_buf, with memory accounting.
  594. */
  595. ret = ttm_mem_global_alloc(tdev->mem_glob, tdev->dma_buf_size,
  596. false, true);
  597. if (unlikely(ret != 0)) {
  598. mutex_unlock(&prime->mutex);
  599. goto out_unref;
  600. }
  601. dma_buf = dma_buf_export(&exp_info);
  602. if (IS_ERR(dma_buf)) {
  603. ret = PTR_ERR(dma_buf);
  604. ttm_mem_global_free(tdev->mem_glob,
  605. tdev->dma_buf_size);
  606. mutex_unlock(&prime->mutex);
  607. goto out_unref;
  608. }
  609. /*
  610. * dma_buf has taken the base object reference
  611. */
  612. base = NULL;
  613. prime->dma_buf = dma_buf;
  614. }
  615. mutex_unlock(&prime->mutex);
  616. ret = dma_buf_fd(dma_buf, flags);
  617. if (ret >= 0) {
  618. *prime_fd = ret;
  619. ret = 0;
  620. } else
  621. dma_buf_put(dma_buf);
  622. out_unref:
  623. if (base)
  624. ttm_base_object_unref(&base);
  625. return ret;
  626. }
  627. EXPORT_SYMBOL_GPL(ttm_prime_handle_to_fd);
  628. /**
  629. * ttm_prime_object_init - Initialize a ttm_prime_object
  630. *
  631. * @tfile: struct ttm_object_file identifying the caller
  632. * @size: The size of the dma_bufs we export.
  633. * @prime: The object to be initialized.
  634. * @shareable: See ttm_base_object_init
  635. * @type: See ttm_base_object_init
  636. * @refcount_release: See ttm_base_object_init
  637. * @ref_obj_release: See ttm_base_object_init
  638. *
  639. * Initializes an object which is compatible with the drm_prime model
  640. * for data sharing between processes and devices.
  641. */
  642. int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size,
  643. struct ttm_prime_object *prime, bool shareable,
  644. enum ttm_object_type type,
  645. void (*refcount_release) (struct ttm_base_object **),
  646. void (*ref_obj_release) (struct ttm_base_object *,
  647. enum ttm_ref_type ref_type))
  648. {
  649. mutex_init(&prime->mutex);
  650. prime->size = PAGE_ALIGN(size);
  651. prime->real_type = type;
  652. prime->dma_buf = NULL;
  653. prime->refcount_release = refcount_release;
  654. return ttm_base_object_init(tfile, &prime->base, shareable,
  655. ttm_prime_type,
  656. ttm_prime_refcount_release,
  657. ref_obj_release);
  658. }
  659. EXPORT_SYMBOL(ttm_prime_object_init);