drm_prime.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright © 2012 Red Hat
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Dave Airlie <airlied@redhat.com>
  25. * Rob Clark <rob.clark@linaro.org>
  26. *
  27. */
  28. #include <linux/export.h>
  29. #include <linux/dma-buf.h>
  30. #include "drmP.h"
  31. /*
  32. * DMA-BUF/GEM Object references and lifetime overview:
  33. *
  34. * On the export the dma_buf holds a reference to the exporting GEM
  35. * object. It takes this reference in handle_to_fd_ioctl, when it
  36. * first calls .prime_export and stores the exporting GEM object in
  37. * the dma_buf priv. This reference is released when the dma_buf
  38. * object goes away in the driver .release function.
  39. *
  40. * On the import the importing GEM object holds a reference to the
  41. * dma_buf (which in turn holds a ref to the exporting GEM object).
  42. * It takes that reference in the fd_to_handle ioctl.
  43. * It calls dma_buf_get, creates an attachment to it and stores the
  44. * attachment in the GEM object. When this attachment is destroyed
  45. * when the imported object is destroyed, we remove the attachment
  46. * and drop the reference to the dma_buf.
  47. *
  48. * Thus the chain of references always flows in one direction
  49. * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
  50. *
  51. * Self-importing: if userspace is using PRIME as a replacement for flink
  52. * then it will get a fd->handle request for a GEM object that it created.
  53. * Drivers should detect this situation and return back the gem object
  54. * from the dma-buf private.
  55. */
  56. struct drm_prime_member {
  57. struct list_head entry;
  58. struct dma_buf *dma_buf;
  59. uint32_t handle;
  60. };
  61. int drm_gem_prime_handle_to_fd(struct drm_device *dev,
  62. struct drm_file *file_priv, uint32_t handle, uint32_t flags,
  63. int *prime_fd)
  64. {
  65. struct drm_gem_object *obj;
  66. void *buf;
  67. int ret;
  68. obj = drm_gem_object_lookup(dev, file_priv, handle);
  69. if (!obj)
  70. return -ENOENT;
  71. mutex_lock(&file_priv->prime.lock);
  72. /* re-export the original imported object */
  73. if (obj->import_attach) {
  74. get_dma_buf(obj->import_attach->dmabuf);
  75. *prime_fd = dma_buf_fd(obj->import_attach->dmabuf, flags);
  76. drm_gem_object_unreference_unlocked(obj);
  77. mutex_unlock(&file_priv->prime.lock);
  78. return 0;
  79. }
  80. if (obj->export_dma_buf) {
  81. get_dma_buf(obj->export_dma_buf);
  82. *prime_fd = dma_buf_fd(obj->export_dma_buf, flags);
  83. drm_gem_object_unreference_unlocked(obj);
  84. } else {
  85. buf = dev->driver->gem_prime_export(dev, obj, flags);
  86. if (IS_ERR(buf)) {
  87. /* normally the created dma-buf takes ownership of the ref,
  88. * but if that fails then drop the ref
  89. */
  90. drm_gem_object_unreference_unlocked(obj);
  91. mutex_unlock(&file_priv->prime.lock);
  92. return PTR_ERR(buf);
  93. }
  94. obj->export_dma_buf = buf;
  95. *prime_fd = dma_buf_fd(buf, flags);
  96. }
  97. /* if we've exported this buffer the cheat and add it to the import list
  98. * so we get the correct handle back
  99. */
  100. ret = drm_prime_add_imported_buf_handle(&file_priv->prime,
  101. obj->export_dma_buf, handle);
  102. if (ret) {
  103. drm_gem_object_unreference_unlocked(obj);
  104. mutex_unlock(&file_priv->prime.lock);
  105. return ret;
  106. }
  107. mutex_unlock(&file_priv->prime.lock);
  108. return 0;
  109. }
  110. EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
  111. int drm_gem_prime_fd_to_handle(struct drm_device *dev,
  112. struct drm_file *file_priv, int prime_fd, uint32_t *handle)
  113. {
  114. struct dma_buf *dma_buf;
  115. struct drm_gem_object *obj;
  116. int ret;
  117. dma_buf = dma_buf_get(prime_fd);
  118. if (IS_ERR(dma_buf))
  119. return PTR_ERR(dma_buf);
  120. mutex_lock(&file_priv->prime.lock);
  121. ret = drm_prime_lookup_imported_buf_handle(&file_priv->prime,
  122. dma_buf, handle);
  123. if (!ret) {
  124. ret = 0;
  125. goto out_put;
  126. }
  127. /* never seen this one, need to import */
  128. obj = dev->driver->gem_prime_import(dev, dma_buf);
  129. if (IS_ERR(obj)) {
  130. ret = PTR_ERR(obj);
  131. goto out_put;
  132. }
  133. ret = drm_gem_handle_create(file_priv, obj, handle);
  134. drm_gem_object_unreference_unlocked(obj);
  135. if (ret)
  136. goto out_put;
  137. ret = drm_prime_add_imported_buf_handle(&file_priv->prime,
  138. dma_buf, *handle);
  139. if (ret)
  140. goto fail;
  141. mutex_unlock(&file_priv->prime.lock);
  142. return 0;
  143. fail:
  144. /* hmm, if driver attached, we are relying on the free-object path
  145. * to detach.. which seems ok..
  146. */
  147. drm_gem_object_handle_unreference_unlocked(obj);
  148. out_put:
  149. dma_buf_put(dma_buf);
  150. mutex_unlock(&file_priv->prime.lock);
  151. return ret;
  152. }
  153. EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
  154. int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  155. struct drm_file *file_priv)
  156. {
  157. struct drm_prime_handle *args = data;
  158. uint32_t flags;
  159. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  160. return -EINVAL;
  161. if (!dev->driver->prime_handle_to_fd)
  162. return -ENOSYS;
  163. /* check flags are valid */
  164. if (args->flags & ~DRM_CLOEXEC)
  165. return -EINVAL;
  166. /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
  167. flags = args->flags & DRM_CLOEXEC;
  168. return dev->driver->prime_handle_to_fd(dev, file_priv,
  169. args->handle, flags, &args->fd);
  170. }
  171. int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  172. struct drm_file *file_priv)
  173. {
  174. struct drm_prime_handle *args = data;
  175. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  176. return -EINVAL;
  177. if (!dev->driver->prime_fd_to_handle)
  178. return -ENOSYS;
  179. return dev->driver->prime_fd_to_handle(dev, file_priv,
  180. args->fd, &args->handle);
  181. }
  182. /*
  183. * drm_prime_pages_to_sg
  184. *
  185. * this helper creates an sg table object from a set of pages
  186. * the driver is responsible for mapping the pages into the
  187. * importers address space
  188. */
  189. struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
  190. {
  191. struct sg_table *sg = NULL;
  192. struct scatterlist *iter;
  193. int i;
  194. int ret;
  195. sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  196. if (!sg)
  197. goto out;
  198. ret = sg_alloc_table(sg, nr_pages, GFP_KERNEL);
  199. if (ret)
  200. goto out;
  201. for_each_sg(sg->sgl, iter, nr_pages, i)
  202. sg_set_page(iter, pages[i], PAGE_SIZE, 0);
  203. return sg;
  204. out:
  205. kfree(sg);
  206. return NULL;
  207. }
  208. EXPORT_SYMBOL(drm_prime_pages_to_sg);
  209. /* export an sg table into an array of pages and addresses
  210. this is currently required by the TTM driver in order to do correct fault
  211. handling */
  212. int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
  213. dma_addr_t *addrs, int max_pages)
  214. {
  215. unsigned count;
  216. struct scatterlist *sg;
  217. struct page *page;
  218. u32 len, offset;
  219. int pg_index;
  220. dma_addr_t addr;
  221. pg_index = 0;
  222. for_each_sg(sgt->sgl, sg, sgt->nents, count) {
  223. len = sg->length;
  224. offset = sg->offset;
  225. page = sg_page(sg);
  226. addr = sg_dma_address(sg);
  227. while (len > 0) {
  228. if (WARN_ON(pg_index >= max_pages))
  229. return -1;
  230. pages[pg_index] = page;
  231. if (addrs)
  232. addrs[pg_index] = addr;
  233. page++;
  234. addr += PAGE_SIZE;
  235. len -= PAGE_SIZE;
  236. pg_index++;
  237. }
  238. }
  239. return 0;
  240. }
  241. EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
  242. /* helper function to cleanup a GEM/prime object */
  243. void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
  244. {
  245. struct dma_buf_attachment *attach;
  246. struct dma_buf *dma_buf;
  247. attach = obj->import_attach;
  248. if (sg)
  249. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  250. dma_buf = attach->dmabuf;
  251. dma_buf_detach(attach->dmabuf, attach);
  252. /* remove the reference */
  253. dma_buf_put(dma_buf);
  254. }
  255. EXPORT_SYMBOL(drm_prime_gem_destroy);
  256. void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
  257. {
  258. INIT_LIST_HEAD(&prime_fpriv->head);
  259. mutex_init(&prime_fpriv->lock);
  260. }
  261. EXPORT_SYMBOL(drm_prime_init_file_private);
  262. void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
  263. {
  264. struct drm_prime_member *member, *safe;
  265. list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
  266. list_del(&member->entry);
  267. kfree(member);
  268. }
  269. }
  270. EXPORT_SYMBOL(drm_prime_destroy_file_private);
  271. int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
  272. {
  273. struct drm_prime_member *member;
  274. member = kmalloc(sizeof(*member), GFP_KERNEL);
  275. if (!member)
  276. return -ENOMEM;
  277. member->dma_buf = dma_buf;
  278. member->handle = handle;
  279. list_add(&member->entry, &prime_fpriv->head);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(drm_prime_add_imported_buf_handle);
  283. int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
  284. {
  285. struct drm_prime_member *member;
  286. list_for_each_entry(member, &prime_fpriv->head, entry) {
  287. if (member->dma_buf == dma_buf) {
  288. *handle = member->handle;
  289. return 0;
  290. }
  291. }
  292. return -ENOENT;
  293. }
  294. EXPORT_SYMBOL(drm_prime_lookup_imported_buf_handle);
  295. void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
  296. {
  297. struct drm_prime_member *member, *safe;
  298. mutex_lock(&prime_fpriv->lock);
  299. list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
  300. if (member->dma_buf == dma_buf) {
  301. list_del(&member->entry);
  302. kfree(member);
  303. }
  304. }
  305. mutex_unlock(&prime_fpriv->lock);
  306. }
  307. EXPORT_SYMBOL(drm_prime_remove_imported_buf_handle);