ttm_object.c 19 KB

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