ttm_object.c 21 KB

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