ttm_object.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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_module.h>
  59. #include <linux/list.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/slab.h>
  62. #include <linux/atomic.h>
  63. #include "ttm_object.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. 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. ttm_object_file_unref(&base->tfile);
  188. if (base->refcount_release)
  189. base->refcount_release(&base);
  190. }
  191. void ttm_base_object_unref(struct ttm_base_object **p_base)
  192. {
  193. struct ttm_base_object *base = *p_base;
  194. *p_base = NULL;
  195. kref_put(&base->refcount, ttm_release_base);
  196. }
  197. struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
  198. uint32_t key)
  199. {
  200. struct ttm_base_object *base = NULL;
  201. struct drm_hash_item *hash;
  202. struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
  203. int ret;
  204. rcu_read_lock();
  205. ret = drm_ht_find_item_rcu(ht, key, &hash);
  206. if (likely(ret == 0)) {
  207. base = drm_hash_entry(hash, struct ttm_ref_object, hash)->obj;
  208. if (!kref_get_unless_zero(&base->refcount))
  209. base = NULL;
  210. }
  211. rcu_read_unlock();
  212. return base;
  213. }
  214. struct ttm_base_object *
  215. ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key)
  216. {
  217. struct ttm_base_object *base = NULL;
  218. struct drm_hash_item *hash;
  219. struct drm_open_hash *ht = &tdev->object_hash;
  220. int ret;
  221. rcu_read_lock();
  222. ret = drm_ht_find_item_rcu(ht, key, &hash);
  223. if (likely(ret == 0)) {
  224. base = drm_hash_entry(hash, struct ttm_base_object, hash);
  225. if (!kref_get_unless_zero(&base->refcount))
  226. base = NULL;
  227. }
  228. rcu_read_unlock();
  229. return base;
  230. }
  231. /**
  232. * ttm_ref_object_exists - Check whether a caller has a valid ref object
  233. * (has opened) a base object.
  234. *
  235. * @tfile: Pointer to a struct ttm_object_file identifying the caller.
  236. * @base: Pointer to a struct base object.
  237. *
  238. * Checks wether the caller identified by @tfile has put a valid USAGE
  239. * reference object on the base object identified by @base.
  240. */
  241. bool ttm_ref_object_exists(struct ttm_object_file *tfile,
  242. struct ttm_base_object *base)
  243. {
  244. struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
  245. struct drm_hash_item *hash;
  246. struct ttm_ref_object *ref;
  247. rcu_read_lock();
  248. if (unlikely(drm_ht_find_item_rcu(ht, base->hash.key, &hash) != 0))
  249. goto out_false;
  250. /*
  251. * Verify that the ref object is really pointing to our base object.
  252. * Our base object could actually be dead, and the ref object pointing
  253. * to another base object with the same handle.
  254. */
  255. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  256. if (unlikely(base != ref->obj))
  257. goto out_false;
  258. /*
  259. * Verify that the ref->obj pointer was actually valid!
  260. */
  261. rmb();
  262. if (unlikely(kref_read(&ref->kref) == 0))
  263. goto out_false;
  264. rcu_read_unlock();
  265. return true;
  266. out_false:
  267. rcu_read_unlock();
  268. return false;
  269. }
  270. int ttm_ref_object_add(struct ttm_object_file *tfile,
  271. struct ttm_base_object *base,
  272. enum ttm_ref_type ref_type, bool *existed,
  273. bool require_existed)
  274. {
  275. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  276. struct ttm_ref_object *ref;
  277. struct drm_hash_item *hash;
  278. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  279. struct ttm_operation_ctx ctx = {
  280. .interruptible = false,
  281. .no_wait_gpu = false
  282. };
  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. if (require_existed)
  300. return -EPERM;
  301. ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
  302. &ctx);
  303. if (unlikely(ret != 0))
  304. return ret;
  305. ref = kmalloc(sizeof(*ref), GFP_KERNEL);
  306. if (unlikely(ref == NULL)) {
  307. ttm_mem_global_free(mem_glob, sizeof(*ref));
  308. return -ENOMEM;
  309. }
  310. ref->hash.key = base->hash.key;
  311. ref->obj = base;
  312. ref->tfile = tfile;
  313. ref->ref_type = ref_type;
  314. kref_init(&ref->kref);
  315. spin_lock(&tfile->lock);
  316. ret = drm_ht_insert_item_rcu(ht, &ref->hash);
  317. if (likely(ret == 0)) {
  318. list_add_tail(&ref->head, &tfile->ref_list);
  319. kref_get(&base->refcount);
  320. spin_unlock(&tfile->lock);
  321. if (existed != NULL)
  322. *existed = false;
  323. break;
  324. }
  325. spin_unlock(&tfile->lock);
  326. BUG_ON(ret != -EINVAL);
  327. ttm_mem_global_free(mem_glob, sizeof(*ref));
  328. kfree(ref);
  329. }
  330. return ret;
  331. }
  332. static void __releases(tfile->lock) __acquires(tfile->lock)
  333. ttm_ref_object_release(struct kref *kref)
  334. {
  335. struct ttm_ref_object *ref =
  336. container_of(kref, struct ttm_ref_object, kref);
  337. struct ttm_base_object *base = ref->obj;
  338. struct ttm_object_file *tfile = ref->tfile;
  339. struct drm_open_hash *ht;
  340. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  341. ht = &tfile->ref_hash[ref->ref_type];
  342. (void)drm_ht_remove_item_rcu(ht, &ref->hash);
  343. list_del(&ref->head);
  344. spin_unlock(&tfile->lock);
  345. if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
  346. base->ref_obj_release(base, ref->ref_type);
  347. ttm_base_object_unref(&ref->obj);
  348. ttm_mem_global_free(mem_glob, sizeof(*ref));
  349. kfree_rcu(ref, rcu_head);
  350. spin_lock(&tfile->lock);
  351. }
  352. int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
  353. unsigned long key, enum ttm_ref_type ref_type)
  354. {
  355. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  356. struct ttm_ref_object *ref;
  357. struct drm_hash_item *hash;
  358. int ret;
  359. spin_lock(&tfile->lock);
  360. ret = drm_ht_find_item(ht, key, &hash);
  361. if (unlikely(ret != 0)) {
  362. spin_unlock(&tfile->lock);
  363. return -EINVAL;
  364. }
  365. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  366. kref_put(&ref->kref, ttm_ref_object_release);
  367. spin_unlock(&tfile->lock);
  368. return 0;
  369. }
  370. void ttm_object_file_release(struct ttm_object_file **p_tfile)
  371. {
  372. struct ttm_ref_object *ref;
  373. struct list_head *list;
  374. unsigned int i;
  375. struct ttm_object_file *tfile = *p_tfile;
  376. *p_tfile = NULL;
  377. spin_lock(&tfile->lock);
  378. /*
  379. * Since we release the lock within the loop, we have to
  380. * restart it from the beginning each time.
  381. */
  382. while (!list_empty(&tfile->ref_list)) {
  383. list = tfile->ref_list.next;
  384. ref = list_entry(list, struct ttm_ref_object, head);
  385. ttm_ref_object_release(&ref->kref);
  386. }
  387. spin_unlock(&tfile->lock);
  388. for (i = 0; i < TTM_REF_NUM; ++i)
  389. drm_ht_remove(&tfile->ref_hash[i]);
  390. ttm_object_file_unref(&tfile);
  391. }
  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. struct ttm_object_device *
  420. ttm_object_device_init(struct ttm_mem_global *mem_glob,
  421. unsigned int hash_order,
  422. const struct dma_buf_ops *ops)
  423. {
  424. struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
  425. int ret;
  426. if (unlikely(tdev == NULL))
  427. return NULL;
  428. tdev->mem_glob = mem_glob;
  429. spin_lock_init(&tdev->object_lock);
  430. atomic_set(&tdev->object_count, 0);
  431. ret = drm_ht_create(&tdev->object_hash, hash_order);
  432. if (ret != 0)
  433. goto out_no_object_hash;
  434. tdev->ops = *ops;
  435. tdev->dmabuf_release = tdev->ops.release;
  436. tdev->ops.release = ttm_prime_dmabuf_release;
  437. tdev->dma_buf_size = ttm_round_pot(sizeof(struct dma_buf)) +
  438. ttm_round_pot(sizeof(struct file));
  439. return tdev;
  440. out_no_object_hash:
  441. kfree(tdev);
  442. return NULL;
  443. }
  444. void ttm_object_device_release(struct ttm_object_device **p_tdev)
  445. {
  446. struct ttm_object_device *tdev = *p_tdev;
  447. *p_tdev = NULL;
  448. drm_ht_remove(&tdev->object_hash);
  449. kfree(tdev);
  450. }
  451. /**
  452. * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
  453. *
  454. * @dma_buf: Non-refcounted pointer to a struct dma-buf.
  455. *
  456. * Obtain a file reference from a lookup structure that doesn't refcount
  457. * the file, but synchronizes with its release method to make sure it has
  458. * not been freed yet. See for example kref_get_unless_zero documentation.
  459. * Returns true if refcounting succeeds, false otherwise.
  460. *
  461. * Nobody really wants this as a public API yet, so let it mature here
  462. * for some time...
  463. */
  464. static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
  465. {
  466. return atomic_long_inc_not_zero(&dmabuf->file->f_count) != 0L;
  467. }
  468. /**
  469. * ttm_prime_refcount_release - refcount release method for a prime object.
  470. *
  471. * @p_base: Pointer to ttm_base_object pointer.
  472. *
  473. * This is a wrapper that calls the refcount_release founction of the
  474. * underlying object. At the same time it cleans up the prime object.
  475. * This function is called when all references to the base object we
  476. * derive from are gone.
  477. */
  478. static void ttm_prime_refcount_release(struct ttm_base_object **p_base)
  479. {
  480. struct ttm_base_object *base = *p_base;
  481. struct ttm_prime_object *prime;
  482. *p_base = NULL;
  483. prime = container_of(base, struct ttm_prime_object, base);
  484. BUG_ON(prime->dma_buf != NULL);
  485. mutex_destroy(&prime->mutex);
  486. if (prime->refcount_release)
  487. prime->refcount_release(&base);
  488. }
  489. /**
  490. * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
  491. *
  492. * @dma_buf:
  493. *
  494. * This function first calls the dma_buf release method the driver
  495. * provides. Then it cleans up our dma_buf pointer used for lookup,
  496. * and finally releases the reference the dma_buf has on our base
  497. * object.
  498. */
  499. static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf)
  500. {
  501. struct ttm_prime_object *prime =
  502. (struct ttm_prime_object *) dma_buf->priv;
  503. struct ttm_base_object *base = &prime->base;
  504. struct ttm_object_device *tdev = base->tfile->tdev;
  505. if (tdev->dmabuf_release)
  506. tdev->dmabuf_release(dma_buf);
  507. mutex_lock(&prime->mutex);
  508. if (prime->dma_buf == dma_buf)
  509. prime->dma_buf = NULL;
  510. mutex_unlock(&prime->mutex);
  511. ttm_mem_global_free(tdev->mem_glob, tdev->dma_buf_size);
  512. ttm_base_object_unref(&base);
  513. }
  514. /**
  515. * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
  516. *
  517. * @tfile: A struct ttm_object_file identifying the caller.
  518. * @fd: The prime / dmabuf fd.
  519. * @handle: The returned handle.
  520. *
  521. * This function returns a handle to an object that previously exported
  522. * a dma-buf. Note that we don't handle imports yet, because we simply
  523. * have no consumers of that implementation.
  524. */
  525. int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
  526. int fd, u32 *handle)
  527. {
  528. struct ttm_object_device *tdev = tfile->tdev;
  529. struct dma_buf *dma_buf;
  530. struct ttm_prime_object *prime;
  531. struct ttm_base_object *base;
  532. int ret;
  533. dma_buf = dma_buf_get(fd);
  534. if (IS_ERR(dma_buf))
  535. return PTR_ERR(dma_buf);
  536. if (dma_buf->ops != &tdev->ops)
  537. return -ENOSYS;
  538. prime = (struct ttm_prime_object *) dma_buf->priv;
  539. base = &prime->base;
  540. *handle = base->hash.key;
  541. ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
  542. dma_buf_put(dma_buf);
  543. return ret;
  544. }
  545. /**
  546. * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
  547. *
  548. * @tfile: Struct ttm_object_file identifying the caller.
  549. * @handle: Handle to the object we're exporting from.
  550. * @flags: flags for dma-buf creation. We just pass them on.
  551. * @prime_fd: The returned file descriptor.
  552. *
  553. */
  554. int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
  555. uint32_t handle, uint32_t flags,
  556. int *prime_fd)
  557. {
  558. struct ttm_object_device *tdev = tfile->tdev;
  559. struct ttm_base_object *base;
  560. struct dma_buf *dma_buf;
  561. struct ttm_prime_object *prime;
  562. int ret;
  563. base = ttm_base_object_lookup(tfile, handle);
  564. if (unlikely(base == NULL ||
  565. base->object_type != ttm_prime_type)) {
  566. ret = -ENOENT;
  567. goto out_unref;
  568. }
  569. prime = container_of(base, struct ttm_prime_object, base);
  570. if (unlikely(!base->shareable)) {
  571. ret = -EPERM;
  572. goto out_unref;
  573. }
  574. ret = mutex_lock_interruptible(&prime->mutex);
  575. if (unlikely(ret != 0)) {
  576. ret = -ERESTARTSYS;
  577. goto out_unref;
  578. }
  579. dma_buf = prime->dma_buf;
  580. if (!dma_buf || !get_dma_buf_unless_doomed(dma_buf)) {
  581. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  582. struct ttm_operation_ctx ctx = {
  583. .interruptible = true,
  584. .no_wait_gpu = false
  585. };
  586. exp_info.ops = &tdev->ops;
  587. exp_info.size = prime->size;
  588. exp_info.flags = flags;
  589. exp_info.priv = prime;
  590. /*
  591. * Need to create a new dma_buf, with memory accounting.
  592. */
  593. ret = ttm_mem_global_alloc(tdev->mem_glob, tdev->dma_buf_size,
  594. &ctx);
  595. if (unlikely(ret != 0)) {
  596. mutex_unlock(&prime->mutex);
  597. goto out_unref;
  598. }
  599. dma_buf = dma_buf_export(&exp_info);
  600. if (IS_ERR(dma_buf)) {
  601. ret = PTR_ERR(dma_buf);
  602. ttm_mem_global_free(tdev->mem_glob,
  603. tdev->dma_buf_size);
  604. mutex_unlock(&prime->mutex);
  605. goto out_unref;
  606. }
  607. /*
  608. * dma_buf has taken the base object reference
  609. */
  610. base = NULL;
  611. prime->dma_buf = dma_buf;
  612. }
  613. mutex_unlock(&prime->mutex);
  614. ret = dma_buf_fd(dma_buf, flags);
  615. if (ret >= 0) {
  616. *prime_fd = ret;
  617. ret = 0;
  618. } else
  619. dma_buf_put(dma_buf);
  620. out_unref:
  621. if (base)
  622. ttm_base_object_unref(&base);
  623. return ret;
  624. }
  625. /**
  626. * ttm_prime_object_init - Initialize a ttm_prime_object
  627. *
  628. * @tfile: struct ttm_object_file identifying the caller
  629. * @size: The size of the dma_bufs we export.
  630. * @prime: The object to be initialized.
  631. * @shareable: See ttm_base_object_init
  632. * @type: See ttm_base_object_init
  633. * @refcount_release: See ttm_base_object_init
  634. * @ref_obj_release: See ttm_base_object_init
  635. *
  636. * Initializes an object which is compatible with the drm_prime model
  637. * for data sharing between processes and devices.
  638. */
  639. int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size,
  640. struct ttm_prime_object *prime, bool shareable,
  641. enum ttm_object_type type,
  642. void (*refcount_release) (struct ttm_base_object **),
  643. void (*ref_obj_release) (struct ttm_base_object *,
  644. enum ttm_ref_type ref_type))
  645. {
  646. mutex_init(&prime->mutex);
  647. prime->size = PAGE_ALIGN(size);
  648. prime->real_type = type;
  649. prime->dma_buf = NULL;
  650. prime->refcount_release = refcount_release;
  651. return ttm_base_object_init(tfile, &prime->base, shareable,
  652. ttm_prime_type,
  653. ttm_prime_refcount_release,
  654. ref_obj_release);
  655. }