ttm_object.c 20 KB

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